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