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