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