org-html.el (org-html-handle-links): Fix bug in setting the attribute for link with...
[org-mode.git] / lisp / org-exp.el
blob82b9003e4fdc125fe351b2c76bce08e4b38ecc4d
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 'org-exp-blocks)
33 (require 'ob-exp)
34 (require 'org-src)
36 (eval-when-compile
37 (require 'cl))
39 (declare-function org-export-latex-preprocess "org-latex" (parameters))
40 (declare-function org-export-ascii-preprocess "org-ascii" (parameters))
41 (declare-function org-export-html-preprocess "org-html" (parameters))
42 (declare-function org-export-docbook-preprocess "org-docbook" (parameters))
43 (declare-function org-infojs-options-inbuffer-template "org-jsinfo" ())
44 (declare-function org-export-htmlize-region-for-paste "org-html" (beg end))
45 (declare-function htmlize-buffer "ext:htmlize" (&optional buffer))
46 (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
47 (declare-function org-table-cookie-line-p "org-table" (line))
48 (declare-function org-table-colgroup-line-p "org-table" (line))
49 (declare-function org-pop-to-buffer-same-window "org-compat"
50 (&optional buffer-or-name norecord label))
51 (declare-function org-unescape-code-in-region "org-src" (beg end))
53 (autoload 'org-export-generic "org-export-generic" "Export using the generic exporter" t)
55 (autoload 'org-export-as-odt "org-odt"
56 "Export the outline to a OpenDocument Text file." t)
57 (autoload 'org-export-as-odt-and-open "org-odt"
58 "Export the outline to a OpenDocument Text file and open it." t)
60 (defgroup org-export nil
61 "Options for exporting org-listings."
62 :tag "Org Export"
63 :group 'org)
65 (defgroup org-export-general nil
66 "General options for exporting Org-mode files."
67 :tag "Org Export General"
68 :group 'org-export)
70 (defcustom org-export-allow-BIND 'confirm
71 "Non-nil means allow #+BIND to define local variable values for export.
72 This is a potential security risk, which is why the user must confirm the
73 use of these lines."
74 :group 'org-export-general
75 :type '(choice
76 (const :tag "Never" nil)
77 (const :tag "Always" t)
78 (const :tag "Make the user confirm for each file" confirm)))
80 ;; FIXME
81 (defvar org-export-publishing-directory nil)
83 (defcustom org-export-show-temporary-export-buffer t
84 "Non-nil means show buffer after exporting to temp buffer.
85 When Org exports to a file, the buffer visiting that file is ever
86 shown, but remains buried. However, when exporting to a temporary
87 buffer, that buffer is popped up in a second window. When this variable
88 is nil, the buffer remains buried also in these cases."
89 :group 'org-export-general
90 :type 'boolean)
92 (defcustom org-export-copy-to-kill-ring t
93 "Non-nil means exported stuff will also be pushed onto the kill ring."
94 :group 'org-export-general
95 :type 'boolean)
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 (:inline-images nil org-export-html-inline-images)
706 (:html-extension nil org-export-html-extension)
707 (:html-preamble nil org-export-html-preamble)
708 (:html-postamble nil org-export-html-postamble)
709 (:xml-declaration nil org-export-html-xml-declaration)
710 (:html-table-tag nil org-export-html-table-tag)
711 (:expand-quoted-html "@" org-export-html-expand)
712 (:timestamp nil org-export-html-with-timestamp)
713 (:publishing-directory nil org-export-publishing-directory)
714 (:select-tags nil org-export-select-tags)
715 (:exclude-tags nil org-export-exclude-tags)
717 (:latex-image-options nil org-export-latex-image-default-option))
718 "List of properties that represent export/publishing variables.
719 Each element is a list of 3 items:
720 1. The property that is used internally, and also for org-publish-project-alist
721 2. The string that can be used in the OPTION lines to set this option,
722 or nil if this option cannot be changed in this way
723 3. The customization variable that sets the default for this option."
726 (defun org-default-export-plist ()
727 "Return the property list with default settings for the export variables."
728 (let* ((infile (org-infile-export-plist))
729 (letbind (plist-get infile :let-bind))
730 (l org-export-plist-vars) rtn e s v)
731 (while (setq e (pop l))
732 (setq s (nth 2 e)
733 v (cond
734 ((assq s letbind) (nth 1 (assq s letbind)))
735 ((boundp s) (symbol-value s)))
736 rtn (cons (car e) (cons v rtn))))
737 rtn))
739 (defvar org-export-inbuffer-options-extra nil
740 "List of additional in-buffer options that should be detected.
741 Just before export, the buffer is scanned for options like #+TITLE, #+EMAIL,
742 etc. Extensions can add to this list to get their options detected, and they
743 can then add a function to `org-export-options-filters' to process these
744 options.
745 Each element in this list must be a list, with the in-buffer keyword as car,
746 and a property (a symbol) as the next element. All occurrences of the
747 keyword will be found, the values concatenated with a space character
748 in between, and the result stored in the export options property list.")
750 (defvar org-export-options-filters nil
751 "Functions to be called to finalize the export/publishing options.
752 All these options are stored in a property list, and each of the functions
753 in this hook gets a chance to modify this property list. Each function
754 must accept the property list as an argument, and must return the (possibly
755 modified) list.")
757 ;; FIXME: should we fold case here?
759 (defun org-infile-export-plist ()
760 "Return the property list with file-local settings for export."
761 (save-excursion
762 (save-restriction
763 (widen)
764 (goto-char (point-min))
765 (let ((re (org-make-options-regexp
766 (append
767 '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE"
768 "MATHJAX"
769 "LINK_UP" "LINK_HOME" "SETUPFILE" "STYLE"
770 "LATEX_HEADER" "LATEX_CLASS" "LATEX_CLASS_OPTIONS"
771 "EXPORT_SELECT_TAGS" "EXPORT_EXCLUDE_TAGS"
772 "KEYWORDS" "DESCRIPTION" "MACRO" "BIND" "XSLT")
773 (mapcar 'car org-export-inbuffer-options-extra))))
774 (case-fold-search t)
775 p key val text options mathjax a pr style
776 latex-header latex-class latex-class-options macros letbind
777 ext-setup-or-nil setup-file setup-dir setup-contents (start 0))
778 (while (or (and ext-setup-or-nil
779 (string-match re ext-setup-or-nil start)
780 (setq start (match-end 0)))
781 (and (setq ext-setup-or-nil nil start 0)
782 (re-search-forward re nil t)))
783 (setq key (upcase (org-match-string-no-properties 1 ext-setup-or-nil))
784 val (org-match-string-no-properties 2 ext-setup-or-nil))
785 (cond
786 ((setq a (assoc key org-export-inbuffer-options-extra))
787 (setq pr (nth 1 a))
788 (setq p (plist-put p pr (concat (plist-get p pr) " " val))))
789 ((string-equal key "TITLE") (setq p (plist-put p :title val)))
790 ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
791 ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
792 ((string-equal key "DATE")
793 ;; If date is an Org timestamp, convert it to a time
794 ;; string using `org-export-date-timestamp-format'
795 (when (string-match org-ts-regexp3 val)
796 (setq val (format-time-string
797 org-export-date-timestamp-format
798 (apply 'encode-time (org-parse-time-string
799 (match-string 0 val))))))
800 (setq p (plist-put p :date val)))
801 ((string-equal key "KEYWORDS") (setq p (plist-put p :keywords val)))
802 ((string-equal key "DESCRIPTION")
803 (setq p (plist-put p :description val)))
804 ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
805 ((string-equal key "STYLE")
806 (setq style (concat style "\n" val)))
807 ((string-equal key "LATEX_HEADER")
808 (setq latex-header (concat latex-header "\n" val)))
809 ((string-equal key "LATEX_CLASS")
810 (setq latex-class val))
811 ((string-equal key "LATEX_CLASS_OPTIONS")
812 (setq latex-class-options val))
813 ((string-equal key "TEXT")
814 (setq text (if text (concat text "\n" val) val)))
815 ((string-equal key "OPTIONS")
816 (setq options (concat val " " options)))
817 ((string-equal key "MATHJAX")
818 (setq mathjax (concat val " " mathjax)))
819 ((string-equal key "BIND")
820 (push (read (concat "(" val ")")) letbind))
821 ((string-equal key "XSLT")
822 (setq p (plist-put p :xslt val)))
823 ((string-equal key "LINK_UP")
824 (setq p (plist-put p :link-up val)))
825 ((string-equal key "LINK_HOME")
826 (setq p (plist-put p :link-home val)))
827 ((string-equal key "EXPORT_SELECT_TAGS")
828 (setq p (plist-put p :select-tags (org-split-string val))))
829 ((string-equal key "EXPORT_EXCLUDE_TAGS")
830 (setq p (plist-put p :exclude-tags (org-split-string val))))
831 ((string-equal key "MACRO")
832 (push val macros))
833 ((equal key "SETUPFILE")
834 (setq setup-file (org-remove-double-quotes (org-trim val))
835 ;; take care of recursive inclusion of setupfiles
836 setup-file (if (or (file-name-absolute-p val) (not setup-dir))
837 (expand-file-name setup-file)
838 (let ((default-directory setup-dir))
839 (expand-file-name setup-file))))
840 (setq setup-dir (file-name-directory setup-file))
841 (setq setup-contents (org-file-contents setup-file 'noerror))
842 (if (not ext-setup-or-nil)
843 (setq ext-setup-or-nil setup-contents start 0)
844 (setq ext-setup-or-nil
845 (concat (substring ext-setup-or-nil 0 start)
846 "\n" setup-contents "\n"
847 (substring ext-setup-or-nil start)))))))
848 (setq p (plist-put p :text text))
849 (when (and letbind (org-export-confirm-letbind))
850 (setq p (plist-put p :let-bind letbind)))
851 (when style (setq p (plist-put p :style-extra style)))
852 (when latex-header
853 (setq p (plist-put p :latex-header-extra (substring latex-header 1))))
854 (when latex-class
855 (setq p (plist-put p :latex-class latex-class)))
856 (when latex-class-options
857 (setq p (plist-put p :latex-class-options latex-class-options)))
858 (when options
859 (setq p (org-export-add-options-to-plist p options)))
860 (when mathjax
861 (setq p (plist-put p :mathjax mathjax)))
862 ;; Add macro definitions
863 (setq p (plist-put p :macro-date "(eval (format-time-string \"$1\"))"))
864 (setq p (plist-put p :macro-time "(eval (format-time-string \"$1\"))"))
865 (setq p (plist-put p :macro-property "(eval (org-entry-get nil \"$1\" 'selective))"))
866 (setq p (plist-put
867 p :macro-modification-time
868 (and (buffer-file-name)
869 (file-exists-p (buffer-file-name))
870 (concat
871 "(eval (format-time-string \"$1\" '"
872 (prin1-to-string (nth 5 (file-attributes
873 (buffer-file-name))))
874 "))"))))
875 (setq p (plist-put p :macro-input-file (and (buffer-file-name)
876 (file-name-nondirectory
877 (buffer-file-name)))))
878 (while (setq val (pop macros))
879 (when (string-match "^\\([-a-zA-Z0-9_]+\\)[ \t]+\\(.*?[ \t]*$\\)" val)
880 (setq p (plist-put
881 p (intern
882 (concat ":macro-" (downcase (match-string 1 val))))
883 (org-export-interpolate-newlines (match-string 2 val))))))
884 p))))
886 (defun org-export-interpolate-newlines (s)
887 (while (string-match "\\\\n" s)
888 (setq s (replace-match "\n" t t s)))
891 (defvar org-export-allow-BIND-local nil)
892 (defun org-export-confirm-letbind ()
893 "Can we use #+BIND values during export?
894 By default this will ask for confirmation by the user, to divert possible
895 security risks."
896 (cond
897 ((not org-export-allow-BIND) nil)
898 ((eq org-export-allow-BIND t) t)
899 ((local-variable-p 'org-export-allow-BIND-local (current-buffer))
900 org-export-allow-BIND-local)
901 (t (org-set-local 'org-export-allow-BIND-local
902 (yes-or-no-p "Allow BIND values in this buffer? ")))))
904 (defun org-install-letbind ()
905 "Install the values from #+BIND lines as local variables."
906 (let ((letbind (plist-get org-export-opt-plist :let-bind))
907 pair)
908 (while (setq pair (pop letbind))
909 (org-set-local (car pair) (nth 1 pair)))))
911 (defun org-export-add-options-to-plist (p options)
912 "Parse an OPTIONS line and set values in the property list P."
913 (let (o)
914 (when options
915 (let ((op org-export-plist-vars))
916 (while (setq o (pop op))
917 (if (and (nth 1 o)
918 (string-match (concat "\\(\\`\\|[ \t]\\)"
919 (regexp-quote (nth 1 o))
920 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
921 options))
922 (setq p (plist-put p (car o)
923 (car (read-from-string
924 (match-string 2 options))))))))))
927 (defun org-export-add-subtree-options (p pos)
928 "Add options in subtree at position POS to property list P."
929 (save-excursion
930 (goto-char pos)
931 (when (org-at-heading-p)
932 (let (a)
933 ;; This is actually read in `org-export-get-title-from-subtree'
934 ;; (when (setq a (org-entry-get pos "EXPORT_TITLE"))
935 ;; (setq p (plist-put p :title a)))
936 (when (setq a (org-entry-get pos "EXPORT_TEXT"))
937 (setq p (plist-put p :text a)))
938 (when (setq a (org-entry-get pos "EXPORT_AUTHOR"))
939 (setq p (plist-put p :author a)))
940 (when (setq a (org-entry-get pos "EXPORT_DATE"))
941 (setq p (plist-put p :date a)))
942 (when (setq a (org-entry-get pos "EXPORT_OPTIONS"))
943 (setq p (org-export-add-options-to-plist p a)))))
946 (defun org-export-directory (type plist)
947 (let* ((val (plist-get plist :publishing-directory))
948 (dir (if (listp val)
949 (or (cdr (assoc type val)) ".")
950 val)))
951 dir))
953 (defun org-export-process-option-filters (plist)
954 (let ((functions org-export-options-filters) f)
955 (while (setq f (pop functions))
956 (setq plist (funcall f plist))))
957 plist)
959 ;;;###autoload
960 (defun org-export (&optional arg)
961 "Export dispatcher for Org-mode.
962 When `org-export-run-in-background' is non-nil, try to run the command
963 in the background. This will be done only for commands that write
964 to a file. For details see the docstring of `org-export-run-in-background'.
966 The prefix argument ARG will be passed to the exporter. However, if
967 ARG is a double universal prefix \\[universal-argument] \\[universal-argument], \
968 that means to inverse the
969 value of `org-export-run-in-background'.
971 If `org-export-initial-scope' is set to 'subtree, try to export
972 the current subtree, otherwise try to export the whole buffer.
973 Pressing `1' will switch between these two options."
974 (interactive "P")
975 (let* ((bg (org-xor (equal arg '(16)) org-export-run-in-background))
976 (subtree-p (or (org-region-active-p)
977 (eq org-export-initial-scope 'subtree)))
978 (regb (and (org-region-active-p) (region-beginning)))
979 (rege (and (org-region-active-p) (region-end)))
980 (help "[t] insert the export option template
981 \[v] limit export to visible part of outline tree
982 \[1] switch buffer/subtree export
983 \[SPC] publish enclosing subtree (with LaTeX_CLASS or EXPORT_FILE_NAME prop)
985 \[a/n/u] export as ASCII/Latin-1/UTF-8 [A/N/U] to temporary buffer
987 \[h] export as HTML [H] to temporary buffer [R] export region
988 \[b] export as HTML and open in browser
990 \[l] export as LaTeX [L] to temporary buffer
991 \[p] export as LaTeX and process to PDF [d] ... and open PDF file
993 \[D] export as DocBook [V] export as DocBook, process to PDF, and open
995 \[o] export as OpenDocument Text [O] ... and open
997 \[j] export as TaskJuggler [J] ... and open
999 \[m] export as Freemind mind map
1000 \[x] export as XOXO
1001 \[g] export using Wes Hardaker's generic exporter
1003 \[i] export current file as iCalendar file
1004 \[I] export all agenda files as iCalendar files [c] ...as one combined file
1006 \[F] publish current file [P] publish current project
1007 \[X] publish a project... [E] publish every projects")
1008 (cmds
1009 '((?t org-insert-export-options-template nil)
1010 (?v org-export-visible nil)
1011 (?a org-export-as-ascii t)
1012 (?A org-export-as-ascii-to-buffer t)
1013 (?n org-export-as-latin1 t)
1014 (?N org-export-as-latin1-to-buffer t)
1015 (?u org-export-as-utf8 t)
1016 (?U org-export-as-utf8-to-buffer t)
1017 (?h org-export-as-html t)
1018 (?b org-export-as-html-and-open t)
1019 (?H org-export-as-html-to-buffer nil)
1020 (?R org-export-region-as-html nil)
1021 (?x org-export-as-xoxo t)
1022 (?g org-export-generic t)
1023 (?D org-export-as-docbook t)
1024 (?V org-export-as-docbook-pdf-and-open t)
1025 (?o org-export-as-odt t)
1026 (?O org-export-as-odt-and-open t)
1027 (?j org-export-as-taskjuggler t)
1028 (?J org-export-as-taskjuggler-and-open t)
1029 (?m org-export-as-freemind t)
1030 (?l org-export-as-latex t)
1031 (?p org-export-as-pdf t)
1032 (?d org-export-as-pdf-and-open t)
1033 (?L org-export-as-latex-to-buffer nil)
1034 (?i org-export-icalendar-this-file t)
1035 (?I org-export-icalendar-all-agenda-files t)
1036 (?c org-export-icalendar-combine-agenda-files t)
1037 (?F org-publish-current-file t)
1038 (?P org-publish-current-project t)
1039 (?X org-publish t)
1040 (?E org-publish-all t)))
1041 r1 r2 ass
1042 (cpos (point)) (cbuf (current-buffer)) bpos)
1043 (save-excursion
1044 (save-window-excursion
1045 (if subtree-p
1046 (message "Export subtree: ")
1047 (message "Export buffer: "))
1048 (delete-other-windows)
1049 (with-output-to-temp-buffer "*Org Export/Publishing Help*"
1050 (princ help))
1051 (org-fit-window-to-buffer (get-buffer-window
1052 "*Org Export/Publishing Help*"))
1053 (while (eq (setq r1 (read-char-exclusive)) ?1)
1054 (cond (subtree-p
1055 (setq subtree-p nil)
1056 (message "Export buffer: "))
1057 ((not subtree-p)
1058 (setq subtree-p t)
1059 (setq bpos (point))
1060 (org-mark-subtree)
1061 (org-activate-mark)
1062 (setq regb (and (org-region-active-p) (region-beginning)))
1063 (setq rege (and (org-region-active-p) (region-end)))
1064 (message "Export subtree: "))))
1065 (when (eq r1 ?\ )
1066 (let ((case-fold-search t)
1067 (end (save-excursion (while (org-up-heading-safe)) (point))))
1068 (outline-next-heading)
1069 (if (re-search-backward
1070 "^[ \t]+\\(:latex_class:\\|:export_title:\\|:export_file_name:\\)[ \t]+\\S-"
1071 end t)
1072 (progn
1073 (org-back-to-heading t)
1074 (setq subtree-p t)
1075 (setq bpos (point))
1076 (message "Select command (for subtree): ")
1077 (setq r1 (read-char-exclusive)))
1078 (error "No enclosing node with LaTeX_CLASS or EXPORT_TITLE or EXPORT_FILE_NAME")
1079 )))))
1080 (if (fboundp 'redisplay) (redisplay)) ;; XEmacs does not have/need (redisplay)
1081 (and bpos (goto-char bpos))
1082 (setq r2 (if (< r1 27) (+ r1 96) r1))
1083 (unless (setq ass (assq r2 cmds))
1084 (error "No command associated with key %c" r1))
1085 (if (and bg (nth 2 ass)
1086 (not (buffer-base-buffer))
1087 (not (org-region-active-p)))
1088 ;; execute in background
1089 (let ((p (start-process
1090 (concat "Exporting " (file-name-nondirectory (buffer-file-name)))
1091 "*Org Processes*"
1092 (expand-file-name invocation-name invocation-directory)
1093 "-batch"
1094 "-l" user-init-file
1095 "--eval" "(require 'org-exp)"
1096 "--eval" "(setq org-wait .2)"
1097 (buffer-file-name)
1098 "-f" (symbol-name (nth 1 ass)))))
1099 (set-process-sentinel p 'org-export-process-sentinel)
1100 (message "Background process \"%s\": started" p))
1101 ;; set the mark correctly when exporting a subtree
1102 (if subtree-p (let (deactivate-mark) (push-mark rege t t) (goto-char regb)))
1104 (call-interactively (nth 1 ass))
1105 (when (and bpos (get-buffer-window cbuf))
1106 (let ((cw (selected-window)))
1107 (select-window (get-buffer-window cbuf))
1108 (goto-char cpos)
1109 (deactivate-mark)
1110 (select-window cw))))))
1112 (defun org-export-process-sentinel (process status)
1113 (if (string-match "\n+\\'" status)
1114 (setq status (substring status 0 -1)))
1115 (message "Background process \"%s\": %s" process status))
1117 ;;; General functions for all backends
1119 (defvar org-export-target-aliases nil
1120 "Alist of targets with invisible aliases.")
1121 (defvar org-export-preferred-target-alist nil
1122 "Alist of section id's with preferred aliases.")
1123 (defvar org-export-id-target-alist nil
1124 "Alist of section id's with preferred aliases.")
1125 (defvar org-export-code-refs nil
1126 "Alist of code references and line numbers.")
1128 (defun org-export-preprocess-string (string &rest parameters)
1129 "Cleanup STRING so that the true exported has a more consistent source.
1130 This function takes STRING, which should be a buffer-string of an org-file
1131 to export. It then creates a temporary buffer where it does its job.
1132 The result is then again returned as a string, and the exporter works
1133 on this string to produce the exported version."
1134 (interactive)
1135 (let* ((org-export-current-backend (or (plist-get parameters :for-backend)
1136 org-export-current-backend))
1137 (archived-trees (plist-get parameters :archived-trees))
1138 (inhibit-read-only t)
1139 (drawers org-drawers)
1140 (source-buffer (current-buffer))
1141 target-alist rtn)
1143 (setq org-export-target-aliases nil
1144 org-export-preferred-target-alist nil
1145 org-export-id-target-alist nil
1146 org-export-code-refs nil)
1148 (with-temp-buffer
1149 (erase-buffer)
1150 (insert string)
1151 (setq case-fold-search t)
1153 (let ((inhibit-read-only t))
1154 (remove-text-properties (point-min) (point-max)
1155 '(read-only t)))
1157 ;; Remove license-to-kill stuff
1158 ;; The caller marks some stuff for killing, stuff that has been
1159 ;; used to create the page title, for example.
1160 (org-export-kill-licensed-text)
1162 (let ((org-inhibit-startup t)) (org-mode))
1163 (setq case-fold-search t)
1164 (org-clone-local-variables source-buffer "^\\(org-\\|orgtbl-\\)")
1165 (org-install-letbind)
1167 ;; Call the hook
1168 (run-hooks 'org-export-preprocess-hook)
1170 (untabify (point-min) (point-max))
1172 ;; Handle include files, and call a hook
1173 (org-export-handle-include-files-recurse)
1174 (run-hooks 'org-export-preprocess-after-include-files-hook)
1176 ;; Get rid of archived trees
1177 (org-export-remove-archived-trees archived-trees)
1179 ;; Remove comment environment and comment subtrees
1180 (org-export-remove-comment-blocks-and-subtrees)
1182 ;; Get rid of excluded trees, and call a hook
1183 (org-export-handle-export-tags (plist-get parameters :select-tags)
1184 (plist-get parameters :exclude-tags))
1185 (run-hooks 'org-export-preprocess-after-tree-selection-hook)
1187 ;; Get rid of tasks, depending on configuration
1188 (org-export-remove-tasks (plist-get parameters :tasks))
1190 ;; Prepare footnotes for export. During that process, footnotes
1191 ;; actually included in the exported part of the buffer go
1192 ;; though some transformations:
1194 ;; 1. They have their label normalized (like "[N]");
1196 ;; 2. They get moved at the same place in the buffer (usually at
1197 ;; its end, but backends may define another place via
1198 ;; `org-footnote-insert-pos-for-preprocessor');
1200 ;; 3. The are stored in `org-export-footnotes-seen', while
1201 ;; `org-export-preprocess-string' is applied to their
1202 ;; definition.
1204 ;; Line-wise exporters ignore `org-export-footnotes-seen', as
1205 ;; they interpret footnotes at the moment they see them in the
1206 ;; buffer. Context-wise exporters grab all the info needed in
1207 ;; that variable and delete moved definitions (as described in
1208 ;; 2nd step).
1209 (when (plist-get parameters :footnotes)
1210 (org-footnote-normalize nil parameters))
1212 ;; Change lists ending. Other parts of export may insert blank
1213 ;; lines and lists' structure could be altered.
1214 (org-export-mark-list-end)
1216 ;; Process the macros
1217 (org-export-preprocess-apply-macros)
1218 (run-hooks 'org-export-preprocess-after-macros-hook)
1220 ;; Export code blocks
1221 (org-export-blocks-preprocess)
1223 ;; Mark lists with properties
1224 (org-export-mark-list-properties)
1226 ;; Handle source code snippets
1227 (org-export-replace-src-segments-and-examples)
1229 ;; Protect short examples marked by a leading colon
1230 (org-export-protect-colon-examples)
1232 ;; Protected spaces
1233 (org-export-convert-protected-spaces)
1235 ;; Find all headings and compute the targets for them
1236 (setq target-alist (org-export-define-heading-targets target-alist))
1238 (run-hooks 'org-export-preprocess-after-headline-targets-hook)
1240 ;; Find HTML special classes for headlines
1241 (org-export-remember-html-container-classes)
1243 ;; Get rid of drawers
1244 (org-export-remove-or-extract-drawers
1245 drawers (plist-get parameters :drawers))
1247 ;; Get the correct stuff before the first headline
1248 (when (plist-get parameters :skip-before-1st-heading)
1249 (goto-char (point-min))
1250 (when (re-search-forward "^\\(#.*\n\\)?\\*+[ \t]" nil t)
1251 (delete-region (point-min) (match-beginning 0))
1252 (goto-char (point-min))
1253 (insert "\n")))
1254 (when (plist-get parameters :add-text)
1255 (goto-char (point-min))
1256 (insert (plist-get parameters :add-text) "\n"))
1258 ;; Remove todo-keywords before exporting, if the user has requested so
1259 (org-export-remove-headline-metadata parameters)
1261 ;; Find targets in comments and move them out of comments,
1262 ;; but mark them as targets that should be invisible
1263 (setq target-alist (org-export-handle-invisible-targets target-alist))
1265 ;; Select and protect backend specific stuff, throw away stuff
1266 ;; that is specific for other backends
1267 (run-hooks 'org-export-preprocess-before-selecting-backend-code-hook)
1268 (org-export-select-backend-specific-text)
1270 ;; Protect quoted subtrees
1271 (org-export-protect-quoted-subtrees)
1273 ;; Remove clock lines
1274 (org-export-remove-clock-lines)
1276 ;; Protect verbatim elements
1277 (org-export-protect-verbatim)
1279 ;; Blockquotes, verse, and center
1280 (org-export-mark-blockquote-verse-center)
1281 (run-hooks 'org-export-preprocess-after-blockquote-hook)
1283 ;; Remove timestamps, if the user has requested so
1284 (unless (plist-get parameters :timestamps)
1285 (org-export-remove-timestamps))
1287 ;; Attach captions to the correct object
1288 (setq target-alist (org-export-attach-captions-and-attributes target-alist))
1290 ;; Find matches for radio targets and turn them into internal links
1291 (org-export-mark-radio-links)
1292 (run-hooks 'org-export-preprocess-after-radio-targets-hook)
1294 ;; Find all links that contain a newline and put them into a single line
1295 (org-export-concatenate-multiline-links)
1297 ;; Normalize links: Convert angle and plain links into bracket links
1298 ;; and expand link abbreviations
1299 (run-hooks 'org-export-preprocess-before-normalizing-links-hook)
1300 (org-export-normalize-links)
1302 ;; Find all internal links. If they have a fuzzy match (i.e. not
1303 ;; a *dedicated* target match, let the link point to the
1304 ;; corresponding section.
1305 (org-export-target-internal-links target-alist)
1307 ;; Find multiline emphasis and put them into single line
1308 (when (plist-get parameters :emph-multiline)
1309 (org-export-concatenate-multiline-emphasis))
1311 ;; Remove special table lines, and store alignment information
1312 (org-store-forced-table-alignment)
1313 (when org-export-table-remove-special-lines
1314 (org-export-remove-special-table-lines))
1316 ;; Another hook
1317 (run-hooks 'org-export-preprocess-before-backend-specifics-hook)
1319 ;; Backend-specific preprocessing
1320 (let* ((backend-name (symbol-name org-export-current-backend))
1321 (f (intern (format "org-export-%s-preprocess" backend-name))))
1322 (require (intern (concat "org-" backend-name)) nil)
1323 (funcall f parameters))
1325 ;; Remove or replace comments
1326 (org-export-handle-comments (plist-get parameters :comments))
1328 ;; Remove #+TBLFM #+TBLNAME #+NAME #+RESULTS lines
1329 (org-export-handle-metalines)
1331 ;; Run the final hook
1332 (run-hooks 'org-export-preprocess-final-hook)
1334 (setq rtn (buffer-string)))
1335 rtn))
1337 (defun org-export-kill-licensed-text ()
1338 "Remove all text that is marked with a :org-license-to-kill property."
1339 (let (p)
1340 (while (setq p (text-property-any (point-min) (point-max)
1341 :org-license-to-kill t))
1342 (delete-region
1343 p (or (next-single-property-change p :org-license-to-kill)
1344 (point-max))))))
1346 (defvar org-export-define-heading-targets-headline-hook nil
1347 "Hook that is run when a headline was matched during target search.
1348 This is part of the preprocessing for export.")
1350 (defun org-export-define-heading-targets (target-alist)
1351 "Find all headings and define the targets for them.
1352 The new targets are added to TARGET-ALIST, which is also returned.
1353 Also find all ID and CUSTOM_ID properties and store them."
1354 (goto-char (point-min))
1355 (org-init-section-numbers)
1356 (let ((re (concat "^" org-outline-regexp
1357 "\\|"
1358 "^[ \t]*:\\(ID\\|CUSTOM_ID\\):[ \t]*\\([^ \t\r\n]+\\)"))
1359 level target last-section-target a id)
1360 (while (re-search-forward re nil t)
1361 (org-if-unprotected-at (match-beginning 0)
1362 (if (match-end 2)
1363 (progn
1364 (setq id (org-match-string-no-properties 2))
1365 (push (cons id target) target-alist)
1366 (setq a (or (assoc last-section-target org-export-target-aliases)
1367 (progn
1368 (push (list last-section-target)
1369 org-export-target-aliases)
1370 (car org-export-target-aliases))))
1371 (push (caar target-alist) (cdr a))
1372 (when (equal (match-string 1) "CUSTOM_ID")
1373 (if (not (assoc last-section-target
1374 org-export-preferred-target-alist))
1375 (push (cons last-section-target id)
1376 org-export-preferred-target-alist)))
1377 (when (equal (match-string 1) "ID")
1378 (if (not (assoc last-section-target
1379 org-export-id-target-alist))
1380 (push (cons last-section-target (concat "ID-" id))
1381 org-export-id-target-alist))))
1382 (setq level (org-reduced-level
1383 (save-excursion (goto-char (point-at-bol))
1384 (org-outline-level))))
1385 (setq target (org-solidify-link-text
1386 (format "sec-%s" (replace-regexp-in-string
1387 "\\." "-"
1388 (org-section-number level)))))
1389 (setq last-section-target target)
1390 (push (cons target target) target-alist)
1391 (add-text-properties
1392 (point-at-bol) (point-at-eol)
1393 (list 'target target))
1394 (run-hooks 'org-export-define-heading-targets-headline-hook)))))
1395 target-alist)
1397 (defun org-export-handle-invisible-targets (target-alist)
1398 "Find targets in comments and move them out of comments.
1399 Mark them as invisible targets."
1400 (let (target tmp a)
1401 (goto-char (point-min))
1402 (while (re-search-forward "^#.*?\\(<<<?\\([^>\r\n]+\\)>>>?\\).*" nil t)
1403 ;; Check if the line before or after is a headline with a target
1404 (if (setq target (or (get-text-property (point-at-bol 0) 'target)
1405 (get-text-property (point-at-bol 2) 'target)))
1406 (progn
1407 ;; use the existing target in a neighboring line
1408 (setq tmp (match-string 2))
1409 (replace-match "")
1410 (and (looking-at "\n") (delete-char 1))
1411 (push (cons (setq tmp (org-solidify-link-text tmp)) target)
1412 target-alist)
1413 (setq a (or (assoc target org-export-target-aliases)
1414 (progn
1415 (push (list target) org-export-target-aliases)
1416 (car org-export-target-aliases))))
1417 (push tmp (cdr a)))
1418 ;; Make an invisible target
1419 (replace-match "\\1(INVISIBLE)"))))
1420 target-alist)
1422 (defun org-export-target-internal-links (target-alist)
1423 "Find all internal links and assign targets to them.
1424 If a link has a fuzzy match (i.e. not a *dedicated* target match),
1425 let the link point to the corresponding section.
1426 This function also handles the id links, if they have a match in
1427 the current file."
1428 (goto-char (point-min))
1429 (while (re-search-forward org-bracket-link-regexp nil t)
1430 (org-if-unprotected-at (1+ (match-beginning 0))
1431 (let* ((org-link-search-must-match-exact-headline t)
1432 (md (match-data))
1433 (desc (match-end 2))
1434 (link (org-link-unescape (match-string 1)))
1435 (slink (org-solidify-link-text link))
1436 found props pos cref
1437 (target
1438 (cond
1439 ((= (string-to-char link) ?#)
1440 ;; user wants exactly this link
1441 link)
1442 ((cdr (assoc slink target-alist))
1443 (or (cdr (assoc (assoc slink target-alist)
1444 org-export-preferred-target-alist))
1445 (cdr (assoc slink target-alist))))
1446 ((and (string-match "^id:" link)
1447 (cdr (assoc (substring link 3) target-alist))))
1448 ((string-match "^(\\(.*\\))$" link)
1449 (setq cref (match-string 1 link))
1450 (concat "coderef:" cref))
1451 ((string-match org-link-types-re link) nil)
1452 ((or (file-name-absolute-p link)
1453 (string-match "^\\." link))
1454 nil)
1456 (let ((org-link-search-inhibit-query t))
1457 (save-excursion
1458 (setq found (condition-case nil (org-link-search link)
1459 (error nil)))
1460 (when (and found
1461 (or (org-at-heading-p)
1462 (not (eq found 'dedicated))))
1463 (or (get-text-property (point) 'target)
1464 (get-text-property
1465 (max (point-min)
1466 (1- (or (previous-single-property-change
1467 (point) 'target) 0)))
1468 'target)))))))))
1469 (when target
1470 (set-match-data md)
1471 (goto-char (match-beginning 1))
1472 (setq props (text-properties-at (point)))
1473 (delete-region (match-beginning 1) (match-end 1))
1474 (setq pos (point))
1475 (insert target)
1476 (unless desc (insert "][" link))
1477 (add-text-properties pos (point) props))))))
1479 (defun org-export-remember-html-container-classes ()
1480 "Store the HTML_CONTAINER_CLASS properties in a text property."
1481 (goto-char (point-min))
1482 (let (class)
1483 (while (re-search-forward
1484 "^[ \t]*:HTML_CONTAINER_CLASS:[ \t]+\\(.+\\)$" nil t)
1485 (setq class (match-string 1))
1486 (save-excursion
1487 (when (re-search-backward "^\\*" (point-min) t)
1488 (org-back-to-heading t)
1489 (put-text-property (point-at-bol) (point-at-eol)
1490 'html-container-class class))))))
1492 (defvar org-export-format-drawer-function nil
1493 "Function to be called to format the contents of a drawer.
1494 The function must accept two parameters:
1495 NAME the drawer name, like \"PROPERTIES\"
1496 CONTENT the content of the drawer.
1497 You can check the export backend through `org-export-current-backend'.
1498 The function should return the text to be inserted into the buffer.
1499 If this is nil, `org-export-format-drawer' is used as a default.")
1501 (defun org-export-remove-or-extract-drawers (all-drawers exp-drawers)
1502 "Remove drawers, or extract and format the content.
1503 ALL-DRAWERS is a list of all drawer names valid in the current buffer.
1504 EXP-DRAWERS can be t to keep all drawer contents, or a list of drawers
1505 whose content to keep. Any drawers that are in ALL-DRAWERS but not in
1506 EXP-DRAWERS will be removed."
1507 (goto-char (point-min))
1508 (let ((re (concat "^[ \t]*:\\("
1509 (mapconcat 'identity all-drawers "\\|")
1510 "\\):[ \t]*$"))
1511 name beg beg-content eol content)
1512 (while (re-search-forward re nil t)
1513 (org-if-unprotected
1514 (setq name (match-string 1))
1515 (setq beg (match-beginning 0)
1516 beg-content (1+ (point-at-eol))
1517 eol (point-at-eol))
1518 (if (not (and (re-search-forward
1519 "^\\([ \t]*:END:[ \t]*\n?\\)\\|^\\*+[ \t]" nil t)
1520 (match-end 1)))
1521 (goto-char eol)
1522 (goto-char (match-beginning 0))
1523 (and (looking-at ".*\n?") (replace-match ""))
1524 (setq content (buffer-substring beg-content (point)))
1525 (delete-region beg (point))
1526 (when (or (eq exp-drawers t)
1527 (member name exp-drawers))
1528 (setq content (funcall (or org-export-format-drawer-function
1529 'org-export-format-drawer)
1530 name content))
1531 (insert content)))))))
1533 (defun org-export-format-drawer (name content)
1534 "Format the content of a drawer as a colon example."
1535 (if (string-match "[ \t]+\\'" content)
1536 (setq content (substring content (match-beginning 0))))
1537 (while (string-match "\\`[ \t]*\n" content)
1538 (setq content (substring content (match-end 0))))
1539 (setq content (org-remove-indentation content))
1540 (setq content (concat ": " (mapconcat 'identity
1541 (org-split-string content "\n")
1542 "\n: ")
1543 "\n"))
1544 (setq content (concat " : " (upcase name) "\n" content))
1545 (org-add-props content nil 'org-protected t))
1547 (defun org-export-handle-export-tags (select-tags exclude-tags)
1548 "Modify the buffer, honoring SELECT-TAGS and EXCLUDE-TAGS.
1549 Both arguments are lists of tags.
1550 If any of SELECT-TAGS is found, all trees not marked by a SELECT-TAG
1551 will be removed.
1552 After that, all subtrees that are marked by EXCLUDE-TAGS will be
1553 removed as well."
1554 (remove-text-properties (point-min) (point-max) '(:org-delete t))
1555 (let* ((re-sel (concat ":\\(" (mapconcat 'regexp-quote
1556 select-tags "\\|")
1557 "\\):"))
1558 (re-excl (concat ":\\(" (mapconcat 'regexp-quote
1559 exclude-tags "\\|")
1560 "\\):"))
1561 beg end cont)
1562 (goto-char (point-min))
1563 (when (and select-tags
1564 (re-search-forward
1565 (concat "^\\*+[ \t].*" re-sel "[^ \t\n]*[ \t]*$") nil t))
1566 ;; At least one tree is marked for export, this means
1567 ;; all the unmarked stuff needs to go.
1568 ;; Dig out the trees that should be exported
1569 (goto-char (point-min))
1570 (outline-next-heading)
1571 (setq beg (point))
1572 (put-text-property beg (point-max) :org-delete t)
1573 (while (re-search-forward re-sel nil t)
1574 (when (org-at-heading-p)
1575 (org-back-to-heading)
1576 (remove-text-properties
1577 (max (1- (point)) (point-min))
1578 (setq cont (save-excursion (org-end-of-subtree t t)))
1579 '(:org-delete t))
1580 (while (and (org-up-heading-safe)
1581 (get-text-property (point) :org-delete))
1582 (remove-text-properties (max (1- (point)) (point-min))
1583 (point-at-eol) '(:org-delete t)))
1584 (goto-char cont))))
1585 ;; Remove the trees explicitly marked for noexport
1586 (when exclude-tags
1587 (goto-char (point-min))
1588 (while (re-search-forward re-excl nil t)
1589 (when (org-at-heading-p)
1590 (org-back-to-heading t)
1591 (setq beg (point))
1592 (org-end-of-subtree t t)
1593 (delete-region beg (point))
1594 (when (featurep 'org-inlinetask)
1595 (org-inlinetask-remove-END-maybe)))))
1596 ;; Remove everything that is now still marked for deletion
1597 (goto-char (point-min))
1598 (while (setq beg (text-property-any (point-min) (point-max) :org-delete t))
1599 (setq end (or (next-single-property-change beg :org-delete)
1600 (point-max)))
1601 (delete-region beg end))))
1603 (defun org-export-remove-tasks (keep)
1604 "Remove tasks depending on configuration.
1605 When KEEP is nil, remove all tasks.
1606 When KEEP is `todo', remove the tasks that are DONE.
1607 When KEEP is `done', remove the tasks that are not yet done.
1608 When it is a list of strings, keep only tasks with these TODO keywords."
1609 (when (or (listp keep) (memq keep '(todo done nil)))
1610 (let ((re (concat "^\\*+[ \t]+\\("
1611 (mapconcat
1612 'regexp-quote
1613 (cond ((not keep) org-todo-keywords-1)
1614 ((eq keep 'todo) org-done-keywords)
1615 ((eq keep 'done) org-not-done-keywords)
1616 ((listp keep)
1617 (org-delete-all keep (copy-sequence
1618 org-todo-keywords-1))))
1619 "\\|")
1620 "\\)\\($\\|[ \t]\\)"))
1621 (case-fold-search nil)
1622 beg)
1623 (goto-char (point-min))
1624 (while (re-search-forward re nil t)
1625 (org-if-unprotected
1626 (setq beg (match-beginning 0))
1627 (org-end-of-subtree t t)
1628 (if (looking-at "^\\*+[ \t]+END[ \t]*$")
1629 ;; Kill the END line of the inline task
1630 (goto-char (min (point-max) (1+ (match-end 0)))))
1631 (delete-region beg (point)))))))
1633 (defun org-export-remove-archived-trees (export-archived-trees)
1634 "Remove archived trees.
1635 When EXPORT-ARCHIVED-TREES is `headline;, only the headline will be exported.
1636 When it is t, the entire archived tree will be exported.
1637 When it is nil the entire tree including the headline will be removed
1638 from the buffer."
1639 (let ((re-archive (concat ":" org-archive-tag ":"))
1640 a b)
1641 (when (not (eq export-archived-trees t))
1642 (goto-char (point-min))
1643 (while (re-search-forward re-archive nil t)
1644 (if (not (org-at-heading-p t))
1645 (goto-char (point-at-eol))
1646 (beginning-of-line 1)
1647 (setq a (if export-archived-trees
1648 (1+ (point-at-eol)) (point))
1649 b (org-end-of-subtree t))
1650 (if (> b a) (delete-region a b)))))))
1652 (defun org-export-remove-headline-metadata (opts)
1653 "Remove meta data from the headline, according to user options."
1654 (let ((re org-complex-heading-regexp)
1655 (todo (plist-get opts :todo-keywords))
1656 (tags (plist-get opts :tags))
1657 (pri (plist-get opts :priority))
1658 (elts '(1 2 3 4 5))
1659 (case-fold-search nil)
1660 rpl)
1661 (setq elts (delq nil (list 1 (if todo 2) (if pri 3) 4 (if tags 5))))
1662 (when (or (not todo) (not tags) (not pri))
1663 (goto-char (point-min))
1664 (while (re-search-forward re nil t)
1665 (org-if-unprotected
1666 (setq rpl (mapconcat (lambda (i) (if (match-end i) (match-string i) ""))
1667 elts " "))
1668 (replace-match rpl t t))))))
1670 (defun org-export-remove-timestamps ()
1671 "Remove timestamps and keywords for export."
1672 (goto-char (point-min))
1673 (while (re-search-forward org-maybe-keyword-time-regexp nil t)
1674 (backward-char 1)
1675 (org-if-unprotected
1676 (unless (save-match-data (org-at-table-p))
1677 (replace-match "")
1678 (beginning-of-line 1)
1679 (if (looking-at "[- \t]*\\(=>[- \t0-9:]*\\)?[ \t]*\n")
1680 (replace-match ""))))))
1682 (defun org-export-remove-clock-lines ()
1683 "Remove clock lines for export."
1684 (goto-char (point-min))
1685 (let ((re (concat "^[ \t]*" org-clock-string ".*\n?")))
1686 (while (re-search-forward re nil t)
1687 (org-if-unprotected
1688 (replace-match "")))))
1690 (defvar org-heading-keyword-regexp-format) ; defined in org.el
1691 (defun org-export-protect-quoted-subtrees ()
1692 "Mark quoted subtrees with the protection property."
1693 (let ((org-re-quote (format org-heading-keyword-regexp-format
1694 org-quote-string)))
1695 (goto-char (point-min))
1696 (while (re-search-forward org-re-quote nil t)
1697 (goto-char (match-beginning 0))
1698 (end-of-line 1)
1699 (add-text-properties (point) (org-end-of-subtree t)
1700 '(org-protected t)))))
1702 (defun org-export-convert-protected-spaces ()
1703 "Convert strings like \\____ to protected spaces in all backends."
1704 (goto-char (point-min))
1705 (while (re-search-forward "\\\\__+" nil t)
1706 (org-if-unprotected-1
1707 (replace-match
1708 (org-add-props
1709 (cond
1710 ((eq org-export-current-backend 'latex)
1711 (format "\\hspace{%dex}" (- (match-end 0) (match-beginning 0))))
1712 ((eq org-export-current-backend 'html)
1713 (org-add-props (match-string 0) nil
1714 'org-whitespace (- (match-end 0) (match-beginning 0))))
1715 ;; ((eq org-export-current-backend 'docbook))
1716 ((eq org-export-current-backend 'ascii)
1717 (org-add-props (match-string 0) '(org-whitespace t)))
1718 (t (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
1719 '(org-protected t))
1720 t t))))
1722 (defun org-export-protect-verbatim ()
1723 "Mark verbatim snippets with the protection property."
1724 (goto-char (point-min))
1725 (while (re-search-forward org-verbatim-re nil t)
1726 (org-if-unprotected
1727 (add-text-properties (match-beginning 4) (match-end 4)
1728 '(org-protected t org-verbatim-emph t))
1729 (goto-char (1+ (match-end 4))))))
1731 (defun org-export-protect-colon-examples ()
1732 "Protect lines starting with a colon."
1733 (goto-char (point-min))
1734 (let ((re "^[ \t]*:\\([ \t]\\|$\\)") beg)
1735 (while (re-search-forward re nil t)
1736 (beginning-of-line 1)
1737 (setq beg (point))
1738 (while (looking-at re)
1739 (end-of-line 1)
1740 (or (eobp) (forward-char 1)))
1741 (add-text-properties beg (if (bolp) (1- (point)) (point))
1742 '(org-protected t)))))
1744 (defvar org-export-backends
1745 '(docbook html beamer ascii latex)
1746 "List of Org supported export backends.")
1748 (defun org-export-select-backend-specific-text ()
1749 (let ((formatters org-export-backends)
1750 (case-fold-search t)
1751 backend backend-name beg beg-content end end-content ind)
1753 (while formatters
1754 (setq backend (pop formatters)
1755 backend-name (symbol-name backend))
1757 ;; Handle #+BACKEND: stuff
1758 (goto-char (point-min))
1759 (while (re-search-forward (concat "^\\([ \t]*\\)#\\+" backend-name
1760 ":[ \t]*\\(.*\\)") nil t)
1761 (if (not (eq backend org-export-current-backend))
1762 (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
1763 (let ((ind (get-text-property (point-at-bol) 'original-indentation)))
1764 (replace-match "\\1\\2" t)
1765 (add-text-properties
1766 (point-at-bol) (min (1+ (point-at-eol)) (point-max))
1767 `(org-protected t original-indentation ,ind org-native-text t)))))
1768 ;; Delete #+ATTR_BACKEND: stuff of another backend. Those
1769 ;; matching the current backend will be taken care of by
1770 ;; `org-export-attach-captions-and-attributes'
1771 (goto-char (point-min))
1772 (while (re-search-forward (concat "^\\([ \t]*\\)#\\+ATTR_" backend-name
1773 ":[ \t]*\\(.*\\)") nil t)
1774 (setq ind (org-get-indentation))
1775 (when (not (eq backend org-export-current-backend))
1776 (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))))
1777 ;; Handle #+BEGIN_BACKEND and #+END_BACKEND stuff
1778 (goto-char (point-min))
1779 (while (re-search-forward (concat "^[ \t]*#\\+BEGIN_" backend-name "\\>.*\n?")
1780 nil t)
1781 (setq beg (match-beginning 0) beg-content (match-end 0))
1782 (setq ind (or (get-text-property beg 'original-indentation)
1783 (save-excursion (goto-char beg) (org-get-indentation))))
1784 (when (re-search-forward (concat "^[ \t]*#\\+END_" backend-name "\\>.*\n?")
1785 nil t)
1786 (setq end (match-end 0) end-content (match-beginning 0))
1787 (if (eq backend org-export-current-backend)
1788 ;; yes, keep this
1789 (progn
1790 (add-text-properties
1791 beg-content end-content
1792 `(org-protected t original-indentation ,ind org-native-text t))
1793 ;; strip protective commas
1794 (org-unescape-code-in-region beg-content end-content)
1795 (delete-region (match-beginning 0) (match-end 0))
1796 (save-excursion
1797 (goto-char beg)
1798 (delete-region (point) (1+ (point-at-eol)))))
1799 ;; No, this is for a different backend, kill it
1800 (delete-region beg end)))))))
1802 (defun org-export-mark-blockquote-verse-center ()
1803 "Mark block quote and verse environments with special cookies.
1804 These special cookies will later be interpreted by the backend."
1805 ;; Blockquotes
1806 (let (type t1 ind beg end beg1 end1 content)
1807 (goto-char (point-min))
1808 (while (re-search-forward
1809 "^\\([ \t]*\\)#\\+\\(begin_\\(\\(block\\)?quote\\|verse\\|center\\)\\>.*\\)"
1810 nil t)
1811 (setq ind (length (match-string 1))
1812 type (downcase (match-string 3))
1813 t1 (if (equal type "quote") "blockquote" type))
1814 (setq beg (match-beginning 0)
1815 beg1 (1+ (match-end 0)))
1816 (when (re-search-forward (concat "^[ \t]*#\\+end_" type "\\>.*") nil t)
1817 (setq end1 (1- (match-beginning 0))
1818 end (+ (point-at-eol) (if (looking-at "\n$") 1 0)))
1819 (setq content (org-remove-indentation (buffer-substring beg1 end1)))
1820 (setq content (concat "ORG-" (upcase t1) "-START\n"
1821 content "\n"
1822 "ORG-" (upcase t1) "-END\n"))
1823 (delete-region beg end)
1824 (insert (org-add-props content nil 'original-indentation ind))))))
1826 (defun org-export-mark-list-end ()
1827 "Mark all list endings with a special string."
1828 (unless (eq org-export-current-backend 'ascii)
1829 (mapc
1830 (lambda (e)
1831 ;; For each type allowing list export, find every list, remove
1832 ;; ending regexp if needed, and insert org-list-end.
1833 (goto-char (point-min))
1834 (while (re-search-forward (org-item-beginning-re) nil t)
1835 (when (eq (nth 2 (org-list-context)) e)
1836 (let* ((struct (org-list-struct))
1837 (bottom (org-list-get-bottom-point struct))
1838 (top (point-at-bol))
1839 (top-ind (org-list-get-ind top struct)))
1840 (goto-char bottom)
1841 (when (and (not (looking-at "[ \t]*$"))
1842 (looking-at org-list-end-re))
1843 (replace-match ""))
1844 (unless (bolp) (insert "\n"))
1845 ;; As org-list-end is inserted at column 0, it would end
1846 ;; by indentation any list. It can be problematic when
1847 ;; there are lists within lists: the inner list end would
1848 ;; also become the outer list end. To avoid this, text
1849 ;; property `original-indentation' is added, as
1850 ;; `org-list-struct' pays attention to it when reading a
1851 ;; list.
1852 (insert (org-add-props
1853 "ORG-LIST-END-MARKER\n"
1854 (list 'original-indentation top-ind)))))))
1855 (cons nil org-list-export-context))))
1857 (defun org-export-mark-list-properties ()
1858 "Mark list with special properties.
1859 These special properties will later be interpreted by the backend."
1860 (let ((mark-list
1861 (function
1862 ;; Mark a list with 3 properties: `list-item' which is
1863 ;; position at beginning of line, `list-struct' which is
1864 ;; list structure, and `list-prevs' which is the alist of
1865 ;; item and its predecessor. Leave point at list ending.
1866 (lambda (ctxt)
1867 (let* ((struct (org-list-struct))
1868 (top (org-list-get-top-point struct))
1869 (bottom (org-list-get-bottom-point struct))
1870 (prevs (org-list-prevs-alist struct))
1871 poi)
1872 ;; Get every item and ending position, without dups and
1873 ;; without bottom point of list.
1874 (mapc (lambda (e)
1875 (let ((pos (car e))
1876 (end (nth 6 e)))
1877 (unless (memq pos poi)
1878 (push pos poi))
1879 (unless (or (= end bottom) (memq end poi))
1880 (push end poi))))
1881 struct)
1882 (setq poi (sort poi '<))
1883 ;; For every point of interest, mark the whole line with
1884 ;; its position in list.
1885 (mapc
1886 (lambda (e)
1887 (goto-char e)
1888 (add-text-properties (point-at-bol) (point-at-eol)
1889 (list 'list-item (point-at-bol)
1890 'list-struct struct
1891 'list-prevs prevs)))
1892 poi)
1893 ;; Take care of bottom point. As babel may have inserted
1894 ;; a new list in buffer, list ending isn't always
1895 ;; marked. Now mark every list ending and add properties
1896 ;; useful to line processing exporters.
1897 (goto-char bottom)
1898 (when (or (looking-at "^ORG-LIST-END-MARKER\n")
1899 (and (not (looking-at "[ \t]*$"))
1900 (looking-at org-list-end-re)))
1901 (replace-match ""))
1902 (unless (bolp) (insert "\n"))
1903 (insert
1904 (org-add-props "ORG-LIST-END-MARKER\n" (list 'list-item bottom
1905 'list-struct struct
1906 'list-prevs prevs)))
1907 ;; Following property is used by LaTeX exporter.
1908 (add-text-properties top (point) (list 'list-context ctxt)))))))
1909 ;; Mark lists except for backends not interpreting them.
1910 (unless (eq org-export-current-backend 'ascii)
1911 (let ((org-list-end-re "^ORG-LIST-END-MARKER\n"))
1912 (mapc
1913 (lambda (e)
1914 (goto-char (point-min))
1915 (while (re-search-forward (org-item-beginning-re) nil t)
1916 (let ((context (nth 2 (org-list-context))))
1917 (if (eq context e)
1918 (funcall mark-list e)
1919 (put-text-property (point-at-bol) (point-at-eol)
1920 'list-context context)))))
1921 (cons nil org-list-export-context))))))
1923 (defun org-export-attach-captions-and-attributes (target-alist)
1924 "Move #+CAPTION, #+ATTR_BACKEND, and #+LABEL text into text properties.
1925 If the next thing following is a table, add the text properties to the first
1926 table line. If it is a link, add it to the line containing the link."
1927 (goto-char (point-min))
1928 (remove-text-properties (point-min) (point-max)
1929 '(org-caption nil org-attributes nil))
1930 (let ((case-fold-search t)
1931 (re (concat "^[ \t]*#\\+caption:[ \t]+\\(.*\\)"
1932 "\\|"
1933 "^[ \t]*#\\+attr_" (symbol-name org-export-current-backend) ":[ \t]+\\(.*\\)"
1934 "\\|"
1935 "^[ \t]*#\\+label:[ \t]+\\(.*\\)"
1936 "\\|"
1937 "^[ \t]*\\(|[^-]\\)"
1938 "\\|"
1939 "^[ \t]*\\[\\[.*\\]\\][ \t]*$"))
1940 cap shortn attr label end)
1941 (while (re-search-forward re nil t)
1942 (cond
1943 ;; there is a caption
1944 ((match-end 1)
1945 (progn
1946 (setq cap (concat cap (if cap " " "") (org-trim (match-string 1))))
1947 (when (string-match "\\[\\(.*\\)\\]{\\(.*\\)}" cap)
1948 (setq shortn (match-string 1 cap)
1949 cap (match-string 2 cap)))
1950 (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))))
1951 ;; there is an attribute
1952 ((match-end 2)
1953 (progn
1954 (setq attr (concat attr (if attr " " "") (org-trim (match-string 2))))
1955 (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))))
1956 ;; there is a label
1957 ((match-end 3)
1958 (progn
1959 (setq label (org-trim (match-string 3)))
1960 (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))))
1962 (setq end (if (match-end 4)
1963 (let ((ee (org-table-end)))
1964 (prog1 (1- (marker-position ee)) (move-marker ee nil)))
1965 (point-at-eol)))
1966 (add-text-properties (point-at-bol) end
1967 (list 'org-caption cap
1968 'org-caption-shortn shortn
1969 'org-attributes attr
1970 'org-label label))
1971 (if label (push (cons label label) target-alist))
1972 (goto-char end)
1973 (setq cap nil shortn nil attr nil label nil)))))
1974 target-alist)
1976 (defun org-export-remove-comment-blocks-and-subtrees ()
1977 "Remove the comment environment, and also commented subtrees."
1978 (let ((re-commented (format org-heading-keyword-regexp-format
1979 org-comment-string))
1980 case-fold-search)
1981 ;; Remove comment environment
1982 (goto-char (point-min))
1983 (setq case-fold-search t)
1984 (while (re-search-forward
1985 "^#\\+begin_comment[ \t]*\n[^\000]*?\n#\\+end_comment\\>.*" nil t)
1986 (replace-match "" t t))
1987 ;; Remove subtrees that are commented
1988 (goto-char (point-min))
1989 (setq case-fold-search nil)
1990 (while (re-search-forward re-commented nil t)
1991 (goto-char (match-beginning 0))
1992 (delete-region (point) (org-end-of-subtree t)))))
1994 (defun org-export-handle-comments (org-commentsp)
1995 "Remove comments, or convert to backend-specific format.
1996 ORG-COMMENTSP can be a format string for publishing comments.
1997 When it is nil, all comments will be removed."
1998 (let ((re "^[ \t]*#\\( \\|$\\)"))
1999 (goto-char (point-min))
2000 (while (re-search-forward re nil t)
2001 (let ((pos (match-beginning 0))
2002 (end (progn (forward-line) (point))))
2003 (if (get-text-property pos 'org-protected)
2004 (forward-line)
2005 (if (not org-commentsp) (delete-region pos end)
2006 (add-text-properties pos end '(org-protected t))
2007 (replace-match
2008 (org-add-props
2009 (format org-commentsp (buffer-substring (match-end 0) end))
2010 nil 'org-protected t)
2011 t t)))))
2012 ;; Hack attack: previous implementation also removed keywords at
2013 ;; column 0. Brainlessly do it again.
2014 (goto-char (point-min))
2015 (while (re-search-forward "^#\\+" nil t)
2016 (unless (get-text-property (point-at-bol) 'org-protected)
2017 (delete-region (point-at-bol) (progn (forward-line) (point)))))))
2019 (defun org-export-handle-metalines ()
2020 "Remove tables and source blocks metalines.
2021 This function should only be called after all block processing
2022 has taken place."
2023 (let ((re "^[ \t]*#\\+\\(tbl\\(?:name\\|fm\\)\\|results\\(?:\\[[a-z0-9]+\\]\\)?\\|name\\):\\(.*\n?\\)")
2024 (case-fold-search t)
2025 pos)
2026 (goto-char (point-min))
2027 (while (or (looking-at re)
2028 (re-search-forward re nil t))
2029 (setq pos (match-beginning 0))
2030 (if (get-text-property (match-beginning 1) 'org-protected)
2031 (goto-char (1+ pos))
2032 (goto-char (1+ pos))
2033 (replace-match "")
2034 (goto-char (max (point-min) (1- pos)))))))
2036 (defun org-export-mark-radio-links ()
2037 "Find all matches for radio targets and turn them into internal links."
2038 (let ((re-radio (and org-target-link-regexp
2039 (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)"))))
2040 (goto-char (point-min))
2041 (when re-radio
2042 (while (re-search-forward re-radio nil t)
2043 (unless
2044 (save-match-data
2045 (or (org-in-regexp org-bracket-link-regexp)
2046 (org-in-regexp org-plain-link-re)
2047 (org-in-regexp "<<[^<>]+>>")))
2048 (org-if-unprotected
2049 (replace-match "\\1[[\\2]]")))))))
2051 (defun org-store-forced-table-alignment ()
2052 "Find table lines which force alignment, store the results in properties."
2053 (let (line cnt cookies)
2054 (goto-char (point-min))
2055 (while (re-search-forward "|[ \t]*<\\([lrc]?[0-9]+\\|[lrc]\\)>[ \t]*|"
2056 nil t)
2057 ;; OK, this looks like a table line with an alignment cookie
2058 (org-if-unprotected
2059 (setq line (buffer-substring (point-at-bol) (point-at-eol)))
2060 (when (and (org-at-table-p)
2061 (org-table-cookie-line-p line))
2062 (setq cnt 0 cookies nil)
2063 (mapc
2064 (lambda (x)
2065 (setq cnt (1+ cnt))
2066 (when (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'" x)
2067 (let ((align (and (match-end 1)
2068 (downcase (match-string 1 x))))
2069 (width (and (match-end 2)
2070 (string-to-number (match-string 2 x)))))
2071 (push (cons cnt (list align width)) cookies))))
2072 (org-split-string line "[ \t]*|[ \t]*"))
2073 (add-text-properties (org-table-begin) (org-table-end)
2074 (list 'org-col-cookies cookies))))
2075 (goto-char (point-at-eol)))))
2077 (defun org-export-remove-special-table-lines ()
2078 "Remove tables lines that are used for internal purposes.
2079 Also, store forced alignment information found in such lines."
2080 (goto-char (point-min))
2081 (while (re-search-forward "^[ \t]*|" nil t)
2082 (org-if-unprotected-at (1- (point))
2083 (beginning-of-line 1)
2084 (if (or (looking-at "[ \t]*| *[!_^] *|")
2085 (not
2086 (memq
2088 (mapcar
2089 (lambda (f)
2090 (or (and org-export-table-remove-empty-lines (= (length f) 0))
2091 (string-match
2092 "\\`<\\([0-9]\\|[lrc]\\|[lrc][0-9]+\\)>\\'" f)))
2093 (org-split-string ;; FIXME, can't we do without splitting???
2094 (buffer-substring (point-at-bol) (point-at-eol))
2095 "[ \t]*|[ \t]*")))))
2096 (delete-region (max (point-min) (1- (point-at-bol)))
2097 (point-at-eol))
2098 (end-of-line 1)))))
2100 (defun org-export-protect-sub-super (s)
2101 (save-match-data
2102 (while (string-match "\\([^\\\\]\\)\\([_^]\\)" s)
2103 (setq s (replace-match "\\1\\\\\\2" nil nil s)))
2106 (defun org-export-normalize-links ()
2107 "Convert all links to bracket links, and expand link abbreviations."
2108 (let ((re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
2109 (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
2110 nodesc)
2111 (goto-char (point-min))
2112 (while (re-search-forward org-bracket-link-regexp nil t)
2113 (put-text-property (match-beginning 0) (match-end 0) 'org-normalized-link t))
2114 (goto-char (point-min))
2115 (while (re-search-forward re-plain-link nil t)
2116 (unless (get-text-property (match-beginning 0) 'org-normalized-link)
2117 (goto-char (1- (match-end 0)))
2118 (org-if-unprotected-at (1+ (match-beginning 0))
2119 (let* ((s (concat (match-string 1)
2120 "[[" (match-string 2) ":" (match-string 3)
2121 "][" (match-string 2) ":" (org-export-protect-sub-super
2122 (match-string 3))
2123 "]]")))
2124 ;; added 'org-link face to links
2125 (put-text-property 0 (length s) 'face 'org-link s)
2126 (replace-match s t t)))))
2127 (goto-char (point-min))
2128 (while (re-search-forward re-angle-link nil t)
2129 (goto-char (1- (match-end 0)))
2130 (org-if-unprotected
2131 (let* ((s (concat (match-string 1)
2132 "[[" (match-string 2) ":" (match-string 3)
2133 "][" (match-string 2) ":" (org-export-protect-sub-super
2134 (match-string 3))
2135 "]]")))
2136 (put-text-property 0 (length s) 'face 'org-link s)
2137 (replace-match s t t))))
2138 (goto-char (point-min))
2139 (while (re-search-forward org-bracket-link-regexp nil t)
2140 (goto-char (1- (match-end 0)))
2141 (setq nodesc (not (match-end 3)))
2142 (org-if-unprotected
2143 (let* ((xx (save-match-data
2144 (org-translate-link
2145 (org-link-expand-abbrev (match-string 1)))))
2146 (s (concat
2147 "[[" (org-add-props (copy-sequence xx)
2148 nil 'org-protected t 'org-no-description nodesc)
2150 (if (match-end 3)
2151 (match-string 2)
2152 (concat "[" (copy-sequence xx)
2153 "]"))
2154 "]")))
2155 (put-text-property 0 (length s) 'face 'org-link s)
2156 (replace-match s t t))))))
2158 (defun org-export-concatenate-multiline-links ()
2159 "Find multi-line links and put it all into a single line.
2160 This is to make sure that the line-processing export backends
2161 can work correctly."
2162 (goto-char (point-min))
2163 (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
2164 (org-if-unprotected-at (match-beginning 1)
2165 (replace-match "\\1 \\3")
2166 (goto-char (match-beginning 0)))))
2168 (defun org-export-concatenate-multiline-emphasis ()
2169 "Find multi-line emphasis and put it all into a single line.
2170 This is to make sure that the line-processing export backends
2171 can work correctly."
2172 (goto-char (point-min))
2173 (while (re-search-forward org-emph-re nil t)
2174 (if (and (not (= (char-after (match-beginning 3))
2175 (char-after (match-beginning 4))))
2176 (save-excursion (goto-char (match-beginning 0))
2177 (save-match-data
2178 (and (not (org-at-table-p))
2179 (not (org-at-heading-p))))))
2180 (org-if-unprotected
2181 (subst-char-in-region (match-beginning 0) (match-end 0)
2182 ?\n ?\ t)
2183 (goto-char (1- (match-end 0))))
2184 (goto-char (1+ (match-beginning 0))))))
2186 (defun org-export-grab-title-from-buffer ()
2187 "Get a title for the current document, from looking at the buffer."
2188 (let ((inhibit-read-only t))
2189 (save-excursion
2190 (goto-char (point-min))
2191 (let ((end (if (looking-at org-outline-regexp)
2192 (point)
2193 (save-excursion (outline-next-heading) (point)))))
2194 (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
2195 ;; Mark the line so that it will not be exported as normal text.
2196 (unless (org-in-block-p org-list-forbidden-blocks)
2197 (org-unmodified
2198 (add-text-properties (match-beginning 0) (match-end 0)
2199 (list :org-license-to-kill t))))
2200 ;; Return the title string
2201 (org-trim (match-string 0)))))))
2203 (defun org-export-get-title-from-subtree ()
2204 "Return subtree title and exclude it from export."
2205 (let ((rbeg (region-beginning)) (rend (region-end))
2206 (inhibit-read-only t)
2207 (tags (plist-get (org-infile-export-plist) :tags))
2208 title)
2209 (save-excursion
2210 (goto-char rbeg)
2211 (when (and (org-at-heading-p)
2212 (>= (org-end-of-subtree t t) rend))
2213 (when (plist-member org-export-opt-plist :tags)
2214 (setq tags (or (plist-get org-export-opt-plist :tags) tags)))
2215 ;; This is a subtree, we take the title from the first heading
2216 (goto-char rbeg)
2217 (looking-at org-todo-line-tags-regexp)
2218 (setq title (if (and (eq tags t) (match-string 4))
2219 (format "%s\t%s" (match-string 3) (match-string 4))
2220 (match-string 3)))
2221 (org-unmodified
2222 (add-text-properties (point) (1+ (point-at-eol))
2223 (list :org-license-to-kill t)))
2224 (setq title (or (org-entry-get nil "EXPORT_TITLE") title))))
2225 title))
2227 (defun org-solidify-link-text (s &optional alist)
2228 "Take link text and make a safe target out of it."
2229 (save-match-data
2230 (let* ((rtn
2231 (mapconcat
2232 'identity
2233 (org-split-string s "[^a-zA-Z0-9_\\.-]+") "-"))
2234 (a (assoc rtn alist)))
2235 (or (cdr a) rtn))))
2237 (defun org-get-min-level (lines &optional offset)
2238 "Get the minimum level in LINES."
2239 (let ((re "^\\(\\*+\\) ") l)
2240 (catch 'exit
2241 (while (setq l (pop lines))
2242 (if (string-match re l)
2243 (throw 'exit (org-tr-level (- (length (match-string 1 l))
2244 (or offset 0))))))
2245 1)))
2247 ;; Variable holding the vector with section numbers
2248 (defvar org-section-numbers (make-vector org-level-max 0))
2250 (defun org-init-section-numbers ()
2251 "Initialize the vector for the section numbers."
2252 (let* ((level -1)
2253 (numbers (nreverse (org-split-string "" "\\.")))
2254 (depth (1- (length org-section-numbers)))
2255 (i depth) number-string)
2256 (while (>= i 0)
2257 (if (> i level)
2258 (aset org-section-numbers i 0)
2259 (setq number-string (or (car numbers) "0"))
2260 (if (string-match "\\`[A-Z]\\'" number-string)
2261 (aset org-section-numbers i
2262 (- (string-to-char number-string) ?A -1))
2263 (aset org-section-numbers i (string-to-number number-string)))
2264 (pop numbers))
2265 (setq i (1- i)))))
2267 (defun org-section-number (&optional level)
2268 "Return a string with the current section number.
2269 When LEVEL is non-nil, increase section numbers on that level."
2270 (let* ((depth (1- (length org-section-numbers)))
2271 (string "")
2272 (fmts (car org-export-section-number-format))
2273 (term (cdr org-export-section-number-format))
2274 (sep "")
2275 ctype fmt idx n)
2276 (when level
2277 (when (> level -1)
2278 (aset org-section-numbers
2279 level (1+ (aref org-section-numbers level))))
2280 (setq idx (1+ level))
2281 (while (<= idx depth)
2282 (if (not (= idx 1))
2283 (aset org-section-numbers idx 0))
2284 (setq idx (1+ idx))))
2285 (setq idx 0)
2286 (while (<= idx depth)
2287 (when (> (aref org-section-numbers idx) 0)
2288 (setq fmt (or (pop fmts) fmt)
2289 ctype (car fmt)
2290 n (aref org-section-numbers idx)
2291 string (if (> n 0)
2292 (concat string sep (org-number-to-counter n ctype))
2293 (concat string ".0"))
2294 sep (nth 1 fmt)))
2295 (setq idx (1+ idx)))
2296 (save-match-data
2297 (if (string-match "\\`\\([@0]\\.\\)+" string)
2298 (setq string (replace-match "" t nil string)))
2299 (if (string-match "\\(\\.0\\)+\\'" string)
2300 (setq string (replace-match "" t nil string))))
2301 (concat string term)))
2303 (defun org-number-to-counter (n type)
2304 "Concert number N to a string counter, according to TYPE.
2305 TYPE must be a string, any of:
2306 1 number
2307 A A,B,....
2308 a a,b,....
2309 I upper case roman numeral
2310 i lower case roman numeral"
2311 (cond
2312 ((equal type "1") (number-to-string n))
2313 ((equal type "A") (char-to-string (+ ?A n -1)))
2314 ((equal type "a") (char-to-string (+ ?a n -1)))
2315 ((equal type "I") (org-number-to-roman n))
2316 ((equal type "i") (downcase (org-number-to-roman n)))
2317 (t (error "Invalid counter type `%s'" type))))
2319 (defun org-number-to-roman (n)
2320 "Convert integer N into a roman numeral."
2321 (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
2322 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
2323 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
2324 ( 1 . "I")))
2325 (res ""))
2326 (if (<= n 0)
2327 (number-to-string n)
2328 (while roman
2329 (if (>= n (caar roman))
2330 (setq n (- n (caar roman))
2331 res (concat res (cdar roman)))
2332 (pop roman)))
2333 res)))
2335 ;;; Macros
2337 (defun org-export-preprocess-apply-macros ()
2338 "Replace macro references."
2339 (goto-char (point-min))
2340 (let (sy val key args args2 ind-str s n)
2341 (while (re-search-forward
2342 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
2343 nil t)
2344 (unless (save-match-data (save-excursion
2345 (goto-char (point-at-bol))
2346 (looking-at "[ \t]*#\\+macro")))
2347 ;; Get macro name (KEY), arguments (ARGS), and indentation of
2348 ;; current line (IND-STR) as strings.
2349 (setq key (downcase (match-string 1))
2350 args (match-string 3)
2351 ind-str (save-match-data (save-excursion
2352 (beginning-of-line)
2353 (looking-at "^\\([ \t]*\\).*")
2354 (match-string 1))))
2355 ;; When macro is defined, retrieve replacement text in VAL,
2356 ;; and proceed with expansion.
2357 (when (setq val (or (plist-get org-export-opt-plist
2358 (intern (concat ":macro-" key)))
2359 (plist-get org-export-opt-plist
2360 (intern (concat ":" key)))))
2361 (save-match-data
2362 ;; If arguments are provided, first retrieve them properly
2363 ;; (in ARGS, as a list), then replace them in VAL.
2364 (when args
2365 (setq args (org-split-string args ",") args2 nil)
2366 (while args
2367 (while (string-match "\\\\\\'" (car args))
2368 ;; Repair bad splits.
2369 (setcar (cdr args) (concat (substring (car args) 0 -1)
2370 "," (nth 1 args)))
2371 (pop args))
2372 (push (pop args) args2))
2373 (setq args (mapcar 'org-trim (nreverse args2)))
2374 (setq s 0)
2375 (while (string-match "\\$\\([0-9]+\\)" val s)
2376 (setq s (1+ (match-beginning 0))
2377 n (string-to-number (match-string 1 val)))
2378 (and (>= (length args) n)
2379 (setq val (replace-match (nth (1- n) args) t t val)))))
2380 ;; VAL starts with "(eval": it is a sexp, `eval' it.
2381 (when (string-match "\\`(eval\\>" val)
2382 (setq val (eval (read val))))
2383 ;; Ensure VAL is a string (or nil) and that each new line
2384 ;; is indented as the first one.
2385 (setq val (and val
2386 (mapconcat 'identity
2387 (org-split-string
2388 (if (stringp val) val (format "%s" val))
2389 "\n")
2390 (concat "\n" ind-str)))))
2391 ;; Eventually do the replacement, if VAL isn't nil. Move
2392 ;; point at beginning of macro for recursive expansions.
2393 (when val
2394 (replace-match val t t)
2395 (goto-char (match-beginning 0))))))))
2397 (defun org-export-apply-macros-in-string (s)
2398 "Apply the macros in string S."
2399 (when s
2400 (with-temp-buffer
2401 (insert s)
2402 (org-export-preprocess-apply-macros)
2403 (buffer-string))))
2405 ;;; Include files
2407 (defun org-export-handle-include-files ()
2408 "Include the contents of include files, with proper formatting."
2409 (let ((case-fold-search t)
2410 params file markup lang start end prefix prefix1 switches all minlevel currentlevel addlevel lines)
2411 (goto-char (point-min))
2412 (while (re-search-forward "^#\\+include:[ \t]+\\(.*\\)" nil t)
2413 (setq params (read (concat "(" (match-string 1) ")"))
2414 prefix (org-get-and-remove-property 'params :prefix)
2415 prefix1 (org-get-and-remove-property 'params :prefix1)
2416 minlevel (org-get-and-remove-property 'params :minlevel)
2417 addlevel (org-get-and-remove-property 'params :addlevel)
2418 lines (org-get-and-remove-property 'params :lines)
2419 file (org-symname-or-string (pop params))
2420 markup (org-symname-or-string (pop params))
2421 lang (and (member markup '("src" "SRC"))
2422 (org-symname-or-string (pop params)))
2423 switches (mapconcat #'(lambda (x) (format "%s" x)) params " ")
2424 start nil end nil)
2425 (delete-region (match-beginning 0) (match-end 0))
2426 (setq currentlevel (or (org-current-level) 0))
2427 (if (or (not file)
2428 (not (file-exists-p file))
2429 (not (file-readable-p file)))
2430 (insert (format "CANNOT INCLUDE FILE %s" file))
2431 (setq all (cons file all))
2432 (when markup
2433 (if (equal (downcase markup) "src")
2434 (setq start (format "#+begin_src %s %s\n"
2435 (or lang "fundamental")
2436 (or switches ""))
2437 end "#+end_src")
2438 (setq start (format "#+begin_%s %s\n" markup switches)
2439 end (format "#+end_%s" markup))))
2440 (insert (or start ""))
2441 (insert (org-get-file-contents (expand-file-name file)
2442 prefix prefix1 markup currentlevel minlevel addlevel lines))
2443 (or (bolp) (newline))
2444 (insert (or end ""))))
2445 all))
2447 (defun org-export-handle-include-files-recurse ()
2448 "Recursively include files aborting on circular inclusion."
2449 (let ((now (list org-current-export-file)) all)
2450 (while now
2451 (setq all (append now all))
2452 (setq now (org-export-handle-include-files))
2453 (let ((intersection
2454 (delq nil
2455 (mapcar (lambda (el) (when (member el all) el)) now))))
2456 (when intersection
2457 (error "Recursive #+INCLUDE: %S" intersection))))))
2459 (defun org-get-file-contents (file &optional prefix prefix1 markup minlevel parentlevel addlevel lines)
2460 "Get the contents of FILE and return them as a string.
2461 If PREFIX is a string, prepend it to each line. If PREFIX1
2462 is a string, prepend it to the first line instead of PREFIX.
2463 If MARKUP, don't protect org-like lines, the exporter will
2464 take care of the block they are in. If ADDLEVEL is a number,
2465 demote included file to current heading level+ADDLEVEL.
2466 If LINES is a string specifying a range of lines,
2467 include only those lines."
2468 (if (stringp markup) (setq markup (downcase markup)))
2469 (with-temp-buffer
2470 (insert-file-contents file)
2471 (when lines
2472 (let* ((lines (split-string lines "-"))
2473 (lbeg (string-to-number (car lines)))
2474 (lend (string-to-number (cadr lines)))
2475 (beg (if (zerop lbeg) (point-min)
2476 (goto-char (point-min))
2477 (forward-line (1- lbeg))
2478 (point)))
2479 (end (if (zerop lend) (point-max)
2480 (goto-char (point-min))
2481 (forward-line (1- lend))
2482 (point))))
2483 (narrow-to-region beg end)))
2484 (when (or prefix prefix1)
2485 (goto-char (point-min))
2486 (while (not (eobp))
2487 (insert (or prefix1 prefix))
2488 (setq prefix1 "")
2489 (beginning-of-line 2)))
2490 (buffer-string)
2491 (when (member markup '("src" "example"))
2492 (goto-char (point-min))
2493 (while (re-search-forward "^\\([*#]\\|[ \t]*#\\+\\)" nil t)
2494 (goto-char (match-beginning 0))
2495 (insert ",")
2496 (end-of-line 1)))
2497 (when minlevel
2498 (dotimes (lvl minlevel)
2499 (org-map-region 'org-demote (point-min) (point-max))))
2500 (when addlevel
2501 (let ((inclevel (or (if (org-before-first-heading-p)
2502 (1- (and (outline-next-heading)
2503 (org-current-level)))
2504 (1- (org-current-level)))
2505 0)))
2506 (dotimes (level (- (+ parentlevel addlevel) inclevel))
2507 (org-map-region 'org-demote (point-min) (point-max)))))
2508 (buffer-string)))
2510 (defun org-get-and-remove-property (listvar prop)
2511 "Check if the value of LISTVAR contains PROP as a property.
2512 If yes, return the value of that property (i.e. the element following
2513 in the list) and remove property and value from the list in LISTVAR."
2514 (let ((list (symbol-value listvar)) m v)
2515 (when (setq m (member prop list))
2516 (setq v (nth 1 m))
2517 (if (equal (car list) prop)
2518 (set listvar (cddr list))
2519 (setcdr (nthcdr (- (length list) (length m) 1) list)
2520 (cddr m))
2521 (set listvar list)))
2524 (defun org-symname-or-string (s)
2525 (if (symbolp s)
2526 (if s (symbol-name s) s)
2529 ;;; Fontification and line numbers for code examples
2531 (defvar org-export-last-code-line-counter-value 0)
2533 (defun org-export-replace-src-segments-and-examples ()
2534 "Replace source code segments with special code for export."
2535 (setq org-export-last-code-line-counter-value 0)
2536 (let ((case-fold-search t)
2537 lang code trans opts indent caption)
2538 (goto-char (point-min))
2539 (while (re-search-forward
2540 "\\(^\\([ \t]*\\)#\\+BEGIN_SRC:?\\([ \t]+\\([^ \t\n]+\\)\\)?\\(.*\\)\n\\([^\000]+?\n\\)[ \t]*#\\+END_SRC.*\n?\\)\\|\\(^\\([ \t]*\\)#\\+BEGIN_EXAMPLE:?\\(?:[ \t]+\\(.*\\)\\)?\n\\([^\000]+?\n\\)[ \t]*#\\+END_EXAMPLE.*\n?\\)"
2541 nil t)
2542 (if (match-end 1)
2543 (if (not (match-string 4))
2544 (error "Source block missing language specification: %s"
2545 (let* ((body (match-string 6))
2546 (nothing (message "body:%s" body))
2547 (preview (or (and (string-match
2548 "^[ \t]*\\([^\n\r]*\\)" body)
2549 (match-string 1 body)) body)))
2550 (if (> (length preview) 35)
2551 (concat (substring preview 0 32) "...")
2552 preview)))
2553 ;; src segments
2554 (setq lang (match-string 4)
2555 opts (match-string 5)
2556 code (match-string 6)
2557 indent (length (match-string 2))
2558 caption (get-text-property 0 'org-caption (match-string 0))))
2559 (setq lang nil
2560 opts (match-string 9)
2561 code (match-string 10)
2562 indent (length (match-string 8))
2563 caption (get-text-property 0 'org-caption (match-string 0))))
2565 (setq trans (org-export-format-source-code-or-example
2566 lang code opts indent caption))
2567 (replace-match trans t t))))
2569 (defvar org-export-latex-verbatim-wrap) ;; defined in org-latex.el
2570 (defvar org-export-latex-listings) ;; defined in org-latex.el
2571 (defvar org-export-latex-listings-langs) ;; defined in org-latex.el
2572 (defvar org-export-latex-listings-w-names) ;; defined in org-latex.el
2573 (defvar org-export-latex-minted-langs) ;; defined in org-latex.el
2574 (defvar org-export-latex-custom-lang-environments) ;; defined in org-latex.el
2575 (defvar org-export-latex-listings-options) ;; defined in org-latex.el
2576 (defvar org-export-latex-minted-options) ;; defined in org-latex.el
2578 (defun org-remove-formatting-on-newlines-in-region (beg end)
2579 "Remove formatting on newline characters."
2580 (interactive "r")
2581 (save-excursion
2582 (goto-char beg)
2583 (while (progn (end-of-line) (< (point) end))
2584 (put-text-property (point) (1+ (point)) 'face nil)
2585 (forward-char 1))))
2587 (defun org-export-format-source-code-or-example
2588 (lang code &optional opts indent caption)
2589 "Format CODE from language LANG and return it formatted for export.
2590 The CODE is marked up in `org-export-current-backend' format.
2592 Check if a function by name
2593 \"org-<backend>-format-source-code-or-example\" is bound. If yes,
2594 use it as the custom formatter. Otherwise, use the default
2595 formatter. Default formatters are provided for docbook, html,
2596 latex and ascii backends. For example, use
2597 `org-html-format-source-code-or-example' to provide a custom
2598 formatter for export to \"html\".
2600 If LANG is nil, do not add any fontification.
2601 OPTS contains formatting options, like `-n' for triggering numbering lines,
2602 and `+n' for continuing previous numbering.
2603 Code formatting according to language currently only works for HTML.
2604 Numbering lines works for all three major backends (html, latex, and ascii).
2605 INDENT was the original indentation of the block."
2606 (save-match-data
2607 (let* ((backend-name (symbol-name org-export-current-backend))
2608 (backend-formatter
2609 (intern (format "org-%s-format-source-code-or-example"
2610 backend-name)))
2611 (backend-feature (intern (concat "org-" backend-name)))
2612 (backend-formatter
2613 (and (require (intern (concat "org-" backend-name)) nil)
2614 (fboundp backend-formatter) backend-formatter))
2615 num cont rtn rpllbl keepp textareap preserve-indentp cols rows fmt)
2616 (setq opts (or opts "")
2617 num (string-match "[-+]n\\>" opts)
2618 cont (string-match "\\+n\\>" opts)
2619 rpllbl (string-match "-r\\>" opts)
2620 keepp (string-match "-k\\>" opts)
2621 textareap (string-match "-t\\>" opts)
2622 preserve-indentp (or org-src-preserve-indentation
2623 (string-match "-i\\>" opts))
2624 cols (if (string-match "-w[ \t]+\\([0-9]+\\)" opts)
2625 (string-to-number (match-string 1 opts))
2627 rows (if (string-match "-h[ \t]+\\([0-9]+\\)" opts)
2628 (string-to-number (match-string 1 opts))
2629 (org-count-lines code))
2630 fmt (if (string-match "-l[ \t]+\"\\([^\"\n]+\\)\"" opts)
2631 (match-string 1 opts)))
2632 (when (and textareap (eq org-export-current-backend 'html))
2633 ;; we cannot use numbering or highlighting.
2634 (setq num nil cont nil lang nil))
2635 (if keepp (setq rpllbl 'keep))
2636 (setq rtn (if preserve-indentp code (org-remove-indentation code)))
2637 (when (string-match "^," rtn)
2638 (setq rtn (with-temp-buffer
2639 (insert rtn)
2640 ;; Free up the protected lines
2641 (goto-char (point-min))
2642 (while (re-search-forward "^," nil t)
2643 (if (or (equal lang "org")
2644 (save-match-data
2645 (looking-at "\\([*#]\\|[ \t]*#\\+\\)")))
2646 (replace-match ""))
2647 (end-of-line 1))
2648 (buffer-string))))
2649 ;; Now backend-specific coding
2650 (setq rtn
2651 (cond
2652 (backend-formatter
2653 (funcall backend-formatter rtn lang caption textareap cols rows num
2654 cont rpllbl fmt))
2655 ((eq org-export-current-backend 'docbook)
2656 (setq rtn (org-export-number-lines rtn 0 0 num cont rpllbl fmt))
2657 (concat "<programlisting><![CDATA["
2659 "]]></programlisting>\n"))
2660 ((eq org-export-current-backend 'html)
2661 ;; We are exporting to HTML
2662 (when lang
2663 (if (featurep 'xemacs)
2664 (require 'htmlize)
2665 (require 'htmlize nil t))
2666 (when (not (fboundp 'htmlize-region-for-paste))
2667 ;; we do not have htmlize.el, or an old version of it
2668 (setq lang nil)
2669 (message
2670 "htmlize.el 1.34 or later is needed for source code formatting")))
2672 (if lang
2673 (let* ((lang-m (when lang
2674 (or (cdr (assoc lang org-src-lang-modes))
2675 lang)))
2676 (mode (and lang-m (intern
2677 (concat
2678 (if (symbolp lang-m)
2679 (symbol-name lang-m)
2680 lang-m)
2681 "-mode"))))
2682 (org-inhibit-startup t)
2683 (org-startup-folded nil))
2684 (setq rtn
2685 (with-temp-buffer
2686 (insert rtn)
2687 (if (functionp mode)
2688 (funcall mode)
2689 (fundamental-mode))
2690 (font-lock-fontify-buffer)
2691 ;; markup each line separately
2692 (org-remove-formatting-on-newlines-in-region (point-min) (point-max))
2693 (org-src-mode)
2694 (set-buffer-modified-p nil)
2695 (org-export-htmlize-region-for-paste
2696 (point-min) (point-max))))
2697 (if (string-match "<pre\\([^>]*\\)>\n*" rtn)
2698 (setq rtn
2699 (concat
2700 (if caption
2701 (concat
2702 "<div class=\"org-src-container\">"
2703 (format
2704 "<label class=\"org-src-name\">%s</label>"
2705 caption))
2707 (replace-match
2708 (format "<pre class=\"src src-%s\">\n" lang)
2709 t t rtn)
2710 (if caption "</div>" "")))))
2711 (if textareap
2712 (setq rtn (concat
2713 (format "<p>\n<textarea cols=\"%d\" rows=\"%d\">"
2714 cols rows)
2715 rtn "</textarea>\n</p>\n"))
2716 (with-temp-buffer
2717 (insert rtn)
2718 (goto-char (point-min))
2719 (while (re-search-forward "[<>&]" nil t)
2720 (replace-match (cdr (assq (char-before)
2721 '((?&."&amp;")(?<."&lt;")(?>."&gt;"))))
2722 t t))
2723 (setq rtn (buffer-string)))
2724 (setq rtn (concat "<pre class=\"example\">\n" rtn "</pre>\n"))))
2725 (unless textareap
2726 (setq rtn (org-export-number-lines rtn 1 1 num cont rpllbl fmt)))
2727 (if (string-match "\\(\\`<[^>]*>\\)\n" rtn)
2728 (setq rtn (replace-match "\\1" t nil rtn)))
2729 rtn)
2730 ((eq org-export-current-backend 'latex)
2731 (setq rtn (org-export-number-lines rtn 0 0 num cont rpllbl fmt))
2732 (cond
2733 ((and lang org-export-latex-listings)
2734 (let* ((make-option-string
2735 (lambda (pair)
2736 (concat (first pair)
2737 (if (> (length (second pair)) 0)
2738 (concat "=" (second pair))))))
2739 (lang-sym (intern lang))
2740 (minted-p (eq org-export-latex-listings 'minted))
2741 (listings-p (not minted-p))
2742 (backend-lang
2743 (or (cadr
2744 (assq
2745 lang-sym
2746 (cond
2747 (minted-p org-export-latex-minted-langs)
2748 (listings-p org-export-latex-listings-langs))))
2749 lang))
2750 (custom-environment
2751 (cadr
2752 (assq
2753 lang-sym
2754 org-export-latex-custom-lang-environments))))
2755 (concat
2756 (when (and listings-p (not custom-environment))
2757 (format
2758 "\\lstset{%s}\n"
2759 (mapconcat
2760 make-option-string
2761 (append org-export-latex-listings-options
2762 `(("language" ,backend-lang))) ",")))
2763 (when (and caption org-export-latex-listings-w-names)
2764 (format
2765 "\n%s $\\equiv$ \n"
2766 (replace-regexp-in-string "_" "\\\\_" caption)))
2767 (cond
2768 (custom-environment
2769 (format "\\begin{%s}\n%s\\end{%s}\n"
2770 custom-environment rtn custom-environment))
2771 (listings-p
2772 (format "\\begin{%s}\n%s\\end{%s}"
2773 "lstlisting" rtn "lstlisting"))
2774 (minted-p
2775 (format
2776 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2777 (mapconcat make-option-string
2778 org-export-latex-minted-options ",")
2779 backend-lang rtn))))))
2780 (t (concat (car org-export-latex-verbatim-wrap)
2781 rtn (cdr org-export-latex-verbatim-wrap)))))
2782 ((eq org-export-current-backend 'ascii)
2783 ;; This is not HTML or LaTeX, so just make it an example.
2784 (setq rtn (org-export-number-lines rtn 0 0 num cont rpllbl fmt))
2785 (concat caption "\n"
2786 (concat
2787 (mapconcat
2788 (lambda (l) (concat " " l))
2789 (org-split-string rtn "\n")
2790 "\n")
2791 "\n")))
2793 (error "Don't know how to markup source or example block in %s"
2794 (upcase backend-name)))))
2795 (setq rtn
2796 (concat
2797 "\n#+BEGIN_" backend-name "\n"
2798 (org-add-props rtn
2799 '(org-protected t org-example t org-native-text t))
2800 "\n#+END_" backend-name "\n"))
2801 (org-add-props rtn nil 'original-indentation indent))))
2803 (defun org-export-number-lines (text &optional skip1 skip2 number cont
2804 replace-labels label-format preprocess)
2805 "Apply line numbers to literal examples and handle code references.
2806 Handle user-specified options under info node `(org)Literal
2807 examples' and return the modified source block.
2809 TEXT contains the source or example block.
2811 SKIP1 and SKIP2 are the number of lines that are to be skipped at
2812 the beginning and end of TEXT. Use these to skip over
2813 backend-specific lines pre-pended or appended to the original
2814 source block.
2816 NUMBER is non-nil if the literal example specifies \"+n\" or
2817 \"-n\" switch. If NUMBER is non-nil add line numbers.
2819 CONT is non-nil if the literal example specifies \"+n\" switch.
2820 If CONT is nil, start numbering this block from 1. Otherwise
2821 continue numbering from the last numbered block.
2823 REPLACE-LABELS is dual-purpose.
2824 1. It controls the retention of labels in the exported block.
2825 2. It specifies in what manner the links (or references) to a
2826 labeled line be formatted.
2828 REPLACE-LABELS is the symbol `keep' if the literal example
2829 specifies \"-k\" option, is numeric if the literal example
2830 specifies \"-r\" option and is nil otherwise.
2832 Handle REPLACE-LABELS as below:
2833 - If nil, retain labels in the exported block and use
2834 user-provided labels for referencing the labeled lines.
2835 - If it is a number, remove labels in the exported block and use
2836 one of line numbers or labels for referencing labeled lines based
2837 on NUMBER option.
2838 - If it is a keep, retain labels in the exported block and use
2839 one of line numbers or labels for referencing labeled lines
2840 based on NUMBER option.
2842 LABEL-FORMAT is the value of \"-l\" switch associated with
2843 literal example. See `org-coderef-label-format'.
2845 PREPROCESS is intended for backend-agnostic handling of source
2846 block numbering. When non-nil do the following:
2847 - do not number the lines
2848 - always strip the labels from exported block
2849 - do not make the labeled line a target of an incoming link.
2850 Instead mark the labeled line with `org-coderef' property and
2851 store the label in it."
2852 (setq skip1 (or skip1 0) skip2 (or skip2 0))
2853 (if (and number (not cont)) (setq org-export-last-code-line-counter-value 0))
2854 (with-temp-buffer
2855 (insert text)
2856 (goto-char (point-max))
2857 (skip-chars-backward " \t\n\r")
2858 (delete-region (point) (point-max))
2859 (beginning-of-line (- 1 skip2))
2860 (let* ((last (org-current-line))
2861 (n org-export-last-code-line-counter-value)
2862 (nmax (+ n (- last skip1)))
2863 (fmt (format "%%%dd: " (length (number-to-string nmax))))
2865 (cond
2866 ((eq org-export-current-backend 'html) (format "<span class=\"linenr\">%s</span>"
2867 fmt))
2868 ((eq org-export-current-backend 'ascii) fmt)
2869 ((eq org-export-current-backend 'latex) fmt)
2870 ((eq org-export-current-backend 'docbook) fmt)
2871 (t "")))
2872 (label-format (or label-format org-coderef-label-format))
2873 (label-pre (if (string-match "%s" label-format)
2874 (substring label-format 0 (match-beginning 0))
2875 label-format))
2876 (label-post (if (string-match "%s" label-format)
2877 (substring label-format (match-end 0))
2878 ""))
2879 (lbl-re
2880 (concat
2881 ".*?\\S-.*?\\([ \t]*\\("
2882 (regexp-quote label-pre)
2883 "\\([-a-zA-Z0-9_ ]+\\)"
2884 (regexp-quote label-post)
2885 "\\)\\)"))
2886 ref)
2888 (org-goto-line (1+ skip1))
2889 (while (and (re-search-forward "^" nil t) (not (eobp)) (< n nmax))
2890 (when number (incf n))
2891 (if (or preprocess (not number))
2892 (forward-char 1)
2893 (insert (format fm n)))
2894 (when (looking-at lbl-re)
2895 (setq ref (match-string 3))
2896 (cond ((numberp replace-labels)
2897 ;; remove labels; use numbers for references when lines
2898 ;; are numbered, use labels otherwise
2899 (delete-region (match-beginning 1) (match-end 1))
2900 (push (cons ref (if (> n 0) n ref)) org-export-code-refs))
2901 ((eq replace-labels 'keep)
2902 ;; don't remove labels; use numbers for references when
2903 ;; lines are numbered, use labels otherwise
2904 (goto-char (match-beginning 2))
2905 (delete-region (match-beginning 2) (match-end 2))
2906 (unless preprocess
2907 (insert "(" ref ")"))
2908 (push (cons ref (if (> n 0) n (concat "(" ref ")")))
2909 org-export-code-refs))
2911 ;; don't remove labels and don't use numbers for
2912 ;; references
2913 (goto-char (match-beginning 2))
2914 (delete-region (match-beginning 2) (match-end 2))
2915 (unless preprocess
2916 (insert "(" ref ")"))
2917 (push (cons ref (concat "(" ref ")")) org-export-code-refs)))
2918 (when (and (eq org-export-current-backend 'html) (not preprocess))
2919 (save-excursion
2920 (beginning-of-line 1)
2921 (insert (format "<span id=\"coderef-%s\" class=\"coderef-off\">"
2922 ref))
2923 (end-of-line 1)
2924 (insert "</span>")))
2925 (when preprocess
2926 (add-text-properties
2927 (point-at-bol) (point-at-eol) (list 'org-coderef ref)))))
2928 (setq org-export-last-code-line-counter-value n)
2929 (goto-char (point-max))
2930 (newline)
2931 (buffer-string))))
2933 (defun org-search-todo-below (line lines level)
2934 "Search the subtree below LINE for any TODO entries."
2935 (let ((rest (cdr (memq line lines)))
2936 (re org-todo-line-regexp)
2937 line lv todo)
2938 (catch 'exit
2939 (while (setq line (pop rest))
2940 (if (string-match re line)
2941 (progn
2942 (setq lv (- (match-end 1) (match-beginning 1))
2943 todo (and (match-beginning 2)
2944 (not (member (match-string 2 line)
2945 org-done-keywords))))
2946 ; TODO, not DONE
2947 (if (<= lv level) (throw 'exit nil))
2948 (if todo (throw 'exit t))))))))
2950 ;;;###autoload
2951 (defun org-export-visible (type arg)
2952 "Create a copy of the visible part of the current buffer, and export it.
2953 The copy is created in a temporary buffer and removed after use.
2954 TYPE is the final key (as a string) that also selects the export command in
2955 the \\<org-mode-map>\\[org-export] export dispatcher.
2956 As a special case, if the you type SPC at the prompt, the temporary
2957 org-mode file will not be removed but presented to you so that you can
2958 continue to use it. The prefix arg ARG is passed through to the exporting
2959 command."
2960 (interactive
2961 (list (progn
2962 (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")
2963 (read-char-exclusive))
2964 current-prefix-arg))
2965 (if (not (member type '(?a ?n ?u ?\C-a ?b ?\C-b ?h ?D ?x ?\ ?l ?p ?d ?L ?H ?R)))
2966 (error "Invalid export key"))
2967 (let* ((binding (cdr (assoc type
2969 (?a . org-export-as-ascii)
2970 (?A . org-export-as-ascii-to-buffer)
2971 (?n . org-export-as-latin1)
2972 (?N . org-export-as-latin1-to-buffer)
2973 (?u . org-export-as-utf8)
2974 (?U . org-export-as-utf8-to-buffer)
2975 (?\C-a . org-export-as-ascii)
2976 (?b . org-export-as-html-and-open)
2977 (?\C-b . org-export-as-html-and-open)
2978 (?h . org-export-as-html)
2979 (?H . org-export-as-html-to-buffer)
2980 (?R . org-export-region-as-html)
2981 (?D . org-export-as-docbook)
2983 (?l . org-export-as-latex)
2984 (?p . org-export-as-pdf)
2985 (?d . org-export-as-pdf-and-open)
2986 (?L . org-export-as-latex-to-buffer)
2988 (?x . org-export-as-xoxo)))))
2989 (keepp (equal type ?\ ))
2990 (file buffer-file-name)
2991 (buffer (get-buffer-create "*Org Export Visible*"))
2992 s e)
2993 ;; Need to hack the drawers here.
2994 (save-excursion
2995 (goto-char (point-min))
2996 (while (re-search-forward org-drawer-regexp nil t)
2997 (goto-char (match-beginning 1))
2998 (or (outline-invisible-p) (org-flag-drawer nil))))
2999 (with-current-buffer buffer (erase-buffer))
3000 (save-excursion
3001 (setq s (goto-char (point-min)))
3002 (while (not (= (point) (point-max)))
3003 (goto-char (org-find-invisible))
3004 (append-to-buffer buffer s (point))
3005 (setq s (goto-char (org-find-visible))))
3006 (org-cycle-hide-drawers 'all)
3007 (goto-char (point-min))
3008 (unless keepp
3009 ;; Copy all comment lines to the end, to make sure #+ settings are
3010 ;; still available for the second export step. Kind of a hack, but
3011 ;; does do the trick.
3012 (if (looking-at "#[^\r\n]*")
3013 (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
3014 (when (re-search-forward "^\\*+[ \t]+" nil t)
3015 (while (re-search-backward "[\n\r]#[^\n\r]*" nil t)
3016 (append-to-buffer buffer (1+ (match-beginning 0))
3017 (min (point-max) (1+ (match-end 0)))))))
3018 (set-buffer buffer)
3019 (let ((buffer-file-name file)
3020 (org-inhibit-startup t))
3021 (org-mode)
3022 (show-all)
3023 (unless keepp (funcall binding arg))))
3024 (if (not keepp)
3025 (kill-buffer buffer)
3026 (switch-to-buffer-other-window buffer)
3027 (goto-char (point-min)))))
3029 (defvar org-export-htmlized-org-css-url) ;; defined in org-html.el
3031 (defun org-export-string (string fmt &optional dir)
3032 "Export STRING to FMT using existing export facilities.
3033 During export STRING is saved to a temporary file whose location
3034 could vary. Optional argument DIR can be used to force the
3035 directory in which the temporary file is created during export
3036 which can be useful for resolving relative paths. Dir defaults
3037 to the value of `temporary-file-directory'."
3038 (let ((temporary-file-directory (or dir temporary-file-directory))
3039 (tmp-file (make-temp-file "org-")))
3040 (unwind-protect
3041 (with-temp-buffer
3042 (insert string)
3043 (write-file tmp-file)
3044 (org-load-modules-maybe)
3045 (unless org-local-vars
3046 (setq org-local-vars (org-get-local-variables)))
3047 (eval ;; convert to fmt -- mimicking `org-run-like-in-org-mode'
3048 (list 'let org-local-vars
3049 (list (intern (format "org-export-as-%s" fmt))
3050 nil nil ''string t dir))))
3051 (delete-file tmp-file))))
3053 ;;;###autoload
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 org-export-copy-to-kill-ring
3342 (org-kill-new (buffer-string))
3343 (when (fboundp 'x-set-selection)
3344 (ignore-errors (x-set-selection 'PRIMARY (buffer-string)))
3345 (ignore-errors (x-set-selection 'CLIPBOARD (buffer-string))))
3346 (message "%s export done, pushed to kill ring and clipboard" format)))
3348 (provide 'org-exp)
3350 ;; Local variables:
3351 ;; generated-autoload-file: "org-loaddefs.el"
3352 ;; End:
3354 ;;; org-exp.el ends here