blob: 18767130599a8f393f942ee0740104dc1db63c10 [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
Bram Moolenaar9d34d902018-04-27 22:18:12 +02004source screendump.vim
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02005source check.vim
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01006
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02007func Test_search_cmdline()
8 if !exists('+incsearch')
9 return
10 endif
11 " need to disable char_avail,
12 " so that expansion of commandline works
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013 call test_override("char_avail", 1)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020014 new
15 call setline(1, [' 1', ' 2 these', ' 3 the', ' 4 their', ' 5 there', ' 6 their', ' 7 the', ' 8 them', ' 9 these', ' 10 foobar'])
16 " Test 1
17 " CTRL-N / CTRL-P skips through the previous search history
18 set noincsearch
19 :1
20 call feedkeys("/foobar\<cr>", 'tx')
21 call feedkeys("/the\<cr>",'tx')
22 call assert_equal('the', @/)
Bram Moolenaar11956692016-08-27 16:26:56 +020023 call feedkeys("/thes\<C-P>\<C-P>\<cr>",'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020024 call assert_equal('foobar', @/)
25
26 " Test 2
Bram Moolenaar11956692016-08-27 16:26:56 +020027 " Ctrl-G goes from one match to the next
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020028 " until the end of the buffer
29 set incsearch nowrapscan
30 :1
31 " first match
32 call feedkeys("/the\<cr>", 'tx')
33 call assert_equal(' 2 these', getline('.'))
34 :1
35 " second match
Bram Moolenaar11956692016-08-27 16:26:56 +020036 call feedkeys("/the\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020037 call assert_equal(' 3 the', getline('.'))
Bram Moolenaardda933d2016-09-03 21:04:58 +020038 call assert_equal([0, 0, 0, 0], getpos('"'))
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020039 :1
40 " third match
Bram Moolenaar11956692016-08-27 16:26:56 +020041 call feedkeys("/the".repeat("\<C-G>", 2)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020042 call assert_equal(' 4 their', getline('.'))
43 :1
44 " fourth match
Bram Moolenaar11956692016-08-27 16:26:56 +020045 call feedkeys("/the".repeat("\<C-G>", 3)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020046 call assert_equal(' 5 there', getline('.'))
47 :1
48 " fifth match
Bram Moolenaar11956692016-08-27 16:26:56 +020049 call feedkeys("/the".repeat("\<C-G>", 4)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020050 call assert_equal(' 6 their', getline('.'))
51 :1
52 " sixth match
Bram Moolenaar11956692016-08-27 16:26:56 +020053 call feedkeys("/the".repeat("\<C-G>", 5)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020054 call assert_equal(' 7 the', getline('.'))
55 :1
56 " seventh match
Bram Moolenaar11956692016-08-27 16:26:56 +020057 call feedkeys("/the".repeat("\<C-G>", 6)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020058 call assert_equal(' 8 them', getline('.'))
59 :1
60 " eigth match
Bram Moolenaar11956692016-08-27 16:26:56 +020061 call feedkeys("/the".repeat("\<C-G>", 7)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020062 call assert_equal(' 9 these', getline('.'))
63 :1
64 " no further match
Bram Moolenaar11956692016-08-27 16:26:56 +020065 call feedkeys("/the".repeat("\<C-G>", 8)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020066 call assert_equal(' 9 these', getline('.'))
Bram Moolenaardda933d2016-09-03 21:04:58 +020067 call assert_equal([0, 0, 0, 0], getpos('"'))
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020068
69 " Test 3
Bram Moolenaar11956692016-08-27 16:26:56 +020070 " Ctrl-G goes from one match to the next
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020071 " and continues back at the top
72 set incsearch wrapscan
73 :1
74 " first match
75 call feedkeys("/the\<cr>", 'tx')
76 call assert_equal(' 2 these', getline('.'))
77 :1
78 " second match
Bram Moolenaar11956692016-08-27 16:26:56 +020079 call feedkeys("/the\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020080 call assert_equal(' 3 the', getline('.'))
81 :1
82 " third match
Bram Moolenaar11956692016-08-27 16:26:56 +020083 call feedkeys("/the".repeat("\<C-G>", 2)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020084 call assert_equal(' 4 their', getline('.'))
85 :1
86 " fourth match
Bram Moolenaar11956692016-08-27 16:26:56 +020087 call feedkeys("/the".repeat("\<C-G>", 3)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020088 call assert_equal(' 5 there', getline('.'))
89 :1
90 " fifth match
Bram Moolenaar11956692016-08-27 16:26:56 +020091 call feedkeys("/the".repeat("\<C-G>", 4)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020092 call assert_equal(' 6 their', getline('.'))
93 :1
94 " sixth match
Bram Moolenaar11956692016-08-27 16:26:56 +020095 call feedkeys("/the".repeat("\<C-G>", 5)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020096 call assert_equal(' 7 the', getline('.'))
97 :1
98 " seventh match
Bram Moolenaar11956692016-08-27 16:26:56 +020099 call feedkeys("/the".repeat("\<C-G>", 6)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200100 call assert_equal(' 8 them', getline('.'))
101 :1
102 " eigth match
Bram Moolenaar11956692016-08-27 16:26:56 +0200103 call feedkeys("/the".repeat("\<C-G>", 7)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200104 call assert_equal(' 9 these', getline('.'))
105 :1
106 " back at first match
Bram Moolenaar11956692016-08-27 16:26:56 +0200107 call feedkeys("/the".repeat("\<C-G>", 8)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200108 call assert_equal(' 2 these', getline('.'))
109
110 " Test 4
Bram Moolenaar11956692016-08-27 16:26:56 +0200111 " CTRL-T goes to the previous match
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200112 set incsearch nowrapscan
113 $
114 " first match
115 call feedkeys("?the\<cr>", 'tx')
116 call assert_equal(' 9 these', getline('.'))
117 $
118 " first match
Bram Moolenaar11956692016-08-27 16:26:56 +0200119 call feedkeys("?the\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200120 call assert_equal(' 9 these', getline('.'))
121 $
122 " second match
Bram Moolenaar11956692016-08-27 16:26:56 +0200123 call feedkeys("?the".repeat("\<C-T>", 1)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200124 call assert_equal(' 8 them', getline('.'))
125 $
126 " last match
Bram Moolenaar11956692016-08-27 16:26:56 +0200127 call feedkeys("?the".repeat("\<C-T>", 7)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200128 call assert_equal(' 2 these', getline('.'))
129 $
130 " last match
Bram Moolenaar11956692016-08-27 16:26:56 +0200131 call feedkeys("?the".repeat("\<C-T>", 8)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200132 call assert_equal(' 2 these', getline('.'))
133
134 " Test 5
Bram Moolenaar11956692016-08-27 16:26:56 +0200135 " CTRL-T goes to the previous match
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200136 set incsearch wrapscan
137 $
138 " first match
139 call feedkeys("?the\<cr>", 'tx')
140 call assert_equal(' 9 these', getline('.'))
141 $
142 " first match at the top
Bram Moolenaar11956692016-08-27 16:26:56 +0200143 call feedkeys("?the\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200144 call assert_equal(' 2 these', getline('.'))
145 $
146 " second match
Bram Moolenaar11956692016-08-27 16:26:56 +0200147 call feedkeys("?the".repeat("\<C-T>", 1)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200148 call assert_equal(' 8 them', getline('.'))
149 $
150 " last match
Bram Moolenaar11956692016-08-27 16:26:56 +0200151 call feedkeys("?the".repeat("\<C-T>", 7)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200152 call assert_equal(' 2 these', getline('.'))
153 $
154 " back at the bottom of the buffer
Bram Moolenaar11956692016-08-27 16:26:56 +0200155 call feedkeys("?the".repeat("\<C-T>", 8)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200156 call assert_equal(' 9 these', getline('.'))
157
158 " Test 6
159 " CTRL-L adds to the search pattern
160 set incsearch wrapscan
161 1
162 " first match
163 call feedkeys("/the\<c-l>\<cr>", 'tx')
164 call assert_equal(' 2 these', getline('.'))
165 1
166 " go to next match of 'thes'
Bram Moolenaar11956692016-08-27 16:26:56 +0200167 call feedkeys("/the\<c-l>\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200168 call assert_equal(' 9 these', getline('.'))
169 1
170 " wrap around
Bram Moolenaar11956692016-08-27 16:26:56 +0200171 call feedkeys("/the\<c-l>\<C-G>\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200172 call assert_equal(' 2 these', getline('.'))
173 1
174 " wrap around
175 set nowrapscan
Bram Moolenaar11956692016-08-27 16:26:56 +0200176 call feedkeys("/the\<c-l>\<C-G>\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200177 call assert_equal(' 9 these', getline('.'))
178
179 " Test 7
180 " <bs> remove from match, but stay at current match
181 set incsearch wrapscan
182 1
183 " first match
184 call feedkeys("/thei\<cr>", 'tx')
185 call assert_equal(' 4 their', getline('.'))
186 1
187 " delete one char, add another
188 call feedkeys("/thei\<bs>s\<cr>", 'tx')
Bram Moolenaardda933d2016-09-03 21:04:58 +0200189 call assert_equal(' 2 these', getline('.'))
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200190 1
191 " delete one char, add another, go to previous match, add one char
Bram Moolenaar11956692016-08-27 16:26:56 +0200192 call feedkeys("/thei\<bs>s\<bs>\<C-T>\<c-l>\<cr>", 'tx')
Bram Moolenaardda933d2016-09-03 21:04:58 +0200193 call assert_equal(' 9 these', getline('.'))
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200194 1
195 " delete all chars, start from the beginning again
196 call feedkeys("/them". repeat("\<bs>",4).'the\>'."\<cr>", 'tx')
197 call assert_equal(' 3 the', getline('.'))
198
199 " clean up
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100200 call test_override("char_avail", 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200201 bw!
202endfunc
203
204func Test_search_cmdline2()
205 if !exists('+incsearch')
206 return
207 endif
208 " need to disable char_avail,
209 " so that expansion of commandline works
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100210 call test_override("char_avail", 1)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200211 new
212 call setline(1, [' 1', ' 2 these', ' 3 the theother'])
213 " Test 1
Bram Moolenaar11956692016-08-27 16:26:56 +0200214 " Ctrl-T goes correctly back and forth
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200215 set incsearch
216 1
217 " first match
218 call feedkeys("/the\<cr>", 'tx')
219 call assert_equal(' 2 these', getline('.'))
220 1
221 " go to next match (on next line)
Bram Moolenaar11956692016-08-27 16:26:56 +0200222 call feedkeys("/the\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200223 call assert_equal(' 3 the theother', getline('.'))
224 1
225 " go to next match (still on line 3)
Bram Moolenaar11956692016-08-27 16:26:56 +0200226 call feedkeys("/the\<C-G>\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200227 call assert_equal(' 3 the theother', getline('.'))
228 1
229 " go to next match (still on line 3)
Bram Moolenaar11956692016-08-27 16:26:56 +0200230 call feedkeys("/the\<C-G>\<C-G>\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200231 call assert_equal(' 3 the theother', getline('.'))
232 1
233 " go to previous match (on line 3)
Bram Moolenaar11956692016-08-27 16:26:56 +0200234 call feedkeys("/the\<C-G>\<C-G>\<C-G>\<C-T>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200235 call assert_equal(' 3 the theother', getline('.'))
236 1
237 " go to previous match (on line 3)
Bram Moolenaar11956692016-08-27 16:26:56 +0200238 call feedkeys("/the\<C-G>\<C-G>\<C-G>\<C-T>\<C-T>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200239 call assert_equal(' 3 the theother', getline('.'))
240 1
241 " go to previous match (on line 2)
Bram Moolenaar11956692016-08-27 16:26:56 +0200242 call feedkeys("/the\<C-G>\<C-G>\<C-G>\<C-T>\<C-T>\<C-T>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200243 call assert_equal(' 2 these', getline('.'))
Bram Moolenaar69a5b862019-07-16 21:38:51 +0200244 1
245 " go to previous match (on line 2)
246 call feedkeys("/the\<C-G>\<C-R>\<C-W>\<cr>", 'tx')
247 call assert_equal('theother', @/)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200248
Bram Moolenaardda933d2016-09-03 21:04:58 +0200249 " Test 2: keep the view,
250 " after deleting a character from the search cmd
251 call setline(1, [' 1', ' 2 these', ' 3 the', ' 4 their', ' 5 there', ' 6 their', ' 7 the', ' 8 them', ' 9 these', ' 10 foobar'])
252 resize 5
253 1
254 call feedkeys("/foo\<bs>\<cr>", 'tx')
255 redraw
256 call assert_equal({'lnum': 10, 'leftcol': 0, 'col': 4, 'topfill': 0, 'topline': 6, 'coladd': 0, 'skipcol': 0, 'curswant': 4}, winsaveview())
257
258 " remove all history entries
Bram Moolenaar69a5b862019-07-16 21:38:51 +0200259 for i in range(11)
Bram Moolenaardda933d2016-09-03 21:04:58 +0200260 call histdel('/')
261 endfor
262
263 " Test 3: reset the view,
264 " after deleting all characters from the search cmd
265 norm! 1gg0
266 " unfortunately, neither "/foo\<c-w>\<cr>", nor "/foo\<bs>\<bs>\<bs>\<cr>",
267 " nor "/foo\<c-u>\<cr>" works to delete the commandline.
268 " In that case Vim should return "E35 no previous regular expression",
269 " but it looks like Vim still sees /foo and therefore the test fails.
Bram Moolenaar1bc353b2019-09-01 14:45:28 +0200270 " Therefore, disabling this test
Bram Moolenaardda933d2016-09-03 21:04:58 +0200271 "call assert_fails(feedkeys("/foo\<c-w>\<cr>", 'tx'), 'E35')
272 "call assert_equal({'lnum': 1, 'leftcol': 0, 'col': 0, 'topfill': 0, 'topline': 1, 'coladd': 0, 'skipcol': 0, 'curswant': 0}, winsaveview())
273
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200274 " clean up
Bram Moolenaardda933d2016-09-03 21:04:58 +0200275 set noincsearch
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100276 call test_override("char_avail", 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200277 bw!
278endfunc
Bram Moolenaarea683da2016-09-09 21:41:34 +0200279
280func Test_use_sub_pat()
281 split
282 let @/ = ''
283 func X()
284 s/^/a/
285 /
286 endfunc
287 call X()
288 bwipe!
289endfunc
Bram Moolenaar6e450a52017-01-06 20:03:58 +0100290
291func Test_searchpair()
292 new
293 call setline(1, ['other code here', '', '[', '" cursor here', ']'])
294 4
Bram Moolenaar3dddb092018-06-24 19:01:59 +0200295 let a = searchpair('\[','',']','bW')
Bram Moolenaar6e450a52017-01-06 20:03:58 +0100296 call assert_equal(3, a)
297 set nomagic
298 4
Bram Moolenaar3dddb092018-06-24 19:01:59 +0200299 let a = searchpair('\[','',']','bW')
Bram Moolenaar6e450a52017-01-06 20:03:58 +0100300 call assert_equal(3, a)
301 set magic
302 q!
303endfunc
304
Bram Moolenaar3dddb092018-06-24 19:01:59 +0200305func Test_searchpair_errors()
306 call assert_fails("call searchpair([0], 'middle', 'end', 'bW', 'skip', 99, 100)", 'E730: using List as a String')
307 call assert_fails("call searchpair('start', {-> 0}, 'end', 'bW', 'skip', 99, 100)", 'E729: using Funcref as a String')
308 call assert_fails("call searchpair('start', 'middle', {'one': 1}, 'bW', 'skip', 99, 100)", 'E731: using Dictionary as a String')
309 call assert_fails("call searchpair('start', 'middle', 'end', 'flags', 'skip', 99, 100)", 'E475: Invalid argument: flags')
310 call assert_fails("call searchpair('start', 'middle', 'end', 'bW', 0, 99, 100)", 'E475: Invalid argument: 0')
311 call assert_fails("call searchpair('start', 'middle', 'end', 'bW', 'func', -99, 100)", 'E475: Invalid argument: -99')
312 call assert_fails("call searchpair('start', 'middle', 'end', 'bW', 'func', 99, -100)", 'E475: Invalid argument: -100')
313endfunc
314
Bram Moolenaar48570482017-10-30 21:48:41 +0100315func Test_searchpair_skip()
316 func Zero()
317 return 0
318 endfunc
319 func Partial(x)
320 return a:x
321 endfunc
322 new
323 call setline(1, ['{', 'foo', 'foo', 'foo', '}'])
324 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', ''))
325 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', '0'))
326 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', {-> 0}))
327 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', function('Zero')))
328 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', function('Partial', [0])))
Bram Moolenaar48570482017-10-30 21:48:41 +0100329 bw!
330endfunc
331
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200332func Test_searchpair_leak()
333 new
334 call setline(1, 'if one else another endif')
335
336 " The error in the skip expression caused memory to leak.
337 call assert_fails("call searchpair('\\<if\\>', '\\<else\\>', '\\<endif\\>', '', '\"foo\" 2')", 'E15:')
338
339 bwipe!
340endfunc
341
Bram Moolenaar66727e12017-03-01 22:17:05 +0100342func Test_searchc()
343 " These commands used to cause memory overflow in searchc().
344 new
345 norm ixx
346 exe "norm 0t\u93cf"
347 bw!
348endfunc
Bram Moolenaara693d052017-06-29 22:23:06 +0200349
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200350func Cmdline3_prep()
351 " need to disable char_avail,
352 " so that expansion of commandline works
353 call test_override("char_avail", 1)
354 new
355 call setline(1, [' 1', ' 2 the~e', ' 3 the theother'])
356 set incsearch
357endfunc
358
Bram Moolenaar976b8472018-08-12 15:49:47 +0200359func Incsearch_cleanup()
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200360 set noincsearch
361 call test_override("char_avail", 0)
362 bw!
363endfunc
364
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200365func Test_search_cmdline3()
366 if !exists('+incsearch')
367 return
368 endif
369 call Cmdline3_prep()
370 1
371 " first match
372 call feedkeys("/the\<c-l>\<cr>", 'tx')
373 call assert_equal(' 2 the~e', getline('.'))
374
Bram Moolenaar976b8472018-08-12 15:49:47 +0200375 call Incsearch_cleanup()
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200376endfunc
377
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200378func Test_search_cmdline3s()
379 if !exists('+incsearch')
380 return
381 endif
382 call Cmdline3_prep()
383 1
384 call feedkeys(":%s/the\<c-l>/xxx\<cr>", 'tx')
385 call assert_equal(' 2 xxxe', getline('.'))
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200386 undo
387 call feedkeys(":%subs/the\<c-l>/xxx\<cr>", 'tx')
388 call assert_equal(' 2 xxxe', getline('.'))
389 undo
390 call feedkeys(":%substitute/the\<c-l>/xxx\<cr>", 'tx')
391 call assert_equal(' 2 xxxe', getline('.'))
Bram Moolenaar167ae422018-08-14 21:32:21 +0200392 undo
393 call feedkeys(":%smagic/the.e/xxx\<cr>", 'tx')
394 call assert_equal(' 2 xxx', getline('.'))
395 undo
396 call assert_fails(":%snomagic/the.e/xxx\<cr>", 'E486')
397 "
398 call feedkeys(":%snomagic/the\\.e/xxx\<cr>", 'tx')
399 call assert_equal(' 2 xxx', getline('.'))
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200400
Bram Moolenaar976b8472018-08-12 15:49:47 +0200401 call Incsearch_cleanup()
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200402endfunc
403
404func Test_search_cmdline3g()
405 if !exists('+incsearch')
406 return
407 endif
408 call Cmdline3_prep()
409 1
410 call feedkeys(":g/the\<c-l>/d\<cr>", 'tx')
411 call assert_equal(' 3 the theother', getline(2))
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200412 undo
413 call feedkeys(":global/the\<c-l>/d\<cr>", 'tx')
414 call assert_equal(' 3 the theother', getline(2))
Bram Moolenaardef7b1d2018-08-13 22:54:35 +0200415 undo
416 call feedkeys(":g!/the\<c-l>/d\<cr>", 'tx')
417 call assert_equal(1, line('$'))
418 call assert_equal(' 2 the~e', getline(1))
419 undo
420 call feedkeys(":global!/the\<c-l>/d\<cr>", 'tx')
421 call assert_equal(1, line('$'))
422 call assert_equal(' 2 the~e', getline(1))
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200423
Bram Moolenaar976b8472018-08-12 15:49:47 +0200424 call Incsearch_cleanup()
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200425endfunc
426
427func Test_search_cmdline3v()
428 if !exists('+incsearch')
429 return
430 endif
431 call Cmdline3_prep()
432 1
433 call feedkeys(":v/the\<c-l>/d\<cr>", 'tx')
434 call assert_equal(1, line('$'))
435 call assert_equal(' 2 the~e', getline(1))
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200436 undo
437 call feedkeys(":vglobal/the\<c-l>/d\<cr>", 'tx')
438 call assert_equal(1, line('$'))
439 call assert_equal(' 2 the~e', getline(1))
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200440
Bram Moolenaar976b8472018-08-12 15:49:47 +0200441 call Incsearch_cleanup()
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200442endfunc
443
Bram Moolenaarda5116d2017-07-01 23:11:17 +0200444func Test_search_cmdline4()
445 if !exists('+incsearch')
446 return
447 endif
448 " need to disable char_avail,
449 " so that expansion of commandline works
450 call test_override("char_avail", 1)
451 new
452 call setline(1, [' 1 the first', ' 2 the second', ' 3 the third'])
453 set incsearch
454 $
455 call feedkeys("?the\<c-g>\<cr>", 'tx')
456 call assert_equal(' 3 the third', getline('.'))
457 $
458 call feedkeys("?the\<c-g>\<c-g>\<cr>", 'tx')
459 call assert_equal(' 1 the first', getline('.'))
460 $
461 call feedkeys("?the\<c-g>\<c-g>\<c-g>\<cr>", 'tx')
462 call assert_equal(' 2 the second', getline('.'))
463 $
464 call feedkeys("?the\<c-t>\<cr>", 'tx')
465 call assert_equal(' 1 the first', getline('.'))
466 $
467 call feedkeys("?the\<c-t>\<c-t>\<cr>", 'tx')
468 call assert_equal(' 3 the third', getline('.'))
469 $
470 call feedkeys("?the\<c-t>\<c-t>\<c-t>\<cr>", 'tx')
471 call assert_equal(' 2 the second', getline('.'))
472 " clean up
473 set noincsearch
474 call test_override("char_avail", 0)
475 bw!
476endfunc
Bram Moolenaardb510072017-09-28 21:52:17 +0200477
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200478func Test_search_cmdline5()
479 if !exists('+incsearch')
480 return
481 endif
482 " Do not call test_override("char_avail", 1) so that <C-g> and <C-t> work
483 " regardless char_avail.
484 new
Bram Moolenaar69a5b862019-07-16 21:38:51 +0200485 call setline(1, [' 1 the first', ' 2 the second', ' 3 the third', ''])
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200486 set incsearch
487 1
488 call feedkeys("/the\<c-g>\<c-g>\<cr>", 'tx')
489 call assert_equal(' 3 the third', getline('.'))
490 $
491 call feedkeys("?the\<c-t>\<c-t>\<c-t>\<cr>", 'tx')
Bram Moolenaar69a5b862019-07-16 21:38:51 +0200492 call assert_equal(' 1 the first', getline('.'))
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200493 " clean up
494 set noincsearch
495 bw!
496endfunc
497
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100498func Test_search_cmdline6()
499 " Test that consecutive matches
500 " are caught by <c-g>/<c-t>
501 if !exists('+incsearch')
502 return
503 endif
504 " need to disable char_avail,
505 " so that expansion of commandline works
506 call test_override("char_avail", 1)
507 new
508 call setline(1, [' bbvimb', ''])
509 set incsearch
510 " first match
511 norm! gg0
512 call feedkeys("/b\<cr>", 'tx')
513 call assert_equal([0,1,2,0], getpos('.'))
514 " second match
515 norm! gg0
516 call feedkeys("/b\<c-g>\<cr>", 'tx')
517 call assert_equal([0,1,3,0], getpos('.'))
518 " third match
519 norm! gg0
520 call feedkeys("/b\<c-g>\<c-g>\<cr>", 'tx')
521 call assert_equal([0,1,7,0], getpos('.'))
522 " first match again
523 norm! gg0
524 call feedkeys("/b\<c-g>\<c-g>\<c-g>\<cr>", 'tx')
525 call assert_equal([0,1,2,0], getpos('.'))
526 set nowrapscan
527 " last match
528 norm! gg0
529 call feedkeys("/b\<c-g>\<c-g>\<c-g>\<cr>", 'tx')
530 call assert_equal([0,1,7,0], getpos('.'))
531 " clean up
532 set wrapscan&vim
533 set noincsearch
534 call test_override("char_avail", 0)
535 bw!
536endfunc
537
538func Test_search_cmdline7()
539 " Test that an pressing <c-g> in an empty command line
540 " does not move the cursor
541 if !exists('+incsearch')
542 return
543 endif
544 " need to disable char_avail,
545 " so that expansion of commandline works
546 call test_override("char_avail", 1)
547 new
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200548 let @/ = 'b'
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100549 call setline(1, [' bbvimb', ''])
550 set incsearch
551 " first match
552 norm! gg0
553 " moves to next match of previous search pattern, just like /<cr>
554 call feedkeys("/\<c-g>\<cr>", 'tx')
555 call assert_equal([0,1,2,0], getpos('.'))
556 " moves to next match of previous search pattern, just like /<cr>
557 call feedkeys("/\<cr>", 'tx')
558 call assert_equal([0,1,3,0], getpos('.'))
559 " moves to next match of previous search pattern, just like /<cr>
560 call feedkeys("/\<c-t>\<cr>", 'tx')
561 call assert_equal([0,1,7,0], getpos('.'))
Bram Moolenaard0480092017-11-16 22:20:39 +0100562
563 " using an offset uses the last search pattern
564 call cursor(1, 1)
565 call setline(1, ['1 bbvimb', ' 2 bbvimb'])
566 let @/ = 'b'
567 call feedkeys("//e\<c-g>\<cr>", 'tx')
568 call assert_equal('1 bbvimb', getline('.'))
569 call assert_equal(4, col('.'))
570
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100571 set noincsearch
572 call test_override("char_avail", 0)
573 bw!
574endfunc
575
576func Test_search_cmdline8()
577 " Highlighting is cleared in all windows
578 " since hls applies to all windows
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200579 CheckOption incsearch
580 CheckFeature terminal
581 CheckNotGui
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100582 if has("win32")
583 throw "Skipped: Bug with sending <ESC> to terminal window not fixed yet"
584 endif
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200585
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100586 let h = winheight(0)
587 if h < 3
588 return
589 endif
590 " Prepare buffer text
591 let lines = ['abb vim vim vi', 'vimvivim']
592 call writefile(lines, 'Xsearch.txt')
Bram Moolenaar13deab82017-11-04 18:48:43 +0100593 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', 'Xsearch.txt'], {'term_rows': 3})
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100594
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200595 call WaitForAssert({-> assert_equal(lines, [term_getline(buf, 1), term_getline(buf, 2)])})
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100596
Bram Moolenaar13deab82017-11-04 18:48:43 +0100597 call term_sendkeys(buf, ":set incsearch hlsearch\<cr>")
598 call term_sendkeys(buf, ":14vsp\<cr>")
599 call term_sendkeys(buf, "/vim\<cr>")
600 call term_sendkeys(buf, "/b\<esc>")
601 call term_sendkeys(buf, "gg0")
602 call term_wait(buf, 500)
603 let screen_line = term_scrape(buf, 1)
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100604 let [a0,a1,a2,a3] = [screen_line[3].attr, screen_line[4].attr,
605 \ screen_line[18].attr, screen_line[19].attr]
606 call assert_notequal(a0, a1)
607 call assert_notequal(a0, a3)
608 call assert_notequal(a1, a2)
609 call assert_equal(a0, a2)
610 call assert_equal(a1, a3)
611 " clean up
612 call delete('Xsearch.txt')
613
614 bwipe!
615endfunc
616
Bram Moolenaardb510072017-09-28 21:52:17 +0200617" Tests for regexp with various magic settings
618func Test_search_regexp()
619 enew!
620
621 put ='1 a aa abb abbccc'
622 exe 'normal! /a*b\{2}c\+/e' . "\<CR>"
623 call assert_equal([0, 2, 17, 0], getpos('.'))
624
625 put ='2 d dd dee deefff'
626 exe 'normal! /\Md\*e\{2}f\+/e' . "\<CR>"
627 call assert_equal([0, 3, 17, 0], getpos('.'))
628
629 set nomagic
630 put ='3 g gg ghh ghhiii'
631 exe 'normal! /g\*h\{2}i\+/e' . "\<CR>"
632 call assert_equal([0, 4, 17, 0], getpos('.'))
633
634 put ='4 j jj jkk jkklll'
635 exe 'normal! /\mj*k\{2}l\+/e' . "\<CR>"
636 call assert_equal([0, 5, 17, 0], getpos('.'))
637
638 put ='5 m mm mnn mnnooo'
639 exe 'normal! /\vm*n{2}o+/e' . "\<CR>"
640 call assert_equal([0, 6, 17, 0], getpos('.'))
641
642 put ='6 x ^aa$ x'
643 exe 'normal! /\V^aa$' . "\<CR>"
644 call assert_equal([0, 7, 5, 0], getpos('.'))
645
646 set magic
647 put ='7 (a)(b) abbaa'
648 exe 'normal! /\v(a)(b)\2\1\1/e' . "\<CR>"
649 call assert_equal([0, 8, 14, 0], getpos('.'))
650
651 put ='8 axx [ab]xx'
652 exe 'normal! /\V[ab]\(\[xy]\)\1' . "\<CR>"
653 call assert_equal([0, 9, 7, 0], getpos('.'))
654
655 set undolevels=100
656 put ='9 foobar'
657 put =''
658 exe "normal! a\<C-G>u\<Esc>"
659 normal G
660 exe 'normal! dv?bar?' . "\<CR>"
661 call assert_equal('9 foo', getline('.'))
662 call assert_equal([0, 10, 5, 0], getpos('.'))
663 call assert_equal(10, line('$'))
664 normal u
665 call assert_equal('9 foobar', getline('.'))
666 call assert_equal([0, 10, 6, 0], getpos('.'))
667 call assert_equal(11, line('$'))
668
669 set undolevels&
670 enew!
671endfunc
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100672
673func Test_search_cmdline_incsearch_highlight()
674 if !exists('+incsearch')
675 return
676 endif
677 set incsearch hlsearch
678 " need to disable char_avail,
679 " so that expansion of commandline works
680 call test_override("char_avail", 1)
681 new
682 call setline(1, ['aaa 1 the first', ' 2 the second', ' 3 the third'])
683
684 1
685 call feedkeys("/second\<cr>", 'tx')
686 call assert_equal('second', @/)
687 call assert_equal(' 2 the second', getline('.'))
688
689 " Canceling search won't change @/
690 1
691 let @/ = 'last pattern'
692 call feedkeys("/third\<C-c>", 'tx')
693 call assert_equal('last pattern', @/)
694 call feedkeys("/third\<Esc>", 'tx')
695 call assert_equal('last pattern', @/)
696 call feedkeys("/3\<bs>\<bs>", 'tx')
697 call assert_equal('last pattern', @/)
698 call feedkeys("/third\<c-g>\<c-t>\<Esc>", 'tx')
699 call assert_equal('last pattern', @/)
700
701 " clean up
702 set noincsearch nohlsearch
703 bw!
704endfunc
705
706func Test_search_cmdline_incsearch_highlight_attr()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200707 CheckOption incsearch
708 CheckFeature terminal
709 CheckNotGui
710
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100711 let h = winheight(0)
712 if h < 3
713 return
714 endif
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100715
716 " Prepare buffer text
Bram Moolenaar13deab82017-11-04 18:48:43 +0100717 let lines = ['abb vim vim vi', 'vimvivim']
718 call writefile(lines, 'Xsearch.txt')
719 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', 'Xsearch.txt'], {'term_rows': 3})
720
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200721 call WaitForAssert({-> assert_equal(lines, [term_getline(buf, 1), term_getline(buf, 2)])})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100722 " wait for vim to complete initialization
723 call term_wait(buf)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100724
725 " Get attr of normal(a0), incsearch(a1), hlsearch(a2) highlight
Bram Moolenaar13deab82017-11-04 18:48:43 +0100726 call term_sendkeys(buf, ":set incsearch hlsearch\<cr>")
727 call term_sendkeys(buf, '/b')
728 call term_wait(buf, 200)
729 let screen_line1 = term_scrape(buf, 1)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100730 call assert_true(len(screen_line1) > 2)
731 " a0: attr_normal
732 let a0 = screen_line1[0].attr
733 " a1: attr_incsearch
734 let a1 = screen_line1[1].attr
735 " a2: attr_hlsearch
736 let a2 = screen_line1[2].attr
737 call assert_notequal(a0, a1)
738 call assert_notequal(a0, a2)
739 call assert_notequal(a1, a2)
Bram Moolenaar13deab82017-11-04 18:48:43 +0100740 call term_sendkeys(buf, "\<cr>gg0")
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100741
742 " Test incremental highlight search
Bram Moolenaar13deab82017-11-04 18:48:43 +0100743 call term_sendkeys(buf, "/vim")
744 call term_wait(buf, 200)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100745 " Buffer:
746 " abb vim vim vi
747 " vimvivim
748 " Search: /vim
749 let attr_line1 = [a0,a0,a0,a0,a1,a1,a1,a0,a2,a2,a2,a0,a0,a0]
750 let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100751 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
752 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100753
754 " Test <C-g>
Bram Moolenaar13deab82017-11-04 18:48:43 +0100755 call term_sendkeys(buf, "\<C-g>\<C-g>")
756 call term_wait(buf, 200)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100757 let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a2,a2,a2,a0,a0,a0]
758 let attr_line2 = [a1,a1,a1,a0,a0,a2,a2,a2]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100759 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
760 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100761
762 " Test <C-t>
Bram Moolenaar13deab82017-11-04 18:48:43 +0100763 call term_sendkeys(buf, "\<C-t>")
764 call term_wait(buf, 200)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100765 let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a1,a1,a1,a0,a0,a0]
766 let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100767 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
768 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100769
770 " Type Enter and a1(incsearch highlight) should become a2(hlsearch highlight)
Bram Moolenaar13deab82017-11-04 18:48:43 +0100771 call term_sendkeys(buf, "\<cr>")
772 call term_wait(buf, 200)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100773 let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a2,a2,a2,a0,a0,a0]
774 let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100775 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
776 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100777
778 " Test nohlsearch. a2(hlsearch highlight) should become a0(normal highlight)
Bram Moolenaar13deab82017-11-04 18:48:43 +0100779 call term_sendkeys(buf, ":1\<cr>")
780 call term_sendkeys(buf, ":set nohlsearch\<cr>")
781 call term_sendkeys(buf, "/vim")
782 call term_wait(buf, 200)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100783 let attr_line1 = [a0,a0,a0,a0,a1,a1,a1,a0,a0,a0,a0,a0,a0,a0]
784 let attr_line2 = [a0,a0,a0,a0,a0,a0,a0,a0]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100785 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
786 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100787 call delete('Xsearch.txt')
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100788
Bram Moolenaarb94340c2017-11-02 16:16:31 +0100789 call delete('Xsearch.txt')
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100790 bwipe!
791endfunc
Bram Moolenaarf45938c2017-11-02 15:59:57 +0100792
Bram Moolenaar548e5982018-12-26 21:45:00 +0100793func Test_incsearch_cmdline_modifier()
794 if !exists('+incsearch')
795 return
796 endif
797 call test_override("char_avail", 1)
798 new
799 call setline(1, ['foo'])
800 set incsearch
801 " Test that error E14 does not occur in parsing command modifier.
802 call feedkeys("V:tab", 'tx')
803
804 call Incsearch_cleanup()
805endfunc
806
Bram Moolenaar9d34d902018-04-27 22:18:12 +0200807func Test_incsearch_scrolling()
808 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200809 throw 'Skipped: cannot make screendumps'
Bram Moolenaar9d34d902018-04-27 22:18:12 +0200810 endif
811 call assert_equal(0, &scrolloff)
812 call writefile([
813 \ 'let dots = repeat(".", 120)',
814 \ 'set incsearch cmdheight=2 scrolloff=0',
815 \ 'call setline(1, [dots, dots, dots, "", "target", dots, dots])',
816 \ 'normal gg',
817 \ 'redraw',
818 \ ], 'Xscript')
819 let buf = RunVimInTerminal('-S Xscript', {'rows': 9, 'cols': 70})
820 " Need to send one key at a time to force a redraw
821 call term_sendkeys(buf, '/')
822 sleep 100m
823 call term_sendkeys(buf, 't')
824 sleep 100m
825 call term_sendkeys(buf, 'a')
826 sleep 100m
827 call term_sendkeys(buf, 'r')
828 sleep 100m
829 call term_sendkeys(buf, 'g')
830 call VerifyScreenDump(buf, 'Test_incsearch_scrolling_01', {})
831
832 call term_sendkeys(buf, "\<Esc>")
833 call StopVimInTerminal(buf)
834 call delete('Xscript')
835endfunc
836
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200837func Test_incsearch_search_dump()
838 if !exists('+incsearch')
839 return
840 endif
841 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200842 throw 'Skipped: cannot make screendumps'
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200843 endif
844 call writefile([
845 \ 'set incsearch hlsearch scrolloff=0',
846 \ 'for n in range(1, 8)',
847 \ ' call setline(n, "foo " . n)',
848 \ 'endfor',
849 \ '3',
850 \ ], 'Xis_search_script')
851 let buf = RunVimInTerminal('-S Xis_search_script', {'rows': 9, 'cols': 70})
852 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
853 " the 'ambiwidth' check.
854 sleep 100m
855
856 " Need to send one key at a time to force a redraw.
857 call term_sendkeys(buf, '/fo')
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200858 call VerifyScreenDump(buf, 'Test_incsearch_search_01', {})
859 call term_sendkeys(buf, "\<Esc>")
860 sleep 100m
861
862 call term_sendkeys(buf, '/\v')
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200863 call VerifyScreenDump(buf, 'Test_incsearch_search_02', {})
864 call term_sendkeys(buf, "\<Esc>")
865
866 call StopVimInTerminal(buf)
867 call delete('Xis_search_script')
868endfunc
869
Bram Moolenaar976b8472018-08-12 15:49:47 +0200870func Test_incsearch_substitute()
871 if !exists('+incsearch')
872 return
873 endif
874 call test_override("char_avail", 1)
875 new
876 set incsearch
877 for n in range(1, 10)
878 call setline(n, 'foo ' . n)
879 endfor
880 4
881 call feedkeys(":.,.+2s/foo\<BS>o\<BS>o/xxx\<cr>", 'tx')
882 call assert_equal('foo 3', getline(3))
883 call assert_equal('xxx 4', getline(4))
884 call assert_equal('xxx 5', getline(5))
885 call assert_equal('xxx 6', getline(6))
886 call assert_equal('foo 7', getline(7))
887
888 call Incsearch_cleanup()
889endfunc
890
Bram Moolenaar164251f2018-08-12 16:26:58 +0200891" Similar to Test_incsearch_substitute() but with a screendump halfway.
892func Test_incsearch_substitute_dump()
893 if !exists('+incsearch')
894 return
895 endif
896 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200897 throw 'Skipped: cannot make screendumps'
Bram Moolenaar164251f2018-08-12 16:26:58 +0200898 endif
899 call writefile([
900 \ 'set incsearch hlsearch scrolloff=0',
901 \ 'for n in range(1, 10)',
902 \ ' call setline(n, "foo " . n)',
903 \ 'endfor',
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200904 \ 'call setline(11, "bar 11")',
Bram Moolenaar164251f2018-08-12 16:26:58 +0200905 \ '3',
906 \ ], 'Xis_subst_script')
907 let buf = RunVimInTerminal('-S Xis_subst_script', {'rows': 9, 'cols': 70})
908 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
909 " the 'ambiwidth' check.
910 sleep 100m
911
912 " Need to send one key at a time to force a redraw.
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200913 " Select three lines at the cursor with typed pattern.
Bram Moolenaar164251f2018-08-12 16:26:58 +0200914 call term_sendkeys(buf, ':.,.+2s/')
915 sleep 100m
916 call term_sendkeys(buf, 'f')
917 sleep 100m
918 call term_sendkeys(buf, 'o')
919 sleep 100m
920 call term_sendkeys(buf, 'o')
921 call VerifyScreenDump(buf, 'Test_incsearch_substitute_01', {})
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200922 call term_sendkeys(buf, "\<Esc>")
923
924 " Select three lines at the cursor using previous pattern.
925 call term_sendkeys(buf, "/foo\<CR>")
926 sleep 100m
927 call term_sendkeys(buf, ':.,.+2s//')
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200928 call VerifyScreenDump(buf, 'Test_incsearch_substitute_02', {})
929
930 " Deleting last slash should remove the match.
931 call term_sendkeys(buf, "\<BS>")
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200932 call VerifyScreenDump(buf, 'Test_incsearch_substitute_03', {})
Bram Moolenaar60d08712018-08-12 21:53:15 +0200933 call term_sendkeys(buf, "\<Esc>")
934
935 " Reverse range is accepted
936 call term_sendkeys(buf, ':5,2s/foo')
Bram Moolenaar60d08712018-08-12 21:53:15 +0200937 call VerifyScreenDump(buf, 'Test_incsearch_substitute_04', {})
Bram Moolenaar164251f2018-08-12 16:26:58 +0200938 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaar2b926fc2018-08-13 11:07:57 +0200939
940 " White space after the command is skipped
941 call term_sendkeys(buf, ':2,3sub /fo')
Bram Moolenaar2b926fc2018-08-13 11:07:57 +0200942 call VerifyScreenDump(buf, 'Test_incsearch_substitute_05', {})
943 call term_sendkeys(buf, "\<Esc>")
944
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200945 " Command modifiers are skipped
946 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.')
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200947 call VerifyScreenDump(buf, 'Test_incsearch_substitute_06', {})
948 call term_sendkeys(buf, "\<Esc>")
949
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200950 " Cursorline highlighting at match
951 call term_sendkeys(buf, ":set cursorline\<CR>")
952 call term_sendkeys(buf, 'G9G')
953 call term_sendkeys(buf, ':9,11s/bar')
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200954 call VerifyScreenDump(buf, 'Test_incsearch_substitute_07', {})
955 call term_sendkeys(buf, "\<Esc>")
956
957 " Cursorline highlighting at cursor when no match
958 call term_sendkeys(buf, ':9,10s/bar')
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200959 call VerifyScreenDump(buf, 'Test_incsearch_substitute_08', {})
960 call term_sendkeys(buf, "\<Esc>")
961
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200962 " Only \v handled as empty pattern, does not move cursor
963 call term_sendkeys(buf, '3G4G')
964 call term_sendkeys(buf, ":nohlsearch\<CR>")
965 call term_sendkeys(buf, ':6,7s/\v')
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200966 call VerifyScreenDump(buf, 'Test_incsearch_substitute_09', {})
967 call term_sendkeys(buf, "\<Esc>")
968
Bram Moolenaarf13daa42018-08-31 22:09:54 +0200969 call term_sendkeys(buf, ":set nocursorline\<CR>")
970
971 " All matches are highlighted for 'hlsearch' after the incsearch canceled
972 call term_sendkeys(buf, "1G*")
973 call term_sendkeys(buf, ":2,5s/foo")
974 sleep 100m
975 call term_sendkeys(buf, "\<Esc>")
976 call VerifyScreenDump(buf, 'Test_incsearch_substitute_10', {})
977
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200978 call term_sendkeys(buf, ":split\<CR>")
979 call term_sendkeys(buf, ":let @/ = 'xyz'\<CR>")
980 call term_sendkeys(buf, ":%s/.")
981 call VerifyScreenDump(buf, 'Test_incsearch_substitute_11', {})
982 call term_sendkeys(buf, "\<BS>")
983 call VerifyScreenDump(buf, 'Test_incsearch_substitute_12', {})
984 call term_sendkeys(buf, "\<Esc>")
985 call VerifyScreenDump(buf, 'Test_incsearch_substitute_13', {})
Bram Moolenaarc6725252019-11-23 21:56:46 +0100986 call term_sendkeys(buf, ":%bwipe!\<CR>")
987 call term_sendkeys(buf, ":only!\<CR>")
988
989 " get :'<,'>s command in history
990 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
991 call term_sendkeys(buf, "aasdfasdf\<Esc>")
992 call term_sendkeys(buf, "V:s/a/b/g\<CR>")
993 " Using '<,'> does not give E20
994 call term_sendkeys(buf, ":new\<CR>")
995 call term_sendkeys(buf, "aasdfasdf\<Esc>")
996 call term_sendkeys(buf, ":\<Up>\<Up>")
997 call VerifyScreenDump(buf, 'Test_incsearch_substitute_14', {})
998 call term_sendkeys(buf, "<Esc>")
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200999
Bram Moolenaar164251f2018-08-12 16:26:58 +02001000 call StopVimInTerminal(buf)
1001 call delete('Xis_subst_script')
1002endfunc
1003
Bram Moolenaar4a7d2d32019-02-21 16:25:50 +01001004func Test_incsearch_with_change()
1005 if !has('timers') || !exists('+incsearch') || !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001006 throw 'Skipped: cannot make screendumps and/or timers feature and/or incsearch option missing'
Bram Moolenaar4a7d2d32019-02-21 16:25:50 +01001007 endif
1008
1009 call writefile([
1010 \ 'set incsearch hlsearch scrolloff=0',
1011 \ 'call setline(1, ["one", "two ------ X", "three"])',
1012 \ 'call timer_start(200, { _ -> setline(2, "x")})',
1013 \ ], 'Xis_change_script')
1014 let buf = RunVimInTerminal('-S Xis_change_script', {'rows': 9, 'cols': 70})
1015 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1016 " the 'ambiwidth' check.
Bram Moolenaare86ecbd2019-02-21 17:48:59 +01001017 sleep 300m
Bram Moolenaar4a7d2d32019-02-21 16:25:50 +01001018
1019 " Highlight X, it will be deleted by the timer callback.
1020 call term_sendkeys(buf, ':%s/X')
1021 call VerifyScreenDump(buf, 'Test_incsearch_change_01', {})
1022 call term_sendkeys(buf, "\<Esc>")
1023
1024 call StopVimInTerminal(buf)
1025 call delete('Xis_change_script')
1026endfunc
1027
Bram Moolenaar81f56532018-08-18 16:19:42 +02001028" Similar to Test_incsearch_substitute_dump() for :sort
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +02001029func Test_incsearch_sort_dump()
Bram Moolenaar81f56532018-08-18 16:19:42 +02001030 if !exists('+incsearch')
1031 return
1032 endif
1033 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001034 throw 'Skipped: cannot make screendumps'
Bram Moolenaar81f56532018-08-18 16:19:42 +02001035 endif
1036 call writefile([
1037 \ 'set incsearch hlsearch scrolloff=0',
1038 \ 'call setline(1, ["another one 2", "that one 3", "the one 1"])',
1039 \ ], 'Xis_sort_script')
1040 let buf = RunVimInTerminal('-S Xis_sort_script', {'rows': 9, 'cols': 70})
1041 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1042 " the 'ambiwidth' check.
1043 sleep 100m
1044
1045 " Need to send one key at a time to force a redraw.
1046 call term_sendkeys(buf, ':sort ni u /on')
Bram Moolenaar81f56532018-08-18 16:19:42 +02001047 call VerifyScreenDump(buf, 'Test_incsearch_sort_01', {})
1048 call term_sendkeys(buf, "\<Esc>")
1049
1050 call StopVimInTerminal(buf)
1051 call delete('Xis_sort_script')
1052endfunc
1053
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001054" Similar to Test_incsearch_substitute_dump() for :vimgrep famiry
1055func Test_incsearch_vimgrep_dump()
1056 if !exists('+incsearch')
1057 return
1058 endif
1059 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001060 throw 'Skipped: cannot make screendumps'
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001061 endif
1062 call writefile([
1063 \ 'set incsearch hlsearch scrolloff=0',
1064 \ 'call setline(1, ["another one 2", "that one 3", "the one 1"])',
1065 \ ], 'Xis_vimgrep_script')
1066 let buf = RunVimInTerminal('-S Xis_vimgrep_script', {'rows': 9, 'cols': 70})
1067 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1068 " the 'ambiwidth' check.
1069 sleep 100m
1070
1071 " Need to send one key at a time to force a redraw.
1072 call term_sendkeys(buf, ':vimgrep on')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001073 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_01', {})
1074 call term_sendkeys(buf, "\<Esc>")
1075
1076 call term_sendkeys(buf, ':vimg /on/ *.txt')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001077 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_02', {})
1078 call term_sendkeys(buf, "\<Esc>")
1079
1080 call term_sendkeys(buf, ':vimgrepadd "\<on')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001081 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_03', {})
1082 call term_sendkeys(buf, "\<Esc>")
1083
1084 call term_sendkeys(buf, ':lv "tha')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001085 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_04', {})
1086 call term_sendkeys(buf, "\<Esc>")
1087
1088 call term_sendkeys(buf, ':lvimgrepa "the" **/*.txt')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001089 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_05', {})
1090 call term_sendkeys(buf, "\<Esc>")
1091
1092 call StopVimInTerminal(buf)
1093 call delete('Xis_vimgrep_script')
1094endfunc
1095
Bram Moolenaar198cb662018-09-06 21:44:17 +02001096func Test_keep_last_search_pattern()
1097 if !exists('+incsearch')
1098 return
1099 endif
1100 new
1101 call setline(1, ['foo', 'foo', 'foo'])
1102 set incsearch
1103 call test_override("char_avail", 1)
1104 let @/ = 'bar'
1105 call feedkeys(":/foo/s//\<Esc>", 'ntx')
1106 call assert_equal('bar', @/)
1107
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001108 " no error message if pattern not found
1109 call feedkeys(":/xyz/s//\<Esc>", 'ntx')
1110 call assert_equal('bar', @/)
1111
Bram Moolenaar198cb662018-09-06 21:44:17 +02001112 bwipe!
1113 call test_override("ALL", 0)
1114 set noincsearch
1115endfunc
1116
Bram Moolenaar99f043a2018-09-09 15:54:14 +02001117func Test_word_under_cursor_after_match()
1118 if !exists('+incsearch')
1119 return
1120 endif
1121 new
1122 call setline(1, 'foo bar')
1123 set incsearch
1124 call test_override("char_avail", 1)
1125 try
1126 call feedkeys("/foo\<C-R>\<C-W>\<CR>", 'ntx')
1127 catch /E486:/
1128 endtry
1129 call assert_equal('foobar', @/)
1130
1131 bwipe!
1132 call test_override("ALL", 0)
1133 set noincsearch
1134endfunc
1135
1136func Test_subst_word_under_cursor()
1137 if !exists('+incsearch')
1138 return
1139 endif
1140 new
1141 call setline(1, ['int SomeLongName;', 'for (xxx = 1; xxx < len; ++xxx)'])
1142 set incsearch
1143 call test_override("char_avail", 1)
1144 call feedkeys("/LongName\<CR>", 'ntx')
1145 call feedkeys(":%s/xxx/\<C-R>\<C-W>/g\<CR>", 'ntx')
1146 call assert_equal('for (SomeLongName = 1; SomeLongName < len; ++SomeLongName)', getline(2))
1147
1148 bwipe!
1149 call test_override("ALL", 0)
1150 set noincsearch
1151endfunc
1152
Bram Moolenaarf45938c2017-11-02 15:59:57 +01001153func Test_search_undefined_behaviour()
1154 if !has("terminal")
1155 return
1156 endif
1157 let h = winheight(0)
1158 if h < 3
1159 return
1160 endif
1161 " did cause an undefined left shift
1162 let g:buf = term_start([GetVimProg(), '--clean', '-e', '-s', '-c', 'call search(getline("."))', 'samples/test000'], {'term_rows': 3})
1163 call assert_equal([''], getline(1, '$'))
1164 call term_sendkeys(g:buf, ":qa!\<cr>")
1165 bwipe!
1166endfunc
Bram Moolenaar2973daa2017-11-02 23:15:40 +01001167
1168func Test_search_undefined_behaviour2()
1169 call search("\%UC0000000")
1170endfunc
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001171
1172" Test for search('multi-byte char', 'bce')
1173func Test_search_multibyte()
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001174 let save_enc = &encoding
1175 set encoding=utf8
1176 enew!
1177 call append('$', 'A')
1178 call cursor(2, 1)
1179 call assert_equal(2, search('A', 'bce', line('.')))
1180 enew!
1181 let &encoding = save_enc
1182endfunc
Bram Moolenaar890dd052017-12-16 19:59:37 +01001183
1184" This was causing E874. Also causes an invalid read?
1185func Test_look_behind()
1186 new
1187 call setline(1, '0\|\&\n\@<=')
1188 call search(getline("."))
1189 bwipe!
1190endfunc
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01001191
1192func Test_search_sentence()
1193 new
1194 " this used to cause a crash
Bram Moolenaar1bd999f2017-12-19 22:25:40 +01001195 call assert_fails("/\\%')", 'E486')
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01001196 call assert_fails("/", 'E486')
Bram Moolenaar1bd999f2017-12-19 22:25:40 +01001197 /\%'(
1198 /
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01001199endfunc
Bram Moolenaar2fb8f682018-12-01 13:14:45 +01001200
1201" Test that there is no crash when there is a last search pattern but no last
1202" substitute pattern.
1203func Test_no_last_substitute_pat()
1204 " Use viminfo to set the last search pattern to a string and make the last
1205 " substitute pattern the most recent used and make it empty (NULL).
1206 call writefile(['~MSle0/bar', '~MSle0~&'], 'Xviminfo')
1207 rviminfo! Xviminfo
1208 call assert_fails('normal n', 'E35:')
1209
1210 call delete('Xviminfo')
1211endfunc
Bram Moolenaar5f5e2032018-12-14 15:47:03 +01001212
1213func Test_search_Ctrl_L_combining()
1214 " Make sure, that Ctrl-L works correctly with combining characters.
1215 " It uses an artificial example of an 'a' with 4 combining chars:
1216 " 'a' U+0061 Dec:97 LATIN SMALL LETTER A &#x61; /\%u61\Z "\u0061"
1217 " ' ̀' U+0300 Dec:768 COMBINING GRAVE ACCENT &#x300; /\%u300\Z "\u0300"
1218 " ' ́' U+0301 Dec:769 COMBINING ACUTE ACCENT &#x301; /\%u301\Z "\u0301"
1219 " ' ̇' U+0307 Dec:775 COMBINING DOT ABOVE &#x307; /\%u307\Z "\u0307"
1220 " ' ̣' U+0323 Dec:803 COMBINING DOT BELOW &#x323; /\%u323 "\u0323"
1221 " Those should also appear on the commandline
Bram Moolenaar30276f22019-01-24 17:59:39 +01001222 if !exists('+incsearch')
Bram Moolenaar5f5e2032018-12-14 15:47:03 +01001223 return
1224 endif
1225 call Cmdline3_prep()
1226 1
1227 let bufcontent = ['', 'Miạ̀́̇m']
1228 call append('$', bufcontent)
1229 call feedkeys("/Mi\<c-l>\<c-l>\<cr>", 'tx')
1230 call assert_equal(5, line('.'))
1231 call assert_equal(bufcontent[1], @/)
1232 call Incsearch_cleanup()
1233endfunc
Bram Moolenaar527a2d82019-02-21 22:28:51 +01001234
Bram Moolenaarab350f82019-02-28 06:25:00 +01001235func Test_large_hex_chars1()
Bram Moolenaar527a2d82019-02-21 22:28:51 +01001236 " This used to cause a crash, the character becomes an NFA state.
1237 try
1238 /\%Ufffffc23
1239 catch
1240 call assert_match('E678:', v:exception)
1241 endtry
Bram Moolenaarab350f82019-02-28 06:25:00 +01001242 try
1243 set re=1
1244 /\%Ufffffc23
1245 catch
1246 call assert_match('E678:', v:exception)
1247 endtry
1248 set re&
1249endfunc
1250
1251func Test_large_hex_chars2()
1252 " This used to cause a crash, the character becomes an NFA state.
1253 try
1254 /[\Ufffffc1f]
1255 catch
1256 call assert_match('E486:', v:exception)
1257 endtry
1258 try
1259 set re=1
1260 /[\Ufffffc1f]
1261 catch
1262 call assert_match('E486:', v:exception)
1263 endtry
1264 set re&
Bram Moolenaar527a2d82019-02-21 22:28:51 +01001265endfunc
Bram Moolenaarcd625122019-02-22 17:29:43 +01001266
1267func Test_one_error_msg()
1268 " This was also giving an internal error
1269 call assert_fails('call search(" \\((\\v[[=P=]]){185}+ ")', 'E871:')
1270endfunc
Bram Moolenaar730f48f2019-04-11 13:45:57 +02001271
1272func Test_incsearch_add_char_under_cursor()
1273 if !exists('+incsearch')
1274 return
1275 endif
1276 set incsearch
1277 new
1278 call setline(1, ['find match', 'anything'])
1279 1
1280 call test_override('char_avail', 1)
1281 call feedkeys("fc/m\<C-L>\<C-L>\<C-L>\<C-L>\<C-L>\<CR>", 'tx')
1282 call assert_equal('match', @/)
1283 call test_override('char_avail', 0)
1284
1285 set incsearch&
1286 bwipe!
1287endfunc
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02001288
1289" Test for the search() function with match at the cursor position
1290func Test_search_match_at_curpos()
1291 new
1292 call append(0, ['foobar', '', 'one two', ''])
1293
1294 normal gg
1295
Bram Moolenaar196b4662019-09-06 21:34:30 +02001296 eval 'foobar'->search('c')
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02001297 call assert_equal([1, 1], [line('.'), col('.')])
1298
1299 normal j
1300 call search('^$', 'c')
1301 call assert_equal([2, 1], [line('.'), col('.')])
1302
1303 call search('^$', 'bc')
1304 call assert_equal([2, 1], [line('.'), col('.')])
1305
1306 exe "normal /two\<CR>"
1307 call search('.', 'c')
1308 call assert_equal([3, 5], [line('.'), col('.')])
1309
1310 close!
1311endfunc
Bram Moolenaardb294ad2019-06-06 12:49:29 +02001312
1313func Test_search_display_pattern()
1314 new
1315 call setline(1, ['foo', 'bar', 'foobar'])
1316
1317 call cursor(1, 1)
1318 let @/ = 'foo'
Bram Moolenaara4208962019-08-24 20:50:19 +02001319 let pat = @/->escape('()*?'. '\s\+')
Bram Moolenaardb294ad2019-06-06 12:49:29 +02001320 let g:a = execute(':unsilent :norm! n')
1321 call assert_match(pat, g:a)
1322
1323 " right-left
1324 if exists("+rightleft")
1325 set rl
1326 call cursor(1, 1)
1327 let @/ = 'foo'
1328 let pat = 'oof/\s\+'
1329 let g:a = execute(':unsilent :norm! n')
1330 call assert_match(pat, g:a)
1331 set norl
1332 endif
1333endfunc
Bram Moolenaar196b4662019-09-06 21:34:30 +02001334
1335func Test_searchdecl()
1336 let lines =<< trim END
1337 int global;
1338
1339 func()
1340 {
1341 int global;
1342 if (cond) {
1343 int local;
1344 }
1345 int local;
1346 // comment
1347 }
1348 END
1349 new
1350 call setline(1, lines)
1351 10
1352 call assert_equal(0, searchdecl('local', 0, 0))
1353 call assert_equal(7, getcurpos()[1])
1354
1355 10
1356 call assert_equal(0, 'local'->searchdecl(0, 1))
1357 call assert_equal(9, getcurpos()[1])
1358
1359 10
1360 call assert_equal(0, searchdecl('global'))
1361 call assert_equal(5, getcurpos()[1])
1362
1363 10
1364 call assert_equal(0, searchdecl('global', 1))
1365 call assert_equal(1, getcurpos()[1])
1366
1367 bwipe!
1368endfunc