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