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