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