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