8

I'm trying to get emacs whitespace-mode enabled automatically only in certain modes. According to the documentation, enabling global-whitespace-mode and setting the whitespace-global-modes variable should do exactly that. But I can't get it to work correctly.

In my .emacs.el I have:

(require 'whitespace)
(global-whitespace-mode t)
(setq whitespace-global-modes '(c-mode c++-mode))

but the definition of whitespace-global-modes seems to be ignored; global-whitespace-mode is enabled in every buffer. I know that I've got the variable name correctly, because C-h v whitespace-global-modes tells me:

whitespace-global-modes's value is (c-mode c++mode)

Documentation:
Modes for which global `whitespace-mode' is automagically turned on.
...

So what am I doing wrong? Have I misunderstood the purpose of whitespace-global-modes?

I'm running emacs 23.2.1.

jchl
  • 6,332
  • 4
  • 27
  • 51

2 Answers2

5

Apparently the meaning of whitespace-global-modes is very different from what you (and I) understand.

How about trying

(require 'whitespace)

(add-hook 'c-mode-hook 
  (function (lambda ()
              (whitespace-mode t))))

and repeating the same thing for c++-mode?

Emre Sevinç
  • 8,211
  • 14
  • 64
  • 105
  • 1
    I think that would have worked too, but I really wanted to get `whitespace-global-modes` to work. I guess one advantage of your solution is that "WS" wouldn't appear in the modeline for buffers where it wasn't doing anything. – jchl Nov 07 '11 at 14:22
  • Yes, WS versus ws was very confusing for me, too. – Emre Sevinç Nov 07 '11 at 14:32
  • Thank you very much! This solves the issue of newly created files and the scratch buffer not adhering to the global definition. Adding hooks for each mode (as described in Mr. Sevinç's answer) eliminates the need to manually turn on whitespacing for new files and/or the scratch buffer. The global behavior was confusing because the global definition worked for existing files that were opened with the application, but not new files or the scratch buffer. – lawlist Apr 12 '13 at 01:21
4

It turns out that the commands in my .emacs.el were (almost) working after all. What confused me was that "WS" appears in the modeline of all buffers, even though only C and C++ buffers were getting the effect of whitespace-mode, as desired.

The other problem was that I had a typo: c++mode rather than c++-mode.

jchl
  • 6,332
  • 4
  • 27
  • 51
  • Hmm, then what is the meaning of WS in the mode line? I'm confused. WS for global, ws for local... that kind of thing? – Emre Sevinç Nov 07 '11 at 14:18