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