Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Implement SearchCurrent for current match hl #11082

Closed
wants to merge 8 commits into from

Conversation

certik
Copy link

@certik certik commented Sep 23, 2019

Uses SearchCurrent to highlight the current match under cursor.

How to use: put this into your init.vim:

hi Search ctermbg=blue ctermfg=red
hi SearchCurrent ctermbg=green ctermfg=red

Then the current match will be highlighted using SearchCurrent, while the rest will be highlighted using Search. An example:

nvim_search_current

Issues to resolve:

  • When "n" is pressed, the colors do not get updated. One has to press /int<CR> (in the screenshot above) when the cursor is at some other place, and it will correctly highlight that new match. One has to figure how to redraw when "n" or "N" is pressed.
  • Update tests for the new behavior (a few currently fail)
  • document the new option SearchCurrent

Fixes #4600.

Uses SearchCurrent to highlight the current match under cursor.
@blueyed
Copy link
Contributor

blueyed commented Sep 23, 2019

For reference: related Vim issue: vim/vim#2819

@justinmk
Copy link
Member

justinmk commented Sep 24, 2019

would it make sense to re-use IncSearch for this instead of adding another highlight group?

One has to figure how to redraw when "n" or "N" is pressed.

In nv_next() try adding redraw_buf_line_later().

@justinmk justinmk added the enhancement feature request label Sep 24, 2019
@justinmk justinmk added this to the 0.5 milestone Sep 24, 2019
@certik
Copy link
Author

certik commented Sep 24, 2019

@justinmk I reused IncSearch, I think that's better. I now redraw the screen in nv_next(), and the problem is fixed! Both "n" and "N" now works. Thanks for the suggestion. Here is how it looks like, with default nvim settings:

nvim_search_current2

And here is how it looks like with hi IncSearch ctermfg=green:

nvim_search_current3

Which I think looks better. I wonder if we should change the default IncSearch settings?

(In order for the background to be green, I had to set the ctermfg (i.e, foreground), which seems weird.)


At this point, I think the basic prototype works exactly as expected. I have a few questions:

  • How does nvim redraw the screen? When a redraw is asked, is the whole screen always redrawn from scratch, and then nvim compares to the old screen and figures out an efficient diff to be sent to the terminal (much like ncurses works)? Or does nvim have some elaborate update mechanism to only draw things that changed?
  • Above I call redraw_later(NOT_VALID), which I assume redraws the whole screen. Is it a waste to always redraw everything in nv_next?
  • Should I figure out how to call the redraw_buf_line_later on the old line and the new line?
  • For efficiency reasons, should this feature in this PR be even considered, since it seems we now have to redraw the screen in nv_next, one way or another.

@certik
Copy link
Author

certik commented Sep 24, 2019

This simplification can be applied:

diff --git a/src/nvim/buffer_defs.h b/src/nvim/buffer_defs.h
index dc0e718d6..16c7804be 100644
--- a/src/nvim/buffer_defs.h
+++ b/src/nvim/buffer_defs.h
@@ -940,7 +940,6 @@ typedef struct {
   buf_T       *buf;     // the buffer to search for a match
   linenr_T lnum;        // the line to search for a match
   int attr;             // attributes to be used for a match
-  int attr_cur2;        // attributes to be used for a current match
   int attr_cur;         // attributes currently active in win_line()
   linenr_T first_lnum;  // first lnum to search for multi-line pat
   colnr_T startcol;     // in win_line() points to char where HL starts
diff --git a/src/nvim/screen.c b/src/nvim/screen.c
index baa100333..766ad4657 100644
--- a/src/nvim/screen.c
+++ b/src/nvim/screen.c
@@ -3001,7 +3001,7 @@ win_line (
               if (wp->w_cursor.lnum == lnum
                   && wp->w_cursor.col >= (long)shl->startcol
                   && wp->w_cursor.col < (long)shl->endcol) {
-                shl->attr_cur = shl->attr_cur2;
+                shl->attr_cur = win_hl_attr(wp, HLF_I);
               } else {
                 shl->attr_cur = shl->attr;
               }
@@ -5613,7 +5613,6 @@ static void init_search_hl(win_T *wp)
   search_hl.lnum = 0;
   search_hl.first_lnum = 0;
   search_hl.attr = win_hl_attr(wp, HLF_L);
-  search_hl.attr_cur2 = win_hl_attr(wp, HLF_I);
 
   // time limit is set at the toplevel, for all windows
 }

and things still work. @justinmk should I do that? I don't know what style is preferred.

@justinmk
Copy link
Member

should I do that? I don't know what style is preferred

Sure.

Note that Bram seems to prefer the SearchCurrent approach, so we should probably stick with that. It can be linked with IncSearch by default.

@certik
Copy link
Author

certik commented Sep 25, 2019

Sure, I'll revert it. It should probably be called something like SearchLastMatch, per Bram's comment.

@certik
Copy link
Author

certik commented Sep 26, 2019

I updated the PR. Interesting is that before one did:

hi IncSearch ctermfg=green

but now one has to do:

hi SearchLastFoundMatch ctermbg=green ctermfg=black

to get the same result. It looks like the IncSearch is doing some inverting.

Anyway, the PR is now really simple. Just four very little changes.

@justinmk do you think that this change looks good? If so, I'll start working on updating the tests to pass.

@justinmk justinmk removed the RFC label Jan 28, 2020
@janlazo
Copy link
Contributor

janlazo commented Dec 26, 2020

Cannot merge without tests.

@janlazo janlazo modified the milestones: 0.5, 0.5.1 Dec 26, 2020
@bfredl bfredl modified the milestones: 0.5.1, 0.6 Aug 15, 2021
@bfredl bfredl modified the milestones: 0.6, 0.7 Nov 27, 2021
@zeertzjq zeertzjq added the needs:response waiting for reply from the author label Dec 29, 2021
@clason
Copy link
Member

clason commented Dec 29, 2021

Bump -- that would be quite nice to have! @certik would you be interested in finishing this (rebase and fix build, docs, and test)?

@bfredl is this approach still fine (especially the forced redraw in nv_next)?

@clason clason added core Nvim core functionality or code highlight labels Dec 29, 2021
src/nvim/normal.c Outdated Show resolved Hide resolved
Copy link
Member

@zeertzjq zeertzjq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A change in cursor_spec.lua should be needed:

diff --git a/test/functional/ui/cursor_spec.lua b/test/functional/ui/cursor_spec.lua
index 03cd4bfd0..4c51547e2 100644
--- a/test/functional/ui/cursor_spec.lua
+++ b/test/functional/ui/cursor_spec.lua
@@ -212,10 +212,10 @@ describe('ui/cursor', function()
         if m.blinkwait then m.blinkwait = 700 end
       end
       if m.hl_id then
-          m.hl_id = 60
+          m.hl_id = 61
           m.attr = {background = Screen.colors.DarkGray}
       end
-      if m.id_lm then m.id_lm = 61 end
+      if m.id_lm then m.id_lm = 62 end
     end
 
     -- Assert the new expectation.

BTW the SearchLastFoundMatch name is really too long.

@numToStr
Copy link
Contributor

I think setting the highlight group somehow affects the matchit highlighting

2021-12-29.16-54-00.mp4

Note: I changed the hl-group name to SearchCurrent

@certik
Copy link
Author

certik commented Dec 29, 2021

This would be very nice to have, but I need help from neovim developers to help me get this over the finish line. I need help with writing tests, as well as feedback if any changes are needed.

@clason clason removed the needs:response waiting for reply from the author label Dec 29, 2021
@clason
Copy link
Member

clason commented Dec 29, 2021

Before any of that, we might want to bikeshed on the name; I agree that SearchLastFoundMatch is a mouthful and would prefer SearchCurrentMatch or even SearchCurrent. (I know Bram has issues with that name as it's not technically descriptive -- but that's what the documentation is for!)


Then, I think the first thing would be to add the new highlight group here:

"i:IncSearch,l:Search,m:MoreMsg,M:ModeMsg,n:LineNr,a:LineNrAbove,b:LineNrBelow,N:CursorLineNr," \

and here:
"IncSearch cterm=reverse gui=reverse",

(probably easiest to link it to IncSearch by default -- or Search, in the hope that this would require fewer changes).

Documentation goes here:

neovim/runtime/doc/syntax.txt

Lines 5170 to 5172 in c46f7ca

*hl-Search*
Search Last search pattern highlighting (see 'hlsearch').
Also used for similar items that need to stand out.

For tests, a good starting point would be to adapt one of the tests here: https://github.com/neovim/neovim/blob/master/test/functional/ui/searchhl_spec.lua
(I'm not a test expert, so I hope someone else will step in here, as that can indeed be rough the first time ;))

@famiu
Copy link
Member

famiu commented Feb 14, 2022

Do you mind if I take over this PR? I think I might be able to finish it if you don't have time

@clason
Copy link
Member

clason commented Apr 2, 2022

@famiu I think you can take the silence for a "yes".

@famiu
Copy link
Member

famiu commented Apr 2, 2022

@famiu I think you can take the silence for a "yes".

Will work on it once I get the PC

@leungbk
Copy link
Contributor

leungbk commented Apr 10, 2022

An initial attempt at this has been merged upstream (vim/vim#10133 (comment)), with some small issues.

@certik
Copy link
Author

certik commented Apr 10, 2022

@famiu yes, please take over the PR. I don't have the time to learn enough about neovim to bring it over the finish line, it looks like tests have to be written etc.

@zeertzjq
Copy link
Member

Superseded by #18081

@zeertzjq zeertzjq closed this Apr 20, 2022
@zeertzjq zeertzjq modified the milestones: 0.9, 0.8 Aug 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Nvim core functionality or code enhancement feature request highlight
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feature request: hl-IncSearch / hl-Search alternating during normal searches