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