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