contrib/lisp/org-wikinodes.el: fix tiny typo.
[org-mode.git] / lisp / org-exp.el
blobc16bec8b060f2ecc514f52a5a36eeaa71c856a10
1 ;;; org-exp.el --- ASCII, HTML, XOXO and iCalendar export for Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
9 ;; Version: 7.01trans
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;;; Commentary:
29 ;;; Code:
31 (require 'org)
32 (require 'org-macs)
33 (require 'org-agenda)
34 (require 'org-exp-blocks)
35 (require 'ob-exp)
36 (require 'org-src)
38 (eval-when-compile
39 (require 'cl))
41 (declare-function org-export-latex-preprocess "org-latex" (parameters))
42 (declare-function org-export-ascii-preprocess "org-ascii" (parameters))
43 (declare-function org-export-html-preprocess "org-html" (parameters))
44 (declare-function org-export-docbook-preprocess "org-docbook" (parameters))
45 (declare-function org-infojs-options-inbuffer-template "org-jsinfo" ())
46 (declare-function org-export-htmlize-region-for-paste "org-html" (beg end))
47 (declare-function htmlize-buffer "ext:htmlize" (&optional buffer))
48 (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
49 (declare-function org-table-cookie-line-p "org-table" (line))
50 (declare-function org-table-colgroup-line-p "org-table" (line))
51 (autoload 'org-export-generic "org-export-generic" "Export using the generic exporter" t)
52 (defgroup org-export nil
53 "Options for exporting org-listings."
54 :tag "Org Export"
55 :group 'org)
57 (defgroup org-export-general nil
58 "General options for exporting Org-mode files."
59 :tag "Org Export General"
60 :group 'org-export)
62 (defcustom org-export-allow-BIND 'confirm
63 "Non-nil means allow #+BIND to define local variable values for export.
64 This is a potential security risk, which is why the user must confirm the
65 use of these lines."
66 :group 'org-export-general
67 :type '(choice
68 (const :tag "Never" nil)
69 (const :tag "Always" t)
70 (const :tag "Make the user confirm for each file" confirm)))
72 ;; FIXME
73 (defvar org-export-publishing-directory nil)
75 (defcustom org-export-show-temporary-export-buffer t
76 "Non-nil means show buffer after exporting to temp buffer.
77 When Org exports to a file, the buffer visiting that file is ever
78 shown, but remains buried. However, when exporting to a temporary
79 buffer, that buffer is popped up in a second window. When this variable
80 is nil, the buffer remains buried also in these cases."
81 :group 'org-export-general
82 :type 'boolean)
84 (defcustom org-export-copy-to-kill-ring t
85 "Non-nil means exported stuff will also be pushed onto the kill ring."
86 :group 'org-export-general
87 :type 'boolean)
89 (defcustom org-export-kill-product-buffer-when-displayed nil
90 "Non-nil means kill the product buffer if it is displayed immediately.
91 This applied to the commands `org-export-html-and-open' and
92 `org-export-as-pdf-and-open'."
93 :group 'org-export-general
94 :type 'boolean)
96 (defcustom org-export-run-in-background nil
97 "Non-nil means export and publishing commands will run in background.
98 This works by starting up a separate Emacs process visiting the same file
99 and doing the export from there.
100 Not all export commands are affected by this - only the ones which
101 actually write to a file, and that do not depend on the buffer state.
102 \\<org-mode-map>
103 If this option is nil, you can still get background export by calling
104 `org-export' with a double prefix arg: \
105 \\[universal-argument] \\[universal-argument] \\[org-export].
107 If this option is t, the double prefix can be used to exceptionally
108 force an export command into the current process."
109 :group 'org-export-general
110 :type 'boolean)
112 (defcustom org-export-select-tags '("export")
113 "Tags that select a tree for export.
114 If any such tag is found in a buffer, all trees that do not carry one
115 of these tags will be deleted before export.
116 Inside trees that are selected like this, you can still deselect a
117 subtree by tagging it with one of the `org-export-exclude-tags'."
118 :group 'org-export-general
119 :type '(repeat (string :tag "Tag")))
121 (defcustom org-export-exclude-tags '("noexport")
122 "Tags that exclude a tree from export.
123 All trees carrying any of these tags will be excluded from export.
124 This is without condition, so even subtrees inside that carry one of the
125 `org-export-select-tags' will be removed."
126 :group 'org-export-general
127 :type '(repeat (string :tag "Tag")))
129 ;; FIXME: rename, this is a general variable
130 (defcustom org-export-html-expand t
131 "Non-nil means for HTML export, treat @<...> as HTML tag.
132 When nil, these tags will be exported as plain text and therefore
133 not be interpreted by a browser.
135 This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
136 :group 'org-export-html
137 :group 'org-export-general
138 :type 'boolean)
140 (defcustom org-export-with-special-strings t
141 "Non-nil means interpret \"\-\", \"--\" and \"---\" for export.
142 When this option is turned on, these strings will be exported as:
144 Org HTML LaTeX
145 -----+----------+--------
146 \\- &shy; \\-
147 -- &ndash; --
148 --- &mdash; ---
149 ... &hellip; \ldots
151 This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
152 :group 'org-export-translation
153 :type 'boolean)
155 (defcustom org-export-html-link-up ""
156 "Where should the \"UP\" link of exported HTML pages lead?"
157 :group 'org-export-html
158 :group 'org-export-general
159 :type '(string :tag "File or URL"))
161 (defcustom org-export-html-link-home ""
162 "Where should the \"HOME\" link of exported HTML pages lead?"
163 :group 'org-export-html
164 :group 'org-export-general
165 :type '(string :tag "File or URL"))
167 (defcustom org-export-language-setup
168 '(("en" "Author" "Date" "Table of Contents" "Footnotes")
169 ("ca" "Autor" "Data" "&Iacute;ndex" "Peus de p&agrave;gina")
170 ("cs" "Autor" "Datum" "Obsah" "Pozn\xe1mky pod carou")
171 ("da" "Ophavsmand" "Dato" "Indhold" "Fodnoter")
172 ("de" "Autor" "Datum" "Inhaltsverzeichnis" "Fu&szlig;noten")
173 ("eo" "A&#365;toro" "Dato" "Enhavo" "Piednotoj")
174 ("es" "Autor" "Fecha" "&Iacute;ndice" "Pies de p&aacute;gina")
175 ("fi" "Tekij&auml;" "P&auml;iv&auml;m&auml;&auml;r&auml;" "Sis&auml;llysluettelo" "Alaviitteet")
176 ("fr" "Auteur" "Date" "Table des mati&egrave;res" "Notes de bas de page")
177 ("hu" "Szerz&otilde;" "D&aacute;tum" "Tartalomjegyz&eacute;k" "L&aacute;bjegyzet")
178 ("is" "H&ouml;fundur" "Dagsetning" "Efnisyfirlit" "Aftanm&aacute;lsgreinar")
179 ("it" "Autore" "Data" "Indice" "Note a pi&egrave; di pagina")
180 ("nl" "Auteur" "Datum" "Inhoudsopgave" "Voetnoten")
181 ("no" "Forfatter" "Dato" "Innhold" "Fotnoter")
182 ("nb" "Forfatter" "Dato" "Innhold" "Fotnoter") ;; nb = Norsk (bokm.l)
183 ("nn" "Forfattar" "Dato" "Innhald" "Fotnotar") ;; nn = Norsk (nynorsk)
184 ("pl" "Autor" "Data" "Spis tre&sacute;ci" "Przypis")
185 ("sv" "F&ouml;rfattare" "Datum" "Inneh&aring;ll" "Fotnoter"))
186 "Terms used in export text, translated to different languages.
187 Use the variable `org-export-default-language' to set the language,
188 or use the +OPTION lines for a per-file setting."
189 :group 'org-export-general
190 :type '(repeat
191 (list
192 (string :tag "HTML language tag")
193 (string :tag "Author")
194 (string :tag "Date")
195 (string :tag "Table of Contents")
196 (string :tag "Footnotes"))))
198 (defcustom org-export-default-language "en"
199 "The default language of HTML export, as a string.
200 This should have an association in `org-export-language-setup'."
201 :group 'org-export-general
202 :type 'string)
204 (defvar org-export-page-description ""
205 "The page description, for the XHTML meta tag.
206 This is best set with the #+DESCRIPTION line in a file, it does not make
207 sense to set this globally.")
209 (defvar org-export-page-keywords ""
210 "The page description, for the XHTML meta tag.
211 This is best set with the #+KEYWORDS line in a file, it does not make
212 sense to set this globally.")
214 (defcustom org-export-skip-text-before-1st-heading nil
215 "Non-nil means skip all text before the first headline when exporting.
216 When nil, that text is exported as well."
217 :group 'org-export-general
218 :type 'boolean)
220 (defcustom org-export-headline-levels 3
221 "The last level which is still exported as a headline.
222 Inferior levels will produce itemize lists when exported.
223 Note that a numeric prefix argument to an exporter function overrides
224 this setting.
226 This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
227 :group 'org-export-general
228 :type 'integer)
230 (defcustom org-export-with-section-numbers t
231 "Non-nil means add section numbers to headlines when exporting.
233 This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
234 :group 'org-export-general
235 :type 'boolean)
237 (defcustom org-export-section-number-format '((("1" ".")) . "")
238 "Format of section numbers for export.
239 The variable has two components.
240 1. A list of lists, each indicating a counter type and a separator.
241 The counter type can be any of \"1\", \"A\", \"a\", \"I\", or \"i\".
242 It causes causes numeric, alphabetic, or roman counters, respectively.
243 The separator is only used if another counter for a subsection is being
244 added.
245 If there are more numbered section levels than entries in this lists,
246 then the last entry will be reused.
247 2. A terminator string that will be added after the entire
248 section number."
249 :group 'org-export-general
250 :type '(cons
251 (repeat
252 (list
253 (string :tag "Counter Type")
254 (string :tag "Separator ")))
255 (string :tag "Terminator")))
257 (defcustom org-export-with-toc t
258 "Non-nil means create a table of contents in exported files.
259 The TOC contains headlines with levels up to`org-export-headline-levels'.
260 When an integer, include levels up to N in the toc, this may then be
261 different from `org-export-headline-levels', but it will not be allowed
262 to be larger than the number of headline levels.
263 When nil, no table of contents is made.
265 Headlines which contain any TODO items will be marked with \"(*)\" in
266 ASCII export, and with red color in HTML output, if the option
267 `org-export-mark-todo-in-toc' is set.
269 In HTML output, the TOC will be clickable.
271 This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
272 or \"toc:3\"."
273 :group 'org-export-general
274 :type '(choice
275 (const :tag "No Table of Contents" nil)
276 (const :tag "Full Table of Contents" t)
277 (integer :tag "TOC to level")))
279 (defcustom org-export-mark-todo-in-toc nil
280 "Non-nil means mark TOC lines that contain any open TODO items."
281 :group 'org-export-general
282 :type 'boolean)
284 (defcustom org-export-with-todo-keywords t
285 "Non-nil means include TODO keywords in export.
286 When nil, remove all these keywords from the export."
287 :group 'org-export-general
288 :type 'boolean)
290 (defcustom org-export-with-priority nil
291 "Non-nil means include priority cookies in export.
292 When nil, remove priority cookies for export."
293 :group 'org-export-general
294 :type 'boolean)
296 (defcustom org-export-preserve-breaks nil
297 "Non-nil means preserve all line breaks when exporting.
298 Normally, in HTML output paragraphs will be reformatted. In ASCII
299 export, line breaks will always be preserved, regardless of this variable.
301 This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
302 :group 'org-export-general
303 :type 'boolean)
305 (defcustom org-export-with-archived-trees 'headline
306 "Whether subtrees with the ARCHIVE tag should be exported.
307 This can have three different values
308 nil Do not export, pretend this tree is not present
309 t Do export the entire tree
310 headline Only export the headline, but skip the tree below it."
311 :group 'org-export-general
312 :group 'org-archive
313 :type '(choice
314 (const :tag "not at all" nil)
315 (const :tag "headline only" 'headline)
316 (const :tag "entirely" t)))
318 (defcustom org-export-author-info t
319 "Non-nil means insert author name and email into the exported file.
321 This option can also be set with the +OPTIONS line,
322 e.g. \"author:nil\"."
323 :group 'org-export-general
324 :type 'boolean)
326 (defcustom org-export-email-info nil
327 "Non-nil means insert author name and email into the exported file.
329 This option can also be set with the +OPTIONS line,
330 e.g. \"email:t\"."
331 :group 'org-export-general
332 :type 'boolean)
334 (defcustom org-export-creator-info t
335 "Non-nil means the postamble should contain a creator sentence.
336 This sentence is \"HTML generated by org-mode XX in emacs XXX\"."
337 :group 'org-export-general
338 :type 'boolean)
340 (defcustom org-export-time-stamp-file t
341 "Non-nil means insert a time stamp into the exported file.
342 The time stamp shows when the file was created.
344 This option can also be set with the +OPTIONS line,
345 e.g. \"timestamp:nil\"."
346 :group 'org-export-general
347 :type 'boolean)
349 (defcustom org-export-with-timestamps t
350 "If nil, do not export time stamps and associated keywords."
351 :group 'org-export-general
352 :type 'boolean)
354 (defcustom org-export-remove-timestamps-from-toc t
355 "If t, remove timestamps from the table of contents entries."
356 :group 'org-export-general
357 :type 'boolean)
359 (defcustom org-export-with-tags 'not-in-toc
360 "If nil, do not export tags, just remove them from headlines.
361 If this is the symbol `not-in-toc', tags will be removed from table of
362 contents entries, but still be shown in the headlines of the document.
364 This option can also be set with the +OPTIONS line, e.g. \"tags:nil\"."
365 :group 'org-export-general
366 :type '(choice
367 (const :tag "Off" nil)
368 (const :tag "Not in TOC" not-in-toc)
369 (const :tag "On" t)))
371 (defcustom org-export-with-drawers nil
372 "Non-nil means export with drawers like the property drawer.
373 When t, all drawers are exported. This may also be a list of
374 drawer names to export."
375 :group 'org-export-general
376 :type '(choice
377 (const :tag "All drawers" t)
378 (const :tag "None" nil)
379 (repeat :tag "Selected drawers"
380 (string :tag "Drawer name"))))
382 (defvar org-export-first-hook nil
383 "Hook called as the first thing in each exporter.
384 Point will be still in the original buffer.
385 Good for general initialization")
387 (defvar org-export-preprocess-hook nil
388 "Hook for preprocessing an export buffer.
389 Pretty much the first thing when exporting is running this hook.
390 Point will be in a temporary buffer that contains a copy of
391 the original buffer, or of the section that is being export.
392 All the other hooks in the org-export-preprocess... category
393 also work in that temporary buffer, already modified by various
394 stages of the processing.")
396 (defvar org-export-preprocess-after-include-files-hook nil
397 "Hook for preprocessing an export buffer.
398 This is run after the contents of included files have been inserted.")
400 (defvar org-export-preprocess-after-tree-selection-hook nil
401 "Hook for preprocessing an export buffer.
402 This is run after selection of trees to be exported has happened.
403 This selection includes tags-based selection, as well as removal
404 of commented and archived trees.")
406 (defvar org-export-preprocess-after-headline-targets-hook nil
407 "Hook for preprocessing export buffer.
408 This is run just after the headline targets have been defined and
409 the target-alist has been set up.")
411 (defvar org-export-preprocess-before-selecting-backend-code-hook nil
412 "Hook for preprocessing an export buffer.
413 This is run just before backend-specific blocks get selected.")
415 (defvar org-export-preprocess-after-blockquote-hook nil
416 "Hook for preprocessing an export buffer.
417 This is run after blockquote/quote/verse/center have been marked
418 with cookies.")
420 (defvar org-export-preprocess-after-radio-targets-hook nil
421 "Hook for preprocessing an export buffer.
422 This is run after radio target processing.")
424 (defvar org-export-preprocess-before-normalizing-links-hook nil
425 "Hook for preprocessing an export buffer.
426 This hook is run before links are normalized.")
428 (defvar org-export-preprocess-before-backend-specifics-hook nil
429 "Hook run before backend-specific functions are called during preprocessing.")
431 (defvar org-export-preprocess-final-hook nil
432 "Hook for preprocessing an export buffer.
433 This is run as the last thing in the preprocessing buffer, just before
434 returning the buffer string to the backend.")
436 (defgroup org-export-translation nil
437 "Options for translating special ascii sequences for the export backends."
438 :tag "Org Export Translation"
439 :group 'org-export)
441 (defcustom org-export-with-emphasize t
442 "Non-nil means interpret *word*, /word/, and _word_ as emphasized text.
443 If the export target supports emphasizing text, the word will be
444 typeset in bold, italic, or underlined, respectively. Works only for
445 single words, but you can say: I *really* *mean* *this*.
446 Not all export backends support this.
448 This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
449 :group 'org-export-translation
450 :type 'boolean)
452 (defcustom org-export-with-footnotes t
453 "If nil, export [1] as a footnote marker.
454 Lines starting with [1] will be formatted as footnotes.
456 This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
457 :group 'org-export-translation
458 :type 'boolean)
460 (defcustom org-export-with-TeX-macros t
461 "Non-nil means interpret simple TeX-like macros when exporting.
462 For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
463 Not only real TeX macros will work here, but the standard HTML entities
464 for math can be used as macro names as well. For a list of supported
465 names in HTML export, see the constant `org-entities' and the user option
466 `org-entities-user'.
467 Not all export backends support this.
469 This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
470 :group 'org-export-translation
471 :group 'org-export-latex
472 :type 'boolean)
474 (defcustom org-export-with-LaTeX-fragments t
475 "Non-nil means process LaTeX math fragments for HTML display.
476 When set, the exporter will find and process LaTeX environments if the
477 \\begin line is the first non-white thing on a line. It will also find
478 and process the math delimiters like $a=b$ and \\( a=b \\) for inline math,
479 $$a=b$$ and \\[ a=b \\] for display math.
481 This option can also be set with the +OPTIONS line, e.g. \"LaTeX:mathjax\".
483 Allowed values are:
485 nil Don't do anything.
486 verbatim Keep eveything in verbatim
487 dvipng Process the LaTeX fragments to images.
488 This will also include processing of non-math environments.
489 t Do MathJax preprocessing if there is at least on math snippet,
490 and arrange for MathJax.js to be loaded.
492 The default is nil, because this option needs the `dvipng' program which
493 is not available on all systems."
494 :group 'org-export-translation
495 :group 'org-export-latex
496 :type '(choice
497 (const :tag "Do not process math in any way" nil)
498 (const :tag "Obsolete, use dvipng setting" t)
499 (const :tag "Use dvipng to make images" dvipng)
500 (const :tag "Use MathJax to display math" mathjax)
501 (const :tag "Leave math verbatim" verbatim)))
503 (defcustom org-export-with-fixed-width t
504 "Non-nil means lines starting with \":\" will be in fixed width font.
505 This can be used to have pre-formatted text, fragments of code etc. For
506 example:
507 : ;; Some Lisp examples
508 : (while (defc cnt)
509 : (ding))
510 will be looking just like this in also HTML. See also the QUOTE keyword.
511 Not all export backends support this.
513 This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
514 :group 'org-export-translation
515 :type 'boolean)
517 (defgroup org-export-tables nil
518 "Options for exporting tables in Org-mode."
519 :tag "Org Export Tables"
520 :group 'org-export)
522 (defcustom org-export-with-tables t
523 "If non-nil, lines starting with \"|\" define a table.
524 For example:
526 | Name | Address | Birthday |
527 |-------------+----------+-----------|
528 | Arthur Dent | England | 29.2.2100 |
530 Not all export backends support this.
532 This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
533 :group 'org-export-tables
534 :type 'boolean)
536 (defcustom org-export-highlight-first-table-line t
537 "Non-nil means highlight the first table line.
538 In HTML export, this means use <th> instead of <td>.
539 In tables created with table.el, this applies to the first table line.
540 In Org-mode tables, all lines before the first horizontal separator
541 line will be formatted with <th> tags."
542 :group 'org-export-tables
543 :type 'boolean)
545 (defcustom org-export-table-remove-special-lines t
546 "Remove special lines and marking characters in calculating tables.
547 This removes the special marking character column from tables that are set
548 up for spreadsheet calculations. It also removes the entire lines
549 marked with `!', `_', or `^'. The lines with `$' are kept, because
550 the values of constants may be useful to have."
551 :group 'org-export-tables
552 :type 'boolean)
554 (defcustom org-export-prefer-native-exporter-for-tables nil
555 "Non-nil means always export tables created with table.el natively.
556 Natively means use the HTML code generator in table.el.
557 When nil, Org-mode's own HTML generator is used when possible (i.e. if
558 the table does not use row- or column-spanning). This has the
559 advantage, that the automatic HTML conversions for math symbols and
560 sub/superscripts can be applied. Org-mode's HTML generator is also
561 much faster. The LaTeX exporter always use the native exporter for
562 table.el tables."
563 :group 'org-export-tables
564 :type 'boolean)
567 (defgroup org-export-xml nil
568 "Options specific for XML export of Org-mode files."
569 :tag "Org Export XML"
570 :group 'org-export)
572 ;;;; Exporting
574 ;;; Variables, constants, and parameter plists
576 (defconst org-level-max 20)
578 (defvar org-current-export-file nil) ; dynamically scoped parameter
579 (defvar org-current-export-dir nil) ; dynamically scoped parameter
580 (defvar org-export-opt-plist nil
581 "Contains the current option plist.")
582 (defvar org-last-level nil) ; dynamically scoped variable
583 (defvar org-min-level nil) ; dynamically scoped variable
584 (defvar org-levels-open nil) ; dynamically scoped parameter
586 (defconst org-export-plist-vars
587 '((:link-up nil org-export-html-link-up)
588 (:link-home nil org-export-html-link-home)
589 (:language nil org-export-default-language)
590 (:keywords nil org-export-page-keywords)
591 (:description nil org-export-page-description)
592 (:customtime nil org-display-custom-times)
593 (:headline-levels "H" org-export-headline-levels)
594 (:section-numbers "num" org-export-with-section-numbers)
595 (:section-number-format nil org-export-section-number-format)
596 (:table-of-contents "toc" org-export-with-toc)
597 (:preserve-breaks "\\n" org-export-preserve-breaks)
598 (:archived-trees nil org-export-with-archived-trees)
599 (:emphasize "*" org-export-with-emphasize)
600 (:sub-superscript "^" org-export-with-sub-superscripts)
601 (:special-strings "-" org-export-with-special-strings)
602 (:footnotes "f" org-export-with-footnotes)
603 (:drawers "d" org-export-with-drawers)
604 (:tags "tags" org-export-with-tags)
605 (:todo-keywords "todo" org-export-with-todo-keywords)
606 (:priority "pri" org-export-with-priority)
607 (:TeX-macros "TeX" org-export-with-TeX-macros)
608 (:LaTeX-fragments "LaTeX" org-export-with-LaTeX-fragments)
609 (:latex-listings nil org-export-latex-listings)
610 (:skip-before-1st-heading "skip" org-export-skip-text-before-1st-heading)
611 (:fixed-width ":" org-export-with-fixed-width)
612 (:timestamps "<" org-export-with-timestamps)
613 (:author-info "author" org-export-author-info)
614 (:email-info "email" org-export-email-info)
615 (:creator-info "creator" org-export-creator-info)
616 (:time-stamp-file "timestamp" org-export-time-stamp-file)
617 (:tables "|" org-export-with-tables)
618 (:table-auto-headline nil org-export-highlight-first-table-line)
619 (:style-include-default nil org-export-html-style-include-default)
620 (:style-include-scripts nil org-export-html-style-include-scripts)
621 (:style nil org-export-html-style)
622 (:style-extra nil org-export-html-style-extra)
623 (:agenda-style nil org-agenda-export-html-style)
624 (:convert-org-links nil org-export-html-link-org-files-as-html)
625 (:inline-images nil org-export-html-inline-images)
626 (:html-extension nil org-export-html-extension)
627 (:xml-declaration nil org-export-html-xml-declaration)
628 (:html-table-tag nil org-export-html-table-tag)
629 (:expand-quoted-html "@" org-export-html-expand)
630 (:timestamp nil org-export-html-with-timestamp)
631 (:publishing-directory nil org-export-publishing-directory)
632 (:preamble nil org-export-html-preamble)
633 (:postamble nil org-export-html-postamble)
634 (:auto-preamble nil org-export-html-auto-preamble)
635 (:auto-postamble nil org-export-html-auto-postamble)
636 (:author nil user-full-name)
637 (:email nil user-mail-address)
638 (:select-tags nil org-export-select-tags)
639 (:exclude-tags nil org-export-exclude-tags)
641 (:latex-image-options nil org-export-latex-image-default-option))
642 "List of properties that represent export/publishing variables.
643 Each element is a list of 3 items:
644 1. The property that is used internally, and also for org-publish-project-alist
645 2. The string that can be used in the OPTION lines to set this option,
646 or nil if this option cannot be changed in this way
647 3. The customization variable that sets the default for this option."
650 (defun org-default-export-plist ()
651 "Return the property list with default settings for the export variables."
652 (let* ((infile (org-infile-export-plist))
653 (letbind (plist-get infile :let-bind))
654 (l org-export-plist-vars) rtn e s v)
655 (while (setq e (pop l))
656 (setq s (nth 2 e)
657 v (cond
658 ((assq s letbind) (nth 1 (assq s letbind)))
659 ((boundp s) (symbol-value s))
660 (t nil))
661 rtn (cons (car e) (cons v rtn))))
662 rtn))
664 (defvar org-export-inbuffer-options-extra nil
665 "List of additional in-buffer options that should be detected.
666 Just before export, the buffer is scanned for options like #+TITLE, #+EMAIL,
667 etc. Extensions can add to this list to get their options detected, and they
668 can then add a function to `org-export-options-filters' to process these
669 options.
670 Each element in this list must be a list, with the in-buffer keyword as car,
671 and a property (a symbol) as the next element. All occurrences of the
672 keyword will be found, the values concatenated with a space character
673 in between, and the result stored in the export options property list.")
675 (defvar org-export-options-filters nil
676 "Functions to be called to finalize the export/publishing options.
677 All these options are stored in a property list, and each of the functions
678 in this hook gets a chance to modify this property list. Each function
679 must accept the property list as an argument, and must return the (possibly
680 modified) list.")
682 ;; FIXME: should we fold case here?
683 (defun org-infile-export-plist ()
684 "Return the property list with file-local settings for export."
685 (save-excursion
686 (save-restriction
687 (widen)
688 (goto-char (point-min))
689 (let ((re (org-make-options-regexp
690 (append
691 '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE"
692 "MATHJAX"
693 "LINK_UP" "LINK_HOME" "SETUPFILE" "STYLE"
694 "LATEX_HEADER" "LATEX_CLASS"
695 "EXPORT_SELECT_TAGS" "EXPORT_EXCLUDE_TAGS"
696 "KEYWORDS" "DESCRIPTION" "MACRO" "BIND" "XSLT")
697 (mapcar 'car org-export-inbuffer-options-extra))))
698 (case-fold-search t)
699 p key val text options mathjax a pr style
700 latex-header latex-class macros letbind
701 ext-setup-or-nil setup-contents (start 0))
702 (while (or (and ext-setup-or-nil
703 (string-match re ext-setup-or-nil start)
704 (setq start (match-end 0)))
705 (and (setq ext-setup-or-nil nil start 0)
706 (re-search-forward re nil t)))
707 (setq key (upcase (org-match-string-no-properties 1 ext-setup-or-nil))
708 val (org-match-string-no-properties 2 ext-setup-or-nil))
709 (cond
710 ((setq a (assoc key org-export-inbuffer-options-extra))
711 (setq pr (nth 1 a))
712 (setq p (plist-put p pr (concat (plist-get p pr) " " val))))
713 ((string-equal key "TITLE") (setq p (plist-put p :title val)))
714 ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
715 ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
716 ((string-equal key "DATE") (setq p (plist-put p :date val)))
717 ((string-equal key "KEYWORDS") (setq p (plist-put p :keywords val)))
718 ((string-equal key "DESCRIPTION")
719 (setq p (plist-put p :description val)))
720 ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
721 ((string-equal key "STYLE")
722 (setq style (concat style "\n" val)))
723 ((string-equal key "LATEX_HEADER")
724 (setq latex-header (concat latex-header "\n" val)))
725 ((string-equal key "LATEX_CLASS")
726 (setq latex-class val))
727 ((string-equal key "TEXT")
728 (setq text (if text (concat text "\n" val) val)))
729 ((string-equal key "OPTIONS")
730 (setq options (concat val " " options)))
731 ((string-equal key "MATHJAX")
732 (setq mathjax (concat val " " mathjax)))
733 ((string-equal key "BIND")
734 (push (read (concat "(" val ")")) letbind))
735 ((string-equal key "XSLT")
736 (setq p (plist-put p :xslt val)))
737 ((string-equal key "LINK_UP")
738 (setq p (plist-put p :link-up val)))
739 ((string-equal key "LINK_HOME")
740 (setq p (plist-put p :link-home val)))
741 ((string-equal key "EXPORT_SELECT_TAGS")
742 (setq p (plist-put p :select-tags (org-split-string val))))
743 ((string-equal key "EXPORT_EXCLUDE_TAGS")
744 (setq p (plist-put p :exclude-tags (org-split-string val))))
745 ((string-equal key "MACRO")
746 (push val macros))
747 ((equal key "SETUPFILE")
748 (setq setup-contents (org-file-contents
749 (expand-file-name
750 (org-remove-double-quotes
751 (org-trim val)))
752 'noerror))
753 (if (not ext-setup-or-nil)
754 (setq ext-setup-or-nil setup-contents start 0)
755 (setq ext-setup-or-nil
756 (concat (substring ext-setup-or-nil 0 start)
757 "\n" setup-contents "\n"
758 (substring ext-setup-or-nil start)))))))
759 (setq p (plist-put p :text text))
760 (when (and letbind (org-export-confirm-letbind))
761 (setq p (plist-put p :let-bind letbind)))
762 (when style (setq p (plist-put p :style-extra style)))
763 (when latex-header
764 (setq p (plist-put p :latex-header-extra (substring latex-header 1))))
765 (when latex-class
766 (setq p (plist-put p :latex-class latex-class)))
767 (when options
768 (setq p (org-export-add-options-to-plist p options)))
769 (when mathjax
770 (setq p (plist-put p :mathjax mathjax)))
771 ;; Add macro definitions
772 (setq p (plist-put p :macro-date "(eval (format-time-string \"$1\"))"))
773 (setq p (plist-put p :macro-time "(eval (format-time-string \"$1\"))"))
774 (setq p (plist-put
775 p :macro-modification-time
776 (and (buffer-file-name)
777 (file-exists-p (buffer-file-name))
778 (concat
779 "(eval (format-time-string \"$1\" '"
780 (prin1-to-string (nth 5 (file-attributes
781 (buffer-file-name))))
782 "))"))))
783 (setq p (plist-put p :macro-input-file (and (buffer-file-name)
784 (file-name-nondirectory
785 (buffer-file-name)))))
786 (while (setq val (pop macros))
787 (when (string-match "^\\([-a-zA-Z0-9_]+\\)[ \t]+\\(.*?[ \t]*$\\)" val)
788 (setq p (plist-put
789 p (intern
790 (concat ":macro-" (downcase (match-string 1 val))))
791 (org-export-interpolate-newlines (match-string 2 val))))))
792 p))))
794 (defun org-export-interpolate-newlines (s)
795 (while (string-match "\\\\n" s)
796 (setq s (replace-match "\n" t t s)))
799 (defvar org-export-allow-BIND-local nil)
800 (defun org-export-confirm-letbind ()
801 "Can we use #+BIND values during export?
802 By default this will ask fro confirmation by the user, to divert possible
803 security risks."
804 (cond
805 ((not org-export-allow-BIND) nil)
806 ((eq org-export-allow-BIND t) t)
807 ((local-variable-p 'org-export-allow-BIND-local (current-buffer))
808 org-export-allow-BIND-local)
809 (t (org-set-local 'org-export-allow-BIND-local
810 (yes-or-no-p "Allow BIND values in this buffer? ")))))
812 (defun org-install-letbind ()
813 "Install the values from #+BIND lines as local variables."
814 (let ((letbind (plist-get org-export-opt-plist :let-bind))
815 pair)
816 (while (setq pair (pop letbind))
817 (org-set-local (car pair) (nth 1 pair)))))
819 (defun org-export-add-options-to-plist (p options)
820 "Parse an OPTIONS line and set values in the property list P."
821 (let (o)
822 (when options
823 (let ((op org-export-plist-vars))
824 (while (setq o (pop op))
825 (if (and (nth 1 o)
826 (string-match (concat (regexp-quote (nth 1 o))
827 ":\\([^ \t\n\r;,.]*\\)")
828 options))
829 (setq p (plist-put p (car o)
830 (car (read-from-string
831 (match-string 1 options))))))))))
834 (defun org-export-add-subtree-options (p pos)
835 "Add options in subtree at position POS to property list P."
836 (save-excursion
837 (goto-char pos)
838 (when (org-at-heading-p)
839 (let (a)
840 ;; This is actually read in `org-export-get-title-from-subtree'
841 ;; (when (setq a (org-entry-get pos "EXPORT_TITLE"))
842 ;; (setq p (plist-put p :title a)))
843 (when (setq a (org-entry-get pos "EXPORT_TEXT"))
844 (setq p (plist-put p :text a)))
845 (when (setq a (org-entry-get pos "EXPORT_AUTHOR"))
846 (setq p (plist-put p :author a)))
847 (when (setq a (org-entry-get pos "EXPORT_DATE"))
848 (setq p (plist-put p :date a)))
849 (when (setq a (org-entry-get pos "EXPORT_OPTIONS"))
850 (setq p (org-export-add-options-to-plist p a)))))
853 (defun org-export-directory (type plist)
854 (let* ((val (plist-get plist :publishing-directory))
855 (dir (if (listp val)
856 (or (cdr (assoc type val)) ".")
857 val)))
858 dir))
860 (defun org-export-process-option-filters (plist)
861 (let ((functions org-export-options-filters) f)
862 (while (setq f (pop functions))
863 (setq plist (funcall f plist))))
864 plist)
866 ;;;###autoload
867 (defun org-export (&optional arg)
868 "Export dispatcher for Org-mode.
869 When `org-export-run-in-background' is non-nil, try to run the command
870 in the background. This will be done only for commands that write
871 to a file. For details see the docstring of `org-export-run-in-background'.
873 The prefix argument ARG will be passed to the exporter. However, if
874 ARG is a double universal prefix \\[universal-argument] \\[universal-argument], \
875 that means to inverse the
876 value of `org-export-run-in-background'."
877 (interactive "P")
878 (let* ((bg (org-xor (equal arg '(16)) org-export-run-in-background))
879 subtree-p
880 (help "[t] insert the export option template
881 \[v] limit export to visible part of outline tree
882 \[1] only export the current subtree
883 \[SPC] publish enclosing subtree (with LaTeX_CLASS or EXPORT_FILE_NAME prop)
885 \[a/n/u] export as ASCII/Latin-1/UTF-8 [A/N/U] to temporary buffer
887 \[h] export as HTML [H] to temporary buffer [R] export region
888 \[b] export as HTML and open in browser
890 \[l] export as LaTeX [L] to temporary buffer
891 \[p] export as LaTeX and process to PDF [d] ... and open PDF file
893 \[D] export as DocBook [V] export as DocBook, process to PDF, and open
895 \[j] export as TaskJuggler [J] ... and open
897 \[m] export as Freemind mind map
898 \[x] export as XOXO
899 \[g] export using Wes Hardaker's generic exporter
901 \[i] export current file as iCalendar file
902 \[I] export all agenda files as iCalendar files [c] ...as one combined file
904 \[F] publish current file [P] publish current project
905 \[X] publish a project... [E] publish every projects")
906 (cmds
907 '((?t org-insert-export-options-template nil)
908 (?v org-export-visible nil)
909 (?a org-export-as-ascii t)
910 (?A org-export-as-ascii-to-buffer t)
911 (?n org-export-as-latin1 t)
912 (?N org-export-as-latin1-to-buffer t)
913 (?u org-export-as-utf8 t)
914 (?U org-export-as-utf8-to-buffer t)
915 (?h org-export-as-html t)
916 (?b org-export-as-html-and-open t)
917 (?H org-export-as-html-to-buffer nil)
918 (?R org-export-region-as-html nil)
919 (?x org-export-as-xoxo t)
920 (?g org-export-generic t)
921 (?D org-export-as-docbook t)
922 (?V org-export-as-docbook-pdf-and-open t)
923 (?j org-export-as-taskjuggler t)
924 (?J org-export-as-taskjuggler-and-open t)
925 (?m org-export-as-freemind t)
926 (?l org-export-as-latex t)
927 (?p org-export-as-pdf t)
928 (?d org-export-as-pdf-and-open t)
929 (?L org-export-as-latex-to-buffer nil)
930 (?i org-export-icalendar-this-file t)
931 (?I org-export-icalendar-all-agenda-files t)
932 (?c org-export-icalendar-combine-agenda-files t)
933 (?F org-publish-current-file t)
934 (?P org-publish-current-project t)
935 (?X org-publish t)
936 (?E org-publish-all t)))
937 r1 r2 ass
938 (cpos (point)) (cbuf (current-buffer)) bpos)
939 (save-excursion
940 (save-window-excursion
941 (delete-other-windows)
942 (with-output-to-temp-buffer "*Org Export/Publishing Help*"
943 (princ help))
944 (org-fit-window-to-buffer (get-buffer-window
945 "*Org Export/Publishing Help*"))
946 (message "Select command: ")
947 (setq r1 (read-char-exclusive))
948 (when (eq r1 ?1)
949 (setq subtree-p t)
950 (message "Select command (for subtree): ")
951 (setq r1 (read-char-exclusive)))
952 (when (eq r1 ?\ )
953 (let ((case-fold-search t))
954 (if (re-search-backward
955 "^[ \t]+\\(:latex_class:\\|:export_title:\\)[ \t]+\\S-"
956 nil t)
957 (progn
958 (org-back-to-heading t)
959 (setq subtree-p t)
960 (setq bpos (point))
961 (message "Select command (for subtree): ")
962 (setq r1 (read-char-exclusive)))
963 (error "No enclosing node with LaTeX_CLASS or EXPORT_FILE_NAME")
964 )))))
965 (and bpos (goto-char bpos))
966 (setq r2 (if (< r1 27) (+ r1 96) r1))
967 (unless (setq ass (assq r2 cmds))
968 (error "No command associated with key %c" r1))
969 (if (and bg (nth 2 ass)
970 (not (buffer-base-buffer))
971 (not (org-region-active-p)))
972 ;; execute in background
973 (let ((p (start-process
974 (concat "Exporting " (file-name-nondirectory (buffer-file-name)))
975 "*Org Processes*"
976 (expand-file-name invocation-name invocation-directory)
977 "-batch"
978 "-l" user-init-file
979 "--eval" "(require 'org-exp)"
980 "--eval" "(setq org-wait .2)"
981 (buffer-file-name)
982 "-f" (symbol-name (nth 1 ass)))))
983 (set-process-sentinel p 'org-export-process-sentinel)
984 (message "Background process \"%s\": started" p))
985 ;; background processing not requested, or not possible
986 (if subtree-p (progn (outline-mark-subtree) (activate-mark)))
987 (call-interactively (nth 1 ass))
988 (when (and bpos (get-buffer-window cbuf))
989 (let ((cw (selected-window)))
990 (select-window (get-buffer-window cbuf))
991 (goto-char cpos)
992 (deactivate-mark)
993 (select-window cw))))))
995 (defun org-export-process-sentinel (process status)
996 (if (string-match "\n+\\'" status)
997 (setq status (substring status 0 -1)))
998 (message "Background process \"%s\": %s" process status))
1000 ;;; General functions for all backends
1002 (defvar org-export-target-aliases nil
1003 "Alist of targets with invisible aliases.")
1004 (defvar org-export-preferred-target-alist nil
1005 "Alist of section id's with preferred aliases.")
1006 (defvar org-export-id-target-alist nil
1007 "Alist of section id's with preferred aliases.")
1008 (defvar org-export-code-refs nil
1009 "Alist of code references and line numbers.")
1011 (defun org-export-preprocess-string (string &rest parameters)
1012 "Cleanup STRING so that that the true exported has a more consistent source.
1013 This function takes STRING, which should be a buffer-string of an org-file
1014 to export. It then creates a temporary buffer where it does its job.
1015 The result is then again returned as a string, and the exporter works
1016 on this string to produce the exported version."
1017 (interactive)
1018 (let* ((htmlp (plist-get parameters :for-html))
1019 (asciip (plist-get parameters :for-ascii))
1020 (latexp (plist-get parameters :for-LaTeX))
1021 (docbookp (plist-get parameters :for-docbook))
1022 (backend (cond (htmlp 'html)
1023 (latexp 'latex)
1024 (asciip 'ascii)
1025 (docbookp 'docbook)))
1026 (archived-trees (plist-get parameters :archived-trees))
1027 (inhibit-read-only t)
1028 (drawers org-drawers)
1029 (outline-regexp "\\*+ ")
1030 target-alist rtn)
1032 (setq org-export-target-aliases nil
1033 org-export-preferred-target-alist nil
1034 org-export-id-target-alist nil
1035 org-export-code-refs nil)
1037 (with-current-buffer (get-buffer-create " org-mode-tmp")
1038 (erase-buffer)
1039 (insert string)
1040 (setq case-fold-search t)
1042 (let ((inhibit-read-only t))
1043 (remove-text-properties (point-min) (point-max)
1044 '(read-only t)))
1046 ;; Remove license-to-kill stuff
1047 ;; The caller marks some stuff for killing, stuff that has been
1048 ;; used to create the page title, for example.
1049 (org-export-kill-licensed-text)
1051 (let ((org-inhibit-startup t)) (org-mode))
1052 (setq case-fold-search t)
1053 (org-install-letbind)
1055 ;; Call the hook
1056 (run-hooks 'org-export-preprocess-hook)
1058 ;; Process the macros
1059 (org-export-preprocess-apply-macros)
1060 (run-hooks 'org-export-preprocess-after-macros-hook)
1062 (untabify (point-min) (point-max))
1064 ;; Handle include files, and call a hook
1065 (org-export-handle-include-files-recurse)
1066 (run-hooks 'org-export-preprocess-after-include-files-hook)
1068 ;; Get rid of archived trees
1069 (org-export-remove-archived-trees archived-trees)
1071 ;; Remove comment environment and comment subtrees
1072 (org-export-remove-comment-blocks-and-subtrees)
1074 ;; Get rid of excluded trees, and call a hook
1075 (org-export-handle-export-tags (plist-get parameters :select-tags)
1076 (plist-get parameters :exclude-tags))
1077 (run-hooks 'org-export-preprocess-after-tree-selection-hook)
1079 ;; Mark end of lists
1080 (org-export-mark-list-ending backend)
1082 ;; Handle source code snippets
1083 (org-export-replace-src-segments-and-examples backend)
1085 ;; Protect short examples marked by a leading colon
1086 (org-export-protect-colon-examples)
1088 ;; Normalize footnotes
1089 (when (plist-get parameters :footnotes)
1090 (org-footnote-normalize nil t))
1092 ;; Find all headings and compute the targets for them
1093 (setq target-alist (org-export-define-heading-targets target-alist))
1095 (run-hooks 'org-export-preprocess-after-headline-targets-hook)
1097 ;; Find HTML special classes for headlines
1098 (org-export-remember-html-container-classes)
1100 ;; Get rid of drawers
1101 (org-export-remove-or-extract-drawers
1102 drawers (plist-get parameters :drawers) backend)
1104 ;; Get the correct stuff before the first headline
1105 (when (plist-get parameters :skip-before-1st-heading)
1106 (goto-char (point-min))
1107 (when (re-search-forward "^\\(#.*\n\\)?\\*+[ \t]" nil t)
1108 (delete-region (point-min) (match-beginning 0))
1109 (goto-char (point-min))
1110 (insert "\n")))
1111 (when (plist-get parameters :add-text)
1112 (goto-char (point-min))
1113 (insert (plist-get parameters :add-text) "\n"))
1115 ;; Remove todo-keywords before exporting, if the user has requested so
1116 (org-export-remove-headline-metadata parameters)
1118 ;; Find targets in comments and move them out of comments,
1119 ;; but mark them as targets that should be invisible
1120 (setq target-alist (org-export-handle-invisible-targets target-alist))
1122 ;; Select and protect backend specific stuff, throw away stuff
1123 ;; that is specific for other backends
1124 (run-hooks 'org-export-preprocess-before-selecting-backend-code-hook)
1125 (org-export-select-backend-specific-text backend)
1127 ;; Protect quoted subtrees
1128 (org-export-protect-quoted-subtrees)
1130 ;; Remove clock lines
1131 (org-export-remove-clock-lines)
1133 ;; Protect verbatim elements
1134 (org-export-protect-verbatim)
1136 ;; Blockquotes, verse, and center
1137 (org-export-mark-blockquote-verse-center)
1138 (run-hooks 'org-export-preprocess-after-blockquote-hook)
1140 ;; Remove timestamps, if the user has requested so
1141 (unless (plist-get parameters :timestamps)
1142 (org-export-remove-timestamps))
1144 ;; Attach captions to the correct object
1145 (setq target-alist (org-export-attach-captions-and-attributes
1146 backend target-alist))
1148 ;; Find matches for radio targets and turn them into internal links
1149 (org-export-mark-radio-links)
1150 (run-hooks 'org-export-preprocess-after-radio-targets-hook)
1152 ;; Find all links that contain a newline and put them into a single line
1153 (org-export-concatenate-multiline-links)
1155 ;; Normalize links: Convert angle and plain links into bracket links
1156 ;; and expand link abbreviations
1157 (run-hooks 'org-export-preprocess-before-normalizing-links-hook)
1158 (org-export-normalize-links)
1160 ;; Find all internal links. If they have a fuzzy match (i.e. not
1161 ;; a *dedicated* target match, let the link point to the
1162 ;; corresponding section.
1163 (org-export-target-internal-links target-alist)
1165 ;; Find multiline emphasis and put them into single line
1166 (when (plist-get parameters :emph-multiline)
1167 (org-export-concatenate-multiline-emphasis))
1169 ;; Remove special table lines
1170 (when org-export-table-remove-special-lines
1171 (org-export-remove-special-table-lines))
1173 ;; Another hook
1174 (run-hooks 'org-export-preprocess-before-backend-specifics-hook)
1176 ;; LaTeX-specific preprocessing
1177 (when latexp
1178 (require 'org-latex nil)
1179 (org-export-latex-preprocess parameters))
1181 ;; ASCII-specific preprocessing
1182 (when asciip
1183 (org-export-ascii-preprocess parameters))
1185 ;; HTML-specific preprocessing
1186 (when htmlp
1187 (org-export-html-preprocess parameters))
1189 ;; DocBook-specific preprocessing
1190 (when docbookp
1191 (require 'org-docbook nil)
1192 (org-export-docbook-preprocess parameters))
1194 ;; Remove or replace comments
1195 (org-export-handle-comments (plist-get parameters :comments))
1197 ;; Remove #+TBLFM and #+TBLNAME lines
1198 (org-export-handle-table-metalines)
1200 ;; Run the final hook
1201 (run-hooks 'org-export-preprocess-final-hook)
1203 (setq rtn (buffer-string)))
1204 (kill-buffer " org-mode-tmp")
1205 rtn))
1207 (defun org-export-kill-licensed-text ()
1208 "Remove all text that is marked with a :org-license-to-kill property."
1209 (let (p)
1210 (while (setq p (text-property-any (point-min) (point-max)
1211 :org-license-to-kill t))
1212 (delete-region
1213 p (or (next-single-property-change p :org-license-to-kill)
1214 (point-max))))))
1216 (defvar org-export-define-heading-targets-headline-hook nil
1217 "Hook that is run when a headline was matched during target search.
1218 This is part of the preprocessing for export.")
1220 (defun org-export-define-heading-targets (target-alist)
1221 "Find all headings and define the targets for them.
1222 The new targets are added to TARGET-ALIST, which is also returned.
1223 Also find all ID and CUSTOM_ID properties and store them."
1224 (goto-char (point-min))
1225 (org-init-section-numbers)
1226 (let ((re (concat "^" org-outline-regexp
1227 "\\|"
1228 "^[ \t]*:\\(ID\\|CUSTOM_ID\\):[ \t]*\\([^ \t\r\n]+\\)"))
1229 level target last-section-target a id)
1230 (while (re-search-forward re nil t)
1231 (org-if-unprotected-at (match-beginning 0)
1232 (if (match-end 2)
1233 (progn
1234 (setq id (org-match-string-no-properties 2))
1235 (push (cons id target) target-alist)
1236 (setq a (or (assoc last-section-target org-export-target-aliases)
1237 (progn
1238 (push (list last-section-target)
1239 org-export-target-aliases)
1240 (car org-export-target-aliases))))
1241 (push (caar target-alist) (cdr a))
1242 (when (equal (match-string 1) "CUSTOM_ID")
1243 (if (not (assoc last-section-target
1244 org-export-preferred-target-alist))
1245 (push (cons last-section-target id)
1246 org-export-preferred-target-alist)))
1247 (when (equal (match-string 1) "ID")
1248 (if (not (assoc last-section-target
1249 org-export-id-target-alist))
1250 (push (cons last-section-target (concat "ID-" id))
1251 org-export-id-target-alist))))
1252 (setq level (org-reduced-level
1253 (save-excursion (goto-char (point-at-bol))
1254 (org-outline-level))))
1255 (setq target (org-solidify-link-text
1256 (format "sec-%s" (replace-regexp-in-string
1257 "\\." "_"
1258 (org-section-number level)))))
1259 (setq last-section-target target)
1260 (push (cons target target) target-alist)
1261 (add-text-properties
1262 (point-at-bol) (point-at-eol)
1263 (list 'target target))
1264 (run-hooks 'org-export-define-heading-targets-headline-hook)))))
1265 target-alist)
1267 (defun org-export-handle-invisible-targets (target-alist)
1268 "Find targets in comments and move them out of comments.
1269 Mark them as invisible targets."
1270 (let (target tmp a)
1271 (goto-char (point-min))
1272 (while (re-search-forward "^#.*?\\(<<<?\\([^>\r\n]+\\)>>>?\\).*" nil t)
1273 ;; Check if the line before or after is a headline with a target
1274 (if (setq target (or (get-text-property (point-at-bol 0) 'target)
1275 (get-text-property (point-at-bol 2) 'target)))
1276 (progn
1277 ;; use the existing target in a neighboring line
1278 (setq tmp (match-string 2))
1279 (replace-match "")
1280 (and (looking-at "\n") (delete-char 1))
1281 (push (cons (setq tmp (org-solidify-link-text tmp)) target)
1282 target-alist)
1283 (setq a (or (assoc target org-export-target-aliases)
1284 (progn
1285 (push (list target) org-export-target-aliases)
1286 (car org-export-target-aliases))))
1287 (push tmp (cdr a)))
1288 ;; Make an invisible target
1289 (replace-match "\\1(INVISIBLE)"))))
1290 target-alist)
1292 (defun org-export-target-internal-links (target-alist)
1293 "Find all internal links and assign targets to them.
1294 If a link has a fuzzy match (i.e. not a *dedicated* target match),
1295 let the link point to the corresponding section.
1296 This function also handles the id links, if they have a match in
1297 the current file."
1298 (goto-char (point-min))
1299 (while (re-search-forward org-bracket-link-regexp nil t)
1300 (org-if-unprotected-at (1+ (match-beginning 0))
1301 (let* ((md (match-data))
1302 (desc (match-end 2))
1303 (link (org-link-unescape (match-string 1)))
1304 (slink (org-solidify-link-text link))
1305 found props pos cref
1306 (target
1307 (cond
1308 ((= (string-to-char link) ?#)
1309 ;; user wants exactly this link
1310 link)
1311 ((cdr (assoc slink target-alist))
1312 (or (cdr (assoc (assoc slink target-alist)
1313 org-export-preferred-target-alist))
1314 (cdr (assoc slink target-alist))))
1315 ((and (string-match "^id:" link)
1316 (cdr (assoc (substring link 3) target-alist))))
1317 ((string-match "^(\\(.*\\))$" link)
1318 (setq cref (match-string 1 link))
1319 (concat "coderef:" cref))
1320 ((string-match org-link-types-re link) nil)
1321 ((or (file-name-absolute-p link)
1322 (string-match "^\\." link))
1323 nil)
1325 (let ((org-link-search-inhibit-query t))
1326 (save-excursion
1327 (setq found (condition-case nil (org-link-search link)
1328 (error nil)))
1329 (when (and found
1330 (or (org-on-heading-p)
1331 (not (eq found 'dedicated))))
1332 (or (get-text-property (point) 'target)
1333 (get-text-property
1334 (max (point-min)
1335 (1- (or (previous-single-property-change
1336 (point) 'target) 0)))
1337 'target)))))))))
1338 (when target
1339 (set-match-data md)
1340 (goto-char (match-beginning 1))
1341 (setq props (text-properties-at (point)))
1342 (delete-region (match-beginning 1) (match-end 1))
1343 (setq pos (point))
1344 (insert target)
1345 (unless desc (insert "][" link))
1346 (add-text-properties pos (point) props))))))
1348 (defun org-export-remember-html-container-classes ()
1349 "Store the HTML_CONTAINER_CLASS properties in a text property."
1350 (goto-char (point-min))
1351 (let (class)
1352 (while (re-search-forward
1353 "^[ \t]*:HTML_CONTAINER_CLASS:[ \t]+\\(.+\\)$" nil t)
1354 (setq class (match-string 1))
1355 (save-excursion
1356 (org-back-to-heading t)
1357 (put-text-property (point-at-bol) (point-at-eol) 'html-container-class class)))))
1359 (defvar org-export-format-drawer-function nil
1360 "Function to be called to format the contents of a drawer.
1361 The function must accept three parameters:
1362 NAME the drawer name, like \"PROPERTIES\"
1363 CONTENT the content of the drawer.
1364 BACKEND one of the symbols html, docbook, latex, ascii, xoxo
1365 The function should return the text to be inserted into the buffer.
1366 If this is nil, `org-export-format-drawer' is used as a default.")
1368 (defun org-export-remove-or-extract-drawers (all-drawers exp-drawers backend)
1369 "Remove drawers, or extract and format the content.
1370 ALL-DRAWERS is a list of all drawer names valid in the current buffer.
1371 EXP-DRAWERS can be t to keep all drawer contents, or a list of drawers
1372 whose content to keep. Any drawers that are in ALL-DRAWERS but not in
1373 EXP-DRAWERS will be removed.
1374 BACKEND is the current export backend."
1375 (goto-char (point-min))
1376 (let ((re (concat "^[ \t]*:\\("
1377 (mapconcat 'identity all-drawers "\\|")
1378 "\\):[ \t]*$"))
1379 name beg beg-content eol content)
1380 (while (re-search-forward re nil t)
1381 (org-if-unprotected
1382 (setq name (match-string 1))
1383 (setq beg (match-beginning 0)
1384 beg-content (1+ (point-at-eol))
1385 eol (point-at-eol))
1386 (if (not (and (re-search-forward
1387 "^\\([ \t]*:END:[ \t]*\n?\\)\\|^\\*+[ \t]" nil t)
1388 (match-end 1)))
1389 (goto-char eol)
1390 (goto-char (match-beginning 0))
1391 (and (looking-at ".*\n?") (replace-match ""))
1392 (setq content (buffer-substring beg-content (point)))
1393 (delete-region beg (point))
1394 (when (or (eq exp-drawers t)
1395 (member name exp-drawers))
1396 (setq content (funcall (or org-export-format-drawer-function
1397 'org-export-format-drawer)
1398 name content backend))
1399 (insert content)))))))
1401 (defun org-export-format-drawer (name content backend)
1402 "Format the content of a drawer as a colon example."
1403 (if (string-match "[ \t]+\\'" content)
1404 (setq content (substring content (match-beginning 0))))
1405 (while (string-match "\\`[ \t]*\n" content)
1406 (setq content (substring content (match-end 0))))
1407 (setq content (org-remove-indentation content))
1408 (setq content (concat ": " (mapconcat 'identity
1409 (org-split-string content "\n")
1410 "\n: ")
1411 "\n"))
1412 (setq content (concat " : " (upcase name) "\n" content))
1413 (org-add-props content nil 'org-protected t))
1415 (defun org-export-handle-export-tags (select-tags exclude-tags)
1416 "Modify the buffer, honoring SELECT-TAGS and EXCLUDE-TAGS.
1417 Both arguments are lists of tags.
1418 If any of SELECT-TAGS is found, all trees not marked by a SELECT-TAG
1419 will be removed.
1420 After that, all subtrees that are marked by EXCLUDE-TAGS will be
1421 removed as well."
1422 (remove-text-properties (point-min) (point-max) '(:org-delete t))
1423 (let* ((re-sel (concat ":\\(" (mapconcat 'regexp-quote
1424 select-tags "\\|")
1425 "\\):"))
1426 (re-excl (concat ":\\(" (mapconcat 'regexp-quote
1427 exclude-tags "\\|")
1428 "\\):"))
1429 beg end cont)
1430 (goto-char (point-min))
1431 (when (and select-tags
1432 (re-search-forward
1433 (concat "^\\*+[ \t].*" re-sel "[^ \t\n]*[ \t]*$") nil t))
1434 ;; At least one tree is marked for export, this means
1435 ;; all the unmarked stuff needs to go.
1436 ;; Dig out the trees that should be exported
1437 (goto-char (point-min))
1438 (outline-next-heading)
1439 (setq beg (point))
1440 (put-text-property beg (point-max) :org-delete t)
1441 (while (re-search-forward re-sel nil t)
1442 (when (org-on-heading-p)
1443 (org-back-to-heading)
1444 (remove-text-properties
1445 (max (1- (point)) (point-min))
1446 (setq cont (save-excursion (org-end-of-subtree t t)))
1447 '(:org-delete t))
1448 (while (and (org-up-heading-safe)
1449 (get-text-property (point) :org-delete))
1450 (remove-text-properties (max (1- (point)) (point-min))
1451 (point-at-eol) '(:org-delete t)))
1452 (goto-char cont))))
1453 ;; Remove the trees explicitly marked for noexport
1454 (when exclude-tags
1455 (goto-char (point-min))
1456 (while (re-search-forward re-excl nil t)
1457 (when (org-at-heading-p)
1458 (org-back-to-heading t)
1459 (setq beg (point))
1460 (org-end-of-subtree t t)
1461 (delete-region beg (point))
1462 (when (featurep 'org-inlinetask)
1463 (org-inlinetask-remove-END-maybe)))))
1464 ;; Remove everything that is now still marked for deletion
1465 (goto-char (point-min))
1466 (while (setq beg (text-property-any (point-min) (point-max) :org-delete t))
1467 (setq end (or (next-single-property-change beg :org-delete)
1468 (point-max)))
1469 (delete-region beg end))))
1471 (defun org-export-remove-archived-trees (export-archived-trees)
1472 "Remove archived trees.
1473 When EXPORT-ARCHIVED-TREES is `headline;, only the headline will be exported.
1474 When it is t, the entire archived tree will be exported.
1475 When it is nil the entire tree including the headline will be removed
1476 from the buffer."
1477 (let ((re-archive (concat ":" org-archive-tag ":"))
1478 a b)
1479 (when (not (eq export-archived-trees t))
1480 (goto-char (point-min))
1481 (while (re-search-forward re-archive nil t)
1482 (if (not (org-on-heading-p t))
1483 (org-end-of-subtree t)
1484 (beginning-of-line 1)
1485 (setq a (if export-archived-trees
1486 (1+ (point-at-eol)) (point))
1487 b (org-end-of-subtree t))
1488 (if (> b a) (delete-region a b)))))))
1490 (defun org-export-remove-headline-metadata (opts)
1491 "Remove meta data from the headline, according to user options."
1492 (let ((re org-complex-heading-regexp)
1493 (todo (plist-get opts :todo-keywords))
1494 (tags (plist-get opts :tags))
1495 (pri (plist-get opts :priority))
1496 (elts '(1 2 3 4 5))
1497 rpl)
1498 (setq elts (delq nil (list 1 (if todo 2) (if pri 3) 4 (if tags 5))))
1499 (when (or (not todo) (not tags) (not pri))
1500 (goto-char (point-min))
1501 (while (re-search-forward re nil t)
1502 (org-if-unprotected
1503 (setq rpl (mapconcat (lambda (i) (if (match-end i) (match-string i) ""))
1504 elts " "))
1505 (replace-match rpl t t))))))
1507 (defun org-export-remove-timestamps ()
1508 "Remove timestamps and keywords for export."
1509 (goto-char (point-min))
1510 (while (re-search-forward org-maybe-keyword-time-regexp nil t)
1511 (backward-char 1)
1512 (org-if-unprotected
1513 (unless (save-match-data (org-at-table-p))
1514 (replace-match "")
1515 (beginning-of-line 1)
1516 (if (looking-at "[- \t]*\\(=>[- \t0-9:]*\\)?[ \t]*\n")
1517 (replace-match ""))))))
1519 (defun org-export-remove-clock-lines ()
1520 "Remove clock lines for export."
1521 (goto-char (point-min))
1522 (let ((re (concat "^[ \t]*" org-clock-string ".*\n?")))
1523 (while (re-search-forward re nil t)
1524 (org-if-unprotected
1525 (replace-match "")))))
1527 (defun org-export-protect-quoted-subtrees ()
1528 "Mark quoted subtrees with the protection property."
1529 (let ((re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>")))
1530 (goto-char (point-min))
1531 (while (re-search-forward re-quote nil t)
1532 (goto-char (match-beginning 0))
1533 (end-of-line 1)
1534 (add-text-properties (point) (org-end-of-subtree t)
1535 '(org-protected t)))))
1537 (defun org-export-protect-verbatim ()
1538 "Mark verbatim snippets with the protection property."
1539 (goto-char (point-min))
1540 (while (re-search-forward org-verbatim-re nil t)
1541 (org-if-unprotected
1542 (add-text-properties (match-beginning 4) (match-end 4)
1543 '(org-protected t org-verbatim-emph t))
1544 (goto-char (1+ (match-end 4))))))
1546 (defun org-export-protect-colon-examples ()
1547 "Protect lines starting with a colon."
1548 (goto-char (point-min))
1549 (let ((re "^[ \t]*:\\([ \t]\\|$\\)") beg)
1550 (while (re-search-forward re nil t)
1551 (beginning-of-line 1)
1552 (setq beg (point))
1553 (while (looking-at re)
1554 (end-of-line 1)
1555 (or (eobp) (forward-char 1)))
1556 (add-text-properties beg (if (bolp) (1- (point)) (point))
1557 '(org-protected t)))))
1559 (defun org-export-select-backend-specific-text (backend)
1560 (let ((formatters
1561 '((docbook "DOCBOOK" "BEGIN_DOCBOOK" "END_DOCBOOK")
1562 (html "HTML" "BEGIN_HTML" "END_HTML")
1563 (beamer "BEAMER" "BEGIN_BEAMER" "END_BEAMER")
1564 (ascii "ASCII" "BEGIN_ASCII" "END_ASCII")
1565 (latex "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
1566 (case-fold-search t)
1567 fmt beg beg-content end end-content)
1569 (while formatters
1570 (setq fmt (pop formatters))
1571 ;; Handle #+Backend: stuff
1572 (goto-char (point-min))
1573 (while (re-search-forward (concat "^\\([ \t]*\\)#\\+" (cadr fmt)
1574 ":[ \t]*\\(.*\\)") nil t)
1575 (if (not (eq (car fmt) backend))
1576 (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
1577 (replace-match "\\1\\2" t)
1578 (add-text-properties
1579 (point-at-bol) (min (1+ (point-at-eol)) (point-max))
1580 '(org-protected t))))
1581 ;; Delete #+attr_Backend: stuff of another backend. Those
1582 ;; matching the current backend will be taken care of by
1583 ;; `org-export-attach-captions-and-attributes'
1584 (goto-char (point-min))
1585 (while (re-search-forward (concat "^\\([ \t]*\\)#\\+attr_" (cadr fmt)
1586 ":[ \t]*\\(.*\\)") nil t)
1587 (when (not (eq (car fmt) backend))
1588 (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))))
1589 ;; Handle #+begin_Backend and #+end_Backend stuff
1590 (goto-char (point-min))
1591 (while (re-search-forward (concat "^[ \t]*#\\+" (caddr fmt) "\\>.*\n?")
1592 nil t)
1593 (setq beg (match-beginning 0) beg-content (match-end 0))
1594 (when (re-search-forward (concat "^[ \t]*#\\+" (cadddr fmt) "\\>.*\n?")
1595 nil t)
1596 (setq end (match-end 0) end-content (match-beginning 0))
1597 (if (eq (car fmt) backend)
1598 ;; yes, keep this
1599 (progn
1600 (add-text-properties beg-content end-content '(org-protected t))
1601 (delete-region (match-beginning 0) (match-end 0))
1602 (save-excursion
1603 (goto-char beg)
1604 (delete-region (point) (1+ (point-at-eol)))))
1605 ;; No, this is for a different backend, kill it
1606 (delete-region beg end)))))))
1608 (defun org-export-mark-blockquote-verse-center ()
1609 "Mark block quote and verse environments with special cookies.
1610 These special cookies will later be interpreted by the backend."
1611 ;; Blockquotes
1612 (let (type t1 ind beg end beg1 end1 content)
1613 (goto-char (point-min))
1614 (while (re-search-forward
1615 "^\\([ \t]*\\)#\\+\\(begin_\\(\\(block\\)?quote\\|verse\\|center\\)\\>.*\\)"
1616 nil t)
1617 (setq ind (length (match-string 1))
1618 type (downcase (match-string 3))
1619 t1 (if (equal type "quote") "blockquote" type))
1620 (setq beg (match-beginning 0)
1621 beg1 (1+ (match-end 0)))
1622 (when (re-search-forward (concat "^[ \t]*#\\+end_" type "\\>.*") nil t)
1623 (setq end1 (1- (match-beginning 0))
1624 end (+ (point-at-eol) (if (looking-at "\n$") 1 0)))
1625 (setq content (org-remove-indentation (buffer-substring beg1 end1)))
1626 (setq content (concat "ORG-" (upcase t1) "-START\n"
1627 content "\n"
1628 "ORG-" (upcase t1) "-END\n"))
1629 (delete-region beg end)
1630 (insert (org-add-props content nil 'original-indentation ind))))))
1632 (defun org-export-mark-list-ending (backend)
1633 "Mark list endings with special cookies.
1634 These special cookies will later be interpreted by the backend.
1635 `org-list-end-re' is replaced by a blank line in the process."
1636 (let ((process-buffer
1637 (lambda (end-list-marker)
1638 (goto-char (point-min))
1639 (while (org-search-forward-unenclosed org-item-beginning-re nil t)
1640 (goto-char (org-list-bottom-point))
1641 (when (and (not (eq org-list-ending-method 'indent))
1642 (looking-at (org-list-end-re)))
1643 (replace-match "\n"))
1644 (insert end-list-marker)))))
1645 ;; We need to divide backends into 3 categories.
1646 (cond
1647 ;; 1. Backends using `org-list-parse-list' do not need markers.
1648 ((memq backend '(latex))
1649 nil)
1650 ;; 2. Line-processing backends need to be told where lists end.
1651 ((memq backend '(html docbook))
1652 (funcall process-buffer "ORG-LIST-END\n"))
1653 ;; 3. Others backends do not need to know this: clean list enders.
1655 (funcall process-buffer "")))))
1657 (defun org-export-attach-captions-and-attributes (backend target-alist)
1658 "Move #+CAPTION, #+ATTR_BACKEND, and #+LABEL text into text properties.
1659 If the next thing following is a table, add the text properties to the first
1660 table line. If it is a link, add it to the line containing the link."
1661 (goto-char (point-min))
1662 (remove-text-properties (point-min) (point-max)
1663 '(org-caption nil org-attributes nil))
1664 (let ((case-fold-search t)
1665 (re (concat "^[ \t]*#\\+caption:[ \t]+\\(.*\\)"
1666 "\\|"
1667 "^[ \t]*#\\+attr_" (symbol-name backend) ":[ \t]+\\(.*\\)"
1668 "\\|"
1669 "^[ \t]*#\\+label:[ \t]+\\(.*\\)"
1670 "\\|"
1671 "^[ \t]*\\(|[^-]\\)"
1672 "\\|"
1673 "^[ \t]*\\[\\[.*\\]\\][ \t]*$"))
1674 cap shortn attr label end)
1675 (while (re-search-forward re nil t)
1676 (cond
1677 ((match-end 1)
1678 (progn
1679 (setq cap (concat cap (if cap " " "") (org-trim (match-string 1))))
1680 (when (string-match "\\[\\(.*\\)\\]{\\(.*\\)}" cap)
1681 (setq shortn (match-string 1 cap)
1682 cap (match-string 2 cap)))
1683 (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))))
1684 ((match-end 2)
1685 (progn
1686 (setq attr (concat attr (if attr " " "") (org-trim (match-string 2))))
1687 (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))))
1688 ((match-end 3)
1689 (progn
1690 (setq label (org-trim (match-string 3)))
1691 (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))))
1693 (setq end (if (match-end 4)
1694 (let ((ee (org-table-end)))
1695 (prog1 (1- (marker-position ee)) (move-marker ee nil)))
1696 (point-at-eol)))
1697 (add-text-properties (point-at-bol) end
1698 (list 'org-caption cap
1699 'org-caption-shortn shortn
1700 'org-attributes attr
1701 'org-label label))
1702 (if label (push (cons label label) target-alist))
1703 (goto-char end)
1704 (setq cap nil attr nil label nil)))))
1705 target-alist)
1707 (defun org-export-remove-comment-blocks-and-subtrees ()
1708 "Remove the comment environment, and also commented subtrees."
1709 (let ((re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
1710 case-fold-search)
1711 ;; Remove comment environment
1712 (goto-char (point-min))
1713 (setq case-fold-search t)
1714 (while (re-search-forward
1715 "^#\\+begin_comment[ \t]*\n[^\000]*?^#\\+end_comment\\>.*" nil t)
1716 (replace-match "" t t))
1717 ;; Remove subtrees that are commented
1718 (goto-char (point-min))
1719 (setq case-fold-search nil)
1720 (while (re-search-forward re-commented nil t)
1721 (goto-char (match-beginning 0))
1722 (delete-region (point) (org-end-of-subtree t)))))
1724 (defun org-export-handle-comments (commentsp)
1725 "Remove comments, or convert to backend-specific format.
1726 COMMENTSP can be a format string for publishing comments.
1727 When it is nil, all comments will be removed."
1728 (let ((re "^\\(#\\|[ \t]*#\\+ \\)\\(.*\n?\\)")
1729 pos)
1730 (goto-char (point-min))
1731 (while (or (looking-at re)
1732 (re-search-forward re nil t))
1733 (setq pos (match-beginning 0))
1734 (if (get-text-property pos 'org-protected)
1735 (goto-char (1+ pos))
1736 (if (and commentsp
1737 (not (equal (char-before (match-end 1)) ?+)))
1738 (progn (add-text-properties
1739 (match-beginning 0) (match-end 0) '(org-protected t))
1740 (replace-match (format commentsp (match-string 2)) t t))
1741 (goto-char (1+ pos))
1742 (replace-match "")
1743 (goto-char (max (point-min) (1- pos))))))))
1745 (defun org-export-handle-table-metalines ()
1746 "Remove table specific metalines #+TBLNAME: and #+TBLFM:."
1747 (let ((re "^[ \t]*#\\+TBL\\(NAME\\|FM\\):\\(.*\n?\\)")
1748 pos)
1749 (goto-char (point-min))
1750 (while (or (looking-at re)
1751 (re-search-forward re nil t))
1752 (setq pos (match-beginning 0))
1753 (if (get-text-property (match-beginning 1) 'org-protected)
1754 (goto-char (1+ pos))
1755 (goto-char (1+ pos))
1756 (replace-match "")
1757 (goto-char (max (point-min) (1- pos)))))))
1759 (defun org-export-mark-radio-links ()
1760 "Find all matches for radio targets and turn them into internal links."
1761 (let ((re-radio (and org-target-link-regexp
1762 (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)"))))
1763 (goto-char (point-min))
1764 (when re-radio
1765 (while (re-search-forward re-radio nil t)
1766 (unless
1767 (save-match-data
1768 (or (org-in-regexp org-bracket-link-regexp)
1769 (org-in-regexp org-plain-link-re)
1770 (org-in-regexp "<<[^<>]+>>")))
1771 (org-if-unprotected
1772 (replace-match "\\1[[\\2]]")))))))
1774 (defun org-export-remove-special-table-lines ()
1775 "Remove tables lines that are used for internal purposes."
1776 (goto-char (point-min))
1777 (while (re-search-forward "^[ \t]*|" nil t)
1778 (org-if-unprotected-at (1- (point))
1779 (beginning-of-line 1)
1780 (if (or (looking-at "[ \t]*| *[!_^] *|")
1781 (not
1782 (memq
1784 (mapcar
1785 (lambda (f)
1786 (or (= (length f) 0)
1787 (string-match
1788 "\\`<\\([0-9]\\|[rl]\\|[rl][0-9]+\\)>\\'" f)))
1789 (org-split-string ;; FIXME, can't we do without splitting???
1790 (buffer-substring (point-at-bol) (point-at-eol))
1791 "[ \t]*|[ \t]*")))))
1792 (delete-region (max (point-min) (1- (point-at-bol)))
1793 (point-at-eol))
1794 (end-of-line 1)))))
1796 (defun org-export-protect-sub-super (s)
1797 (save-match-data
1798 (while (string-match "\\([^\\\\]\\)\\([_^]\\)" s)
1799 (setq s (replace-match "\\1\\\\\\2" nil nil s)))
1802 (defun org-export-normalize-links ()
1803 "Convert all links to bracket links, and expand link abbreviations."
1804 (let ((re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
1805 (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
1806 nodesc)
1807 (goto-char (point-min))
1808 (while (re-search-forward re-plain-link nil t)
1809 (goto-char (1- (match-end 0)))
1810 (org-if-unprotected-at (1+ (match-beginning 0))
1811 (let* ((s (concat (match-string 1)
1812 "[[" (match-string 2) ":" (match-string 3)
1813 "][" (match-string 2) ":" (org-export-protect-sub-super
1814 (match-string 3))
1815 "]]")))
1816 ;; added 'org-link face to links
1817 (put-text-property 0 (length s) 'face 'org-link s)
1818 (replace-match s t t))))
1819 (goto-char (point-min))
1820 (while (re-search-forward re-angle-link nil t)
1821 (goto-char (1- (match-end 0)))
1822 (org-if-unprotected
1823 (let* ((s (concat (match-string 1)
1824 "[[" (match-string 2) ":" (match-string 3)
1825 "][" (match-string 2) ":" (org-export-protect-sub-super
1826 (match-string 3))
1827 "]]")))
1828 (put-text-property 0 (length s) 'face 'org-link s)
1829 (replace-match s t t))))
1830 (goto-char (point-min))
1831 (while (re-search-forward org-bracket-link-regexp nil t)
1832 (goto-char (1- (match-end 0)))
1833 (setq nodesc (not (match-end 3)))
1834 (org-if-unprotected
1835 (let* ((xx (save-match-data
1836 (org-translate-link
1837 (org-link-expand-abbrev (match-string 1)))))
1838 (s (concat
1839 "[[" (org-add-props (copy-sequence xx)
1840 nil 'org-protected t 'org-no-description nodesc)
1842 (if (match-end 3)
1843 (match-string 2)
1844 (concat "[" (copy-sequence xx)
1845 "]"))
1846 "]")))
1847 (put-text-property 0 (length s) 'face 'org-link s)
1848 (replace-match s t t))))))
1850 (defun org-export-concatenate-multiline-links ()
1851 "Find multi-line links and put it all into a single line.
1852 This is to make sure that the line-processing export backends
1853 can work correctly."
1854 (goto-char (point-min))
1855 (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
1856 (org-if-unprotected-at (match-beginning 1)
1857 (replace-match "\\1 \\3")
1858 (goto-char (match-beginning 0)))))
1860 (defun org-export-concatenate-multiline-emphasis ()
1861 "Find multi-line emphasis and put it all into a single line.
1862 This is to make sure that the line-processing export backends
1863 can work correctly."
1864 (goto-char (point-min))
1865 (while (re-search-forward org-emph-re nil t)
1866 (if (and (not (= (char-after (match-beginning 3))
1867 (char-after (match-beginning 4))))
1868 (save-excursion (goto-char (match-beginning 0))
1869 (save-match-data
1870 (and (not (org-at-table-p))
1871 (not (org-at-heading-p))))))
1872 (org-if-unprotected
1873 (subst-char-in-region (match-beginning 0) (match-end 0)
1874 ?\n ?\ t)
1875 (goto-char (1- (match-end 0))))
1876 (goto-char (1+ (match-beginning 0))))))
1878 (defun org-export-grab-title-from-buffer ()
1879 "Get a title for the current document, from looking at the buffer."
1880 (let ((inhibit-read-only t))
1881 (save-excursion
1882 (goto-char (point-min))
1883 (let ((end (if (looking-at org-outline-regexp)
1884 (point)
1885 (save-excursion (outline-next-heading) (point)))))
1886 (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
1887 ;; Mark the line so that it will not be exported as normal text.
1888 (org-unmodified
1889 (add-text-properties (match-beginning 0) (match-end 0)
1890 (list :org-license-to-kill t)))
1891 ;; Return the title string
1892 (org-trim (match-string 0)))))))
1894 (defun org-export-get-title-from-subtree ()
1895 "Return subtree title and exclude it from export."
1896 (let (title (rbeg (region-beginning)) (rend (region-end)))
1897 (save-excursion
1898 (goto-char rbeg)
1899 (when (and (org-at-heading-p)
1900 (>= (org-end-of-subtree t t) rend))
1901 ;; This is a subtree, we take the title from the first heading
1902 (goto-char rbeg)
1903 (looking-at org-todo-line-regexp)
1904 (setq title (match-string 3))
1905 (org-unmodified
1906 (add-text-properties (point) (1+ (point-at-eol))
1907 (list :org-license-to-kill t)))
1908 (setq title (or (org-entry-get nil "EXPORT_TITLE") title))))
1909 title))
1911 (defun org-solidify-link-text (s &optional alist)
1912 "Take link text and make a safe target out of it."
1913 (save-match-data
1914 (let* ((rtn
1915 (mapconcat
1916 'identity
1917 (org-split-string s "[ \t\r\n]+") "=="))
1918 (a (assoc rtn alist)))
1919 (or (cdr a) rtn))))
1921 (defun org-get-min-level (lines &optional offset)
1922 "Get the minimum level in LINES."
1923 (let ((re "^\\(\\*+\\) ") l)
1924 (catch 'exit
1925 (while (setq l (pop lines))
1926 (if (string-match re l)
1927 (throw 'exit (org-tr-level (- (length (match-string 1 l))
1928 (or offset 0))))))
1929 1)))
1931 ;; Variable holding the vector with section numbers
1932 (defvar org-section-numbers (make-vector org-level-max 0))
1934 (defun org-init-section-numbers ()
1935 "Initialize the vector for the section numbers."
1936 (let* ((level -1)
1937 (numbers (nreverse (org-split-string "" "\\.")))
1938 (depth (1- (length org-section-numbers)))
1939 (i depth) number-string)
1940 (while (>= i 0)
1941 (if (> i level)
1942 (aset org-section-numbers i 0)
1943 (setq number-string (or (car numbers) "0"))
1944 (if (string-match "\\`[A-Z]\\'" number-string)
1945 (aset org-section-numbers i
1946 (- (string-to-char number-string) ?A -1))
1947 (aset org-section-numbers i (string-to-number number-string)))
1948 (pop numbers))
1949 (setq i (1- i)))))
1951 (defun org-section-number (&optional level)
1952 "Return a string with the current section number.
1953 When LEVEL is non-nil, increase section numbers on that level."
1954 (let* ((depth (1- (length org-section-numbers)))
1955 (string "")
1956 (fmts (car org-export-section-number-format))
1957 (term (cdr org-export-section-number-format))
1958 (sep "")
1959 ctype fmt idx n)
1960 (when level
1961 (when (> level -1)
1962 (aset org-section-numbers
1963 level (1+ (aref org-section-numbers level))))
1964 (setq idx (1+ level))
1965 (while (<= idx depth)
1966 (if (not (= idx 1))
1967 (aset org-section-numbers idx 0))
1968 (setq idx (1+ idx))))
1969 (setq idx 0)
1970 (while (<= idx depth)
1971 (when (> (aref org-section-numbers idx) 0)
1972 (setq fmt (or (pop fmts) fmt)
1973 ctype (car fmt)
1974 n (aref org-section-numbers idx)
1975 string (if (> n 0)
1976 (concat string sep (org-number-to-counter n ctype))
1977 (concat string ".0"))
1978 sep (nth 1 fmt)))
1979 (setq idx (1+ idx)))
1980 (save-match-data
1981 (if (string-match "\\`\\([@0]\\.\\)+" string)
1982 (setq string (replace-match "" t nil string)))
1983 (if (string-match "\\(\\.0\\)+\\'" string)
1984 (setq string (replace-match "" t nil string))))
1985 (concat string term)))
1987 (defun org-number-to-counter (n type)
1988 "Concert number N to a string counter, according to TYPE.
1989 TYPE must be a string, any of:
1990 1 number
1991 A A,B,....
1992 a a,b,....
1993 I upper case roman numeral
1994 i lower case roman numeral"
1995 (cond
1996 ((equal type "1") (number-to-string n))
1997 ((equal type "A") (char-to-string (+ ?A n -1)))
1998 ((equal type "a") (char-to-string (+ ?a n -1)))
1999 ((equal type "I") (org-number-to-roman n))
2000 ((equal type "i") (downcase (org-number-to-roman n)))
2001 (t (error "Invalid counter type `%s'" type))))
2003 (defun org-number-to-roman (n)
2004 "Convert integer N into a roman numeral."
2005 (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
2006 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
2007 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
2008 ( 1 . "I")))
2009 (res ""))
2010 (if (<= n 0)
2011 (number-to-string n)
2012 (while roman
2013 (if (>= n (caar roman))
2014 (setq n (- n (caar roman))
2015 res (concat res (cdar roman)))
2016 (pop roman)))
2017 res)))
2019 ;;; Macros
2021 (defun org-export-preprocess-apply-macros ()
2022 "Replace macro references."
2023 (goto-char (point-min))
2024 (let (sy val key args args2 s n)
2025 (while (re-search-forward
2026 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
2027 nil t)
2028 (unless (save-match-data
2029 (save-excursion
2030 (goto-char (point-at-bol))
2031 (looking-at "[ \t]*#\\+macro")))
2032 (setq key (downcase (match-string 1))
2033 args (match-string 3))
2034 (when (setq val (or (plist-get org-export-opt-plist
2035 (intern (concat ":macro-" key)))
2036 (plist-get org-export-opt-plist
2037 (intern (concat ":" key)))))
2038 (save-match-data
2039 (when args
2040 (setq args (org-split-string args ",") args2 nil)
2041 (while args
2042 (while (string-match "\\\\\\'" (car args))
2043 ;; repair bad splits
2044 (setcar (cdr args) (concat (substring (car args) 0 -1)
2045 "," (nth 1 args)))
2046 (pop args))
2047 (push (pop args) args2))
2048 (setq args (mapcar 'org-trim (nreverse args2)))
2049 (setq s 0)
2050 (while (string-match "\\$\\([0-9]+\\)" val s)
2051 (setq s (1+ (match-beginning 0))
2052 n (string-to-number (match-string 1 val)))
2053 (and (>= (length args) n)
2054 (setq val (replace-match (nth (1- n) args) t t val)))))
2055 (when (string-match "\\`(eval\\>" val)
2056 (setq val (eval (read val))))
2057 (if (and val (not (stringp val)))
2058 (setq val (format "%s" val))))
2059 (and (stringp val)
2060 (prog1 (replace-match val t t)
2061 (goto-char (match-beginning 0)))))))))
2063 (defun org-export-apply-macros-in-string (s)
2064 "Apply the macros in string S."
2065 (when s
2066 (with-temp-buffer
2067 (insert s)
2068 (org-export-preprocess-apply-macros)
2069 (buffer-string))))
2071 ;;; Include files
2073 (defun org-export-handle-include-files ()
2074 "Include the contents of include files, with proper formatting."
2075 (let ((case-fold-search t)
2076 params file markup lang start end prefix prefix1 switches all)
2077 (goto-char (point-min))
2078 (while (re-search-forward "^#\\+INCLUDE:?[ \t]+\\(.*\\)" nil t)
2079 (setq params (read (concat "(" (match-string 1) ")"))
2080 prefix (org-get-and-remove-property 'params :prefix)
2081 prefix1 (org-get-and-remove-property 'params :prefix1)
2082 file (org-symname-or-string (pop params))
2083 markup (org-symname-or-string (pop params))
2084 lang (and (member markup '("src" "SRC"))
2085 (org-symname-or-string (pop params)))
2086 switches (mapconcat '(lambda (x) (format "%s" x)) params " ")
2087 start nil end nil)
2088 (delete-region (match-beginning 0) (match-end 0))
2089 (if (or (not file)
2090 (not (file-exists-p file))
2091 (not (file-readable-p file)))
2092 (insert (format "CANNOT INCLUDE FILE %s" file))
2093 (setq all (cons file all))
2094 (when markup
2095 (if (equal (downcase markup) "src")
2096 (setq start (format "#+begin_src %s %s\n"
2097 (or lang "fundamental")
2098 (or switches ""))
2099 end "#+end_src")
2100 (setq start (format "#+begin_%s %s\n" markup switches)
2101 end (format "#+end_%s" markup))))
2102 (insert (or start ""))
2103 (insert (org-get-file-contents (expand-file-name file)
2104 prefix prefix1 markup))
2105 (or (bolp) (newline))
2106 (insert (or end ""))))
2107 all))
2109 (defun org-export-handle-include-files-recurse ()
2110 "Recursively include files aborting on circular inclusion."
2111 (let ((now (list org-current-export-file)) all)
2112 (while now
2113 (setq all (append now all))
2114 (setq now (org-export-handle-include-files))
2115 (let ((intersection
2116 (delq nil
2117 (mapcar (lambda (el) (when (member el all) el)) now))))
2118 (when intersection
2119 (error "Recursive #+INCLUDE: %S" intersection))))))
2121 (defun org-get-file-contents (file &optional prefix prefix1 markup)
2122 "Get the contents of FILE and return them as a string.
2123 If PREFIX is a string, prepend it to each line. If PREFIX1
2124 is a string, prepend it to the first line instead of PREFIX.
2125 If MARKUP, don't protect org-like lines, the exporter will
2126 take care of the block they are in."
2127 (if (stringp markup) (setq markup (downcase markup)))
2128 (with-temp-buffer
2129 (insert-file-contents file)
2130 (when (or prefix prefix1)
2131 (goto-char (point-min))
2132 (while (not (eobp))
2133 (insert (or prefix1 prefix))
2134 (setq prefix1 "")
2135 (beginning-of-line 2)))
2136 (buffer-string)
2137 (when (member markup '("src" "example"))
2138 (goto-char (point-min))
2139 (while (re-search-forward "^\\([*#]\\|[ \t]*#\\+\\)" nil t)
2140 (goto-char (match-beginning 0))
2141 (insert ",")
2142 (end-of-line 1)))
2143 (buffer-string)))
2145 (defun org-get-and-remove-property (listvar prop)
2146 "Check if the value of LISTVAR contains PROP as a property.
2147 If yes, return the value of that property (i.e. the element following
2148 in the list) and remove property and value from the list in LISTVAR."
2149 (let ((list (symbol-value listvar)) m v)
2150 (when (setq m (member prop list))
2151 (setq v (nth 1 m))
2152 (if (equal (car list) prop)
2153 (set listvar (cddr list))
2154 (setcdr (nthcdr (- (length list) (length m) 1) list)
2155 (cddr m))
2156 (set listvar list)))
2159 (defun org-symname-or-string (s)
2160 (if (symbolp s)
2161 (if s (symbol-name s) s)
2164 ;;; Fontification and line numbers for code examples
2166 (defvar org-export-last-code-line-counter-value 0)
2168 (defun org-export-replace-src-segments-and-examples (backend)
2169 "Replace source code segments with special code for export."
2170 (setq org-export-last-code-line-counter-value 0)
2171 (let ((case-fold-search t)
2172 lang code trans opts indent caption)
2173 (goto-char (point-min))
2174 (while (re-search-forward
2175 "\\(^\\([ \t]*\\)#\\+BEGIN_SRC:?\\([ \t]+\\([^ \t\n]+\\)\\)?\\(.*\\)\n\\([^\000]+?\n\\)[ \t]*#\\+END_SRC.*\n?\\)\\|\\(^\\([ \t]*\\)#\\+BEGIN_EXAMPLE:?\\(?:[ \t]+\\(.*\\)\\)?\n\\([^\000]+?\n\\)[ \t]*#\\+END_EXAMPLE.*\n?\\)"
2176 nil t)
2177 (if (match-end 1)
2178 (if (not (match-string 4))
2179 (error "Source block missing language specification: %s"
2180 (let* ((body (match-string 6))
2181 (nothing (message "body:%s" body))
2182 (preview (or (and (string-match
2183 "^[ \t]*\\([^\n\r]*\\)" body)
2184 (match-string 1 body)) body)))
2185 (if (> (length preview) 35)
2186 (concat (substring preview 0 32) "...")
2187 preview)))
2188 ;; src segments
2189 (setq lang (match-string 4)
2190 opts (match-string 5)
2191 code (match-string 6)
2192 indent (length (match-string 2))
2193 caption (get-text-property 0 'org-caption (match-string 0))))
2194 (setq lang nil
2195 opts (match-string 9)
2196 code (match-string 10)
2197 indent (length (match-string 8))
2198 caption (get-text-property 0 'org-caption (match-string 0))))
2200 (setq trans (org-export-format-source-code-or-example
2201 backend lang code opts indent caption))
2202 (replace-match trans t t))))
2204 (defvar htmlp) ;; dynamically scoped
2205 (defvar latexp) ;; dynamically scoped
2206 (defvar org-export-latex-verbatim-wrap) ;; defined in org-latex.el
2207 (defvar org-export-latex-listings) ;; defined in org-latex.el
2208 (defvar org-export-latex-listings-langs) ;; defined in org-latex.el
2209 (defvar org-export-latex-listings-w-names) ;; defined in org-latex.el
2211 (defun org-export-format-source-code-or-example
2212 (backend lang code &optional opts indent caption)
2213 "Format CODE from language LANG and return it formatted for export.
2214 If LANG is nil, do not add any fontification.
2215 OPTS contains formatting options, like `-n' for triggering numbering lines,
2216 and `+n' for continuing previous numbering.
2217 Code formatting according to language currently only works for HTML.
2218 Numbering lines works for all three major backends (html, latex, and ascii).
2219 INDENT was the original indentation of the block."
2220 (save-match-data
2221 (let (num cont rtn rpllbl keepp textareap preserve-indentp cols rows fmt)
2222 (setq opts (or opts "")
2223 num (string-match "[-+]n\\>" opts)
2224 cont (string-match "\\+n\\>" opts)
2225 rpllbl (string-match "-r\\>" opts)
2226 keepp (string-match "-k\\>" opts)
2227 textareap (string-match "-t\\>" opts)
2228 preserve-indentp (or org-src-preserve-indentation
2229 (string-match "-i\\>" opts))
2230 cols (if (string-match "-w[ \t]+\\([0-9]+\\)" opts)
2231 (string-to-number (match-string 1 opts))
2233 rows (if (string-match "-h[ \t]+\\([0-9]+\\)" opts)
2234 (string-to-number (match-string 1 opts))
2235 (org-count-lines code))
2236 fmt (if (string-match "-l[ \t]+\"\\([^\"\n]+\\)\"" opts)
2237 (match-string 1 opts)))
2238 (when (and textareap (eq backend 'html))
2239 ;; we cannot use numbering or highlighting.
2240 (setq num nil cont nil lang nil))
2241 (if keepp (setq rpllbl 'keep))
2242 (setq rtn (if preserve-indentp code (org-remove-indentation code)))
2243 (when (string-match "^," rtn)
2244 (setq rtn (with-temp-buffer
2245 (insert rtn)
2246 ;; Free up the protected lines
2247 (goto-char (point-min))
2248 (while (re-search-forward "^," nil t)
2249 (if (or (equal lang "org")
2250 (save-match-data
2251 (looking-at "\\([*#]\\|[ \t]*#\\+\\)")))
2252 (replace-match ""))
2253 (end-of-line 1))
2254 (buffer-string))))
2255 ;; Now backend-specific coding
2256 (setq rtn
2257 (cond
2258 ((eq backend 'docbook)
2259 (setq rtn (org-export-number-lines rtn 'docbook 0 0 num cont rpllbl fmt))
2260 (concat "\n#+BEGIN_DOCBOOK\n"
2261 (org-add-props (concat "<programlisting><![CDATA["
2263 "]]></programlisting>\n")
2264 '(org-protected t org-example t))
2265 "#+END_DOCBOOK\n"))
2266 ((eq backend 'html)
2267 ;; We are exporting to HTML
2268 (when lang
2269 (if (featurep 'xemacs)
2270 (require 'htmlize)
2271 (require 'htmlize nil t))
2272 (when (not (fboundp 'htmlize-region-for-paste))
2273 ;; we do not have htmlize.el, or an old version of it
2274 (setq lang nil)
2275 (message
2276 "htmlize.el 1.34 or later is needed for source code formatting")))
2278 (if lang
2279 (let* ((lang-m (when lang
2280 (or (cdr (assoc lang org-src-lang-modes))
2281 lang)))
2282 (mode (and lang-m (intern
2283 (concat
2284 (if (symbolp lang-m)
2285 (symbol-name lang-m)
2286 lang-m)
2287 "-mode"))))
2288 (org-inhibit-startup t)
2289 (org-startup-folded nil))
2290 (setq rtn
2291 (with-temp-buffer
2292 (insert rtn)
2293 (if (functionp mode)
2294 (funcall mode)
2295 (fundamental-mode))
2296 (font-lock-fontify-buffer)
2297 (org-src-mode)
2298 (set-buffer-modified-p nil)
2299 (org-export-htmlize-region-for-paste
2300 (point-min) (point-max))))
2301 (if (string-match "<pre\\([^>]*\\)>\n*" rtn)
2302 (setq rtn
2303 (concat
2304 (if caption
2305 (concat
2306 "<div class=\"org-src-container\">"
2307 (format
2308 "<label class=\"org-src-name\">%s</label>"
2309 caption))
2311 (replace-match
2312 (format "<pre class=\"src src-%s\">\n" lang)
2313 t t rtn)
2314 (if caption "</div>" "")))))
2315 (if textareap
2316 (setq rtn (concat
2317 (format "<p>\n<textarea cols=\"%d\" rows=\"%d\">"
2318 cols rows)
2319 rtn "</textarea>\n</p>\n"))
2320 (with-temp-buffer
2321 (insert rtn)
2322 (goto-char (point-min))
2323 (while (re-search-forward "[<>&]" nil t)
2324 (replace-match (cdr (assq (char-before)
2325 '((?&."&amp;")(?<."&lt;")(?>."&gt;"))))
2326 t t))
2327 (setq rtn (buffer-string)))
2328 (setq rtn (concat "<pre class=\"example\">\n" rtn "</pre>\n"))))
2329 (unless textareap
2330 (setq rtn (org-export-number-lines rtn 'html 1 1 num
2331 cont rpllbl fmt)))
2332 (if (string-match "\\(\\`<[^>]*>\\)\n" rtn)
2333 (setq rtn (replace-match "\\1" t nil rtn)))
2334 (concat "\n#+BEGIN_HTML\n" (org-add-props rtn '(org-protected t org-example t)) "\n#+END_HTML\n\n"))
2335 ((eq backend 'latex)
2336 (setq rtn (org-export-number-lines rtn 'latex 0 0 num cont rpllbl fmt))
2337 (concat "#+BEGIN_LaTeX\n"
2338 (org-add-props
2339 (if org-export-latex-listings
2340 (concat
2341 (if lang
2342 (let*
2343 ((lang-sym (intern lang))
2344 (lstlang
2345 (or (cadr
2346 (assq
2347 lang-sym
2348 org-export-latex-listings-langs))
2349 lang)))
2350 (format "\\lstset{language=%s}\n" lstlang))
2351 "\n")
2352 (when (and caption
2353 org-export-latex-listings-w-names)
2354 (format "\n%s $\\equiv$ \n"
2355 (replace-regexp-in-string
2356 "_" "\\\\_" caption)))
2357 "\\begin{lstlisting}\n"
2358 rtn "\\end{lstlisting}\n")
2359 (concat (car org-export-latex-verbatim-wrap)
2360 rtn (cdr org-export-latex-verbatim-wrap)))
2361 '(org-protected t org-example t))
2362 "#+END_LaTeX\n"))
2363 ((eq backend 'ascii)
2364 ;; This is not HTML or LaTeX, so just make it an example.
2365 (setq rtn (org-export-number-lines rtn 'ascii 0 0 num cont rpllbl fmt))
2366 (concat caption "\n"
2367 "#+BEGIN_ASCII\n"
2368 (org-add-props
2369 (concat
2370 (mapconcat
2371 (lambda (l) (concat " " l))
2372 (org-split-string rtn "\n")
2373 "\n")
2374 "\n")
2375 '(org-protected t org-example t))
2376 "#+END_ASCII\n"))))
2377 (org-add-props rtn nil 'original-indentation indent))))
2379 (defun org-export-number-lines (text backend
2380 &optional skip1 skip2 number cont
2381 replace-labels label-format)
2382 (setq skip1 (or skip1 0) skip2 (or skip2 0))
2383 (if (not cont) (setq org-export-last-code-line-counter-value 0))
2384 (with-temp-buffer
2385 (insert text)
2386 (goto-char (point-max))
2387 (skip-chars-backward " \t\n\r")
2388 (delete-region (point) (point-max))
2389 (beginning-of-line (- 1 skip2))
2390 (let* ((last (org-current-line))
2391 (n org-export-last-code-line-counter-value)
2392 (nmax (+ n (- last skip1)))
2393 (fmt (format "%%%dd: " (length (number-to-string nmax))))
2395 (cond
2396 ((eq backend 'html) (format "<span class=\"linenr\">%s</span>"
2397 fmt))
2398 ((eq backend 'ascii) fmt)
2399 ((eq backend 'latex) fmt)
2400 ((eq backend 'docbook) fmt)
2401 (t "")))
2402 (label-format (or label-format org-coderef-label-format))
2403 (label-pre (if (string-match "%s" label-format)
2404 (substring label-format 0 (match-beginning 0))
2405 label-format))
2406 (label-post (if (string-match "%s" label-format)
2407 (substring label-format (match-end 0))
2408 ""))
2409 (lbl-re
2410 (concat
2411 ".*?\\S-.*?\\([ \t]*\\("
2412 (regexp-quote label-pre)
2413 "\\([-a-zA-Z0-9_ ]+\\)"
2414 (regexp-quote label-post)
2415 "\\)\\)"))
2416 ref)
2418 (org-goto-line (1+ skip1))
2419 (while (and (re-search-forward "^" nil t) (not (eobp)) (< n nmax))
2420 (if number
2421 (insert (format fm (incf n)))
2422 (forward-char 1))
2423 (when (looking-at lbl-re)
2424 (setq ref (match-string 3))
2425 (cond ((numberp replace-labels)
2426 ;; remove labels; use numbers for references when lines
2427 ;; are numbered, use labels otherwise
2428 (delete-region (match-beginning 1) (match-end 1))
2429 (push (cons ref (if (> n 0) n ref)) org-export-code-refs))
2430 ((eq replace-labels 'keep)
2431 ;; don't remove labels; use numbers for references when
2432 ;; lines are numbered, use labels otherwise
2433 (goto-char (match-beginning 2))
2434 (delete-region (match-beginning 2) (match-end 2))
2435 (insert "(" ref ")")
2436 (push (cons ref (if (> n 0) n (concat "(" ref ")")))
2437 org-export-code-refs))
2439 ;; don't remove labels and don't use numbers for
2440 ;; references
2441 (goto-char (match-beginning 2))
2442 (delete-region (match-beginning 2) (match-end 2))
2443 (insert "(" ref ")")
2444 (push (cons ref (concat "(" ref ")")) org-export-code-refs)))
2445 (when (eq backend 'html)
2446 (save-excursion
2447 (beginning-of-line 1)
2448 (insert (format "<span id=\"coderef-%s\" class=\"coderef-off\">"
2449 ref))
2450 (end-of-line 1)
2451 (insert "</span>")))))
2452 (setq org-export-last-code-line-counter-value n)
2453 (goto-char (point-max))
2454 (newline)
2455 (buffer-string))))
2457 (defun org-search-todo-below (line lines level)
2458 "Search the subtree below LINE for any TODO entries."
2459 (let ((rest (cdr (memq line lines)))
2460 (re org-todo-line-regexp)
2461 line lv todo)
2462 (catch 'exit
2463 (while (setq line (pop rest))
2464 (if (string-match re line)
2465 (progn
2466 (setq lv (- (match-end 1) (match-beginning 1))
2467 todo (and (match-beginning 2)
2468 (not (member (match-string 2 line)
2469 org-done-keywords))))
2470 ; TODO, not DONE
2471 (if (<= lv level) (throw 'exit nil))
2472 (if todo (throw 'exit t))))))))
2474 ;;;###autoload
2475 (defun org-export-visible (type arg)
2476 "Create a copy of the visible part of the current buffer, and export it.
2477 The copy is created in a temporary buffer and removed after use.
2478 TYPE is the final key (as a string) that also selects the export command in
2479 the \\<org-mode-map>\\[org-export] export dispatcher.
2480 As a special case, if the you type SPC at the prompt, the temporary
2481 org-mode file will not be removed but presented to you so that you can
2482 continue to use it. The prefix arg ARG is passed through to the exporting
2483 command."
2484 (interactive
2485 (list (progn
2486 (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")
2487 (read-char-exclusive))
2488 current-prefix-arg))
2489 (if (not (member type '(?a ?n ?u ?\C-a ?b ?\C-b ?h ?D ?x ?\ ?l ?p ?d ?L)))
2490 (error "Invalid export key"))
2491 (let* ((binding (cdr (assoc type
2493 (?a . org-export-as-ascii)
2494 (?A . org-export-as-ascii-to-buffer)
2495 (?n . org-export-as-latin1)
2496 (?N . org-export-as-latin1-to-buffer)
2497 (?u . org-export-as-utf8)
2498 (?U . org-export-as-utf8-to-buffer)
2499 (?\C-a . org-export-as-ascii)
2500 (?b . org-export-as-html-and-open)
2501 (?\C-b . org-export-as-html-and-open)
2502 (?h . org-export-as-html)
2503 (?H . org-export-as-html-to-buffer)
2504 (?R . org-export-region-as-html)
2505 (?D . org-export-as-docbook)
2507 (?l . org-export-as-latex)
2508 (?p . org-export-as-pdf)
2509 (?d . org-export-as-pdf-and-open)
2510 (?L . org-export-as-latex-to-buffer)
2512 (?x . org-export-as-xoxo)))))
2513 (keepp (equal type ?\ ))
2514 (file buffer-file-name)
2515 (buffer (get-buffer-create "*Org Export Visible*"))
2516 s e)
2517 ;; Need to hack the drawers here.
2518 (save-excursion
2519 (goto-char (point-min))
2520 (while (re-search-forward org-drawer-regexp nil t)
2521 (goto-char (match-beginning 1))
2522 (or (org-invisible-p) (org-flag-drawer nil))))
2523 (with-current-buffer buffer (erase-buffer))
2524 (save-excursion
2525 (setq s (goto-char (point-min)))
2526 (while (not (= (point) (point-max)))
2527 (goto-char (org-find-invisible))
2528 (append-to-buffer buffer s (point))
2529 (setq s (goto-char (org-find-visible))))
2530 (org-cycle-hide-drawers 'all)
2531 (goto-char (point-min))
2532 (unless keepp
2533 ;; Copy all comment lines to the end, to make sure #+ settings are
2534 ;; still available for the second export step. Kind of a hack, but
2535 ;; does do the trick.
2536 (if (looking-at "#[^\r\n]*")
2537 (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
2538 (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
2539 (append-to-buffer buffer (1+ (match-beginning 0))
2540 (min (point-max) (1+ (match-end 0))))))
2541 (set-buffer buffer)
2542 (let ((buffer-file-name file)
2543 (org-inhibit-startup t))
2544 (org-mode)
2545 (show-all)
2546 (unless keepp (funcall binding arg))))
2547 (if (not keepp)
2548 (kill-buffer buffer)
2549 (switch-to-buffer-other-window buffer)
2550 (goto-char (point-min)))))
2552 (defun org-find-visible ()
2553 (let ((s (point)))
2554 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
2555 (get-char-property s 'invisible)))
2557 (defun org-find-invisible ()
2558 (let ((s (point)))
2559 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
2560 (not (get-char-property s 'invisible))))
2563 (defvar org-export-htmlized-org-css-url) ;; defined in org-html.el
2565 ;;;###autoload
2566 (defun org-export-as-org (arg &optional hidden ext-plist
2567 to-buffer body-only pub-dir)
2568 "Make a copy with not-exporting stuff removed.
2569 The purpose of this function is to provide a way to export the source
2570 Org file of a webpage in Org format, but with sensitive and/or irrelevant
2571 stuff removed. This command will remove the following:
2573 - archived trees (if the variable `org-export-with-archived-trees' is nil)
2574 - comment blocks and trees starting with the COMMENT keyword
2575 - only trees that are consistent with `org-export-select-tags'
2576 and `org-export-exclude-tags'.
2578 The only arguments that will be used are EXT-PLIST and PUB-DIR,
2579 all the others will be ignored (but are present so that the general
2580 mechanism to call publishing functions will work).
2582 EXT-PLIST is a property list with external parameters overriding
2583 org-mode's default settings, but still inferior to file-local
2584 settings. When PUB-DIR is set, use this as the publishing
2585 directory."
2586 (interactive "P")
2587 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
2588 ext-plist
2589 (org-infile-export-plist)))
2590 (bfname (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
2591 (filename (concat (file-name-as-directory
2592 (or pub-dir
2593 (org-export-directory :org opt-plist)))
2594 (file-name-sans-extension
2595 (file-name-nondirectory bfname))
2596 ".org"))
2597 (filename (and filename
2598 (if (equal (file-truename filename)
2599 (file-truename bfname))
2600 (concat (file-name-sans-extension filename)
2601 "-source."
2602 (file-name-extension filename))
2603 filename)))
2604 (backup-inhibited t)
2605 (buffer (find-file-noselect filename))
2606 (region (buffer-string))
2607 str-ret)
2608 (save-excursion
2609 (switch-to-buffer buffer)
2610 (erase-buffer)
2611 (insert region)
2612 (let ((org-inhibit-startup t)) (org-mode))
2613 (org-install-letbind)
2615 ;; Get rid of archived trees
2616 (org-export-remove-archived-trees (plist-get opt-plist :archived-trees))
2618 ;; Remove comment environment and comment subtrees
2619 (org-export-remove-comment-blocks-and-subtrees)
2621 ;; Get rid of excluded trees
2622 (org-export-handle-export-tags (plist-get opt-plist :select-tags)
2623 (plist-get opt-plist :exclude-tags))
2625 (when (or (plist-get opt-plist :plain-source)
2626 (not (or (plist-get opt-plist :plain-source)
2627 (plist-get opt-plist :htmlized-source))))
2628 ;; Either nothing special is requested (default call)
2629 ;; or the plain source is explicitly requested
2630 ;; so: save it
2631 (save-buffer))
2632 (when (plist-get opt-plist :htmlized-source)
2633 ;; Make the htmlized version
2634 (require 'htmlize)
2635 (require 'org-html)
2636 (font-lock-fontify-buffer)
2637 (let* ((htmlize-output-type 'css)
2638 (newbuf (htmlize-buffer)))
2639 (with-current-buffer newbuf
2640 (when org-export-htmlized-org-css-url
2641 (goto-char (point-min))
2642 (and (re-search-forward
2643 "<style type=\"text/css\">[^\000]*?\n[ \t]*</style>.*"
2644 nil t)
2645 (replace-match
2646 (format
2647 "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\">"
2648 org-export-htmlized-org-css-url)
2649 t t)))
2650 (write-file (concat filename ".html")))
2651 (kill-buffer newbuf)))
2652 (set-buffer-modified-p nil)
2653 (if (equal to-buffer 'string)
2654 (progn (setq str-ret (buffer-string))
2655 (kill-buffer (current-buffer))
2656 str-ret)
2657 (kill-buffer (current-buffer))))))
2659 (defvar org-archive-location) ;; gets loaded with the org-archive require.
2660 (defun org-get-current-options ()
2661 "Return a string with current options as keyword options.
2662 Does include HTML export options as well as TODO and CATEGORY stuff."
2663 (require 'org-archive)
2664 (format
2665 "#+TITLE: %s
2666 #+AUTHOR: %s
2667 #+EMAIL: %s
2668 #+DATE: %s
2669 #+DESCRIPTION:
2670 #+KEYWORDS:
2671 #+LANGUAGE: %s
2672 #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s -:%s f:%s *:%s <:%s
2673 #+OPTIONS: TeX:%s LaTeX:%s skip:%s d:%s todo:%s pri:%s tags:%s
2675 #+EXPORT_SELECT_TAGS: %s
2676 #+EXPORT_EXCLUDE_TAGS: %s
2677 #+LINK_UP: %s
2678 #+LINK_HOME: %s
2679 #+XSLT:
2680 #+CATEGORY: %s
2681 #+SEQ_TODO: %s
2682 #+TYP_TODO: %s
2683 #+PRIORITIES: %c %c %c
2684 #+DRAWERS: %s
2685 #+STARTUP: %s %s %s %s %s
2686 #+TAGS: %s
2687 #+FILETAGS: %s
2688 #+ARCHIVE: %s
2689 #+LINK: %s
2691 (buffer-name) (user-full-name) user-mail-address
2692 (format-time-string (substring (car org-time-stamp-formats) 1 -1))
2693 org-export-default-language
2694 org-export-headline-levels
2695 org-export-with-section-numbers
2696 org-export-with-toc
2697 org-export-preserve-breaks
2698 org-export-html-expand
2699 org-export-with-fixed-width
2700 org-export-with-tables
2701 org-export-with-sub-superscripts
2702 org-export-with-special-strings
2703 org-export-with-footnotes
2704 org-export-with-emphasize
2705 org-export-with-timestamps
2706 org-export-with-TeX-macros
2707 org-export-with-LaTeX-fragments
2708 org-export-skip-text-before-1st-heading
2709 org-export-with-drawers
2710 org-export-with-todo-keywords
2711 org-export-with-priority
2712 org-export-with-tags
2713 (if (featurep 'org-jsinfo) (org-infojs-options-inbuffer-template) "")
2714 (mapconcat 'identity org-export-select-tags " ")
2715 (mapconcat 'identity org-export-exclude-tags " ")
2716 org-export-html-link-up
2717 org-export-html-link-home
2718 (or (ignore-errors
2719 (file-name-sans-extension
2720 (file-name-nondirectory (buffer-file-name (buffer-base-buffer)))))
2721 "NOFILENAME")
2722 "TODO FEEDBACK VERIFY DONE"
2723 "Me Jason Marie DONE"
2724 org-highest-priority org-lowest-priority org-default-priority
2725 (mapconcat 'identity org-drawers " ")
2726 (cdr (assoc org-startup-folded
2727 '((nil . "showall") (t . "overview") (content . "content"))))
2728 (if org-odd-levels-only "odd" "oddeven")
2729 (if org-hide-leading-stars "hidestars" "showstars")
2730 (if org-startup-align-all-tables "align" "noalign")
2731 (cond ((eq org-log-done t) "logdone")
2732 ((equal org-log-done 'note) "lognotedone")
2733 ((not org-log-done) "nologdone"))
2734 (or (mapconcat (lambda (x)
2735 (cond
2736 ((equal :startgroup (car x)) "{")
2737 ((equal :endgroup (car x)) "}")
2738 ((equal :newline (car x)) "")
2739 ((cdr x) (format "%s(%c)" (car x) (cdr x)))
2740 (t (car x))))
2741 (or org-tag-alist (org-get-buffer-tags)) " ") "")
2742 (mapconcat 'identity org-file-tags " ")
2743 org-archive-location
2744 "org file:~/org/%s.org"
2747 ;;;###autoload
2748 (defun org-insert-export-options-template ()
2749 "Insert into the buffer a template with information for exporting."
2750 (interactive)
2751 (if (not (bolp)) (newline))
2752 (let ((s (org-get-current-options)))
2753 (and (string-match "#\\+CATEGORY" s)
2754 (setq s (substring s 0 (match-beginning 0))))
2755 (insert s)))
2757 (defvar org-table-colgroup-info nil)
2759 (defun org-table-clean-before-export (lines &optional maybe-quoted)
2760 "Check if the table has a marking column.
2761 If yes remove the column and the special lines."
2762 (setq org-table-colgroup-info nil)
2763 (if (memq nil
2764 (mapcar
2765 (lambda (x) (or (string-match "^[ \t]*|-" x)
2766 (string-match
2767 (if maybe-quoted
2768 "^[ \t]*| *\\\\?\\([\#!$*_^ /]\\) *|"
2769 "^[ \t]*| *\\([\#!$*_^ /]\\) *|")
2770 x)))
2771 lines))
2772 ;; No special marking column
2773 (progn
2774 (setq org-table-clean-did-remove-column nil)
2775 (delq nil
2776 (mapcar
2777 (lambda (x)
2778 (cond
2779 ((org-table-colgroup-line-p x)
2780 ;; This line contains colgroup info, extract it
2781 ;; and then discard the line
2782 (setq org-table-colgroup-info
2783 (mapcar (lambda (x)
2784 (cond ((member x '("<" "&lt;")) :start)
2785 ((member x '(">" "&gt;")) :end)
2786 ((member x '("<>" "&lt;&gt;")) :startend)
2787 (t nil)))
2788 (org-split-string x "[ \t]*|[ \t]*")))
2789 nil)
2790 ((org-table-cookie-line-p x)
2791 ;; This line contains formatting cookies, discard it
2792 nil)
2793 (t x)))
2794 lines)))
2795 ;; there is a special marking column
2796 (setq org-table-clean-did-remove-column t)
2797 (delq nil
2798 (mapcar
2799 (lambda (x)
2800 (cond
2801 ((org-table-colgroup-line-p x)
2802 ;; This line contains colgroup info, extract it
2803 ;; and then discard the line
2804 (setq org-table-colgroup-info
2805 (mapcar (lambda (x)
2806 (cond ((member x '("<" "&lt;")) :start)
2807 ((member x '(">" "&gt;")) :end)
2808 ((member x '("<>" "&lt;&gt;")) :startend)
2809 (t nil)))
2810 (cdr (org-split-string x "[ \t]*|[ \t]*"))))
2811 nil)
2812 ((org-table-cookie-line-p x)
2813 ;; This line contains formatting cookies, discard it
2814 nil)
2815 ((string-match "^[ \t]*| *[!_^/] *|" x)
2816 ;; ignore this line
2817 nil)
2818 ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
2819 (string-match "^\\([ \t]*\\)|[^|]*|" x))
2820 ;; remove the first column
2821 (replace-match "\\1|" t nil x))))
2822 lines))))
2824 (defun org-export-cleanup-toc-line (s)
2825 "Remove tags and timestamps from lines going into the toc."
2826 (when (memq org-export-with-tags '(not-in-toc nil))
2827 (if (string-match (org-re " +:[[:alnum:]_@#%:]+: *$") s)
2828 (setq s (replace-match "" t t s))))
2829 (when org-export-remove-timestamps-from-toc
2830 (while (string-match org-maybe-keyword-time-regexp s)
2831 (setq s (replace-match "" t t s))))
2832 (while (string-match org-bracket-link-regexp s)
2833 (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
2834 t t s)))
2835 (while (string-match "\\[\\([0-9]\\|fn:[^]]*\\)\\]" s)
2836 (setq s (replace-match "" t t s)))
2840 (defun org-get-text-property-any (pos prop &optional object)
2841 (or (get-text-property pos prop object)
2842 (and (setq pos (next-single-property-change pos prop object))
2843 (get-text-property pos prop object))))
2845 (defun org-export-get-coderef-format (path desc)
2846 (save-match-data
2847 (if (and desc (string-match
2848 (regexp-quote (concat "(" path ")"))
2849 desc))
2850 (replace-match "%s" t t desc)
2851 (or desc "%s"))))
2853 (defun org-export-push-to-kill-ring (format)
2854 "Push buffer content to kill ring.
2855 The depends on the variable `org-export-copy-to-kill'."
2856 (when org-export-copy-to-kill-ring
2857 (org-kill-new (buffer-string))
2858 (when (fboundp 'x-set-selection)
2859 (ignore-errors (x-set-selection 'PRIMARY (buffer-string)))
2860 (ignore-errors (x-set-selection 'CLIPBOARD (buffer-string))))
2861 (message "%s export done, pushed to kill ring and clipboard" format)))
2863 (provide 'org-exp)
2865 ;; arch-tag: 65985fe9-095c-49c7-a7b6-cb4ee15c0a95
2867 ;;; org-exp.el ends here