Handling of file inclusion through keywords is done before export
[org-mode.git] / contrib / lisp / org-export.el
blobd22d972ad6d3fe14ea20f096e2fa613f0a48dc61
1 ;;; org-export.el --- Generic Export Engine For Org
3 ;; Copyright (C) 2012 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ;;; Commentary:
23 ;; This library implements a generic export engine for Org, built on
24 ;; its syntactical parser: Org Elements.
26 ;; Besides that parser, the generic exporter is made of three distinct
27 ;; parts:
29 ;; - The communication channel consists in a property list, which is
30 ;; created and updated during the process. Its use is to offer
31 ;; every piece of information, would it be export options or
32 ;; contextual data, all in a single place. The exhaustive list of
33 ;; properties is given in "The Communication Channel" section of
34 ;; this file.
36 ;; - The transcoder walks the parse tree, ignores or treat as plain
37 ;; text elements and objects according to export options, and
38 ;; eventually calls back-end specific functions to do the real
39 ;; transcoding, concatenating their return value along the way.
41 ;; - The filter system is activated at the very beginning and the very
42 ;; end of the export process, and each time an element or an object
43 ;; has been converted. It is the entry point to fine-tune standard
44 ;; output from back-end transcoders.
46 ;; The core function is `org-export-as'. It returns the transcoded
47 ;; buffer as a string.
49 ;; In order to derive an exporter out of this generic implementation,
50 ;; one can define a transcode function for each element or object.
51 ;; Such function should return a string for the corresponding element,
52 ;; without any trailing space, or nil. It must accept three
53 ;; arguments:
54 ;; 1. the element or object itself,
55 ;; 2. its contents, or nil when it isn't recursive,
56 ;; 3. the property list used as a communication channel.
58 ;; If no such function is found, that element or object type will
59 ;; simply be ignored, along with any separating blank line. The same
60 ;; will happen if the function returns the nil value. If that
61 ;; function returns the empty string, the type will be ignored, but
62 ;; the blank lines will be kept.
64 ;; Contents, when not nil, are stripped from any global indentation
65 ;; (although the relative one is preserved). They also always end
66 ;; with a single newline character.
68 ;; These functions must follow a strict naming convention:
69 ;; `org-BACKEND-TYPE' where, obviously, BACKEND is the name of the
70 ;; export back-end and TYPE the type of the element or object handled.
72 ;; Moreover, two additional functions can be defined. On the one
73 ;; hand, `org-BACKEND-template' returns the final transcoded string,
74 ;; and can be used to add a preamble and a postamble to document's
75 ;; body. It must accept two arguments: the transcoded string and the
76 ;; property list containing export options. On the other hand,
77 ;; `org-BACKEND-plain-text', when defined, is to be called on every
78 ;; text not recognized as an element or an object. It must accept two
79 ;; arguments: the text string and the information channel.
81 ;; Any back-end can define its own variables. Among them, those
82 ;; customizables should belong to the `org-export-BACKEND' group.
83 ;; Also, a special variable, `org-BACKEND-option-alist', allows to
84 ;; define buffer keywords and "#+options:" items specific to that
85 ;; back-end. See `org-export-option-alist' for supported defaults and
86 ;; syntax.
88 ;; Tools for common tasks across back-ends are implemented in the
89 ;; penultimate part of this file. A dispatcher for standard back-ends
90 ;; is provided in the last one.
92 ;;; Code:
93 (eval-when-compile (require 'cl))
94 (require 'org-element)
95 ;; Require major back-ends
96 (require 'org-e-ascii "../../EXPERIMENTAL/org-e-ascii.el")
97 (require 'org-e-latex "../../EXPERIMENTAL/org-e-latex.el")
100 ;;; Internal Variables
102 ;; Among internal variables, the most important is
103 ;; `org-export-option-alist'. This variable define the global export
104 ;; options, shared between every exporter, and how they are acquired.
106 (defconst org-export-max-depth 19
107 "Maximum nesting depth for headlines, counting from 0.")
109 (defconst org-export-option-alist
110 '((:author "AUTHOR" nil user-full-name t)
111 (:creator "CREATOR" nil org-export-creator-string)
112 (:date "DATE" nil nil t)
113 (:description "DESCRIPTION" nil nil newline)
114 (:email "EMAIL" nil user-mail-address t)
115 (:exclude-tags "EXPORT_EXCLUDE_TAGS" nil org-export-exclude-tags split)
116 (:filter-babel-call nil nil org-export-filter-babel-call-functions)
117 (:filter-center-block nil nil org-export-filter-center-block-functions)
118 (:filter-comment nil nil org-export-filter-comment-functions)
119 (:filter-comment-block nil nil org-export-filter-comment-block-functions)
120 (:filter-drawer nil nil org-export-filter-drawer-functions)
121 (:filter-dynamic-block nil nil org-export-filter-dynamic-block-functions)
122 (:filter-emphasis nil nil org-export-filter-emphasis-functions)
123 (:filter-entity nil nil org-export-filter-entity-functions)
124 (:filter-example-block nil nil org-export-filter-example-block-functions)
125 (:filter-export-block nil nil org-export-filter-export-block-functions)
126 (:filter-export-snippet nil nil org-export-filter-export-snippet-functions)
127 (:filter-final-output nil nil org-export-filter-final-output-functions)
128 (:filter-fixed-width nil nil org-export-filter-fixed-width-functions)
129 (:filter-footnote-definition nil nil org-export-filter-footnote-definition-functions)
130 (:filter-footnote-reference nil nil org-export-filter-footnote-reference-functions)
131 (:filter-headline nil nil org-export-filter-headline-functions)
132 (:filter-horizontal-rule nil nil org-export-filter-horizontal-rule-functions)
133 (:filter-inline-babel-call nil nil org-export-filter-inline-babel-call-functions)
134 (:filter-inline-src-block nil nil org-export-filter-inline-src-block-functions)
135 (:filter-inlinetask nil nil org-export-filter-inlinetask-functions)
136 (:filter-item nil nil org-export-filter-item-functions)
137 (:filter-keyword nil nil org-export-filter-keyword-functions)
138 (:filter-latex-environment nil nil org-export-filter-latex-environment-functions)
139 (:filter-latex-fragment nil nil org-export-filter-latex-fragment-functions)
140 (:filter-line-break nil nil org-export-filter-line-break-functions)
141 (:filter-link nil nil org-export-filter-link-functions)
142 (:filter-macro nil nil org-export-filter-macro-functions)
143 (:filter-paragraph nil nil org-export-filter-paragraph-functions)
144 (:filter-parse-tree nil nil org-export-filter-parse-tree-functions)
145 (:filter-plain-list nil nil org-export-filter-plain-list-functions)
146 (:filter-plain-text nil nil org-export-filter-plain-text-functions)
147 (:filter-property-drawer nil nil org-export-filter-property-drawer-functions)
148 (:filter-quote-block nil nil org-export-filter-quote-block-functions)
149 (:filter-quote-section nil nil org-export-filter-quote-section-functions)
150 (:filter-radio-target nil nil org-export-filter-radio-target-functions)
151 (:filter-section nil nil org-export-filter-section-functions)
152 (:filter-special-block nil nil org-export-filter-special-block-functions)
153 (:filter-src-block nil nil org-export-filter-src-block-functions)
154 (:filter-statistics-cookie nil nil org-export-filter-statistics-cookie-functions)
155 (:filter-subscript nil nil org-export-filter-subscript-functions)
156 (:filter-superscript nil nil org-export-filter-superscript-functions)
157 (:filter-table nil nil org-export-filter-table-functions)
158 (:filter-target nil nil org-export-filter-target-functions)
159 (:filter-time-stamp nil nil org-export-filter-time-stamp-functions)
160 (:filter-verbatim nil nil org-export-filter-verbatim-functions)
161 (:filter-verse-block nil nil org-export-filter-verse-block-functions)
162 (:headline-levels nil "H" org-export-headline-levels)
163 (:keywords "KEYWORDS" nil nil space)
164 (:language "LANGUAGE" nil org-export-default-language t)
165 (:preserve-breaks nil "\\n" org-export-preserve-breaks)
166 (:section-numbers nil "num" org-export-with-section-numbers)
167 (:select-tags "EXPORT_SELECT_TAGS" nil org-export-select-tags split)
168 (:time-stamp-file nil "timestamp" org-export-time-stamp-file)
169 (:title "TITLE" nil nil space)
170 (:with-archived-trees nil "arch" org-export-with-archived-trees)
171 (:with-author nil "author" org-export-with-author)
172 (:with-creator nil "creator" org-export-with-creator)
173 (:with-drawers nil "d" org-export-with-drawers)
174 (:with-email nil "email" org-export-with-email)
175 (:with-emphasize nil "*" org-export-with-emphasize)
176 (:with-entities nil "e" org-export-with-entities)
177 (:with-fixed-width nil ":" org-export-with-fixed-width)
178 (:with-footnotes nil "f" org-export-with-footnotes)
179 (:with-priority nil "pri" org-export-with-priority)
180 (:with-special-strings nil "-" org-export-with-special-strings)
181 (:with-sub-superscript nil "^" org-export-with-sub-superscripts)
182 (:with-toc nil "toc" org-export-with-toc)
183 (:with-tables nil "|" org-export-with-tables)
184 (:with-tags nil "tags" org-export-with-tags)
185 (:with-tasks nil "tasks" org-export-with-tasks)
186 (:with-timestamps nil "<" org-export-with-timestamps)
187 (:with-todo-keywords nil "todo" org-export-with-todo-keywords))
188 "Alist between export properties and ways to set them.
190 The car of the alist is the property name, and the cdr is a list
191 like \(KEYWORD OPTION DEFAULT BEHAVIOUR\) where:
193 KEYWORD is a string representing a buffer keyword, or nil.
194 OPTION is a string that could be found in an #+OPTIONS: line.
195 DEFAULT is the default value for the property.
196 BEHAVIOUR determine how Org should handle multiple keywords for
197 the same property. It is a symbol among:
198 nil Keep old value and discard the new one.
199 t Replace old value with the new one.
200 `space' Concatenate the values, separating them with a space.
201 `newline' Concatenate the values, separating them with
202 a newline.
203 `split' Split values at white spaces, and cons them to the
204 previous list.
206 KEYWORD and OPTION have precedence over DEFAULT.
208 All these properties should be back-end agnostic. For back-end
209 specific properties, define a similar variable named
210 `org-BACKEND-option-alist', replacing BACKEND with the name of
211 the appropriate back-end. You can also redefine properties
212 there, as they have precedence over these.")
214 (defconst org-export-special-keywords
215 '("SETUP_FILE" "OPTIONS" "MACRO")
216 "List of in-buffer keywords that require special treatment.
217 These keywords are not directly associated to a property. The
218 way they are handled must be hard-coded into
219 `org-export-get-inbuffer-options' function.")
223 ;;; User-configurable Variables
225 ;; Configuration for the masses.
227 ;; They should never be evaled directly, as their value is to be
228 ;; stored in a property list (cf. `org-export-option-alist').
230 (defgroup org-export nil
231 "Options for exporting Org mode files."
232 :tag "Org Export"
233 :group 'org)
235 (defgroup org-export-general nil
236 "General options for export engine."
237 :tag "Org Export General"
238 :group 'org-export)
240 (defcustom org-export-with-archived-trees 'headline
241 "Whether sub-trees with the ARCHIVE tag should be exported.
243 This can have three different values:
244 nil Do not export, pretend this tree is not present.
245 t Do export the entire tree.
246 `headline' Only export the headline, but skip the tree below it.
248 This option can also be set with the #+OPTIONS line,
249 e.g. \"arch:nil\"."
250 :group 'org-export-general
251 :type '(choice
252 (const :tag "Not at all" nil)
253 (const :tag "Headline only" 'headline)
254 (const :tag "Entirely" t)))
256 (defcustom org-export-with-author t
257 "Non-nil means insert author name into the exported file.
258 This option can also be set with the #+OPTIONS line,
259 e.g. \"author:nil\"."
260 :group 'org-export-general
261 :type 'boolean)
263 (defcustom org-export-with-creator 'comment
264 "Non-nil means the postamble should contain a creator sentence.
266 The sentence can be set in `org-export-creator-string' and
267 defaults to \"Generated by Org mode XX in Emacs XXX.\".
269 If the value is `comment' insert it as a comment."
270 :group 'org-export-general
271 :type '(choice
272 (const :tag "No creator sentence" nil)
273 (const :tag "Sentence as a comment" 'comment)
274 (const :tag "Insert the sentence" t)))
276 (defcustom org-export-creator-string
277 (format "Generated by Org mode %s in Emacs %s." org-version emacs-version)
278 "String to insert at the end of the generated document."
279 :group 'org-export-general
280 :type '(string :tag "Creator string"))
282 (defcustom org-export-with-drawers t
283 "Non-nil means export contents of standard drawers.
285 When t, all drawers are exported. This may also be a list of
286 drawer names to export. This variable doesn't apply to
287 properties drawers.
289 This option can also be set with the #+OPTIONS line,
290 e.g. \"d:nil\"."
291 :group 'org-export-general
292 :type '(choice
293 (const :tag "All drawers" t)
294 (const :tag "None" nil)
295 (repeat :tag "Selected drawers"
296 (string :tag "Drawer name"))))
298 (defcustom org-export-with-email nil
299 "Non-nil means insert author email into the exported file.
300 This option can also be set with the #+OPTIONS line,
301 e.g. \"email:t\"."
302 :group 'org-export-general
303 :type 'boolean)
305 (defcustom org-export-with-emphasize t
306 "Non-nil means interpret *word*, /word/, and _word_ as emphasized text.
308 If the export target supports emphasizing text, the word will be
309 typeset in bold, italic, or underlined, respectively. Not all
310 export backends support this.
312 This option can also be set with the #+OPTIONS line, e.g. \"*:nil\"."
313 :group 'org-export-general
314 :type 'boolean)
316 (defcustom org-export-exclude-tags '("noexport")
317 "Tags that exclude a tree from export.
318 All trees carrying any of these tags will be excluded from
319 export. This is without condition, so even subtrees inside that
320 carry one of the `org-export-select-tags' will be removed."
321 :group 'org-export-general
322 :type '(repeat (string :tag "Tag")))
324 (defcustom org-export-with-fixed-width t
325 "Non-nil means lines starting with \":\" will be in fixed width font.
327 This can be used to have pre-formatted text, fragments of code
328 etc. For example:
329 : ;; Some Lisp examples
330 : (while (defc cnt)
331 : (ding))
332 will be looking just like this in also HTML. See also the QUOTE
333 keyword. Not all export backends support this.
335 This option can also be set with the #+OPTIONS line, e.g. \"::nil\"."
336 :group 'org-export-translation
337 :type 'boolean)
339 (defcustom org-export-with-footnotes t
340 "Non-nil means Org footnotes should be exported.
341 This option can also be set with the #+OPTIONS line,
342 e.g. \"f:nil\"."
343 :group 'org-export-general
344 :type 'boolean)
346 (defcustom org-export-headline-levels 3
347 "The last level which is still exported as a headline.
349 Inferior levels will produce itemize lists when exported. Note
350 that a numeric prefix argument to an exporter function overrides
351 this setting.
353 This option can also be set with the #+OPTIONS line, e.g. \"H:2\"."
354 :group 'org-export-general
355 :type 'integer)
357 (defcustom org-export-default-language "en"
358 "The default language for export and clocktable translations, as a string.
359 This may have an association in
360 `org-clock-clocktable-language-setup'."
361 :group 'org-export-general
362 :type '(string :tag "Language"))
364 (defcustom org-export-preserve-breaks nil
365 "Non-nil means preserve all line breaks when exporting.
367 Normally, in HTML output paragraphs will be reformatted.
369 This option can also be set with the #+OPTIONS line,
370 e.g. \"\\n:t\"."
371 :group 'org-export-general
372 :type 'boolean)
374 (defcustom org-export-with-entities t
375 "Non-nil means interpret entities when exporting.
377 For example, HTML export converts \\alpha to &alpha; and \\AA to
378 &Aring;.
380 For a list of supported names, see the constant `org-entities'
381 and the user option `org-entities-user'.
383 This option can also be set with the #+OPTIONS line,
384 e.g. \"e:nil\"."
385 :group 'org-export-general
386 :type 'boolean)
388 (defcustom org-export-with-priority nil
389 "Non-nil means include priority cookies in export.
390 When nil, remove priority cookies for export."
391 :group 'org-export-general
392 :type 'boolean)
394 (defcustom org-export-with-section-numbers t
395 "Non-nil means add section numbers to headlines when exporting.
397 When set to an integer n, numbering will only happen for
398 headlines whose relative level is higher or equal to n.
400 This option can also be set with the #+OPTIONS line,
401 e.g. \"num:t\"."
402 :group 'org-export-general
403 :type 'boolean)
405 (defcustom org-export-select-tags '("export")
406 "Tags that select a tree for export.
407 If any such tag is found in a buffer, all trees that do not carry
408 one of these tags will be deleted before export. Inside trees
409 that are selected like this, you can still deselect a subtree by
410 tagging it with one of the `org-export-exclude-tags'."
411 :group 'org-export-general
412 :type '(repeat (string :tag "Tag")))
414 (defcustom org-export-with-special-strings t
415 "Non-nil means interpret \"\-\", \"--\" and \"---\" for export.
417 When this option is turned on, these strings will be exported as:
419 Org HTML LaTeX
420 -----+----------+--------
421 \\- &shy; \\-
422 -- &ndash; --
423 --- &mdash; ---
424 ... &hellip; \ldots
426 This option can also be set with the #+OPTIONS line,
427 e.g. \"-:nil\"."
428 :group 'org-export-general
429 :type 'boolean)
431 (defcustom org-export-with-sub-superscripts t
432 "Non-nil means interpret \"_\" and \"^\" for export.
434 When this option is turned on, you can use TeX-like syntax for
435 sub- and superscripts. Several characters after \"_\" or \"^\"
436 will be considered as a single item - so grouping with {} is
437 normally not needed. For example, the following things will be
438 parsed as single sub- or superscripts.
440 10^24 or 10^tau several digits will be considered 1 item.
441 10^-12 or 10^-tau a leading sign with digits or a word
442 x^2-y^3 will be read as x^2 - y^3, because items are
443 terminated by almost any nonword/nondigit char.
444 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
446 Still, ambiguity is possible - so when in doubt use {} to enclose
447 the sub/superscript. If you set this variable to the symbol
448 `{}', the braces are *required* in order to trigger
449 interpretations as sub/superscript. This can be helpful in
450 documents that need \"_\" frequently in plain text.
452 This option can also be set with the #+OPTIONS line,
453 e.g. \"^:nil\"."
454 :group 'org-export-general
455 :type '(choice
456 (const :tag "Interpret them" t)
457 (const :tag "Curly brackets only" {})
458 (const :tag "Do not interpret them" nil)))
460 (defcustom org-export-with-toc t
461 "Non-nil means create a table of contents in exported files.
463 The TOC contains headlines with levels up
464 to`org-export-headline-levels'. When an integer, include levels
465 up to N in the toc, this may then be different from
466 `org-export-headline-levels', but it will not be allowed to be
467 larger than the number of headline levels. When nil, no table of
468 contents is made.
470 This option can also be set with the #+OPTIONS line,
471 e.g. \"toc:nil\" or \"toc:3\"."
472 :group 'org-export-general
473 :type '(choice
474 (const :tag "No Table of Contents" nil)
475 (const :tag "Full Table of Contents" t)
476 (integer :tag "TOC to level")))
478 (defcustom org-export-with-tables t
479 "If non-nil, lines starting with \"|\" define a table.
480 For example:
482 | Name | Address | Birthday |
483 |-------------+----------+-----------|
484 | Arthur Dent | England | 29.2.2100 |
486 This option can also be set with the #+OPTIONS line, e.g. \"|:nil\"."
487 :group 'org-export-general
488 :type 'boolean)
490 (defcustom org-export-with-tags t
491 "If nil, do not export tags, just remove them from headlines.
493 If this is the symbol `not-in-toc', tags will be removed from
494 table of contents entries, but still be shown in the headlines of
495 the document.
497 This option can also be set with the #+OPTIONS line,
498 e.g. \"tags:nil\"."
499 :group 'org-export-general
500 :type '(choice
501 (const :tag "Off" nil)
502 (const :tag "Not in TOC" not-in-toc)
503 (const :tag "On" t)))
505 (defcustom org-export-with-tasks t
506 "Non-nil means include TODO items for export.
507 This may have the following values:
508 t include tasks independent of state.
509 todo include only tasks that are not yet done.
510 done include only tasks that are already done.
511 nil remove all tasks before export
512 list of keywords keep only tasks with these keywords"
513 :group 'org-export-general
514 :type '(choice
515 (const :tag "All tasks" t)
516 (const :tag "No tasks" nil)
517 (const :tag "Not-done tasks" todo)
518 (const :tag "Only done tasks" done)
519 (repeat :tag "Specific TODO keywords"
520 (string :tag "Keyword"))))
522 (defcustom org-export-time-stamp-file t
523 "Non-nil means insert a time stamp into the exported file.
524 The time stamp shows when the file was created.
526 This option can also be set with the #+OPTIONS line,
527 e.g. \"timestamp:nil\"."
528 :group 'org-export-general
529 :type 'boolean)
531 (defcustom org-export-with-timestamps t
532 "If nil, do not export time stamps and associated keywords."
533 :group 'org-export-general
534 :type 'boolean)
536 (defcustom org-export-with-todo-keywords t
537 "Non-nil means include TODO keywords in export.
538 When nil, remove all these keywords from the export.")
540 (defcustom org-export-allow-BIND 'confirm
541 "Non-nil means allow #+BIND to define local variable values for export.
542 This is a potential security risk, which is why the user must
543 confirm the use of these lines."
544 :group 'org-export-general
545 :type '(choice
546 (const :tag "Never" nil)
547 (const :tag "Always" t)
548 (const :tag "Ask a confirmation for each file" confirm)))
550 (defcustom org-export-snippet-translation-alist nil
551 "Alist between export snippets back-ends and exporter back-ends.
553 This variable allows to provide shortcuts for export snippets.
555 For example, with a value of '\(\(\"h\" . \"html\"\)\), the HTML
556 back-end will recognize the contents of \"@h{<b>}\" as HTML code
557 while every other back-end will ignore it."
558 :group 'org-export-general
559 :type '(repeat
560 (cons
561 (string :tag "Shortcut")
562 (string :tag "Back-end"))))
564 (defcustom org-export-coding-system nil
565 "Coding system for the exported file."
566 :group 'org-export-general
567 :type 'coding-system)
569 (defcustom org-export-copy-to-kill-ring t
570 "Non-nil means exported stuff will also be pushed onto the kill ring."
571 :group 'org-export-general
572 :type 'boolean)
574 (defcustom org-export-initial-scope 'buffer
575 "The initial scope when exporting with `org-export-dispatch'.
576 This variable can be either set to `buffer' or `subtree'."
577 :group 'org-export-general
578 :type '(choice
579 (const :tag "Export current buffer" 'buffer)
580 (const :tag "Export current subtree" 'subtree)))
582 (defcustom org-export-show-temporary-export-buffer t
583 "Non-nil means show buffer after exporting to temp buffer.
584 When Org exports to a file, the buffer visiting that file is ever
585 shown, but remains buried. However, when exporting to a temporary
586 buffer, that buffer is popped up in a second window. When this variable
587 is nil, the buffer remains buried also in these cases."
588 :group 'org-export-general
589 :type 'boolean)
591 (defcustom org-export-dispatch-use-expert-ui nil
592 "Non-nil means using a non-intrusive `org-export-dispatch'.
593 In that case, no help buffer is displayed. Though, an indicator
594 for current export scope is added to the prompt \(i.e. \"b\" when
595 output is restricted to body only, \"s\" when it is restricted to
596 the current subtree and \"v\" when only visible elements are
597 considered for export\). Also, \[?] allows to switch back to
598 standard mode."
599 :group 'org-export-general
600 :type 'boolean)
604 ;;; The Communication Channel
606 ;; During export process, every function has access to a number of
607 ;; properties. They are of three types:
609 ;; 1. Export options are collected once at the very beginning of the
610 ;; process, out of the original buffer and environment. The task
611 ;; is handled by `org-export-collect-options' function.
613 ;; All export options are defined through the
614 ;; `org-export-option-alist' variable.
616 ;; 2. Tree properties are extracted directly from the parsed tree, by
617 ;; `org-export-collect-tree-properties' and depend on export
618 ;; options (whole trees may be filtered out of the export process).
620 ;; 3. Local options are updated during parsing, and their value
621 ;; depends on the level of recursion. For now, only `:genealogy'
622 ;; belongs to that category.
624 ;; Here is the full list of properties available during transcode
625 ;; process, with their category (option, tree or local), their
626 ;; value type and the function updating them, when appropriate.
628 ;; + `author' :: Author's name.
629 ;; - category :: option
630 ;; - type :: string
632 ;; + `back-end' :: Current back-end used for transcoding.
633 ;; - category :: tree
634 ;; - type :: symbol
636 ;; + `creator' :: String to write as creation information.
637 ;; - category :: option
638 ;; - type :: string
640 ;; + `date' :: String to use as date.
641 ;; - category :: option
642 ;; - type :: string
644 ;; + `description' :: Description text for the current data.
645 ;; - category :: option
646 ;; - type :: string
648 ;; + `email' :: Author's email.
649 ;; - category :: option
650 ;; - type :: string
652 ;; + `exclude-tags' :: Tags for exclusion of subtrees from export
653 ;; process.
654 ;; - category :: option
655 ;; - type :: list of strings
657 ;; + `footnote-definition-alist' :: Alist between footnote labels and
658 ;; their definition, as parsed data. Only non-inlined footnotes
659 ;; are represented in this alist. Also, every definition isn't
660 ;; guaranteed to be referenced in the parse tree. The purpose of
661 ;; this property is to preserve definitions from oblivion
662 ;; (i.e. when the parse tree comes from a part of the original
663 ;; buffer), it isn't meant for direct use in a back-end. To
664 ;; retrieve a definition relative to a reference, use
665 ;; `org-export-get-footnote-definition' instead.
666 ;; - category :: option
667 ;; - type :: alist (STRING . LIST)
669 ;; + `genealogy' :: Flat list of current object or element's parents
670 ;; from closest to farthest.
671 ;; - category :: local
672 ;; - type :: list of elements and objects
674 ;; + `headline-levels' :: Maximum level being exported as an
675 ;; headline. Comparison is done with the relative level of
676 ;; headlines in the parse tree, not necessarily with their
677 ;; actual level.
678 ;; - category :: option
679 ;; - type :: integer
681 ;; + `headline-offset' :: Difference between relative and real level
682 ;; of headlines in the parse tree. For example, a value of -1
683 ;; means a level 2 headline should be considered as level
684 ;; 1 (cf. `org-export-get-relative-level').
685 ;; - category :: tree
686 ;; - type :: integer
688 ;; + `headline-numbering' :: Alist between headlines' beginning
689 ;; position and their numbering, as a list of numbers
690 ;; (cf. `org-export-get-headline-number').
691 ;; - category :: tree
692 ;; - type :: alist (INTEGER . LIST)
694 ;; + `included-files' :: List of files, with full path, included in
695 ;; the current buffer, through the "#+include:" keyword. It is
696 ;; mainly used to verify that no infinite recursive inclusion
697 ;; happens.
698 ;; - category :: local
699 ;; - type :: list of strings
701 ;; + `keywords' :: List of keywords attached to data.
702 ;; - category :: option
703 ;; - type :: string
705 ;; + `language' :: Default language used for translations.
706 ;; - category :: option
707 ;; - type :: string
709 ;; + `parse-tree' :: Whole parse tree, available at any time during
710 ;; transcoding.
711 ;; - category :: global
712 ;; - type :: list (as returned by `org-element-parse-buffer')
714 ;; + `preserve-breaks' :: Non-nil means transcoding should preserve
715 ;; all line breaks.
716 ;; - category :: option
717 ;; - type :: symbol (nil, t)
719 ;; + `section-numbers' :: Non-nil means transcoding should add
720 ;; section numbers to headlines.
721 ;; - category :: option
722 ;; - type :: symbol (nil, t)
724 ;; + `select-tags' :: List of tags enforcing inclusion of sub-trees in
725 ;; transcoding. When such a tag is present,
726 ;; subtrees without it are de facto excluded from
727 ;; the process. See `use-select-tags'.
728 ;; - category :: option
729 ;; - type :: list of strings
731 ;; + `target-list' :: List of targets encountered in the parse tree.
732 ;; This is used to partly resolve "fuzzy" links
733 ;; (cf. `org-export-resolve-fuzzy-link').
734 ;; - category :: tree
735 ;; - type :: list of strings
737 ;; + `time-stamp-file' :: Non-nil means transcoding should insert
738 ;; a time stamp in the output.
739 ;; - category :: option
740 ;; - type :: symbol (nil, t)
742 ;; + `use-select-tags' :: When non-nil, a select tags has been found
743 ;; in the parse tree. Thus, any headline without one will be
744 ;; filtered out. See `select-tags'.
745 ;; - category :: tree
746 ;; - type :: interger or nil
748 ;; + `with-archived-trees' :: Non-nil when archived subtrees should
749 ;; also be transcoded. If it is set to the `headline' symbol,
750 ;; only the archived headline's name is retained.
751 ;; - category :: option
752 ;; - type :: symbol (nil, t, `headline')
754 ;; + `with-author' :: Non-nil means author's name should be included
755 ;; in the output.
756 ;; - category :: option
757 ;; - type :: symbol (nil, t)
759 ;; + `with-creator' :: Non-nild means a creation sentence should be
760 ;; inserted at the end of the transcoded string. If the value
761 ;; is `comment', it should be commented.
762 ;; - category :: option
763 ;; - type :: symbol (`comment', nil, t)
765 ;; + `with-drawers' :: Non-nil means drawers should be exported. If
766 ;; its value is a list of names, only drawers with such names
767 ;; will be transcoded.
768 ;; - category :: option
769 ;; - type :: symbol (nil, t) or list of strings
771 ;; + `with-email' :: Non-nil means output should contain author's
772 ;; email.
773 ;; - category :: option
774 ;; - type :: symbol (nil, t)
776 ;; + `with-emphasize' :: Non-nil means emphasized text should be
777 ;; interpreted.
778 ;; - category :: option
779 ;; - type :: symbol (nil, t)
781 ;; + `with-fixed-width' :: Non-nil if transcoder should interpret
782 ;; strings starting with a colon as a fixed-with (verbatim)
783 ;; area.
784 ;; - category :: option
785 ;; - type :: symbol (nil, t)
787 ;; + `with-footnotes' :: Non-nil if transcoder should interpret
788 ;; footnotes.
789 ;; - category :: option
790 ;; - type :: symbol (nil, t)
792 ;; + `with-priority' :: Non-nil means transcoding should include
793 ;; priority cookies.
794 ;; - category :: option
795 ;; - type :: symbol (nil, t)
797 ;; + `with-special-strings' :: Non-nil means transcoding should
798 ;; interpret special strings in plain text.
799 ;; - category :: option
800 ;; - type :: symbol (nil, t)
802 ;; + `with-sub-superscript' :: Non-nil means transcoding should
803 ;; interpret subscript and superscript. With a value of "{}",
804 ;; only interpret those using curly brackets.
805 ;; - category :: option
806 ;; - type :: symbol (nil, {}, t)
808 ;; + `with-tables' :: Non-nil means transcoding should interpret
809 ;; tables.
810 ;; - category :: option
811 ;; - type :: symbol (nil, t)
813 ;; + `with-tags' :: Non-nil means transcoding should keep tags in
814 ;; headlines. A `not-in-toc' value will remove them
815 ;; from the table of contents, if any, nonetheless.
816 ;; - category :: option
817 ;; - type :: symbol (nil, t, `not-in-toc')
819 ;; + `with-tasks' :: Non-nil means transcoding should include
820 ;; headlines with a TODO keyword. A `todo' value
821 ;; will only include headlines with a todo type
822 ;; keyword while a `done' value will do the
823 ;; contrary. If a list of strings is provided, only
824 ;; tasks with keywords belonging to that list will
825 ;; be kept.
826 ;; - category :: option
827 ;; - type :: symbol (t, todo, done, nil) or list of strings
829 ;; + `with-timestamps' :: Non-nil means transcoding should include
830 ;; time stamps and associated keywords. Otherwise, completely
831 ;; remove them.
832 ;; - category :: option
833 ;; - type :: symbol: (t, nil)
835 ;; + `with-toc' :: Non-nil means that a table of contents has to be
836 ;; added to the output. An integer value limits its
837 ;; depth.
838 ;; - category :: option
839 ;; - type :: symbol (nil, t or integer)
841 ;; + `with-todo-keywords' :: Non-nil means transcoding should
842 ;; include TODO keywords.
843 ;; - category :: option
844 ;; - type :: symbol (nil, t)
846 ;;;; Export Options
848 ;; Export options come from five sources, in increasing precedence
849 ;; order:
851 ;; - Global variables,
852 ;; - External options provided at export time,
853 ;; - Options keyword symbols,
854 ;; - Buffer keywords,
855 ;; - Subtree properties.
857 ;; The central internal function with regards to export options is
858 ;; `org-export-collect-options'. It updates global variables with
859 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
860 ;; the different sources.
862 ;; The internal functions doing the retrieval are:
863 ;; `org-export-parse-option-keyword' ,
864 ;; `org-export-get-subtree-options' ,
865 ;; `org-export-get-inbuffer-options' and
866 ;; `org-export-get-global-options'.
868 ;; Some properties do not rely on the previous sources but still
869 ;; depend on the original buffer are taken care of in
870 ;; `org-export-initial-options'.
872 ;; Also, `org-export-confirm-letbind' and `org-export-install-letbind'
873 ;; take care of the part relative to "#+BIND:" keywords.
875 (defun org-export-collect-options (backend subtreep ext-plist)
876 "Collect export options from the current buffer.
878 BACKEND is a symbol specifying the back-end to use.
880 When SUBTREEP is non-nil, assume the export is done against the
881 current sub-tree.
883 EXT-PLIST is a property list with external parameters overriding
884 org-mode's default settings, but still inferior to file-local
885 settings."
886 ;; First install #+BIND variables.
887 (org-export-install-letbind-maybe)
888 ;; Get and prioritize export options...
889 (let ((options (org-combine-plists
890 ;; ... from global variables...
891 (org-export-get-global-options backend)
892 ;; ... from buffer's name (default title)...
893 `(:title
894 ,(or (let ((file (buffer-file-name (buffer-base-buffer))))
895 (and file
896 (file-name-sans-extension
897 (file-name-nondirectory file))))
898 (buffer-name (buffer-base-buffer))))
899 ;; ... from an external property list...
900 ext-plist
901 ;; ... from in-buffer settings...
902 (org-export-get-inbuffer-options
903 (org-with-wide-buffer (buffer-string)) backend
904 (and buffer-file-name
905 (org-remove-double-quotes buffer-file-name)))
906 ;; ... and from subtree, when appropriate.
907 (and subtreep
908 (org-export-get-subtree-options)))))
909 ;; Add initial options.
910 (setq options (append (org-export-initial-options options) options))
911 ;; Return plist.
912 options))
914 (defun org-export-parse-option-keyword (options backend)
915 "Parse an OPTIONS line and return values as a plist.
916 BACKEND is a symbol specifying the back-end to use."
917 (let* ((all (append org-export-option-alist
918 (let ((var (intern
919 (format "org-%s-option-alist" backend))))
920 (and (boundp var) (eval var)))))
921 ;; Build an alist between #+OPTION: item and property-name.
922 (alist (delq nil
923 (mapcar (lambda (e)
924 (when (nth 2 e) (cons (regexp-quote (nth 2 e))
925 (car e))))
926 all)))
927 plist)
928 (mapc (lambda (e)
929 (when (string-match (concat "\\(\\`\\|[ \t]\\)"
930 (car e)
931 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
932 options)
933 (setq plist (plist-put plist
934 (cdr e)
935 (car (read-from-string
936 (match-string 2 options)))))))
937 alist)
938 plist))
940 (defun org-export-get-subtree-options ()
941 "Get export options in subtree at point.
943 Assume point is at subtree's beginning.
945 Return options as a plist."
946 (let (prop plist)
947 (when (setq prop (progn (looking-at org-todo-line-regexp)
948 (or (save-match-data
949 (org-entry-get (point) "EXPORT_TITLE"))
950 (org-match-string-no-properties 3))))
951 (setq plist
952 (plist-put
953 plist :title
954 (org-element-parse-secondary-string
955 prop
956 (cdr (assq 'keyword org-element-string-restrictions))))))
957 (when (setq prop (org-entry-get (point) "EXPORT_TEXT"))
958 (setq plist (plist-put plist :text prop)))
959 (when (setq prop (org-entry-get (point) "EXPORT_AUTHOR"))
960 (setq plist (plist-put plist :author prop)))
961 (when (setq prop (org-entry-get (point) "EXPORT_DATE"))
962 (setq plist (plist-put plist :date prop)))
963 (when (setq prop (org-entry-get (point) "EXPORT_OPTIONS"))
964 (setq plist (org-export-add-options-to-plist plist prop)))
965 plist))
967 (defun org-export-get-inbuffer-options (buffer-string backend files)
968 "Return in-buffer options as a plist.
969 BUFFER-STRING is the string of the buffer. BACKEND is a symbol
970 specifying which back-end should be used. FILES is a list of
971 setup files names read so far, used to avoid circular
972 dependencies."
973 (let ((case-fold-search t) plist)
974 ;; 1. Special keywords, as in `org-export-special-keywords'.
975 (let ((start 0)
976 (special-re (org-make-options-regexp org-export-special-keywords)))
977 (while (string-match special-re buffer-string start)
978 (setq start (match-end 0))
979 (let ((key (upcase (org-match-string-no-properties 1 buffer-string)))
980 ;; Special keywords do not have their value expanded.
981 (val (org-match-string-no-properties 2 buffer-string)))
982 (setq plist
983 (org-combine-plists
984 (cond
985 ((string= key "SETUP_FILE")
986 (let ((file (expand-file-name
987 (org-remove-double-quotes (org-trim val)))))
988 ;; Avoid circular dependencies.
989 (unless (member file files)
990 (org-export-get-inbuffer-options
991 (org-file-contents file 'noerror)
992 backend
993 (cons file files)))))
994 ((string= key "OPTIONS")
995 (org-export-parse-option-keyword val backend))
996 ((string= key "MACRO")
997 (when (string-match
998 "^\\([-a-zA-Z0-9_]+\\)\\(?:[ \t]+\\(.*?\\)[ \t]*$\\)?"
999 val)
1000 (let ((key (intern
1001 (concat ":macro-"
1002 (downcase (match-string 1 val)))))
1003 (value (match-string 2 val)))
1004 (cond
1005 ((not value) "")
1006 ((string-match "\\`(eval\\>" value) (list key value))
1008 (list
1010 ;; If user explicitly asks for a newline, be
1011 ;; sure to preserve it from further filling
1012 ;; with `hard-newline'.
1013 (replace-regexp-in-string
1014 "\\\\n" hard-newline value))))))))
1015 plist)))))
1016 ;; 2. Standard options, as in `org-export-option-alist'.
1017 (let* ((all (append org-export-option-alist
1018 (let ((var (intern
1019 (format "org-%s-option-alist" backend))))
1020 (and (boundp var) (eval var)))))
1021 ;; Build alist between keyword name and property name.
1022 (alist (delq nil (mapcar (lambda (e)
1023 (when (nth 1 e) (cons (nth 1 e) (car e))))
1024 all)))
1025 ;; Build regexp matching all keywords associated to export
1026 ;; options. Note: the search is case insensitive.
1027 (opt-re (org-make-options-regexp
1028 (delq nil (mapcar (lambda (e) (nth 1 e)) all))))
1029 (start 0))
1030 (while (string-match opt-re buffer-string start)
1031 (setq start (match-end 0))
1032 (let* ((key (upcase (org-match-string-no-properties 1 buffer-string)))
1033 ;; Expand value, applying restrictions for keywords.
1034 (val (org-match-string-no-properties 2 buffer-string))
1035 (prop (cdr (assoc key alist)))
1036 (behaviour (nth 4 (assq prop all))))
1037 (setq plist
1038 (plist-put
1039 plist prop
1040 ;; Handle value depending on specified BEHAVIOUR.
1041 (case behaviour
1042 (space (if (plist-get plist prop)
1043 (concat (plist-get plist prop) " " (org-trim val))
1044 (org-trim val)))
1045 (newline (org-trim
1046 (concat
1047 (plist-get plist prop) "\n" (org-trim val))))
1048 (split `(,@(plist-get plist prop) ,@(org-split-string val)))
1049 ('t val)
1050 (otherwise (plist-get plist prop)))))))
1051 ;; Parse keywords specified in `org-element-parsed-keywords'.
1052 (mapc
1053 (lambda (key)
1054 (let* ((prop (cdr (assoc (upcase key) alist)))
1055 (value (and prop (plist-get plist prop))))
1056 (when (stringp value)
1057 (setq plist
1058 (plist-put
1059 plist prop
1060 (org-element-parse-secondary-string
1061 value
1062 (cdr (assq 'keyword org-element-string-restrictions))))))))
1063 org-element-parsed-keywords))
1064 ;; Return final value.
1065 plist))
1067 (defun org-export-get-global-options (backend)
1068 "Return global export options as a plist.
1069 BACKEND is a symbol specifying which back-end should be used."
1070 (let ((all (append org-export-option-alist
1071 (let ((var (intern
1072 (format "org-%s-option-alist" backend))))
1073 (and (boundp var) (eval var)))))
1074 ;; Output value.
1075 plist)
1076 (mapc (lambda (cell)
1077 (setq plist
1078 (plist-put plist (car cell) (eval (nth 3 cell)))))
1079 all)
1080 ;; Return value.
1081 plist))
1083 (defun org-export-initial-options (options)
1084 "Return a plist with non-optional properties.
1085 OPTIONS is the export options plist computed so far."
1086 (list
1087 ;; `:macro-date', `:macro-time' and `:macro-property' could as well
1088 ;; be initialized as tree properties, since they don't depend on
1089 ;; initial environment. Though, it may be more logical to keep
1090 ;; them close to other ":macro-" properties.
1091 :macro-date "(eval (format-time-string \"$1\"))"
1092 :macro-time "(eval (format-time-string \"$1\"))"
1093 :macro-property "(eval (org-entry-get nil \"$1\" 'selective))"
1094 :macro-modification-time
1095 (and (buffer-file-name)
1096 (file-exists-p (buffer-file-name))
1097 (concat "(eval (format-time-string \"$1\" '"
1098 (prin1-to-string (nth 5 (file-attributes (buffer-file-name))))
1099 "))"))
1100 :macro-input-file (and (buffer-file-name)
1101 (file-name-nondirectory (buffer-file-name)))
1102 ;; Footnotes definitions must be collected in the original buffer,
1103 ;; as there's no insurance that they will still be in the parse
1104 ;; tree, due to some narrowing.
1105 :footnote-definition-alist
1106 (let (alist)
1107 (org-with-wide-buffer
1108 (goto-char (point-min))
1109 (while (re-search-forward org-footnote-definition-re nil t)
1110 (let ((def (org-footnote-at-definition-p)))
1111 (when def
1112 (org-skip-whitespace)
1113 (push (cons (car def)
1114 (save-restriction
1115 (narrow-to-region (point) (nth 2 def))
1116 ;; Like `org-element-parse-buffer', but
1117 ;; makes sure the definition doesn't start
1118 ;; with a section element.
1119 (nconc
1120 (list 'org-data nil)
1121 (org-element-parse-elements
1122 (point-min) (point-max) nil nil nil nil nil))))
1123 alist))))
1124 alist))))
1126 (defvar org-export-allow-BIND-local nil)
1127 (defun org-export-confirm-letbind ()
1128 "Can we use #+BIND values during export?
1129 By default this will ask for confirmation by the user, to divert
1130 possible security risks."
1131 (cond
1132 ((not org-export-allow-BIND) nil)
1133 ((eq org-export-allow-BIND t) t)
1134 ((local-variable-p 'org-export-allow-BIND-local) org-export-allow-BIND-local)
1135 (t (org-set-local 'org-export-allow-BIND-local
1136 (yes-or-no-p "Allow BIND values in this buffer? ")))))
1138 (defun org-export-install-letbind-maybe ()
1139 "Install the values from #+BIND lines as local variables.
1140 Variables must be installed before in-buffer options are
1141 retrieved."
1142 (let (letbind pair)
1143 (org-with-wide-buffer
1144 (goto-char (point-min))
1145 (while (re-search-forward (org-make-options-regexp '("BIND")) nil t)
1146 (when (org-export-confirm-letbind)
1147 (push (read (concat "(" (org-match-string-no-properties 2) ")"))
1148 letbind))))
1149 (while (setq pair (pop letbind))
1150 (org-set-local (car pair) (nth 1 pair)))))
1153 ;;;; Tree Properties
1155 ;; They are initialized at the beginning of the transcoding process by
1156 ;; `org-export-collect-tree-properties'.
1158 ;; Dedicated functions focus on computing the value of specific tree
1159 ;; properties during initialization. Thus,
1160 ;; `org-export-use-select-tag-p' determines if an headline makes use
1161 ;; of an export tag enforcing inclusion. `org-export-get-min-level'
1162 ;; gets the minimal exportable level, used as a basis to compute
1163 ;; relative level for headlines. `org-export-get-point-max' returns
1164 ;; the maximum exportable ending position in the parse tree.
1165 ;; Eventually `org-export-collect-headline-numbering' builds an alist
1166 ;; between headlines' beginning position and their numbering.
1168 (defun org-export-collect-tree-properties (data info backend)
1169 "Extract tree properties from parse tree.
1171 DATA is the parse tree from which information is retrieved. INFO
1172 is a list holding export options. BACKEND is the back-end called
1173 for transcoding, as a symbol.
1175 Following tree properties are set:
1176 `:back-end' Back-end used for transcoding.
1178 `:headline-offset' Offset between true level of headlines and
1179 local level. An offset of -1 means an headline
1180 of level 2 should be considered as a level
1181 1 headline in the context.
1183 `:headline-numbering' Alist of all headlines' beginning position
1184 as key an the associated numbering as value.
1186 `:parse-tree' Whole parse tree.
1188 `:target-list' List of all targets in the parse tree.
1190 `:use-select-tags' Non-nil when parsed tree use a special tag to
1191 enforce transcoding of the headline."
1192 ;; First, set `:use-select-tags' property, as it will be required
1193 ;; for further computations.
1194 (setq info
1195 (org-combine-plists
1196 info `(:use-select-tags ,(org-export-use-select-tags-p data info))))
1197 ;; Then get `:headline-offset' in order to be able to use
1198 ;; `org-export-get-relative-level'.
1199 (setq info
1200 (org-combine-plists
1201 info `(:headline-offset ,(- 1 (org-export-get-min-level data info)))))
1202 ;; Now, get the rest of the tree properties, now `:use-select-tags'
1203 ;; is set...
1204 (nconc
1205 `(:parse-tree
1206 ,data
1207 :target-list
1208 ,(org-element-map data 'target (lambda (target local) target) info)
1209 :headline-numbering ,(org-export-collect-headline-numbering data info)
1210 :back-end ,backend)
1211 info))
1213 (defun org-export-use-select-tags-p (data options)
1214 "Non-nil when data use a tag enforcing transcoding.
1215 DATA is parsed data as returned by `org-element-parse-buffer'.
1216 OPTIONS is a plist holding export options."
1217 (org-element-map
1218 data
1219 'headline
1220 (lambda (headline info)
1221 (let ((tags (org-element-get-property :with-tags headline)))
1222 (and tags (string-match
1223 (format ":%s:" (plist-get info :select-tags)) tags))))
1224 options
1225 'stop-at-first-match))
1227 (defun org-export-get-min-level (data options)
1228 "Return minimum exportable headline's level in DATA.
1229 DATA is parsed tree as returned by `org-element-parse-buffer'.
1230 OPTIONS is a plist holding export options."
1231 (catch 'exit
1232 (let ((min-level 10000))
1233 (mapc (lambda (blob)
1234 (when (and (eq (car blob) 'headline)
1235 (not (org-export-skip-p blob options)))
1236 (setq min-level
1237 (min (org-element-get-property :level blob) min-level)))
1238 (when (= min-level 1) (throw 'exit 1)))
1239 (org-element-get-contents data))
1240 ;; If no headline was found, for the sake of consistency, set
1241 ;; minimum level to 1 nonetheless.
1242 (if (= min-level 10000) 1 min-level))))
1244 (defun org-export-collect-headline-numbering (data options)
1245 "Return numbering of all exportable headlines in a parse tree.
1247 DATA is the parse tree. OPTIONS is the plist holding export
1248 options.
1250 Return an alist whose key is an headline and value is its
1251 associated numbering \(in the shape of a list of numbers\)."
1252 (let ((numbering (make-vector org-export-max-depth 0)))
1253 (org-element-map
1254 data
1255 'headline
1256 (lambda (headline info)
1257 (let ((relative-level
1258 (1- (org-export-get-relative-level headline info))))
1259 (cons
1260 headline
1261 (loop for n across numbering
1262 for idx from 0 to org-export-max-depth
1263 when (< idx relative-level) collect n
1264 when (= idx relative-level) collect (aset numbering idx (1+ n))
1265 when (> idx relative-level) do (aset numbering idx 0)))))
1266 options)))
1270 ;;; The Transcoder
1272 ;; This function reads Org data (obtained with, i.e.
1273 ;; `org-element-parse-buffer') and transcodes it into a specified
1274 ;; back-end output. It takes care of updating local properties,
1275 ;; filtering out elements or objects according to export options and
1276 ;; organizing the output blank lines and white space are preserved.
1278 ;; Though, this function is inapropriate for secondary strings, which
1279 ;; require a fresh copy of the plist passed as INFO argument. Thus,
1280 ;; `org-export-secondary-string' is provided for that specific task.
1282 ;; Internally, three functions handle the filtering of objects and
1283 ;; elements during the export. More precisely, `org-export-skip-p'
1284 ;; determines if the considered object or element should be ignored
1285 ;; altogether, `org-export-interpret-p' tells which elements or
1286 ;; objects should be seen as real Org syntax and `org-export-expand'
1287 ;; transforms the others back into their original shape.
1289 (defun org-export-data (data backend info)
1290 "Convert DATA to a string into BACKEND format.
1292 DATA is a nested list as returned by `org-element-parse-buffer'.
1294 BACKEND is a symbol among supported exporters.
1296 INFO is a plist holding export options and also used as
1297 a communication channel between elements when walking the nested
1298 list. See `org-export-update-info' function for more
1299 details.
1301 Return transcoded string."
1302 (mapconcat
1303 ;; BLOB can be an element, an object, a string, or nil.
1304 (lambda (blob)
1305 (cond
1306 ((not blob) nil)
1307 ;; BLOB is a string. Check if the optional transcoder for plain
1308 ;; text exists, and call it in that case. Otherwise, simply
1309 ;; return string. Also update INFO and call
1310 ;; `org-export-filter-plain-text-functions'.
1311 ((stringp blob)
1312 (let ((transcoder (intern (format "org-%s-plain-text" backend))))
1313 (org-export-filter-apply-functions
1314 (plist-get info :filter-plain-text)
1315 (if (fboundp transcoder) (funcall transcoder blob info) blob)
1316 backend info)))
1317 ;; BLOB is an element or an object.
1319 (let* ((type (if (stringp blob) 'plain-text (car blob)))
1320 ;; 1. Determine the appropriate TRANSCODER.
1321 (transcoder
1322 (cond
1323 ;; 1.0 A full Org document is inserted.
1324 ((eq type 'org-data) 'identity)
1325 ;; 1.1. BLOB should be ignored.
1326 ((org-export-skip-p blob info) nil)
1327 ;; 1.2. BLOB shouldn't be transcoded. Interpret it
1328 ;; back into Org syntax.
1329 ((not (org-export-interpret-p blob info))
1330 'org-export-expand)
1331 ;; 1.3. Else apply naming convention.
1332 (t (let ((trans (intern
1333 (format "org-%s-%s" backend type))))
1334 (and (fboundp trans) trans)))))
1335 ;; 2. Compute CONTENTS of BLOB.
1336 (contents
1337 (cond
1338 ;; Case 0. No transcoder defined: ignore BLOB.
1339 ((not transcoder) nil)
1340 ;; Case 1. Transparently export an Org document.
1341 ((eq type 'org-data) (org-export-data blob backend info))
1342 ;; Case 2. For a recursive object.
1343 ((memq type org-element-recursive-objects)
1344 (org-export-data
1345 blob backend
1346 (org-combine-plists
1347 info
1348 `(:genealogy ,(cons blob (plist-get info :genealogy))))))
1349 ;; Case 3. For a recursive element.
1350 ((memq type org-element-greater-elements)
1351 ;; Ignore contents of an archived tree
1352 ;; when `:with-archived-trees' is `headline'.
1353 (unless (and
1354 (eq type 'headline)
1355 (eq (plist-get info :with-archived-trees) 'headline)
1356 (org-element-get-property :archivedp blob))
1357 (org-element-normalize-string
1358 (org-export-data
1359 blob backend
1360 (org-combine-plists
1361 info `(:genealogy
1362 ,(cons blob (plist-get info :genealogy))))))))
1363 ;; Case 4. For a paragraph.
1364 ((eq type 'paragraph)
1365 (let ((paragraph
1366 (org-element-normalize-contents
1367 blob
1368 ;; When normalizing contents of an item or
1369 ;; a footnote definition, ignore first line's
1370 ;; indentation: there is none and it might be
1371 ;; misleading.
1372 (and (not (org-export-get-previous-element blob info))
1373 (let ((parent (caar (plist-get info :genealogy))))
1374 (memq parent '(footnote-definition item)))))))
1375 (org-export-data
1376 paragraph backend
1377 (org-combine-plists
1378 info `(:genealogy
1379 ,(cons paragraph (plist-get info :genealogy)))))))))
1380 ;; 3. Transcode BLOB into RESULTS string.
1381 (results (cond
1382 ((not transcoder) nil)
1383 ((eq transcoder 'org-export-expand)
1384 (org-export-data
1385 `(org-data nil ,(funcall transcoder blob contents))
1386 backend info))
1387 (t (funcall transcoder blob contents info)))))
1388 ;; 4. Discard nil results. Otherwise, update INFO, append
1389 ;; the same white space between elements or objects as in
1390 ;; the original buffer, and call appropriate filters.
1391 (when results
1392 ;; No filter for a full document.
1393 (if (eq type 'org-data) results
1394 (org-export-filter-apply-functions
1395 (plist-get info (intern (format ":filter-%s" type)))
1396 (let ((post-blank (org-element-get-property :post-blank blob)))
1397 (if (memq type org-element-all-elements)
1398 (concat (org-element-normalize-string results)
1399 (make-string post-blank ?\n))
1400 (concat results (make-string post-blank ? ))))
1401 backend info)))))))
1402 (org-element-get-contents data) ""))
1404 (defun org-export-secondary-string (secondary backend info)
1405 "Convert SECONDARY string into BACKEND format.
1407 SECONDARY is a nested list as returned by
1408 `org-element-parse-secondary-string'.
1410 BACKEND is a symbol among supported exporters. INFO is a plist
1411 used as a communication channel.
1413 Return transcoded string."
1414 ;; Make SECONDARY acceptable for `org-export-data'.
1415 (let ((s (if (listp secondary) secondary (list secondary))))
1416 (org-export-data `(org-data nil ,@s) backend (copy-sequence info))))
1418 (defun org-export-skip-p (blob info)
1419 "Non-nil when element or object BLOB should be skipped during export.
1420 INFO is the plist holding export options."
1421 ;; Check headline.
1422 (unless (stringp blob)
1423 (case (car blob)
1424 ('headline
1425 (let ((with-tasks (plist-get info :with-tasks))
1426 (todo (org-element-get-property :todo-keyword blob))
1427 (todo-type (org-element-get-property :todo-type blob))
1428 (archived (plist-get info :with-archived-trees))
1429 (tag-list (let ((tags (org-element-get-property :tags blob)))
1430 (and tags (org-split-string tags ":")))))
1432 ;; Ignore subtrees with an exclude tag.
1433 (loop for k in (plist-get info :exclude-tags)
1434 thereis (member k tag-list))
1435 ;; Ignore subtrees without a select tag, when such tag is found
1436 ;; in the buffer.
1437 (and (plist-get info :use-select-tags)
1438 (loop for k in (plist-get info :select-tags)
1439 never (member k tag-list)))
1440 ;; Ignore commented sub-trees.
1441 (org-element-get-property :commentedp blob)
1442 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
1443 (and (not archived) (org-element-get-property :archivedp blob))
1444 ;; Ignore tasks, if specified by `:with-tasks' property.
1445 (and todo (not with-tasks))
1446 (and todo
1447 (memq with-tasks '(todo done))
1448 (not (eq todo-type with-tasks)))
1449 (and todo
1450 (consp with-tasks)
1451 (not (member todo with-tasks))))))
1452 ;; Check time-stamp.
1453 ('time-stamp (not (plist-get info :with-timestamps)))
1454 ;; Check drawer.
1455 ('drawer
1456 (or (not (plist-get info :with-drawers))
1457 (and (consp (plist-get info :with-drawers))
1458 (not (member (org-element-get-property :drawer-name blob)
1459 (plist-get info :with-drawers))))))
1460 ;; Check export snippet.
1461 ('export-snippet
1462 (let* ((raw-back-end (org-element-get-property :back-end blob))
1463 (true-back-end
1464 (or (cdr (assoc raw-back-end org-export-snippet-translation-alist))
1465 raw-back-end)))
1466 (not (string= (symbol-name (plist-get info :back-end))
1467 true-back-end)))))))
1469 (defun org-export-interpret-p (blob info)
1470 "Non-nil if element or object BLOB should be interpreted as Org syntax.
1471 Check is done according to export options INFO, stored as
1472 a plist."
1473 (case (car blob)
1474 ;; ... entities...
1475 (entity (plist-get info :with-entities))
1476 ;; ... emphasis...
1477 (emphasis (plist-get info :with-emphasize))
1478 ;; ... fixed-width areas.
1479 (fixed-width (plist-get info :with-fixed-width))
1480 ;; ... footnotes...
1481 ((footnote-definition footnote-reference)
1482 (plist-get info :with-footnotes))
1483 ;; ... sub/superscripts...
1484 ((subscript superscript)
1485 (let ((sub/super-p (plist-get info :with-sub-superscript)))
1486 (if (eq sub/super-p '{})
1487 (org-element-get-property :use-brackets-p blob)
1488 sub/super-p)))
1489 ;; ... tables...
1490 (table (plist-get info :with-tables))
1491 (otherwise t)))
1493 (defsubst org-export-expand (blob contents)
1494 "Expand a parsed element or object to its original state.
1495 BLOB is either an element or an object. CONTENTS is its
1496 contents, as a string or nil."
1497 (funcall
1498 (intern (format "org-element-%s-interpreter" (car blob))) blob contents))
1502 ;;; The Filter System
1504 ;; Filters allow end-users to tweak easily the transcoded output.
1505 ;; They are the functional counterpart of hooks, as every filter in
1506 ;; a set is applied to the return value of the previous one.
1508 ;; Every set is back-end agnostic. Although, a filter is always
1509 ;; called, in addition to the string it applies to, with the back-end
1510 ;; used as argument, so it's easy enough for the end-user to add
1511 ;; back-end specific filters in the set. The communication channel,
1512 ;; as a plist, is required as the third argument.
1514 ;; Filters sets are defined below. There are of four types:
1516 ;; - `org-export-filter-parse-tree-functions' applies directly on the
1517 ;; complete parsed tree. It's the only filters set that doesn't
1518 ;; apply to a string.
1519 ;; - `org-export-filter-final-output-functions' applies to the final
1520 ;; transcoded string.
1521 ;; - `org-export-filter-plain-text-functions' applies to any string
1522 ;; not recognized as Org syntax.
1523 ;; - `org-export-filter-TYPE-functions' applies on the string returned
1524 ;; after an element or object of type TYPE has been transcoded.
1526 ;; All filters sets are applied through
1527 ;; `org-export-filter-apply-functions' function. Filters in a set are
1528 ;; applied in a LIFO fashion. It allows developers to be sure that
1529 ;; their filters will be applied first.
1531 ;;;; Special Filters
1532 (defvar org-export-filter-parse-tree-functions nil
1533 "Filter, or list of filters, applied to the parsed tree.
1534 Each filter is called with three arguments: the parse tree, as
1535 returned by `org-element-parse-buffer', the back-end, as
1536 a symbol, and the communication channel, as a plist. It must
1537 return the modified parse tree to transcode.")
1539 (defvar org-export-filter-final-output-functions nil
1540 "Filter, or list of filters, applied to the transcoded string.
1541 Each filter is called with three arguments: the full transcoded
1542 string, the back-end, as a symbol, and the communication channel,
1543 as a plist. It must return a string that will be used as the
1544 final export output.")
1546 (defvar org-export-filter-plain-text-functions nil
1547 "Filter, or list of filters, applied to plain text.
1548 Each filter is called with three arguments: a string which
1549 contains no Org syntax, the back-end, as a symbol, and the
1550 communication channel, as a plist. It must return a string or
1551 nil.")
1554 ;;;; Elements Filters
1556 (defvar org-export-filter-center-block-functions nil
1557 "List of functions applied to a transcoded center block.
1558 Each filter is called with three arguments: the transcoded center
1559 block, as a string, the back-end, as a symbol, and the
1560 communication channel, as a plist. It must return a string or
1561 nil.")
1563 (defvar org-export-filter-drawer-functions nil
1564 "List of functions applied to a transcoded drawer.
1565 Each filter is called with three arguments: the transcoded
1566 drawer, as a string, the back-end, as a symbol, and the
1567 communication channel, as a plist. It must return a string or
1568 nil.")
1570 (defvar org-export-filter-dynamic-block-functions nil
1571 "List of functions applied to a transcoded dynamic-block.
1572 Each filter is called with three arguments: the transcoded
1573 dynamic-block, as a string, the back-end, as a symbol, and the
1574 communication channel, as a plist. It must return a string or
1575 nil.")
1577 (defvar org-export-filter-headline-functions nil
1578 "List of functions applied to a transcoded headline.
1579 Each filter is called with three arguments: the transcoded
1580 headline, as a string, the back-end, as a symbol, and the
1581 communication channel, as a plist. It must return a string or
1582 nil.")
1584 (defvar org-export-filter-inlinetask-functions nil
1585 "List of functions applied to a transcoded inlinetask.
1586 Each filter is called with three arguments: the transcoded
1587 inlinetask, as a string, the back-end, as a symbol, and the
1588 communication channel, as a plist. It must return a string or
1589 nil.")
1591 (defvar org-export-filter-plain-list-functions nil
1592 "List of functions applied to a transcoded plain-list.
1593 Each filter is called with three arguments: the transcoded
1594 plain-list, as a string, the back-end, as a symbol, and the
1595 communication channel, as a plist. It must return a string or
1596 nil.")
1598 (defvar org-export-filter-item-functions nil
1599 "List of functions applied to a transcoded item.
1600 Each filter is called with three arguments: the transcoded item,
1601 as a string, the back-end, as a symbol, and the communication
1602 channel, as a plist. It must return a string or nil.")
1604 (defvar org-export-filter-comment-functions nil
1605 "List of functions applied to a transcoded comment.
1606 Each filter is called with three arguments: the transcoded
1607 comment, as a string, the back-end, as a symbol, and the
1608 communication channel, as a plist. It must return a string or
1609 nil.")
1611 (defvar org-export-filter-comment-block-functions nil
1612 "List of functions applied to a transcoded comment-comment.
1613 Each filter is called with three arguments: the transcoded
1614 comment-block, as a string, the back-end, as a symbol, and the
1615 communication channel, as a plist. It must return a string or
1616 nil.")
1618 (defvar org-export-filter-example-block-functions nil
1619 "List of functions applied to a transcoded example-block.
1620 Each filter is called with three arguments: the transcoded
1621 example-block, as a string, the back-end, as a symbol, and the
1622 communication channel, as a plist. It must return a string or
1623 nil.")
1625 (defvar org-export-filter-export-block-functions nil
1626 "List of functions applied to a transcoded export-block.
1627 Each filter is called with three arguments: the transcoded
1628 export-block, as a string, the back-end, as a symbol, and the
1629 communication channel, as a plist. It must return a string or
1630 nil.")
1632 (defvar org-export-filter-fixed-width-functions nil
1633 "List of functions applied to a transcoded fixed-width.
1634 Each filter is called with three arguments: the transcoded
1635 fixed-width, as a string, the back-end, as a symbol, and the
1636 communication channel, as a plist. It must return a string or
1637 nil.")
1639 (defvar org-export-filter-footnote-definition-functions nil
1640 "List of functions applied to a transcoded footnote-definition.
1641 Each filter is called with three arguments: the transcoded
1642 footnote-definition, as a string, the back-end, as a symbol, and
1643 the communication channel, as a plist. It must return a string
1644 or nil.")
1646 (defvar org-export-filter-horizontal-rule-functions nil
1647 "List of functions applied to a transcoded horizontal-rule.
1648 Each filter is called with three arguments: the transcoded
1649 horizontal-rule, as a string, the back-end, as a symbol, and the
1650 communication channel, as a plist. It must return a string or
1651 nil.")
1653 (defvar org-export-filter-keyword-functions nil
1654 "List of functions applied to a transcoded keyword.
1655 Each filter is called with three arguments: the transcoded
1656 keyword, as a string, the back-end, as a symbol, and the
1657 communication channel, as a plist. It must return a string or
1658 nil.")
1660 (defvar org-export-filter-latex-environment-functions nil
1661 "List of functions applied to a transcoded latex-environment.
1662 Each filter is called with three arguments: the transcoded
1663 latex-environment, as a string, the back-end, as a symbol, and
1664 the communication channel, as a plist. It must return a string
1665 or nil.")
1667 (defvar org-export-filter-babel-call-functions nil
1668 "List of functions applied to a transcoded babel-call.
1669 Each filter is called with three arguments: the transcoded
1670 babel-call, as a string, the back-end, as a symbol, and the
1671 communication channel, as a plist. It must return a string or
1672 nil.")
1674 (defvar org-export-filter-paragraph-functions nil
1675 "List of functions applied to a transcoded paragraph.
1676 Each filter is called with three arguments: the transcoded
1677 paragraph, as a string, the back-end, as a symbol, and the
1678 communication channel, as a plist. It must return a string or
1679 nil.")
1681 (defvar org-export-filter-property-drawer-functions nil
1682 "List of functions applied to a transcoded property-drawer.
1683 Each filter is called with three arguments: the transcoded
1684 property-drawer, as a string, the back-end, as a symbol, and the
1685 communication channel, as a plist. It must return a string or
1686 nil.")
1688 (defvar org-export-filter-quote-block-functions nil
1689 "List of functions applied to a transcoded quote block.
1690 Each filter is called with three arguments: the transcoded quote
1691 block, as a string, the back-end, as a symbol, and the
1692 communication channel, as a plist. It must return a string or
1693 nil.")
1695 (defvar org-export-filter-quote-section-functions nil
1696 "List of functions applied to a transcoded quote-section.
1697 Each filter is called with three arguments: the transcoded
1698 quote-section, as a string, the back-end, as a symbol, and the
1699 communication channel, as a plist. It must return a string or
1700 nil.")
1702 (defvar org-export-filter-section-functions nil
1703 "List of functions applied to a transcoded section.
1704 Each filter is called with three arguments: the transcoded
1705 section, as a string, the back-end, as a symbol, and the
1706 communication channel, as a plist. It must return a string or
1707 nil.")
1709 (defvar org-export-filter-special-block-functions nil
1710 "List of functions applied to a transcoded special block.
1711 Each filter is called with three arguments: the transcoded
1712 special block, as a string, the back-end, as a symbol, and the
1713 communication channel, as a plist. It must return a string or
1714 nil.")
1716 (defvar org-export-filter-src-block-functions nil
1717 "List of functions applied to a transcoded src-block.
1718 Each filter is called with three arguments: the transcoded
1719 src-block, as a string, the back-end, as a symbol, and the
1720 communication channel, as a plist. It must return a string or
1721 nil.")
1723 (defvar org-export-filter-table-functions nil
1724 "List of functions applied to a transcoded table.
1725 Each filter is called with three arguments: the transcoded table,
1726 as a string, the back-end, as a symbol, and the communication
1727 channel, as a plist. It must return a string or nil.")
1729 (defvar org-export-filter-verse-block-functions nil
1730 "List of functions applied to a transcoded verse block.
1731 Each filter is called with three arguments: the transcoded verse
1732 block, as a string, the back-end, as a symbol, and the
1733 communication channel, as a plist. It must return a string or
1734 nil.")
1737 ;;;; Objects Filters
1739 (defvar org-export-filter-emphasis-functions nil
1740 "List of functions applied to a transcoded emphasis.
1741 Each filter is called with three arguments: the transcoded
1742 emphasis, as a string, the back-end, as a symbol, and the
1743 communication channel, as a plist. It must return a string or
1744 nil.")
1746 (defvar org-export-filter-entity-functions nil
1747 "List of functions applied to a transcoded entity.
1748 Each filter is called with three arguments: the transcoded
1749 entity, as a string, the back-end, as a symbol, and the
1750 communication channel, as a plist. It must return a string or
1751 nil.")
1753 (defvar org-export-filter-export-snippet-functions nil
1754 "List of functions applied to a transcoded export-snippet.
1755 Each filter is called with three arguments: the transcoded
1756 export-snippet, as a string, the back-end, as a symbol, and the
1757 communication channel, as a plist. It must return a string or
1758 nil.")
1760 (defvar org-export-filter-footnote-reference-functions nil
1761 "List of functions applied to a transcoded footnote-reference.
1762 Each filter is called with three arguments: the transcoded
1763 footnote-reference, as a string, the back-end, as a symbol, and
1764 the communication channel, as a plist. It must return a string
1765 or nil.")
1767 (defvar org-export-filter-inline-babel-call-functions nil
1768 "List of functions applied to a transcoded inline-babel-call.
1769 Each filter is called with three arguments: the transcoded
1770 inline-babel-call, as a string, the back-end, as a symbol, and
1771 the communication channel, as a plist. It must return a string
1772 or nil.")
1774 (defvar org-export-filter-inline-src-block-functions nil
1775 "List of functions applied to a transcoded inline-src-block.
1776 Each filter is called with three arguments: the transcoded
1777 inline-src-block, as a string, the back-end, as a symbol, and the
1778 communication channel, as a plist. It must return a string or
1779 nil.")
1781 (defvar org-export-filter-latex-fragment-functions nil
1782 "List of functions applied to a transcoded latex-fragment.
1783 Each filter is called with three arguments: the transcoded
1784 latex-fragment, as a string, the back-end, as a symbol, and the
1785 communication channel, as a plist. It must return a string or
1786 nil.")
1788 (defvar org-export-filter-line-break-functions nil
1789 "List of functions applied to a transcoded line-break.
1790 Each filter is called with three arguments: the transcoded
1791 line-break, as a string, the back-end, as a symbol, and the
1792 communication channel, as a plist. It must return a string or
1793 nil.")
1795 (defvar org-export-filter-link-functions nil
1796 "List of functions applied to a transcoded link.
1797 Each filter is called with three arguments: the transcoded link,
1798 as a string, the back-end, as a symbol, and the communication
1799 channel, as a plist. It must return a string or nil.")
1801 (defvar org-export-filter-macro-functions nil
1802 "List of functions applied to a transcoded macro.
1803 Each filter is called with three arguments: the transcoded macro,
1804 as a string, the back-end, as a symbol, and the communication
1805 channel, as a plist. It must return a string or nil.")
1807 (defvar org-export-filter-radio-target-functions nil
1808 "List of functions applied to a transcoded radio-target.
1809 Each filter is called with three arguments: the transcoded
1810 radio-target, as a string, the back-end, as a symbol, and the
1811 communication channel, as a plist. It must return a string or
1812 nil.")
1814 (defvar org-export-filter-statistics-cookie-functions nil
1815 "List of functions applied to a transcoded statistics-cookie.
1816 Each filter is called with three arguments: the transcoded
1817 statistics-cookie, as a string, the back-end, as a symbol, and
1818 the communication channel, as a plist. It must return a string
1819 or nil.")
1821 (defvar org-export-filter-subscript-functions nil
1822 "List of functions applied to a transcoded subscript.
1823 Each filter is called with three arguments: the transcoded
1824 subscript, as a string, the back-end, as a symbol, and the
1825 communication channel, as a plist. It must return a string or
1826 nil.")
1828 (defvar org-export-filter-superscript-functions nil
1829 "List of functions applied to a transcoded superscript.
1830 Each filter is called with three arguments: the transcoded
1831 superscript, as a string, the back-end, as a symbol, and the
1832 communication channel, as a plist. It must return a string or
1833 nil.")
1835 (defvar org-export-filter-target-functions nil
1836 "List of functions applied to a transcoded target.
1837 Each filter is called with three arguments: the transcoded
1838 target, as a string, the back-end, as a symbol, and the
1839 communication channel, as a plist. It must return a string or
1840 nil.")
1842 (defvar org-export-filter-time-stamp-functions nil
1843 "List of functions applied to a transcoded time-stamp.
1844 Each filter is called with three arguments: the transcoded
1845 time-stamp, as a string, the back-end, as a symbol, and the
1846 communication channel, as a plist. It must return a string or
1847 nil.")
1849 (defvar org-export-filter-verbatim-functions nil
1850 "List of functions applied to a transcoded verbatim.
1851 Each filter is called with three arguments: the transcoded
1852 verbatim, as a string, the back-end, as a symbol, and the
1853 communication channel, as a plist. It must return a string or
1854 nil.")
1856 (defun org-export-filter-apply-functions (filters value backend info)
1857 "Call every function in FILTERS with arguments VALUE, BACKEND and INFO.
1858 Functions are called in a LIFO fashion, to be sure that developer
1859 specified filters, if any, are called first."
1860 (loop for filter in filters
1861 if (not value) return nil else
1862 do (setq value (funcall filter value backend info)))
1863 value)
1867 ;;; Core functions
1869 ;; This is the room for the main function, `org-export-as', along with
1870 ;; its derivatives, `org-export-to-buffer' and `org-export-to-file'.
1871 ;; They differ only by the way they output the resulting code.
1873 ;; `org-export-output-file-name' is an auxiliary function meant to be
1874 ;; used with `org-export-to-file'. With a given extension, it tries
1875 ;; to provide a canonical file name to write export output to.
1877 ;; Note that `org-export-as' doesn't really parse the current buffer,
1878 ;; but a copy of it (with the same buffer-local variables and
1879 ;; visibility), where include keywords are expanded and Babel blocks
1880 ;; are executed, if appropriate.
1881 ;; `org-export-with-current-buffer-copy' macro prepares that copy.
1883 ;; File inclusion is taken care of by
1884 ;; `org-export-expand-include-keyword' and
1885 ;; `org-export-prepare-file-contents'. Structure wise, including
1886 ;; a whole Org file in a buffer often makes little sense. For
1887 ;; example, if the file contains an headline and the include keyword
1888 ;; was within an item, the item should contain the headline. That's
1889 ;; why file inclusion should be done before any structure can be
1890 ;; associated to the file, that is before parsing.
1892 (defun org-export-as (backend
1893 &optional subtreep visible-only body-only ext-plist)
1894 "Transcode current Org buffer into BACKEND code.
1896 If narrowing is active in the current buffer, only transcode its
1897 narrowed part.
1899 If a region is active, transcode that region.
1901 When optional argument SUBTREEP is non-nil, transcode the
1902 sub-tree at point, extracting information from the headline
1903 properties first.
1905 When optional argument VISIBLE-ONLY is non-nil, don't export
1906 contents of hidden elements.
1908 When optional argument BODY-ONLY is non-nil, only return body
1909 code, without preamble nor postamble.
1911 EXT-PLIST, when provided, is a property list with external
1912 parameters overriding Org default settings, but still inferior to
1913 file-local settings.
1915 Return code as a string."
1916 (save-excursion
1917 (save-restriction
1918 ;; Narrow buffer to an appropriate region for parsing.
1919 (when (org-region-active-p)
1920 (narrow-to-region (region-beginning) (region-end))
1921 (goto-char (point-min)))
1922 (when (and subtreep (not (org-at-heading-p)))
1923 ;; Ensure point is at sub-tree's beginning.
1924 (org-with-limited-levels (org-back-to-heading (not visible-only))))
1925 ;; Retrieve export options (INFO) and parsed tree (RAW-DATA),
1926 ;; Then options can be completed with tree properties. Note:
1927 ;; Buffer isn't parsed directly. Instead, a temporary copy is
1928 ;; created, where include keywords are expanded and code blocks
1929 ;; are evaluated. RAW-DATA is the parsed tree of the buffer
1930 ;; resulting from that process. Eventually call
1931 ;; `org-export-filter-parse-tree-functions'.
1932 (let* ((info (org-export-collect-options backend subtreep ext-plist))
1933 (raw-data (progn
1934 (when subtreep ; Only parse subtree contents.
1935 (let ((end (save-excursion (org-end-of-subtree t))))
1936 (narrow-to-region
1937 (progn (forward-line) (point)) end)))
1938 (org-export-filter-apply-functions
1939 (plist-get info :filter-parse-tree)
1940 (org-export-with-current-buffer-copy
1941 (org-export-expand-include-keyword nil)
1942 (let ((org-current-export-file (current-buffer)))
1943 (org-export-blocks-preprocess))
1944 (org-element-parse-buffer nil visible-only))
1945 backend info))))
1946 ;; Now get full initial options with tree properties.
1947 (setq info
1948 (org-combine-plists
1949 info
1950 (org-export-collect-tree-properties raw-data info backend)))
1951 ;; Now transcode RAW-DATA. Also call
1952 ;; `org-export-filter-final-output-functions'.
1953 (let* ((body (org-element-normalize-string
1954 (org-export-data raw-data backend info)))
1955 (template (intern (format "org-%s-template" backend)))
1956 (output (org-export-filter-apply-functions
1957 (plist-get info :filter-final-output)
1958 (if (or (not (fboundp template)) body-only) body
1959 (funcall template body info))
1960 backend info)))
1961 ;; Maybe add final OUTPUT to kill ring before returning it.
1962 (when org-export-copy-to-kill-ring (org-kill-new output))
1963 output)))))
1965 (defun org-export-to-buffer (backend buffer &optional subtreep visible-only
1966 body-only ext-plist)
1967 "Call `org-export-as' with output to a specified buffer.
1969 BACKEND is the back-end used for transcoding, as a symbol.
1971 BUFFER is the output buffer. If it already exists, it will be
1972 erased first, otherwise, it will be created.
1974 Arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and EXT-PLIST are
1975 similar to those used in `org-export-as', which see.
1977 Return buffer."
1978 (let ((out (org-export-as backend subtreep visible-only body-only ext-plist))
1979 (buffer (get-buffer-create buffer)))
1980 (with-current-buffer buffer
1981 (erase-buffer)
1982 (insert out)
1983 (goto-char (point-min)))
1984 buffer))
1986 (defun org-export-to-file (backend file &optional subtreep visible-only
1987 body-only ext-plist)
1988 "Call `org-export-as' with output to a specified file.
1990 BACKEND is the back-end used for transcoding, as a symbol. FILE
1991 is the name of the output file, as a string.
1993 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
1994 EXT-PLIST are similar to those used in `org-export-as', which
1995 see.
1997 Return output file's name."
1998 ;; Checks for FILE permissions. `write-file' would do the same, but
1999 ;; we'd rather avoid needless transcoding of parse tree.
2000 (unless (file-writable-p file) (error "Output file not writable"))
2001 ;; Insert contents to a temporary buffer and write it to FILE.
2002 (let ((out (org-export-as
2003 backend subtreep visible-only body-only ext-plist)))
2004 (with-temp-buffer
2005 (insert out)
2006 (let ((coding-system-for-write org-export-coding-system))
2007 (write-file file))))
2008 ;; Return full path.
2009 file)
2011 (defun org-export-output-file-name (extension &optional subtreep pub-dir)
2012 "Return output file's name according to buffer specifications.
2014 EXTENSION is a string representing the output file extension,
2015 with the leading dot.
2017 With a non-nil optional argument SUBTREEP, try to determine
2018 output file's name by looking for \"EXPORT_FILE_NAME\" property
2019 of subtree at point.
2021 When optional argument PUB-DIR is set, use it as the publishing
2022 directory.
2024 Return file name as a string, or nil if it couldn't be
2025 determined."
2026 (let ((base-name
2027 ;; File name may come from EXPORT_FILE_NAME subtree property,
2028 ;; assuming point is at beginning of said sub-tree.
2029 (file-name-sans-extension
2030 (or (and subtreep
2031 (org-entry-get
2032 (save-excursion
2033 (ignore-errors
2034 (org-back-to-heading (not visible-only)) (point)))
2035 "EXPORT_FILE_NAME" t))
2036 ;; File name may be extracted from buffer's associated
2037 ;; file, if any.
2038 (buffer-file-name (buffer-base-buffer))
2039 ;; Can't determine file name on our own: Ask user.
2040 (let ((read-file-name-function
2041 (and org-completion-use-ido 'ido-read-file-name)))
2042 (read-file-name
2043 "Output file: " pub-dir nil nil nil
2044 (lambda (name)
2045 (string= (file-name-extension name t) extension))))))))
2046 ;; Build file name. Enforce EXTENSION over whatever user may have
2047 ;; come up with. PUB-DIR, if defined, always has precedence over
2048 ;; any provided path.
2049 (cond
2050 (pub-dir
2051 (concat (file-name-as-directory pub-dir)
2052 (file-name-nondirectory base-name)
2053 extension))
2054 ((string= (file-name-nondirectory base-name) base-name)
2055 (concat (file-name-as-directory ".") base-name extension))
2056 (t (concat base-name extension)))))
2058 (defmacro org-export-with-current-buffer-copy (&rest body)
2059 "Apply BODY in a copy of the current buffer.
2061 The copy preserves local variables and visibility of the original
2062 buffer.
2064 Point is at buffer's beginning when BODY is applied."
2065 (org-with-gensyms (original-buffer offset buffer-string overlays)
2066 `(let ((,original-buffer ,(current-buffer))
2067 (,offset ,(1- (point-min)))
2068 (,buffer-string ,(buffer-string))
2069 (,overlays (mapcar
2070 'copy-overlay (overlays-in (point-min) (point-max)))))
2071 (with-temp-buffer
2072 (let ((buffer-invisibility-spec nil))
2073 (org-clone-local-variables
2074 ,original-buffer
2075 "^\\(org-\\|orgtbl-\\|major-mode$\\|outline-\\(regexp\\|level\\)$\\)")
2076 (insert ,buffer-string)
2077 (mapc (lambda (ov)
2078 (move-overlay
2080 (- (overlay-start ov) ,offset)
2081 (- (overlay-end ov) ,offset)
2082 (current-buffer)))
2083 ,overlays)
2084 (goto-char (point-min))
2085 (progn ,@body))))))
2086 (def-edebug-spec org-export-with-current-buffer-copy (body))
2088 (defun org-export-expand-include-keyword (included)
2089 "Expand every include keyword in buffer.
2090 INCLUDED is a list of included file names along with their line
2091 restriction, when appropriate. It is used to avoid infinite
2092 recursion."
2093 (let ((case-fold-search nil))
2094 (goto-char (point-min))
2095 (while (re-search-forward "^[ \t]*#\\+include: \\(.*\\)" nil t)
2096 (when (eq (car (save-match-data (org-element-at-point))) 'keyword)
2097 (beginning-of-line)
2098 ;; Extract arguments from keyword's value.
2099 (let* ((value (match-string 1))
2100 (ind (org-get-indentation))
2101 (file (and (string-match "^\"\\(\\S-+\\)\"" value)
2102 (prog1 (expand-file-name (match-string 1 value))
2103 (setq value (replace-match "" nil nil value)))))
2104 (lines
2105 (and (string-match
2106 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value)
2107 (prog1 (match-string 1 value)
2108 (setq value (replace-match "" nil nil value)))))
2109 (env (cond ((string-match "\\<example\\>" value) 'example)
2110 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
2111 (match-string 1 value))))
2112 ;; Minimal level of included file defaults to the child
2113 ;; level of the current headline, if any, or one. It
2114 ;; only applies is the file is meant to be included as
2115 ;; an Org one.
2116 (minlevel
2117 (and (not env)
2118 (if (string-match ":minlevel +\\([0-9]+\\)" value)
2119 (prog1 (string-to-number (match-string 1 value))
2120 (setq value (replace-match "" nil nil value)))
2121 (let ((cur (org-current-level)))
2122 (if cur (1+ (org-reduced-level cur)) 1))))))
2123 ;; Remove keyword.
2124 (delete-region (point) (progn (forward-line) (point)))
2125 (cond
2126 ((not (file-readable-p file)) (error "Cannot include file %s" file))
2127 ;; Check if files has already been parsed. Look after
2128 ;; inclusion lines too, as different parts of the same file
2129 ;; can be included too.
2130 ((member (list file lines) included)
2131 (error "Recursive file inclusion: %s" file))
2133 (cond
2134 ((eq env 'example)
2135 (insert
2136 (let ((ind-str (make-string ind ? ))
2137 (contents
2138 ;; Protect sensitive contents with commas.
2139 (replace-regexp-in-string
2140 "\\(^\\)\\([*]\\|[ \t]*#\\+\\)" ","
2141 (org-export-prepare-file-contents file lines)
2142 nil nil 1)))
2143 (format "%s#+begin_example\n%s%s#+end_example\n"
2144 ind-str contents ind-str))))
2145 ((stringp env)
2146 (insert
2147 (let ((ind-str (make-string ind ? ))
2148 (contents
2149 ;; Protect sensitive contents with commas.
2150 (replace-regexp-in-string
2151 (if (string= env "org") "\\(^\\)\\(.\\)"
2152 "\\(^\\)\\([*]\\|[ \t]*#\\+\\)") ","
2153 (org-export-prepare-file-contents file lines)
2154 nil nil 1)))
2155 (format "%s#+begin_src %s\n%s%s#+end_src\n"
2156 ind-str env contents ind-str))))
2158 (insert
2159 (with-temp-buffer
2160 (org-mode)
2161 (insert
2162 (org-export-prepare-file-contents file lines ind minlevel))
2163 (org-export-expand-include-keyword
2164 (cons (list file lines) included))
2165 (buffer-string))))))))))))
2167 (defun org-export-prepare-file-contents (file &optional lines ind minlevel)
2168 "Prepare the contents of FILE for inclusion and return them as a string.
2170 When optional argument LINES is a string specifying a range of
2171 lines, include only those lines.
2173 Optional argument IND, when non-nil, is an integer specifying the
2174 global indentation of returned contents. Since its purpose is to
2175 allow an included file to stay in the same environment it was
2176 created \(i.e. a list item), it doesn't apply past the first
2177 headline encountered.
2179 Optional argument MINLEVEL, when non-nil, is an integer
2180 specifying the level that any top-level headline in the included
2181 file should have."
2182 (with-temp-buffer
2183 (insert-file-contents file)
2184 (when lines
2185 (let* ((lines (split-string lines "-"))
2186 (lbeg (string-to-number (car lines)))
2187 (lend (string-to-number (cadr lines)))
2188 (beg (if (zerop lbeg) (point-min)
2189 (goto-char (point-min))
2190 (forward-line (1- lbeg))
2191 (point)))
2192 (end (if (zerop lend) (point-max)
2193 (goto-char (point-min))
2194 (forward-line (1- lend))
2195 (point))))
2196 (narrow-to-region beg end)))
2197 ;; Remove blank lines at beginning and end of contents. The logic
2198 ;; behind that removal is that blank lines around include keyword
2199 ;; override blank lines in included file.
2200 (goto-char (point-min))
2201 (org-skip-whitespace)
2202 (beginning-of-line)
2203 (delete-region (point-min) (point))
2204 (goto-char (point-max))
2205 (skip-chars-backward " \r\t\n")
2206 (forward-line)
2207 (delete-region (point) (point-max))
2208 ;; If IND is set, preserve indentation of include keyword until
2209 ;; the first headline encountered.
2210 (when ind
2211 (unless (eq major-mode 'org-mode) (org-mode))
2212 (goto-char (point-min))
2213 (let ((ind-str (make-string ind ? )))
2214 (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
2215 ;; Do not move footnote definitions out of column 0.
2216 (unless (and (looking-at org-footnote-definition-re)
2217 (eq (car (org-element-at-point)) 'footnote-definition))
2218 (insert ind-str))
2219 (forward-line))))
2220 ;; When MINLEVEL is specified, compute minimal level for headlines
2221 ;; in the file (CUR-MIN), and remove stars to each headline so
2222 ;; that headlines with minimal level have a level of MINLEVEL.
2223 (when minlevel
2224 (unless (eq major-mode 'org-mode) (org-mode))
2225 (let ((levels (org-map-entries
2226 (lambda () (org-reduced-level (org-current-level))))))
2227 (when levels
2228 (let ((offset (- minlevel (apply 'min levels))))
2229 (unless (zerop offset)
2230 (when org-odd-levels-only (setq offset (* offset 2)))
2231 ;; Only change stars, don't bother moving whole
2232 ;; sections.
2233 (org-map-entries
2234 (lambda () (if (< offset 0) (delete-char (abs offset))
2235 (insert (make-string offset ?*))))))))))
2236 (buffer-string)))
2239 ;;; Tools For Back-Ends
2241 ;; A whole set of tools is available to help build new exporters. Any
2242 ;; function general enough to have its use across many back-ends
2243 ;; should be added here.
2245 ;; As of now, functions operating on footnotes, headlines, links,
2246 ;; macros, references, src-blocks, tables and tables of contents are
2247 ;; implemented.
2249 ;;;; For Footnotes
2251 ;; `org-export-collect-footnote-definitions' is a tool to list
2252 ;; actually used footnotes definitions in the whole parse tree, or in
2253 ;; an headline, in order to add footnote listings throughout the
2254 ;; transcoded data.
2256 ;; `org-export-footnote-first-reference-p' is a predicate used by some
2257 ;; back-ends, when they need to attach the footnote definition only to
2258 ;; the first occurrence of the corresponding label.
2260 ;; `org-export-get-footnote-definition' and
2261 ;; `org-export-get-footnote-number' provide easier access to
2262 ;; additional information relative to a footnote reference.
2264 (defun org-export-collect-footnote-definitions (data info)
2265 "Return an alist between footnote numbers, labels and definitions.
2267 DATA is the parse tree from which definitions are collected.
2268 INFO is the plist used as a communication channel.
2270 Definitions are sorted by order of references. They either
2271 appear as Org data \(transcoded with `org-export-data'\) or as
2272 a secondary string for inlined footnotes \(transcoded with
2273 `org-export-secondary-string'\). Unreferenced definitions are
2274 ignored."
2275 (let (refs)
2276 ;; Collect seen references in REFS.
2277 (org-element-map
2278 data 'footnote-reference
2279 (lambda (footnote local)
2280 (when (org-export-footnote-first-reference-p footnote local)
2281 (list (org-export-get-footnote-number footnote local)
2282 (org-element-get-property :label footnote)
2283 (org-export-get-footnote-definition footnote local))))
2284 info)))
2286 (defun org-export-footnote-first-reference-p (footnote-reference info)
2287 "Non-nil when a footnote reference is the first one for its label.
2289 FOOTNOTE-REFERENCE is the footnote reference being considered.
2290 INFO is the plist used as a communication channel."
2291 (let ((label (org-element-get-property :label footnote-reference)))
2292 (or (not label)
2293 (equal
2294 footnote-reference
2295 (org-element-map
2296 (plist-get info :parse-tree) 'footnote-reference
2297 (lambda (footnote local)
2298 (when (string= (org-element-get-property :label footnote) label)
2299 footnote))
2300 info 'first-match)))))
2302 (defun org-export-get-footnote-definition (footnote-reference info)
2303 "Return definition of FOOTNOTE-REFERENCE as parsed data.
2304 INFO is the plist used as a communication channel."
2305 (let ((label (org-element-get-property :label footnote-reference)))
2306 (or (org-element-get-property :inline-definition footnote-reference)
2307 (cdr (assoc label (plist-get info :footnote-definition-alist))))))
2309 (defun org-export-get-footnote-number (footnote info)
2310 "Return number associated to a footnote.
2312 FOOTNOTE is either a footnote reference or a footnote definition.
2313 INFO is the plist used as a communication channel."
2314 (let ((label (org-element-get-property :label footnote)) seen-refs)
2315 (org-element-map
2316 (plist-get info :parse-tree) 'footnote-reference
2317 (lambda (fn local)
2318 (let ((fn-lbl (org-element-get-property :label fn)))
2319 (cond
2320 ((and (not fn-lbl) (equal fn footnote)) (1+ (length seen-refs)))
2321 ((and label (string= label fn-lbl)) (1+ (length seen-refs)))
2322 ;; Anonymous footnote: it's always a new one. Also, be sure
2323 ;; to return nil from the `cond' so `first-match' doesn't
2324 ;; get us out of the loop.
2325 ((not fn-lbl) (push 'inline seen-refs) nil)
2326 ;; Label not seen so far: add it so SEEN-REFS. Again,
2327 ;; return nil to stay in the loop.
2328 ((not (member fn-lbl seen-refs)) (push fn-lbl seen-refs) nil))))
2329 info 'first-match)))
2332 ;;;; For Headlines
2334 ;; `org-export-get-relative-level' is a shortcut to get headline
2335 ;; level, relatively to the lower headline level in the parsed tree.
2337 ;; `org-export-get-headline-number' returns the section number of an
2338 ;; headline, while `org-export-number-to-roman' allows to convert it
2339 ;; to roman numbers.
2341 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
2342 ;; `org-export-last-sibling-p' are three useful predicates when it
2343 ;; comes to fulfill the `:headline-levels' property.
2345 (defun org-export-get-relative-level (headline info)
2346 "Return HEADLINE relative level within current parsed tree.
2347 INFO is a plist holding contextual information."
2348 (+ (org-element-get-property :level headline)
2349 (or (plist-get info :headline-offset) 0)))
2351 (defun org-export-low-level-p (headline info)
2352 "Non-nil when HEADLINE is considered as low level.
2354 INFO is a plist used as a communication channel.
2356 A low level headlines has a relative level greater than
2357 `:headline-levels' property value.
2359 Return value is the difference between HEADLINE relative level
2360 and the last level being considered as high enough, or nil."
2361 (let ((limit (plist-get info :headline-levels)))
2362 (when (wholenump limit)
2363 (let ((level (org-export-get-relative-level headline info)))
2364 (and (> level limit) (- level limit))))))
2366 (defun org-export-get-headline-number (headline info)
2367 "Return HEADLINE numbering as a list of numbers.
2368 INFO is a plist holding contextual information."
2369 (cdr (assoc headline (plist-get info :headline-numbering))))
2371 (defun org-export-number-to-roman (n)
2372 "Convert integer N into a roman numeral."
2373 (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
2374 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
2375 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
2376 ( 1 . "I")))
2377 (res ""))
2378 (if (<= n 0)
2379 (number-to-string n)
2380 (while roman
2381 (if (>= n (caar roman))
2382 (setq n (- n (caar roman))
2383 res (concat res (cdar roman)))
2384 (pop roman)))
2385 res)))
2387 (defun org-export-first-sibling-p (headline info)
2388 "Non-nil when HEADLINE is the first sibling in its sub-tree.
2389 INFO is the plist used as a communication channel."
2390 (not (eq (car (org-export-get-previous-element headline info)) 'headline)))
2392 (defun org-export-last-sibling-p (headline info)
2393 "Non-nil when HEADLINE is the last sibling in its sub-tree.
2394 INFO is the plist used as a communication channel."
2395 (equal
2396 (car (last (org-element-get-contents (car (plist-get info :genealogy)))))
2397 headline))
2400 ;;;; For Links
2402 ;; `org-export-solidify-link-text' turns a string into a safer version
2403 ;; for links, replacing most non-standard characters with hyphens.
2405 ;; `org-export-get-coderef-format' returns an appropriate format
2406 ;; string for coderefs.
2408 ;; `org-export-inline-image-p' returns a non-nil value when the link
2409 ;; provided should be considered as an inline image.
2411 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
2412 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
2413 ;; returns an appropriate unique identifier when found, or nil.
2415 ;; `org-export-resolve-id-link' returns the first headline with
2416 ;; specified id or custom-id in parse tree, or nil when none was
2417 ;; found.
2419 ;; `org-export-resolve-coderef' associates a reference to a line
2420 ;; number in the element it belongs, or returns the reference itself
2421 ;; when the element isn't numbered.
2423 (defun org-export-solidify-link-text (s)
2424 "Take link text S and make a safe target out of it."
2425 (save-match-data
2426 (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_\\.-]+") "-")))
2428 (defun org-export-get-coderef-format (path desc)
2429 "Return format string for code reference link.
2430 PATH is the link path. DESC is its description."
2431 (save-match-data
2432 (cond ((string-match (regexp-quote (concat "(" path ")")) desc)
2433 (replace-match "%s" t t desc))
2434 ((string= desc "") "%s")
2435 (t desc))))
2437 (defun org-export-inline-image-p (link &optional extensions)
2438 "Non-nil if LINK object points to an inline image.
2440 When non-nil, optional argument EXTENSIONS is a list of valid
2441 extensions for image files, as strings. Otherwise, a default
2442 list is provided \(cf `org-image-file-name-regexp'\)."
2443 (and (not (org-element-get-contents link))
2444 (string= (org-element-get-property :type link) "file")
2445 (org-file-image-p
2446 (expand-file-name (org-element-get-property :path link))
2447 extensions)))
2449 (defun org-export-resolve-fuzzy-link (link info)
2450 "Return LINK destination.
2452 INFO is a plist holding contextual information.
2454 Return value can be an object, an element, or nil:
2456 - If LINK path exactly matches any target, return the target
2457 object.
2459 - If LINK path exactly matches any headline name, return that
2460 element. If more than one headline share that name, priority
2461 will be given to the one with the closest common ancestor, if
2462 any, or the first one in the parse tree otherwise.
2464 - Otherwise, return nil.
2466 Assume LINK type is \"fuzzy\"."
2467 (let ((path (org-element-get-property :path link)))
2468 ;; Link points to a target: return it.
2469 (or (loop for target in (plist-get info :target-list)
2470 when (string= (org-element-get-property :raw-value target) path)
2471 return target)
2472 ;; Link either points to an headline or nothing. Try to find
2473 ;; the source, with priority given to headlines with the closest
2474 ;; common ancestor. If such candidate is found, return its
2475 ;; beginning position as an unique identifier, otherwise return
2476 ;; nil.
2477 (let ((find-headline
2478 (function
2479 ;; Return first headline whose `:raw-value' property
2480 ;; is NAME in parse tree DATA, or nil.
2481 (lambda (name data)
2482 (org-element-map
2483 data 'headline
2484 (lambda (headline local)
2485 (when (string=
2486 (org-element-get-property :raw-value headline)
2487 name)
2488 headline))
2489 info 'first-match)))))
2490 ;; Search among headlines sharing an ancestor with link,
2491 ;; from closest to farthest.
2492 (or (catch 'exit
2493 (mapc
2494 (lambda (parent)
2495 (when (eq (car parent) 'headline)
2496 (let ((foundp (funcall find-headline path parent)))
2497 (when foundp (throw 'exit foundp)))))
2498 (plist-get info :genealogy)) nil)
2499 ;; No match with a common ancestor: try the full parse-tree.
2500 (funcall find-headline path (plist-get info :parse-tree)))))))
2502 (defun org-export-resolve-id-link (link info)
2503 "Return headline referenced as LINK destination.
2505 INFO is a plist used as a communication channel.
2507 Return value can be an headline element or nil. Assume LINK type
2508 is either \"id\" or \"custom-id\"."
2509 (let ((id (org-element-get-property :path link)))
2510 (org-element-map
2511 (plist-get info :parse-tree) 'headline
2512 (lambda (headline local)
2513 (when (or (string= (org-element-get-property :id headline) id)
2514 (string= (org-element-get-property :custom-id headline) id))
2515 headline))
2516 info 'first-match)))
2518 (defun org-export-resolve-coderef (ref info)
2519 "Resolve a code reference REF.
2521 INFO is a plist used as a communication channel.
2523 Return associated line number in source code, or REF itself,
2524 depending on src-block or example element's switches."
2525 (org-element-map
2526 (plist-get info :parse-tree) '(src-block example)
2527 (lambda (el local)
2528 (let ((switches (or (org-element-get-property :switches el) "")))
2529 (with-temp-buffer
2530 (insert (org-trim (org-element-get-property :value el)))
2531 ;; Build reference regexp.
2532 (let* ((label
2533 (or (and (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
2534 (match-string 1 switches))
2535 org-coderef-label-format))
2536 (ref-re
2537 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
2538 (replace-regexp-in-string "%s" ref label nil t))))
2539 ;; Element containing REF is found. Only associate REF to
2540 ;; a line number if element has "+n" or "-n" and "-k" or
2541 ;; "-r" as switches. When it has "+n", count accumulated
2542 ;; locs before, too.
2543 (when (re-search-backward ref-re nil t)
2544 (cond
2545 ((not (string-match "-[kr]\\>" switches)) ref)
2546 ((string-match "-n\\>" switches) (line-number-at-pos))
2547 ((string-match "\\+n\\>" switches)
2548 (+ (org-export-get-loc el local) (line-number-at-pos)))
2549 (t ref)))))))
2550 info 'first-match))
2553 ;;;; For Macros
2555 ;; `org-export-expand-macro' simply takes care of expanding macros.
2557 (defun org-export-expand-macro (macro info)
2558 "Expand MACRO and return it as a string.
2559 INFO is a plist holding export options."
2560 (let* ((key (org-element-get-property :key macro))
2561 (args (org-element-get-property :args macro))
2562 (value (plist-get info (intern (format ":macro-%s" key)))))
2563 ;; Replace arguments in VALUE.
2564 (let ((s 0) n)
2565 (while (string-match "\\$\\([0-9]+\\)" value s)
2566 (setq s (1+ (match-beginning 0))
2567 n (string-to-number (match-string 1 value)))
2568 (and (>= (length args) n)
2569 (setq value (replace-match (nth (1- n) args) t t value)))))
2570 ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
2571 (when (string-match "\\`(eval\\>" value)
2572 (setq value (eval (read value))))
2573 ;; Return expanded string.
2574 (format "%s" value)))
2577 ;;;; For References
2579 ;; `org-export-get-ordinal' associates a sequence number to any object
2580 ;; or element.
2582 (defun org-export-get-ordinal
2583 (element info &optional types within-section predicate)
2584 "Return ordinal number of an element or object.
2586 ELEMENT is the element or object considered. INFO is the plist
2587 used as a communication channel.
2589 Optional argument TYPES, when non-nil, is a list of element or
2590 object types, as symbols, that should also be counted in.
2591 Otherwise, only provided element's type is considered.
2593 When optional argument WITHIN-SECTION is non-nil, narrow counting
2594 to the section containing ELEMENT.
2596 Optional argument PREDICATE is a function returning a non-nil
2597 value if the current element or object should be counted in. It
2598 accepts one argument: the element or object being considered.
2599 This argument allows to count only a certain type of objects,
2600 like inline images, which are a subset of links \(in that case,
2601 `org-export-inline-image-p' might be an useful predicate\)."
2602 (let ((counter 0)
2603 ;; Determine if search should apply to current section, in
2604 ;; which case it should be retrieved first, or to full parse
2605 ;; tree. As a special case, an element or object without
2606 ;; a parent headline will also trigger a full search,
2607 ;; notwithstanding WITHIN-SECTION value.
2608 (data
2609 (if (not within-section) (plist-get info :parse-tree)
2610 (or (org-export-get-parent-headline element info)
2611 (plist-get info :parse-tree)))))
2612 ;; Increment counter until ELEMENT is found again.
2613 (org-element-map
2614 data (or types (car element))
2615 (lambda (el local)
2616 (cond
2617 ((equal element el) (1+ counter))
2618 ((not predicate) (incf counter) nil)
2619 ((funcall predicate el) (incf counter) nil)))
2620 info 'first-match)))
2623 ;;;; For Src-Blocks
2625 ;; `org-export-get-loc' counts number of code lines accumulated in
2626 ;; src-block or example-block elements with a "+n" switch until
2627 ;; a given element, excluded. Note: "-n" switches reset that count.
2629 ;; `org-export-handle-code' takes care of line numbering and reference
2630 ;; cleaning in source code, when appropriate.
2632 (defun org-export-get-loc (element info)
2633 "Return accumulated lines of code up to ELEMENT.
2635 INFO is the plist used as a communication channel.
2637 ELEMENT is excluded from count."
2638 (let ((loc 0))
2639 (org-element-map
2640 (plist-get info :parse-tree) `(src-block example-block ,(car element))
2641 (lambda (el local)
2642 (cond
2643 ;; ELEMENT is reached: Quit the loop.
2644 ((equal el element) t)
2645 ;; Only count lines from src-block and example-block elements
2646 ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
2647 ((not (memq (car el) '(src-block example-block))) nil)
2648 ((let ((switches (org-element-get-property :switches el)))
2649 (when (and switches (string-match "\\([-+]\\)n\\>" switches))
2650 ;; Accumulate locs or reset them.
2651 (let ((accumulatep (string= (match-string 1 switches) "-"))
2652 (lines (org-count-lines
2653 (org-trim (org-element-get-property :value el)))))
2654 (setq loc (if accumulatep lines (+ loc lines))))))
2655 ;; Return nil to stay in the loop.
2656 nil)))
2657 info 'first-match)
2658 ;; Return value.
2659 loc))
2661 (defun org-export-handle-code (element info &optional num-fmt ref-fmt delayed)
2662 "Handle line numbers and code references in ELEMENT.
2664 ELEMENT has either a `src-block' an `example-block' type. INFO
2665 is a plist used as a communication channel.
2667 If optional argument NUM-FMT is a string, it will be used as
2668 a format string for numbers at beginning of each line.
2670 If optional argument REF-FMT is a string, it will be used as
2671 a format string for each line of code containing a reference.
2673 When optional argument DELAYED is non-nil, `org-loc' and
2674 `org-coderef' properties, set to an adequate value, are applied
2675 to, respectively, numbered lines and lines with a reference. No
2676 line numbering is done and all references are stripped from the
2677 resulting string. Both NUM-FMT and REF-FMT arguments are ignored
2678 in that situation.
2680 Return new code as a string."
2681 (let* ((switches (or (org-element-get-property :switches element) ""))
2682 (code (org-element-get-property :value element))
2683 (numberp (string-match "[-+]n\\>" switches))
2684 (accumulatep (string-match "\\+n\\>" switches))
2685 ;; Initialize loc counter when any kind of numbering is
2686 ;; active.
2687 (total-LOC (cond
2688 (accumulatep (org-export-get-loc element info))
2689 (numberp 0)))
2690 ;; Get code and clean it. Remove blank lines at its
2691 ;; beginning and end. Also remove protective commas.
2692 (preserve-indent-p (or org-src-preserve-indentation
2693 (string-match "-i\\>" switches)))
2694 (replace-labels (when (string-match "-r\\>" switches)
2695 (if (string-match "-k\\>" switches) 'keep t)))
2696 (code (let ((c (replace-regexp-in-string
2697 "\\`\\([ \t]*\n\\)+" ""
2698 (replace-regexp-in-string
2699 "\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n" code))))
2700 ;; If appropriate, remove global indentation.
2701 (unless preserve-indent-p (setq c (org-remove-indentation c)))
2702 ;; Free up the protected lines. Note: Org blocks
2703 ;; have commas at the beginning or every line.
2704 (if (string=
2705 (or (org-element-get-property :language element) "")
2706 "org")
2707 (replace-regexp-in-string "^," "" c)
2708 (replace-regexp-in-string
2709 "^\\(,\\)\\(:?\\*\\|[ \t]*#\\+\\)" "" c nil nil 1))))
2710 ;; Split code to process it line by line.
2711 (code-lines (org-split-string code "\n"))
2712 ;; If numbering is active, ensure line numbers will be
2713 ;; correctly padded before applying the format string.
2714 (num-fmt
2715 (when (and (not delayed) numberp)
2716 (format (if (stringp num-fmt) num-fmt "%s: ")
2717 (format "%%%ds"
2718 (length (number-to-string
2719 (+ (length code-lines) total-LOC)))))))
2720 ;; Get format used for references.
2721 (label-fmt (or (and (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
2722 (match-string 1 switches))
2723 org-coderef-label-format))
2724 ;; Build a regexp matching a loc with a reference.
2725 (with-ref-re (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
2726 (replace-regexp-in-string
2727 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
2728 (org-element-normalize-string
2729 (mapconcat
2730 (lambda (loc)
2731 ;; Maybe add line number to current line of code (LOC).
2732 (when numberp
2733 (incf total-LOC)
2734 (setq loc (if delayed (org-add-props loc nil 'org-loc total-LOC)
2735 (concat (format num-fmt total-LOC) loc))))
2736 ;; Take action if at a ref line.
2737 (when (string-match with-ref-re loc)
2738 (let ((ref (match-string 3 loc)))
2739 (setq loc
2740 ;; Option "-r" without "-k" removes labels.
2741 ;; A non-nil DELAYED removes labels unconditionally.
2742 (if (or delayed
2743 (and replace-labels (not (eq replace-labels 'keep))))
2744 (replace-match "" nil nil loc 1)
2745 (replace-match (format "(%s)" ref) nil nil loc 2)))
2746 ;; Store REF in `org-coderef' property if DELAYED asks to.
2747 (cond (delayed (setq loc (org-add-props loc nil 'org-coderef ref)))
2748 ;; If REF-FMT is defined, apply it to current LOC.
2749 ((stringp ref-fmt) (setq loc (format ref-fmt loc))))))
2750 ;; Return updated LOC for concatenation.
2751 loc)
2752 code-lines "\n"))))
2755 ;;;; For Tables
2757 ;; `org-export-table-format-info' extracts formatting information
2758 ;; (alignment, column groups and presence of a special column) from
2759 ;; a raw table and returns it as a property list.
2761 ;; `org-export-clean-table' cleans the raw table from any Org
2762 ;; table-specific syntax.
2764 (defun org-export-table-format-info (table)
2765 "Extract info from TABLE.
2766 Return a plist whose properties and values are:
2767 `:alignment' vector of strings among \"r\", \"l\" and \"c\",
2768 `:column-groups' vector of symbols among `start', `end', `start-end',
2769 `:row-groups' list of integers representing row groups.
2770 `:special-column-p' non-nil if table has a special column.
2771 `:width' vector of integers representing desired width of
2772 current column, or nil."
2773 (with-temp-buffer
2774 (insert table)
2775 (goto-char 1)
2776 (org-table-align)
2777 (let ((align (vconcat (mapcar (lambda (c) (if c "r" "l"))
2778 org-table-last-alignment)))
2779 (width (make-vector (length org-table-last-alignment) nil))
2780 (colgroups (make-vector (length org-table-last-alignment) nil))
2781 (row-group 0)
2782 (rowgroups)
2783 (special-column-p 'empty))
2784 (mapc (lambda (row)
2785 (if (string-match "^[ \t]*|[-+]+|[ \t]*$" row)
2786 (incf row-group)
2787 ;; Determine if a special column is present by looking
2788 ;; for special markers in the first column. More
2789 ;; accurately, the first column is considered special
2790 ;; if it only contains special markers and, maybe,
2791 ;; empty cells.
2792 (setq special-column-p
2793 (cond
2794 ((not special-column-p) nil)
2795 ((string-match "^[ \t]*| *\\\\?\\([/#!$*_^]\\) *|" row)
2796 'special)
2797 ((string-match "^[ \t]*| +|" row) special-column-p))))
2798 (cond
2799 ;; Read forced alignment and width information, if any,
2800 ;; and determine final alignment for the table.
2801 ((org-table-cookie-line-p row)
2802 (let ((col 0))
2803 (mapc (lambda (field)
2804 (when (string-match
2805 "<\\([lrc]\\)?\\([0-9]+\\)?>" field)
2806 (let ((align-data (match-string 1 field)))
2807 (when align-data (aset align col align-data)))
2808 (let ((w-data (match-string 2 field)))
2809 (when w-data
2810 (aset width col (string-to-number w-data)))))
2811 (incf col))
2812 (org-split-string row "[ \t]*|[ \t]*"))))
2813 ;; Read column groups information.
2814 ((org-table-colgroup-line-p row)
2815 (let ((col 0))
2816 (mapc (lambda (field)
2817 (aset colgroups col
2818 (cond ((string= "<" field) 'start)
2819 ((string= ">" field) 'end)
2820 ((string= "<>" field) 'start-end)))
2821 (incf col))
2822 (org-split-string row "[ \t]*|[ \t]*"))))
2823 ;; Contents line.
2824 (t (push row-group rowgroups))))
2825 (org-split-string table "\n"))
2826 ;; Return plist.
2827 (list :alignment align
2828 :column-groups colgroups
2829 :row-groups (reverse rowgroups)
2830 :special-column-p (eq special-column-p 'special)
2831 :width width))))
2833 (defun org-export-clean-table (table specialp)
2834 "Clean string TABLE from its formatting elements.
2835 Remove any row containing column groups or formatting cookies and
2836 rows starting with a special marker. If SPECIALP is non-nil,
2837 assume the table contains a special formatting column and remove
2838 it also."
2839 (let ((rows (org-split-string table "\n")))
2840 (mapconcat 'identity
2841 (delq nil
2842 (mapcar
2843 (lambda (row)
2844 (cond
2845 ((org-table-colgroup-line-p row) nil)
2846 ((org-table-cookie-line-p row) nil)
2847 ;; Ignore rows starting with a special marker.
2848 ((string-match "^[ \t]*| *[!_^/] *|" row) nil)
2849 ;; Remove special column.
2850 ((and specialp
2851 (or (string-match "^\\([ \t]*\\)|-+\\+" row)
2852 (string-match "^\\([ \t]*\\)|[^|]*|" row)))
2853 (replace-match "\\1|" t nil row))
2854 (t row)))
2855 rows))
2856 "\n")))
2859 ;;;; For Tables Of Contents
2861 ;; `org-export-collect-headlines' builds a list of all exportable
2862 ;; headline elements, maybe limited to a certain depth. One can then
2863 ;; easily parse it and transcode it.
2865 ;; Building lists of tables, figures or listings is quite similar.
2866 ;; Once the generic function `org-export-collect-elements' is defined,
2867 ;; `org-export-collect-tables', `org-export-collect-figures' and
2868 ;; `org-export-collect-listings' can be derived from it.
2870 (defun org-export-collect-headlines (info &optional n)
2871 "Collect headlines in order to build a table of contents.
2873 INFO is a plist used as a communication channel.
2875 When non-nil, optional argument N must be an integer. It
2876 specifies the depth of the table of contents.
2878 Return a list of all exportable headlines as parsed elements."
2879 (org-element-map
2880 (plist-get info :parse-tree)
2881 'headline
2882 (lambda (headline local)
2883 ;; Strip contents from HEADLINE.
2884 (let ((relative-level (org-export-get-relative-level headline local)))
2885 (unless (and n (> relative-level n)) headline)))
2886 info))
2888 (defun org-export-collect-elements (type info &optional predicate)
2889 "Collect referenceable elements of a determined type.
2891 TYPE can be a symbol or a list of symbols specifying element
2892 types to search. Only elements with a caption or a name are
2893 collected.
2895 INFO is a plist used as a communication channel.
2897 When non-nil, optional argument PREDICATE is a function accepting
2898 one argument, an element of type TYPE. It returns a non-nil
2899 value when that element should be collected.
2901 Return a list of all elements found, in order of appearance."
2902 (org-element-map
2903 (plist-get info :parse-tree) type
2904 (lambda (element local)
2905 (and (or (org-element-get-property :caption element)
2906 (org-element-get-property :name element))
2907 (or (not predicate) (funcall predicate element))
2908 element)) info))
2910 (defun org-export-collect-tables (info)
2911 "Build a list of tables.
2913 INFO is a plist used as a communication channel.
2915 Return a list of table elements with a caption or a name
2916 affiliated keyword."
2917 (org-export-collect-elements 'table info))
2919 (defun org-export-collect-figures (info predicate)
2920 "Build a list of figures.
2922 INFO is a plist used as a communication channel. PREDICATE is
2923 a function which accepts one argument: a paragraph element and
2924 whose return value is non-nil when that element should be
2925 collected.
2927 A figure is a paragraph type element, with a caption or a name,
2928 verifying PREDICATE. The latter has to be provided since
2929 a \"figure\" is a vague concept that may depend on back-end.
2931 Return a list of elements recognized as figures."
2932 (org-export-collect-elements 'paragraph info predicate))
2934 (defun org-export-collect-listings (info)
2935 "Build a list of src blocks.
2937 INFO is a plist used as a communication channel.
2939 Return a list of src-block elements with a caption or a name
2940 affiliated keyword."
2941 (org-export-collect-elements 'src-block info))
2944 ;;;; Topology
2946 (defun org-export-get-parent-headline (blob info)
2947 "Return BLOB's closest parent headline or nil.
2948 INFO is a plist used as a communication channel."
2949 (catch 'exit
2950 (mapc
2951 (lambda (el) (when (eq (car el) 'headline) (throw 'exit el)))
2952 (plist-get info :genealogy))
2953 nil))
2955 (defun org-export-get-previous-element (blob info)
2956 "Return previous element or object.
2958 BLOB is an element or object. INFO is a plist used as
2959 a communication channel.
2961 Return previous element or object, a string, or nil."
2962 (let ((parent (car (plist-get info :genealogy))))
2963 (cadr (member blob (reverse (org-element-get-contents parent))))))
2965 (defun org-export-get-next-element (blob info)
2966 "Return next element or object.
2968 BLOB is an element or object. INFO is a plist used as
2969 a communication channel.
2971 Return next element or object, a string, or nil."
2972 (let ((parent (car (plist-get info :genealogy))))
2973 (cadr (member blob (org-element-get-contents parent)))))
2977 ;;; The Dispatcher
2979 ;; `org-export-dispatch' is the standard interactive way to start an
2980 ;; export process. It uses `org-export-dispatch-ui' as a subroutine
2981 ;; for its interface. Most commons back-ends should have an entry in
2982 ;; it.
2984 (defun org-export-dispatch ()
2985 "Export dispatcher for Org mode.
2987 It provides an access to common export related tasks in a buffer.
2988 Its interface comes in two flavours: standard and expert. While
2989 both share the same set of bindings, only the former displays the
2990 valid keys associations. Set `org-export-dispatch-use-expert-ui'
2991 to switch to one or the other.
2993 Return an error if key pressed has no associated command."
2994 (interactive)
2995 (let* ((input (org-export-dispatch-ui
2996 (if (listp org-export-initial-scope) org-export-initial-scope
2997 (list org-export-initial-scope))
2998 org-export-dispatch-use-expert-ui))
2999 (raw-key (car input))
3000 (scope (cdr input)))
3001 ;; Translate "C-a", "C-b"... into "a", "b"... Then take action
3002 ;; depending on user's key pressed.
3003 (case (if (< raw-key 27) (+ raw-key 96) raw-key)
3004 ;; Export with `e-ascii' back-end.
3005 ((?A ?N ?U)
3006 (let ((outbuf
3007 (org-export-to-buffer
3008 'e-ascii "*Org E-ASCII Export*"
3009 (memq 'subtree scope) (memq 'visible scope) (memq 'body scope)
3010 `(:ascii-charset
3011 ,(case raw-key (?A 'ascii) (?N 'latin1) (t 'utf-8))))))
3012 (with-current-buffer outbuf (text-mode))
3013 (when org-export-show-temporary-export-buffer
3014 (switch-to-buffer-other-window outbuf))))
3015 ((?a ?n ?u)
3016 (org-e-ascii-export-to-ascii
3017 (memq 'subtree scope) (memq 'visible scope) (memq 'body scope)
3018 `(:ascii-charset ,(case raw-key (?a 'ascii) (?n 'latin1) (t 'utf-8)))))
3019 ;; Export with `e-latex' back-end.
3021 (let ((outbuf
3022 (org-export-to-buffer
3023 'e-latex "*Org E-LaTeX Export*"
3024 (memq 'subtree scope) (memq 'visible scope) (memq 'body scope))))
3025 (with-current-buffer outbuf (latex-mode))
3026 (when org-export-show-temporary-export-buffer
3027 (switch-to-buffer-other-window outbuf))))
3028 (?l (org-e-latex-export-to-latex
3029 (memq 'subtree scope) (memq 'visible scope) (memq 'body scope)))
3030 (?p (org-e-latex-export-to-pdf
3031 (memq 'subtree scope) (memq 'visible scope) (memq 'body scope)))
3032 (?d (org-open-file
3033 (org-e-latex-export-to-pdf
3034 (memq 'subtree scope) (memq 'visible scope) (memq 'body scope))))
3035 ;; Undefined command.
3036 (t (error "No command associated with key %s"
3037 (char-to-string raw-key))))))
3039 (defun org-export-dispatch-ui (scope expertp)
3040 "Handle interface for `org-export-dispatch'.
3042 SCOPE is a list containing current interactive options set for
3043 export. It can contain any of the following symbols:
3044 `body' toggles a body-only export
3045 `subtree' restricts export to current subtree
3046 `visible' restricts export to visible part of buffer.
3048 EXPERTP, when non-nil, triggers expert UI. In that case, no help
3049 buffer is provided, but indications about currently active
3050 options are given in the prompt. Moreover, \[?] allows to switch
3051 back to standard interface.
3053 Return value is a list with key pressed as car and a list of
3054 final interactive export options as cdr."
3055 (let ((help (format "------------------- General Options --------------------
3056 \[1] Body only: %s
3057 \[2] Export scope: %s
3058 \[3] Visible only: %s
3060 -------------- ASCII/Latin-1/UTF-8 Export --------------
3061 \[a/n/u] to TXT file [A/N/U] to temporary buffer
3063 --------------------- LaTeX Export ---------------------
3064 \[l] to TEX file [L] to temporary buffer
3065 \[p] to PDF file [d] ... and open it"
3066 (if (memq 'body scope) "On" "Off")
3067 (if (memq 'subtree scope) "Subtree" "Buffer")
3068 (if (memq 'visible scope) "On" "Off")))
3069 (standard-prompt "Export command: ")
3070 (expert-prompt (format "Export command (%s%s%s): "
3071 (if (memq 'body scope) "b" "-")
3072 (if (memq 'subtree scope) "s" "-")
3073 (if (memq 'visible scope) "v" "-")))
3074 (handle-keypress
3075 (function
3076 ;; Read a character from command input, toggling interactive
3077 ;; options when applicable. PROMPT is the displayed prompt,
3078 ;; as a string.
3079 (lambda (prompt)
3080 (let ((key (read-char-exclusive prompt)))
3081 (cond
3082 ;; Ignore non-standard characters (i.e. "M-a").
3083 ((not (characterp key)) (org-export-dispatch-ui scope expertp))
3084 ;; Switch back to standard interface.
3085 ((and (eq key ??) expertp) (org-export-dispatch-ui scope nil))
3086 ((eq key ?1)
3087 (org-export-dispatch-ui
3088 (if (memq 'body scope) (remq 'body scope) (cons 'body scope))
3089 expertp))
3090 ((eq key ?2)
3091 (org-export-dispatch-ui
3092 (if (memq 'subtree scope) (remq 'subtree scope)
3093 (cons 'subtree scope))
3094 expertp))
3095 ((eq key ?3)
3096 (org-export-dispatch-ui
3097 (if (memq 'visible scope) (remq 'visible scope)
3098 (cons 'visible scope))
3099 expertp))
3100 (t (cons key scope))))))))
3101 ;; With expert UI, just read key with a fancy prompt. In standard
3102 ;; UI, display an intrusive help buffer.
3103 (if expertp (funcall handle-keypress expert-prompt)
3104 (save-window-excursion
3105 (delete-other-windows)
3106 (with-output-to-temp-buffer "*Org Export/Publishing Help*" (princ help))
3107 (org-fit-window-to-buffer
3108 (get-buffer-window "*Org Export/Publishing Help*"))
3109 (funcall handle-keypress standard-prompt)))))
3112 (provide 'org-export)
3113 ;;; org-export.el ends here