Currently, whitespace.el shows me newline characters by changing the display table to show a $ at the end of lines.

I don't actually find this useful, so I disable it while I enjoy the many other benefits of whitespace-mode.

When I'm composing emails in message-mode I want to show the newline characters that have the 'hard text property, because it affects how the flowed text shows up.

Can I hack this distinction into the display table logic of whitespace-mode? My backup plan relies on longlines-show-hard-newlines but I'd prefer to extend whitespace.el if possible.

  • I would have thought just adding newline and newline-mark to whitespace-style when entering message-mode would do it, but upon checking it seems whitespace-style is not buffer local.
    – aerique
    Oct 7 '14 at 7:17
  • I am able to toggle the whitespace-style in message-mode. I personally use whitespace-newline-mode for this, but of course there are other ways to do it. I find this toggle uses one face for every newline - it hacks the display table to show a fontified "$\n" whenever the text has a "\n". The question is whether I can modify this display table logic to distinguish on other criteria, like text properties. Does that makes sense? Oct 7 '14 at 12:59

Here is a very ugly solution. First, define a function to find hard newlines such that it can be used by font-lock-mode:

(defun whitespace-find-hard-newlines (end)
  (search-forward "\n" end t)
  (while (and (not (memq 'hard (text-properties-at (match-beginning 0))))
       (search-forward "\n" end t)))
  (memq 'hard (text-properties-at (match-beginning 0))))

Here is what I used in my *scratch* buffer to test it. The switch to fundamental-mode made sure everything got initialized properly.

  (font-lock-add-keywords nil
    '((whitespace-find-hard-newlines 0 '(background-color . "#eee") t))))

Why is it ugly? Because adding a background color to the "newline" will create a light grey line to the right edge of the window. It looks ugly. You can't use the foreground color because there is nothing there to highlight. The $ sign that you're seeing comes from a display table and that only works on characters. On the character level, however, the hard and soft newlines don't differ.

  • Thanks! I'm still learning the ins-and-outs of font lock keywords. I'll play with this later and probably accept it - I'm fine with ugly solutions. Oct 9 '14 at 16:12
  • This makes sense and works as advertised! I'll keep playing with it, but in the meantime, this is perfect for me. Oct 14 '14 at 0:10
  • I tried this, but it makes my Emacs 25 get stuck in an infinite loop in font-lock-fontify-keywords-region. (whitespace-find-hard-newlines itself terminates as it's supposed to.) I'll try to debug this and figure out why it happens, but any insights are welcome.
    – legoscia
    Mar 3 '15 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.