Subject: 24.3.91; highlight-regexp in find-file-hook doesn't work as expected

From: [email-masked] (Jan Synáček)
Date: Wed, 21 May 2014 09:20:55 +0200

I use the following snippet to highlight trailing whitespace:

(defun jsynacek-highlight-trailing-whitespace ()
  (highlight-regexp "\\s-+$" 'hi-pink))
(add-hook 'find-file-hook 'jsynacek-highlight-trailing-whitespace)

After opening any org-mode file or a plain text file (and possibly more
formats), whenever a paragraph ends, the whole rest of its last line is
highlighted.

The following piece of text can be used (without the pseudo quotes):

<start file>
stuff

stuff2

test1
test2
test3  
test4

end
<end file>

To reproduce the problem:
0) Save the above piece of text to a file.
1) Start Emacs.
2) Eval the elisp snippet I've provided.
3) Open the text file.
4) Observe incorrect highlighting.

Note that only the line beginning with "test3" has whitespaces at the
end, and I expect only those whitespaces to be highlighted.

The problem doesn't appear in .el files.

In GNU Emacs 24.3.91.1 (x86_64-unknown-linux-gnu, GTK+ Version 3.10.8)
 of 2014-05-12 on jsynacek-ntb.brq.redhat.com
Windowing system distributor `Fedora Project', version 11.0.11404000
Configured using:
 `configure --prefix=/home/jsynacek/.local/emacs-24.3.91'

Important settings:
  value of $LANG: en_US.UTF-8
  locale-coding-system: utf-8-unix

Minor modes in effect:
  helm-match-plugin-mode: t
  helm-occur-match-plugin-mode: t
  erc-list-mode: t
  erc-menu-mode: t
  erc-autojoin-mode: t
  erc-ring-mode: t
  erc-networks-mode: t
  erc-pcomplete-mode: t
  erc-track-mode: t
  erc-match-mode: t
  erc-button-mode: t
  erc-fill-mode: t
  erc-stamp-mode: t
  erc-netsplit-mode: t
  erc-irccontrols-mode: t
  erc-noncommands-mode: t
  erc-move-to-prompt-mode: t
  erc-readonly-mode: t
  ido-everywhere: t
  recentf-mode: t
  projectile-global-mode: t
  projectile-mode: t
  magit-auto-revert-mode: t
  shell-dirtrack-mode: t
  diff-auto-refine-mode: t
  override-global-mode: t
  winner-mode: t
  delete-selection-mode: t
  show-paren-mode: t
  global-auto-revert-mode: t
  tooltip-mode: t
  mouse-wheel-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  buffer-read-only: t
  column-number-mode: t
  line-number-mode: t
  transient-mark-mode: t

Features:
(shadow sort mail-extr dabbrev emacsbug sendmail visual-regexp
helm-command helm-elisp helm-eval eldoc helm-mode eieio-opt speedbar
sb-image ezimage dframe vc-git misearch multi-isearch hi-lock
org-element org-rmail org-mhe org-irc org-info org-gnus org-docview
doc-view jka-compr image-mode org-bibtex bibtex org-bbdb org-w3m
epa-file helm-files image-dired dired-aux ffap helm-buffers
helm-elscreen helm-tags helm-bookmark helm-adaptative helm-info helm-net
browse-url xml url url-proxy url-privacy url-expand url-methods
url-history url-cookie url-domsuf url-util url-parse url-vars mailcap
helm-plugin bookmark helm-locate helm-help helm-match-plugin helm-grep
helm-regexp helm-external helm-utils setup-erc erc-autoaway erc-list
erc-menu erc-join erc-ring erc-networks erc-pcomplete erc-track
erc-match erc-button erc-fill erc-stamp erc-netsplit erc-goodies erc
erc-backend erc-compat pp private undo-tree diff smex ido recentf
tree-widget wid-edit projectile pkg-info lisp-mnt epl s ob-python
ob-plantuml htmlize org org-macro org-footnote org-pcomplete org-list
org-faces org-entities noutline outline org-version ob-emacs-lisp ob
ob-tangle org-src ob-ref ob-lob ob-table ob-keys ob-exp ob-comint
ob-core ob-eval org-compat org-macs org-loaddefs find-func
magit-key-mode magit view tramp tramp-compat auth-source eieio
eieio-core gnus-util password-cache tramp-loaddefs trampver shell
pcomplete help-mode grep compile comint epa derived epg epg-config
diff-mode ansi-color git-rebase-mode thingatpt git-commit-mode log-edit
message byte-opt format-spec rfc822 mml mml-sec mm-decode mm-bodies
mm-encode mail-parse rfc2231 rfc2047 rfc2045 ietf-drums mm-util
mail-prsvr mailabbrev mail-utils gmm-utils mailheader pcvs-util add-log
edmacro kmacro ibuffer helm dired-x dired cal-menu calendar cal-loaddefs
server use-package diminish bytecomp byte-compile cconv bind-key
easy-mmode winner ring delsel solarized-dark-theme solarized dash paren
autorevert filenotify cus-start cus-load cl-macs ace-window
ace-jump-mode cl gv cl-loaddefs cl-lib browse-kill-ring-autoloads
buffer-move-autoloads etags-select-autoloads highlight-symbol-autoloads
pydoc-info advice help-fns info-look info easymenu package time-date
tooltip electric uniquify ediff-hook vc-hooks lisp-float-type mwheel
x-win x-dnd tool-bar dnd fontset image regexp-opt fringe tabulated-list
newcomment lisp-mode prog-mode register page menu-bar rfn-eshadow timer
select scroll-bar mouse jit-lock font-lock syntax facemenu font-core
frame cham georgian utf-8-lang misc-lang vietnamese tibetan thai
tai-viet lao korean japanese hebrew greek romanian slovak czech european
ethiopic indian cyrillic chinese case-table epa-hook jka-cmpr-hook help
simple abbrev minibuffer nadvice loaddefs button faces cus-face macroexp
files text-properties overlay sha1 md5 base64 format env code-pages mule
custom widget hashtable-print-readable backquote make-network-process
dbusbind gfilenotify dynamic-setting system-font-setting
font-render-setting move-toolbar gtk x-toolkit x multi-tty emacs)

-- 
Jan Synacek
Software Engineer, Red Hat