1 ;;; ox.el --- Generic Export Engine for Org Mode
3 ;; Copyright (C) 2012-2014 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; This library implements a generic export engine for Org, built on
26 ;; its syntactical parser: Org Elements.
28 ;; Besides that parser, the generic exporter is made of three distinct
31 ;; - The communication channel consists of a property list, which is
32 ;; created and updated during the process. Its use is to offer
33 ;; every piece of information, would it be about initial environment
34 ;; or contextual data, all in a single place.
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 functions is `org-export-as'. It returns the transcoded
48 ;; buffer as a string. Its derivatives are `org-export-to-buffer' and
49 ;; `org-export-to-file'.
51 ;; An export back-end is defined with `org-export-define-backend'.
52 ;; This function can also support specific buffer keywords, OPTION
53 ;; keyword's items and filters. Refer to function's documentation for
56 ;; If the new back-end shares most properties with another one,
57 ;; `org-export-define-derived-backend' can be used to simplify the
60 ;; Any back-end can define its own variables. Among them, those
61 ;; customizable should belong to the `org-export-BACKEND' group.
63 ;; Tools for common tasks across back-ends are implemented in the
64 ;; following part of the file.
66 ;; Eventually, a dispatcher (`org-export-dispatch') is provided in the
69 ;; See <http://orgmode.org/worg/dev/org-export-reference.html> for
74 (eval-when-compile (require 'cl
))
75 (require 'org-element
)
79 (declare-function org-publish
"ox-publish" (project &optional force async
))
80 (declare-function org-publish-all
"ox-publish" (&optional force async
))
82 org-publish-current-file
"ox-publish" (&optional force async
))
83 (declare-function org-publish-current-project
"ox-publish"
84 (&optional force async
))
86 (defvar org-publish-project-alist
)
87 (defvar org-table-number-fraction
)
88 (defvar org-table-number-regexp
)
91 ;;; Internal Variables
93 ;; Among internal variables, the most important is
94 ;; `org-export-options-alist'. This variable define the global export
95 ;; options, shared between every exporter, and how they are acquired.
97 (defconst org-export-max-depth
19
98 "Maximum nesting depth for headlines, counting from 0.")
100 (defconst org-export-options-alist
101 '((:title
"TITLE" nil nil space
)
102 (:date
"DATE" nil nil t
)
103 (:author
"AUTHOR" nil user-full-name t
)
104 (:email
"EMAIL" nil user-mail-address t
)
105 (:description
"DESCRIPTION" nil nil newline
)
106 (:keywords
"KEYWORDS" nil nil space
)
107 (:language
"LANGUAGE" nil org-export-default-language t
)
108 (:select-tags
"SELECT_TAGS" nil org-export-select-tags split
)
109 (:exclude-tags
"EXCLUDE_TAGS" nil org-export-exclude-tags split
)
110 (:creator
"CREATOR" nil org-export-creator-string
)
111 (:headline-levels nil
"H" org-export-headline-levels
)
112 (:preserve-breaks nil
"\\n" org-export-preserve-breaks
)
113 (:section-numbers nil
"num" org-export-with-section-numbers
)
114 (:time-stamp-file nil
"timestamp" org-export-time-stamp-file
)
115 (:with-archived-trees nil
"arch" org-export-with-archived-trees
)
116 (:with-author nil
"author" org-export-with-author
)
117 (:with-clocks nil
"c" org-export-with-clocks
)
118 (:with-creator nil
"creator" org-export-with-creator
)
119 (:with-date nil
"date" org-export-with-date
)
120 (:with-drawers nil
"d" org-export-with-drawers
)
121 (:with-email nil
"email" org-export-with-email
)
122 (:with-emphasize nil
"*" org-export-with-emphasize
)
123 (:with-entities nil
"e" org-export-with-entities
)
124 (:with-fixed-width nil
":" org-export-with-fixed-width
)
125 (:with-footnotes nil
"f" org-export-with-footnotes
)
126 (:with-inlinetasks nil
"inline" org-export-with-inlinetasks
)
127 (:with-latex nil
"tex" org-export-with-latex
)
128 (:with-planning nil
"p" org-export-with-planning
)
129 (:with-priority nil
"pri" org-export-with-priority
)
130 (:with-properties nil
"prop" org-export-with-properties
)
131 (:with-smart-quotes nil
"'" org-export-with-smart-quotes
)
132 (:with-special-strings nil
"-" org-export-with-special-strings
)
133 (:with-statistics-cookies nil
"stat" org-export-with-statistics-cookies
)
134 (:with-sub-superscript nil
"^" org-export-with-sub-superscripts
)
135 (:with-toc nil
"toc" org-export-with-toc
)
136 (:with-tables nil
"|" org-export-with-tables
)
137 (:with-tags nil
"tags" org-export-with-tags
)
138 (:with-tasks nil
"tasks" org-export-with-tasks
)
139 (:with-timestamps nil
"<" org-export-with-timestamps
)
140 (:with-title nil
"title" org-export-with-title
)
141 (:with-todo-keywords nil
"todo" org-export-with-todo-keywords
))
142 "Alist between export properties and ways to set them.
144 The CAR of the alist is the property name, and the CDR is a list
145 like (KEYWORD OPTION DEFAULT BEHAVIOR) where:
147 KEYWORD is a string representing a buffer keyword, or nil. Each
148 property defined this way can also be set, during subtree
149 export, through a headline property named after the keyword
150 with the \"EXPORT_\" prefix (i.e. DATE keyword and EXPORT_DATE
152 OPTION is a string that could be found in an #+OPTIONS: line.
153 DEFAULT is the default value for the property.
154 BEHAVIOR determines how Org should handle multiple keywords for
155 the same property. It is a symbol among:
156 nil Keep old value and discard the new one.
157 t Replace old value with the new one.
158 `space' Concatenate the values, separating them with a space.
159 `newline' Concatenate the values, separating them with
161 `split' Split values at white spaces, and cons them to the
164 Values set through KEYWORD and OPTION have precedence over
167 All these properties should be back-end agnostic. Back-end
168 specific properties are set through `org-export-define-backend'.
169 Properties redefined there have precedence over these.")
171 (defconst org-export-special-keywords
'("FILETAGS" "SETUPFILE" "OPTIONS")
172 "List of in-buffer keywords that require special treatment.
173 These keywords are not directly associated to a property. The
174 way they are handled must be hard-coded into
175 `org-export--get-inbuffer-options' function.")
177 (defconst org-export-document-properties
179 (mapcar (lambda (option)
180 (and (member (nth 1 option
) org-element-document-properties
)
182 org-export-options-alist
))
183 "List of properties containing parsed data.")
185 (defconst org-export-filters-alist
186 '((:filter-body . org-export-filter-body-functions
)
187 (:filter-bold . org-export-filter-bold-functions
)
188 (:filter-babel-call . org-export-filter-babel-call-functions
)
189 (:filter-center-block . org-export-filter-center-block-functions
)
190 (:filter-clock . org-export-filter-clock-functions
)
191 (:filter-code . org-export-filter-code-functions
)
192 (:filter-comment . org-export-filter-comment-functions
)
193 (:filter-comment-block . org-export-filter-comment-block-functions
)
194 (:filter-diary-sexp . org-export-filter-diary-sexp-functions
)
195 (:filter-drawer . org-export-filter-drawer-functions
)
196 (:filter-dynamic-block . org-export-filter-dynamic-block-functions
)
197 (:filter-entity . org-export-filter-entity-functions
)
198 (:filter-example-block . org-export-filter-example-block-functions
)
199 (:filter-export-block . org-export-filter-export-block-functions
)
200 (:filter-export-snippet . org-export-filter-export-snippet-functions
)
201 (:filter-final-output . org-export-filter-final-output-functions
)
202 (:filter-fixed-width . org-export-filter-fixed-width-functions
)
203 (:filter-footnote-definition . org-export-filter-footnote-definition-functions
)
204 (:filter-footnote-reference . org-export-filter-footnote-reference-functions
)
205 (:filter-headline . org-export-filter-headline-functions
)
206 (:filter-horizontal-rule . org-export-filter-horizontal-rule-functions
)
207 (:filter-inline-babel-call . org-export-filter-inline-babel-call-functions
)
208 (:filter-inline-src-block . org-export-filter-inline-src-block-functions
)
209 (:filter-inlinetask . org-export-filter-inlinetask-functions
)
210 (:filter-italic . org-export-filter-italic-functions
)
211 (:filter-item . org-export-filter-item-functions
)
212 (:filter-keyword . org-export-filter-keyword-functions
)
213 (:filter-latex-environment . org-export-filter-latex-environment-functions
)
214 (:filter-latex-fragment . org-export-filter-latex-fragment-functions
)
215 (:filter-line-break . org-export-filter-line-break-functions
)
216 (:filter-link . org-export-filter-link-functions
)
217 (:filter-node-property . org-export-filter-node-property-functions
)
218 (:filter-options . org-export-filter-options-functions
)
219 (:filter-paragraph . org-export-filter-paragraph-functions
)
220 (:filter-parse-tree . org-export-filter-parse-tree-functions
)
221 (:filter-plain-list . org-export-filter-plain-list-functions
)
222 (:filter-plain-text . org-export-filter-plain-text-functions
)
223 (:filter-planning . org-export-filter-planning-functions
)
224 (:filter-property-drawer . org-export-filter-property-drawer-functions
)
225 (:filter-quote-block . org-export-filter-quote-block-functions
)
226 (:filter-radio-target . org-export-filter-radio-target-functions
)
227 (:filter-section . org-export-filter-section-functions
)
228 (:filter-special-block . org-export-filter-special-block-functions
)
229 (:filter-src-block . org-export-filter-src-block-functions
)
230 (:filter-statistics-cookie . org-export-filter-statistics-cookie-functions
)
231 (:filter-strike-through . org-export-filter-strike-through-functions
)
232 (:filter-subscript . org-export-filter-subscript-functions
)
233 (:filter-superscript . org-export-filter-superscript-functions
)
234 (:filter-table . org-export-filter-table-functions
)
235 (:filter-table-cell . org-export-filter-table-cell-functions
)
236 (:filter-table-row . org-export-filter-table-row-functions
)
237 (:filter-target . org-export-filter-target-functions
)
238 (:filter-timestamp . org-export-filter-timestamp-functions
)
239 (:filter-underline . org-export-filter-underline-functions
)
240 (:filter-verbatim . org-export-filter-verbatim-functions
)
241 (:filter-verse-block . org-export-filter-verse-block-functions
))
242 "Alist between filters properties and initial values.
244 The key of each association is a property name accessible through
245 the communication channel. Its value is a configurable global
246 variable defining initial filters.
248 This list is meant to install user specified filters. Back-end
249 developers may install their own filters using
250 `org-export-define-backend'. Filters defined there will always
251 be prepended to the current list, so they always get applied
254 (defconst org-export-default-inline-image-rule
258 '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm"
259 "xpm" "pbm" "pgm" "ppm") t
))))
260 "Default rule for link matching an inline image.
261 This rule applies to links with no description. By default, it
262 will be considered as an inline image if it targets a local file
263 whose extension is either \"png\", \"jpeg\", \"jpg\", \"gif\",
264 \"tiff\", \"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\" or \"ppm\".
265 See `org-export-inline-image-p' for more information about
268 (defconst org-export-ignored-local-variables
269 '(org-font-lock-keywords
270 org-element--cache org-element--cache-objects org-element--cache-sync-keys
271 org-element--cache-sync-requests org-element--cache-sync-timer
)
272 "List of variables not copied through upon buffer duplication.
273 Export process takes place on a copy of the original buffer.
274 When this copy is created, all Org related local variables not in
275 this list are copied to the new buffer. Variables with an
276 unreadable value are also ignored.")
278 (defvar org-export-async-debug nil
279 "Non-nil means asynchronous export process should leave data behind.
281 This data is found in the appropriate \"*Org Export Process*\"
282 buffer, and in files prefixed with \"org-export-process\" and
283 located in `temporary-file-directory'.
285 When non-nil, it will also set `debug-on-error' to a non-nil
286 value in the external process.")
288 (defvar org-export-stack-contents nil
289 "Record asynchronously generated export results and processes.
290 This is an alist: its CAR is the source of the
291 result (destination file or buffer for a finished process,
292 original buffer for a running one) and its CDR is a list
293 containing the back-end used, as a symbol, and either a process
294 or the time at which it finished. It is used to build the menu
295 from `org-export-stack'.")
297 (defvar org-export--registered-backends nil
298 "List of backends currently available in the exporter.
299 This variable is set with `org-export-define-backend' and
300 `org-export-define-derived-backend' functions.")
302 (defvar org-export-dispatch-last-action nil
303 "Last command called from the dispatcher.
304 The value should be a list. Its CAR is the action, as a symbol,
305 and its CDR is a list of export options.")
307 (defvar org-export-dispatch-last-position
(make-marker)
308 "The position where the last export command was created using the dispatcher.
309 This marker will be used with `C-u C-c C-e' to make sure export repetition
310 uses the same subtree if the previous command was restricted to a subtree.")
312 ;; For compatibility with Org < 8
313 (defvar org-export-current-backend nil
314 "Name, if any, of the back-end used during an export process.
316 Its value is a symbol such as `html', `latex', `ascii', or nil if
317 the back-end is anonymous (see `org-export-create-backend') or if
318 there is no export process in progress.
320 It can be used to teach Babel blocks how to act differently
321 according to the back-end used.")
325 ;;; User-configurable Variables
327 ;; Configuration for the masses.
329 ;; They should never be accessed directly, as their value is to be
330 ;; stored in a property list (cf. `org-export-options-alist').
331 ;; Back-ends will read their value from there instead.
333 (defgroup org-export nil
334 "Options for exporting Org mode files."
338 (defgroup org-export-general nil
339 "General options for export engine."
340 :tag
"Org Export General"
343 (defcustom org-export-with-archived-trees
'headline
344 "Whether sub-trees with the ARCHIVE tag should be exported.
346 This can have three different values:
347 nil Do not export, pretend this tree is not present.
348 t Do export the entire tree.
349 `headline' Only export the headline, but skip the tree below it.
351 This option can also be set with the OPTIONS keyword,
353 :group
'org-export-general
355 (const :tag
"Not at all" nil
)
356 (const :tag
"Headline only" headline
)
357 (const :tag
"Entirely" t
)))
359 (defcustom org-export-with-author t
360 "Non-nil means insert author name into the exported file.
361 This option can also be set with the OPTIONS keyword,
362 e.g. \"author:nil\"."
363 :group
'org-export-general
366 (defcustom org-export-with-clocks nil
367 "Non-nil means export CLOCK keywords.
368 This option can also be set with the OPTIONS keyword,
370 :group
'org-export-general
373 (defcustom org-export-with-creator nil
374 "Non-nil means the postamble should contain a creator sentence.
376 The sentence can be set in `org-export-creator-string', which
379 This option can also be set with the OPTIONS keyword, e.g.,
381 :group
'org-export-general
383 :package-version
'(Org .
"8.3")
386 (defcustom org-export-with-date t
387 "Non-nil means insert date in the exported document.
388 This option can also be set with the OPTIONS keyword,
390 :group
'org-export-general
393 (defcustom org-export-date-timestamp-format nil
394 "Time-stamp format string to use for DATE keyword.
396 The format string, when specified, only applies if date consists
397 in a single time-stamp. Otherwise its value will be ignored.
399 See `format-time-string' for details on how to build this
401 :group
'org-export-general
403 (string :tag
"Time-stamp format string")
404 (const :tag
"No format string" nil
)))
406 (defcustom org-export-creator-string
407 (format "Emacs %s (Org mode %s)"
409 (if (fboundp 'org-version
) (org-version) "unknown version"))
410 "Information about the creator of the document.
411 This option can also be set on with the CREATOR keyword."
412 :group
'org-export-general
413 :type
'(string :tag
"Creator string"))
415 (defcustom org-export-with-drawers
'(not "LOGBOOK")
416 "Non-nil means export contents of standard drawers.
418 When t, all drawers are exported. This may also be a list of
419 drawer names to export, as strings. If that list starts with
420 `not', only drawers with such names will be ignored.
422 This variable doesn't apply to properties drawers. See
423 `org-export-with-properties' instead.
425 This option can also be set with the OPTIONS keyword,
427 :group
'org-export-general
429 :package-version
'(Org .
"8.0")
431 (const :tag
"All drawers" t
)
432 (const :tag
"None" nil
)
433 (repeat :tag
"Selected drawers"
434 (string :tag
"Drawer name"))
435 (list :tag
"Ignored drawers"
436 (const :format
"" not
)
437 (repeat :tag
"Specify names of drawers to ignore during export"
439 (string :tag
"Drawer name")))))
441 (defcustom org-export-with-email nil
442 "Non-nil means insert author email into the exported file.
443 This option can also be set with the OPTIONS keyword,
445 :group
'org-export-general
448 (defcustom org-export-with-emphasize t
449 "Non-nil means interpret *word*, /word/, _word_ and +word+.
451 If the export target supports emphasizing text, the word will be
452 typeset in bold, italic, with an underline or strike-through,
455 This option can also be set with the OPTIONS keyword,
457 :group
'org-export-general
460 (defcustom org-export-exclude-tags
'("noexport")
461 "Tags that exclude a tree from export.
463 All trees carrying any of these tags will be excluded from
464 export. This is without condition, so even subtrees inside that
465 carry one of the `org-export-select-tags' will be removed.
467 This option can also be set with the EXCLUDE_TAGS keyword."
468 :group
'org-export-general
469 :type
'(repeat (string :tag
"Tag")))
471 (defcustom org-export-with-fixed-width t
472 "Non-nil means export lines starting with \":\".
473 This option can also be set with the OPTIONS keyword,
475 :group
'org-export-general
477 :package-version
'(Org .
"8.0")
480 (defcustom org-export-with-footnotes t
481 "Non-nil means Org footnotes should be exported.
482 This option can also be set with the OPTIONS keyword,
484 :group
'org-export-general
487 (defcustom org-export-with-latex t
488 "Non-nil means process LaTeX environments and fragments.
490 This option can also be set with the OPTIONS line,
491 e.g. \"tex:verbatim\". Allowed values are:
493 nil Ignore math snippets.
494 `verbatim' Keep everything in verbatim.
495 t Allow export of math snippets."
496 :group
'org-export-general
498 :package-version
'(Org .
"8.0")
500 (const :tag
"Do not process math in any way" nil
)
501 (const :tag
"Interpret math snippets" t
)
502 (const :tag
"Leave math verbatim" verbatim
)))
504 (defcustom org-export-headline-levels
3
505 "The last level which is still exported as a headline.
507 Inferior levels will usually produce itemize or enumerate lists
508 when exported, but back-end behaviour may differ.
510 This option can also be set with the OPTIONS keyword,
512 :group
'org-export-general
515 (defcustom org-export-default-language
"en"
516 "The default language for export and clocktable translations, as a string.
517 This may have an association in
518 `org-clock-clocktable-language-setup',
519 `org-export-smart-quotes-alist' and `org-export-dictionary'.
520 This option can also be set with the LANGUAGE keyword."
521 :group
'org-export-general
522 :type
'(string :tag
"Language"))
524 (defcustom org-export-preserve-breaks nil
525 "Non-nil means preserve all line breaks when exporting.
526 This option can also be set with the OPTIONS keyword,
528 :group
'org-export-general
531 (defcustom org-export-with-entities t
532 "Non-nil means interpret entities when exporting.
534 For example, HTML export converts \\alpha to α and \\AA to
537 For a list of supported names, see the constant `org-entities'
538 and the user option `org-entities-user'.
540 This option can also be set with the OPTIONS keyword,
542 :group
'org-export-general
545 (defcustom org-export-with-inlinetasks t
546 "Non-nil means inlinetasks should be exported.
547 This option can also be set with the OPTIONS keyword,
548 e.g. \"inline:nil\"."
549 :group
'org-export-general
551 :package-version
'(Org .
"8.0")
554 (defcustom org-export-with-planning nil
555 "Non-nil means include planning info in export.
557 Planning info is the line containing either SCHEDULED:,
558 DEADLINE:, CLOSED: time-stamps, or a combination of them.
560 This option can also be set with the OPTIONS keyword,
562 :group
'org-export-general
564 :package-version
'(Org .
"8.0")
567 (defcustom org-export-with-priority nil
568 "Non-nil means include priority cookies in export.
569 This option can also be set with the OPTIONS keyword,
571 :group
'org-export-general
574 (defcustom org-export-with-properties nil
575 "Non-nil means export contents of properties drawers.
577 When t, all properties are exported. This may also be a list of
578 properties to export, as strings.
580 This option can also be set with the OPTIONS keyword,
582 :group
'org-export-general
584 :package-version
'(Org .
"8.3")
586 (const :tag
"All properties" t
)
587 (const :tag
"None" nil
)
588 (repeat :tag
"Selected properties"
589 (string :tag
"Property name"))))
591 (defcustom org-export-with-section-numbers t
592 "Non-nil means add section numbers to headlines when exporting.
594 When set to an integer n, numbering will only happen for
595 headlines whose relative level is higher or equal to n.
597 This option can also be set with the OPTIONS keyword,
599 :group
'org-export-general
602 (defcustom org-export-select-tags
'("export")
603 "Tags that select a tree for export.
605 If any such tag is found in a buffer, all trees that do not carry
606 one of these tags will be ignored during export. Inside trees
607 that are selected like this, you can still deselect a subtree by
608 tagging it with one of the `org-export-exclude-tags'.
610 This option can also be set with the SELECT_TAGS keyword."
611 :group
'org-export-general
612 :type
'(repeat (string :tag
"Tag")))
614 (defcustom org-export-with-smart-quotes nil
615 "Non-nil means activate smart quotes during export.
616 This option can also be set with the OPTIONS keyword,
619 When setting this to non-nil, you need to take care of
620 using the correct Babel package when exporting to LaTeX.
621 E.g., you can load Babel for french like this:
623 #+LATEX_HEADER: \\usepackage[french]{babel}"
624 :group
'org-export-general
626 :package-version
'(Org .
"8.0")
629 (defcustom org-export-with-special-strings t
630 "Non-nil means interpret \"\\-\", \"--\" and \"---\" for export.
632 When this option is turned on, these strings will be exported as:
635 -----+----------+--------+-------
639 ... … \\ldots …
641 This option can also be set with the OPTIONS keyword,
643 :group
'org-export-general
646 (defcustom org-export-with-statistics-cookies t
647 "Non-nil means include statistics cookies in export.
648 This option can also be set with the OPTIONS keyword,
650 :group
'org-export-general
652 :package-version
'(Org .
"8.0")
655 (defcustom org-export-with-sub-superscripts t
656 "Non-nil means interpret \"_\" and \"^\" for export.
658 If you want to control how Org displays those characters, see
659 `org-use-sub-superscripts'. `org-export-with-sub-superscripts'
660 used to be an alias for `org-use-sub-superscripts' in Org <8.0,
663 When this option is turned on, you can use TeX-like syntax for
664 sub- and superscripts and see them exported correctly.
666 You can also set the option with #+OPTIONS: ^:t
668 Several characters after \"_\" or \"^\" will be considered as a
669 single item - so grouping with {} is normally not needed. For
670 example, the following things will be parsed as single sub- or
673 10^24 or 10^tau several digits will be considered 1 item.
674 10^-12 or 10^-tau a leading sign with digits or a word
675 x^2-y^3 will be read as x^2 - y^3, because items are
676 terminated by almost any nonword/nondigit char.
677 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
679 Still, ambiguity is possible. So when in doubt, use {} to enclose
680 the sub/superscript. If you set this variable to the symbol `{}',
681 the braces are *required* in order to trigger interpretations as
682 sub/superscript. This can be helpful in documents that need \"_\"
683 frequently in plain text."
684 :group
'org-export-general
686 :package-version
'(Org .
"8.0")
688 (const :tag
"Interpret them" t
)
689 (const :tag
"Curly brackets only" {})
690 (const :tag
"Do not interpret them" nil
)))
692 (defcustom org-export-with-toc t
693 "Non-nil means create a table of contents in exported files.
695 The TOC contains headlines with levels up
696 to`org-export-headline-levels'. When an integer, include levels
697 up to N in the toc, this may then be different from
698 `org-export-headline-levels', but it will not be allowed to be
699 larger than the number of headline levels. When nil, no table of
702 This option can also be set with the OPTIONS keyword,
703 e.g. \"toc:nil\" or \"toc:3\"."
704 :group
'org-export-general
706 (const :tag
"No Table of Contents" nil
)
707 (const :tag
"Full Table of Contents" t
)
708 (integer :tag
"TOC to level")))
710 (defcustom org-export-with-tables t
711 "Non-nil means export tables.
712 This option can also be set with the OPTIONS keyword,
714 :group
'org-export-general
716 :package-version
'(Org .
"8.0")
719 (defcustom org-export-with-tags t
720 "If nil, do not export tags, just remove them from headlines.
722 If this is the symbol `not-in-toc', tags will be removed from
723 table of contents entries, but still be shown in the headlines of
726 This option can also be set with the OPTIONS keyword,
728 :group
'org-export-general
730 (const :tag
"Off" nil
)
731 (const :tag
"Not in TOC" not-in-toc
)
732 (const :tag
"On" t
)))
734 (defcustom org-export-with-tasks t
735 "Non-nil means include TODO items for export.
737 This may have the following values:
738 t include tasks independent of state.
739 `todo' include only tasks that are not yet done.
740 `done' include only tasks that are already done.
741 nil ignore all tasks.
742 list of keywords include tasks with these keywords.
744 This option can also be set with the OPTIONS keyword,
746 :group
'org-export-general
748 (const :tag
"All tasks" t
)
749 (const :tag
"No tasks" nil
)
750 (const :tag
"Not-done tasks" todo
)
751 (const :tag
"Only done tasks" done
)
752 (repeat :tag
"Specific TODO keywords"
753 (string :tag
"Keyword"))))
755 (defcustom org-export-with-title t
756 "Non-nil means print title into the exported file.
757 This option can also be set with the OPTIONS keyword,
759 :group
'org-export-general
761 :package-version
'(Org .
"8.3")
764 (defcustom org-export-time-stamp-file t
765 "Non-nil means insert a time stamp into the exported file.
766 The time stamp shows when the file was created. This option can
767 also be set with the OPTIONS keyword, e.g. \"timestamp:nil\"."
768 :group
'org-export-general
771 (defcustom org-export-with-timestamps t
772 "Non nil means allow timestamps in export.
774 It can be set to any of the following values:
775 t export all timestamps.
776 `active' export active timestamps only.
777 `inactive' export inactive timestamps only.
778 nil do not export timestamps
780 This only applies to timestamps isolated in a paragraph
781 containing only timestamps. Other timestamps are always
784 This option can also be set with the OPTIONS keyword, e.g.
786 :group
'org-export-general
788 (const :tag
"All timestamps" t
)
789 (const :tag
"Only active timestamps" active
)
790 (const :tag
"Only inactive timestamps" inactive
)
791 (const :tag
"No timestamp" nil
)))
793 (defcustom org-export-with-todo-keywords t
794 "Non-nil means include TODO keywords in export.
795 When nil, remove all these keywords from the export. This option
796 can also be set with the OPTIONS keyword, e.g. \"todo:nil\"."
797 :group
'org-export-general
800 (defcustom org-export-allow-bind-keywords nil
801 "Non-nil means BIND keywords can define local variable values.
802 This is a potential security risk, which is why the default value
803 is nil. You can also allow them through local buffer variables."
804 :group
'org-export-general
806 :package-version
'(Org .
"8.0")
809 (defcustom org-export-snippet-translation-alist nil
810 "Alist between export snippets back-ends and exporter back-ends.
812 This variable allows to provide shortcuts for export snippets.
814 For example, with a value of '\(\(\"h\" . \"html\"\)\), the
815 HTML back-end will recognize the contents of \"@@h:<b>@@\" as
816 HTML code while every other back-end will ignore it."
817 :group
'org-export-general
819 :package-version
'(Org .
"8.0")
821 (cons (string :tag
"Shortcut")
822 (string :tag
"Back-end"))))
824 (defcustom org-export-coding-system nil
825 "Coding system for the exported file."
826 :group
'org-export-general
828 :package-version
'(Org .
"8.0")
829 :type
'coding-system
)
831 (defcustom org-export-copy-to-kill-ring nil
832 "Non-nil means pushing export output to the kill ring.
833 This variable is ignored during asynchronous export."
834 :group
'org-export-general
836 :package-version
'(Org .
"8.3")
838 (const :tag
"Always" t
)
839 (const :tag
"When export is done interactively" if-interactive
)
840 (const :tag
"Never" nil
)))
842 (defcustom org-export-initial-scope
'buffer
843 "The initial scope when exporting with `org-export-dispatch'.
844 This variable can be either set to `buffer' or `subtree'."
845 :group
'org-export-general
847 (const :tag
"Export current buffer" buffer
)
848 (const :tag
"Export current subtree" subtree
)))
850 (defcustom org-export-show-temporary-export-buffer t
851 "Non-nil means show buffer after exporting to temp buffer.
852 When Org exports to a file, the buffer visiting that file is never
853 shown, but remains buried. However, when exporting to
854 a temporary buffer, that buffer is popped up in a second window.
855 When this variable is nil, the buffer remains buried also in
857 :group
'org-export-general
860 (defcustom org-export-in-background nil
861 "Non-nil means export and publishing commands will run in background.
862 Results from an asynchronous export are never displayed
863 automatically. But you can retrieve them with \\[org-export-stack]."
864 :group
'org-export-general
866 :package-version
'(Org .
"8.0")
869 (defcustom org-export-async-init-file nil
870 "File used to initialize external export process.
872 Value must be either nil or an absolute file name. When nil, the
873 external process is launched like a regular Emacs session,
874 loading user's initialization file and any site specific
875 configuration. If a file is provided, it, and only it, is loaded
878 Therefore, using a specific configuration makes the process to
879 load faster and the export more portable."
880 :group
'org-export-general
882 :package-version
'(Org .
"8.0")
884 (const :tag
"Regular startup" nil
)
885 (file :tag
"Specific start-up file" :must-match t
)))
887 (defcustom org-export-dispatch-use-expert-ui nil
888 "Non-nil means using a non-intrusive `org-export-dispatch'.
889 In that case, no help buffer is displayed. Though, an indicator
890 for current export scope is added to the prompt (\"b\" when
891 output is restricted to body only, \"s\" when it is restricted to
892 the current subtree, \"v\" when only visible elements are
893 considered for export, \"f\" when publishing functions should be
894 passed the FORCE argument and \"a\" when the export should be
895 asynchronous). Also, \[?] allows to switch back to standard
897 :group
'org-export-general
899 :package-version
'(Org .
"8.0")
904 ;;; Defining Back-ends
906 ;; An export back-end is a structure with `org-export-backend' type
907 ;; and `name', `parent', `transcoders', `options', `filters', `blocks'
910 ;; At the lowest level, a back-end is created with
911 ;; `org-export-create-backend' function.
913 ;; A named back-end can be registered with
914 ;; `org-export-register-backend' function. A registered back-end can
915 ;; later be referred to by its name, with `org-export-get-backend'
916 ;; function. Also, such a back-end can become the parent of a derived
917 ;; back-end from which slot values will be inherited by default.
918 ;; `org-export-derived-backend-p' can check if a given back-end is
919 ;; derived from a list of back-end names.
921 ;; `org-export-get-all-transcoders', `org-export-get-all-options' and
922 ;; `org-export-get-all-filters' return the full alist of transcoders,
923 ;; options and filters, including those inherited from ancestors.
925 ;; At a higher level, `org-export-define-backend' is the standard way
926 ;; to define an export back-end. If the new back-end is similar to
927 ;; a registered back-end, `org-export-define-derived-backend' may be
930 ;; Eventually `org-export-barf-if-invalid-backend' returns an error
931 ;; when a given back-end hasn't been registered yet.
933 (defstruct (org-export-backend (:constructor org-export-create-backend
)
935 name parent transcoders options filters blocks menu
)
937 (defun org-export-get-backend (name)
938 "Return export back-end named after NAME.
939 NAME is a symbol. Return nil if no such back-end is found."
941 (dolist (b org-export--registered-backends
)
942 (when (eq (org-export-backend-name b
) name
)
945 (defun org-export-register-backend (backend)
946 "Register BACKEND as a known export back-end.
947 BACKEND is a structure with `org-export-backend' type."
948 ;; Refuse to register an unnamed back-end.
949 (unless (org-export-backend-name backend
)
950 (error "Cannot register a unnamed export back-end"))
951 ;; Refuse to register a back-end with an unknown parent.
952 (let ((parent (org-export-backend-parent backend
)))
953 (when (and parent
(not (org-export-get-backend parent
)))
954 (error "Cannot use unknown \"%s\" back-end as a parent" parent
)))
955 ;; Register dedicated export blocks in the parser.
956 (dolist (name (org-export-backend-blocks backend
))
957 (add-to-list 'org-element-block-name-alist
958 (cons name
'org-element-export-block-parser
)))
959 ;; If a back-end with the same name as BACKEND is already
960 ;; registered, replace it with BACKEND. Otherwise, simply add
961 ;; BACKEND to the list of registered back-ends.
962 (let ((old (org-export-get-backend (org-export-backend-name backend
))))
963 (if old
(setcar (memq old org-export--registered-backends
) backend
)
964 (push backend org-export--registered-backends
))))
966 (defun org-export-barf-if-invalid-backend (backend)
967 "Signal an error if BACKEND isn't defined."
968 (unless (org-export-backend-p backend
)
969 (error "Unknown \"%s\" back-end: Aborting export" backend
)))
971 (defun org-export-derived-backend-p (backend &rest backends
)
972 "Non-nil if BACKEND is derived from one of BACKENDS.
973 BACKEND is an export back-end, as returned by, e.g.,
974 `org-export-create-backend', or a symbol referring to
975 a registered back-end. BACKENDS is constituted of symbols."
976 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
979 (while (org-export-backend-parent backend
)
980 (when (memq (org-export-backend-name backend
) backends
)
983 (org-export-get-backend (org-export-backend-parent backend
))))
984 (memq (org-export-backend-name backend
) backends
))))
986 (defun org-export-get-all-transcoders (backend)
987 "Return full translation table for BACKEND.
989 BACKEND is an export back-end, as return by, e.g,,
990 `org-export-create-backend'. Return value is an alist where
991 keys are element or object types, as symbols, and values are
994 Unlike to `org-export-backend-transcoders', this function
995 also returns transcoders inherited from parent back-ends,
997 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
999 (let ((transcoders (org-export-backend-transcoders backend
))
1001 (while (setq parent
(org-export-backend-parent backend
))
1002 (setq backend
(org-export-get-backend parent
))
1004 (append transcoders
(org-export-backend-transcoders backend
))))
1007 (defun org-export-get-all-options (backend)
1008 "Return export options for BACKEND.
1010 BACKEND is an export back-end, as return by, e.g,,
1011 `org-export-create-backend'. See `org-export-options-alist'
1012 for the shape of the return value.
1014 Unlike to `org-export-backend-options', this function also
1015 returns options inherited from parent back-ends, if any."
1016 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
1018 (let ((options (org-export-backend-options backend
))
1020 (while (setq parent
(org-export-backend-parent backend
))
1021 (setq backend
(org-export-get-backend parent
))
1022 (setq options
(append options
(org-export-backend-options backend
))))
1025 (defun org-export-get-all-filters (backend)
1026 "Return complete list of filters for BACKEND.
1028 BACKEND is an export back-end, as return by, e.g,,
1029 `org-export-create-backend'. Return value is an alist where
1030 keys are symbols and values lists of functions.
1032 Unlike to `org-export-backend-filters', this function also
1033 returns filters inherited from parent back-ends, if any."
1034 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
1036 (let ((filters (org-export-backend-filters backend
))
1038 (while (setq parent
(org-export-backend-parent backend
))
1039 (setq backend
(org-export-get-backend parent
))
1040 (setq filters
(append filters
(org-export-backend-filters backend
))))
1043 (defun org-export-define-backend (backend transcoders
&rest body
)
1044 "Define a new back-end BACKEND.
1046 TRANSCODERS is an alist between object or element types and
1047 functions handling them.
1049 These functions should return a string without any trailing
1050 space, or nil. They must accept three arguments: the object or
1051 element itself, its contents or nil when it isn't recursive and
1052 the property list used as a communication channel.
1054 Contents, when not nil, are stripped from any global indentation
1055 \(although the relative one is preserved). They also always end
1056 with a single newline character.
1058 If, for a given type, no function is found, that element or
1059 object type will simply be ignored, along with any blank line or
1060 white space at its end. The same will happen if the function
1061 returns the nil value. If that function returns the empty
1062 string, the type will be ignored, but the blank lines or white
1063 spaces will be kept.
1065 In addition to element and object types, one function can be
1066 associated to the `template' (or `inner-template') symbol and
1067 another one to the `plain-text' symbol.
1069 The former returns the final transcoded string, and can be used
1070 to add a preamble and a postamble to document's body. It must
1071 accept two arguments: the transcoded string and the property list
1072 containing export options. A function associated to `template'
1073 will not be applied if export has option \"body-only\".
1074 A function associated to `inner-template' is always applied.
1076 The latter, when defined, is to be called on every text not
1077 recognized as an element or an object. It must accept two
1078 arguments: the text string and the information channel. It is an
1079 appropriate place to protect special chars relative to the
1082 BODY can start with pre-defined keyword arguments. The following
1083 keywords are understood:
1087 String, or list of strings, representing block names that
1088 will not be parsed. This is used to specify blocks that will
1089 contain raw code specific to the back-end. These blocks
1090 still have to be handled by the relative `export-block' type
1095 Alist between filters and function, or list of functions,
1096 specific to the back-end. See `org-export-filters-alist' for
1097 a list of all allowed filters. Filters defined here
1098 shouldn't make a back-end test, as it may prevent back-ends
1099 derived from this one to behave properly.
1103 Menu entry for the export dispatcher. It should be a list
1106 '(KEY DESCRIPTION-OR-ORDINAL ACTION-OR-MENU)
1110 KEY is a free character selecting the back-end.
1112 DESCRIPTION-OR-ORDINAL is either a string or a number.
1114 If it is a string, is will be used to name the back-end in
1115 its menu entry. If it is a number, the following menu will
1116 be displayed as a sub-menu of the back-end with the same
1117 KEY. Also, the number will be used to determine in which
1118 order such sub-menus will appear (lowest first).
1120 ACTION-OR-MENU is either a function or an alist.
1122 If it is an action, it will be called with four
1123 arguments (booleans): ASYNC, SUBTREEP, VISIBLE-ONLY and
1124 BODY-ONLY. See `org-export-as' for further explanations on
1127 If it is an alist, associations should follow the
1130 '(KEY DESCRIPTION ACTION)
1132 where KEY, DESCRIPTION and ACTION are described above.
1134 Valid values include:
1136 '(?m \"My Special Back-end\" my-special-export-function)
1140 '(?l \"Export to LaTeX\"
1141 \(?p \"As PDF file\" org-latex-export-to-pdf)
1142 \(?o \"As PDF file and open\"
1144 \(if a (org-latex-export-to-pdf t s v b)
1146 \(org-latex-export-to-pdf nil s v b)))))))
1148 or the following, which will be added to the previous
1152 \((?B \"As TEX buffer (Beamer)\" org-beamer-export-as-latex)
1153 \(?P \"As PDF file (Beamer)\" org-beamer-export-to-pdf)))
1157 Alist between back-end specific properties introduced in
1158 communication channel and how their value are acquired. See
1159 `org-export-options-alist' for more information about
1160 structure of the values."
1161 (declare (indent 1))
1162 (let (blocks filters menu-entry options contents
)
1163 (while (keywordp (car body
))
1164 (let ((keyword (pop body
)))
1166 (:export-block
(let ((names (pop body
)))
1167 (setq blocks
(if (consp names
) (mapcar 'upcase names
)
1168 (list (upcase names
))))))
1169 (:filters-alist
(setq filters
(pop body
)))
1170 (:menu-entry
(setq menu-entry
(pop body
)))
1171 (:options-alist
(setq options
(pop body
)))
1172 (t (error "Unknown keyword: %s" keyword
)))))
1173 (org-export-register-backend
1174 (org-export-create-backend :name backend
1175 :transcoders transcoders
1179 :menu menu-entry
))))
1181 (defun org-export-define-derived-backend (child parent
&rest body
)
1182 "Create a new back-end as a variant of an existing one.
1184 CHILD is the name of the derived back-end. PARENT is the name of
1185 the parent back-end.
1187 BODY can start with pre-defined keyword arguments. The following
1188 keywords are understood:
1192 String, or list of strings, representing block names that
1193 will not be parsed. This is used to specify blocks that will
1194 contain raw code specific to the back-end. These blocks
1195 still have to be handled by the relative `export-block' type
1200 Alist of filters that will overwrite or complete filters
1201 defined in PARENT back-end. See `org-export-filters-alist'
1202 for a list of allowed filters.
1206 Menu entry for the export dispatcher. See
1207 `org-export-define-backend' for more information about the
1212 Alist of back-end specific properties that will overwrite or
1213 complete those defined in PARENT back-end. Refer to
1214 `org-export-options-alist' for more information about
1215 structure of the values.
1219 Alist of element and object types and transcoders that will
1220 overwrite or complete transcode table from PARENT back-end.
1221 Refer to `org-export-define-backend' for detailed information
1224 As an example, here is how one could define \"my-latex\" back-end
1225 as a variant of `latex' back-end with a custom template function:
1227 \(org-export-define-derived-backend 'my-latex 'latex
1228 :translate-alist '((template . my-latex-template-fun)))
1230 The back-end could then be called with, for example:
1232 \(org-export-to-buffer 'my-latex \"*Test my-latex*\")"
1233 (declare (indent 2))
1234 (let (blocks filters menu-entry options transcoders contents
)
1235 (while (keywordp (car body
))
1236 (let ((keyword (pop body
)))
1238 (:export-block
(let ((names (pop body
)))
1239 (setq blocks
(if (consp names
) (mapcar 'upcase names
)
1240 (list (upcase names
))))))
1241 (:filters-alist
(setq filters
(pop body
)))
1242 (:menu-entry
(setq menu-entry
(pop body
)))
1243 (:options-alist
(setq options
(pop body
)))
1244 (:translate-alist
(setq transcoders
(pop body
)))
1245 (t (error "Unknown keyword: %s" keyword
)))))
1246 (org-export-register-backend
1247 (org-export-create-backend :name child
1249 :transcoders transcoders
1253 :menu menu-entry
))))
1257 ;;; The Communication Channel
1259 ;; During export process, every function has access to a number of
1260 ;; properties. They are of two types:
1262 ;; 1. Environment options are collected once at the very beginning of
1263 ;; the process, out of the original buffer and configuration.
1264 ;; Collecting them is handled by `org-export-get-environment'
1267 ;; Most environment options are defined through the
1268 ;; `org-export-options-alist' variable.
1270 ;; 2. Tree properties are extracted directly from the parsed tree,
1271 ;; just before export, by `org-export-collect-tree-properties'.
1273 ;;;; Environment Options
1275 ;; Environment options encompass all parameters defined outside the
1276 ;; scope of the parsed data. They come from five sources, in
1277 ;; increasing precedence order:
1279 ;; - Global variables,
1280 ;; - Buffer's attributes,
1281 ;; - Options keyword symbols,
1282 ;; - Buffer keywords,
1283 ;; - Subtree properties.
1285 ;; The central internal function with regards to environment options
1286 ;; is `org-export-get-environment'. It updates global variables with
1287 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
1288 ;; the different sources.
1290 ;; The internal functions doing the retrieval are:
1291 ;; `org-export--get-global-options',
1292 ;; `org-export--get-buffer-attributes',
1293 ;; `org-export--parse-option-keyword',
1294 ;; `org-export--get-subtree-options' and
1295 ;; `org-export--get-inbuffer-options'
1297 ;; Also, `org-export--list-bound-variables' collects bound variables
1298 ;; along with their value in order to set them as buffer local
1299 ;; variables later in the process.
1301 (defun org-export-get-environment (&optional backend subtreep ext-plist
)
1302 "Collect export options from the current buffer.
1304 Optional argument BACKEND is an export back-end, as returned by
1305 `org-export-create-backend'.
1307 When optional argument SUBTREEP is non-nil, assume the export is
1308 done against the current sub-tree.
1310 Third optional argument EXT-PLIST is a property list with
1311 external parameters overriding Org default settings, but still
1312 inferior to file-local settings."
1313 ;; First install #+BIND variables since these must be set before
1314 ;; global options are read.
1315 (dolist (pair (org-export--list-bound-variables))
1316 (org-set-local (car pair
) (nth 1 pair
)))
1317 ;; Get and prioritize export options...
1319 ;; ... from global variables...
1320 (org-export--get-global-options backend
)
1321 ;; ... from an external property list...
1323 ;; ... from in-buffer settings...
1324 (org-export--get-inbuffer-options backend
)
1325 ;; ... and from subtree, when appropriate.
1326 (and subtreep
(org-export--get-subtree-options backend
))
1327 ;; Eventually add misc. properties.
1331 :translate-alist
(org-export-get-all-transcoders backend
)
1332 :footnote-definition-alist
1333 ;; Footnotes definitions must be collected in the original
1334 ;; buffer, as there's no insurance that they will still be in
1335 ;; the parse tree, due to possible narrowing.
1337 (org-with-wide-buffer
1338 (goto-char (point-min))
1339 (while (re-search-forward org-footnote-re nil t
)
1341 (let ((fn (save-match-data (org-element-context))))
1342 (case (org-element-type fn
)
1343 (footnote-definition
1345 (cons (org-element-property :label fn
)
1346 (let ((cbeg (org-element-property :contents-begin fn
)))
1348 (org-element--parse-elements
1349 cbeg
(org-element-property :contents-end fn
)
1350 nil nil nil nil
(list 'org-data nil
)))))
1353 (let ((label (org-element-property :label fn
))
1354 (cbeg (org-element-property :contents-begin fn
)))
1355 (when (and label cbeg
1356 (eq (org-element-property :type fn
) 'inline
))
1359 (org-element-parse-secondary-string
1361 cbeg
(org-element-property :contents-end fn
))
1362 (org-element-restriction 'footnote-reference
)))
1366 ;; Collect id references.
1368 (org-with-wide-buffer
1369 (goto-char (point-min))
1370 (while (re-search-forward "\\[\\[id:\\S-+?\\]" nil t
)
1371 (let ((link (org-element-context)))
1372 (when (eq (org-element-type link
) 'link
)
1373 (let* ((id (org-element-property :path link
))
1374 (file (org-id-find-id-file id
)))
1376 (push (cons id
(file-relative-name file
)) alist
)))))))
1379 (defun org-export--parse-option-keyword (options &optional backend
)
1380 "Parse an OPTIONS line and return values as a plist.
1381 Optional argument BACKEND is an export back-end, as returned by,
1382 e.g., `org-export-create-backend'. It specifies which back-end
1383 specific items to read, if any."
1385 ;; Priority is given to back-end specific options.
1386 (append (and backend
(org-export-get-all-options backend
))
1387 org-export-options-alist
))
1389 (dolist (option all
)
1390 (let ((property (car option
))
1391 (item (nth 2 option
)))
1393 (not (plist-member plist property
))
1394 (string-match (concat "\\(\\`\\|[ \t]\\)"
1396 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
1398 (setq plist
(plist-put plist
1400 (car (read-from-string
1401 (match-string 2 options
))))))))
1404 (defun org-export--get-subtree-options (&optional backend
)
1405 "Get export options in subtree at point.
1406 Optional argument BACKEND is an export back-end, as returned by,
1407 e.g., `org-export-create-backend'. It specifies back-end used
1408 for export. Return options as a plist."
1409 ;; For each buffer keyword, create a headline property setting the
1410 ;; same property in communication channel. The name for the property
1411 ;; is the keyword with "EXPORT_" appended to it.
1412 (org-with-wide-buffer
1414 ;; Make sure point is at a heading.
1415 (if (org-at-heading-p) (org-up-heading-safe) (org-back-to-heading t
))
1416 ;; Take care of EXPORT_TITLE. If it isn't defined, use headline's
1417 ;; title (with no todo keyword, priority cookie or tag) as its
1419 (when (setq prop
(or (org-entry-get (point) "EXPORT_TITLE")
1420 (progn (looking-at org-complex-heading-regexp
)
1421 (org-match-string-no-properties 4))))
1425 (org-element-parse-secondary-string
1426 prop
(org-element-restriction 'keyword
)))))
1427 ;; EXPORT_OPTIONS are parsed in a non-standard way.
1428 (when (setq prop
(org-entry-get (point) "EXPORT_OPTIONS"))
1430 (nconc plist
(org-export--parse-option-keyword prop backend
))))
1431 ;; Handle other keywords. TITLE keyword is excluded as it has
1432 ;; been handled already.
1433 (let ((seen '("TITLE")))
1436 (let ((property (car option
))
1437 (keyword (nth 1 option
)))
1438 (when (and keyword
(not (member keyword seen
)))
1439 (let* ((subtree-prop (concat "EXPORT_" keyword
))
1440 ;; Export properties are not case-sensitive.
1441 (value (let ((case-fold-search t
))
1442 (org-entry-get (point) subtree-prop
))))
1444 (when (and value
(not (plist-member plist property
)))
1450 ;; Parse VALUE if required.
1451 ((member keyword org-element-document-properties
)
1452 (org-element-parse-secondary-string
1453 value
(org-element-restriction 'keyword
)))
1454 ;; If BEHAVIOR is `split' expected value is
1455 ;; a list of strings, not a string.
1456 ((eq (nth 4 option
) 'split
) (org-split-string value
))
1458 ;; Look for both general keywords and back-end specific
1459 ;; options, with priority given to the latter.
1460 (append (and backend
(org-export-get-all-options backend
))
1461 org-export-options-alist
)))
1465 (defun org-export--get-inbuffer-options (&optional backend
)
1466 "Return current buffer export options, as a plist.
1468 Optional argument BACKEND, when non-nil, is an export back-end,
1469 as returned by, e.g., `org-export-create-backend'. It specifies
1470 which back-end specific options should also be read in the
1473 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1475 get-options
; For byte-compiler.
1476 (case-fold-search t
)
1478 ;; Priority is given to back-end specific options.
1479 (and backend
(org-export-get-all-options backend
))
1480 org-export-options-alist
))
1481 (regexp (format "^[ \t]*#\\+%s:"
1482 (regexp-opt (nconc (delq nil
(mapcar 'cadr options
))
1483 org-export-special-keywords
))))
1486 ;; Return all properties associated to KEYWORD.
1488 (dolist (option options properties
)
1489 (when (equal (nth 1 option
) keyword
)
1490 (pushnew (car option
) properties
))))))
1492 (lambda (&optional files plist
)
1493 ;; Recursively read keywords in buffer. FILES is a list
1494 ;; of files read so far. PLIST is the current property
1496 (org-with-wide-buffer
1497 (goto-char (point-min))
1498 (while (re-search-forward regexp nil t
)
1499 (let ((element (org-element-at-point)))
1500 (when (eq (org-element-type element
) 'keyword
)
1501 (let ((key (org-element-property :key element
))
1502 (val (org-element-property :value element
)))
1504 ;; Options in `org-export-special-keywords'.
1505 ((equal key
"SETUPFILE")
1506 (let ((file (expand-file-name
1507 (org-remove-double-quotes (org-trim val
)))))
1508 ;; Avoid circular dependencies.
1509 (unless (member file files
)
1511 (insert (org-file-contents file
'noerror
))
1512 (let ((org-inhibit-startup t
)) (org-mode))
1513 (setq plist
(funcall get-options
1514 (cons file files
) plist
))))))
1515 ((equal key
"OPTIONS")
1519 (org-export--parse-option-keyword val backend
))))
1520 ((equal key
"FILETAGS")
1526 (append (org-split-string val
":")
1527 (plist-get plist
:filetags
)))))))
1529 ;; Options in `org-export-options-alist'.
1530 (dolist (property (funcall find-properties key
))
1531 (let ((behaviour (nth 4 (assq property options
))))
1535 ;; Handle value depending on specified
1539 (if (not (plist-get plist property
))
1541 (concat (plist-get plist property
)
1546 (concat (plist-get plist property
)
1549 (split `(,@(plist-get plist property
)
1550 ,@(org-split-string val
)))
1553 (if (not (plist-member plist property
)) val
1554 (plist-get plist property
))))))))))))))
1555 ;; Return final value.
1557 ;; Read options in the current buffer.
1558 (setq plist
(funcall get-options
1559 (and buffer-file-name
(list buffer-file-name
)) nil
))
1560 ;; Parse keywords specified in `org-element-document-properties'
1561 ;; and return PLIST.
1562 (dolist (keyword org-element-document-properties plist
)
1563 (dolist (property (funcall find-properties keyword
))
1564 (let ((value (plist-get plist property
)))
1565 (when (stringp value
)
1567 (plist-put plist property
1568 (org-element-parse-secondary-string
1569 value
(org-element-restriction 'keyword
))))))))))
1571 (defun org-export--get-buffer-attributes ()
1572 "Return properties related to buffer attributes, as a plist."
1573 (list :input-buffer
(buffer-name (buffer-base-buffer))
1574 :input-file
(buffer-file-name (buffer-base-buffer))))
1576 (defun org-export--get-global-options (&optional backend
)
1577 "Return global export options as a plist.
1578 Optional argument BACKEND, if non-nil, is an export back-end, as
1579 returned by, e.g., `org-export-create-backend'. It specifies
1580 which back-end specific export options should also be read in the
1583 ;; Priority is given to back-end specific options.
1584 (all (append (and backend
(org-export-get-all-options backend
))
1585 org-export-options-alist
)))
1586 (dolist (cell all plist
)
1587 (let ((prop (car cell
)))
1588 (unless (plist-member plist prop
)
1593 ;; Evaluate default value provided. If keyword is
1594 ;; a member of `org-element-document-properties',
1595 ;; parse it as a secondary string before storing it.
1596 (let ((value (eval (nth 3 cell
))))
1597 (if (and (stringp value
)
1598 (member (nth 1 cell
)
1599 org-element-document-properties
))
1600 (org-element-parse-secondary-string
1601 value
(org-element-restriction 'keyword
))
1604 (defun org-export--list-bound-variables ()
1605 "Return variables bound from BIND keywords in current buffer.
1606 Also look for BIND keywords in setup files. The return value is
1607 an alist where associations are (VARIABLE-NAME VALUE)."
1608 (when org-export-allow-bind-keywords
1609 (let* (collect-bind ; For byte-compiler.
1611 (lambda (files alist
)
1612 ;; Return an alist between variable names and their
1613 ;; value. FILES is a list of setup files names read so
1614 ;; far, used to avoid circular dependencies. ALIST is
1615 ;; the alist collected so far.
1616 (let ((case-fold-search t
))
1617 (org-with-wide-buffer
1618 (goto-char (point-min))
1619 (while (re-search-forward
1620 "^[ \t]*#\\+\\(BIND\\|SETUPFILE\\):" nil t
)
1621 (let ((element (org-element-at-point)))
1622 (when (eq (org-element-type element
) 'keyword
)
1623 (let ((val (org-element-property :value element
)))
1624 (if (equal (org-element-property :key element
) "BIND")
1625 (push (read (format "(%s)" val
)) alist
)
1626 ;; Enter setup file.
1627 (let ((file (expand-file-name
1628 (org-remove-double-quotes val
))))
1629 (unless (member file files
)
1631 (let ((org-inhibit-startup t
)) (org-mode))
1632 (insert (org-file-contents file
'noerror
))
1634 (funcall collect-bind
1638 ;; Return value in appropriate order of appearance.
1639 (nreverse (funcall collect-bind nil nil
)))))
1641 ;; defsubst org-export-get-parent must be defined before first use,
1642 ;; was originally defined in the topology section
1644 (defsubst org-export-get-parent
(blob)
1645 "Return BLOB parent or nil.
1646 BLOB is the element or object considered."
1647 (org-element-property :parent blob
))
1649 ;;;; Tree Properties
1651 ;; Tree properties are information extracted from parse tree. They
1652 ;; are initialized at the beginning of the transcoding process by
1653 ;; `org-export-collect-tree-properties'.
1655 ;; Dedicated functions focus on computing the value of specific tree
1656 ;; properties during initialization. Thus,
1657 ;; `org-export--populate-ignore-list' lists elements and objects that
1658 ;; should be skipped during export, `org-export--get-min-level' gets
1659 ;; the minimal exportable level, used as a basis to compute relative
1660 ;; level for headlines. Eventually
1661 ;; `org-export--collect-headline-numbering' builds an alist between
1662 ;; headlines and their numbering.
1664 (defun org-export-collect-tree-properties (data info
)
1665 "Extract tree properties from parse tree.
1667 DATA is the parse tree from which information is retrieved. INFO
1668 is a list holding export options.
1670 Following tree properties are set or updated:
1672 `:exported-data' Hash table used to memoize results from
1675 `:footnote-definition-alist' List of footnotes definitions in
1676 original buffer and current parse tree.
1678 `:headline-offset' Offset between true level of headlines and
1679 local level. An offset of -1 means a headline
1680 of level 2 should be considered as a level
1681 1 headline in the context.
1683 `:headline-numbering' Alist of all headlines as key an the
1684 associated numbering as value.
1686 Return updated plist."
1687 ;; Install the parse tree in the communication channel.
1688 (setq info
(plist-put info
:parse-tree data
))
1689 ;; Compute `:headline-offset' in order to be able to use
1690 ;; `org-export-get-relative-level'.
1694 (- 1 (org-export--get-min-level data info
))))
1695 ;; Footnote definitions in parse tree override those stored in
1696 ;; `:footnote-definition-alist'. This way, any change to
1697 ;; a definition in the parse tree (e.g., through a parse tree
1698 ;; filter) propagates into the alist.
1699 (let ((defs (plist-get info
:footnote-definition-alist
)))
1700 (org-element-map data
'(footnote-definition footnote-reference
)
1702 (cond ((eq (org-element-type fn
) 'footnote-definition
)
1703 (push (cons (org-element-property :label fn
)
1704 (append '(org-data nil
) (org-element-contents fn
)))
1706 ((eq (org-element-property :type fn
) 'inline
)
1707 (push (cons (org-element-property :label fn
)
1708 (org-element-contents fn
))
1710 (setq info
(plist-put info
:footnote-definition-alist defs
)))
1711 ;; Properties order doesn't matter: get the rest of the tree
1714 `(:headline-numbering
,(org-export--collect-headline-numbering data info
)
1715 :unnumbered-headline-id
,(org-export--collect-unnumbered-headline-id data info
)
1716 :exported-data
,(make-hash-table :test
'eq
:size
4001))
1719 (defun org-export--get-min-level (data options
)
1720 "Return minimum exportable headline's level in DATA.
1721 DATA is parsed tree as returned by `org-element-parse-buffer'.
1722 OPTIONS is a plist holding export options."
1724 (let ((min-level 10000))
1727 (when (and (eq (org-element-type blob
) 'headline
)
1728 (not (org-element-property :footnote-section-p blob
))
1729 (not (memq blob
(plist-get options
:ignore-list
))))
1730 (setq min-level
(min (org-element-property :level blob
) min-level
)))
1731 (when (= min-level
1) (throw 'exit
1)))
1732 (org-element-contents data
))
1733 ;; If no headline was found, for the sake of consistency, set
1734 ;; minimum level to 1 nonetheless.
1735 (if (= min-level
10000) 1 min-level
))))
1737 (defun org-export--collect-headline-numbering (data options
)
1738 "Return numbering of all exportable, numbered headlines in a parse tree.
1740 DATA is the parse tree. OPTIONS is the plist holding export
1743 Return an alist whose key is a headline and value is its
1744 associated numbering \(in the shape of a list of numbers\) or nil
1745 for a footnotes section."
1746 (let ((numbering (make-vector org-export-max-depth
0)))
1747 (org-element-map data
'headline
1749 (when (and (org-export-numbered-headline-p headline options
)
1750 (not (org-element-property :footnote-section-p headline
)))
1751 (let ((relative-level
1752 (1- (org-export-get-relative-level headline options
))))
1755 (loop for n across numbering
1756 for idx from
0 to org-export-max-depth
1757 when
(< idx relative-level
) collect n
1758 when
(= idx relative-level
) collect
(aset numbering idx
(1+ n
))
1759 when
(> idx relative-level
) do
(aset numbering idx
0))))))
1762 (defun org-export--collect-unnumbered-headline-id (data options
)
1763 "Return numbering of all exportable, unnumbered headlines.
1764 DATA is the parse tree. OPTIONS is the plist holding export
1765 options. Unnumbered headlines are numbered as a function of
1768 (org-element-map data
'headline
1770 (unless (org-export-numbered-headline-p headline options
)
1771 (list headline
(incf num
)))))))
1773 (defun org-export--selected-trees (data info
)
1774 "List headlines and inlinetasks with a select tag in their tree.
1775 DATA is parsed data as returned by `org-element-parse-buffer'.
1776 INFO is a plist holding export options."
1777 (let* (selected-trees
1778 walk-data
; For byte-compiler.
1781 (lambda (data genealogy
)
1782 (let ((type (org-element-type data
)))
1784 ((memq type
'(headline inlinetask
))
1785 (let ((tags (org-element-property :tags data
)))
1786 (if (loop for tag in
(plist-get info
:select-tags
)
1787 thereis
(member tag tags
))
1788 ;; When a select tag is found, mark full
1789 ;; genealogy and every headline within the tree
1791 (setq selected-trees
1794 (org-element-map data
'(headline inlinetask
)
1797 ;; If at a headline, continue searching in tree,
1799 (when (eq type
'headline
)
1800 (dolist (el (org-element-contents data
))
1801 (funcall walk-data el
(cons data genealogy
)))))))
1802 ((or (eq type
'org-data
)
1803 (memq type org-element-greater-elements
))
1804 (dolist (el (org-element-contents data
))
1805 (funcall walk-data el genealogy
)))))))))
1806 (funcall walk-data data nil
)
1809 (defun org-export--skip-p (blob options selected
)
1810 "Non-nil when element or object BLOB should be skipped during export.
1811 OPTIONS is the plist holding export options. SELECTED, when
1812 non-nil, is a list of headlines or inlinetasks belonging to
1813 a tree with a select tag."
1814 (case (org-element-type blob
)
1815 (clock (not (plist-get options
:with-clocks
)))
1817 (let ((with-drawers-p (plist-get options
:with-drawers
)))
1818 (or (not with-drawers-p
)
1819 (and (consp with-drawers-p
)
1820 ;; If `:with-drawers' value starts with `not', ignore
1821 ;; every drawer whose name belong to that list.
1822 ;; Otherwise, ignore drawers whose name isn't in that
1824 (let ((name (org-element-property :drawer-name blob
)))
1825 (if (eq (car with-drawers-p
) 'not
)
1826 (member-ignore-case name
(cdr with-drawers-p
))
1827 (not (member-ignore-case name with-drawers-p
))))))))
1828 (fixed-width (not (plist-get options
:with-fixed-width
)))
1829 ((footnote-definition footnote-reference
)
1830 (not (plist-get options
:with-footnotes
)))
1831 ((headline inlinetask
)
1832 (let ((with-tasks (plist-get options
:with-tasks
))
1833 (todo (org-element-property :todo-keyword blob
))
1834 (todo-type (org-element-property :todo-type blob
))
1835 (archived (plist-get options
:with-archived-trees
))
1836 (tags (org-element-property :tags blob
)))
1838 (and (eq (org-element-type blob
) 'inlinetask
)
1839 (not (plist-get options
:with-inlinetasks
)))
1840 ;; Ignore subtrees with an exclude tag.
1841 (loop for k in
(plist-get options
:exclude-tags
)
1842 thereis
(member k tags
))
1843 ;; When a select tag is present in the buffer, ignore any tree
1845 (and selected
(not (memq blob selected
)))
1846 ;; Ignore commented sub-trees.
1847 (org-element-property :commentedp blob
)
1848 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
1849 (and (not archived
) (org-element-property :archivedp blob
))
1850 ;; Ignore tasks, if specified by `:with-tasks' property.
1852 (or (not with-tasks
)
1853 (and (memq with-tasks
'(todo done
))
1854 (not (eq todo-type with-tasks
)))
1855 (and (consp with-tasks
) (not (member todo with-tasks
))))))))
1856 ((latex-environment latex-fragment
) (not (plist-get options
:with-latex
)))
1858 (let ((properties-set (plist-get options
:with-properties
)))
1859 (cond ((null properties-set
) t
)
1860 ((consp properties-set
)
1861 (not (member-ignore-case (org-element-property :key blob
)
1862 properties-set
))))))
1863 (planning (not (plist-get options
:with-planning
)))
1864 (property-drawer (not (plist-get options
:with-properties
)))
1865 (statistics-cookie (not (plist-get options
:with-statistics-cookies
)))
1866 (table (not (plist-get options
:with-tables
)))
1868 (and (org-export-table-has-special-column-p
1869 (org-export-get-parent-table blob
))
1870 (org-export-first-sibling-p blob options
)))
1871 (table-row (org-export-table-row-is-special-p blob options
))
1873 ;; `:with-timestamps' only applies to isolated timestamps
1874 ;; objects, i.e. timestamp objects in a paragraph containing only
1875 ;; timestamps and whitespaces.
1876 (when (let ((parent (org-export-get-parent-element blob
)))
1877 (and (memq (org-element-type parent
) '(paragraph verse-block
))
1878 (not (org-element-map parent
1880 (remq 'timestamp org-element-all-objects
))
1882 (or (not (stringp obj
)) (org-string-nw-p obj
)))
1884 (case (plist-get options
:with-timestamps
)
1887 (not (memq (org-element-property :type blob
) '(active active-range
))))
1889 (not (memq (org-element-property :type blob
)
1890 '(inactive inactive-range
)))))))))
1895 ;; `org-export-data' reads a parse tree (obtained with, i.e.
1896 ;; `org-element-parse-buffer') and transcodes it into a specified
1897 ;; back-end output. It takes care of filtering out elements or
1898 ;; objects according to export options and organizing the output blank
1899 ;; lines and white space are preserved. The function memoizes its
1900 ;; results, so it is cheap to call it within transcoders.
1902 ;; It is possible to modify locally the back-end used by
1903 ;; `org-export-data' or even use a temporary back-end by using
1904 ;; `org-export-data-with-backend'.
1906 ;; `org-export-transcoder' is an accessor returning appropriate
1907 ;; translator function for a given element or object.
1909 (defun org-export-transcoder (blob info
)
1910 "Return appropriate transcoder for BLOB.
1911 INFO is a plist containing export directives."
1912 (let ((type (org-element-type blob
)))
1913 ;; Return contents only for complete parse trees.
1914 (if (eq type
'org-data
) (lambda (blob contents info
) contents
)
1915 (let ((transcoder (cdr (assq type
(plist-get info
:translate-alist
)))))
1916 (and (functionp transcoder
) transcoder
)))))
1918 (defun org-export-data (data info
)
1919 "Convert DATA into current back-end format.
1921 DATA is a parse tree, an element or an object or a secondary
1922 string. INFO is a plist holding export options.
1925 (or (gethash data
(plist-get info
:exported-data
))
1926 (let* ((type (org-element-type data
))
1929 ;; Ignored element/object.
1930 ((memq data
(plist-get info
:ignore-list
)) nil
)
1932 ((eq type
'plain-text
)
1933 (org-export-filter-apply-functions
1934 (plist-get info
:filter-plain-text
)
1935 (let ((transcoder (org-export-transcoder data info
)))
1936 (if transcoder
(funcall transcoder data info
) data
))
1938 ;; Secondary string.
1940 (mapconcat (lambda (obj) (org-export-data obj info
)) data
""))
1941 ;; Element/Object without contents or, as a special
1942 ;; case, headline with archive tag and archived trees
1943 ;; restricted to title only.
1944 ((or (not (org-element-contents data
))
1945 (and (eq type
'headline
)
1946 (eq (plist-get info
:with-archived-trees
) 'headline
)
1947 (org-element-property :archivedp data
)))
1948 (let ((transcoder (org-export-transcoder data info
)))
1949 (or (and (functionp transcoder
)
1950 (funcall transcoder data nil info
))
1951 ;; Export snippets never return a nil value so
1952 ;; that white spaces following them are never
1954 (and (eq type
'export-snippet
) ""))))
1955 ;; Element/Object with contents.
1957 (let ((transcoder (org-export-transcoder data info
)))
1959 (let* ((greaterp (memq type org-element-greater-elements
))
1962 (memq type org-element-recursive-objects
)))
1965 (lambda (element) (org-export-data element info
))
1966 (org-element-contents
1967 (if (or greaterp objectp
) data
1968 ;; Elements directly containing
1969 ;; objects must have their indentation
1970 ;; normalized first.
1971 (org-element-normalize-contents
1973 ;; When normalizing contents of the
1974 ;; first paragraph in an item or
1975 ;; a footnote definition, ignore
1976 ;; first line's indentation: there is
1977 ;; none and it might be misleading.
1978 (when (eq type
'paragraph
)
1979 (let ((parent (org-export-get-parent data
)))
1981 (eq (car (org-element-contents parent
))
1983 (memq (org-element-type parent
)
1984 '(footnote-definition item
))))))))
1986 (funcall transcoder data
1987 (if (not greaterp
) contents
1988 (org-element-normalize-string contents
))
1990 ;; Final result will be memoized before being returned.
1995 ((memq type
'(org-data plain-text nil
)) results
)
1996 ;; Append the same white space between elements or objects
1997 ;; as in the original buffer, and call appropriate filters.
2000 (org-export-filter-apply-functions
2001 (plist-get info
(intern (format ":filter-%s" type
)))
2002 (let ((post-blank (or (org-element-property :post-blank data
)
2004 (if (memq type org-element-all-elements
)
2005 (concat (org-element-normalize-string results
)
2006 (make-string post-blank ?
\n))
2007 (concat results
(make-string post-blank ?\s
))))
2010 (plist-get info
:exported-data
)))))
2012 (defun org-export-data-with-backend (data backend info
)
2013 "Convert DATA into BACKEND format.
2015 DATA is an element, an object, a secondary string or a string.
2016 BACKEND is a symbol. INFO is a plist used as a communication
2019 Unlike to `org-export-with-backend', this function will
2020 recursively convert DATA using BACKEND translation table."
2021 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
2024 ;; Set-up a new communication channel with translations defined in
2025 ;; BACKEND as the translate table and a new hash table for
2029 (list :back-end backend
2030 :translate-alist
(org-export-get-all-transcoders backend
)
2031 ;; Size of the hash table is reduced since this function
2032 ;; will probably be used on small trees.
2033 :exported-data
(make-hash-table :test
'eq
:size
401)))))
2035 (defun org-export-prune-tree (data info
)
2036 "Prune non exportable elements from DATA.
2037 DATA is the parse tree to traverse. INFO is the plist holding
2038 export info. Also set `:ignore-list' in INFO to a list of
2039 objects which should be ignored during export, but not removed
2043 ;; First find trees containing a select tag, if any.
2044 (selected (org-export--selected-trees data info
))
2047 ;; Prune non-exportable elements and objects from tree.
2048 ;; As a special case, special rows and cells from tables
2049 ;; are stored in IGNORE, as they still need to be accessed
2051 (let ((type (org-element-type data
)))
2052 (if (org-export--skip-p data info selected
)
2053 (if (memq type
'(table-cell table-row
)) (push data ignore
)
2054 (org-element-extract-element data
))
2055 (if (and (eq type
'headline
)
2056 (eq (plist-get info
:with-archived-trees
) 'headline
)
2057 (org-element-property :archivedp data
))
2058 ;; If headline is archived but tree below has to
2059 ;; be skipped, remove contents.
2060 (org-element-set-contents data
)
2061 ;; Move into secondary string, if any.
2063 (cdr (assq type org-element-secondary-value-alist
))))
2065 (mapc walk-data
(org-element-property sec-prop data
))))
2066 ;; Move into recursive objects/elements.
2067 (mapc walk-data
(org-element-contents data
))))))))
2068 ;; If a select tag is active, also ignore the section before the
2069 ;; first headline, if any.
2071 (let ((first-element (car (org-element-contents data
))))
2072 (when (eq (org-element-type first-element
) 'section
)
2073 (org-element-extract-element first-element
))))
2074 ;; Prune tree and communication channel.
2075 (funcall walk-data data
)
2076 (dolist (prop org-export-document-properties
)
2077 (funcall walk-data
(plist-get info prop
)))
2078 ;; Eventually set `:ignore-list'.
2079 (plist-put info
:ignore-list ignore
)))
2081 (defun org-export-remove-uninterpreted-data (data info
)
2082 "Change uninterpreted elements back into Org syntax.
2083 DATA is the parse tree. INFO is a plist containing export
2084 options. Each uninterpreted element or object is changed back
2085 into a string. Contents, if any, are not modified. The parse
2086 tree is modified by side effect."
2087 (org-export--remove-uninterpreted-data-1 data info
)
2088 (dolist (prop org-export-document-properties
)
2091 (org-export--remove-uninterpreted-data-1
2092 (plist-get info prop
)
2095 (defun org-export--remove-uninterpreted-data-1 (data info
)
2096 "Change uninterpreted elements back into Org syntax.
2097 DATA is a parse tree or a secondary string. INFO is a plist
2098 containing export options. It is modified by side effect and
2099 returned by the function."
2100 (org-element-map data
2101 '(entity bold italic latex-environment latex-fragment strike-through
2102 subscript superscript underline
)
2105 (case (org-element-type blob
)
2108 (and (not (plist-get info
:with-entities
))
2110 (org-export-expand blob nil
)
2112 (or (org-element-property :post-blank blob
) 0)
2115 ((bold italic strike-through underline
)
2116 (and (not (plist-get info
:with-emphasize
))
2117 (let ((marker (case (org-element-type blob
)
2120 (strike-through "+")
2124 (org-element-contents blob
)
2128 (or (org-element-property :post-blank blob
)
2131 ;; ... LaTeX environments and fragments...
2132 ((latex-environment latex-fragment
)
2133 (and (eq (plist-get info
:with-latex
) 'verbatim
)
2134 (list (org-export-expand blob nil
))))
2135 ;; ... sub/superscripts...
2136 ((subscript superscript
)
2137 (let ((sub/super-p
(plist-get info
:with-sub-superscript
))
2138 (bracketp (org-element-property :use-brackets-p blob
)))
2139 (and (or (not sub
/super-p
)
2140 (and (eq sub
/super-p
'{}) (not bracketp
)))
2143 (if (eq (org-element-type blob
) 'subscript
)
2146 (and bracketp
"{")))
2147 (org-element-contents blob
)
2150 (and (org-element-property :post-blank blob
)
2152 (org-element-property :post-blank blob
)
2155 ;; Splice NEW at BLOB location in parse tree.
2156 (dolist (e new
(org-element-extract-element blob
))
2157 (unless (string= e
"") (org-element-insert-before e blob
))))))
2159 ;; Return modified parse tree.
2162 (defun org-export-expand (blob contents
&optional with-affiliated
)
2163 "Expand a parsed element or object to its original state.
2165 BLOB is either an element or an object. CONTENTS is its
2166 contents, as a string or nil.
2168 When optional argument WITH-AFFILIATED is non-nil, add affiliated
2169 keywords before output."
2170 (let ((type (org-element-type blob
)))
2171 (concat (and with-affiliated
(memq type org-element-all-elements
)
2172 (org-element--interpret-affiliated-keywords blob
))
2173 (funcall (intern (format "org-element-%s-interpreter" type
))
2178 ;;; The Filter System
2180 ;; Filters allow end-users to tweak easily the transcoded output.
2181 ;; They are the functional counterpart of hooks, as every filter in
2182 ;; a set is applied to the return value of the previous one.
2184 ;; Every set is back-end agnostic. Although, a filter is always
2185 ;; called, in addition to the string it applies to, with the back-end
2186 ;; used as argument, so it's easy for the end-user to add back-end
2187 ;; specific filters in the set. The communication channel, as
2188 ;; a plist, is required as the third argument.
2190 ;; From the developer side, filters sets can be installed in the
2191 ;; process with the help of `org-export-define-backend', which
2192 ;; internally stores filters as an alist. Each association has a key
2193 ;; among the following symbols and a function or a list of functions
2196 ;; - `:filter-options' applies to the property list containing export
2197 ;; options. Unlike to other filters, functions in this list accept
2198 ;; two arguments instead of three: the property list containing
2199 ;; export options and the back-end. Users can set its value through
2200 ;; `org-export-filter-options-functions' variable.
2202 ;; - `:filter-parse-tree' applies directly to the complete parsed
2203 ;; tree. Users can set it through
2204 ;; `org-export-filter-parse-tree-functions' variable.
2206 ;; - `:filter-body' applies to the body of the output, before template
2207 ;; translator chimes in. Users can set it through
2208 ;; `org-export-filter-body-functions' variable.
2210 ;; - `:filter-final-output' applies to the final transcoded string.
2211 ;; Users can set it with `org-export-filter-final-output-functions'
2214 ;; - `:filter-plain-text' applies to any string not recognized as Org
2215 ;; syntax. `org-export-filter-plain-text-functions' allows users to
2218 ;; - `:filter-TYPE' applies on the string returned after an element or
2219 ;; object of type TYPE has been transcoded. A user can modify
2220 ;; `org-export-filter-TYPE-functions' to install these filters.
2222 ;; All filters sets are applied with
2223 ;; `org-export-filter-apply-functions' function. Filters in a set are
2224 ;; applied in a LIFO fashion. It allows developers to be sure that
2225 ;; their filters will be applied first.
2227 ;; Filters properties are installed in communication channel with
2228 ;; `org-export-install-filters' function.
2230 ;; Eventually, two hooks (`org-export-before-processing-hook' and
2231 ;; `org-export-before-parsing-hook') are run at the beginning of the
2232 ;; export process and just before parsing to allow for heavy structure
2238 (defvar org-export-before-processing-hook nil
2239 "Hook run at the beginning of the export process.
2241 This is run before include keywords and macros are expanded and
2242 Babel code blocks executed, on a copy of the original buffer
2243 being exported. Visibility and narrowing are preserved. Point
2244 is at the beginning of the buffer.
2246 Every function in this hook will be called with one argument: the
2247 back-end currently used, as a symbol.")
2249 (defvar org-export-before-parsing-hook nil
2250 "Hook run before parsing an export buffer.
2252 This is run after include keywords and macros have been expanded
2253 and Babel code blocks executed, on a copy of the original buffer
2254 being exported. Visibility and narrowing are preserved. Point
2255 is at the beginning of the buffer.
2257 Every function in this hook will be called with one argument: the
2258 back-end currently used, as a symbol.")
2261 ;;;; Special Filters
2263 (defvar org-export-filter-options-functions nil
2264 "List of functions applied to the export options.
2265 Each filter is called with two arguments: the export options, as
2266 a plist, and the back-end, as a symbol. It must return
2267 a property list containing export options.")
2269 (defvar org-export-filter-parse-tree-functions nil
2270 "List of functions applied to the parsed tree.
2271 Each filter is called with three arguments: the parse tree, as
2272 returned by `org-element-parse-buffer', the back-end, as
2273 a symbol, and the communication channel, as a plist. It must
2274 return the modified parse tree to transcode.")
2276 (defvar org-export-filter-plain-text-functions nil
2277 "List of functions applied to plain text.
2278 Each filter is called with three arguments: a string which
2279 contains no Org syntax, the back-end, as a symbol, and the
2280 communication channel, as a plist. It must return a string or
2283 (defvar org-export-filter-body-functions nil
2284 "List of functions applied to transcoded body.
2285 Each filter is called with three arguments: a string which
2286 contains no Org syntax, the back-end, as a symbol, and the
2287 communication channel, as a plist. It must return a string or
2290 (defvar org-export-filter-final-output-functions nil
2291 "List of functions applied to the transcoded string.
2292 Each filter is called with three arguments: the full transcoded
2293 string, the back-end, as a symbol, and the communication channel,
2294 as a plist. It must return a string that will be used as the
2295 final export output.")
2298 ;;;; Elements Filters
2300 (defvar org-export-filter-babel-call-functions nil
2301 "List of functions applied to a transcoded babel-call.
2302 Each filter is called with three arguments: the transcoded data,
2303 as a string, the back-end, as a symbol, and the communication
2304 channel, as a plist. It must return a string or nil.")
2306 (defvar org-export-filter-center-block-functions nil
2307 "List of functions applied to a transcoded center block.
2308 Each filter is called with three arguments: the transcoded data,
2309 as a string, the back-end, as a symbol, and the communication
2310 channel, as a plist. It must return a string or nil.")
2312 (defvar org-export-filter-clock-functions nil
2313 "List of functions applied to a transcoded clock.
2314 Each filter is called with three arguments: the transcoded data,
2315 as a string, the back-end, as a symbol, and the communication
2316 channel, as a plist. It must return a string or nil.")
2318 (defvar org-export-filter-comment-functions nil
2319 "List of functions applied to a transcoded comment.
2320 Each filter is called with three arguments: the transcoded data,
2321 as a string, the back-end, as a symbol, and the communication
2322 channel, as a plist. It must return a string or nil.")
2324 (defvar org-export-filter-comment-block-functions nil
2325 "List of functions applied to a transcoded comment-block.
2326 Each filter is called with three arguments: the transcoded data,
2327 as a string, the back-end, as a symbol, and the communication
2328 channel, as a plist. It must return a string or nil.")
2330 (defvar org-export-filter-diary-sexp-functions nil
2331 "List of functions applied to a transcoded diary-sexp.
2332 Each filter is called with three arguments: the transcoded data,
2333 as a string, the back-end, as a symbol, and the communication
2334 channel, as a plist. It must return a string or nil.")
2336 (defvar org-export-filter-drawer-functions nil
2337 "List of functions applied to a transcoded drawer.
2338 Each filter is called with three arguments: the transcoded data,
2339 as a string, the back-end, as a symbol, and the communication
2340 channel, as a plist. It must return a string or nil.")
2342 (defvar org-export-filter-dynamic-block-functions nil
2343 "List of functions applied to a transcoded dynamic-block.
2344 Each filter is called with three arguments: the transcoded data,
2345 as a string, the back-end, as a symbol, and the communication
2346 channel, as a plist. It must return a string or nil.")
2348 (defvar org-export-filter-example-block-functions nil
2349 "List of functions applied to a transcoded example-block.
2350 Each filter is called with three arguments: the transcoded data,
2351 as a string, the back-end, as a symbol, and the communication
2352 channel, as a plist. It must return a string or nil.")
2354 (defvar org-export-filter-export-block-functions nil
2355 "List of functions applied to a transcoded export-block.
2356 Each filter is called with three arguments: the transcoded data,
2357 as a string, the back-end, as a symbol, and the communication
2358 channel, as a plist. It must return a string or nil.")
2360 (defvar org-export-filter-fixed-width-functions nil
2361 "List of functions applied to a transcoded fixed-width.
2362 Each filter is called with three arguments: the transcoded data,
2363 as a string, the back-end, as a symbol, and the communication
2364 channel, as a plist. It must return a string or nil.")
2366 (defvar org-export-filter-footnote-definition-functions nil
2367 "List of functions applied to a transcoded footnote-definition.
2368 Each filter is called with three arguments: the transcoded data,
2369 as a string, the back-end, as a symbol, and the communication
2370 channel, as a plist. It must return a string or nil.")
2372 (defvar org-export-filter-headline-functions nil
2373 "List of functions applied to a transcoded headline.
2374 Each filter is called with three arguments: the transcoded data,
2375 as a string, the back-end, as a symbol, and the communication
2376 channel, as a plist. It must return a string or nil.")
2378 (defvar org-export-filter-horizontal-rule-functions nil
2379 "List of functions applied to a transcoded horizontal-rule.
2380 Each filter is called with three arguments: the transcoded data,
2381 as a string, the back-end, as a symbol, and the communication
2382 channel, as a plist. It must return a string or nil.")
2384 (defvar org-export-filter-inlinetask-functions nil
2385 "List of functions applied to a transcoded inlinetask.
2386 Each filter is called with three arguments: the transcoded data,
2387 as a string, the back-end, as a symbol, and the communication
2388 channel, as a plist. It must return a string or nil.")
2390 (defvar org-export-filter-item-functions nil
2391 "List of functions applied to a transcoded item.
2392 Each filter is called with three arguments: the transcoded data,
2393 as a string, the back-end, as a symbol, and the communication
2394 channel, as a plist. It must return a string or nil.")
2396 (defvar org-export-filter-keyword-functions nil
2397 "List of functions applied to a transcoded keyword.
2398 Each filter is called with three arguments: the transcoded data,
2399 as a string, the back-end, as a symbol, and the communication
2400 channel, as a plist. It must return a string or nil.")
2402 (defvar org-export-filter-latex-environment-functions nil
2403 "List of functions applied to a transcoded latex-environment.
2404 Each filter is called with three arguments: the transcoded data,
2405 as a string, the back-end, as a symbol, and the communication
2406 channel, as a plist. It must return a string or nil.")
2408 (defvar org-export-filter-node-property-functions nil
2409 "List of functions applied to a transcoded node-property.
2410 Each filter is called with three arguments: the transcoded data,
2411 as a string, the back-end, as a symbol, and the communication
2412 channel, as a plist. It must return a string or nil.")
2414 (defvar org-export-filter-paragraph-functions nil
2415 "List of functions applied to a transcoded paragraph.
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-plain-list-functions nil
2421 "List of functions applied to a transcoded plain-list.
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-planning-functions nil
2427 "List of functions applied to a transcoded planning.
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-property-drawer-functions nil
2433 "List of functions applied to a transcoded property-drawer.
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-quote-block-functions nil
2439 "List of functions applied to a transcoded quote block.
2440 Each filter is called with three arguments: the transcoded quote
2441 data, as a string, the back-end, as a symbol, and the
2442 communication channel, as a plist. It must return a string or
2445 (defvar org-export-filter-section-functions nil
2446 "List of functions applied to a transcoded section.
2447 Each filter is called with three arguments: the transcoded data,
2448 as a string, the back-end, as a symbol, and the communication
2449 channel, as a plist. It must return a string or nil.")
2451 (defvar org-export-filter-special-block-functions nil
2452 "List of functions applied to a transcoded special block.
2453 Each filter is called with three arguments: the transcoded data,
2454 as a string, the back-end, as a symbol, and the communication
2455 channel, as a plist. It must return a string or nil.")
2457 (defvar org-export-filter-src-block-functions nil
2458 "List of functions applied to a transcoded src-block.
2459 Each filter is called with three arguments: the transcoded data,
2460 as a string, the back-end, as a symbol, and the communication
2461 channel, as a plist. It must return a string or nil.")
2463 (defvar org-export-filter-table-functions nil
2464 "List of functions applied to a transcoded table.
2465 Each filter is called with three arguments: the transcoded data,
2466 as a string, the back-end, as a symbol, and the communication
2467 channel, as a plist. It must return a string or nil.")
2469 (defvar org-export-filter-table-cell-functions nil
2470 "List of functions applied to a transcoded table-cell.
2471 Each filter is called with three arguments: the transcoded data,
2472 as a string, the back-end, as a symbol, and the communication
2473 channel, as a plist. It must return a string or nil.")
2475 (defvar org-export-filter-table-row-functions nil
2476 "List of functions applied to a transcoded table-row.
2477 Each filter is called with three arguments: the transcoded data,
2478 as a string, the back-end, as a symbol, and the communication
2479 channel, as a plist. It must return a string or nil.")
2481 (defvar org-export-filter-verse-block-functions nil
2482 "List of functions applied to a transcoded verse block.
2483 Each filter is called with three arguments: the transcoded data,
2484 as a string, the back-end, as a symbol, and the communication
2485 channel, as a plist. It must return a string or nil.")
2488 ;;;; Objects Filters
2490 (defvar org-export-filter-bold-functions nil
2491 "List of functions applied to transcoded bold text.
2492 Each filter is called with three arguments: the transcoded data,
2493 as a string, the back-end, as a symbol, and the communication
2494 channel, as a plist. It must return a string or nil.")
2496 (defvar org-export-filter-code-functions nil
2497 "List of functions applied to transcoded code text.
2498 Each filter is called with three arguments: the transcoded data,
2499 as a string, the back-end, as a symbol, and the communication
2500 channel, as a plist. It must return a string or nil.")
2502 (defvar org-export-filter-entity-functions nil
2503 "List of functions applied to a transcoded entity.
2504 Each filter is called with three arguments: the transcoded data,
2505 as a string, the back-end, as a symbol, and the communication
2506 channel, as a plist. It must return a string or nil.")
2508 (defvar org-export-filter-export-snippet-functions nil
2509 "List of functions applied to a transcoded export-snippet.
2510 Each filter is called with three arguments: the transcoded data,
2511 as a string, the back-end, as a symbol, and the communication
2512 channel, as a plist. It must return a string or nil.")
2514 (defvar org-export-filter-footnote-reference-functions nil
2515 "List of functions applied to a transcoded footnote-reference.
2516 Each filter is called with three arguments: the transcoded data,
2517 as a string, the back-end, as a symbol, and the communication
2518 channel, as a plist. It must return a string or nil.")
2520 (defvar org-export-filter-inline-babel-call-functions nil
2521 "List of functions applied to a transcoded inline-babel-call.
2522 Each filter is called with three arguments: the transcoded data,
2523 as a string, the back-end, as a symbol, and the communication
2524 channel, as a plist. It must return a string or nil.")
2526 (defvar org-export-filter-inline-src-block-functions nil
2527 "List of functions applied to a transcoded inline-src-block.
2528 Each filter is called with three arguments: the transcoded data,
2529 as a string, the back-end, as a symbol, and the communication
2530 channel, as a plist. It must return a string or nil.")
2532 (defvar org-export-filter-italic-functions nil
2533 "List of functions applied to transcoded italic text.
2534 Each filter is called with three arguments: the transcoded data,
2535 as a string, the back-end, as a symbol, and the communication
2536 channel, as a plist. It must return a string or nil.")
2538 (defvar org-export-filter-latex-fragment-functions nil
2539 "List of functions applied to a transcoded latex-fragment.
2540 Each filter is called with three arguments: the transcoded data,
2541 as a string, the back-end, as a symbol, and the communication
2542 channel, as a plist. It must return a string or nil.")
2544 (defvar org-export-filter-line-break-functions nil
2545 "List of functions applied to a transcoded line-break.
2546 Each filter is called with three arguments: the transcoded data,
2547 as a string, the back-end, as a symbol, and the communication
2548 channel, as a plist. It must return a string or nil.")
2550 (defvar org-export-filter-link-functions nil
2551 "List of functions applied to a transcoded link.
2552 Each filter is called with three arguments: the transcoded data,
2553 as a string, the back-end, as a symbol, and the communication
2554 channel, as a plist. It must return a string or nil.")
2556 (defvar org-export-filter-radio-target-functions nil
2557 "List of functions applied to a transcoded radio-target.
2558 Each filter is called with three arguments: the transcoded data,
2559 as a string, the back-end, as a symbol, and the communication
2560 channel, as a plist. It must return a string or nil.")
2562 (defvar org-export-filter-statistics-cookie-functions nil
2563 "List of functions applied to a transcoded statistics-cookie.
2564 Each filter is called with three arguments: the transcoded data,
2565 as a string, the back-end, as a symbol, and the communication
2566 channel, as a plist. It must return a string or nil.")
2568 (defvar org-export-filter-strike-through-functions nil
2569 "List of functions applied to transcoded strike-through text.
2570 Each filter is called with three arguments: the transcoded data,
2571 as a string, the back-end, as a symbol, and the communication
2572 channel, as a plist. It must return a string or nil.")
2574 (defvar org-export-filter-subscript-functions nil
2575 "List of functions applied to a transcoded subscript.
2576 Each filter is called with three arguments: the transcoded data,
2577 as a string, the back-end, as a symbol, and the communication
2578 channel, as a plist. It must return a string or nil.")
2580 (defvar org-export-filter-superscript-functions nil
2581 "List of functions applied to a transcoded superscript.
2582 Each filter is called with three arguments: the transcoded data,
2583 as a string, the back-end, as a symbol, and the communication
2584 channel, as a plist. It must return a string or nil.")
2586 (defvar org-export-filter-target-functions nil
2587 "List of functions applied to a transcoded target.
2588 Each filter is called with three arguments: the transcoded data,
2589 as a string, the back-end, as a symbol, and the communication
2590 channel, as a plist. It must return a string or nil.")
2592 (defvar org-export-filter-timestamp-functions nil
2593 "List of functions applied to a transcoded timestamp.
2594 Each filter is called with three arguments: the transcoded data,
2595 as a string, the back-end, as a symbol, and the communication
2596 channel, as a plist. It must return a string or nil.")
2598 (defvar org-export-filter-underline-functions nil
2599 "List of functions applied to transcoded underline text.
2600 Each filter is called with three arguments: the transcoded data,
2601 as a string, the back-end, as a symbol, and the communication
2602 channel, as a plist. It must return a string or nil.")
2604 (defvar org-export-filter-verbatim-functions nil
2605 "List of functions applied to transcoded verbatim text.
2606 Each filter is called with three arguments: the transcoded data,
2607 as a string, the back-end, as a symbol, and the communication
2608 channel, as a plist. It must return a string or nil.")
2613 ;; Internal function `org-export-install-filters' installs filters
2614 ;; hard-coded in back-ends (developer filters) and filters from global
2615 ;; variables (user filters) in the communication channel.
2617 ;; Internal function `org-export-filter-apply-functions' takes care
2618 ;; about applying each filter in order to a given data. It ignores
2619 ;; filters returning a nil value but stops whenever a filter returns
2622 (defun org-export-filter-apply-functions (filters value info
)
2623 "Call every function in FILTERS.
2625 Functions are called with arguments VALUE, current export
2626 back-end's name and INFO. A function returning a nil value will
2627 be skipped. If it returns the empty string, the process ends and
2630 Call is done in a LIFO fashion, to be sure that developer
2631 specified filters, if any, are called first."
2633 (let* ((backend (plist-get info
:back-end
))
2634 (backend-name (and backend
(org-export-backend-name backend
))))
2635 (dolist (filter filters value
)
2636 (let ((result (funcall filter value backend-name info
)))
2637 (cond ((not result
) value
)
2638 ((equal value
"") (throw 'exit nil
))
2639 (t (setq value result
))))))))
2641 (defun org-export-install-filters (info)
2642 "Install filters properties in communication channel.
2643 INFO is a plist containing the current communication channel.
2644 Return the updated communication channel."
2646 ;; Install user-defined filters with `org-export-filters-alist'
2647 ;; and filters already in INFO (through ext-plist mechanism).
2649 (let* ((prop (car p
))
2650 (info-value (plist-get info prop
))
2651 (default-value (symbol-value (cdr p
))))
2653 (plist-put plist prop
2654 ;; Filters in INFO will be called
2655 ;; before those user provided.
2656 (append (if (listp info-value
) info-value
2659 org-export-filters-alist
)
2660 ;; Prepend back-end specific filters to that list.
2662 ;; Single values get consed, lists are appended.
2663 (let ((key (car p
)) (value (cdr p
)))
2668 (if (atom value
) (cons value
(plist-get plist key
))
2669 (append value
(plist-get plist key
))))))))
2670 (org-export-get-all-filters (plist-get info
:back-end
)))
2671 ;; Return new communication channel.
2672 (org-combine-plists info plist
)))
2678 ;; This is the room for the main function, `org-export-as', along with
2679 ;; its derivative, `org-export-string-as'.
2680 ;; `org-export--copy-to-kill-ring-p' determines if output of these
2681 ;; function should be added to kill ring.
2683 ;; Note that `org-export-as' doesn't really parse the current buffer,
2684 ;; but a copy of it (with the same buffer-local variables and
2685 ;; visibility), where macros and include keywords are expanded and
2686 ;; Babel blocks are executed, if appropriate.
2687 ;; `org-export-with-buffer-copy' macro prepares that copy.
2689 ;; File inclusion is taken care of by
2690 ;; `org-export-expand-include-keyword' and
2691 ;; `org-export--prepare-file-contents'. Structure wise, including
2692 ;; a whole Org file in a buffer often makes little sense. For
2693 ;; example, if the file contains a headline and the include keyword
2694 ;; was within an item, the item should contain the headline. That's
2695 ;; why file inclusion should be done before any structure can be
2696 ;; associated to the file, that is before parsing.
2698 ;; `org-export-insert-default-template' is a command to insert
2699 ;; a default template (or a back-end specific template) at point or in
2702 (defun org-export-copy-buffer ()
2703 "Return a copy of the current buffer.
2704 The copy preserves Org buffer-local variables, visibility and
2706 (let ((copy-buffer-fun (org-export--generate-copy-script (current-buffer)))
2707 (new-buf (generate-new-buffer (buffer-name))))
2708 (with-current-buffer new-buf
2709 (funcall copy-buffer-fun
)
2710 (set-buffer-modified-p nil
))
2713 (defmacro org-export-with-buffer-copy
(&rest body
)
2714 "Apply BODY in a copy of the current buffer.
2715 The copy preserves local variables, visibility and contents of
2716 the original buffer. Point is at the beginning of the buffer
2717 when BODY is applied."
2719 (org-with-gensyms (buf-copy)
2720 `(let ((,buf-copy
(org-export-copy-buffer)))
2722 (with-current-buffer ,buf-copy
2723 (goto-char (point-min))
2725 (and (buffer-live-p ,buf-copy
)
2726 ;; Kill copy without confirmation.
2727 (progn (with-current-buffer ,buf-copy
2728 (restore-buffer-modified-p nil
))
2729 (kill-buffer ,buf-copy
)))))))
2731 (defun org-export--generate-copy-script (buffer)
2732 "Generate a function duplicating BUFFER.
2734 The copy will preserve local variables, visibility, contents and
2735 narrowing of the original buffer. If a region was active in
2736 BUFFER, contents will be narrowed to that region instead.
2738 The resulting function can be evaluated at a later time, from
2739 another buffer, effectively cloning the original buffer there.
2741 The function assumes BUFFER's major mode is `org-mode'."
2742 (with-current-buffer buffer
2744 (let ((inhibit-modification-hooks t
))
2745 ;; Set major mode. Ignore `org-mode-hook' as it has been run
2746 ;; already in BUFFER.
2747 (let ((org-mode-hook nil
) (org-inhibit-startup t
)) (org-mode))
2748 ;; Copy specific buffer local variables and variables set
2749 ;; through BIND keywords.
2750 ,@(let ((bound-variables (org-export--list-bound-variables))
2752 (dolist (entry (buffer-local-variables (buffer-base-buffer)) vars
)
2754 (let ((var (car entry
))
2756 (and (not (memq var org-export-ignored-local-variables
))
2760 buffer-file-coding-system
))
2761 (assq var bound-variables
)
2762 (string-match "^\\(org-\\|orgtbl-\\)"
2764 ;; Skip unreadable values, as they cannot be
2765 ;; sent to external process.
2766 (or (not val
) (ignore-errors (read (format "%S" val
))))
2767 (push `(set (make-local-variable (quote ,var
))
2770 ;; Whole buffer contents.
2772 ,(org-with-wide-buffer
2773 (buffer-substring-no-properties
2774 (point-min) (point-max))))
2776 ,(if (org-region-active-p)
2777 `(narrow-to-region ,(region-beginning) ,(region-end))
2778 `(narrow-to-region ,(point-min) ,(point-max)))
2779 ;; Current position of point.
2780 (goto-char ,(point))
2781 ;; Overlays with invisible property.
2785 (let ((invis-prop (overlay-get ov
'invisible
)))
2788 (make-overlay ,(overlay-start ov
)
2790 'invisible
(quote ,invis-prop
))
2792 (overlays-in (point-min) (point-max)))
2796 (defun org-export-as
2797 (backend &optional subtreep visible-only body-only ext-plist
)
2798 "Transcode current Org buffer into BACKEND code.
2800 BACKEND is either an export back-end, as returned by, e.g.,
2801 `org-export-create-backend', or a symbol referring to
2802 a registered back-end.
2804 If narrowing is active in the current buffer, only transcode its
2807 If a region is active, transcode that region.
2809 When optional argument SUBTREEP is non-nil, transcode the
2810 sub-tree at point, extracting information from the headline
2813 When optional argument VISIBLE-ONLY is non-nil, don't export
2814 contents of hidden elements.
2816 When optional argument BODY-ONLY is non-nil, only return body
2817 code, without surrounding template.
2819 Optional argument EXT-PLIST, when provided, is a property list
2820 with external parameters overriding Org default settings, but
2821 still inferior to file-local settings.
2823 Return code as a string."
2824 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
2825 (org-export-barf-if-invalid-backend backend
)
2828 ;; Narrow buffer to an appropriate region or subtree for
2829 ;; parsing. If parsing subtree, be sure to remove main headline
2831 (cond ((org-region-active-p)
2832 (narrow-to-region (region-beginning) (region-end)))
2834 (org-narrow-to-subtree)
2835 (goto-char (point-min))
2837 (narrow-to-region (point) (point-max))))
2838 ;; Initialize communication channel with original buffer
2839 ;; attributes, unavailable in its copy.
2840 (let* ((org-export-current-backend (org-export-backend-name backend
))
2841 (info (org-combine-plists
2842 (list :export-options
2844 (list (and subtreep
'subtree
)
2845 (and visible-only
'visible-only
)
2846 (and body-only
'body-only
))))
2847 (org-export--get-buffer-attributes)))
2849 ;; Update communication channel and get parse tree. Buffer
2850 ;; isn't parsed directly. Instead, a temporary copy is
2851 ;; created, where include keywords, macros are expanded and
2852 ;; code blocks are evaluated.
2853 (org-export-with-buffer-copy
2854 ;; Run first hook with current back-end's name as argument.
2855 (run-hook-with-args 'org-export-before-processing-hook
2856 (org-export-backend-name backend
))
2857 (org-export-expand-include-keyword)
2858 ;; Update macro templates since #+INCLUDE keywords might have
2859 ;; added some new ones.
2860 (org-macro-initialize-templates)
2861 (org-macro-replace-all org-macro-templates
)
2862 (org-export-execute-babel-code)
2863 ;; Update radio targets since keyword inclusion might have
2865 (org-update-radio-target-regexp)
2866 ;; Run last hook with current back-end's name as argument.
2867 (goto-char (point-min))
2869 (run-hook-with-args 'org-export-before-parsing-hook
2870 (org-export-backend-name backend
)))
2871 ;; Update communication channel with environment. Also
2872 ;; install user's and developer's filters.
2874 (org-export-install-filters
2876 info
(org-export-get-environment backend subtreep ext-plist
))))
2877 ;; Call options filters and update export options. We do not
2878 ;; use `org-export-filter-apply-functions' here since the
2879 ;; arity of such filters is different.
2880 (let ((backend-name (org-export-backend-name backend
)))
2881 (dolist (filter (plist-get info
:filter-options
))
2882 (let ((result (funcall filter info backend-name
)))
2883 (when result
(setq info result
)))))
2884 ;; Expand export-specific set of macros: {{{author}}},
2885 ;; {{{date(FORMAT)}}}, {{{email}}} and {{{title}}}. It must
2886 ;; be done once regular macros have been expanded, since
2887 ;; document keywords may contain one of them.
2888 (org-macro-replace-all
2889 (list (cons "author"
2890 (org-element-interpret-data (plist-get info
:author
)))
2892 (let* ((date (plist-get info
:date
))
2893 (value (or (org-element-interpret-data date
) "")))
2894 (if (and (not (cdr date
))
2895 (eq (org-element-type (car date
)) 'timestamp
))
2896 (format "(eval (if (org-string-nw-p \"$1\") %s %S))"
2897 (format "(org-timestamp-format '%S \"$1\")"
2898 (org-element-copy (car date
)))
2901 ;; EMAIL is not a parsed keyword: store it as-is.
2902 (cons "email" (or (plist-get info
:email
) ""))
2904 (org-element-interpret-data (plist-get info
:title
)))
2905 (cons "results" "$1"))
2908 (setq tree
(org-element-parse-buffer nil visible-only
))
2909 ;; Prune tree from non-exported elements and transform
2910 ;; uninterpreted elements or objects in both parse tree and
2911 ;; communication channel.
2912 (org-export-prune-tree tree info
)
2913 (org-export-remove-uninterpreted-data tree info
)
2914 ;; Parse buffer, handle uninterpreted elements or objects,
2915 ;; then call parse-tree filters.
2917 (org-export-filter-apply-functions
2918 (plist-get info
:filter-parse-tree
) tree info
))
2919 ;; Now tree is complete, compute its properties and add them
2920 ;; to communication channel.
2923 info
(org-export-collect-tree-properties tree info
)))
2924 ;; Eventually transcode TREE. Wrap the resulting string into
2926 (let* ((body (org-element-normalize-string
2927 (or (org-export-data tree info
) "")))
2928 (inner-template (cdr (assq 'inner-template
2929 (plist-get info
:translate-alist
))))
2930 (full-body (org-export-filter-apply-functions
2931 (plist-get info
:filter-body
)
2932 (if (not (functionp inner-template
)) body
2933 (funcall inner-template body info
))
2935 (template (cdr (assq 'template
2936 (plist-get info
:translate-alist
)))))
2937 ;; Remove all text properties since they cannot be
2938 ;; retrieved from an external process. Finally call
2939 ;; final-output filter and return result.
2941 (org-export-filter-apply-functions
2942 (plist-get info
:filter-final-output
)
2943 (if (or (not (functionp template
)) body-only
) full-body
2944 (funcall template full-body info
))
2948 (defun org-export-string-as (string backend
&optional body-only ext-plist
)
2949 "Transcode STRING into BACKEND code.
2951 BACKEND is either an export back-end, as returned by, e.g.,
2952 `org-export-create-backend', or a symbol referring to
2953 a registered back-end.
2955 When optional argument BODY-ONLY is non-nil, only return body
2956 code, without preamble nor postamble.
2958 Optional argument EXT-PLIST, when provided, is a property list
2959 with external parameters overriding Org default settings, but
2960 still inferior to file-local settings.
2962 Return code as a string."
2965 (let ((org-inhibit-startup t
)) (org-mode))
2966 (org-export-as backend nil nil body-only ext-plist
)))
2969 (defun org-export-replace-region-by (backend)
2970 "Replace the active region by its export to BACKEND.
2971 BACKEND is either an export back-end, as returned by, e.g.,
2972 `org-export-create-backend', or a symbol referring to
2973 a registered back-end."
2974 (if (not (org-region-active-p))
2975 (user-error "No active region to replace")
2976 (let* ((beg (region-beginning))
2978 (str (buffer-substring beg end
)) rpl
)
2979 (setq rpl
(org-export-string-as str backend t
))
2980 (delete-region beg end
)
2984 (defun org-export-insert-default-template (&optional backend subtreep
)
2985 "Insert all export keywords with default values at beginning of line.
2987 BACKEND is a symbol referring to the name of a registered export
2988 back-end, for which specific export options should be added to
2989 the template, or `default' for default template. When it is nil,
2990 the user will be prompted for a category.
2992 If SUBTREEP is non-nil, export configuration will be set up
2993 locally for the subtree through node properties."
2995 (unless (derived-mode-p 'org-mode
) (user-error "Not in an Org mode buffer"))
2996 (when (and subtreep
(org-before-first-heading-p))
2997 (user-error "No subtree to set export options for"))
2998 (let ((node (and subtreep
(save-excursion (org-back-to-heading t
) (point))))
3002 (org-completing-read
3003 "Options category: "
3005 (mapcar #'(lambda (b)
3006 (symbol-name (org-export-backend-name b
)))
3007 org-export--registered-backends
))
3010 ;; Populate OPTIONS and KEYWORDS.
3011 (dolist (entry (cond ((eq backend
'default
) org-export-options-alist
)
3012 ((org-export-backend-p backend
)
3013 (org-export-backend-options backend
))
3014 (t (org-export-backend-options
3015 (org-export-get-backend backend
)))))
3016 (let ((keyword (nth 1 entry
))
3017 (option (nth 2 entry
)))
3019 (keyword (unless (assoc keyword keywords
)
3021 (if (eq (nth 4 entry
) 'split
)
3022 (mapconcat #'identity
(eval (nth 3 entry
)) " ")
3023 (eval (nth 3 entry
)))))
3024 (push (cons keyword value
) keywords
))))
3025 (option (unless (assoc option options
)
3026 (push (cons option
(eval (nth 3 entry
))) options
))))))
3027 ;; Move to an appropriate location in order to insert options.
3028 (unless subtreep
(beginning-of-line))
3029 ;; First (multiple) OPTIONS lines. Never go past fill-column.
3033 #'(lambda (opt) (format "%s:%S" (car opt
) (cdr opt
)))
3034 (sort options
(lambda (k1 k2
) (string< (car k1
) (car k2
)))))))
3037 node
"EXPORT_OPTIONS" (mapconcat 'identity items
" "))
3039 (insert "#+OPTIONS:")
3042 (< (+ width
(length (car items
)) 1) fill-column
))
3043 (let ((item (pop items
)))
3045 (incf width
(1+ (length item
))))))
3047 ;; Then the rest of keywords, in the order specified in either
3048 ;; `org-export-options-alist' or respective export back-ends.
3049 (dolist (key (nreverse keywords
))
3050 (let ((val (cond ((equal (car key
) "DATE")
3053 (org-insert-time-stamp (current-time)))))
3054 ((equal (car key
) "TITLE")
3055 (or (let ((visited-file
3056 (buffer-file-name (buffer-base-buffer))))
3058 (file-name-sans-extension
3059 (file-name-nondirectory visited-file
))))
3060 (buffer-name (buffer-base-buffer))))
3062 (if subtreep
(org-entry-put node
(concat "EXPORT_" (car key
)) val
)
3066 (if (org-string-nw-p val
) (format " %s" val
) ""))))))))
3068 (defun org-export-expand-include-keyword (&optional included dir footnotes
)
3069 "Expand every include keyword in buffer.
3070 Optional argument INCLUDED is a list of included file names along
3071 with their line restriction, when appropriate. It is used to
3072 avoid infinite recursion. Optional argument DIR is the current
3073 working directory. It is used to properly resolve relative
3074 paths. Optional argument FOOTNOTES is a hash-table used for
3075 storing and resolving footnotes. It is created automatically."
3076 (let ((case-fold-search t
)
3077 (file-prefix (make-hash-table :test
#'equal
))
3079 (footnotes (or footnotes
(make-hash-table :test
#'equal
)))
3080 (include-re "^[ \t]*#\\+INCLUDE:"))
3081 ;; If :minlevel is not set the text-property
3082 ;; `:org-include-induced-level' will be used to determine the
3083 ;; relative level when expanding INCLUDE.
3084 ;; Only affects included Org documents.
3085 (goto-char (point-min))
3086 (while (re-search-forward include-re nil t
)
3087 (put-text-property (line-beginning-position) (line-end-position)
3088 :org-include-induced-level
3089 (1+ (org-reduced-level (or (org-current-level) 0)))))
3090 ;; Expand INCLUDE keywords.
3091 (goto-char (point-min))
3092 (while (re-search-forward include-re nil t
)
3093 (let ((element (save-match-data (org-element-at-point))))
3094 (when (eq (org-element-type element
) 'keyword
)
3096 ;; Extract arguments from keyword's value.
3097 (let* ((value (org-element-property :value element
))
3098 (ind (org-get-indentation))
3100 (file (and (string-match
3101 "^\\(\".+?\"\\|\\S-+\\)\\(?:\\s-+\\|$\\)" value
)
3104 (let ((matched (match-string 1 value
)))
3105 (when (string-match "\\(::\\(.*?\\)\\)\"?\\'" matched
)
3106 (setq location
(match-string 2 matched
))
3108 (replace-match "" nil nil matched
1)))
3110 (org-remove-double-quotes
3113 (setq value
(replace-match "" nil nil value
)))))
3115 (and (string-match ":only-contents *\\([^: \r\t\n]\\S-*\\)?" value
)
3116 (prog1 (org-not-nil (match-string 1 value
))
3117 (setq value
(replace-match "" nil nil value
)))))
3120 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\""
3122 (prog1 (match-string 1 value
)
3123 (setq value
(replace-match "" nil nil value
)))))
3124 (env (cond ((string-match "\\<example\\>" value
)
3126 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value
)
3128 ;; Minimal level of included file defaults to the child
3129 ;; level of the current headline, if any, or one. It
3130 ;; only applies is the file is meant to be included as
3134 (if (string-match ":minlevel +\\([0-9]+\\)" value
)
3135 (prog1 (string-to-number (match-string 1 value
))
3136 (setq value
(replace-match "" nil nil value
)))
3137 (get-text-property (point) :org-include-induced-level
))))
3138 (src-args (and (eq env
'literal
)
3139 (match-string 1 value
)))
3140 (block (and (string-match "\\<\\(\\S-+\\)\\>" value
)
3141 (match-string 1 value
))))
3143 (delete-region (point) (progn (forward-line) (point)))
3146 ((not (file-readable-p file
))
3147 (error "Cannot include file %s" file
))
3148 ;; Check if files has already been parsed. Look after
3149 ;; inclusion lines too, as different parts of the same file
3150 ;; can be included too.
3151 ((member (list file lines
) included
)
3152 (error "Recursive file inclusion: %s" file
))
3157 (let ((ind-str (make-string ind ?
))
3158 (arg-str (if (stringp src-args
)
3159 (format " %s" src-args
)
3162 (org-escape-code-in-string
3163 (org-export--prepare-file-contents file lines
))))
3164 (format "%s#+BEGIN_%s%s\n%s%s#+END_%s\n"
3165 ind-str block arg-str contents ind-str block
))))
3168 (let ((ind-str (make-string ind ?
))
3170 (org-export--prepare-file-contents file lines
)))
3171 (format "%s#+BEGIN_%s\n%s%s#+END_%s\n"
3172 ind-str block contents ind-str block
))))
3176 (let ((org-inhibit-startup t
)
3179 (org-export--inclusion-absolute-lines
3180 file location only-contents lines
)
3183 (insert (org-export--prepare-file-contents
3184 file lines ind minlevel
3185 (or (gethash file file-prefix
)
3186 (puthash file
(incf current-prefix
) file-prefix
))
3188 (org-export-expand-include-keyword
3189 (cons (list file lines
) included
)
3190 (file-name-directory file
)
3193 ;; Expand footnotes after all files have been
3194 ;; included. Footnotes are stored at end of buffer.
3196 (org-with-wide-buffer
3197 (goto-char (point-max))
3198 (maphash (lambda (ref def
) (insert (format "\n[%s] %s\n" ref def
)))
3199 footnotes
)))))))))))
3201 (defun org-export--inclusion-absolute-lines (file location only-contents lines
)
3202 "Resolve absolute lines for an included file with file-link.
3204 FILE is string file-name of the file to include. LOCATION is a
3205 string name within FILE to be included (located via
3206 `org-link-search'). If ONLY-CONTENTS is non-nil only the
3207 contents of the named element will be included, as determined
3208 Org-Element. If LINES is non-nil only those lines are included.
3210 Return a string of lines to be included in the format expected by
3211 `org-export--prepare-file-contents'."
3213 (insert-file-contents file
)
3214 (unless (eq major-mode
'org-mode
)
3215 (let ((org-inhibit-startup t
)) (org-mode)))
3217 ;; Enforce consistent search.
3218 (let ((org-link-search-must-match-exact-headline t
))
3219 (org-link-search location
))
3221 (error (format "%s for %s::%s" (error-message-string err
) file location
))))
3222 (let* ((element (org-element-at-point))
3224 (and only-contents
(org-element-property :contents-begin element
))))
3226 (or contents-begin
(org-element-property :begin element
))
3227 (org-element-property (if contents-begin
:contents-end
:end
) element
))
3228 (when (and only-contents
3229 (memq (org-element-type element
) '(headline inlinetask
)))
3230 ;; Skip planning line and property-drawer.
3231 (goto-char (point-min))
3232 (when (org-looking-at-p org-planning-line-re
) (forward-line))
3233 (when (looking-at org-property-drawer-re
) (goto-char (match-end 0)))
3234 (unless (bolp) (forward-line))
3235 (narrow-to-region (point) (point-max))))
3237 (org-skip-whitespace)
3239 (let* ((lines (split-string lines
"-"))
3240 (lbeg (string-to-number (car lines
)))
3241 (lend (string-to-number (cadr lines
)))
3242 (beg (if (zerop lbeg
) (point-min)
3243 (goto-char (point-min))
3244 (forward-line (1- lbeg
))
3246 (end (if (zerop lend
) (point-max)
3248 (forward-line (1- lend
))
3250 (narrow-to-region beg end
)))
3251 (let ((end (point-max)))
3252 (goto-char (point-min))
3254 (let ((start-line (line-number-at-pos)))
3260 (while (< (point) end
) (incf counter
) (forward-line))
3263 (defun org-export--update-footnote-label (ref-begin digit-label id
)
3264 "Prefix footnote-label at point REF-BEGIN in buffer with ID.
3266 REF-BEGIN corresponds to the property `:begin' of objects of type
3267 footnote-definition and footnote-reference.
3269 If DIGIT-LABEL is non-nil the label is assumed to be of the form
3270 \[N] where N is one or more numbers.
3272 Return the new label."
3273 (goto-char (1+ ref-begin
))
3274 (buffer-substring (point)
3276 (if digit-label
(insert (format "fn:%d-" id
))
3278 (insert (format "%d-" id
)))
3279 (1- (search-forward "]")))))
3281 (defun org-export--prepare-file-contents (file &optional lines ind minlevel id footnotes
)
3282 "Prepare contents of FILE for inclusion and return it as a string.
3284 When optional argument LINES is a string specifying a range of
3285 lines, include only those lines.
3287 Optional argument IND, when non-nil, is an integer specifying the
3288 global indentation of returned contents. Since its purpose is to
3289 allow an included file to stay in the same environment it was
3290 created \(i.e. a list item), it doesn't apply past the first
3291 headline encountered.
3293 Optional argument MINLEVEL, when non-nil, is an integer
3294 specifying the level that any top-level headline in the included
3296 Optional argument ID is an integer that will be inserted before
3297 each footnote definition and reference if FILE is an Org file.
3298 This is useful to avoid conflicts when more than one Org file
3299 with footnotes is included in a document.
3301 Optional argument FOOTNOTES is a hash-table to store footnotes in
3302 the included document.
3305 (insert-file-contents file
)
3307 (let* ((lines (split-string lines
"-"))
3308 (lbeg (string-to-number (car lines
)))
3309 (lend (string-to-number (cadr lines
)))
3310 (beg (if (zerop lbeg
) (point-min)
3311 (goto-char (point-min))
3312 (forward-line (1- lbeg
))
3314 (end (if (zerop lend
) (point-max)
3315 (goto-char (point-min))
3316 (forward-line (1- lend
))
3318 (narrow-to-region beg end
)))
3319 ;; Remove blank lines at beginning and end of contents. The logic
3320 ;; behind that removal is that blank lines around include keyword
3321 ;; override blank lines in included file.
3322 (goto-char (point-min))
3323 (org-skip-whitespace)
3325 (delete-region (point-min) (point))
3326 (goto-char (point-max))
3327 (skip-chars-backward " \r\t\n")
3329 (delete-region (point) (point-max))
3330 ;; If IND is set, preserve indentation of include keyword until
3331 ;; the first headline encountered.
3333 (unless (eq major-mode
'org-mode
)
3334 (let ((org-inhibit-startup t
)) (org-mode)))
3335 (goto-char (point-min))
3336 (let ((ind-str (make-string ind ?
)))
3337 (while (not (or (eobp) (looking-at org-outline-regexp-bol
)))
3338 ;; Do not move footnote definitions out of column 0.
3339 (unless (and (looking-at org-footnote-definition-re
)
3340 (eq (org-element-type (org-element-at-point))
3341 'footnote-definition
))
3344 ;; When MINLEVEL is specified, compute minimal level for headlines
3345 ;; in the file (CUR-MIN), and remove stars to each headline so
3346 ;; that headlines with minimal level have a level of MINLEVEL.
3348 (unless (eq major-mode
'org-mode
)
3349 (let ((org-inhibit-startup t
)) (org-mode)))
3350 (org-with-limited-levels
3351 (let ((levels (org-map-entries
3352 (lambda () (org-reduced-level (org-current-level))))))
3354 (let ((offset (- minlevel
(apply 'min levels
))))
3355 (unless (zerop offset
)
3356 (when org-odd-levels-only
(setq offset
(* offset
2)))
3357 ;; Only change stars, don't bother moving whole
3360 (lambda () (if (< offset
0) (delete-char (abs offset
))
3361 (insert (make-string offset ?
*)))))))))))
3362 ;; Append ID to all footnote references and definitions, so they
3363 ;; become file specific and cannot collide with footnotes in other
3364 ;; included files. Further, collect relevant footnotes outside of
3367 (let ((marker-min (point-min-marker))
3368 (marker-max (point-max-marker)))
3369 (goto-char (point-min))
3370 (while (re-search-forward org-footnote-re nil t
)
3371 (let ((reference (org-element-context)))
3372 (when (eq (org-element-type reference
) 'footnote-reference
)
3373 (let* ((label (org-element-property :label reference
))
3374 (digit-label (and label
(org-string-match-p "\\`[0-9]+\\'" label
))))
3375 ;; Update the footnote-reference at point and collect
3376 ;; the new label, which is only used for footnotes
3379 ;; If label is akin to [1] convert it to [fn:ID-1].
3380 ;; Otherwise add "ID-" after "fn:".
3381 (let ((new-label (org-export--update-footnote-label
3382 (org-element-property :begin reference
) digit-label id
)))
3383 (unless (eq (org-element-property :type reference
) 'inline
)
3384 (org-with-wide-buffer
3385 (let* ((definition (org-footnote-get-definition label
))
3386 (beginning (nth 1 definition
)))
3388 (error "Definition not found for footnote %s in file %s" label file
))
3389 (if (or (< beginning marker-min
) (> beginning marker-max
))
3390 ;; Store since footnote-definition is outside of LINES.
3392 (org-element-normalize-string (nth 3 definition
))
3394 ;; Update label of definition since it is included directly.
3395 (org-export--update-footnote-label beginning digit-label id
)))))))))))
3396 (set-marker marker-min nil
)
3397 (set-marker marker-max nil
)))
3398 (org-element-normalize-string (buffer-string))))
3400 (defun org-export-execute-babel-code ()
3401 "Execute every Babel code in the visible part of current buffer."
3402 ;; Get a pristine copy of current buffer so Babel references can be
3403 ;; properly resolved.
3404 (let ((reference (org-export-copy-buffer)))
3405 (unwind-protect (org-babel-exp-process-buffer reference
)
3406 (kill-buffer reference
))))
3408 (defun org-export--copy-to-kill-ring-p ()
3409 "Return a non-nil value when output should be added to the kill ring.
3410 See also `org-export-copy-to-kill-ring'."
3411 (if (eq org-export-copy-to-kill-ring
'if-interactive
)
3412 (not (or executing-kbd-macro noninteractive
))
3413 (eq org-export-copy-to-kill-ring t
)))
3417 ;;; Tools For Back-Ends
3419 ;; A whole set of tools is available to help build new exporters. Any
3420 ;; function general enough to have its use across many back-ends
3421 ;; should be added here.
3423 ;;;; For Affiliated Keywords
3425 ;; `org-export-read-attribute' reads a property from a given element
3426 ;; as a plist. It can be used to normalize affiliated keywords'
3429 ;; Since captions can span over multiple lines and accept dual values,
3430 ;; their internal representation is a bit tricky. Therefore,
3431 ;; `org-export-get-caption' transparently returns a given element's
3432 ;; caption as a secondary string.
3434 (defun org-export-read-attribute (attribute element
&optional property
)
3435 "Turn ATTRIBUTE property from ELEMENT into a plist.
3437 When optional argument PROPERTY is non-nil, return the value of
3438 that property within attributes.
3440 This function assumes attributes are defined as \":keyword
3441 value\" pairs. It is appropriate for `:attr_html' like
3444 All values will become strings except the empty string and
3445 \"nil\", which will become nil. Also, values containing only
3446 double quotes will be read as-is, which means that \"\" value
3447 will become the empty string."
3448 (let* ((prepare-value
3451 (cond ((member str
'(nil "" "nil")) nil
)
3452 ((string-match "^\"\\(\"+\\)?\"$" str
)
3453 (or (match-string 1 str
) ""))
3456 (let ((value (org-element-property attribute element
)))
3458 (let ((s (mapconcat 'identity value
" ")) result
)
3459 (while (string-match
3460 "\\(?:^\\|[ \t]+\\)\\(:[-a-zA-Z0-9_]+\\)\\([ \t]+\\|$\\)"
3462 (let ((value (substring s
0 (match-beginning 0))))
3463 (push (funcall prepare-value value
) result
))
3464 (push (intern (match-string 1 s
)) result
)
3465 (setq s
(substring s
(match-end 0))))
3466 ;; Ignore any string before first property with `cdr'.
3467 (cdr (nreverse (cons (funcall prepare-value s
) result
))))))))
3468 (if property
(plist-get attributes property
) attributes
)))
3470 (defun org-export-get-caption (element &optional shortp
)
3471 "Return caption from ELEMENT as a secondary string.
3473 When optional argument SHORTP is non-nil, return short caption,
3474 as a secondary string, instead.
3476 Caption lines are separated by a white space."
3477 (let ((full-caption (org-element-property :caption element
)) caption
)
3478 (dolist (line full-caption
(cdr caption
))
3479 (let ((cap (funcall (if shortp
'cdr
'car
) line
)))
3481 (setq caption
(nconc (list " ") (copy-sequence cap
) caption
)))))))
3484 ;;;; For Derived Back-ends
3486 ;; `org-export-with-backend' is a function allowing to locally use
3487 ;; another back-end to transcode some object or element. In a derived
3488 ;; back-end, it may be used as a fall-back function once all specific
3489 ;; cases have been treated.
3491 (defun org-export-with-backend (backend data
&optional contents info
)
3492 "Call a transcoder from BACKEND on DATA.
3493 BACKEND is an export back-end, as returned by, e.g.,
3494 `org-export-create-backend', or a symbol referring to
3495 a registered back-end. DATA is an Org element, object, secondary
3496 string or string. CONTENTS, when non-nil, is the transcoded
3497 contents of DATA element, as a string. INFO, when non-nil, is
3498 the communication channel used for export, as a plist."
3499 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
3500 (org-export-barf-if-invalid-backend backend
)
3501 (let ((type (org-element-type data
)))
3502 (if (memq type
'(nil org-data
)) (error "No foreign transcoder available")
3503 (let* ((all-transcoders (org-export-get-all-transcoders backend
))
3504 (transcoder (cdr (assq type all-transcoders
))))
3505 (if (not (functionp transcoder
))
3506 (error "No foreign transcoder available")
3508 transcoder data contents
3510 info
(list :back-end backend
3511 :translate-alist all-transcoders
3512 :exported-data
(make-hash-table :test
'eq
:size
401)))))))))
3515 ;;;; For Export Snippets
3517 ;; Every export snippet is transmitted to the back-end. Though, the
3518 ;; latter will only retain one type of export-snippet, ignoring
3519 ;; others, based on the former's target back-end. The function
3520 ;; `org-export-snippet-backend' returns that back-end for a given
3523 (defun org-export-snippet-backend (export-snippet)
3524 "Return EXPORT-SNIPPET targeted back-end as a symbol.
3525 Translation, with `org-export-snippet-translation-alist', is
3527 (let ((back-end (org-element-property :back-end export-snippet
)))
3529 (or (cdr (assoc back-end org-export-snippet-translation-alist
))
3535 ;; `org-export-collect-footnote-definitions' is a tool to list
3536 ;; actually used footnotes definitions in the whole parse tree, or in
3537 ;; a headline, in order to add footnote listings throughout the
3540 ;; `org-export-footnote-first-reference-p' is a predicate used by some
3541 ;; back-ends, when they need to attach the footnote definition only to
3542 ;; the first occurrence of the corresponding label.
3544 ;; `org-export-get-footnote-definition' and
3545 ;; `org-export-get-footnote-number' provide easier access to
3546 ;; additional information relative to a footnote reference.
3548 (defun org-export-get-footnote-definition (footnote-reference info
)
3549 "Return definition of FOOTNOTE-REFERENCE as parsed data.
3550 INFO is the plist used as a communication channel. If no such
3551 definition can be found, raise an error."
3552 (let ((label (org-element-property :label footnote-reference
)))
3554 (cdr (assoc label
(plist-get info
:footnote-definition-alist
)))
3555 (org-element-contents footnote-reference
))
3556 (error "Definition not found for footnote %s" label
))))
3558 (defun org-export--footnote-reference-map
3559 (function data info
&optional body-first
)
3560 "Apply FUNCTION on every footnote reference in DATA.
3561 INFO is a plist containing export state. By default, as soon as
3562 a new footnote reference is encountered, FUNCTION is called onto
3563 its definition. However, if BODY-FIRST is non-nil, this step is
3564 delayed until the end of the process."
3565 (let* ((definitions)
3567 (search-ref) ; For byte-compiler.
3569 (lambda (data delayp
)
3570 ;; Search footnote references through DATA, filling
3571 ;; SEEN-REFS along the way. When DELAYP is non-nil, store
3572 ;; footnote definitions so they can be entered later.
3573 (org-element-map data
'footnote-reference
3575 (funcall function f
)
3576 (let ((--label (org-element-property :label f
)))
3577 (unless (and --label
(member --label seen-refs
))
3578 (when --label
(push --label seen-refs
))
3579 ;; Search for subsequent references in footnote
3580 ;; definition so numbering follows reading logic,
3581 ;; unless DELAYP in non-nil.
3584 (push (org-export-get-footnote-definition f info
)
3586 ;; Do not force entering inline definitions,
3587 ;; since `org-element-map' already traverses them
3588 ;; at the right time.
3589 ((eq (org-element-property :type f
) 'inline
))
3590 (t (funcall search-ref
3591 (org-export-get-footnote-definition f info
)
3594 ;; Don't enter footnote definitions since it will happen
3595 ;; when their first reference is found. Moreover, if
3596 ;; DELAYP is non-nil, make sure we postpone entering
3597 ;; definitions of inline references.
3598 (if delayp
'(footnote-definition footnote-reference
)
3599 'footnote-definition
)))))
3600 (funcall search-ref data body-first
)
3601 (funcall search-ref
(nreverse definitions
) nil
)))
3603 (defun org-export-collect-footnote-definitions (info &optional data body-first
)
3604 "Return an alist between footnote numbers, labels and definitions.
3606 INFO is the current export state, as a plist.
3608 Definitions are collected throughout the whole parse tree, or
3611 Sorting is done by order of references. As soon as a new
3612 reference is encountered, other references are searched within
3613 its definition. However, if BODY-FIRST is non-nil, this step is
3614 delayed after the whole tree is checked. This alters results
3615 when references are found in footnote definitions.
3617 Definitions either appear as Org data or as a secondary string
3618 for inlined footnotes. Unreferenced definitions are ignored."
3619 (let ((n 0) labels alist
)
3620 (org-export--footnote-reference-map
3622 ;; Collect footnote number, label and definition.
3623 (let ((l (org-element-property :label f
)))
3624 (unless (and l
(member l labels
))
3626 (push (list n l
(org-export-get-footnote-definition f info
)) alist
))
3627 (when l
(push l labels
))))
3628 (or data
(plist-get info
:parse-tree
)) info body-first
)
3631 (defun org-export-footnote-first-reference-p
3632 (footnote-reference info
&optional data body-first
)
3633 "Non-nil when a footnote reference is the first one for its label.
3635 FOOTNOTE-REFERENCE is the footnote reference being considered.
3636 INFO is a plist containing current export state.
3638 Search is done throughout the whole parse tree, or DATA when
3641 By default, as soon as a new footnote reference is encountered,
3642 other references are searched within its definition. However, if
3643 BODY-FIRST is non-nil, this step is delayed after the whole tree
3644 is checked. This alters results when references are found in
3645 footnote definitions."
3646 (let ((label (org-element-property :label footnote-reference
)))
3647 ;; Anonymous footnotes are always a first reference.
3650 (org-export--footnote-reference-map
3652 (let ((l (org-element-property :label f
)))
3653 (when (and l label
(string= label l
))
3654 (throw 'exit
(eq footnote-reference f
)))))
3655 (or data
(plist-get info
:parse-tree
)) info body-first
)))))
3657 (defun org-export-get-footnote-number (footnote info
&optional data body-first
)
3658 "Return number associated to a footnote.
3660 FOOTNOTE is either a footnote reference or a footnote definition.
3661 INFO is the plist containing export state.
3663 Number is unique throughout the whole parse tree, or DATA, when
3666 By default, as soon as a new footnote reference is encountered,
3667 counting process moves into its definition. However, if
3668 BODY-FIRST is non-nil, this step is delayed until the end of the
3669 process, leading to a different order when footnotes are nested."
3672 (label (org-element-property :label footnote
)))
3674 (org-export--footnote-reference-map
3676 (let ((l (org-element-property :label f
)))
3678 ;; Anonymous footnote match: return number.
3679 ((and (not l
) (not label
) (eq footnote f
)) (throw 'exit
(1+ count
)))
3680 ;; Labels match: return number.
3681 ((and label l
(string= label l
)) (throw 'exit
(1+ count
)))
3682 ;; Otherwise store label and increase counter if label
3683 ;; wasn't encountered yet.
3684 ((not l
) (incf count
))
3685 ((not (member l seen
)) (push l seen
) (incf count
)))))
3686 (or data
(plist-get info
:parse-tree
)) info body-first
))))
3691 ;; `org-export-get-relative-level' is a shortcut to get headline
3692 ;; level, relatively to the lower headline level in the parsed tree.
3694 ;; `org-export-get-headline-number' returns the section number of an
3695 ;; headline, while `org-export-number-to-roman' allows to convert it
3696 ;; to roman numbers. With an optional argument,
3697 ;; `org-export-get-headline-number' returns a number to unnumbered
3698 ;; headlines (used for internal id).
3700 ;; `org-export-get-headline-id' returns the unique internal id of a
3703 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
3704 ;; `org-export-last-sibling-p' are three useful predicates when it
3705 ;; comes to fulfill the `:headline-levels' property.
3707 ;; `org-export-get-tags', `org-export-get-category' and
3708 ;; `org-export-get-node-property' extract useful information from an
3709 ;; headline or a parent headline. They all handle inheritance.
3711 ;; `org-export-get-alt-title' tries to retrieve an alternative title,
3712 ;; as a secondary string, suitable for table of contents. It falls
3713 ;; back onto default title.
3715 (defun org-export-get-relative-level (headline info
)
3716 "Return HEADLINE relative level within current parsed tree.
3717 INFO is a plist holding contextual information."
3718 (+ (org-element-property :level headline
)
3719 (or (plist-get info
:headline-offset
) 0)))
3721 (defun org-export-low-level-p (headline info
)
3722 "Non-nil when HEADLINE is considered as low level.
3724 INFO is a plist used as a communication channel.
3726 A low level headlines has a relative level greater than
3727 `:headline-levels' property value.
3729 Return value is the difference between HEADLINE relative level
3730 and the last level being considered as high enough, or nil."
3731 (let ((limit (plist-get info
:headline-levels
)))
3732 (when (wholenump limit
)
3733 (let ((level (org-export-get-relative-level headline info
)))
3734 (and (> level limit
) (- level limit
))))))
3736 (defun org-export-get-headline-id (headline info
)
3737 "Return a unique ID for HEADLINE.
3738 INFO is a plist holding contextual information."
3739 (let ((numbered (org-export-numbered-headline-p headline info
)))
3741 (if numbered
"sec-" "unnumbered-")
3742 (mapconcat #'number-to-string
3744 (org-export-get-headline-number headline info
)
3745 (cdr (assq headline
(plist-get info
:unnumbered-headline-id
)))) "-"))))
3747 (defun org-export-get-headline-number (headline info
)
3748 "Return numbered HEADLINE numbering as a list of numbers.
3749 INFO is a plist holding contextual information."
3750 (and (org-export-numbered-headline-p headline info
)
3751 (cdr (assq headline
(plist-get info
:headline-numbering
)))))
3753 (defun org-export-numbered-headline-p (headline info
)
3754 "Return a non-nil value if HEADLINE element should be numbered.
3755 INFO is a plist used as a communication channel."
3757 (lambda (head) (org-not-nil (org-element-property :UNNUMBERED head
)))
3758 (org-element-lineage headline nil t
))
3759 (let ((sec-num (plist-get info
:section-numbers
))
3760 (level (org-export-get-relative-level headline info
)))
3761 (if (wholenump sec-num
) (<= level sec-num
) sec-num
))))
3763 (defun org-export-number-to-roman (n)
3764 "Convert integer N into a roman numeral."
3765 (let ((roman '((1000 .
"M") (900 .
"CM") (500 .
"D") (400 .
"CD")
3766 ( 100 .
"C") ( 90 .
"XC") ( 50 .
"L") ( 40 .
"XL")
3767 ( 10 .
"X") ( 9 .
"IX") ( 5 .
"V") ( 4 .
"IV")
3771 (number-to-string n
)
3773 (if (>= n
(caar roman
))
3774 (setq n
(- n
(caar roman
))
3775 res
(concat res
(cdar roman
)))
3779 (defun org-export-get-tags (element info
&optional tags inherited
)
3780 "Return list of tags associated to ELEMENT.
3782 ELEMENT has either an `headline' or an `inlinetask' type. INFO
3783 is a plist used as a communication channel.
3785 Select tags (see `org-export-select-tags') and exclude tags (see
3786 `org-export-exclude-tags') are removed from the list.
3788 When non-nil, optional argument TAGS should be a list of strings.
3789 Any tag belonging to this list will also be removed.
3791 When optional argument INHERITED is non-nil, tags can also be
3792 inherited from parent headlines and FILETAGS keywords."
3794 (lambda (tag) (or (member tag
(plist-get info
:select-tags
))
3795 (member tag
(plist-get info
:exclude-tags
))
3797 (if (not inherited
) (org-element-property :tags element
)
3798 ;; Build complete list of inherited tags.
3799 (let ((current-tag-list (org-element-property :tags element
)))
3800 (dolist (parent (org-element-lineage element
))
3801 (dolist (tag (org-element-property :tags parent
))
3802 (when (and (memq (org-element-type parent
) '(headline inlinetask
))
3803 (not (member tag current-tag-list
)))
3804 (push tag current-tag-list
))))
3805 ;; Add FILETAGS keywords and return results.
3806 (org-uniquify (append (plist-get info
:filetags
) current-tag-list
))))))
3808 (defun org-export-get-node-property (property blob
&optional inherited
)
3809 "Return node PROPERTY value for BLOB.
3811 PROPERTY is an upcase symbol (i.e. `:COOKIE_DATA'). BLOB is an
3814 If optional argument INHERITED is non-nil, the value can be
3815 inherited from a parent headline.
3817 Return value is a string or nil."
3818 (let ((headline (if (eq (org-element-type blob
) 'headline
) blob
3819 (org-export-get-parent-headline blob
))))
3820 (if (not inherited
) (org-element-property property blob
)
3821 (let ((parent headline
) value
)
3824 (when (plist-member (nth 1 parent
) property
)
3825 (throw 'found
(org-element-property property parent
)))
3826 (setq parent
(org-element-property :parent parent
))))))))
3828 (defun org-export-get-category (blob info
)
3829 "Return category for element or object BLOB.
3831 INFO is a plist used as a communication channel.
3833 CATEGORY is automatically inherited from a parent headline, from
3834 #+CATEGORY: keyword or created out of original file name. If all
3835 fail, the fall-back value is \"???\"."
3836 (or (org-export-get-node-property :CATEGORY blob t
)
3837 (org-element-map (plist-get info
:parse-tree
) 'keyword
3839 (when (equal (org-element-property :key kwd
) "CATEGORY")
3840 (org-element-property :value kwd
)))
3842 (let ((file (plist-get info
:input-file
)))
3843 (and file
(file-name-sans-extension (file-name-nondirectory file
))))
3846 (defun org-export-get-alt-title (headline info
)
3847 "Return alternative title for HEADLINE, as a secondary string.
3848 INFO is a plist used as a communication channel. If no optional
3849 title is defined, fall-back to the regular title."
3850 (let ((alt (org-element-property :ALT_TITLE headline
)))
3851 (if alt
(org-element-parse-secondary-string
3852 alt
(org-element-restriction 'headline
) headline
)
3853 (org-element-property :title headline
))))
3855 (defun org-export-first-sibling-p (blob info
)
3856 "Non-nil when BLOB is the first sibling in its parent.
3857 BLOB is an element or an object. If BLOB is a headline, non-nil
3858 means it is the first sibling in the sub-tree. INFO is a plist
3859 used as a communication channel."
3860 (memq (org-element-type (org-export-get-previous-element blob info
))
3863 (defun org-export-last-sibling-p (blob info
)
3864 "Non-nil when BLOB is the last sibling in its parent.
3865 BLOB is an element or an object. INFO is a plist used as
3866 a communication channel."
3867 (not (org-export-get-next-element blob info
)))
3872 ;; `org-export-get-date' returns a date appropriate for the document
3873 ;; to about to be exported. In particular, it takes care of
3874 ;; `org-export-date-timestamp-format'.
3876 (defun org-export-get-date (info &optional fmt
)
3877 "Return date value for the current document.
3879 INFO is a plist used as a communication channel. FMT, when
3880 non-nil, is a time format string that will be applied on the date
3881 if it consists in a single timestamp object. It defaults to
3882 `org-export-date-timestamp-format' when nil.
3884 A proper date can be a secondary string, a string or nil. It is
3885 meant to be translated with `org-export-data' or alike."
3886 (let ((date (plist-get info
:date
))
3887 (fmt (or fmt org-export-date-timestamp-format
)))
3888 (cond ((not date
) nil
)
3891 (eq (org-element-type (car date
)) 'timestamp
))
3892 (org-timestamp-format (car date
) fmt
))
3898 ;; `org-export-custom-protocol-maybe' handles custom protocol defined
3899 ;; with `org-add-link-type', which see.
3901 ;; `org-export-solidify-link-text' turns a string into a safer version
3902 ;; for links, replacing most non-standard characters with hyphens.
3904 ;; `org-export-get-coderef-format' returns an appropriate format
3905 ;; string for coderefs.
3907 ;; `org-export-inline-image-p' returns a non-nil value when the link
3908 ;; provided should be considered as an inline image.
3910 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
3911 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
3912 ;; returns an appropriate unique identifier when found, or nil.
3914 ;; `org-export-resolve-id-link' returns the first headline with
3915 ;; specified id or custom-id in parse tree, the path to the external
3916 ;; file with the id or nil when neither was found.
3918 ;; `org-export-resolve-coderef' associates a reference to a line
3919 ;; number in the element it belongs, or returns the reference itself
3920 ;; when the element isn't numbered.
3922 (defun org-export-solidify-link-text (s)
3923 "Take link text S and make a safe target out of it."
3925 (mapconcat 'identity
(org-split-string s
"[^a-zA-Z0-9_.-:]+") "-")))
3927 (defun org-export-custom-protocol-maybe (link desc backend
)
3928 "Try exporting LINK with a dedicated function.
3930 DESC is its description, as a string, or nil. BACKEND is the
3931 back-end used for export, as a symbol.
3933 Return output as a string, or nil if no protocol handles LINK.
3935 A custom protocol has precedence over regular back-end export.
3936 The function ignores links with an implicit type (e.g.,
3938 (let ((type (org-element-property :type link
)))
3939 (unless (or (member type
'("coderef" "custom-id" "fuzzy" "radio"))
3941 (let ((protocol (nth 2 (assoc type org-link-protocols
))))
3942 (and (functionp protocol
)
3944 (org-link-unescape (org-element-property :path link
))
3948 (defun org-export-get-coderef-format (path desc
)
3949 "Return format string for code reference link.
3950 PATH is the link path. DESC is its description."
3952 (cond ((not desc
) "%s")
3953 ((string-match (regexp-quote (concat "(" path
")")) desc
)
3954 (replace-match "%s" t t desc
))
3957 (defun org-export-inline-image-p (link &optional rules
)
3958 "Non-nil if LINK object points to an inline image.
3960 Optional argument is a set of RULES defining inline images. It
3961 is an alist where associations have the following shape:
3965 Applying a rule means apply REGEXP against LINK's path when its
3966 type is TYPE. The function will return a non-nil value if any of
3967 the provided rules is non-nil. The default rule is
3968 `org-export-default-inline-image-rule'.
3970 This only applies to links without a description."
3971 (and (not (org-element-contents link
))
3972 (let ((case-fold-search t
))
3974 (dolist (rule (or rules org-export-default-inline-image-rule
))
3975 (and (string= (org-element-property :type link
) (car rule
))
3976 (org-string-match-p (cdr rule
)
3977 (org-element-property :path link
))
3978 (throw 'exit t
)))))))
3980 (defun org-export-resolve-coderef (ref info
)
3981 "Resolve a code reference REF.
3983 INFO is a plist used as a communication channel.
3985 Return associated line number in source code, or REF itself,
3986 depending on src-block or example element's switches."
3987 (org-element-map (plist-get info
:parse-tree
) '(example-block src-block
)
3990 (insert (org-trim (org-element-property :value el
)))
3991 (let* ((label-fmt (regexp-quote
3992 (or (org-element-property :label-fmt el
)
3993 org-coderef-label-format
)))
3995 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
3996 (replace-regexp-in-string "%s" ref label-fmt nil t
))))
3997 ;; Element containing REF is found. Resolve it to either
3998 ;; a label or a line number, as needed.
3999 (when (re-search-backward ref-re nil t
)
4001 ((org-element-property :use-labels el
) ref
)
4002 ((eq (org-element-property :number-lines el
) 'continued
)
4003 (+ (org-export-get-loc el info
) (line-number-at-pos)))
4004 (t (line-number-at-pos)))))))
4007 (defun org-export-resolve-fuzzy-link (link info
)
4008 "Return LINK destination.
4010 INFO is a plist holding contextual information.
4012 Return value can be an object, an element, or nil:
4014 - If LINK path matches a target object (i.e. <<path>>) return it.
4016 - If LINK path exactly matches the name affiliated keyword
4017 \(i.e. #+NAME: path) of an element, return that element.
4019 - If LINK path exactly matches any headline name, return that
4020 element. If more than one headline share that name, priority
4021 will be given to the one with the closest common ancestor, if
4022 any, or the first one in the parse tree otherwise.
4024 - Otherwise, return nil.
4026 Assume LINK type is \"fuzzy\". White spaces are not
4028 (let* ((raw-path (org-element-property :path link
))
4029 (match-title-p (eq (aref raw-path
0) ?
*))
4030 ;; Split PATH at white spaces so matches are space
4032 (path (org-split-string
4033 (if match-title-p
(substring raw-path
1) raw-path
)))
4034 ;; Cache for destinations that are not position dependent.
4036 (or (plist-get info
:resolve-fuzzy-link-cache
)
4037 (plist-get (setq info
(plist-put info
:resolve-fuzzy-link-cache
4038 (make-hash-table :test
'equal
)))
4039 :resolve-fuzzy-link-cache
)))
4040 (cached (gethash path link-cache
'not-found
)))
4042 ;; Destination is not position dependent: use cached value.
4043 ((and (not match-title-p
) (not (eq cached
'not-found
))) cached
)
4044 ;; First try to find a matching "<<path>>" unless user specified
4045 ;; he was looking for a headline (path starts with a "*"
4047 ((and (not match-title-p
)
4048 (let ((match (org-element-map (plist-get info
:parse-tree
) 'target
4050 (and (equal (org-split-string
4051 (org-element-property :value blob
))
4054 info
'first-match
)))
4055 (and match
(puthash path match link-cache
)))))
4056 ;; Then try to find an element with a matching "#+NAME: path"
4057 ;; affiliated keyword.
4058 ((and (not match-title-p
)
4059 (let ((match (org-element-map (plist-get info
:parse-tree
)
4060 org-element-all-elements
4062 (let ((name (org-element-property :name el
)))
4064 (equal (org-split-string name
) path
))
4066 info
'first-match
)))
4067 (and match
(puthash path match link-cache
)))))
4068 ;; Last case: link either points to a headline or to nothingness.
4069 ;; Try to find the source, with priority given to headlines with
4070 ;; the closest common ancestor. If such candidate is found,
4071 ;; return it, otherwise return nil.
4073 (let ((find-headline
4075 ;; Return first headline whose `:raw-value' property is
4076 ;; NAME in parse tree DATA, or nil. Statistics cookies
4079 (org-element-map data
'headline
4081 (when (equal (org-split-string
4082 (replace-regexp-in-string
4083 "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
4084 (org-element-property :raw-value headline
)))
4087 info
'first-match
)))))
4088 ;; Search among headlines sharing an ancestor with link, from
4089 ;; closest to farthest.
4092 (let ((parent-hl (org-export-get-parent-headline link
)))
4093 (if (not parent-hl
) (list (plist-get info
:parse-tree
))
4094 (org-element-lineage parent-hl nil t
))))
4095 (let ((foundp (funcall find-headline path parent
)))
4096 (when foundp
(throw 'exit foundp
))))
4097 ;; No destination found: return nil.
4098 (and (not match-title-p
) (puthash path nil link-cache
))))))))
4100 (defun org-export-resolve-id-link (link info
)
4101 "Return headline referenced as LINK destination.
4103 INFO is a plist used as a communication channel.
4105 Return value can be the headline element matched in current parse
4106 tree, a file name or nil. Assume LINK type is either \"id\" or
4108 (let ((id (org-element-property :path link
)))
4109 ;; First check if id is within the current parse tree.
4110 (or (org-element-map (plist-get info
:parse-tree
) 'headline
4112 (when (or (string= (org-element-property :ID headline
) id
)
4113 (string= (org-element-property :CUSTOM_ID headline
) id
))
4116 ;; Otherwise, look for external files.
4117 (cdr (assoc id
(plist-get info
:id-alist
))))))
4119 (defun org-export-resolve-radio-link (link info
)
4120 "Return radio-target object referenced as LINK destination.
4122 INFO is a plist used as a communication channel.
4124 Return value can be a radio-target object or nil. Assume LINK
4125 has type \"radio\"."
4126 (let ((path (replace-regexp-in-string
4127 "[ \r\t\n]+" " " (org-element-property :path link
))))
4128 (org-element-map (plist-get info
:parse-tree
) 'radio-target
4130 (and (eq (compare-strings
4131 (replace-regexp-in-string
4132 "[ \r\t\n]+" " " (org-element-property :value radio
))
4133 nil nil path nil nil t
)
4136 info
'first-match
)))
4141 ;; `org-export-get-ordinal' associates a sequence number to any object
4144 (defun org-export-get-ordinal (element info
&optional types predicate
)
4145 "Return ordinal number of an element or object.
4147 ELEMENT is the element or object considered. INFO is the plist
4148 used as a communication channel.
4150 Optional argument TYPES, when non-nil, is a list of element or
4151 object types, as symbols, that should also be counted in.
4152 Otherwise, only provided element's type is considered.
4154 Optional argument PREDICATE is a function returning a non-nil
4155 value if the current element or object should be counted in. It
4156 accepts two arguments: the element or object being considered and
4157 the plist used as a communication channel. This allows to count
4158 only a certain type of objects (i.e. inline images).
4160 Return value is a list of numbers if ELEMENT is a headline or an
4161 item. It is nil for keywords. It represents the footnote number
4162 for footnote definitions and footnote references. If ELEMENT is
4163 a target, return the same value as if ELEMENT was the closest
4164 table, item or headline containing the target. In any other
4165 case, return the sequence number of ELEMENT among elements or
4166 objects of the same type."
4167 ;; Ordinal of a target object refer to the ordinal of the closest
4168 ;; table, item, or headline containing the object.
4169 (when (eq (org-element-type element
) 'target
)
4171 (org-element-lineage
4173 '(footnote-definition footnote-reference headline item table
))))
4174 (case (org-element-type element
)
4175 ;; Special case 1: A headline returns its number as a list.
4176 (headline (org-export-get-headline-number element info
))
4177 ;; Special case 2: An item returns its number as a list.
4178 (item (let ((struct (org-element-property :structure element
)))
4179 (org-list-get-item-number
4180 (org-element-property :begin element
)
4182 (org-list-prevs-alist struct
)
4183 (org-list-parents-alist struct
))))
4184 ((footnote-definition footnote-reference
)
4185 (org-export-get-footnote-number element info
))
4188 ;; Increment counter until ELEMENT is found again.
4189 (org-element-map (plist-get info
:parse-tree
)
4190 (or types
(org-element-type element
))
4193 ((eq element el
) (1+ counter
))
4194 ((not predicate
) (incf counter
) nil
)
4195 ((funcall predicate el info
) (incf counter
) nil
)))
4196 info
'first-match
)))))
4201 ;; `org-export-get-loc' counts number of code lines accumulated in
4202 ;; src-block or example-block elements with a "+n" switch until
4203 ;; a given element, excluded. Note: "-n" switches reset that count.
4205 ;; `org-export-unravel-code' extracts source code (along with a code
4206 ;; references alist) from an `element-block' or `src-block' type
4209 ;; `org-export-format-code' applies a formatting function to each line
4210 ;; of code, providing relative line number and code reference when
4211 ;; appropriate. Since it doesn't access the original element from
4212 ;; which the source code is coming, it expects from the code calling
4213 ;; it to know if lines should be numbered and if code references
4216 ;; Eventually, `org-export-format-code-default' is a higher-level
4217 ;; function (it makes use of the two previous functions) which handles
4218 ;; line numbering and code references inclusion, and returns source
4219 ;; code in a format suitable for plain text or verbatim output.
4221 (defun org-export-get-loc (element info
)
4222 "Return accumulated lines of code up to ELEMENT.
4224 INFO is the plist used as a communication channel.
4226 ELEMENT is excluded from count."
4228 (org-element-map (plist-get info
:parse-tree
)
4229 `(src-block example-block
,(org-element-type element
))
4232 ;; ELEMENT is reached: Quit the loop.
4234 ;; Only count lines from src-block and example-block elements
4235 ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
4236 ((not (memq (org-element-type el
) '(src-block example-block
))) nil
)
4237 ((let ((linums (org-element-property :number-lines el
)))
4239 ;; Accumulate locs or reset them.
4240 (let ((lines (org-count-lines
4241 (org-trim (org-element-property :value el
)))))
4242 (setq loc
(if (eq linums
'new
) lines
(+ loc lines
))))))
4243 ;; Return nil to stay in the loop.
4249 (defun org-export-unravel-code (element)
4250 "Clean source code and extract references out of it.
4252 ELEMENT has either a `src-block' an `example-block' type.
4254 Return a cons cell whose CAR is the source code, cleaned from any
4255 reference, protective commas and spurious indentation, and CDR is
4256 an alist between relative line number (integer) and name of code
4257 reference on that line (string)."
4258 (let* ((line 0) refs
4259 (value (org-element-property :value element
))
4260 ;; Get code and clean it. Remove blank lines at its
4261 ;; beginning and end.
4262 (code (replace-regexp-in-string
4263 "\\`\\([ \t]*\n\\)+" ""
4264 (replace-regexp-in-string
4265 "\\([ \t]*\n\\)*[ \t]*\\'" "\n"
4266 (if (or org-src-preserve-indentation
4267 (org-element-property :preserve-indent element
))
4269 (org-element-remove-indentation value
)))))
4270 ;; Get format used for references.
4271 (label-fmt (regexp-quote
4272 (or (org-element-property :label-fmt element
)
4273 org-coderef-label-format
)))
4274 ;; Build a regexp matching a loc with a reference.
4276 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
4277 (replace-regexp-in-string
4278 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t
))))
4281 ;; Code with references removed.
4282 (org-element-normalize-string
4286 (if (not (string-match with-ref-re loc
)) loc
4287 ;; Ref line: remove ref, and signal its position in REFS.
4288 (push (cons line
(match-string 3 loc
)) refs
)
4289 (replace-match "" nil nil loc
1)))
4290 (org-split-string code
"\n") "\n"))
4294 (defun org-export-format-code (code fun
&optional num-lines ref-alist
)
4295 "Format CODE by applying FUN line-wise and return it.
4297 CODE is a string representing the code to format. FUN is
4298 a function. It must accept three arguments: a line of
4299 code (string), the current line number (integer) or nil and the
4300 reference associated to the current line (string) or nil.
4302 Optional argument NUM-LINES can be an integer representing the
4303 number of code lines accumulated until the current code. Line
4304 numbers passed to FUN will take it into account. If it is nil,
4305 FUN's second argument will always be nil. This number can be
4306 obtained with `org-export-get-loc' function.
4308 Optional argument REF-ALIST can be an alist between relative line
4309 number (i.e. ignoring NUM-LINES) and the name of the code
4310 reference on it. If it is nil, FUN's third argument will always
4311 be nil. It can be obtained through the use of
4312 `org-export-unravel-code' function."
4313 (let ((--locs (org-split-string code
"\n"))
4315 (org-element-normalize-string
4319 (let ((--ref (cdr (assq --line ref-alist
))))
4320 (funcall fun --loc
(and num-lines
(+ num-lines --line
)) --ref
)))
4323 (defun org-export-format-code-default (element info
)
4324 "Return source code from ELEMENT, formatted in a standard way.
4326 ELEMENT is either a `src-block' or `example-block' element. INFO
4327 is a plist used as a communication channel.
4329 This function takes care of line numbering and code references
4330 inclusion. Line numbers, when applicable, appear at the
4331 beginning of the line, separated from the code by two white
4332 spaces. Code references, on the other hand, appear flushed to
4333 the right, separated by six white spaces from the widest line of
4335 ;; Extract code and references.
4336 (let* ((code-info (org-export-unravel-code element
))
4337 (code (car code-info
))
4338 (code-lines (org-split-string code
"\n")))
4339 (if (null code-lines
) ""
4340 (let* ((refs (and (org-element-property :retain-labels element
)
4342 ;; Handle line numbering.
4343 (num-start (case (org-element-property :number-lines element
)
4344 (continued (org-export-get-loc element info
))
4349 (length (number-to-string
4350 (+ (length code-lines
) num-start
))))))
4351 ;; Prepare references display, if required. Any reference
4352 ;; should start six columns after the widest line of code,
4353 ;; wrapped with parenthesis.
4355 (+ (apply 'max
(mapcar 'length code-lines
))
4356 (if (not num-start
) 0 (length (format num-fmt num-start
))))))
4357 (org-export-format-code
4359 (lambda (loc line-num ref
)
4360 (let ((number-str (and num-fmt
(format num-fmt line-num
))))
4365 (concat (make-string
4367 (+ (length loc
) (length number-str
))) ?
)
4368 (format "(%s)" ref
))))))
4374 ;; `org-export-table-has-special-column-p' and and
4375 ;; `org-export-table-row-is-special-p' are predicates used to look for
4376 ;; meta-information about the table structure.
4378 ;; `org-table-has-header-p' tells when the rows before the first rule
4379 ;; should be considered as table's header.
4381 ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
4382 ;; and `org-export-table-cell-borders' extract information from
4383 ;; a table-cell element.
4385 ;; `org-export-table-dimensions' gives the number on rows and columns
4386 ;; in the table, ignoring horizontal rules and special columns.
4387 ;; `org-export-table-cell-address', given a table-cell object, returns
4388 ;; the absolute address of a cell. On the other hand,
4389 ;; `org-export-get-table-cell-at' does the contrary.
4391 ;; `org-export-table-cell-starts-colgroup-p',
4392 ;; `org-export-table-cell-ends-colgroup-p',
4393 ;; `org-export-table-row-starts-rowgroup-p',
4394 ;; `org-export-table-row-ends-rowgroup-p',
4395 ;; `org-export-table-row-starts-header-p',
4396 ;; `org-export-table-row-ends-header-p' and
4397 ;; `org-export-table-row-in-header-p' indicate position of current row
4398 ;; or cell within the table.
4400 (defun org-export-table-has-special-column-p (table)
4401 "Non-nil when TABLE has a special column.
4402 All special columns will be ignored during export."
4403 ;; The table has a special column when every first cell of every row
4404 ;; has an empty value or contains a symbol among "/", "#", "!", "$",
4405 ;; "*" "_" and "^". Though, do not consider a first row containing
4406 ;; only empty cells as special.
4407 (let ((special-column-p 'empty
))
4411 (when (eq (org-element-property :type row
) 'standard
)
4412 (let ((value (org-element-contents
4413 (car (org-element-contents row
)))))
4414 (cond ((member value
'(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
4415 (setq special-column-p
'special
))
4417 (t (throw 'exit nil
))))))
4418 (org-element-contents table
))
4419 (eq special-column-p
'special
))))
4421 (defun org-export-table-has-header-p (table info
)
4422 "Non-nil when TABLE has a header.
4424 INFO is a plist used as a communication channel.
4426 A table has a header when it contains at least two row groups."
4427 (let ((cache (or (plist-get info
:table-header-cache
)
4428 (plist-get (setq info
4429 (plist-put info
:table-header-cache
4430 (make-hash-table :test
'eq
)))
4431 :table-header-cache
))))
4432 (or (gethash table cache
)
4433 (let ((rowgroup 1) row-flag
)
4436 (org-element-map table
'table-row
4440 ((and row-flag
(eq (org-element-property :type row
) 'rule
))
4441 (incf rowgroup
) (setq row-flag nil
))
4442 ((and (not row-flag
) (eq (org-element-property :type row
)
4444 (setq row-flag t
) nil
)))
4448 (defun org-export-table-row-is-special-p (table-row info
)
4449 "Non-nil if TABLE-ROW is considered special.
4451 INFO is a plist used as the communication channel.
4453 All special rows will be ignored during export."
4454 (when (eq (org-element-property :type table-row
) 'standard
)
4455 (let ((first-cell (org-element-contents
4456 (car (org-element-contents table-row
)))))
4457 ;; A row is special either when...
4459 ;; ... it starts with a field only containing "/",
4460 (equal first-cell
'("/"))
4461 ;; ... the table contains a special column and the row start
4462 ;; with a marking character among, "^", "_", "$" or "!",
4463 (and (org-export-table-has-special-column-p
4464 (org-export-get-parent table-row
))
4465 (member first-cell
'(("^") ("_") ("$") ("!"))))
4466 ;; ... it contains only alignment cookies and empty cells.
4467 (let ((special-row-p 'empty
))
4471 (let ((value (org-element-contents cell
)))
4472 ;; Since VALUE is a secondary string, the following
4473 ;; checks avoid expanding it with `org-export-data'.
4475 ((and (not (cdr value
))
4476 (stringp (car value
))
4477 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
4479 (setq special-row-p
'cookie
))
4480 (t (throw 'exit nil
)))))
4481 (org-element-contents table-row
))
4482 (eq special-row-p
'cookie
)))))))
4484 (defun org-export-table-row-group (table-row info
)
4485 "Return TABLE-ROW's group number, as an integer.
4487 INFO is a plist used as the communication channel.
4489 Return value is the group number, as an integer, or nil for
4490 special rows and rows separators. First group is also table's
4492 (let ((cache (or (plist-get info
:table-row-group-cache
)
4493 (plist-get (setq info
4494 (plist-put info
:table-row-group-cache
4495 (make-hash-table :test
'eq
)))
4496 :table-row-group-cache
))))
4497 (cond ((gethash table-row cache
))
4498 ((eq (org-element-property :type table-row
) 'rule
) nil
)
4499 (t (let ((group 0) row-flag
)
4500 (org-element-map (org-export-get-parent table-row
) 'table-row
4502 (if (eq (org-element-property :type row
) 'rule
)
4504 (unless row-flag
(incf group
) (setq row-flag t
)))
4505 (when (eq table-row row
) (puthash table-row group cache
)))
4506 info
'first-match
))))))
4508 (defun org-export-table-cell-width (table-cell info
)
4509 "Return TABLE-CELL contents width.
4511 INFO is a plist used as the communication channel.
4513 Return value is the width given by the last width cookie in the
4514 same column as TABLE-CELL, or nil."
4515 (let* ((row (org-export-get-parent table-cell
))
4516 (table (org-export-get-parent row
))
4517 (cells (org-element-contents row
))
4518 (columns (length cells
))
4519 (column (- columns
(length (memq table-cell cells
))))
4520 (cache (or (plist-get info
:table-cell-width-cache
)
4521 (plist-get (setq info
4522 (plist-put info
:table-cell-width-cache
4523 (make-hash-table :test
'eq
)))
4524 :table-cell-width-cache
)))
4525 (width-vector (or (gethash table cache
)
4526 (puthash table
(make-vector columns
'empty
) cache
)))
4527 (value (aref width-vector column
)))
4528 (if (not (eq value
'empty
)) value
4530 (dolist (row (org-element-contents table
)
4531 (aset width-vector column cookie-width
))
4532 (when (org-export-table-row-is-special-p row info
)
4533 ;; In a special row, try to find a width cookie at COLUMN.
4534 (let* ((value (org-element-contents
4535 (elt (org-element-contents row
) column
)))
4536 (cookie (car value
)))
4537 ;; The following checks avoid expanding unnecessarily
4538 ;; the cell with `org-export-data'.
4542 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" cookie
)
4543 (match-string 1 cookie
))
4545 (string-to-number (match-string 1 cookie
)))))))))))
4547 (defun org-export-table-cell-alignment (table-cell info
)
4548 "Return TABLE-CELL contents alignment.
4550 INFO is a plist used as the communication channel.
4552 Return alignment as specified by the last alignment cookie in the
4553 same column as TABLE-CELL. If no such cookie is found, a default
4554 alignment value will be deduced from fraction of numbers in the
4555 column (see `org-table-number-fraction' for more information).
4556 Possible values are `left', `right' and `center'."
4557 ;; Load `org-table-number-fraction' and `org-table-number-regexp'.
4558 (require 'org-table
)
4559 (let* ((row (org-export-get-parent table-cell
))
4560 (table (org-export-get-parent row
))
4561 (cells (org-element-contents row
))
4562 (columns (length cells
))
4563 (column (- columns
(length (memq table-cell cells
))))
4564 (cache (or (plist-get info
:table-cell-alignment-cache
)
4565 (plist-get (setq info
4566 (plist-put info
:table-cell-alignment-cache
4567 (make-hash-table :test
'eq
)))
4568 :table-cell-alignment-cache
)))
4569 (align-vector (or (gethash table cache
)
4570 (puthash table
(make-vector columns nil
) cache
))))
4571 (or (aref align-vector column
)
4572 (let ((number-cells 0)
4575 previous-cell-number-p
)
4576 (dolist (row (org-element-contents (org-export-get-parent row
)))
4578 ;; In a special row, try to find an alignment cookie at
4580 ((org-export-table-row-is-special-p row info
)
4581 (let ((value (org-element-contents
4582 (elt (org-element-contents row
) column
))))
4583 ;; Since VALUE is a secondary string, the following
4584 ;; checks avoid useless expansion through
4585 ;; `org-export-data'.
4588 (stringp (car value
))
4589 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
4591 (match-string 1 (car value
)))
4592 (setq cookie-align
(match-string 1 (car value
))))))
4593 ;; Ignore table rules.
4594 ((eq (org-element-property :type row
) 'rule
))
4595 ;; In a standard row, check if cell's contents are
4596 ;; expressing some kind of number. Increase NUMBER-CELLS
4597 ;; accordingly. Though, don't bother if an alignment
4598 ;; cookie has already defined cell's alignment.
4600 (let ((value (org-export-data
4601 (org-element-contents
4602 (elt (org-element-contents row
) column
))
4605 ;; Treat an empty cell as a number if it follows
4607 (if (not (or (string-match org-table-number-regexp value
)
4608 (and (string= value
"") previous-cell-number-p
)))
4609 (setq previous-cell-number-p nil
)
4610 (setq previous-cell-number-p t
)
4611 (incf number-cells
))))))
4612 ;; Return value. Alignment specified by cookies has
4613 ;; precedence over alignment deduced from cell's contents.
4616 (cond ((equal cookie-align
"l") 'left
)
4617 ((equal cookie-align
"r") 'right
)
4618 ((equal cookie-align
"c") 'center
)
4619 ((>= (/ (float number-cells
) total-cells
)
4620 org-table-number-fraction
)
4624 (defun org-export-table-cell-borders (table-cell info
)
4625 "Return TABLE-CELL borders.
4627 INFO is a plist used as a communication channel.
4629 Return value is a list of symbols, or nil. Possible values are:
4630 `top', `bottom', `above', `below', `left' and `right'. Note:
4631 `top' (resp. `bottom') only happen for a cell in the first
4632 row (resp. last row) of the table, ignoring table rules, if any.
4634 Returned borders ignore special rows."
4635 (let* ((row (org-export-get-parent table-cell
))
4636 (table (org-export-get-parent-table table-cell
))
4638 ;; Top/above border? TABLE-CELL has a border above when a rule
4639 ;; used to demarcate row groups can be found above. Hence,
4640 ;; finding a rule isn't sufficient to push `above' in BORDERS:
4641 ;; another regular row has to be found above that rule.
4645 (cond ((eq (org-element-property :type row
) 'rule
)
4647 ((not (org-export-table-row-is-special-p row info
))
4648 (if rule-flag
(throw 'exit
(push 'above borders
))
4649 (throw 'exit nil
)))))
4650 ;; Look at every row before the current one.
4651 (cdr (memq row
(reverse (org-element-contents table
)))))
4652 ;; No rule above, or rule found starts the table (ignoring any
4653 ;; special row): TABLE-CELL is at the top of the table.
4654 (when rule-flag
(push 'above borders
))
4655 (push 'top borders
)))
4656 ;; Bottom/below border? TABLE-CELL has a border below when next
4657 ;; non-regular row below is a rule.
4661 (cond ((eq (org-element-property :type row
) 'rule
)
4663 ((not (org-export-table-row-is-special-p row info
))
4664 (if rule-flag
(throw 'exit
(push 'below borders
))
4665 (throw 'exit nil
)))))
4666 ;; Look at every row after the current one.
4667 (cdr (memq row
(org-element-contents table
))))
4668 ;; No rule below, or rule found ends the table (modulo some
4669 ;; special row): TABLE-CELL is at the bottom of the table.
4670 (when rule-flag
(push 'below borders
))
4671 (push 'bottom borders
)))
4672 ;; Right/left borders? They can only be specified by column
4673 ;; groups. Column groups are defined in a row starting with "/".
4674 ;; Also a column groups row only contains "<", "<>", ">" or blank
4677 (let ((column (let ((cells (org-element-contents row
)))
4678 (- (length cells
) (length (memq table-cell cells
))))))
4681 (unless (eq (org-element-property :type row
) 'rule
)
4682 (when (equal (org-element-contents
4683 (car (org-element-contents row
)))
4685 (let ((column-groups
4688 (let ((value (org-element-contents cell
)))
4689 (when (member value
'(("<") ("<>") (">") nil
))
4691 (org-element-contents row
))))
4692 ;; There's a left border when previous cell, if
4693 ;; any, ends a group, or current one starts one.
4694 (when (or (and (not (zerop column
))
4695 (member (elt column-groups
(1- column
))
4697 (member (elt column-groups column
) '("<" "<>")))
4698 (push 'left borders
))
4699 ;; There's a right border when next cell, if any,
4700 ;; starts a group, or current one ends one.
4701 (when (or (and (/= (1+ column
) (length column-groups
))
4702 (member (elt column-groups
(1+ column
))
4704 (member (elt column-groups column
) '(">" "<>")))
4705 (push 'right borders
))
4706 (throw 'exit nil
)))))
4707 ;; Table rows are read in reverse order so last column groups
4708 ;; row has precedence over any previous one.
4709 (reverse (org-element-contents table
)))))
4713 (defun org-export-table-cell-starts-colgroup-p (table-cell info
)
4714 "Non-nil when TABLE-CELL is at the beginning of a column group.
4715 INFO is a plist used as a communication channel."
4716 ;; A cell starts a column group either when it is at the beginning
4717 ;; of a row (or after the special column, if any) or when it has
4719 (or (eq (org-element-map (org-export-get-parent table-cell
) 'table-cell
4720 'identity info
'first-match
)
4722 (memq 'left
(org-export-table-cell-borders table-cell info
))))
4724 (defun org-export-table-cell-ends-colgroup-p (table-cell info
)
4725 "Non-nil when TABLE-CELL is at the end of a column group.
4726 INFO is a plist used as a communication channel."
4727 ;; A cell ends a column group either when it is at the end of a row
4728 ;; or when it has a right border.
4729 (or (eq (car (last (org-element-contents
4730 (org-export-get-parent table-cell
))))
4732 (memq 'right
(org-export-table-cell-borders table-cell info
))))
4734 (defun org-export-table-row-starts-rowgroup-p (table-row info
)
4735 "Non-nil when TABLE-ROW is at the beginning of a row group.
4736 INFO is a plist used as a communication channel."
4737 (unless (or (eq (org-element-property :type table-row
) 'rule
)
4738 (org-export-table-row-is-special-p table-row info
))
4739 (let ((borders (org-export-table-cell-borders
4740 (car (org-element-contents table-row
)) info
)))
4741 (or (memq 'top borders
) (memq 'above borders
)))))
4743 (defun org-export-table-row-ends-rowgroup-p (table-row info
)
4744 "Non-nil when TABLE-ROW is at the end of a row group.
4745 INFO is a plist used as a communication channel."
4746 (unless (or (eq (org-element-property :type table-row
) 'rule
)
4747 (org-export-table-row-is-special-p table-row info
))
4748 (let ((borders (org-export-table-cell-borders
4749 (car (org-element-contents table-row
)) info
)))
4750 (or (memq 'bottom borders
) (memq 'below borders
)))))
4752 (defun org-export-table-row-in-header-p (table-row info
)
4753 "Non-nil when TABLE-ROW is located within table's header.
4754 INFO is a plist used as a communication channel. Always return
4755 nil for special rows and rows separators."
4756 (and (org-export-table-has-header-p
4757 (org-export-get-parent-table table-row
) info
)
4758 (eql (org-export-table-row-group table-row info
) 1)))
4760 (defun org-export-table-row-starts-header-p (table-row info
)
4761 "Non-nil when TABLE-ROW is the first table header's row.
4762 INFO is a plist used as a communication channel."
4763 (and (org-export-table-row-in-header-p table-row info
)
4764 (org-export-table-row-starts-rowgroup-p table-row info
)))
4766 (defun org-export-table-row-ends-header-p (table-row info
)
4767 "Non-nil when TABLE-ROW is the last table header's row.
4768 INFO is a plist used as a communication channel."
4769 (and (org-export-table-row-in-header-p table-row info
)
4770 (org-export-table-row-ends-rowgroup-p table-row info
)))
4772 (defun org-export-table-row-number (table-row info
)
4773 "Return TABLE-ROW number.
4774 INFO is a plist used as a communication channel. Return value is
4775 zero-based and ignores separators. The function returns nil for
4776 special columns and separators."
4777 (when (and (eq (org-element-property :type table-row
) 'standard
)
4778 (not (org-export-table-row-is-special-p table-row info
)))
4780 (org-element-map (org-export-get-parent-table table-row
) 'table-row
4782 (cond ((eq row table-row
) number
)
4783 ((eq (org-element-property :type row
) 'standard
)
4784 (incf number
) nil
)))
4785 info
'first-match
))))
4787 (defun org-export-table-dimensions (table info
)
4788 "Return TABLE dimensions.
4790 INFO is a plist used as a communication channel.
4792 Return value is a CONS like (ROWS . COLUMNS) where
4793 ROWS (resp. COLUMNS) is the number of exportable
4794 rows (resp. columns)."
4795 (let (first-row (columns 0) (rows 0))
4796 ;; Set number of rows, and extract first one.
4797 (org-element-map table
'table-row
4799 (when (eq (org-element-property :type row
) 'standard
)
4801 (unless first-row
(setq first-row row
)))) info
)
4802 ;; Set number of columns.
4803 (org-element-map first-row
'table-cell
(lambda (cell) (incf columns
)) info
)
4805 (cons rows columns
)))
4807 (defun org-export-table-cell-address (table-cell info
)
4808 "Return address of a regular TABLE-CELL object.
4810 TABLE-CELL is the cell considered. INFO is a plist used as
4811 a communication channel.
4813 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4814 zero-based index. Only exportable cells are considered. The
4815 function returns nil for other cells."
4816 (let* ((table-row (org-export-get-parent table-cell
))
4817 (row-number (org-export-table-row-number table-row info
)))
4820 (let ((col-count 0))
4821 (org-element-map table-row
'table-cell
4823 (if (eq cell table-cell
) col-count
(incf col-count
) nil
))
4824 info
'first-match
))))))
4826 (defun org-export-get-table-cell-at (address table info
)
4827 "Return regular table-cell object at ADDRESS in TABLE.
4829 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4830 zero-based index. TABLE is a table type element. INFO is
4831 a plist used as a communication channel.
4833 If no table-cell, among exportable cells, is found at ADDRESS,
4835 (let ((column-pos (cdr address
)) (column-count 0))
4837 ;; Row at (car address) or nil.
4838 (let ((row-pos (car address
)) (row-count 0))
4839 (org-element-map table
'table-row
4841 (cond ((eq (org-element-property :type row
) 'rule
) nil
)
4842 ((= row-count row-pos
) row
)
4843 (t (incf row-count
) nil
)))
4847 (if (= column-count column-pos
) cell
4848 (incf column-count
) nil
))
4849 info
'first-match
)))
4852 ;;;; For Tables Of Contents
4854 ;; `org-export-collect-headlines' builds a list of all exportable
4855 ;; headline elements, maybe limited to a certain depth. One can then
4856 ;; easily parse it and transcode it.
4858 ;; Building lists of tables, figures or listings is quite similar.
4859 ;; Once the generic function `org-export-collect-elements' is defined,
4860 ;; `org-export-collect-tables', `org-export-collect-figures' and
4861 ;; `org-export-collect-listings' can be derived from it.
4863 (defun org-export-collect-headlines (info &optional n scope
)
4864 "Collect headlines in order to build a table of contents.
4866 INFO is a plist used as a communication channel.
4868 When optional argument N is an integer, it specifies the depth of
4869 the table of contents. Otherwise, it is set to the value of the
4870 last headline level. See `org-export-headline-levels' for more
4873 Optional argument SCOPE, when non-nil, is an element. If it is
4874 a headline, only children of SCOPE are collected. Otherwise,
4875 collect children of the headline containing provided element. If
4876 there is no such headline, collect all headlines. In any case,
4877 argument N becomes relative to the level of that headline.
4879 Return a list of all exportable headlines as parsed elements.
4880 Footnote sections are ignored."
4881 (let* ((scope (cond ((not scope
) (plist-get info
:parse-tree
))
4882 ((eq (org-element-type scope
) 'headline
) scope
)
4883 ((org-export-get-parent-headline scope
))
4884 (t (plist-get info
:parse-tree
))))
4885 (limit (plist-get info
:headline-levels
))
4886 (n (if (not (wholenump n
)) limit
4887 (min (if (eq (org-element-type scope
) 'org-data
) n
4888 (+ (org-export-get-relative-level scope info
) n
))
4890 (org-element-map (org-element-contents scope
) 'headline
4892 (unless (org-element-property :footnote-section-p headline
)
4893 (let ((level (org-export-get-relative-level headline info
)))
4894 (and (<= level n
) headline
))))
4897 (defun org-export-collect-elements (type info
&optional predicate
)
4898 "Collect referenceable elements of a determined type.
4900 TYPE can be a symbol or a list of symbols specifying element
4901 types to search. Only elements with a caption are collected.
4903 INFO is a plist used as a communication channel.
4905 When non-nil, optional argument PREDICATE is a function accepting
4906 one argument, an element of type TYPE. It returns a non-nil
4907 value when that element should be collected.
4909 Return a list of all elements found, in order of appearance."
4910 (org-element-map (plist-get info
:parse-tree
) type
4912 (and (org-element-property :caption element
)
4913 (or (not predicate
) (funcall predicate element
))
4917 (defun org-export-collect-tables (info)
4918 "Build a list of tables.
4919 INFO is a plist used as a communication channel.
4921 Return a list of table elements with a caption."
4922 (org-export-collect-elements 'table info
))
4924 (defun org-export-collect-figures (info predicate
)
4925 "Build a list of figures.
4927 INFO is a plist used as a communication channel. PREDICATE is
4928 a function which accepts one argument: a paragraph element and
4929 whose return value is non-nil when that element should be
4932 A figure is a paragraph type element, with a caption, verifying
4933 PREDICATE. The latter has to be provided since a \"figure\" is
4934 a vague concept that may depend on back-end.
4936 Return a list of elements recognized as figures."
4937 (org-export-collect-elements 'paragraph info predicate
))
4939 (defun org-export-collect-listings (info)
4940 "Build a list of src blocks.
4942 INFO is a plist used as a communication channel.
4944 Return a list of src-block elements with a caption."
4945 (org-export-collect-elements 'src-block info
))
4950 ;; The main function for the smart quotes sub-system is
4951 ;; `org-export-activate-smart-quotes', which replaces every quote in
4952 ;; a given string from the parse tree with its "smart" counterpart.
4954 ;; Dictionary for smart quotes is stored in
4955 ;; `org-export-smart-quotes-alist'.
4957 ;; Internally, regexps matching potential smart quotes (checks at
4958 ;; string boundaries are also necessary) are defined in
4959 ;; `org-export-smart-quotes-regexps'.
4961 (defconst org-export-smart-quotes-alist
4963 ;; one may use: »...«, "...", ›...‹, or '...'.
4964 ;; http://sproget.dk/raad-og-regler/retskrivningsregler/retskrivningsregler/a7-40-60/a7-58-anforselstegn/
4965 ;; LaTeX quotes require Babel!
4966 (opening-double-quote :utf-8
"»" :html
"»" :latex
">>"
4967 :texinfo
"@guillemetright{}")
4968 (closing-double-quote :utf-8
"«" :html
"«" :latex
"<<"
4969 :texinfo
"@guillemetleft{}")
4970 (opening-single-quote :utf-8
"›" :html
"›" :latex
"\\frq{}"
4971 :texinfo
"@guilsinglright{}")
4972 (closing-single-quote :utf-8
"‹" :html
"‹" :latex
"\\flq{}"
4973 :texinfo
"@guilsingleft{}")
4974 (apostrophe :utf-8
"’" :html
"’"))
4976 (opening-double-quote :utf-8
"„" :html
"„" :latex
"\"`"
4977 :texinfo
"@quotedblbase{}")
4978 (closing-double-quote :utf-8
"“" :html
"“" :latex
"\"'"
4979 :texinfo
"@quotedblleft{}")
4980 (opening-single-quote :utf-8
"‚" :html
"‚" :latex
"\\glq{}"
4981 :texinfo
"@quotesinglbase{}")
4982 (closing-single-quote :utf-8
"‘" :html
"‘" :latex
"\\grq{}"
4983 :texinfo
"@quoteleft{}")
4984 (apostrophe :utf-8
"’" :html
"’"))
4986 (opening-double-quote :utf-8
"“" :html
"“" :latex
"``" :texinfo
"``")
4987 (closing-double-quote :utf-8
"”" :html
"”" :latex
"''" :texinfo
"''")
4988 (opening-single-quote :utf-8
"‘" :html
"‘" :latex
"`" :texinfo
"`")
4989 (closing-single-quote :utf-8
"’" :html
"’" :latex
"'" :texinfo
"'")
4990 (apostrophe :utf-8
"’" :html
"’"))
4992 (opening-double-quote :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
4993 :texinfo
"@guillemetleft{}")
4994 (closing-double-quote :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
4995 :texinfo
"@guillemetright{}")
4996 (opening-single-quote :utf-8
"“" :html
"“" :latex
"``" :texinfo
"``")
4997 (closing-single-quote :utf-8
"”" :html
"”" :latex
"''" :texinfo
"''")
4998 (apostrophe :utf-8
"’" :html
"’"))
5000 (opening-double-quote :utf-8
"« " :html
"« " :latex
"\\og "
5001 :texinfo
"@guillemetleft{}@tie{}")
5002 (closing-double-quote :utf-8
" »" :html
" »" :latex
"\\fg{}"
5003 :texinfo
"@tie{}@guillemetright{}")
5004 (opening-single-quote :utf-8
"« " :html
"« " :latex
"\\og "
5005 :texinfo
"@guillemetleft{}@tie{}")
5006 (closing-single-quote :utf-8
" »" :html
" »" :latex
"\\fg{}"
5007 :texinfo
"@tie{}@guillemetright{}")
5008 (apostrophe :utf-8
"’" :html
"’"))
5010 ;; https://nn.wikipedia.org/wiki/Sitatteikn
5011 (opening-double-quote :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
5012 :texinfo
"@guillemetleft{}")
5013 (closing-double-quote :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
5014 :texinfo
"@guillemetright{}")
5015 (opening-single-quote :utf-8
"‘" :html
"‘" :latex
"`" :texinfo
"`")
5016 (closing-single-quote :utf-8
"’" :html
"’" :latex
"'" :texinfo
"'")
5017 (apostrophe :utf-8
"’" :html
"’"))
5019 ;; https://nn.wikipedia.org/wiki/Sitatteikn
5020 (opening-double-quote :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
5021 :texinfo
"@guillemetleft{}")
5022 (closing-double-quote :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
5023 :texinfo
"@guillemetright{}")
5024 (opening-single-quote :utf-8
"‘" :html
"‘" :latex
"`" :texinfo
"`")
5025 (closing-single-quote :utf-8
"’" :html
"’" :latex
"'" :texinfo
"'")
5026 (apostrophe :utf-8
"’" :html
"’"))
5028 ;; https://nn.wikipedia.org/wiki/Sitatteikn
5029 (opening-double-quote :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
5030 :texinfo
"@guillemetleft{}")
5031 (closing-double-quote :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
5032 :texinfo
"@guillemetright{}")
5033 (opening-single-quote :utf-8
"‘" :html
"‘" :latex
"`" :texinfo
"`")
5034 (closing-single-quote :utf-8
"’" :html
"’" :latex
"'" :texinfo
"'")
5035 (apostrophe :utf-8
"’" :html
"’"))
5037 ;; http://ru.wikipedia.org/wiki/%D0%9A%D0%B0%D0%B2%D1%8B%D1%87%D0%BA%D0%B8#.D0.9A.D0.B0.D0.B2.D1.8B.D1.87.D0.BA.D0.B8.2C_.D0.B8.D1.81.D0.BF.D0.BE.D0.BB.D1.8C.D0.B7.D1.83.D0.B5.D0.BC.D1.8B.D0.B5_.D0.B2_.D1.80.D1.83.D1.81.D1.81.D0.BA.D0.BE.D0.BC_.D1.8F.D0.B7.D1.8B.D0.BA.D0.B5
5038 ;; http://www.artlebedev.ru/kovodstvo/sections/104/
5039 (opening-double-quote :utf-8
"«" :html
"«" :latex
"{}<<"
5040 :texinfo
"@guillemetleft{}")
5041 (closing-double-quote :utf-8
"»" :html
"»" :latex
">>{}"
5042 :texinfo
"@guillemetright{}")
5043 (opening-single-quote :utf-8
"„" :html
"„" :latex
"\\glqq{}"
5044 :texinfo
"@quotedblbase{}")
5045 (closing-single-quote :utf-8
"“" :html
"“" :latex
"\\grqq{}"
5046 :texinfo
"@quotedblleft{}")
5047 (apostrophe :utf-8
"’" :html
: "'"))
5049 ;; based on https://sv.wikipedia.org/wiki/Citattecken
5050 (opening-double-quote :utf-8
"”" :html
"”" :latex
"’’" :texinfo
"’’")
5051 (closing-double-quote :utf-8
"”" :html
"”" :latex
"’’" :texinfo
"’’")
5052 (opening-single-quote :utf-8
"’" :html
"’" :latex
"’" :texinfo
"`")
5053 (closing-single-quote :utf-8
"’" :html
"’" :latex
"’" :texinfo
"'")
5054 (apostrophe :utf-8
"’" :html
"’"))
5056 "Smart quotes translations.
5058 Alist whose CAR is a language string and CDR is an alist with
5059 quote type as key and a plist associating various encodings to
5060 their translation as value.
5062 A quote type can be any symbol among `opening-double-quote',
5063 `closing-double-quote', `opening-single-quote',
5064 `closing-single-quote' and `apostrophe'.
5066 Valid encodings include `:utf-8', `:html', `:latex' and
5069 If no translation is found, the quote character is left as-is.")
5071 (defconst org-export-smart-quotes-regexps
5073 ;; Possible opening quote at beginning of string.
5074 "\\`\\([\"']\\)\\(\\w\\|\\s.\\|\\s_\\|\\s(\\)"
5075 ;; Possible closing quote at beginning of string.
5076 "\\`\\([\"']\\)\\(\\s-\\|\\s)\\|\\s.\\)"
5077 ;; Possible apostrophe at beginning of string.
5079 ;; Opening single and double quotes.
5080 "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\(?:\\w\\|\\s.\\|\\s_\\)"
5081 ;; Closing single and double quotes.
5082 "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\(?:\\s-\\|\\s)\\|\\s.\\)"
5085 ;; Possible opening quote at end of string.
5086 "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\'"
5087 ;; Possible closing quote at end of string.
5088 "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\'"
5089 ;; Possible apostrophe at end of string.
5091 "List of regexps matching a quote or an apostrophe.
5092 In every regexp, quote or apostrophe matched is put in group 1.")
5094 (defun org-export-activate-smart-quotes (s encoding info
&optional original
)
5095 "Replace regular quotes with \"smart\" quotes in string S.
5097 ENCODING is a symbol among `:html', `:latex', `:texinfo' and
5098 `:utf-8'. INFO is a plist used as a communication channel.
5100 The function has to retrieve information about string
5101 surroundings in parse tree. It can only happen with an
5102 unmodified string. Thus, if S has already been through another
5103 process, a non-nil ORIGINAL optional argument will provide that
5106 Return the new string."
5108 (let* ((prev (org-export-get-previous-element (or original s
) info
))
5109 ;; Try to be flexible when computing number of blanks
5110 ;; before object. The previous object may be a string
5111 ;; introduced by the back-end and not completely parsed.
5112 (pre-blank (and prev
5113 (or (org-element-property :post-blank prev
)
5114 ;; A string with missing `:post-blank'
5117 (string-match " *\\'" prev
)
5118 (length (match-string 0 prev
)))
5121 (next (org-export-get-next-element (or original s
) info
))
5124 ;; Return smart quote associated to a give quote Q, as
5125 ;; a string. TYPE is a symbol among `open', `close' and
5127 (let ((key (case type
5128 (apostrophe 'apostrophe
)
5129 (open (if (equal "'" q
) 'opening-single-quote
5130 'opening-double-quote
))
5131 (otherwise (if (equal "'" q
) 'closing-single-quote
5132 'closing-double-quote
)))))
5135 (cdr (assoc (plist-get info
:language
)
5136 org-export-smart-quotes-alist
))))
5139 (if (or (equal "\"" s
) (equal "'" s
))
5140 ;; Only a quote: no regexp can match. We have to check both
5141 ;; sides and decide what to do.
5142 (cond ((and (not prev
) (not next
)) s
)
5143 ((not prev
) (funcall get-smart-quote s
'open
))
5144 ((and (not next
) (zerop pre-blank
))
5145 (funcall get-smart-quote s
'close
))
5147 ((zerop pre-blank
) (funcall get-smart-quote s
'apostrophe
))
5148 (t (funcall get-smart-quote
'open
)))
5149 ;; 1. Replace quote character at the beginning of S.
5152 ((and prev
(zerop pre-blank
)
5153 (string-match (nth 2 org-export-smart-quotes-regexps
) s
))
5154 (setq s
(replace-match
5155 (funcall get-smart-quote
(match-string 1 s
) 'apostrophe
)
5158 ((and prev
(zerop pre-blank
)
5159 (string-match (nth 1 org-export-smart-quotes-regexps
) s
))
5160 (setq s
(replace-match
5161 (funcall get-smart-quote
(match-string 1 s
) 'close
)
5164 ((and (or (not prev
) (> pre-blank
0))
5165 (string-match (nth 0 org-export-smart-quotes-regexps
) s
))
5166 (setq s
(replace-match
5167 (funcall get-smart-quote
(match-string 1 s
) 'open
)
5169 ;; 2. Replace quotes in the middle of the string.
5170 (setq s
(replace-regexp-in-string
5172 (nth 3 org-export-smart-quotes-regexps
)
5174 (funcall get-smart-quote
(match-string 1 text
) 'open
))
5176 (setq s
(replace-regexp-in-string
5178 (nth 4 org-export-smart-quotes-regexps
)
5180 (funcall get-smart-quote
(match-string 1 text
) 'close
))
5182 (setq s
(replace-regexp-in-string
5184 (nth 5 org-export-smart-quotes-regexps
)
5186 (funcall get-smart-quote
(match-string 1 text
) 'apostrophe
))
5188 ;; 3. Replace quote character at the end of S.
5191 ((and next
(string-match (nth 8 org-export-smart-quotes-regexps
) s
))
5192 (setq s
(replace-match
5193 (funcall get-smart-quote
(match-string 1 s
) 'apostrophe
)
5197 (string-match (nth 7 org-export-smart-quotes-regexps
) s
))
5198 (setq s
(replace-match
5199 (funcall get-smart-quote
(match-string 1 s
) 'close
)
5202 ((and next
(string-match (nth 6 org-export-smart-quotes-regexps
) s
))
5203 (setq s
(replace-match
5204 (funcall get-smart-quote
(match-string 1 s
) 'open
)
5206 ;; Return string with smart quotes.
5211 ;; Here are various functions to retrieve information about the
5212 ;; neighborhood of a given element or object. Neighbors of interest
5213 ;; are direct parent (`org-export-get-parent'), parent headline
5214 ;; (`org-export-get-parent-headline'), first element containing an
5215 ;; object, (`org-export-get-parent-element'), parent table
5216 ;; (`org-export-get-parent-table'), previous element or object
5217 ;; (`org-export-get-previous-element') and next element or object
5218 ;; (`org-export-get-next-element').
5220 ;; defsubst org-export-get-parent must be defined before first use
5222 (define-obsolete-function-alias
5223 'org-export-get-genealogy
'org-element-lineage
"25.1")
5225 (defun org-export-get-parent-headline (blob)
5226 "Return BLOB parent headline or nil.
5227 BLOB is the element or object being considered."
5228 (org-element-lineage blob
'(headline)))
5230 (defun org-export-get-parent-element (object)
5231 "Return first element containing OBJECT or nil.
5232 OBJECT is the object to consider."
5233 (org-element-lineage object org-element-all-elements
))
5235 (defun org-export-get-parent-table (object)
5236 "Return OBJECT parent table or nil.
5237 OBJECT is either a `table-cell' or `table-element' type object."
5238 (org-element-lineage object
'(table)))
5240 (defun org-export-get-previous-element (blob info
&optional n
)
5241 "Return previous element or object.
5243 BLOB is an element or object. INFO is a plist used as
5244 a communication channel. Return previous exportable element or
5245 object, a string, or nil.
5247 When optional argument N is a positive integer, return a list
5248 containing up to N siblings before BLOB, from farthest to
5249 closest. With any other non-nil value, return a list containing
5251 (let* ((secondary (org-element-secondary-p blob
))
5252 (parent (org-export-get-parent blob
))
5254 (if secondary
(org-element-property secondary parent
)
5255 (org-element-contents parent
)))
5258 (dolist (obj (cdr (memq blob
(reverse siblings
))) prev
)
5259 (cond ((memq obj
(plist-get info
:ignore-list
)))
5260 ((null n
) (throw 'exit obj
))
5261 ((not (wholenump n
)) (push obj prev
))
5262 ((zerop n
) (throw 'exit prev
))
5263 (t (decf n
) (push obj prev
)))))))
5265 (defun org-export-get-next-element (blob info
&optional n
)
5266 "Return next element or object.
5268 BLOB is an element or object. INFO is a plist used as
5269 a communication channel. Return next exportable element or
5270 object, a string, or nil.
5272 When optional argument N is a positive integer, return a list
5273 containing up to N siblings after BLOB, from closest to farthest.
5274 With any other non-nil value, return a list containing all of
5276 (let* ((secondary (org-element-secondary-p blob
))
5277 (parent (org-export-get-parent blob
))
5280 (if secondary
(org-element-property secondary parent
)
5281 (org-element-contents parent
)))))
5284 (dolist (obj siblings
(nreverse next
))
5285 (cond ((memq obj
(plist-get info
:ignore-list
)))
5286 ((null n
) (throw 'exit obj
))
5287 ((not (wholenump n
)) (push obj next
))
5288 ((zerop n
) (throw 'exit
(nreverse next
)))
5289 (t (decf n
) (push obj next
)))))))
5294 ;; `org-export-translate' translates a string according to the language
5295 ;; specified by the LANGUAGE keyword. `org-export-dictionary' contains
5296 ;; the dictionary used for the translation.
5298 (defconst org-export-dictionary
5300 ("fr" :default
"%e %n : %c" :html
"%e %n : %c"))
5302 ("ca" :default
"Autor")
5303 ("cs" :default
"Autor")
5304 ("da" :default
"Forfatter")
5305 ("de" :default
"Autor")
5306 ("eo" :html
"Aŭtoro")
5307 ("es" :default
"Autor")
5308 ("et" :default
"Autor")
5309 ("fi" :html
"Tekijä")
5310 ("fr" :default
"Auteur")
5311 ("hu" :default
"Szerzõ")
5312 ("is" :html
"Höfundur")
5313 ("it" :default
"Autore")
5314 ("ja" :default
"著者" :html
"著者")
5315 ("nl" :default
"Auteur")
5316 ("no" :default
"Forfatter")
5317 ("nb" :default
"Forfatter")
5318 ("nn" :default
"Forfattar")
5319 ("pl" :default
"Autor")
5320 ("pt_BR" :default
"Autor")
5321 ("ru" :html
"Автор" :utf-8
"Автор")
5322 ("sv" :html
"Författare")
5323 ("uk" :html
"Автор" :utf-8
"Автор")
5324 ("zh-CN" :html
"作者" :utf-8
"作者")
5325 ("zh-TW" :html
"作者" :utf-8
"作者"))
5326 ("Continued from previous page"
5327 ("de" :default
"Fortsetzung von vorheriger Seite")
5328 ("es" :default
"Continúa de la página anterior")
5329 ("fr" :default
"Suite de la page précédente")
5330 ("it" :default
"Continua da pagina precedente")
5331 ("ja" :default
"前ページからの続き")
5332 ("nl" :default
"Vervolg van vorige pagina")
5333 ("pt" :default
"Continuação da página anterior")
5334 ("ru" :html
"(Продолжение)"
5335 :utf-8
"(Продолжение)"))
5336 ("Continued on next page"
5337 ("de" :default
"Fortsetzung nächste Seite")
5338 ("es" :default
"Continúa en la siguiente página")
5339 ("fr" :default
"Suite page suivante")
5340 ("it" :default
"Continua alla pagina successiva")
5341 ("ja" :default
"次ページに続く")
5342 ("nl" :default
"Vervolg op volgende pagina")
5343 ("pt" :default
"Continua na página seguinte")
5344 ("ru" :html
"(Продолжение следует)"
5345 :utf-8
"(Продолжение следует)"))
5347 ("ca" :default
"Data")
5348 ("cs" :default
"Datum")
5349 ("da" :default
"Dato")
5350 ("de" :default
"Datum")
5351 ("eo" :default
"Dato")
5352 ("es" :default
"Fecha")
5353 ("et" :html
"Kuupäev" :utf-8
"Kuupäev")
5354 ("fi" :html
"Päivämäärä")
5355 ("hu" :html
"Dátum")
5356 ("is" :default
"Dagsetning")
5357 ("it" :default
"Data")
5358 ("ja" :default
"日付" :html
"日付")
5359 ("nl" :default
"Datum")
5360 ("no" :default
"Dato")
5361 ("nb" :default
"Dato")
5362 ("nn" :default
"Dato")
5363 ("pl" :default
"Data")
5364 ("pt_BR" :default
"Data")
5365 ("ru" :html
"Дата" :utf-8
"Дата")
5366 ("sv" :default
"Datum")
5367 ("uk" :html
"Дата" :utf-8
"Дата")
5368 ("zh-CN" :html
"日期" :utf-8
"日期")
5369 ("zh-TW" :html
"日期" :utf-8
"日期"))
5371 ("da" :default
"Ligning")
5372 ("de" :default
"Gleichung")
5373 ("es" :html
"Ecuación" :default
"Ecuación")
5374 ("et" :html
"Võrrand" :utf-8
"Võrrand")
5375 ("fr" :ascii
"Equation" :default
"Équation")
5376 ("ja" :default
"方程式")
5377 ("no" :default
"Ligning")
5378 ("nb" :default
"Ligning")
5379 ("nn" :default
"Likning")
5380 ("pt_BR" :html
"Equação" :default
"Equação" :ascii
"Equacao")
5381 ("ru" :html
"Уравнение"
5383 ("sv" :default
"Ekvation")
5384 ("zh-CN" :html
"方程" :utf-8
"方程"))
5386 ("da" :default
"Figur")
5387 ("de" :default
"Abbildung")
5388 ("es" :default
"Figura")
5389 ("et" :default
"Joonis")
5390 ("ja" :default
"図" :html
"図")
5391 ("no" :default
"Illustrasjon")
5392 ("nb" :default
"Illustrasjon")
5393 ("nn" :default
"Illustrasjon")
5394 ("pt_BR" :default
"Figura")
5395 ("ru" :html
"Рисунок" :utf-8
"Рисунок")
5396 ("sv" :default
"Illustration")
5397 ("zh-CN" :html
"图" :utf-8
"图"))
5399 ("da" :default
"Figur %d")
5400 ("de" :default
"Abbildung %d:")
5401 ("es" :default
"Figura %d:")
5402 ("et" :default
"Joonis %d:")
5403 ("fr" :default
"Figure %d :" :html
"Figure %d :")
5404 ("ja" :default
"図%d: " :html
"図%d: ")
5405 ("no" :default
"Illustrasjon %d")
5406 ("nb" :default
"Illustrasjon %d")
5407 ("nn" :default
"Illustrasjon %d")
5408 ("pt_BR" :default
"Figura %d:")
5409 ("ru" :html
"Рис. %d.:" :utf-8
"Рис. %d.:")
5410 ("sv" :default
"Illustration %d")
5411 ("zh-CN" :html
"图%d " :utf-8
"图%d "))
5413 ("ca" :html
"Peus de pàgina")
5414 ("cs" :default
"Pozn\xe1mky pod carou")
5415 ("da" :default
"Fodnoter")
5416 ("de" :html
"Fußnoten" :default
"Fußnoten")
5417 ("eo" :default
"Piednotoj")
5418 ("es" :html
"Nota al pie de página" :default
"Nota al pie de página")
5419 ("et" :html
"Allmärkused" :utf-8
"Allmärkused")
5420 ("fi" :default
"Alaviitteet")
5421 ("fr" :default
"Notes de bas de page")
5422 ("hu" :html
"Lábjegyzet")
5423 ("is" :html
"Aftanmálsgreinar")
5424 ("it" :html
"Note a piè di pagina")
5425 ("ja" :default
"脚注" :html
"脚注")
5426 ("nl" :default
"Voetnoten")
5427 ("no" :default
"Fotnoter")
5428 ("nb" :default
"Fotnoter")
5429 ("nn" :default
"Fotnotar")
5430 ("pl" :default
"Przypis")
5431 ("pt_BR" :html
"Notas de Rodapé" :default
"Notas de Rodapé" :ascii
"Notas de Rodape")
5432 ("ru" :html
"Сноски" :utf-8
"Сноски")
5433 ("sv" :default
"Fotnoter")
5434 ("uk" :html
"Примітки"
5436 ("zh-CN" :html
"脚注" :utf-8
"脚注")
5437 ("zh-TW" :html
"腳註" :utf-8
"腳註"))
5439 ("da" :default
"Programmer")
5440 ("de" :default
"Programmauflistungsverzeichnis")
5441 ("es" :default
"Indice de Listados de programas")
5442 ("et" :default
"Loendite nimekiri")
5443 ("fr" :default
"Liste des programmes")
5444 ("ja" :default
"ソースコード目次")
5445 ("no" :default
"Dataprogrammer")
5446 ("nb" :default
"Dataprogrammer")
5447 ("ru" :html
"Список распечаток"
5448 :utf-8
"Список распечаток")
5449 ("zh-CN" :html
"代码目录" :utf-8
"代码目录"))
5451 ("da" :default
"Tabeller")
5452 ("de" :default
"Tabellenverzeichnis")
5453 ("es" :default
"Indice de tablas")
5454 ("et" :default
"Tabelite nimekiri")
5455 ("fr" :default
"Liste des tableaux")
5456 ("ja" :default
"表目次")
5457 ("no" :default
"Tabeller")
5458 ("nb" :default
"Tabeller")
5459 ("nn" :default
"Tabeller")
5460 ("pt_BR" :default
"Índice de Tabelas" :ascii
"Indice de Tabelas")
5461 ("ru" :html
"Список таблиц"
5462 :utf-8
"Список таблиц")
5463 ("sv" :default
"Tabeller")
5464 ("zh-CN" :html
"表格目录" :utf-8
"表格目录"))
5466 ("da" :default
"Program %d")
5467 ("de" :default
"Programmlisting %d")
5468 ("es" :default
"Listado de programa %d")
5469 ("et" :default
"Loend %d")
5470 ("fr" :default
"Programme %d :" :html
"Programme %d :")
5471 ("ja" :default
"ソースコード%d:")
5472 ("no" :default
"Dataprogram %d")
5473 ("nb" :default
"Dataprogram %d")
5474 ("pt_BR" :default
"Listagem %d")
5475 ("ru" :html
"Распечатка %d.:"
5476 :utf-8
"Распечатка %d.:")
5477 ("zh-CN" :html
"代码%d " :utf-8
"代码%d "))
5479 ("fr" :ascii
"References" :default
"Références")
5480 ("de" :default
"Quellen"))
5482 ("da" :default
"jævnfør afsnit %s")
5483 ("de" :default
"siehe Abschnitt %s")
5484 ("es" :default
"vea seccion %s")
5485 ("et" :html
"Vaata peatükki %s" :utf-8
"Vaata peatükki %s")
5486 ("fr" :default
"cf. section %s")
5487 ("ja" :default
"セクション %s を参照")
5488 ("pt_BR" :html
"Veja a seção %s" :default
"Veja a seção %s"
5489 :ascii
"Veja a secao %s")
5490 ("ru" :html
"См. раздел %s"
5491 :utf-8
"См. раздел %s")
5492 ("zh-CN" :html
"参见第%s节" :utf-8
"参见第%s节"))
5494 ("de" :default
"Tabelle")
5495 ("es" :default
"Tabla")
5496 ("et" :default
"Tabel")
5497 ("fr" :default
"Tableau")
5498 ("ja" :default
"表" :html
"表")
5499 ("pt_BR" :default
"Tabela")
5500 ("ru" :html
"Таблица"
5502 ("zh-CN" :html
"表" :utf-8
"表"))
5504 ("da" :default
"Tabel %d")
5505 ("de" :default
"Tabelle %d")
5506 ("es" :default
"Tabla %d")
5507 ("et" :default
"Tabel %d")
5508 ("fr" :default
"Tableau %d :")
5509 ("ja" :default
"表%d:" :html
"表%d:")
5510 ("no" :default
"Tabell %d")
5511 ("nb" :default
"Tabell %d")
5512 ("nn" :default
"Tabell %d")
5513 ("pt_BR" :default
"Tabela %d")
5514 ("ru" :html
"Таблица %d.:"
5515 :utf-8
"Таблица %d.:")
5516 ("sv" :default
"Tabell %d")
5517 ("zh-CN" :html
"表%d " :utf-8
"表%d "))
5518 ("Table of Contents"
5519 ("ca" :html
"Índex")
5520 ("cs" :default
"Obsah")
5521 ("da" :default
"Indhold")
5522 ("de" :default
"Inhaltsverzeichnis")
5523 ("eo" :default
"Enhavo")
5524 ("es" :html
"Índice")
5525 ("et" :default
"Sisukord")
5526 ("fi" :html
"Sisällysluettelo")
5527 ("fr" :ascii
"Sommaire" :default
"Table des matières")
5528 ("hu" :html
"Tartalomjegyzék")
5529 ("is" :default
"Efnisyfirlit")
5530 ("it" :default
"Indice")
5531 ("ja" :default
"目次" :html
"目次")
5532 ("nl" :default
"Inhoudsopgave")
5533 ("no" :default
"Innhold")
5534 ("nb" :default
"Innhold")
5535 ("nn" :default
"Innhald")
5536 ("pl" :html
"Spis treści")
5537 ("pt_BR" :html
"Índice" :utf8
"Índice" :ascii
"Indice")
5538 ("ru" :html
"Содержание"
5539 :utf-8
"Содержание")
5540 ("sv" :html
"Innehåll")
5541 ("uk" :html
"Зміст" :utf-8
"Зміст")
5542 ("zh-CN" :html
"目录" :utf-8
"目录")
5543 ("zh-TW" :html
"目錄" :utf-8
"目錄"))
5544 ("Unknown reference"
5545 ("da" :default
"ukendt reference")
5546 ("de" :default
"Unbekannter Verweis")
5547 ("es" :default
"referencia desconocida")
5548 ("et" :default
"Tundmatu viide")
5549 ("fr" :ascii
"Destination inconnue" :default
"Référence inconnue")
5550 ("ja" :default
"不明な参照先")
5551 ("pt_BR" :default
"Referência desconhecida"
5552 :ascii
"Referencia desconhecida")
5553 ("ru" :html
"Неизвестная ссылка"
5554 :utf-8
"Неизвестная ссылка")
5555 ("zh-CN" :html
"未知引用" :utf-8
"未知引用")))
5556 "Dictionary for export engine.
5558 Alist whose car is the string to translate and cdr is an alist
5559 whose car is the language string and cdr is a plist whose
5560 properties are possible charsets and values translated terms.
5562 It is used as a database for `org-export-translate'. Since this
5563 function returns the string as-is if no translation was found,
5564 the variable only needs to record values different from the
5567 (defun org-export-translate (s encoding info
)
5568 "Translate string S according to language specification.
5570 ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
5571 and `:utf-8'. INFO is a plist used as a communication channel.
5573 Translation depends on `:language' property. Return the
5574 translated string. If no translation is found, try to fall back
5575 to `:default' encoding. If it fails, return S."
5576 (let* ((lang (plist-get info
:language
))
5577 (translations (cdr (assoc lang
5578 (cdr (assoc s org-export-dictionary
))))))
5579 (or (plist-get translations encoding
)
5580 (plist-get translations
:default
)
5585 ;;; Asynchronous Export
5587 ;; `org-export-async-start' is the entry point for asynchronous
5588 ;; export. It recreates current buffer (including visibility,
5589 ;; narrowing and visited file) in an external Emacs process, and
5590 ;; evaluates a command there. It then applies a function on the
5591 ;; returned results in the current process.
5593 ;; At a higher level, `org-export-to-buffer' and `org-export-to-file'
5594 ;; allow to export to a buffer or a file, asynchronously or not.
5596 ;; `org-export-output-file-name' is an auxiliary function meant to be
5597 ;; used with `org-export-to-file'. With a given extension, it tries
5598 ;; to provide a canonical file name to write export output to.
5600 ;; Asynchronously generated results are never displayed directly.
5601 ;; Instead, they are stored in `org-export-stack-contents'. They can
5602 ;; then be retrieved by calling `org-export-stack'.
5604 ;; Export Stack is viewed through a dedicated major mode
5605 ;;`org-export-stack-mode' and tools: `org-export-stack-refresh',
5606 ;;`org-export-stack-delete', `org-export-stack-view' and
5607 ;;`org-export-stack-clear'.
5609 ;; For back-ends, `org-export-add-to-stack' add a new source to stack.
5610 ;; It should be used whenever `org-export-async-start' is called.
5612 (defmacro org-export-async-start
(fun &rest body
)
5613 "Call function FUN on the results returned by BODY evaluation.
5615 FUN is an anonymous function of one argument. BODY evaluation
5616 happens in an asynchronous process, from a buffer which is an
5617 exact copy of the current one.
5619 Use `org-export-add-to-stack' in FUN in order to register results
5622 This is a low level function. See also `org-export-to-buffer'
5623 and `org-export-to-file' for more specialized functions."
5624 (declare (indent 1) (debug t
))
5625 (org-with-gensyms (process temp-file copy-fun proc-buffer coding
)
5626 ;; Write the full sexp evaluating BODY in a copy of the current
5627 ;; buffer to a temporary file, as it may be too long for program
5628 ;; args in `start-process'.
5629 `(with-temp-message "Initializing asynchronous export process"
5630 (let ((,copy-fun
(org-export--generate-copy-script (current-buffer)))
5631 (,temp-file
(make-temp-file "org-export-process"))
5632 (,coding buffer-file-coding-system
))
5633 (with-temp-file ,temp-file
5635 ;; Null characters (from variable values) are inserted
5636 ;; within the file. As a consequence, coding system for
5637 ;; buffer contents will not be recognized properly. So,
5638 ;; we make sure it is the same as the one used to display
5639 ;; the original buffer.
5640 (format ";; -*- coding: %s; -*-\n%S"
5643 (when org-export-async-debug
'(setq debug-on-error t
))
5644 ;; Ignore `kill-emacs-hook' and code evaluation
5645 ;; queries from Babel as we need a truly
5646 ;; non-interactive process.
5647 (setq kill-emacs-hook nil
5648 org-babel-confirm-evaluate-answer-no t
)
5649 ;; Initialize export framework.
5651 ;; Re-create current buffer there.
5652 (funcall ,,copy-fun
)
5653 (restore-buffer-modified-p nil
)
5654 ;; Sexp to evaluate in the buffer.
5655 (print (progn ,,@body
))))))
5656 ;; Start external process.
5657 (let* ((process-connection-type nil
)
5658 (,proc-buffer
(generate-new-buffer-name "*Org Export Process*"))
5663 (list "org-export-process"
5665 (expand-file-name invocation-name invocation-directory
)
5667 (if org-export-async-init-file
5668 (list "-Q" "-l" org-export-async-init-file
)
5669 (list "-l" user-init-file
))
5670 (list "-l" ,temp-file
)))))
5671 ;; Register running process in stack.
5672 (org-export-add-to-stack (get-buffer ,proc-buffer
) nil
,process
)
5673 ;; Set-up sentinel in order to catch results.
5674 (let ((handler ,fun
))
5675 (set-process-sentinel
5678 (let ((proc-buffer (process-buffer p
)))
5679 (when (eq (process-status p
) 'exit
)
5681 (if (zerop (process-exit-status p
))
5684 (with-current-buffer proc-buffer
5685 (goto-char (point-max))
5687 (read (current-buffer)))))
5688 (funcall ,handler results
))
5689 (unless org-export-async-debug
5690 (and (get-buffer proc-buffer
)
5691 (kill-buffer proc-buffer
))))
5692 (org-export-add-to-stack proc-buffer nil p
)
5694 (message "Process '%s' exited abnormally" p
))
5695 (unless org-export-async-debug
5696 (delete-file ,,temp-file
)))))))))))))
5699 (defun org-export-to-buffer
5701 &optional async subtreep visible-only body-only ext-plist
5703 "Call `org-export-as' with output to a specified buffer.
5705 BACKEND is either an export back-end, as returned by, e.g.,
5706 `org-export-create-backend', or a symbol referring to
5707 a registered back-end.
5709 BUFFER is the name of the output buffer. If it already exists,
5710 it will be erased first, otherwise, it will be created.
5712 A non-nil optional argument ASYNC means the process should happen
5713 asynchronously. The resulting buffer should then be accessible
5714 through the `org-export-stack' interface. When ASYNC is nil, the
5715 buffer is displayed if `org-export-show-temporary-export-buffer'
5718 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
5719 EXT-PLIST are similar to those used in `org-export-as', which
5722 Optional argument POST-PROCESS is a function which should accept
5723 no argument. It is always called within the current process,
5724 from BUFFER, with point at its beginning. Export back-ends can
5725 use it to set a major mode there, e.g,
5727 \(defun org-latex-export-as-latex
5728 \(&optional async subtreep visible-only body-only ext-plist)
5730 \(org-export-to-buffer 'latex \"*Org LATEX Export*\"
5731 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
5733 This function returns BUFFER."
5734 (declare (indent 2))
5736 (org-export-async-start
5738 (with-current-buffer (get-buffer-create ,buffer
)
5740 (setq buffer-file-coding-system
',buffer-file-coding-system
)
5742 (goto-char (point-min))
5743 (org-export-add-to-stack (current-buffer) ',backend
)
5744 (ignore-errors (funcall ,post-process
))))
5746 ',backend
,subtreep
,visible-only
,body-only
',ext-plist
))
5748 (org-export-as backend subtreep visible-only body-only ext-plist
))
5749 (buffer (get-buffer-create buffer
))
5750 (encoding buffer-file-coding-system
))
5751 (when (and (org-string-nw-p output
) (org-export--copy-to-kill-ring-p))
5752 (org-kill-new output
))
5753 (with-current-buffer buffer
5755 (setq buffer-file-coding-system encoding
)
5757 (goto-char (point-min))
5758 (and (functionp post-process
) (funcall post-process
)))
5759 (when org-export-show-temporary-export-buffer
5760 (switch-to-buffer-other-window buffer
))
5764 (defun org-export-to-file
5765 (backend file
&optional async subtreep visible-only body-only ext-plist
5767 "Call `org-export-as' with output to a specified file.
5769 BACKEND is either an export back-end, as returned by, e.g.,
5770 `org-export-create-backend', or a symbol referring to
5771 a registered back-end. FILE is the name of the output file, as
5774 A non-nil optional argument ASYNC means the process should happen
5775 asynchronously. The resulting buffer will then be accessible
5776 through the `org-export-stack' interface.
5778 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
5779 EXT-PLIST are similar to those used in `org-export-as', which
5782 Optional argument POST-PROCESS is called with FILE as its
5783 argument and happens asynchronously when ASYNC is non-nil. It
5784 has to return a file name, or nil. Export back-ends can use this
5785 to send the output file through additional processing, e.g,
5787 \(defun org-latex-export-to-latex
5788 \(&optional async subtreep visible-only body-only ext-plist)
5790 \(let ((outfile (org-export-output-file-name \".tex\" subtreep)))
5791 \(org-export-to-file 'latex outfile
5792 async subtreep visible-only body-only ext-plist
5793 \(lambda (file) (org-latex-compile file)))
5795 The function returns either a file name returned by POST-PROCESS,
5797 (declare (indent 2))
5798 (if (not (file-writable-p file
)) (error "Output file not writable")
5799 (let ((ext-plist (org-combine-plists `(:output-file
,file
) ext-plist
))
5800 (encoding (or org-export-coding-system buffer-file-coding-system
)))
5802 (org-export-async-start
5804 (org-export-add-to-stack (expand-file-name file
) ',backend
))
5807 ',backend
,subtreep
,visible-only
,body-only
5811 (let ((coding-system-for-write ',encoding
))
5812 (write-file ,file
)))
5813 (or (ignore-errors (funcall ',post-process
,file
)) ,file
)))
5814 (let ((output (org-export-as
5815 backend subtreep visible-only body-only ext-plist
)))
5818 (let ((coding-system-for-write encoding
))
5820 (when (and (org-export--copy-to-kill-ring-p) (org-string-nw-p output
))
5821 (org-kill-new output
))
5822 ;; Get proper return value.
5823 (or (and (functionp post-process
) (funcall post-process file
))
5826 (defun org-export-output-file-name (extension &optional subtreep pub-dir
)
5827 "Return output file's name according to buffer specifications.
5829 EXTENSION is a string representing the output file extension,
5830 with the leading dot.
5832 With a non-nil optional argument SUBTREEP, try to determine
5833 output file's name by looking for \"EXPORT_FILE_NAME\" property
5834 of subtree at point.
5836 When optional argument PUB-DIR is set, use it as the publishing
5839 When optional argument VISIBLE-ONLY is non-nil, don't export
5840 contents of hidden elements.
5842 Return file name as a string."
5843 (let* ((visited-file (buffer-file-name (buffer-base-buffer)))
5845 ;; File name may come from EXPORT_FILE_NAME subtree
5846 ;; property, assuming point is at beginning of said
5848 (file-name-sans-extension
5852 (ignore-errors (org-back-to-heading) (point)))
5853 "EXPORT_FILE_NAME" t
))
5854 ;; File name may be extracted from buffer's associated
5856 (and visited-file
(file-name-nondirectory visited-file
))
5857 ;; Can't determine file name on our own: Ask user.
5858 (let ((read-file-name-function
5859 (and org-completion-use-ido
'ido-read-file-name
)))
5861 "Output file: " pub-dir nil nil nil
5863 (string= (file-name-extension name t
) extension
)))))))
5865 ;; Build file name. Enforce EXTENSION over whatever user
5866 ;; may have come up with. PUB-DIR, if defined, always has
5867 ;; precedence over any provided path.
5870 (concat (file-name-as-directory pub-dir
)
5871 (file-name-nondirectory base-name
)
5873 ((file-name-absolute-p base-name
) (concat base-name extension
))
5874 (t (concat (file-name-as-directory ".") base-name extension
)))))
5875 ;; If writing to OUTPUT-FILE would overwrite original file, append
5876 ;; EXTENSION another time to final name.
5877 (if (and visited-file
(org-file-equal-p visited-file output-file
))
5878 (concat output-file extension
)
5881 (defun org-export-add-to-stack (source backend
&optional process
)
5882 "Add a new result to export stack if not present already.
5884 SOURCE is a buffer or a file name containing export results.
5885 BACKEND is a symbol representing export back-end used to generate
5888 Entries already pointing to SOURCE and unavailable entries are
5889 removed beforehand. Return the new stack."
5890 (setq org-export-stack-contents
5891 (cons (list source backend
(or process
(current-time)))
5892 (org-export-stack-remove source
))))
5894 (defun org-export-stack ()
5895 "Menu for asynchronous export results and running processes."
5897 (let ((buffer (get-buffer-create "*Org Export Stack*")))
5899 (when (zerop (buffer-size)) (org-export-stack-mode))
5900 (org-export-stack-refresh)
5901 (pop-to-buffer buffer
))
5902 (message "Type \"q\" to quit, \"?\" for help"))
5904 (defun org-export--stack-source-at-point ()
5905 "Return source from export results at point in stack."
5906 (let ((source (car (nth (1- (org-current-line)) org-export-stack-contents
))))
5907 (if (not source
) (error "Source unavailable, please refresh buffer")
5908 (let ((source-name (if (stringp source
) source
(buffer-name source
))))
5911 (looking-at (concat ".* +" (regexp-quote source-name
) "$")))
5913 ;; SOURCE is not consistent with current line. The stack
5914 ;; view is outdated.
5915 (error "Source unavailable; type `g' to update buffer"))))))
5917 (defun org-export-stack-clear ()
5918 "Remove all entries from export stack."
5920 (setq org-export-stack-contents nil
))
5922 (defun org-export-stack-refresh (&rest dummy
)
5923 "Refresh the asynchronous export stack.
5924 DUMMY is ignored. Unavailable sources are removed from the list.
5925 Return the new stack."
5926 (let ((inhibit-read-only t
))
5933 (let ((proc-p (processp (nth 2 entry
))))
5936 (format " %-12s " (or (nth 1 entry
) ""))
5938 (let ((data (nth 2 entry
)))
5939 (if proc-p
(format " %6s " (process-status data
))
5940 ;; Compute age of the results.
5943 (float-time (time-since data
)))))
5946 (let ((source (car entry
)))
5947 (if (stringp source
) source
5948 (buffer-name source
)))))))
5949 ;; Clear stack from exited processes, dead buffers or
5950 ;; non-existent files.
5951 (setq org-export-stack-contents
5954 (if (processp (nth 2 el
))
5955 (buffer-live-p (process-buffer (nth 2 el
)))
5956 (let ((source (car el
)))
5957 (if (bufferp source
) (buffer-live-p source
)
5958 (file-exists-p source
)))))
5959 org-export-stack-contents
)) "\n")))))))
5961 (defun org-export-stack-remove (&optional source
)
5962 "Remove export results at point from stack.
5963 If optional argument SOURCE is non-nil, remove it instead."
5965 (let ((source (or source
(org-export--stack-source-at-point))))
5966 (setq org-export-stack-contents
5967 (org-remove-if (lambda (el) (equal (car el
) source
))
5968 org-export-stack-contents
))))
5970 (defun org-export-stack-view (&optional in-emacs
)
5971 "View export results at point in stack.
5972 With an optional prefix argument IN-EMACS, force viewing files
5975 (let ((source (org-export--stack-source-at-point)))
5976 (cond ((processp source
)
5977 (org-switch-to-buffer-other-window (process-buffer source
)))
5978 ((bufferp source
) (org-switch-to-buffer-other-window source
))
5979 (t (org-open-file source in-emacs
)))))
5981 (defvar org-export-stack-mode-map
5982 (let ((km (make-sparse-keymap)))
5983 (define-key km
" " 'next-line
)
5984 (define-key km
"n" 'next-line
)
5985 (define-key km
"\C-n" 'next-line
)
5986 (define-key km
[down] 'next-line)
5987 (define-key km "p" 'previous-line)
5988 (define-key km "\C-p" 'previous-line)
5989 (define-key km "\C-?" 'previous-line)
5990 (define-key km [up] 'previous-line)
5991 (define-key km "C" 'org-export-stack-clear)
5992 (define-key km "v" 'org-export-stack-view)
5993 (define-key km (kbd "RET") 'org-export-stack-view)
5994 (define-key km "d" 'org-export-stack-remove)
5996 "Keymap for Org Export Stack.")
5998 (define-derived-mode org-export-stack-mode special-mode "Org-Stack"
5999 "Mode for displaying asynchronous export stack.
6001 Type \\[org-export-stack] to visualize the asynchronous export
6004 In an Org Export Stack buffer, use \\<org-export-stack-mode-map>\\[org-export-stack-view] to view export output
6005 on current line, \\[org-export-stack-remove] to remove it from the stack and \\[org-export-stack-clear] to clear
6008 Removing entries in an Org Export Stack buffer doesn't affect
6009 files or buffers, only the display.
6011 \\{org-export-stack-mode-map}"
6014 (setq buffer-read-only t
6019 (format " %-12s | %6s | %s" "Back-End" "Age" "Source")))
6020 (org-add-hook 'post-command-hook 'org-export-stack-refresh nil t)
6021 (set (make-local-variable 'revert-buffer-function)
6022 'org-export-stack-refresh))
6028 ;; `org-export-dispatch' is the standard interactive way to start an
6029 ;; export process. It uses `org-export--dispatch-ui' as a subroutine
6030 ;; for its interface, which, in turn, delegates response to key
6031 ;; pressed to `org-export--dispatch-action'.
6034 (defun org-export-dispatch (&optional arg)
6035 "Export dispatcher for Org mode.
6037 It provides an access to common export related tasks in a buffer.
6038 Its interface comes in two flavors: standard and expert.
6040 While both share the same set of bindings, only the former
6041 displays the valid keys associations in a dedicated buffer.
6042 Scrolling (resp. line-wise motion) in this buffer is done with
6043 SPC and DEL (resp. C-n and C-p) keys.
6045 Set variable `org-export-dispatch-use-expert-ui' to switch to one
6046 flavor or the other.
6048 When ARG is \\[universal-argument], repeat the last export action, with the same set
6049 of options used back then, on the current buffer.
6051 When ARG is \\[universal-argument] \\[universal-argument], display the asynchronous export stack."
6054 (cond ((equal arg '(16)) '(stack))
6055 ((and arg org-export-dispatch-last-action))
6056 (t (save-window-excursion
6059 ;; Remember where we are
6060 (move-marker org-export-dispatch-last-position
6062 (org-base-buffer (current-buffer)))
6063 ;; Get and store an export command
6064 (setq org-export-dispatch-last-action
6065 (org-export--dispatch-ui
6066 (list org-export-initial-scope
6067 (and org-export-in-background 'async))
6069 org-export-dispatch-use-expert-ui)))
6070 (and (get-buffer "*Org Export Dispatcher*")
6071 (kill-buffer "*Org Export Dispatcher*")))))))
6072 (action (car input))
6073 (optns (cdr input)))
6074 (unless (memq 'subtree optns)
6075 (move-marker org-export-dispatch-last-position nil))
6077 ;; First handle special hard-coded actions.
6078 (template (org-export-insert-default-template nil optns))
6079 (stack (org-export-stack))
6080 (publish-current-file
6081 (org-publish-current-file (memq 'force optns) (memq 'async optns)))
6082 (publish-current-project
6083 (org-publish-current-project (memq 'force optns) (memq 'async optns)))
6084 (publish-choose-project
6085 (org-publish (assoc (org-icompleting-read
6087 org-publish-project-alist nil t)
6088 org-publish-project-alist)
6090 (memq 'async optns)))
6091 (publish-all (org-publish-all (memq 'force optns) (memq 'async optns)))
6095 ;; Repeating command, maybe move cursor to restore subtree
6097 (if (eq (marker-buffer org-export-dispatch-last-position)
6098 (org-base-buffer (current-buffer)))
6099 (goto-char org-export-dispatch-last-position)
6100 ;; We are in a different buffer, forget position.
6101 (move-marker org-export-dispatch-last-position nil)))
6103 ;; Return a symbol instead of a list to ease
6104 ;; asynchronous export macro use.
6105 (and (memq 'async optns) t)
6106 (and (memq 'subtree optns) t)
6107 (and (memq 'visible optns) t)
6108 (and (memq 'body optns) t)))))))
6110 (defun org-export--dispatch-ui (options first-key expertp)
6111 "Handle interface for `org-export-dispatch'.
6113 OPTIONS is a list containing current interactive options set for
6114 export. It can contain any of the following symbols:
6115 `body' toggles a body-only export
6116 `subtree' restricts export to current subtree
6117 `visible' restricts export to visible part of buffer.
6118 `force' force publishing files.
6119 `async' use asynchronous export process
6121 FIRST-KEY is the key pressed to select the first level menu. It
6122 is nil when this menu hasn't been selected yet.
6124 EXPERTP, when non-nil, triggers expert UI. In that case, no help
6125 buffer is provided, but indications about currently active
6126 options are given in the prompt. Moreover, \[?] allows to switch
6127 back to standard interface."
6129 (lambda (key &optional access-key)
6130 ;; Fontify KEY string. Optional argument ACCESS-KEY, when
6131 ;; non-nil is the required first-level key to activate
6132 ;; KEY. When its value is t, activate KEY independently
6133 ;; on the first key, if any. A nil value means KEY will
6134 ;; only be activated at first level.
6135 (if (or (eq access-key t) (eq access-key first-key))
6136 (org-propertize key 'face 'org-warning)
6140 ;; Fontify VALUE string.
6141 (org-propertize value 'face 'font-lock-variable-name-face)))
6142 ;; Prepare menu entries by extracting them from registered
6143 ;; back-ends and sorting them by access key and by ordinal,
6146 (sort (sort (delq nil
6147 (mapcar 'org-export-backend-menu
6148 org-export--registered-backends))
6150 (let ((key-a (nth 1 a))
6152 (cond ((and (numberp key-a) (numberp key-b))
6154 ((numberp key-b) t)))))
6155 'car-less-than-car))
6156 ;; Compute a list of allowed keys based on the first key
6157 ;; pressed, if any. Some keys
6158 ;; (?^B, ?^V, ?^S, ?^F, ?^A, ?&, ?# and ?q) are always
6161 (nconc (list 2 22 19 6 1)
6162 (if (not first-key) (org-uniquify (mapcar 'car entries))
6164 (dolist (entry entries (sort (mapcar 'car sub-menu) '<))
6165 (when (eq (car entry) first-key)
6166 (setq sub-menu (append (nth 2 entry) sub-menu))))))
6167 (cond ((eq first-key ?P) (list ?f ?p ?x ?a))
6168 ((not first-key) (list ?P)))
6170 (when expertp (list ??))
6172 ;; Build the help menu for standard UI.
6176 ;; Options are hard-coded.
6177 (format "[%s] Body only: %s [%s] Visible only: %s
6178 \[%s] Export scope: %s [%s] Force publishing: %s
6179 \[%s] Async export: %s\n\n"
6180 (funcall fontify-key "C-b" t)
6181 (funcall fontify-value
6182 (if (memq 'body options) "On " "Off"))
6183 (funcall fontify-key "C-v" t)
6184 (funcall fontify-value
6185 (if (memq 'visible options) "On " "Off"))
6186 (funcall fontify-key "C-s" t)
6187 (funcall fontify-value
6188 (if (memq 'subtree options) "Subtree" "Buffer "))
6189 (funcall fontify-key "C-f" t)
6190 (funcall fontify-value
6191 (if (memq 'force options) "On " "Off"))
6192 (funcall fontify-key "C-a" t)
6193 (funcall fontify-value
6194 (if (memq 'async options) "On " "Off")))
6195 ;; Display registered back-end entries. When a key
6196 ;; appears for the second time, do not create another
6197 ;; entry, but append its sub-menu to existing menu.
6201 (let ((top-key (car entry)))
6203 (unless (eq top-key last-key)
6204 (setq last-key top-key)
6205 (format "\n[%s] %s\n"
6206 (funcall fontify-key (char-to-string top-key))
6208 (let ((sub-menu (nth 2 entry)))
6209 (unless (functionp sub-menu)
6210 ;; Split sub-menu into two columns.
6217 (if (zerop (mod index 2)) " [%s] %-26s"
6219 (funcall fontify-key
6220 (char-to-string (car sub-entry))
6224 (when (zerop (mod index 2)) "\n"))))))))
6226 ;; Publishing menu is hard-coded.
6227 (format "\n[%s] Publish
6228 [%s] Current file [%s] Current project
6229 [%s] Choose project [%s] All projects\n\n\n"
6230 (funcall fontify-key "P")
6231 (funcall fontify-key "f" ?P)
6232 (funcall fontify-key "p" ?P)
6233 (funcall fontify-key "x" ?P)
6234 (funcall fontify-key "a" ?P))
6235 (format "[%s] Export stack [%s] Insert template\n"
6236 (funcall fontify-key "&" t)
6237 (funcall fontify-key "#" t))
6239 (funcall fontify-key "q" t)
6240 (if first-key "Main menu" "Exit")))))
6241 ;; Build prompts for both standard and expert UI.
6242 (standard-prompt (unless expertp "Export command: "))
6246 "Export command (C-%s%s%s%s%s) [%s]: "
6247 (if (memq 'body options) (funcall fontify-key "b" t) "b")
6248 (if (memq 'visible options) (funcall fontify-key "v" t) "v")
6249 (if (memq 'subtree options) (funcall fontify-key "s" t) "s")
6250 (if (memq 'force options) (funcall fontify-key "f" t) "f")
6251 (if (memq 'async options) (funcall fontify-key "a" t) "a")
6252 (mapconcat (lambda (k)
6253 ;; Strip control characters.
6254 (unless (< k 27) (char-to-string k)))
6255 allowed-keys "")))))
6256 ;; With expert UI, just read key with a fancy prompt. In standard
6257 ;; UI, display an intrusive help buffer.
6259 (org-export--dispatch-action
6260 expert-prompt allowed-keys entries options first-key expertp)
6261 ;; At first call, create frame layout in order to display menu.
6262 (unless (get-buffer "*Org Export Dispatcher*")
6263 (delete-other-windows)
6264 (org-switch-to-buffer-other-window
6265 (get-buffer-create "*Org Export Dispatcher*"))
6266 (setq cursor-type nil
6267 header-line-format "Use SPC, DEL, C-n or C-p to navigate.")
6268 ;; Make sure that invisible cursor will not highlight square
6270 (set-syntax-table (copy-syntax-table))
6271 (modify-syntax-entry ?\[ "w"))
6272 ;; At this point, the buffer containing the menu exists and is
6273 ;; visible in the current window. So, refresh it.
6274 (with-current-buffer "*Org Export Dispatcher*"
6275 ;; Refresh help. Maintain display continuity by re-visiting
6276 ;; previous window position.
6277 (let ((pos (window-start)))
6280 (set-window-start nil pos)))
6281 (org-fit-window-to-buffer)
6282 (org-export--dispatch-action
6283 standard-prompt allowed-keys entries options first-key expertp))))
6285 (defun org-export--dispatch-action
6286 (prompt allowed-keys entries options first-key expertp)
6287 "Read a character from command input and act accordingly.
6289 PROMPT is the displayed prompt, as a string. ALLOWED-KEYS is
6290 a list of characters available at a given step in the process.
6291 ENTRIES is a list of menu entries. OPTIONS, FIRST-KEY and
6292 EXPERTP are the same as defined in `org-export--dispatch-ui',
6295 Toggle export options when required. Otherwise, return value is
6296 a list with action as CAR and a list of interactive export
6299 ;; Scrolling: when in non-expert mode, act on motion keys (C-n,
6301 (while (and (setq key (read-char-exclusive prompt))
6303 (memq key '(14 16 ?\s ?\d)))
6305 (14 (if (not (pos-visible-in-window-p (point-max)))
6306 (ignore-errors (scroll-up 1))
6307 (message "End of buffer")
6309 (16 (if (not (pos-visible-in-window-p (point-min)))
6310 (ignore-errors (scroll-down 1))
6311 (message "Beginning of buffer")
6313 (?\s (if (not (pos-visible-in-window-p (point-max)))
6315 (message "End of buffer")
6317 (?\d (if (not (pos-visible-in-window-p (point-min)))
6319 (message "Beginning of buffer")
6322 ;; Ignore undefined associations.
6323 ((not (memq key allowed-keys))
6325 (unless expertp (message "Invalid key") (sit-for 1))
6326 (org-export--dispatch-ui options first-key expertp))
6327 ;; q key at first level aborts export. At second level, cancel
6328 ;; first key instead.
6329 ((eq key ?q) (if (not first-key) (error "Export aborted")
6330 (org-export--dispatch-ui options nil expertp)))
6331 ;; Help key: Switch back to standard interface if expert UI was
6333 ((eq key ??) (org-export--dispatch-ui options first-key nil))
6334 ;; Send request for template insertion along with export scope.
6335 ((eq key ?#) (cons 'template (memq 'subtree options)))
6336 ;; Switch to asynchronous export stack.
6337 ((eq key ?&) '(stack))
6338 ;; Toggle options: C-b (2) C-v (22) C-s (19) C-f (6) C-a (1).
6339 ((memq key '(2 22 19 6 1))
6340 (org-export--dispatch-ui
6341 (let ((option (case key (2 'body) (22 'visible) (19 'subtree)
6342 (6 'force) (1 'async))))
6343 (if (memq option options) (remq option options)
6344 (cons option options)))
6346 ;; Action selected: Send key and options back to
6347 ;; `org-export-dispatch'.
6348 ((or first-key (functionp (nth 2 (assq key entries))))
6350 ((not first-key) (nth 2 (assq key entries)))
6351 ;; Publishing actions are hard-coded. Send a special
6352 ;; signal to `org-export-dispatch'.
6355 (?f 'publish-current-file)
6356 (?p 'publish-current-project)
6357 (?x 'publish-choose-project)
6359 ;; Return first action associated to FIRST-KEY + KEY
6360 ;; path. Indeed, derived backends can share the same
6363 (mapc (lambda (entry)
6364 (let ((match (assq key (nth 2 entry))))
6365 (when match (throw 'found (nth 2 match)))))
6366 (member (assq first-key entries) entries)))))
6368 ;; Otherwise, enter sub-menu.
6369 (t (org-export--dispatch-ui options key expertp)))))
6376 ;; generated-autoload-file: "org-loaddefs.el"