xref: /vim-8.2.3635/src/testdir/test_visual.vim (revision 80b2ba3e)
1" Tests for various Visual modes.
2
3source shared.vim
4source check.vim
5
6func Test_block_shift_multibyte()
7  " Uses double-wide character.
8  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
15
16func 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
24func 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
31
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
44
45func Test_Visual_vapo()
46  new
47  normal oxx
48  normal vapo
49  bwipe!
50endfunc
51
52func Test_Visual_inner_quote()
53  new
54  normal oxX
55  normal vki'
56  bwipe!
57endfunc
58
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!'
69      let g:msg = 'Snap! E315 error!'
70  endtry
71endfunc
72
73func Test_visual_mode_reset()
74  enew
75  let g:msg = "Everything's fine."
76  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)
85endfunc
86
87" Test for visual block shift and tab characters.
88func Test_block_shift_tab()
89  new
90  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
98  %d _
99  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
123  " 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!
138endfunc
139
140" Tests Blockwise Visual when there are TABs before the text.
141func Test_blockwise_visual()
142  new
143  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
164  bw!
165endfunc
166
167" Test swapping corners in blockwise visual mode with o and O
168func Test_blockwise_visual_o_O()
169  new
170
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"
176  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&
181
182  call assert_equal(['..........',
183        \            '...c   d..',
184        \            '...     ..',
185        \            '...a   bf.',
186        \            '..........'], getline(1, '$'))
187
188  bw!
189endfun
190
191" Test Virtual replace mode.
192func Test_virtual_replace()
193  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
199  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))
227
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
236  enew!
237  set noai bs&vim
238  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
244endfunc
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, '$'))
295
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
304  " clean up
305  %d_
306  set bs&vim
307endfunc
308
309func 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
346func Test_Visual_sentence_textobject()
347  new
348  call setline(1, ['First sentence. Second sentence. Third', 'sentence. Fourth sentence'])
349
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
386
387func Test_Visual_paragraph_textobject()
388  new
389  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)
400
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
438
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
449
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
473
474" Test for 'p'ut in visual block mode
475func Test_visual_block_put()
476  new
477  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, '$'))
482  bw!
483endfunc
484
485" 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
653  " 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
661  " 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
667  bwipe!
668endfunc
669
670func 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
709func Test_gv_with_exclusive_selection()
710  new
711
712  " gv with exclusive selection after an operation
713  call append('$', ['zzz ', 'äà '])
714  set selection=exclusive
715  normal Gkv3lyjv3lpgvcxxx
716  call assert_equal(['', 'zzz ', 'xxx '], getline(1, '$'))
717
718  " gv with exclusive selection without an operation
719  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
729" Tests for the visual block mode commands
730func Test_visual_block_mode()
731  new
732  call append(0, '')
733  call setline(1, repeat(['abcdefghijklm'], 5))
734  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
752  " 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
762  " 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  call assert_equal(['aaxa', 'bbxb', 'ccxc'], getline(1, '$'))
811  " Repeat the previous test but use 'l' to move the cursor instead of '$'
812  call setline(1, ['aaa', 'bbb', 'ccc'])
813  exe "normal! gg2l\<C-V>2jA\<Left>x"
814  call assert_equal(['aaxa', 'bbxb', 'ccxc'], getline(1, '$'))
815
816  " Change a characterwise motion to a blockwise motion using CTRL-V
817  %d _
818  call setline(1, ['123', '456', '789'])
819  exe "normal ld\<C-V>j"
820  call assert_equal(['13', '46', '789'], getline(1, '$'))
821
822  " Test from ':help v_b_I_example'
823  %d _
824  setlocal tabstop=8 shiftwidth=4
825  let lines =<< trim END
826    abcdefghijklmnopqrstuvwxyz
827    abc		defghijklmnopqrstuvwxyz
828    abcdef  ghi		jklmnopqrstuvwxyz
829    abcdefghijklmnopqrstuvwxyz
830  END
831  call setline(1, lines)
832  exe "normal ggfo\<C-V>3jISTRING"
833  let expected =<< trim END
834    abcdefghijklmnSTRINGopqrstuvwxyz
835    abc	      STRING  defghijklmnopqrstuvwxyz
836    abcdef  ghi   STRING  	jklmnopqrstuvwxyz
837    abcdefghijklmnSTRINGopqrstuvwxyz
838  END
839  call assert_equal(expected, getline(1, '$'))
840
841  " Test from ':help v_b_A_example'
842  %d _
843  let lines =<< trim END
844    abcdefghijklmnopqrstuvwxyz
845    abc		defghijklmnopqrstuvwxyz
846    abcdef  ghi		jklmnopqrstuvwxyz
847    abcdefghijklmnopqrstuvwxyz
848  END
849  call setline(1, lines)
850  exe "normal ggfo\<C-V>3j$ASTRING"
851  let expected =<< trim END
852    abcdefghijklmnopqrstuvwxyzSTRING
853    abc		defghijklmnopqrstuvwxyzSTRING
854    abcdef  ghi		jklmnopqrstuvwxyzSTRING
855    abcdefghijklmnopqrstuvwxyzSTRING
856  END
857  call assert_equal(expected, getline(1, '$'))
858
859  " Test from ':help v_b_<_example'
860  %d _
861  let lines =<< trim END
862    abcdefghijklmnopqrstuvwxyz
863    abc		defghijklmnopqrstuvwxyz
864    abcdef  ghi		jklmnopqrstuvwxyz
865    abcdefghijklmnopqrstuvwxyz
866  END
867  call setline(1, lines)
868  exe "normal ggfo\<C-V>3j3l<.."
869  let expected =<< trim END
870    abcdefghijklmnopqrstuvwxyz
871    abc	      defghijklmnopqrstuvwxyz
872    abcdef  ghi   jklmnopqrstuvwxyz
873    abcdefghijklmnopqrstuvwxyz
874  END
875  call assert_equal(expected, getline(1, '$'))
876
877  " Test from ':help v_b_>_example'
878  %d _
879  let lines =<< trim END
880    abcdefghijklmnopqrstuvwxyz
881    abc		defghijklmnopqrstuvwxyz
882    abcdef  ghi		jklmnopqrstuvwxyz
883    abcdefghijklmnopqrstuvwxyz
884  END
885  call setline(1, lines)
886  exe "normal ggfo\<C-V>3j>.."
887  let expected =<< trim END
888    abcdefghijklmn		  opqrstuvwxyz
889    abc			    defghijklmnopqrstuvwxyz
890    abcdef  ghi			    jklmnopqrstuvwxyz
891    abcdefghijklmn		  opqrstuvwxyz
892  END
893  call assert_equal(expected, getline(1, '$'))
894
895  " Test from ':help v_b_r_example'
896  %d _
897  let lines =<< trim END
898    abcdefghijklmnopqrstuvwxyz
899    abc		defghijklmnopqrstuvwxyz
900    abcdef  ghi		jklmnopqrstuvwxyz
901    abcdefghijklmnopqrstuvwxyz
902  END
903  call setline(1, lines)
904  exe "normal ggfo\<C-V>5l3jrX"
905  let expected =<< trim END
906    abcdefghijklmnXXXXXXuvwxyz
907    abc	      XXXXXXhijklmnopqrstuvwxyz
908    abcdef  ghi   XXXXXX    jklmnopqrstuvwxyz
909    abcdefghijklmnXXXXXXuvwxyz
910  END
911  call assert_equal(expected, getline(1, '$'))
912
913  bwipe!
914  set tabstop& shiftwidth&
915endfunc
916
917func Test_visual_force_motion_feedkeys()
918    onoremap <expr> i- execute('let g:mode = mode(1)')->slice(0, 0)
919    call feedkeys('dvi-', 'x')
920    call assert_equal('nov', g:mode)
921    call feedkeys('di-', 'x')
922    call assert_equal('no', g:mode)
923    ounmap i-
924endfunc
925
926" Test block-insert using cursor keys for movement
927func Test_visual_block_insert_cursor_keys()
928  new
929  call append(0, ['aaaaaa', 'bbbbbb', 'cccccc', 'dddddd'])
930  call cursor(1, 1)
931
932  exe "norm! l\<C-V>jjjlllI\<Right>\<Right>  \<Esc>"
933  call assert_equal(['aaa  aaa', 'bbb  bbb', 'ccc  ccc', 'ddd  ddd'],
934        \ getline(1, 4))
935
936  call deletebufline('', 1, '$')
937  call setline(1, ['xaaa', 'bbbb', 'cccc', 'dddd'])
938  call cursor(1, 1)
939  exe "norm! \<C-V>jjjI<>\<Left>p\<Esc>"
940  call assert_equal(['<p>xaaa', '<p>bbbb', '<p>cccc', '<p>dddd'],
941        \ getline(1, 4))
942  bwipe!
943endfunc
944
945func Test_visual_block_create()
946  new
947  call append(0, '')
948  " Test for Visual block was created with the last <C-v>$
949  call setline(1, ['A23', '4567'])
950  call cursor(1, 1)
951  exe "norm! l\<C-V>j$Aab\<Esc>"
952  call assert_equal(['A23ab', '4567ab'], getline(1, 2))
953
954  " Test for Visual block was created with the middle <C-v>$ (1)
955  call deletebufline('', 1, '$')
956  call setline(1, ['B23', '4567'])
957  call cursor(1, 1)
958  exe "norm! l\<C-V>j$hAab\<Esc>"
959  call assert_equal(['B23 ab', '4567ab'], getline(1, 2))
960
961  " Test for Visual block was created with the middle <C-v>$ (2)
962  call deletebufline('', 1, '$')
963  call setline(1, ['C23', '4567'])
964  call cursor(1, 1)
965  exe "norm! l\<C-V>j$hhAab\<Esc>"
966  call assert_equal(['C23ab', '456ab7'], getline(1, 2))
967  bwipe!
968endfunc
969
970" Test for Visual block insert when virtualedit=all
971func Test_virtualedit_visual_block()
972  set ve=all
973  new
974  call append(0, ["\t\tline1", "\t\tline2", "\t\tline3"])
975  call cursor(1, 1)
976  exe "norm! 07l\<C-V>jjIx\<Esc>"
977  call assert_equal(["       x \tline1",
978        \ "       x \tline2",
979        \ "       x \tline3"], getline(1, 3))
980
981  " Test for Visual block append when virtualedit=all
982  exe "norm! 012l\<C-v>jjAx\<Esc>"
983  call assert_equal(['       x     x   line1',
984        \ '       x     x   line2',
985        \ '       x     x   line3'], getline(1, 3))
986  set ve=
987  bwipe!
988endfunc
989
990" Test for changing case
991func Test_visual_change_case()
992  new
993  " gUe must uppercase a whole word, also when ß changes to SS
994  exe "normal Gothe youtußeuu end\<Esc>Ypk0wgUe\r"
995  " gUfx must uppercase until x, inclusive.
996  exe "normal O- youßtußexu -\<Esc>0fogUfx\r"
997  " VU must uppercase a whole line
998  exe "normal YpkVU\r"
999  " same, when it's the last line in the buffer
1000  exe "normal YPGi111\<Esc>VUddP\r"
1001  " Uppercase two lines
1002  exe "normal Oblah di\rdoh dut\<Esc>VkUj\r"
1003  " Uppercase part of two lines
1004  exe "normal ddppi333\<Esc>k0i222\<Esc>fyllvjfuUk"
1005  call assert_equal(['the YOUTUSSEUU end', '- yOUSSTUSSEXu -',
1006        \ 'THE YOUTUSSEUU END', '111THE YOUTUSSEUU END', 'BLAH DI', 'DOH DUT',
1007        \ '222the yoUTUSSEUU END', '333THE YOUTUßeuu end'], getline(2, '$'))
1008  bwipe!
1009endfunc
1010
1011" Test for Visual replace using Enter or NL
1012func Test_visual_replace_crnl()
1013  new
1014  exe "normal G3o123456789\e2k05l\<C-V>2jr\r"
1015  exe "normal G3o98765\e2k02l\<C-V>2jr\<C-V>\r\n"
1016  exe "normal G3o123456789\e2k05l\<C-V>2jr\n"
1017  exe "normal G3o98765\e2k02l\<C-V>2jr\<C-V>\n"
1018  call assert_equal(['12345', '789', '12345', '789', '12345', '789', "98\r65",
1019        \ "98\r65", "98\r65", '12345', '789', '12345', '789', '12345', '789',
1020        \ "98\n65", "98\n65", "98\n65"], getline(2, '$'))
1021  bwipe!
1022endfunc
1023
1024func Test_ve_block_curpos()
1025  new
1026  " Test cursor position. When ve=block and Visual block mode and $gj
1027  call append(0, ['12345', '789'])
1028  call cursor(1, 3)
1029  set virtualedit=block
1030  exe "norm! \<C-V>$gj\<Esc>"
1031  call assert_equal([0, 2, 4, 0], getpos("'>"))
1032  set virtualedit=
1033  bwipe!
1034endfunc
1035
1036" Test for block_insert when replacing spaces in front of the a with tabs
1037func Test_block_insert_replace_tabs()
1038  new
1039  set ts=8 sts=4 sw=4
1040  call append(0, ["#define BO_ALL\t    0x0001",
1041        \ "#define BO_BS\t    0x0002",
1042        \ "#define BO_CRSR\t    0x0004"])
1043  call cursor(1, 1)
1044  exe "norm! f0\<C-V>2jI\<tab>\<esc>"
1045  call assert_equal([
1046        \ "#define BO_ALL\t\t0x0001",
1047        \ "#define BO_BS\t    \t0x0002",
1048        \ "#define BO_CRSR\t    \t0x0004", ''], getline(1, '$'))
1049  set ts& sts& sw&
1050  bwipe!
1051endfunc
1052
1053" Test for * register in :
1054func Test_star_register()
1055  call assert_fails('*bfirst', 'E16:')
1056  new
1057  call setline(1, ['foo', 'bar', 'baz', 'qux'])
1058  exe "normal jVj\<ESC>"
1059  *yank r
1060  call assert_equal("bar\nbaz\n", @r)
1061
1062  delmarks < >
1063  call assert_fails('*yank', 'E20:')
1064  close!
1065endfunc
1066
1067" Test for changing text in visual mode with 'exclusive' selection
1068func Test_exclusive_selection()
1069  new
1070  call setline(1, ['one', 'two'])
1071  set selection=exclusive
1072  call feedkeys("vwcabc", 'xt')
1073  call assert_equal('abctwo', getline(1))
1074  call setline(1, ["\tone"])
1075  set virtualedit=all
1076  call feedkeys('0v2lcl', 'xt')
1077  call assert_equal('l      one', getline(1))
1078  set virtualedit&
1079  set selection&
1080  close!
1081endfunc
1082
1083" Test for starting linewise visual with a count.
1084" This test needs to be run without any previous visual mode. Otherwise the
1085" count will use the count from the previous visual mode.
1086func Test_linewise_visual_with_count()
1087  let after =<< trim [CODE]
1088    call setline(1, ['one', 'two', 'three', 'four'])
1089    norm! 3Vy
1090    call assert_equal("one\ntwo\nthree\n", @")
1091    call writefile(v:errors, 'Xtestout')
1092    qall!
1093  [CODE]
1094  if RunVim([], after, '')
1095    call assert_equal([], readfile('Xtestout'))
1096    call delete('Xtestout')
1097  endif
1098endfunc
1099
1100" Test for starting characterwise visual with a count.
1101" This test needs to be run without any previous visual mode. Otherwise the
1102" count will use the count from the previous visual mode.
1103func Test_characterwise_visual_with_count()
1104  let after =<< trim [CODE]
1105    call setline(1, ['one two', 'three'])
1106    norm! l5vy
1107    call assert_equal("ne tw", @")
1108    call writefile(v:errors, 'Xtestout')
1109    qall!
1110  [CODE]
1111  if RunVim([], after, '')
1112    call assert_equal([], readfile('Xtestout'))
1113    call delete('Xtestout')
1114  endif
1115endfunc
1116
1117" Test for visually selecting an inner block (iB)
1118func Test_visual_inner_block()
1119  new
1120  call setline(1, ['one', '{', 'two', '{', 'three', '}', 'four', '}', 'five'])
1121  call cursor(5, 1)
1122  " visually select all the lines in the block and then execute iB
1123  call feedkeys("ViB\<C-C>", 'xt')
1124  call assert_equal([0, 5, 1, 0], getpos("'<"))
1125  call assert_equal([0, 5, 6, 0], getpos("'>"))
1126  " visually select two inner blocks
1127  call feedkeys("ViBiB\<C-C>", 'xt')
1128  call assert_equal([0, 3, 1, 0], getpos("'<"))
1129  call assert_equal([0, 7, 5, 0], getpos("'>"))
1130  " try to select non-existing inner block
1131  call cursor(5, 1)
1132  call assert_beeps('normal ViBiBiB')
1133  " try to select a unclosed inner block
1134  8,9d
1135  call cursor(5, 1)
1136  call assert_beeps('normal ViBiB')
1137  close!
1138endfunc
1139
1140func Test_visual_put_in_block()
1141  new
1142  call setline(1, ['xxxx', 'y∞yy', 'zzzz'])
1143  normal 1G2yl
1144  exe "normal 1G2l\<C-V>jjlp"
1145  call assert_equal(['xxxx', 'y∞xx', 'zzxx'], getline(1, 3))
1146  bwipe!
1147endfunc
1148
1149func Test_visual_put_in_block_using_zp()
1150  new
1151  " paste using zP
1152  call setline(1, ['/path;text', '/path;text', '/path;text', '',
1153    \ '/subdir',
1154    \ '/longsubdir',
1155    \ '/longlongsubdir'])
1156  exe "normal! 5G\<c-v>2j$y"
1157  norm! 1Gf;zP
1158  call assert_equal(['/path/subdir;text', '/path/longsubdir;text', '/path/longlongsubdir;text'], getline(1, 3))
1159  %d
1160  " paste using zP
1161  call setline(1, ['/path;text', '/path;text', '/path;text', '',
1162    \ '/subdir',
1163    \ '/longsubdir',
1164    \ '/longlongsubdir'])
1165  exe "normal! 5G\<c-v>2j$y"
1166  norm! 1Gf;hzp
1167  call assert_equal(['/path/subdir;text', '/path/longsubdir;text', '/path/longlongsubdir;text'], getline(1, 3))
1168  bwipe!
1169endfunc
1170
1171func Test_visual_put_in_block_using_zy_and_zp()
1172  new
1173
1174  " Test 1) Paste using zp - after the cursor without trailing spaces
1175  call setline(1, ['/path;text', '/path;text', '/path;text', '',
1176    \ 'texttext  /subdir           columntext',
1177		\ 'texttext  /longsubdir       columntext',
1178    \ 'texttext  /longlongsubdir   columntext'])
1179  exe "normal! 5G0f/\<c-v>2jezy"
1180  norm! 1G0f;hzp
1181  call assert_equal(['/path/subdir;text', '/path/longsubdir;text', '/path/longlongsubdir;text'], getline(1, 3))
1182
1183  " Test 2) Paste using zP - in front of the cursor without trailing spaces
1184  %d
1185  call setline(1, ['/path;text', '/path;text', '/path;text', '',
1186    \ 'texttext  /subdir           columntext',
1187		\ 'texttext  /longsubdir       columntext',
1188    \ 'texttext  /longlongsubdir   columntext'])
1189  exe "normal! 5G0f/\<c-v>2jezy"
1190  norm! 1G0f;zP
1191  call assert_equal(['/path/subdir;text', '/path/longsubdir;text', '/path/longlongsubdir;text'], getline(1, 3))
1192
1193  " Test 3) Paste using p - with trailing spaces
1194  %d
1195  call setline(1, ['/path;text', '/path;text', '/path;text', '',
1196    \ 'texttext  /subdir           columntext',
1197		\ 'texttext  /longsubdir       columntext',
1198    \ 'texttext  /longlongsubdir   columntext'])
1199  exe "normal! 5G0f/\<c-v>2jezy"
1200  norm! 1G0f;hp
1201  call assert_equal(['/path/subdir        ;text', '/path/longsubdir    ;text', '/path/longlongsubdir;text'], getline(1, 3))
1202
1203  " Test 4) Paste using P - with trailing spaces
1204  %d
1205  call setline(1, ['/path;text', '/path;text', '/path;text', '',
1206    \ 'texttext  /subdir           columntext',
1207		\ 'texttext  /longsubdir       columntext',
1208    \ 'texttext  /longlongsubdir   columntext'])
1209  exe "normal! 5G0f/\<c-v>2jezy"
1210  norm! 1G0f;P
1211  call assert_equal(['/path/subdir        ;text', '/path/longsubdir    ;text', '/path/longlongsubdir;text'], getline(1, 3))
1212
1213  " Test 5) Yank with spaces inside the block
1214  %d
1215  call setline(1, ['/path;text', '/path;text', '/path;text', '',
1216    \ 'texttext  /sub    dir/           columntext',
1217    \ 'texttext  /lon    gsubdir/       columntext',
1218    \ 'texttext  /lon    glongsubdir/   columntext'])
1219  exe "normal! 5G0f/\<c-v>2jf/zy"
1220  norm! 1G0f;zP
1221  call assert_equal(['/path/sub    dir/;text', '/path/lon    gsubdir/;text', '/path/lon    glongsubdir/;text'], getline(1, 3))
1222  bwipe!
1223endfunc
1224
1225func Test_visual_put_blockedit_zy_and_zp()
1226  new
1227
1228  call setline(1, ['aa', 'bbbbb', 'ccc', '', 'XX', 'GGHHJ', 'RTZU'])
1229  exe "normal! gg0\<c-v>2j$zy"
1230  norm! 5gg0zP
1231  call assert_equal(['aa', 'bbbbb', 'ccc', '', 'aaXX', 'bbbbbGGHHJ', 'cccRTZU'], getline(1, 7))
1232  "
1233  " now with blockmode editing
1234  sil %d
1235  :set ve=block
1236  call setline(1, ['aa', 'bbbbb', 'ccc', '', 'XX', 'GGHHJ', 'RTZU'])
1237  exe "normal! gg0\<c-v>2j$zy"
1238  norm! 5gg0zP
1239  call assert_equal(['aa', 'bbbbb', 'ccc', '', 'aaXX', 'bbbbbGGHHJ', 'cccRTZU'], getline(1, 7))
1240  set ve&vim
1241  bw!
1242endfunc
1243
1244
1245" vim: shiftwidth=2 sts=2 expandtab
1246