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