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