The attached patch series should fix this bug. Patch 1: ert-x: New `ert-with-test-buffer-selected' convenience macro * lisp/emacs-lisp/ert-x.el (ert-with-test-buffer-selected): New convenience macro that extends `ert-with-test-buffer' by displaying the test buffer in a temporary selected window. This makes it easier to simulate user input in the body via `execute-kbd-macro'. * test/lisp/emacs-lisp/ert-x-tests.el (ert-test-test-buffer-selected/*): Add tests. Patch 2: ; whitespace: Delete unused `whitespace-font-lock-refontify' var Patch 3: ; whitespace: Use `defvar-local' for buffer-local vars Patch 4: ; whitespace: Fix indentation Patch 5: whitespace: Reset `whitespace-buffer-changed' when refontifying * lisp/whitespace.el (whitespace-post-command-hook): Add missing reset of `whitespace-buffer-changed' back to nil between commands. Patch 6: whitespace: Include empty final line in BoB empty match * lisp/whitespace.el (whitespace-empty-at-bob-regexp): Include any last line trailing whitespace in the BoB empty line match to ensure that those characters get highlighted. Patch 7: whitespace: Redo BoB/EoB empty line highlighting * lisp/whitespace.el (whitespace--empty-at-bob-matcher, whitespace--empty-at-eob-matcher, whitespace--update-bob-eob, whitespace-color-off, whitespace-color-on, whitespace-empty-at-bob-regexp, whitespace-empty-at-eob-regexp, whitespace-looking-back, whitespace-post-command-hook): Redo the `empty' line highlighting logic to ensure that a buffer change causes all affected `empty' lines to become (un)highlighted (bug#37467). Also, for improved UX, don't highlight BoB empty lines at or below point (not just when point is at 1), or EoB empty lines at or above point (not just when point is `eobp'). (whitespace-bob-marker, whitespace-eob-marker): Clarify documentation. * test/lisp/whitespace-tests.el (whitespace--with-test-buffer, whitespace--fu, whitespace-tests--empty-bob, whitespace-tests--empty-eob): Add tests.