blob: d57dbd7e5b8ddb822bb729cb47440098b7d30bd6 [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()
Bram Moolenaarb68df222020-03-19 15:05:28 +01008 CheckOption incsearch
9
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020010 " need to disable char_avail,
11 " so that expansion of commandline works
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012 call test_override("char_avail", 1)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020013 new
14 call setline(1, [' 1', ' 2 these', ' 3 the', ' 4 their', ' 5 there', ' 6 their', ' 7 the', ' 8 them', ' 9 these', ' 10 foobar'])
15 " Test 1
16 " CTRL-N / CTRL-P skips through the previous search history
17 set noincsearch
18 :1
19 call feedkeys("/foobar\<cr>", 'tx')
20 call feedkeys("/the\<cr>",'tx')
21 call assert_equal('the', @/)
Bram Moolenaar11956692016-08-27 16:26:56 +020022 call feedkeys("/thes\<C-P>\<C-P>\<cr>",'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020023 call assert_equal('foobar', @/)
24
25 " Test 2
Bram Moolenaar11956692016-08-27 16:26:56 +020026 " Ctrl-G goes from one match to the next
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020027 " until the end of the buffer
28 set incsearch nowrapscan
29 :1
30 " first match
31 call feedkeys("/the\<cr>", 'tx')
32 call assert_equal(' 2 these', getline('.'))
33 :1
34 " second match
Bram Moolenaar11956692016-08-27 16:26:56 +020035 call feedkeys("/the\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020036 call assert_equal(' 3 the', getline('.'))
Bram Moolenaardda933d2016-09-03 21:04:58 +020037 call assert_equal([0, 0, 0, 0], getpos('"'))
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020038 :1
39 " third match
Bram Moolenaar11956692016-08-27 16:26:56 +020040 call feedkeys("/the".repeat("\<C-G>", 2)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020041 call assert_equal(' 4 their', getline('.'))
42 :1
43 " fourth match
Bram Moolenaar11956692016-08-27 16:26:56 +020044 call feedkeys("/the".repeat("\<C-G>", 3)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020045 call assert_equal(' 5 there', getline('.'))
46 :1
47 " fifth match
Bram Moolenaar11956692016-08-27 16:26:56 +020048 call feedkeys("/the".repeat("\<C-G>", 4)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020049 call assert_equal(' 6 their', getline('.'))
50 :1
51 " sixth match
Bram Moolenaar11956692016-08-27 16:26:56 +020052 call feedkeys("/the".repeat("\<C-G>", 5)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020053 call assert_equal(' 7 the', getline('.'))
54 :1
55 " seventh match
Bram Moolenaar11956692016-08-27 16:26:56 +020056 call feedkeys("/the".repeat("\<C-G>", 6)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020057 call assert_equal(' 8 them', getline('.'))
58 :1
Bram Moolenaar3ba35402019-12-21 22:00:50 +010059 " eighth match
Bram Moolenaar11956692016-08-27 16:26:56 +020060 call feedkeys("/the".repeat("\<C-G>", 7)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020061 call assert_equal(' 9 these', getline('.'))
62 :1
63 " no further match
Bram Moolenaar11956692016-08-27 16:26:56 +020064 call feedkeys("/the".repeat("\<C-G>", 8)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020065 call assert_equal(' 9 these', getline('.'))
Bram Moolenaardda933d2016-09-03 21:04:58 +020066 call assert_equal([0, 0, 0, 0], getpos('"'))
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020067
68 " Test 3
Bram Moolenaar11956692016-08-27 16:26:56 +020069 " Ctrl-G goes from one match to the next
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020070 " and continues back at the top
71 set incsearch wrapscan
72 :1
73 " first match
74 call feedkeys("/the\<cr>", 'tx')
75 call assert_equal(' 2 these', getline('.'))
76 :1
77 " second match
Bram Moolenaar11956692016-08-27 16:26:56 +020078 call feedkeys("/the\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020079 call assert_equal(' 3 the', getline('.'))
80 :1
81 " third match
Bram Moolenaar11956692016-08-27 16:26:56 +020082 call feedkeys("/the".repeat("\<C-G>", 2)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020083 call assert_equal(' 4 their', getline('.'))
84 :1
85 " fourth match
Bram Moolenaar11956692016-08-27 16:26:56 +020086 call feedkeys("/the".repeat("\<C-G>", 3)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020087 call assert_equal(' 5 there', getline('.'))
88 :1
89 " fifth match
Bram Moolenaar11956692016-08-27 16:26:56 +020090 call feedkeys("/the".repeat("\<C-G>", 4)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020091 call assert_equal(' 6 their', getline('.'))
92 :1
93 " sixth match
Bram Moolenaar11956692016-08-27 16:26:56 +020094 call feedkeys("/the".repeat("\<C-G>", 5)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020095 call assert_equal(' 7 the', getline('.'))
96 :1
97 " seventh match
Bram Moolenaar11956692016-08-27 16:26:56 +020098 call feedkeys("/the".repeat("\<C-G>", 6)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020099 call assert_equal(' 8 them', getline('.'))
100 :1
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100101 " eighth match
Bram Moolenaar11956692016-08-27 16:26:56 +0200102 call feedkeys("/the".repeat("\<C-G>", 7)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200103 call assert_equal(' 9 these', getline('.'))
104 :1
105 " back at first match
Bram Moolenaar11956692016-08-27 16:26:56 +0200106 call feedkeys("/the".repeat("\<C-G>", 8)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200107 call assert_equal(' 2 these', getline('.'))
108
109 " Test 4
Bram Moolenaar11956692016-08-27 16:26:56 +0200110 " CTRL-T goes to the previous match
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200111 set incsearch nowrapscan
112 $
113 " first match
114 call feedkeys("?the\<cr>", 'tx')
115 call assert_equal(' 9 these', getline('.'))
116 $
117 " first match
Bram Moolenaar11956692016-08-27 16:26:56 +0200118 call feedkeys("?the\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200119 call assert_equal(' 9 these', getline('.'))
120 $
121 " second match
Bram Moolenaar11956692016-08-27 16:26:56 +0200122 call feedkeys("?the".repeat("\<C-T>", 1)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200123 call assert_equal(' 8 them', getline('.'))
124 $
125 " last match
Bram Moolenaar11956692016-08-27 16:26:56 +0200126 call feedkeys("?the".repeat("\<C-T>", 7)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200127 call assert_equal(' 2 these', getline('.'))
128 $
129 " last match
Bram Moolenaar11956692016-08-27 16:26:56 +0200130 call feedkeys("?the".repeat("\<C-T>", 8)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200131 call assert_equal(' 2 these', getline('.'))
132
133 " Test 5
Bram Moolenaar11956692016-08-27 16:26:56 +0200134 " CTRL-T goes to the previous match
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200135 set incsearch wrapscan
136 $
137 " first match
138 call feedkeys("?the\<cr>", 'tx')
139 call assert_equal(' 9 these', getline('.'))
140 $
141 " first match at the top
Bram Moolenaar11956692016-08-27 16:26:56 +0200142 call feedkeys("?the\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200143 call assert_equal(' 2 these', getline('.'))
144 $
145 " second match
Bram Moolenaar11956692016-08-27 16:26:56 +0200146 call feedkeys("?the".repeat("\<C-T>", 1)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200147 call assert_equal(' 8 them', getline('.'))
148 $
149 " last match
Bram Moolenaar11956692016-08-27 16:26:56 +0200150 call feedkeys("?the".repeat("\<C-T>", 7)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200151 call assert_equal(' 2 these', getline('.'))
152 $
153 " back at the bottom of the buffer
Bram Moolenaar11956692016-08-27 16:26:56 +0200154 call feedkeys("?the".repeat("\<C-T>", 8)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200155 call assert_equal(' 9 these', getline('.'))
156
157 " Test 6
158 " CTRL-L adds to the search pattern
159 set incsearch wrapscan
160 1
161 " first match
162 call feedkeys("/the\<c-l>\<cr>", 'tx')
163 call assert_equal(' 2 these', getline('.'))
164 1
165 " go to next match of 'thes'
Bram Moolenaar11956692016-08-27 16:26:56 +0200166 call feedkeys("/the\<c-l>\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200167 call assert_equal(' 9 these', getline('.'))
168 1
169 " wrap around
Bram Moolenaar11956692016-08-27 16:26:56 +0200170 call feedkeys("/the\<c-l>\<C-G>\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200171 call assert_equal(' 2 these', getline('.'))
172 1
173 " wrap around
174 set nowrapscan
Bram Moolenaar11956692016-08-27 16:26:56 +0200175 call feedkeys("/the\<c-l>\<C-G>\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200176 call assert_equal(' 9 these', getline('.'))
177
178 " Test 7
179 " <bs> remove from match, but stay at current match
180 set incsearch wrapscan
181 1
182 " first match
183 call feedkeys("/thei\<cr>", 'tx')
184 call assert_equal(' 4 their', getline('.'))
185 1
186 " delete one char, add another
187 call feedkeys("/thei\<bs>s\<cr>", 'tx')
Bram Moolenaardda933d2016-09-03 21:04:58 +0200188 call assert_equal(' 2 these', getline('.'))
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200189 1
190 " delete one char, add another, go to previous match, add one char
Bram Moolenaar11956692016-08-27 16:26:56 +0200191 call feedkeys("/thei\<bs>s\<bs>\<C-T>\<c-l>\<cr>", 'tx')
Bram Moolenaardda933d2016-09-03 21:04:58 +0200192 call assert_equal(' 9 these', getline('.'))
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200193 1
194 " delete all chars, start from the beginning again
195 call feedkeys("/them". repeat("\<bs>",4).'the\>'."\<cr>", 'tx')
196 call assert_equal(' 3 the', getline('.'))
197
198 " clean up
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100199 call test_override("char_avail", 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200200 bw!
201endfunc
202
203func Test_search_cmdline2()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100204 CheckOption incsearch
205
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200206 " need to disable char_avail,
207 " so that expansion of commandline works
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100208 call test_override("char_avail", 1)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200209 new
210 call setline(1, [' 1', ' 2 these', ' 3 the theother'])
211 " Test 1
Bram Moolenaar11956692016-08-27 16:26:56 +0200212 " Ctrl-T goes correctly back and forth
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200213 set incsearch
214 1
215 " first match
216 call feedkeys("/the\<cr>", 'tx')
217 call assert_equal(' 2 these', getline('.'))
218 1
219 " go to next match (on next line)
Bram Moolenaar11956692016-08-27 16:26:56 +0200220 call feedkeys("/the\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200221 call assert_equal(' 3 the theother', getline('.'))
222 1
223 " go to next match (still on line 3)
Bram Moolenaar11956692016-08-27 16:26:56 +0200224 call feedkeys("/the\<C-G>\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200225 call assert_equal(' 3 the theother', getline('.'))
226 1
227 " go to next match (still on line 3)
Bram Moolenaar11956692016-08-27 16:26:56 +0200228 call feedkeys("/the\<C-G>\<C-G>\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200229 call assert_equal(' 3 the theother', getline('.'))
230 1
231 " go to previous match (on line 3)
Bram Moolenaar11956692016-08-27 16:26:56 +0200232 call feedkeys("/the\<C-G>\<C-G>\<C-G>\<C-T>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200233 call assert_equal(' 3 the theother', getline('.'))
234 1
235 " go to previous match (on line 3)
Bram Moolenaar11956692016-08-27 16:26:56 +0200236 call feedkeys("/the\<C-G>\<C-G>\<C-G>\<C-T>\<C-T>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200237 call assert_equal(' 3 the theother', getline('.'))
238 1
239 " go to previous match (on line 2)
Bram Moolenaar11956692016-08-27 16:26:56 +0200240 call feedkeys("/the\<C-G>\<C-G>\<C-G>\<C-T>\<C-T>\<C-T>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200241 call assert_equal(' 2 these', getline('.'))
Bram Moolenaar69a5b862019-07-16 21:38:51 +0200242 1
243 " go to previous match (on line 2)
244 call feedkeys("/the\<C-G>\<C-R>\<C-W>\<cr>", 'tx')
245 call assert_equal('theother', @/)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200246
Bram Moolenaardda933d2016-09-03 21:04:58 +0200247 " Test 2: keep the view,
248 " after deleting a character from the search cmd
249 call setline(1, [' 1', ' 2 these', ' 3 the', ' 4 their', ' 5 there', ' 6 their', ' 7 the', ' 8 them', ' 9 these', ' 10 foobar'])
250 resize 5
251 1
252 call feedkeys("/foo\<bs>\<cr>", 'tx')
253 redraw
254 call assert_equal({'lnum': 10, 'leftcol': 0, 'col': 4, 'topfill': 0, 'topline': 6, 'coladd': 0, 'skipcol': 0, 'curswant': 4}, winsaveview())
255
256 " remove all history entries
Bram Moolenaar69a5b862019-07-16 21:38:51 +0200257 for i in range(11)
Bram Moolenaardda933d2016-09-03 21:04:58 +0200258 call histdel('/')
259 endfor
260
261 " Test 3: reset the view,
262 " after deleting all characters from the search cmd
263 norm! 1gg0
264 " unfortunately, neither "/foo\<c-w>\<cr>", nor "/foo\<bs>\<bs>\<bs>\<cr>",
265 " nor "/foo\<c-u>\<cr>" works to delete the commandline.
266 " In that case Vim should return "E35 no previous regular expression",
267 " but it looks like Vim still sees /foo and therefore the test fails.
Bram Moolenaar1bc353b2019-09-01 14:45:28 +0200268 " Therefore, disabling this test
Bram Moolenaardda933d2016-09-03 21:04:58 +0200269 "call assert_fails(feedkeys("/foo\<c-w>\<cr>", 'tx'), 'E35')
270 "call assert_equal({'lnum': 1, 'leftcol': 0, 'col': 0, 'topfill': 0, 'topline': 1, 'coladd': 0, 'skipcol': 0, 'curswant': 0}, winsaveview())
271
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200272 " clean up
Bram Moolenaardda933d2016-09-03 21:04:58 +0200273 set noincsearch
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100274 call test_override("char_avail", 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200275 bw!
276endfunc
Bram Moolenaarea683da2016-09-09 21:41:34 +0200277
278func Test_use_sub_pat()
279 split
280 let @/ = ''
281 func X()
282 s/^/a/
283 /
284 endfunc
285 call X()
286 bwipe!
287endfunc
Bram Moolenaar6e450a52017-01-06 20:03:58 +0100288
289func Test_searchpair()
290 new
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100291 call setline(1, ['other code', 'here [', ' [', ' " cursor here', ' ]]'])
292
Bram Moolenaar6e450a52017-01-06 20:03:58 +0100293 4
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100294 call assert_equal(3, searchpair('\[', '', ']', 'bW'))
295 call assert_equal([0, 3, 2, 0], getpos('.'))
296 4
297 call assert_equal(2, searchpair('\[', '', ']', 'bWr'))
298 call assert_equal([0, 2, 6, 0], getpos('.'))
299 4
300 call assert_equal(1, searchpair('\[', '', ']', 'bWm'))
301 call assert_equal([0, 3, 2, 0], getpos('.'))
302 4|norm ^
303 call assert_equal(5, searchpair('\[', '', ']', 'Wn'))
304 call assert_equal([0, 4, 2, 0], getpos('.'))
305 4
306 call assert_equal(2, searchpair('\[', '', ']', 'bW',
307 \ 'getline(".") =~ "^\\s*\["'))
308 call assert_equal([0, 2, 6, 0], getpos('.'))
Bram Moolenaar6e450a52017-01-06 20:03:58 +0100309 set nomagic
310 4
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100311 call assert_equal(3, searchpair('\[', '', ']', 'bW'))
312 call assert_equal([0, 3, 2, 0], getpos('.'))
Bram Moolenaar6e450a52017-01-06 20:03:58 +0100313 set magic
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100314 4|norm ^
315 call assert_equal(0, searchpair('{', '', '}', 'bW'))
316 call assert_equal([0, 4, 2, 0], getpos('.'))
317
318 %d
319 call setline(1, ['if 1', ' if 2', ' else', ' endif 2', 'endif 1'])
320
321 /\<if 1
322 call assert_equal(5, searchpair('\<if\>', '\<else\>', '\<endif\>', 'W'))
323 call assert_equal([0, 5, 1, 0], getpos('.'))
324 /\<if 2
325 call assert_equal(3, searchpair('\<if\>', '\<else\>', '\<endif\>', 'W'))
326 call assert_equal([0, 3, 3, 0], getpos('.'))
327
328 q!
329endfunc
330
331func Test_searchpairpos()
332 new
333 call setline(1, ['other code', 'here [', ' [', ' " cursor here', ' ]]'])
334
335 4
336 call assert_equal([3, 2], searchpairpos('\[', '', ']', 'bW'))
337 call assert_equal([0, 3, 2, 0], getpos('.'))
338 4
339 call assert_equal([2, 6], searchpairpos('\[', '', ']', 'bWr'))
340 call assert_equal([0, 2, 6, 0], getpos('.'))
341 4|norm ^
342 call assert_equal([5, 2], searchpairpos('\[', '', ']', 'Wn'))
343 call assert_equal([0, 4, 2, 0], getpos('.'))
344 4
345 call assert_equal([2, 6], searchpairpos('\[', '', ']', 'bW',
346 \ 'getline(".") =~ "^\\s*\["'))
347 call assert_equal([0, 2, 6, 0], getpos('.'))
348 4
349 call assert_equal([2, 6], searchpairpos('\[', '', ']', 'bWr'))
350 call assert_equal([0, 2, 6, 0], getpos('.'))
351 set nomagic
352 4
353 call assert_equal([3, 2], searchpairpos('\[', '', ']', 'bW'))
354 call assert_equal([0, 3, 2, 0], getpos('.'))
355 set magic
356 4|norm ^
357 call assert_equal([0, 0], searchpairpos('{', '', '}', 'bW'))
358 call assert_equal([0, 4, 2, 0], getpos('.'))
359
360 %d
361 call setline(1, ['if 1', ' if 2', ' else', ' endif 2', 'endif 1'])
362 /\<if 1
363 call assert_equal([5, 1], searchpairpos('\<if\>', '\<else\>', '\<endif\>', 'W'))
364 call assert_equal([0, 5, 1, 0], getpos('.'))
365 /\<if 2
366 call assert_equal([3, 3], searchpairpos('\<if\>', '\<else\>', '\<endif\>', 'W'))
367 call assert_equal([0, 3, 3, 0], getpos('.'))
368
Bram Moolenaar6e450a52017-01-06 20:03:58 +0100369 q!
370endfunc
371
Bram Moolenaar3dddb092018-06-24 19:01:59 +0200372func Test_searchpair_errors()
373 call assert_fails("call searchpair([0], 'middle', 'end', 'bW', 'skip', 99, 100)", 'E730: using List as a String')
374 call assert_fails("call searchpair('start', {-> 0}, 'end', 'bW', 'skip', 99, 100)", 'E729: using Funcref as a String')
375 call assert_fails("call searchpair('start', 'middle', {'one': 1}, 'bW', 'skip', 99, 100)", 'E731: using Dictionary as a String')
376 call assert_fails("call searchpair('start', 'middle', 'end', 'flags', 'skip', 99, 100)", 'E475: Invalid argument: flags')
377 call assert_fails("call searchpair('start', 'middle', 'end', 'bW', 0, 99, 100)", 'E475: Invalid argument: 0')
378 call assert_fails("call searchpair('start', 'middle', 'end', 'bW', 'func', -99, 100)", 'E475: Invalid argument: -99')
379 call assert_fails("call searchpair('start', 'middle', 'end', 'bW', 'func', 99, -100)", 'E475: Invalid argument: -100')
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100380 call assert_fails("call searchpair('start', 'middle', 'end', 'e')", 'E475: Invalid argument: e')
381 call assert_fails("call searchpair('start', 'middle', 'end', 'sn')", 'E475: Invalid argument: sn')
382endfunc
383
384func Test_searchpairpos_errors()
385 call assert_fails("call searchpairpos([0], 'middle', 'end', 'bW', 'skip', 99, 100)", 'E730: using List as a String')
386 call assert_fails("call searchpairpos('start', {-> 0}, 'end', 'bW', 'skip', 99, 100)", 'E729: using Funcref as a String')
387 call assert_fails("call searchpairpos('start', 'middle', {'one': 1}, 'bW', 'skip', 99, 100)", 'E731: using Dictionary as a String')
388 call assert_fails("call searchpairpos('start', 'middle', 'end', 'flags', 'skip', 99, 100)", 'E475: Invalid argument: flags')
389 call assert_fails("call searchpairpos('start', 'middle', 'end', 'bW', 0, 99, 100)", 'E475: Invalid argument: 0')
390 call assert_fails("call searchpairpos('start', 'middle', 'end', 'bW', 'func', -99, 100)", 'E475: Invalid argument: -99')
391 call assert_fails("call searchpairpos('start', 'middle', 'end', 'bW', 'func', 99, -100)", 'E475: Invalid argument: -100')
392 call assert_fails("call searchpairpos('start', 'middle', 'end', 'e')", 'E475: Invalid argument: e')
393 call assert_fails("call searchpairpos('start', 'middle', 'end', 'sn')", 'E475: Invalid argument: sn')
Bram Moolenaar3dddb092018-06-24 19:01:59 +0200394endfunc
395
Bram Moolenaar48570482017-10-30 21:48:41 +0100396func Test_searchpair_skip()
397 func Zero()
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100398 return 0
Bram Moolenaar48570482017-10-30 21:48:41 +0100399 endfunc
400 func Partial(x)
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100401 return a:x
Bram Moolenaar48570482017-10-30 21:48:41 +0100402 endfunc
403 new
404 call setline(1, ['{', 'foo', 'foo', 'foo', '}'])
405 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', ''))
406 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', '0'))
407 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', {-> 0}))
408 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', function('Zero')))
409 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', function('Partial', [0])))
Bram Moolenaar48570482017-10-30 21:48:41 +0100410 bw!
411endfunc
412
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200413func Test_searchpair_leak()
414 new
415 call setline(1, 'if one else another endif')
416
417 " The error in the skip expression caused memory to leak.
418 call assert_fails("call searchpair('\\<if\\>', '\\<else\\>', '\\<endif\\>', '', '\"foo\" 2')", 'E15:')
419
420 bwipe!
421endfunc
422
Bram Moolenaar66727e12017-03-01 22:17:05 +0100423func Test_searchc()
424 " These commands used to cause memory overflow in searchc().
425 new
426 norm ixx
427 exe "norm 0t\u93cf"
428 bw!
429endfunc
Bram Moolenaara693d052017-06-29 22:23:06 +0200430
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200431func Cmdline3_prep()
432 " need to disable char_avail,
433 " so that expansion of commandline works
434 call test_override("char_avail", 1)
435 new
436 call setline(1, [' 1', ' 2 the~e', ' 3 the theother'])
437 set incsearch
438endfunc
439
Bram Moolenaar976b8472018-08-12 15:49:47 +0200440func Incsearch_cleanup()
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200441 set noincsearch
442 call test_override("char_avail", 0)
443 bw!
444endfunc
445
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200446func Test_search_cmdline3()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100447 CheckOption incsearch
448
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200449 call Cmdline3_prep()
450 1
451 " first match
452 call feedkeys("/the\<c-l>\<cr>", 'tx')
453 call assert_equal(' 2 the~e', getline('.'))
454
Bram Moolenaar976b8472018-08-12 15:49:47 +0200455 call Incsearch_cleanup()
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200456endfunc
457
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200458func Test_search_cmdline3s()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100459 CheckOption incsearch
460
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200461 call Cmdline3_prep()
462 1
463 call feedkeys(":%s/the\<c-l>/xxx\<cr>", 'tx')
464 call assert_equal(' 2 xxxe', getline('.'))
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200465 undo
466 call feedkeys(":%subs/the\<c-l>/xxx\<cr>", 'tx')
467 call assert_equal(' 2 xxxe', getline('.'))
468 undo
469 call feedkeys(":%substitute/the\<c-l>/xxx\<cr>", 'tx')
470 call assert_equal(' 2 xxxe', getline('.'))
Bram Moolenaar167ae422018-08-14 21:32:21 +0200471 undo
472 call feedkeys(":%smagic/the.e/xxx\<cr>", 'tx')
473 call assert_equal(' 2 xxx', getline('.'))
474 undo
475 call assert_fails(":%snomagic/the.e/xxx\<cr>", 'E486')
476 "
477 call feedkeys(":%snomagic/the\\.e/xxx\<cr>", 'tx')
478 call assert_equal(' 2 xxx', getline('.'))
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200479
Bram Moolenaar976b8472018-08-12 15:49:47 +0200480 call Incsearch_cleanup()
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200481endfunc
482
483func Test_search_cmdline3g()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100484 CheckOption incsearch
485
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200486 call Cmdline3_prep()
487 1
488 call feedkeys(":g/the\<c-l>/d\<cr>", 'tx')
489 call assert_equal(' 3 the theother', getline(2))
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200490 undo
491 call feedkeys(":global/the\<c-l>/d\<cr>", 'tx')
492 call assert_equal(' 3 the theother', getline(2))
Bram Moolenaardef7b1d2018-08-13 22:54:35 +0200493 undo
494 call feedkeys(":g!/the\<c-l>/d\<cr>", 'tx')
495 call assert_equal(1, line('$'))
496 call assert_equal(' 2 the~e', getline(1))
497 undo
498 call feedkeys(":global!/the\<c-l>/d\<cr>", 'tx')
499 call assert_equal(1, line('$'))
500 call assert_equal(' 2 the~e', getline(1))
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200501
Bram Moolenaar976b8472018-08-12 15:49:47 +0200502 call Incsearch_cleanup()
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200503endfunc
504
505func Test_search_cmdline3v()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100506 CheckOption incsearch
507
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200508 call Cmdline3_prep()
509 1
510 call feedkeys(":v/the\<c-l>/d\<cr>", 'tx')
511 call assert_equal(1, line('$'))
512 call assert_equal(' 2 the~e', getline(1))
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200513 undo
514 call feedkeys(":vglobal/the\<c-l>/d\<cr>", 'tx')
515 call assert_equal(1, line('$'))
516 call assert_equal(' 2 the~e', getline(1))
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200517
Bram Moolenaar976b8472018-08-12 15:49:47 +0200518 call Incsearch_cleanup()
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200519endfunc
520
Bram Moolenaarda5116d2017-07-01 23:11:17 +0200521func Test_search_cmdline4()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100522 CheckOption incsearch
523
Bram Moolenaarda5116d2017-07-01 23:11:17 +0200524 " need to disable char_avail,
525 " so that expansion of commandline works
526 call test_override("char_avail", 1)
527 new
528 call setline(1, [' 1 the first', ' 2 the second', ' 3 the third'])
529 set incsearch
530 $
531 call feedkeys("?the\<c-g>\<cr>", 'tx')
532 call assert_equal(' 3 the third', getline('.'))
533 $
534 call feedkeys("?the\<c-g>\<c-g>\<cr>", 'tx')
535 call assert_equal(' 1 the first', getline('.'))
536 $
537 call feedkeys("?the\<c-g>\<c-g>\<c-g>\<cr>", 'tx')
538 call assert_equal(' 2 the second', getline('.'))
539 $
540 call feedkeys("?the\<c-t>\<cr>", 'tx')
541 call assert_equal(' 1 the first', getline('.'))
542 $
543 call feedkeys("?the\<c-t>\<c-t>\<cr>", 'tx')
544 call assert_equal(' 3 the third', getline('.'))
545 $
546 call feedkeys("?the\<c-t>\<c-t>\<c-t>\<cr>", 'tx')
547 call assert_equal(' 2 the second', getline('.'))
548 " clean up
549 set noincsearch
550 call test_override("char_avail", 0)
551 bw!
552endfunc
Bram Moolenaardb510072017-09-28 21:52:17 +0200553
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200554func Test_search_cmdline5()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100555 CheckOption incsearch
556
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200557 " Do not call test_override("char_avail", 1) so that <C-g> and <C-t> work
558 " regardless char_avail.
559 new
Bram Moolenaar69a5b862019-07-16 21:38:51 +0200560 call setline(1, [' 1 the first', ' 2 the second', ' 3 the third', ''])
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200561 set incsearch
562 1
563 call feedkeys("/the\<c-g>\<c-g>\<cr>", 'tx')
564 call assert_equal(' 3 the third', getline('.'))
565 $
566 call feedkeys("?the\<c-t>\<c-t>\<c-t>\<cr>", 'tx')
Bram Moolenaar69a5b862019-07-16 21:38:51 +0200567 call assert_equal(' 1 the first', getline('.'))
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200568 " clean up
569 set noincsearch
570 bw!
571endfunc
572
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100573func Test_search_cmdline6()
574 " Test that consecutive matches
575 " are caught by <c-g>/<c-t>
Bram Moolenaarb68df222020-03-19 15:05:28 +0100576 CheckOption incsearch
577
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100578 " need to disable char_avail,
579 " so that expansion of commandline works
580 call test_override("char_avail", 1)
581 new
582 call setline(1, [' bbvimb', ''])
583 set incsearch
584 " first match
585 norm! gg0
586 call feedkeys("/b\<cr>", 'tx')
587 call assert_equal([0,1,2,0], getpos('.'))
588 " second match
589 norm! gg0
590 call feedkeys("/b\<c-g>\<cr>", 'tx')
591 call assert_equal([0,1,3,0], getpos('.'))
592 " third match
593 norm! gg0
594 call feedkeys("/b\<c-g>\<c-g>\<cr>", 'tx')
595 call assert_equal([0,1,7,0], getpos('.'))
596 " first match again
597 norm! gg0
598 call feedkeys("/b\<c-g>\<c-g>\<c-g>\<cr>", 'tx')
599 call assert_equal([0,1,2,0], getpos('.'))
600 set nowrapscan
601 " last match
602 norm! gg0
603 call feedkeys("/b\<c-g>\<c-g>\<c-g>\<cr>", 'tx')
604 call assert_equal([0,1,7,0], getpos('.'))
605 " clean up
606 set wrapscan&vim
607 set noincsearch
608 call test_override("char_avail", 0)
609 bw!
610endfunc
611
612func Test_search_cmdline7()
613 " Test that an pressing <c-g> in an empty command line
614 " does not move the cursor
Bram Moolenaarb68df222020-03-19 15:05:28 +0100615 CheckOption incsearch
616
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100617 " need to disable char_avail,
618 " so that expansion of commandline works
619 call test_override("char_avail", 1)
620 new
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200621 let @/ = 'b'
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100622 call setline(1, [' bbvimb', ''])
623 set incsearch
624 " first match
625 norm! gg0
626 " moves to next match of previous search pattern, just like /<cr>
627 call feedkeys("/\<c-g>\<cr>", 'tx')
628 call assert_equal([0,1,2,0], getpos('.'))
629 " moves to next match of previous search pattern, just like /<cr>
630 call feedkeys("/\<cr>", 'tx')
631 call assert_equal([0,1,3,0], getpos('.'))
632 " moves to next match of previous search pattern, just like /<cr>
633 call feedkeys("/\<c-t>\<cr>", 'tx')
634 call assert_equal([0,1,7,0], getpos('.'))
Bram Moolenaard0480092017-11-16 22:20:39 +0100635
636 " using an offset uses the last search pattern
637 call cursor(1, 1)
638 call setline(1, ['1 bbvimb', ' 2 bbvimb'])
639 let @/ = 'b'
640 call feedkeys("//e\<c-g>\<cr>", 'tx')
641 call assert_equal('1 bbvimb', getline('.'))
642 call assert_equal(4, col('.'))
643
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100644 set noincsearch
645 call test_override("char_avail", 0)
646 bw!
647endfunc
648
649func Test_search_cmdline8()
650 " Highlighting is cleared in all windows
651 " since hls applies to all windows
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200652 CheckOption incsearch
653 CheckFeature terminal
654 CheckNotGui
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100655 if has("win32")
656 throw "Skipped: Bug with sending <ESC> to terminal window not fixed yet"
657 endif
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200658
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100659 let h = winheight(0)
660 if h < 3
661 return
662 endif
663 " Prepare buffer text
664 let lines = ['abb vim vim vi', 'vimvivim']
665 call writefile(lines, 'Xsearch.txt')
Bram Moolenaar13deab82017-11-04 18:48:43 +0100666 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', 'Xsearch.txt'], {'term_rows': 3})
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100667
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200668 call WaitForAssert({-> assert_equal(lines, [term_getline(buf, 1), term_getline(buf, 2)])})
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100669
Bram Moolenaar13deab82017-11-04 18:48:43 +0100670 call term_sendkeys(buf, ":set incsearch hlsearch\<cr>")
671 call term_sendkeys(buf, ":14vsp\<cr>")
672 call term_sendkeys(buf, "/vim\<cr>")
673 call term_sendkeys(buf, "/b\<esc>")
674 call term_sendkeys(buf, "gg0")
675 call term_wait(buf, 500)
676 let screen_line = term_scrape(buf, 1)
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100677 let [a0,a1,a2,a3] = [screen_line[3].attr, screen_line[4].attr,
678 \ screen_line[18].attr, screen_line[19].attr]
679 call assert_notequal(a0, a1)
680 call assert_notequal(a0, a3)
681 call assert_notequal(a1, a2)
682 call assert_equal(a0, a2)
683 call assert_equal(a1, a3)
684 " clean up
685 call delete('Xsearch.txt')
686
687 bwipe!
688endfunc
689
Bram Moolenaardb510072017-09-28 21:52:17 +0200690" Tests for regexp with various magic settings
691func Test_search_regexp()
692 enew!
693
694 put ='1 a aa abb abbccc'
695 exe 'normal! /a*b\{2}c\+/e' . "\<CR>"
696 call assert_equal([0, 2, 17, 0], getpos('.'))
697
698 put ='2 d dd dee deefff'
699 exe 'normal! /\Md\*e\{2}f\+/e' . "\<CR>"
700 call assert_equal([0, 3, 17, 0], getpos('.'))
701
702 set nomagic
703 put ='3 g gg ghh ghhiii'
704 exe 'normal! /g\*h\{2}i\+/e' . "\<CR>"
705 call assert_equal([0, 4, 17, 0], getpos('.'))
706
707 put ='4 j jj jkk jkklll'
708 exe 'normal! /\mj*k\{2}l\+/e' . "\<CR>"
709 call assert_equal([0, 5, 17, 0], getpos('.'))
710
711 put ='5 m mm mnn mnnooo'
712 exe 'normal! /\vm*n{2}o+/e' . "\<CR>"
713 call assert_equal([0, 6, 17, 0], getpos('.'))
714
715 put ='6 x ^aa$ x'
716 exe 'normal! /\V^aa$' . "\<CR>"
717 call assert_equal([0, 7, 5, 0], getpos('.'))
718
719 set magic
720 put ='7 (a)(b) abbaa'
721 exe 'normal! /\v(a)(b)\2\1\1/e' . "\<CR>"
722 call assert_equal([0, 8, 14, 0], getpos('.'))
723
724 put ='8 axx [ab]xx'
725 exe 'normal! /\V[ab]\(\[xy]\)\1' . "\<CR>"
726 call assert_equal([0, 9, 7, 0], getpos('.'))
727
728 set undolevels=100
729 put ='9 foobar'
730 put =''
731 exe "normal! a\<C-G>u\<Esc>"
732 normal G
733 exe 'normal! dv?bar?' . "\<CR>"
734 call assert_equal('9 foo', getline('.'))
735 call assert_equal([0, 10, 5, 0], getpos('.'))
736 call assert_equal(10, line('$'))
737 normal u
738 call assert_equal('9 foobar', getline('.'))
739 call assert_equal([0, 10, 6, 0], getpos('.'))
740 call assert_equal(11, line('$'))
741
742 set undolevels&
743 enew!
744endfunc
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100745
746func Test_search_cmdline_incsearch_highlight()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100747 CheckOption incsearch
748
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100749 set incsearch hlsearch
750 " need to disable char_avail,
751 " so that expansion of commandline works
752 call test_override("char_avail", 1)
753 new
754 call setline(1, ['aaa 1 the first', ' 2 the second', ' 3 the third'])
755
756 1
757 call feedkeys("/second\<cr>", 'tx')
758 call assert_equal('second', @/)
759 call assert_equal(' 2 the second', getline('.'))
760
761 " Canceling search won't change @/
762 1
763 let @/ = 'last pattern'
764 call feedkeys("/third\<C-c>", 'tx')
765 call assert_equal('last pattern', @/)
766 call feedkeys("/third\<Esc>", 'tx')
767 call assert_equal('last pattern', @/)
768 call feedkeys("/3\<bs>\<bs>", 'tx')
769 call assert_equal('last pattern', @/)
770 call feedkeys("/third\<c-g>\<c-t>\<Esc>", 'tx')
771 call assert_equal('last pattern', @/)
772
773 " clean up
774 set noincsearch nohlsearch
775 bw!
776endfunc
777
778func Test_search_cmdline_incsearch_highlight_attr()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200779 CheckOption incsearch
780 CheckFeature terminal
781 CheckNotGui
782
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100783 let h = winheight(0)
784 if h < 3
785 return
786 endif
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100787
788 " Prepare buffer text
Bram Moolenaar13deab82017-11-04 18:48:43 +0100789 let lines = ['abb vim vim vi', 'vimvivim']
790 call writefile(lines, 'Xsearch.txt')
791 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', 'Xsearch.txt'], {'term_rows': 3})
792
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200793 call WaitForAssert({-> assert_equal(lines, [term_getline(buf, 1), term_getline(buf, 2)])})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100794 " wait for vim to complete initialization
795 call term_wait(buf)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100796
797 " Get attr of normal(a0), incsearch(a1), hlsearch(a2) highlight
Bram Moolenaar13deab82017-11-04 18:48:43 +0100798 call term_sendkeys(buf, ":set incsearch hlsearch\<cr>")
799 call term_sendkeys(buf, '/b')
800 call term_wait(buf, 200)
801 let screen_line1 = term_scrape(buf, 1)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100802 call assert_true(len(screen_line1) > 2)
803 " a0: attr_normal
804 let a0 = screen_line1[0].attr
805 " a1: attr_incsearch
806 let a1 = screen_line1[1].attr
807 " a2: attr_hlsearch
808 let a2 = screen_line1[2].attr
809 call assert_notequal(a0, a1)
810 call assert_notequal(a0, a2)
811 call assert_notequal(a1, a2)
Bram Moolenaar13deab82017-11-04 18:48:43 +0100812 call term_sendkeys(buf, "\<cr>gg0")
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100813
814 " Test incremental highlight search
Bram Moolenaar13deab82017-11-04 18:48:43 +0100815 call term_sendkeys(buf, "/vim")
816 call term_wait(buf, 200)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100817 " Buffer:
818 " abb vim vim vi
819 " vimvivim
820 " Search: /vim
821 let attr_line1 = [a0,a0,a0,a0,a1,a1,a1,a0,a2,a2,a2,a0,a0,a0]
822 let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100823 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
824 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100825
826 " Test <C-g>
Bram Moolenaar13deab82017-11-04 18:48:43 +0100827 call term_sendkeys(buf, "\<C-g>\<C-g>")
828 call term_wait(buf, 200)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100829 let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a2,a2,a2,a0,a0,a0]
830 let attr_line2 = [a1,a1,a1,a0,a0,a2,a2,a2]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100831 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
832 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100833
834 " Test <C-t>
Bram Moolenaar13deab82017-11-04 18:48:43 +0100835 call term_sendkeys(buf, "\<C-t>")
836 call term_wait(buf, 200)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100837 let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a1,a1,a1,a0,a0,a0]
838 let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100839 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
840 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100841
842 " Type Enter and a1(incsearch highlight) should become a2(hlsearch highlight)
Bram Moolenaar13deab82017-11-04 18:48:43 +0100843 call term_sendkeys(buf, "\<cr>")
844 call term_wait(buf, 200)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100845 let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a2,a2,a2,a0,a0,a0]
846 let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100847 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
848 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100849
850 " Test nohlsearch. a2(hlsearch highlight) should become a0(normal highlight)
Bram Moolenaar13deab82017-11-04 18:48:43 +0100851 call term_sendkeys(buf, ":1\<cr>")
852 call term_sendkeys(buf, ":set nohlsearch\<cr>")
853 call term_sendkeys(buf, "/vim")
854 call term_wait(buf, 200)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100855 let attr_line1 = [a0,a0,a0,a0,a1,a1,a1,a0,a0,a0,a0,a0,a0,a0]
856 let attr_line2 = [a0,a0,a0,a0,a0,a0,a0,a0]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100857 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
858 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100859 call delete('Xsearch.txt')
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100860
Bram Moolenaarb94340c2017-11-02 16:16:31 +0100861 call delete('Xsearch.txt')
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100862 bwipe!
863endfunc
Bram Moolenaarf45938c2017-11-02 15:59:57 +0100864
Bram Moolenaar548e5982018-12-26 21:45:00 +0100865func Test_incsearch_cmdline_modifier()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100866 CheckOption incsearch
867
Bram Moolenaar548e5982018-12-26 21:45:00 +0100868 call test_override("char_avail", 1)
869 new
870 call setline(1, ['foo'])
871 set incsearch
872 " Test that error E14 does not occur in parsing command modifier.
873 call feedkeys("V:tab", 'tx')
874
875 call Incsearch_cleanup()
876endfunc
877
Bram Moolenaar9d34d902018-04-27 22:18:12 +0200878func Test_incsearch_scrolling()
879 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200880 throw 'Skipped: cannot make screendumps'
Bram Moolenaar9d34d902018-04-27 22:18:12 +0200881 endif
882 call assert_equal(0, &scrolloff)
883 call writefile([
884 \ 'let dots = repeat(".", 120)',
885 \ 'set incsearch cmdheight=2 scrolloff=0',
886 \ 'call setline(1, [dots, dots, dots, "", "target", dots, dots])',
887 \ 'normal gg',
888 \ 'redraw',
889 \ ], 'Xscript')
890 let buf = RunVimInTerminal('-S Xscript', {'rows': 9, 'cols': 70})
891 " Need to send one key at a time to force a redraw
892 call term_sendkeys(buf, '/')
893 sleep 100m
894 call term_sendkeys(buf, 't')
895 sleep 100m
896 call term_sendkeys(buf, 'a')
897 sleep 100m
898 call term_sendkeys(buf, 'r')
899 sleep 100m
900 call term_sendkeys(buf, 'g')
901 call VerifyScreenDump(buf, 'Test_incsearch_scrolling_01', {})
902
903 call term_sendkeys(buf, "\<Esc>")
904 call StopVimInTerminal(buf)
905 call delete('Xscript')
906endfunc
907
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200908func Test_incsearch_search_dump()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100909 CheckOption incsearch
910 CheckScreendump
911
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200912 call writefile([
913 \ 'set incsearch hlsearch scrolloff=0',
914 \ 'for n in range(1, 8)',
915 \ ' call setline(n, "foo " . n)',
916 \ 'endfor',
917 \ '3',
918 \ ], 'Xis_search_script')
919 let buf = RunVimInTerminal('-S Xis_search_script', {'rows': 9, 'cols': 70})
920 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
921 " the 'ambiwidth' check.
922 sleep 100m
923
924 " Need to send one key at a time to force a redraw.
925 call term_sendkeys(buf, '/fo')
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200926 call VerifyScreenDump(buf, 'Test_incsearch_search_01', {})
927 call term_sendkeys(buf, "\<Esc>")
928 sleep 100m
929
930 call term_sendkeys(buf, '/\v')
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200931 call VerifyScreenDump(buf, 'Test_incsearch_search_02', {})
932 call term_sendkeys(buf, "\<Esc>")
933
934 call StopVimInTerminal(buf)
935 call delete('Xis_search_script')
936endfunc
937
Bram Moolenaar976b8472018-08-12 15:49:47 +0200938func Test_incsearch_substitute()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100939 CheckOption incsearch
940
Bram Moolenaar976b8472018-08-12 15:49:47 +0200941 call test_override("char_avail", 1)
942 new
943 set incsearch
944 for n in range(1, 10)
945 call setline(n, 'foo ' . n)
946 endfor
947 4
948 call feedkeys(":.,.+2s/foo\<BS>o\<BS>o/xxx\<cr>", 'tx')
949 call assert_equal('foo 3', getline(3))
950 call assert_equal('xxx 4', getline(4))
951 call assert_equal('xxx 5', getline(5))
952 call assert_equal('xxx 6', getline(6))
953 call assert_equal('foo 7', getline(7))
954
955 call Incsearch_cleanup()
956endfunc
957
Bram Moolenaar164251f2018-08-12 16:26:58 +0200958" Similar to Test_incsearch_substitute() but with a screendump halfway.
959func Test_incsearch_substitute_dump()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100960 CheckOption incsearch
961 CheckScreendump
962
Bram Moolenaar164251f2018-08-12 16:26:58 +0200963 call writefile([
964 \ 'set incsearch hlsearch scrolloff=0',
965 \ 'for n in range(1, 10)',
966 \ ' call setline(n, "foo " . n)',
967 \ 'endfor',
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200968 \ 'call setline(11, "bar 11")',
Bram Moolenaar164251f2018-08-12 16:26:58 +0200969 \ '3',
970 \ ], 'Xis_subst_script')
971 let buf = RunVimInTerminal('-S Xis_subst_script', {'rows': 9, 'cols': 70})
972 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
973 " the 'ambiwidth' check.
974 sleep 100m
975
976 " Need to send one key at a time to force a redraw.
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200977 " Select three lines at the cursor with typed pattern.
Bram Moolenaar164251f2018-08-12 16:26:58 +0200978 call term_sendkeys(buf, ':.,.+2s/')
979 sleep 100m
980 call term_sendkeys(buf, 'f')
981 sleep 100m
982 call term_sendkeys(buf, 'o')
983 sleep 100m
984 call term_sendkeys(buf, 'o')
985 call VerifyScreenDump(buf, 'Test_incsearch_substitute_01', {})
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200986 call term_sendkeys(buf, "\<Esc>")
987
988 " Select three lines at the cursor using previous pattern.
989 call term_sendkeys(buf, "/foo\<CR>")
990 sleep 100m
991 call term_sendkeys(buf, ':.,.+2s//')
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200992 call VerifyScreenDump(buf, 'Test_incsearch_substitute_02', {})
993
994 " Deleting last slash should remove the match.
995 call term_sendkeys(buf, "\<BS>")
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200996 call VerifyScreenDump(buf, 'Test_incsearch_substitute_03', {})
Bram Moolenaar60d08712018-08-12 21:53:15 +0200997 call term_sendkeys(buf, "\<Esc>")
998
999 " Reverse range is accepted
1000 call term_sendkeys(buf, ':5,2s/foo')
Bram Moolenaar60d08712018-08-12 21:53:15 +02001001 call VerifyScreenDump(buf, 'Test_incsearch_substitute_04', {})
Bram Moolenaar164251f2018-08-12 16:26:58 +02001002 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaar2b926fc2018-08-13 11:07:57 +02001003
1004 " White space after the command is skipped
1005 call term_sendkeys(buf, ':2,3sub /fo')
Bram Moolenaar2b926fc2018-08-13 11:07:57 +02001006 call VerifyScreenDump(buf, 'Test_incsearch_substitute_05', {})
1007 call term_sendkeys(buf, "\<Esc>")
1008
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001009 " Command modifiers are skipped
1010 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 +02001011 call VerifyScreenDump(buf, 'Test_incsearch_substitute_06', {})
1012 call term_sendkeys(buf, "\<Esc>")
1013
Bram Moolenaar2f6a3462018-08-14 18:16:52 +02001014 " Cursorline highlighting at match
1015 call term_sendkeys(buf, ":set cursorline\<CR>")
1016 call term_sendkeys(buf, 'G9G')
1017 call term_sendkeys(buf, ':9,11s/bar')
Bram Moolenaar2f6a3462018-08-14 18:16:52 +02001018 call VerifyScreenDump(buf, 'Test_incsearch_substitute_07', {})
1019 call term_sendkeys(buf, "\<Esc>")
1020
1021 " Cursorline highlighting at cursor when no match
1022 call term_sendkeys(buf, ':9,10s/bar')
Bram Moolenaar2f6a3462018-08-14 18:16:52 +02001023 call VerifyScreenDump(buf, 'Test_incsearch_substitute_08', {})
1024 call term_sendkeys(buf, "\<Esc>")
1025
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +02001026 " Only \v handled as empty pattern, does not move cursor
1027 call term_sendkeys(buf, '3G4G')
1028 call term_sendkeys(buf, ":nohlsearch\<CR>")
1029 call term_sendkeys(buf, ':6,7s/\v')
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +02001030 call VerifyScreenDump(buf, 'Test_incsearch_substitute_09', {})
1031 call term_sendkeys(buf, "\<Esc>")
1032
Bram Moolenaarf13daa42018-08-31 22:09:54 +02001033 call term_sendkeys(buf, ":set nocursorline\<CR>")
1034
1035 " All matches are highlighted for 'hlsearch' after the incsearch canceled
1036 call term_sendkeys(buf, "1G*")
1037 call term_sendkeys(buf, ":2,5s/foo")
1038 sleep 100m
1039 call term_sendkeys(buf, "\<Esc>")
1040 call VerifyScreenDump(buf, 'Test_incsearch_substitute_10', {})
1041
Bram Moolenaar65985ac2018-09-16 17:08:04 +02001042 call term_sendkeys(buf, ":split\<CR>")
1043 call term_sendkeys(buf, ":let @/ = 'xyz'\<CR>")
1044 call term_sendkeys(buf, ":%s/.")
1045 call VerifyScreenDump(buf, 'Test_incsearch_substitute_11', {})
1046 call term_sendkeys(buf, "\<BS>")
1047 call VerifyScreenDump(buf, 'Test_incsearch_substitute_12', {})
1048 call term_sendkeys(buf, "\<Esc>")
1049 call VerifyScreenDump(buf, 'Test_incsearch_substitute_13', {})
Bram Moolenaarc6725252019-11-23 21:56:46 +01001050 call term_sendkeys(buf, ":%bwipe!\<CR>")
1051 call term_sendkeys(buf, ":only!\<CR>")
1052
1053 " get :'<,'>s command in history
1054 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
1055 call term_sendkeys(buf, "aasdfasdf\<Esc>")
1056 call term_sendkeys(buf, "V:s/a/b/g\<CR>")
1057 " Using '<,'> does not give E20
1058 call term_sendkeys(buf, ":new\<CR>")
1059 call term_sendkeys(buf, "aasdfasdf\<Esc>")
1060 call term_sendkeys(buf, ":\<Up>\<Up>")
1061 call VerifyScreenDump(buf, 'Test_incsearch_substitute_14', {})
1062 call term_sendkeys(buf, "<Esc>")
Bram Moolenaar65985ac2018-09-16 17:08:04 +02001063
Bram Moolenaar164251f2018-08-12 16:26:58 +02001064 call StopVimInTerminal(buf)
1065 call delete('Xis_subst_script')
1066endfunc
1067
Bram Moolenaarc036e872020-02-21 21:30:52 +01001068func Test_incsearch_highlighting()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001069 CheckOption incsearch
1070 CheckScreendump
Bram Moolenaarc036e872020-02-21 21:30:52 +01001071
1072 call writefile([
1073 \ 'set incsearch hlsearch',
1074 \ 'call setline(1, "hello/there")',
1075 \ ], 'Xis_subst_hl_script')
1076 let buf = RunVimInTerminal('-S Xis_subst_hl_script', {'rows': 4, 'cols': 20})
1077 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1078 " the 'ambiwidth' check.
1079 sleep 300m
1080
1081 " Using a different search delimiter should still highlight matches
1082 " that contain a '/'.
1083 call term_sendkeys(buf, ":%s;ello/the")
1084 call VerifyScreenDump(buf, 'Test_incsearch_substitute_15', {})
1085 call term_sendkeys(buf, "<Esc>")
Bram Moolenaarb68df222020-03-19 15:05:28 +01001086
1087 call StopVimInTerminal(buf)
1088 call delete('Xis_subst_hl_script')
Bram Moolenaarc036e872020-02-21 21:30:52 +01001089endfunc
1090
Bram Moolenaar4a7d2d32019-02-21 16:25:50 +01001091func Test_incsearch_with_change()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001092 CheckFeature timers
1093 CheckOption incsearch
1094 CheckScreendump
Bram Moolenaar4a7d2d32019-02-21 16:25:50 +01001095
1096 call writefile([
1097 \ 'set incsearch hlsearch scrolloff=0',
1098 \ 'call setline(1, ["one", "two ------ X", "three"])',
1099 \ 'call timer_start(200, { _ -> setline(2, "x")})',
1100 \ ], 'Xis_change_script')
1101 let buf = RunVimInTerminal('-S Xis_change_script', {'rows': 9, 'cols': 70})
1102 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1103 " the 'ambiwidth' check.
Bram Moolenaare86ecbd2019-02-21 17:48:59 +01001104 sleep 300m
Bram Moolenaar4a7d2d32019-02-21 16:25:50 +01001105
1106 " Highlight X, it will be deleted by the timer callback.
1107 call term_sendkeys(buf, ':%s/X')
1108 call VerifyScreenDump(buf, 'Test_incsearch_change_01', {})
1109 call term_sendkeys(buf, "\<Esc>")
1110
1111 call StopVimInTerminal(buf)
1112 call delete('Xis_change_script')
1113endfunc
1114
Bram Moolenaar81f56532018-08-18 16:19:42 +02001115" Similar to Test_incsearch_substitute_dump() for :sort
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +02001116func Test_incsearch_sort_dump()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001117 CheckOption incsearch
1118 CheckScreendump
1119
Bram Moolenaar81f56532018-08-18 16:19:42 +02001120 call writefile([
1121 \ 'set incsearch hlsearch scrolloff=0',
1122 \ 'call setline(1, ["another one 2", "that one 3", "the one 1"])',
1123 \ ], 'Xis_sort_script')
1124 let buf = RunVimInTerminal('-S Xis_sort_script', {'rows': 9, 'cols': 70})
1125 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1126 " the 'ambiwidth' check.
1127 sleep 100m
1128
1129 " Need to send one key at a time to force a redraw.
1130 call term_sendkeys(buf, ':sort ni u /on')
Bram Moolenaar81f56532018-08-18 16:19:42 +02001131 call VerifyScreenDump(buf, 'Test_incsearch_sort_01', {})
1132 call term_sendkeys(buf, "\<Esc>")
1133
1134 call StopVimInTerminal(buf)
1135 call delete('Xis_sort_script')
1136endfunc
1137
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001138" Similar to Test_incsearch_substitute_dump() for :vimgrep famiry
1139func Test_incsearch_vimgrep_dump()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001140 CheckOption incsearch
1141 CheckScreendump
1142
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001143 call writefile([
1144 \ 'set incsearch hlsearch scrolloff=0',
1145 \ 'call setline(1, ["another one 2", "that one 3", "the one 1"])',
1146 \ ], 'Xis_vimgrep_script')
1147 let buf = RunVimInTerminal('-S Xis_vimgrep_script', {'rows': 9, 'cols': 70})
1148 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1149 " the 'ambiwidth' check.
1150 sleep 100m
1151
1152 " Need to send one key at a time to force a redraw.
1153 call term_sendkeys(buf, ':vimgrep on')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001154 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_01', {})
1155 call term_sendkeys(buf, "\<Esc>")
1156
1157 call term_sendkeys(buf, ':vimg /on/ *.txt')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001158 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_02', {})
1159 call term_sendkeys(buf, "\<Esc>")
1160
1161 call term_sendkeys(buf, ':vimgrepadd "\<on')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001162 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_03', {})
1163 call term_sendkeys(buf, "\<Esc>")
1164
1165 call term_sendkeys(buf, ':lv "tha')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001166 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_04', {})
1167 call term_sendkeys(buf, "\<Esc>")
1168
1169 call term_sendkeys(buf, ':lvimgrepa "the" **/*.txt')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001170 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_05', {})
1171 call term_sendkeys(buf, "\<Esc>")
1172
1173 call StopVimInTerminal(buf)
1174 call delete('Xis_vimgrep_script')
1175endfunc
1176
Bram Moolenaar198cb662018-09-06 21:44:17 +02001177func Test_keep_last_search_pattern()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001178 CheckOption incsearch
1179
Bram Moolenaar198cb662018-09-06 21:44:17 +02001180 new
1181 call setline(1, ['foo', 'foo', 'foo'])
1182 set incsearch
1183 call test_override("char_avail", 1)
1184 let @/ = 'bar'
1185 call feedkeys(":/foo/s//\<Esc>", 'ntx')
1186 call assert_equal('bar', @/)
1187
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001188 " no error message if pattern not found
1189 call feedkeys(":/xyz/s//\<Esc>", 'ntx')
1190 call assert_equal('bar', @/)
1191
Bram Moolenaar198cb662018-09-06 21:44:17 +02001192 bwipe!
1193 call test_override("ALL", 0)
1194 set noincsearch
1195endfunc
1196
Bram Moolenaar99f043a2018-09-09 15:54:14 +02001197func Test_word_under_cursor_after_match()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001198 CheckOption incsearch
1199
Bram Moolenaar99f043a2018-09-09 15:54:14 +02001200 new
1201 call setline(1, 'foo bar')
1202 set incsearch
1203 call test_override("char_avail", 1)
1204 try
1205 call feedkeys("/foo\<C-R>\<C-W>\<CR>", 'ntx')
1206 catch /E486:/
1207 endtry
1208 call assert_equal('foobar', @/)
1209
1210 bwipe!
1211 call test_override("ALL", 0)
1212 set noincsearch
1213endfunc
1214
1215func Test_subst_word_under_cursor()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001216 CheckOption incsearch
1217
Bram Moolenaar99f043a2018-09-09 15:54:14 +02001218 new
1219 call setline(1, ['int SomeLongName;', 'for (xxx = 1; xxx < len; ++xxx)'])
1220 set incsearch
1221 call test_override("char_avail", 1)
1222 call feedkeys("/LongName\<CR>", 'ntx')
1223 call feedkeys(":%s/xxx/\<C-R>\<C-W>/g\<CR>", 'ntx')
1224 call assert_equal('for (SomeLongName = 1; SomeLongName < len; ++SomeLongName)', getline(2))
1225
1226 bwipe!
1227 call test_override("ALL", 0)
1228 set noincsearch
1229endfunc
1230
Bram Moolenaarf45938c2017-11-02 15:59:57 +01001231func Test_search_undefined_behaviour()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001232 CheckFeature terminal
1233
Bram Moolenaarf45938c2017-11-02 15:59:57 +01001234 let h = winheight(0)
1235 if h < 3
1236 return
1237 endif
1238 " did cause an undefined left shift
1239 let g:buf = term_start([GetVimProg(), '--clean', '-e', '-s', '-c', 'call search(getline("."))', 'samples/test000'], {'term_rows': 3})
1240 call assert_equal([''], getline(1, '$'))
1241 call term_sendkeys(g:buf, ":qa!\<cr>")
1242 bwipe!
1243endfunc
Bram Moolenaar2973daa2017-11-02 23:15:40 +01001244
1245func Test_search_undefined_behaviour2()
1246 call search("\%UC0000000")
1247endfunc
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001248
1249" Test for search('multi-byte char', 'bce')
1250func Test_search_multibyte()
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001251 let save_enc = &encoding
1252 set encoding=utf8
1253 enew!
1254 call append('$', 'A')
1255 call cursor(2, 1)
1256 call assert_equal(2, search('A', 'bce', line('.')))
1257 enew!
1258 let &encoding = save_enc
1259endfunc
Bram Moolenaar890dd052017-12-16 19:59:37 +01001260
1261" This was causing E874. Also causes an invalid read?
1262func Test_look_behind()
1263 new
Bram Moolenaar3ba35402019-12-21 22:00:50 +01001264 call setline(1, '0\|\&\n\@<=')
Bram Moolenaar890dd052017-12-16 19:59:37 +01001265 call search(getline("."))
1266 bwipe!
1267endfunc
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01001268
1269func Test_search_sentence()
1270 new
1271 " this used to cause a crash
Bram Moolenaar1bd999f2017-12-19 22:25:40 +01001272 call assert_fails("/\\%')", 'E486')
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01001273 call assert_fails("/", 'E486')
Bram Moolenaar1bd999f2017-12-19 22:25:40 +01001274 /\%'(
1275 /
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01001276endfunc
Bram Moolenaar2fb8f682018-12-01 13:14:45 +01001277
1278" Test that there is no crash when there is a last search pattern but no last
1279" substitute pattern.
1280func Test_no_last_substitute_pat()
1281 " Use viminfo to set the last search pattern to a string and make the last
1282 " substitute pattern the most recent used and make it empty (NULL).
1283 call writefile(['~MSle0/bar', '~MSle0~&'], 'Xviminfo')
1284 rviminfo! Xviminfo
1285 call assert_fails('normal n', 'E35:')
1286
1287 call delete('Xviminfo')
1288endfunc
Bram Moolenaar5f5e2032018-12-14 15:47:03 +01001289
1290func Test_search_Ctrl_L_combining()
1291 " Make sure, that Ctrl-L works correctly with combining characters.
1292 " It uses an artificial example of an 'a' with 4 combining chars:
Bram Moolenaar3ba35402019-12-21 22:00:50 +01001293 " 'a' U+0061 Dec:97 LATIN SMALL LETTER A &#x61; /\%u61\Z "\u0061"
Bram Moolenaar5f5e2032018-12-14 15:47:03 +01001294 " ' ̀' U+0300 Dec:768 COMBINING GRAVE ACCENT &#x300; /\%u300\Z "\u0300"
1295 " ' ́' U+0301 Dec:769 COMBINING ACUTE ACCENT &#x301; /\%u301\Z "\u0301"
1296 " ' ̇' U+0307 Dec:775 COMBINING DOT ABOVE &#x307; /\%u307\Z "\u0307"
Bram Moolenaar3ba35402019-12-21 22:00:50 +01001297 " ' ̣' U+0323 Dec:803 COMBINING DOT BELOW &#x323; /\%u323 "\u0323"
Bram Moolenaar5f5e2032018-12-14 15:47:03 +01001298 " Those should also appear on the commandline
Bram Moolenaarb68df222020-03-19 15:05:28 +01001299 CheckOption incsearch
1300
Bram Moolenaar5f5e2032018-12-14 15:47:03 +01001301 call Cmdline3_prep()
1302 1
1303 let bufcontent = ['', 'Miạ̀́̇m']
1304 call append('$', bufcontent)
1305 call feedkeys("/Mi\<c-l>\<c-l>\<cr>", 'tx')
1306 call assert_equal(5, line('.'))
1307 call assert_equal(bufcontent[1], @/)
1308 call Incsearch_cleanup()
1309endfunc
Bram Moolenaar527a2d82019-02-21 22:28:51 +01001310
Bram Moolenaarab350f82019-02-28 06:25:00 +01001311func Test_large_hex_chars1()
Bram Moolenaar527a2d82019-02-21 22:28:51 +01001312 " This used to cause a crash, the character becomes an NFA state.
1313 try
1314 /\%Ufffffc23
1315 catch
1316 call assert_match('E678:', v:exception)
1317 endtry
Bram Moolenaarab350f82019-02-28 06:25:00 +01001318 try
1319 set re=1
1320 /\%Ufffffc23
1321 catch
1322 call assert_match('E678:', v:exception)
1323 endtry
1324 set re&
1325endfunc
1326
1327func Test_large_hex_chars2()
1328 " This used to cause a crash, the character becomes an NFA state.
1329 try
1330 /[\Ufffffc1f]
1331 catch
1332 call assert_match('E486:', v:exception)
1333 endtry
1334 try
1335 set re=1
1336 /[\Ufffffc1f]
1337 catch
1338 call assert_match('E486:', v:exception)
1339 endtry
1340 set re&
Bram Moolenaar527a2d82019-02-21 22:28:51 +01001341endfunc
Bram Moolenaarcd625122019-02-22 17:29:43 +01001342
1343func Test_one_error_msg()
1344 " This was also giving an internal error
1345 call assert_fails('call search(" \\((\\v[[=P=]]){185}+ ")', 'E871:')
1346endfunc
Bram Moolenaar730f48f2019-04-11 13:45:57 +02001347
1348func Test_incsearch_add_char_under_cursor()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001349 CheckOption incsearch
1350
Bram Moolenaar730f48f2019-04-11 13:45:57 +02001351 set incsearch
1352 new
1353 call setline(1, ['find match', 'anything'])
1354 1
1355 call test_override('char_avail', 1)
1356 call feedkeys("fc/m\<C-L>\<C-L>\<C-L>\<C-L>\<C-L>\<CR>", 'tx')
1357 call assert_equal('match', @/)
1358 call test_override('char_avail', 0)
1359
1360 set incsearch&
1361 bwipe!
1362endfunc
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02001363
1364" Test for the search() function with match at the cursor position
1365func Test_search_match_at_curpos()
1366 new
1367 call append(0, ['foobar', '', 'one two', ''])
1368
1369 normal gg
1370
Bram Moolenaar196b4662019-09-06 21:34:30 +02001371 eval 'foobar'->search('c')
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02001372 call assert_equal([1, 1], [line('.'), col('.')])
1373
1374 normal j
1375 call search('^$', 'c')
1376 call assert_equal([2, 1], [line('.'), col('.')])
1377
1378 call search('^$', 'bc')
1379 call assert_equal([2, 1], [line('.'), col('.')])
1380
1381 exe "normal /two\<CR>"
1382 call search('.', 'c')
1383 call assert_equal([3, 5], [line('.'), col('.')])
1384
1385 close!
1386endfunc
Bram Moolenaardb294ad2019-06-06 12:49:29 +02001387
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001388" Test for error cases with the search() function
1389func Test_search_errors()
1390 call assert_fails("call search('pat', [])", 'E730:')
1391 call assert_fails("call search('pat', 'b', {})", 'E728:')
1392 call assert_fails("call search('pat', 'b', 1, [])", 'E745:')
1393 call assert_fails("call search('pat', 'ns')", 'E475:')
1394 call assert_fails("call search('pat', 'mr')", 'E475:')
1395endfunc
1396
Bram Moolenaardb294ad2019-06-06 12:49:29 +02001397func Test_search_display_pattern()
1398 new
1399 call setline(1, ['foo', 'bar', 'foobar'])
1400
1401 call cursor(1, 1)
1402 let @/ = 'foo'
Bram Moolenaara4208962019-08-24 20:50:19 +02001403 let pat = @/->escape('()*?'. '\s\+')
Bram Moolenaardb294ad2019-06-06 12:49:29 +02001404 let g:a = execute(':unsilent :norm! n')
1405 call assert_match(pat, g:a)
1406
1407 " right-left
1408 if exists("+rightleft")
1409 set rl
1410 call cursor(1, 1)
1411 let @/ = 'foo'
1412 let pat = 'oof/\s\+'
1413 let g:a = execute(':unsilent :norm! n')
1414 call assert_match(pat, g:a)
1415 set norl
1416 endif
1417endfunc
Bram Moolenaar196b4662019-09-06 21:34:30 +02001418
1419func Test_searchdecl()
1420 let lines =<< trim END
1421 int global;
1422
1423 func()
1424 {
1425 int global;
1426 if (cond) {
1427 int local;
1428 }
1429 int local;
1430 // comment
1431 }
1432 END
1433 new
1434 call setline(1, lines)
1435 10
1436 call assert_equal(0, searchdecl('local', 0, 0))
1437 call assert_equal(7, getcurpos()[1])
1438
1439 10
1440 call assert_equal(0, 'local'->searchdecl(0, 1))
1441 call assert_equal(9, getcurpos()[1])
1442
1443 10
1444 call assert_equal(0, searchdecl('global'))
1445 call assert_equal(5, getcurpos()[1])
1446
1447 10
1448 call assert_equal(0, searchdecl('global', 1))
1449 call assert_equal(1, getcurpos()[1])
1450
1451 bwipe!
1452endfunc
Bram Moolenaar98a336d2020-01-20 20:22:30 +01001453
1454func Test_search_special()
Bram Moolenaarfe4bbac2020-01-20 21:12:20 +01001455 " this was causing illegal memory access and an endless loop
1456 set t_PE=
Bram Moolenaar98a336d2020-01-20 20:22:30 +01001457 exe "norm /\x80PS"
1458endfunc
Bram Moolenaar07ada5f2020-02-05 20:38:22 +01001459
1460" Test for command failures when the last search pattern is not set.
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001461" Need to run this in a new vim instance where last search pattern is not set.
Bram Moolenaar07ada5f2020-02-05 20:38:22 +01001462func Test_search_with_no_last_pat()
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001463 let lines =<< trim [SCRIPT]
1464 call assert_fails("normal i\<C-R>/\e", 'E35:')
1465 call assert_fails("exe '/'", 'E35:')
1466 call assert_fails("exe '?'", 'E35:')
1467 call assert_fails("/", 'E35:')
1468 call assert_fails("?", 'E35:')
1469 call assert_fails("normal n", 'E35:')
1470 call assert_fails("normal N", 'E35:')
1471 call assert_fails("normal gn", 'E35:')
1472 call assert_fails("normal gN", 'E35:')
1473 call assert_fails("normal cgn", 'E35:')
1474 call assert_fails("normal cgN", 'E35:')
1475 let p = []
1476 let p = @/
1477 call assert_equal('', p)
1478 call assert_fails("normal :\<C-R>/", 'E35:')
1479 call assert_fails("//p", 'E35:')
1480 call assert_fails(";//p", 'E35:')
1481 call assert_fails("??p", 'E35:')
1482 call assert_fails(";??p", 'E35:')
1483 call assert_fails('g//p', 'E476:')
1484 call assert_fails('v//p', 'E476:')
1485 call writefile(v:errors, 'Xresult')
1486 qall!
1487 [SCRIPT]
1488 call writefile(lines, 'Xscript')
1489
1490 if RunVim([], [], '--clean -S Xscript')
1491 call assert_equal([], readfile('Xresult'))
1492 endif
1493 call delete('Xscript')
1494 call delete('Xresult')
Bram Moolenaar07ada5f2020-02-05 20:38:22 +01001495endfunc
1496
1497" Test for using tilde (~) atom in search. This should use the last used
1498" substitute pattern
1499func Test_search_tilde_pat()
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001500 let lines =<< trim [SCRIPT]
1501 set regexpengine=1
1502 call assert_fails('exe "normal /~\<CR>"', 'E33:')
1503 call assert_fails('exe "normal ?~\<CR>"', 'E33:')
1504 set regexpengine=2
1505 call assert_fails('exe "normal /~\<CR>"', 'E383:')
1506 call assert_fails('exe "normal ?~\<CR>"', 'E383:')
1507 set regexpengine&
1508 call writefile(v:errors, 'Xresult')
1509 qall!
1510 [SCRIPT]
1511 call writefile(lines, 'Xscript')
1512 if RunVim([], [], '--clean -S Xscript')
1513 call assert_equal([], readfile('Xresult'))
1514 endif
1515 call delete('Xscript')
1516 call delete('Xresult')
Bram Moolenaar07ada5f2020-02-05 20:38:22 +01001517endfunc
1518
Bram Moolenaar406cd902020-02-18 21:54:41 +01001519" Test for searching a pattern that is not present with 'nowrapscan'
1520func Test_search_pat_not_found()
1521 new
1522 set nowrapscan
1523 let @/ = '1abcxyz2'
1524 call assert_fails('normal n', 'E385:')
1525 call assert_fails('normal N', 'E384:')
1526 set wrapscan&
1527 close
1528endfunc
1529
1530" Test for v:searchforward variable
1531func Test_searchforward_var()
1532 new
1533 call setline(1, ['foo', '', 'foo'])
1534 call cursor(2, 1)
1535 let @/ = 'foo'
1536 let v:searchforward = 0
1537 normal N
1538 call assert_equal(3, line('.'))
1539 call cursor(2, 1)
1540 let v:searchforward = 1
1541 normal N
1542 call assert_equal(1, line('.'))
1543 close!
1544endfunc
1545
Bram Moolenaar07ada5f2020-02-05 20:38:22 +01001546" vim: shiftwidth=2 sts=2 expandtab