1

I am trying to configure hl-line or hl-line+ in emacs to respect the existing highlights/text background colors in the buffer.

I have configured hl-line in ~/.emacs as follows:

(require 'hl-line)
(global-hl-line-mode 1)

Then I highlight the symbol at point using highlight-symbol-at-point (M-s h .). This highlights the symbol under the cursor all over the buffer with a yellow background.

However, when I move the cursor over a line containing that symbol, the hl-line overlay hides the yellow background. My expectation would be for the line to be highlighted, but for the yellow background to be respected.

After doing some digging, I also tried with the hl-line+ package as it has an overlay priority option that sounds promising. I downloaded the hl-line+.el file and setup my ~/.emacs as follows:

(add-to-list 'load-path (expand-file-name "~/.emacs.d/packages/hl-line+/"))
(require 'hl-line+)
(global-hl-line-mode 1)
(setq-default hl-line-overlay-priority -100)

However, this still has the same incorrect behivour to hide the existing yellow background.

Anybody knows how to configure either of these packages to respect the existing background colors?

Drew
  • 29,895
  • 7
  • 74
  • 104
ed56
  • 11
  • 2

2 Answers2

0

highlight-symbol uses font-lock, which adds text properties (face or font-lock-face) to chars in the buffer.

Overlay properties (including face and `font-lock-face) are not applied to chars in the buffer. They're applied to buffer positions. They "overlay" the buffer contents; they're not part of the buffer contents.

Overlay properties always take priority over text properties. This means overlay highlighting always overrides text-property highlighting (e.g. by font-lock).

Overlay priorities only specify the relative priority among overlays. Again, any overlay highlighting overrides any text-property highlighting.

So I think the answer is that you can't do what you request. (Someone else will correct me, if there's a way around this.)

Drew
  • 29,895
  • 7
  • 74
  • 104
  • Thanks for taking the time to explain, let's hope somebody can come up with a workaround. This seems such a basic feature, i.e. highlight a word + highlight current line, it is strange not working out of the box. Sometimes it feels emacs purposedly make the easy difficult. – ed56 Nov 09 '21 at 14:11
0

Reading the hi-lock.el documentation, this explanation stood out:

"In buffers where Font Lock mode is enabled, patterns are highlighted using font lock. In buffers where Font Lock mode is disabled, patterns are applied using overlays"

So as it turns out, the hi-lock.el package is able to highlight using overlays, but only when font lock mode is disabled. I tested this by disabling font lock with M-x font-lock-mode, and I can confirm the highlighting is now properly displayed when also highlighting the current line with hl-line. The problem is that all language syntax highlighting is now gone, so this option is no good.

If anybody knows how to configure hi-lock.el to use overlays with font-lock-mode enabled, then that would be the best answer.

In the meantime, there is an alternative MELPA package that supports overlay highlighting, and it woks like a charm with hl-line: symbol-overlay.el.

Having said that, I am not too fond of default key bindings and highlighting colors. For reference I leave here my configuration:

(require 'symbol-overlay)
(global-set-key (kbd "<f2>") 'symbol-overlay-jump-next)
(global-set-key (kbd "S-<f2>") 'symbol-overlay-jump-prev)
(global-set-key (kbd "C-<f2>") 'symbol-overlay-put)
(global-set-key (kbd "M-<f2>") 'symbol-overlay-query-replace)
(global-set-key (kbd "<f7>") 'symbol-overlay-mode)
(global-set-key (kbd "<f8>") 'symbol-overlay-remove-all)

(set-face-attribute 'symbol-overlay-default-face nil :background "coral1" :foreground "black")
(set-face-attribute 'symbol-overlay-face-1 nil :background "gold2" :foreground "black")
(set-face-attribute 'symbol-overlay-face-2 nil :background "chocolate2" :foreground "black")
(set-face-attribute 'symbol-overlay-face-3 nil :background "PaleGreen2" :foreground "black")
(set-face-attribute 'symbol-overlay-face-4 nil :background "SkyBlue2" :foreground "black")
(set-face-attribute 'symbol-overlay-face-5 nil :background "PaleVioletRed2" :foreground "black")
(set-face-attribute 'symbol-overlay-face-6 nil :background "IndianRed2" :foreground "black")
(set-face-attribute 'symbol-overlay-face-7 nil :background "PaleTurquoise2" :foreground "black")
(set-face-attribute 'symbol-overlay-face-8 nil :background "MediumOrchid2" :foreground "black")
ed56
  • 11
  • 2