blob: 5422b33f6a704740ac57a9e095729c0b186e60aa [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
Bram Moolenaar12a49402022-06-21 22:35:40 +0100356 delfunc Waitabit
Bram Moolenaar5ea38d12022-06-16 21:20:48 +0100357 bwipe!
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100358endfunc
359
Bram Moolenaare5303952022-06-19 17:05:47 +0100360func SearchpairSkip()
361 let id = synID(line('.'), col('.'), 0)
362 let attr = synIDattr(id, 'name')
363 return attr !~ 'comment'
364endfunc
365
366func Test_searchpair_timeout_with_skip()
367 let g:test_is_flaky = 1
368
369 edit ../evalfunc.c
370 if has('win32')
371 " Windows timeouts are rather coarse grained, about 16ms.
372 let ms = 20
373 let min_time = 0.016
374 let max_time = min_time * 10.0
375 else
376 let ms = 1
377 let min_time = 0.001
378 let max_time = min_time * 10.0
Bram Moolenaare366ed42022-06-19 20:13:56 +0100379 if RunningWithValgrind()
Bram Moolenaare5303952022-06-19 17:05:47 +0100380 let max_time += 0.04 " this can be slow with valgrind
381 endif
382 endif
383 let start = reltime()
384 let found = searchpair('(', '', ')', 'crnm', 'SearchpairSkip()', 0, ms)
385 let elapsed = reltimefloat(reltime(start))
386 call assert_inrange(min_time, max_time, elapsed)
387
388 bwipe!
389endfunc
390
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100391func Test_searchpairpos()
392 new
393 call setline(1, ['other code', 'here [', ' [', ' " cursor here', ' ]]'])
394
395 4
396 call assert_equal([3, 2], searchpairpos('\[', '', ']', 'bW'))
397 call assert_equal([0, 3, 2, 0], getpos('.'))
398 4
399 call assert_equal([2, 6], searchpairpos('\[', '', ']', 'bWr'))
400 call assert_equal([0, 2, 6, 0], getpos('.'))
401 4|norm ^
402 call assert_equal([5, 2], searchpairpos('\[', '', ']', 'Wn'))
403 call assert_equal([0, 4, 2, 0], getpos('.'))
404 4
405 call assert_equal([2, 6], searchpairpos('\[', '', ']', 'bW',
406 \ 'getline(".") =~ "^\\s*\["'))
407 call assert_equal([0, 2, 6, 0], getpos('.'))
408 4
409 call assert_equal([2, 6], searchpairpos('\[', '', ']', 'bWr'))
410 call assert_equal([0, 2, 6, 0], getpos('.'))
411 set nomagic
412 4
413 call assert_equal([3, 2], searchpairpos('\[', '', ']', 'bW'))
414 call assert_equal([0, 3, 2, 0], getpos('.'))
415 set magic
416 4|norm ^
417 call assert_equal([0, 0], searchpairpos('{', '', '}', 'bW'))
418 call assert_equal([0, 4, 2, 0], getpos('.'))
419
420 %d
421 call setline(1, ['if 1', ' if 2', ' else', ' endif 2', 'endif 1'])
422 /\<if 1
423 call assert_equal([5, 1], searchpairpos('\<if\>', '\<else\>', '\<endif\>', 'W'))
424 call assert_equal([0, 5, 1, 0], getpos('.'))
425 /\<if 2
426 call assert_equal([3, 3], searchpairpos('\<if\>', '\<else\>', '\<endif\>', 'W'))
427 call assert_equal([0, 3, 3, 0], getpos('.'))
428
Bram Moolenaar6e450a52017-01-06 20:03:58 +0100429 q!
430endfunc
431
Bram Moolenaar3dddb092018-06-24 19:01:59 +0200432func Test_searchpair_errors()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200433 call assert_fails("call searchpair([0], 'middle', 'end', 'bW', 'skip', 99, 100)", 'E730: Using a List as a String')
434 call assert_fails("call searchpair('start', {-> 0}, 'end', 'bW', 'skip', 99, 100)", 'E729: Using a Funcref as a String')
435 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 +0200436 call assert_fails("call searchpair('start', 'middle', 'end', 'flags', 'skip', 99, 100)", 'E475: Invalid argument: flags')
Bram Moolenaar3dddb092018-06-24 19:01:59 +0200437 call assert_fails("call searchpair('start', 'middle', 'end', 'bW', 'func', -99, 100)", 'E475: Invalid argument: -99')
438 call assert_fails("call searchpair('start', 'middle', 'end', 'bW', 'func', 99, -100)", 'E475: Invalid argument: -100')
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100439 call assert_fails("call searchpair('start', 'middle', 'end', 'e')", 'E475: Invalid argument: e')
440 call assert_fails("call searchpair('start', 'middle', 'end', 'sn')", 'E475: Invalid argument: sn')
441endfunc
442
443func Test_searchpairpos_errors()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200444 call assert_fails("call searchpairpos([0], 'middle', 'end', 'bW', 'skip', 99, 100)", 'E730: Using a List as a String')
445 call assert_fails("call searchpairpos('start', {-> 0}, 'end', 'bW', 'skip', 99, 100)", 'E729: Using a Funcref as a String')
446 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 +0100447 call assert_fails("call searchpairpos('start', 'middle', 'end', 'flags', 'skip', 99, 100)", 'E475: Invalid argument: flags')
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100448 call assert_fails("call searchpairpos('start', 'middle', 'end', 'bW', 'func', -99, 100)", 'E475: Invalid argument: -99')
449 call assert_fails("call searchpairpos('start', 'middle', 'end', 'bW', 'func', 99, -100)", 'E475: Invalid argument: -100')
450 call assert_fails("call searchpairpos('start', 'middle', 'end', 'e')", 'E475: Invalid argument: e')
451 call assert_fails("call searchpairpos('start', 'middle', 'end', 'sn')", 'E475: Invalid argument: sn')
Bram Moolenaar3dddb092018-06-24 19:01:59 +0200452endfunc
453
Bram Moolenaar48570482017-10-30 21:48:41 +0100454func Test_searchpair_skip()
455 func Zero()
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100456 return 0
Bram Moolenaar48570482017-10-30 21:48:41 +0100457 endfunc
458 func Partial(x)
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100459 return a:x
Bram Moolenaar48570482017-10-30 21:48:41 +0100460 endfunc
461 new
462 call setline(1, ['{', 'foo', 'foo', 'foo', '}'])
463 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', ''))
464 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', '0'))
465 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', {-> 0}))
466 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', function('Zero')))
467 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', function('Partial', [0])))
Bram Moolenaar48570482017-10-30 21:48:41 +0100468 bw!
469endfunc
470
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200471func Test_searchpair_leak()
472 new
473 call setline(1, 'if one else another endif')
474
475 " The error in the skip expression caused memory to leak.
476 call assert_fails("call searchpair('\\<if\\>', '\\<else\\>', '\\<endif\\>', '', '\"foo\" 2')", 'E15:')
477
478 bwipe!
479endfunc
480
Bram Moolenaar66727e12017-03-01 22:17:05 +0100481func Test_searchc()
482 " These commands used to cause memory overflow in searchc().
483 new
484 norm ixx
485 exe "norm 0t\u93cf"
486 bw!
487endfunc
Bram Moolenaara693d052017-06-29 22:23:06 +0200488
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200489func Cmdline3_prep()
490 " need to disable char_avail,
491 " so that expansion of commandline works
492 call test_override("char_avail", 1)
493 new
494 call setline(1, [' 1', ' 2 the~e', ' 3 the theother'])
495 set incsearch
496endfunc
497
Bram Moolenaar976b8472018-08-12 15:49:47 +0200498func Incsearch_cleanup()
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200499 set noincsearch
500 call test_override("char_avail", 0)
501 bw!
502endfunc
503
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200504func Test_search_cmdline3()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100505 CheckOption incsearch
506
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200507 call Cmdline3_prep()
508 1
509 " first match
510 call feedkeys("/the\<c-l>\<cr>", 'tx')
511 call assert_equal(' 2 the~e', getline('.'))
512
Bram Moolenaar976b8472018-08-12 15:49:47 +0200513 call Incsearch_cleanup()
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200514endfunc
515
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200516func Test_search_cmdline3s()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100517 CheckOption incsearch
518
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200519 call Cmdline3_prep()
520 1
521 call feedkeys(":%s/the\<c-l>/xxx\<cr>", 'tx')
522 call assert_equal(' 2 xxxe', getline('.'))
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200523 undo
524 call feedkeys(":%subs/the\<c-l>/xxx\<cr>", 'tx')
525 call assert_equal(' 2 xxxe', getline('.'))
526 undo
527 call feedkeys(":%substitute/the\<c-l>/xxx\<cr>", 'tx')
528 call assert_equal(' 2 xxxe', getline('.'))
Bram Moolenaar167ae422018-08-14 21:32:21 +0200529 undo
530 call feedkeys(":%smagic/the.e/xxx\<cr>", 'tx')
531 call assert_equal(' 2 xxx', getline('.'))
532 undo
Bram Moolenaare2e40752020-09-04 21:18:46 +0200533 call assert_fails(":%snomagic/the.e/xxx\<cr>", 'E486:')
Bram Moolenaar167ae422018-08-14 21:32:21 +0200534 "
535 call feedkeys(":%snomagic/the\\.e/xxx\<cr>", 'tx')
536 call assert_equal(' 2 xxx', getline('.'))
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200537
Bram Moolenaar976b8472018-08-12 15:49:47 +0200538 call Incsearch_cleanup()
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200539endfunc
540
541func Test_search_cmdline3g()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100542 CheckOption incsearch
543
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200544 call Cmdline3_prep()
545 1
546 call feedkeys(":g/the\<c-l>/d\<cr>", 'tx')
547 call assert_equal(' 3 the theother', getline(2))
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200548 undo
549 call feedkeys(":global/the\<c-l>/d\<cr>", 'tx')
550 call assert_equal(' 3 the theother', getline(2))
Bram Moolenaardef7b1d2018-08-13 22:54:35 +0200551 undo
552 call feedkeys(":g!/the\<c-l>/d\<cr>", 'tx')
553 call assert_equal(1, line('$'))
554 call assert_equal(' 2 the~e', getline(1))
555 undo
556 call feedkeys(":global!/the\<c-l>/d\<cr>", 'tx')
557 call assert_equal(1, line('$'))
558 call assert_equal(' 2 the~e', getline(1))
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200559
Bram Moolenaar976b8472018-08-12 15:49:47 +0200560 call Incsearch_cleanup()
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200561endfunc
562
563func Test_search_cmdline3v()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100564 CheckOption incsearch
565
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200566 call Cmdline3_prep()
567 1
568 call feedkeys(":v/the\<c-l>/d\<cr>", 'tx')
569 call assert_equal(1, line('$'))
570 call assert_equal(' 2 the~e', getline(1))
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200571 undo
572 call feedkeys(":vglobal/the\<c-l>/d\<cr>", 'tx')
573 call assert_equal(1, line('$'))
574 call assert_equal(' 2 the~e', getline(1))
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200575
Bram Moolenaar976b8472018-08-12 15:49:47 +0200576 call Incsearch_cleanup()
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200577endfunc
578
Bram Moolenaarda5116d2017-07-01 23:11:17 +0200579func Test_search_cmdline4()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100580 CheckOption incsearch
581
Bram Moolenaarda5116d2017-07-01 23:11:17 +0200582 " need to disable char_avail,
583 " so that expansion of commandline works
584 call test_override("char_avail", 1)
585 new
586 call setline(1, [' 1 the first', ' 2 the second', ' 3 the third'])
587 set incsearch
588 $
589 call feedkeys("?the\<c-g>\<cr>", 'tx')
590 call assert_equal(' 3 the third', getline('.'))
591 $
592 call feedkeys("?the\<c-g>\<c-g>\<cr>", 'tx')
593 call assert_equal(' 1 the first', getline('.'))
594 $
595 call feedkeys("?the\<c-g>\<c-g>\<c-g>\<cr>", 'tx')
596 call assert_equal(' 2 the second', getline('.'))
597 $
598 call feedkeys("?the\<c-t>\<cr>", 'tx')
599 call assert_equal(' 1 the first', getline('.'))
600 $
601 call feedkeys("?the\<c-t>\<c-t>\<cr>", 'tx')
602 call assert_equal(' 3 the third', getline('.'))
603 $
604 call feedkeys("?the\<c-t>\<c-t>\<c-t>\<cr>", 'tx')
605 call assert_equal(' 2 the second', getline('.'))
606 " clean up
607 set noincsearch
608 call test_override("char_avail", 0)
609 bw!
610endfunc
Bram Moolenaardb510072017-09-28 21:52:17 +0200611
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200612func Test_search_cmdline5()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100613 CheckOption incsearch
614
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200615 " Do not call test_override("char_avail", 1) so that <C-g> and <C-t> work
616 " regardless char_avail.
617 new
Bram Moolenaar69a5b862019-07-16 21:38:51 +0200618 call setline(1, [' 1 the first', ' 2 the second', ' 3 the third', ''])
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200619 set incsearch
620 1
621 call feedkeys("/the\<c-g>\<c-g>\<cr>", 'tx')
622 call assert_equal(' 3 the third', getline('.'))
623 $
624 call feedkeys("?the\<c-t>\<c-t>\<c-t>\<cr>", 'tx')
Bram Moolenaar69a5b862019-07-16 21:38:51 +0200625 call assert_equal(' 1 the first', getline('.'))
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200626 " clean up
627 set noincsearch
628 bw!
629endfunc
630
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100631func Test_search_cmdline6()
632 " Test that consecutive matches
633 " are caught by <c-g>/<c-t>
Bram Moolenaarb68df222020-03-19 15:05:28 +0100634 CheckOption incsearch
635
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100636 " need to disable char_avail,
637 " so that expansion of commandline works
638 call test_override("char_avail", 1)
639 new
640 call setline(1, [' bbvimb', ''])
641 set incsearch
642 " first match
643 norm! gg0
644 call feedkeys("/b\<cr>", 'tx')
645 call assert_equal([0,1,2,0], getpos('.'))
646 " second match
647 norm! gg0
648 call feedkeys("/b\<c-g>\<cr>", 'tx')
649 call assert_equal([0,1,3,0], getpos('.'))
650 " third match
651 norm! gg0
652 call feedkeys("/b\<c-g>\<c-g>\<cr>", 'tx')
653 call assert_equal([0,1,7,0], getpos('.'))
654 " first match again
655 norm! gg0
656 call feedkeys("/b\<c-g>\<c-g>\<c-g>\<cr>", 'tx')
657 call assert_equal([0,1,2,0], getpos('.'))
658 set nowrapscan
659 " last match
660 norm! gg0
661 call feedkeys("/b\<c-g>\<c-g>\<c-g>\<cr>", 'tx')
662 call assert_equal([0,1,7,0], getpos('.'))
663 " clean up
664 set wrapscan&vim
665 set noincsearch
666 call test_override("char_avail", 0)
667 bw!
668endfunc
669
670func Test_search_cmdline7()
Bram Moolenaard66cdcd2020-07-26 13:27:16 +0200671 " Test that pressing <c-g> in an empty command line
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100672 " does not move the cursor
Bram Moolenaarb68df222020-03-19 15:05:28 +0100673 CheckOption incsearch
674
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100675 " need to disable char_avail,
676 " so that expansion of commandline works
677 call test_override("char_avail", 1)
678 new
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200679 let @/ = 'b'
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100680 call setline(1, [' bbvimb', ''])
681 set incsearch
682 " first match
683 norm! gg0
684 " moves to next match of previous search pattern, just like /<cr>
685 call feedkeys("/\<c-g>\<cr>", 'tx')
686 call assert_equal([0,1,2,0], getpos('.'))
687 " moves to next match of previous search pattern, just like /<cr>
688 call feedkeys("/\<cr>", 'tx')
689 call assert_equal([0,1,3,0], getpos('.'))
690 " moves to next match of previous search pattern, just like /<cr>
691 call feedkeys("/\<c-t>\<cr>", 'tx')
692 call assert_equal([0,1,7,0], getpos('.'))
Bram Moolenaard0480092017-11-16 22:20:39 +0100693
694 " using an offset uses the last search pattern
695 call cursor(1, 1)
696 call setline(1, ['1 bbvimb', ' 2 bbvimb'])
697 let @/ = 'b'
698 call feedkeys("//e\<c-g>\<cr>", 'tx')
699 call assert_equal('1 bbvimb', getline('.'))
700 call assert_equal(4, col('.'))
701
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100702 set noincsearch
703 call test_override("char_avail", 0)
704 bw!
705endfunc
706
707func Test_search_cmdline8()
708 " Highlighting is cleared in all windows
709 " since hls applies to all windows
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200710 CheckOption incsearch
711 CheckFeature terminal
712 CheckNotGui
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100713 if has("win32")
714 throw "Skipped: Bug with sending <ESC> to terminal window not fixed yet"
715 endif
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200716
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100717 let h = winheight(0)
718 if h < 3
719 return
720 endif
721 " Prepare buffer text
722 let lines = ['abb vim vim vi', 'vimvivim']
723 call writefile(lines, 'Xsearch.txt')
Bram Moolenaar13deab82017-11-04 18:48:43 +0100724 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', 'Xsearch.txt'], {'term_rows': 3})
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100725
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200726 call WaitForAssert({-> assert_equal(lines, [term_getline(buf, 1), term_getline(buf, 2)])})
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100727
Bram Moolenaar13deab82017-11-04 18:48:43 +0100728 call term_sendkeys(buf, ":set incsearch hlsearch\<cr>")
729 call term_sendkeys(buf, ":14vsp\<cr>")
730 call term_sendkeys(buf, "/vim\<cr>")
731 call term_sendkeys(buf, "/b\<esc>")
732 call term_sendkeys(buf, "gg0")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200733 call TermWait(buf, 250)
Bram Moolenaar13deab82017-11-04 18:48:43 +0100734 let screen_line = term_scrape(buf, 1)
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100735 let [a0,a1,a2,a3] = [screen_line[3].attr, screen_line[4].attr,
736 \ screen_line[18].attr, screen_line[19].attr]
737 call assert_notequal(a0, a1)
738 call assert_notequal(a0, a3)
739 call assert_notequal(a1, a2)
740 call assert_equal(a0, a2)
741 call assert_equal(a1, a3)
742 " clean up
743 call delete('Xsearch.txt')
744
745 bwipe!
746endfunc
747
Bram Moolenaardb510072017-09-28 21:52:17 +0200748" Tests for regexp with various magic settings
Bram Moolenaar4d23c522020-04-09 18:42:11 +0200749func Run_search_regexp_magic_opt()
Bram Moolenaardb510072017-09-28 21:52:17 +0200750 put ='1 a aa abb abbccc'
751 exe 'normal! /a*b\{2}c\+/e' . "\<CR>"
752 call assert_equal([0, 2, 17, 0], getpos('.'))
753
754 put ='2 d dd dee deefff'
755 exe 'normal! /\Md\*e\{2}f\+/e' . "\<CR>"
756 call assert_equal([0, 3, 17, 0], getpos('.'))
757
758 set nomagic
759 put ='3 g gg ghh ghhiii'
760 exe 'normal! /g\*h\{2}i\+/e' . "\<CR>"
761 call assert_equal([0, 4, 17, 0], getpos('.'))
762
763 put ='4 j jj jkk jkklll'
764 exe 'normal! /\mj*k\{2}l\+/e' . "\<CR>"
765 call assert_equal([0, 5, 17, 0], getpos('.'))
766
767 put ='5 m mm mnn mnnooo'
768 exe 'normal! /\vm*n{2}o+/e' . "\<CR>"
769 call assert_equal([0, 6, 17, 0], getpos('.'))
770
771 put ='6 x ^aa$ x'
772 exe 'normal! /\V^aa$' . "\<CR>"
773 call assert_equal([0, 7, 5, 0], getpos('.'))
774
775 set magic
776 put ='7 (a)(b) abbaa'
777 exe 'normal! /\v(a)(b)\2\1\1/e' . "\<CR>"
778 call assert_equal([0, 8, 14, 0], getpos('.'))
779
780 put ='8 axx [ab]xx'
781 exe 'normal! /\V[ab]\(\[xy]\)\1' . "\<CR>"
782 call assert_equal([0, 9, 7, 0], getpos('.'))
783
Bram Moolenaar4d23c522020-04-09 18:42:11 +0200784 %d
785endfunc
786
787func Test_search_regexp()
788 enew!
789
790 set regexpengine=1
791 call Run_search_regexp_magic_opt()
792 set regexpengine=2
793 call Run_search_regexp_magic_opt()
794 set regexpengine&
795
Bram Moolenaardb510072017-09-28 21:52:17 +0200796 set undolevels=100
797 put ='9 foobar'
798 put =''
799 exe "normal! a\<C-G>u\<Esc>"
800 normal G
801 exe 'normal! dv?bar?' . "\<CR>"
802 call assert_equal('9 foo', getline('.'))
Bram Moolenaar4d23c522020-04-09 18:42:11 +0200803 call assert_equal([0, 2, 5, 0], getpos('.'))
804 call assert_equal(2, line('$'))
Bram Moolenaardb510072017-09-28 21:52:17 +0200805 normal u
806 call assert_equal('9 foobar', getline('.'))
Bram Moolenaar4d23c522020-04-09 18:42:11 +0200807 call assert_equal([0, 2, 6, 0], getpos('.'))
808 call assert_equal(3, line('$'))
Bram Moolenaardb510072017-09-28 21:52:17 +0200809
810 set undolevels&
811 enew!
812endfunc
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100813
814func Test_search_cmdline_incsearch_highlight()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100815 CheckOption incsearch
816
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100817 set incsearch hlsearch
818 " need to disable char_avail,
819 " so that expansion of commandline works
820 call test_override("char_avail", 1)
821 new
822 call setline(1, ['aaa 1 the first', ' 2 the second', ' 3 the third'])
823
824 1
825 call feedkeys("/second\<cr>", 'tx')
826 call assert_equal('second', @/)
827 call assert_equal(' 2 the second', getline('.'))
828
829 " Canceling search won't change @/
830 1
831 let @/ = 'last pattern'
832 call feedkeys("/third\<C-c>", 'tx')
833 call assert_equal('last pattern', @/)
834 call feedkeys("/third\<Esc>", 'tx')
835 call assert_equal('last pattern', @/)
836 call feedkeys("/3\<bs>\<bs>", 'tx')
837 call assert_equal('last pattern', @/)
838 call feedkeys("/third\<c-g>\<c-t>\<Esc>", 'tx')
839 call assert_equal('last pattern', @/)
840
841 " clean up
842 set noincsearch nohlsearch
843 bw!
844endfunc
845
846func Test_search_cmdline_incsearch_highlight_attr()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200847 CheckOption incsearch
848 CheckFeature terminal
849 CheckNotGui
850
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100851 let h = winheight(0)
852 if h < 3
853 return
854 endif
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100855
856 " Prepare buffer text
Bram Moolenaar13deab82017-11-04 18:48:43 +0100857 let lines = ['abb vim vim vi', 'vimvivim']
858 call writefile(lines, 'Xsearch.txt')
859 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', 'Xsearch.txt'], {'term_rows': 3})
860
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200861 call WaitForAssert({-> assert_equal(lines, [term_getline(buf, 1), term_getline(buf, 2)])})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100862 " wait for vim to complete initialization
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200863 call TermWait(buf)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100864
865 " Get attr of normal(a0), incsearch(a1), hlsearch(a2) highlight
Bram Moolenaar13deab82017-11-04 18:48:43 +0100866 call term_sendkeys(buf, ":set incsearch hlsearch\<cr>")
867 call term_sendkeys(buf, '/b')
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200868 call TermWait(buf, 100)
Bram Moolenaar13deab82017-11-04 18:48:43 +0100869 let screen_line1 = term_scrape(buf, 1)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100870 call assert_true(len(screen_line1) > 2)
871 " a0: attr_normal
872 let a0 = screen_line1[0].attr
873 " a1: attr_incsearch
874 let a1 = screen_line1[1].attr
875 " a2: attr_hlsearch
876 let a2 = screen_line1[2].attr
877 call assert_notequal(a0, a1)
878 call assert_notequal(a0, a2)
879 call assert_notequal(a1, a2)
Bram Moolenaar13deab82017-11-04 18:48:43 +0100880 call term_sendkeys(buf, "\<cr>gg0")
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100881
882 " Test incremental highlight search
Bram Moolenaar13deab82017-11-04 18:48:43 +0100883 call term_sendkeys(buf, "/vim")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200884 call TermWait(buf, 100)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100885 " Buffer:
886 " abb vim vim vi
887 " vimvivim
888 " Search: /vim
889 let attr_line1 = [a0,a0,a0,a0,a1,a1,a1,a0,a2,a2,a2,a0,a0,a0]
890 let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100891 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
892 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100893
894 " Test <C-g>
Bram Moolenaar13deab82017-11-04 18:48:43 +0100895 call term_sendkeys(buf, "\<C-g>\<C-g>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200896 call TermWait(buf, 100)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100897 let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a2,a2,a2,a0,a0,a0]
898 let attr_line2 = [a1,a1,a1,a0,a0,a2,a2,a2]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100899 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
900 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100901
902 " Test <C-t>
Bram Moolenaar13deab82017-11-04 18:48:43 +0100903 call term_sendkeys(buf, "\<C-t>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200904 call TermWait(buf, 100)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100905 let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a1,a1,a1,a0,a0,a0]
906 let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100907 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
908 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100909
910 " Type Enter and a1(incsearch highlight) should become a2(hlsearch highlight)
Bram Moolenaar13deab82017-11-04 18:48:43 +0100911 call term_sendkeys(buf, "\<cr>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200912 call TermWait(buf, 100)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100913 let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a2,a2,a2,a0,a0,a0]
914 let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100915 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
916 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100917
918 " Test nohlsearch. a2(hlsearch highlight) should become a0(normal highlight)
Bram Moolenaar13deab82017-11-04 18:48:43 +0100919 call term_sendkeys(buf, ":1\<cr>")
920 call term_sendkeys(buf, ":set nohlsearch\<cr>")
921 call term_sendkeys(buf, "/vim")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200922 call TermWait(buf, 100)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100923 let attr_line1 = [a0,a0,a0,a0,a1,a1,a1,a0,a0,a0,a0,a0,a0,a0]
924 let attr_line2 = [a0,a0,a0,a0,a0,a0,a0,a0]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100925 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
926 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100927 call delete('Xsearch.txt')
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100928
Bram Moolenaarb94340c2017-11-02 16:16:31 +0100929 call delete('Xsearch.txt')
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100930 bwipe!
931endfunc
Bram Moolenaarf45938c2017-11-02 15:59:57 +0100932
Bram Moolenaar548e5982018-12-26 21:45:00 +0100933func Test_incsearch_cmdline_modifier()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100934 CheckOption incsearch
935
Bram Moolenaar548e5982018-12-26 21:45:00 +0100936 call test_override("char_avail", 1)
937 new
938 call setline(1, ['foo'])
939 set incsearch
940 " Test that error E14 does not occur in parsing command modifier.
941 call feedkeys("V:tab", 'tx')
942
943 call Incsearch_cleanup()
944endfunc
945
Bram Moolenaar9d34d902018-04-27 22:18:12 +0200946func Test_incsearch_scrolling()
Bram Moolenaar494e9062020-05-31 21:28:02 +0200947 CheckRunVimInTerminal
Bram Moolenaar9d34d902018-04-27 22:18:12 +0200948 call assert_equal(0, &scrolloff)
949 call writefile([
950 \ 'let dots = repeat(".", 120)',
951 \ 'set incsearch cmdheight=2 scrolloff=0',
952 \ 'call setline(1, [dots, dots, dots, "", "target", dots, dots])',
953 \ 'normal gg',
954 \ 'redraw',
955 \ ], 'Xscript')
956 let buf = RunVimInTerminal('-S Xscript', {'rows': 9, 'cols': 70})
957 " Need to send one key at a time to force a redraw
958 call term_sendkeys(buf, '/')
959 sleep 100m
960 call term_sendkeys(buf, 't')
961 sleep 100m
962 call term_sendkeys(buf, 'a')
963 sleep 100m
964 call term_sendkeys(buf, 'r')
965 sleep 100m
966 call term_sendkeys(buf, 'g')
967 call VerifyScreenDump(buf, 'Test_incsearch_scrolling_01', {})
968
969 call term_sendkeys(buf, "\<Esc>")
970 call StopVimInTerminal(buf)
971 call delete('Xscript')
972endfunc
973
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200974func Test_incsearch_search_dump()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100975 CheckOption incsearch
976 CheckScreendump
977
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200978 call writefile([
979 \ 'set incsearch hlsearch scrolloff=0',
980 \ 'for n in range(1, 8)',
981 \ ' call setline(n, "foo " . n)',
982 \ 'endfor',
983 \ '3',
984 \ ], 'Xis_search_script')
985 let buf = RunVimInTerminal('-S Xis_search_script', {'rows': 9, 'cols': 70})
986 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
987 " the 'ambiwidth' check.
988 sleep 100m
989
990 " Need to send one key at a time to force a redraw.
991 call term_sendkeys(buf, '/fo')
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200992 call VerifyScreenDump(buf, 'Test_incsearch_search_01', {})
993 call term_sendkeys(buf, "\<Esc>")
994 sleep 100m
995
996 call term_sendkeys(buf, '/\v')
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200997 call VerifyScreenDump(buf, 'Test_incsearch_search_02', {})
998 call term_sendkeys(buf, "\<Esc>")
999
1000 call StopVimInTerminal(buf)
1001 call delete('Xis_search_script')
1002endfunc
1003
Bram Moolenaar41f08952021-02-22 22:13:49 +01001004func Test_hlsearch_dump()
1005 CheckOption hlsearch
1006 CheckScreendump
1007
1008 call writefile([
1009 \ 'set hlsearch cursorline',
1010 \ 'call setline(1, ["xxx", "xxx", "xxx"])',
1011 \ '/.*',
1012 \ '2',
1013 \ ], 'Xhlsearch_script')
1014 let buf = RunVimInTerminal('-S Xhlsearch_script', {'rows': 6, 'cols': 50})
1015 call VerifyScreenDump(buf, 'Test_hlsearch_1', {})
1016
1017 call term_sendkeys(buf, "/\\_.*\<CR>")
1018 call VerifyScreenDump(buf, 'Test_hlsearch_2', {})
1019
1020 call StopVimInTerminal(buf)
1021 call delete('Xhlsearch_script')
1022endfunc
1023
Bram Moolenaar2d5f3852021-04-21 15:11:42 +02001024func Test_hlsearch_and_visual()
1025 CheckOption hlsearch
1026 CheckScreendump
1027
1028 call writefile([
1029 \ 'set hlsearch',
1030 \ 'call setline(1, repeat(["xxx yyy zzz"], 3))',
1031 \ 'hi Search cterm=bold',
1032 \ '/yyy',
1033 \ 'call cursor(1, 6)',
1034 \ ], 'Xhlvisual_script')
1035 let buf = RunVimInTerminal('-S Xhlvisual_script', {'rows': 6, 'cols': 40})
1036 call term_sendkeys(buf, "vjj")
1037 call VerifyScreenDump(buf, 'Test_hlsearch_visual_1', {})
1038 call term_sendkeys(buf, "\<Esc>")
1039
1040 call StopVimInTerminal(buf)
1041 call delete('Xhlvisual_script')
1042endfunc
1043
Bram Moolenaare71c0eb2021-05-30 16:43:11 +02001044func Test_hlsearch_block_visual_match()
1045 CheckScreendump
1046
1047 let lines =<< trim END
1048 set hlsearch
1049 call setline(1, ['aa', 'bbbb', 'cccccc'])
1050 END
1051 call writefile(lines, 'Xhlsearch_block')
1052 let buf = RunVimInTerminal('-S Xhlsearch_block', {'rows': 9, 'cols': 60})
1053
1054 call term_sendkeys(buf, "G\<C-V>$kk\<Esc>")
1055 sleep 100m
1056 call term_sendkeys(buf, "/\\%V\<CR>")
1057 sleep 100m
1058 call VerifyScreenDump(buf, 'Test_hlsearch_block_visual_match', {})
1059
1060 call StopVimInTerminal(buf)
1061 call delete('Xhlsearch_block')
1062endfunc
1063
Bram Moolenaar976b8472018-08-12 15:49:47 +02001064func Test_incsearch_substitute()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001065 CheckOption incsearch
1066
Bram Moolenaar976b8472018-08-12 15:49:47 +02001067 call test_override("char_avail", 1)
1068 new
1069 set incsearch
1070 for n in range(1, 10)
1071 call setline(n, 'foo ' . n)
1072 endfor
1073 4
1074 call feedkeys(":.,.+2s/foo\<BS>o\<BS>o/xxx\<cr>", 'tx')
1075 call assert_equal('foo 3', getline(3))
1076 call assert_equal('xxx 4', getline(4))
1077 call assert_equal('xxx 5', getline(5))
1078 call assert_equal('xxx 6', getline(6))
1079 call assert_equal('foo 7', getline(7))
1080
1081 call Incsearch_cleanup()
1082endfunc
1083
Bram Moolenaar795aaa12020-10-02 20:36:01 +02001084func Test_incsearch_substitute_long_line()
1085 new
1086 call test_override("char_avail", 1)
1087 set incsearch
1088
1089 call repeat('x', 100000)->setline(1)
1090 call feedkeys(':s/\%c', 'xt')
1091 redraw
1092 call feedkeys("\<Esc>", 'xt')
1093
1094 call Incsearch_cleanup()
1095 bwipe!
1096endfunc
1097
LemonBoya4399382022-04-09 21:04:08 +01001098func Test_hlsearch_cursearch()
1099 CheckScreendump
1100
1101 let lines =<< trim END
1102 set hlsearch scrolloff=0
Bram Moolenaar9b367502022-04-22 20:07:21 +01001103 call setline(1, ['one', 'foo', 'bar', 'baz', 'foo the foo and foo', 'bar'])
LemonBoya4399382022-04-09 21:04:08 +01001104 hi Search ctermbg=yellow
1105 hi CurSearch ctermbg=blue
1106 END
1107 call writefile(lines, 'Xhlsearch_cursearch')
1108 let buf = RunVimInTerminal('-S Xhlsearch_cursearch', {'rows': 9, 'cols': 60})
1109
1110 call term_sendkeys(buf, "gg/foo\<CR>")
1111 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_single_line_1', {})
1112
1113 call term_sendkeys(buf, "n")
1114 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_single_line_2', {})
1115
Bram Moolenaar9b367502022-04-22 20:07:21 +01001116 call term_sendkeys(buf, "n")
1117 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_single_line_2a', {})
1118
1119 call term_sendkeys(buf, "n")
1120 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_single_line_2b', {})
1121
1122 call term_sendkeys(buf, ":call setline(5, 'foo')\<CR>")
1123 call term_sendkeys(buf, "0?\<CR>")
LemonBoya4399382022-04-09 21:04:08 +01001124 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_single_line_3', {})
1125
1126 call term_sendkeys(buf, "gg/foo\\nbar\<CR>")
Bram Moolenaar693ccd12022-04-16 12:04:37 +01001127 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_multiple_line_1', {})
1128
1129 call term_sendkeys(buf, ":call setline(1, ['---', 'abcdefg', 'hijkl', '---', 'abcdefg', 'hijkl'])\<CR>")
1130 call term_sendkeys(buf, "gg/efg\\nhij\<CR>")
1131 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_multiple_line_2', {})
1132 call term_sendkeys(buf, "h\<C-L>")
1133 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_multiple_line_3', {})
1134 call term_sendkeys(buf, "j\<C-L>")
1135 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_multiple_line_4', {})
1136 call term_sendkeys(buf, "h\<C-L>")
1137 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_multiple_line_5', {})
LemonBoya4399382022-04-09 21:04:08 +01001138
Bram Moolenaar368137a2022-05-31 13:43:12 +01001139 " check clearing CurSearch when using it for another match
1140 call term_sendkeys(buf, "G?^abcd\<CR>Y")
1141 call term_sendkeys(buf, "kkP")
1142 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_changed_1', {})
1143
LemonBoya4399382022-04-09 21:04:08 +01001144 call StopVimInTerminal(buf)
1145 call delete('Xhlsearch_cursearch')
1146endfunc
1147
Bram Moolenaar164251f2018-08-12 16:26:58 +02001148" Similar to Test_incsearch_substitute() but with a screendump halfway.
1149func Test_incsearch_substitute_dump()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001150 CheckOption incsearch
1151 CheckScreendump
1152
Bram Moolenaar164251f2018-08-12 16:26:58 +02001153 call writefile([
1154 \ 'set incsearch hlsearch scrolloff=0',
1155 \ 'for n in range(1, 10)',
1156 \ ' call setline(n, "foo " . n)',
1157 \ 'endfor',
Bram Moolenaar2f6a3462018-08-14 18:16:52 +02001158 \ 'call setline(11, "bar 11")',
Bram Moolenaar164251f2018-08-12 16:26:58 +02001159 \ '3',
1160 \ ], 'Xis_subst_script')
1161 let buf = RunVimInTerminal('-S Xis_subst_script', {'rows': 9, 'cols': 70})
1162 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1163 " the 'ambiwidth' check.
1164 sleep 100m
1165
1166 " Need to send one key at a time to force a redraw.
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02001167 " Select three lines at the cursor with typed pattern.
Bram Moolenaar164251f2018-08-12 16:26:58 +02001168 call term_sendkeys(buf, ':.,.+2s/')
1169 sleep 100m
1170 call term_sendkeys(buf, 'f')
1171 sleep 100m
1172 call term_sendkeys(buf, 'o')
1173 sleep 100m
1174 call term_sendkeys(buf, 'o')
1175 call VerifyScreenDump(buf, 'Test_incsearch_substitute_01', {})
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02001176 call term_sendkeys(buf, "\<Esc>")
1177
1178 " Select three lines at the cursor using previous pattern.
1179 call term_sendkeys(buf, "/foo\<CR>")
1180 sleep 100m
1181 call term_sendkeys(buf, ':.,.+2s//')
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02001182 call VerifyScreenDump(buf, 'Test_incsearch_substitute_02', {})
1183
1184 " Deleting last slash should remove the match.
1185 call term_sendkeys(buf, "\<BS>")
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02001186 call VerifyScreenDump(buf, 'Test_incsearch_substitute_03', {})
Bram Moolenaar60d08712018-08-12 21:53:15 +02001187 call term_sendkeys(buf, "\<Esc>")
1188
1189 " Reverse range is accepted
1190 call term_sendkeys(buf, ':5,2s/foo')
Bram Moolenaar60d08712018-08-12 21:53:15 +02001191 call VerifyScreenDump(buf, 'Test_incsearch_substitute_04', {})
Bram Moolenaar164251f2018-08-12 16:26:58 +02001192 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaar2b926fc2018-08-13 11:07:57 +02001193
1194 " White space after the command is skipped
1195 call term_sendkeys(buf, ':2,3sub /fo')
Bram Moolenaar2b926fc2018-08-13 11:07:57 +02001196 call VerifyScreenDump(buf, 'Test_incsearch_substitute_05', {})
1197 call term_sendkeys(buf, "\<Esc>")
1198
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001199 " Command modifiers are skipped
1200 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 +02001201 call VerifyScreenDump(buf, 'Test_incsearch_substitute_06', {})
1202 call term_sendkeys(buf, "\<Esc>")
1203
Bram Moolenaar2f6a3462018-08-14 18:16:52 +02001204 " Cursorline highlighting at match
1205 call term_sendkeys(buf, ":set cursorline\<CR>")
1206 call term_sendkeys(buf, 'G9G')
1207 call term_sendkeys(buf, ':9,11s/bar')
Bram Moolenaar2f6a3462018-08-14 18:16:52 +02001208 call VerifyScreenDump(buf, 'Test_incsearch_substitute_07', {})
1209 call term_sendkeys(buf, "\<Esc>")
1210
1211 " Cursorline highlighting at cursor when no match
1212 call term_sendkeys(buf, ':9,10s/bar')
Bram Moolenaar2f6a3462018-08-14 18:16:52 +02001213 call VerifyScreenDump(buf, 'Test_incsearch_substitute_08', {})
1214 call term_sendkeys(buf, "\<Esc>")
1215
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +02001216 " Only \v handled as empty pattern, does not move cursor
1217 call term_sendkeys(buf, '3G4G')
1218 call term_sendkeys(buf, ":nohlsearch\<CR>")
1219 call term_sendkeys(buf, ':6,7s/\v')
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +02001220 call VerifyScreenDump(buf, 'Test_incsearch_substitute_09', {})
1221 call term_sendkeys(buf, "\<Esc>")
1222
Bram Moolenaarf13daa42018-08-31 22:09:54 +02001223 call term_sendkeys(buf, ":set nocursorline\<CR>")
1224
1225 " All matches are highlighted for 'hlsearch' after the incsearch canceled
1226 call term_sendkeys(buf, "1G*")
1227 call term_sendkeys(buf, ":2,5s/foo")
1228 sleep 100m
1229 call term_sendkeys(buf, "\<Esc>")
1230 call VerifyScreenDump(buf, 'Test_incsearch_substitute_10', {})
1231
Bram Moolenaar65985ac2018-09-16 17:08:04 +02001232 call term_sendkeys(buf, ":split\<CR>")
1233 call term_sendkeys(buf, ":let @/ = 'xyz'\<CR>")
1234 call term_sendkeys(buf, ":%s/.")
1235 call VerifyScreenDump(buf, 'Test_incsearch_substitute_11', {})
1236 call term_sendkeys(buf, "\<BS>")
1237 call VerifyScreenDump(buf, 'Test_incsearch_substitute_12', {})
1238 call term_sendkeys(buf, "\<Esc>")
1239 call VerifyScreenDump(buf, 'Test_incsearch_substitute_13', {})
Bram Moolenaarc6725252019-11-23 21:56:46 +01001240 call term_sendkeys(buf, ":%bwipe!\<CR>")
1241 call term_sendkeys(buf, ":only!\<CR>")
1242
1243 " get :'<,'>s command in history
1244 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
1245 call term_sendkeys(buf, "aasdfasdf\<Esc>")
1246 call term_sendkeys(buf, "V:s/a/b/g\<CR>")
1247 " Using '<,'> does not give E20
1248 call term_sendkeys(buf, ":new\<CR>")
1249 call term_sendkeys(buf, "aasdfasdf\<Esc>")
1250 call term_sendkeys(buf, ":\<Up>\<Up>")
1251 call VerifyScreenDump(buf, 'Test_incsearch_substitute_14', {})
1252 call term_sendkeys(buf, "<Esc>")
Bram Moolenaar65985ac2018-09-16 17:08:04 +02001253
Bram Moolenaar164251f2018-08-12 16:26:58 +02001254 call StopVimInTerminal(buf)
1255 call delete('Xis_subst_script')
1256endfunc
1257
Bram Moolenaarc036e872020-02-21 21:30:52 +01001258func Test_incsearch_highlighting()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001259 CheckOption incsearch
1260 CheckScreendump
Bram Moolenaarc036e872020-02-21 21:30:52 +01001261
1262 call writefile([
1263 \ 'set incsearch hlsearch',
1264 \ 'call setline(1, "hello/there")',
1265 \ ], 'Xis_subst_hl_script')
1266 let buf = RunVimInTerminal('-S Xis_subst_hl_script', {'rows': 4, 'cols': 20})
1267 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1268 " the 'ambiwidth' check.
1269 sleep 300m
1270
1271 " Using a different search delimiter should still highlight matches
1272 " that contain a '/'.
1273 call term_sendkeys(buf, ":%s;ello/the")
1274 call VerifyScreenDump(buf, 'Test_incsearch_substitute_15', {})
1275 call term_sendkeys(buf, "<Esc>")
Bram Moolenaarb68df222020-03-19 15:05:28 +01001276
1277 call StopVimInTerminal(buf)
1278 call delete('Xis_subst_hl_script')
Bram Moolenaarc036e872020-02-21 21:30:52 +01001279endfunc
1280
Bram Moolenaar4a7d2d32019-02-21 16:25:50 +01001281func Test_incsearch_with_change()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001282 CheckFeature timers
1283 CheckOption incsearch
1284 CheckScreendump
Bram Moolenaar4a7d2d32019-02-21 16:25:50 +01001285
1286 call writefile([
1287 \ 'set incsearch hlsearch scrolloff=0',
1288 \ 'call setline(1, ["one", "two ------ X", "three"])',
1289 \ 'call timer_start(200, { _ -> setline(2, "x")})',
1290 \ ], 'Xis_change_script')
1291 let buf = RunVimInTerminal('-S Xis_change_script', {'rows': 9, 'cols': 70})
1292 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1293 " the 'ambiwidth' check.
Bram Moolenaare86ecbd2019-02-21 17:48:59 +01001294 sleep 300m
Bram Moolenaar4a7d2d32019-02-21 16:25:50 +01001295
1296 " Highlight X, it will be deleted by the timer callback.
1297 call term_sendkeys(buf, ':%s/X')
1298 call VerifyScreenDump(buf, 'Test_incsearch_change_01', {})
1299 call term_sendkeys(buf, "\<Esc>")
1300
1301 call StopVimInTerminal(buf)
1302 call delete('Xis_change_script')
1303endfunc
1304
Bram Moolenaar81f56532018-08-18 16:19:42 +02001305" Similar to Test_incsearch_substitute_dump() for :sort
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +02001306func Test_incsearch_sort_dump()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001307 CheckOption incsearch
1308 CheckScreendump
1309
Bram Moolenaar81f56532018-08-18 16:19:42 +02001310 call writefile([
1311 \ 'set incsearch hlsearch scrolloff=0',
1312 \ 'call setline(1, ["another one 2", "that one 3", "the one 1"])',
1313 \ ], 'Xis_sort_script')
1314 let buf = RunVimInTerminal('-S Xis_sort_script', {'rows': 9, 'cols': 70})
1315 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1316 " the 'ambiwidth' check.
1317 sleep 100m
1318
Bram Moolenaar81f56532018-08-18 16:19:42 +02001319 call term_sendkeys(buf, ':sort ni u /on')
Bram Moolenaar81f56532018-08-18 16:19:42 +02001320 call VerifyScreenDump(buf, 'Test_incsearch_sort_01', {})
1321 call term_sendkeys(buf, "\<Esc>")
1322
Bram Moolenaar333015a2020-04-25 15:54:16 +02001323 call term_sendkeys(buf, ':sort! /on')
1324 call VerifyScreenDump(buf, 'Test_incsearch_sort_02', {})
1325 call term_sendkeys(buf, "\<Esc>")
1326
Bram Moolenaar81f56532018-08-18 16:19:42 +02001327 call StopVimInTerminal(buf)
1328 call delete('Xis_sort_script')
1329endfunc
1330
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001331" Similar to Test_incsearch_substitute_dump() for :vimgrep famiry
1332func Test_incsearch_vimgrep_dump()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001333 CheckOption incsearch
1334 CheckScreendump
1335
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001336 call writefile([
1337 \ 'set incsearch hlsearch scrolloff=0',
1338 \ 'call setline(1, ["another one 2", "that one 3", "the one 1"])',
1339 \ ], 'Xis_vimgrep_script')
1340 let buf = RunVimInTerminal('-S Xis_vimgrep_script', {'rows': 9, 'cols': 70})
1341 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1342 " the 'ambiwidth' check.
1343 sleep 100m
1344
1345 " Need to send one key at a time to force a redraw.
1346 call term_sendkeys(buf, ':vimgrep on')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001347 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_01', {})
1348 call term_sendkeys(buf, "\<Esc>")
1349
1350 call term_sendkeys(buf, ':vimg /on/ *.txt')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001351 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_02', {})
1352 call term_sendkeys(buf, "\<Esc>")
1353
1354 call term_sendkeys(buf, ':vimgrepadd "\<on')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001355 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_03', {})
1356 call term_sendkeys(buf, "\<Esc>")
1357
1358 call term_sendkeys(buf, ':lv "tha')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001359 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_04', {})
1360 call term_sendkeys(buf, "\<Esc>")
1361
1362 call term_sendkeys(buf, ':lvimgrepa "the" **/*.txt')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001363 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_05', {})
1364 call term_sendkeys(buf, "\<Esc>")
1365
1366 call StopVimInTerminal(buf)
1367 call delete('Xis_vimgrep_script')
1368endfunc
1369
Bram Moolenaar198cb662018-09-06 21:44:17 +02001370func Test_keep_last_search_pattern()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001371 CheckOption incsearch
1372
Bram Moolenaar198cb662018-09-06 21:44:17 +02001373 new
1374 call setline(1, ['foo', 'foo', 'foo'])
1375 set incsearch
1376 call test_override("char_avail", 1)
1377 let @/ = 'bar'
1378 call feedkeys(":/foo/s//\<Esc>", 'ntx')
1379 call assert_equal('bar', @/)
1380
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001381 " no error message if pattern not found
1382 call feedkeys(":/xyz/s//\<Esc>", 'ntx')
1383 call assert_equal('bar', @/)
1384
Bram Moolenaar198cb662018-09-06 21:44:17 +02001385 bwipe!
1386 call test_override("ALL", 0)
1387 set noincsearch
1388endfunc
1389
Bram Moolenaar99f043a2018-09-09 15:54:14 +02001390func Test_word_under_cursor_after_match()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001391 CheckOption incsearch
1392
Bram Moolenaar99f043a2018-09-09 15:54:14 +02001393 new
1394 call setline(1, 'foo bar')
1395 set incsearch
1396 call test_override("char_avail", 1)
1397 try
1398 call feedkeys("/foo\<C-R>\<C-W>\<CR>", 'ntx')
1399 catch /E486:/
1400 endtry
1401 call assert_equal('foobar', @/)
1402
1403 bwipe!
1404 call test_override("ALL", 0)
1405 set noincsearch
1406endfunc
1407
1408func Test_subst_word_under_cursor()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001409 CheckOption incsearch
1410
Bram Moolenaar99f043a2018-09-09 15:54:14 +02001411 new
1412 call setline(1, ['int SomeLongName;', 'for (xxx = 1; xxx < len; ++xxx)'])
1413 set incsearch
1414 call test_override("char_avail", 1)
1415 call feedkeys("/LongName\<CR>", 'ntx')
1416 call feedkeys(":%s/xxx/\<C-R>\<C-W>/g\<CR>", 'ntx')
1417 call assert_equal('for (SomeLongName = 1; SomeLongName < len; ++SomeLongName)', getline(2))
1418
1419 bwipe!
1420 call test_override("ALL", 0)
1421 set noincsearch
1422endfunc
1423
Bram Moolenaarf45938c2017-11-02 15:59:57 +01001424func Test_search_undefined_behaviour()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001425 CheckFeature terminal
1426
Bram Moolenaarf45938c2017-11-02 15:59:57 +01001427 let h = winheight(0)
1428 if h < 3
1429 return
1430 endif
1431 " did cause an undefined left shift
1432 let g:buf = term_start([GetVimProg(), '--clean', '-e', '-s', '-c', 'call search(getline("."))', 'samples/test000'], {'term_rows': 3})
1433 call assert_equal([''], getline(1, '$'))
1434 call term_sendkeys(g:buf, ":qa!\<cr>")
1435 bwipe!
1436endfunc
Bram Moolenaar2973daa2017-11-02 23:15:40 +01001437
1438func Test_search_undefined_behaviour2()
1439 call search("\%UC0000000")
1440endfunc
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001441
1442" Test for search('multi-byte char', 'bce')
1443func Test_search_multibyte()
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001444 let save_enc = &encoding
1445 set encoding=utf8
1446 enew!
1447 call append('$', 'A')
1448 call cursor(2, 1)
1449 call assert_equal(2, search('A', 'bce', line('.')))
1450 enew!
1451 let &encoding = save_enc
1452endfunc
Bram Moolenaar890dd052017-12-16 19:59:37 +01001453
1454" This was causing E874. Also causes an invalid read?
1455func Test_look_behind()
1456 new
Bram Moolenaar3ba35402019-12-21 22:00:50 +01001457 call setline(1, '0\|\&\n\@<=')
Bram Moolenaar890dd052017-12-16 19:59:37 +01001458 call search(getline("."))
1459 bwipe!
1460endfunc
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01001461
Bram Moolenaar872bee52021-05-24 22:56:15 +02001462func Test_search_visual_area_linewise()
1463 new
1464 call setline(1, ['aa', 'bb', 'cc'])
1465 exe "normal 2GV\<Esc>"
1466 for engine in [1, 2]
1467 exe 'set regexpengine=' .. engine
1468 exe "normal gg/\\%'<\<CR>>"
1469 call assert_equal([0, 2, 1, 0, 1], getcurpos(), 'engine ' .. engine)
1470 exe "normal gg/\\%'>\<CR>"
1471 call assert_equal([0, 2, 2, 0, 2], getcurpos(), 'engine ' .. engine)
1472 endfor
1473
1474 bwipe!
1475 set regexpengine&
1476endfunc
1477
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01001478func Test_search_sentence()
1479 new
1480 " this used to cause a crash
Bram Moolenaar1bd999f2017-12-19 22:25:40 +01001481 /\%'(
1482 /
Bram Moolenaar872bee52021-05-24 22:56:15 +02001483 bwipe
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01001484endfunc
Bram Moolenaar2fb8f682018-12-01 13:14:45 +01001485
1486" Test that there is no crash when there is a last search pattern but no last
1487" substitute pattern.
1488func Test_no_last_substitute_pat()
1489 " Use viminfo to set the last search pattern to a string and make the last
1490 " substitute pattern the most recent used and make it empty (NULL).
1491 call writefile(['~MSle0/bar', '~MSle0~&'], 'Xviminfo')
1492 rviminfo! Xviminfo
1493 call assert_fails('normal n', 'E35:')
1494
1495 call delete('Xviminfo')
1496endfunc
Bram Moolenaar5f5e2032018-12-14 15:47:03 +01001497
1498func Test_search_Ctrl_L_combining()
1499 " Make sure, that Ctrl-L works correctly with combining characters.
1500 " It uses an artificial example of an 'a' with 4 combining chars:
Bram Moolenaar3ba35402019-12-21 22:00:50 +01001501 " 'a' U+0061 Dec:97 LATIN SMALL LETTER A &#x61; /\%u61\Z "\u0061"
Bram Moolenaar5f5e2032018-12-14 15:47:03 +01001502 " ' ̀' U+0300 Dec:768 COMBINING GRAVE ACCENT &#x300; /\%u300\Z "\u0300"
1503 " ' ́' U+0301 Dec:769 COMBINING ACUTE ACCENT &#x301; /\%u301\Z "\u0301"
1504 " ' ̇' U+0307 Dec:775 COMBINING DOT ABOVE &#x307; /\%u307\Z "\u0307"
Bram Moolenaar3ba35402019-12-21 22:00:50 +01001505 " ' ̣' U+0323 Dec:803 COMBINING DOT BELOW &#x323; /\%u323 "\u0323"
Bram Moolenaar5f5e2032018-12-14 15:47:03 +01001506 " Those should also appear on the commandline
Bram Moolenaarb68df222020-03-19 15:05:28 +01001507 CheckOption incsearch
1508
Bram Moolenaar5f5e2032018-12-14 15:47:03 +01001509 call Cmdline3_prep()
1510 1
1511 let bufcontent = ['', 'Miạ̀́̇m']
1512 call append('$', bufcontent)
1513 call feedkeys("/Mi\<c-l>\<c-l>\<cr>", 'tx')
1514 call assert_equal(5, line('.'))
1515 call assert_equal(bufcontent[1], @/)
1516 call Incsearch_cleanup()
1517endfunc
Bram Moolenaar527a2d82019-02-21 22:28:51 +01001518
Bram Moolenaarab350f82019-02-28 06:25:00 +01001519func Test_large_hex_chars1()
Bram Moolenaar527a2d82019-02-21 22:28:51 +01001520 " This used to cause a crash, the character becomes an NFA state.
1521 try
1522 /\%Ufffffc23
1523 catch
1524 call assert_match('E678:', v:exception)
1525 endtry
Bram Moolenaarab350f82019-02-28 06:25:00 +01001526 try
1527 set re=1
1528 /\%Ufffffc23
1529 catch
1530 call assert_match('E678:', v:exception)
1531 endtry
1532 set re&
1533endfunc
1534
1535func Test_large_hex_chars2()
1536 " This used to cause a crash, the character becomes an NFA state.
1537 try
1538 /[\Ufffffc1f]
1539 catch
1540 call assert_match('E486:', v:exception)
1541 endtry
1542 try
1543 set re=1
1544 /[\Ufffffc1f]
1545 catch
1546 call assert_match('E486:', v:exception)
1547 endtry
1548 set re&
Bram Moolenaar527a2d82019-02-21 22:28:51 +01001549endfunc
Bram Moolenaarcd625122019-02-22 17:29:43 +01001550
1551func Test_one_error_msg()
Bram Moolenaar8832a342020-04-11 18:36:38 +02001552 " This was also giving an internal error
Bram Moolenaarcd625122019-02-22 17:29:43 +01001553 call assert_fails('call search(" \\((\\v[[=P=]]){185}+ ")', 'E871:')
1554endfunc
Bram Moolenaar730f48f2019-04-11 13:45:57 +02001555
1556func Test_incsearch_add_char_under_cursor()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001557 CheckOption incsearch
1558
Bram Moolenaar730f48f2019-04-11 13:45:57 +02001559 set incsearch
1560 new
1561 call setline(1, ['find match', 'anything'])
1562 1
1563 call test_override('char_avail', 1)
1564 call feedkeys("fc/m\<C-L>\<C-L>\<C-L>\<C-L>\<C-L>\<CR>", 'tx')
1565 call assert_equal('match', @/)
1566 call test_override('char_avail', 0)
1567
1568 set incsearch&
1569 bwipe!
1570endfunc
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02001571
1572" Test for the search() function with match at the cursor position
1573func Test_search_match_at_curpos()
1574 new
1575 call append(0, ['foobar', '', 'one two', ''])
1576
1577 normal gg
1578
Bram Moolenaar196b4662019-09-06 21:34:30 +02001579 eval 'foobar'->search('c')
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02001580 call assert_equal([1, 1], [line('.'), col('.')])
1581
1582 normal j
1583 call search('^$', 'c')
1584 call assert_equal([2, 1], [line('.'), col('.')])
1585
1586 call search('^$', 'bc')
1587 call assert_equal([2, 1], [line('.'), col('.')])
1588
1589 exe "normal /two\<CR>"
1590 call search('.', 'c')
1591 call assert_equal([3, 5], [line('.'), col('.')])
1592
1593 close!
1594endfunc
Bram Moolenaardb294ad2019-06-06 12:49:29 +02001595
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001596" Test for error cases with the search() function
1597func Test_search_errors()
1598 call assert_fails("call search('pat', [])", 'E730:')
1599 call assert_fails("call search('pat', 'b', {})", 'E728:')
1600 call assert_fails("call search('pat', 'b', 1, [])", 'E745:')
1601 call assert_fails("call search('pat', 'ns')", 'E475:')
1602 call assert_fails("call search('pat', 'mr')", 'E475:')
Bram Moolenaar8832a342020-04-11 18:36:38 +02001603
1604 new
1605 call setline(1, ['foo', 'bar'])
1606 call assert_fails('call feedkeys("/foo/;/bar/;\<CR>", "tx")', 'E386:')
1607 bwipe!
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001608endfunc
1609
Paul Ollis65745772022-06-05 16:55:54 +01001610func Test_search_timeout()
Bram Moolenaar73171ba2022-06-20 12:39:39 +01001611 let g:test_is_flaky = 1
Paul Ollis65745772022-06-05 16:55:54 +01001612 new
Bram Moolenaar616592e2022-06-17 15:17:10 +01001613 " use a complicated pattern that should be slow with the BT engine
Paul Ollis65745772022-06-05 16:55:54 +01001614 let pattern = '\%#=1a*.*X\@<=b*'
Bram Moolenaar616592e2022-06-17 15:17:10 +01001615
1616 " use a timeout of 50 msec
1617 let search_timeout = 0.05
1618
1619 " fill the buffer so that it takes 15 times the timeout to search
Paul Ollis65745772022-06-05 16:55:54 +01001620 let slow_target_timeout = search_timeout * 15.0
1621
Bram Moolenaar616592e2022-06-17 15:17:10 +01001622 " Fill the buffer with more and more text until searching takes more time
1623 " than slow_target_timeout.
Paul Ollis65745772022-06-05 16:55:54 +01001624 for n in range(40, 400, 30)
1625 call setline(1, ['aaa', repeat('abc ', n), 'ccc'])
1626 let start = reltime()
1627 call search(pattern, '', 0)
1628 let elapsed = reltimefloat(reltime(start))
1629 if elapsed > slow_target_timeout
1630 break
1631 endif
1632 endfor
1633 call assert_true(elapsed > slow_target_timeout)
1634
Bram Moolenaar616592e2022-06-17 15:17:10 +01001635 " Check that the timeout kicks in, the time should be less than half of what
1636 " we measured without the timeout. This is permissive, because the timer is
1637 " known to overrun, especially when using valgrind.
Paul Ollis65745772022-06-05 16:55:54 +01001638 let max_time = elapsed / 2.0
1639 let start = reltime()
1640 call search(pattern, '', 0, float2nr(search_timeout * 1000))
1641 let elapsed = reltimefloat(reltime(start))
Bram Moolenaar616592e2022-06-17 15:17:10 +01001642 call assert_inrange(search_timeout * 0.9, max_time, elapsed)
Paul Ollis65745772022-06-05 16:55:54 +01001643
1644 bwipe!
1645endfunc
1646
Bram Moolenaardb294ad2019-06-06 12:49:29 +02001647func Test_search_display_pattern()
1648 new
1649 call setline(1, ['foo', 'bar', 'foobar'])
1650
1651 call cursor(1, 1)
1652 let @/ = 'foo'
Bram Moolenaara4208962019-08-24 20:50:19 +02001653 let pat = @/->escape('()*?'. '\s\+')
Bram Moolenaardb294ad2019-06-06 12:49:29 +02001654 let g:a = execute(':unsilent :norm! n')
1655 call assert_match(pat, g:a)
1656
1657 " right-left
1658 if exists("+rightleft")
1659 set rl
1660 call cursor(1, 1)
1661 let @/ = 'foo'
1662 let pat = 'oof/\s\+'
1663 let g:a = execute(':unsilent :norm! n')
1664 call assert_match(pat, g:a)
1665 set norl
1666 endif
1667endfunc
Bram Moolenaar196b4662019-09-06 21:34:30 +02001668
1669func Test_searchdecl()
1670 let lines =<< trim END
1671 int global;
1672
1673 func()
1674 {
1675 int global;
1676 if (cond) {
1677 int local;
1678 }
1679 int local;
1680 // comment
1681 }
1682 END
1683 new
1684 call setline(1, lines)
1685 10
1686 call assert_equal(0, searchdecl('local', 0, 0))
1687 call assert_equal(7, getcurpos()[1])
1688
1689 10
1690 call assert_equal(0, 'local'->searchdecl(0, 1))
1691 call assert_equal(9, getcurpos()[1])
1692
1693 10
1694 call assert_equal(0, searchdecl('global'))
1695 call assert_equal(5, getcurpos()[1])
1696
1697 10
1698 call assert_equal(0, searchdecl('global', 1))
1699 call assert_equal(1, getcurpos()[1])
1700
1701 bwipe!
1702endfunc
Bram Moolenaar98a336d2020-01-20 20:22:30 +01001703
1704func Test_search_special()
Bram Moolenaarfe4bbac2020-01-20 21:12:20 +01001705 " this was causing illegal memory access and an endless loop
1706 set t_PE=
Bram Moolenaar98a336d2020-01-20 20:22:30 +01001707 exe "norm /\x80PS"
1708endfunc
Bram Moolenaar07ada5f2020-02-05 20:38:22 +01001709
1710" Test for command failures when the last search pattern is not set.
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001711" Need to run this in a new vim instance where last search pattern is not set.
Bram Moolenaar07ada5f2020-02-05 20:38:22 +01001712func Test_search_with_no_last_pat()
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001713 let lines =<< trim [SCRIPT]
1714 call assert_fails("normal i\<C-R>/\e", 'E35:')
1715 call assert_fails("exe '/'", 'E35:')
1716 call assert_fails("exe '?'", 'E35:')
1717 call assert_fails("/", 'E35:')
1718 call assert_fails("?", 'E35:')
1719 call assert_fails("normal n", 'E35:')
1720 call assert_fails("normal N", 'E35:')
1721 call assert_fails("normal gn", 'E35:')
1722 call assert_fails("normal gN", 'E35:')
1723 call assert_fails("normal cgn", 'E35:')
1724 call assert_fails("normal cgN", 'E35:')
1725 let p = []
1726 let p = @/
1727 call assert_equal('', p)
1728 call assert_fails("normal :\<C-R>/", 'E35:')
1729 call assert_fails("//p", 'E35:')
1730 call assert_fails(";//p", 'E35:')
1731 call assert_fails("??p", 'E35:')
1732 call assert_fails(";??p", 'E35:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001733 call assert_fails('g//p', ['E35:', 'E476:'])
1734 call assert_fails('v//p', ['E35:', 'E476:'])
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001735 call writefile(v:errors, 'Xresult')
1736 qall!
1737 [SCRIPT]
1738 call writefile(lines, 'Xscript')
1739
1740 if RunVim([], [], '--clean -S Xscript')
1741 call assert_equal([], readfile('Xresult'))
1742 endif
1743 call delete('Xscript')
1744 call delete('Xresult')
Bram Moolenaar07ada5f2020-02-05 20:38:22 +01001745endfunc
1746
1747" Test for using tilde (~) atom in search. This should use the last used
1748" substitute pattern
1749func Test_search_tilde_pat()
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001750 let lines =<< trim [SCRIPT]
1751 set regexpengine=1
1752 call assert_fails('exe "normal /~\<CR>"', 'E33:')
1753 call assert_fails('exe "normal ?~\<CR>"', 'E33:')
1754 set regexpengine=2
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001755 call assert_fails('exe "normal /~\<CR>"', ['E33:', 'E383:'])
1756 call assert_fails('exe "normal ?~\<CR>"', ['E33:', 'E383:'])
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001757 set regexpengine&
1758 call writefile(v:errors, 'Xresult')
1759 qall!
1760 [SCRIPT]
1761 call writefile(lines, 'Xscript')
1762 if RunVim([], [], '--clean -S Xscript')
1763 call assert_equal([], readfile('Xresult'))
1764 endif
1765 call delete('Xscript')
1766 call delete('Xresult')
Bram Moolenaar07ada5f2020-02-05 20:38:22 +01001767endfunc
1768
Bram Moolenaar406cd902020-02-18 21:54:41 +01001769" Test for searching a pattern that is not present with 'nowrapscan'
1770func Test_search_pat_not_found()
1771 new
1772 set nowrapscan
1773 let @/ = '1abcxyz2'
1774 call assert_fails('normal n', 'E385:')
1775 call assert_fails('normal N', 'E384:')
1776 set wrapscan&
1777 close
1778endfunc
1779
1780" Test for v:searchforward variable
1781func Test_searchforward_var()
1782 new
1783 call setline(1, ['foo', '', 'foo'])
1784 call cursor(2, 1)
1785 let @/ = 'foo'
1786 let v:searchforward = 0
1787 normal N
1788 call assert_equal(3, line('.'))
1789 call cursor(2, 1)
1790 let v:searchforward = 1
1791 normal N
1792 call assert_equal(1, line('.'))
1793 close!
1794endfunc
1795
Bram Moolenaar476a6132020-04-08 19:48:56 +02001796" Test for invalid regular expressions
1797func Test_invalid_regexp()
1798 set regexpengine=1
1799 call assert_fails("call search(repeat('\\(.\\)', 10))", 'E51:')
Bram Moolenaar4d23c522020-04-09 18:42:11 +02001800 call assert_fails("call search('\\%(')", 'E53:')
1801 call assert_fails("call search('\\(')", 'E54:')
1802 call assert_fails("call search('\\)')", 'E55:')
Bram Moolenaar476a6132020-04-08 19:48:56 +02001803 call assert_fails("call search('x\\@#')", 'E59:')
Bram Moolenaar4d23c522020-04-09 18:42:11 +02001804 call assert_fails('call search(''\v%(%(%(%(%(%(%(%(%(%(%(a){1}){1}){1}){1}){1}){1}){1}){1}){1}){1}){1}'')', 'E60:')
1805 call assert_fails("call search('a\\+*')", 'E61:')
Bram Moolenaar476a6132020-04-08 19:48:56 +02001806 call assert_fails("call search('\\_m')", 'E63:')
1807 call assert_fails("call search('\\+')", 'E64:')
1808 call assert_fails("call search('\\1')", 'E65:')
1809 call assert_fails("call search('\\z\\(\\)')", 'E66:')
1810 call assert_fails("call search('\\z2')", 'E67:')
1811 call assert_fails("call search('\\zx')", 'E68:')
1812 call assert_fails("call search('\\%[ab')", 'E69:')
1813 call assert_fails("call search('\\%[]')", 'E70:')
1814 call assert_fails("call search('\\%a')", 'E71:')
1815 call assert_fails("call search('ab\\%[\\(cd\\)]')", 'E369:')
1816 call assert_fails("call search('ab\\%[\\%(cd\\)]')", 'E369:')
1817 set regexpengine=2
1818 call assert_fails("call search('\\_')", 'E865:')
1819 call assert_fails("call search('\\+')", 'E866:')
1820 call assert_fails("call search('\\zx')", 'E867:')
1821 call assert_fails("call search('\\%a')", 'E867:')
1822 call assert_fails("call search('x\\@#')", 'E869:')
1823 call assert_fails("call search(repeat('\\(.\\)', 10))", 'E872:')
1824 call assert_fails("call search('\\_m')", 'E877:')
1825 call assert_fails("call search('\\%(')", 'E53:')
1826 call assert_fails("call search('\\(')", 'E54:')
1827 call assert_fails("call search('\\)')", 'E55:')
1828 call assert_fails("call search('\\z\\(\\)')", 'E66:')
Dominique Pelle8bfa0eb2022-01-02 16:16:33 +00001829 call assert_fails("call search('\\z2')", 'E67:')
1830 call assert_fails("call search('\\zx')", 'E867:')
Bram Moolenaar476a6132020-04-08 19:48:56 +02001831 call assert_fails("call search('\\%[ab')", 'E69:')
Bram Moolenaar4d23c522020-04-09 18:42:11 +02001832 call assert_fails("call search('\\%[]')", 'E70:')
Bram Moolenaar476a6132020-04-08 19:48:56 +02001833 call assert_fails("call search('\\%9999999999999999999999999999v')", 'E951:')
1834 set regexpengine&
1835 call assert_fails("call search('\\%#=3ab')", 'E864:')
1836endfunc
1837
Bram Moolenaar004a6782020-04-11 17:09:31 +02001838" Test for searching a very complex pattern in a string. Should switch the
1839" regexp engine from NFA to the old engine.
1840func Test_regexp_switch_engine()
1841 let l = readfile('samples/re.freeze.txt')
1842 let v = substitute(l[4], '..\@<!', '', '')
1843 call assert_equal(l[4], v)
1844endfunc
1845
1846" Test for the \%V atom to search within visually selected text
1847func Test_search_in_visual_area()
1848 new
1849 call setline(1, ['foo bar1', 'foo bar2', 'foo bar3', 'foo bar4'])
1850 exe "normal 2GVjo/\\%Vbar\<CR>\<Esc>"
1851 call assert_equal([2, 5], [line('.'), col('.')])
1852 exe "normal 2GVj$?\\%Vbar\<CR>\<Esc>"
1853 call assert_equal([3, 5], [line('.'), col('.')])
1854 close!
1855endfunc
1856
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001857" Test for searching with 'smartcase' and 'ignorecase'
1858func Test_search_smartcase()
1859 new
1860 call setline(1, ['', 'Hello'])
1861 set noignorecase nosmartcase
1862 call assert_fails('exe "normal /\\a\\_.\\(.*\\)O\<CR>"', 'E486:')
1863
1864 set ignorecase nosmartcase
1865 exe "normal /\\a\\_.\\(.*\\)O\<CR>"
1866 call assert_equal([2, 1], [line('.'), col('.')])
1867
1868 call cursor(1, 1)
1869 set ignorecase smartcase
1870 call assert_fails('exe "normal /\\a\\_.\\(.*\\)O\<CR>"', 'E486:')
1871
1872 exe "normal /\\a\\_.\\(.*\\)o\<CR>"
1873 call assert_equal([2, 1], [line('.'), col('.')])
1874
Bram Moolenaar224a5f12020-04-28 20:29:07 +02001875 " Test for using special atoms with 'smartcase'
1876 call setline(1, ['', ' Hello\ '])
1877 call cursor(1, 1)
1878 call feedkeys('/\_.\%(\uello\)\' .. "\<CR>", 'xt')
1879 call assert_equal([2, 4], [line('.'), col('.')])
1880
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001881 set ignorecase& smartcase&
1882 close!
Bram Moolenaard66cdcd2020-07-26 13:27:16 +02001883endfun
1884
1885" Test 'smartcase' with utf-8.
1886func Test_search_smartcase_utf8()
1887 new
1888 let save_enc = &encoding
1889 set encoding=utf8 ignorecase smartcase
1890
1891 call setline(1, 'Café cafÉ')
1892 1s/café/x/g
1893 call assert_equal('x x', getline(1))
1894
1895 call setline(1, 'Café cafÉ')
1896 1s/cafÉ/x/g
1897 call assert_equal('Café x', getline(1))
1898
1899 set ignorecase& smartcase&
1900 let &encoding = save_enc
1901 close!
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001902endfunc
1903
1904" Test searching past the end of a file
1905func Test_search_past_eof()
1906 new
1907 call setline(1, ['Line'])
1908 exe "normal /\\n\\zs\<CR>"
1909 call assert_equal([1, 4], [line('.'), col('.')])
1910 close!
1911endfunc
1912
Bram Moolenaar224a5f12020-04-28 20:29:07 +02001913" Test for various search offsets
1914func Test_search_offset()
1915 " With /e, for a match in the first column of a line, the cursor should be
1916 " placed at the end of the previous line.
1917 new
1918 call setline(1, ['one two', 'three four'])
1919 call search('two\_.', 'e')
1920 call assert_equal([1, 7], [line('.'), col('.')])
1921
1922 " with cursor at the beginning of the file, use /s+1
1923 call cursor(1, 1)
1924 exe "normal /two/s+1\<CR>"
1925 call assert_equal([1, 6], [line('.'), col('.')])
1926
1927 " with cursor at the end of the file, use /e-1
1928 call cursor(2, 10)
1929 exe "normal ?three?e-1\<CR>"
1930 call assert_equal([2, 4], [line('.'), col('.')])
1931
1932 " line offset - after the last line
1933 call cursor(1, 1)
1934 exe "normal /three/+1\<CR>"
1935 call assert_equal([2, 1], [line('.'), col('.')])
1936
1937 " line offset - before the first line
1938 call cursor(2, 1)
1939 exe "normal ?one?-1\<CR>"
1940 call assert_equal([1, 1], [line('.'), col('.')])
1941
1942 " character offset - before the first character in the file
1943 call cursor(2, 1)
1944 exe "normal ?one?s-1\<CR>"
1945 call assert_equal([1, 1], [line('.'), col('.')])
1946 call cursor(2, 1)
1947 exe "normal ?one?e-3\<CR>"
1948 call assert_equal([1, 1], [line('.'), col('.')])
1949
1950 " character offset - after the last character in the file
1951 call cursor(1, 1)
1952 exe "normal /four/s+4\<CR>"
1953 call assert_equal([2, 10], [line('.'), col('.')])
1954 call cursor(1, 1)
1955 exe "normal /four/e+1\<CR>"
1956 call assert_equal([2, 10], [line('.'), col('.')])
1957
1958 close!
1959endfunc
1960
1961" Test for searching for matching parenthesis using %
1962func Test_search_match_paren()
1963 new
1964 call setline(1, "abc(def')'ghi'('jk'\\t'lm)no")
1965 " searching for a matching parenthesis should skip single quoted characters
1966 call cursor(1, 4)
1967 normal %
1968 call assert_equal([1, 25], [line('.'), col('.')])
1969 normal %
1970 call assert_equal([1, 4], [line('.'), col('.')])
1971 call cursor(1, 5)
1972 normal ])
1973 call assert_equal([1, 25], [line('.'), col('.')])
1974 call cursor(1, 24)
1975 normal [(
1976 call assert_equal([1, 4], [line('.'), col('.')])
1977
1978 " matching parenthesis in 'virtualedit' mode with cursor after the eol
1979 call setline(1, 'abc(defgh)')
1980 set virtualedit=all
1981 normal 20|%
1982 call assert_equal(4, col('.'))
1983 set virtualedit&
1984 close!
1985endfunc
1986
1987" Test for searching a pattern and stopping before a specified line
1988func Test_search_stopline()
1989 new
1990 call setline(1, ['', '', '', 'vim'])
1991 call assert_equal(0, search('vim', 'n', 3))
1992 call assert_equal(4, search('vim', 'n', 4))
1993 call setline(1, ['vim', '', '', ''])
1994 call cursor(4, 1)
1995 call assert_equal(0, search('vim', 'bn', 2))
1996 call assert_equal(1, search('vim', 'bn', 1))
1997 close!
1998endfunc
1999
Bram Moolenaar6bed0db2020-11-25 17:41:20 +01002000func Test_incsearch_highlighting_newline()
Bram Moolenaar448465e2020-11-25 13:49:27 +01002001 CheckRunVimInTerminal
2002 CheckOption incsearch
2003 CheckScreendump
2004 new
2005 call test_override("char_avail", 1)
2006
2007 let commands =<< trim [CODE]
2008 set incsearch nohls
2009 call setline(1, ['test', 'xxx'])
2010 [CODE]
2011 call writefile(commands, 'Xincsearch_nl')
2012 let buf = RunVimInTerminal('-S Xincsearch_nl', {'rows': 5, 'cols': 10})
Bram Moolenaar448465e2020-11-25 13:49:27 +01002013 call term_sendkeys(buf, '/test')
Bram Moolenaar448465e2020-11-25 13:49:27 +01002014 call VerifyScreenDump(buf, 'Test_incsearch_newline1', {})
Bram Moolenaar6bed0db2020-11-25 17:41:20 +01002015 " Need to send one key at a time to force a redraw
Bram Moolenaar448465e2020-11-25 13:49:27 +01002016 call term_sendkeys(buf, '\n')
Bram Moolenaar448465e2020-11-25 13:49:27 +01002017 call VerifyScreenDump(buf, 'Test_incsearch_newline2', {})
2018 call term_sendkeys(buf, 'x')
Bram Moolenaar448465e2020-11-25 13:49:27 +01002019 call VerifyScreenDump(buf, 'Test_incsearch_newline3', {})
2020 call term_sendkeys(buf, 'x')
2021 call VerifyScreenDump(buf, 'Test_incsearch_newline4', {})
2022 call term_sendkeys(buf, "\<CR>")
Bram Moolenaar448465e2020-11-25 13:49:27 +01002023 call VerifyScreenDump(buf, 'Test_incsearch_newline5', {})
2024 call StopVimInTerminal(buf)
2025
2026 " clean up
2027 call delete('Xincsearch_nl')
2028 call test_override("char_avail", 0)
2029 bw
2030endfunc
2031
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01002032func Test_incsearch_substitute_dump2()
2033 CheckOption incsearch
2034 CheckScreendump
2035
2036 call writefile([
2037 \ 'set incsearch hlsearch scrolloff=0',
2038 \ 'for n in range(1, 4)',
2039 \ ' call setline(n, "foo " . n)',
2040 \ 'endfor',
2041 \ 'call setline(5, "abc|def")',
2042 \ '3',
2043 \ ], 'Xis_subst_script2')
2044 let buf = RunVimInTerminal('-S Xis_subst_script2', {'rows': 9, 'cols': 70})
2045
2046 call term_sendkeys(buf, ':%s/\vabc|')
2047 sleep 100m
2048 call VerifyScreenDump(buf, 'Test_incsearch_sub_01', {})
2049 call term_sendkeys(buf, "\<Esc>")
2050
2051 " The following should not be highlighted
2052 call term_sendkeys(buf, ':1,5s/\v|')
2053 sleep 100m
2054 call VerifyScreenDump(buf, 'Test_incsearch_sub_02', {})
2055
2056
2057 call StopVimInTerminal(buf)
2058 call delete('Xis_subst_script2')
2059endfunc
2060
Christian Brabandt78ba9332021-08-01 12:44:37 +02002061func Test_pattern_is_uppercase_smartcase()
2062 new
2063 let input=['abc', 'ABC', 'Abc', 'abC']
2064 call setline(1, input)
2065 call cursor(1,1)
2066 " default, matches firstline
2067 %s/abc//g
2068 call assert_equal(['', 'ABC', 'Abc', 'abC'],
2069 \ getline(1, '$'))
2070
2071 set smartcase ignorecase
2072 sil %d
2073 call setline(1, input)
2074 call cursor(1,1)
2075 " with smartcase and incsearch set, matches everything
2076 %s/abc//g
2077 call assert_equal(['', '', '', ''], getline(1, '$'))
2078
2079 sil %d
2080 call setline(1, input)
2081 call cursor(1,1)
2082 " with smartcase and incsearch set and found an uppercase letter,
2083 " match only that.
2084 %s/abC//g
2085 call assert_equal(['abc', 'ABC', 'Abc', ''],
2086 \ getline(1, '$'))
2087
2088 sil %d
2089 call setline(1, input)
2090 call cursor(1,1)
2091 exe "norm! vG$\<esc>"
2092 " \%V should not be detected as uppercase letter
2093 %s/\%Vabc//g
2094 call assert_equal(['', '', '', ''], getline(1, '$'))
2095
2096 call setline(1, input)
2097 call cursor(1,1)
2098 exe "norm! vG$\<esc>"
2099 " \v%V should not be detected as uppercase letter
2100 %s/\v%Vabc//g
2101 call assert_equal(['', '', '', ''], getline(1, '$'))
2102
2103 call setline(1, input)
2104 call cursor(1,1)
2105 exe "norm! vG$\<esc>"
2106 " \v%VabC should be detected as uppercase letter
2107 %s/\v%VabC//g
2108 call assert_equal(['abc', 'ABC', 'Abc', ''],
2109 \ getline(1, '$'))
2110
Christian Brabandtbc67e5a2021-08-05 15:24:59 +02002111 call setline(1, input)
2112 call cursor(1,1)
2113 " \Vabc should match everything
2114 %s/\Vabc//g
2115 call assert_equal(['', '', '', ''], getline(1, '$'))
2116
2117 call setline(1, input + ['_abc'])
2118 " _ matches normally
2119 %s/\v_.*//g
2120 call assert_equal(['abc', 'ABC', 'Abc', 'abC', ''], getline(1, '$'))
2121
Christian Brabandt78ba9332021-08-01 12:44:37 +02002122 set smartcase& ignorecase&
2123 bw!
2124endfunc
2125
Bram Moolenaard8d957d2021-10-04 19:47:35 +01002126func Test_no_last_search_pattern()
2127 CheckOption incsearch
2128
2129 let @/ = ""
2130 set incsearch
Bram Moolenaar9af9fd62021-10-04 20:09:19 +01002131 " these were causing a crash
2132 call feedkeys("//\<C-G>", 'xt')
2133 call feedkeys("//\<C-T>", 'xt')
2134 call feedkeys("??\<C-G>", 'xt')
2135 call feedkeys("??\<C-T>", 'xt')
Bram Moolenaard8d957d2021-10-04 19:47:35 +01002136endfunc
2137
Bram Moolenaar35a319b2021-10-09 13:58:55 +01002138func Test_search_with_invalid_range()
2139 new
2140 let lines =<< trim END
2141 /\%.v
2142 5/
2143 c
2144 END
2145 call writefile(lines, 'Xrangesearch')
2146 source Xrangesearch
2147
2148 bwipe!
2149 call delete('Xrangesearch')
2150endfunc
2151
Bram Moolenaard8d957d2021-10-04 19:47:35 +01002152
Bram Moolenaar07ada5f2020-02-05 20:38:22 +01002153" vim: shiftwidth=2 sts=2 expandtab