vim-patch:8.1.0295: no 'incsearch' highlighting for :vimgrep and similar

Problem:    No 'incsearch' highlighting for :vimgrep and similar commands.
Solution:   Parse the :vimgrep command and similar ones to locate the search
            pattern. (Hirohito Higashi, closes vim/vim#3344)
264cf5cfaf
This commit is contained in:
Aufar Gilbran
2020-08-19 00:49:51 +08:00
parent ab7e101540
commit 77bb48e740
3 changed files with 152 additions and 34 deletions

View File

@@ -307,48 +307,67 @@ static bool do_incsearch_highlighting(int firstc, incsearch_state_T *s,
cmdmod = save_cmdmod;
cmd = skip_range(ea.cmd, NULL);
if (*cmd == 's' || *cmd == 'g' || *cmd == 'v') {
if (*cmd == 's' || *cmd == 'g' || *cmd == 'v' || *cmd == 'l') {
// Skip over "substitute" to find the pattern separator.
for (p = cmd; ASCII_ISALPHA(*p); p++) {}
if (*skipwhite(p) != NUL
&& (STRNCMP(cmd, "substitute", p - cmd) == 0
|| STRNCMP(cmd, "smagic", p - cmd) == 0
|| STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0
|| STRNCMP(cmd, "sort", p - cmd) == 0
|| STRNCMP(cmd, "global", p - cmd) == 0
|| STRNCMP(cmd, "vglobal", p - cmd) == 0)) {
if (*cmd == 's' && cmd[1] == 'm') {
p_magic = true;
}
else if (*cmd == 's' && cmd[1] == 'n') {
p_magic = false;
}
// Check for "global!/".
if (*cmd == 'g' && *p == '!') {
p++;
if (*skipwhite(p) == NUL) {
return false;
if (*skipwhite(p) != NUL) {
if (STRNCMP(cmd, "substitute", p - cmd) == 0
|| STRNCMP(cmd, "smagic", p - cmd) == 0
|| STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0
|| STRNCMP(cmd, "sort", MAX(p - cmd,3)) == 0
|| STRNCMP(cmd, "global", p - cmd) == 0
|| STRNCMP(cmd, "vglobal", p - cmd) == 0) {
if (*cmd == 's' && cmd[1] == 'm') {
p_magic = true;
}
}
// For ":sort" skip over flags.
if (cmd[0] == 's' && cmd[1] == 'o') {
while (ASCII_ISALPHA(*(p = skipwhite(p)))) {
++p;
else if (*cmd == 's' && cmd[1] == 'n') {
p_magic = false;
}
if (*p == NUL) {
return false;
}
}
p = skipwhite(p);
delim = *p++;
end = skip_regexp(p, delim, p_magic, NULL);
// Check for "global!/".
if (*cmd == 'g' && *p == '!') {
p++;
if (*skipwhite(p) == NUL) {
return false;
}
}
// For ":sort" skip over flags.
if (cmd[0] == 's' && cmd[1] == 'o') {
while (ASCII_ISALPHA(*(p = skipwhite(p)))) {
++p;
}
if (*p == NUL) {
return false;
}
}
p = skipwhite(p);
delim = *p++;
end = skip_regexp(p, delim, p_magic, NULL);
} else if (STRNCMP(cmd, "vimgrep", MAX(p - cmd, 3)) == 0
|| STRNCMP(cmd, "vimgrepadd", MAX(p - cmd, 8)) == 0
|| STRNCMP(cmd, "lvimgrep", MAX(p - cmd, 2)) == 0
|| STRNCMP(cmd, "lvimgrepadd", MAX(p - cmd, 9)) == 0) {
// Check for "!/".
if (*p == '!') {
p++;
if (*skipwhite(p) == NUL) {
return false;
}
}
p = skipwhite(p);
delim = (vim_isIDc(*p)) ? ' ' : *p++;
end = skip_regexp(p, delim, p_magic, NULL);
}
else {
end = p;
delim = -1;
}
if (end > p || *end == delim) {
pos_T save_cursor = curwin->w_cursor;
// found a non-empty pattern
// found a non-empty pattern or //
*skiplen = (int)(p - ccline.cmdbuff);
*patlen = (int)(end - p);

View File

@@ -719,6 +719,53 @@ func Test_incsearch_ssort_dump()
call delete('Xis_sort_script')
endfunc
" Similar to Test_incsearch_substitute_dump() for :vimgrep famiry
func Test_incsearch_vimgrep_dump()
if !exists('+incsearch')
return
endif
if !CanRunVimInTerminal()
throw 'Skipped: cannot make screendumps'
endif
call writefile([
\ 'set incsearch hlsearch scrolloff=0',
\ 'call setline(1, ["another one 2", "that one 3", "the one 1"])',
\ ], 'Xis_vimgrep_script')
let buf = RunVimInTerminal('-S Xis_vimgrep_script', {'rows': 9, 'cols': 70})
" Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
" the 'ambiwidth' check.
sleep 100m
" Need to send one key at a time to force a redraw.
call term_sendkeys(buf, ':vimgrep on')
sleep 100m
call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_01', {})
call term_sendkeys(buf, "\<Esc>")
call term_sendkeys(buf, ':vimg /on/ *.txt')
sleep 100m
call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_02', {})
call term_sendkeys(buf, "\<Esc>")
call term_sendkeys(buf, ':vimgrepadd "\<on')
sleep 100m
call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_03', {})
call term_sendkeys(buf, "\<Esc>")
call term_sendkeys(buf, ':lv "tha')
sleep 100m
call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_04', {})
call term_sendkeys(buf, "\<Esc>")
call term_sendkeys(buf, ':lvimgrepa "the" **/*.txt')
sleep 100m
call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_05', {})
call term_sendkeys(buf, "\<Esc>")
call StopVimInTerminal(buf)
call delete('Xis_vimgrep_script')
endfunc
func Test_incsearch_with_change()
if !has('timers') || !exists('+incsearch') || !CanRunVimInTerminal()
throw 'Skipped: cannot make screendumps and/or timers feature and/or incsearch option missing'

View File

@@ -587,4 +587,56 @@ describe('search cmdline', function()
]])
feed('<esc>')
end)
it('incsearch works with :vimgrep family', function()
-- oldtest: Test_incsearch_vimgrep_dump().
screen:try_resize(30, 4)
command('set incsearch hlsearch scrolloff=0')
funcs.setline(1, {'another one 2', 'that one 3', 'the one 1'})
feed(':vimgrep on')
screen:expect([[
another {inc:on}e 2 |
that {hl:on}e 3 |
the {hl:on}e 1 |
:vimgrep on^ |
]])
feed('<esc>')
feed(':vimg /on/ *.txt')
screen:expect([[
another {inc:on}e 2 |
that {hl:on}e 3 |
the {hl:on}e 1 |
:vimg /on/ *.txt^ |
]])
feed('<esc>')
feed(':vimgrepadd "\\<LT>on')
screen:expect([[
another {inc:on}e 2 |
that {hl:on}e 3 |
the {hl:on}e 1 |
:vimgrepadd "\<on^ |
]])
feed('<esc>')
feed(':lv "tha')
screen:expect([[
another one 2 |
{inc:tha}t one 3 |
the one 1 |
:lv "tha^ |
]])
feed('<esc>')
feed(':lvimgrepa "the" **/*.txt')
screen:expect([[
ano{inc:the}r one 2 |
that one 3 |
{hl:the} one 1 |
:lvimgrepa "the" **/*.txt^ |
]])
feed('<esc>')
end)
end)