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