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