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