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