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