emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: David Lukes <dafydd.lukes@gmail.com>
To: emacs-orgmode@gnu.org
Cc: David Lukes <dafydd.lukes@gmail.com>
Subject: [PATCH] ox: Prevent auto-formatting in export buffers
Date: Mon, 28 Feb 2022 15:07:50 +0100	[thread overview]
Message-ID: <20220228140750.75761-1-dafydd.lukes@gmail.com> (raw)

* ox.el (org-export-to-file): `write-region' instead of  `write-file'.

* ox-odt.el (org-odt-template, org-odt--export-wrap): `write-region'
instead of `save-buffer'.

`write-file' and `save-buffer' trigger major mode changes, which leads
to various mode-related hooks being run.  This is undesirable: running
these on generated files is wasted time and computation, and it can even
lead to hard to track data corruption when auto-formatting hooks are
involved.  One such case is the 2006 version of the tidy program which
ships with stock macOS and can corrupt multi-byte UTF-8 codepoints in
HTML and ODT (via XML) exports.  And even recent versions of tidy can
re-arrange whitespace in the exported documents in unwanted ways.

TINYCHANGE
---
 lisp/ox-odt.el |  4 ++--
 lisp/ox.el     | 16 ++++++----------
 2 files changed, 8 insertions(+), 12 deletions(-)

diff --git a/lisp/ox-odt.el b/lisp/ox-odt.el
index 7f2e8ba47..6a8e75e9d 100644
--- a/lisp/ox-odt.el
+++ b/lisp/ox-odt.el
@@ -1427,7 +1427,7 @@ original parsed data.  INFO is a plist holding export options."
 			(level (string-to-number (match-string 2))))
 		    (if (wholenump sec-num) (<= level sec-num) sec-num))
 	    (replace-match replacement t nil))))
-      (save-buffer 0)))
+      (write-region nil nil buffer-file-name)))
   ;; Update content.xml.
 
   (let* ( ;; `org-display-custom-times' should be accessed right
@@ -4018,7 +4018,7 @@ contextual information."
 		   ;; Prettify output if needed.
 		   (when org-odt-prettify-xml
 		     (indent-region (point-min) (point-max)))
-		   (save-buffer 0)))))
+		   (write-region nil nil buffer-file-name)))))
 	   ;; Run zip.
 	   (let* ((target --out-file)
 		  (target-name (file-name-nondirectory target))
diff --git a/lisp/ox.el b/lisp/ox.el
index 2a3edaa50..8ec1e25ee 100644
--- a/lisp/ox.el
+++ b/lisp/ox.el
@@ -6459,18 +6459,14 @@ or FILE."
 	    `(let ((output
 		    (org-export-as
 		     ',backend ,subtreep ,visible-only ,body-only
-		     ',ext-plist)))
-	       (with-temp-buffer
-		 (insert output)
-		 (let ((coding-system-for-write ',encoding))
-		   (write-file ,file)))
+		     ',ext-plist))
+		   (coding-system-for-write ',encoding))
+	       (write-region output nil ,file)
 	       (or (ignore-errors (funcall ',post-process ,file)) ,file)))
         (let ((output (org-export-as
-                       backend subtreep visible-only body-only ext-plist)))
-          (with-temp-buffer
-            (insert output)
-            (let ((coding-system-for-write encoding))
-	      (write-file file)))
+                       backend subtreep visible-only body-only ext-plist))
+              (coding-system-for-write encoding))
+          (write-region output nil file)
           (when (and (org-export--copy-to-kill-ring-p) (org-string-nw-p output))
             (org-kill-new output))
           ;; Get proper return value.
-- 
2.34.1



                 reply	other threads:[~2022-02-28 14:26 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220228140750.75761-1-dafydd.lukes@gmail.com \
    --to=dafydd.lukes@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).