Subject: bug#76355: [PATCH] Add new minor mode for clear whitespaces on save.

From: Elijah Gabe Pérez <[email-masked]>
Date: Sun, 16 Feb 2025 18:00:56 -0600

Tags: patch

This feature adds a minor mode (and global) for clean whitespaces in
current file/buffer on save.

I know that already exists the option 'auto-cleanup' in
whitespace-action that does the same, however, it just depends on the
whitespace-mode, so if i turn-off whitespace-mode auto-cleanup won't
work.

Also I know there's already a package on MELPA that does something like
this, however i think this kind of functionality must be included in
Emacs so I did my own implementation.

This patch is just a working prototype, and I want to know if there is
interest in this so I can polish it.

In GNU Emacs 31.0.50 (build 1, x86_64-redhat-linux-gnu, GTK+ Version
 3.24.43, cairo version 1.18.2) of 2025-02-03 built on
 49a55a317a644839ad7d857f65aa5be0
System Description: Fedora Linux 41 (Workstation Edition)

Configured using:

>From 894a27e270373db41674ba6bc460fdee29f15585 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?El=C3=ADas=20Gabriel=20P=C3=A9rez?= <[email-masked]>
Date: Sun, 16 Feb 2025 16:33:46 -0600
Subject: [PATCH] Add new minor mode for clear whitespaces on save.

Add minor mode and global minor mode for clean whitespaces in current
file/buffer on save.

* lisp/whitespace.el (whitespace-autoclean-on-save-mode):
New minor mode.
(turn-on-whitespace-cleanup-on-save): New function.
(global-whitespace-cleanup-on-save): New global minor mode.
---
 lisp/whitespace.el | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/lisp/whitespace.el b/lisp/whitespace.el
index efee11faf98..98210febf2d 100644
--- a/lisp/whitespace.el
+++ b/lisp/whitespace.el
@@ -1008,11 +1008,21 @@ whitespace-newline-mode
   ;; sync states (running a batch job)
   (setq whitespace-newline-mode whitespace-mode))
 
+(define-minor-mode whitespace-autoclean-on-save-mode
+  "Toggle cleanup of whitespace before saving the buffer.
+Cleans up any whitespace in the buffer before saving."
+  :lighter    " wscln"
+  :init-value nil
+  :global     nil
+  :group      'whitespace
+  (if whitespace-autoclean-on-save-mode
+      (add-hook 'write-file-functions 'whitespace-cleanup nil t)
+    (remove-hook 'write-file-functions 'whitespace-cleanup t)))
+
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;;;; User commands - Global mode
 
-
 ;;;###autoload
 (define-globalized-minor-mode global-whitespace-mode
   whitespace-mode
@@ -1069,6 +1079,20 @@ global-whitespace-newline-mode
                "use `global-whitespace-mode' with `whitespace-style' set to `(newline-mark newline)' instead."
                "28.1")
 
+(defun turn-on-whitespace-autoclean-on-save ()
+  "Turn on whitespace visualization."
+  ;; prepare local hooks
+  (if (not whitespace-autoclean-on-save-mode)
+      (whitespace-autoclean-on-save-mode)
+    (whitespace-autoclean-on-save-mode)))
+
+;;;###autoload
+(define-globalized-minor-mode global-whitespace-cleanup-on-save
+  whitespace-autoclean-on-save
+  turn-on-whitespace-autoclean-on-save
+  :init-value nil
+  :group 'whitespace)
+
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;;;; User commands - Toggle
-- 
2.48.1

-- 
                                                   - E.G via GNU Emacs.