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