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