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