blob: 97ddfdbc6e694bb6611473f492debce81a9a2579 [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')
Bram Moolenaar8832a342020-04-11 18:36:38 +020020 call feedkeys("/the\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020021 call assert_equal('the', @/)
Bram Moolenaar8832a342020-04-11 18:36:38 +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 Moolenaare2e40752020-09-04 21:18:46 +0200269 "call assert_fails(feedkeys("/foo\<c-w>\<cr>", 'tx'), 'E35:')
Bram Moolenaardda933d2016-09-03 21:04:58 +0200270 "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 Moolenaarbade44e2020-09-26 22:39:24 +0200293 " should not give an error for using "42"
294 call assert_equal(0, searchpair('a', 'b', 'c', '', 42))
295
Bram Moolenaar6e450a52017-01-06 20:03:58 +0100296 4
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100297 call assert_equal(3, searchpair('\[', '', ']', 'bW'))
298 call assert_equal([0, 3, 2, 0], getpos('.'))
299 4
300 call assert_equal(2, searchpair('\[', '', ']', 'bWr'))
301 call assert_equal([0, 2, 6, 0], getpos('.'))
302 4
303 call assert_equal(1, searchpair('\[', '', ']', 'bWm'))
304 call assert_equal([0, 3, 2, 0], getpos('.'))
305 4|norm ^
306 call assert_equal(5, searchpair('\[', '', ']', 'Wn'))
307 call assert_equal([0, 4, 2, 0], getpos('.'))
308 4
309 call assert_equal(2, searchpair('\[', '', ']', 'bW',
310 \ 'getline(".") =~ "^\\s*\["'))
311 call assert_equal([0, 2, 6, 0], getpos('.'))
Bram Moolenaar6e450a52017-01-06 20:03:58 +0100312 set nomagic
313 4
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100314 call assert_equal(3, searchpair('\[', '', ']', 'bW'))
315 call assert_equal([0, 3, 2, 0], getpos('.'))
Bram Moolenaar6e450a52017-01-06 20:03:58 +0100316 set magic
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100317 4|norm ^
318 call assert_equal(0, searchpair('{', '', '}', 'bW'))
319 call assert_equal([0, 4, 2, 0], getpos('.'))
320
321 %d
322 call setline(1, ['if 1', ' if 2', ' else', ' endif 2', 'endif 1'])
323
324 /\<if 1
325 call assert_equal(5, searchpair('\<if\>', '\<else\>', '\<endif\>', 'W'))
326 call assert_equal([0, 5, 1, 0], getpos('.'))
327 /\<if 2
328 call assert_equal(3, searchpair('\<if\>', '\<else\>', '\<endif\>', 'W'))
329 call assert_equal([0, 3, 3, 0], getpos('.'))
330
331 q!
332endfunc
333
334func Test_searchpairpos()
335 new
336 call setline(1, ['other code', 'here [', ' [', ' " cursor here', ' ]]'])
337
338 4
339 call assert_equal([3, 2], searchpairpos('\[', '', ']', 'bW'))
340 call assert_equal([0, 3, 2, 0], getpos('.'))
341 4
342 call assert_equal([2, 6], searchpairpos('\[', '', ']', 'bWr'))
343 call assert_equal([0, 2, 6, 0], getpos('.'))
344 4|norm ^
345 call assert_equal([5, 2], searchpairpos('\[', '', ']', 'Wn'))
346 call assert_equal([0, 4, 2, 0], getpos('.'))
347 4
348 call assert_equal([2, 6], searchpairpos('\[', '', ']', 'bW',
349 \ 'getline(".") =~ "^\\s*\["'))
350 call assert_equal([0, 2, 6, 0], getpos('.'))
351 4
352 call assert_equal([2, 6], searchpairpos('\[', '', ']', 'bWr'))
353 call assert_equal([0, 2, 6, 0], getpos('.'))
354 set nomagic
355 4
356 call assert_equal([3, 2], searchpairpos('\[', '', ']', 'bW'))
357 call assert_equal([0, 3, 2, 0], getpos('.'))
358 set magic
359 4|norm ^
360 call assert_equal([0, 0], searchpairpos('{', '', '}', 'bW'))
361 call assert_equal([0, 4, 2, 0], getpos('.'))
362
363 %d
364 call setline(1, ['if 1', ' if 2', ' else', ' endif 2', 'endif 1'])
365 /\<if 1
366 call assert_equal([5, 1], searchpairpos('\<if\>', '\<else\>', '\<endif\>', 'W'))
367 call assert_equal([0, 5, 1, 0], getpos('.'))
368 /\<if 2
369 call assert_equal([3, 3], searchpairpos('\<if\>', '\<else\>', '\<endif\>', 'W'))
370 call assert_equal([0, 3, 3, 0], getpos('.'))
371
Bram Moolenaar6e450a52017-01-06 20:03:58 +0100372 q!
373endfunc
374
Bram Moolenaar3dddb092018-06-24 19:01:59 +0200375func Test_searchpair_errors()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200376 call assert_fails("call searchpair([0], 'middle', 'end', 'bW', 'skip', 99, 100)", 'E730: Using a List as a String')
377 call assert_fails("call searchpair('start', {-> 0}, 'end', 'bW', 'skip', 99, 100)", 'E729: Using a Funcref as a String')
378 call assert_fails("call searchpair('start', 'middle', {'one': 1}, 'bW', 'skip', 99, 100)", 'E731: Using a Dictionary as a String')
Bram Moolenaar3dddb092018-06-24 19:01:59 +0200379 call assert_fails("call searchpair('start', 'middle', 'end', 'flags', 'skip', 99, 100)", 'E475: Invalid argument: flags')
Bram Moolenaar3dddb092018-06-24 19:01:59 +0200380 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()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200387 call assert_fails("call searchpairpos([0], 'middle', 'end', 'bW', 'skip', 99, 100)", 'E730: Using a List as a String')
388 call assert_fails("call searchpairpos('start', {-> 0}, 'end', 'bW', 'skip', 99, 100)", 'E729: Using a Funcref as a String')
389 call assert_fails("call searchpairpos('start', 'middle', {'one': 1}, 'bW', 'skip', 99, 100)", 'E731: Using a Dictionary as a String')
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100390 call assert_fails("call searchpairpos('start', 'middle', 'end', 'flags', 'skip', 99, 100)", 'E475: Invalid argument: flags')
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100391 call assert_fails("call searchpairpos('start', 'middle', 'end', 'bW', 'func', -99, 100)", 'E475: Invalid argument: -99')
392 call assert_fails("call searchpairpos('start', 'middle', 'end', 'bW', 'func', 99, -100)", 'E475: Invalid argument: -100')
393 call assert_fails("call searchpairpos('start', 'middle', 'end', 'e')", 'E475: Invalid argument: e')
394 call assert_fails("call searchpairpos('start', 'middle', 'end', 'sn')", 'E475: Invalid argument: sn')
Bram Moolenaar3dddb092018-06-24 19:01:59 +0200395endfunc
396
Bram Moolenaar48570482017-10-30 21:48:41 +0100397func Test_searchpair_skip()
398 func Zero()
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100399 return 0
Bram Moolenaar48570482017-10-30 21:48:41 +0100400 endfunc
401 func Partial(x)
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100402 return a:x
Bram Moolenaar48570482017-10-30 21:48:41 +0100403 endfunc
404 new
405 call setline(1, ['{', 'foo', 'foo', 'foo', '}'])
406 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', ''))
407 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', '0'))
408 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', {-> 0}))
409 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', function('Zero')))
410 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', function('Partial', [0])))
Bram Moolenaar48570482017-10-30 21:48:41 +0100411 bw!
412endfunc
413
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200414func Test_searchpair_leak()
415 new
416 call setline(1, 'if one else another endif')
417
418 " The error in the skip expression caused memory to leak.
419 call assert_fails("call searchpair('\\<if\\>', '\\<else\\>', '\\<endif\\>', '', '\"foo\" 2')", 'E15:')
420
421 bwipe!
422endfunc
423
Bram Moolenaar66727e12017-03-01 22:17:05 +0100424func Test_searchc()
425 " These commands used to cause memory overflow in searchc().
426 new
427 norm ixx
428 exe "norm 0t\u93cf"
429 bw!
430endfunc
Bram Moolenaara693d052017-06-29 22:23:06 +0200431
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200432func Cmdline3_prep()
433 " need to disable char_avail,
434 " so that expansion of commandline works
435 call test_override("char_avail", 1)
436 new
437 call setline(1, [' 1', ' 2 the~e', ' 3 the theother'])
438 set incsearch
439endfunc
440
Bram Moolenaar976b8472018-08-12 15:49:47 +0200441func Incsearch_cleanup()
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200442 set noincsearch
443 call test_override("char_avail", 0)
444 bw!
445endfunc
446
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200447func Test_search_cmdline3()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100448 CheckOption incsearch
449
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200450 call Cmdline3_prep()
451 1
452 " first match
453 call feedkeys("/the\<c-l>\<cr>", 'tx')
454 call assert_equal(' 2 the~e', getline('.'))
455
Bram Moolenaar976b8472018-08-12 15:49:47 +0200456 call Incsearch_cleanup()
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200457endfunc
458
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200459func Test_search_cmdline3s()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100460 CheckOption incsearch
461
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200462 call Cmdline3_prep()
463 1
464 call feedkeys(":%s/the\<c-l>/xxx\<cr>", 'tx')
465 call assert_equal(' 2 xxxe', getline('.'))
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200466 undo
467 call feedkeys(":%subs/the\<c-l>/xxx\<cr>", 'tx')
468 call assert_equal(' 2 xxxe', getline('.'))
469 undo
470 call feedkeys(":%substitute/the\<c-l>/xxx\<cr>", 'tx')
471 call assert_equal(' 2 xxxe', getline('.'))
Bram Moolenaar167ae422018-08-14 21:32:21 +0200472 undo
473 call feedkeys(":%smagic/the.e/xxx\<cr>", 'tx')
474 call assert_equal(' 2 xxx', getline('.'))
475 undo
Bram Moolenaare2e40752020-09-04 21:18:46 +0200476 call assert_fails(":%snomagic/the.e/xxx\<cr>", 'E486:')
Bram Moolenaar167ae422018-08-14 21:32:21 +0200477 "
478 call feedkeys(":%snomagic/the\\.e/xxx\<cr>", 'tx')
479 call assert_equal(' 2 xxx', getline('.'))
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200480
Bram Moolenaar976b8472018-08-12 15:49:47 +0200481 call Incsearch_cleanup()
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200482endfunc
483
484func Test_search_cmdline3g()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100485 CheckOption incsearch
486
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200487 call Cmdline3_prep()
488 1
489 call feedkeys(":g/the\<c-l>/d\<cr>", 'tx')
490 call assert_equal(' 3 the theother', getline(2))
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200491 undo
492 call feedkeys(":global/the\<c-l>/d\<cr>", 'tx')
493 call assert_equal(' 3 the theother', getline(2))
Bram Moolenaardef7b1d2018-08-13 22:54:35 +0200494 undo
495 call feedkeys(":g!/the\<c-l>/d\<cr>", 'tx')
496 call assert_equal(1, line('$'))
497 call assert_equal(' 2 the~e', getline(1))
498 undo
499 call feedkeys(":global!/the\<c-l>/d\<cr>", 'tx')
500 call assert_equal(1, line('$'))
501 call assert_equal(' 2 the~e', getline(1))
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200502
Bram Moolenaar976b8472018-08-12 15:49:47 +0200503 call Incsearch_cleanup()
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200504endfunc
505
506func Test_search_cmdline3v()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100507 CheckOption incsearch
508
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200509 call Cmdline3_prep()
510 1
511 call feedkeys(":v/the\<c-l>/d\<cr>", 'tx')
512 call assert_equal(1, line('$'))
513 call assert_equal(' 2 the~e', getline(1))
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200514 undo
515 call feedkeys(":vglobal/the\<c-l>/d\<cr>", 'tx')
516 call assert_equal(1, line('$'))
517 call assert_equal(' 2 the~e', getline(1))
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200518
Bram Moolenaar976b8472018-08-12 15:49:47 +0200519 call Incsearch_cleanup()
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200520endfunc
521
Bram Moolenaarda5116d2017-07-01 23:11:17 +0200522func Test_search_cmdline4()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100523 CheckOption incsearch
524
Bram Moolenaarda5116d2017-07-01 23:11:17 +0200525 " need to disable char_avail,
526 " so that expansion of commandline works
527 call test_override("char_avail", 1)
528 new
529 call setline(1, [' 1 the first', ' 2 the second', ' 3 the third'])
530 set incsearch
531 $
532 call feedkeys("?the\<c-g>\<cr>", 'tx')
533 call assert_equal(' 3 the third', getline('.'))
534 $
535 call feedkeys("?the\<c-g>\<c-g>\<cr>", 'tx')
536 call assert_equal(' 1 the first', getline('.'))
537 $
538 call feedkeys("?the\<c-g>\<c-g>\<c-g>\<cr>", 'tx')
539 call assert_equal(' 2 the second', getline('.'))
540 $
541 call feedkeys("?the\<c-t>\<cr>", 'tx')
542 call assert_equal(' 1 the first', getline('.'))
543 $
544 call feedkeys("?the\<c-t>\<c-t>\<cr>", 'tx')
545 call assert_equal(' 3 the third', getline('.'))
546 $
547 call feedkeys("?the\<c-t>\<c-t>\<c-t>\<cr>", 'tx')
548 call assert_equal(' 2 the second', getline('.'))
549 " clean up
550 set noincsearch
551 call test_override("char_avail", 0)
552 bw!
553endfunc
Bram Moolenaardb510072017-09-28 21:52:17 +0200554
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200555func Test_search_cmdline5()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100556 CheckOption incsearch
557
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200558 " Do not call test_override("char_avail", 1) so that <C-g> and <C-t> work
559 " regardless char_avail.
560 new
Bram Moolenaar69a5b862019-07-16 21:38:51 +0200561 call setline(1, [' 1 the first', ' 2 the second', ' 3 the third', ''])
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200562 set incsearch
563 1
564 call feedkeys("/the\<c-g>\<c-g>\<cr>", 'tx')
565 call assert_equal(' 3 the third', getline('.'))
566 $
567 call feedkeys("?the\<c-t>\<c-t>\<c-t>\<cr>", 'tx')
Bram Moolenaar69a5b862019-07-16 21:38:51 +0200568 call assert_equal(' 1 the first', getline('.'))
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200569 " clean up
570 set noincsearch
571 bw!
572endfunc
573
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100574func Test_search_cmdline6()
575 " Test that consecutive matches
576 " are caught by <c-g>/<c-t>
Bram Moolenaarb68df222020-03-19 15:05:28 +0100577 CheckOption incsearch
578
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100579 " need to disable char_avail,
580 " so that expansion of commandline works
581 call test_override("char_avail", 1)
582 new
583 call setline(1, [' bbvimb', ''])
584 set incsearch
585 " first match
586 norm! gg0
587 call feedkeys("/b\<cr>", 'tx')
588 call assert_equal([0,1,2,0], getpos('.'))
589 " second match
590 norm! gg0
591 call feedkeys("/b\<c-g>\<cr>", 'tx')
592 call assert_equal([0,1,3,0], getpos('.'))
593 " third match
594 norm! gg0
595 call feedkeys("/b\<c-g>\<c-g>\<cr>", 'tx')
596 call assert_equal([0,1,7,0], getpos('.'))
597 " first match again
598 norm! gg0
599 call feedkeys("/b\<c-g>\<c-g>\<c-g>\<cr>", 'tx')
600 call assert_equal([0,1,2,0], getpos('.'))
601 set nowrapscan
602 " last match
603 norm! gg0
604 call feedkeys("/b\<c-g>\<c-g>\<c-g>\<cr>", 'tx')
605 call assert_equal([0,1,7,0], getpos('.'))
606 " clean up
607 set wrapscan&vim
608 set noincsearch
609 call test_override("char_avail", 0)
610 bw!
611endfunc
612
613func Test_search_cmdline7()
Bram Moolenaard66cdcd2020-07-26 13:27:16 +0200614 " Test that pressing <c-g> in an empty command line
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100615 " does not move the cursor
Bram Moolenaarb68df222020-03-19 15:05:28 +0100616 CheckOption incsearch
617
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100618 " need to disable char_avail,
619 " so that expansion of commandline works
620 call test_override("char_avail", 1)
621 new
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200622 let @/ = 'b'
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100623 call setline(1, [' bbvimb', ''])
624 set incsearch
625 " first match
626 norm! gg0
627 " moves to next match of previous search pattern, just like /<cr>
628 call feedkeys("/\<c-g>\<cr>", 'tx')
629 call assert_equal([0,1,2,0], getpos('.'))
630 " moves to next match of previous search pattern, just like /<cr>
631 call feedkeys("/\<cr>", 'tx')
632 call assert_equal([0,1,3,0], getpos('.'))
633 " moves to next match of previous search pattern, just like /<cr>
634 call feedkeys("/\<c-t>\<cr>", 'tx')
635 call assert_equal([0,1,7,0], getpos('.'))
Bram Moolenaard0480092017-11-16 22:20:39 +0100636
637 " using an offset uses the last search pattern
638 call cursor(1, 1)
639 call setline(1, ['1 bbvimb', ' 2 bbvimb'])
640 let @/ = 'b'
641 call feedkeys("//e\<c-g>\<cr>", 'tx')
642 call assert_equal('1 bbvimb', getline('.'))
643 call assert_equal(4, col('.'))
644
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100645 set noincsearch
646 call test_override("char_avail", 0)
647 bw!
648endfunc
649
650func Test_search_cmdline8()
651 " Highlighting is cleared in all windows
652 " since hls applies to all windows
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200653 CheckOption incsearch
654 CheckFeature terminal
655 CheckNotGui
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100656 if has("win32")
657 throw "Skipped: Bug with sending <ESC> to terminal window not fixed yet"
658 endif
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200659
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100660 let h = winheight(0)
661 if h < 3
662 return
663 endif
664 " Prepare buffer text
665 let lines = ['abb vim vim vi', 'vimvivim']
666 call writefile(lines, 'Xsearch.txt')
Bram Moolenaar13deab82017-11-04 18:48:43 +0100667 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', 'Xsearch.txt'], {'term_rows': 3})
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100668
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200669 call WaitForAssert({-> assert_equal(lines, [term_getline(buf, 1), term_getline(buf, 2)])})
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100670
Bram Moolenaar13deab82017-11-04 18:48:43 +0100671 call term_sendkeys(buf, ":set incsearch hlsearch\<cr>")
672 call term_sendkeys(buf, ":14vsp\<cr>")
673 call term_sendkeys(buf, "/vim\<cr>")
674 call term_sendkeys(buf, "/b\<esc>")
675 call term_sendkeys(buf, "gg0")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200676 call TermWait(buf, 250)
Bram Moolenaar13deab82017-11-04 18:48:43 +0100677 let screen_line = term_scrape(buf, 1)
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100678 let [a0,a1,a2,a3] = [screen_line[3].attr, screen_line[4].attr,
679 \ screen_line[18].attr, screen_line[19].attr]
680 call assert_notequal(a0, a1)
681 call assert_notequal(a0, a3)
682 call assert_notequal(a1, a2)
683 call assert_equal(a0, a2)
684 call assert_equal(a1, a3)
685 " clean up
686 call delete('Xsearch.txt')
687
688 bwipe!
689endfunc
690
Bram Moolenaardb510072017-09-28 21:52:17 +0200691" Tests for regexp with various magic settings
Bram Moolenaar4d23c522020-04-09 18:42:11 +0200692func Run_search_regexp_magic_opt()
Bram Moolenaardb510072017-09-28 21:52:17 +0200693 put ='1 a aa abb abbccc'
694 exe 'normal! /a*b\{2}c\+/e' . "\<CR>"
695 call assert_equal([0, 2, 17, 0], getpos('.'))
696
697 put ='2 d dd dee deefff'
698 exe 'normal! /\Md\*e\{2}f\+/e' . "\<CR>"
699 call assert_equal([0, 3, 17, 0], getpos('.'))
700
701 set nomagic
702 put ='3 g gg ghh ghhiii'
703 exe 'normal! /g\*h\{2}i\+/e' . "\<CR>"
704 call assert_equal([0, 4, 17, 0], getpos('.'))
705
706 put ='4 j jj jkk jkklll'
707 exe 'normal! /\mj*k\{2}l\+/e' . "\<CR>"
708 call assert_equal([0, 5, 17, 0], getpos('.'))
709
710 put ='5 m mm mnn mnnooo'
711 exe 'normal! /\vm*n{2}o+/e' . "\<CR>"
712 call assert_equal([0, 6, 17, 0], getpos('.'))
713
714 put ='6 x ^aa$ x'
715 exe 'normal! /\V^aa$' . "\<CR>"
716 call assert_equal([0, 7, 5, 0], getpos('.'))
717
718 set magic
719 put ='7 (a)(b) abbaa'
720 exe 'normal! /\v(a)(b)\2\1\1/e' . "\<CR>"
721 call assert_equal([0, 8, 14, 0], getpos('.'))
722
723 put ='8 axx [ab]xx'
724 exe 'normal! /\V[ab]\(\[xy]\)\1' . "\<CR>"
725 call assert_equal([0, 9, 7, 0], getpos('.'))
726
Bram Moolenaar4d23c522020-04-09 18:42:11 +0200727 %d
728endfunc
729
730func Test_search_regexp()
731 enew!
732
733 set regexpengine=1
734 call Run_search_regexp_magic_opt()
735 set regexpengine=2
736 call Run_search_regexp_magic_opt()
737 set regexpengine&
738
Bram Moolenaardb510072017-09-28 21:52:17 +0200739 set undolevels=100
740 put ='9 foobar'
741 put =''
742 exe "normal! a\<C-G>u\<Esc>"
743 normal G
744 exe 'normal! dv?bar?' . "\<CR>"
745 call assert_equal('9 foo', getline('.'))
Bram Moolenaar4d23c522020-04-09 18:42:11 +0200746 call assert_equal([0, 2, 5, 0], getpos('.'))
747 call assert_equal(2, line('$'))
Bram Moolenaardb510072017-09-28 21:52:17 +0200748 normal u
749 call assert_equal('9 foobar', getline('.'))
Bram Moolenaar4d23c522020-04-09 18:42:11 +0200750 call assert_equal([0, 2, 6, 0], getpos('.'))
751 call assert_equal(3, line('$'))
Bram Moolenaardb510072017-09-28 21:52:17 +0200752
753 set undolevels&
754 enew!
755endfunc
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100756
757func Test_search_cmdline_incsearch_highlight()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100758 CheckOption incsearch
759
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100760 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
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200806 call TermWait(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')
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200811 call TermWait(buf, 100)
Bram Moolenaar13deab82017-11-04 18:48:43 +0100812 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")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200827 call TermWait(buf, 100)
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>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200839 call TermWait(buf, 100)
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>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200847 call TermWait(buf, 100)
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>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200855 call TermWait(buf, 100)
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")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200865 call TermWait(buf, 100)
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()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100877 CheckOption incsearch
878
Bram Moolenaar548e5982018-12-26 21:45:00 +0100879 call test_override("char_avail", 1)
880 new
881 call setline(1, ['foo'])
882 set incsearch
883 " Test that error E14 does not occur in parsing command modifier.
884 call feedkeys("V:tab", 'tx')
885
886 call Incsearch_cleanup()
887endfunc
888
Bram Moolenaar9d34d902018-04-27 22:18:12 +0200889func Test_incsearch_scrolling()
Bram Moolenaar494e9062020-05-31 21:28:02 +0200890 CheckRunVimInTerminal
Bram Moolenaar9d34d902018-04-27 22:18:12 +0200891 call assert_equal(0, &scrolloff)
892 call writefile([
893 \ 'let dots = repeat(".", 120)',
894 \ 'set incsearch cmdheight=2 scrolloff=0',
895 \ 'call setline(1, [dots, dots, dots, "", "target", dots, dots])',
896 \ 'normal gg',
897 \ 'redraw',
898 \ ], 'Xscript')
899 let buf = RunVimInTerminal('-S Xscript', {'rows': 9, 'cols': 70})
900 " Need to send one key at a time to force a redraw
901 call term_sendkeys(buf, '/')
902 sleep 100m
903 call term_sendkeys(buf, 't')
904 sleep 100m
905 call term_sendkeys(buf, 'a')
906 sleep 100m
907 call term_sendkeys(buf, 'r')
908 sleep 100m
909 call term_sendkeys(buf, 'g')
910 call VerifyScreenDump(buf, 'Test_incsearch_scrolling_01', {})
911
912 call term_sendkeys(buf, "\<Esc>")
913 call StopVimInTerminal(buf)
914 call delete('Xscript')
915endfunc
916
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200917func Test_incsearch_search_dump()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100918 CheckOption incsearch
919 CheckScreendump
920
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200921 call writefile([
922 \ 'set incsearch hlsearch scrolloff=0',
923 \ 'for n in range(1, 8)',
924 \ ' call setline(n, "foo " . n)',
925 \ 'endfor',
926 \ '3',
927 \ ], 'Xis_search_script')
928 let buf = RunVimInTerminal('-S Xis_search_script', {'rows': 9, 'cols': 70})
929 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
930 " the 'ambiwidth' check.
931 sleep 100m
932
933 " Need to send one key at a time to force a redraw.
934 call term_sendkeys(buf, '/fo')
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200935 call VerifyScreenDump(buf, 'Test_incsearch_search_01', {})
936 call term_sendkeys(buf, "\<Esc>")
937 sleep 100m
938
939 call term_sendkeys(buf, '/\v')
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200940 call VerifyScreenDump(buf, 'Test_incsearch_search_02', {})
941 call term_sendkeys(buf, "\<Esc>")
942
943 call StopVimInTerminal(buf)
944 call delete('Xis_search_script')
945endfunc
946
Bram Moolenaar41f08952021-02-22 22:13:49 +0100947func Test_hlsearch_dump()
948 CheckOption hlsearch
949 CheckScreendump
950
951 call writefile([
952 \ 'set hlsearch cursorline',
953 \ 'call setline(1, ["xxx", "xxx", "xxx"])',
954 \ '/.*',
955 \ '2',
956 \ ], 'Xhlsearch_script')
957 let buf = RunVimInTerminal('-S Xhlsearch_script', {'rows': 6, 'cols': 50})
958 call VerifyScreenDump(buf, 'Test_hlsearch_1', {})
959
960 call term_sendkeys(buf, "/\\_.*\<CR>")
961 call VerifyScreenDump(buf, 'Test_hlsearch_2', {})
962
963 call StopVimInTerminal(buf)
964 call delete('Xhlsearch_script')
965endfunc
966
Bram Moolenaar2d5f3852021-04-21 15:11:42 +0200967func Test_hlsearch_and_visual()
968 CheckOption hlsearch
969 CheckScreendump
970
971 call writefile([
972 \ 'set hlsearch',
973 \ 'call setline(1, repeat(["xxx yyy zzz"], 3))',
974 \ 'hi Search cterm=bold',
975 \ '/yyy',
976 \ 'call cursor(1, 6)',
977 \ ], 'Xhlvisual_script')
978 let buf = RunVimInTerminal('-S Xhlvisual_script', {'rows': 6, 'cols': 40})
979 call term_sendkeys(buf, "vjj")
980 call VerifyScreenDump(buf, 'Test_hlsearch_visual_1', {})
981 call term_sendkeys(buf, "\<Esc>")
982
983 call StopVimInTerminal(buf)
984 call delete('Xhlvisual_script')
985endfunc
986
Bram Moolenaare71c0eb2021-05-30 16:43:11 +0200987func Test_hlsearch_block_visual_match()
988 CheckScreendump
989
990 let lines =<< trim END
991 set hlsearch
992 call setline(1, ['aa', 'bbbb', 'cccccc'])
993 END
994 call writefile(lines, 'Xhlsearch_block')
995 let buf = RunVimInTerminal('-S Xhlsearch_block', {'rows': 9, 'cols': 60})
996
997 call term_sendkeys(buf, "G\<C-V>$kk\<Esc>")
998 sleep 100m
999 call term_sendkeys(buf, "/\\%V\<CR>")
1000 sleep 100m
1001 call VerifyScreenDump(buf, 'Test_hlsearch_block_visual_match', {})
1002
1003 call StopVimInTerminal(buf)
1004 call delete('Xhlsearch_block')
1005endfunc
1006
Bram Moolenaar976b8472018-08-12 15:49:47 +02001007func Test_incsearch_substitute()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001008 CheckOption incsearch
1009
Bram Moolenaar976b8472018-08-12 15:49:47 +02001010 call test_override("char_avail", 1)
1011 new
1012 set incsearch
1013 for n in range(1, 10)
1014 call setline(n, 'foo ' . n)
1015 endfor
1016 4
1017 call feedkeys(":.,.+2s/foo\<BS>o\<BS>o/xxx\<cr>", 'tx')
1018 call assert_equal('foo 3', getline(3))
1019 call assert_equal('xxx 4', getline(4))
1020 call assert_equal('xxx 5', getline(5))
1021 call assert_equal('xxx 6', getline(6))
1022 call assert_equal('foo 7', getline(7))
1023
1024 call Incsearch_cleanup()
1025endfunc
1026
Bram Moolenaar795aaa12020-10-02 20:36:01 +02001027func Test_incsearch_substitute_long_line()
1028 new
1029 call test_override("char_avail", 1)
1030 set incsearch
1031
1032 call repeat('x', 100000)->setline(1)
1033 call feedkeys(':s/\%c', 'xt')
1034 redraw
1035 call feedkeys("\<Esc>", 'xt')
1036
1037 call Incsearch_cleanup()
1038 bwipe!
1039endfunc
1040
LemonBoya4399382022-04-09 21:04:08 +01001041func Test_hlsearch_cursearch()
1042 CheckScreendump
1043
1044 let lines =<< trim END
1045 set hlsearch scrolloff=0
1046 call setline(1, ['one', 'foo', 'bar', 'baz', 'foo', 'bar'])
1047 hi Search ctermbg=yellow
1048 hi CurSearch ctermbg=blue
1049 END
1050 call writefile(lines, 'Xhlsearch_cursearch')
1051 let buf = RunVimInTerminal('-S Xhlsearch_cursearch', {'rows': 9, 'cols': 60})
1052
1053 call term_sendkeys(buf, "gg/foo\<CR>")
1054 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_single_line_1', {})
1055
1056 call term_sendkeys(buf, "n")
1057 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_single_line_2', {})
1058
1059 call term_sendkeys(buf, "?\<CR>")
1060 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_single_line_3', {})
1061
1062 call term_sendkeys(buf, "gg/foo\\nbar\<CR>")
Bram Moolenaar693ccd12022-04-16 12:04:37 +01001063 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_multiple_line_1', {})
1064
1065 call term_sendkeys(buf, ":call setline(1, ['---', 'abcdefg', 'hijkl', '---', 'abcdefg', 'hijkl'])\<CR>")
1066 call term_sendkeys(buf, "gg/efg\\nhij\<CR>")
1067 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_multiple_line_2', {})
1068 call term_sendkeys(buf, "h\<C-L>")
1069 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_multiple_line_3', {})
1070 call term_sendkeys(buf, "j\<C-L>")
1071 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_multiple_line_4', {})
1072 call term_sendkeys(buf, "h\<C-L>")
1073 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_multiple_line_5', {})
LemonBoya4399382022-04-09 21:04:08 +01001074
1075 call StopVimInTerminal(buf)
1076 call delete('Xhlsearch_cursearch')
1077endfunc
1078
Bram Moolenaar164251f2018-08-12 16:26:58 +02001079" Similar to Test_incsearch_substitute() but with a screendump halfway.
1080func Test_incsearch_substitute_dump()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001081 CheckOption incsearch
1082 CheckScreendump
1083
Bram Moolenaar164251f2018-08-12 16:26:58 +02001084 call writefile([
1085 \ 'set incsearch hlsearch scrolloff=0',
1086 \ 'for n in range(1, 10)',
1087 \ ' call setline(n, "foo " . n)',
1088 \ 'endfor',
Bram Moolenaar2f6a3462018-08-14 18:16:52 +02001089 \ 'call setline(11, "bar 11")',
Bram Moolenaar164251f2018-08-12 16:26:58 +02001090 \ '3',
1091 \ ], 'Xis_subst_script')
1092 let buf = RunVimInTerminal('-S Xis_subst_script', {'rows': 9, 'cols': 70})
1093 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1094 " the 'ambiwidth' check.
1095 sleep 100m
1096
1097 " Need to send one key at a time to force a redraw.
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02001098 " Select three lines at the cursor with typed pattern.
Bram Moolenaar164251f2018-08-12 16:26:58 +02001099 call term_sendkeys(buf, ':.,.+2s/')
1100 sleep 100m
1101 call term_sendkeys(buf, 'f')
1102 sleep 100m
1103 call term_sendkeys(buf, 'o')
1104 sleep 100m
1105 call term_sendkeys(buf, 'o')
1106 call VerifyScreenDump(buf, 'Test_incsearch_substitute_01', {})
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02001107 call term_sendkeys(buf, "\<Esc>")
1108
1109 " Select three lines at the cursor using previous pattern.
1110 call term_sendkeys(buf, "/foo\<CR>")
1111 sleep 100m
1112 call term_sendkeys(buf, ':.,.+2s//')
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02001113 call VerifyScreenDump(buf, 'Test_incsearch_substitute_02', {})
1114
1115 " Deleting last slash should remove the match.
1116 call term_sendkeys(buf, "\<BS>")
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02001117 call VerifyScreenDump(buf, 'Test_incsearch_substitute_03', {})
Bram Moolenaar60d08712018-08-12 21:53:15 +02001118 call term_sendkeys(buf, "\<Esc>")
1119
1120 " Reverse range is accepted
1121 call term_sendkeys(buf, ':5,2s/foo')
Bram Moolenaar60d08712018-08-12 21:53:15 +02001122 call VerifyScreenDump(buf, 'Test_incsearch_substitute_04', {})
Bram Moolenaar164251f2018-08-12 16:26:58 +02001123 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaar2b926fc2018-08-13 11:07:57 +02001124
1125 " White space after the command is skipped
1126 call term_sendkeys(buf, ':2,3sub /fo')
Bram Moolenaar2b926fc2018-08-13 11:07:57 +02001127 call VerifyScreenDump(buf, 'Test_incsearch_substitute_05', {})
1128 call term_sendkeys(buf, "\<Esc>")
1129
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001130 " Command modifiers are skipped
1131 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 +02001132 call VerifyScreenDump(buf, 'Test_incsearch_substitute_06', {})
1133 call term_sendkeys(buf, "\<Esc>")
1134
Bram Moolenaar2f6a3462018-08-14 18:16:52 +02001135 " Cursorline highlighting at match
1136 call term_sendkeys(buf, ":set cursorline\<CR>")
1137 call term_sendkeys(buf, 'G9G')
1138 call term_sendkeys(buf, ':9,11s/bar')
Bram Moolenaar2f6a3462018-08-14 18:16:52 +02001139 call VerifyScreenDump(buf, 'Test_incsearch_substitute_07', {})
1140 call term_sendkeys(buf, "\<Esc>")
1141
1142 " Cursorline highlighting at cursor when no match
1143 call term_sendkeys(buf, ':9,10s/bar')
Bram Moolenaar2f6a3462018-08-14 18:16:52 +02001144 call VerifyScreenDump(buf, 'Test_incsearch_substitute_08', {})
1145 call term_sendkeys(buf, "\<Esc>")
1146
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +02001147 " Only \v handled as empty pattern, does not move cursor
1148 call term_sendkeys(buf, '3G4G')
1149 call term_sendkeys(buf, ":nohlsearch\<CR>")
1150 call term_sendkeys(buf, ':6,7s/\v')
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +02001151 call VerifyScreenDump(buf, 'Test_incsearch_substitute_09', {})
1152 call term_sendkeys(buf, "\<Esc>")
1153
Bram Moolenaarf13daa42018-08-31 22:09:54 +02001154 call term_sendkeys(buf, ":set nocursorline\<CR>")
1155
1156 " All matches are highlighted for 'hlsearch' after the incsearch canceled
1157 call term_sendkeys(buf, "1G*")
1158 call term_sendkeys(buf, ":2,5s/foo")
1159 sleep 100m
1160 call term_sendkeys(buf, "\<Esc>")
1161 call VerifyScreenDump(buf, 'Test_incsearch_substitute_10', {})
1162
Bram Moolenaar65985ac2018-09-16 17:08:04 +02001163 call term_sendkeys(buf, ":split\<CR>")
1164 call term_sendkeys(buf, ":let @/ = 'xyz'\<CR>")
1165 call term_sendkeys(buf, ":%s/.")
1166 call VerifyScreenDump(buf, 'Test_incsearch_substitute_11', {})
1167 call term_sendkeys(buf, "\<BS>")
1168 call VerifyScreenDump(buf, 'Test_incsearch_substitute_12', {})
1169 call term_sendkeys(buf, "\<Esc>")
1170 call VerifyScreenDump(buf, 'Test_incsearch_substitute_13', {})
Bram Moolenaarc6725252019-11-23 21:56:46 +01001171 call term_sendkeys(buf, ":%bwipe!\<CR>")
1172 call term_sendkeys(buf, ":only!\<CR>")
1173
1174 " get :'<,'>s command in history
1175 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
1176 call term_sendkeys(buf, "aasdfasdf\<Esc>")
1177 call term_sendkeys(buf, "V:s/a/b/g\<CR>")
1178 " Using '<,'> does not give E20
1179 call term_sendkeys(buf, ":new\<CR>")
1180 call term_sendkeys(buf, "aasdfasdf\<Esc>")
1181 call term_sendkeys(buf, ":\<Up>\<Up>")
1182 call VerifyScreenDump(buf, 'Test_incsearch_substitute_14', {})
1183 call term_sendkeys(buf, "<Esc>")
Bram Moolenaar65985ac2018-09-16 17:08:04 +02001184
Bram Moolenaar164251f2018-08-12 16:26:58 +02001185 call StopVimInTerminal(buf)
1186 call delete('Xis_subst_script')
1187endfunc
1188
Bram Moolenaarc036e872020-02-21 21:30:52 +01001189func Test_incsearch_highlighting()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001190 CheckOption incsearch
1191 CheckScreendump
Bram Moolenaarc036e872020-02-21 21:30:52 +01001192
1193 call writefile([
1194 \ 'set incsearch hlsearch',
1195 \ 'call setline(1, "hello/there")',
1196 \ ], 'Xis_subst_hl_script')
1197 let buf = RunVimInTerminal('-S Xis_subst_hl_script', {'rows': 4, 'cols': 20})
1198 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1199 " the 'ambiwidth' check.
1200 sleep 300m
1201
1202 " Using a different search delimiter should still highlight matches
1203 " that contain a '/'.
1204 call term_sendkeys(buf, ":%s;ello/the")
1205 call VerifyScreenDump(buf, 'Test_incsearch_substitute_15', {})
1206 call term_sendkeys(buf, "<Esc>")
Bram Moolenaarb68df222020-03-19 15:05:28 +01001207
1208 call StopVimInTerminal(buf)
1209 call delete('Xis_subst_hl_script')
Bram Moolenaarc036e872020-02-21 21:30:52 +01001210endfunc
1211
Bram Moolenaar4a7d2d32019-02-21 16:25:50 +01001212func Test_incsearch_with_change()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001213 CheckFeature timers
1214 CheckOption incsearch
1215 CheckScreendump
Bram Moolenaar4a7d2d32019-02-21 16:25:50 +01001216
1217 call writefile([
1218 \ 'set incsearch hlsearch scrolloff=0',
1219 \ 'call setline(1, ["one", "two ------ X", "three"])',
1220 \ 'call timer_start(200, { _ -> setline(2, "x")})',
1221 \ ], 'Xis_change_script')
1222 let buf = RunVimInTerminal('-S Xis_change_script', {'rows': 9, 'cols': 70})
1223 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1224 " the 'ambiwidth' check.
Bram Moolenaare86ecbd2019-02-21 17:48:59 +01001225 sleep 300m
Bram Moolenaar4a7d2d32019-02-21 16:25:50 +01001226
1227 " Highlight X, it will be deleted by the timer callback.
1228 call term_sendkeys(buf, ':%s/X')
1229 call VerifyScreenDump(buf, 'Test_incsearch_change_01', {})
1230 call term_sendkeys(buf, "\<Esc>")
1231
1232 call StopVimInTerminal(buf)
1233 call delete('Xis_change_script')
1234endfunc
1235
Bram Moolenaar81f56532018-08-18 16:19:42 +02001236" Similar to Test_incsearch_substitute_dump() for :sort
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +02001237func Test_incsearch_sort_dump()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001238 CheckOption incsearch
1239 CheckScreendump
1240
Bram Moolenaar81f56532018-08-18 16:19:42 +02001241 call writefile([
1242 \ 'set incsearch hlsearch scrolloff=0',
1243 \ 'call setline(1, ["another one 2", "that one 3", "the one 1"])',
1244 \ ], 'Xis_sort_script')
1245 let buf = RunVimInTerminal('-S Xis_sort_script', {'rows': 9, 'cols': 70})
1246 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1247 " the 'ambiwidth' check.
1248 sleep 100m
1249
Bram Moolenaar81f56532018-08-18 16:19:42 +02001250 call term_sendkeys(buf, ':sort ni u /on')
Bram Moolenaar81f56532018-08-18 16:19:42 +02001251 call VerifyScreenDump(buf, 'Test_incsearch_sort_01', {})
1252 call term_sendkeys(buf, "\<Esc>")
1253
Bram Moolenaar333015a2020-04-25 15:54:16 +02001254 call term_sendkeys(buf, ':sort! /on')
1255 call VerifyScreenDump(buf, 'Test_incsearch_sort_02', {})
1256 call term_sendkeys(buf, "\<Esc>")
1257
Bram Moolenaar81f56532018-08-18 16:19:42 +02001258 call StopVimInTerminal(buf)
1259 call delete('Xis_sort_script')
1260endfunc
1261
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001262" Similar to Test_incsearch_substitute_dump() for :vimgrep famiry
1263func Test_incsearch_vimgrep_dump()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001264 CheckOption incsearch
1265 CheckScreendump
1266
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001267 call writefile([
1268 \ 'set incsearch hlsearch scrolloff=0',
1269 \ 'call setline(1, ["another one 2", "that one 3", "the one 1"])',
1270 \ ], 'Xis_vimgrep_script')
1271 let buf = RunVimInTerminal('-S Xis_vimgrep_script', {'rows': 9, 'cols': 70})
1272 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1273 " the 'ambiwidth' check.
1274 sleep 100m
1275
1276 " Need to send one key at a time to force a redraw.
1277 call term_sendkeys(buf, ':vimgrep on')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001278 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_01', {})
1279 call term_sendkeys(buf, "\<Esc>")
1280
1281 call term_sendkeys(buf, ':vimg /on/ *.txt')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001282 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_02', {})
1283 call term_sendkeys(buf, "\<Esc>")
1284
1285 call term_sendkeys(buf, ':vimgrepadd "\<on')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001286 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_03', {})
1287 call term_sendkeys(buf, "\<Esc>")
1288
1289 call term_sendkeys(buf, ':lv "tha')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001290 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_04', {})
1291 call term_sendkeys(buf, "\<Esc>")
1292
1293 call term_sendkeys(buf, ':lvimgrepa "the" **/*.txt')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001294 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_05', {})
1295 call term_sendkeys(buf, "\<Esc>")
1296
1297 call StopVimInTerminal(buf)
1298 call delete('Xis_vimgrep_script')
1299endfunc
1300
Bram Moolenaar198cb662018-09-06 21:44:17 +02001301func Test_keep_last_search_pattern()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001302 CheckOption incsearch
1303
Bram Moolenaar198cb662018-09-06 21:44:17 +02001304 new
1305 call setline(1, ['foo', 'foo', 'foo'])
1306 set incsearch
1307 call test_override("char_avail", 1)
1308 let @/ = 'bar'
1309 call feedkeys(":/foo/s//\<Esc>", 'ntx')
1310 call assert_equal('bar', @/)
1311
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001312 " no error message if pattern not found
1313 call feedkeys(":/xyz/s//\<Esc>", 'ntx')
1314 call assert_equal('bar', @/)
1315
Bram Moolenaar198cb662018-09-06 21:44:17 +02001316 bwipe!
1317 call test_override("ALL", 0)
1318 set noincsearch
1319endfunc
1320
Bram Moolenaar99f043a2018-09-09 15:54:14 +02001321func Test_word_under_cursor_after_match()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001322 CheckOption incsearch
1323
Bram Moolenaar99f043a2018-09-09 15:54:14 +02001324 new
1325 call setline(1, 'foo bar')
1326 set incsearch
1327 call test_override("char_avail", 1)
1328 try
1329 call feedkeys("/foo\<C-R>\<C-W>\<CR>", 'ntx')
1330 catch /E486:/
1331 endtry
1332 call assert_equal('foobar', @/)
1333
1334 bwipe!
1335 call test_override("ALL", 0)
1336 set noincsearch
1337endfunc
1338
1339func Test_subst_word_under_cursor()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001340 CheckOption incsearch
1341
Bram Moolenaar99f043a2018-09-09 15:54:14 +02001342 new
1343 call setline(1, ['int SomeLongName;', 'for (xxx = 1; xxx < len; ++xxx)'])
1344 set incsearch
1345 call test_override("char_avail", 1)
1346 call feedkeys("/LongName\<CR>", 'ntx')
1347 call feedkeys(":%s/xxx/\<C-R>\<C-W>/g\<CR>", 'ntx')
1348 call assert_equal('for (SomeLongName = 1; SomeLongName < len; ++SomeLongName)', getline(2))
1349
1350 bwipe!
1351 call test_override("ALL", 0)
1352 set noincsearch
1353endfunc
1354
Bram Moolenaarf45938c2017-11-02 15:59:57 +01001355func Test_search_undefined_behaviour()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001356 CheckFeature terminal
1357
Bram Moolenaarf45938c2017-11-02 15:59:57 +01001358 let h = winheight(0)
1359 if h < 3
1360 return
1361 endif
1362 " did cause an undefined left shift
1363 let g:buf = term_start([GetVimProg(), '--clean', '-e', '-s', '-c', 'call search(getline("."))', 'samples/test000'], {'term_rows': 3})
1364 call assert_equal([''], getline(1, '$'))
1365 call term_sendkeys(g:buf, ":qa!\<cr>")
1366 bwipe!
1367endfunc
Bram Moolenaar2973daa2017-11-02 23:15:40 +01001368
1369func Test_search_undefined_behaviour2()
1370 call search("\%UC0000000")
1371endfunc
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001372
1373" Test for search('multi-byte char', 'bce')
1374func Test_search_multibyte()
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001375 let save_enc = &encoding
1376 set encoding=utf8
1377 enew!
1378 call append('$', 'A')
1379 call cursor(2, 1)
1380 call assert_equal(2, search('A', 'bce', line('.')))
1381 enew!
1382 let &encoding = save_enc
1383endfunc
Bram Moolenaar890dd052017-12-16 19:59:37 +01001384
1385" This was causing E874. Also causes an invalid read?
1386func Test_look_behind()
1387 new
Bram Moolenaar3ba35402019-12-21 22:00:50 +01001388 call setline(1, '0\|\&\n\@<=')
Bram Moolenaar890dd052017-12-16 19:59:37 +01001389 call search(getline("."))
1390 bwipe!
1391endfunc
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01001392
Bram Moolenaar872bee52021-05-24 22:56:15 +02001393func Test_search_visual_area_linewise()
1394 new
1395 call setline(1, ['aa', 'bb', 'cc'])
1396 exe "normal 2GV\<Esc>"
1397 for engine in [1, 2]
1398 exe 'set regexpengine=' .. engine
1399 exe "normal gg/\\%'<\<CR>>"
1400 call assert_equal([0, 2, 1, 0, 1], getcurpos(), 'engine ' .. engine)
1401 exe "normal gg/\\%'>\<CR>"
1402 call assert_equal([0, 2, 2, 0, 2], getcurpos(), 'engine ' .. engine)
1403 endfor
1404
1405 bwipe!
1406 set regexpengine&
1407endfunc
1408
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01001409func Test_search_sentence()
1410 new
1411 " this used to cause a crash
Bram Moolenaar1bd999f2017-12-19 22:25:40 +01001412 /\%'(
1413 /
Bram Moolenaar872bee52021-05-24 22:56:15 +02001414 bwipe
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01001415endfunc
Bram Moolenaar2fb8f682018-12-01 13:14:45 +01001416
1417" Test that there is no crash when there is a last search pattern but no last
1418" substitute pattern.
1419func Test_no_last_substitute_pat()
1420 " Use viminfo to set the last search pattern to a string and make the last
1421 " substitute pattern the most recent used and make it empty (NULL).
1422 call writefile(['~MSle0/bar', '~MSle0~&'], 'Xviminfo')
1423 rviminfo! Xviminfo
1424 call assert_fails('normal n', 'E35:')
1425
1426 call delete('Xviminfo')
1427endfunc
Bram Moolenaar5f5e2032018-12-14 15:47:03 +01001428
1429func Test_search_Ctrl_L_combining()
1430 " Make sure, that Ctrl-L works correctly with combining characters.
1431 " It uses an artificial example of an 'a' with 4 combining chars:
Bram Moolenaar3ba35402019-12-21 22:00:50 +01001432 " 'a' U+0061 Dec:97 LATIN SMALL LETTER A &#x61; /\%u61\Z "\u0061"
Bram Moolenaar5f5e2032018-12-14 15:47:03 +01001433 " ' ̀' U+0300 Dec:768 COMBINING GRAVE ACCENT &#x300; /\%u300\Z "\u0300"
1434 " ' ́' U+0301 Dec:769 COMBINING ACUTE ACCENT &#x301; /\%u301\Z "\u0301"
1435 " ' ̇' U+0307 Dec:775 COMBINING DOT ABOVE &#x307; /\%u307\Z "\u0307"
Bram Moolenaar3ba35402019-12-21 22:00:50 +01001436 " ' ̣' U+0323 Dec:803 COMBINING DOT BELOW &#x323; /\%u323 "\u0323"
Bram Moolenaar5f5e2032018-12-14 15:47:03 +01001437 " Those should also appear on the commandline
Bram Moolenaarb68df222020-03-19 15:05:28 +01001438 CheckOption incsearch
1439
Bram Moolenaar5f5e2032018-12-14 15:47:03 +01001440 call Cmdline3_prep()
1441 1
1442 let bufcontent = ['', 'Miạ̀́̇m']
1443 call append('$', bufcontent)
1444 call feedkeys("/Mi\<c-l>\<c-l>\<cr>", 'tx')
1445 call assert_equal(5, line('.'))
1446 call assert_equal(bufcontent[1], @/)
1447 call Incsearch_cleanup()
1448endfunc
Bram Moolenaar527a2d82019-02-21 22:28:51 +01001449
Bram Moolenaarab350f82019-02-28 06:25:00 +01001450func Test_large_hex_chars1()
Bram Moolenaar527a2d82019-02-21 22:28:51 +01001451 " This used to cause a crash, the character becomes an NFA state.
1452 try
1453 /\%Ufffffc23
1454 catch
1455 call assert_match('E678:', v:exception)
1456 endtry
Bram Moolenaarab350f82019-02-28 06:25:00 +01001457 try
1458 set re=1
1459 /\%Ufffffc23
1460 catch
1461 call assert_match('E678:', v:exception)
1462 endtry
1463 set re&
1464endfunc
1465
1466func Test_large_hex_chars2()
1467 " This used to cause a crash, the character becomes an NFA state.
1468 try
1469 /[\Ufffffc1f]
1470 catch
1471 call assert_match('E486:', v:exception)
1472 endtry
1473 try
1474 set re=1
1475 /[\Ufffffc1f]
1476 catch
1477 call assert_match('E486:', v:exception)
1478 endtry
1479 set re&
Bram Moolenaar527a2d82019-02-21 22:28:51 +01001480endfunc
Bram Moolenaarcd625122019-02-22 17:29:43 +01001481
1482func Test_one_error_msg()
Bram Moolenaar8832a342020-04-11 18:36:38 +02001483 " This was also giving an internal error
Bram Moolenaarcd625122019-02-22 17:29:43 +01001484 call assert_fails('call search(" \\((\\v[[=P=]]){185}+ ")', 'E871:')
1485endfunc
Bram Moolenaar730f48f2019-04-11 13:45:57 +02001486
1487func Test_incsearch_add_char_under_cursor()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001488 CheckOption incsearch
1489
Bram Moolenaar730f48f2019-04-11 13:45:57 +02001490 set incsearch
1491 new
1492 call setline(1, ['find match', 'anything'])
1493 1
1494 call test_override('char_avail', 1)
1495 call feedkeys("fc/m\<C-L>\<C-L>\<C-L>\<C-L>\<C-L>\<CR>", 'tx')
1496 call assert_equal('match', @/)
1497 call test_override('char_avail', 0)
1498
1499 set incsearch&
1500 bwipe!
1501endfunc
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02001502
1503" Test for the search() function with match at the cursor position
1504func Test_search_match_at_curpos()
1505 new
1506 call append(0, ['foobar', '', 'one two', ''])
1507
1508 normal gg
1509
Bram Moolenaar196b4662019-09-06 21:34:30 +02001510 eval 'foobar'->search('c')
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02001511 call assert_equal([1, 1], [line('.'), col('.')])
1512
1513 normal j
1514 call search('^$', 'c')
1515 call assert_equal([2, 1], [line('.'), col('.')])
1516
1517 call search('^$', 'bc')
1518 call assert_equal([2, 1], [line('.'), col('.')])
1519
1520 exe "normal /two\<CR>"
1521 call search('.', 'c')
1522 call assert_equal([3, 5], [line('.'), col('.')])
1523
1524 close!
1525endfunc
Bram Moolenaardb294ad2019-06-06 12:49:29 +02001526
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001527" Test for error cases with the search() function
1528func Test_search_errors()
1529 call assert_fails("call search('pat', [])", 'E730:')
1530 call assert_fails("call search('pat', 'b', {})", 'E728:')
1531 call assert_fails("call search('pat', 'b', 1, [])", 'E745:')
1532 call assert_fails("call search('pat', 'ns')", 'E475:')
1533 call assert_fails("call search('pat', 'mr')", 'E475:')
Bram Moolenaar8832a342020-04-11 18:36:38 +02001534
1535 new
1536 call setline(1, ['foo', 'bar'])
1537 call assert_fails('call feedkeys("/foo/;/bar/;\<CR>", "tx")', 'E386:')
1538 bwipe!
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001539endfunc
1540
Bram Moolenaardb294ad2019-06-06 12:49:29 +02001541func Test_search_display_pattern()
1542 new
1543 call setline(1, ['foo', 'bar', 'foobar'])
1544
1545 call cursor(1, 1)
1546 let @/ = 'foo'
Bram Moolenaara4208962019-08-24 20:50:19 +02001547 let pat = @/->escape('()*?'. '\s\+')
Bram Moolenaardb294ad2019-06-06 12:49:29 +02001548 let g:a = execute(':unsilent :norm! n')
1549 call assert_match(pat, g:a)
1550
1551 " right-left
1552 if exists("+rightleft")
1553 set rl
1554 call cursor(1, 1)
1555 let @/ = 'foo'
1556 let pat = 'oof/\s\+'
1557 let g:a = execute(':unsilent :norm! n')
1558 call assert_match(pat, g:a)
1559 set norl
1560 endif
1561endfunc
Bram Moolenaar196b4662019-09-06 21:34:30 +02001562
1563func Test_searchdecl()
1564 let lines =<< trim END
1565 int global;
1566
1567 func()
1568 {
1569 int global;
1570 if (cond) {
1571 int local;
1572 }
1573 int local;
1574 // comment
1575 }
1576 END
1577 new
1578 call setline(1, lines)
1579 10
1580 call assert_equal(0, searchdecl('local', 0, 0))
1581 call assert_equal(7, getcurpos()[1])
1582
1583 10
1584 call assert_equal(0, 'local'->searchdecl(0, 1))
1585 call assert_equal(9, getcurpos()[1])
1586
1587 10
1588 call assert_equal(0, searchdecl('global'))
1589 call assert_equal(5, getcurpos()[1])
1590
1591 10
1592 call assert_equal(0, searchdecl('global', 1))
1593 call assert_equal(1, getcurpos()[1])
1594
1595 bwipe!
1596endfunc
Bram Moolenaar98a336d2020-01-20 20:22:30 +01001597
1598func Test_search_special()
Bram Moolenaarfe4bbac2020-01-20 21:12:20 +01001599 " this was causing illegal memory access and an endless loop
1600 set t_PE=
Bram Moolenaar98a336d2020-01-20 20:22:30 +01001601 exe "norm /\x80PS"
1602endfunc
Bram Moolenaar07ada5f2020-02-05 20:38:22 +01001603
1604" Test for command failures when the last search pattern is not set.
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001605" Need to run this in a new vim instance where last search pattern is not set.
Bram Moolenaar07ada5f2020-02-05 20:38:22 +01001606func Test_search_with_no_last_pat()
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001607 let lines =<< trim [SCRIPT]
1608 call assert_fails("normal i\<C-R>/\e", 'E35:')
1609 call assert_fails("exe '/'", 'E35:')
1610 call assert_fails("exe '?'", 'E35:')
1611 call assert_fails("/", 'E35:')
1612 call assert_fails("?", 'E35:')
1613 call assert_fails("normal n", 'E35:')
1614 call assert_fails("normal N", 'E35:')
1615 call assert_fails("normal gn", 'E35:')
1616 call assert_fails("normal gN", 'E35:')
1617 call assert_fails("normal cgn", 'E35:')
1618 call assert_fails("normal cgN", 'E35:')
1619 let p = []
1620 let p = @/
1621 call assert_equal('', p)
1622 call assert_fails("normal :\<C-R>/", 'E35:')
1623 call assert_fails("//p", 'E35:')
1624 call assert_fails(";//p", 'E35:')
1625 call assert_fails("??p", 'E35:')
1626 call assert_fails(";??p", 'E35:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001627 call assert_fails('g//p', ['E35:', 'E476:'])
1628 call assert_fails('v//p', ['E35:', 'E476:'])
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001629 call writefile(v:errors, 'Xresult')
1630 qall!
1631 [SCRIPT]
1632 call writefile(lines, 'Xscript')
1633
1634 if RunVim([], [], '--clean -S Xscript')
1635 call assert_equal([], readfile('Xresult'))
1636 endif
1637 call delete('Xscript')
1638 call delete('Xresult')
Bram Moolenaar07ada5f2020-02-05 20:38:22 +01001639endfunc
1640
1641" Test for using tilde (~) atom in search. This should use the last used
1642" substitute pattern
1643func Test_search_tilde_pat()
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001644 let lines =<< trim [SCRIPT]
1645 set regexpengine=1
1646 call assert_fails('exe "normal /~\<CR>"', 'E33:')
1647 call assert_fails('exe "normal ?~\<CR>"', 'E33:')
1648 set regexpengine=2
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001649 call assert_fails('exe "normal /~\<CR>"', ['E33:', 'E383:'])
1650 call assert_fails('exe "normal ?~\<CR>"', ['E33:', 'E383:'])
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001651 set regexpengine&
1652 call writefile(v:errors, 'Xresult')
1653 qall!
1654 [SCRIPT]
1655 call writefile(lines, 'Xscript')
1656 if RunVim([], [], '--clean -S Xscript')
1657 call assert_equal([], readfile('Xresult'))
1658 endif
1659 call delete('Xscript')
1660 call delete('Xresult')
Bram Moolenaar07ada5f2020-02-05 20:38:22 +01001661endfunc
1662
Bram Moolenaar406cd902020-02-18 21:54:41 +01001663" Test for searching a pattern that is not present with 'nowrapscan'
1664func Test_search_pat_not_found()
1665 new
1666 set nowrapscan
1667 let @/ = '1abcxyz2'
1668 call assert_fails('normal n', 'E385:')
1669 call assert_fails('normal N', 'E384:')
1670 set wrapscan&
1671 close
1672endfunc
1673
1674" Test for v:searchforward variable
1675func Test_searchforward_var()
1676 new
1677 call setline(1, ['foo', '', 'foo'])
1678 call cursor(2, 1)
1679 let @/ = 'foo'
1680 let v:searchforward = 0
1681 normal N
1682 call assert_equal(3, line('.'))
1683 call cursor(2, 1)
1684 let v:searchforward = 1
1685 normal N
1686 call assert_equal(1, line('.'))
1687 close!
1688endfunc
1689
Bram Moolenaar476a6132020-04-08 19:48:56 +02001690" Test for invalid regular expressions
1691func Test_invalid_regexp()
1692 set regexpengine=1
1693 call assert_fails("call search(repeat('\\(.\\)', 10))", 'E51:')
Bram Moolenaar4d23c522020-04-09 18:42:11 +02001694 call assert_fails("call search('\\%(')", 'E53:')
1695 call assert_fails("call search('\\(')", 'E54:')
1696 call assert_fails("call search('\\)')", 'E55:')
Bram Moolenaar476a6132020-04-08 19:48:56 +02001697 call assert_fails("call search('x\\@#')", 'E59:')
Bram Moolenaar4d23c522020-04-09 18:42:11 +02001698 call assert_fails('call search(''\v%(%(%(%(%(%(%(%(%(%(%(a){1}){1}){1}){1}){1}){1}){1}){1}){1}){1}){1}'')', 'E60:')
1699 call assert_fails("call search('a\\+*')", 'E61:')
Bram Moolenaar476a6132020-04-08 19:48:56 +02001700 call assert_fails("call search('\\_m')", 'E63:')
1701 call assert_fails("call search('\\+')", 'E64:')
1702 call assert_fails("call search('\\1')", 'E65:')
1703 call assert_fails("call search('\\z\\(\\)')", 'E66:')
1704 call assert_fails("call search('\\z2')", 'E67:')
1705 call assert_fails("call search('\\zx')", 'E68:')
1706 call assert_fails("call search('\\%[ab')", 'E69:')
1707 call assert_fails("call search('\\%[]')", 'E70:')
1708 call assert_fails("call search('\\%a')", 'E71:')
1709 call assert_fails("call search('ab\\%[\\(cd\\)]')", 'E369:')
1710 call assert_fails("call search('ab\\%[\\%(cd\\)]')", 'E369:')
1711 set regexpengine=2
1712 call assert_fails("call search('\\_')", 'E865:')
1713 call assert_fails("call search('\\+')", 'E866:')
1714 call assert_fails("call search('\\zx')", 'E867:')
1715 call assert_fails("call search('\\%a')", 'E867:')
1716 call assert_fails("call search('x\\@#')", 'E869:')
1717 call assert_fails("call search(repeat('\\(.\\)', 10))", 'E872:')
1718 call assert_fails("call search('\\_m')", 'E877:')
1719 call assert_fails("call search('\\%(')", 'E53:')
1720 call assert_fails("call search('\\(')", 'E54:')
1721 call assert_fails("call search('\\)')", 'E55:')
1722 call assert_fails("call search('\\z\\(\\)')", 'E66:')
Dominique Pelle8bfa0eb2022-01-02 16:16:33 +00001723 call assert_fails("call search('\\z2')", 'E67:')
1724 call assert_fails("call search('\\zx')", 'E867:')
Bram Moolenaar476a6132020-04-08 19:48:56 +02001725 call assert_fails("call search('\\%[ab')", 'E69:')
Bram Moolenaar4d23c522020-04-09 18:42:11 +02001726 call assert_fails("call search('\\%[]')", 'E70:')
Bram Moolenaar476a6132020-04-08 19:48:56 +02001727 call assert_fails("call search('\\%9999999999999999999999999999v')", 'E951:')
1728 set regexpengine&
1729 call assert_fails("call search('\\%#=3ab')", 'E864:')
1730endfunc
1731
Bram Moolenaar004a6782020-04-11 17:09:31 +02001732" Test for searching a very complex pattern in a string. Should switch the
1733" regexp engine from NFA to the old engine.
1734func Test_regexp_switch_engine()
1735 let l = readfile('samples/re.freeze.txt')
1736 let v = substitute(l[4], '..\@<!', '', '')
1737 call assert_equal(l[4], v)
1738endfunc
1739
1740" Test for the \%V atom to search within visually selected text
1741func Test_search_in_visual_area()
1742 new
1743 call setline(1, ['foo bar1', 'foo bar2', 'foo bar3', 'foo bar4'])
1744 exe "normal 2GVjo/\\%Vbar\<CR>\<Esc>"
1745 call assert_equal([2, 5], [line('.'), col('.')])
1746 exe "normal 2GVj$?\\%Vbar\<CR>\<Esc>"
1747 call assert_equal([3, 5], [line('.'), col('.')])
1748 close!
1749endfunc
1750
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001751" Test for searching with 'smartcase' and 'ignorecase'
1752func Test_search_smartcase()
1753 new
1754 call setline(1, ['', 'Hello'])
1755 set noignorecase nosmartcase
1756 call assert_fails('exe "normal /\\a\\_.\\(.*\\)O\<CR>"', 'E486:')
1757
1758 set ignorecase nosmartcase
1759 exe "normal /\\a\\_.\\(.*\\)O\<CR>"
1760 call assert_equal([2, 1], [line('.'), col('.')])
1761
1762 call cursor(1, 1)
1763 set ignorecase smartcase
1764 call assert_fails('exe "normal /\\a\\_.\\(.*\\)O\<CR>"', 'E486:')
1765
1766 exe "normal /\\a\\_.\\(.*\\)o\<CR>"
1767 call assert_equal([2, 1], [line('.'), col('.')])
1768
Bram Moolenaar224a5f12020-04-28 20:29:07 +02001769 " Test for using special atoms with 'smartcase'
1770 call setline(1, ['', ' Hello\ '])
1771 call cursor(1, 1)
1772 call feedkeys('/\_.\%(\uello\)\' .. "\<CR>", 'xt')
1773 call assert_equal([2, 4], [line('.'), col('.')])
1774
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001775 set ignorecase& smartcase&
1776 close!
Bram Moolenaard66cdcd2020-07-26 13:27:16 +02001777endfun
1778
1779" Test 'smartcase' with utf-8.
1780func Test_search_smartcase_utf8()
1781 new
1782 let save_enc = &encoding
1783 set encoding=utf8 ignorecase smartcase
1784
1785 call setline(1, 'Café cafÉ')
1786 1s/café/x/g
1787 call assert_equal('x x', getline(1))
1788
1789 call setline(1, 'Café cafÉ')
1790 1s/cafÉ/x/g
1791 call assert_equal('Café x', getline(1))
1792
1793 set ignorecase& smartcase&
1794 let &encoding = save_enc
1795 close!
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001796endfunc
1797
1798" Test searching past the end of a file
1799func Test_search_past_eof()
1800 new
1801 call setline(1, ['Line'])
1802 exe "normal /\\n\\zs\<CR>"
1803 call assert_equal([1, 4], [line('.'), col('.')])
1804 close!
1805endfunc
1806
Bram Moolenaar224a5f12020-04-28 20:29:07 +02001807" Test for various search offsets
1808func Test_search_offset()
1809 " With /e, for a match in the first column of a line, the cursor should be
1810 " placed at the end of the previous line.
1811 new
1812 call setline(1, ['one two', 'three four'])
1813 call search('two\_.', 'e')
1814 call assert_equal([1, 7], [line('.'), col('.')])
1815
1816 " with cursor at the beginning of the file, use /s+1
1817 call cursor(1, 1)
1818 exe "normal /two/s+1\<CR>"
1819 call assert_equal([1, 6], [line('.'), col('.')])
1820
1821 " with cursor at the end of the file, use /e-1
1822 call cursor(2, 10)
1823 exe "normal ?three?e-1\<CR>"
1824 call assert_equal([2, 4], [line('.'), col('.')])
1825
1826 " line offset - after the last line
1827 call cursor(1, 1)
1828 exe "normal /three/+1\<CR>"
1829 call assert_equal([2, 1], [line('.'), col('.')])
1830
1831 " line offset - before the first line
1832 call cursor(2, 1)
1833 exe "normal ?one?-1\<CR>"
1834 call assert_equal([1, 1], [line('.'), col('.')])
1835
1836 " character offset - before the first character in the file
1837 call cursor(2, 1)
1838 exe "normal ?one?s-1\<CR>"
1839 call assert_equal([1, 1], [line('.'), col('.')])
1840 call cursor(2, 1)
1841 exe "normal ?one?e-3\<CR>"
1842 call assert_equal([1, 1], [line('.'), col('.')])
1843
1844 " character offset - after the last character in the file
1845 call cursor(1, 1)
1846 exe "normal /four/s+4\<CR>"
1847 call assert_equal([2, 10], [line('.'), col('.')])
1848 call cursor(1, 1)
1849 exe "normal /four/e+1\<CR>"
1850 call assert_equal([2, 10], [line('.'), col('.')])
1851
1852 close!
1853endfunc
1854
1855" Test for searching for matching parenthesis using %
1856func Test_search_match_paren()
1857 new
1858 call setline(1, "abc(def')'ghi'('jk'\\t'lm)no")
1859 " searching for a matching parenthesis should skip single quoted characters
1860 call cursor(1, 4)
1861 normal %
1862 call assert_equal([1, 25], [line('.'), col('.')])
1863 normal %
1864 call assert_equal([1, 4], [line('.'), col('.')])
1865 call cursor(1, 5)
1866 normal ])
1867 call assert_equal([1, 25], [line('.'), col('.')])
1868 call cursor(1, 24)
1869 normal [(
1870 call assert_equal([1, 4], [line('.'), col('.')])
1871
1872 " matching parenthesis in 'virtualedit' mode with cursor after the eol
1873 call setline(1, 'abc(defgh)')
1874 set virtualedit=all
1875 normal 20|%
1876 call assert_equal(4, col('.'))
1877 set virtualedit&
1878 close!
1879endfunc
1880
1881" Test for searching a pattern and stopping before a specified line
1882func Test_search_stopline()
1883 new
1884 call setline(1, ['', '', '', 'vim'])
1885 call assert_equal(0, search('vim', 'n', 3))
1886 call assert_equal(4, search('vim', 'n', 4))
1887 call setline(1, ['vim', '', '', ''])
1888 call cursor(4, 1)
1889 call assert_equal(0, search('vim', 'bn', 2))
1890 call assert_equal(1, search('vim', 'bn', 1))
1891 close!
1892endfunc
1893
Bram Moolenaar6bed0db2020-11-25 17:41:20 +01001894func Test_incsearch_highlighting_newline()
Bram Moolenaar448465e2020-11-25 13:49:27 +01001895 CheckRunVimInTerminal
1896 CheckOption incsearch
1897 CheckScreendump
1898 new
1899 call test_override("char_avail", 1)
1900
1901 let commands =<< trim [CODE]
1902 set incsearch nohls
1903 call setline(1, ['test', 'xxx'])
1904 [CODE]
1905 call writefile(commands, 'Xincsearch_nl')
1906 let buf = RunVimInTerminal('-S Xincsearch_nl', {'rows': 5, 'cols': 10})
Bram Moolenaar448465e2020-11-25 13:49:27 +01001907 call term_sendkeys(buf, '/test')
Bram Moolenaar448465e2020-11-25 13:49:27 +01001908 call VerifyScreenDump(buf, 'Test_incsearch_newline1', {})
Bram Moolenaar6bed0db2020-11-25 17:41:20 +01001909 " Need to send one key at a time to force a redraw
Bram Moolenaar448465e2020-11-25 13:49:27 +01001910 call term_sendkeys(buf, '\n')
Bram Moolenaar448465e2020-11-25 13:49:27 +01001911 call VerifyScreenDump(buf, 'Test_incsearch_newline2', {})
1912 call term_sendkeys(buf, 'x')
Bram Moolenaar448465e2020-11-25 13:49:27 +01001913 call VerifyScreenDump(buf, 'Test_incsearch_newline3', {})
1914 call term_sendkeys(buf, 'x')
1915 call VerifyScreenDump(buf, 'Test_incsearch_newline4', {})
1916 call term_sendkeys(buf, "\<CR>")
Bram Moolenaar448465e2020-11-25 13:49:27 +01001917 call VerifyScreenDump(buf, 'Test_incsearch_newline5', {})
1918 call StopVimInTerminal(buf)
1919
1920 " clean up
1921 call delete('Xincsearch_nl')
1922 call test_override("char_avail", 0)
1923 bw
1924endfunc
1925
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01001926func Test_incsearch_substitute_dump2()
1927 CheckOption incsearch
1928 CheckScreendump
1929
1930 call writefile([
1931 \ 'set incsearch hlsearch scrolloff=0',
1932 \ 'for n in range(1, 4)',
1933 \ ' call setline(n, "foo " . n)',
1934 \ 'endfor',
1935 \ 'call setline(5, "abc|def")',
1936 \ '3',
1937 \ ], 'Xis_subst_script2')
1938 let buf = RunVimInTerminal('-S Xis_subst_script2', {'rows': 9, 'cols': 70})
1939
1940 call term_sendkeys(buf, ':%s/\vabc|')
1941 sleep 100m
1942 call VerifyScreenDump(buf, 'Test_incsearch_sub_01', {})
1943 call term_sendkeys(buf, "\<Esc>")
1944
1945 " The following should not be highlighted
1946 call term_sendkeys(buf, ':1,5s/\v|')
1947 sleep 100m
1948 call VerifyScreenDump(buf, 'Test_incsearch_sub_02', {})
1949
1950
1951 call StopVimInTerminal(buf)
1952 call delete('Xis_subst_script2')
1953endfunc
1954
Christian Brabandt78ba9332021-08-01 12:44:37 +02001955func Test_pattern_is_uppercase_smartcase()
1956 new
1957 let input=['abc', 'ABC', 'Abc', 'abC']
1958 call setline(1, input)
1959 call cursor(1,1)
1960 " default, matches firstline
1961 %s/abc//g
1962 call assert_equal(['', 'ABC', 'Abc', 'abC'],
1963 \ getline(1, '$'))
1964
1965 set smartcase ignorecase
1966 sil %d
1967 call setline(1, input)
1968 call cursor(1,1)
1969 " with smartcase and incsearch set, matches everything
1970 %s/abc//g
1971 call assert_equal(['', '', '', ''], getline(1, '$'))
1972
1973 sil %d
1974 call setline(1, input)
1975 call cursor(1,1)
1976 " with smartcase and incsearch set and found an uppercase letter,
1977 " match only that.
1978 %s/abC//g
1979 call assert_equal(['abc', 'ABC', 'Abc', ''],
1980 \ getline(1, '$'))
1981
1982 sil %d
1983 call setline(1, input)
1984 call cursor(1,1)
1985 exe "norm! vG$\<esc>"
1986 " \%V should not be detected as uppercase letter
1987 %s/\%Vabc//g
1988 call assert_equal(['', '', '', ''], getline(1, '$'))
1989
1990 call setline(1, input)
1991 call cursor(1,1)
1992 exe "norm! vG$\<esc>"
1993 " \v%V should not be detected as uppercase letter
1994 %s/\v%Vabc//g
1995 call assert_equal(['', '', '', ''], getline(1, '$'))
1996
1997 call setline(1, input)
1998 call cursor(1,1)
1999 exe "norm! vG$\<esc>"
2000 " \v%VabC should be detected as uppercase letter
2001 %s/\v%VabC//g
2002 call assert_equal(['abc', 'ABC', 'Abc', ''],
2003 \ getline(1, '$'))
2004
Christian Brabandtbc67e5a2021-08-05 15:24:59 +02002005 call setline(1, input)
2006 call cursor(1,1)
2007 " \Vabc should match everything
2008 %s/\Vabc//g
2009 call assert_equal(['', '', '', ''], getline(1, '$'))
2010
2011 call setline(1, input + ['_abc'])
2012 " _ matches normally
2013 %s/\v_.*//g
2014 call assert_equal(['abc', 'ABC', 'Abc', 'abC', ''], getline(1, '$'))
2015
Christian Brabandt78ba9332021-08-01 12:44:37 +02002016 set smartcase& ignorecase&
2017 bw!
2018endfunc
2019
Bram Moolenaard8d957d2021-10-04 19:47:35 +01002020func Test_no_last_search_pattern()
2021 CheckOption incsearch
2022
2023 let @/ = ""
2024 set incsearch
Bram Moolenaar9af9fd62021-10-04 20:09:19 +01002025 " these were causing a crash
2026 call feedkeys("//\<C-G>", 'xt')
2027 call feedkeys("//\<C-T>", 'xt')
2028 call feedkeys("??\<C-G>", 'xt')
2029 call feedkeys("??\<C-T>", 'xt')
Bram Moolenaard8d957d2021-10-04 19:47:35 +01002030endfunc
2031
Bram Moolenaar35a319b2021-10-09 13:58:55 +01002032func Test_search_with_invalid_range()
2033 new
2034 let lines =<< trim END
2035 /\%.v
2036 5/
2037 c
2038 END
2039 call writefile(lines, 'Xrangesearch')
2040 source Xrangesearch
2041
2042 bwipe!
2043 call delete('Xrangesearch')
2044endfunc
2045
Bram Moolenaard8d957d2021-10-04 19:47:35 +01002046
Bram Moolenaar07ada5f2020-02-05 20:38:22 +01002047" vim: shiftwidth=2 sts=2 expandtab