Minor fix
[org-mode/org-tableheadings.git] / lisp / org-exp.el
blob07f0b0598208a9079d3244d521f7e9db948d1d4e
1 ;;; org-exp.el --- ASCII, HTML, XOXO and iCalendar export for Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
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: 6.34trans
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 (require 'org)
30 (require 'org-macs)
31 (require 'org-agenda)
32 (require 'org-exp-blocks)
33 (require 'org-src)
34 (eval-when-compile
35 (require 'cl))
37 (declare-function org-export-latex-preprocess "org-latex" (parameters))
38 (declare-function org-export-ascii-preprocess "org-ascii" (parameters))
39 (declare-function org-export-html-preprocess "org-html" (parameters))
40 (declare-function org-export-docbook-preprocess "org-docbook" (parameters))
41 (declare-function org-infojs-options-inbuffer-template "org-jsinfo" ())
42 (declare-function org-export-htmlize-region-for-paste "org-html" (beg end))
43 (declare-function htmlize-buffer "ext:htmlize" (&optional buffer))
44 (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
45 (autoload 'org-export-generic "org-export-generic" "Export using the generic exporter" t)
46 (defgroup org-export nil
47 "Options for exporting org-listings."
48 :tag "Org Export"
49 :group 'org)
51 (defgroup org-export-general nil
52 "General options for exporting Org-mode files."
53 :tag "Org Export General"
54 :group 'org-export)
56 (defcustom org-export-allow-BIND 'confirm
57 "Non-nil means allow #+BIND to define local variable values for export.
58 This is a potential security risk, which is why the user must confirm the
59 use of these lines."
60 :group 'org-export-general
61 :type '(choice
62 (const :tag "Never" nil)
63 (const :tag "Always" t)
64 (const :tag "Make the user confirm for each file" confirm)))
66 ;; FIXME
67 (defvar org-export-publishing-directory nil)
69 (defcustom org-export-show-temporary-export-buffer t
70 "Non-nil means show buffer after exporting to temp buffer.
71 When Org exports to a file, the buffer visiting that file is ever
72 shown, but remains buried. However, when exporting to a temporary
73 buffer, that buffer is popped up in a second window. When this variable
74 is nil, the buffer remains buried also in these cases."
75 :group 'org-export-general
76 :type 'boolean)
78 (defcustom org-export-copy-to-kill-ring t
79 "Non-nil means exported stuff will also be pushed onto the kill ring."
80 :group 'org-export-general
81 :type 'boolean)
83 (defcustom org-export-kill-product-buffer-when-displayed nil
84 "Non-nil means kill the product buffer if it is displayed immediately.
85 This applied to the commands `org-export-html-and-open' and
86 `org-export-as-pdf-and-open'."
87 :group 'org-export-general
88 :type 'boolean)
90 (defcustom org-export-run-in-background nil
91 "Non-nil means export and publishing commands will run in background.
92 This works by starting up a separate Emacs process visiting the same file
93 and doing the export from there.
94 Not all export commands are affected by this - only the ones which
95 actually write to a file, and that do not depend on the buffer state.
97 If this option is nil, you can still get background export by calling
98 `org-export' with a double prefix arg: `C-u C-u C-c C-e'.
100 If this option is t, the double prefix can be used to exceptionally
101 force an export command into the current process."
102 :group 'org-export-general
103 :type 'boolean)
105 (defcustom org-export-select-tags '("export")
106 "Tags that select a tree for export.
107 If any such tag is found in a buffer, all trees that do not carry one
108 of these tags will be deleted before export.
109 Inside trees that are selected like this, you can still deselect a
110 subtree by tagging it with one of the `org-export-exclude-tags'."
111 :group 'org-export-general
112 :type '(repeat (string :tag "Tag")))
114 (defcustom org-export-exclude-tags '("noexport")
115 "Tags that exclude a tree from export.
116 All trees carrying any of these tags will be excluded from export.
117 This is without condition, so even subtrees inside that carry one of the
118 `org-export-select-tags' will be removed."
119 :group 'org-export-general
120 :type '(repeat (string :tag "Tag")))
122 ;; FIXME: rename, this is a general variable
123 (defcustom org-export-html-expand t
124 "Non-nil means for HTML export, treat @<...> as HTML tag.
125 When nil, these tags will be exported as plain text and therefore
126 not be interpreted by a browser.
128 This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
129 :group 'org-export-html
130 :group 'org-export-general
131 :type 'boolean)
133 (defcustom org-export-with-special-strings t
134 "Non-nil means interpret \"\-\", \"--\" and \"---\" for export.
135 When this option is turned on, these strings will be exported as:
137 Org HTML LaTeX
138 -----+----------+--------
139 \\- &shy; \\-
140 -- &ndash; --
141 --- &mdash; ---
142 ... &hellip; \ldots
144 This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
145 :group 'org-export-translation
146 :type 'boolean)
148 (defcustom org-export-html-link-up ""
149 "Where should the \"UP\" link of exported HTML pages lead?"
150 :group 'org-export-html
151 :group 'org-export-general
152 :type '(string :tag "File or URL"))
154 (defcustom org-export-html-link-home ""
155 "Where should the \"HOME\" link of exported HTML pages lead?"
156 :group 'org-export-html
157 :group 'org-export-general
158 :type '(string :tag "File or URL"))
160 (defcustom org-export-language-setup
161 '(("en" "Author" "Date" "Table of Contents" "Footnotes")
162 ("ca" "Autor" "Data" "&Iacute;ndex" "Peus de p&agrave;gina")
163 ("cs" "Autor" "Datum" "Obsah" "Pozn\xe1mky pod carou")
164 ("da" "Ophavsmand" "Dato" "Indhold" "Fodnoter")
165 ("de" "Autor" "Datum" "Inhaltsverzeichnis" "Fu&szlig;noten")
166 ("eo" "A&#365;toro" "Dato" "Enhavo" "Piednotoj")
167 ("es" "Autor" "Fecha" "&Iacute;ndice" "Pies de p&aacute;gina")
168 ("fi" "Tekij&auml;" "P&auml;iv&auml;m&auml;&auml;r&auml;" "Sis&auml;llysluettelo" "Alaviitteet")
169 ("fr" "Auteur" "Date" "Table des mati&egrave;res" "Notes de bas de page")
170 ("hu" "Szerz&otilde;" "D&aacute;tum" "Tartalomjegyz&eacute;k" "L&aacute;bjegyzet")
171 ("is" "H&ouml;fundur" "Dagsetning" "Efnisyfirlit" "Aftanm&aacute;lsgreinar")
172 ("it" "Autore" "Data" "Indice" "Note a pi&egrave; di pagina")
173 ("nl" "Auteur" "Datum" "Inhoudsopgave" "Voetnoten")
174 ("no" "Forfatter" "Dato" "Innhold" "Fotnoter")
175 ("nb" "Forfatter" "Dato" "Innhold" "Fotnoter") ;; nb = Norsk (bokm.l)
176 ("nn" "Forfattar" "Dato" "Innhald" "Fotnotar") ;; nn = Norsk (nynorsk)
177 ("pl" "Autor" "Data" "Spis tre&sacute;ci" "Przypis")
178 ("sv" "F&ouml;rfattare" "Datum" "Inneh&aring;ll" "Fotnoter"))
179 "Terms used in export text, translated to different languages.
180 Use the variable `org-export-default-language' to set the language,
181 or use the +OPTION lines for a per-file setting."
182 :group 'org-export-general
183 :type '(repeat
184 (list
185 (string :tag "HTML language tag")
186 (string :tag "Author")
187 (string :tag "Date")
188 (string :tag "Table of Contents")
189 (string :tag "Footnotes"))))
191 (defcustom org-export-default-language "en"
192 "The default language of HTML export, as a string.
193 This should have an association in `org-export-language-setup'."
194 :group 'org-export-general
195 :type 'string)
197 (defvar org-export-page-description ""
198 "The page description, for the XHTML meta tag.
199 This is best set with the #+DESCRIPTION line in a file, it does not make
200 sense to set this globally.")
202 (defvar org-export-page-keywords ""
203 "The page description, for the XHTML meta tag.
204 This is best set with the #+KEYWORDS line in a file, it does not make
205 sense to set this globally.")
207 (defcustom org-export-skip-text-before-1st-heading nil
208 "Non-nil means skip all text before the first headline when exporting.
209 When nil, that text is exported as well."
210 :group 'org-export-general
211 :type 'boolean)
213 (defcustom org-export-headline-levels 3
214 "The last level which is still exported as a headline.
215 Inferior levels will produce itemize lists when exported.
216 Note that a numeric prefix argument to an exporter function overrides
217 this setting.
219 This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
220 :group 'org-export-general
221 :type 'integer)
223 (defcustom org-export-with-section-numbers t
224 "Non-nil means add section numbers to headlines when exporting.
226 This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
227 :group 'org-export-general
228 :type 'boolean)
230 (defcustom org-export-section-number-format '((("1" ".")) . "")
231 "Format of section numbers for export.
232 The variable has two components.
233 1. A list of lists, each indicating a counter type and a separator.
234 The counter type can be any of \"1\", \"A\", \"a\", \"I\", or \"a\".
235 It causes causes numeric, alphabetic, or roman counters, respectively.
236 The separator is only used if another counter for a subsection is being
237 added.
238 If there are more numbered section levels than entries in this lists,
239 then the last entry will be reused.
240 2. A terminator string that will be added after the entire
241 section number."
242 :group 'org-export-general
243 :type '(cons
244 (repeat
245 (list
246 (string :tag "Counter Type")
247 (string :tag "Separator ")))
248 (string :tag "Terminator")))
250 (defcustom org-export-with-toc t
251 "Non-nil means create a table of contents in exported files.
252 The TOC contains headlines with levels up to`org-export-headline-levels'.
253 When an integer, include levels up to N in the toc, this may then be
254 different from `org-export-headline-levels', but it will not be allowed
255 to be larger than the number of headline levels.
256 When nil, no table of contents is made.
258 Headlines which contain any TODO items will be marked with \"(*)\" in
259 ASCII export, and with red color in HTML output, if the option
260 `org-export-mark-todo-in-toc' is set.
262 In HTML output, the TOC will be clickable.
264 This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
265 or \"toc:3\"."
266 :group 'org-export-general
267 :type '(choice
268 (const :tag "No Table of Contents" nil)
269 (const :tag "Full Table of Contents" t)
270 (integer :tag "TOC to level")))
272 (defcustom org-export-mark-todo-in-toc nil
273 "Non-nil means mark TOC lines that contain any open TODO items."
274 :group 'org-export-general
275 :type 'boolean)
277 (defcustom org-export-with-todo-keywords t
278 "Non-nil means include TODO keywords in export.
279 When nil, remove all these keywords from the export."
280 :group 'org-export-general
281 :type 'boolean)
283 (defcustom org-export-with-priority nil
284 "Non-nil means include priority cookies in export.
285 When nil, remove priority cookies for export."
286 :group 'org-export-general
287 :type 'boolean)
289 (defcustom org-export-preserve-breaks nil
290 "Non-nil means preserve all line breaks when exporting.
291 Normally, in HTML output paragraphs will be reformatted. In ASCII
292 export, line breaks will always be preserved, regardless of this variable.
294 This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
295 :group 'org-export-general
296 :type 'boolean)
298 (defcustom org-export-with-archived-trees 'headline
299 "Whether subtrees with the ARCHIVE tag should be exported.
300 This can have three different values
301 nil Do not export, pretend this tree is not present
302 t Do export the entire tree
303 headline Only export the headline, but skip the tree below it."
304 :group 'org-export-general
305 :group 'org-archive
306 :type '(choice
307 (const :tag "not at all" nil)
308 (const :tag "headline only" 'headline)
309 (const :tag "entirely" t)))
311 (defcustom org-export-author-info t
312 "Non-nil means insert author name and email into the exported file.
314 This option can also be set with the +OPTIONS line,
315 e.g. \"author:nil\"."
316 :group 'org-export-general
317 :type 'boolean)
319 (defcustom org-export-email-info nil
320 "Non-nil means insert author name and email into the exported file.
322 This option can also be set with the +OPTIONS line,
323 e.g. \"email:t\"."
324 :group 'org-export-general
325 :type 'boolean)
327 (defcustom org-export-creator-info t
328 "Non-nil means the postamble should contain a creator sentence.
329 This sentence is \"HTML generated by org-mode XX in emacs XXX\"."
330 :group 'org-export-general
331 :type 'boolean)
333 (defcustom org-export-time-stamp-file t
334 "Non-nil means insert a time stamp into the exported file.
335 The time stamp shows when the file was created.
337 This option can also be set with the +OPTIONS line,
338 e.g. \"timestamp:nil\"."
339 :group 'org-export-general
340 :type 'boolean)
342 (defcustom org-export-with-timestamps t
343 "If nil, do not export time stamps and associated keywords."
344 :group 'org-export-general
345 :type 'boolean)
347 (defcustom org-export-remove-timestamps-from-toc t
348 "If t, remove timestamps from the table of contents entries."
349 :group 'org-export-general
350 :type 'boolean)
352 (defcustom org-export-with-tags 'not-in-toc
353 "If nil, do not export tags, just remove them from headlines.
354 If this is the symbol `not-in-toc', tags will be removed from table of
355 contents entries, but still be shown in the headlines of the document.
357 This option can also be set with the +OPTIONS line, e.g. \"tags:nil\"."
358 :group 'org-export-general
359 :type '(choice
360 (const :tag "Off" nil)
361 (const :tag "Not in TOC" not-in-toc)
362 (const :tag "On" t)))
364 (defcustom org-export-with-drawers nil
365 "Non-nil means export with drawers like the property drawer.
366 When t, all drawers are exported. This may also be a list of
367 drawer names to export."
368 :group 'org-export-general
369 :type '(choice
370 (const :tag "All drawers" t)
371 (const :tag "None" nil)
372 (repeat :tag "Selected drawers"
373 (string :tag "Drawer name"))))
375 (defvar org-export-first-hook nil
376 "Hook called as the first thing in each exporter.
377 Point will be still in the original buffer.
378 Good for general initialization")
380 (defvar org-export-preprocess-hook nil
381 "Hook for preprocessing an export buffer.
382 Pretty much the first thing when exporting is running this hook.
383 Point will be in a temporary buffer that contains a copy of
384 the original buffer, or of the section that is being export.
385 All the other hooks in the org-export-preprocess... category
386 also work in that temporary buffer, already modified by various
387 stages of the processing.")
389 (defvar org-export-preprocess-after-include-files-hook nil
390 "Hook for preprocessing an export buffer.
391 This is run after the contents of included files have been inserted.")
393 (defvar org-export-preprocess-after-tree-selection-hook nil
394 "Hook for preprocessing an export buffer.
395 This is run after selection of trees to be exported has happened.
396 This selection includes tags-based selection, as well as removal
397 of commented and archived trees.")
399 (defvar org-export-preprocess-after-headline-targets-hook nil
400 "Hook for preprocessing export buffer.
401 This is run just after the headline targets have been defined and
402 the target-alist has been set up.")
404 (defvar org-export-preprocess-before-selecting-backend-code-hook nil
405 "Hook for preprocessing an export buffer.
406 This is run just before backend-specific blocks get selected.")
408 (defvar org-export-preprocess-after-blockquote-hook nil
409 "Hook for preprocessing an export buffer.
410 This is run after blockquote/quote/verse/center have been marked
411 with cookies.")
413 (defvar org-export-preprocess-before-normalizing-links-hook nil
414 "Hook for preprocessing an export buffer.
415 This hook is run before links are normalized.")
417 (defvar org-export-preprocess-before-backend-specifics-hook nil
418 "Hook run before backend-specific functions are called during preprocessing.")
420 (defvar org-export-preprocess-final-hook nil
421 "Hook for preprocessing an export buffer.
422 This is run as the last thing in the preprocessing buffer, just before
423 returning the buffer string to the backend.")
425 (defgroup org-export-translation nil
426 "Options for translating special ascii sequences for the export backends."
427 :tag "Org Export Translation"
428 :group 'org-export)
430 (defcustom org-export-with-emphasize t
431 "Non-nil means interpret *word*, /word/, and _word_ as emphasized text.
432 If the export target supports emphasizing text, the word will be
433 typeset in bold, italic, or underlined, respectively. Works only for
434 single words, but you can say: I *really* *mean* *this*.
435 Not all export backends support this.
437 This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
438 :group 'org-export-translation
439 :type 'boolean)
441 (defcustom org-export-with-footnotes t
442 "If nil, export [1] as a footnote marker.
443 Lines starting with [1] will be formatted as footnotes.
445 This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
446 :group 'org-export-translation
447 :type 'boolean)
449 (defcustom org-export-with-sub-superscripts t
450 "Non-nil means interpret \"_\" and \"^\" for export.
451 When this option is turned on, you can use TeX-like syntax for sub- and
452 superscripts. Several characters after \"_\" or \"^\" will be
453 considered as a single item - so grouping with {} is normally not
454 needed. For example, the following things will be parsed as single
455 sub- or superscripts.
457 10^24 or 10^tau several digits will be considered 1 item.
458 10^-12 or 10^-tau a leading sign with digits or a word
459 x^2-y^3 will be read as x^2 - y^3, because items are
460 terminated by almost any nonword/nondigit char.
461 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
463 Still, ambiguity is possible - so when in doubt use {} to enclose the
464 sub/superscript. If you set this variable to the symbol `{}',
465 the braces are *required* in order to trigger interpretations as
466 sub/superscript. This can be helpful in documents that need \"_\"
467 frequently in plain text.
469 Not all export backends support this, but HTML does.
471 This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
472 :group 'org-export-translation
473 :type '(choice
474 (const :tag "Always interpret" t)
475 (const :tag "Only with braces" {})
476 (const :tag "Never interpret" nil)))
478 (defcustom org-export-with-TeX-macros t
479 "Non-nil means interpret simple TeX-like macros when exporting.
480 For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
481 Not only real TeX macros will work here, but the standard HTML entities
482 for math can be used as macro names as well. For a list of supported
483 names in HTML export, see the constant `org-entities' and the user option
484 `org-entities-user'.
485 Not all export backends support this.
487 This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
488 :group 'org-export-translation
489 :group 'org-export-latex
490 :type 'boolean)
492 (defcustom org-export-with-LaTeX-fragments nil
493 "Non-nil means convert LaTeX fragments to images when exporting to HTML.
494 When set, the exporter will find LaTeX environments if the \\begin line is
495 the first non-white thing on a line. It will also find the math delimiters
496 like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
497 display math.
499 This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\".
501 The default is nil, because this option needs the `dvipng' program which
502 is not available on all systems."
503 :group 'org-export-translation
504 :group 'org-export-latex
505 :type 'boolean)
507 (defcustom org-export-with-fixed-width t
508 "Non-nil means lines starting with \":\" will be in fixed width font.
509 This can be used to have pre-formatted text, fragments of code etc. For
510 example:
511 : ;; Some Lisp examples
512 : (while (defc cnt)
513 : (ding))
514 will be looking just like this in also HTML. See also the QUOTE keyword.
515 Not all export backends support this.
517 This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
518 :group 'org-export-translation
519 :type 'boolean)
521 (defcustom org-match-sexp-depth 3
522 "Number of stacked braces for sub/superscript matching.
523 This has to be set before loading org.el to be effective."
524 :group 'org-export-translation
525 :type 'integer)
527 (defgroup org-export-tables nil
528 "Options for exporting tables in Org-mode."
529 :tag "Org Export Tables"
530 :group 'org-export)
532 (defcustom org-export-with-tables t
533 "If non-nil, lines starting with \"|\" define a table.
534 For example:
536 | Name | Address | Birthday |
537 |-------------+----------+-----------|
538 | Arthur Dent | England | 29.2.2100 |
540 Not all export backends support this.
542 This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
543 :group 'org-export-tables
544 :type 'boolean)
546 (defcustom org-export-highlight-first-table-line t
547 "Non-nil means highlight the first table line.
548 In HTML export, this means use <th> instead of <td>.
549 In tables created with table.el, this applies to the first table line.
550 In Org-mode tables, all lines before the first horizontal separator
551 line will be formatted with <th> tags."
552 :group 'org-export-tables
553 :type 'boolean)
555 (defcustom org-export-table-remove-special-lines t
556 "Remove special lines and marking characters in calculating tables.
557 This removes the special marking character column from tables that are set
558 up for spreadsheet calculations. It also removes the entire lines
559 marked with `!', `_', or `^'. The lines with `$' are kept, because
560 the values of constants may be useful to have."
561 :group 'org-export-tables
562 :type 'boolean)
564 (defcustom org-export-prefer-native-exporter-for-tables nil
565 "Non-nil means always export tables created with table.el natively.
566 Natively means use the HTML code generator in table.el.
567 When nil, Org-mode's own HTML generator is used when possible (i.e. if
568 the table does not use row- or column-spanning). This has the
569 advantage, that the automatic HTML conversions for math symbols and
570 sub/superscripts can be applied. Org-mode's HTML generator is also
571 much faster. The LaTeX exporter always use the native exporter for
572 table.el tables."
573 :group 'org-export-tables
574 :type 'boolean)
577 (defgroup org-export-xml nil
578 "Options specific for XML export of Org-mode files."
579 :tag "Org Export XML"
580 :group 'org-export)
582 ;;;; Exporting
584 ;;; Variables, constants, and parameter plists
586 (defconst org-level-max 20)
588 (defvar org-current-export-file nil) ; dynamically scoped parameter
589 (defvar org-current-export-dir nil) ; dynamically scoped parameter
590 (defvar org-export-opt-plist nil
591 "Contains the current option plist.")
592 (defvar org-last-level nil) ; dynamically scoped variable
593 (defvar org-min-level nil) ; dynamically scoped variable
594 (defvar org-levels-open nil) ; dynamically scoped parameter
596 (defconst org-export-plist-vars
597 '((:link-up nil org-export-html-link-up)
598 (:link-home nil org-export-html-link-home)
599 (:language nil org-export-default-language)
600 (:keywords nil org-export-page-keywords)
601 (:description nil org-export-page-description)
602 (:customtime nil org-display-custom-times)
603 (:headline-levels "H" org-export-headline-levels)
604 (:section-numbers "num" org-export-with-section-numbers)
605 (:section-number-format nil org-export-section-number-format)
606 (:table-of-contents "toc" org-export-with-toc)
607 (:preserve-breaks "\\n" org-export-preserve-breaks)
608 (:archived-trees nil org-export-with-archived-trees)
609 (:emphasize "*" org-export-with-emphasize)
610 (:sub-superscript "^" org-export-with-sub-superscripts)
611 (:special-strings "-" org-export-with-special-strings)
612 (:footnotes "f" org-export-with-footnotes)
613 (:drawers "d" org-export-with-drawers)
614 (:tags "tags" org-export-with-tags)
615 (:todo-keywords "todo" org-export-with-todo-keywords)
616 (:priority "pri" org-export-with-priority)
617 (:TeX-macros "TeX" org-export-with-TeX-macros)
618 (:LaTeX-fragments "LaTeX" org-export-with-LaTeX-fragments)
619 (:latex-listings nil org-export-latex-listings)
620 (:skip-before-1st-heading "skip" org-export-skip-text-before-1st-heading)
621 (:fixed-width ":" org-export-with-fixed-width)
622 (:timestamps "<" org-export-with-timestamps)
623 (:author-info "author" org-export-author-info)
624 (:email-info "email" org-export-email-info)
625 (:creator-info "creator" org-export-creator-info)
626 (:time-stamp-file "timestamp" org-export-time-stamp-file)
627 (:tables "|" org-export-with-tables)
628 (:table-auto-headline nil org-export-highlight-first-table-line)
629 (:style-include-default nil org-export-html-style-include-default)
630 (:style-include-scripts nil org-export-html-style-include-scripts)
631 (:style nil org-export-html-style)
632 (:style-extra nil org-export-html-style-extra)
633 (:agenda-style nil org-agenda-export-html-style)
634 (:convert-org-links nil org-export-html-link-org-files-as-html)
635 (:inline-images nil org-export-html-inline-images)
636 (:html-extension nil org-export-html-extension)
637 (:xml-declaration nil org-export-html-xml-declaration)
638 (:html-table-tag nil org-export-html-table-tag)
639 (:expand-quoted-html "@" org-export-html-expand)
640 (:timestamp nil org-export-html-with-timestamp)
641 (:publishing-directory nil org-export-publishing-directory)
642 (:preamble nil org-export-html-preamble)
643 (:postamble nil org-export-html-postamble)
644 (:auto-preamble nil org-export-html-auto-preamble)
645 (:auto-postamble nil org-export-html-auto-postamble)
646 (:author nil user-full-name)
647 (:email nil user-mail-address)
648 (:select-tags nil org-export-select-tags)
649 (:exclude-tags nil org-export-exclude-tags)
651 (:latex-image-options nil org-export-latex-image-default-option))
652 "List of properties that represent export/publishing variables.
653 Each element is a list of 3 items:
654 1. The property that is used internally, and also for org-publish-project-alist
655 2. The string that can be used in the OPTION lines to set this option,
656 or nil if this option cannot be changed in this way
657 3. The customization variable that sets the default for this option."
660 (defun org-default-export-plist ()
661 "Return the property list with default settings for the export variables."
662 (let* ((infile (org-infile-export-plist))
663 (letbind (plist-get infile :let-bind))
664 (l org-export-plist-vars) rtn e s v)
665 (while (setq e (pop l))
666 (setq s (nth 2 e)
667 v (cond
668 ((assq s letbind) (nth 1 (assq s letbind)))
669 ((boundp s) (symbol-value s))
670 (t nil))
671 rtn (cons (car e) (cons v rtn))))
672 rtn))
674 (defvar org-export-inbuffer-options-extra nil
675 "List of additional in-buffer options that should be detected.
676 Just before export, the buffer is scanned for options like #+TITLE, #+EMAIL,
677 etc. Extensions can add to this list to get their options detected, and they
678 can then add a function to `org-export-options-filters' to process these
679 options.
680 Each element in this list must be a list, with the in-buffer keyword as car,
681 and a property (a symbol) as the next element. All occurrences of the
682 keyword will be found, the values concatenated with a space character
683 in between, and the result stored in the export options property list.")
685 (defvar org-export-options-filters nil
686 "Functions to be called to finalize the export/publishing options.
687 All these options are stored in a property list, and each of the functions
688 in this hook gets a chance to modify this property list. Each function
689 must accept the property list as an argument, and must return the (possibly
690 modified) list.")
692 ;; FIXME: should we fold case here?
693 (defun org-infile-export-plist ()
694 "Return the property list with file-local settings for export."
695 (save-excursion
696 (save-restriction
697 (widen)
698 (goto-char (point-min))
699 (let ((re (org-make-options-regexp
700 (append
701 '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE"
702 "LINK_UP" "LINK_HOME" "SETUPFILE" "STYLE"
703 "LATEX_HEADER" "LATEX_CLASS"
704 "EXPORT_SELECT_TAGS" "EXPORT_EXCLUDE_TAGS"
705 "KEYWORDS" "DESCRIPTION" "MACRO" "BIND")
706 (mapcar 'car org-export-inbuffer-options-extra))))
707 p key val text options a pr style
708 latex-header latex-class macros letbind
709 ext-setup-or-nil setup-contents (start 0))
710 (while (or (and ext-setup-or-nil
711 (string-match re ext-setup-or-nil start)
712 (setq start (match-end 0)))
713 (and (setq ext-setup-or-nil nil start 0)
714 (re-search-forward re nil t)))
715 (setq key (upcase (org-match-string-no-properties 1 ext-setup-or-nil))
716 val (org-match-string-no-properties 2 ext-setup-or-nil))
717 (cond
718 ((setq a (assoc key org-export-inbuffer-options-extra))
719 (setq pr (nth 1 a))
720 (setq p (plist-put p pr (concat (plist-get p pr) " " val))))
721 ((string-equal key "TITLE") (setq p (plist-put p :title val)))
722 ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
723 ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
724 ((string-equal key "DATE") (setq p (plist-put p :date val)))
725 ((string-equal key "KEYWORDS") (setq p (plist-put p :keywords val)))
726 ((string-equal key "DESCRIPTION")
727 (setq p (plist-put p :description val)))
728 ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
729 ((string-equal key "STYLE")
730 (setq style (concat style "\n" val)))
731 ((string-equal key "LATEX_HEADER")
732 (setq latex-header (concat latex-header "\n" val)))
733 ((string-equal key "LATEX_CLASS")
734 (setq latex-class val))
735 ((string-equal key "TEXT")
736 (setq text (if text (concat text "\n" val) val)))
737 ((string-equal key "OPTIONS")
738 (setq options (concat val " " options)))
739 ((string-equal key "BIND")
740 (push (read (concat "(" val ")")) letbind))
741 ((string-equal key "LINK_UP")
742 (setq p (plist-put p :link-up val)))
743 ((string-equal key "LINK_HOME")
744 (setq p (plist-put p :link-home val)))
745 ((string-equal key "EXPORT_SELECT_TAGS")
746 (setq p (plist-put p :select-tags (org-split-string val))))
747 ((string-equal key "EXPORT_EXCLUDE_TAGS")
748 (setq p (plist-put p :exclude-tags (org-split-string val))))
749 ((string-equal key "MACRO")
750 (push val macros))
751 ((equal key "SETUPFILE")
752 (setq setup-contents (org-file-contents
753 (expand-file-name
754 (org-remove-double-quotes
755 (org-trim val)))
756 'noerror))
757 (if (not ext-setup-or-nil)
758 (setq ext-setup-or-nil setup-contents start 0)
759 (setq ext-setup-or-nil
760 (concat (substring ext-setup-or-nil 0 start)
761 "\n" setup-contents "\n"
762 (substring ext-setup-or-nil start)))))))
763 (setq p (plist-put p :text text))
764 (when (and letbind (org-export-confirm-letbind))
765 (setq p (plist-put p :let-bind letbind)))
766 (when style (setq p (plist-put p :style-extra style)))
767 (when latex-header
768 (setq p (plist-put p :latex-header-extra (substring latex-header 1))))
769 (when latex-class
770 (setq p (plist-put p :latex-class latex-class)))
771 (when options
772 (setq p (org-export-add-options-to-plist p options)))
773 ;; Add macro definitions
774 (setq p (plist-put p :macro-date "(eval (format-time-string \"$1\"))"))
775 (setq p (plist-put p :macro-time "(eval (format-time-string \"$1\"))"))
776 (setq p (plist-put
777 p :macro-modification-time
778 (and (buffer-file-name)
779 (file-exists-p (buffer-file-name))
780 (concat
781 "(eval (format-time-string \"$1\" '"
782 (prin1-to-string (nth 5 (file-attributes
783 (buffer-file-name))))
784 "))"))))
785 (setq p (plist-put p :macro-input-file (and (buffer-file-name)
786 (file-name-nondirectory
787 (buffer-file-name)))))
788 (while (setq val (pop macros))
789 (when (string-match "^\\([-a-zA-Z0-9_]+\\)[ \t]+\\(.*?[ \t]*$\\)" val)
790 (setq p (plist-put
791 p (intern
792 (concat ":macro-" (downcase (match-string 1 val))))
793 (org-export-interpolate-newlines (match-string 2 val))))))
794 p))))
796 (defun org-export-interpolate-newlines (s)
797 (while (string-match "\\\\n" s)
798 (setq s (replace-match "\n" t t s)))
801 (defvar org-export-allow-BIND-local nil)
802 (defun org-export-confirm-letbind ()
803 "Can we use #+BIND values during export?
804 By default this will ask fro confirmation by the user, to divert possible
805 security risks."
806 (cond
807 ((not org-export-allow-BIND) nil)
808 ((eq org-export-allow-BIND t) t)
809 ((local-variable-p 'org-export-allow-BIND-local (current-buffer))
810 org-export-allow-BIND-local)
811 (t (org-set-local 'org-export-allow-BIND-local
812 (yes-or-no-p "Allow BIND values in this buffer? ")))))
814 (defun org-install-letbind ()
815 "Install the values from #+BIND lines as local variables."
816 (let ((letbind (plist-get org-export-opt-plist :let-bind))
817 pair)
818 (while (setq pair (pop letbind))
819 (org-set-local (car pair) (nth 1 pair)))))
821 (defun org-export-add-options-to-plist (p options)
822 "Parse an OPTIONS line and set values in the property list P."
823 (let (o)
824 (when options
825 (let ((op org-export-plist-vars))
826 (while (setq o (pop op))
827 (if (and (nth 1 o)
828 (string-match (concat (regexp-quote (nth 1 o))
829 ":\\([^ \t\n\r;,.]*\\)")
830 options))
831 (setq p (plist-put p (car o)
832 (car (read-from-string
833 (match-string 1 options))))))))))
836 (defun org-export-add-subtree-options (p pos)
837 "Add options in subtree at position POS to property list P."
838 (save-excursion
839 (goto-char pos)
840 (when (org-at-heading-p)
841 (let (a)
842 ;; This is actually read in `org-export-get-title-from-subtree'
843 ;; (when (setq a (org-entry-get pos "EXPORT_TITLE"))
844 ;; (setq p (plist-put p :title a)))
845 (when (setq a (org-entry-get pos "EXPORT_TEXT"))
846 (setq p (plist-put p :text a)))
847 (when (setq a (org-entry-get pos "EXPORT_AUTHOR"))
848 (setq p (plist-put p :author a)))
849 (when (setq a (org-entry-get pos "EXPORT_DATE"))
850 (setq p (plist-put p :date a)))
851 (when (setq a (org-entry-get pos "EXPORT_OPTIONS"))
852 (setq p (org-export-add-options-to-plist p a)))))
855 (defun org-export-directory (type plist)
856 (let* ((val (plist-get plist :publishing-directory))
857 (dir (if (listp val)
858 (or (cdr (assoc type val)) ".")
859 val)))
860 dir))
862 (defun org-export-process-option-filters (plist)
863 (let ((functions org-export-options-filters) f)
864 (while (setq f (pop functions))
865 (setq plist (funcall f plist))))
866 plist)
868 ;;;###autoload
869 (defun org-export (&optional arg)
870 "Export dispatcher for Org-mode.
871 When `org-export-run-in-background' is non-nil, try to run the command
872 in the background. This will be done only for commands that write
873 to a file. For details see the docstring of `org-export-run-in-background'.
875 The prefix argument ARG will be passed to the exporter. However, if
876 ARG is a double universal prefix `C-u C-u', that means to inverse the
877 value of `org-export-run-in-background'."
878 (interactive "P")
879 (let* ((bg (org-xor (equal arg '(16)) org-export-run-in-background))
880 subtree-p
881 (help "[t] insert the export option template
882 \[v] limit export to visible part of outline tree
883 \[1] only export the current subtree
884 \[SPC] publish enclosing subtree (with LaTeX_CLASS or EXPORT_FILE_NAME prop)
886 \[a] export as ASCII [A] to temporary buffer
888 \[h] export as HTML [H] to temporary buffer [R] export region
889 \[b] export as HTML and open in browser
891 \[l] export as LaTeX [L] to temporary buffer
892 \[p] export as LaTeX and process to PDF [d] ... and open PDF file
894 \[D] export as DocBook [V] export as DocBook, process to PDF, and open
896 \[m] export as Freemind mind map
897 \[x] export as XOXO
898 \[g] export using Wes Hardaker's generic exporter
900 \[i] export current file as iCalendar file
901 \[I] export all agenda files as iCalendar files [c] ...as one combined file
903 \[F] publish current file [P] publish current project
904 \[X] publish a project... [E] publish every projects")
905 (cmds
906 '((?t org-insert-export-options-template nil)
907 (?v org-export-visible nil)
908 (?a org-export-as-ascii t)
909 (?A org-export-as-ascii-to-buffer t)
910 (?h org-export-as-html t)
911 (?b org-export-as-html-and-open t)
912 (?H org-export-as-html-to-buffer nil)
913 (?R org-export-region-as-html nil)
914 (?x org-export-as-xoxo t)
915 (?g org-export-generic t)
916 (?D org-export-as-docbook t)
917 (?V org-export-as-docbook-pdf-and-open t)
918 (?m org-export-as-freemind t)
919 (?l org-export-as-latex t)
920 (?p org-export-as-pdf t)
921 (?d org-export-as-pdf-and-open t)
922 (?L org-export-as-latex-to-buffer nil)
923 (?i org-export-icalendar-this-file t)
924 (?I org-export-icalendar-all-agenda-files t)
925 (?c org-export-icalendar-combine-agenda-files t)
926 (?F org-publish-current-file t)
927 (?P org-publish-current-project t)
928 (?X org-publish t)
929 (?E org-publish-all t)))
930 r1 r2 ass
931 (cpos (point)) (cbuf (current-buffer)) bpos)
932 (save-excursion
933 (save-window-excursion
934 (delete-other-windows)
935 (with-output-to-temp-buffer "*Org Export/Publishing Help*"
936 (princ help))
937 (org-fit-window-to-buffer (get-buffer-window
938 "*Org Export/Publishing Help*"))
939 (message "Select command: ")
940 (setq r1 (read-char-exclusive))
941 (when (eq r1 ?1)
942 (setq subtree-p t)
943 (message "Select command (for subtree): ")
944 (setq r1 (read-char-exclusive)))
945 (when (eq r1 ?\ )
946 (let ((case-fold-search t))
947 (if (re-search-backward
948 "^[ \t]+\\(:latex_class:\\|:export_title:\\)[ \t]+\\S-"
949 nil t)
950 (progn
951 (org-back-to-heading t)
952 (setq subtree-p t)
953 (setq bpos (point))
954 (message "Select command (for subtree): ")
955 (setq r1 (read-char-exclusive)))
956 (error "No enclosing node with LaTeX_CLASS or EXPORT_FILE_NAME")
957 )))))
958 (and bpos (goto-char bpos))
959 (setq r2 (if (< r1 27) (+ r1 96) r1))
960 (unless (setq ass (assq r2 cmds))
961 (error "No command associated with key %c" r1))
962 (if (and bg (nth 2 ass)
963 (not (buffer-base-buffer))
964 (not (org-region-active-p)))
965 ;; execute in background
966 (let ((p (start-process
967 (concat "Exporting " (file-name-nondirectory (buffer-file-name)))
968 "*Org Processes*"
969 (expand-file-name invocation-name invocation-directory)
970 "-batch"
971 "-l" user-init-file
972 "--eval" "(require 'org-exp)"
973 "--eval" "(setq org-wait .2)"
974 (buffer-file-name)
975 "-f" (symbol-name (nth 1 ass)))))
976 (set-process-sentinel p 'org-export-process-sentinel)
977 (message "Background process \"%s\": started" p))
978 ;; background processing not requested, or not possible
979 (if subtree-p (progn (outline-mark-subtree) (activate-mark)))
980 (call-interactively (nth 1 ass))
981 (when (and bpos (get-buffer-window cbuf))
982 (let ((cw (selected-window)))
983 (select-window (get-buffer-window cbuf))
984 (goto-char cpos)
985 (deactivate-mark)
986 (select-window cw))))))
988 (defun org-export-process-sentinel (process status)
989 (if (string-match "\n+\\'" status)
990 (setq status (substring status 0 -1)))
991 (message "Background process \"%s\": %s" process status))
993 ;;; General functions for all backends
995 (defvar org-export-target-aliases nil
996 "Alist of targets with invisible aliases.")
997 (defvar org-export-preferred-target-alist nil
998 "Alist of section id's with preferred aliases.")
999 (defvar org-export-id-target-alist nil
1000 "Alist of section id's with preferred aliases.")
1001 (defvar org-export-code-refs nil
1002 "Alist of code references and line numbers")
1004 (defun org-export-preprocess-string (string &rest parameters)
1005 "Cleanup STRING so that that the true exported has a more consistent source.
1006 This function takes STRING, which should be a buffer-string of an org-file
1007 to export. It then creates a temporary buffer where it does its job.
1008 The result is then again returned as a string, and the exporter works
1009 on this string to produce the exported version."
1010 (interactive)
1011 (let* ((htmlp (plist-get parameters :for-html))
1012 (asciip (plist-get parameters :for-ascii))
1013 (latexp (plist-get parameters :for-LaTeX))
1014 (docbookp (plist-get parameters :for-docbook))
1015 (backend (cond (htmlp 'html)
1016 (latexp 'latex)
1017 (asciip 'ascii)
1018 (docbookp 'docbook)))
1019 (archived-trees (plist-get parameters :archived-trees))
1020 (inhibit-read-only t)
1021 (drawers org-drawers)
1022 (outline-regexp "\\*+ ")
1023 target-alist rtn)
1025 (setq org-export-target-aliases nil
1026 org-export-preferred-target-alist nil
1027 org-export-id-target-alist nil
1028 org-export-code-refs nil)
1030 (with-current-buffer (get-buffer-create " org-mode-tmp")
1031 (erase-buffer)
1032 (insert string)
1033 (setq case-fold-search t)
1035 (let ((inhibit-read-only t))
1036 (remove-text-properties (point-min) (point-max)
1037 '(read-only t)))
1039 ;; Remove license-to-kill stuff
1040 ;; The caller marks some stuff for killing, stuff that has been
1041 ;; used to create the page title, for example.
1042 (org-export-kill-licensed-text)
1044 (let ((org-inhibit-startup t)) (org-mode))
1045 (setq case-fold-search t)
1046 (org-install-letbind)
1048 ;; Call the hook
1049 (run-hooks 'org-export-preprocess-hook)
1051 ;; Process the macros
1052 (org-export-preprocess-apply-macros)
1053 (run-hooks 'org-export-preprocess-after-macros-hook)
1055 (untabify (point-min) (point-max))
1057 ;; Handle include files, and call a hook
1058 (org-export-handle-include-files)
1059 (run-hooks 'org-export-preprocess-after-include-files-hook)
1061 ;; Get rid of archived trees
1062 (org-export-remove-archived-trees archived-trees)
1064 ;; Remove comment environment and comment subtrees
1065 (org-export-remove-comment-blocks-and-subtrees)
1067 ;; Get rid of excluded trees, and call a hook
1068 (org-export-handle-export-tags (plist-get parameters :select-tags)
1069 (plist-get parameters :exclude-tags))
1070 (run-hooks 'org-export-preprocess-after-tree-selection-hook)
1072 ;; Handle source code snippets
1073 (org-export-replace-src-segments-and-examples backend)
1075 ;; Protect short examples marked by a leading colon
1076 (org-export-protect-colon-examples)
1078 ;; Normalize footnotes
1079 (when (plist-get parameters :footnotes)
1080 (org-footnote-normalize nil t))
1082 ;; Find all headings and compute the targets for them
1083 (setq target-alist (org-export-define-heading-targets target-alist))
1085 (run-hooks 'org-export-preprocess-after-headline-targets-hook)
1087 ;; Find HTML special classes for headlines
1088 (org-export-remember-html-container-classes)
1090 ;; Get rid of drawers
1091 (org-export-remove-or-extract-drawers
1092 drawers (plist-get parameters :drawers) backend)
1094 ;; Get the correct stuff before the first headline
1095 (when (plist-get parameters :skip-before-1st-heading)
1096 (goto-char (point-min))
1097 (when (re-search-forward "^\\(#.*\n\\)?\\*+[ \t]" nil t)
1098 (delete-region (point-min) (match-beginning 0))
1099 (goto-char (point-min))
1100 (insert "\n")))
1101 (when (plist-get parameters :add-text)
1102 (goto-char (point-min))
1103 (insert (plist-get parameters :add-text) "\n"))
1105 ;; Remove todo-keywords before exporting, if the user has requested so
1106 (org-export-remove-headline-metadata parameters)
1108 ;; Find targets in comments and move them out of comments,
1109 ;; but mark them as targets that should be invisible
1110 (setq target-alist (org-export-handle-invisible-targets target-alist))
1112 ;; Select and protect backend specific stuff, throw away stuff
1113 ;; that is specific for other backends
1114 (run-hooks 'org-export-preprocess-before-selecting-backend-code-hook)
1115 (org-export-select-backend-specific-text backend)
1117 ;; Protect quoted subtrees
1118 (org-export-protect-quoted-subtrees)
1120 ;; Remove clock lines
1121 (org-export-remove-clock-lines)
1123 ;; Protect verbatim elements
1124 (org-export-protect-verbatim)
1126 ;; Blockquotes, verse, and center
1127 (org-export-mark-blockquote-verse-center)
1128 (run-hooks 'org-export-preprocess-after-blockquote-hook)
1130 ;; Remove timestamps, if the user has requested so
1131 (unless (plist-get parameters :timestamps)
1132 (org-export-remove-timestamps))
1134 ;; Attach captions to the correct object
1135 (setq target-alist (org-export-attach-captions-and-attributes
1136 backend target-alist))
1138 ;; Find matches for radio targets and turn them into internal links
1139 (org-export-mark-radio-links)
1141 ;; Find all links that contain a newline and put them into a single line
1142 (org-export-concatenate-multiline-links)
1144 ;; Normalize links: Convert angle and plain links into bracket links
1145 ;; and expand link abbreviations
1146 (run-hooks 'org-export-preprocess-before-normalizing-links-hook)
1147 (org-export-normalize-links)
1149 ;; Find all internal links. If they have a fuzzy match (i.e. not
1150 ;; a *dedicated* target match, let the link point to the
1151 ;; corresponding section.
1152 (org-export-target-internal-links target-alist)
1154 ;; Find multiline emphasis and put them into single line
1155 (when (plist-get parameters :emph-multiline)
1156 (org-export-concatenate-multiline-emphasis))
1158 ;; Remove special table lines
1159 (when org-export-table-remove-special-lines
1160 (org-export-remove-special-table-lines))
1162 ;; Another hook
1163 (run-hooks 'org-export-preprocess-before-backend-specifics-hook)
1165 ;; LaTeX-specific preprocessing
1166 (when latexp
1167 (require 'org-latex nil)
1168 (org-export-latex-preprocess parameters))
1170 ;; ASCII-specific preprocessing
1171 (when asciip
1172 (org-export-ascii-preprocess parameters))
1174 ;; HTML-specific preprocessing
1175 (when htmlp
1176 (org-export-html-preprocess parameters))
1178 ;; DocBook-specific preprocessing
1179 (when docbookp
1180 (require 'org-docbook nil)
1181 (org-export-docbook-preprocess parameters))
1183 ;; Remove or replace comments
1184 (org-export-handle-comments (plist-get parameters :comments))
1186 ;; Run the final hook
1187 (run-hooks 'org-export-preprocess-final-hook)
1189 (setq rtn (buffer-string)))
1190 (kill-buffer " org-mode-tmp")
1191 rtn))
1193 (defun org-export-kill-licensed-text ()
1194 "Remove all text that is marked with a :org-license-to-kill property."
1195 (let (p)
1196 (while (setq p (text-property-any (point-min) (point-max)
1197 :org-license-to-kill t))
1198 (delete-region
1199 p (or (next-single-property-change p :org-license-to-kill)
1200 (point-max))))))
1202 (defun org-export-define-heading-targets (target-alist)
1203 "Find all headings and define the targets for them.
1204 The new targets are added to TARGET-ALIST, which is also returned."
1205 (goto-char (point-min))
1206 (org-init-section-numbers)
1207 (let ((re (concat "^" org-outline-regexp
1208 "\\| [ \t]*:\\(ID\\|CUSTOM_ID\\):[ \t]*\\([^ \t\r\n]+\\)"))
1209 level target last-section-target a id)
1210 (while (re-search-forward re nil t)
1211 (if (match-end 2)
1212 (progn
1213 (setq id (org-match-string-no-properties 2))
1214 (push (cons id target) target-alist)
1215 (setq a (or (assoc last-section-target org-export-target-aliases)
1216 (progn
1217 (push (list last-section-target)
1218 org-export-target-aliases)
1219 (car org-export-target-aliases))))
1220 (push (caar target-alist) (cdr a))
1221 (when (equal (match-string 1) "CUSTOM_ID")
1222 (if (not (assoc last-section-target
1223 org-export-preferred-target-alist))
1224 (push (cons last-section-target id)
1225 org-export-preferred-target-alist)))
1226 (when (equal (match-string 1) "ID")
1227 (if (not (assoc last-section-target
1228 org-export-id-target-alist))
1229 (push (cons last-section-target (concat "ID-" id))
1230 org-export-id-target-alist))))
1231 (setq level (org-reduced-level
1232 (save-excursion (goto-char (point-at-bol))
1233 (org-outline-level))))
1234 (setq target (org-solidify-link-text
1235 (format "sec-%s" (org-section-number level))))
1236 (setq last-section-target target)
1237 (push (cons target target) target-alist)
1238 (add-text-properties
1239 (point-at-bol) (point-at-eol)
1240 (list 'target target)))))
1241 target-alist)
1243 (defun org-export-handle-invisible-targets (target-alist)
1244 "Find targets in comments and move them out of comments.
1245 Mark them as invisible targets."
1246 (let (target tmp a)
1247 (goto-char (point-min))
1248 (while (re-search-forward "^#.*?\\(<<<?\\([^>\r\n]+\\)>>>?\\).*" nil t)
1249 ;; Check if the line before or after is a headline with a target
1250 (if (setq target (or (get-text-property (point-at-bol 0) 'target)
1251 (get-text-property (point-at-bol 2) 'target)))
1252 (progn
1253 ;; use the existing target in a neighboring line
1254 (setq tmp (match-string 2))
1255 (replace-match "")
1256 (and (looking-at "\n") (delete-char 1))
1257 (push (cons (setq tmp (org-solidify-link-text tmp)) target)
1258 target-alist)
1259 (setq a (or (assoc target org-export-target-aliases)
1260 (progn
1261 (push (list target) org-export-target-aliases)
1262 (car org-export-target-aliases))))
1263 (push tmp (cdr a)))
1264 ;; Make an invisible target
1265 (replace-match "\\1(INVISIBLE)"))))
1266 target-alist)
1268 (defun org-export-target-internal-links (target-alist)
1269 "Find all internal links and assign targets to them.
1270 If a link has a fuzzy match (i.e. not a *dedicated* target match),
1271 let the link point to the corresponding section.
1272 This function also handles the id links, if they have a match in
1273 the current file."
1274 (goto-char (point-min))
1275 (while (re-search-forward org-bracket-link-regexp nil t)
1276 (org-if-unprotected-at (1+ (match-beginning 0))
1277 (let* ((md (match-data))
1278 (desc (match-end 2))
1279 (link (org-link-unescape (match-string 1)))
1280 (slink (org-solidify-link-text link))
1281 found props pos cref
1282 (target
1283 (cond
1284 ((= (string-to-char link) ?#)
1285 ;; user wants exactly this link
1286 link)
1287 ((cdr (assoc slink target-alist))
1288 (or (cdr (assoc (assoc slink target-alist)
1289 org-export-preferred-target-alist))
1290 (cdr (assoc slink target-alist))))
1291 ((and (string-match "^id:" link)
1292 (cdr (assoc (substring link 3) target-alist))))
1293 ((string-match "^(\\(.*\\))$" link)
1294 (setq cref (match-string 1 link))
1295 (concat "coderef:" cref))
1296 ((string-match org-link-types-re link) nil)
1297 ((or (file-name-absolute-p link)
1298 (string-match "^\\." link))
1299 nil)
1301 (save-excursion
1302 (setq found (condition-case nil (org-link-search link)
1303 (error nil)))
1304 (when (and found
1305 (or (org-on-heading-p)
1306 (not (eq found 'dedicated))))
1307 (or (get-text-property (point) 'target)
1308 (get-text-property
1309 (max (point-min)
1310 (1- (or (previous-single-property-change
1311 (point) 'target) 0)))
1312 'target))))))))
1313 (when target
1314 (set-match-data md)
1315 (goto-char (match-beginning 1))
1316 (setq props (text-properties-at (point)))
1317 (delete-region (match-beginning 1) (match-end 1))
1318 (setq pos (point))
1319 (insert target)
1320 (unless desc (insert "][" link))
1321 (add-text-properties pos (point) props))))))
1323 (defun org-export-remember-html-container-classes ()
1324 "Store the HTML_CONTAINER_CLASS properties in a text property."
1325 (goto-char (point-min))
1326 (let (class)
1327 (while (re-search-forward
1328 "^[ \t]*:HTML_CONTAINER_CLASS:[ \t]+\\(\\S-+\\)" nil t)
1329 (setq class (match-string 1))
1330 (save-excursion
1331 (org-back-to-heading t)
1332 (put-text-property (point-at-bol) (point-at-eol) 'html-container-class class)))))
1334 (defvar org-export-format-drawer-function nil
1335 "Function to be called to format the contents of a drawer.
1336 The function must accept three parameters:
1337 BACKEND one of the symbols html, docbook, latex, ascii, xoxo
1338 NAME the drawer name, like \"PROPERTIES\"
1339 CONTENT the content of the drawer.
1340 The function should return the text to be inserted into the buffer.
1341 If this is nil, `org-export-format-drawer' is used as a default.")
1343 (defun org-export-remove-or-extract-drawers (all-drawers exp-drawers backend)
1344 "Remove drawers, or extract and format the content.
1345 ALL-DRAWERS is a list of all drawer names valid in the current buffer.
1346 EXP-DRAWERS can be t to keep all drawer contents, or a list of drawers
1347 whose content to keep. Any drawers that are in ALL-DRAWERS but not in
1348 EXP-DRAWERS will be removed.
1349 BACKEND is the current export backend."
1350 (goto-char (point-min))
1351 (let ((re (concat "^[ \t]*:\\("
1352 (mapconcat 'identity all-drawers "\\|")
1353 "\\):[ \t]*$"))
1354 name beg beg-content eol content)
1355 (while (re-search-forward re nil t)
1356 (org-if-unprotected
1357 (setq name (match-string 1))
1358 (setq beg (match-beginning 0)
1359 beg-content (1+ (point-at-eol))
1360 eol (point-at-eol))
1361 (if (not (and (re-search-forward
1362 "^\\([ \t]*:END:[ \t]*\n?\\)\\|^\\*+[ \t]" nil t)
1363 (match-end 1)))
1364 (goto-char eol)
1365 (goto-char (match-beginning 0))
1366 (and (looking-at ".*\n?") (replace-match ""))
1367 (setq content (buffer-substring beg-content (point)))
1368 (delete-region beg (point))
1369 (when (or (eq exp-drawers t)
1370 (member name exp-drawers))
1371 (setq content (funcall (or org-export-format-drawer-function
1372 'org-export-format-drawer)
1373 name content backend))
1374 (insert content)))))))
1376 (defun org-export-format-drawer (name content backend)
1377 "Format the content of a drawer as a colon example."
1378 (if (string-match "[ \t]+\\'" content)
1379 (setq content (substring content (match-beginning 0))))
1380 (while (string-match "\\`[ \t]*\n" content)
1381 (setq content (substring content (match-end 0))))
1382 (setq content (org-remove-indentation content))
1383 (setq content (concat ": " (mapconcat 'identity
1384 (org-split-string content "\n")
1385 "\n: ")
1386 "\n"))
1387 (setq content (concat " : " (upcase name) "\n" content))
1388 (org-add-props content nil 'org-protected t))
1390 (defun org-export-handle-export-tags (select-tags exclude-tags)
1391 "Modify the buffer, honoring SELECT-TAGS and EXCLUDE-TAGS.
1392 Both arguments are lists of tags.
1393 If any of SELECT-TAGS is found, all trees not marked by a SELECT-TAG
1394 will be removed.
1395 After that, all subtrees that are marked by EXCLUDE-TAGS will be
1396 removed as well."
1397 (remove-text-properties (point-min) (point-max) '(:org-delete t))
1398 (let* ((re-sel (concat ":\\(" (mapconcat 'regexp-quote
1399 select-tags "\\|")
1400 "\\):"))
1401 (re-excl (concat ":\\(" (mapconcat 'regexp-quote
1402 exclude-tags "\\|")
1403 "\\):"))
1404 beg end cont)
1405 (goto-char (point-min))
1406 (when (and select-tags
1407 (re-search-forward
1408 (concat "^\\*+[ \t].*" re-sel "[^ \t\n]*[ \t]*$") nil t))
1409 ;; At least one tree is marked for export, this means
1410 ;; all the unmarked stuff needs to go.
1411 ;; Dig out the trees that should be exported
1412 (goto-char (point-min))
1413 (outline-next-heading)
1414 (setq beg (point))
1415 (put-text-property beg (point-max) :org-delete t)
1416 (while (re-search-forward re-sel nil t)
1417 (when (org-on-heading-p)
1418 (org-back-to-heading)
1419 (remove-text-properties
1420 (max (1- (point)) (point-min))
1421 (setq cont (save-excursion (org-end-of-subtree t t)))
1422 '(:org-delete t))
1423 (while (and (org-up-heading-safe)
1424 (get-text-property (point) :org-delete))
1425 (remove-text-properties (max (1- (point)) (point-min))
1426 (point-at-eol) '(:org-delete t)))
1427 (goto-char cont))))
1428 ;; Remove the trees explicitly marked for noexport
1429 (when exclude-tags
1430 (goto-char (point-min))
1431 (while (re-search-forward re-excl nil t)
1432 (when (org-at-heading-p)
1433 (org-back-to-heading t)
1434 (setq beg (point))
1435 (org-end-of-subtree t t)
1436 (delete-region beg (point))
1437 (when (featurep 'org-inlinetask)
1438 (org-inlinetask-remove-END-maybe)))))
1439 ;; Remove everything that is now still marked for deletion
1440 (goto-char (point-min))
1441 (while (setq beg (text-property-any (point-min) (point-max) :org-delete t))
1442 (setq end (or (next-single-property-change beg :org-delete)
1443 (point-max)))
1444 (delete-region beg end))))
1446 (defun org-export-remove-archived-trees (export-archived-trees)
1447 "Remove archived trees.
1448 When EXPORT-ARCHIVED-TREES is `headline;, only the headline will be exported.
1449 When it is t, the entire archived tree will be exported.
1450 When it is nil the entire tree including the headline will be removed
1451 from the buffer."
1452 (let ((re-archive (concat ":" org-archive-tag ":"))
1453 a b)
1454 (when (not (eq export-archived-trees t))
1455 (goto-char (point-min))
1456 (while (re-search-forward re-archive nil t)
1457 (if (not (org-on-heading-p t))
1458 (org-end-of-subtree t)
1459 (beginning-of-line 1)
1460 (setq a (if export-archived-trees
1461 (1+ (point-at-eol)) (point))
1462 b (org-end-of-subtree t))
1463 (if (> b a) (delete-region a b)))))))
1465 (defun org-export-remove-headline-metadata (opts)
1466 "Remove meta data from the headline, according to user options."
1467 (let ((re org-complex-heading-regexp)
1468 (todo (plist-get opts :todo-keywords))
1469 (tags (plist-get opts :tags))
1470 (pri (plist-get opts :priority))
1471 (elts '(1 2 3 4 5))
1472 rpl)
1473 (setq elts (delq nil (list 1 (if todo 2) (if pri 3) 4 (if tags 5))))
1474 (when (or (not todo) (not tags) (not pri))
1475 (goto-char (point-min))
1476 (while (re-search-forward re nil t)
1477 (org-if-unprotected
1478 (setq rpl (mapconcat (lambda (i) (if (match-end i) (match-string i) ""))
1479 elts " "))
1480 (replace-match rpl t t))))))
1482 (defun org-export-remove-timestamps ()
1483 "Remove timestamps and keywords for export."
1484 (goto-char (point-min))
1485 (while (re-search-forward org-maybe-keyword-time-regexp nil t)
1486 (backward-char 1)
1487 (org-if-unprotected
1488 (unless (save-match-data (org-at-table-p))
1489 (replace-match "")
1490 (beginning-of-line 1)
1491 (if (looking-at "[- \t]*\\(=>[- \t0-9:]*\\)?[ \t]*\n")
1492 (replace-match ""))))))
1494 (defun org-export-remove-clock-lines ()
1495 "Remove clock lines for export."
1496 (goto-char (point-min))
1497 (let ((re (concat "^[ \t]*" org-clock-string ".*\n?")))
1498 (while (re-search-forward re nil t)
1499 (org-if-unprotected
1500 (replace-match "")))))
1502 (defun org-export-protect-quoted-subtrees ()
1503 "Mark quoted subtrees with the protection property."
1504 (let ((re-quote (concat "^\\*+[ \t]+" org-quote-string "\\>")))
1505 (goto-char (point-min))
1506 (while (re-search-forward re-quote nil t)
1507 (goto-char (match-beginning 0))
1508 (end-of-line 1)
1509 (add-text-properties (point) (org-end-of-subtree t)
1510 '(org-protected t)))))
1512 (defun org-export-protect-verbatim ()
1513 "Mark verbatim snippets with the protection property."
1514 (goto-char (point-min))
1515 (while (re-search-forward org-verbatim-re nil t)
1516 (org-if-unprotected
1517 (add-text-properties (match-beginning 4) (match-end 4)
1518 '(org-protected t org-verbatim-emph t))
1519 (goto-char (1+ (match-end 4))))))
1521 (defun org-export-protect-colon-examples ()
1522 "Protect lines starting with a colon."
1523 (goto-char (point-min))
1524 (let ((re "^[ \t]*:\\([ \t]\\|$\\)") beg)
1525 (while (re-search-forward re nil t)
1526 (beginning-of-line 1)
1527 (setq beg (point))
1528 (while (looking-at re)
1529 (end-of-line 1)
1530 (or (eobp) (forward-char 1)))
1531 (add-text-properties beg (if (bolp) (1- (point)) (point))
1532 '(org-protected t)))))
1534 (defun org-export-select-backend-specific-text (backend)
1535 (let ((formatters
1536 '((docbook "DOCBOOK" "BEGIN_DOCBOOK" "END_DOCBOOK")
1537 (html "HTML" "BEGIN_HTML" "END_HTML")
1538 (beamer "BEAMER" "BEGIN_BEAMER" "END_BEAMER")
1539 (ascii "ASCII" "BEGIN_ASCII" "END_ASCII")
1540 (latex "LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
1541 (case-fold-search t)
1542 fmt beg beg-content end end-content)
1544 (while formatters
1545 (setq fmt (pop formatters))
1546 (when (eq (car fmt) backend)
1547 ;; This is selected code, put it into the file for real
1548 (goto-char (point-min))
1549 (while (re-search-forward (concat "^\\([ \t]*\\)#\\+" (cadr fmt)
1550 ":[ \t]*\\(.*\\)") nil t)
1551 (replace-match "\\1\\2" t)
1552 (add-text-properties
1553 (point-at-bol) (min (1+ (point-at-eol)) (point-max))
1554 '(org-protected t))))
1555 (goto-char (point-min))
1556 (while (re-search-forward (concat "^[ \t]*#\\+" (caddr fmt) "\\>.*\n?")
1557 nil t)
1558 (setq beg (match-beginning 0) beg-content (match-end 0))
1559 (when (re-search-forward (concat "^[ \t]*#\\+" (cadddr fmt) "\\>.*\n?")
1560 nil t)
1561 (setq end (match-end 0) end-content (match-beginning 0))
1562 (if (eq (car fmt) backend)
1563 ;; yes, keep this
1564 (progn
1565 (add-text-properties beg-content end-content '(org-protected t))
1566 (delete-region (match-beginning 0) (match-end 0))
1567 (save-excursion
1568 (goto-char beg)
1569 (delete-region (point) (1+ (point-at-eol)))))
1570 ;; No, this is for a different backend, kill it
1571 (delete-region beg end)))))))
1573 (defun org-export-mark-blockquote-verse-center ()
1574 "Mark block quote and verse environments with special cookies.
1575 These special cookies will later be interpreted by the backend."
1576 ;; Blockquotes
1577 (let (type t1 ind beg end beg1 end1 content)
1578 (goto-char (point-min))
1579 (while (re-search-forward
1580 "^\\([ \t]*\\)#\\+\\(begin_\\(\\(block\\)?quote\\|verse\\|center\\)\\>.*\\)"
1581 nil t)
1582 (setq ind (length (match-string 1))
1583 type (downcase (match-string 3))
1584 t1 (if (equal type "quote") "blockquote" type))
1585 (setq beg (match-beginning 0)
1586 beg1 (1+ (match-end 0)))
1587 (when (re-search-forward (concat "^[ \t]*#\\+end_" type "\\>.*") nil t)
1588 (setq end (1+ (point-at-eol))
1589 end1 (1- (match-beginning 0)))
1590 (setq content (org-remove-indentation (buffer-substring beg1 end1)))
1591 (setq content (concat "ORG-" (upcase t1) "-START\n"
1592 content "\n"
1593 "ORG-" (upcase t1) "-END\n"))
1594 (delete-region beg end)
1595 (insert (org-add-props content nil 'original-indentation ind))))))
1597 (defun org-export-attach-captions-and-attributes (backend target-alist)
1598 "Move #+CAPTION, #+ATTR_BACKEND, and #+LABEL text into text properties.
1599 If the next thing following is a table, add the text properties to the first
1600 table line. If it is a link, add it to the line containing the link."
1601 (goto-char (point-min))
1602 (remove-text-properties (point-min) (point-max)
1603 '(org-caption nil org-attributes nil))
1604 (let ((case-fold-search t)
1605 (re (concat "^[ \t]*#\\+caption:[ \t]+\\(.*\\)"
1606 "\\|"
1607 "^[ \t]*#\\+attr_" (symbol-name backend) ":[ \t]+\\(.*\\)"
1608 "\\|"
1609 "^[ \t]*#\\+label:[ \t]+\\(.*\\)"
1610 "\\|"
1611 "^[ \t]*\\(|[^-]\\)"
1612 "\\|"
1613 "^[ \t]*\\[\\[.*\\]\\][ \t]*$"))
1614 cap attr label end)
1615 (while (re-search-forward re nil t)
1616 (cond
1617 ((match-end 1)
1618 (setq cap (concat cap (if cap " " "") (org-trim (match-string 1)))))
1619 ((match-end 2)
1620 (setq attr (concat attr (if attr " " "") (org-trim (match-string 2)))))
1621 ((match-end 3)
1622 (setq label (org-trim (match-string 3))))
1624 (setq end (if (match-end 4)
1625 (let ((ee (org-table-end)))
1626 (prog1 (1- (marker-position ee)) (move-marker ee nil)))
1627 (point-at-eol)))
1628 (add-text-properties (point-at-bol) end
1629 (list 'org-caption cap
1630 'org-attributes attr
1631 'org-label label))
1632 (if label (push (cons label label) target-alist))
1633 (goto-char end)
1634 (setq cap nil attr nil label nil)))))
1635 target-alist)
1637 (defun org-export-remove-comment-blocks-and-subtrees ()
1638 "Remove the comment environment, and also commented subtrees."
1639 (let ((re-commented (concat "^\\*+[ \t]+" org-comment-string "\\>"))
1640 case-fold-search)
1641 ;; Remove comment environment
1642 (goto-char (point-min))
1643 (setq case-fold-search t)
1644 (while (re-search-forward
1645 "^#\\+begin_comment[ \t]*\n[^\000]*?^#\\+end_comment\\>.*" nil t)
1646 (replace-match "" t t))
1647 ;; Remove subtrees that are commented
1648 (goto-char (point-min))
1649 (setq case-fold-search nil)
1650 (while (re-search-forward re-commented nil t)
1651 (goto-char (match-beginning 0))
1652 (delete-region (point) (org-end-of-subtree t)))))
1654 (defun org-export-handle-comments (commentsp)
1655 "Remove comments, or convert to backend-specific format.
1656 COMMENTSP can be a format string for publishing comments.
1657 When it is nil, all comments will be removed."
1658 (let ((re "^\\(#\\|[ \t]*#\\+\\)\\(.*\n?\\)")
1659 pos)
1660 (goto-char (point-min))
1661 (while (or (looking-at re)
1662 (re-search-forward re nil t))
1663 (setq pos (match-beginning 0))
1664 (if (and commentsp
1665 (not (equal (char-before (match-end 1)) ?+)))
1666 (progn (add-text-properties
1667 (match-beginning 0) (match-end 0) '(org-protected t))
1668 (replace-match (format commentsp (match-string 2)) t t))
1669 (goto-char (1+ pos))
1670 (org-if-unprotected
1671 (replace-match "")
1672 (goto-char (max (point-min) (1- pos))))))))
1674 (defun org-export-mark-radio-links ()
1675 "Find all matches for radio targets and turn them into internal links."
1676 (let ((re-radio (and org-target-link-regexp
1677 (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)"))))
1678 (goto-char (point-min))
1679 (when re-radio
1680 (while (re-search-forward re-radio nil t)
1681 (unless
1682 (save-match-data
1683 (or (org-in-regexp org-bracket-link-regexp)
1684 (org-in-regexp org-plain-link-re)
1685 (org-in-regexp "<<[^<>]+>>")))
1686 (org-if-unprotected
1687 (replace-match "\\1[[\\2]]")))))))
1689 (defun org-export-remove-special-table-lines ()
1690 "Remove tables lines that are used for internal purposes."
1691 (goto-char (point-min))
1692 (while (re-search-forward "^[ \t]*|" nil t)
1693 (beginning-of-line 1)
1694 (if (or (looking-at "[ \t]*| *[!_^] *|")
1695 (not
1696 (memq
1698 (mapcar
1699 (lambda (f)
1700 (or (= (length f) 0)
1701 (string-match
1702 "\\`<\\([0-9]\\|[rl]\\|[rl][0-9]+\\)>\\'" f)))
1703 (org-split-string ;; FIXME, can't we do this without splitting???
1704 (buffer-substring (point-at-bol) (point-at-eol))
1705 "[ \t]*|[ \t]*")))))
1706 (delete-region (max (point-min) (1- (point-at-bol)))
1707 (point-at-eol))
1708 (end-of-line 1))))
1710 (defun org-export-protect-sub-super (s)
1711 (save-match-data
1712 (while (string-match "\\([^\\\\]\\)\\([_^]\\)" s)
1713 (setq s (replace-match "\\1\\\\\\2" nil nil s)))
1716 (defun org-export-normalize-links ()
1717 "Convert all links to bracket links, and expand link abbreviations."
1718 (let ((re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
1719 (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
1720 nodesc)
1721 (goto-char (point-min))
1722 (while (re-search-forward re-plain-link nil t)
1723 (goto-char (1- (match-end 0)))
1724 (org-if-unprotected-at (1+ (match-beginning 0))
1725 (let* ((s (concat (match-string 1)
1726 "[[" (match-string 2) ":" (match-string 3)
1727 "][" (match-string 2) ":" (org-export-protect-sub-super
1728 (match-string 3))
1729 "]]")))
1730 ;; added 'org-link face to links
1731 (put-text-property 0 (length s) 'face 'org-link s)
1732 (replace-match s t t))))
1733 (goto-char (point-min))
1734 (while (re-search-forward re-angle-link nil t)
1735 (goto-char (1- (match-end 0)))
1736 (org-if-unprotected
1737 (let* ((s (concat (match-string 1)
1738 "[[" (match-string 2) ":" (match-string 3)
1739 "][" (match-string 2) ":" (org-export-protect-sub-super
1740 (match-string 3))
1741 "]]")))
1742 (put-text-property 0 (length s) 'face 'org-link s)
1743 (replace-match s t t))))
1744 (goto-char (point-min))
1745 (while (re-search-forward org-bracket-link-regexp nil t)
1746 (goto-char (1- (match-end 0)))
1747 (setq nodesc (not (match-end 3)))
1748 (org-if-unprotected
1749 (let* ((xx (save-match-data
1750 (org-translate-link
1751 (org-link-expand-abbrev (match-string 1)))))
1752 (s (concat
1753 "[[" (org-add-props (copy-sequence xx)
1754 nil 'org-protected t 'org-no-description nodesc)
1756 (if (match-end 3)
1757 (match-string 2)
1758 (concat "[" (copy-sequence xx)
1759 "]"))
1760 "]")))
1761 (put-text-property 0 (length s) 'face 'org-link s)
1762 (replace-match s t t))))))
1764 (defun org-export-concatenate-multiline-links ()
1765 "Find multi-line links and put it all into a single line.
1766 This is to make sure that the line-processing export backends
1767 can work correctly."
1768 (goto-char (point-min))
1769 (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
1770 (org-if-unprotected-at (match-beginning 1)
1771 (replace-match "\\1 \\3")
1772 (goto-char (match-beginning 0)))))
1774 (defun org-export-concatenate-multiline-emphasis ()
1775 "Find multi-line emphasis and put it all into a single line.
1776 This is to make sure that the line-processing export backends
1777 can work correctly."
1778 (goto-char (point-min))
1779 (while (re-search-forward org-emph-re nil t)
1780 (if (and (not (= (char-after (match-beginning 3))
1781 (char-after (match-beginning 4))))
1782 (save-excursion (goto-char (match-beginning 0))
1783 (save-match-data (not (org-at-table-p)))))
1784 (org-if-unprotected
1785 (subst-char-in-region (match-beginning 0) (match-end 0)
1786 ?\n ?\ t)
1787 (goto-char (1- (match-end 0))))
1788 (goto-char (1+ (match-beginning 0))))))
1790 (defun org-export-grab-title-from-buffer ()
1791 "Get a title for the current document, from looking at the buffer."
1792 (let ((inhibit-read-only t))
1793 (save-excursion
1794 (goto-char (point-min))
1795 (let ((end (if (looking-at org-outline-regexp)
1796 (point)
1797 (save-excursion (outline-next-heading) (point)))))
1798 (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t)
1799 ;; Mark the line so that it will not be exported as normal text.
1800 (org-unmodified
1801 (add-text-properties (match-beginning 0) (match-end 0)
1802 (list :org-license-to-kill t)))
1803 ;; Return the title string
1804 (org-trim (match-string 0)))))))
1806 (defun org-export-get-title-from-subtree ()
1807 "Return subtree title and exclude it from export."
1808 (let (title (rbeg (region-beginning)) (rend (region-end)))
1809 (save-excursion
1810 (goto-char rbeg)
1811 (when (and (org-at-heading-p)
1812 (>= (org-end-of-subtree t t) rend))
1813 ;; This is a subtree, we take the title from the first heading
1814 (goto-char rbeg)
1815 (looking-at org-todo-line-regexp)
1816 (setq title (match-string 3))
1817 (org-unmodified
1818 (add-text-properties (point) (1+ (point-at-eol))
1819 (list :org-license-to-kill t)))
1820 (setq title (or (org-entry-get nil "EXPORT_TITLE") title))))
1821 title))
1823 (defun org-solidify-link-text (s &optional alist)
1824 "Take link text and make a safe target out of it."
1825 (save-match-data
1826 (let* ((rtn
1827 (mapconcat
1828 'identity
1829 (org-split-string s "[ \t\r\n]+") "=="))
1830 (a (assoc rtn alist)))
1831 (or (cdr a) rtn))))
1833 (defun org-get-min-level (lines &optional offset)
1834 "Get the minimum level in LINES."
1835 (let ((re "^\\(\\*+\\) ") l)
1836 (catch 'exit
1837 (while (setq l (pop lines))
1838 (if (string-match re l)
1839 (throw 'exit (org-tr-level (- (length (match-string 1 l))
1840 (or offset 0))))))
1841 1)))
1843 ;; Variable holding the vector with section numbers
1844 (defvar org-section-numbers (make-vector org-level-max 0))
1846 (defun org-init-section-numbers ()
1847 "Initialize the vector for the section numbers."
1848 (let* ((level -1)
1849 (numbers (nreverse (org-split-string "" "\\.")))
1850 (depth (1- (length org-section-numbers)))
1851 (i depth) number-string)
1852 (while (>= i 0)
1853 (if (> i level)
1854 (aset org-section-numbers i 0)
1855 (setq number-string (or (car numbers) "0"))
1856 (if (string-match "\\`[A-Z]\\'" number-string)
1857 (aset org-section-numbers i
1858 (- (string-to-char number-string) ?A -1))
1859 (aset org-section-numbers i (string-to-number number-string)))
1860 (pop numbers))
1861 (setq i (1- i)))))
1863 (defun org-section-number (&optional level)
1864 "Return a string with the current section number.
1865 When LEVEL is non-nil, increase section numbers on that level."
1866 (let* ((depth (1- (length org-section-numbers)))
1867 (string "")
1868 (fmts (car org-export-section-number-format))
1869 (term (cdr org-export-section-number-format))
1870 (sep "")
1871 ctype fmt idx n)
1872 (when level
1873 (when (> level -1)
1874 (aset org-section-numbers
1875 level (1+ (aref org-section-numbers level))))
1876 (setq idx (1+ level))
1877 (while (<= idx depth)
1878 (if (not (= idx 1))
1879 (aset org-section-numbers idx 0))
1880 (setq idx (1+ idx))))
1881 (setq idx 0)
1882 (while (<= idx depth)
1883 (when (> (aref org-section-numbers idx) 0)
1884 (setq fmt (or (pop fmts) fmt)
1885 ctype (car fmt)
1886 n (aref org-section-numbers idx)
1887 string (if (> n 0)
1888 (concat string sep (org-number-to-counter n ctype))
1889 (concat string ".0"))
1890 sep (nth 1 fmt)))
1891 (setq idx (1+ idx)))
1892 (save-match-data
1893 (if (string-match "\\`\\([@0]\\.\\)+" string)
1894 (setq string (replace-match "" t nil string)))
1895 (if (string-match "\\(\\.0\\)+\\'" string)
1896 (setq string (replace-match "" t nil string))))
1897 (concat string term)))
1899 (defun org-number-to-counter (n type)
1900 "Concert number N to a string counter, according to TYPE.
1901 TYPE must be a string, any of:
1902 1 number
1903 A A,B,....
1904 a a,b,....
1905 I upper case roman numeral
1906 i lower case roman numeral"
1907 (cond
1908 ((equal type "1") (number-to-string n))
1909 ((equal type "A") (char-to-string (+ ?A n -1)))
1910 ((equal type "a") (char-to-string (+ ?a n -1)))
1911 ((equal type "I") (org-number-to-roman n))
1912 ((equal type "i") (downcase (org-number-to-roman n)))
1913 (t (error "Invalid counter type `%s'" type))))
1915 (defun org-number-to-roman (n)
1916 "Convert integer N into a roman numeral."
1917 (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
1918 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
1919 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
1920 ( 1 . "I")))
1921 (res ""))
1922 (if (<= n 0)
1923 (number-to-string n)
1924 (while roman
1925 (if (>= n (caar roman))
1926 (setq n (- n (caar roman))
1927 res (concat res (cdar roman)))
1928 (pop roman)))
1929 res)))
1931 ;;; Macros
1933 (defun org-export-preprocess-apply-macros ()
1934 "Replace macro references."
1935 (goto-char (point-min))
1936 (let (sy val key args args2 s n)
1937 (while (re-search-forward
1938 "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
1939 nil t)
1940 (unless (save-match-data
1941 (save-excursion
1942 (goto-char (point-at-bol))
1943 (looking-at "[ \t]*#\\+macro")))
1944 (setq key (downcase (match-string 1))
1945 args (match-string 3))
1946 (when (setq val (or (plist-get org-export-opt-plist
1947 (intern (concat ":macro-" key)))
1948 (plist-get org-export-opt-plist
1949 (intern (concat ":" key)))))
1950 (save-match-data
1951 (when args
1952 (setq args (org-split-string args ",[ \t\n]*") args2 nil)
1953 (setq args (mapcar 'org-trim args))
1954 (while args
1955 (while (string-match "\\\\\\'" (car args))
1956 ;; repair bad splits
1957 (setcar (cdr args) (concat (substring (car args) 0 -1)
1958 ";" (nth 1 args)))
1959 (pop args))
1960 (push (pop args) args2))
1961 (setq args (nreverse args2))
1962 (setq s 0)
1963 (while (string-match "\\$\\([0-9]+\\)" val s)
1964 (setq s (1+ (match-beginning 0))
1965 n (string-to-number (match-string 1 val)))
1966 (and (>= (length args) n)
1967 (setq val (replace-match (nth (1- n) args) t t val)))))
1968 (when (string-match "\\`(eval\\>" val)
1969 (setq val (eval (read val))))
1970 (if (and val (not (stringp val)))
1971 (setq val (format "%s" val))))
1972 (and (stringp val)
1973 (prog1 (replace-match val t t)
1974 (goto-char (match-beginning 0)))))))))
1976 (defun org-export-apply-macros-in-string (s)
1977 "Apply the macros in string S."
1978 (when s
1979 (with-temp-buffer
1980 (insert s)
1981 (org-export-preprocess-apply-macros)
1982 (buffer-string))))
1984 ;;; Include files
1986 (defun org-export-handle-include-files ()
1987 "Include the contents of include files, with proper formatting."
1988 (let ((case-fold-search t)
1989 params file markup lang start end prefix prefix1 switches)
1990 (goto-char (point-min))
1991 (while (re-search-forward "^#\\+INCLUDE:?[ \t]+\\(.*\\)" nil t)
1992 (setq params (read (concat "(" (match-string 1) ")"))
1993 prefix (org-get-and-remove-property 'params :prefix)
1994 prefix1 (org-get-and-remove-property 'params :prefix1)
1995 file (org-symname-or-string (pop params))
1996 markup (org-symname-or-string (pop params))
1997 lang (and (member markup '("src" "SRC"))
1998 (org-symname-or-string (pop params)))
1999 switches (mapconcat '(lambda (x) (format "%s" x)) params " ")
2000 start nil end nil)
2001 (delete-region (match-beginning 0) (match-end 0))
2002 (if (or (not file)
2003 (not (file-exists-p file))
2004 (not (file-readable-p file)))
2005 (insert (format "CANNOT INCLUDE FILE %s" file))
2006 (when markup
2007 (if (equal (downcase markup) "src")
2008 (setq start (format "#+begin_src %s %s\n"
2009 (or lang "fundamental")
2010 (or switches ""))
2011 end "#+end_src")
2012 (setq start (format "#+begin_%s %s\n" markup switches)
2013 end (format "#+end_%s" markup))))
2014 (insert (or start ""))
2015 (insert (org-get-file-contents (expand-file-name file)
2016 prefix prefix1 markup))
2017 (or (bolp) (newline))
2018 (insert (or end ""))))))
2020 (defun org-get-file-contents (file &optional prefix prefix1 markup)
2021 "Get the contents of FILE and return them as a string.
2022 If PREFIX is a string, prepend it to each line. If PREFIX1
2023 is a string, prepend it to the first line instead of PREFIX.
2024 If MARKUP, don't protect org-like lines, the exporter will
2025 take care of the block they are in."
2026 (if (stringp markup) (setq markup (downcase markup)))
2027 (with-temp-buffer
2028 (insert-file-contents file)
2029 (when (or prefix prefix1)
2030 (goto-char (point-min))
2031 (while (not (eobp))
2032 (insert (or prefix1 prefix))
2033 (setq prefix1 nil)
2034 (beginning-of-line 2)))
2035 (buffer-string)
2036 (when (member markup '("src" "example"))
2037 (goto-char (point-min))
2038 (while (re-search-forward "^\\([*#]\\|[ \t]*#\\+\\)" nil t)
2039 (goto-char (match-beginning 0))
2040 (insert ",")
2041 (end-of-line 1)))
2042 (buffer-string)))
2044 (defun org-get-and-remove-property (listvar prop)
2045 "Check if the value of LISTVAR contains PROP as a property.
2046 If yes, return the value of that property (i.e. the element following
2047 in the list) and remove property and value from the list in LISTVAR."
2048 (let ((list (symbol-value listvar)) m v)
2049 (when (setq m (member prop list))
2050 (setq v (nth 1 m))
2051 (if (equal (car list) prop)
2052 (set listvar (cddr list))
2053 (setcdr (nthcdr (- (length list) (length m) 1) list)
2054 (cddr m))
2055 (set listvar list)))
2058 (defun org-symname-or-string (s)
2059 (if (symbolp s)
2060 (if s (symbol-name s) s)
2063 ;;; Fontification and line numbers for code examples
2065 (defvar org-export-last-code-line-counter-value 0)
2067 (defun org-export-replace-src-segments-and-examples (backend)
2068 "Replace source code segments with special code for export."
2069 (setq org-export-last-code-line-counter-value 0)
2070 (let ((case-fold-search t)
2071 lang code trans opts indent caption)
2072 (goto-char (point-min))
2073 (while (re-search-forward
2074 "\\(^\\([ \t]*\\)#\\+BEGIN_SRC:?[ \t]+\\([^ \t\n]+\\)\\(.*\\)\n\\([^\000]+?\n\\)[ \t]*#\\+END_SRC.*\n?\\)\\|\\(^\\([ \t]*\\)#\\+BEGIN_EXAMPLE:?\\(?:[ \t]+\\(.*\\)\\)?\n\\([^\000]+?\n\\)[ \t]*#\\+END_EXAMPLE.*\n?\\)"
2075 nil t)
2076 (if (match-end 1)
2077 ;; src segments
2078 (setq lang (match-string 3)
2079 opts (match-string 4)
2080 code (match-string 5)
2081 indent (length (match-string 2))
2082 caption (get-text-property 0 'org-caption (match-string 0)))
2083 (setq lang nil
2084 opts (match-string 8)
2085 code (match-string 9)
2086 indent (length (match-string 7))
2087 caption (get-text-property 0 'org-caption (match-string 0))))
2089 (setq trans (org-export-format-source-code-or-example
2090 backend lang code opts indent caption))
2091 (replace-match trans t t))))
2093 (defvar htmlp) ;; dynamically scoped
2094 (defvar latexp) ;; dynamically scoped
2095 (defvar org-export-latex-verbatim-wrap) ;; defined in org-latex.el
2096 (defvar org-export-latex-listings) ;; defined in org-latex.el
2097 (defvar org-export-latex-listings-langs) ;; defined in org-latex.el
2099 (defun org-export-format-source-code-or-example
2100 (backend lang code &optional opts indent caption)
2101 "Format CODE from language LANG and return it formatted for export.
2102 If LANG is nil, do not add any fontification.
2103 OPTS contains formatting options, like `-n' for triggering numbering lines,
2104 and `+n' for continuing previous numbering.
2105 Code formatting according to language currently only works for HTML.
2106 Numbering lines works for all three major backends (html, latex, and ascii).
2107 INDENT was the original indentation of the block."
2108 (save-match-data
2109 (let (num cont rtn rpllbl keepp textareap preserve-indentp cols rows fmt)
2110 (setq opts (or opts "")
2111 num (string-match "[-+]n\\>" opts)
2112 cont (string-match "\\+n\\>" opts)
2113 rpllbl (string-match "-r\\>" opts)
2114 keepp (string-match "-k\\>" opts)
2115 textareap (string-match "-t\\>" opts)
2116 preserve-indentp (or org-src-preserve-indentation
2117 (string-match "-i\\>" opts))
2118 cols (if (string-match "-w[ \t]+\\([0-9]+\\)" opts)
2119 (string-to-number (match-string 1 opts))
2121 rows (if (string-match "-h[ \t]+\\([0-9]+\\)" opts)
2122 (string-to-number (match-string 1 opts))
2123 (org-count-lines code))
2124 fmt (if (string-match "-l[ \t]+\"\\([^\"\n]+\\)\"" opts)
2125 (match-string 1 opts)))
2126 (when (and textareap (eq backend 'html))
2127 ;; we cannot use numbering or highlighting.
2128 (setq num nil cont nil lang nil))
2129 (if keepp (setq rpllbl 'keep))
2130 (setq rtn (if preserve-indentp code (org-remove-indentation code)))
2131 (when (string-match "^," rtn)
2132 (setq rtn (with-temp-buffer
2133 (insert rtn)
2134 ;; Free up the protected lines
2135 (goto-char (point-min))
2136 (while (re-search-forward "^," nil t)
2137 (if (or (equal lang "org")
2138 (save-match-data
2139 (looking-at "\\([*#]\\|[ \t]*#\\+\\)")))
2140 (replace-match ""))
2141 (end-of-line 1))
2142 (buffer-string))))
2143 ;; Now backend-specific coding
2144 (setq rtn
2145 (cond
2146 ((eq backend 'docbook)
2147 (setq rtn (org-export-number-lines rtn 'docbook 0 0 num cont rpllbl fmt))
2148 (concat "\n#+BEGIN_DOCBOOK\n"
2149 (org-add-props (concat "<programlisting><![CDATA["
2151 "]]></programlisting>\n")
2152 '(org-protected t))
2153 "#+END_DOCBOOK\n"))
2154 ((eq backend 'html)
2155 ;; We are exporting to HTML
2156 (when lang
2157 (require 'htmlize nil t)
2158 (when (not (fboundp 'htmlize-region-for-paste))
2159 ;; we do not have htmlize.el, or an old version of it
2160 (setq lang nil)
2161 (message
2162 "htmlize.el 1.34 or later is needed for source code formatting")))
2164 (if lang
2165 (let* ((lang-m (when lang
2166 (or (cdr (assoc lang org-src-lang-modes))
2167 lang)))
2168 (mode (and lang-m (intern
2169 (concat
2170 (if (symbolp lang-m)
2171 (symbol-name lang-m)
2172 lang-m)
2173 "-mode"))))
2174 (org-inhibit-startup t)
2175 (org-startup-folded nil))
2176 (setq rtn
2177 (with-temp-buffer
2178 (insert rtn)
2179 (if (functionp mode)
2180 (funcall mode)
2181 (fundamental-mode))
2182 (font-lock-fontify-buffer)
2183 (org-src-mode)
2184 (set-buffer-modified-p nil)
2185 (org-export-htmlize-region-for-paste
2186 (point-min) (point-max))))
2187 (if (string-match "<pre\\([^>]*\\)>\n*" rtn)
2188 (setq rtn
2189 (concat
2190 (if caption
2191 (concat
2192 "<div class=\"org-src-container\">"
2193 (format
2194 "<label class=\"org-src-name\">%s</label>"
2195 caption))
2197 (replace-match
2198 (format "<pre class=\"src src-%s\">\n" lang)
2199 t t rtn)
2200 (if caption "</div>" "")))))
2201 (if textareap
2202 (setq rtn (concat
2203 (format "<p>\n<textarea cols=\"%d\" rows=\"%d\">"
2204 cols rows)
2205 rtn "</textarea>\n</p>\n"))
2206 (with-temp-buffer
2207 (insert rtn)
2208 (goto-char (point-min))
2209 (while (re-search-forward "[<>&]" nil t)
2210 (replace-match (cdr (assq (char-before)
2211 '((?&."&amp;")(?<."&lt;")(?>."&gt;"))))
2212 t t))
2213 (setq rtn (buffer-string)))
2214 (setq rtn (concat "<pre class=\"example\">\n" rtn "</pre>\n"))))
2215 (unless textareap
2216 (setq rtn (org-export-number-lines rtn 'html 1 1 num
2217 cont rpllbl fmt)))
2218 (if (string-match "\\(\\`<[^>]*>\\)\n" rtn)
2219 (setq rtn (replace-match "\\1" t nil rtn)))
2220 (concat "\n#+BEGIN_HTML\n" (org-add-props rtn '(org-protected t)) "\n#+END_HTML\n\n"))
2221 ((eq backend 'latex)
2222 (setq rtn (org-export-number-lines rtn 'latex 0 0 num cont rpllbl fmt))
2223 (concat "#+BEGIN_LaTeX\n"
2224 (org-add-props
2225 (if org-export-latex-listings
2226 (concat
2227 (if lang
2228 (let*
2229 ((lang-sym (intern lang))
2230 (lstlang
2231 (or (cadr
2232 (assq
2233 lang-sym
2234 org-export-latex-listings-langs))
2235 lang)))
2236 (format "\\lstset{language=%s}\n" lstlang))
2237 "\n")
2238 (when caption
2239 (format "\n%s $\\equiv$ \n" caption))
2240 "\\begin{lstlisting}\n"
2241 rtn "\\end{lstlisting}\n")
2242 (concat (car org-export-latex-verbatim-wrap)
2243 rtn (cdr org-export-latex-verbatim-wrap)))
2244 '(org-protected t))
2245 "#+END_LaTeX\n"))
2246 ((eq backend 'ascii)
2247 ;; This is not HTML or LaTeX, so just make it an example.
2248 (setq rtn (org-export-number-lines rtn 'ascii 0 0 num cont rpllbl fmt))
2249 (concat caption "\n"
2250 "#+BEGIN_ASCII\n"
2251 (org-add-props
2252 (concat
2253 (mapconcat
2254 (lambda (l) (concat " " l))
2255 (org-split-string rtn "\n")
2256 "\n")
2257 "\n")
2258 '(org-protected t))
2259 "#+END_ASCII\n"))))
2260 (org-add-props rtn nil 'original-indentation indent))))
2262 (defun org-export-number-lines (text backend
2263 &optional skip1 skip2 number cont
2264 replace-labels label-format)
2265 (setq skip1 (or skip1 0) skip2 (or skip2 0))
2266 (if (not cont) (setq org-export-last-code-line-counter-value 0))
2267 (with-temp-buffer
2268 (insert text)
2269 (goto-char (point-max))
2270 (skip-chars-backward " \t\n\r")
2271 (delete-region (point) (point-max))
2272 (beginning-of-line (- 1 skip2))
2273 (let* ((last (org-current-line))
2274 (n org-export-last-code-line-counter-value)
2275 (nmax (+ n (- last skip1)))
2276 (fmt (format "%%%dd: " (length (number-to-string nmax))))
2278 (cond
2279 ((eq backend 'html) (format "<span class=\"linenr\">%s</span>"
2280 fmt))
2281 ((eq backend 'ascii) fmt)
2282 ((eq backend 'latex) fmt)
2283 ((eq backend 'docbook) fmt)
2284 (t "")))
2285 (label-format (or label-format org-coderef-label-format))
2286 (label-pre (if (string-match "%s" label-format)
2287 (substring label-format 0 (match-beginning 0))
2288 label-format))
2289 (label-post (if (string-match "%s" label-format)
2290 (substring label-format (match-end 0))
2291 ""))
2292 (lbl-re
2293 (concat
2294 ".*?\\S-.*?\\([ \t]*\\("
2295 (regexp-quote label-pre)
2296 "\\([-a-zA-Z0-9_ ]+\\)"
2297 (regexp-quote label-post)
2298 "\\)\\)"))
2299 ref)
2301 (org-goto-line (1+ skip1))
2302 (while (and (re-search-forward "^" nil t) (not (eobp)) (< n nmax))
2303 (if number
2304 (insert (format fm (incf n)))
2305 (forward-char 1))
2306 (when (looking-at lbl-re)
2307 (setq ref (match-string 3))
2308 (cond ((numberp replace-labels)
2309 ;; remove labels; use numbers for references when lines
2310 ;; are numbered, use labels otherwise
2311 (delete-region (match-beginning 1) (match-end 1))
2312 (push (cons ref (if (> n 0) n ref)) org-export-code-refs))
2313 ((eq replace-labels 'keep)
2314 ;; don't remove labels; use numbers for references when
2315 ;; lines are numbered, use labels otherwise
2316 (goto-char (match-beginning 2))
2317 (delete-region (match-beginning 2) (match-end 2))
2318 (insert "(" ref ")")
2319 (push (cons ref (if (> n 0) n (concat "(" ref ")")))
2320 org-export-code-refs))
2322 ;; don't remove labels and don't use numbers for
2323 ;; references
2324 (goto-char (match-beginning 2))
2325 (delete-region (match-beginning 2) (match-end 2))
2326 (insert "(" ref ")")
2327 (push (cons ref (concat "(" ref ")")) org-export-code-refs)))
2328 (when (eq backend 'html)
2329 (save-excursion
2330 (beginning-of-line 1)
2331 (insert (format "<span id=\"coderef-%s\" class=\"coderef-off\">"
2332 ref))
2333 (end-of-line 1)
2334 (insert "</span>")))))
2335 (setq org-export-last-code-line-counter-value n)
2336 (goto-char (point-max))
2337 (newline)
2338 (buffer-string))))
2340 (defun org-search-todo-below (line lines level)
2341 "Search the subtree below LINE for any TODO entries."
2342 (let ((rest (cdr (memq line lines)))
2343 (re org-todo-line-regexp)
2344 line lv todo)
2345 (catch 'exit
2346 (while (setq line (pop rest))
2347 (if (string-match re line)
2348 (progn
2349 (setq lv (- (match-end 1) (match-beginning 1))
2350 todo (and (match-beginning 2)
2351 (not (member (match-string 2 line)
2352 org-done-keywords))))
2353 ; TODO, not DONE
2354 (if (<= lv level) (throw 'exit nil))
2355 (if todo (throw 'exit t))))))))
2357 ;;;###autoload
2358 (defun org-export-visible (type arg)
2359 "Create a copy of the visible part of the current buffer, and export it.
2360 The copy is created in a temporary buffer and removed after use.
2361 TYPE is the final key (as a string) that also select the export command in
2362 the `C-c C-e' export dispatcher.
2363 As a special case, if the you type SPC at the prompt, the temporary
2364 org-mode file will not be removed but presented to you so that you can
2365 continue to use it. The prefix arg ARG is passed through to the exporting
2366 command."
2367 (interactive
2368 (list (progn
2369 (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")
2370 (read-char-exclusive))
2371 current-prefix-arg))
2372 (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?D ?x ?\ ?l ?p ?d ?L)))
2373 (error "Invalid export key"))
2374 (let* ((binding (cdr (assoc type
2375 '((?a . org-export-as-ascii)
2376 (?A . org-export-as-ascii-to-buffer)
2377 (?\C-a . org-export-as-ascii)
2378 (?b . org-export-as-html-and-open)
2379 (?\C-b . org-export-as-html-and-open)
2380 (?h . org-export-as-html)
2381 (?H . org-export-as-html-to-buffer)
2382 (?R . org-export-region-as-html)
2383 (?D . org-export-as-docbook)
2385 (?l . org-export-as-latex)
2386 (?p . org-export-as-pdf)
2387 (?d . org-export-as-pdf-and-open)
2388 (?L . org-export-as-latex-to-buffer)
2390 (?x . org-export-as-xoxo)))))
2391 (keepp (equal type ?\ ))
2392 (file buffer-file-name)
2393 (buffer (get-buffer-create "*Org Export Visible*"))
2394 s e)
2395 ;; Need to hack the drawers here.
2396 (save-excursion
2397 (goto-char (point-min))
2398 (while (re-search-forward org-drawer-regexp nil t)
2399 (goto-char (match-beginning 1))
2400 (or (org-invisible-p) (org-flag-drawer nil))))
2401 (with-current-buffer buffer (erase-buffer))
2402 (save-excursion
2403 (setq s (goto-char (point-min)))
2404 (while (not (= (point) (point-max)))
2405 (goto-char (org-find-invisible))
2406 (append-to-buffer buffer s (point))
2407 (setq s (goto-char (org-find-visible))))
2408 (org-cycle-hide-drawers 'all)
2409 (goto-char (point-min))
2410 (unless keepp
2411 ;; Copy all comment lines to the end, to make sure #+ settings are
2412 ;; still available for the second export step. Kind of a hack, but
2413 ;; does do the trick.
2414 (if (looking-at "#[^\r\n]*")
2415 (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
2416 (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
2417 (append-to-buffer buffer (1+ (match-beginning 0))
2418 (min (point-max) (1+ (match-end 0))))))
2419 (set-buffer buffer)
2420 (let ((buffer-file-name file)
2421 (org-inhibit-startup t))
2422 (org-mode)
2423 (show-all)
2424 (unless keepp (funcall binding arg))))
2425 (if (not keepp)
2426 (kill-buffer buffer)
2427 (switch-to-buffer-other-window buffer)
2428 (goto-char (point-min)))))
2430 (defun org-find-visible ()
2431 (let ((s (point)))
2432 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
2433 (get-char-property s 'invisible)))
2435 (defun org-find-invisible ()
2436 (let ((s (point)))
2437 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
2438 (not (get-char-property s 'invisible))))
2441 (defvar org-export-htmlized-org-css-url) ;; defined in org-html.el
2443 ;;;###autoload
2444 (defun org-export-as-org (arg &optional hidden ext-plist
2445 to-buffer body-only pub-dir)
2446 "Make a copy with not-exporting stuff removed.
2447 The purpose of this function is to provide a way to export the source
2448 Org file of a webpage in Org format, but with sensitive and/or irrelevant
2449 stuff removed. This command will remove the following:
2451 - archived trees (if the variable `org-export-with-archived-trees' is nil)
2452 - comment blocks and trees starting with the COMMENT keyword
2453 - only trees that are consistent with `org-export-select-tags'
2454 and `org-export-exclude-tags'.
2456 The only arguments that will be used are EXT-PLIST and PUB-DIR,
2457 all the others will be ignored (but are present so that the general
2458 mechanism to call publishing functions will work).
2460 EXT-PLIST is a property list with external parameters overriding
2461 org-mode's default settings, but still inferior to file-local
2462 settings. When PUB-DIR is set, use this as the publishing
2463 directory."
2464 (interactive "P")
2465 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
2466 ext-plist
2467 (org-infile-export-plist)))
2468 (bfname (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
2469 (filename (concat (file-name-as-directory
2470 (or pub-dir
2471 (org-export-directory :org opt-plist)))
2472 (file-name-sans-extension
2473 (file-name-nondirectory bfname))
2474 ".org"))
2475 (filename (and filename
2476 (if (equal (file-truename filename)
2477 (file-truename bfname))
2478 (concat (file-name-sans-extension filename)
2479 "-source."
2480 (file-name-extension filename))
2481 filename)))
2482 (backup-inhibited t)
2483 (buffer (find-file-noselect filename))
2484 (region (buffer-string)))
2485 (save-excursion
2486 (switch-to-buffer buffer)
2487 (erase-buffer)
2488 (insert region)
2489 (let ((org-inhibit-startup t)) (org-mode))
2490 (org-install-letbind)
2492 ;; Get rid of archived trees
2493 (org-export-remove-archived-trees (plist-get opt-plist :archived-trees))
2495 ;; Remove comment environment and comment subtrees
2496 (org-export-remove-comment-blocks-and-subtrees)
2498 ;; Get rid of excluded trees
2499 (org-export-handle-export-tags (plist-get opt-plist :select-tags)
2500 (plist-get opt-plist :exclude-tags))
2502 (when (or (plist-get opt-plist :plain-source)
2503 (not (or (plist-get opt-plist :plain-source)
2504 (plist-get opt-plist :htmlized-source))))
2505 ;; Either nothing special is requested (default call)
2506 ;; or the plain source is explicitly requested
2507 ;; so: save it
2508 (save-buffer))
2509 (when (plist-get opt-plist :htmlized-source)
2510 ;; Make the htmlized version
2511 (require 'htmlize)
2512 (require 'org-html)
2513 (font-lock-fontify-buffer)
2514 (let* ((htmlize-output-type 'css)
2515 (newbuf (htmlize-buffer)))
2516 (with-current-buffer newbuf
2517 (when org-export-htmlized-org-css-url
2518 (goto-char (point-min))
2519 (and (re-search-forward
2520 "<style type=\"text/css\">[^\000]*?\n[ \t]*</style>.*"
2521 nil t)
2522 (replace-match
2523 (format
2524 "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\">"
2525 org-export-htmlized-org-css-url)
2526 t t)))
2527 (write-file (concat filename ".html")))
2528 (kill-buffer newbuf)))
2529 (set-buffer-modified-p nil)
2530 (kill-buffer (current-buffer)))))
2532 (defvar org-archive-location) ;; gets loaded with the org-archive require.
2533 (defun org-get-current-options ()
2534 "Return a string with current options as keyword options.
2535 Does include HTML export options as well as TODO and CATEGORY stuff."
2536 (require 'org-archive)
2537 (format
2538 "#+TITLE: %s
2539 #+AUTHOR: %s
2540 #+EMAIL: %s
2541 #+DATE: %s
2542 #+DESCRIPTION:
2543 #+KEYWORDS:
2544 #+LANGUAGE: %s
2545 #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s -:%s f:%s *:%s <:%s
2546 #+OPTIONS: TeX:%s LaTeX:%s skip:%s d:%s todo:%s pri:%s tags:%s
2548 #+EXPORT_SELECT_TAGS: %s
2549 #+EXPORT_EXCLUDE_TAGS: %s
2550 #+LINK_UP: %s
2551 #+LINK_HOME: %s
2552 #+CATEGORY: %s
2553 #+SEQ_TODO: %s
2554 #+TYP_TODO: %s
2555 #+PRIORITIES: %c %c %c
2556 #+DRAWERS: %s
2557 #+STARTUP: %s %s %s %s %s
2558 #+TAGS: %s
2559 #+FILETAGS: %s
2560 #+ARCHIVE: %s
2561 #+LINK: %s
2563 (buffer-name) (user-full-name) user-mail-address
2564 (format-time-string (substring (car org-time-stamp-formats) 1 -1))
2565 org-export-default-language
2566 org-export-headline-levels
2567 org-export-with-section-numbers
2568 org-export-with-toc
2569 org-export-preserve-breaks
2570 org-export-html-expand
2571 org-export-with-fixed-width
2572 org-export-with-tables
2573 org-export-with-sub-superscripts
2574 org-export-with-special-strings
2575 org-export-with-footnotes
2576 org-export-with-emphasize
2577 org-export-with-timestamps
2578 org-export-with-TeX-macros
2579 org-export-with-LaTeX-fragments
2580 org-export-skip-text-before-1st-heading
2581 org-export-with-drawers
2582 org-export-with-todo-keywords
2583 org-export-with-priority
2584 org-export-with-tags
2585 (if (featurep 'org-jsinfo) (org-infojs-options-inbuffer-template) "")
2586 (mapconcat 'identity org-export-select-tags " ")
2587 (mapconcat 'identity org-export-exclude-tags " ")
2588 org-export-html-link-up
2589 org-export-html-link-home
2590 (or (ignore-errors
2591 (file-name-sans-extension
2592 (file-name-nondirectory (buffer-file-name (buffer-base-buffer)))))
2593 "NOFILENAME")
2594 "TODO FEEDBACK VERIFY DONE"
2595 "Me Jason Marie DONE"
2596 org-highest-priority org-lowest-priority org-default-priority
2597 (mapconcat 'identity org-drawers " ")
2598 (cdr (assoc org-startup-folded
2599 '((nil . "showall") (t . "overview") (content . "content"))))
2600 (if org-odd-levels-only "odd" "oddeven")
2601 (if org-hide-leading-stars "hidestars" "showstars")
2602 (if org-startup-align-all-tables "align" "noalign")
2603 (cond ((eq org-log-done t) "logdone")
2604 ((equal org-log-done 'note) "lognotedone")
2605 ((not org-log-done) "nologdone"))
2606 (or (mapconcat (lambda (x)
2607 (cond
2608 ((equal :startgroup (car x)) "{")
2609 ((equal :endgroup (car x)) "}")
2610 ((equal :newline (car x)) "")
2611 ((cdr x) (format "%s(%c)" (car x) (cdr x)))
2612 (t (car x))))
2613 (or org-tag-alist (org-get-buffer-tags)) " ") "")
2614 (mapconcat 'identity org-file-tags " ")
2615 org-archive-location
2616 "org file:~/org/%s.org"
2619 ;;;###autoload
2620 (defun org-insert-export-options-template ()
2621 "Insert into the buffer a template with information for exporting."
2622 (interactive)
2623 (if (not (bolp)) (newline))
2624 (let ((s (org-get-current-options)))
2625 (and (string-match "#\\+CATEGORY" s)
2626 (setq s (substring s 0 (match-beginning 0))))
2627 (insert s)))
2629 (defvar org-table-colgroup-info nil)
2631 (defun org-table-clean-before-export (lines &optional maybe-quoted)
2632 "Check if the table has a marking column.
2633 If yes remove the column and the special lines."
2634 (setq org-table-colgroup-info nil)
2635 (if (memq nil
2636 (mapcar
2637 (lambda (x) (or (string-match "^[ \t]*|-" x)
2638 (string-match
2639 (if maybe-quoted
2640 "^[ \t]*| *\\\\?\\([\#!$*_^ /]\\) *|"
2641 "^[ \t]*| *\\([\#!$*_^ /]\\) *|")
2642 x)))
2643 lines))
2644 (progn
2645 (setq org-table-clean-did-remove-column nil)
2646 (delq nil
2647 (mapcar
2648 (lambda (x)
2649 (cond
2650 ((string-match "^[ \t]*| */ *|" x)
2651 (setq org-table-colgroup-info
2652 (mapcar (lambda (x)
2653 (cond ((member x '("<" "&lt;")) :start)
2654 ((member x '(">" "&gt;")) :end)
2655 ((member x '("<>" "&lt;&gt;")) :startend)
2656 (t nil)))
2657 (org-split-string x "[ \t]*|[ \t]*")))
2658 nil)
2659 (t x)))
2660 lines)))
2661 (setq org-table-clean-did-remove-column t)
2662 (delq nil
2663 (mapcar
2664 (lambda (x)
2665 (cond
2666 ((string-match "^[ \t]*| */ *|" x)
2667 (setq org-table-colgroup-info
2668 (mapcar (lambda (x)
2669 (cond ((member x '("<" "&lt;")) :start)
2670 ((member x '(">" "&gt;")) :end)
2671 ((member x '("<>" "&lt;&gt;")) :startend)
2672 (t nil)))
2673 (cdr (org-split-string x "[ \t]*|[ \t]*"))))
2674 nil)
2675 ((string-match "^[ \t]*| *[!_^/] *|" x)
2676 nil) ; ignore this line
2677 ((or (string-match "^\\([ \t]*\\)|-+\\+" x)
2678 (string-match "^\\([ \t]*\\)|[^|]*|" x))
2679 ;; remove the first column
2680 (replace-match "\\1|" t nil x))))
2681 lines))))
2683 (defun org-export-cleanup-toc-line (s)
2684 "Remove tags and timestamps from lines going into the toc."
2685 (when (memq org-export-with-tags '(not-in-toc nil))
2686 (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s)
2687 (setq s (replace-match "" t t s))))
2688 (when org-export-remove-timestamps-from-toc
2689 (while (string-match org-maybe-keyword-time-regexp s)
2690 (setq s (replace-match "" t t s))))
2691 (while (string-match org-bracket-link-regexp s)
2692 (setq s (replace-match (match-string (if (match-end 3) 3 1) s)
2693 t t s)))
2694 (while (string-match "\\[\\([0-9]\\|fn:[^]]*\\)\\]" s)
2695 (setq s (replace-match "" t t s)))
2698 (defun org-create-multibrace-regexp (left right n)
2699 "Create a regular expression which will match a balanced sexp.
2700 Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
2701 as single character strings.
2702 The regexp returned will match the entire expression including the
2703 delimiters. It will also define a single group which contains the
2704 match except for the outermost delimiters. The maximum depth of
2705 stacked delimiters is N. Escaping delimiters is not possible."
2706 (let* ((nothing (concat "[^" left right "]*?"))
2707 (or "\\|")
2708 (re nothing)
2709 (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
2710 (while (> n 1)
2711 (setq n (1- n)
2712 re (concat re or next)
2713 next (concat "\\(?:" nothing left next right "\\)+" nothing)))
2714 (concat left "\\(" re "\\)" right)))
2716 (defvar org-match-substring-regexp
2717 (concat
2718 "\\([^\\]\\)\\([_^]\\)\\("
2719 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
2720 "\\|"
2721 "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
2722 "\\|"
2723 "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
2724 "The regular expression matching a sub- or superscript.")
2726 (defvar org-match-substring-with-braces-regexp
2727 (concat
2728 "\\([^\\]\\)\\([_^]\\)\\("
2729 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
2730 "\\)")
2731 "The regular expression matching a sub- or superscript, forcing braces.")
2734 (defun org-get-text-property-any (pos prop &optional object)
2735 (or (get-text-property pos prop object)
2736 (and (setq pos (next-single-property-change pos prop object))
2737 (get-text-property pos prop object))))
2739 (defun org-export-get-coderef-format (path desc)
2740 (save-match-data
2741 (if (and desc (string-match
2742 (regexp-quote (concat "(" path ")"))
2743 desc))
2744 (replace-match "%s" t t desc)
2745 (or desc "%s"))))
2747 (defun org-export-push-to-kill-ring (format)
2748 "Push buffer content to kill ring.
2749 The depends on the variable `org-export-copy-to-kill'."
2750 (when org-export-copy-to-kill-ring
2751 (org-kill-new (buffer-string))
2752 (when (fboundp 'x-set-selection)
2753 (ignore-errors (x-set-selection 'PRIMARY (buffer-string)))
2754 (ignore-errors (x-set-selection 'CLIPBOARD (buffer-string))))
2755 (message "%s export done, pushed to kill ring and clipboard" format)))
2757 (provide 'org-exp)
2759 ;; arch-tag: 65985fe9-095c-49c7-a7b6-cb4ee15c0a95
2761 ;;; org-exp.el ends here