I've tried to configure whitespace-mode to do what I want but with no luck. I'm trying to achieve the following (see image).
whitespace-mode, there's always:
(setq-default show-trailing-whitespace t)
which might not give you that exact visualisation, but will let you visualise trailing whitespace nevertheless.
I also have a keybinding for this:
(defun toggle-show-trailing-whitespace () "Toggle `show-trailing-whitespace'." (interactive) (setq show-trailing-whitespace (not show-trailing-whitespace)))
whitespace-mode ensure you have
trailing included in
whitespace-style, and once again there's a specific face:
In either case, I don't know how you'll get spaces to appear as dots for that face only, unless you can specify a font which has dots for spaces? Someone else might know otherwise.
The documentation in the emacs manual says:
Whitespace mode is a buffer-local minor mode that lets you “visualize” many kinds of whitespace in the buffer, by either drawing the whitespace characters with a special face or displaying them as special glyphs.
The relevant docstrings contain more information. So let's say we want do display spaces as ·:
(setq whitespace-display-mappings '((space-mark 32 [?·])))
Note we could add an entry for
tab-mark for this to apply to tabs
Now this is only applied if
(setq whitespace-style '(space-mark))
The problem is we can't specify different glyphs for different kinds of spaces (in your case, trailing versus other spaces), but what we can specify for each kind is a face to be used, so we instead do:
(setq whitespace-style '(face trailing spaces space-mark))
that is in addition of using the · glyph, we want both trailing spaces and (non trailing, I guess) spaces to be displayed by faces.
Now we have to M-x
whitespace-space to a "non visible" face by setting both its
Background attributes to
our background color ; and
whitespace-trailing to whatever we want.
whitespace-mode has to be restarted for changes to these
variables to take effect.
Also, non trailing whitespaces will be visible as a · when point is
there if the
cursor-type is a
box, or when in highlighted region, that can be annoying.
After all, this is a bit convoluted:
there's a space in the buffer there,
but it is displayed as a · glyph,
but its face "hides" it, making it look like a space.
Additionnaly, trailing white spaces are not shown when point is at line end, after these spaces; this is by design:
This feature does not apply when point is at the end of the line containing the whitespace. Strictly speaking, that is “trailing whitespace” nonetheless, but displaying it specially in that case looks ugly while you are typing in new text. In this special case, the location of point is enough to show you that the spaces are present.
If you really want to change this, here is how it's implemented in
(defun whitespace-trailing-regexp (limit) "Match trailing spaces which do not contain the point at end of line." (let ((status t)) (while (if (re-search-forward whitespace-trailing-regexp limit t) (= whitespace-point (match-end 1)) ;; loop if point at eol (setq status nil))) ;; end of buffer status))
You can redefine this function in your init file, removing the end of line test:
(defun whitespace-trailing-regexp (limit) "Match all trailing spaces. This overloads the definition in whitespace.el." (let ((status t)) (while (unless (re-search-forward whitespace-trailing-regexp limit t) (setq status nil))) ;; end of buffer status))
Of course, all of this must be done after
whitespace has been loaded, which can be ensured by including
(require 'whitespace) first. Another possibility would be to advise the function, I'm not sure what's the prefered way here.