org-odt.el: Cleanup temporary XML buffers after export

* lisp/org-odt.el (org-odt-zip-dir)
(org-odt-cleanup-xml-buffers): New.
(org-export-as-odt-and-open, org-export-as-odt)
(org-odt-init-outfile, org-odt-save-as-outfile)
(org-export-as-odf, org-export-as-odf-and-open): Use
`org-odt-cleanup-xml-buffers'.
This commit is contained in:
Jambunathan K 2012-07-01 15:18:20 +05:30
parent 86ce84643e
commit af6cb0bd35
1 changed files with 60 additions and 40 deletions

View File

@ -326,6 +326,8 @@ a per-file basis. For example,
(defconst org-export-odt-tmpdir-prefix "%s-") (defconst org-export-odt-tmpdir-prefix "%s-")
(defconst org-export-odt-bookmark-prefix "OrgXref.") (defconst org-export-odt-bookmark-prefix "OrgXref.")
(defvar org-odt-zip-dir nil
"Temporary directory that holds XML files during export.")
(defvar org-export-odt-embed-images t (defvar org-export-odt-embed-images t
"Should the images be copied in to the odt file or just linked?") "Should the images be copied in to the odt file or just linked?")
@ -426,6 +428,33 @@ variable, the list of valid values are populated based on
`(const :tag ,c ,c)) `(const :tag ,c ,c))
(org-lparse-reachable-formats "odt"))))) (org-lparse-reachable-formats "odt")))))
(defmacro org-odt-cleanup-xml-buffers (&rest body)
`(let ((org-odt-zip-dir
(make-temp-file
(format org-export-odt-tmpdir-prefix "odf") t))
(--cleanup-xml-buffers
(function
(lambda nil
(let ((xml-files '("mimetype" "META-INF/manifest.xml" "content.xml"
"meta.xml" "styles.xml")))
;; kill all xml buffers
(mapc (lambda (file)
(let ((buf (find-file-noselect
(expand-file-name file org-odt-zip-dir) t)))
(when (buffer-name buf)
(set-buffer-modified-p nil)
(kill-buffer buf))))
xml-files))
;; delete temporary directory.
(delete-directory org-odt-zip-dir t)))))
(condition-case-unless-debug err
(prog1 (progn ,@body)
(funcall --cleanup-xml-buffers))
((quit error)
(funcall --cleanup-xml-buffers)
(message "OpenDocument export failed: %s"
(error-message-string err))))))
;;;###autoload ;;;###autoload
(defun org-export-as-odt-and-open (arg) (defun org-export-as-odt-and-open (arg)
"Export the outline as ODT and immediately open it with a browser. "Export the outline as ODT and immediately open it with a browser.
@ -433,8 +462,9 @@ If there is an active region, export only the region.
The prefix ARG specifies how many levels of the outline should become The prefix ARG specifies how many levels of the outline should become
headlines. The default is 3. Lower levels will become bulleted lists." headlines. The default is 3. Lower levels will become bulleted lists."
(interactive "P") (interactive "P")
(org-odt-cleanup-xml-buffers
(org-lparse-and-open (org-lparse-and-open
(or org-export-odt-preferred-output-format "odt") "odt" arg)) (or org-export-odt-preferred-output-format "odt") "odt" arg)))
;;;###autoload ;;;###autoload
(defun org-export-as-odt-batch () (defun org-export-as-odt-batch ()
@ -465,8 +495,9 @@ the file header and footer, simply return the content of
<body>...</body>, without even the body tags themselves. When <body>...</body>, without even the body tags themselves. When
PUB-DIR is set, use this as the publishing directory." PUB-DIR is set, use this as the publishing directory."
(interactive "P") (interactive "P")
(org-odt-cleanup-xml-buffers
(org-lparse (or org-export-odt-preferred-output-format "odt") (org-lparse (or org-export-odt-preferred-output-format "odt")
"odt" arg hidden ext-plist to-buffer body-only pub-dir)) "odt" arg hidden ext-plist to-buffer body-only pub-dir)))
(defvar org-odt-entity-control-callbacks-alist (defvar org-odt-entity-control-callbacks-alist
`((EXPORT `((EXPORT
@ -2207,10 +2238,7 @@ captions on export.")
;; Not at all OSes ship with zip by default ;; Not at all OSes ship with zip by default
(error "Executable \"zip\" needed for creating OpenDocument files")) (error "Executable \"zip\" needed for creating OpenDocument files"))
(let* ((outdir (make-temp-file (let* ((content-file (expand-file-name "content.xml" org-odt-zip-dir)))
(format org-export-odt-tmpdir-prefix org-lparse-backend) t))
(content-file (expand-file-name "content.xml" outdir)))
;; init conten.xml ;; init conten.xml
(require 'nxml-mode) (require 'nxml-mode)
(let ((nxml-auto-insert-xml-declaration-flag nil)) (let ((nxml-auto-insert-xml-declaration-flag nil))
@ -2260,11 +2288,9 @@ visually."
(org-odt-write-manifest-file) (org-odt-write-manifest-file)
(let ((xml-files '("mimetype" "META-INF/manifest.xml" "content.xml" (let ((xml-files '("mimetype" "META-INF/manifest.xml" "content.xml"
"meta.xml")) "meta.xml")))
(zipdir default-directory))
(when (equal org-lparse-backend 'odt) (when (equal org-lparse-backend 'odt)
(push "styles.xml" xml-files)) (push "styles.xml" xml-files))
(message "Switching to directory %s" (expand-file-name zipdir))
;; save all xml files ;; save all xml files
(mapc (lambda (file) (mapc (lambda (file)
@ -2300,15 +2326,8 @@ visually."
cmds)) cmds))
;; move the file from outdir to target-dir ;; move the file from outdir to target-dir
(rename-file target-name target-dir) (rename-file target-name target-dir)))
;; kill all xml buffers
(mapc (lambda (file)
(kill-buffer
(find-file-noselect (expand-file-name file zipdir) t)))
xml-files)
(delete-directory zipdir)))
(message "Created %s" target) (message "Created %s" target)
(set-buffer (find-file-noselect target t))) (set-buffer (find-file-noselect target t)))
@ -2765,6 +2784,7 @@ non-nil."
(file-name-directory buffer-file-name)))) (file-name-directory buffer-file-name))))
(read-file-name "ODF filename: " nil odf-filename nil (read-file-name "ODF filename: " nil odf-filename nil
(file-name-nondirectory odf-filename))))) (file-name-nondirectory odf-filename)))))
(org-odt-cleanup-xml-buffers
(let* ((org-lparse-backend 'odf) (let* ((org-lparse-backend 'odf)
org-lparse-opt-plist org-lparse-opt-plist
(filename (or odf-file (filename (or odf-file
@ -2785,7 +2805,7 @@ non-nil."
(or (org-export-push-to-kill-ring (or (org-export-push-to-kill-ring
(upcase (symbol-name org-lparse-backend))) (upcase (symbol-name org-lparse-backend)))
(message "Exporting... done"))) (message "Exporting... done")))
(org-odt-save-as-outfile filename nil))) (org-odt-save-as-outfile filename nil))))
;;;###autoload ;;;###autoload
(defun org-export-as-odf-and-open () (defun org-export-as-odf-and-open ()