9

I have show-trailing-whitespace set to t globally in my .emacs file, and this is generally not a problem except for when I'm in diff-mode looking at a patch which has mandatory trailing white space for blank context lines.

One solution would be to simply turn off show-trailing-whitespace in diff-mode in some relevant hook, but actually I'd like for trailing white space in change lines to still show up. For example, if I've accidentally introduced trailing white space or if I'm removing it, that's ok, and I still want that to show up in the trailing-whitespace face. I just don't want the blank context lines to be treated as trailing white space since they are necessary for the diff format itself.

Here's an example of the current behavior that I get:

enter image description here

And this is what I would like:

enter image description here

Here's another example, but this time, with actual whitespace addition:

enter image description here

However if I disable show-trailing-whitespace entirely in diff-mode, then I wouldn't see any dark red at all. Instead, this is what I'm wanting:

enter image description here

Basically, I think this involves making show-trailing-whitespace aware that diff lines with just a single space are special.

b4hand
  • 1,995
  • 1
  • 19
  • 31
  • 3
    Are you sure just disabling this entirely in diff-mode is not what you want? If you've added or removed trailing whitespace accidentally, won't diff-mode highlight that regardless of your emacs configuration (by virtue of the fact that it's a change in the file)? – Malabarba Sep 25 '14 at 19:28
  • I want to see the trailing whitespace in the white space font of red. Feel free to include an answer for disabling just in diff-mode as that might be helpful for other people, but that's not exactly what I was looking for. – b4hand Sep 25 '14 at 20:26
  • 1
    Would it be satisfactory if diff-mode's own highlighting face was set to something similar to white-space-mode's red? – Malabarba Sep 25 '14 at 22:22

2 Answers2

2

The value of show-trailing-whitespace becomes buffer local when set so you can just set it in the relevant hook:

(add-hook 'diff-mode-hook (lambda () (setq show-trailing-whitespace nil)))

To check the documentation for show-trailing-whitespace: C-h v show-trailing-whitespace.

aerique
  • 603
  • 6
  • 5
1

One solution is to turn off Trailing whitespace mode in Diff mode, and instead define your own custom font lock rules for the trailing whitespace that you don't want to see.

(defvar diff-trailing-whitespace-keywords
  '(("^[+-<>]\\(.*\\S \\)?\\(\\s +\\)$" (2 'trailing-whitespace t))))
(defun diff-mode-font-lock-add-trailing-whitespace ()
  (setq diff-font-lock-keywords-and-whitespace
    (append diff-font-lock-keywords
        diff-trailing-whitespace-keywords))
  (setcar diff-font-lock-defaults 'diff-font-lock-keywords-and-whitespace))
(defun turn-off-trailing-whitespace ()
  (setq show-trailing-whitespace nil))
(add-hook 'diff-mode-hook 'turn-off-trailing-whitespace)
(eval-after-load "diff-mode" '(diff-mode-font-lock-add-trailing-whitespace))