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