> buffer-modified-p is not the only parameter affected by juggling around > the local variables. There will be undo history, > buffer-chars-modified-tick, before/after-change-hooks triggered by > `org-preserve-local-variables', etc. > > However, I do not see any obvious way how your proposed change can > negatively affect all the above. Feel free to propose a patch.
I've written a patch proposal. It deals with buffer-modified-p and undo history, but not the other two points you mention. I have tested it and it works, but I had never dealt before with `buffer-undo-list' so maybe there are some cases that I have not considered and for which this patch could be problematic. Let me know what you think:
>From 14506bea13bf6278d95825257d90bbc3390ae8f1 Mon Sep 17 00:00:00 2001 From: Ignacio Casso <ignacioca...@hotmail.com> Date: Sun, 12 Jun 2022 10:38:53 +0200 Subject: [PATCH] Do not mark buffer as modified with org-preserve-local-variables * lisp/org-macs.el (org-preserve-local-variables): Do not mark buffer as modified or alter `buffer-undo-list' when body does not actually modify the buffer. This commit fixes a bug with `org-copy-subtree', which marked the buffer as modified and added an entry to the undo list when the visited file had local variables. --- lisp/org-macs.el | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lisp/org-macs.el b/lisp/org-macs.el index 19e5f42e9..64beeff53 100644 --- a/lisp/org-macs.el +++ b/lisp/org-macs.el @@ -163,16 +163,24 @@ (org-with-wide-buffer (goto-char (point-max)) (let ((case-fold-search t)) - (and (re-search-backward "^[ \t]*# +Local Variables:" + (when (re-search-backward "^[ \t]*# +Local Variables:" (max (- (point) 3000) 1) t) - (delete-and-extract-region (point) (point-max))))))) + (undo-boundary) + (delete-and-extract-region (point) (point-max)))))) + (tick-counter (buffer-modified-tick)) + modified) (unwind-protect (progn ,@body) (when local-variables + (setq modified (< tick-counter (buffer-modified-tick))) (org-with-wide-buffer (goto-char (point-max)) (unless (bolp) (insert "\n")) - (insert local-variables)))))) + (insert local-variables)) + (unless modified + (set-buffer-modified-p nil) + (setq buffer-undo-list + (seq-drop-while 'identity buffer-undo-list))))))) (defmacro org-no-popups (&rest body) "Suppress popup windows and evaluate BODY." -- 2.25.1