Merge branch 'maint'
[org-mode.git] / lisp / org-exp.el
blob40f90c2735680f21d5dd75abe05678194e93be9a
1 ;;; org-exp.el --- Export internals for Org-mode
3 ;; Copyright (C) 2004-2013 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;;; Code:
29 (require 'org)
30 (require 'org-macs)
31 (require 'org-agenda)
32 (require 'ob-exp)
33 (require 'org-src)
35 (eval-when-compile
36 (require 'cl))
38 (declare-function org-export-latex-preprocess "org-latex" (parameters))
39 (declare-function org-export-ascii-preprocess "org-ascii" (parameters))
40 (declare-function org-export-html-preprocess "org-html" (parameters))
41 (declare-function org-export-docbook-preprocess "org-docbook" (parameters))
42 (declare-function org-infojs-options-inbuffer-template "org-jsinfo" ())
43 (declare-function org-export-htmlize-region-for-paste "org-html" (beg end))
44 (declare-function htmlize-buffer "ext:htmlize" (&optional buffer))
45 (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
46 (declare-function org-table-cookie-line-p "org-table" (line))
47 (declare-function org-table-colgroup-line-p "org-table" (line))
48 (declare-function org-pop-to-buffer-same-window "org-compat"
49 (&optional buffer-or-name norecord label))
50 (declare-function org-unescape-code-in-region "org-src" (beg end))
52 (autoload 'org-export-generic "org-export-generic" "Export using the generic exporter" t)
54 (autoload 'org-export-as-odt "org-odt"
55 "Export the outline to a OpenDocument Text file." t)
56 (autoload 'org-export-as-odt-and-open "org-odt"
57 "Export the outline to a OpenDocument Text file and open it." t)
59 (defgroup org-export nil
60 "Options for exporting org-listings."
61 :tag "Org Export"
62 :group 'org)
64 (defgroup org-export-general nil
65 "General options for exporting Org-mode files."
66 :tag "Org Export General"
67 :group 'org-export)
69 (defcustom org-export-allow-BIND 'confirm
70 "Non-nil means allow #+BIND to define local variable values for export.
71 This is a potential security risk, which is why the user must confirm the
72 use of these lines."
73 :group 'org-export-general
74 :type '(choice
75 (const :tag "Never" nil)
76 (const :tag "Always" t)
77 (const :tag "Make the user confirm for each file" confirm)))
79 ;; FIXME
80 (defvar org-export-publishing-directory nil)
82 (defcustom org-export-show-temporary-export-buffer t
83 "Non-nil means show buffer after exporting to temp buffer.
84 When Org exports to a file, the buffer visiting that file is ever
85 shown, but remains buried. However, when exporting to a temporary
86 buffer, that buffer is popped up in a second window. When this variable
87 is nil, the buffer remains buried also in these cases."
88 :group 'org-export-general
89 :type 'boolean)
91 (defcustom org-export-copy-to-kill-ring 'if-interactive
92 "Should we push exported content to the kill ring?"
93 :group 'org-export-general
94 :version "24.3"
95 :type '(choice
96 (const :tag "Always" t)
97 (const :tag "When export is done interactively" if-interactive)
98 (const :tag "Never" nil)))
100 (defcustom org-export-kill-product-buffer-when-displayed nil
101 "Non-nil means kill the product buffer if it is displayed immediately.
102 This applied to the commands `org-export-as-html-and-open' and
103 `org-export-as-pdf-and-open'."
104 :group 'org-export-general
105 :version "24.1"
106 :type 'boolean)
108 (defcustom org-export-run-in-background nil
109 "Non-nil means export and publishing commands will run in background.
110 This works by starting up a separate Emacs process visiting the same file
111 and doing the export from there.
112 Not all export commands are affected by this - only the ones which
113 actually write to a file, and that do not depend on the buffer state.
114 \\<org-mode-map>
115 If this option is nil, you can still get background export by calling
116 `org-export' with a double prefix arg: \
117 \\[universal-argument] \\[universal-argument] \\[org-export].
119 If this option is t, the double prefix can be used to exceptionally
120 force an export command into the current process."
121 :group 'org-export-general
122 :type 'boolean)
124 (defcustom org-export-initial-scope 'buffer
125 "The initial scope when exporting with `org-export'.
126 This variable can be either set to 'buffer or 'subtree."
127 :group 'org-export-general
128 :version "24.1"
129 :type '(choice
130 (const :tag "Export current buffer" 'buffer)
131 (const :tag "Export current subtree" 'subtree)))
133 (defcustom org-export-select-tags '("export")
134 "Tags that select a tree for export.
135 If any such tag is found in a buffer, all trees that do not carry one
136 of these tags will be deleted before export.
137 Inside trees that are selected like this, you can still deselect a
138 subtree by tagging it with one of the `org-export-exclude-tags'."
139 :group 'org-export-general
140 :type '(repeat (string :tag "Tag")))
142 (defcustom org-export-exclude-tags '("noexport")
143 "Tags that exclude a tree from export.
144 All trees carrying any of these tags will be excluded from export.
145 This is without condition, so even subtrees inside that carry one of the
146 `org-export-select-tags' will be removed."
147 :group 'org-export-general
148 :type '(repeat (string :tag "Tag")))
150 ;; FIXME: rename, this is a general variable
151 (defcustom org-export-html-expand t
152 "Non-nil means for HTML export, treat @<...> as HTML tag.
153 When nil, these tags will be exported as plain text and therefore
154 not be interpreted by a browser.
156 This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
157 :group 'org-export-html
158 :group 'org-export-general
159 :type 'boolean)
161 (defcustom org-export-with-special-strings t
162 "Non-nil means interpret \"\-\", \"--\" and \"---\" for export.
163 When this option is turned on, these strings will be exported as:
165 Org HTML LaTeX
166 -----+----------+--------
167 \\- &shy; \\-
168 -- &ndash; --
169 --- &mdash; ---
170 ... &hellip; \ldots
172 This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
173 :group 'org-export-translation
174 :type 'boolean)
176 (defcustom org-export-html-link-up ""
177 "Where should the \"UP\" link of exported HTML pages lead?"
178 :group 'org-export-html
179 :group 'org-export-general
180 :type '(string :tag "File or URL"))
182 (defcustom org-export-html-link-home ""
183 "Where should the \"HOME\" link of exported HTML pages lead?"
184 :group 'org-export-html
185 :group 'org-export-general
186 :type '(string :tag "File or URL"))
188 (defcustom org-export-language-setup
189 '(("en" "Author" "Date" "Table of Contents" "Footnotes")
190 ("ca" "Autor" "Data" "&Iacute;ndex" "Peus de p&agrave;gina")
191 ("cs" "Autor" "Datum" "Obsah" "Pozn\xe1mky pod carou")
192 ("da" "Ophavsmand" "Dato" "Indhold" "Fodnoter")
193 ("de" "Autor" "Datum" "Inhaltsverzeichnis" "Fu&szlig;noten")
194 ("eo" "A&#365;toro" "Dato" "Enhavo" "Piednotoj")
195 ("es" "Autor" "Fecha" "&Iacute;ndice" "Pies de p&aacute;gina")
196 ("fi" "Tekij&auml;" "P&auml;iv&auml;m&auml;&auml;r&auml;" "Sis&auml;llysluettelo" "Alaviitteet")
197 ("fr" "Auteur" "Date" "Sommaire" "Notes de bas de page")
198 ("hu" "Szerz&otilde;" "D&aacute;tum" "Tartalomjegyz&eacute;k" "L&aacute;bjegyzet")
199 ("is" "H&ouml;fundur" "Dagsetning" "Efnisyfirlit" "Aftanm&aacute;lsgreinar")
200 ("it" "Autore" "Data" "Indice" "Note a pi&egrave; di pagina")
201 ;; Use numeric character entities for proper rendering of non-UTF8 documents
202 ;; ("ja" "著者" "日付" "目次" "脚注")
203 ("ja" "&#33879;&#32773;" "&#26085;&#20184;" "&#30446;&#27425;" "&#33050;&#27880;")
204 ("nl" "Auteur" "Datum" "Inhoudsopgave" "Voetnoten")
205 ("no" "Forfatter" "Dato" "Innhold" "Fotnoter")
206 ("nb" "Forfatter" "Dato" "Innhold" "Fotnoter") ;; nb = Norsk (bokm.l)
207 ("nn" "Forfattar" "Dato" "Innhald" "Fotnotar") ;; nn = Norsk (nynorsk)
208 ("pl" "Autor" "Data" "Spis tre&#x015b;ci" "Przypis")
209 ;; Use numeric character entities for proper rendering of non-UTF8 documents
210 ;; ("ru" "Автор" "Дата" "Содержание" "Сноски")
211 ("ru" "&#1040;&#1074;&#1090;&#1086;&#1088;" "&#1044;&#1072;&#1090;&#1072;" "&#1057;&#1086;&#1076;&#1077;&#1088;&#1078;&#1072;&#1085;&#1080;&#1077;" "&#1057;&#1085;&#1086;&#1089;&#1082;&#1080;")
212 ("sv" "F&ouml;rfattare" "Datum" "Inneh&aring;ll" "Fotnoter")
213 ;; Use numeric character entities for proper rendering of non-UTF8 documents
214 ;; ("uk" "Автор" "Дата" "Зміст" "Примітки")
215 ("uk" "&#1040;&#1074;&#1090;&#1086;&#1088;" "&#1044;&#1072;&#1090;&#1072;" "&#1047;&#1084;&#1110;&#1089;&#1090;" "&#1055;&#1088;&#1080;&#1084;&#1110;&#1090;&#1082;&#1080;")
216 ;; Use numeric character entities for proper rendering of non-UTF8 documents
217 ;; ("zh-CN" "作者" "日期" "目录" "脚注")
218 ("zh-CN" "&#20316;&#32773;" "&#26085;&#26399;" "&#30446;&#24405;" "&#33050;&#27880;")
219 ;; Use numeric character entities for proper rendering of non-UTF8 documents
220 ;; ("zh-TW" "作者" "日期" "目錄" "腳註")
221 ("zh-TW" "&#20316;&#32773;" "&#26085;&#26399;" "&#30446;&#37636;" "&#33139;&#35387;"))
222 "Terms used in export text, translated to different languages.
223 Use the variable `org-export-default-language' to set the language,
224 or use the +OPTION lines for a per-file setting."
225 :group 'org-export-general
226 :type '(repeat
227 (list
228 (string :tag "HTML language tag")
229 (string :tag "Author")
230 (string :tag "Date")
231 (string :tag "Table of Contents")
232 (string :tag "Footnotes"))))
234 (defcustom org-export-default-language "en"
235 "The default language for export and clocktable translations, as a string.
236 This should have an association in `org-export-language-setup'
237 and in `org-clock-clocktable-language-setup'."
238 :group 'org-export-general
239 :type 'string)
241 (defcustom org-export-date-timestamp-format "%Y-%m-%d"
242 "Time string format for Org timestamps in the #+DATE option."
243 :group 'org-export-general
244 :version "24.1"
245 :type 'string)
247 (defvar org-export-page-description ""
248 "The page description, for the XHTML meta tag.
249 This is best set with the #+DESCRIPTION line in a file, it does not make
250 sense to set this globally.")
252 (defvar org-export-page-keywords ""
253 "The page description, for the XHTML meta tag.
254 This is best set with the #+KEYWORDS line in a file, it does not make
255 sense to set this globally.")
257 (defcustom org-export-skip-text-before-1st-heading nil
258 "Non-nil means skip all text before the first headline when exporting.
259 When nil, that text is exported as well."
260 :group 'org-export-general
261 :type 'boolean)
263 (defcustom org-export-headline-levels 3
264 "The last level which is still exported as a headline.
265 Inferior levels will produce itemize lists when exported.
266 Note that a numeric prefix argument to an exporter function overrides
267 this setting.
269 This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
270 :group 'org-export-general
271 :type 'integer)
273 (defcustom org-export-with-section-numbers t
274 "Non-nil means add section numbers to headlines when exporting.
276 This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
277 :group 'org-export-general
278 :type 'boolean)
280 (defcustom org-export-section-number-format '((("1" ".")) . "")
281 "Format of section numbers for export.
282 The variable has two components.
283 1. A list of lists, each indicating a counter type and a separator.
284 The counter type can be any of \"1\", \"A\", \"a\", \"I\", or \"i\".
285 It causes causes numeric, alphabetic, or roman counters, respectively.
286 The separator is only used if another counter for a subsection is being
287 added.
288 If there are more numbered section levels than entries in this lists,
289 then the last entry will be reused.
290 2. A terminator string that will be added after the entire
291 section number."
292 :group 'org-export-general
293 :type '(cons
294 (repeat
295 (list
296 (string :tag "Counter Type")
297 (string :tag "Separator ")))
298 (string :tag "Terminator")))
300 (defcustom org-export-with-toc t
301 "Non-nil means create a table of contents in exported files.
302 The TOC contains headlines with levels up to`org-export-headline-levels'.
303 When an integer, include levels up to N in the toc, this may then be
304 different from `org-export-headline-levels', but it will not be allowed
305 to be larger than the number of headline levels.
306 When nil, no table of contents is made.
308 Headlines which contain any TODO items will be marked with \"(*)\" in
309 ASCII export, and with red color in HTML output, if the option
310 `org-export-mark-todo-in-toc' is set.
312 In HTML output, the TOC will be clickable.
314 This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
315 or \"toc:3\"."
316 :group 'org-export-general
317 :type '(choice
318 (const :tag "No Table of Contents" nil)
319 (const :tag "Full Table of Contents" t)
320 (integer :tag "TOC to level")))
322 (defcustom org-export-mark-todo-in-toc nil
323 "Non-nil means mark TOC lines that contain any open TODO items."
324 :group 'org-export-general
325 :type 'boolean)
327 (defcustom org-export-with-todo-keywords t
328 "Non-nil means include TODO keywords in export.
329 When nil, remove all these keywords from the export."
330 :group 'org-export-general
331 :type 'boolean)
333 (defcustom org-export-with-tasks t
334 "Non-nil means include TODO items for export.
335 This may have the following values:
336 t include tasks independent of state.
337 todo include only tasks that are not yet done.
338 done include only tasks that are already done.
339 nil remove all tasks before export
340 list of TODO kwds keep only tasks with these keywords"
341 :group 'org-export-general
342 :version "24.1"
343 :type '(choice
344 (const :tag "All tasks" t)
345 (const :tag "No tasks" nil)
346 (const :tag "Not-done tasks" todo)
347 (const :tag "Only done tasks" done)
348 (repeat :tag "Specific TODO keywords"
349 (string :tag "Keyword"))))
351 (defcustom org-export-with-priority nil
352 "Non-nil means include priority cookies in export.
353 When nil, remove priority cookies for export."
354 :group 'org-export-general
355 :type 'boolean)
357 (defcustom org-export-preserve-breaks nil
358 "Non-nil means preserve all line breaks when exporting.
359 Normally, in HTML output paragraphs will be reformatted. In ASCII
360 export, line breaks will always be preserved, regardless of this variable.
362 This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
363 :group 'org-export-general
364 :type 'boolean)
366 (defcustom org-export-with-archived-trees 'headline
367 "Whether subtrees with the ARCHIVE tag should be exported.
368 This can have three different values
369 nil Do not export, pretend this tree is not present
370 t Do export the entire tree
371 headline Only export the headline, but skip the tree below it."
372 :group 'org-export-general
373 :group 'org-archive
374 :type '(choice
375 (const :tag "not at all" nil)
376 (const :tag "headline only" 'headline)
377 (const :tag "entirely" t)))
379 (defcustom org-export-author-info t
380 "Non-nil means insert author name and email into the exported file.
382 This option can also be set with the +OPTIONS line,
383 e.g. \"author:nil\"."
384 :group 'org-export-general
385 :type 'boolean)
387 (defcustom org-export-email-info nil
388 "Non-nil means insert author name and email into the exported file.
390 This option can also be set with the +OPTIONS line,
391 e.g. \"email:t\"."
392 :group 'org-export-general
393 :version "24.1"
394 :type 'boolean)
396 (defcustom org-export-creator-info t
397 "Non-nil means the postamble should contain a creator sentence.
398 This sentence is \"HTML generated by org-mode XX in emacs XXX\"."
399 :group 'org-export-general
400 :type 'boolean)
402 (defcustom org-export-time-stamp-file t
403 "Non-nil means insert a time stamp into the exported file.
404 The time stamp shows when the file was created.
406 This option can also be set with the +OPTIONS line,
407 e.g. \"timestamp:nil\"."
408 :group 'org-export-general
409 :type 'boolean)
411 (defcustom org-export-with-timestamps t
412 "If nil, do not export time stamps and associated keywords."
413 :group 'org-export-general
414 :type 'boolean)
416 (defcustom org-export-remove-timestamps-from-toc t
417 "If t, remove timestamps from the table of contents entries."
418 :group 'org-export-general
419 :type 'boolean)
421 (defcustom org-export-with-tags 'not-in-toc
422 "If nil, do not export tags, just remove them from headlines.
423 If this is the symbol `not-in-toc', tags will be removed from table of
424 contents entries, but still be shown in the headlines of the document.
426 This option can also be set with the +OPTIONS line, e.g. \"tags:nil\"."
427 :group 'org-export-general
428 :type '(choice
429 (const :tag "Off" nil)
430 (const :tag "Not in TOC" not-in-toc)
431 (const :tag "On" t)))
433 (defcustom org-export-with-drawers nil
434 "Non-nil means export with drawers like the property drawer.
435 When t, all drawers are exported. This may also be a list of
436 drawer names to export."
437 :group 'org-export-general
438 :type '(choice
439 (const :tag "All drawers" t)
440 (const :tag "None" nil)
441 (repeat :tag "Selected drawers"
442 (string :tag "Drawer name"))))
444 (defvar org-export-first-hook nil
445 "Hook called as the first thing in each exporter.
446 Point will be still in the original buffer.
447 Good for general initialization")
449 (defvar org-export-preprocess-hook nil
450 "Hook for preprocessing an export buffer.
451 Pretty much the first thing when exporting is running this hook.
452 Point will be in a temporary buffer that contains a copy of
453 the original buffer, or of the section that is being exported.
454 All the other hooks in the org-export-preprocess... category
455 also work in that temporary buffer, already modified by various
456 stages of the processing.")
458 (defvar org-export-preprocess-after-include-files-hook nil
459 "Hook for preprocessing an export buffer.
460 This is run after the contents of included files have been inserted.")
462 (defvar org-export-preprocess-after-tree-selection-hook nil
463 "Hook for preprocessing an export buffer.
464 This is run after selection of trees to be exported has happened.
465 This selection includes tags-based selection, as well as removal
466 of commented and archived trees.")
468 (defvar org-export-preprocess-after-headline-targets-hook nil
469 "Hook for preprocessing export buffer.
470 This is run just after the headline targets have been defined and
471 the target-alist has been set up.")
473 (defvar org-export-preprocess-before-selecting-backend-code-hook nil
474 "Hook for preprocessing an export buffer.
475 This is run just before backend-specific blocks get selected.")
477 (defvar org-export-preprocess-after-blockquote-hook nil
478 "Hook for preprocessing an export buffer.
479 This is run after blockquote/quote/verse/center have been marked
480 with cookies.")
482 (defvar org-export-preprocess-after-radio-targets-hook nil
483 "Hook for preprocessing an export buffer.
484 This is run after radio target processing.")
486 (defvar org-export-preprocess-before-normalizing-links-hook nil
487 "Hook for preprocessing an export buffer.
488 This hook is run before links are normalized.")
490 (defvar org-export-preprocess-before-backend-specifics-hook nil
491 "Hook run before backend-specific functions are called during preprocessing.")
493 (defvar org-export-preprocess-final-hook nil
494 "Hook for preprocessing an export buffer.
495 This is run as the last thing in the preprocessing buffer, just before
496 returning the buffer string to the backend.")
498 (defgroup org-export-translation nil
499 "Options for translating special ascii sequences for the export backends."
500 :tag "Org Export Translation"
501 :group 'org-export)
503 (defcustom org-export-with-emphasize t
504 "Non-nil means interpret *word*, /word/, and _word_ as emphasized text.
505 If the export target supports emphasizing text, the word will be
506 typeset in bold, italic, or underlined, respectively. Works only for
507 single words, but you can say: I *really* *mean* *this*.
508 Not all export backends support this.
510 This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
511 :group 'org-export-translation
512 :type 'boolean)
514 (defcustom org-export-with-footnotes t
515 "If nil, export [1] as a footnote marker.
516 Lines starting with [1] will be formatted as footnotes.
518 This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
519 :group 'org-export-translation
520 :type 'boolean)
522 (defcustom org-export-with-TeX-macros t
523 "Non-nil means interpret simple TeX-like macros when exporting.
524 For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
525 Not only real TeX macros will work here, but the standard HTML entities
526 for math can be used as macro names as well. For a list of supported
527 names in HTML export, see the constant `org-entities' and the user option
528 `org-entities-user'.
529 Not all export backends support this.
531 This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
532 :group 'org-export-translation
533 :group 'org-export-latex
534 :type 'boolean)
536 (defcustom org-export-with-LaTeX-fragments t
537 "Non-nil means process LaTeX math fragments for HTML display.
538 When set, the exporter will find and process LaTeX environments if the
539 \\begin line is the first non-white thing on a line. It will also find
540 and process the math delimiters like $a=b$ and \\( a=b \\) for inline math,
541 $$a=b$$ and \\=\\[ a=b \\] for display math.
543 This option can also be set with the +OPTIONS line, e.g. \"LaTeX:mathjax\".
545 Allowed values are:
547 nil Don't do anything.
548 verbatim Keep everything in verbatim
549 dvipng Process the LaTeX fragments to images.
550 This will also include processing of non-math environments.
551 imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
552 to convert pdf files to png files.
553 t Do MathJax preprocessing if there is at least on math snippet,
554 and arrange for MathJax.js to be loaded.
556 The default is nil, because this option needs the `dvipng' program which
557 is not available on all systems."
558 :group 'org-export-translation
559 :group 'org-export-latex
560 :type '(choice
561 (const :tag "Do not process math in any way" nil)
562 (const :tag "Obsolete, use dvipng setting" t)
563 (const :tag "Use dvipng to make images" dvipng)
564 (const :tag "Use imagemagick to make images" imagemagick)
565 (const :tag "Use MathJax to display math" mathjax)
566 (const :tag "Leave math verbatim" verbatim)))
568 (defcustom org-export-with-fixed-width t
569 "Non-nil means lines starting with \":\" will be in fixed width font.
570 This can be used to have pre-formatted text, fragments of code etc. For
571 example:
572 : ;; Some Lisp examples
573 : (while (defc cnt)
574 : (ding))
575 will be looking just like this in also HTML. See also the QUOTE keyword.
576 Not all export backends support this.
578 This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
579 :group 'org-export-translation
580 :type 'boolean)
582 (defgroup org-export-tables nil
583 "Options for exporting tables in Org-mode."
584 :tag "Org Export Tables"
585 :group 'org-export)
587 (defcustom org-export-with-tables t
588 "If non-nil, lines starting with \"|\" define a table.
589 For example:
591 | Name | Address | Birthday |
592 |-------------+----------+-----------|
593 | Arthur Dent | England | 29.2.2100 |
595 Not all export backends support this.
597 This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
598 :group 'org-export-tables
599 :type 'boolean)
601 (defcustom org-export-highlight-first-table-line t
602 "Non-nil means highlight the first table line.
603 In HTML export, this means use <th> instead of <td>.
604 In tables created with table.el, this applies to the first table line.
605 In Org-mode tables, all lines before the first horizontal separator
606 line will be formatted with <th> tags."
607 :group 'org-export-tables
608 :type 'boolean)
610 (defcustom org-export-table-remove-special-lines t
611 "Remove special lines and marking characters in calculating tables.
612 This removes the special marking character column from tables that are set
613 up for spreadsheet calculations. It also removes the entire lines
614 marked with `!', `_', or `^'. The lines with `$' are kept, because
615 the values of constants may be useful to have."
616 :group 'org-export-tables
617 :type 'boolean)
619 (defcustom org-export-table-remove-empty-lines t
620 "Remove empty lines when exporting tables.
621 This is the global equivalent of the :remove-nil-lines option
622 when locally sending a table with #+ORGTBL."
623 :group 'org-export-tables
624 :version "24.1"
625 :type 'boolean)
627 (defcustom org-export-prefer-native-exporter-for-tables nil
628 "Non-nil means always export tables created with table.el natively.
629 Natively means use the HTML code generator in table.el.
630 When nil, Org-mode's own HTML generator is used when possible (i.e. if
631 the table does not use row- or column-spanning). This has the
632 advantage, that the automatic HTML conversions for math symbols and
633 sub/superscripts can be applied. Org-mode's HTML generator is also
634 much faster. The LaTeX exporter always use the native exporter for
635 table.el tables."
636 :group 'org-export-tables
637 :type 'boolean)
639 ;;;; Exporting
641 ;;; Variables, constants, and parameter plists
643 (defconst org-level-max 20)
645 (defvar org-export-current-backend nil
646 "During export, this will be bound to a symbol such as 'html,
647 'latex, 'docbook, 'ascii, etc, indicating which of the export
648 backends is in use. Otherwise it has the value nil. Users
649 should not attempt to change the value of this variable
650 directly, but it can be used in code to test whether export is
651 in progress, and if so, what the backend is.")
653 (defvar org-current-export-file nil) ; dynamically scoped parameter
654 (defvar org-current-export-dir nil) ; dynamically scoped parameter
655 (defvar org-export-opt-plist nil
656 "Contains the current option plist.")
657 (defvar org-last-level nil) ; dynamically scoped variable
658 (defvar org-min-level nil) ; dynamically scoped variable
659 (defvar org-levels-open nil) ; dynamically scoped parameter
660 (defvar org-export-footnotes-data nil
661 "Alist of labels used in buffers, along with their definition.")
662 (defvar org-export-footnotes-seen nil
663 "Alist of labels encountered so far by the exporter, along with their definition.")
666 (defconst org-export-plist-vars
667 '((:link-up nil org-export-html-link-up)
668 (:link-home nil org-export-html-link-home)
669 (:language nil org-export-default-language)
670 (:keywords nil org-export-page-keywords)
671 (:description nil org-export-page-description)
672 (:customtime nil org-display-custom-times)
673 (:headline-levels "H" org-export-headline-levels)
674 (:section-numbers "num" org-export-with-section-numbers)
675 (:section-number-format nil org-export-section-number-format)
676 (:table-of-contents "toc" org-export-with-toc)
677 (:preserve-breaks "\\n" org-export-preserve-breaks)
678 (:archived-trees nil org-export-with-archived-trees)
679 (:emphasize "*" org-export-with-emphasize)
680 (:sub-superscript "^" org-export-with-sub-superscripts)
681 (:special-strings "-" org-export-with-special-strings)
682 (:footnotes "f" org-export-with-footnotes)
683 (:drawers "d" org-export-with-drawers)
684 (:tags "tags" org-export-with-tags)
685 (:todo-keywords "todo" org-export-with-todo-keywords)
686 (:tasks "tasks" org-export-with-tasks)
687 (:priority "pri" org-export-with-priority)
688 (:TeX-macros "TeX" org-export-with-TeX-macros)
689 (:LaTeX-fragments "LaTeX" org-export-with-LaTeX-fragments)
690 (:latex-listings nil org-export-latex-listings)
691 (:skip-before-1st-heading "skip" org-export-skip-text-before-1st-heading)
692 (:fixed-width ":" org-export-with-fixed-width)
693 (:timestamps "<" org-export-with-timestamps)
694 (:author nil user-full-name)
695 (:email nil user-mail-address)
696 (:author-info "author" org-export-author-info)
697 (:email-info "email" org-export-email-info)
698 (:creator-info "creator" org-export-creator-info)
699 (:time-stamp-file "timestamp" org-export-time-stamp-file)
700 (:tables "|" org-export-with-tables)
701 (:table-auto-headline nil org-export-highlight-first-table-line)
702 (:style-include-default nil org-export-html-style-include-default)
703 (:style-include-scripts nil org-export-html-style-include-scripts)
704 (:style nil org-export-html-style)
705 (:style-extra nil org-export-html-style-extra)
706 (:agenda-style nil org-agenda-export-html-style)
707 (:convert-org-links nil org-export-html-link-org-files-as-html)
708 (:html-inline-images nil org-export-html-inline-images)
709 (:latex-inline-images nil org-export-latex-inline-images)
710 (:odt-inline-images nil org-export-odt-inline-images)
711 (:docbook-inline-images nil org-export-docbook-inline-images)
712 (:html-extension nil org-export-html-extension)
713 (:html-preamble nil org-export-html-preamble)
714 (:html-postamble nil org-export-html-postamble)
715 (:xml-declaration nil org-export-html-xml-declaration)
716 (:html-table-tag nil org-export-html-table-tag)
717 (:expand-quoted-html "@" org-export-html-expand)
718 (:timestamp nil org-export-html-with-timestamp)
719 (:publishing-directory nil org-export-publishing-directory)
720 (:select-tags nil org-export-select-tags)
721 (:exclude-tags nil org-export-exclude-tags)
723 (:latex-image-options nil org-export-latex-image-default-option))
724 "List of properties that represent export/publishing variables.
725 Each element is a list of 3 items:
726 1. The property that is used internally, and also for org-publish-project-alist
727 2. The string that can be used in the OPTION lines to set this option,
728 or nil if this option cannot be changed in this way
729 3. The customization variable that sets the default for this option."
732 (defun org-default-export-plist ()
733 "Return the property list with default settings for the export variables."
734 (let* ((infile (org-infile-export-plist))
735 (letbind (plist-get infile :let-bind))
736 (l org-export-plist-vars) rtn e s v)
737 (while (setq e (pop l))
738 (setq s (nth 2 e)
739 v (cond
740 ((assq s letbind) (nth 1 (assq s letbind)))
741 ((boundp s) (symbol-value s)))
742 rtn (cons (car e) (cons v rtn))))
743 rtn))
745 (defvar org-export-inbuffer-options-extra nil
746 "List of additional in-buffer options that should be detected.
747 Just before export, the buffer is scanned for options like #+TITLE, #+EMAIL,
748 etc. Extensions can add to this list to get their options detected, and they
749 can then add a function to `org-export-options-filters' to process these
750 options.
751 Each element in this list must be a list, with the in-buffer keyword as car,
752 and a property (a symbol) as the next element. All occurrences of the
753 keyword will be found, the values concatenated with a space character
754 in between, and the result stored in the export options property list.")
756 (defvar org-export-options-filters nil
757 "Functions to be called to finalize the export/publishing options.
758 All these options are stored in a property list, and each of the functions
759 in this hook gets a chance to modify this property list. Each function
760 must accept the property list as an argument, and must return the (possibly
761 modified) list.")
763 ;; FIXME: should we fold case here?
765 (defun org-infile-export-plist ()
766 "Return the property list with file-local settings for export."
767 (save-excursion
768 (save-restriction
769 (widen)
770 (goto-char (point-min))
771 (let ((re (org-make-options-regexp
772 (append
773 '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE"
774 "MATHJAX"
775 "LINK_UP" "LINK_HOME" "SETUPFILE" "STYLE"
776 "LATEX_HEADER" "LATEX_CLASS" "LATEX_CLASS_OPTIONS"
777 "EXPORT_SELECT_TAGS" "EXPORT_EXCLUDE_TAGS"
778 "KEYWORDS" "DESCRIPTION" "MACRO" "BIND" "XSLT")
779 (mapcar 'car org-export-inbuffer-options-extra))))
780 (case-fold-search t)
781 p key val text options mathjax a pr style
782 latex-header latex-class latex-class-options macros letbind
783 ext-setup-or-nil setup-file setup-dir setup-contents (start 0))
784 (while (or (and ext-setup-or-nil
785 (string-match re ext-setup-or-nil start)
786 (setq start (match-end 0)))
787 (and (setq ext-setup-or-nil nil start 0)
788 (re-search-forward re nil t)))
789 (setq key (upcase (org-match-string-no-properties 1 ext-setup-or-nil))
790 val (org-match-string-no-properties 2 ext-setup-or-nil))
791 (cond
792 ((setq a (assoc key org-export-inbuffer-options-extra))
793 (setq pr (nth 1 a))
794 (setq p (plist-put p pr (concat (plist-get p pr) " " val))))
795 ((string-equal key "TITLE") (setq p (plist-put p :title val)))
796 ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
797 ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
798 ((string-equal key "DATE")
799 ;; If date is an Org timestamp, convert it to a time
800 ;; string using `org-export-date-timestamp-format'
801 (when (string-match org-ts-regexp3 val)
802 (setq val (format-time-string
803 org-export-date-timestamp-format
804 (apply 'encode-time (org-parse-time-string
805 (match-string 0 val))))))
806 (setq p (plist-put p :date val)))
807 ((string-equal key "KEYWORDS") (setq p (plist-put p :keywords val)))
808 ((string-equal key "DESCRIPTION")
809 (setq p (plist-put p :description val)))
810 ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
811 ((string-equal key "STYLE")
812 (setq style (concat style "\n" val)))
813 ((string-equal key "LATEX_HEADER")
814 (setq latex-header (concat latex-header "\n" val)))
815 ((string-equal key "LATEX_CLASS")
816 (setq latex-class val))
817 ((string-equal key "LATEX_CLASS_OPTIONS")
818 (setq latex-class-options val))
819 ((string-equal key "TEXT")
820 (setq text (if text (concat text "\n" val) val)))
821 ((string-equal key "OPTIONS")
822 (setq options (concat val " " options)))
823 ((string-equal key "MATHJAX")
824 (setq mathjax (concat val " " mathjax)))
825 ((string-equal key "BIND")
826 (push (read (concat "(" val ")")) letbind))
827 ((string-equal key "XSLT")
828 (setq p (plist-put p :xslt val)))
829 ((string-equal key "LINK_UP")
830 (setq p (plist-put p :link-up val)))
831 ((string-equal key "LINK_HOME")
832 (setq p (plist-put p :link-home val)))
833 ((string-equal key "EXPORT_SELECT_TAGS")
834 (setq p (plist-put p :select-tags (org-split-string val))))
835 ((string-equal key "EXPORT_EXCLUDE_TAGS")
836 (setq p (plist-put p :exclude-tags (org-split-string val))))
837 ((string-equal key "MACRO")
838 (push val macros))
839 ((equal key "SETUPFILE")
840 (setq setup-file (org-remove-double-quotes (org-trim val))
841 ;; take care of recursive inclusion of setupfiles
842 setup-file (if (or (file-name-absolute-p val) (not setup-dir))
843 (expand-file-name setup-file)
844 (let ((default-directory setup-dir))
845 (expand-file-name setup-file))))
846 (setq setup-dir (file-name-directory setup-file))
847 (setq setup-contents (org-file-contents setup-file 'noerror))
848 (if (not ext-setup-or-nil)
849 (setq ext-setup-or-nil setup-contents start 0)
850 (setq ext-setup-or-nil
851 (concat (substring ext-setup-or-nil 0 start)
852 "\n" setup-contents "\n"
853 (substring ext-setup-or-nil start)))))))
854 (setq p (plist-put p :text text))
855 (when (and letbind (org-export-confirm-letbind))
856 (setq p (plist-put p :let-bind letbind)))
857 (when style (setq p (plist-put p :style-extra style)))
858 (when latex-header
859 (setq p (plist-put p :latex-header-extra (substring latex-header 1))))
860 (when latex-class
861 (setq p (plist-put p :latex-class latex-class)))
862 (when latex-class-options
863 (setq p (plist-put p :latex-class-options latex-class-options)))
864 (when options
865 (setq p (org-export-add-options-to-plist p options)))
866 (when mathjax
867 (setq p (plist-put p :mathjax mathjax)))
868 ;; Add macro definitions
869 (setq p (plist-put p :macro-date "(eval (format-time-string \"$1\"))"))
870 (setq p (plist-put p :macro-time "(eval (format-time-string \"$1\"))"))
871 (setq p (plist-put p :macro-property "(eval (org-entry-get nil \"$1\" 'selective))"))
872 (setq p (plist-put
873 p :macro-modification-time
874 (and (buffer-file-name)
875 (file-exists-p (buffer-file-name))
876 (concat
877 "(eval (format-time-string \"$1\" '"
878 (prin1-to-string (nth 5 (file-attributes
879 (buffer-file-name))))
880 "))"))))
881 (setq p (plist-put p :macro-input-file (and (buffer-file-name)
882 (file-name-nondirectory
883 (buffer-file-name)))))
884 (while (setq val (pop macros))
885 (when (string-match "^\\([-a-zA-Z0-9_]+\\)[ \t]+\\(.*?[ \t]*$\\)" val)
886 (setq p (plist-put
887 p (intern
888 (concat ":macro-" (downcase (match-string 1 val))))
889 (org-export-interpolate-newlines (match-string 2 val))))))
890 p))))
892 (defun org-export-interpolate-newlines (s)
893 (while (string-match "\\\\n" s)
894 (setq s (replace-match "\n" t t s)))
897 (defvar org-export-allow-BIND-local nil)
898 (defun org-export-confirm-letbind ()
899 "Can we use #+BIND values during export?
900 By default this will ask for confirmation by the user, to divert possible
901 security risks."
902 (cond
903 ((not org-export-allow-BIND) nil)
904 ((eq org-export-allow-BIND t) t)
905 ((local-variable-p 'org-export-allow-BIND-local (current-buffer))
906 org-export-allow-BIND-local)
907 (t (org-set-local 'org-export-allow-BIND-local
908 (yes-or-no-p "Allow BIND values in this buffer? ")))))
910 (defun org-install-letbind ()
911 "Install the values from #+BIND lines as local variables."
912 (let ((letbind (plist-get org-export-opt-plist :let-bind))
913 pair)
914 (while (setq pair (pop letbind))
915 (org-set-local (car pair) (nth 1 pair)))))
917 (defun org-export-add-options-to-plist (p options)
918 "Parse an OPTIONS line and set values in the property list P."
919 (let (o)
920 (when options
921 (let ((op org-export-plist-vars))
922 (while (setq o (pop op))
923 (if (and (nth 1 o)
924 (string-match (concat "\\(\\`\\|[ \t]\\)"
925 (regexp-quote (nth 1 o))
926 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
927 options))
928 (setq p (plist-put p (car o)
929 (car (read-from-string
930 (match-string 2 options))))))))))
933 (defun org-export-add-subtree-options (p pos)
934 "Add options in subtree at position POS to property list P."
935 (save-excursion
936 (goto-char pos)
937 (when (org-at-heading-p)
938 (let (a)
939 ;; This is actually read in `org-export-get-title-from-subtree'
940 ;; (when (setq a (org-entry-get pos "EXPORT_TITLE"))
941 ;; (setq p (plist-put p :title a)))
942 (when (setq a (org-entry-get pos "EXPORT_TEXT"))
943 (setq p (plist-put p :text a)))
944 (when (setq a (org-entry-get pos "EXPORT_AUTHOR"))
945 (setq p (plist-put p :author a)))
946 (when (setq a (org-entry-get pos "EXPORT_DATE"))
947 (setq p (plist-put p :date a)))
948 (when (setq a (org-entry-get pos "EXPORT_OPTIONS"))
949 (setq p (org-export-add-options-to-plist p a)))))
952 (defun org-export-directory (type plist)
953 (let* ((val (plist-get plist :publishing-directory))
954 (dir (if (listp val)
955 (or (cdr (assoc type val)) ".")
956 val)))
957 dir))
959 (defun org-export-process-option-filters (plist)
960 (let ((functions org-export-options-filters) f)
961 (while (setq f (pop functions))
962 (setq plist (funcall f plist))))
963 plist)
965 ;;;###autoload
966 (defun org-export (&optional arg)
967 "Export dispatcher for Org-mode.
968 When `org-export-run-in-background' is non-nil, try to run the command
969 in the background. This will be done only for commands that write
970 to a file. For details see the docstring of `org-export-run-in-background'.
972 The prefix argument ARG will be passed to the exporter. However, if
973 ARG is a double universal prefix \\[universal-argument] \\[universal-argument], \
974 that means to inverse the
975 value of `org-export-run-in-background'.
977 If `org-export-initial-scope' is set to 'subtree, try to export
978 the current subtree, otherwise try to export the whole buffer.
979 Pressing `1' will switch between these two options."
980 (interactive "P")
981 (let* ((bg (org-xor (equal arg '(16)) org-export-run-in-background))
982 (subtree-p (or (org-region-active-p)
983 (eq org-export-initial-scope 'subtree)))
984 (regb (and (org-region-active-p) (region-beginning)))
985 (rege (and (org-region-active-p) (region-end)))
986 (help "[t] insert the export option template
987 \[v] limit export to visible part of outline tree
988 \[1] switch buffer/subtree export
989 \[SPC] publish enclosing subtree (with LaTeX_CLASS or EXPORT_FILE_NAME prop)
991 \[a/n/u] export as ASCII/Latin-1/UTF-8 [A/N/U] to temporary buffer
993 \[h] export as HTML [H] to temporary buffer [R] export region
994 \[b] export as HTML and open in browser
996 \[l] export as LaTeX [L] to temporary buffer
997 \[p] export as LaTeX and process to PDF [d] ... and open PDF file
999 \[D] export as DocBook [V] export as DocBook, process to PDF, and open
1001 \[o] export as OpenDocument Text [O] ... and open
1003 \[j] export as TaskJuggler [J] ... and open
1005 \[m] export as Freemind mind map
1006 \[x] export as XOXO
1007 \[g] export using Wes Hardaker's generic exporter
1009 \[i] export current file as iCalendar file
1010 \[I] export all agenda files as iCalendar files [c] ...as one combined file
1012 \[F] publish current file [P] publish current project
1013 \[X] publish a project... [E] publish every projects")
1014 (cmds
1015 '((?t org-insert-export-options-template nil)
1016 (?v org-export-visible nil)
1017 (?a org-export-as-ascii t)
1018 (?A org-export-as-ascii-to-buffer t)
1019 (?n org-export-as-latin1 t)
1020 (?N org-export-as-latin1-to-buffer t)
1021 (?u org-export-as-utf8 t)
1022 (?U org-export-as-utf8-to-buffer t)
1023 (?h org-export-as-html t)
1024 (?b org-export-as-html-and-open t)
1025 (?H org-export-as-html-to-buffer nil)
1026 (?R org-export-region-as-html nil)
1027 (?x org-export-as-xoxo t)
1028 (?g org-export-generic t)
1029 (?D org-export-as-docbook t)
1030 (?V org-export-as-docbook-pdf-and-open t)
1031 (?o org-export-as-odt t)
1032 (?O org-export-as-odt-and-open t)
1033 (?j org-export-as-taskjuggler t)
1034 (?J org-export-as-taskjuggler-and-open t)
1035 (?m org-export-as-freemind t)
1036 (?l org-export-as-latex t)
1037 (?p org-export-as-pdf t)
1038 (?d org-export-as-pdf-and-open t)
1039 (?L org-export-as-latex-to-buffer nil)
1040 (?i org-export-icalendar-this-file t)
1041 (?I org-export-icalendar-all-agenda-files t)
1042 (?c org-export-icalendar-combine-agenda-files t)
1043 (?F org-publish-current-file t)
1044 (?P org-publish-current-project t)
1045 (?X org-publish t)
1046 (?E org-publish-all t)))
1047 r1 r2 ass
1048 (cpos (point)) (cbuf (current-buffer)) bpos)
1049 (save-excursion
1050 (save-window-excursion
1051 (if subtree-p
1052 (message "Export subtree: ")
1053 (message "Export buffer: "))
1054 (delete-other-windows)
1055 (with-output-to-temp-buffer "*Org Export/Publishing Help*"
1056 (princ help))
1057 (org-fit-window-to-buffer (get-buffer-window
1058 "*Org Export/Publishing Help*"))
1059 (while (eq (setq r1 (read-char-exclusive)) ?1)
1060 (cond (subtree-p
1061 (setq subtree-p nil)
1062 (message "Export buffer: "))
1063 ((not subtree-p)
1064 (setq subtree-p t)
1065 (setq bpos (point))
1066 (org-mark-subtree)
1067 (org-activate-mark)
1068 (setq regb (and (org-region-active-p) (region-beginning)))
1069 (setq rege (and (org-region-active-p) (region-end)))
1070 (message "Export subtree: "))))
1071 (when (eq r1 ?\ )
1072 (let ((case-fold-search t)
1073 (end (save-excursion (while (org-up-heading-safe)) (point))))
1074 (outline-next-heading)
1075 (if (re-search-backward
1076 "^[ \t]+\\(:latex_class:\\|:export_title:\\|:export_file_name:\\)[ \t]+\\S-"
1077 end t)
1078 (progn
1079 (org-back-to-heading t)
1080 (setq subtree-p t)
1081 (setq bpos (point))
1082 (message "Select command (for subtree): ")
1083 (setq r1 (read-char-exclusive)))
1084 (error "No enclosing node with LaTeX_CLASS or EXPORT_TITLE or EXPORT_FILE_NAME")
1085 )))))
1086 (if (fboundp 'redisplay) (redisplay)) ;; XEmacs does not have/need (redisplay)
1087 (and bpos (goto-char bpos))
1088 (setq r2 (if (< r1 27) (+ r1 96) r1))
1089 (unless (setq ass (assq r2 cmds))
1090 (error "No command associated with key %c" r1))
1091 (if (and bg (nth 2 ass)
1092 (not (buffer-base-buffer))
1093 (not (org-region-active-p)))
1094 ;; execute in background
1095 (let ((p (start-process
1096 (concat "Exporting " (file-name-nondirectory (buffer-file-name)))
1097 "*Org Processes*"
1098 (expand-file-name invocation-name invocation-directory)
1099 "-batch"
1100 "-l" user-init-file
1101 "--eval" "(require 'org-exp)"
1102 "--eval" "(setq org-wait .2)"
1103 (buffer-file-name)
1104 "-f" (symbol-name (nth 1 ass)))))
1105 (set-process-sentinel p 'org-export-process-sentinel)
1106 (message "Background process \"%s\": started" p))
1107 ;; set the mark correctly when exporting a subtree
1108 (if subtree-p (let (deactivate-mark) (push-mark rege t t) (goto-char regb)))
1110 (call-interactively (nth 1 ass))
1111 (when (and bpos (get-buffer-window cbuf))
1112 (let ((cw (selected-window)))
1113 (select-window (get-buffer-window cbuf))
1114 (goto-char cpos)
1115 (deactivate-mark)
1116 (select-window cw))))))
1118 (defun org-export-process-sentinel (process status)
1119 (if (string-match "\n+\\'" status)
1120 (setq status (substring status 0 -1)))
1121 (message "Background process \"%s\": %s" process status))
1123 ;;; General functions for all backends
1125 (defvar org-export-target-aliases nil
1126 "Alist of targets with invisible aliases.")
1127 (defvar org-export-preferred-target-alist nil
1128 "Alist of section id's with preferred aliases.")
1129 (defvar org-export-id-target-alist nil
1130 "Alist of section id's with preferred aliases.")
1131 (defvar org-export-code-refs nil
1132 "Alist of code references and line numbers.")
1134 (defun org-export-preprocess-string (string &rest parameters)
1135 "Cleanup STRING so that the true exported has a more consistent source.
1136 This function takes STRING, which should be a buffer-string of an org-file
1137 to export. It then creates a temporary buffer where it does its job.
1138 The result is then again returned as a string, and the exporter works
1139 on this string to produce the exported version."
1140 (interactive)
1141 (let* ((org-export-current-backend (or (plist-get parameters :for-backend)
1142 org-export-current-backend))
1143 (archived-trees (plist-get parameters :archived-trees))
1144 (inhibit-read-only t)
1145 (drawers org-drawers)
1146 (source-buffer (current-buffer))
1147 target-alist rtn)
1149 (setq org-export-target-aliases nil
1150 org-export-preferred-target-alist nil
1151 org-export-id-target-alist nil
1152 org-export-code-refs nil)
1154 (with-temp-buffer
1155 (erase-buffer)
1156 (insert string)
1157 (setq case-fold-search t)
1159 (let ((inhibit-read-only t))
1160 (remove-text-properties (point-min) (point-max)
1161 '(read-only t)))
1163 ;; Remove license-to-kill stuff
1164 ;; The caller marks some stuff for killing, stuff that has been
1165 ;; used to create the page title, for example.
1166 (org-export-kill-licensed-text)
1168 (let ((org-inhibit-startup t)) (org-mode))
1169 (setq case-fold-search t)
1170 (org-clone-local-variables source-buffer "^\\(org-\\|orgtbl-\\)")
1171 (org-install-letbind)
1173 ;; Call the hook
1174 (run-hooks 'org-export-preprocess-hook)
1176 (untabify (point-min) (point-max))
1178 ;; Handle include files, and call a hook
1179 (org-export-handle-include-files-recurse)
1180 (run-hooks 'org-export-preprocess-after-include-files-hook)
1182 ;; Get rid of archived trees
1183 (org-export-remove-archived-trees archived-trees)
1185 ;; Remove comment environment and comment subtrees
1186 (org-export-remove-comment-blocks-and-subtrees)
1188 ;; Get rid of excluded trees, and call a hook
1189 (org-export-handle-export-tags (plist-get parameters :select-tags)
1190 (plist-get parameters :exclude-tags))
1191 (run-hooks 'org-export-preprocess-after-tree-selection-hook)
1193 ;; Get rid of tasks, depending on configuration
1194 (org-export-remove-tasks (plist-get parameters :tasks))
1196 ;; Prepare footnotes for export. During that process, footnotes
1197 ;; actually included in the exported part of the buffer go
1198 ;; though some transformations:
1200 ;; 1. They have their label normalized (like "[N]");
1202 ;; 2. They get moved at the same place in the buffer (usually at
1203 ;; its end, but backends may define another place via
1204 ;; `org-footnote-insert-pos-for-preprocessor');
1206 ;; 3. The are stored in `org-export-footnotes-seen', while
1207 ;; `org-export-preprocess-string' is applied to their
1208 ;; definition.
1210 ;; Line-wise exporters ignore `org-export-footnotes-seen', as
1211 ;; they interpret footnotes at the moment they see them in the
1212 ;; buffer. Context-wise exporters grab all the info needed in
1213 ;; that variable and delete moved definitions (as described in
1214 ;; 2nd step).
1215 (when (plist-get parameters :footnotes)
1216 (org-footnote-normalize nil parameters))
1218 ;; Change lists ending. Other parts of export may insert blank
1219 ;; lines and lists' structure could be altered.
1220 (org-export-mark-list-end)
1222 ;; Process the macros
1223 (org-export-preprocess-apply-macros)
1224 (run-hooks 'org-export-preprocess-after-macros-hook)
1226 ;; Export code blocks
1227 (org-export-blocks-preprocess)
1229 ;; Mark lists with properties
1230 (org-export-mark-list-properties)
1232 ;; Handle source code snippets
1233 (org-export-replace-src-segments-and-examples)
1235 ;; Protect short examples marked by a leading colon
1236 (org-export-protect-colon-examples)
1238 ;; Protected spaces
1239 (org-export-convert-protected-spaces)
1241 ;; Find all headings and compute the targets for them
1242 (setq target-alist (org-export-define-heading-targets target-alist))
1244 (run-hooks 'org-export-preprocess-after-headline-targets-hook)
1246 ;; Find HTML special classes for headlines
1247 (org-export-remember-html-container-classes)
1249 ;; Get rid of drawers
1250 (org-export-remove-or-extract-drawers
1251 drawers (plist-get parameters :drawers))
1253 ;; Get the correct stuff before the first headline
1254 (when (plist-get parameters :skip-before-1st-heading)
1255 (goto-char (point-min))
1256 (when (re-search-forward "^\\(#.*\n\\)?\\*+[ \t]" nil t)
1257 (delete-region (point-min) (match-beginning 0))
1258 (goto-char (point-min))
1259 (insert "\n")))
1260 (when (plist-get parameters :add-text)
1261 (goto-char (point-min))
1262 (insert (plist-get parameters :add-text) "\n"))
1264 ;; Remove todo-keywords before exporting, if the user has requested so
1265 (org-export-remove-headline-metadata parameters)
1267 ;; Find targets in comments and move them out of comments,
1268 ;; but mark them as targets that should be invisible
1269 (setq target-alist (org-export-handle-invisible-targets target-alist))
1271 ;; Select and protect backend specific stuff, throw away stuff
1272 ;; that is specific for other backends
1273 (run-hooks 'org-export-preprocess-before-selecting-backend-code-hook)
1274 (org-export-select-backend-specific-text)
1276 ;; Protect quoted subtrees
1277 (org-export-protect-quoted-subtrees)
1279 ;; Remove clock lines
1280 (org-export-remove-clock-lines)
1282 ;; Protect verbatim elements
1283 (org-export-protect-verbatim)
1285 ;; Blockquotes, verse, and center
1286 (org-export-mark-blockquote-verse-center)
1287 (run-hooks 'org-export-preprocess-after-blockquote-hook)
1289 ;; Remove timestamps, if the user has requested so
1290 (unless (plist-get parameters :timestamps)
1291 (org-export-remove-timestamps))
1293 ;; Attach captions to the correct object
1294 (setq target-alist (org-export-attach-captions-and-attributes target-alist))
1296 ;; Find matches for radio targets and turn them into internal links
1297 (org-export-mark-radio-links)
1298 (run-hooks 'org-export-preprocess-after-radio-targets-hook)
1300 ;; Find all links that contain a newline and put them into a single line
1301 (org-export-concatenate-multiline-links)
1303 ;; Normalize links: Convert angle and plain links into bracket links
1304 ;; and expand link abbreviations
1305 (run-hooks 'org-export-preprocess-before-normalizing-links-hook)
1306 (org-export-normalize-links)
1308 ;; Find all internal links. If they have a fuzzy match (i.e. not
1309 ;; a *dedicated* target match, let the link point to the
1310 ;; corresponding section.
1311 (org-export-target-internal-links target-alist)
1313 ;; Find multiline emphasis and put them into single line
1314 (when (plist-get parameters :emph-multiline)
1315 (org-export-concatenate-multiline-emphasis))
1317 ;; Remove special table lines, and store alignment information
1318 (org-store-forced-table-alignment)
1319 (when org-export-table-remove-special-lines
1320 (org-export-remove-special-table-lines))
1322 ;; Another hook
1323 (run-hooks 'org-export-preprocess-before-backend-specifics-hook)
1325 ;; Backend-specific preprocessing
1326 (let* ((backend-name (symbol-name org-export-current-backend))
1327 (f (intern (format "org-export-%s-preprocess" backend-name))))
1328 (require (intern (concat "org-" backend-name)) nil)
1329 (funcall f parameters))
1331 ;; Remove or replace comments
1332 (org-export-handle-comments (plist-get parameters :comments))
1334 ;; Remove #+TBLFM #+TBLNAME #+NAME #+RESULTS lines
1335 (org-export-handle-metalines)
1337 ;; Run the final hook
1338 (run-hooks 'org-export-preprocess-final-hook)
1340 (setq rtn (buffer-string)))
1341 rtn))
1343 (defun org-export-kill-licensed-text ()
1344 "Remove all text that is marked with a :org-license-to-kill property."
1345 (let (p)
1346 (while (setq p (text-property-any (point-min) (point-max)
1347 :org-license-to-kill t))
1348 (delete-region
1349 p (or (next-single-property-change p :org-license-to-kill)
1350 (point-max))))))
1352 (defvar org-export-define-heading-targets-headline-hook nil
1353 "Hook that is run when a headline was matched during target search.
1354 This is part of the preprocessing for export.")
1356 (defun org-export-define-heading-targets (target-alist)
1357 "Find all headings and define the targets for them.
1358 The new targets are added to TARGET-ALIST, which is also returned.
1359 Also find all ID and CUSTOM_ID properties and store them."
1360 (goto-char (point-min))
1361 (org-init-section-numbers)
1362 (let ((re (concat "^" org-outline-regexp
1363 "\\|"
1364 "^[ \t]*:\\(ID\\|CUSTOM_ID\\):[ \t]*\\([^ \t\r\n]+\\)"))
1365 level target last-section-target a id)
1366 (while (re-search-forward re nil t)
1367 (org-if-unprotected-at (match-beginning 0)
1368 (if (match-end 2)
1369 (progn
1370 (setq id (org-match-string-no-properties 2))
1371 (push (cons id target) target-alist)
1372 (setq a (or (assoc last-section-target org-export-target-aliases)
1373 (progn
1374 (push (list last-section-target)
1375 org-export-target-aliases)
1376 (car org-export-target-aliases))))
1377 (push (caar target-alist) (cdr a))
1378 (when (equal (match-string 1) "CUSTOM_ID")
1379 (if (not (assoc last-section-target
1380 org-export-preferred-target-alist))
1381 (push (cons last-section-target id)
1382 org-export-preferred-target-alist)))
1383 (when (equal (match-string 1) "ID")
1384 (if (not (assoc last-section-target
1385 org-export-id-target-alist))
1386 (push (cons last-section-target (concat "ID-" id))
1387 org-export-id-target-alist))))
1388 (setq level (org-reduced-level
1389 (save-excursion (goto-char (point-at-bol))
1390 (org-outline-level))))
1391 (setq target (org-solidify-link-text
1392 (format "sec-%s" (replace-regexp-in-string
1393 "\\." "-"
1394 (org-section-number level)))))
1395 (setq last-section-target target)
1396 (push (cons target target) target-alist)
1397 (add-text-properties
1398 (point-at-bol) (point-at-eol)
1399 (list 'target target))
1400 (run-hooks 'org-export-define-heading-targets-headline-hook)))))
1401 target-alist)
1403 (defun org-export-handle-invisible-targets (target-alist)
1404 "Find targets in comments and move them out of comments.
1405 Mark them as invisible targets."
1406 (let (target tmp a)
1407 (goto-char (point-min))
1408 (while (re-search-forward "^#.*?\\(<<<?\\([^>\r\n]+\\)>>>?\\).*" nil t)
1409 ;; Check if the line before or after is a headline with a target
1410 (if (setq target (or (get-text-property (point-at-bol 0) 'target)
1411 (get-text-property (point-at-bol 2) 'target)))
1412 (progn
1413 ;; use the existing target in a neighboring line
1414 (setq tmp (match-string 2))
1415 (replace-match "")
1416 (and (looking-at "\n") (delete-char 1))
1417 (push (cons (setq tmp (org-solidify-link-text tmp)) target)
1418 target-alist)
1419 (setq a (or (assoc target org-export-target-aliases)
1420 (progn
1421 (push (list target) org-export-target-aliases)
1422 (car org-export-target-aliases))))
1423 (push tmp (cdr a)))
1424 ;; Make an invisible target
1425 (replace-match "\\1(INVISIBLE)"))))
1426 target-alist)
1428 (defun org-export-target-internal-links (target-alist)
1429 "Find all internal links and assign targets to them.
1430 If a link has a fuzzy match (i.e. not a *dedicated* target match),
1431 let the link point to the corresponding section.
1432 This function also handles the id links, if they have a match in
1433 the current file."
1434 (goto-char (point-min))
1435 (while (re-search-forward org-bracket-link-regexp nil t)
1436 (org-if-unprotected-at (1+ (match-beginning 0))
1437 (let* ((org-link-search-must-match-exact-headline t)
1438 (md (match-data))
1439 (desc (match-end 2))
1440 (link (org-link-unescape (match-string 1)))
1441 (slink (org-solidify-link-text link))
1442 found props pos cref
1443 (target
1444 (cond
1445 ((= (string-to-char link) ?#)
1446 ;; user wants exactly this link
1447 link)
1448 ((cdr (assoc slink target-alist))
1449 (or (cdr (assoc (assoc slink target-alist)
1450 org-export-preferred-target-alist))
1451 (cdr (assoc slink target-alist))))
1452 ((and (string-match "^id:" link)
1453 (cdr (assoc (substring link 3) target-alist))))
1454 ((string-match "^(\\(.*\\))$" link)
1455 (setq cref (match-string 1 link))
1456 (concat "coderef:" cref))
1457 ((string-match org-link-types-re link) nil)
1458 ((or (file-name-absolute-p link)
1459 (string-match "^\\." link))
1460 nil)
1462 (let ((org-link-search-inhibit-query t))
1463 (save-excursion
1464 (setq found (condition-case nil (org-link-search link)
1465 (error nil)))
1466 (when (and found
1467 (or (org-at-heading-p)
1468 (not (eq found 'dedicated))))
1469 (or (get-text-property (point) 'target)
1470 (get-text-property
1471 (max (point-min)
1472 (1- (or (previous-single-property-change
1473 (point) 'target) 0)))
1474 'target)))))))))
1475 (when target
1476 (set-match-data md)
1477 (goto-char (match-beginning 1))
1478 (setq props (text-properties-at (point)))
1479 (delete-region (match-beginning 1) (match-end 1))
1480 (setq pos (point))
1481 (insert target)
1482 (unless desc (insert "][" link))
1483 (add-text-properties pos (point) props))))))
1485 (defun org-export-remember-html-container-classes ()
1486 "Store the HTML_CONTAINER_CLASS properties in a text property."
1487 (goto-char (point-min))
1488 (let (class)
1489 (while (re-search-forward
1490 "^[ \t]*:HTML_CONTAINER_CLASS:[ \t]+\\(.+\\)$" nil t)
1491 (setq class (match-string 1))
1492 (save-excursion
1493 (when (re-search-backward "^\\*" (point-min) t)
1494 (org-back-to-heading t)
1495 (put-text-property (point-at-bol) (point-at-eol)
1496 'html-container-class class))))))
1498 (defvar org-export-format-drawer-function nil
1499 "Function to be called to format the contents of a drawer.
1500 The function must accept two parameters:
1501 NAME the drawer name, like \"PROPERTIES\"
1502 CONTENT the content of the drawer.
1503 You can check the export backend through `org-export-current-backend'.
1504 The function should return the text to be inserted into the buffer.
1505 If this is nil, `org-export-format-drawer' is used as a default.")
1507 (defun org-export-remove-or-extract-drawers (all-drawers exp-drawers)
1508 "Remove drawers, or extract and format the content.
1509 ALL-DRAWERS is a list of all drawer names valid in the current buffer.
1510 EXP-DRAWERS can be t to keep all drawer contents, or a list of drawers
1511 whose content to keep. Any drawers that are in ALL-DRAWERS but not in
1512 EXP-DRAWERS will be removed."
1513 (goto-char (point-min))
1514 (let ((re (concat "^[ \t]*:\\("
1515 (mapconcat 'identity all-drawers "\\|")
1516 "\\):[ \t]*$"))
1517 name beg beg-content eol content)
1518 (while (re-search-forward re nil t)
1519 (org-if-unprotected
1520 (setq name (match-string 1))
1521 (setq beg (match-beginning 0)
1522 beg-content (1+ (point-at-eol))
1523 eol (point-at-eol))
1524 (if (not (and (re-search-forward
1525 "^\\([ \t]*:END:[ \t]*\n?\\)\\|^\\*+[ \t]" nil t)
1526 (match-end 1)))
1527 (goto-char eol)
1528 (goto-char (match-beginning 0))
1529 (and (looking-at ".*\n?") (replace-match ""))
1530 (setq content (buffer-substring beg-content (point)))
1531 (delete-region beg (point))
1532 (when (or (eq exp-drawers t)
1533 (member name exp-drawers))
1534 (setq content (funcall (or org-export-format-drawer-function
1535 'org-export-format-drawer)
1536 name content))
1537 (insert content)))))))
1539 (defun org-export-format-drawer (name content)
1540 "Format the content of a drawer as a colon example."
1541 (if (string-match "[ \t]+\\'" content)
1542 (setq content (substring content (match-beginning 0))))
1543 (while (string-match "\\`[ \t]*\n" content)
1544 (setq content (substring content (match-end 0))))
1545 (setq content (org-remove-indentation content))
1546 (setq content (concat ": " (mapconcat 'identity
1547 (org-split-string content "\n")
1548 "\n: ")
1549 "\n"))
1550 (setq content (concat " : " (upcase name) "\n" content))
1551 (org-add-props content nil 'org-protected t))
1553 (defun org-export-handle-export-tags (select-tags exclude-tags)
1554 "Modify the buffer, honoring SELECT-TAGS and EXCLUDE-TAGS.
1555 Both arguments are lists of tags.
1556 If any of SELECT-TAGS is found, all trees not marked by a SELECT-TAG
1557 will be removed.
1558 After that, all subtrees that are marked by EXCLUDE-TAGS will be
1559 removed as well."
1560 (remove-text-properties (point-min) (point-max) '(:org-delete t))
1561 (let* ((re-sel (concat ":\\(" (mapconcat 'regexp-quote
1562 select-tags "\\|")
1563 "\\):"))
1564 (re-excl (concat ":\\(" (mapconcat 'regexp-quote
1565 exclude-tags "\\|")
1566 "\\):"))
1567 beg end cont)
1568 (goto-char (point-min))
1569 (when (and select-tags
1570 (re-search-forward
1571 (concat "^\\*+[ \t].*" re-sel "[^ \t\n]*[ \t]*$") nil t))
1572 ;; At least one tree is marked for export, this means
1573 ;; all the unmarked stuff needs to go.
1574 ;; Dig out the trees that should be exported
1575 (goto-char (point-min))
1576 (outline-next-heading)
1577 (setq beg (point))
1578 (put-text-property beg (point-max) :org-delete t)
1579 (while (re-search-forward re-sel nil t)
1580 (when (org-at-heading-p)
1581 (org-back-to-heading)
1582 (remove-text-properties
1583 (max (1- (point)) (point-min))
1584 (setq cont (save-excursion (org-end-of-subtree t t)))
1585 '(:org-delete t))
1586 (while (and (org-up-heading-safe)
1587 (get-text-property (point) :org-delete))
1588 (remove-text-properties (max (1- (point)) (point-min))
1589 (point-at-eol) '(:org-delete t)))
1590 (goto-char cont))))
1591 ;; Remove the trees explicitly marked for noexport
1592 (when exclude-tags
1593 (goto-char (point-min))
1594 (while (re-search-forward re-excl nil t)
1595 (when (org-at-heading-p)
1596 (org-back-to-heading t)
1597 (setq beg (point))
1598 (org-end-of-subtree t t)
1599 (delete-region beg (point))
1600 (when (featurep 'org-inlinetask)
1601 (org-inlinetask-remove-END-maybe)))))
1602 ;; Remove everything that is now still marked for deletion
1603 (goto-char (point-min))
1604 (while (setq beg (text-property-any (point-min) (point-max) :org-delete t))
1605 (setq end (or (next-single-property-change beg :org-delete)
1606 (point-max)))
1607 (delete-region beg end))))
1609 (defun org-export-remove-tasks (keep)
1610 "Remove tasks depending on configuration.
1611 When KEEP is nil, remove all tasks.
1612 When KEEP is `todo', remove the tasks that are DONE.
1613 When KEEP is `done', remove the tasks that are not yet done.
1614 When it is a list of strings, keep only tasks with these TODO keywords."
1615 (when (or (listp keep) (memq keep '(todo done nil)))
1616 (let ((re (concat "^\\*+[ \t]+\\("
1617 (mapconcat
1618 'regexp-quote
1619 (cond ((not keep) org-todo-keywords-1)
1620 ((eq keep 'todo) org-done-keywords)
1621 ((eq keep 'done) org-not-done-keywords)
1622 ((listp keep)
1623 (org-delete-all keep (copy-sequence
1624 org-todo-keywords-1))))
1625 "\\|")
1626 "\\)\\($\\|[ \t]\\)"))
1627 (case-fold-search nil)
1628 beg)
1629 (goto-char (point-min))
1630 (while (re-search-forward re nil t)
1631 (org-if-unprotected
1632 (setq beg (match-beginning 0))
1633 (org-end-of-subtree t t)
1634 (if (looking-at "^\\*+[ \t]+END[ \t]*$")
1635 ;; Kill the END line of the inline task
1636 (goto-char (min (point-max) (1+ (match-end 0)))))
1637 (delete-region beg (point)))))))
1639 (defun org-export-remove-archived-trees (export-archived-trees)
1640 "Remove archived trees.
1641 When EXPORT-ARCHIVED-TREES is `headline;, only the headline will be exported.
1642 When it is t, the entire archived tree will be exported.
1643 When it is nil the entire tree including the headline will be removed
1644 from the buffer."
1645 (let ((re-archive (concat ":" org-archive-tag ":"))
1646 a b)
1647 (when (not (eq export-archived-trees t))
1648 (goto-char (point-min))
1649 (while (re-search-forward re-archive nil t)
1650 (if (not (org-at-heading-p t))
1651 (goto-char (point-at-eol))
1652 (beginning-of-line 1)
1653 (setq a (if export-archived-trees
1654 (1+ (point-at-eol)) (point))
1655 b (org-end-of-subtree t))
1656 (if (> b a) (delete-region a b)))))))
1658 (defun org-export-remove-headline-metadata (opts)
1659 "Remove meta data from the headline, according to user options."
1660 (let ((re org-complex-heading-regexp)
1661 (todo (plist-get opts :todo-keywords))
1662 (tags (plist-get opts :tags))
1663 (pri (plist-get opts :priority))
1664 (elts '(1 2 3 4 5))
1665 (case-fold-search nil)
1666 rpl)
1667 (setq elts (delq nil (list 1 (if todo 2) (if pri 3) 4 (if tags 5))))
1668 (when (or (not todo) (not tags) (not pri))
1669 (goto-char (point-min))
1670 (while (re-search-forward re nil t)
1671 (org-if-unprotected
1672 (setq rpl (mapconcat (lambda (i) (if (match-end i) (match-string i) ""))
1673 elts " "))
1674 (replace-match rpl t t))))))
1676 (defun org-export-remove-timestamps ()
1677 "Remove timestamps and keywords for export."
1678 (goto-char (point-min))
1679 (while (re-search-forward org-maybe-keyword-time-regexp nil t)
1680 (backward-char 1)
1681 (org-if-unprotected
1682 (unless (save-match-data (org-at-table-p))
1683 (replace-match "")
1684 (beginning-of-line 1)
1685 (if (looking-at "[- \t]*\\(=>[- \t0-9:]*\\)?[ \t]*\n")
1686 (replace-match ""))))))
1688 (defun org-export-remove-clock-lines ()
1689 "Remove clock lines for export."
1690 (goto-char (point-min))
1691 (let ((re (concat "^[ \t]*" org-clock-string ".*\n?")))
1692 (while (re-search-forward re nil t)
1693 (org-if-unprotected
1694 (replace-match "")))))
1696 (defvar org-heading-keyword-regexp-format) ; defined in org.el
1697 (defun org-export-protect-quoted-subtrees ()
1698 "Mark quoted subtrees with the protection property."
1699 (let ((org-re-quote (format org-heading-keyword-regexp-format
1700 org-quote-string)))
1701 (goto-char (point-min))
1702 (while (re-search-forward org-re-quote nil t)
1703 (goto-char (match-beginning 0))
1704 (end-of-line 1)
1705 (add-text-properties (point) (org-end-of-subtree t)
1706 '(org-protected t)))))
1708 (defun org-export-convert-protected-spaces ()
1709 "Convert strings like \\____ to protected spaces in all backends."
1710 (goto-char (point-min))
1711 (while (re-search-forward "\\\\__+" nil t)
1712 (org-if-unprotected-1
1713 (replace-match
1714 (org-add-props
1715 (cond
1716 ((eq org-export-current-backend 'latex)
1717 (format "\\hspace{%dex}" (- (match-end 0) (match-beginning 0))))
1718 ((eq org-export-current-backend 'html)
1719 (org-add-props (match-string 0) nil
1720 'org-whitespace (- (match-end 0) (match-beginning 0))))
1721 ;; ((eq org-export-current-backend 'docbook))
1722 ((eq org-export-current-backend 'ascii)
1723 (org-add-props (match-string 0) '(org-whitespace t)))
1724 (t (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
1725 '(org-protected t))
1726 t t))))
1728 (defun org-export-protect-verbatim ()
1729 "Mark verbatim snippets with the protection property."
1730 (goto-char (point-min))
1731 (while (re-search-forward org-verbatim-re nil t)
1732 (org-if-unprotected
1733 (add-text-properties (match-beginning 4) (match-end 4)
1734 '(org-protected t org-verbatim-emph t))
1735 (goto-char (1+ (match-end 4))))))
1737 (defun org-export-protect-colon-examples ()
1738 "Protect lines starting with a colon."
1739 (goto-char (point-min))
1740 (let ((re "^[ \t]*:\\([ \t]\\|$\\)") beg)
1741 (while (re-search-forward re nil t)
1742 (beginning-of-line 1)
1743 (setq beg (point))
1744 (while (looking-at re)
1745 (end-of-line 1)
1746 (or (eobp) (forward-char 1)))
1747 (add-text-properties beg (if (bolp) (1- (point)) (point))
1748 '(org-protected t)))))
1750 (defvar org-export-backends
1751 '(docbook html beamer ascii latex)
1752 "List of Org supported export backends.")
1754 (defun org-export-select-backend-specific-text ()
1755 (let ((formatters org-export-backends)
1756 (case-fold-search t)
1757 backend backend-name beg beg-content end end-content ind)
1759 (while formatters
1760 (setq backend (pop formatters)
1761 backend-name (symbol-name backend))
1763 ;; Handle #+BACKEND: stuff
1764 (goto-char (point-min))
1765 (while (re-search-forward (concat "^\\([ \t]*\\)#\\+" backend-name
1766 ":[ \t]*\\(.*\\)") nil t)
1767 (if (not (eq backend org-export-current-backend))
1768 (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
1769 (let ((ind (get-text-property (point-at-bol) 'original-indentation)))
1770 (replace-match "\\1\\2" t)
1771 (add-text-properties
1772 (point-at-bol) (min (1+ (point-at-eol)) (point-max))
1773 `(org-protected t original-indentation ,ind org-native-text t)))))
1774 ;; Delete #+ATTR_BACKEND: stuff of another backend. Those
1775 ;; matching the current backend will be taken care of by
1776 ;; `org-export-attach-captions-and-attributes'
1777 (goto-char (point-min))
1778 (while (re-search-forward (concat "^\\([ \t]*\\)#\\+ATTR_" backend-name
1779 ":[ \t]*\\(.*\\)") nil t)
1780 (setq ind (org-get-indentation))
1781 (when (not (eq backend org-export-current-backend))
1782 (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))))
1783 ;; Handle #+BEGIN_BACKEND and #+END_BACKEND stuff
1784 (goto-char (point-min))
1785 (while (re-search-forward (concat "^[ \t]*#\\+BEGIN_" backend-name "\\>.*\n?")
1786 nil t)
1787 (setq beg (match-beginning 0) beg-content (match-end 0))
1788 (setq ind (or (get-text-property beg 'original-indentation)
1789 (save-excursion (goto-char beg) (org-get-indentation))))
1790 (when (re-search-forward (concat "^[ \t]*#\\+END_" backend-name "\\>.*\n?")
1791 nil t)
1792 (setq end (match-end 0) end-content (match-beginning 0))
1793 (if (eq backend org-export-current-backend)
1794 ;; yes, keep this
1795 (progn
1796 (add-text-properties
1797 beg-content end-content
1798 `(org-protected t original-indentation ,ind org-native-text t))
1799 ;; strip protective commas
1800 (org-unescape-code-in-region beg-content end-content)
1801 (delete-region (match-beginning 0) (match-end 0))
1802 (save-excursion
1803 (goto-char beg)
1804 (delete-region (point) (1+ (point-at-eol)))))
1805 ;; No, this is for a different backend, kill it
1806 (delete-region beg end)))))))
1808 (defun org-export-mark-blockquote-verse-center ()
1809 "Mark block quote and verse environments with special cookies.
1810 These special cookies will later be interpreted by the backend."
1811 ;; Blockquotes
1812 (let (type t1 ind beg end beg1 end1 content)
1813 (goto-char (point-min))
1814 (while (re-search-forward
1815 "^\\([ \t]*\\)#\\+\\(begin_\\(\\(block\\)?quote\\|verse\\|center\\)\\>.*\\)"
1816 nil t)
1817 (setq ind (length (match-string 1))
1818 type (downcase (match-string 3))
1819 t1 (if (equal type "quote") "blockquote" type))
1820 (setq beg (match-beginning 0)
1821 beg1 (1+ (match-end 0)))
1822 (when (re-search-forward (concat "^[ \t]*#\\+end_" type "\\>.*") nil t)
1823 (setq end1 (1- (match-beginning 0))
1824 end (+ (point-at-eol) (if (looking-at "\n$") 1 0)))
1825 (setq content (org-remove-indentation (buffer-substring beg1 end1)))
1826 (setq content (concat "ORG-" (upcase t1) "-START\n"
1827 content "\n"
1828 "ORG-" (upcase t1) "-END\n"))
1829 (delete-region beg end)
1830 (insert (org-add-props content nil 'original-indentation ind))))))
1832 (defun org-export-mark-list-end ()
1833 "Mark all list endings with a special string."
1834 (unless (eq org-export-current-backend 'ascii)
1835 (mapc
1836 (lambda (e)
1837 ;; For each type allowing list export, find every list, remove
1838 ;; ending regexp if needed, and insert org-list-end.
1839 (goto-char (point-min))
1840 (while (re-search-forward (org-item-beginning-re) nil t)
1841 (when (eq (nth 2 (org-list-context)) e)
1842 (let* ((struct (org-list-struct))
1843 (bottom (org-list-get-bottom-point struct))
1844 (top (point-at-bol))
1845 (top-ind (org-list-get-ind top struct)))
1846 (goto-char bottom)
1847 (when (and (not (looking-at "[ \t]*$"))
1848 (looking-at org-list-end-re))
1849 (replace-match ""))
1850 (unless (bolp) (insert "\n"))
1851 ;; As org-list-end is inserted at column 0, it would end
1852 ;; by indentation any list. It can be problematic when
1853 ;; there are lists within lists: the inner list end would
1854 ;; also become the outer list end. To avoid this, text
1855 ;; property `original-indentation' is added, as
1856 ;; `org-list-struct' pays attention to it when reading a
1857 ;; list.
1858 (insert (org-add-props
1859 "ORG-LIST-END-MARKER\n"
1860 (list 'original-indentation top-ind)))))))
1861 (cons nil org-list-export-context))))
1863 (defun org-export-mark-list-properties ()
1864 "Mark list with special properties.
1865 These special properties will later be interpreted by the backend."
1866 (let ((mark-list
1867 (function
1868 ;; Mark a list with 3 properties: `list-item' which is
1869 ;; position at beginning of line, `list-struct' which is
1870 ;; list structure, and `list-prevs' which is the alist of
1871 ;; item and its predecessor. Leave point at list ending.
1872 (lambda (ctxt)
1873 (let* ((struct (org-list-struct))
1874 (top (org-list-get-top-point struct))
1875 (bottom (org-list-get-bottom-point struct))
1876 (prevs (org-list-prevs-alist struct))
1877 poi)
1878 ;; Get every item and ending position, without dups and
1879 ;; without bottom point of list.
1880 (mapc (lambda (e)
1881 (let ((pos (car e))
1882 (end (nth 6 e)))
1883 (unless (memq pos poi)
1884 (push pos poi))
1885 (unless (or (= end bottom) (memq end poi))
1886 (push end poi))))
1887 struct)
1888 (setq poi (sort poi '<))
1889 ;; For every point of interest, mark the whole line with
1890 ;; its position in list.
1891 (mapc
1892 (lambda (e)
1893 (goto-char e)
1894 (add-text-properties (point-at-bol) (point-at-eol)
1895 (list 'list-item (point-at-bol)
1896 'list-struct struct
1897 'list-prevs prevs)))
1898 poi)
1899 ;; Take care of bottom point. As babel may have inserted
1900 ;; a new list in buffer, list ending isn't always
1901 ;; marked. Now mark every list ending and add properties
1902 ;; useful to line processing exporters.
1903 (goto-char bottom)
1904 (when (or (looking-at "^ORG-LIST-END-MARKER\n")
1905 (and (not (looking-at "[ \t]*$"))
1906 (looking-at org-list-end-re)))
1907 (replace-match ""))
1908 (unless (bolp) (insert "\n"))
1909 (insert
1910 (org-add-props "ORG-LIST-END-MARKER\n" (list 'list-item bottom
1911 'list-struct struct
1912 'list-prevs prevs)))
1913 ;; Following property is used by LaTeX exporter.
1914 (add-text-properties top (point) (list 'list-context ctxt)))))))
1915 ;; Mark lists except for backends not interpreting them.
1916 (unless (eq org-export-current-backend 'ascii)
1917 (let ((org-list-end-re "^ORG-LIST-END-MARKER\n"))
1918 (mapc
1919 (lambda (e)
1920 (goto-char (point-min))
1921 (while (re-search-forward (org-item-beginning-re) nil t)
1922 (let ((context (nth 2 (org-list-context))))
1923 (if (eq context e)
1924 (funcall mark-list e)
1925 (put-text-property (point-at-bol) (point-at-eol)
1926 'list-context context)))))
1927 (cons nil org-list-export-context))))))
1929 (defun org-export-attach-captions-and-attributes (target-alist)
1930 "Move #+CAPTION, #+ATTR_BACKEND, and #+LABEL text into text properties.
1931 If the next thing following is a table, add the text properties to the first
1932 table line. If it is a link, add it to the line containing the link."
1933 (goto-char (point-min))
1934 (remove-text-properties (point-min) (point-max)
1935 '(org-caption nil org-attributes nil))
1936 (let ((case-fold-search t)
1937 (re (concat "^[ \t]*#\\+caption:[ \t]+\\(.*\\)"
1938 "\\|"
1939 "^[ \t]*#\\+attr_" (symbol-name org-export-current-backend) ":[ \t]+\\(.*\\)"
1940 "\\|"
1941 "^[ \t]*#\\+label:[ \t]+\\(.*\\)"
1942 "\\|"
1943 "^[ \t]*\\(|[^-]\\)"
1944 "\\|"
1945 "^[ \t]*\\[\\[.*\\]\\][ \t]*$"))
1946 cap shortn attr label end)
1947 (while (re-search-forward re nil t)
1948 (cond
1949 ;; there is a caption
1950 ((match-end 1)
1951 (progn
1952 (setq cap (concat cap (if cap " " "") (org-trim (match-string 1))))
1953 (when (string-match "\\[\\(.*\\)\\]{\\(.*\\)}" cap)
1954 (setq shortn (match-string 1 cap)
1955 cap (match-string 2 cap)))
1956 (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))))
1957 ;; there is an attribute
1958 ((match-end 2)
1959 (progn
1960 (setq attr (concat attr (if attr " " "") (org-trim (match-string 2))))
1961 (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))))
1962 ;; there is a label
1963 ((match-end 3)
1964 (progn
1965 (setq label (org-trim (match-string 3)))
1966 (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))))
1968 (setq end (if (match-end 4)
1969 (let ((ee (org-table-end)))
1970 (prog1 (1- (marker-position ee)) (move-marker ee nil)))
1971 (point-at-eol)))
1972 (add-text-properties (point-at-bol) end
1973 (list 'org-caption cap
1974 'org-caption-shortn shortn
1975 'org-attributes attr
1976 'org-label label))
1977 (if label (push (cons label label) target-alist))
1978 (goto-char end)
1979 (setq cap nil shortn nil attr nil label nil)))))
1980 target-alist)
1982 (defun org-export-remove-comment-blocks-and-subtrees ()
1983 "Remove the comment environment, and also commented subtrees."
1984 (let ((re-commented (format org-heading-keyword-regexp-format
1985 org-comment-string))
1986 case-fold-search)
1987 ;; Remove comment environment
1988 (goto-char (point-min))
1989 (setq case-fold-search t)
1990 (while (re-search-forward
1991 "^#\\+begin_comment[ \t]*\n[^\000]*?\n#\\+end_comment\\>.*" nil t)
1992 (replace-match "" t t))
1993 ;; Remove subtrees that are commented
1994 (goto-char (point-min))
1995 (setq case-fold-search nil)
1996 (while (re-search-forward re-commented nil t)
1997 (goto-char (match-beginning 0))
1998 (delete-region (point) (org-end-of-subtree t)))))
2000 (defun org-export-handle-comments (org-commentsp)
2001 "Remove comments, or convert to backend-specific format.
2002 ORG-COMMENTSP can be a format string for publishing comments.
2003 When it is nil, all comments will be removed."
2004 (let ((re "^[ \t]*#\\( \\|$\\)"))
2005 (goto-char (point-min))
2006 (while (re-search-forward re nil t)
2007 (let ((pos (match-beginning 0))
2008 (end (progn (forward-line) (point))))
2009 (if (get-text-property pos 'org-protected)
2010 (forward-line)
2011 (if (not org-commentsp) (delete-region pos end)
2012 (add-text-properties pos end '(org-protected t))
2013 (replace-match
2014 (org-add-props
2015 (format org-commentsp (buffer-substring (match-end 0) end))
2016 nil 'org-protected t)
2017 t t)))))
2018 ;; Hack attack: previous implementation also removed keywords at
2019 ;; column 0. Brainlessly do it again.
2020 (goto-char (point-min))
2021 (while (re-search-forward "^#\\+" nil t)
2022 (unless (get-text-property (point-at-bol) 'org-protected)
2023 (delete-region (point-at-bol) (progn (forward-line) (point)))))))
2025 (defun org-export-handle-metalines ()
2026 "Remove tables and source blocks metalines.
2027 This function should only be called after all block processing
2028 has taken place."
2029 (let ((re "^[ \t]*#\\+\\(tbl\\(?:name\\|fm\\)\\|results\\(?:\\[[a-z0-9]+\\]\\)?\\|name\\):\\(.*\n?\\)")
2030 (case-fold-search t)
2031 pos)
2032 (goto-char (point-min))
2033 (while (or (looking-at re)
2034 (re-search-forward re nil t))
2035 (setq pos (match-beginning 0))
2036 (if (get-text-property (match-beginning 1) 'org-protected)
2037 (goto-char (1+ pos))
2038 (goto-char (1+ pos))
2039 (replace-match "")
2040 (goto-char (max (point-min) (1- pos)))))))
2042 (defun org-export-mark-radio-links ()
2043 "Find all matches for radio targets and turn them into internal links."
2044 (let ((re-radio (and org-target-link-regexp
2045 (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)"))))
2046 (goto-char (point-min))
2047 (when re-radio
2048 (while (re-search-forward re-radio nil t)
2049 (unless
2050 (save-match-data
2051 (or (org-in-regexp org-bracket-link-regexp)
2052 (org-in-regexp org-plain-link-re)
2053 (org-in-regexp "<<[^<>]+>>")))
2054 (org-if-unprotected
2055 (replace-match "\\1[[\\2]]")))))))
2057 (defun org-store-forced-table-alignment ()
2058 "Find table lines which force alignment, store the results in properties."
2059 (let (line cnt cookies)
2060 (goto-char (point-min))
2061 (while (re-search-forward "|[ \t]*<\\([lrc]?[0-9]+\\|[lrc]\\)>[ \t]*|"
2062 nil t)
2063 ;; OK, this looks like a table line with an alignment cookie
2064 (org-if-unprotected
2065 (setq line (buffer-substring (point-at-bol) (point-at-eol)))
2066 (when (and (org-at-table-p)
2067 (org-table-cookie-line-p line))
2068 (setq cnt 0 cookies nil)
2069 (mapc
2070 (lambda (x)
2071 (setq cnt (1+ cnt))
2072 (when (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'" x)
2073 (let ((align (and (match-end 1)
2074 (downcase (match-string 1 x))))
2075 (width (and (match-end 2)
2076 (string-to-number (match-string 2 x)))))
2077 (push (cons cnt (list align width)) cookies))))
2078 (org-split-string line "[ \t]*|[ \t]*"))
2079 (add-text-properties (org-table-begin) (org-table-end)
2080 (list 'org-col-cookies cookies))))
2081 (goto-char (point-at-eol)))))
2083 (defun org-export-remove-special-table-lines ()
2084 "Remove tables lines that are used for internal purposes.
2085 Also, store forced alignment information found in such lines."
2086 (goto-char (point-min))
2087 (while (re-search-forward "^[ \t]*|" nil t)
2088 (org-if-unprotected-at (1- (point))
2089 (beginning-of-line 1)
2090 (if (or (looking-at "[ \t]*| *[!_^] *|")
2091 (not
2092 (memq
2094 (mapcar
2095 (lambda (f)
2096 (or (and org-export-table-remove-empty-lines (= (length f) 0))
2097 (string-match
2098 "\\`<\\([0-9]\\|[lrc]\\|[lrc][0-9]+\\)>\\'" f)))
2099 (org-split-string ;; FIXME, can't we do without splitting???
2100 (buffer-substring (point-at-bol) (point-at-eol))
2101 "[ \t]*|[ \t]*")))))
2102 (delete-region (max (point-min) (1- (point-at-bol)))
2103 (point-at-eol))
2104 (end-of-line 1)))))
2106 (defun org-export-protect-sub-super (s)
2107 (save-match-data
2108 (while (string-match "\\([^\\\\]\\)\\([_^]\\)" s)
2109 (setq s (replace-match "\\1\\\\\\2" nil nil s)))
2112 (defun org-export-normalize-links ()
2113 "Convert all links to bracket links, and expand link abbreviations."
2114 (let ((re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
2115 (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
2116 nodesc)
2117 (goto-char (point-min))
2118 (while (re-search-forward org-bracket-link-regexp nil t)
2119 (put-text-property (match-beginning 0) (match-end 0) 'org-normalized-link t))
2120 (goto-char (point-min))
2121 (while (re-search-forward re-plain-link nil t)
2122 (unless (get-text-property (match-beginning 0) 'org-normalized-link)
2123 (goto-char (1- (match-end 0)))
2124 (org-if-unprotected-at (1+ (match-beginning 0))
2125 (let* ((s (concat (match-string 1)
2126 "[[" (match-string 2) ":" (match-string 3)
2127 "][" (match-string 2) ":" (org-export-protect-sub-super
2128 (match-string 3))
2129 "]]")))
2130 ;; added 'org-link face to links
2131 (put-text-property 0 (length s) 'face 'org-link s)
2132 (replace-match s t t)))))
2133 (goto-char (point-min))
2134 (while (re-search-forward re-angle-link nil t)
2135 (goto-char (1- (match-end 0)))
2136 (org-if-unprotected
2137 (let* ((s (concat (match-string 1)
2138 "[[" (match-string 2) ":" (match-string 3)
2139 "][" (match-string 2) ":" (org-export-protect-sub-super
2140 (match-string 3))
2141 "]]")))
2142 (put-text-property 0 (length s) 'face 'org-link s)
2143 (replace-match s t t))))
2144 (goto-char (point-min))
2145 (while (re-search-forward org-bracket-link-regexp nil t)
2146 (goto-char (1- (match-end 0)))
2147 (setq nodesc (not (match-end 3)))
2148 (org-if-unprotected
2149 (let* ((xx (save-match-data
2150 (org-translate-link
2151 (org-link-expand-abbrev (match-string 1)))))
2152 (s (concat
2153 "[[" (org-add-props (copy-sequence xx)
2154 nil 'org-protected t 'org-no-description nodesc)
2156 (if (match-end 3)
2157 (match-string 2)
2158 (concat "[" (copy-sequence xx)
2159 "]"))
2160 "]")))
2161 (put-text-property 0 (length s) 'face 'org-link s)
2162 (replace-match s t t))))))
2164 (defun org-export-concatenate-multiline-links ()
2165 "Find multi-line links and put it all into a single line.
2166 This is to make sure that the line-processing export backends
2167 can work correctly."
2168 (goto-char (point-min))
2169 (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
2170 (org-if-unprotected-at (match-beginning 1)
2171 (replace-match "\\1 \\3")
2172 (goto-char (match-beginning 0)))))
2174 (defun org-export-concatenate-multiline-emphasis ()
2175 "Find multi-line emphasis and put it all into a single line.
2176 This is to make sure that the line-processing export backends
2177 can work correctly."
2178 (goto-char (point-min))
2179 (while (re-search-forward org-emph-re nil t)
2180 (if (and (not (= (char-after (match-beginning 3))
2181 (char-after (match-beginning 4))))
2182 (save-excursion (goto-char (match-beginning 0))
2183 (save-match-data
2184 (and (not (org-at-table-p))
2185 (not (org-at-heading-p))))))
2186 (org-if-unprotected
2187 (subst-char-in-region (match-beginning 0) (match-end 0)
2188 ?\n ?\ t)
2189 (goto-char (1- (match-end 0))))
2190 (goto-char (1+ (match-beginning 0))))))
2192 (defun org-export-grab-title-from-buffer ()
2193 "Get a title for the current document, from looking at the buffer."
2194 (let ((inhibit-read-only t))
2195 (save-excursion
2196 (goto-char (point-min))
2197 (let ((end (if (looking-at org-outline-regexp)
2198 (point)
2199 (save-excursion (outline-next-heading) (point)))))
2200 (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
2201 ;; Mark the line so that it will not be exported as normal text.
2202 (unless (org-in-block-p org-list-forbidden-blocks)
2203 (org-unmodified
2204 (add-text-properties (match-beginning 0) (match-end 0)
2205 (list :org-license-to-kill t))))
2206 ;; Return the title string
2207 (org-trim (match-string 0)))))))
2209 (defun org-export-get-title-from-subtree ()
2210 "Return subtree title and exclude it from export."
2211 (let ((rbeg (region-beginning)) (rend (region-end))
2212 (inhibit-read-only t)
2213 (tags (plist-get (org-infile-export-plist) :tags))
2214 title)
2215 (save-excursion
2216 (goto-char rbeg)
2217 (when (and (org-at-heading-p)
2218 (>= (org-end-of-subtree t t) rend))
2219 (when (plist-member org-export-opt-plist :tags)
2220 (setq tags (or (plist-get org-export-opt-plist :tags) tags)))
2221 ;; This is a subtree, we take the title from the first heading
2222 (goto-char rbeg)
2223 (looking-at org-todo-line-tags-regexp)
2224 (setq title (if (and (eq tags t) (match-string 4))
2225 (format "%s\t%s" (match-string 3) (match-string 4))
2226 (match-string 3)))
2227 (org-unmodified
2228 (add-text-properties (point) (1+ (point-at-eol))
2229 (list :org-license-to-kill t)))
2230 (setq title (or (org-entry-get nil "EXPORT_TITLE") title))))
2231 title))
2233 (defun org-solidify-link-text (s &optional alist)
2234 "Take link text and make a safe target out of it."
2235 (save-match-data
2236 (let* ((rtn
2237 (mapconcat
2238 'identity
2239 (org-split-string s "[^a-zA-Z0-9_\\.-]+") "-"))
2240 (a (assoc rtn alist)))
2241 (or (cdr a) rtn))))
2243 (defun org-get-min-level (lines &optional offset)
2244 "Get the minimum level in LINES."
2245 (let ((re "^\\(\\*+\\) ") l)
2246 (catch 'exit
2247 (while (setq l (pop lines))
2248 (if (string-match re l)
2249 (throw 'exit (org-tr-level (- (length (match-string 1 l))
2250 (or offset 0))))))
2251 1)))
2253 ;; Variable holding the vector with section numbers
2254 (defvar org-section-numbers (make-vector org-level-max 0))
2256 (defun org-init-section-numbers ()
2257 "Initialize the vector for the section numbers."
2258 (let* ((level -1)
2259 (numbers (nreverse (org-split-string "" "\\.")))
2260 (depth (1- (length org-section-numbers)))
2261 (i depth) number-string)
2262 (while (>= i 0)
2263 (if (> i level)
2264 (aset org-section-numbers i 0)
2265 (setq number-string (or (car numbers) "0"))
2266 (if (string-match "\\`[A-Z]\\'" number-string)
2267 (aset org-section-numbers i
2268 (- (string-to-char number-string) ?A -1))
2269 (aset org-section-numbers i (string-to-number number-string)))
2270 (pop numbers))
2271 (setq i (1- i)))))
2273 (defun org-section-number (&optional level)
2274 "Return a string with the current section number.
2275 When LEVEL is non-nil, increase section numbers on that level."
2276 (let* ((depth (1- (length org-section-numbers)))
2277 (string "")
2278 (fmts (car org-export-section-number-format))
2279 (term (cdr org-export-section-number-format))
2280 (sep "")
2281 ctype fmt idx n)
2282 (when level
2283 (when (> level -1)
2284 (aset org-section-numbers
2285 level (1+ (aref org-section-numbers level))))
2286 (setq idx (1+ level))
2287 (while (<= idx depth)
2288 (if (not (= idx 1))
2289 (aset org-section-numbers idx 0))
2290 (setq idx (1+ idx))))
2291 (setq idx 0)
2292 (while (<= idx depth)
2293 (when (> (aref org-section-numbers idx) 0)
2294 (setq fmt (or (pop fmts) fmt)
2295 ctype (car fmt)
2296 n (aref org-section-numbers idx)
2297 string (if (> n 0)
2298 (concat string sep (org-number-to-counter n ctype))
2299 (concat string ".0"))
2300 sep (nth 1 fmt)))
2301 (setq idx (1+ idx)))
2302 (save-match-data
2303 (if (string-match "\\`\\([@0]\\.\\)+" string)
2304 (setq string (replace-match "" t nil string)))
2305 (if (string-match "\\(\\.0\\)+\\'" string)
2306 (setq string (replace-match "" t nil string))))
2307 (concat string term)))
2309 (defun org-number-to-counter (n type)
2310 "Concert number N to a string counter, according to TYPE.
2311 TYPE must be a string, any of:
2312 1 number
2313 A A,B,....
2314 a a,b,....
2315 I upper case roman numeral
2316 i lower case roman numeral"
2317 (cond
2318 ((equal type "1") (number-to-string n))
2319 ((equal type "A") (char-to-string (+ ?A n -1)))
2320 ((equal type "a") (char-to-string (+ ?a n -1)))
2321 ((equal type "I") (org-number-to-roman n))
2322 ((equal type "i") (downcase (org-number-to-roman n)))
2323 (t (error "Invalid counter type `%s'" type))))
2325 (defun org-number-to-roman (n)
2326 "Convert integer N into a roman numeral."
2327 (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
2328 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
2329 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
2330 ( 1 . "I")))
2331 (res ""))
2332 (if (<= n 0)
2333 (number-to-string n)
2334 (while roman
2335 (if (>= n (caar roman))
2336 (setq n (- n (caar roman))
2337 res (concat res (cdar roman)))
2338 (pop roman)))
2339 res)))
2341 ;;; Macros
2343 (defun org-export-preprocess-apply-macros ()
2344 "Replace macro references."
2345 (goto-char (point-min))
2346 (let (sy val key args args2 ind-str s n)
2347 (while (re-search-forward
2348 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
2349 nil t)
2350 (unless (save-match-data (save-excursion
2351 (goto-char (point-at-bol))
2352 (looking-at "[ \t]*#\\+macro")))
2353 ;; Get macro name (KEY), arguments (ARGS), and indentation of
2354 ;; current line (IND-STR) as strings.
2355 (setq key (downcase (match-string 1))
2356 args (match-string 3)
2357 ind-str (save-match-data (save-excursion
2358 (beginning-of-line)
2359 (looking-at "^\\([ \t]*\\).*")
2360 (match-string 1))))
2361 ;; When macro is defined, retrieve replacement text in VAL,
2362 ;; and proceed with expansion.
2363 (when (setq val (or (plist-get org-export-opt-plist
2364 (intern (concat ":macro-" key)))
2365 (plist-get org-export-opt-plist
2366 (intern (concat ":" key)))))
2367 (save-match-data
2368 ;; If arguments are provided, first retrieve them properly
2369 ;; (in ARGS, as a list), then replace them in VAL.
2370 (when args
2371 (setq args (org-split-string args ",") args2 nil)
2372 (while args
2373 (while (string-match "\\\\\\'" (car args))
2374 ;; Repair bad splits.
2375 (setcar (cdr args) (concat (substring (car args) 0 -1)
2376 "," (nth 1 args)))
2377 (pop args))
2378 (push (pop args) args2))
2379 (setq args (mapcar 'org-trim (nreverse args2)))
2380 (setq s 0)
2381 (while (string-match "\\$\\([0-9]+\\)" val s)
2382 (setq s (1+ (match-beginning 0))
2383 n (string-to-number (match-string 1 val)))
2384 (and (>= (length args) n)
2385 (setq val (replace-match (nth (1- n) args) t t val)))))
2386 ;; VAL starts with "(eval": it is a sexp, `eval' it.
2387 (when (string-match "\\`(eval\\>" val)
2388 (setq val (eval (read val))))
2389 ;; Ensure VAL is a string (or nil) and that each new line
2390 ;; is indented as the first one.
2391 (setq val (and val
2392 (mapconcat 'identity
2393 (org-split-string
2394 (if (stringp val) val (format "%s" val))
2395 "\n")
2396 (concat "\n" ind-str)))))
2397 ;; Eventually do the replacement, if VAL isn't nil. Move
2398 ;; point at beginning of macro for recursive expansions.
2399 (when val
2400 (replace-match val t t)
2401 (goto-char (match-beginning 0))))))))
2403 (defun org-export-apply-macros-in-string (s)
2404 "Apply the macros in string S."
2405 (when s
2406 (with-temp-buffer
2407 (insert s)
2408 (org-export-preprocess-apply-macros)
2409 (buffer-string))))
2411 ;;; Include files
2413 (defun org-export-handle-include-files ()
2414 "Include the contents of include files, with proper formatting."
2415 (let ((case-fold-search t)
2416 params file markup lang start end prefix prefix1 switches all minlevel currentlevel addlevel lines)
2417 (goto-char (point-min))
2418 (while (re-search-forward "^#\\+include:[ \t]+\\(.*\\)" nil t)
2419 (setq params (read (concat "(" (match-string 1) ")"))
2420 prefix (org-get-and-remove-property 'params :prefix)
2421 prefix1 (org-get-and-remove-property 'params :prefix1)
2422 minlevel (org-get-and-remove-property 'params :minlevel)
2423 addlevel (org-get-and-remove-property 'params :addlevel)
2424 lines (org-get-and-remove-property 'params :lines)
2425 file (org-symname-or-string (pop params))
2426 markup (org-symname-or-string (pop params))
2427 lang (and (member markup '("src" "SRC"))
2428 (org-symname-or-string (pop params)))
2429 switches (mapconcat #'(lambda (x) (format "%s" x)) params " ")
2430 start nil end nil)
2431 (delete-region (match-beginning 0) (match-end 0))
2432 (setq currentlevel (or (org-current-level) 0))
2433 (if (or (not file)
2434 (not (file-exists-p file))
2435 (not (file-readable-p file)))
2436 (insert (format "CANNOT INCLUDE FILE %s" file))
2437 (setq all (cons file all))
2438 (when markup
2439 (if (equal (downcase markup) "src")
2440 (setq start (format "#+begin_src %s %s\n"
2441 (or lang "fundamental")
2442 (or switches ""))
2443 end "#+end_src")
2444 (setq start (format "#+begin_%s %s\n" markup switches)
2445 end (format "#+end_%s" markup))))
2446 (insert (or start ""))
2447 (insert (org-get-file-contents (expand-file-name file)
2448 prefix prefix1 markup currentlevel minlevel addlevel lines))
2449 (or (bolp) (newline))
2450 (insert (or end ""))))
2451 all))
2453 (defun org-export-handle-include-files-recurse ()
2454 "Recursively include files aborting on circular inclusion."
2455 (let ((now (list org-current-export-file)) all)
2456 (while now
2457 (setq all (append now all))
2458 (setq now (org-export-handle-include-files))
2459 (let ((intersection
2460 (delq nil
2461 (mapcar (lambda (el) (when (member el all) el)) now))))
2462 (when intersection
2463 (error "Recursive #+INCLUDE: %S" intersection))))))
2465 (defun org-get-file-contents (file &optional prefix prefix1 markup minlevel parentlevel addlevel lines)
2466 "Get the contents of FILE and return them as a string.
2467 If PREFIX is a string, prepend it to each line. If PREFIX1
2468 is a string, prepend it to the first line instead of PREFIX.
2469 If MARKUP, don't protect org-like lines, the exporter will
2470 take care of the block they are in. If ADDLEVEL is a number,
2471 demote included file to current heading level+ADDLEVEL.
2472 If LINES is a string specifying a range of lines,
2473 include only those lines."
2474 (if (stringp markup) (setq markup (downcase markup)))
2475 (with-temp-buffer
2476 (insert-file-contents file)
2477 (when lines
2478 (let* ((lines (split-string lines "-"))
2479 (lbeg (string-to-number (car lines)))
2480 (lend (string-to-number (cadr lines)))
2481 (beg (if (zerop lbeg) (point-min)
2482 (goto-char (point-min))
2483 (forward-line (1- lbeg))
2484 (point)))
2485 (end (if (zerop lend) (point-max)
2486 (goto-char (point-min))
2487 (forward-line (1- lend))
2488 (point))))
2489 (narrow-to-region beg end)))
2490 (when (or prefix prefix1)
2491 (goto-char (point-min))
2492 (while (not (eobp))
2493 (insert (or prefix1 prefix))
2494 (setq prefix1 "")
2495 (beginning-of-line 2)))
2496 (buffer-string)
2497 (when (member markup '("src" "example"))
2498 (goto-char (point-min))
2499 (while (re-search-forward "^\\([*#]\\|[ \t]*#\\+\\)" nil t)
2500 (goto-char (match-beginning 0))
2501 (insert ",")
2502 (end-of-line 1)))
2503 (when minlevel
2504 (dotimes (lvl minlevel)
2505 (org-map-region 'org-demote (point-min) (point-max))))
2506 (when addlevel
2507 (let ((inclevel (or (if (org-before-first-heading-p)
2508 (1- (and (outline-next-heading)
2509 (org-current-level)))
2510 (1- (org-current-level)))
2511 0)))
2512 (dotimes (level (- (+ parentlevel addlevel) inclevel))
2513 (org-map-region 'org-demote (point-min) (point-max)))))
2514 (buffer-string)))
2516 (defun org-get-and-remove-property (listvar prop)
2517 "Check if the value of LISTVAR contains PROP as a property.
2518 If yes, return the value of that property (i.e. the element following
2519 in the list) and remove property and value from the list in LISTVAR."
2520 (let ((list (symbol-value listvar)) m v)
2521 (when (setq m (member prop list))
2522 (setq v (nth 1 m))
2523 (if (equal (car list) prop)
2524 (set listvar (cddr list))
2525 (setcdr (nthcdr (- (length list) (length m) 1) list)
2526 (cddr m))
2527 (set listvar list)))
2530 (defun org-symname-or-string (s)
2531 (if (symbolp s)
2532 (if s (symbol-name s) s)
2535 ;;; Fontification and line numbers for code examples
2537 (defvar org-export-last-code-line-counter-value 0)
2539 (defun org-export-replace-src-segments-and-examples ()
2540 "Replace source code segments with special code for export."
2541 (setq org-export-last-code-line-counter-value 0)
2542 (let ((case-fold-search t)
2543 lang code trans opts indent caption)
2544 (goto-char (point-min))
2545 (while (re-search-forward
2546 "\\(^\\([ \t]*\\)#\\+BEGIN_SRC:?\\([ \t]+\\([^ \t\n]+\\)\\)?\\(.*\\)\n\\([^\000]+?\n\\)[ \t]*#\\+END_SRC.*\n?\\)\\|\\(^\\([ \t]*\\)#\\+BEGIN_EXAMPLE:?\\(?:[ \t]+\\(.*\\)\\)?\n\\([^\000]+?\n\\)[ \t]*#\\+END_EXAMPLE.*\n?\\)"
2547 nil t)
2548 (if (match-end 1)
2549 (if (not (match-string 4))
2550 (error "Source block missing language specification: %s"
2551 (let* ((body (match-string 6))
2552 (nothing (message "body:%s" body))
2553 (preview (or (and (string-match
2554 "^[ \t]*\\([^\n\r]*\\)" body)
2555 (match-string 1 body)) body)))
2556 (if (> (length preview) 35)
2557 (concat (substring preview 0 32) "...")
2558 preview)))
2559 ;; src segments
2560 (setq lang (match-string 4)
2561 opts (match-string 5)
2562 code (match-string 6)
2563 indent (length (match-string 2))
2564 caption (get-text-property 0 'org-caption (match-string 0))))
2565 (setq lang nil
2566 opts (match-string 9)
2567 code (match-string 10)
2568 indent (length (match-string 8))
2569 caption (get-text-property 0 'org-caption (match-string 0))))
2571 (setq trans (org-export-format-source-code-or-example
2572 lang code opts indent caption))
2573 (replace-match trans t t))))
2575 (defvar org-export-latex-verbatim-wrap) ;; defined in org-latex.el
2576 (defvar org-export-latex-listings) ;; defined in org-latex.el
2577 (defvar org-export-latex-listings-langs) ;; defined in org-latex.el
2578 (defvar org-export-latex-listings-w-names) ;; defined in org-latex.el
2579 (defvar org-export-latex-minted-langs) ;; defined in org-latex.el
2580 (defvar org-export-latex-custom-lang-environments) ;; defined in org-latex.el
2581 (defvar org-export-latex-listings-options) ;; defined in org-latex.el
2582 (defvar org-export-latex-minted-options) ;; defined in org-latex.el
2584 (defun org-remove-formatting-on-newlines-in-region (beg end)
2585 "Remove formatting on newline characters."
2586 (interactive "r")
2587 (save-excursion
2588 (goto-char beg)
2589 (while (progn (end-of-line) (< (point) end))
2590 (put-text-property (point) (1+ (point)) 'face nil)
2591 (forward-char 1))))
2593 (defun org-export-format-source-code-or-example
2594 (lang code &optional opts indent caption)
2595 "Format CODE from language LANG and return it formatted for export.
2596 The CODE is marked up in `org-export-current-backend' format.
2598 Check if a function by name
2599 \"org-<backend>-format-source-code-or-example\" is bound. If yes,
2600 use it as the custom formatter. Otherwise, use the default
2601 formatter. Default formatters are provided for docbook, html,
2602 latex and ascii backends. For example, use
2603 `org-html-format-source-code-or-example' to provide a custom
2604 formatter for export to \"html\".
2606 If LANG is nil, do not add any fontification.
2607 OPTS contains formatting options, like `-n' for triggering numbering lines,
2608 and `+n' for continuing previous numbering.
2609 Code formatting according to language currently only works for HTML.
2610 Numbering lines works for all three major backends (html, latex, and ascii).
2611 INDENT was the original indentation of the block."
2612 (save-match-data
2613 (let* ((backend-name (symbol-name org-export-current-backend))
2614 (backend-formatter
2615 (intern (format "org-%s-format-source-code-or-example"
2616 backend-name)))
2617 (backend-feature (intern (concat "org-" backend-name)))
2618 (backend-formatter
2619 (and (require (intern (concat "org-" backend-name)) nil)
2620 (fboundp backend-formatter) backend-formatter))
2621 num cont rtn rpllbl keepp textareap preserve-indentp cols rows fmt)
2622 (setq opts (or opts "")
2623 num (string-match "[-+]n\\>" opts)
2624 cont (string-match "\\+n\\>" opts)
2625 rpllbl (string-match "-r\\>" opts)
2626 keepp (string-match "-k\\>" opts)
2627 textareap (string-match "-t\\>" opts)
2628 preserve-indentp (or org-src-preserve-indentation
2629 (string-match "-i\\>" opts))
2630 cols (if (string-match "-w[ \t]+\\([0-9]+\\)" opts)
2631 (string-to-number (match-string 1 opts))
2633 rows (if (string-match "-h[ \t]+\\([0-9]+\\)" opts)
2634 (string-to-number (match-string 1 opts))
2635 (org-count-lines code))
2636 fmt (if (string-match "-l[ \t]+\"\\([^\"\n]+\\)\"" opts)
2637 (match-string 1 opts)))
2638 (when (and textareap (eq org-export-current-backend 'html))
2639 ;; we cannot use numbering or highlighting.
2640 (setq num nil cont nil lang nil))
2641 (if keepp (setq rpllbl 'keep))
2642 (setq rtn (if preserve-indentp code (org-remove-indentation code)))
2643 (when (string-match "^," rtn)
2644 (setq rtn (with-temp-buffer
2645 (insert rtn)
2646 ;; Free up the protected lines
2647 (goto-char (point-min))
2648 (while (re-search-forward "^," nil t)
2649 (if (or (equal lang "org")
2650 (save-match-data
2651 (looking-at "\\([*#]\\|[ \t]*#\\+\\)")))
2652 (replace-match ""))
2653 (end-of-line 1))
2654 (buffer-string))))
2655 ;; Now backend-specific coding
2656 (setq rtn
2657 (cond
2658 (backend-formatter
2659 (funcall backend-formatter rtn lang caption textareap cols rows num
2660 cont rpllbl fmt))
2661 ((eq org-export-current-backend 'docbook)
2662 (setq rtn (org-export-number-lines rtn 0 0 num cont rpllbl fmt))
2663 (concat "<programlisting><![CDATA["
2665 "]]></programlisting>\n"))
2666 ((eq org-export-current-backend 'html)
2667 ;; We are exporting to HTML
2668 (when lang
2669 (if (featurep 'xemacs)
2670 (require 'htmlize)
2671 (require 'htmlize nil t))
2672 (when (not (fboundp 'htmlize-region-for-paste))
2673 ;; we do not have htmlize.el, or an old version of it
2674 (setq lang nil)
2675 (message
2676 "htmlize.el 1.34 or later is needed for source code formatting")))
2678 (if lang
2679 (let* ((lang-m (when lang
2680 (or (cdr (assoc lang org-src-lang-modes))
2681 lang)))
2682 (mode (and lang-m (intern
2683 (concat
2684 (if (symbolp lang-m)
2685 (symbol-name lang-m)
2686 lang-m)
2687 "-mode"))))
2688 (org-inhibit-startup t)
2689 (org-startup-folded nil))
2690 (setq rtn
2691 (with-temp-buffer
2692 (insert rtn)
2693 (if (functionp mode)
2694 (funcall mode)
2695 (fundamental-mode))
2696 (font-lock-fontify-buffer)
2697 ;; markup each line separately
2698 (org-remove-formatting-on-newlines-in-region (point-min) (point-max))
2699 (org-src-mode)
2700 (set-buffer-modified-p nil)
2701 (org-export-htmlize-region-for-paste
2702 (point-min) (point-max))))
2703 (if (string-match "<pre\\([^>]*\\)>\n*" rtn)
2704 (setq rtn
2705 (concat
2706 (if caption
2707 (concat
2708 "<div class=\"org-src-container\">"
2709 (format
2710 "<label class=\"org-src-name\">%s</label>"
2711 caption))
2713 (replace-match
2714 (format "<pre class=\"src src-%s\">\n" lang)
2715 t t rtn)
2716 (if caption "</div>" "")))))
2717 (if textareap
2718 (setq rtn (concat
2719 (format "<p>\n<textarea cols=\"%d\" rows=\"%d\">"
2720 cols rows)
2721 rtn "</textarea>\n</p>\n"))
2722 (with-temp-buffer
2723 (insert rtn)
2724 (goto-char (point-min))
2725 (while (re-search-forward "[<>&]" nil t)
2726 (replace-match (cdr (assq (char-before)
2727 '((?&."&amp;")(?<."&lt;")(?>."&gt;"))))
2728 t t))
2729 (setq rtn (buffer-string)))
2730 (setq rtn (concat "<pre class=\"example\">\n" rtn "</pre>\n"))))
2731 (unless textareap
2732 (setq rtn (org-export-number-lines rtn 1 1 num cont rpllbl fmt)))
2733 (if (string-match "\\(\\`<[^>]*>\\)\n" rtn)
2734 (setq rtn (replace-match "\\1" t nil rtn)))
2735 rtn)
2736 ((eq org-export-current-backend 'latex)
2737 (setq rtn (org-export-number-lines rtn 0 0 num cont rpllbl fmt))
2738 (cond
2739 ((and lang org-export-latex-listings)
2740 (let* ((make-option-string
2741 (lambda (pair)
2742 (concat (first pair)
2743 (if (> (length (second pair)) 0)
2744 (concat "=" (second pair))))))
2745 (lang-sym (intern lang))
2746 (minted-p (eq org-export-latex-listings 'minted))
2747 (listings-p (not minted-p))
2748 (backend-lang
2749 (or (cadr
2750 (assq
2751 lang-sym
2752 (cond
2753 (minted-p org-export-latex-minted-langs)
2754 (listings-p org-export-latex-listings-langs))))
2755 lang))
2756 (custom-environment
2757 (cadr
2758 (assq
2759 lang-sym
2760 org-export-latex-custom-lang-environments))))
2761 (concat
2762 (when (and listings-p (not custom-environment))
2763 (format
2764 "\\lstset{%s}\n"
2765 (mapconcat
2766 make-option-string
2767 (append org-export-latex-listings-options
2768 `(("language" ,backend-lang))) ",")))
2769 (when (and caption org-export-latex-listings-w-names)
2770 (format
2771 "\n%s $\\equiv$ \n"
2772 (replace-regexp-in-string "_" "\\\\_" caption)))
2773 (cond
2774 (custom-environment
2775 (format "\\begin{%s}\n%s\\end{%s}\n"
2776 custom-environment rtn custom-environment))
2777 (listings-p
2778 (format "\\begin{%s}\n%s\\end{%s}"
2779 "lstlisting" rtn "lstlisting"))
2780 (minted-p
2781 (format
2782 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2783 (mapconcat make-option-string
2784 org-export-latex-minted-options ",")
2785 backend-lang rtn))))))
2786 (t (concat (car org-export-latex-verbatim-wrap)
2787 rtn (cdr org-export-latex-verbatim-wrap)))))
2788 ((eq org-export-current-backend 'ascii)
2789 ;; This is not HTML or LaTeX, so just make it an example.
2790 (setq rtn (org-export-number-lines rtn 0 0 num cont rpllbl fmt))
2791 (concat caption "\n"
2792 (concat
2793 (mapconcat
2794 (lambda (l) (concat " " l))
2795 (org-split-string rtn "\n")
2796 "\n")
2797 "\n")))
2799 (error "Don't know how to markup source or example block in %s"
2800 (upcase backend-name)))))
2801 (setq rtn
2802 (concat
2803 "\n#+BEGIN_" backend-name "\n"
2804 (org-add-props rtn
2805 '(org-protected t org-example t org-native-text t))
2806 "\n#+END_" backend-name "\n"))
2807 (org-add-props rtn nil 'original-indentation indent))))
2809 (defun org-export-number-lines (text &optional skip1 skip2 number cont
2810 replace-labels label-format preprocess)
2811 "Apply line numbers to literal examples and handle code references.
2812 Handle user-specified options under info node `(org)Literal
2813 examples' and return the modified source block.
2815 TEXT contains the source or example block.
2817 SKIP1 and SKIP2 are the number of lines that are to be skipped at
2818 the beginning and end of TEXT. Use these to skip over
2819 backend-specific lines pre-pended or appended to the original
2820 source block.
2822 NUMBER is non-nil if the literal example specifies \"+n\" or
2823 \"-n\" switch. If NUMBER is non-nil add line numbers.
2825 CONT is non-nil if the literal example specifies \"+n\" switch.
2826 If CONT is nil, start numbering this block from 1. Otherwise
2827 continue numbering from the last numbered block.
2829 REPLACE-LABELS is dual-purpose.
2830 1. It controls the retention of labels in the exported block.
2831 2. It specifies in what manner the links (or references) to a
2832 labeled line be formatted.
2834 REPLACE-LABELS is the symbol `keep' if the literal example
2835 specifies \"-k\" option, is numeric if the literal example
2836 specifies \"-r\" option and is nil otherwise.
2838 Handle REPLACE-LABELS as below:
2839 - If nil, retain labels in the exported block and use
2840 user-provided labels for referencing the labeled lines.
2841 - If it is a number, remove labels in the exported block and use
2842 one of line numbers or labels for referencing labeled lines based
2843 on NUMBER option.
2844 - If it is a keep, retain labels in the exported block and use
2845 one of line numbers or labels for referencing labeled lines
2846 based on NUMBER option.
2848 LABEL-FORMAT is the value of \"-l\" switch associated with
2849 literal example. See `org-coderef-label-format'.
2851 PREPROCESS is intended for backend-agnostic handling of source
2852 block numbering. When non-nil do the following:
2853 - do not number the lines
2854 - always strip the labels from exported block
2855 - do not make the labeled line a target of an incoming link.
2856 Instead mark the labeled line with `org-coderef' property and
2857 store the label in it."
2858 (setq skip1 (or skip1 0) skip2 (or skip2 0))
2859 (if (and number (not cont)) (setq org-export-last-code-line-counter-value 0))
2860 (with-temp-buffer
2861 (insert text)
2862 (goto-char (point-max))
2863 (skip-chars-backward " \t\n\r")
2864 (delete-region (point) (point-max))
2865 (beginning-of-line (- 1 skip2))
2866 (let* ((last (org-current-line))
2867 (n org-export-last-code-line-counter-value)
2868 (nmax (+ n (- last skip1)))
2869 (fmt (format "%%%dd: " (length (number-to-string nmax))))
2871 (cond
2872 ((eq org-export-current-backend 'html) (format "<span class=\"linenr\">%s</span>"
2873 fmt))
2874 ((eq org-export-current-backend 'ascii) fmt)
2875 ((eq org-export-current-backend 'latex) fmt)
2876 ((eq org-export-current-backend 'docbook) fmt)
2877 (t "")))
2878 (label-format (or label-format org-coderef-label-format))
2879 (label-pre (if (string-match "%s" label-format)
2880 (substring label-format 0 (match-beginning 0))
2881 label-format))
2882 (label-post (if (string-match "%s" label-format)
2883 (substring label-format (match-end 0))
2884 ""))
2885 (lbl-re
2886 (concat
2887 ".*?\\S-.*?\\([ \t]*\\("
2888 (regexp-quote label-pre)
2889 "\\([-a-zA-Z0-9_ ]+\\)"
2890 (regexp-quote label-post)
2891 "\\)\\)"))
2892 ref)
2894 (org-goto-line (1+ skip1))
2895 (while (and (re-search-forward "^" nil t) (not (eobp)) (< n nmax))
2896 (when number (incf n))
2897 (if (or preprocess (not number))
2898 (forward-char 1)
2899 (insert (format fm n)))
2900 (when (looking-at lbl-re)
2901 (setq ref (match-string 3))
2902 (cond ((numberp replace-labels)
2903 ;; remove labels; use numbers for references when lines
2904 ;; are numbered, use labels otherwise
2905 (delete-region (match-beginning 1) (match-end 1))
2906 (push (cons ref (if (> n 0) n ref)) org-export-code-refs))
2907 ((eq replace-labels 'keep)
2908 ;; don't remove labels; use numbers for references when
2909 ;; lines are numbered, use labels otherwise
2910 (goto-char (match-beginning 2))
2911 (delete-region (match-beginning 2) (match-end 2))
2912 (unless preprocess
2913 (insert "(" ref ")"))
2914 (push (cons ref (if (> n 0) n (concat "(" ref ")")))
2915 org-export-code-refs))
2917 ;; don't remove labels and don't use numbers for
2918 ;; references
2919 (goto-char (match-beginning 2))
2920 (delete-region (match-beginning 2) (match-end 2))
2921 (unless preprocess
2922 (insert "(" ref ")"))
2923 (push (cons ref (concat "(" ref ")")) org-export-code-refs)))
2924 (when (and (eq org-export-current-backend 'html) (not preprocess))
2925 (save-excursion
2926 (beginning-of-line 1)
2927 (insert (format "<span id=\"coderef-%s\" class=\"coderef-off\">"
2928 ref))
2929 (end-of-line 1)
2930 (insert "</span>")))
2931 (when preprocess
2932 (add-text-properties
2933 (point-at-bol) (point-at-eol) (list 'org-coderef ref)))))
2934 (setq org-export-last-code-line-counter-value n)
2935 (goto-char (point-max))
2936 (newline)
2937 (buffer-string))))
2939 (defun org-search-todo-below (line lines level)
2940 "Search the subtree below LINE for any TODO entries."
2941 (let ((rest (cdr (memq line lines)))
2942 (re org-todo-line-regexp)
2943 line lv todo)
2944 (catch 'exit
2945 (while (setq line (pop rest))
2946 (if (string-match re line)
2947 (progn
2948 (setq lv (- (match-end 1) (match-beginning 1))
2949 todo (and (match-beginning 2)
2950 (not (member (match-string 2 line)
2951 org-done-keywords))))
2952 ; TODO, not DONE
2953 (if (<= lv level) (throw 'exit nil))
2954 (if todo (throw 'exit t))))))))
2956 ;;;###autoload
2957 (defun org-export-visible (type arg)
2958 "Create a copy of the visible part of the current buffer, and export it.
2959 The copy is created in a temporary buffer and removed after use.
2960 TYPE is the final key (as a string) that also selects the export command in
2961 the \\<org-mode-map>\\[org-export] export dispatcher.
2962 As a special case, if the you type SPC at the prompt, the temporary
2963 org-mode file will not be removed but presented to you so that you can
2964 continue to use it. The prefix arg ARG is passed through to the exporting
2965 command."
2966 (interactive
2967 (list (progn
2968 (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]buffer with HTML [D]ocBook [l]atex [p]df [d]view pdf [L]atex buffer [x]OXO [ ]keep buffer")
2969 (read-char-exclusive))
2970 current-prefix-arg))
2971 (if (not (member type '(?a ?n ?u ?\C-a ?b ?\C-b ?h ?D ?x ?\ ?l ?p ?d ?L ?H ?R)))
2972 (error "Invalid export key"))
2973 (let* ((binding (cdr (assoc type
2975 (?a . org-export-as-ascii)
2976 (?A . org-export-as-ascii-to-buffer)
2977 (?n . org-export-as-latin1)
2978 (?N . org-export-as-latin1-to-buffer)
2979 (?u . org-export-as-utf8)
2980 (?U . org-export-as-utf8-to-buffer)
2981 (?\C-a . org-export-as-ascii)
2982 (?b . org-export-as-html-and-open)
2983 (?\C-b . org-export-as-html-and-open)
2984 (?h . org-export-as-html)
2985 (?H . org-export-as-html-to-buffer)
2986 (?R . org-export-region-as-html)
2987 (?D . org-export-as-docbook)
2989 (?l . org-export-as-latex)
2990 (?p . org-export-as-pdf)
2991 (?d . org-export-as-pdf-and-open)
2992 (?L . org-export-as-latex-to-buffer)
2994 (?x . org-export-as-xoxo)))))
2995 (keepp (equal type ?\ ))
2996 (file buffer-file-name)
2997 (buffer (get-buffer-create "*Org Export Visible*"))
2998 s e)
2999 ;; Need to hack the drawers here.
3000 (save-excursion
3001 (goto-char (point-min))
3002 (while (re-search-forward org-drawer-regexp nil t)
3003 (goto-char (match-beginning 1))
3004 (or (outline-invisible-p) (org-flag-drawer nil))))
3005 (with-current-buffer buffer (erase-buffer))
3006 (save-excursion
3007 (setq s (goto-char (point-min)))
3008 (while (not (= (point) (point-max)))
3009 (goto-char (org-find-invisible))
3010 (append-to-buffer buffer s (point))
3011 (setq s (goto-char (org-find-visible))))
3012 (org-cycle-hide-drawers 'all)
3013 (goto-char (point-min))
3014 (unless keepp
3015 ;; Copy all comment lines to the end, to make sure #+ settings are
3016 ;; still available for the second export step. Kind of a hack, but
3017 ;; does do the trick.
3018 (if (looking-at "#[^\r\n]*")
3019 (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
3020 (when (re-search-forward "^\\*+[ \t]+" nil t)
3021 (while (re-search-backward "[\n\r]#[^\n\r]*" nil t)
3022 (append-to-buffer buffer (1+ (match-beginning 0))
3023 (min (point-max) (1+ (match-end 0)))))))
3024 (set-buffer buffer)
3025 (let ((buffer-file-name file)
3026 (org-inhibit-startup t))
3027 (org-mode)
3028 (show-all)
3029 (unless keepp (funcall binding arg))))
3030 (if (not keepp)
3031 (kill-buffer buffer)
3032 (switch-to-buffer-other-window buffer)
3033 (goto-char (point-min)))))
3035 (defvar org-export-htmlized-org-css-url) ;; defined in org-html.el
3037 (defun org-export-string (string fmt &optional dir)
3038 "Export STRING to FMT using existing export facilities.
3039 During export STRING is saved to a temporary file whose location
3040 could vary. Optional argument DIR can be used to force the
3041 directory in which the temporary file is created during export
3042 which can be useful for resolving relative paths. Dir defaults
3043 to the value of `temporary-file-directory'."
3044 (let ((temporary-file-directory (or dir temporary-file-directory))
3045 (tmp-file (make-temp-file "org-")))
3046 (unwind-protect
3047 (with-temp-buffer
3048 (insert string)
3049 (write-file tmp-file)
3050 (org-load-modules-maybe)
3051 (unless org-local-vars
3052 (setq org-local-vars (org-get-local-variables)))
3053 (eval ;; convert to fmt -- mimicking `org-run-like-in-org-mode'
3054 (list 'let org-local-vars
3055 (list (intern (format "org-export-as-%s" fmt))
3056 nil nil ''string t dir))))
3057 (delete-file tmp-file))))
3059 ;;;###autoload
3060 (defun org-export-as-org (arg &optional ext-plist to-buffer body-only pub-dir)
3061 "Make a copy with not-exporting stuff removed.
3062 The purpose of this function is to provide a way to export the source
3063 Org file of a webpage in Org format, but with sensitive and/or irrelevant
3064 stuff removed. This command will remove the following:
3066 - archived trees (if the variable `org-export-with-archived-trees' is nil)
3067 - comment blocks and trees starting with the COMMENT keyword
3068 - only trees that are consistent with `org-export-select-tags'
3069 and `org-export-exclude-tags'.
3071 The only arguments that will be used are EXT-PLIST and PUB-DIR,
3072 all the others will be ignored (but are present so that the general
3073 mechanism to call publishing functions will work).
3075 EXT-PLIST is a property list with external parameters overriding
3076 org-mode's default settings, but still inferior to file-local
3077 settings. When PUB-DIR is set, use this as the publishing
3078 directory."
3079 (interactive "P")
3080 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
3081 ext-plist
3082 (org-infile-export-plist)))
3083 (bfname (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
3084 (filename (concat (file-name-as-directory
3085 (or pub-dir
3086 (org-export-directory :org opt-plist)))
3087 (file-name-sans-extension
3088 (file-name-nondirectory bfname))
3089 ".org"))
3090 (filename (and filename
3091 (if (equal (file-truename filename)
3092 (file-truename bfname))
3093 (concat (file-name-sans-extension filename)
3094 "-source."
3095 (file-name-extension filename))
3096 filename)))
3097 (backup-inhibited t)
3098 (buffer (find-file-noselect filename))
3099 (region (buffer-string))
3100 str-ret)
3101 (save-excursion
3102 (org-pop-to-buffer-same-window buffer)
3103 (erase-buffer)
3104 (insert region)
3105 (let ((org-inhibit-startup t)) (org-mode))
3106 (org-install-letbind)
3108 ;; Get rid of archived trees
3109 (org-export-remove-archived-trees (plist-get opt-plist :archived-trees))
3111 ;; Remove comment environment and comment subtrees
3112 (org-export-remove-comment-blocks-and-subtrees)
3114 ;; Get rid of excluded trees
3115 (org-export-handle-export-tags (plist-get opt-plist :select-tags)
3116 (plist-get opt-plist :exclude-tags))
3118 (when (or (plist-get opt-plist :plain-source)
3119 (not (or (plist-get opt-plist :plain-source)
3120 (plist-get opt-plist :htmlized-source))))
3121 ;; Either nothing special is requested (default call)
3122 ;; or the plain source is explicitly requested
3123 ;; so: save it
3124 (save-buffer))
3125 (when (plist-get opt-plist :htmlized-source)
3126 ;; Make the htmlized version
3127 (require 'htmlize)
3128 (require 'org-html)
3129 (font-lock-fontify-buffer)
3130 (let* ((htmlize-output-type 'css)
3131 (newbuf (htmlize-buffer)))
3132 (with-current-buffer newbuf
3133 (when org-export-htmlized-org-css-url
3134 (goto-char (point-min))
3135 (and (re-search-forward
3136 "<style type=\"text/css\">[^\000]*?\n[ \t]*</style>.*"
3137 nil t)
3138 (replace-match
3139 (format
3140 "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\">"
3141 org-export-htmlized-org-css-url)
3142 t t)))
3143 (write-file (concat filename ".html")))
3144 (kill-buffer newbuf)))
3145 (set-buffer-modified-p nil)
3146 (if (equal to-buffer 'string)
3147 (progn (setq str-ret (buffer-string))
3148 (kill-buffer (current-buffer))
3149 str-ret)
3150 (kill-buffer (current-buffer))))))
3152 (defvar org-archive-location) ;; gets loaded with the org-archive require.
3153 (defun org-get-current-options ()
3154 "Return a string with current options as keyword options.
3155 Does include HTML export options as well as TODO and CATEGORY stuff."
3156 (require 'org-archive)
3157 (format
3158 "#+TITLE: %s
3159 #+AUTHOR: %s
3160 #+EMAIL: %s
3161 #+DATE: %s
3162 #+DESCRIPTION:
3163 #+KEYWORDS:
3164 #+LANGUAGE: %s
3165 #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s -:%s f:%s *:%s <:%s
3166 #+OPTIONS: TeX:%s LaTeX:%s skip:%s d:%s todo:%s pri:%s tags:%s
3168 #+EXPORT_SELECT_TAGS: %s
3169 #+EXPORT_EXCLUDE_TAGS: %s
3170 #+LINK_UP: %s
3171 #+LINK_HOME: %s
3172 #+XSLT:
3173 #+CATEGORY: %s
3174 #+SEQ_TODO: %s
3175 #+TYP_TODO: %s
3176 #+PRIORITIES: %c %c %c
3177 #+DRAWERS: %s
3178 #+STARTUP: %s %s %s %s %s
3179 #+TAGS: %s
3180 #+FILETAGS: %s
3181 #+ARCHIVE: %s
3182 #+LINK: %s
3184 (buffer-name) (user-full-name) user-mail-address
3185 (format-time-string (substring (car org-time-stamp-formats) 1 -1))
3186 org-export-default-language
3187 org-export-headline-levels
3188 org-export-with-section-numbers
3189 org-export-with-toc
3190 org-export-preserve-breaks
3191 org-export-html-expand
3192 org-export-with-fixed-width
3193 org-export-with-tables
3194 org-export-with-sub-superscripts
3195 org-export-with-special-strings
3196 org-export-with-footnotes
3197 org-export-with-emphasize
3198 org-export-with-timestamps
3199 org-export-with-TeX-macros
3200 org-export-with-LaTeX-fragments
3201 org-export-skip-text-before-1st-heading
3202 org-export-with-drawers
3203 org-export-with-todo-keywords
3204 org-export-with-priority
3205 org-export-with-tags
3206 (if (featurep 'org-jsinfo) (org-infojs-options-inbuffer-template) "")
3207 (mapconcat 'identity org-export-select-tags " ")
3208 (mapconcat 'identity org-export-exclude-tags " ")
3209 org-export-html-link-up
3210 org-export-html-link-home
3211 (or (ignore-errors
3212 (file-name-sans-extension
3213 (file-name-nondirectory (buffer-file-name (buffer-base-buffer)))))
3214 "NOFILENAME")
3215 "TODO FEEDBACK VERIFY DONE"
3216 "Me Jason Marie DONE"
3217 org-highest-priority org-lowest-priority org-default-priority
3218 (mapconcat 'identity org-drawers " ")
3219 (cdr (assoc org-startup-folded
3220 '((nil . "showall") (t . "overview") (content . "content"))))
3221 (if org-odd-levels-only "odd" "oddeven")
3222 (if org-hide-leading-stars "hidestars" "showstars")
3223 (if org-startup-align-all-tables "align" "noalign")
3224 (cond ((eq org-log-done t) "logdone")
3225 ((equal org-log-done 'note) "lognotedone")
3226 ((not org-log-done) "nologdone"))
3227 (or (mapconcat (lambda (x)
3228 (cond
3229 ((equal :startgroup (car x)) "{")
3230 ((equal :endgroup (car x)) "}")
3231 ((equal :newline (car x)) "")
3232 ((cdr x) (format "%s(%c)" (car x) (cdr x)))
3233 (t (car x))))
3234 (or org-tag-alist (org-get-buffer-tags)) " ") "")
3235 (mapconcat 'identity org-file-tags " ")
3236 org-archive-location
3237 "org file:~/org/%s.org"))
3239 (defun org-insert-export-options-template ()
3240 "Insert into the buffer a template with information for exporting."
3241 (interactive)
3242 (if (not (bolp)) (newline))
3243 (let ((s (org-get-current-options)))
3244 (and (string-match "#\\+CATEGORY" s)
3245 (setq s (substring s 0 (match-beginning 0))))
3246 (insert s)))
3248 (defvar org-table-colgroup-info nil)
3250 (defun org-table-clean-before-export (lines &optional maybe-quoted)
3251 "Check if the table has a marking column.
3252 If yes remove the column and the special lines."
3253 (setq org-table-colgroup-info nil)
3254 (if (memq nil
3255 (mapcar
3256 (lambda (x) (or (string-match "^[ \t]*|-" x)
3257 (string-match
3258 (if maybe-quoted
3259 "^[ \t]*| *\\\\?\\([\#!$*_^ /]\\) *|"
3260 "^[ \t]*| *\\([\#!$*_^ /]\\) *|")
3261 x)))
3262 lines))
3263 ;; No special marking column
3264 (progn
3265 (setq org-table-clean-did-remove-column nil)
3266 (delq nil
3267 (mapcar
3268 (lambda (x)
3269 (cond
3270 ((org-table-colgroup-line-p x)
3271 ;; This line contains colgroup info, extract it
3272 ;; and then discard the line
3273 (setq org-table-colgroup-info
3274 (mapcar (lambda (x)
3275 (cond ((member x '("<" "&lt;")) :start)
3276 ((member x '(">" "&gt;")) :end)
3277 ((member x '("<>" "&lt;&gt;")) :startend)))
3278 (org-split-string x "[ \t]*|[ \t]*")))
3279 nil)
3280 ((org-table-cookie-line-p x)
3281 ;; This line contains formatting cookies, discard it
3282 nil)
3283 (t x)))
3284 lines)))
3285 ;; there is a special marking column
3286 (setq org-table-clean-did-remove-column t)
3287 (delq nil
3288 (mapcar
3289 (lambda (x)
3290 (cond
3291 ((org-table-colgroup-line-p x)
3292 ;; This line contains colgroup info, extract it
3293 ;; and then discard the line
3294 (setq org-table-colgroup-info
3295 (mapcar (lambda (x)
3296 (cond ((member x '("<" "&lt;")) :start)
3297 ((member x '(">" "&gt;")) :end)
3298 ((member x '("<>" "&lt;&gt;")) :startend)))
3299 (cdr (org-split-string x "[ \t]*|[ \t]*"))))
3300 nil)
3301 ((org-table-cookie-line-p x)
3302 ;; This line contains formatting cookies, discard it
3303 nil)
3304 ((string-match "^[ \t]*| *\\([!_^/$]\\|\\\\\\$\\) *|" x)
3305 ;; ignore this line
3306 nil)
3307 ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
3308 (string-match "^\\([ \t]*\\)|[^|]*|" x))
3309 ;; remove the first column
3310 (replace-match "\\1|" t nil x))))
3311 lines))))
3313 (defun org-export-cleanup-toc-line (s)
3314 "Remove tags and timestamps from lines going into the toc."
3315 (if (not s)
3316 "" ; Return a string when argument is nil
3317 (when (memq org-export-with-tags '(not-in-toc nil))
3318 (if (string-match (org-re " +:[[:alnum:]_@#%:]+: *$") s)
3319 (setq s (replace-match "" t t s))))
3320 (when org-export-remove-timestamps-from-toc
3321 (while (string-match org-maybe-keyword-time-regexp s)
3322 (setq s (replace-match "" t t s))))
3323 (while (string-match org-bracket-link-regexp s)
3324 (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
3325 t t s)))
3326 (while (string-match "\\[\\([0-9]\\|fn:[^]]*\\)\\]" s)
3327 (setq s (replace-match "" t t s)))
3331 (defun org-get-text-property-any (pos prop &optional object)
3332 (or (get-text-property pos prop object)
3333 (and (setq pos (next-single-property-change pos prop object))
3334 (get-text-property pos prop object))))
3336 (defun org-export-get-coderef-format (path desc)
3337 (save-match-data
3338 (if (and desc (string-match
3339 (regexp-quote (concat "(" path ")"))
3340 desc))
3341 (replace-match "%s" t t desc)
3342 (or desc "%s"))))
3344 (defun org-export-push-to-kill-ring (format)
3345 "Push buffer content to kill ring.
3346 The depends on the variable `org-export-copy-to-kill-ring'."
3347 (when (or (and (eq org-export-copy-to-kill-ring 'if-interactive)
3348 (not (or executing-kbd-macro noninteractive)))
3349 (eq org-export-copy-to-kill-ring t))
3350 (org-kill-new (buffer-string))
3351 (when (fboundp 'x-set-selection)
3352 (ignore-errors (x-set-selection 'PRIMARY (buffer-string)))
3353 (ignore-errors (x-set-selection 'CLIPBOARD (buffer-string))))
3354 (message "%s export done, pushed to kill ring and clipboard" format)))
3356 (provide 'org-exp)
3358 ;; Local variables:
3359 ;; generated-autoload-file: "org-loaddefs.el"
3360 ;; End:
3362 ;;; org-exp.el ends here