6

When using Whitespace Mode with the empty style enabled, the blank lines at the end of a file aren't highlighted if I'm on the very last line. However, I also don't want them to be highlighted if I'm on the line before last, since it visually interferes with appending text to the end of a file. For instance, say I have an unsaved buffer which looks like this, where $ denotes a newline character and | denotes point:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.|$

Now if I decide to add a new paragraph:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.$
$
|$

Whitespace Mode will now highlight the two blank lines at the end. However, as soon as I start writing text, it becomes obvious that there isn't, in fact, any issue with those blank lines since my point is positioned to rectify them:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.$
$
Pellentesque quis metus eget urna dictum pulvinar.|$

Therefore, how can I prevent Whitespace Mode from highlighting the blank lines in the situation just before I start typing Pellentesque, when my point is on the second-to-last blank line?

Lily Chung
  • 375
  • 1
  • 8
  • What's yours `whitespace-style` set to? – Maciej Goszczycki Sep 24 '14 at 19:54
  • @mgoszcz2 `(face trailing empty space-before-tab::tab lines-tail)` – Lily Chung Sep 24 '14 at 19:56
  • I can't seem to be able to replicate this. For me empty lines don't show until I move the cursor away. – Maciej Goszczycki Sep 24 '14 at 20:09
  • @mgoszcz2 Are you certain that the situation with the newline characters and point is exactly as described in the question? If point is *after* the very last newline character then the empty lines don't show for me. But I want them not to show even if point is just before the very last newline character. – Lily Chung Sep 24 '14 at 20:12
  • I'm not sure by now. Here's a video of how it works for me, https://www.youtube.com/watch?v=umEvhK3wVhs – Maciej Goszczycki Sep 24 '14 at 20:37
  • @mgoszcz2 Right, the highlighting goes away if your point is after the last newline character, but not when it is just before it. At 0:19, when point is two characters below the `H`, there ought not to be highlighting, and there isn't (good). At 0:20, when point is just below the `H`, there ought not to be any highlighting, but there is (bad). At 0:21, when point is on the `H`, there ought to be highlighting, and there is (good). – Lily Chung Sep 24 '14 at 20:43
  • Sorry. I'm sure it's fixable, but I don't know how to do it. – Maciej Goszczycki Sep 24 '14 at 20:59

2 Answers2

3

I don't think whitespace-mode can do what you're asking out-of-the-box.

But this is Emacs! You can get (maybe?) what you want by redefining the functions whitespace-empty-at-eob-regexp and whitespace-post-command-hook.

In whitespace-empty-at-eob-regexp (the function, not the actual regexp) you should find the following branch:

(cond
 ;; at eob
 ((= limit e)
  (when (/= whitespace-point e)
    (goto-char limit)
    (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp b)))
...

If you change the last line I quoted to look like this

    (setq r (whitespace-looking-back whitespace-empty-at-eob-regexp whitespace-point)))

Then whitespace-mode will only highlight lines from point to the end. This probably won't do what you expect, though, because whitespace-mode is clever about when it refontifies. So!

In whitespace-post-command-hook you should find the following logic:

(let ((refontify
       (or
        ...
        ;; it is at end of buffer (eob)
        (= whitespace-point (1+ (buffer-size)))
        ;; the buffer was modified and ...
        ...

If you insert at the same depth a clause like this

        ;; it is inside eob whitespace region
        (>= whitespace-point whitespace-eob-marker)

Then you'll get refontification more often. I wish I could say "whenever point is in the empty space at the end of the buffer" but I'm pretty confident there are situations where you can trick Emacs into getting out of sync.

You could also do what I do, and just type really fast in the blank lines.

purple_arrows
  • 2,373
  • 10
  • 19
  • Thanks for the answer! I'll test this out when I have some time. – Lily Chung Oct 08 '14 at 23:14
  • I got this working by using `(setq r (whitespace-looking-back whitespace-empty-at-eob-regexp (+ 1 whitespace-point))))`; I also couldn't get the refontification working correctly so I just set it to occur after every command. Thank you very much! – Lily Chung Jan 29 '15 at 01:54
0

I don't have an answer for doing what you want with whitespace-mode. Perhaps someone else will.

An alternative is to use library Highlight Chars. It doesn't have this problem when highlighting trailing whitespace. It doesn't treat newline chars as whitespace.

highlight-chars.el

Drew
  • 75,699
  • 9
  • 109
  • 225
  • Oops! I accidentally put the wrong style in the question- I meant `empty`, not `lines-tail`. The issue is that I do want empty lines at the beginning or end to be highlighted, just not when my point is in position to fix it. – Lily Chung Sep 24 '14 at 16:28