Fix bug with line protection while including source code files
[org-mode.git] / lisp / org-exp.el
blob47e3c9cf185859bbd5a96080df5312c58b7d672a
1 ;;; org-exp.el --- ASCII, HTML, XOXO and iCalendar export for Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
4 ;; Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
9 ;; Version: 6.30a
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;;; Commentary:
29 (require 'org)
30 (require 'org-agenda)
31 (require 'org-exp-blocks)
32 (eval-when-compile
33 (require 'cl))
35 (declare-function org-export-latex-preprocess "org-latex" (parameters))
36 (declare-function org-export-ascii-preprocess "org-ascii" (parameters))
37 (declare-function org-export-html-preprocess "org-html" (parameters))
38 (declare-function org-export-docbook-preprocess "org-docbook" (parameters))
39 (declare-function org-agenda-skip "org-agenda" ())
40 (declare-function org-infojs-options-inbuffer-template "org-jsinfo" ())
41 (declare-function org-export-htmlize-region-for-paste "org-html" (beg end))
42 (declare-function htmlize-buffer "htmlize" (&optional buffer))
43 (autoload 'org-export-generic "org-export-generic" "Export using the generic exporter" t)
44 (defgroup org-export nil
45 "Options for exporting org-listings."
46 :tag "Org Export"
47 :group 'org)
49 (defgroup org-export-general nil
50 "General options for exporting Org-mode files."
51 :tag "Org Export General"
52 :group 'org-export)
54 (defcustom org-export-allow-BIND 'confirm
55 "Non-nil means, allow #+BIND to define local variable values for export.
56 This is a potential security risk, which is why the user must confirm the
57 use of these lines."
58 :group 'org-export-general
59 :type '(choice
60 (const :tag "Never" nil)
61 (const :tag "Always" t)
62 (const :tag "Make the user confirm for each file" confirm)))
64 ;; FIXME
65 (defvar org-export-publishing-directory nil)
67 (defcustom org-export-show-temporary-export-buffer t
68 "Non-nil means, show buffer after exporting to temp buffer.
69 When Org exports to a file, the buffer visiting that file is ever
70 shown, but remains buried. However, when exporting to a temporary
71 buffer, that buffer is popped up in a second window. When this variable
72 is nil, the buffer remains buried also in these cases."
73 :group 'org-export-general
74 :type 'boolean)
76 (defcustom org-export-copy-to-kill-ring t
77 "Non-nil means, exported stuff will also be pushed onto the kill ring."
78 :group 'org-export-general
79 :type 'boolean)
81 (defcustom org-export-run-in-background nil
82 "Non-nil means export and publishing commands will run in background.
83 This works by starting up a separate Emacs process visiting the same file
84 and doing the export from there.
85 Not all export commands are affected by this - only the ones which
86 actually write to a file, and that do not depend on the buffer state.
88 If this option is nil, you can still get background export by calling
89 `org-export' with a double prefix arg: `C-u C-u C-c C-e'.
91 If this option is t, the double prefix can be used to exceptionally
92 force an export command into the current process."
93 :group 'org-export-general
94 :type 'boolean)
96 (defcustom org-export-select-tags '("export")
97 "Tags that select a tree for export.
98 If any such tag is found in a buffer, all trees that do not carry one
99 of these tags will be deleted before export.
100 Inside trees that are selected like this, you can still deselect a
101 subtree by tagging it with one of the `org-export-exclude-tags'."
102 :group 'org-export-general
103 :type '(repeat (string :tag "Tag")))
105 (defcustom org-export-exclude-tags '("noexport")
106 "Tags that exclude a tree from export.
107 All trees carrying any of these tags will be excluded from export.
108 This is without condition, so even subtrees inside that carry one of the
109 `org-export-select-tags' will be removed."
110 :group 'org-export-general
111 :type '(repeat (string :tag "Tag")))
113 ;; FIXME: rename, this is a general variable
114 (defcustom org-export-html-expand t
115 "Non-nil means, for HTML export, treat @<...> as HTML tag.
116 When nil, these tags will be exported as plain text and therefore
117 not be interpreted by a browser.
119 This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
120 :group 'org-export-html
121 :group 'org-export-general
122 :type 'boolean)
124 (defcustom org-export-with-special-strings t
125 "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
126 When this option is turned on, these strings will be exported as:
128 Org HTML LaTeX
129 -----+----------+--------
130 \\- &shy; \\-
131 -- &ndash; --
132 --- &mdash; ---
133 ... &hellip; \ldots
135 This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
136 :group 'org-export-translation
137 :type 'boolean)
139 (defcustom org-export-html-link-up ""
140 "Where should the \"UP\" link of exported HTML pages lead?"
141 :group 'org-export-html
142 :group 'org-export-general
143 :type '(string :tag "File or URL"))
145 (defcustom org-export-html-link-home ""
146 "Where should the \"HOME\" link of exported HTML pages lead?"
147 :group 'org-export-html
148 :group 'org-export-general
149 :type '(string :tag "File or URL"))
151 (defcustom org-export-language-setup
152 '(("en" "Author" "Date" "Table of Contents" "Footnotes")
153 ("ca" "Autor" "Data" "&Iacute;ndex" "Peus de p&agrave;gina")
154 ("cs" "Autor" "Datum" "Obsah" "Pozn\xe1mky pod carou")
155 ("da" "Ophavsmand" "Dato" "Indhold" "Fodnoter")
156 ("de" "Autor" "Datum" "Inhaltsverzeichnis" "Fu&szlig;noten")
157 ("eo" "A&#365;toro" "Dato" "Enhavo" "Piednotoj")
158 ("es" "Autor" "Fecha" "&Iacute;ndice" "Pies de p&aacute;gina")
159 ("fi" "Tekij&auml;" "P&auml;iv&auml;m&auml;&auml;r&auml;" "Sis&auml;llysluettelo" "Alaviitteet")
160 ("fr" "Auteur" "Date" "Table des mati&egrave;res" "Notes de bas de page")
161 ("hu" "Szerz&otilde;" "D&aacute;tum" "Tartalomjegyz&eacute;k" "L&aacute;bjegyzet")
162 ("is" "H&ouml;fundur" "Dagsetning" "Efnisyfirlit" "Aftanm&aacute;lsgreinar")
163 ("it" "Autore" "Data" "Indice" "Note a pi&egrave; di pagina")
164 ("nl" "Auteur" "Datum" "Inhoudsopgave" "Voetnoten")
165 ("no" "Forfatter" "Dato" "Innhold" "Fotnoter")
166 ("nb" "Forfatter" "Dato" "Innhold" "Fotnoter") ;; nb = Norsk (bokm.l)
167 ("nn" "Forfattar" "Dato" "Innhald" "Fotnotar") ;; nn = Norsk (nynorsk)
168 ("pl" "Autor" "Data" "Spis tre&sacute;ci" "Przypis")
169 ("sv" "F&ouml;rfattare" "Datum" "Inneh&aring;ll" "Fotnoter"))
170 "Terms used in export text, translated to different languages.
171 Use the variable `org-export-default-language' to set the language,
172 or use the +OPTION lines for a per-file setting."
173 :group 'org-export-general
174 :type '(repeat
175 (list
176 (string :tag "HTML language tag")
177 (string :tag "Author")
178 (string :tag "Date")
179 (string :tag "Table of Contents")
180 (string :tag "Footnotes"))))
182 (defcustom org-export-default-language "en"
183 "The default language of HTML export, as a string.
184 This should have an association in `org-export-language-setup'."
185 :group 'org-export-general
186 :type 'string)
188 (defvar org-export-page-description ""
189 "The page description, for the XHTML meta tag.
190 This is best set with the #+DESCRIPTION line in a file, it does not make
191 sense to set this globally.")
193 (defvar org-export-page-keywords ""
194 "The page description, for the XHTML meta tag.
195 This is best set with the #+KEYWORDS line in a file, it does not make
196 sense to set this globally.")
198 (defcustom org-export-skip-text-before-1st-heading nil
199 "Non-nil means, skip all text before the first headline when exporting.
200 When nil, that text is exported as well."
201 :group 'org-export-general
202 :type 'boolean)
204 (defcustom org-export-headline-levels 3
205 "The last level which is still exported as a headline.
206 Inferior levels will produce itemize lists when exported.
207 Note that a numeric prefix argument to an exporter function overrides
208 this setting.
210 This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
211 :group 'org-export-general
212 :type 'integer)
214 (defcustom org-export-with-section-numbers t
215 "Non-nil means, add section numbers to headlines when exporting.
217 This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
218 :group 'org-export-general
219 :type 'boolean)
221 (defcustom org-export-section-number-format '((("1" ".")) . "")
222 "Format of section numbers for export.
223 The variable has two components.
224 1. A list of lists, each indicating a counter type and a separator.
225 The counter type can be any of \"1\", \"A\", \"a\", \"I\", or \"a\".
226 It causes causes numeric, alphabetic, or roman counters, respectively.
227 The separator is only used if another counter for a subsection is being
228 added.
229 If there are more numbered section levels than entries in this lists,
230 then the last entry will be reused.
231 2. A terminator string that will be added after the entire
232 section number."
233 :group 'org-export-general
234 :type '(cons
235 (repeat
236 (list
237 (string :tag "Counter Type")
238 (string :tag "Separator ")))
239 (string :tag "Terminator")))
241 (defcustom org-export-with-toc t
242 "Non-nil means, create a table of contents in exported files.
243 The TOC contains headlines with levels up to`org-export-headline-levels'.
244 When an integer, include levels up to N in the toc, this may then be
245 different from `org-export-headline-levels', but it will not be allowed
246 to be larger than the number of headline levels.
247 When nil, no table of contents is made.
249 Headlines which contain any TODO items will be marked with \"(*)\" in
250 ASCII export, and with red color in HTML output, if the option
251 `org-export-mark-todo-in-toc' is set.
253 In HTML output, the TOC will be clickable.
255 This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
256 or \"toc:3\"."
257 :group 'org-export-general
258 :type '(choice
259 (const :tag "No Table of Contents" nil)
260 (const :tag "Full Table of Contents" t)
261 (integer :tag "TOC to level")))
263 (defcustom org-export-mark-todo-in-toc nil
264 "Non-nil means, mark TOC lines that contain any open TODO items."
265 :group 'org-export-general
266 :type 'boolean)
268 (defcustom org-export-with-todo-keywords t
269 "Non-nil means, include TODO keywords in export.
270 When nil, remove all these keywords from the export."
271 :group 'org-export-general
272 :type 'boolean)
274 (defcustom org-export-with-priority nil
275 "Non-nil means, include priority cookies in export.
276 When nil, remove priority cookies for export."
277 :group 'org-export-general
278 :type 'boolean)
280 (defcustom org-export-preserve-breaks nil
281 "Non-nil means, preserve all line breaks when exporting.
282 Normally, in HTML output paragraphs will be reformatted. In ASCII
283 export, line breaks will always be preserved, regardless of this variable.
285 This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
286 :group 'org-export-general
287 :type 'boolean)
289 (defcustom org-export-with-archived-trees 'headline
290 "Whether subtrees with the ARCHIVE tag should be exported.
291 This can have three different values
292 nil Do not export, pretend this tree is not present
293 t Do export the entire tree
294 headline Only export the headline, but skip the tree below it."
295 :group 'org-export-general
296 :group 'org-archive
297 :type '(choice
298 (const :tag "not at all" nil)
299 (const :tag "headline only" 'headline)
300 (const :tag "entirely" t)))
302 (defcustom org-export-author-info t
303 "Non-nil means, insert author name and email into the exported file.
305 This option can also be set with the +OPTIONS line,
306 e.g. \"author-info:nil\"."
307 :group 'org-export-general
308 :type 'boolean)
310 (defcustom org-export-creator-info t
311 "Non-nil means, the postamble should contain a creator sentence.
312 This sentence is \"HTML generated by org-mode XX in emacs XXX\"."
313 :group 'org-export-general
314 :type 'boolean)
316 (defcustom org-export-time-stamp-file t
317 "Non-nil means, insert a time stamp into the exported file.
318 The time stamp shows when the file was created.
320 This option can also be set with the +OPTIONS line,
321 e.g. \"timestamp:nil\"."
322 :group 'org-export-general
323 :type 'boolean)
325 (defcustom org-export-with-timestamps t
326 "If nil, do not export time stamps and associated keywords."
327 :group 'org-export-general
328 :type 'boolean)
330 (defcustom org-export-remove-timestamps-from-toc t
331 "If t, remove timestamps from the table of contents entries."
332 :group 'org-export-general
333 :type 'boolean)
335 (defcustom org-export-with-tags 'not-in-toc
336 "If nil, do not export tags, just remove them from headlines.
337 If this is the symbol `not-in-toc', tags will be removed from table of
338 contents entries, but still be shown in the headlines of the document.
340 This option can also be set with the +OPTIONS line, e.g. \"tags:nil\"."
341 :group 'org-export-general
342 :type '(choice
343 (const :tag "Off" nil)
344 (const :tag "Not in TOC" not-in-toc)
345 (const :tag "On" t)))
347 (defcustom org-export-with-drawers nil
348 "Non-nil means, export with drawers like the property drawer.
349 When t, all drawers are exported. This may also be a list of
350 drawer names to export."
351 :group 'org-export-general
352 :type '(choice
353 (const :tag "All drawers" t)
354 (const :tag "None" nil)
355 (repeat :tag "Selected drawers"
356 (string :tag "Drawer name"))))
358 (defvar org-export-preprocess-hook nil
359 "Hook for preprocessing an export buffer.
360 Pretty much the first thing when exporting is running this hook.")
362 (defvar org-export-preprocess-after-include-files-hook nil
363 "Hook for preprocessing an export buffer.
364 This is run after the contents of included files have been inserted.")
366 (defvar org-export-preprocess-after-tree-selection-hook nil
367 "Hook for preprocessing an export buffer.
368 This is run after selection of trees to be exported has happened.
369 This selection includes tags-based selection, as well as removal
370 of commented and archived trees.")
372 (defvar org-export-preprocess-after-blockquote-hook nil
373 "Hook for preprocessing an export buffer.
374 This is run after blockquote/quote/verse/center have been marked
375 with cookies.")
377 (defvar org-export-preprocess-before-backend-specifics-hook nil
378 "Hook run before backend-specific functions are called during preprocessing.")
380 (defvar org-export-preprocess-final-hook nil
381 "Hook for preprocessing an export buffer.
382 This is run as the last thing in the preprocessing buffer, just before
383 returning the buffer string to the backend.")
385 (defgroup org-export-translation nil
386 "Options for translating special ascii sequences for the export backends."
387 :tag "Org Export Translation"
388 :group 'org-export)
390 (defcustom org-export-with-emphasize t
391 "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
392 If the export target supports emphasizing text, the word will be
393 typeset in bold, italic, or underlined, respectively. Works only for
394 single words, but you can say: I *really* *mean* *this*.
395 Not all export backends support this.
397 This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
398 :group 'org-export-translation
399 :type 'boolean)
401 (defcustom org-export-with-footnotes t
402 "If nil, export [1] as a footnote marker.
403 Lines starting with [1] will be formatted as footnotes.
405 This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
406 :group 'org-export-translation
407 :type 'boolean)
409 (defcustom org-export-with-sub-superscripts t
410 "Non-nil means, interpret \"_\" and \"^\" for export.
411 When this option is turned on, you can use TeX-like syntax for sub- and
412 superscripts. Several characters after \"_\" or \"^\" will be
413 considered as a single item - so grouping with {} is normally not
414 needed. For example, the following things will be parsed as single
415 sub- or superscripts.
417 10^24 or 10^tau several digits will be considered 1 item.
418 10^-12 or 10^-tau a leading sign with digits or a word
419 x^2-y^3 will be read as x^2 - y^3, because items are
420 terminated by almost any nonword/nondigit char.
421 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
423 Still, ambiguity is possible - so when in doubt use {} to enclose the
424 sub/superscript. If you set this variable to the symbol `{}',
425 the braces are *required* in order to trigger interpretations as
426 sub/superscript. This can be helpful in documents that need \"_\"
427 frequently in plain text.
429 Not all export backends support this, but HTML does.
431 This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
432 :group 'org-export-translation
433 :type '(choice
434 (const :tag "Always interpret" t)
435 (const :tag "Only with braces" {})
436 (const :tag "Never interpret" nil)))
438 (defcustom org-export-with-TeX-macros t
439 "Non-nil means, interpret simple TeX-like macros when exporting.
440 For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
441 Not only real TeX macros will work here, but the standard HTML entities
442 for math can be used as macro names as well. For a list of supported
443 names in HTML export, see the constant `org-html-entities'.
444 Not all export backends support this.
446 This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
447 :group 'org-export-translation
448 :group 'org-export-latex
449 :type 'boolean)
451 (defcustom org-export-with-LaTeX-fragments nil
452 "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
453 When set, the exporter will find LaTeX environments if the \\begin line is
454 the first non-white thing on a line. It will also find the math delimiters
455 like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
456 display math.
458 This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\".
460 The default is nil, because this option needs the `dvipng' program which
461 is not available on all systems."
462 :group 'org-export-translation
463 :group 'org-export-latex
464 :type 'boolean)
466 (defcustom org-export-with-fixed-width t
467 "Non-nil means, lines starting with \":\" will be in fixed width font.
468 This can be used to have pre-formatted text, fragments of code etc. For
469 example:
470 : ;; Some Lisp examples
471 : (while (defc cnt)
472 : (ding))
473 will be looking just like this in also HTML. See also the QUOTE keyword.
474 Not all export backends support this.
476 This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
477 :group 'org-export-translation
478 :type 'boolean)
480 (defcustom org-match-sexp-depth 3
481 "Number of stacked braces for sub/superscript matching.
482 This has to be set before loading org.el to be effective."
483 :group 'org-export-translation
484 :type 'integer)
486 (defgroup org-export-tables nil
487 "Options for exporting tables in Org-mode."
488 :tag "Org Export Tables"
489 :group 'org-export)
491 (defcustom org-export-with-tables t
492 "If non-nil, lines starting with \"|\" define a table.
493 For example:
495 | Name | Address | Birthday |
496 |-------------+----------+-----------|
497 | Arthur Dent | England | 29.2.2100 |
499 Not all export backends support this.
501 This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
502 :group 'org-export-tables
503 :type 'boolean)
505 (defcustom org-export-highlight-first-table-line t
506 "Non-nil means, highlight the first table line.
507 In HTML export, this means use <th> instead of <td>.
508 In tables created with table.el, this applies to the first table line.
509 In Org-mode tables, all lines before the first horizontal separator
510 line will be formatted with <th> tags."
511 :group 'org-export-tables
512 :type 'boolean)
514 (defcustom org-export-table-remove-special-lines t
515 "Remove special lines and marking characters in calculating tables.
516 This removes the special marking character column from tables that are set
517 up for spreadsheet calculations. It also removes the entire lines
518 marked with `!', `_', or `^'. The lines with `$' are kept, because
519 the values of constants may be useful to have."
520 :group 'org-export-tables
521 :type 'boolean)
523 (defcustom org-export-prefer-native-exporter-for-tables nil
524 "Non-nil means, always export tables created with table.el natively.
525 Natively means, use the HTML code generator in table.el.
526 When nil, Org-mode's own HTML generator is used when possible (i.e. if
527 the table does not use row- or column-spanning). This has the
528 advantage, that the automatic HTML conversions for math symbols and
529 sub/superscripts can be applied. Org-mode's HTML generator is also
530 much faster."
531 :group 'org-export-tables
532 :type 'boolean)
535 (defgroup org-export-xml nil
536 "Options specific for XML export of Org-mode files."
537 :tag "Org Export XML"
538 :group 'org-export)
540 ;;;; Exporting
542 ;;; Variables, constants, and parameter plists
544 (defconst org-level-max 20)
546 (defvar org-current-export-file nil) ; dynamically scoped parameter
547 (defvar org-current-export-dir nil) ; dynamically scoped parameter
548 (defvar org-export-opt-plist nil
549 "Contains the current option plist.")
550 (defvar org-last-level nil) ; dynamically scoped variable
551 (defvar org-min-level nil) ; dynamically scoped variable
552 (defvar org-levels-open nil) ; dynamically scoped parameter
554 (defconst org-export-plist-vars
555 '((:link-up nil org-export-html-link-up)
556 (:link-home nil org-export-html-link-home)
557 (:language nil org-export-default-language)
558 (:keywords nil org-export-page-keywords)
559 (:description nil org-export-page-description)
560 (:customtime nil org-display-custom-times)
561 (:headline-levels "H" org-export-headline-levels)
562 (:section-numbers "num" org-export-with-section-numbers)
563 (:section-number-format nil org-export-section-number-format)
564 (:table-of-contents "toc" org-export-with-toc)
565 (:preserve-breaks "\\n" org-export-preserve-breaks)
566 (:archived-trees nil org-export-with-archived-trees)
567 (:emphasize "*" org-export-with-emphasize)
568 (:sub-superscript "^" org-export-with-sub-superscripts)
569 (:special-strings "-" org-export-with-special-strings)
570 (:footnotes "f" org-export-with-footnotes)
571 (:drawers "d" org-export-with-drawers)
572 (:tags "tags" org-export-with-tags)
573 (:todo-keywords "todo" org-export-with-todo-keywords)
574 (:priority "pri" org-export-with-priority)
575 (:TeX-macros "TeX" org-export-with-TeX-macros)
576 (:LaTeX-fragments "LaTeX" org-export-with-LaTeX-fragments)
577 (:latex-listings nil org-export-latex-listings)
578 (:skip-before-1st-heading "skip" org-export-skip-text-before-1st-heading)
579 (:fixed-width ":" org-export-with-fixed-width)
580 (:timestamps "<" org-export-with-timestamps)
581 (:author-info "author" org-export-author-info)
582 (:creator-info "creator" org-export-creator-info)
583 (:time-stamp-file "timestamp" org-export-time-stamp-file)
584 (:tables "|" org-export-with-tables)
585 (:table-auto-headline nil org-export-highlight-first-table-line)
586 (:style-include-default nil org-export-html-style-include-default)
587 (:style-include-scripts nil org-export-html-style-include-scripts)
588 (:style nil org-export-html-style)
589 (:style-extra nil org-export-html-style-extra)
590 (:agenda-style nil org-agenda-export-html-style)
591 (:convert-org-links nil org-export-html-link-org-files-as-html)
592 (:inline-images nil org-export-html-inline-images)
593 (:html-extension nil org-export-html-extension)
594 (:xml-declaration nil org-export-html-xml-declaration)
595 (:html-table-tag nil org-export-html-table-tag)
596 (:expand-quoted-html "@" org-export-html-expand)
597 (:timestamp nil org-export-html-with-timestamp)
598 (:publishing-directory nil org-export-publishing-directory)
599 (:preamble nil org-export-html-preamble)
600 (:postamble nil org-export-html-postamble)
601 (:auto-preamble nil org-export-html-auto-preamble)
602 (:auto-postamble nil org-export-html-auto-postamble)
603 (:author nil user-full-name)
604 (:email nil user-mail-address)
605 (:select-tags nil org-export-select-tags)
606 (:exclude-tags nil org-export-exclude-tags)
608 (:latex-image-options nil org-export-latex-image-default-option))
609 "List of properties that represent export/publishing variables.
610 Each element is a list of 3 items:
611 1. The property that is used internally, and also for org-publish-project-alist
612 2. The string that can be used in the OPTION lines to set this option,
613 or nil if this option cannot be changed in this way
614 3. The customization variable that sets the default for this option."
617 (defun org-default-export-plist ()
618 "Return the property list with default settings for the export variables."
619 (let* ((infile (org-infile-export-plist))
620 (letbind (plist-get infile :let-bind))
621 (l org-export-plist-vars) rtn e s v)
622 (while (setq e (pop l))
623 (setq s (nth 2 e)
624 v (cond
625 ((assq s letbind) (nth 1 (assq s letbind)))
626 ((boundp s) (symbol-value s))
627 (t nil))
628 rtn (cons (car e) (cons v rtn))))
629 rtn))
631 (defvar org-export-inbuffer-options-extra nil
632 "List of additional in-buffer options that should be detected.
633 Just before export, the buffer is scanned for options like #+TITLE, #+EMAIL,
634 etc. Extensions can add to this list to get their options detected, and they
635 can then add a function to `org-export-options-filters' to process these
636 options.
637 Each element in this list must be a list, with the in-buffer keyword as car,
638 and a property (a symbol) as the next element. All occurrences of the
639 keyword will be found, the values concatenated with a space character
640 in between, and the result stored in the export options property list.")
642 (defvar org-export-options-filters nil
643 "Functions to be called to finalize the export/publishing options.
644 All these options are stored in a property list, and each of the functions
645 in this hook gets a chance to modify this property list. Each function
646 must accept the property list as an argument, and must return the (possibly
647 modified) list.")
649 ;; FIXME: should we fold case here?
650 (defun org-infile-export-plist ()
651 "Return the property list with file-local settings for export."
652 (save-excursion
653 (save-restriction
654 (widen)
655 (goto-char (point-min))
656 (let ((re (org-make-options-regexp
657 (append
658 '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE"
659 "LINK_UP" "LINK_HOME" "SETUPFILE" "STYLE" "LATEX_HEADER"
660 "EXPORT_SELECT_TAGS" "EXPORT_EXCLUDE_TAGS"
661 "KEYWORDS" "DESCRIPTION" "MACRO" "BIND")
662 (mapcar 'car org-export-inbuffer-options-extra))))
663 p key val text options a pr style
664 latex-header macros letbind
665 ext-setup-or-nil setup-contents (start 0))
666 (while (or (and ext-setup-or-nil
667 (string-match re ext-setup-or-nil start)
668 (setq start (match-end 0)))
669 (and (setq ext-setup-or-nil nil start 0)
670 (re-search-forward re nil t)))
671 (setq key (upcase (org-match-string-no-properties 1 ext-setup-or-nil))
672 val (org-match-string-no-properties 2 ext-setup-or-nil))
673 (cond
674 ((setq a (assoc key org-export-inbuffer-options-extra))
675 (setq pr (nth 1 a))
676 (setq p (plist-put p pr (concat (plist-get p pr) " " val))))
677 ((string-equal key "TITLE") (setq p (plist-put p :title val)))
678 ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
679 ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
680 ((string-equal key "DATE") (setq p (plist-put p :date val)))
681 ((string-equal key "KEYWORDS") (setq p (plist-put p :keywords val)))
682 ((string-equal key "DESCRIPTION")
683 (setq p (plist-put p :description val)))
684 ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
685 ((string-equal key "STYLE")
686 (setq style (concat style "\n" val)))
687 ((string-equal key "LATEX_HEADER")
688 (setq latex-header (concat latex-header "\n" val)))
689 ((string-equal key "TEXT")
690 (setq text (if text (concat text "\n" val) val)))
691 ((string-equal key "OPTIONS")
692 (setq options (concat val " " options)))
693 ((string-equal key "BIND")
694 (push (read (concat "(" val ")")) letbind))
695 ((string-equal key "LINK_UP")
696 (setq p (plist-put p :link-up val)))
697 ((string-equal key "LINK_HOME")
698 (setq p (plist-put p :link-home val)))
699 ((string-equal key "EXPORT_SELECT_TAGS")
700 (setq p (plist-put p :select-tags (org-split-string val))))
701 ((string-equal key "EXPORT_EXCLUDE_TAGS")
702 (setq p (plist-put p :exclude-tags (org-split-string val))))
703 ((string-equal key "MACRO")
704 (push val macros))
705 ((equal key "SETUPFILE")
706 (setq setup-contents (org-file-contents
707 (expand-file-name
708 (org-remove-double-quotes
709 (org-trim val)))
710 'noerror))
711 (if (not ext-setup-or-nil)
712 (setq ext-setup-or-nil setup-contents start 0)
713 (setq ext-setup-or-nil
714 (concat (substring ext-setup-or-nil 0 start)
715 "\n" setup-contents "\n"
716 (substring ext-setup-or-nil start)))))))
717 (setq p (plist-put p :text text))
718 (when (and letbind (org-export-confirm-letbind))
719 (setq p (plist-put p :let-bind letbind)))
720 (when style (setq p (plist-put p :style-extra style)))
721 (when latex-header
722 (setq p (plist-put p :latex-header-extra (substring latex-header 1))))
723 (when options
724 (setq p (org-export-add-options-to-plist p options)))
725 ;; Add macro definitions
726 (setq p (plist-put p :macro-date "(eval (format-time-string \"$1\"))"))
727 (setq p (plist-put p :macro-time "(eval (format-time-string \"$1\"))"))
728 (setq p (plist-put
729 p :macro-modification-time
730 (and (buffer-file-name)
731 (file-exists-p (buffer-file-name))
732 (concat
733 "(eval (format-time-string \"$1\" '"
734 (prin1-to-string (nth 5 (file-attributes
735 (buffer-file-name))))
736 "))"))))
737 (setq p (plist-put p :macro-input-file (and (buffer-file-name)
738 (file-name-nondirectory
739 (buffer-file-name)))))
740 (while (setq val (pop macros))
741 (when (string-match "^\\([-a-zA-Z0-9_]+\\)[ \t]+\\(.*?[ \t]*$\\)" val)
742 (setq p (plist-put
743 p (intern
744 (concat ":macro-" (downcase (match-string 1 val))))
745 (org-export-interpolate-newlines (match-string 2 val))))))
746 p))))
748 (defun org-export-interpolate-newlines (s)
749 (while (string-match "\\\\n" s)
750 (setq s (replace-match "\n" t t s)))
753 (defvar org-export-allow-BIND-local nil)
754 (defun org-export-confirm-letbind ()
755 "Can we use #+BIND values during export?
756 By default this will ask fro confirmation by the user, to divert possible
757 security risks."
758 (cond
759 ((not org-export-allow-BIND) nil)
760 ((eq org-export-allow-BIND t) t)
761 ((local-variable-p 'org-export-allow-BIND-local (current-buffer))
762 org-export-allow-BIND-local)
763 (t (org-set-local 'org-export-allow-BIND-local
764 (yes-or-no-p "Allow BIND values in this buffer? ")))))
766 (defun org-install-letbind ()
767 "Install the values from #+BIND lines as local variables."
768 (let ((letbind (plist-get org-export-opt-plist :let-bind)))
769 (while letbind
770 (org-set-local (caar letbind) (nth 1 (pop letbind))))))
772 (defun org-export-add-options-to-plist (p options)
773 "Parse an OPTIONS line and set values in the property list P."
774 (let (o)
775 (when options
776 (let ((op org-export-plist-vars))
777 (while (setq o (pop op))
778 (if (and (nth 1 o)
779 (string-match (concat (regexp-quote (nth 1 o))
780 ":\\([^ \t\n\r;,.]*\\)")
781 options))
782 (setq p (plist-put p (car o)
783 (car (read-from-string
784 (match-string 1 options))))))))))
787 (defun org-export-add-subtree-options (p pos)
788 "Add options in subtree at position POS to property list P."
789 (save-excursion
790 (goto-char pos)
791 (when (org-at-heading-p)
792 (let (a)
793 ;; This is actually read in `org-export-get-title-from-subtree'
794 ;; (when (setq a (org-entry-get pos "EXPORT_TITLE"))
795 ;; (setq p (plist-put p :title a)))
796 (when (setq a (org-entry-get pos "EXPORT_TEXT"))
797 (setq p (plist-put p :text a)))
798 (when (setq a (org-entry-get pos "EXPORT_AUTHOR"))
799 (setq p (plist-put p :author a)))
800 (when (setq a (org-entry-get pos "EXPORT_DATE"))
801 (setq p (plist-put p :date a)))
802 (when (setq a (org-entry-get pos "EXPORT_OPTIONS"))
803 (setq p (org-export-add-options-to-plist p a)))))
806 (defun org-export-directory (type plist)
807 (let* ((val (plist-get plist :publishing-directory))
808 (dir (if (listp val)
809 (or (cdr (assoc type val)) ".")
810 val)))
811 dir))
813 (defun org-export-process-option-filters (plist)
814 (let ((functions org-export-options-filters) f)
815 (while (setq f (pop functions))
816 (setq plist (funcall f plist))))
817 plist)
819 ;;;###autoload
820 (defun org-export (&optional arg)
821 "Export dispatcher for Org-mode.
822 When `org-export-run-in-background' is non-nil, try to run the command
823 in the background. This will be done only for commands that write
824 to a file. For details see the docstring of `org-export-run-in-background'.
826 The prefix argument ARG will be passed to the exporter. However, if
827 ARG is a double universal prefix `C-u C-u', that means to inverse the
828 value of `org-export-run-in-background'."
829 (interactive "P")
830 (let* ((bg (org-xor (equal arg '(16)) org-export-run-in-background))
831 (help "[t] insert the export option template
832 \[v] limit export to visible part of outline tree
834 \[a] export as ASCII [A] to temporary buffer
836 \[h] export as HTML [H] to temporary buffer [R] export region
837 \[b] export as HTML and open in browser
839 \[l] export as LaTeX [L] to temporary buffer
840 \[p] export as LaTeX and process to PDF
841 \[d] export as LaTeX, process to PDF, and open the resulting PDF document
843 \[D] export as DocBook
844 \[V] export as DocBook, process to PDF, and open the resulting PDF document
846 \[x] export as XOXO
847 \[g] export using Wes Hardaker's generic exporter
849 \[i] export current file as iCalendar file
850 \[I] export all agenda files as iCalendar files
851 \[c] export agenda files into combined iCalendar file
853 \[F] publish current file [P] publish current project
854 \[X] publish a project... [E] publish every projects")
855 (cmds
856 '((?t org-insert-export-options-template nil)
857 (?v org-export-visible nil)
858 (?a org-export-as-ascii t)
859 (?A org-export-as-ascii-to-buffer t)
860 (?h org-export-as-html t)
861 (?b org-export-as-html-and-open t)
862 (?H org-export-as-html-to-buffer nil)
863 (?R org-export-region-as-html nil)
864 (?x org-export-as-xoxo t)
865 (?g org-export-generic t)
866 (?D org-export-as-docbook t)
867 (?V org-export-as-docbook-pdf-and-open t)
868 (?l org-export-as-latex t)
869 (?p org-export-as-pdf t)
870 (?d org-export-as-pdf-and-open t)
871 (?L org-export-as-latex-to-buffer nil)
872 (?i org-export-icalendar-this-file t)
873 (?I org-export-icalendar-all-agenda-files t)
874 (?c org-export-icalendar-combine-agenda-files t)
875 (?F org-publish-current-file t)
876 (?P org-publish-current-project t)
877 (?X org-publish t)
878 (?E org-publish-all t)))
879 r1 r2 ass)
880 (save-excursion
881 (save-window-excursion
882 (delete-other-windows)
883 (with-output-to-temp-buffer "*Org Export/Publishing Help*"
884 (princ help))
885 (org-fit-window-to-buffer (get-buffer-window
886 "*Org Export/Publishing Help*"))
887 (message "Select command: ")
888 (setq r1 (read-char-exclusive))))
889 (setq r2 (if (< r1 27) (+ r1 96) r1))
890 (unless (setq ass (assq r2 cmds))
891 (error "No command associated with key %c" r1))
892 (if (and bg (nth 2 ass)
893 (not (buffer-base-buffer))
894 (not (org-region-active-p)))
895 ;; execute in background
896 (let ((p (start-process
897 (concat "Exporting " (file-name-nondirectory (buffer-file-name)))
898 "*Org Processes*"
899 (expand-file-name invocation-name invocation-directory)
900 "-batch"
901 "-l" user-init-file
902 "--eval" "(require 'org-exp)"
903 "--eval" "(setq org-wait .2)"
904 (buffer-file-name)
905 "-f" (symbol-name (nth 1 ass)))))
906 (set-process-sentinel p 'org-export-process-sentinel)
907 (message "Background process \"%s\": started" p))
908 ;; background processing not requested, or not possible
909 (call-interactively (nth 1 ass)))))
911 (defun org-export-process-sentinel (process status)
912 (if (string-match "\n+\\'" status)
913 (setq status (substring status 0 -1)))
914 (message "Background process \"%s\": %s" process status))
916 (defconst org-html-entities
917 '(("nbsp")
918 ("iexcl")
919 ("cent")
920 ("pound")
921 ("curren")
922 ("yen")
923 ("brvbar")
924 ("vert" . "&#124;")
925 ("sect")
926 ("uml")
927 ("copy")
928 ("ordf")
929 ("laquo")
930 ("not")
931 ("shy")
932 ("reg")
933 ("macr")
934 ("deg")
935 ("pm" . "&plusmn;")
936 ("plusmn")
937 ("sup2")
938 ("sup3")
939 ("acute")
940 ("micro")
941 ("para")
942 ("middot")
943 ("odot"."o")
944 ("star"."*")
945 ("cedil")
946 ("sup1")
947 ("ordm")
948 ("raquo")
949 ("frac14")
950 ("frac12")
951 ("frac34")
952 ("iquest")
953 ("Agrave")
954 ("Aacute")
955 ("Acirc")
956 ("Atilde")
957 ("Auml")
958 ("Aring") ("AA"."&Aring;")
959 ("AElig")
960 ("Ccedil")
961 ("Egrave")
962 ("Eacute")
963 ("Ecirc")
964 ("Euml")
965 ("Igrave")
966 ("Iacute")
967 ("Icirc")
968 ("Iuml")
969 ("ETH")
970 ("Ntilde")
971 ("Ograve")
972 ("Oacute")
973 ("Ocirc")
974 ("Otilde")
975 ("Ouml")
976 ("times")
977 ("Oslash")
978 ("Ugrave")
979 ("Uacute")
980 ("Ucirc")
981 ("Uuml")
982 ("Yacute")
983 ("THORN")
984 ("szlig")
985 ("agrave")
986 ("aacute")
987 ("acirc")
988 ("atilde")
989 ("auml")
990 ("aring")
991 ("aelig")
992 ("ccedil")
993 ("egrave")
994 ("eacute")
995 ("ecirc")
996 ("euml")
997 ("igrave")
998 ("iacute")
999 ("icirc")
1000 ("iuml")
1001 ("eth")
1002 ("ntilde")
1003 ("ograve")
1004 ("oacute")
1005 ("ocirc")
1006 ("otilde")
1007 ("ouml")
1008 ("divide")
1009 ("oslash")
1010 ("ugrave")
1011 ("uacute")
1012 ("ucirc")
1013 ("uuml")
1014 ("yacute")
1015 ("thorn")
1016 ("yuml")
1017 ("fnof")
1018 ("Alpha")
1019 ("Beta")
1020 ("Gamma")
1021 ("Delta")
1022 ("Epsilon")
1023 ("Zeta")
1024 ("Eta")
1025 ("Theta")
1026 ("Iota")
1027 ("Kappa")
1028 ("Lambda")
1029 ("Mu")
1030 ("Nu")
1031 ("Xi")
1032 ("Omicron")
1033 ("Pi")
1034 ("Rho")
1035 ("Sigma")
1036 ("Tau")
1037 ("Upsilon")
1038 ("Phi")
1039 ("Chi")
1040 ("Psi")
1041 ("Omega")
1042 ("alpha")
1043 ("beta")
1044 ("gamma")
1045 ("delta")
1046 ("epsilon")
1047 ("varepsilon"."&epsilon;")
1048 ("zeta")
1049 ("eta")
1050 ("theta")
1051 ("iota")
1052 ("kappa")
1053 ("lambda")
1054 ("mu")
1055 ("nu")
1056 ("xi")
1057 ("omicron")
1058 ("pi")
1059 ("rho")
1060 ("sigmaf") ("varsigma"."&sigmaf;")
1061 ("sigma")
1062 ("tau")
1063 ("upsilon")
1064 ("phi")
1065 ("chi")
1066 ("psi")
1067 ("omega")
1068 ("thetasym") ("vartheta"."&thetasym;")
1069 ("upsih")
1070 ("piv")
1071 ("bull") ("bullet"."&bull;")
1072 ("hellip") ("dots"."&hellip;")
1073 ("prime")
1074 ("Prime")
1075 ("oline")
1076 ("frasl")
1077 ("weierp")
1078 ("image")
1079 ("real")
1080 ("trade")
1081 ("alefsym")
1082 ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
1083 ("uarr") ("uparrow"."&uarr;")
1084 ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
1085 ("darr")("downarrow"."&darr;")
1086 ("harr") ("leftrightarrow"."&harr;")
1087 ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
1088 ("lArr") ("Leftarrow"."&lArr;")
1089 ("uArr") ("Uparrow"."&uArr;")
1090 ("rArr") ("Rightarrow"."&rArr;")
1091 ("dArr") ("Downarrow"."&dArr;")
1092 ("hArr") ("Leftrightarrow"."&hArr;")
1093 ("forall")
1094 ("part") ("partial"."&part;")
1095 ("exist") ("exists"."&exist;")
1096 ("empty") ("emptyset"."&empty;")
1097 ("nabla")
1098 ("isin") ("in"."&isin;")
1099 ("notin")
1100 ("ni")
1101 ("prod")
1102 ("sum")
1103 ("minus")
1104 ("lowast") ("ast"."&lowast;")
1105 ("radic")
1106 ("prop") ("proptp"."&prop;")
1107 ("infin") ("infty"."&infin;")
1108 ("ang") ("angle"."&ang;")
1109 ("and") ("wedge"."&and;")
1110 ("or") ("vee"."&or;")
1111 ("cap")
1112 ("cup")
1113 ("int")
1114 ("there4")
1115 ("sim")
1116 ("cong") ("simeq"."&cong;")
1117 ("asymp")("approx"."&asymp;")
1118 ("ne") ("neq"."&ne;")
1119 ("equiv")
1120 ("le")
1121 ("ge")
1122 ("sub") ("subset"."&sub;")
1123 ("sup") ("supset"."&sup;")
1124 ("nsub")
1125 ("sube")
1126 ("supe")
1127 ("oplus")
1128 ("otimes")
1129 ("perp")
1130 ("sdot") ("cdot"."&sdot;")
1131 ("lceil")
1132 ("rceil")
1133 ("lfloor")
1134 ("rfloor")
1135 ("lang")
1136 ("rang")
1137 ("loz") ("Diamond"."&loz;")
1138 ("spades") ("spadesuit"."&spades;")
1139 ("clubs") ("clubsuit"."&clubs;")
1140 ("hearts") ("diamondsuit"."&hearts;")
1141 ("diams") ("diamondsuit"."&diams;")
1142 ("smile"."&#9786;") ("blacksmile"."&#9787;") ("sad"."&#9785;")
1143 ("quot")
1144 ("amp")
1145 ("lt")
1146 ("gt")
1147 ("OElig")
1148 ("oelig")
1149 ("Scaron")
1150 ("scaron")
1151 ("Yuml")
1152 ("circ")
1153 ("tilde")
1154 ("ensp")
1155 ("emsp")
1156 ("thinsp")
1157 ("zwnj")
1158 ("zwj")
1159 ("lrm")
1160 ("rlm")
1161 ("ndash")
1162 ("mdash")
1163 ("lsquo")
1164 ("rsquo")
1165 ("sbquo")
1166 ("ldquo")
1167 ("rdquo")
1168 ("bdquo")
1169 ("dagger")
1170 ("Dagger")
1171 ("permil")
1172 ("lsaquo")
1173 ("rsaquo")
1174 ("euro")
1176 ("arccos"."arccos")
1177 ("arcsin"."arcsin")
1178 ("arctan"."arctan")
1179 ("arg"."arg")
1180 ("cos"."cos")
1181 ("cosh"."cosh")
1182 ("cot"."cot")
1183 ("coth"."coth")
1184 ("csc"."csc")
1185 ("deg"."deg")
1186 ("det"."det")
1187 ("dim"."dim")
1188 ("exp"."exp")
1189 ("gcd"."gcd")
1190 ("hom"."hom")
1191 ("inf"."inf")
1192 ("ker"."ker")
1193 ("lg"."lg")
1194 ("lim"."lim")
1195 ("liminf"."liminf")
1196 ("limsup"."limsup")
1197 ("ln"."ln")
1198 ("log"."log")
1199 ("max"."max")
1200 ("min"."min")
1201 ("Pr"."Pr")
1202 ("sec"."sec")
1203 ("sin"."sin")
1204 ("sinh"."sinh")
1205 ("sup"."sup")
1206 ("tan"."tan")
1207 ("tanh"."tanh")
1209 "Entities for TeX->HTML translation.
1210 Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
1211 \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
1212 In that case, \"\\ent\" will be translated to \"&other;\".
1213 The list contains HTML entities for Latin-1, Greek and other symbols.
1214 It is supplemented by a number of commonly used TeX macros with appropriate
1215 translations. There is currently no way for users to extend this.")
1217 ;;; General functions for all backends
1219 (defvar org-export-target-aliases nil
1220 "Alist of targets with invisible aliases.")
1221 (defvar org-export-preferred-target-alist nil
1222 "Alist of section id's with preferred aliases.")
1223 (defvar org-export-code-refs nil
1224 "Alist of code references and line numbers")
1226 (defun org-export-preprocess-string (string &rest parameters)
1227 "Cleanup STRING so that that the true exported has a more consistent source.
1228 This function takes STRING, which should be a buffer-string of an org-file
1229 to export. It then creates a temporary buffer where it does its job.
1230 The result is then again returned as a string, and the exporter works
1231 on this string to produce the exported version."
1232 (interactive)
1233 (let* ((htmlp (plist-get parameters :for-html))
1234 (asciip (plist-get parameters :for-ascii))
1235 (latexp (plist-get parameters :for-LaTeX))
1236 (docbookp (plist-get parameters :for-docbook))
1237 (backend (cond (htmlp 'html)
1238 (latexp 'latex)
1239 (asciip 'ascii)
1240 (docbookp 'docbook)))
1241 (archived-trees (plist-get parameters :archived-trees))
1242 (inhibit-read-only t)
1243 (drawers org-drawers)
1244 (outline-regexp "\\*+ ")
1245 target-alist rtn)
1247 (setq org-export-target-aliases nil)
1248 (setq org-export-preferred-target-alist nil)
1249 (setq org-export-code-refs nil)
1251 (with-current-buffer (get-buffer-create " org-mode-tmp")
1252 (erase-buffer)
1253 (insert string)
1254 (setq case-fold-search t)
1256 ;; Remove license-to-kill stuff
1257 ;; The caller marks some stuff for killing, stuff that has been
1258 ;; used to create the page title, for example.
1259 (org-export-kill-licensed-text)
1261 (let ((org-inhibit-startup t)) (org-mode))
1262 (setq case-fold-search t)
1263 (org-install-letbind)
1265 ;; Call the hook
1266 (run-hooks 'org-export-preprocess-hook)
1268 ;; Process the macros
1269 (org-export-preprocess-apply-macros)
1270 (run-hooks 'org-export-preprocess-after-macros-hook)
1272 (untabify (point-min) (point-max))
1274 ;; Handle include files, and call a hook
1275 (org-export-handle-include-files)
1276 (run-hooks 'org-export-preprocess-after-include-files-hook)
1278 ;; Get rid of archived trees
1279 (org-export-remove-archived-trees archived-trees)
1281 ;; Remove comment environment and comment subtrees
1282 (org-export-remove-comment-blocks-and-subtrees)
1284 ;; Get rid of excluded trees, and call a hook
1285 (org-export-handle-export-tags (plist-get parameters :select-tags)
1286 (plist-get parameters :exclude-tags))
1287 (run-hooks 'org-export-preprocess-after-tree-selection-hook)
1289 ;; Handle source code snippets
1290 (org-export-replace-src-segments-and-examples backend)
1292 ;; Protect short examples marked by a leading colon
1293 (org-export-protect-colon-examples)
1295 ;; Normalize footnotes
1296 (when (plist-get parameters :footnotes)
1297 (org-footnote-normalize nil t))
1299 ;; Find all headings and compute the targets for them
1300 (setq target-alist (org-export-define-heading-targets target-alist))
1302 ;; Get rid of drawers
1303 (org-export-remove-or-extract-drawers drawers
1304 (plist-get parameters :drawers))
1306 ;; Get the correct stuff before the first headline
1307 (when (plist-get parameters :skip-before-1st-heading)
1308 (goto-char (point-min))
1309 (when (re-search-forward "^\\(#.*\n\\)?\\*+[ \t]" nil t)
1310 (delete-region (point-min) (match-beginning 0))
1311 (goto-char (point-min))
1312 (insert "\n")))
1313 (when (plist-get parameters :add-text)
1314 (goto-char (point-min))
1315 (insert (plist-get parameters :add-text) "\n"))
1317 ;; Remove todo-keywords before exporting, if the user has requested so
1318 (org-export-remove-headline-metadata parameters)
1320 ;; Find targets in comments and move them out of comments,
1321 ;; but mark them as targets that should be invisible
1322 (setq target-alist (org-export-handle-invisible-targets target-alist))
1324 ;; Select and protect backend specific stuff, throw away stuff
1325 ;; that is specific for other backends
1326 (org-export-select-backend-specific-text backend)
1328 ;; Protect quoted subtrees
1329 (org-export-protect-quoted-subtrees)
1331 ;; Remove clock lines
1332 (org-export-remove-clock-lines)
1334 ;; Protect verbatim elements
1335 (org-export-protect-verbatim)
1337 ;; Blockquotes, verse, and center
1338 (org-export-mark-blockquote-verse-center)
1339 (run-hooks 'org-export-preprocess-after-blockquote-hook)
1341 ;; Remove timestamps, if the user has requested so
1342 (unless (plist-get parameters :timestamps)
1343 (org-export-remove-timestamps))
1345 ;; Attach captions to the correct object
1346 (setq target-alist (org-export-attach-captions-and-attributes
1347 backend target-alist))
1349 ;; Find matches for radio targets and turn them into internal links
1350 (org-export-mark-radio-links)
1352 ;; Find all links that contain a newline and put them into a single line
1353 (org-export-concatenate-multiline-links)
1355 ;; Normalize links: Convert angle and plain links into bracket links
1356 ;; and expand link abbreviations
1357 (org-export-normalize-links)
1359 ;; Find all internal links. If they have a fuzzy match (i.e. not
1360 ;; a *dedicated* target match, let the link point to the
1361 ;; corresponding section.
1362 (org-export-target-internal-links target-alist)
1364 ;; Find multiline emphasis and put them into single line
1365 (when (plist-get parameters :emph-multiline)
1366 (org-export-concatenate-multiline-emphasis))
1368 ;; Remove special table lines
1369 (when org-export-table-remove-special-lines
1370 (org-export-remove-special-table-lines))
1372 ;; Another hook
1373 (run-hooks 'org-export-preprocess-before-backend-specifics-hook)
1375 ;; LaTeX-specific preprocessing
1376 (when latexp
1377 (require 'org-latex nil)
1378 (org-export-latex-preprocess parameters))
1380 ;; ASCII-specific preprocessing
1381 (when asciip
1382 (org-export-ascii-preprocess parameters))
1384 ;; HTML-specific preprocessing
1385 (when htmlp
1386 (org-export-html-preprocess parameters))
1388 ;; DocBook-specific preprocessing
1389 (when docbookp
1390 (require 'org-docbook nil)
1391 (org-export-docbook-preprocess parameters))
1393 ;; Remove or replace comments
1394 (org-export-handle-comments (plist-get parameters :comments))
1396 ;; Run the final hook
1397 (run-hooks 'org-export-preprocess-final-hook)
1399 (setq rtn (buffer-string)))
1400 (kill-buffer " org-mode-tmp")
1401 rtn))
1403 (defun org-export-kill-licensed-text ()
1404 "Remove all text that is marked with a :org-license-to-kill property."
1405 (let (p)
1406 (while (setq p (text-property-any (point-min) (point-max)
1407 :org-license-to-kill t))
1408 (delete-region
1409 p (or (next-single-property-change p :org-license-to-kill)
1410 (point-max))))))
1412 (defun org-export-define-heading-targets (target-alist)
1413 "Find all headings and define the targets for them.
1414 The new targets are added to TARGET-ALIST, which is also returned."
1415 (goto-char (point-min))
1416 (org-init-section-numbers)
1417 (let ((re (concat "^" org-outline-regexp
1418 "\\| [ \t]*:\\(ID\\|CUSTOM_ID\\):[ \t]*\\([^ \t\r\n]+\\)"))
1419 level target last-section-target a id)
1420 (while (re-search-forward re nil t)
1421 (if (match-end 2)
1422 (progn
1423 (setq id (org-match-string-no-properties 2))
1424 (push (cons id target) target-alist)
1425 (setq a (or (assoc last-section-target org-export-target-aliases)
1426 (progn
1427 (push (list last-section-target)
1428 org-export-target-aliases)
1429 (car org-export-target-aliases))))
1430 (push (caar target-alist) (cdr a))
1431 (when (equal (match-string 1) "CUSTOM_ID")
1432 (if (not (assoc last-section-target
1433 org-export-preferred-target-alist))
1434 (push (cons last-section-target id)
1435 org-export-preferred-target-alist))))
1436 (setq level (org-reduced-level
1437 (save-excursion (goto-char (point-at-bol))
1438 (org-outline-level))))
1439 (setq target (org-solidify-link-text
1440 (format "sec-%s" (org-section-number level))))
1441 (setq last-section-target target)
1442 (push (cons target target) target-alist)
1443 (add-text-properties
1444 (point-at-bol) (point-at-eol)
1445 (list 'target target)))))
1446 target-alist)
1448 (defun org-export-handle-invisible-targets (target-alist)
1449 "Find targets in comments and move them out of comments.
1450 Mark them as invisible targets."
1451 (let (target tmp a)
1452 (goto-char (point-min))
1453 (while (re-search-forward "^#.*?\\(<<<?\\([^>\r\n]+\\)>>>?\\).*" nil t)
1454 ;; Check if the line before or after is a headline with a target
1455 (if (setq target (or (get-text-property (point-at-bol 0) 'target)
1456 (get-text-property (point-at-bol 2) 'target)))
1457 (progn
1458 ;; use the existing target in a neighboring line
1459 (setq tmp (match-string 2))
1460 (replace-match "")
1461 (and (looking-at "\n") (delete-char 1))
1462 (push (cons (setq tmp (org-solidify-link-text tmp)) target)
1463 target-alist)
1464 (setq a (or (assoc target org-export-target-aliases)
1465 (progn
1466 (push (list target) org-export-target-aliases)
1467 (car org-export-target-aliases))))
1468 (push tmp (cdr a)))
1469 ;; Make an invisible target
1470 (replace-match "\\1(INVISIBLE)"))))
1471 target-alist)
1473 (defun org-export-target-internal-links (target-alist)
1474 "Find all internal links and assign targets to them.
1475 If a link has a fuzzy match (i.e. not a *dedicated* target match),
1476 let the link point to the corresponding section.
1477 This function also handles the id links, if they have a match in
1478 the current file."
1479 (goto-char (point-min))
1480 (while (re-search-forward org-bracket-link-regexp nil t)
1481 (org-if-unprotected
1482 (let* ((md (match-data))
1483 (desc (match-end 2))
1484 (link (org-link-unescape (match-string 1)))
1485 (slink (org-solidify-link-text link))
1486 found props pos cref
1487 (target
1488 (cond
1489 ((= (string-to-char link) ?#)
1490 ;; user wants exactly this link
1491 link)
1492 ((cdr (assoc slink target-alist))
1493 (or (cdr (assoc (assoc slink target-alist)
1494 org-export-preferred-target-alist))
1495 (cdr (assoc slink target-alist))))
1496 ((and (string-match "^id:" link)
1497 (cdr (assoc (substring link 3) target-alist))))
1498 ((string-match "^(\\(.*\\))$" link)
1499 (setq cref (match-string 1 link))
1500 (concat "coderef:" cref))
1501 ((string-match org-link-types-re link) nil)
1502 ((or (file-name-absolute-p link)
1503 (string-match "^\\." link))
1504 nil)
1506 (save-excursion
1507 (setq found (condition-case nil (org-link-search link)
1508 (error nil)))
1509 (when (and found
1510 (or (org-on-heading-p)
1511 (not (eq found 'dedicated))))
1512 (or (get-text-property (point) 'target)
1513 (get-text-property
1514 (max (point-min)
1515 (1- (or (previous-single-property-change
1516 (point) 'target) 0)))
1517 'target))))))))
1518 (when target
1519 (set-match-data md)
1520 (goto-char (match-beginning 1))
1521 (setq props (text-properties-at (point)))
1522 (delete-region (match-beginning 1) (match-end 1))
1523 (setq pos (point))
1524 (insert target)
1525 (unless desc (insert "][" link))
1526 (add-text-properties pos (point) props))))))
1528 (defun org-export-remove-or-extract-drawers (all-drawers exp-drawers)
1529 "Remove drawers, or extract the content.
1530 ALL-DRAWERS is a list of all drawer names valid in the current buffer.
1531 EXP-DRAWERS can be t to keep all drawer contents, or a list of drawers
1532 whose content to keep."
1533 (unless (eq t exp-drawers)
1534 (goto-char (point-min))
1535 (let ((re (concat "^[ \t]*:\\("
1536 (mapconcat
1537 'identity
1538 (org-delete-all exp-drawers
1539 (copy-sequence all-drawers))
1540 "\\|")
1541 "\\):[ \t]*$"))
1542 beg eol)
1543 (while (re-search-forward re nil t)
1544 (org-if-unprotected
1545 (setq beg (match-beginning 0)
1546 eol (match-end 0))
1547 (if (re-search-forward "^\\([ \t]*:END:[ \t]*\n?\\)\\|^\\*+[ \t]"
1548 nil t)
1549 (if (match-end 1)
1550 ;; terminated in this entry
1551 (progn
1552 (delete-region beg (match-end 1))
1553 (goto-char beg))
1554 (goto-char eol))))))))
1556 (defun org-export-handle-export-tags (select-tags exclude-tags)
1557 "Modify the buffer, honoring SELECT-TAGS and EXCLUDE-TAGS.
1558 Both arguments are lists of tags.
1559 If any of SELECT-TAGS is found, all trees not marked by a SELECT-TAG
1560 will be removed.
1561 After that, all subtrees that are marked by EXCLUDE-TAGS will be
1562 removed as well."
1563 (remove-text-properties (point-min) (point-max) '(:org-delete t))
1564 (let* ((re-sel (concat ":\\(" (mapconcat 'regexp-quote
1565 select-tags "\\|")
1566 "\\):"))
1567 (re-excl (concat ":\\(" (mapconcat 'regexp-quote
1568 exclude-tags "\\|")
1569 "\\):"))
1570 beg end cont)
1571 (goto-char (point-min))
1572 (when (and select-tags
1573 (re-search-forward
1574 (concat "^\\*+[ \t].*" re-sel "[^ \t\n]*[ \t]*$") nil t))
1575 ;; At least one tree is marked for export, this means
1576 ;; all the unmarked stuff needs to go.
1577 ;; Dig out the trees that should be exported
1578 (goto-char (point-min))
1579 (outline-next-heading)
1580 (setq beg (point))
1581 (put-text-property beg (point-max) :org-delete t)
1582 (while (re-search-forward re-sel nil t)
1583 (when (org-on-heading-p)
1584 (org-back-to-heading)
1585 (remove-text-properties
1586 (max (1- (point)) (point-min))
1587 (setq cont (save-excursion (org-end-of-subtree t t)))
1588 '(:org-delete t))
1589 (while (and (org-up-heading-safe)
1590 (get-text-property (point) :org-delete))
1591 (remove-text-properties (max (1- (point)) (point-min))
1592 (point-at-eol) '(:org-delete t)))
1593 (goto-char cont))))
1594 ;; Remove the trees explicitly marked for noexport
1595 (when exclude-tags
1596 (goto-char (point-min))
1597 (while (re-search-forward re-excl nil t)
1598 (when (org-at-heading-p)
1599 (org-back-to-heading t)
1600 (setq beg (point))
1601 (org-end-of-subtree t)
1602 (delete-region beg (point)))))
1603 ;; Remove everything that is now still marked for deletion
1604 (goto-char (point-min))
1605 (while (setq beg (text-property-any (point-min) (point-max) :org-delete t))
1606 (setq end (or (next-single-property-change beg :org-delete)
1607 (point-max)))
1608 (delete-region beg end))))
1610 (defun org-export-remove-archived-trees (export-archived-trees)
1611 "Remove archived trees.
1612 When EXPORT-ARCHIVED-TREES is `headline;, only the headline will be exported.
1613 When it is t, the entire archived tree will be exported.
1614 When it is nil the entire tree including the headline will be removed
1615 from the buffer."
1616 (let ((re-archive (concat ":" org-archive-tag ":"))
1617 a b)
1618 (when (not (eq export-archived-trees t))
1619 (goto-char (point-min))
1620 (while (re-search-forward re-archive nil t)
1621 (if (not (org-on-heading-p t))
1622 (org-end-of-subtree t)
1623 (beginning-of-line 1)
1624 (setq a (if export-archived-trees
1625 (1+ (point-at-eol)) (point))
1626 b (org-end-of-subtree t))
1627 (if (> b a) (delete-region a b)))))))
1629 (defun org-export-remove-headline-metadata (opts)
1630 "Remove meta data from the headline, according to user options."
1631 (let ((re org-complex-heading-regexp)
1632 (todo (plist-get opts :todo-keywords))
1633 (tags (plist-get opts :tags))
1634 (pri (plist-get opts :priority))
1635 (elts '(1 2 3 4 5))
1636 rpl)
1637 (setq elts (delq nil (list 1 (if todo 2) (if pri 3) 4 (if tags 5))))
1638 (when (or (not todo) (not tags) (not pri))
1639 (goto-char (point-min))
1640 (while (re-search-forward re nil t)
1641 (org-if-unprotected
1642 (setq rpl (mapconcat (lambda (i) (if (match-end i) (match-string i) ""))
1643 elts " "))
1644 (replace-match rpl t t))))))
1646 (defun org-export-remove-timestamps ()
1647 "Remove timestamps and keywords for export."
1648 (goto-char (point-min))
1649 (while (re-search-forward org-maybe-keyword-time-regexp nil t)
1650 (backward-char 1)
1651 (org-if-unprotected
1652 (unless (save-match-data (org-at-table-p))
1653 (replace-match "")
1654 (beginning-of-line 1)
1655 (if (looking-at "[- \t]*\\(=>[- \t0-9:]*\\)?[ \t]*\n")
1656 (replace-match ""))))))
1658 (defun org-export-remove-clock-lines ()
1659 "Remove clock lines for export."
1660 (goto-char (point-min))
1661 (let ((re (concat "^[ \t]*" org-clock-string ".*\n?")))
1662 (while (re-search-forward re nil t)
1663 (org-if-unprotected
1664 (replace-match "")))))
1666 (defun org-export-protect-quoted-subtrees ()
1667 "Mark quoted subtrees with the protection property."
1668 (let ((re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>")))
1669 (goto-char (point-min))
1670 (while (re-search-forward re-quote nil t)
1671 (goto-char (match-beginning 0))
1672 (end-of-line 1)
1673 (add-text-properties (point) (org-end-of-subtree t)
1674 '(org-protected t)))))
1676 (defun org-export-protect-verbatim ()
1677 "Mark verbatim snippets with the protection property."
1678 (goto-char (point-min))
1679 (while (re-search-forward org-verbatim-re nil t)
1680 (add-text-properties (match-beginning 4) (match-end 4)
1681 '(org-protected t))
1682 (goto-char (1+ (match-end 4)))))
1684 (defun org-export-protect-colon-examples ()
1685 "Protect lines starting with a colon."
1686 (goto-char (point-min))
1687 (let ((re "^[ \t]*:\\([ \t]\\|$\\)") beg)
1688 (while (re-search-forward re nil t)
1689 (beginning-of-line 1)
1690 (setq beg (point))
1691 (while (looking-at re)
1692 (end-of-line 1)
1693 (or (eobp) (forward-char 1)))
1694 (add-text-properties beg (if (bolp) (1- (point)) (point))
1695 '(org-protected t)))))
1697 (defun org-export-select-backend-specific-text (backend)
1698 (let ((formatters
1699 '((docbook "DOCBOOK" "BEGIN_DOCBOOK" "END_DOCBOOK")
1700 (html "HTML" "BEGIN_HTML" "END_HTML")
1701 (ascii "ASCII" "BEGIN_ASCII" "END_ASCII")
1702 (latex "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
1703 (case-fold-search t)
1704 fmt beg beg-content end end-content)
1706 (while formatters
1707 (setq fmt (pop formatters))
1708 (when (eq (car fmt) backend)
1709 ;; This is selected code, put it into the file for real
1710 (goto-char (point-min))
1711 (while (re-search-forward (concat "^\\([ \t]*\\)#\\+" (cadr fmt)
1712 ":[ \t]*\\(.*\\)") nil t)
1713 (replace-match "\\1\\2" t)
1714 (add-text-properties
1715 (point-at-bol) (min (1+ (point-at-eol)) (point-max))
1716 '(org-protected t))))
1717 (goto-char (point-min))
1718 (while (re-search-forward (concat "^[ \t]*#\\+" (caddr fmt) "\\>.*\n?")
1719 nil t)
1720 (setq beg (match-beginning 0) beg-content (match-end 0))
1721 (when (re-search-forward (concat "^[ \t]*#\\+" (cadddr fmt) "\\>.*\n?")
1722 nil t)
1723 (setq end (match-end 0) end-content (match-beginning 0))
1724 (if (eq (car fmt) backend)
1725 ;; yes, keep this
1726 (add-text-properties beg-content end-content '(org-protected t))
1727 ;; No, this is for a different backend, kill it
1728 (delete-region beg end)))))))
1730 (defun org-export-mark-blockquote-verse-center ()
1731 "Mark block quote and verse environments with special cookies.
1732 These special cookies will later be interpreted by the backend."
1733 ;; Blockquotes
1734 (let (type t1 ind beg end beg1 end1 content)
1735 (goto-char (point-min))
1736 (while (re-search-forward
1737 "^\\([ \t]*\\)#\\+\\(begin_\\(\\(block\\)?quote\\|verse\\|center\\)\\>.*\\)"
1738 nil t)
1739 (setq ind (length (match-string 1))
1740 type (downcase (match-string 3))
1741 t1 (if (equal type "quote") "blockquote" type))
1742 (setq beg (match-beginning 0)
1743 beg1 (1+ (match-end 0)))
1744 (when (re-search-forward (concat "^[ \t]*#\\+end_" type "\\>.*") nil t)
1745 (setq end (1+ (point-at-eol))
1746 end1 (1- (match-beginning 0)))
1747 (setq content (org-remove-indentation (buffer-substring beg1 end1)))
1748 (setq content (concat "ORG-" (upcase t1) "-START\n"
1749 content "\n"
1750 "ORG-" (upcase t1) "-END\n"))
1751 (delete-region beg end)
1752 (insert (org-add-props content nil 'original-indentation ind))))))
1754 (defun org-export-attach-captions-and-attributes (backend target-alist)
1755 "Move #+CAPTION, #+ATTR_BACKEND, and #+LABEL text into text properties.
1756 If the next thing following is a table, add the text properties to the first
1757 table line. If it is a link, add it to the line containing the link."
1758 (goto-char (point-min))
1759 (remove-text-properties (point-min) (point-max)
1760 '(org-caption nil org-attributes nil))
1761 (let ((case-fold-search t)
1762 (re (concat "^[ \t]*#\\+caption:[ \t]+\\(.*\\)"
1763 "\\|"
1764 "^[ \t]*#\\+attr_" (symbol-name backend) ":[ \t]+\\(.*\\)"
1765 "\\|"
1766 "^[ \t]*#\\+label:[ \t]+\\(.*\\)"
1767 "\\|"
1768 "^[ \t]*|[^-]"
1769 "\\|"
1770 "^[ \t]*\\[\\[.*\\]\\][ \t]*$"))
1771 cap attr label)
1772 (while (re-search-forward re nil t)
1773 (cond
1774 ((match-end 1)
1775 (setq cap (concat cap (if cap " " "") (org-trim (match-string 1)))))
1776 ((match-end 2)
1777 (setq attr (concat attr (if attr " " "") (org-trim (match-string 2)))))
1778 ((match-end 3)
1779 (setq label (org-trim (match-string 3))))
1781 (add-text-properties (point-at-bol) (point-at-eol)
1782 (list 'org-caption cap
1783 'org-attributes attr
1784 'org-label label))
1785 (if label (push (cons label label) target-alist))
1786 (setq cap nil attr nil label nil)))))
1787 target-alist)
1789 (defun org-export-remove-comment-blocks-and-subtrees ()
1790 "Remove the comment environment, and also commented subtrees."
1791 (let ((re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
1792 (case-fold-search nil))
1793 ;; Remove comment environment
1794 (goto-char (point-min))
1795 (while (re-search-forward
1796 "^#\\+BEGIN_COMMENT[ \t]*\n[^\000]*?^#\\+END_COMMENT\\>.*" nil t)
1797 (replace-match "" t t))
1798 ;; Remove subtrees that are commented
1799 (goto-char (point-min))
1800 (while (re-search-forward re-commented nil t)
1801 (goto-char (match-beginning 0))
1802 (delete-region (point) (org-end-of-subtree t)))))
1804 (defun org-export-handle-comments (commentsp)
1805 "Remove comments, or convert to backend-specific format.
1806 COMMENTSP can be a format string for publishing comments.
1807 When it is nil, all comments will be removed."
1808 (let ((re "^\\(#\\|[ \t]*#\\+\\)\\(.*\n?\\)")
1809 pos)
1810 (goto-char (point-min))
1811 (while (or (looking-at re)
1812 (re-search-forward re nil t))
1813 (setq pos (match-beginning 0))
1814 (if (and commentsp
1815 (not (equal (char-before (match-end 1)) ?+)))
1816 (progn (add-text-properties
1817 (match-beginning 0) (match-end 0) '(org-protected t))
1818 (replace-match (format commentsp (match-string 2)) t t))
1819 (goto-char (1+ pos))
1820 (org-if-unprotected
1821 (replace-match "")
1822 (goto-char (max (point-min) (1- pos))))))))
1824 (defun org-export-mark-radio-links ()
1825 "Find all matches for radio targets and turn them into internal links."
1826 (let ((re-radio (and org-target-link-regexp
1827 (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)"))))
1828 (goto-char (point-min))
1829 (when re-radio
1830 (while (re-search-forward re-radio nil t)
1831 (unless
1832 (save-match-data
1833 (or (org-in-regexp org-bracket-link-regexp)
1834 (org-in-regexp org-plain-link-re)))
1835 (org-if-unprotected
1836 (replace-match "\\1[[\\2]]")))))))
1838 (defun org-export-remove-special-table-lines ()
1839 "Remove tables lines that are used for internal purposes."
1840 (goto-char (point-min))
1841 (while (re-search-forward "^[ \t]*|" nil t)
1842 (beginning-of-line 1)
1843 (if (or (looking-at "[ \t]*| *[!_^] *|")
1844 (and (looking-at "[ \t]*|\\( *\\(<[0-9]+>\\|<[rl]>\\|<[rl][0-9]+>\\)? *|\\)+[ \t]*$")
1845 (not (looking-at ".*?| *[^ <|\n]"))))
1846 (delete-region (max (point-min) (1- (point-at-bol)))
1847 (point-at-eol))
1848 (end-of-line 1))))
1850 (defun org-export-protect-sub-super (s)
1851 (save-match-data
1852 (while (string-match "\\([^\\\\]\\)\\([_^]\\)" s)
1853 (setq s (replace-match "\\1\\\\\\2" nil nil s)))
1856 (defun org-export-normalize-links ()
1857 "Convert all links to bracket links, and expand link abbreviations."
1858 (let ((re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
1859 (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
1860 nodesc)
1861 (goto-char (point-min))
1862 (while (re-search-forward re-plain-link nil t)
1863 (goto-char (1- (match-end 0)))
1864 (org-if-unprotected-at (1+ (match-beginning 0))
1865 (let* ((s (concat (match-string 1)
1866 "[[" (match-string 2) ":" (match-string 3)
1867 "][" (match-string 2) ":" (org-export-protect-sub-super
1868 (match-string 3))
1869 "]]")))
1870 ;; added 'org-link face to links
1871 (put-text-property 0 (length s) 'face 'org-link s)
1872 (replace-match s t t))))
1873 (goto-char (point-min))
1874 (while (re-search-forward re-angle-link nil t)
1875 (goto-char (1- (match-end 0)))
1876 (org-if-unprotected
1877 (let* ((s (concat (match-string 1)
1878 "[[" (match-string 2) ":" (match-string 3)
1879 "][" (match-string 2) ":" (org-export-protect-sub-super
1880 (match-string 3))
1881 "]]")))
1882 (put-text-property 0 (length s) 'face 'org-link s)
1883 (replace-match s t t))))
1884 (goto-char (point-min))
1885 (while (re-search-forward org-bracket-link-regexp nil t)
1886 (goto-char (1- (match-end 0)))
1887 (setq nodesc (not (match-end 3)))
1888 (org-if-unprotected
1889 (let* ((xx (save-match-data
1890 (org-translate-link
1891 (org-link-expand-abbrev (match-string 1)))))
1892 (s (concat
1893 "[[" (org-add-props (copy-sequence xx)
1894 nil 'org-protected t 'org-no-description nodesc)
1896 (if (match-end 3)
1897 (match-string 2)
1898 (concat "[" (copy-sequence xx)
1899 "]"))
1900 "]")))
1901 (put-text-property 0 (length s) 'face 'org-link s)
1902 (replace-match s t t))))))
1904 (defun org-export-concatenate-multiline-links ()
1905 "Find multi-line links and put it all into a single line.
1906 This is to make sure that the line-processing export backends
1907 can work correctly."
1908 (goto-char (point-min))
1909 (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
1910 (org-if-unprotected
1911 (replace-match "\\1 \\3")
1912 (goto-char (match-beginning 0)))))
1914 (defun org-export-concatenate-multiline-emphasis ()
1915 "Find multi-line emphasis and put it all into a single line.
1916 This is to make sure that the line-processing export backends
1917 can work correctly."
1918 (goto-char (point-min))
1919 (while (re-search-forward org-emph-re nil t)
1920 (if (and (not (= (char-after (match-beginning 3))
1921 (char-after (match-beginning 4))))
1922 (save-excursion (goto-char (match-beginning 0))
1923 (save-match-data (not (org-at-table-p)))))
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))))))
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))
1935 (let ((end (if (looking-at org-outline-regexp)
1936 (point)
1937 (save-excursion (outline-next-heading) (point)))))
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)))))))
1946 (defun org-export-get-title-from-subtree ()
1947 "Return subtree title and exclude it from export."
1948 (let (title (rbeg (region-beginning)) (rend (region-end)))
1949 (save-excursion
1950 (goto-char rbeg)
1951 (when (and (org-at-heading-p)
1952 (>= (org-end-of-subtree t t) rend))
1953 ;; This is a subtree, we take the title from the first heading
1954 (goto-char rbeg)
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))
1959 (list :org-license-to-kill t)))
1960 (setq title (or (org-entry-get nil "EXPORT_TITLE") title))))
1961 title))
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))))
1973 (defun org-get-min-level (lines &optional offset)
1974 "Get the minimum level in LINES."
1975 (let ((re "^\\(\\*+\\) ") l)
1976 (catch 'exit
1977 (while (setq l (pop lines))
1978 (if (string-match re l)
1979 (throw 'exit (org-tr-level (- (length (match-string 1 l))
1980 (or offset 0))))))
1981 1)))
1983 ;; Variable holding the vector with section numbers
1984 (defvar org-section-numbers (make-vector org-level-max 0))
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))
1999 (aset org-section-numbers i (string-to-number number-string)))
2000 (pop numbers))
2001 (setq i (1- i)))))
2003 (defun org-section-number (&optional level)
2004 "Return a string with the current section number.
2005 When LEVEL is non-nil, increase section numbers on that level."
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)
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)
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)))
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))))
2037 (concat string term)))
2039 (defun org-number-to-counter (n type)
2040 "Concert number N to a string counter, according to TYPE.
2041 TYPE must be a string, any of:
2042 1 number
2043 A A,B,....
2044 a a,b,....
2045 I upper case roman numeral
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))))
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)))
2071 ;;; Macros
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
2078 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
2079 nil t)
2080 (setq key (downcase (match-string 1))
2081 args (match-string 3))
2082 (when (setq val (or (plist-get org-export-opt-plist
2083 (intern (concat ":macro-" key)))
2084 (plist-get org-export-opt-plist
2085 (intern (concat ":" key)))))
2086 (save-match-data
2087 (when args
2088 (setq args (org-split-string args ",[ \t\n]*") args2 nil)
2089 (setq args (mapcar 'org-trim args))
2090 (while args
2091 (while (string-match "\\\\\\'" (car args))
2092 ;; repair bad splits
2093 (setcar (cdr args) (concat (substring (car args) 0 -1)
2094 ";" (nth 1 args)))
2095 (pop args))
2096 (push (pop args) args2))
2097 (setq args (nreverse args2))
2098 (setq s 0)
2099 (while (string-match "\\$\\([0-9]+\\)" val s)
2100 (setq s (1+ (match-beginning 0))
2101 n (string-to-number (match-string 1 val)))
2102 (and (>= (length args) n)
2103 (setq val (replace-match (nth (1- n) args) t t val)))))
2104 (when (string-match "\\`(eval\\>" val)
2105 (setq val (eval (read val))))
2106 (if (and val (not (stringp val)))
2107 (setq val (format "%s" val))))
2108 (and (stringp val)
2109 (replace-match val t t))))))
2111 (defun org-export-apply-macros-in-string (s)
2112 "Apply the macros in string S."
2113 (when s
2114 (with-temp-buffer
2115 (insert s)
2116 (org-export-preprocess-apply-macros)
2117 (buffer-string))))
2119 ;;; Include files
2121 (defun org-export-handle-include-files ()
2122 "Include the contents of include files, with proper formatting."
2123 (let ((case-fold-search t)
2124 params file markup lang start end prefix prefix1 switches)
2125 (goto-char (point-min))
2126 (while (re-search-forward "^#\\+INCLUDE:?[ \t]+\\(.*\\)" nil t)
2127 (setq params (read (concat "(" (match-string 1) ")"))
2128 prefix (org-get-and-remove-property 'params :prefix)
2129 prefix1 (org-get-and-remove-property 'params :prefix1)
2130 file (org-symname-or-string (pop params))
2131 markup (org-symname-or-string (pop params))
2132 lang (and (member markup '("src" "SRC"))
2133 (org-symname-or-string (pop params)))
2134 switches (mapconcat '(lambda (x) (format "%s" x)) params " ")
2135 start nil end nil)
2136 (delete-region (match-beginning 0) (match-end 0))
2137 (if (or (not file)
2138 (not (file-exists-p file))
2139 (not (file-readable-p file)))
2140 (insert (format "CANNOT INCLUDE FILE %s" file))
2141 (when markup
2142 (if (equal (downcase markup) "src")
2143 (setq start (format "#+begin_src %s %s\n"
2144 (or lang "fundamental")
2145 (or switches ""))
2146 end "#+end_src")
2147 (setq start (format "#+begin_%s %s\n" markup switches)
2148 end (format "#+end_%s" markup))))
2149 (insert (or start ""))
2150 (insert (org-get-file-contents (expand-file-name file)
2151 prefix prefix1 markup))
2152 (or (bolp) (newline))
2153 (insert (or end ""))))))
2155 (defun org-get-file-contents (file &optional prefix prefix1 markup)
2156 "Get the contents of FILE and return them as a string.
2157 If PREFIX is a string, prepend it to each line. If PREFIX1
2158 is a string, prepend it to the first line instead of PREFIX.
2159 If MARKUP, don't protect org-like lines, the exporter will
2160 take care of the block they are in."
2161 (if (stringp markup) (setq markup (downcase markup)))
2162 (with-temp-buffer
2163 (insert-file-contents file)
2164 (when (or prefix prefix1)
2165 (goto-char (point-min))
2166 (while (not (eobp))
2167 (insert (or prefix1 prefix))
2168 (setq prefix1 nil)
2169 (beginning-of-line 2)))
2170 (buffer-string)
2171 (when (member markup '("src" "example"))
2172 (goto-char (point-min))
2173 (while (re-search-forward "^\\([*#]\\|[ \t]*#\\+\\)" nil t)
2174 (goto-char (match-beginning 0))
2175 (insert ",")
2176 (end-of-line 1)))
2177 (buffer-string)))
2179 (defun org-get-and-remove-property (listvar prop)
2180 "Check if the value of LISTVAR contains PROP as a property.
2181 If yes, return the value of that property (i.e. the element following
2182 in the list) and remove property and value from the list in LISTVAR."
2183 (let ((list (symbol-value listvar)) m v)
2184 (when (setq m (member prop list))
2185 (setq v (nth 1 m))
2186 (if (equal (car list) prop)
2187 (set listvar (cddr list))
2188 (setcdr (nthcdr (- (length list) (length m) 1) list)
2189 (cddr m))
2190 (set listvar list)))
2193 (defun org-symname-or-string (s)
2194 (if (symbolp s)
2195 (if s (symbol-name s) s)
2198 ;;; Fontification and line numbers for code examples
2200 (defvar org-export-last-code-line-counter-value 0)
2202 (defun org-export-replace-src-segments-and-examples (backend)
2203 "Replace source code segments with special code for export."
2204 (setq org-export-last-code-line-counter-value 0)
2205 (let ((case-fold-search t)
2206 lang code trans opts indent)
2207 (goto-char (point-min))
2208 (while (re-search-forward
2209 "\\(^\\([ \t]*\\)#\\+BEGIN_SRC:?[ \t]+\\([^ \t\n]+\\)\\(.*\\)\n\\([^\000]+?\n\\)[ \t]*#\\+END_SRC.*\\)\\|\\(^\\([ \t]*\\)#\\+BEGIN_EXAMPLE:?\\(?:[ \t]+\\(.*\\)\\)?\n\\([^\000]+?\n\\)[ \t]*#\\+END_EXAMPLE.*\\)"
2210 nil t)
2211 (if (match-end 1)
2212 ;; src segments
2213 (setq lang (match-string 3)
2214 opts (match-string 4)
2215 code (match-string 5)
2216 indent (length (match-string 2)))
2217 (setq lang nil
2218 opts (match-string 8)
2219 code (match-string 9)
2220 indent (length (match-string 7))))
2222 (setq trans (org-export-format-source-code-or-example
2223 backend lang code opts indent))
2224 (replace-match trans t t))))
2226 (defvar htmlp) ;; dynamically scoped
2227 (defvar latexp) ;; dynamically scoped
2228 (defvar org-export-latex-verbatim-wrap) ;; defined in org-latex.el
2229 (defvar org-export-latex-listings) ;; defined in org-latex.el
2230 (defvar org-export-latex-listings-langs) ;; defined in org-latex.el
2232 (defun org-export-format-source-code-or-example
2233 (backend lang code &optional opts indent)
2234 "Format CODE from language LANG and return it formatted for export.
2235 If LANG is nil, do not add any fontification.
2236 OPTS contains formatting optons, like `-n' for triggering numbering lines,
2237 and `+n' for continuing previous numering.
2238 Code formatting according to language currently only works for HTML.
2239 Numbering lines works for all three major backends (html, latex, and ascii).
2240 INDENT was the original indentation of the block."
2241 (save-match-data
2242 (let (num cont rtn rpllbl keepp textareap cols rows fmt)
2243 (setq opts (or opts "")
2244 num (string-match "[-+]n\\>" opts)
2245 cont (string-match "\\+n\\>" opts)
2246 rpllbl (string-match "-r\\>" opts)
2247 keepp (string-match "-k\\>" opts)
2248 textareap (string-match "-t\\>" opts)
2249 cols (if (string-match "-w[ \t]+\\([0-9]+\\)" opts)
2250 (string-to-number (match-string 1 opts))
2252 rows (if (string-match "-h[ \t]+\\([0-9]+\\)" opts)
2253 (string-to-number (match-string 1 opts))
2254 (org-count-lines code))
2255 fmt (if (string-match "-l[ \t]+\"\\([^\"\n]+\\)\"" opts)
2256 (match-string 1 opts)))
2257 (when (and textareap (eq backend 'html))
2258 ;; we cannot use numbering or highlighting.
2259 (setq num nil cont nil lang nil))
2260 (if keepp (setq rpllbl 'keep))
2261 (setq rtn (org-remove-indentation code))
2262 (when (string-match "^," rtn)
2263 (setq rtn (with-temp-buffer
2264 (insert rtn)
2265 ;; Free up the protected lines
2266 (goto-char (point-min))
2267 (while (re-search-forward "^," nil t)
2268 (if (or (equal lang "org")
2269 (save-match-data
2270 (looking-at "\\([*#]\\|[ \t]*#\\+\\)")))
2271 (replace-match ""))
2272 (end-of-line 1))
2273 (buffer-string))))
2274 ;; Now backend-specific coding
2275 (setq rtn
2276 (cond
2277 ((eq backend 'docbook)
2278 (setq rtn (org-export-number-lines rtn 'docbook 0 0 num cont rpllbl fmt))
2279 (concat "\n#+BEGIN_DOCBOOK\n"
2280 (org-add-props (concat "<programlisting><![CDATA["
2282 "]]>\n</programlisting>\n")
2283 '(org-protected t))
2284 "#+END_DOCBOOK\n"))
2285 ((eq backend 'html)
2286 ;; We are exporting to HTML
2287 (when lang
2288 (require 'htmlize nil t)
2289 (when (not (fboundp 'htmlize-region-for-paste))
2290 ;; we do not have htmlize.el, or an old version of it
2291 (setq lang nil)
2292 (message
2293 "htmlize.el 1.34 or later is needed for source code formatting")))
2295 (if lang
2296 (let* ((lang-m (when lang
2297 (or (cdr (assoc lang org-src-lang-modes))
2298 lang)))
2299 (mode (and lang-m (intern
2300 (concat
2301 (if (symbolp lang-m)
2302 (symbol-name lang-m)
2303 lang-m)
2304 "-mode"))))
2305 (org-inhibit-startup t)
2306 (org-startup-folded nil))
2307 (setq rtn
2308 (with-temp-buffer
2309 (insert rtn)
2310 (if (functionp mode)
2311 (funcall mode)
2312 (fundamental-mode))
2313 (font-lock-fontify-buffer)
2314 (org-src-mode)
2315 (set-buffer-modified-p nil)
2316 (org-export-htmlize-region-for-paste
2317 (point-min) (point-max))))
2318 (if (string-match "<pre\\([^>]*\\)>\n*" rtn)
2319 (setq rtn (replace-match
2320 (format "<pre class=\"src src-%s\">\n" lang)
2321 t t rtn))))
2322 (if textareap
2323 (setq rtn (concat
2324 (format "<p>\n<textarea cols=\"%d\" rows=\"%d\" overflow-x:scroll >\n"
2325 cols rows)
2326 rtn "</textarea>\n</p>\n"))
2327 (with-temp-buffer
2328 (insert rtn)
2329 (goto-char (point-min))
2330 (while (re-search-forward "[<>&]" nil t)
2331 (replace-match (cdr (assq (char-before)
2332 '((?&."&amp;")(?<."&lt;")(?>."&gt;"))))
2333 t t))
2334 (setq rtn (buffer-string)))
2335 (setq rtn (concat "<pre class=\"example\">\n" rtn "</pre>\n"))))
2336 (unless textareap
2337 (setq rtn (org-export-number-lines rtn 'html 1 1 num
2338 cont rpllbl fmt)))
2339 (if (string-match "\\(\\`<[^>]*>\\)\n" rtn)
2340 (setq rtn (replace-match "\\1" t nil rtn)))
2341 (concat "\n#+BEGIN_HTML\n" (org-add-props rtn '(org-protected t)) "\n#+END_HTML\n\n"))
2342 ((eq backend 'latex)
2343 (setq rtn (org-export-number-lines rtn 'latex 0 0 num cont rpllbl fmt))
2344 (concat "\n#+BEGIN_LaTeX\n"
2345 (org-add-props
2346 (if org-export-latex-listings
2347 (concat
2348 (if lang
2349 (let*
2350 ((lang-sym (intern lang))
2351 (lstlang
2352 (or (cadr
2353 (assq
2354 lang-sym
2355 org-export-latex-listings-langs))
2356 lang)))
2357 (format "\\lstset{language=%s}\n" lstlang))
2359 "\\begin{lstlisting}\n"
2360 rtn "\\end{lstlisting}\n")
2361 (concat (car org-export-latex-verbatim-wrap)
2362 rtn (cdr org-export-latex-verbatim-wrap)))
2363 '(org-protected t))
2364 "#+END_LaTeX\n\n"))
2365 ((eq backend 'ascii)
2366 ;; This is not HTML or LaTeX, so just make it an example.
2367 (setq rtn (org-export-number-lines rtn 'ascii 0 0 num cont rpllbl fmt))
2368 (concat "#+BEGIN_ASCII\n"
2369 (org-add-props
2370 (concat
2371 (mapconcat
2372 (lambda (l) (concat " " l))
2373 (org-split-string rtn "\n")
2374 "\n")
2375 "\n")
2376 '(org-protected t))
2377 "#+END_ASCII\n"))))
2378 (org-add-props rtn nil 'original-indentation indent))))
2380 (defun org-export-number-lines (text backend
2381 &optional skip1 skip2 number cont
2382 replace-labels label-format)
2383 (setq skip1 (or skip1 0) skip2 (or skip2 0))
2384 (if (not cont) (setq org-export-last-code-line-counter-value 0))
2385 (with-temp-buffer
2386 (insert text)
2387 (goto-char (point-max))
2388 (skip-chars-backward " \t\n\r")
2389 (delete-region (point) (point-max))
2390 (beginning-of-line (- 1 skip2))
2391 (let* ((last (org-current-line))
2392 (n org-export-last-code-line-counter-value)
2393 (nmax (+ n (- last skip1)))
2394 (fmt (format "%%%dd: " (length (number-to-string nmax))))
2396 (cond
2397 ((eq backend 'html) (format "<span class=\"linenr\">%s</span>"
2398 fmt))
2399 ((eq backend 'ascii) fmt)
2400 ((eq backend 'latex) fmt)
2401 ((eq backend 'docbook) fmt)
2402 (t "")))
2403 (label-format (or label-format org-coderef-label-format))
2404 (label-pre (if (string-match "%s" label-format)
2405 (substring label-format 0 (match-beginning 0))
2406 label-format))
2407 (label-post (if (string-match "%s" label-format)
2408 (substring label-format (match-end 0))
2409 ""))
2410 (lbl-re
2411 (concat
2412 ".*?\\S-.*?\\([ \t]*\\("
2413 (regexp-quote label-pre)
2414 "\\([-a-zA-Z0-9_ ]+\\)"
2415 (regexp-quote label-post)
2416 "\\)\\)"))
2417 ref)
2419 (org-goto-line (1+ skip1))
2420 (while (and (re-search-forward "^" nil t) (not (eobp)) (< n nmax))
2421 (if number
2422 (insert (format fm (incf n)))
2423 (forward-char 1))
2424 (when (looking-at lbl-re)
2425 (setq ref (match-string 3))
2426 (cond ((numberp replace-labels)
2427 ;; remove labels; use numbers for references when lines
2428 ;; are numbered, use labels otherwise
2429 (delete-region (match-beginning 1) (match-end 1))
2430 (push (cons ref (if (> n 0) n ref)) org-export-code-refs))
2431 ((eq replace-labels 'keep)
2432 ;; don't remove labels; use numbers for references when
2433 ;; lines are numbered, use labels otherwise
2434 (goto-char (match-beginning 2))
2435 (delete-region (match-beginning 2) (match-end 2))
2436 (insert "(" ref ")")
2437 (push (cons ref (if (> n 0) n (concat "(" ref ")")))
2438 org-export-code-refs))
2440 ;; don't remove labels and don't use numbers for
2441 ;; references
2442 (goto-char (match-beginning 2))
2443 (delete-region (match-beginning 2) (match-end 2))
2444 (insert "(" ref ")")
2445 (push (cons ref (concat "(" ref ")")) org-export-code-refs)))
2446 (when (eq backend 'html)
2447 (save-excursion
2448 (beginning-of-line 1)
2449 (insert (format "<span id=\"coderef-%s\" class=\"coderef-off\">"
2450 ref))
2451 (end-of-line 1)
2452 (insert "</span>")))))
2453 (setq org-export-last-code-line-counter-value n)
2454 (goto-char (point-max))
2455 (newline)
2456 (buffer-string))))
2458 (defun org-search-todo-below (line lines level)
2459 "Search the subtree below LINE for any TODO entries."
2460 (let ((rest (cdr (memq line lines)))
2461 (re org-todo-line-regexp)
2462 line lv todo)
2463 (catch 'exit
2464 (while (setq line (pop rest))
2465 (if (string-match re line)
2466 (progn
2467 (setq lv (- (match-end 1) (match-beginning 1))
2468 todo (and (match-beginning 2)
2469 (not (member (match-string 2 line)
2470 org-done-keywords))))
2471 ; TODO, not DONE
2472 (if (<= lv level) (throw 'exit nil))
2473 (if todo (throw 'exit t))))))))
2475 ;;;###autoload
2476 (defun org-export-visible (type arg)
2477 "Create a copy of the visible part of the current buffer, and export it.
2478 The copy is created in a temporary buffer and removed after use.
2479 TYPE is the final key (as a string) that also select the export command in
2480 the `C-c C-e' export dispatcher.
2481 As a special case, if the you type SPC at the prompt, the temporary
2482 org-mode file will not be removed but presented to you so that you can
2483 continue to use it. The prefix arg ARG is passed through to the exporting
2484 command."
2485 (interactive
2486 (list (progn
2487 (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [D]ocBook [x]OXO [ ]keep buffer")
2488 (read-char-exclusive))
2489 current-prefix-arg))
2490 (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?D ?x ?\ )))
2491 (error "Invalid export key"))
2492 (let* ((binding (cdr (assoc type
2493 '((?a . org-export-as-ascii)
2494 (?A . org-export-as-ascii-to-buffer)
2495 (?\C-a . org-export-as-ascii)
2496 (?b . org-export-as-html-and-open)
2497 (?\C-b . org-export-as-html-and-open)
2498 (?h . org-export-as-html)
2499 (?H . org-export-as-html-to-buffer)
2500 (?R . org-export-region-as-html)
2501 (?D . org-export-as-docbook)
2502 (?x . org-export-as-xoxo)))))
2503 (keepp (equal type ?\ ))
2504 (file buffer-file-name)
2505 (buffer (get-buffer-create "*Org Export Visible*"))
2506 s e)
2507 ;; Need to hack the drawers here.
2508 (save-excursion
2509 (goto-char (point-min))
2510 (while (re-search-forward org-drawer-regexp nil t)
2511 (goto-char (match-beginning 1))
2512 (or (org-invisible-p) (org-flag-drawer nil))))
2513 (with-current-buffer buffer (erase-buffer))
2514 (save-excursion
2515 (setq s (goto-char (point-min)))
2516 (while (not (= (point) (point-max)))
2517 (goto-char (org-find-invisible))
2518 (append-to-buffer buffer s (point))
2519 (setq s (goto-char (org-find-visible))))
2520 (org-cycle-hide-drawers 'all)
2521 (goto-char (point-min))
2522 (unless keepp
2523 ;; Copy all comment lines to the end, to make sure #+ settings are
2524 ;; still available for the second export step. Kind of a hack, but
2525 ;; does do the trick.
2526 (if (looking-at "#[^\r\n]*")
2527 (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
2528 (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
2529 (append-to-buffer buffer (1+ (match-beginning 0))
2530 (min (point-max) (1+ (match-end 0))))))
2531 (set-buffer buffer)
2532 (let ((buffer-file-name file)
2533 (org-inhibit-startup t))
2534 (org-mode)
2535 (show-all)
2536 (unless keepp (funcall binding arg))))
2537 (if (not keepp)
2538 (kill-buffer buffer)
2539 (switch-to-buffer-other-window buffer)
2540 (goto-char (point-min)))))
2542 (defun org-find-visible ()
2543 (let ((s (point)))
2544 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
2545 (get-char-property s 'invisible)))
2547 (defun org-find-invisible ()
2548 (let ((s (point)))
2549 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
2550 (not (get-char-property s 'invisible))))
2553 (defvar org-export-htmlized-org-css-url) ;; defined in org-html.el
2555 ;;;###autoload
2556 (defun org-export-as-org (arg &optional hidden ext-plist
2557 to-buffer body-only pub-dir)
2558 "Make a copy with not-exporting stuff removed.
2559 The purpose of this function is to provide a way to export the source
2560 Org file of a webpage in Org format, but with sensitive and/or irrelevant
2561 stuff removed. This command will remove the following:
2563 - archived trees (if the variable `org-export-with-archived-trees' is nil)
2564 - comment blocks and trees starting with the COMMENT keyword
2565 - only trees that are consistent with `org-export-select-tags'
2566 and `org-export-exclude-tags'.
2568 The only arguments that will be used are EXT-PLIST and PUB-DIR,
2569 all the others will be ignored (but are present so that the general
2570 mechanism to call publishing functions will work).
2572 EXT-PLIST is a property list with external parameters overriding
2573 org-mode's default settings, but still inferior to file-local
2574 settings. When PUB-DIR is set, use this as the publishing
2575 directory."
2576 (interactive "P")
2577 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
2578 ext-plist
2579 (org-infile-export-plist)))
2580 (bfname (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
2581 (filename (concat (file-name-as-directory
2582 (or pub-dir
2583 (org-export-directory :org opt-plist)))
2584 (file-name-sans-extension
2585 (file-name-nondirectory bfname))
2586 ".org"))
2587 (filename (and filename
2588 (if (equal (file-truename filename)
2589 (file-truename bfname))
2590 (concat (file-name-sans-extension filename)
2591 "-source."
2592 (file-name-extension filename))
2593 filename)))
2594 (backup-inhibited t)
2595 (buffer (find-file-noselect filename))
2596 (region (buffer-string)))
2597 (save-excursion
2598 (switch-to-buffer buffer)
2599 (erase-buffer)
2600 (insert region)
2601 (let ((org-inhibit-startup t)) (org-mode))
2602 (org-install-letbind)
2604 ;; Get rid of archived trees
2605 (org-export-remove-archived-trees (plist-get opt-plist :archived-trees))
2607 ;; Remove comment environment and comment subtrees
2608 (org-export-remove-comment-blocks-and-subtrees)
2610 ;; Get rid of excluded trees
2611 (org-export-handle-export-tags (plist-get opt-plist :select-tags)
2612 (plist-get opt-plist :exclude-tags))
2614 (when (or (plist-get opt-plist :plain-source)
2615 (not (or (plist-get opt-plist :plain-source)
2616 (plist-get opt-plist :htmlized-source))))
2617 ;; Either nothing special is requested (default call)
2618 ;; or the plain source is explicitly requested
2619 ;; so: save it
2620 (save-buffer))
2621 (when (plist-get opt-plist :htmlized-source)
2622 ;; Make the htmlized version
2623 (require 'htmlize)
2624 (require 'org-html)
2625 (font-lock-fontify-buffer)
2626 (let* ((htmlize-output-type 'css)
2627 (newbuf (htmlize-buffer)))
2628 (with-current-buffer newbuf
2629 (when org-export-htmlized-org-css-url
2630 (goto-char (point-min))
2631 (and (re-search-forward
2632 "<style type=\"text/css\">[^\000]*?\n[ \t]*</style>.*"
2633 nil t)
2634 (replace-match
2635 (format
2636 "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\">"
2637 org-export-htmlized-org-css-url)
2638 t t)))
2639 (write-file (concat filename ".html")))
2640 (kill-buffer newbuf)))
2641 (set-buffer-modified-p nil)
2642 (kill-buffer (current-buffer)))))
2644 (defvar org-archive-location) ;; gets loaded with the org-archive require.
2645 (defun org-get-current-options ()
2646 "Return a string with current options as keyword options.
2647 Does include HTML export options as well as TODO and CATEGORY stuff."
2648 (require 'org-archive)
2649 (format
2650 "#+TITLE: %s
2651 #+AUTHOR: %s
2652 #+EMAIL: %s
2653 #+DATE: %s
2654 #+DESCRIPTION:
2655 #+KEYWORDS:
2656 #+LANGUAGE: %s
2657 #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s -:%s f:%s *:%s <:%s
2658 #+OPTIONS: TeX:%s LaTeX:%s skip:%s d:%s todo:%s pri:%s tags:%s
2660 #+EXPORT_SELECT_TAGS: %s
2661 #+EXPORT_EXCLUDE_TAGS: %s
2662 #+LINK_UP: %s
2663 #+LINK_HOME: %s
2664 #+CATEGORY: %s
2665 #+SEQ_TODO: %s
2666 #+TYP_TODO: %s
2667 #+PRIORITIES: %c %c %c
2668 #+DRAWERS: %s
2669 #+STARTUP: %s %s %s %s %s
2670 #+TAGS: %s
2671 #+FILETAGS: %s
2672 #+ARCHIVE: %s
2673 #+LINK: %s
2675 (buffer-name) (user-full-name) user-mail-address
2676 (format-time-string (substring (car org-time-stamp-formats) 1 -1))
2677 org-export-default-language
2678 org-export-headline-levels
2679 org-export-with-section-numbers
2680 org-export-with-toc
2681 org-export-preserve-breaks
2682 org-export-html-expand
2683 org-export-with-fixed-width
2684 org-export-with-tables
2685 org-export-with-sub-superscripts
2686 org-export-with-special-strings
2687 org-export-with-footnotes
2688 org-export-with-emphasize
2689 org-export-with-timestamps
2690 org-export-with-TeX-macros
2691 org-export-with-LaTeX-fragments
2692 org-export-skip-text-before-1st-heading
2693 org-export-with-drawers
2694 org-export-with-todo-keywords
2695 org-export-with-priority
2696 org-export-with-tags
2697 (if (featurep 'org-jsinfo) (org-infojs-options-inbuffer-template) "")
2698 (mapconcat 'identity org-export-select-tags " ")
2699 (mapconcat 'identity org-export-exclude-tags " ")
2700 org-export-html-link-up
2701 org-export-html-link-home
2702 (or (ignore-errors
2703 (file-name-sans-extension
2704 (file-name-nondirectory (buffer-file-name (buffer-base-buffer)))))
2705 "NOFILENAME")
2706 "TODO FEEDBACK VERIFY DONE"
2707 "Me Jason Marie DONE"
2708 org-highest-priority org-lowest-priority org-default-priority
2709 (mapconcat 'identity org-drawers " ")
2710 (cdr (assoc org-startup-folded
2711 '((nil . "showall") (t . "overview") (content . "content"))))
2712 (if org-odd-levels-only "odd" "oddeven")
2713 (if org-hide-leading-stars "hidestars" "showstars")
2714 (if org-startup-align-all-tables "align" "noalign")
2715 (cond ((eq org-log-done t) "logdone")
2716 ((equal org-log-done 'note) "lognotedone")
2717 ((not org-log-done) "nologdone"))
2718 (or (mapconcat (lambda (x)
2719 (cond
2720 ((equal '(:startgroup) x) "{")
2721 ((equal '(:endgroup) x) "}")
2722 ((cdr x) (format "%s(%c)" (car x) (cdr x)))
2723 (t (car x))))
2724 (or org-tag-alist (org-get-buffer-tags)) " ") "")
2725 (mapconcat 'identity org-file-tags " ")
2726 org-archive-location
2727 "org file:~/org/%s.org"
2730 ;;;###autoload
2731 (defun org-insert-export-options-template ()
2732 "Insert into the buffer a template with information for exporting."
2733 (interactive)
2734 (if (not (bolp)) (newline))
2735 (let ((s (org-get-current-options)))
2736 (and (string-match "#\\+CATEGORY" s)
2737 (setq s (substring s 0 (match-beginning 0))))
2738 (insert s)))
2740 (defvar org-table-colgroup-info nil)
2742 (defun org-table-clean-before-export (lines &optional maybe-quoted)
2743 "Check if the table has a marking column.
2744 If yes remove the column and the special lines."
2745 (setq org-table-colgroup-info nil)
2746 (if (memq nil
2747 (mapcar
2748 (lambda (x) (or (string-match "^[ \t]*|-" x)
2749 (string-match
2750 (if maybe-quoted
2751 "^[ \t]*| *\\\\?\\([\#!$*_^ /]\\) *|"
2752 "^[ \t]*| *\\([\#!$*_^ /]\\) *|")
2753 x)))
2754 lines))
2755 (progn
2756 (setq org-table-clean-did-remove-column nil)
2757 (delq nil
2758 (mapcar
2759 (lambda (x)
2760 (cond
2761 ((string-match "^[ \t]*| */ *|" x)
2762 (setq org-table-colgroup-info
2763 (mapcar (lambda (x)
2764 (cond ((member x '("<" "&lt;")) :start)
2765 ((member x '(">" "&gt;")) :end)
2766 ((member x '("<>" "&lt;&gt;")) :startend)
2767 (t nil)))
2768 (org-split-string x "[ \t]*|[ \t]*")))
2769 nil)
2770 (t x)))
2771 lines)))
2772 (setq org-table-clean-did-remove-column t)
2773 (delq nil
2774 (mapcar
2775 (lambda (x)
2776 (cond
2777 ((string-match "^[ \t]*| */ *|" x)
2778 (setq org-table-colgroup-info
2779 (mapcar (lambda (x)
2780 (cond ((member x '("<" "&lt;")) :start)
2781 ((member x '(">" "&gt;")) :end)
2782 ((member x '("<>" "&lt;&gt;")) :startend)
2783 (t nil)))
2784 (cdr (org-split-string x "[ \t]*|[ \t]*"))))
2785 nil)
2786 ((string-match "^[ \t]*| *[!_^/] *|" x)
2787 nil) ; ignore this line
2788 ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
2789 (string-match "^\\([ \t]*\\)|[^|]*|" x))
2790 ;; remove the first column
2791 (replace-match "\\1|" t nil x))))
2792 lines))))
2794 (defun org-export-cleanup-toc-line (s)
2795 "Remove tags and timestamps from lines going into the toc."
2796 (when (memq org-export-with-tags '(not-in-toc nil))
2797 (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s)
2798 (setq s (replace-match "" t t s))))
2799 (when org-export-remove-timestamps-from-toc
2800 (while (string-match org-maybe-keyword-time-regexp s)
2801 (setq s (replace-match "" t t s))))
2802 (while (string-match org-bracket-link-regexp s)
2803 (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
2804 t t s)))
2807 (defun org-create-multibrace-regexp (left right n)
2808 "Create a regular expression which will match a balanced sexp.
2809 Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
2810 as single character strings.
2811 The regexp returned will match the entire expression including the
2812 delimiters. It will also define a single group which contains the
2813 match except for the outermost delimiters. The maximum depth of
2814 stacked delimiters is N. Escaping delimiters is not possible."
2815 (let* ((nothing (concat "[^" left right "]*?"))
2816 (or "\\|")
2817 (re nothing)
2818 (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
2819 (while (> n 1)
2820 (setq n (1- n)
2821 re (concat re or next)
2822 next (concat "\\(?:" nothing left next right "\\)+" nothing)))
2823 (concat left "\\(" re "\\)" right)))
2825 (defvar org-match-substring-regexp
2826 (concat
2827 "\\([^\\]\\)\\([_^]\\)\\("
2828 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
2829 "\\|"
2830 "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
2831 "\\|"
2832 "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
2833 "The regular expression matching a sub- or superscript.")
2835 (defvar org-match-substring-with-braces-regexp
2836 (concat
2837 "\\([^\\]\\)\\([_^]\\)\\("
2838 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
2839 "\\)")
2840 "The regular expression matching a sub- or superscript, forcing braces.")
2843 (defun org-get-text-property-any (pos prop &optional object)
2844 (or (get-text-property pos prop object)
2845 (and (setq pos (next-single-property-change pos prop object))
2846 (get-text-property pos prop object))))
2848 (defun org-export-get-coderef-format (path desc)
2849 (save-match-data
2850 (if (and desc (string-match
2851 (regexp-quote (concat "(" path ")"))
2852 desc))
2853 (replace-match "%s" t t desc)
2854 (or desc "%s"))))
2856 (defun org-export-push-to-kill-ring (format)
2857 "Push buffer content to kill ring.
2858 The depends on the variable `org-export-copy-to-kill'."
2859 (when org-export-copy-to-kill-ring
2860 (org-kill-new (buffer-string))
2861 (when (fboundp 'x-set-selection)
2862 (ignore-errors (x-set-selection 'PRIMARY (buffer-string)))
2863 (ignore-errors (x-set-selection 'CLIPBOARD (buffer-string))))
2864 (message "%s export done, pushed to kill ring and clipboard" format)))
2866 (provide 'org-exp)
2868 ;; arch-tag: 65985fe9-095c-49c7-a7b6-cb4ee15c0a95
2870 ;;; org-exp.el ends here