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