Improve footnotes handling in exporters
[org-mode/org-jambu.git] / lisp / org-exp.el
blob8c414ec75056f7708a718659eb5483c7d95240f0
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-markers nil
627 "Alist of markers used for footnotes, along with their associated definition.")
628 (defvar org-export-footnotes-data nil
629 "Alist of labels used in buffers, along with their associated definition.")
631 (defconst org-export-plist-vars
632 '((:link-up nil org-export-html-link-up)
633 (:link-home nil org-export-html-link-home)
634 (:language nil org-export-default-language)
635 (:keywords nil org-export-page-keywords)
636 (:description nil org-export-page-description)
637 (:customtime nil org-display-custom-times)
638 (:headline-levels "H" org-export-headline-levels)
639 (:section-numbers "num" org-export-with-section-numbers)
640 (:section-number-format nil org-export-section-number-format)
641 (:table-of-contents "toc" org-export-with-toc)
642 (:preserve-breaks "\\n" org-export-preserve-breaks)
643 (:archived-trees nil org-export-with-archived-trees)
644 (:emphasize "*" org-export-with-emphasize)
645 (:sub-superscript "^" org-export-with-sub-superscripts)
646 (:special-strings "-" org-export-with-special-strings)
647 (:footnotes "f" org-export-with-footnotes)
648 (:drawers "d" org-export-with-drawers)
649 (:tags "tags" org-export-with-tags)
650 (:todo-keywords "todo" org-export-with-todo-keywords)
651 (:tasks "tasks" org-export-with-tasks)
652 (:priority "pri" org-export-with-priority)
653 (:TeX-macros "TeX" org-export-with-TeX-macros)
654 (:LaTeX-fragments "LaTeX" org-export-with-LaTeX-fragments)
655 (:latex-listings nil org-export-latex-listings)
656 (:skip-before-1st-heading "skip" org-export-skip-text-before-1st-heading)
657 (:fixed-width ":" org-export-with-fixed-width)
658 (:timestamps "<" org-export-with-timestamps)
659 (:author nil user-full-name)
660 (:email nil user-mail-address)
661 (:author-info "author" org-export-author-info)
662 (:email-info "email" org-export-email-info)
663 (:creator-info "creator" org-export-creator-info)
664 (:time-stamp-file "timestamp" org-export-time-stamp-file)
665 (:tables "|" org-export-with-tables)
666 (:table-auto-headline nil org-export-highlight-first-table-line)
667 (:style-include-default nil org-export-html-style-include-default)
668 (:style-include-scripts nil org-export-html-style-include-scripts)
669 (:style nil org-export-html-style)
670 (:style-extra nil org-export-html-style-extra)
671 (:agenda-style nil org-agenda-export-html-style)
672 (:convert-org-links nil org-export-html-link-org-files-as-html)
673 (:inline-images nil org-export-html-inline-images)
674 (:html-extension nil org-export-html-extension)
675 (:html-preamble nil org-export-html-preamble)
676 (:html-postamble nil org-export-html-postamble)
677 (:xml-declaration nil org-export-html-xml-declaration)
678 (:html-table-tag nil org-export-html-table-tag)
679 (:expand-quoted-html "@" org-export-html-expand)
680 (:timestamp nil org-export-html-with-timestamp)
681 (:publishing-directory nil org-export-publishing-directory)
682 (:select-tags nil org-export-select-tags)
683 (:exclude-tags nil org-export-exclude-tags)
685 (:latex-image-options nil org-export-latex-image-default-option))
686 "List of properties that represent export/publishing variables.
687 Each element is a list of 3 items:
688 1. The property that is used internally, and also for org-publish-project-alist
689 2. The string that can be used in the OPTION lines to set this option,
690 or nil if this option cannot be changed in this way
691 3. The customization variable that sets the default for this option."
694 (defun org-default-export-plist ()
695 "Return the property list with default settings for the export variables."
696 (let* ((infile (org-infile-export-plist))
697 (letbind (plist-get infile :let-bind))
698 (l org-export-plist-vars) rtn e s v)
699 (while (setq e (pop l))
700 (setq s (nth 2 e)
701 v (cond
702 ((assq s letbind) (nth 1 (assq s letbind)))
703 ((boundp s) (symbol-value s))
704 (t nil))
705 rtn (cons (car e) (cons v rtn))))
706 rtn))
708 (defvar org-export-inbuffer-options-extra nil
709 "List of additional in-buffer options that should be detected.
710 Just before export, the buffer is scanned for options like #+TITLE, #+EMAIL,
711 etc. Extensions can add to this list to get their options detected, and they
712 can then add a function to `org-export-options-filters' to process these
713 options.
714 Each element in this list must be a list, with the in-buffer keyword as car,
715 and a property (a symbol) as the next element. All occurrences of the
716 keyword will be found, the values concatenated with a space character
717 in between, and the result stored in the export options property list.")
719 (defvar org-export-options-filters nil
720 "Functions to be called to finalize the export/publishing options.
721 All these options are stored in a property list, and each of the functions
722 in this hook gets a chance to modify this property list. Each function
723 must accept the property list as an argument, and must return the (possibly
724 modified) list.")
726 ;; FIXME: should we fold case here?
727 (defun org-infile-export-plist ()
728 "Return the property list with file-local settings for export."
729 (save-excursion
730 (save-restriction
731 (widen)
732 (goto-char (point-min))
733 (let ((re (org-make-options-regexp
734 (append
735 '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE"
736 "MATHJAX"
737 "LINK_UP" "LINK_HOME" "SETUPFILE" "STYLE"
738 "LATEX_HEADER" "LATEX_CLASS"
739 "EXPORT_SELECT_TAGS" "EXPORT_EXCLUDE_TAGS"
740 "KEYWORDS" "DESCRIPTION" "MACRO" "BIND" "XSLT")
741 (mapcar 'car org-export-inbuffer-options-extra))))
742 (case-fold-search t)
743 p key val text options mathjax a pr style
744 latex-header latex-class macros letbind
745 ext-setup-or-nil setup-contents (start 0))
746 (while (or (and ext-setup-or-nil
747 (string-match re ext-setup-or-nil start)
748 (setq start (match-end 0)))
749 (and (setq ext-setup-or-nil nil start 0)
750 (re-search-forward re nil t)))
751 (setq key (upcase (org-match-string-no-properties 1 ext-setup-or-nil))
752 val (org-match-string-no-properties 2 ext-setup-or-nil))
753 (cond
754 ((setq a (assoc key org-export-inbuffer-options-extra))
755 (setq pr (nth 1 a))
756 (setq p (plist-put p pr (concat (plist-get p pr) " " val))))
757 ((string-equal key "TITLE") (setq p (plist-put p :title val)))
758 ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
759 ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
760 ((string-equal key "DATE") (setq p (plist-put p :date val)))
761 ((string-equal key "KEYWORDS") (setq p (plist-put p :keywords val)))
762 ((string-equal key "DESCRIPTION")
763 (setq p (plist-put p :description val)))
764 ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
765 ((string-equal key "STYLE")
766 (setq style (concat style "\n" val)))
767 ((string-equal key "LATEX_HEADER")
768 (setq latex-header (concat latex-header "\n" val)))
769 ((string-equal key "LATEX_CLASS")
770 (setq latex-class val))
771 ((string-equal key "TEXT")
772 (setq text (if text (concat text "\n" val) val)))
773 ((string-equal key "OPTIONS")
774 (setq options (concat val " " options)))
775 ((string-equal key "MATHJAX")
776 (setq mathjax (concat val " " mathjax)))
777 ((string-equal key "BIND")
778 (push (read (concat "(" val ")")) letbind))
779 ((string-equal key "XSLT")
780 (setq p (plist-put p :xslt val)))
781 ((string-equal key "LINK_UP")
782 (setq p (plist-put p :link-up val)))
783 ((string-equal key "LINK_HOME")
784 (setq p (plist-put p :link-home val)))
785 ((string-equal key "EXPORT_SELECT_TAGS")
786 (setq p (plist-put p :select-tags (org-split-string val))))
787 ((string-equal key "EXPORT_EXCLUDE_TAGS")
788 (setq p (plist-put p :exclude-tags (org-split-string val))))
789 ((string-equal key "MACRO")
790 (push val macros))
791 ((equal key "SETUPFILE")
792 (setq setup-contents (org-file-contents
793 (expand-file-name
794 (org-remove-double-quotes
795 (org-trim val)))
796 'noerror))
797 (if (not ext-setup-or-nil)
798 (setq ext-setup-or-nil setup-contents start 0)
799 (setq ext-setup-or-nil
800 (concat (substring ext-setup-or-nil 0 start)
801 "\n" setup-contents "\n"
802 (substring ext-setup-or-nil start)))))))
803 (setq p (plist-put p :text text))
804 (when (and letbind (org-export-confirm-letbind))
805 (setq p (plist-put p :let-bind letbind)))
806 (when style (setq p (plist-put p :style-extra style)))
807 (when latex-header
808 (setq p (plist-put p :latex-header-extra (substring latex-header 1))))
809 (when latex-class
810 (setq p (plist-put p :latex-class latex-class)))
811 (when options
812 (setq p (org-export-add-options-to-plist p options)))
813 (when mathjax
814 (setq p (plist-put p :mathjax mathjax)))
815 ;; Add macro definitions
816 (setq p (plist-put p :macro-date "(eval (format-time-string \"$1\"))"))
817 (setq p (plist-put p :macro-time "(eval (format-time-string \"$1\"))"))
818 (setq p (plist-put p :macro-property "(eval (org-entry-get nil \"$1\" 'selective))"))
819 (setq p (plist-put
820 p :macro-modification-time
821 (and (buffer-file-name)
822 (file-exists-p (buffer-file-name))
823 (concat
824 "(eval (format-time-string \"$1\" '"
825 (prin1-to-string (nth 5 (file-attributes
826 (buffer-file-name))))
827 "))"))))
828 (setq p (plist-put p :macro-input-file (and (buffer-file-name)
829 (file-name-nondirectory
830 (buffer-file-name)))))
831 (while (setq val (pop macros))
832 (when (string-match "^\\([-a-zA-Z0-9_]+\\)[ \t]+\\(.*?[ \t]*$\\)" val)
833 (setq p (plist-put
834 p (intern
835 (concat ":macro-" (downcase (match-string 1 val))))
836 (org-export-interpolate-newlines (match-string 2 val))))))
837 p))))
839 (defun org-export-interpolate-newlines (s)
840 (while (string-match "\\\\n" s)
841 (setq s (replace-match "\n" t t s)))
844 (defvar org-export-allow-BIND-local nil)
845 (defun org-export-confirm-letbind ()
846 "Can we use #+BIND values during export?
847 By default this will ask for confirmation by the user, to divert possible
848 security risks."
849 (cond
850 ((not org-export-allow-BIND) nil)
851 ((eq org-export-allow-BIND t) t)
852 ((local-variable-p 'org-export-allow-BIND-local (current-buffer))
853 org-export-allow-BIND-local)
854 (t (org-set-local 'org-export-allow-BIND-local
855 (yes-or-no-p "Allow BIND values in this buffer? ")))))
857 (defun org-install-letbind ()
858 "Install the values from #+BIND lines as local variables."
859 (let ((letbind (plist-get org-export-opt-plist :let-bind))
860 pair)
861 (while (setq pair (pop letbind))
862 (org-set-local (car pair) (nth 1 pair)))))
864 (defun org-export-add-options-to-plist (p options)
865 "Parse an OPTIONS line and set values in the property list P."
866 (let (o)
867 (when options
868 (let ((op org-export-plist-vars))
869 (while (setq o (pop op))
870 (if (and (nth 1 o)
871 (string-match (concat "\\(\\`\\|[ \t]\\)"
872 (regexp-quote (nth 1 o))
873 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
874 options))
875 (setq p (plist-put p (car o)
876 (car (read-from-string
877 (match-string 2 options))))))))))
880 (defun org-export-add-subtree-options (p pos)
881 "Add options in subtree at position POS to property list P."
882 (save-excursion
883 (goto-char pos)
884 (when (org-at-heading-p)
885 (let (a)
886 ;; This is actually read in `org-export-get-title-from-subtree'
887 ;; (when (setq a (org-entry-get pos "EXPORT_TITLE"))
888 ;; (setq p (plist-put p :title a)))
889 (when (setq a (org-entry-get pos "EXPORT_TEXT"))
890 (setq p (plist-put p :text a)))
891 (when (setq a (org-entry-get pos "EXPORT_AUTHOR"))
892 (setq p (plist-put p :author a)))
893 (when (setq a (org-entry-get pos "EXPORT_DATE"))
894 (setq p (plist-put p :date a)))
895 (when (setq a (org-entry-get pos "EXPORT_OPTIONS"))
896 (setq p (org-export-add-options-to-plist p a)))))
899 (defun org-export-directory (type plist)
900 (let* ((val (plist-get plist :publishing-directory))
901 (dir (if (listp val)
902 (or (cdr (assoc type val)) ".")
903 val)))
904 dir))
906 (defun org-export-process-option-filters (plist)
907 (let ((functions org-export-options-filters) f)
908 (while (setq f (pop functions))
909 (setq plist (funcall f plist))))
910 plist)
912 ;;;###autoload
913 (defun org-export (&optional arg)
914 "Export dispatcher for Org-mode.
915 When `org-export-run-in-background' is non-nil, try to run the command
916 in the background. This will be done only for commands that write
917 to a file. For details see the docstring of `org-export-run-in-background'.
919 The prefix argument ARG will be passed to the exporter. However, if
920 ARG is a double universal prefix \\[universal-argument] \\[universal-argument], \
921 that means to inverse the
922 value of `org-export-run-in-background'.
924 If `org-export-initial-scope' is set to 'subtree, try to export
925 the current subtree, otherwise try to export the whole buffer.
926 Pressing `1' will switch between these two options."
927 (interactive "P")
928 (let* ((bg (org-xor (equal arg '(16)) org-export-run-in-background))
929 (subtree-p (or (org-region-active-p)
930 (eq org-export-initial-scope 'subtree)))
931 (help "[t] insert the export option template
932 \[v] limit export to visible part of outline tree
933 \[1] switch buffer/subtree export
934 \[SPC] publish enclosing subtree (with LaTeX_CLASS or EXPORT_FILE_NAME prop)
936 \[a/n/u] export as ASCII/Latin-1/UTF-8 [A/N/U] to temporary buffer
938 \[h] export as HTML [H] to temporary buffer [R] export region
939 \[b] export as HTML and open in browser
941 \[l] export as LaTeX [L] to temporary buffer
942 \[p] export as LaTeX and process to PDF [d] ... and open PDF file
944 \[D] export as DocBook [V] export as DocBook, process to PDF, and open
946 \[o] export as OpenDocumentText [O] ... and open
948 \[j] export as TaskJuggler [J] ... and open
950 \[m] export as Freemind mind map
951 \[x] export as XOXO
952 \[g] export using Wes Hardaker's generic exporter
954 \[i] export current file as iCalendar file
955 \[I] export all agenda files as iCalendar files [c] ...as one combined file
957 \[F] publish current file [P] publish current project
958 \[X] publish a project... [E] publish every projects")
959 (cmds
960 '((?t org-insert-export-options-template nil)
961 (?v org-export-visible nil)
962 (?a org-export-as-ascii t)
963 (?A org-export-as-ascii-to-buffer t)
964 (?n org-export-as-latin1 t)
965 (?N org-export-as-latin1-to-buffer t)
966 (?u org-export-as-utf8 t)
967 (?U org-export-as-utf8-to-buffer t)
968 (?h org-export-as-html t)
969 (?b org-export-as-html-and-open t)
970 (?H org-export-as-html-to-buffer nil)
971 (?R org-export-region-as-html nil)
972 (?x org-export-as-xoxo t)
973 (?g org-export-generic t)
974 (?D org-export-as-docbook t)
975 (?V org-export-as-docbook-pdf-and-open t)
976 (?o org-export-as-odt t)
977 (?O org-export-as-odt-and-open t)
978 (?j org-export-as-taskjuggler t)
979 (?J org-export-as-taskjuggler-and-open t)
980 (?m org-export-as-freemind t)
981 (?l org-export-as-latex t)
982 (?p org-export-as-pdf t)
983 (?d org-export-as-pdf-and-open t)
984 (?L org-export-as-latex-to-buffer nil)
985 (?i org-export-icalendar-this-file t)
986 (?I org-export-icalendar-all-agenda-files t)
987 (?c org-export-icalendar-combine-agenda-files t)
988 (?F org-publish-current-file t)
989 (?P org-publish-current-project t)
990 (?X org-publish t)
991 (?E org-publish-all t)))
992 r1 r2 ass
993 (cpos (point)) (cbuf (current-buffer)) bpos)
994 (save-excursion
995 (save-window-excursion
996 (if subtree-p
997 (message "Export subtree: ")
998 (message "Export buffer: "))
999 (delete-other-windows)
1000 (with-output-to-temp-buffer "*Org Export/Publishing Help*"
1001 (princ help))
1002 (org-fit-window-to-buffer (get-buffer-window
1003 "*Org Export/Publishing Help*"))
1004 (while (eq (setq r1 (read-char-exclusive)) ?1)
1005 (cond (subtree-p
1006 (setq subtree-p nil)
1007 (message "Export buffer: "))
1008 ((not subtree-p)
1009 (setq subtree-p t)
1010 (message "Export subtree: "))))
1011 (when (eq r1 ?\ )
1012 (let ((case-fold-search t)
1013 (end (save-excursion (while (org-up-heading-safe)) (point))))
1014 (outline-next-heading)
1015 (if (re-search-backward
1016 "^[ \t]+\\(:latex_class:\\|:export_title:\\|:export_file_name:\\)[ \t]+\\S-"
1017 end t)
1018 (progn
1019 (org-back-to-heading t)
1020 (setq subtree-p t)
1021 (setq bpos (point))
1022 (message "Select command (for subtree): ")
1023 (setq r1 (read-char-exclusive)))
1024 (error "No enclosing node with LaTeX_CLASS or EXPORT_TITLE or EXPORT_FILE_NAME")
1025 )))))
1026 (redisplay)
1027 (and bpos (goto-char bpos))
1028 (setq r2 (if (< r1 27) (+ r1 96) r1))
1029 (unless (setq ass (assq r2 cmds))
1030 (error "No command associated with key %c" r1))
1031 (if (and bg (nth 2 ass)
1032 (not (buffer-base-buffer))
1033 (not (org-region-active-p)))
1034 ;; execute in background
1035 (let ((p (start-process
1036 (concat "Exporting " (file-name-nondirectory (buffer-file-name)))
1037 "*Org Processes*"
1038 (expand-file-name invocation-name invocation-directory)
1039 "-batch"
1040 "-l" user-init-file
1041 "--eval" "(require 'org-exp)"
1042 "--eval" "(setq org-wait .2)"
1043 (buffer-file-name)
1044 "-f" (symbol-name (nth 1 ass)))))
1045 (set-process-sentinel p 'org-export-process-sentinel)
1046 (message "Background process \"%s\": started" p))
1047 ;; background processing not requested, or not possible
1048 (if subtree-p (progn (org-mark-subtree) (org-activate-mark)))
1049 (call-interactively (nth 1 ass))
1050 (when (and bpos (get-buffer-window cbuf))
1051 (let ((cw (selected-window)))
1052 (select-window (get-buffer-window cbuf))
1053 (goto-char cpos)
1054 (deactivate-mark)
1055 (select-window cw))))))
1057 (defun org-export-process-sentinel (process status)
1058 (if (string-match "\n+\\'" status)
1059 (setq status (substring status 0 -1)))
1060 (message "Background process \"%s\": %s" process status))
1062 ;;; General functions for all backends
1064 (defvar org-export-target-aliases nil
1065 "Alist of targets with invisible aliases.")
1066 (defvar org-export-preferred-target-alist nil
1067 "Alist of section id's with preferred aliases.")
1068 (defvar org-export-id-target-alist nil
1069 "Alist of section id's with preferred aliases.")
1070 (defvar org-export-code-refs nil
1071 "Alist of code references and line numbers.")
1073 (defun org-export-preprocess-string (string &rest parameters)
1074 "Cleanup STRING so that the true exported has a more consistent source.
1075 This function takes STRING, which should be a buffer-string of an org-file
1076 to export. It then creates a temporary buffer where it does its job.
1077 The result is then again returned as a string, and the exporter works
1078 on this string to produce the exported version."
1079 (interactive)
1080 (let* ((org-export-current-backend (plist-get parameters :for-backend))
1081 (archived-trees (plist-get parameters :archived-trees))
1082 (inhibit-read-only t)
1083 (drawers org-drawers)
1084 (outline-regexp "\\*+ ")
1085 (source-buffer (current-buffer))
1086 target-alist rtn)
1088 (setq org-export-target-aliases nil
1089 org-export-preferred-target-alist nil
1090 org-export-id-target-alist nil
1091 org-export-code-refs nil)
1093 (with-current-buffer (get-buffer-create " org-mode-tmp")
1094 (erase-buffer)
1095 (insert string)
1096 (setq case-fold-search t)
1098 (let ((inhibit-read-only t))
1099 (remove-text-properties (point-min) (point-max)
1100 '(read-only t)))
1102 ;; Remove license-to-kill stuff
1103 ;; The caller marks some stuff for killing, stuff that has been
1104 ;; used to create the page title, for example.
1105 (org-export-kill-licensed-text)
1107 (let ((org-inhibit-startup t)) (org-mode))
1108 (setq case-fold-search t)
1109 (org-clone-local-variables source-buffer "^\\(org-\\|orgtbl-\\)")
1110 (org-install-letbind)
1112 ;; Call the hook
1113 (run-hooks 'org-export-preprocess-hook)
1115 (untabify (point-min) (point-max))
1117 ;; Handle include files, and call a hook
1118 (org-export-handle-include-files-recurse)
1119 (run-hooks 'org-export-preprocess-after-include-files-hook)
1121 ;; Change lists ending. Other parts of export may insert blank
1122 ;; lines and lists' structure could be altered.
1123 (org-export-mark-list-end)
1125 ;; Process the macros
1126 (org-export-preprocess-apply-macros)
1127 (run-hooks 'org-export-preprocess-after-macros-hook)
1129 ;; Get rid of archived trees
1130 (org-export-remove-archived-trees archived-trees)
1132 ;; Remove comment environment and comment subtrees
1133 (org-export-remove-comment-blocks-and-subtrees)
1135 ;; Get rid of excluded trees, and call a hook
1136 (org-export-handle-export-tags (plist-get parameters :select-tags)
1137 (plist-get parameters :exclude-tags))
1138 (run-hooks 'org-export-preprocess-after-tree-selection-hook)
1140 ;; Get rid of tasks, depending on configuration
1141 (org-export-remove-tasks (plist-get parameters :tasks))
1143 ;; Export code blocks
1144 (org-export-blocks-preprocess)
1146 ;; Mark lists with properties
1147 (org-export-mark-list-properties)
1149 ;; Handle source code snippets
1150 (org-export-replace-src-segments-and-examples)
1152 ;; Protect short examples marked by a leading colon
1153 (org-export-protect-colon-examples)
1155 ;; Protected spaces
1156 (org-export-convert-protected-spaces)
1158 ;; Normalize footnotes
1159 (when (plist-get parameters :footnotes)
1160 (org-footnote-normalize nil 'pre-process-p))
1162 ;; Find all headings and compute the targets for them
1163 (setq target-alist (org-export-define-heading-targets target-alist))
1165 (run-hooks 'org-export-preprocess-after-headline-targets-hook)
1167 ;; Find HTML special classes for headlines
1168 (org-export-remember-html-container-classes)
1170 ;; Get rid of drawers
1171 (org-export-remove-or-extract-drawers
1172 drawers (plist-get parameters :drawers))
1174 ;; Get the correct stuff before the first headline
1175 (when (plist-get parameters :skip-before-1st-heading)
1176 (goto-char (point-min))
1177 (when (re-search-forward "^\\(#.*\n\\)?\\*+[ \t]" nil t)
1178 (delete-region (point-min) (match-beginning 0))
1179 (goto-char (point-min))
1180 (insert "\n")))
1181 (when (plist-get parameters :add-text)
1182 (goto-char (point-min))
1183 (insert (plist-get parameters :add-text) "\n"))
1185 ;; Remove todo-keywords before exporting, if the user has requested so
1186 (org-export-remove-headline-metadata parameters)
1188 ;; Find targets in comments and move them out of comments,
1189 ;; but mark them as targets that should be invisible
1190 (setq target-alist (org-export-handle-invisible-targets target-alist))
1192 ;; Select and protect backend specific stuff, throw away stuff
1193 ;; that is specific for other backends
1194 (run-hooks 'org-export-preprocess-before-selecting-backend-code-hook)
1195 (org-export-select-backend-specific-text)
1197 ;; Protect quoted subtrees
1198 (org-export-protect-quoted-subtrees)
1200 ;; Remove clock lines
1201 (org-export-remove-clock-lines)
1203 ;; Protect verbatim elements
1204 (org-export-protect-verbatim)
1206 ;; Blockquotes, verse, and center
1207 (org-export-mark-blockquote-verse-center)
1208 (run-hooks 'org-export-preprocess-after-blockquote-hook)
1210 ;; Remove timestamps, if the user has requested so
1211 (unless (plist-get parameters :timestamps)
1212 (org-export-remove-timestamps))
1214 ;; Attach captions to the correct object
1215 (setq target-alist (org-export-attach-captions-and-attributes target-alist))
1217 ;; Find matches for radio targets and turn them into internal links
1218 (org-export-mark-radio-links)
1219 (run-hooks 'org-export-preprocess-after-radio-targets-hook)
1221 ;; Find all links that contain a newline and put them into a single line
1222 (org-export-concatenate-multiline-links)
1224 ;; Normalize links: Convert angle and plain links into bracket links
1225 ;; and expand link abbreviations
1226 (run-hooks 'org-export-preprocess-before-normalizing-links-hook)
1227 (org-export-normalize-links)
1229 ;; Find all internal links. If they have a fuzzy match (i.e. not
1230 ;; a *dedicated* target match, let the link point to the
1231 ;; corresponding section.
1232 (org-export-target-internal-links target-alist)
1234 ;; Find multiline emphasis and put them into single line
1235 (when (plist-get parameters :emph-multiline)
1236 (org-export-concatenate-multiline-emphasis))
1238 ;; Remove special table lines, and store alignment information
1239 (org-store-forced-table-alignment)
1240 (when org-export-table-remove-special-lines
1241 (org-export-remove-special-table-lines))
1243 ;; Another hook
1244 (run-hooks 'org-export-preprocess-before-backend-specifics-hook)
1246 ;; Backend-specific preprocessing
1247 (let* ((backend-name (symbol-name org-export-current-backend))
1248 (f (intern (format "org-export-%s-preprocess" backend-name))))
1249 (require (intern (concat "org-" backend-name)) nil)
1250 (funcall f parameters))
1252 ;; Remove or replace comments
1253 (org-export-handle-comments (plist-get parameters :comments))
1255 ;; Remove #+TBLFM and #+TBLNAME lines
1256 (org-export-handle-table-metalines)
1258 ;; Run the final hook
1259 (run-hooks 'org-export-preprocess-final-hook)
1261 (setq rtn (buffer-string)))
1262 (kill-buffer " org-mode-tmp")
1263 rtn))
1265 (defun org-export-kill-licensed-text ()
1266 "Remove all text that is marked with a :org-license-to-kill property."
1267 (let (p)
1268 (while (setq p (text-property-any (point-min) (point-max)
1269 :org-license-to-kill t))
1270 (delete-region
1271 p (or (next-single-property-change p :org-license-to-kill)
1272 (point-max))))))
1274 (defvar org-export-define-heading-targets-headline-hook nil
1275 "Hook that is run when a headline was matched during target search.
1276 This is part of the preprocessing for export.")
1278 (defun org-export-define-heading-targets (target-alist)
1279 "Find all headings and define the targets for them.
1280 The new targets are added to TARGET-ALIST, which is also returned.
1281 Also find all ID and CUSTOM_ID properties and store them."
1282 (goto-char (point-min))
1283 (org-init-section-numbers)
1284 (let ((re (concat "^" org-outline-regexp
1285 "\\|"
1286 "^[ \t]*:\\(ID\\|CUSTOM_ID\\):[ \t]*\\([^ \t\r\n]+\\)"))
1287 level target last-section-target a id)
1288 (while (re-search-forward re nil t)
1289 (org-if-unprotected-at (match-beginning 0)
1290 (if (match-end 2)
1291 (progn
1292 (setq id (org-match-string-no-properties 2))
1293 (push (cons id target) target-alist)
1294 (setq a (or (assoc last-section-target org-export-target-aliases)
1295 (progn
1296 (push (list last-section-target)
1297 org-export-target-aliases)
1298 (car org-export-target-aliases))))
1299 (push (caar target-alist) (cdr a))
1300 (when (equal (match-string 1) "CUSTOM_ID")
1301 (if (not (assoc last-section-target
1302 org-export-preferred-target-alist))
1303 (push (cons last-section-target id)
1304 org-export-preferred-target-alist)))
1305 (when (equal (match-string 1) "ID")
1306 (if (not (assoc last-section-target
1307 org-export-id-target-alist))
1308 (push (cons last-section-target (concat "ID-" id))
1309 org-export-id-target-alist))))
1310 (setq level (org-reduced-level
1311 (save-excursion (goto-char (point-at-bol))
1312 (org-outline-level))))
1313 (setq target (org-solidify-link-text
1314 (format "sec-%s" (replace-regexp-in-string
1315 "\\." "-"
1316 (org-section-number level)))))
1317 (setq last-section-target target)
1318 (push (cons target target) target-alist)
1319 (add-text-properties
1320 (point-at-bol) (point-at-eol)
1321 (list 'target target))
1322 (run-hooks 'org-export-define-heading-targets-headline-hook)))))
1323 target-alist)
1325 (defun org-export-handle-invisible-targets (target-alist)
1326 "Find targets in comments and move them out of comments.
1327 Mark them as invisible targets."
1328 (let (target tmp a)
1329 (goto-char (point-min))
1330 (while (re-search-forward "^#.*?\\(<<<?\\([^>\r\n]+\\)>>>?\\).*" nil t)
1331 ;; Check if the line before or after is a headline with a target
1332 (if (setq target (or (get-text-property (point-at-bol 0) 'target)
1333 (get-text-property (point-at-bol 2) 'target)))
1334 (progn
1335 ;; use the existing target in a neighboring line
1336 (setq tmp (match-string 2))
1337 (replace-match "")
1338 (and (looking-at "\n") (delete-char 1))
1339 (push (cons (setq tmp (org-solidify-link-text tmp)) target)
1340 target-alist)
1341 (setq a (or (assoc target org-export-target-aliases)
1342 (progn
1343 (push (list target) org-export-target-aliases)
1344 (car org-export-target-aliases))))
1345 (push tmp (cdr a)))
1346 ;; Make an invisible target
1347 (replace-match "\\1(INVISIBLE)"))))
1348 target-alist)
1350 (defun org-export-target-internal-links (target-alist)
1351 "Find all internal links and assign targets to them.
1352 If a link has a fuzzy match (i.e. not a *dedicated* target match),
1353 let the link point to the corresponding section.
1354 This function also handles the id links, if they have a match in
1355 the current file."
1356 (goto-char (point-min))
1357 (while (re-search-forward org-bracket-link-regexp nil t)
1358 (org-if-unprotected-at (1+ (match-beginning 0))
1359 (let* ((org-link-search-must-match-exact-headline t)
1360 (md (match-data))
1361 (desc (match-end 2))
1362 (link (org-link-unescape (match-string 1)))
1363 (slink (org-solidify-link-text link))
1364 found props pos cref
1365 (target
1366 (cond
1367 ((= (string-to-char link) ?#)
1368 ;; user wants exactly this link
1369 link)
1370 ((cdr (assoc slink target-alist))
1371 (or (cdr (assoc (assoc slink target-alist)
1372 org-export-preferred-target-alist))
1373 (cdr (assoc slink target-alist))))
1374 ((and (string-match "^id:" link)
1375 (cdr (assoc (substring link 3) target-alist))))
1376 ((string-match "^(\\(.*\\))$" link)
1377 (setq cref (match-string 1 link))
1378 (concat "coderef:" cref))
1379 ((string-match org-link-types-re link) nil)
1380 ((or (file-name-absolute-p link)
1381 (string-match "^\\." link))
1382 nil)
1384 (let ((org-link-search-inhibit-query t))
1385 (save-excursion
1386 (setq found (condition-case nil (org-link-search link)
1387 (error nil)))
1388 (when (and found
1389 (or (org-on-heading-p)
1390 (not (eq found 'dedicated))))
1391 (or (get-text-property (point) 'target)
1392 (get-text-property
1393 (max (point-min)
1394 (1- (or (previous-single-property-change
1395 (point) 'target) 0)))
1396 'target)))))))))
1397 (when target
1398 (set-match-data md)
1399 (goto-char (match-beginning 1))
1400 (setq props (text-properties-at (point)))
1401 (delete-region (match-beginning 1) (match-end 1))
1402 (setq pos (point))
1403 (insert target)
1404 (unless desc (insert "][" link))
1405 (add-text-properties pos (point) props))))))
1407 (defun org-export-remember-html-container-classes ()
1408 "Store the HTML_CONTAINER_CLASS properties in a text property."
1409 (goto-char (point-min))
1410 (let (class)
1411 (while (re-search-forward
1412 "^[ \t]*:HTML_CONTAINER_CLASS:[ \t]+\\(.+\\)$" nil t)
1413 (setq class (match-string 1))
1414 (save-excursion
1415 (org-back-to-heading t)
1416 (put-text-property (point-at-bol) (point-at-eol) 'html-container-class class)))))
1418 (defvar org-export-format-drawer-function nil
1419 "Function to be called to format the contents of a drawer.
1420 The function must accept three parameters:
1421 NAME the drawer name, like \"PROPERTIES\"
1422 CONTENT the content of the drawer.
1423 You can check the export backend through `org-export-current-backend'.
1424 The function should return the text to be inserted into the buffer.
1425 If this is nil, `org-export-format-drawer' is used as a default.")
1427 (defun org-export-remove-or-extract-drawers (all-drawers exp-drawers)
1428 "Remove drawers, or extract and format the content.
1429 ALL-DRAWERS is a list of all drawer names valid in the current buffer.
1430 EXP-DRAWERS can be t to keep all drawer contents, or a list of drawers
1431 whose content to keep. Any drawers that are in ALL-DRAWERS but not in
1432 EXP-DRAWERS will be removed."
1433 (goto-char (point-min))
1434 (let ((re (concat "^[ \t]*:\\("
1435 (mapconcat 'identity all-drawers "\\|")
1436 "\\):[ \t]*$"))
1437 name beg beg-content eol content)
1438 (while (re-search-forward re nil t)
1439 (org-if-unprotected
1440 (setq name (match-string 1))
1441 (setq beg (match-beginning 0)
1442 beg-content (1+ (point-at-eol))
1443 eol (point-at-eol))
1444 (if (not (and (re-search-forward
1445 "^\\([ \t]*:END:[ \t]*\n?\\)\\|^\\*+[ \t]" nil t)
1446 (match-end 1)))
1447 (goto-char eol)
1448 (goto-char (match-beginning 0))
1449 (and (looking-at ".*\n?") (replace-match ""))
1450 (setq content (buffer-substring beg-content (point)))
1451 (delete-region beg (point))
1452 (when (or (eq exp-drawers t)
1453 (member name exp-drawers))
1454 (setq content (funcall (or org-export-format-drawer-function
1455 'org-export-format-drawer)
1456 name content))
1457 (insert content)))))))
1459 (defun org-export-format-drawer (name content)
1460 "Format the content of a drawer as a colon example."
1461 (if (string-match "[ \t]+\\'" content)
1462 (setq content (substring content (match-beginning 0))))
1463 (while (string-match "\\`[ \t]*\n" content)
1464 (setq content (substring content (match-end 0))))
1465 (setq content (org-remove-indentation content))
1466 (setq content (concat ": " (mapconcat 'identity
1467 (org-split-string content "\n")
1468 "\n: ")
1469 "\n"))
1470 (setq content (concat " : " (upcase name) "\n" content))
1471 (org-add-props content nil 'org-protected t))
1473 (defun org-export-handle-export-tags (select-tags exclude-tags)
1474 "Modify the buffer, honoring SELECT-TAGS and EXCLUDE-TAGS.
1475 Both arguments are lists of tags.
1476 If any of SELECT-TAGS is found, all trees not marked by a SELECT-TAG
1477 will be removed.
1478 After that, all subtrees that are marked by EXCLUDE-TAGS will be
1479 removed as well."
1480 (remove-text-properties (point-min) (point-max) '(:org-delete t))
1481 (let* ((re-sel (concat ":\\(" (mapconcat 'regexp-quote
1482 select-tags "\\|")
1483 "\\):"))
1484 (re-excl (concat ":\\(" (mapconcat 'regexp-quote
1485 exclude-tags "\\|")
1486 "\\):"))
1487 beg end cont)
1488 (goto-char (point-min))
1489 (when (and select-tags
1490 (re-search-forward
1491 (concat "^\\*+[ \t].*" re-sel "[^ \t\n]*[ \t]*$") nil t))
1492 ;; At least one tree is marked for export, this means
1493 ;; all the unmarked stuff needs to go.
1494 ;; Dig out the trees that should be exported
1495 (goto-char (point-min))
1496 (outline-next-heading)
1497 (setq beg (point))
1498 (put-text-property beg (point-max) :org-delete t)
1499 (while (re-search-forward re-sel nil t)
1500 (when (org-on-heading-p)
1501 (org-back-to-heading)
1502 (remove-text-properties
1503 (max (1- (point)) (point-min))
1504 (setq cont (save-excursion (org-end-of-subtree t t)))
1505 '(:org-delete t))
1506 (while (and (org-up-heading-safe)
1507 (get-text-property (point) :org-delete))
1508 (remove-text-properties (max (1- (point)) (point-min))
1509 (point-at-eol) '(:org-delete t)))
1510 (goto-char cont))))
1511 ;; Remove the trees explicitly marked for noexport
1512 (when exclude-tags
1513 (goto-char (point-min))
1514 (while (re-search-forward re-excl nil t)
1515 (when (org-at-heading-p)
1516 (org-back-to-heading t)
1517 (setq beg (point))
1518 (org-end-of-subtree t t)
1519 (delete-region beg (point))
1520 (when (featurep 'org-inlinetask)
1521 (org-inlinetask-remove-END-maybe)))))
1522 ;; Remove everything that is now still marked for deletion
1523 (goto-char (point-min))
1524 (while (setq beg (text-property-any (point-min) (point-max) :org-delete t))
1525 (setq end (or (next-single-property-change beg :org-delete)
1526 (point-max)))
1527 (delete-region beg end))))
1529 (defun org-export-remove-tasks (keep)
1530 "Remove tasks depending on configuration.
1531 When KEEP is nil, remove all tasks.
1532 When KEEP is `todo', remove the tasks that are DONE.
1533 When KEEP is `done', remove the tasks that are not yet done.
1534 When it is a list of strings, keep only tasks with these TODO keywords."
1535 (when (or (listp keep) (memq keep '(todo done nil)))
1536 (let ((re (concat "^\\*+[ \t]+\\("
1537 (mapconcat
1538 'regexp-quote
1539 (cond ((not keep) org-todo-keywords-1)
1540 ((eq keep 'todo) org-done-keywords)
1541 ((eq keep 'done) org-not-done-keywords)
1542 ((listp keep)
1543 (org-delete-all keep (copy-sequence
1544 org-todo-keywords-1))))
1545 "\\|")
1546 "\\)\\($\\|[ \t]\\)"))
1547 (case-fold-search nil)
1548 beg)
1549 (goto-char (point-min))
1550 (while (re-search-forward re nil t)
1551 (org-if-unprotected
1552 (setq beg (match-beginning 0))
1553 (org-end-of-subtree t t)
1554 (if (looking-at "^\\*+[ \t]+END[ \t]*$")
1555 ;; Kill the END line of the inline task
1556 (goto-char (min (point-max) (1+ (match-end 0)))))
1557 (delete-region beg (point)))))))
1559 (defun org-export-remove-archived-trees (export-archived-trees)
1560 "Remove archived trees.
1561 When EXPORT-ARCHIVED-TREES is `headline;, only the headline will be exported.
1562 When it is t, the entire archived tree will be exported.
1563 When it is nil the entire tree including the headline will be removed
1564 from the buffer."
1565 (let ((re-archive (concat ":" org-archive-tag ":"))
1566 a b)
1567 (when (not (eq export-archived-trees t))
1568 (goto-char (point-min))
1569 (while (re-search-forward re-archive nil t)
1570 (if (not (org-on-heading-p t))
1571 (goto-char (point-at-eol))
1572 (beginning-of-line 1)
1573 (setq a (if export-archived-trees
1574 (1+ (point-at-eol)) (point))
1575 b (org-end-of-subtree t))
1576 (if (> b a) (delete-region a b)))))))
1578 (defun org-export-remove-headline-metadata (opts)
1579 "Remove meta data from the headline, according to user options."
1580 (let ((re org-complex-heading-regexp)
1581 (todo (plist-get opts :todo-keywords))
1582 (tags (plist-get opts :tags))
1583 (pri (plist-get opts :priority))
1584 (elts '(1 2 3 4 5))
1585 (case-fold-search nil)
1586 rpl)
1587 (setq elts (delq nil (list 1 (if todo 2) (if pri 3) 4 (if tags 5))))
1588 (when (or (not todo) (not tags) (not pri))
1589 (goto-char (point-min))
1590 (while (re-search-forward re nil t)
1591 (org-if-unprotected
1592 (setq rpl (mapconcat (lambda (i) (if (match-end i) (match-string i) ""))
1593 elts " "))
1594 (replace-match rpl t t))))))
1596 (defun org-export-remove-timestamps ()
1597 "Remove timestamps and keywords for export."
1598 (goto-char (point-min))
1599 (while (re-search-forward org-maybe-keyword-time-regexp nil t)
1600 (backward-char 1)
1601 (org-if-unprotected
1602 (unless (save-match-data (org-at-table-p))
1603 (replace-match "")
1604 (beginning-of-line 1)
1605 (if (looking-at "[- \t]*\\(=>[- \t0-9:]*\\)?[ \t]*\n")
1606 (replace-match ""))))))
1608 (defun org-export-remove-clock-lines ()
1609 "Remove clock lines for export."
1610 (goto-char (point-min))
1611 (let ((re (concat "^[ \t]*" org-clock-string ".*\n?")))
1612 (while (re-search-forward re nil t)
1613 (org-if-unprotected
1614 (replace-match "")))))
1616 (defun org-export-protect-quoted-subtrees ()
1617 "Mark quoted subtrees with the protection property."
1618 (let ((re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>")))
1619 (goto-char (point-min))
1620 (while (re-search-forward re-quote nil t)
1621 (goto-char (match-beginning 0))
1622 (end-of-line 1)
1623 (add-text-properties (point) (org-end-of-subtree t)
1624 '(org-protected t)))))
1626 (defun org-export-convert-protected-spaces ()
1627 "Convert strings like \\____ to protected spaces in all backends."
1628 (goto-char (point-min))
1629 (while (re-search-forward "\\\\__+" nil t)
1630 (org-if-unprotected-1
1631 (replace-match
1632 (org-add-props
1633 (cond
1634 ((eq org-export-current-backend 'latex)
1635 (format "\\hspace{%dex}" (- (match-end 0) (match-beginning 0))))
1636 ((eq org-export-current-backend 'html)
1637 (org-add-props (match-string 0) nil
1638 'org-whitespace (- (match-end 0) (match-beginning 0))))
1639 ;; ((eq org-export-current-backend 'docbook))
1640 ((eq org-export-current-backend 'ascii)
1641 (org-add-props (match-string 0) '(org-whitespace t)))
1642 (t (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
1643 '(org-protected t))
1644 t t))))
1646 (defun org-export-protect-verbatim ()
1647 "Mark verbatim snippets with the protection property."
1648 (goto-char (point-min))
1649 (while (re-search-forward org-verbatim-re nil t)
1650 (org-if-unprotected
1651 (add-text-properties (match-beginning 4) (match-end 4)
1652 '(org-protected t org-verbatim-emph t))
1653 (goto-char (1+ (match-end 4))))))
1655 (defun org-export-protect-colon-examples ()
1656 "Protect lines starting with a colon."
1657 (goto-char (point-min))
1658 (let ((re "^[ \t]*:\\([ \t]\\|$\\)") beg)
1659 (while (re-search-forward re nil t)
1660 (beginning-of-line 1)
1661 (setq beg (point))
1662 (while (looking-at re)
1663 (end-of-line 1)
1664 (or (eobp) (forward-char 1)))
1665 (add-text-properties beg (if (bolp) (1- (point)) (point))
1666 '(org-protected t)))))
1668 (defvar org-export-backends
1669 '(docbook html beamer ascii latex)
1670 "List of Org supported export backends.")
1672 (defun org-export-select-backend-specific-text ()
1673 (let ((formatters org-export-backends)
1674 (case-fold-search t)
1675 backend backend-name beg beg-content end end-content ind)
1677 (while formatters
1678 (setq backend (pop formatters)
1679 backend-name (symbol-name backend))
1681 ;; Handle #+BACKEND: stuff
1682 (goto-char (point-min))
1683 (while (re-search-forward (concat "^\\([ \t]*\\)#\\+" backend-name
1684 ":[ \t]*\\(.*\\)") nil t)
1685 (if (not (eq backend org-export-current-backend))
1686 (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
1687 (replace-match "\\1\\2" t)
1688 (add-text-properties
1689 (point-at-bol) (min (1+ (point-at-eol)) (point-max))
1690 `(org-protected t original-indentation ,ind org-native-text t))))
1691 ;; Delete #+ATTR_BACKEND: stuff of another backend. Those
1692 ;; matching the current backend will be taken care of by
1693 ;; `org-export-attach-captions-and-attributes'
1694 (goto-char (point-min))
1695 (while (re-search-forward (concat "^\\([ \t]*\\)#\\+ATTR_" backend-name
1696 ":[ \t]*\\(.*\\)") nil t)
1697 (setq ind (org-get-indentation))
1698 (when (not (eq backend org-export-current-backend))
1699 (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))))
1700 ;; Handle #+BEGIN_BACKEND and #+END_BACKEND stuff
1701 (goto-char (point-min))
1702 (while (re-search-forward (concat "^[ \t]*#\\+BEGIN_" backend-name "\\>.*\n?")
1703 nil t)
1704 (setq beg (match-beginning 0) beg-content (match-end 0))
1705 (setq ind (save-excursion (goto-char beg) (org-get-indentation)))
1706 (when (re-search-forward (concat "^[ \t]*#\\+END_" backend-name "\\>.*\n?")
1707 nil t)
1708 (setq end (match-end 0) end-content (match-beginning 0))
1709 (if (eq backend org-export-current-backend)
1710 ;; yes, keep this
1711 (progn
1712 (add-text-properties
1713 beg-content end-content
1714 `(org-protected t original-indentation ,ind org-native-text t))
1715 ;; strip protective commas
1716 (save-excursion
1717 (save-match-data
1718 (goto-char beg-content)
1719 (while (re-search-forward "^[ \t]*\\(,\\)" end-content t)
1720 (replace-match "" nil nil nil 1))))
1721 (delete-region (match-beginning 0) (match-end 0))
1722 (save-excursion
1723 (goto-char beg)
1724 (delete-region (point) (1+ (point-at-eol)))))
1725 ;; No, this is for a different backend, kill it
1726 (delete-region beg end)))))))
1728 (defun org-export-mark-blockquote-verse-center ()
1729 "Mark block quote and verse environments with special cookies.
1730 These special cookies will later be interpreted by the backend."
1731 ;; Blockquotes
1732 (let (type t1 ind beg end beg1 end1 content)
1733 (goto-char (point-min))
1734 (while (re-search-forward
1735 "^\\([ \t]*\\)#\\+\\(begin_\\(\\(block\\)?quote\\|verse\\|center\\)\\>.*\\)"
1736 nil t)
1737 (setq ind (length (match-string 1))
1738 type (downcase (match-string 3))
1739 t1 (if (equal type "quote") "blockquote" type))
1740 (setq beg (match-beginning 0)
1741 beg1 (1+ (match-end 0)))
1742 (when (re-search-forward (concat "^[ \t]*#\\+end_" type "\\>.*") nil t)
1743 (setq end1 (1- (match-beginning 0))
1744 end (+ (point-at-eol) (if (looking-at "\n$") 1 0)))
1745 (setq content (org-remove-indentation (buffer-substring beg1 end1)))
1746 (setq content (concat "ORG-" (upcase t1) "-START\n"
1747 content "\n"
1748 "ORG-" (upcase t1) "-END\n"))
1749 (delete-region beg end)
1750 (insert (org-add-props content nil 'original-indentation ind))))))
1752 (defun org-export-mark-list-end ()
1753 "Mark all list endings with a special string."
1754 (unless (eq org-export-current-backend 'ascii)
1755 (mapc
1756 (lambda (e)
1757 ;; For each type allowing list export, find every list, remove
1758 ;; ending regexp if needed, and insert org-list-end.
1759 (goto-char (point-min))
1760 (while (re-search-forward (org-item-beginning-re) nil t)
1761 (when (eq (nth 2 (org-list-context)) e)
1762 (let* ((struct (org-list-struct))
1763 (bottom (org-list-get-bottom-point struct))
1764 (top (point-at-bol))
1765 (top-ind (org-list-get-ind top struct)))
1766 (goto-char bottom)
1767 (when (and (not (eq org-list-ending-method 'indent))
1768 (looking-at org-list-end-re))
1769 (replace-match ""))
1770 (unless (bolp) (insert "\n"))
1771 ;; As org-list-end is inserted at column 0, it would end
1772 ;; by indentation any list. It can be problematic when
1773 ;; there are lists within lists: the inner list end would
1774 ;; also become the outer list end. To avoid this, text
1775 ;; property `original-indentation' is added, as
1776 ;; `org-list-struct' pays attention to it when reading a
1777 ;; list.
1778 (insert (org-add-props
1779 "ORG-LIST-END-MARKER\n"
1780 (list 'original-indentation top-ind)))))))
1781 (cons nil org-list-export-context))))
1783 (defun org-export-mark-list-properties ()
1784 "Mark list with special properties.
1785 These special properties will later be interpreted by the backend."
1786 (let ((mark-list
1787 (function
1788 ;; Mark a list with 3 properties: `list-item' which is
1789 ;; position at beginning of line, `list-struct' which is
1790 ;; list structure, and `list-prevs' which is the alist of
1791 ;; item and its predecessor. Leave point at list ending.
1792 (lambda (ctxt)
1793 (let* ((struct (org-list-struct))
1794 (top (org-list-get-top-point struct))
1795 (bottom (org-list-get-bottom-point struct))
1796 (prevs (org-list-prevs-alist struct))
1797 poi)
1798 ;; Get every item and ending position, without dups and
1799 ;; without bottom point of list.
1800 (mapc (lambda (e)
1801 (let ((pos (car e))
1802 (end (nth 6 e)))
1803 (unless (memq pos poi)
1804 (push pos poi))
1805 (unless (or (= end bottom) (memq end poi))
1806 (push end poi))))
1807 struct)
1808 (setq poi (sort poi '<))
1809 ;; For every point of interest, mark the whole line with
1810 ;; its position in list.
1811 (mapc
1812 (lambda (e)
1813 (goto-char e)
1814 (add-text-properties (point-at-bol) (point-at-eol)
1815 (list 'list-item (point-at-bol)
1816 'list-struct struct
1817 'list-prevs prevs)))
1818 poi)
1819 ;; Take care of bottom point. As babel may have inserted
1820 ;; a new list in buffer, list ending isn't always
1821 ;; marked. Now mark every list ending and add properties
1822 ;; useful to line processing exporters.
1823 (goto-char bottom)
1824 (when (or (looking-at "^ORG-LIST-END-MARKER\n")
1825 (and (not (eq org-list-ending-method 'indent))
1826 (looking-at org-list-end-re)))
1827 (replace-match ""))
1828 (unless (bolp) (insert "\n"))
1829 (insert
1830 (org-add-props "ORG-LIST-END-MARKER\n" (list 'list-item bottom
1831 'list-struct struct
1832 'list-prevs prevs)))
1833 ;; Following property is used by LaTeX exporter.
1834 (add-text-properties top (point) (list 'list-context ctxt)))))))
1835 ;; Mark lists except for backends not interpreting them.
1836 (unless (eq org-export-current-backend 'ascii)
1837 (let ((org-list-end-re "^ORG-LIST-END-MARKER\n"))
1838 (mapc
1839 (lambda (e)
1840 (goto-char (point-min))
1841 (while (re-search-forward (org-item-beginning-re) nil t)
1842 (let ((context (nth 2 (org-list-context))))
1843 (if (eq context e)
1844 (funcall mark-list e)
1845 (put-text-property (point-at-bol) (point-at-eol)
1846 'list-context context)))))
1847 (cons nil org-list-export-context))))))
1849 (defun org-export-attach-captions-and-attributes (target-alist)
1850 "Move #+CAPTION, #+ATTR_BACKEND, and #+LABEL text into text properties.
1851 If the next thing following is a table, add the text properties to the first
1852 table line. If it is a link, add it to the line containing the link."
1853 (goto-char (point-min))
1854 (remove-text-properties (point-min) (point-max)
1855 '(org-caption nil org-attributes nil))
1856 (let ((case-fold-search t)
1857 (re (concat "^[ \t]*#\\+caption:[ \t]+\\(.*\\)"
1858 "\\|"
1859 "^[ \t]*#\\+attr_" (symbol-name org-export-current-backend) ":[ \t]+\\(.*\\)"
1860 "\\|"
1861 "^[ \t]*#\\+label:[ \t]+\\(.*\\)"
1862 "\\|"
1863 "^[ \t]*\\(|[^-]\\)"
1864 "\\|"
1865 "^[ \t]*\\[\\[.*\\]\\][ \t]*$"))
1866 cap shortn attr label end)
1867 (while (re-search-forward re nil t)
1868 (cond
1869 ;; there is a caption
1870 ((match-end 1)
1871 (progn
1872 (setq cap (concat cap (if cap " " "") (org-trim (match-string 1))))
1873 (when (string-match "\\[\\(.*\\)\\]{\\(.*\\)}" cap)
1874 (setq shortn (match-string 1 cap)
1875 cap (match-string 2 cap)))
1876 (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))))
1877 ;; there is an attribute
1878 ((match-end 2)
1879 (progn
1880 (setq attr (concat attr (if attr " " "") (org-trim (match-string 2))))
1881 (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))))
1882 ;; there is a label
1883 ((match-end 3)
1884 (progn
1885 (setq label (org-trim (match-string 3)))
1886 (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))))
1888 (setq end (if (match-end 4)
1889 (let ((ee (org-table-end)))
1890 (prog1 (1- (marker-position ee)) (move-marker ee nil)))
1891 (point-at-eol)))
1892 (add-text-properties (point-at-bol) end
1893 (list 'org-caption cap
1894 'org-caption-shortn shortn
1895 'org-attributes attr
1896 'org-label label))
1897 (if label (push (cons label label) target-alist))
1898 (goto-char end)
1899 (setq cap nil shortn nil attr nil label nil)))))
1900 target-alist)
1902 (defun org-export-remove-comment-blocks-and-subtrees ()
1903 "Remove the comment environment, and also commented subtrees."
1904 (let ((re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
1905 case-fold-search)
1906 ;; Remove comment environment
1907 (goto-char (point-min))
1908 (setq case-fold-search t)
1909 (while (re-search-forward
1910 "^#\\+begin_comment[ \t]*\n[^\000]*?\n#\\+end_comment\\>.*" nil t)
1911 (replace-match "" t t))
1912 ;; Remove subtrees that are commented
1913 (goto-char (point-min))
1914 (setq case-fold-search nil)
1915 (while (re-search-forward re-commented nil t)
1916 (goto-char (match-beginning 0))
1917 (delete-region (point) (org-end-of-subtree t)))))
1919 (defun org-export-handle-comments (commentsp)
1920 "Remove comments, or convert to backend-specific format.
1921 COMMENTSP can be a format string for publishing comments.
1922 When it is nil, all comments will be removed."
1923 (let ((re "^\\(#\\|[ \t]*#\\+ \\)\\(.*\n?\\)")
1924 pos)
1925 (goto-char (point-min))
1926 (while (or (looking-at re)
1927 (re-search-forward re nil t))
1928 (setq pos (match-beginning 0))
1929 (if (get-text-property pos 'org-protected)
1930 (goto-char (1+ pos))
1931 (if (and commentsp
1932 (not (equal (char-before (match-end 1)) ?+)))
1933 (progn (add-text-properties
1934 (match-beginning 0) (match-end 0) '(org-protected t))
1935 (replace-match (org-add-props
1936 (format commentsp (match-string 2))
1937 nil 'org-protected t)
1938 t t))
1939 (goto-char (1+ pos))
1940 (replace-match "")
1941 (goto-char (max (point-min) (1- pos))))))))
1943 (defun org-export-handle-table-metalines ()
1944 "Remove table specific metalines #+TBLNAME: and #+TBLFM:."
1945 (let ((re "^[ \t]*#\\+TBL\\(NAME\\|FM\\):\\(.*\n?\\)")
1946 pos)
1947 (goto-char (point-min))
1948 (while (or (looking-at re)
1949 (re-search-forward re nil t))
1950 (setq pos (match-beginning 0))
1951 (if (get-text-property (match-beginning 1) 'org-protected)
1952 (goto-char (1+ pos))
1953 (goto-char (1+ pos))
1954 (replace-match "")
1955 (goto-char (max (point-min) (1- pos)))))))
1957 (defun org-export-mark-radio-links ()
1958 "Find all matches for radio targets and turn them into internal links."
1959 (let ((re-radio (and org-target-link-regexp
1960 (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)"))))
1961 (goto-char (point-min))
1962 (when re-radio
1963 (while (re-search-forward re-radio nil t)
1964 (unless
1965 (save-match-data
1966 (or (org-in-regexp org-bracket-link-regexp)
1967 (org-in-regexp org-plain-link-re)
1968 (org-in-regexp "<<[^<>]+>>")))
1969 (org-if-unprotected
1970 (replace-match "\\1[[\\2]]")))))))
1972 (defun org-store-forced-table-alignment ()
1973 "Find table lines which force alignment, store the results in properties."
1974 (let (line cnt aligns)
1975 (goto-char (point-min))
1976 (while (re-search-forward "|[ \t]*<[lrc][0-9]*>[ \t]*|" nil t)
1977 ;; OK, this looks like a table line with an alignment cookie
1978 (org-if-unprotected
1979 (setq line (buffer-substring (point-at-bol) (point-at-eol)))
1980 (when (and (org-at-table-p)
1981 (org-table-cookie-line-p line))
1982 (setq cnt 0 aligns nil)
1983 (mapc
1984 (lambda (x)
1985 (setq cnt (1+ cnt))
1986 (if (string-match "\\`<\\([lrc]\\)" x)
1987 (push (cons cnt (downcase (match-string 1 x))) aligns)))
1988 (org-split-string line "[ \t]*|[ \t]*"))
1989 (add-text-properties (org-table-begin) (org-table-end)
1990 (list 'org-forced-aligns aligns))))
1991 (goto-char (point-at-eol)))))
1993 (defun org-export-remove-special-table-lines ()
1994 "Remove tables lines that are used for internal purposes.
1995 Also, store forced alignment information found in such lines."
1996 (goto-char (point-min))
1997 (while (re-search-forward "^[ \t]*|" nil t)
1998 (org-if-unprotected-at (1- (point))
1999 (beginning-of-line 1)
2000 (if (or (looking-at "[ \t]*| *[!_^] *|")
2001 (not
2002 (memq
2004 (mapcar
2005 (lambda (f)
2006 (or (and org-export-table-remove-empty-lines (= (length f) 0))
2007 (string-match
2008 "\\`<\\([0-9]\\|[lrc]\\|[lrc][0-9]+\\)>\\'" f)))
2009 (org-split-string ;; FIXME, can't we do without splitting???
2010 (buffer-substring (point-at-bol) (point-at-eol))
2011 "[ \t]*|[ \t]*")))))
2012 (delete-region (max (point-min) (1- (point-at-bol)))
2013 (point-at-eol))
2014 (end-of-line 1)))))
2016 (defun org-export-protect-sub-super (s)
2017 (save-match-data
2018 (while (string-match "\\([^\\\\]\\)\\([_^]\\)" s)
2019 (setq s (replace-match "\\1\\\\\\2" nil nil s)))
2022 (defun org-export-normalize-links ()
2023 "Convert all links to bracket links, and expand link abbreviations."
2024 (let ((re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
2025 (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
2026 nodesc)
2027 (goto-char (point-min))
2028 (while (re-search-forward re-plain-link nil t)
2029 (unless (org-string-match-p
2030 "\\[\\[\\S-+:\\S-*?\\<"
2031 (buffer-substring (point-at-bol) (match-beginning 0)))
2032 (goto-char (1- (match-end 0)))
2033 (org-if-unprotected-at (1+ (match-beginning 0))
2034 (let* ((s (concat (match-string 1)
2035 "[[" (match-string 2) ":" (match-string 3)
2036 "][" (match-string 2) ":" (org-export-protect-sub-super
2037 (match-string 3))
2038 "]]")))
2039 ;; added 'org-link face to links
2040 (put-text-property 0 (length s) 'face 'org-link s)
2041 (replace-match s t t)))))
2042 (goto-char (point-min))
2043 (while (re-search-forward re-angle-link nil t)
2044 (goto-char (1- (match-end 0)))
2045 (org-if-unprotected
2046 (let* ((s (concat (match-string 1)
2047 "[[" (match-string 2) ":" (match-string 3)
2048 "][" (match-string 2) ":" (org-export-protect-sub-super
2049 (match-string 3))
2050 "]]")))
2051 (put-text-property 0 (length s) 'face 'org-link s)
2052 (replace-match s t t))))
2053 (goto-char (point-min))
2054 (while (re-search-forward org-bracket-link-regexp nil t)
2055 (goto-char (1- (match-end 0)))
2056 (setq nodesc (not (match-end 3)))
2057 (org-if-unprotected
2058 (let* ((xx (save-match-data
2059 (org-translate-link
2060 (org-link-expand-abbrev (match-string 1)))))
2061 (s (concat
2062 "[[" (org-add-props (copy-sequence xx)
2063 nil 'org-protected t 'org-no-description nodesc)
2065 (if (match-end 3)
2066 (match-string 2)
2067 (concat "[" (copy-sequence xx)
2068 "]"))
2069 "]")))
2070 (put-text-property 0 (length s) 'face 'org-link s)
2071 (replace-match s t t))))))
2073 (defun org-export-concatenate-multiline-links ()
2074 "Find multi-line links and put it all into a single line.
2075 This is to make sure that the line-processing export backends
2076 can work correctly."
2077 (goto-char (point-min))
2078 (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
2079 (org-if-unprotected-at (match-beginning 1)
2080 (replace-match "\\1 \\3")
2081 (goto-char (match-beginning 0)))))
2083 (defun org-export-concatenate-multiline-emphasis ()
2084 "Find multi-line emphasis and put it all into a single line.
2085 This is to make sure that the line-processing export backends
2086 can work correctly."
2087 (goto-char (point-min))
2088 (while (re-search-forward org-emph-re nil t)
2089 (if (and (not (= (char-after (match-beginning 3))
2090 (char-after (match-beginning 4))))
2091 (save-excursion (goto-char (match-beginning 0))
2092 (save-match-data
2093 (and (not (org-at-table-p))
2094 (not (org-at-heading-p))))))
2095 (org-if-unprotected
2096 (subst-char-in-region (match-beginning 0) (match-end 0)
2097 ?\n ?\ t)
2098 (goto-char (1- (match-end 0))))
2099 (goto-char (1+ (match-beginning 0))))))
2101 (defun org-export-grab-title-from-buffer ()
2102 "Get a title for the current document, from looking at the buffer."
2103 (let ((inhibit-read-only t))
2104 (save-excursion
2105 (goto-char (point-min))
2106 (let ((end (if (looking-at org-outline-regexp)
2107 (point)
2108 (save-excursion (outline-next-heading) (point)))))
2109 (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
2110 ;; Mark the line so that it will not be exported as normal text.
2111 (org-unmodified
2112 (add-text-properties (match-beginning 0) (match-end 0)
2113 (list :org-license-to-kill t)))
2114 ;; Return the title string
2115 (org-trim (match-string 0)))))))
2117 (defun org-export-get-title-from-subtree ()
2118 "Return subtree title and exclude it from export."
2119 (let ((rbeg (region-beginning)) (rend (region-end))
2120 (inhibit-read-only t) title)
2121 (save-excursion
2122 (goto-char rbeg)
2123 (when (and (org-at-heading-p)
2124 (>= (org-end-of-subtree t t) rend))
2125 ;; This is a subtree, we take the title from the first heading
2126 (goto-char rbeg)
2127 (looking-at org-todo-line-regexp)
2128 (setq title (match-string 3))
2129 (org-unmodified
2130 (add-text-properties (point) (1+ (point-at-eol))
2131 (list :org-license-to-kill t)))
2132 (setq title (or (org-entry-get nil "EXPORT_TITLE") title))))
2133 title))
2135 (defun org-solidify-link-text (s &optional alist)
2136 "Take link text and make a safe target out of it."
2137 (save-match-data
2138 (let* ((rtn
2139 (mapconcat
2140 'identity
2141 (org-split-string s "[^a-zA-Z0-9_\\.-]+") "-"))
2142 (a (assoc rtn alist)))
2143 (or (cdr a) rtn))))
2145 (defun org-get-min-level (lines &optional offset)
2146 "Get the minimum level in LINES."
2147 (let ((re "^\\(\\*+\\) ") l)
2148 (catch 'exit
2149 (while (setq l (pop lines))
2150 (if (string-match re l)
2151 (throw 'exit (org-tr-level (- (length (match-string 1 l))
2152 (or offset 0))))))
2153 1)))
2155 ;; Variable holding the vector with section numbers
2156 (defvar org-section-numbers (make-vector org-level-max 0))
2158 (defun org-init-section-numbers ()
2159 "Initialize the vector for the section numbers."
2160 (let* ((level -1)
2161 (numbers (nreverse (org-split-string "" "\\.")))
2162 (depth (1- (length org-section-numbers)))
2163 (i depth) number-string)
2164 (while (>= i 0)
2165 (if (> i level)
2166 (aset org-section-numbers i 0)
2167 (setq number-string (or (car numbers) "0"))
2168 (if (string-match "\\`[A-Z]\\'" number-string)
2169 (aset org-section-numbers i
2170 (- (string-to-char number-string) ?A -1))
2171 (aset org-section-numbers i (string-to-number number-string)))
2172 (pop numbers))
2173 (setq i (1- i)))))
2175 (defun org-section-number (&optional level)
2176 "Return a string with the current section number.
2177 When LEVEL is non-nil, increase section numbers on that level."
2178 (let* ((depth (1- (length org-section-numbers)))
2179 (string "")
2180 (fmts (car org-export-section-number-format))
2181 (term (cdr org-export-section-number-format))
2182 (sep "")
2183 ctype fmt idx n)
2184 (when level
2185 (when (> level -1)
2186 (aset org-section-numbers
2187 level (1+ (aref org-section-numbers level))))
2188 (setq idx (1+ level))
2189 (while (<= idx depth)
2190 (if (not (= idx 1))
2191 (aset org-section-numbers idx 0))
2192 (setq idx (1+ idx))))
2193 (setq idx 0)
2194 (while (<= idx depth)
2195 (when (> (aref org-section-numbers idx) 0)
2196 (setq fmt (or (pop fmts) fmt)
2197 ctype (car fmt)
2198 n (aref org-section-numbers idx)
2199 string (if (> n 0)
2200 (concat string sep (org-number-to-counter n ctype))
2201 (concat string ".0"))
2202 sep (nth 1 fmt)))
2203 (setq idx (1+ idx)))
2204 (save-match-data
2205 (if (string-match "\\`\\([@0]\\.\\)+" string)
2206 (setq string (replace-match "" t nil string)))
2207 (if (string-match "\\(\\.0\\)+\\'" string)
2208 (setq string (replace-match "" t nil string))))
2209 (concat string term)))
2211 (defun org-number-to-counter (n type)
2212 "Concert number N to a string counter, according to TYPE.
2213 TYPE must be a string, any of:
2214 1 number
2215 A A,B,....
2216 a a,b,....
2217 I upper case roman numeral
2218 i lower case roman numeral"
2219 (cond
2220 ((equal type "1") (number-to-string n))
2221 ((equal type "A") (char-to-string (+ ?A n -1)))
2222 ((equal type "a") (char-to-string (+ ?a n -1)))
2223 ((equal type "I") (org-number-to-roman n))
2224 ((equal type "i") (downcase (org-number-to-roman n)))
2225 (t (error "Invalid counter type `%s'" type))))
2227 (defun org-number-to-roman (n)
2228 "Convert integer N into a roman numeral."
2229 (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
2230 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
2231 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
2232 ( 1 . "I")))
2233 (res ""))
2234 (if (<= n 0)
2235 (number-to-string n)
2236 (while roman
2237 (if (>= n (caar roman))
2238 (setq n (- n (caar roman))
2239 res (concat res (cdar roman)))
2240 (pop roman)))
2241 res)))
2243 ;;; Macros
2245 (defun org-export-preprocess-apply-macros ()
2246 "Replace macro references."
2247 (goto-char (point-min))
2248 (let (sy val key args args2 ind-str s n)
2249 (while (re-search-forward
2250 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
2251 nil t)
2252 (unless (save-match-data (save-excursion
2253 (goto-char (point-at-bol))
2254 (looking-at "[ \t]*#\\+macro")))
2255 ;; Get macro name (KEY), arguments (ARGS), and indentation of
2256 ;; current line (IND-STR) as strings.
2257 (setq key (downcase (match-string 1))
2258 args (match-string 3)
2259 ind-str (save-match-data (save-excursion
2260 (beginning-of-line)
2261 (looking-at "^\\([ \t]*\\).*")
2262 (match-string 1))))
2263 ;; When macro is defined, retrieve replacement text in VAL,
2264 ;; and proceed with expansion.
2265 (when (setq val (or (plist-get org-export-opt-plist
2266 (intern (concat ":macro-" key)))
2267 (plist-get org-export-opt-plist
2268 (intern (concat ":" key)))))
2269 (save-match-data
2270 ;; If arguments are provided, first retreive them properly
2271 ;; (in ARGS, as a list), then replace them in VAL.
2272 (when args
2273 (setq args (org-split-string args ",") args2 nil)
2274 (while args
2275 (while (string-match "\\\\\\'" (car args))
2276 ;; Repair bad splits.
2277 (setcar (cdr args) (concat (substring (car args) 0 -1)
2278 "," (nth 1 args)))
2279 (pop args))
2280 (push (pop args) args2))
2281 (setq args (mapcar 'org-trim (nreverse args2)))
2282 (setq s 0)
2283 (while (string-match "\\$\\([0-9]+\\)" val s)
2284 (setq s (1+ (match-beginning 0))
2285 n (string-to-number (match-string 1 val)))
2286 (and (>= (length args) n)
2287 (setq val (replace-match (nth (1- n) args) t t val)))))
2288 ;; VAL starts with "(eval": it is a sexp, `eval' it.
2289 (when (string-match "\\`(eval\\>" val)
2290 (setq val (eval (read val))))
2291 ;; Ensure VAL is a string (or nil) and that each new line
2292 ;; is indented as the first one.
2293 (setq val (and val
2294 (mapconcat 'identity
2295 (org-split-string
2296 (if (stringp val) val (format "%s" val))
2297 "\n")
2298 (concat "\n" ind-str)))))
2299 ;; Eventually do the replacement, if VAL isn't nil. Move
2300 ;; point at beginning of macro for recursive expansions.
2301 (when val
2302 (replace-match val t t)
2303 (goto-char (match-beginning 0))))))))
2305 (defun org-export-apply-macros-in-string (s)
2306 "Apply the macros in string S."
2307 (when s
2308 (with-temp-buffer
2309 (insert s)
2310 (org-export-preprocess-apply-macros)
2311 (buffer-string))))
2313 ;;; Include files
2315 (defun org-export-handle-include-files ()
2316 "Include the contents of include files, with proper formatting."
2317 (let ((case-fold-search t)
2318 params file markup lang start end prefix prefix1 switches all minlevel lines)
2319 (goto-char (point-min))
2320 (while (re-search-forward "^#\\+INCLUDE:?[ \t]+\\(.*\\)" nil t)
2321 (setq params (read (concat "(" (match-string 1) ")"))
2322 prefix (org-get-and-remove-property 'params :prefix)
2323 prefix1 (org-get-and-remove-property 'params :prefix1)
2324 minlevel (org-get-and-remove-property 'params :minlevel)
2325 lines (org-get-and-remove-property 'params :lines)
2326 file (org-symname-or-string (pop params))
2327 markup (org-symname-or-string (pop params))
2328 lang (and (member markup '("src" "SRC"))
2329 (org-symname-or-string (pop params)))
2330 switches (mapconcat #'(lambda (x) (format "%s" x)) params " ")
2331 start nil end nil)
2332 (delete-region (match-beginning 0) (match-end 0))
2333 (if (or (not file)
2334 (not (file-exists-p file))
2335 (not (file-readable-p file)))
2336 (insert (format "CANNOT INCLUDE FILE %s" file))
2337 (setq all (cons file all))
2338 (when markup
2339 (if (equal (downcase markup) "src")
2340 (setq start (format "#+begin_src %s %s\n"
2341 (or lang "fundamental")
2342 (or switches ""))
2343 end "#+end_src")
2344 (setq start (format "#+begin_%s %s\n" markup switches)
2345 end (format "#+end_%s" markup))))
2346 (insert (or start ""))
2347 (insert (org-get-file-contents (expand-file-name file)
2348 prefix prefix1 markup minlevel lines))
2349 (or (bolp) (newline))
2350 (insert (or end ""))))
2351 all))
2353 (defun org-export-handle-include-files-recurse ()
2354 "Recursively include files aborting on circular inclusion."
2355 (let ((now (list org-current-export-file)) all)
2356 (while now
2357 (setq all (append now all))
2358 (setq now (org-export-handle-include-files))
2359 (let ((intersection
2360 (delq nil
2361 (mapcar (lambda (el) (when (member el all) el)) now))))
2362 (when intersection
2363 (error "Recursive #+INCLUDE: %S" intersection))))))
2365 (defun org-get-file-contents (file &optional prefix prefix1 markup minlevel lines)
2366 "Get the contents of FILE and return them as a string.
2367 If PREFIX is a string, prepend it to each line. If PREFIX1
2368 is a string, prepend it to the first line instead of PREFIX.
2369 If MARKUP, don't protect org-like lines, the exporter will
2370 take care of the block they are in. If LINES is a string
2371 specifying a range of lines, include only those lines ."
2372 (if (stringp markup) (setq markup (downcase markup)))
2373 (with-temp-buffer
2374 (insert-file-contents file)
2375 (when lines
2376 (let* ((lines (split-string lines "-"))
2377 (lbeg (string-to-number (car lines)))
2378 (lend (string-to-number (cadr lines)))
2379 (beg (if (zerop lbeg) (point-min)
2380 (goto-char (point-min))
2381 (forward-line (1- lbeg))
2382 (point)))
2383 (end (if (zerop lend) (point-max)
2384 (goto-char (point-min))
2385 (forward-line (1- lend))
2386 (point))))
2387 (narrow-to-region beg end)))
2388 (when (or prefix prefix1)
2389 (goto-char (point-min))
2390 (while (not (eobp))
2391 (insert (or prefix1 prefix))
2392 (setq prefix1 "")
2393 (beginning-of-line 2)))
2394 (buffer-string)
2395 (when (member markup '("src" "example"))
2396 (goto-char (point-min))
2397 (while (re-search-forward "^\\([*#]\\|[ \t]*#\\+\\)" nil t)
2398 (goto-char (match-beginning 0))
2399 (insert ",")
2400 (end-of-line 1)))
2401 (when minlevel
2402 (dotimes (lvl minlevel)
2403 (org-map-region 'org-demote (point-min) (point-max))))
2404 (buffer-string)))
2406 (defun org-get-and-remove-property (listvar prop)
2407 "Check if the value of LISTVAR contains PROP as a property.
2408 If yes, return the value of that property (i.e. the element following
2409 in the list) and remove property and value from the list in LISTVAR."
2410 (let ((list (symbol-value listvar)) m v)
2411 (when (setq m (member prop list))
2412 (setq v (nth 1 m))
2413 (if (equal (car list) prop)
2414 (set listvar (cddr list))
2415 (setcdr (nthcdr (- (length list) (length m) 1) list)
2416 (cddr m))
2417 (set listvar list)))
2420 (defun org-symname-or-string (s)
2421 (if (symbolp s)
2422 (if s (symbol-name s) s)
2425 ;;; Fontification and line numbers for code examples
2427 (defvar org-export-last-code-line-counter-value 0)
2429 (defun org-export-replace-src-segments-and-examples ()
2430 "Replace source code segments with special code for export."
2431 (setq org-export-last-code-line-counter-value 0)
2432 (let ((case-fold-search t)
2433 lang code trans opts indent caption)
2434 (goto-char (point-min))
2435 (while (re-search-forward
2436 "\\(^\\([ \t]*\\)#\\+BEGIN_SRC:?\\([ \t]+\\([^ \t\n]+\\)\\)?\\(.*\\)\n\\([^\000]+?\n\\)[ \t]*#\\+END_SRC.*\n?\\)\\|\\(^\\([ \t]*\\)#\\+BEGIN_EXAMPLE:?\\(?:[ \t]+\\(.*\\)\\)?\n\\([^\000]+?\n\\)[ \t]*#\\+END_EXAMPLE.*\n?\\)"
2437 nil t)
2438 (if (match-end 1)
2439 (if (not (match-string 4))
2440 (error "Source block missing language specification: %s"
2441 (let* ((body (match-string 6))
2442 (nothing (message "body:%s" body))
2443 (preview (or (and (string-match
2444 "^[ \t]*\\([^\n\r]*\\)" body)
2445 (match-string 1 body)) body)))
2446 (if (> (length preview) 35)
2447 (concat (substring preview 0 32) "...")
2448 preview)))
2449 ;; src segments
2450 (setq lang (match-string 4)
2451 opts (match-string 5)
2452 code (match-string 6)
2453 indent (length (match-string 2))
2454 caption (get-text-property 0 'org-caption (match-string 0))))
2455 (setq lang nil
2456 opts (match-string 9)
2457 code (match-string 10)
2458 indent (length (match-string 8))
2459 caption (get-text-property 0 'org-caption (match-string 0))))
2461 (setq trans (org-export-format-source-code-or-example
2462 lang code opts indent caption))
2463 (replace-match trans t t))))
2465 (defvar org-export-latex-verbatim-wrap) ;; defined in org-latex.el
2466 (defvar org-export-latex-listings) ;; defined in org-latex.el
2467 (defvar org-export-latex-listings-langs) ;; defined in org-latex.el
2468 (defvar org-export-latex-listings-w-names) ;; defined in org-latex.el
2469 (defvar org-export-latex-minted-langs) ;; defined in org-latex.el
2470 (defvar org-export-latex-custom-lang-environments) ;; defined in org-latex.el
2471 (defvar org-export-latex-listings-options) ;; defined in org-latex.el
2472 (defvar org-export-latex-minted-options) ;; defined in org-latex.el
2474 (defun org-remove-formatting-on-newlines-in-region (beg end)
2475 "Remove formatting on newline characters"
2476 (interactive "r")
2477 (save-excursion
2478 (goto-char beg)
2479 (while (progn (end-of-line) (< (point) end))
2480 (put-text-property (point) (1+ (point)) 'face nil)
2481 (forward-char 1))))
2483 (defun org-export-format-source-code-or-example
2484 (lang code &optional opts indent caption)
2485 "Format CODE from language LANG and return it formatted for export.
2486 The CODE is marked up in `org-export-current-backend' format.
2488 Check if a function by name
2489 \"org-<backend>-format-source-code-or-example\" is bound. If yes,
2490 use it as the custom formatter. Otherwise, use the default
2491 formatter. Default formatters are provided for docbook, html,
2492 latex and ascii backends. For example, use
2493 `org-html-format-source-code-or-example' to provide a custom
2494 formatter for export to \"html\".
2496 If LANG is nil, do not add any fontification.
2497 OPTS contains formatting options, like `-n' for triggering numbering lines,
2498 and `+n' for continuing previous numbering.
2499 Code formatting according to language currently only works for HTML.
2500 Numbering lines works for all three major backends (html, latex, and ascii).
2501 INDENT was the original indentation of the block."
2502 (save-match-data
2503 (let* ((backend-name (symbol-name org-export-current-backend))
2504 (backend-formatter
2505 (intern (format "org-%s-format-source-code-or-example"
2506 backend-name)))
2507 (backend-feature (intern (concat "org-" backend-name)))
2508 (backend-formatter
2509 (and (require (intern (concat "org-" backend-name)) nil)
2510 (fboundp backend-formatter) backend-formatter))
2511 num cont rtn rpllbl keepp textareap preserve-indentp cols rows fmt)
2512 (setq opts (or opts "")
2513 num (string-match "[-+]n\\>" opts)
2514 cont (string-match "\\+n\\>" opts)
2515 rpllbl (string-match "-r\\>" opts)
2516 keepp (string-match "-k\\>" opts)
2517 textareap (string-match "-t\\>" opts)
2518 preserve-indentp (or org-src-preserve-indentation
2519 (string-match "-i\\>" opts))
2520 cols (if (string-match "-w[ \t]+\\([0-9]+\\)" opts)
2521 (string-to-number (match-string 1 opts))
2523 rows (if (string-match "-h[ \t]+\\([0-9]+\\)" opts)
2524 (string-to-number (match-string 1 opts))
2525 (org-count-lines code))
2526 fmt (if (string-match "-l[ \t]+\"\\([^\"\n]+\\)\"" opts)
2527 (match-string 1 opts)))
2528 (when (and textareap (eq org-export-current-backend 'html))
2529 ;; we cannot use numbering or highlighting.
2530 (setq num nil cont nil lang nil))
2531 (if keepp (setq rpllbl 'keep))
2532 (setq rtn (if preserve-indentp code (org-remove-indentation code)))
2533 (when (string-match "^," rtn)
2534 (setq rtn (with-temp-buffer
2535 (insert rtn)
2536 ;; Free up the protected lines
2537 (goto-char (point-min))
2538 (while (re-search-forward "^," nil t)
2539 (if (or (equal lang "org")
2540 (save-match-data
2541 (looking-at "\\([*#]\\|[ \t]*#\\+\\)")))
2542 (replace-match ""))
2543 (end-of-line 1))
2544 (buffer-string))))
2545 ;; Now backend-specific coding
2546 (setq rtn
2547 (cond
2548 (backend-formatter
2549 (funcall backend-formatter rtn lang caption textareap cols rows num
2550 cont rpllbl fmt))
2551 ((eq org-export-current-backend 'docbook)
2552 (setq rtn (org-export-number-lines rtn 0 0 num cont rpllbl fmt))
2553 (concat "<programlisting><![CDATA["
2555 "]]></programlisting>\n"))
2556 ((eq org-export-current-backend 'html)
2557 ;; We are exporting to HTML
2558 (when lang
2559 (if (featurep 'xemacs)
2560 (require 'htmlize)
2561 (require 'htmlize nil t))
2562 (when (not (fboundp 'htmlize-region-for-paste))
2563 ;; we do not have htmlize.el, or an old version of it
2564 (setq lang nil)
2565 (message
2566 "htmlize.el 1.34 or later is needed for source code formatting")))
2568 (if lang
2569 (let* ((lang-m (when lang
2570 (or (cdr (assoc lang org-src-lang-modes))
2571 lang)))
2572 (mode (and lang-m (intern
2573 (concat
2574 (if (symbolp lang-m)
2575 (symbol-name lang-m)
2576 lang-m)
2577 "-mode"))))
2578 (org-inhibit-startup t)
2579 (org-startup-folded nil))
2580 (setq rtn
2581 (with-temp-buffer
2582 (insert rtn)
2583 (if (functionp mode)
2584 (funcall mode)
2585 (fundamental-mode))
2586 (font-lock-fontify-buffer)
2587 ;; markup each line separately
2588 (org-remove-formatting-on-newlines-in-region (point-min) (point-max))
2589 (org-src-mode)
2590 (set-buffer-modified-p nil)
2591 (org-export-htmlize-region-for-paste
2592 (point-min) (point-max))))
2593 (if (string-match "<pre\\([^>]*\\)>\n*" rtn)
2594 (setq rtn
2595 (concat
2596 (if caption
2597 (concat
2598 "<div class=\"org-src-container\">"
2599 (format
2600 "<label class=\"org-src-name\">%s</label>"
2601 caption))
2603 (replace-match
2604 (format "<pre class=\"src src-%s\">\n" lang)
2605 t t rtn)
2606 (if caption "</div>" "")))))
2607 (if textareap
2608 (setq rtn (concat
2609 (format "<p>\n<textarea cols=\"%d\" rows=\"%d\">"
2610 cols rows)
2611 rtn "</textarea>\n</p>\n"))
2612 (with-temp-buffer
2613 (insert rtn)
2614 (goto-char (point-min))
2615 (while (re-search-forward "[<>&]" nil t)
2616 (replace-match (cdr (assq (char-before)
2617 '((?&."&amp;")(?<."&lt;")(?>."&gt;"))))
2618 t t))
2619 (setq rtn (buffer-string)))
2620 (setq rtn (concat "<pre class=\"example\">\n" rtn "</pre>\n"))))
2621 (unless textareap
2622 (setq rtn (org-export-number-lines rtn 1 1 num cont rpllbl fmt)))
2623 (if (string-match "\\(\\`<[^>]*>\\)\n" rtn)
2624 (setq rtn (replace-match "\\1" t nil rtn)))
2625 rtn)
2626 ((eq org-export-current-backend 'latex)
2627 (setq rtn (org-export-number-lines rtn 0 0 num cont rpllbl fmt))
2628 (cond
2629 ((and lang org-export-latex-listings)
2630 (flet ((make-option-string
2631 (pair)
2632 (concat (first pair)
2633 (if (> (length (second pair)) 0)
2634 (concat "=" (second pair))))))
2635 (let* ((lang-sym (intern lang))
2636 (minted-p (eq org-export-latex-listings 'minted))
2637 (listings-p (not minted-p))
2638 (backend-lang
2639 (or (cadr
2640 (assq
2641 lang-sym
2642 (cond
2643 (minted-p org-export-latex-minted-langs)
2644 (listings-p org-export-latex-listings-langs))))
2645 lang))
2646 (custom-environment
2647 (cadr
2648 (assq
2649 lang-sym
2650 org-export-latex-custom-lang-environments))))
2651 (concat
2652 (when (and listings-p (not custom-environment))
2653 (format
2654 "\\lstset{%s}\n"
2655 (mapconcat
2656 #'make-option-string
2657 (append org-export-latex-listings-options
2658 `(("language" ,backend-lang))) ",")))
2659 (when (and caption org-export-latex-listings-w-names)
2660 (format
2661 "\n%s $\\equiv$ \n"
2662 (replace-regexp-in-string "_" "\\\\_" caption)))
2663 (cond
2664 (custom-environment
2665 (format "\\begin{%s}\n%s\\end{%s}\n"
2666 custom-environment rtn custom-environment))
2667 (listings-p
2668 (format "\\begin{%s}\n%s\\end{%s}\n"
2669 "lstlisting" rtn "lstlisting"))
2670 (minted-p
2671 (format
2672 "\\begin{minted}[%s]{%s}\n%s\\end{minted}\n"
2673 (mapconcat #'make-option-string
2674 org-export-latex-minted-options ",")
2675 backend-lang rtn)))))))
2676 (t (concat (car org-export-latex-verbatim-wrap)
2677 rtn (cdr org-export-latex-verbatim-wrap)))))
2678 ((eq org-export-current-backend 'ascii)
2679 ;; This is not HTML or LaTeX, so just make it an example.
2680 (setq rtn (org-export-number-lines rtn 0 0 num cont rpllbl fmt))
2681 (concat caption "\n"
2682 (concat
2683 (mapconcat
2684 (lambda (l) (concat " " l))
2685 (org-split-string rtn "\n")
2686 "\n")
2687 "\n")
2690 (error "Don't know how to markup source or example block in %s"
2691 (upcase backend-name)))))
2692 (setq rtn
2693 (concat
2694 "\n#+BEGIN_" backend-name "\n"
2695 (org-add-props rtn
2696 '(org-protected t org-example t org-native-text t))
2697 "\n#+END_" backend-name "\n\n"))
2698 (org-add-props rtn nil 'original-indentation indent))))
2700 (defun org-export-number-lines (text &optional skip1 skip2 number cont
2701 replace-labels label-format)
2702 (setq skip1 (or skip1 0) skip2 (or skip2 0))
2703 (if (not cont) (setq org-export-last-code-line-counter-value 0))
2704 (with-temp-buffer
2705 (insert text)
2706 (goto-char (point-max))
2707 (skip-chars-backward " \t\n\r")
2708 (delete-region (point) (point-max))
2709 (beginning-of-line (- 1 skip2))
2710 (let* ((last (org-current-line))
2711 (n org-export-last-code-line-counter-value)
2712 (nmax (+ n (- last skip1)))
2713 (fmt (format "%%%dd: " (length (number-to-string nmax))))
2715 (cond
2716 ((eq org-export-current-backend 'html) (format "<span class=\"linenr\">%s</span>"
2717 fmt))
2718 ((eq org-export-current-backend 'ascii) fmt)
2719 ((eq org-export-current-backend 'latex) fmt)
2720 ((eq org-export-current-backend 'docbook) fmt)
2721 (t "")))
2722 (label-format (or label-format org-coderef-label-format))
2723 (label-pre (if (string-match "%s" label-format)
2724 (substring label-format 0 (match-beginning 0))
2725 label-format))
2726 (label-post (if (string-match "%s" label-format)
2727 (substring label-format (match-end 0))
2728 ""))
2729 (lbl-re
2730 (concat
2731 ".*?\\S-.*?\\([ \t]*\\("
2732 (regexp-quote label-pre)
2733 "\\([-a-zA-Z0-9_ ]+\\)"
2734 (regexp-quote label-post)
2735 "\\)\\)"))
2736 ref)
2738 (org-goto-line (1+ skip1))
2739 (while (and (re-search-forward "^" nil t) (not (eobp)) (< n nmax))
2740 (if number
2741 (insert (format fm (incf n)))
2742 (forward-char 1))
2743 (when (looking-at lbl-re)
2744 (setq ref (match-string 3))
2745 (cond ((numberp replace-labels)
2746 ;; remove labels; use numbers for references when lines
2747 ;; are numbered, use labels otherwise
2748 (delete-region (match-beginning 1) (match-end 1))
2749 (push (cons ref (if (> n 0) n ref)) org-export-code-refs))
2750 ((eq replace-labels 'keep)
2751 ;; don't remove labels; use numbers for references when
2752 ;; lines are numbered, use labels otherwise
2753 (goto-char (match-beginning 2))
2754 (delete-region (match-beginning 2) (match-end 2))
2755 (insert "(" ref ")")
2756 (push (cons ref (if (> n 0) n (concat "(" ref ")")))
2757 org-export-code-refs))
2759 ;; don't remove labels and don't use numbers for
2760 ;; references
2761 (goto-char (match-beginning 2))
2762 (delete-region (match-beginning 2) (match-end 2))
2763 (insert "(" ref ")")
2764 (push (cons ref (concat "(" ref ")")) org-export-code-refs)))
2765 (when (eq org-export-current-backend 'html)
2766 (save-excursion
2767 (beginning-of-line 1)
2768 (insert (format "<span id=\"coderef-%s\" class=\"coderef-off\">"
2769 ref))
2770 (end-of-line 1)
2771 (insert "</span>")))))
2772 (setq org-export-last-code-line-counter-value n)
2773 (goto-char (point-max))
2774 (newline)
2775 (buffer-string))))
2777 (defun org-search-todo-below (line lines level)
2778 "Search the subtree below LINE for any TODO entries."
2779 (let ((rest (cdr (memq line lines)))
2780 (re org-todo-line-regexp)
2781 line lv todo)
2782 (catch 'exit
2783 (while (setq line (pop rest))
2784 (if (string-match re line)
2785 (progn
2786 (setq lv (- (match-end 1) (match-beginning 1))
2787 todo (and (match-beginning 2)
2788 (not (member (match-string 2 line)
2789 org-done-keywords))))
2790 ; TODO, not DONE
2791 (if (<= lv level) (throw 'exit nil))
2792 (if todo (throw 'exit t))))))))
2794 ;;;###autoload
2795 (defun org-export-visible (type arg)
2796 "Create a copy of the visible part of the current buffer, and export it.
2797 The copy is created in a temporary buffer and removed after use.
2798 TYPE is the final key (as a string) that also selects the export command in
2799 the \\<org-mode-map>\\[org-export] export dispatcher.
2800 As a special case, if the you type SPC at the prompt, the temporary
2801 org-mode file will not be removed but presented to you so that you can
2802 continue to use it. The prefix arg ARG is passed through to the exporting
2803 command."
2804 (interactive
2805 (list (progn
2806 (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")
2807 (read-char-exclusive))
2808 current-prefix-arg))
2809 (if (not (member type '(?a ?n ?u ?\C-a ?b ?\C-b ?h ?D ?x ?\ ?l ?p ?d ?L ?H ?R)))
2810 (error "Invalid export key"))
2811 (let* ((binding (cdr (assoc type
2813 (?a . org-export-as-ascii)
2814 (?A . org-export-as-ascii-to-buffer)
2815 (?n . org-export-as-latin1)
2816 (?N . org-export-as-latin1-to-buffer)
2817 (?u . org-export-as-utf8)
2818 (?U . org-export-as-utf8-to-buffer)
2819 (?\C-a . org-export-as-ascii)
2820 (?b . org-export-as-html-and-open)
2821 (?\C-b . org-export-as-html-and-open)
2822 (?h . org-export-as-html)
2823 (?H . org-export-as-html-to-buffer)
2824 (?R . org-export-region-as-html)
2825 (?D . org-export-as-docbook)
2827 (?l . org-export-as-latex)
2828 (?p . org-export-as-pdf)
2829 (?d . org-export-as-pdf-and-open)
2830 (?L . org-export-as-latex-to-buffer)
2832 (?x . org-export-as-xoxo)))))
2833 (keepp (equal type ?\ ))
2834 (file buffer-file-name)
2835 (buffer (get-buffer-create "*Org Export Visible*"))
2836 s e)
2837 ;; Need to hack the drawers here.
2838 (save-excursion
2839 (goto-char (point-min))
2840 (while (re-search-forward org-drawer-regexp nil t)
2841 (goto-char (match-beginning 1))
2842 (or (outline-invisible-p) (org-flag-drawer nil))))
2843 (with-current-buffer buffer (erase-buffer))
2844 (save-excursion
2845 (setq s (goto-char (point-min)))
2846 (while (not (= (point) (point-max)))
2847 (goto-char (org-find-invisible))
2848 (append-to-buffer buffer s (point))
2849 (setq s (goto-char (org-find-visible))))
2850 (org-cycle-hide-drawers 'all)
2851 (goto-char (point-min))
2852 (unless keepp
2853 ;; Copy all comment lines to the end, to make sure #+ settings are
2854 ;; still available for the second export step. Kind of a hack, but
2855 ;; does do the trick.
2856 (if (looking-at "#[^\r\n]*")
2857 (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
2858 (when (re-search-forward "^\\*+[ \t]+" nil t)
2859 (while (re-search-backward "[\n\r]#[^\n\r]*" nil t)
2860 (append-to-buffer buffer (1+ (match-beginning 0))
2861 (min (point-max) (1+ (match-end 0)))))))
2862 (set-buffer buffer)
2863 (let ((buffer-file-name file)
2864 (org-inhibit-startup t))
2865 (org-mode)
2866 (show-all)
2867 (unless keepp (funcall binding arg))))
2868 (if (not keepp)
2869 (kill-buffer buffer)
2870 (switch-to-buffer-other-window buffer)
2871 (goto-char (point-min)))))
2873 (defun org-find-visible ()
2874 (let ((s (point)))
2875 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
2876 (get-char-property s 'invisible)))
2878 (defun org-find-invisible ()
2879 (let ((s (point)))
2880 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
2881 (not (get-char-property s 'invisible))))
2884 (defvar org-export-htmlized-org-css-url) ;; defined in org-html.el
2886 (defun org-export-string (string fmt &optional dir)
2887 "Export STRING to FMT using existing export facilities.
2888 During export STRING is saved to a temporary file whose location
2889 could vary. Optional argument DIR can be used to force the
2890 directory in which the temporary file is created during export
2891 which can be useful for resolving relative paths. Dir defaults
2892 to the value of `temporary-file-directory'."
2893 (let ((temporary-file-directory (or dir temporary-file-directory))
2894 (tmp-file (make-temp-file "org-")))
2895 (unwind-protect
2896 (with-temp-buffer
2897 (insert string)
2898 (write-file tmp-file)
2899 (org-load-modules-maybe)
2900 (unless org-local-vars
2901 (setq org-local-vars (org-get-local-variables)))
2902 (eval ;; convert to fmt -- mimicing `org-run-like-in-org-mode'
2903 (list 'let org-local-vars
2904 (list (intern (format "org-export-as-%s" fmt))
2905 nil nil nil ''string t))))
2906 (delete-file tmp-file))))
2908 ;;;###autoload
2909 (defun org-export-as-org (arg &optional hidden ext-plist
2910 to-buffer body-only pub-dir)
2911 "Make a copy with not-exporting stuff removed.
2912 The purpose of this function is to provide a way to export the source
2913 Org file of a webpage in Org format, but with sensitive and/or irrelevant
2914 stuff removed. This command will remove the following:
2916 - archived trees (if the variable `org-export-with-archived-trees' is nil)
2917 - comment blocks and trees starting with the COMMENT keyword
2918 - only trees that are consistent with `org-export-select-tags'
2919 and `org-export-exclude-tags'.
2921 The only arguments that will be used are EXT-PLIST and PUB-DIR,
2922 all the others will be ignored (but are present so that the general
2923 mechanism to call publishing functions will work).
2925 EXT-PLIST is a property list with external parameters overriding
2926 org-mode's default settings, but still inferior to file-local
2927 settings. When PUB-DIR is set, use this as the publishing
2928 directory."
2929 (interactive "P")
2930 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
2931 ext-plist
2932 (org-infile-export-plist)))
2933 (bfname (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
2934 (filename (concat (file-name-as-directory
2935 (or pub-dir
2936 (org-export-directory :org opt-plist)))
2937 (file-name-sans-extension
2938 (file-name-nondirectory bfname))
2939 ".org"))
2940 (filename (and filename
2941 (if (equal (file-truename filename)
2942 (file-truename bfname))
2943 (concat (file-name-sans-extension filename)
2944 "-source."
2945 (file-name-extension filename))
2946 filename)))
2947 (backup-inhibited t)
2948 (buffer (find-file-noselect filename))
2949 (region (buffer-string))
2950 str-ret)
2951 (save-excursion
2952 (switch-to-buffer buffer)
2953 (erase-buffer)
2954 (insert region)
2955 (let ((org-inhibit-startup t)) (org-mode))
2956 (org-install-letbind)
2958 ;; Get rid of archived trees
2959 (org-export-remove-archived-trees (plist-get opt-plist :archived-trees))
2961 ;; Remove comment environment and comment subtrees
2962 (org-export-remove-comment-blocks-and-subtrees)
2964 ;; Get rid of excluded trees
2965 (org-export-handle-export-tags (plist-get opt-plist :select-tags)
2966 (plist-get opt-plist :exclude-tags))
2968 (when (or (plist-get opt-plist :plain-source)
2969 (not (or (plist-get opt-plist :plain-source)
2970 (plist-get opt-plist :htmlized-source))))
2971 ;; Either nothing special is requested (default call)
2972 ;; or the plain source is explicitly requested
2973 ;; so: save it
2974 (save-buffer))
2975 (when (plist-get opt-plist :htmlized-source)
2976 ;; Make the htmlized version
2977 (require 'htmlize)
2978 (require 'org-html)
2979 (font-lock-fontify-buffer)
2980 (let* ((htmlize-output-type 'css)
2981 (newbuf (htmlize-buffer)))
2982 (with-current-buffer newbuf
2983 (when org-export-htmlized-org-css-url
2984 (goto-char (point-min))
2985 (and (re-search-forward
2986 "<style type=\"text/css\">[^\000]*?\n[ \t]*</style>.*"
2987 nil t)
2988 (replace-match
2989 (format
2990 "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\">"
2991 org-export-htmlized-org-css-url)
2992 t t)))
2993 (write-file (concat filename ".html")))
2994 (kill-buffer newbuf)))
2995 (set-buffer-modified-p nil)
2996 (if (equal to-buffer 'string)
2997 (progn (setq str-ret (buffer-string))
2998 (kill-buffer (current-buffer))
2999 str-ret)
3000 (kill-buffer (current-buffer))))))
3002 (defvar org-archive-location) ;; gets loaded with the org-archive require.
3003 (defun org-get-current-options ()
3004 "Return a string with current options as keyword options.
3005 Does include HTML export options as well as TODO and CATEGORY stuff."
3006 (require 'org-archive)
3007 (format
3008 "#+TITLE: %s
3009 #+AUTHOR: %s
3010 #+EMAIL: %s
3011 #+DATE: %s
3012 #+DESCRIPTION:
3013 #+KEYWORDS:
3014 #+LANGUAGE: %s
3015 #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s -:%s f:%s *:%s <:%s
3016 #+OPTIONS: TeX:%s LaTeX:%s skip:%s d:%s todo:%s pri:%s tags:%s
3018 #+EXPORT_SELECT_TAGS: %s
3019 #+EXPORT_EXCLUDE_TAGS: %s
3020 #+LINK_UP: %s
3021 #+LINK_HOME: %s
3022 #+XSLT:
3023 #+CATEGORY: %s
3024 #+SEQ_TODO: %s
3025 #+TYP_TODO: %s
3026 #+PRIORITIES: %c %c %c
3027 #+DRAWERS: %s
3028 #+STARTUP: %s %s %s %s %s
3029 #+TAGS: %s
3030 #+FILETAGS: %s
3031 #+ARCHIVE: %s
3032 #+LINK: %s
3034 (buffer-name) (user-full-name) user-mail-address
3035 (format-time-string (substring (car org-time-stamp-formats) 1 -1))
3036 org-export-default-language
3037 org-export-headline-levels
3038 org-export-with-section-numbers
3039 org-export-with-toc
3040 org-export-preserve-breaks
3041 org-export-html-expand
3042 org-export-with-fixed-width
3043 org-export-with-tables
3044 org-export-with-sub-superscripts
3045 org-export-with-special-strings
3046 org-export-with-footnotes
3047 org-export-with-emphasize
3048 org-export-with-timestamps
3049 org-export-with-TeX-macros
3050 org-export-with-LaTeX-fragments
3051 org-export-skip-text-before-1st-heading
3052 org-export-with-drawers
3053 org-export-with-todo-keywords
3054 org-export-with-priority
3055 org-export-with-tags
3056 (if (featurep 'org-jsinfo) (org-infojs-options-inbuffer-template) "")
3057 (mapconcat 'identity org-export-select-tags " ")
3058 (mapconcat 'identity org-export-exclude-tags " ")
3059 org-export-html-link-up
3060 org-export-html-link-home
3061 (or (ignore-errors
3062 (file-name-sans-extension
3063 (file-name-nondirectory (buffer-file-name (buffer-base-buffer)))))
3064 "NOFILENAME")
3065 "TODO FEEDBACK VERIFY DONE"
3066 "Me Jason Marie DONE"
3067 org-highest-priority org-lowest-priority org-default-priority
3068 (mapconcat 'identity org-drawers " ")
3069 (cdr (assoc org-startup-folded
3070 '((nil . "showall") (t . "overview") (content . "content"))))
3071 (if org-odd-levels-only "odd" "oddeven")
3072 (if org-hide-leading-stars "hidestars" "showstars")
3073 (if org-startup-align-all-tables "align" "noalign")
3074 (cond ((eq org-log-done t) "logdone")
3075 ((equal org-log-done 'note) "lognotedone")
3076 ((not org-log-done) "nologdone"))
3077 (or (mapconcat (lambda (x)
3078 (cond
3079 ((equal :startgroup (car x)) "{")
3080 ((equal :endgroup (car x)) "}")
3081 ((equal :newline (car x)) "")
3082 ((cdr x) (format "%s(%c)" (car x) (cdr x)))
3083 (t (car x))))
3084 (or org-tag-alist (org-get-buffer-tags)) " ") "")
3085 (mapconcat 'identity org-file-tags " ")
3086 org-archive-location
3087 "org file:~/org/%s.org"
3090 ;;;###autoload
3091 (defun org-insert-export-options-template ()
3092 "Insert into the buffer a template with information for exporting."
3093 (interactive)
3094 (if (not (bolp)) (newline))
3095 (let ((s (org-get-current-options)))
3096 (and (string-match "#\\+CATEGORY" s)
3097 (setq s (substring s 0 (match-beginning 0))))
3098 (insert s)))
3100 (defvar org-table-colgroup-info nil)
3102 (defun org-table-clean-before-export (lines &optional maybe-quoted)
3103 "Check if the table has a marking column.
3104 If yes remove the column and the special lines."
3105 (setq org-table-colgroup-info nil)
3106 (if (memq nil
3107 (mapcar
3108 (lambda (x) (or (string-match "^[ \t]*|-" x)
3109 (string-match
3110 (if maybe-quoted
3111 "^[ \t]*| *\\\\?\\([\#!$*_^ /]\\) *|"
3112 "^[ \t]*| *\\([\#!$*_^ /]\\) *|")
3113 x)))
3114 lines))
3115 ;; No special marking column
3116 (progn
3117 (setq org-table-clean-did-remove-column nil)
3118 (delq nil
3119 (mapcar
3120 (lambda (x)
3121 (cond
3122 ((org-table-colgroup-line-p x)
3123 ;; This line contains colgroup info, extract it
3124 ;; and then discard the line
3125 (setq org-table-colgroup-info
3126 (mapcar (lambda (x)
3127 (cond ((member x '("<" "&lt;")) :start)
3128 ((member x '(">" "&gt;")) :end)
3129 ((member x '("<>" "&lt;&gt;")) :startend)
3130 (t nil)))
3131 (org-split-string x "[ \t]*|[ \t]*")))
3132 nil)
3133 ((org-table-cookie-line-p x)
3134 ;; This line contains formatting cookies, discard it
3135 nil)
3136 (t x)))
3137 lines)))
3138 ;; there is a special marking column
3139 (setq org-table-clean-did-remove-column t)
3140 (delq nil
3141 (mapcar
3142 (lambda (x)
3143 (cond
3144 ((org-table-colgroup-line-p x)
3145 ;; This line contains colgroup info, extract it
3146 ;; and then discard the line
3147 (setq org-table-colgroup-info
3148 (mapcar (lambda (x)
3149 (cond ((member x '("<" "&lt;")) :start)
3150 ((member x '(">" "&gt;")) :end)
3151 ((member x '("<>" "&lt;&gt;")) :startend)
3152 (t nil)))
3153 (cdr (org-split-string x "[ \t]*|[ \t]*"))))
3154 nil)
3155 ((org-table-cookie-line-p x)
3156 ;; This line contains formatting cookies, discard it
3157 nil)
3158 ((string-match "^[ \t]*| *[!_^/] *|" x)
3159 ;; ignore this line
3160 nil)
3161 ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
3162 (string-match "^\\([ \t]*\\)|[^|]*|" x))
3163 ;; remove the first column
3164 (replace-match "\\1|" t nil x))))
3165 lines))))
3167 (defun org-export-cleanup-toc-line (s)
3168 "Remove tags and timestamps from lines going into the toc."
3169 (when (memq org-export-with-tags '(not-in-toc nil))
3170 (if (string-match (org-re " +:[[:alnum:]_@#%:]+: *$") s)
3171 (setq s (replace-match "" t t s))))
3172 (when org-export-remove-timestamps-from-toc
3173 (while (string-match org-maybe-keyword-time-regexp s)
3174 (setq s (replace-match "" t t s))))
3175 (while (string-match org-bracket-link-regexp s)
3176 (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
3177 t t s)))
3178 (while (string-match "\\[\\([0-9]\\|fn:[^]]*\\)\\]" s)
3179 (setq s (replace-match "" t t s)))
3183 (defun org-get-text-property-any (pos prop &optional object)
3184 (or (get-text-property pos prop object)
3185 (and (setq pos (next-single-property-change pos prop object))
3186 (get-text-property pos prop object))))
3188 (defun org-export-get-coderef-format (path desc)
3189 (save-match-data
3190 (if (and desc (string-match
3191 (regexp-quote (concat "(" path ")"))
3192 desc))
3193 (replace-match "%s" t t desc)
3194 (or desc "%s"))))
3196 (defun org-export-push-to-kill-ring (format)
3197 "Push buffer content to kill ring.
3198 The depends on the variable `org-export-copy-to-kill'."
3199 (when org-export-copy-to-kill-ring
3200 (org-kill-new (buffer-string))
3201 (when (fboundp 'x-set-selection)
3202 (ignore-errors (x-set-selection 'PRIMARY (buffer-string)))
3203 (ignore-errors (x-set-selection 'CLIPBOARD (buffer-string))))
3204 (message "%s export done, pushed to kill ring and clipboard" format)))
3206 (provide 'org-exp)
3208 ;; arch-tag: 65985fe9-095c-49c7-a7b6-cb4ee15c0a95
3210 ;;; org-exp.el ends here