branch: scratch/editorconfig
commit 964747660cacf01799512e28c8999afea345203e
Author: Stefan Monnier <[email protected]>
Commit: Stefan Monnier <[email protected]>
Don't hook into `read-only-mode-hook`
We shouldn't re-set variables just because `read-only-mode` is
(de)activated,
and with Emacs-30's hooks it would be even less natural to do.
Also `buffer-read-only` can change without calling `read-only-mode`,
so better test it dynamically when we try to trim whitespace.
* editorconfig.el (editorconfig--delete-trailing-whitespace): New function.
(editorconfig-set-trailing-ws): Use it.
Also prefer `before-save-hook` and use `add/remove-hook` to
manipulate hooks, like god intended.
Don't test `buffer-read-only` any more.
(editorconfig-mode): Don't hook into `read-only-mode-hook` any more.
* ert-tests/editorconfig.el (test-trim-trailing-ws): Adjust
test accordingly.
---
editorconfig.el | 19 ++++++++++---------
ert-tests/editorconfig.el | 15 +++++++++------
2 files changed, 19 insertions(+), 15 deletions(-)
diff --git a/editorconfig.el b/editorconfig.el
index cf6c03249a..66e99c4185 100644
--- a/editorconfig.el
+++ b/editorconfig.el
@@ -612,24 +612,26 @@ to non-nil when FINAL-NEWLINE is true."
(setq-local require-final-newline nil)
(setq-local mode-require-final-newline nil))))
+(defun editorconfig--delete-trailing-whitespace ()
+ "Call `delete-trailing-whitespace' unless the buffer is read-only."
+ (unless buffer-read-only (delete-trailing-whitespace)))
+
(defun editorconfig-set-trailing-ws (trim-trailing-ws)
"Set up trimming of trailing whitespace at end of lines by TRIM-TRAILING-WS."
- (make-local-variable 'write-file-functions) ;; just current buffer
- (when (and (equal trim-trailing-ws "true")
- (not buffer-read-only))
+ (when (equal trim-trailing-ws "true")
;; when true we push delete-trailing-whitespace (emacs > 21)
;; to write-file-functions
(if editorconfig-trim-whitespaces-mode
(funcall editorconfig-trim-whitespaces-mode 1)
- (add-to-list 'write-file-functions 'delete-trailing-whitespace)))
- (when (or (equal trim-trailing-ws "false")
- buffer-read-only)
+ (add-hook 'before-save-hook
+ #'editorconfig--delete-trailing-whitespace nil t)))
+ (when (equal trim-trailing-ws "false")
;; when false we remove every delete-trailing-whitespace
;; from write-file-functions
(when editorconfig-trim-whitespaces-mode
(funcall editorconfig-trim-whitespaces-mode 0))
- (setq write-file-functions
- (remove 'delete-trailing-whitespace write-file-functions))))
+ (remove-hook 'before-save-hook
+ #'editorconfig--delete-trailing-whitespace t)))
(defun editorconfig-set-line-length (length)
"Set the max line length (`fill-column') to LENGTH."
@@ -867,7 +869,6 @@ To disable EditorConfig in some buffers, modify
:lighter editorconfig-mode-lighter
(let ((modehooks '(prog-mode-hook
text-mode-hook
- read-only-mode-hook
;; Some modes call `kill-all-local-variables' in their
init
;; code, which clears some values set by editorconfig.
;; For those modes, editorconfig-apply need to be called
diff --git a/ert-tests/editorconfig.el b/ert-tests/editorconfig.el
index 4fcfe13f4e..92a2dbd4f5 100644
--- a/ert-tests/editorconfig.el
+++ b/ert-tests/editorconfig.el
@@ -102,12 +102,15 @@
(ert-deftest test-trim-trailing-ws nil
(editorconfig-mode 1)
(with-visit-file (concat editorconfig-ert-dir "trim.txt")
- (should (memq 'delete-trailing-whitespace
- write-file-functions)))
- (with-visit-file (concat editorconfig-ert-dir "trim.txt")
- (read-only-mode 1)
- (should (not (memq 'delete-trailing-whitespace
- write-file-functions))))
+ (should (memq #'editorconfig--delete-trailing-whitespace
+ before-save-hook)))
+ ;; We used to re-apply the vars when switching `read-only-mode',
+ ;; but now instead we use a hook function that checks `buffer-read-only'
+ ;; every time we save the file.
+ ;;(with-visit-file (concat editorconfig-ert-dir "trim.txt")
+ ;; (read-only-mode 1)
+ ;; (should (not (memq #'editorconfig--delete-trailing-whitespace
+ ;; before-save-hook))))
(editorconfig-mode -1))
(ert-deftest test-charset nil