]> git.eshelyaron.com Git - emacs.git/commitdiff
simple.el (delete-trailing-whitespace-mode): New minor mode (bug#78264)
authorStefan Monnier <monnier@iro.umontreal.ca>
Mon, 5 May 2025 15:55:29 +0000 (11:55 -0400)
committerEshel Yaron <me@eshelyaron.com>
Mon, 12 May 2025 19:56:20 +0000 (21:56 +0200)
Partly motivated by bug#78097.

* lisp/simple.el (delete-trailing-whitespace-if-possible): New function.
(delete-trailing-whitespace-mode): New minor mode.

* lisp/editorconfig.el (editorconfig-trim-whitespaces-mode):
Change default to `delete-trailing-whitespace-mode`.
(editorconfig--get-trailing-ws): Simplify accordingly.
(editorconfig--add-hook-safe-p): Delete function.  Don't touch
`safe-local-eval-function` any more.

(cherry picked from commit 8a19c249f813e9f3830308e40f0205d7665f78a3)

lisp/editorconfig.el
lisp/simple.el

index 28b1cc9a3d18c9eff0e939cd448d1967d7c1389b..0c219e13e4e0a8497b0b136bae886c858bb53ca0 100644 (file)
@@ -271,13 +271,13 @@ a list of settings in the form (VARIABLE . VALUE)."
   :version "30.1"
   :risky t)
 
-(defcustom editorconfig-trim-whitespaces-mode nil
+(defcustom editorconfig-trim-whitespaces-mode #'delete-trailing-whitespace-mode
   "Buffer local minor-mode to use to trim trailing whitespaces.
 
 If set, enable that mode when `trim_trailing_whitespace` is set to true.
 Otherwise, use `delete-trailing-whitespace'."
   :version "30.1"
-  :type 'symbol)
+  :type 'function)
 
 (defvar-local editorconfig-properties-hash nil
   "Hash object of EditorConfig properties that was enabled for current buffer.
@@ -536,33 +536,17 @@ This function will revert buffer when the coding-system has been changed."
   "Call `delete-trailing-whitespace' unless the buffer is read-only."
   (unless buffer-read-only (delete-trailing-whitespace)))
 
-;; Arrange for our (eval . (add-hook ...)) "local var" to be considered safe.
-(defun editorconfig--add-hook-safe-p (exp)
-  (equal exp '(add-hook 'before-save-hook
-                        #'editorconfig--delete-trailing-whitespace nil t)))
-(let ((predicates (get 'add-hook 'safe-local-eval-function)))
-  (when (functionp predicates)
-    (setq predicates (list predicates)))
-  (unless (memq #'editorconfig--add-hook-safe-p predicates)
-    (put 'add-hook 'safe-local-eval-function #'editorconfig--add-hook-safe-p)))
-
 (defun editorconfig--get-trailing-ws (props)
   "Get vars to trim of trailing whitespace according to PROPS."
-  (pcase (gethash 'trim_trailing_whitespace props)
-    ("true"
-     `((eval
-        . ,(if editorconfig-trim-whitespaces-mode
-               `(,editorconfig-trim-whitespaces-mode 1)
-             '(add-hook 'before-save-hook
-                        #'editorconfig--delete-trailing-whitespace nil t)))))
-    ("false"
-     ;; Just do it right away rather than return a (VAR . VAL), which
-     ;; would be probably more trouble than it's worth.
-     (when editorconfig-trim-whitespaces-mode
-       (funcall editorconfig-trim-whitespaces-mode 0))
-     (remove-hook 'before-save-hook
-                  #'editorconfig--delete-trailing-whitespace t)
-     nil)))
+  (let ((fun (or editorconfig-trim-whitespaces-mode
+                 #'delete-trailing-whitespace-mode)))
+    (pcase (gethash 'trim_trailing_whitespace props)
+      ("true" `((eval . (,fun 1))))
+      ("false"
+       ;; Just do it right away rather than return a (VAR . VAL), which
+       ;; would be probably more trouble than it's worth.
+       (funcall fun 0)
+       nil))))
 
 (defun editorconfig--get-line-length (props)
   "Get the max line length (`fill-column') to PROPS."
index fcc91910a6eb256058dd9666e1adb97ca8d8accb..aaf64fb6adc42a1daed9ebe951320162a0f5df74 100644 (file)
@@ -900,6 +900,21 @@ buffer if the variable `delete-trailing-lines' is non-nil."
   ;; Return nil for the benefit of `write-file-functions'.
   nil)
 
+(defun delete-trailing-whitespace-if-possible ()
+  "Call `delete-trailing-whitespace' unless the buffer is read-only."
+  (unless buffer-read-only (delete-trailing-whitespace)))
+
+(define-minor-mode delete-trailing-whitespace-mode
+  "Delete trailing whitespace before saving the current buffer."
+  :global nil
+  (cond
+   (delete-trailing-whitespace-mode
+    (add-hook 'before-save-hook
+              #'delete-trailing-whitespace-if-possible nil t))
+   (t
+    (remove-hook 'before-save-hook
+                 #'delete-trailing-whitespace-if-possible t))))
+
 (defun newline-and-indent (&optional arg)
   "Insert a newline, then indent according to major mode.
 Indentation is done using the value of `indent-line-function'.