6

Goal: I want to have show-trailing-whitespace enabled for all buffers save a few. Exceptions posing a problem are *Shell Command Output* and its cousin *Async Shell Command*.

I usually have show-trailing-whitespace customized to t. Therefore it is active in all new buffers.

I would also like to have it turned off for certain buffers, foremost amongst them *Shell Command Output*. This poses a problem for me:

  • The output buffer doesn't use a special mode; it is still in fundamental-mode. There is no fundamental-mode-hook that I could hook this setting into.
  • There is the after-major-mode-change-hook which is run when the major mode is changed to fundamental-mode, but the buffer starts out in that mode and therefore this hook is not run.
  • There doesn't seem to be a way to hook into get-buffer-create.

I know I can always advise the function get-buffer-create for this particular example, but I try to avoid that as much as possible.

Any hints?

Drew
  • 29,895
  • 7
  • 74
  • 104
Moritz Bunkus
  • 11,592
  • 3
  • 37
  • 49

2 Answers2

2

You might be better off looking at the problem from the other side, and only set the var in those modes where you want to see trailing whitespace.

But I think you have a good point: these shell output buffers should not use fundamental-mode. It's probably time for M-x report-emacs-bug

Stefan
  • 27,908
  • 4
  • 53
  • 82
  • I know, but until today those "other modes" were pretty much "all modes". Hence my preference not having to add a gazillion of hooks. – Moritz Bunkus Oct 16 '12 at 15:01
  • 1
    `prog-mode-hook` and `text-mode-hook` should cover a good proportion of those modes. – Stefan Oct 16 '12 at 15:05
  • Good point, especially as I didn't know about that generic `prog-mode-hook`. I think I'll go that route; definitely preferable to using `advise`. Thanks. – Moritz Bunkus Oct 16 '12 at 15:33
0

In accordance with the accepted answer, here's a code snippet that enables trailing whitespaces highlighting for specific modes only:

(setq-default show-trailing-whitespace nil)

(defun namespace/show-trailing-whitespace ()
  "Highlight trailing whitespaces in this buffer."
  (setq-local show-trailing-whitespace t))

(dolist (hook '(prog-mode-hook text-mode-hook))
  (add-hook hook 'namespace/show-trailing-whitespace))

This snippet is essentially taken from Steve Purcell's configuration.

Y. E.
  • 687
  • 1
  • 10
  • 29