blob: 67d29c5d46b6b01b96b40dc7f810df190a043a36 [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
336
337 func Waitabit()
338 sleep 20m
339 return 1 " skip match
340 endfunc
341
342 new
343 call setline(1, range(100))
344 call setline(1, "(start here")
345 call setline(100, "end here)")
346 let starttime = reltime()
347
348 " A timeout of 100 msec should happen after about five times of 20 msec wait
349 " in Waitabit(). When the timeout applies to each search the elapsed time
350 " will be much longer.
351 call assert_equal(0, searchpair('(', '\d', ')', '', "Waitabit()", 0, 100))
352 let elapsed = reltime(starttime)->reltimefloat()
353 call assert_inrange(0.09, 0.300, elapsed)
354
355 bwipe!
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100356endfunc
357
Bram Moolenaare5303952022-06-19 17:05:47 +0100358func SearchpairSkip()
359 let id = synID(line('.'), col('.'), 0)
360 let attr = synIDattr(id, 'name')
361 return attr !~ 'comment'
362endfunc
363
364func Test_searchpair_timeout_with_skip()
365 let g:test_is_flaky = 1
366
367 edit ../evalfunc.c
368 if has('win32')
369 " Windows timeouts are rather coarse grained, about 16ms.
370 let ms = 20
371 let min_time = 0.016
372 let max_time = min_time * 10.0
373 else
374 let ms = 1
375 let min_time = 0.001
376 let max_time = min_time * 10.0
Bram Moolenaare366ed42022-06-19 20:13:56 +0100377 if RunningWithValgrind()
Bram Moolenaare5303952022-06-19 17:05:47 +0100378 let max_time += 0.04 " this can be slow with valgrind
379 endif
380 endif
381 let start = reltime()
382 let found = searchpair('(', '', ')', 'crnm', 'SearchpairSkip()', 0, ms)
383 let elapsed = reltimefloat(reltime(start))
384 call assert_inrange(min_time, max_time, elapsed)
385
386 bwipe!
387endfunc
388
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100389func Test_searchpairpos()
390 new
391 call setline(1, ['other code', 'here [', ' [', ' " cursor here', ' ]]'])
392
393 4
394 call assert_equal([3, 2], searchpairpos('\[', '', ']', 'bW'))
395 call assert_equal([0, 3, 2, 0], getpos('.'))
396 4
397 call assert_equal([2, 6], searchpairpos('\[', '', ']', 'bWr'))
398 call assert_equal([0, 2, 6, 0], getpos('.'))
399 4|norm ^
400 call assert_equal([5, 2], searchpairpos('\[', '', ']', 'Wn'))
401 call assert_equal([0, 4, 2, 0], getpos('.'))
402 4
403 call assert_equal([2, 6], searchpairpos('\[', '', ']', 'bW',
404 \ 'getline(".") =~ "^\\s*\["'))
405 call assert_equal([0, 2, 6, 0], getpos('.'))
406 4
407 call assert_equal([2, 6], searchpairpos('\[', '', ']', 'bWr'))
408 call assert_equal([0, 2, 6, 0], getpos('.'))
409 set nomagic
410 4
411 call assert_equal([3, 2], searchpairpos('\[', '', ']', 'bW'))
412 call assert_equal([0, 3, 2, 0], getpos('.'))
413 set magic
414 4|norm ^
415 call assert_equal([0, 0], searchpairpos('{', '', '}', 'bW'))
416 call assert_equal([0, 4, 2, 0], getpos('.'))
417
418 %d
419 call setline(1, ['if 1', ' if 2', ' else', ' endif 2', 'endif 1'])
420 /\<if 1
421 call assert_equal([5, 1], searchpairpos('\<if\>', '\<else\>', '\<endif\>', 'W'))
422 call assert_equal([0, 5, 1, 0], getpos('.'))
423 /\<if 2
424 call assert_equal([3, 3], searchpairpos('\<if\>', '\<else\>', '\<endif\>', 'W'))
425 call assert_equal([0, 3, 3, 0], getpos('.'))
426
Bram Moolenaar6e450a52017-01-06 20:03:58 +0100427 q!
428endfunc
429
Bram Moolenaar3dddb092018-06-24 19:01:59 +0200430func Test_searchpair_errors()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200431 call assert_fails("call searchpair([0], 'middle', 'end', 'bW', 'skip', 99, 100)", 'E730: Using a List as a String')
432 call assert_fails("call searchpair('start', {-> 0}, 'end', 'bW', 'skip', 99, 100)", 'E729: Using a Funcref as a String')
433 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 +0200434 call assert_fails("call searchpair('start', 'middle', 'end', 'flags', 'skip', 99, 100)", 'E475: Invalid argument: flags')
Bram Moolenaar3dddb092018-06-24 19:01:59 +0200435 call assert_fails("call searchpair('start', 'middle', 'end', 'bW', 'func', -99, 100)", 'E475: Invalid argument: -99')
436 call assert_fails("call searchpair('start', 'middle', 'end', 'bW', 'func', 99, -100)", 'E475: Invalid argument: -100')
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100437 call assert_fails("call searchpair('start', 'middle', 'end', 'e')", 'E475: Invalid argument: e')
438 call assert_fails("call searchpair('start', 'middle', 'end', 'sn')", 'E475: Invalid argument: sn')
439endfunc
440
441func Test_searchpairpos_errors()
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200442 call assert_fails("call searchpairpos([0], 'middle', 'end', 'bW', 'skip', 99, 100)", 'E730: Using a List as a String')
443 call assert_fails("call searchpairpos('start', {-> 0}, 'end', 'bW', 'skip', 99, 100)", 'E729: Using a Funcref as a String')
444 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 +0100445 call assert_fails("call searchpairpos('start', 'middle', 'end', 'flags', 'skip', 99, 100)", 'E475: Invalid argument: flags')
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100446 call assert_fails("call searchpairpos('start', 'middle', 'end', 'bW', 'func', -99, 100)", 'E475: Invalid argument: -99')
447 call assert_fails("call searchpairpos('start', 'middle', 'end', 'bW', 'func', 99, -100)", 'E475: Invalid argument: -100')
448 call assert_fails("call searchpairpos('start', 'middle', 'end', 'e')", 'E475: Invalid argument: e')
449 call assert_fails("call searchpairpos('start', 'middle', 'end', 'sn')", 'E475: Invalid argument: sn')
Bram Moolenaar3dddb092018-06-24 19:01:59 +0200450endfunc
451
Bram Moolenaar48570482017-10-30 21:48:41 +0100452func Test_searchpair_skip()
453 func Zero()
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100454 return 0
Bram Moolenaar48570482017-10-30 21:48:41 +0100455 endfunc
456 func Partial(x)
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100457 return a:x
Bram Moolenaar48570482017-10-30 21:48:41 +0100458 endfunc
459 new
460 call setline(1, ['{', 'foo', 'foo', 'foo', '}'])
461 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', ''))
462 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', '0'))
463 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', {-> 0}))
464 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', function('Zero')))
465 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', function('Partial', [0])))
Bram Moolenaar48570482017-10-30 21:48:41 +0100466 bw!
467endfunc
468
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200469func Test_searchpair_leak()
470 new
471 call setline(1, 'if one else another endif')
472
473 " The error in the skip expression caused memory to leak.
474 call assert_fails("call searchpair('\\<if\\>', '\\<else\\>', '\\<endif\\>', '', '\"foo\" 2')", 'E15:')
475
476 bwipe!
477endfunc
478
Bram Moolenaar66727e12017-03-01 22:17:05 +0100479func Test_searchc()
480 " These commands used to cause memory overflow in searchc().
481 new
482 norm ixx
483 exe "norm 0t\u93cf"
484 bw!
485endfunc
Bram Moolenaara693d052017-06-29 22:23:06 +0200486
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200487func Cmdline3_prep()
488 " need to disable char_avail,
489 " so that expansion of commandline works
490 call test_override("char_avail", 1)
491 new
492 call setline(1, [' 1', ' 2 the~e', ' 3 the theother'])
493 set incsearch
494endfunc
495
Bram Moolenaar976b8472018-08-12 15:49:47 +0200496func Incsearch_cleanup()
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200497 set noincsearch
498 call test_override("char_avail", 0)
499 bw!
500endfunc
501
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200502func Test_search_cmdline3()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100503 CheckOption incsearch
504
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200505 call Cmdline3_prep()
506 1
507 " first match
508 call feedkeys("/the\<c-l>\<cr>", 'tx')
509 call assert_equal(' 2 the~e', getline('.'))
510
Bram Moolenaar976b8472018-08-12 15:49:47 +0200511 call Incsearch_cleanup()
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200512endfunc
513
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200514func Test_search_cmdline3s()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100515 CheckOption incsearch
516
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200517 call Cmdline3_prep()
518 1
519 call feedkeys(":%s/the\<c-l>/xxx\<cr>", 'tx')
520 call assert_equal(' 2 xxxe', getline('.'))
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200521 undo
522 call feedkeys(":%subs/the\<c-l>/xxx\<cr>", 'tx')
523 call assert_equal(' 2 xxxe', getline('.'))
524 undo
525 call feedkeys(":%substitute/the\<c-l>/xxx\<cr>", 'tx')
526 call assert_equal(' 2 xxxe', getline('.'))
Bram Moolenaar167ae422018-08-14 21:32:21 +0200527 undo
528 call feedkeys(":%smagic/the.e/xxx\<cr>", 'tx')
529 call assert_equal(' 2 xxx', getline('.'))
530 undo
Bram Moolenaare2e40752020-09-04 21:18:46 +0200531 call assert_fails(":%snomagic/the.e/xxx\<cr>", 'E486:')
Bram Moolenaar167ae422018-08-14 21:32:21 +0200532 "
533 call feedkeys(":%snomagic/the\\.e/xxx\<cr>", 'tx')
534 call assert_equal(' 2 xxx', getline('.'))
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200535
Bram Moolenaar976b8472018-08-12 15:49:47 +0200536 call Incsearch_cleanup()
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200537endfunc
538
539func Test_search_cmdline3g()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100540 CheckOption incsearch
541
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200542 call Cmdline3_prep()
543 1
544 call feedkeys(":g/the\<c-l>/d\<cr>", 'tx')
545 call assert_equal(' 3 the theother', getline(2))
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200546 undo
547 call feedkeys(":global/the\<c-l>/d\<cr>", 'tx')
548 call assert_equal(' 3 the theother', getline(2))
Bram Moolenaardef7b1d2018-08-13 22:54:35 +0200549 undo
550 call feedkeys(":g!/the\<c-l>/d\<cr>", 'tx')
551 call assert_equal(1, line('$'))
552 call assert_equal(' 2 the~e', getline(1))
553 undo
554 call feedkeys(":global!/the\<c-l>/d\<cr>", 'tx')
555 call assert_equal(1, line('$'))
556 call assert_equal(' 2 the~e', getline(1))
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200557
Bram Moolenaar976b8472018-08-12 15:49:47 +0200558 call Incsearch_cleanup()
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200559endfunc
560
561func Test_search_cmdline3v()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100562 CheckOption incsearch
563
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200564 call Cmdline3_prep()
565 1
566 call feedkeys(":v/the\<c-l>/d\<cr>", 'tx')
567 call assert_equal(1, line('$'))
568 call assert_equal(' 2 the~e', getline(1))
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200569 undo
570 call feedkeys(":vglobal/the\<c-l>/d\<cr>", 'tx')
571 call assert_equal(1, line('$'))
572 call assert_equal(' 2 the~e', getline(1))
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200573
Bram Moolenaar976b8472018-08-12 15:49:47 +0200574 call Incsearch_cleanup()
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200575endfunc
576
Bram Moolenaarda5116d2017-07-01 23:11:17 +0200577func Test_search_cmdline4()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100578 CheckOption incsearch
579
Bram Moolenaarda5116d2017-07-01 23:11:17 +0200580 " need to disable char_avail,
581 " so that expansion of commandline works
582 call test_override("char_avail", 1)
583 new
584 call setline(1, [' 1 the first', ' 2 the second', ' 3 the third'])
585 set incsearch
586 $
587 call feedkeys("?the\<c-g>\<cr>", 'tx')
588 call assert_equal(' 3 the third', getline('.'))
589 $
590 call feedkeys("?the\<c-g>\<c-g>\<cr>", 'tx')
591 call assert_equal(' 1 the first', getline('.'))
592 $
593 call feedkeys("?the\<c-g>\<c-g>\<c-g>\<cr>", 'tx')
594 call assert_equal(' 2 the second', getline('.'))
595 $
596 call feedkeys("?the\<c-t>\<cr>", 'tx')
597 call assert_equal(' 1 the first', getline('.'))
598 $
599 call feedkeys("?the\<c-t>\<c-t>\<cr>", 'tx')
600 call assert_equal(' 3 the third', getline('.'))
601 $
602 call feedkeys("?the\<c-t>\<c-t>\<c-t>\<cr>", 'tx')
603 call assert_equal(' 2 the second', getline('.'))
604 " clean up
605 set noincsearch
606 call test_override("char_avail", 0)
607 bw!
608endfunc
Bram Moolenaardb510072017-09-28 21:52:17 +0200609
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200610func Test_search_cmdline5()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100611 CheckOption incsearch
612
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200613 " Do not call test_override("char_avail", 1) so that <C-g> and <C-t> work
614 " regardless char_avail.
615 new
Bram Moolenaar69a5b862019-07-16 21:38:51 +0200616 call setline(1, [' 1 the first', ' 2 the second', ' 3 the third', ''])
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200617 set incsearch
618 1
619 call feedkeys("/the\<c-g>\<c-g>\<cr>", 'tx')
620 call assert_equal(' 3 the third', getline('.'))
621 $
622 call feedkeys("?the\<c-t>\<c-t>\<c-t>\<cr>", 'tx')
Bram Moolenaar69a5b862019-07-16 21:38:51 +0200623 call assert_equal(' 1 the first', getline('.'))
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200624 " clean up
625 set noincsearch
626 bw!
627endfunc
628
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100629func Test_search_cmdline6()
630 " Test that consecutive matches
631 " are caught by <c-g>/<c-t>
Bram Moolenaarb68df222020-03-19 15:05:28 +0100632 CheckOption incsearch
633
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100634 " need to disable char_avail,
635 " so that expansion of commandline works
636 call test_override("char_avail", 1)
637 new
638 call setline(1, [' bbvimb', ''])
639 set incsearch
640 " first match
641 norm! gg0
642 call feedkeys("/b\<cr>", 'tx')
643 call assert_equal([0,1,2,0], getpos('.'))
644 " second match
645 norm! gg0
646 call feedkeys("/b\<c-g>\<cr>", 'tx')
647 call assert_equal([0,1,3,0], getpos('.'))
648 " third match
649 norm! gg0
650 call feedkeys("/b\<c-g>\<c-g>\<cr>", 'tx')
651 call assert_equal([0,1,7,0], getpos('.'))
652 " first match again
653 norm! gg0
654 call feedkeys("/b\<c-g>\<c-g>\<c-g>\<cr>", 'tx')
655 call assert_equal([0,1,2,0], getpos('.'))
656 set nowrapscan
657 " last match
658 norm! gg0
659 call feedkeys("/b\<c-g>\<c-g>\<c-g>\<cr>", 'tx')
660 call assert_equal([0,1,7,0], getpos('.'))
661 " clean up
662 set wrapscan&vim
663 set noincsearch
664 call test_override("char_avail", 0)
665 bw!
666endfunc
667
668func Test_search_cmdline7()
Bram Moolenaard66cdcd2020-07-26 13:27:16 +0200669 " Test that pressing <c-g> in an empty command line
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100670 " does not move the cursor
Bram Moolenaarb68df222020-03-19 15:05:28 +0100671 CheckOption incsearch
672
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100673 " need to disable char_avail,
674 " so that expansion of commandline works
675 call test_override("char_avail", 1)
676 new
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200677 let @/ = 'b'
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100678 call setline(1, [' bbvimb', ''])
679 set incsearch
680 " first match
681 norm! gg0
682 " moves to next match of previous search pattern, just like /<cr>
683 call feedkeys("/\<c-g>\<cr>", 'tx')
684 call assert_equal([0,1,2,0], getpos('.'))
685 " moves to next match of previous search pattern, just like /<cr>
686 call feedkeys("/\<cr>", 'tx')
687 call assert_equal([0,1,3,0], getpos('.'))
688 " moves to next match of previous search pattern, just like /<cr>
689 call feedkeys("/\<c-t>\<cr>", 'tx')
690 call assert_equal([0,1,7,0], getpos('.'))
Bram Moolenaard0480092017-11-16 22:20:39 +0100691
692 " using an offset uses the last search pattern
693 call cursor(1, 1)
694 call setline(1, ['1 bbvimb', ' 2 bbvimb'])
695 let @/ = 'b'
696 call feedkeys("//e\<c-g>\<cr>", 'tx')
697 call assert_equal('1 bbvimb', getline('.'))
698 call assert_equal(4, col('.'))
699
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100700 set noincsearch
701 call test_override("char_avail", 0)
702 bw!
703endfunc
704
705func Test_search_cmdline8()
706 " Highlighting is cleared in all windows
707 " since hls applies to all windows
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200708 CheckOption incsearch
709 CheckFeature terminal
710 CheckNotGui
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100711 if has("win32")
712 throw "Skipped: Bug with sending <ESC> to terminal window not fixed yet"
713 endif
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200714
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100715 let h = winheight(0)
716 if h < 3
717 return
718 endif
719 " Prepare buffer text
720 let lines = ['abb vim vim vi', 'vimvivim']
721 call writefile(lines, 'Xsearch.txt')
Bram Moolenaar13deab82017-11-04 18:48:43 +0100722 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', 'Xsearch.txt'], {'term_rows': 3})
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100723
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200724 call WaitForAssert({-> assert_equal(lines, [term_getline(buf, 1), term_getline(buf, 2)])})
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100725
Bram Moolenaar13deab82017-11-04 18:48:43 +0100726 call term_sendkeys(buf, ":set incsearch hlsearch\<cr>")
727 call term_sendkeys(buf, ":14vsp\<cr>")
728 call term_sendkeys(buf, "/vim\<cr>")
729 call term_sendkeys(buf, "/b\<esc>")
730 call term_sendkeys(buf, "gg0")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200731 call TermWait(buf, 250)
Bram Moolenaar13deab82017-11-04 18:48:43 +0100732 let screen_line = term_scrape(buf, 1)
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100733 let [a0,a1,a2,a3] = [screen_line[3].attr, screen_line[4].attr,
734 \ screen_line[18].attr, screen_line[19].attr]
735 call assert_notequal(a0, a1)
736 call assert_notequal(a0, a3)
737 call assert_notequal(a1, a2)
738 call assert_equal(a0, a2)
739 call assert_equal(a1, a3)
740 " clean up
741 call delete('Xsearch.txt')
742
743 bwipe!
744endfunc
745
Bram Moolenaardb510072017-09-28 21:52:17 +0200746" Tests for regexp with various magic settings
Bram Moolenaar4d23c522020-04-09 18:42:11 +0200747func Run_search_regexp_magic_opt()
Bram Moolenaardb510072017-09-28 21:52:17 +0200748 put ='1 a aa abb abbccc'
749 exe 'normal! /a*b\{2}c\+/e' . "\<CR>"
750 call assert_equal([0, 2, 17, 0], getpos('.'))
751
752 put ='2 d dd dee deefff'
753 exe 'normal! /\Md\*e\{2}f\+/e' . "\<CR>"
754 call assert_equal([0, 3, 17, 0], getpos('.'))
755
756 set nomagic
757 put ='3 g gg ghh ghhiii'
758 exe 'normal! /g\*h\{2}i\+/e' . "\<CR>"
759 call assert_equal([0, 4, 17, 0], getpos('.'))
760
761 put ='4 j jj jkk jkklll'
762 exe 'normal! /\mj*k\{2}l\+/e' . "\<CR>"
763 call assert_equal([0, 5, 17, 0], getpos('.'))
764
765 put ='5 m mm mnn mnnooo'
766 exe 'normal! /\vm*n{2}o+/e' . "\<CR>"
767 call assert_equal([0, 6, 17, 0], getpos('.'))
768
769 put ='6 x ^aa$ x'
770 exe 'normal! /\V^aa$' . "\<CR>"
771 call assert_equal([0, 7, 5, 0], getpos('.'))
772
773 set magic
774 put ='7 (a)(b) abbaa'
775 exe 'normal! /\v(a)(b)\2\1\1/e' . "\<CR>"
776 call assert_equal([0, 8, 14, 0], getpos('.'))
777
778 put ='8 axx [ab]xx'
779 exe 'normal! /\V[ab]\(\[xy]\)\1' . "\<CR>"
780 call assert_equal([0, 9, 7, 0], getpos('.'))
781
Bram Moolenaar4d23c522020-04-09 18:42:11 +0200782 %d
783endfunc
784
785func Test_search_regexp()
786 enew!
787
788 set regexpengine=1
789 call Run_search_regexp_magic_opt()
790 set regexpengine=2
791 call Run_search_regexp_magic_opt()
792 set regexpengine&
793
Bram Moolenaardb510072017-09-28 21:52:17 +0200794 set undolevels=100
795 put ='9 foobar'
796 put =''
797 exe "normal! a\<C-G>u\<Esc>"
798 normal G
799 exe 'normal! dv?bar?' . "\<CR>"
800 call assert_equal('9 foo', getline('.'))
Bram Moolenaar4d23c522020-04-09 18:42:11 +0200801 call assert_equal([0, 2, 5, 0], getpos('.'))
802 call assert_equal(2, line('$'))
Bram Moolenaardb510072017-09-28 21:52:17 +0200803 normal u
804 call assert_equal('9 foobar', getline('.'))
Bram Moolenaar4d23c522020-04-09 18:42:11 +0200805 call assert_equal([0, 2, 6, 0], getpos('.'))
806 call assert_equal(3, line('$'))
Bram Moolenaardb510072017-09-28 21:52:17 +0200807
808 set undolevels&
809 enew!
810endfunc
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100811
812func Test_search_cmdline_incsearch_highlight()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100813 CheckOption incsearch
814
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100815 set incsearch hlsearch
816 " need to disable char_avail,
817 " so that expansion of commandline works
818 call test_override("char_avail", 1)
819 new
820 call setline(1, ['aaa 1 the first', ' 2 the second', ' 3 the third'])
821
822 1
823 call feedkeys("/second\<cr>", 'tx')
824 call assert_equal('second', @/)
825 call assert_equal(' 2 the second', getline('.'))
826
827 " Canceling search won't change @/
828 1
829 let @/ = 'last pattern'
830 call feedkeys("/third\<C-c>", 'tx')
831 call assert_equal('last pattern', @/)
832 call feedkeys("/third\<Esc>", 'tx')
833 call assert_equal('last pattern', @/)
834 call feedkeys("/3\<bs>\<bs>", 'tx')
835 call assert_equal('last pattern', @/)
836 call feedkeys("/third\<c-g>\<c-t>\<Esc>", 'tx')
837 call assert_equal('last pattern', @/)
838
839 " clean up
840 set noincsearch nohlsearch
841 bw!
842endfunc
843
844func Test_search_cmdline_incsearch_highlight_attr()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200845 CheckOption incsearch
846 CheckFeature terminal
847 CheckNotGui
848
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100849 let h = winheight(0)
850 if h < 3
851 return
852 endif
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100853
854 " Prepare buffer text
Bram Moolenaar13deab82017-11-04 18:48:43 +0100855 let lines = ['abb vim vim vi', 'vimvivim']
856 call writefile(lines, 'Xsearch.txt')
857 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', 'Xsearch.txt'], {'term_rows': 3})
858
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200859 call WaitForAssert({-> assert_equal(lines, [term_getline(buf, 1), term_getline(buf, 2)])})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100860 " wait for vim to complete initialization
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200861 call TermWait(buf)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100862
863 " Get attr of normal(a0), incsearch(a1), hlsearch(a2) highlight
Bram Moolenaar13deab82017-11-04 18:48:43 +0100864 call term_sendkeys(buf, ":set incsearch hlsearch\<cr>")
865 call term_sendkeys(buf, '/b')
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200866 call TermWait(buf, 100)
Bram Moolenaar13deab82017-11-04 18:48:43 +0100867 let screen_line1 = term_scrape(buf, 1)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100868 call assert_true(len(screen_line1) > 2)
869 " a0: attr_normal
870 let a0 = screen_line1[0].attr
871 " a1: attr_incsearch
872 let a1 = screen_line1[1].attr
873 " a2: attr_hlsearch
874 let a2 = screen_line1[2].attr
875 call assert_notequal(a0, a1)
876 call assert_notequal(a0, a2)
877 call assert_notequal(a1, a2)
Bram Moolenaar13deab82017-11-04 18:48:43 +0100878 call term_sendkeys(buf, "\<cr>gg0")
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100879
880 " Test incremental highlight search
Bram Moolenaar13deab82017-11-04 18:48:43 +0100881 call term_sendkeys(buf, "/vim")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200882 call TermWait(buf, 100)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100883 " Buffer:
884 " abb vim vim vi
885 " vimvivim
886 " Search: /vim
887 let attr_line1 = [a0,a0,a0,a0,a1,a1,a1,a0,a2,a2,a2,a0,a0,a0]
888 let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100889 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
890 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100891
892 " Test <C-g>
Bram Moolenaar13deab82017-11-04 18:48:43 +0100893 call term_sendkeys(buf, "\<C-g>\<C-g>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200894 call TermWait(buf, 100)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100895 let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a2,a2,a2,a0,a0,a0]
896 let attr_line2 = [a1,a1,a1,a0,a0,a2,a2,a2]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100897 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
898 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100899
900 " Test <C-t>
Bram Moolenaar13deab82017-11-04 18:48:43 +0100901 call term_sendkeys(buf, "\<C-t>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200902 call TermWait(buf, 100)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100903 let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a1,a1,a1,a0,a0,a0]
904 let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100905 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
906 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100907
908 " Type Enter and a1(incsearch highlight) should become a2(hlsearch highlight)
Bram Moolenaar13deab82017-11-04 18:48:43 +0100909 call term_sendkeys(buf, "\<cr>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200910 call TermWait(buf, 100)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100911 let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a2,a2,a2,a0,a0,a0]
912 let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100913 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
914 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100915
916 " Test nohlsearch. a2(hlsearch highlight) should become a0(normal highlight)
Bram Moolenaar13deab82017-11-04 18:48:43 +0100917 call term_sendkeys(buf, ":1\<cr>")
918 call term_sendkeys(buf, ":set nohlsearch\<cr>")
919 call term_sendkeys(buf, "/vim")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200920 call TermWait(buf, 100)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100921 let attr_line1 = [a0,a0,a0,a0,a1,a1,a1,a0,a0,a0,a0,a0,a0,a0]
922 let attr_line2 = [a0,a0,a0,a0,a0,a0,a0,a0]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100923 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
924 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100925 call delete('Xsearch.txt')
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100926
Bram Moolenaarb94340c2017-11-02 16:16:31 +0100927 call delete('Xsearch.txt')
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100928 bwipe!
929endfunc
Bram Moolenaarf45938c2017-11-02 15:59:57 +0100930
Bram Moolenaar548e5982018-12-26 21:45:00 +0100931func Test_incsearch_cmdline_modifier()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100932 CheckOption incsearch
933
Bram Moolenaar548e5982018-12-26 21:45:00 +0100934 call test_override("char_avail", 1)
935 new
936 call setline(1, ['foo'])
937 set incsearch
938 " Test that error E14 does not occur in parsing command modifier.
939 call feedkeys("V:tab", 'tx')
940
941 call Incsearch_cleanup()
942endfunc
943
Bram Moolenaar9d34d902018-04-27 22:18:12 +0200944func Test_incsearch_scrolling()
Bram Moolenaar494e9062020-05-31 21:28:02 +0200945 CheckRunVimInTerminal
Bram Moolenaar9d34d902018-04-27 22:18:12 +0200946 call assert_equal(0, &scrolloff)
947 call writefile([
948 \ 'let dots = repeat(".", 120)',
949 \ 'set incsearch cmdheight=2 scrolloff=0',
950 \ 'call setline(1, [dots, dots, dots, "", "target", dots, dots])',
951 \ 'normal gg',
952 \ 'redraw',
953 \ ], 'Xscript')
954 let buf = RunVimInTerminal('-S Xscript', {'rows': 9, 'cols': 70})
955 " Need to send one key at a time to force a redraw
956 call term_sendkeys(buf, '/')
957 sleep 100m
958 call term_sendkeys(buf, 't')
959 sleep 100m
960 call term_sendkeys(buf, 'a')
961 sleep 100m
962 call term_sendkeys(buf, 'r')
963 sleep 100m
964 call term_sendkeys(buf, 'g')
965 call VerifyScreenDump(buf, 'Test_incsearch_scrolling_01', {})
966
967 call term_sendkeys(buf, "\<Esc>")
968 call StopVimInTerminal(buf)
969 call delete('Xscript')
970endfunc
971
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200972func Test_incsearch_search_dump()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100973 CheckOption incsearch
974 CheckScreendump
975
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200976 call writefile([
977 \ 'set incsearch hlsearch scrolloff=0',
978 \ 'for n in range(1, 8)',
979 \ ' call setline(n, "foo " . n)',
980 \ 'endfor',
981 \ '3',
982 \ ], 'Xis_search_script')
983 let buf = RunVimInTerminal('-S Xis_search_script', {'rows': 9, 'cols': 70})
984 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
985 " the 'ambiwidth' check.
986 sleep 100m
987
988 " Need to send one key at a time to force a redraw.
989 call term_sendkeys(buf, '/fo')
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200990 call VerifyScreenDump(buf, 'Test_incsearch_search_01', {})
991 call term_sendkeys(buf, "\<Esc>")
992 sleep 100m
993
994 call term_sendkeys(buf, '/\v')
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200995 call VerifyScreenDump(buf, 'Test_incsearch_search_02', {})
996 call term_sendkeys(buf, "\<Esc>")
997
998 call StopVimInTerminal(buf)
999 call delete('Xis_search_script')
1000endfunc
1001
Bram Moolenaar41f08952021-02-22 22:13:49 +01001002func Test_hlsearch_dump()
1003 CheckOption hlsearch
1004 CheckScreendump
1005
1006 call writefile([
1007 \ 'set hlsearch cursorline',
1008 \ 'call setline(1, ["xxx", "xxx", "xxx"])',
1009 \ '/.*',
1010 \ '2',
1011 \ ], 'Xhlsearch_script')
1012 let buf = RunVimInTerminal('-S Xhlsearch_script', {'rows': 6, 'cols': 50})
1013 call VerifyScreenDump(buf, 'Test_hlsearch_1', {})
1014
1015 call term_sendkeys(buf, "/\\_.*\<CR>")
1016 call VerifyScreenDump(buf, 'Test_hlsearch_2', {})
1017
1018 call StopVimInTerminal(buf)
1019 call delete('Xhlsearch_script')
1020endfunc
1021
Bram Moolenaar2d5f3852021-04-21 15:11:42 +02001022func Test_hlsearch_and_visual()
1023 CheckOption hlsearch
1024 CheckScreendump
1025
1026 call writefile([
1027 \ 'set hlsearch',
1028 \ 'call setline(1, repeat(["xxx yyy zzz"], 3))',
1029 \ 'hi Search cterm=bold',
1030 \ '/yyy',
1031 \ 'call cursor(1, 6)',
1032 \ ], 'Xhlvisual_script')
1033 let buf = RunVimInTerminal('-S Xhlvisual_script', {'rows': 6, 'cols': 40})
1034 call term_sendkeys(buf, "vjj")
1035 call VerifyScreenDump(buf, 'Test_hlsearch_visual_1', {})
1036 call term_sendkeys(buf, "\<Esc>")
1037
1038 call StopVimInTerminal(buf)
1039 call delete('Xhlvisual_script')
1040endfunc
1041
Bram Moolenaare71c0eb2021-05-30 16:43:11 +02001042func Test_hlsearch_block_visual_match()
1043 CheckScreendump
1044
1045 let lines =<< trim END
1046 set hlsearch
1047 call setline(1, ['aa', 'bbbb', 'cccccc'])
1048 END
1049 call writefile(lines, 'Xhlsearch_block')
1050 let buf = RunVimInTerminal('-S Xhlsearch_block', {'rows': 9, 'cols': 60})
1051
1052 call term_sendkeys(buf, "G\<C-V>$kk\<Esc>")
1053 sleep 100m
1054 call term_sendkeys(buf, "/\\%V\<CR>")
1055 sleep 100m
1056 call VerifyScreenDump(buf, 'Test_hlsearch_block_visual_match', {})
1057
1058 call StopVimInTerminal(buf)
1059 call delete('Xhlsearch_block')
1060endfunc
1061
Bram Moolenaar976b8472018-08-12 15:49:47 +02001062func Test_incsearch_substitute()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001063 CheckOption incsearch
1064
Bram Moolenaar976b8472018-08-12 15:49:47 +02001065 call test_override("char_avail", 1)
1066 new
1067 set incsearch
1068 for n in range(1, 10)
1069 call setline(n, 'foo ' . n)
1070 endfor
1071 4
1072 call feedkeys(":.,.+2s/foo\<BS>o\<BS>o/xxx\<cr>", 'tx')
1073 call assert_equal('foo 3', getline(3))
1074 call assert_equal('xxx 4', getline(4))
1075 call assert_equal('xxx 5', getline(5))
1076 call assert_equal('xxx 6', getline(6))
1077 call assert_equal('foo 7', getline(7))
1078
1079 call Incsearch_cleanup()
1080endfunc
1081
Bram Moolenaar795aaa12020-10-02 20:36:01 +02001082func Test_incsearch_substitute_long_line()
1083 new
1084 call test_override("char_avail", 1)
1085 set incsearch
1086
1087 call repeat('x', 100000)->setline(1)
1088 call feedkeys(':s/\%c', 'xt')
1089 redraw
1090 call feedkeys("\<Esc>", 'xt')
1091
1092 call Incsearch_cleanup()
1093 bwipe!
1094endfunc
1095
LemonBoya4399382022-04-09 21:04:08 +01001096func Test_hlsearch_cursearch()
1097 CheckScreendump
1098
1099 let lines =<< trim END
1100 set hlsearch scrolloff=0
Bram Moolenaar9b367502022-04-22 20:07:21 +01001101 call setline(1, ['one', 'foo', 'bar', 'baz', 'foo the foo and foo', 'bar'])
LemonBoya4399382022-04-09 21:04:08 +01001102 hi Search ctermbg=yellow
1103 hi CurSearch ctermbg=blue
1104 END
1105 call writefile(lines, 'Xhlsearch_cursearch')
1106 let buf = RunVimInTerminal('-S Xhlsearch_cursearch', {'rows': 9, 'cols': 60})
1107
1108 call term_sendkeys(buf, "gg/foo\<CR>")
1109 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_single_line_1', {})
1110
1111 call term_sendkeys(buf, "n")
1112 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_single_line_2', {})
1113
Bram Moolenaar9b367502022-04-22 20:07:21 +01001114 call term_sendkeys(buf, "n")
1115 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_single_line_2a', {})
1116
1117 call term_sendkeys(buf, "n")
1118 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_single_line_2b', {})
1119
1120 call term_sendkeys(buf, ":call setline(5, 'foo')\<CR>")
1121 call term_sendkeys(buf, "0?\<CR>")
LemonBoya4399382022-04-09 21:04:08 +01001122 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_single_line_3', {})
1123
1124 call term_sendkeys(buf, "gg/foo\\nbar\<CR>")
Bram Moolenaar693ccd12022-04-16 12:04:37 +01001125 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_multiple_line_1', {})
1126
1127 call term_sendkeys(buf, ":call setline(1, ['---', 'abcdefg', 'hijkl', '---', 'abcdefg', 'hijkl'])\<CR>")
1128 call term_sendkeys(buf, "gg/efg\\nhij\<CR>")
1129 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_multiple_line_2', {})
1130 call term_sendkeys(buf, "h\<C-L>")
1131 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_multiple_line_3', {})
1132 call term_sendkeys(buf, "j\<C-L>")
1133 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_multiple_line_4', {})
1134 call term_sendkeys(buf, "h\<C-L>")
1135 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_multiple_line_5', {})
LemonBoya4399382022-04-09 21:04:08 +01001136
Bram Moolenaar368137a2022-05-31 13:43:12 +01001137 " check clearing CurSearch when using it for another match
1138 call term_sendkeys(buf, "G?^abcd\<CR>Y")
1139 call term_sendkeys(buf, "kkP")
1140 call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_changed_1', {})
1141
LemonBoya4399382022-04-09 21:04:08 +01001142 call StopVimInTerminal(buf)
1143 call delete('Xhlsearch_cursearch')
1144endfunc
1145
Bram Moolenaar164251f2018-08-12 16:26:58 +02001146" Similar to Test_incsearch_substitute() but with a screendump halfway.
1147func Test_incsearch_substitute_dump()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001148 CheckOption incsearch
1149 CheckScreendump
1150
Bram Moolenaar164251f2018-08-12 16:26:58 +02001151 call writefile([
1152 \ 'set incsearch hlsearch scrolloff=0',
1153 \ 'for n in range(1, 10)',
1154 \ ' call setline(n, "foo " . n)',
1155 \ 'endfor',
Bram Moolenaar2f6a3462018-08-14 18:16:52 +02001156 \ 'call setline(11, "bar 11")',
Bram Moolenaar164251f2018-08-12 16:26:58 +02001157 \ '3',
1158 \ ], 'Xis_subst_script')
1159 let buf = RunVimInTerminal('-S Xis_subst_script', {'rows': 9, 'cols': 70})
1160 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1161 " the 'ambiwidth' check.
1162 sleep 100m
1163
1164 " Need to send one key at a time to force a redraw.
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02001165 " Select three lines at the cursor with typed pattern.
Bram Moolenaar164251f2018-08-12 16:26:58 +02001166 call term_sendkeys(buf, ':.,.+2s/')
1167 sleep 100m
1168 call term_sendkeys(buf, 'f')
1169 sleep 100m
1170 call term_sendkeys(buf, 'o')
1171 sleep 100m
1172 call term_sendkeys(buf, 'o')
1173 call VerifyScreenDump(buf, 'Test_incsearch_substitute_01', {})
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02001174 call term_sendkeys(buf, "\<Esc>")
1175
1176 " Select three lines at the cursor using previous pattern.
1177 call term_sendkeys(buf, "/foo\<CR>")
1178 sleep 100m
1179 call term_sendkeys(buf, ':.,.+2s//')
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02001180 call VerifyScreenDump(buf, 'Test_incsearch_substitute_02', {})
1181
1182 " Deleting last slash should remove the match.
1183 call term_sendkeys(buf, "\<BS>")
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02001184 call VerifyScreenDump(buf, 'Test_incsearch_substitute_03', {})
Bram Moolenaar60d08712018-08-12 21:53:15 +02001185 call term_sendkeys(buf, "\<Esc>")
1186
1187 " Reverse range is accepted
1188 call term_sendkeys(buf, ':5,2s/foo')
Bram Moolenaar60d08712018-08-12 21:53:15 +02001189 call VerifyScreenDump(buf, 'Test_incsearch_substitute_04', {})
Bram Moolenaar164251f2018-08-12 16:26:58 +02001190 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaar2b926fc2018-08-13 11:07:57 +02001191
1192 " White space after the command is skipped
1193 call term_sendkeys(buf, ':2,3sub /fo')
Bram Moolenaar2b926fc2018-08-13 11:07:57 +02001194 call VerifyScreenDump(buf, 'Test_incsearch_substitute_05', {})
1195 call term_sendkeys(buf, "\<Esc>")
1196
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001197 " Command modifiers are skipped
1198 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 +02001199 call VerifyScreenDump(buf, 'Test_incsearch_substitute_06', {})
1200 call term_sendkeys(buf, "\<Esc>")
1201
Bram Moolenaar2f6a3462018-08-14 18:16:52 +02001202 " Cursorline highlighting at match
1203 call term_sendkeys(buf, ":set cursorline\<CR>")
1204 call term_sendkeys(buf, 'G9G')
1205 call term_sendkeys(buf, ':9,11s/bar')
Bram Moolenaar2f6a3462018-08-14 18:16:52 +02001206 call VerifyScreenDump(buf, 'Test_incsearch_substitute_07', {})
1207 call term_sendkeys(buf, "\<Esc>")
1208
1209 " Cursorline highlighting at cursor when no match
1210 call term_sendkeys(buf, ':9,10s/bar')
Bram Moolenaar2f6a3462018-08-14 18:16:52 +02001211 call VerifyScreenDump(buf, 'Test_incsearch_substitute_08', {})
1212 call term_sendkeys(buf, "\<Esc>")
1213
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +02001214 " Only \v handled as empty pattern, does not move cursor
1215 call term_sendkeys(buf, '3G4G')
1216 call term_sendkeys(buf, ":nohlsearch\<CR>")
1217 call term_sendkeys(buf, ':6,7s/\v')
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +02001218 call VerifyScreenDump(buf, 'Test_incsearch_substitute_09', {})
1219 call term_sendkeys(buf, "\<Esc>")
1220
Bram Moolenaarf13daa42018-08-31 22:09:54 +02001221 call term_sendkeys(buf, ":set nocursorline\<CR>")
1222
1223 " All matches are highlighted for 'hlsearch' after the incsearch canceled
1224 call term_sendkeys(buf, "1G*")
1225 call term_sendkeys(buf, ":2,5s/foo")
1226 sleep 100m
1227 call term_sendkeys(buf, "\<Esc>")
1228 call VerifyScreenDump(buf, 'Test_incsearch_substitute_10', {})
1229
Bram Moolenaar65985ac2018-09-16 17:08:04 +02001230 call term_sendkeys(buf, ":split\<CR>")
1231 call term_sendkeys(buf, ":let @/ = 'xyz'\<CR>")
1232 call term_sendkeys(buf, ":%s/.")
1233 call VerifyScreenDump(buf, 'Test_incsearch_substitute_11', {})
1234 call term_sendkeys(buf, "\<BS>")
1235 call VerifyScreenDump(buf, 'Test_incsearch_substitute_12', {})
1236 call term_sendkeys(buf, "\<Esc>")
1237 call VerifyScreenDump(buf, 'Test_incsearch_substitute_13', {})
Bram Moolenaarc6725252019-11-23 21:56:46 +01001238 call term_sendkeys(buf, ":%bwipe!\<CR>")
1239 call term_sendkeys(buf, ":only!\<CR>")
1240
1241 " get :'<,'>s command in history
1242 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
1243 call term_sendkeys(buf, "aasdfasdf\<Esc>")
1244 call term_sendkeys(buf, "V:s/a/b/g\<CR>")
1245 " Using '<,'> does not give E20
1246 call term_sendkeys(buf, ":new\<CR>")
1247 call term_sendkeys(buf, "aasdfasdf\<Esc>")
1248 call term_sendkeys(buf, ":\<Up>\<Up>")
1249 call VerifyScreenDump(buf, 'Test_incsearch_substitute_14', {})
1250 call term_sendkeys(buf, "<Esc>")
Bram Moolenaar65985ac2018-09-16 17:08:04 +02001251
Bram Moolenaar164251f2018-08-12 16:26:58 +02001252 call StopVimInTerminal(buf)
1253 call delete('Xis_subst_script')
1254endfunc
1255
Bram Moolenaarc036e872020-02-21 21:30:52 +01001256func Test_incsearch_highlighting()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001257 CheckOption incsearch
1258 CheckScreendump
Bram Moolenaarc036e872020-02-21 21:30:52 +01001259
1260 call writefile([
1261 \ 'set incsearch hlsearch',
1262 \ 'call setline(1, "hello/there")',
1263 \ ], 'Xis_subst_hl_script')
1264 let buf = RunVimInTerminal('-S Xis_subst_hl_script', {'rows': 4, 'cols': 20})
1265 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1266 " the 'ambiwidth' check.
1267 sleep 300m
1268
1269 " Using a different search delimiter should still highlight matches
1270 " that contain a '/'.
1271 call term_sendkeys(buf, ":%s;ello/the")
1272 call VerifyScreenDump(buf, 'Test_incsearch_substitute_15', {})
1273 call term_sendkeys(buf, "<Esc>")
Bram Moolenaarb68df222020-03-19 15:05:28 +01001274
1275 call StopVimInTerminal(buf)
1276 call delete('Xis_subst_hl_script')
Bram Moolenaarc036e872020-02-21 21:30:52 +01001277endfunc
1278
Bram Moolenaar4a7d2d32019-02-21 16:25:50 +01001279func Test_incsearch_with_change()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001280 CheckFeature timers
1281 CheckOption incsearch
1282 CheckScreendump
Bram Moolenaar4a7d2d32019-02-21 16:25:50 +01001283
1284 call writefile([
1285 \ 'set incsearch hlsearch scrolloff=0',
1286 \ 'call setline(1, ["one", "two ------ X", "three"])',
1287 \ 'call timer_start(200, { _ -> setline(2, "x")})',
1288 \ ], 'Xis_change_script')
1289 let buf = RunVimInTerminal('-S Xis_change_script', {'rows': 9, 'cols': 70})
1290 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1291 " the 'ambiwidth' check.
Bram Moolenaare86ecbd2019-02-21 17:48:59 +01001292 sleep 300m
Bram Moolenaar4a7d2d32019-02-21 16:25:50 +01001293
1294 " Highlight X, it will be deleted by the timer callback.
1295 call term_sendkeys(buf, ':%s/X')
1296 call VerifyScreenDump(buf, 'Test_incsearch_change_01', {})
1297 call term_sendkeys(buf, "\<Esc>")
1298
1299 call StopVimInTerminal(buf)
1300 call delete('Xis_change_script')
1301endfunc
1302
Bram Moolenaar81f56532018-08-18 16:19:42 +02001303" Similar to Test_incsearch_substitute_dump() for :sort
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +02001304func Test_incsearch_sort_dump()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001305 CheckOption incsearch
1306 CheckScreendump
1307
Bram Moolenaar81f56532018-08-18 16:19:42 +02001308 call writefile([
1309 \ 'set incsearch hlsearch scrolloff=0',
1310 \ 'call setline(1, ["another one 2", "that one 3", "the one 1"])',
1311 \ ], 'Xis_sort_script')
1312 let buf = RunVimInTerminal('-S Xis_sort_script', {'rows': 9, 'cols': 70})
1313 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1314 " the 'ambiwidth' check.
1315 sleep 100m
1316
Bram Moolenaar81f56532018-08-18 16:19:42 +02001317 call term_sendkeys(buf, ':sort ni u /on')
Bram Moolenaar81f56532018-08-18 16:19:42 +02001318 call VerifyScreenDump(buf, 'Test_incsearch_sort_01', {})
1319 call term_sendkeys(buf, "\<Esc>")
1320
Bram Moolenaar333015a2020-04-25 15:54:16 +02001321 call term_sendkeys(buf, ':sort! /on')
1322 call VerifyScreenDump(buf, 'Test_incsearch_sort_02', {})
1323 call term_sendkeys(buf, "\<Esc>")
1324
Bram Moolenaar81f56532018-08-18 16:19:42 +02001325 call StopVimInTerminal(buf)
1326 call delete('Xis_sort_script')
1327endfunc
1328
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001329" Similar to Test_incsearch_substitute_dump() for :vimgrep famiry
1330func Test_incsearch_vimgrep_dump()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001331 CheckOption incsearch
1332 CheckScreendump
1333
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001334 call writefile([
1335 \ 'set incsearch hlsearch scrolloff=0',
1336 \ 'call setline(1, ["another one 2", "that one 3", "the one 1"])',
1337 \ ], 'Xis_vimgrep_script')
1338 let buf = RunVimInTerminal('-S Xis_vimgrep_script', {'rows': 9, 'cols': 70})
1339 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1340 " the 'ambiwidth' check.
1341 sleep 100m
1342
1343 " Need to send one key at a time to force a redraw.
1344 call term_sendkeys(buf, ':vimgrep on')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001345 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_01', {})
1346 call term_sendkeys(buf, "\<Esc>")
1347
1348 call term_sendkeys(buf, ':vimg /on/ *.txt')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001349 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_02', {})
1350 call term_sendkeys(buf, "\<Esc>")
1351
1352 call term_sendkeys(buf, ':vimgrepadd "\<on')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001353 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_03', {})
1354 call term_sendkeys(buf, "\<Esc>")
1355
1356 call term_sendkeys(buf, ':lv "tha')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001357 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_04', {})
1358 call term_sendkeys(buf, "\<Esc>")
1359
1360 call term_sendkeys(buf, ':lvimgrepa "the" **/*.txt')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001361 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_05', {})
1362 call term_sendkeys(buf, "\<Esc>")
1363
1364 call StopVimInTerminal(buf)
1365 call delete('Xis_vimgrep_script')
1366endfunc
1367
Bram Moolenaar198cb662018-09-06 21:44:17 +02001368func Test_keep_last_search_pattern()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001369 CheckOption incsearch
1370
Bram Moolenaar198cb662018-09-06 21:44:17 +02001371 new
1372 call setline(1, ['foo', 'foo', 'foo'])
1373 set incsearch
1374 call test_override("char_avail", 1)
1375 let @/ = 'bar'
1376 call feedkeys(":/foo/s//\<Esc>", 'ntx')
1377 call assert_equal('bar', @/)
1378
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001379 " no error message if pattern not found
1380 call feedkeys(":/xyz/s//\<Esc>", 'ntx')
1381 call assert_equal('bar', @/)
1382
Bram Moolenaar198cb662018-09-06 21:44:17 +02001383 bwipe!
1384 call test_override("ALL", 0)
1385 set noincsearch
1386endfunc
1387
Bram Moolenaar99f043a2018-09-09 15:54:14 +02001388func Test_word_under_cursor_after_match()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001389 CheckOption incsearch
1390
Bram Moolenaar99f043a2018-09-09 15:54:14 +02001391 new
1392 call setline(1, 'foo bar')
1393 set incsearch
1394 call test_override("char_avail", 1)
1395 try
1396 call feedkeys("/foo\<C-R>\<C-W>\<CR>", 'ntx')
1397 catch /E486:/
1398 endtry
1399 call assert_equal('foobar', @/)
1400
1401 bwipe!
1402 call test_override("ALL", 0)
1403 set noincsearch
1404endfunc
1405
1406func Test_subst_word_under_cursor()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001407 CheckOption incsearch
1408
Bram Moolenaar99f043a2018-09-09 15:54:14 +02001409 new
1410 call setline(1, ['int SomeLongName;', 'for (xxx = 1; xxx < len; ++xxx)'])
1411 set incsearch
1412 call test_override("char_avail", 1)
1413 call feedkeys("/LongName\<CR>", 'ntx')
1414 call feedkeys(":%s/xxx/\<C-R>\<C-W>/g\<CR>", 'ntx')
1415 call assert_equal('for (SomeLongName = 1; SomeLongName < len; ++SomeLongName)', getline(2))
1416
1417 bwipe!
1418 call test_override("ALL", 0)
1419 set noincsearch
1420endfunc
1421
Bram Moolenaarf45938c2017-11-02 15:59:57 +01001422func Test_search_undefined_behaviour()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001423 CheckFeature terminal
1424
Bram Moolenaarf45938c2017-11-02 15:59:57 +01001425 let h = winheight(0)
1426 if h < 3
1427 return
1428 endif
1429 " did cause an undefined left shift
1430 let g:buf = term_start([GetVimProg(), '--clean', '-e', '-s', '-c', 'call search(getline("."))', 'samples/test000'], {'term_rows': 3})
1431 call assert_equal([''], getline(1, '$'))
1432 call term_sendkeys(g:buf, ":qa!\<cr>")
1433 bwipe!
1434endfunc
Bram Moolenaar2973daa2017-11-02 23:15:40 +01001435
1436func Test_search_undefined_behaviour2()
1437 call search("\%UC0000000")
1438endfunc
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001439
1440" Test for search('multi-byte char', 'bce')
1441func Test_search_multibyte()
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001442 let save_enc = &encoding
1443 set encoding=utf8
1444 enew!
1445 call append('$', 'A')
1446 call cursor(2, 1)
1447 call assert_equal(2, search('A', 'bce', line('.')))
1448 enew!
1449 let &encoding = save_enc
1450endfunc
Bram Moolenaar890dd052017-12-16 19:59:37 +01001451
1452" This was causing E874. Also causes an invalid read?
1453func Test_look_behind()
1454 new
Bram Moolenaar3ba35402019-12-21 22:00:50 +01001455 call setline(1, '0\|\&\n\@<=')
Bram Moolenaar890dd052017-12-16 19:59:37 +01001456 call search(getline("."))
1457 bwipe!
1458endfunc
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01001459
Bram Moolenaar872bee52021-05-24 22:56:15 +02001460func Test_search_visual_area_linewise()
1461 new
1462 call setline(1, ['aa', 'bb', 'cc'])
1463 exe "normal 2GV\<Esc>"
1464 for engine in [1, 2]
1465 exe 'set regexpengine=' .. engine
1466 exe "normal gg/\\%'<\<CR>>"
1467 call assert_equal([0, 2, 1, 0, 1], getcurpos(), 'engine ' .. engine)
1468 exe "normal gg/\\%'>\<CR>"
1469 call assert_equal([0, 2, 2, 0, 2], getcurpos(), 'engine ' .. engine)
1470 endfor
1471
1472 bwipe!
1473 set regexpengine&
1474endfunc
1475
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01001476func Test_search_sentence()
1477 new
1478 " this used to cause a crash
Bram Moolenaar1bd999f2017-12-19 22:25:40 +01001479 /\%'(
1480 /
Bram Moolenaar872bee52021-05-24 22:56:15 +02001481 bwipe
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01001482endfunc
Bram Moolenaar2fb8f682018-12-01 13:14:45 +01001483
1484" Test that there is no crash when there is a last search pattern but no last
1485" substitute pattern.
1486func Test_no_last_substitute_pat()
1487 " Use viminfo to set the last search pattern to a string and make the last
1488 " substitute pattern the most recent used and make it empty (NULL).
1489 call writefile(['~MSle0/bar', '~MSle0~&'], 'Xviminfo')
1490 rviminfo! Xviminfo
1491 call assert_fails('normal n', 'E35:')
1492
1493 call delete('Xviminfo')
1494endfunc
Bram Moolenaar5f5e2032018-12-14 15:47:03 +01001495
1496func Test_search_Ctrl_L_combining()
1497 " Make sure, that Ctrl-L works correctly with combining characters.
1498 " It uses an artificial example of an 'a' with 4 combining chars:
Bram Moolenaar3ba35402019-12-21 22:00:50 +01001499 " 'a' U+0061 Dec:97 LATIN SMALL LETTER A &#x61; /\%u61\Z "\u0061"
Bram Moolenaar5f5e2032018-12-14 15:47:03 +01001500 " ' ̀' U+0300 Dec:768 COMBINING GRAVE ACCENT &#x300; /\%u300\Z "\u0300"
1501 " ' ́' U+0301 Dec:769 COMBINING ACUTE ACCENT &#x301; /\%u301\Z "\u0301"
1502 " ' ̇' U+0307 Dec:775 COMBINING DOT ABOVE &#x307; /\%u307\Z "\u0307"
Bram Moolenaar3ba35402019-12-21 22:00:50 +01001503 " ' ̣' U+0323 Dec:803 COMBINING DOT BELOW &#x323; /\%u323 "\u0323"
Bram Moolenaar5f5e2032018-12-14 15:47:03 +01001504 " Those should also appear on the commandline
Bram Moolenaarb68df222020-03-19 15:05:28 +01001505 CheckOption incsearch
1506
Bram Moolenaar5f5e2032018-12-14 15:47:03 +01001507 call Cmdline3_prep()
1508 1
1509 let bufcontent = ['', 'Miạ̀́̇m']
1510 call append('$', bufcontent)
1511 call feedkeys("/Mi\<c-l>\<c-l>\<cr>", 'tx')
1512 call assert_equal(5, line('.'))
1513 call assert_equal(bufcontent[1], @/)
1514 call Incsearch_cleanup()
1515endfunc
Bram Moolenaar527a2d82019-02-21 22:28:51 +01001516
Bram Moolenaarab350f82019-02-28 06:25:00 +01001517func Test_large_hex_chars1()
Bram Moolenaar527a2d82019-02-21 22:28:51 +01001518 " This used to cause a crash, the character becomes an NFA state.
1519 try
1520 /\%Ufffffc23
1521 catch
1522 call assert_match('E678:', v:exception)
1523 endtry
Bram Moolenaarab350f82019-02-28 06:25:00 +01001524 try
1525 set re=1
1526 /\%Ufffffc23
1527 catch
1528 call assert_match('E678:', v:exception)
1529 endtry
1530 set re&
1531endfunc
1532
1533func Test_large_hex_chars2()
1534 " This used to cause a crash, the character becomes an NFA state.
1535 try
1536 /[\Ufffffc1f]
1537 catch
1538 call assert_match('E486:', v:exception)
1539 endtry
1540 try
1541 set re=1
1542 /[\Ufffffc1f]
1543 catch
1544 call assert_match('E486:', v:exception)
1545 endtry
1546 set re&
Bram Moolenaar527a2d82019-02-21 22:28:51 +01001547endfunc
Bram Moolenaarcd625122019-02-22 17:29:43 +01001548
1549func Test_one_error_msg()
Bram Moolenaar8832a342020-04-11 18:36:38 +02001550 " This was also giving an internal error
Bram Moolenaarcd625122019-02-22 17:29:43 +01001551 call assert_fails('call search(" \\((\\v[[=P=]]){185}+ ")', 'E871:')
1552endfunc
Bram Moolenaar730f48f2019-04-11 13:45:57 +02001553
1554func Test_incsearch_add_char_under_cursor()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001555 CheckOption incsearch
1556
Bram Moolenaar730f48f2019-04-11 13:45:57 +02001557 set incsearch
1558 new
1559 call setline(1, ['find match', 'anything'])
1560 1
1561 call test_override('char_avail', 1)
1562 call feedkeys("fc/m\<C-L>\<C-L>\<C-L>\<C-L>\<C-L>\<CR>", 'tx')
1563 call assert_equal('match', @/)
1564 call test_override('char_avail', 0)
1565
1566 set incsearch&
1567 bwipe!
1568endfunc
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02001569
1570" Test for the search() function with match at the cursor position
1571func Test_search_match_at_curpos()
1572 new
1573 call append(0, ['foobar', '', 'one two', ''])
1574
1575 normal gg
1576
Bram Moolenaar196b4662019-09-06 21:34:30 +02001577 eval 'foobar'->search('c')
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02001578 call assert_equal([1, 1], [line('.'), col('.')])
1579
1580 normal j
1581 call search('^$', 'c')
1582 call assert_equal([2, 1], [line('.'), col('.')])
1583
1584 call search('^$', 'bc')
1585 call assert_equal([2, 1], [line('.'), col('.')])
1586
1587 exe "normal /two\<CR>"
1588 call search('.', 'c')
1589 call assert_equal([3, 5], [line('.'), col('.')])
1590
1591 close!
1592endfunc
Bram Moolenaardb294ad2019-06-06 12:49:29 +02001593
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001594" Test for error cases with the search() function
1595func Test_search_errors()
1596 call assert_fails("call search('pat', [])", 'E730:')
1597 call assert_fails("call search('pat', 'b', {})", 'E728:')
1598 call assert_fails("call search('pat', 'b', 1, [])", 'E745:')
1599 call assert_fails("call search('pat', 'ns')", 'E475:')
1600 call assert_fails("call search('pat', 'mr')", 'E475:')
Bram Moolenaar8832a342020-04-11 18:36:38 +02001601
1602 new
1603 call setline(1, ['foo', 'bar'])
1604 call assert_fails('call feedkeys("/foo/;/bar/;\<CR>", "tx")', 'E386:')
1605 bwipe!
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001606endfunc
1607
Paul Ollis65745772022-06-05 16:55:54 +01001608func Test_search_timeout()
1609 new
Bram Moolenaar616592e2022-06-17 15:17:10 +01001610 " use a complicated pattern that should be slow with the BT engine
Paul Ollis65745772022-06-05 16:55:54 +01001611 let pattern = '\%#=1a*.*X\@<=b*'
Bram Moolenaar616592e2022-06-17 15:17:10 +01001612
1613 " use a timeout of 50 msec
1614 let search_timeout = 0.05
1615
1616 " fill the buffer so that it takes 15 times the timeout to search
Paul Ollis65745772022-06-05 16:55:54 +01001617 let slow_target_timeout = search_timeout * 15.0
1618
Bram Moolenaar616592e2022-06-17 15:17:10 +01001619 " Fill the buffer with more and more text until searching takes more time
1620 " than slow_target_timeout.
Paul Ollis65745772022-06-05 16:55:54 +01001621 for n in range(40, 400, 30)
1622 call setline(1, ['aaa', repeat('abc ', n), 'ccc'])
1623 let start = reltime()
1624 call search(pattern, '', 0)
1625 let elapsed = reltimefloat(reltime(start))
1626 if elapsed > slow_target_timeout
1627 break
1628 endif
1629 endfor
1630 call assert_true(elapsed > slow_target_timeout)
1631
Bram Moolenaar616592e2022-06-17 15:17:10 +01001632 " Check that the timeout kicks in, the time should be less than half of what
1633 " we measured without the timeout. This is permissive, because the timer is
1634 " known to overrun, especially when using valgrind.
Paul Ollis65745772022-06-05 16:55:54 +01001635 let max_time = elapsed / 2.0
1636 let start = reltime()
1637 call search(pattern, '', 0, float2nr(search_timeout * 1000))
1638 let elapsed = reltimefloat(reltime(start))
Bram Moolenaar616592e2022-06-17 15:17:10 +01001639 call assert_inrange(search_timeout * 0.9, max_time, elapsed)
Paul Ollis65745772022-06-05 16:55:54 +01001640
1641 bwipe!
1642endfunc
1643
Bram Moolenaardb294ad2019-06-06 12:49:29 +02001644func Test_search_display_pattern()
1645 new
1646 call setline(1, ['foo', 'bar', 'foobar'])
1647
1648 call cursor(1, 1)
1649 let @/ = 'foo'
Bram Moolenaara4208962019-08-24 20:50:19 +02001650 let pat = @/->escape('()*?'. '\s\+')
Bram Moolenaardb294ad2019-06-06 12:49:29 +02001651 let g:a = execute(':unsilent :norm! n')
1652 call assert_match(pat, g:a)
1653
1654 " right-left
1655 if exists("+rightleft")
1656 set rl
1657 call cursor(1, 1)
1658 let @/ = 'foo'
1659 let pat = 'oof/\s\+'
1660 let g:a = execute(':unsilent :norm! n')
1661 call assert_match(pat, g:a)
1662 set norl
1663 endif
1664endfunc
Bram Moolenaar196b4662019-09-06 21:34:30 +02001665
1666func Test_searchdecl()
1667 let lines =<< trim END
1668 int global;
1669
1670 func()
1671 {
1672 int global;
1673 if (cond) {
1674 int local;
1675 }
1676 int local;
1677 // comment
1678 }
1679 END
1680 new
1681 call setline(1, lines)
1682 10
1683 call assert_equal(0, searchdecl('local', 0, 0))
1684 call assert_equal(7, getcurpos()[1])
1685
1686 10
1687 call assert_equal(0, 'local'->searchdecl(0, 1))
1688 call assert_equal(9, getcurpos()[1])
1689
1690 10
1691 call assert_equal(0, searchdecl('global'))
1692 call assert_equal(5, getcurpos()[1])
1693
1694 10
1695 call assert_equal(0, searchdecl('global', 1))
1696 call assert_equal(1, getcurpos()[1])
1697
1698 bwipe!
1699endfunc
Bram Moolenaar98a336d2020-01-20 20:22:30 +01001700
1701func Test_search_special()
Bram Moolenaarfe4bbac2020-01-20 21:12:20 +01001702 " this was causing illegal memory access and an endless loop
1703 set t_PE=
Bram Moolenaar98a336d2020-01-20 20:22:30 +01001704 exe "norm /\x80PS"
1705endfunc
Bram Moolenaar07ada5f2020-02-05 20:38:22 +01001706
1707" Test for command failures when the last search pattern is not set.
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001708" Need to run this in a new vim instance where last search pattern is not set.
Bram Moolenaar07ada5f2020-02-05 20:38:22 +01001709func Test_search_with_no_last_pat()
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001710 let lines =<< trim [SCRIPT]
1711 call assert_fails("normal i\<C-R>/\e", 'E35:')
1712 call assert_fails("exe '/'", 'E35:')
1713 call assert_fails("exe '?'", 'E35:')
1714 call assert_fails("/", 'E35:')
1715 call assert_fails("?", 'E35:')
1716 call assert_fails("normal n", 'E35:')
1717 call assert_fails("normal N", 'E35:')
1718 call assert_fails("normal gn", 'E35:')
1719 call assert_fails("normal gN", 'E35:')
1720 call assert_fails("normal cgn", 'E35:')
1721 call assert_fails("normal cgN", 'E35:')
1722 let p = []
1723 let p = @/
1724 call assert_equal('', p)
1725 call assert_fails("normal :\<C-R>/", 'E35:')
1726 call assert_fails("//p", 'E35:')
1727 call assert_fails(";//p", 'E35:')
1728 call assert_fails("??p", 'E35:')
1729 call assert_fails(";??p", 'E35:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001730 call assert_fails('g//p', ['E35:', 'E476:'])
1731 call assert_fails('v//p', ['E35:', 'E476:'])
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001732 call writefile(v:errors, 'Xresult')
1733 qall!
1734 [SCRIPT]
1735 call writefile(lines, 'Xscript')
1736
1737 if RunVim([], [], '--clean -S Xscript')
1738 call assert_equal([], readfile('Xresult'))
1739 endif
1740 call delete('Xscript')
1741 call delete('Xresult')
Bram Moolenaar07ada5f2020-02-05 20:38:22 +01001742endfunc
1743
1744" Test for using tilde (~) atom in search. This should use the last used
1745" substitute pattern
1746func Test_search_tilde_pat()
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001747 let lines =<< trim [SCRIPT]
1748 set regexpengine=1
1749 call assert_fails('exe "normal /~\<CR>"', 'E33:')
1750 call assert_fails('exe "normal ?~\<CR>"', 'E33:')
1751 set regexpengine=2
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001752 call assert_fails('exe "normal /~\<CR>"', ['E33:', 'E383:'])
1753 call assert_fails('exe "normal ?~\<CR>"', ['E33:', 'E383:'])
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001754 set regexpengine&
1755 call writefile(v:errors, 'Xresult')
1756 qall!
1757 [SCRIPT]
1758 call writefile(lines, 'Xscript')
1759 if RunVim([], [], '--clean -S Xscript')
1760 call assert_equal([], readfile('Xresult'))
1761 endif
1762 call delete('Xscript')
1763 call delete('Xresult')
Bram Moolenaar07ada5f2020-02-05 20:38:22 +01001764endfunc
1765
Bram Moolenaar406cd902020-02-18 21:54:41 +01001766" Test for searching a pattern that is not present with 'nowrapscan'
1767func Test_search_pat_not_found()
1768 new
1769 set nowrapscan
1770 let @/ = '1abcxyz2'
1771 call assert_fails('normal n', 'E385:')
1772 call assert_fails('normal N', 'E384:')
1773 set wrapscan&
1774 close
1775endfunc
1776
1777" Test for v:searchforward variable
1778func Test_searchforward_var()
1779 new
1780 call setline(1, ['foo', '', 'foo'])
1781 call cursor(2, 1)
1782 let @/ = 'foo'
1783 let v:searchforward = 0
1784 normal N
1785 call assert_equal(3, line('.'))
1786 call cursor(2, 1)
1787 let v:searchforward = 1
1788 normal N
1789 call assert_equal(1, line('.'))
1790 close!
1791endfunc
1792
Bram Moolenaar476a6132020-04-08 19:48:56 +02001793" Test for invalid regular expressions
1794func Test_invalid_regexp()
1795 set regexpengine=1
1796 call assert_fails("call search(repeat('\\(.\\)', 10))", 'E51:')
Bram Moolenaar4d23c522020-04-09 18:42:11 +02001797 call assert_fails("call search('\\%(')", 'E53:')
1798 call assert_fails("call search('\\(')", 'E54:')
1799 call assert_fails("call search('\\)')", 'E55:')
Bram Moolenaar476a6132020-04-08 19:48:56 +02001800 call assert_fails("call search('x\\@#')", 'E59:')
Bram Moolenaar4d23c522020-04-09 18:42:11 +02001801 call assert_fails('call search(''\v%(%(%(%(%(%(%(%(%(%(%(a){1}){1}){1}){1}){1}){1}){1}){1}){1}){1}){1}'')', 'E60:')
1802 call assert_fails("call search('a\\+*')", 'E61:')
Bram Moolenaar476a6132020-04-08 19:48:56 +02001803 call assert_fails("call search('\\_m')", 'E63:')
1804 call assert_fails("call search('\\+')", 'E64:')
1805 call assert_fails("call search('\\1')", 'E65:')
1806 call assert_fails("call search('\\z\\(\\)')", 'E66:')
1807 call assert_fails("call search('\\z2')", 'E67:')
1808 call assert_fails("call search('\\zx')", 'E68:')
1809 call assert_fails("call search('\\%[ab')", 'E69:')
1810 call assert_fails("call search('\\%[]')", 'E70:')
1811 call assert_fails("call search('\\%a')", 'E71:')
1812 call assert_fails("call search('ab\\%[\\(cd\\)]')", 'E369:')
1813 call assert_fails("call search('ab\\%[\\%(cd\\)]')", 'E369:')
1814 set regexpengine=2
1815 call assert_fails("call search('\\_')", 'E865:')
1816 call assert_fails("call search('\\+')", 'E866:')
1817 call assert_fails("call search('\\zx')", 'E867:')
1818 call assert_fails("call search('\\%a')", 'E867:')
1819 call assert_fails("call search('x\\@#')", 'E869:')
1820 call assert_fails("call search(repeat('\\(.\\)', 10))", 'E872:')
1821 call assert_fails("call search('\\_m')", 'E877:')
1822 call assert_fails("call search('\\%(')", 'E53:')
1823 call assert_fails("call search('\\(')", 'E54:')
1824 call assert_fails("call search('\\)')", 'E55:')
1825 call assert_fails("call search('\\z\\(\\)')", 'E66:')
Dominique Pelle8bfa0eb2022-01-02 16:16:33 +00001826 call assert_fails("call search('\\z2')", 'E67:')
1827 call assert_fails("call search('\\zx')", 'E867:')
Bram Moolenaar476a6132020-04-08 19:48:56 +02001828 call assert_fails("call search('\\%[ab')", 'E69:')
Bram Moolenaar4d23c522020-04-09 18:42:11 +02001829 call assert_fails("call search('\\%[]')", 'E70:')
Bram Moolenaar476a6132020-04-08 19:48:56 +02001830 call assert_fails("call search('\\%9999999999999999999999999999v')", 'E951:')
1831 set regexpengine&
1832 call assert_fails("call search('\\%#=3ab')", 'E864:')
1833endfunc
1834
Bram Moolenaar004a6782020-04-11 17:09:31 +02001835" Test for searching a very complex pattern in a string. Should switch the
1836" regexp engine from NFA to the old engine.
1837func Test_regexp_switch_engine()
1838 let l = readfile('samples/re.freeze.txt')
1839 let v = substitute(l[4], '..\@<!', '', '')
1840 call assert_equal(l[4], v)
1841endfunc
1842
1843" Test for the \%V atom to search within visually selected text
1844func Test_search_in_visual_area()
1845 new
1846 call setline(1, ['foo bar1', 'foo bar2', 'foo bar3', 'foo bar4'])
1847 exe "normal 2GVjo/\\%Vbar\<CR>\<Esc>"
1848 call assert_equal([2, 5], [line('.'), col('.')])
1849 exe "normal 2GVj$?\\%Vbar\<CR>\<Esc>"
1850 call assert_equal([3, 5], [line('.'), col('.')])
1851 close!
1852endfunc
1853
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001854" Test for searching with 'smartcase' and 'ignorecase'
1855func Test_search_smartcase()
1856 new
1857 call setline(1, ['', 'Hello'])
1858 set noignorecase nosmartcase
1859 call assert_fails('exe "normal /\\a\\_.\\(.*\\)O\<CR>"', 'E486:')
1860
1861 set ignorecase nosmartcase
1862 exe "normal /\\a\\_.\\(.*\\)O\<CR>"
1863 call assert_equal([2, 1], [line('.'), col('.')])
1864
1865 call cursor(1, 1)
1866 set ignorecase smartcase
1867 call assert_fails('exe "normal /\\a\\_.\\(.*\\)O\<CR>"', 'E486:')
1868
1869 exe "normal /\\a\\_.\\(.*\\)o\<CR>"
1870 call assert_equal([2, 1], [line('.'), col('.')])
1871
Bram Moolenaar224a5f12020-04-28 20:29:07 +02001872 " Test for using special atoms with 'smartcase'
1873 call setline(1, ['', ' Hello\ '])
1874 call cursor(1, 1)
1875 call feedkeys('/\_.\%(\uello\)\' .. "\<CR>", 'xt')
1876 call assert_equal([2, 4], [line('.'), col('.')])
1877
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001878 set ignorecase& smartcase&
1879 close!
Bram Moolenaard66cdcd2020-07-26 13:27:16 +02001880endfun
1881
1882" Test 'smartcase' with utf-8.
1883func Test_search_smartcase_utf8()
1884 new
1885 let save_enc = &encoding
1886 set encoding=utf8 ignorecase smartcase
1887
1888 call setline(1, 'Café cafÉ')
1889 1s/café/x/g
1890 call assert_equal('x x', getline(1))
1891
1892 call setline(1, 'Café cafÉ')
1893 1s/cafÉ/x/g
1894 call assert_equal('Café x', getline(1))
1895
1896 set ignorecase& smartcase&
1897 let &encoding = save_enc
1898 close!
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001899endfunc
1900
1901" Test searching past the end of a file
1902func Test_search_past_eof()
1903 new
1904 call setline(1, ['Line'])
1905 exe "normal /\\n\\zs\<CR>"
1906 call assert_equal([1, 4], [line('.'), col('.')])
1907 close!
1908endfunc
1909
Bram Moolenaar224a5f12020-04-28 20:29:07 +02001910" Test for various search offsets
1911func Test_search_offset()
1912 " With /e, for a match in the first column of a line, the cursor should be
1913 " placed at the end of the previous line.
1914 new
1915 call setline(1, ['one two', 'three four'])
1916 call search('two\_.', 'e')
1917 call assert_equal([1, 7], [line('.'), col('.')])
1918
1919 " with cursor at the beginning of the file, use /s+1
1920 call cursor(1, 1)
1921 exe "normal /two/s+1\<CR>"
1922 call assert_equal([1, 6], [line('.'), col('.')])
1923
1924 " with cursor at the end of the file, use /e-1
1925 call cursor(2, 10)
1926 exe "normal ?three?e-1\<CR>"
1927 call assert_equal([2, 4], [line('.'), col('.')])
1928
1929 " line offset - after the last line
1930 call cursor(1, 1)
1931 exe "normal /three/+1\<CR>"
1932 call assert_equal([2, 1], [line('.'), col('.')])
1933
1934 " line offset - before the first line
1935 call cursor(2, 1)
1936 exe "normal ?one?-1\<CR>"
1937 call assert_equal([1, 1], [line('.'), col('.')])
1938
1939 " character offset - before the first character in the file
1940 call cursor(2, 1)
1941 exe "normal ?one?s-1\<CR>"
1942 call assert_equal([1, 1], [line('.'), col('.')])
1943 call cursor(2, 1)
1944 exe "normal ?one?e-3\<CR>"
1945 call assert_equal([1, 1], [line('.'), col('.')])
1946
1947 " character offset - after the last character in the file
1948 call cursor(1, 1)
1949 exe "normal /four/s+4\<CR>"
1950 call assert_equal([2, 10], [line('.'), col('.')])
1951 call cursor(1, 1)
1952 exe "normal /four/e+1\<CR>"
1953 call assert_equal([2, 10], [line('.'), col('.')])
1954
1955 close!
1956endfunc
1957
1958" Test for searching for matching parenthesis using %
1959func Test_search_match_paren()
1960 new
1961 call setline(1, "abc(def')'ghi'('jk'\\t'lm)no")
1962 " searching for a matching parenthesis should skip single quoted characters
1963 call cursor(1, 4)
1964 normal %
1965 call assert_equal([1, 25], [line('.'), col('.')])
1966 normal %
1967 call assert_equal([1, 4], [line('.'), col('.')])
1968 call cursor(1, 5)
1969 normal ])
1970 call assert_equal([1, 25], [line('.'), col('.')])
1971 call cursor(1, 24)
1972 normal [(
1973 call assert_equal([1, 4], [line('.'), col('.')])
1974
1975 " matching parenthesis in 'virtualedit' mode with cursor after the eol
1976 call setline(1, 'abc(defgh)')
1977 set virtualedit=all
1978 normal 20|%
1979 call assert_equal(4, col('.'))
1980 set virtualedit&
1981 close!
1982endfunc
1983
1984" Test for searching a pattern and stopping before a specified line
1985func Test_search_stopline()
1986 new
1987 call setline(1, ['', '', '', 'vim'])
1988 call assert_equal(0, search('vim', 'n', 3))
1989 call assert_equal(4, search('vim', 'n', 4))
1990 call setline(1, ['vim', '', '', ''])
1991 call cursor(4, 1)
1992 call assert_equal(0, search('vim', 'bn', 2))
1993 call assert_equal(1, search('vim', 'bn', 1))
1994 close!
1995endfunc
1996
Bram Moolenaar6bed0db2020-11-25 17:41:20 +01001997func Test_incsearch_highlighting_newline()
Bram Moolenaar448465e2020-11-25 13:49:27 +01001998 CheckRunVimInTerminal
1999 CheckOption incsearch
2000 CheckScreendump
2001 new
2002 call test_override("char_avail", 1)
2003
2004 let commands =<< trim [CODE]
2005 set incsearch nohls
2006 call setline(1, ['test', 'xxx'])
2007 [CODE]
2008 call writefile(commands, 'Xincsearch_nl')
2009 let buf = RunVimInTerminal('-S Xincsearch_nl', {'rows': 5, 'cols': 10})
Bram Moolenaar448465e2020-11-25 13:49:27 +01002010 call term_sendkeys(buf, '/test')
Bram Moolenaar448465e2020-11-25 13:49:27 +01002011 call VerifyScreenDump(buf, 'Test_incsearch_newline1', {})
Bram Moolenaar6bed0db2020-11-25 17:41:20 +01002012 " Need to send one key at a time to force a redraw
Bram Moolenaar448465e2020-11-25 13:49:27 +01002013 call term_sendkeys(buf, '\n')
Bram Moolenaar448465e2020-11-25 13:49:27 +01002014 call VerifyScreenDump(buf, 'Test_incsearch_newline2', {})
2015 call term_sendkeys(buf, 'x')
Bram Moolenaar448465e2020-11-25 13:49:27 +01002016 call VerifyScreenDump(buf, 'Test_incsearch_newline3', {})
2017 call term_sendkeys(buf, 'x')
2018 call VerifyScreenDump(buf, 'Test_incsearch_newline4', {})
2019 call term_sendkeys(buf, "\<CR>")
Bram Moolenaar448465e2020-11-25 13:49:27 +01002020 call VerifyScreenDump(buf, 'Test_incsearch_newline5', {})
2021 call StopVimInTerminal(buf)
2022
2023 " clean up
2024 call delete('Xincsearch_nl')
2025 call test_override("char_avail", 0)
2026 bw
2027endfunc
2028
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01002029func Test_incsearch_substitute_dump2()
2030 CheckOption incsearch
2031 CheckScreendump
2032
2033 call writefile([
2034 \ 'set incsearch hlsearch scrolloff=0',
2035 \ 'for n in range(1, 4)',
2036 \ ' call setline(n, "foo " . n)',
2037 \ 'endfor',
2038 \ 'call setline(5, "abc|def")',
2039 \ '3',
2040 \ ], 'Xis_subst_script2')
2041 let buf = RunVimInTerminal('-S Xis_subst_script2', {'rows': 9, 'cols': 70})
2042
2043 call term_sendkeys(buf, ':%s/\vabc|')
2044 sleep 100m
2045 call VerifyScreenDump(buf, 'Test_incsearch_sub_01', {})
2046 call term_sendkeys(buf, "\<Esc>")
2047
2048 " The following should not be highlighted
2049 call term_sendkeys(buf, ':1,5s/\v|')
2050 sleep 100m
2051 call VerifyScreenDump(buf, 'Test_incsearch_sub_02', {})
2052
2053
2054 call StopVimInTerminal(buf)
2055 call delete('Xis_subst_script2')
2056endfunc
2057
Christian Brabandt78ba9332021-08-01 12:44:37 +02002058func Test_pattern_is_uppercase_smartcase()
2059 new
2060 let input=['abc', 'ABC', 'Abc', 'abC']
2061 call setline(1, input)
2062 call cursor(1,1)
2063 " default, matches firstline
2064 %s/abc//g
2065 call assert_equal(['', 'ABC', 'Abc', 'abC'],
2066 \ getline(1, '$'))
2067
2068 set smartcase ignorecase
2069 sil %d
2070 call setline(1, input)
2071 call cursor(1,1)
2072 " with smartcase and incsearch set, matches everything
2073 %s/abc//g
2074 call assert_equal(['', '', '', ''], getline(1, '$'))
2075
2076 sil %d
2077 call setline(1, input)
2078 call cursor(1,1)
2079 " with smartcase and incsearch set and found an uppercase letter,
2080 " match only that.
2081 %s/abC//g
2082 call assert_equal(['abc', 'ABC', 'Abc', ''],
2083 \ getline(1, '$'))
2084
2085 sil %d
2086 call setline(1, input)
2087 call cursor(1,1)
2088 exe "norm! vG$\<esc>"
2089 " \%V should not be detected as uppercase letter
2090 %s/\%Vabc//g
2091 call assert_equal(['', '', '', ''], getline(1, '$'))
2092
2093 call setline(1, input)
2094 call cursor(1,1)
2095 exe "norm! vG$\<esc>"
2096 " \v%V should not be detected as uppercase letter
2097 %s/\v%Vabc//g
2098 call assert_equal(['', '', '', ''], getline(1, '$'))
2099
2100 call setline(1, input)
2101 call cursor(1,1)
2102 exe "norm! vG$\<esc>"
2103 " \v%VabC should be detected as uppercase letter
2104 %s/\v%VabC//g
2105 call assert_equal(['abc', 'ABC', 'Abc', ''],
2106 \ getline(1, '$'))
2107
Christian Brabandtbc67e5a2021-08-05 15:24:59 +02002108 call setline(1, input)
2109 call cursor(1,1)
2110 " \Vabc should match everything
2111 %s/\Vabc//g
2112 call assert_equal(['', '', '', ''], getline(1, '$'))
2113
2114 call setline(1, input + ['_abc'])
2115 " _ matches normally
2116 %s/\v_.*//g
2117 call assert_equal(['abc', 'ABC', 'Abc', 'abC', ''], getline(1, '$'))
2118
Christian Brabandt78ba9332021-08-01 12:44:37 +02002119 set smartcase& ignorecase&
2120 bw!
2121endfunc
2122
Bram Moolenaard8d957d2021-10-04 19:47:35 +01002123func Test_no_last_search_pattern()
2124 CheckOption incsearch
2125
2126 let @/ = ""
2127 set incsearch
Bram Moolenaar9af9fd62021-10-04 20:09:19 +01002128 " these were causing a crash
2129 call feedkeys("//\<C-G>", 'xt')
2130 call feedkeys("//\<C-T>", 'xt')
2131 call feedkeys("??\<C-G>", 'xt')
2132 call feedkeys("??\<C-T>", 'xt')
Bram Moolenaard8d957d2021-10-04 19:47:35 +01002133endfunc
2134
Bram Moolenaar35a319b2021-10-09 13:58:55 +01002135func Test_search_with_invalid_range()
2136 new
2137 let lines =<< trim END
2138 /\%.v
2139 5/
2140 c
2141 END
2142 call writefile(lines, 'Xrangesearch')
2143 source Xrangesearch
2144
2145 bwipe!
2146 call delete('Xrangesearch')
2147endfunc
2148
Bram Moolenaard8d957d2021-10-04 19:47:35 +01002149
Bram Moolenaar07ada5f2020-02-05 20:38:22 +01002150" vim: shiftwidth=2 sts=2 expandtab