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