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