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