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