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