blob: fbd3ca4fcde6b36eb3429210da38c3c7849e5ab6 [file] [log] [blame]
Bram Moolenaar1f3e7d32019-12-06 20:43:36 +01001" Tests for various Visual modes.
Bram Moolenaarc3c766e2017-03-08 22:55:19 +01002
Bram Moolenaard1ad99b2020-10-04 16:16:54 +02003source shared.vim
Bram Moolenaar7d7bcc62021-06-28 21:54:27 +02004source check.vim
Bram Moolenaard1ad99b2020-10-04 16:16:54 +02005
Bram Moolenaar019b9c62016-03-05 17:26:00 +01006func Test_block_shift_multibyte()
Bram Moolenaarf8eb9c52017-01-02 17:31:24 +01007 " Uses double-wide character.
Bram Moolenaar019b9c62016-03-05 17:26:00 +01008 split
9 call setline(1, ['xヹxxx', 'ヹxxx'])
10 exe "normal 1G0l\<C-V>jl>"
11 call assert_equal('x ヹxxx', getline(1))
12 call assert_equal(' ヹxxx', getline(2))
13 q!
14endfunc
Bram Moolenaarf8eb9c52017-01-02 17:31:24 +010015
Bram Moolenaarbae5a172017-08-06 15:42:06 +020016func Test_block_shift_overflow()
17 " This used to cause a multiplication overflow followed by a crash.
18 new
19 normal ii
20 exe "normal \<C-V>876543210>"
21 q!
22endfunc
23
Bram Moolenaarf8eb9c52017-01-02 17:31:24 +010024func Test_dotregister_paste()
25 new
26 exe "norm! ihello world\<esc>"
27 norm! 0ve".p
28 call assert_equal('hello world world', getline(1))
29 q!
30endfunc
Bram Moolenaar23fa81d2017-02-01 21:50:21 +010031
32func Test_Visual_ctrl_o()
33 new
34 call setline(1, ['one', 'two', 'three'])
35 call cursor(1,2)
36 set noshowmode
37 set tw=0
38 call feedkeys("\<c-v>jjlIa\<c-\>\<c-o>:set tw=88\<cr>\<esc>", 'tx')
39 call assert_equal(['oane', 'tawo', 'tahree'], getline(1, 3))
40 call assert_equal(88, &tw)
41 set tw&
42 bw!
43endfu
Bram Moolenaar84b2a382017-02-17 11:40:00 +010044
45func Test_Visual_vapo()
46 new
47 normal oxx
48 normal vapo
49 bwipe!
50endfunc
Bram Moolenaar46522af2017-02-18 23:12:01 +010051
52func Test_Visual_inner_quote()
53 new
54 normal oxX
55 normal vki'
56 bwipe!
57endfunc
Bram Moolenaar75373f32017-08-07 22:02:30 +020058
59" Test for Visual mode not being reset causing E315 error.
60func TriggerTheProblem()
61 " At this point there is no visual selection because :call reset it.
62 " Let's restore the selection:
63 normal gv
64 '<,'>del _
65 try
66 exe "normal \<Esc>"
67 catch /^Vim\%((\a\+)\)\=:E315/
68 echom 'Snap! E315 error!'
Bram Moolenaar63e82db2018-03-06 12:10:48 +010069 let g:msg = 'Snap! E315 error!'
Bram Moolenaar75373f32017-08-07 22:02:30 +020070 endtry
71endfunc
72
73func Test_visual_mode_reset()
Bram Moolenaar75373f32017-08-07 22:02:30 +020074 enew
Bram Moolenaar63e82db2018-03-06 12:10:48 +010075 let g:msg = "Everything's fine."
Bram Moolenaar75373f32017-08-07 22:02:30 +020076 enew
77 setl buftype=nofile
78 call append(line('$'), 'Delete this line.')
79
80 " NOTE: this has to be done by a call to a function because executing :del
81 " the ex-way will require the colon operator which resets the visual mode
82 " thus preventing the problem:
83 exe "normal! GV:call TriggerTheProblem()\<CR>"
84 call assert_equal("Everything's fine.", g:msg)
Bram Moolenaar75373f32017-08-07 22:02:30 +020085endfunc
Bram Moolenaar67418d92017-10-15 22:07:39 +020086
Bram Moolenaar15993ce2017-10-26 20:21:44 +020087" Test for visual block shift and tab characters.
88func Test_block_shift_tab()
Bram Moolenaar3e72dca2021-05-29 16:30:12 +020089 new
Bram Moolenaar15993ce2017-10-26 20:21:44 +020090 call append(0, repeat(['one two three'], 5))
91 call cursor(1,1)
92 exe "normal i\<C-G>u"
93 exe "normal fe\<C-V>4jR\<Esc>ugvr1"
94 call assert_equal('on1 two three', getline(1))
95 call assert_equal('on1 two three', getline(2))
96 call assert_equal('on1 two three', getline(5))
97
Bram Moolenaar3e72dca2021-05-29 16:30:12 +020098 %d _
Bram Moolenaar15993ce2017-10-26 20:21:44 +020099 call append(0, repeat(['abcdefghijklmnopqrstuvwxyz'], 5))
100 call cursor(1,1)
101 exe "normal \<C-V>4jI \<Esc>j<<11|D"
102 exe "normal j7|a\<Tab>\<Tab>"
103 exe "normal j7|a\<Tab>\<Tab> "
104 exe "normal j7|a\<Tab> \<Tab>\<Esc>4k13|\<C-V>4j<"
105 call assert_equal(' abcdefghijklmnopqrstuvwxyz', getline(1))
106 call assert_equal('abcdefghij', getline(2))
107 call assert_equal(" abc\<Tab> defghijklmnopqrstuvwxyz", getline(3))
108 call assert_equal(" abc\<Tab> defghijklmnopqrstuvwxyz", getline(4))
109 call assert_equal(" abc\<Tab> defghijklmnopqrstuvwxyz", getline(5))
110
111 %s/\s\+//g
112 call cursor(1,1)
113 exe "normal \<C-V>4jI \<Esc>j<<"
114 exe "normal j7|a\<Tab>\<Tab>"
115 exe "normal j7|a\<Tab>\<Tab>\<Tab>\<Tab>\<Tab>"
116 exe "normal j7|a\<Tab> \<Tab>\<Tab>\<Esc>4k13|\<C-V>4j3<"
117 call assert_equal(' abcdefghijklmnopqrstuvwxyz', getline(1))
118 call assert_equal('abcdefghij', getline(2))
119 call assert_equal(" abc\<Tab> defghijklmnopqrstuvwxyz", getline(3))
120 call assert_equal(" abc\<Tab>\<Tab>defghijklmnopqrstuvwxyz", getline(4))
121 call assert_equal(" abc\<Tab> defghijklmnopqrstuvwxyz", getline(5))
122
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200123 " Test for block shift with space characters at the beginning and with
124 " 'noexpandtab' and 'expandtab'
125 %d _
126 call setline(1, [" 1", " 2", " 3"])
127 setlocal shiftwidth=2 noexpandtab
128 exe "normal gg\<C-V>3j>"
129 call assert_equal(["\t1", "\t2", "\t3"], getline(1, '$'))
130 %d _
131 call setline(1, [" 1", " 2", " 3"])
132 setlocal shiftwidth=2 expandtab
133 exe "normal gg\<C-V>3j>"
134 call assert_equal([" 1", " 2", " 3"], getline(1, '$'))
135 setlocal shiftwidth&
136
137 bw!
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200138endfunc
139
140" Tests Blockwise Visual when there are TABs before the text.
141func Test_blockwise_visual()
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200142 new
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200143 call append(0, ['123456',
144 \ '234567',
145 \ '345678',
146 \ '',
147 \ 'test text test tex start here',
148 \ "\t\tsome text",
149 \ "\t\ttest text",
150 \ 'test text'])
151 call cursor(1,1)
152 exe "normal /start here$\<CR>"
153 exe 'normal "by$' . "\<C-V>jjlld"
154 exe "normal /456$\<CR>"
155 exe "normal \<C-V>jj" . '"bP'
156 call assert_equal(['123start here56',
157 \ '234start here67',
158 \ '345start here78',
159 \ '',
160 \ 'test text test tex rt here',
161 \ "\t\tsomext",
162 \ "\t\ttesext"], getline(1, 7))
163
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200164 bw!
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200165endfunc
166
Bram Moolenaar2e949762018-05-20 14:06:38 +0200167" Test swapping corners in blockwise visual mode with o and O
168func Test_blockwise_visual_o_O()
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200169 new
Bram Moolenaar2e949762018-05-20 14:06:38 +0200170
171 exe "norm! 10i.\<Esc>Y4P3lj\<C-V>4l2jr "
172 exe "norm! gvO\<Esc>ra"
173 exe "norm! gvO\<Esc>rb"
174 exe "norm! gvo\<C-c>rc"
175 exe "norm! gvO\<C-c>rd"
Bram Moolenaar1671f442020-03-10 07:48:13 +0100176 set selection=exclusive
177 exe "norm! gvOo\<C-c>re"
178 call assert_equal('...a be.', getline(4))
179 exe "norm! gvOO\<C-c>rf"
180 set selection&
Bram Moolenaar2e949762018-05-20 14:06:38 +0200181
182 call assert_equal(['..........',
183 \ '...c d..',
184 \ '... ..',
Bram Moolenaar1671f442020-03-10 07:48:13 +0100185 \ '...a bf.',
Bram Moolenaar2e949762018-05-20 14:06:38 +0200186 \ '..........'], getline(1, '$'))
187
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200188 bw!
Bram Moolenaar2e949762018-05-20 14:06:38 +0200189endfun
190
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200191" Test Virtual replace mode.
192func Test_virtual_replace()
Bram Moolenaardf0d24b2018-03-06 14:22:58 +0100193 if exists('&t_kD')
194 let save_t_kD = &t_kD
195 endif
196 if exists('&t_kb')
197 let save_t_kb = &t_kb
198 endif
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200199 exe "set t_kD=\<C-V>x7f t_kb=\<C-V>x08"
200 enew!
201 exe "normal a\nabcdefghi\njk\tlmn\n opq rst\n\<C-D>uvwxyz"
202 call cursor(1,1)
203 set ai bs=2
204 exe "normal gR0\<C-D> 1\nA\nBCDEFGHIJ\n\tKL\nMNO\nPQR"
205 call assert_equal([' 1',
206 \ ' A',
207 \ ' BCDEFGHIJ',
208 \ ' KL',
209 \ ' MNO',
210 \ ' PQR',
211 \ ], getline(1, 6))
212 normal G
213 mark a
214 exe "normal o0\<C-D>\nabcdefghi\njk\tlmn\n opq\trst\n\<C-D>uvwxyz\n"
215 exe "normal 'ajgR0\<C-D> 1\nA\nBCDEFGHIJ\n\tKL\nMNO\nPQR" . repeat("\<BS>", 29)
216 call assert_equal([' 1',
217 \ 'abcdefghi',
218 \ 'jk lmn',
219 \ ' opq rst',
220 \ 'uvwxyz'], getline(7, 11))
221 normal G
222 exe "normal iab\tcdefghi\tjkl"
223 exe "normal 0gRAB......CDEFGHI.J\<Esc>o"
224 exe "normal iabcdefghijklmnopqrst\<Esc>0gRAB\tIJKLMNO\tQR"
225 call assert_equal(['AB......CDEFGHI.Jkl',
226 \ 'AB IJKLMNO QRst'], getline(12, 13))
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200227
228 " Test inserting Tab with 'noexpandtab' and 'softabstop' set to 4
229 %d
230 call setline(1, 'aaaaaaaaaaaaa')
231 set softtabstop=4
232 exe "normal gggR\<Tab>\<Tab>x"
233 call assert_equal("\txaaaa", getline(1))
234 set softtabstop&
235
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200236 enew!
Bram Moolenaare7808482018-03-06 13:17:23 +0100237 set noai bs&vim
Bram Moolenaardf0d24b2018-03-06 14:22:58 +0100238 if exists('save_t_kD')
239 let &t_kD = save_t_kD
240 endif
241 if exists('save_t_kb')
242 let &t_kb = save_t_kb
243 endif
Bram Moolenaar63e82db2018-03-06 12:10:48 +0100244endfunc
245
246" Test Virtual replace mode.
247func Test_virtual_replace2()
248 enew!
249 set bs=2
250 exe "normal a\nabcdefghi\njk\tlmn\n opq rst\n\<C-D>uvwxyz"
251 call cursor(1,1)
252 " Test 1: Test that del deletes the newline
253 exe "normal gR0\<del> 1\nA\nBCDEFGHIJ\n\tKL\nMNO\nPQR"
254 call assert_equal(['0 1',
255 \ 'A',
256 \ 'BCDEFGHIJ',
257 \ ' KL',
258 \ 'MNO',
259 \ 'PQR',
260 \ ], getline(1, 6))
261 " Test 2:
262 " a newline is not deleted, if no newline has been added in virtual replace mode
263 %d_
264 call setline(1, ['abcd', 'efgh', 'ijkl'])
265 call cursor(2,1)
266 exe "norm! gR1234\<cr>5\<bs>\<bs>\<bs>"
267 call assert_equal(['abcd',
268 \ '123h',
269 \ 'ijkl'], getline(1, '$'))
270 " Test 3:
271 " a newline is deleted, if a newline has been inserted before in virtual replace mode
272 %d_
273 call setline(1, ['abcd', 'efgh', 'ijkl'])
274 call cursor(2,1)
275 exe "norm! gR1234\<cr>\<cr>56\<bs>\<bs>\<bs>"
276 call assert_equal(['abcd',
277 \ '1234',
278 \ 'ijkl'], getline(1, '$'))
279 " Test 4:
280 " delete add a newline, delete it, add it again and check undo
281 %d_
282 call setline(1, ['abcd', 'efgh', 'ijkl'])
283 call cursor(2,1)
284 " break undo sequence explicitly
285 let &ul = &ul
286 exe "norm! gR1234\<cr>\<bs>\<del>56\<cr>"
287 let &ul = &ul
288 call assert_equal(['abcd',
289 \ '123456',
290 \ ''], getline(1, '$'))
291 norm! u
292 call assert_equal(['abcd',
293 \ 'efgh',
294 \ 'ijkl'], getline(1, '$'))
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200295
296 " Test for truncating spaces in a newly added line using 'autoindent' if
297 " characters are not added to that line.
298 %d_
299 call setline(1, [' app', ' bee', ' cat'])
300 setlocal autoindent
301 exe "normal gg$gRt\n\nr"
302 call assert_equal([' apt', '', ' rat'], getline(1, '$'))
303
Bram Moolenaar63e82db2018-03-06 12:10:48 +0100304 " clean up
305 %d_
306 set bs&vim
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200307endfunc
Bram Moolenaar6d3a1942019-01-03 23:10:32 +0100308
Bram Moolenaar81b1ba42019-01-13 16:12:40 +0100309func Test_Visual_word_textobject()
310 new
311 call setline(1, ['First sentence. Second sentence.'])
312
313 " When start and end of visual area are identical, 'aw' or 'iw' select
314 " the whole word.
315 norm! 1go2fcvawy
316 call assert_equal('Second ', @")
317 norm! 1go2fcviwy
318 call assert_equal('Second', @")
319
320 " When start and end of visual area are not identical, 'aw' or 'iw'
321 " extend the word in direction of the end of the visual area.
322 norm! 1go2fcvlawy
323 call assert_equal('cond ', @")
324 norm! gv2awy
325 call assert_equal('cond sentence.', @")
326
327 norm! 1go2fcvliwy
328 call assert_equal('cond', @")
329 norm! gv2iwy
330 call assert_equal('cond sentence', @")
331
332 " Extend visual area in opposite direction.
333 norm! 1go2fcvhawy
334 call assert_equal(' Sec', @")
335 norm! gv2awy
336 call assert_equal(' sentence. Sec', @")
337
338 norm! 1go2fcvhiwy
339 call assert_equal('Sec', @")
340 norm! gv2iwy
341 call assert_equal('. Sec', @")
342
343 bwipe!
344endfunc
345
Bram Moolenaar6d3a1942019-01-03 23:10:32 +0100346func Test_Visual_sentence_textobject()
347 new
Bram Moolenaar81b1ba42019-01-13 16:12:40 +0100348 call setline(1, ['First sentence. Second sentence. Third', 'sentence. Fourth sentence'])
Bram Moolenaar6d3a1942019-01-03 23:10:32 +0100349
350 " When start and end of visual area are identical, 'as' or 'is' select
351 " the whole sentence.
352 norm! 1gofdvasy
353 call assert_equal('Second sentence. ', @")
354 norm! 1gofdvisy
355 call assert_equal('Second sentence.', @")
356
357 " When start and end of visual area are not identical, 'as' or 'is'
358 " extend the sentence in direction of the end of the visual area.
359 norm! 1gofdvlasy
360 call assert_equal('d sentence. ', @")
361 norm! gvasy
362 call assert_equal("d sentence. Third\nsentence. ", @")
363
364 norm! 1gofdvlisy
365 call assert_equal('d sentence.', @")
366 norm! gvisy
367 call assert_equal('d sentence. ', @")
368 norm! gvisy
369 call assert_equal("d sentence. Third\nsentence.", @")
370
371 " Extend visual area in opposite direction.
372 norm! 1gofdvhasy
373 call assert_equal(' Second', @")
374 norm! gvasy
375 call assert_equal("First sentence. Second", @")
376
377 norm! 1gofdvhisy
378 call assert_equal('Second', @")
379 norm! gvisy
380 call assert_equal(' Second', @")
381 norm! gvisy
382 call assert_equal('First sentence. Second', @")
383
384 bwipe!
385endfunc
Bram Moolenaar81b1ba42019-01-13 16:12:40 +0100386
387func Test_Visual_paragraph_textobject()
388 new
Bram Moolenaar8a9bc952020-10-02 18:48:07 +0200389 let lines =<< trim [END]
390 First line.
391
392 Second line.
393 Third line.
394 Fourth line.
395 Fifth line.
396
397 Sixth line.
398 [END]
399 call setline(1, lines)
Bram Moolenaar81b1ba42019-01-13 16:12:40 +0100400
401 " When start and end of visual area are identical, 'ap' or 'ip' select
402 " the whole paragraph.
403 norm! 4ggvapy
404 call assert_equal("Second line.\nThird line.\nFourth line.\nFifth line.\n\n", @")
405 norm! 4ggvipy
406 call assert_equal("Second line.\nThird line.\nFourth line.\nFifth line.\n", @")
407
408 " When start and end of visual area are not identical, 'ap' or 'ip'
409 " extend the sentence in direction of the end of the visual area.
410 " FIXME: actually, it is not sufficient to have different start and
411 " end of visual selection, the start line and end line have to differ,
412 " which is not consistent with the documentation.
413 norm! 4ggVjapy
414 call assert_equal("Third line.\nFourth line.\nFifth line.\n\n", @")
415 norm! gvapy
416 call assert_equal("Third line.\nFourth line.\nFifth line.\n\nSixth line.\n", @")
417 norm! 4ggVjipy
418 call assert_equal("Third line.\nFourth line.\nFifth line.\n", @")
419 norm! gvipy
420 call assert_equal("Third line.\nFourth line.\nFifth line.\n\n", @")
421 norm! gvipy
422 call assert_equal("Third line.\nFourth line.\nFifth line.\n\nSixth line.\n", @")
423
424 " Extend visual area in opposite direction.
425 norm! 5ggVkapy
426 call assert_equal("\nSecond line.\nThird line.\nFourth line.\n", @")
427 norm! gvapy
428 call assert_equal("First line.\n\nSecond line.\nThird line.\nFourth line.\n", @")
429 norm! 5ggVkipy
430 call assert_equal("Second line.\nThird line.\nFourth line.\n", @")
431 norma gvipy
432 call assert_equal("\nSecond line.\nThird line.\nFourth line.\n", @")
433 norm! gvipy
434 call assert_equal("First line.\n\nSecond line.\nThird line.\nFourth line.\n", @")
435
436 bwipe!
437endfunc
Bram Moolenaar19a66852019-03-07 11:25:32 +0100438
439func Test_curswant_not_changed()
440 new
441 call setline(1, ['one', 'two'])
442 au InsertLeave * call getcurpos()
443 call feedkeys("gg0\<C-V>jI123 \<Esc>j", 'xt')
444 call assert_equal([0, 2, 1, 0, 1], getcurpos())
445
446 bwipe!
447 au! InsertLeave
448endfunc
Bram Moolenaarc6b37db2019-04-27 18:00:34 +0200449
450" Tests for "vaBiB", end could be wrong.
451func Test_Visual_Block()
452 new
453 a
454- Bug in "vPPPP" on this text:
455 {
456 cmd;
457 {
458 cmd;\t/* <-- Start cursor here */
459 {
460 }
461 }
462 }
463.
464 normal gg
465 call search('Start cursor here')
466 normal vaBiBD
467 call assert_equal(['- Bug in "vPPPP" on this text:',
468 \ "\t{",
469 \ "\t}"], getline(1, '$'))
470
471 close!
472endfunc
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +0100473
474" Test for 'p'ut in visual block mode
475func Test_visual_block_put()
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200476 new
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +0100477 call append(0, ['One', 'Two', 'Three'])
478 normal gg
479 yank
480 call feedkeys("jl\<C-V>ljp", 'xt')
481 call assert_equal(['One', 'T', 'Tee', 'One', ''], getline(1, '$'))
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200482 bw!
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +0100483endfunc
484
Bram Moolenaar309976e2019-12-05 18:16:33 +0100485" Visual modes (v V CTRL-V) followed by an operator; count; repeating
486func Test_visual_mode_op()
487 new
488 call append(0, '')
489
490 call setline(1, 'apple banana cherry')
491 call cursor(1, 1)
492 normal lvld.l3vd.
493 call assert_equal('a y', getline(1))
494
495 call setline(1, ['line 1 line 1', 'line 2 line 2', 'line 3 line 3',
496 \ 'line 4 line 4', 'line 5 line 5', 'line 6 line 6'])
497 call cursor(1, 1)
498 exe "normal Vcnewline\<Esc>j.j2Vd."
499 call assert_equal(['newline', 'newline'], getline(1, '$'))
500
501 call deletebufline('', 1, '$')
502 call setline(1, ['xxxxxxxxxxxxx', 'xxxxxxxxxxxxx', 'xxxxxxxxxxxxx',
503 \ 'xxxxxxxxxxxxx'])
504 exe "normal \<C-V>jlc \<Esc>l.l2\<C-V>c----\<Esc>l."
505 call assert_equal([' --------x',
506 \ ' --------x',
507 \ 'xxxx--------x',
508 \ 'xxxx--------x'], getline(1, '$'))
509
510 bwipe!
511endfunc
512
513" Visual mode maps (movement and text object)
514" Visual mode maps; count; repeating
515" - Simple
516" - With an Ex command (custom text object)
517func Test_visual_mode_maps()
518 new
519 call append(0, '')
520
521 func SelectInCaps()
522 let [line1, col1] = searchpos('\u', 'bcnW')
523 let [line2, col2] = searchpos('.\u', 'nW')
524 call setpos("'<", [0, line1, col1, 0])
525 call setpos("'>", [0, line2, col2, 0])
526 normal! gv
527 endfunction
528
529 vnoremap W /\u/s-1<CR>
530 vnoremap iW :<C-U>call SelectInCaps()<CR>
531
532 call setline(1, 'KiwiRaspberryDateWatermelonPeach')
533 call cursor(1, 1)
534 exe "normal vWcNo\<Esc>l.fD2vd."
535 call assert_equal('NoNoberryach', getline(1))
536
537 call setline(1, 'JambuRambutanBananaTangerineMango')
538 call cursor(1, 1)
539 exe "normal llviWc-\<Esc>l.l2vdl."
540 call assert_equal('--ago', getline(1))
541
542 vunmap W
543 vunmap iW
544 bwipe!
545 delfunc SelectInCaps
546endfunc
547
548" Operator-pending mode maps (movement and text object)
549" - Simple
550" - With Ex command moving the cursor
551" - With Ex command and Visual selection (custom text object)
552func Test_visual_oper_pending_mode_maps()
553 new
554 call append(0, '')
555
556 func MoveToCap()
557 call search('\u', 'W')
558 endfunction
559
560 func SelectInCaps()
561 let [line1, col1] = searchpos('\u', 'bcnW')
562 let [line2, col2] = searchpos('.\u', 'nW')
563 call setpos("'<", [0, line1, col1, 0])
564 call setpos("'>", [0, line2, col2, 0])
565 normal! gv
566 endfunction
567
568 onoremap W /\u/<CR>
569 onoremap <Leader>W :<C-U>call MoveToCap()<CR>
570 onoremap iW :<C-U>call SelectInCaps()<CR>
571
572 call setline(1, 'PineappleQuinceLoganberryOrangeGrapefruitKiwiZ')
573 call cursor(1, 1)
574 exe "normal cW-\<Esc>l.l2.l."
575 call assert_equal('----Z', getline(1))
576
577 call setline(1, 'JuniperDurianZ')
578 call cursor(1, 1)
579 exe "normal g?\WfD."
580 call assert_equal('WhavcreQhevnaZ', getline(1))
581
582 call setline(1, 'LemonNectarineZ')
583 call cursor(1, 1)
584 exe "normal yiWPlciWNew\<Esc>fr."
585 call assert_equal('LemonNewNewZ', getline(1))
586
587 ounmap W
588 ounmap <Leader>W
589 ounmap iW
590 bwipe!
591 delfunc MoveToCap
592 delfunc SelectInCaps
593endfunc
594
595" Patch 7.3.879: Properly abort Operator-pending mode for "dv:<Esc>" etc.
596func Test_op_pend_mode_abort()
597 new
598 call append(0, '')
599
600 call setline(1, ['zzzz', 'zzzz'])
601 call cursor(1, 1)
602
603 exe "normal dV:\<CR>dv:\<CR>"
604 call assert_equal(['zzz'], getline(1, 2))
605 set nomodifiable
606 call assert_fails('exe "normal d:\<CR>"', 'E21:')
607 set modifiable
608 call feedkeys("dv:\<Esc>dV:\<Esc>", 'xt')
609 call assert_equal(['zzz'], getline(1, 2))
610 set nomodifiable
611 let v:errmsg = ''
612 call feedkeys("d:\<Esc>", 'xt')
613 call assert_true(v:errmsg !~# '^E21:')
614 set modifiable
615
616 bwipe!
617endfunc
618
619func Test_characterwise_visual_mode()
620 new
621
622 " characterwise visual mode: replace last line
623 $put ='a'
624 let @" = 'x'
625 normal v$p
626 call assert_equal('x', getline('$'))
627
628 " characterwise visual mode: delete middle line
629 call deletebufline('', 1, '$')
630 call append('$', ['a', 'b', 'c'])
631 normal G
632 normal kkv$d
633 call assert_equal(['', 'b', 'c'], getline(1, '$'))
634
635 " characterwise visual mode: delete middle two lines
636 call deletebufline('', 1, '$')
637 call append('$', ['a', 'b', 'c'])
638 normal Gkkvj$d
639 call assert_equal(['', 'c'], getline(1, '$'))
640
641 " characterwise visual mode: delete last line
642 call deletebufline('', 1, '$')
643 call append('$', ['a', 'b', 'c'])
644 normal Gv$d
645 call assert_equal(['', 'a', 'b', ''], getline(1, '$'))
646
647 " characterwise visual mode: delete last two lines
648 call deletebufline('', 1, '$')
649 call append('$', ['a', 'b', 'c'])
650 normal Gkvj$d
651 call assert_equal(['', 'a', ''], getline(1, '$'))
652
Bram Moolenaar8a9bc952020-10-02 18:48:07 +0200653 " characterwise visual mode: use a count with the visual mode from the last
654 " line in the buffer
655 %d _
656 call setline(1, ['one', 'two', 'three', 'four'])
657 norm! vj$y
658 norm! G1vy
659 call assert_equal('four', @")
660
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200661 " characterwise visual mode: replace a single character line and the eol
662 %d _
663 call setline(1, "a")
664 normal v$rx
665 call assert_equal(['x'], getline(1, '$'))
666
Bram Moolenaar309976e2019-12-05 18:16:33 +0100667 bwipe!
668endfunc
669
Bram Moolenaar309976e2019-12-05 18:16:33 +0100670func Test_visual_mode_put()
671 new
672
673 " v_p: replace last character with line register at middle line
674 call append('$', ['aaa', 'bbb', 'ccc'])
675 normal G
676 -2yank
677 normal k$vp
678 call assert_equal(['', 'aaa', 'bb', 'aaa', '', 'ccc'], getline(1, '$'))
679
680 " v_p: replace last character with line register at middle line selecting
681 " newline
682 call deletebufline('', 1, '$')
683 call append('$', ['aaa', 'bbb', 'ccc'])
684 normal G
685 -2yank
686 normal k$v$p
687 call assert_equal(['', 'aaa', 'bb', 'aaa', 'ccc'], getline(1, '$'))
688
689 " v_p: replace last character with line register at last line
690 call deletebufline('', 1, '$')
691 call append('$', ['aaa', 'bbb', 'ccc'])
692 normal G
693 -2yank
694 normal $vp
695 call assert_equal(['', 'aaa', 'bbb', 'cc', 'aaa', ''], getline(1, '$'))
696
697 " v_p: replace last character with line register at last line selecting
698 " newline
699 call deletebufline('', 1, '$')
700 call append('$', ['aaa', 'bbb', 'ccc'])
701 normal G
702 -2yank
703 normal $v$p
704 call assert_equal(['', 'aaa', 'bbb', 'cc', 'aaa', ''], getline(1, '$'))
705
706 bwipe!
707endfunc
708
Bram Moolenaar515545e2020-03-22 14:08:59 +0100709func Test_gv_with_exclusive_selection()
Bram Moolenaar309976e2019-12-05 18:16:33 +0100710 new
711
Bram Moolenaar515545e2020-03-22 14:08:59 +0100712 " gv with exclusive selection after an operation
Bram Moolenaar309976e2019-12-05 18:16:33 +0100713 call append('$', ['zzz ', 'äà '])
714 set selection=exclusive
715 normal Gkv3lyjv3lpgvcxxx
716 call assert_equal(['', 'zzz ', 'xxx '], getline(1, '$'))
717
Bram Moolenaar515545e2020-03-22 14:08:59 +0100718 " gv with exclusive selection without an operation
Bram Moolenaar309976e2019-12-05 18:16:33 +0100719 call deletebufline('', 1, '$')
720 call append('$', 'zzz ')
721 set selection=exclusive
722 exe "normal G0v3l\<Esc>gvcxxx"
723 call assert_equal(['', 'xxx '], getline(1, '$'))
724
725 set selection&vim
726 bwipe!
727endfunc
728
Bram Moolenaar1f3e7d32019-12-06 20:43:36 +0100729" Tests for the visual block mode commands
730func Test_visual_block_mode()
731 new
732 call append(0, '')
Bram Moolenaar1671f442020-03-10 07:48:13 +0100733 call setline(1, repeat(['abcdefghijklm'], 5))
Bram Moolenaar1f3e7d32019-12-06 20:43:36 +0100734 call cursor(1, 1)
735
736 " Test shift-right of a block
737 exe "normal jllll\<C-V>jj>wll\<C-V>jlll>"
738 " Test shift-left of a block
739 exe "normal G$hhhh\<C-V>kk<"
740 " Test block-insert
741 exe "normal Gkl\<C-V>kkkIxyz"
742 " Test block-replace
743 exe "normal Gllll\<C-V>kkklllrq"
744 " Test block-change
745 exe "normal G$khhh\<C-V>hhkkcmno"
746 call assert_equal(['axyzbcdefghijklm',
747 \ 'axyzqqqq mno ghijklm',
748 \ 'axyzqqqqef mno ghijklm',
749 \ 'axyzqqqqefgmnoklm',
750 \ 'abcdqqqqijklm'], getline(1, 5))
751
Bram Moolenaar1671f442020-03-10 07:48:13 +0100752 " Test 'C' to change till the end of the line
753 call cursor(3, 4)
754 exe "normal! \<C-V>j3lCooo"
755 call assert_equal(['axyooo', 'axyooo'], getline(3, 4))
756
757 " Test 'D' to delete till the end of the line
758 call cursor(3, 3)
759 exe "normal! \<C-V>j2lD"
760 call assert_equal(['ax', 'ax'], getline(3, 4))
761
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200762 " Test block insert with a short line that ends before the block
763 %d _
764 call setline(1, [" one", "a", " two"])
765 exe "normal gg\<C-V>2jIx"
766 call assert_equal([" xone", "a", " xtwo"], getline(1, '$'))
767
768 " Test block append at EOL with '$' and without '$'
769 %d _
770 call setline(1, ["one", "a", "two"])
771 exe "normal gg$\<C-V>2jAx"
772 call assert_equal(["onex", "ax", "twox"], getline(1, '$'))
773 %d _
774 call setline(1, ["one", "a", "two"])
775 exe "normal gg3l\<C-V>2jAx"
776 call assert_equal(["onex", "a x", "twox"], getline(1, '$'))
777
778 " Test block replace with an empty line in the middle and use $ to jump to
779 " the end of the line.
780 %d _
781 call setline(1, ['one', '', 'two'])
782 exe "normal gg$\<C-V>2jrx"
783 call assert_equal(["onx", "", "twx"], getline(1, '$'))
784
785 " Test block replace with an empty line in the middle and move cursor to the
786 " end of the line
787 %d _
788 call setline(1, ['one', '', 'two'])
789 exe "normal gg2l\<C-V>2jrx"
790 call assert_equal(["onx", "", "twx"], getline(1, '$'))
791
792 " Replace odd number of characters with a multibyte character
793 %d _
794 call setline(1, ['abcd', 'efgh'])
795 exe "normal ggl\<C-V>2ljr\u1100"
796 call assert_equal(["a\u1100 ", "e\u1100 "], getline(1, '$'))
797
798 " During visual block append, if the cursor moved outside of the selected
799 " range, then the edit should not be applied to the block.
800 %d _
801 call setline(1, ['aaa', 'bbb', 'ccc'])
802 exe "normal 2G\<C-V>jAx\<Up>"
803 call assert_equal(['aaa', 'bxbb', 'ccc'], getline(1, '$'))
804
805 " During visual block append, if the cursor is moved before the start of the
806 " block, then the new text should be appended there.
807 %d _
808 call setline(1, ['aaa', 'bbb', 'ccc'])
809 exe "normal $\<C-V>2jA\<Left>x"
810 " BUG: Instead of adding x as the third character in all the three lines,
811 " 'a' is added in the second and third lines at the end. This bug is not
812 " reproducible if this operation is performed manually.
813 "call assert_equal(['aaxa', 'bbxb', 'ccxc'], getline(1, '$'))
814 call assert_equal(['aaxa', 'bbba', 'ccca'], getline(1, '$'))
Yegappan Lakshmanan59b26232021-06-05 20:59:22 +0200815 " Repeat the previous test but use 'l' to move the cursor instead of '$'
816 call setline(1, ['aaa', 'bbb', 'ccc'])
817 exe "normal! gg2l\<C-V>2jA\<Left>x"
818 call assert_equal(['aaxa', 'bbxb', 'ccxc'], getline(1, '$'))
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200819
820 " Change a characterwise motion to a blockwise motion using CTRL-V
821 %d _
822 call setline(1, ['123', '456', '789'])
823 exe "normal ld\<C-V>j"
824 call assert_equal(['13', '46', '789'], getline(1, '$'))
825
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +0200826 " Test from ':help v_b_I_example'
827 %d _
828 setlocal tabstop=8 shiftwidth=4
829 let lines =<< trim END
830 abcdefghijklmnopqrstuvwxyz
831 abc defghijklmnopqrstuvwxyz
832 abcdef ghi jklmnopqrstuvwxyz
833 abcdefghijklmnopqrstuvwxyz
834 END
835 call setline(1, lines)
836 exe "normal ggfo\<C-V>3jISTRING"
837 let expected =<< trim END
838 abcdefghijklmnSTRINGopqrstuvwxyz
839 abc STRING defghijklmnopqrstuvwxyz
840 abcdef ghi STRING jklmnopqrstuvwxyz
841 abcdefghijklmnSTRINGopqrstuvwxyz
842 END
843 call assert_equal(expected, getline(1, '$'))
844
845 " Test from ':help v_b_A_example'
846 %d _
847 let lines =<< trim END
848 abcdefghijklmnopqrstuvwxyz
849 abc defghijklmnopqrstuvwxyz
850 abcdef ghi jklmnopqrstuvwxyz
851 abcdefghijklmnopqrstuvwxyz
852 END
853 call setline(1, lines)
854 exe "normal ggfo\<C-V>3j$ASTRING"
855 let expected =<< trim END
856 abcdefghijklmnopqrstuvwxyzSTRING
857 abc defghijklmnopqrstuvwxyzSTRING
858 abcdef ghi jklmnopqrstuvwxyzSTRING
859 abcdefghijklmnopqrstuvwxyzSTRING
860 END
861 call assert_equal(expected, getline(1, '$'))
862
863 " Test from ':help v_b_<_example'
864 %d _
865 let lines =<< trim END
866 abcdefghijklmnopqrstuvwxyz
867 abc defghijklmnopqrstuvwxyz
868 abcdef ghi jklmnopqrstuvwxyz
869 abcdefghijklmnopqrstuvwxyz
870 END
871 call setline(1, lines)
872 exe "normal ggfo\<C-V>3j3l<.."
873 let expected =<< trim END
874 abcdefghijklmnopqrstuvwxyz
875 abc defghijklmnopqrstuvwxyz
876 abcdef ghi jklmnopqrstuvwxyz
877 abcdefghijklmnopqrstuvwxyz
878 END
879 call assert_equal(expected, getline(1, '$'))
880
881 " Test from ':help v_b_>_example'
882 %d _
883 let lines =<< trim END
884 abcdefghijklmnopqrstuvwxyz
885 abc defghijklmnopqrstuvwxyz
886 abcdef ghi jklmnopqrstuvwxyz
887 abcdefghijklmnopqrstuvwxyz
888 END
889 call setline(1, lines)
890 exe "normal ggfo\<C-V>3j>.."
891 let expected =<< trim END
892 abcdefghijklmn opqrstuvwxyz
893 abc defghijklmnopqrstuvwxyz
894 abcdef ghi jklmnopqrstuvwxyz
895 abcdefghijklmn opqrstuvwxyz
896 END
897 call assert_equal(expected, getline(1, '$'))
898
899 " Test from ':help v_b_r_example'
900 %d _
901 let lines =<< trim END
902 abcdefghijklmnopqrstuvwxyz
903 abc defghijklmnopqrstuvwxyz
904 abcdef ghi jklmnopqrstuvwxyz
905 abcdefghijklmnopqrstuvwxyz
906 END
907 call setline(1, lines)
908 exe "normal ggfo\<C-V>5l3jrX"
909 let expected =<< trim END
910 abcdefghijklmnXXXXXXuvwxyz
911 abc XXXXXXhijklmnopqrstuvwxyz
912 abcdef ghi XXXXXX jklmnopqrstuvwxyz
913 abcdefghijklmnXXXXXXuvwxyz
914 END
915 call assert_equal(expected, getline(1, '$'))
916
Bram Moolenaar1f3e7d32019-12-06 20:43:36 +0100917 bwipe!
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +0200918 set tabstop& shiftwidth&
Bram Moolenaar1f3e7d32019-12-06 20:43:36 +0100919endfunc
920
Bram Moolenaar21492742021-06-04 21:57:57 +0200921func Test_visual_force_motion_feedkeys()
922 onoremap <expr> i- execute('let g:mode = mode(1)')->slice(0, 0)
923 call feedkeys('dvi-', 'x')
924 call assert_equal('nov', g:mode)
925 call feedkeys('di-', 'x')
926 call assert_equal('no', g:mode)
927 ounmap i-
928endfunc
929
Bram Moolenaar1f3e7d32019-12-06 20:43:36 +0100930" Test block-insert using cursor keys for movement
931func Test_visual_block_insert_cursor_keys()
932 new
933 call append(0, ['aaaaaa', 'bbbbbb', 'cccccc', 'dddddd'])
934 call cursor(1, 1)
935
936 exe "norm! l\<C-V>jjjlllI\<Right>\<Right> \<Esc>"
937 call assert_equal(['aaa aaa', 'bbb bbb', 'ccc ccc', 'ddd ddd'],
938 \ getline(1, 4))
939
940 call deletebufline('', 1, '$')
941 call setline(1, ['xaaa', 'bbbb', 'cccc', 'dddd'])
942 call cursor(1, 1)
943 exe "norm! \<C-V>jjjI<>\<Left>p\<Esc>"
944 call assert_equal(['<p>xaaa', '<p>bbbb', '<p>cccc', '<p>dddd'],
945 \ getline(1, 4))
946 bwipe!
947endfunc
948
949func Test_visual_block_create()
950 new
951 call append(0, '')
952 " Test for Visual block was created with the last <C-v>$
953 call setline(1, ['A23', '4567'])
954 call cursor(1, 1)
955 exe "norm! l\<C-V>j$Aab\<Esc>"
956 call assert_equal(['A23ab', '4567ab'], getline(1, 2))
957
958 " Test for Visual block was created with the middle <C-v>$ (1)
959 call deletebufline('', 1, '$')
960 call setline(1, ['B23', '4567'])
961 call cursor(1, 1)
962 exe "norm! l\<C-V>j$hAab\<Esc>"
963 call assert_equal(['B23 ab', '4567ab'], getline(1, 2))
964
965 " Test for Visual block was created with the middle <C-v>$ (2)
966 call deletebufline('', 1, '$')
967 call setline(1, ['C23', '4567'])
968 call cursor(1, 1)
969 exe "norm! l\<C-V>j$hhAab\<Esc>"
970 call assert_equal(['C23ab', '456ab7'], getline(1, 2))
971 bwipe!
972endfunc
973
974" Test for Visual block insert when virtualedit=all
975func Test_virtualedit_visual_block()
976 set ve=all
977 new
978 call append(0, ["\t\tline1", "\t\tline2", "\t\tline3"])
979 call cursor(1, 1)
980 exe "norm! 07l\<C-V>jjIx\<Esc>"
981 call assert_equal([" x \tline1",
982 \ " x \tline2",
983 \ " x \tline3"], getline(1, 3))
984
985 " Test for Visual block append when virtualedit=all
986 exe "norm! 012l\<C-v>jjAx\<Esc>"
987 call assert_equal([' x x line1',
988 \ ' x x line2',
989 \ ' x x line3'], getline(1, 3))
990 set ve=
991 bwipe!
992endfunc
993
994" Test for changing case
995func Test_visual_change_case()
996 new
997 " gUe must uppercase a whole word, also when ß changes to SS
998 exe "normal Gothe youtußeuu end\<Esc>Ypk0wgUe\r"
999 " gUfx must uppercase until x, inclusive.
1000 exe "normal O- youßtußexu -\<Esc>0fogUfx\r"
1001 " VU must uppercase a whole line
1002 exe "normal YpkVU\r"
1003 " same, when it's the last line in the buffer
1004 exe "normal YPGi111\<Esc>VUddP\r"
1005 " Uppercase two lines
1006 exe "normal Oblah di\rdoh dut\<Esc>VkUj\r"
1007 " Uppercase part of two lines
1008 exe "normal ddppi333\<Esc>k0i222\<Esc>fyllvjfuUk"
1009 call assert_equal(['the YOUTUSSEUU end', '- yOUSSTUSSEXu -',
1010 \ 'THE YOUTUSSEUU END', '111THE YOUTUSSEUU END', 'BLAH DI', 'DOH DUT',
1011 \ '222the yoUTUSSEUU END', '333THE YOUTUßeuu end'], getline(2, '$'))
1012 bwipe!
1013endfunc
1014
1015" Test for Visual replace using Enter or NL
1016func Test_visual_replace_crnl()
1017 new
1018 exe "normal G3o123456789\e2k05l\<C-V>2jr\r"
1019 exe "normal G3o98765\e2k02l\<C-V>2jr\<C-V>\r\n"
1020 exe "normal G3o123456789\e2k05l\<C-V>2jr\n"
1021 exe "normal G3o98765\e2k02l\<C-V>2jr\<C-V>\n"
1022 call assert_equal(['12345', '789', '12345', '789', '12345', '789', "98\r65",
1023 \ "98\r65", "98\r65", '12345', '789', '12345', '789', '12345', '789',
1024 \ "98\n65", "98\n65", "98\n65"], getline(2, '$'))
1025 bwipe!
1026endfunc
1027
1028func Test_ve_block_curpos()
1029 new
1030 " Test cursor position. When ve=block and Visual block mode and $gj
1031 call append(0, ['12345', '789'])
1032 call cursor(1, 3)
1033 set virtualedit=block
1034 exe "norm! \<C-V>$gj\<Esc>"
1035 call assert_equal([0, 2, 4, 0], getpos("'>"))
1036 set virtualedit=
1037 bwipe!
1038endfunc
1039
1040" Test for block_insert when replacing spaces in front of the a with tabs
1041func Test_block_insert_replace_tabs()
1042 new
1043 set ts=8 sts=4 sw=4
1044 call append(0, ["#define BO_ALL\t 0x0001",
1045 \ "#define BO_BS\t 0x0002",
1046 \ "#define BO_CRSR\t 0x0004"])
1047 call cursor(1, 1)
1048 exe "norm! f0\<C-V>2jI\<tab>\<esc>"
1049 call assert_equal([
1050 \ "#define BO_ALL\t\t0x0001",
1051 \ "#define BO_BS\t \t0x0002",
1052 \ "#define BO_CRSR\t \t0x0004", ''], getline(1, '$'))
1053 set ts& sts& sw&
1054 bwipe!
1055endfunc
1056
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01001057" Test for * register in :
1058func Test_star_register()
1059 call assert_fails('*bfirst', 'E16:')
1060 new
1061 call setline(1, ['foo', 'bar', 'baz', 'qux'])
1062 exe "normal jVj\<ESC>"
1063 *yank r
1064 call assert_equal("bar\nbaz\n", @r)
1065
1066 delmarks < >
1067 call assert_fails('*yank', 'E20:')
1068 close!
1069endfunc
1070
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01001071" Test for changing text in visual mode with 'exclusive' selection
1072func Test_exclusive_selection()
1073 new
1074 call setline(1, ['one', 'two'])
1075 set selection=exclusive
1076 call feedkeys("vwcabc", 'xt')
1077 call assert_equal('abctwo', getline(1))
1078 call setline(1, ["\tone"])
1079 set virtualedit=all
1080 call feedkeys('0v2lcl', 'xt')
1081 call assert_equal('l one', getline(1))
1082 set virtualedit&
1083 set selection&
1084 close!
1085endfunc
1086
Bram Moolenaard1ad99b2020-10-04 16:16:54 +02001087" Test for starting linewise visual with a count.
1088" This test needs to be run without any previous visual mode. Otherwise the
1089" count will use the count from the previous visual mode.
1090func Test_linewise_visual_with_count()
1091 let after =<< trim [CODE]
1092 call setline(1, ['one', 'two', 'three', 'four'])
1093 norm! 3Vy
1094 call assert_equal("one\ntwo\nthree\n", @")
1095 call writefile(v:errors, 'Xtestout')
1096 qall!
1097 [CODE]
1098 if RunVim([], after, '')
1099 call assert_equal([], readfile('Xtestout'))
1100 call delete('Xtestout')
1101 endif
1102endfunc
1103
1104" Test for starting characterwise visual with a count.
1105" This test needs to be run without any previous visual mode. Otherwise the
1106" count will use the count from the previous visual mode.
1107func Test_characterwise_visual_with_count()
1108 let after =<< trim [CODE]
1109 call setline(1, ['one two', 'three'])
1110 norm! l5vy
1111 call assert_equal("ne tw", @")
1112 call writefile(v:errors, 'Xtestout')
1113 qall!
1114 [CODE]
1115 if RunVim([], after, '')
1116 call assert_equal([], readfile('Xtestout'))
1117 call delete('Xtestout')
1118 endif
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01001119endfunc
1120
Bram Moolenaar224a5f12020-04-28 20:29:07 +02001121" Test for visually selecting an inner block (iB)
1122func Test_visual_inner_block()
1123 new
1124 call setline(1, ['one', '{', 'two', '{', 'three', '}', 'four', '}', 'five'])
1125 call cursor(5, 1)
1126 " visually select all the lines in the block and then execute iB
1127 call feedkeys("ViB\<C-C>", 'xt')
1128 call assert_equal([0, 5, 1, 0], getpos("'<"))
1129 call assert_equal([0, 5, 6, 0], getpos("'>"))
1130 " visually select two inner blocks
1131 call feedkeys("ViBiB\<C-C>", 'xt')
1132 call assert_equal([0, 3, 1, 0], getpos("'<"))
1133 call assert_equal([0, 7, 5, 0], getpos("'>"))
1134 " try to select non-existing inner block
1135 call cursor(5, 1)
1136 call assert_beeps('normal ViBiBiB')
1137 " try to select a unclosed inner block
1138 8,9d
1139 call cursor(5, 1)
1140 call assert_beeps('normal ViBiB')
1141 close!
1142endfunc
1143
Bram Moolenaarcd942772020-08-22 21:08:44 +02001144func Test_visual_put_in_block()
1145 new
1146 call setline(1, ['xxxx', 'yyy', 'zzzz'])
1147 normal 1G2yl
1148 exe "normal 1G2l\<C-V>jjlp"
1149 call assert_equal(['xxxx', 'yxx', 'zzxx'], getline(1, 3))
1150 bwipe!
1151endfunc
1152
Christian Brabandt2fa93842021-05-30 22:17:25 +02001153func Test_visual_put_in_block_using_zp()
1154 new
1155 " paste using zP
1156 call setline(1, ['/path;text', '/path;text', '/path;text', '',
1157 \ '/subdir',
1158 \ '/longsubdir',
1159 \ '/longlongsubdir'])
1160 exe "normal! 5G\<c-v>2j$y"
1161 norm! 1Gf;zP
1162 call assert_equal(['/path/subdir;text', '/path/longsubdir;text', '/path/longlongsubdir;text'], getline(1, 3))
1163 %d
1164 " paste using zP
1165 call setline(1, ['/path;text', '/path;text', '/path;text', '',
1166 \ '/subdir',
1167 \ '/longsubdir',
1168 \ '/longlongsubdir'])
1169 exe "normal! 5G\<c-v>2j$y"
1170 norm! 1Gf;hzp
1171 call assert_equal(['/path/subdir;text', '/path/longsubdir;text', '/path/longlongsubdir;text'], getline(1, 3))
1172 bwipe!
1173endfunc
1174
Christian Brabandt544a38e2021-06-10 19:39:11 +02001175func Test_visual_put_in_block_using_zy_and_zp()
1176 new
1177
1178 " Test 1) Paste using zp - after the cursor without trailing spaces
1179 call setline(1, ['/path;text', '/path;text', '/path;text', '',
1180 \ 'texttext /subdir columntext',
1181 \ 'texttext /longsubdir columntext',
1182 \ 'texttext /longlongsubdir columntext'])
1183 exe "normal! 5G0f/\<c-v>2jezy"
1184 norm! 1G0f;hzp
1185 call assert_equal(['/path/subdir;text', '/path/longsubdir;text', '/path/longlongsubdir;text'], getline(1, 3))
1186
1187 " Test 2) Paste using zP - in front of the cursor without trailing spaces
1188 %d
1189 call setline(1, ['/path;text', '/path;text', '/path;text', '',
1190 \ 'texttext /subdir columntext',
1191 \ 'texttext /longsubdir columntext',
1192 \ 'texttext /longlongsubdir columntext'])
1193 exe "normal! 5G0f/\<c-v>2jezy"
1194 norm! 1G0f;zP
1195 call assert_equal(['/path/subdir;text', '/path/longsubdir;text', '/path/longlongsubdir;text'], getline(1, 3))
1196
1197 " Test 3) Paste using p - with trailing spaces
1198 %d
1199 call setline(1, ['/path;text', '/path;text', '/path;text', '',
1200 \ 'texttext /subdir columntext',
1201 \ 'texttext /longsubdir columntext',
1202 \ 'texttext /longlongsubdir columntext'])
1203 exe "normal! 5G0f/\<c-v>2jezy"
1204 norm! 1G0f;hp
1205 call assert_equal(['/path/subdir ;text', '/path/longsubdir ;text', '/path/longlongsubdir;text'], getline(1, 3))
1206
1207 " Test 4) Paste using P - with trailing spaces
1208 %d
1209 call setline(1, ['/path;text', '/path;text', '/path;text', '',
1210 \ 'texttext /subdir columntext',
1211 \ 'texttext /longsubdir columntext',
1212 \ 'texttext /longlongsubdir columntext'])
1213 exe "normal! 5G0f/\<c-v>2jezy"
1214 norm! 1G0f;P
1215 call assert_equal(['/path/subdir ;text', '/path/longsubdir ;text', '/path/longlongsubdir;text'], getline(1, 3))
1216
1217 " Test 5) Yank with spaces inside the block
1218 %d
1219 call setline(1, ['/path;text', '/path;text', '/path;text', '',
1220 \ 'texttext /sub dir/ columntext',
1221 \ 'texttext /lon gsubdir/ columntext',
1222 \ 'texttext /lon glongsubdir/ columntext'])
1223 exe "normal! 5G0f/\<c-v>2jf/zy"
1224 norm! 1G0f;zP
1225 call assert_equal(['/path/sub dir/;text', '/path/lon gsubdir/;text', '/path/lon glongsubdir/;text'], getline(1, 3))
1226 bwipe!
1227endfunc
1228
Bram Moolenaar7d7bcc62021-06-28 21:54:27 +02001229func Test_visual_put_blockedit_zy_and_zp()
1230 new
1231
1232 call setline(1, ['aa', 'bbbbb', 'ccc', '', 'XX', 'GGHHJ', 'RTZU'])
1233 exe "normal! gg0\<c-v>2j$zy"
1234 norm! 5gg0zP
1235 call assert_equal(['aa', 'bbbbb', 'ccc', '', 'aaXX', 'bbbbbGGHHJ', 'cccRTZU'], getline(1, 7))
1236 "
1237 " now with blockmode editing
1238 sil %d
1239 :set ve=block
1240 call setline(1, ['aa', 'bbbbb', 'ccc', '', 'XX', 'GGHHJ', 'RTZU'])
1241 exe "normal! gg0\<c-v>2j$zy"
1242 norm! 5gg0zP
1243 call assert_equal(['aa', 'bbbbb', 'ccc', '', 'aaXX', 'bbbbbGGHHJ', 'cccRTZU'], getline(1, 7))
1244 set ve&vim
1245 bw!
1246endfunc
1247
Christian Brabandt544a38e2021-06-10 19:39:11 +02001248
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +01001249" vim: shiftwidth=2 sts=2 expandtab