blob: 794dd1d01e0dd342cd5f3343ceeb4da9b2509bbe [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
Bram Moolenaar5ea38d12022-06-16 21:20:48 +0100331 bwipe!
332endfunc
333
334func Test_searchpair_timeout()
335 CheckFeature reltime
Bram Moolenaar73171ba2022-06-20 12:39:39 +0100336 let g:test_is_flaky = 1
Bram Moolenaar5ea38d12022-06-16 21:20:48 +0100337
338 func Waitabit()
339 sleep 20m
340 return 1 " skip match
341 endfunc
342
343 new
344 call setline(1, range(100))
345 call setline(1, "(start here")
346 call setline(100, "end here)")
347 let starttime = reltime()
348
349 " A timeout of 100 msec should happen after about five times of 20 msec wait
350 " in Waitabit(). When the timeout applies to each search the elapsed time
351 " will be much longer.
352 call assert_equal(0, searchpair('(', '\d', ')', '', "Waitabit()", 0, 100))
353 let elapsed = reltime(starttime)->reltimefloat()
354 call assert_inrange(0.09, 0.300, elapsed)
355
356 bwipe!
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100357endfunc
358
Bram Moolenaare5303952022-06-19 17:05:47 +0100359func SearchpairSkip()
360 let id = synID(line('.'), col('.'), 0)
361 let attr = synIDattr(id, 'name')
362 return attr !~ 'comment'
363endfunc
364
365func Test_searchpair_timeout_with_skip()
366 let g:test_is_flaky = 1
367
368 edit ../evalfunc.c
369 if has('win32')
370 " Windows timeouts are rather coarse grained, about 16ms.
371 let ms = 20
372 let min_time = 0.016
373 let max_time = min_time * 10.0
374 else
375 let ms = 1
376 let min_time = 0.001
377 let max_time = min_time * 10.0
Bram Moolenaare366ed42022-06-19 20:13:56 +0100378 if RunningWithValgrind()
Bram Moolenaare5303952022-06-19 17:05:47 +0100379 let max_time += 0.04 " this can be slow with valgrind
380 endif
381 endif
382 let start = reltime()
383 let found = searchpair('(', '', ')', 'crnm', 'SearchpairSkip()', 0, ms)
384 let elapsed = reltimefloat(reltime(start))
385 call assert_inrange(min_time, max_time, elapsed)
386
387 bwipe!
388endfunc
389
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100390func Test_searchpairpos()
391 new
392 call setline(1, ['other code', 'here [', ' [', ' " cursor here', ' ]]'])
393
394 4
395 call assert_equal([3, 2], searchpairpos('\[', '', ']', 'bW'))
396 call assert_equal([0, 3, 2, 0], getpos('.'))
397 4
398 call assert_equal([2, 6], searchpairpos('\[', '', ']', 'bWr'))
399 call assert_equal([0, 2, 6, 0], getpos('.'))
400 4|norm ^
401 call assert_equal([5, 2], searchpairpos('\[', '', ']', 'Wn'))
402 call assert_equal([0, 4, 2, 0], getpos('.'))
403 4
404 call assert_equal([2, 6], searchpairpos('\[', '', ']', 'bW',
405 \ 'getline(".") =~ "^\\s*\["'))
406 call assert_equal([0, 2, 6, 0], getpos('.'))
407 4
408 call assert_equal([2, 6], searchpairpos('\[', '', ']', 'bWr'))
409 call assert_equal([0, 2, 6, 0], getpos('.'))
410 set nomagic
411 4
412 call assert_equal([3, 2], searchpairpos('\[', '', ']', 'bW'))
413 call assert_equal([0, 3, 2, 0], getpos('.'))
414 set magic
415 4|norm ^
416 call assert_equal([0, 0], searchpairpos('{', '', '}', 'bW'))
417 call assert_equal([0, 4, 2, 0], getpos('.'))
418
419 %d
420 call setline(1, ['if 1', ' if 2', ' else', ' endif 2', 'endif 1'])
421 /\<if 1
422 call assert_equal([5, 1], searchpairpos('\<if\>', '\<else\>', '\<endif\>', 'W'))
423 call assert_equal([0, 5, 1, 0], getpos('.'))
424 /\<if 2
425 call assert_equal([3, 3], searchpairpos('\<if\>', '\<else\>', '\<endif\>', 'W'))
426 call assert_equal([0, 3, 3, 0], getpos('.'))
427
Bram Moolenaar6e450a52017-01-06 20:03:58 +0100428 q!
429endfunc
430
Bram Moolenaar3dddb092018-06-24 19:01:59 +0200431func Test_searchpair_errors()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200432 call assert_fails("call searchpair([0], 'middle', 'end', 'bW', 'skip', 99, 100)", 'E730: Using a List as a String')
433 call assert_fails("call searchpair('start', {-> 0}, 'end', 'bW', 'skip', 99, 100)", 'E729: Using a Funcref as a String')
434 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 +0200435 call assert_fails("call searchpair('start', 'middle', 'end', 'flags', 'skip', 99, 100)", 'E475: Invalid argument: flags')
Bram Moolenaar3dddb092018-06-24 19:01:59 +0200436 call assert_fails("call searchpair('start', 'middle', 'end', 'bW', 'func', -99, 100)", 'E475: Invalid argument: -99')
437 call assert_fails("call searchpair('start', 'middle', 'end', 'bW', 'func', 99, -100)", 'E475: Invalid argument: -100')
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100438 call assert_fails("call searchpair('start', 'middle', 'end', 'e')", 'E475: Invalid argument: e')
439 call assert_fails("call searchpair('start', 'middle', 'end', 'sn')", 'E475: Invalid argument: sn')
440endfunc
441
442func Test_searchpairpos_errors()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200443 call assert_fails("call searchpairpos([0], 'middle', 'end', 'bW', 'skip', 99, 100)", 'E730: Using a List as a String')
444 call assert_fails("call searchpairpos('start', {-> 0}, 'end', 'bW', 'skip', 99, 100)", 'E729: Using a Funcref as a String')
445 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 +0100446 call assert_fails("call searchpairpos('start', 'middle', 'end', 'flags', 'skip', 99, 100)", 'E475: Invalid argument: flags')
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100447 call assert_fails("call searchpairpos('start', 'middle', 'end', 'bW', 'func', -99, 100)", 'E475: Invalid argument: -99')
448 call assert_fails("call searchpairpos('start', 'middle', 'end', 'bW', 'func', 99, -100)", 'E475: Invalid argument: -100')
449 call assert_fails("call searchpairpos('start', 'middle', 'end', 'e')", 'E475: Invalid argument: e')
450 call assert_fails("call searchpairpos('start', 'middle', 'end', 'sn')", 'E475: Invalid argument: sn')
Bram Moolenaar3dddb092018-06-24 19:01:59 +0200451endfunc
452
Bram Moolenaar48570482017-10-30 21:48:41 +0100453func Test_searchpair_skip()
454 func Zero()
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100455 return 0
Bram Moolenaar48570482017-10-30 21:48:41 +0100456 endfunc
457 func Partial(x)
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100458 return a:x
Bram Moolenaar48570482017-10-30 21:48:41 +0100459 endfunc
460 new
461 call setline(1, ['{', 'foo', 'foo', 'foo', '}'])
462 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', ''))
463 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', '0'))
464 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', {-> 0}))
465 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', function('Zero')))
466 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', function('Partial', [0])))
Bram Moolenaar48570482017-10-30 21:48:41 +0100467 bw!
468endfunc
469
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200470func Test_searchpair_leak()
471 new
472 call setline(1, 'if one else another endif')
473
474 " The error in the skip expression caused memory to leak.
475 call assert_fails("call searchpair('\\<if\\>', '\\<else\\>', '\\<endif\\>', '', '\"foo\" 2')", 'E15:')
476
477 bwipe!
478endfunc
479
Bram Moolenaar66727e12017-03-01 22:17:05 +0100480func Test_searchc()
481 " These commands used to cause memory overflow in searchc().
482 new
483 norm ixx
484 exe "norm 0t\u93cf"
485 bw!
486endfunc
Bram Moolenaara693d052017-06-29 22:23:06 +0200487
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200488func Cmdline3_prep()
489 " need to disable char_avail,
490 " so that expansion of commandline works
491 call test_override("char_avail", 1)
492 new
493 call setline(1, [' 1', ' 2 the~e', ' 3 the theother'])
494 set incsearch
495endfunc
496
Bram Moolenaar976b8472018-08-12 15:49:47 +0200497func Incsearch_cleanup()
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200498 set noincsearch
499 call test_override("char_avail", 0)
500 bw!
501endfunc
502
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200503func Test_search_cmdline3()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100504 CheckOption incsearch
505
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200506 call Cmdline3_prep()
507 1
508 " first match
509 call feedkeys("/the\<c-l>\<cr>", 'tx')
510 call assert_equal(' 2 the~e', getline('.'))
511
Bram Moolenaar976b8472018-08-12 15:49:47 +0200512 call Incsearch_cleanup()
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200513endfunc
514
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200515func Test_search_cmdline3s()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100516 CheckOption incsearch
517
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200518 call Cmdline3_prep()
519 1
520 call feedkeys(":%s/the\<c-l>/xxx\<cr>", 'tx')
521 call assert_equal(' 2 xxxe', getline('.'))
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200522 undo
523 call feedkeys(":%subs/the\<c-l>/xxx\<cr>", 'tx')
524 call assert_equal(' 2 xxxe', getline('.'))
525 undo
526 call feedkeys(":%substitute/the\<c-l>/xxx\<cr>", 'tx')
527 call assert_equal(' 2 xxxe', getline('.'))
Bram Moolenaar167ae422018-08-14 21:32:21 +0200528 undo
529 call feedkeys(":%smagic/the.e/xxx\<cr>", 'tx')
530 call assert_equal(' 2 xxx', getline('.'))
531 undo
Bram Moolenaare2e40752020-09-04 21:18:46 +0200532 call assert_fails(":%snomagic/the.e/xxx\<cr>", 'E486:')
Bram Moolenaar167ae422018-08-14 21:32:21 +0200533 "
534 call feedkeys(":%snomagic/the\\.e/xxx\<cr>", 'tx')
535 call assert_equal(' 2 xxx', getline('.'))
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200536
Bram Moolenaar976b8472018-08-12 15:49:47 +0200537 call Incsearch_cleanup()
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200538endfunc
539
540func Test_search_cmdline3g()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100541 CheckOption incsearch
542
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200543 call Cmdline3_prep()
544 1
545 call feedkeys(":g/the\<c-l>/d\<cr>", 'tx')
546 call assert_equal(' 3 the theother', getline(2))
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200547 undo
548 call feedkeys(":global/the\<c-l>/d\<cr>", 'tx')
549 call assert_equal(' 3 the theother', getline(2))
Bram Moolenaardef7b1d2018-08-13 22:54:35 +0200550 undo
551 call feedkeys(":g!/the\<c-l>/d\<cr>", 'tx')
552 call assert_equal(1, line('$'))
553 call assert_equal(' 2 the~e', getline(1))
554 undo
555 call feedkeys(":global!/the\<c-l>/d\<cr>", 'tx')
556 call assert_equal(1, line('$'))
557 call assert_equal(' 2 the~e', getline(1))
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200558
Bram Moolenaar976b8472018-08-12 15:49:47 +0200559 call Incsearch_cleanup()
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200560endfunc
561
562func Test_search_cmdline3v()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100563 CheckOption incsearch
564
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200565 call Cmdline3_prep()
566 1
567 call feedkeys(":v/the\<c-l>/d\<cr>", 'tx')
568 call assert_equal(1, line('$'))
569 call assert_equal(' 2 the~e', getline(1))
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200570 undo
571 call feedkeys(":vglobal/the\<c-l>/d\<cr>", 'tx')
572 call assert_equal(1, line('$'))
573 call assert_equal(' 2 the~e', getline(1))
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200574
Bram Moolenaar976b8472018-08-12 15:49:47 +0200575 call Incsearch_cleanup()
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200576endfunc
577
Bram Moolenaarda5116d2017-07-01 23:11:17 +0200578func Test_search_cmdline4()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100579 CheckOption incsearch
580
Bram Moolenaarda5116d2017-07-01 23:11:17 +0200581 " need to disable char_avail,
582 " so that expansion of commandline works
583 call test_override("char_avail", 1)
584 new
585 call setline(1, [' 1 the first', ' 2 the second', ' 3 the third'])
586 set incsearch
587 $
588 call feedkeys("?the\<c-g>\<cr>", 'tx')
589 call assert_equal(' 3 the third', getline('.'))
590 $
591 call feedkeys("?the\<c-g>\<c-g>\<cr>", 'tx')
592 call assert_equal(' 1 the first', getline('.'))
593 $
594 call feedkeys("?the\<c-g>\<c-g>\<c-g>\<cr>", 'tx')
595 call assert_equal(' 2 the second', getline('.'))
596 $
597 call feedkeys("?the\<c-t>\<cr>", 'tx')
598 call assert_equal(' 1 the first', getline('.'))
599 $
600 call feedkeys("?the\<c-t>\<c-t>\<cr>", 'tx')
601 call assert_equal(' 3 the third', getline('.'))
602 $
603 call feedkeys("?the\<c-t>\<c-t>\<c-t>\<cr>", 'tx')
604 call assert_equal(' 2 the second', getline('.'))
605 " clean up
606 set noincsearch
607 call test_override("char_avail", 0)
608 bw!
609endfunc
Bram Moolenaardb510072017-09-28 21:52:17 +0200610
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200611func Test_search_cmdline5()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100612 CheckOption incsearch
613
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200614 " Do not call test_override("char_avail", 1) so that <C-g> and <C-t> work
615 " regardless char_avail.
616 new
Bram Moolenaar69a5b862019-07-16 21:38:51 +0200617 call setline(1, [' 1 the first', ' 2 the second', ' 3 the third', ''])
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200618 set incsearch
619 1
620 call feedkeys("/the\<c-g>\<c-g>\<cr>", 'tx')
621 call assert_equal(' 3 the third', getline('.'))
622 $
623 call feedkeys("?the\<c-t>\<c-t>\<c-t>\<cr>", 'tx')
Bram Moolenaar69a5b862019-07-16 21:38:51 +0200624 call assert_equal(' 1 the first', getline('.'))
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200625 " clean up
626 set noincsearch
627 bw!
628endfunc
629
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100630func Test_search_cmdline6()
631 " Test that consecutive matches
632 " are caught by <c-g>/<c-t>
Bram Moolenaarb68df222020-03-19 15:05:28 +0100633 CheckOption incsearch
634
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100635 " need to disable char_avail,
636 " so that expansion of commandline works
637 call test_override("char_avail", 1)
638 new
639 call setline(1, [' bbvimb', ''])
640 set incsearch
641 " first match
642 norm! gg0
643 call feedkeys("/b\<cr>", 'tx')
644 call assert_equal([0,1,2,0], getpos('.'))
645 " second match
646 norm! gg0
647 call feedkeys("/b\<c-g>\<cr>", 'tx')
648 call assert_equal([0,1,3,0], getpos('.'))
649 " third match
650 norm! gg0
651 call feedkeys("/b\<c-g>\<c-g>\<cr>", 'tx')
652 call assert_equal([0,1,7,0], getpos('.'))
653 " first match again
654 norm! gg0
655 call feedkeys("/b\<c-g>\<c-g>\<c-g>\<cr>", 'tx')
656 call assert_equal([0,1,2,0], getpos('.'))
657 set nowrapscan
658 " last match
659 norm! gg0
660 call feedkeys("/b\<c-g>\<c-g>\<c-g>\<cr>", 'tx')
661 call assert_equal([0,1,7,0], getpos('.'))
662 " clean up
663 set wrapscan&vim
664 set noincsearch
665 call test_override("char_avail", 0)
666 bw!
667endfunc
668
669func Test_search_cmdline7()
Bram Moolenaard66cdcd2020-07-26 13:27:16 +0200670 " Test that pressing <c-g> in an empty command line
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100671 " does not move the cursor
Bram Moolenaarb68df222020-03-19 15:05:28 +0100672 CheckOption incsearch
673
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100674 " need to disable char_avail,
675 " so that expansion of commandline works
676 call test_override("char_avail", 1)
677 new
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200678 let @/ = 'b'
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100679 call setline(1, [' bbvimb', ''])
680 set incsearch
681 " first match
682 norm! gg0
683 " moves to next match of previous search pattern, just like /<cr>
684 call feedkeys("/\<c-g>\<cr>", 'tx')
685 call assert_equal([0,1,2,0], getpos('.'))
686 " moves to next match of previous search pattern, just like /<cr>
687 call feedkeys("/\<cr>", 'tx')
688 call assert_equal([0,1,3,0], getpos('.'))
689 " moves to next match of previous search pattern, just like /<cr>
690 call feedkeys("/\<c-t>\<cr>", 'tx')
691 call assert_equal([0,1,7,0], getpos('.'))
Bram Moolenaard0480092017-11-16 22:20:39 +0100692
693 " using an offset uses the last search pattern
694 call cursor(1, 1)
695 call setline(1, ['1 bbvimb', ' 2 bbvimb'])
696 let @/ = 'b'
697 call feedkeys("//e\<c-g>\<cr>", 'tx')
698 call assert_equal('1 bbvimb', getline('.'))
699 call assert_equal(4, col('.'))
700
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100701 set noincsearch
702 call test_override("char_avail", 0)
703 bw!
704endfunc
705
706func Test_search_cmdline8()
707 " Highlighting is cleared in all windows
708 " since hls applies to all windows
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200709 CheckOption incsearch
710 CheckFeature terminal
711 CheckNotGui
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100712 if has("win32")
713 throw "Skipped: Bug with sending <ESC> to terminal window not fixed yet"
714 endif
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200715
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100716 let h = winheight(0)
717 if h < 3
718 return
719 endif
720 " Prepare buffer text
721 let lines = ['abb vim vim vi', 'vimvivim']
722 call writefile(lines, 'Xsearch.txt')
Bram Moolenaar13deab82017-11-04 18:48:43 +0100723 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', 'Xsearch.txt'], {'term_rows': 3})
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100724
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200725 call WaitForAssert({-> assert_equal(lines, [term_getline(buf, 1), term_getline(buf, 2)])})
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100726
Bram Moolenaar13deab82017-11-04 18:48:43 +0100727 call term_sendkeys(buf, ":set incsearch hlsearch\<cr>")
728 call term_sendkeys(buf, ":14vsp\<cr>")
729 call term_sendkeys(buf, "/vim\<cr>")
730 call term_sendkeys(buf, "/b\<esc>")
731 call term_sendkeys(buf, "gg0")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200732 call TermWait(buf, 250)
Bram Moolenaar13deab82017-11-04 18:48:43 +0100733 let screen_line = term_scrape(buf, 1)
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100734 let [a0,a1,a2,a3] = [screen_line[3].attr, screen_line[4].attr,
735 \ screen_line[18].attr, screen_line[19].attr]
736 call assert_notequal(a0, a1)
737 call assert_notequal(a0, a3)
738 call assert_notequal(a1, a2)
739 call assert_equal(a0, a2)
740 call assert_equal(a1, a3)
741 " clean up
742 call delete('Xsearch.txt')
743
744 bwipe!
745endfunc
746
Bram Moolenaardb510072017-09-28 21:52:17 +0200747" Tests for regexp with various magic settings
Bram Moolenaar4d23c522020-04-09 18:42:11 +0200748func Run_search_regexp_magic_opt()
Bram Moolenaardb510072017-09-28 21:52:17 +0200749 put ='1 a aa abb abbccc'
750 exe 'normal! /a*b\{2}c\+/e' . "\<CR>"
751 call assert_equal([0, 2, 17, 0], getpos('.'))
752
753 put ='2 d dd dee deefff'
754 exe 'normal! /\Md\*e\{2}f\+/e' . "\<CR>"
755 call assert_equal([0, 3, 17, 0], getpos('.'))
756
757 set nomagic
758 put ='3 g gg ghh ghhiii'
759 exe 'normal! /g\*h\{2}i\+/e' . "\<CR>"
760 call assert_equal([0, 4, 17, 0], getpos('.'))
761
762 put ='4 j jj jkk jkklll'
763 exe 'normal! /\mj*k\{2}l\+/e' . "\<CR>"
764 call assert_equal([0, 5, 17, 0], getpos('.'))
765
766 put ='5 m mm mnn mnnooo'
767 exe 'normal! /\vm*n{2}o+/e' . "\<CR>"
768 call assert_equal([0, 6, 17, 0], getpos('.'))
769
770 put ='6 x ^aa$ x'
771 exe 'normal! /\V^aa$' . "\<CR>"
772 call assert_equal([0, 7, 5, 0], getpos('.'))
773
774 set magic
775 put ='7 (a)(b) abbaa'
776 exe 'normal! /\v(a)(b)\2\1\1/e' . "\<CR>"
777 call assert_equal([0, 8, 14, 0], getpos('.'))
778
779 put ='8 axx [ab]xx'
780 exe 'normal! /\V[ab]\(\[xy]\)\1' . "\<CR>"
781 call assert_equal([0, 9, 7, 0], getpos('.'))
782
Bram Moolenaar4d23c522020-04-09 18:42:11 +0200783 %d
784endfunc
785
786func Test_search_regexp()
787 enew!
788
789 set regexpengine=1
790 call Run_search_regexp_magic_opt()
791 set regexpengine=2
792 call Run_search_regexp_magic_opt()
793 set regexpengine&
794
Bram Moolenaardb510072017-09-28 21:52:17 +0200795 set undolevels=100
796 put ='9 foobar'
797 put =''
798 exe "normal! a\<C-G>u\<Esc>"
799 normal G
800 exe 'normal! dv?bar?' . "\<CR>"
801 call assert_equal('9 foo', getline('.'))
Bram Moolenaar4d23c522020-04-09 18:42:11 +0200802 call assert_equal([0, 2, 5, 0], getpos('.'))
803 call assert_equal(2, line('$'))
Bram Moolenaardb510072017-09-28 21:52:17 +0200804 normal u
805 call assert_equal('9 foobar', getline('.'))
Bram Moolenaar4d23c522020-04-09 18:42:11 +0200806 call assert_equal([0, 2, 6, 0], getpos('.'))
807 call assert_equal(3, line('$'))
Bram Moolenaardb510072017-09-28 21:52:17 +0200808
809 set undolevels&
810 enew!
811endfunc
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100812
813func Test_search_cmdline_incsearch_highlight()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100814 CheckOption incsearch
815
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100816 set incsearch hlsearch
817 " need to disable char_avail,
818 " so that expansion of commandline works
819 call test_override("char_avail", 1)
820 new
821 call setline(1, ['aaa 1 the first', ' 2 the second', ' 3 the third'])
822
823 1
824 call feedkeys("/second\<cr>", 'tx')
825 call assert_equal('second', @/)
826 call assert_equal(' 2 the second', getline('.'))
827
828 " Canceling search won't change @/
829 1
830 let @/ = 'last pattern'
831 call feedkeys("/third\<C-c>", 'tx')
832 call assert_equal('last pattern', @/)
833 call feedkeys("/third\<Esc>", 'tx')
834 call assert_equal('last pattern', @/)
835 call feedkeys("/3\<bs>\<bs>", 'tx')
836 call assert_equal('last pattern', @/)
837 call feedkeys("/third\<c-g>\<c-t>\<Esc>", 'tx')
838 call assert_equal('last pattern', @/)
839
840 " clean up
841 set noincsearch nohlsearch
842 bw!
843endfunc
844
845func Test_search_cmdline_incsearch_highlight_attr()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200846 CheckOption incsearch
847 CheckFeature terminal
848 CheckNotGui
849
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100850 let h = winheight(0)
851 if h < 3
852 return
853 endif
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100854
855 " Prepare buffer text
Bram Moolenaar13deab82017-11-04 18:48:43 +0100856 let lines = ['abb vim vim vi', 'vimvivim']
857 call writefile(lines, 'Xsearch.txt')
858 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', 'Xsearch.txt'], {'term_rows': 3})
859
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200860 call WaitForAssert({-> assert_equal(lines, [term_getline(buf, 1), term_getline(buf, 2)])})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100861 " wait for vim to complete initialization
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200862 call TermWait(buf)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100863
864 " Get attr of normal(a0), incsearch(a1), hlsearch(a2) highlight
Bram Moolenaar13deab82017-11-04 18:48:43 +0100865 call term_sendkeys(buf, ":set incsearch hlsearch\<cr>")
866 call term_sendkeys(buf, '/b')
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200867 call TermWait(buf, 100)
Bram Moolenaar13deab82017-11-04 18:48:43 +0100868 let screen_line1 = term_scrape(buf, 1)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100869 call assert_true(len(screen_line1) > 2)
870 " a0: attr_normal
871 let a0 = screen_line1[0].attr
872 " a1: attr_incsearch
873 let a1 = screen_line1[1].attr
874 " a2: attr_hlsearch
875 let a2 = screen_line1[2].attr
876 call assert_notequal(a0, a1)
877 call assert_notequal(a0, a2)
878 call assert_notequal(a1, a2)
Bram Moolenaar13deab82017-11-04 18:48:43 +0100879 call term_sendkeys(buf, "\<cr>gg0")
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100880
881 " Test incremental highlight search
Bram Moolenaar13deab82017-11-04 18:48:43 +0100882 call term_sendkeys(buf, "/vim")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200883 call TermWait(buf, 100)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100884 " Buffer:
885 " abb vim vim vi
886 " vimvivim
887 " Search: /vim
888 let attr_line1 = [a0,a0,a0,a0,a1,a1,a1,a0,a2,a2,a2,a0,a0,a0]
889 let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100890 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
891 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100892
893 " Test <C-g>
Bram Moolenaar13deab82017-11-04 18:48:43 +0100894 call term_sendkeys(buf, "\<C-g>\<C-g>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200895 call TermWait(buf, 100)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100896 let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a2,a2,a2,a0,a0,a0]
897 let attr_line2 = [a1,a1,a1,a0,a0,a2,a2,a2]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100898 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
899 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100900
901 " Test <C-t>
Bram Moolenaar13deab82017-11-04 18:48:43 +0100902 call term_sendkeys(buf, "\<C-t>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200903 call TermWait(buf, 100)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100904 let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a1,a1,a1,a0,a0,a0]
905 let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100906 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
907 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100908
909 " Type Enter and a1(incsearch highlight) should become a2(hlsearch highlight)
Bram Moolenaar13deab82017-11-04 18:48:43 +0100910 call term_sendkeys(buf, "\<cr>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200911 call TermWait(buf, 100)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100912 let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a2,a2,a2,a0,a0,a0]
913 let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100914 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
915 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100916
917 " Test nohlsearch. a2(hlsearch highlight) should become a0(normal highlight)
Bram Moolenaar13deab82017-11-04 18:48:43 +0100918 call term_sendkeys(buf, ":1\<cr>")
919 call term_sendkeys(buf, ":set nohlsearch\<cr>")
920 call term_sendkeys(buf, "/vim")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200921 call TermWait(buf, 100)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100922 let attr_line1 = [a0,a0,a0,a0,a1,a1,a1,a0,a0,a0,a0,a0,a0,a0]
923 let attr_line2 = [a0,a0,a0,a0,a0,a0,a0,a0]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100924 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
925 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100926 call delete('Xsearch.txt')
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100927
Bram Moolenaarb94340c2017-11-02 16:16:31 +0100928 call delete('Xsearch.txt')
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100929 bwipe!
930endfunc
Bram Moolenaarf45938c2017-11-02 15:59:57 +0100931
Bram Moolenaar548e5982018-12-26 21:45:00 +0100932func Test_incsearch_cmdline_modifier()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100933 CheckOption incsearch
934
Bram Moolenaar548e5982018-12-26 21:45:00 +0100935 call test_override("char_avail", 1)
936 new
937 call setline(1, ['foo'])
938 set incsearch
939 " Test that error E14 does not occur in parsing command modifier.
940 call feedkeys("V:tab", 'tx')
941
942 call Incsearch_cleanup()
943endfunc
944
Bram Moolenaar9d34d902018-04-27 22:18:12 +0200945func Test_incsearch_scrolling()
Bram Moolenaar494e9062020-05-31 21:28:02 +0200946 CheckRunVimInTerminal
Bram Moolenaar9d34d902018-04-27 22:18:12 +0200947 call assert_equal(0, &scrolloff)
948 call writefile([
949 \ 'let dots = repeat(".", 120)',
950 \ 'set incsearch cmdheight=2 scrolloff=0',
951 \ 'call setline(1, [dots, dots, dots, "", "target", dots, dots])',
952 \ 'normal gg',
953 \ 'redraw',
954 \ ], 'Xscript')
955 let buf = RunVimInTerminal('-S Xscript', {'rows': 9, 'cols': 70})
956 " Need to send one key at a time to force a redraw
957 call term_sendkeys(buf, '/')
958 sleep 100m
959 call term_sendkeys(buf, 't')
960 sleep 100m
961 call term_sendkeys(buf, 'a')
962 sleep 100m
963 call term_sendkeys(buf, 'r')
964 sleep 100m
965 call term_sendkeys(buf, 'g')
966 call VerifyScreenDump(buf, 'Test_incsearch_scrolling_01', {})
967
968 call term_sendkeys(buf, "\<Esc>")
969 call StopVimInTerminal(buf)
970 call delete('Xscript')
971endfunc
972
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200973func Test_incsearch_search_dump()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100974 CheckOption incsearch
975 CheckScreendump
976
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200977 call writefile([
978 \ 'set incsearch hlsearch scrolloff=0',
979 \ 'for n in range(1, 8)',
980 \ ' call setline(n, "foo " . n)',
981 \ 'endfor',
982 \ '3',
983 \ ], 'Xis_search_script')
984 let buf = RunVimInTerminal('-S Xis_search_script', {'rows': 9, 'cols': 70})
985 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
986 " the 'ambiwidth' check.
987 sleep 100m
988
989 " Need to send one key at a time to force a redraw.
990 call term_sendkeys(buf, '/fo')
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200991 call VerifyScreenDump(buf, 'Test_incsearch_search_01', {})
992 call term_sendkeys(buf, "\<Esc>")
993 sleep 100m
994
995 call term_sendkeys(buf, '/\v')
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200996 call VerifyScreenDump(buf, 'Test_incsearch_search_02', {})
997 call term_sendkeys(buf, "\<Esc>")
998
999 call StopVimInTerminal(buf)
1000 call delete('Xis_search_script')
1001endfunc
1002
Bram Moolenaar41f08952021-02-22 22:13:49 +01001003func Test_hlsearch_dump()
1004 CheckOption hlsearch
1005 CheckScreendump
1006
1007 call writefile([
1008 \ 'set hlsearch cursorline',
1009 \ 'call setline(1, ["xxx", "xxx", "xxx"])',
1010 \ '/.*',
1011 \ '2',
1012 \ ], 'Xhlsearch_script')
1013 let buf = RunVimInTerminal('-S Xhlsearch_script', {'rows': 6, 'cols': 50})
1014 call VerifyScreenDump(buf, 'Test_hlsearch_1', {})
1015
1016 call term_sendkeys(buf, "/\\_.*\<CR>")
1017 call VerifyScreenDump(buf, 'Test_hlsearch_2', {})
1018
1019 call StopVimInTerminal(buf)
1020 call delete('Xhlsearch_script')
1021endfunc
1022
Bram Moolenaar2d5f3852021-04-21 15:11:42 +02001023func Test_hlsearch_and_visual()
1024 CheckOption hlsearch
1025 CheckScreendump
1026
1027 call writefile([
1028 \ 'set hlsearch',
1029 \ 'call setline(1, repeat(["xxx yyy zzz"], 3))',
1030 \ 'hi Search cterm=bold',
1031 \ '/yyy',
1032 \ 'call cursor(1, 6)',
1033 \ ], 'Xhlvisual_script')
1034 let buf = RunVimInTerminal('-S Xhlvisual_script', {'rows': 6, 'cols': 40})
1035 call term_sendkeys(buf, "vjj")
1036 call VerifyScreenDump(buf, 'Test_hlsearch_visual_1', {})
1037 call term_sendkeys(buf, "\<Esc>")
1038
1039 call StopVimInTerminal(buf)
1040 call delete('Xhlvisual_script')
1041endfunc
1042
Bram Moolenaare71c0eb2021-05-30 16:43:11 +02001043func Test_hlsearch_block_visual_match()
1044 CheckScreendump
1045
1046 let lines =<< trim END
1047 set hlsearch
1048 call setline(1, ['aa', 'bbbb', 'cccccc'])
1049 END
1050 call writefile(lines, 'Xhlsearch_block')
1051 let buf = RunVimInTerminal('-S Xhlsearch_block', {'rows': 9, 'cols': 60})
1052
1053 call term_sendkeys(buf, "G\<C-V>$kk\<Esc>")
1054 sleep 100m
1055 call term_sendkeys(buf, "/\\%V\<CR>")
1056 sleep 100m
1057 call VerifyScreenDump(buf, 'Test_hlsearch_block_visual_match', {})
1058
1059 call StopVimInTerminal(buf)
1060 call delete('Xhlsearch_block')
1061endfunc
1062
Bram Moolenaar976b8472018-08-12 15:49:47 +02001063func Test_incsearch_substitute()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001064 CheckOption incsearch
1065
Bram Moolenaar976b8472018-08-12 15:49:47 +02001066 call test_override("char_avail", 1)
1067 new
1068 set incsearch
1069 for n in range(1, 10)
1070 call setline(n, 'foo ' . n)
1071 endfor
1072 4
1073 call feedkeys(":.,.+2s/foo\<BS>o\<BS>o/xxx\<cr>", 'tx')
1074 call assert_equal('foo 3', getline(3))
1075 call assert_equal('xxx 4', getline(4))
1076 call assert_equal('xxx 5', getline(5))
1077 call assert_equal('xxx 6', getline(6))
1078 call assert_equal('foo 7', getline(7))
1079
1080 call Incsearch_cleanup()
1081endfunc
1082
Bram Moolenaar795aaa12020-10-02 20:36:01 +02001083func Test_incsearch_substitute_long_line()
1084 new
1085 call test_override("char_avail", 1)
1086 set incsearch
1087
1088 call repeat('x', 100000)->setline(1)
1089 call feedkeys(':s/\%c', 'xt')
1090 redraw
1091 call feedkeys("\<Esc>", 'xt')
1092
1093 call Incsearch_cleanup()
1094 bwipe!
1095endfunc
1096
LemonBoya4399382022-04-09 21:04:08 +01001097func Test_hlsearch_cursearch()
1098 CheckScreendump
1099
1100 let lines =<< trim END
1101 set hlsearch scrolloff=0
Bram Moolenaar9b367502022-04-22 20:07:21 +01001102 call setline(1, ['one', 'foo', 'bar', 'baz', 'foo the foo and foo', 'bar'])
LemonBoya4399382022-04-09 21:04:08 +01001103 hi Search ctermbg=yellow
1104 hi CurSearch ctermbg=blue
1105 END
1106 call writefile(lines, 'Xhlsearch_cursearch')
1107 let buf = RunVimInTerminal('-S Xhlsearch_cursearch', {'rows': 9, 'cols': 60})
1108
1109 call term_sendkeys(buf, "gg/foo\<CR>")
1110 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_single_line_1', {})
1111
1112 call term_sendkeys(buf, "n")
1113 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_single_line_2', {})
1114
Bram Moolenaar9b367502022-04-22 20:07:21 +01001115 call term_sendkeys(buf, "n")
1116 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_single_line_2a', {})
1117
1118 call term_sendkeys(buf, "n")
1119 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_single_line_2b', {})
1120
1121 call term_sendkeys(buf, ":call setline(5, 'foo')\<CR>")
1122 call term_sendkeys(buf, "0?\<CR>")
LemonBoya4399382022-04-09 21:04:08 +01001123 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_single_line_3', {})
1124
1125 call term_sendkeys(buf, "gg/foo\\nbar\<CR>")
Bram Moolenaar693ccd12022-04-16 12:04:37 +01001126 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_multiple_line_1', {})
1127
1128 call term_sendkeys(buf, ":call setline(1, ['---', 'abcdefg', 'hijkl', '---', 'abcdefg', 'hijkl'])\<CR>")
1129 call term_sendkeys(buf, "gg/efg\\nhij\<CR>")
1130 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_multiple_line_2', {})
1131 call term_sendkeys(buf, "h\<C-L>")
1132 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_multiple_line_3', {})
1133 call term_sendkeys(buf, "j\<C-L>")
1134 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_multiple_line_4', {})
1135 call term_sendkeys(buf, "h\<C-L>")
1136 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_multiple_line_5', {})
LemonBoya4399382022-04-09 21:04:08 +01001137
Bram Moolenaar368137a2022-05-31 13:43:12 +01001138 " check clearing CurSearch when using it for another match
1139 call term_sendkeys(buf, "G?^abcd\<CR>Y")
1140 call term_sendkeys(buf, "kkP")
1141 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_changed_1', {})
1142
LemonBoya4399382022-04-09 21:04:08 +01001143 call StopVimInTerminal(buf)
1144 call delete('Xhlsearch_cursearch')
1145endfunc
1146
Bram Moolenaar164251f2018-08-12 16:26:58 +02001147" Similar to Test_incsearch_substitute() but with a screendump halfway.
1148func Test_incsearch_substitute_dump()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001149 CheckOption incsearch
1150 CheckScreendump
1151
Bram Moolenaar164251f2018-08-12 16:26:58 +02001152 call writefile([
1153 \ 'set incsearch hlsearch scrolloff=0',
1154 \ 'for n in range(1, 10)',
1155 \ ' call setline(n, "foo " . n)',
1156 \ 'endfor',
Bram Moolenaar2f6a3462018-08-14 18:16:52 +02001157 \ 'call setline(11, "bar 11")',
Bram Moolenaar164251f2018-08-12 16:26:58 +02001158 \ '3',
1159 \ ], 'Xis_subst_script')
1160 let buf = RunVimInTerminal('-S Xis_subst_script', {'rows': 9, 'cols': 70})
1161 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1162 " the 'ambiwidth' check.
1163 sleep 100m
1164
1165 " Need to send one key at a time to force a redraw.
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02001166 " Select three lines at the cursor with typed pattern.
Bram Moolenaar164251f2018-08-12 16:26:58 +02001167 call term_sendkeys(buf, ':.,.+2s/')
1168 sleep 100m
1169 call term_sendkeys(buf, 'f')
1170 sleep 100m
1171 call term_sendkeys(buf, 'o')
1172 sleep 100m
1173 call term_sendkeys(buf, 'o')
1174 call VerifyScreenDump(buf, 'Test_incsearch_substitute_01', {})
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02001175 call term_sendkeys(buf, "\<Esc>")
1176
1177 " Select three lines at the cursor using previous pattern.
1178 call term_sendkeys(buf, "/foo\<CR>")
1179 sleep 100m
1180 call term_sendkeys(buf, ':.,.+2s//')
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02001181 call VerifyScreenDump(buf, 'Test_incsearch_substitute_02', {})
1182
1183 " Deleting last slash should remove the match.
1184 call term_sendkeys(buf, "\<BS>")
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02001185 call VerifyScreenDump(buf, 'Test_incsearch_substitute_03', {})
Bram Moolenaar60d08712018-08-12 21:53:15 +02001186 call term_sendkeys(buf, "\<Esc>")
1187
1188 " Reverse range is accepted
1189 call term_sendkeys(buf, ':5,2s/foo')
Bram Moolenaar60d08712018-08-12 21:53:15 +02001190 call VerifyScreenDump(buf, 'Test_incsearch_substitute_04', {})
Bram Moolenaar164251f2018-08-12 16:26:58 +02001191 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaar2b926fc2018-08-13 11:07:57 +02001192
1193 " White space after the command is skipped
1194 call term_sendkeys(buf, ':2,3sub /fo')
Bram Moolenaar2b926fc2018-08-13 11:07:57 +02001195 call VerifyScreenDump(buf, 'Test_incsearch_substitute_05', {})
1196 call term_sendkeys(buf, "\<Esc>")
1197
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001198 " Command modifiers are skipped
1199 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 +02001200 call VerifyScreenDump(buf, 'Test_incsearch_substitute_06', {})
1201 call term_sendkeys(buf, "\<Esc>")
1202
Bram Moolenaar2f6a3462018-08-14 18:16:52 +02001203 " Cursorline highlighting at match
1204 call term_sendkeys(buf, ":set cursorline\<CR>")
1205 call term_sendkeys(buf, 'G9G')
1206 call term_sendkeys(buf, ':9,11s/bar')
Bram Moolenaar2f6a3462018-08-14 18:16:52 +02001207 call VerifyScreenDump(buf, 'Test_incsearch_substitute_07', {})
1208 call term_sendkeys(buf, "\<Esc>")
1209
1210 " Cursorline highlighting at cursor when no match
1211 call term_sendkeys(buf, ':9,10s/bar')
Bram Moolenaar2f6a3462018-08-14 18:16:52 +02001212 call VerifyScreenDump(buf, 'Test_incsearch_substitute_08', {})
1213 call term_sendkeys(buf, "\<Esc>")
1214
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +02001215 " Only \v handled as empty pattern, does not move cursor
1216 call term_sendkeys(buf, '3G4G')
1217 call term_sendkeys(buf, ":nohlsearch\<CR>")
1218 call term_sendkeys(buf, ':6,7s/\v')
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +02001219 call VerifyScreenDump(buf, 'Test_incsearch_substitute_09', {})
1220 call term_sendkeys(buf, "\<Esc>")
1221
Bram Moolenaarf13daa42018-08-31 22:09:54 +02001222 call term_sendkeys(buf, ":set nocursorline\<CR>")
1223
1224 " All matches are highlighted for 'hlsearch' after the incsearch canceled
1225 call term_sendkeys(buf, "1G*")
1226 call term_sendkeys(buf, ":2,5s/foo")
1227 sleep 100m
1228 call term_sendkeys(buf, "\<Esc>")
1229 call VerifyScreenDump(buf, 'Test_incsearch_substitute_10', {})
1230
Bram Moolenaar65985ac2018-09-16 17:08:04 +02001231 call term_sendkeys(buf, ":split\<CR>")
1232 call term_sendkeys(buf, ":let @/ = 'xyz'\<CR>")
1233 call term_sendkeys(buf, ":%s/.")
1234 call VerifyScreenDump(buf, 'Test_incsearch_substitute_11', {})
1235 call term_sendkeys(buf, "\<BS>")
1236 call VerifyScreenDump(buf, 'Test_incsearch_substitute_12', {})
1237 call term_sendkeys(buf, "\<Esc>")
1238 call VerifyScreenDump(buf, 'Test_incsearch_substitute_13', {})
Bram Moolenaarc6725252019-11-23 21:56:46 +01001239 call term_sendkeys(buf, ":%bwipe!\<CR>")
1240 call term_sendkeys(buf, ":only!\<CR>")
1241
1242 " get :'<,'>s command in history
1243 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
1244 call term_sendkeys(buf, "aasdfasdf\<Esc>")
1245 call term_sendkeys(buf, "V:s/a/b/g\<CR>")
1246 " Using '<,'> does not give E20
1247 call term_sendkeys(buf, ":new\<CR>")
1248 call term_sendkeys(buf, "aasdfasdf\<Esc>")
1249 call term_sendkeys(buf, ":\<Up>\<Up>")
1250 call VerifyScreenDump(buf, 'Test_incsearch_substitute_14', {})
1251 call term_sendkeys(buf, "<Esc>")
Bram Moolenaar65985ac2018-09-16 17:08:04 +02001252
Bram Moolenaar164251f2018-08-12 16:26:58 +02001253 call StopVimInTerminal(buf)
1254 call delete('Xis_subst_script')
1255endfunc
1256
Bram Moolenaarc036e872020-02-21 21:30:52 +01001257func Test_incsearch_highlighting()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001258 CheckOption incsearch
1259 CheckScreendump
Bram Moolenaarc036e872020-02-21 21:30:52 +01001260
1261 call writefile([
1262 \ 'set incsearch hlsearch',
1263 \ 'call setline(1, "hello/there")',
1264 \ ], 'Xis_subst_hl_script')
1265 let buf = RunVimInTerminal('-S Xis_subst_hl_script', {'rows': 4, 'cols': 20})
1266 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1267 " the 'ambiwidth' check.
1268 sleep 300m
1269
1270 " Using a different search delimiter should still highlight matches
1271 " that contain a '/'.
1272 call term_sendkeys(buf, ":%s;ello/the")
1273 call VerifyScreenDump(buf, 'Test_incsearch_substitute_15', {})
1274 call term_sendkeys(buf, "<Esc>")
Bram Moolenaarb68df222020-03-19 15:05:28 +01001275
1276 call StopVimInTerminal(buf)
1277 call delete('Xis_subst_hl_script')
Bram Moolenaarc036e872020-02-21 21:30:52 +01001278endfunc
1279
Bram Moolenaar4a7d2d32019-02-21 16:25:50 +01001280func Test_incsearch_with_change()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001281 CheckFeature timers
1282 CheckOption incsearch
1283 CheckScreendump
Bram Moolenaar4a7d2d32019-02-21 16:25:50 +01001284
1285 call writefile([
1286 \ 'set incsearch hlsearch scrolloff=0',
1287 \ 'call setline(1, ["one", "two ------ X", "three"])',
1288 \ 'call timer_start(200, { _ -> setline(2, "x")})',
1289 \ ], 'Xis_change_script')
1290 let buf = RunVimInTerminal('-S Xis_change_script', {'rows': 9, 'cols': 70})
1291 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1292 " the 'ambiwidth' check.
Bram Moolenaare86ecbd2019-02-21 17:48:59 +01001293 sleep 300m
Bram Moolenaar4a7d2d32019-02-21 16:25:50 +01001294
1295 " Highlight X, it will be deleted by the timer callback.
1296 call term_sendkeys(buf, ':%s/X')
1297 call VerifyScreenDump(buf, 'Test_incsearch_change_01', {})
1298 call term_sendkeys(buf, "\<Esc>")
1299
1300 call StopVimInTerminal(buf)
1301 call delete('Xis_change_script')
1302endfunc
1303
Bram Moolenaar81f56532018-08-18 16:19:42 +02001304" Similar to Test_incsearch_substitute_dump() for :sort
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +02001305func Test_incsearch_sort_dump()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001306 CheckOption incsearch
1307 CheckScreendump
1308
Bram Moolenaar81f56532018-08-18 16:19:42 +02001309 call writefile([
1310 \ 'set incsearch hlsearch scrolloff=0',
1311 \ 'call setline(1, ["another one 2", "that one 3", "the one 1"])',
1312 \ ], 'Xis_sort_script')
1313 let buf = RunVimInTerminal('-S Xis_sort_script', {'rows': 9, 'cols': 70})
1314 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1315 " the 'ambiwidth' check.
1316 sleep 100m
1317
Bram Moolenaar81f56532018-08-18 16:19:42 +02001318 call term_sendkeys(buf, ':sort ni u /on')
Bram Moolenaar81f56532018-08-18 16:19:42 +02001319 call VerifyScreenDump(buf, 'Test_incsearch_sort_01', {})
1320 call term_sendkeys(buf, "\<Esc>")
1321
Bram Moolenaar333015a2020-04-25 15:54:16 +02001322 call term_sendkeys(buf, ':sort! /on')
1323 call VerifyScreenDump(buf, 'Test_incsearch_sort_02', {})
1324 call term_sendkeys(buf, "\<Esc>")
1325
Bram Moolenaar81f56532018-08-18 16:19:42 +02001326 call StopVimInTerminal(buf)
1327 call delete('Xis_sort_script')
1328endfunc
1329
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001330" Similar to Test_incsearch_substitute_dump() for :vimgrep famiry
1331func Test_incsearch_vimgrep_dump()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001332 CheckOption incsearch
1333 CheckScreendump
1334
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001335 call writefile([
1336 \ 'set incsearch hlsearch scrolloff=0',
1337 \ 'call setline(1, ["another one 2", "that one 3", "the one 1"])',
1338 \ ], 'Xis_vimgrep_script')
1339 let buf = RunVimInTerminal('-S Xis_vimgrep_script', {'rows': 9, 'cols': 70})
1340 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1341 " the 'ambiwidth' check.
1342 sleep 100m
1343
1344 " Need to send one key at a time to force a redraw.
1345 call term_sendkeys(buf, ':vimgrep on')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001346 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_01', {})
1347 call term_sendkeys(buf, "\<Esc>")
1348
1349 call term_sendkeys(buf, ':vimg /on/ *.txt')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001350 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_02', {})
1351 call term_sendkeys(buf, "\<Esc>")
1352
1353 call term_sendkeys(buf, ':vimgrepadd "\<on')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001354 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_03', {})
1355 call term_sendkeys(buf, "\<Esc>")
1356
1357 call term_sendkeys(buf, ':lv "tha')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001358 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_04', {})
1359 call term_sendkeys(buf, "\<Esc>")
1360
1361 call term_sendkeys(buf, ':lvimgrepa "the" **/*.txt')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001362 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_05', {})
1363 call term_sendkeys(buf, "\<Esc>")
1364
1365 call StopVimInTerminal(buf)
1366 call delete('Xis_vimgrep_script')
1367endfunc
1368
Bram Moolenaar198cb662018-09-06 21:44:17 +02001369func Test_keep_last_search_pattern()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001370 CheckOption incsearch
1371
Bram Moolenaar198cb662018-09-06 21:44:17 +02001372 new
1373 call setline(1, ['foo', 'foo', 'foo'])
1374 set incsearch
1375 call test_override("char_avail", 1)
1376 let @/ = 'bar'
1377 call feedkeys(":/foo/s//\<Esc>", 'ntx')
1378 call assert_equal('bar', @/)
1379
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001380 " no error message if pattern not found
1381 call feedkeys(":/xyz/s//\<Esc>", 'ntx')
1382 call assert_equal('bar', @/)
1383
Bram Moolenaar198cb662018-09-06 21:44:17 +02001384 bwipe!
1385 call test_override("ALL", 0)
1386 set noincsearch
1387endfunc
1388
Bram Moolenaar99f043a2018-09-09 15:54:14 +02001389func Test_word_under_cursor_after_match()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001390 CheckOption incsearch
1391
Bram Moolenaar99f043a2018-09-09 15:54:14 +02001392 new
1393 call setline(1, 'foo bar')
1394 set incsearch
1395 call test_override("char_avail", 1)
1396 try
1397 call feedkeys("/foo\<C-R>\<C-W>\<CR>", 'ntx')
1398 catch /E486:/
1399 endtry
1400 call assert_equal('foobar', @/)
1401
1402 bwipe!
1403 call test_override("ALL", 0)
1404 set noincsearch
1405endfunc
1406
1407func Test_subst_word_under_cursor()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001408 CheckOption incsearch
1409
Bram Moolenaar99f043a2018-09-09 15:54:14 +02001410 new
1411 call setline(1, ['int SomeLongName;', 'for (xxx = 1; xxx < len; ++xxx)'])
1412 set incsearch
1413 call test_override("char_avail", 1)
1414 call feedkeys("/LongName\<CR>", 'ntx')
1415 call feedkeys(":%s/xxx/\<C-R>\<C-W>/g\<CR>", 'ntx')
1416 call assert_equal('for (SomeLongName = 1; SomeLongName < len; ++SomeLongName)', getline(2))
1417
1418 bwipe!
1419 call test_override("ALL", 0)
1420 set noincsearch
1421endfunc
1422
Bram Moolenaarf45938c2017-11-02 15:59:57 +01001423func Test_search_undefined_behaviour()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001424 CheckFeature terminal
1425
Bram Moolenaarf45938c2017-11-02 15:59:57 +01001426 let h = winheight(0)
1427 if h < 3
1428 return
1429 endif
1430 " did cause an undefined left shift
1431 let g:buf = term_start([GetVimProg(), '--clean', '-e', '-s', '-c', 'call search(getline("."))', 'samples/test000'], {'term_rows': 3})
1432 call assert_equal([''], getline(1, '$'))
1433 call term_sendkeys(g:buf, ":qa!\<cr>")
1434 bwipe!
1435endfunc
Bram Moolenaar2973daa2017-11-02 23:15:40 +01001436
1437func Test_search_undefined_behaviour2()
1438 call search("\%UC0000000")
1439endfunc
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001440
1441" Test for search('multi-byte char', 'bce')
1442func Test_search_multibyte()
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001443 let save_enc = &encoding
1444 set encoding=utf8
1445 enew!
1446 call append('$', 'A')
1447 call cursor(2, 1)
1448 call assert_equal(2, search('A', 'bce', line('.')))
1449 enew!
1450 let &encoding = save_enc
1451endfunc
Bram Moolenaar890dd052017-12-16 19:59:37 +01001452
1453" This was causing E874. Also causes an invalid read?
1454func Test_look_behind()
1455 new
Bram Moolenaar3ba35402019-12-21 22:00:50 +01001456 call setline(1, '0\|\&\n\@<=')
Bram Moolenaar890dd052017-12-16 19:59:37 +01001457 call search(getline("."))
1458 bwipe!
1459endfunc
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01001460
Bram Moolenaar872bee52021-05-24 22:56:15 +02001461func Test_search_visual_area_linewise()
1462 new
1463 call setline(1, ['aa', 'bb', 'cc'])
1464 exe "normal 2GV\<Esc>"
1465 for engine in [1, 2]
1466 exe 'set regexpengine=' .. engine
1467 exe "normal gg/\\%'<\<CR>>"
1468 call assert_equal([0, 2, 1, 0, 1], getcurpos(), 'engine ' .. engine)
1469 exe "normal gg/\\%'>\<CR>"
1470 call assert_equal([0, 2, 2, 0, 2], getcurpos(), 'engine ' .. engine)
1471 endfor
1472
1473 bwipe!
1474 set regexpengine&
1475endfunc
1476
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01001477func Test_search_sentence()
1478 new
1479 " this used to cause a crash
Bram Moolenaar1bd999f2017-12-19 22:25:40 +01001480 /\%'(
1481 /
Bram Moolenaar872bee52021-05-24 22:56:15 +02001482 bwipe
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01001483endfunc
Bram Moolenaar2fb8f682018-12-01 13:14:45 +01001484
1485" Test that there is no crash when there is a last search pattern but no last
1486" substitute pattern.
1487func Test_no_last_substitute_pat()
1488 " Use viminfo to set the last search pattern to a string and make the last
1489 " substitute pattern the most recent used and make it empty (NULL).
1490 call writefile(['~MSle0/bar', '~MSle0~&'], 'Xviminfo')
1491 rviminfo! Xviminfo
1492 call assert_fails('normal n', 'E35:')
1493
1494 call delete('Xviminfo')
1495endfunc
Bram Moolenaar5f5e2032018-12-14 15:47:03 +01001496
1497func Test_search_Ctrl_L_combining()
1498 " Make sure, that Ctrl-L works correctly with combining characters.
1499 " It uses an artificial example of an 'a' with 4 combining chars:
Bram Moolenaar3ba35402019-12-21 22:00:50 +01001500 " 'a' U+0061 Dec:97 LATIN SMALL LETTER A &#x61; /\%u61\Z "\u0061"
Bram Moolenaar5f5e2032018-12-14 15:47:03 +01001501 " ' ̀' U+0300 Dec:768 COMBINING GRAVE ACCENT &#x300; /\%u300\Z "\u0300"
1502 " ' ́' U+0301 Dec:769 COMBINING ACUTE ACCENT &#x301; /\%u301\Z "\u0301"
1503 " ' ̇' U+0307 Dec:775 COMBINING DOT ABOVE &#x307; /\%u307\Z "\u0307"
Bram Moolenaar3ba35402019-12-21 22:00:50 +01001504 " ' ̣' U+0323 Dec:803 COMBINING DOT BELOW &#x323; /\%u323 "\u0323"
Bram Moolenaar5f5e2032018-12-14 15:47:03 +01001505 " Those should also appear on the commandline
Bram Moolenaarb68df222020-03-19 15:05:28 +01001506 CheckOption incsearch
1507
Bram Moolenaar5f5e2032018-12-14 15:47:03 +01001508 call Cmdline3_prep()
1509 1
1510 let bufcontent = ['', 'Miạ̀́̇m']
1511 call append('$', bufcontent)
1512 call feedkeys("/Mi\<c-l>\<c-l>\<cr>", 'tx')
1513 call assert_equal(5, line('.'))
1514 call assert_equal(bufcontent[1], @/)
1515 call Incsearch_cleanup()
1516endfunc
Bram Moolenaar527a2d82019-02-21 22:28:51 +01001517
Bram Moolenaarab350f82019-02-28 06:25:00 +01001518func Test_large_hex_chars1()
Bram Moolenaar527a2d82019-02-21 22:28:51 +01001519 " This used to cause a crash, the character becomes an NFA state.
1520 try
1521 /\%Ufffffc23
1522 catch
1523 call assert_match('E678:', v:exception)
1524 endtry
Bram Moolenaarab350f82019-02-28 06:25:00 +01001525 try
1526 set re=1
1527 /\%Ufffffc23
1528 catch
1529 call assert_match('E678:', v:exception)
1530 endtry
1531 set re&
1532endfunc
1533
1534func Test_large_hex_chars2()
1535 " This used to cause a crash, the character becomes an NFA state.
1536 try
1537 /[\Ufffffc1f]
1538 catch
1539 call assert_match('E486:', v:exception)
1540 endtry
1541 try
1542 set re=1
1543 /[\Ufffffc1f]
1544 catch
1545 call assert_match('E486:', v:exception)
1546 endtry
1547 set re&
Bram Moolenaar527a2d82019-02-21 22:28:51 +01001548endfunc
Bram Moolenaarcd625122019-02-22 17:29:43 +01001549
1550func Test_one_error_msg()
Bram Moolenaar8832a342020-04-11 18:36:38 +02001551 " This was also giving an internal error
Bram Moolenaarcd625122019-02-22 17:29:43 +01001552 call assert_fails('call search(" \\((\\v[[=P=]]){185}+ ")', 'E871:')
1553endfunc
Bram Moolenaar730f48f2019-04-11 13:45:57 +02001554
1555func Test_incsearch_add_char_under_cursor()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001556 CheckOption incsearch
1557
Bram Moolenaar730f48f2019-04-11 13:45:57 +02001558 set incsearch
1559 new
1560 call setline(1, ['find match', 'anything'])
1561 1
1562 call test_override('char_avail', 1)
1563 call feedkeys("fc/m\<C-L>\<C-L>\<C-L>\<C-L>\<C-L>\<CR>", 'tx')
1564 call assert_equal('match', @/)
1565 call test_override('char_avail', 0)
1566
1567 set incsearch&
1568 bwipe!
1569endfunc
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02001570
1571" Test for the search() function with match at the cursor position
1572func Test_search_match_at_curpos()
1573 new
1574 call append(0, ['foobar', '', 'one two', ''])
1575
1576 normal gg
1577
Bram Moolenaar196b4662019-09-06 21:34:30 +02001578 eval 'foobar'->search('c')
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02001579 call assert_equal([1, 1], [line('.'), col('.')])
1580
1581 normal j
1582 call search('^$', 'c')
1583 call assert_equal([2, 1], [line('.'), col('.')])
1584
1585 call search('^$', 'bc')
1586 call assert_equal([2, 1], [line('.'), col('.')])
1587
1588 exe "normal /two\<CR>"
1589 call search('.', 'c')
1590 call assert_equal([3, 5], [line('.'), col('.')])
1591
1592 close!
1593endfunc
Bram Moolenaardb294ad2019-06-06 12:49:29 +02001594
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001595" Test for error cases with the search() function
1596func Test_search_errors()
1597 call assert_fails("call search('pat', [])", 'E730:')
1598 call assert_fails("call search('pat', 'b', {})", 'E728:')
1599 call assert_fails("call search('pat', 'b', 1, [])", 'E745:')
1600 call assert_fails("call search('pat', 'ns')", 'E475:')
1601 call assert_fails("call search('pat', 'mr')", 'E475:')
Bram Moolenaar8832a342020-04-11 18:36:38 +02001602
1603 new
1604 call setline(1, ['foo', 'bar'])
1605 call assert_fails('call feedkeys("/foo/;/bar/;\<CR>", "tx")', 'E386:')
1606 bwipe!
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001607endfunc
1608
Paul Ollis65745772022-06-05 16:55:54 +01001609func Test_search_timeout()
Bram Moolenaar73171ba2022-06-20 12:39:39 +01001610 let g:test_is_flaky = 1
Paul Ollis65745772022-06-05 16:55:54 +01001611 new
Bram Moolenaar616592e2022-06-17 15:17:10 +01001612 " use a complicated pattern that should be slow with the BT engine
Paul Ollis65745772022-06-05 16:55:54 +01001613 let pattern = '\%#=1a*.*X\@<=b*'
Bram Moolenaar616592e2022-06-17 15:17:10 +01001614
1615 " use a timeout of 50 msec
1616 let search_timeout = 0.05
1617
1618 " fill the buffer so that it takes 15 times the timeout to search
Paul Ollis65745772022-06-05 16:55:54 +01001619 let slow_target_timeout = search_timeout * 15.0
1620
Bram Moolenaar616592e2022-06-17 15:17:10 +01001621 " Fill the buffer with more and more text until searching takes more time
1622 " than slow_target_timeout.
Paul Ollis65745772022-06-05 16:55:54 +01001623 for n in range(40, 400, 30)
1624 call setline(1, ['aaa', repeat('abc ', n), 'ccc'])
1625 let start = reltime()
1626 call search(pattern, '', 0)
1627 let elapsed = reltimefloat(reltime(start))
1628 if elapsed > slow_target_timeout
1629 break
1630 endif
1631 endfor
1632 call assert_true(elapsed > slow_target_timeout)
1633
Bram Moolenaar616592e2022-06-17 15:17:10 +01001634 " Check that the timeout kicks in, the time should be less than half of what
1635 " we measured without the timeout. This is permissive, because the timer is
1636 " known to overrun, especially when using valgrind.
Paul Ollis65745772022-06-05 16:55:54 +01001637 let max_time = elapsed / 2.0
1638 let start = reltime()
1639 call search(pattern, '', 0, float2nr(search_timeout * 1000))
1640 let elapsed = reltimefloat(reltime(start))
Bram Moolenaar616592e2022-06-17 15:17:10 +01001641 call assert_inrange(search_timeout * 0.9, max_time, elapsed)
Paul Ollis65745772022-06-05 16:55:54 +01001642
1643 bwipe!
1644endfunc
1645
Bram Moolenaardb294ad2019-06-06 12:49:29 +02001646func Test_search_display_pattern()
1647 new
1648 call setline(1, ['foo', 'bar', 'foobar'])
1649
1650 call cursor(1, 1)
1651 let @/ = 'foo'
Bram Moolenaara4208962019-08-24 20:50:19 +02001652 let pat = @/->escape('()*?'. '\s\+')
Bram Moolenaardb294ad2019-06-06 12:49:29 +02001653 let g:a = execute(':unsilent :norm! n')
1654 call assert_match(pat, g:a)
1655
1656 " right-left
1657 if exists("+rightleft")
1658 set rl
1659 call cursor(1, 1)
1660 let @/ = 'foo'
1661 let pat = 'oof/\s\+'
1662 let g:a = execute(':unsilent :norm! n')
1663 call assert_match(pat, g:a)
1664 set norl
1665 endif
1666endfunc
Bram Moolenaar196b4662019-09-06 21:34:30 +02001667
1668func Test_searchdecl()
1669 let lines =<< trim END
1670 int global;
1671
1672 func()
1673 {
1674 int global;
1675 if (cond) {
1676 int local;
1677 }
1678 int local;
1679 // comment
1680 }
1681 END
1682 new
1683 call setline(1, lines)
1684 10
1685 call assert_equal(0, searchdecl('local', 0, 0))
1686 call assert_equal(7, getcurpos()[1])
1687
1688 10
1689 call assert_equal(0, 'local'->searchdecl(0, 1))
1690 call assert_equal(9, getcurpos()[1])
1691
1692 10
1693 call assert_equal(0, searchdecl('global'))
1694 call assert_equal(5, getcurpos()[1])
1695
1696 10
1697 call assert_equal(0, searchdecl('global', 1))
1698 call assert_equal(1, getcurpos()[1])
1699
1700 bwipe!
1701endfunc
Bram Moolenaar98a336d2020-01-20 20:22:30 +01001702
1703func Test_search_special()
Bram Moolenaarfe4bbac2020-01-20 21:12:20 +01001704 " this was causing illegal memory access and an endless loop
1705 set t_PE=
Bram Moolenaar98a336d2020-01-20 20:22:30 +01001706 exe "norm /\x80PS"
1707endfunc
Bram Moolenaar07ada5f2020-02-05 20:38:22 +01001708
1709" Test for command failures when the last search pattern is not set.
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001710" Need to run this in a new vim instance where last search pattern is not set.
Bram Moolenaar07ada5f2020-02-05 20:38:22 +01001711func Test_search_with_no_last_pat()
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001712 let lines =<< trim [SCRIPT]
1713 call assert_fails("normal i\<C-R>/\e", 'E35:')
1714 call assert_fails("exe '/'", 'E35:')
1715 call assert_fails("exe '?'", 'E35:')
1716 call assert_fails("/", 'E35:')
1717 call assert_fails("?", 'E35:')
1718 call assert_fails("normal n", 'E35:')
1719 call assert_fails("normal N", 'E35:')
1720 call assert_fails("normal gn", 'E35:')
1721 call assert_fails("normal gN", 'E35:')
1722 call assert_fails("normal cgn", 'E35:')
1723 call assert_fails("normal cgN", 'E35:')
1724 let p = []
1725 let p = @/
1726 call assert_equal('', p)
1727 call assert_fails("normal :\<C-R>/", 'E35:')
1728 call assert_fails("//p", 'E35:')
1729 call assert_fails(";//p", 'E35:')
1730 call assert_fails("??p", 'E35:')
1731 call assert_fails(";??p", 'E35:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001732 call assert_fails('g//p', ['E35:', 'E476:'])
1733 call assert_fails('v//p', ['E35:', 'E476:'])
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001734 call writefile(v:errors, 'Xresult')
1735 qall!
1736 [SCRIPT]
1737 call writefile(lines, 'Xscript')
1738
1739 if RunVim([], [], '--clean -S Xscript')
1740 call assert_equal([], readfile('Xresult'))
1741 endif
1742 call delete('Xscript')
1743 call delete('Xresult')
Bram Moolenaar07ada5f2020-02-05 20:38:22 +01001744endfunc
1745
1746" Test for using tilde (~) atom in search. This should use the last used
1747" substitute pattern
1748func Test_search_tilde_pat()
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001749 let lines =<< trim [SCRIPT]
1750 set regexpengine=1
1751 call assert_fails('exe "normal /~\<CR>"', 'E33:')
1752 call assert_fails('exe "normal ?~\<CR>"', 'E33:')
1753 set regexpengine=2
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001754 call assert_fails('exe "normal /~\<CR>"', ['E33:', 'E383:'])
1755 call assert_fails('exe "normal ?~\<CR>"', ['E33:', 'E383:'])
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001756 set regexpengine&
1757 call writefile(v:errors, 'Xresult')
1758 qall!
1759 [SCRIPT]
1760 call writefile(lines, 'Xscript')
1761 if RunVim([], [], '--clean -S Xscript')
1762 call assert_equal([], readfile('Xresult'))
1763 endif
1764 call delete('Xscript')
1765 call delete('Xresult')
Bram Moolenaar07ada5f2020-02-05 20:38:22 +01001766endfunc
1767
Bram Moolenaar406cd902020-02-18 21:54:41 +01001768" Test for searching a pattern that is not present with 'nowrapscan'
1769func Test_search_pat_not_found()
1770 new
1771 set nowrapscan
1772 let @/ = '1abcxyz2'
1773 call assert_fails('normal n', 'E385:')
1774 call assert_fails('normal N', 'E384:')
1775 set wrapscan&
1776 close
1777endfunc
1778
1779" Test for v:searchforward variable
1780func Test_searchforward_var()
1781 new
1782 call setline(1, ['foo', '', 'foo'])
1783 call cursor(2, 1)
1784 let @/ = 'foo'
1785 let v:searchforward = 0
1786 normal N
1787 call assert_equal(3, line('.'))
1788 call cursor(2, 1)
1789 let v:searchforward = 1
1790 normal N
1791 call assert_equal(1, line('.'))
1792 close!
1793endfunc
1794
Bram Moolenaar476a6132020-04-08 19:48:56 +02001795" Test for invalid regular expressions
1796func Test_invalid_regexp()
1797 set regexpengine=1
1798 call assert_fails("call search(repeat('\\(.\\)', 10))", 'E51:')
Bram Moolenaar4d23c522020-04-09 18:42:11 +02001799 call assert_fails("call search('\\%(')", 'E53:')
1800 call assert_fails("call search('\\(')", 'E54:')
1801 call assert_fails("call search('\\)')", 'E55:')
Bram Moolenaar476a6132020-04-08 19:48:56 +02001802 call assert_fails("call search('x\\@#')", 'E59:')
Bram Moolenaar4d23c522020-04-09 18:42:11 +02001803 call assert_fails('call search(''\v%(%(%(%(%(%(%(%(%(%(%(a){1}){1}){1}){1}){1}){1}){1}){1}){1}){1}){1}'')', 'E60:')
1804 call assert_fails("call search('a\\+*')", 'E61:')
Bram Moolenaar476a6132020-04-08 19:48:56 +02001805 call assert_fails("call search('\\_m')", 'E63:')
1806 call assert_fails("call search('\\+')", 'E64:')
1807 call assert_fails("call search('\\1')", 'E65:')
1808 call assert_fails("call search('\\z\\(\\)')", 'E66:')
1809 call assert_fails("call search('\\z2')", 'E67:')
1810 call assert_fails("call search('\\zx')", 'E68:')
1811 call assert_fails("call search('\\%[ab')", 'E69:')
1812 call assert_fails("call search('\\%[]')", 'E70:')
1813 call assert_fails("call search('\\%a')", 'E71:')
1814 call assert_fails("call search('ab\\%[\\(cd\\)]')", 'E369:')
1815 call assert_fails("call search('ab\\%[\\%(cd\\)]')", 'E369:')
1816 set regexpengine=2
1817 call assert_fails("call search('\\_')", 'E865:')
1818 call assert_fails("call search('\\+')", 'E866:')
1819 call assert_fails("call search('\\zx')", 'E867:')
1820 call assert_fails("call search('\\%a')", 'E867:')
1821 call assert_fails("call search('x\\@#')", 'E869:')
1822 call assert_fails("call search(repeat('\\(.\\)', 10))", 'E872:')
1823 call assert_fails("call search('\\_m')", 'E877:')
1824 call assert_fails("call search('\\%(')", 'E53:')
1825 call assert_fails("call search('\\(')", 'E54:')
1826 call assert_fails("call search('\\)')", 'E55:')
1827 call assert_fails("call search('\\z\\(\\)')", 'E66:')
Dominique Pelle8bfa0eb2022-01-02 16:16:33 +00001828 call assert_fails("call search('\\z2')", 'E67:')
1829 call assert_fails("call search('\\zx')", 'E867:')
Bram Moolenaar476a6132020-04-08 19:48:56 +02001830 call assert_fails("call search('\\%[ab')", 'E69:')
Bram Moolenaar4d23c522020-04-09 18:42:11 +02001831 call assert_fails("call search('\\%[]')", 'E70:')
Bram Moolenaar476a6132020-04-08 19:48:56 +02001832 call assert_fails("call search('\\%9999999999999999999999999999v')", 'E951:')
1833 set regexpengine&
1834 call assert_fails("call search('\\%#=3ab')", 'E864:')
1835endfunc
1836
Bram Moolenaar004a6782020-04-11 17:09:31 +02001837" Test for searching a very complex pattern in a string. Should switch the
1838" regexp engine from NFA to the old engine.
1839func Test_regexp_switch_engine()
1840 let l = readfile('samples/re.freeze.txt')
1841 let v = substitute(l[4], '..\@<!', '', '')
1842 call assert_equal(l[4], v)
1843endfunc
1844
1845" Test for the \%V atom to search within visually selected text
1846func Test_search_in_visual_area()
1847 new
1848 call setline(1, ['foo bar1', 'foo bar2', 'foo bar3', 'foo bar4'])
1849 exe "normal 2GVjo/\\%Vbar\<CR>\<Esc>"
1850 call assert_equal([2, 5], [line('.'), col('.')])
1851 exe "normal 2GVj$?\\%Vbar\<CR>\<Esc>"
1852 call assert_equal([3, 5], [line('.'), col('.')])
1853 close!
1854endfunc
1855
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001856" Test for searching with 'smartcase' and 'ignorecase'
1857func Test_search_smartcase()
1858 new
1859 call setline(1, ['', 'Hello'])
1860 set noignorecase nosmartcase
1861 call assert_fails('exe "normal /\\a\\_.\\(.*\\)O\<CR>"', 'E486:')
1862
1863 set ignorecase nosmartcase
1864 exe "normal /\\a\\_.\\(.*\\)O\<CR>"
1865 call assert_equal([2, 1], [line('.'), col('.')])
1866
1867 call cursor(1, 1)
1868 set ignorecase smartcase
1869 call assert_fails('exe "normal /\\a\\_.\\(.*\\)O\<CR>"', 'E486:')
1870
1871 exe "normal /\\a\\_.\\(.*\\)o\<CR>"
1872 call assert_equal([2, 1], [line('.'), col('.')])
1873
Bram Moolenaar224a5f12020-04-28 20:29:07 +02001874 " Test for using special atoms with 'smartcase'
1875 call setline(1, ['', ' Hello\ '])
1876 call cursor(1, 1)
1877 call feedkeys('/\_.\%(\uello\)\' .. "\<CR>", 'xt')
1878 call assert_equal([2, 4], [line('.'), col('.')])
1879
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001880 set ignorecase& smartcase&
1881 close!
Bram Moolenaard66cdcd2020-07-26 13:27:16 +02001882endfun
1883
1884" Test 'smartcase' with utf-8.
1885func Test_search_smartcase_utf8()
1886 new
1887 let save_enc = &encoding
1888 set encoding=utf8 ignorecase smartcase
1889
1890 call setline(1, 'Café cafÉ')
1891 1s/café/x/g
1892 call assert_equal('x x', getline(1))
1893
1894 call setline(1, 'Café cafÉ')
1895 1s/cafÉ/x/g
1896 call assert_equal('Café x', getline(1))
1897
1898 set ignorecase& smartcase&
1899 let &encoding = save_enc
1900 close!
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001901endfunc
1902
1903" Test searching past the end of a file
1904func Test_search_past_eof()
1905 new
1906 call setline(1, ['Line'])
1907 exe "normal /\\n\\zs\<CR>"
1908 call assert_equal([1, 4], [line('.'), col('.')])
1909 close!
1910endfunc
1911
Bram Moolenaar224a5f12020-04-28 20:29:07 +02001912" Test for various search offsets
1913func Test_search_offset()
1914 " With /e, for a match in the first column of a line, the cursor should be
1915 " placed at the end of the previous line.
1916 new
1917 call setline(1, ['one two', 'three four'])
1918 call search('two\_.', 'e')
1919 call assert_equal([1, 7], [line('.'), col('.')])
1920
1921 " with cursor at the beginning of the file, use /s+1
1922 call cursor(1, 1)
1923 exe "normal /two/s+1\<CR>"
1924 call assert_equal([1, 6], [line('.'), col('.')])
1925
1926 " with cursor at the end of the file, use /e-1
1927 call cursor(2, 10)
1928 exe "normal ?three?e-1\<CR>"
1929 call assert_equal([2, 4], [line('.'), col('.')])
1930
1931 " line offset - after the last line
1932 call cursor(1, 1)
1933 exe "normal /three/+1\<CR>"
1934 call assert_equal([2, 1], [line('.'), col('.')])
1935
1936 " line offset - before the first line
1937 call cursor(2, 1)
1938 exe "normal ?one?-1\<CR>"
1939 call assert_equal([1, 1], [line('.'), col('.')])
1940
1941 " character offset - before the first character in the file
1942 call cursor(2, 1)
1943 exe "normal ?one?s-1\<CR>"
1944 call assert_equal([1, 1], [line('.'), col('.')])
1945 call cursor(2, 1)
1946 exe "normal ?one?e-3\<CR>"
1947 call assert_equal([1, 1], [line('.'), col('.')])
1948
1949 " character offset - after the last character in the file
1950 call cursor(1, 1)
1951 exe "normal /four/s+4\<CR>"
1952 call assert_equal([2, 10], [line('.'), col('.')])
1953 call cursor(1, 1)
1954 exe "normal /four/e+1\<CR>"
1955 call assert_equal([2, 10], [line('.'), col('.')])
1956
1957 close!
1958endfunc
1959
1960" Test for searching for matching parenthesis using %
1961func Test_search_match_paren()
1962 new
1963 call setline(1, "abc(def')'ghi'('jk'\\t'lm)no")
1964 " searching for a matching parenthesis should skip single quoted characters
1965 call cursor(1, 4)
1966 normal %
1967 call assert_equal([1, 25], [line('.'), col('.')])
1968 normal %
1969 call assert_equal([1, 4], [line('.'), col('.')])
1970 call cursor(1, 5)
1971 normal ])
1972 call assert_equal([1, 25], [line('.'), col('.')])
1973 call cursor(1, 24)
1974 normal [(
1975 call assert_equal([1, 4], [line('.'), col('.')])
1976
1977 " matching parenthesis in 'virtualedit' mode with cursor after the eol
1978 call setline(1, 'abc(defgh)')
1979 set virtualedit=all
1980 normal 20|%
1981 call assert_equal(4, col('.'))
1982 set virtualedit&
1983 close!
1984endfunc
1985
1986" Test for searching a pattern and stopping before a specified line
1987func Test_search_stopline()
1988 new
1989 call setline(1, ['', '', '', 'vim'])
1990 call assert_equal(0, search('vim', 'n', 3))
1991 call assert_equal(4, search('vim', 'n', 4))
1992 call setline(1, ['vim', '', '', ''])
1993 call cursor(4, 1)
1994 call assert_equal(0, search('vim', 'bn', 2))
1995 call assert_equal(1, search('vim', 'bn', 1))
1996 close!
1997endfunc
1998
Bram Moolenaar6bed0db2020-11-25 17:41:20 +01001999func Test_incsearch_highlighting_newline()
Bram Moolenaar448465e2020-11-25 13:49:27 +01002000 CheckRunVimInTerminal
2001 CheckOption incsearch
2002 CheckScreendump
2003 new
2004 call test_override("char_avail", 1)
2005
2006 let commands =<< trim [CODE]
2007 set incsearch nohls
2008 call setline(1, ['test', 'xxx'])
2009 [CODE]
2010 call writefile(commands, 'Xincsearch_nl')
2011 let buf = RunVimInTerminal('-S Xincsearch_nl', {'rows': 5, 'cols': 10})
Bram Moolenaar448465e2020-11-25 13:49:27 +01002012 call term_sendkeys(buf, '/test')
Bram Moolenaar448465e2020-11-25 13:49:27 +01002013 call VerifyScreenDump(buf, 'Test_incsearch_newline1', {})
Bram Moolenaar6bed0db2020-11-25 17:41:20 +01002014 " Need to send one key at a time to force a redraw
Bram Moolenaar448465e2020-11-25 13:49:27 +01002015 call term_sendkeys(buf, '\n')
Bram Moolenaar448465e2020-11-25 13:49:27 +01002016 call VerifyScreenDump(buf, 'Test_incsearch_newline2', {})
2017 call term_sendkeys(buf, 'x')
Bram Moolenaar448465e2020-11-25 13:49:27 +01002018 call VerifyScreenDump(buf, 'Test_incsearch_newline3', {})
2019 call term_sendkeys(buf, 'x')
2020 call VerifyScreenDump(buf, 'Test_incsearch_newline4', {})
2021 call term_sendkeys(buf, "\<CR>")
Bram Moolenaar448465e2020-11-25 13:49:27 +01002022 call VerifyScreenDump(buf, 'Test_incsearch_newline5', {})
2023 call StopVimInTerminal(buf)
2024
2025 " clean up
2026 call delete('Xincsearch_nl')
2027 call test_override("char_avail", 0)
2028 bw
2029endfunc
2030
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01002031func Test_incsearch_substitute_dump2()
2032 CheckOption incsearch
2033 CheckScreendump
2034
2035 call writefile([
2036 \ 'set incsearch hlsearch scrolloff=0',
2037 \ 'for n in range(1, 4)',
2038 \ ' call setline(n, "foo " . n)',
2039 \ 'endfor',
2040 \ 'call setline(5, "abc|def")',
2041 \ '3',
2042 \ ], 'Xis_subst_script2')
2043 let buf = RunVimInTerminal('-S Xis_subst_script2', {'rows': 9, 'cols': 70})
2044
2045 call term_sendkeys(buf, ':%s/\vabc|')
2046 sleep 100m
2047 call VerifyScreenDump(buf, 'Test_incsearch_sub_01', {})
2048 call term_sendkeys(buf, "\<Esc>")
2049
2050 " The following should not be highlighted
2051 call term_sendkeys(buf, ':1,5s/\v|')
2052 sleep 100m
2053 call VerifyScreenDump(buf, 'Test_incsearch_sub_02', {})
2054
2055
2056 call StopVimInTerminal(buf)
2057 call delete('Xis_subst_script2')
2058endfunc
2059
Christian Brabandt78ba9332021-08-01 12:44:37 +02002060func Test_pattern_is_uppercase_smartcase()
2061 new
2062 let input=['abc', 'ABC', 'Abc', 'abC']
2063 call setline(1, input)
2064 call cursor(1,1)
2065 " default, matches firstline
2066 %s/abc//g
2067 call assert_equal(['', 'ABC', 'Abc', 'abC'],
2068 \ getline(1, '$'))
2069
2070 set smartcase ignorecase
2071 sil %d
2072 call setline(1, input)
2073 call cursor(1,1)
2074 " with smartcase and incsearch set, matches everything
2075 %s/abc//g
2076 call assert_equal(['', '', '', ''], getline(1, '$'))
2077
2078 sil %d
2079 call setline(1, input)
2080 call cursor(1,1)
2081 " with smartcase and incsearch set and found an uppercase letter,
2082 " match only that.
2083 %s/abC//g
2084 call assert_equal(['abc', 'ABC', 'Abc', ''],
2085 \ getline(1, '$'))
2086
2087 sil %d
2088 call setline(1, input)
2089 call cursor(1,1)
2090 exe "norm! vG$\<esc>"
2091 " \%V should not be detected as uppercase letter
2092 %s/\%Vabc//g
2093 call assert_equal(['', '', '', ''], getline(1, '$'))
2094
2095 call setline(1, input)
2096 call cursor(1,1)
2097 exe "norm! vG$\<esc>"
2098 " \v%V should not be detected as uppercase letter
2099 %s/\v%Vabc//g
2100 call assert_equal(['', '', '', ''], getline(1, '$'))
2101
2102 call setline(1, input)
2103 call cursor(1,1)
2104 exe "norm! vG$\<esc>"
2105 " \v%VabC should be detected as uppercase letter
2106 %s/\v%VabC//g
2107 call assert_equal(['abc', 'ABC', 'Abc', ''],
2108 \ getline(1, '$'))
2109
Christian Brabandtbc67e5a2021-08-05 15:24:59 +02002110 call setline(1, input)
2111 call cursor(1,1)
2112 " \Vabc should match everything
2113 %s/\Vabc//g
2114 call assert_equal(['', '', '', ''], getline(1, '$'))
2115
2116 call setline(1, input + ['_abc'])
2117 " _ matches normally
2118 %s/\v_.*//g
2119 call assert_equal(['abc', 'ABC', 'Abc', 'abC', ''], getline(1, '$'))
2120
Christian Brabandt78ba9332021-08-01 12:44:37 +02002121 set smartcase& ignorecase&
2122 bw!
2123endfunc
2124
Bram Moolenaard8d957d2021-10-04 19:47:35 +01002125func Test_no_last_search_pattern()
2126 CheckOption incsearch
2127
2128 let @/ = ""
2129 set incsearch
Bram Moolenaar9af9fd62021-10-04 20:09:19 +01002130 " these were causing a crash
2131 call feedkeys("//\<C-G>", 'xt')
2132 call feedkeys("//\<C-T>", 'xt')
2133 call feedkeys("??\<C-G>", 'xt')
2134 call feedkeys("??\<C-T>", 'xt')
Bram Moolenaard8d957d2021-10-04 19:47:35 +01002135endfunc
2136
Bram Moolenaar35a319b2021-10-09 13:58:55 +01002137func Test_search_with_invalid_range()
2138 new
2139 let lines =<< trim END
2140 /\%.v
2141 5/
2142 c
2143 END
2144 call writefile(lines, 'Xrangesearch')
2145 source Xrangesearch
2146
2147 bwipe!
2148 call delete('Xrangesearch')
2149endfunc
2150
Bram Moolenaard8d957d2021-10-04 19:47:35 +01002151
Bram Moolenaar07ada5f2020-02-05 20:38:22 +01002152" vim: shiftwidth=2 sts=2 expandtab