org-babel-exp-lob-one-liners should not parse the entire buffer.
[org-mode.git] / lisp / org-exp.el
blob69f0280de82e291bef7f42b957720ec333dcbdd5
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" "LATEX_CLASS_OPTIONS"
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 latex-class-options 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 "LATEX_CLASS_OPTIONS")
774 (setq latex-class-options val))
775 ((string-equal key "TEXT")
776 (setq text (if text (concat text "\n" val) val)))
777 ((string-equal key "OPTIONS")
778 (setq options (concat val " " options)))
779 ((string-equal key "MATHJAX")
780 (setq mathjax (concat val " " mathjax)))
781 ((string-equal key "BIND")
782 (push (read (concat "(" val ")")) letbind))
783 ((string-equal key "XSLT")
784 (setq p (plist-put p :xslt val)))
785 ((string-equal key "LINK_UP")
786 (setq p (plist-put p :link-up val)))
787 ((string-equal key "LINK_HOME")
788 (setq p (plist-put p :link-home val)))
789 ((string-equal key "EXPORT_SELECT_TAGS")
790 (setq p (plist-put p :select-tags (org-split-string val))))
791 ((string-equal key "EXPORT_EXCLUDE_TAGS")
792 (setq p (plist-put p :exclude-tags (org-split-string val))))
793 ((string-equal key "MACRO")
794 (push val macros))
795 ((equal key "SETUPFILE")
796 (setq setup-file (org-remove-double-quotes (org-trim val))
797 ;; take care of recursive inclusion of setupfiles
798 setup-file (if (or (file-name-absolute-p val) (not setup-dir))
799 (expand-file-name setup-file)
800 (let ((default-directory setup-dir))
801 (expand-file-name setup-file))))
802 (setq setup-dir (file-name-directory setup-file))
803 (setq setup-contents (org-file-contents setup-file 'noerror))
804 (if (not ext-setup-or-nil)
805 (setq ext-setup-or-nil setup-contents start 0)
806 (setq ext-setup-or-nil
807 (concat (substring ext-setup-or-nil 0 start)
808 "\n" setup-contents "\n"
809 (substring ext-setup-or-nil start)))))))
810 (setq p (plist-put p :text text))
811 (when (and letbind (org-export-confirm-letbind))
812 (setq p (plist-put p :let-bind letbind)))
813 (when style (setq p (plist-put p :style-extra style)))
814 (when latex-header
815 (setq p (plist-put p :latex-header-extra (substring latex-header 1))))
816 (when latex-class
817 (setq p (plist-put p :latex-class latex-class)))
818 (when latex-class-options
819 (setq p (plist-put p :latex-class-options latex-class-options)))
820 (when options
821 (setq p (org-export-add-options-to-plist p options)))
822 (when mathjax
823 (setq p (plist-put p :mathjax mathjax)))
824 ;; Add macro definitions
825 (setq p (plist-put p :macro-date "(eval (format-time-string \"$1\"))"))
826 (setq p (plist-put p :macro-time "(eval (format-time-string \"$1\"))"))
827 (setq p (plist-put p :macro-property "(eval (org-entry-get nil \"$1\" 'selective))"))
828 (setq p (plist-put
829 p :macro-modification-time
830 (and (buffer-file-name)
831 (file-exists-p (buffer-file-name))
832 (concat
833 "(eval (format-time-string \"$1\" '"
834 (prin1-to-string (nth 5 (file-attributes
835 (buffer-file-name))))
836 "))"))))
837 (setq p (plist-put p :macro-input-file (and (buffer-file-name)
838 (file-name-nondirectory
839 (buffer-file-name)))))
840 (while (setq val (pop macros))
841 (when (string-match "^\\([-a-zA-Z0-9_]+\\)[ \t]+\\(.*?[ \t]*$\\)" val)
842 (setq p (plist-put
843 p (intern
844 (concat ":macro-" (downcase (match-string 1 val))))
845 (org-export-interpolate-newlines (match-string 2 val))))))
846 p))))
848 (defun org-export-interpolate-newlines (s)
849 (while (string-match "\\\\n" s)
850 (setq s (replace-match "\n" t t s)))
853 (defvar org-export-allow-BIND-local nil)
854 (defun org-export-confirm-letbind ()
855 "Can we use #+BIND values during export?
856 By default this will ask for confirmation by the user, to divert possible
857 security risks."
858 (cond
859 ((not org-export-allow-BIND) nil)
860 ((eq org-export-allow-BIND t) t)
861 ((local-variable-p 'org-export-allow-BIND-local (current-buffer))
862 org-export-allow-BIND-local)
863 (t (org-set-local 'org-export-allow-BIND-local
864 (yes-or-no-p "Allow BIND values in this buffer? ")))))
866 (defun org-install-letbind ()
867 "Install the values from #+BIND lines as local variables."
868 (let ((letbind (plist-get org-export-opt-plist :let-bind))
869 pair)
870 (while (setq pair (pop letbind))
871 (org-set-local (car pair) (nth 1 pair)))))
873 (defun org-export-add-options-to-plist (p options)
874 "Parse an OPTIONS line and set values in the property list P."
875 (let (o)
876 (when options
877 (let ((op org-export-plist-vars))
878 (while (setq o (pop op))
879 (if (and (nth 1 o)
880 (string-match (concat "\\(\\`\\|[ \t]\\)"
881 (regexp-quote (nth 1 o))
882 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
883 options))
884 (setq p (plist-put p (car o)
885 (car (read-from-string
886 (match-string 2 options))))))))))
889 (defun org-export-add-subtree-options (p pos)
890 "Add options in subtree at position POS to property list P."
891 (save-excursion
892 (goto-char pos)
893 (when (org-at-heading-p)
894 (let (a)
895 ;; This is actually read in `org-export-get-title-from-subtree'
896 ;; (when (setq a (org-entry-get pos "EXPORT_TITLE"))
897 ;; (setq p (plist-put p :title a)))
898 (when (setq a (org-entry-get pos "EXPORT_TEXT"))
899 (setq p (plist-put p :text a)))
900 (when (setq a (org-entry-get pos "EXPORT_AUTHOR"))
901 (setq p (plist-put p :author a)))
902 (when (setq a (org-entry-get pos "EXPORT_DATE"))
903 (setq p (plist-put p :date a)))
904 (when (setq a (org-entry-get pos "EXPORT_OPTIONS"))
905 (setq p (org-export-add-options-to-plist p a)))))
908 (defun org-export-directory (type plist)
909 (let* ((val (plist-get plist :publishing-directory))
910 (dir (if (listp val)
911 (or (cdr (assoc type val)) ".")
912 val)))
913 dir))
915 (defun org-export-process-option-filters (plist)
916 (let ((functions org-export-options-filters) f)
917 (while (setq f (pop functions))
918 (setq plist (funcall f plist))))
919 plist)
921 ;;;###autoload
922 (defun org-export (&optional arg)
923 "Export dispatcher for Org-mode.
924 When `org-export-run-in-background' is non-nil, try to run the command
925 in the background. This will be done only for commands that write
926 to a file. For details see the docstring of `org-export-run-in-background'.
928 The prefix argument ARG will be passed to the exporter. However, if
929 ARG is a double universal prefix \\[universal-argument] \\[universal-argument], \
930 that means to inverse the
931 value of `org-export-run-in-background'.
933 If `org-export-initial-scope' is set to 'subtree, try to export
934 the current subtree, otherwise try to export the whole buffer.
935 Pressing `1' will switch between these two options."
936 (interactive "P")
937 (let* ((bg (org-xor (equal arg '(16)) org-export-run-in-background))
938 (subtree-p (or (org-region-active-p)
939 (eq org-export-initial-scope 'subtree)))
940 (help "[t] insert the export option template
941 \[v] limit export to visible part of outline tree
942 \[1] switch buffer/subtree export
943 \[SPC] publish enclosing subtree (with LaTeX_CLASS or EXPORT_FILE_NAME prop)
945 \[a/n/u] export as ASCII/Latin-1/UTF-8 [A/N/U] to temporary buffer
947 \[h] export as HTML [H] to temporary buffer [R] export region
948 \[b] export as HTML and open in browser
950 \[l] export as LaTeX [L] to temporary buffer
951 \[p] export as LaTeX and process to PDF [d] ... and open PDF file
953 \[D] export as DocBook [V] export as DocBook, process to PDF, and open
955 \[o] export as OpenDocumentText [O] ... and open
957 \[j] export as TaskJuggler [J] ... and open
959 \[m] export as Freemind mind map
960 \[x] export as XOXO
961 \[g] export using Wes Hardaker's generic exporter
963 \[i] export current file as iCalendar file
964 \[I] export all agenda files as iCalendar files [c] ...as one combined file
966 \[F] publish current file [P] publish current project
967 \[X] publish a project... [E] publish every projects")
968 (cmds
969 '((?t org-insert-export-options-template nil)
970 (?v org-export-visible nil)
971 (?a org-export-as-ascii t)
972 (?A org-export-as-ascii-to-buffer t)
973 (?n org-export-as-latin1 t)
974 (?N org-export-as-latin1-to-buffer t)
975 (?u org-export-as-utf8 t)
976 (?U org-export-as-utf8-to-buffer t)
977 (?h org-export-as-html t)
978 (?b org-export-as-html-and-open t)
979 (?H org-export-as-html-to-buffer nil)
980 (?R org-export-region-as-html nil)
981 (?x org-export-as-xoxo t)
982 (?g org-export-generic t)
983 (?D org-export-as-docbook t)
984 (?V org-export-as-docbook-pdf-and-open t)
985 (?o org-export-as-odt t)
986 (?O org-export-as-odt-and-open t)
987 (?j org-export-as-taskjuggler t)
988 (?J org-export-as-taskjuggler-and-open t)
989 (?m org-export-as-freemind t)
990 (?l org-export-as-latex t)
991 (?p org-export-as-pdf t)
992 (?d org-export-as-pdf-and-open t)
993 (?L org-export-as-latex-to-buffer nil)
994 (?i org-export-icalendar-this-file t)
995 (?I org-export-icalendar-all-agenda-files t)
996 (?c org-export-icalendar-combine-agenda-files t)
997 (?F org-publish-current-file t)
998 (?P org-publish-current-project t)
999 (?X org-publish t)
1000 (?E org-publish-all t)))
1001 r1 r2 ass
1002 (cpos (point)) (cbuf (current-buffer)) bpos)
1003 (save-excursion
1004 (save-window-excursion
1005 (if subtree-p
1006 (message "Export subtree: ")
1007 (message "Export buffer: "))
1008 (delete-other-windows)
1009 (with-output-to-temp-buffer "*Org Export/Publishing Help*"
1010 (princ help))
1011 (org-fit-window-to-buffer (get-buffer-window
1012 "*Org Export/Publishing Help*"))
1013 (while (eq (setq r1 (read-char-exclusive)) ?1)
1014 (cond (subtree-p
1015 (setq subtree-p nil)
1016 (message "Export buffer: "))
1017 ((not subtree-p)
1018 (setq subtree-p t)
1019 (setq bpos (point))
1020 (message "Export subtree: "))))
1021 (when (eq r1 ?\ )
1022 (let ((case-fold-search t)
1023 (end (save-excursion (while (org-up-heading-safe)) (point))))
1024 (outline-next-heading)
1025 (if (re-search-backward
1026 "^[ \t]+\\(:latex_class:\\|:export_title:\\|:export_file_name:\\)[ \t]+\\S-"
1027 end t)
1028 (progn
1029 (org-back-to-heading t)
1030 (setq subtree-p t)
1031 (setq bpos (point))
1032 (message "Select command (for subtree): ")
1033 (setq r1 (read-char-exclusive)))
1034 (error "No enclosing node with LaTeX_CLASS or EXPORT_TITLE or EXPORT_FILE_NAME")
1035 )))))
1036 (if (fboundp 'redisplay) (redisplay)) ;; XEmacs does not have/need (redisplay)
1037 (and bpos (goto-char bpos))
1038 (setq r2 (if (< r1 27) (+ r1 96) r1))
1039 (unless (setq ass (assq r2 cmds))
1040 (error "No command associated with key %c" r1))
1041 (if (and bg (nth 2 ass)
1042 (not (buffer-base-buffer))
1043 (not (org-region-active-p)))
1044 ;; execute in background
1045 (let ((p (start-process
1046 (concat "Exporting " (file-name-nondirectory (buffer-file-name)))
1047 "*Org Processes*"
1048 (expand-file-name invocation-name invocation-directory)
1049 "-batch"
1050 "-l" user-init-file
1051 "--eval" "(require 'org-exp)"
1052 "--eval" "(setq org-wait .2)"
1053 (buffer-file-name)
1054 "-f" (symbol-name (nth 1 ass)))))
1055 (set-process-sentinel p 'org-export-process-sentinel)
1056 (message "Background process \"%s\": started" p))
1057 ;; background processing not requested, or not possible
1058 (if subtree-p (progn (org-mark-subtree) (org-activate-mark)))
1059 (call-interactively (nth 1 ass))
1060 (when (and bpos (get-buffer-window cbuf))
1061 (let ((cw (selected-window)))
1062 (select-window (get-buffer-window cbuf))
1063 (goto-char cpos)
1064 (deactivate-mark)
1065 (select-window cw))))))
1067 (defun org-export-process-sentinel (process status)
1068 (if (string-match "\n+\\'" status)
1069 (setq status (substring status 0 -1)))
1070 (message "Background process \"%s\": %s" process status))
1072 ;;; General functions for all backends
1074 (defvar org-export-target-aliases nil
1075 "Alist of targets with invisible aliases.")
1076 (defvar org-export-preferred-target-alist nil
1077 "Alist of section id's with preferred aliases.")
1078 (defvar org-export-id-target-alist nil
1079 "Alist of section id's with preferred aliases.")
1080 (defvar org-export-code-refs nil
1081 "Alist of code references and line numbers.")
1083 (defun org-export-preprocess-string (string &rest parameters)
1084 "Cleanup STRING so that the true exported has a more consistent source.
1085 This function takes STRING, which should be a buffer-string of an org-file
1086 to export. It then creates a temporary buffer where it does its job.
1087 The result is then again returned as a string, and the exporter works
1088 on this string to produce the exported version."
1089 (interactive)
1090 (let* ((org-export-current-backend (or (plist-get parameters :for-backend)
1091 org-export-current-backend))
1092 (archived-trees (plist-get parameters :archived-trees))
1093 (inhibit-read-only t)
1094 (drawers org-drawers)
1095 (source-buffer (current-buffer))
1096 target-alist rtn)
1098 (setq org-export-target-aliases nil
1099 org-export-preferred-target-alist nil
1100 org-export-id-target-alist nil
1101 org-export-code-refs nil)
1103 (with-temp-buffer
1104 (erase-buffer)
1105 (insert string)
1106 (setq case-fold-search t)
1108 (let ((inhibit-read-only t))
1109 (remove-text-properties (point-min) (point-max)
1110 '(read-only t)))
1112 ;; Remove license-to-kill stuff
1113 ;; The caller marks some stuff for killing, stuff that has been
1114 ;; used to create the page title, for example.
1115 (org-export-kill-licensed-text)
1117 (let ((org-inhibit-startup t)) (org-mode))
1118 (setq case-fold-search t)
1119 (org-clone-local-variables source-buffer "^\\(org-\\|orgtbl-\\)")
1120 (org-install-letbind)
1122 ;; Call the hook
1123 (run-hooks 'org-export-preprocess-hook)
1125 (untabify (point-min) (point-max))
1127 ;; Handle include files, and call a hook
1128 (org-export-handle-include-files-recurse)
1129 (run-hooks 'org-export-preprocess-after-include-files-hook)
1131 ;; Get rid of archived trees
1132 (org-export-remove-archived-trees archived-trees)
1134 ;; Remove comment environment and comment subtrees
1135 (org-export-remove-comment-blocks-and-subtrees)
1137 ;; Get rid of excluded trees, and call a hook
1138 (org-export-handle-export-tags (plist-get parameters :select-tags)
1139 (plist-get parameters :exclude-tags))
1140 (run-hooks 'org-export-preprocess-after-tree-selection-hook)
1142 ;; Get rid of tasks, depending on configuration
1143 (org-export-remove-tasks (plist-get parameters :tasks))
1145 ;; Prepare footnotes for export. During that process, footnotes
1146 ;; actually included in the exported part of the buffer go
1147 ;; though some transformations:
1149 ;; 1. They have their label normalized (like "[N]");
1151 ;; 2. They get moved at the same place in the buffer (usually at
1152 ;; its end, but backends may define another place via
1153 ;; `org-footnote-insert-pos-for-preprocessor');
1155 ;; 3. The are stored in `org-export-footnotes-seen', while
1156 ;; `org-export-preprocess-string' is applied to their
1157 ;; definition.
1159 ;; Line-wise exporters ignore `org-export-footnotes-seen', as
1160 ;; they interpret footnotes at the moment they see them in the
1161 ;; buffer. Context-wise exporters grab all the info needed in
1162 ;; that variable and delete moved definitions (as described in
1163 ;; 2nd step).
1164 (when (plist-get parameters :footnotes)
1165 (org-footnote-normalize nil parameters))
1167 ;; Change lists ending. Other parts of export may insert blank
1168 ;; lines and lists' structure could be altered.
1169 (org-export-mark-list-end)
1171 ;; Process the macros
1172 (org-export-preprocess-apply-macros)
1173 (run-hooks 'org-export-preprocess-after-macros-hook)
1175 ;; Export code blocks
1176 (org-export-blocks-preprocess)
1178 ;; Mark lists with properties
1179 (org-export-mark-list-properties)
1181 ;; Handle source code snippets
1182 (org-export-replace-src-segments-and-examples)
1184 ;; Protect short examples marked by a leading colon
1185 (org-export-protect-colon-examples)
1187 ;; Protected spaces
1188 (org-export-convert-protected-spaces)
1190 ;; Find all headings and compute the targets for them
1191 (setq target-alist (org-export-define-heading-targets target-alist))
1193 (run-hooks 'org-export-preprocess-after-headline-targets-hook)
1195 ;; Find HTML special classes for headlines
1196 (org-export-remember-html-container-classes)
1198 ;; Get rid of drawers
1199 (org-export-remove-or-extract-drawers
1200 drawers (plist-get parameters :drawers))
1202 ;; Get the correct stuff before the first headline
1203 (when (plist-get parameters :skip-before-1st-heading)
1204 (goto-char (point-min))
1205 (when (re-search-forward "^\\(#.*\n\\)?\\*+[ \t]" nil t)
1206 (delete-region (point-min) (match-beginning 0))
1207 (goto-char (point-min))
1208 (insert "\n")))
1209 (when (plist-get parameters :add-text)
1210 (goto-char (point-min))
1211 (insert (plist-get parameters :add-text) "\n"))
1213 ;; Remove todo-keywords before exporting, if the user has requested so
1214 (org-export-remove-headline-metadata parameters)
1216 ;; Find targets in comments and move them out of comments,
1217 ;; but mark them as targets that should be invisible
1218 (setq target-alist (org-export-handle-invisible-targets target-alist))
1220 ;; Select and protect backend specific stuff, throw away stuff
1221 ;; that is specific for other backends
1222 (run-hooks 'org-export-preprocess-before-selecting-backend-code-hook)
1223 (org-export-select-backend-specific-text)
1225 ;; Protect quoted subtrees
1226 (org-export-protect-quoted-subtrees)
1228 ;; Remove clock lines
1229 (org-export-remove-clock-lines)
1231 ;; Protect verbatim elements
1232 (org-export-protect-verbatim)
1234 ;; Blockquotes, verse, and center
1235 (org-export-mark-blockquote-verse-center)
1236 (run-hooks 'org-export-preprocess-after-blockquote-hook)
1238 ;; Remove timestamps, if the user has requested so
1239 (unless (plist-get parameters :timestamps)
1240 (org-export-remove-timestamps))
1242 ;; Attach captions to the correct object
1243 (setq target-alist (org-export-attach-captions-and-attributes target-alist))
1245 ;; Find matches for radio targets and turn them into internal links
1246 (org-export-mark-radio-links)
1247 (run-hooks 'org-export-preprocess-after-radio-targets-hook)
1249 ;; Find all links that contain a newline and put them into a single line
1250 (org-export-concatenate-multiline-links)
1252 ;; Normalize links: Convert angle and plain links into bracket links
1253 ;; and expand link abbreviations
1254 (run-hooks 'org-export-preprocess-before-normalizing-links-hook)
1255 (org-export-normalize-links)
1257 ;; Find all internal links. If they have a fuzzy match (i.e. not
1258 ;; a *dedicated* target match, let the link point to the
1259 ;; corresponding section.
1260 (org-export-target-internal-links target-alist)
1262 ;; Find multiline emphasis and put them into single line
1263 (when (plist-get parameters :emph-multiline)
1264 (org-export-concatenate-multiline-emphasis))
1266 ;; Remove special table lines, and store alignment information
1267 (org-store-forced-table-alignment)
1268 (when org-export-table-remove-special-lines
1269 (org-export-remove-special-table-lines))
1271 ;; Another hook
1272 (run-hooks 'org-export-preprocess-before-backend-specifics-hook)
1274 ;; Backend-specific preprocessing
1275 (let* ((backend-name (symbol-name org-export-current-backend))
1276 (f (intern (format "org-export-%s-preprocess" backend-name))))
1277 (require (intern (concat "org-" backend-name)) nil)
1278 (funcall f parameters))
1280 ;; Remove or replace comments
1281 (org-export-handle-comments (plist-get parameters :comments))
1283 ;; Remove #+TBLFM and #+TBLNAME lines
1284 (org-export-handle-table-metalines)
1286 ;; Run the final hook
1287 (run-hooks 'org-export-preprocess-final-hook)
1289 (setq rtn (buffer-string)))
1290 rtn))
1292 (defun org-export-kill-licensed-text ()
1293 "Remove all text that is marked with a :org-license-to-kill property."
1294 (let (p)
1295 (while (setq p (text-property-any (point-min) (point-max)
1296 :org-license-to-kill t))
1297 (delete-region
1298 p (or (next-single-property-change p :org-license-to-kill)
1299 (point-max))))))
1301 (defvar org-export-define-heading-targets-headline-hook nil
1302 "Hook that is run when a headline was matched during target search.
1303 This is part of the preprocessing for export.")
1305 (defun org-export-define-heading-targets (target-alist)
1306 "Find all headings and define the targets for them.
1307 The new targets are added to TARGET-ALIST, which is also returned.
1308 Also find all ID and CUSTOM_ID properties and store them."
1309 (goto-char (point-min))
1310 (org-init-section-numbers)
1311 (let ((re (concat "^" org-outline-regexp
1312 "\\|"
1313 "^[ \t]*:\\(ID\\|CUSTOM_ID\\):[ \t]*\\([^ \t\r\n]+\\)"))
1314 level target last-section-target a id)
1315 (while (re-search-forward re nil t)
1316 (org-if-unprotected-at (match-beginning 0)
1317 (if (match-end 2)
1318 (progn
1319 (setq id (org-match-string-no-properties 2))
1320 (push (cons id target) target-alist)
1321 (setq a (or (assoc last-section-target org-export-target-aliases)
1322 (progn
1323 (push (list last-section-target)
1324 org-export-target-aliases)
1325 (car org-export-target-aliases))))
1326 (push (caar target-alist) (cdr a))
1327 (when (equal (match-string 1) "CUSTOM_ID")
1328 (if (not (assoc last-section-target
1329 org-export-preferred-target-alist))
1330 (push (cons last-section-target id)
1331 org-export-preferred-target-alist)))
1332 (when (equal (match-string 1) "ID")
1333 (if (not (assoc last-section-target
1334 org-export-id-target-alist))
1335 (push (cons last-section-target (concat "ID-" id))
1336 org-export-id-target-alist))))
1337 (setq level (org-reduced-level
1338 (save-excursion (goto-char (point-at-bol))
1339 (org-outline-level))))
1340 (setq target (org-solidify-link-text
1341 (format "sec-%s" (replace-regexp-in-string
1342 "\\." "-"
1343 (org-section-number level)))))
1344 (setq last-section-target target)
1345 (push (cons target target) target-alist)
1346 (add-text-properties
1347 (point-at-bol) (point-at-eol)
1348 (list 'target target))
1349 (run-hooks 'org-export-define-heading-targets-headline-hook)))))
1350 target-alist)
1352 (defun org-export-handle-invisible-targets (target-alist)
1353 "Find targets in comments and move them out of comments.
1354 Mark them as invisible targets."
1355 (let (target tmp a)
1356 (goto-char (point-min))
1357 (while (re-search-forward "^#.*?\\(<<<?\\([^>\r\n]+\\)>>>?\\).*" nil t)
1358 ;; Check if the line before or after is a headline with a target
1359 (if (setq target (or (get-text-property (point-at-bol 0) 'target)
1360 (get-text-property (point-at-bol 2) 'target)))
1361 (progn
1362 ;; use the existing target in a neighboring line
1363 (setq tmp (match-string 2))
1364 (replace-match "")
1365 (and (looking-at "\n") (delete-char 1))
1366 (push (cons (setq tmp (org-solidify-link-text tmp)) target)
1367 target-alist)
1368 (setq a (or (assoc target org-export-target-aliases)
1369 (progn
1370 (push (list target) org-export-target-aliases)
1371 (car org-export-target-aliases))))
1372 (push tmp (cdr a)))
1373 ;; Make an invisible target
1374 (replace-match "\\1(INVISIBLE)"))))
1375 target-alist)
1377 (defun org-export-target-internal-links (target-alist)
1378 "Find all internal links and assign targets to them.
1379 If a link has a fuzzy match (i.e. not a *dedicated* target match),
1380 let the link point to the corresponding section.
1381 This function also handles the id links, if they have a match in
1382 the current file."
1383 (goto-char (point-min))
1384 (while (re-search-forward org-bracket-link-regexp nil t)
1385 (org-if-unprotected-at (1+ (match-beginning 0))
1386 (let* ((org-link-search-must-match-exact-headline t)
1387 (md (match-data))
1388 (desc (match-end 2))
1389 (link (org-link-unescape (match-string 1)))
1390 (slink (org-solidify-link-text link))
1391 found props pos cref
1392 (target
1393 (cond
1394 ((= (string-to-char link) ?#)
1395 ;; user wants exactly this link
1396 link)
1397 ((cdr (assoc slink target-alist))
1398 (or (cdr (assoc (assoc slink target-alist)
1399 org-export-preferred-target-alist))
1400 (cdr (assoc slink target-alist))))
1401 ((and (string-match "^id:" link)
1402 (cdr (assoc (substring link 3) target-alist))))
1403 ((string-match "^(\\(.*\\))$" link)
1404 (setq cref (match-string 1 link))
1405 (concat "coderef:" cref))
1406 ((string-match org-link-types-re link) nil)
1407 ((or (file-name-absolute-p link)
1408 (string-match "^\\." link))
1409 nil)
1411 (let ((org-link-search-inhibit-query t))
1412 (save-excursion
1413 (setq found (condition-case nil (org-link-search link)
1414 (error nil)))
1415 (when (and found
1416 (or (org-on-heading-p)
1417 (not (eq found 'dedicated))))
1418 (or (get-text-property (point) 'target)
1419 (get-text-property
1420 (max (point-min)
1421 (1- (or (previous-single-property-change
1422 (point) 'target) 0)))
1423 'target)))))))))
1424 (when target
1425 (set-match-data md)
1426 (goto-char (match-beginning 1))
1427 (setq props (text-properties-at (point)))
1428 (delete-region (match-beginning 1) (match-end 1))
1429 (setq pos (point))
1430 (insert target)
1431 (unless desc (insert "][" link))
1432 (add-text-properties pos (point) props))))))
1434 (defun org-export-remember-html-container-classes ()
1435 "Store the HTML_CONTAINER_CLASS properties in a text property."
1436 (goto-char (point-min))
1437 (let (class)
1438 (while (re-search-forward
1439 "^[ \t]*:HTML_CONTAINER_CLASS:[ \t]+\\(.+\\)$" nil t)
1440 (setq class (match-string 1))
1441 (save-excursion
1442 (org-back-to-heading t)
1443 (put-text-property (point-at-bol) (point-at-eol) 'html-container-class class)))))
1445 (defvar org-export-format-drawer-function nil
1446 "Function to be called to format the contents of a drawer.
1447 The function must accept two parameters:
1448 NAME the drawer name, like \"PROPERTIES\"
1449 CONTENT the content of the drawer.
1450 You can check the export backend through `org-export-current-backend'.
1451 The function should return the text to be inserted into the buffer.
1452 If this is nil, `org-export-format-drawer' is used as a default.")
1454 (defun org-export-remove-or-extract-drawers (all-drawers exp-drawers)
1455 "Remove drawers, or extract and format the content.
1456 ALL-DRAWERS is a list of all drawer names valid in the current buffer.
1457 EXP-DRAWERS can be t to keep all drawer contents, or a list of drawers
1458 whose content to keep. Any drawers that are in ALL-DRAWERS but not in
1459 EXP-DRAWERS will be removed."
1460 (goto-char (point-min))
1461 (let ((re (concat "^[ \t]*:\\("
1462 (mapconcat 'identity all-drawers "\\|")
1463 "\\):[ \t]*$"))
1464 name beg beg-content eol content)
1465 (while (re-search-forward re nil t)
1466 (org-if-unprotected
1467 (setq name (match-string 1))
1468 (setq beg (match-beginning 0)
1469 beg-content (1+ (point-at-eol))
1470 eol (point-at-eol))
1471 (if (not (and (re-search-forward
1472 "^\\([ \t]*:END:[ \t]*\n?\\)\\|^\\*+[ \t]" nil t)
1473 (match-end 1)))
1474 (goto-char eol)
1475 (goto-char (match-beginning 0))
1476 (and (looking-at ".*\n?") (replace-match ""))
1477 (setq content (buffer-substring beg-content (point)))
1478 (delete-region beg (point))
1479 (when (or (eq exp-drawers t)
1480 (member name exp-drawers))
1481 (setq content (funcall (or org-export-format-drawer-function
1482 'org-export-format-drawer)
1483 name content))
1484 (insert content)))))))
1486 (defun org-export-format-drawer (name content)
1487 "Format the content of a drawer as a colon example."
1488 (if (string-match "[ \t]+\\'" content)
1489 (setq content (substring content (match-beginning 0))))
1490 (while (string-match "\\`[ \t]*\n" content)
1491 (setq content (substring content (match-end 0))))
1492 (setq content (org-remove-indentation content))
1493 (setq content (concat ": " (mapconcat 'identity
1494 (org-split-string content "\n")
1495 "\n: ")
1496 "\n"))
1497 (setq content (concat " : " (upcase name) "\n" content))
1498 (org-add-props content nil 'org-protected t))
1500 (defun org-export-handle-export-tags (select-tags exclude-tags)
1501 "Modify the buffer, honoring SELECT-TAGS and EXCLUDE-TAGS.
1502 Both arguments are lists of tags.
1503 If any of SELECT-TAGS is found, all trees not marked by a SELECT-TAG
1504 will be removed.
1505 After that, all subtrees that are marked by EXCLUDE-TAGS will be
1506 removed as well."
1507 (remove-text-properties (point-min) (point-max) '(:org-delete t))
1508 (let* ((re-sel (concat ":\\(" (mapconcat 'regexp-quote
1509 select-tags "\\|")
1510 "\\):"))
1511 (re-excl (concat ":\\(" (mapconcat 'regexp-quote
1512 exclude-tags "\\|")
1513 "\\):"))
1514 beg end cont)
1515 (goto-char (point-min))
1516 (when (and select-tags
1517 (re-search-forward
1518 (concat "^\\*+[ \t].*" re-sel "[^ \t\n]*[ \t]*$") nil t))
1519 ;; At least one tree is marked for export, this means
1520 ;; all the unmarked stuff needs to go.
1521 ;; Dig out the trees that should be exported
1522 (goto-char (point-min))
1523 (outline-next-heading)
1524 (setq beg (point))
1525 (put-text-property beg (point-max) :org-delete t)
1526 (while (re-search-forward re-sel nil t)
1527 (when (org-on-heading-p)
1528 (org-back-to-heading)
1529 (remove-text-properties
1530 (max (1- (point)) (point-min))
1531 (setq cont (save-excursion (org-end-of-subtree t t)))
1532 '(:org-delete t))
1533 (while (and (org-up-heading-safe)
1534 (get-text-property (point) :org-delete))
1535 (remove-text-properties (max (1- (point)) (point-min))
1536 (point-at-eol) '(:org-delete t)))
1537 (goto-char cont))))
1538 ;; Remove the trees explicitly marked for noexport
1539 (when exclude-tags
1540 (goto-char (point-min))
1541 (while (re-search-forward re-excl nil t)
1542 (when (org-at-heading-p)
1543 (org-back-to-heading t)
1544 (setq beg (point))
1545 (org-end-of-subtree t t)
1546 (delete-region beg (point))
1547 (when (featurep 'org-inlinetask)
1548 (org-inlinetask-remove-END-maybe)))))
1549 ;; Remove everything that is now still marked for deletion
1550 (goto-char (point-min))
1551 (while (setq beg (text-property-any (point-min) (point-max) :org-delete t))
1552 (setq end (or (next-single-property-change beg :org-delete)
1553 (point-max)))
1554 (delete-region beg end))))
1556 (defun org-export-remove-tasks (keep)
1557 "Remove tasks depending on configuration.
1558 When KEEP is nil, remove all tasks.
1559 When KEEP is `todo', remove the tasks that are DONE.
1560 When KEEP is `done', remove the tasks that are not yet done.
1561 When it is a list of strings, keep only tasks with these TODO keywords."
1562 (when (or (listp keep) (memq keep '(todo done nil)))
1563 (let ((re (concat "^\\*+[ \t]+\\("
1564 (mapconcat
1565 'regexp-quote
1566 (cond ((not keep) org-todo-keywords-1)
1567 ((eq keep 'todo) org-done-keywords)
1568 ((eq keep 'done) org-not-done-keywords)
1569 ((listp keep)
1570 (org-delete-all keep (copy-sequence
1571 org-todo-keywords-1))))
1572 "\\|")
1573 "\\)\\($\\|[ \t]\\)"))
1574 (case-fold-search nil)
1575 beg)
1576 (goto-char (point-min))
1577 (while (re-search-forward re nil t)
1578 (org-if-unprotected
1579 (setq beg (match-beginning 0))
1580 (org-end-of-subtree t t)
1581 (if (looking-at "^\\*+[ \t]+END[ \t]*$")
1582 ;; Kill the END line of the inline task
1583 (goto-char (min (point-max) (1+ (match-end 0)))))
1584 (delete-region beg (point)))))))
1586 (defun org-export-remove-archived-trees (export-archived-trees)
1587 "Remove archived trees.
1588 When EXPORT-ARCHIVED-TREES is `headline;, only the headline will be exported.
1589 When it is t, the entire archived tree will be exported.
1590 When it is nil the entire tree including the headline will be removed
1591 from the buffer."
1592 (let ((re-archive (concat ":" org-archive-tag ":"))
1593 a b)
1594 (when (not (eq export-archived-trees t))
1595 (goto-char (point-min))
1596 (while (re-search-forward re-archive nil t)
1597 (if (not (org-on-heading-p t))
1598 (goto-char (point-at-eol))
1599 (beginning-of-line 1)
1600 (setq a (if export-archived-trees
1601 (1+ (point-at-eol)) (point))
1602 b (org-end-of-subtree t))
1603 (if (> b a) (delete-region a b)))))))
1605 (defun org-export-remove-headline-metadata (opts)
1606 "Remove meta data from the headline, according to user options."
1607 (let ((re org-complex-heading-regexp)
1608 (todo (plist-get opts :todo-keywords))
1609 (tags (plist-get opts :tags))
1610 (pri (plist-get opts :priority))
1611 (elts '(1 2 3 4 5))
1612 (case-fold-search nil)
1613 rpl)
1614 (setq elts (delq nil (list 1 (if todo 2) (if pri 3) 4 (if tags 5))))
1615 (when (or (not todo) (not tags) (not pri))
1616 (goto-char (point-min))
1617 (while (re-search-forward re nil t)
1618 (org-if-unprotected
1619 (setq rpl (mapconcat (lambda (i) (if (match-end i) (match-string i) ""))
1620 elts " "))
1621 (replace-match rpl t t))))))
1623 (defun org-export-remove-timestamps ()
1624 "Remove timestamps and keywords for export."
1625 (goto-char (point-min))
1626 (while (re-search-forward org-maybe-keyword-time-regexp nil t)
1627 (backward-char 1)
1628 (org-if-unprotected
1629 (unless (save-match-data (org-at-table-p))
1630 (replace-match "")
1631 (beginning-of-line 1)
1632 (if (looking-at "[- \t]*\\(=>[- \t0-9:]*\\)?[ \t]*\n")
1633 (replace-match ""))))))
1635 (defun org-export-remove-clock-lines ()
1636 "Remove clock lines for export."
1637 (goto-char (point-min))
1638 (let ((re (concat "^[ \t]*" org-clock-string ".*\n?")))
1639 (while (re-search-forward re nil t)
1640 (org-if-unprotected
1641 (replace-match "")))))
1643 (defvar org-heading-keyword-regexp-format) ; defined in org.el
1644 (defun org-export-protect-quoted-subtrees ()
1645 "Mark quoted subtrees with the protection property."
1646 (let ((org-re-quote (format org-heading-keyword-regexp-format
1647 org-quote-string)))
1648 (goto-char (point-min))
1649 (while (re-search-forward org-re-quote nil t)
1650 (goto-char (match-beginning 0))
1651 (end-of-line 1)
1652 (add-text-properties (point) (org-end-of-subtree t)
1653 '(org-protected t)))))
1655 (defun org-export-convert-protected-spaces ()
1656 "Convert strings like \\____ to protected spaces in all backends."
1657 (goto-char (point-min))
1658 (while (re-search-forward "\\\\__+" nil t)
1659 (org-if-unprotected-1
1660 (replace-match
1661 (org-add-props
1662 (cond
1663 ((eq org-export-current-backend 'latex)
1664 (format "\\hspace{%dex}" (- (match-end 0) (match-beginning 0))))
1665 ((eq org-export-current-backend 'html)
1666 (org-add-props (match-string 0) nil
1667 'org-whitespace (- (match-end 0) (match-beginning 0))))
1668 ;; ((eq org-export-current-backend 'docbook))
1669 ((eq org-export-current-backend 'ascii)
1670 (org-add-props (match-string 0) '(org-whitespace t)))
1671 (t (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
1672 '(org-protected t))
1673 t t))))
1675 (defun org-export-protect-verbatim ()
1676 "Mark verbatim snippets with the protection property."
1677 (goto-char (point-min))
1678 (while (re-search-forward org-verbatim-re nil t)
1679 (org-if-unprotected
1680 (add-text-properties (match-beginning 4) (match-end 4)
1681 '(org-protected t org-verbatim-emph t))
1682 (goto-char (1+ (match-end 4))))))
1684 (defun org-export-protect-colon-examples ()
1685 "Protect lines starting with a colon."
1686 (goto-char (point-min))
1687 (let ((re "^[ \t]*:\\([ \t]\\|$\\)") beg)
1688 (while (re-search-forward re nil t)
1689 (beginning-of-line 1)
1690 (setq beg (point))
1691 (while (looking-at re)
1692 (end-of-line 1)
1693 (or (eobp) (forward-char 1)))
1694 (add-text-properties beg (if (bolp) (1- (point)) (point))
1695 '(org-protected t)))))
1697 (defvar org-export-backends
1698 '(docbook html beamer ascii latex)
1699 "List of Org supported export backends.")
1701 (defun org-export-select-backend-specific-text ()
1702 (let ((formatters org-export-backends)
1703 (case-fold-search t)
1704 backend backend-name beg beg-content end end-content ind)
1706 (while formatters
1707 (setq backend (pop formatters)
1708 backend-name (symbol-name backend))
1710 ;; Handle #+BACKEND: stuff
1711 (goto-char (point-min))
1712 (while (re-search-forward (concat "^\\([ \t]*\\)#\\+" backend-name
1713 ":[ \t]*\\(.*\\)") nil t)
1714 (if (not (eq backend org-export-current-backend))
1715 (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
1716 (replace-match "\\1\\2" t)
1717 (add-text-properties
1718 (point-at-bol) (min (1+ (point-at-eol)) (point-max))
1719 `(org-protected t original-indentation ,ind org-native-text t))))
1720 ;; Delete #+ATTR_BACKEND: stuff of another backend. Those
1721 ;; matching the current backend will be taken care of by
1722 ;; `org-export-attach-captions-and-attributes'
1723 (goto-char (point-min))
1724 (while (re-search-forward (concat "^\\([ \t]*\\)#\\+ATTR_" backend-name
1725 ":[ \t]*\\(.*\\)") nil t)
1726 (setq ind (org-get-indentation))
1727 (when (not (eq backend org-export-current-backend))
1728 (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))))
1729 ;; Handle #+BEGIN_BACKEND and #+END_BACKEND stuff
1730 (goto-char (point-min))
1731 (while (re-search-forward (concat "^[ \t]*#\\+BEGIN_" backend-name "\\>.*\n?")
1732 nil t)
1733 (setq beg (match-beginning 0) beg-content (match-end 0))
1734 (setq ind (save-excursion (goto-char beg) (org-get-indentation)))
1735 (when (re-search-forward (concat "^[ \t]*#\\+END_" backend-name "\\>.*\n?")
1736 nil t)
1737 (setq end (match-end 0) end-content (match-beginning 0))
1738 (if (eq backend org-export-current-backend)
1739 ;; yes, keep this
1740 (progn
1741 (add-text-properties
1742 beg-content end-content
1743 `(org-protected t original-indentation ,ind org-native-text t))
1744 ;; strip protective commas
1745 (save-excursion
1746 (save-match-data
1747 (goto-char beg-content)
1748 (let ((front-line (save-excursion
1749 (re-search-forward
1750 "[^[:space:]]" end-content t)
1751 (goto-char (match-beginning 0))
1752 (current-column))))
1753 (while (re-search-forward "^[ \t]*\\(,\\)" end-content t)
1754 (when (= (current-column) front-line)
1755 (replace-match "" nil nil nil 1))))))
1756 (delete-region (match-beginning 0) (match-end 0))
1757 (save-excursion
1758 (goto-char beg)
1759 (delete-region (point) (1+ (point-at-eol)))))
1760 ;; No, this is for a different backend, kill it
1761 (delete-region beg end)))))))
1763 (defun org-export-mark-blockquote-verse-center ()
1764 "Mark block quote and verse environments with special cookies.
1765 These special cookies will later be interpreted by the backend."
1766 ;; Blockquotes
1767 (let (type t1 ind beg end beg1 end1 content)
1768 (goto-char (point-min))
1769 (while (re-search-forward
1770 "^\\([ \t]*\\)#\\+\\(begin_\\(\\(block\\)?quote\\|verse\\|center\\)\\>.*\\)"
1771 nil t)
1772 (setq ind (length (match-string 1))
1773 type (downcase (match-string 3))
1774 t1 (if (equal type "quote") "blockquote" type))
1775 (setq beg (match-beginning 0)
1776 beg1 (1+ (match-end 0)))
1777 (when (re-search-forward (concat "^[ \t]*#\\+end_" type "\\>.*") nil t)
1778 (setq end1 (1- (match-beginning 0))
1779 end (+ (point-at-eol) (if (looking-at "\n$") 1 0)))
1780 (setq content (org-remove-indentation (buffer-substring beg1 end1)))
1781 (setq content (concat "ORG-" (upcase t1) "-START\n"
1782 content "\n"
1783 "ORG-" (upcase t1) "-END\n"))
1784 (delete-region beg end)
1785 (insert (org-add-props content nil 'original-indentation ind))))))
1787 (defun org-export-mark-list-end ()
1788 "Mark all list endings with a special string."
1789 (unless (eq org-export-current-backend 'ascii)
1790 (mapc
1791 (lambda (e)
1792 ;; For each type allowing list export, find every list, remove
1793 ;; ending regexp if needed, and insert org-list-end.
1794 (goto-char (point-min))
1795 (while (re-search-forward (org-item-beginning-re) nil t)
1796 (when (eq (nth 2 (org-list-context)) e)
1797 (let* ((struct (org-list-struct))
1798 (bottom (org-list-get-bottom-point struct))
1799 (top (point-at-bol))
1800 (top-ind (org-list-get-ind top struct)))
1801 (goto-char bottom)
1802 (when (and (not (eq org-list-ending-method 'indent))
1803 (not (looking-at "[ \t]*$"))
1804 (looking-at org-list-end-re))
1805 (replace-match ""))
1806 (unless (bolp) (insert "\n"))
1807 ;; As org-list-end is inserted at column 0, it would end
1808 ;; by indentation any list. It can be problematic when
1809 ;; there are lists within lists: the inner list end would
1810 ;; also become the outer list end. To avoid this, text
1811 ;; property `original-indentation' is added, as
1812 ;; `org-list-struct' pays attention to it when reading a
1813 ;; list.
1814 (insert (org-add-props
1815 "ORG-LIST-END-MARKER\n"
1816 (list 'original-indentation top-ind)))))))
1817 (cons nil org-list-export-context))))
1819 (defun org-export-mark-list-properties ()
1820 "Mark list with special properties.
1821 These special properties will later be interpreted by the backend."
1822 (let ((mark-list
1823 (function
1824 ;; Mark a list with 3 properties: `list-item' which is
1825 ;; position at beginning of line, `list-struct' which is
1826 ;; list structure, and `list-prevs' which is the alist of
1827 ;; item and its predecessor. Leave point at list ending.
1828 (lambda (ctxt)
1829 (let* ((struct (org-list-struct))
1830 (top (org-list-get-top-point struct))
1831 (bottom (org-list-get-bottom-point struct))
1832 (prevs (org-list-prevs-alist struct))
1833 poi)
1834 ;; Get every item and ending position, without dups and
1835 ;; without bottom point of list.
1836 (mapc (lambda (e)
1837 (let ((pos (car e))
1838 (end (nth 6 e)))
1839 (unless (memq pos poi)
1840 (push pos poi))
1841 (unless (or (= end bottom) (memq end poi))
1842 (push end poi))))
1843 struct)
1844 (setq poi (sort poi '<))
1845 ;; For every point of interest, mark the whole line with
1846 ;; its position in list.
1847 (mapc
1848 (lambda (e)
1849 (goto-char e)
1850 (add-text-properties (point-at-bol) (point-at-eol)
1851 (list 'list-item (point-at-bol)
1852 'list-struct struct
1853 'list-prevs prevs)))
1854 poi)
1855 ;; Take care of bottom point. As babel may have inserted
1856 ;; a new list in buffer, list ending isn't always
1857 ;; marked. Now mark every list ending and add properties
1858 ;; useful to line processing exporters.
1859 (goto-char bottom)
1860 (when (or (looking-at "^ORG-LIST-END-MARKER\n")
1861 (and (not (eq org-list-ending-method 'indent))
1862 (not (looking-at "[ \t]*$"))
1863 (looking-at org-list-end-re)))
1864 (replace-match ""))
1865 (unless (bolp) (insert "\n"))
1866 (insert
1867 (org-add-props "ORG-LIST-END-MARKER\n" (list 'list-item bottom
1868 'list-struct struct
1869 'list-prevs prevs)))
1870 ;; Following property is used by LaTeX exporter.
1871 (add-text-properties top (point) (list 'list-context ctxt)))))))
1872 ;; Mark lists except for backends not interpreting them.
1873 (unless (eq org-export-current-backend 'ascii)
1874 (let ((org-list-end-re "^ORG-LIST-END-MARKER\n"))
1875 (mapc
1876 (lambda (e)
1877 (goto-char (point-min))
1878 (while (re-search-forward (org-item-beginning-re) nil t)
1879 (let ((context (nth 2 (org-list-context))))
1880 (if (eq context e)
1881 (funcall mark-list e)
1882 (put-text-property (point-at-bol) (point-at-eol)
1883 'list-context context)))))
1884 (cons nil org-list-export-context))))))
1886 (defun org-export-attach-captions-and-attributes (target-alist)
1887 "Move #+CAPTION, #+ATTR_BACKEND, and #+LABEL text into text properties.
1888 If the next thing following is a table, add the text properties to the first
1889 table line. If it is a link, add it to the line containing the link."
1890 (goto-char (point-min))
1891 (remove-text-properties (point-min) (point-max)
1892 '(org-caption nil org-attributes nil))
1893 (let ((case-fold-search t)
1894 (re (concat "^[ \t]*#\\+caption:[ \t]+\\(.*\\)"
1895 "\\|"
1896 "^[ \t]*#\\+attr_" (symbol-name org-export-current-backend) ":[ \t]+\\(.*\\)"
1897 "\\|"
1898 "^[ \t]*#\\+label:[ \t]+\\(.*\\)"
1899 "\\|"
1900 "^[ \t]*\\(|[^-]\\)"
1901 "\\|"
1902 "^[ \t]*\\[\\[.*\\]\\][ \t]*$"))
1903 cap shortn attr label end)
1904 (while (re-search-forward re nil t)
1905 (cond
1906 ;; there is a caption
1907 ((match-end 1)
1908 (progn
1909 (setq cap (concat cap (if cap " " "") (org-trim (match-string 1))))
1910 (when (string-match "\\[\\(.*\\)\\]{\\(.*\\)}" cap)
1911 (setq shortn (match-string 1 cap)
1912 cap (match-string 2 cap)))
1913 (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))))
1914 ;; there is an attribute
1915 ((match-end 2)
1916 (progn
1917 (setq attr (concat attr (if attr " " "") (org-trim (match-string 2))))
1918 (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))))
1919 ;; there is a label
1920 ((match-end 3)
1921 (progn
1922 (setq label (org-trim (match-string 3)))
1923 (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))))
1925 (setq end (if (match-end 4)
1926 (let ((ee (org-table-end)))
1927 (prog1 (1- (marker-position ee)) (move-marker ee nil)))
1928 (point-at-eol)))
1929 (add-text-properties (point-at-bol) end
1930 (list 'org-caption cap
1931 'org-caption-shortn shortn
1932 'org-attributes attr
1933 'org-label label))
1934 (if label (push (cons label label) target-alist))
1935 (goto-char end)
1936 (setq cap nil shortn nil attr nil label nil)))))
1937 target-alist)
1939 (defun org-export-remove-comment-blocks-and-subtrees ()
1940 "Remove the comment environment, and also commented subtrees."
1941 (let ((re-commented (format org-heading-keyword-regexp-format
1942 org-comment-string))
1943 case-fold-search)
1944 ;; Remove comment environment
1945 (goto-char (point-min))
1946 (setq case-fold-search t)
1947 (while (re-search-forward
1948 "^#\\+begin_comment[ \t]*\n[^\000]*?\n#\\+end_comment\\>.*" nil t)
1949 (replace-match "" t t))
1950 ;; Remove subtrees that are commented
1951 (goto-char (point-min))
1952 (setq case-fold-search nil)
1953 (while (re-search-forward re-commented nil t)
1954 (goto-char (match-beginning 0))
1955 (delete-region (point) (org-end-of-subtree t)))))
1957 (defun org-export-handle-comments (org-commentsp)
1958 "Remove comments, or convert to backend-specific format.
1959 ORG-COMMENTSP can be a format string for publishing comments.
1960 When it is nil, all comments will be removed."
1961 (let ((re "^\\(#\\|[ \t]*#\\+ \\)\\(.*\n?\\)")
1962 pos)
1963 (goto-char (point-min))
1964 (while (or (looking-at re)
1965 (re-search-forward re nil t))
1966 (setq pos (match-beginning 0))
1967 (if (get-text-property pos 'org-protected)
1968 (goto-char (1+ pos))
1969 (if (and org-commentsp
1970 (not (equal (char-before (match-end 1)) ?+)))
1971 (progn (add-text-properties
1972 (match-beginning 0) (match-end 0) '(org-protected t))
1973 (replace-match (org-add-props
1974 (format org-commentsp (match-string 2))
1975 nil 'org-protected t)
1976 t t))
1977 (goto-char (1+ pos))
1978 (replace-match "")
1979 (goto-char (max (point-min) (1- pos))))))))
1981 (defun org-export-handle-table-metalines ()
1982 "Remove table specific metalines #+TBLNAME: and #+TBLFM:."
1983 (let ((re "^[ \t]*#\\+TBL\\(NAME\\|FM\\):\\(.*\n?\\)")
1984 pos)
1985 (goto-char (point-min))
1986 (while (or (looking-at re)
1987 (re-search-forward re nil t))
1988 (setq pos (match-beginning 0))
1989 (if (get-text-property (match-beginning 1) 'org-protected)
1990 (goto-char (1+ pos))
1991 (goto-char (1+ pos))
1992 (replace-match "")
1993 (goto-char (max (point-min) (1- pos)))))))
1995 (defun org-export-mark-radio-links ()
1996 "Find all matches for radio targets and turn them into internal links."
1997 (let ((re-radio (and org-target-link-regexp
1998 (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)"))))
1999 (goto-char (point-min))
2000 (when re-radio
2001 (while (re-search-forward re-radio nil t)
2002 (unless
2003 (save-match-data
2004 (or (org-in-regexp org-bracket-link-regexp)
2005 (org-in-regexp org-plain-link-re)
2006 (org-in-regexp "<<[^<>]+>>")))
2007 (org-if-unprotected
2008 (replace-match "\\1[[\\2]]")))))))
2010 (defun org-store-forced-table-alignment ()
2011 "Find table lines which force alignment, store the results in properties."
2012 (let (line cnt cookies)
2013 (goto-char (point-min))
2014 (while (re-search-forward "|[ \t]*<\\([lrc]?[0-9]+\\|[lrc]\\)>[ \t]*|"
2015 nil t)
2016 ;; OK, this looks like a table line with an alignment cookie
2017 (org-if-unprotected
2018 (setq line (buffer-substring (point-at-bol) (point-at-eol)))
2019 (when (and (org-at-table-p)
2020 (org-table-cookie-line-p line))
2021 (setq cnt 0 cookies nil)
2022 (mapc
2023 (lambda (x)
2024 (setq cnt (1+ cnt))
2025 (when (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'" x)
2026 (let ((align (and (match-end 1)
2027 (downcase (match-string 1 x))))
2028 (width (and (match-end 2)
2029 (string-to-number (match-string 2 x)))))
2030 (push (cons cnt (list align width)) cookies))))
2031 (org-split-string line "[ \t]*|[ \t]*"))
2032 (add-text-properties (org-table-begin) (org-table-end)
2033 (list 'org-col-cookies cookies))))
2034 (goto-char (point-at-eol)))))
2036 (defun org-export-remove-special-table-lines ()
2037 "Remove tables lines that are used for internal purposes.
2038 Also, store forced alignment information found in such lines."
2039 (goto-char (point-min))
2040 (while (re-search-forward "^[ \t]*|" nil t)
2041 (org-if-unprotected-at (1- (point))
2042 (beginning-of-line 1)
2043 (if (or (looking-at "[ \t]*| *[!_^] *|")
2044 (not
2045 (memq
2047 (mapcar
2048 (lambda (f)
2049 (or (and org-export-table-remove-empty-lines (= (length f) 0))
2050 (string-match
2051 "\\`<\\([0-9]\\|[lrc]\\|[lrc][0-9]+\\)>\\'" f)))
2052 (org-split-string ;; FIXME, can't we do without splitting???
2053 (buffer-substring (point-at-bol) (point-at-eol))
2054 "[ \t]*|[ \t]*")))))
2055 (delete-region (max (point-min) (1- (point-at-bol)))
2056 (point-at-eol))
2057 (end-of-line 1)))))
2059 (defun org-export-protect-sub-super (s)
2060 (save-match-data
2061 (while (string-match "\\([^\\\\]\\)\\([_^]\\)" s)
2062 (setq s (replace-match "\\1\\\\\\2" nil nil s)))
2065 (defun org-export-normalize-links ()
2066 "Convert all links to bracket links, and expand link abbreviations."
2067 (let ((re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
2068 (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
2069 nodesc)
2070 (goto-char (point-min))
2071 (while (re-search-forward re-plain-link nil t)
2072 (unless (org-string-match-p
2073 "\\[\\[\\S-+:\\S-*?\\<"
2074 (buffer-substring (point-at-bol) (match-beginning 0)))
2075 (goto-char (1- (match-end 0)))
2076 (org-if-unprotected-at (1+ (match-beginning 0))
2077 (let* ((s (concat (match-string 1)
2078 "[[" (match-string 2) ":" (match-string 3)
2079 "][" (match-string 2) ":" (org-export-protect-sub-super
2080 (match-string 3))
2081 "]]")))
2082 ;; added 'org-link face to links
2083 (put-text-property 0 (length s) 'face 'org-link s)
2084 (replace-match s t t)))))
2085 (goto-char (point-min))
2086 (while (re-search-forward re-angle-link nil t)
2087 (goto-char (1- (match-end 0)))
2088 (org-if-unprotected
2089 (let* ((s (concat (match-string 1)
2090 "[[" (match-string 2) ":" (match-string 3)
2091 "][" (match-string 2) ":" (org-export-protect-sub-super
2092 (match-string 3))
2093 "]]")))
2094 (put-text-property 0 (length s) 'face 'org-link s)
2095 (replace-match s t t))))
2096 (goto-char (point-min))
2097 (while (re-search-forward org-bracket-link-regexp nil t)
2098 (goto-char (1- (match-end 0)))
2099 (setq nodesc (not (match-end 3)))
2100 (org-if-unprotected
2101 (let* ((xx (save-match-data
2102 (org-translate-link
2103 (org-link-expand-abbrev (match-string 1)))))
2104 (s (concat
2105 "[[" (org-add-props (copy-sequence xx)
2106 nil 'org-protected t 'org-no-description nodesc)
2108 (if (match-end 3)
2109 (match-string 2)
2110 (concat "[" (copy-sequence xx)
2111 "]"))
2112 "]")))
2113 (put-text-property 0 (length s) 'face 'org-link s)
2114 (replace-match s t t))))))
2116 (defun org-export-concatenate-multiline-links ()
2117 "Find multi-line links and put it all into a single line.
2118 This is to make sure that the line-processing export backends
2119 can work correctly."
2120 (goto-char (point-min))
2121 (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
2122 (org-if-unprotected-at (match-beginning 1)
2123 (replace-match "\\1 \\3")
2124 (goto-char (match-beginning 0)))))
2126 (defun org-export-concatenate-multiline-emphasis ()
2127 "Find multi-line emphasis and put it all into a single line.
2128 This is to make sure that the line-processing export backends
2129 can work correctly."
2130 (goto-char (point-min))
2131 (while (re-search-forward org-emph-re nil t)
2132 (if (and (not (= (char-after (match-beginning 3))
2133 (char-after (match-beginning 4))))
2134 (save-excursion (goto-char (match-beginning 0))
2135 (save-match-data
2136 (and (not (org-at-table-p))
2137 (not (org-at-heading-p))))))
2138 (org-if-unprotected
2139 (subst-char-in-region (match-beginning 0) (match-end 0)
2140 ?\n ?\ t)
2141 (goto-char (1- (match-end 0))))
2142 (goto-char (1+ (match-beginning 0))))))
2144 (defun org-export-grab-title-from-buffer ()
2145 "Get a title for the current document, from looking at the buffer."
2146 (let ((inhibit-read-only t))
2147 (save-excursion
2148 (goto-char (point-min))
2149 (let ((end (if (looking-at org-outline-regexp)
2150 (point)
2151 (save-excursion (outline-next-heading) (point)))))
2152 (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
2153 ;; Mark the line so that it will not be exported as normal text.
2154 (unless (org-in-block-p org-list-forbidden-blocks)
2155 (org-unmodified
2156 (add-text-properties (match-beginning 0) (match-end 0)
2157 (list :org-license-to-kill t))))
2158 ;; Return the title string
2159 (org-trim (match-string 0)))))))
2161 (defun org-export-get-title-from-subtree ()
2162 "Return subtree title and exclude it from export."
2163 (let ((rbeg (region-beginning)) (rend (region-end))
2164 (inhibit-read-only t)
2165 (tags (plist-get (org-infile-export-plist) :tags))
2166 title)
2167 (save-excursion
2168 (goto-char rbeg)
2169 (when (and (org-at-heading-p)
2170 (>= (org-end-of-subtree t t) rend))
2171 (when (plist-member org-export-opt-plist :tags)
2172 (setq tags (or (plist-get org-export-opt-plist :tags) tags)))
2173 ;; This is a subtree, we take the title from the first heading
2174 (goto-char rbeg)
2175 (looking-at org-todo-line-tags-regexp)
2176 (setq title (if (eq tags t)
2177 (format "%s\t%s" (match-string 3) (match-string 4))
2178 (match-string 3)))
2179 (org-unmodified
2180 (add-text-properties (point) (1+ (point-at-eol))
2181 (list :org-license-to-kill t)))
2182 (setq title (or (org-entry-get nil "EXPORT_TITLE") title))))
2183 title))
2185 (defun org-solidify-link-text (s &optional alist)
2186 "Take link text and make a safe target out of it."
2187 (save-match-data
2188 (let* ((rtn
2189 (mapconcat
2190 'identity
2191 (org-split-string s "[^a-zA-Z0-9_\\.-]+") "-"))
2192 (a (assoc rtn alist)))
2193 (or (cdr a) rtn))))
2195 (defun org-get-min-level (lines &optional offset)
2196 "Get the minimum level in LINES."
2197 (let ((re "^\\(\\*+\\) ") l)
2198 (catch 'exit
2199 (while (setq l (pop lines))
2200 (if (string-match re l)
2201 (throw 'exit (org-tr-level (- (length (match-string 1 l))
2202 (or offset 0))))))
2203 1)))
2205 ;; Variable holding the vector with section numbers
2206 (defvar org-section-numbers (make-vector org-level-max 0))
2208 (defun org-init-section-numbers ()
2209 "Initialize the vector for the section numbers."
2210 (let* ((level -1)
2211 (numbers (nreverse (org-split-string "" "\\.")))
2212 (depth (1- (length org-section-numbers)))
2213 (i depth) number-string)
2214 (while (>= i 0)
2215 (if (> i level)
2216 (aset org-section-numbers i 0)
2217 (setq number-string (or (car numbers) "0"))
2218 (if (string-match "\\`[A-Z]\\'" number-string)
2219 (aset org-section-numbers i
2220 (- (string-to-char number-string) ?A -1))
2221 (aset org-section-numbers i (string-to-number number-string)))
2222 (pop numbers))
2223 (setq i (1- i)))))
2225 (defun org-section-number (&optional level)
2226 "Return a string with the current section number.
2227 When LEVEL is non-nil, increase section numbers on that level."
2228 (let* ((depth (1- (length org-section-numbers)))
2229 (string "")
2230 (fmts (car org-export-section-number-format))
2231 (term (cdr org-export-section-number-format))
2232 (sep "")
2233 ctype fmt idx n)
2234 (when level
2235 (when (> level -1)
2236 (aset org-section-numbers
2237 level (1+ (aref org-section-numbers level))))
2238 (setq idx (1+ level))
2239 (while (<= idx depth)
2240 (if (not (= idx 1))
2241 (aset org-section-numbers idx 0))
2242 (setq idx (1+ idx))))
2243 (setq idx 0)
2244 (while (<= idx depth)
2245 (when (> (aref org-section-numbers idx) 0)
2246 (setq fmt (or (pop fmts) fmt)
2247 ctype (car fmt)
2248 n (aref org-section-numbers idx)
2249 string (if (> n 0)
2250 (concat string sep (org-number-to-counter n ctype))
2251 (concat string ".0"))
2252 sep (nth 1 fmt)))
2253 (setq idx (1+ idx)))
2254 (save-match-data
2255 (if (string-match "\\`\\([@0]\\.\\)+" string)
2256 (setq string (replace-match "" t nil string)))
2257 (if (string-match "\\(\\.0\\)+\\'" string)
2258 (setq string (replace-match "" t nil string))))
2259 (concat string term)))
2261 (defun org-number-to-counter (n type)
2262 "Concert number N to a string counter, according to TYPE.
2263 TYPE must be a string, any of:
2264 1 number
2265 A A,B,....
2266 a a,b,....
2267 I upper case roman numeral
2268 i lower case roman numeral"
2269 (cond
2270 ((equal type "1") (number-to-string n))
2271 ((equal type "A") (char-to-string (+ ?A n -1)))
2272 ((equal type "a") (char-to-string (+ ?a n -1)))
2273 ((equal type "I") (org-number-to-roman n))
2274 ((equal type "i") (downcase (org-number-to-roman n)))
2275 (t (error "Invalid counter type `%s'" type))))
2277 (defun org-number-to-roman (n)
2278 "Convert integer N into a roman numeral."
2279 (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
2280 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
2281 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
2282 ( 1 . "I")))
2283 (res ""))
2284 (if (<= n 0)
2285 (number-to-string n)
2286 (while roman
2287 (if (>= n (caar roman))
2288 (setq n (- n (caar roman))
2289 res (concat res (cdar roman)))
2290 (pop roman)))
2291 res)))
2293 ;;; Macros
2295 (defun org-export-preprocess-apply-macros ()
2296 "Replace macro references."
2297 (goto-char (point-min))
2298 (let (sy val key args args2 ind-str s n)
2299 (while (re-search-forward
2300 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
2301 nil t)
2302 (unless (save-match-data (save-excursion
2303 (goto-char (point-at-bol))
2304 (looking-at "[ \t]*#\\+macro")))
2305 ;; Get macro name (KEY), arguments (ARGS), and indentation of
2306 ;; current line (IND-STR) as strings.
2307 (setq key (downcase (match-string 1))
2308 args (match-string 3)
2309 ind-str (save-match-data (save-excursion
2310 (beginning-of-line)
2311 (looking-at "^\\([ \t]*\\).*")
2312 (match-string 1))))
2313 ;; When macro is defined, retrieve replacement text in VAL,
2314 ;; and proceed with expansion.
2315 (when (setq val (or (plist-get org-export-opt-plist
2316 (intern (concat ":macro-" key)))
2317 (plist-get org-export-opt-plist
2318 (intern (concat ":" key)))))
2319 (save-match-data
2320 ;; If arguments are provided, first retreive them properly
2321 ;; (in ARGS, as a list), then replace them in VAL.
2322 (when args
2323 (setq args (org-split-string args ",") args2 nil)
2324 (while args
2325 (while (string-match "\\\\\\'" (car args))
2326 ;; Repair bad splits.
2327 (setcar (cdr args) (concat (substring (car args) 0 -1)
2328 "," (nth 1 args)))
2329 (pop args))
2330 (push (pop args) args2))
2331 (setq args (mapcar 'org-trim (nreverse args2)))
2332 (setq s 0)
2333 (while (string-match "\\$\\([0-9]+\\)" val s)
2334 (setq s (1+ (match-beginning 0))
2335 n (string-to-number (match-string 1 val)))
2336 (and (>= (length args) n)
2337 (setq val (replace-match (nth (1- n) args) t t val)))))
2338 ;; VAL starts with "(eval": it is a sexp, `eval' it.
2339 (when (string-match "\\`(eval\\>" val)
2340 (setq val (eval (read val))))
2341 ;; Ensure VAL is a string (or nil) and that each new line
2342 ;; is indented as the first one.
2343 (setq val (and val
2344 (mapconcat 'identity
2345 (org-split-string
2346 (if (stringp val) val (format "%s" val))
2347 "\n")
2348 (concat "\n" ind-str)))))
2349 ;; Eventually do the replacement, if VAL isn't nil. Move
2350 ;; point at beginning of macro for recursive expansions.
2351 (when val
2352 (replace-match val t t)
2353 (goto-char (match-beginning 0))))))))
2355 (defun org-export-apply-macros-in-string (s)
2356 "Apply the macros in string S."
2357 (when s
2358 (with-temp-buffer
2359 (insert s)
2360 (org-export-preprocess-apply-macros)
2361 (buffer-string))))
2363 ;;; Include files
2365 (defun org-export-handle-include-files ()
2366 "Include the contents of include files, with proper formatting."
2367 (let ((case-fold-search t)
2368 params file markup lang start end prefix prefix1 switches all minlevel lines)
2369 (goto-char (point-min))
2370 (while (re-search-forward "^#\\+INCLUDE:?[ \t]+\\(.*\\)" nil t)
2371 (setq params (read (concat "(" (match-string 1) ")"))
2372 prefix (org-get-and-remove-property 'params :prefix)
2373 prefix1 (org-get-and-remove-property 'params :prefix1)
2374 minlevel (org-get-and-remove-property 'params :minlevel)
2375 lines (org-get-and-remove-property 'params :lines)
2376 file (org-symname-or-string (pop params))
2377 markup (org-symname-or-string (pop params))
2378 lang (and (member markup '("src" "SRC"))
2379 (org-symname-or-string (pop params)))
2380 switches (mapconcat #'(lambda (x) (format "%s" x)) params " ")
2381 start nil end nil)
2382 (delete-region (match-beginning 0) (match-end 0))
2383 (if (or (not file)
2384 (not (file-exists-p file))
2385 (not (file-readable-p file)))
2386 (insert (format "CANNOT INCLUDE FILE %s" file))
2387 (setq all (cons file all))
2388 (when markup
2389 (if (equal (downcase markup) "src")
2390 (setq start (format "#+begin_src %s %s\n"
2391 (or lang "fundamental")
2392 (or switches ""))
2393 end "#+end_src")
2394 (setq start (format "#+begin_%s %s\n" markup switches)
2395 end (format "#+end_%s" markup))))
2396 (insert (or start ""))
2397 (insert (org-get-file-contents (expand-file-name file)
2398 prefix prefix1 markup minlevel lines))
2399 (or (bolp) (newline))
2400 (insert (or end ""))))
2401 all))
2403 (defun org-export-handle-include-files-recurse ()
2404 "Recursively include files aborting on circular inclusion."
2405 (let ((now (list org-current-export-file)) all)
2406 (while now
2407 (setq all (append now all))
2408 (setq now (org-export-handle-include-files))
2409 (let ((intersection
2410 (delq nil
2411 (mapcar (lambda (el) (when (member el all) el)) now))))
2412 (when intersection
2413 (error "Recursive #+INCLUDE: %S" intersection))))))
2415 (defun org-get-file-contents (file &optional prefix prefix1 markup minlevel lines)
2416 "Get the contents of FILE and return them as a string.
2417 If PREFIX is a string, prepend it to each line. If PREFIX1
2418 is a string, prepend it to the first line instead of PREFIX.
2419 If MARKUP, don't protect org-like lines, the exporter will
2420 take care of the block they are in. If LINES is a string
2421 specifying a range of lines, include only those lines ."
2422 (if (stringp markup) (setq markup (downcase markup)))
2423 (with-temp-buffer
2424 (insert-file-contents file)
2425 (when lines
2426 (let* ((lines (split-string lines "-"))
2427 (lbeg (string-to-number (car lines)))
2428 (lend (string-to-number (cadr lines)))
2429 (beg (if (zerop lbeg) (point-min)
2430 (goto-char (point-min))
2431 (forward-line (1- lbeg))
2432 (point)))
2433 (end (if (zerop lend) (point-max)
2434 (goto-char (point-min))
2435 (forward-line (1- lend))
2436 (point))))
2437 (narrow-to-region beg end)))
2438 (when (or prefix prefix1)
2439 (goto-char (point-min))
2440 (while (not (eobp))
2441 (insert (or prefix1 prefix))
2442 (setq prefix1 "")
2443 (beginning-of-line 2)))
2444 (buffer-string)
2445 (when (member markup '("src" "example"))
2446 (goto-char (point-min))
2447 (while (re-search-forward "^\\([*#]\\|[ \t]*#\\+\\)" nil t)
2448 (goto-char (match-beginning 0))
2449 (insert ",")
2450 (end-of-line 1)))
2451 (when minlevel
2452 (dotimes (lvl minlevel)
2453 (org-map-region 'org-demote (point-min) (point-max))))
2454 (buffer-string)))
2456 (defun org-get-and-remove-property (listvar prop)
2457 "Check if the value of LISTVAR contains PROP as a property.
2458 If yes, return the value of that property (i.e. the element following
2459 in the list) and remove property and value from the list in LISTVAR."
2460 (let ((list (symbol-value listvar)) m v)
2461 (when (setq m (member prop list))
2462 (setq v (nth 1 m))
2463 (if (equal (car list) prop)
2464 (set listvar (cddr list))
2465 (setcdr (nthcdr (- (length list) (length m) 1) list)
2466 (cddr m))
2467 (set listvar list)))
2470 (defun org-symname-or-string (s)
2471 (if (symbolp s)
2472 (if s (symbol-name s) s)
2475 ;;; Fontification and line numbers for code examples
2477 (defvar org-export-last-code-line-counter-value 0)
2479 (defun org-export-replace-src-segments-and-examples ()
2480 "Replace source code segments with special code for export."
2481 (setq org-export-last-code-line-counter-value 0)
2482 (let ((case-fold-search t)
2483 lang code trans opts indent caption)
2484 (goto-char (point-min))
2485 (while (re-search-forward
2486 "\\(^\\([ \t]*\\)#\\+BEGIN_SRC:?\\([ \t]+\\([^ \t\n]+\\)\\)?\\(.*\\)\n\\([^\000]+?\n\\)[ \t]*#\\+END_SRC.*\n?\\)\\|\\(^\\([ \t]*\\)#\\+BEGIN_EXAMPLE:?\\(?:[ \t]+\\(.*\\)\\)?\n\\([^\000]+?\n\\)[ \t]*#\\+END_EXAMPLE.*\n?\\)"
2487 nil t)
2488 (if (match-end 1)
2489 (if (not (match-string 4))
2490 (error "Source block missing language specification: %s"
2491 (let* ((body (match-string 6))
2492 (nothing (message "body:%s" body))
2493 (preview (or (and (string-match
2494 "^[ \t]*\\([^\n\r]*\\)" body)
2495 (match-string 1 body)) body)))
2496 (if (> (length preview) 35)
2497 (concat (substring preview 0 32) "...")
2498 preview)))
2499 ;; src segments
2500 (setq lang (match-string 4)
2501 opts (match-string 5)
2502 code (match-string 6)
2503 indent (length (match-string 2))
2504 caption (get-text-property 0 'org-caption (match-string 0))))
2505 (setq lang nil
2506 opts (match-string 9)
2507 code (match-string 10)
2508 indent (length (match-string 8))
2509 caption (get-text-property 0 'org-caption (match-string 0))))
2511 (setq trans (org-export-format-source-code-or-example
2512 lang code opts indent caption))
2513 (replace-match trans t t))))
2515 (defvar org-export-latex-verbatim-wrap) ;; defined in org-latex.el
2516 (defvar org-export-latex-listings) ;; defined in org-latex.el
2517 (defvar org-export-latex-listings-langs) ;; defined in org-latex.el
2518 (defvar org-export-latex-listings-w-names) ;; defined in org-latex.el
2519 (defvar org-export-latex-minted-langs) ;; defined in org-latex.el
2520 (defvar org-export-latex-custom-lang-environments) ;; defined in org-latex.el
2521 (defvar org-export-latex-listings-options) ;; defined in org-latex.el
2522 (defvar org-export-latex-minted-options) ;; defined in org-latex.el
2524 (defun org-remove-formatting-on-newlines-in-region (beg end)
2525 "Remove formatting on newline characters"
2526 (interactive "r")
2527 (save-excursion
2528 (goto-char beg)
2529 (while (progn (end-of-line) (< (point) end))
2530 (put-text-property (point) (1+ (point)) 'face nil)
2531 (forward-char 1))))
2533 (defun org-export-format-source-code-or-example
2534 (lang code &optional opts indent caption)
2535 "Format CODE from language LANG and return it formatted for export.
2536 The CODE is marked up in `org-export-current-backend' format.
2538 Check if a function by name
2539 \"org-<backend>-format-source-code-or-example\" is bound. If yes,
2540 use it as the custom formatter. Otherwise, use the default
2541 formatter. Default formatters are provided for docbook, html,
2542 latex and ascii backends. For example, use
2543 `org-html-format-source-code-or-example' to provide a custom
2544 formatter for export to \"html\".
2546 If LANG is nil, do not add any fontification.
2547 OPTS contains formatting options, like `-n' for triggering numbering lines,
2548 and `+n' for continuing previous numbering.
2549 Code formatting according to language currently only works for HTML.
2550 Numbering lines works for all three major backends (html, latex, and ascii).
2551 INDENT was the original indentation of the block."
2552 (save-match-data
2553 (let* ((backend-name (symbol-name org-export-current-backend))
2554 (backend-formatter
2555 (intern (format "org-%s-format-source-code-or-example"
2556 backend-name)))
2557 (backend-feature (intern (concat "org-" backend-name)))
2558 (backend-formatter
2559 (and (require (intern (concat "org-" backend-name)) nil)
2560 (fboundp backend-formatter) backend-formatter))
2561 num cont rtn rpllbl keepp textareap preserve-indentp cols rows fmt)
2562 (setq opts (or opts "")
2563 num (string-match "[-+]n\\>" opts)
2564 cont (string-match "\\+n\\>" opts)
2565 rpllbl (string-match "-r\\>" opts)
2566 keepp (string-match "-k\\>" opts)
2567 textareap (string-match "-t\\>" opts)
2568 preserve-indentp (or org-src-preserve-indentation
2569 (string-match "-i\\>" opts))
2570 cols (if (string-match "-w[ \t]+\\([0-9]+\\)" opts)
2571 (string-to-number (match-string 1 opts))
2573 rows (if (string-match "-h[ \t]+\\([0-9]+\\)" opts)
2574 (string-to-number (match-string 1 opts))
2575 (org-count-lines code))
2576 fmt (if (string-match "-l[ \t]+\"\\([^\"\n]+\\)\"" opts)
2577 (match-string 1 opts)))
2578 (when (and textareap (eq org-export-current-backend 'html))
2579 ;; we cannot use numbering or highlighting.
2580 (setq num nil cont nil lang nil))
2581 (if keepp (setq rpllbl 'keep))
2582 (setq rtn (if preserve-indentp code (org-remove-indentation code)))
2583 (when (string-match "^," rtn)
2584 (setq rtn (with-temp-buffer
2585 (insert rtn)
2586 ;; Free up the protected lines
2587 (goto-char (point-min))
2588 (while (re-search-forward "^," nil t)
2589 (if (or (equal lang "org")
2590 (save-match-data
2591 (looking-at "\\([*#]\\|[ \t]*#\\+\\)")))
2592 (replace-match ""))
2593 (end-of-line 1))
2594 (buffer-string))))
2595 ;; Now backend-specific coding
2596 (setq rtn
2597 (cond
2598 (backend-formatter
2599 (funcall backend-formatter rtn lang caption textareap cols rows num
2600 cont rpllbl fmt))
2601 ((eq org-export-current-backend 'docbook)
2602 (setq rtn (org-export-number-lines rtn 0 0 num cont rpllbl fmt))
2603 (concat "<programlisting><![CDATA["
2605 "]]></programlisting>\n"))
2606 ((eq org-export-current-backend 'html)
2607 ;; We are exporting to HTML
2608 (when lang
2609 (if (featurep 'xemacs)
2610 (require 'htmlize)
2611 (require 'htmlize nil t))
2612 (when (not (fboundp 'htmlize-region-for-paste))
2613 ;; we do not have htmlize.el, or an old version of it
2614 (setq lang nil)
2615 (message
2616 "htmlize.el 1.34 or later is needed for source code formatting")))
2618 (if lang
2619 (let* ((lang-m (when lang
2620 (or (cdr (assoc lang org-src-lang-modes))
2621 lang)))
2622 (mode (and lang-m (intern
2623 (concat
2624 (if (symbolp lang-m)
2625 (symbol-name lang-m)
2626 lang-m)
2627 "-mode"))))
2628 (org-inhibit-startup t)
2629 (org-startup-folded nil))
2630 (setq rtn
2631 (with-temp-buffer
2632 (insert rtn)
2633 (if (functionp mode)
2634 (funcall mode)
2635 (fundamental-mode))
2636 (font-lock-fontify-buffer)
2637 ;; markup each line separately
2638 (org-remove-formatting-on-newlines-in-region (point-min) (point-max))
2639 (org-src-mode)
2640 (set-buffer-modified-p nil)
2641 (org-export-htmlize-region-for-paste
2642 (point-min) (point-max))))
2643 (if (string-match "<pre\\([^>]*\\)>\n*" rtn)
2644 (setq rtn
2645 (concat
2646 (if caption
2647 (concat
2648 "<div class=\"org-src-container\">"
2649 (format
2650 "<label class=\"org-src-name\">%s</label>"
2651 caption))
2653 (replace-match
2654 (format "<pre class=\"src src-%s\">\n" lang)
2655 t t rtn)
2656 (if caption "</div>" "")))))
2657 (if textareap
2658 (setq rtn (concat
2659 (format "<p>\n<textarea cols=\"%d\" rows=\"%d\">"
2660 cols rows)
2661 rtn "</textarea>\n</p>\n"))
2662 (with-temp-buffer
2663 (insert rtn)
2664 (goto-char (point-min))
2665 (while (re-search-forward "[<>&]" nil t)
2666 (replace-match (cdr (assq (char-before)
2667 '((?&."&amp;")(?<."&lt;")(?>."&gt;"))))
2668 t t))
2669 (setq rtn (buffer-string)))
2670 (setq rtn (concat "<pre class=\"example\">\n" rtn "</pre>\n"))))
2671 (unless textareap
2672 (setq rtn (org-export-number-lines rtn 1 1 num cont rpllbl fmt)))
2673 (if (string-match "\\(\\`<[^>]*>\\)\n" rtn)
2674 (setq rtn (replace-match "\\1" t nil rtn)))
2675 rtn)
2676 ((eq org-export-current-backend 'latex)
2677 (setq rtn (org-export-number-lines rtn 0 0 num cont rpllbl fmt))
2678 (cond
2679 ((and lang org-export-latex-listings)
2680 (flet ((make-option-string
2681 (pair)
2682 (concat (first pair)
2683 (if (> (length (second pair)) 0)
2684 (concat "=" (second pair))))))
2685 (let* ((lang-sym (intern lang))
2686 (minted-p (eq org-export-latex-listings 'minted))
2687 (listings-p (not minted-p))
2688 (backend-lang
2689 (or (cadr
2690 (assq
2691 lang-sym
2692 (cond
2693 (minted-p org-export-latex-minted-langs)
2694 (listings-p org-export-latex-listings-langs))))
2695 lang))
2696 (custom-environment
2697 (cadr
2698 (assq
2699 lang-sym
2700 org-export-latex-custom-lang-environments))))
2701 (concat
2702 (when (and listings-p (not custom-environment))
2703 (format
2704 "\\lstset{%s}\n"
2705 (mapconcat
2706 #'make-option-string
2707 (append org-export-latex-listings-options
2708 `(("language" ,backend-lang))) ",")))
2709 (when (and caption org-export-latex-listings-w-names)
2710 (format
2711 "\n%s $\\equiv$ \n"
2712 (replace-regexp-in-string "_" "\\\\_" caption)))
2713 (cond
2714 (custom-environment
2715 (format "\\begin{%s}\n%s\\end{%s}\n"
2716 custom-environment rtn custom-environment))
2717 (listings-p
2718 (format "\\begin{%s}\n%s\\end{%s}"
2719 "lstlisting" rtn "lstlisting"))
2720 (minted-p
2721 (format
2722 "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
2723 (mapconcat #'make-option-string
2724 org-export-latex-minted-options ",")
2725 backend-lang rtn)))))))
2726 (t (concat (car org-export-latex-verbatim-wrap)
2727 rtn (cdr org-export-latex-verbatim-wrap)))))
2728 ((eq org-export-current-backend 'ascii)
2729 ;; This is not HTML or LaTeX, so just make it an example.
2730 (setq rtn (org-export-number-lines rtn 0 0 num cont rpllbl fmt))
2731 (concat caption "\n"
2732 (concat
2733 (mapconcat
2734 (lambda (l) (concat " " l))
2735 (org-split-string rtn "\n")
2736 "\n")
2737 "\n")
2740 (error "Don't know how to markup source or example block in %s"
2741 (upcase backend-name)))))
2742 (setq rtn
2743 (concat
2744 "\n#+BEGIN_" backend-name "\n"
2745 (org-add-props rtn
2746 '(org-protected t org-example t org-native-text t))
2747 "\n#+END_" backend-name "\n"))
2748 (org-add-props rtn nil 'original-indentation indent))))
2750 (defun org-export-number-lines (text &optional skip1 skip2 number cont
2751 replace-labels label-format preprocess)
2752 "Apply line numbers to literal examples and handle code references.
2753 Handle user-specified options under info node `(org)Literal
2754 examples' and return the modified source block.
2756 TEXT contains the source or example block.
2758 SKIP1 and SKIP2 are the number of lines that are to be skipped at
2759 the beginning and end of TEXT. Use these to skip over
2760 backend-specific lines pre-pended or appended to the original
2761 source block.
2763 NUMBER is non-nil if the literal example specifies \"+n\" or
2764 \"-n\" switch. If NUMBER is non-nil add line numbers.
2766 CONT is non-nil if the literal example specifies \"+n\" switch.
2767 If CONT is nil, start numbering this block from 1. Otherwise
2768 continue numbering from the last numbered block.
2770 REPLACE-LABELS is dual-purpose.
2771 1. It controls the retention of labels in the exported block.
2772 2. It specifies in what manner the links (or references) to a
2773 labelled line be formatted.
2775 REPLACE-LABELS is the symbol `keep' if the literal example
2776 specifies \"-k\" option, is numeric if the literal example
2777 specifies \"-r\" option and is nil otherwise.
2779 Handle REPLACE-LABELS as below:
2780 - If nil, retain labels in the exported block and use
2781 user-provided labels for referencing the labelled lines.
2782 - If it is a number, remove labels in the exported block and use
2783 one of line numbers or labels for referencing labelled lines based
2784 on NUMBER option.
2785 - If it is a keep, retain labels in the exported block and use
2786 one of line numbers or labels for referencing labelled lines
2787 based on NUMBER option.
2789 LABEL-FORMAT is the value of \"-l\" switch associated with
2790 literal example. See `org-coderef-label-format'.
2792 PREPROCESS is intended for backend-agnostic handling of source
2793 block numbering. When non-nil do the following:
2794 - do not number the lines
2795 - always strip the labels from exported block
2796 - do not make the labelled line a target of an incoming link.
2797 Instead mark the labelled line with `org-coderef' property and
2798 store the label in it."
2799 (setq skip1 (or skip1 0) skip2 (or skip2 0))
2800 (if (and number (not cont)) (setq org-export-last-code-line-counter-value 0))
2801 (with-temp-buffer
2802 (insert text)
2803 (goto-char (point-max))
2804 (skip-chars-backward " \t\n\r")
2805 (delete-region (point) (point-max))
2806 (beginning-of-line (- 1 skip2))
2807 (let* ((last (org-current-line))
2808 (n org-export-last-code-line-counter-value)
2809 (nmax (+ n (- last skip1)))
2810 (fmt (format "%%%dd: " (length (number-to-string nmax))))
2812 (cond
2813 ((eq org-export-current-backend 'html) (format "<span class=\"linenr\">%s</span>"
2814 fmt))
2815 ((eq org-export-current-backend 'ascii) fmt)
2816 ((eq org-export-current-backend 'latex) fmt)
2817 ((eq org-export-current-backend 'docbook) fmt)
2818 (t "")))
2819 (label-format (or label-format org-coderef-label-format))
2820 (label-pre (if (string-match "%s" label-format)
2821 (substring label-format 0 (match-beginning 0))
2822 label-format))
2823 (label-post (if (string-match "%s" label-format)
2824 (substring label-format (match-end 0))
2825 ""))
2826 (lbl-re
2827 (concat
2828 ".*?\\S-.*?\\([ \t]*\\("
2829 (regexp-quote label-pre)
2830 "\\([-a-zA-Z0-9_ ]+\\)"
2831 (regexp-quote label-post)
2832 "\\)\\)"))
2833 ref)
2835 (org-goto-line (1+ skip1))
2836 (while (and (re-search-forward "^" nil t) (not (eobp)) (< n nmax))
2837 (when number (incf n))
2838 (if (or preprocess (not number))
2839 (forward-char 1)
2840 (insert (format fm n)))
2841 (when (looking-at lbl-re)
2842 (setq ref (match-string 3))
2843 (cond ((numberp replace-labels)
2844 ;; remove labels; use numbers for references when lines
2845 ;; are numbered, use labels otherwise
2846 (delete-region (match-beginning 1) (match-end 1))
2847 (push (cons ref (if (> n 0) n ref)) org-export-code-refs))
2848 ((eq replace-labels 'keep)
2849 ;; don't remove labels; use numbers for references when
2850 ;; lines are numbered, use labels otherwise
2851 (goto-char (match-beginning 2))
2852 (delete-region (match-beginning 2) (match-end 2))
2853 (unless preprocess
2854 (insert "(" ref ")"))
2855 (push (cons ref (if (> n 0) n (concat "(" ref ")")))
2856 org-export-code-refs))
2858 ;; don't remove labels and don't use numbers for
2859 ;; references
2860 (goto-char (match-beginning 2))
2861 (delete-region (match-beginning 2) (match-end 2))
2862 (unless preprocess
2863 (insert "(" ref ")"))
2864 (push (cons ref (concat "(" ref ")")) org-export-code-refs)))
2865 (when (and (eq org-export-current-backend 'html) (not preprocess))
2866 (save-excursion
2867 (beginning-of-line 1)
2868 (insert (format "<span id=\"coderef-%s\" class=\"coderef-off\">"
2869 ref))
2870 (end-of-line 1)
2871 (insert "</span>")))
2872 (when preprocess
2873 (add-text-properties
2874 (point-at-bol) (point-at-eol) (list 'org-coderef ref)))))
2875 (setq org-export-last-code-line-counter-value n)
2876 (goto-char (point-max))
2877 (newline)
2878 (buffer-string))))
2880 (defun org-search-todo-below (line lines level)
2881 "Search the subtree below LINE for any TODO entries."
2882 (let ((rest (cdr (memq line lines)))
2883 (re org-todo-line-regexp)
2884 line lv todo)
2885 (catch 'exit
2886 (while (setq line (pop rest))
2887 (if (string-match re line)
2888 (progn
2889 (setq lv (- (match-end 1) (match-beginning 1))
2890 todo (and (match-beginning 2)
2891 (not (member (match-string 2 line)
2892 org-done-keywords))))
2893 ; TODO, not DONE
2894 (if (<= lv level) (throw 'exit nil))
2895 (if todo (throw 'exit t))))))))
2897 ;;;###autoload
2898 (defun org-export-visible (type arg)
2899 "Create a copy of the visible part of the current buffer, and export it.
2900 The copy is created in a temporary buffer and removed after use.
2901 TYPE is the final key (as a string) that also selects the export command in
2902 the \\<org-mode-map>\\[org-export] export dispatcher.
2903 As a special case, if the you type SPC at the prompt, the temporary
2904 org-mode file will not be removed but presented to you so that you can
2905 continue to use it. The prefix arg ARG is passed through to the exporting
2906 command."
2907 (interactive
2908 (list (progn
2909 (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")
2910 (read-char-exclusive))
2911 current-prefix-arg))
2912 (if (not (member type '(?a ?n ?u ?\C-a ?b ?\C-b ?h ?D ?x ?\ ?l ?p ?d ?L ?H ?R)))
2913 (error "Invalid export key"))
2914 (let* ((binding (cdr (assoc type
2916 (?a . org-export-as-ascii)
2917 (?A . org-export-as-ascii-to-buffer)
2918 (?n . org-export-as-latin1)
2919 (?N . org-export-as-latin1-to-buffer)
2920 (?u . org-export-as-utf8)
2921 (?U . org-export-as-utf8-to-buffer)
2922 (?\C-a . org-export-as-ascii)
2923 (?b . org-export-as-html-and-open)
2924 (?\C-b . org-export-as-html-and-open)
2925 (?h . org-export-as-html)
2926 (?H . org-export-as-html-to-buffer)
2927 (?R . org-export-region-as-html)
2928 (?D . org-export-as-docbook)
2930 (?l . org-export-as-latex)
2931 (?p . org-export-as-pdf)
2932 (?d . org-export-as-pdf-and-open)
2933 (?L . org-export-as-latex-to-buffer)
2935 (?x . org-export-as-xoxo)))))
2936 (keepp (equal type ?\ ))
2937 (file buffer-file-name)
2938 (buffer (get-buffer-create "*Org Export Visible*"))
2939 s e)
2940 ;; Need to hack the drawers here.
2941 (save-excursion
2942 (goto-char (point-min))
2943 (while (re-search-forward org-drawer-regexp nil t)
2944 (goto-char (match-beginning 1))
2945 (or (outline-invisible-p) (org-flag-drawer nil))))
2946 (with-current-buffer buffer (erase-buffer))
2947 (save-excursion
2948 (setq s (goto-char (point-min)))
2949 (while (not (= (point) (point-max)))
2950 (goto-char (org-find-invisible))
2951 (append-to-buffer buffer s (point))
2952 (setq s (goto-char (org-find-visible))))
2953 (org-cycle-hide-drawers 'all)
2954 (goto-char (point-min))
2955 (unless keepp
2956 ;; Copy all comment lines to the end, to make sure #+ settings are
2957 ;; still available for the second export step. Kind of a hack, but
2958 ;; does do the trick.
2959 (if (looking-at "#[^\r\n]*")
2960 (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
2961 (when (re-search-forward "^\\*+[ \t]+" nil t)
2962 (while (re-search-backward "[\n\r]#[^\n\r]*" nil t)
2963 (append-to-buffer buffer (1+ (match-beginning 0))
2964 (min (point-max) (1+ (match-end 0)))))))
2965 (set-buffer buffer)
2966 (let ((buffer-file-name file)
2967 (org-inhibit-startup t))
2968 (org-mode)
2969 (show-all)
2970 (unless keepp (funcall binding arg))))
2971 (if (not keepp)
2972 (kill-buffer buffer)
2973 (switch-to-buffer-other-window buffer)
2974 (goto-char (point-min)))))
2976 (defvar org-export-htmlized-org-css-url) ;; defined in org-html.el
2978 (defun org-export-string (string fmt &optional dir)
2979 "Export STRING to FMT using existing export facilities.
2980 During export STRING is saved to a temporary file whose location
2981 could vary. Optional argument DIR can be used to force the
2982 directory in which the temporary file is created during export
2983 which can be useful for resolving relative paths. Dir defaults
2984 to the value of `temporary-file-directory'."
2985 (let ((temporary-file-directory (or dir temporary-file-directory))
2986 (tmp-file (make-temp-file "org-")))
2987 (unwind-protect
2988 (with-temp-buffer
2989 (insert string)
2990 (write-file tmp-file)
2991 (org-load-modules-maybe)
2992 (unless org-local-vars
2993 (setq org-local-vars (org-get-local-variables)))
2994 (eval ;; convert to fmt -- mimicing `org-run-like-in-org-mode'
2995 (list 'let org-local-vars
2996 (list (intern (format "org-export-as-%s" fmt))
2997 nil nil nil ''string t))))
2998 (delete-file tmp-file))))
3000 ;;;###autoload
3001 (defun org-export-as-org (arg &optional hidden ext-plist
3002 to-buffer body-only pub-dir)
3003 "Make a copy with not-exporting stuff removed.
3004 The purpose of this function is to provide a way to export the source
3005 Org file of a webpage in Org format, but with sensitive and/or irrelevant
3006 stuff removed. This command will remove the following:
3008 - archived trees (if the variable `org-export-with-archived-trees' is nil)
3009 - comment blocks and trees starting with the COMMENT keyword
3010 - only trees that are consistent with `org-export-select-tags'
3011 and `org-export-exclude-tags'.
3013 The only arguments that will be used are EXT-PLIST and PUB-DIR,
3014 all the others will be ignored (but are present so that the general
3015 mechanism to call publishing functions will work).
3017 EXT-PLIST is a property list with external parameters overriding
3018 org-mode's default settings, but still inferior to file-local
3019 settings. When PUB-DIR is set, use this as the publishing
3020 directory."
3021 (interactive "P")
3022 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
3023 ext-plist
3024 (org-infile-export-plist)))
3025 (bfname (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
3026 (filename (concat (file-name-as-directory
3027 (or pub-dir
3028 (org-export-directory :org opt-plist)))
3029 (file-name-sans-extension
3030 (file-name-nondirectory bfname))
3031 ".org"))
3032 (filename (and filename
3033 (if (equal (file-truename filename)
3034 (file-truename bfname))
3035 (concat (file-name-sans-extension filename)
3036 "-source."
3037 (file-name-extension filename))
3038 filename)))
3039 (backup-inhibited t)
3040 (buffer (find-file-noselect filename))
3041 (region (buffer-string))
3042 str-ret)
3043 (save-excursion
3044 (org-pop-to-buffer-same-window buffer)
3045 (erase-buffer)
3046 (insert region)
3047 (let ((org-inhibit-startup t)) (org-mode))
3048 (org-install-letbind)
3050 ;; Get rid of archived trees
3051 (org-export-remove-archived-trees (plist-get opt-plist :archived-trees))
3053 ;; Remove comment environment and comment subtrees
3054 (org-export-remove-comment-blocks-and-subtrees)
3056 ;; Get rid of excluded trees
3057 (org-export-handle-export-tags (plist-get opt-plist :select-tags)
3058 (plist-get opt-plist :exclude-tags))
3060 (when (or (plist-get opt-plist :plain-source)
3061 (not (or (plist-get opt-plist :plain-source)
3062 (plist-get opt-plist :htmlized-source))))
3063 ;; Either nothing special is requested (default call)
3064 ;; or the plain source is explicitly requested
3065 ;; so: save it
3066 (save-buffer))
3067 (when (plist-get opt-plist :htmlized-source)
3068 ;; Make the htmlized version
3069 (require 'htmlize)
3070 (require 'org-html)
3071 (font-lock-fontify-buffer)
3072 (let* ((htmlize-output-type 'css)
3073 (newbuf (htmlize-buffer)))
3074 (with-current-buffer newbuf
3075 (when org-export-htmlized-org-css-url
3076 (goto-char (point-min))
3077 (and (re-search-forward
3078 "<style type=\"text/css\">[^\000]*?\n[ \t]*</style>.*"
3079 nil t)
3080 (replace-match
3081 (format
3082 "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\">"
3083 org-export-htmlized-org-css-url)
3084 t t)))
3085 (write-file (concat filename ".html")))
3086 (kill-buffer newbuf)))
3087 (set-buffer-modified-p nil)
3088 (if (equal to-buffer 'string)
3089 (progn (setq str-ret (buffer-string))
3090 (kill-buffer (current-buffer))
3091 str-ret)
3092 (kill-buffer (current-buffer))))))
3094 (defvar org-archive-location) ;; gets loaded with the org-archive require.
3095 (defun org-get-current-options ()
3096 "Return a string with current options as keyword options.
3097 Does include HTML export options as well as TODO and CATEGORY stuff."
3098 (require 'org-archive)
3099 (format
3100 "#+TITLE: %s
3101 #+AUTHOR: %s
3102 #+EMAIL: %s
3103 #+DATE: %s
3104 #+DESCRIPTION:
3105 #+KEYWORDS:
3106 #+LANGUAGE: %s
3107 #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s -:%s f:%s *:%s <:%s
3108 #+OPTIONS: TeX:%s LaTeX:%s skip:%s d:%s todo:%s pri:%s tags:%s
3110 #+EXPORT_SELECT_TAGS: %s
3111 #+EXPORT_EXCLUDE_TAGS: %s
3112 #+LINK_UP: %s
3113 #+LINK_HOME: %s
3114 #+XSLT:
3115 #+CATEGORY: %s
3116 #+SEQ_TODO: %s
3117 #+TYP_TODO: %s
3118 #+PRIORITIES: %c %c %c
3119 #+DRAWERS: %s
3120 #+STARTUP: %s %s %s %s %s
3121 #+TAGS: %s
3122 #+FILETAGS: %s
3123 #+ARCHIVE: %s
3124 #+LINK: %s
3126 (buffer-name) (user-full-name) user-mail-address
3127 (format-time-string (substring (car org-time-stamp-formats) 1 -1))
3128 org-export-default-language
3129 org-export-headline-levels
3130 org-export-with-section-numbers
3131 org-export-with-toc
3132 org-export-preserve-breaks
3133 org-export-html-expand
3134 org-export-with-fixed-width
3135 org-export-with-tables
3136 org-export-with-sub-superscripts
3137 org-export-with-special-strings
3138 org-export-with-footnotes
3139 org-export-with-emphasize
3140 org-export-with-timestamps
3141 org-export-with-TeX-macros
3142 org-export-with-LaTeX-fragments
3143 org-export-skip-text-before-1st-heading
3144 org-export-with-drawers
3145 org-export-with-todo-keywords
3146 org-export-with-priority
3147 org-export-with-tags
3148 (if (featurep 'org-jsinfo) (org-infojs-options-inbuffer-template) "")
3149 (mapconcat 'identity org-export-select-tags " ")
3150 (mapconcat 'identity org-export-exclude-tags " ")
3151 org-export-html-link-up
3152 org-export-html-link-home
3153 (or (ignore-errors
3154 (file-name-sans-extension
3155 (file-name-nondirectory (buffer-file-name (buffer-base-buffer)))))
3156 "NOFILENAME")
3157 "TODO FEEDBACK VERIFY DONE"
3158 "Me Jason Marie DONE"
3159 org-highest-priority org-lowest-priority org-default-priority
3160 (mapconcat 'identity org-drawers " ")
3161 (cdr (assoc org-startup-folded
3162 '((nil . "showall") (t . "overview") (content . "content"))))
3163 (if org-odd-levels-only "odd" "oddeven")
3164 (if org-hide-leading-stars "hidestars" "showstars")
3165 (if org-startup-align-all-tables "align" "noalign")
3166 (cond ((eq org-log-done t) "logdone")
3167 ((equal org-log-done 'note) "lognotedone")
3168 ((not org-log-done) "nologdone"))
3169 (or (mapconcat (lambda (x)
3170 (cond
3171 ((equal :startgroup (car x)) "{")
3172 ((equal :endgroup (car x)) "}")
3173 ((equal :newline (car x)) "")
3174 ((cdr x) (format "%s(%c)" (car x) (cdr x)))
3175 (t (car x))))
3176 (or org-tag-alist (org-get-buffer-tags)) " ") "")
3177 (mapconcat 'identity org-file-tags " ")
3178 org-archive-location
3179 "org file:~/org/%s.org"
3182 ;;;###autoload
3183 (defun org-insert-export-options-template ()
3184 "Insert into the buffer a template with information for exporting."
3185 (interactive)
3186 (if (not (bolp)) (newline))
3187 (let ((s (org-get-current-options)))
3188 (and (string-match "#\\+CATEGORY" s)
3189 (setq s (substring s 0 (match-beginning 0))))
3190 (insert s)))
3192 (defvar org-table-colgroup-info nil)
3194 (defun org-table-clean-before-export (lines &optional maybe-quoted)
3195 "Check if the table has a marking column.
3196 If yes remove the column and the special lines."
3197 (setq org-table-colgroup-info nil)
3198 (if (memq nil
3199 (mapcar
3200 (lambda (x) (or (string-match "^[ \t]*|-" x)
3201 (string-match
3202 (if maybe-quoted
3203 "^[ \t]*| *\\\\?\\([\#!$*_^ /]\\) *|"
3204 "^[ \t]*| *\\([\#!$*_^ /]\\) *|")
3205 x)))
3206 lines))
3207 ;; No special marking column
3208 (progn
3209 (setq org-table-clean-did-remove-column nil)
3210 (delq nil
3211 (mapcar
3212 (lambda (x)
3213 (cond
3214 ((org-table-colgroup-line-p x)
3215 ;; This line contains colgroup info, extract it
3216 ;; and then discard the line
3217 (setq org-table-colgroup-info
3218 (mapcar (lambda (x)
3219 (cond ((member x '("<" "&lt;")) :start)
3220 ((member x '(">" "&gt;")) :end)
3221 ((member x '("<>" "&lt;&gt;")) :startend)
3222 (t nil)))
3223 (org-split-string x "[ \t]*|[ \t]*")))
3224 nil)
3225 ((org-table-cookie-line-p x)
3226 ;; This line contains formatting cookies, discard it
3227 nil)
3228 (t x)))
3229 lines)))
3230 ;; there is a special marking column
3231 (setq org-table-clean-did-remove-column t)
3232 (delq nil
3233 (mapcar
3234 (lambda (x)
3235 (cond
3236 ((org-table-colgroup-line-p x)
3237 ;; This line contains colgroup info, extract it
3238 ;; and then discard the line
3239 (setq org-table-colgroup-info
3240 (mapcar (lambda (x)
3241 (cond ((member x '("<" "&lt;")) :start)
3242 ((member x '(">" "&gt;")) :end)
3243 ((member x '("<>" "&lt;&gt;")) :startend)
3244 (t nil)))
3245 (cdr (org-split-string x "[ \t]*|[ \t]*"))))
3246 nil)
3247 ((org-table-cookie-line-p x)
3248 ;; This line contains formatting cookies, discard it
3249 nil)
3250 ((string-match "^[ \t]*| *[!_^/] *|" x)
3251 ;; ignore this line
3252 nil)
3253 ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
3254 (string-match "^\\([ \t]*\\)|[^|]*|" x))
3255 ;; remove the first column
3256 (replace-match "\\1|" t nil x))))
3257 lines))))
3259 (defun org-export-cleanup-toc-line (s)
3260 "Remove tags and timestamps from lines going into the toc."
3261 (when (memq org-export-with-tags '(not-in-toc nil))
3262 (if (string-match (org-re " +:[[:alnum:]_@#%:]+: *$") s)
3263 (setq s (replace-match "" t t s))))
3264 (when org-export-remove-timestamps-from-toc
3265 (while (string-match org-maybe-keyword-time-regexp s)
3266 (setq s (replace-match "" t t s))))
3267 (while (string-match org-bracket-link-regexp s)
3268 (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
3269 t t s)))
3270 (while (string-match "\\[\\([0-9]\\|fn:[^]]*\\)\\]" s)
3271 (setq s (replace-match "" t t s)))
3275 (defun org-get-text-property-any (pos prop &optional object)
3276 (or (get-text-property pos prop object)
3277 (and (setq pos (next-single-property-change pos prop object))
3278 (get-text-property pos prop object))))
3280 (defun org-export-get-coderef-format (path desc)
3281 (save-match-data
3282 (if (and desc (string-match
3283 (regexp-quote (concat "(" path ")"))
3284 desc))
3285 (replace-match "%s" t t desc)
3286 (or desc "%s"))))
3288 (defun org-export-push-to-kill-ring (format)
3289 "Push buffer content to kill ring.
3290 The depends on the variable `org-export-copy-to-kill-ring'."
3291 (when org-export-copy-to-kill-ring
3292 (org-kill-new (buffer-string))
3293 (when (fboundp 'x-set-selection)
3294 (ignore-errors (x-set-selection 'PRIMARY (buffer-string)))
3295 (ignore-errors (x-set-selection 'CLIPBOARD (buffer-string))))
3296 (message "%s export done, pushed to kill ring and clipboard" format)))
3298 (provide 'org-exp)
3300 ;;; org-exp.el ends here