Merge stuff from upsteam ruby-mode, part 1.
[emacs.git] / lisp / org / org-odt.el
blobca43d05bdbdf63d92ae475e043b34ca3b7862be7
1 ;;; org-odt.el --- OpenDocument Text exporter for Org-mode
3 ;; Copyright (C) 2010-2012 Free Software Foundation, Inc.
5 ;; Author: Jambunathan K <kjambunathan at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;;; Code:
27 (eval-when-compile
28 (require 'cl))
29 (require 'org-lparse)
31 (defgroup org-export-odt nil
32 "Options specific for ODT export of Org-mode files."
33 :tag "Org Export ODT"
34 :group 'org-export
35 :version "24.1")
37 (defvar org-lparse-dyn-first-heading-pos) ; let bound during org-do-lparse
38 (defun org-odt-insert-toc ()
39 (goto-char (point-min))
40 (cond
41 ((re-search-forward
42 "\\(<text:p [^>]*>\\)?\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*\\(</text:p>\\)?"
43 nil t)
44 (replace-match ""))
46 (goto-char org-lparse-dyn-first-heading-pos)))
47 (insert (org-odt-format-toc)))
49 (defun org-odt-end-export ()
50 (org-odt-insert-toc)
51 (org-odt-fixup-label-references)
53 ;; remove empty paragraphs
54 (goto-char (point-min))
55 (while (re-search-forward
56 "<text:p\\( text:style-name=\"Text_20_body\"\\)?>[ \r\n\t]*</text:p>"
57 nil t)
58 (replace-match ""))
59 (goto-char (point-min))
61 ;; Convert whitespace place holders
62 (goto-char (point-min))
63 (let (beg end n)
64 (while (setq beg (next-single-property-change (point) 'org-whitespace))
65 (setq n (get-text-property beg 'org-whitespace)
66 end (next-single-property-change beg 'org-whitespace))
67 (goto-char beg)
68 (delete-region beg end)
69 (insert (format "<span style=\"visibility:hidden;\">%s</span>"
70 (make-string n ?x)))))
72 ;; Remove empty lines at the beginning of the file.
73 (goto-char (point-min))
74 (when (looking-at "\\s-+\n") (replace-match ""))
76 ;; Remove display properties
77 (remove-text-properties (point-min) (point-max) '(display t)))
79 (defvar org-odt-suppress-xref nil)
80 (defconst org-export-odt-special-string-regexps
81 '(("\\\\-" . "&#x00ad;\\1") ; shy
82 ("---\\([^-]\\)" . "&#x2014;\\1") ; mdash
83 ("--\\([^-]\\)" . "&#x2013;\\1") ; ndash
84 ("\\.\\.\\." . "&#x2026;")) ; hellip
85 "Regular expressions for special string conversion.")
87 (defconst org-odt-lib-dir (file-name-directory load-file-name)
88 "Location of ODT exporter.
89 Use this to infer values of `org-odt-styles-dir' and
90 `org-export-odt-schema-dir'.")
92 (defvar org-odt-data-dir nil
93 "Data directory for ODT exporter.
94 Use this to infer values of `org-odt-styles-dir' and
95 `org-export-odt-schema-dir'.")
97 (defconst org-odt-schema-dir-list
98 (list
99 (and org-odt-data-dir
100 (expand-file-name "./schema/" org-odt-data-dir)) ; bail out
101 (eval-when-compile
102 (and (boundp 'org-odt-data-dir) org-odt-data-dir ; see make install
103 (expand-file-name "./schema/" org-odt-data-dir)))
104 (expand-file-name "../contrib/odt/etc/schema/" org-odt-lib-dir) ; git
106 "List of directories to search for OpenDocument schema files.
107 Use this list to set the default value of
108 `org-export-odt-schema-dir'. The entries in this list are
109 populated heuristically based on the values of `org-odt-lib-dir'
110 and `org-odt-data-dir'.")
112 (defcustom org-export-odt-schema-dir
113 (let* ((schema-dir
114 (catch 'schema-dir
115 (message "Debug (org-odt): Searching for OpenDocument schema files...")
116 (mapc
117 (lambda (schema-dir)
118 (when schema-dir
119 (message "Debug (org-odt): Trying %s..." schema-dir)
120 (when (and (file-readable-p
121 (expand-file-name "od-manifest-schema-v1.2-cs01.rnc"
122 schema-dir))
123 (file-readable-p
124 (expand-file-name "od-schema-v1.2-cs01.rnc"
125 schema-dir))
126 (file-readable-p
127 (expand-file-name "schemas.xml" schema-dir)))
128 (message "Debug (org-odt): Using schema files under %s"
129 schema-dir)
130 (throw 'schema-dir schema-dir))))
131 org-odt-schema-dir-list)
132 (message "Debug (org-odt): No OpenDocument schema files installed")
133 nil)))
134 schema-dir)
135 "Directory that contains OpenDocument schema files.
137 This directory contains:
138 1. rnc files for OpenDocument schema
139 2. a \"schemas.xml\" file that specifies locating rules needed
140 for auto validation of OpenDocument XML files.
142 Use the customize interface to set this variable. This ensures
143 that `rng-schema-locating-files' is updated and auto-validation
144 of OpenDocument XML takes place based on the value
145 `rng-nxml-auto-validate-flag'.
147 The default value of this variable varies depending on the
148 version of org in use and is initialized from
149 `org-odt-schema-dir-list'. The OASIS schema files are available
150 only in the org's private git repository. It is *not* bundled
151 with GNU ELPA tar or standard Emacs distribution."
152 :type '(choice
153 (const :tag "Not set" nil)
154 (directory :tag "Schema directory"))
155 :group 'org-export-odt
156 :version "24.1"
157 :set
158 (lambda (var value)
159 "Set `org-export-odt-schema-dir'.
160 Also add it to `rng-schema-locating-files'."
161 (let ((schema-dir value))
162 (set var
163 (if (and
164 (file-readable-p
165 (expand-file-name "od-manifest-schema-v1.2-cs01.rnc" schema-dir))
166 (file-readable-p
167 (expand-file-name "od-schema-v1.2-cs01.rnc" schema-dir))
168 (file-readable-p
169 (expand-file-name "schemas.xml" schema-dir)))
170 schema-dir
171 (when value
172 (message "Error (org-odt): %s has no OpenDocument schema files"
173 value))
174 nil)))
175 (when org-export-odt-schema-dir
176 (eval-after-load 'rng-loc
177 '(add-to-list 'rng-schema-locating-files
178 (expand-file-name "schemas.xml"
179 org-export-odt-schema-dir))))))
181 (defconst org-odt-styles-dir-list
182 (list
183 (and org-odt-data-dir
184 (expand-file-name "./styles/" org-odt-data-dir)) ; bail out
185 (eval-when-compile
186 (and (boundp 'org-odt-data-dir) org-odt-data-dir ; see make install
187 (expand-file-name "./styles/" org-odt-data-dir)))
188 (expand-file-name "../etc/styles/" org-odt-lib-dir) ; git
189 (expand-file-name "./etc/styles/" org-odt-lib-dir) ; elpa
190 (expand-file-name "./org/" data-directory) ; system
192 "List of directories to search for OpenDocument styles files.
193 See `org-odt-styles-dir'. The entries in this list are populated
194 heuristically based on the values of `org-odt-lib-dir' and
195 `org-odt-data-dir'.")
197 (defconst org-odt-styles-dir
198 (let* ((styles-dir
199 (catch 'styles-dir
200 (message "Debug (org-odt): Searching for OpenDocument styles files...")
201 (mapc (lambda (styles-dir)
202 (when styles-dir
203 (message "Debug (org-odt): Trying %s..." styles-dir)
204 (when (and (file-readable-p
205 (expand-file-name
206 "OrgOdtContentTemplate.xml" styles-dir))
207 (file-readable-p
208 (expand-file-name
209 "OrgOdtStyles.xml" styles-dir)))
210 (message "Debug (org-odt): Using styles under %s"
211 styles-dir)
212 (throw 'styles-dir styles-dir))))
213 org-odt-styles-dir-list)
214 nil)))
215 (unless styles-dir
216 (error "Error (org-odt): Cannot find factory styles files. Aborting."))
217 styles-dir)
218 "Directory that holds auxiliary XML files used by the ODT exporter.
220 This directory contains the following XML files -
221 \"OrgOdtStyles.xml\" and \"OrgOdtContentTemplate.xml\". These
222 XML files are used as the default values of
223 `org-export-odt-styles-file' and
224 `org-export-odt-content-template-file'.
226 The default value of this variable varies depending on the
227 version of org in use and is initialized from
228 `org-odt-styles-dir-list'. Note that the user could be using org
229 from one of: org's own private git repository, GNU ELPA tar or
230 standard Emacs.")
232 (defvar org-odt-file-extensions
233 '(("odt" . "OpenDocument Text")
234 ("ott" . "OpenDocument Text Template")
235 ("odm" . "OpenDocument Master Document")
236 ("ods" . "OpenDocument Spreadsheet")
237 ("ots" . "OpenDocument Spreadsheet Template")
238 ("odg" . "OpenDocument Drawing (Graphics)")
239 ("otg" . "OpenDocument Drawing Template")
240 ("odp" . "OpenDocument Presentation")
241 ("otp" . "OpenDocument Presentation Template")
242 ("odi" . "OpenDocument Image")
243 ("odf" . "OpenDocument Formula")
244 ("odc" . "OpenDocument Chart")))
246 (mapc
247 (lambda (desc)
248 ;; Let Org open all OpenDocument files using system-registered app
249 (add-to-list 'org-file-apps
250 (cons (concat "\\." (car desc) "\\'") 'system))
251 ;; Let Emacs open all OpenDocument files in archive mode
252 (add-to-list 'auto-mode-alist
253 (cons (concat "\\." (car desc) "\\'") 'archive-mode)))
254 org-odt-file-extensions)
256 ;; register the odt exporter with the pre-processor
257 (add-to-list 'org-export-backends 'odt)
259 ;; register the odt exporter with org-lparse library
260 (org-lparse-register-backend 'odt)
262 (defun org-odt-unload-function ()
263 (org-lparse-unregister-backend 'odt)
264 (remove-hook 'org-export-preprocess-after-blockquote-hook
265 'org-export-odt-preprocess-latex-fragments)
266 nil)
268 (defcustom org-export-odt-content-template-file nil
269 "Template file for \"content.xml\".
270 The exporter embeds the exported content just before
271 \"</office:text>\" element.
273 If unspecified, the file named \"OrgOdtContentTemplate.xml\"
274 under `org-odt-styles-dir' is used."
275 :type 'file
276 :group 'org-export-odt
277 :version "24.1")
279 (defcustom org-export-odt-styles-file nil
280 "Default styles file for use with ODT export.
281 Valid values are one of:
282 1. nil
283 2. path to a styles.xml file
284 3. path to a *.odt or a *.ott file
285 4. list of the form (ODT-OR-OTT-FILE (FILE-MEMBER-1 FILE-MEMBER-2
286 ...))
288 In case of option 1, an in-built styles.xml is used. See
289 `org-odt-styles-dir' for more information.
291 In case of option 3, the specified file is unzipped and the
292 styles.xml embedded therein is used.
294 In case of option 4, the specified ODT-OR-OTT-FILE is unzipped
295 and FILE-MEMBER-1, FILE-MEMBER-2 etc are copied in to the
296 generated odt file. Use relative path for specifying the
297 FILE-MEMBERS. styles.xml must be specified as one of the
298 FILE-MEMBERS.
300 Use options 1, 2 or 3 only if styles.xml alone suffices for
301 achieving the desired formatting. Use option 4, if the styles.xml
302 references additional files like header and footer images for
303 achieving the desired formatting.
305 Use \"#+ODT_STYLES_FILE: ...\" directive to set this variable on
306 a per-file basis. For example,
308 #+ODT_STYLES_FILE: \"/path/to/styles.xml\" or
309 #+ODT_STYLES_FILE: (\"/path/to/file.ott\" (\"styles.xml\" \"image/hdr.png\"))."
310 :group 'org-export-odt
311 :version "24.1"
312 :type
313 '(choice
314 (const :tag "Factory settings" nil)
315 (file :must-match t :tag "styles.xml")
316 (file :must-match t :tag "ODT or OTT file")
317 (list :tag "ODT or OTT file + Members"
318 (file :must-match t :tag "ODF Text or Text Template file")
319 (cons :tag "Members"
320 (file :tag " Member" "styles.xml")
321 (repeat (file :tag "Member"))))))
323 (eval-after-load 'org-exp
324 '(add-to-list 'org-export-inbuffer-options-extra
325 '("ODT_STYLES_FILE" :odt-styles-file)))
327 (defconst org-export-odt-tmpdir-prefix "%s-")
328 (defconst org-export-odt-bookmark-prefix "OrgXref.")
330 (defvar org-export-odt-embed-images t
331 "Should the images be copied in to the odt file or just linked?")
333 (defvar org-export-odt-inline-images 'maybe)
334 (defcustom org-export-odt-inline-image-extensions
335 '("png" "jpeg" "jpg" "gif")
336 "Extensions of image files that can be inlined into HTML."
337 :type '(repeat (string :tag "Extension"))
338 :group 'org-export-odt
339 :version "24.1")
341 (defcustom org-export-odt-pixels-per-inch display-pixels-per-inch
342 "Scaling factor for converting images pixels to inches.
343 Use this for sizing of embedded images. See Info node `(org)
344 Images in ODT export' for more information."
345 :type 'float
346 :group 'org-export-odt
347 :version "24.1")
349 (defcustom org-export-odt-create-custom-styles-for-srcblocks t
350 "Whether custom styles for colorized source blocks be automatically created.
351 When this option is turned on, the exporter creates custom styles
352 for source blocks based on the advice of `htmlfontify'. Creation
353 of custom styles happen as part of `org-odt-hfy-face-to-css'.
355 When this option is turned off exporter does not create such
356 styles.
358 Use the latter option if you do not want the custom styles to be
359 based on your current display settings. It is necessary that the
360 styles.xml already contains needed styles for colorizing to work.
362 This variable is effective only if
363 `org-export-odt-fontify-srcblocks' is turned on."
364 :group 'org-export-odt
365 :version "24.1"
366 :type 'boolean)
368 (defvar org-export-odt-default-org-styles-alist
369 '((paragraph . ((default . "Text_20_body")
370 (fixedwidth . "OrgFixedWidthBlock")
371 (verse . "OrgVerse")
372 (quote . "Quotations")
373 (blockquote . "Quotations")
374 (center . "OrgCenter")
375 (left . "OrgLeft")
376 (right . "OrgRight")
377 (title . "OrgTitle")
378 (subtitle . "OrgSubtitle")
379 (footnote . "Footnote")
380 (src . "OrgSrcBlock")
381 (illustration . "Illustration")
382 (table . "Table")
383 (definition-term . "Text_20_body_20_bold")
384 (horizontal-line . "Horizontal_20_Line")))
385 (character . ((bold . "Bold")
386 (emphasis . "Emphasis")
387 (code . "OrgCode")
388 (verbatim . "OrgCode")
389 (strike . "Strikethrough")
390 (underline . "Underline")
391 (subscript . "OrgSubscript")
392 (superscript . "OrgSuperscript")))
393 (list . ((ordered . "OrgNumberedList")
394 (unordered . "OrgBulletedList")
395 (description . "OrgDescriptionList"))))
396 "Default styles for various entities.")
398 (defvar org-export-odt-org-styles-alist org-export-odt-default-org-styles-alist)
399 (defun org-odt-get-style-name-for-entity (category &optional entity)
400 (let ((entity (or entity 'default)))
402 (cdr (assoc entity (cdr (assoc category
403 org-export-odt-org-styles-alist))))
404 (cdr (assoc entity (cdr (assoc category
405 org-export-odt-default-org-styles-alist))))
406 (error "Cannot determine style name for entity %s of type %s"
407 entity category))))
409 (defcustom org-export-odt-preferred-output-format nil
410 "Automatically post-process to this format after exporting to \"odt\".
411 Interactive commands `org-export-as-odt' and
412 `org-export-as-odt-and-open' export first to \"odt\" format and
413 then use `org-export-odt-convert-process' to convert the
414 resulting document to this format. During customization of this
415 variable, the list of valid values are populated based on
416 `org-export-odt-convert-capabilities'."
417 :group 'org-export-odt
418 :version "24.1"
419 :type '(choice :convert-widget
420 (lambda (w)
421 (apply 'widget-convert (widget-type w)
422 (eval (car (widget-get w :args)))))
423 `((const :tag "None" nil)
424 ,@(mapcar (lambda (c)
425 `(const :tag ,c ,c))
426 (org-lparse-reachable-formats "odt")))))
428 ;;;###autoload
429 (defun org-export-as-odt-and-open (arg)
430 "Export the outline as ODT and immediately open it with a browser.
431 If there is an active region, export only the region.
432 The prefix ARG specifies how many levels of the outline should become
433 headlines. The default is 3. Lower levels will become bulleted lists."
434 (interactive "P")
435 (org-lparse-and-open
436 (or org-export-odt-preferred-output-format "odt") "odt" arg))
438 ;;;###autoload
439 (defun org-export-as-odt-batch ()
440 "Call the function `org-lparse-batch'.
441 This function can be used in batch processing as:
442 emacs --batch
443 --load=$HOME/lib/emacs/org.el
444 --eval \"(setq org-export-headline-levels 2)\"
445 --visit=MyFile --funcall org-export-as-odt-batch"
446 (org-lparse-batch "odt"))
448 ;;; org-export-as-odt
449 ;;;###autoload
450 (defun org-export-as-odt (arg &optional hidden ext-plist
451 to-buffer body-only pub-dir)
452 "Export the outline as a OpenDocumentText file.
453 If there is an active region, export only the region. The prefix
454 ARG specifies how many levels of the outline should become
455 headlines. The default is 3. Lower levels will become bulleted
456 lists. HIDDEN is obsolete and does nothing.
457 EXT-PLIST is a property list with external parameters overriding
458 org-mode's default settings, but still inferior to file-local
459 settings. When TO-BUFFER is non-nil, create a buffer with that
460 name and export to that buffer. If TO-BUFFER is the symbol
461 `string', don't leave any buffer behind but just return the
462 resulting XML as a string. When BODY-ONLY is set, don't produce
463 the file header and footer, simply return the content of
464 <body>...</body>, without even the body tags themselves. When
465 PUB-DIR is set, use this as the publishing directory."
466 (interactive "P")
467 (org-lparse (or org-export-odt-preferred-output-format "odt")
468 "odt" arg hidden ext-plist to-buffer body-only pub-dir))
470 (defvar org-odt-entity-control-callbacks-alist
471 `((EXPORT
472 . (org-odt-begin-export org-odt-end-export))
473 (DOCUMENT-CONTENT
474 . (org-odt-begin-document-content org-odt-end-document-content))
475 (DOCUMENT-BODY
476 . (org-odt-begin-document-body org-odt-end-document-body))
477 (TOC
478 . (org-odt-begin-toc org-odt-end-toc))
479 (ENVIRONMENT
480 . (org-odt-begin-environment org-odt-end-environment))
481 (FOOTNOTE-DEFINITION
482 . (org-odt-begin-footnote-definition org-odt-end-footnote-definition))
483 (TABLE
484 . (org-odt-begin-table org-odt-end-table))
485 (TABLE-ROWGROUP
486 . (org-odt-begin-table-rowgroup org-odt-end-table-rowgroup))
487 (LIST
488 . (org-odt-begin-list org-odt-end-list))
489 (LIST-ITEM
490 . (org-odt-begin-list-item org-odt-end-list-item))
491 (OUTLINE
492 . (org-odt-begin-outline org-odt-end-outline))
493 (OUTLINE-TEXT
494 . (org-odt-begin-outline-text org-odt-end-outline-text))
495 (PARAGRAPH
496 . (org-odt-begin-paragraph org-odt-end-paragraph)))
499 (defvar org-odt-entity-format-callbacks-alist
500 `((EXTRA-TARGETS . org-lparse-format-extra-targets)
501 (ORG-TAGS . org-lparse-format-org-tags)
502 (SECTION-NUMBER . org-lparse-format-section-number)
503 (HEADLINE . org-odt-format-headline)
504 (TOC-ENTRY . org-odt-format-toc-entry)
505 (TOC-ITEM . org-odt-format-toc-item)
506 (TAGS . org-odt-format-tags)
507 (SPACES . org-odt-format-spaces)
508 (TABS . org-odt-format-tabs)
509 (LINE-BREAK . org-odt-format-line-break)
510 (FONTIFY . org-odt-format-fontify)
511 (TODO . org-lparse-format-todo)
512 (LINK . org-odt-format-link)
513 (INLINE-IMAGE . org-odt-format-inline-image)
514 (ORG-LINK . org-odt-format-org-link)
515 (HEADING . org-odt-format-heading)
516 (ANCHOR . org-odt-format-anchor)
517 (TABLE . org-lparse-format-table)
518 (TABLE-ROW . org-odt-format-table-row)
519 (TABLE-CELL . org-odt-format-table-cell)
520 (FOOTNOTES-SECTION . ignore)
521 (FOOTNOTE-REFERENCE . org-odt-format-footnote-reference)
522 (HORIZONTAL-LINE . org-odt-format-horizontal-line)
523 (COMMENT . org-odt-format-comment)
524 (LINE . org-odt-format-line)
525 (ORG-ENTITY . org-odt-format-org-entity))
528 ;;;_. callbacks
529 ;;;_. control callbacks
530 ;;;_ , document body
531 (defun org-odt-begin-office-body ()
532 ;; automatic styles
533 (insert-file-contents
534 (or org-export-odt-content-template-file
535 (expand-file-name "OrgOdtContentTemplate.xml"
536 org-odt-styles-dir)))
537 (goto-char (point-min))
538 (re-search-forward "</office:text>" nil nil)
539 (delete-region (match-beginning 0) (point-max)))
541 ;; Following variable is let bound when `org-do-lparse' is in
542 ;; progress. See org-html.el.
543 (defvar org-lparse-toc)
544 (defun org-odt-format-toc ()
545 (if (not org-lparse-toc) "" (concat "\n" org-lparse-toc "\n")))
547 (defun org-odt-format-preamble (opt-plist)
548 (let* ((title (plist-get opt-plist :title))
549 (author (plist-get opt-plist :author))
550 (date (plist-get opt-plist :date))
551 (iso-date (org-odt-format-date date))
552 (date (org-odt-format-date date "%d %b %Y"))
553 (email (plist-get opt-plist :email))
554 ;; switch on or off above vars based on user settings
555 (author (and (plist-get opt-plist :author-info) (or author email)))
556 (email (and (plist-get opt-plist :email-info) email))
557 (date (and (plist-get opt-plist :time-stamp-file) date)))
558 (concat
559 ;; title
560 (when title
561 (concat
562 (org-odt-format-stylized-paragraph
563 'title (org-odt-format-tags
564 '("<text:title>" . "</text:title>") title))
565 ;; separator
566 "<text:p text:style-name=\"OrgTitle\"/>"))
567 (cond
568 ((and author (not email))
569 ;; author only
570 (concat
571 (org-odt-format-stylized-paragraph
572 'subtitle
573 (org-odt-format-tags
574 '("<text:initial-creator>" . "</text:initial-creator>")
575 author))
576 ;; separator
577 "<text:p text:style-name=\"OrgSubtitle\"/>"))
578 ((and author email)
579 ;; author and email
580 (concat
581 (org-odt-format-stylized-paragraph
582 'subtitle
583 (org-odt-format-link
584 (org-odt-format-tags
585 '("<text:initial-creator>" . "</text:initial-creator>")
586 author) (concat "mailto:" email)))
587 ;; separator
588 "<text:p text:style-name=\"OrgSubtitle\"/>")))
589 ;; date
590 (when date
591 (concat
592 (org-odt-format-stylized-paragraph
593 'subtitle
594 (org-odt-format-tags
595 '("<text:date style:data-style-name=\"%s\" text:date-value=\"%s\">"
596 . "</text:date>") date "N75" iso-date))
597 ;; separator
598 "<text:p text:style-name=\"OrgSubtitle\"/>")))))
600 (defun org-odt-begin-document-body (opt-plist)
601 (org-odt-begin-office-body)
602 (insert (org-odt-format-preamble opt-plist))
603 (setq org-lparse-dyn-first-heading-pos (point)))
605 (defvar org-lparse-body-only) ; let bound during org-do-lparse
606 (defvar org-lparse-to-buffer) ; let bound during org-do-lparse
607 (defun org-odt-end-document-body (opt-plist)
608 (unless org-lparse-body-only
609 (org-lparse-insert-tag "</office:text>")
610 (org-lparse-insert-tag "</office:body>")))
612 (defun org-odt-begin-document-content (opt-plist)
613 (ignore))
615 (defun org-odt-end-document-content ()
616 (org-lparse-insert-tag "</office:document-content>"))
618 (defun org-odt-begin-outline (level1 snumber title tags
619 target extra-targets class)
620 (org-lparse-insert
621 'HEADING (org-lparse-format
622 'HEADLINE title extra-targets tags snumber level1)
623 level1 target))
625 (defun org-odt-end-outline ()
626 (ignore))
628 (defun org-odt-begin-outline-text (level1 snumber class)
629 (ignore))
631 (defun org-odt-end-outline-text ()
632 (ignore))
634 (defun org-odt-begin-section (style &optional name)
635 (let ((default-name (car (org-odt-add-automatic-style "Section"))))
636 (org-lparse-insert-tag
637 "<text:section text:style-name=\"%s\" text:name=\"%s\">"
638 style (or name default-name))))
640 (defun org-odt-end-section ()
641 (org-lparse-insert-tag "</text:section>"))
643 (defun org-odt-begin-paragraph (&optional style)
644 (org-lparse-insert-tag
645 "<text:p%s>" (org-odt-get-extra-attrs-for-paragraph-style style)))
647 (defun org-odt-end-paragraph ()
648 (org-lparse-insert-tag "</text:p>"))
650 (defun org-odt-get-extra-attrs-for-paragraph-style (style)
651 (let (style-name)
652 (setq style-name
653 (cond
654 ((stringp style) style)
655 ((symbolp style) (org-odt-get-style-name-for-entity
656 'paragraph style))))
657 (unless style-name
658 (error "Don't know how to handle paragraph style %s" style))
659 (format " text:style-name=\"%s\"" style-name)))
661 (defun org-odt-format-stylized-paragraph (style text)
662 (org-odt-format-tags
663 '("<text:p%s>" . "</text:p>") text
664 (org-odt-get-extra-attrs-for-paragraph-style style)))
666 (defvar org-lparse-opt-plist) ; bound during org-do-lparse
667 (defun org-odt-format-author (&optional author)
668 (when (setq author (or author (plist-get org-lparse-opt-plist :author)))
669 (org-odt-format-tags '("<dc:creator>" . "</dc:creator>") author)))
671 (defun org-odt-format-date (&optional org-ts fmt)
672 (save-match-data
673 (let* ((time
674 (and (stringp org-ts)
675 (string-match org-ts-regexp0 org-ts)
676 (apply 'encode-time
677 (org-fix-decoded-time
678 (org-parse-time-string (match-string 0 org-ts) t)))))
679 date)
680 (cond
681 (fmt (format-time-string fmt time))
682 (t (setq date (format-time-string "%Y-%m-%dT%H:%M:%S%z" time))
683 (format "%s:%s" (substring date 0 -2) (substring date -2)))))))
685 (defun org-odt-begin-annotation (&optional author date)
686 (org-lparse-insert-tag "<office:annotation>")
687 (when (setq author (org-odt-format-author author))
688 (insert author))
689 (insert (org-odt-format-tags
690 '("<dc:date>" . "</dc:date>")
691 (org-odt-format-date
692 (or date (plist-get org-lparse-opt-plist :date)))))
693 (org-lparse-begin-paragraph))
695 (defun org-odt-end-annotation ()
696 (org-lparse-insert-tag "</office:annotation>"))
698 (defun org-odt-begin-environment (style env-options-plist)
699 (case style
700 (annotation
701 (org-lparse-stash-save-paragraph-state)
702 (org-odt-begin-annotation (plist-get env-options-plist 'author)
703 (plist-get env-options-plist 'date)))
704 ((blockquote verse center quote)
705 (org-lparse-begin-paragraph style)
706 (list))
707 ((fixedwidth native)
708 (org-lparse-end-paragraph)
709 (list))
710 (t (error "Unknown environment %s" style))))
712 (defun org-odt-end-environment (style env-options-plist)
713 (case style
714 (annotation
715 (org-lparse-end-paragraph)
716 (org-odt-end-annotation)
717 (org-lparse-stash-pop-paragraph-state))
718 ((blockquote verse center quote)
719 (org-lparse-end-paragraph)
720 (list))
721 ((fixedwidth native)
722 (org-lparse-begin-paragraph)
723 (list))
724 (t (error "Unknown environment %s" style))))
726 (defvar org-lparse-list-stack) ; dynamically bound in org-do-lparse
727 (defvar org-odt-list-stack-stashed)
728 (defun org-odt-begin-list (ltype)
729 (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
730 ltype))
731 (let* ((style-name (org-odt-get-style-name-for-entity 'list ltype))
732 (extra (concat (if (or org-lparse-list-table-p
733 (and (= 1 (length org-lparse-list-stack))
734 (null org-odt-list-stack-stashed)))
735 " text:continue-numbering=\"false\""
736 " text:continue-numbering=\"true\"")
737 (when style-name
738 (format " text:style-name=\"%s\"" style-name)))))
739 (case ltype
740 ((ordered unordered description)
741 (org-lparse-end-paragraph)
742 (org-lparse-insert-tag "<text:list%s>" extra))
743 (t (error "Unknown list type: %s" ltype)))))
745 (defun org-odt-end-list (ltype)
746 (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
747 ltype))
748 (if ltype
749 (org-lparse-insert-tag "</text:list>")
750 (error "Unknown list type: %s" ltype)))
752 (defun org-odt-begin-list-item (ltype &optional arg headline)
753 (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
754 ltype))
755 (case ltype
756 (ordered
757 (assert (not headline) t)
758 (let* ((counter arg) (extra ""))
759 (org-lparse-insert-tag (if (= (length org-lparse-list-stack)
760 (length org-odt-list-stack-stashed))
761 "<text:list-header>" "<text:list-item>"))
762 (org-lparse-begin-paragraph)))
763 (unordered
764 (let* ((id arg) (extra ""))
765 (org-lparse-insert-tag (if (= (length org-lparse-list-stack)
766 (length org-odt-list-stack-stashed))
767 "<text:list-header>" "<text:list-item>"))
768 (org-lparse-begin-paragraph)
769 (insert (if headline (org-odt-format-target headline id)
770 (org-odt-format-bookmark "" id)))))
771 (description
772 (assert (not headline) t)
773 (let ((term (or arg "(no term)")))
774 (insert
775 (org-odt-format-tags
776 '("<text:list-item>" . "</text:list-item>")
777 (org-odt-format-stylized-paragraph 'definition-term term)))
778 (org-lparse-begin-list-item 'unordered)
779 (org-lparse-begin-list 'description)
780 (org-lparse-begin-list-item 'unordered)))
781 (t (error "Unknown list type"))))
783 (defun org-odt-end-list-item (ltype)
784 (setq ltype (or (org-lparse-html-list-type-to-canonical-list-type ltype)
785 ltype))
786 (case ltype
787 ((ordered unordered)
788 (org-lparse-insert-tag (if (= (length org-lparse-list-stack)
789 (length org-odt-list-stack-stashed))
790 (prog1 "</text:list-header>"
791 (setq org-odt-list-stack-stashed nil))
792 "</text:list-item>")))
793 (description
794 (org-lparse-end-list-item-1)
795 (org-lparse-end-list 'description)
796 (org-lparse-end-list-item-1))
797 (t (error "Unknown list type"))))
799 (defun org-odt-discontinue-list ()
800 (let ((stashed-stack org-lparse-list-stack))
801 (loop for list-type in stashed-stack
802 do (org-lparse-end-list-item-1 list-type)
803 (org-lparse-end-list list-type))
804 (setq org-odt-list-stack-stashed stashed-stack)))
806 (defun org-odt-continue-list ()
807 (setq org-odt-list-stack-stashed (nreverse org-odt-list-stack-stashed))
808 (loop for list-type in org-odt-list-stack-stashed
809 do (org-lparse-begin-list list-type)
810 (org-lparse-begin-list-item list-type)))
812 ;; Following variables are let bound when table emission is in
813 ;; progress. See org-lparse.el.
814 (defvar org-lparse-table-begin-marker)
815 (defvar org-lparse-table-ncols)
816 (defvar org-lparse-table-rowgrp-open)
817 (defvar org-lparse-table-rownum)
818 (defvar org-lparse-table-cur-rowgrp-is-hdr)
819 (defvar org-lparse-table-is-styled)
820 (defvar org-lparse-table-rowgrp-info)
821 (defvar org-lparse-table-colalign-vector)
823 (defvar org-odt-table-style nil
824 "Table style specified by \"#+ATTR_ODT: <style-name>\" line.
825 This is set during `org-odt-begin-table'.")
827 (defvar org-odt-table-style-spec nil
828 "Entry for `org-odt-table-style' in `org-export-odt-table-styles'.")
830 (defcustom org-export-odt-table-styles
831 '(("OrgEquation" "OrgEquation"
832 ((use-first-column-styles . t)
833 (use-last-column-styles . t))))
834 "Specify how Table Styles should be derived from a Table Template.
835 This is a list where each element is of the
836 form (TABLE-STYLE-NAME TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS).
838 TABLE-STYLE-NAME is the style associated with the table through
839 `org-odt-table-style'.
841 TABLE-TEMPLATE-NAME is a set of - upto 9 - automatic
842 TABLE-CELL-STYLE-NAMEs and PARAGRAPH-STYLE-NAMEs (as defined
843 below) that is included in
844 `org-export-odt-content-template-file'.
846 TABLE-CELL-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
847 \"TableCell\"
848 PARAGRAPH-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
849 \"TableParagraph\"
850 TABLE-CELL-TYPE := \"FirstRow\" | \"LastColumn\" |
851 \"FirstRow\" | \"LastRow\" |
852 \"EvenRow\" | \"OddRow\" |
853 \"EvenColumn\" | \"OddColumn\" | \"\"
854 where \"+\" above denotes string concatenation.
856 TABLE-CELL-OPTIONS is an alist where each element is of the
857 form (TABLE-CELL-STYLE-SELECTOR . ON-OR-OFF).
858 TABLE-CELL-STYLE-SELECTOR := `use-first-row-styles' |
859 `use-last-row-styles' |
860 `use-first-column-styles' |
861 `use-last-column-styles' |
862 `use-banding-rows-styles' |
863 `use-banding-columns-styles' |
864 `use-first-row-styles'
865 ON-OR-OFF := `t' | `nil'
867 For example, with the following configuration
869 \(setq org-export-odt-table-styles
870 '\(\(\"TableWithHeaderRowsAndColumns\" \"Custom\"
871 \(\(use-first-row-styles . t\)
872 \(use-first-column-styles . t\)\)\)
873 \(\"TableWithHeaderColumns\" \"Custom\"
874 \(\(use-first-column-styles . t\)\)\)\)\)
876 1. A table associated with \"TableWithHeaderRowsAndColumns\"
877 style will use the following table-cell styles -
878 \"CustomFirstRowTableCell\", \"CustomFirstColumnTableCell\",
879 \"CustomTableCell\" and the following paragraph styles
880 \"CustomFirstRowTableParagraph\",
881 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
882 as appropriate.
884 2. A table associated with \"TableWithHeaderColumns\" style will
885 use the following table-cell styles -
886 \"CustomFirstColumnTableCell\", \"CustomTableCell\" and the
887 following paragraph styles
888 \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
889 as appropriate..
891 Note that TABLE-TEMPLATE-NAME corresponds to the
892 \"<table:table-template>\" elements contained within
893 \"<office:styles>\". The entries (TABLE-STYLE-NAME
894 TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS) correspond to
895 \"table:template-name\" and \"table:use-first-row-styles\" etc
896 attributes of \"<table:table>\" element. Refer ODF-1.2
897 specification for more information. Also consult the
898 implementation filed under `org-odt-get-table-cell-styles'.
900 The TABLE-STYLE-NAME \"OrgEquation\" is used internally for
901 formatting of numbered display equations. Do not delete this
902 style from the list."
903 :group 'org-export-odt
904 :version "24.1"
905 :type '(choice
906 (const :tag "None" nil)
907 (repeat :tag "Table Styles"
908 (list :tag "Table Style Specification"
909 (string :tag "Table Style Name")
910 (string :tag "Table Template Name")
911 (alist :options (use-first-row-styles
912 use-last-row-styles
913 use-first-column-styles
914 use-last-column-styles
915 use-banding-rows-styles
916 use-banding-columns-styles)
917 :key-type symbol
918 :value-type (const :tag "True" t))))))
920 (defvar org-odt-table-style-format
922 <style:style style:name=\"%s\" style:family=\"table\">
923 <style:table-properties style:rel-width=\"%d%%\" fo:margin-top=\"0cm\" fo:margin-bottom=\"0.20cm\" table:align=\"center\"/>
924 </style:style>
926 "Template for auto-generated Table styles.")
928 (defvar org-odt-automatic-styles '()
929 "Registry of automatic styles for various OBJECT-TYPEs.
930 The variable has the following form:
931 \(\(OBJECT-TYPE-A
932 \(\(OBJECT-NAME-A.1 OBJECT-PROPS-A.1\)
933 \(OBJECT-NAME-A.2 OBJECT-PROPS-A.2\) ...\)\)
934 \(OBJECT-TYPE-B
935 \(\(OBJECT-NAME-B.1 OBJECT-PROPS-B.1\)
936 \(OBJECT-NAME-B.2 OBJECT-PROPS-B.2\) ...\)\)
937 ...\).
939 OBJECT-TYPEs could be \"Section\", \"Table\", \"Figure\" etc.
940 OBJECT-PROPS is (typically) a plist created by passing
941 \"#+ATTR_ODT: \" option to `org-lparse-get-block-params'.
943 Use `org-odt-add-automatic-style' to add update this variable.'")
945 (defvar org-odt-object-counters nil
946 "Running counters for various OBJECT-TYPEs.
947 Use this to generate automatic names and style-names. See
948 `org-odt-add-automatic-style'.")
950 (defun org-odt-write-automatic-styles ()
951 "Write automatic styles to \"content.xml\"."
952 (with-current-buffer
953 (find-file-noselect (expand-file-name "content.xml") t)
954 ;; position the cursor
955 (goto-char (point-min))
956 (re-search-forward " </office:automatic-styles>" nil t)
957 (goto-char (match-beginning 0))
958 ;; write automatic table styles
959 (loop for (style-name props) in
960 (plist-get org-odt-automatic-styles 'Table) do
961 (when (setq props (or (plist-get props :rel-width) 96))
962 (insert (format org-odt-table-style-format style-name props))))))
964 (defun org-odt-add-automatic-style (object-type &optional object-props)
965 "Create an automatic style of type OBJECT-TYPE with param OBJECT-PROPS.
966 OBJECT-PROPS is (typically) a plist created by passing
967 \"#+ATTR_ODT: \" option of the object in question to
968 `org-lparse-get-block-params'.
970 Use `org-odt-object-counters' to generate an automatic
971 OBJECT-NAME and STYLE-NAME. If OBJECT-PROPS is non-nil, add a
972 new entry in `org-odt-automatic-styles'. Return (OBJECT-NAME
973 . STYLE-NAME)."
974 (assert (stringp object-type))
975 (let* ((object (intern object-type))
976 (seqvar object)
977 (seqno (1+ (or (plist-get org-odt-object-counters seqvar) 0)))
978 (object-name (format "%s%d" object-type seqno)) style-name)
979 (setq org-odt-object-counters
980 (plist-put org-odt-object-counters seqvar seqno))
981 (when object-props
982 (setq style-name (format "Org%s" object-name))
983 (setq org-odt-automatic-styles
984 (plist-put org-odt-automatic-styles object
985 (append (list (list style-name object-props))
986 (plist-get org-odt-automatic-styles object)))))
987 (cons object-name style-name)))
989 (defvar org-odt-table-indentedp nil)
990 (defun org-odt-begin-table (caption label attributes)
991 (setq org-odt-table-indentedp (not (null org-lparse-list-stack)))
992 (when org-odt-table-indentedp
993 ;; Within the Org file, the table is appearing within a list item.
994 ;; OpenDocument doesn't allow table to appear within list items.
995 ;; Temporarily terminate the list, emit the table and then
996 ;; re-continue the list.
997 (org-odt-discontinue-list)
998 ;; Put the Table in an indented section.
999 (let ((level (length org-odt-list-stack-stashed)))
1000 (org-odt-begin-section (format "OrgIndentedSection-Level-%d" level))))
1001 (setq attributes (org-lparse-get-block-params attributes))
1002 (setq org-odt-table-style (plist-get attributes :style))
1003 (setq org-odt-table-style-spec
1004 (assoc org-odt-table-style org-export-odt-table-styles))
1005 (when (or label caption)
1006 (insert
1007 (org-odt-format-stylized-paragraph
1008 'table (org-odt-format-entity-caption label caption "__Table__"))))
1009 (let ((name-and-style (org-odt-add-automatic-style "Table" attributes)))
1010 (org-lparse-insert-tag
1011 "<table:table table:name=\"%s\" table:style-name=\"%s\">"
1012 (car name-and-style) (or (nth 1 org-odt-table-style-spec)
1013 (cdr name-and-style) "OrgTable")))
1014 (setq org-lparse-table-begin-marker (point)))
1016 (defvar org-lparse-table-colalign-info)
1017 (defun org-odt-end-table ()
1018 (goto-char org-lparse-table-begin-marker)
1019 (loop for level from 0 below org-lparse-table-ncols
1020 do (let* ((col-cookie (and org-lparse-table-is-styled
1021 (cdr (assoc (1+ level)
1022 org-lparse-table-colalign-info))))
1023 (extra-columns (or (nth 1 col-cookie) 0)))
1024 (dotimes (i (1+ extra-columns))
1025 (insert
1026 (org-odt-format-tags
1027 "<table:table-column table:style-name=\"%sColumn\"/>"
1028 "" (or (nth 1 org-odt-table-style-spec) "OrgTable"))))
1029 (insert "\n")))
1030 ;; fill style attributes for table cells
1031 (when org-lparse-table-is-styled
1032 (while (re-search-forward "@@\\(table-cell:p\\|table-cell:style-name\\)@@\\([0-9]+\\)@@\\([0-9]+\\)@@" nil t)
1033 (let* ((spec (match-string 1))
1034 (r (string-to-number (match-string 2)))
1035 (c (string-to-number (match-string 3)))
1036 (cell-styles (org-odt-get-table-cell-styles
1037 r c org-odt-table-style-spec))
1038 (table-cell-style (car cell-styles))
1039 (table-cell-paragraph-style (cdr cell-styles)))
1040 (cond
1041 ((equal spec "table-cell:p")
1042 (replace-match table-cell-paragraph-style t t))
1043 ((equal spec "table-cell:style-name")
1044 (replace-match table-cell-style t t))))))
1045 (goto-char (point-max))
1046 (org-lparse-insert-tag "</table:table>")
1047 (when org-odt-table-indentedp
1048 (org-odt-end-section)
1049 (org-odt-continue-list)))
1051 (defun org-odt-begin-table-rowgroup (&optional is-header-row)
1052 (when org-lparse-table-rowgrp-open
1053 (org-lparse-end 'TABLE-ROWGROUP))
1054 (org-lparse-insert-tag (if is-header-row
1055 "<table:table-header-rows>"
1056 "<table:table-rows>"))
1057 (setq org-lparse-table-rowgrp-open t)
1058 (setq org-lparse-table-cur-rowgrp-is-hdr is-header-row))
1060 (defun org-odt-end-table-rowgroup ()
1061 (when org-lparse-table-rowgrp-open
1062 (setq org-lparse-table-rowgrp-open nil)
1063 (org-lparse-insert-tag
1064 (if org-lparse-table-cur-rowgrp-is-hdr
1065 "</table:table-header-rows>" "</table:table-rows>"))))
1067 (defun org-odt-format-table-row (row)
1068 (org-odt-format-tags
1069 '("<table:table-row>" . "</table:table-row>") row))
1071 (defun org-odt-get-table-cell-styles (r c &optional style-spec)
1072 "Retrieve styles applicable to a table cell.
1073 R and C are (zero-based) row and column numbers of the table
1074 cell. STYLE-SPEC is an entry in `org-export-odt-table-styles'
1075 applicable to the current table. It is `nil' if the table is not
1076 associated with any style attributes.
1078 Return a cons of (TABLE-CELL-STYLE-NAME . PARAGRAPH-STYLE-NAME).
1080 When STYLE-SPEC is nil, style the table cell the conventional way
1081 - choose cell borders based on row and column groupings and
1082 choose paragraph alignment based on `org-col-cookies' text
1083 property. See also
1084 `org-odt-get-paragraph-style-cookie-for-table-cell'.
1086 When STYLE-SPEC is non-nil, ignore the above cookie and return
1087 styles congruent with the ODF-1.2 specification."
1088 (cond
1089 (style-spec
1091 ;; LibreOffice - particularly the Writer - honors neither table
1092 ;; templates nor custom table-cell styles. Inorder to retain
1093 ;; inter-operability with LibreOffice, only automatic styles are
1094 ;; used for styling of table-cells. The current implementation is
1095 ;; congruent with ODF-1.2 specification and hence is
1096 ;; future-compatible.
1098 ;; Additional Note: LibreOffice's AutoFormat facility for tables -
1099 ;; which recognizes as many as 16 different cell types - is much
1100 ;; richer. Unfortunately it is NOT amenable to easy configuration
1101 ;; by hand.
1103 (let* ((template-name (nth 1 style-spec))
1104 (cell-style-selectors (nth 2 style-spec))
1105 (cell-type
1106 (cond
1107 ((and (cdr (assoc 'use-first-column-styles cell-style-selectors))
1108 (= c 0)) "FirstColumn")
1109 ((and (cdr (assoc 'use-last-column-styles cell-style-selectors))
1110 (= c (1- org-lparse-table-ncols))) "LastColumn")
1111 ((and (cdr (assoc 'use-first-row-styles cell-style-selectors))
1112 (= r 0)) "FirstRow")
1113 ((and (cdr (assoc 'use-last-row-styles cell-style-selectors))
1114 (= r org-lparse-table-rownum))
1115 "LastRow")
1116 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
1117 (= (% r 2) 1)) "EvenRow")
1118 ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
1119 (= (% r 2) 0)) "OddRow")
1120 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
1121 (= (% c 2) 1)) "EvenColumn")
1122 ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
1123 (= (% c 2) 0)) "OddColumn")
1124 (t ""))))
1125 (cons
1126 (concat template-name cell-type "TableCell")
1127 (concat template-name cell-type "TableParagraph"))))
1129 (cons
1130 (concat
1131 "OrgTblCell"
1132 (cond
1133 ((= r 0) "T")
1134 ((eq (cdr (assoc r org-lparse-table-rowgrp-info)) :start) "T")
1135 (t ""))
1136 (when (= r org-lparse-table-rownum) "B")
1137 (cond
1138 ((= c 0) "")
1139 ((or (memq (nth c org-table-colgroup-info) '(:start :startend))
1140 (memq (nth (1- c) org-table-colgroup-info) '(:end :startend))) "L")
1141 (t "")))
1142 (capitalize (aref org-lparse-table-colalign-vector c))))))
1144 (defun org-odt-get-paragraph-style-cookie-for-table-cell (r c)
1145 (concat
1146 (and (not org-odt-table-style-spec)
1147 (cond
1148 (org-lparse-table-cur-rowgrp-is-hdr "OrgTableHeading")
1149 ((and (= c 0) (org-lparse-get 'TABLE-FIRST-COLUMN-AS-LABELS))
1150 "OrgTableHeading")
1151 (t "OrgTableContents")))
1152 (and org-lparse-table-is-styled
1153 (format "@@table-cell:p@@%03d@@%03d@@" r c))))
1155 (defun org-odt-get-style-name-cookie-for-table-cell (r c)
1156 (when org-lparse-table-is-styled
1157 (format "@@table-cell:style-name@@%03d@@%03d@@" r c)))
1159 (defun org-odt-format-table-cell (data r c horiz-span)
1160 (concat
1161 (let* ((paragraph-style-cookie
1162 (org-odt-get-paragraph-style-cookie-for-table-cell r c))
1163 (style-name-cookie
1164 (org-odt-get-style-name-cookie-for-table-cell r c))
1165 (extra (and style-name-cookie
1166 (format " table:style-name=\"%s\"" style-name-cookie)))
1167 (extra (concat extra
1168 (and (> horiz-span 0)
1169 (format " table:number-columns-spanned=\"%d\""
1170 (1+ horiz-span))))))
1171 (org-odt-format-tags
1172 '("<table:table-cell%s>" . "</table:table-cell>")
1173 (if org-lparse-list-table-p data
1174 (org-odt-format-stylized-paragraph paragraph-style-cookie data)) extra))
1175 (let (s)
1176 (dotimes (i horiz-span)
1177 (setq s (concat s "\n<table:covered-table-cell/>"))) s)
1178 "\n"))
1180 (defun org-odt-begin-footnote-definition (n)
1181 (org-lparse-begin-paragraph 'footnote))
1183 (defun org-odt-end-footnote-definition (n)
1184 (org-lparse-end-paragraph))
1186 (defun org-odt-begin-toc (lang-specific-heading max-level)
1187 ;; Strings in `org-export-language-setup' can contain named html
1188 ;; entities. Replace those with utf-8 equivalents.
1189 (let ((i 0) entity rpl)
1190 (while (string-match "&\\([^#].*?\\);" lang-specific-heading i)
1191 (setq entity (match-string 1 lang-specific-heading))
1192 (if (not (setq rpl (org-entity-get-representation entity 'utf8)))
1193 (setq i (match-end 0))
1194 (setq i (+ (match-beginning 0) (length rpl)))
1195 (setq lang-specific-heading
1196 (replace-match rpl t t lang-specific-heading)))))
1197 (insert
1198 (format "
1199 <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
1200 <text:table-of-content-source text:outline-level=\"%d\">
1201 <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
1202 " max-level lang-specific-heading))
1203 (loop for level from 1 upto 10
1204 do (insert (format
1206 <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
1207 <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
1208 <text:index-entry-chapter/>
1209 <text:index-entry-text/>
1210 <text:index-entry-link-end/>
1211 </text:table-of-content-entry-template>
1212 " level level)))
1214 (insert
1215 (format "
1216 </text:table-of-content-source>
1218 <text:index-body>
1219 <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
1220 <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
1221 </text:index-title>
1222 " lang-specific-heading)))
1224 (defun org-odt-end-toc ()
1225 (insert "
1226 </text:index-body>
1227 </text:table-of-content>
1230 (defun org-odt-format-toc-entry (snumber todo headline tags href)
1231 (setq headline (concat
1232 (and org-export-with-section-numbers
1233 (concat snumber ". "))
1234 headline
1235 (and tags
1236 (concat
1237 (org-lparse-format 'SPACES 3)
1238 (org-lparse-format 'FONTIFY tags "tag")))))
1239 (when todo
1240 (setq headline (org-lparse-format 'FONTIFY headline "todo")))
1242 (let ((org-odt-suppress-xref t))
1243 (org-odt-format-link headline (concat "#" href))))
1245 (defun org-odt-format-toc-item (toc-entry level org-last-level)
1246 (let ((style (format "Contents_20_%d"
1247 (+ level (or (org-lparse-get 'TOPLEVEL-HLEVEL) 1) -1))))
1248 (insert "\n" (org-odt-format-stylized-paragraph style toc-entry) "\n")))
1250 ;; Following variable is let bound during 'ORG-LINK callback. See
1251 ;; org-html.el
1252 (defvar org-lparse-link-description-is-image nil)
1253 (defun org-odt-format-link (desc href &optional attr)
1254 (cond
1255 ((and (= (string-to-char href) ?#) (not org-odt-suppress-xref))
1256 (setq href (substring href 1))
1257 (let ((xref-format "text"))
1258 (when (numberp desc)
1259 (setq desc (format "%d" desc) xref-format "number"))
1260 (when (listp desc)
1261 (setq desc (mapconcat 'identity desc ".") xref-format "chapter"))
1262 (setq href (concat org-export-odt-bookmark-prefix href))
1263 (org-odt-format-tags
1264 '("<text:bookmark-ref text:reference-format=\"%s\" text:ref-name=\"%s\">" .
1265 "</text:bookmark-ref>")
1266 desc xref-format href)))
1267 (org-lparse-link-description-is-image
1268 (org-odt-format-tags
1269 '("<draw:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</draw:a>")
1270 desc href (or attr "")))
1272 (org-odt-format-tags
1273 '("<text:a xlink:type=\"simple\" xlink:href=\"%s\" %s>" . "</text:a>")
1274 desc href (or attr "")))))
1276 (defun org-odt-format-spaces (n)
1277 (cond
1278 ((= n 1) " ")
1279 ((> n 1) (concat
1280 " " (org-odt-format-tags "<text:s text:c=\"%d\"/>" "" (1- n))))
1281 (t "")))
1283 (defun org-odt-format-tabs (&optional n)
1284 (let ((tab "<text:tab/>")
1285 (n (or n 1)))
1286 (insert tab)))
1288 (defun org-odt-format-line-break ()
1289 (org-odt-format-tags "<text:line-break/>" ""))
1291 (defun org-odt-format-horizontal-line ()
1292 (org-odt-format-stylized-paragraph 'horizontal-line ""))
1294 (defun org-odt-encode-plain-text (line &optional no-whitespace-filling)
1295 (setq line (org-xml-encode-plain-text line))
1296 (if no-whitespace-filling line
1297 (org-odt-fill-tabs-and-spaces line)))
1299 (defun org-odt-format-line (line)
1300 (case org-lparse-dyn-current-environment
1301 (fixedwidth (concat
1302 (org-odt-format-stylized-paragraph
1303 'fixedwidth (org-odt-encode-plain-text line)) "\n"))
1304 (t (concat line "\n"))))
1306 (defun org-odt-format-comment (fmt &rest args)
1307 (let ((comment (apply 'format fmt args)))
1308 (format "\n<!-- %s -->\n" comment)))
1310 (defun org-odt-format-org-entity (wd)
1311 (org-entity-get-representation wd 'utf8))
1313 (defun org-odt-fill-tabs-and-spaces (line)
1314 (replace-regexp-in-string
1315 "\\([\t]\\|\\([ ]+\\)\\)" (lambda (s)
1316 (cond
1317 ((string= s "\t") (org-odt-format-tabs))
1318 (t (org-odt-format-spaces (length s))))) line))
1320 (defcustom org-export-odt-fontify-srcblocks t
1321 "Specify whether or not source blocks need to be fontified.
1322 Turn this option on if you want to colorize the source code
1323 blocks in the exported file. For colorization to work, you need
1324 to make available an enhanced version of `htmlfontify' library."
1325 :type 'boolean
1326 :group 'org-export-odt
1327 :version "24.1")
1329 (defun org-odt-format-source-line-with-line-number-and-label
1330 (line rpllbl num fontifier par-style)
1332 (let ((keep-label (not (numberp rpllbl)))
1333 (ref (org-find-text-property-in-string 'org-coderef line)))
1334 (setq line (concat line (and keep-label ref (format "(%s)" ref))))
1335 (setq line (funcall fontifier line))
1336 (when ref
1337 (setq line (org-odt-format-target line (concat "coderef-" ref))))
1338 (setq line (org-odt-format-stylized-paragraph par-style line))
1339 (if (not num) line
1340 (org-odt-format-tags '("<text:list-item>" . "</text:list-item>") line))))
1342 (defun org-odt-format-source-code-or-example-plain
1343 (lines lang caption textareap cols rows num cont rpllbl fmt)
1344 "Format source or example blocks much like fixedwidth blocks.
1345 Use this when `org-export-odt-fontify-srcblocks' option is turned
1346 off."
1347 (let* ((lines (org-split-string lines "[\r\n]"))
1348 (line-count (length lines))
1349 (i 0))
1350 (mapconcat
1351 (lambda (line)
1352 (incf i)
1353 (org-odt-format-source-line-with-line-number-and-label
1354 line rpllbl num 'org-odt-encode-plain-text
1355 (if (= i line-count) "OrgFixedWidthBlockLastLine"
1356 "OrgFixedWidthBlock")))
1357 lines "\n")))
1359 (defvar org-src-block-paragraph-format
1360 "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
1361 <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
1362 <style:background-image/>
1363 </style:paragraph-properties>
1364 <style:text-properties fo:color=\"%s\"/>
1365 </style:style>"
1366 "Custom paragraph style for colorized source and example blocks.
1367 This style is much the same as that of \"OrgFixedWidthBlock\"
1368 except that the foreground and background colors are set
1369 according to the default face identified by the `htmlfontify'.")
1371 (defvar hfy-optimisations)
1372 (declare-function hfy-face-to-style "htmlfontify" (fn))
1373 (declare-function hfy-face-or-def-to-name "htmlfontify" (fn))
1375 (defun org-odt-hfy-face-to-css (fn)
1376 "Create custom style for face FN.
1377 When FN is the default face, use it's foreground and background
1378 properties to create \"OrgSrcBlock\" paragraph style. Otherwise
1379 use it's color attribute to create a character style whose name
1380 is obtained from FN. Currently all attributes of FN other than
1381 color are ignored.
1383 The style name for a face FN is derived using the following
1384 operations on the face name in that order - de-dash, CamelCase
1385 and prefix with \"OrgSrc\". For example,
1386 `font-lock-function-name-face' is associated with
1387 \"OrgSrcFontLockFunctionNameFace\"."
1388 (let* ((css-list (hfy-face-to-style fn))
1389 (style-name ((lambda (fn)
1390 (concat "OrgSrc"
1391 (mapconcat
1392 'capitalize (split-string
1393 (hfy-face-or-def-to-name fn) "-")
1394 ""))) fn))
1395 (color-val (cdr (assoc "color" css-list)))
1396 (background-color-val (cdr (assoc "background" css-list)))
1397 (style (and org-export-odt-create-custom-styles-for-srcblocks
1398 (cond
1399 ((eq fn 'default)
1400 (format org-src-block-paragraph-format
1401 background-color-val color-val))
1403 (format
1405 <style:style style:name=\"%s\" style:family=\"text\">
1406 <style:text-properties fo:color=\"%s\"/>
1407 </style:style>" style-name color-val))))))
1408 (cons style-name style)))
1410 (defun org-odt-insert-custom-styles-for-srcblocks (styles)
1411 "Save STYLES used for colorizing of source blocks.
1412 Update styles.xml with styles that were collected as part of
1413 `org-odt-hfy-face-to-css' callbacks."
1414 (when styles
1415 (with-current-buffer
1416 (find-file-noselect (expand-file-name "styles.xml") t)
1417 (goto-char (point-min))
1418 (when (re-search-forward "</office:styles>" nil t)
1419 (goto-char (match-beginning 0))
1420 (insert "\n<!-- Org Htmlfontify Styles -->\n" styles "\n")))))
1422 (defun org-odt-format-source-code-or-example-colored
1423 (lines lang caption textareap cols rows num cont rpllbl fmt)
1424 "Format source or example blocks using `htmlfontify-string'.
1425 Use this routine when `org-export-odt-fontify-srcblocks' option
1426 is turned on."
1427 (let* ((lang-m (and lang (or (cdr (assoc lang org-src-lang-modes)) lang)))
1428 (mode (and lang-m (intern (concat (if (symbolp lang-m)
1429 (symbol-name lang-m)
1430 lang-m) "-mode"))))
1431 (org-inhibit-startup t)
1432 (org-startup-folded nil)
1433 (lines (with-temp-buffer
1434 (insert lines)
1435 (if (functionp mode) (funcall mode) (fundamental-mode))
1436 (font-lock-fontify-buffer)
1437 (buffer-string)))
1438 (hfy-html-quote-regex "\\([<\"&> ]\\)")
1439 (hfy-html-quote-map '(("\"" "&quot;")
1440 ("<" "&lt;")
1441 ("&" "&amp;")
1442 (">" "&gt;")
1443 (" " "<text:s/>")
1444 (" " "<text:tab/>")))
1445 (hfy-face-to-css 'org-odt-hfy-face-to-css)
1446 (hfy-optimisations-1 (copy-seq hfy-optimisations))
1447 (hfy-optimisations (add-to-list 'hfy-optimisations-1
1448 'body-text-only))
1449 (hfy-begin-span-handler
1450 (lambda (style text-block text-id text-begins-block-p)
1451 (insert (format "<text:span text:style-name=\"%s\">" style))))
1452 (hfy-end-span-handler (lambda nil (insert "</text:span>"))))
1453 (when (fboundp 'htmlfontify-string)
1454 (let* ((lines (org-split-string lines "[\r\n]"))
1455 (line-count (length lines))
1456 (i 0))
1457 (mapconcat
1458 (lambda (line)
1459 (incf i)
1460 (org-odt-format-source-line-with-line-number-and-label
1461 line rpllbl num 'htmlfontify-string
1462 (if (= i line-count) "OrgSrcBlockLastLine" "OrgSrcBlock")))
1463 lines "\n")))))
1465 (defun org-odt-format-source-code-or-example (lines lang caption textareap
1466 cols rows num cont
1467 rpllbl fmt)
1468 "Format source or example blocks for export.
1469 Use `org-odt-format-source-code-or-example-plain' or
1470 `org-odt-format-source-code-or-example-colored' depending on the
1471 value of `org-export-odt-fontify-srcblocks."
1472 (setq lines (org-export-number-lines
1473 lines 0 0 num cont rpllbl fmt 'preprocess)
1474 lines (funcall
1475 (or (and org-export-odt-fontify-srcblocks
1476 (or (featurep 'htmlfontify)
1477 ;; htmlfontify.el was introduced in Emacs 23.2
1478 ;; So load it with some caution
1479 (require 'htmlfontify nil t))
1480 (fboundp 'htmlfontify-string)
1481 'org-odt-format-source-code-or-example-colored)
1482 'org-odt-format-source-code-or-example-plain)
1483 lines lang caption textareap cols rows num cont rpllbl fmt))
1484 (if (not num) lines
1485 (let ((extra (format " text:continue-numbering=\"%s\""
1486 (if cont "true" "false"))))
1487 (org-odt-format-tags
1488 '("<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>"
1489 . "</text:list>") lines extra))))
1491 (defun org-odt-remap-stylenames (style-name)
1493 (cdr (assoc style-name '(("timestamp-wrapper" . "OrgTimestampWrapper")
1494 ("timestamp" . "OrgTimestamp")
1495 ("timestamp-kwd" . "OrgTimestampKeyword")
1496 ("tag" . "OrgTag")
1497 ("todo" . "OrgTodo")
1498 ("done" . "OrgDone")
1499 ("target" . "OrgTarget"))))
1500 style-name))
1502 (defun org-odt-format-fontify (text style &optional id)
1503 (let* ((style-name
1504 (cond
1505 ((stringp style)
1506 (org-odt-remap-stylenames style))
1507 ((symbolp style)
1508 (org-odt-get-style-name-for-entity 'character style))
1509 ((listp style)
1510 (assert (< 1 (length style)))
1511 (let ((parent-style (pop style)))
1512 (mapconcat (lambda (s)
1513 ;; (assert (stringp s) t)
1514 (org-odt-remap-stylenames s)) style "")
1515 (org-odt-remap-stylenames parent-style)))
1516 (t (error "Don't how to handle style %s" style)))))
1517 (org-odt-format-tags
1518 '("<text:span text:style-name=\"%s\">" . "</text:span>")
1519 text style-name)))
1521 (defun org-odt-relocate-relative-path (path dir)
1522 (if (file-name-absolute-p path) path
1523 (file-relative-name (expand-file-name path dir)
1524 (expand-file-name "eyecandy" dir))))
1526 (defun org-odt-format-inline-image (thefile)
1527 (let* ((thelink (if (file-name-absolute-p thefile) thefile
1528 (org-xml-format-href
1529 (org-odt-relocate-relative-path
1530 thefile org-current-export-file))))
1531 (href
1532 (org-odt-format-tags
1533 "<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
1534 (if org-export-odt-embed-images
1535 (org-odt-copy-image-file thefile) thelink))))
1536 (org-export-odt-format-image thefile href)))
1538 (defvar org-odt-entity-labels-alist nil
1539 "Associate Labels with the Labeled entities.
1540 Each element of the alist is of the form (LABEL-NAME
1541 CATEGORY-NAME SEQNO LABEL-STYLE-NAME). LABEL-NAME is same as
1542 that specified by \"#+LABEL: ...\" line. CATEGORY-NAME is the
1543 type of the entity that LABEL-NAME is attached to. CATEGORY-NAME
1544 can be one of \"Table\", \"Figure\" or \"Equation\". SEQNO is
1545 the unique number assigned to the referenced entity on a
1546 per-CATEGORY basis. It is generated sequentially and is 1-based.
1547 LABEL-STYLE-NAME is a key `org-odt-label-styles'.
1549 See `org-odt-add-label-definition' and
1550 `org-odt-fixup-label-references'.")
1552 (defun org-export-odt-format-formula (src href)
1553 (save-match-data
1554 (let* ((caption (org-find-text-property-in-string 'org-caption src))
1555 (caption (and caption (org-xml-format-desc caption)))
1556 (label (org-find-text-property-in-string 'org-label src))
1557 (latex-frag (org-find-text-property-in-string 'org-latex-src src))
1558 (embed-as (or (and latex-frag
1559 (org-find-text-property-in-string
1560 'org-latex-src-embed-type src))
1561 (if (or caption label) 'paragraph 'character)))
1562 width height)
1563 (when latex-frag
1564 (setq href (org-propertize href :title "LaTeX Fragment"
1565 :description latex-frag)))
1566 (cond
1567 ((eq embed-as 'character)
1568 (org-odt-format-entity "InlineFormula" href width height))
1570 (org-lparse-end-paragraph)
1571 (org-lparse-insert-list-table
1572 `((,(org-odt-format-entity
1573 (if (not (or caption label)) "DisplayFormula"
1574 "CaptionedDisplayFormula")
1575 href width height :caption caption :label label)
1576 ,(if (not (or caption label)) ""
1577 (let* ((label-props (car org-odt-entity-labels-alist)))
1578 (setcar (last label-props) "math-label")
1579 (apply 'org-odt-format-label-definition
1580 caption label-props)))))
1581 nil nil nil ":style \"OrgEquation\"" nil '((1 "c" 8) (2 "c" 1)))
1582 (throw 'nextline nil))))))
1584 (defvar org-odt-embedded-formulas-count 0)
1585 (defun org-odt-copy-formula-file (path)
1586 "Returns the internal name of the file"
1587 (let* ((src-file (expand-file-name
1588 path (file-name-directory org-current-export-file)))
1589 (target-dir (format "Formula-%04d/"
1590 (incf org-odt-embedded-formulas-count)))
1591 (target-file (concat target-dir "content.xml")))
1592 (when (not org-lparse-to-buffer)
1593 (message "Embedding %s as %s ..."
1594 (substring-no-properties path) target-file)
1596 (make-directory target-dir)
1597 (org-odt-create-manifest-file-entry
1598 "application/vnd.oasis.opendocument.formula" target-dir "1.2")
1600 (case (org-odt-is-formula-link-p src-file)
1601 (mathml
1602 (copy-file src-file target-file 'overwrite))
1603 (odf
1604 (org-odt-zip-extract-one src-file "content.xml" target-dir))
1606 (error "%s is not a formula file" src-file)))
1608 (org-odt-create-manifest-file-entry "text/xml" target-file))
1609 target-file))
1611 (defun org-odt-format-inline-formula (thefile)
1612 (let* ((thelink (if (file-name-absolute-p thefile) thefile
1613 (org-xml-format-href
1614 (org-odt-relocate-relative-path
1615 thefile org-current-export-file))))
1616 (href
1617 (org-odt-format-tags
1618 "<draw:object xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>" ""
1619 (file-name-directory (org-odt-copy-formula-file thefile)))))
1620 (org-export-odt-format-formula thefile href)))
1622 (defun org-odt-is-formula-link-p (file)
1623 (let ((case-fold-search nil))
1624 (cond
1625 ((string-match "\\.\\(mathml\\|mml\\)\\'" file)
1626 'mathml)
1627 ((string-match "\\.odf\\'" file)
1628 'odf))))
1630 (defun org-odt-format-org-link (opt-plist type-1 path fragment desc attr
1631 descp)
1632 "Make a OpenDocument link.
1633 OPT-PLIST is an options list.
1634 TYPE-1 is the device-type of the link (THIS://foo.html).
1635 PATH is the path of the link (http://THIS#location).
1636 FRAGMENT is the fragment part of the link, if any (foo.html#THIS).
1637 DESC is the link description, if any.
1638 ATTR is a string of other attributes of the a element."
1639 (declare (special org-lparse-par-open))
1640 (save-match-data
1641 (let* ((may-inline-p
1642 (and (member type-1 '("http" "https" "file"))
1643 (org-lparse-should-inline-p path descp)
1644 (not fragment)))
1645 (type (if (equal type-1 "id") "file" type-1))
1646 (filename path)
1647 (thefile path)
1648 sec-frag sec-nos)
1649 (cond
1650 ;; check for inlined images
1651 ((and (member type '("file"))
1652 (not fragment)
1653 (org-file-image-p
1654 filename org-export-odt-inline-image-extensions)
1655 (or (eq t org-export-odt-inline-images)
1656 (and org-export-odt-inline-images (not descp))))
1657 (org-odt-format-inline-image thefile))
1658 ;; check for embedded formulas
1659 ((and (member type '("file"))
1660 (not fragment)
1661 (org-odt-is-formula-link-p filename)
1662 (or (not descp)))
1663 (org-odt-format-inline-formula thefile))
1664 ;; code references
1665 ((string= type "coderef")
1666 (let* ((ref fragment)
1667 (lineno-or-ref (cdr (assoc ref org-export-code-refs)))
1668 (desc (and descp desc))
1669 (org-odt-suppress-xref nil)
1670 (href (org-xml-format-href (concat "#coderef-" ref))))
1671 (cond
1672 ((and (numberp lineno-or-ref) (not desc))
1673 (org-odt-format-link lineno-or-ref href))
1674 ((and (numberp lineno-or-ref) desc
1675 (string-match (regexp-quote (concat "(" ref ")")) desc))
1676 (format (replace-match "%s" t t desc)
1677 (org-odt-format-link lineno-or-ref href)))
1679 (setq desc (format
1680 (if (and desc (string-match
1681 (regexp-quote (concat "(" ref ")"))
1682 desc))
1683 (replace-match "%s" t t desc)
1684 (or desc "%s"))
1685 lineno-or-ref))
1686 (org-odt-format-link (org-xml-format-desc desc) href)))))
1687 ;; links to headlines
1688 ((and (string= type "")
1689 (or (not thefile) (string= thefile ""))
1690 (plist-get org-lparse-opt-plist :section-numbers)
1691 (setq sec-frag fragment)
1692 (or (string-match "\\`sec\\(\\(-[0-9]+\\)+\\)" sec-frag)
1693 (and (setq sec-frag
1694 (loop for alias in org-export-target-aliases do
1695 (when (member fragment (cdr alias))
1696 (return (car alias)))))
1697 (string-match "\\`sec\\(\\(-[0-9]+\\)+\\)" sec-frag)))
1698 (setq sec-nos (org-split-string (match-string 1 sec-frag) "-"))
1699 (<= (length sec-nos) (plist-get org-lparse-opt-plist
1700 :headline-levels)))
1701 (let ((org-odt-suppress-xref nil))
1702 (org-odt-format-link sec-nos (concat "#" sec-frag) attr)))
1704 (when (string= type "file")
1705 (setq thefile
1706 (cond
1707 ((file-name-absolute-p path)
1708 (concat "file://" (expand-file-name path)))
1709 (t (org-odt-relocate-relative-path
1710 thefile org-current-export-file)))))
1712 (when (and (member type '("" "http" "https" "file")) fragment)
1713 (setq thefile (concat thefile "#" fragment)))
1715 (setq thefile (org-xml-format-href thefile))
1717 (when (not (member type '("" "file")))
1718 (setq thefile (concat type ":" thefile)))
1720 (let ((org-odt-suppress-xref nil))
1721 (org-odt-format-link
1722 (org-xml-format-desc desc) thefile attr)))))))
1724 (defun org-odt-format-heading (text level &optional id)
1725 (let* ((text (if id (org-odt-format-target text id) text)))
1726 (org-odt-format-tags
1727 '("<text:h text:style-name=\"Heading_20_%s\" text:outline-level=\"%s\">" .
1728 "</text:h>") text level level)))
1730 (defun org-odt-format-headline (title extra-targets tags
1731 &optional snumber level)
1732 (concat
1733 (org-lparse-format 'EXTRA-TARGETS extra-targets)
1735 ;; No need to generate section numbers. They are auto-generated by
1736 ;; the application
1738 ;; (concat (org-lparse-format 'SECTION-NUMBER snumber level) " ")
1739 title
1740 (and tags (concat (org-lparse-format 'SPACES 3)
1741 (org-lparse-format 'ORG-TAGS tags)))))
1743 (defun org-odt-format-anchor (text name &optional class)
1744 (org-odt-format-target text name))
1746 (defun org-odt-format-bookmark (text id)
1747 (if id
1748 (org-odt-format-tags "<text:bookmark text:name=\"%s\"/>" text id)
1749 text))
1751 (defun org-odt-format-target (text id)
1752 (let ((name (concat org-export-odt-bookmark-prefix id)))
1753 (concat
1754 (and id (org-odt-format-tags
1755 "<text:bookmark-start text:name=\"%s\"/>" "" name))
1756 (org-odt-format-bookmark text id)
1757 (and id (org-odt-format-tags
1758 "<text:bookmark-end text:name=\"%s\"/>" "" name)))))
1760 (defun org-odt-format-footnote (n def)
1761 (let ((id (concat "fn" n))
1762 (note-class "footnote")
1763 (par-style "Footnote"))
1764 (org-odt-format-tags
1765 '("<text:note text:id=\"%s\" text:note-class=\"%s\">" .
1766 "</text:note>")
1767 (concat
1768 (org-odt-format-tags
1769 '("<text:note-citation>" . "</text:note-citation>")
1771 (org-odt-format-tags
1772 '("<text:note-body>" . "</text:note-body>")
1773 def))
1774 id note-class)))
1776 (defun org-odt-format-footnote-reference (n def refcnt)
1777 (if (= refcnt 1)
1778 (org-odt-format-footnote n def)
1779 (org-odt-format-footnote-ref n)))
1781 (defun org-odt-format-footnote-ref (n)
1782 (let ((note-class "footnote")
1783 (ref-format "text")
1784 (ref-name (concat "fn" n)))
1785 (org-odt-format-tags
1786 '("<text:span text:style-name=\"%s\">" . "</text:span>")
1787 (org-odt-format-tags
1788 '("<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">" . "</text:note-ref>")
1789 n note-class ref-format ref-name)
1790 "OrgSuperscript")))
1792 (defun org-odt-get-image-name (file-name)
1793 (require 'sha1)
1794 (file-relative-name
1795 (expand-file-name
1796 (concat (sha1 file-name) "." (file-name-extension file-name)) "Pictures")))
1798 (defun org-export-odt-format-image (src href)
1799 "Create image tag with source and attributes."
1800 (save-match-data
1801 (let* ((caption (org-find-text-property-in-string 'org-caption src))
1802 (caption (and caption (org-xml-format-desc caption)))
1803 (attr (org-find-text-property-in-string 'org-attributes src))
1804 (label (org-find-text-property-in-string 'org-label src))
1805 (latex-frag (org-find-text-property-in-string
1806 'org-latex-src src))
1807 (category (and latex-frag "__DvipngImage__"))
1808 (attr-plist (org-lparse-get-block-params attr))
1809 (user-frame-anchor
1810 (car (assoc-string (plist-get attr-plist :anchor)
1811 '(("as-char") ("paragraph") ("page")) t)))
1812 (user-frame-style
1813 (and user-frame-anchor (plist-get attr-plist :style)))
1814 (user-frame-attrs
1815 (and user-frame-anchor (plist-get attr-plist :attributes)))
1816 (user-frame-params
1817 (list user-frame-style user-frame-attrs user-frame-anchor))
1818 (embed-as (cond
1819 (latex-frag
1820 (symbol-name
1821 (case (org-find-text-property-in-string
1822 'org-latex-src-embed-type src)
1823 (paragraph 'paragraph)
1824 (t 'as-char))))
1825 (user-frame-anchor)
1826 (t "paragraph")))
1827 (size (org-odt-image-size-from-file
1828 src (plist-get attr-plist :width)
1829 (plist-get attr-plist :height)
1830 (plist-get attr-plist :scale) nil embed-as))
1831 (width (car size)) (height (cdr size)))
1832 (when latex-frag
1833 (setq href (org-propertize href :title "LaTeX Fragment"
1834 :description latex-frag)))
1835 (let ((frame-style-handle (concat (and (or caption label) "Captioned")
1836 embed-as "Image")))
1837 (org-odt-format-entity
1838 frame-style-handle href width height
1839 :caption caption :label label :category category
1840 :user-frame-params user-frame-params)))))
1842 (defun org-odt-format-object-description (title description)
1843 (concat (and title (org-odt-format-tags
1844 '("<svg:title>" . "</svg:title>")
1845 (org-odt-encode-plain-text title t)))
1846 (and description (org-odt-format-tags
1847 '("<svg:desc>" . "</svg:desc>")
1848 (org-odt-encode-plain-text description t)))))
1850 (defun org-odt-format-frame (text width height style &optional
1851 extra anchor-type)
1852 (let ((frame-attrs
1853 (concat
1854 (if width (format " svg:width=\"%0.2fcm\"" width) "")
1855 (if height (format " svg:height=\"%0.2fcm\"" height) "")
1856 extra
1857 (format " text:anchor-type=\"%s\"" (or anchor-type "paragraph")))))
1858 (org-odt-format-tags
1859 '("<draw:frame draw:style-name=\"%s\"%s>" . "</draw:frame>")
1860 (concat text (org-odt-format-object-description
1861 (get-text-property 0 :title text)
1862 (get-text-property 0 :description text)))
1863 style frame-attrs)))
1865 (defun org-odt-format-textbox (text width height style &optional
1866 extra anchor-type)
1867 (org-odt-format-frame
1868 (org-odt-format-tags
1869 '("<draw:text-box %s>" . "</draw:text-box>")
1870 text (concat (format " fo:min-height=\"%0.2fcm\"" (or height .2))
1871 (unless width
1872 (format " fo:min-width=\"%0.2fcm\"" (or width .2)))))
1873 width nil style extra anchor-type))
1875 (defun org-odt-format-inlinetask (heading content
1876 &optional todo priority tags)
1877 (org-odt-format-stylized-paragraph
1878 nil (org-odt-format-textbox
1879 (concat (org-odt-format-stylized-paragraph
1880 "OrgInlineTaskHeading"
1881 (org-lparse-format
1882 'HEADLINE (concat (org-lparse-format-todo todo) " " heading)
1883 nil tags))
1884 content) nil nil "OrgInlineTaskFrame" " style:rel-width=\"100%\"")))
1886 (defvar org-odt-entity-frame-styles
1887 '(("As-CharImage" "__Figure__" ("OrgInlineImage" nil "as-char"))
1888 ("ParagraphImage" "__Figure__" ("OrgDisplayImage" nil "paragraph"))
1889 ("PageImage" "__Figure__" ("OrgPageImage" nil "page"))
1890 ("CaptionedAs-CharImage" "__Figure__"
1891 ("OrgCaptionedImage"
1892 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
1893 ("OrgInlineImage" nil "as-char"))
1894 ("CaptionedParagraphImage" "__Figure__"
1895 ("OrgCaptionedImage"
1896 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
1897 ("OrgImageCaptionFrame" nil "paragraph"))
1898 ("CaptionedPageImage" "__Figure__"
1899 ("OrgCaptionedImage"
1900 " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
1901 ("OrgPageImageCaptionFrame" nil "page"))
1902 ("InlineFormula" "__MathFormula__" ("OrgInlineFormula" nil "as-char"))
1903 ("DisplayFormula" "__MathFormula__" ("OrgDisplayFormula" nil "as-char"))
1904 ("CaptionedDisplayFormula" "__MathFormula__"
1905 ("OrgCaptionedFormula" nil "paragraph")
1906 ("OrgFormulaCaptionFrame" nil "as-char"))))
1908 (defun org-odt-merge-frame-params(default-frame-params user-frame-params)
1909 (if (not user-frame-params) default-frame-params
1910 (assert (= (length default-frame-params) 3))
1911 (assert (= (length user-frame-params) 3))
1912 (loop for user-frame-param in user-frame-params
1913 for default-frame-param in default-frame-params
1914 collect (or user-frame-param default-frame-param))))
1916 (defun* org-odt-format-entity (entity href width height
1917 &key caption label category
1918 user-frame-params)
1919 (let* ((entity-style (assoc-string entity org-odt-entity-frame-styles t))
1920 default-frame-params frame-params)
1921 (cond
1922 ((not (or caption label))
1923 (setq default-frame-params (nth 2 entity-style))
1924 (setq frame-params (org-odt-merge-frame-params
1925 default-frame-params user-frame-params))
1926 (apply 'org-odt-format-frame href width height frame-params))
1928 (setq default-frame-params (nth 3 entity-style))
1929 (setq frame-params (org-odt-merge-frame-params
1930 default-frame-params user-frame-params))
1931 (apply 'org-odt-format-textbox
1932 (org-odt-format-stylized-paragraph
1933 'illustration
1934 (concat
1935 (apply 'org-odt-format-frame href width height
1936 (nth 2 entity-style))
1937 (org-odt-format-entity-caption
1938 label caption (or category (nth 1 entity-style)))))
1939 width height frame-params)))))
1941 (defvar org-odt-embedded-images-count 0)
1942 (defun org-odt-copy-image-file (path)
1943 "Returns the internal name of the file"
1944 (let* ((image-type (file-name-extension path))
1945 (media-type (format "image/%s" image-type))
1946 (src-file (expand-file-name
1947 path (file-name-directory org-current-export-file)))
1948 (target-dir "Images/")
1949 (target-file
1950 (format "%s%04d.%s" target-dir
1951 (incf org-odt-embedded-images-count) image-type)))
1952 (when (not org-lparse-to-buffer)
1953 (message "Embedding %s as %s ..."
1954 (substring-no-properties path) target-file)
1956 (when (= 1 org-odt-embedded-images-count)
1957 (make-directory target-dir)
1958 (org-odt-create-manifest-file-entry "" target-dir))
1960 (copy-file src-file target-file 'overwrite)
1961 (org-odt-create-manifest-file-entry media-type target-file))
1962 target-file))
1964 (defvar org-export-odt-image-size-probe-method
1965 (append (and (executable-find "identify") '(imagemagick)) ; See Bug#10675
1966 '(emacs fixed))
1967 "Ordered list of methods for determining image sizes.")
1969 (defvar org-export-odt-default-image-sizes-alist
1970 '(("as-char" . (5 . 0.4))
1971 ("paragraph" . (5 . 5)))
1972 "Hardcoded image dimensions one for each of the anchor
1973 methods.")
1975 ;; A4 page size is 21.0 by 29.7 cms
1976 ;; The default page settings has 2cm margin on each of the sides. So
1977 ;; the effective text area is 17.0 by 25.7 cm
1978 (defvar org-export-odt-max-image-size '(17.0 . 20.0)
1979 "Limiting dimensions for an embedded image.")
1981 (defun org-odt-do-image-size (probe-method file &optional dpi anchor-type)
1982 (setq dpi (or dpi org-export-odt-pixels-per-inch))
1983 (setq anchor-type (or anchor-type "paragraph"))
1984 (flet ((size-in-cms (size-in-pixels)
1985 (flet ((pixels-to-cms (pixels)
1986 (let* ((cms-per-inch 2.54)
1987 (inches (/ pixels dpi)))
1988 (* cms-per-inch inches))))
1989 (and size-in-pixels
1990 (cons (pixels-to-cms (car size-in-pixels))
1991 (pixels-to-cms (cdr size-in-pixels)))))))
1992 (case probe-method
1993 (emacs
1994 (size-in-cms (ignore-errors ; Emacs could be in batch mode
1995 (clear-image-cache)
1996 (image-size (create-image file) 'pixels))))
1997 (imagemagick
1998 (size-in-cms
1999 (let ((dim (shell-command-to-string
2000 (format "identify -format \"%%w:%%h\" \"%s\"" file))))
2001 (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim)
2002 (cons (string-to-number (match-string 1 dim))
2003 (string-to-number (match-string 2 dim)))))))
2005 (cdr (assoc-string anchor-type
2006 org-export-odt-default-image-sizes-alist))))))
2008 (defun org-odt-image-size-from-file (file &optional user-width
2009 user-height scale dpi embed-as)
2010 (unless (file-name-absolute-p file)
2011 (setq file (expand-file-name
2012 file (file-name-directory org-current-export-file))))
2013 (let* (size width height)
2014 (unless (and user-height user-width)
2015 (loop for probe-method in org-export-odt-image-size-probe-method
2016 until size
2017 do (setq size (org-odt-do-image-size
2018 probe-method file dpi embed-as)))
2019 (or size (error "Cannot determine Image size. Aborting ..."))
2020 (setq width (car size) height (cdr size)))
2021 (cond
2022 (scale
2023 (setq width (* width scale) height (* height scale)))
2024 ((and user-height user-width)
2025 (setq width user-width height user-height))
2026 (user-height
2027 (setq width (* user-height (/ width height)) height user-height))
2028 (user-width
2029 (setq height (* user-width (/ height width)) width user-width))
2030 (t (ignore)))
2031 ;; ensure that an embedded image fits comfortably within a page
2032 (let ((max-width (car org-export-odt-max-image-size))
2033 (max-height (cdr org-export-odt-max-image-size)))
2034 (when (or (> width max-width) (> height max-height))
2035 (let* ((scale1 (/ max-width width))
2036 (scale2 (/ max-height height))
2037 (scale (min scale1 scale2)))
2038 (setq width (* scale width) height (* scale height)))))
2039 (cons width height)))
2041 (defvar org-odt-entity-counts-plist nil
2042 "Plist of running counters of SEQNOs for each of the CATEGORY-NAMEs.
2043 See `org-odt-entity-labels-alist' for known CATEGORY-NAMEs.")
2045 (defvar org-odt-label-styles
2046 '(("math-formula" "%c" "text" "(%n)")
2047 ("math-label" "(%n)" "text" "(%n)")
2048 ("category-and-value" "%e %n: %c" "category-and-value" "%e %n")
2049 ("value" "%e %n: %c" "value" "%n"))
2050 "Specify how labels are applied and referenced.
2051 This is an alist where each element is of the
2052 form (LABEL-STYLE-NAME LABEL-ATTACH-FMT LABEL-REF-MODE
2053 LABEL-REF-FMT).
2055 LABEL-ATTACH-FMT controls how labels and captions are attached to
2056 an entity. It may contain following specifiers - %e, %n and %c.
2057 %e is replaced with the CATEGORY-NAME. %n is replaced with
2058 \"<text:sequence ...> SEQNO </text:sequence>\". %c is replaced
2059 with CAPTION. See `org-odt-format-label-definition'.
2061 LABEL-REF-MODE and LABEL-REF-FMT controls how label references
2062 are generated. The following XML is generated for a label
2063 reference - \"<text:sequence-ref
2064 text:reference-format=\"LABEL-REF-MODE\" ...> LABEL-REF-FMT
2065 </text:sequence-ref>\". LABEL-REF-FMT may contain following
2066 specifiers - %e and %n. %e is replaced with the CATEGORY-NAME.
2067 %n is replaced with SEQNO. See
2068 `org-odt-format-label-reference'.")
2070 (defcustom org-export-odt-category-strings
2071 '(("en" "Table" "Figure" "Equation" "Equation"))
2072 "Specify category strings for various captionable entities.
2073 Captionable entity can be one of a Table, an Embedded Image, a
2074 LaTeX fragment (generated with dvipng) or a Math Formula.
2076 For example, when `org-export-default-language' is \"en\", an
2077 embedded image will be captioned as \"Figure 1: Orgmode Logo\".
2078 If you want the images to be captioned instead as \"Illustration
2079 1: Orgmode Logo\", then modify the entry for \"en\" as shown
2080 below.
2082 \(setq org-export-odt-category-strings
2083 '\(\(\"en\" \"Table\" \"Illustration\"
2084 \"Equation\" \"Equation\"\)\)\)"
2085 :group 'org-export-odt
2086 :version "24.1"
2087 :type '(repeat (list (string :tag "Language tag")
2088 (choice :tag "Table"
2089 (const :tag "Use Default" nil)
2090 (string :tag "Category string"))
2091 (choice :tag "Figure"
2092 (const :tag "Use Default" nil)
2093 (string :tag "Category string"))
2094 (choice :tag "Math Formula"
2095 (const :tag "Use Default" nil)
2096 (string :tag "Category string"))
2097 (choice :tag "Dvipng Image"
2098 (const :tag "Use Default" nil)
2099 (string :tag "Category string")))))
2101 (defvar org-odt-category-map-alist
2102 '(("__Table__" "Table" "value")
2103 ("__Figure__" "Illustration" "value")
2104 ("__MathFormula__" "Text" "math-formula")
2105 ("__DvipngImage__" "Equation" "value")
2106 ;; ("__Table__" "Table" "category-and-value")
2107 ;; ("__Figure__" "Figure" "category-and-value")
2108 ;; ("__DvipngImage__" "Equation" "category-and-value")
2110 "Map a CATEGORY-HANDLE to OD-VARIABLE and LABEL-STYLE.
2111 This is a list where each entry is of the form \\(CATEGORY-HANDLE
2112 OD-VARIABLE LABEL-STYLE\\). CATEGORY_HANDLE identifies the
2113 captionable entity in question. OD-VARIABLE is the OpenDocument
2114 sequence counter associated with the entity. These counters are
2115 declared within
2116 \"<text:sequence-decls>...</text:sequence-decls>\" block of
2117 `org-export-odt-content-template-file'. LABEL-STYLE is a key
2118 into `org-odt-label-styles' and specifies how a given entity
2119 should be captioned and referenced.
2121 The position of a CATEGORY-HANDLE in this list is used as an
2122 index in to per-language entry for
2123 `org-export-odt-category-strings' to retrieve a CATEGORY-NAME.
2124 This CATEGORY-NAME is then used for qualifying the user-specified
2125 captions on export.")
2127 (defun org-odt-add-label-definition (label default-category)
2128 "Create an entry in `org-odt-entity-labels-alist' and return it."
2129 (let* ((label-props (assoc default-category org-odt-category-map-alist))
2130 ;; identify the sequence number
2131 (counter (nth 1 label-props))
2132 (sequence-var (intern counter))
2133 (seqno (1+ (or (plist-get org-odt-entity-counts-plist sequence-var)
2134 0)))
2135 ;; assign an internal label, if user has not provided one
2136 (label (if label (substring-no-properties label)
2137 (format "%s-%s" default-category seqno)))
2138 ;; identify label style
2139 (label-style (nth 2 label-props))
2140 ;; grok language setting
2141 (en-strings (assoc-default "en" org-export-odt-category-strings))
2142 (lang (plist-get org-lparse-opt-plist :language))
2143 (lang-strings (assoc-default lang org-export-odt-category-strings))
2144 ;; retrieve localized category sting
2145 (pos (- (length org-odt-category-map-alist)
2146 (length (memq label-props org-odt-category-map-alist))))
2147 (category (or (nth pos lang-strings) (nth pos en-strings)))
2148 (label-props (list label category counter seqno label-style)))
2149 ;; synchronize internal counters
2150 (setq org-odt-entity-counts-plist
2151 (plist-put org-odt-entity-counts-plist sequence-var seqno))
2152 ;; stash label properties for later retrieval
2153 (push label-props org-odt-entity-labels-alist)
2154 label-props))
2156 (defun org-odt-format-label-definition (caption label category counter
2157 seqno label-style)
2158 (assert label)
2159 (format-spec
2160 (cadr (assoc-string label-style org-odt-label-styles t))
2161 `((?e . ,category)
2162 (?n . ,(org-odt-format-tags
2163 '("<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">" . "</text:sequence>")
2164 (format "%d" seqno) label counter counter))
2165 (?c . ,(or caption "")))))
2167 (defun org-odt-format-label-reference (label category counter
2168 seqno label-style)
2169 (assert label)
2170 (save-match-data
2171 (let* ((fmt (cddr (assoc-string label-style org-odt-label-styles t)))
2172 (fmt1 (car fmt))
2173 (fmt2 (cadr fmt)))
2174 (org-odt-format-tags
2175 '("<text:sequence-ref text:reference-format=\"%s\" text:ref-name=\"%s\">"
2176 . "</text:sequence-ref>")
2177 (format-spec fmt2 `((?e . ,category)
2178 (?n . ,(format "%d" seqno)))) fmt1 label))))
2180 (defun org-odt-fixup-label-references ()
2181 (goto-char (point-min))
2182 (while (re-search-forward
2183 "<text:sequence-ref text:ref-name=\"\\([^\"]+\\)\">[ \t\n]*</text:sequence-ref>"
2184 nil t)
2185 (let* ((label (match-string 1))
2186 (label-def (assoc label org-odt-entity-labels-alist))
2187 (rpl (and label-def
2188 (apply 'org-odt-format-label-reference label-def))))
2189 (if rpl (replace-match rpl t t)
2190 (org-lparse-warn
2191 (format "Unable to resolve reference to label \"%s\"" label))))))
2193 (defun org-odt-format-entity-caption (label caption category)
2194 (if (not (or label caption)) ""
2195 (apply 'org-odt-format-label-definition caption
2196 (org-odt-add-label-definition label category))))
2198 (defun org-odt-format-tags (tag text &rest args)
2199 (let ((prefix (when org-lparse-encode-pending "@"))
2200 (suffix (when org-lparse-encode-pending "@")))
2201 (apply 'org-lparse-format-tags tag text prefix suffix args)))
2203 (defvar org-odt-manifest-file-entries nil)
2204 (defun org-odt-init-outfile (filename)
2205 (unless (executable-find "zip")
2206 ;; Not at all OSes ship with zip by default
2207 (error "Executable \"zip\" needed for creating OpenDocument files"))
2209 (let* ((outdir (make-temp-file
2210 (format org-export-odt-tmpdir-prefix org-lparse-backend) t))
2211 (content-file (expand-file-name "content.xml" outdir)))
2213 ;; init conten.xml
2214 (require 'nxml-mode)
2215 (let ((nxml-auto-insert-xml-declaration-flag nil))
2216 (find-file-noselect content-file t))
2218 ;; reset variables
2219 (setq org-odt-manifest-file-entries nil
2220 org-odt-embedded-images-count 0
2221 org-odt-embedded-formulas-count 0
2222 org-odt-entity-labels-alist nil
2223 org-odt-list-stack-stashed nil
2224 org-odt-automatic-styles nil
2225 org-odt-object-counters nil
2226 org-odt-entity-counts-plist nil)
2227 content-file))
2229 (defcustom org-export-odt-prettify-xml nil
2230 "Specify whether or not the xml output should be prettified.
2231 When this option is turned on, `indent-region' is run on all
2232 component xml buffers before they are saved. Turn this off for
2233 regular use. Turn this on if you need to examine the xml
2234 visually."
2235 :group 'org-export-odt
2236 :version "24.1"
2237 :type 'boolean)
2239 (defvar hfy-user-sheet-assoc) ; bound during org-do-lparse
2240 (defun org-odt-save-as-outfile (target opt-plist)
2241 ;; write automatic styles
2242 (org-odt-write-automatic-styles)
2244 ;; write meta file
2245 (org-odt-update-meta-file opt-plist)
2247 ;; write styles file
2248 (when (equal org-lparse-backend 'odt)
2249 (org-odt-update-styles-file opt-plist))
2251 ;; create mimetype file
2252 (let ((mimetype (org-odt-write-mimetype-file org-lparse-backend)))
2253 (org-odt-create-manifest-file-entry mimetype "/" "1.2"))
2255 ;; create a manifest entry for content.xml
2256 (org-odt-create-manifest-file-entry "text/xml" "content.xml")
2258 ;; write out the manifest entries before zipping
2259 (org-odt-write-manifest-file)
2261 (let ((xml-files '("mimetype" "META-INF/manifest.xml" "content.xml"
2262 "meta.xml"))
2263 (zipdir default-directory))
2264 (when (equal org-lparse-backend 'odt)
2265 (push "styles.xml" xml-files))
2266 (message "Switching to directory %s" (expand-file-name zipdir))
2268 ;; save all xml files
2269 (mapc (lambda (file)
2270 (with-current-buffer
2271 (find-file-noselect (expand-file-name file) t)
2272 ;; prettify output if needed
2273 (when org-export-odt-prettify-xml
2274 (indent-region (point-min) (point-max)))
2275 (save-buffer 0)))
2276 xml-files)
2278 (let* ((target-name (file-name-nondirectory target))
2279 (target-dir (file-name-directory target))
2280 (cmds `(("zip" "-mX0" ,target-name "mimetype")
2281 ("zip" "-rmTq" ,target-name "."))))
2282 (when (file-exists-p target)
2283 ;; FIXME: If the file is locked this throws a cryptic error
2284 (delete-file target))
2286 (let ((coding-system-for-write 'no-conversion) exitcode err-string)
2287 (message "Creating odt file...")
2288 (mapc
2289 (lambda (cmd)
2290 (message "Running %s" (mapconcat 'identity cmd " "))
2291 (setq err-string
2292 (with-output-to-string
2293 (setq exitcode
2294 (apply 'call-process (car cmd)
2295 nil standard-output nil (cdr cmd)))))
2296 (or (zerop exitcode)
2297 (ignore (message "%s" err-string))
2298 (error "Unable to create odt file (%S)" exitcode)))
2299 cmds))
2301 ;; move the file from outdir to target-dir
2302 (rename-file target-name target-dir)
2304 ;; kill all xml buffers
2305 (mapc (lambda (file)
2306 (kill-buffer
2307 (find-file-noselect (expand-file-name file zipdir) t)))
2308 xml-files)
2310 (delete-directory zipdir)))
2311 (message "Created %s" target)
2312 (set-buffer (find-file-noselect target t)))
2314 (defconst org-odt-manifest-file-entry-tag
2316 <manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"%s/>")
2318 (defun org-odt-create-manifest-file-entry (&rest args)
2319 (push args org-odt-manifest-file-entries))
2321 (defun org-odt-write-manifest-file ()
2322 (make-directory "META-INF")
2323 (let ((manifest-file (expand-file-name "META-INF/manifest.xml")))
2324 (with-current-buffer
2325 (let ((nxml-auto-insert-xml-declaration-flag nil))
2326 (find-file-noselect manifest-file t))
2327 (insert
2328 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
2329 <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n")
2330 (mapc
2331 (lambda (file-entry)
2332 (let* ((version (nth 2 file-entry))
2333 (extra (if version
2334 (format " manifest:version=\"%s\"" version)
2335 "")))
2336 (insert
2337 (format org-odt-manifest-file-entry-tag
2338 (nth 0 file-entry) (nth 1 file-entry) extra))))
2339 org-odt-manifest-file-entries)
2340 (insert "\n</manifest:manifest>"))))
2342 (defun org-odt-update-meta-file (opt-plist)
2343 (let ((date (org-odt-format-date (plist-get opt-plist :date)))
2344 (author (or (plist-get opt-plist :author) ""))
2345 (email (plist-get opt-plist :email))
2346 (keywords (plist-get opt-plist :keywords))
2347 (description (plist-get opt-plist :description))
2348 (title (plist-get opt-plist :title)))
2349 (write-region
2350 (concat
2351 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
2352 <office:document-meta
2353 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
2354 xmlns:xlink=\"http://www.w3.org/1999/xlink\"
2355 xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
2356 xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
2357 xmlns:ooo=\"http://openoffice.org/2004/office\"
2358 office:version=\"1.2\">
2359 <office:meta>" "\n"
2360 (org-odt-format-author)
2361 (org-odt-format-tags
2362 '("\n<meta:initial-creator>" . "</meta:initial-creator>") author)
2363 (org-odt-format-tags '("\n<dc:date>" . "</dc:date>") date)
2364 (org-odt-format-tags
2365 '("\n<meta:creation-date>" . "</meta:creation-date>") date)
2366 (org-odt-format-tags '("\n<meta:generator>" . "</meta:generator>")
2367 (when org-export-creator-info
2368 (format "Org-%s/Emacs-%s"
2369 org-version emacs-version)))
2370 (org-odt-format-tags '("\n<meta:keyword>" . "</meta:keyword>") keywords)
2371 (org-odt-format-tags '("\n<dc:subject>" . "</dc:subject>") description)
2372 (org-odt-format-tags '("\n<dc:title>" . "</dc:title>") title)
2373 "\n"
2374 " </office:meta>" "</office:document-meta>")
2375 nil (expand-file-name "meta.xml")))
2377 ;; create a manifest entry for meta.xml
2378 (org-odt-create-manifest-file-entry "text/xml" "meta.xml"))
2380 (defun org-odt-update-styles-file (opt-plist)
2381 ;; write styles file
2382 (let ((styles-file (plist-get opt-plist :odt-styles-file)))
2383 (org-odt-copy-styles-file (and styles-file
2384 (read (org-trim styles-file)))))
2386 ;; Update styles.xml - take care of outline numbering
2387 (with-current-buffer
2388 (find-file-noselect (expand-file-name "styles.xml") t)
2389 ;; Don't make automatic backup of styles.xml file. This setting
2390 ;; prevents the backed-up styles.xml file from being zipped in to
2391 ;; odt file. This is more of a hackish fix. Better alternative
2392 ;; would be to fix the zip command so that the output odt file
2393 ;; includes only the needed files and excludes any auto-generated
2394 ;; extra files like backups and auto-saves etc etc. Note that
2395 ;; currently the zip command zips up the entire temp directory so
2396 ;; that any auto-generated files created under the hood ends up in
2397 ;; the resulting odt file.
2398 (set (make-local-variable 'backup-inhibited) t)
2400 ;; Import local setting of `org-export-with-section-numbers'
2401 (org-lparse-bind-local-variables opt-plist)
2402 (org-odt-configure-outline-numbering
2403 (if org-export-with-section-numbers org-export-headline-levels 0)))
2405 ;; Write custom styles for source blocks
2406 (org-odt-insert-custom-styles-for-srcblocks
2407 (mapconcat
2408 (lambda (style)
2409 (format " %s\n" (cddr style)))
2410 hfy-user-sheet-assoc "")))
2412 (defun org-odt-write-mimetype-file (format)
2413 ;; create mimetype file
2414 (let ((mimetype
2415 (case format
2416 (odt "application/vnd.oasis.opendocument.text")
2417 (odf "application/vnd.oasis.opendocument.formula")
2418 (t (error "Unknown OpenDocument backend %S" org-lparse-backend)))))
2419 (write-region mimetype nil (expand-file-name "mimetype"))
2420 mimetype))
2422 (defun org-odt-finalize-outfile ()
2423 (org-odt-delete-empty-paragraphs))
2425 (defun org-odt-delete-empty-paragraphs ()
2426 (goto-char (point-min))
2427 (let ((open "<text:p[^>]*>")
2428 (close "</text:p>"))
2429 (while (re-search-forward (format "%s[ \r\n\t]*%s" open close) nil t)
2430 (replace-match ""))))
2432 (defcustom org-export-odt-convert-processes
2433 '(("LibreOffice"
2434 "soffice --headless --convert-to %f%x --outdir %d %i")
2435 ("unoconv"
2436 "unoconv -f %f -o %d %i"))
2437 "Specify a list of document converters and their usage.
2438 The converters in this list are offered as choices while
2439 customizing `org-export-odt-convert-process'.
2441 This variable is a list where each element is of the
2442 form (CONVERTER-NAME CONVERTER-CMD). CONVERTER-NAME is the name
2443 of the converter. CONVERTER-CMD is the shell command for the
2444 converter and can contain format specifiers. These format
2445 specifiers are interpreted as below:
2447 %i input file name in full
2448 %I input file name as a URL
2449 %f format of the output file
2450 %o output file name in full
2451 %O output file name as a URL
2452 %d output dir in full
2453 %D output dir as a URL.
2454 %x extra options as set in `org-export-odt-convert-capabilities'."
2455 :group 'org-export-odt
2456 :version "24.1"
2457 :type
2458 '(choice
2459 (const :tag "None" nil)
2460 (alist :tag "Converters"
2461 :key-type (string :tag "Converter Name")
2462 :value-type (group (string :tag "Command line")))))
2464 (defcustom org-export-odt-convert-process "LibreOffice"
2465 "Use this converter to convert from \"odt\" format to other formats.
2466 During customization, the list of converter names are populated
2467 from `org-export-odt-convert-processes'."
2468 :group 'org-export-odt
2469 :version "24.1"
2470 :type '(choice :convert-widget
2471 (lambda (w)
2472 (apply 'widget-convert (widget-type w)
2473 (eval (car (widget-get w :args)))))
2474 `((const :tag "None" nil)
2475 ,@(mapcar (lambda (c)
2476 `(const :tag ,(car c) ,(car c)))
2477 org-export-odt-convert-processes))))
2479 (defcustom org-export-odt-convert-capabilities
2480 '(("Text"
2481 ("odt" "ott" "doc" "rtf" "docx")
2482 (("pdf" "pdf") ("odt" "odt") ("rtf" "rtf") ("ott" "ott")
2483 ("doc" "doc" ":\"MS Word 97\"") ("docx" "docx") ("html" "html")))
2484 ("Web"
2485 ("html")
2486 (("pdf" "pdf") ("odt" "odt") ("html" "html")))
2487 ("Spreadsheet"
2488 ("ods" "ots" "xls" "csv" "xlsx")
2489 (("pdf" "pdf") ("ots" "ots") ("html" "html") ("csv" "csv") ("ods" "ods")
2490 ("xls" "xls") ("xlsx" "xlsx")))
2491 ("Presentation"
2492 ("odp" "otp" "ppt" "pptx")
2493 (("pdf" "pdf") ("swf" "swf") ("odp" "odp") ("otp" "otp") ("ppt" "ppt")
2494 ("pptx" "pptx") ("odg" "odg"))))
2495 "Specify input and output formats of `org-export-odt-convert-process'.
2496 More correctly, specify the set of input and output formats that
2497 the user is actually interested in.
2499 This variable is an alist where each element is of the
2500 form (DOCUMENT-CLASS INPUT-FMT-LIST OUTPUT-FMT-ALIST).
2501 INPUT-FMT-LIST is a list of INPUT-FMTs. OUTPUT-FMT-ALIST is an
2502 alist where each element is of the form (OUTPUT-FMT
2503 OUTPUT-FILE-EXTENSION EXTRA-OPTIONS).
2505 The variable is interpreted as follows:
2506 `org-export-odt-convert-process' can take any document that is in
2507 INPUT-FMT-LIST and produce any document that is in the
2508 OUTPUT-FMT-LIST. A document converted to OUTPUT-FMT will have
2509 OUTPUT-FILE-EXTENSION as the file name extension. OUTPUT-FMT
2510 serves dual purposes:
2511 - It is used for populating completion candidates during
2512 `org-export-odt-convert' commands.
2513 - It is used as the value of \"%f\" specifier in
2514 `org-export-odt-convert-process'.
2516 EXTRA-OPTIONS is used as the value of \"%x\" specifier in
2517 `org-export-odt-convert-process'.
2519 DOCUMENT-CLASS is used to group a set of file formats in
2520 INPUT-FMT-LIST in to a single class.
2522 Note that this variable inherently captures how LibreOffice based
2523 converters work. LibreOffice maps documents of various formats
2524 to classes like Text, Web, Spreadsheet, Presentation etc and
2525 allow document of a given class (irrespective of it's source
2526 format) to be converted to any of the export formats associated
2527 with that class.
2529 See default setting of this variable for an typical
2530 configuration."
2531 :group 'org-export-odt
2532 :version "24.1"
2533 :type
2534 '(choice
2535 (const :tag "None" nil)
2536 (alist :tag "Capabilities"
2537 :key-type (string :tag "Document Class")
2538 :value-type
2539 (group (repeat :tag "Input formats" (string :tag "Input format"))
2540 (alist :tag "Output formats"
2541 :key-type (string :tag "Output format")
2542 :value-type
2543 (group (string :tag "Output file extension")
2544 (choice
2545 (const :tag "None" nil)
2546 (string :tag "Extra options"))))))))
2548 (declare-function org-create-math-formula "org"
2549 (latex-frag &optional mathml-file))
2551 ;;;###autoload
2552 (defun org-export-odt-convert (&optional in-file out-fmt prefix-arg)
2553 "Convert IN-FILE to format OUT-FMT using a command line converter.
2554 IN-FILE is the file to be converted. If unspecified, it defaults
2555 to variable `buffer-file-name'. OUT-FMT is the desired output
2556 format. Use `org-export-odt-convert-process' as the converter.
2557 If PREFIX-ARG is non-nil then the newly converted file is opened
2558 using `org-open-file'."
2559 (interactive
2560 (append (org-lparse-convert-read-params) current-prefix-arg))
2561 (org-lparse-do-convert in-file out-fmt prefix-arg))
2563 (defun org-odt-get (what &optional opt-plist)
2564 (case what
2565 (BACKEND 'odt)
2566 (EXPORT-DIR (org-export-directory :html opt-plist))
2567 (FILE-NAME-EXTENSION "odt")
2568 (EXPORT-BUFFER-NAME "*Org ODT Export*")
2569 (ENTITY-CONTROL org-odt-entity-control-callbacks-alist)
2570 (ENTITY-FORMAT org-odt-entity-format-callbacks-alist)
2571 (INIT-METHOD 'org-odt-init-outfile)
2572 (FINAL-METHOD 'org-odt-finalize-outfile)
2573 (SAVE-METHOD 'org-odt-save-as-outfile)
2574 (CONVERT-METHOD
2575 (and org-export-odt-convert-process
2576 (cadr (assoc-string org-export-odt-convert-process
2577 org-export-odt-convert-processes t))))
2578 (CONVERT-CAPABILITIES
2579 (and org-export-odt-convert-process
2580 (cadr (assoc-string org-export-odt-convert-process
2581 org-export-odt-convert-processes t))
2582 org-export-odt-convert-capabilities))
2583 (TOPLEVEL-HLEVEL 1)
2584 (SPECIAL-STRING-REGEXPS org-export-odt-special-string-regexps)
2585 (INLINE-IMAGES 'maybe)
2586 (INLINE-IMAGE-EXTENSIONS '("png" "jpeg" "jpg" "gif" "svg"))
2587 (PLAIN-TEXT-MAP '(("&" . "&amp;") ("<" . "&lt;") (">" . "&gt;")))
2588 (TABLE-FIRST-COLUMN-AS-LABELS nil)
2589 (FOOTNOTE-SEPARATOR (org-lparse-format 'FONTIFY "," 'superscript))
2590 (CODING-SYSTEM-FOR-WRITE 'utf-8)
2591 (CODING-SYSTEM-FOR-SAVE 'utf-8)
2592 (t (error "Unknown property: %s" what))))
2594 (defvar org-lparse-latex-fragment-fallback) ; set by org-do-lparse
2595 (defun org-export-odt-do-preprocess-latex-fragments ()
2596 "Convert LaTeX fragments to images."
2597 (let* ((latex-frag-opt (plist-get org-lparse-opt-plist :LaTeX-fragments))
2598 (latex-frag-opt ; massage the options
2599 (or (and (member latex-frag-opt '(mathjax t))
2600 (not (and (fboundp 'org-format-latex-mathml-available-p)
2601 (org-format-latex-mathml-available-p)))
2602 (prog1 org-lparse-latex-fragment-fallback
2603 (org-lparse-warn
2604 (concat
2605 "LaTeX to MathML converter not available. "
2606 (format "Using %S instead."
2607 org-lparse-latex-fragment-fallback)))))
2608 latex-frag-opt))
2609 cache-dir display-msg)
2610 (cond
2611 ((eq latex-frag-opt 'dvipng)
2612 (setq cache-dir "ltxpng/")
2613 (setq display-msg "Creating LaTeX image %s"))
2614 ((member latex-frag-opt '(mathjax t))
2615 (setq latex-frag-opt 'mathml)
2616 (setq cache-dir "ltxmathml/")
2617 (setq display-msg "Creating MathML formula %s")))
2618 (when (and org-current-export-file)
2619 (org-format-latex
2620 (concat cache-dir (file-name-sans-extension
2621 (file-name-nondirectory org-current-export-file)))
2622 org-current-export-dir nil display-msg
2623 nil nil latex-frag-opt))))
2625 (defadvice org-format-latex-as-mathml
2626 (after org-odt-protect-latex-fragment activate)
2627 "Encode LaTeX fragment as XML.
2628 Do this when translation to MathML fails."
2629 (when (or (not (> (length ad-return-value) 0))
2630 (get-text-property 0 'org-protected ad-return-value))
2631 (setq ad-return-value
2632 (org-propertize (org-odt-encode-plain-text (ad-get-arg 0))
2633 'org-protected t))))
2635 (defun org-export-odt-preprocess-latex-fragments ()
2636 (when (equal org-export-current-backend 'odt)
2637 (org-export-odt-do-preprocess-latex-fragments)))
2639 (defun org-export-odt-preprocess-label-references ()
2640 (goto-char (point-min))
2641 (let (label label-components category value pretty-label)
2642 (while (re-search-forward "\\\\ref{\\([^{}\n]+\\)}" nil t)
2643 (org-if-unprotected-at (match-beginning 1)
2644 (replace-match
2645 (let ((org-lparse-encode-pending t)
2646 (label (match-string 1)))
2647 ;; markup generated below is mostly an eye-candy. At
2648 ;; pre-processing stage, there is no information on which
2649 ;; entity a label reference points to. The actual markup
2650 ;; is generated as part of `org-odt-fixup-label-references'
2651 ;; which gets called at the fag end of export. By this
2652 ;; time we would have seen and collected all the label
2653 ;; definitions in `org-odt-entity-labels-alist'.
2654 (org-odt-format-tags
2655 '("<text:sequence-ref text:ref-name=\"%s\">" .
2656 "</text:sequence-ref>")
2657 "" (org-add-props label '(org-protected t)))) t t)))))
2659 ;; process latex fragments as part of
2660 ;; `org-export-preprocess-after-blockquote-hook'. Note that this hook
2661 ;; is the one that is closest and well before the call to
2662 ;; `org-export-attach-captions-and-attributes' in
2663 ;; `org-export-preprocess-string'. The above arrangement permits
2664 ;; captions, labels and attributes to be attached to png images
2665 ;; generated out of latex equations.
2666 (add-hook 'org-export-preprocess-after-blockquote-hook
2667 'org-export-odt-preprocess-latex-fragments)
2669 (defun org-export-odt-preprocess (parameters)
2670 (org-export-odt-preprocess-label-references))
2672 (declare-function archive-zip-extract "arc-mode.el" (archive name))
2673 (defun org-odt-zip-extract-one (archive member &optional target)
2674 (require 'arc-mode)
2675 (let* ((target (or target default-directory))
2676 (archive (expand-file-name archive))
2677 (archive-zip-extract
2678 (list "unzip" "-qq" "-o" "-d" target))
2679 exit-code command-output)
2680 (setq command-output
2681 (with-temp-buffer
2682 (setq exit-code (archive-zip-extract archive member))
2683 (buffer-string)))
2684 (unless (zerop exit-code)
2685 (message command-output)
2686 (error "Extraction failed"))))
2688 (defun org-odt-zip-extract (archive members &optional target)
2689 (when (atom members) (setq members (list members)))
2690 (mapc (lambda (member)
2691 (org-odt-zip-extract-one archive member target))
2692 members))
2694 (defun org-odt-copy-styles-file (&optional styles-file)
2695 ;; Non-availability of styles.xml is not a critical error. For now
2696 ;; throw an error purely for aesthetic reasons.
2697 (setq styles-file (or styles-file
2698 org-export-odt-styles-file
2699 (expand-file-name "OrgOdtStyles.xml"
2700 org-odt-styles-dir)
2701 (error "org-odt: Missing styles file?")))
2702 (cond
2703 ((listp styles-file)
2704 (let ((archive (nth 0 styles-file))
2705 (members (nth 1 styles-file)))
2706 (org-odt-zip-extract archive members)
2707 (mapc
2708 (lambda (member)
2709 (when (org-file-image-p member)
2710 (let* ((image-type (file-name-extension member))
2711 (media-type (format "image/%s" image-type)))
2712 (org-odt-create-manifest-file-entry media-type member))))
2713 members)))
2714 ((and (stringp styles-file) (file-exists-p styles-file))
2715 (let ((styles-file-type (file-name-extension styles-file)))
2716 (cond
2717 ((string= styles-file-type "xml")
2718 (copy-file styles-file "styles.xml" t))
2719 ((member styles-file-type '("odt" "ott"))
2720 (org-odt-zip-extract styles-file "styles.xml")))))
2722 (error (format "Invalid specification of styles.xml file: %S"
2723 org-export-odt-styles-file))))
2725 ;; create a manifest entry for styles.xml
2726 (org-odt-create-manifest-file-entry "text/xml" "styles.xml"))
2728 (defun org-odt-configure-outline-numbering (level)
2729 "Outline numbering is retained only upto LEVEL.
2730 To disable outline numbering pass a LEVEL of 0."
2731 (goto-char (point-min))
2732 (let ((regex
2733 "<text:outline-level-style\\([^>]*\\)text:level=\"\\([^\"]*\\)\"\\([^>]*\\)>")
2734 (replacement
2735 "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
2736 (while (re-search-forward regex nil t)
2737 (when (> (string-to-number (match-string 2)) level)
2738 (replace-match replacement t nil))))
2739 (save-buffer 0))
2741 ;;;###autoload
2742 (defun org-export-as-odf (latex-frag &optional odf-file)
2743 "Export LATEX-FRAG as OpenDocument formula file ODF-FILE.
2744 Use `org-create-math-formula' to convert LATEX-FRAG first to
2745 MathML. When invoked as an interactive command, use
2746 `org-latex-regexps' to infer LATEX-FRAG from currently active
2747 region. If no LaTeX fragments are found, prompt for it. Push
2748 MathML source to kill ring, if `org-export-copy-to-kill-ring' is
2749 non-nil."
2750 (interactive
2751 `(,(let (frag)
2752 (setq frag (and (setq frag (and (region-active-p)
2753 (buffer-substring (region-beginning)
2754 (region-end))))
2755 (loop for e in org-latex-regexps
2756 thereis (when (string-match (nth 1 e) frag)
2757 (match-string (nth 2 e) frag)))))
2758 (read-string "LaTeX Fragment: " frag nil frag))
2759 ,(let ((odf-filename (expand-file-name
2760 (concat
2761 (file-name-sans-extension
2762 (or (file-name-nondirectory buffer-file-name)))
2763 "." "odf")
2764 (file-name-directory buffer-file-name))))
2765 (read-file-name "ODF filename: " nil odf-filename nil
2766 (file-name-nondirectory odf-filename)))))
2767 (let* ((org-lparse-backend 'odf)
2768 org-lparse-opt-plist
2769 (filename (or odf-file
2770 (expand-file-name
2771 (concat
2772 (file-name-sans-extension
2773 (or (file-name-nondirectory buffer-file-name)))
2774 "." "odf")
2775 (file-name-directory buffer-file-name))))
2776 (buffer (find-file-noselect (org-odt-init-outfile filename)))
2777 (coding-system-for-write 'utf-8)
2778 (save-buffer-coding-system 'utf-8))
2779 (set-buffer buffer)
2780 (set-buffer-file-coding-system coding-system-for-write)
2781 (let ((mathml (org-create-math-formula latex-frag)))
2782 (unless mathml (error "No Math formula created"))
2783 (insert mathml)
2784 (or (org-export-push-to-kill-ring
2785 (upcase (symbol-name org-lparse-backend)))
2786 (message "Exporting... done")))
2787 (org-odt-save-as-outfile filename nil)))
2789 ;;;###autoload
2790 (defun org-export-as-odf-and-open ()
2791 "Export LaTeX fragment as OpenDocument formula and immediately open it.
2792 Use `org-export-as-odf' to read LaTeX fragment and OpenDocument
2793 formula file."
2794 (interactive)
2795 (org-lparse-and-open
2796 nil nil nil (call-interactively 'org-export-as-odf)))
2798 (provide 'org-odt)
2800 ;;; org-odt.el ends here