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