robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 9a29622b3b00ddb601

master
70.3 KiB2380 linesraw
1" Test for the search command
2
3source shared.vim
4source screendump.vim
5source check.vim
6
7func Test_search_cmdline()
8  CheckOption incsearch
9
10  " need to disable char_avail,
11  " so that expansion of commandline works
12  call Ntest_override("char_avail", 1)
13  new
14  call setline(1, ['  1', '  2 these', '  3 the', '  4 their', '  5 there', '  6 their', '  7 the', '  8 them', '  9 these', ' 10 foobar'])
15  " Test 1
16  " CTRL-N / CTRL-P skips through the previous search history
17  set noincsearch
18  :1
19  call feedkeys("/foobar\<cr>", 'tx')
20  call feedkeys("/the\<cr>", 'tx')
21  call assert_equal('the', @/)
22  call feedkeys("/thes\<C-P>\<C-P>\<cr>", 'tx')
23  call assert_equal('foobar', @/)
24
25  " Test 2
26  " Ctrl-G goes from one match to the next
27  " until the end of the buffer
28  set incsearch nowrapscan
29  :1
30  " first match
31  call feedkeys("/the\<cr>", 'tx')
32  call assert_equal('  2 these', getline('.'))
33  :1
34  " second match
35  call feedkeys("/the\<C-G>\<cr>", 'tx')
36  call assert_equal('  3 the', getline('.'))
37  call assert_equal([0, 0, 0, 0], getpos('"'))
38  :1
39  " third match
40  call feedkeys("/the".repeat("\<C-G>", 2)."\<cr>", 'tx')
41  call assert_equal('  4 their', getline('.'))
42  :1
43  " fourth match
44  call feedkeys("/the".repeat("\<C-G>", 3)."\<cr>", 'tx')
45  call assert_equal('  5 there', getline('.'))
46  :1
47  " fifth match
48  call feedkeys("/the".repeat("\<C-G>", 4)."\<cr>", 'tx')
49  call assert_equal('  6 their', getline('.'))
50  :1
51  " sixth match
52  call feedkeys("/the".repeat("\<C-G>", 5)."\<cr>", 'tx')
53  call assert_equal('  7 the', getline('.'))
54  :1
55  " seventh match
56  call feedkeys("/the".repeat("\<C-G>", 6)."\<cr>", 'tx')
57  call assert_equal('  8 them', getline('.'))
58  :1
59  " eighth match
60  call feedkeys("/the".repeat("\<C-G>", 7)."\<cr>", 'tx')
61  call assert_equal('  9 these', getline('.'))
62  :1
63  " no further match
64  call feedkeys("/the".repeat("\<C-G>", 8)."\<cr>", 'tx')
65  call assert_equal('  9 these', getline('.'))
66  call assert_equal([0, 0, 0, 0], getpos('"'))
67
68  " Test 3
69  " Ctrl-G goes from one match to the next
70  " and continues back at the top
71  set incsearch wrapscan
72  :1
73  " first match
74  call feedkeys("/the\<cr>", 'tx')
75  call assert_equal('  2 these', getline('.'))
76  :1
77  " second match
78  call feedkeys("/the\<C-G>\<cr>", 'tx')
79  call assert_equal('  3 the', getline('.'))
80  :1
81  " third match
82  call feedkeys("/the".repeat("\<C-G>", 2)."\<cr>", 'tx')
83  call assert_equal('  4 their', getline('.'))
84  :1
85  " fourth match
86  call feedkeys("/the".repeat("\<C-G>", 3)."\<cr>", 'tx')
87  call assert_equal('  5 there', getline('.'))
88  :1
89  " fifth match
90  call feedkeys("/the".repeat("\<C-G>", 4)."\<cr>", 'tx')
91  call assert_equal('  6 their', getline('.'))
92  :1
93  " sixth match
94  call feedkeys("/the".repeat("\<C-G>", 5)."\<cr>", 'tx')
95  call assert_equal('  7 the', getline('.'))
96  :1
97  " seventh match
98  call feedkeys("/the".repeat("\<C-G>", 6)."\<cr>", 'tx')
99  call assert_equal('  8 them', getline('.'))
100  :1
101  " eighth match
102  call feedkeys("/the".repeat("\<C-G>", 7)."\<cr>", 'tx')
103  call assert_equal('  9 these', getline('.'))
104  :1
105  " back at first match
106  call feedkeys("/the".repeat("\<C-G>", 8)."\<cr>", 'tx')
107  call assert_equal('  2 these', getline('.'))
108
109  " Test 4
110  " CTRL-T goes to the previous match
111  set incsearch nowrapscan
112  $
113  " first match
114  call feedkeys("?the\<cr>", 'tx')
115  call assert_equal('  9 these', getline('.'))
116  $
117  " first match
118  call feedkeys("?the\<C-G>\<cr>", 'tx')
119  call assert_equal('  9 these', getline('.'))
120  $
121  " second match
122  call feedkeys("?the".repeat("\<C-T>", 1)."\<cr>", 'tx')
123  call assert_equal('  8 them', getline('.'))
124  $
125  " last match
126  call feedkeys("?the".repeat("\<C-T>", 7)."\<cr>", 'tx')
127  call assert_equal('  2 these', getline('.'))
128  $
129  " last match
130  call feedkeys("?the".repeat("\<C-T>", 8)."\<cr>", 'tx')
131  call assert_equal('  2 these', getline('.'))
132
133  " Test 5
134  " CTRL-T goes to the previous match
135  set incsearch wrapscan
136  $
137  " first match
138  call feedkeys("?the\<cr>", 'tx')
139  call assert_equal('  9 these', getline('.'))
140  $
141  " first match at the top
142  call feedkeys("?the\<C-G>\<cr>", 'tx')
143  call assert_equal('  2 these', getline('.'))
144  $
145  " second match
146  call feedkeys("?the".repeat("\<C-T>", 1)."\<cr>", 'tx')
147  call assert_equal('  8 them', getline('.'))
148  $
149  " last match
150  call feedkeys("?the".repeat("\<C-T>", 7)."\<cr>", 'tx')
151  call assert_equal('  2 these', getline('.'))
152  $
153  " back at the bottom of the buffer
154  call feedkeys("?the".repeat("\<C-T>", 8)."\<cr>", 'tx')
155  call assert_equal('  9 these', getline('.'))
156
157  " Test 6
158  " CTRL-L adds to the search pattern
159  set incsearch wrapscan
160  1
161  " first match
162  call feedkeys("/the\<c-l>\<cr>", 'tx')
163  call assert_equal('  2 these', getline('.'))
164  1
165  " go to next match of 'thes'
166  call feedkeys("/the\<c-l>\<C-G>\<cr>", 'tx')
167  call assert_equal('  9 these', getline('.'))
168  1
169  " wrap around
170  call feedkeys("/the\<c-l>\<C-G>\<C-G>\<cr>", 'tx')
171  call assert_equal('  2 these', getline('.'))
172  1
173  " wrap around
174  set nowrapscan
175  call feedkeys("/the\<c-l>\<C-G>\<C-G>\<cr>", 'tx')
176  call assert_equal('  9 these', getline('.'))
177
178  " Test 7
179  " <bs> remove from match, but stay at current match
180  set incsearch wrapscan
181  1
182  " first match
183  call feedkeys("/thei\<cr>", 'tx')
184  call assert_equal('  4 their', getline('.'))
185  1
186  " delete one char, add another
187  call feedkeys("/thei\<bs>s\<cr>", 'tx')
188  call assert_equal('  2 these', getline('.'))
189  1
190  " delete one char, add another,  go to previous match, add one char
191  call feedkeys("/thei\<bs>s\<bs>\<C-T>\<c-l>\<cr>", 'tx')
192  call assert_equal('  9 these', getline('.'))
193  1
194  " delete all chars, start from the beginning again
195  call feedkeys("/them". repeat("\<bs>",4).'the\>'."\<cr>", 'tx')
196  call assert_equal('  3 the', getline('.'))
197
198  " clean up
199  call Ntest_override("char_avail", 0)
200  bw!
201endfunc
202
203func Test_search_cmdline2()
204  CheckOption incsearch
205
206  " need to disable char_avail,
207  " so that expansion of commandline works
208  call Ntest_override("char_avail", 1)
209  new
210  call setline(1, ['  1', '  2 these', '  3 the theother'])
211  " Test 1
212  " Ctrl-T goes correctly back and forth
213  set incsearch
214  1
215  " first match
216  call feedkeys("/the\<cr>", 'tx')
217  call assert_equal('  2 these', getline('.'))
218  1
219  " go to next match (on next line)
220  call feedkeys("/the\<C-G>\<cr>", 'tx')
221  call assert_equal('  3 the theother', getline('.'))
222  1
223  " go to next match (still on line 3)
224  call feedkeys("/the\<C-G>\<C-G>\<cr>", 'tx')
225  call assert_equal('  3 the theother', getline('.'))
226  1
227  " go to next match (still on line 3)
228  call feedkeys("/the\<C-G>\<C-G>\<C-G>\<cr>", 'tx')
229  call assert_equal('  3 the theother', getline('.'))
230  1
231  " go to previous match (on line 3)
232  call feedkeys("/the\<C-G>\<C-G>\<C-G>\<C-T>\<cr>", 'tx')
233  call assert_equal('  3 the theother', getline('.'))
234  1
235  " go to previous match (on line 3)
236  call feedkeys("/the\<C-G>\<C-G>\<C-G>\<C-T>\<C-T>\<cr>", 'tx')
237  call assert_equal('  3 the theother', getline('.'))
238  1
239  " go to previous match (on line 2)
240  call feedkeys("/the\<C-G>\<C-G>\<C-G>\<C-T>\<C-T>\<C-T>\<cr>", 'tx')
241  call assert_equal('  2 these', getline('.'))
242  1
243  " go to previous match (on line 2)
244  call feedkeys("/the\<C-G>\<C-R>\<C-W>\<cr>", 'tx')
245  call assert_equal('theother', @/)
246
247  " Test 2: keep the view,
248  " after deleting a character from the search cmd
249  call setline(1, ['  1', '  2 these', '  3 the', '  4 their', '  5 there', '  6 their', '  7 the', '  8 them', '  9 these', ' 10 foobar'])
250  resize 5
251  1
252  call feedkeys("/foo\<bs>\<cr>", 'tx')
253  redraw
254  call assert_equal({'lnum': 10, 'leftcol': 0, 'col': 4, 'topfill': 0, 'topline': 6, 'coladd': 0, 'skipcol': 0, 'curswant': 4}, winsaveview())
255
256  " remove all history entries
257  for i in range(11)
258      call histdel('/')
259  endfor
260
261  " Test 3: reset the view,
262  " after deleting all characters from the search cmd
263  norm! 1gg0
264  " unfortunately, neither "/foo\<c-w>\<cr>", nor "/foo\<bs>\<bs>\<bs>\<cr>",
265  " nor "/foo\<c-u>\<cr>" works to delete the commandline.
266  " In that case Vim should return "E35 no previous regular expression",
267  " but it looks like Vim still sees /foo and therefore the test fails.
268  " Therefore, disabling this test
269  "call assert_fails(feedkeys("/foo\<c-w>\<cr>", 'tx'), 'E35')
270  "call assert_equal({'lnum': 1, 'leftcol': 0, 'col': 0, 'topfill': 0, 'topline': 1, 'coladd': 0, 'skipcol': 0, 'curswant': 0}, winsaveview())
271
272  " clean up
273  set noincsearch
274  call Ntest_override("char_avail", 0)
275  bw!
276endfunc
277
278func Test_use_sub_pat()
279  split
280  let @/ = ''
281  func X()
282    s/^/a/
283    /
284  endfunc
285  call X()
286  bwipe!
287endfunc
288
289func Test_searchpair()
290  new
291  call setline(1, ['other code', 'here [', ' [', ' " cursor here', ' ]]'])
292
293  " should not give an error for using "42"
294  call assert_equal(0, searchpair('a', 'b', 'c', '', 42))
295
296  4
297  call assert_equal(3, searchpair('\[', '', ']', 'bW'))
298  call assert_equal([0, 3, 2, 0], getpos('.'))
299  4
300  call assert_equal(2, searchpair('\[', '', ']', 'bWr'))
301  call assert_equal([0, 2, 6, 0], getpos('.'))
302  4
303  call assert_equal(1, searchpair('\[', '', ']', 'bWm'))
304  call assert_equal([0, 3, 2, 0], getpos('.'))
305  4|norm ^
306  call assert_equal(5, searchpair('\[', '', ']', 'Wn'))
307  call assert_equal([0, 4, 2, 0], getpos('.'))
308  4
309  call assert_equal(2, searchpair('\[', '', ']', 'bW',
310        \                         'getline(".") =~ "^\\s*\["'))
311  call assert_equal([0, 2, 6, 0], getpos('.'))
312  set nomagic
313  4
314  call assert_equal(3, searchpair('\[', '', ']', 'bW'))
315  call assert_equal([0, 3, 2, 0], getpos('.'))
316  set magic
317  4|norm ^
318  call assert_equal(0, searchpair('{', '', '}', 'bW'))
319  call assert_equal([0, 4, 2, 0], getpos('.'))
320
321  %d
322  call setline(1, ['if 1', '  if 2', '  else', '  endif 2', 'endif 1'])
323
324  /\<if 1
325  call assert_equal(5, searchpair('\<if\>', '\<else\>', '\<endif\>', 'W'))
326  call assert_equal([0, 5, 1, 0], getpos('.'))
327  /\<if 2
328  call assert_equal(3, searchpair('\<if\>', '\<else\>', '\<endif\>', 'W'))
329  call assert_equal([0, 3, 3, 0], getpos('.'))
330
331  q!
332endfunc
333
334func Test_searchpairpos()
335  new
336  call setline(1, ['other code', 'here [', ' [', ' " cursor here', ' ]]'])
337
338  4
339  call assert_equal([3, 2], searchpairpos('\[', '', ']', 'bW'))
340  call assert_equal([0, 3, 2, 0], getpos('.'))
341  4
342  call assert_equal([2, 6], searchpairpos('\[', '', ']', 'bWr'))
343  call assert_equal([0, 2, 6, 0], getpos('.'))
344  4|norm ^
345  call assert_equal([5, 2], searchpairpos('\[', '', ']', 'Wn'))
346  call assert_equal([0, 4, 2, 0], getpos('.'))
347  4
348  call assert_equal([2, 6], searchpairpos('\[', '', ']', 'bW',
349        \                                 'getline(".") =~ "^\\s*\["'))
350  call assert_equal([0, 2, 6, 0], getpos('.'))
351  4
352  call assert_equal([2, 6], searchpairpos('\[', '', ']', 'bWr'))
353  call assert_equal([0, 2, 6, 0], getpos('.'))
354  set nomagic
355  4
356  call assert_equal([3, 2], searchpairpos('\[', '', ']', 'bW'))
357  call assert_equal([0, 3, 2, 0], getpos('.'))
358  set magic
359  4|norm ^
360  call assert_equal([0, 0], searchpairpos('{', '', '}', 'bW'))
361  call assert_equal([0, 4, 2, 0], getpos('.'))
362
363  %d
364  call setline(1, ['if 1', '  if 2', '  else', '  endif 2', 'endif 1'])
365  /\<if 1
366  call assert_equal([5, 1], searchpairpos('\<if\>', '\<else\>', '\<endif\>', 'W'))
367  call assert_equal([0, 5, 1, 0], getpos('.'))
368  /\<if 2
369  call assert_equal([3, 3], searchpairpos('\<if\>', '\<else\>', '\<endif\>', 'W'))
370  call assert_equal([0, 3, 3, 0], getpos('.'))
371
372  q!
373endfunc
374
375func Test_searchpair_errors()
376  call assert_fails("call searchpair([0], 'middle', 'end', 'bW', 'skip', 99, 100)", 'E730: Using a List as a String')
377  call assert_fails("call searchpair('start', {-> 0}, 'end', 'bW', 'skip', 99, 100)", 'E729: Using a Funcref as a String')
378  call assert_fails("call searchpair('start', 'middle', {'one': 1}, 'bW', 'skip', 99, 100)", 'E731: Using a Dictionary as a String')
379  call assert_fails("call searchpair('start', 'middle', 'end', 'flags', 'skip', 99, 100)", 'E475: Invalid argument: flags')
380  call assert_fails("call searchpair('start', 'middle', 'end', 'bW', 'func', -99, 100)", 'E475: Invalid argument: -99')
381  call assert_fails("call searchpair('start', 'middle', 'end', 'bW', 'func', 99, -100)", 'E475: Invalid argument: -100')
382  call assert_fails("call searchpair('start', 'middle', 'end', 'e')", 'E475: Invalid argument: e')
383  call assert_fails("call searchpair('start', 'middle', 'end', 'sn')", 'E475: Invalid argument: sn')
384endfunc
385
386func Test_searchpairpos_errors()
387  call assert_fails("call searchpairpos([0], 'middle', 'end', 'bW', 'skip', 99, 100)", 'E730: Using a List as a String')
388  call assert_fails("call searchpairpos('start', {-> 0}, 'end', 'bW', 'skip', 99, 100)", 'E729: Using a Funcref as a String')
389  call assert_fails("call searchpairpos('start', 'middle', {'one': 1}, 'bW', 'skip', 99, 100)", 'E731: Using a Dictionary as a String')
390  call assert_fails("call searchpairpos('start', 'middle', 'end', 'flags', 'skip', 99, 100)", 'E475: Invalid argument: flags')
391  call assert_fails("call searchpairpos('start', 'middle', 'end', 'bW', 'func', -99, 100)", 'E475: Invalid argument: -99')
392  call assert_fails("call searchpairpos('start', 'middle', 'end', 'bW', 'func', 99, -100)", 'E475: Invalid argument: -100')
393  call assert_fails("call searchpairpos('start', 'middle', 'end', 'e')", 'E475: Invalid argument: e')
394  call assert_fails("call searchpairpos('start', 'middle', 'end', 'sn')", 'E475: Invalid argument: sn')
395endfunc
396
397func Test_searchpair_skip()
398    func Zero()
399      return 0
400    endfunc
401    func Partial(x)
402      return a:x
403    endfunc
404    new
405    call setline(1, ['{', 'foo', 'foo', 'foo', '}'])
406    3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', ''))
407    3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', '0'))
408    3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', {-> 0}))
409    3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', function('Zero')))
410    3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', function('Partial', [0])))
411    bw!
412endfunc
413
414func Test_searchpair_leak()
415  new
416  call setline(1, 'if one else another endif')
417
418  " The error in the skip expression caused memory to leak.
419  call assert_fails("call searchpair('\\<if\\>', '\\<else\\>', '\\<endif\\>', '', '\"foo\" 2')", 'E15:')
420
421  bwipe!
422endfunc
423
424func Test_searchc()
425  " These commands used to cause memory overflow in searchc().
426  new
427  norm ixx
428  exe "norm 0t\u93cf"
429  bw!
430endfunc
431
432func Cmdline3_prep()
433  " need to disable char_avail,
434  " so that expansion of commandline works
435  call Ntest_override("char_avail", 1)
436  new
437  call setline(1, ['  1', '  2 the~e', '  3 the theother'])
438  set incsearch
439endfunc
440
441func Incsearch_cleanup()
442  set noincsearch
443  call Ntest_override("char_avail", 0)
444  bw!
445endfunc
446
447func Test_search_cmdline3()
448  CheckOption incsearch
449
450  call Cmdline3_prep()
451  1
452  " first match
453  call feedkeys("/the\<c-l>\<cr>", 'tx')
454  call assert_equal('  2 the~e', getline('.'))
455
456  call Incsearch_cleanup()
457endfunc
458
459func Test_search_cmdline3s()
460  CheckOption incsearch
461
462  call Cmdline3_prep()
463  1
464  call feedkeys(":%s/the\<c-l>/xxx\<cr>", 'tx')
465  call assert_equal('  2 xxxe', getline('.'))
466  undo
467  call feedkeys(":%subs/the\<c-l>/xxx\<cr>", 'tx')
468  call assert_equal('  2 xxxe', getline('.'))
469  undo
470  call feedkeys(":%substitute/the\<c-l>/xxx\<cr>", 'tx')
471  call assert_equal('  2 xxxe', getline('.'))
472  undo
473  call feedkeys(":%smagic/the.e/xxx\<cr>", 'tx')
474  call assert_equal('  2 xxx', getline('.'))
475  undo
476  call assert_fails(":%snomagic/the.e/xxx\<cr>", 'E486')
477  "
478  call feedkeys(":%snomagic/the\\.e/xxx\<cr>", 'tx')
479  call assert_equal('  2 xxx', getline('.'))
480
481  call Incsearch_cleanup()
482endfunc
483
484func Test_search_cmdline3g()
485  CheckOption incsearch
486
487  call Cmdline3_prep()
488  1
489  call feedkeys(":g/the\<c-l>/d\<cr>", 'tx')
490  call assert_equal('  3 the theother', getline(2))
491  undo
492  call feedkeys(":global/the\<c-l>/d\<cr>", 'tx')
493  call assert_equal('  3 the theother', getline(2))
494  undo
495  call feedkeys(":g!/the\<c-l>/d\<cr>", 'tx')
496  call assert_equal(1, line('$'))
497  call assert_equal('  2 the~e', getline(1))
498  undo
499  call feedkeys(":global!/the\<c-l>/d\<cr>", 'tx')
500  call assert_equal(1, line('$'))
501  call assert_equal('  2 the~e', getline(1))
502
503  call Incsearch_cleanup()
504endfunc
505
506func Test_search_cmdline3v()
507  CheckOption incsearch
508
509  call Cmdline3_prep()
510  1
511  call feedkeys(":v/the\<c-l>/d\<cr>", 'tx')
512  call assert_equal(1, line('$'))
513  call assert_equal('  2 the~e', getline(1))
514  undo
515  call feedkeys(":vglobal/the\<c-l>/d\<cr>", 'tx')
516  call assert_equal(1, line('$'))
517  call assert_equal('  2 the~e', getline(1))
518
519  call Incsearch_cleanup()
520endfunc
521
522func Test_search_cmdline4()
523  CheckOption incsearch
524
525  " need to disable char_avail,
526  " so that expansion of commandline works
527  call Ntest_override("char_avail", 1)
528  new
529  call setline(1, ['  1 the first', '  2 the second', '  3 the third'])
530  set incsearch
531  $
532  call feedkeys("?the\<c-g>\<cr>", 'tx')
533  call assert_equal('  3 the third', getline('.'))
534  $
535  call feedkeys("?the\<c-g>\<c-g>\<cr>", 'tx')
536  call assert_equal('  1 the first', getline('.'))
537  $
538  call feedkeys("?the\<c-g>\<c-g>\<c-g>\<cr>", 'tx')
539  call assert_equal('  2 the second', getline('.'))
540  $
541  call feedkeys("?the\<c-t>\<cr>", 'tx')
542  call assert_equal('  1 the first', getline('.'))
543  $
544  call feedkeys("?the\<c-t>\<c-t>\<cr>", 'tx')
545  call assert_equal('  3 the third', getline('.'))
546  $
547  call feedkeys("?the\<c-t>\<c-t>\<c-t>\<cr>", 'tx')
548  call assert_equal('  2 the second', getline('.'))
549  " clean up
550  set noincsearch
551  call Ntest_override("char_avail", 0)
552  bw!
553endfunc
554
555func Test_search_cmdline5()
556  CheckOption incsearch
557
558  " Do not call test_override("char_avail", 1) so that <C-g> and <C-t> work
559  " regardless char_avail.
560  new
561  call setline(1, ['  1 the first', '  2 the second', '  3 the third', ''])
562  set incsearch
563  1
564  call feedkeys("/the\<c-g>\<c-g>\<cr>", 'tx')
565  call assert_equal('  3 the third', getline('.'))
566  $
567  call feedkeys("?the\<c-t>\<c-t>\<c-t>\<cr>", 'tx')
568  call assert_equal('  1 the first', getline('.'))
569  " clean up
570  set noincsearch
571  bw!
572endfunc
573
574func Test_search_cmdline6()
575  " Test that consecutive matches
576  " are caught by <c-g>/<c-t>
577  CheckOption incsearch
578
579  " need to disable char_avail,
580  " so that expansion of commandline works
581  call Ntest_override("char_avail", 1)
582  new
583  call setline(1, [' bbvimb', ''])
584  set incsearch
585  " first match
586  norm! gg0
587  call feedkeys("/b\<cr>", 'tx')
588  call assert_equal([0,1,2,0], getpos('.'))
589  " second match
590  norm! gg0
591  call feedkeys("/b\<c-g>\<cr>", 'tx')
592  call assert_equal([0,1,3,0], getpos('.'))
593  " third match
594  norm! gg0
595  call feedkeys("/b\<c-g>\<c-g>\<cr>", 'tx')
596  call assert_equal([0,1,7,0], getpos('.'))
597  " first match again
598  norm! gg0
599  call feedkeys("/b\<c-g>\<c-g>\<c-g>\<cr>", 'tx')
600  call assert_equal([0,1,2,0], getpos('.'))
601  set nowrapscan
602  " last match
603  norm! gg0
604  call feedkeys("/b\<c-g>\<c-g>\<c-g>\<cr>", 'tx')
605  call assert_equal([0,1,7,0], getpos('.'))
606  " clean up
607  set wrapscan&vim
608  set noincsearch
609  call Ntest_override("char_avail", 0)
610  bw!
611endfunc
612
613func Test_search_cmdline7()
614  " Test that pressing <c-g> in an empty command line
615  " does not move the cursor
616  if !exists('+incsearch')
617    return
618  endif
619  " need to disable char_avail,
620  " so that expansion of commandline works
621  call Ntest_override("char_avail", 1)
622  new
623  let @/ = 'b'
624  call setline(1, [' bbvimb', ''])
625  set incsearch
626  " first match
627  norm! gg0
628  " moves to next match of previous search pattern, just like /<cr>
629  call feedkeys("/\<c-g>\<cr>", 'tx')
630  call assert_equal([0,1,2,0], getpos('.'))
631  " moves to next match of previous search pattern, just like /<cr>
632  call feedkeys("/\<cr>", 'tx')
633  call assert_equal([0,1,3,0], getpos('.'))
634  " moves to next match of previous search pattern, just like /<cr>
635  call feedkeys("/\<c-t>\<cr>", 'tx')
636  call assert_equal([0,1,7,0], getpos('.'))
637
638  " using an offset uses the last search pattern
639  call cursor(1, 1)
640  call setline(1, ['1 bbvimb', ' 2 bbvimb'])
641  let @/ = 'b'
642  call feedkeys("//e\<c-g>\<cr>", 'tx')
643  call assert_equal('1 bbvimb', getline('.'))
644  call assert_equal(4, col('.'))
645  call cursor(1, 1)
646  call feedkeys("//+1\<c-g>\<cr>", 'tx')
647  call assert_equal(' 2 bbvimb', getline('.'))
648  call assert_equal([0, 2, 1, 0], getpos('.'))
649  call setline(1, ['blah blah blah'])
650  call feedkeys("gg0/blah/e\<c-g>\<cr>", 'tx')
651  call assert_equal([0, 1, 9, 0], getpos('.'))
652  call feedkeys("gg0/blah/e\<c-g>\<c-g>\<cr>", 'tx')
653  call assert_equal([0, 1, 14, 0], getpos('.'))
654  call feedkeys("gg0/blah/e\<c-g>\<c-g>\<c-t>\<cr>", 'tx')
655  call assert_equal([0, 1, 9, 0], getpos('.'))
656  call cursor(1, col('$'))
657  call feedkeys("?blah?e\<c-g>\<cr>", 'tx')
658  call assert_equal([0, 1, 9, 0], getpos('.'))
659  call feedkeys("gg0/blah/e+1\<c-g>\<cr>", 'tx')
660  call assert_equal([0, 1, 10, 0], getpos('.'))
661  call feedkeys("gg0/blah/e-2\<c-g>\<cr>", 'tx')
662  call assert_equal([0, 1, 7, 0], getpos('.'))
663  call setline(1, ['a/b a/b a/b'])
664  call feedkeys("gg0/a\\/b/e\<c-g>\<cr>", 'tx')
665  call assert_equal([0, 1, 7, 0], getpos('.'))
666
667  set noincsearch
668  call Ntest_override("char_avail", 0)
669  bw!
670endfunc
671
672func Test_search_cmdline8()
673  " Highlighting is cleared in all windows
674  " since hls applies to all windows
675  CheckOption incsearch
676  CheckFeature terminal
677  CheckNotGui
678  if has("win32")
679    throw "Skipped: Bug with sending <ESC> to terminal window not fixed yet"
680  endif
681
682  let h = winheight(0)
683  if h < 3
684    return
685  endif
686  " Prepare buffer text
687  let lines = ['abb vim vim vi', 'vimvivim']
688  call writefile(lines, 'Xsearch.txt', 'D')
689  let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', 'Xsearch.txt'], {'term_rows': 3})
690
691  call WaitForAssert({-> assert_equal(lines, [term_getline(buf, 1), term_getline(buf, 2)])})
692
693  call term_sendkeys(buf, ":set incsearch hlsearch\<cr>")
694  call term_sendkeys(buf, ":14vsp\<cr>")
695  call term_sendkeys(buf, "/vim\<cr>")
696  call term_sendkeys(buf, "/b\<esc>")
697  call term_sendkeys(buf, "gg0")
698  call TermWait(buf, 250)
699  let screen_line = term_scrape(buf, 1)
700  let [a0,a1,a2,a3] = [screen_line[3].attr, screen_line[4].attr,
701        \ screen_line[18].attr, screen_line[19].attr]
702  call assert_notequal(a0, a1)
703  call assert_notequal(a0, a3)
704  call assert_notequal(a1, a2)
705  call assert_equal(a0, a2)
706  call assert_equal(a1, a3)
707
708  " clean up
709  bwipe!
710endfunc
711
712" Tests for regexp with various magic settings
713func Run_search_regexp_magic_opt()
714  put ='1 a aa abb abbccc'
715  exe 'normal! /a*b\{2}c\+/e' . "\<CR>"
716  call assert_equal([0, 2, 17, 0], getpos('.'))
717
718  put ='2 d dd dee deefff'
719  exe 'normal! /\Md\*e\{2}f\+/e' . "\<CR>"
720  call assert_equal([0, 3, 17, 0], getpos('.'))
721
722  set nomagic
723  put ='3 g gg ghh ghhiii'
724  exe 'normal! /g\*h\{2}i\+/e' . "\<CR>"
725  call assert_equal([0, 4, 17, 0], getpos('.'))
726
727  put ='4 j jj jkk jkklll'
728  exe 'normal! /\mj*k\{2}l\+/e' . "\<CR>"
729  call assert_equal([0, 5, 17, 0], getpos('.'))
730
731  put ='5 m mm mnn mnnooo'
732  exe 'normal! /\vm*n{2}o+/e' . "\<CR>"
733  call assert_equal([0, 6, 17, 0], getpos('.'))
734
735  put ='6 x ^aa$ x'
736  exe 'normal! /\V^aa$' . "\<CR>"
737  call assert_equal([0, 7, 5, 0], getpos('.'))
738
739  set magic
740  put ='7 (a)(b) abbaa'
741  exe 'normal! /\v(a)(b)\2\1\1/e' . "\<CR>"
742  call assert_equal([0, 8, 14, 0], getpos('.'))
743
744  put ='8 axx [ab]xx'
745  exe 'normal! /\V[ab]\(\[xy]\)\1' . "\<CR>"
746  call assert_equal([0, 9, 7, 0], getpos('.'))
747
748  %d
749endfunc
750
751func Test_search_regexp()
752  enew!
753
754  set regexpengine=1
755  call Run_search_regexp_magic_opt()
756  set regexpengine=2
757  call Run_search_regexp_magic_opt()
758  set regexpengine&
759
760  set undolevels=100
761  put ='9 foobar'
762  put =''
763  exe "normal! a\<C-G>u\<Esc>"
764  normal G
765  exe 'normal! dv?bar?' . "\<CR>"
766  call assert_equal('9 foo', getline('.'))
767  call assert_equal([0, 2, 5, 0], getpos('.'))
768  call assert_equal(2, line('$'))
769  normal u
770  " Nvim: undo restores the cursor to where "dv?bar?" started.
771  call assert_equal('9 foobar', getline(2))
772  call assert_equal([0, 3, 1, 0], getpos('.'))
773  call assert_equal(3, line('$'))
774
775  set undolevels&
776  enew!
777endfunc
778
779func Test_search_cmdline_incsearch_highlight()
780  CheckOption incsearch
781
782  set incsearch hlsearch
783  " need to disable char_avail,
784  " so that expansion of commandline works
785  call Ntest_override("char_avail", 1)
786  new
787  call setline(1, ['aaa  1 the first', '  2 the second', '  3 the third'])
788
789  1
790  call feedkeys("/second\<cr>", 'tx')
791  call assert_equal('second', @/)
792  call assert_equal('  2 the second', getline('.'))
793
794  " Canceling search won't change @/
795  1
796  let @/ = 'last pattern'
797  call feedkeys("/third\<C-c>", 'tx')
798  call assert_equal('last pattern', @/)
799  call feedkeys("/third\<Esc>", 'tx')
800  call assert_equal('last pattern', @/)
801  call feedkeys("/3\<bs>\<bs>", 'tx')
802  call assert_equal('last pattern', @/)
803  call feedkeys("/third\<c-g>\<c-t>\<Esc>", 'tx')
804  call assert_equal('last pattern', @/)
805
806  " clean up
807  set noincsearch nohlsearch
808  call Ntest_override("char_avail", 0)
809  bw!
810endfunc
811
812func Test_search_cmdline_incsearch_highlight_attr()
813  CheckOption incsearch
814  CheckFeature terminal
815  CheckNotGui
816
817  let h = winheight(0)
818  if h < 3
819    return
820  endif
821
822  " Prepare buffer text
823  let lines = ['abb vim vim vi', 'vimvivim']
824  call writefile(lines, 'Xsearch.txt', 'D')
825  let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', 'Xsearch.txt'], {'term_rows': 3})
826
827  call WaitForAssert({-> assert_equal(lines, [term_getline(buf, 1), term_getline(buf, 2)])})
828  " wait for vim to complete initialization
829  call TermWait(buf)
830
831  " Get attr of normal(a0), incsearch(a1), hlsearch(a2) highlight
832  call term_sendkeys(buf, ":set incsearch hlsearch\<cr>")
833  call term_sendkeys(buf, '/b')
834  call TermWait(buf, 100)
835  let screen_line1 = term_scrape(buf, 1)
836  call assert_true(len(screen_line1) > 2)
837  " a0: attr_normal
838  let a0 = screen_line1[0].attr
839  " a1: attr_incsearch
840  let a1 = screen_line1[1].attr
841  " a2: attr_hlsearch
842  let a2 = screen_line1[2].attr
843  call assert_notequal(a0, a1)
844  call assert_notequal(a0, a2)
845  call assert_notequal(a1, a2)
846  call term_sendkeys(buf, "\<cr>gg0")
847
848  " Test incremental highlight search
849  call term_sendkeys(buf, "/vim")
850  call TermWait(buf, 100)
851  " Buffer:
852  " abb vim vim vi
853  " vimvivim
854  " Search: /vim
855  let attr_line1 = [a0,a0,a0,a0,a1,a1,a1,a0,a2,a2,a2,a0,a0,a0]
856  let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2]
857  call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
858  call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
859
860  " Test <C-g>
861  call term_sendkeys(buf, "\<C-g>\<C-g>")
862  call TermWait(buf, 100)
863  let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a2,a2,a2,a0,a0,a0]
864  let attr_line2 = [a1,a1,a1,a0,a0,a2,a2,a2]
865  call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
866  call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
867
868  " Test <C-t>
869  call term_sendkeys(buf, "\<C-t>")
870  call TermWait(buf, 100)
871  let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a1,a1,a1,a0,a0,a0]
872  let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2]
873  call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
874  call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
875
876  " Type Enter and a1(incsearch highlight) should become a2(hlsearch highlight)
877  call term_sendkeys(buf, "\<cr>")
878  call TermWait(buf, 100)
879  let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a2,a2,a2,a0,a0,a0]
880  let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2]
881  call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
882  call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
883
884  " Test nohlsearch. a2(hlsearch highlight) should become a0(normal highlight)
885  call term_sendkeys(buf, ":1\<cr>")
886  call term_sendkeys(buf, ":set nohlsearch\<cr>")
887  call term_sendkeys(buf, "/vim")
888  call TermWait(buf, 100)
889  let attr_line1 = [a0,a0,a0,a0,a1,a1,a1,a0,a0,a0,a0,a0,a0,a0]
890  let attr_line2 = [a0,a0,a0,a0,a0,a0,a0,a0]
891  call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
892  call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
893
894  bwipe!
895endfunc
896
897func Test_incsearch_cmdline_modifier()
898  CheckOption incsearch
899
900  call Ntest_override("char_avail", 1)
901  new
902  call setline(1, ['foo'])
903  set incsearch
904  " Test that error E14 does not occur in parsing command modifier.
905  call feedkeys("V:tab", 'tx')
906
907  call Incsearch_cleanup()
908endfunc
909
910func Test_incsearch_scrolling()
911  CheckScreendump
912  CheckRunVimInTerminal
913  call assert_equal(0, &scrolloff)
914  call writefile([
915	\ 'let dots = repeat(".", 120)',
916	\ 'set incsearch cmdheight=2 scrolloff=0',
917	\ 'call setline(1, [dots, dots, dots, "", "target", dots, dots])',
918	\ 'normal gg',
919	\ 'redraw',
920	\ ], 'Xscript', 'D')
921  let buf = RunVimInTerminal('-S Xscript', {'rows': 9, 'cols': 70})
922  " Need to send one key at a time to force a redraw
923  call term_sendkeys(buf, '/')
924  sleep 100m
925  call term_sendkeys(buf, 't')
926  sleep 100m
927  call term_sendkeys(buf, 'a')
928  sleep 100m
929  call term_sendkeys(buf, 'r')
930  sleep 100m
931  call term_sendkeys(buf, 'g')
932  call VerifyScreenDump(buf, 'Test_incsearch_scrolling_01', {})
933
934  call term_sendkeys(buf, "\<Esc>")
935  call StopVimInTerminal(buf)
936endfunc
937
938func Test_incsearch_search_dump()
939  CheckOption incsearch
940  CheckScreendump
941
942  call writefile([
943	\ 'set incsearch hlsearch scrolloff=0',
944	\ 'for n in range(1, 8)',
945	\ '  call setline(n, "foo " . n)',
946	\ 'endfor',
947	\ '3',
948	\ ], 'Xis_search_script', 'D')
949  let buf = RunVimInTerminal('-S Xis_search_script', {'rows': 9, 'cols': 70})
950  " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
951  " the 'ambiwidth' check.
952  sleep 100m
953
954  " Need to send one key at a time to force a redraw.
955  call term_sendkeys(buf, '/fo')
956  call VerifyScreenDump(buf, 'Test_incsearch_search_01', {})
957  call term_sendkeys(buf, "\<Esc>")
958  sleep 100m
959
960  call term_sendkeys(buf, '/\v')
961  call VerifyScreenDump(buf, 'Test_incsearch_search_02', {})
962  call term_sendkeys(buf, "\<Esc>")
963
964  call StopVimInTerminal(buf)
965endfunc
966
967func Test_hlsearch_dump()
968  CheckOption hlsearch
969  CheckScreendump
970
971  call writefile([
972	\ 'set hlsearch cursorline',
973        \ 'call setline(1, ["xxx", "xxx", "xxx"])',
974	\ '/.*',
975	\ '2',
976	\ ], 'Xhlsearch_script', 'D')
977  let buf = RunVimInTerminal('-S Xhlsearch_script', {'rows': 6, 'cols': 50})
978  call VerifyScreenDump(buf, 'Test_hlsearch_1', {})
979
980  call term_sendkeys(buf, "/\\_.*\<CR>")
981  call VerifyScreenDump(buf, 'Test_hlsearch_2', {})
982
983  call StopVimInTerminal(buf)
984endfunc
985
986func Test_hlsearch_and_visual()
987  CheckOption hlsearch
988  CheckScreendump
989
990  call writefile([
991	\ 'set hlsearch',
992        \ 'call setline(1, repeat(["xxx yyy zzz"], 3))',
993        \ 'hi Search cterm=bold',
994	\ '/yyy',
995	\ 'call cursor(1, 6)',
996	\ ], 'Xhlvisual_script', 'D')
997  let buf = RunVimInTerminal('-S Xhlvisual_script', {'rows': 6, 'cols': 40})
998  call term_sendkeys(buf, "vjj")
999  call WaitForAssert({-> assert_match('VISUAL.*-\d', term_getline(buf, 6))}, 1000)
1000  call VerifyScreenDump(buf, 'Test_hlsearch_visual_1', {})
1001  call term_sendkeys(buf, "\<Esc>")
1002
1003  call StopVimInTerminal(buf)
1004endfunc
1005
1006func Test_hlsearch_block_visual_match()
1007  CheckScreendump
1008
1009  let lines =<< trim END
1010    set hlsearch
1011    call setline(1, ['aa', 'bbbb', 'cccccc'])
1012  END
1013  call writefile(lines, 'Xhlsearch_block', 'D')
1014  let buf = RunVimInTerminal('-S Xhlsearch_block', {'rows': 9, 'cols': 60})
1015
1016  call term_sendkeys(buf, "G\<C-V>$kk\<Esc>")
1017  sleep 100m
1018  call term_sendkeys(buf, "/\\%V\<CR>")
1019  sleep 100m
1020  call VerifyScreenDump(buf, 'Test_hlsearch_block_visual_match', {})
1021
1022  call StopVimInTerminal(buf)
1023endfunc
1024
1025func Test_incsearch_substitute()
1026  CheckOption incsearch
1027
1028  call Ntest_override("char_avail", 1)
1029  new
1030  set incsearch
1031  for n in range(1, 10)
1032    call setline(n, 'foo ' . n)
1033  endfor
1034  4
1035  call feedkeys(":.,.+2s/foo\<BS>o\<BS>o/xxx\<cr>", 'tx')
1036  call assert_equal('foo 3', getline(3))
1037  call assert_equal('xxx 4', getline(4))
1038  call assert_equal('xxx 5', getline(5))
1039  call assert_equal('xxx 6', getline(6))
1040  call assert_equal('foo 7', getline(7))
1041
1042  call Incsearch_cleanup()
1043endfunc
1044
1045func Test_incsearch_substitute_long_line()
1046  new
1047  call Ntest_override("char_avail", 1)
1048  set incsearch
1049
1050  call repeat('x', 100000)->setline(1)
1051  call feedkeys(':s/\%c', 'xt')
1052  redraw
1053  call feedkeys("\<Esc>", 'xt')
1054
1055  call Incsearch_cleanup()
1056  bwipe!
1057endfunc
1058
1059func Test_hlsearch_cursearch()
1060  CheckScreendump
1061
1062  let lines =<< trim END
1063    set hlsearch scrolloff=0
1064    call setline(1, ['one', 'foo', 'bar', 'baz', 'foo the foo and foo', 'bar'])
1065    hi Search ctermbg=yellow
1066    hi CurSearch ctermbg=blue
1067  END
1068  call writefile(lines, 'Xhlsearch_cursearch', 'D')
1069  let buf = RunVimInTerminal('-S Xhlsearch_cursearch', {'rows': 9, 'cols': 60})
1070
1071  call term_sendkeys(buf, "gg/foo\<CR>")
1072  call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_single_line_1', {})
1073
1074  call term_sendkeys(buf, "n")
1075  call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_single_line_2', {})
1076
1077  call term_sendkeys(buf, "n")
1078  call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_single_line_2a', {})
1079
1080  call term_sendkeys(buf, "n")
1081  call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_single_line_2b', {})
1082
1083  call term_sendkeys(buf, ":call setline(5, 'foo')\<CR>")
1084  call term_sendkeys(buf, "0?\<CR>")
1085  call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_single_line_3', {})
1086
1087  call term_sendkeys(buf, "gg/foo\\nbar\<CR>")
1088  call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_multiple_line_1', {})
1089
1090  call term_sendkeys(buf, ":call setline(1, ['---', 'abcdefg', 'hijkl', '---', 'abcdefg', 'hijkl'])\<CR>")
1091  call term_sendkeys(buf, "gg/efg\\nhij\<CR>")
1092  call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_multiple_line_2', {})
1093  call term_sendkeys(buf, "h\<C-L>")
1094  call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_multiple_line_3', {})
1095  call term_sendkeys(buf, "j\<C-L>")
1096  call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_multiple_line_4', {})
1097  call term_sendkeys(buf, "h\<C-L>")
1098  call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_multiple_line_5', {})
1099
1100  " check clearing CurSearch when using it for another match
1101  call term_sendkeys(buf, "G?^abcd\<CR>Y")
1102  call term_sendkeys(buf, "kkP")
1103  call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_changed_1', {})
1104
1105  call StopVimInTerminal(buf)
1106endfunc
1107
1108" Similar to Test_incsearch_substitute() but with a screendump halfway.
1109func Test_incsearch_substitute_dump()
1110  CheckOption incsearch
1111  CheckScreendump
1112
1113  call writefile([
1114	\ 'set incsearch hlsearch scrolloff=0',
1115	\ 'for n in range(1, 10)',
1116	\ '  call setline(n, "foo " . n)',
1117	\ 'endfor',
1118	\ 'call setline(11, "bar 11")',
1119	\ '3',
1120	\ ], 'Xis_subst_script', 'D')
1121  let buf = RunVimInTerminal('-S Xis_subst_script', {'rows': 9, 'cols': 70})
1122  " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1123  " the 'ambiwidth' check.
1124  sleep 100m
1125
1126  " Need to send one key at a time to force a redraw.
1127  " Select three lines at the cursor with typed pattern.
1128  call term_sendkeys(buf, ':.,.+2s/')
1129  sleep 100m
1130  call term_sendkeys(buf, 'f')
1131  sleep 100m
1132  call term_sendkeys(buf, 'o')
1133  sleep 100m
1134  call term_sendkeys(buf, 'o')
1135  call VerifyScreenDump(buf, 'Test_incsearch_substitute_01', {})
1136  call term_sendkeys(buf, "\<Esc>")
1137
1138  " Select three lines at the cursor using previous pattern.
1139  call term_sendkeys(buf, "/foo\<CR>")
1140  sleep 100m
1141  call term_sendkeys(buf, ':.,.+2s//')
1142  call VerifyScreenDump(buf, 'Test_incsearch_substitute_02', {})
1143
1144  " Deleting last slash should remove the match.
1145  call term_sendkeys(buf, "\<BS>")
1146  call VerifyScreenDump(buf, 'Test_incsearch_substitute_03', {})
1147  call term_sendkeys(buf, "\<Esc>")
1148
1149  " Reverse range is accepted
1150  call term_sendkeys(buf, ':5,2s/foo')
1151  call VerifyScreenDump(buf, 'Test_incsearch_substitute_04', {})
1152  call term_sendkeys(buf, "\<Esc>")
1153
1154  " White space after the command is skipped
1155  call term_sendkeys(buf, ':2,3sub  /fo')
1156  call VerifyScreenDump(buf, 'Test_incsearch_substitute_05', {})
1157  call term_sendkeys(buf, "\<Esc>")
1158
1159  " Command modifiers are skipped
1160  call term_sendkeys(buf, ':above below browse botr confirm keepmar keepalt keeppat keepjum filter xxx hide lockm leftabove noau noswap rightbel sandbox silent silent! $tab top unsil vert verbose 4,5s/fo.')
1161  call VerifyScreenDump(buf, 'Test_incsearch_substitute_06', {})
1162  call term_sendkeys(buf, "\<Esc>")
1163
1164  " Cursorline highlighting at match
1165  call term_sendkeys(buf, ":set cursorline\<CR>")
1166  call term_sendkeys(buf, 'G9G')
1167  call term_sendkeys(buf, ':9,11s/bar')
1168  call VerifyScreenDump(buf, 'Test_incsearch_substitute_07', {})
1169  call term_sendkeys(buf, "\<Esc>")
1170
1171  " Cursorline highlighting at cursor when no match
1172  call term_sendkeys(buf, ':9,10s/bar')
1173  call VerifyScreenDump(buf, 'Test_incsearch_substitute_08', {})
1174  call term_sendkeys(buf, "\<Esc>")
1175
1176  " Only \v handled as empty pattern, does not move cursor
1177  call term_sendkeys(buf, '3G4G')
1178  call term_sendkeys(buf, ":nohlsearch\<CR>")
1179  call term_sendkeys(buf, ':6,7s/\v')
1180  call VerifyScreenDump(buf, 'Test_incsearch_substitute_09', {})
1181  call term_sendkeys(buf, "\<Esc>")
1182
1183  call term_sendkeys(buf, ":set nocursorline\<CR>")
1184
1185  " All matches are highlighted for 'hlsearch' after the incsearch canceled
1186  call term_sendkeys(buf, "1G*")
1187  call term_sendkeys(buf, ":2,5s/foo")
1188  sleep 100m
1189  call term_sendkeys(buf, "\<Esc>")
1190  call VerifyScreenDump(buf, 'Test_incsearch_substitute_10', {})
1191
1192  call term_sendkeys(buf, ":split\<CR>")
1193  call term_sendkeys(buf, ":let @/ = 'xyz'\<CR>")
1194  call term_sendkeys(buf, ":%s/.")
1195  call VerifyScreenDump(buf, 'Test_incsearch_substitute_11', {})
1196  call term_sendkeys(buf, "\<BS>")
1197  call VerifyScreenDump(buf, 'Test_incsearch_substitute_12', {})
1198  call term_sendkeys(buf, "\<Esc>")
1199  call VerifyScreenDump(buf, 'Test_incsearch_substitute_13', {})
1200  call term_sendkeys(buf, ":%bwipe!\<CR>")
1201  call term_sendkeys(buf, ":only!\<CR>")
1202
1203  "  get :'<,'>s command in history
1204  call term_sendkeys(buf, ":set cmdheight=2\<CR>")
1205  call term_sendkeys(buf, "aasdfasdf\<Esc>")
1206  call term_sendkeys(buf, "V:s/a/b/g\<CR>")
1207  " Using '<,'> does not give E20
1208  call term_sendkeys(buf, ":new\<CR>")
1209  call term_sendkeys(buf, "aasdfasdf\<Esc>")
1210  call term_sendkeys(buf, ":\<Up>\<Up>")
1211  call VerifyScreenDump(buf, 'Test_incsearch_substitute_14', {})
1212  call term_sendkeys(buf, "<Esc>")
1213
1214  call StopVimInTerminal(buf)
1215endfunc
1216
1217func Test_incsearch_highlighting()
1218  CheckOption incsearch
1219  CheckScreendump
1220
1221  call writefile([
1222	\ 'set incsearch hlsearch',
1223	\ 'call setline(1, "hello/there")',
1224	\ ], 'Xis_subst_hl_script', 'D')
1225  let buf = RunVimInTerminal('-S Xis_subst_hl_script', {'rows': 4, 'cols': 20})
1226  " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1227  " the 'ambiwidth' check.
1228  sleep 300m
1229
1230  " Using a different search delimiter should still highlight matches
1231  " that contain a '/'.
1232  call term_sendkeys(buf, ":%s;ello/the")
1233  call VerifyScreenDump(buf, 'Test_incsearch_substitute_15', {})
1234  call term_sendkeys(buf, "<Esc>")
1235
1236  call StopVimInTerminal(buf)
1237endfunc
1238
1239func Test_incsearch_with_change()
1240  CheckFeature timers
1241  CheckOption incsearch
1242  CheckScreendump
1243 
1244  call writefile([
1245	\ 'set incsearch hlsearch scrolloff=0',
1246	\ 'call setline(1, ["one", "two ------ X", "three"])',
1247	\ 'call timer_start(200, { _ -> setline(2, "x")})',
1248	\ ], 'Xis_change_script', 'D')
1249  let buf = RunVimInTerminal('-S Xis_change_script', {'rows': 9, 'cols': 70})
1250  " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1251  " the 'ambiwidth' check.
1252  sleep 300m
1253
1254  " Highlight X, it will be deleted by the timer callback.
1255  call term_sendkeys(buf, ':%s/X')
1256  call VerifyScreenDump(buf, 'Test_incsearch_change_01', {})
1257  call term_sendkeys(buf, "\<Esc>")
1258
1259  call StopVimInTerminal(buf)
1260endfunc
1261
1262" Similar to Test_incsearch_substitute_dump() for :sort
1263func Test_incsearch_sort_dump()
1264  CheckOption incsearch
1265  CheckScreendump
1266
1267  call writefile([
1268	\ 'set incsearch hlsearch scrolloff=0',
1269	\ 'call setline(1, ["another one 2", "that one 3", "the one 1"])',
1270	\ ], 'Xis_sort_script', 'D')
1271  let buf = RunVimInTerminal('-S Xis_sort_script', {'rows': 9, 'cols': 70})
1272  " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1273  " the 'ambiwidth' check.
1274  sleep 100m
1275
1276  call term_sendkeys(buf, ':sort ni u /on')
1277  call VerifyScreenDump(buf, 'Test_incsearch_sort_01', {})
1278  call term_sendkeys(buf, "\<Esc>")
1279
1280  call term_sendkeys(buf, ':sort! /on')
1281  call VerifyScreenDump(buf, 'Test_incsearch_sort_02', {})
1282  call term_sendkeys(buf, "\<Esc>")
1283
1284  call StopVimInTerminal(buf)
1285endfunc
1286
1287" Similar to Test_incsearch_sort_dump() for :uniq
1288func Test_incsearch_uniq_dump()
1289  CheckOption incsearch
1290  CheckScreendump
1291
1292  call writefile([
1293	\ 'set incsearch hlsearch scrolloff=0',
1294	\ 'call setline(1, ["another one 2", "that one 3", "the one 1"])',
1295	\ ], 'Xis_uniq_script', 'D')
1296  let buf = RunVimInTerminal('-S Xis_uniq_script', {'rows': 9, 'cols': 70})
1297  " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1298  " the 'ambiwidth' check.
1299  sleep 100m
1300
1301  call term_sendkeys(buf, ':uniq /on')
1302  call VerifyScreenDump(buf, 'Test_incsearch_uniq_01', {})
1303  call term_sendkeys(buf, "\<Esc>")
1304
1305  call term_sendkeys(buf, ':uniq! /on')
1306  call VerifyScreenDump(buf, 'Test_incsearch_uniq_02', {})
1307  call term_sendkeys(buf, "\<Esc>")
1308
1309  call StopVimInTerminal(buf)
1310endfunc
1311
1312" Similar to Test_incsearch_substitute_dump() for :vimgrep famiry
1313func Test_incsearch_vimgrep_dump()
1314  CheckOption incsearch
1315  CheckScreendump
1316
1317  call writefile([
1318	\ 'set incsearch hlsearch scrolloff=0',
1319	\ 'call setline(1, ["another one 2", "that one 3", "the one 1"])',
1320	\ ], 'Xis_vimgrep_script', 'D')
1321  let buf = RunVimInTerminal('-S Xis_vimgrep_script', {'rows': 9, 'cols': 70})
1322  " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1323  " the 'ambiwidth' check.
1324  sleep 100m
1325
1326  " Need to send one key at a time to force a redraw.
1327  call term_sendkeys(buf, ':vimgrep on')
1328  call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_01', {})
1329  call term_sendkeys(buf, "\<Esc>")
1330
1331  call term_sendkeys(buf, ':vimg /on/ *.txt')
1332  call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_02', {})
1333  call term_sendkeys(buf, "\<Esc>")
1334
1335  call term_sendkeys(buf, ':vimgrepadd "\<on')
1336  call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_03', {})
1337  call term_sendkeys(buf, "\<Esc>")
1338
1339  call term_sendkeys(buf, ':lv "tha')
1340  call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_04', {})
1341  call term_sendkeys(buf, "\<Esc>")
1342
1343  call term_sendkeys(buf, ':lvimgrepa "the" **/*.txt')
1344  call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_05', {})
1345  call term_sendkeys(buf, "\<Esc>")
1346
1347  call StopVimInTerminal(buf)
1348endfunc
1349
1350func Test_keep_last_search_pattern()
1351  CheckOption incsearch
1352
1353  new
1354  call setline(1, ['foo', 'foo', 'foo'])
1355  set incsearch
1356  call Ntest_override("char_avail", 1)
1357  let @/ = 'bar'
1358  call feedkeys(":/foo/s//\<Esc>", 'ntx')
1359  call assert_equal('bar', @/)
1360
1361  " no error message if pattern not found
1362  call feedkeys(":/xyz/s//\<Esc>", 'ntx')
1363  call assert_equal('bar', @/)
1364
1365  bwipe!
1366  call Ntest_override("ALL", 0)
1367  set noincsearch
1368endfunc
1369
1370func Test_word_under_cursor_after_match()
1371  CheckOption incsearch
1372
1373  new
1374  call setline(1, 'foo bar')
1375  set incsearch
1376  call Ntest_override("char_avail", 1)
1377  try
1378    call feedkeys("/foo\<C-R>\<C-W>\<CR>", 'ntx')
1379  catch /E486:/
1380  endtry
1381  call assert_equal('foobar', @/)
1382
1383  bwipe!
1384  call Ntest_override("ALL", 0)
1385  set noincsearch
1386endfunc
1387
1388func Test_subst_word_under_cursor()
1389  CheckOption incsearch
1390
1391  new
1392  call setline(1, ['int SomeLongName;', 'for (xxx = 1; xxx < len; ++xxx)'])
1393  set incsearch
1394  call Ntest_override("char_avail", 1)
1395  call feedkeys("/LongName\<CR>", 'ntx')
1396  call feedkeys(":%s/xxx/\<C-R>\<C-W>/g\<CR>", 'ntx')
1397  call assert_equal('for (SomeLongName = 1; SomeLongName < len; ++SomeLongName)', getline(2))
1398
1399  bwipe!
1400  call Ntest_override("ALL", 0)
1401  set noincsearch
1402endfunc
1403
1404func Test_search_skip_all_matches()
1405  enew
1406  call setline(1, ['no match here',
1407        \ 'match this line',
1408        \ 'nope',
1409        \ 'match in this line',
1410        \ 'last line',
1411        \ ])
1412  call cursor(1, 1)
1413  let lnum = search('this', '', 0, 0, 'getline(".") =~ "this line"')
1414  " Only check that no match is found.  Previously it searched forever.
1415  call assert_equal(0, lnum)
1416
1417  bwipe!
1418endfunc
1419
1420func Test_search_undefined_behaviour()
1421  CheckFeature terminal
1422
1423  let h = winheight(0)
1424  if h < 3
1425    return
1426  endif
1427  " did cause an undefined left shift
1428  let g:buf = term_start([GetVimProg(), '--clean', '-e', '-s', '-c', 'call search(getline("."))', 'samples/test000'], {'term_rows': 3})
1429  call assert_equal([''], getline(1, '$'))
1430  call term_sendkeys(g:buf, ":qa!\<cr>")
1431  bwipe!
1432endfunc
1433
1434func Test_search_undefined_behaviour2()
1435  call search("\%UC0000000")
1436endfunc
1437
1438" Test for search('multi-byte char', 'bce')
1439func Test_search_multibyte()
1440  let save_enc = &encoding
1441  set encoding=utf8
1442  enew!
1443  call append('$', 'A')
1444  call cursor(2, 1)
1445  call assert_equal(2, search('A', 'bce', line('.')))
1446  enew!
1447  let &encoding = save_enc
1448endfunc
1449
1450" This was causing E874.  Also causes an invalid read?
1451func Test_look_behind()
1452  new
1453  call setline(1, '0\|\&\n\@<=')
1454  call search(getline("."))
1455  bwipe!
1456endfunc
1457
1458func Test_search_visual_area_linewise()
1459  new
1460  call setline(1, ['aa', 'bb', 'cc'])
1461  exe "normal 2GV\<Esc>"
1462  for engine in [1, 2]
1463    exe 'set regexpengine=' .. engine
1464    exe "normal gg/\\%'<\<CR>>"
1465    call assert_equal([0, 2, 1, 0, 1], getcurpos(), 'engine ' .. engine)
1466    exe "normal gg/\\%'>\<CR>"
1467    call assert_equal([0, 2, 2, 0, 2], getcurpos(), 'engine ' .. engine)
1468  endfor
1469
1470  bwipe!
1471  set regexpengine&
1472endfunc
1473
1474func Test_search_sentence()
1475  new
1476  " this used to cause a crash
1477  /\%'(
1478  /
1479  bwipe
1480endfunc
1481
1482" Test that there is no crash when there is a last search pattern but no last
1483" substitute pattern.
1484func Test_no_last_substitute_pat()
1485  throw 'Skipped: TODO: '
1486  " Use viminfo to set the last search pattern to a string and make the last
1487  " substitute pattern the most recent used and make it empty (NULL).
1488  call writefile(['~MSle0/bar', '~MSle0~&'], 'Xviminfo', 'D')
1489  rviminfo! Xviminfo
1490  call assert_fails('normal n', 'E35:')
1491endfunc
1492
1493func Test_search_Ctrl_L_combining()
1494  " Make sure, that Ctrl-L works correctly with combining characters.
1495  " It uses an artificial example of an 'a' with 4 combining chars:
1496    " 'a' U+0061 Dec:97 LATIN SMALL LETTER A &#x61; /\%u61\Z "\u0061"
1497    " ' ̀' U+0300 Dec:768 COMBINING GRAVE ACCENT &#x300; /\%u300\Z "\u0300"
1498    " ' ́' U+0301 Dec:769 COMBINING ACUTE ACCENT &#x301; /\%u301\Z "\u0301"
1499    " ' ̇' U+0307 Dec:775 COMBINING DOT ABOVE &#x307; /\%u307\Z "\u0307"
1500    " ' ̣' U+0323 Dec:803 COMBINING DOT BELOW &#x323; /\%u323 "\u0323"
1501  " Those should also appear on the commandline
1502  CheckOption incsearch
1503
1504  call Cmdline3_prep()
1505  1
1506  let bufcontent = ['', 'Miạ̀́̇m']
1507  call append('$', bufcontent)
1508  call feedkeys("/Mi\<c-l>\<c-l>\<cr>", 'tx')
1509  call assert_equal(5, line('.'))
1510  call assert_equal(bufcontent[1], @/)
1511  call Incsearch_cleanup()
1512endfunc
1513
1514func Test_large_hex_chars1()
1515  " This used to cause a crash, the character becomes an NFA state.
1516  try
1517    /\%Ufffffc23
1518  catch
1519    call assert_match('E678:', v:exception)
1520  endtry
1521  try
1522    set re=1
1523    /\%Ufffffc23
1524  catch
1525    call assert_match('E678:', v:exception)
1526  endtry
1527  set re&
1528endfunc
1529
1530func Test_large_hex_chars2()
1531  " This used to cause a crash, the character becomes an NFA state.
1532  try
1533    /[\Ufffffc1f]
1534  catch
1535    call assert_match('E1541:', v:exception)
1536  endtry
1537  try
1538    set re=1
1539    /[\Ufffffc1f]
1540  catch
1541    call assert_match('E1541:', v:exception)
1542  endtry
1543  set re&
1544endfunc
1545
1546func Test_large_hex_chars3()
1547  " Validate max number of Unicode char
1548  try
1549    /[\UFFFFFFFF]
1550  catch
1551    call assert_match('E1541:', v:exception)
1552  endtry
1553  try
1554    /[\UFFFFFFF]
1555  catch
1556    call assert_match('E486:', v:exception)
1557  endtry
1558  try
1559    /\%#=2[\d32-\UFFFFFFFF]
1560  catch
1561    call assert_match('E1541:', v:exception)
1562  endtry
1563  try
1564    /\%#=1[\UFFFFFFFF]
1565  catch
1566    call assert_match('E1541:', v:exception)
1567  endtry
1568  try
1569    /\%#=1[\d32-\UFFFFFFFF]
1570  catch
1571    call assert_match('E945:', v:exception)
1572  endtry
1573endfunc
1574
1575func Test_one_error_msg()
1576  " This was also giving an internal error
1577  call assert_fails('call search(" \\((\\v[[=P=]]){185}+             ")', 'E871:')
1578endfunc
1579
1580func Test_incsearch_add_char_under_cursor()
1581  CheckOption incsearch
1582
1583  set incsearch
1584  new
1585  call setline(1, ['find match', 'anything'])
1586  1
1587  call Ntest_override('char_avail', 1)
1588  call feedkeys("fc/m\<C-L>\<C-L>\<C-L>\<C-L>\<C-L>\<CR>", 'tx')
1589  call assert_equal('match', @/)
1590  call Ntest_override('char_avail', 0)
1591
1592  set incsearch&
1593  bwipe!
1594endfunc
1595
1596" Test for the search() function with match at the cursor position
1597func Test_search_match_at_curpos()
1598  new
1599  call append(0, ['foobar', '', 'one two', ''])
1600
1601  normal gg
1602
1603  eval 'foobar'->search('c')
1604  call assert_equal([1, 1], [line('.'), col('.')])
1605
1606  normal j
1607  call search('^$', 'c')
1608  call assert_equal([2, 1], [line('.'), col('.')])
1609
1610  call search('^$', 'bc')
1611  call assert_equal([2, 1], [line('.'), col('.')])
1612
1613  exe "normal /two\<CR>"
1614  call search('.', 'c')
1615  call assert_equal([3, 5], [line('.'), col('.')])
1616
1617  bw!
1618endfunc
1619
1620" Test for error cases with the search() function
1621func Test_search_errors()
1622  call assert_fails("call search('pat', [])", 'E730:')
1623  call assert_fails("call search('pat', 'b', {})", 'E728:')
1624  call assert_fails("call search('pat', 'b', 1, [])", 'E745:')
1625  call assert_fails("call search('pat', 'ns')", 'E475:')
1626  call assert_fails("call search('pat', 'mr')", 'E475:')
1627
1628  new
1629  call setline(1, ['foo', 'bar'])
1630  call assert_fails('call feedkeys("/foo/;/bar/;\<CR>", "tx")', 'E386:')
1631  bwipe!
1632endfunc
1633
1634func Test_search_display_pattern()
1635  new
1636  call setline(1, ['foo', 'bar', 'foobar'])
1637
1638  call cursor(1, 1)
1639  let @/ = 'foo'
1640  let pat = @/->escape('()*?'. '\s\+')
1641  let g:a = execute(':unsilent :norm! n')
1642  call assert_match(pat, g:a)
1643
1644  " right-left
1645  if exists("+rightleft")
1646    set rl
1647    call cursor(1, 1)
1648    let @/ = 'foo'
1649    let pat = 'oof/\s\+'
1650    let g:a = execute(':unsilent :norm! n')
1651    call assert_match(pat, g:a)
1652    set norl
1653  endif
1654endfunc
1655
1656func Test_searchdecl()
1657  let lines =<< trim END
1658     int global;
1659
1660     func()
1661     {
1662       int global;
1663       if (cond) {
1664	 int local;
1665       }
1666       int local;
1667       // comment
1668     }
1669  END
1670  new
1671  call setline(1, lines)
1672  10
1673  call assert_equal(0, searchdecl('local', 0, 0))
1674  call assert_equal(7, getcurpos()[1])
1675
1676  10
1677  call assert_equal(0, 'local'->searchdecl(0, 1))
1678  call assert_equal(9, getcurpos()[1])
1679
1680  10
1681  call assert_equal(0, searchdecl('global'))
1682  call assert_equal(5, getcurpos()[1])
1683
1684  10
1685  call assert_equal(0, searchdecl('global', 1))
1686  call assert_equal(1, getcurpos()[1])
1687
1688  bwipe!
1689endfunc
1690
1691func Test_search_special()
1692  " this was causing illegal memory access and an endless loop
1693  set t_PE=
1694  exe "norm /\x80PS"
1695endfunc
1696
1697" Test for command failures when the last search pattern is not set.
1698" Need to run this in a new vim instance where last search pattern is not set.
1699func Test_search_with_no_last_pat()
1700  let lines =<< trim [SCRIPT]
1701    call assert_fails("normal i\<C-R>/\e", 'E35:')
1702    call assert_fails("exe '/'", 'E35:')
1703    call assert_fails("exe '?'", 'E35:')
1704    call assert_fails("/", 'E35:')
1705    call assert_fails("?", 'E35:')
1706    call assert_fails("normal n", 'E35:')
1707    call assert_fails("normal N", 'E35:')
1708    call assert_fails("normal gn", 'E35:')
1709    call assert_fails("normal gN", 'E35:')
1710    call assert_fails("normal cgn", 'E35:')
1711    call assert_fails("normal cgN", 'E35:')
1712    let p = []
1713    let p = @/
1714    call assert_equal('', p)
1715    call assert_fails("normal :\<C-R>/", 'E35:')
1716    call assert_fails("//p", 'E35:')
1717    call assert_fails(";//p", 'E35:')
1718    call assert_fails("??p", 'E35:')
1719    call assert_fails(";??p", 'E35:')
1720    call assert_fails('g//p', ['E35:', 'E476:'])
1721    call assert_fails('v//p', ['E35:', 'E476:'])
1722    call writefile(v:errors, 'Xresult')
1723    qall!
1724  [SCRIPT]
1725  call writefile(lines, 'Xscript', 'D')
1726
1727  if RunVim([], [], '--clean -S Xscript')
1728    call assert_equal([], readfile('Xresult'))
1729  endif
1730  call delete('Xresult')
1731endfunc
1732
1733" Test for using the last substitute pattern without last search pattern.
1734func Test_search_with_last_substitute_pat()
1735  let lines =<< trim [SCRIPT]
1736    new
1737    set shortmess+=S
1738    call setline(1, repeat(['foofoo'], 3))
1739    %s/foo/bar/
1740    call assert_equal(repeat(['barfoo'], 3), getline(1, '$'))
1741
1742    call cursor(1, 1)
1743    call assert_equal("/foo", execute('call feedkeys("/\r", "tx")', '')->trim())
1744    call assert_equal([0, 1, 4, 0], getpos('.'))
1745
1746    if has('rightleft')
1747      set rightleft rightleftcmd=search
1748      call cursor(1, 1)
1749      call assert_equal("oof/", execute('call feedkeys("/\r", "tx")', '')->trim())
1750      call assert_equal([0, 1, 4, 0], getpos('.'))
1751    endif
1752
1753    call writefile(v:errors, 'Xresult')
1754    qall!
1755  [SCRIPT]
1756  call writefile(lines, 'Xscript', 'D')
1757
1758  if RunVim([], [], '--clean -S Xscript')
1759    call assert_equal([], readfile('Xresult'))
1760  endif
1761  call delete('Xresult')
1762endfunc
1763
1764" Test for using tilde (~) atom in search. This should use the last used
1765" substitute pattern
1766func Test_search_tilde_pat()
1767  let lines =<< trim [SCRIPT]
1768    set regexpengine=1
1769    call assert_fails('exe "normal /~\<CR>"', 'E33:')
1770    call assert_fails('exe "normal ?~\<CR>"', 'E33:')
1771    set regexpengine=2
1772    call assert_fails('exe "normal /~\<CR>"', ['E33:', 'E383:'])
1773    call assert_fails('exe "normal ?~\<CR>"', ['E33:', 'E383:'])
1774    set regexpengine&
1775    call writefile(v:errors, 'Xresult')
1776    qall!
1777  [SCRIPT]
1778  call writefile(lines, 'Xscript', 'D')
1779  if RunVim([], [], '--clean -S Xscript')
1780    call assert_equal([], readfile('Xresult'))
1781  endif
1782  call delete('Xresult')
1783endfunc
1784
1785" Test for searching a pattern that is not present with 'nowrapscan'
1786func Test_search_pat_not_found()
1787  new
1788  set nowrapscan
1789  let @/ = '1abcxyz2'
1790  call assert_fails('normal n', 'E385:')
1791  call assert_fails('normal N', 'E384:')
1792  set wrapscan&
1793  bw
1794endfunc
1795
1796" Test for v:searchforward variable
1797func Test_searchforward_var()
1798  new
1799  call setline(1, ['foo', '', 'foo'])
1800  call cursor(2, 1)
1801  let @/ = 'foo'
1802  let v:searchforward = 0
1803  normal N
1804  call assert_equal(3, line('.'))
1805  call cursor(2, 1)
1806  let v:searchforward = 1
1807  normal N
1808  call assert_equal(1, line('.'))
1809  bw!
1810endfunc
1811
1812" Test for invalid regular expressions
1813func Test_invalid_regexp()
1814  set regexpengine=1
1815  call assert_fails("call search(repeat('\\(.\\)', 10))", 'E51:')
1816  call assert_fails("call search('\\%(')", 'E53:')
1817  call assert_fails("call search('\\(')", 'E54:')
1818  call assert_fails("call search('\\)')", 'E55:')
1819  call assert_fails("call search('x\\@#')", 'E59:')
1820  call assert_fails('call search(''\v%(%(%(%(%(%(%(%(%(%(%(a){1}){1}){1}){1}){1}){1}){1}){1}){1}){1}){1}'')', 'E60:')
1821  call assert_fails("call search('a\\+*')", 'E61:')
1822  call assert_fails("call search('\\_m')", 'E63:')
1823  call assert_fails("call search('\\+')", 'E64:')
1824  call assert_fails("call search('\\1')", 'E65:')
1825  call assert_fails("call search('\\z\\(\\)')", 'E66:')
1826  call assert_fails("call search('\\z2')", 'E67:')
1827  call assert_fails("call search('\\zx')", 'E68:')
1828  call assert_fails("call search('\\%[ab')", 'E69:')
1829  call assert_fails("call search('\\%[]')", 'E70:')
1830  call assert_fails("call search('\\%a')", 'E71:')
1831  call assert_fails("call search('ab\\%[\\(cd\\)]')", 'E369:')
1832  call assert_fails("call search('ab\\%[\\%(cd\\)]')", 'E369:')
1833  set regexpengine=2
1834  call assert_fails("call search('\\_')", 'E865:')
1835  call assert_fails("call search('\\+')", 'E866:')
1836  call assert_fails("call search('\\zx')", 'E867:')
1837  call assert_fails("call search('\\%a')", 'E867:')
1838  call assert_fails("call search('x\\@#')", 'E869:')
1839  call assert_fails("call search(repeat('\\(.\\)', 10))", 'E872:')
1840  call assert_fails("call search('\\_m')", 'E877:')
1841  call assert_fails("call search('\\%(')", 'E53:')
1842  call assert_fails("call search('\\(')", 'E54:')
1843  call assert_fails("call search('\\)')", 'E55:')
1844  call assert_fails("call search('\\z\\(\\)')", 'E66:')
1845  call assert_fails("call search('\\z2')", 'E67:')
1846  call assert_fails("call search('\\zx')", 'E867:')
1847  call assert_fails("call search('\\%[ab')", 'E69:')
1848  call assert_fails("call search('\\%[]')", 'E70:')
1849  call assert_fails("call search('\\%9999999999999999999999999999v')", 'E951:')
1850  set regexpengine&
1851  call assert_fails("call search('\\%#=3ab')", 'E864:')
1852endfunc
1853
1854" Test for searching a very complex pattern in a string. Should switch the
1855" regexp engine from NFA to the old engine.
1856func Test_regexp_switch_engine()
1857  let l = readfile('samples/re.freeze.txt')
1858  let v = substitute(l[4], '..\@<!', '', '')
1859  call assert_equal(l[4], v)
1860endfunc
1861
1862" Test for the \%V atom to search within visually selected text
1863func Test_search_in_visual_area()
1864  new
1865  call setline(1, ['foo bar1', 'foo bar2', 'foo bar3', 'foo bar4'])
1866  exe "normal 2GVjo/\\%Vbar\<CR>\<Esc>"
1867  call assert_equal([2, 5], [line('.'), col('.')])
1868  exe "normal 2GVj$?\\%Vbar\<CR>\<Esc>"
1869  call assert_equal([3, 5], [line('.'), col('.')])
1870  bw!
1871endfunc
1872
1873" Test for searching with 'smartcase' and 'ignorecase'
1874func Test_search_smartcase()
1875  new
1876  call setline(1, ['', 'Hello'])
1877  set noignorecase nosmartcase
1878  call assert_fails('exe "normal /\\a\\_.\\(.*\\)O\<CR>"', 'E486:')
1879
1880  set ignorecase nosmartcase
1881  exe "normal /\\a\\_.\\(.*\\)O\<CR>"
1882  call assert_equal([2, 1], [line('.'), col('.')])
1883
1884  call cursor(1, 1)
1885  set ignorecase smartcase
1886  call assert_fails('exe "normal /\\a\\_.\\(.*\\)O\<CR>"', 'E486:')
1887
1888  exe "normal /\\a\\_.\\(.*\\)o\<CR>"
1889  call assert_equal([2, 1], [line('.'), col('.')])
1890
1891  " Test for using special atoms with 'smartcase'
1892  call setline(1, ['', '    Hello\ '])
1893  call cursor(1, 1)
1894  call feedkeys('/\_.\%(\uello\)\' .. "\<CR>", 'xt')
1895  call assert_equal([2, 4], [line('.'), col('.')])
1896
1897  set ignorecase& smartcase&
1898  bw!
1899endfun
1900
1901" Test 'smartcase' with utf-8.
1902func Test_search_smartcase_utf8()
1903  new
1904  let save_enc = &encoding
1905  set encoding=utf8 ignorecase smartcase
1906
1907  call setline(1, 'Café cafÉ')
1908  1s/café/x/g
1909  call assert_equal('x x', getline(1))
1910
1911  call setline(1, 'Café cafÉ')
1912  1s/cafÉ/x/g
1913  call assert_equal('Café x', getline(1))
1914
1915  set ignorecase& smartcase&
1916  let &encoding = save_enc
1917  bwipe!
1918endfunc
1919
1920" Test searching past the end of a file
1921func Test_search_past_eof()
1922  new
1923  call setline(1, ['Line'])
1924  exe "normal /\\n\\zs\<CR>"
1925  call assert_equal([1, 4], [line('.'), col('.')])
1926  bwipe!
1927endfunc
1928
1929" Test setting the start of the match and still finding a next match in the
1930" same line.
1931func Test_search_set_start_same_line()
1932  new
1933  set cpo-=c
1934
1935  call setline(1, ['1', '2', '3 .', '4', '5'])
1936  exe "normal /\\_s\\zs\\S\<CR>"
1937  call assert_equal([2, 1], [line('.'), col('.')])
1938  exe 'normal n'
1939  call assert_equal([3, 1], [line('.'), col('.')])
1940  exe 'normal n'
1941  call assert_equal([3, 3], [line('.'), col('.')])
1942  exe 'normal n'
1943  call assert_equal([4, 1], [line('.'), col('.')])
1944  exe 'normal n'
1945  call assert_equal([5, 1], [line('.'), col('.')])
1946
1947  set cpo+=c
1948  bwipe!
1949endfunc
1950
1951" Test for various search offsets
1952func Test_search_offset()
1953  " With /e, for a match in the first column of a line, the cursor should be
1954  " placed at the end of the previous line.
1955  new
1956  call setline(1, ['one two', 'three four'])
1957  call search('two\_.', 'e')
1958  call assert_equal([1, 7], [line('.'), col('.')])
1959
1960  " with cursor at the beginning of the file, use /s+1
1961  " '+' without a digit is the same as +1, and 'b' is an alias for 's'
1962  for searchcmd in ['/two/s+1', '/two/s+', '/two/b+1', '/two/b+', '/']
1963    call cursor(1, 1)
1964    exe $"normal {searchcmd}\<CR>"
1965    call assert_equal([1, 6], [line('.'), col('.')], searchcmd)
1966    call assert_equal('/two/s+1', Screenline(&lines)->trim(), searchcmd)
1967  endfor
1968
1969  " repeat the same search pattern with different offsets
1970  for [offset, col] in [['s', 5], ['e-1', 6], ['s+2', 7], ['s-2', 3], ['', 5]]
1971    let searchcmd = $'//{offset}'
1972    call cursor(1, 1)
1973    exe $"normal {searchcmd}\<CR>"
1974    call assert_equal([1, col], [line('.'), col('.')], searchcmd)
1975    call assert_equal(col == 5 ? '/two' : $'/two/{offset}',
1976                      \ Screenline(&lines)->trim(), searchcmd)
1977  endfor
1978
1979  " with cursor at the end of the file, use /e-1
1980  " '-' without a digit is the same as -1
1981  for searchcmd in ['?three?e-1', '?three?e-', '?']
1982    call cursor(2, 10)
1983    exe $"normal {searchcmd}\<CR>"
1984    call assert_equal([2, 4], [line('.'), col('.')], searchcmd)
1985    call assert_equal('?three?e-1', Screenline(&lines)->trim(), searchcmd)
1986  endfor
1987
1988  " repeat the same search pattern with different offsets
1989  for [offset, col] in [['e', 5], ['s+1', 2], ['e-2', 3], ['e+2', 7], ['', 1]]
1990    let searchcmd = $'??{offset}'
1991    call cursor(2, 10)
1992    exe $"normal {searchcmd}\<CR>"
1993    call assert_equal([2, col], [line('.'), col('.')], searchcmd)
1994    call assert_equal(col == 1 ? '?three' : $'?three?{offset}',
1995                      \ Screenline(&lines)->trim(), searchcmd)
1996  endfor
1997
1998  " line offset - after the last line
1999  " '+' without a digit and '1' without a sign are the same as +1
2000  for searchcmd in ['/three/+1', '/three/+', '/three/1', '/']
2001    call cursor(1, 1)
2002    exe $"normal {searchcmd}\<CR>"
2003    call assert_equal([2, 1], [line('.'), col('.')], searchcmd)
2004    call assert_equal('/three/+1', Screenline(&lines)->trim(), searchcmd)
2005  endfor
2006
2007  " repeat the same search pattern with different line offsets
2008  for [offset, lnum] in [['+0', 2], ['-1', 1], ['+2', 2], ['-2', 1]]
2009    let searchcmd = $'//{offset}'
2010    call cursor(1, 1)
2011    exe $"normal {searchcmd}\<CR>"
2012    call assert_equal([lnum, 1], [line('.'), col('.')], searchcmd)
2013    call assert_equal($'/three/{offset}',
2014                      \ Screenline(&lines)->trim(), searchcmd)
2015  endfor
2016
2017  " line offset - before the first line
2018  " '-' without a digit is the same as -1
2019  for searchcmd in ['?one?-1', '?one?-', '?']
2020    call cursor(2, 1)
2021    exe $"normal {searchcmd}\<CR>"
2022    call assert_equal([1, 1], [line('.'), col('.')], searchcmd)
2023    call assert_equal('?one?-1', Screenline(&lines)->trim(), searchcmd)
2024  endfor
2025
2026  " repeat the same search pattern with different line offsets
2027  for [offset, lnum] in [['+0', 1], ['+1', 2], ['-2', 1], ['+2', 2]]
2028    let searchcmd = $'??{offset}'
2029    call cursor(2, 1)
2030    exe $"normal {searchcmd}\<CR>"
2031    call assert_equal([lnum, 1], [line('.'), col('.')], searchcmd)
2032    call assert_equal($'?one?{offset}',
2033                      \ Screenline(&lines)->trim(), searchcmd)
2034  endfor
2035
2036  " character offset - before the first character in the file
2037  call cursor(2, 1)
2038  exe "normal ?one?s-1\<CR>"
2039  call assert_equal([1, 1], [line('.'), col('.')])
2040  call assert_equal('?one?s-1', Screenline(&lines)->trim())
2041  call cursor(2, 1)
2042  exe "normal ?one?e-3\<CR>"
2043  call assert_equal([1, 1], [line('.'), col('.')])
2044  call assert_equal('?one?e-3', Screenline(&lines)->trim())
2045
2046  " character offset - after the last character in the file
2047  call cursor(1, 1)
2048  exe "normal /four/s+4\<CR>"
2049  call assert_equal([2, 10], [line('.'), col('.')])
2050  call assert_equal('/four/s+4', Screenline(&lines)->trim())
2051  call cursor(1, 1)
2052  exe "normal /four/e+1\<CR>"
2053  call assert_equal([2, 10], [line('.'), col('.')])
2054  call assert_equal('/four/e+1', Screenline(&lines)->trim())
2055
2056  bw!
2057endfunc
2058
2059" Test for searching for matching parenthesis using %
2060func Test_search_match_paren()
2061  new
2062  call setline(1, "abc(def')'ghi'('jk'\\t'lm)no")
2063  " searching for a matching parenthesis should skip single quoted characters
2064  call cursor(1, 4)
2065  normal %
2066  call assert_equal([1, 25], [line('.'), col('.')])
2067  normal %
2068  call assert_equal([1, 4], [line('.'), col('.')])
2069  call cursor(1, 5)
2070  normal ])
2071  call assert_equal([1, 25], [line('.'), col('.')])
2072  call cursor(1, 24)
2073  normal [(
2074  call assert_equal([1, 4], [line('.'), col('.')])
2075
2076  call setline(1, ['x" (a "b" )\', '")'])
2077  call cursor(1, 4)
2078  normal %
2079  call assert_equal([1, 11], [line('.'), col('.')])
2080  normal %
2081  call assert_equal([1, 4], [line('.'), col('.')])
2082  call cursor(1, 10)
2083  normal [(
2084  call assert_equal([1, 4], [line('.'), col('.')])
2085  call cursor(1, 4)
2086  normal ])
2087  call assert_equal([1, 11], [line('.'), col('.')])
2088
2089  " matching parenthesis in 'virtualedit' mode with cursor after the eol
2090  call setline(1, 'abc(defgh)')
2091  set virtualedit=all
2092  normal 20|%
2093  call assert_equal(4, col('.'))
2094  set virtualedit&
2095  bw!
2096endfunc
2097
2098" Test for searching a pattern and stopping before a specified line
2099func Test_search_stopline()
2100  new
2101  call setline(1, ['', '', '', 'vim'])
2102  call assert_equal(0, search('vim', 'n', 3))
2103  call assert_equal(4, search('vim', 'n', 4))
2104  call setline(1, ['vim', '', '', ''])
2105  call cursor(4, 1)
2106  call assert_equal(0, search('vim', 'bn', 2))
2107  call assert_equal(1, search('vim', 'bn', 1))
2108  bw!
2109endfunc
2110
2111func Test_incsearch_highlighting_newline()
2112  CheckRunVimInTerminal
2113  CheckOption incsearch
2114  CheckScreendump
2115  new
2116  call test_override("char_avail", 1)
2117
2118  let commands =<< trim [CODE]
2119    set incsearch nohls
2120    call setline(1, ['test', 'xxx'])
2121  [CODE]
2122  call writefile(commands, 'Xincsearch_nl', 'D')
2123  let buf = RunVimInTerminal('-S Xincsearch_nl', {'rows': 5, 'cols': 10})
2124  call TermWait(buf, 100)
2125  call term_sendkeys(buf, '/test')
2126  call VerifyScreenDump(buf, 'Test_incsearch_newline1', {})
2127  " Need to send one key at a time to force a redraw
2128  call term_sendkeys(buf, '\n')
2129  call VerifyScreenDump(buf, 'Test_incsearch_newline2', {})
2130  call term_sendkeys(buf, 'x')
2131  call VerifyScreenDump(buf, 'Test_incsearch_newline3', {})
2132  call term_sendkeys(buf, 'x')
2133  call VerifyScreenDump(buf, 'Test_incsearch_newline4', {})
2134  call term_sendkeys(buf, "\<CR>")
2135  call VerifyScreenDump(buf, 'Test_incsearch_newline5', {})
2136  call StopVimInTerminal(buf)
2137
2138  " clean up
2139  call test_override("char_avail", 0)
2140  bw
2141endfunc
2142
2143func Test_incsearch_substitute_dump2()
2144  CheckOption incsearch
2145  CheckScreendump
2146
2147  call writefile([
2148	\ 'set incsearch hlsearch scrolloff=0',
2149	\ 'for n in range(1, 4)',
2150	\ '  call setline(n, "foo " . n)',
2151	\ 'endfor',
2152	\ 'call setline(5, "abc|def")',
2153	\ '3',
2154	\ ], 'Xis_subst_script2', 'D')
2155  let buf = RunVimInTerminal('-S Xis_subst_script2', {'rows': 9, 'cols': 70})
2156
2157  call term_sendkeys(buf, ':%s/\vabc|')
2158  sleep 100m
2159  call VerifyScreenDump(buf, 'Test_incsearch_sub_01', {})
2160  call term_sendkeys(buf, "\<Esc>")
2161
2162  " The following should not be highlighted
2163  call term_sendkeys(buf, ':1,5s/\v|')
2164  sleep 100m
2165  call VerifyScreenDump(buf, 'Test_incsearch_sub_02', {})
2166
2167
2168  call StopVimInTerminal(buf)
2169endfunc
2170
2171func Test_incsearch_restore_view()
2172  CheckOption incsearch
2173  CheckScreendump
2174
2175  let lines =<< trim [CODE]
2176    set incsearch nohlsearch
2177    setlocal scrolloff=0 smoothscroll
2178    call setline(1, [join(range(25), ' '), '', '', '', '', 'xxx'])
2179    call feedkeys("2\<C-E>", 't')
2180  [CODE]
2181  call writefile(lines, 'Xincsearch_restore_view', 'D')
2182  let buf = RunVimInTerminal('-S Xincsearch_restore_view', {'rows': 6, 'cols': 20})
2183
2184  call VerifyScreenDump(buf, 'Test_incsearch_restore_view_01', {})
2185  call term_sendkeys(buf, '/xx')
2186  call VerifyScreenDump(buf, 'Test_incsearch_restore_view_02', {})
2187  call term_sendkeys(buf, 'x')
2188  call VerifyScreenDump(buf, 'Test_incsearch_restore_view_03', {})
2189  call term_sendkeys(buf, "\<Esc>")
2190  call VerifyScreenDump(buf, 'Test_incsearch_restore_view_01', {})
2191
2192  call StopVimInTerminal(buf)
2193endfunc
2194
2195func Test_pattern_is_uppercase_smartcase()
2196  new
2197  let input=['abc', 'ABC', 'Abc', 'abC']
2198  call setline(1, input)
2199  call cursor(1,1)
2200  " default, matches firstline
2201  %s/abc//g
2202  call assert_equal(['', 'ABC', 'Abc', 'abC'],
2203        \ getline(1, '$'))
2204
2205  set smartcase ignorecase
2206  sil %d
2207  call setline(1, input)
2208  call cursor(1,1)
2209  " with smartcase and incsearch set, matches everything
2210  %s/abc//g
2211  call assert_equal(['', '', '', ''], getline(1, '$'))
2212
2213  sil %d
2214  call setline(1, input)
2215  call cursor(1,1)
2216  " with smartcase and incsearch set and found an uppercase letter,
2217  " match only that.
2218  %s/abC//g
2219  call assert_equal(['abc', 'ABC', 'Abc', ''],
2220        \ getline(1, '$'))
2221
2222  sil %d
2223  call setline(1, input)
2224  call cursor(1,1)
2225  exe "norm! vG$\<esc>"
2226  " \%V should not be detected as uppercase letter
2227  %s/\%Vabc//g
2228  call assert_equal(['', '', '', ''], getline(1, '$'))
2229
2230  call setline(1, input)
2231  call cursor(1,1)
2232  exe "norm! vG$\<esc>"
2233  " \v%V should not be detected as uppercase letter
2234  %s/\v%Vabc//g
2235  call assert_equal(['', '', '', ''], getline(1, '$'))
2236
2237  call setline(1, input)
2238  call cursor(1,1)
2239  exe "norm! vG$\<esc>"
2240  " \v%VabC should be detected as uppercase letter
2241  %s/\v%VabC//g
2242  call assert_equal(['abc', 'ABC', 'Abc', ''],
2243        \ getline(1, '$'))
2244
2245  call setline(1, input)
2246  call cursor(1,1)
2247  " \Vabc should match everything
2248  %s/\Vabc//g
2249  call assert_equal(['', '', '', ''], getline(1, '$'))
2250
2251  call setline(1, input + ['_abc'])
2252  " _ matches normally
2253  %s/\v_.*//g
2254  call assert_equal(['abc', 'ABC', 'Abc', 'abC', ''], getline(1, '$'))
2255
2256  set smartcase& ignorecase&
2257  bw!
2258endfunc
2259
2260func Test_no_last_search_pattern()
2261  CheckOption incsearch
2262
2263  let @/ = ""
2264  set incsearch
2265  " these were causing a crash
2266  call feedkeys("//\<C-G>", 'xt')
2267  call feedkeys("//\<C-T>", 'xt')
2268  call feedkeys("??\<C-G>", 'xt')
2269  call feedkeys("??\<C-T>", 'xt')
2270endfunc
2271
2272func Test_search_with_invalid_range()
2273  new
2274  let lines =<< trim END
2275    /\%.v
2276    5/
2277    c
2278  END
2279  call writefile(lines, 'Xrangesearch', 'D')
2280  source Xrangesearch
2281
2282  bwipe!
2283endfunc
2284
2285func Test_incsearch_delimiter_ctrlg()
2286  CheckOption incsearch
2287  CheckScreendump
2288  CheckRunVimInTerminal
2289  call assert_equal(0, &scrolloff)
2290  call writefile([
2291	\ 'set incsearch hls',
2292  \ 'call setline(1, ["1 vim inc", "2 vim /", "3 vim /", "4 vim ?", "5 vim ?"])',
2293	\ 'normal gg',
2294	\ 'redraw',
2295	\ ], 'Xscript_incsearch_delim', 'D')
2296  let buf = RunVimInTerminal('-S Xscript_incsearch_delim', {'rows': 6})
2297
2298  call term_sendkeys(buf, '/')
2299  sleep 100m
2300  call term_sendkeys(buf, 'v')
2301  sleep 100m
2302  call term_sendkeys(buf, 'i')
2303  sleep 100m
2304  call term_sendkeys(buf, 'm')
2305  sleep 100m
2306  call term_sendkeys(buf, ' ')
2307  sleep 100m
2308  call term_sendkeys(buf, '/')
2309  sleep 100m
2310  call term_sendkeys(buf, "\<C-G>")
2311  call VerifyScreenDump(buf, 'Test_incsearch_delim_01', {})
2312  call term_sendkeys(buf, "\<Esc>")
2313
2314  call term_sendkeys(buf, ":5\<cr>")
2315  call term_sendkeys(buf, '?')
2316  sleep 100m
2317  call term_sendkeys(buf, 'v')
2318  sleep 100m
2319  call term_sendkeys(buf, 'i')
2320  sleep 100m
2321  call term_sendkeys(buf, 'm')
2322  sleep 100m
2323  call term_sendkeys(buf, ' ')
2324  sleep 100m
2325  call term_sendkeys(buf, '?')
2326  sleep 100m
2327  call term_sendkeys(buf, "\<C-T>")
2328  call VerifyScreenDump(buf, 'Test_incsearch_delim_02', {})
2329  call term_sendkeys(buf, "\<Esc>")
2330
2331  call term_sendkeys(buf, '/')
2332  sleep 100m
2333  call term_sendkeys(buf, 'v')
2334  sleep 100m
2335  call term_sendkeys(buf, 'i')
2336  sleep 100m
2337  call term_sendkeys(buf, 'm')
2338  sleep 100m
2339  call term_sendkeys(buf, ' ')
2340  sleep 100m
2341  call term_sendkeys(buf, '\/')
2342  sleep 100m
2343  call term_sendkeys(buf, "\<C-G>")
2344  call VerifyScreenDump(buf, 'Test_incsearch_delim_03', {})
2345  call term_sendkeys(buf, "\<Esc>")
2346
2347  call term_sendkeys(buf, ":5\<cr>")
2348  call term_sendkeys(buf, '?')
2349  sleep 100m
2350  call term_sendkeys(buf, 'v')
2351  sleep 100m
2352  call term_sendkeys(buf, 'i')
2353  sleep 100m
2354  call term_sendkeys(buf, 'm')
2355  sleep 100m
2356  call term_sendkeys(buf, ' ')
2357  sleep 100m
2358  call term_sendkeys(buf, '\?')
2359  sleep 100m
2360  call term_sendkeys(buf, "\<C-T>")
2361  call VerifyScreenDump(buf, 'Test_incsearch_delim_04', {})
2362  call term_sendkeys(buf, "\<Esc>")
2363
2364  call StopVimInTerminal(buf)
2365endfunc
2366
2367func Test_searchcount_maxcount_cached()
2368  new
2369  call setline(1, repeat(['foo'], 5))
2370  set maxsearchcount=99
2371  let @/ = 'foo'
2372  call cursor(1, 1)
2373  call searchcount(#{recompute: v:true,  maxcount: 3})
2374  let r = searchcount(#{recompute: v:false, maxcount: 3})
2375  call assert_equal(3, r.maxcount)
2376  set maxsearchcount&
2377  bwipe!
2378endfunc
2379
2380" vim: shiftwidth=2 sts=2 expandtab