xref: /vim-8.2.3635/src/testdir/test_visual.vim (revision 8ea05de6)
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)
84
85endfunc
86
87" Test for visual block shift and tab characters.
88func Test_block_shift_tab()
89  enew!
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  enew!
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  enew!
124endfunc
125
126" Tests Blockwise Visual when there are TABs before the text.
127func Test_blockwise_visual()
128  enew!
129  call append(0, ['123456',
130	      \ '234567',
131	      \ '345678',
132	      \ '',
133	      \ 'test text test tex start here',
134	      \ "\t\tsome text",
135	      \ "\t\ttest text",
136	      \ 'test text'])
137  call cursor(1,1)
138  exe "normal /start here$\<CR>"
139  exe 'normal "by$' . "\<C-V>jjlld"
140  exe "normal /456$\<CR>"
141  exe "normal \<C-V>jj" . '"bP'
142  call assert_equal(['123start here56',
143	      \ '234start here67',
144	      \ '345start here78',
145	      \ '',
146	      \ 'test text test tex rt here',
147	      \ "\t\tsomext",
148	      \ "\t\ttesext"], getline(1, 7))
149
150  enew!
151endfunc
152
153" Test swapping corners in blockwise visual mode with o and O
154func Test_blockwise_visual_o_O()
155  enew!
156
157  exe "norm! 10i.\<Esc>Y4P3lj\<C-V>4l2jr "
158  exe "norm! gvO\<Esc>ra"
159  exe "norm! gvO\<Esc>rb"
160  exe "norm! gvo\<C-c>rc"
161  exe "norm! gvO\<C-c>rd"
162  set selection=exclusive
163  exe "norm! gvOo\<C-c>re"
164  call assert_equal('...a   be.', getline(4))
165  exe "norm! gvOO\<C-c>rf"
166  set selection&
167
168  call assert_equal(['..........',
169        \            '...c   d..',
170        \            '...     ..',
171        \            '...a   bf.',
172        \            '..........'], getline(1, '$'))
173
174  enew!
175endfun
176
177" Test Virtual replace mode.
178func Test_virtual_replace()
179  if exists('&t_kD')
180    let save_t_kD = &t_kD
181  endif
182  if exists('&t_kb')
183    let save_t_kb = &t_kb
184  endif
185  exe "set t_kD=\<C-V>x7f t_kb=\<C-V>x08"
186  enew!
187  exe "normal a\nabcdefghi\njk\tlmn\n    opq	rst\n\<C-D>uvwxyz"
188  call cursor(1,1)
189  set ai bs=2
190  exe "normal gR0\<C-D> 1\nA\nBCDEFGHIJ\n\tKL\nMNO\nPQR"
191  call assert_equal([' 1',
192	      \ ' A',
193	      \ ' BCDEFGHIJ',
194	      \ ' 	KL',
195	      \ '	MNO',
196	      \ '	PQR',
197	      \ ], getline(1, 6))
198  normal G
199  mark a
200  exe "normal o0\<C-D>\nabcdefghi\njk\tlmn\n    opq\trst\n\<C-D>uvwxyz\n"
201  exe "normal 'ajgR0\<C-D> 1\nA\nBCDEFGHIJ\n\tKL\nMNO\nPQR" . repeat("\<BS>", 29)
202  call assert_equal([' 1',
203	      \ 'abcdefghi',
204	      \ 'jk	lmn',
205	      \ '    opq	rst',
206	      \ 'uvwxyz'], getline(7, 11))
207  normal G
208  exe "normal iab\tcdefghi\tjkl"
209  exe "normal 0gRAB......CDEFGHI.J\<Esc>o"
210  exe "normal iabcdefghijklmnopqrst\<Esc>0gRAB\tIJKLMNO\tQR"
211  call assert_equal(['AB......CDEFGHI.Jkl',
212	      \ 'AB	IJKLMNO	QRst'], getline(12, 13))
213
214  " Test inserting Tab with 'noexpandtab' and 'softabstop' set to 4
215  %d
216  call setline(1, 'aaaaaaaaaaaaa')
217  set softtabstop=4
218  exe "normal gggR\<Tab>\<Tab>x"
219  call assert_equal("\txaaaa", getline(1))
220  set softtabstop&
221
222  enew!
223  set noai bs&vim
224  if exists('save_t_kD')
225    let &t_kD = save_t_kD
226  endif
227  if exists('save_t_kb')
228    let &t_kb = save_t_kb
229  endif
230endfunc
231
232" Test Virtual replace mode.
233func Test_virtual_replace2()
234  enew!
235  set bs=2
236  exe "normal a\nabcdefghi\njk\tlmn\n    opq	rst\n\<C-D>uvwxyz"
237  call cursor(1,1)
238  " Test 1: Test that del deletes the newline
239  exe "normal gR0\<del> 1\nA\nBCDEFGHIJ\n\tKL\nMNO\nPQR"
240  call assert_equal(['0 1',
241	      \ 'A',
242	      \ 'BCDEFGHIJ',
243	      \ '	KL',
244	      \ 'MNO',
245	      \ 'PQR',
246	      \ ], getline(1, 6))
247  " Test 2:
248  " a newline is not deleted, if no newline has been added in virtual replace mode
249  %d_
250  call setline(1, ['abcd', 'efgh', 'ijkl'])
251  call cursor(2,1)
252  exe "norm! gR1234\<cr>5\<bs>\<bs>\<bs>"
253  call assert_equal(['abcd',
254        \ '123h',
255        \ 'ijkl'], getline(1, '$'))
256  " Test 3:
257  " a newline is deleted, if a newline has been inserted before in virtual replace mode
258  %d_
259  call setline(1, ['abcd', 'efgh', 'ijkl'])
260  call cursor(2,1)
261  exe "norm! gR1234\<cr>\<cr>56\<bs>\<bs>\<bs>"
262  call assert_equal(['abcd',
263        \ '1234',
264        \ 'ijkl'], getline(1, '$'))
265  " Test 4:
266  " delete add a newline, delete it, add it again and check undo
267  %d_
268  call setline(1, ['abcd', 'efgh', 'ijkl'])
269  call cursor(2,1)
270  " break undo sequence explicitly
271  let &ul = &ul
272  exe "norm! gR1234\<cr>\<bs>\<del>56\<cr>"
273  let &ul = &ul
274  call assert_equal(['abcd',
275        \ '123456',
276        \ ''], getline(1, '$'))
277  norm! u
278  call assert_equal(['abcd',
279        \ 'efgh',
280        \ 'ijkl'], getline(1, '$'))
281
282  " Test for truncating spaces in a newly added line using 'autoindent' if
283  " characters are not added to that line.
284  %d_
285  call setline(1, ['    app', '    bee', '    cat'])
286  setlocal autoindent
287  exe "normal gg$gRt\n\nr"
288  call assert_equal(['    apt', '', '    rat'], getline(1, '$'))
289
290  " clean up
291  %d_
292  set bs&vim
293endfunc
294
295func Test_Visual_word_textobject()
296  new
297  call setline(1, ['First sentence. Second sentence.'])
298
299  " When start and end of visual area are identical, 'aw' or 'iw' select
300  " the whole word.
301  norm! 1go2fcvawy
302  call assert_equal('Second ', @")
303  norm! 1go2fcviwy
304  call assert_equal('Second', @")
305
306  " When start and end of visual area are not identical, 'aw' or 'iw'
307  " extend the word in direction of the end of the visual area.
308  norm! 1go2fcvlawy
309  call assert_equal('cond ', @")
310  norm! gv2awy
311  call assert_equal('cond sentence.', @")
312
313  norm! 1go2fcvliwy
314  call assert_equal('cond', @")
315  norm! gv2iwy
316  call assert_equal('cond sentence', @")
317
318  " Extend visual area in opposite direction.
319  norm! 1go2fcvhawy
320  call assert_equal(' Sec', @")
321  norm! gv2awy
322  call assert_equal(' sentence. Sec', @")
323
324  norm! 1go2fcvhiwy
325  call assert_equal('Sec', @")
326  norm! gv2iwy
327  call assert_equal('. Sec', @")
328
329  bwipe!
330endfunc
331
332func Test_Visual_sentence_textobject()
333  new
334  call setline(1, ['First sentence. Second sentence. Third', 'sentence. Fourth sentence'])
335
336  " When start and end of visual area are identical, 'as' or 'is' select
337  " the whole sentence.
338  norm! 1gofdvasy
339  call assert_equal('Second sentence. ', @")
340  norm! 1gofdvisy
341  call assert_equal('Second sentence.', @")
342
343  " When start and end of visual area are not identical, 'as' or 'is'
344  " extend the sentence in direction of the end of the visual area.
345  norm! 1gofdvlasy
346  call assert_equal('d sentence. ', @")
347  norm! gvasy
348  call assert_equal("d sentence. Third\nsentence. ", @")
349
350  norm! 1gofdvlisy
351  call assert_equal('d sentence.', @")
352  norm! gvisy
353  call assert_equal('d sentence. ', @")
354  norm! gvisy
355  call assert_equal("d sentence. Third\nsentence.", @")
356
357  " Extend visual area in opposite direction.
358  norm! 1gofdvhasy
359  call assert_equal(' Second', @")
360  norm! gvasy
361  call assert_equal("First sentence. Second", @")
362
363  norm! 1gofdvhisy
364  call assert_equal('Second', @")
365  norm! gvisy
366  call assert_equal(' Second', @")
367  norm! gvisy
368  call assert_equal('First sentence. Second', @")
369
370  bwipe!
371endfunc
372
373func Test_Visual_paragraph_textobject()
374  new
375  let lines =<< trim [END]
376    First line.
377
378    Second line.
379    Third line.
380    Fourth line.
381    Fifth line.
382
383    Sixth line.
384  [END]
385  call setline(1, lines)
386
387  " When start and end of visual area are identical, 'ap' or 'ip' select
388  " the whole paragraph.
389  norm! 4ggvapy
390  call assert_equal("Second line.\nThird line.\nFourth line.\nFifth line.\n\n", @")
391  norm! 4ggvipy
392  call assert_equal("Second line.\nThird line.\nFourth line.\nFifth line.\n", @")
393
394  " When start and end of visual area are not identical, 'ap' or 'ip'
395  " extend the sentence in direction of the end of the visual area.
396  " FIXME: actually, it is not sufficient to have different start and
397  " end of visual selection, the start line and end line have to differ,
398  " which is not consistent with the documentation.
399  norm! 4ggVjapy
400  call assert_equal("Third line.\nFourth line.\nFifth line.\n\n", @")
401  norm! gvapy
402  call assert_equal("Third line.\nFourth line.\nFifth line.\n\nSixth line.\n", @")
403  norm! 4ggVjipy
404  call assert_equal("Third line.\nFourth line.\nFifth line.\n", @")
405  norm! gvipy
406  call assert_equal("Third line.\nFourth line.\nFifth line.\n\n", @")
407  norm! gvipy
408  call assert_equal("Third line.\nFourth line.\nFifth line.\n\nSixth line.\n", @")
409
410  " Extend visual area in opposite direction.
411  norm! 5ggVkapy
412  call assert_equal("\nSecond line.\nThird line.\nFourth line.\n", @")
413  norm! gvapy
414  call assert_equal("First line.\n\nSecond line.\nThird line.\nFourth line.\n", @")
415  norm! 5ggVkipy
416  call assert_equal("Second line.\nThird line.\nFourth line.\n", @")
417  norma gvipy
418  call assert_equal("\nSecond line.\nThird line.\nFourth line.\n", @")
419  norm! gvipy
420  call assert_equal("First line.\n\nSecond line.\nThird line.\nFourth line.\n", @")
421
422  bwipe!
423endfunc
424
425func Test_curswant_not_changed()
426  new
427  call setline(1, ['one', 'two'])
428  au InsertLeave * call getcurpos()
429  call feedkeys("gg0\<C-V>jI123 \<Esc>j", 'xt')
430  call assert_equal([0, 2, 1, 0, 1], getcurpos())
431
432  bwipe!
433  au! InsertLeave
434endfunc
435
436" Tests for "vaBiB", end could be wrong.
437func Test_Visual_Block()
438  new
439  a
440- Bug in "vPPPP" on this text:
441	{
442		cmd;
443		{
444			cmd;\t/* <-- Start cursor here */
445			{
446			}
447		}
448	}
449.
450  normal gg
451  call search('Start cursor here')
452  normal vaBiBD
453  call assert_equal(['- Bug in "vPPPP" on this text:',
454	      \ "\t{",
455	      \ "\t}"], getline(1, '$'))
456
457  close!
458endfunc
459
460" Test for 'p'ut in visual block mode
461func Test_visual_block_put()
462  enew
463
464  call append(0, ['One', 'Two', 'Three'])
465  normal gg
466  yank
467  call feedkeys("jl\<C-V>ljp", 'xt')
468  call assert_equal(['One', 'T', 'Tee', 'One', ''], getline(1, '$'))
469
470  enew!
471endfunc
472
473" Visual modes (v V CTRL-V) followed by an operator; count; repeating
474func Test_visual_mode_op()
475  new
476  call append(0, '')
477
478  call setline(1, 'apple banana cherry')
479  call cursor(1, 1)
480  normal lvld.l3vd.
481  call assert_equal('a y', getline(1))
482
483  call setline(1, ['line 1 line 1', 'line 2 line 2', 'line 3 line 3',
484        \ 'line 4 line 4', 'line 5 line 5', 'line 6 line 6'])
485  call cursor(1, 1)
486  exe "normal Vcnewline\<Esc>j.j2Vd."
487  call assert_equal(['newline', 'newline'], getline(1, '$'))
488
489  call deletebufline('', 1, '$')
490  call setline(1, ['xxxxxxxxxxxxx', 'xxxxxxxxxxxxx', 'xxxxxxxxxxxxx',
491        \ 'xxxxxxxxxxxxx'])
492  exe "normal \<C-V>jlc  \<Esc>l.l2\<C-V>c----\<Esc>l."
493  call assert_equal(['    --------x',
494        \ '    --------x',
495        \ 'xxxx--------x',
496        \ 'xxxx--------x'], getline(1, '$'))
497
498  bwipe!
499endfunc
500
501" Visual mode maps (movement and text object)
502" Visual mode maps; count; repeating
503"   - Simple
504"   - With an Ex command (custom text object)
505func Test_visual_mode_maps()
506  new
507  call append(0, '')
508
509  func SelectInCaps()
510    let [line1, col1] = searchpos('\u', 'bcnW')
511    let [line2, col2] = searchpos('.\u', 'nW')
512    call setpos("'<", [0, line1, col1, 0])
513    call setpos("'>", [0, line2, col2, 0])
514    normal! gv
515  endfunction
516
517  vnoremap W /\u/s-1<CR>
518  vnoremap iW :<C-U>call SelectInCaps()<CR>
519
520  call setline(1, 'KiwiRaspberryDateWatermelonPeach')
521  call cursor(1, 1)
522  exe "normal vWcNo\<Esc>l.fD2vd."
523  call assert_equal('NoNoberryach', getline(1))
524
525  call setline(1, 'JambuRambutanBananaTangerineMango')
526  call cursor(1, 1)
527  exe "normal llviWc-\<Esc>l.l2vdl."
528  call assert_equal('--ago', getline(1))
529
530  vunmap W
531  vunmap iW
532  bwipe!
533  delfunc SelectInCaps
534endfunc
535
536" Operator-pending mode maps (movement and text object)
537"   - Simple
538"   - With Ex command moving the cursor
539"   - With Ex command and Visual selection (custom text object)
540func Test_visual_oper_pending_mode_maps()
541  new
542  call append(0, '')
543
544  func MoveToCap()
545    call search('\u', 'W')
546  endfunction
547
548  func SelectInCaps()
549    let [line1, col1] = searchpos('\u', 'bcnW')
550    let [line2, col2] = searchpos('.\u', 'nW')
551    call setpos("'<", [0, line1, col1, 0])
552    call setpos("'>", [0, line2, col2, 0])
553    normal! gv
554  endfunction
555
556  onoremap W /\u/<CR>
557  onoremap <Leader>W :<C-U>call MoveToCap()<CR>
558  onoremap iW :<C-U>call SelectInCaps()<CR>
559
560  call setline(1, 'PineappleQuinceLoganberryOrangeGrapefruitKiwiZ')
561  call cursor(1, 1)
562  exe "normal cW-\<Esc>l.l2.l."
563  call assert_equal('----Z', getline(1))
564
565  call setline(1, 'JuniperDurianZ')
566  call cursor(1, 1)
567  exe "normal g?\WfD."
568  call assert_equal('WhavcreQhevnaZ', getline(1))
569
570  call setline(1, 'LemonNectarineZ')
571  call cursor(1, 1)
572  exe "normal yiWPlciWNew\<Esc>fr."
573  call assert_equal('LemonNewNewZ', getline(1))
574
575  ounmap W
576  ounmap <Leader>W
577  ounmap iW
578  bwipe!
579  delfunc MoveToCap
580  delfunc SelectInCaps
581endfunc
582
583" Patch 7.3.879: Properly abort Operator-pending mode for "dv:<Esc>" etc.
584func Test_op_pend_mode_abort()
585  new
586  call append(0, '')
587
588  call setline(1, ['zzzz', 'zzzz'])
589  call cursor(1, 1)
590
591  exe "normal dV:\<CR>dv:\<CR>"
592  call assert_equal(['zzz'], getline(1, 2))
593  set nomodifiable
594  call assert_fails('exe "normal d:\<CR>"', 'E21:')
595  set modifiable
596  call feedkeys("dv:\<Esc>dV:\<Esc>", 'xt')
597  call assert_equal(['zzz'], getline(1, 2))
598  set nomodifiable
599  let v:errmsg = ''
600  call feedkeys("d:\<Esc>", 'xt')
601  call assert_true(v:errmsg !~# '^E21:')
602  set modifiable
603
604  bwipe!
605endfunc
606
607func Test_characterwise_visual_mode()
608  new
609
610  " characterwise visual mode: replace last line
611  $put ='a'
612  let @" = 'x'
613  normal v$p
614  call assert_equal('x', getline('$'))
615
616  " characterwise visual mode: delete middle line
617  call deletebufline('', 1, '$')
618  call append('$', ['a', 'b', 'c'])
619  normal G
620  normal kkv$d
621  call assert_equal(['', 'b', 'c'], getline(1, '$'))
622
623  " characterwise visual mode: delete middle two lines
624  call deletebufline('', 1, '$')
625  call append('$', ['a', 'b', 'c'])
626  normal Gkkvj$d
627  call assert_equal(['', 'c'], getline(1, '$'))
628
629  " characterwise visual mode: delete last line
630  call deletebufline('', 1, '$')
631  call append('$', ['a', 'b', 'c'])
632  normal Gv$d
633  call assert_equal(['', 'a', 'b', ''], getline(1, '$'))
634
635  " characterwise visual mode: delete last two lines
636  call deletebufline('', 1, '$')
637  call append('$', ['a', 'b', 'c'])
638  normal Gkvj$d
639  call assert_equal(['', 'a', ''], getline(1, '$'))
640
641  " characterwise visual mode: use a count with the visual mode from the last
642  " line in the buffer
643  %d _
644  call setline(1, ['one', 'two', 'three', 'four'])
645  norm! vj$y
646  norm! G1vy
647  call assert_equal('four', @")
648
649  bwipe!
650endfunc
651
652func Test_visual_mode_put()
653  new
654
655  " v_p: replace last character with line register at middle line
656  call append('$', ['aaa', 'bbb', 'ccc'])
657  normal G
658  -2yank
659  normal k$vp
660  call assert_equal(['', 'aaa', 'bb', 'aaa', '', 'ccc'], getline(1, '$'))
661
662  " v_p: replace last character with line register at middle line selecting
663  " newline
664  call deletebufline('', 1, '$')
665  call append('$', ['aaa', 'bbb', 'ccc'])
666  normal G
667  -2yank
668  normal k$v$p
669  call assert_equal(['', 'aaa', 'bb', 'aaa', 'ccc'], getline(1, '$'))
670
671  " v_p: replace last character with line register at last line
672  call deletebufline('', 1, '$')
673  call append('$', ['aaa', 'bbb', 'ccc'])
674  normal G
675  -2yank
676  normal $vp
677  call assert_equal(['', 'aaa', 'bbb', 'cc', 'aaa', ''], getline(1, '$'))
678
679  " v_p: replace last character with line register at last line selecting
680  " newline
681  call deletebufline('', 1, '$')
682  call append('$', ['aaa', 'bbb', 'ccc'])
683  normal G
684  -2yank
685  normal $v$p
686  call assert_equal(['', 'aaa', 'bbb', 'cc', 'aaa', ''], getline(1, '$'))
687
688  bwipe!
689endfunc
690
691func Test_gv_with_exclusive_selection()
692  new
693
694  " gv with exclusive selection after an operation
695  call append('$', ['zzz ', 'äà '])
696  set selection=exclusive
697  normal Gkv3lyjv3lpgvcxxx
698  call assert_equal(['', 'zzz ', 'xxx '], getline(1, '$'))
699
700  " gv with exclusive selection without an operation
701  call deletebufline('', 1, '$')
702  call append('$', 'zzz ')
703  set selection=exclusive
704  exe "normal G0v3l\<Esc>gvcxxx"
705  call assert_equal(['', 'xxx '], getline(1, '$'))
706
707  set selection&vim
708  bwipe!
709endfunc
710
711" Tests for the visual block mode commands
712func Test_visual_block_mode()
713  new
714  call append(0, '')
715  call setline(1, repeat(['abcdefghijklm'], 5))
716  call cursor(1, 1)
717
718  " Test shift-right of a block
719  exe "normal jllll\<C-V>jj>wll\<C-V>jlll>"
720  " Test shift-left of a block
721  exe "normal G$hhhh\<C-V>kk<"
722  " Test block-insert
723  exe "normal Gkl\<C-V>kkkIxyz"
724  " Test block-replace
725  exe "normal Gllll\<C-V>kkklllrq"
726  " Test block-change
727  exe "normal G$khhh\<C-V>hhkkcmno"
728  call assert_equal(['axyzbcdefghijklm',
729        \ 'axyzqqqq   mno	      ghijklm',
730        \ 'axyzqqqqef mno        ghijklm',
731        \ 'axyzqqqqefgmnoklm',
732        \ 'abcdqqqqijklm'], getline(1, 5))
733
734  " Test 'C' to change till the end of the line
735  call cursor(3, 4)
736  exe "normal! \<C-V>j3lCooo"
737  call assert_equal(['axyooo', 'axyooo'], getline(3, 4))
738
739  " Test 'D' to delete till the end of the line
740  call cursor(3, 3)
741  exe "normal! \<C-V>j2lD"
742  call assert_equal(['ax', 'ax'], getline(3, 4))
743
744  bwipe!
745endfunc
746
747" Test block-insert using cursor keys for movement
748func Test_visual_block_insert_cursor_keys()
749  new
750  call append(0, ['aaaaaa', 'bbbbbb', 'cccccc', 'dddddd'])
751  call cursor(1, 1)
752
753  exe "norm! l\<C-V>jjjlllI\<Right>\<Right>  \<Esc>"
754  call assert_equal(['aaa  aaa', 'bbb  bbb', 'ccc  ccc', 'ddd  ddd'],
755        \ getline(1, 4))
756
757  call deletebufline('', 1, '$')
758  call setline(1, ['xaaa', 'bbbb', 'cccc', 'dddd'])
759  call cursor(1, 1)
760  exe "norm! \<C-V>jjjI<>\<Left>p\<Esc>"
761  call assert_equal(['<p>xaaa', '<p>bbbb', '<p>cccc', '<p>dddd'],
762        \ getline(1, 4))
763  bwipe!
764endfunc
765
766func Test_visual_block_create()
767  new
768  call append(0, '')
769  " Test for Visual block was created with the last <C-v>$
770  call setline(1, ['A23', '4567'])
771  call cursor(1, 1)
772  exe "norm! l\<C-V>j$Aab\<Esc>"
773  call assert_equal(['A23ab', '4567ab'], getline(1, 2))
774
775  " Test for Visual block was created with the middle <C-v>$ (1)
776  call deletebufline('', 1, '$')
777  call setline(1, ['B23', '4567'])
778  call cursor(1, 1)
779  exe "norm! l\<C-V>j$hAab\<Esc>"
780  call assert_equal(['B23 ab', '4567ab'], getline(1, 2))
781
782  " Test for Visual block was created with the middle <C-v>$ (2)
783  call deletebufline('', 1, '$')
784  call setline(1, ['C23', '4567'])
785  call cursor(1, 1)
786  exe "norm! l\<C-V>j$hhAab\<Esc>"
787  call assert_equal(['C23ab', '456ab7'], getline(1, 2))
788  bwipe!
789endfunc
790
791" Test for Visual block insert when virtualedit=all
792func Test_virtualedit_visual_block()
793  set ve=all
794  new
795  call append(0, ["\t\tline1", "\t\tline2", "\t\tline3"])
796  call cursor(1, 1)
797  exe "norm! 07l\<C-V>jjIx\<Esc>"
798  call assert_equal(["       x \tline1",
799        \ "       x \tline2",
800        \ "       x \tline3"], getline(1, 3))
801
802  " Test for Visual block append when virtualedit=all
803  exe "norm! 012l\<C-v>jjAx\<Esc>"
804  call assert_equal(['       x     x   line1',
805        \ '       x     x   line2',
806        \ '       x     x   line3'], getline(1, 3))
807  set ve=
808  bwipe!
809endfunc
810
811" Test for changing case
812func Test_visual_change_case()
813  new
814  " gUe must uppercase a whole word, also when ß changes to SS
815  exe "normal Gothe youtußeuu end\<Esc>Ypk0wgUe\r"
816  " gUfx must uppercase until x, inclusive.
817  exe "normal O- youßtußexu -\<Esc>0fogUfx\r"
818  " VU must uppercase a whole line
819  exe "normal YpkVU\r"
820  " same, when it's the last line in the buffer
821  exe "normal YPGi111\<Esc>VUddP\r"
822  " Uppercase two lines
823  exe "normal Oblah di\rdoh dut\<Esc>VkUj\r"
824  " Uppercase part of two lines
825  exe "normal ddppi333\<Esc>k0i222\<Esc>fyllvjfuUk"
826  call assert_equal(['the YOUTUSSEUU end', '- yOUSSTUSSEXu -',
827        \ 'THE YOUTUSSEUU END', '111THE YOUTUSSEUU END', 'BLAH DI', 'DOH DUT',
828        \ '222the yoUTUSSEUU END', '333THE YOUTUßeuu end'], getline(2, '$'))
829  bwipe!
830endfunc
831
832" Test for Visual replace using Enter or NL
833func Test_visual_replace_crnl()
834  new
835  exe "normal G3o123456789\e2k05l\<C-V>2jr\r"
836  exe "normal G3o98765\e2k02l\<C-V>2jr\<C-V>\r\n"
837  exe "normal G3o123456789\e2k05l\<C-V>2jr\n"
838  exe "normal G3o98765\e2k02l\<C-V>2jr\<C-V>\n"
839  call assert_equal(['12345', '789', '12345', '789', '12345', '789', "98\r65",
840        \ "98\r65", "98\r65", '12345', '789', '12345', '789', '12345', '789',
841        \ "98\n65", "98\n65", "98\n65"], getline(2, '$'))
842  bwipe!
843endfunc
844
845func Test_ve_block_curpos()
846  new
847  " Test cursor position. When ve=block and Visual block mode and $gj
848  call append(0, ['12345', '789'])
849  call cursor(1, 3)
850  set virtualedit=block
851  exe "norm! \<C-V>$gj\<Esc>"
852  call assert_equal([0, 2, 4, 0], getpos("'>"))
853  set virtualedit=
854  bwipe!
855endfunc
856
857" Test for block_insert when replacing spaces in front of the a with tabs
858func Test_block_insert_replace_tabs()
859  new
860  set ts=8 sts=4 sw=4
861  call append(0, ["#define BO_ALL\t    0x0001",
862        \ "#define BO_BS\t    0x0002",
863        \ "#define BO_CRSR\t    0x0004"])
864  call cursor(1, 1)
865  exe "norm! f0\<C-V>2jI\<tab>\<esc>"
866  call assert_equal([
867        \ "#define BO_ALL\t\t0x0001",
868        \ "#define BO_BS\t    \t0x0002",
869        \ "#define BO_CRSR\t    \t0x0004", ''], getline(1, '$'))
870  set ts& sts& sw&
871  bwipe!
872endfunc
873
874" Test for * register in :
875func Test_star_register()
876  call assert_fails('*bfirst', 'E16:')
877  new
878  call setline(1, ['foo', 'bar', 'baz', 'qux'])
879  exe "normal jVj\<ESC>"
880  *yank r
881  call assert_equal("bar\nbaz\n", @r)
882
883  delmarks < >
884  call assert_fails('*yank', 'E20:')
885  close!
886endfunc
887
888" Test for changing text in visual mode with 'exclusive' selection
889func Test_exclusive_selection()
890  new
891  call setline(1, ['one', 'two'])
892  set selection=exclusive
893  call feedkeys("vwcabc", 'xt')
894  call assert_equal('abctwo', getline(1))
895  call setline(1, ["\tone"])
896  set virtualedit=all
897  call feedkeys('0v2lcl', 'xt')
898  call assert_equal('l      one', getline(1))
899  set virtualedit&
900  set selection&
901  close!
902endfunc
903
904" Test for starting linewise visual with a count.
905" This test needs to be run without any previous visual mode. Otherwise the
906" count will use the count from the previous visual mode.
907func Test_linewise_visual_with_count()
908  let after =<< trim [CODE]
909    call setline(1, ['one', 'two', 'three', 'four'])
910    norm! 3Vy
911    call assert_equal("one\ntwo\nthree\n", @")
912    call writefile(v:errors, 'Xtestout')
913    qall!
914  [CODE]
915  if RunVim([], after, '')
916    call assert_equal([], readfile('Xtestout'))
917    call delete('Xtestout')
918  endif
919endfunc
920
921" Test for starting characterwise visual with a count.
922" This test needs to be run without any previous visual mode. Otherwise the
923" count will use the count from the previous visual mode.
924func Test_characterwise_visual_with_count()
925  let after =<< trim [CODE]
926    call setline(1, ['one two', 'three'])
927    norm! l5vy
928    call assert_equal("ne tw", @")
929    call writefile(v:errors, 'Xtestout')
930    qall!
931  [CODE]
932  if RunVim([], after, '')
933    call assert_equal([], readfile('Xtestout'))
934    call delete('Xtestout')
935  endif
936endfunc
937
938" Test for visually selecting an inner block (iB)
939func Test_visual_inner_block()
940  new
941  call setline(1, ['one', '{', 'two', '{', 'three', '}', 'four', '}', 'five'])
942  call cursor(5, 1)
943  " visually select all the lines in the block and then execute iB
944  call feedkeys("ViB\<C-C>", 'xt')
945  call assert_equal([0, 5, 1, 0], getpos("'<"))
946  call assert_equal([0, 5, 6, 0], getpos("'>"))
947  " visually select two inner blocks
948  call feedkeys("ViBiB\<C-C>", 'xt')
949  call assert_equal([0, 3, 1, 0], getpos("'<"))
950  call assert_equal([0, 7, 5, 0], getpos("'>"))
951  " try to select non-existing inner block
952  call cursor(5, 1)
953  call assert_beeps('normal ViBiBiB')
954  " try to select a unclosed inner block
955  8,9d
956  call cursor(5, 1)
957  call assert_beeps('normal ViBiB')
958  close!
959endfunc
960
961func Test_visual_put_in_block()
962  new
963  call setline(1, ['xxxx', 'y∞yy', 'zzzz'])
964  normal 1G2yl
965  exe "normal 1G2l\<C-V>jjlp"
966  call assert_equal(['xxxx', 'y∞xx', 'zzxx'], getline(1, 3))
967  bwipe!
968endfunc
969
970" vim: shiftwidth=2 sts=2 expandtab
971