* lisp/files.el (file-subdir-of-p): Fix typo.
[emacs.git] / lisp / org / org-exp.el
blobc7e1a94216e12bc4a6eb4eb6c3e7caa55d010634
1 ;;; org-exp.el --- ASCII, HTML, XOXO and iCalendar export for Org-mode
3 ;; Copyright (C) 2004-2012 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 (replace-match "\\1\\2" t)
1734 (add-text-properties
1735 (point-at-bol) (min (1+ (point-at-eol)) (point-max))
1736 `(org-protected t original-indentation ,ind org-native-text t))))
1737 ;; Delete #+ATTR_BACKEND: stuff of another backend. Those
1738 ;; matching the current backend will be taken care of by
1739 ;; `org-export-attach-captions-and-attributes'
1740 (goto-char (point-min))
1741 (while (re-search-forward (concat "^\\([ \t]*\\)#\\+ATTR_" backend-name
1742 ":[ \t]*\\(.*\\)") nil t)
1743 (setq ind (org-get-indentation))
1744 (when (not (eq backend org-export-current-backend))
1745 (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))))
1746 ;; Handle #+BEGIN_BACKEND and #+END_BACKEND stuff
1747 (goto-char (point-min))
1748 (while (re-search-forward (concat "^[ \t]*#\\+BEGIN_" backend-name "\\>.*\n?")
1749 nil t)
1750 (setq beg (match-beginning 0) beg-content (match-end 0))
1751 (setq ind (save-excursion (goto-char beg) (org-get-indentation)))
1752 (when (re-search-forward (concat "^[ \t]*#\\+END_" backend-name "\\>.*\n?")
1753 nil t)
1754 (setq end (match-end 0) end-content (match-beginning 0))
1755 (if (eq backend org-export-current-backend)
1756 ;; yes, keep this
1757 (progn
1758 (add-text-properties
1759 beg-content end-content
1760 `(org-protected t original-indentation ,ind org-native-text t))
1761 ;; strip protective commas
1762 (save-excursion
1763 (save-match-data
1764 (goto-char beg-content)
1765 (let ((front-line (save-excursion
1766 (re-search-forward
1767 "[^[:space:]]" end-content t)
1768 (goto-char (match-beginning 0))
1769 (current-column))))
1770 (while (re-search-forward "^[ \t]*\\(,\\)" end-content t)
1771 (when (= (current-column) front-line)
1772 (replace-match "" nil nil nil 1))))))
1773 (delete-region (match-beginning 0) (match-end 0))
1774 (save-excursion
1775 (goto-char beg)
1776 (delete-region (point) (1+ (point-at-eol)))))
1777 ;; No, this is for a different backend, kill it
1778 (delete-region beg end)))))))
1780 (defun org-export-mark-blockquote-verse-center ()
1781 "Mark block quote and verse environments with special cookies.
1782 These special cookies will later be interpreted by the backend."
1783 ;; Blockquotes
1784 (let (type t1 ind beg end beg1 end1 content)
1785 (goto-char (point-min))
1786 (while (re-search-forward
1787 "^\\([ \t]*\\)#\\+\\(begin_\\(\\(block\\)?quote\\|verse\\|center\\)\\>.*\\)"
1788 nil t)
1789 (setq ind (length (match-string 1))
1790 type (downcase (match-string 3))
1791 t1 (if (equal type "quote") "blockquote" type))
1792 (setq beg (match-beginning 0)
1793 beg1 (1+ (match-end 0)))
1794 (when (re-search-forward (concat "^[ \t]*#\\+end_" type "\\>.*") nil t)
1795 (setq end1 (1- (match-beginning 0))
1796 end (+ (point-at-eol) (if (looking-at "\n$") 1 0)))
1797 (setq content (org-remove-indentation (buffer-substring beg1 end1)))
1798 (setq content (concat "ORG-" (upcase t1) "-START\n"
1799 content "\n"
1800 "ORG-" (upcase t1) "-END\n"))
1801 (delete-region beg end)
1802 (insert (org-add-props content nil 'original-indentation ind))))))
1804 (defun org-export-mark-list-end ()
1805 "Mark all list endings with a special string."
1806 (unless (eq org-export-current-backend 'ascii)
1807 (mapc
1808 (lambda (e)
1809 ;; For each type allowing list export, find every list, remove
1810 ;; ending regexp if needed, and insert org-list-end.
1811 (goto-char (point-min))
1812 (while (re-search-forward (org-item-beginning-re) nil t)
1813 (when (eq (nth 2 (org-list-context)) e)
1814 (let* ((struct (org-list-struct))
1815 (bottom (org-list-get-bottom-point struct))
1816 (top (point-at-bol))
1817 (top-ind (org-list-get-ind top struct)))
1818 (goto-char bottom)
1819 (when (and (not (eq org-list-ending-method 'indent))
1820 (not (looking-at "[ \t]*$"))
1821 (looking-at org-list-end-re))
1822 (replace-match ""))
1823 (unless (bolp) (insert "\n"))
1824 ;; As org-list-end is inserted at column 0, it would end
1825 ;; by indentation any list. It can be problematic when
1826 ;; there are lists within lists: the inner list end would
1827 ;; also become the outer list end. To avoid this, text
1828 ;; property `original-indentation' is added, as
1829 ;; `org-list-struct' pays attention to it when reading a
1830 ;; list.
1831 (insert (org-add-props
1832 "ORG-LIST-END-MARKER\n"
1833 (list 'original-indentation top-ind)))))))
1834 (cons nil org-list-export-context))))
1836 (defun org-export-mark-list-properties ()
1837 "Mark list with special properties.
1838 These special properties will later be interpreted by the backend."
1839 (let ((mark-list
1840 (function
1841 ;; Mark a list with 3 properties: `list-item' which is
1842 ;; position at beginning of line, `list-struct' which is
1843 ;; list structure, and `list-prevs' which is the alist of
1844 ;; item and its predecessor. Leave point at list ending.
1845 (lambda (ctxt)
1846 (let* ((struct (org-list-struct))
1847 (top (org-list-get-top-point struct))
1848 (bottom (org-list-get-bottom-point struct))
1849 (prevs (org-list-prevs-alist struct))
1850 poi)
1851 ;; Get every item and ending position, without dups and
1852 ;; without bottom point of list.
1853 (mapc (lambda (e)
1854 (let ((pos (car e))
1855 (end (nth 6 e)))
1856 (unless (memq pos poi)
1857 (push pos poi))
1858 (unless (or (= end bottom) (memq end poi))
1859 (push end poi))))
1860 struct)
1861 (setq poi (sort poi '<))
1862 ;; For every point of interest, mark the whole line with
1863 ;; its position in list.
1864 (mapc
1865 (lambda (e)
1866 (goto-char e)
1867 (add-text-properties (point-at-bol) (point-at-eol)
1868 (list 'list-item (point-at-bol)
1869 'list-struct struct
1870 'list-prevs prevs)))
1871 poi)
1872 ;; Take care of bottom point. As babel may have inserted
1873 ;; a new list in buffer, list ending isn't always
1874 ;; marked. Now mark every list ending and add properties
1875 ;; useful to line processing exporters.
1876 (goto-char bottom)
1877 (when (or (looking-at "^ORG-LIST-END-MARKER\n")
1878 (and (not (eq org-list-ending-method 'indent))
1879 (not (looking-at "[ \t]*$"))
1880 (looking-at org-list-end-re)))
1881 (replace-match ""))
1882 (unless (bolp) (insert "\n"))
1883 (insert
1884 (org-add-props "ORG-LIST-END-MARKER\n" (list 'list-item bottom
1885 'list-struct struct
1886 'list-prevs prevs)))
1887 ;; Following property is used by LaTeX exporter.
1888 (add-text-properties top (point) (list 'list-context ctxt)))))))
1889 ;; Mark lists except for backends not interpreting them.
1890 (unless (eq org-export-current-backend 'ascii)
1891 (let ((org-list-end-re "^ORG-LIST-END-MARKER\n"))
1892 (mapc
1893 (lambda (e)
1894 (goto-char (point-min))
1895 (while (re-search-forward (org-item-beginning-re) nil t)
1896 (let ((context (nth 2 (org-list-context))))
1897 (if (eq context e)
1898 (funcall mark-list e)
1899 (put-text-property (point-at-bol) (point-at-eol)
1900 'list-context context)))))
1901 (cons nil org-list-export-context))))))
1903 (defun org-export-attach-captions-and-attributes (target-alist)
1904 "Move #+CAPTION, #+ATTR_BACKEND, and #+LABEL text into text properties.
1905 If the next thing following is a table, add the text properties to the first
1906 table line. If it is a link, add it to the line containing the link."
1907 (goto-char (point-min))
1908 (remove-text-properties (point-min) (point-max)
1909 '(org-caption nil org-attributes nil))
1910 (let ((case-fold-search t)
1911 (re (concat "^[ \t]*#\\+caption:[ \t]+\\(.*\\)"
1912 "\\|"
1913 "^[ \t]*#\\+attr_" (symbol-name org-export-current-backend) ":[ \t]+\\(.*\\)"
1914 "\\|"
1915 "^[ \t]*#\\+label:[ \t]+\\(.*\\)"
1916 "\\|"
1917 "^[ \t]*\\(|[^-]\\)"
1918 "\\|"
1919 "^[ \t]*\\[\\[.*\\]\\][ \t]*$"))
1920 cap shortn attr label end)
1921 (while (re-search-forward re nil t)
1922 (cond
1923 ;; there is a caption
1924 ((match-end 1)
1925 (progn
1926 (setq cap (concat cap (if cap " " "") (org-trim (match-string 1))))
1927 (when (string-match "\\[\\(.*\\)\\]{\\(.*\\)}" cap)
1928 (setq shortn (match-string 1 cap)
1929 cap (match-string 2 cap)))
1930 (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))))
1931 ;; there is an attribute
1932 ((match-end 2)
1933 (progn
1934 (setq attr (concat attr (if attr " " "") (org-trim (match-string 2))))
1935 (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))))
1936 ;; there is a label
1937 ((match-end 3)
1938 (progn
1939 (setq label (org-trim (match-string 3)))
1940 (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))))
1942 (setq end (if (match-end 4)
1943 (let ((ee (org-table-end)))
1944 (prog1 (1- (marker-position ee)) (move-marker ee nil)))
1945 (point-at-eol)))
1946 (add-text-properties (point-at-bol) end
1947 (list 'org-caption cap
1948 'org-caption-shortn shortn
1949 'org-attributes attr
1950 'org-label label))
1951 (if label (push (cons label label) target-alist))
1952 (goto-char end)
1953 (setq cap nil shortn nil attr nil label nil)))))
1954 target-alist)
1956 (defun org-export-remove-comment-blocks-and-subtrees ()
1957 "Remove the comment environment, and also commented subtrees."
1958 (let ((re-commented (format org-heading-keyword-regexp-format
1959 org-comment-string))
1960 case-fold-search)
1961 ;; Remove comment environment
1962 (goto-char (point-min))
1963 (setq case-fold-search t)
1964 (while (re-search-forward
1965 "^#\\+begin_comment[ \t]*\n[^\000]*?\n#\\+end_comment\\>.*" nil t)
1966 (replace-match "" t t))
1967 ;; Remove subtrees that are commented
1968 (goto-char (point-min))
1969 (setq case-fold-search nil)
1970 (while (re-search-forward re-commented nil t)
1971 (goto-char (match-beginning 0))
1972 (delete-region (point) (org-end-of-subtree t)))))
1974 (defun org-export-handle-comments (org-commentsp)
1975 "Remove comments, or convert to backend-specific format.
1976 ORG-COMMENTSP can be a format string for publishing comments.
1977 When it is nil, all comments will be removed."
1978 (let ((re "^\\(#\\|[ \t]*#\\+ \\)\\(.*\n?\\)")
1979 pos)
1980 (goto-char (point-min))
1981 (while (or (looking-at re)
1982 (re-search-forward re nil t))
1983 (setq pos (match-beginning 0))
1984 (if (get-text-property pos 'org-protected)
1985 (goto-char (1+ pos))
1986 (if (and org-commentsp
1987 (not (equal (char-before (match-end 1)) ?+)))
1988 (progn (add-text-properties
1989 (match-beginning 0) (match-end 0) '(org-protected t))
1990 (replace-match (org-add-props
1991 (format org-commentsp (match-string 2))
1992 nil 'org-protected t)
1993 t t))
1994 (goto-char (1+ pos))
1995 (replace-match "")
1996 (goto-char (max (point-min) (1- pos))))))))
1998 (defun org-export-handle-table-metalines ()
1999 "Remove table specific metalines #+TBLNAME: and #+TBLFM:."
2000 (let ((re "^[ \t]*#\\+TBL\\(NAME\\|FM\\):\\(.*\n?\\)")
2001 pos)
2002 (goto-char (point-min))
2003 (while (or (looking-at re)
2004 (re-search-forward re nil t))
2005 (setq pos (match-beginning 0))
2006 (if (get-text-property (match-beginning 1) 'org-protected)
2007 (goto-char (1+ pos))
2008 (goto-char (1+ pos))
2009 (replace-match "")
2010 (goto-char (max (point-min) (1- pos)))))))
2012 (defun org-export-res/src-name-cleanup ()
2013 "Clean up #+results and #+name lines for export.
2014 This function should only be called after all block processing
2015 has taken place."
2016 (interactive)
2017 (save-excursion
2018 (goto-char (point-min))
2019 (let ((case-fold-search t))
2020 (while (org-re-search-forward-unprotected
2021 "#\\+\\(name\\|results\\(\\[[a-z0-9]+\\]\\)?\\):" nil t)
2022 (delete-region (match-beginning 0) (progn (forward-line) (point)))))))
2024 (defun org-export-mark-radio-links ()
2025 "Find all matches for radio targets and turn them into internal links."
2026 (let ((re-radio (and org-target-link-regexp
2027 (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)"))))
2028 (goto-char (point-min))
2029 (when re-radio
2030 (while (re-search-forward re-radio nil t)
2031 (unless
2032 (save-match-data
2033 (or (org-in-regexp org-bracket-link-regexp)
2034 (org-in-regexp org-plain-link-re)
2035 (org-in-regexp "<<[^<>]+>>")))
2036 (org-if-unprotected
2037 (replace-match "\\1[[\\2]]")))))))
2039 (defun org-store-forced-table-alignment ()
2040 "Find table lines which force alignment, store the results in properties."
2041 (let (line cnt cookies)
2042 (goto-char (point-min))
2043 (while (re-search-forward "|[ \t]*<\\([lrc]?[0-9]+\\|[lrc]\\)>[ \t]*|"
2044 nil t)
2045 ;; OK, this looks like a table line with an alignment cookie
2046 (org-if-unprotected
2047 (setq line (buffer-substring (point-at-bol) (point-at-eol)))
2048 (when (and (org-at-table-p)
2049 (org-table-cookie-line-p line))
2050 (setq cnt 0 cookies nil)
2051 (mapc
2052 (lambda (x)
2053 (setq cnt (1+ cnt))
2054 (when (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'" x)
2055 (let ((align (and (match-end 1)
2056 (downcase (match-string 1 x))))
2057 (width (and (match-end 2)
2058 (string-to-number (match-string 2 x)))))
2059 (push (cons cnt (list align width)) cookies))))
2060 (org-split-string line "[ \t]*|[ \t]*"))
2061 (add-text-properties (org-table-begin) (org-table-end)
2062 (list 'org-col-cookies cookies))))
2063 (goto-char (point-at-eol)))))
2065 (defun org-export-remove-special-table-lines ()
2066 "Remove tables lines that are used for internal purposes.
2067 Also, store forced alignment information found in such lines."
2068 (goto-char (point-min))
2069 (while (re-search-forward "^[ \t]*|" nil t)
2070 (org-if-unprotected-at (1- (point))
2071 (beginning-of-line 1)
2072 (if (or (looking-at "[ \t]*| *[!_^] *|")
2073 (not
2074 (memq
2076 (mapcar
2077 (lambda (f)
2078 (or (and org-export-table-remove-empty-lines (= (length f) 0))
2079 (string-match
2080 "\\`<\\([0-9]\\|[lrc]\\|[lrc][0-9]+\\)>\\'" f)))
2081 (org-split-string ;; FIXME, can't we do without splitting???
2082 (buffer-substring (point-at-bol) (point-at-eol))
2083 "[ \t]*|[ \t]*")))))
2084 (delete-region (max (point-min) (1- (point-at-bol)))
2085 (point-at-eol))
2086 (end-of-line 1)))))
2088 (defun org-export-protect-sub-super (s)
2089 (save-match-data
2090 (while (string-match "\\([^\\\\]\\)\\([_^]\\)" s)
2091 (setq s (replace-match "\\1\\\\\\2" nil nil s)))
2094 (defun org-export-normalize-links ()
2095 "Convert all links to bracket links, and expand link abbreviations."
2096 (let ((re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
2097 (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
2098 nodesc)
2099 (goto-char (point-min))
2100 (while (re-search-forward org-bracket-link-regexp nil t)
2101 (put-text-property (match-beginning 0) (match-end 0) 'org-normalized-link t))
2102 (goto-char (point-min))
2103 (while (re-search-forward re-plain-link nil t)
2104 (unless (get-text-property (match-beginning 0) 'org-normalized-link)
2105 (goto-char (1- (match-end 0)))
2106 (org-if-unprotected-at (1+ (match-beginning 0))
2107 (let* ((s (concat (match-string 1)
2108 "[[" (match-string 2) ":" (match-string 3)
2109 "][" (match-string 2) ":" (org-export-protect-sub-super
2110 (match-string 3))
2111 "]]")))
2112 ;; added 'org-link face to links
2113 (put-text-property 0 (length s) 'face 'org-link s)
2114 (replace-match s t t)))))
2115 (goto-char (point-min))
2116 (while (re-search-forward re-angle-link nil t)
2117 (goto-char (1- (match-end 0)))
2118 (org-if-unprotected
2119 (let* ((s (concat (match-string 1)
2120 "[[" (match-string 2) ":" (match-string 3)
2121 "][" (match-string 2) ":" (org-export-protect-sub-super
2122 (match-string 3))
2123 "]]")))
2124 (put-text-property 0 (length s) 'face 'org-link s)
2125 (replace-match s t t))))
2126 (goto-char (point-min))
2127 (while (re-search-forward org-bracket-link-regexp nil t)
2128 (goto-char (1- (match-end 0)))
2129 (setq nodesc (not (match-end 3)))
2130 (org-if-unprotected
2131 (let* ((xx (save-match-data
2132 (org-translate-link
2133 (org-link-expand-abbrev (match-string 1)))))
2134 (s (concat
2135 "[[" (org-add-props (copy-sequence xx)
2136 nil 'org-protected t 'org-no-description nodesc)
2138 (if (match-end 3)
2139 (match-string 2)
2140 (concat "[" (copy-sequence xx)
2141 "]"))
2142 "]")))
2143 (put-text-property 0 (length s) 'face 'org-link s)
2144 (replace-match s t t))))))
2146 (defun org-export-concatenate-multiline-links ()
2147 "Find multi-line links and put it all into a single line.
2148 This is to make sure that the line-processing export backends
2149 can work correctly."
2150 (goto-char (point-min))
2151 (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
2152 (org-if-unprotected-at (match-beginning 1)
2153 (replace-match "\\1 \\3")
2154 (goto-char (match-beginning 0)))))
2156 (defun org-export-concatenate-multiline-emphasis ()
2157 "Find multi-line emphasis and put it all into a single line.
2158 This is to make sure that the line-processing export backends
2159 can work correctly."
2160 (goto-char (point-min))
2161 (while (re-search-forward org-emph-re nil t)
2162 (if (and (not (= (char-after (match-beginning 3))
2163 (char-after (match-beginning 4))))
2164 (save-excursion (goto-char (match-beginning 0))
2165 (save-match-data
2166 (and (not (org-at-table-p))
2167 (not (org-at-heading-p))))))
2168 (org-if-unprotected
2169 (subst-char-in-region (match-beginning 0) (match-end 0)
2170 ?\n ?\ t)
2171 (goto-char (1- (match-end 0))))
2172 (goto-char (1+ (match-beginning 0))))))
2174 (defun org-export-grab-title-from-buffer ()
2175 "Get a title for the current document, from looking at the buffer."
2176 (let ((inhibit-read-only t))
2177 (save-excursion
2178 (goto-char (point-min))
2179 (let ((end (if (looking-at org-outline-regexp)
2180 (point)
2181 (save-excursion (outline-next-heading) (point)))))
2182 (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
2183 ;; Mark the line so that it will not be exported as normal text.
2184 (unless (org-in-block-p org-list-forbidden-blocks)
2185 (org-unmodified
2186 (add-text-properties (match-beginning 0) (match-end 0)
2187 (list :org-license-to-kill t))))
2188 ;; Return the title string
2189 (org-trim (match-string 0)))))))
2191 (defun org-export-get-title-from-subtree ()
2192 "Return subtree title and exclude it from export."
2193 (let ((rbeg (region-beginning)) (rend (region-end))
2194 (inhibit-read-only t)
2195 (tags (plist-get (org-infile-export-plist) :tags))
2196 title)
2197 (save-excursion
2198 (goto-char rbeg)
2199 (when (and (org-at-heading-p)
2200 (>= (org-end-of-subtree t t) rend))
2201 (when (plist-member org-export-opt-plist :tags)
2202 (setq tags (or (plist-get org-export-opt-plist :tags) tags)))
2203 ;; This is a subtree, we take the title from the first heading
2204 (goto-char rbeg)
2205 (looking-at org-todo-line-tags-regexp)
2206 (setq title (if (eq tags t)
2207 (format "%s\t%s" (match-string 3) (match-string 4))
2208 (match-string 3)))
2209 (org-unmodified
2210 (add-text-properties (point) (1+ (point-at-eol))
2211 (list :org-license-to-kill t)))
2212 (setq title (or (org-entry-get nil "EXPORT_TITLE") title))))
2213 title))
2215 (defun org-solidify-link-text (s &optional alist)
2216 "Take link text and make a safe target out of it."
2217 (save-match-data
2218 (let* ((rtn
2219 (mapconcat
2220 'identity
2221 (org-split-string s "[^a-zA-Z0-9_\\.-]+") "-"))
2222 (a (assoc rtn alist)))
2223 (or (cdr a) rtn))))
2225 (defun org-get-min-level (lines &optional offset)
2226 "Get the minimum level in LINES."
2227 (let ((re "^\\(\\*+\\) ") l)
2228 (catch 'exit
2229 (while (setq l (pop lines))
2230 (if (string-match re l)
2231 (throw 'exit (org-tr-level (- (length (match-string 1 l))
2232 (or offset 0))))))
2233 1)))
2235 ;; Variable holding the vector with section numbers
2236 (defvar org-section-numbers (make-vector org-level-max 0))
2238 (defun org-init-section-numbers ()
2239 "Initialize the vector for the section numbers."
2240 (let* ((level -1)
2241 (numbers (nreverse (org-split-string "" "\\.")))
2242 (depth (1- (length org-section-numbers)))
2243 (i depth) number-string)
2244 (while (>= i 0)
2245 (if (> i level)
2246 (aset org-section-numbers i 0)
2247 (setq number-string (or (car numbers) "0"))
2248 (if (string-match "\\`[A-Z]\\'" number-string)
2249 (aset org-section-numbers i
2250 (- (string-to-char number-string) ?A -1))
2251 (aset org-section-numbers i (string-to-number number-string)))
2252 (pop numbers))
2253 (setq i (1- i)))))
2255 (defun org-section-number (&optional level)
2256 "Return a string with the current section number.
2257 When LEVEL is non-nil, increase section numbers on that level."
2258 (let* ((depth (1- (length org-section-numbers)))
2259 (string "")
2260 (fmts (car org-export-section-number-format))
2261 (term (cdr org-export-section-number-format))
2262 (sep "")
2263 ctype fmt idx n)
2264 (when level
2265 (when (> level -1)
2266 (aset org-section-numbers
2267 level (1+ (aref org-section-numbers level))))
2268 (setq idx (1+ level))
2269 (while (<= idx depth)
2270 (if (not (= idx 1))
2271 (aset org-section-numbers idx 0))
2272 (setq idx (1+ idx))))
2273 (setq idx 0)
2274 (while (<= idx depth)
2275 (when (> (aref org-section-numbers idx) 0)
2276 (setq fmt (or (pop fmts) fmt)
2277 ctype (car fmt)
2278 n (aref org-section-numbers idx)
2279 string (if (> n 0)
2280 (concat string sep (org-number-to-counter n ctype))
2281 (concat string ".0"))
2282 sep (nth 1 fmt)))
2283 (setq idx (1+ idx)))
2284 (save-match-data
2285 (if (string-match "\\`\\([@0]\\.\\)+" string)
2286 (setq string (replace-match "" t nil string)))
2287 (if (string-match "\\(\\.0\\)+\\'" string)
2288 (setq string (replace-match "" t nil string))))
2289 (concat string term)))
2291 (defun org-number-to-counter (n type)
2292 "Concert number N to a string counter, according to TYPE.
2293 TYPE must be a string, any of:
2294 1 number
2295 A A,B,....
2296 a a,b,....
2297 I upper case roman numeral
2298 i lower case roman numeral"
2299 (cond
2300 ((equal type "1") (number-to-string n))
2301 ((equal type "A") (char-to-string (+ ?A n -1)))
2302 ((equal type "a") (char-to-string (+ ?a n -1)))
2303 ((equal type "I") (org-number-to-roman n))
2304 ((equal type "i") (downcase (org-number-to-roman n)))
2305 (t (error "Invalid counter type `%s'" type))))
2307 (defun org-number-to-roman (n)
2308 "Convert integer N into a roman numeral."
2309 (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
2310 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
2311 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
2312 ( 1 . "I")))
2313 (res ""))
2314 (if (<= n 0)
2315 (number-to-string n)
2316 (while roman
2317 (if (>= n (caar roman))
2318 (setq n (- n (caar roman))
2319 res (concat res (cdar roman)))
2320 (pop roman)))
2321 res)))
2323 ;;; Macros
2325 (defun org-export-preprocess-apply-macros ()
2326 "Replace macro references."
2327 (goto-char (point-min))
2328 (let (sy val key args args2 ind-str s n)
2329 (while (re-search-forward
2330 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
2331 nil t)
2332 (unless (save-match-data (save-excursion
2333 (goto-char (point-at-bol))
2334 (looking-at "[ \t]*#\\+macro")))
2335 ;; Get macro name (KEY), arguments (ARGS), and indentation of
2336 ;; current line (IND-STR) as strings.
2337 (setq key (downcase (match-string 1))
2338 args (match-string 3)
2339 ind-str (save-match-data (save-excursion
2340 (beginning-of-line)
2341 (looking-at "^\\([ \t]*\\).*")
2342 (match-string 1))))
2343 ;; When macro is defined, retrieve replacement text in VAL,
2344 ;; and proceed with expansion.
2345 (when (setq val (or (plist-get org-export-opt-plist
2346 (intern (concat ":macro-" key)))
2347 (plist-get org-export-opt-plist
2348 (intern (concat ":" key)))))
2349 (save-match-data
2350 ;; If arguments are provided, first retrieve them properly
2351 ;; (in ARGS, as a list), then replace them in VAL.
2352 (when args
2353 (setq args (org-split-string args ",") args2 nil)
2354 (while args
2355 (while (string-match "\\\\\\'" (car args))
2356 ;; Repair bad splits.
2357 (setcar (cdr args) (concat (substring (car args) 0 -1)
2358 "," (nth 1 args)))
2359 (pop args))
2360 (push (pop args) args2))
2361 (setq args (mapcar 'org-trim (nreverse args2)))
2362 (setq s 0)
2363 (while (string-match "\\$\\([0-9]+\\)" val s)
2364 (setq s (1+ (match-beginning 0))
2365 n (string-to-number (match-string 1 val)))
2366 (and (>= (length args) n)
2367 (setq val (replace-match (nth (1- n) args) t t val)))))
2368 ;; VAL starts with "(eval": it is a sexp, `eval' it.
2369 (when (string-match "\\`(eval\\>" val)
2370 (setq val (eval (read val))))
2371 ;; Ensure VAL is a string (or nil) and that each new line
2372 ;; is indented as the first one.
2373 (setq val (and val
2374 (mapconcat 'identity
2375 (org-split-string
2376 (if (stringp val) val (format "%s" val))
2377 "\n")
2378 (concat "\n" ind-str)))))
2379 ;; Eventually do the replacement, if VAL isn't nil. Move
2380 ;; point at beginning of macro for recursive expansions.
2381 (when val
2382 (replace-match val t t)
2383 (goto-char (match-beginning 0))))))))
2385 (defun org-export-apply-macros-in-string (s)
2386 "Apply the macros in string S."
2387 (when s
2388 (with-temp-buffer
2389 (insert s)
2390 (org-export-preprocess-apply-macros)
2391 (buffer-string))))
2393 ;;; Include files
2395 (defun org-export-handle-include-files ()
2396 "Include the contents of include files, with proper formatting."
2397 (let ((case-fold-search t)
2398 params file markup lang start end prefix prefix1 switches all minlevel lines)
2399 (goto-char (point-min))
2400 (while (re-search-forward "^#\\+INCLUDE:?[ \t]+\\(.*\\)" nil t)
2401 (setq params (read (concat "(" (match-string 1) ")"))
2402 prefix (org-get-and-remove-property 'params :prefix)
2403 prefix1 (org-get-and-remove-property 'params :prefix1)
2404 minlevel (org-get-and-remove-property 'params :minlevel)
2405 lines (org-get-and-remove-property 'params :lines)
2406 file (org-symname-or-string (pop params))
2407 markup (org-symname-or-string (pop params))
2408 lang (and (member markup '("src" "SRC"))
2409 (org-symname-or-string (pop params)))
2410 switches (mapconcat #'(lambda (x) (format "%s" x)) params " ")
2411 start nil end nil)
2412 (delete-region (match-beginning 0) (match-end 0))
2413 (if (or (not file)
2414 (not (file-exists-p file))
2415 (not (file-readable-p file)))
2416 (insert (format "CANNOT INCLUDE FILE %s" file))
2417 (setq all (cons file all))
2418 (when markup
2419 (if (equal (downcase markup) "src")
2420 (setq start (format "#+begin_src %s %s\n"
2421 (or lang "fundamental")
2422 (or switches ""))
2423 end "#+end_src")
2424 (setq start (format "#+begin_%s %s\n" markup switches)
2425 end (format "#+end_%s" markup))))
2426 (insert (or start ""))
2427 (insert (org-get-file-contents (expand-file-name file)
2428 prefix prefix1 markup minlevel lines))
2429 (or (bolp) (newline))
2430 (insert (or end ""))))
2431 all))
2433 (defun org-export-handle-include-files-recurse ()
2434 "Recursively include files aborting on circular inclusion."
2435 (let ((now (list org-current-export-file)) all)
2436 (while now
2437 (setq all (append now all))
2438 (setq now (org-export-handle-include-files))
2439 (let ((intersection
2440 (delq nil
2441 (mapcar (lambda (el) (when (member el all) el)) now))))
2442 (when intersection
2443 (error "Recursive #+INCLUDE: %S" intersection))))))
2445 (defun org-get-file-contents (file &optional prefix prefix1 markup minlevel lines)
2446 "Get the contents of FILE and return them as a string.
2447 If PREFIX is a string, prepend it to each line. If PREFIX1
2448 is a string, prepend it to the first line instead of PREFIX.
2449 If MARKUP, don't protect org-like lines, the exporter will
2450 take care of the block they are in. If LINES is a string
2451 specifying a range of lines, include only those lines ."
2452 (if (stringp markup) (setq markup (downcase markup)))
2453 (with-temp-buffer
2454 (insert-file-contents file)
2455 (when lines
2456 (let* ((lines (split-string lines "-"))
2457 (lbeg (string-to-number (car lines)))
2458 (lend (string-to-number (cadr lines)))
2459 (beg (if (zerop lbeg) (point-min)
2460 (goto-char (point-min))
2461 (forward-line (1- lbeg))
2462 (point)))
2463 (end (if (zerop lend) (point-max)
2464 (goto-char (point-min))
2465 (forward-line (1- lend))
2466 (point))))
2467 (narrow-to-region beg end)))
2468 (when (or prefix prefix1)
2469 (goto-char (point-min))
2470 (while (not (eobp))
2471 (insert (or prefix1 prefix))
2472 (setq prefix1 "")
2473 (beginning-of-line 2)))
2474 (buffer-string)
2475 (when (member markup '("src" "example"))
2476 (goto-char (point-min))
2477 (while (re-search-forward "^\\([*#]\\|[ \t]*#\\+\\)" nil t)
2478 (goto-char (match-beginning 0))
2479 (insert ",")
2480 (end-of-line 1)))
2481 (when minlevel
2482 (dotimes (lvl minlevel)
2483 (org-map-region 'org-demote (point-min) (point-max))))
2484 (buffer-string)))
2486 (defun org-get-and-remove-property (listvar prop)
2487 "Check if the value of LISTVAR contains PROP as a property.
2488 If yes, return the value of that property (i.e. the element following
2489 in the list) and remove property and value from the list in LISTVAR."
2490 (let ((list (symbol-value listvar)) m v)
2491 (when (setq m (member prop list))
2492 (setq v (nth 1 m))
2493 (if (equal (car list) prop)
2494 (set listvar (cddr list))
2495 (setcdr (nthcdr (- (length list) (length m) 1) list)
2496 (cddr m))
2497 (set listvar list)))
2500 (defun org-symname-or-string (s)
2501 (if (symbolp s)
2502 (if s (symbol-name s) s)
2505 ;;; Fontification and line numbers for code examples
2507 (defvar org-export-last-code-line-counter-value 0)
2509 (defun org-export-replace-src-segments-and-examples ()
2510 "Replace source code segments with special code for export."
2511 (setq org-export-last-code-line-counter-value 0)
2512 (let ((case-fold-search t)
2513 lang code trans opts indent caption)
2514 (goto-char (point-min))
2515 (while (re-search-forward
2516 "\\(^\\([ \t]*\\)#\\+BEGIN_SRC:?\\([ \t]+\\([^ \t\n]+\\)\\)?\\(.*\\)\n\\([^\000]+?\n\\)[ \t]*#\\+END_SRC.*\n?\\)\\|\\(^\\([ \t]*\\)#\\+BEGIN_EXAMPLE:?\\(?:[ \t]+\\(.*\\)\\)?\n\\([^\000]+?\n\\)[ \t]*#\\+END_EXAMPLE.*\n?\\)"
2517 nil t)
2518 (if (match-end 1)
2519 (if (not (match-string 4))
2520 (error "Source block missing language specification: %s"
2521 (let* ((body (match-string 6))
2522 (nothing (message "body:%s" body))
2523 (preview (or (and (string-match
2524 "^[ \t]*\\([^\n\r]*\\)" body)
2525 (match-string 1 body)) body)))
2526 (if (> (length preview) 35)
2527 (concat (substring preview 0 32) "...")
2528 preview)))
2529 ;; src segments
2530 (setq lang (match-string 4)
2531 opts (match-string 5)
2532 code (match-string 6)
2533 indent (length (match-string 2))
2534 caption (get-text-property 0 'org-caption (match-string 0))))
2535 (setq lang nil
2536 opts (match-string 9)
2537 code (match-string 10)
2538 indent (length (match-string 8))
2539 caption (get-text-property 0 'org-caption (match-string 0))))
2541 (setq trans (org-export-format-source-code-or-example
2542 lang code opts indent caption))
2543 (replace-match trans t t))))
2545 (defvar org-export-latex-verbatim-wrap) ;; defined in org-latex.el
2546 (defvar org-export-latex-listings) ;; defined in org-latex.el
2547 (defvar org-export-latex-listings-langs) ;; defined in org-latex.el
2548 (defvar org-export-latex-listings-w-names) ;; defined in org-latex.el
2549 (defvar org-export-latex-minted-langs) ;; defined in org-latex.el
2550 (defvar org-export-latex-custom-lang-environments) ;; defined in org-latex.el
2551 (defvar org-export-latex-listings-options) ;; defined in org-latex.el
2552 (defvar org-export-latex-minted-options) ;; defined in org-latex.el
2554 (defun org-remove-formatting-on-newlines-in-region (beg end)
2555 "Remove formatting on newline characters"
2556 (interactive "r")
2557 (save-excursion
2558 (goto-char beg)
2559 (while (progn (end-of-line) (< (point) end))
2560 (put-text-property (point) (1+ (point)) 'face nil)
2561 (forward-char 1))))
2563 (defun org-export-format-source-code-or-example
2564 (lang code &optional opts indent caption)
2565 "Format CODE from language LANG and return it formatted for export.
2566 The CODE is marked up in `org-export-current-backend' format.
2568 Check if a function by name
2569 \"org-<backend>-format-source-code-or-example\" is bound. If yes,
2570 use it as the custom formatter. Otherwise, use the default
2571 formatter. Default formatters are provided for docbook, html,
2572 latex and ascii backends. For example, use
2573 `org-html-format-source-code-or-example' to provide a custom
2574 formatter for export to \"html\".
2576 If LANG is nil, do not add any fontification.
2577 OPTS contains formatting options, like `-n' for triggering numbering lines,
2578 and `+n' for continuing previous numbering.
2579 Code formatting according to language currently only works for HTML.
2580 Numbering lines works for all three major backends (html, latex, and ascii).
2581 INDENT was the original indentation of the block."
2582 (save-match-data
2583 (let* ((backend-name (symbol-name org-export-current-backend))
2584 (backend-formatter
2585 (intern (format "org-%s-format-source-code-or-example"
2586 backend-name)))
2587 (backend-feature (intern (concat "org-" backend-name)))
2588 (backend-formatter
2589 (and (require (intern (concat "org-" backend-name)) nil)
2590 (fboundp backend-formatter) backend-formatter))
2591 num cont rtn rpllbl keepp textareap preserve-indentp cols rows fmt)
2592 (setq opts (or opts "")
2593 num (string-match "[-+]n\\>" opts)
2594 cont (string-match "\\+n\\>" opts)
2595 rpllbl (string-match "-r\\>" opts)
2596 keepp (string-match "-k\\>" opts)
2597 textareap (string-match "-t\\>" opts)
2598 preserve-indentp (or org-src-preserve-indentation
2599 (string-match "-i\\>" opts))
2600 cols (if (string-match "-w[ \t]+\\([0-9]+\\)" opts)
2601 (string-to-number (match-string 1 opts))
2603 rows (if (string-match "-h[ \t]+\\([0-9]+\\)" opts)
2604 (string-to-number (match-string 1 opts))
2605 (org-count-lines code))
2606 fmt (if (string-match "-l[ \t]+\"\\([^\"\n]+\\)\"" opts)
2607 (match-string 1 opts)))
2608 (when (and textareap (eq org-export-current-backend 'html))
2609 ;; we cannot use numbering or highlighting.
2610 (setq num nil cont nil lang nil))
2611 (if keepp (setq rpllbl 'keep))
2612 (setq rtn (if preserve-indentp code (org-remove-indentation code)))
2613 (when (string-match "^," rtn)
2614 (setq rtn (with-temp-buffer
2615 (insert rtn)
2616 ;; Free up the protected lines
2617 (goto-char (point-min))
2618 (while (re-search-forward "^," nil t)
2619 (if (or (equal lang "org")
2620 (save-match-data
2621 (looking-at "\\([*#]\\|[ \t]*#\\+\\)")))
2622 (replace-match ""))
2623 (end-of-line 1))
2624 (buffer-string))))
2625 ;; Now backend-specific coding
2626 (setq rtn
2627 (cond
2628 (backend-formatter
2629 (funcall backend-formatter rtn lang caption textareap cols rows num
2630 cont rpllbl fmt))
2631 ((eq org-export-current-backend 'docbook)
2632 (setq rtn (org-export-number-lines rtn 0 0 num cont rpllbl fmt))
2633 (concat "<programlisting><![CDATA["
2635 "]]></programlisting>\n"))
2636 ((eq org-export-current-backend 'html)
2637 ;; We are exporting to HTML
2638 (when lang
2639 (if (featurep 'xemacs)
2640 (require 'htmlize)
2641 (require 'htmlize nil t))
2642 (when (not (fboundp 'htmlize-region-for-paste))
2643 ;; we do not have htmlize.el, or an old version of it
2644 (setq lang nil)
2645 (message
2646 "htmlize.el 1.34 or later is needed for source code formatting")))
2648 (if lang
2649 (let* ((lang-m (when lang
2650 (or (cdr (assoc lang org-src-lang-modes))
2651 lang)))
2652 (mode (and lang-m (intern
2653 (concat
2654 (if (symbolp lang-m)
2655 (symbol-name lang-m)
2656 lang-m)
2657 "-mode"))))
2658 (org-inhibit-startup t)
2659 (org-startup-folded nil))
2660 (setq rtn
2661 (with-temp-buffer
2662 (insert rtn)
2663 (if (functionp mode)
2664 (funcall mode)
2665 (fundamental-mode))
2666 (font-lock-fontify-buffer)
2667 ;; markup each line separately
2668 (org-remove-formatting-on-newlines-in-region (point-min) (point-max))
2669 (org-src-mode)
2670 (set-buffer-modified-p nil)
2671 (org-export-htmlize-region-for-paste
2672 (point-min) (point-max))))
2673 (if (string-match "<pre\\([^>]*\\)>\n*" rtn)
2674 (setq rtn
2675 (concat
2676 (if caption
2677 (concat
2678 "<div class=\"org-src-container\">"
2679 (format
2680 "<label class=\"org-src-name\">%s</label>"
2681 caption))
2683 (replace-match
2684 (format "<pre class=\"src src-%s\">\n" lang)
2685 t t rtn)
2686 (if caption "</div>" "")))))
2687 (if textareap
2688 (setq rtn (concat
2689 (format "<p>\n<textarea cols=\"%d\" rows=\"%d\">"
2690 cols rows)
2691 rtn "</textarea>\n</p>\n"))
2692 (with-temp-buffer
2693 (insert rtn)
2694 (goto-char (point-min))
2695 (while (re-search-forward "[<>&]" nil t)
2696 (replace-match (cdr (assq (char-before)
2697 '((?&."&amp;")(?<."&lt;")(?>."&gt;"))))
2698 t t))
2699 (setq rtn (buffer-string)))
2700 (setq rtn (concat "<pre class=\"example\">\n" rtn "</pre>\n"))))
2701 (unless textareap
2702 (setq rtn (org-export-number-lines rtn 1 1 num cont rpllbl fmt)))
2703 (if (string-match "\\(\\`<[^>]*>\\)\n" rtn)
2704 (setq rtn (replace-match "\\1" t nil rtn)))
2705 rtn)
2706 ((eq org-export-current-backend 'latex)
2707 (setq rtn (org-export-number-lines rtn 0 0 num cont rpllbl fmt))
2708 (cond
2709 ((and lang org-export-latex-listings)
2710 (flet ((make-option-string
2711 (pair)
2712 (concat (first pair)
2713 (if (> (length (second pair)) 0)
2714 (concat "=" (second pair))))))
2715 (let* ((lang-sym (intern lang))
2716 (minted-p (eq org-export-latex-listings 'minted))
2717 (listings-p (not minted-p))
2718 (backend-lang
2719 (or (cadr
2720 (assq
2721 lang-sym
2722 (cond
2723 (minted-p org-export-latex-minted-langs)
2724 (listings-p org-export-latex-listings-langs))))
2725 lang))
2726 (custom-environment
2727 (cadr
2728 (assq
2729 lang-sym
2730 org-export-latex-custom-lang-environments))))
2731 (concat
2732 (when (and listings-p (not custom-environment))
2733 (format
2734 "\\lstset{%s}\n"
2735 (mapconcat
2736 #'make-option-string
2737 (append org-export-latex-listings-options
2738 `(("language" ,backend-lang))) ",")))
2739 (when (and caption org-export-latex-listings-w-names)
2740 (format
2741 "\n%s $\\equiv$ \n"
2742 (replace-regexp-in-string "_" "\\\\_" caption)))
2743 (cond
2744 (custom-environment
2745 (format "\\begin{%s}\n%s\\end{%s}\n"
2746 custom-environment rtn custom-environment))
2747 (listings-p
2748 (format "\\begin{%s}\n%s\\end{%s}"
2749 "lstlisting" rtn "lstlisting"))
2750 (minted-p
2751 (format
2752 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2753 (mapconcat #'make-option-string
2754 org-export-latex-minted-options ",")
2755 backend-lang rtn)))))))
2756 (t (concat (car org-export-latex-verbatim-wrap)
2757 rtn (cdr org-export-latex-verbatim-wrap)))))
2758 ((eq org-export-current-backend 'ascii)
2759 ;; This is not HTML or LaTeX, so just make it an example.
2760 (setq rtn (org-export-number-lines rtn 0 0 num cont rpllbl fmt))
2761 (concat caption "\n"
2762 (concat
2763 (mapconcat
2764 (lambda (l) (concat " " l))
2765 (org-split-string rtn "\n")
2766 "\n")
2767 "\n")
2770 (error "Don't know how to markup source or example block in %s"
2771 (upcase backend-name)))))
2772 (setq rtn
2773 (concat
2774 "\n#+BEGIN_" backend-name "\n"
2775 (org-add-props rtn
2776 '(org-protected t org-example t org-native-text t))
2777 "\n#+END_" backend-name "\n"))
2778 (org-add-props rtn nil 'original-indentation indent))))
2780 (defun org-export-number-lines (text &optional skip1 skip2 number cont
2781 replace-labels label-format preprocess)
2782 "Apply line numbers to literal examples and handle code references.
2783 Handle user-specified options under info node `(org)Literal
2784 examples' and return the modified source block.
2786 TEXT contains the source or example block.
2788 SKIP1 and SKIP2 are the number of lines that are to be skipped at
2789 the beginning and end of TEXT. Use these to skip over
2790 backend-specific lines pre-pended or appended to the original
2791 source block.
2793 NUMBER is non-nil if the literal example specifies \"+n\" or
2794 \"-n\" switch. If NUMBER is non-nil add line numbers.
2796 CONT is non-nil if the literal example specifies \"+n\" switch.
2797 If CONT is nil, start numbering this block from 1. Otherwise
2798 continue numbering from the last numbered block.
2800 REPLACE-LABELS is dual-purpose.
2801 1. It controls the retention of labels in the exported block.
2802 2. It specifies in what manner the links (or references) to a
2803 labeled line be formatted.
2805 REPLACE-LABELS is the symbol `keep' if the literal example
2806 specifies \"-k\" option, is numeric if the literal example
2807 specifies \"-r\" option and is nil otherwise.
2809 Handle REPLACE-LABELS as below:
2810 - If nil, retain labels in the exported block and use
2811 user-provided labels for referencing the labeled lines.
2812 - If it is a number, remove labels in the exported block and use
2813 one of line numbers or labels for referencing labeled lines based
2814 on NUMBER option.
2815 - If it is a keep, retain labels in the exported block and use
2816 one of line numbers or labels for referencing labeled lines
2817 based on NUMBER option.
2819 LABEL-FORMAT is the value of \"-l\" switch associated with
2820 literal example. See `org-coderef-label-format'.
2822 PREPROCESS is intended for backend-agnostic handling of source
2823 block numbering. When non-nil do the following:
2824 - do not number the lines
2825 - always strip the labels from exported block
2826 - do not make the labeled line a target of an incoming link.
2827 Instead mark the labeled line with `org-coderef' property and
2828 store the label in it."
2829 (setq skip1 (or skip1 0) skip2 (or skip2 0))
2830 (if (and number (not cont)) (setq org-export-last-code-line-counter-value 0))
2831 (with-temp-buffer
2832 (insert text)
2833 (goto-char (point-max))
2834 (skip-chars-backward " \t\n\r")
2835 (delete-region (point) (point-max))
2836 (beginning-of-line (- 1 skip2))
2837 (let* ((last (org-current-line))
2838 (n org-export-last-code-line-counter-value)
2839 (nmax (+ n (- last skip1)))
2840 (fmt (format "%%%dd: " (length (number-to-string nmax))))
2842 (cond
2843 ((eq org-export-current-backend 'html) (format "<span class=\"linenr\">%s</span>"
2844 fmt))
2845 ((eq org-export-current-backend 'ascii) fmt)
2846 ((eq org-export-current-backend 'latex) fmt)
2847 ((eq org-export-current-backend 'docbook) fmt)
2848 (t "")))
2849 (label-format (or label-format org-coderef-label-format))
2850 (label-pre (if (string-match "%s" label-format)
2851 (substring label-format 0 (match-beginning 0))
2852 label-format))
2853 (label-post (if (string-match "%s" label-format)
2854 (substring label-format (match-end 0))
2855 ""))
2856 (lbl-re
2857 (concat
2858 ".*?\\S-.*?\\([ \t]*\\("
2859 (regexp-quote label-pre)
2860 "\\([-a-zA-Z0-9_ ]+\\)"
2861 (regexp-quote label-post)
2862 "\\)\\)"))
2863 ref)
2865 (org-goto-line (1+ skip1))
2866 (while (and (re-search-forward "^" nil t) (not (eobp)) (< n nmax))
2867 (when number (incf n))
2868 (if (or preprocess (not number))
2869 (forward-char 1)
2870 (insert (format fm n)))
2871 (when (looking-at lbl-re)
2872 (setq ref (match-string 3))
2873 (cond ((numberp replace-labels)
2874 ;; remove labels; use numbers for references when lines
2875 ;; are numbered, use labels otherwise
2876 (delete-region (match-beginning 1) (match-end 1))
2877 (push (cons ref (if (> n 0) n ref)) org-export-code-refs))
2878 ((eq replace-labels 'keep)
2879 ;; don't remove labels; use numbers for references when
2880 ;; lines are numbered, use labels otherwise
2881 (goto-char (match-beginning 2))
2882 (delete-region (match-beginning 2) (match-end 2))
2883 (unless preprocess
2884 (insert "(" ref ")"))
2885 (push (cons ref (if (> n 0) n (concat "(" ref ")")))
2886 org-export-code-refs))
2888 ;; don't remove labels and don't use numbers for
2889 ;; references
2890 (goto-char (match-beginning 2))
2891 (delete-region (match-beginning 2) (match-end 2))
2892 (unless preprocess
2893 (insert "(" ref ")"))
2894 (push (cons ref (concat "(" ref ")")) org-export-code-refs)))
2895 (when (and (eq org-export-current-backend 'html) (not preprocess))
2896 (save-excursion
2897 (beginning-of-line 1)
2898 (insert (format "<span id=\"coderef-%s\" class=\"coderef-off\">"
2899 ref))
2900 (end-of-line 1)
2901 (insert "</span>")))
2902 (when preprocess
2903 (add-text-properties
2904 (point-at-bol) (point-at-eol) (list 'org-coderef ref)))))
2905 (setq org-export-last-code-line-counter-value n)
2906 (goto-char (point-max))
2907 (newline)
2908 (buffer-string))))
2910 (defun org-search-todo-below (line lines level)
2911 "Search the subtree below LINE for any TODO entries."
2912 (let ((rest (cdr (memq line lines)))
2913 (re org-todo-line-regexp)
2914 line lv todo)
2915 (catch 'exit
2916 (while (setq line (pop rest))
2917 (if (string-match re line)
2918 (progn
2919 (setq lv (- (match-end 1) (match-beginning 1))
2920 todo (and (match-beginning 2)
2921 (not (member (match-string 2 line)
2922 org-done-keywords))))
2923 ; TODO, not DONE
2924 (if (<= lv level) (throw 'exit nil))
2925 (if todo (throw 'exit t))))))))
2927 ;;;###autoload
2928 (defun org-export-visible (type arg)
2929 "Create a copy of the visible part of the current buffer, and export it.
2930 The copy is created in a temporary buffer and removed after use.
2931 TYPE is the final key (as a string) that also selects the export command in
2932 the \\<org-mode-map>\\[org-export] export dispatcher.
2933 As a special case, if the you type SPC at the prompt, the temporary
2934 org-mode file will not be removed but presented to you so that you can
2935 continue to use it. The prefix arg ARG is passed through to the exporting
2936 command."
2937 (interactive
2938 (list (progn
2939 (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")
2940 (read-char-exclusive))
2941 current-prefix-arg))
2942 (if (not (member type '(?a ?n ?u ?\C-a ?b ?\C-b ?h ?D ?x ?\ ?l ?p ?d ?L ?H ?R)))
2943 (error "Invalid export key"))
2944 (let* ((binding (cdr (assoc type
2946 (?a . org-export-as-ascii)
2947 (?A . org-export-as-ascii-to-buffer)
2948 (?n . org-export-as-latin1)
2949 (?N . org-export-as-latin1-to-buffer)
2950 (?u . org-export-as-utf8)
2951 (?U . org-export-as-utf8-to-buffer)
2952 (?\C-a . org-export-as-ascii)
2953 (?b . org-export-as-html-and-open)
2954 (?\C-b . org-export-as-html-and-open)
2955 (?h . org-export-as-html)
2956 (?H . org-export-as-html-to-buffer)
2957 (?R . org-export-region-as-html)
2958 (?D . org-export-as-docbook)
2960 (?l . org-export-as-latex)
2961 (?p . org-export-as-pdf)
2962 (?d . org-export-as-pdf-and-open)
2963 (?L . org-export-as-latex-to-buffer)
2965 (?x . org-export-as-xoxo)))))
2966 (keepp (equal type ?\ ))
2967 (file buffer-file-name)
2968 (buffer (get-buffer-create "*Org Export Visible*"))
2969 s e)
2970 ;; Need to hack the drawers here.
2971 (save-excursion
2972 (goto-char (point-min))
2973 (while (re-search-forward org-drawer-regexp nil t)
2974 (goto-char (match-beginning 1))
2975 (or (outline-invisible-p) (org-flag-drawer nil))))
2976 (with-current-buffer buffer (erase-buffer))
2977 (save-excursion
2978 (setq s (goto-char (point-min)))
2979 (while (not (= (point) (point-max)))
2980 (goto-char (org-find-invisible))
2981 (append-to-buffer buffer s (point))
2982 (setq s (goto-char (org-find-visible))))
2983 (org-cycle-hide-drawers 'all)
2984 (goto-char (point-min))
2985 (unless keepp
2986 ;; Copy all comment lines to the end, to make sure #+ settings are
2987 ;; still available for the second export step. Kind of a hack, but
2988 ;; does do the trick.
2989 (if (looking-at "#[^\r\n]*")
2990 (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
2991 (when (re-search-forward "^\\*+[ \t]+" nil t)
2992 (while (re-search-backward "[\n\r]#[^\n\r]*" nil t)
2993 (append-to-buffer buffer (1+ (match-beginning 0))
2994 (min (point-max) (1+ (match-end 0)))))))
2995 (set-buffer buffer)
2996 (let ((buffer-file-name file)
2997 (org-inhibit-startup t))
2998 (org-mode)
2999 (show-all)
3000 (unless keepp (funcall binding arg))))
3001 (if (not keepp)
3002 (kill-buffer buffer)
3003 (switch-to-buffer-other-window buffer)
3004 (goto-char (point-min)))))
3006 (defvar org-export-htmlized-org-css-url) ;; defined in org-html.el
3008 (defun org-export-string (string fmt &optional dir)
3009 "Export STRING to FMT using existing export facilities.
3010 During export STRING is saved to a temporary file whose location
3011 could vary. Optional argument DIR can be used to force the
3012 directory in which the temporary file is created during export
3013 which can be useful for resolving relative paths. Dir defaults
3014 to the value of `temporary-file-directory'."
3015 (let ((temporary-file-directory (or dir temporary-file-directory))
3016 (tmp-file (make-temp-file "org-")))
3017 (unwind-protect
3018 (with-temp-buffer
3019 (insert string)
3020 (write-file tmp-file)
3021 (org-load-modules-maybe)
3022 (unless org-local-vars
3023 (setq org-local-vars (org-get-local-variables)))
3024 (eval ;; convert to fmt -- mimicking `org-run-like-in-org-mode'
3025 (list 'let org-local-vars
3026 (list (intern (format "org-export-as-%s" fmt))
3027 nil nil nil ''string t))))
3028 (delete-file tmp-file))))
3030 ;;;###autoload
3031 (defun org-export-as-org (arg &optional hidden ext-plist
3032 to-buffer body-only pub-dir)
3033 "Make a copy with not-exporting stuff removed.
3034 The purpose of this function is to provide a way to export the source
3035 Org file of a webpage in Org format, but with sensitive and/or irrelevant
3036 stuff removed. This command will remove the following:
3038 - archived trees (if the variable `org-export-with-archived-trees' is nil)
3039 - comment blocks and trees starting with the COMMENT keyword
3040 - only trees that are consistent with `org-export-select-tags'
3041 and `org-export-exclude-tags'.
3043 The only arguments that will be used are EXT-PLIST and PUB-DIR,
3044 all the others will be ignored (but are present so that the general
3045 mechanism to call publishing functions will work).
3047 EXT-PLIST is a property list with external parameters overriding
3048 org-mode's default settings, but still inferior to file-local
3049 settings. When PUB-DIR is set, use this as the publishing
3050 directory."
3051 (interactive "P")
3052 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
3053 ext-plist
3054 (org-infile-export-plist)))
3055 (bfname (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
3056 (filename (concat (file-name-as-directory
3057 (or pub-dir
3058 (org-export-directory :org opt-plist)))
3059 (file-name-sans-extension
3060 (file-name-nondirectory bfname))
3061 ".org"))
3062 (filename (and filename
3063 (if (equal (file-truename filename)
3064 (file-truename bfname))
3065 (concat (file-name-sans-extension filename)
3066 "-source."
3067 (file-name-extension filename))
3068 filename)))
3069 (backup-inhibited t)
3070 (buffer (find-file-noselect filename))
3071 (region (buffer-string))
3072 str-ret)
3073 (save-excursion
3074 (org-pop-to-buffer-same-window buffer)
3075 (erase-buffer)
3076 (insert region)
3077 (let ((org-inhibit-startup t)) (org-mode))
3078 (org-install-letbind)
3080 ;; Get rid of archived trees
3081 (org-export-remove-archived-trees (plist-get opt-plist :archived-trees))
3083 ;; Remove comment environment and comment subtrees
3084 (org-export-remove-comment-blocks-and-subtrees)
3086 ;; Get rid of excluded trees
3087 (org-export-handle-export-tags (plist-get opt-plist :select-tags)
3088 (plist-get opt-plist :exclude-tags))
3090 (when (or (plist-get opt-plist :plain-source)
3091 (not (or (plist-get opt-plist :plain-source)
3092 (plist-get opt-plist :htmlized-source))))
3093 ;; Either nothing special is requested (default call)
3094 ;; or the plain source is explicitly requested
3095 ;; so: save it
3096 (save-buffer))
3097 (when (plist-get opt-plist :htmlized-source)
3098 ;; Make the htmlized version
3099 (require 'htmlize)
3100 (require 'org-html)
3101 (font-lock-fontify-buffer)
3102 (let* ((htmlize-output-type 'css)
3103 (newbuf (htmlize-buffer)))
3104 (with-current-buffer newbuf
3105 (when org-export-htmlized-org-css-url
3106 (goto-char (point-min))
3107 (and (re-search-forward
3108 "<style type=\"text/css\">[^\000]*?\n[ \t]*</style>.*"
3109 nil t)
3110 (replace-match
3111 (format
3112 "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\">"
3113 org-export-htmlized-org-css-url)
3114 t t)))
3115 (write-file (concat filename ".html")))
3116 (kill-buffer newbuf)))
3117 (set-buffer-modified-p nil)
3118 (if (equal to-buffer 'string)
3119 (progn (setq str-ret (buffer-string))
3120 (kill-buffer (current-buffer))
3121 str-ret)
3122 (kill-buffer (current-buffer))))))
3124 (defvar org-archive-location) ;; gets loaded with the org-archive require.
3125 (defun org-get-current-options ()
3126 "Return a string with current options as keyword options.
3127 Does include HTML export options as well as TODO and CATEGORY stuff."
3128 (require 'org-archive)
3129 (format
3130 "#+TITLE: %s
3131 #+AUTHOR: %s
3132 #+EMAIL: %s
3133 #+DATE: %s
3134 #+DESCRIPTION:
3135 #+KEYWORDS:
3136 #+LANGUAGE: %s
3137 #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s -:%s f:%s *:%s <:%s
3138 #+OPTIONS: TeX:%s LaTeX:%s skip:%s d:%s todo:%s pri:%s tags:%s
3140 #+EXPORT_SELECT_TAGS: %s
3141 #+EXPORT_EXCLUDE_TAGS: %s
3142 #+LINK_UP: %s
3143 #+LINK_HOME: %s
3144 #+XSLT:
3145 #+CATEGORY: %s
3146 #+SEQ_TODO: %s
3147 #+TYP_TODO: %s
3148 #+PRIORITIES: %c %c %c
3149 #+DRAWERS: %s
3150 #+STARTUP: %s %s %s %s %s
3151 #+TAGS: %s
3152 #+FILETAGS: %s
3153 #+ARCHIVE: %s
3154 #+LINK: %s
3156 (buffer-name) (user-full-name) user-mail-address
3157 (format-time-string (substring (car org-time-stamp-formats) 1 -1))
3158 org-export-default-language
3159 org-export-headline-levels
3160 org-export-with-section-numbers
3161 org-export-with-toc
3162 org-export-preserve-breaks
3163 org-export-html-expand
3164 org-export-with-fixed-width
3165 org-export-with-tables
3166 org-export-with-sub-superscripts
3167 org-export-with-special-strings
3168 org-export-with-footnotes
3169 org-export-with-emphasize
3170 org-export-with-timestamps
3171 org-export-with-TeX-macros
3172 org-export-with-LaTeX-fragments
3173 org-export-skip-text-before-1st-heading
3174 org-export-with-drawers
3175 org-export-with-todo-keywords
3176 org-export-with-priority
3177 org-export-with-tags
3178 (if (featurep 'org-jsinfo) (org-infojs-options-inbuffer-template) "")
3179 (mapconcat 'identity org-export-select-tags " ")
3180 (mapconcat 'identity org-export-exclude-tags " ")
3181 org-export-html-link-up
3182 org-export-html-link-home
3183 (or (ignore-errors
3184 (file-name-sans-extension
3185 (file-name-nondirectory (buffer-file-name (buffer-base-buffer)))))
3186 "NOFILENAME")
3187 "TODO FEEDBACK VERIFY DONE"
3188 "Me Jason Marie DONE"
3189 org-highest-priority org-lowest-priority org-default-priority
3190 (mapconcat 'identity org-drawers " ")
3191 (cdr (assoc org-startup-folded
3192 '((nil . "showall") (t . "overview") (content . "content"))))
3193 (if org-odd-levels-only "odd" "oddeven")
3194 (if org-hide-leading-stars "hidestars" "showstars")
3195 (if org-startup-align-all-tables "align" "noalign")
3196 (cond ((eq org-log-done t) "logdone")
3197 ((equal org-log-done 'note) "lognotedone")
3198 ((not org-log-done) "nologdone"))
3199 (or (mapconcat (lambda (x)
3200 (cond
3201 ((equal :startgroup (car x)) "{")
3202 ((equal :endgroup (car x)) "}")
3203 ((equal :newline (car x)) "")
3204 ((cdr x) (format "%s(%c)" (car x) (cdr x)))
3205 (t (car x))))
3206 (or org-tag-alist (org-get-buffer-tags)) " ") "")
3207 (mapconcat 'identity org-file-tags " ")
3208 org-archive-location
3209 "org file:~/org/%s.org"
3212 ;;;###autoload
3213 (defun org-insert-export-options-template ()
3214 "Insert into the buffer a template with information for exporting."
3215 (interactive)
3216 (if (not (bolp)) (newline))
3217 (let ((s (org-get-current-options)))
3218 (and (string-match "#\\+CATEGORY" s)
3219 (setq s (substring s 0 (match-beginning 0))))
3220 (insert s)))
3222 (defvar org-table-colgroup-info nil)
3224 (defun org-table-clean-before-export (lines &optional maybe-quoted)
3225 "Check if the table has a marking column.
3226 If yes remove the column and the special lines."
3227 (setq org-table-colgroup-info nil)
3228 (if (memq nil
3229 (mapcar
3230 (lambda (x) (or (string-match "^[ \t]*|-" x)
3231 (string-match
3232 (if maybe-quoted
3233 "^[ \t]*| *\\\\?\\([\#!$*_^ /]\\) *|"
3234 "^[ \t]*| *\\([\#!$*_^ /]\\) *|")
3235 x)))
3236 lines))
3237 ;; No special marking column
3238 (progn
3239 (setq org-table-clean-did-remove-column nil)
3240 (delq nil
3241 (mapcar
3242 (lambda (x)
3243 (cond
3244 ((org-table-colgroup-line-p x)
3245 ;; This line contains colgroup info, extract it
3246 ;; and then discard the line
3247 (setq org-table-colgroup-info
3248 (mapcar (lambda (x)
3249 (cond ((member x '("<" "&lt;")) :start)
3250 ((member x '(">" "&gt;")) :end)
3251 ((member x '("<>" "&lt;&gt;")) :startend)
3252 (t nil)))
3253 (org-split-string x "[ \t]*|[ \t]*")))
3254 nil)
3255 ((org-table-cookie-line-p x)
3256 ;; This line contains formatting cookies, discard it
3257 nil)
3258 (t x)))
3259 lines)))
3260 ;; there is a special marking column
3261 (setq org-table-clean-did-remove-column t)
3262 (delq nil
3263 (mapcar
3264 (lambda (x)
3265 (cond
3266 ((org-table-colgroup-line-p x)
3267 ;; This line contains colgroup info, extract it
3268 ;; and then discard the line
3269 (setq org-table-colgroup-info
3270 (mapcar (lambda (x)
3271 (cond ((member x '("<" "&lt;")) :start)
3272 ((member x '(">" "&gt;")) :end)
3273 ((member x '("<>" "&lt;&gt;")) :startend)
3274 (t nil)))
3275 (cdr (org-split-string x "[ \t]*|[ \t]*"))))
3276 nil)
3277 ((org-table-cookie-line-p x)
3278 ;; This line contains formatting cookies, discard it
3279 nil)
3280 ((string-match "^[ \t]*| *[!_^/] *|" x)
3281 ;; ignore this line
3282 nil)
3283 ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
3284 (string-match "^\\([ \t]*\\)|[^|]*|" x))
3285 ;; remove the first column
3286 (replace-match "\\1|" t nil x))))
3287 lines))))
3289 (defun org-export-cleanup-toc-line (s)
3290 "Remove tags and timestamps from lines going into the toc."
3291 (when (memq org-export-with-tags '(not-in-toc nil))
3292 (if (string-match (org-re " +:[[:alnum:]_@#%:]+: *$") s)
3293 (setq s (replace-match "" t t s))))
3294 (when org-export-remove-timestamps-from-toc
3295 (while (string-match org-maybe-keyword-time-regexp s)
3296 (setq s (replace-match "" t t s))))
3297 (while (string-match org-bracket-link-regexp s)
3298 (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
3299 t t s)))
3300 (while (string-match "\\[\\([0-9]\\|fn:[^]]*\\)\\]" s)
3301 (setq s (replace-match "" t t s)))
3305 (defun org-get-text-property-any (pos prop &optional object)
3306 (or (get-text-property pos prop object)
3307 (and (setq pos (next-single-property-change pos prop object))
3308 (get-text-property pos prop object))))
3310 (defun org-export-get-coderef-format (path desc)
3311 (save-match-data
3312 (if (and desc (string-match
3313 (regexp-quote (concat "(" path ")"))
3314 desc))
3315 (replace-match "%s" t t desc)
3316 (or desc "%s"))))
3318 (defun org-export-push-to-kill-ring (format)
3319 "Push buffer content to kill ring.
3320 The depends on the variable `org-export-copy-to-kill-ring'."
3321 (when org-export-copy-to-kill-ring
3322 (org-kill-new (buffer-string))
3323 (when (fboundp 'x-set-selection)
3324 (ignore-errors (x-set-selection 'PRIMARY (buffer-string)))
3325 (ignore-errors (x-set-selection 'CLIPBOARD (buffer-string))))
3326 (message "%s export done, pushed to kill ring and clipboard" format)))
3328 (provide 'org-exp)
3330 ;;; org-exp.el ends here