blob: ca06c34b47cb5f1b95f7fc983fdb3fb58c30196c [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 Moolenaar6e450a52017-01-06 20:03:58 +0100293 4
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100294 call assert_equal(3, searchpair('\[', '', ']', 'bW'))
295 call assert_equal([0, 3, 2, 0], getpos('.'))
296 4
297 call assert_equal(2, searchpair('\[', '', ']', 'bWr'))
298 call assert_equal([0, 2, 6, 0], getpos('.'))
299 4
300 call assert_equal(1, searchpair('\[', '', ']', 'bWm'))
301 call assert_equal([0, 3, 2, 0], getpos('.'))
302 4|norm ^
303 call assert_equal(5, searchpair('\[', '', ']', 'Wn'))
304 call assert_equal([0, 4, 2, 0], getpos('.'))
305 4
306 call assert_equal(2, searchpair('\[', '', ']', 'bW',
307 \ 'getline(".") =~ "^\\s*\["'))
308 call assert_equal([0, 2, 6, 0], getpos('.'))
Bram Moolenaar6e450a52017-01-06 20:03:58 +0100309 set nomagic
310 4
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100311 call assert_equal(3, searchpair('\[', '', ']', 'bW'))
312 call assert_equal([0, 3, 2, 0], getpos('.'))
Bram Moolenaar6e450a52017-01-06 20:03:58 +0100313 set magic
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100314 4|norm ^
315 call assert_equal(0, searchpair('{', '', '}', 'bW'))
316 call assert_equal([0, 4, 2, 0], getpos('.'))
317
318 %d
319 call setline(1, ['if 1', ' if 2', ' else', ' endif 2', 'endif 1'])
320
321 /\<if 1
322 call assert_equal(5, searchpair('\<if\>', '\<else\>', '\<endif\>', 'W'))
323 call assert_equal([0, 5, 1, 0], getpos('.'))
324 /\<if 2
325 call assert_equal(3, searchpair('\<if\>', '\<else\>', '\<endif\>', 'W'))
326 call assert_equal([0, 3, 3, 0], getpos('.'))
327
328 q!
329endfunc
330
331func Test_searchpairpos()
332 new
333 call setline(1, ['other code', 'here [', ' [', ' " cursor here', ' ]]'])
334
335 4
336 call assert_equal([3, 2], searchpairpos('\[', '', ']', 'bW'))
337 call assert_equal([0, 3, 2, 0], getpos('.'))
338 4
339 call assert_equal([2, 6], searchpairpos('\[', '', ']', 'bWr'))
340 call assert_equal([0, 2, 6, 0], getpos('.'))
341 4|norm ^
342 call assert_equal([5, 2], searchpairpos('\[', '', ']', 'Wn'))
343 call assert_equal([0, 4, 2, 0], getpos('.'))
344 4
345 call assert_equal([2, 6], searchpairpos('\[', '', ']', 'bW',
346 \ 'getline(".") =~ "^\\s*\["'))
347 call assert_equal([0, 2, 6, 0], getpos('.'))
348 4
349 call assert_equal([2, 6], searchpairpos('\[', '', ']', 'bWr'))
350 call assert_equal([0, 2, 6, 0], getpos('.'))
351 set nomagic
352 4
353 call assert_equal([3, 2], searchpairpos('\[', '', ']', 'bW'))
354 call assert_equal([0, 3, 2, 0], getpos('.'))
355 set magic
356 4|norm ^
357 call assert_equal([0, 0], searchpairpos('{', '', '}', 'bW'))
358 call assert_equal([0, 4, 2, 0], getpos('.'))
359
360 %d
361 call setline(1, ['if 1', ' if 2', ' else', ' endif 2', 'endif 1'])
362 /\<if 1
363 call assert_equal([5, 1], searchpairpos('\<if\>', '\<else\>', '\<endif\>', 'W'))
364 call assert_equal([0, 5, 1, 0], getpos('.'))
365 /\<if 2
366 call assert_equal([3, 3], searchpairpos('\<if\>', '\<else\>', '\<endif\>', 'W'))
367 call assert_equal([0, 3, 3, 0], getpos('.'))
368
Bram Moolenaar6e450a52017-01-06 20:03:58 +0100369 q!
370endfunc
371
Bram Moolenaar3dddb092018-06-24 19:01:59 +0200372func Test_searchpair_errors()
373 call assert_fails("call searchpair([0], 'middle', 'end', 'bW', 'skip', 99, 100)", 'E730: using List as a String')
374 call assert_fails("call searchpair('start', {-> 0}, 'end', 'bW', 'skip', 99, 100)", 'E729: using Funcref as a String')
375 call assert_fails("call searchpair('start', 'middle', {'one': 1}, 'bW', 'skip', 99, 100)", 'E731: using Dictionary as a String')
376 call assert_fails("call searchpair('start', 'middle', 'end', 'flags', 'skip', 99, 100)", 'E475: Invalid argument: flags')
Bram Moolenaar3dddb092018-06-24 19:01:59 +0200377 call assert_fails("call searchpair('start', 'middle', 'end', 'bW', 'func', -99, 100)", 'E475: Invalid argument: -99')
378 call assert_fails("call searchpair('start', 'middle', 'end', 'bW', 'func', 99, -100)", 'E475: Invalid argument: -100')
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100379 call assert_fails("call searchpair('start', 'middle', 'end', 'e')", 'E475: Invalid argument: e')
380 call assert_fails("call searchpair('start', 'middle', 'end', 'sn')", 'E475: Invalid argument: sn')
381endfunc
382
383func Test_searchpairpos_errors()
384 call assert_fails("call searchpairpos([0], 'middle', 'end', 'bW', 'skip', 99, 100)", 'E730: using List as a String')
385 call assert_fails("call searchpairpos('start', {-> 0}, 'end', 'bW', 'skip', 99, 100)", 'E729: using Funcref as a String')
386 call assert_fails("call searchpairpos('start', 'middle', {'one': 1}, 'bW', 'skip', 99, 100)", 'E731: using Dictionary as a String')
387 call assert_fails("call searchpairpos('start', 'middle', 'end', 'flags', 'skip', 99, 100)", 'E475: Invalid argument: flags')
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100388 call assert_fails("call searchpairpos('start', 'middle', 'end', 'bW', 'func', -99, 100)", 'E475: Invalid argument: -99')
389 call assert_fails("call searchpairpos('start', 'middle', 'end', 'bW', 'func', 99, -100)", 'E475: Invalid argument: -100')
390 call assert_fails("call searchpairpos('start', 'middle', 'end', 'e')", 'E475: Invalid argument: e')
391 call assert_fails("call searchpairpos('start', 'middle', 'end', 'sn')", 'E475: Invalid argument: sn')
Bram Moolenaar3dddb092018-06-24 19:01:59 +0200392endfunc
393
Bram Moolenaar48570482017-10-30 21:48:41 +0100394func Test_searchpair_skip()
395 func Zero()
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100396 return 0
Bram Moolenaar48570482017-10-30 21:48:41 +0100397 endfunc
398 func Partial(x)
Bram Moolenaar3ba35402019-12-21 22:00:50 +0100399 return a:x
Bram Moolenaar48570482017-10-30 21:48:41 +0100400 endfunc
401 new
402 call setline(1, ['{', 'foo', 'foo', 'foo', '}'])
403 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', ''))
404 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', '0'))
405 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', {-> 0}))
406 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', function('Zero')))
407 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', function('Partial', [0])))
Bram Moolenaar48570482017-10-30 21:48:41 +0100408 bw!
409endfunc
410
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200411func Test_searchpair_leak()
412 new
413 call setline(1, 'if one else another endif')
414
415 " The error in the skip expression caused memory to leak.
416 call assert_fails("call searchpair('\\<if\\>', '\\<else\\>', '\\<endif\\>', '', '\"foo\" 2')", 'E15:')
417
418 bwipe!
419endfunc
420
Bram Moolenaar66727e12017-03-01 22:17:05 +0100421func Test_searchc()
422 " These commands used to cause memory overflow in searchc().
423 new
424 norm ixx
425 exe "norm 0t\u93cf"
426 bw!
427endfunc
Bram Moolenaara693d052017-06-29 22:23:06 +0200428
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200429func Cmdline3_prep()
430 " need to disable char_avail,
431 " so that expansion of commandline works
432 call test_override("char_avail", 1)
433 new
434 call setline(1, [' 1', ' 2 the~e', ' 3 the theother'])
435 set incsearch
436endfunc
437
Bram Moolenaar976b8472018-08-12 15:49:47 +0200438func Incsearch_cleanup()
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200439 set noincsearch
440 call test_override("char_avail", 0)
441 bw!
442endfunc
443
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200444func Test_search_cmdline3()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100445 CheckOption incsearch
446
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200447 call Cmdline3_prep()
448 1
449 " first match
450 call feedkeys("/the\<c-l>\<cr>", 'tx')
451 call assert_equal(' 2 the~e', getline('.'))
452
Bram Moolenaar976b8472018-08-12 15:49:47 +0200453 call Incsearch_cleanup()
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200454endfunc
455
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200456func Test_search_cmdline3s()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100457 CheckOption incsearch
458
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200459 call Cmdline3_prep()
460 1
461 call feedkeys(":%s/the\<c-l>/xxx\<cr>", 'tx')
462 call assert_equal(' 2 xxxe', getline('.'))
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200463 undo
464 call feedkeys(":%subs/the\<c-l>/xxx\<cr>", 'tx')
465 call assert_equal(' 2 xxxe', getline('.'))
466 undo
467 call feedkeys(":%substitute/the\<c-l>/xxx\<cr>", 'tx')
468 call assert_equal(' 2 xxxe', getline('.'))
Bram Moolenaar167ae422018-08-14 21:32:21 +0200469 undo
470 call feedkeys(":%smagic/the.e/xxx\<cr>", 'tx')
471 call assert_equal(' 2 xxx', getline('.'))
472 undo
Bram Moolenaare2e40752020-09-04 21:18:46 +0200473 call assert_fails(":%snomagic/the.e/xxx\<cr>", 'E486:')
Bram Moolenaar167ae422018-08-14 21:32:21 +0200474 "
475 call feedkeys(":%snomagic/the\\.e/xxx\<cr>", 'tx')
476 call assert_equal(' 2 xxx', getline('.'))
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200477
Bram Moolenaar976b8472018-08-12 15:49:47 +0200478 call Incsearch_cleanup()
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200479endfunc
480
481func Test_search_cmdline3g()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100482 CheckOption incsearch
483
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200484 call Cmdline3_prep()
485 1
486 call feedkeys(":g/the\<c-l>/d\<cr>", 'tx')
487 call assert_equal(' 3 the theother', getline(2))
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200488 undo
489 call feedkeys(":global/the\<c-l>/d\<cr>", 'tx')
490 call assert_equal(' 3 the theother', getline(2))
Bram Moolenaardef7b1d2018-08-13 22:54:35 +0200491 undo
492 call feedkeys(":g!/the\<c-l>/d\<cr>", 'tx')
493 call assert_equal(1, line('$'))
494 call assert_equal(' 2 the~e', getline(1))
495 undo
496 call feedkeys(":global!/the\<c-l>/d\<cr>", 'tx')
497 call assert_equal(1, line('$'))
498 call assert_equal(' 2 the~e', getline(1))
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200499
Bram Moolenaar976b8472018-08-12 15:49:47 +0200500 call Incsearch_cleanup()
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200501endfunc
502
503func Test_search_cmdline3v()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100504 CheckOption incsearch
505
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200506 call Cmdline3_prep()
507 1
508 call feedkeys(":v/the\<c-l>/d\<cr>", 'tx')
509 call assert_equal(1, line('$'))
510 call assert_equal(' 2 the~e', getline(1))
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200511 undo
512 call feedkeys(":vglobal/the\<c-l>/d\<cr>", 'tx')
513 call assert_equal(1, line('$'))
514 call assert_equal(' 2 the~e', getline(1))
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200515
Bram Moolenaar976b8472018-08-12 15:49:47 +0200516 call Incsearch_cleanup()
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200517endfunc
518
Bram Moolenaarda5116d2017-07-01 23:11:17 +0200519func Test_search_cmdline4()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100520 CheckOption incsearch
521
Bram Moolenaarda5116d2017-07-01 23:11:17 +0200522 " need to disable char_avail,
523 " so that expansion of commandline works
524 call test_override("char_avail", 1)
525 new
526 call setline(1, [' 1 the first', ' 2 the second', ' 3 the third'])
527 set incsearch
528 $
529 call feedkeys("?the\<c-g>\<cr>", 'tx')
530 call assert_equal(' 3 the third', getline('.'))
531 $
532 call feedkeys("?the\<c-g>\<c-g>\<cr>", 'tx')
533 call assert_equal(' 1 the first', getline('.'))
534 $
535 call feedkeys("?the\<c-g>\<c-g>\<c-g>\<cr>", 'tx')
536 call assert_equal(' 2 the second', getline('.'))
537 $
538 call feedkeys("?the\<c-t>\<cr>", 'tx')
539 call assert_equal(' 1 the first', getline('.'))
540 $
541 call feedkeys("?the\<c-t>\<c-t>\<cr>", 'tx')
542 call assert_equal(' 3 the third', getline('.'))
543 $
544 call feedkeys("?the\<c-t>\<c-t>\<c-t>\<cr>", 'tx')
545 call assert_equal(' 2 the second', getline('.'))
546 " clean up
547 set noincsearch
548 call test_override("char_avail", 0)
549 bw!
550endfunc
Bram Moolenaardb510072017-09-28 21:52:17 +0200551
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200552func Test_search_cmdline5()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100553 CheckOption incsearch
554
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200555 " Do not call test_override("char_avail", 1) so that <C-g> and <C-t> work
556 " regardless char_avail.
557 new
Bram Moolenaar69a5b862019-07-16 21:38:51 +0200558 call setline(1, [' 1 the first', ' 2 the second', ' 3 the third', ''])
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200559 set incsearch
560 1
561 call feedkeys("/the\<c-g>\<c-g>\<cr>", 'tx')
562 call assert_equal(' 3 the third', getline('.'))
563 $
564 call feedkeys("?the\<c-t>\<c-t>\<c-t>\<cr>", 'tx')
Bram Moolenaar69a5b862019-07-16 21:38:51 +0200565 call assert_equal(' 1 the first', getline('.'))
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200566 " clean up
567 set noincsearch
568 bw!
569endfunc
570
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100571func Test_search_cmdline6()
572 " Test that consecutive matches
573 " are caught by <c-g>/<c-t>
Bram Moolenaarb68df222020-03-19 15:05:28 +0100574 CheckOption incsearch
575
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100576 " need to disable char_avail,
577 " so that expansion of commandline works
578 call test_override("char_avail", 1)
579 new
580 call setline(1, [' bbvimb', ''])
581 set incsearch
582 " first match
583 norm! gg0
584 call feedkeys("/b\<cr>", 'tx')
585 call assert_equal([0,1,2,0], getpos('.'))
586 " second match
587 norm! gg0
588 call feedkeys("/b\<c-g>\<cr>", 'tx')
589 call assert_equal([0,1,3,0], getpos('.'))
590 " third match
591 norm! gg0
592 call feedkeys("/b\<c-g>\<c-g>\<cr>", 'tx')
593 call assert_equal([0,1,7,0], getpos('.'))
594 " first match again
595 norm! gg0
596 call feedkeys("/b\<c-g>\<c-g>\<c-g>\<cr>", 'tx')
597 call assert_equal([0,1,2,0], getpos('.'))
598 set nowrapscan
599 " last match
600 norm! gg0
601 call feedkeys("/b\<c-g>\<c-g>\<c-g>\<cr>", 'tx')
602 call assert_equal([0,1,7,0], getpos('.'))
603 " clean up
604 set wrapscan&vim
605 set noincsearch
606 call test_override("char_avail", 0)
607 bw!
608endfunc
609
610func Test_search_cmdline7()
Bram Moolenaard66cdcd2020-07-26 13:27:16 +0200611 " Test that pressing <c-g> in an empty command line
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100612 " does not move the cursor
Bram Moolenaarb68df222020-03-19 15:05:28 +0100613 CheckOption incsearch
614
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100615 " need to disable char_avail,
616 " so that expansion of commandline works
617 call test_override("char_avail", 1)
618 new
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200619 let @/ = 'b'
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100620 call setline(1, [' bbvimb', ''])
621 set incsearch
622 " first match
623 norm! gg0
624 " moves to next match of previous search pattern, just like /<cr>
625 call feedkeys("/\<c-g>\<cr>", 'tx')
626 call assert_equal([0,1,2,0], getpos('.'))
627 " moves to next match of previous search pattern, just like /<cr>
628 call feedkeys("/\<cr>", 'tx')
629 call assert_equal([0,1,3,0], getpos('.'))
630 " moves to next match of previous search pattern, just like /<cr>
631 call feedkeys("/\<c-t>\<cr>", 'tx')
632 call assert_equal([0,1,7,0], getpos('.'))
Bram Moolenaard0480092017-11-16 22:20:39 +0100633
634 " using an offset uses the last search pattern
635 call cursor(1, 1)
636 call setline(1, ['1 bbvimb', ' 2 bbvimb'])
637 let @/ = 'b'
638 call feedkeys("//e\<c-g>\<cr>", 'tx')
639 call assert_equal('1 bbvimb', getline('.'))
640 call assert_equal(4, col('.'))
641
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100642 set noincsearch
643 call test_override("char_avail", 0)
644 bw!
645endfunc
646
647func Test_search_cmdline8()
648 " Highlighting is cleared in all windows
649 " since hls applies to all windows
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200650 CheckOption incsearch
651 CheckFeature terminal
652 CheckNotGui
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100653 if has("win32")
654 throw "Skipped: Bug with sending <ESC> to terminal window not fixed yet"
655 endif
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200656
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100657 let h = winheight(0)
658 if h < 3
659 return
660 endif
661 " Prepare buffer text
662 let lines = ['abb vim vim vi', 'vimvivim']
663 call writefile(lines, 'Xsearch.txt')
Bram Moolenaar13deab82017-11-04 18:48:43 +0100664 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', 'Xsearch.txt'], {'term_rows': 3})
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100665
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200666 call WaitForAssert({-> assert_equal(lines, [term_getline(buf, 1), term_getline(buf, 2)])})
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100667
Bram Moolenaar13deab82017-11-04 18:48:43 +0100668 call term_sendkeys(buf, ":set incsearch hlsearch\<cr>")
669 call term_sendkeys(buf, ":14vsp\<cr>")
670 call term_sendkeys(buf, "/vim\<cr>")
671 call term_sendkeys(buf, "/b\<esc>")
672 call term_sendkeys(buf, "gg0")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200673 call TermWait(buf, 250)
Bram Moolenaar13deab82017-11-04 18:48:43 +0100674 let screen_line = term_scrape(buf, 1)
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100675 let [a0,a1,a2,a3] = [screen_line[3].attr, screen_line[4].attr,
676 \ screen_line[18].attr, screen_line[19].attr]
677 call assert_notequal(a0, a1)
678 call assert_notequal(a0, a3)
679 call assert_notequal(a1, a2)
680 call assert_equal(a0, a2)
681 call assert_equal(a1, a3)
682 " clean up
683 call delete('Xsearch.txt')
684
685 bwipe!
686endfunc
687
Bram Moolenaardb510072017-09-28 21:52:17 +0200688" Tests for regexp with various magic settings
Bram Moolenaar4d23c522020-04-09 18:42:11 +0200689func Run_search_regexp_magic_opt()
Bram Moolenaardb510072017-09-28 21:52:17 +0200690 put ='1 a aa abb abbccc'
691 exe 'normal! /a*b\{2}c\+/e' . "\<CR>"
692 call assert_equal([0, 2, 17, 0], getpos('.'))
693
694 put ='2 d dd dee deefff'
695 exe 'normal! /\Md\*e\{2}f\+/e' . "\<CR>"
696 call assert_equal([0, 3, 17, 0], getpos('.'))
697
698 set nomagic
699 put ='3 g gg ghh ghhiii'
700 exe 'normal! /g\*h\{2}i\+/e' . "\<CR>"
701 call assert_equal([0, 4, 17, 0], getpos('.'))
702
703 put ='4 j jj jkk jkklll'
704 exe 'normal! /\mj*k\{2}l\+/e' . "\<CR>"
705 call assert_equal([0, 5, 17, 0], getpos('.'))
706
707 put ='5 m mm mnn mnnooo'
708 exe 'normal! /\vm*n{2}o+/e' . "\<CR>"
709 call assert_equal([0, 6, 17, 0], getpos('.'))
710
711 put ='6 x ^aa$ x'
712 exe 'normal! /\V^aa$' . "\<CR>"
713 call assert_equal([0, 7, 5, 0], getpos('.'))
714
715 set magic
716 put ='7 (a)(b) abbaa'
717 exe 'normal! /\v(a)(b)\2\1\1/e' . "\<CR>"
718 call assert_equal([0, 8, 14, 0], getpos('.'))
719
720 put ='8 axx [ab]xx'
721 exe 'normal! /\V[ab]\(\[xy]\)\1' . "\<CR>"
722 call assert_equal([0, 9, 7, 0], getpos('.'))
723
Bram Moolenaar4d23c522020-04-09 18:42:11 +0200724 %d
725endfunc
726
727func Test_search_regexp()
728 enew!
729
730 set regexpengine=1
731 call Run_search_regexp_magic_opt()
732 set regexpengine=2
733 call Run_search_regexp_magic_opt()
734 set regexpengine&
735
Bram Moolenaardb510072017-09-28 21:52:17 +0200736 set undolevels=100
737 put ='9 foobar'
738 put =''
739 exe "normal! a\<C-G>u\<Esc>"
740 normal G
741 exe 'normal! dv?bar?' . "\<CR>"
742 call assert_equal('9 foo', getline('.'))
Bram Moolenaar4d23c522020-04-09 18:42:11 +0200743 call assert_equal([0, 2, 5, 0], getpos('.'))
744 call assert_equal(2, line('$'))
Bram Moolenaardb510072017-09-28 21:52:17 +0200745 normal u
746 call assert_equal('9 foobar', getline('.'))
Bram Moolenaar4d23c522020-04-09 18:42:11 +0200747 call assert_equal([0, 2, 6, 0], getpos('.'))
748 call assert_equal(3, line('$'))
Bram Moolenaardb510072017-09-28 21:52:17 +0200749
750 set undolevels&
751 enew!
752endfunc
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100753
754func Test_search_cmdline_incsearch_highlight()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100755 CheckOption incsearch
756
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100757 set incsearch hlsearch
758 " need to disable char_avail,
759 " so that expansion of commandline works
760 call test_override("char_avail", 1)
761 new
762 call setline(1, ['aaa 1 the first', ' 2 the second', ' 3 the third'])
763
764 1
765 call feedkeys("/second\<cr>", 'tx')
766 call assert_equal('second', @/)
767 call assert_equal(' 2 the second', getline('.'))
768
769 " Canceling search won't change @/
770 1
771 let @/ = 'last pattern'
772 call feedkeys("/third\<C-c>", 'tx')
773 call assert_equal('last pattern', @/)
774 call feedkeys("/third\<Esc>", 'tx')
775 call assert_equal('last pattern', @/)
776 call feedkeys("/3\<bs>\<bs>", 'tx')
777 call assert_equal('last pattern', @/)
778 call feedkeys("/third\<c-g>\<c-t>\<Esc>", 'tx')
779 call assert_equal('last pattern', @/)
780
781 " clean up
782 set noincsearch nohlsearch
783 bw!
784endfunc
785
786func Test_search_cmdline_incsearch_highlight_attr()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200787 CheckOption incsearch
788 CheckFeature terminal
789 CheckNotGui
790
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100791 let h = winheight(0)
792 if h < 3
793 return
794 endif
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100795
796 " Prepare buffer text
Bram Moolenaar13deab82017-11-04 18:48:43 +0100797 let lines = ['abb vim vim vi', 'vimvivim']
798 call writefile(lines, 'Xsearch.txt')
799 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', 'Xsearch.txt'], {'term_rows': 3})
800
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200801 call WaitForAssert({-> assert_equal(lines, [term_getline(buf, 1), term_getline(buf, 2)])})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100802 " wait for vim to complete initialization
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200803 call TermWait(buf)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100804
805 " Get attr of normal(a0), incsearch(a1), hlsearch(a2) highlight
Bram Moolenaar13deab82017-11-04 18:48:43 +0100806 call term_sendkeys(buf, ":set incsearch hlsearch\<cr>")
807 call term_sendkeys(buf, '/b')
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200808 call TermWait(buf, 100)
Bram Moolenaar13deab82017-11-04 18:48:43 +0100809 let screen_line1 = term_scrape(buf, 1)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100810 call assert_true(len(screen_line1) > 2)
811 " a0: attr_normal
812 let a0 = screen_line1[0].attr
813 " a1: attr_incsearch
814 let a1 = screen_line1[1].attr
815 " a2: attr_hlsearch
816 let a2 = screen_line1[2].attr
817 call assert_notequal(a0, a1)
818 call assert_notequal(a0, a2)
819 call assert_notequal(a1, a2)
Bram Moolenaar13deab82017-11-04 18:48:43 +0100820 call term_sendkeys(buf, "\<cr>gg0")
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100821
822 " Test incremental highlight search
Bram Moolenaar13deab82017-11-04 18:48:43 +0100823 call term_sendkeys(buf, "/vim")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200824 call TermWait(buf, 100)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100825 " Buffer:
826 " abb vim vim vi
827 " vimvivim
828 " Search: /vim
829 let attr_line1 = [a0,a0,a0,a0,a1,a1,a1,a0,a2,a2,a2,a0,a0,a0]
830 let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100831 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
832 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100833
834 " Test <C-g>
Bram Moolenaar13deab82017-11-04 18:48:43 +0100835 call term_sendkeys(buf, "\<C-g>\<C-g>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200836 call TermWait(buf, 100)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100837 let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a2,a2,a2,a0,a0,a0]
838 let attr_line2 = [a1,a1,a1,a0,a0,a2,a2,a2]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100839 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
840 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100841
842 " Test <C-t>
Bram Moolenaar13deab82017-11-04 18:48:43 +0100843 call term_sendkeys(buf, "\<C-t>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200844 call TermWait(buf, 100)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100845 let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a1,a1,a1,a0,a0,a0]
846 let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100847 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
848 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100849
850 " Type Enter and a1(incsearch highlight) should become a2(hlsearch highlight)
Bram Moolenaar13deab82017-11-04 18:48:43 +0100851 call term_sendkeys(buf, "\<cr>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200852 call TermWait(buf, 100)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100853 let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a2,a2,a2,a0,a0,a0]
854 let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100855 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
856 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100857
858 " Test nohlsearch. a2(hlsearch highlight) should become a0(normal highlight)
Bram Moolenaar13deab82017-11-04 18:48:43 +0100859 call term_sendkeys(buf, ":1\<cr>")
860 call term_sendkeys(buf, ":set nohlsearch\<cr>")
861 call term_sendkeys(buf, "/vim")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200862 call TermWait(buf, 100)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100863 let attr_line1 = [a0,a0,a0,a0,a1,a1,a1,a0,a0,a0,a0,a0,a0,a0]
864 let attr_line2 = [a0,a0,a0,a0,a0,a0,a0,a0]
Bram Moolenaar13deab82017-11-04 18:48:43 +0100865 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr'))
866 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr'))
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +0100867 call delete('Xsearch.txt')
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100868
Bram Moolenaarb94340c2017-11-02 16:16:31 +0100869 call delete('Xsearch.txt')
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100870 bwipe!
871endfunc
Bram Moolenaarf45938c2017-11-02 15:59:57 +0100872
Bram Moolenaar548e5982018-12-26 21:45:00 +0100873func Test_incsearch_cmdline_modifier()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100874 CheckOption incsearch
875
Bram Moolenaar548e5982018-12-26 21:45:00 +0100876 call test_override("char_avail", 1)
877 new
878 call setline(1, ['foo'])
879 set incsearch
880 " Test that error E14 does not occur in parsing command modifier.
881 call feedkeys("V:tab", 'tx')
882
883 call Incsearch_cleanup()
884endfunc
885
Bram Moolenaar9d34d902018-04-27 22:18:12 +0200886func Test_incsearch_scrolling()
Bram Moolenaar494e9062020-05-31 21:28:02 +0200887 CheckRunVimInTerminal
Bram Moolenaar9d34d902018-04-27 22:18:12 +0200888 call assert_equal(0, &scrolloff)
889 call writefile([
890 \ 'let dots = repeat(".", 120)',
891 \ 'set incsearch cmdheight=2 scrolloff=0',
892 \ 'call setline(1, [dots, dots, dots, "", "target", dots, dots])',
893 \ 'normal gg',
894 \ 'redraw',
895 \ ], 'Xscript')
896 let buf = RunVimInTerminal('-S Xscript', {'rows': 9, 'cols': 70})
897 " Need to send one key at a time to force a redraw
898 call term_sendkeys(buf, '/')
899 sleep 100m
900 call term_sendkeys(buf, 't')
901 sleep 100m
902 call term_sendkeys(buf, 'a')
903 sleep 100m
904 call term_sendkeys(buf, 'r')
905 sleep 100m
906 call term_sendkeys(buf, 'g')
907 call VerifyScreenDump(buf, 'Test_incsearch_scrolling_01', {})
908
909 call term_sendkeys(buf, "\<Esc>")
910 call StopVimInTerminal(buf)
911 call delete('Xscript')
912endfunc
913
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200914func Test_incsearch_search_dump()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100915 CheckOption incsearch
916 CheckScreendump
917
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200918 call writefile([
919 \ 'set incsearch hlsearch scrolloff=0',
920 \ 'for n in range(1, 8)',
921 \ ' call setline(n, "foo " . n)',
922 \ 'endfor',
923 \ '3',
924 \ ], 'Xis_search_script')
925 let buf = RunVimInTerminal('-S Xis_search_script', {'rows': 9, 'cols': 70})
926 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
927 " the 'ambiwidth' check.
928 sleep 100m
929
930 " Need to send one key at a time to force a redraw.
931 call term_sendkeys(buf, '/fo')
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200932 call VerifyScreenDump(buf, 'Test_incsearch_search_01', {})
933 call term_sendkeys(buf, "\<Esc>")
934 sleep 100m
935
936 call term_sendkeys(buf, '/\v')
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200937 call VerifyScreenDump(buf, 'Test_incsearch_search_02', {})
938 call term_sendkeys(buf, "\<Esc>")
939
940 call StopVimInTerminal(buf)
941 call delete('Xis_search_script')
942endfunc
943
Bram Moolenaar976b8472018-08-12 15:49:47 +0200944func Test_incsearch_substitute()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100945 CheckOption incsearch
946
Bram Moolenaar976b8472018-08-12 15:49:47 +0200947 call test_override("char_avail", 1)
948 new
949 set incsearch
950 for n in range(1, 10)
951 call setline(n, 'foo ' . n)
952 endfor
953 4
954 call feedkeys(":.,.+2s/foo\<BS>o\<BS>o/xxx\<cr>", 'tx')
955 call assert_equal('foo 3', getline(3))
956 call assert_equal('xxx 4', getline(4))
957 call assert_equal('xxx 5', getline(5))
958 call assert_equal('xxx 6', getline(6))
959 call assert_equal('foo 7', getline(7))
960
961 call Incsearch_cleanup()
962endfunc
963
Bram Moolenaar164251f2018-08-12 16:26:58 +0200964" Similar to Test_incsearch_substitute() but with a screendump halfway.
965func Test_incsearch_substitute_dump()
Bram Moolenaarb68df222020-03-19 15:05:28 +0100966 CheckOption incsearch
967 CheckScreendump
968
Bram Moolenaar164251f2018-08-12 16:26:58 +0200969 call writefile([
970 \ 'set incsearch hlsearch scrolloff=0',
971 \ 'for n in range(1, 10)',
972 \ ' call setline(n, "foo " . n)',
973 \ 'endfor',
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200974 \ 'call setline(11, "bar 11")',
Bram Moolenaar164251f2018-08-12 16:26:58 +0200975 \ '3',
976 \ ], 'Xis_subst_script')
977 let buf = RunVimInTerminal('-S Xis_subst_script', {'rows': 9, 'cols': 70})
978 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
979 " the 'ambiwidth' check.
980 sleep 100m
981
982 " Need to send one key at a time to force a redraw.
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200983 " Select three lines at the cursor with typed pattern.
Bram Moolenaar164251f2018-08-12 16:26:58 +0200984 call term_sendkeys(buf, ':.,.+2s/')
985 sleep 100m
986 call term_sendkeys(buf, 'f')
987 sleep 100m
988 call term_sendkeys(buf, 'o')
989 sleep 100m
990 call term_sendkeys(buf, 'o')
991 call VerifyScreenDump(buf, 'Test_incsearch_substitute_01', {})
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200992 call term_sendkeys(buf, "\<Esc>")
993
994 " Select three lines at the cursor using previous pattern.
995 call term_sendkeys(buf, "/foo\<CR>")
996 sleep 100m
997 call term_sendkeys(buf, ':.,.+2s//')
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200998 call VerifyScreenDump(buf, 'Test_incsearch_substitute_02', {})
999
1000 " Deleting last slash should remove the match.
1001 call term_sendkeys(buf, "\<BS>")
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02001002 call VerifyScreenDump(buf, 'Test_incsearch_substitute_03', {})
Bram Moolenaar60d08712018-08-12 21:53:15 +02001003 call term_sendkeys(buf, "\<Esc>")
1004
1005 " Reverse range is accepted
1006 call term_sendkeys(buf, ':5,2s/foo')
Bram Moolenaar60d08712018-08-12 21:53:15 +02001007 call VerifyScreenDump(buf, 'Test_incsearch_substitute_04', {})
Bram Moolenaar164251f2018-08-12 16:26:58 +02001008 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaar2b926fc2018-08-13 11:07:57 +02001009
1010 " White space after the command is skipped
1011 call term_sendkeys(buf, ':2,3sub /fo')
Bram Moolenaar2b926fc2018-08-13 11:07:57 +02001012 call VerifyScreenDump(buf, 'Test_incsearch_substitute_05', {})
1013 call term_sendkeys(buf, "\<Esc>")
1014
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001015 " Command modifiers are skipped
1016 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 +02001017 call VerifyScreenDump(buf, 'Test_incsearch_substitute_06', {})
1018 call term_sendkeys(buf, "\<Esc>")
1019
Bram Moolenaar2f6a3462018-08-14 18:16:52 +02001020 " Cursorline highlighting at match
1021 call term_sendkeys(buf, ":set cursorline\<CR>")
1022 call term_sendkeys(buf, 'G9G')
1023 call term_sendkeys(buf, ':9,11s/bar')
Bram Moolenaar2f6a3462018-08-14 18:16:52 +02001024 call VerifyScreenDump(buf, 'Test_incsearch_substitute_07', {})
1025 call term_sendkeys(buf, "\<Esc>")
1026
1027 " Cursorline highlighting at cursor when no match
1028 call term_sendkeys(buf, ':9,10s/bar')
Bram Moolenaar2f6a3462018-08-14 18:16:52 +02001029 call VerifyScreenDump(buf, 'Test_incsearch_substitute_08', {})
1030 call term_sendkeys(buf, "\<Esc>")
1031
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +02001032 " Only \v handled as empty pattern, does not move cursor
1033 call term_sendkeys(buf, '3G4G')
1034 call term_sendkeys(buf, ":nohlsearch\<CR>")
1035 call term_sendkeys(buf, ':6,7s/\v')
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +02001036 call VerifyScreenDump(buf, 'Test_incsearch_substitute_09', {})
1037 call term_sendkeys(buf, "\<Esc>")
1038
Bram Moolenaarf13daa42018-08-31 22:09:54 +02001039 call term_sendkeys(buf, ":set nocursorline\<CR>")
1040
1041 " All matches are highlighted for 'hlsearch' after the incsearch canceled
1042 call term_sendkeys(buf, "1G*")
1043 call term_sendkeys(buf, ":2,5s/foo")
1044 sleep 100m
1045 call term_sendkeys(buf, "\<Esc>")
1046 call VerifyScreenDump(buf, 'Test_incsearch_substitute_10', {})
1047
Bram Moolenaar65985ac2018-09-16 17:08:04 +02001048 call term_sendkeys(buf, ":split\<CR>")
1049 call term_sendkeys(buf, ":let @/ = 'xyz'\<CR>")
1050 call term_sendkeys(buf, ":%s/.")
1051 call VerifyScreenDump(buf, 'Test_incsearch_substitute_11', {})
1052 call term_sendkeys(buf, "\<BS>")
1053 call VerifyScreenDump(buf, 'Test_incsearch_substitute_12', {})
1054 call term_sendkeys(buf, "\<Esc>")
1055 call VerifyScreenDump(buf, 'Test_incsearch_substitute_13', {})
Bram Moolenaarc6725252019-11-23 21:56:46 +01001056 call term_sendkeys(buf, ":%bwipe!\<CR>")
1057 call term_sendkeys(buf, ":only!\<CR>")
1058
1059 " get :'<,'>s command in history
1060 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
1061 call term_sendkeys(buf, "aasdfasdf\<Esc>")
1062 call term_sendkeys(buf, "V:s/a/b/g\<CR>")
1063 " Using '<,'> does not give E20
1064 call term_sendkeys(buf, ":new\<CR>")
1065 call term_sendkeys(buf, "aasdfasdf\<Esc>")
1066 call term_sendkeys(buf, ":\<Up>\<Up>")
1067 call VerifyScreenDump(buf, 'Test_incsearch_substitute_14', {})
1068 call term_sendkeys(buf, "<Esc>")
Bram Moolenaar65985ac2018-09-16 17:08:04 +02001069
Bram Moolenaar164251f2018-08-12 16:26:58 +02001070 call StopVimInTerminal(buf)
1071 call delete('Xis_subst_script')
1072endfunc
1073
Bram Moolenaarc036e872020-02-21 21:30:52 +01001074func Test_incsearch_highlighting()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001075 CheckOption incsearch
1076 CheckScreendump
Bram Moolenaarc036e872020-02-21 21:30:52 +01001077
1078 call writefile([
1079 \ 'set incsearch hlsearch',
1080 \ 'call setline(1, "hello/there")',
1081 \ ], 'Xis_subst_hl_script')
1082 let buf = RunVimInTerminal('-S Xis_subst_hl_script', {'rows': 4, 'cols': 20})
1083 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1084 " the 'ambiwidth' check.
1085 sleep 300m
1086
1087 " Using a different search delimiter should still highlight matches
1088 " that contain a '/'.
1089 call term_sendkeys(buf, ":%s;ello/the")
1090 call VerifyScreenDump(buf, 'Test_incsearch_substitute_15', {})
1091 call term_sendkeys(buf, "<Esc>")
Bram Moolenaarb68df222020-03-19 15:05:28 +01001092
1093 call StopVimInTerminal(buf)
1094 call delete('Xis_subst_hl_script')
Bram Moolenaarc036e872020-02-21 21:30:52 +01001095endfunc
1096
Bram Moolenaar4a7d2d32019-02-21 16:25:50 +01001097func Test_incsearch_with_change()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001098 CheckFeature timers
1099 CheckOption incsearch
1100 CheckScreendump
Bram Moolenaar4a7d2d32019-02-21 16:25:50 +01001101
1102 call writefile([
1103 \ 'set incsearch hlsearch scrolloff=0',
1104 \ 'call setline(1, ["one", "two ------ X", "three"])',
1105 \ 'call timer_start(200, { _ -> setline(2, "x")})',
1106 \ ], 'Xis_change_script')
1107 let buf = RunVimInTerminal('-S Xis_change_script', {'rows': 9, 'cols': 70})
1108 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1109 " the 'ambiwidth' check.
Bram Moolenaare86ecbd2019-02-21 17:48:59 +01001110 sleep 300m
Bram Moolenaar4a7d2d32019-02-21 16:25:50 +01001111
1112 " Highlight X, it will be deleted by the timer callback.
1113 call term_sendkeys(buf, ':%s/X')
1114 call VerifyScreenDump(buf, 'Test_incsearch_change_01', {})
1115 call term_sendkeys(buf, "\<Esc>")
1116
1117 call StopVimInTerminal(buf)
1118 call delete('Xis_change_script')
1119endfunc
1120
Bram Moolenaar81f56532018-08-18 16:19:42 +02001121" Similar to Test_incsearch_substitute_dump() for :sort
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +02001122func Test_incsearch_sort_dump()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001123 CheckOption incsearch
1124 CheckScreendump
1125
Bram Moolenaar81f56532018-08-18 16:19:42 +02001126 call writefile([
1127 \ 'set incsearch hlsearch scrolloff=0',
1128 \ 'call setline(1, ["another one 2", "that one 3", "the one 1"])',
1129 \ ], 'Xis_sort_script')
1130 let buf = RunVimInTerminal('-S Xis_sort_script', {'rows': 9, 'cols': 70})
1131 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1132 " the 'ambiwidth' check.
1133 sleep 100m
1134
Bram Moolenaar81f56532018-08-18 16:19:42 +02001135 call term_sendkeys(buf, ':sort ni u /on')
Bram Moolenaar81f56532018-08-18 16:19:42 +02001136 call VerifyScreenDump(buf, 'Test_incsearch_sort_01', {})
1137 call term_sendkeys(buf, "\<Esc>")
1138
Bram Moolenaar333015a2020-04-25 15:54:16 +02001139 call term_sendkeys(buf, ':sort! /on')
1140 call VerifyScreenDump(buf, 'Test_incsearch_sort_02', {})
1141 call term_sendkeys(buf, "\<Esc>")
1142
Bram Moolenaar81f56532018-08-18 16:19:42 +02001143 call StopVimInTerminal(buf)
1144 call delete('Xis_sort_script')
1145endfunc
1146
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001147" Similar to Test_incsearch_substitute_dump() for :vimgrep famiry
1148func Test_incsearch_vimgrep_dump()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001149 CheckOption incsearch
1150 CheckScreendump
1151
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001152 call writefile([
1153 \ 'set incsearch hlsearch scrolloff=0',
1154 \ 'call setline(1, ["another one 2", "that one 3", "the one 1"])',
1155 \ ], 'Xis_vimgrep_script')
1156 let buf = RunVimInTerminal('-S Xis_vimgrep_script', {'rows': 9, 'cols': 70})
1157 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by
1158 " the 'ambiwidth' check.
1159 sleep 100m
1160
1161 " Need to send one key at a time to force a redraw.
1162 call term_sendkeys(buf, ':vimgrep on')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001163 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_01', {})
1164 call term_sendkeys(buf, "\<Esc>")
1165
1166 call term_sendkeys(buf, ':vimg /on/ *.txt')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001167 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_02', {})
1168 call term_sendkeys(buf, "\<Esc>")
1169
1170 call term_sendkeys(buf, ':vimgrepadd "\<on')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001171 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_03', {})
1172 call term_sendkeys(buf, "\<Esc>")
1173
1174 call term_sendkeys(buf, ':lv "tha')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001175 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_04', {})
1176 call term_sendkeys(buf, "\<Esc>")
1177
1178 call term_sendkeys(buf, ':lvimgrepa "the" **/*.txt')
Bram Moolenaar264cf5c2018-08-18 21:05:31 +02001179 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_05', {})
1180 call term_sendkeys(buf, "\<Esc>")
1181
1182 call StopVimInTerminal(buf)
1183 call delete('Xis_vimgrep_script')
1184endfunc
1185
Bram Moolenaar198cb662018-09-06 21:44:17 +02001186func Test_keep_last_search_pattern()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001187 CheckOption incsearch
1188
Bram Moolenaar198cb662018-09-06 21:44:17 +02001189 new
1190 call setline(1, ['foo', 'foo', 'foo'])
1191 set incsearch
1192 call test_override("char_avail", 1)
1193 let @/ = 'bar'
1194 call feedkeys(":/foo/s//\<Esc>", 'ntx')
1195 call assert_equal('bar', @/)
1196
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001197 " no error message if pattern not found
1198 call feedkeys(":/xyz/s//\<Esc>", 'ntx')
1199 call assert_equal('bar', @/)
1200
Bram Moolenaar198cb662018-09-06 21:44:17 +02001201 bwipe!
1202 call test_override("ALL", 0)
1203 set noincsearch
1204endfunc
1205
Bram Moolenaar99f043a2018-09-09 15:54:14 +02001206func Test_word_under_cursor_after_match()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001207 CheckOption incsearch
1208
Bram Moolenaar99f043a2018-09-09 15:54:14 +02001209 new
1210 call setline(1, 'foo bar')
1211 set incsearch
1212 call test_override("char_avail", 1)
1213 try
1214 call feedkeys("/foo\<C-R>\<C-W>\<CR>", 'ntx')
1215 catch /E486:/
1216 endtry
1217 call assert_equal('foobar', @/)
1218
1219 bwipe!
1220 call test_override("ALL", 0)
1221 set noincsearch
1222endfunc
1223
1224func Test_subst_word_under_cursor()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001225 CheckOption incsearch
1226
Bram Moolenaar99f043a2018-09-09 15:54:14 +02001227 new
1228 call setline(1, ['int SomeLongName;', 'for (xxx = 1; xxx < len; ++xxx)'])
1229 set incsearch
1230 call test_override("char_avail", 1)
1231 call feedkeys("/LongName\<CR>", 'ntx')
1232 call feedkeys(":%s/xxx/\<C-R>\<C-W>/g\<CR>", 'ntx')
1233 call assert_equal('for (SomeLongName = 1; SomeLongName < len; ++SomeLongName)', getline(2))
1234
1235 bwipe!
1236 call test_override("ALL", 0)
1237 set noincsearch
1238endfunc
1239
Bram Moolenaarf45938c2017-11-02 15:59:57 +01001240func Test_search_undefined_behaviour()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001241 CheckFeature terminal
1242
Bram Moolenaarf45938c2017-11-02 15:59:57 +01001243 let h = winheight(0)
1244 if h < 3
1245 return
1246 endif
1247 " did cause an undefined left shift
1248 let g:buf = term_start([GetVimProg(), '--clean', '-e', '-s', '-c', 'call search(getline("."))', 'samples/test000'], {'term_rows': 3})
1249 call assert_equal([''], getline(1, '$'))
1250 call term_sendkeys(g:buf, ":qa!\<cr>")
1251 bwipe!
1252endfunc
Bram Moolenaar2973daa2017-11-02 23:15:40 +01001253
1254func Test_search_undefined_behaviour2()
1255 call search("\%UC0000000")
1256endfunc
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001257
1258" Test for search('multi-byte char', 'bce')
1259func Test_search_multibyte()
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001260 let save_enc = &encoding
1261 set encoding=utf8
1262 enew!
1263 call append('$', 'A')
1264 call cursor(2, 1)
1265 call assert_equal(2, search('A', 'bce', line('.')))
1266 enew!
1267 let &encoding = save_enc
1268endfunc
Bram Moolenaar890dd052017-12-16 19:59:37 +01001269
1270" This was causing E874. Also causes an invalid read?
1271func Test_look_behind()
1272 new
Bram Moolenaar3ba35402019-12-21 22:00:50 +01001273 call setline(1, '0\|\&\n\@<=')
Bram Moolenaar890dd052017-12-16 19:59:37 +01001274 call search(getline("."))
1275 bwipe!
1276endfunc
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01001277
1278func Test_search_sentence()
1279 new
1280 " this used to cause a crash
Bram Moolenaare2e40752020-09-04 21:18:46 +02001281 call assert_fails("/\\%')", 'E486:')
1282 call assert_fails("/", 'E486:')
Bram Moolenaar1bd999f2017-12-19 22:25:40 +01001283 /\%'(
1284 /
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01001285endfunc
Bram Moolenaar2fb8f682018-12-01 13:14:45 +01001286
1287" Test that there is no crash when there is a last search pattern but no last
1288" substitute pattern.
1289func Test_no_last_substitute_pat()
1290 " Use viminfo to set the last search pattern to a string and make the last
1291 " substitute pattern the most recent used and make it empty (NULL).
1292 call writefile(['~MSle0/bar', '~MSle0~&'], 'Xviminfo')
1293 rviminfo! Xviminfo
1294 call assert_fails('normal n', 'E35:')
1295
1296 call delete('Xviminfo')
1297endfunc
Bram Moolenaar5f5e2032018-12-14 15:47:03 +01001298
1299func Test_search_Ctrl_L_combining()
1300 " Make sure, that Ctrl-L works correctly with combining characters.
1301 " It uses an artificial example of an 'a' with 4 combining chars:
Bram Moolenaar3ba35402019-12-21 22:00:50 +01001302 " 'a' U+0061 Dec:97 LATIN SMALL LETTER A &#x61; /\%u61\Z "\u0061"
Bram Moolenaar5f5e2032018-12-14 15:47:03 +01001303 " ' ̀' U+0300 Dec:768 COMBINING GRAVE ACCENT &#x300; /\%u300\Z "\u0300"
1304 " ' ́' U+0301 Dec:769 COMBINING ACUTE ACCENT &#x301; /\%u301\Z "\u0301"
1305 " ' ̇' U+0307 Dec:775 COMBINING DOT ABOVE &#x307; /\%u307\Z "\u0307"
Bram Moolenaar3ba35402019-12-21 22:00:50 +01001306 " ' ̣' U+0323 Dec:803 COMBINING DOT BELOW &#x323; /\%u323 "\u0323"
Bram Moolenaar5f5e2032018-12-14 15:47:03 +01001307 " Those should also appear on the commandline
Bram Moolenaarb68df222020-03-19 15:05:28 +01001308 CheckOption incsearch
1309
Bram Moolenaar5f5e2032018-12-14 15:47:03 +01001310 call Cmdline3_prep()
1311 1
1312 let bufcontent = ['', 'Miạ̀́̇m']
1313 call append('$', bufcontent)
1314 call feedkeys("/Mi\<c-l>\<c-l>\<cr>", 'tx')
1315 call assert_equal(5, line('.'))
1316 call assert_equal(bufcontent[1], @/)
1317 call Incsearch_cleanup()
1318endfunc
Bram Moolenaar527a2d82019-02-21 22:28:51 +01001319
Bram Moolenaarab350f82019-02-28 06:25:00 +01001320func Test_large_hex_chars1()
Bram Moolenaar527a2d82019-02-21 22:28:51 +01001321 " This used to cause a crash, the character becomes an NFA state.
1322 try
1323 /\%Ufffffc23
1324 catch
1325 call assert_match('E678:', v:exception)
1326 endtry
Bram Moolenaarab350f82019-02-28 06:25:00 +01001327 try
1328 set re=1
1329 /\%Ufffffc23
1330 catch
1331 call assert_match('E678:', v:exception)
1332 endtry
1333 set re&
1334endfunc
1335
1336func Test_large_hex_chars2()
1337 " This used to cause a crash, the character becomes an NFA state.
1338 try
1339 /[\Ufffffc1f]
1340 catch
1341 call assert_match('E486:', v:exception)
1342 endtry
1343 try
1344 set re=1
1345 /[\Ufffffc1f]
1346 catch
1347 call assert_match('E486:', v:exception)
1348 endtry
1349 set re&
Bram Moolenaar527a2d82019-02-21 22:28:51 +01001350endfunc
Bram Moolenaarcd625122019-02-22 17:29:43 +01001351
1352func Test_one_error_msg()
Bram Moolenaar8832a342020-04-11 18:36:38 +02001353 " This was also giving an internal error
Bram Moolenaarcd625122019-02-22 17:29:43 +01001354 call assert_fails('call search(" \\((\\v[[=P=]]){185}+ ")', 'E871:')
1355endfunc
Bram Moolenaar730f48f2019-04-11 13:45:57 +02001356
1357func Test_incsearch_add_char_under_cursor()
Bram Moolenaarb68df222020-03-19 15:05:28 +01001358 CheckOption incsearch
1359
Bram Moolenaar730f48f2019-04-11 13:45:57 +02001360 set incsearch
1361 new
1362 call setline(1, ['find match', 'anything'])
1363 1
1364 call test_override('char_avail', 1)
1365 call feedkeys("fc/m\<C-L>\<C-L>\<C-L>\<C-L>\<C-L>\<CR>", 'tx')
1366 call assert_equal('match', @/)
1367 call test_override('char_avail', 0)
1368
1369 set incsearch&
1370 bwipe!
1371endfunc
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02001372
1373" Test for the search() function with match at the cursor position
1374func Test_search_match_at_curpos()
1375 new
1376 call append(0, ['foobar', '', 'one two', ''])
1377
1378 normal gg
1379
Bram Moolenaar196b4662019-09-06 21:34:30 +02001380 eval 'foobar'->search('c')
Bram Moolenaarc6b37db2019-04-27 18:00:34 +02001381 call assert_equal([1, 1], [line('.'), col('.')])
1382
1383 normal j
1384 call search('^$', 'c')
1385 call assert_equal([2, 1], [line('.'), col('.')])
1386
1387 call search('^$', 'bc')
1388 call assert_equal([2, 1], [line('.'), col('.')])
1389
1390 exe "normal /two\<CR>"
1391 call search('.', 'c')
1392 call assert_equal([3, 5], [line('.'), col('.')])
1393
1394 close!
1395endfunc
Bram Moolenaardb294ad2019-06-06 12:49:29 +02001396
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001397" Test for error cases with the search() function
1398func Test_search_errors()
1399 call assert_fails("call search('pat', [])", 'E730:')
1400 call assert_fails("call search('pat', 'b', {})", 'E728:')
1401 call assert_fails("call search('pat', 'b', 1, [])", 'E745:')
1402 call assert_fails("call search('pat', 'ns')", 'E475:')
1403 call assert_fails("call search('pat', 'mr')", 'E475:')
Bram Moolenaar8832a342020-04-11 18:36:38 +02001404
1405 new
1406 call setline(1, ['foo', 'bar'])
1407 call assert_fails('call feedkeys("/foo/;/bar/;\<CR>", "tx")', 'E386:')
1408 bwipe!
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001409endfunc
1410
Bram Moolenaardb294ad2019-06-06 12:49:29 +02001411func Test_search_display_pattern()
1412 new
1413 call setline(1, ['foo', 'bar', 'foobar'])
1414
1415 call cursor(1, 1)
1416 let @/ = 'foo'
Bram Moolenaara4208962019-08-24 20:50:19 +02001417 let pat = @/->escape('()*?'. '\s\+')
Bram Moolenaardb294ad2019-06-06 12:49:29 +02001418 let g:a = execute(':unsilent :norm! n')
1419 call assert_match(pat, g:a)
1420
1421 " right-left
1422 if exists("+rightleft")
1423 set rl
1424 call cursor(1, 1)
1425 let @/ = 'foo'
1426 let pat = 'oof/\s\+'
1427 let g:a = execute(':unsilent :norm! n')
1428 call assert_match(pat, g:a)
1429 set norl
1430 endif
1431endfunc
Bram Moolenaar196b4662019-09-06 21:34:30 +02001432
1433func Test_searchdecl()
1434 let lines =<< trim END
1435 int global;
1436
1437 func()
1438 {
1439 int global;
1440 if (cond) {
1441 int local;
1442 }
1443 int local;
1444 // comment
1445 }
1446 END
1447 new
1448 call setline(1, lines)
1449 10
1450 call assert_equal(0, searchdecl('local', 0, 0))
1451 call assert_equal(7, getcurpos()[1])
1452
1453 10
1454 call assert_equal(0, 'local'->searchdecl(0, 1))
1455 call assert_equal(9, getcurpos()[1])
1456
1457 10
1458 call assert_equal(0, searchdecl('global'))
1459 call assert_equal(5, getcurpos()[1])
1460
1461 10
1462 call assert_equal(0, searchdecl('global', 1))
1463 call assert_equal(1, getcurpos()[1])
1464
1465 bwipe!
1466endfunc
Bram Moolenaar98a336d2020-01-20 20:22:30 +01001467
1468func Test_search_special()
Bram Moolenaarfe4bbac2020-01-20 21:12:20 +01001469 " this was causing illegal memory access and an endless loop
1470 set t_PE=
Bram Moolenaar98a336d2020-01-20 20:22:30 +01001471 exe "norm /\x80PS"
1472endfunc
Bram Moolenaar07ada5f2020-02-05 20:38:22 +01001473
1474" Test for command failures when the last search pattern is not set.
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001475" Need to run this in a new vim instance where last search pattern is not set.
Bram Moolenaar07ada5f2020-02-05 20:38:22 +01001476func Test_search_with_no_last_pat()
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001477 let lines =<< trim [SCRIPT]
1478 call assert_fails("normal i\<C-R>/\e", 'E35:')
1479 call assert_fails("exe '/'", 'E35:')
1480 call assert_fails("exe '?'", 'E35:')
1481 call assert_fails("/", 'E35:')
1482 call assert_fails("?", 'E35:')
1483 call assert_fails("normal n", 'E35:')
1484 call assert_fails("normal N", 'E35:')
1485 call assert_fails("normal gn", 'E35:')
1486 call assert_fails("normal gN", 'E35:')
1487 call assert_fails("normal cgn", 'E35:')
1488 call assert_fails("normal cgN", 'E35:')
1489 let p = []
1490 let p = @/
1491 call assert_equal('', p)
1492 call assert_fails("normal :\<C-R>/", 'E35:')
1493 call assert_fails("//p", 'E35:')
1494 call assert_fails(";//p", 'E35:')
1495 call assert_fails("??p", 'E35:')
1496 call assert_fails(";??p", 'E35:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001497 call assert_fails('g//p', ['E35:', 'E476:'])
1498 call assert_fails('v//p', ['E35:', 'E476:'])
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001499 call writefile(v:errors, 'Xresult')
1500 qall!
1501 [SCRIPT]
1502 call writefile(lines, 'Xscript')
1503
1504 if RunVim([], [], '--clean -S Xscript')
1505 call assert_equal([], readfile('Xresult'))
1506 endif
1507 call delete('Xscript')
1508 call delete('Xresult')
Bram Moolenaar07ada5f2020-02-05 20:38:22 +01001509endfunc
1510
1511" Test for using tilde (~) atom in search. This should use the last used
1512" substitute pattern
1513func Test_search_tilde_pat()
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001514 let lines =<< trim [SCRIPT]
1515 set regexpengine=1
1516 call assert_fails('exe "normal /~\<CR>"', 'E33:')
1517 call assert_fails('exe "normal ?~\<CR>"', 'E33:')
1518 set regexpengine=2
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001519 call assert_fails('exe "normal /~\<CR>"', ['E33:', 'E383:'])
1520 call assert_fails('exe "normal ?~\<CR>"', ['E33:', 'E383:'])
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001521 set regexpengine&
1522 call writefile(v:errors, 'Xresult')
1523 qall!
1524 [SCRIPT]
1525 call writefile(lines, 'Xscript')
1526 if RunVim([], [], '--clean -S Xscript')
1527 call assert_equal([], readfile('Xresult'))
1528 endif
1529 call delete('Xscript')
1530 call delete('Xresult')
Bram Moolenaar07ada5f2020-02-05 20:38:22 +01001531endfunc
1532
Bram Moolenaar406cd902020-02-18 21:54:41 +01001533" Test for searching a pattern that is not present with 'nowrapscan'
1534func Test_search_pat_not_found()
1535 new
1536 set nowrapscan
1537 let @/ = '1abcxyz2'
1538 call assert_fails('normal n', 'E385:')
1539 call assert_fails('normal N', 'E384:')
1540 set wrapscan&
1541 close
1542endfunc
1543
1544" Test for v:searchforward variable
1545func Test_searchforward_var()
1546 new
1547 call setline(1, ['foo', '', 'foo'])
1548 call cursor(2, 1)
1549 let @/ = 'foo'
1550 let v:searchforward = 0
1551 normal N
1552 call assert_equal(3, line('.'))
1553 call cursor(2, 1)
1554 let v:searchforward = 1
1555 normal N
1556 call assert_equal(1, line('.'))
1557 close!
1558endfunc
1559
Bram Moolenaar476a6132020-04-08 19:48:56 +02001560" Test for invalid regular expressions
1561func Test_invalid_regexp()
1562 set regexpengine=1
1563 call assert_fails("call search(repeat('\\(.\\)', 10))", 'E51:')
Bram Moolenaar4d23c522020-04-09 18:42:11 +02001564 call assert_fails("call search('\\%(')", 'E53:')
1565 call assert_fails("call search('\\(')", 'E54:')
1566 call assert_fails("call search('\\)')", 'E55:')
Bram Moolenaar476a6132020-04-08 19:48:56 +02001567 call assert_fails("call search('x\\@#')", 'E59:')
Bram Moolenaar4d23c522020-04-09 18:42:11 +02001568 call assert_fails('call search(''\v%(%(%(%(%(%(%(%(%(%(%(a){1}){1}){1}){1}){1}){1}){1}){1}){1}){1}){1}'')', 'E60:')
1569 call assert_fails("call search('a\\+*')", 'E61:')
Bram Moolenaar476a6132020-04-08 19:48:56 +02001570 call assert_fails("call search('\\_m')", 'E63:')
1571 call assert_fails("call search('\\+')", 'E64:')
1572 call assert_fails("call search('\\1')", 'E65:')
1573 call assert_fails("call search('\\z\\(\\)')", 'E66:')
1574 call assert_fails("call search('\\z2')", 'E67:')
1575 call assert_fails("call search('\\zx')", 'E68:')
1576 call assert_fails("call search('\\%[ab')", 'E69:')
1577 call assert_fails("call search('\\%[]')", 'E70:')
1578 call assert_fails("call search('\\%a')", 'E71:')
1579 call assert_fails("call search('ab\\%[\\(cd\\)]')", 'E369:')
1580 call assert_fails("call search('ab\\%[\\%(cd\\)]')", 'E369:')
1581 set regexpengine=2
1582 call assert_fails("call search('\\_')", 'E865:')
1583 call assert_fails("call search('\\+')", 'E866:')
1584 call assert_fails("call search('\\zx')", 'E867:')
1585 call assert_fails("call search('\\%a')", 'E867:')
1586 call assert_fails("call search('x\\@#')", 'E869:')
1587 call assert_fails("call search(repeat('\\(.\\)', 10))", 'E872:')
1588 call assert_fails("call search('\\_m')", 'E877:')
1589 call assert_fails("call search('\\%(')", 'E53:')
1590 call assert_fails("call search('\\(')", 'E54:')
1591 call assert_fails("call search('\\)')", 'E55:')
1592 call assert_fails("call search('\\z\\(\\)')", 'E66:')
1593 call assert_fails("call search('\\%[ab')", 'E69:')
Bram Moolenaar4d23c522020-04-09 18:42:11 +02001594 call assert_fails("call search('\\%[]')", 'E70:')
Bram Moolenaar476a6132020-04-08 19:48:56 +02001595 call assert_fails("call search('\\%9999999999999999999999999999v')", 'E951:')
1596 set regexpengine&
1597 call assert_fails("call search('\\%#=3ab')", 'E864:')
1598endfunc
1599
Bram Moolenaar004a6782020-04-11 17:09:31 +02001600" Test for searching a very complex pattern in a string. Should switch the
1601" regexp engine from NFA to the old engine.
1602func Test_regexp_switch_engine()
1603 let l = readfile('samples/re.freeze.txt')
1604 let v = substitute(l[4], '..\@<!', '', '')
1605 call assert_equal(l[4], v)
1606endfunc
1607
1608" Test for the \%V atom to search within visually selected text
1609func Test_search_in_visual_area()
1610 new
1611 call setline(1, ['foo bar1', 'foo bar2', 'foo bar3', 'foo bar4'])
1612 exe "normal 2GVjo/\\%Vbar\<CR>\<Esc>"
1613 call assert_equal([2, 5], [line('.'), col('.')])
1614 exe "normal 2GVj$?\\%Vbar\<CR>\<Esc>"
1615 call assert_equal([3, 5], [line('.'), col('.')])
1616 close!
1617endfunc
1618
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001619" Test for searching with 'smartcase' and 'ignorecase'
1620func Test_search_smartcase()
1621 new
1622 call setline(1, ['', 'Hello'])
1623 set noignorecase nosmartcase
1624 call assert_fails('exe "normal /\\a\\_.\\(.*\\)O\<CR>"', 'E486:')
1625
1626 set ignorecase nosmartcase
1627 exe "normal /\\a\\_.\\(.*\\)O\<CR>"
1628 call assert_equal([2, 1], [line('.'), col('.')])
1629
1630 call cursor(1, 1)
1631 set ignorecase smartcase
1632 call assert_fails('exe "normal /\\a\\_.\\(.*\\)O\<CR>"', 'E486:')
1633
1634 exe "normal /\\a\\_.\\(.*\\)o\<CR>"
1635 call assert_equal([2, 1], [line('.'), col('.')])
1636
Bram Moolenaar224a5f12020-04-28 20:29:07 +02001637 " Test for using special atoms with 'smartcase'
1638 call setline(1, ['', ' Hello\ '])
1639 call cursor(1, 1)
1640 call feedkeys('/\_.\%(\uello\)\' .. "\<CR>", 'xt')
1641 call assert_equal([2, 4], [line('.'), col('.')])
1642
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001643 set ignorecase& smartcase&
1644 close!
Bram Moolenaard66cdcd2020-07-26 13:27:16 +02001645endfun
1646
1647" Test 'smartcase' with utf-8.
1648func Test_search_smartcase_utf8()
1649 new
1650 let save_enc = &encoding
1651 set encoding=utf8 ignorecase smartcase
1652
1653 call setline(1, 'Café cafÉ')
1654 1s/café/x/g
1655 call assert_equal('x x', getline(1))
1656
1657 call setline(1, 'Café cafÉ')
1658 1s/cafÉ/x/g
1659 call assert_equal('Café x', getline(1))
1660
1661 set ignorecase& smartcase&
1662 let &encoding = save_enc
1663 close!
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001664endfunc
1665
1666" Test searching past the end of a file
1667func Test_search_past_eof()
1668 new
1669 call setline(1, ['Line'])
1670 exe "normal /\\n\\zs\<CR>"
1671 call assert_equal([1, 4], [line('.'), col('.')])
1672 close!
1673endfunc
1674
Bram Moolenaar224a5f12020-04-28 20:29:07 +02001675" Test for various search offsets
1676func Test_search_offset()
1677 " With /e, for a match in the first column of a line, the cursor should be
1678 " placed at the end of the previous line.
1679 new
1680 call setline(1, ['one two', 'three four'])
1681 call search('two\_.', 'e')
1682 call assert_equal([1, 7], [line('.'), col('.')])
1683
1684 " with cursor at the beginning of the file, use /s+1
1685 call cursor(1, 1)
1686 exe "normal /two/s+1\<CR>"
1687 call assert_equal([1, 6], [line('.'), col('.')])
1688
1689 " with cursor at the end of the file, use /e-1
1690 call cursor(2, 10)
1691 exe "normal ?three?e-1\<CR>"
1692 call assert_equal([2, 4], [line('.'), col('.')])
1693
1694 " line offset - after the last line
1695 call cursor(1, 1)
1696 exe "normal /three/+1\<CR>"
1697 call assert_equal([2, 1], [line('.'), col('.')])
1698
1699 " line offset - before the first line
1700 call cursor(2, 1)
1701 exe "normal ?one?-1\<CR>"
1702 call assert_equal([1, 1], [line('.'), col('.')])
1703
1704 " character offset - before the first character in the file
1705 call cursor(2, 1)
1706 exe "normal ?one?s-1\<CR>"
1707 call assert_equal([1, 1], [line('.'), col('.')])
1708 call cursor(2, 1)
1709 exe "normal ?one?e-3\<CR>"
1710 call assert_equal([1, 1], [line('.'), col('.')])
1711
1712 " character offset - after the last character in the file
1713 call cursor(1, 1)
1714 exe "normal /four/s+4\<CR>"
1715 call assert_equal([2, 10], [line('.'), col('.')])
1716 call cursor(1, 1)
1717 exe "normal /four/e+1\<CR>"
1718 call assert_equal([2, 10], [line('.'), col('.')])
1719
1720 close!
1721endfunc
1722
1723" Test for searching for matching parenthesis using %
1724func Test_search_match_paren()
1725 new
1726 call setline(1, "abc(def')'ghi'('jk'\\t'lm)no")
1727 " searching for a matching parenthesis should skip single quoted characters
1728 call cursor(1, 4)
1729 normal %
1730 call assert_equal([1, 25], [line('.'), col('.')])
1731 normal %
1732 call assert_equal([1, 4], [line('.'), col('.')])
1733 call cursor(1, 5)
1734 normal ])
1735 call assert_equal([1, 25], [line('.'), col('.')])
1736 call cursor(1, 24)
1737 normal [(
1738 call assert_equal([1, 4], [line('.'), col('.')])
1739
1740 " matching parenthesis in 'virtualedit' mode with cursor after the eol
1741 call setline(1, 'abc(defgh)')
1742 set virtualedit=all
1743 normal 20|%
1744 call assert_equal(4, col('.'))
1745 set virtualedit&
1746 close!
1747endfunc
1748
1749" Test for searching a pattern and stopping before a specified line
1750func Test_search_stopline()
1751 new
1752 call setline(1, ['', '', '', 'vim'])
1753 call assert_equal(0, search('vim', 'n', 3))
1754 call assert_equal(4, search('vim', 'n', 4))
1755 call setline(1, ['vim', '', '', ''])
1756 call cursor(4, 1)
1757 call assert_equal(0, search('vim', 'bn', 2))
1758 call assert_equal(1, search('vim', 'bn', 1))
1759 close!
1760endfunc
1761
Bram Moolenaar07ada5f2020-02-05 20:38:22 +01001762" vim: shiftwidth=2 sts=2 expandtab