3

In ~/.emacs I've globally enabled whitespace-mode:

(require 'whitespace)
(setq whitespace-style '(face empty tabs lines-tail trailing))
(global-whitespace-mode t)

Example buffer content:

-*- fill-column: 100; whitespace-line-column: nil -*-
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut
labore et dolore magna aliquyam erat, sed diam voluptua.

According to documentation for whitespace-line-column: If nil, the `fill-column' variable value is used. However, that doesn't happen. In the buffer with the above sample content, highlight starts at column 80 (default value):

Screenshot with highlight starting at column 80

What I already tried, to no avail:

  • Enabled whitespace-mode for that buffer explicitly.

  • Buffer local: whitespace-line-column: 100

  • In ~/.emacs: whitespace-line-column: nil, whitespace-line-column: 100

Version: GNU Emacs 24.4.1 (i686-pc-mingw32) of 2014-10-24 on LEG570

How can I specify line length for whitespace-mode for an individual buffer?

feklee
  • 1,029
  • 5
  • 17

1 Answers1

7

First off, since you didn't mention it: Using the same version of Emacs (24.4.1), the behavior you describe can be reproduced in emacs -Q.

Secondly, what you are doing should work (AFAICT). It seems that turning on whitespace-mode globally (or even in a mode hook) before visiting a file with a custom whitespace-line-column is what's causing the problem. You might want to consider reporting the issue to the Emacs devs via M-x report-emacs-bug.

Finally, you can get the behavior you want by doing the following:

  1. Change your config to

    (require 'whitespace)
    (setq whitespace-style '(face empty tabs lines-tail trailing))
    (add-hook 'hack-local-variables-hook 'whitespace-mode)
    
  2. Change headers of any files that should use a custom whitespace-line-column to

    -*- whitespace-line-column: 100; -*-
    
itsjeyd
  • 14,586
  • 3
  • 58
  • 87
  • Thanks for the suggestions! However, manually enabling `whitespace-mode` every time I open a new file is really not an option for me. – feklee Jan 28 '15 at 09:26
  • @feklee OK, it turns out there is a hook called `hack-local-variables-hook` that gets run after applying [file-local variables](https://www.gnu.org/software/emacs/manual/html_node/elisp/File-Local-Variables.html). If you add `whitespace-mode` to this hook, it will behave correctly for files that use a custom `whitespace-line-column` (and you won't have to turn it on manually). I updated my answer accordingly. – itsjeyd Jan 28 '15 at 09:42
  • 1
    Works great so far! Starting `whitespace-mode` from `hack-local-variables-hook` makes it also respect a global setting of `whitespace-line-column: nil`. So I don't need to specify an explicit value in every file. – feklee Jan 28 '15 at 11:39
  • this isn't working for me, the whitespace-line-column keeps reverting to the default. – fommil Jun 17 '15 at 16:54
  • 1
    aha! I had `:safe` local variables set, and this needs to be added to the safe list. – fommil Jun 17 '15 at 16:55
  • 1
    FYI `(put 'whitespace-line-column 'safe-local-variable #'integerp)` lets me use these settings as safe variables. – fommil Jun 17 '15 at 17:08
  • was this ever reported as a bug upstream? `auto-insert-alist` templates are able to see local variables, so I presume `auto-insert-mode` is doing The Right Thing. – fommil Nov 22 '15 at 11:06
  • 1
    I have also run into the same problem and have reported the bug here: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=24904 – DeX3 Nov 09 '16 at 09:36