aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Monnier2024-06-12 18:27:15 -0400
committerStefan Monnier2024-06-19 14:39:17 -0400
commit527a45abc37f1885652b0f29ed5eeabff6db6cda (patch)
treec6160f9a8f3855dc2a1867ddb4de85495439caf0
parent25523847e7931de1f0ee638d2a8847d979fdb496 (diff)
downloademacs-527a45abc37f1885652b0f29ed5eeabff6db6cda.tar.gz
emacs-527a45abc37f1885652b0f29ed5eeabff6db6cda.zip
lisp/editorconfig: Don't hook into `read-only-mode-hook`
Don'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. * lisp/editorconfig.el (editorconfig--delete-trailing-whitespace): New function. (editorconfig-set-trailing-ws): Use it. Use `pcase`. 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.
-rw-r--r--lisp/editorconfig.el22
1 files changed, 15 insertions, 7 deletions
diff --git a/lisp/editorconfig.el b/lisp/editorconfig.el
index b8eced27807..a0bd0ae3e74 100644
--- a/lisp/editorconfig.el
+++ b/lisp/editorconfig.el
@@ -349,13 +349,22 @@ to non-nil when FINAL-NEWLINE is true."
349 ("false" 349 ("false"
350 ))) 350 )))
351 351
352(defun editorconfig--delete-trailing-whitespace ()
353 "Call `delete-trailing-whitespace' unless the buffer is read-only."
354 (unless buffer-read-only (delete-trailing-whitespace)))
355
352(defun editorconfig-set-trailing-ws (trim-trailing-ws) 356(defun editorconfig-set-trailing-ws (trim-trailing-ws)
353 (if editorconfig-trim-whitespaces-mode 357 (pcase trim-trailing-ws
354 (funcall editorconfig-trim-whitespaces-mode 1) 358 ("true"
355 ...) 359 (if editorconfig-trim-whitespaces-mode
356 (when editorconfig-trim-whitespaces-mode 360 (funcall editorconfig-trim-whitespaces-mode 1)
357 (funcall editorconfig-trim-whitespaces-mode 0)) 361 (add-hook 'before-save-hook
358 ...) 362 #'editorconfig--delete-trailing-whitespace nil t)))
363 ("false"
364 (when editorconfig-trim-whitespaces-mode
365 (funcall editorconfig-trim-whitespaces-mode 0))
366 (remove-hook 'before-save-hook
367 #'editorconfig--delete-trailing-whitespace t))))
359 368
360(defun editorconfig-set-line-length (length) 369(defun editorconfig-set-line-length (length)
361 "Set the max line length (`fill-column') to LENGTH." 370 "Set the max line length (`fill-column') to LENGTH."
@@ -521,7 +530,6 @@ F is that function, and FILENAME and ARGS are arguments passed to F."
521 :global t 530 :global t
522 (let ((modehooks '(prog-mode-hook 531 (let ((modehooks '(prog-mode-hook
523 text-mode-hook 532 text-mode-hook
524 read-only-mode-hook
525 ;; Some modes call `kill-all-local-variables' in their init 533 ;; Some modes call `kill-all-local-variables' in their init
526 ;; code, which clears some values set by editorconfig. 534 ;; code, which clears some values set by editorconfig.
527 ;; For those modes, editorconfig-apply need to be called 535 ;; For those modes, editorconfig-apply need to be called