blob: 9b96b49fdefbea7745845de6cb13d00f3803f09b [file] [log] [blame]
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001" Test for the search command
2
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01003source shared.vim
4
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02005func Test_search_cmdline()
6 if !exists('+incsearch')
7 return
8 endif
9 " need to disable char_avail,
10 " so that expansion of commandline works
Bram Moolenaareb992cb2017-03-09 18:20:16 +010011 call test_override("char_avail", 1)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020012 new
13 call setline(1, [' 1', ' 2 these', ' 3 the', ' 4 their', ' 5 there', ' 6 their', ' 7 the', ' 8 them', ' 9 these', ' 10 foobar'])
14 " Test 1
15 " CTRL-N / CTRL-P skips through the previous search history
16 set noincsearch
17 :1
18 call feedkeys("/foobar\<cr>", 'tx')
19 call feedkeys("/the\<cr>",'tx')
20 call assert_equal('the', @/)
Bram Moolenaar11956692016-08-27 16:26:56 +020021 call feedkeys("/thes\<C-P>\<C-P>\<cr>",'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020022 call assert_equal('foobar', @/)
23
24 " Test 2
Bram Moolenaar11956692016-08-27 16:26:56 +020025 " Ctrl-G goes from one match to the next
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020026 " until the end of the buffer
27 set incsearch nowrapscan
28 :1
29 " first match
30 call feedkeys("/the\<cr>", 'tx')
31 call assert_equal(' 2 these', getline('.'))
32 :1
33 " second match
Bram Moolenaar11956692016-08-27 16:26:56 +020034 call feedkeys("/the\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020035 call assert_equal(' 3 the', getline('.'))
Bram Moolenaardda933d2016-09-03 21:04:58 +020036 call assert_equal([0, 0, 0, 0], getpos('"'))
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020037 :1
38 " third match
Bram Moolenaar11956692016-08-27 16:26:56 +020039 call feedkeys("/the".repeat("\<C-G>", 2)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020040 call assert_equal(' 4 their', getline('.'))
41 :1
42 " fourth match
Bram Moolenaar11956692016-08-27 16:26:56 +020043 call feedkeys("/the".repeat("\<C-G>", 3)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020044 call assert_equal(' 5 there', getline('.'))
45 :1
46 " fifth match
Bram Moolenaar11956692016-08-27 16:26:56 +020047 call feedkeys("/the".repeat("\<C-G>", 4)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020048 call assert_equal(' 6 their', getline('.'))
49 :1
50 " sixth match
Bram Moolenaar11956692016-08-27 16:26:56 +020051 call feedkeys("/the".repeat("\<C-G>", 5)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020052 call assert_equal(' 7 the', getline('.'))
53 :1
54 " seventh match
Bram Moolenaar11956692016-08-27 16:26:56 +020055 call feedkeys("/the".repeat("\<C-G>", 6)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020056 call assert_equal(' 8 them', getline('.'))
57 :1
58 " eigth match
Bram Moolenaar11956692016-08-27 16:26:56 +020059 call feedkeys("/the".repeat("\<C-G>", 7)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020060 call assert_equal(' 9 these', getline('.'))
61 :1
62 " no further match
Bram Moolenaar11956692016-08-27 16:26:56 +020063 call feedkeys("/the".repeat("\<C-G>", 8)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020064 call assert_equal(' 9 these', getline('.'))
Bram Moolenaardda933d2016-09-03 21:04:58 +020065 call assert_equal([0, 0, 0, 0], getpos('"'))
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020066
67 " Test 3
Bram Moolenaar11956692016-08-27 16:26:56 +020068 " Ctrl-G goes from one match to the next
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020069 " and continues back at the top
70 set incsearch wrapscan
71 :1
72 " first match
73 call feedkeys("/the\<cr>", 'tx')
74 call assert_equal(' 2 these', getline('.'))
75 :1
76 " second match
Bram Moolenaar11956692016-08-27 16:26:56 +020077 call feedkeys("/the\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020078 call assert_equal(' 3 the', getline('.'))
79 :1
80 " third match
Bram Moolenaar11956692016-08-27 16:26:56 +020081 call feedkeys("/the".repeat("\<C-G>", 2)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020082 call assert_equal(' 4 their', getline('.'))
83 :1
84 " fourth match
Bram Moolenaar11956692016-08-27 16:26:56 +020085 call feedkeys("/the".repeat("\<C-G>", 3)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020086 call assert_equal(' 5 there', getline('.'))
87 :1
88 " fifth match
Bram Moolenaar11956692016-08-27 16:26:56 +020089 call feedkeys("/the".repeat("\<C-G>", 4)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020090 call assert_equal(' 6 their', getline('.'))
91 :1
92 " sixth match
Bram Moolenaar11956692016-08-27 16:26:56 +020093 call feedkeys("/the".repeat("\<C-G>", 5)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020094 call assert_equal(' 7 the', getline('.'))
95 :1
96 " seventh match
Bram Moolenaar11956692016-08-27 16:26:56 +020097 call feedkeys("/the".repeat("\<C-G>", 6)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020098 call assert_equal(' 8 them', getline('.'))
99 :1
100 " eigth match
Bram Moolenaar11956692016-08-27 16:26:56 +0200101 call feedkeys("/the".repeat("\<C-G>", 7)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200102 call assert_equal(' 9 these', getline('.'))
103 :1
104 " back at first match
Bram Moolenaar11956692016-08-27 16:26:56 +0200105 call feedkeys("/the".repeat("\<C-G>", 8)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200106 call assert_equal(' 2 these', getline('.'))
107
108 " Test 4
Bram Moolenaar11956692016-08-27 16:26:56 +0200109 " CTRL-T goes to the previous match
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200110 set incsearch nowrapscan
111 $
112 " first match
113 call feedkeys("?the\<cr>", 'tx')
114 call assert_equal(' 9 these', getline('.'))
115 $
116 " first match
Bram Moolenaar11956692016-08-27 16:26:56 +0200117 call feedkeys("?the\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200118 call assert_equal(' 9 these', getline('.'))
119 $
120 " second match
Bram Moolenaar11956692016-08-27 16:26:56 +0200121 call feedkeys("?the".repeat("\<C-T>", 1)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200122 call assert_equal(' 8 them', getline('.'))
123 $
124 " last match
Bram Moolenaar11956692016-08-27 16:26:56 +0200125 call feedkeys("?the".repeat("\<C-T>", 7)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200126 call assert_equal(' 2 these', getline('.'))
127 $
128 " last match
Bram Moolenaar11956692016-08-27 16:26:56 +0200129 call feedkeys("?the".repeat("\<C-T>", 8)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200130 call assert_equal(' 2 these', getline('.'))
131
132 " Test 5
Bram Moolenaar11956692016-08-27 16:26:56 +0200133 " CTRL-T goes to the previous match
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200134 set incsearch wrapscan
135 $
136 " first match
137 call feedkeys("?the\<cr>", 'tx')
138 call assert_equal(' 9 these', getline('.'))
139 $
140 " first match at the top
Bram Moolenaar11956692016-08-27 16:26:56 +0200141 call feedkeys("?the\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200142 call assert_equal(' 2 these', getline('.'))
143 $
144 " second match
Bram Moolenaar11956692016-08-27 16:26:56 +0200145 call feedkeys("?the".repeat("\<C-T>", 1)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200146 call assert_equal(' 8 them', getline('.'))
147 $
148 " last match
Bram Moolenaar11956692016-08-27 16:26:56 +0200149 call feedkeys("?the".repeat("\<C-T>", 7)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200150 call assert_equal(' 2 these', getline('.'))
151 $
152 " back at the bottom of the buffer
Bram Moolenaar11956692016-08-27 16:26:56 +0200153 call feedkeys("?the".repeat("\<C-T>", 8)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200154 call assert_equal(' 9 these', getline('.'))
155
156 " Test 6
157 " CTRL-L adds to the search pattern
158 set incsearch wrapscan
159 1
160 " first match
161 call feedkeys("/the\<c-l>\<cr>", 'tx')
162 call assert_equal(' 2 these', getline('.'))
163 1
164 " go to next match of 'thes'
Bram Moolenaar11956692016-08-27 16:26:56 +0200165 call feedkeys("/the\<c-l>\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200166 call assert_equal(' 9 these', getline('.'))
167 1
168 " wrap around
Bram Moolenaar11956692016-08-27 16:26:56 +0200169 call feedkeys("/the\<c-l>\<C-G>\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200170 call assert_equal(' 2 these', getline('.'))
171 1
172 " wrap around
173 set nowrapscan
Bram Moolenaar11956692016-08-27 16:26:56 +0200174 call feedkeys("/the\<c-l>\<C-G>\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200175 call assert_equal(' 9 these', getline('.'))
176
177 " Test 7
178 " <bs> remove from match, but stay at current match
179 set incsearch wrapscan
180 1
181 " first match
182 call feedkeys("/thei\<cr>", 'tx')
183 call assert_equal(' 4 their', getline('.'))
184 1
185 " delete one char, add another
186 call feedkeys("/thei\<bs>s\<cr>", 'tx')
Bram Moolenaardda933d2016-09-03 21:04:58 +0200187 call assert_equal(' 2 these', getline('.'))
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200188 1
189 " delete one char, add another, go to previous match, add one char
Bram Moolenaar11956692016-08-27 16:26:56 +0200190 call feedkeys("/thei\<bs>s\<bs>\<C-T>\<c-l>\<cr>", 'tx')
Bram Moolenaardda933d2016-09-03 21:04:58 +0200191 call assert_equal(' 9 these', getline('.'))
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200192 1
193 " delete all chars, start from the beginning again
194 call feedkeys("/them". repeat("\<bs>",4).'the\>'."\<cr>", 'tx')
195 call assert_equal(' 3 the', getline('.'))
196
197 " clean up
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100198 call test_override("char_avail", 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200199 bw!
200endfunc
201
202func Test_search_cmdline2()
203 if !exists('+incsearch')
204 return
205 endif
206 " need to disable char_avail,
207 " so that expansion of commandline works
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100208 call test_override("char_avail", 1)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200209 new
210 call setline(1, [' 1', ' 2 these', ' 3 the theother'])
211 " Test 1
Bram Moolenaar11956692016-08-27 16:26:56 +0200212 " Ctrl-T goes correctly back and forth
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200213 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)
Bram Moolenaar11956692016-08-27 16:26:56 +0200220 call feedkeys("/the\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200221 call assert_equal(' 3 the theother', getline('.'))
222 1
223 " go to next match (still on line 3)
Bram Moolenaar11956692016-08-27 16:26:56 +0200224 call feedkeys("/the\<C-G>\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200225 call assert_equal(' 3 the theother', getline('.'))
226 1
227 " go to next match (still on line 3)
Bram Moolenaar11956692016-08-27 16:26:56 +0200228 call feedkeys("/the\<C-G>\<C-G>\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200229 call assert_equal(' 3 the theother', getline('.'))
230 1
231 " go to previous match (on line 3)
Bram Moolenaar11956692016-08-27 16:26:56 +0200232 call feedkeys("/the\<C-G>\<C-G>\<C-G>\<C-T>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200233 call assert_equal(' 3 the theother', getline('.'))
234 1
235 " go to previous match (on line 3)
Bram Moolenaar11956692016-08-27 16:26:56 +0200236 call feedkeys("/the\<C-G>\<C-G>\<C-G>\<C-T>\<C-T>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200237 call assert_equal(' 3 the theother', getline('.'))
238 1
239 " go to previous match (on line 2)
Bram Moolenaar11956692016-08-27 16:26:56 +0200240 call feedkeys("/the\<C-G>\<C-G>\<C-G>\<C-T>\<C-T>\<C-T>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200241 call assert_equal(' 2 these', getline('.'))
242
Bram Moolenaardda933d2016-09-03 21:04:58 +0200243 " Test 2: keep the view,
244 " after deleting a character from the search cmd
245 call setline(1, [' 1', ' 2 these', ' 3 the', ' 4 their', ' 5 there', ' 6 their', ' 7 the', ' 8 them', ' 9 these', ' 10 foobar'])
246 resize 5
247 1
248 call feedkeys("/foo\<bs>\<cr>", 'tx')
249 redraw
250 call assert_equal({'lnum': 10, 'leftcol': 0, 'col': 4, 'topfill': 0, 'topline': 6, 'coladd': 0, 'skipcol': 0, 'curswant': 4}, winsaveview())
251
252 " remove all history entries
253 for i in range(10)
254 call histdel('/')
255 endfor
256
257 " Test 3: reset the view,
258 " after deleting all characters from the search cmd
259 norm! 1gg0
260 " unfortunately, neither "/foo\<c-w>\<cr>", nor "/foo\<bs>\<bs>\<bs>\<cr>",
261 " nor "/foo\<c-u>\<cr>" works to delete the commandline.
262 " In that case Vim should return "E35 no previous regular expression",
263 " but it looks like Vim still sees /foo and therefore the test fails.
264 " Therefore, disableing this test
265 "call assert_fails(feedkeys("/foo\<c-w>\<cr>", 'tx'), 'E35')
266 "call assert_equal({'lnum': 1, 'leftcol': 0, 'col': 0, 'topfill': 0, 'topline': 1, 'coladd': 0, 'skipcol': 0, 'curswant': 0}, winsaveview())
267
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200268 " clean up
Bram Moolenaardda933d2016-09-03 21:04:58 +0200269 set noincsearch
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100270 call test_override("char_avail", 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200271 bw!
272endfunc
Bram Moolenaarea683da2016-09-09 21:41:34 +0200273
274func Test_use_sub_pat()
275 split
276 let @/ = ''
277 func X()
278 s/^/a/
279 /
280 endfunc
281 call X()
282 bwipe!
283endfunc
Bram Moolenaar6e450a52017-01-06 20:03:58 +0100284
285func Test_searchpair()
286 new
287 call setline(1, ['other code here', '', '[', '" cursor here', ']'])
288 4
289 let a=searchpair('\[','',']','bW')
290 call assert_equal(3, a)
291 set nomagic
292 4
293 let a=searchpair('\[','',']','bW')
294 call assert_equal(3, a)
295 set magic
296 q!
297endfunc
298
Bram Moolenaar48570482017-10-30 21:48:41 +0100299func Test_searchpair_skip()
300 func Zero()
301 return 0
302 endfunc
303 func Partial(x)
304 return a:x
305 endfunc
306 new
307 call setline(1, ['{', 'foo', 'foo', 'foo', '}'])
308 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', ''))
309 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', '0'))
310 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', {-> 0}))
311 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', function('Zero')))
312 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', function('Partial', [0])))
313 " invalid argument
314 3 | call assert_equal(0, searchpair('{', '', '}', 'bWn', 0))
315 bw!
316endfunc
317
Bram Moolenaar66727e12017-03-01 22:17:05 +0100318func Test_searchc()
319 " These commands used to cause memory overflow in searchc().
320 new
321 norm ixx
322 exe "norm 0t\u93cf"
323 bw!
324endfunc
Bram Moolenaara693d052017-06-29 22:23:06 +0200325
326func Test_search_cmdline3()
327 if !exists('+incsearch')
328 return
329 endif
330 " need to disable char_avail,
331 " so that expansion of commandline works
332 call test_override("char_avail", 1)
333 new
334 call setline(1, [' 1', ' 2 the~e', ' 3 the theother'])
335 set incsearch
336 1
337 " first match
338 call feedkeys("/the\<c-l>\<cr>", 'tx')
339 call assert_equal(' 2 the~e', getline('.'))
340 " clean up
341 set noincsearch
342 call test_override("char_avail", 0)
343 bw!
344endfunc
Bram Moolenaarda5116d2017-07-01 23:11:17 +0200345
346func Test_search_cmdline4()
347 if !exists('+incsearch')
348 return
349 endif
350 " need to disable char_avail,
351 " so that expansion of commandline works
352 call test_override("char_avail", 1)
353 new
354 call setline(1, [' 1 the first', ' 2 the second', ' 3 the third'])
355 set incsearch
356 $
357 call feedkeys("?the\<c-g>\<cr>", 'tx')
358 call assert_equal(' 3 the third', getline('.'))
359 $
360 call feedkeys("?the\<c-g>\<c-g>\<cr>", 'tx')
361 call assert_equal(' 1 the first', getline('.'))
362 $
363 call feedkeys("?the\<c-g>\<c-g>\<c-g>\<cr>", 'tx')
364 call assert_equal(' 2 the second', getline('.'))
365 $
366 call feedkeys("?the\<c-t>\<cr>", 'tx')
367 call assert_equal(' 1 the first', getline('.'))
368 $
369 call feedkeys("?the\<c-t>\<c-t>\<cr>", 'tx')
370 call assert_equal(' 3 the third', getline('.'))
371 $
372 call feedkeys("?the\<c-t>\<c-t>\<c-t>\<cr>", 'tx')
373 call assert_equal(' 2 the second', getline('.'))
374 " clean up
375 set noincsearch
376 call test_override("char_avail", 0)
377 bw!
378endfunc
Bram Moolenaardb510072017-09-28 21:52:17 +0200379
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200380func Test_search_cmdline5()
381 if !exists('+incsearch')
382 return
383 endif
384 " Do not call test_override("char_avail", 1) so that <C-g> and <C-t> work
385 " regardless char_avail.
386 new
387 call setline(1, [' 1 the first', ' 2 the second', ' 3 the third'])
388 set incsearch
389 1
390 call feedkeys("/the\<c-g>\<c-g>\<cr>", 'tx')
391 call assert_equal(' 3 the third', getline('.'))
392 $
393 call feedkeys("?the\<c-t>\<c-t>\<c-t>\<cr>", 'tx')
394 call assert_equal(' 2 the second', getline('.'))
395 " clean up
396 set noincsearch
397 bw!
398endfunc
399
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100400func 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!
438endfunc
439
440func 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('.'))
Bram Moolenaard0480092017-11-16 22:20:39 +0100464
465 " using an offset uses the last search pattern
466 call cursor(1, 1)
467 call setline(1, ['1 bbvimb', ' 2 bbvimb'])
468 let @/ = 'b'
469 call feedkeys("//e\<c-g>\<cr>", 'tx')
470 call assert_equal('1 bbvimb', getline('.'))
471 call assert_equal(4, col('.'))
472
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100473 set noincsearch
474 call test_override("char_avail", 0)
475 bw!
476endfunc
477
478func Test_search_cmdline8()
479 " Highlighting is cleared in all windows
480 " since hls applies to all windows
481 if !exists('+incsearch') || !has('terminal') || has('gui_running') || winwidth(0) < 30
482 return
483 endif
484 if has("win32")
485 throw "Skipped: Bug with sending <ESC> to terminal window not fixed yet"
486 endif
487 let h = winheight(0)
488 if h < 3
489 return
490 endif
491 " Prepare buffer text
492 let lines = ['abb vim vim vi', 'vimvivim']
493 call writefile(lines, 'Xsearch.txt')
Bram Moolenaar13deab82017-11-04 18:48:43 +0100494 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', 'Xsearch.txt'], {'term_rows': 3})
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100495
Bram Moolenaar13deab82017-11-04 18:48:43 +0100496 call WaitFor({-> lines == [term_getline(buf, 1), term_getline(buf, 2)] })
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100497
Bram Moolenaar13deab82017-11-04 18:48:43 +0100498 call term_sendkeys(buf, ":set incsearch hlsearch\<cr>")
499 call term_sendkeys(buf, ":14vsp\<cr>")
500 call term_sendkeys(buf, "/vim\<cr>")
501 call term_sendkeys(buf, "/b\<esc>")
502 call term_sendkeys(buf, "gg0")
503 call term_wait(buf, 500)
504 let screen_line = term_scrape(buf, 1)
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100505 let [a0,a1,a2,a3] = [screen_line[3].attr, screen_line[4].attr,
506 \ screen_line[18].attr, screen_line[19].attr]
507 call assert_notequal(a0, a1)
508 call assert_notequal(a0, a3)
509 call assert_notequal(a1, a2)
510 call assert_equal(a0, a2)
511 call assert_equal(a1, a3)
512 " clean up
513 call delete('Xsearch.txt')
514
515 bwipe!
516endfunc
517
Bram Moolenaardb510072017-09-28 21:52:17 +0200518" Tests for regexp with various magic settings
519func Test_search_regexp()
520 enew!
521
522 put ='1 a aa abb abbccc'
523 exe 'normal! /a*b\{2}c\+/e' . "\<CR>"
524 call assert_equal([0, 2, 17, 0], getpos('.'))
525
526 put ='2 d dd dee deefff'
527 exe 'normal! /\Md\*e\{2}f\+/e' . "\<CR>"
528 call assert_equal([0, 3, 17, 0], getpos('.'))
529
530 set nomagic
531 put ='3 g gg ghh ghhiii'
532 exe 'normal! /g\*h\{2}i\+/e' . "\<CR>"
533 call assert_equal([0, 4, 17, 0], getpos('.'))
534
535 put ='4 j jj jkk jkklll'
536 exe 'normal! /\mj*k\{2}l\+/e' . "\<CR>"
537 call assert_equal([0, 5, 17, 0], getpos('.'))
538
539 put ='5 m mm mnn mnnooo'
540 exe 'normal! /\vm*n{2}o+/e' . "\<CR>"
541 call assert_equal([0, 6, 17, 0], getpos('.'))
542
543 put ='6 x ^aa$ x'
544 exe 'normal! /\V^aa$' . "\<CR>"
545 call assert_equal([0, 7, 5, 0], getpos('.'))
546
547 set magic
548 put ='7 (a)(b) abbaa'
549 exe 'normal! /\v(a)(b)\2\1\1/e' . "\<CR>"
550 call assert_equal([0, 8, 14, 0], getpos('.'))
551
552 put ='8 axx [ab]xx'
553 exe 'normal! /\V[ab]\(\[xy]\)\1' . "\<CR>"
554 call assert_equal([0, 9, 7, 0], getpos('.'))
555
556 set undolevels=100
557 put ='9 foobar'
558 put =''
559 exe "normal! a\<C-G>u\<Esc>"
560 normal G
561 exe 'normal! dv?bar?' . "\<CR>"
562 call assert_equal('9 foo', getline('.'))
563 call assert_equal([0, 10, 5, 0], getpos('.'))
564 call assert_equal(10, line('$'))
565 normal u
566 call assert_equal('9 foobar', getline('.'))
567 call assert_equal([0, 10, 6, 0], getpos('.'))
568 call assert_equal(11, line('$'))
569
570 set undolevels&
571 enew!
572endfunc
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100573
574func Test_search_cmdline_incsearch_highlight()
575 if !exists('+incsearch')
576 return
577 endif
578 set incsearch hlsearch
579 " need to disable char_avail,
580 " so that expansion of commandline works
581 call test_override("char_avail", 1)
582 new
583 call setline(1, ['aaa 1 the first', ' 2 the second', ' 3 the third'])
584
585 1
586 call feedkeys("/second\<cr>", 'tx')
587 call assert_equal('second', @/)
588 call assert_equal(' 2 the second', getline('.'))
589
590 " Canceling search won't change @/
591 1
592 let @/ = 'last pattern'
593 call feedkeys("/third\<C-c>", 'tx')
594 call assert_equal('last pattern', @/)
595 call feedkeys("/third\<Esc>", 'tx')
596 call assert_equal('last pattern', @/)
597 call feedkeys("/3\<bs>\<bs>", 'tx')
598 call assert_equal('last pattern', @/)
599 call feedkeys("/third\<c-g>\<c-t>\<Esc>", 'tx')
600 call assert_equal('last pattern', @/)
601
602 " clean up
603 set noincsearch nohlsearch
604 bw!
605endfunc
606
607func Test_search_cmdline_incsearch_highlight_attr()
608 if !exists('+incsearch') || !has('terminal') || has('gui_running')
609 return
610 endif
611 let h = winheight(0)
612 if h < 3
613 return
614 endif
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100615
616 " Prepare buffer text
Bram Moolenaar13deab82017-11-04 18:48:43 +0100617 let lines = ['abb vim vim vi', 'vimvivim']
618 call writefile(lines, 'Xsearch.txt')
619 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', 'Xsearch.txt'], {'term_rows': 3})
620
621 call WaitFor({-> lines == [term_getline(buf, 1), term_getline(buf, 2)] })
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100622
623 " Get attr of normal(a0), incsearch(a1), hlsearch(a2) highlight
Bram Moolenaar13deab82017-11-04 18:48:43 +0100624 call term_sendkeys(buf, ":set incsearch hlsearch\<cr>")
625 call term_sendkeys(buf, '/b')
626 call term_wait(buf, 200)
627 let screen_line1 = term_scrape(buf, 1)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100628 call assert_true(len(screen_line1) > 2)
629 " a0: attr_normal
630 let a0 = screen_line1[0].attr
631 " a1: attr_incsearch
632 let a1 = screen_line1[1].attr
633 " a2: attr_hlsearch
634 let a2 = screen_line1[2].attr
635 call assert_notequal(a0, a1)
636 call assert_notequal(a0, a2)
637 call assert_notequal(a1, a2)
Bram Moolenaar13deab82017-11-04 18:48:43 +0100638 call term_sendkeys(buf, "\<cr>gg0")
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100639
640 " Test incremental highlight search
Bram Moolenaar13deab82017-11-04 18:48:43 +0100641 call term_sendkeys(buf, "/vim")
642 call term_wait(buf, 200)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100643 " Buffer:
644 " abb vim vim vi
645 " vimvivim
646 " Search: /vim
647 let attr_line1 = [a0,a0,a0,a0,a1,a1,a1,a0,a2,a2,a2,a0,a0,a0]
648 let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100649 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
650 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100651
652 " Test <C-g>
Bram Moolenaar13deab82017-11-04 18:48:43 +0100653 call term_sendkeys(buf, "\<C-g>\<C-g>")
654 call term_wait(buf, 200)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100655 let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a2,a2,a2,a0,a0,a0]
656 let attr_line2 = [a1,a1,a1,a0,a0,a2,a2,a2]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100657 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
658 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100659
660 " Test <C-t>
Bram Moolenaar13deab82017-11-04 18:48:43 +0100661 call term_sendkeys(buf, "\<C-t>")
662 call term_wait(buf, 200)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100663 let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a1,a1,a1,a0,a0,a0]
664 let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100665 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
666 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100667
668 " Type Enter and a1(incsearch highlight) should become a2(hlsearch highlight)
Bram Moolenaar13deab82017-11-04 18:48:43 +0100669 call term_sendkeys(buf, "\<cr>")
670 call term_wait(buf, 200)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100671 let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a2,a2,a2,a0,a0,a0]
672 let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100673 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
674 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100675
676 " Test nohlsearch. a2(hlsearch highlight) should become a0(normal highlight)
Bram Moolenaar13deab82017-11-04 18:48:43 +0100677 call term_sendkeys(buf, ":1\<cr>")
678 call term_sendkeys(buf, ":set nohlsearch\<cr>")
679 call term_sendkeys(buf, "/vim")
680 call term_wait(buf, 200)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100681 let attr_line1 = [a0,a0,a0,a0,a1,a1,a1,a0,a0,a0,a0,a0,a0,a0]
682 let attr_line2 = [a0,a0,a0,a0,a0,a0,a0,a0]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100683 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
684 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100685 call delete('Xsearch.txt')
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100686
Bram Moolenaarb94340c2017-11-02 16:16:31 +0100687 call delete('Xsearch.txt')
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100688 bwipe!
689endfunc
Bram Moolenaarf45938c2017-11-02 15:59:57 +0100690
691func Test_search_undefined_behaviour()
692 if !has("terminal")
693 return
694 endif
695 let h = winheight(0)
696 if h < 3
697 return
698 endif
699 " did cause an undefined left shift
700 let g:buf = term_start([GetVimProg(), '--clean', '-e', '-s', '-c', 'call search(getline("."))', 'samples/test000'], {'term_rows': 3})
701 call assert_equal([''], getline(1, '$'))
702 call term_sendkeys(g:buf, ":qa!\<cr>")
703 bwipe!
704endfunc
Bram Moolenaar2973daa2017-11-02 23:15:40 +0100705
706func Test_search_undefined_behaviour2()
707 call search("\%UC0000000")
708endfunc
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100709
710" Test for search('multi-byte char', 'bce')
711func Test_search_multibyte()
712 if !has('multi_byte')
713 return
714 endif
715 let save_enc = &encoding
716 set encoding=utf8
717 enew!
718 call append('$', '')
719 call cursor(2, 1)
720 call assert_equal(2, search('', 'bce', line('.')))
721 enew!
722 let &encoding = save_enc
723endfunc
Bram Moolenaar890dd052017-12-16 19:59:37 +0100724
725" This was causing E874. Also causes an invalid read?
726func Test_look_behind()
727 new
728 call setline(1, '0\|\&\n\@<=')
729 call search(getline("."))
730 bwipe!
731endfunc
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100732
733func Test_search_sentence()
734 new
735 " this used to cause a crash
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100736 call assert_fails("/\\%')", 'E486')
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100737 call assert_fails("/", 'E486')
Bram Moolenaar1bd999f2017-12-19 22:25:40 +0100738 /\%'(
739 /
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +0100740endfunc