summaryrefslogtreecommitdiff
path: root/lisp/whitespace.el
diff options
context:
space:
mode:
authorRichard Hansen <rhansen@rhansen.org>2022-12-13 01:33:43 -0500
committerEli Zaretskii <eliz@gnu.org>2022-12-21 16:02:30 +0200
commitd76d7a3bebf1ff0b06a38f7f96d316752844ed10 (patch)
treea58b2418407d023fdad97f7c14e806f5b1416bdb /lisp/whitespace.el
parent12b2b8864c295ce27594e8a907ebb3423e58a9d4 (diff)
downloademacs-d76d7a3bebf1ff0b06a38f7f96d316752844ed10.tar.gz
whitespace: Avoid mutating original buffer's markers in clones
* lisp/whitespace.el (whitespace--clone): New hook function that is run after cloning a buffer that copies `whitespace-bob-marker' and `whitespace-eob-marker' and changes the copies to point to the new buffer (Bug#59618). (whitespace-color-on): Register the hook function. (whitespace-color-off): Unregister the hook function. * test/lisp/whitespace-tests.el (whitespace-tests--with-test-buffer): New macro. (whitespace-tests--check-markers): New function. (whitespace-tests--indirect-clone-breaks-base-markers) (whitespace-tests--indirect-clone-markers) (whitespace-tests--regular-clone-markers): New tests.
Diffstat (limited to 'lisp/whitespace.el')
-rw-r--r--lisp/whitespace.el15
1 files changed, 15 insertions, 0 deletions
diff --git a/lisp/whitespace.el b/lisp/whitespace.el
index 9bc6ad9db46..558be1841ab 100644
--- a/lisp/whitespace.el
+++ b/lisp/whitespace.el
@@ -2093,6 +2093,17 @@ resultant list will be returned."
t))
+(defun whitespace--clone ()
+ "Hook function run after `make-indirect-buffer' and `clone-buffer'."
+ (when (whitespace-style-face-p)
+ (setq-local whitespace-bob-marker
+ (copy-marker (marker-position whitespace-bob-marker)
+ (marker-insertion-type whitespace-bob-marker)))
+ (setq-local whitespace-eob-marker
+ (copy-marker (marker-position whitespace-eob-marker)
+ (marker-insertion-type whitespace-eob-marker)))))
+
+
(defun whitespace-color-on ()
"Turn on color visualization."
(when (whitespace-style-face-p)
@@ -2111,6 +2122,8 @@ resultant list will be returned."
;; The -1 ensures that it runs before any
;; `font-lock-mode' hook functions.
-1 t)
+ (add-hook 'clone-buffer-hook #'whitespace--clone nil t)
+ (add-hook 'clone-indirect-buffer-hook #'whitespace--clone nil t)
;; Add whitespace-mode color into font lock.
(setq
whitespace-font-lock-keywords
@@ -2204,6 +2217,8 @@ resultant list will be returned."
(remove-hook 'before-change-functions #'whitespace-buffer-changed t)
(remove-hook 'after-change-functions #'whitespace--update-bob-eob
t)
+ (remove-hook 'clone-buffer-hook #'whitespace--clone t)
+ (remove-hook 'clone-indirect-buffer-hook #'whitespace--clone t)
(font-lock-remove-keywords nil whitespace-font-lock-keywords)
(font-lock-flush)))