Hello, David Lukeš <dafydd.lu...@gmail.com> writes:
> So I think Org should try to protect the export buffer from these > shenanigans as much as possible. The best way I can think of to achieve > that is to keep the export buffer in fundamental mode. This should > prevent all the mode-related code from running, potentially even making > the export speedier. > > After some experimenting, the way I finally got this to work was by > locally overriding the `set-auto-mode’ function: > > ┌──── > │ diff --git a/lisp/ox.el b/lisp/ox.el > │ index 2a3edaa50..d5a77c26e 100644 > │ --- a/lisp/ox.el > │ +++ b/lisp/ox.el > │ @@ -6462,14 +6462,16 @@ or FILE." > │ ',ext-plist))) > │ (with-temp-buffer > │ (insert output) > │ - (let ((coding-system-for-write ',encoding)) > │ + (cl-letf ((coding-system-for-write ',encoding) > │ + ((symbol-function 'set-auto-mode) #'ignore)) > │ (write-file ,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)) > │ + (cl-letf ((coding-system-for-write encoding) > │ + ((symbol-function 'set-auto-mode) #'ignore)) > │ (write-file file))) > │ (when (and (org-export--copy-to-kill-ring-p) (org-string-nw-p > output)) > │ (org-kill-new output)) > └──── > > What are your thoughts? Is this desirable? In my mind, yes: even if > someone has configured auto-formatting manually, they might still not > realize it’s getting run in a hidden buffer they never get to see. After > all, this is an implementation detail – if Emacs had a built-in function > to write a string to a file, I presume that would get used instead? > > (In a way, I was lucky that I encountered quite noticeable issues with > mangled characters. With the recent version of tidy, which only messes > up whitespace, I might not have noticed at all.) > > And if such protection is desirable, is this the best way to do it? Or > can you come up with a better approach? What about using `write-region' instead of `write-file' and not touching `set-auto-mode' function? Could you test it? Note the arguments are different, the equivalent to (write-file file) would be (write-region nil nil file). Regards, -- Nicolas Goaziou