evil icon indicating copy to clipboard operation
evil copied to clipboard

Selection behind persistent search highlight (doesn't match Vim)

Open duianto opened this issue 6 years ago • 3 comments

Issue type

  • Bug report

Environment

Evil version: 6fde982d731e2cc4e5f6bded6f8955ab2daee3b7
Emacs version: GNU Emacs 26.1 (build 1, x86_64-w64-mingw32) of 2018-05-30
Operating System: Windows 10 Version 1803
Evil installation type: MELPA
Graphical/Terminal: Graphical
Tested in a make emacs session (see CONTRIBUTING.md): Yes, (Clean Emacs setup, only Evil installed)

Reproduction steps

  • Enable persistent search highlighting by adding the following to init.el:
;; This needs to be set before the evil package is loaded
(setq evil-search-module 'evil-search)

before the lines that load the evil package:

(require 'evil)
(evil-mode 1)
  • Re/Start Emacs

  • Open a new buffer: C-x b test <return>

  • Type or copy and paste this line into the new buffer:

pre-selected-post
  • Search for it: / pre-selected-post <return>

  • Move the cursor to the "s" in "selected"

  • Press: v to start a char selection

  • Press: e to move the cursor to the end of the word "selected".

Expected behavior

The selection should appear in front of the search highlight, like it does in Vim:

vim-visual-char-selection-in-front-of-search-pattern

That would make it easier to select part of a search highlighted phrase.

In Vim a block selection C-v from a line above, to a line below looks like this.

vim-visual-block-selection-in-front-of-search-pattern

Actual behavior

The selection appears behind the search highlight.

emacs-evil-visual-char-selection-behind-search-pattern

A block selection makes it clear that the selection appears behind the search highlight in Emacs.

emacs-evil-visual-block-selection-behind-search-pattern

Further notes

Possible Cause

The selections overlay priority is 100, but the search highlights overlay priority is 1000.

The overlay priority of both the selection and the search highlight can be seen by following the steps above, then:

  • Move the cursor to the beginning of the visual char selected word "selected" by pressing o.

{} = search highlight

() = selection

[] = cursor

{pre-([s]elected)-post}
  • Then describe the character under the cursor: M-x describe-char RET

The bottom of the help window shows:

There are 2 overlays here:
 From 1 to 18
  evil-ex-hl           evil-ex-search
  face                 evil-ex-lazy-highlight
  priority             1000
 From 5 to 13
  face                 region
  priority             (nil . 100)
  window               #<window 3 on test>

This might be the commit where the search overlay priority was introduced and set to 1000:

search: add basic highlighting framework

A possible reason for it being set to 1000, might have been to match the Emacs search highlight.

Because when the automatic Emacs C-s search highlight is turned off:

(setq lazy-highlight-cleanup nil)

and the steps above are followed but one searches with C-s instead of /, then the describe char output becomes:

There are 2 overlays here:
 From 1 to 18
  face                 lazy-highlight
  priority             1000
  window               #<window 3 on *scratch*>
 From 5 to 13
  face                 region
  priority             (nil . 100)
  window               #<window 3 on *scratch*>

Possible solutions

If lowering the search highlights priority or raising the selection priority causes other issues, then maybe either of these suggestions could work:

  • The search highlights overlay priority could be lowered below the selection priority, if both the search highlight and a visual selection are active.

  • A new overlay with another color could be shown where the search highlight and the selection overlap.

duianto avatar Sep 15 '18 17:09 duianto

My temporary solution:

(define-advice evil-ex-hl-match-hook (:around (fn hl) add-priority-reset-hook)
    (or (funcall fn hl)
        (lambda (_ ov)
          (overlay-put ov 'priority 0))))

twlz0ne avatar Jul 09 '20 11:07 twlz0ne

Confirmed, the workaround works for me as well:

emacs_2020-07-11_14-09-25

emacs_2020-07-11_14-09-19

Thanks

Evil version 1.14.0 GNU Emacs 26.3 (build 1, x86_64-w64-mingw32) of 2019-08-29 Windows 1903

duianto avatar Jul 11 '20 12:07 duianto

Is there a reason why the visual selection overlay has a lower priority than the search highlight ones? A PR to fix this would be trivial but I don't like to change behavior without knowing the reasons behind the original decision first.

Townk avatar May 22 '21 01:05 Townk