Skip to content

Commit

Permalink
vim-patch:8.2.4724: current instance of last search pattern not easil…
Browse files Browse the repository at this point in the history
…y spotted

Problem:    Current instance of last search pattern not easily spotted.
Solution:   Add CurSearch highlighting. (closes vim/vim#10133)
vim/vim@a439938

This fixes CurSearch highlight for multiline match.
Omit screen redrawing code because Nvim redraws CurSearch differently.
  • Loading branch information
zeertzjq authored and kraftwerk28 committed Jun 1, 2022
1 parent 6aba29c commit 6c64ff9
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 51 deletions.
1 change: 1 addition & 0 deletions src/nvim/buffer_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,7 @@ typedef struct {
// match (may continue in next line)
buf_T *buf; // the buffer to search for a match
linenr_T lnum; // the line to search for a match
linenr_T lines; // number of lines starting from lnum
int attr; // attributes to be used for a match
int attr_cur; // attributes currently active in win_line()
linenr_T first_lnum; // first lnum to search for multi-line pat
Expand Down
10 changes: 9 additions & 1 deletion src/nvim/match.c
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ bool prepare_search_hl_line(win_T *wp, linenr_T lnum, colnr_T mincol, char_u **l
}
shl->startcol = MAXCOL;
shl->endcol = MAXCOL;
shl->lines = 0;
shl->attr_cur = 0;
shl->is_addpos = false;
if (cur != NULL) {
Expand All @@ -606,6 +607,11 @@ bool prepare_search_hl_line(win_T *wp, linenr_T lnum, colnr_T mincol, char_u **l
} else {
shl->endcol = MAXCOL;
}
if (shl->rm.endpos[0].lnum != shl->rm.startpos[0].lnum) {
shl->lines = shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
} else {
shl->lines = 1;
}
// Highlight one character for an empty match.
if (shl->startcol == shl->endcol) {
if ((*line)[shl->endcol] != NUL) {
Expand Down Expand Up @@ -668,10 +674,12 @@ int update_search_hl(win_T *wp, linenr_T lnum, colnr_T col, char_u **line, match
if (shl->endcol < next_col) {
shl->endcol = next_col;
}
// Use "CurSearch" highlight for current search match
// Highlight the match were the cursor is using the CurSearch
// group.
if (shl == search_hl
&& (HL_ATTR(HLF_LC) || wp->w_hl_ids[HLF_LC])
&& wp->w_cursor.lnum == lnum
&& wp->w_cursor.lnum < shl->lnum + shl->lines
&& wp->w_cursor.col >= shl->startcol
&& wp->w_cursor.col < shl->endcol) {
shl->attr_cur = win_hl_attr(wp, HLF_LC) ? win_hl_attr(wp, HLF_LC) : HL_ATTR(HLF_LC);
Expand Down
4 changes: 2 additions & 2 deletions src/nvim/option.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ typedef struct vimoption {

#define HIGHLIGHT_INIT \
"8:SpecialKey,~:EndOfBuffer,z:TermCursor,Z:TermCursorNC,@:NonText,d:Directory,e:ErrorMsg," \
"i:IncSearch,l:Search,m:MoreMsg,M:ModeMsg,n:LineNr,a:LineNrAbove,b:LineNrBelow,N:CursorLineNr," \
"G:CursorLineSign,O:CursorLineFold" \
"i:IncSearch,l:Search,y:CurSearch,m:MoreMsg,M:ModeMsg,n:LineNr,a:LineNrAbove,b:LineNrBelow," \
"N:CursorLineNr,G:CursorLineSign,O:CursorLineFold" \
"r:Question,s:StatusLine,S:StatusLineNC,c:VertSplit,t:Title,v:Visual,V:VisualNOS,w:WarningMsg," \
"W:WildMenu,f:Folded,F:FoldColumn,A:DiffAdd,C:DiffChange,D:DiffDelete,T:DiffText,>:SignColumn," \
"-:Conceal,B:SpellBad,P:SpellCap,R:SpellRare,L:SpellLocal,+:Pmenu,=:PmenuSel,x:PmenuSbar," \
Expand Down
28 changes: 28 additions & 0 deletions src/nvim/testdir/test_search.vim
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,34 @@ func Test_incsearch_substitute()
call Incsearch_cleanup()
endfunc

func Test_hlsearch_cursearch()
CheckScreendump

let lines =<< trim END
set hlsearch scrolloff=0
call setline(1, ['one', 'foo', 'bar', 'baz', 'foo', 'bar'])
hi Search ctermbg=yellow
hi CurSearch ctermbg=blue
END
call writefile(lines, 'Xhlsearch_cursearch')
let buf = RunVimInTerminal('-S Xhlsearch_cursearch', {'rows': 9, 'cols': 60})

call term_sendkeys(buf, "gg/foo\<CR>")
call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_single_line_1', {})

call term_sendkeys(buf, "n")
call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_single_line_2', {})

call term_sendkeys(buf, "?\<CR>")
call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_single_line_3', {})

call term_sendkeys(buf, "gg/foo\\nbar\<CR>")
call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_multiple_line', {})

call StopVimInTerminal(buf)
call delete('Xhlsearch_cursearch')
endfunc

" Similar to Test_incsearch_substitute() but with a screendump halfway.
func Test_incsearch_substitute_dump()
CheckOption incsearch
Expand Down
150 changes: 102 additions & 48 deletions test/functional/ui/searchhl_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -109,54 +109,108 @@ describe('search highlighting', function()
]])
end)

it('works for match under cursor', function()
screen:set_default_attr_ids({
[1] = {background = Screen.colors.Yellow},
[2] = {foreground = Screen.colors.Gray100, background = Screen.colors.Gray0},
[3] = {foreground = Screen.colors.Red},
})

command('highlight CurSearch guibg=Black guifg=White')
insert([[
There is no way that a bee should be
able to fly. Its wings are too small
to get its fat little body off the
ground. The bee, of course, flies
anyway because bees don't care what
humans think is impossible.]])

feed('/bee<CR>')
screen:expect{grid=[[
There is no way that a {2:^bee} should be |
able to fly. Its wings are too small |
to get its fat little body off the |
ground. The {1:bee}, of course, flies |
anyway because {1:bee}s don't care what |
humans think is impossible. |
{3:search hit BOTTOM, continuing at TOP} |
]]}

feed('nn')
screen:expect{grid=[[
There is no way that a {1:bee} should be |
able to fly. Its wings are too small |
to get its fat little body off the |
ground. The {1:bee}, of course, flies |
anyway because {2:^bee}s don't care what |
humans think is impossible. |
/bee |
]]}

feed('N')
screen:expect{grid=[[
There is no way that a {1:bee} should be |
able to fly. Its wings are too small |
to get its fat little body off the |
ground. The {2:^bee}, of course, flies |
anyway because {1:bee}s don't care what |
humans think is impossible. |
?bee |
]]}
describe('CurSearch highlight', function()
before_each(function()
screen:set_default_attr_ids({
[1] = {background = Screen.colors.Yellow}, -- Search
[2] = {foreground = Screen.colors.White, background = Screen.colors.Black}, -- CurSearch
[3] = {foreground = Screen.colors.Red}, -- WarningMsg
})
command('highlight CurSearch guibg=Black guifg=White')
end)

it('works for match under cursor', function()
insert([[
There is no way that a bee should be
able to fly. Its wings are too small
to get its fat little body off the
ground. The bee, of course, flies
anyway because bees don't care what
humans think is impossible.]])

feed('/bee<CR>')
screen:expect{grid=[[
There is no way that a {2:^bee} should be |
able to fly. Its wings are too small |
to get its fat little body off the |
ground. The {1:bee}, of course, flies |
anyway because {1:bee}s don't care what |
humans think is impossible. |
{3:search hit BOTTOM, continuing at TOP} |
]]}

feed('nn')
screen:expect{grid=[[
There is no way that a {1:bee} should be |
able to fly. Its wings are too small |
to get its fat little body off the |
ground. The {1:bee}, of course, flies |
anyway because {2:^bee}s don't care what |
humans think is impossible. |
/bee |
]]}

feed('N')
screen:expect{grid=[[
There is no way that a {1:bee} should be |
able to fly. Its wings are too small |
to get its fat little body off the |
ground. The {2:^bee}, of course, flies |
anyway because {1:bee}s don't care what |
humans think is impossible. |
?bee |
]]}
end)

it('works for multiline match', function()
insert([[
one
foo
bar
baz
foo
bar]])
feed('gg/foo<CR>')
screen:expect([[
one |
{2:^foo} |
bar |
baz |
{1:foo} |
bar |
/foo |
]])
feed('n')
screen:expect([[
one |
{1:foo} |
bar |
baz |
{2:^foo} |
bar |
/foo |
]])
feed('?<CR>')
screen:expect([[
one |
{2:^foo} |
bar |
baz |
{1:foo} |
bar |
?foo |
]])
feed('gg/foo\\nbar<CR>')
screen:expect([[
one |
{2:^foo} |
{1:bar} |
baz |
{1:foo} |
{1:bar} |
/foo\nbar |
]])
end)
end)

it('highlights after EOL', function()
Expand Down

0 comments on commit 6c64ff9

Please sign in to comment.