Subject: 28.1; whitespace-style cannot be configured for diff-mode via hook

From: YE <[email-masked]>
Date: Thu, 21 Jul 2022 17:46:17 +0300

Steps To Reproduce:

emacs -Q

*diff-mode-hook*

1.
#+begin_src emacs-lisp
  (defun my-diff-mode ()
    (setq-local whitespace-style '(face trailing spaces))
    (message "diff-mode-hook: %S" whitespace-style))

  (add-hook 'diff-mode-hook 'my-diff-mode)
#+end_src

2. 'M-x vc-dir <any-repo>'
3. Activate diff view (f.i. 'M-x vc-diff' or 'M-x log-view-diff')
4. See that 'C-h v whitespace-style' outputs '(face trailing)' instead of the
expected value '(face trailing spaces)'.

Additional Information

A bit of investigation showed that 'diff-setup-whitespace' always sets
'whitespace-style' to '(face trailing)'.

The debugged order of setting 'whitespace-style' value:
- diff-setup-whitespace: (face trailing)
- diff-mode-hook: (face trailing spaces)
- diff-setup-whitespace: (face trailing)

So might be the order of the value setting can be fixed?

Or maybe adding a defcustom 'diff-whitespace-style' would be a proper solution
here? I started working on such a patch but stuck disliking the probable need in
the 'whitespace-style' large ':type' definition duplication.

*whitespace-mode-hook*

I also attempted another approach (though didn't investigate it,
so probably it's a separate issue). It's also not functional.

emacs -Q

1.
#+begin_src emacs-lisp
(defun my-whitespace-mode ()
  (when (eq major-mode 'diff-mode)
    (setq-local whitespace-style '(face trailing spaces))))

(add-hook 'whitespace-mode-hook 'my-whitespace-mode)
#+end_src

2. In diff-mode 'C-h v whitespace-style' outputs expected '(face trailing)'.
3. Enable whitespace-mode 'M-x whitespace-mode'.
4. See that 'C-h v whitespace-style' outputs expected '(face trailing spaces)'.
   But this doesn't change the buffer's output.
5. Eval '(whitespace-turn-on)' to see the expected buffer output (or toggle the
   mode twice with 'M-x whitespace-mode').

*Current Workaround*

The workaround I currently use adds a keybinding to set 'whitespace-style'
and toggle 'whitespace-mode'.

#+begin_src emacs-lisp
(defun my-diff-whitespace ()
  "Toggle whitespace visualization with local `whitespace-style'."
  (interactive)
  (setq-local whitespace-style '(face trailing spaces))
  (whitespace-mode 'toggle))

(define-key diff-mode-map (kbd "C-c b w") 'my-diff-whitespace)
#+end_src