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-todo-keywords nil
"todo" org-export-with-todo-keywords
))
141 "Alist between export properties and ways to set them.
143 The CAR of the alist is the property name, and the CDR is a list
144 like (KEYWORD OPTION DEFAULT BEHAVIOR) where:
146 KEYWORD is a string representing a buffer keyword, or nil. Each
147 property defined this way can also be set, during subtree
148 export, through a headline property named after the keyword
149 with the \"EXPORT_\" prefix (i.e. DATE keyword and EXPORT_DATE
151 OPTION is a string that could be found in an #+OPTIONS: line.
152 DEFAULT is the default value for the property.
153 BEHAVIOR determines how Org should handle multiple keywords for
154 the same property. It is a symbol among:
155 nil Keep old value and discard the new one.
156 t Replace old value with the new one.
157 `space' Concatenate the values, separating them with a space.
158 `newline' Concatenate the values, separating them with
160 `split' Split values at white spaces, and cons them to the
163 Values set through KEYWORD and OPTION have precedence over
166 All these properties should be back-end agnostic. Back-end
167 specific properties are set through `org-export-define-backend'.
168 Properties redefined there have precedence over these.")
170 (defconst org-export-special-keywords
'("FILETAGS" "SETUPFILE" "OPTIONS")
171 "List of in-buffer keywords that require special treatment.
172 These keywords are not directly associated to a property. The
173 way they are handled must be hard-coded into
174 `org-export--get-inbuffer-options' function.")
176 (defconst org-export-filters-alist
177 '((:filter-body . org-export-filter-body-functions
)
178 (:filter-bold . org-export-filter-bold-functions
)
179 (:filter-babel-call . org-export-filter-babel-call-functions
)
180 (:filter-center-block . org-export-filter-center-block-functions
)
181 (:filter-clock . org-export-filter-clock-functions
)
182 (:filter-code . org-export-filter-code-functions
)
183 (:filter-comment . org-export-filter-comment-functions
)
184 (:filter-comment-block . org-export-filter-comment-block-functions
)
185 (:filter-diary-sexp . org-export-filter-diary-sexp-functions
)
186 (:filter-drawer . org-export-filter-drawer-functions
)
187 (:filter-dynamic-block . org-export-filter-dynamic-block-functions
)
188 (:filter-entity . org-export-filter-entity-functions
)
189 (:filter-example-block . org-export-filter-example-block-functions
)
190 (:filter-export-block . org-export-filter-export-block-functions
)
191 (:filter-export-snippet . org-export-filter-export-snippet-functions
)
192 (:filter-final-output . org-export-filter-final-output-functions
)
193 (:filter-fixed-width . org-export-filter-fixed-width-functions
)
194 (:filter-footnote-definition . org-export-filter-footnote-definition-functions
)
195 (:filter-footnote-reference . org-export-filter-footnote-reference-functions
)
196 (:filter-headline . org-export-filter-headline-functions
)
197 (:filter-horizontal-rule . org-export-filter-horizontal-rule-functions
)
198 (:filter-inline-babel-call . org-export-filter-inline-babel-call-functions
)
199 (:filter-inline-src-block . org-export-filter-inline-src-block-functions
)
200 (:filter-inlinetask . org-export-filter-inlinetask-functions
)
201 (:filter-italic . org-export-filter-italic-functions
)
202 (:filter-item . org-export-filter-item-functions
)
203 (:filter-keyword . org-export-filter-keyword-functions
)
204 (:filter-latex-environment . org-export-filter-latex-environment-functions
)
205 (:filter-latex-fragment . org-export-filter-latex-fragment-functions
)
206 (:filter-line-break . org-export-filter-line-break-functions
)
207 (:filter-link . org-export-filter-link-functions
)
208 (:filter-node-property . org-export-filter-node-property-functions
)
209 (:filter-options . org-export-filter-options-functions
)
210 (:filter-paragraph . org-export-filter-paragraph-functions
)
211 (:filter-parse-tree . org-export-filter-parse-tree-functions
)
212 (:filter-plain-list . org-export-filter-plain-list-functions
)
213 (:filter-plain-text . org-export-filter-plain-text-functions
)
214 (:filter-planning . org-export-filter-planning-functions
)
215 (:filter-property-drawer . org-export-filter-property-drawer-functions
)
216 (:filter-quote-block . org-export-filter-quote-block-functions
)
217 (:filter-radio-target . org-export-filter-radio-target-functions
)
218 (:filter-section . org-export-filter-section-functions
)
219 (:filter-special-block . org-export-filter-special-block-functions
)
220 (:filter-src-block . org-export-filter-src-block-functions
)
221 (:filter-statistics-cookie . org-export-filter-statistics-cookie-functions
)
222 (:filter-strike-through . org-export-filter-strike-through-functions
)
223 (:filter-subscript . org-export-filter-subscript-functions
)
224 (:filter-superscript . org-export-filter-superscript-functions
)
225 (:filter-table . org-export-filter-table-functions
)
226 (:filter-table-cell . org-export-filter-table-cell-functions
)
227 (:filter-table-row . org-export-filter-table-row-functions
)
228 (:filter-target . org-export-filter-target-functions
)
229 (:filter-timestamp . org-export-filter-timestamp-functions
)
230 (:filter-underline . org-export-filter-underline-functions
)
231 (:filter-verbatim . org-export-filter-verbatim-functions
)
232 (:filter-verse-block . org-export-filter-verse-block-functions
))
233 "Alist between filters properties and initial values.
235 The key of each association is a property name accessible through
236 the communication channel. Its value is a configurable global
237 variable defining initial filters.
239 This list is meant to install user specified filters. Back-end
240 developers may install their own filters using
241 `org-export-define-backend'. Filters defined there will always
242 be prepended to the current list, so they always get applied
245 (defconst org-export-default-inline-image-rule
249 '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm"
250 "xpm" "pbm" "pgm" "ppm") t
))))
251 "Default rule for link matching an inline image.
252 This rule applies to links with no description. By default, it
253 will be considered as an inline image if it targets a local file
254 whose extension is either \"png\", \"jpeg\", \"jpg\", \"gif\",
255 \"tiff\", \"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\" or \"ppm\".
256 See `org-export-inline-image-p' for more information about
259 (defconst org-export-ignored-local-variables
260 '(org-font-lock-keywords
261 org-element--cache org-element--cache-objects org-element--cache-sync-keys
262 org-element--cache-sync-requests org-element--cache-sync-timer
)
263 "List of variables not copied through upon buffer duplication.
264 Export process takes place on a copy of the original buffer.
265 When this copy is created, all Org related local variables not in
266 this list are copied to the new buffer. Variables with an
267 unreadable value are also ignored.")
269 (defvar org-export-async-debug nil
270 "Non-nil means asynchronous export process should leave data behind.
272 This data is found in the appropriate \"*Org Export Process*\"
273 buffer, and in files prefixed with \"org-export-process\" and
274 located in `temporary-file-directory'.
276 When non-nil, it will also set `debug-on-error' to a non-nil
277 value in the external process.")
279 (defvar org-export-stack-contents nil
280 "Record asynchronously generated export results and processes.
281 This is an alist: its CAR is the source of the
282 result (destination file or buffer for a finished process,
283 original buffer for a running one) and its CDR is a list
284 containing the back-end used, as a symbol, and either a process
285 or the time at which it finished. It is used to build the menu
286 from `org-export-stack'.")
288 (defvar org-export--registered-backends nil
289 "List of backends currently available in the exporter.
290 This variable is set with `org-export-define-backend' and
291 `org-export-define-derived-backend' functions.")
293 (defvar org-export-dispatch-last-action nil
294 "Last command called from the dispatcher.
295 The value should be a list. Its CAR is the action, as a symbol,
296 and its CDR is a list of export options.")
298 (defvar org-export-dispatch-last-position
(make-marker)
299 "The position where the last export command was created using the dispatcher.
300 This marker will be used with `C-u C-c C-e' to make sure export repetition
301 uses the same subtree if the previous command was restricted to a subtree.")
303 ;; For compatibility with Org < 8
304 (defvar org-export-current-backend nil
305 "Name, if any, of the back-end used during an export process.
307 Its value is a symbol such as `html', `latex', `ascii', or nil if
308 the back-end is anonymous (see `org-export-create-backend') or if
309 there is no export process in progress.
311 It can be used to teach Babel blocks how to act differently
312 according to the back-end used.")
316 ;;; User-configurable Variables
318 ;; Configuration for the masses.
320 ;; They should never be accessed directly, as their value is to be
321 ;; stored in a property list (cf. `org-export-options-alist').
322 ;; Back-ends will read their value from there instead.
324 (defgroup org-export nil
325 "Options for exporting Org mode files."
329 (defgroup org-export-general nil
330 "General options for export engine."
331 :tag
"Org Export General"
334 (defcustom org-export-with-archived-trees
'headline
335 "Whether sub-trees with the ARCHIVE tag should be exported.
337 This can have three different values:
338 nil Do not export, pretend this tree is not present.
339 t Do export the entire tree.
340 `headline' Only export the headline, but skip the tree below it.
342 This option can also be set with the OPTIONS keyword,
344 :group
'org-export-general
346 (const :tag
"Not at all" nil
)
347 (const :tag
"Headline only" headline
)
348 (const :tag
"Entirely" t
)))
350 (defcustom org-export-with-author t
351 "Non-nil means insert author name into the exported file.
352 This option can also be set with the OPTIONS keyword,
353 e.g. \"author:nil\"."
354 :group
'org-export-general
357 (defcustom org-export-with-clocks nil
358 "Non-nil means export CLOCK keywords.
359 This option can also be set with the OPTIONS keyword,
361 :group
'org-export-general
364 (defcustom org-export-with-creator
'comment
365 "Non-nil means the postamble should contain a creator sentence.
367 The sentence can be set in `org-export-creator-string' and
368 defaults to \"Generated by Org mode XX in Emacs XXX.\".
370 If the value is `comment' insert it as a comment."
371 :group
'org-export-general
373 (const :tag
"No creator sentence" nil
)
374 (const :tag
"Sentence as a comment" comment
)
375 (const :tag
"Insert the sentence" t
)))
377 (defcustom org-export-with-date t
378 "Non-nil means insert date in the exported document.
379 This option can also be set with the OPTIONS keyword,
381 :group
'org-export-general
384 (defcustom org-export-date-timestamp-format nil
385 "Time-stamp format string to use for DATE keyword.
387 The format string, when specified, only applies if date consists
388 in a single time-stamp. Otherwise its value will be ignored.
390 See `format-time-string' for details on how to build this
392 :group
'org-export-general
394 (string :tag
"Time-stamp format string")
395 (const :tag
"No format string" nil
)))
397 (defcustom org-export-creator-string
398 (format "Emacs %s (Org mode %s)"
400 (if (fboundp 'org-version
) (org-version) "unknown version"))
401 "Information about the creator of the document.
402 This option can also be set on with the CREATOR keyword."
403 :group
'org-export-general
404 :type
'(string :tag
"Creator string"))
406 (defcustom org-export-with-drawers
'(not "LOGBOOK")
407 "Non-nil means export contents of standard drawers.
409 When t, all drawers are exported. This may also be a list of
410 drawer names to export, as strings. If that list starts with
411 `not', only drawers with such names will be ignored.
413 This variable doesn't apply to properties drawers. See
414 `org-export-with-properties' instead.
416 This option can also be set with the OPTIONS keyword,
418 :group
'org-export-general
420 :package-version
'(Org .
"8.0")
422 (const :tag
"All drawers" t
)
423 (const :tag
"None" nil
)
424 (repeat :tag
"Selected drawers"
425 (string :tag
"Drawer name"))
426 (list :tag
"Ignored drawers"
427 (const :format
"" not
)
428 (repeat :tag
"Specify names of drawers to ignore during export"
430 (string :tag
"Drawer name")))))
432 (defcustom org-export-with-email nil
433 "Non-nil means insert author email into the exported file.
434 This option can also be set with the OPTIONS keyword,
436 :group
'org-export-general
439 (defcustom org-export-with-emphasize t
440 "Non-nil means interpret *word*, /word/, _word_ and +word+.
442 If the export target supports emphasizing text, the word will be
443 typeset in bold, italic, with an underline or strike-through,
446 This option can also be set with the OPTIONS keyword,
448 :group
'org-export-general
451 (defcustom org-export-exclude-tags
'("noexport")
452 "Tags that exclude a tree from export.
454 All trees carrying any of these tags will be excluded from
455 export. This is without condition, so even subtrees inside that
456 carry one of the `org-export-select-tags' will be removed.
458 This option can also be set with the EXCLUDE_TAGS keyword."
459 :group
'org-export-general
460 :type
'(repeat (string :tag
"Tag")))
462 (defcustom org-export-with-fixed-width t
463 "Non-nil means export lines starting with \":\".
464 This option can also be set with the OPTIONS keyword,
466 :group
'org-export-general
468 :package-version
'(Org .
"8.0")
471 (defcustom org-export-with-footnotes t
472 "Non-nil means Org footnotes should be exported.
473 This option can also be set with the OPTIONS keyword,
475 :group
'org-export-general
478 (defcustom org-export-with-latex t
479 "Non-nil means process LaTeX environments and fragments.
481 This option can also be set with the OPTIONS line,
482 e.g. \"tex:verbatim\". Allowed values are:
484 nil Ignore math snippets.
485 `verbatim' Keep everything in verbatim.
486 t Allow export of math snippets."
487 :group
'org-export-general
489 :package-version
'(Org .
"8.0")
491 (const :tag
"Do not process math in any way" nil
)
492 (const :tag
"Interpret math snippets" t
)
493 (const :tag
"Leave math verbatim" verbatim
)))
495 (defcustom org-export-headline-levels
3
496 "The last level which is still exported as a headline.
498 Inferior levels will usually produce itemize or enumerate lists
499 when exported, but back-end behaviour may differ.
501 This option can also be set with the OPTIONS keyword,
503 :group
'org-export-general
506 (defcustom org-export-default-language
"en"
507 "The default language for export and clocktable translations, as a string.
508 This may have an association in
509 `org-clock-clocktable-language-setup',
510 `org-export-smart-quotes-alist' and `org-export-dictionary'.
511 This option can also be set with the LANGUAGE keyword."
512 :group
'org-export-general
513 :type
'(string :tag
"Language"))
515 (defcustom org-export-preserve-breaks nil
516 "Non-nil means preserve all line breaks when exporting.
517 This option can also be set with the OPTIONS keyword,
519 :group
'org-export-general
522 (defcustom org-export-with-entities t
523 "Non-nil means interpret entities when exporting.
525 For example, HTML export converts \\alpha to α and \\AA to
528 For a list of supported names, see the constant `org-entities'
529 and the user option `org-entities-user'.
531 This option can also be set with the OPTIONS keyword,
533 :group
'org-export-general
536 (defcustom org-export-with-inlinetasks t
537 "Non-nil means inlinetasks should be exported.
538 This option can also be set with the OPTIONS keyword,
539 e.g. \"inline:nil\"."
540 :group
'org-export-general
542 :package-version
'(Org .
"8.0")
545 (defcustom org-export-with-planning nil
546 "Non-nil means include planning info in export.
548 Planning info is the line containing either SCHEDULED:,
549 DEADLINE:, CLOSED: time-stamps, or a combination of them.
551 This option can also be set with the OPTIONS keyword,
553 :group
'org-export-general
555 :package-version
'(Org .
"8.0")
558 (defcustom org-export-with-priority nil
559 "Non-nil means include priority cookies in export.
560 This option can also be set with the OPTIONS keyword,
562 :group
'org-export-general
565 (defcustom org-export-with-properties nil
566 "Non-nil means export contents of properties drawers.
568 When t, all properties are exported. This may also be a list of
569 properties to export, as strings.
571 This option can also be set with the OPTIONS keyword,
573 :group
'org-export-general
575 :package-version
'(Org .
"8.3")
577 (const :tag
"All properties" t
)
578 (const :tag
"None" nil
)
579 (repeat :tag
"Selected properties"
580 (string :tag
"Property name"))))
582 (defcustom org-export-with-section-numbers t
583 "Non-nil means add section numbers to headlines when exporting.
585 When set to an integer n, numbering will only happen for
586 headlines whose relative level is higher or equal to n.
588 This option can also be set with the OPTIONS keyword,
590 :group
'org-export-general
593 (defcustom org-export-select-tags
'("export")
594 "Tags that select a tree for export.
596 If any such tag is found in a buffer, all trees that do not carry
597 one of these tags will be ignored during export. Inside trees
598 that are selected like this, you can still deselect a subtree by
599 tagging it with one of the `org-export-exclude-tags'.
601 This option can also be set with the SELECT_TAGS keyword."
602 :group
'org-export-general
603 :type
'(repeat (string :tag
"Tag")))
605 (defcustom org-export-with-smart-quotes nil
606 "Non-nil means activate smart quotes during export.
607 This option can also be set with the OPTIONS keyword,
610 When setting this to non-nil, you need to take care of
611 using the correct Babel package when exporting to LaTeX.
612 E.g., you can load Babel for french like this:
614 #+LATEX_HEADER: \\usepackage[french]{babel}"
615 :group
'org-export-general
617 :package-version
'(Org .
"8.0")
620 (defcustom org-export-with-special-strings t
621 "Non-nil means interpret \"\\-\", \"--\" and \"---\" for export.
623 When this option is turned on, these strings will be exported as:
626 -----+----------+--------+-------
630 ... … \\ldots …
632 This option can also be set with the OPTIONS keyword,
634 :group
'org-export-general
637 (defcustom org-export-with-statistics-cookies t
638 "Non-nil means include statistics cookies in export.
639 This option can also be set with the OPTIONS keyword,
641 :group
'org-export-general
643 :package-version
'(Org .
"8.0")
646 (defcustom org-export-with-sub-superscripts t
647 "Non-nil means interpret \"_\" and \"^\" for export.
649 If you want to control how Org displays those characters, see
650 `org-use-sub-superscripts'. `org-export-with-sub-superscripts'
651 used to be an alias for `org-use-sub-superscripts' in Org <8.0,
654 When this option is turned on, you can use TeX-like syntax for
655 sub- and superscripts and see them exported correctly.
657 You can also set the option with #+OPTIONS: ^:t
659 Several characters after \"_\" or \"^\" will be considered as a
660 single item - so grouping with {} is normally not needed. For
661 example, the following things will be parsed as single sub- or
664 10^24 or 10^tau several digits will be considered 1 item.
665 10^-12 or 10^-tau a leading sign with digits or a word
666 x^2-y^3 will be read as x^2 - y^3, because items are
667 terminated by almost any nonword/nondigit char.
668 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
670 Still, ambiguity is possible. So when in doubt, use {} to enclose
671 the sub/superscript. If you set this variable to the symbol `{}',
672 the braces are *required* in order to trigger interpretations as
673 sub/superscript. This can be helpful in documents that need \"_\"
674 frequently in plain text."
675 :group
'org-export-general
677 :package-version
'(Org .
"8.0")
679 (const :tag
"Interpret them" t
)
680 (const :tag
"Curly brackets only" {})
681 (const :tag
"Do not interpret them" nil
)))
683 (defcustom org-export-with-toc t
684 "Non-nil means create a table of contents in exported files.
686 The TOC contains headlines with levels up
687 to`org-export-headline-levels'. When an integer, include levels
688 up to N in the toc, this may then be different from
689 `org-export-headline-levels', but it will not be allowed to be
690 larger than the number of headline levels. When nil, no table of
693 This option can also be set with the OPTIONS keyword,
694 e.g. \"toc:nil\" or \"toc:3\"."
695 :group
'org-export-general
697 (const :tag
"No Table of Contents" nil
)
698 (const :tag
"Full Table of Contents" t
)
699 (integer :tag
"TOC to level")))
701 (defcustom org-export-with-tables t
702 "Non-nil means export tables.
703 This option can also be set with the OPTIONS keyword,
705 :group
'org-export-general
707 :package-version
'(Org .
"8.0")
710 (defcustom org-export-with-tags t
711 "If nil, do not export tags, just remove them from headlines.
713 If this is the symbol `not-in-toc', tags will be removed from
714 table of contents entries, but still be shown in the headlines of
717 This option can also be set with the OPTIONS keyword,
719 :group
'org-export-general
721 (const :tag
"Off" nil
)
722 (const :tag
"Not in TOC" not-in-toc
)
723 (const :tag
"On" t
)))
725 (defcustom org-export-with-tasks t
726 "Non-nil means include TODO items for export.
728 This may have the following values:
729 t include tasks independent of state.
730 `todo' include only tasks that are not yet done.
731 `done' include only tasks that are already done.
732 nil ignore all tasks.
733 list of keywords include tasks with these keywords.
735 This option can also be set with the OPTIONS keyword,
737 :group
'org-export-general
739 (const :tag
"All tasks" t
)
740 (const :tag
"No tasks" nil
)
741 (const :tag
"Not-done tasks" todo
)
742 (const :tag
"Only done tasks" done
)
743 (repeat :tag
"Specific TODO keywords"
744 (string :tag
"Keyword"))))
746 (defcustom org-export-time-stamp-file t
747 "Non-nil means insert a time stamp into the exported file.
748 The time stamp shows when the file was created. This option can
749 also be set with the OPTIONS keyword, e.g. \"timestamp:nil\"."
750 :group
'org-export-general
753 (defcustom org-export-with-timestamps t
754 "Non nil means allow timestamps in export.
756 It can be set to any of the following values:
757 t export all timestamps.
758 `active' export active timestamps only.
759 `inactive' export inactive timestamps only.
760 nil do not export timestamps
762 This only applies to timestamps isolated in a paragraph
763 containing only timestamps. Other timestamps are always
766 This option can also be set with the OPTIONS keyword, e.g.
768 :group
'org-export-general
770 (const :tag
"All timestamps" t
)
771 (const :tag
"Only active timestamps" active
)
772 (const :tag
"Only inactive timestamps" inactive
)
773 (const :tag
"No timestamp" nil
)))
775 (defcustom org-export-with-todo-keywords t
776 "Non-nil means include TODO keywords in export.
777 When nil, remove all these keywords from the export. This option
778 can also be set with the OPTIONS keyword, e.g. \"todo:nil\"."
779 :group
'org-export-general
782 (defcustom org-export-allow-bind-keywords nil
783 "Non-nil means BIND keywords can define local variable values.
784 This is a potential security risk, which is why the default value
785 is nil. You can also allow them through local buffer variables."
786 :group
'org-export-general
788 :package-version
'(Org .
"8.0")
791 (defcustom org-export-snippet-translation-alist nil
792 "Alist between export snippets back-ends and exporter back-ends.
794 This variable allows to provide shortcuts for export snippets.
796 For example, with a value of '\(\(\"h\" . \"html\"\)\), the
797 HTML back-end will recognize the contents of \"@@h:<b>@@\" as
798 HTML code while every other back-end will ignore it."
799 :group
'org-export-general
801 :package-version
'(Org .
"8.0")
803 (cons (string :tag
"Shortcut")
804 (string :tag
"Back-end"))))
806 (defcustom org-export-coding-system nil
807 "Coding system for the exported file."
808 :group
'org-export-general
810 :package-version
'(Org .
"8.0")
811 :type
'coding-system
)
813 (defcustom org-export-copy-to-kill-ring nil
814 "Non-nil means pushing export output to the kill ring.
815 This variable is ignored during asynchronous export."
816 :group
'org-export-general
818 :package-version
'(Org .
"8.3")
820 (const :tag
"Always" t
)
821 (const :tag
"When export is done interactively" if-interactive
)
822 (const :tag
"Never" nil
)))
824 (defcustom org-export-initial-scope
'buffer
825 "The initial scope when exporting with `org-export-dispatch'.
826 This variable can be either set to `buffer' or `subtree'."
827 :group
'org-export-general
829 (const :tag
"Export current buffer" buffer
)
830 (const :tag
"Export current subtree" subtree
)))
832 (defcustom org-export-show-temporary-export-buffer t
833 "Non-nil means show buffer after exporting to temp buffer.
834 When Org exports to a file, the buffer visiting that file is never
835 shown, but remains buried. However, when exporting to
836 a temporary buffer, that buffer is popped up in a second window.
837 When this variable is nil, the buffer remains buried also in
839 :group
'org-export-general
842 (defcustom org-export-in-background nil
843 "Non-nil means export and publishing commands will run in background.
844 Results from an asynchronous export are never displayed
845 automatically. But you can retrieve them with \\[org-export-stack]."
846 :group
'org-export-general
848 :package-version
'(Org .
"8.0")
851 (defcustom org-export-async-init-file nil
852 "File used to initialize external export process.
854 Value must be either nil or an absolute file name. When nil, the
855 external process is launched like a regular Emacs session,
856 loading user's initialization file and any site specific
857 configuration. If a file is provided, it, and only it, is loaded
860 Therefore, using a specific configuration makes the process to
861 load faster and the export more portable."
862 :group
'org-export-general
864 :package-version
'(Org .
"8.0")
866 (const :tag
"Regular startup" nil
)
867 (file :tag
"Specific start-up file" :must-match t
)))
869 (defcustom org-export-dispatch-use-expert-ui nil
870 "Non-nil means using a non-intrusive `org-export-dispatch'.
871 In that case, no help buffer is displayed. Though, an indicator
872 for current export scope is added to the prompt (\"b\" when
873 output is restricted to body only, \"s\" when it is restricted to
874 the current subtree, \"v\" when only visible elements are
875 considered for export, \"f\" when publishing functions should be
876 passed the FORCE argument and \"a\" when the export should be
877 asynchronous). Also, \[?] allows to switch back to standard
879 :group
'org-export-general
881 :package-version
'(Org .
"8.0")
886 ;;; Defining Back-ends
888 ;; An export back-end is a structure with `org-export-backend' type
889 ;; and `name', `parent', `transcoders', `options', `filters', `blocks'
892 ;; At the lowest level, a back-end is created with
893 ;; `org-export-create-backend' function.
895 ;; A named back-end can be registered with
896 ;; `org-export-register-backend' function. A registered back-end can
897 ;; later be referred to by its name, with `org-export-get-backend'
898 ;; function. Also, such a back-end can become the parent of a derived
899 ;; back-end from which slot values will be inherited by default.
900 ;; `org-export-derived-backend-p' can check if a given back-end is
901 ;; derived from a list of back-end names.
903 ;; `org-export-get-all-transcoders', `org-export-get-all-options' and
904 ;; `org-export-get-all-filters' return the full alist of transcoders,
905 ;; options and filters, including those inherited from ancestors.
907 ;; At a higher level, `org-export-define-backend' is the standard way
908 ;; to define an export back-end. If the new back-end is similar to
909 ;; a registered back-end, `org-export-define-derived-backend' may be
912 ;; Eventually `org-export-barf-if-invalid-backend' returns an error
913 ;; when a given back-end hasn't been registered yet.
915 (defstruct (org-export-backend (:constructor org-export-create-backend
)
917 name parent transcoders options filters blocks menu
)
919 (defun org-export-get-backend (name)
920 "Return export back-end named after NAME.
921 NAME is a symbol. Return nil if no such back-end is found."
923 (dolist (b org-export--registered-backends
)
924 (when (eq (org-export-backend-name b
) name
)
927 (defun org-export-register-backend (backend)
928 "Register BACKEND as a known export back-end.
929 BACKEND is a structure with `org-export-backend' type."
930 ;; Refuse to register an unnamed back-end.
931 (unless (org-export-backend-name backend
)
932 (error "Cannot register a unnamed export back-end"))
933 ;; Refuse to register a back-end with an unknown parent.
934 (let ((parent (org-export-backend-parent backend
)))
935 (when (and parent
(not (org-export-get-backend parent
)))
936 (error "Cannot use unknown \"%s\" back-end as a parent" parent
)))
937 ;; Register dedicated export blocks in the parser.
938 (dolist (name (org-export-backend-blocks backend
))
939 (add-to-list 'org-element-block-name-alist
940 (cons name
'org-element-export-block-parser
)))
941 ;; If a back-end with the same name as BACKEND is already
942 ;; registered, replace it with BACKEND. Otherwise, simply add
943 ;; BACKEND to the list of registered back-ends.
944 (let ((old (org-export-get-backend (org-export-backend-name backend
))))
945 (if old
(setcar (memq old org-export--registered-backends
) backend
)
946 (push backend org-export--registered-backends
))))
948 (defun org-export-barf-if-invalid-backend (backend)
949 "Signal an error if BACKEND isn't defined."
950 (unless (org-export-backend-p backend
)
951 (error "Unknown \"%s\" back-end: Aborting export" backend
)))
953 (defun org-export-derived-backend-p (backend &rest backends
)
954 "Non-nil if BACKEND is derived from one of BACKENDS.
955 BACKEND is an export back-end, as returned by, e.g.,
956 `org-export-create-backend', or a symbol referring to
957 a registered back-end. BACKENDS is constituted of symbols."
958 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
961 (while (org-export-backend-parent backend
)
962 (when (memq (org-export-backend-name backend
) backends
)
965 (org-export-get-backend (org-export-backend-parent backend
))))
966 (memq (org-export-backend-name backend
) backends
))))
968 (defun org-export-get-all-transcoders (backend)
969 "Return full translation table for BACKEND.
971 BACKEND is an export back-end, as return by, e.g,,
972 `org-export-create-backend'. Return value is an alist where
973 keys are element or object types, as symbols, and values are
976 Unlike to `org-export-backend-transcoders', this function
977 also returns transcoders inherited from parent back-ends,
979 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
981 (let ((transcoders (org-export-backend-transcoders backend
))
983 (while (setq parent
(org-export-backend-parent backend
))
984 (setq backend
(org-export-get-backend parent
))
986 (append transcoders
(org-export-backend-transcoders backend
))))
989 (defun org-export-get-all-options (backend)
990 "Return export options for BACKEND.
992 BACKEND is an export back-end, as return by, e.g,,
993 `org-export-create-backend'. See `org-export-options-alist'
994 for the shape of the return value.
996 Unlike to `org-export-backend-options', this function also
997 returns options inherited from parent back-ends, if any."
998 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
1000 (let ((options (org-export-backend-options backend
))
1002 (while (setq parent
(org-export-backend-parent backend
))
1003 (setq backend
(org-export-get-backend parent
))
1004 (setq options
(append options
(org-export-backend-options backend
))))
1007 (defun org-export-get-all-filters (backend)
1008 "Return complete list of filters for BACKEND.
1010 BACKEND is an export back-end, as return by, e.g,,
1011 `org-export-create-backend'. Return value is an alist where
1012 keys are symbols and values lists of functions.
1014 Unlike to `org-export-backend-filters', this function also
1015 returns filters inherited from parent back-ends, if any."
1016 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
1018 (let ((filters (org-export-backend-filters backend
))
1020 (while (setq parent
(org-export-backend-parent backend
))
1021 (setq backend
(org-export-get-backend parent
))
1022 (setq filters
(append filters
(org-export-backend-filters backend
))))
1025 (defun org-export-define-backend (backend transcoders
&rest body
)
1026 "Define a new back-end BACKEND.
1028 TRANSCODERS is an alist between object or element types and
1029 functions handling them.
1031 These functions should return a string without any trailing
1032 space, or nil. They must accept three arguments: the object or
1033 element itself, its contents or nil when it isn't recursive and
1034 the property list used as a communication channel.
1036 Contents, when not nil, are stripped from any global indentation
1037 \(although the relative one is preserved). They also always end
1038 with a single newline character.
1040 If, for a given type, no function is found, that element or
1041 object type will simply be ignored, along with any blank line or
1042 white space at its end. The same will happen if the function
1043 returns the nil value. If that function returns the empty
1044 string, the type will be ignored, but the blank lines or white
1045 spaces will be kept.
1047 In addition to element and object types, one function can be
1048 associated to the `template' (or `inner-template') symbol and
1049 another one to the `plain-text' symbol.
1051 The former returns the final transcoded string, and can be used
1052 to add a preamble and a postamble to document's body. It must
1053 accept two arguments: the transcoded string and the property list
1054 containing export options. A function associated to `template'
1055 will not be applied if export has option \"body-only\".
1056 A function associated to `inner-template' is always applied.
1058 The latter, when defined, is to be called on every text not
1059 recognized as an element or an object. It must accept two
1060 arguments: the text string and the information channel. It is an
1061 appropriate place to protect special chars relative to the
1064 BODY can start with pre-defined keyword arguments. The following
1065 keywords are understood:
1069 String, or list of strings, representing block names that
1070 will not be parsed. This is used to specify blocks that will
1071 contain raw code specific to the back-end. These blocks
1072 still have to be handled by the relative `export-block' type
1077 Alist between filters and function, or list of functions,
1078 specific to the back-end. See `org-export-filters-alist' for
1079 a list of all allowed filters. Filters defined here
1080 shouldn't make a back-end test, as it may prevent back-ends
1081 derived from this one to behave properly.
1085 Menu entry for the export dispatcher. It should be a list
1088 '(KEY DESCRIPTION-OR-ORDINAL ACTION-OR-MENU)
1092 KEY is a free character selecting the back-end.
1094 DESCRIPTION-OR-ORDINAL is either a string or a number.
1096 If it is a string, is will be used to name the back-end in
1097 its menu entry. If it is a number, the following menu will
1098 be displayed as a sub-menu of the back-end with the same
1099 KEY. Also, the number will be used to determine in which
1100 order such sub-menus will appear (lowest first).
1102 ACTION-OR-MENU is either a function or an alist.
1104 If it is an action, it will be called with four
1105 arguments (booleans): ASYNC, SUBTREEP, VISIBLE-ONLY and
1106 BODY-ONLY. See `org-export-as' for further explanations on
1109 If it is an alist, associations should follow the
1112 '(KEY DESCRIPTION ACTION)
1114 where KEY, DESCRIPTION and ACTION are described above.
1116 Valid values include:
1118 '(?m \"My Special Back-end\" my-special-export-function)
1122 '(?l \"Export to LaTeX\"
1123 \(?p \"As PDF file\" org-latex-export-to-pdf)
1124 \(?o \"As PDF file and open\"
1126 \(if a (org-latex-export-to-pdf t s v b)
1128 \(org-latex-export-to-pdf nil s v b)))))))
1130 or the following, which will be added to the previous
1134 \((?B \"As TEX buffer (Beamer)\" org-beamer-export-as-latex)
1135 \(?P \"As PDF file (Beamer)\" org-beamer-export-to-pdf)))
1139 Alist between back-end specific properties introduced in
1140 communication channel and how their value are acquired. See
1141 `org-export-options-alist' for more information about
1142 structure of the values."
1143 (declare (indent 1))
1144 (let (blocks filters menu-entry options contents
)
1145 (while (keywordp (car body
))
1146 (let ((keyword (pop body
)))
1148 (:export-block
(let ((names (pop body
)))
1149 (setq blocks
(if (consp names
) (mapcar 'upcase names
)
1150 (list (upcase names
))))))
1151 (:filters-alist
(setq filters
(pop body
)))
1152 (:menu-entry
(setq menu-entry
(pop body
)))
1153 (:options-alist
(setq options
(pop body
)))
1154 (t (error "Unknown keyword: %s" keyword
)))))
1155 (org-export-register-backend
1156 (org-export-create-backend :name backend
1157 :transcoders transcoders
1161 :menu menu-entry
))))
1163 (defun org-export-define-derived-backend (child parent
&rest body
)
1164 "Create a new back-end as a variant of an existing one.
1166 CHILD is the name of the derived back-end. PARENT is the name of
1167 the parent back-end.
1169 BODY can start with pre-defined keyword arguments. The following
1170 keywords are understood:
1174 String, or list of strings, representing block names that
1175 will not be parsed. This is used to specify blocks that will
1176 contain raw code specific to the back-end. These blocks
1177 still have to be handled by the relative `export-block' type
1182 Alist of filters that will overwrite or complete filters
1183 defined in PARENT back-end. See `org-export-filters-alist'
1184 for a list of allowed filters.
1188 Menu entry for the export dispatcher. See
1189 `org-export-define-backend' for more information about the
1194 Alist of back-end specific properties that will overwrite or
1195 complete those defined in PARENT back-end. Refer to
1196 `org-export-options-alist' for more information about
1197 structure of the values.
1201 Alist of element and object types and transcoders that will
1202 overwrite or complete transcode table from PARENT back-end.
1203 Refer to `org-export-define-backend' for detailed information
1206 As an example, here is how one could define \"my-latex\" back-end
1207 as a variant of `latex' back-end with a custom template function:
1209 \(org-export-define-derived-backend 'my-latex 'latex
1210 :translate-alist '((template . my-latex-template-fun)))
1212 The back-end could then be called with, for example:
1214 \(org-export-to-buffer 'my-latex \"*Test my-latex*\")"
1215 (declare (indent 2))
1216 (let (blocks filters menu-entry options transcoders contents
)
1217 (while (keywordp (car body
))
1218 (let ((keyword (pop body
)))
1220 (:export-block
(let ((names (pop body
)))
1221 (setq blocks
(if (consp names
) (mapcar 'upcase names
)
1222 (list (upcase names
))))))
1223 (:filters-alist
(setq filters
(pop body
)))
1224 (:menu-entry
(setq menu-entry
(pop body
)))
1225 (:options-alist
(setq options
(pop body
)))
1226 (:translate-alist
(setq transcoders
(pop body
)))
1227 (t (error "Unknown keyword: %s" keyword
)))))
1228 (org-export-register-backend
1229 (org-export-create-backend :name child
1231 :transcoders transcoders
1235 :menu menu-entry
))))
1239 ;;; The Communication Channel
1241 ;; During export process, every function has access to a number of
1242 ;; properties. They are of two types:
1244 ;; 1. Environment options are collected once at the very beginning of
1245 ;; the process, out of the original buffer and configuration.
1246 ;; Collecting them is handled by `org-export-get-environment'
1249 ;; Most environment options are defined through the
1250 ;; `org-export-options-alist' variable.
1252 ;; 2. Tree properties are extracted directly from the parsed tree,
1253 ;; just before export, by `org-export-collect-tree-properties'.
1255 ;;;; Environment Options
1257 ;; Environment options encompass all parameters defined outside the
1258 ;; scope of the parsed data. They come from five sources, in
1259 ;; increasing precedence order:
1261 ;; - Global variables,
1262 ;; - Buffer's attributes,
1263 ;; - Options keyword symbols,
1264 ;; - Buffer keywords,
1265 ;; - Subtree properties.
1267 ;; The central internal function with regards to environment options
1268 ;; is `org-export-get-environment'. It updates global variables with
1269 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
1270 ;; the different sources.
1272 ;; The internal functions doing the retrieval are:
1273 ;; `org-export--get-global-options',
1274 ;; `org-export--get-buffer-attributes',
1275 ;; `org-export--parse-option-keyword',
1276 ;; `org-export--get-subtree-options' and
1277 ;; `org-export--get-inbuffer-options'
1279 ;; Also, `org-export--list-bound-variables' collects bound variables
1280 ;; along with their value in order to set them as buffer local
1281 ;; variables later in the process.
1283 (defun org-export-get-environment (&optional backend subtreep ext-plist
)
1284 "Collect export options from the current buffer.
1286 Optional argument BACKEND is an export back-end, as returned by
1287 `org-export-create-backend'.
1289 When optional argument SUBTREEP is non-nil, assume the export is
1290 done against the current sub-tree.
1292 Third optional argument EXT-PLIST is a property list with
1293 external parameters overriding Org default settings, but still
1294 inferior to file-local settings."
1295 ;; First install #+BIND variables since these must be set before
1296 ;; global options are read.
1297 (dolist (pair (org-export--list-bound-variables))
1298 (org-set-local (car pair
) (nth 1 pair
)))
1299 ;; Get and prioritize export options...
1301 ;; ... from global variables...
1302 (org-export--get-global-options backend
)
1303 ;; ... from an external property list...
1305 ;; ... from in-buffer settings...
1306 (org-export--get-inbuffer-options backend
)
1307 ;; ... and from subtree, when appropriate.
1308 (and subtreep
(org-export--get-subtree-options backend
))
1309 ;; Eventually add misc. properties.
1313 :translate-alist
(org-export-get-all-transcoders backend
)
1314 :footnote-definition-alist
1315 ;; Footnotes definitions must be collected in the original
1316 ;; buffer, as there's no insurance that they will still be in
1317 ;; the parse tree, due to possible narrowing.
1319 (org-with-wide-buffer
1320 (goto-char (point-min))
1321 (while (re-search-forward org-footnote-re nil t
)
1323 (let ((fn (save-match-data (org-element-context))))
1324 (case (org-element-type fn
)
1325 (footnote-definition
1327 (cons (org-element-property :label fn
)
1328 (let ((cbeg (org-element-property :contents-begin fn
)))
1330 (org-element--parse-elements
1331 cbeg
(org-element-property :contents-end fn
)
1332 nil nil nil nil
(list 'org-data nil
)))))
1335 (let ((label (org-element-property :label fn
))
1336 (cbeg (org-element-property :contents-begin fn
)))
1337 (when (and label cbeg
1338 (eq (org-element-property :type fn
) 'inline
))
1341 (org-element-parse-secondary-string
1343 cbeg
(org-element-property :contents-end fn
))
1344 (org-element-restriction 'footnote-reference
)))
1348 ;; Collect id references.
1350 (org-with-wide-buffer
1351 (goto-char (point-min))
1352 (while (re-search-forward "\\[\\[id:\\S-+?\\]" nil t
)
1353 (let ((link (org-element-context)))
1354 (when (eq (org-element-type link
) 'link
)
1355 (let* ((id (org-element-property :path link
))
1356 (file (org-id-find-id-file id
)))
1358 (push (cons id
(file-relative-name file
)) alist
)))))))
1361 (defun org-export--parse-option-keyword (options &optional backend
)
1362 "Parse an OPTIONS line and return values as a plist.
1363 Optional argument BACKEND is an export back-end, as returned by,
1364 e.g., `org-export-create-backend'. It specifies which back-end
1365 specific items to read, if any."
1367 ;; Priority is given to back-end specific options.
1368 (append (and backend
(org-export-get-all-options backend
))
1369 org-export-options-alist
))
1371 (dolist (option all
)
1372 (let ((property (car option
))
1373 (item (nth 2 option
)))
1375 (not (plist-member plist property
))
1376 (string-match (concat "\\(\\`\\|[ \t]\\)"
1378 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
1380 (setq plist
(plist-put plist
1382 (car (read-from-string
1383 (match-string 2 options
))))))))
1386 (defun org-export--get-subtree-options (&optional backend
)
1387 "Get export options in subtree at point.
1388 Optional argument BACKEND is an export back-end, as returned by,
1389 e.g., `org-export-create-backend'. It specifies back-end used
1390 for export. Return options as a plist."
1391 ;; For each buffer keyword, create a headline property setting the
1392 ;; same property in communication channel. The name for the property
1393 ;; is the keyword with "EXPORT_" appended to it.
1394 (org-with-wide-buffer
1396 ;; Make sure point is at a heading.
1397 (if (org-at-heading-p) (org-up-heading-safe) (org-back-to-heading t
))
1398 ;; Take care of EXPORT_TITLE. If it isn't defined, use headline's
1399 ;; title (with no todo keyword, priority cookie or tag) as its
1401 (when (setq prop
(or (org-entry-get (point) "EXPORT_TITLE")
1402 (progn (looking-at org-complex-heading-regexp
)
1403 (org-match-string-no-properties 4))))
1407 (org-element-parse-secondary-string
1408 prop
(org-element-restriction 'keyword
)))))
1409 ;; EXPORT_OPTIONS are parsed in a non-standard way.
1410 (when (setq prop
(org-entry-get (point) "EXPORT_OPTIONS"))
1412 (nconc plist
(org-export--parse-option-keyword prop backend
))))
1413 ;; Handle other keywords. TITLE keyword is excluded as it has
1414 ;; been handled already.
1415 (let ((seen '("TITLE")))
1418 (let ((property (car option
))
1419 (keyword (nth 1 option
)))
1420 (when (and keyword
(not (member keyword seen
)))
1421 (let* ((subtree-prop (concat "EXPORT_" keyword
))
1422 ;; Export properties are not case-sensitive.
1423 (value (let ((case-fold-search t
))
1424 (org-entry-get (point) subtree-prop
))))
1426 (when (and value
(not (plist-member plist property
)))
1432 ;; Parse VALUE if required.
1433 ((member keyword org-element-document-properties
)
1434 (org-element-parse-secondary-string
1435 value
(org-element-restriction 'keyword
)))
1436 ;; If BEHAVIOR is `split' expected value is
1437 ;; a list of strings, not a string.
1438 ((eq (nth 4 option
) 'split
) (org-split-string value
))
1440 ;; Look for both general keywords and back-end specific
1441 ;; options, with priority given to the latter.
1442 (append (and backend
(org-export-get-all-options backend
))
1443 org-export-options-alist
)))
1447 (defun org-export--get-inbuffer-options (&optional backend
)
1448 "Return current buffer export options, as a plist.
1450 Optional argument BACKEND, when non-nil, is an export back-end,
1451 as returned by, e.g., `org-export-create-backend'. It specifies
1452 which back-end specific options should also be read in the
1455 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1457 get-options
; For byte-compiler.
1458 (case-fold-search t
)
1460 ;; Priority is given to back-end specific options.
1461 (and backend
(org-export-get-all-options backend
))
1462 org-export-options-alist
))
1463 (regexp (format "^[ \t]*#\\+%s:"
1464 (regexp-opt (nconc (delq nil
(mapcar 'cadr options
))
1465 org-export-special-keywords
))))
1468 ;; Return all properties associated to KEYWORD.
1470 (dolist (option options properties
)
1471 (when (equal (nth 1 option
) keyword
)
1472 (pushnew (car option
) properties
))))))
1474 (lambda (&optional files plist
)
1475 ;; Recursively read keywords in buffer. FILES is a list
1476 ;; of files read so far. PLIST is the current property
1478 (org-with-wide-buffer
1479 (goto-char (point-min))
1480 (while (re-search-forward regexp nil t
)
1481 (let ((element (org-element-at-point)))
1482 (when (eq (org-element-type element
) 'keyword
)
1483 (let ((key (org-element-property :key element
))
1484 (val (org-element-property :value element
)))
1486 ;; Options in `org-export-special-keywords'.
1487 ((equal key
"SETUPFILE")
1488 (let ((file (expand-file-name
1489 (org-remove-double-quotes (org-trim val
)))))
1490 ;; Avoid circular dependencies.
1491 (unless (member file files
)
1493 (insert (org-file-contents file
'noerror
))
1494 (let ((org-inhibit-startup t
)) (org-mode))
1495 (setq plist
(funcall get-options
1496 (cons file files
) plist
))))))
1497 ((equal key
"OPTIONS")
1501 (org-export--parse-option-keyword val backend
))))
1502 ((equal key
"FILETAGS")
1508 (append (org-split-string val
":")
1509 (plist-get plist
:filetags
)))))))
1511 ;; Options in `org-export-options-alist'.
1512 (dolist (property (funcall find-properties key
))
1513 (let ((behaviour (nth 4 (assq property options
))))
1517 ;; Handle value depending on specified
1521 (if (not (plist-get plist property
))
1523 (concat (plist-get plist property
)
1528 (concat (plist-get plist property
)
1531 (split `(,@(plist-get plist property
)
1532 ,@(org-split-string val
)))
1535 (if (not (plist-member plist property
)) val
1536 (plist-get plist property
))))))))))))))
1537 ;; Return final value.
1539 ;; Read options in the current buffer.
1540 (setq plist
(funcall get-options
1541 (and buffer-file-name
(list buffer-file-name
)) nil
))
1542 ;; Parse keywords specified in `org-element-document-properties'
1543 ;; and return PLIST.
1544 (dolist (keyword org-element-document-properties plist
)
1545 (dolist (property (funcall find-properties keyword
))
1546 (let ((value (plist-get plist property
)))
1547 (when (stringp value
)
1549 (plist-put plist property
1550 (org-element-parse-secondary-string
1551 value
(org-element-restriction 'keyword
))))))))))
1553 (defun org-export--get-buffer-attributes ()
1554 "Return properties related to buffer attributes, as a plist."
1555 (list :input-buffer
(buffer-name (buffer-base-buffer))
1556 :input-file
(buffer-file-name (buffer-base-buffer))))
1558 (defun org-export--get-global-options (&optional backend
)
1559 "Return global export options as a plist.
1560 Optional argument BACKEND, if non-nil, is an export back-end, as
1561 returned by, e.g., `org-export-create-backend'. It specifies
1562 which back-end specific export options should also be read in the
1565 ;; Priority is given to back-end specific options.
1566 (all (append (and backend
(org-export-get-all-options backend
))
1567 org-export-options-alist
)))
1568 (dolist (cell all plist
)
1569 (let ((prop (car cell
)))
1570 (unless (plist-member plist prop
)
1575 ;; Evaluate default value provided. If keyword is
1576 ;; a member of `org-element-document-properties',
1577 ;; parse it as a secondary string before storing it.
1578 (let ((value (eval (nth 3 cell
))))
1579 (if (and (stringp value
)
1580 (member (nth 1 cell
)
1581 org-element-document-properties
))
1582 (org-element-parse-secondary-string
1583 value
(org-element-restriction 'keyword
))
1586 (defun org-export--list-bound-variables ()
1587 "Return variables bound from BIND keywords in current buffer.
1588 Also look for BIND keywords in setup files. The return value is
1589 an alist where associations are (VARIABLE-NAME VALUE)."
1590 (when org-export-allow-bind-keywords
1591 (let* (collect-bind ; For byte-compiler.
1593 (lambda (files alist
)
1594 ;; Return an alist between variable names and their
1595 ;; value. FILES is a list of setup files names read so
1596 ;; far, used to avoid circular dependencies. ALIST is
1597 ;; the alist collected so far.
1598 (let ((case-fold-search t
))
1599 (org-with-wide-buffer
1600 (goto-char (point-min))
1601 (while (re-search-forward
1602 "^[ \t]*#\\+\\(BIND\\|SETUPFILE\\):" nil t
)
1603 (let ((element (org-element-at-point)))
1604 (when (eq (org-element-type element
) 'keyword
)
1605 (let ((val (org-element-property :value element
)))
1606 (if (equal (org-element-property :key element
) "BIND")
1607 (push (read (format "(%s)" val
)) alist
)
1608 ;; Enter setup file.
1609 (let ((file (expand-file-name
1610 (org-remove-double-quotes val
))))
1611 (unless (member file files
)
1613 (let ((org-inhibit-startup t
)) (org-mode))
1614 (insert (org-file-contents file
'noerror
))
1616 (funcall collect-bind
1620 ;; Return value in appropriate order of appearance.
1621 (nreverse (funcall collect-bind nil nil
)))))
1623 ;; defsubst org-export-get-parent must be defined before first use,
1624 ;; was originally defined in the topology section
1626 (defsubst org-export-get-parent
(blob)
1627 "Return BLOB parent or nil.
1628 BLOB is the element or object considered."
1629 (org-element-property :parent blob
))
1631 ;;;; Tree Properties
1633 ;; Tree properties are information extracted from parse tree. They
1634 ;; are initialized at the beginning of the transcoding process by
1635 ;; `org-export-collect-tree-properties'.
1637 ;; Dedicated functions focus on computing the value of specific tree
1638 ;; properties during initialization. Thus,
1639 ;; `org-export--populate-ignore-list' lists elements and objects that
1640 ;; should be skipped during export, `org-export--get-min-level' gets
1641 ;; the minimal exportable level, used as a basis to compute relative
1642 ;; level for headlines. Eventually
1643 ;; `org-export--collect-headline-numbering' builds an alist between
1644 ;; headlines and their numbering.
1646 (defun org-export-collect-tree-properties (data info
)
1647 "Extract tree properties from parse tree.
1649 DATA is the parse tree from which information is retrieved. INFO
1650 is a list holding export options.
1652 Following tree properties are set or updated:
1654 `:exported-data' Hash table used to memoize results from
1657 `:footnote-definition-alist' List of footnotes definitions in
1658 original buffer and current parse tree.
1660 `:headline-offset' Offset between true level of headlines and
1661 local level. An offset of -1 means a headline
1662 of level 2 should be considered as a level
1663 1 headline in the context.
1665 `:headline-numbering' Alist of all headlines as key an the
1666 associated numbering as value.
1668 `:ignore-list' List of elements that should be ignored during
1671 Return updated plist."
1672 ;; Install the parse tree in the communication channel.
1673 (setq info
(plist-put info
:parse-tree data
))
1674 ;; Get the list of elements and objects to ignore, and put it into
1675 ;; `:ignore-list'. Do not overwrite any user ignore that might have
1676 ;; been done during parse tree filtering.
1680 (append (org-export--populate-ignore-list data info
)
1681 (plist-get info
:ignore-list
))))
1682 ;; Compute `:headline-offset' in order to be able to use
1683 ;; `org-export-get-relative-level'.
1687 (- 1 (org-export--get-min-level data info
))))
1688 ;; Footnote definitions in parse tree override those stored in
1689 ;; `:footnote-definition-alist'. This way, any change to
1690 ;; a definition in the parse tree (e.g., through a parse tree
1691 ;; filter) propagates into the alist.
1692 (let ((defs (plist-get info
:footnote-definition-alist
)))
1693 (org-element-map data
'(footnote-definition footnote-reference
)
1695 (cond ((eq (org-element-type fn
) 'footnote-definition
)
1696 (push (cons (org-element-property :label fn
)
1697 (append '(org-data nil
) (org-element-contents fn
)))
1699 ((eq (org-element-property :type fn
) 'inline
)
1700 (push (cons (org-element-property :label fn
)
1701 (org-element-contents fn
))
1703 (setq info
(plist-put info
:footnote-definition-alist defs
)))
1704 ;; Properties order doesn't matter: get the rest of the tree
1707 `(:headline-numbering
,(org-export--collect-headline-numbering data info
)
1708 :unnumbered-headline-id
,(org-export--collect-unnumbered-headline-id data info
)
1709 :exported-data
,(make-hash-table :test
'eq
:size
4001))
1712 (defun org-export--get-min-level (data options
)
1713 "Return minimum exportable headline's level in DATA.
1714 DATA is parsed tree as returned by `org-element-parse-buffer'.
1715 OPTIONS is a plist holding export options."
1717 (let ((min-level 10000))
1720 (when (and (eq (org-element-type blob
) 'headline
)
1721 (not (org-element-property :footnote-section-p blob
))
1722 (not (memq blob
(plist-get options
:ignore-list
))))
1723 (setq min-level
(min (org-element-property :level blob
) min-level
)))
1724 (when (= min-level
1) (throw 'exit
1)))
1725 (org-element-contents data
))
1726 ;; If no headline was found, for the sake of consistency, set
1727 ;; minimum level to 1 nonetheless.
1728 (if (= min-level
10000) 1 min-level
))))
1730 (defun org-export--collect-headline-numbering (data options
)
1731 "Return numbering of all exportable, numbered headlines in a parse tree.
1733 DATA is the parse tree. OPTIONS is the plist holding export
1736 Return an alist whose key is a headline and value is its
1737 associated numbering \(in the shape of a list of numbers\) or nil
1738 for a footnotes section."
1739 (let ((numbering (make-vector org-export-max-depth
0)))
1740 (org-element-map data
'headline
1742 (when (and (org-export-numbered-headline-p headline options
)
1743 (not (org-element-property :footnote-section-p headline
)))
1744 (let ((relative-level
1745 (1- (org-export-get-relative-level headline options
))))
1748 (loop for n across numbering
1749 for idx from
0 to org-export-max-depth
1750 when
(< idx relative-level
) collect n
1751 when
(= idx relative-level
) collect
(aset numbering idx
(1+ n
))
1752 when
(> idx relative-level
) do
(aset numbering idx
0))))))
1755 (defun org-export--collect-unnumbered-headline-id (data options
)
1756 "Return numbering of all exportable, unnumbered headlines.
1757 DATA is the parse tree. OPTIONS is the plist holding export
1758 options. Unnumbered headlines are numbered as a function of
1761 (org-element-map data
'headline
1763 (unless (org-export-numbered-headline-p headline options
)
1764 (list headline
(incf num
)))))))
1766 (defun org-export--populate-ignore-list (data options
)
1767 "Return list of elements and objects to ignore during export.
1768 DATA is the parse tree to traverse. OPTIONS is the plist holding
1771 ;; First find trees containing a select tag, if any.
1772 (selected (org-export--selected-trees data options
))
1773 ;; If a select tag is active, also ignore the section before
1774 ;; the first headline, if any.
1775 (ignore (and selected
1776 (let ((first-element (car (org-element-contents data
))))
1777 (and (eq (org-element-type first-element
) 'section
)
1781 ;; Collect ignored elements or objects into IGNORE-LIST.
1782 (let ((type (org-element-type data
)))
1783 (if (org-export--skip-p data options selected
) (push data ignore
)
1784 (if (and (eq type
'headline
)
1785 (eq (plist-get options
:with-archived-trees
) 'headline
)
1786 (org-element-property :archivedp data
))
1787 ;; If headline is archived but tree below has
1788 ;; to be skipped, add it to ignore list.
1789 (dolist (element (org-element-contents data
))
1790 (push element ignore
))
1791 ;; Move into secondary string, if any.
1793 (cdr (assq type org-element-secondary-value-alist
))))
1795 (mapc walk-data
(org-element-property sec-prop data
))))
1796 ;; Move into recursive objects/elements.
1797 (mapc walk-data
(org-element-contents data
))))))))
1799 (funcall walk-data data
)
1803 (defun org-export--selected-trees (data info
)
1804 "Return list of headlines and inlinetasks with a select tag in their tree.
1805 DATA is parsed data as returned by `org-element-parse-buffer'.
1806 INFO is a plist holding export options."
1807 (let* (selected-trees
1808 walk-data
; For byte-compiler.
1811 (lambda (data genealogy
)
1812 (let ((type (org-element-type data
)))
1814 ((memq type
'(headline inlinetask
))
1815 (let ((tags (org-element-property :tags data
)))
1816 (if (loop for tag in
(plist-get info
:select-tags
)
1817 thereis
(member tag tags
))
1818 ;; When a select tag is found, mark full
1819 ;; genealogy and every headline within the tree
1821 (setq selected-trees
1824 (org-element-map data
'(headline inlinetask
)
1827 ;; If at a headline, continue searching in tree,
1829 (when (eq type
'headline
)
1831 (funcall walk-data el
(cons data genealogy
)))
1832 (org-element-contents data
))))))
1833 ((or (eq type
'org-data
)
1834 (memq type org-element-greater-elements
))
1835 (mapc (lambda (el) (funcall walk-data el genealogy
))
1836 (org-element-contents data
)))))))))
1837 (funcall walk-data data nil
)
1840 (defun org-export--skip-p (blob options selected
)
1841 "Non-nil when element or object BLOB should be skipped during export.
1842 OPTIONS is the plist holding export options. SELECTED, when
1843 non-nil, is a list of headlines or inlinetasks belonging to
1844 a tree with a select tag."
1845 (case (org-element-type blob
)
1846 (clock (not (plist-get options
:with-clocks
)))
1848 (let ((with-drawers-p (plist-get options
:with-drawers
)))
1849 (or (not with-drawers-p
)
1850 (and (consp with-drawers-p
)
1851 ;; If `:with-drawers' value starts with `not', ignore
1852 ;; every drawer whose name belong to that list.
1853 ;; Otherwise, ignore drawers whose name isn't in that
1855 (let ((name (org-element-property :drawer-name blob
)))
1856 (if (eq (car with-drawers-p
) 'not
)
1857 (member-ignore-case name
(cdr with-drawers-p
))
1858 (not (member-ignore-case name with-drawers-p
))))))))
1859 (fixed-width (not (plist-get options
:with-fixed-width
)))
1860 ((footnote-definition footnote-reference
)
1861 (not (plist-get options
:with-footnotes
)))
1862 ((headline inlinetask
)
1863 (let ((with-tasks (plist-get options
:with-tasks
))
1864 (todo (org-element-property :todo-keyword blob
))
1865 (todo-type (org-element-property :todo-type blob
))
1866 (archived (plist-get options
:with-archived-trees
))
1867 (tags (org-element-property :tags blob
)))
1869 (and (eq (org-element-type blob
) 'inlinetask
)
1870 (not (plist-get options
:with-inlinetasks
)))
1871 ;; Ignore subtrees with an exclude tag.
1872 (loop for k in
(plist-get options
:exclude-tags
)
1873 thereis
(member k tags
))
1874 ;; When a select tag is present in the buffer, ignore any tree
1876 (and selected
(not (memq blob selected
)))
1877 ;; Ignore commented sub-trees.
1878 (org-element-property :commentedp blob
)
1879 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
1880 (and (not archived
) (org-element-property :archivedp blob
))
1881 ;; Ignore tasks, if specified by `:with-tasks' property.
1883 (or (not with-tasks
)
1884 (and (memq with-tasks
'(todo done
))
1885 (not (eq todo-type with-tasks
)))
1886 (and (consp with-tasks
) (not (member todo with-tasks
))))))))
1887 ((latex-environment latex-fragment
) (not (plist-get options
:with-latex
)))
1889 (let ((properties-set (plist-get options
:with-properties
)))
1890 (cond ((null properties-set
) t
)
1891 ((consp properties-set
)
1892 (not (member-ignore-case (org-element-property :key blob
)
1893 properties-set
))))))
1894 (planning (not (plist-get options
:with-planning
)))
1895 (property-drawer (not (plist-get options
:with-properties
)))
1896 (statistics-cookie (not (plist-get options
:with-statistics-cookies
)))
1897 (table (not (plist-get options
:with-tables
)))
1899 (and (org-export-table-has-special-column-p
1900 (org-export-get-parent-table blob
))
1901 (not (org-export-get-previous-element blob options
))))
1902 (table-row (org-export-table-row-is-special-p blob options
))
1904 ;; `:with-timestamps' only applies to isolated timestamps
1905 ;; objects, i.e. timestamp objects in a paragraph containing only
1906 ;; timestamps and whitespaces.
1907 (when (let ((parent (org-export-get-parent-element blob
)))
1908 (and (memq (org-element-type parent
) '(paragraph verse-block
))
1909 (not (org-element-map parent
1911 (remq 'timestamp org-element-all-objects
))
1913 (or (not (stringp obj
)) (org-string-nw-p obj
)))
1915 (case (plist-get options
:with-timestamps
)
1918 (not (memq (org-element-property :type blob
) '(active active-range
))))
1920 (not (memq (org-element-property :type blob
)
1921 '(inactive inactive-range
)))))))))
1926 ;; `org-export-data' reads a parse tree (obtained with, i.e.
1927 ;; `org-element-parse-buffer') and transcodes it into a specified
1928 ;; back-end output. It takes care of filtering out elements or
1929 ;; objects according to export options and organizing the output blank
1930 ;; lines and white space are preserved. The function memoizes its
1931 ;; results, so it is cheap to call it within transcoders.
1933 ;; It is possible to modify locally the back-end used by
1934 ;; `org-export-data' or even use a temporary back-end by using
1935 ;; `org-export-data-with-backend'.
1937 ;; Internally, three functions handle the filtering of objects and
1938 ;; elements during the export. In particular,
1939 ;; `org-export-ignore-element' marks an element or object so future
1940 ;; parse tree traversals skip it and `org-export-expand' transforms
1941 ;; the others back into their original shape.
1943 ;; `org-export-transcoder' is an accessor returning appropriate
1944 ;; translator function for a given element or object.
1946 (defun org-export-transcoder (blob info
)
1947 "Return appropriate transcoder for BLOB.
1948 INFO is a plist containing export directives."
1949 (let ((type (org-element-type blob
)))
1950 ;; Return contents only for complete parse trees.
1951 (if (eq type
'org-data
) (lambda (blob contents info
) contents
)
1952 (let ((transcoder (cdr (assq type
(plist-get info
:translate-alist
)))))
1953 (and (functionp transcoder
) transcoder
)))))
1955 (defun org-export-data (data info
)
1956 "Convert DATA into current back-end format.
1958 DATA is a parse tree, an element or an object or a secondary
1959 string. INFO is a plist holding export options.
1962 (or (gethash data
(plist-get info
:exported-data
))
1963 (let* ((type (org-element-type data
))
1966 ;; Ignored element/object.
1967 ((memq data
(plist-get info
:ignore-list
)) nil
)
1969 ((eq type
'plain-text
)
1970 (org-export-filter-apply-functions
1971 (plist-get info
:filter-plain-text
)
1972 (let ((transcoder (org-export-transcoder data info
)))
1973 (if transcoder
(funcall transcoder data info
) data
))
1975 ;; Secondary string.
1977 (mapconcat (lambda (obj) (org-export-data obj info
)) data
""))
1978 ;; Element/Object without contents or, as a special
1979 ;; case, headline with archive tag and archived trees
1980 ;; restricted to title only.
1981 ((or (not (org-element-contents data
))
1982 (and (eq type
'headline
)
1983 (eq (plist-get info
:with-archived-trees
) 'headline
)
1984 (org-element-property :archivedp data
)))
1985 (let ((transcoder (org-export-transcoder data info
)))
1986 (or (and (functionp transcoder
)
1987 (funcall transcoder data nil info
))
1988 ;; Export snippets never return a nil value so
1989 ;; that white spaces following them are never
1991 (and (eq type
'export-snippet
) ""))))
1992 ;; Element/Object with contents.
1994 (let ((transcoder (org-export-transcoder data info
)))
1996 (let* ((greaterp (memq type org-element-greater-elements
))
1999 (memq type org-element-recursive-objects
)))
2002 (lambda (element) (org-export-data element info
))
2003 (org-element-contents
2004 (if (or greaterp objectp
) data
2005 ;; Elements directly containing
2006 ;; objects must have their indentation
2007 ;; normalized first.
2008 (org-element-normalize-contents
2010 ;; When normalizing contents of the
2011 ;; first paragraph in an item or
2012 ;; a footnote definition, ignore
2013 ;; first line's indentation: there is
2014 ;; none and it might be misleading.
2015 (when (eq type
'paragraph
)
2016 (let ((parent (org-export-get-parent data
)))
2018 (eq (car (org-element-contents parent
))
2020 (memq (org-element-type parent
)
2021 '(footnote-definition item
))))))))
2023 (funcall transcoder data
2024 (if (not greaterp
) contents
2025 (org-element-normalize-string contents
))
2027 ;; Final result will be memoized before being returned.
2032 ((memq type
'(org-data plain-text nil
)) results
)
2033 ;; Append the same white space between elements or objects
2034 ;; as in the original buffer, and call appropriate filters.
2037 (org-export-filter-apply-functions
2038 (plist-get info
(intern (format ":filter-%s" type
)))
2039 (let ((post-blank (or (org-element-property :post-blank data
)
2041 (if (memq type org-element-all-elements
)
2042 (concat (org-element-normalize-string results
)
2043 (make-string post-blank ?
\n))
2044 (concat results
(make-string post-blank ?\s
))))
2047 (plist-get info
:exported-data
)))))
2049 (defun org-export-data-with-backend (data backend info
)
2050 "Convert DATA into BACKEND format.
2052 DATA is an element, an object, a secondary string or a string.
2053 BACKEND is a symbol. INFO is a plist used as a communication
2056 Unlike to `org-export-with-backend', this function will
2057 recursively convert DATA using BACKEND translation table."
2058 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
2061 ;; Set-up a new communication channel with translations defined in
2062 ;; BACKEND as the translate table and a new hash table for
2066 (list :back-end backend
2067 :translate-alist
(org-export-get-all-transcoders backend
)
2068 ;; Size of the hash table is reduced since this function
2069 ;; will probably be used on small trees.
2070 :exported-data
(make-hash-table :test
'eq
:size
401)))))
2072 (defun org-export-remove-uninterpreted-data (data info
)
2073 "Change uninterpreted elements back into Org syntax.
2074 DATA is the parse tree. INFO is a plist containing export
2075 options. Each uninterpreted element or object is changed back
2076 into a string. Contents, if any, are not modified. The parse
2077 tree is modified by side effect and returned by the function."
2078 (org-export--remove-uninterpreted-data-1 data info
)
2079 (dolist (prop '(:author
:date
:title
))
2082 (org-export--remove-uninterpreted-data-1
2083 (plist-get info prop
)
2086 (defun org-export--remove-uninterpreted-data-1 (data info
)
2087 "Change uninterpreted elements back into Org syntax.
2088 DATA is a parse tree or a secondary string. INFO is a plist
2089 containing export options. It is modified by side effect and
2090 returned by the function."
2091 (org-element-map data
2092 '(entity bold italic latex-environment latex-fragment strike-through
2093 subscript superscript underline
)
2096 (case (org-element-type blob
)
2099 (and (not (plist-get info
:with-entities
))
2101 (org-export-expand blob nil
)
2103 (or (org-element-property :post-blank blob
) 0)
2106 ((bold italic strike-through underline
)
2107 (and (not (plist-get info
:with-emphasize
))
2108 (let ((marker (case (org-element-type blob
)
2111 (strike-through "+")
2115 (org-element-contents blob
)
2119 (or (org-element-property :post-blank blob
)
2122 ;; ... LaTeX environments and fragments...
2123 ((latex-environment latex-fragment
)
2124 (and (eq (plist-get info
:with-latex
) 'verbatim
)
2125 (list (org-export-expand blob nil
))))
2126 ;; ... sub/superscripts...
2127 ((subscript superscript
)
2128 (let ((sub/super-p
(plist-get info
:with-sub-superscript
))
2129 (bracketp (org-element-property :use-brackets-p blob
)))
2130 (and (or (not sub
/super-p
)
2131 (and (eq sub
/super-p
'{}) (not bracketp
)))
2134 (if (eq (org-element-type blob
) 'subscript
)
2137 (and bracketp
"{")))
2138 (org-element-contents blob
)
2141 (and (org-element-property :post-blank blob
)
2143 (org-element-property :post-blank blob
)
2146 ;; Splice NEW at BLOB location in parse tree.
2147 (dolist (e new
(org-element-extract-element blob
))
2148 (unless (string= e
"") (org-element-insert-before e blob
))))))
2150 ;; Return modified parse tree.
2153 (defun org-export-expand (blob contents
&optional with-affiliated
)
2154 "Expand a parsed element or object to its original state.
2156 BLOB is either an element or an object. CONTENTS is its
2157 contents, as a string or nil.
2159 When optional argument WITH-AFFILIATED is non-nil, add affiliated
2160 keywords before output."
2161 (let ((type (org-element-type blob
)))
2162 (concat (and with-affiliated
(memq type org-element-all-elements
)
2163 (org-element--interpret-affiliated-keywords blob
))
2164 (funcall (intern (format "org-element-%s-interpreter" type
))
2167 (defun org-export-ignore-element (element info
)
2168 "Add ELEMENT to `:ignore-list' in INFO.
2170 Any element in `:ignore-list' will be skipped when using
2171 `org-element-map'. INFO is modified by side effects."
2172 (plist-put info
:ignore-list
(cons element
(plist-get info
:ignore-list
))))
2176 ;;; The Filter System
2178 ;; Filters allow end-users to tweak easily the transcoded output.
2179 ;; They are the functional counterpart of hooks, as every filter in
2180 ;; a set is applied to the return value of the previous one.
2182 ;; Every set is back-end agnostic. Although, a filter is always
2183 ;; called, in addition to the string it applies to, with the back-end
2184 ;; used as argument, so it's easy for the end-user to add back-end
2185 ;; specific filters in the set. The communication channel, as
2186 ;; a plist, is required as the third argument.
2188 ;; From the developer side, filters sets can be installed in the
2189 ;; process with the help of `org-export-define-backend', which
2190 ;; internally stores filters as an alist. Each association has a key
2191 ;; among the following symbols and a function or a list of functions
2194 ;; - `:filter-options' applies to the property list containing export
2195 ;; options. Unlike to other filters, functions in this list accept
2196 ;; two arguments instead of three: the property list containing
2197 ;; export options and the back-end. Users can set its value through
2198 ;; `org-export-filter-options-functions' variable.
2200 ;; - `:filter-parse-tree' applies directly to the complete parsed
2201 ;; tree. Users can set it through
2202 ;; `org-export-filter-parse-tree-functions' variable.
2204 ;; - `:filter-body' applies to the body of the output, before template
2205 ;; translator chimes in. Users can set it through
2206 ;; `org-export-filter-body-functions' variable.
2208 ;; - `:filter-final-output' applies to the final transcoded string.
2209 ;; Users can set it with `org-export-filter-final-output-functions'
2212 ;; - `:filter-plain-text' applies to any string not recognized as Org
2213 ;; syntax. `org-export-filter-plain-text-functions' allows users to
2216 ;; - `:filter-TYPE' applies on the string returned after an element or
2217 ;; object of type TYPE has been transcoded. A user can modify
2218 ;; `org-export-filter-TYPE-functions' to install these filters.
2220 ;; All filters sets are applied with
2221 ;; `org-export-filter-apply-functions' function. Filters in a set are
2222 ;; applied in a LIFO fashion. It allows developers to be sure that
2223 ;; their filters will be applied first.
2225 ;; Filters properties are installed in communication channel with
2226 ;; `org-export-install-filters' function.
2228 ;; Eventually, two hooks (`org-export-before-processing-hook' and
2229 ;; `org-export-before-parsing-hook') are run at the beginning of the
2230 ;; export process and just before parsing to allow for heavy structure
2236 (defvar org-export-before-processing-hook nil
2237 "Hook run at the beginning of the export process.
2239 This is run before include keywords and macros are expanded and
2240 Babel code blocks executed, on a copy of the original buffer
2241 being exported. Visibility and narrowing are preserved. Point
2242 is at the beginning of the buffer.
2244 Every function in this hook will be called with one argument: the
2245 back-end currently used, as a symbol.")
2247 (defvar org-export-before-parsing-hook nil
2248 "Hook run before parsing an export buffer.
2250 This is run after include keywords and macros have been expanded
2251 and Babel code blocks executed, on a copy of the original buffer
2252 being exported. Visibility and narrowing are preserved. Point
2253 is at the beginning of the buffer.
2255 Every function in this hook will be called with one argument: the
2256 back-end currently used, as a symbol.")
2259 ;;;; Special Filters
2261 (defvar org-export-filter-options-functions nil
2262 "List of functions applied to the export options.
2263 Each filter is called with two arguments: the export options, as
2264 a plist, and the back-end, as a symbol. It must return
2265 a property list containing export options.")
2267 (defvar org-export-filter-parse-tree-functions nil
2268 "List of functions applied to the parsed tree.
2269 Each filter is called with three arguments: the parse tree, as
2270 returned by `org-element-parse-buffer', the back-end, as
2271 a symbol, and the communication channel, as a plist. It must
2272 return the modified parse tree to transcode.")
2274 (defvar org-export-filter-plain-text-functions nil
2275 "List of functions applied to plain text.
2276 Each filter is called with three arguments: a string which
2277 contains no Org syntax, the back-end, as a symbol, and the
2278 communication channel, as a plist. It must return a string or
2281 (defvar org-export-filter-body-functions nil
2282 "List of functions applied to transcoded body.
2283 Each filter is called with three arguments: a string which
2284 contains no Org syntax, the back-end, as a symbol, and the
2285 communication channel, as a plist. It must return a string or
2288 (defvar org-export-filter-final-output-functions nil
2289 "List of functions applied to the transcoded string.
2290 Each filter is called with three arguments: the full transcoded
2291 string, the back-end, as a symbol, and the communication channel,
2292 as a plist. It must return a string that will be used as the
2293 final export output.")
2296 ;;;; Elements Filters
2298 (defvar org-export-filter-babel-call-functions nil
2299 "List of functions applied to a transcoded babel-call.
2300 Each filter is called with three arguments: the transcoded data,
2301 as a string, the back-end, as a symbol, and the communication
2302 channel, as a plist. It must return a string or nil.")
2304 (defvar org-export-filter-center-block-functions nil
2305 "List of functions applied to a transcoded center block.
2306 Each filter is called with three arguments: the transcoded data,
2307 as a string, the back-end, as a symbol, and the communication
2308 channel, as a plist. It must return a string or nil.")
2310 (defvar org-export-filter-clock-functions nil
2311 "List of functions applied to a transcoded clock.
2312 Each filter is called with three arguments: the transcoded data,
2313 as a string, the back-end, as a symbol, and the communication
2314 channel, as a plist. It must return a string or nil.")
2316 (defvar org-export-filter-comment-functions nil
2317 "List of functions applied to a transcoded comment.
2318 Each filter is called with three arguments: the transcoded data,
2319 as a string, the back-end, as a symbol, and the communication
2320 channel, as a plist. It must return a string or nil.")
2322 (defvar org-export-filter-comment-block-functions nil
2323 "List of functions applied to a transcoded comment-block.
2324 Each filter is called with three arguments: the transcoded data,
2325 as a string, the back-end, as a symbol, and the communication
2326 channel, as a plist. It must return a string or nil.")
2328 (defvar org-export-filter-diary-sexp-functions nil
2329 "List of functions applied to a transcoded diary-sexp.
2330 Each filter is called with three arguments: the transcoded data,
2331 as a string, the back-end, as a symbol, and the communication
2332 channel, as a plist. It must return a string or nil.")
2334 (defvar org-export-filter-drawer-functions nil
2335 "List of functions applied to a transcoded drawer.
2336 Each filter is called with three arguments: the transcoded data,
2337 as a string, the back-end, as a symbol, and the communication
2338 channel, as a plist. It must return a string or nil.")
2340 (defvar org-export-filter-dynamic-block-functions nil
2341 "List of functions applied to a transcoded dynamic-block.
2342 Each filter is called with three arguments: the transcoded data,
2343 as a string, the back-end, as a symbol, and the communication
2344 channel, as a plist. It must return a string or nil.")
2346 (defvar org-export-filter-example-block-functions nil
2347 "List of functions applied to a transcoded example-block.
2348 Each filter is called with three arguments: the transcoded data,
2349 as a string, the back-end, as a symbol, and the communication
2350 channel, as a plist. It must return a string or nil.")
2352 (defvar org-export-filter-export-block-functions nil
2353 "List of functions applied to a transcoded export-block.
2354 Each filter is called with three arguments: the transcoded data,
2355 as a string, the back-end, as a symbol, and the communication
2356 channel, as a plist. It must return a string or nil.")
2358 (defvar org-export-filter-fixed-width-functions nil
2359 "List of functions applied to a transcoded fixed-width.
2360 Each filter is called with three arguments: the transcoded data,
2361 as a string, the back-end, as a symbol, and the communication
2362 channel, as a plist. It must return a string or nil.")
2364 (defvar org-export-filter-footnote-definition-functions nil
2365 "List of functions applied to a transcoded footnote-definition.
2366 Each filter is called with three arguments: the transcoded data,
2367 as a string, the back-end, as a symbol, and the communication
2368 channel, as a plist. It must return a string or nil.")
2370 (defvar org-export-filter-headline-functions nil
2371 "List of functions applied to a transcoded headline.
2372 Each filter is called with three arguments: the transcoded data,
2373 as a string, the back-end, as a symbol, and the communication
2374 channel, as a plist. It must return a string or nil.")
2376 (defvar org-export-filter-horizontal-rule-functions nil
2377 "List of functions applied to a transcoded horizontal-rule.
2378 Each filter is called with three arguments: the transcoded data,
2379 as a string, the back-end, as a symbol, and the communication
2380 channel, as a plist. It must return a string or nil.")
2382 (defvar org-export-filter-inlinetask-functions nil
2383 "List of functions applied to a transcoded inlinetask.
2384 Each filter is called with three arguments: the transcoded data,
2385 as a string, the back-end, as a symbol, and the communication
2386 channel, as a plist. It must return a string or nil.")
2388 (defvar org-export-filter-item-functions nil
2389 "List of functions applied to a transcoded item.
2390 Each filter is called with three arguments: the transcoded data,
2391 as a string, the back-end, as a symbol, and the communication
2392 channel, as a plist. It must return a string or nil.")
2394 (defvar org-export-filter-keyword-functions nil
2395 "List of functions applied to a transcoded keyword.
2396 Each filter is called with three arguments: the transcoded data,
2397 as a string, the back-end, as a symbol, and the communication
2398 channel, as a plist. It must return a string or nil.")
2400 (defvar org-export-filter-latex-environment-functions nil
2401 "List of functions applied to a transcoded latex-environment.
2402 Each filter is called with three arguments: the transcoded data,
2403 as a string, the back-end, as a symbol, and the communication
2404 channel, as a plist. It must return a string or nil.")
2406 (defvar org-export-filter-node-property-functions nil
2407 "List of functions applied to a transcoded node-property.
2408 Each filter is called with three arguments: the transcoded data,
2409 as a string, the back-end, as a symbol, and the communication
2410 channel, as a plist. It must return a string or nil.")
2412 (defvar org-export-filter-paragraph-functions nil
2413 "List of functions applied to a transcoded paragraph.
2414 Each filter is called with three arguments: the transcoded data,
2415 as a string, the back-end, as a symbol, and the communication
2416 channel, as a plist. It must return a string or nil.")
2418 (defvar org-export-filter-plain-list-functions nil
2419 "List of functions applied to a transcoded plain-list.
2420 Each filter is called with three arguments: the transcoded data,
2421 as a string, the back-end, as a symbol, and the communication
2422 channel, as a plist. It must return a string or nil.")
2424 (defvar org-export-filter-planning-functions nil
2425 "List of functions applied to a transcoded planning.
2426 Each filter is called with three arguments: the transcoded data,
2427 as a string, the back-end, as a symbol, and the communication
2428 channel, as a plist. It must return a string or nil.")
2430 (defvar org-export-filter-property-drawer-functions nil
2431 "List of functions applied to a transcoded property-drawer.
2432 Each filter is called with three arguments: the transcoded data,
2433 as a string, the back-end, as a symbol, and the communication
2434 channel, as a plist. It must return a string or nil.")
2436 (defvar org-export-filter-quote-block-functions nil
2437 "List of functions applied to a transcoded quote block.
2438 Each filter is called with three arguments: the transcoded quote
2439 data, as a string, the back-end, as a symbol, and the
2440 communication channel, as a plist. It must return a string or
2443 (defvar org-export-filter-section-functions nil
2444 "List of functions applied to a transcoded section.
2445 Each filter is called with three arguments: the transcoded data,
2446 as a string, the back-end, as a symbol, and the communication
2447 channel, as a plist. It must return a string or nil.")
2449 (defvar org-export-filter-special-block-functions nil
2450 "List of functions applied to a transcoded special block.
2451 Each filter is called with three arguments: the transcoded data,
2452 as a string, the back-end, as a symbol, and the communication
2453 channel, as a plist. It must return a string or nil.")
2455 (defvar org-export-filter-src-block-functions nil
2456 "List of functions applied to a transcoded src-block.
2457 Each filter is called with three arguments: the transcoded data,
2458 as a string, the back-end, as a symbol, and the communication
2459 channel, as a plist. It must return a string or nil.")
2461 (defvar org-export-filter-table-functions nil
2462 "List of functions applied to a transcoded table.
2463 Each filter is called with three arguments: the transcoded data,
2464 as a string, the back-end, as a symbol, and the communication
2465 channel, as a plist. It must return a string or nil.")
2467 (defvar org-export-filter-table-cell-functions nil
2468 "List of functions applied to a transcoded table-cell.
2469 Each filter is called with three arguments: the transcoded data,
2470 as a string, the back-end, as a symbol, and the communication
2471 channel, as a plist. It must return a string or nil.")
2473 (defvar org-export-filter-table-row-functions nil
2474 "List of functions applied to a transcoded table-row.
2475 Each filter is called with three arguments: the transcoded data,
2476 as a string, the back-end, as a symbol, and the communication
2477 channel, as a plist. It must return a string or nil.")
2479 (defvar org-export-filter-verse-block-functions nil
2480 "List of functions applied to a transcoded verse block.
2481 Each filter is called with three arguments: the transcoded data,
2482 as a string, the back-end, as a symbol, and the communication
2483 channel, as a plist. It must return a string or nil.")
2486 ;;;; Objects Filters
2488 (defvar org-export-filter-bold-functions nil
2489 "List of functions applied to transcoded bold text.
2490 Each filter is called with three arguments: the transcoded data,
2491 as a string, the back-end, as a symbol, and the communication
2492 channel, as a plist. It must return a string or nil.")
2494 (defvar org-export-filter-code-functions nil
2495 "List of functions applied to transcoded code text.
2496 Each filter is called with three arguments: the transcoded data,
2497 as a string, the back-end, as a symbol, and the communication
2498 channel, as a plist. It must return a string or nil.")
2500 (defvar org-export-filter-entity-functions nil
2501 "List of functions applied to a transcoded entity.
2502 Each filter is called with three arguments: the transcoded data,
2503 as a string, the back-end, as a symbol, and the communication
2504 channel, as a plist. It must return a string or nil.")
2506 (defvar org-export-filter-export-snippet-functions nil
2507 "List of functions applied to a transcoded export-snippet.
2508 Each filter is called with three arguments: the transcoded data,
2509 as a string, the back-end, as a symbol, and the communication
2510 channel, as a plist. It must return a string or nil.")
2512 (defvar org-export-filter-footnote-reference-functions nil
2513 "List of functions applied to a transcoded footnote-reference.
2514 Each filter is called with three arguments: the transcoded data,
2515 as a string, the back-end, as a symbol, and the communication
2516 channel, as a plist. It must return a string or nil.")
2518 (defvar org-export-filter-inline-babel-call-functions nil
2519 "List of functions applied to a transcoded inline-babel-call.
2520 Each filter is called with three arguments: the transcoded data,
2521 as a string, the back-end, as a symbol, and the communication
2522 channel, as a plist. It must return a string or nil.")
2524 (defvar org-export-filter-inline-src-block-functions nil
2525 "List of functions applied to a transcoded inline-src-block.
2526 Each filter is called with three arguments: the transcoded data,
2527 as a string, the back-end, as a symbol, and the communication
2528 channel, as a plist. It must return a string or nil.")
2530 (defvar org-export-filter-italic-functions nil
2531 "List of functions applied to transcoded italic text.
2532 Each filter is called with three arguments: the transcoded data,
2533 as a string, the back-end, as a symbol, and the communication
2534 channel, as a plist. It must return a string or nil.")
2536 (defvar org-export-filter-latex-fragment-functions nil
2537 "List of functions applied to a transcoded latex-fragment.
2538 Each filter is called with three arguments: the transcoded data,
2539 as a string, the back-end, as a symbol, and the communication
2540 channel, as a plist. It must return a string or nil.")
2542 (defvar org-export-filter-line-break-functions nil
2543 "List of functions applied to a transcoded line-break.
2544 Each filter is called with three arguments: the transcoded data,
2545 as a string, the back-end, as a symbol, and the communication
2546 channel, as a plist. It must return a string or nil.")
2548 (defvar org-export-filter-link-functions nil
2549 "List of functions applied to a transcoded link.
2550 Each filter is called with three arguments: the transcoded data,
2551 as a string, the back-end, as a symbol, and the communication
2552 channel, as a plist. It must return a string or nil.")
2554 (defvar org-export-filter-radio-target-functions nil
2555 "List of functions applied to a transcoded radio-target.
2556 Each filter is called with three arguments: the transcoded data,
2557 as a string, the back-end, as a symbol, and the communication
2558 channel, as a plist. It must return a string or nil.")
2560 (defvar org-export-filter-statistics-cookie-functions nil
2561 "List of functions applied to a transcoded statistics-cookie.
2562 Each filter is called with three arguments: the transcoded data,
2563 as a string, the back-end, as a symbol, and the communication
2564 channel, as a plist. It must return a string or nil.")
2566 (defvar org-export-filter-strike-through-functions nil
2567 "List of functions applied to transcoded strike-through text.
2568 Each filter is called with three arguments: the transcoded data,
2569 as a string, the back-end, as a symbol, and the communication
2570 channel, as a plist. It must return a string or nil.")
2572 (defvar org-export-filter-subscript-functions nil
2573 "List of functions applied to a transcoded subscript.
2574 Each filter is called with three arguments: the transcoded data,
2575 as a string, the back-end, as a symbol, and the communication
2576 channel, as a plist. It must return a string or nil.")
2578 (defvar org-export-filter-superscript-functions nil
2579 "List of functions applied to a transcoded superscript.
2580 Each filter is called with three arguments: the transcoded data,
2581 as a string, the back-end, as a symbol, and the communication
2582 channel, as a plist. It must return a string or nil.")
2584 (defvar org-export-filter-target-functions nil
2585 "List of functions applied to a transcoded target.
2586 Each filter is called with three arguments: the transcoded data,
2587 as a string, the back-end, as a symbol, and the communication
2588 channel, as a plist. It must return a string or nil.")
2590 (defvar org-export-filter-timestamp-functions nil
2591 "List of functions applied to a transcoded timestamp.
2592 Each filter is called with three arguments: the transcoded data,
2593 as a string, the back-end, as a symbol, and the communication
2594 channel, as a plist. It must return a string or nil.")
2596 (defvar org-export-filter-underline-functions nil
2597 "List of functions applied to transcoded underline text.
2598 Each filter is called with three arguments: the transcoded data,
2599 as a string, the back-end, as a symbol, and the communication
2600 channel, as a plist. It must return a string or nil.")
2602 (defvar org-export-filter-verbatim-functions nil
2603 "List of functions applied to transcoded verbatim text.
2604 Each filter is called with three arguments: the transcoded data,
2605 as a string, the back-end, as a symbol, and the communication
2606 channel, as a plist. It must return a string or nil.")
2611 ;; Internal function `org-export-install-filters' installs filters
2612 ;; hard-coded in back-ends (developer filters) and filters from global
2613 ;; variables (user filters) in the communication channel.
2615 ;; Internal function `org-export-filter-apply-functions' takes care
2616 ;; about applying each filter in order to a given data. It ignores
2617 ;; filters returning a nil value but stops whenever a filter returns
2620 (defun org-export-filter-apply-functions (filters value info
)
2621 "Call every function in FILTERS.
2623 Functions are called with arguments VALUE, current export
2624 back-end's name and INFO. A function returning a nil value will
2625 be skipped. If it returns the empty string, the process ends and
2628 Call is done in a LIFO fashion, to be sure that developer
2629 specified filters, if any, are called first."
2631 (let* ((backend (plist-get info
:back-end
))
2632 (backend-name (and backend
(org-export-backend-name backend
))))
2633 (dolist (filter filters value
)
2634 (let ((result (funcall filter value backend-name info
)))
2635 (cond ((not result
) value
)
2636 ((equal value
"") (throw 'exit nil
))
2637 (t (setq value result
))))))))
2639 (defun org-export-install-filters (info)
2640 "Install filters properties in communication channel.
2641 INFO is a plist containing the current communication channel.
2642 Return the updated communication channel."
2644 ;; Install user-defined filters with `org-export-filters-alist'
2645 ;; and filters already in INFO (through ext-plist mechanism).
2647 (let* ((prop (car p
))
2648 (info-value (plist-get info prop
))
2649 (default-value (symbol-value (cdr p
))))
2651 (plist-put plist prop
2652 ;; Filters in INFO will be called
2653 ;; before those user provided.
2654 (append (if (listp info-value
) info-value
2657 org-export-filters-alist
)
2658 ;; Prepend back-end specific filters to that list.
2660 ;; Single values get consed, lists are appended.
2661 (let ((key (car p
)) (value (cdr p
)))
2666 (if (atom value
) (cons value
(plist-get plist key
))
2667 (append value
(plist-get plist key
))))))))
2668 (org-export-get-all-filters (plist-get info
:back-end
)))
2669 ;; Return new communication channel.
2670 (org-combine-plists info plist
)))
2676 ;; This is the room for the main function, `org-export-as', along with
2677 ;; its derivative, `org-export-string-as'.
2678 ;; `org-export--copy-to-kill-ring-p' determines if output of these
2679 ;; function should be added to kill ring.
2681 ;; Note that `org-export-as' doesn't really parse the current buffer,
2682 ;; but a copy of it (with the same buffer-local variables and
2683 ;; visibility), where macros and include keywords are expanded and
2684 ;; Babel blocks are executed, if appropriate.
2685 ;; `org-export-with-buffer-copy' macro prepares that copy.
2687 ;; File inclusion is taken care of by
2688 ;; `org-export-expand-include-keyword' and
2689 ;; `org-export--prepare-file-contents'. Structure wise, including
2690 ;; a whole Org file in a buffer often makes little sense. For
2691 ;; example, if the file contains a headline and the include keyword
2692 ;; was within an item, the item should contain the headline. That's
2693 ;; why file inclusion should be done before any structure can be
2694 ;; associated to the file, that is before parsing.
2696 ;; `org-export-insert-default-template' is a command to insert
2697 ;; a default template (or a back-end specific template) at point or in
2700 (defun org-export-copy-buffer ()
2701 "Return a copy of the current buffer.
2702 The copy preserves Org buffer-local variables, visibility and
2704 (let ((copy-buffer-fun (org-export--generate-copy-script (current-buffer)))
2705 (new-buf (generate-new-buffer (buffer-name))))
2706 (with-current-buffer new-buf
2707 (funcall copy-buffer-fun
)
2708 (set-buffer-modified-p nil
))
2711 (defmacro org-export-with-buffer-copy
(&rest body
)
2712 "Apply BODY in a copy of the current buffer.
2713 The copy preserves local variables, visibility and contents of
2714 the original buffer. Point is at the beginning of the buffer
2715 when BODY is applied."
2717 (org-with-gensyms (buf-copy)
2718 `(let ((,buf-copy
(org-export-copy-buffer)))
2720 (with-current-buffer ,buf-copy
2721 (goto-char (point-min))
2723 (and (buffer-live-p ,buf-copy
)
2724 ;; Kill copy without confirmation.
2725 (progn (with-current-buffer ,buf-copy
2726 (restore-buffer-modified-p nil
))
2727 (kill-buffer ,buf-copy
)))))))
2729 (defun org-export--generate-copy-script (buffer)
2730 "Generate a function duplicating BUFFER.
2732 The copy will preserve local variables, visibility, contents and
2733 narrowing of the original buffer. If a region was active in
2734 BUFFER, contents will be narrowed to that region instead.
2736 The resulting function can be evaluated at a later time, from
2737 another buffer, effectively cloning the original buffer there.
2739 The function assumes BUFFER's major mode is `org-mode'."
2740 (with-current-buffer buffer
2742 (let ((inhibit-modification-hooks t
))
2743 ;; Set major mode. Ignore `org-mode-hook' as it has been run
2744 ;; already in BUFFER.
2745 (let ((org-mode-hook nil
) (org-inhibit-startup t
)) (org-mode))
2746 ;; Copy specific buffer local variables and variables set
2747 ;; through BIND keywords.
2748 ,@(let ((bound-variables (org-export--list-bound-variables))
2750 (dolist (entry (buffer-local-variables (buffer-base-buffer)) vars
)
2752 (let ((var (car entry
))
2754 (and (not (memq var org-export-ignored-local-variables
))
2758 buffer-file-coding-system
))
2759 (assq var bound-variables
)
2760 (string-match "^\\(org-\\|orgtbl-\\)"
2762 ;; Skip unreadable values, as they cannot be
2763 ;; sent to external process.
2764 (or (not val
) (ignore-errors (read (format "%S" val
))))
2765 (push `(set (make-local-variable (quote ,var
))
2768 ;; Whole buffer contents.
2770 ,(org-with-wide-buffer
2771 (buffer-substring-no-properties
2772 (point-min) (point-max))))
2774 ,(if (org-region-active-p)
2775 `(narrow-to-region ,(region-beginning) ,(region-end))
2776 `(narrow-to-region ,(point-min) ,(point-max)))
2777 ;; Current position of point.
2778 (goto-char ,(point))
2779 ;; Overlays with invisible property.
2783 (let ((invis-prop (overlay-get ov
'invisible
)))
2786 (make-overlay ,(overlay-start ov
)
2788 'invisible
(quote ,invis-prop
))
2790 (overlays-in (point-min) (point-max)))
2794 (defun org-export-as
2795 (backend &optional subtreep visible-only body-only ext-plist
)
2796 "Transcode current Org buffer into BACKEND code.
2798 BACKEND is either an export back-end, as returned by, e.g.,
2799 `org-export-create-backend', or a symbol referring to
2800 a registered back-end.
2802 If narrowing is active in the current buffer, only transcode its
2805 If a region is active, transcode that region.
2807 When optional argument SUBTREEP is non-nil, transcode the
2808 sub-tree at point, extracting information from the headline
2811 When optional argument VISIBLE-ONLY is non-nil, don't export
2812 contents of hidden elements.
2814 When optional argument BODY-ONLY is non-nil, only return body
2815 code, without surrounding template.
2817 Optional argument EXT-PLIST, when provided, is a property list
2818 with external parameters overriding Org default settings, but
2819 still inferior to file-local settings.
2821 Return code as a string."
2822 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
2823 (org-export-barf-if-invalid-backend backend
)
2826 ;; Narrow buffer to an appropriate region or subtree for
2827 ;; parsing. If parsing subtree, be sure to remove main headline
2829 (cond ((org-region-active-p)
2830 (narrow-to-region (region-beginning) (region-end)))
2832 (org-narrow-to-subtree)
2833 (goto-char (point-min))
2835 (narrow-to-region (point) (point-max))))
2836 ;; Initialize communication channel with original buffer
2837 ;; attributes, unavailable in its copy.
2838 (let* ((org-export-current-backend (org-export-backend-name backend
))
2839 (info (org-combine-plists
2840 (list :export-options
2842 (list (and subtreep
'subtree
)
2843 (and visible-only
'visible-only
)
2844 (and body-only
'body-only
))))
2845 (org-export--get-buffer-attributes)))
2847 ;; Update communication channel and get parse tree. Buffer
2848 ;; isn't parsed directly. Instead, a temporary copy is
2849 ;; created, where include keywords, macros are expanded and
2850 ;; code blocks are evaluated.
2851 (org-export-with-buffer-copy
2852 ;; Run first hook with current back-end's name as argument.
2853 (run-hook-with-args 'org-export-before-processing-hook
2854 (org-export-backend-name backend
))
2855 (org-export-expand-include-keyword)
2856 ;; Update macro templates since #+INCLUDE keywords might have
2857 ;; added some new ones.
2858 (org-macro-initialize-templates)
2859 (org-macro-replace-all org-macro-templates
)
2860 (org-export-execute-babel-code)
2861 ;; Update radio targets since keyword inclusion might have
2863 (org-update-radio-target-regexp)
2864 ;; Run last hook with current back-end's name as argument.
2865 (goto-char (point-min))
2867 (run-hook-with-args 'org-export-before-parsing-hook
2868 (org-export-backend-name backend
)))
2869 ;; Update communication channel with environment. Also
2870 ;; install user's and developer's filters.
2872 (org-export-install-filters
2874 info
(org-export-get-environment backend subtreep ext-plist
))))
2875 ;; Expand export-specific set of macros: {{{author}}},
2876 ;; {{{date}}}, {{{email}}} and {{{title}}}. It must be done
2877 ;; once regular macros have been expanded, since document
2878 ;; keywords may contain one of them.
2879 (org-macro-replace-all
2880 (list (cons "author"
2881 (org-element-interpret-data (plist-get info
:author
)))
2883 (org-element-interpret-data (plist-get info
:date
)))
2884 ;; EMAIL is not a parsed keyword: store it as-is.
2885 (cons "email" (or (plist-get info
:email
) ""))
2887 (org-element-interpret-data (plist-get info
:title
))))
2890 (setq tree
(org-element-parse-buffer nil visible-only
))
2891 ;; Handle left-over uninterpreted elements or objects in
2892 ;; parse tree and communication channel.
2893 (org-export-remove-uninterpreted-data tree info
)
2894 ;; Call options filters and update export options. We do not
2895 ;; use `org-export-filter-apply-functions' here since the
2896 ;; arity of such filters is different.
2897 (let ((backend-name (org-export-backend-name backend
)))
2898 (dolist (filter (plist-get info
:filter-options
))
2899 (let ((result (funcall filter info backend-name
)))
2900 (when result
(setq info result
)))))
2901 ;; Parse buffer, handle uninterpreted elements or objects,
2902 ;; then call parse-tree filters.
2904 (org-export-filter-apply-functions
2905 (plist-get info
:filter-parse-tree
) tree info
))
2906 ;; Now tree is complete, compute its properties and add them
2907 ;; to communication channel.
2910 info
(org-export-collect-tree-properties tree info
)))
2911 ;; Eventually transcode TREE. Wrap the resulting string into
2913 (let* ((body (org-element-normalize-string
2914 (or (org-export-data tree info
) "")))
2915 (inner-template (cdr (assq 'inner-template
2916 (plist-get info
:translate-alist
))))
2917 (full-body (org-export-filter-apply-functions
2918 (plist-get info
:filter-body
)
2919 (if (not (functionp inner-template
)) body
2920 (funcall inner-template body info
))
2922 (template (cdr (assq 'template
2923 (plist-get info
:translate-alist
)))))
2924 ;; Remove all text properties since they cannot be
2925 ;; retrieved from an external process. Finally call
2926 ;; final-output filter and return result.
2928 (org-export-filter-apply-functions
2929 (plist-get info
:filter-final-output
)
2930 (if (or (not (functionp template
)) body-only
) full-body
2931 (funcall template full-body info
))
2935 (defun org-export-string-as (string backend
&optional body-only ext-plist
)
2936 "Transcode STRING into BACKEND code.
2938 BACKEND is either an export back-end, as returned by, e.g.,
2939 `org-export-create-backend', or a symbol referring to
2940 a registered back-end.
2942 When optional argument BODY-ONLY is non-nil, only return body
2943 code, without preamble nor postamble.
2945 Optional argument EXT-PLIST, when provided, is a property list
2946 with external parameters overriding Org default settings, but
2947 still inferior to file-local settings.
2949 Return code as a string."
2952 (let ((org-inhibit-startup t
)) (org-mode))
2953 (org-export-as backend nil nil body-only ext-plist
)))
2956 (defun org-export-replace-region-by (backend)
2957 "Replace the active region by its export to BACKEND.
2958 BACKEND is either an export back-end, as returned by, e.g.,
2959 `org-export-create-backend', or a symbol referring to
2960 a registered back-end."
2961 (if (not (org-region-active-p))
2962 (user-error "No active region to replace")
2963 (let* ((beg (region-beginning))
2965 (str (buffer-substring beg end
)) rpl
)
2966 (setq rpl
(org-export-string-as str backend t
))
2967 (delete-region beg end
)
2971 (defun org-export-insert-default-template (&optional backend subtreep
)
2972 "Insert all export keywords with default values at beginning of line.
2974 BACKEND is a symbol referring to the name of a registered export
2975 back-end, for which specific export options should be added to
2976 the template, or `default' for default template. When it is nil,
2977 the user will be prompted for a category.
2979 If SUBTREEP is non-nil, export configuration will be set up
2980 locally for the subtree through node properties."
2982 (unless (derived-mode-p 'org-mode
) (user-error "Not in an Org mode buffer"))
2983 (when (and subtreep
(org-before-first-heading-p))
2984 (user-error "No subtree to set export options for"))
2985 (let ((node (and subtreep
(save-excursion (org-back-to-heading t
) (point))))
2989 (org-completing-read
2990 "Options category: "
2992 (mapcar #'(lambda (b)
2993 (symbol-name (org-export-backend-name b
)))
2994 org-export--registered-backends
))
2997 ;; Populate OPTIONS and KEYWORDS.
2998 (dolist (entry (cond ((eq backend
'default
) org-export-options-alist
)
2999 ((org-export-backend-p backend
)
3000 (org-export-backend-options backend
))
3001 (t (org-export-backend-options
3002 (org-export-get-backend backend
)))))
3003 (let ((keyword (nth 1 entry
))
3004 (option (nth 2 entry
)))
3006 (keyword (unless (assoc keyword keywords
)
3008 (if (eq (nth 4 entry
) 'split
)
3009 (mapconcat #'identity
(eval (nth 3 entry
)) " ")
3010 (eval (nth 3 entry
)))))
3011 (push (cons keyword value
) keywords
))))
3012 (option (unless (assoc option options
)
3013 (push (cons option
(eval (nth 3 entry
))) options
))))))
3014 ;; Move to an appropriate location in order to insert options.
3015 (unless subtreep
(beginning-of-line))
3016 ;; First (multiple) OPTIONS lines. Never go past fill-column.
3020 #'(lambda (opt) (format "%s:%S" (car opt
) (cdr opt
)))
3021 (sort options
(lambda (k1 k2
) (string< (car k1
) (car k2
)))))))
3024 node
"EXPORT_OPTIONS" (mapconcat 'identity items
" "))
3026 (insert "#+OPTIONS:")
3029 (< (+ width
(length (car items
)) 1) fill-column
))
3030 (let ((item (pop items
)))
3032 (incf width
(1+ (length item
))))))
3034 ;; Then the rest of keywords, in the order specified in either
3035 ;; `org-export-options-alist' or respective export back-ends.
3036 (dolist (key (nreverse keywords
))
3037 (let ((val (cond ((equal (car key
) "DATE")
3040 (org-insert-time-stamp (current-time)))))
3041 ((equal (car key
) "TITLE")
3042 (or (let ((visited-file
3043 (buffer-file-name (buffer-base-buffer))))
3045 (file-name-sans-extension
3046 (file-name-nondirectory visited-file
))))
3047 (buffer-name (buffer-base-buffer))))
3049 (if subtreep
(org-entry-put node
(concat "EXPORT_" (car key
)) val
)
3053 (if (org-string-nw-p val
) (format " %s" val
) ""))))))))
3055 (defun org-export-expand-include-keyword (&optional included dir footnotes
)
3056 "Expand every include keyword in buffer.
3057 Optional argument INCLUDED is a list of included file names along
3058 with their line restriction, when appropriate. It is used to
3059 avoid infinite recursion. Optional argument DIR is the current
3060 working directory. It is used to properly resolve relative
3061 paths. Optional argument FOOTNOTES is a hash-table used for
3062 storing and resolving footnotes. It is created automatically."
3063 (let ((case-fold-search t
)
3064 (file-prefix (make-hash-table :test
#'equal
))
3066 (footnotes (or footnotes
(make-hash-table :test
#'equal
)))
3067 (include-re "^[ \t]*#\\+INCLUDE:"))
3068 ;; If :minlevel is not set the text-property
3069 ;; `:org-include-induced-level' will be used to determine the
3070 ;; relative level when expanding INCLUDE.
3071 ;; Only affects included Org documents.
3072 (goto-char (point-min))
3073 (while (re-search-forward include-re nil t
)
3074 (put-text-property (line-beginning-position) (line-end-position)
3075 :org-include-induced-level
3076 (1+ (org-reduced-level (or (org-current-level) 0)))))
3077 ;; Expand INCLUDE keywords.
3078 (goto-char (point-min))
3079 (while (re-search-forward include-re nil t
)
3080 (let ((element (save-match-data (org-element-at-point))))
3081 (when (eq (org-element-type element
) 'keyword
)
3083 ;; Extract arguments from keyword's value.
3084 (let* ((value (org-element-property :value element
))
3085 (ind (org-get-indentation))
3087 (file (and (string-match
3088 "^\\(\".+?\"\\|\\S-+\\)\\(?:\\s-+\\|$\\)" value
)
3091 (let ((matched (match-string 1 value
)))
3092 (when (string-match "\\(::\\(.*?\\)\\)\"?\\'" matched
)
3093 (setq location
(match-string 2 matched
))
3095 (replace-match "" nil nil matched
1)))
3097 (org-remove-double-quotes
3100 (setq value
(replace-match "" nil nil value
)))))
3102 (and (string-match ":only-contents *\\([^: \r\t\n]\\S-*\\)?" value
)
3103 (prog1 (org-not-nil (match-string 1 value
))
3104 (setq value
(replace-match "" nil nil value
)))))
3107 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\""
3109 (prog1 (match-string 1 value
)
3110 (setq value
(replace-match "" nil nil value
)))))
3111 (env (cond ((string-match "\\<example\\>" value
)
3113 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value
)
3115 ;; Minimal level of included file defaults to the child
3116 ;; level of the current headline, if any, or one. It
3117 ;; only applies is the file is meant to be included as
3121 (if (string-match ":minlevel +\\([0-9]+\\)" value
)
3122 (prog1 (string-to-number (match-string 1 value
))
3123 (setq value
(replace-match "" nil nil value
)))
3124 (get-text-property (point) :org-include-induced-level
))))
3125 (src-args (and (eq env
'literal
)
3126 (match-string 1 value
)))
3127 (block (and (string-match "\\<\\(\\S-+\\)\\>" value
)
3128 (match-string 1 value
))))
3130 (delete-region (point) (progn (forward-line) (point)))
3133 ((not (file-readable-p file
))
3134 (error "Cannot include file %s" file
))
3135 ;; Check if files has already been parsed. Look after
3136 ;; inclusion lines too, as different parts of the same file
3137 ;; can be included too.
3138 ((member (list file lines
) included
)
3139 (error "Recursive file inclusion: %s" file
))
3144 (let ((ind-str (make-string ind ?
))
3145 (arg-str (if (stringp src-args
)
3146 (format " %s" src-args
)
3149 (org-escape-code-in-string
3150 (org-export--prepare-file-contents file lines
))))
3151 (format "%s#+BEGIN_%s%s\n%s%s#+END_%s\n"
3152 ind-str block arg-str contents ind-str block
))))
3155 (let ((ind-str (make-string ind ?
))
3157 (org-export--prepare-file-contents file lines
)))
3158 (format "%s#+BEGIN_%s\n%s%s#+END_%s\n"
3159 ind-str block contents ind-str block
))))
3163 (let ((org-inhibit-startup t
)
3166 (org-export--inclusion-absolute-lines
3167 file location only-contents lines
)
3170 (insert (org-export--prepare-file-contents
3171 file lines ind minlevel
3172 (or (gethash file file-prefix
)
3173 (puthash file
(incf current-prefix
) file-prefix
))
3175 (org-export-expand-include-keyword
3176 (cons (list file lines
) included
)
3177 (file-name-directory file
)
3180 ;; Expand footnotes after all files have been
3181 ;; included. Footnotes are stored at end of buffer.
3183 (org-with-wide-buffer
3184 (goto-char (point-max))
3185 (maphash (lambda (ref def
) (insert (format "\n[%s] %s\n" ref def
)))
3186 footnotes
)))))))))))
3188 (defun org-export--inclusion-absolute-lines (file location only-contents lines
)
3189 "Resolve absolute lines for an included file with file-link.
3191 FILE is string file-name of the file to include. LOCATION is a
3192 string name within FILE to be included (located via
3193 `org-link-search'). If ONLY-CONTENTS is non-nil only the
3194 contents of the named element will be included, as determined
3195 Org-Element. If LINES is non-nil only those lines are included.
3197 Return a string of lines to be included in the format expected by
3198 `org-export--prepare-file-contents'."
3200 (insert-file-contents file
)
3201 (unless (eq major-mode
'org-mode
)
3202 (let ((org-inhibit-startup t
)) (org-mode)))
3204 ;; Enforce consistent search.
3205 (let ((org-link-search-must-match-exact-headline t
))
3206 (org-link-search location
))
3208 (error (format "%s for %s::%s" (error-message-string err
) file location
))))
3209 (let* ((element (org-element-at-point))
3211 (and only-contents
(org-element-property :contents-begin element
))))
3213 (or contents-begin
(org-element-property :begin element
))
3214 (org-element-property (if contents-begin
:contents-end
:end
) element
))
3215 (when (and only-contents
3216 (memq (org-element-type element
) '(headline inlinetask
)))
3217 ;; Skip planning line and property-drawer.
3218 (goto-char (point-min))
3219 (when (org-looking-at-p org-planning-line-re
) (forward-line))
3220 (when (looking-at org-property-drawer-re
) (goto-char (match-end 0)))
3221 (unless (bolp) (forward-line))
3222 (narrow-to-region (point) (point-max))))
3224 (org-skip-whitespace)
3226 (let* ((lines (split-string lines
"-"))
3227 (lbeg (string-to-number (car lines
)))
3228 (lend (string-to-number (cadr lines
)))
3229 (beg (if (zerop lbeg
) (point-min)
3230 (goto-char (point-min))
3231 (forward-line (1- lbeg
))
3233 (end (if (zerop lend
) (point-max)
3235 (forward-line (1- lend
))
3237 (narrow-to-region beg end
)))
3238 (let ((end (point-max)))
3239 (goto-char (point-min))
3241 (let ((start-line (line-number-at-pos)))
3247 (while (< (point) end
) (incf counter
) (forward-line))
3250 (defun org-export--update-footnote-label (ref-begin digit-label id
)
3251 "Prefix footnote-label at point REF-BEGIN in buffer with ID.
3253 REF-BEGIN corresponds to the property `:begin' of objects of type
3254 footnote-definition and footnote-reference.
3256 If DIGIT-LABEL is non-nil the label is assumed to be of the form
3257 \[N] where N is one or more numbers.
3259 Return the new label."
3260 (goto-char (1+ ref-begin
))
3261 (buffer-substring (point)
3263 (if digit-label
(insert (format "fn:%d-" id
))
3265 (insert (format "%d-" id
)))
3266 (1- (search-forward "]")))))
3268 (defun org-export--prepare-file-contents (file &optional lines ind minlevel id footnotes
)
3269 "Prepare contents of FILE for inclusion and return it as a string.
3271 When optional argument LINES is a string specifying a range of
3272 lines, include only those lines.
3274 Optional argument IND, when non-nil, is an integer specifying the
3275 global indentation of returned contents. Since its purpose is to
3276 allow an included file to stay in the same environment it was
3277 created \(i.e. a list item), it doesn't apply past the first
3278 headline encountered.
3280 Optional argument MINLEVEL, when non-nil, is an integer
3281 specifying the level that any top-level headline in the included
3283 Optional argument ID is an integer that will be inserted before
3284 each footnote definition and reference if FILE is an Org file.
3285 This is useful to avoid conflicts when more than one Org file
3286 with footnotes is included in a document.
3288 Optional argument FOOTNOTES is a hash-table to store footnotes in
3289 the included document.
3292 (insert-file-contents file
)
3294 (let* ((lines (split-string lines
"-"))
3295 (lbeg (string-to-number (car lines
)))
3296 (lend (string-to-number (cadr lines
)))
3297 (beg (if (zerop lbeg
) (point-min)
3298 (goto-char (point-min))
3299 (forward-line (1- lbeg
))
3301 (end (if (zerop lend
) (point-max)
3302 (goto-char (point-min))
3303 (forward-line (1- lend
))
3305 (narrow-to-region beg end
)))
3306 ;; Remove blank lines at beginning and end of contents. The logic
3307 ;; behind that removal is that blank lines around include keyword
3308 ;; override blank lines in included file.
3309 (goto-char (point-min))
3310 (org-skip-whitespace)
3312 (delete-region (point-min) (point))
3313 (goto-char (point-max))
3314 (skip-chars-backward " \r\t\n")
3316 (delete-region (point) (point-max))
3317 ;; If IND is set, preserve indentation of include keyword until
3318 ;; the first headline encountered.
3320 (unless (eq major-mode
'org-mode
)
3321 (let ((org-inhibit-startup t
)) (org-mode)))
3322 (goto-char (point-min))
3323 (let ((ind-str (make-string ind ?
)))
3324 (while (not (or (eobp) (looking-at org-outline-regexp-bol
)))
3325 ;; Do not move footnote definitions out of column 0.
3326 (unless (and (looking-at org-footnote-definition-re
)
3327 (eq (org-element-type (org-element-at-point))
3328 'footnote-definition
))
3331 ;; When MINLEVEL is specified, compute minimal level for headlines
3332 ;; in the file (CUR-MIN), and remove stars to each headline so
3333 ;; that headlines with minimal level have a level of MINLEVEL.
3335 (unless (eq major-mode
'org-mode
)
3336 (let ((org-inhibit-startup t
)) (org-mode)))
3337 (org-with-limited-levels
3338 (let ((levels (org-map-entries
3339 (lambda () (org-reduced-level (org-current-level))))))
3341 (let ((offset (- minlevel
(apply 'min levels
))))
3342 (unless (zerop offset
)
3343 (when org-odd-levels-only
(setq offset
(* offset
2)))
3344 ;; Only change stars, don't bother moving whole
3347 (lambda () (if (< offset
0) (delete-char (abs offset
))
3348 (insert (make-string offset ?
*)))))))))))
3349 ;; Append ID to all footnote references and definitions, so they
3350 ;; become file specific and cannot collide with footnotes in other
3351 ;; included files. Further, collect relevant footnotes outside of
3354 (let ((marker-min (point-min-marker))
3355 (marker-max (point-max-marker)))
3356 (goto-char (point-min))
3357 (while (re-search-forward org-footnote-re nil t
)
3358 (let ((reference (org-element-context)))
3359 (when (eq (org-element-type reference
) 'footnote-reference
)
3360 (let* ((label (org-element-property :label reference
))
3361 (digit-label (and label
(org-string-match-p "\\`[0-9]+\\'" label
))))
3362 ;; Update the footnote-reference at point and collect
3363 ;; the new label, which is only used for footnotes
3366 ;; If label is akin to [1] convert it to [fn:ID-1].
3367 ;; Otherwise add "ID-" after "fn:".
3368 (let ((new-label (org-export--update-footnote-label
3369 (org-element-property :begin reference
) digit-label id
)))
3370 (unless (eq (org-element-property :type reference
) 'inline
)
3371 (org-with-wide-buffer
3372 (let* ((definition (org-footnote-get-definition label
))
3373 (beginning (nth 1 definition
)))
3375 (error "Definition not found for footnote %s in file %s" label file
))
3376 (if (or (< beginning marker-min
) (> beginning marker-max
))
3377 ;; Store since footnote-definition is outside of LINES.
3379 (org-element-normalize-string (nth 3 definition
))
3381 ;; Update label of definition since it is included directly.
3382 (org-export--update-footnote-label beginning digit-label id
)))))))))))
3383 (set-marker marker-min nil
)
3384 (set-marker marker-max nil
)))
3385 (org-element-normalize-string (buffer-string))))
3387 (defun org-export-execute-babel-code ()
3388 "Execute every Babel code in the visible part of current buffer."
3389 ;; Get a pristine copy of current buffer so Babel references can be
3390 ;; properly resolved.
3391 (let ((reference (org-export-copy-buffer)))
3392 (unwind-protect (org-babel-exp-process-buffer reference
)
3393 (kill-buffer reference
))))
3395 (defun org-export--copy-to-kill-ring-p ()
3396 "Return a non-nil value when output should be added to the kill ring.
3397 See also `org-export-copy-to-kill-ring'."
3398 (if (eq org-export-copy-to-kill-ring
'if-interactive
)
3399 (not (or executing-kbd-macro noninteractive
))
3400 (eq org-export-copy-to-kill-ring t
)))
3404 ;;; Tools For Back-Ends
3406 ;; A whole set of tools is available to help build new exporters. Any
3407 ;; function general enough to have its use across many back-ends
3408 ;; should be added here.
3410 ;;;; For Affiliated Keywords
3412 ;; `org-export-read-attribute' reads a property from a given element
3413 ;; as a plist. It can be used to normalize affiliated keywords'
3416 ;; Since captions can span over multiple lines and accept dual values,
3417 ;; their internal representation is a bit tricky. Therefore,
3418 ;; `org-export-get-caption' transparently returns a given element's
3419 ;; caption as a secondary string.
3421 (defun org-export-read-attribute (attribute element
&optional property
)
3422 "Turn ATTRIBUTE property from ELEMENT into a plist.
3424 When optional argument PROPERTY is non-nil, return the value of
3425 that property within attributes.
3427 This function assumes attributes are defined as \":keyword
3428 value\" pairs. It is appropriate for `:attr_html' like
3431 All values will become strings except the empty string and
3432 \"nil\", which will become nil. Also, values containing only
3433 double quotes will be read as-is, which means that \"\" value
3434 will become the empty string."
3435 (let* ((prepare-value
3438 (cond ((member str
'(nil "" "nil")) nil
)
3439 ((string-match "^\"\\(\"+\\)?\"$" str
)
3440 (or (match-string 1 str
) ""))
3443 (let ((value (org-element-property attribute element
)))
3445 (let ((s (mapconcat 'identity value
" ")) result
)
3446 (while (string-match
3447 "\\(?:^\\|[ \t]+\\)\\(:[-a-zA-Z0-9_]+\\)\\([ \t]+\\|$\\)"
3449 (let ((value (substring s
0 (match-beginning 0))))
3450 (push (funcall prepare-value value
) result
))
3451 (push (intern (match-string 1 s
)) result
)
3452 (setq s
(substring s
(match-end 0))))
3453 ;; Ignore any string before first property with `cdr'.
3454 (cdr (nreverse (cons (funcall prepare-value s
) result
))))))))
3455 (if property
(plist-get attributes property
) attributes
)))
3457 (defun org-export-get-caption (element &optional shortp
)
3458 "Return caption from ELEMENT as a secondary string.
3460 When optional argument SHORTP is non-nil, return short caption,
3461 as a secondary string, instead.
3463 Caption lines are separated by a white space."
3464 (let ((full-caption (org-element-property :caption element
)) caption
)
3465 (dolist (line full-caption
(cdr caption
))
3466 (let ((cap (funcall (if shortp
'cdr
'car
) line
)))
3468 (setq caption
(nconc (list " ") (copy-sequence cap
) caption
)))))))
3471 ;;;; For Derived Back-ends
3473 ;; `org-export-with-backend' is a function allowing to locally use
3474 ;; another back-end to transcode some object or element. In a derived
3475 ;; back-end, it may be used as a fall-back function once all specific
3476 ;; cases have been treated.
3478 (defun org-export-with-backend (backend data
&optional contents info
)
3479 "Call a transcoder from BACKEND on DATA.
3480 BACKEND is an export back-end, as returned by, e.g.,
3481 `org-export-create-backend', or a symbol referring to
3482 a registered back-end. DATA is an Org element, object, secondary
3483 string or string. CONTENTS, when non-nil, is the transcoded
3484 contents of DATA element, as a string. INFO, when non-nil, is
3485 the communication channel used for export, as a plist."
3486 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
3487 (org-export-barf-if-invalid-backend backend
)
3488 (let ((type (org-element-type data
)))
3489 (if (memq type
'(nil org-data
)) (error "No foreign transcoder available")
3490 (let* ((all-transcoders (org-export-get-all-transcoders backend
))
3491 (transcoder (cdr (assq type all-transcoders
))))
3492 (if (not (functionp transcoder
))
3493 (error "No foreign transcoder available")
3495 transcoder data contents
3497 info
(list :back-end backend
3498 :translate-alist all-transcoders
3499 :exported-data
(make-hash-table :test
'eq
:size
401)))))))))
3502 ;;;; For Export Snippets
3504 ;; Every export snippet is transmitted to the back-end. Though, the
3505 ;; latter will only retain one type of export-snippet, ignoring
3506 ;; others, based on the former's target back-end. The function
3507 ;; `org-export-snippet-backend' returns that back-end for a given
3510 (defun org-export-snippet-backend (export-snippet)
3511 "Return EXPORT-SNIPPET targeted back-end as a symbol.
3512 Translation, with `org-export-snippet-translation-alist', is
3514 (let ((back-end (org-element-property :back-end export-snippet
)))
3516 (or (cdr (assoc back-end org-export-snippet-translation-alist
))
3522 ;; `org-export-collect-footnote-definitions' is a tool to list
3523 ;; actually used footnotes definitions in the whole parse tree, or in
3524 ;; a headline, in order to add footnote listings throughout the
3527 ;; `org-export-footnote-first-reference-p' is a predicate used by some
3528 ;; back-ends, when they need to attach the footnote definition only to
3529 ;; the first occurrence of the corresponding label.
3531 ;; `org-export-get-footnote-definition' and
3532 ;; `org-export-get-footnote-number' provide easier access to
3533 ;; additional information relative to a footnote reference.
3535 (defun org-export-collect-footnote-definitions (data info
)
3536 "Return an alist between footnote numbers, labels and definitions.
3538 DATA is the parse tree from which definitions are collected.
3539 INFO is the plist used as a communication channel.
3541 Definitions are sorted by order of references. They either
3542 appear as Org data or as a secondary string for inlined
3543 footnotes. Unreferenced definitions are ignored."
3545 collect-fn
; for byte-compiler.
3549 ;; Collect footnote number, label and definition in DATA.
3550 (org-element-map data
'footnote-reference
3552 (when (org-export-footnote-first-reference-p fn info
)
3553 (let ((def (org-export-get-footnote-definition fn info
)))
3555 (list (org-export-get-footnote-number fn info
)
3556 (org-element-property :label fn
)
3559 ;; Also search in definition for nested footnotes.
3560 (when (eq (org-element-property :type fn
) 'standard
)
3561 (funcall collect-fn def
)))))
3562 ;; Don't enter footnote definitions since it will happen
3563 ;; when their first reference is found.
3564 info nil
'footnote-definition
)))))
3565 (funcall collect-fn
(plist-get info
:parse-tree
))
3566 (reverse num-alist
)))
3568 (defun org-export-footnote-first-reference-p (footnote-reference info
)
3569 "Non-nil when a footnote reference is the first one for its label.
3571 FOOTNOTE-REFERENCE is the footnote reference being considered.
3572 INFO is the plist used as a communication channel."
3573 (let ((label (org-element-property :label footnote-reference
)))
3574 ;; Anonymous footnotes are always a first reference.
3576 ;; Otherwise, return the first footnote with the same LABEL and
3577 ;; test if it is equal to FOOTNOTE-REFERENCE.
3578 (let* (search-refs ; for byte-compiler.
3582 (org-element-map data
'footnote-reference
3585 ((string= (org-element-property :label fn
) label
)
3587 ;; If FN isn't inlined, be sure to traverse its
3588 ;; definition before resuming search. See
3589 ;; comments in `org-export-get-footnote-number'
3590 ;; for more information.
3591 ((eq (org-element-property :type fn
) 'standard
)
3592 (funcall search-refs
3593 (org-export-get-footnote-definition fn info
)))))
3594 ;; Don't enter footnote definitions since it will
3595 ;; happen when their first reference is found.
3596 info
'first-match
'footnote-definition
)))))
3597 (eq (catch 'exit
(funcall search-refs
(plist-get info
:parse-tree
)))
3598 footnote-reference
)))))
3600 (defun org-export-get-footnote-definition (footnote-reference info
)
3601 "Return definition of FOOTNOTE-REFERENCE as parsed data.
3602 INFO is the plist used as a communication channel. If no such
3603 definition can be found, raise an error."
3604 (let ((label (org-element-property :label footnote-reference
)))
3606 (cdr (assoc label
(plist-get info
:footnote-definition-alist
)))
3607 (org-element-contents footnote-reference
))
3608 (error "Definition not found for footnote %s" label
))))
3610 (defun org-export-get-footnote-number (footnote info
)
3611 "Return number associated to a footnote.
3613 FOOTNOTE is either a footnote reference or a footnote definition.
3614 INFO is the plist used as a communication channel."
3615 (let* ((label (org-element-property :label footnote
))
3617 search-ref
; For byte-compiler.
3621 ;; Search footnote references through DATA, filling
3622 ;; SEEN-REFS along the way.
3623 (org-element-map data
'footnote-reference
3625 (let ((fn-lbl (org-element-property :label fn
)))
3627 ;; Anonymous footnote match: return number.
3628 ((and (not fn-lbl
) (eq fn footnote
))
3629 (throw 'exit
(1+ (length seen-refs
))))
3630 ;; Labels match: return number.
3631 ((and label
(string= label fn-lbl
))
3632 (throw 'exit
(1+ (length seen-refs
))))
3633 ;; Anonymous footnote: it's always a new one.
3634 ;; Also, be sure to return nil from the `cond' so
3635 ;; `first-match' doesn't get us out of the loop.
3636 ((not fn-lbl
) (push 'inline seen-refs
) nil
)
3637 ;; Label not seen so far: add it so SEEN-REFS.
3639 ;; Also search for subsequent references in
3640 ;; footnote definition so numbering follows
3641 ;; reading logic. Note that we don't have to care
3642 ;; about inline definitions, since
3643 ;; `org-element-map' already traverses them at the
3646 ;; Once again, return nil to stay in the loop.
3647 ((not (member fn-lbl seen-refs
))
3648 (push fn-lbl seen-refs
)
3650 (org-export-get-footnote-definition fn info
))
3652 ;; Don't enter footnote definitions since it will
3653 ;; happen when their first reference is found.
3654 info
'first-match
'footnote-definition
)))))
3655 (catch 'exit
(funcall search-ref
(plist-get info
:parse-tree
)))))
3660 ;; `org-export-get-relative-level' is a shortcut to get headline
3661 ;; level, relatively to the lower headline level in the parsed tree.
3663 ;; `org-export-get-headline-number' returns the section number of an
3664 ;; headline, while `org-export-number-to-roman' allows to convert it
3665 ;; to roman numbers. With an optional argument,
3666 ;; `org-export-get-headline-number' returns a number to unnumbered
3667 ;; headlines (used for internal id).
3669 ;; `org-export-get-headline-id' returns the unique internal id of a
3672 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
3673 ;; `org-export-last-sibling-p' are three useful predicates when it
3674 ;; comes to fulfill the `:headline-levels' property.
3676 ;; `org-export-get-tags', `org-export-get-category' and
3677 ;; `org-export-get-node-property' extract useful information from an
3678 ;; headline or a parent headline. They all handle inheritance.
3680 ;; `org-export-get-alt-title' tries to retrieve an alternative title,
3681 ;; as a secondary string, suitable for table of contents. It falls
3682 ;; back onto default title.
3684 (defun org-export-get-relative-level (headline info
)
3685 "Return HEADLINE relative level within current parsed tree.
3686 INFO is a plist holding contextual information."
3687 (+ (org-element-property :level headline
)
3688 (or (plist-get info
:headline-offset
) 0)))
3690 (defun org-export-low-level-p (headline info
)
3691 "Non-nil when HEADLINE is considered as low level.
3693 INFO is a plist used as a communication channel.
3695 A low level headlines has a relative level greater than
3696 `:headline-levels' property value.
3698 Return value is the difference between HEADLINE relative level
3699 and the last level being considered as high enough, or nil."
3700 (let ((limit (plist-get info
:headline-levels
)))
3701 (when (wholenump limit
)
3702 (let ((level (org-export-get-relative-level headline info
)))
3703 (and (> level limit
) (- level limit
))))))
3705 (defun org-export-get-headline-id (headline info
)
3706 "Return a unique ID for HEADLINE.
3707 INFO is a plist holding contextual information."
3708 (let ((numbered (org-export-numbered-headline-p headline info
)))
3710 (if numbered
"sec-" "unnumbered-")
3711 (mapconcat #'number-to-string
3713 (org-export-get-headline-number headline info
)
3714 (cdr (assq headline
(plist-get info
:unnumbered-headline-id
)))) "-"))))
3716 (defun org-export-get-headline-number (headline info
)
3717 "Return numbered HEADLINE numbering as a list of numbers.
3718 INFO is a plist holding contextual information."
3719 (and (org-export-numbered-headline-p headline info
)
3720 (cdr (assq headline
(plist-get info
:headline-numbering
)))))
3722 (defun org-export-numbered-headline-p (headline info
)
3723 "Return a non-nil value if HEADLINE element should be numbered.
3724 INFO is a plist used as a communication channel."
3726 (lambda (head) (org-not-nil (org-element-property :UNNUMBERED head
)))
3727 (org-element-lineage headline nil t
))
3728 (let ((sec-num (plist-get info
:section-numbers
))
3729 (level (org-export-get-relative-level headline info
)))
3730 (if (wholenump sec-num
) (<= level sec-num
) sec-num
))))
3732 (defun org-export-number-to-roman (n)
3733 "Convert integer N into a roman numeral."
3734 (let ((roman '((1000 .
"M") (900 .
"CM") (500 .
"D") (400 .
"CD")
3735 ( 100 .
"C") ( 90 .
"XC") ( 50 .
"L") ( 40 .
"XL")
3736 ( 10 .
"X") ( 9 .
"IX") ( 5 .
"V") ( 4 .
"IV")
3740 (number-to-string n
)
3742 (if (>= n
(caar roman
))
3743 (setq n
(- n
(caar roman
))
3744 res
(concat res
(cdar roman
)))
3748 (defun org-export-get-tags (element info
&optional tags inherited
)
3749 "Return list of tags associated to ELEMENT.
3751 ELEMENT has either an `headline' or an `inlinetask' type. INFO
3752 is a plist used as a communication channel.
3754 Select tags (see `org-export-select-tags') and exclude tags (see
3755 `org-export-exclude-tags') are removed from the list.
3757 When non-nil, optional argument TAGS should be a list of strings.
3758 Any tag belonging to this list will also be removed.
3760 When optional argument INHERITED is non-nil, tags can also be
3761 inherited from parent headlines and FILETAGS keywords."
3763 (lambda (tag) (or (member tag
(plist-get info
:select-tags
))
3764 (member tag
(plist-get info
:exclude-tags
))
3766 (if (not inherited
) (org-element-property :tags element
)
3767 ;; Build complete list of inherited tags.
3768 (let ((current-tag-list (org-element-property :tags element
)))
3769 (dolist (parent (org-element-lineage element
))
3770 (dolist (tag (org-element-property :tags parent
))
3771 (when (and (memq (org-element-type parent
) '(headline inlinetask
))
3772 (not (member tag current-tag-list
)))
3773 (push tag current-tag-list
))))
3774 ;; Add FILETAGS keywords and return results.
3775 (org-uniquify (append (plist-get info
:filetags
) current-tag-list
))))))
3777 (defun org-export-get-node-property (property blob
&optional inherited
)
3778 "Return node PROPERTY value for BLOB.
3780 PROPERTY is an upcase symbol (i.e. `:COOKIE_DATA'). BLOB is an
3783 If optional argument INHERITED is non-nil, the value can be
3784 inherited from a parent headline.
3786 Return value is a string or nil."
3787 (let ((headline (if (eq (org-element-type blob
) 'headline
) blob
3788 (org-export-get-parent-headline blob
))))
3789 (if (not inherited
) (org-element-property property blob
)
3790 (let ((parent headline
) value
)
3793 (when (plist-member (nth 1 parent
) property
)
3794 (throw 'found
(org-element-property property parent
)))
3795 (setq parent
(org-element-property :parent parent
))))))))
3797 (defun org-export-get-category (blob info
)
3798 "Return category for element or object BLOB.
3800 INFO is a plist used as a communication channel.
3802 CATEGORY is automatically inherited from a parent headline, from
3803 #+CATEGORY: keyword or created out of original file name. If all
3804 fail, the fall-back value is \"???\"."
3805 (or (org-export-get-node-property :CATEGORY blob t
)
3806 (org-element-map (plist-get info
:parse-tree
) 'keyword
3808 (when (equal (org-element-property :key kwd
) "CATEGORY")
3809 (org-element-property :value kwd
)))
3811 (let ((file (plist-get info
:input-file
)))
3812 (and file
(file-name-sans-extension (file-name-nondirectory file
))))
3815 (defun org-export-get-alt-title (headline info
)
3816 "Return alternative title for HEADLINE, as a secondary string.
3817 INFO is a plist used as a communication channel. If no optional
3818 title is defined, fall-back to the regular title."
3819 (or (org-element-property :alt-title headline
)
3820 (org-element-property :title headline
)))
3822 (defun org-export-first-sibling-p (blob info
)
3823 "Non-nil when BLOB is the first sibling in its parent.
3824 BLOB is an element or an object. If BLOB is a headline, non-nil
3825 means it is the first sibling in the sub-tree. INFO is a plist
3826 used as a communication channel."
3827 (memq (org-element-type (org-export-get-previous-element blob info
))
3830 (defun org-export-last-sibling-p (blob info
)
3831 "Non-nil when BLOB is the last sibling in its parent.
3832 BLOB is an element or an object. INFO is a plist used as
3833 a communication channel."
3834 (not (org-export-get-next-element blob info
)))
3839 ;; `org-export-get-date' returns a date appropriate for the document
3840 ;; to about to be exported. In particular, it takes care of
3841 ;; `org-export-date-timestamp-format'.
3843 (defun org-export-get-date (info &optional fmt
)
3844 "Return date value for the current document.
3846 INFO is a plist used as a communication channel. FMT, when
3847 non-nil, is a time format string that will be applied on the date
3848 if it consists in a single timestamp object. It defaults to
3849 `org-export-date-timestamp-format' when nil.
3851 A proper date can be a secondary string, a string or nil. It is
3852 meant to be translated with `org-export-data' or alike."
3853 (let ((date (plist-get info
:date
))
3854 (fmt (or fmt org-export-date-timestamp-format
)))
3855 (cond ((not date
) nil
)
3858 (eq (org-element-type (car date
)) 'timestamp
))
3859 (org-timestamp-format (car date
) fmt
))
3865 ;; `org-export-custom-protocol-maybe' handles custom protocol defined
3866 ;; with `org-add-link-type', which see.
3868 ;; `org-export-solidify-link-text' turns a string into a safer version
3869 ;; for links, replacing most non-standard characters with hyphens.
3871 ;; `org-export-get-coderef-format' returns an appropriate format
3872 ;; string for coderefs.
3874 ;; `org-export-inline-image-p' returns a non-nil value when the link
3875 ;; provided should be considered as an inline image.
3877 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
3878 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
3879 ;; returns an appropriate unique identifier when found, or nil.
3881 ;; `org-export-resolve-id-link' returns the first headline with
3882 ;; specified id or custom-id in parse tree, the path to the external
3883 ;; file with the id or nil when neither was found.
3885 ;; `org-export-resolve-coderef' associates a reference to a line
3886 ;; number in the element it belongs, or returns the reference itself
3887 ;; when the element isn't numbered.
3889 (defun org-export-solidify-link-text (s)
3890 "Take link text S and make a safe target out of it."
3892 (mapconcat 'identity
(org-split-string s
"[^a-zA-Z0-9_.-:]+") "-")))
3894 (defun org-export-custom-protocol-maybe (link desc info
)
3895 "Try exporting LINK with a dedicated function.
3897 DESC is its description, as a string, or nil. INFO is the plist
3898 containing export state. Return output as a string, or nil if no
3899 protocol handles LINK.
3901 A custom protocol is expected to have precedence over regular
3902 back-end export. The function ignores links with an implicit
3903 type (e.g., \"custom-id\")."
3904 (let ((type (org-element-property :type link
))
3905 (backend (let ((b (plist-get info
:back-end
)))
3906 (and b
(org-export-backend-name b
)))))
3907 (unless (or (member type
'("coderef" "custom-id" "fuzzy" "radio"))
3909 (let ((protocol (nth 2 (assoc type org-link-protocols
))))
3910 (and (functionp protocol
)
3912 (org-link-unescape (org-element-property :path link
))
3916 (defun org-export-get-coderef-format (path desc
)
3917 "Return format string for code reference link.
3918 PATH is the link path. DESC is its description."
3920 (cond ((not desc
) "%s")
3921 ((string-match (regexp-quote (concat "(" path
")")) desc
)
3922 (replace-match "%s" t t desc
))
3925 (defun org-export-inline-image-p (link &optional rules
)
3926 "Non-nil if LINK object points to an inline image.
3928 Optional argument is a set of RULES defining inline images. It
3929 is an alist where associations have the following shape:
3933 Applying a rule means apply REGEXP against LINK's path when its
3934 type is TYPE. The function will return a non-nil value if any of
3935 the provided rules is non-nil. The default rule is
3936 `org-export-default-inline-image-rule'.
3938 This only applies to links without a description."
3939 (and (not (org-element-contents link
))
3940 (let ((case-fold-search t
))
3942 (dolist (rule (or rules org-export-default-inline-image-rule
))
3943 (and (string= (org-element-property :type link
) (car rule
))
3944 (org-string-match-p (cdr rule
)
3945 (org-element-property :path link
))
3946 (throw 'exit t
)))))))
3948 (defun org-export-resolve-coderef (ref info
)
3949 "Resolve a code reference REF.
3951 INFO is a plist used as a communication channel.
3953 Return associated line number in source code, or REF itself,
3954 depending on src-block or example element's switches."
3955 (org-element-map (plist-get info
:parse-tree
) '(example-block src-block
)
3958 (insert (org-trim (org-element-property :value el
)))
3959 (let* ((label-fmt (regexp-quote
3960 (or (org-element-property :label-fmt el
)
3961 org-coderef-label-format
)))
3963 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
3964 (replace-regexp-in-string "%s" ref label-fmt nil t
))))
3965 ;; Element containing REF is found. Resolve it to either
3966 ;; a label or a line number, as needed.
3967 (when (re-search-backward ref-re nil t
)
3969 ((org-element-property :use-labels el
) ref
)
3970 ((eq (org-element-property :number-lines el
) 'continued
)
3971 (+ (org-export-get-loc el info
) (line-number-at-pos)))
3972 (t (line-number-at-pos)))))))
3975 (defun org-export-resolve-fuzzy-link (link info
)
3976 "Return LINK destination.
3978 INFO is a plist holding contextual information.
3980 Return value can be an object, an element, or nil:
3982 - If LINK path matches a target object (i.e. <<path>>) return it.
3984 - If LINK path exactly matches the name affiliated keyword
3985 \(i.e. #+NAME: path) of an element, return that element.
3987 - If LINK path exactly matches any headline name, return that
3988 element. If more than one headline share that name, priority
3989 will be given to the one with the closest common ancestor, if
3990 any, or the first one in the parse tree otherwise.
3992 - Otherwise, return nil.
3994 Assume LINK type is \"fuzzy\". White spaces are not
3996 (let* ((raw-path (org-element-property :path link
))
3997 (match-title-p (eq (aref raw-path
0) ?
*))
3998 ;; Split PATH at white spaces so matches are space
4000 (path (org-split-string
4001 (if match-title-p
(substring raw-path
1) raw-path
)))
4002 ;; Cache for destinations that are not position dependent.
4004 (or (plist-get info
:resolve-fuzzy-link-cache
)
4005 (plist-get (setq info
(plist-put info
:resolve-fuzzy-link-cache
4006 (make-hash-table :test
'equal
)))
4007 :resolve-fuzzy-link-cache
)))
4008 (cached (gethash path link-cache
'not-found
)))
4010 ;; Destination is not position dependent: use cached value.
4011 ((and (not match-title-p
) (not (eq cached
'not-found
))) cached
)
4012 ;; First try to find a matching "<<path>>" unless user specified
4013 ;; he was looking for a headline (path starts with a "*"
4015 ((and (not match-title-p
)
4016 (let ((match (org-element-map (plist-get info
:parse-tree
) 'target
4018 (and (equal (org-split-string
4019 (org-element-property :value blob
))
4022 info
'first-match
)))
4023 (and match
(puthash path match link-cache
)))))
4024 ;; Then try to find an element with a matching "#+NAME: path"
4025 ;; affiliated keyword.
4026 ((and (not match-title-p
)
4027 (let ((match (org-element-map (plist-get info
:parse-tree
)
4028 org-element-all-elements
4030 (let ((name (org-element-property :name el
)))
4032 (equal (org-split-string name
) path
))
4034 info
'first-match
)))
4035 (and match
(puthash path match link-cache
)))))
4036 ;; Last case: link either points to a headline or to nothingness.
4037 ;; Try to find the source, with priority given to headlines with
4038 ;; the closest common ancestor. If such candidate is found,
4039 ;; return it, otherwise return nil.
4041 (let ((find-headline
4043 ;; Return first headline whose `:raw-value' property is
4044 ;; NAME in parse tree DATA, or nil. Statistics cookies
4047 (org-element-map data
'headline
4049 (when (equal (org-split-string
4050 (replace-regexp-in-string
4051 "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
4052 (org-element-property :raw-value headline
)))
4055 info
'first-match
)))))
4056 ;; Search among headlines sharing an ancestor with link, from
4057 ;; closest to farthest.
4060 (let ((parent-hl (org-export-get-parent-headline link
)))
4061 (if (not parent-hl
) (list (plist-get info
:parse-tree
))
4062 (org-element-lineage parent-hl nil t
))))
4063 (let ((foundp (funcall find-headline path parent
)))
4064 (when foundp
(throw 'exit foundp
))))
4065 ;; No destination found: return nil.
4066 (and (not match-title-p
) (puthash path nil link-cache
))))))))
4068 (defun org-export-resolve-id-link (link info
)
4069 "Return headline referenced as LINK destination.
4071 INFO is a plist used as a communication channel.
4073 Return value can be the headline element matched in current parse
4074 tree, a file name or nil. Assume LINK type is either \"id\" or
4076 (let ((id (org-element-property :path link
)))
4077 ;; First check if id is within the current parse tree.
4078 (or (org-element-map (plist-get info
:parse-tree
) 'headline
4080 (when (or (string= (org-element-property :ID headline
) id
)
4081 (string= (org-element-property :CUSTOM_ID headline
) id
))
4084 ;; Otherwise, look for external files.
4085 (cdr (assoc id
(plist-get info
:id-alist
))))))
4087 (defun org-export-resolve-radio-link (link info
)
4088 "Return radio-target object referenced as LINK destination.
4090 INFO is a plist used as a communication channel.
4092 Return value can be a radio-target object or nil. Assume LINK
4093 has type \"radio\"."
4094 (let ((path (replace-regexp-in-string
4095 "[ \r\t\n]+" " " (org-element-property :path link
))))
4096 (org-element-map (plist-get info
:parse-tree
) 'radio-target
4098 (and (eq (compare-strings
4099 (replace-regexp-in-string
4100 "[ \r\t\n]+" " " (org-element-property :value radio
))
4101 nil nil path nil nil t
)
4104 info
'first-match
)))
4109 ;; `org-export-get-ordinal' associates a sequence number to any object
4112 (defun org-export-get-ordinal (element info
&optional types predicate
)
4113 "Return ordinal number of an element or object.
4115 ELEMENT is the element or object considered. INFO is the plist
4116 used as a communication channel.
4118 Optional argument TYPES, when non-nil, is a list of element or
4119 object types, as symbols, that should also be counted in.
4120 Otherwise, only provided element's type is considered.
4122 Optional argument PREDICATE is a function returning a non-nil
4123 value if the current element or object should be counted in. It
4124 accepts two arguments: the element or object being considered and
4125 the plist used as a communication channel. This allows to count
4126 only a certain type of objects (i.e. inline images).
4128 Return value is a list of numbers if ELEMENT is a headline or an
4129 item. It is nil for keywords. It represents the footnote number
4130 for footnote definitions and footnote references. If ELEMENT is
4131 a target, return the same value as if ELEMENT was the closest
4132 table, item or headline containing the target. In any other
4133 case, return the sequence number of ELEMENT among elements or
4134 objects of the same type."
4135 ;; Ordinal of a target object refer to the ordinal of the closest
4136 ;; table, item, or headline containing the object.
4137 (when (eq (org-element-type element
) 'target
)
4139 (org-element-lineage
4141 '(footnote-definition footnote-reference headline item table
))))
4142 (case (org-element-type element
)
4143 ;; Special case 1: A headline returns its number as a list.
4144 (headline (org-export-get-headline-number element info
))
4145 ;; Special case 2: An item returns its number as a list.
4146 (item (let ((struct (org-element-property :structure element
)))
4147 (org-list-get-item-number
4148 (org-element-property :begin element
)
4150 (org-list-prevs-alist struct
)
4151 (org-list-parents-alist struct
))))
4152 ((footnote-definition footnote-reference
)
4153 (org-export-get-footnote-number element info
))
4156 ;; Increment counter until ELEMENT is found again.
4157 (org-element-map (plist-get info
:parse-tree
)
4158 (or types
(org-element-type element
))
4161 ((eq element el
) (1+ counter
))
4162 ((not predicate
) (incf counter
) nil
)
4163 ((funcall predicate el info
) (incf counter
) nil
)))
4164 info
'first-match
)))))
4169 ;; `org-export-get-loc' counts number of code lines accumulated in
4170 ;; src-block or example-block elements with a "+n" switch until
4171 ;; a given element, excluded. Note: "-n" switches reset that count.
4173 ;; `org-export-unravel-code' extracts source code (along with a code
4174 ;; references alist) from an `element-block' or `src-block' type
4177 ;; `org-export-format-code' applies a formatting function to each line
4178 ;; of code, providing relative line number and code reference when
4179 ;; appropriate. Since it doesn't access the original element from
4180 ;; which the source code is coming, it expects from the code calling
4181 ;; it to know if lines should be numbered and if code references
4184 ;; Eventually, `org-export-format-code-default' is a higher-level
4185 ;; function (it makes use of the two previous functions) which handles
4186 ;; line numbering and code references inclusion, and returns source
4187 ;; code in a format suitable for plain text or verbatim output.
4189 (defun org-export-get-loc (element info
)
4190 "Return accumulated lines of code up to ELEMENT.
4192 INFO is the plist used as a communication channel.
4194 ELEMENT is excluded from count."
4196 (org-element-map (plist-get info
:parse-tree
)
4197 `(src-block example-block
,(org-element-type element
))
4200 ;; ELEMENT is reached: Quit the loop.
4202 ;; Only count lines from src-block and example-block elements
4203 ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
4204 ((not (memq (org-element-type el
) '(src-block example-block
))) nil
)
4205 ((let ((linums (org-element-property :number-lines el
)))
4207 ;; Accumulate locs or reset them.
4208 (let ((lines (org-count-lines
4209 (org-trim (org-element-property :value el
)))))
4210 (setq loc
(if (eq linums
'new
) lines
(+ loc lines
))))))
4211 ;; Return nil to stay in the loop.
4217 (defun org-export-unravel-code (element)
4218 "Clean source code and extract references out of it.
4220 ELEMENT has either a `src-block' an `example-block' type.
4222 Return a cons cell whose CAR is the source code, cleaned from any
4223 reference, protective commas and spurious indentation, and CDR is
4224 an alist between relative line number (integer) and name of code
4225 reference on that line (string)."
4226 (let* ((line 0) refs
4227 (value (org-element-property :value element
))
4228 ;; Get code and clean it. Remove blank lines at its
4229 ;; beginning and end.
4230 (code (replace-regexp-in-string
4231 "\\`\\([ \t]*\n\\)+" ""
4232 (replace-regexp-in-string
4233 "\\([ \t]*\n\\)*[ \t]*\\'" "\n"
4234 (if (or org-src-preserve-indentation
4235 (org-element-property :preserve-indent element
))
4237 (org-element-remove-indentation value
)))))
4238 ;; Get format used for references.
4239 (label-fmt (regexp-quote
4240 (or (org-element-property :label-fmt element
)
4241 org-coderef-label-format
)))
4242 ;; Build a regexp matching a loc with a reference.
4244 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
4245 (replace-regexp-in-string
4246 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t
))))
4249 ;; Code with references removed.
4250 (org-element-normalize-string
4254 (if (not (string-match with-ref-re loc
)) loc
4255 ;; Ref line: remove ref, and signal its position in REFS.
4256 (push (cons line
(match-string 3 loc
)) refs
)
4257 (replace-match "" nil nil loc
1)))
4258 (org-split-string code
"\n") "\n"))
4262 (defun org-export-format-code (code fun
&optional num-lines ref-alist
)
4263 "Format CODE by applying FUN line-wise and return it.
4265 CODE is a string representing the code to format. FUN is
4266 a function. It must accept three arguments: a line of
4267 code (string), the current line number (integer) or nil and the
4268 reference associated to the current line (string) or nil.
4270 Optional argument NUM-LINES can be an integer representing the
4271 number of code lines accumulated until the current code. Line
4272 numbers passed to FUN will take it into account. If it is nil,
4273 FUN's second argument will always be nil. This number can be
4274 obtained with `org-export-get-loc' function.
4276 Optional argument REF-ALIST can be an alist between relative line
4277 number (i.e. ignoring NUM-LINES) and the name of the code
4278 reference on it. If it is nil, FUN's third argument will always
4279 be nil. It can be obtained through the use of
4280 `org-export-unravel-code' function."
4281 (let ((--locs (org-split-string code
"\n"))
4283 (org-element-normalize-string
4287 (let ((--ref (cdr (assq --line ref-alist
))))
4288 (funcall fun --loc
(and num-lines
(+ num-lines --line
)) --ref
)))
4291 (defun org-export-format-code-default (element info
)
4292 "Return source code from ELEMENT, formatted in a standard way.
4294 ELEMENT is either a `src-block' or `example-block' element. INFO
4295 is a plist used as a communication channel.
4297 This function takes care of line numbering and code references
4298 inclusion. Line numbers, when applicable, appear at the
4299 beginning of the line, separated from the code by two white
4300 spaces. Code references, on the other hand, appear flushed to
4301 the right, separated by six white spaces from the widest line of
4303 ;; Extract code and references.
4304 (let* ((code-info (org-export-unravel-code element
))
4305 (code (car code-info
))
4306 (code-lines (org-split-string code
"\n")))
4307 (if (null code-lines
) ""
4308 (let* ((refs (and (org-element-property :retain-labels element
)
4310 ;; Handle line numbering.
4311 (num-start (case (org-element-property :number-lines element
)
4312 (continued (org-export-get-loc element info
))
4317 (length (number-to-string
4318 (+ (length code-lines
) num-start
))))))
4319 ;; Prepare references display, if required. Any reference
4320 ;; should start six columns after the widest line of code,
4321 ;; wrapped with parenthesis.
4323 (+ (apply 'max
(mapcar 'length code-lines
))
4324 (if (not num-start
) 0 (length (format num-fmt num-start
))))))
4325 (org-export-format-code
4327 (lambda (loc line-num ref
)
4328 (let ((number-str (and num-fmt
(format num-fmt line-num
))))
4333 (concat (make-string
4335 (+ (length loc
) (length number-str
))) ?
)
4336 (format "(%s)" ref
))))))
4342 ;; `org-export-table-has-special-column-p' and and
4343 ;; `org-export-table-row-is-special-p' are predicates used to look for
4344 ;; meta-information about the table structure.
4346 ;; `org-table-has-header-p' tells when the rows before the first rule
4347 ;; should be considered as table's header.
4349 ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
4350 ;; and `org-export-table-cell-borders' extract information from
4351 ;; a table-cell element.
4353 ;; `org-export-table-dimensions' gives the number on rows and columns
4354 ;; in the table, ignoring horizontal rules and special columns.
4355 ;; `org-export-table-cell-address', given a table-cell object, returns
4356 ;; the absolute address of a cell. On the other hand,
4357 ;; `org-export-get-table-cell-at' does the contrary.
4359 ;; `org-export-table-cell-starts-colgroup-p',
4360 ;; `org-export-table-cell-ends-colgroup-p',
4361 ;; `org-export-table-row-starts-rowgroup-p',
4362 ;; `org-export-table-row-ends-rowgroup-p',
4363 ;; `org-export-table-row-starts-header-p',
4364 ;; `org-export-table-row-ends-header-p' and
4365 ;; `org-export-table-row-in-header-p' indicate position of current row
4366 ;; or cell within the table.
4368 (defun org-export-table-has-special-column-p (table)
4369 "Non-nil when TABLE has a special column.
4370 All special columns will be ignored during export."
4371 ;; The table has a special column when every first cell of every row
4372 ;; has an empty value or contains a symbol among "/", "#", "!", "$",
4373 ;; "*" "_" and "^". Though, do not consider a first row containing
4374 ;; only empty cells as special.
4375 (let ((special-column-p 'empty
))
4379 (when (eq (org-element-property :type row
) 'standard
)
4380 (let ((value (org-element-contents
4381 (car (org-element-contents row
)))))
4382 (cond ((member value
'(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
4383 (setq special-column-p
'special
))
4385 (t (throw 'exit nil
))))))
4386 (org-element-contents table
))
4387 (eq special-column-p
'special
))))
4389 (defun org-export-table-has-header-p (table info
)
4390 "Non-nil when TABLE has a header.
4392 INFO is a plist used as a communication channel.
4394 A table has a header when it contains at least two row groups."
4395 (let ((cache (or (plist-get info
:table-header-cache
)
4396 (plist-get (setq info
4397 (plist-put info
:table-header-cache
4398 (make-hash-table :test
'eq
)))
4399 :table-header-cache
))))
4400 (or (gethash table cache
)
4401 (let ((rowgroup 1) row-flag
)
4404 (org-element-map table
'table-row
4408 ((and row-flag
(eq (org-element-property :type row
) 'rule
))
4409 (incf rowgroup
) (setq row-flag nil
))
4410 ((and (not row-flag
) (eq (org-element-property :type row
)
4412 (setq row-flag t
) nil
)))
4416 (defun org-export-table-row-is-special-p (table-row info
)
4417 "Non-nil if TABLE-ROW is considered special.
4419 INFO is a plist used as the communication channel.
4421 All special rows will be ignored during export."
4422 (when (eq (org-element-property :type table-row
) 'standard
)
4423 (let ((first-cell (org-element-contents
4424 (car (org-element-contents table-row
)))))
4425 ;; A row is special either when...
4427 ;; ... it starts with a field only containing "/",
4428 (equal first-cell
'("/"))
4429 ;; ... the table contains a special column and the row start
4430 ;; with a marking character among, "^", "_", "$" or "!",
4431 (and (org-export-table-has-special-column-p
4432 (org-export-get-parent table-row
))
4433 (member first-cell
'(("^") ("_") ("$") ("!"))))
4434 ;; ... it contains only alignment cookies and empty cells.
4435 (let ((special-row-p 'empty
))
4439 (let ((value (org-element-contents cell
)))
4440 ;; Since VALUE is a secondary string, the following
4441 ;; checks avoid expanding it with `org-export-data'.
4443 ((and (not (cdr value
))
4444 (stringp (car value
))
4445 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
4447 (setq special-row-p
'cookie
))
4448 (t (throw 'exit nil
)))))
4449 (org-element-contents table-row
))
4450 (eq special-row-p
'cookie
)))))))
4452 (defun org-export-table-row-group (table-row info
)
4453 "Return TABLE-ROW's group number, as an integer.
4455 INFO is a plist used as the communication channel.
4457 Return value is the group number, as an integer, or nil for
4458 special rows and rows separators. First group is also table's
4460 (let ((cache (or (plist-get info
:table-row-group-cache
)
4461 (plist-get (setq info
4462 (plist-put info
:table-row-group-cache
4463 (make-hash-table :test
'eq
)))
4464 :table-row-group-cache
))))
4465 (cond ((gethash table-row cache
))
4466 ((eq (org-element-property :type table-row
) 'rule
) nil
)
4467 (t (let ((group 0) row-flag
)
4468 (org-element-map (org-export-get-parent table-row
) 'table-row
4470 (if (eq (org-element-property :type row
) 'rule
)
4472 (unless row-flag
(incf group
) (setq row-flag t
)))
4473 (when (eq table-row row
) (puthash table-row group cache
)))
4474 info
'first-match
))))))
4476 (defun org-export-table-cell-width (table-cell info
)
4477 "Return TABLE-CELL contents width.
4479 INFO is a plist used as the communication channel.
4481 Return value is the width given by the last width cookie in the
4482 same column as TABLE-CELL, or nil."
4483 (let* ((row (org-export-get-parent table-cell
))
4484 (table (org-export-get-parent row
))
4485 (cells (org-element-contents row
))
4486 (columns (length cells
))
4487 (column (- columns
(length (memq table-cell cells
))))
4488 (cache (or (plist-get info
:table-cell-width-cache
)
4489 (plist-get (setq info
4490 (plist-put info
:table-cell-width-cache
4491 (make-hash-table :test
'eq
)))
4492 :table-cell-width-cache
)))
4493 (width-vector (or (gethash table cache
)
4494 (puthash table
(make-vector columns
'empty
) cache
)))
4495 (value (aref width-vector column
)))
4496 (if (not (eq value
'empty
)) value
4498 (dolist (row (org-element-contents table
)
4499 (aset width-vector column cookie-width
))
4500 (when (org-export-table-row-is-special-p row info
)
4501 ;; In a special row, try to find a width cookie at COLUMN.
4502 (let* ((value (org-element-contents
4503 (elt (org-element-contents row
) column
)))
4504 (cookie (car value
)))
4505 ;; The following checks avoid expanding unnecessarily
4506 ;; the cell with `org-export-data'.
4510 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" cookie
)
4511 (match-string 1 cookie
))
4513 (string-to-number (match-string 1 cookie
)))))))))))
4515 (defun org-export-table-cell-alignment (table-cell info
)
4516 "Return TABLE-CELL contents alignment.
4518 INFO is a plist used as the communication channel.
4520 Return alignment as specified by the last alignment cookie in the
4521 same column as TABLE-CELL. If no such cookie is found, a default
4522 alignment value will be deduced from fraction of numbers in the
4523 column (see `org-table-number-fraction' for more information).
4524 Possible values are `left', `right' and `center'."
4525 ;; Load `org-table-number-fraction' and `org-table-number-regexp'.
4526 (require 'org-table
)
4527 (let* ((row (org-export-get-parent table-cell
))
4528 (table (org-export-get-parent row
))
4529 (cells (org-element-contents row
))
4530 (columns (length cells
))
4531 (column (- columns
(length (memq table-cell cells
))))
4532 (cache (or (plist-get info
:table-cell-alignment-cache
)
4533 (plist-get (setq info
4534 (plist-put info
:table-cell-alignment-cache
4535 (make-hash-table :test
'eq
)))
4536 :table-cell-alignment-cache
)))
4537 (align-vector (or (gethash table cache
)
4538 (puthash table
(make-vector columns nil
) cache
))))
4539 (or (aref align-vector column
)
4540 (let ((number-cells 0)
4543 previous-cell-number-p
)
4544 (dolist (row (org-element-contents (org-export-get-parent row
)))
4546 ;; In a special row, try to find an alignment cookie at
4548 ((org-export-table-row-is-special-p row info
)
4549 (let ((value (org-element-contents
4550 (elt (org-element-contents row
) column
))))
4551 ;; Since VALUE is a secondary string, the following
4552 ;; checks avoid useless expansion through
4553 ;; `org-export-data'.
4556 (stringp (car value
))
4557 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
4559 (match-string 1 (car value
)))
4560 (setq cookie-align
(match-string 1 (car value
))))))
4561 ;; Ignore table rules.
4562 ((eq (org-element-property :type row
) 'rule
))
4563 ;; In a standard row, check if cell's contents are
4564 ;; expressing some kind of number. Increase NUMBER-CELLS
4565 ;; accordingly. Though, don't bother if an alignment
4566 ;; cookie has already defined cell's alignment.
4568 (let ((value (org-export-data
4569 (org-element-contents
4570 (elt (org-element-contents row
) column
))
4573 ;; Treat an empty cell as a number if it follows
4575 (if (not (or (string-match org-table-number-regexp value
)
4576 (and (string= value
"") previous-cell-number-p
)))
4577 (setq previous-cell-number-p nil
)
4578 (setq previous-cell-number-p t
)
4579 (incf number-cells
))))))
4580 ;; Return value. Alignment specified by cookies has
4581 ;; precedence over alignment deduced from cell's contents.
4584 (cond ((equal cookie-align
"l") 'left
)
4585 ((equal cookie-align
"r") 'right
)
4586 ((equal cookie-align
"c") 'center
)
4587 ((>= (/ (float number-cells
) total-cells
)
4588 org-table-number-fraction
)
4592 (defun org-export-table-cell-borders (table-cell info
)
4593 "Return TABLE-CELL borders.
4595 INFO is a plist used as a communication channel.
4597 Return value is a list of symbols, or nil. Possible values are:
4598 `top', `bottom', `above', `below', `left' and `right'. Note:
4599 `top' (resp. `bottom') only happen for a cell in the first
4600 row (resp. last row) of the table, ignoring table rules, if any.
4602 Returned borders ignore special rows."
4603 (let* ((row (org-export-get-parent table-cell
))
4604 (table (org-export-get-parent-table table-cell
))
4606 ;; Top/above border? TABLE-CELL has a border above when a rule
4607 ;; used to demarcate row groups can be found above. Hence,
4608 ;; finding a rule isn't sufficient to push `above' in BORDERS:
4609 ;; another regular row has to be found above that rule.
4613 (cond ((eq (org-element-property :type row
) 'rule
)
4615 ((not (org-export-table-row-is-special-p row info
))
4616 (if rule-flag
(throw 'exit
(push 'above borders
))
4617 (throw 'exit nil
)))))
4618 ;; Look at every row before the current one.
4619 (cdr (memq row
(reverse (org-element-contents table
)))))
4620 ;; No rule above, or rule found starts the table (ignoring any
4621 ;; special row): TABLE-CELL is at the top of the table.
4622 (when rule-flag
(push 'above borders
))
4623 (push 'top borders
)))
4624 ;; Bottom/below border? TABLE-CELL has a border below when next
4625 ;; non-regular row below is a rule.
4629 (cond ((eq (org-element-property :type row
) 'rule
)
4631 ((not (org-export-table-row-is-special-p row info
))
4632 (if rule-flag
(throw 'exit
(push 'below borders
))
4633 (throw 'exit nil
)))))
4634 ;; Look at every row after the current one.
4635 (cdr (memq row
(org-element-contents table
))))
4636 ;; No rule below, or rule found ends the table (modulo some
4637 ;; special row): TABLE-CELL is at the bottom of the table.
4638 (when rule-flag
(push 'below borders
))
4639 (push 'bottom borders
)))
4640 ;; Right/left borders? They can only be specified by column
4641 ;; groups. Column groups are defined in a row starting with "/".
4642 ;; Also a column groups row only contains "<", "<>", ">" or blank
4645 (let ((column (let ((cells (org-element-contents row
)))
4646 (- (length cells
) (length (memq table-cell cells
))))))
4649 (unless (eq (org-element-property :type row
) 'rule
)
4650 (when (equal (org-element-contents
4651 (car (org-element-contents row
)))
4653 (let ((column-groups
4656 (let ((value (org-element-contents cell
)))
4657 (when (member value
'(("<") ("<>") (">") nil
))
4659 (org-element-contents row
))))
4660 ;; There's a left border when previous cell, if
4661 ;; any, ends a group, or current one starts one.
4662 (when (or (and (not (zerop column
))
4663 (member (elt column-groups
(1- column
))
4665 (member (elt column-groups column
) '("<" "<>")))
4666 (push 'left borders
))
4667 ;; There's a right border when next cell, if any,
4668 ;; starts a group, or current one ends one.
4669 (when (or (and (/= (1+ column
) (length column-groups
))
4670 (member (elt column-groups
(1+ column
))
4672 (member (elt column-groups column
) '(">" "<>")))
4673 (push 'right borders
))
4674 (throw 'exit nil
)))))
4675 ;; Table rows are read in reverse order so last column groups
4676 ;; row has precedence over any previous one.
4677 (reverse (org-element-contents table
)))))
4681 (defun org-export-table-cell-starts-colgroup-p (table-cell info
)
4682 "Non-nil when TABLE-CELL is at the beginning of a column group.
4683 INFO is a plist used as a communication channel."
4684 ;; A cell starts a column group either when it is at the beginning
4685 ;; of a row (or after the special column, if any) or when it has
4687 (or (eq (org-element-map (org-export-get-parent table-cell
) 'table-cell
4688 'identity info
'first-match
)
4690 (memq 'left
(org-export-table-cell-borders table-cell info
))))
4692 (defun org-export-table-cell-ends-colgroup-p (table-cell info
)
4693 "Non-nil when TABLE-CELL is at the end of a column group.
4694 INFO is a plist used as a communication channel."
4695 ;; A cell ends a column group either when it is at the end of a row
4696 ;; or when it has a right border.
4697 (or (eq (car (last (org-element-contents
4698 (org-export-get-parent table-cell
))))
4700 (memq 'right
(org-export-table-cell-borders table-cell info
))))
4702 (defun org-export-table-row-starts-rowgroup-p (table-row info
)
4703 "Non-nil when TABLE-ROW is at the beginning of a row group.
4704 INFO is a plist used as a communication channel."
4705 (unless (or (eq (org-element-property :type table-row
) 'rule
)
4706 (org-export-table-row-is-special-p table-row info
))
4707 (let ((borders (org-export-table-cell-borders
4708 (car (org-element-contents table-row
)) info
)))
4709 (or (memq 'top borders
) (memq 'above borders
)))))
4711 (defun org-export-table-row-ends-rowgroup-p (table-row info
)
4712 "Non-nil when TABLE-ROW is at the end of a row group.
4713 INFO is a plist used as a communication channel."
4714 (unless (or (eq (org-element-property :type table-row
) 'rule
)
4715 (org-export-table-row-is-special-p table-row info
))
4716 (let ((borders (org-export-table-cell-borders
4717 (car (org-element-contents table-row
)) info
)))
4718 (or (memq 'bottom borders
) (memq 'below borders
)))))
4720 (defun org-export-table-row-in-header-p (table-row info
)
4721 "Non-nil when TABLE-ROW is located within table's header.
4722 INFO is a plist used as a communication channel. Always return
4723 nil for special rows and rows separators."
4724 (and (org-export-table-has-header-p
4725 (org-export-get-parent-table table-row
) info
)
4726 (eql (org-export-table-row-group table-row info
) 1)))
4728 (defun org-export-table-row-starts-header-p (table-row info
)
4729 "Non-nil when TABLE-ROW is the first table header's row.
4730 INFO is a plist used as a communication channel."
4731 (and (org-export-table-row-in-header-p table-row info
)
4732 (org-export-table-row-starts-rowgroup-p table-row info
)))
4734 (defun org-export-table-row-ends-header-p (table-row info
)
4735 "Non-nil when TABLE-ROW is the last table header's row.
4736 INFO is a plist used as a communication channel."
4737 (and (org-export-table-row-in-header-p table-row info
)
4738 (org-export-table-row-ends-rowgroup-p table-row info
)))
4740 (defun org-export-table-row-number (table-row info
)
4741 "Return TABLE-ROW number.
4742 INFO is a plist used as a communication channel. Return value is
4743 zero-based and ignores separators. The function returns nil for
4744 special columns and separators."
4745 (when (and (eq (org-element-property :type table-row
) 'standard
)
4746 (not (org-export-table-row-is-special-p table-row info
)))
4748 (org-element-map (org-export-get-parent-table table-row
) 'table-row
4750 (cond ((eq row table-row
) number
)
4751 ((eq (org-element-property :type row
) 'standard
)
4752 (incf number
) nil
)))
4753 info
'first-match
))))
4755 (defun org-export-table-dimensions (table info
)
4756 "Return TABLE dimensions.
4758 INFO is a plist used as a communication channel.
4760 Return value is a CONS like (ROWS . COLUMNS) where
4761 ROWS (resp. COLUMNS) is the number of exportable
4762 rows (resp. columns)."
4763 (let (first-row (columns 0) (rows 0))
4764 ;; Set number of rows, and extract first one.
4765 (org-element-map table
'table-row
4767 (when (eq (org-element-property :type row
) 'standard
)
4769 (unless first-row
(setq first-row row
)))) info
)
4770 ;; Set number of columns.
4771 (org-element-map first-row
'table-cell
(lambda (cell) (incf columns
)) info
)
4773 (cons rows columns
)))
4775 (defun org-export-table-cell-address (table-cell info
)
4776 "Return address of a regular TABLE-CELL object.
4778 TABLE-CELL is the cell considered. INFO is a plist used as
4779 a communication channel.
4781 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4782 zero-based index. Only exportable cells are considered. The
4783 function returns nil for other cells."
4784 (let* ((table-row (org-export-get-parent table-cell
))
4785 (row-number (org-export-table-row-number table-row info
)))
4788 (let ((col-count 0))
4789 (org-element-map table-row
'table-cell
4791 (if (eq cell table-cell
) col-count
(incf col-count
) nil
))
4792 info
'first-match
))))))
4794 (defun org-export-get-table-cell-at (address table info
)
4795 "Return regular table-cell object at ADDRESS in TABLE.
4797 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4798 zero-based index. TABLE is a table type element. INFO is
4799 a plist used as a communication channel.
4801 If no table-cell, among exportable cells, is found at ADDRESS,
4803 (let ((column-pos (cdr address
)) (column-count 0))
4805 ;; Row at (car address) or nil.
4806 (let ((row-pos (car address
)) (row-count 0))
4807 (org-element-map table
'table-row
4809 (cond ((eq (org-element-property :type row
) 'rule
) nil
)
4810 ((= row-count row-pos
) row
)
4811 (t (incf row-count
) nil
)))
4815 (if (= column-count column-pos
) cell
4816 (incf column-count
) nil
))
4817 info
'first-match
)))
4820 ;;;; For Tables Of Contents
4822 ;; `org-export-collect-headlines' builds a list of all exportable
4823 ;; headline elements, maybe limited to a certain depth. One can then
4824 ;; easily parse it and transcode it.
4826 ;; Building lists of tables, figures or listings is quite similar.
4827 ;; Once the generic function `org-export-collect-elements' is defined,
4828 ;; `org-export-collect-tables', `org-export-collect-figures' and
4829 ;; `org-export-collect-listings' can be derived from it.
4831 (defun org-export-collect-headlines (info &optional n scope
)
4832 "Collect headlines in order to build a table of contents.
4834 INFO is a plist used as a communication channel.
4836 When optional argument N is an integer, it specifies the depth of
4837 the table of contents. Otherwise, it is set to the value of the
4838 last headline level. See `org-export-headline-levels' for more
4841 Optional argument SCOPE, when non-nil, is an element. If it is
4842 a headline, only children of SCOPE are collected. Otherwise,
4843 collect children of the headline containing provided element. If
4844 there is no such headline, collect all headlines. In any case,
4845 argument N becomes relative to the level of that headline.
4847 Return a list of all exportable headlines as parsed elements.
4848 Footnote sections are ignored."
4849 (let* ((scope (cond ((not scope
) (plist-get info
:parse-tree
))
4850 ((eq (org-element-type scope
) 'headline
) scope
)
4851 ((org-export-get-parent-headline scope
))
4852 (t (plist-get info
:parse-tree
))))
4853 (limit (plist-get info
:headline-levels
))
4854 (n (if (not (wholenump n
)) limit
4855 (min (if (eq (org-element-type scope
) 'org-data
) n
4856 (+ (org-export-get-relative-level scope info
) n
))
4858 (org-element-map (org-element-contents scope
) 'headline
4860 (unless (org-element-property :footnote-section-p headline
)
4861 (let ((level (org-export-get-relative-level headline info
)))
4862 (and (<= level n
) headline
))))
4865 (defun org-export-collect-elements (type info
&optional predicate
)
4866 "Collect referenceable elements of a determined type.
4868 TYPE can be a symbol or a list of symbols specifying element
4869 types to search. Only elements with a caption are collected.
4871 INFO is a plist used as a communication channel.
4873 When non-nil, optional argument PREDICATE is a function accepting
4874 one argument, an element of type TYPE. It returns a non-nil
4875 value when that element should be collected.
4877 Return a list of all elements found, in order of appearance."
4878 (org-element-map (plist-get info
:parse-tree
) type
4880 (and (org-element-property :caption element
)
4881 (or (not predicate
) (funcall predicate element
))
4885 (defun org-export-collect-tables (info)
4886 "Build a list of tables.
4887 INFO is a plist used as a communication channel.
4889 Return a list of table elements with a caption."
4890 (org-export-collect-elements 'table info
))
4892 (defun org-export-collect-figures (info predicate
)
4893 "Build a list of figures.
4895 INFO is a plist used as a communication channel. PREDICATE is
4896 a function which accepts one argument: a paragraph element and
4897 whose return value is non-nil when that element should be
4900 A figure is a paragraph type element, with a caption, verifying
4901 PREDICATE. The latter has to be provided since a \"figure\" is
4902 a vague concept that may depend on back-end.
4904 Return a list of elements recognized as figures."
4905 (org-export-collect-elements 'paragraph info predicate
))
4907 (defun org-export-collect-listings (info)
4908 "Build a list of src blocks.
4910 INFO is a plist used as a communication channel.
4912 Return a list of src-block elements with a caption."
4913 (org-export-collect-elements 'src-block info
))
4918 ;; The main function for the smart quotes sub-system is
4919 ;; `org-export-activate-smart-quotes', which replaces every quote in
4920 ;; a given string from the parse tree with its "smart" counterpart.
4922 ;; Dictionary for smart quotes is stored in
4923 ;; `org-export-smart-quotes-alist'.
4925 ;; Internally, regexps matching potential smart quotes (checks at
4926 ;; string boundaries are also necessary) are defined in
4927 ;; `org-export-smart-quotes-regexps'.
4929 (defconst org-export-smart-quotes-alist
4931 ;; one may use: »...«, "...", ›...‹, or '...'.
4932 ;; http://sproget.dk/raad-og-regler/retskrivningsregler/retskrivningsregler/a7-40-60/a7-58-anforselstegn/
4933 ;; LaTeX quotes require Babel!
4934 (opening-double-quote :utf-8
"»" :html
"»" :latex
">>"
4935 :texinfo
"@guillemetright{}")
4936 (closing-double-quote :utf-8
"«" :html
"«" :latex
"<<"
4937 :texinfo
"@guillemetleft{}")
4938 (opening-single-quote :utf-8
"›" :html
"›" :latex
"\\frq{}"
4939 :texinfo
"@guilsinglright{}")
4940 (closing-single-quote :utf-8
"‹" :html
"‹" :latex
"\\flq{}"
4941 :texinfo
"@guilsingleft{}")
4942 (apostrophe :utf-8
"’" :html
"’"))
4944 (opening-double-quote :utf-8
"„" :html
"„" :latex
"\"`"
4945 :texinfo
"@quotedblbase{}")
4946 (closing-double-quote :utf-8
"“" :html
"“" :latex
"\"'"
4947 :texinfo
"@quotedblleft{}")
4948 (opening-single-quote :utf-8
"‚" :html
"‚" :latex
"\\glq{}"
4949 :texinfo
"@quotesinglbase{}")
4950 (closing-single-quote :utf-8
"‘" :html
"‘" :latex
"\\grq{}"
4951 :texinfo
"@quoteleft{}")
4952 (apostrophe :utf-8
"’" :html
"’"))
4954 (opening-double-quote :utf-8
"“" :html
"“" :latex
"``" :texinfo
"``")
4955 (closing-double-quote :utf-8
"”" :html
"”" :latex
"''" :texinfo
"''")
4956 (opening-single-quote :utf-8
"‘" :html
"‘" :latex
"`" :texinfo
"`")
4957 (closing-single-quote :utf-8
"’" :html
"’" :latex
"'" :texinfo
"'")
4958 (apostrophe :utf-8
"’" :html
"’"))
4960 (opening-double-quote :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
4961 :texinfo
"@guillemetleft{}")
4962 (closing-double-quote :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
4963 :texinfo
"@guillemetright{}")
4964 (opening-single-quote :utf-8
"“" :html
"“" :latex
"``" :texinfo
"``")
4965 (closing-single-quote :utf-8
"”" :html
"”" :latex
"''" :texinfo
"''")
4966 (apostrophe :utf-8
"’" :html
"’"))
4968 (opening-double-quote :utf-8
"« " :html
"« " :latex
"\\og "
4969 :texinfo
"@guillemetleft{}@tie{}")
4970 (closing-double-quote :utf-8
" »" :html
" »" :latex
"\\fg{}"
4971 :texinfo
"@tie{}@guillemetright{}")
4972 (opening-single-quote :utf-8
"« " :html
"« " :latex
"\\og "
4973 :texinfo
"@guillemetleft{}@tie{}")
4974 (closing-single-quote :utf-8
" »" :html
" »" :latex
"\\fg{}"
4975 :texinfo
"@tie{}@guillemetright{}")
4976 (apostrophe :utf-8
"’" :html
"’"))
4978 ;; https://nn.wikipedia.org/wiki/Sitatteikn
4979 (opening-double-quote :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
4980 :texinfo
"@guillemetleft{}")
4981 (closing-double-quote :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
4982 :texinfo
"@guillemetright{}")
4983 (opening-single-quote :utf-8
"‘" :html
"‘" :latex
"`" :texinfo
"`")
4984 (closing-single-quote :utf-8
"’" :html
"’" :latex
"'" :texinfo
"'")
4985 (apostrophe :utf-8
"’" :html
"’"))
4987 ;; https://nn.wikipedia.org/wiki/Sitatteikn
4988 (opening-double-quote :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
4989 :texinfo
"@guillemetleft{}")
4990 (closing-double-quote :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
4991 :texinfo
"@guillemetright{}")
4992 (opening-single-quote :utf-8
"‘" :html
"‘" :latex
"`" :texinfo
"`")
4993 (closing-single-quote :utf-8
"’" :html
"’" :latex
"'" :texinfo
"'")
4994 (apostrophe :utf-8
"’" :html
"’"))
4996 ;; https://nn.wikipedia.org/wiki/Sitatteikn
4997 (opening-double-quote :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
4998 :texinfo
"@guillemetleft{}")
4999 (closing-double-quote :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
5000 :texinfo
"@guillemetright{}")
5001 (opening-single-quote :utf-8
"‘" :html
"‘" :latex
"`" :texinfo
"`")
5002 (closing-single-quote :utf-8
"’" :html
"’" :latex
"'" :texinfo
"'")
5003 (apostrophe :utf-8
"’" :html
"’"))
5005 ;; 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
5006 ;; http://www.artlebedev.ru/kovodstvo/sections/104/
5007 (opening-double-quote :utf-8
"«" :html
"«" :latex
"{}<<"
5008 :texinfo
"@guillemetleft{}")
5009 (closing-double-quote :utf-8
"»" :html
"»" :latex
">>{}"
5010 :texinfo
"@guillemetright{}")
5011 (opening-single-quote :utf-8
"„" :html
"„" :latex
"\\glqq{}"
5012 :texinfo
"@quotedblbase{}")
5013 (closing-single-quote :utf-8
"“" :html
"“" :latex
"\\grqq{}"
5014 :texinfo
"@quotedblleft{}")
5015 (apostrophe :utf-8
"’" :html
: "'"))
5017 ;; based on https://sv.wikipedia.org/wiki/Citattecken
5018 (opening-double-quote :utf-8
"”" :html
"”" :latex
"’’" :texinfo
"’’")
5019 (closing-double-quote :utf-8
"”" :html
"”" :latex
"’’" :texinfo
"’’")
5020 (opening-single-quote :utf-8
"’" :html
"’" :latex
"’" :texinfo
"`")
5021 (closing-single-quote :utf-8
"’" :html
"’" :latex
"’" :texinfo
"'")
5022 (apostrophe :utf-8
"’" :html
"’"))
5024 "Smart quotes translations.
5026 Alist whose CAR is a language string and CDR is an alist with
5027 quote type as key and a plist associating various encodings to
5028 their translation as value.
5030 A quote type can be any symbol among `opening-double-quote',
5031 `closing-double-quote', `opening-single-quote',
5032 `closing-single-quote' and `apostrophe'.
5034 Valid encodings include `:utf-8', `:html', `:latex' and
5037 If no translation is found, the quote character is left as-is.")
5039 (defconst org-export-smart-quotes-regexps
5041 ;; Possible opening quote at beginning of string.
5042 "\\`\\([\"']\\)\\(\\w\\|\\s.\\|\\s_\\|\\s(\\)"
5043 ;; Possible closing quote at beginning of string.
5044 "\\`\\([\"']\\)\\(\\s-\\|\\s)\\|\\s.\\)"
5045 ;; Possible apostrophe at beginning of string.
5047 ;; Opening single and double quotes.
5048 "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\(?:\\w\\|\\s.\\|\\s_\\)"
5049 ;; Closing single and double quotes.
5050 "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\(?:\\s-\\|\\s)\\|\\s.\\)"
5053 ;; Possible opening quote at end of string.
5054 "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\'"
5055 ;; Possible closing quote at end of string.
5056 "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\'"
5057 ;; Possible apostrophe at end of string.
5059 "List of regexps matching a quote or an apostrophe.
5060 In every regexp, quote or apostrophe matched is put in group 1.")
5062 (defun org-export-activate-smart-quotes (s encoding info
&optional original
)
5063 "Replace regular quotes with \"smart\" quotes in string S.
5065 ENCODING is a symbol among `:html', `:latex', `:texinfo' and
5066 `:utf-8'. INFO is a plist used as a communication channel.
5068 The function has to retrieve information about string
5069 surroundings in parse tree. It can only happen with an
5070 unmodified string. Thus, if S has already been through another
5071 process, a non-nil ORIGINAL optional argument will provide that
5074 Return the new string."
5076 (let* ((prev (org-export-get-previous-element (or original s
) info
))
5077 ;; Try to be flexible when computing number of blanks
5078 ;; before object. The previous object may be a string
5079 ;; introduced by the back-end and not completely parsed.
5080 (pre-blank (and prev
5081 (or (org-element-property :post-blank prev
)
5082 ;; A string with missing `:post-blank'
5085 (string-match " *\\'" prev
)
5086 (length (match-string 0 prev
)))
5089 (next (org-export-get-next-element (or original s
) info
))
5092 ;; Return smart quote associated to a give quote Q, as
5093 ;; a string. TYPE is a symbol among `open', `close' and
5095 (let ((key (case type
5096 (apostrophe 'apostrophe
)
5097 (open (if (equal "'" q
) 'opening-single-quote
5098 'opening-double-quote
))
5099 (otherwise (if (equal "'" q
) 'closing-single-quote
5100 'closing-double-quote
)))))
5103 (cdr (assoc (plist-get info
:language
)
5104 org-export-smart-quotes-alist
))))
5107 (if (or (equal "\"" s
) (equal "'" s
))
5108 ;; Only a quote: no regexp can match. We have to check both
5109 ;; sides and decide what to do.
5110 (cond ((and (not prev
) (not next
)) s
)
5111 ((not prev
) (funcall get-smart-quote s
'open
))
5112 ((and (not next
) (zerop pre-blank
))
5113 (funcall get-smart-quote s
'close
))
5115 ((zerop pre-blank
) (funcall get-smart-quote s
'apostrophe
))
5116 (t (funcall get-smart-quote
'open
)))
5117 ;; 1. Replace quote character at the beginning of S.
5120 ((and prev
(zerop pre-blank
)
5121 (string-match (nth 2 org-export-smart-quotes-regexps
) s
))
5122 (setq s
(replace-match
5123 (funcall get-smart-quote
(match-string 1 s
) 'apostrophe
)
5126 ((and prev
(zerop pre-blank
)
5127 (string-match (nth 1 org-export-smart-quotes-regexps
) s
))
5128 (setq s
(replace-match
5129 (funcall get-smart-quote
(match-string 1 s
) 'close
)
5132 ((and (or (not prev
) (> pre-blank
0))
5133 (string-match (nth 0 org-export-smart-quotes-regexps
) s
))
5134 (setq s
(replace-match
5135 (funcall get-smart-quote
(match-string 1 s
) 'open
)
5137 ;; 2. Replace quotes in the middle of the string.
5138 (setq s
(replace-regexp-in-string
5140 (nth 3 org-export-smart-quotes-regexps
)
5142 (funcall get-smart-quote
(match-string 1 text
) 'open
))
5144 (setq s
(replace-regexp-in-string
5146 (nth 4 org-export-smart-quotes-regexps
)
5148 (funcall get-smart-quote
(match-string 1 text
) 'close
))
5150 (setq s
(replace-regexp-in-string
5152 (nth 5 org-export-smart-quotes-regexps
)
5154 (funcall get-smart-quote
(match-string 1 text
) 'apostrophe
))
5156 ;; 3. Replace quote character at the end of S.
5159 ((and next
(string-match (nth 8 org-export-smart-quotes-regexps
) s
))
5160 (setq s
(replace-match
5161 (funcall get-smart-quote
(match-string 1 s
) 'apostrophe
)
5165 (string-match (nth 7 org-export-smart-quotes-regexps
) s
))
5166 (setq s
(replace-match
5167 (funcall get-smart-quote
(match-string 1 s
) 'close
)
5170 ((and next
(string-match (nth 6 org-export-smart-quotes-regexps
) s
))
5171 (setq s
(replace-match
5172 (funcall get-smart-quote
(match-string 1 s
) 'open
)
5174 ;; Return string with smart quotes.
5179 ;; Here are various functions to retrieve information about the
5180 ;; neighborhood of a given element or object. Neighbors of interest
5181 ;; are direct parent (`org-export-get-parent'), parent headline
5182 ;; (`org-export-get-parent-headline'), first element containing an
5183 ;; object, (`org-export-get-parent-element'), parent table
5184 ;; (`org-export-get-parent-table'), previous element or object
5185 ;; (`org-export-get-previous-element') and next element or object
5186 ;; (`org-export-get-next-element').
5188 ;; defsubst org-export-get-parent must be defined before first use
5190 (define-obsolete-function-alias
5191 'org-export-get-genealogy
'org-element-lineage
"25.1")
5193 (defun org-export-get-parent-headline (blob)
5194 "Return BLOB parent headline or nil.
5195 BLOB is the element or object being considered."
5196 (org-element-lineage blob
'(headline)))
5198 (defun org-export-get-parent-element (object)
5199 "Return first element containing OBJECT or nil.
5200 OBJECT is the object to consider."
5201 (org-element-lineage object org-element-all-elements
))
5203 (defun org-export-get-parent-table (object)
5204 "Return OBJECT parent table or nil.
5205 OBJECT is either a `table-cell' or `table-element' type object."
5206 (org-element-lineage object
'(table)))
5208 (defun org-export-get-previous-element (blob info
&optional n
)
5209 "Return previous element or object.
5211 BLOB is an element or object. INFO is a plist used as
5212 a communication channel. Return previous exportable element or
5213 object, a string, or nil.
5215 When optional argument N is a positive integer, return a list
5216 containing up to N siblings before BLOB, from farthest to
5217 closest. With any other non-nil value, return a list containing
5219 (let* ((secondary (org-element-secondary-p blob
))
5220 (parent (org-export-get-parent blob
))
5222 (if secondary
(org-element-property secondary parent
)
5223 (org-element-contents parent
)))
5226 (dolist (obj (cdr (memq blob
(reverse siblings
))) prev
)
5227 (cond ((memq obj
(plist-get info
:ignore-list
)))
5228 ((null n
) (throw 'exit obj
))
5229 ((not (wholenump n
)) (push obj prev
))
5230 ((zerop n
) (throw 'exit prev
))
5231 (t (decf n
) (push obj prev
)))))))
5233 (defun org-export-get-next-element (blob info
&optional n
)
5234 "Return next element or object.
5236 BLOB is an element or object. INFO is a plist used as
5237 a communication channel. Return next exportable element or
5238 object, a string, or nil.
5240 When optional argument N is a positive integer, return a list
5241 containing up to N siblings after BLOB, from closest to farthest.
5242 With any other non-nil value, return a list containing all of
5244 (let* ((secondary (org-element-secondary-p blob
))
5245 (parent (org-export-get-parent blob
))
5248 (if secondary
(org-element-property secondary parent
)
5249 (org-element-contents parent
)))))
5252 (dolist (obj siblings
(nreverse next
))
5253 (cond ((memq obj
(plist-get info
:ignore-list
)))
5254 ((null n
) (throw 'exit obj
))
5255 ((not (wholenump n
)) (push obj next
))
5256 ((zerop n
) (throw 'exit
(nreverse next
)))
5257 (t (decf n
) (push obj next
)))))))
5262 ;; `org-export-translate' translates a string according to the language
5263 ;; specified by the LANGUAGE keyword. `org-export-dictionary' contains
5264 ;; the dictionary used for the translation.
5266 (defconst org-export-dictionary
5268 ("fr" :default
"%e %n : %c" :html
"%e %n : %c"))
5270 ("ca" :default
"Autor")
5271 ("cs" :default
"Autor")
5272 ("da" :default
"Forfatter")
5273 ("de" :default
"Autor")
5274 ("eo" :html
"Aŭtoro")
5275 ("es" :default
"Autor")
5276 ("et" :default
"Autor")
5277 ("fi" :html
"Tekijä")
5278 ("fr" :default
"Auteur")
5279 ("hu" :default
"Szerzõ")
5280 ("is" :html
"Höfundur")
5281 ("it" :default
"Autore")
5282 ("ja" :default
"著者" :html
"著者")
5283 ("nl" :default
"Auteur")
5284 ("no" :default
"Forfatter")
5285 ("nb" :default
"Forfatter")
5286 ("nn" :default
"Forfattar")
5287 ("pl" :default
"Autor")
5288 ("pt_BR" :default
"Autor")
5289 ("ru" :html
"Автор" :utf-8
"Автор")
5290 ("sv" :html
"Författare")
5291 ("uk" :html
"Автор" :utf-8
"Автор")
5292 ("zh-CN" :html
"作者" :utf-8
"作者")
5293 ("zh-TW" :html
"作者" :utf-8
"作者"))
5294 ("Continued from previous page"
5295 ("de" :default
"Fortsetzung von vorheriger Seite")
5296 ("es" :default
"Continúa de la página anterior")
5297 ("fr" :default
"Suite de la page précédente")
5298 ("it" :default
"Continua da pagina precedente")
5299 ("ja" :default
"前ページからの続き")
5300 ("nl" :default
"Vervolg van vorige pagina")
5301 ("pt" :default
"Continuação da página anterior")
5302 ("ru" :html
"(Продолжение)"
5303 :utf-8
"(Продолжение)"))
5304 ("Continued on next page"
5305 ("de" :default
"Fortsetzung nächste Seite")
5306 ("es" :default
"Continúa en la siguiente página")
5307 ("fr" :default
"Suite page suivante")
5308 ("it" :default
"Continua alla pagina successiva")
5309 ("ja" :default
"次ページに続く")
5310 ("nl" :default
"Vervolg op volgende pagina")
5311 ("pt" :default
"Continua na página seguinte")
5312 ("ru" :html
"(Продолжение следует)"
5313 :utf-8
"(Продолжение следует)"))
5315 ("ca" :default
"Data")
5316 ("cs" :default
"Datum")
5317 ("da" :default
"Dato")
5318 ("de" :default
"Datum")
5319 ("eo" :default
"Dato")
5320 ("es" :default
"Fecha")
5321 ("et" :html
"Kuupäev" :utf-8
"Kuupäev")
5322 ("fi" :html
"Päivämäärä")
5323 ("hu" :html
"Dátum")
5324 ("is" :default
"Dagsetning")
5325 ("it" :default
"Data")
5326 ("ja" :default
"日付" :html
"日付")
5327 ("nl" :default
"Datum")
5328 ("no" :default
"Dato")
5329 ("nb" :default
"Dato")
5330 ("nn" :default
"Dato")
5331 ("pl" :default
"Data")
5332 ("pt_BR" :default
"Data")
5333 ("ru" :html
"Дата" :utf-8
"Дата")
5334 ("sv" :default
"Datum")
5335 ("uk" :html
"Дата" :utf-8
"Дата")
5336 ("zh-CN" :html
"日期" :utf-8
"日期")
5337 ("zh-TW" :html
"日期" :utf-8
"日期"))
5339 ("da" :default
"Ligning")
5340 ("de" :default
"Gleichung")
5341 ("es" :html
"Ecuación" :default
"Ecuación")
5342 ("et" :html
"Võrrand" :utf-8
"Võrrand")
5343 ("fr" :ascii
"Equation" :default
"Équation")
5344 ("ja" :default
"方程式")
5345 ("no" :default
"Ligning")
5346 ("nb" :default
"Ligning")
5347 ("nn" :default
"Likning")
5348 ("pt_BR" :html
"Equação" :default
"Equação" :ascii
"Equacao")
5349 ("ru" :html
"Уравнение"
5351 ("sv" :default
"Ekvation")
5352 ("zh-CN" :html
"方程" :utf-8
"方程"))
5354 ("da" :default
"Figur")
5355 ("de" :default
"Abbildung")
5356 ("es" :default
"Figura")
5357 ("et" :default
"Joonis")
5358 ("ja" :default
"図" :html
"図")
5359 ("no" :default
"Illustrasjon")
5360 ("nb" :default
"Illustrasjon")
5361 ("nn" :default
"Illustrasjon")
5362 ("pt_BR" :default
"Figura")
5363 ("ru" :html
"Рисунок" :utf-8
"Рисунок")
5364 ("sv" :default
"Illustration")
5365 ("zh-CN" :html
"图" :utf-8
"图"))
5367 ("da" :default
"Figur %d")
5368 ("de" :default
"Abbildung %d:")
5369 ("es" :default
"Figura %d:")
5370 ("et" :default
"Joonis %d:")
5371 ("fr" :default
"Figure %d :" :html
"Figure %d :")
5372 ("ja" :default
"図%d: " :html
"図%d: ")
5373 ("no" :default
"Illustrasjon %d")
5374 ("nb" :default
"Illustrasjon %d")
5375 ("nn" :default
"Illustrasjon %d")
5376 ("pt_BR" :default
"Figura %d:")
5377 ("ru" :html
"Рис. %d.:" :utf-8
"Рис. %d.:")
5378 ("sv" :default
"Illustration %d")
5379 ("zh-CN" :html
"图%d " :utf-8
"图%d "))
5381 ("ca" :html
"Peus de pàgina")
5382 ("cs" :default
"Pozn\xe1mky pod carou")
5383 ("da" :default
"Fodnoter")
5384 ("de" :html
"Fußnoten" :default
"Fußnoten")
5385 ("eo" :default
"Piednotoj")
5386 ("es" :html
"Nota al pie de página" :default
"Nota al pie de página")
5387 ("et" :html
"Allmärkused" :utf-8
"Allmärkused")
5388 ("fi" :default
"Alaviitteet")
5389 ("fr" :default
"Notes de bas de page")
5390 ("hu" :html
"Lábjegyzet")
5391 ("is" :html
"Aftanmálsgreinar")
5392 ("it" :html
"Note a piè di pagina")
5393 ("ja" :default
"脚注" :html
"脚注")
5394 ("nl" :default
"Voetnoten")
5395 ("no" :default
"Fotnoter")
5396 ("nb" :default
"Fotnoter")
5397 ("nn" :default
"Fotnotar")
5398 ("pl" :default
"Przypis")
5399 ("pt_BR" :html
"Notas de Rodapé" :default
"Notas de Rodapé" :ascii
"Notas de Rodape")
5400 ("ru" :html
"Сноски" :utf-8
"Сноски")
5401 ("sv" :default
"Fotnoter")
5402 ("uk" :html
"Примітки"
5404 ("zh-CN" :html
"脚注" :utf-8
"脚注")
5405 ("zh-TW" :html
"腳註" :utf-8
"腳註"))
5407 ("da" :default
"Programmer")
5408 ("de" :default
"Programmauflistungsverzeichnis")
5409 ("es" :default
"Indice de Listados de programas")
5410 ("et" :default
"Loendite nimekiri")
5411 ("fr" :default
"Liste des programmes")
5412 ("ja" :default
"ソースコード目次")
5413 ("no" :default
"Dataprogrammer")
5414 ("nb" :default
"Dataprogrammer")
5415 ("ru" :html
"Список распечаток"
5416 :utf-8
"Список распечаток")
5417 ("zh-CN" :html
"代码目录" :utf-8
"代码目录"))
5419 ("da" :default
"Tabeller")
5420 ("de" :default
"Tabellenverzeichnis")
5421 ("es" :default
"Indice de tablas")
5422 ("et" :default
"Tabelite nimekiri")
5423 ("fr" :default
"Liste des tableaux")
5424 ("ja" :default
"表目次")
5425 ("no" :default
"Tabeller")
5426 ("nb" :default
"Tabeller")
5427 ("nn" :default
"Tabeller")
5428 ("pt_BR" :default
"Índice de Tabelas" :ascii
"Indice de Tabelas")
5429 ("ru" :html
"Список таблиц"
5430 :utf-8
"Список таблиц")
5431 ("sv" :default
"Tabeller")
5432 ("zh-CN" :html
"表格目录" :utf-8
"表格目录"))
5434 ("da" :default
"Program %d")
5435 ("de" :default
"Programmlisting %d")
5436 ("es" :default
"Listado de programa %d")
5437 ("et" :default
"Loend %d")
5438 ("fr" :default
"Programme %d :" :html
"Programme %d :")
5439 ("ja" :default
"ソースコード%d:")
5440 ("no" :default
"Dataprogram %d")
5441 ("nb" :default
"Dataprogram %d")
5442 ("pt_BR" :default
"Listagem %d")
5443 ("ru" :html
"Распечатка %d.:"
5444 :utf-8
"Распечатка %d.:")
5445 ("zh-CN" :html
"代码%d " :utf-8
"代码%d "))
5447 ("fr" :ascii
"References" :default
"Références")
5448 ("de" :default
"Quellen"))
5450 ("da" :default
"jævnfør afsnit %s")
5451 ("de" :default
"siehe Abschnitt %s")
5452 ("es" :default
"vea seccion %s")
5453 ("et" :html
"Vaata peatükki %s" :utf-8
"Vaata peatükki %s")
5454 ("fr" :default
"cf. section %s")
5455 ("ja" :default
"セクション %s を参照")
5456 ("pt_BR" :html
"Veja a seção %s" :default
"Veja a seção %s"
5457 :ascii
"Veja a secao %s")
5458 ("ru" :html
"См. раздел %s"
5459 :utf-8
"См. раздел %s")
5460 ("zh-CN" :html
"参见第%s节" :utf-8
"参见第%s节"))
5462 ("de" :default
"Tabelle")
5463 ("es" :default
"Tabla")
5464 ("et" :default
"Tabel")
5465 ("fr" :default
"Tableau")
5466 ("ja" :default
"表" :html
"表")
5467 ("pt_BR" :default
"Tabela")
5468 ("ru" :html
"Таблица"
5470 ("zh-CN" :html
"表" :utf-8
"表"))
5472 ("da" :default
"Tabel %d")
5473 ("de" :default
"Tabelle %d")
5474 ("es" :default
"Tabla %d")
5475 ("et" :default
"Tabel %d")
5476 ("fr" :default
"Tableau %d :")
5477 ("ja" :default
"表%d:" :html
"表%d:")
5478 ("no" :default
"Tabell %d")
5479 ("nb" :default
"Tabell %d")
5480 ("nn" :default
"Tabell %d")
5481 ("pt_BR" :default
"Tabela %d")
5482 ("ru" :html
"Таблица %d.:"
5483 :utf-8
"Таблица %d.:")
5484 ("sv" :default
"Tabell %d")
5485 ("zh-CN" :html
"表%d " :utf-8
"表%d "))
5486 ("Table of Contents"
5487 ("ca" :html
"Índex")
5488 ("cs" :default
"Obsah")
5489 ("da" :default
"Indhold")
5490 ("de" :default
"Inhaltsverzeichnis")
5491 ("eo" :default
"Enhavo")
5492 ("es" :html
"Índice")
5493 ("et" :default
"Sisukord")
5494 ("fi" :html
"Sisällysluettelo")
5495 ("fr" :ascii
"Sommaire" :default
"Table des matières")
5496 ("hu" :html
"Tartalomjegyzék")
5497 ("is" :default
"Efnisyfirlit")
5498 ("it" :default
"Indice")
5499 ("ja" :default
"目次" :html
"目次")
5500 ("nl" :default
"Inhoudsopgave")
5501 ("no" :default
"Innhold")
5502 ("nb" :default
"Innhold")
5503 ("nn" :default
"Innhald")
5504 ("pl" :html
"Spis treści")
5505 ("pt_BR" :html
"Índice" :utf8
"Índice" :ascii
"Indice")
5506 ("ru" :html
"Содержание"
5507 :utf-8
"Содержание")
5508 ("sv" :html
"Innehåll")
5509 ("uk" :html
"Зміст" :utf-8
"Зміст")
5510 ("zh-CN" :html
"目录" :utf-8
"目录")
5511 ("zh-TW" :html
"目錄" :utf-8
"目錄"))
5512 ("Unknown reference"
5513 ("da" :default
"ukendt reference")
5514 ("de" :default
"Unbekannter Verweis")
5515 ("es" :default
"referencia desconocida")
5516 ("et" :default
"Tundmatu viide")
5517 ("fr" :ascii
"Destination inconnue" :default
"Référence inconnue")
5518 ("ja" :default
"不明な参照先")
5519 ("pt_BR" :default
"Referência desconhecida"
5520 :ascii
"Referencia desconhecida")
5521 ("ru" :html
"Неизвестная ссылка"
5522 :utf-8
"Неизвестная ссылка")
5523 ("zh-CN" :html
"未知引用" :utf-8
"未知引用")))
5524 "Dictionary for export engine.
5526 Alist whose CAR is the string to translate and CDR is an alist
5527 whose CAR is the language string and CDR is a plist whose
5528 properties are possible charsets and values translated terms.
5530 It is used as a database for `org-export-translate'. Since this
5531 function returns the string as-is if no translation was found,
5532 the variable only needs to record values different from the
5535 (defun org-export-translate (s encoding info
)
5536 "Translate string S according to language specification.
5538 ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
5539 and `:utf-8'. INFO is a plist used as a communication channel.
5541 Translation depends on `:language' property. Return the
5542 translated string. If no translation is found, try to fall back
5543 to `:default' encoding. If it fails, return S."
5544 (let* ((lang (plist-get info
:language
))
5545 (translations (cdr (assoc lang
5546 (cdr (assoc s org-export-dictionary
))))))
5547 (or (plist-get translations encoding
)
5548 (plist-get translations
:default
)
5553 ;;; Asynchronous Export
5555 ;; `org-export-async-start' is the entry point for asynchronous
5556 ;; export. It recreates current buffer (including visibility,
5557 ;; narrowing and visited file) in an external Emacs process, and
5558 ;; evaluates a command there. It then applies a function on the
5559 ;; returned results in the current process.
5561 ;; At a higher level, `org-export-to-buffer' and `org-export-to-file'
5562 ;; allow to export to a buffer or a file, asynchronously or not.
5564 ;; `org-export-output-file-name' is an auxiliary function meant to be
5565 ;; used with `org-export-to-file'. With a given extension, it tries
5566 ;; to provide a canonical file name to write export output to.
5568 ;; Asynchronously generated results are never displayed directly.
5569 ;; Instead, they are stored in `org-export-stack-contents'. They can
5570 ;; then be retrieved by calling `org-export-stack'.
5572 ;; Export Stack is viewed through a dedicated major mode
5573 ;;`org-export-stack-mode' and tools: `org-export-stack-refresh',
5574 ;;`org-export-stack-delete', `org-export-stack-view' and
5575 ;;`org-export-stack-clear'.
5577 ;; For back-ends, `org-export-add-to-stack' add a new source to stack.
5578 ;; It should be used whenever `org-export-async-start' is called.
5580 (defmacro org-export-async-start
(fun &rest body
)
5581 "Call function FUN on the results returned by BODY evaluation.
5583 FUN is an anonymous function of one argument. BODY evaluation
5584 happens in an asynchronous process, from a buffer which is an
5585 exact copy of the current one.
5587 Use `org-export-add-to-stack' in FUN in order to register results
5590 This is a low level function. See also `org-export-to-buffer'
5591 and `org-export-to-file' for more specialized functions."
5592 (declare (indent 1) (debug t
))
5593 (org-with-gensyms (process temp-file copy-fun proc-buffer coding
)
5594 ;; Write the full sexp evaluating BODY in a copy of the current
5595 ;; buffer to a temporary file, as it may be too long for program
5596 ;; args in `start-process'.
5597 `(with-temp-message "Initializing asynchronous export process"
5598 (let ((,copy-fun
(org-export--generate-copy-script (current-buffer)))
5599 (,temp-file
(make-temp-file "org-export-process"))
5600 (,coding buffer-file-coding-system
))
5601 (with-temp-file ,temp-file
5603 ;; Null characters (from variable values) are inserted
5604 ;; within the file. As a consequence, coding system for
5605 ;; buffer contents will not be recognized properly. So,
5606 ;; we make sure it is the same as the one used to display
5607 ;; the original buffer.
5608 (format ";; -*- coding: %s; -*-\n%S"
5611 (when org-export-async-debug
'(setq debug-on-error t
))
5612 ;; Ignore `kill-emacs-hook' and code evaluation
5613 ;; queries from Babel as we need a truly
5614 ;; non-interactive process.
5615 (setq kill-emacs-hook nil
5616 org-babel-confirm-evaluate-answer-no t
)
5617 ;; Initialize export framework.
5619 ;; Re-create current buffer there.
5620 (funcall ,,copy-fun
)
5621 (restore-buffer-modified-p nil
)
5622 ;; Sexp to evaluate in the buffer.
5623 (print (progn ,,@body
))))))
5624 ;; Start external process.
5625 (let* ((process-connection-type nil
)
5626 (,proc-buffer
(generate-new-buffer-name "*Org Export Process*"))
5631 (list "org-export-process"
5633 (expand-file-name invocation-name invocation-directory
)
5635 (if org-export-async-init-file
5636 (list "-Q" "-l" org-export-async-init-file
)
5637 (list "-l" user-init-file
))
5638 (list "-l" ,temp-file
)))))
5639 ;; Register running process in stack.
5640 (org-export-add-to-stack (get-buffer ,proc-buffer
) nil
,process
)
5641 ;; Set-up sentinel in order to catch results.
5642 (let ((handler ,fun
))
5643 (set-process-sentinel
5646 (let ((proc-buffer (process-buffer p
)))
5647 (when (eq (process-status p
) 'exit
)
5649 (if (zerop (process-exit-status p
))
5652 (with-current-buffer proc-buffer
5653 (goto-char (point-max))
5655 (read (current-buffer)))))
5656 (funcall ,handler results
))
5657 (unless org-export-async-debug
5658 (and (get-buffer proc-buffer
)
5659 (kill-buffer proc-buffer
))))
5660 (org-export-add-to-stack proc-buffer nil p
)
5662 (message "Process '%s' exited abnormally" p
))
5663 (unless org-export-async-debug
5664 (delete-file ,,temp-file
)))))))))))))
5667 (defun org-export-to-buffer
5669 &optional async subtreep visible-only body-only ext-plist
5671 "Call `org-export-as' with output to a specified buffer.
5673 BACKEND is either an export back-end, as returned by, e.g.,
5674 `org-export-create-backend', or a symbol referring to
5675 a registered back-end.
5677 BUFFER is the name of the output buffer. If it already exists,
5678 it will be erased first, otherwise, it will be created.
5680 A non-nil optional argument ASYNC means the process should happen
5681 asynchronously. The resulting buffer should then be accessible
5682 through the `org-export-stack' interface. When ASYNC is nil, the
5683 buffer is displayed if `org-export-show-temporary-export-buffer'
5686 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
5687 EXT-PLIST are similar to those used in `org-export-as', which
5690 Optional argument POST-PROCESS is a function which should accept
5691 no argument. It is always called within the current process,
5692 from BUFFER, with point at its beginning. Export back-ends can
5693 use it to set a major mode there, e.g,
5695 \(defun org-latex-export-as-latex
5696 \(&optional async subtreep visible-only body-only ext-plist)
5698 \(org-export-to-buffer 'latex \"*Org LATEX Export*\"
5699 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
5701 This function returns BUFFER."
5702 (declare (indent 2))
5704 (org-export-async-start
5706 (with-current-buffer (get-buffer-create ,buffer
)
5708 (setq buffer-file-coding-system
',buffer-file-coding-system
)
5710 (goto-char (point-min))
5711 (org-export-add-to-stack (current-buffer) ',backend
)
5712 (ignore-errors (funcall ,post-process
))))
5714 ',backend
,subtreep
,visible-only
,body-only
',ext-plist
))
5716 (org-export-as backend subtreep visible-only body-only ext-plist
))
5717 (buffer (get-buffer-create buffer
))
5718 (encoding buffer-file-coding-system
))
5719 (when (and (org-string-nw-p output
) (org-export--copy-to-kill-ring-p))
5720 (org-kill-new output
))
5721 (with-current-buffer buffer
5723 (setq buffer-file-coding-system encoding
)
5725 (goto-char (point-min))
5726 (and (functionp post-process
) (funcall post-process
)))
5727 (when org-export-show-temporary-export-buffer
5728 (switch-to-buffer-other-window buffer
))
5732 (defun org-export-to-file
5733 (backend file
&optional async subtreep visible-only body-only ext-plist
5735 "Call `org-export-as' with output to a specified file.
5737 BACKEND is either an export back-end, as returned by, e.g.,
5738 `org-export-create-backend', or a symbol referring to
5739 a registered back-end. FILE is the name of the output file, as
5742 A non-nil optional argument ASYNC means the process should happen
5743 asynchronously. The resulting buffer will then be accessible
5744 through the `org-export-stack' interface.
5746 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
5747 EXT-PLIST are similar to those used in `org-export-as', which
5750 Optional argument POST-PROCESS is called with FILE as its
5751 argument and happens asynchronously when ASYNC is non-nil. It
5752 has to return a file name, or nil. Export back-ends can use this
5753 to send the output file through additional processing, e.g,
5755 \(defun org-latex-export-to-latex
5756 \(&optional async subtreep visible-only body-only ext-plist)
5758 \(let ((outfile (org-export-output-file-name \".tex\" subtreep)))
5759 \(org-export-to-file 'latex outfile
5760 async subtreep visible-only body-only ext-plist
5761 \(lambda (file) (org-latex-compile file)))
5763 The function returns either a file name returned by POST-PROCESS,
5765 (declare (indent 2))
5766 (if (not (file-writable-p file
)) (error "Output file not writable")
5767 (let ((ext-plist (org-combine-plists `(:output-file
,file
) ext-plist
))
5768 (encoding (or org-export-coding-system buffer-file-coding-system
)))
5770 (org-export-async-start
5772 (org-export-add-to-stack (expand-file-name file
) ',backend
))
5775 ',backend
,subtreep
,visible-only
,body-only
5779 (let ((coding-system-for-write ',encoding
))
5780 (write-file ,file
)))
5781 (or (ignore-errors (funcall ',post-process
,file
)) ,file
)))
5782 (let ((output (org-export-as
5783 backend subtreep visible-only body-only ext-plist
)))
5786 (let ((coding-system-for-write encoding
))
5788 (when (and (org-export--copy-to-kill-ring-p) (org-string-nw-p output
))
5789 (org-kill-new output
))
5790 ;; Get proper return value.
5791 (or (and (functionp post-process
) (funcall post-process file
))
5794 (defun org-export-output-file-name (extension &optional subtreep pub-dir
)
5795 "Return output file's name according to buffer specifications.
5797 EXTENSION is a string representing the output file extension,
5798 with the leading dot.
5800 With a non-nil optional argument SUBTREEP, try to determine
5801 output file's name by looking for \"EXPORT_FILE_NAME\" property
5802 of subtree at point.
5804 When optional argument PUB-DIR is set, use it as the publishing
5807 When optional argument VISIBLE-ONLY is non-nil, don't export
5808 contents of hidden elements.
5810 Return file name as a string."
5811 (let* ((visited-file (buffer-file-name (buffer-base-buffer)))
5813 ;; File name may come from EXPORT_FILE_NAME subtree
5814 ;; property, assuming point is at beginning of said
5816 (file-name-sans-extension
5820 (ignore-errors (org-back-to-heading) (point)))
5821 "EXPORT_FILE_NAME" t
))
5822 ;; File name may be extracted from buffer's associated
5824 (and visited-file
(file-name-nondirectory visited-file
))
5825 ;; Can't determine file name on our own: Ask user.
5826 (let ((read-file-name-function
5827 (and org-completion-use-ido
'ido-read-file-name
)))
5829 "Output file: " pub-dir nil nil nil
5831 (string= (file-name-extension name t
) extension
)))))))
5833 ;; Build file name. Enforce EXTENSION over whatever user
5834 ;; may have come up with. PUB-DIR, if defined, always has
5835 ;; precedence over any provided path.
5838 (concat (file-name-as-directory pub-dir
)
5839 (file-name-nondirectory base-name
)
5841 ((file-name-absolute-p base-name
) (concat base-name extension
))
5842 (t (concat (file-name-as-directory ".") base-name extension
)))))
5843 ;; If writing to OUTPUT-FILE would overwrite original file, append
5844 ;; EXTENSION another time to final name.
5845 (if (and visited-file
(org-file-equal-p visited-file output-file
))
5846 (concat output-file extension
)
5849 (defun org-export-add-to-stack (source backend
&optional process
)
5850 "Add a new result to export stack if not present already.
5852 SOURCE is a buffer or a file name containing export results.
5853 BACKEND is a symbol representing export back-end used to generate
5856 Entries already pointing to SOURCE and unavailable entries are
5857 removed beforehand. Return the new stack."
5858 (setq org-export-stack-contents
5859 (cons (list source backend
(or process
(current-time)))
5860 (org-export-stack-remove source
))))
5862 (defun org-export-stack ()
5863 "Menu for asynchronous export results and running processes."
5865 (let ((buffer (get-buffer-create "*Org Export Stack*")))
5867 (when (zerop (buffer-size)) (org-export-stack-mode))
5868 (org-export-stack-refresh)
5869 (pop-to-buffer buffer
))
5870 (message "Type \"q\" to quit, \"?\" for help"))
5872 (defun org-export--stack-source-at-point ()
5873 "Return source from export results at point in stack."
5874 (let ((source (car (nth (1- (org-current-line)) org-export-stack-contents
))))
5875 (if (not source
) (error "Source unavailable, please refresh buffer")
5876 (let ((source-name (if (stringp source
) source
(buffer-name source
))))
5879 (looking-at (concat ".* +" (regexp-quote source-name
) "$")))
5881 ;; SOURCE is not consistent with current line. The stack
5882 ;; view is outdated.
5883 (error "Source unavailable; type `g' to update buffer"))))))
5885 (defun org-export-stack-clear ()
5886 "Remove all entries from export stack."
5888 (setq org-export-stack-contents nil
))
5890 (defun org-export-stack-refresh (&rest dummy
)
5891 "Refresh the asynchronous export stack.
5892 DUMMY is ignored. Unavailable sources are removed from the list.
5893 Return the new stack."
5894 (let ((inhibit-read-only t
))
5901 (let ((proc-p (processp (nth 2 entry
))))
5904 (format " %-12s " (or (nth 1 entry
) ""))
5906 (let ((data (nth 2 entry
)))
5907 (if proc-p
(format " %6s " (process-status data
))
5908 ;; Compute age of the results.
5911 (float-time (time-since data
)))))
5914 (let ((source (car entry
)))
5915 (if (stringp source
) source
5916 (buffer-name source
)))))))
5917 ;; Clear stack from exited processes, dead buffers or
5918 ;; non-existent files.
5919 (setq org-export-stack-contents
5922 (if (processp (nth 2 el
))
5923 (buffer-live-p (process-buffer (nth 2 el
)))
5924 (let ((source (car el
)))
5925 (if (bufferp source
) (buffer-live-p source
)
5926 (file-exists-p source
)))))
5927 org-export-stack-contents
)) "\n")))))))
5929 (defun org-export-stack-remove (&optional source
)
5930 "Remove export results at point from stack.
5931 If optional argument SOURCE is non-nil, remove it instead."
5933 (let ((source (or source
(org-export--stack-source-at-point))))
5934 (setq org-export-stack-contents
5935 (org-remove-if (lambda (el) (equal (car el
) source
))
5936 org-export-stack-contents
))))
5938 (defun org-export-stack-view (&optional in-emacs
)
5939 "View export results at point in stack.
5940 With an optional prefix argument IN-EMACS, force viewing files
5943 (let ((source (org-export--stack-source-at-point)))
5944 (cond ((processp source
)
5945 (org-switch-to-buffer-other-window (process-buffer source
)))
5946 ((bufferp source
) (org-switch-to-buffer-other-window source
))
5947 (t (org-open-file source in-emacs
)))))
5949 (defvar org-export-stack-mode-map
5950 (let ((km (make-sparse-keymap)))
5951 (define-key km
" " 'next-line
)
5952 (define-key km
"n" 'next-line
)
5953 (define-key km
"\C-n" 'next-line
)
5954 (define-key km
[down] 'next-line)
5955 (define-key km "p" 'previous-line)
5956 (define-key km "\C-p" 'previous-line)
5957 (define-key km "\C-?" 'previous-line)
5958 (define-key km [up] 'previous-line)
5959 (define-key km "C" 'org-export-stack-clear)
5960 (define-key km "v" 'org-export-stack-view)
5961 (define-key km (kbd "RET") 'org-export-stack-view)
5962 (define-key km "d" 'org-export-stack-remove)
5964 "Keymap for Org Export Stack.")
5966 (define-derived-mode org-export-stack-mode special-mode "Org-Stack"
5967 "Mode for displaying asynchronous export stack.
5969 Type \\[org-export-stack] to visualize the asynchronous export
5972 In an Org Export Stack buffer, use \\<org-export-stack-mode-map>\\[org-export-stack-view] to view export output
5973 on current line, \\[org-export-stack-remove] to remove it from the stack and \\[org-export-stack-clear] to clear
5976 Removing entries in an Org Export Stack buffer doesn't affect
5977 files or buffers, only the display.
5979 \\{org-export-stack-mode-map}"
5982 (setq buffer-read-only t
5987 (format " %-12s | %6s | %s" "Back-End" "Age" "Source")))
5988 (org-add-hook 'post-command-hook 'org-export-stack-refresh nil t)
5989 (set (make-local-variable 'revert-buffer-function)
5990 'org-export-stack-refresh))
5996 ;; `org-export-dispatch' is the standard interactive way to start an
5997 ;; export process. It uses `org-export--dispatch-ui' as a subroutine
5998 ;; for its interface, which, in turn, delegates response to key
5999 ;; pressed to `org-export--dispatch-action'.
6002 (defun org-export-dispatch (&optional arg)
6003 "Export dispatcher for Org mode.
6005 It provides an access to common export related tasks in a buffer.
6006 Its interface comes in two flavors: standard and expert.
6008 While both share the same set of bindings, only the former
6009 displays the valid keys associations in a dedicated buffer.
6010 Scrolling (resp. line-wise motion) in this buffer is done with
6011 SPC and DEL (resp. C-n and C-p) keys.
6013 Set variable `org-export-dispatch-use-expert-ui' to switch to one
6014 flavor or the other.
6016 When ARG is \\[universal-argument], repeat the last export action, with the same set
6017 of options used back then, on the current buffer.
6019 When ARG is \\[universal-argument] \\[universal-argument], display the asynchronous export stack."
6022 (cond ((equal arg '(16)) '(stack))
6023 ((and arg org-export-dispatch-last-action))
6024 (t (save-window-excursion
6027 ;; Remember where we are
6028 (move-marker org-export-dispatch-last-position
6030 (org-base-buffer (current-buffer)))
6031 ;; Get and store an export command
6032 (setq org-export-dispatch-last-action
6033 (org-export--dispatch-ui
6034 (list org-export-initial-scope
6035 (and org-export-in-background 'async))
6037 org-export-dispatch-use-expert-ui)))
6038 (and (get-buffer "*Org Export Dispatcher*")
6039 (kill-buffer "*Org Export Dispatcher*")))))))
6040 (action (car input))
6041 (optns (cdr input)))
6042 (unless (memq 'subtree optns)
6043 (move-marker org-export-dispatch-last-position nil))
6045 ;; First handle special hard-coded actions.
6046 (template (org-export-insert-default-template nil optns))
6047 (stack (org-export-stack))
6048 (publish-current-file
6049 (org-publish-current-file (memq 'force optns) (memq 'async optns)))
6050 (publish-current-project
6051 (org-publish-current-project (memq 'force optns) (memq 'async optns)))
6052 (publish-choose-project
6053 (org-publish (assoc (org-icompleting-read
6055 org-publish-project-alist nil t)
6056 org-publish-project-alist)
6058 (memq 'async optns)))
6059 (publish-all (org-publish-all (memq 'force optns) (memq 'async optns)))
6063 ;; Repeating command, maybe move cursor to restore subtree
6065 (if (eq (marker-buffer org-export-dispatch-last-position)
6066 (org-base-buffer (current-buffer)))
6067 (goto-char org-export-dispatch-last-position)
6068 ;; We are in a different buffer, forget position.
6069 (move-marker org-export-dispatch-last-position nil)))
6071 ;; Return a symbol instead of a list to ease
6072 ;; asynchronous export macro use.
6073 (and (memq 'async optns) t)
6074 (and (memq 'subtree optns) t)
6075 (and (memq 'visible optns) t)
6076 (and (memq 'body optns) t)))))))
6078 (defun org-export--dispatch-ui (options first-key expertp)
6079 "Handle interface for `org-export-dispatch'.
6081 OPTIONS is a list containing current interactive options set for
6082 export. It can contain any of the following symbols:
6083 `body' toggles a body-only export
6084 `subtree' restricts export to current subtree
6085 `visible' restricts export to visible part of buffer.
6086 `force' force publishing files.
6087 `async' use asynchronous export process
6089 FIRST-KEY is the key pressed to select the first level menu. It
6090 is nil when this menu hasn't been selected yet.
6092 EXPERTP, when non-nil, triggers expert UI. In that case, no help
6093 buffer is provided, but indications about currently active
6094 options are given in the prompt. Moreover, \[?] allows to switch
6095 back to standard interface."
6097 (lambda (key &optional access-key)
6098 ;; Fontify KEY string. Optional argument ACCESS-KEY, when
6099 ;; non-nil is the required first-level key to activate
6100 ;; KEY. When its value is t, activate KEY independently
6101 ;; on the first key, if any. A nil value means KEY will
6102 ;; only be activated at first level.
6103 (if (or (eq access-key t) (eq access-key first-key))
6104 (org-propertize key 'face 'org-warning)
6108 ;; Fontify VALUE string.
6109 (org-propertize value 'face 'font-lock-variable-name-face)))
6110 ;; Prepare menu entries by extracting them from registered
6111 ;; back-ends and sorting them by access key and by ordinal,
6114 (sort (sort (delq nil
6115 (mapcar 'org-export-backend-menu
6116 org-export--registered-backends))
6118 (let ((key-a (nth 1 a))
6120 (cond ((and (numberp key-a) (numberp key-b))
6122 ((numberp key-b) t)))))
6123 'car-less-than-car))
6124 ;; Compute a list of allowed keys based on the first key
6125 ;; pressed, if any. Some keys
6126 ;; (?^B, ?^V, ?^S, ?^F, ?^A, ?&, ?# and ?q) are always
6129 (nconc (list 2 22 19 6 1)
6130 (if (not first-key) (org-uniquify (mapcar 'car entries))
6132 (dolist (entry entries (sort (mapcar 'car sub-menu) '<))
6133 (when (eq (car entry) first-key)
6134 (setq sub-menu (append (nth 2 entry) sub-menu))))))
6135 (cond ((eq first-key ?P) (list ?f ?p ?x ?a))
6136 ((not first-key) (list ?P)))
6138 (when expertp (list ??))
6140 ;; Build the help menu for standard UI.
6144 ;; Options are hard-coded.
6145 (format "[%s] Body only: %s [%s] Visible only: %s
6146 \[%s] Export scope: %s [%s] Force publishing: %s
6147 \[%s] Async export: %s\n\n"
6148 (funcall fontify-key "C-b" t)
6149 (funcall fontify-value
6150 (if (memq 'body options) "On " "Off"))
6151 (funcall fontify-key "C-v" t)
6152 (funcall fontify-value
6153 (if (memq 'visible options) "On " "Off"))
6154 (funcall fontify-key "C-s" t)
6155 (funcall fontify-value
6156 (if (memq 'subtree options) "Subtree" "Buffer "))
6157 (funcall fontify-key "C-f" t)
6158 (funcall fontify-value
6159 (if (memq 'force options) "On " "Off"))
6160 (funcall fontify-key "C-a" t)
6161 (funcall fontify-value
6162 (if (memq 'async options) "On " "Off")))
6163 ;; Display registered back-end entries. When a key
6164 ;; appears for the second time, do not create another
6165 ;; entry, but append its sub-menu to existing menu.
6169 (let ((top-key (car entry)))
6171 (unless (eq top-key last-key)
6172 (setq last-key top-key)
6173 (format "\n[%s] %s\n"
6174 (funcall fontify-key (char-to-string top-key))
6176 (let ((sub-menu (nth 2 entry)))
6177 (unless (functionp sub-menu)
6178 ;; Split sub-menu into two columns.
6185 (if (zerop (mod index 2)) " [%s] %-26s"
6187 (funcall fontify-key
6188 (char-to-string (car sub-entry))
6192 (when (zerop (mod index 2)) "\n"))))))))
6194 ;; Publishing menu is hard-coded.
6195 (format "\n[%s] Publish
6196 [%s] Current file [%s] Current project
6197 [%s] Choose project [%s] All projects\n\n\n"
6198 (funcall fontify-key "P")
6199 (funcall fontify-key "f" ?P)
6200 (funcall fontify-key "p" ?P)
6201 (funcall fontify-key "x" ?P)
6202 (funcall fontify-key "a" ?P))
6203 (format "[%s] Export stack [%s] Insert template\n"
6204 (funcall fontify-key "&" t)
6205 (funcall fontify-key "#" t))
6207 (funcall fontify-key "q" t)
6208 (if first-key "Main menu" "Exit")))))
6209 ;; Build prompts for both standard and expert UI.
6210 (standard-prompt (unless expertp "Export command: "))
6214 "Export command (C-%s%s%s%s%s) [%s]: "
6215 (if (memq 'body options) (funcall fontify-key "b" t) "b")
6216 (if (memq 'visible options) (funcall fontify-key "v" t) "v")
6217 (if (memq 'subtree options) (funcall fontify-key "s" t) "s")
6218 (if (memq 'force options) (funcall fontify-key "f" t) "f")
6219 (if (memq 'async options) (funcall fontify-key "a" t) "a")
6220 (mapconcat (lambda (k)
6221 ;; Strip control characters.
6222 (unless (< k 27) (char-to-string k)))
6223 allowed-keys "")))))
6224 ;; With expert UI, just read key with a fancy prompt. In standard
6225 ;; UI, display an intrusive help buffer.
6227 (org-export--dispatch-action
6228 expert-prompt allowed-keys entries options first-key expertp)
6229 ;; At first call, create frame layout in order to display menu.
6230 (unless (get-buffer "*Org Export Dispatcher*")
6231 (delete-other-windows)
6232 (org-switch-to-buffer-other-window
6233 (get-buffer-create "*Org Export Dispatcher*"))
6234 (setq cursor-type nil
6235 header-line-format "Use SPC, DEL, C-n or C-p to navigate.")
6236 ;; Make sure that invisible cursor will not highlight square
6238 (set-syntax-table (copy-syntax-table))
6239 (modify-syntax-entry ?\[ "w"))
6240 ;; At this point, the buffer containing the menu exists and is
6241 ;; visible in the current window. So, refresh it.
6242 (with-current-buffer "*Org Export Dispatcher*"
6243 ;; Refresh help. Maintain display continuity by re-visiting
6244 ;; previous window position.
6245 (let ((pos (window-start)))
6248 (set-window-start nil pos)))
6249 (org-fit-window-to-buffer)
6250 (org-export--dispatch-action
6251 standard-prompt allowed-keys entries options first-key expertp))))
6253 (defun org-export--dispatch-action
6254 (prompt allowed-keys entries options first-key expertp)
6255 "Read a character from command input and act accordingly.
6257 PROMPT is the displayed prompt, as a string. ALLOWED-KEYS is
6258 a list of characters available at a given step in the process.
6259 ENTRIES is a list of menu entries. OPTIONS, FIRST-KEY and
6260 EXPERTP are the same as defined in `org-export--dispatch-ui',
6263 Toggle export options when required. Otherwise, return value is
6264 a list with action as CAR and a list of interactive export
6267 ;; Scrolling: when in non-expert mode, act on motion keys (C-n,
6269 (while (and (setq key (read-char-exclusive prompt))
6271 (memq key '(14 16 ?\s ?\d)))
6273 (14 (if (not (pos-visible-in-window-p (point-max)))
6274 (ignore-errors (scroll-up 1))
6275 (message "End of buffer")
6277 (16 (if (not (pos-visible-in-window-p (point-min)))
6278 (ignore-errors (scroll-down 1))
6279 (message "Beginning of buffer")
6281 (?\s (if (not (pos-visible-in-window-p (point-max)))
6283 (message "End of buffer")
6285 (?\d (if (not (pos-visible-in-window-p (point-min)))
6287 (message "Beginning of buffer")
6290 ;; Ignore undefined associations.
6291 ((not (memq key allowed-keys))
6293 (unless expertp (message "Invalid key") (sit-for 1))
6294 (org-export--dispatch-ui options first-key expertp))
6295 ;; q key at first level aborts export. At second level, cancel
6296 ;; first key instead.
6297 ((eq key ?q) (if (not first-key) (error "Export aborted")
6298 (org-export--dispatch-ui options nil expertp)))
6299 ;; Help key: Switch back to standard interface if expert UI was
6301 ((eq key ??) (org-export--dispatch-ui options first-key nil))
6302 ;; Send request for template insertion along with export scope.
6303 ((eq key ?#) (cons 'template (memq 'subtree options)))
6304 ;; Switch to asynchronous export stack.
6305 ((eq key ?&) '(stack))
6306 ;; Toggle options: C-b (2) C-v (22) C-s (19) C-f (6) C-a (1).
6307 ((memq key '(2 22 19 6 1))
6308 (org-export--dispatch-ui
6309 (let ((option (case key (2 'body) (22 'visible) (19 'subtree)
6310 (6 'force) (1 'async))))
6311 (if (memq option options) (remq option options)
6312 (cons option options)))
6314 ;; Action selected: Send key and options back to
6315 ;; `org-export-dispatch'.
6316 ((or first-key (functionp (nth 2 (assq key entries))))
6318 ((not first-key) (nth 2 (assq key entries)))
6319 ;; Publishing actions are hard-coded. Send a special
6320 ;; signal to `org-export-dispatch'.
6323 (?f 'publish-current-file)
6324 (?p 'publish-current-project)
6325 (?x 'publish-choose-project)
6327 ;; Return first action associated to FIRST-KEY + KEY
6328 ;; path. Indeed, derived backends can share the same
6331 (mapc (lambda (entry)
6332 (let ((match (assq key (nth 2 entry))))
6333 (when match (throw 'found (nth 2 match)))))
6334 (member (assq first-key entries) entries)))))
6336 ;; Otherwise, enter sub-menu.
6337 (t (org-export--dispatch-ui options key expertp)))))
6344 ;; generated-autoload-file: "org-loaddefs.el"