blob: af54615c48a104985b203d4ae27e899a78a048f1 [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 Moolenaar9cee4a12021-07-03 15:08:37 +02005source screendump.vim
Bram Moolenaard1ad99b2020-10-04 16:16:54 +02006
Bram Moolenaar019b9c62016-03-05 17:26:00 +01007func Test_block_shift_multibyte()
Bram Moolenaarf8eb9c52017-01-02 17:31:24 +01008 " Uses double-wide character.
Bram Moolenaar019b9c62016-03-05 17:26:00 +01009 split
10 call setline(1, ['xヹxxx', 'ヹxxx'])
11 exe "normal 1G0l\<C-V>jl>"
12 call assert_equal('x ヹxxx', getline(1))
13 call assert_equal(' ヹxxx', getline(2))
14 q!
15endfunc
Bram Moolenaarf8eb9c52017-01-02 17:31:24 +010016
Bram Moolenaarbae5a172017-08-06 15:42:06 +020017func Test_block_shift_overflow()
18 " This used to cause a multiplication overflow followed by a crash.
19 new
20 normal ii
21 exe "normal \<C-V>876543210>"
22 q!
23endfunc
24
Bram Moolenaarf8eb9c52017-01-02 17:31:24 +010025func Test_dotregister_paste()
26 new
27 exe "norm! ihello world\<esc>"
28 norm! 0ve".p
29 call assert_equal('hello world world', getline(1))
30 q!
31endfunc
Bram Moolenaar23fa81d2017-02-01 21:50:21 +010032
33func Test_Visual_ctrl_o()
34 new
35 call setline(1, ['one', 'two', 'three'])
36 call cursor(1,2)
37 set noshowmode
38 set tw=0
39 call feedkeys("\<c-v>jjlIa\<c-\>\<c-o>:set tw=88\<cr>\<esc>", 'tx')
40 call assert_equal(['oane', 'tawo', 'tahree'], getline(1, 3))
41 call assert_equal(88, &tw)
42 set tw&
43 bw!
44endfu
Bram Moolenaar84b2a382017-02-17 11:40:00 +010045
46func Test_Visual_vapo()
47 new
48 normal oxx
49 normal vapo
50 bwipe!
51endfunc
Bram Moolenaar46522af2017-02-18 23:12:01 +010052
53func Test_Visual_inner_quote()
54 new
55 normal oxX
56 normal vki'
57 bwipe!
58endfunc
Bram Moolenaar75373f32017-08-07 22:02:30 +020059
60" Test for Visual mode not being reset causing E315 error.
61func TriggerTheProblem()
62 " At this point there is no visual selection because :call reset it.
63 " Let's restore the selection:
64 normal gv
65 '<,'>del _
66 try
67 exe "normal \<Esc>"
68 catch /^Vim\%((\a\+)\)\=:E315/
69 echom 'Snap! E315 error!'
Bram Moolenaar63e82db2018-03-06 12:10:48 +010070 let g:msg = 'Snap! E315 error!'
Bram Moolenaar75373f32017-08-07 22:02:30 +020071 endtry
72endfunc
73
74func Test_visual_mode_reset()
Bram Moolenaar75373f32017-08-07 22:02:30 +020075 enew
Bram Moolenaar63e82db2018-03-06 12:10:48 +010076 let g:msg = "Everything's fine."
Bram Moolenaar75373f32017-08-07 22:02:30 +020077 enew
78 setl buftype=nofile
79 call append(line('$'), 'Delete this line.')
80
81 " NOTE: this has to be done by a call to a function because executing :del
82 " the ex-way will require the colon operator which resets the visual mode
83 " thus preventing the problem:
84 exe "normal! GV:call TriggerTheProblem()\<CR>"
85 call assert_equal("Everything's fine.", g:msg)
Bram Moolenaar75373f32017-08-07 22:02:30 +020086endfunc
Bram Moolenaar67418d92017-10-15 22:07:39 +020087
Bram Moolenaar15993ce2017-10-26 20:21:44 +020088" Test for visual block shift and tab characters.
89func Test_block_shift_tab()
Bram Moolenaar3e72dca2021-05-29 16:30:12 +020090 new
Bram Moolenaar15993ce2017-10-26 20:21:44 +020091 call append(0, repeat(['one two three'], 5))
92 call cursor(1,1)
93 exe "normal i\<C-G>u"
94 exe "normal fe\<C-V>4jR\<Esc>ugvr1"
95 call assert_equal('on1 two three', getline(1))
96 call assert_equal('on1 two three', getline(2))
97 call assert_equal('on1 two three', getline(5))
98
Bram Moolenaar3e72dca2021-05-29 16:30:12 +020099 %d _
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200100 call append(0, repeat(['abcdefghijklmnopqrstuvwxyz'], 5))
101 call cursor(1,1)
102 exe "normal \<C-V>4jI \<Esc>j<<11|D"
103 exe "normal j7|a\<Tab>\<Tab>"
104 exe "normal j7|a\<Tab>\<Tab> "
105 exe "normal j7|a\<Tab> \<Tab>\<Esc>4k13|\<C-V>4j<"
106 call assert_equal(' abcdefghijklmnopqrstuvwxyz', getline(1))
107 call assert_equal('abcdefghij', getline(2))
108 call assert_equal(" abc\<Tab> defghijklmnopqrstuvwxyz", getline(3))
109 call assert_equal(" abc\<Tab> defghijklmnopqrstuvwxyz", getline(4))
110 call assert_equal(" abc\<Tab> defghijklmnopqrstuvwxyz", getline(5))
111
112 %s/\s\+//g
113 call cursor(1,1)
114 exe "normal \<C-V>4jI \<Esc>j<<"
115 exe "normal j7|a\<Tab>\<Tab>"
116 exe "normal j7|a\<Tab>\<Tab>\<Tab>\<Tab>\<Tab>"
117 exe "normal j7|a\<Tab> \<Tab>\<Tab>\<Esc>4k13|\<C-V>4j3<"
118 call assert_equal(' abcdefghijklmnopqrstuvwxyz', getline(1))
119 call assert_equal('abcdefghij', getline(2))
120 call assert_equal(" abc\<Tab> defghijklmnopqrstuvwxyz", getline(3))
121 call assert_equal(" abc\<Tab>\<Tab>defghijklmnopqrstuvwxyz", getline(4))
122 call assert_equal(" abc\<Tab> defghijklmnopqrstuvwxyz", getline(5))
123
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200124 " Test for block shift with space characters at the beginning and with
125 " 'noexpandtab' and 'expandtab'
126 %d _
127 call setline(1, [" 1", " 2", " 3"])
128 setlocal shiftwidth=2 noexpandtab
129 exe "normal gg\<C-V>3j>"
130 call assert_equal(["\t1", "\t2", "\t3"], getline(1, '$'))
131 %d _
132 call setline(1, [" 1", " 2", " 3"])
133 setlocal shiftwidth=2 expandtab
134 exe "normal gg\<C-V>3j>"
135 call assert_equal([" 1", " 2", " 3"], getline(1, '$'))
136 setlocal shiftwidth&
137
138 bw!
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200139endfunc
140
141" Tests Blockwise Visual when there are TABs before the text.
142func Test_blockwise_visual()
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200143 new
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200144 call append(0, ['123456',
145 \ '234567',
146 \ '345678',
147 \ '',
148 \ 'test text test tex start here',
149 \ "\t\tsome text",
150 \ "\t\ttest text",
151 \ 'test text'])
152 call cursor(1,1)
153 exe "normal /start here$\<CR>"
154 exe 'normal "by$' . "\<C-V>jjlld"
155 exe "normal /456$\<CR>"
156 exe "normal \<C-V>jj" . '"bP'
157 call assert_equal(['123start here56',
158 \ '234start here67',
159 \ '345start here78',
160 \ '',
161 \ 'test text test tex rt here',
162 \ "\t\tsomext",
163 \ "\t\ttesext"], getline(1, 7))
164
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200165 bw!
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200166endfunc
167
Bram Moolenaar2e949762018-05-20 14:06:38 +0200168" Test swapping corners in blockwise visual mode with o and O
169func Test_blockwise_visual_o_O()
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200170 new
Bram Moolenaar2e949762018-05-20 14:06:38 +0200171
172 exe "norm! 10i.\<Esc>Y4P3lj\<C-V>4l2jr "
173 exe "norm! gvO\<Esc>ra"
174 exe "norm! gvO\<Esc>rb"
175 exe "norm! gvo\<C-c>rc"
176 exe "norm! gvO\<C-c>rd"
Bram Moolenaar1671f442020-03-10 07:48:13 +0100177 set selection=exclusive
178 exe "norm! gvOo\<C-c>re"
179 call assert_equal('...a be.', getline(4))
180 exe "norm! gvOO\<C-c>rf"
181 set selection&
Bram Moolenaar2e949762018-05-20 14:06:38 +0200182
183 call assert_equal(['..........',
184 \ '...c d..',
185 \ '... ..',
Bram Moolenaar1671f442020-03-10 07:48:13 +0100186 \ '...a bf.',
Bram Moolenaar2e949762018-05-20 14:06:38 +0200187 \ '..........'], getline(1, '$'))
188
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200189 bw!
Bram Moolenaar2e949762018-05-20 14:06:38 +0200190endfun
191
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200192" Test Virtual replace mode.
193func Test_virtual_replace()
Bram Moolenaardf0d24b2018-03-06 14:22:58 +0100194 if exists('&t_kD')
195 let save_t_kD = &t_kD
196 endif
197 if exists('&t_kb')
198 let save_t_kb = &t_kb
199 endif
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200200 exe "set t_kD=\<C-V>x7f t_kb=\<C-V>x08"
201 enew!
202 exe "normal a\nabcdefghi\njk\tlmn\n opq rst\n\<C-D>uvwxyz"
203 call cursor(1,1)
204 set ai bs=2
205 exe "normal gR0\<C-D> 1\nA\nBCDEFGHIJ\n\tKL\nMNO\nPQR"
206 call assert_equal([' 1',
207 \ ' A',
208 \ ' BCDEFGHIJ',
209 \ ' KL',
210 \ ' MNO',
211 \ ' PQR',
212 \ ], getline(1, 6))
213 normal G
214 mark a
215 exe "normal o0\<C-D>\nabcdefghi\njk\tlmn\n opq\trst\n\<C-D>uvwxyz\n"
216 exe "normal 'ajgR0\<C-D> 1\nA\nBCDEFGHIJ\n\tKL\nMNO\nPQR" . repeat("\<BS>", 29)
217 call assert_equal([' 1',
218 \ 'abcdefghi',
219 \ 'jk lmn',
220 \ ' opq rst',
221 \ 'uvwxyz'], getline(7, 11))
222 normal G
223 exe "normal iab\tcdefghi\tjkl"
224 exe "normal 0gRAB......CDEFGHI.J\<Esc>o"
225 exe "normal iabcdefghijklmnopqrst\<Esc>0gRAB\tIJKLMNO\tQR"
226 call assert_equal(['AB......CDEFGHI.Jkl',
227 \ 'AB IJKLMNO QRst'], getline(12, 13))
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200228
229 " Test inserting Tab with 'noexpandtab' and 'softabstop' set to 4
230 %d
231 call setline(1, 'aaaaaaaaaaaaa')
232 set softtabstop=4
233 exe "normal gggR\<Tab>\<Tab>x"
234 call assert_equal("\txaaaa", getline(1))
235 set softtabstop&
236
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200237 enew!
Bram Moolenaare7808482018-03-06 13:17:23 +0100238 set noai bs&vim
Bram Moolenaardf0d24b2018-03-06 14:22:58 +0100239 if exists('save_t_kD')
240 let &t_kD = save_t_kD
241 endif
242 if exists('save_t_kb')
243 let &t_kb = save_t_kb
244 endif
Bram Moolenaar63e82db2018-03-06 12:10:48 +0100245endfunc
246
247" Test Virtual replace mode.
248func Test_virtual_replace2()
249 enew!
250 set bs=2
251 exe "normal a\nabcdefghi\njk\tlmn\n opq rst\n\<C-D>uvwxyz"
252 call cursor(1,1)
253 " Test 1: Test that del deletes the newline
254 exe "normal gR0\<del> 1\nA\nBCDEFGHIJ\n\tKL\nMNO\nPQR"
255 call assert_equal(['0 1',
256 \ 'A',
257 \ 'BCDEFGHIJ',
258 \ ' KL',
259 \ 'MNO',
260 \ 'PQR',
261 \ ], getline(1, 6))
262 " Test 2:
263 " a newline is not deleted, if no newline has been added in virtual replace mode
264 %d_
265 call setline(1, ['abcd', 'efgh', 'ijkl'])
266 call cursor(2,1)
267 exe "norm! gR1234\<cr>5\<bs>\<bs>\<bs>"
268 call assert_equal(['abcd',
269 \ '123h',
270 \ 'ijkl'], getline(1, '$'))
271 " Test 3:
272 " a newline is deleted, if a newline has been inserted before in virtual replace mode
273 %d_
274 call setline(1, ['abcd', 'efgh', 'ijkl'])
275 call cursor(2,1)
276 exe "norm! gR1234\<cr>\<cr>56\<bs>\<bs>\<bs>"
277 call assert_equal(['abcd',
278 \ '1234',
279 \ 'ijkl'], getline(1, '$'))
280 " Test 4:
281 " delete add a newline, delete it, add it again and check undo
282 %d_
283 call setline(1, ['abcd', 'efgh', 'ijkl'])
284 call cursor(2,1)
285 " break undo sequence explicitly
286 let &ul = &ul
287 exe "norm! gR1234\<cr>\<bs>\<del>56\<cr>"
288 let &ul = &ul
289 call assert_equal(['abcd',
290 \ '123456',
291 \ ''], getline(1, '$'))
292 norm! u
293 call assert_equal(['abcd',
294 \ 'efgh',
295 \ 'ijkl'], getline(1, '$'))
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200296
297 " Test for truncating spaces in a newly added line using 'autoindent' if
298 " characters are not added to that line.
299 %d_
300 call setline(1, [' app', ' bee', ' cat'])
301 setlocal autoindent
302 exe "normal gg$gRt\n\nr"
303 call assert_equal([' apt', '', ' rat'], getline(1, '$'))
304
Bram Moolenaar63e82db2018-03-06 12:10:48 +0100305 " clean up
306 %d_
307 set bs&vim
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200308endfunc
Bram Moolenaar6d3a1942019-01-03 23:10:32 +0100309
Bram Moolenaar81b1ba42019-01-13 16:12:40 +0100310func Test_Visual_word_textobject()
311 new
312 call setline(1, ['First sentence. Second sentence.'])
313
314 " When start and end of visual area are identical, 'aw' or 'iw' select
315 " the whole word.
316 norm! 1go2fcvawy
317 call assert_equal('Second ', @")
318 norm! 1go2fcviwy
319 call assert_equal('Second', @")
320
321 " When start and end of visual area are not identical, 'aw' or 'iw'
322 " extend the word in direction of the end of the visual area.
323 norm! 1go2fcvlawy
324 call assert_equal('cond ', @")
325 norm! gv2awy
326 call assert_equal('cond sentence.', @")
327
328 norm! 1go2fcvliwy
329 call assert_equal('cond', @")
330 norm! gv2iwy
331 call assert_equal('cond sentence', @")
332
333 " Extend visual area in opposite direction.
334 norm! 1go2fcvhawy
335 call assert_equal(' Sec', @")
336 norm! gv2awy
337 call assert_equal(' sentence. Sec', @")
338
339 norm! 1go2fcvhiwy
340 call assert_equal('Sec', @")
341 norm! gv2iwy
342 call assert_equal('. Sec', @")
343
344 bwipe!
345endfunc
346
Bram Moolenaar6d3a1942019-01-03 23:10:32 +0100347func Test_Visual_sentence_textobject()
348 new
Bram Moolenaar81b1ba42019-01-13 16:12:40 +0100349 call setline(1, ['First sentence. Second sentence. Third', 'sentence. Fourth sentence'])
Bram Moolenaar6d3a1942019-01-03 23:10:32 +0100350
351 " When start and end of visual area are identical, 'as' or 'is' select
352 " the whole sentence.
353 norm! 1gofdvasy
354 call assert_equal('Second sentence. ', @")
355 norm! 1gofdvisy
356 call assert_equal('Second sentence.', @")
357
358 " When start and end of visual area are not identical, 'as' or 'is'
359 " extend the sentence in direction of the end of the visual area.
360 norm! 1gofdvlasy
361 call assert_equal('d sentence. ', @")
362 norm! gvasy
363 call assert_equal("d sentence. Third\nsentence. ", @")
364
365 norm! 1gofdvlisy
366 call assert_equal('d sentence.', @")
367 norm! gvisy
368 call assert_equal('d sentence. ', @")
369 norm! gvisy
370 call assert_equal("d sentence. Third\nsentence.", @")
371
372 " Extend visual area in opposite direction.
373 norm! 1gofdvhasy
374 call assert_equal(' Second', @")
375 norm! gvasy
376 call assert_equal("First sentence. Second", @")
377
378 norm! 1gofdvhisy
379 call assert_equal('Second', @")
380 norm! gvisy
381 call assert_equal(' Second', @")
382 norm! gvisy
383 call assert_equal('First sentence. Second', @")
384
385 bwipe!
386endfunc
Bram Moolenaar81b1ba42019-01-13 16:12:40 +0100387
388func Test_Visual_paragraph_textobject()
389 new
Bram Moolenaar8a9bc952020-10-02 18:48:07 +0200390 let lines =<< trim [END]
391 First line.
392
393 Second line.
394 Third line.
395 Fourth line.
396 Fifth line.
397
398 Sixth line.
399 [END]
400 call setline(1, lines)
Bram Moolenaar81b1ba42019-01-13 16:12:40 +0100401
402 " When start and end of visual area are identical, 'ap' or 'ip' select
403 " the whole paragraph.
404 norm! 4ggvapy
405 call assert_equal("Second line.\nThird line.\nFourth line.\nFifth line.\n\n", @")
406 norm! 4ggvipy
407 call assert_equal("Second line.\nThird line.\nFourth line.\nFifth line.\n", @")
408
409 " When start and end of visual area are not identical, 'ap' or 'ip'
410 " extend the sentence in direction of the end of the visual area.
411 " FIXME: actually, it is not sufficient to have different start and
412 " end of visual selection, the start line and end line have to differ,
413 " which is not consistent with the documentation.
414 norm! 4ggVjapy
415 call assert_equal("Third line.\nFourth line.\nFifth line.\n\n", @")
416 norm! gvapy
417 call assert_equal("Third line.\nFourth line.\nFifth line.\n\nSixth line.\n", @")
418 norm! 4ggVjipy
419 call assert_equal("Third line.\nFourth line.\nFifth line.\n", @")
420 norm! gvipy
421 call assert_equal("Third line.\nFourth line.\nFifth line.\n\n", @")
422 norm! gvipy
423 call assert_equal("Third line.\nFourth line.\nFifth line.\n\nSixth line.\n", @")
424
425 " Extend visual area in opposite direction.
426 norm! 5ggVkapy
427 call assert_equal("\nSecond line.\nThird line.\nFourth line.\n", @")
428 norm! gvapy
429 call assert_equal("First line.\n\nSecond line.\nThird line.\nFourth line.\n", @")
430 norm! 5ggVkipy
431 call assert_equal("Second line.\nThird line.\nFourth line.\n", @")
432 norma gvipy
433 call assert_equal("\nSecond line.\nThird line.\nFourth line.\n", @")
434 norm! gvipy
435 call assert_equal("First line.\n\nSecond line.\nThird line.\nFourth line.\n", @")
436
437 bwipe!
438endfunc
Bram Moolenaar19a66852019-03-07 11:25:32 +0100439
440func Test_curswant_not_changed()
441 new
442 call setline(1, ['one', 'two'])
443 au InsertLeave * call getcurpos()
444 call feedkeys("gg0\<C-V>jI123 \<Esc>j", 'xt')
445 call assert_equal([0, 2, 1, 0, 1], getcurpos())
446
447 bwipe!
448 au! InsertLeave
449endfunc
Bram Moolenaarc6b37db2019-04-27 18:00:34 +0200450
451" Tests for "vaBiB", end could be wrong.
452func Test_Visual_Block()
453 new
454 a
455- Bug in "vPPPP" on this text:
456 {
457 cmd;
458 {
459 cmd;\t/* <-- Start cursor here */
460 {
461 }
462 }
463 }
464.
465 normal gg
466 call search('Start cursor here')
467 normal vaBiBD
468 call assert_equal(['- Bug in "vPPPP" on this text:',
469 \ "\t{",
470 \ "\t}"], getline(1, '$'))
471
472 close!
473endfunc
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +0100474
475" Test for 'p'ut in visual block mode
476func Test_visual_block_put()
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200477 new
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +0100478 call append(0, ['One', 'Two', 'Three'])
479 normal gg
480 yank
481 call feedkeys("jl\<C-V>ljp", 'xt')
482 call assert_equal(['One', 'T', 'Tee', 'One', ''], getline(1, '$'))
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200483 bw!
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +0100484endfunc
485
Bram Moolenaar309976e2019-12-05 18:16:33 +0100486" Visual modes (v V CTRL-V) followed by an operator; count; repeating
487func Test_visual_mode_op()
488 new
489 call append(0, '')
490
491 call setline(1, 'apple banana cherry')
492 call cursor(1, 1)
493 normal lvld.l3vd.
494 call assert_equal('a y', getline(1))
495
496 call setline(1, ['line 1 line 1', 'line 2 line 2', 'line 3 line 3',
497 \ 'line 4 line 4', 'line 5 line 5', 'line 6 line 6'])
498 call cursor(1, 1)
499 exe "normal Vcnewline\<Esc>j.j2Vd."
500 call assert_equal(['newline', 'newline'], getline(1, '$'))
501
502 call deletebufline('', 1, '$')
503 call setline(1, ['xxxxxxxxxxxxx', 'xxxxxxxxxxxxx', 'xxxxxxxxxxxxx',
504 \ 'xxxxxxxxxxxxx'])
505 exe "normal \<C-V>jlc \<Esc>l.l2\<C-V>c----\<Esc>l."
506 call assert_equal([' --------x',
507 \ ' --------x',
508 \ 'xxxx--------x',
509 \ 'xxxx--------x'], getline(1, '$'))
510
511 bwipe!
512endfunc
513
514" Visual mode maps (movement and text object)
515" Visual mode maps; count; repeating
516" - Simple
517" - With an Ex command (custom text object)
518func Test_visual_mode_maps()
519 new
520 call append(0, '')
521
522 func SelectInCaps()
523 let [line1, col1] = searchpos('\u', 'bcnW')
524 let [line2, col2] = searchpos('.\u', 'nW')
525 call setpos("'<", [0, line1, col1, 0])
526 call setpos("'>", [0, line2, col2, 0])
527 normal! gv
528 endfunction
529
530 vnoremap W /\u/s-1<CR>
531 vnoremap iW :<C-U>call SelectInCaps()<CR>
532
533 call setline(1, 'KiwiRaspberryDateWatermelonPeach')
534 call cursor(1, 1)
535 exe "normal vWcNo\<Esc>l.fD2vd."
536 call assert_equal('NoNoberryach', getline(1))
537
538 call setline(1, 'JambuRambutanBananaTangerineMango')
539 call cursor(1, 1)
540 exe "normal llviWc-\<Esc>l.l2vdl."
541 call assert_equal('--ago', getline(1))
542
543 vunmap W
544 vunmap iW
545 bwipe!
546 delfunc SelectInCaps
547endfunc
548
549" Operator-pending mode maps (movement and text object)
550" - Simple
551" - With Ex command moving the cursor
552" - With Ex command and Visual selection (custom text object)
553func Test_visual_oper_pending_mode_maps()
554 new
555 call append(0, '')
556
557 func MoveToCap()
558 call search('\u', 'W')
559 endfunction
560
561 func SelectInCaps()
562 let [line1, col1] = searchpos('\u', 'bcnW')
563 let [line2, col2] = searchpos('.\u', 'nW')
564 call setpos("'<", [0, line1, col1, 0])
565 call setpos("'>", [0, line2, col2, 0])
566 normal! gv
567 endfunction
568
569 onoremap W /\u/<CR>
570 onoremap <Leader>W :<C-U>call MoveToCap()<CR>
571 onoremap iW :<C-U>call SelectInCaps()<CR>
572
573 call setline(1, 'PineappleQuinceLoganberryOrangeGrapefruitKiwiZ')
574 call cursor(1, 1)
575 exe "normal cW-\<Esc>l.l2.l."
576 call assert_equal('----Z', getline(1))
577
578 call setline(1, 'JuniperDurianZ')
579 call cursor(1, 1)
580 exe "normal g?\WfD."
581 call assert_equal('WhavcreQhevnaZ', getline(1))
582
583 call setline(1, 'LemonNectarineZ')
584 call cursor(1, 1)
585 exe "normal yiWPlciWNew\<Esc>fr."
586 call assert_equal('LemonNewNewZ', getline(1))
587
588 ounmap W
589 ounmap <Leader>W
590 ounmap iW
591 bwipe!
592 delfunc MoveToCap
593 delfunc SelectInCaps
594endfunc
595
596" Patch 7.3.879: Properly abort Operator-pending mode for "dv:<Esc>" etc.
597func Test_op_pend_mode_abort()
598 new
599 call append(0, '')
600
601 call setline(1, ['zzzz', 'zzzz'])
602 call cursor(1, 1)
603
604 exe "normal dV:\<CR>dv:\<CR>"
605 call assert_equal(['zzz'], getline(1, 2))
606 set nomodifiable
607 call assert_fails('exe "normal d:\<CR>"', 'E21:')
608 set modifiable
609 call feedkeys("dv:\<Esc>dV:\<Esc>", 'xt')
610 call assert_equal(['zzz'], getline(1, 2))
611 set nomodifiable
612 let v:errmsg = ''
613 call feedkeys("d:\<Esc>", 'xt')
614 call assert_true(v:errmsg !~# '^E21:')
615 set modifiable
616
617 bwipe!
618endfunc
619
620func Test_characterwise_visual_mode()
621 new
622
623 " characterwise visual mode: replace last line
624 $put ='a'
625 let @" = 'x'
626 normal v$p
627 call assert_equal('x', getline('$'))
628
629 " characterwise visual mode: delete middle line
630 call deletebufline('', 1, '$')
631 call append('$', ['a', 'b', 'c'])
632 normal G
633 normal kkv$d
634 call assert_equal(['', 'b', 'c'], getline(1, '$'))
635
636 " characterwise visual mode: delete middle two lines
637 call deletebufline('', 1, '$')
638 call append('$', ['a', 'b', 'c'])
639 normal Gkkvj$d
640 call assert_equal(['', 'c'], getline(1, '$'))
641
642 " characterwise visual mode: delete last line
643 call deletebufline('', 1, '$')
644 call append('$', ['a', 'b', 'c'])
645 normal Gv$d
646 call assert_equal(['', 'a', 'b', ''], getline(1, '$'))
647
648 " characterwise visual mode: delete last two lines
649 call deletebufline('', 1, '$')
650 call append('$', ['a', 'b', 'c'])
651 normal Gkvj$d
652 call assert_equal(['', 'a', ''], getline(1, '$'))
653
Bram Moolenaar8a9bc952020-10-02 18:48:07 +0200654 " characterwise visual mode: use a count with the visual mode from the last
655 " line in the buffer
656 %d _
657 call setline(1, ['one', 'two', 'three', 'four'])
658 norm! vj$y
659 norm! G1vy
660 call assert_equal('four', @")
661
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200662 " characterwise visual mode: replace a single character line and the eol
663 %d _
664 call setline(1, "a")
665 normal v$rx
666 call assert_equal(['x'], getline(1, '$'))
667
Bram Moolenaar6ecf58b2021-12-16 10:05:41 +0000668 " replace a character with composing characters
669 call setline(1, "xã̳x")
670 normal gg0lvrb
671 call assert_equal("xbx", getline(1))
672
Bram Moolenaar309976e2019-12-05 18:16:33 +0100673 bwipe!
674endfunc
675
Bram Moolenaar309976e2019-12-05 18:16:33 +0100676func Test_visual_mode_put()
677 new
678
679 " v_p: replace last character with line register at middle line
680 call append('$', ['aaa', 'bbb', 'ccc'])
681 normal G
682 -2yank
683 normal k$vp
684 call assert_equal(['', 'aaa', 'bb', 'aaa', '', 'ccc'], getline(1, '$'))
685
686 " v_p: replace last character with line register at middle line selecting
687 " newline
688 call deletebufline('', 1, '$')
689 call append('$', ['aaa', 'bbb', 'ccc'])
690 normal G
691 -2yank
692 normal k$v$p
693 call assert_equal(['', 'aaa', 'bb', 'aaa', 'ccc'], getline(1, '$'))
694
695 " v_p: replace last character with line register at last line
696 call deletebufline('', 1, '$')
697 call append('$', ['aaa', 'bbb', 'ccc'])
698 normal G
699 -2yank
700 normal $vp
701 call assert_equal(['', 'aaa', 'bbb', 'cc', 'aaa', ''], getline(1, '$'))
702
703 " v_p: replace last character with line register at last line selecting
704 " newline
705 call deletebufline('', 1, '$')
706 call append('$', ['aaa', 'bbb', 'ccc'])
707 normal G
708 -2yank
709 normal $v$p
710 call assert_equal(['', 'aaa', 'bbb', 'cc', 'aaa', ''], getline(1, '$'))
711
712 bwipe!
713endfunc
714
Bram Moolenaar515545e2020-03-22 14:08:59 +0100715func Test_gv_with_exclusive_selection()
Bram Moolenaar309976e2019-12-05 18:16:33 +0100716 new
717
Bram Moolenaar515545e2020-03-22 14:08:59 +0100718 " gv with exclusive selection after an operation
Bram Moolenaar309976e2019-12-05 18:16:33 +0100719 call append('$', ['zzz ', 'äà '])
720 set selection=exclusive
721 normal Gkv3lyjv3lpgvcxxx
722 call assert_equal(['', 'zzz ', 'xxx '], getline(1, '$'))
723
Bram Moolenaar515545e2020-03-22 14:08:59 +0100724 " gv with exclusive selection without an operation
Bram Moolenaar309976e2019-12-05 18:16:33 +0100725 call deletebufline('', 1, '$')
726 call append('$', 'zzz ')
727 set selection=exclusive
728 exe "normal G0v3l\<Esc>gvcxxx"
729 call assert_equal(['', 'xxx '], getline(1, '$'))
730
731 set selection&vim
732 bwipe!
733endfunc
734
Bram Moolenaar1f3e7d32019-12-06 20:43:36 +0100735" Tests for the visual block mode commands
736func Test_visual_block_mode()
737 new
738 call append(0, '')
Bram Moolenaar1671f442020-03-10 07:48:13 +0100739 call setline(1, repeat(['abcdefghijklm'], 5))
Bram Moolenaar1f3e7d32019-12-06 20:43:36 +0100740 call cursor(1, 1)
741
742 " Test shift-right of a block
743 exe "normal jllll\<C-V>jj>wll\<C-V>jlll>"
744 " Test shift-left of a block
745 exe "normal G$hhhh\<C-V>kk<"
746 " Test block-insert
747 exe "normal Gkl\<C-V>kkkIxyz"
748 " Test block-replace
749 exe "normal Gllll\<C-V>kkklllrq"
750 " Test block-change
751 exe "normal G$khhh\<C-V>hhkkcmno"
752 call assert_equal(['axyzbcdefghijklm',
753 \ 'axyzqqqq mno ghijklm',
754 \ 'axyzqqqqef mno ghijklm',
755 \ 'axyzqqqqefgmnoklm',
756 \ 'abcdqqqqijklm'], getline(1, 5))
757
Bram Moolenaar1671f442020-03-10 07:48:13 +0100758 " Test 'C' to change till the end of the line
759 call cursor(3, 4)
760 exe "normal! \<C-V>j3lCooo"
761 call assert_equal(['axyooo', 'axyooo'], getline(3, 4))
762
763 " Test 'D' to delete till the end of the line
764 call cursor(3, 3)
765 exe "normal! \<C-V>j2lD"
766 call assert_equal(['ax', 'ax'], getline(3, 4))
767
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200768 " Test block insert with a short line that ends before the block
769 %d _
770 call setline(1, [" one", "a", " two"])
771 exe "normal gg\<C-V>2jIx"
772 call assert_equal([" xone", "a", " xtwo"], getline(1, '$'))
773
774 " Test block append at EOL with '$' and without '$'
775 %d _
776 call setline(1, ["one", "a", "two"])
777 exe "normal gg$\<C-V>2jAx"
778 call assert_equal(["onex", "ax", "twox"], getline(1, '$'))
779 %d _
780 call setline(1, ["one", "a", "two"])
781 exe "normal gg3l\<C-V>2jAx"
782 call assert_equal(["onex", "a x", "twox"], getline(1, '$'))
783
784 " Test block replace with an empty line in the middle and use $ to jump to
785 " the end of the line.
786 %d _
787 call setline(1, ['one', '', 'two'])
788 exe "normal gg$\<C-V>2jrx"
789 call assert_equal(["onx", "", "twx"], getline(1, '$'))
790
791 " Test block replace with an empty line in the middle and move cursor to the
792 " end of the line
793 %d _
794 call setline(1, ['one', '', 'two'])
795 exe "normal gg2l\<C-V>2jrx"
796 call assert_equal(["onx", "", "twx"], getline(1, '$'))
797
798 " Replace odd number of characters with a multibyte character
799 %d _
800 call setline(1, ['abcd', 'efgh'])
801 exe "normal ggl\<C-V>2ljr\u1100"
802 call assert_equal(["a\u1100 ", "e\u1100 "], getline(1, '$'))
803
804 " During visual block append, if the cursor moved outside of the selected
805 " range, then the edit should not be applied to the block.
806 %d _
807 call setline(1, ['aaa', 'bbb', 'ccc'])
808 exe "normal 2G\<C-V>jAx\<Up>"
809 call assert_equal(['aaa', 'bxbb', 'ccc'], getline(1, '$'))
810
811 " During visual block append, if the cursor is moved before the start of the
812 " block, then the new text should be appended there.
813 %d _
814 call setline(1, ['aaa', 'bbb', 'ccc'])
815 exe "normal $\<C-V>2jA\<Left>x"
Bram Moolenaar4067bd32021-06-29 18:54:35 +0200816 call assert_equal(['aaxa', 'bbxb', 'ccxc'], getline(1, '$'))
Yegappan Lakshmanan59b26232021-06-05 20:59:22 +0200817 " Repeat the previous test but use 'l' to move the cursor instead of '$'
818 call setline(1, ['aaa', 'bbb', 'ccc'])
819 exe "normal! gg2l\<C-V>2jA\<Left>x"
820 call assert_equal(['aaxa', 'bbxb', 'ccxc'], getline(1, '$'))
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200821
822 " Change a characterwise motion to a blockwise motion using CTRL-V
823 %d _
824 call setline(1, ['123', '456', '789'])
825 exe "normal ld\<C-V>j"
826 call assert_equal(['13', '46', '789'], getline(1, '$'))
827
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +0200828 " Test from ':help v_b_I_example'
829 %d _
830 setlocal tabstop=8 shiftwidth=4
831 let lines =<< trim END
832 abcdefghijklmnopqrstuvwxyz
833 abc defghijklmnopqrstuvwxyz
834 abcdef ghi jklmnopqrstuvwxyz
835 abcdefghijklmnopqrstuvwxyz
836 END
837 call setline(1, lines)
838 exe "normal ggfo\<C-V>3jISTRING"
839 let expected =<< trim END
840 abcdefghijklmnSTRINGopqrstuvwxyz
841 abc STRING defghijklmnopqrstuvwxyz
842 abcdef ghi STRING jklmnopqrstuvwxyz
843 abcdefghijklmnSTRINGopqrstuvwxyz
844 END
845 call assert_equal(expected, getline(1, '$'))
846
847 " Test from ':help v_b_A_example'
848 %d _
849 let lines =<< trim END
850 abcdefghijklmnopqrstuvwxyz
851 abc defghijklmnopqrstuvwxyz
852 abcdef ghi jklmnopqrstuvwxyz
853 abcdefghijklmnopqrstuvwxyz
854 END
855 call setline(1, lines)
856 exe "normal ggfo\<C-V>3j$ASTRING"
857 let expected =<< trim END
858 abcdefghijklmnopqrstuvwxyzSTRING
859 abc defghijklmnopqrstuvwxyzSTRING
860 abcdef ghi jklmnopqrstuvwxyzSTRING
861 abcdefghijklmnopqrstuvwxyzSTRING
862 END
863 call assert_equal(expected, getline(1, '$'))
864
865 " Test from ':help v_b_<_example'
866 %d _
867 let lines =<< trim END
868 abcdefghijklmnopqrstuvwxyz
869 abc defghijklmnopqrstuvwxyz
870 abcdef ghi jklmnopqrstuvwxyz
871 abcdefghijklmnopqrstuvwxyz
872 END
873 call setline(1, lines)
874 exe "normal ggfo\<C-V>3j3l<.."
875 let expected =<< trim END
876 abcdefghijklmnopqrstuvwxyz
877 abc defghijklmnopqrstuvwxyz
878 abcdef ghi jklmnopqrstuvwxyz
879 abcdefghijklmnopqrstuvwxyz
880 END
881 call assert_equal(expected, getline(1, '$'))
882
883 " Test from ':help v_b_>_example'
884 %d _
885 let lines =<< trim END
886 abcdefghijklmnopqrstuvwxyz
887 abc defghijklmnopqrstuvwxyz
888 abcdef ghi jklmnopqrstuvwxyz
889 abcdefghijklmnopqrstuvwxyz
890 END
891 call setline(1, lines)
892 exe "normal ggfo\<C-V>3j>.."
893 let expected =<< trim END
894 abcdefghijklmn opqrstuvwxyz
895 abc defghijklmnopqrstuvwxyz
896 abcdef ghi jklmnopqrstuvwxyz
897 abcdefghijklmn opqrstuvwxyz
898 END
899 call assert_equal(expected, getline(1, '$'))
900
901 " Test from ':help v_b_r_example'
902 %d _
903 let lines =<< trim END
904 abcdefghijklmnopqrstuvwxyz
905 abc defghijklmnopqrstuvwxyz
906 abcdef ghi jklmnopqrstuvwxyz
907 abcdefghijklmnopqrstuvwxyz
908 END
909 call setline(1, lines)
910 exe "normal ggfo\<C-V>5l3jrX"
911 let expected =<< trim END
912 abcdefghijklmnXXXXXXuvwxyz
913 abc XXXXXXhijklmnopqrstuvwxyz
914 abcdef ghi XXXXXX jklmnopqrstuvwxyz
915 abcdefghijklmnXXXXXXuvwxyz
916 END
917 call assert_equal(expected, getline(1, '$'))
918
Bram Moolenaar1f3e7d32019-12-06 20:43:36 +0100919 bwipe!
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +0200920 set tabstop& shiftwidth&
Bram Moolenaar1f3e7d32019-12-06 20:43:36 +0100921endfunc
922
Bram Moolenaar21492742021-06-04 21:57:57 +0200923func Test_visual_force_motion_feedkeys()
924 onoremap <expr> i- execute('let g:mode = mode(1)')->slice(0, 0)
925 call feedkeys('dvi-', 'x')
926 call assert_equal('nov', g:mode)
927 call feedkeys('di-', 'x')
928 call assert_equal('no', g:mode)
929 ounmap i-
930endfunc
931
Bram Moolenaar1f3e7d32019-12-06 20:43:36 +0100932" Test block-insert using cursor keys for movement
933func Test_visual_block_insert_cursor_keys()
934 new
935 call append(0, ['aaaaaa', 'bbbbbb', 'cccccc', 'dddddd'])
936 call cursor(1, 1)
937
938 exe "norm! l\<C-V>jjjlllI\<Right>\<Right> \<Esc>"
939 call assert_equal(['aaa aaa', 'bbb bbb', 'ccc ccc', 'ddd ddd'],
940 \ getline(1, 4))
941
942 call deletebufline('', 1, '$')
943 call setline(1, ['xaaa', 'bbbb', 'cccc', 'dddd'])
944 call cursor(1, 1)
945 exe "norm! \<C-V>jjjI<>\<Left>p\<Esc>"
946 call assert_equal(['<p>xaaa', '<p>bbbb', '<p>cccc', '<p>dddd'],
947 \ getline(1, 4))
948 bwipe!
949endfunc
950
951func Test_visual_block_create()
952 new
953 call append(0, '')
954 " Test for Visual block was created with the last <C-v>$
955 call setline(1, ['A23', '4567'])
956 call cursor(1, 1)
957 exe "norm! l\<C-V>j$Aab\<Esc>"
958 call assert_equal(['A23ab', '4567ab'], getline(1, 2))
959
960 " Test for Visual block was created with the middle <C-v>$ (1)
961 call deletebufline('', 1, '$')
962 call setline(1, ['B23', '4567'])
963 call cursor(1, 1)
964 exe "norm! l\<C-V>j$hAab\<Esc>"
965 call assert_equal(['B23 ab', '4567ab'], getline(1, 2))
966
967 " Test for Visual block was created with the middle <C-v>$ (2)
968 call deletebufline('', 1, '$')
969 call setline(1, ['C23', '4567'])
970 call cursor(1, 1)
971 exe "norm! l\<C-V>j$hhAab\<Esc>"
972 call assert_equal(['C23ab', '456ab7'], getline(1, 2))
973 bwipe!
974endfunc
975
976" Test for Visual block insert when virtualedit=all
977func Test_virtualedit_visual_block()
978 set ve=all
979 new
980 call append(0, ["\t\tline1", "\t\tline2", "\t\tline3"])
981 call cursor(1, 1)
982 exe "norm! 07l\<C-V>jjIx\<Esc>"
983 call assert_equal([" x \tline1",
984 \ " x \tline2",
985 \ " x \tline3"], getline(1, 3))
986
987 " Test for Visual block append when virtualedit=all
988 exe "norm! 012l\<C-v>jjAx\<Esc>"
989 call assert_equal([' x x line1',
990 \ ' x x line2',
991 \ ' x x line3'], getline(1, 3))
992 set ve=
993 bwipe!
994endfunc
995
996" Test for changing case
997func Test_visual_change_case()
998 new
999 " gUe must uppercase a whole word, also when ß changes to SS
1000 exe "normal Gothe youtußeuu end\<Esc>Ypk0wgUe\r"
1001 " gUfx must uppercase until x, inclusive.
1002 exe "normal O- youßtußexu -\<Esc>0fogUfx\r"
1003 " VU must uppercase a whole line
1004 exe "normal YpkVU\r"
1005 " same, when it's the last line in the buffer
1006 exe "normal YPGi111\<Esc>VUddP\r"
1007 " Uppercase two lines
1008 exe "normal Oblah di\rdoh dut\<Esc>VkUj\r"
1009 " Uppercase part of two lines
1010 exe "normal ddppi333\<Esc>k0i222\<Esc>fyllvjfuUk"
1011 call assert_equal(['the YOUTUSSEUU end', '- yOUSSTUSSEXu -',
1012 \ 'THE YOUTUSSEUU END', '111THE YOUTUSSEUU END', 'BLAH DI', 'DOH DUT',
1013 \ '222the yoUTUSSEUU END', '333THE YOUTUßeuu end'], getline(2, '$'))
1014 bwipe!
1015endfunc
1016
1017" Test for Visual replace using Enter or NL
1018func Test_visual_replace_crnl()
1019 new
1020 exe "normal G3o123456789\e2k05l\<C-V>2jr\r"
1021 exe "normal G3o98765\e2k02l\<C-V>2jr\<C-V>\r\n"
1022 exe "normal G3o123456789\e2k05l\<C-V>2jr\n"
1023 exe "normal G3o98765\e2k02l\<C-V>2jr\<C-V>\n"
1024 call assert_equal(['12345', '789', '12345', '789', '12345', '789', "98\r65",
1025 \ "98\r65", "98\r65", '12345', '789', '12345', '789', '12345', '789',
1026 \ "98\n65", "98\n65", "98\n65"], getline(2, '$'))
1027 bwipe!
1028endfunc
1029
1030func Test_ve_block_curpos()
1031 new
1032 " Test cursor position. When ve=block and Visual block mode and $gj
1033 call append(0, ['12345', '789'])
1034 call cursor(1, 3)
1035 set virtualedit=block
1036 exe "norm! \<C-V>$gj\<Esc>"
1037 call assert_equal([0, 2, 4, 0], getpos("'>"))
1038 set virtualedit=
1039 bwipe!
1040endfunc
1041
1042" Test for block_insert when replacing spaces in front of the a with tabs
1043func Test_block_insert_replace_tabs()
1044 new
1045 set ts=8 sts=4 sw=4
1046 call append(0, ["#define BO_ALL\t 0x0001",
1047 \ "#define BO_BS\t 0x0002",
1048 \ "#define BO_CRSR\t 0x0004"])
1049 call cursor(1, 1)
1050 exe "norm! f0\<C-V>2jI\<tab>\<esc>"
1051 call assert_equal([
1052 \ "#define BO_ALL\t\t0x0001",
1053 \ "#define BO_BS\t \t0x0002",
1054 \ "#define BO_CRSR\t \t0x0004", ''], getline(1, '$'))
1055 set ts& sts& sw&
1056 bwipe!
1057endfunc
1058
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01001059" Test for * register in :
1060func Test_star_register()
1061 call assert_fails('*bfirst', 'E16:')
1062 new
1063 call setline(1, ['foo', 'bar', 'baz', 'qux'])
1064 exe "normal jVj\<ESC>"
1065 *yank r
1066 call assert_equal("bar\nbaz\n", @r)
1067
1068 delmarks < >
1069 call assert_fails('*yank', 'E20:')
1070 close!
1071endfunc
1072
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01001073" Test for changing text in visual mode with 'exclusive' selection
1074func Test_exclusive_selection()
1075 new
1076 call setline(1, ['one', 'two'])
1077 set selection=exclusive
1078 call feedkeys("vwcabc", 'xt')
1079 call assert_equal('abctwo', getline(1))
1080 call setline(1, ["\tone"])
1081 set virtualedit=all
1082 call feedkeys('0v2lcl', 'xt')
1083 call assert_equal('l one', getline(1))
1084 set virtualedit&
1085 set selection&
1086 close!
1087endfunc
1088
Bram Moolenaard1ad99b2020-10-04 16:16:54 +02001089" Test for starting linewise visual with a count.
1090" This test needs to be run without any previous visual mode. Otherwise the
1091" count will use the count from the previous visual mode.
1092func Test_linewise_visual_with_count()
1093 let after =<< trim [CODE]
1094 call setline(1, ['one', 'two', 'three', 'four'])
1095 norm! 3Vy
1096 call assert_equal("one\ntwo\nthree\n", @")
1097 call writefile(v:errors, 'Xtestout')
1098 qall!
1099 [CODE]
1100 if RunVim([], after, '')
1101 call assert_equal([], readfile('Xtestout'))
1102 call delete('Xtestout')
1103 endif
1104endfunc
1105
1106" Test for starting characterwise visual with a count.
1107" This test needs to be run without any previous visual mode. Otherwise the
1108" count will use the count from the previous visual mode.
1109func Test_characterwise_visual_with_count()
1110 let after =<< trim [CODE]
1111 call setline(1, ['one two', 'three'])
1112 norm! l5vy
1113 call assert_equal("ne tw", @")
1114 call writefile(v:errors, 'Xtestout')
1115 qall!
1116 [CODE]
1117 if RunVim([], after, '')
1118 call assert_equal([], readfile('Xtestout'))
1119 call delete('Xtestout')
1120 endif
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01001121endfunc
1122
Bram Moolenaar224a5f12020-04-28 20:29:07 +02001123" Test for visually selecting an inner block (iB)
1124func Test_visual_inner_block()
1125 new
1126 call setline(1, ['one', '{', 'two', '{', 'three', '}', 'four', '}', 'five'])
1127 call cursor(5, 1)
1128 " visually select all the lines in the block and then execute iB
1129 call feedkeys("ViB\<C-C>", 'xt')
1130 call assert_equal([0, 5, 1, 0], getpos("'<"))
1131 call assert_equal([0, 5, 6, 0], getpos("'>"))
1132 " visually select two inner blocks
1133 call feedkeys("ViBiB\<C-C>", 'xt')
1134 call assert_equal([0, 3, 1, 0], getpos("'<"))
1135 call assert_equal([0, 7, 5, 0], getpos("'>"))
1136 " try to select non-existing inner block
1137 call cursor(5, 1)
1138 call assert_beeps('normal ViBiBiB')
1139 " try to select a unclosed inner block
1140 8,9d
1141 call cursor(5, 1)
1142 call assert_beeps('normal ViBiB')
1143 close!
1144endfunc
1145
Bram Moolenaarcd942772020-08-22 21:08:44 +02001146func Test_visual_put_in_block()
1147 new
1148 call setline(1, ['xxxx', 'y∞yy', 'zzzz'])
1149 normal 1G2yl
1150 exe "normal 1G2l\<C-V>jjlp"
1151 call assert_equal(['xxxx', 'y∞xx', 'zzxx'], getline(1, 3))
1152 bwipe!
1153endfunc
1154
Christian Brabandt2fa93842021-05-30 22:17:25 +02001155func Test_visual_put_in_block_using_zp()
1156 new
1157 " paste using zP
1158 call setline(1, ['/path;text', '/path;text', '/path;text', '',
1159 \ '/subdir',
1160 \ '/longsubdir',
1161 \ '/longlongsubdir'])
1162 exe "normal! 5G\<c-v>2j$y"
1163 norm! 1Gf;zP
1164 call assert_equal(['/path/subdir;text', '/path/longsubdir;text', '/path/longlongsubdir;text'], getline(1, 3))
1165 %d
1166 " paste using zP
1167 call setline(1, ['/path;text', '/path;text', '/path;text', '',
1168 \ '/subdir',
1169 \ '/longsubdir',
1170 \ '/longlongsubdir'])
1171 exe "normal! 5G\<c-v>2j$y"
1172 norm! 1Gf;hzp
1173 call assert_equal(['/path/subdir;text', '/path/longsubdir;text', '/path/longlongsubdir;text'], getline(1, 3))
1174 bwipe!
1175endfunc
1176
Christian Brabandt544a38e2021-06-10 19:39:11 +02001177func Test_visual_put_in_block_using_zy_and_zp()
1178 new
1179
1180 " Test 1) Paste using zp - after the cursor without trailing spaces
1181 call setline(1, ['/path;text', '/path;text', '/path;text', '',
1182 \ 'texttext /subdir columntext',
1183 \ 'texttext /longsubdir columntext',
1184 \ 'texttext /longlongsubdir columntext'])
1185 exe "normal! 5G0f/\<c-v>2jezy"
1186 norm! 1G0f;hzp
1187 call assert_equal(['/path/subdir;text', '/path/longsubdir;text', '/path/longlongsubdir;text'], getline(1, 3))
1188
1189 " Test 2) Paste using zP - in front of the cursor without trailing spaces
1190 %d
1191 call setline(1, ['/path;text', '/path;text', '/path;text', '',
1192 \ 'texttext /subdir columntext',
1193 \ 'texttext /longsubdir columntext',
1194 \ 'texttext /longlongsubdir columntext'])
1195 exe "normal! 5G0f/\<c-v>2jezy"
1196 norm! 1G0f;zP
1197 call assert_equal(['/path/subdir;text', '/path/longsubdir;text', '/path/longlongsubdir;text'], getline(1, 3))
1198
1199 " Test 3) Paste using p - with trailing spaces
1200 %d
1201 call setline(1, ['/path;text', '/path;text', '/path;text', '',
1202 \ 'texttext /subdir columntext',
1203 \ 'texttext /longsubdir columntext',
1204 \ 'texttext /longlongsubdir columntext'])
1205 exe "normal! 5G0f/\<c-v>2jezy"
1206 norm! 1G0f;hp
1207 call assert_equal(['/path/subdir ;text', '/path/longsubdir ;text', '/path/longlongsubdir;text'], getline(1, 3))
1208
1209 " Test 4) Paste using P - with trailing spaces
1210 %d
1211 call setline(1, ['/path;text', '/path;text', '/path;text', '',
1212 \ 'texttext /subdir columntext',
1213 \ 'texttext /longsubdir columntext',
1214 \ 'texttext /longlongsubdir columntext'])
1215 exe "normal! 5G0f/\<c-v>2jezy"
1216 norm! 1G0f;P
1217 call assert_equal(['/path/subdir ;text', '/path/longsubdir ;text', '/path/longlongsubdir;text'], getline(1, 3))
1218
1219 " Test 5) Yank with spaces inside the block
1220 %d
1221 call setline(1, ['/path;text', '/path;text', '/path;text', '',
1222 \ 'texttext /sub dir/ columntext',
1223 \ 'texttext /lon gsubdir/ columntext',
1224 \ 'texttext /lon glongsubdir/ columntext'])
1225 exe "normal! 5G0f/\<c-v>2jf/zy"
1226 norm! 1G0f;zP
1227 call assert_equal(['/path/sub dir/;text', '/path/lon gsubdir/;text', '/path/lon glongsubdir/;text'], getline(1, 3))
1228 bwipe!
1229endfunc
1230
Bram Moolenaar7d7bcc62021-06-28 21:54:27 +02001231func Test_visual_put_blockedit_zy_and_zp()
1232 new
1233
1234 call setline(1, ['aa', 'bbbbb', 'ccc', '', 'XX', 'GGHHJ', 'RTZU'])
1235 exe "normal! gg0\<c-v>2j$zy"
1236 norm! 5gg0zP
1237 call assert_equal(['aa', 'bbbbb', 'ccc', '', 'aaXX', 'bbbbbGGHHJ', 'cccRTZU'], getline(1, 7))
1238 "
1239 " now with blockmode editing
1240 sil %d
1241 :set ve=block
1242 call setline(1, ['aa', 'bbbbb', 'ccc', '', 'XX', 'GGHHJ', 'RTZU'])
1243 exe "normal! gg0\<c-v>2j$zy"
1244 norm! 5gg0zP
1245 call assert_equal(['aa', 'bbbbb', 'ccc', '', 'aaXX', 'bbbbbGGHHJ', 'cccRTZU'], getline(1, 7))
1246 set ve&vim
1247 bw!
1248endfunc
1249
Bram Moolenaar44db8212022-01-25 21:26:17 +00001250func Test_visual_block_yank_zy()
1251 new
1252 " this was reading before the start of the line
1253 exe "norm o\<C-T>\<Esc>\<C-V>zy"
1254 bwipe!
1255endfunc
1256
Bram Moolenaar9cee4a12021-07-03 15:08:37 +02001257func Test_visual_block_with_virtualedit()
1258 CheckScreendump
1259
1260 let lines =<< trim END
1261 call setline(1, ['aaaaaa', 'bbbb', 'cc'])
1262 set virtualedit=block
1263 normal G
1264 END
1265 call writefile(lines, 'XTest_block')
1266
1267 let buf = RunVimInTerminal('-S XTest_block', {'rows': 8, 'cols': 50})
1268 call term_sendkeys(buf, "\<C-V>gg$")
1269 call VerifyScreenDump(buf, 'Test_visual_block_with_virtualedit', {})
1270
Bram Moolenaarb17ab862021-07-03 22:15:17 +02001271 call term_sendkeys(buf, "\<Esc>gg\<C-V>G$")
1272 call VerifyScreenDump(buf, 'Test_visual_block_with_virtualedit2', {})
1273
Bram Moolenaar9cee4a12021-07-03 15:08:37 +02001274 " clean up
1275 call term_sendkeys(buf, "\<Esc>")
1276 call StopVimInTerminal(buf)
Dominique Pelle6c72fd52021-07-04 12:30:06 +02001277 call delete('XTest_block')
Bram Moolenaar9cee4a12021-07-03 15:08:37 +02001278endfunc
1279
Bram Moolenaar615ddd52021-11-17 18:00:31 +00001280func Test_visual_block_ctrl_w_f()
1281 " Emtpy block selected in new buffer should not result in an error.
1282 au! BufNew foo sil norm f
1283 edit foo
1284
1285 au! BufNew
1286endfunc
1287
Bram Moolenaar9f8c3042022-01-17 17:30:21 +00001288func Test_visual_block_append_invalid_char()
1289 " this was going over the end of the line
Bram Moolenaar262898a2022-01-17 17:52:22 +00001290 set isprint=@,161-255
Bram Moolenaar9f8c3042022-01-17 17:30:21 +00001291 new
1292 call setline(1, [' let xxx', 'xxxxxˆ', 'xxxxxxxxxxx'])
1293 exe "normal 0\<C-V>jjA-\<Esc>"
1294 call assert_equal([' - let xxx', 'xxxxx -ˆ', 'xxxxxxxx-xxx'], getline(1, 3))
1295 bwipe!
Bram Moolenaar262898a2022-01-17 17:52:22 +00001296 set isprint&
Bram Moolenaar9f8c3042022-01-17 17:30:21 +00001297endfunc
1298
Bram Moolenaarb07626d2021-10-11 15:40:43 +01001299func Test_visual_reselect_with_count()
1300 " this was causing an illegal memory access
1301 let lines =<< trim END
1302
1303
1304
1305 :
1306 r<sfile>
1307 exe "%norm e3\<c-v>kr\t"
1308 :
1309
1310 :
1311 END
1312 call writefile(lines, 'XvisualReselect')
1313 source XvisualReselect
1314
1315 bwipe!
1316 call delete('XvisualReselect')
1317endfunc
1318
Bram Moolenaar57df9e82022-01-20 12:10:48 +00001319func Test_visual_block_insert_round_off()
1320 new
1321 " The number of characters are tuned to fill a 4096 byte allocated block,
1322 " so that valgrind reports going over the end.
1323 call setline(1, ['xxxxx', repeat('0', 1350), "\t", repeat('x', 60)])
1324 exe "normal gg0\<C-V>GI" .. repeat('0', 1320) .. "\<Esc>"
1325 bwipe!
1326endfunc
1327
Bram Moolenaar05b27612022-01-20 13:32:50 +00001328" this was causing an ml_get error
1329func Test_visual_exchange_windows()
1330 enew!
1331 new
1332 call setline(1, ['foo', 'bar'])
1333 exe "normal G\<C-V>gg\<C-W>\<C-X>OO\<Esc>"
1334 bwipe!
1335 bwipe!
1336endfunc
1337
Bram Moolenaardc5490e2022-01-25 13:52:53 +00001338" this was leaving the end of the Visual area beyond the end of a line
1339func Test_visual_ex_copy_line()
1340 new
1341 call setline(1, ["aaa", "bbbbbbbbbxbb"])
1342 /x
1343 exe "normal ggvjfxO"
1344 t0
1345 normal gNU
1346 bwipe!
1347endfunc
1348
Bram Moolenaar8d02ce12022-01-25 18:24:00 +00001349" This was leaving the end of the Visual area beyond the end of a line.
1350" Set 'undolevels' to start a new undo block.
1351func Test_visual_undo_deletes_last_line()
1352 new
1353 call setline(1, ["aaa", "ccc", "dyd"])
1354 set undolevels=100
1355 exe "normal obbbbbbbbbxbb\<Esc>"
1356 set undolevels=100
1357 /y
1358 exe "normal ggvjfxO"
1359 undo
1360 normal gNU
1361 bwipe!
1362endfunc
1363
Christian Brabandt544a38e2021-06-10 19:39:11 +02001364
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +01001365" vim: shiftwidth=2 sts=2 expandtab