From 64163618d21bfa31e56b47c813ce50681c3d3556 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Sun, 18 Dec 2022 23:04:00 -0500 Subject: whitespace: Fix unintended change in buffer modification status * lisp/whitespace.el (whitespace--empty-at-bob-matcher) whitespace--empty-at-eob-matcher, whitespace--update-bob-eob): Silently add the `font-lock-multiline' text property when highlighting beginning-of-buffer and end-of-buffer empty lines to prevent Emacs from running modification hooks or considering the buffer to be modified (Bug#60066). * test/lisp/whitespace-tests.el (whitespace-tests--empty-bob-eob-modified): Add a regression test. --- lisp/whitespace.el | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) (limited to 'lisp/whitespace.el') diff --git a/lisp/whitespace.el b/lisp/whitespace.el index 25ea07e9db7..9bc6ad9db46 100644 --- a/lisp/whitespace.el +++ b/lisp/whitespace.el @@ -2268,10 +2268,11 @@ Highlighting those lines can be distracting.)" (save-excursion (goto-char whitespace-point) (line-beginning-position))))) (when (= p 1) - ;; See the comment in `whitespace--update-bob-eob' for why this - ;; text property is added here. - (put-text-property 1 whitespace-bob-marker - 'font-lock-multiline t)) + (with-silent-modifications + ;; See the comment in `whitespace--update-bob-eob' for why + ;; this text property is added here. + (put-text-property 1 whitespace-bob-marker + 'font-lock-multiline t))) (when (< p e) (set-match-data (list p e)) (goto-char e)))) @@ -2292,10 +2293,11 @@ about to start typing, and if they do, that line and previous empty lines will no longer be EoB empty lines. Highlighting those lines can be distracting.)" (when (= limit (1+ (buffer-size))) - ;; See the comment in `whitespace--update-bob-eob' for why this - ;; text property is added here. - (put-text-property whitespace-eob-marker limit - 'font-lock-multiline t)) + (with-silent-modifications + ;; See the comment in `whitespace--update-bob-eob' for why this + ;; text property is added here. + (put-text-property whitespace-eob-marker limit + 'font-lock-multiline t))) (let ((b (max (point) whitespace-eob-marker whitespace-bob-marker ; See comment in the bob func. (save-excursion (goto-char whitespace-point) @@ -2437,8 +2439,9 @@ purposes)." (save-match-data (when (looking-at whitespace-empty-at-bob-regexp) (set-marker whitespace-bob-marker (match-end 1)) - (put-text-property (match-beginning 1) (match-end 1) - 'font-lock-multiline t)))) + (with-silent-modifications + (put-text-property (match-beginning 1) (match-end 1) + 'font-lock-multiline t))))) (when (or (null end) (>= end (save-excursion (goto-char whitespace-eob-marker) @@ -2451,8 +2454,9 @@ purposes)." (when (whitespace--looking-back whitespace-empty-at-eob-regexp) (set-marker whitespace-eob-marker (match-beginning 1)) - (put-text-property (match-beginning 1) (match-end 1) - 'font-lock-multiline t))))))))) + (with-silent-modifications + (put-text-property (match-beginning 1) (match-end 1) + 'font-lock-multiline t)))))))))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -- cgit v1.2.1 From d76d7a3bebf1ff0b06a38f7f96d316752844ed10 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Tue, 13 Dec 2022 01:33:43 -0500 Subject: 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. --- lisp/whitespace.el | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'lisp/whitespace.el') 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))) -- cgit v1.2.1