Skip to content

Commit f8f8b2e

Browse files
committed
patch 8.0.1250: 'hlsearch' highlighting not removed after incsearch
Problem: 'hlsearch' highlighting not removed after incsearch (lacygoill) Solution: Redraw all windows. Start search at the end of the match. Improve how CTRL-G works with incremental search. Add tests. (Christian Brabandt, Hirohito Higashi, haya14busa, closes #2267)
1 parent c20e0d5 commit f8f8b2e

4 files changed

Lines changed: 125 additions & 3 deletions

File tree

runtime/doc/options.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4454,8 +4454,8 @@ A jump table for the options with a short description can be found at |Q_op|.
44544454
Example: >
44554455
augroup vimrc-incsearch-highlight
44564456
autocmd!
4457-
autocmd CmdlineEnter [/\?] :set hlsearch
4458-
autocmd CmdlineLeave [/\?] :set nohlsearch
4457+
autocmd CmdlineEnter /,\? :set hlsearch
4458+
autocmd CmdlineLeave /,\? :set nohlsearch
44594459
augroup END
44604460
<
44614461
CTRL-L can be used to add one character from after the current match

src/ex_getln.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1717,12 +1717,19 @@ getcmdline(
17171717
pos_T t;
17181718
int search_flags = SEARCH_NOOF;
17191719

1720+
if (ccline.cmdlen == 0)
1721+
goto cmdline_not_changed;
1722+
17201723
save_last_search_pattern();
17211724
cursor_off();
17221725
out_flush();
17231726
if (c == Ctrl_G)
17241727
{
17251728
t = match_end;
1729+
if (LT_POS(match_start, match_end))
1730+
/* start searching at the end of the match
1731+
* not at the beginning of the next column */
1732+
(void)decl(&t);
17261733
search_flags += SEARCH_COL;
17271734
}
17281735
else
@@ -1945,6 +1952,7 @@ getcmdline(
19451952
{
19461953
i = 0;
19471954
SET_NO_HLSEARCH(TRUE); /* turn off previous highlight */
1955+
redraw_all_later(SOME_VALID);
19481956
}
19491957
else
19501958
{
@@ -2082,7 +2090,7 @@ getcmdline(
20822090
curwin->w_botline = old_botline;
20832091
highlight_match = FALSE;
20842092
validate_cursor(); /* needed for TAB */
2085-
redraw_later(SOME_VALID);
2093+
redraw_all_later(SOME_VALID);
20862094
}
20872095
#endif
20882096

src/testdir/test_search.vim

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,117 @@ func Test_search_cmdline5()
397397
bw!
398398
endfunc
399399

400+
func Test_search_cmdline6()
401+
" Test that consecutive matches
402+
" are caught by <c-g>/<c-t>
403+
if !exists('+incsearch')
404+
return
405+
endif
406+
" need to disable char_avail,
407+
" so that expansion of commandline works
408+
call test_override("char_avail", 1)
409+
new
410+
call setline(1, [' bbvimb', ''])
411+
set incsearch
412+
" first match
413+
norm! gg0
414+
call feedkeys("/b\<cr>", 'tx')
415+
call assert_equal([0,1,2,0], getpos('.'))
416+
" second match
417+
norm! gg0
418+
call feedkeys("/b\<c-g>\<cr>", 'tx')
419+
call assert_equal([0,1,3,0], getpos('.'))
420+
" third match
421+
norm! gg0
422+
call feedkeys("/b\<c-g>\<c-g>\<cr>", 'tx')
423+
call assert_equal([0,1,7,0], getpos('.'))
424+
" first match again
425+
norm! gg0
426+
call feedkeys("/b\<c-g>\<c-g>\<c-g>\<cr>", 'tx')
427+
call assert_equal([0,1,2,0], getpos('.'))
428+
set nowrapscan
429+
" last match
430+
norm! gg0
431+
call feedkeys("/b\<c-g>\<c-g>\<c-g>\<cr>", 'tx')
432+
call assert_equal([0,1,7,0], getpos('.'))
433+
" clean up
434+
set wrapscan&vim
435+
set noincsearch
436+
call test_override("char_avail", 0)
437+
bw!
438+
endfunc
439+
440+
func Test_search_cmdline7()
441+
" Test that an pressing <c-g> in an empty command line
442+
" does not move the cursor
443+
if !exists('+incsearch')
444+
return
445+
endif
446+
" need to disable char_avail,
447+
" so that expansion of commandline works
448+
call test_override("char_avail", 1)
449+
new
450+
let @/='b'
451+
call setline(1, [' bbvimb', ''])
452+
set incsearch
453+
" first match
454+
norm! gg0
455+
" moves to next match of previous search pattern, just like /<cr>
456+
call feedkeys("/\<c-g>\<cr>", 'tx')
457+
call assert_equal([0,1,2,0], getpos('.'))
458+
" moves to next match of previous search pattern, just like /<cr>
459+
call feedkeys("/\<cr>", 'tx')
460+
call assert_equal([0,1,3,0], getpos('.'))
461+
" moves to next match of previous search pattern, just like /<cr>
462+
call feedkeys("/\<c-t>\<cr>", 'tx')
463+
call assert_equal([0,1,7,0], getpos('.'))
464+
set noincsearch
465+
call test_override("char_avail", 0)
466+
bw!
467+
endfunc
468+
469+
func Test_search_cmdline8()
470+
" Highlighting is cleared in all windows
471+
" since hls applies to all windows
472+
if !exists('+incsearch') || !has('terminal') || has('gui_running') || winwidth(0) < 30
473+
return
474+
endif
475+
if has("win32")
476+
throw "Skipped: Bug with sending <ESC> to terminal window not fixed yet"
477+
endif
478+
let h = winheight(0)
479+
if h < 3
480+
return
481+
endif
482+
" Prepare buffer text
483+
let lines = ['abb vim vim vi', 'vimvivim']
484+
call writefile(lines, 'Xsearch.txt')
485+
let g:buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', 'Xsearch.txt'], {'term_rows': 3})
486+
487+
call term_wait(g:buf, 200)
488+
call assert_equal(lines[0], term_getline(g:buf, 1))
489+
call assert_equal(lines[1], term_getline(g:buf, 2))
490+
491+
call term_sendkeys(g:buf, ":set incsearch hlsearch\<cr>")
492+
call term_sendkeys(g:buf, ":14vsp\<cr>")
493+
call term_sendkeys(g:buf, "/vim\<cr>")
494+
call term_sendkeys(g:buf, "/b\<esc>")
495+
call term_sendkeys(g:buf, "gg0")
496+
call term_wait(g:buf, 500)
497+
let screen_line = term_scrape(g:buf, 1)
498+
let [a0,a1,a2,a3] = [screen_line[3].attr, screen_line[4].attr,
499+
\ screen_line[18].attr, screen_line[19].attr]
500+
call assert_notequal(a0, a1)
501+
call assert_notequal(a0, a3)
502+
call assert_notequal(a1, a2)
503+
call assert_equal(a0, a2)
504+
call assert_equal(a1, a3)
505+
" clean up
506+
call delete('Xsearch.txt')
507+
508+
bwipe!
509+
endfunc
510+
400511
" Tests for regexp with various magic settings
401512
func Test_search_regexp()
402513
enew!
@@ -566,6 +677,7 @@ func Test_search_cmdline_incsearch_highlight_attr()
566677
let attr_line2 = [a0,a0,a0,a0,a0,a0,a0,a0]
567678
call assert_equal(attr_line1, map(term_scrape(g:buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
568679
call assert_equal(attr_line2, map(term_scrape(g:buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
680+
call delete('Xsearch.txt')
569681

570682
call delete('Xsearch.txt')
571683
bwipe!

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,8 @@ static char *(features[]) =
761761

762762
static int included_patches[] =
763763
{ /* Add new patch number below this line */
764+
/**/
765+
1250,
764766
/**/
765767
1249,
766768
/**/

0 commit comments

Comments
 (0)