org-table: use HH:MM:SS as the standard display of durations.
[org-mode.git] / lisp / org-exp.el
blob8628d1fd746babf8eceb655bde8a58d57a4fb17a
1 ;;; org-exp.el --- ASCII, HTML, XOXO and iCalendar export for Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
9 ;; Version: 7.6
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;;; Commentary:
29 ;;; Code:
31 (require 'org)
32 (require 'org-macs)
33 (require 'org-agenda)
34 (require 'org-exp-blocks)
35 (require 'ob-exp)
36 (require 'org-src)
38 (eval-when-compile
39 (require 'cl))
41 (declare-function org-export-latex-preprocess "org-latex" (parameters))
42 (declare-function org-export-ascii-preprocess "org-ascii" (parameters))
43 (declare-function org-export-html-preprocess "org-html" (parameters))
44 (declare-function org-export-docbook-preprocess "org-docbook" (parameters))
45 (declare-function org-infojs-options-inbuffer-template "org-jsinfo" ())
46 (declare-function org-export-htmlize-region-for-paste "org-html" (beg end))
47 (declare-function htmlize-buffer "ext:htmlize" (&optional buffer))
48 (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
49 (declare-function org-table-cookie-line-p "org-table" (line))
50 (declare-function org-table-colgroup-line-p "org-table" (line))
51 (declare-function org-pop-to-buffer-same-window "org-compat"
52 (&optional buffer-or-name norecord label))
54 (autoload 'org-export-generic "org-export-generic" "Export using the generic exporter" t)
56 (autoload 'org-export-as-odt "org-odt"
57 "Export the outline to a OpenDocumentText file." t)
58 (autoload 'org-export-as-odt-and-open "org-odt"
59 "Export the outline to a OpenDocumentText file and open it." t)
61 (defgroup org-export nil
62 "Options for exporting org-listings."
63 :tag "Org Export"
64 :group 'org)
66 (defgroup org-export-general nil
67 "General options for exporting Org-mode files."
68 :tag "Org Export General"
69 :group 'org-export)
71 (defcustom org-export-allow-BIND 'confirm
72 "Non-nil means allow #+BIND to define local variable values for export.
73 This is a potential security risk, which is why the user must confirm the
74 use of these lines."
75 :group 'org-export-general
76 :type '(choice
77 (const :tag "Never" nil)
78 (const :tag "Always" t)
79 (const :tag "Make the user confirm for each file" confirm)))
81 ;; FIXME
82 (defvar org-export-publishing-directory nil)
84 (defcustom org-export-show-temporary-export-buffer t
85 "Non-nil means show buffer after exporting to temp buffer.
86 When Org exports to a file, the buffer visiting that file is ever
87 shown, but remains buried. However, when exporting to a temporary
88 buffer, that buffer is popped up in a second window. When this variable
89 is nil, the buffer remains buried also in these cases."
90 :group 'org-export-general
91 :type 'boolean)
93 (defcustom org-export-copy-to-kill-ring t
94 "Non-nil means exported stuff will also be pushed onto the kill ring."
95 :group 'org-export-general
96 :type 'boolean)
98 (defcustom org-export-kill-product-buffer-when-displayed nil
99 "Non-nil means kill the product buffer if it is displayed immediately.
100 This applied to the commands `org-export-as-html-and-open' and
101 `org-export-as-pdf-and-open'."
102 :group 'org-export-general
103 :type 'boolean)
105 (defcustom org-export-run-in-background nil
106 "Non-nil means export and publishing commands will run in background.
107 This works by starting up a separate Emacs process visiting the same file
108 and doing the export from there.
109 Not all export commands are affected by this - only the ones which
110 actually write to a file, and that do not depend on the buffer state.
111 \\<org-mode-map>
112 If this option is nil, you can still get background export by calling
113 `org-export' with a double prefix arg: \
114 \\[universal-argument] \\[universal-argument] \\[org-export].
116 If this option is t, the double prefix can be used to exceptionally
117 force an export command into the current process."
118 :group 'org-export-general
119 :type 'boolean)
121 (defcustom org-export-initial-scope 'buffer
122 "The initial scope when exporting with `org-export'.
123 This variable can be either set to 'buffer or 'subtree."
124 :group 'org-export-general
125 :type '(choice
126 (const :tag "Export current buffer" 'buffer)
127 (const :tag "Export current subtree" 'subtree)))
129 (defcustom org-export-select-tags '("export")
130 "Tags that select a tree for export.
131 If any such tag is found in a buffer, all trees that do not carry one
132 of these tags will be deleted before export.
133 Inside trees that are selected like this, you can still deselect a
134 subtree by tagging it with one of the `org-export-exclude-tags'."
135 :group 'org-export-general
136 :type '(repeat (string :tag "Tag")))
138 (defcustom org-export-exclude-tags '("noexport")
139 "Tags that exclude a tree from export.
140 All trees carrying any of these tags will be excluded from export.
141 This is without condition, so even subtrees inside that carry one of the
142 `org-export-select-tags' will be removed."
143 :group 'org-export-general
144 :type '(repeat (string :tag "Tag")))
146 ;; FIXME: rename, this is a general variable
147 (defcustom org-export-html-expand t
148 "Non-nil means for HTML export, treat @<...> as HTML tag.
149 When nil, these tags will be exported as plain text and therefore
150 not be interpreted by a browser.
152 This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
153 :group 'org-export-html
154 :group 'org-export-general
155 :type 'boolean)
157 (defcustom org-export-with-special-strings t
158 "Non-nil means interpret \"\-\", \"--\" and \"---\" for export.
159 When this option is turned on, these strings will be exported as:
161 Org HTML LaTeX
162 -----+----------+--------
163 \\- &shy; \\-
164 -- &ndash; --
165 --- &mdash; ---
166 ... &hellip; \ldots
168 This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
169 :group 'org-export-translation
170 :type 'boolean)
172 (defcustom org-export-html-link-up ""
173 "Where should the \"UP\" link of exported HTML pages lead?"
174 :group 'org-export-html
175 :group 'org-export-general
176 :type '(string :tag "File or URL"))
178 (defcustom org-export-html-link-home ""
179 "Where should the \"HOME\" link of exported HTML pages lead?"
180 :group 'org-export-html
181 :group 'org-export-general
182 :type '(string :tag "File or URL"))
184 (defcustom org-export-language-setup
185 '(("en" "Author" "Date" "Table of Contents" "Footnotes")
186 ("ca" "Autor" "Data" "&Iacute;ndex" "Peus de p&agrave;gina")
187 ("cs" "Autor" "Datum" "Obsah" "Pozn\xe1mky pod carou")
188 ("da" "Ophavsmand" "Dato" "Indhold" "Fodnoter")
189 ("de" "Autor" "Datum" "Inhaltsverzeichnis" "Fu&szlig;noten")
190 ("eo" "A&#365;toro" "Dato" "Enhavo" "Piednotoj")
191 ("es" "Autor" "Fecha" "&Iacute;ndice" "Pies de p&aacute;gina")
192 ("fi" "Tekij&auml;" "P&auml;iv&auml;m&auml;&auml;r&auml;" "Sis&auml;llysluettelo" "Alaviitteet")
193 ("fr" "Auteur" "Date" "Table des mati&egrave;res" "Notes de bas de page")
194 ("hu" "Szerz&otilde;" "D&aacute;tum" "Tartalomjegyz&eacute;k" "L&aacute;bjegyzet")
195 ("is" "H&ouml;fundur" "Dagsetning" "Efnisyfirlit" "Aftanm&aacute;lsgreinar")
196 ("it" "Autore" "Data" "Indice" "Note a pi&egrave; di pagina")
197 ("nl" "Auteur" "Datum" "Inhoudsopgave" "Voetnoten")
198 ("no" "Forfatter" "Dato" "Innhold" "Fotnoter")
199 ("nb" "Forfatter" "Dato" "Innhold" "Fotnoter") ;; nb = Norsk (bokm.l)
200 ("nn" "Forfattar" "Dato" "Innhald" "Fotnotar") ;; nn = Norsk (nynorsk)
201 ("pl" "Autor" "Data" "Spis tre&#x015b;ci" "Przypis")
202 ("sv" "F&ouml;rfattare" "Datum" "Inneh&aring;ll" "Fotnoter"))
203 "Terms used in export text, translated to different languages.
204 Use the variable `org-export-default-language' to set the language,
205 or use the +OPTION lines for a per-file setting."
206 :group 'org-export-general
207 :type '(repeat
208 (list
209 (string :tag "HTML language tag")
210 (string :tag "Author")
211 (string :tag "Date")
212 (string :tag "Table of Contents")
213 (string :tag "Footnotes"))))
215 (defcustom org-export-default-language "en"
216 "The default language for export and clocktable translations, as a string.
217 This should have an association in `org-export-language-setup'
218 and in `org-clock-clocktable-language-setup'."
219 :group 'org-export-general
220 :type 'string)
222 (defvar org-export-page-description ""
223 "The page description, for the XHTML meta tag.
224 This is best set with the #+DESCRIPTION line in a file, it does not make
225 sense to set this globally.")
227 (defvar org-export-page-keywords ""
228 "The page description, for the XHTML meta tag.
229 This is best set with the #+KEYWORDS line in a file, it does not make
230 sense to set this globally.")
232 (defcustom org-export-skip-text-before-1st-heading nil
233 "Non-nil means skip all text before the first headline when exporting.
234 When nil, that text is exported as well."
235 :group 'org-export-general
236 :type 'boolean)
238 (defcustom org-export-headline-levels 3
239 "The last level which is still exported as a headline.
240 Inferior levels will produce itemize lists when exported.
241 Note that a numeric prefix argument to an exporter function overrides
242 this setting.
244 This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
245 :group 'org-export-general
246 :type 'integer)
248 (defcustom org-export-with-section-numbers t
249 "Non-nil means add section numbers to headlines when exporting.
251 This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
252 :group 'org-export-general
253 :type 'boolean)
255 (defcustom org-export-section-number-format '((("1" ".")) . "")
256 "Format of section numbers for export.
257 The variable has two components.
258 1. A list of lists, each indicating a counter type and a separator.
259 The counter type can be any of \"1\", \"A\", \"a\", \"I\", or \"i\".
260 It causes causes numeric, alphabetic, or roman counters, respectively.
261 The separator is only used if another counter for a subsection is being
262 added.
263 If there are more numbered section levels than entries in this lists,
264 then the last entry will be reused.
265 2. A terminator string that will be added after the entire
266 section number."
267 :group 'org-export-general
268 :type '(cons
269 (repeat
270 (list
271 (string :tag "Counter Type")
272 (string :tag "Separator ")))
273 (string :tag "Terminator")))
275 (defcustom org-export-with-toc t
276 "Non-nil means create a table of contents in exported files.
277 The TOC contains headlines with levels up to`org-export-headline-levels'.
278 When an integer, include levels up to N in the toc, this may then be
279 different from `org-export-headline-levels', but it will not be allowed
280 to be larger than the number of headline levels.
281 When nil, no table of contents is made.
283 Headlines which contain any TODO items will be marked with \"(*)\" in
284 ASCII export, and with red color in HTML output, if the option
285 `org-export-mark-todo-in-toc' is set.
287 In HTML output, the TOC will be clickable.
289 This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
290 or \"toc:3\"."
291 :group 'org-export-general
292 :type '(choice
293 (const :tag "No Table of Contents" nil)
294 (const :tag "Full Table of Contents" t)
295 (integer :tag "TOC to level")))
297 (defcustom org-export-mark-todo-in-toc nil
298 "Non-nil means mark TOC lines that contain any open TODO items."
299 :group 'org-export-general
300 :type 'boolean)
302 (defcustom org-export-with-todo-keywords t
303 "Non-nil means include TODO keywords in export.
304 When nil, remove all these keywords from the export."
305 :group 'org-export-general
306 :type 'boolean)
308 (defcustom org-export-with-tasks t
309 "Non-nil means include TODO items for export.
310 This may have the following values:
311 t include tasks independent of state.
312 todo include only tasks that are not yet done.
313 done include only tasks that are already done.
314 nil remove all tasks before export
315 list of TODO kwds keep only tasks with these keywords"
316 :group 'org-export-general
317 :type '(choice
318 (const :tag "All tasks" t)
319 (const :tag "No tasks" nil)
320 (const :tag "Not-done tasks" todo)
321 (const :tag "Only done tasks" done)
322 (repeat :tag "Specific TODO keywords"
323 (string :tag "Keyword"))))
325 (defcustom org-export-with-priority nil
326 "Non-nil means include priority cookies in export.
327 When nil, remove priority cookies for export."
328 :group 'org-export-general
329 :type 'boolean)
331 (defcustom org-export-preserve-breaks nil
332 "Non-nil means preserve all line breaks when exporting.
333 Normally, in HTML output paragraphs will be reformatted. In ASCII
334 export, line breaks will always be preserved, regardless of this variable.
336 This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
337 :group 'org-export-general
338 :type 'boolean)
340 (defcustom org-export-with-archived-trees 'headline
341 "Whether subtrees with the ARCHIVE tag should be exported.
342 This can have three different values
343 nil Do not export, pretend this tree is not present
344 t Do export the entire tree
345 headline Only export the headline, but skip the tree below it."
346 :group 'org-export-general
347 :group 'org-archive
348 :type '(choice
349 (const :tag "not at all" nil)
350 (const :tag "headline only" 'headline)
351 (const :tag "entirely" t)))
353 (defcustom org-export-author-info t
354 "Non-nil means insert author name and email into the exported file.
356 This option can also be set with the +OPTIONS line,
357 e.g. \"author:nil\"."
358 :group 'org-export-general
359 :type 'boolean)
361 (defcustom org-export-email-info nil
362 "Non-nil means insert author name and email into the exported file.
364 This option can also be set with the +OPTIONS line,
365 e.g. \"email:t\"."
366 :group 'org-export-general
367 :type 'boolean)
369 (defcustom org-export-creator-info t
370 "Non-nil means the postamble should contain a creator sentence.
371 This sentence is \"HTML generated by org-mode XX in emacs XXX\"."
372 :group 'org-export-general
373 :type 'boolean)
375 (defcustom org-export-time-stamp-file t
376 "Non-nil means insert a time stamp into the exported file.
377 The time stamp shows when the file was created.
379 This option can also be set with the +OPTIONS line,
380 e.g. \"timestamp:nil\"."
381 :group 'org-export-general
382 :type 'boolean)
384 (defcustom org-export-with-timestamps t
385 "If nil, do not export time stamps and associated keywords."
386 :group 'org-export-general
387 :type 'boolean)
389 (defcustom org-export-remove-timestamps-from-toc t
390 "If t, remove timestamps from the table of contents entries."
391 :group 'org-export-general
392 :type 'boolean)
394 (defcustom org-export-with-tags 'not-in-toc
395 "If nil, do not export tags, just remove them from headlines.
396 If this is the symbol `not-in-toc', tags will be removed from table of
397 contents entries, but still be shown in the headlines of the document.
399 This option can also be set with the +OPTIONS line, e.g. \"tags:nil\"."
400 :group 'org-export-general
401 :type '(choice
402 (const :tag "Off" nil)
403 (const :tag "Not in TOC" not-in-toc)
404 (const :tag "On" t)))
406 (defcustom org-export-with-drawers nil
407 "Non-nil means export with drawers like the property drawer.
408 When t, all drawers are exported. This may also be a list of
409 drawer names to export."
410 :group 'org-export-general
411 :type '(choice
412 (const :tag "All drawers" t)
413 (const :tag "None" nil)
414 (repeat :tag "Selected drawers"
415 (string :tag "Drawer name"))))
417 (defvar org-export-first-hook nil
418 "Hook called as the first thing in each exporter.
419 Point will be still in the original buffer.
420 Good for general initialization")
422 (defvar org-export-preprocess-hook nil
423 "Hook for preprocessing an export buffer.
424 Pretty much the first thing when exporting is running this hook.
425 Point will be in a temporary buffer that contains a copy of
426 the original buffer, or of the section that is being exported.
427 All the other hooks in the org-export-preprocess... category
428 also work in that temporary buffer, already modified by various
429 stages of the processing.")
431 (defvar org-export-preprocess-after-include-files-hook nil
432 "Hook for preprocessing an export buffer.
433 This is run after the contents of included files have been inserted.")
435 (defvar org-export-preprocess-after-tree-selection-hook nil
436 "Hook for preprocessing an export buffer.
437 This is run after selection of trees to be exported has happened.
438 This selection includes tags-based selection, as well as removal
439 of commented and archived trees.")
441 (defvar org-export-preprocess-after-headline-targets-hook nil
442 "Hook for preprocessing export buffer.
443 This is run just after the headline targets have been defined and
444 the target-alist has been set up.")
446 (defvar org-export-preprocess-before-selecting-backend-code-hook nil
447 "Hook for preprocessing an export buffer.
448 This is run just before backend-specific blocks get selected.")
450 (defvar org-export-preprocess-after-blockquote-hook nil
451 "Hook for preprocessing an export buffer.
452 This is run after blockquote/quote/verse/center have been marked
453 with cookies.")
455 (defvar org-export-preprocess-after-radio-targets-hook nil
456 "Hook for preprocessing an export buffer.
457 This is run after radio target processing.")
459 (defvar org-export-preprocess-before-normalizing-links-hook nil
460 "Hook for preprocessing an export buffer.
461 This hook is run before links are normalized.")
463 (defvar org-export-preprocess-before-backend-specifics-hook nil
464 "Hook run before backend-specific functions are called during preprocessing.")
466 (defvar org-export-preprocess-final-hook nil
467 "Hook for preprocessing an export buffer.
468 This is run as the last thing in the preprocessing buffer, just before
469 returning the buffer string to the backend.")
471 (defgroup org-export-translation nil
472 "Options for translating special ascii sequences for the export backends."
473 :tag "Org Export Translation"
474 :group 'org-export)
476 (defcustom org-export-with-emphasize t
477 "Non-nil means interpret *word*, /word/, and _word_ as emphasized text.
478 If the export target supports emphasizing text, the word will be
479 typeset in bold, italic, or underlined, respectively. Works only for
480 single words, but you can say: I *really* *mean* *this*.
481 Not all export backends support this.
483 This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
484 :group 'org-export-translation
485 :type 'boolean)
487 (defcustom org-export-with-footnotes t
488 "If nil, export [1] as a footnote marker.
489 Lines starting with [1] will be formatted as footnotes.
491 This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
492 :group 'org-export-translation
493 :type 'boolean)
495 (defcustom org-export-with-TeX-macros t
496 "Non-nil means interpret simple TeX-like macros when exporting.
497 For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
498 Not only real TeX macros will work here, but the standard HTML entities
499 for math can be used as macro names as well. For a list of supported
500 names in HTML export, see the constant `org-entities' and the user option
501 `org-entities-user'.
502 Not all export backends support this.
504 This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
505 :group 'org-export-translation
506 :group 'org-export-latex
507 :type 'boolean)
509 (defcustom org-export-with-LaTeX-fragments t
510 "Non-nil means process LaTeX math fragments for HTML display.
511 When set, the exporter will find and process LaTeX environments if the
512 \\begin line is the first non-white thing on a line. It will also find
513 and process the math delimiters like $a=b$ and \\( a=b \\) for inline math,
514 $$a=b$$ and \\=\\[ a=b \\] for display math.
516 This option can also be set with the +OPTIONS line, e.g. \"LaTeX:mathjax\".
518 Allowed values are:
520 nil Don't do anything.
521 verbatim Keep everything in verbatim
522 dvipng Process the LaTeX fragments to images.
523 This will also include processing of non-math environments.
524 t Do MathJax preprocessing if there is at least on math snippet,
525 and arrange for MathJax.js to be loaded.
527 The default is nil, because this option needs the `dvipng' program which
528 is not available on all systems."
529 :group 'org-export-translation
530 :group 'org-export-latex
531 :type '(choice
532 (const :tag "Do not process math in any way" nil)
533 (const :tag "Obsolete, use dvipng setting" t)
534 (const :tag "Use dvipng to make images" dvipng)
535 (const :tag "Use MathJax to display math" mathjax)
536 (const :tag "Leave math verbatim" verbatim)))
538 (defcustom org-export-with-fixed-width t
539 "Non-nil means lines starting with \":\" will be in fixed width font.
540 This can be used to have pre-formatted text, fragments of code etc. For
541 example:
542 : ;; Some Lisp examples
543 : (while (defc cnt)
544 : (ding))
545 will be looking just like this in also HTML. See also the QUOTE keyword.
546 Not all export backends support this.
548 This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
549 :group 'org-export-translation
550 :type 'boolean)
552 (defgroup org-export-tables nil
553 "Options for exporting tables in Org-mode."
554 :tag "Org Export Tables"
555 :group 'org-export)
557 (defcustom org-export-with-tables t
558 "If non-nil, lines starting with \"|\" define a table.
559 For example:
561 | Name | Address | Birthday |
562 |-------------+----------+-----------|
563 | Arthur Dent | England | 29.2.2100 |
565 Not all export backends support this.
567 This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
568 :group 'org-export-tables
569 :type 'boolean)
571 (defcustom org-export-highlight-first-table-line t
572 "Non-nil means highlight the first table line.
573 In HTML export, this means use <th> instead of <td>.
574 In tables created with table.el, this applies to the first table line.
575 In Org-mode tables, all lines before the first horizontal separator
576 line will be formatted with <th> tags."
577 :group 'org-export-tables
578 :type 'boolean)
580 (defcustom org-export-table-remove-special-lines t
581 "Remove special lines and marking characters in calculating tables.
582 This removes the special marking character column from tables that are set
583 up for spreadsheet calculations. It also removes the entire lines
584 marked with `!', `_', or `^'. The lines with `$' are kept, because
585 the values of constants may be useful to have."
586 :group 'org-export-tables
587 :type 'boolean)
589 (defcustom org-export-table-remove-empty-lines t
590 "Remove empty lines when exporting tables.
591 This is the global equivalent of the :remove-nil-lines option
592 when locally sending a table with #+ORGTBL."
593 :group 'org-export-tables
594 :type 'boolean)
596 (defcustom org-export-prefer-native-exporter-for-tables nil
597 "Non-nil means always export tables created with table.el natively.
598 Natively means use the HTML code generator in table.el.
599 When nil, Org-mode's own HTML generator is used when possible (i.e. if
600 the table does not use row- or column-spanning). This has the
601 advantage, that the automatic HTML conversions for math symbols and
602 sub/superscripts can be applied. Org-mode's HTML generator is also
603 much faster. The LaTeX exporter always use the native exporter for
604 table.el tables."
605 :group 'org-export-tables
606 :type 'boolean)
608 ;;;; Exporting
610 ;;; Variables, constants, and parameter plists
612 (defconst org-level-max 20)
614 (defvar org-export-current-backend nil
615 "During export, this will be bound to a symbol such as 'html,
616 'latex, 'docbook, 'ascii, etc, indicating which of the export
617 backends is in use. Otherwise it has the value nil. Users
618 should not attempt to change the value of this variable
619 directly, but it can be used in code to test whether export is
620 in progress, and if so, what the backend is.")
622 (defvar org-current-export-file nil) ; dynamically scoped parameter
623 (defvar org-current-export-dir nil) ; dynamically scoped parameter
624 (defvar org-export-opt-plist nil
625 "Contains the current option plist.")
626 (defvar org-last-level nil) ; dynamically scoped variable
627 (defvar org-min-level nil) ; dynamically scoped variable
628 (defvar org-levels-open nil) ; dynamically scoped parameter
629 (defvar org-export-footnotes-data nil
630 "Alist of labels used in buffers, along with their definition.")
631 (defvar org-export-footnotes-seen nil
632 "Alist of labels encountered so far by the exporter, along with their definition.")
635 (defconst org-export-plist-vars
636 '((:link-up nil org-export-html-link-up)
637 (:link-home nil org-export-html-link-home)
638 (:language nil org-export-default-language)
639 (:keywords nil org-export-page-keywords)
640 (:description nil org-export-page-description)
641 (:customtime nil org-display-custom-times)
642 (:headline-levels "H" org-export-headline-levels)
643 (:section-numbers "num" org-export-with-section-numbers)
644 (:section-number-format nil org-export-section-number-format)
645 (:table-of-contents "toc" org-export-with-toc)
646 (:preserve-breaks "\\n" org-export-preserve-breaks)
647 (:archived-trees nil org-export-with-archived-trees)
648 (:emphasize "*" org-export-with-emphasize)
649 (:sub-superscript "^" org-export-with-sub-superscripts)
650 (:special-strings "-" org-export-with-special-strings)
651 (:footnotes "f" org-export-with-footnotes)
652 (:drawers "d" org-export-with-drawers)
653 (:tags "tags" org-export-with-tags)
654 (:todo-keywords "todo" org-export-with-todo-keywords)
655 (:tasks "tasks" org-export-with-tasks)
656 (:priority "pri" org-export-with-priority)
657 (:TeX-macros "TeX" org-export-with-TeX-macros)
658 (:LaTeX-fragments "LaTeX" org-export-with-LaTeX-fragments)
659 (:latex-listings nil org-export-latex-listings)
660 (:skip-before-1st-heading "skip" org-export-skip-text-before-1st-heading)
661 (:fixed-width ":" org-export-with-fixed-width)
662 (:timestamps "<" org-export-with-timestamps)
663 (:author nil user-full-name)
664 (:email nil user-mail-address)
665 (:author-info "author" org-export-author-info)
666 (:email-info "email" org-export-email-info)
667 (:creator-info "creator" org-export-creator-info)
668 (:time-stamp-file "timestamp" org-export-time-stamp-file)
669 (:tables "|" org-export-with-tables)
670 (:table-auto-headline nil org-export-highlight-first-table-line)
671 (:style-include-default nil org-export-html-style-include-default)
672 (:style-include-scripts nil org-export-html-style-include-scripts)
673 (:style nil org-export-html-style)
674 (:style-extra nil org-export-html-style-extra)
675 (:agenda-style nil org-agenda-export-html-style)
676 (:convert-org-links nil org-export-html-link-org-files-as-html)
677 (:inline-images nil org-export-html-inline-images)
678 (:html-extension nil org-export-html-extension)
679 (:html-preamble nil org-export-html-preamble)
680 (:html-postamble nil org-export-html-postamble)
681 (:xml-declaration nil org-export-html-xml-declaration)
682 (:html-table-tag nil org-export-html-table-tag)
683 (:expand-quoted-html "@" org-export-html-expand)
684 (:timestamp nil org-export-html-with-timestamp)
685 (:publishing-directory nil org-export-publishing-directory)
686 (:select-tags nil org-export-select-tags)
687 (:exclude-tags nil org-export-exclude-tags)
689 (:latex-image-options nil org-export-latex-image-default-option))
690 "List of properties that represent export/publishing variables.
691 Each element is a list of 3 items:
692 1. The property that is used internally, and also for org-publish-project-alist
693 2. The string that can be used in the OPTION lines to set this option,
694 or nil if this option cannot be changed in this way
695 3. The customization variable that sets the default for this option."
698 (defun org-default-export-plist ()
699 "Return the property list with default settings for the export variables."
700 (let* ((infile (org-infile-export-plist))
701 (letbind (plist-get infile :let-bind))
702 (l org-export-plist-vars) rtn e s v)
703 (while (setq e (pop l))
704 (setq s (nth 2 e)
705 v (cond
706 ((assq s letbind) (nth 1 (assq s letbind)))
707 ((boundp s) (symbol-value s))
708 (t nil))
709 rtn (cons (car e) (cons v rtn))))
710 rtn))
712 (defvar org-export-inbuffer-options-extra nil
713 "List of additional in-buffer options that should be detected.
714 Just before export, the buffer is scanned for options like #+TITLE, #+EMAIL,
715 etc. Extensions can add to this list to get their options detected, and they
716 can then add a function to `org-export-options-filters' to process these
717 options.
718 Each element in this list must be a list, with the in-buffer keyword as car,
719 and a property (a symbol) as the next element. All occurrences of the
720 keyword will be found, the values concatenated with a space character
721 in between, and the result stored in the export options property list.")
723 (defvar org-export-options-filters nil
724 "Functions to be called to finalize the export/publishing options.
725 All these options are stored in a property list, and each of the functions
726 in this hook gets a chance to modify this property list. Each function
727 must accept the property list as an argument, and must return the (possibly
728 modified) list.")
730 ;; FIXME: should we fold case here?
731 (defun org-infile-export-plist ()
732 "Return the property list with file-local settings for export."
733 (save-excursion
734 (save-restriction
735 (widen)
736 (goto-char (point-min))
737 (let ((re (org-make-options-regexp
738 (append
739 '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE"
740 "MATHJAX"
741 "LINK_UP" "LINK_HOME" "SETUPFILE" "STYLE"
742 "LATEX_HEADER" "LATEX_CLASS"
743 "EXPORT_SELECT_TAGS" "EXPORT_EXCLUDE_TAGS"
744 "KEYWORDS" "DESCRIPTION" "MACRO" "BIND" "XSLT")
745 (mapcar 'car org-export-inbuffer-options-extra))))
746 (case-fold-search t)
747 p key val text options mathjax a pr style
748 latex-header latex-class macros letbind
749 ext-setup-or-nil setup-contents (start 0))
750 (while (or (and ext-setup-or-nil
751 (string-match re ext-setup-or-nil start)
752 (setq start (match-end 0)))
753 (and (setq ext-setup-or-nil nil start 0)
754 (re-search-forward re nil t)))
755 (setq key (upcase (org-match-string-no-properties 1 ext-setup-or-nil))
756 val (org-match-string-no-properties 2 ext-setup-or-nil))
757 (cond
758 ((setq a (assoc key org-export-inbuffer-options-extra))
759 (setq pr (nth 1 a))
760 (setq p (plist-put p pr (concat (plist-get p pr) " " val))))
761 ((string-equal key "TITLE") (setq p (plist-put p :title val)))
762 ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
763 ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
764 ((string-equal key "DATE") (setq p (plist-put p :date val)))
765 ((string-equal key "KEYWORDS") (setq p (plist-put p :keywords val)))
766 ((string-equal key "DESCRIPTION")
767 (setq p (plist-put p :description val)))
768 ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
769 ((string-equal key "STYLE")
770 (setq style (concat style "\n" val)))
771 ((string-equal key "LATEX_HEADER")
772 (setq latex-header (concat latex-header "\n" val)))
773 ((string-equal key "LATEX_CLASS")
774 (setq latex-class val))
775 ((string-equal key "TEXT")
776 (setq text (if text (concat text "\n" val) val)))
777 ((string-equal key "OPTIONS")
778 (setq options (concat val " " options)))
779 ((string-equal key "MATHJAX")
780 (setq mathjax (concat val " " mathjax)))
781 ((string-equal key "BIND")
782 (push (read (concat "(" val ")")) letbind))
783 ((string-equal key "XSLT")
784 (setq p (plist-put p :xslt val)))
785 ((string-equal key "LINK_UP")
786 (setq p (plist-put p :link-up val)))
787 ((string-equal key "LINK_HOME")
788 (setq p (plist-put p :link-home val)))
789 ((string-equal key "EXPORT_SELECT_TAGS")
790 (setq p (plist-put p :select-tags (org-split-string val))))
791 ((string-equal key "EXPORT_EXCLUDE_TAGS")
792 (setq p (plist-put p :exclude-tags (org-split-string val))))
793 ((string-equal key "MACRO")
794 (push val macros))
795 ((equal key "SETUPFILE")
796 (setq setup-contents (org-file-contents
797 (expand-file-name
798 (org-remove-double-quotes
799 (org-trim val)))
800 '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 (looking-at org-list-end-re))
1790 (replace-match ""))
1791 (unless (bolp) (insert "\n"))
1792 ;; As org-list-end is inserted at column 0, it would end
1793 ;; by indentation any list. It can be problematic when
1794 ;; there are lists within lists: the inner list end would
1795 ;; also become the outer list end. To avoid this, text
1796 ;; property `original-indentation' is added, as
1797 ;; `org-list-struct' pays attention to it when reading a
1798 ;; list.
1799 (insert (org-add-props
1800 "ORG-LIST-END-MARKER\n"
1801 (list 'original-indentation top-ind)))))))
1802 (cons nil org-list-export-context))))
1804 (defun org-export-mark-list-properties ()
1805 "Mark list with special properties.
1806 These special properties will later be interpreted by the backend."
1807 (let ((mark-list
1808 (function
1809 ;; Mark a list with 3 properties: `list-item' which is
1810 ;; position at beginning of line, `list-struct' which is
1811 ;; list structure, and `list-prevs' which is the alist of
1812 ;; item and its predecessor. Leave point at list ending.
1813 (lambda (ctxt)
1814 (let* ((struct (org-list-struct))
1815 (top (org-list-get-top-point struct))
1816 (bottom (org-list-get-bottom-point struct))
1817 (prevs (org-list-prevs-alist struct))
1818 poi)
1819 ;; Get every item and ending position, without dups and
1820 ;; without bottom point of list.
1821 (mapc (lambda (e)
1822 (let ((pos (car e))
1823 (end (nth 6 e)))
1824 (unless (memq pos poi)
1825 (push pos poi))
1826 (unless (or (= end bottom) (memq end poi))
1827 (push end poi))))
1828 struct)
1829 (setq poi (sort poi '<))
1830 ;; For every point of interest, mark the whole line with
1831 ;; its position in list.
1832 (mapc
1833 (lambda (e)
1834 (goto-char e)
1835 (add-text-properties (point-at-bol) (point-at-eol)
1836 (list 'list-item (point-at-bol)
1837 'list-struct struct
1838 'list-prevs prevs)))
1839 poi)
1840 ;; Take care of bottom point. As babel may have inserted
1841 ;; a new list in buffer, list ending isn't always
1842 ;; marked. Now mark every list ending and add properties
1843 ;; useful to line processing exporters.
1844 (goto-char bottom)
1845 (when (or (looking-at "^ORG-LIST-END-MARKER\n")
1846 (and (not (eq org-list-ending-method 'indent))
1847 (looking-at org-list-end-re)))
1848 (replace-match ""))
1849 (unless (bolp) (insert "\n"))
1850 (insert
1851 (org-add-props "ORG-LIST-END-MARKER\n" (list 'list-item bottom
1852 'list-struct struct
1853 'list-prevs prevs)))
1854 ;; Following property is used by LaTeX exporter.
1855 (add-text-properties top (point) (list 'list-context ctxt)))))))
1856 ;; Mark lists except for backends not interpreting them.
1857 (unless (eq org-export-current-backend 'ascii)
1858 (let ((org-list-end-re "^ORG-LIST-END-MARKER\n"))
1859 (mapc
1860 (lambda (e)
1861 (goto-char (point-min))
1862 (while (re-search-forward (org-item-beginning-re) nil t)
1863 (let ((context (nth 2 (org-list-context))))
1864 (if (eq context e)
1865 (funcall mark-list e)
1866 (put-text-property (point-at-bol) (point-at-eol)
1867 'list-context context)))))
1868 (cons nil org-list-export-context))))))
1870 (defun org-export-attach-captions-and-attributes (target-alist)
1871 "Move #+CAPTION, #+ATTR_BACKEND, and #+LABEL text into text properties.
1872 If the next thing following is a table, add the text properties to the first
1873 table line. If it is a link, add it to the line containing the link."
1874 (goto-char (point-min))
1875 (remove-text-properties (point-min) (point-max)
1876 '(org-caption nil org-attributes nil))
1877 (let ((case-fold-search t)
1878 (re (concat "^[ \t]*#\\+caption:[ \t]+\\(.*\\)"
1879 "\\|"
1880 "^[ \t]*#\\+attr_" (symbol-name org-export-current-backend) ":[ \t]+\\(.*\\)"
1881 "\\|"
1882 "^[ \t]*#\\+label:[ \t]+\\(.*\\)"
1883 "\\|"
1884 "^[ \t]*\\(|[^-]\\)"
1885 "\\|"
1886 "^[ \t]*\\[\\[.*\\]\\][ \t]*$"))
1887 cap shortn attr label end)
1888 (while (re-search-forward re nil t)
1889 (cond
1890 ;; there is a caption
1891 ((match-end 1)
1892 (progn
1893 (setq cap (concat cap (if cap " " "") (org-trim (match-string 1))))
1894 (when (string-match "\\[\\(.*\\)\\]{\\(.*\\)}" cap)
1895 (setq shortn (match-string 1 cap)
1896 cap (match-string 2 cap)))
1897 (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))))
1898 ;; there is an attribute
1899 ((match-end 2)
1900 (progn
1901 (setq attr (concat attr (if attr " " "") (org-trim (match-string 2))))
1902 (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))))
1903 ;; there is a label
1904 ((match-end 3)
1905 (progn
1906 (setq label (org-trim (match-string 3)))
1907 (delete-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))))
1909 (setq end (if (match-end 4)
1910 (let ((ee (org-table-end)))
1911 (prog1 (1- (marker-position ee)) (move-marker ee nil)))
1912 (point-at-eol)))
1913 (add-text-properties (point-at-bol) end
1914 (list 'org-caption cap
1915 'org-caption-shortn shortn
1916 'org-attributes attr
1917 'org-label label))
1918 (if label (push (cons label label) target-alist))
1919 (goto-char end)
1920 (setq cap nil shortn nil attr nil label nil)))))
1921 target-alist)
1923 (defun org-export-remove-comment-blocks-and-subtrees ()
1924 "Remove the comment environment, and also commented subtrees."
1925 (let ((re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
1926 case-fold-search)
1927 ;; Remove comment environment
1928 (goto-char (point-min))
1929 (setq case-fold-search t)
1930 (while (re-search-forward
1931 "^#\\+begin_comment[ \t]*\n[^\000]*?\n#\\+end_comment\\>.*" nil t)
1932 (replace-match "" t t))
1933 ;; Remove subtrees that are commented
1934 (goto-char (point-min))
1935 (setq case-fold-search nil)
1936 (while (re-search-forward re-commented nil t)
1937 (goto-char (match-beginning 0))
1938 (delete-region (point) (org-end-of-subtree t)))))
1940 (defun org-export-handle-comments (org-commentsp)
1941 "Remove comments, or convert to backend-specific format.
1942 ORG-COMMENTSP can be a format string for publishing comments.
1943 When it is nil, all comments will be removed."
1944 (let ((re "^\\(#\\|[ \t]*#\\+ \\)\\(.*\n?\\)")
1945 pos)
1946 (goto-char (point-min))
1947 (while (or (looking-at re)
1948 (re-search-forward re nil t))
1949 (setq pos (match-beginning 0))
1950 (if (get-text-property pos 'org-protected)
1951 (goto-char (1+ pos))
1952 (if (and org-commentsp
1953 (not (equal (char-before (match-end 1)) ?+)))
1954 (progn (add-text-properties
1955 (match-beginning 0) (match-end 0) '(org-protected t))
1956 (replace-match (org-add-props
1957 (format org-commentsp (match-string 2))
1958 nil 'org-protected t)
1959 t t))
1960 (goto-char (1+ pos))
1961 (replace-match "")
1962 (goto-char (max (point-min) (1- pos))))))))
1964 (defun org-export-handle-table-metalines ()
1965 "Remove table specific metalines #+TBLNAME: and #+TBLFM:."
1966 (let ((re "^[ \t]*#\\+TBL\\(NAME\\|FM\\):\\(.*\n?\\)")
1967 pos)
1968 (goto-char (point-min))
1969 (while (or (looking-at re)
1970 (re-search-forward re nil t))
1971 (setq pos (match-beginning 0))
1972 (if (get-text-property (match-beginning 1) 'org-protected)
1973 (goto-char (1+ pos))
1974 (goto-char (1+ pos))
1975 (replace-match "")
1976 (goto-char (max (point-min) (1- pos)))))))
1978 (defun org-export-mark-radio-links ()
1979 "Find all matches for radio targets and turn them into internal links."
1980 (let ((re-radio (and org-target-link-regexp
1981 (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)"))))
1982 (goto-char (point-min))
1983 (when re-radio
1984 (while (re-search-forward re-radio nil t)
1985 (unless
1986 (save-match-data
1987 (or (org-in-regexp org-bracket-link-regexp)
1988 (org-in-regexp org-plain-link-re)
1989 (org-in-regexp "<<[^<>]+>>")))
1990 (org-if-unprotected
1991 (replace-match "\\1[[\\2]]")))))))
1993 (defun org-store-forced-table-alignment ()
1994 "Find table lines which force alignment, store the results in properties."
1995 (let (line cnt aligns)
1996 (goto-char (point-min))
1997 (while (re-search-forward "|[ \t]*<[lrc][0-9]*>[ \t]*|" nil t)
1998 ;; OK, this looks like a table line with an alignment cookie
1999 (org-if-unprotected
2000 (setq line (buffer-substring (point-at-bol) (point-at-eol)))
2001 (when (and (org-at-table-p)
2002 (org-table-cookie-line-p line))
2003 (setq cnt 0 aligns nil)
2004 (mapc
2005 (lambda (x)
2006 (setq cnt (1+ cnt))
2007 (if (string-match "\\`<\\([lrc]\\)" x)
2008 (push (cons cnt (downcase (match-string 1 x))) aligns)))
2009 (org-split-string line "[ \t]*|[ \t]*"))
2010 (add-text-properties (org-table-begin) (org-table-end)
2011 (list 'org-forced-aligns aligns))))
2012 (goto-char (point-at-eol)))))
2014 (defun org-export-remove-special-table-lines ()
2015 "Remove tables lines that are used for internal purposes.
2016 Also, store forced alignment information found in such lines."
2017 (goto-char (point-min))
2018 (while (re-search-forward "^[ \t]*|" nil t)
2019 (org-if-unprotected-at (1- (point))
2020 (beginning-of-line 1)
2021 (if (or (looking-at "[ \t]*| *[!_^] *|")
2022 (not
2023 (memq
2025 (mapcar
2026 (lambda (f)
2027 (or (and org-export-table-remove-empty-lines (= (length f) 0))
2028 (string-match
2029 "\\`<\\([0-9]\\|[lrc]\\|[lrc][0-9]+\\)>\\'" f)))
2030 (org-split-string ;; FIXME, can't we do without splitting???
2031 (buffer-substring (point-at-bol) (point-at-eol))
2032 "[ \t]*|[ \t]*")))))
2033 (delete-region (max (point-min) (1- (point-at-bol)))
2034 (point-at-eol))
2035 (end-of-line 1)))))
2037 (defun org-export-protect-sub-super (s)
2038 (save-match-data
2039 (while (string-match "\\([^\\\\]\\)\\([_^]\\)" s)
2040 (setq s (replace-match "\\1\\\\\\2" nil nil s)))
2043 (defun org-export-normalize-links ()
2044 "Convert all links to bracket links, and expand link abbreviations."
2045 (let ((re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
2046 (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
2047 nodesc)
2048 (goto-char (point-min))
2049 (while (re-search-forward re-plain-link nil t)
2050 (unless (org-string-match-p
2051 "\\[\\[\\S-+:\\S-*?\\<"
2052 (buffer-substring (point-at-bol) (match-beginning 0)))
2053 (goto-char (1- (match-end 0)))
2054 (org-if-unprotected-at (1+ (match-beginning 0))
2055 (let* ((s (concat (match-string 1)
2056 "[[" (match-string 2) ":" (match-string 3)
2057 "][" (match-string 2) ":" (org-export-protect-sub-super
2058 (match-string 3))
2059 "]]")))
2060 ;; added 'org-link face to links
2061 (put-text-property 0 (length s) 'face 'org-link s)
2062 (replace-match s t t)))))
2063 (goto-char (point-min))
2064 (while (re-search-forward re-angle-link nil t)
2065 (goto-char (1- (match-end 0)))
2066 (org-if-unprotected
2067 (let* ((s (concat (match-string 1)
2068 "[[" (match-string 2) ":" (match-string 3)
2069 "][" (match-string 2) ":" (org-export-protect-sub-super
2070 (match-string 3))
2071 "]]")))
2072 (put-text-property 0 (length s) 'face 'org-link s)
2073 (replace-match s t t))))
2074 (goto-char (point-min))
2075 (while (re-search-forward org-bracket-link-regexp nil t)
2076 (goto-char (1- (match-end 0)))
2077 (setq nodesc (not (match-end 3)))
2078 (org-if-unprotected
2079 (let* ((xx (save-match-data
2080 (org-translate-link
2081 (org-link-expand-abbrev (match-string 1)))))
2082 (s (concat
2083 "[[" (org-add-props (copy-sequence xx)
2084 nil 'org-protected t 'org-no-description nodesc)
2086 (if (match-end 3)
2087 (match-string 2)
2088 (concat "[" (copy-sequence xx)
2089 "]"))
2090 "]")))
2091 (put-text-property 0 (length s) 'face 'org-link s)
2092 (replace-match s t t))))))
2094 (defun org-export-concatenate-multiline-links ()
2095 "Find multi-line links and put it all into a single line.
2096 This is to make sure that the line-processing export backends
2097 can work correctly."
2098 (goto-char (point-min))
2099 (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
2100 (org-if-unprotected-at (match-beginning 1)
2101 (replace-match "\\1 \\3")
2102 (goto-char (match-beginning 0)))))
2104 (defun org-export-concatenate-multiline-emphasis ()
2105 "Find multi-line emphasis and put it all into a single line.
2106 This is to make sure that the line-processing export backends
2107 can work correctly."
2108 (goto-char (point-min))
2109 (while (re-search-forward org-emph-re nil t)
2110 (if (and (not (= (char-after (match-beginning 3))
2111 (char-after (match-beginning 4))))
2112 (save-excursion (goto-char (match-beginning 0))
2113 (save-match-data
2114 (and (not (org-at-table-p))
2115 (not (org-at-heading-p))))))
2116 (org-if-unprotected
2117 (subst-char-in-region (match-beginning 0) (match-end 0)
2118 ?\n ?\ t)
2119 (goto-char (1- (match-end 0))))
2120 (goto-char (1+ (match-beginning 0))))))
2122 (defun org-export-grab-title-from-buffer ()
2123 "Get a title for the current document, from looking at the buffer."
2124 (let ((inhibit-read-only t))
2125 (save-excursion
2126 (goto-char (point-min))
2127 (let ((end (if (looking-at org-outline-regexp)
2128 (point)
2129 (save-excursion (outline-next-heading) (point)))))
2130 (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
2131 ;; Mark the line so that it will not be exported as normal text.
2132 (org-unmodified
2133 (add-text-properties (match-beginning 0) (match-end 0)
2134 (list :org-license-to-kill t)))
2135 ;; Return the title string
2136 (org-trim (match-string 0)))))))
2138 (defun org-export-get-title-from-subtree ()
2139 "Return subtree title and exclude it from export."
2140 (let ((rbeg (region-beginning)) (rend (region-end))
2141 (inhibit-read-only t) title)
2142 (save-excursion
2143 (goto-char rbeg)
2144 (when (and (org-at-heading-p)
2145 (>= (org-end-of-subtree t t) rend))
2146 ;; This is a subtree, we take the title from the first heading
2147 (goto-char rbeg)
2148 (looking-at org-todo-line-regexp)
2149 (setq title (match-string 3))
2150 (org-unmodified
2151 (add-text-properties (point) (1+ (point-at-eol))
2152 (list :org-license-to-kill t)))
2153 (setq title (or (org-entry-get nil "EXPORT_TITLE") title))))
2154 title))
2156 (defun org-solidify-link-text (s &optional alist)
2157 "Take link text and make a safe target out of it."
2158 (save-match-data
2159 (let* ((rtn
2160 (mapconcat
2161 'identity
2162 (org-split-string s "[^a-zA-Z0-9_\\.-]+") "-"))
2163 (a (assoc rtn alist)))
2164 (or (cdr a) rtn))))
2166 (defun org-get-min-level (lines &optional offset)
2167 "Get the minimum level in LINES."
2168 (let ((re "^\\(\\*+\\) ") l)
2169 (catch 'exit
2170 (while (setq l (pop lines))
2171 (if (string-match re l)
2172 (throw 'exit (org-tr-level (- (length (match-string 1 l))
2173 (or offset 0))))))
2174 1)))
2176 ;; Variable holding the vector with section numbers
2177 (defvar org-section-numbers (make-vector org-level-max 0))
2179 (defun org-init-section-numbers ()
2180 "Initialize the vector for the section numbers."
2181 (let* ((level -1)
2182 (numbers (nreverse (org-split-string "" "\\.")))
2183 (depth (1- (length org-section-numbers)))
2184 (i depth) number-string)
2185 (while (>= i 0)
2186 (if (> i level)
2187 (aset org-section-numbers i 0)
2188 (setq number-string (or (car numbers) "0"))
2189 (if (string-match "\\`[A-Z]\\'" number-string)
2190 (aset org-section-numbers i
2191 (- (string-to-char number-string) ?A -1))
2192 (aset org-section-numbers i (string-to-number number-string)))
2193 (pop numbers))
2194 (setq i (1- i)))))
2196 (defun org-section-number (&optional level)
2197 "Return a string with the current section number.
2198 When LEVEL is non-nil, increase section numbers on that level."
2199 (let* ((depth (1- (length org-section-numbers)))
2200 (string "")
2201 (fmts (car org-export-section-number-format))
2202 (term (cdr org-export-section-number-format))
2203 (sep "")
2204 ctype fmt idx n)
2205 (when level
2206 (when (> level -1)
2207 (aset org-section-numbers
2208 level (1+ (aref org-section-numbers level))))
2209 (setq idx (1+ level))
2210 (while (<= idx depth)
2211 (if (not (= idx 1))
2212 (aset org-section-numbers idx 0))
2213 (setq idx (1+ idx))))
2214 (setq idx 0)
2215 (while (<= idx depth)
2216 (when (> (aref org-section-numbers idx) 0)
2217 (setq fmt (or (pop fmts) fmt)
2218 ctype (car fmt)
2219 n (aref org-section-numbers idx)
2220 string (if (> n 0)
2221 (concat string sep (org-number-to-counter n ctype))
2222 (concat string ".0"))
2223 sep (nth 1 fmt)))
2224 (setq idx (1+ idx)))
2225 (save-match-data
2226 (if (string-match "\\`\\([@0]\\.\\)+" string)
2227 (setq string (replace-match "" t nil string)))
2228 (if (string-match "\\(\\.0\\)+\\'" string)
2229 (setq string (replace-match "" t nil string))))
2230 (concat string term)))
2232 (defun org-number-to-counter (n type)
2233 "Concert number N to a string counter, according to TYPE.
2234 TYPE must be a string, any of:
2235 1 number
2236 A A,B,....
2237 a a,b,....
2238 I upper case roman numeral
2239 i lower case roman numeral"
2240 (cond
2241 ((equal type "1") (number-to-string n))
2242 ((equal type "A") (char-to-string (+ ?A n -1)))
2243 ((equal type "a") (char-to-string (+ ?a n -1)))
2244 ((equal type "I") (org-number-to-roman n))
2245 ((equal type "i") (downcase (org-number-to-roman n)))
2246 (t (error "Invalid counter type `%s'" type))))
2248 (defun org-number-to-roman (n)
2249 "Convert integer N into a roman numeral."
2250 (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
2251 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
2252 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
2253 ( 1 . "I")))
2254 (res ""))
2255 (if (<= n 0)
2256 (number-to-string n)
2257 (while roman
2258 (if (>= n (caar roman))
2259 (setq n (- n (caar roman))
2260 res (concat res (cdar roman)))
2261 (pop roman)))
2262 res)))
2264 ;;; Macros
2266 (defun org-export-preprocess-apply-macros ()
2267 "Replace macro references."
2268 (goto-char (point-min))
2269 (let (sy val key args args2 ind-str s n)
2270 (while (re-search-forward
2271 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
2272 nil t)
2273 (unless (save-match-data (save-excursion
2274 (goto-char (point-at-bol))
2275 (looking-at "[ \t]*#\\+macro")))
2276 ;; Get macro name (KEY), arguments (ARGS), and indentation of
2277 ;; current line (IND-STR) as strings.
2278 (setq key (downcase (match-string 1))
2279 args (match-string 3)
2280 ind-str (save-match-data (save-excursion
2281 (beginning-of-line)
2282 (looking-at "^\\([ \t]*\\).*")
2283 (match-string 1))))
2284 ;; When macro is defined, retrieve replacement text in VAL,
2285 ;; and proceed with expansion.
2286 (when (setq val (or (plist-get org-export-opt-plist
2287 (intern (concat ":macro-" key)))
2288 (plist-get org-export-opt-plist
2289 (intern (concat ":" key)))))
2290 (save-match-data
2291 ;; If arguments are provided, first retreive them properly
2292 ;; (in ARGS, as a list), then replace them in VAL.
2293 (when args
2294 (setq args (org-split-string args ",") args2 nil)
2295 (while args
2296 (while (string-match "\\\\\\'" (car args))
2297 ;; Repair bad splits.
2298 (setcar (cdr args) (concat (substring (car args) 0 -1)
2299 "," (nth 1 args)))
2300 (pop args))
2301 (push (pop args) args2))
2302 (setq args (mapcar 'org-trim (nreverse args2)))
2303 (setq s 0)
2304 (while (string-match "\\$\\([0-9]+\\)" val s)
2305 (setq s (1+ (match-beginning 0))
2306 n (string-to-number (match-string 1 val)))
2307 (and (>= (length args) n)
2308 (setq val (replace-match (nth (1- n) args) t t val)))))
2309 ;; VAL starts with "(eval": it is a sexp, `eval' it.
2310 (when (string-match "\\`(eval\\>" val)
2311 (setq val (eval (read val))))
2312 ;; Ensure VAL is a string (or nil) and that each new line
2313 ;; is indented as the first one.
2314 (setq val (and val
2315 (mapconcat 'identity
2316 (org-split-string
2317 (if (stringp val) val (format "%s" val))
2318 "\n")
2319 (concat "\n" ind-str)))))
2320 ;; Eventually do the replacement, if VAL isn't nil. Move
2321 ;; point at beginning of macro for recursive expansions.
2322 (when val
2323 (replace-match val t t)
2324 (goto-char (match-beginning 0))))))))
2326 (defun org-export-apply-macros-in-string (s)
2327 "Apply the macros in string S."
2328 (when s
2329 (with-temp-buffer
2330 (insert s)
2331 (org-export-preprocess-apply-macros)
2332 (buffer-string))))
2334 ;;; Include files
2336 (defun org-export-handle-include-files ()
2337 "Include the contents of include files, with proper formatting."
2338 (let ((case-fold-search t)
2339 params file markup lang start end prefix prefix1 switches all minlevel lines)
2340 (goto-char (point-min))
2341 (while (re-search-forward "^#\\+INCLUDE:?[ \t]+\\(.*\\)" nil t)
2342 (setq params (read (concat "(" (match-string 1) ")"))
2343 prefix (org-get-and-remove-property 'params :prefix)
2344 prefix1 (org-get-and-remove-property 'params :prefix1)
2345 minlevel (org-get-and-remove-property 'params :minlevel)
2346 lines (org-get-and-remove-property 'params :lines)
2347 file (org-symname-or-string (pop params))
2348 markup (org-symname-or-string (pop params))
2349 lang (and (member markup '("src" "SRC"))
2350 (org-symname-or-string (pop params)))
2351 switches (mapconcat #'(lambda (x) (format "%s" x)) params " ")
2352 start nil end nil)
2353 (delete-region (match-beginning 0) (match-end 0))
2354 (if (or (not file)
2355 (not (file-exists-p file))
2356 (not (file-readable-p file)))
2357 (insert (format "CANNOT INCLUDE FILE %s" file))
2358 (setq all (cons file all))
2359 (when markup
2360 (if (equal (downcase markup) "src")
2361 (setq start (format "#+begin_src %s %s\n"
2362 (or lang "fundamental")
2363 (or switches ""))
2364 end "#+end_src")
2365 (setq start (format "#+begin_%s %s\n" markup switches)
2366 end (format "#+end_%s" markup))))
2367 (insert (or start ""))
2368 (insert (org-get-file-contents (expand-file-name file)
2369 prefix prefix1 markup minlevel lines))
2370 (or (bolp) (newline))
2371 (insert (or end ""))))
2372 all))
2374 (defun org-export-handle-include-files-recurse ()
2375 "Recursively include files aborting on circular inclusion."
2376 (let ((now (list org-current-export-file)) all)
2377 (while now
2378 (setq all (append now all))
2379 (setq now (org-export-handle-include-files))
2380 (let ((intersection
2381 (delq nil
2382 (mapcar (lambda (el) (when (member el all) el)) now))))
2383 (when intersection
2384 (error "Recursive #+INCLUDE: %S" intersection))))))
2386 (defun org-get-file-contents (file &optional prefix prefix1 markup minlevel lines)
2387 "Get the contents of FILE and return them as a string.
2388 If PREFIX is a string, prepend it to each line. If PREFIX1
2389 is a string, prepend it to the first line instead of PREFIX.
2390 If MARKUP, don't protect org-like lines, the exporter will
2391 take care of the block they are in. If LINES is a string
2392 specifying a range of lines, include only those lines ."
2393 (if (stringp markup) (setq markup (downcase markup)))
2394 (with-temp-buffer
2395 (insert-file-contents file)
2396 (when lines
2397 (let* ((lines (split-string lines "-"))
2398 (lbeg (string-to-number (car lines)))
2399 (lend (string-to-number (cadr lines)))
2400 (beg (if (zerop lbeg) (point-min)
2401 (goto-char (point-min))
2402 (forward-line (1- lbeg))
2403 (point)))
2404 (end (if (zerop lend) (point-max)
2405 (goto-char (point-min))
2406 (forward-line (1- lend))
2407 (point))))
2408 (narrow-to-region beg end)))
2409 (when (or prefix prefix1)
2410 (goto-char (point-min))
2411 (while (not (eobp))
2412 (insert (or prefix1 prefix))
2413 (setq prefix1 "")
2414 (beginning-of-line 2)))
2415 (buffer-string)
2416 (when (member markup '("src" "example"))
2417 (goto-char (point-min))
2418 (while (re-search-forward "^\\([*#]\\|[ \t]*#\\+\\)" nil t)
2419 (goto-char (match-beginning 0))
2420 (insert ",")
2421 (end-of-line 1)))
2422 (when minlevel
2423 (dotimes (lvl minlevel)
2424 (org-map-region 'org-demote (point-min) (point-max))))
2425 (buffer-string)))
2427 (defun org-get-and-remove-property (listvar prop)
2428 "Check if the value of LISTVAR contains PROP as a property.
2429 If yes, return the value of that property (i.e. the element following
2430 in the list) and remove property and value from the list in LISTVAR."
2431 (let ((list (symbol-value listvar)) m v)
2432 (when (setq m (member prop list))
2433 (setq v (nth 1 m))
2434 (if (equal (car list) prop)
2435 (set listvar (cddr list))
2436 (setcdr (nthcdr (- (length list) (length m) 1) list)
2437 (cddr m))
2438 (set listvar list)))
2441 (defun org-symname-or-string (s)
2442 (if (symbolp s)
2443 (if s (symbol-name s) s)
2446 ;;; Fontification and line numbers for code examples
2448 (defvar org-export-last-code-line-counter-value 0)
2450 (defun org-export-replace-src-segments-and-examples ()
2451 "Replace source code segments with special code for export."
2452 (setq org-export-last-code-line-counter-value 0)
2453 (let ((case-fold-search t)
2454 lang code trans opts indent caption)
2455 (goto-char (point-min))
2456 (while (re-search-forward
2457 "\\(^\\([ \t]*\\)#\\+BEGIN_SRC:?\\([ \t]+\\([^ \t\n]+\\)\\)?\\(.*\\)\n\\([^\000]+?\n\\)[ \t]*#\\+END_SRC.*\n?\\)\\|\\(^\\([ \t]*\\)#\\+BEGIN_EXAMPLE:?\\(?:[ \t]+\\(.*\\)\\)?\n\\([^\000]+?\n\\)[ \t]*#\\+END_EXAMPLE.*\n?\\)"
2458 nil t)
2459 (if (match-end 1)
2460 (if (not (match-string 4))
2461 (error "Source block missing language specification: %s"
2462 (let* ((body (match-string 6))
2463 (nothing (message "body:%s" body))
2464 (preview (or (and (string-match
2465 "^[ \t]*\\([^\n\r]*\\)" body)
2466 (match-string 1 body)) body)))
2467 (if (> (length preview) 35)
2468 (concat (substring preview 0 32) "...")
2469 preview)))
2470 ;; src segments
2471 (setq lang (match-string 4)
2472 opts (match-string 5)
2473 code (match-string 6)
2474 indent (length (match-string 2))
2475 caption (get-text-property 0 'org-caption (match-string 0))))
2476 (setq lang nil
2477 opts (match-string 9)
2478 code (match-string 10)
2479 indent (length (match-string 8))
2480 caption (get-text-property 0 'org-caption (match-string 0))))
2482 (setq trans (org-export-format-source-code-or-example
2483 lang code opts indent caption))
2484 (replace-match trans t t))))
2486 (defvar org-export-latex-verbatim-wrap) ;; defined in org-latex.el
2487 (defvar org-export-latex-listings) ;; defined in org-latex.el
2488 (defvar org-export-latex-listings-langs) ;; defined in org-latex.el
2489 (defvar org-export-latex-listings-w-names) ;; defined in org-latex.el
2490 (defvar org-export-latex-minted-langs) ;; defined in org-latex.el
2491 (defvar org-export-latex-custom-lang-environments) ;; defined in org-latex.el
2492 (defvar org-export-latex-listings-options) ;; defined in org-latex.el
2493 (defvar org-export-latex-minted-options) ;; defined in org-latex.el
2495 (defun org-remove-formatting-on-newlines-in-region (beg end)
2496 "Remove formatting on newline characters"
2497 (interactive "r")
2498 (save-excursion
2499 (goto-char beg)
2500 (while (progn (end-of-line) (< (point) end))
2501 (put-text-property (point) (1+ (point)) 'face nil)
2502 (forward-char 1))))
2504 (defun org-export-format-source-code-or-example
2505 (lang code &optional opts indent caption)
2506 "Format CODE from language LANG and return it formatted for export.
2507 The CODE is marked up in `org-export-current-backend' format.
2509 Check if a function by name
2510 \"org-<backend>-format-source-code-or-example\" is bound. If yes,
2511 use it as the custom formatter. Otherwise, use the default
2512 formatter. Default formatters are provided for docbook, html,
2513 latex and ascii backends. For example, use
2514 `org-html-format-source-code-or-example' to provide a custom
2515 formatter for export to \"html\".
2517 If LANG is nil, do not add any fontification.
2518 OPTS contains formatting options, like `-n' for triggering numbering lines,
2519 and `+n' for continuing previous numbering.
2520 Code formatting according to language currently only works for HTML.
2521 Numbering lines works for all three major backends (html, latex, and ascii).
2522 INDENT was the original indentation of the block."
2523 (save-match-data
2524 (let* ((backend-name (symbol-name org-export-current-backend))
2525 (backend-formatter
2526 (intern (format "org-%s-format-source-code-or-example"
2527 backend-name)))
2528 (backend-feature (intern (concat "org-" backend-name)))
2529 (backend-formatter
2530 (and (require (intern (concat "org-" backend-name)) nil)
2531 (fboundp backend-formatter) backend-formatter))
2532 num cont rtn rpllbl keepp textareap preserve-indentp cols rows fmt)
2533 (setq opts (or opts "")
2534 num (string-match "[-+]n\\>" opts)
2535 cont (string-match "\\+n\\>" opts)
2536 rpllbl (string-match "-r\\>" opts)
2537 keepp (string-match "-k\\>" opts)
2538 textareap (string-match "-t\\>" opts)
2539 preserve-indentp (or org-src-preserve-indentation
2540 (string-match "-i\\>" opts))
2541 cols (if (string-match "-w[ \t]+\\([0-9]+\\)" opts)
2542 (string-to-number (match-string 1 opts))
2544 rows (if (string-match "-h[ \t]+\\([0-9]+\\)" opts)
2545 (string-to-number (match-string 1 opts))
2546 (org-count-lines code))
2547 fmt (if (string-match "-l[ \t]+\"\\([^\"\n]+\\)\"" opts)
2548 (match-string 1 opts)))
2549 (when (and textareap (eq org-export-current-backend 'html))
2550 ;; we cannot use numbering or highlighting.
2551 (setq num nil cont nil lang nil))
2552 (if keepp (setq rpllbl 'keep))
2553 (setq rtn (if preserve-indentp code (org-remove-indentation code)))
2554 (when (string-match "^," rtn)
2555 (setq rtn (with-temp-buffer
2556 (insert rtn)
2557 ;; Free up the protected lines
2558 (goto-char (point-min))
2559 (while (re-search-forward "^," nil t)
2560 (if (or (equal lang "org")
2561 (save-match-data
2562 (looking-at "\\([*#]\\|[ \t]*#\\+\\)")))
2563 (replace-match ""))
2564 (end-of-line 1))
2565 (buffer-string))))
2566 ;; Now backend-specific coding
2567 (setq rtn
2568 (cond
2569 (backend-formatter
2570 (funcall backend-formatter rtn lang caption textareap cols rows num
2571 cont rpllbl fmt))
2572 ((eq org-export-current-backend 'docbook)
2573 (setq rtn (org-export-number-lines rtn 0 0 num cont rpllbl fmt))
2574 (concat "<programlisting><![CDATA["
2576 "]]></programlisting>\n"))
2577 ((eq org-export-current-backend 'html)
2578 ;; We are exporting to HTML
2579 (when lang
2580 (if (featurep 'xemacs)
2581 (require 'htmlize)
2582 (require 'htmlize nil t))
2583 (when (not (fboundp 'htmlize-region-for-paste))
2584 ;; we do not have htmlize.el, or an old version of it
2585 (setq lang nil)
2586 (message
2587 "htmlize.el 1.34 or later is needed for source code formatting")))
2589 (if lang
2590 (let* ((lang-m (when lang
2591 (or (cdr (assoc lang org-src-lang-modes))
2592 lang)))
2593 (mode (and lang-m (intern
2594 (concat
2595 (if (symbolp lang-m)
2596 (symbol-name lang-m)
2597 lang-m)
2598 "-mode"))))
2599 (org-inhibit-startup t)
2600 (org-startup-folded nil))
2601 (setq rtn
2602 (with-temp-buffer
2603 (insert rtn)
2604 (if (functionp mode)
2605 (funcall mode)
2606 (fundamental-mode))
2607 (font-lock-fontify-buffer)
2608 ;; markup each line separately
2609 (org-remove-formatting-on-newlines-in-region (point-min) (point-max))
2610 (org-src-mode)
2611 (set-buffer-modified-p nil)
2612 (org-export-htmlize-region-for-paste
2613 (point-min) (point-max))))
2614 (if (string-match "<pre\\([^>]*\\)>\n*" rtn)
2615 (setq rtn
2616 (concat
2617 (if caption
2618 (concat
2619 "<div class=\"org-src-container\">"
2620 (format
2621 "<label class=\"org-src-name\">%s</label>"
2622 caption))
2624 (replace-match
2625 (format "<pre class=\"src src-%s\">\n" lang)
2626 t t rtn)
2627 (if caption "</div>" "")))))
2628 (if textareap
2629 (setq rtn (concat
2630 (format "<p>\n<textarea cols=\"%d\" rows=\"%d\">"
2631 cols rows)
2632 rtn "</textarea>\n</p>\n"))
2633 (with-temp-buffer
2634 (insert rtn)
2635 (goto-char (point-min))
2636 (while (re-search-forward "[<>&]" nil t)
2637 (replace-match (cdr (assq (char-before)
2638 '((?&."&amp;")(?<."&lt;")(?>."&gt;"))))
2639 t t))
2640 (setq rtn (buffer-string)))
2641 (setq rtn (concat "<pre class=\"example\">\n" rtn "</pre>\n"))))
2642 (unless textareap
2643 (setq rtn (org-export-number-lines rtn 1 1 num cont rpllbl fmt)))
2644 (if (string-match "\\(\\`<[^>]*>\\)\n" rtn)
2645 (setq rtn (replace-match "\\1" t nil rtn)))
2646 rtn)
2647 ((eq org-export-current-backend 'latex)
2648 (setq rtn (org-export-number-lines rtn 0 0 num cont rpllbl fmt))
2649 (cond
2650 ((and lang org-export-latex-listings)
2651 (flet ((make-option-string
2652 (pair)
2653 (concat (first pair)
2654 (if (> (length (second pair)) 0)
2655 (concat "=" (second pair))))))
2656 (let* ((lang-sym (intern lang))
2657 (minted-p (eq org-export-latex-listings 'minted))
2658 (listings-p (not minted-p))
2659 (backend-lang
2660 (or (cadr
2661 (assq
2662 lang-sym
2663 (cond
2664 (minted-p org-export-latex-minted-langs)
2665 (listings-p org-export-latex-listings-langs))))
2666 lang))
2667 (custom-environment
2668 (cadr
2669 (assq
2670 lang-sym
2671 org-export-latex-custom-lang-environments))))
2672 (concat
2673 (when (and listings-p (not custom-environment))
2674 (format
2675 "\\lstset{%s}\n"
2676 (mapconcat
2677 #'make-option-string
2678 (append org-export-latex-listings-options
2679 `(("language" ,backend-lang))) ",")))
2680 (when (and caption org-export-latex-listings-w-names)
2681 (format
2682 "\n%s $\\equiv$ \n"
2683 (replace-regexp-in-string "_" "\\\\_" caption)))
2684 (cond
2685 (custom-environment
2686 (format "\\begin{%s}\n%s\\end{%s}\n"
2687 custom-environment rtn custom-environment))
2688 (listings-p
2689 (format "\\begin{%s}\n%s\\end{%s}\n"
2690 "lstlisting" rtn "lstlisting"))
2691 (minted-p
2692 (format
2693 "\\begin{minted}[%s]{%s}\n%s\\end{minted}\n"
2694 (mapconcat #'make-option-string
2695 org-export-latex-minted-options ",")
2696 backend-lang rtn)))))))
2697 (t (concat (car org-export-latex-verbatim-wrap)
2698 rtn (cdr org-export-latex-verbatim-wrap)))))
2699 ((eq org-export-current-backend 'ascii)
2700 ;; This is not HTML or LaTeX, so just make it an example.
2701 (setq rtn (org-export-number-lines rtn 0 0 num cont rpllbl fmt))
2702 (concat caption "\n"
2703 (concat
2704 (mapconcat
2705 (lambda (l) (concat " " l))
2706 (org-split-string rtn "\n")
2707 "\n")
2708 "\n")
2711 (error "Don't know how to markup source or example block in %s"
2712 (upcase backend-name)))))
2713 (setq rtn
2714 (concat
2715 "\n#+BEGIN_" backend-name "\n"
2716 (org-add-props rtn
2717 '(org-protected t org-example t org-native-text t))
2718 "\n#+END_" backend-name "\n\n"))
2719 (org-add-props rtn nil 'original-indentation indent))))
2721 (defun org-export-number-lines (text &optional skip1 skip2 number cont
2722 replace-labels label-format)
2723 (setq skip1 (or skip1 0) skip2 (or skip2 0))
2724 (if (not cont) (setq org-export-last-code-line-counter-value 0))
2725 (with-temp-buffer
2726 (insert text)
2727 (goto-char (point-max))
2728 (skip-chars-backward " \t\n\r")
2729 (delete-region (point) (point-max))
2730 (beginning-of-line (- 1 skip2))
2731 (let* ((last (org-current-line))
2732 (n org-export-last-code-line-counter-value)
2733 (nmax (+ n (- last skip1)))
2734 (fmt (format "%%%dd: " (length (number-to-string nmax))))
2736 (cond
2737 ((eq org-export-current-backend 'html) (format "<span class=\"linenr\">%s</span>"
2738 fmt))
2739 ((eq org-export-current-backend 'ascii) fmt)
2740 ((eq org-export-current-backend 'latex) fmt)
2741 ((eq org-export-current-backend 'docbook) fmt)
2742 (t "")))
2743 (label-format (or label-format org-coderef-label-format))
2744 (label-pre (if (string-match "%s" label-format)
2745 (substring label-format 0 (match-beginning 0))
2746 label-format))
2747 (label-post (if (string-match "%s" label-format)
2748 (substring label-format (match-end 0))
2749 ""))
2750 (lbl-re
2751 (concat
2752 ".*?\\S-.*?\\([ \t]*\\("
2753 (regexp-quote label-pre)
2754 "\\([-a-zA-Z0-9_ ]+\\)"
2755 (regexp-quote label-post)
2756 "\\)\\)"))
2757 ref)
2759 (org-goto-line (1+ skip1))
2760 (while (and (re-search-forward "^" nil t) (not (eobp)) (< n nmax))
2761 (if number
2762 (insert (format fm (incf n)))
2763 (forward-char 1))
2764 (when (looking-at lbl-re)
2765 (setq ref (match-string 3))
2766 (cond ((numberp replace-labels)
2767 ;; remove labels; use numbers for references when lines
2768 ;; are numbered, use labels otherwise
2769 (delete-region (match-beginning 1) (match-end 1))
2770 (push (cons ref (if (> n 0) n ref)) org-export-code-refs))
2771 ((eq replace-labels 'keep)
2772 ;; don't remove labels; use numbers for references when
2773 ;; lines are numbered, use labels otherwise
2774 (goto-char (match-beginning 2))
2775 (delete-region (match-beginning 2) (match-end 2))
2776 (insert "(" ref ")")
2777 (push (cons ref (if (> n 0) n (concat "(" ref ")")))
2778 org-export-code-refs))
2780 ;; don't remove labels and don't use numbers for
2781 ;; references
2782 (goto-char (match-beginning 2))
2783 (delete-region (match-beginning 2) (match-end 2))
2784 (insert "(" ref ")")
2785 (push (cons ref (concat "(" ref ")")) org-export-code-refs)))
2786 (when (eq org-export-current-backend 'html)
2787 (save-excursion
2788 (beginning-of-line 1)
2789 (insert (format "<span id=\"coderef-%s\" class=\"coderef-off\">"
2790 ref))
2791 (end-of-line 1)
2792 (insert "</span>")))))
2793 (setq org-export-last-code-line-counter-value n)
2794 (goto-char (point-max))
2795 (newline)
2796 (buffer-string))))
2798 (defun org-search-todo-below (line lines level)
2799 "Search the subtree below LINE for any TODO entries."
2800 (let ((rest (cdr (memq line lines)))
2801 (re org-todo-line-regexp)
2802 line lv todo)
2803 (catch 'exit
2804 (while (setq line (pop rest))
2805 (if (string-match re line)
2806 (progn
2807 (setq lv (- (match-end 1) (match-beginning 1))
2808 todo (and (match-beginning 2)
2809 (not (member (match-string 2 line)
2810 org-done-keywords))))
2811 ; TODO, not DONE
2812 (if (<= lv level) (throw 'exit nil))
2813 (if todo (throw 'exit t))))))))
2815 ;;;###autoload
2816 (defun org-export-visible (type arg)
2817 "Create a copy of the visible part of the current buffer, and export it.
2818 The copy is created in a temporary buffer and removed after use.
2819 TYPE is the final key (as a string) that also selects the export command in
2820 the \\<org-mode-map>\\[org-export] export dispatcher.
2821 As a special case, if the you type SPC at the prompt, the temporary
2822 org-mode file will not be removed but presented to you so that you can
2823 continue to use it. The prefix arg ARG is passed through to the exporting
2824 command."
2825 (interactive
2826 (list (progn
2827 (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")
2828 (read-char-exclusive))
2829 current-prefix-arg))
2830 (if (not (member type '(?a ?n ?u ?\C-a ?b ?\C-b ?h ?D ?x ?\ ?l ?p ?d ?L ?H ?R)))
2831 (error "Invalid export key"))
2832 (let* ((binding (cdr (assoc type
2834 (?a . org-export-as-ascii)
2835 (?A . org-export-as-ascii-to-buffer)
2836 (?n . org-export-as-latin1)
2837 (?N . org-export-as-latin1-to-buffer)
2838 (?u . org-export-as-utf8)
2839 (?U . org-export-as-utf8-to-buffer)
2840 (?\C-a . org-export-as-ascii)
2841 (?b . org-export-as-html-and-open)
2842 (?\C-b . org-export-as-html-and-open)
2843 (?h . org-export-as-html)
2844 (?H . org-export-as-html-to-buffer)
2845 (?R . org-export-region-as-html)
2846 (?D . org-export-as-docbook)
2848 (?l . org-export-as-latex)
2849 (?p . org-export-as-pdf)
2850 (?d . org-export-as-pdf-and-open)
2851 (?L . org-export-as-latex-to-buffer)
2853 (?x . org-export-as-xoxo)))))
2854 (keepp (equal type ?\ ))
2855 (file buffer-file-name)
2856 (buffer (get-buffer-create "*Org Export Visible*"))
2857 s e)
2858 ;; Need to hack the drawers here.
2859 (save-excursion
2860 (goto-char (point-min))
2861 (while (re-search-forward org-drawer-regexp nil t)
2862 (goto-char (match-beginning 1))
2863 (or (outline-invisible-p) (org-flag-drawer nil))))
2864 (with-current-buffer buffer (erase-buffer))
2865 (save-excursion
2866 (setq s (goto-char (point-min)))
2867 (while (not (= (point) (point-max)))
2868 (goto-char (org-find-invisible))
2869 (append-to-buffer buffer s (point))
2870 (setq s (goto-char (org-find-visible))))
2871 (org-cycle-hide-drawers 'all)
2872 (goto-char (point-min))
2873 (unless keepp
2874 ;; Copy all comment lines to the end, to make sure #+ settings are
2875 ;; still available for the second export step. Kind of a hack, but
2876 ;; does do the trick.
2877 (if (looking-at "#[^\r\n]*")
2878 (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
2879 (when (re-search-forward "^\\*+[ \t]+" nil t)
2880 (while (re-search-backward "[\n\r]#[^\n\r]*" nil t)
2881 (append-to-buffer buffer (1+ (match-beginning 0))
2882 (min (point-max) (1+ (match-end 0)))))))
2883 (set-buffer buffer)
2884 (let ((buffer-file-name file)
2885 (org-inhibit-startup t))
2886 (org-mode)
2887 (show-all)
2888 (unless keepp (funcall binding arg))))
2889 (if (not keepp)
2890 (kill-buffer buffer)
2891 (switch-to-buffer-other-window buffer)
2892 (goto-char (point-min)))))
2894 (defun org-find-visible ()
2895 (let ((s (point)))
2896 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
2897 (get-char-property s 'invisible)))
2899 (defun org-find-invisible ()
2900 (let ((s (point)))
2901 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
2902 (not (get-char-property s 'invisible))))
2905 (defvar org-export-htmlized-org-css-url) ;; defined in org-html.el
2907 (defun org-export-string (string fmt &optional dir)
2908 "Export STRING to FMT using existing export facilities.
2909 During export STRING is saved to a temporary file whose location
2910 could vary. Optional argument DIR can be used to force the
2911 directory in which the temporary file is created during export
2912 which can be useful for resolving relative paths. Dir defaults
2913 to the value of `temporary-file-directory'."
2914 (let ((temporary-file-directory (or dir temporary-file-directory))
2915 (tmp-file (make-temp-file "org-")))
2916 (unwind-protect
2917 (with-temp-buffer
2918 (insert string)
2919 (write-file tmp-file)
2920 (org-load-modules-maybe)
2921 (unless org-local-vars
2922 (setq org-local-vars (org-get-local-variables)))
2923 (eval ;; convert to fmt -- mimicing `org-run-like-in-org-mode'
2924 (list 'let org-local-vars
2925 (list (intern (format "org-export-as-%s" fmt))
2926 nil nil nil ''string t))))
2927 (delete-file tmp-file))))
2929 ;;;###autoload
2930 (defun org-export-as-org (arg &optional hidden ext-plist
2931 to-buffer body-only pub-dir)
2932 "Make a copy with not-exporting stuff removed.
2933 The purpose of this function is to provide a way to export the source
2934 Org file of a webpage in Org format, but with sensitive and/or irrelevant
2935 stuff removed. This command will remove the following:
2937 - archived trees (if the variable `org-export-with-archived-trees' is nil)
2938 - comment blocks and trees starting with the COMMENT keyword
2939 - only trees that are consistent with `org-export-select-tags'
2940 and `org-export-exclude-tags'.
2942 The only arguments that will be used are EXT-PLIST and PUB-DIR,
2943 all the others will be ignored (but are present so that the general
2944 mechanism to call publishing functions will work).
2946 EXT-PLIST is a property list with external parameters overriding
2947 org-mode's default settings, but still inferior to file-local
2948 settings. When PUB-DIR is set, use this as the publishing
2949 directory."
2950 (interactive "P")
2951 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
2952 ext-plist
2953 (org-infile-export-plist)))
2954 (bfname (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
2955 (filename (concat (file-name-as-directory
2956 (or pub-dir
2957 (org-export-directory :org opt-plist)))
2958 (file-name-sans-extension
2959 (file-name-nondirectory bfname))
2960 ".org"))
2961 (filename (and filename
2962 (if (equal (file-truename filename)
2963 (file-truename bfname))
2964 (concat (file-name-sans-extension filename)
2965 "-source."
2966 (file-name-extension filename))
2967 filename)))
2968 (backup-inhibited t)
2969 (buffer (find-file-noselect filename))
2970 (region (buffer-string))
2971 str-ret)
2972 (save-excursion
2973 (org-pop-to-buffer-same-window buffer)
2974 (erase-buffer)
2975 (insert region)
2976 (let ((org-inhibit-startup t)) (org-mode))
2977 (org-install-letbind)
2979 ;; Get rid of archived trees
2980 (org-export-remove-archived-trees (plist-get opt-plist :archived-trees))
2982 ;; Remove comment environment and comment subtrees
2983 (org-export-remove-comment-blocks-and-subtrees)
2985 ;; Get rid of excluded trees
2986 (org-export-handle-export-tags (plist-get opt-plist :select-tags)
2987 (plist-get opt-plist :exclude-tags))
2989 (when (or (plist-get opt-plist :plain-source)
2990 (not (or (plist-get opt-plist :plain-source)
2991 (plist-get opt-plist :htmlized-source))))
2992 ;; Either nothing special is requested (default call)
2993 ;; or the plain source is explicitly requested
2994 ;; so: save it
2995 (save-buffer))
2996 (when (plist-get opt-plist :htmlized-source)
2997 ;; Make the htmlized version
2998 (require 'htmlize)
2999 (require 'org-html)
3000 (font-lock-fontify-buffer)
3001 (let* ((htmlize-output-type 'css)
3002 (newbuf (htmlize-buffer)))
3003 (with-current-buffer newbuf
3004 (when org-export-htmlized-org-css-url
3005 (goto-char (point-min))
3006 (and (re-search-forward
3007 "<style type=\"text/css\">[^\000]*?\n[ \t]*</style>.*"
3008 nil t)
3009 (replace-match
3010 (format
3011 "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\">"
3012 org-export-htmlized-org-css-url)
3013 t t)))
3014 (write-file (concat filename ".html")))
3015 (kill-buffer newbuf)))
3016 (set-buffer-modified-p nil)
3017 (if (equal to-buffer 'string)
3018 (progn (setq str-ret (buffer-string))
3019 (kill-buffer (current-buffer))
3020 str-ret)
3021 (kill-buffer (current-buffer))))))
3023 (defvar org-archive-location) ;; gets loaded with the org-archive require.
3024 (defun org-get-current-options ()
3025 "Return a string with current options as keyword options.
3026 Does include HTML export options as well as TODO and CATEGORY stuff."
3027 (require 'org-archive)
3028 (format
3029 "#+TITLE: %s
3030 #+AUTHOR: %s
3031 #+EMAIL: %s
3032 #+DATE: %s
3033 #+DESCRIPTION:
3034 #+KEYWORDS:
3035 #+LANGUAGE: %s
3036 #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s -:%s f:%s *:%s <:%s
3037 #+OPTIONS: TeX:%s LaTeX:%s skip:%s d:%s todo:%s pri:%s tags:%s
3039 #+EXPORT_SELECT_TAGS: %s
3040 #+EXPORT_EXCLUDE_TAGS: %s
3041 #+LINK_UP: %s
3042 #+LINK_HOME: %s
3043 #+XSLT:
3044 #+CATEGORY: %s
3045 #+SEQ_TODO: %s
3046 #+TYP_TODO: %s
3047 #+PRIORITIES: %c %c %c
3048 #+DRAWERS: %s
3049 #+STARTUP: %s %s %s %s %s
3050 #+TAGS: %s
3051 #+FILETAGS: %s
3052 #+ARCHIVE: %s
3053 #+LINK: %s
3055 (buffer-name) (user-full-name) user-mail-address
3056 (format-time-string (substring (car org-time-stamp-formats) 1 -1))
3057 org-export-default-language
3058 org-export-headline-levels
3059 org-export-with-section-numbers
3060 org-export-with-toc
3061 org-export-preserve-breaks
3062 org-export-html-expand
3063 org-export-with-fixed-width
3064 org-export-with-tables
3065 org-export-with-sub-superscripts
3066 org-export-with-special-strings
3067 org-export-with-footnotes
3068 org-export-with-emphasize
3069 org-export-with-timestamps
3070 org-export-with-TeX-macros
3071 org-export-with-LaTeX-fragments
3072 org-export-skip-text-before-1st-heading
3073 org-export-with-drawers
3074 org-export-with-todo-keywords
3075 org-export-with-priority
3076 org-export-with-tags
3077 (if (featurep 'org-jsinfo) (org-infojs-options-inbuffer-template) "")
3078 (mapconcat 'identity org-export-select-tags " ")
3079 (mapconcat 'identity org-export-exclude-tags " ")
3080 org-export-html-link-up
3081 org-export-html-link-home
3082 (or (ignore-errors
3083 (file-name-sans-extension
3084 (file-name-nondirectory (buffer-file-name (buffer-base-buffer)))))
3085 "NOFILENAME")
3086 "TODO FEEDBACK VERIFY DONE"
3087 "Me Jason Marie DONE"
3088 org-highest-priority org-lowest-priority org-default-priority
3089 (mapconcat 'identity org-drawers " ")
3090 (cdr (assoc org-startup-folded
3091 '((nil . "showall") (t . "overview") (content . "content"))))
3092 (if org-odd-levels-only "odd" "oddeven")
3093 (if org-hide-leading-stars "hidestars" "showstars")
3094 (if org-startup-align-all-tables "align" "noalign")
3095 (cond ((eq org-log-done t) "logdone")
3096 ((equal org-log-done 'note) "lognotedone")
3097 ((not org-log-done) "nologdone"))
3098 (or (mapconcat (lambda (x)
3099 (cond
3100 ((equal :startgroup (car x)) "{")
3101 ((equal :endgroup (car x)) "}")
3102 ((equal :newline (car x)) "")
3103 ((cdr x) (format "%s(%c)" (car x) (cdr x)))
3104 (t (car x))))
3105 (or org-tag-alist (org-get-buffer-tags)) " ") "")
3106 (mapconcat 'identity org-file-tags " ")
3107 org-archive-location
3108 "org file:~/org/%s.org"
3111 ;;;###autoload
3112 (defun org-insert-export-options-template ()
3113 "Insert into the buffer a template with information for exporting."
3114 (interactive)
3115 (if (not (bolp)) (newline))
3116 (let ((s (org-get-current-options)))
3117 (and (string-match "#\\+CATEGORY" s)
3118 (setq s (substring s 0 (match-beginning 0))))
3119 (insert s)))
3121 (defvar org-table-colgroup-info nil)
3123 (defun org-table-clean-before-export (lines &optional maybe-quoted)
3124 "Check if the table has a marking column.
3125 If yes remove the column and the special lines."
3126 (setq org-table-colgroup-info nil)
3127 (if (memq nil
3128 (mapcar
3129 (lambda (x) (or (string-match "^[ \t]*|-" x)
3130 (string-match
3131 (if maybe-quoted
3132 "^[ \t]*| *\\\\?\\([\#!$*_^ /]\\) *|"
3133 "^[ \t]*| *\\([\#!$*_^ /]\\) *|")
3134 x)))
3135 lines))
3136 ;; No special marking column
3137 (progn
3138 (setq org-table-clean-did-remove-column nil)
3139 (delq nil
3140 (mapcar
3141 (lambda (x)
3142 (cond
3143 ((org-table-colgroup-line-p x)
3144 ;; This line contains colgroup info, extract it
3145 ;; and then discard the line
3146 (setq org-table-colgroup-info
3147 (mapcar (lambda (x)
3148 (cond ((member x '("<" "&lt;")) :start)
3149 ((member x '(">" "&gt;")) :end)
3150 ((member x '("<>" "&lt;&gt;")) :startend)
3151 (t nil)))
3152 (org-split-string x "[ \t]*|[ \t]*")))
3153 nil)
3154 ((org-table-cookie-line-p x)
3155 ;; This line contains formatting cookies, discard it
3156 nil)
3157 (t x)))
3158 lines)))
3159 ;; there is a special marking column
3160 (setq org-table-clean-did-remove-column t)
3161 (delq nil
3162 (mapcar
3163 (lambda (x)
3164 (cond
3165 ((org-table-colgroup-line-p x)
3166 ;; This line contains colgroup info, extract it
3167 ;; and then discard the line
3168 (setq org-table-colgroup-info
3169 (mapcar (lambda (x)
3170 (cond ((member x '("<" "&lt;")) :start)
3171 ((member x '(">" "&gt;")) :end)
3172 ((member x '("<>" "&lt;&gt;")) :startend)
3173 (t nil)))
3174 (cdr (org-split-string x "[ \t]*|[ \t]*"))))
3175 nil)
3176 ((org-table-cookie-line-p x)
3177 ;; This line contains formatting cookies, discard it
3178 nil)
3179 ((string-match "^[ \t]*| *[!_^/] *|" x)
3180 ;; ignore this line
3181 nil)
3182 ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
3183 (string-match "^\\([ \t]*\\)|[^|]*|" x))
3184 ;; remove the first column
3185 (replace-match "\\1|" t nil x))))
3186 lines))))
3188 (defun org-export-cleanup-toc-line (s)
3189 "Remove tags and timestamps from lines going into the toc."
3190 (when (memq org-export-with-tags '(not-in-toc nil))
3191 (if (string-match (org-re " +:[[:alnum:]_@#%:]+: *$") s)
3192 (setq s (replace-match "" t t s))))
3193 (when org-export-remove-timestamps-from-toc
3194 (while (string-match org-maybe-keyword-time-regexp s)
3195 (setq s (replace-match "" t t s))))
3196 (while (string-match org-bracket-link-regexp s)
3197 (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
3198 t t s)))
3199 (while (string-match "\\[\\([0-9]\\|fn:[^]]*\\)\\]" s)
3200 (setq s (replace-match "" t t s)))
3204 (defun org-get-text-property-any (pos prop &optional object)
3205 (or (get-text-property pos prop object)
3206 (and (setq pos (next-single-property-change pos prop object))
3207 (get-text-property pos prop object))))
3209 (defun org-export-get-coderef-format (path desc)
3210 (save-match-data
3211 (if (and desc (string-match
3212 (regexp-quote (concat "(" path ")"))
3213 desc))
3214 (replace-match "%s" t t desc)
3215 (or desc "%s"))))
3217 (defun org-export-push-to-kill-ring (format)
3218 "Push buffer content to kill ring.
3219 The depends on the variable `org-export-copy-to-kill'."
3220 (when org-export-copy-to-kill-ring
3221 (org-kill-new (buffer-string))
3222 (when (fboundp 'x-set-selection)
3223 (ignore-errors (x-set-selection 'PRIMARY (buffer-string)))
3224 (ignore-errors (x-set-selection 'CLIPBOARD (buffer-string))))
3225 (message "%s export done, pushed to kill ring and clipboard" format)))
3227 (provide 'org-exp)
3229 ;; arch-tag: 65985fe9-095c-49c7-a7b6-cb4ee15c0a95
3231 ;;; org-exp.el ends here