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