From f59d012af7e607448fdb435fcb4becb6a6ebe665 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Sat, 10 Dec 2022 00:57:15 -0500 Subject: [PATCH] whitespace: Use `define-globalized-minor-mode' for global mode * lisp/whitespace.el (global-whitespace-mode): Fix interoperability between `whitespace-mode' and `global-whitespace-mode' by using `define-globalized-minor-mode'. (Bug#60334) * test/lisp/whitespace-tests.el (whitespace-tests--global): Add a regression test. --- lisp/whitespace.el | 35 ++++++----------------------------- test/lisp/whitespace-tests.el | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 29 deletions(-) diff --git a/lisp/whitespace.el b/lisp/whitespace.el index 558be1841ab..7a30274a330 100644 --- a/lisp/whitespace.el +++ b/lisp/whitespace.el @@ -1014,34 +1014,11 @@ See also `whitespace-newline' and `whitespace-display-mappings'." ;;;###autoload -(define-minor-mode global-whitespace-mode - "Toggle whitespace visualization globally (Global Whitespace mode). - -See also `whitespace-style', `whitespace-newline' and -`whitespace-display-mappings'." - :lighter " WS" +(define-globalized-minor-mode global-whitespace-mode + whitespace-mode + whitespace-turn-on-if-enabled :init-value nil - :global t - :group 'whitespace - (cond - (noninteractive ; running a batch job - (setq global-whitespace-mode nil)) - (global-whitespace-mode ; global-whitespace-mode on - (save-current-buffer - (add-hook 'find-file-hook 'whitespace-turn-on-if-enabled) - (add-hook 'after-change-major-mode-hook 'whitespace-turn-on-if-enabled) - (dolist (buffer (buffer-list)) ; adjust all local mode - (set-buffer buffer) - (unless whitespace-mode - (whitespace-turn-on-if-enabled))))) - (t ; global-whitespace-mode off - (save-current-buffer - (remove-hook 'find-file-hook 'whitespace-turn-on-if-enabled) - (remove-hook 'after-change-major-mode-hook 'whitespace-turn-on-if-enabled) - (dolist (buffer (buffer-list)) ; adjust all local mode - (set-buffer buffer) - (unless whitespace-mode - (whitespace-turn-off))))))) + :group 'whitespace) (defvar whitespace-enable-predicate (lambda () @@ -1067,7 +1044,7 @@ This variable is normally modified via `add-function'.") (defun whitespace-turn-on-if-enabled () (when (funcall whitespace-enable-predicate) - (whitespace-turn-on))) + (whitespace-mode))) ;;;###autoload (define-minor-mode global-whitespace-newline-mode @@ -2511,7 +2488,7 @@ purposes)." (setq whitespace-display-table-was-local t) ;; Save the old table so we can restore it when ;; `whitespace-mode' is switched off again. - (when (or whitespace-mode global-whitespace-mode) + (when whitespace-mode (setq whitespace-display-table (copy-sequence buffer-display-table))) ;; Assure `buffer-display-table' is unique diff --git a/test/lisp/whitespace-tests.el b/test/lisp/whitespace-tests.el index d72748cd0c9..3a53e025036 100644 --- a/test/lisp/whitespace-tests.el +++ b/test/lisp/whitespace-tests.el @@ -57,6 +57,24 @@ buffer's content." (whitespace-cleanup) (buffer-string))) +(ert-deftest whitespace-tests--global () + (let ((backup global-whitespace-mode) + (noninteractive nil) + (whitespace-enable-predicate (lambda () t))) + (unwind-protect + (progn + (global-whitespace-mode 1) + (ert-with-test-buffer-selected () + (normal-mode) + (should whitespace-mode) + (global-whitespace-mode -1) + (should (null whitespace-mode)) + (whitespace-mode 1) + (should whitespace-mode) + (global-whitespace-mode 1) + (should whitespace-mode))) + (global-whitespace-mode (if backup 1 -1))))) + (ert-deftest whitespace-cleanup-eob () (let ((whitespace-style '(empty))) (should (equal (whitespace-tests--cleanup-string "a\n") -- 2.39.2