blob: 9567aa468b2c1be5dbc097424fc309aa6f3d8106 [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
Bram Moolenaar3ba35402019-12-21 22:00:50 +010060 " eighth 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
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100102 " eighth 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
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100293 call setline(1, ['other code', 'here [', ' [', ' " cursor here', ' ]]'])
294
Bram Moolenaar6e450a52017-01-06 20:03:58 +0100295 4
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100296 call assert_equal(3, searchpair('\[', '', ']', 'bW'))
297 call assert_equal([0, 3, 2, 0], getpos('.'))
298 4
299 call assert_equal(2, searchpair('\[', '', ']', 'bWr'))
300 call assert_equal([0, 2, 6, 0], getpos('.'))
301 4
302 call assert_equal(1, searchpair('\[', '', ']', 'bWm'))
303 call assert_equal([0, 3, 2, 0], getpos('.'))
304 4|norm ^
305 call assert_equal(5, searchpair('\[', '', ']', 'Wn'))
306 call assert_equal([0, 4, 2, 0], getpos('.'))
307 4
308 call assert_equal(2, searchpair('\[', '', ']', 'bW',
309 \ 'getline(".") =~ "^\\s*\["'))
310 call assert_equal([0, 2, 6, 0], getpos('.'))
Bram Moolenaar6e450a52017-01-06 20:03:58 +0100311 set nomagic
312 4
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100313 call assert_equal(3, searchpair('\[', '', ']', 'bW'))
314 call assert_equal([0, 3, 2, 0], getpos('.'))
Bram Moolenaar6e450a52017-01-06 20:03:58 +0100315 set magic
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100316 4|norm ^
317 call assert_equal(0, searchpair('{', '', '}', 'bW'))
318 call assert_equal([0, 4, 2, 0], getpos('.'))
319
320 %d
321 call setline(1, ['if 1', ' if 2', ' else', ' endif 2', 'endif 1'])
322
323 /\<if 1
324 call assert_equal(5, searchpair('\<if\>', '\<else\>', '\<endif\>', 'W'))
325 call assert_equal([0, 5, 1, 0], getpos('.'))
326 /\<if 2
327 call assert_equal(3, searchpair('\<if\>', '\<else\>', '\<endif\>', 'W'))
328 call assert_equal([0, 3, 3, 0], getpos('.'))
329
330 q!
331endfunc
332
333func Test_searchpairpos()
334 new
335 call setline(1, ['other code', 'here [', ' [', ' " cursor here', ' ]]'])
336
337 4
338 call assert_equal([3, 2], searchpairpos('\[', '', ']', 'bW'))
339 call assert_equal([0, 3, 2, 0], getpos('.'))
340 4
341 call assert_equal([2, 6], searchpairpos('\[', '', ']', 'bWr'))
342 call assert_equal([0, 2, 6, 0], getpos('.'))
343 4|norm ^
344 call assert_equal([5, 2], searchpairpos('\[', '', ']', 'Wn'))
345 call assert_equal([0, 4, 2, 0], getpos('.'))
346 4
347 call assert_equal([2, 6], searchpairpos('\[', '', ']', 'bW',
348 \ 'getline(".") =~ "^\\s*\["'))
349 call assert_equal([0, 2, 6, 0], getpos('.'))
350 4
351 call assert_equal([2, 6], searchpairpos('\[', '', ']', 'bWr'))
352 call assert_equal([0, 2, 6, 0], getpos('.'))
353 set nomagic
354 4
355 call assert_equal([3, 2], searchpairpos('\[', '', ']', 'bW'))
356 call assert_equal([0, 3, 2, 0], getpos('.'))
357 set magic
358 4|norm ^
359 call assert_equal([0, 0], searchpairpos('{', '', '}', 'bW'))
360 call assert_equal([0, 4, 2, 0], getpos('.'))
361
362 %d
363 call setline(1, ['if 1', ' if 2', ' else', ' endif 2', 'endif 1'])
364 /\<if 1
365 call assert_equal([5, 1], searchpairpos('\<if\>', '\<else\>', '\<endif\>', 'W'))
366 call assert_equal([0, 5, 1, 0], getpos('.'))
367 /\<if 2
368 call assert_equal([3, 3], searchpairpos('\<if\>', '\<else\>', '\<endif\>', 'W'))
369 call assert_equal([0, 3, 3, 0], getpos('.'))
370
Bram Moolenaar6e450a52017-01-06 20:03:58 +0100371 q!
372endfunc
373
Bram Moolenaar3dddb092018-06-24 19:01:59 +0200374func Test_searchpair_errors()
375 call assert_fails("call searchpair([0], 'middle', 'end', 'bW', 'skip', 99, 100)", 'E730: using List as a String')
376 call assert_fails("call searchpair('start', {-> 0}, 'end', 'bW', 'skip', 99, 100)", 'E729: using Funcref as a String')
377 call assert_fails("call searchpair('start', 'middle', {'one': 1}, 'bW', 'skip', 99, 100)", 'E731: using Dictionary as a String')
378 call assert_fails("call searchpair('start', 'middle', 'end', 'flags', 'skip', 99, 100)", 'E475: Invalid argument: flags')
379 call assert_fails("call searchpair('start', 'middle', 'end', 'bW', 0, 99, 100)", 'E475: Invalid argument: 0')
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')
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100382 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 List as a String')
388 call assert_fails("call searchpairpos('start', {-> 0}, 'end', 'bW', 'skip', 99, 100)", 'E729: using Funcref as a String')
389 call assert_fails("call searchpairpos('start', 'middle', {'one': 1}, 'bW', 'skip', 99, 100)", 'E731: using 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', 0, 99, 100)", 'E475: Invalid argument: 0')
392 call assert_fails("call searchpairpos('start', 'middle', 'end', 'bW', 'func', -99, 100)", 'E475: Invalid argument: -99')
393 call assert_fails("call searchpairpos('start', 'middle', 'end', 'bW', 'func', 99, -100)", 'E475: Invalid argument: -100')
394 call assert_fails("call searchpairpos('start', 'middle', 'end', 'e')", 'E475: Invalid argument: e')
395 call assert_fails("call searchpairpos('start', 'middle', 'end', 'sn')", 'E475: Invalid argument: sn')
Bram Moolenaar3dddb092018-06-24 19:01:59 +0200396endfunc
397
Bram Moolenaar48570482017-10-30 21:48:41 +0100398func Test_searchpair_skip()
399 func Zero()
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100400 return 0
Bram Moolenaar48570482017-10-30 21:48:41 +0100401 endfunc
402 func Partial(x)
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100403 return a:x
Bram Moolenaar48570482017-10-30 21:48:41 +0100404 endfunc
405 new
406 call setline(1, ['{', 'foo', 'foo', 'foo', '}'])
407 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', ''))
408 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', '0'))
409 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', {-> 0}))
410 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', function('Zero')))
411 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', function('Partial', [0])))
Bram Moolenaar48570482017-10-30 21:48:41 +0100412 bw!
413endfunc
414
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200415func Test_searchpair_leak()
416 new
417 call setline(1, 'if one else another endif')
418
419 " The error in the skip expression caused memory to leak.
420 call assert_fails("call searchpair('\\<if\\>', '\\<else\\>', '\\<endif\\>', '', '\"foo\" 2')", 'E15:')
421
422 bwipe!
423endfunc
424
Bram Moolenaar66727e12017-03-01 22:17:05 +0100425func Test_searchc()
426 " These commands used to cause memory overflow in searchc().
427 new
428 norm ixx
429 exe "norm 0t\u93cf"
430 bw!
431endfunc
Bram Moolenaara693d052017-06-29 22:23:06 +0200432
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200433func Cmdline3_prep()
434 " need to disable char_avail,
435 " so that expansion of commandline works
436 call test_override("char_avail", 1)
437 new
438 call setline(1, [' 1', ' 2 the~e', ' 3 the theother'])
439 set incsearch
440endfunc
441
Bram Moolenaar976b8472018-08-12 15:49:47 +0200442func Incsearch_cleanup()
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200443 set noincsearch
444 call test_override("char_avail", 0)
445 bw!
446endfunc
447
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200448func Test_search_cmdline3()
449 if !exists('+incsearch')
450 return
451 endif
452 call Cmdline3_prep()
453 1
454 " first match
455 call feedkeys("/the\<c-l>\<cr>", 'tx')
456 call assert_equal(' 2 the~e', getline('.'))
457
Bram Moolenaar976b8472018-08-12 15:49:47 +0200458 call Incsearch_cleanup()
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200459endfunc
460
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200461func Test_search_cmdline3s()
462 if !exists('+incsearch')
463 return
464 endif
465 call Cmdline3_prep()
466 1
467 call feedkeys(":%s/the\<c-l>/xxx\<cr>", 'tx')
468 call assert_equal(' 2 xxxe', getline('.'))
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200469 undo
470 call feedkeys(":%subs/the\<c-l>/xxx\<cr>", 'tx')
471 call assert_equal(' 2 xxxe', getline('.'))
472 undo
473 call feedkeys(":%substitute/the\<c-l>/xxx\<cr>", 'tx')
474 call assert_equal(' 2 xxxe', getline('.'))
Bram Moolenaar167ae422018-08-14 21:32:21 +0200475 undo
476 call feedkeys(":%smagic/the.e/xxx\<cr>", 'tx')
477 call assert_equal(' 2 xxx', getline('.'))
478 undo
479 call assert_fails(":%snomagic/the.e/xxx\<cr>", 'E486')
480 "
481 call feedkeys(":%snomagic/the\\.e/xxx\<cr>", 'tx')
482 call assert_equal(' 2 xxx', getline('.'))
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200483
Bram Moolenaar976b8472018-08-12 15:49:47 +0200484 call Incsearch_cleanup()
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200485endfunc
486
487func Test_search_cmdline3g()
488 if !exists('+incsearch')
489 return
490 endif
491 call Cmdline3_prep()
492 1
493 call feedkeys(":g/the\<c-l>/d\<cr>", 'tx')
494 call assert_equal(' 3 the theother', getline(2))
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200495 undo
496 call feedkeys(":global/the\<c-l>/d\<cr>", 'tx')
497 call assert_equal(' 3 the theother', getline(2))
Bram Moolenaardef7b1d2018-08-13 22:54:35 +0200498 undo
499 call feedkeys(":g!/the\<c-l>/d\<cr>", 'tx')
500 call assert_equal(1, line('$'))
501 call assert_equal(' 2 the~e', getline(1))
502 undo
503 call feedkeys(":global!/the\<c-l>/d\<cr>", 'tx')
504 call assert_equal(1, line('$'))
505 call assert_equal(' 2 the~e', getline(1))
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200506
Bram Moolenaar976b8472018-08-12 15:49:47 +0200507 call Incsearch_cleanup()
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200508endfunc
509
510func Test_search_cmdline3v()
511 if !exists('+incsearch')
512 return
513 endif
514 call Cmdline3_prep()
515 1
516 call feedkeys(":v/the\<c-l>/d\<cr>", 'tx')
517 call assert_equal(1, line('$'))
518 call assert_equal(' 2 the~e', getline(1))
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200519 undo
520 call feedkeys(":vglobal/the\<c-l>/d\<cr>", 'tx')
521 call assert_equal(1, line('$'))
522 call assert_equal(' 2 the~e', getline(1))
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200523
Bram Moolenaar976b8472018-08-12 15:49:47 +0200524 call Incsearch_cleanup()
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200525endfunc
526
Bram Moolenaarda5116d2017-07-01 23:11:17 +0200527func Test_search_cmdline4()
528 if !exists('+incsearch')
529 return
530 endif
531 " need to disable char_avail,
532 " so that expansion of commandline works
533 call test_override("char_avail", 1)
534 new
535 call setline(1, [' 1 the first', ' 2 the second', ' 3 the third'])
536 set incsearch
537 $
538 call feedkeys("?the\<c-g>\<cr>", 'tx')
539 call assert_equal(' 3 the third', getline('.'))
540 $
541 call feedkeys("?the\<c-g>\<c-g>\<cr>", 'tx')
542 call assert_equal(' 1 the first', getline('.'))
543 $
544 call feedkeys("?the\<c-g>\<c-g>\<c-g>\<cr>", 'tx')
545 call assert_equal(' 2 the second', getline('.'))
546 $
547 call feedkeys("?the\<c-t>\<cr>", 'tx')
548 call assert_equal(' 1 the first', getline('.'))
549 $
550 call feedkeys("?the\<c-t>\<c-t>\<cr>", 'tx')
551 call assert_equal(' 3 the third', getline('.'))
552 $
553 call feedkeys("?the\<c-t>\<c-t>\<c-t>\<cr>", 'tx')
554 call assert_equal(' 2 the second', getline('.'))
555 " clean up
556 set noincsearch
557 call test_override("char_avail", 0)
558 bw!
559endfunc
Bram Moolenaardb510072017-09-28 21:52:17 +0200560
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200561func Test_search_cmdline5()
562 if !exists('+incsearch')
563 return
564 endif
565 " Do not call test_override("char_avail", 1) so that <C-g> and <C-t> work
566 " regardless char_avail.
567 new
Bram Moolenaar69a5b862019-07-16 21:38:51 +0200568 call setline(1, [' 1 the first', ' 2 the second', ' 3 the third', ''])
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200569 set incsearch
570 1
571 call feedkeys("/the\<c-g>\<c-g>\<cr>", 'tx')
572 call assert_equal(' 3 the third', getline('.'))
573 $
574 call feedkeys("?the\<c-t>\<c-t>\<c-t>\<cr>", 'tx')
Bram Moolenaar69a5b862019-07-16 21:38:51 +0200575 call assert_equal(' 1 the first', getline('.'))
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200576 " clean up
577 set noincsearch
578 bw!
579endfunc
580
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100581func Test_search_cmdline6()
582 " Test that consecutive matches
583 " are caught by <c-g>/<c-t>
584 if !exists('+incsearch')
585 return
586 endif
587 " need to disable char_avail,
588 " so that expansion of commandline works
589 call test_override("char_avail", 1)
590 new
591 call setline(1, [' bbvimb', ''])
592 set incsearch
593 " first match
594 norm! gg0
595 call feedkeys("/b\<cr>", 'tx')
596 call assert_equal([0,1,2,0], getpos('.'))
597 " second match
598 norm! gg0
599 call feedkeys("/b\<c-g>\<cr>", 'tx')
600 call assert_equal([0,1,3,0], getpos('.'))
601 " third match
602 norm! gg0
603 call feedkeys("/b\<c-g>\<c-g>\<cr>", 'tx')
604 call assert_equal([0,1,7,0], getpos('.'))
605 " first match again
606 norm! gg0
607 call feedkeys("/b\<c-g>\<c-g>\<c-g>\<cr>", 'tx')
608 call assert_equal([0,1,2,0], getpos('.'))
609 set nowrapscan
610 " last match
611 norm! gg0
612 call feedkeys("/b\<c-g>\<c-g>\<c-g>\<cr>", 'tx')
613 call assert_equal([0,1,7,0], getpos('.'))
614 " clean up
615 set wrapscan&vim
616 set noincsearch
617 call test_override("char_avail", 0)
618 bw!
619endfunc
620
621func Test_search_cmdline7()
622 " Test that an pressing <c-g> in an empty command line
623 " does not move the cursor
624 if !exists('+incsearch')
625 return
626 endif
627 " need to disable char_avail,
628 " so that expansion of commandline works
629 call test_override("char_avail", 1)
630 new
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200631 let @/ = 'b'
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100632 call setline(1, [' bbvimb', ''])
633 set incsearch
634 " first match
635 norm! gg0
636 " moves to next match of previous search pattern, just like /<cr>
637 call feedkeys("/\<c-g>\<cr>", 'tx')
638 call assert_equal([0,1,2,0], getpos('.'))
639 " moves to next match of previous search pattern, just like /<cr>
640 call feedkeys("/\<cr>", 'tx')
641 call assert_equal([0,1,3,0], getpos('.'))
642 " moves to next match of previous search pattern, just like /<cr>
643 call feedkeys("/\<c-t>\<cr>", 'tx')
644 call assert_equal([0,1,7,0], getpos('.'))
Bram Moolenaard0480092017-11-16 22:20:39 +0100645
646 " using an offset uses the last search pattern
647 call cursor(1, 1)
648 call setline(1, ['1 bbvimb', ' 2 bbvimb'])
649 let @/ = 'b'
650 call feedkeys("//e\<c-g>\<cr>", 'tx')
651 call assert_equal('1 bbvimb', getline('.'))
652 call assert_equal(4, col('.'))
653
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100654 set noincsearch
655 call test_override("char_avail", 0)
656 bw!
657endfunc
658
659func Test_search_cmdline8()
660 " Highlighting is cleared in all windows
661 " since hls applies to all windows
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200662 CheckOption incsearch
663 CheckFeature terminal
664 CheckNotGui
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100665 if has("win32")
666 throw "Skipped: Bug with sending <ESC> to terminal window not fixed yet"
667 endif
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200668
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100669 let h = winheight(0)
670 if h < 3
671 return
672 endif
673 " Prepare buffer text
674 let lines = ['abb vim vim vi', 'vimvivim']
675 call writefile(lines, 'Xsearch.txt')
Bram Moolenaar13deab82017-11-04 18:48:43 +0100676 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', 'Xsearch.txt'], {'term_rows': 3})
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100677
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200678 call WaitForAssert({-> assert_equal(lines, [term_getline(buf, 1), term_getline(buf, 2)])})
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100679
Bram Moolenaar13deab82017-11-04 18:48:43 +0100680 call term_sendkeys(buf, ":set incsearch hlsearch\<cr>")
681 call term_sendkeys(buf, ":14vsp\<cr>")
682 call term_sendkeys(buf, "/vim\<cr>")
683 call term_sendkeys(buf, "/b\<esc>")
684 call term_sendkeys(buf, "gg0")
685 call term_wait(buf, 500)
686 let screen_line = term_scrape(buf, 1)
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100687 let [a0,a1,a2,a3] = [screen_line[3].attr, screen_line[4].attr,
688 \ screen_line[18].attr, screen_line[19].attr]
689 call assert_notequal(a0, a1)
690 call assert_notequal(a0, a3)
691 call assert_notequal(a1, a2)
692 call assert_equal(a0, a2)
693 call assert_equal(a1, a3)
694 " clean up
695 call delete('Xsearch.txt')
696
697 bwipe!
698endfunc
699
Bram Moolenaardb510072017-09-28 21:52:17 +0200700" Tests for regexp with various magic settings
701func Test_search_regexp()
702 enew!
703
704 put ='1 a aa abb abbccc'
705 exe 'normal! /a*b\{2}c\+/e' . "\<CR>"
706 call assert_equal([0, 2, 17, 0], getpos('.'))
707
708 put ='2 d dd dee deefff'
709 exe 'normal! /\Md\*e\{2}f\+/e' . "\<CR>"
710 call assert_equal([0, 3, 17, 0], getpos('.'))
711
712 set nomagic
713 put ='3 g gg ghh ghhiii'
714 exe 'normal! /g\*h\{2}i\+/e' . "\<CR>"
715 call assert_equal([0, 4, 17, 0], getpos('.'))
716
717 put ='4 j jj jkk jkklll'
718 exe 'normal! /\mj*k\{2}l\+/e' . "\<CR>"
719 call assert_equal([0, 5, 17, 0], getpos('.'))
720
721 put ='5 m mm mnn mnnooo'
722 exe 'normal! /\vm*n{2}o+/e' . "\<CR>"
723 call assert_equal([0, 6, 17, 0], getpos('.'))
724
725 put ='6 x ^aa$ x'
726 exe 'normal! /\V^aa$' . "\<CR>"
727 call assert_equal([0, 7, 5, 0], getpos('.'))
728
729 set magic
730 put ='7 (a)(b) abbaa'
731 exe 'normal! /\v(a)(b)\2\1\1/e' . "\<CR>"
732 call assert_equal([0, 8, 14, 0], getpos('.'))
733
734 put ='8 axx [ab]xx'
735 exe 'normal! /\V[ab]\(\[xy]\)\1' . "\<CR>"
736 call assert_equal([0, 9, 7, 0], getpos('.'))
737
738 set undolevels=100
739 put ='9 foobar'
740 put =''
741 exe "normal! a\<C-G>u\<Esc>"
742 normal G
743 exe 'normal! dv?bar?' . "\<CR>"
744 call assert_equal('9 foo', getline('.'))
745 call assert_equal([0, 10, 5, 0], getpos('.'))
746 call assert_equal(10, line('$'))
747 normal u
748 call assert_equal('9 foobar', getline('.'))
749 call assert_equal([0, 10, 6, 0], getpos('.'))
750 call assert_equal(11, line('$'))
751
752 set undolevels&
753 enew!
754endfunc
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100755
756func Test_search_cmdline_incsearch_highlight()
757 if !exists('+incsearch')
758 return
759 endif
760 set incsearch hlsearch
761 " need to disable char_avail,
762 " so that expansion of commandline works
763 call test_override("char_avail", 1)
764 new
765 call setline(1, ['aaa 1 the first', ' 2 the second', ' 3 the third'])
766
767 1
768 call feedkeys("/second\<cr>", 'tx')
769 call assert_equal('second', @/)
770 call assert_equal(' 2 the second', getline('.'))
771
772 " Canceling search won't change @/
773 1
774 let @/ = 'last pattern'
775 call feedkeys("/third\<C-c>", 'tx')
776 call assert_equal('last pattern', @/)
777 call feedkeys("/third\<Esc>", 'tx')
778 call assert_equal('last pattern', @/)
779 call feedkeys("/3\<bs>\<bs>", 'tx')
780 call assert_equal('last pattern', @/)
781 call feedkeys("/third\<c-g>\<c-t>\<Esc>", 'tx')
782 call assert_equal('last pattern', @/)
783
784 " clean up
785 set noincsearch nohlsearch
786 bw!
787endfunc
788
789func Test_search_cmdline_incsearch_highlight_attr()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200790 CheckOption incsearch
791 CheckFeature terminal
792 CheckNotGui
793
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100794 let h = winheight(0)
795 if h < 3
796 return
797 endif
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100798
799 " Prepare buffer text
Bram Moolenaar13deab82017-11-04 18:48:43 +0100800 let lines = ['abb vim vim vi', 'vimvivim']
801 call writefile(lines, 'Xsearch.txt')
802 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', 'Xsearch.txt'], {'term_rows': 3})
803
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200804 call WaitForAssert({-> assert_equal(lines, [term_getline(buf, 1), term_getline(buf, 2)])})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100805 " wait for vim to complete initialization
806 call term_wait(buf)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100807
808 " Get attr of normal(a0), incsearch(a1), hlsearch(a2) highlight
Bram Moolenaar13deab82017-11-04 18:48:43 +0100809 call term_sendkeys(buf, ":set incsearch hlsearch\<cr>")
810 call term_sendkeys(buf, '/b')
811 call term_wait(buf, 200)
812 let screen_line1 = term_scrape(buf, 1)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100813 call assert_true(len(screen_line1) > 2)
814 " a0: attr_normal
815 let a0 = screen_line1[0].attr
816 " a1: attr_incsearch
817 let a1 = screen_line1[1].attr
818 " a2: attr_hlsearch
819 let a2 = screen_line1[2].attr
820 call assert_notequal(a0, a1)
821 call assert_notequal(a0, a2)
822 call assert_notequal(a1, a2)
Bram Moolenaar13deab82017-11-04 18:48:43 +0100823 call term_sendkeys(buf, "\<cr>gg0")
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100824
825 " Test incremental highlight search
Bram Moolenaar13deab82017-11-04 18:48:43 +0100826 call term_sendkeys(buf, "/vim")
827 call term_wait(buf, 200)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100828 " Buffer:
829 " abb vim vim vi
830 " vimvivim
831 " Search: /vim
832 let attr_line1 = [a0,a0,a0,a0,a1,a1,a1,a0,a2,a2,a2,a0,a0,a0]
833 let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100834 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
835 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100836
837 " Test <C-g>
Bram Moolenaar13deab82017-11-04 18:48:43 +0100838 call term_sendkeys(buf, "\<C-g>\<C-g>")
839 call term_wait(buf, 200)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100840 let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a2,a2,a2,a0,a0,a0]
841 let attr_line2 = [a1,a1,a1,a0,a0,a2,a2,a2]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100842 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
843 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100844
845 " Test <C-t>
Bram Moolenaar13deab82017-11-04 18:48:43 +0100846 call term_sendkeys(buf, "\<C-t>")
847 call term_wait(buf, 200)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100848 let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a1,a1,a1,a0,a0,a0]
849 let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100850 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
851 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100852
853 " Type Enter and a1(incsearch highlight) should become a2(hlsearch highlight)
Bram Moolenaar13deab82017-11-04 18:48:43 +0100854 call term_sendkeys(buf, "\<cr>")
855 call term_wait(buf, 200)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100856 let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a2,a2,a2,a0,a0,a0]
857 let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100858 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
859 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100860
861 " Test nohlsearch. a2(hlsearch highlight) should become a0(normal highlight)
Bram Moolenaar13deab82017-11-04 18:48:43 +0100862 call term_sendkeys(buf, ":1\<cr>")
863 call term_sendkeys(buf, ":set nohlsearch\<cr>")
864 call term_sendkeys(buf, "/vim")
865 call term_wait(buf, 200)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100866 let attr_line1 = [a0,a0,a0,a0,a1,a1,a1,a0,a0,a0,a0,a0,a0,a0]
867 let attr_line2 = [a0,a0,a0,a0,a0,a0,a0,a0]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100868 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
869 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100870 call delete('Xsearch.txt')
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100871
Bram Moolenaarb94340c2017-11-02 16:16:31 +0100872 call delete('Xsearch.txt')
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100873 bwipe!
874endfunc
Bram Moolenaarf45938c2017-11-02 15:59:57 +0100875
Bram Moolenaar548e5982018-12-26 21:45:00 +0100876func Test_incsearch_cmdline_modifier()
877 if !exists('+incsearch')
878 return
879 endif
880 call test_override("char_avail", 1)
881 new
882 call setline(1, ['foo'])
883 set incsearch
884 " Test that error E14 does not occur in parsing command modifier.
885 call feedkeys("V:tab", 'tx')
886
887 call Incsearch_cleanup()
888endfunc
889
Bram Moolenaar9d34d902018-04-27 22:18:12 +0200890func Test_incsearch_scrolling()
891 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200892 throw 'Skipped: cannot make screendumps'
Bram Moolenaar9d34d902018-04-27 22:18:12 +0200893 endif
894 call assert_equal(0, &scrolloff)
895 call writefile([
896 \ 'let dots = repeat(".", 120)',
897 \ 'set incsearch cmdheight=2 scrolloff=0',
898 \ 'call setline(1, [dots, dots, dots, "", "target", dots, dots])',
899 \ 'normal gg',
900 \ 'redraw',
901 \ ], 'Xscript')
902 let buf = RunVimInTerminal('-S Xscript', {'rows': 9, 'cols': 70})
903 " Need to send one key at a time to force a redraw
904 call term_sendkeys(buf, '/')
905 sleep 100m
906 call term_sendkeys(buf, 't')
907 sleep 100m
908 call term_sendkeys(buf, 'a')
909 sleep 100m
910 call term_sendkeys(buf, 'r')
911 sleep 100m
912 call term_sendkeys(buf, 'g')
913 call VerifyScreenDump(buf, 'Test_incsearch_scrolling_01', {})
914
915 call term_sendkeys(buf, "\<Esc>")
916 call StopVimInTerminal(buf)
917 call delete('Xscript')
918endfunc
919
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200920func Test_incsearch_search_dump()
921 if !exists('+incsearch')
922 return
923 endif
924 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200925 throw 'Skipped: cannot make screendumps'
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200926 endif
927 call writefile([
928 \ 'set incsearch hlsearch scrolloff=0',
929 \ 'for n in range(1, 8)',
930 \ ' call setline(n, "foo " . n)',
931 \ 'endfor',
932 \ '3',
933 \ ], 'Xis_search_script')
934 let buf = RunVimInTerminal('-S Xis_search_script', {'rows': 9, 'cols': 70})
935 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
936 " the 'ambiwidth' check.
937 sleep 100m
938
939 " Need to send one key at a time to force a redraw.
940 call term_sendkeys(buf, '/fo')
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200941 call VerifyScreenDump(buf, 'Test_incsearch_search_01', {})
942 call term_sendkeys(buf, "\<Esc>")
943 sleep 100m
944
945 call term_sendkeys(buf, '/\v')
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200946 call VerifyScreenDump(buf, 'Test_incsearch_search_02', {})
947 call term_sendkeys(buf, "\<Esc>")
948
949 call StopVimInTerminal(buf)
950 call delete('Xis_search_script')
951endfunc
952
Bram Moolenaar976b8472018-08-12 15:49:47 +0200953func Test_incsearch_substitute()
954 if !exists('+incsearch')
955 return
956 endif
957 call test_override("char_avail", 1)
958 new
959 set incsearch
960 for n in range(1, 10)
961 call setline(n, 'foo ' . n)
962 endfor
963 4
964 call feedkeys(":.,.+2s/foo\<BS>o\<BS>o/xxx\<cr>", 'tx')
965 call assert_equal('foo 3', getline(3))
966 call assert_equal('xxx 4', getline(4))
967 call assert_equal('xxx 5', getline(5))
968 call assert_equal('xxx 6', getline(6))
969 call assert_equal('foo 7', getline(7))
970
971 call Incsearch_cleanup()
972endfunc
973
Bram Moolenaar164251f2018-08-12 16:26:58 +0200974" Similar to Test_incsearch_substitute() but with a screendump halfway.
975func Test_incsearch_substitute_dump()
976 if !exists('+incsearch')
977 return
978 endif
979 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200980 throw 'Skipped: cannot make screendumps'
Bram Moolenaar164251f2018-08-12 16:26:58 +0200981 endif
982 call writefile([
983 \ 'set incsearch hlsearch scrolloff=0',
984 \ 'for n in range(1, 10)',
985 \ ' call setline(n, "foo " . n)',
986 \ 'endfor',
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200987 \ 'call setline(11, "bar 11")',
Bram Moolenaar164251f2018-08-12 16:26:58 +0200988 \ '3',
989 \ ], 'Xis_subst_script')
990 let buf = RunVimInTerminal('-S Xis_subst_script', {'rows': 9, 'cols': 70})
991 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
992 " the 'ambiwidth' check.
993 sleep 100m
994
995 " Need to send one key at a time to force a redraw.
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200996 " Select three lines at the cursor with typed pattern.
Bram Moolenaar164251f2018-08-12 16:26:58 +0200997 call term_sendkeys(buf, ':.,.+2s/')
998 sleep 100m
999 call term_sendkeys(buf, 'f')
1000 sleep 100m
1001 call term_sendkeys(buf, 'o')
1002 sleep 100m
1003 call term_sendkeys(buf, 'o')
1004 call VerifyScreenDump(buf, 'Test_incsearch_substitute_01', {})
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02001005 call term_sendkeys(buf, "\<Esc>")
1006
1007 " Select three lines at the cursor using previous pattern.
1008 call term_sendkeys(buf, "/foo\<CR>")
1009 sleep 100m
1010 call term_sendkeys(buf, ':.,.+2s//')
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02001011 call VerifyScreenDump(buf, 'Test_incsearch_substitute_02', {})
1012
1013 " Deleting last slash should remove the match.
1014 call term_sendkeys(buf, "\<BS>")
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02001015 call VerifyScreenDump(buf, 'Test_incsearch_substitute_03', {})
Bram Moolenaar60d08712018-08-12 21:53:15 +02001016 call term_sendkeys(buf, "\<Esc>")
1017
1018 " Reverse range is accepted
1019 call term_sendkeys(buf, ':5,2s/foo')
Bram Moolenaar60d08712018-08-12 21:53:15 +02001020 call VerifyScreenDump(buf, 'Test_incsearch_substitute_04', {})
Bram Moolenaar164251f2018-08-12 16:26:58 +02001021 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaar2b926fc2018-08-13 11:07:57 +02001022
1023 " White space after the command is skipped
1024 call term_sendkeys(buf, ':2,3sub /fo')
Bram Moolenaar2b926fc2018-08-13 11:07:57 +02001025 call VerifyScreenDump(buf, 'Test_incsearch_substitute_05', {})
1026 call term_sendkeys(buf, "\<Esc>")
1027
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001028 " Command modifiers are skipped
1029 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 +02001030 call VerifyScreenDump(buf, 'Test_incsearch_substitute_06', {})
1031 call term_sendkeys(buf, "\<Esc>")
1032
Bram Moolenaar2f6a3462018-08-14 18:16:52 +02001033 " Cursorline highlighting at match
1034 call term_sendkeys(buf, ":set cursorline\<CR>")
1035 call term_sendkeys(buf, 'G9G')
1036 call term_sendkeys(buf, ':9,11s/bar')
Bram Moolenaar2f6a3462018-08-14 18:16:52 +02001037 call VerifyScreenDump(buf, 'Test_incsearch_substitute_07', {})
1038 call term_sendkeys(buf, "\<Esc>")
1039
1040 " Cursorline highlighting at cursor when no match
1041 call term_sendkeys(buf, ':9,10s/bar')
Bram Moolenaar2f6a3462018-08-14 18:16:52 +02001042 call VerifyScreenDump(buf, 'Test_incsearch_substitute_08', {})
1043 call term_sendkeys(buf, "\<Esc>")
1044
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +02001045 " Only \v handled as empty pattern, does not move cursor
1046 call term_sendkeys(buf, '3G4G')
1047 call term_sendkeys(buf, ":nohlsearch\<CR>")
1048 call term_sendkeys(buf, ':6,7s/\v')
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +02001049 call VerifyScreenDump(buf, 'Test_incsearch_substitute_09', {})
1050 call term_sendkeys(buf, "\<Esc>")
1051
Bram Moolenaarf13daa42018-08-31 22:09:54 +02001052 call term_sendkeys(buf, ":set nocursorline\<CR>")
1053
1054 " All matches are highlighted for 'hlsearch' after the incsearch canceled
1055 call term_sendkeys(buf, "1G*")
1056 call term_sendkeys(buf, ":2,5s/foo")
1057 sleep 100m
1058 call term_sendkeys(buf, "\<Esc>")
1059 call VerifyScreenDump(buf, 'Test_incsearch_substitute_10', {})
1060
Bram Moolenaar65985ac2018-09-16 17:08:04 +02001061 call term_sendkeys(buf, ":split\<CR>")
1062 call term_sendkeys(buf, ":let @/ = 'xyz'\<CR>")
1063 call term_sendkeys(buf, ":%s/.")
1064 call VerifyScreenDump(buf, 'Test_incsearch_substitute_11', {})
1065 call term_sendkeys(buf, "\<BS>")
1066 call VerifyScreenDump(buf, 'Test_incsearch_substitute_12', {})
1067 call term_sendkeys(buf, "\<Esc>")
1068 call VerifyScreenDump(buf, 'Test_incsearch_substitute_13', {})
Bram Moolenaarc6725252019-11-23 21:56:46 +01001069 call term_sendkeys(buf, ":%bwipe!\<CR>")
1070 call term_sendkeys(buf, ":only!\<CR>")
1071
1072 " get :'<,'>s command in history
1073 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
1074 call term_sendkeys(buf, "aasdfasdf\<Esc>")
1075 call term_sendkeys(buf, "V:s/a/b/g\<CR>")
1076 " Using '<,'> does not give E20
1077 call term_sendkeys(buf, ":new\<CR>")
1078 call term_sendkeys(buf, "aasdfasdf\<Esc>")
1079 call term_sendkeys(buf, ":\<Up>\<Up>")
1080 call VerifyScreenDump(buf, 'Test_incsearch_substitute_14', {})
1081 call term_sendkeys(buf, "<Esc>")
Bram Moolenaar65985ac2018-09-16 17:08:04 +02001082
Bram Moolenaar164251f2018-08-12 16:26:58 +02001083 call StopVimInTerminal(buf)
1084 call delete('Xis_subst_script')
1085endfunc
1086
Bram Moolenaar4a7d2d32019-02-21 16:25:50 +01001087func Test_incsearch_with_change()
1088 if !has('timers') || !exists('+incsearch') || !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001089 throw 'Skipped: cannot make screendumps and/or timers feature and/or incsearch option missing'
Bram Moolenaar4a7d2d32019-02-21 16:25:50 +01001090 endif
1091
1092 call writefile([
1093 \ 'set incsearch hlsearch scrolloff=0',
1094 \ 'call setline(1, ["one", "two ------ X", "three"])',
1095 \ 'call timer_start(200, { _ -> setline(2, "x")})',
1096 \ ], 'Xis_change_script')
1097 let buf = RunVimInTerminal('-S Xis_change_script', {'rows': 9, 'cols': 70})
1098 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1099 " the 'ambiwidth' check.
Bram Moolenaare86ecbd2019-02-21 17:48:59 +01001100 sleep 300m
Bram Moolenaar4a7d2d32019-02-21 16:25:50 +01001101
1102 " Highlight X, it will be deleted by the timer callback.
1103 call term_sendkeys(buf, ':%s/X')
1104 call VerifyScreenDump(buf, 'Test_incsearch_change_01', {})
1105 call term_sendkeys(buf, "\<Esc>")
1106
1107 call StopVimInTerminal(buf)
1108 call delete('Xis_change_script')
1109endfunc
1110
Bram Moolenaar81f56532018-08-18 16:19:42 +02001111" Similar to Test_incsearch_substitute_dump() for :sort
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +02001112func Test_incsearch_sort_dump()
Bram Moolenaar81f56532018-08-18 16:19:42 +02001113 if !exists('+incsearch')
1114 return
1115 endif
1116 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001117 throw 'Skipped: cannot make screendumps'
Bram Moolenaar81f56532018-08-18 16:19:42 +02001118 endif
1119 call writefile([
1120 \ 'set incsearch hlsearch scrolloff=0',
1121 \ 'call setline(1, ["another one 2", "that one 3", "the one 1"])',
1122 \ ], 'Xis_sort_script')
1123 let buf = RunVimInTerminal('-S Xis_sort_script', {'rows': 9, 'cols': 70})
1124 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1125 " the 'ambiwidth' check.
1126 sleep 100m
1127
1128 " Need to send one key at a time to force a redraw.
1129 call term_sendkeys(buf, ':sort ni u /on')
Bram Moolenaar81f56532018-08-18 16:19:42 +02001130 call VerifyScreenDump(buf, 'Test_incsearch_sort_01', {})
1131 call term_sendkeys(buf, "\<Esc>")
1132
1133 call StopVimInTerminal(buf)
1134 call delete('Xis_sort_script')
1135endfunc
1136
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001137" Similar to Test_incsearch_substitute_dump() for :vimgrep famiry
1138func Test_incsearch_vimgrep_dump()
1139 if !exists('+incsearch')
1140 return
1141 endif
1142 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001143 throw 'Skipped: cannot make screendumps'
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001144 endif
1145 call writefile([
1146 \ 'set incsearch hlsearch scrolloff=0',
1147 \ 'call setline(1, ["another one 2", "that one 3", "the one 1"])',
1148 \ ], 'Xis_vimgrep_script')
1149 let buf = RunVimInTerminal('-S Xis_vimgrep_script', {'rows': 9, 'cols': 70})
1150 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1151 " the 'ambiwidth' check.
1152 sleep 100m
1153
1154 " Need to send one key at a time to force a redraw.
1155 call term_sendkeys(buf, ':vimgrep on')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001156 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_01', {})
1157 call term_sendkeys(buf, "\<Esc>")
1158
1159 call term_sendkeys(buf, ':vimg /on/ *.txt')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001160 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_02', {})
1161 call term_sendkeys(buf, "\<Esc>")
1162
1163 call term_sendkeys(buf, ':vimgrepadd "\<on')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001164 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_03', {})
1165 call term_sendkeys(buf, "\<Esc>")
1166
1167 call term_sendkeys(buf, ':lv "tha')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001168 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_04', {})
1169 call term_sendkeys(buf, "\<Esc>")
1170
1171 call term_sendkeys(buf, ':lvimgrepa "the" **/*.txt')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001172 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_05', {})
1173 call term_sendkeys(buf, "\<Esc>")
1174
1175 call StopVimInTerminal(buf)
1176 call delete('Xis_vimgrep_script')
1177endfunc
1178
Bram Moolenaar198cb662018-09-06 21:44:17 +02001179func Test_keep_last_search_pattern()
1180 if !exists('+incsearch')
1181 return
1182 endif
1183 new
1184 call setline(1, ['foo', 'foo', 'foo'])
1185 set incsearch
1186 call test_override("char_avail", 1)
1187 let @/ = 'bar'
1188 call feedkeys(":/foo/s//\<Esc>", 'ntx')
1189 call assert_equal('bar', @/)
1190
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001191 " no error message if pattern not found
1192 call feedkeys(":/xyz/s//\<Esc>", 'ntx')
1193 call assert_equal('bar', @/)
1194
Bram Moolenaar198cb662018-09-06 21:44:17 +02001195 bwipe!
1196 call test_override("ALL", 0)
1197 set noincsearch
1198endfunc
1199
Bram Moolenaar99f043a2018-09-09 15:54:14 +02001200func Test_word_under_cursor_after_match()
1201 if !exists('+incsearch')
1202 return
1203 endif
1204 new
1205 call setline(1, 'foo bar')
1206 set incsearch
1207 call test_override("char_avail", 1)
1208 try
1209 call feedkeys("/foo\<C-R>\<C-W>\<CR>", 'ntx')
1210 catch /E486:/
1211 endtry
1212 call assert_equal('foobar', @/)
1213
1214 bwipe!
1215 call test_override("ALL", 0)
1216 set noincsearch
1217endfunc
1218
1219func Test_subst_word_under_cursor()
1220 if !exists('+incsearch')
1221 return
1222 endif
1223 new
1224 call setline(1, ['int SomeLongName;', 'for (xxx = 1; xxx < len; ++xxx)'])
1225 set incsearch
1226 call test_override("char_avail", 1)
1227 call feedkeys("/LongName\<CR>", 'ntx')
1228 call feedkeys(":%s/xxx/\<C-R>\<C-W>/g\<CR>", 'ntx')
1229 call assert_equal('for (SomeLongName = 1; SomeLongName < len; ++SomeLongName)', getline(2))
1230
1231 bwipe!
1232 call test_override("ALL", 0)
1233 set noincsearch
1234endfunc
1235
Bram Moolenaarf45938c2017-11-02 15:59:57 +01001236func Test_search_undefined_behaviour()
1237 if !has("terminal")
1238 return
1239 endif
1240 let h = winheight(0)
1241 if h < 3
1242 return
1243 endif
1244 " did cause an undefined left shift
1245 let g:buf = term_start([GetVimProg(), '--clean', '-e', '-s', '-c', 'call search(getline("."))', 'samples/test000'], {'term_rows': 3})
1246 call assert_equal([''], getline(1, '$'))
1247 call term_sendkeys(g:buf, ":qa!\<cr>")
1248 bwipe!
1249endfunc
Bram Moolenaar2973daa2017-11-02 23:15:40 +01001250
1251func Test_search_undefined_behaviour2()
1252 call search("\%UC0000000")
1253endfunc
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001254
1255" Test for search('multi-byte char', 'bce')
1256func Test_search_multibyte()
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001257 let save_enc = &encoding
1258 set encoding=utf8
1259 enew!
1260 call append('$', 'A')
1261 call cursor(2, 1)
1262 call assert_equal(2, search('A', 'bce', line('.')))
1263 enew!
1264 let &encoding = save_enc
1265endfunc
Bram Moolenaar890dd052017-12-16 19:59:37 +01001266
1267" This was causing E874. Also causes an invalid read?
1268func Test_look_behind()
1269 new
Bram Moolenaar3ba35402019-12-21 22:00:50 +01001270 call setline(1, '0\|\&\n\@<=')
Bram Moolenaar890dd052017-12-16 19:59:37 +01001271 call search(getline("."))
1272 bwipe!
1273endfunc
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01001274
1275func Test_search_sentence()
1276 new
1277 " this used to cause a crash
Bram Moolenaar1bd999f2017-12-19 22:25:40 +01001278 call assert_fails("/\\%')", 'E486')
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01001279 call assert_fails("/", 'E486')
Bram Moolenaar1bd999f2017-12-19 22:25:40 +01001280 /\%'(
1281 /
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01001282endfunc
Bram Moolenaar2fb8f682018-12-01 13:14:45 +01001283
1284" Test that there is no crash when there is a last search pattern but no last
1285" substitute pattern.
1286func Test_no_last_substitute_pat()
1287 " Use viminfo to set the last search pattern to a string and make the last
1288 " substitute pattern the most recent used and make it empty (NULL).
1289 call writefile(['~MSle0/bar', '~MSle0~&'], 'Xviminfo')
1290 rviminfo! Xviminfo
1291 call assert_fails('normal n', 'E35:')
1292
1293 call delete('Xviminfo')
1294endfunc
Bram Moolenaar5f5e2032018-12-14 15:47:03 +01001295
1296func Test_search_Ctrl_L_combining()
1297 " Make sure, that Ctrl-L works correctly with combining characters.
1298 " It uses an artificial example of an 'a' with 4 combining chars:
Bram Moolenaar3ba35402019-12-21 22:00:50 +01001299 " 'a' U+0061 Dec:97 LATIN SMALL LETTER A &#x61; /\%u61\Z "\u0061"
Bram Moolenaar5f5e2032018-12-14 15:47:03 +01001300 " ' ̀' U+0300 Dec:768 COMBINING GRAVE ACCENT &#x300; /\%u300\Z "\u0300"
1301 " ' ́' U+0301 Dec:769 COMBINING ACUTE ACCENT &#x301; /\%u301\Z "\u0301"
1302 " ' ̇' U+0307 Dec:775 COMBINING DOT ABOVE &#x307; /\%u307\Z "\u0307"
Bram Moolenaar3ba35402019-12-21 22:00:50 +01001303 " ' ̣' U+0323 Dec:803 COMBINING DOT BELOW &#x323; /\%u323 "\u0323"
Bram Moolenaar5f5e2032018-12-14 15:47:03 +01001304 " Those should also appear on the commandline
Bram Moolenaar30276f22019-01-24 17:59:39 +01001305 if !exists('+incsearch')
Bram Moolenaar5f5e2032018-12-14 15:47:03 +01001306 return
1307 endif
1308 call Cmdline3_prep()
1309 1
1310 let bufcontent = ['', 'Miạ̀́̇m']
1311 call append('$', bufcontent)
1312 call feedkeys("/Mi\<c-l>\<c-l>\<cr>", 'tx')
1313 call assert_equal(5, line('.'))
1314 call assert_equal(bufcontent[1], @/)
1315 call Incsearch_cleanup()
1316endfunc
Bram Moolenaar527a2d82019-02-21 22:28:51 +01001317
Bram Moolenaarab350f82019-02-28 06:25:00 +01001318func Test_large_hex_chars1()
Bram Moolenaar527a2d82019-02-21 22:28:51 +01001319 " This used to cause a crash, the character becomes an NFA state.
1320 try
1321 /\%Ufffffc23
1322 catch
1323 call assert_match('E678:', v:exception)
1324 endtry
Bram Moolenaarab350f82019-02-28 06:25:00 +01001325 try
1326 set re=1
1327 /\%Ufffffc23
1328 catch
1329 call assert_match('E678:', v:exception)
1330 endtry
1331 set re&
1332endfunc
1333
1334func Test_large_hex_chars2()
1335 " This used to cause a crash, the character becomes an NFA state.
1336 try
1337 /[\Ufffffc1f]
1338 catch
1339 call assert_match('E486:', v:exception)
1340 endtry
1341 try
1342 set re=1
1343 /[\Ufffffc1f]
1344 catch
1345 call assert_match('E486:', v:exception)
1346 endtry
1347 set re&
Bram Moolenaar527a2d82019-02-21 22:28:51 +01001348endfunc
Bram Moolenaarcd625122019-02-22 17:29:43 +01001349
1350func Test_one_error_msg()
1351 " This was also giving an internal error
1352 call assert_fails('call search(" \\((\\v[[=P=]]){185}+ ")', 'E871:')
1353endfunc
Bram Moolenaar730f48f2019-04-11 13:45:57 +02001354
1355func Test_incsearch_add_char_under_cursor()
1356 if !exists('+incsearch')
1357 return
1358 endif
1359 set incsearch
1360 new
1361 call setline(1, ['find match', 'anything'])
1362 1
1363 call test_override('char_avail', 1)
1364 call feedkeys("fc/m\<C-L>\<C-L>\<C-L>\<C-L>\<C-L>\<CR>", 'tx')
1365 call assert_equal('match', @/)
1366 call test_override('char_avail', 0)
1367
1368 set incsearch&
1369 bwipe!
1370endfunc
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02001371
1372" Test for the search() function with match at the cursor position
1373func Test_search_match_at_curpos()
1374 new
1375 call append(0, ['foobar', '', 'one two', ''])
1376
1377 normal gg
1378
Bram Moolenaar196b4662019-09-06 21:34:30 +02001379 eval 'foobar'->search('c')
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02001380 call assert_equal([1, 1], [line('.'), col('.')])
1381
1382 normal j
1383 call search('^$', 'c')
1384 call assert_equal([2, 1], [line('.'), col('.')])
1385
1386 call search('^$', 'bc')
1387 call assert_equal([2, 1], [line('.'), col('.')])
1388
1389 exe "normal /two\<CR>"
1390 call search('.', 'c')
1391 call assert_equal([3, 5], [line('.'), col('.')])
1392
1393 close!
1394endfunc
Bram Moolenaardb294ad2019-06-06 12:49:29 +02001395
1396func Test_search_display_pattern()
1397 new
1398 call setline(1, ['foo', 'bar', 'foobar'])
1399
1400 call cursor(1, 1)
1401 let @/ = 'foo'
Bram Moolenaara4208962019-08-24 20:50:19 +02001402 let pat = @/->escape('()*?'. '\s\+')
Bram Moolenaardb294ad2019-06-06 12:49:29 +02001403 let g:a = execute(':unsilent :norm! n')
1404 call assert_match(pat, g:a)
1405
1406 " right-left
1407 if exists("+rightleft")
1408 set rl
1409 call cursor(1, 1)
1410 let @/ = 'foo'
1411 let pat = 'oof/\s\+'
1412 let g:a = execute(':unsilent :norm! n')
1413 call assert_match(pat, g:a)
1414 set norl
1415 endif
1416endfunc
Bram Moolenaar196b4662019-09-06 21:34:30 +02001417
1418func Test_searchdecl()
1419 let lines =<< trim END
1420 int global;
1421
1422 func()
1423 {
1424 int global;
1425 if (cond) {
1426 int local;
1427 }
1428 int local;
1429 // comment
1430 }
1431 END
1432 new
1433 call setline(1, lines)
1434 10
1435 call assert_equal(0, searchdecl('local', 0, 0))
1436 call assert_equal(7, getcurpos()[1])
1437
1438 10
1439 call assert_equal(0, 'local'->searchdecl(0, 1))
1440 call assert_equal(9, getcurpos()[1])
1441
1442 10
1443 call assert_equal(0, searchdecl('global'))
1444 call assert_equal(5, getcurpos()[1])
1445
1446 10
1447 call assert_equal(0, searchdecl('global', 1))
1448 call assert_equal(1, getcurpos()[1])
1449
1450 bwipe!
1451endfunc
Bram Moolenaar98a336d2020-01-20 20:22:30 +01001452
1453func Test_search_special()
Bram Moolenaarfe4bbac2020-01-20 21:12:20 +01001454 " this was causing illegal memory access and an endless loop
1455 set t_PE=
Bram Moolenaar98a336d2020-01-20 20:22:30 +01001456 exe "norm /\x80PS"
1457endfunc
Bram Moolenaar07ada5f2020-02-05 20:38:22 +01001458
1459" Test for command failures when the last search pattern is not set.
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001460" Need to run this in a new vim instance where last search pattern is not set.
Bram Moolenaar07ada5f2020-02-05 20:38:22 +01001461func Test_search_with_no_last_pat()
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001462 let lines =<< trim [SCRIPT]
1463 call assert_fails("normal i\<C-R>/\e", 'E35:')
1464 call assert_fails("exe '/'", 'E35:')
1465 call assert_fails("exe '?'", 'E35:')
1466 call assert_fails("/", 'E35:')
1467 call assert_fails("?", 'E35:')
1468 call assert_fails("normal n", 'E35:')
1469 call assert_fails("normal N", 'E35:')
1470 call assert_fails("normal gn", 'E35:')
1471 call assert_fails("normal gN", 'E35:')
1472 call assert_fails("normal cgn", 'E35:')
1473 call assert_fails("normal cgN", 'E35:')
1474 let p = []
1475 let p = @/
1476 call assert_equal('', p)
1477 call assert_fails("normal :\<C-R>/", 'E35:')
1478 call assert_fails("//p", 'E35:')
1479 call assert_fails(";//p", 'E35:')
1480 call assert_fails("??p", 'E35:')
1481 call assert_fails(";??p", 'E35:')
1482 call assert_fails('g//p', 'E476:')
1483 call assert_fails('v//p', 'E476:')
1484 call writefile(v:errors, 'Xresult')
1485 qall!
1486 [SCRIPT]
1487 call writefile(lines, 'Xscript')
1488
1489 if RunVim([], [], '--clean -S Xscript')
1490 call assert_equal([], readfile('Xresult'))
1491 endif
1492 call delete('Xscript')
1493 call delete('Xresult')
Bram Moolenaar07ada5f2020-02-05 20:38:22 +01001494endfunc
1495
1496" Test for using tilde (~) atom in search. This should use the last used
1497" substitute pattern
1498func Test_search_tilde_pat()
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001499 let lines =<< trim [SCRIPT]
1500 set regexpengine=1
1501 call assert_fails('exe "normal /~\<CR>"', 'E33:')
1502 call assert_fails('exe "normal ?~\<CR>"', 'E33:')
1503 set regexpengine=2
1504 call assert_fails('exe "normal /~\<CR>"', 'E383:')
1505 call assert_fails('exe "normal ?~\<CR>"', 'E383:')
1506 set regexpengine&
1507 call writefile(v:errors, 'Xresult')
1508 qall!
1509 [SCRIPT]
1510 call writefile(lines, 'Xscript')
1511 if RunVim([], [], '--clean -S Xscript')
1512 call assert_equal([], readfile('Xresult'))
1513 endif
1514 call delete('Xscript')
1515 call delete('Xresult')
Bram Moolenaar07ada5f2020-02-05 20:38:22 +01001516endfunc
1517
1518" vim: shiftwidth=2 sts=2 expandtab