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.
This commit is contained in:
Richard Hansen 2022-12-18 23:04:00 -05:00 committed by Eli Zaretskii
parent a75d1da911
commit 64163618d2
2 changed files with 26 additions and 12 deletions

View file

@ -2268,10 +2268,11 @@ Highlighting those lines can be distracting.)"
(save-excursion (goto-char whitespace-point) (save-excursion (goto-char whitespace-point)
(line-beginning-position))))) (line-beginning-position)))))
(when (= p 1) (when (= p 1)
;; See the comment in `whitespace--update-bob-eob' for why this (with-silent-modifications
;; text property is added here. ;; See the comment in `whitespace--update-bob-eob' for why
;; this text property is added here.
(put-text-property 1 whitespace-bob-marker (put-text-property 1 whitespace-bob-marker
'font-lock-multiline t)) 'font-lock-multiline t)))
(when (< p e) (when (< p e)
(set-match-data (list p e)) (set-match-data (list p e))
(goto-char 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 empty lines will no longer be EoB empty lines. Highlighting
those lines can be distracting.)" those lines can be distracting.)"
(when (= limit (1+ (buffer-size))) (when (= limit (1+ (buffer-size)))
(with-silent-modifications
;; See the comment in `whitespace--update-bob-eob' for why this ;; See the comment in `whitespace--update-bob-eob' for why this
;; text property is added here. ;; text property is added here.
(put-text-property whitespace-eob-marker limit (put-text-property whitespace-eob-marker limit
'font-lock-multiline t)) 'font-lock-multiline t)))
(let ((b (max (point) whitespace-eob-marker (let ((b (max (point) whitespace-eob-marker
whitespace-bob-marker ; See comment in the bob func. whitespace-bob-marker ; See comment in the bob func.
(save-excursion (goto-char whitespace-point) (save-excursion (goto-char whitespace-point)
@ -2437,8 +2439,9 @@ purposes)."
(save-match-data (save-match-data
(when (looking-at whitespace-empty-at-bob-regexp) (when (looking-at whitespace-empty-at-bob-regexp)
(set-marker whitespace-bob-marker (match-end 1)) (set-marker whitespace-bob-marker (match-end 1))
(with-silent-modifications
(put-text-property (match-beginning 1) (match-end 1) (put-text-property (match-beginning 1) (match-end 1)
'font-lock-multiline t)))) 'font-lock-multiline t)))))
(when (or (null end) (when (or (null end)
(>= end (save-excursion (>= end (save-excursion
(goto-char whitespace-eob-marker) (goto-char whitespace-eob-marker)
@ -2451,8 +2454,9 @@ purposes)."
(when (whitespace--looking-back (when (whitespace--looking-back
whitespace-empty-at-eob-regexp) whitespace-empty-at-eob-regexp)
(set-marker whitespace-eob-marker (match-beginning 1)) (set-marker whitespace-eob-marker (match-beginning 1))
(with-silent-modifications
(put-text-property (match-beginning 1) (match-end 1) (put-text-property (match-beginning 1) (match-end 1)
'font-lock-multiline t))))))))) 'font-lock-multiline t))))))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

View file

@ -327,6 +327,16 @@ buffer's content."
"«:whitespace-empty:\n" "«:whitespace-empty:\n"
"»"))))) "»")))))
(ert-deftest whitespace-tests--empty-bob-eob-modified ()
"Regression test for Bug#60066."
(whitespace-tests--with-test-buffer '()
(insert "\nx\n\n")
(goto-char 2)
(set-buffer-modified-p nil)
(let ((whitespace-style '(face empty)))
(whitespace-mode 1)
(should (not (buffer-modified-p))))))
(provide 'whitespace-tests) (provide 'whitespace-tests)
;;; whitespace-tests.el ends here ;;; whitespace-tests.el ends here