xref: /vim-8.2.3635/src/testdir/test_visual.vim (revision 1d59aa1f)
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  call setline(1, ['First line.',
374  \                '',
375  \                'Second line.',
376  \                'Third line.',
377  \                'Fourth line.',
378  \                'Fifth line.',
379  \                '',
380  \                'Sixth line.'])
381
382  " When start and end of visual area are identical, 'ap' or 'ip' select
383  " the whole paragraph.
384  norm! 4ggvapy
385  call assert_equal("Second line.\nThird line.\nFourth line.\nFifth line.\n\n", @")
386  norm! 4ggvipy
387  call assert_equal("Second line.\nThird line.\nFourth line.\nFifth line.\n", @")
388
389  " When start and end of visual area are not identical, 'ap' or 'ip'
390  " extend the sentence in direction of the end of the visual area.
391  " FIXME: actually, it is not sufficient to have different start and
392  " end of visual selection, the start line and end line have to differ,
393  " which is not consistent with the documentation.
394  norm! 4ggVjapy
395  call assert_equal("Third line.\nFourth line.\nFifth line.\n\n", @")
396  norm! gvapy
397  call assert_equal("Third line.\nFourth line.\nFifth line.\n\nSixth line.\n", @")
398  norm! 4ggVjipy
399  call assert_equal("Third line.\nFourth line.\nFifth line.\n", @")
400  norm! gvipy
401  call assert_equal("Third line.\nFourth line.\nFifth line.\n\n", @")
402  norm! gvipy
403  call assert_equal("Third line.\nFourth line.\nFifth line.\n\nSixth line.\n", @")
404
405  " Extend visual area in opposite direction.
406  norm! 5ggVkapy
407  call assert_equal("\nSecond line.\nThird line.\nFourth line.\n", @")
408  norm! gvapy
409  call assert_equal("First line.\n\nSecond line.\nThird line.\nFourth line.\n", @")
410  norm! 5ggVkipy
411  call assert_equal("Second line.\nThird line.\nFourth line.\n", @")
412  norma gvipy
413  call assert_equal("\nSecond line.\nThird line.\nFourth line.\n", @")
414  norm! gvipy
415  call assert_equal("First line.\n\nSecond line.\nThird line.\nFourth line.\n", @")
416
417  bwipe!
418endfunc
419
420func Test_curswant_not_changed()
421  new
422  call setline(1, ['one', 'two'])
423  au InsertLeave * call getcurpos()
424  call feedkeys("gg0\<C-V>jI123 \<Esc>j", 'xt')
425  call assert_equal([0, 2, 1, 0, 1], getcurpos())
426
427  bwipe!
428  au! InsertLeave
429endfunc
430
431" Tests for "vaBiB", end could be wrong.
432func Test_Visual_Block()
433  new
434  a
435- Bug in "vPPPP" on this text:
436	{
437		cmd;
438		{
439			cmd;\t/* <-- Start cursor here */
440			{
441			}
442		}
443	}
444.
445  normal gg
446  call search('Start cursor here')
447  normal vaBiBD
448  call assert_equal(['- Bug in "vPPPP" on this text:',
449	      \ "\t{",
450	      \ "\t}"], getline(1, '$'))
451
452  close!
453endfunc
454
455" Test for 'p'ut in visual block mode
456func Test_visual_block_put()
457  enew
458
459  call append(0, ['One', 'Two', 'Three'])
460  normal gg
461  yank
462  call feedkeys("jl\<C-V>ljp", 'xt')
463  call assert_equal(['One', 'T', 'Tee', 'One', ''], getline(1, '$'))
464
465  enew!
466endfunc
467
468" Visual modes (v V CTRL-V) followed by an operator; count; repeating
469func Test_visual_mode_op()
470  new
471  call append(0, '')
472
473  call setline(1, 'apple banana cherry')
474  call cursor(1, 1)
475  normal lvld.l3vd.
476  call assert_equal('a y', getline(1))
477
478  call setline(1, ['line 1 line 1', 'line 2 line 2', 'line 3 line 3',
479        \ 'line 4 line 4', 'line 5 line 5', 'line 6 line 6'])
480  call cursor(1, 1)
481  exe "normal Vcnewline\<Esc>j.j2Vd."
482  call assert_equal(['newline', 'newline'], getline(1, '$'))
483
484  call deletebufline('', 1, '$')
485  call setline(1, ['xxxxxxxxxxxxx', 'xxxxxxxxxxxxx', 'xxxxxxxxxxxxx',
486        \ 'xxxxxxxxxxxxx'])
487  exe "normal \<C-V>jlc  \<Esc>l.l2\<C-V>c----\<Esc>l."
488  call assert_equal(['    --------x',
489        \ '    --------x',
490        \ 'xxxx--------x',
491        \ 'xxxx--------x'], getline(1, '$'))
492
493  bwipe!
494endfunc
495
496" Visual mode maps (movement and text object)
497" Visual mode maps; count; repeating
498"   - Simple
499"   - With an Ex command (custom text object)
500func Test_visual_mode_maps()
501  new
502  call append(0, '')
503
504  func SelectInCaps()
505    let [line1, col1] = searchpos('\u', 'bcnW')
506    let [line2, col2] = searchpos('.\u', 'nW')
507    call setpos("'<", [0, line1, col1, 0])
508    call setpos("'>", [0, line2, col2, 0])
509    normal! gv
510  endfunction
511
512  vnoremap W /\u/s-1<CR>
513  vnoremap iW :<C-U>call SelectInCaps()<CR>
514
515  call setline(1, 'KiwiRaspberryDateWatermelonPeach')
516  call cursor(1, 1)
517  exe "normal vWcNo\<Esc>l.fD2vd."
518  call assert_equal('NoNoberryach', getline(1))
519
520  call setline(1, 'JambuRambutanBananaTangerineMango')
521  call cursor(1, 1)
522  exe "normal llviWc-\<Esc>l.l2vdl."
523  call assert_equal('--ago', getline(1))
524
525  vunmap W
526  vunmap iW
527  bwipe!
528  delfunc SelectInCaps
529endfunc
530
531" Operator-pending mode maps (movement and text object)
532"   - Simple
533"   - With Ex command moving the cursor
534"   - With Ex command and Visual selection (custom text object)
535func Test_visual_oper_pending_mode_maps()
536  new
537  call append(0, '')
538
539  func MoveToCap()
540    call search('\u', 'W')
541  endfunction
542
543  func SelectInCaps()
544    let [line1, col1] = searchpos('\u', 'bcnW')
545    let [line2, col2] = searchpos('.\u', 'nW')
546    call setpos("'<", [0, line1, col1, 0])
547    call setpos("'>", [0, line2, col2, 0])
548    normal! gv
549  endfunction
550
551  onoremap W /\u/<CR>
552  onoremap <Leader>W :<C-U>call MoveToCap()<CR>
553  onoremap iW :<C-U>call SelectInCaps()<CR>
554
555  call setline(1, 'PineappleQuinceLoganberryOrangeGrapefruitKiwiZ')
556  call cursor(1, 1)
557  exe "normal cW-\<Esc>l.l2.l."
558  call assert_equal('----Z', getline(1))
559
560  call setline(1, 'JuniperDurianZ')
561  call cursor(1, 1)
562  exe "normal g?\WfD."
563  call assert_equal('WhavcreQhevnaZ', getline(1))
564
565  call setline(1, 'LemonNectarineZ')
566  call cursor(1, 1)
567  exe "normal yiWPlciWNew\<Esc>fr."
568  call assert_equal('LemonNewNewZ', getline(1))
569
570  ounmap W
571  ounmap <Leader>W
572  ounmap iW
573  bwipe!
574  delfunc MoveToCap
575  delfunc SelectInCaps
576endfunc
577
578" Patch 7.3.879: Properly abort Operator-pending mode for "dv:<Esc>" etc.
579func Test_op_pend_mode_abort()
580  new
581  call append(0, '')
582
583  call setline(1, ['zzzz', 'zzzz'])
584  call cursor(1, 1)
585
586  exe "normal dV:\<CR>dv:\<CR>"
587  call assert_equal(['zzz'], getline(1, 2))
588  set nomodifiable
589  call assert_fails('exe "normal d:\<CR>"', 'E21:')
590  set modifiable
591  call feedkeys("dv:\<Esc>dV:\<Esc>", 'xt')
592  call assert_equal(['zzz'], getline(1, 2))
593  set nomodifiable
594  let v:errmsg = ''
595  call feedkeys("d:\<Esc>", 'xt')
596  call assert_true(v:errmsg !~# '^E21:')
597  set modifiable
598
599  bwipe!
600endfunc
601
602func Test_characterwise_visual_mode()
603  new
604
605  " characterwise visual mode: replace last line
606  $put ='a'
607  let @" = 'x'
608  normal v$p
609  call assert_equal('x', getline('$'))
610
611  " characterwise visual mode: delete middle line
612  call deletebufline('', 1, '$')
613  call append('$', ['a', 'b', 'c'])
614  normal G
615  normal kkv$d
616  call assert_equal(['', 'b', 'c'], getline(1, '$'))
617
618  " characterwise visual mode: delete middle two lines
619  call deletebufline('', 1, '$')
620  call append('$', ['a', 'b', 'c'])
621  normal Gkkvj$d
622  call assert_equal(['', 'c'], getline(1, '$'))
623
624  " characterwise visual mode: delete last line
625  call deletebufline('', 1, '$')
626  call append('$', ['a', 'b', 'c'])
627  normal Gv$d
628  call assert_equal(['', 'a', 'b', ''], getline(1, '$'))
629
630  " characterwise visual mode: delete last two lines
631  call deletebufline('', 1, '$')
632  call append('$', ['a', 'b', 'c'])
633  normal Gkvj$d
634  call assert_equal(['', 'a', ''], getline(1, '$'))
635
636  bwipe!
637endfunc
638
639func Test_visual_mode_put()
640  new
641
642  " v_p: replace last character with line register at middle line
643  call append('$', ['aaa', 'bbb', 'ccc'])
644  normal G
645  -2yank
646  normal k$vp
647  call assert_equal(['', 'aaa', 'bb', 'aaa', '', 'ccc'], getline(1, '$'))
648
649  " v_p: replace last character with line register at middle line selecting
650  " newline
651  call deletebufline('', 1, '$')
652  call append('$', ['aaa', 'bbb', 'ccc'])
653  normal G
654  -2yank
655  normal k$v$p
656  call assert_equal(['', 'aaa', 'bb', 'aaa', 'ccc'], getline(1, '$'))
657
658  " v_p: replace last character with line register at last line
659  call deletebufline('', 1, '$')
660  call append('$', ['aaa', 'bbb', 'ccc'])
661  normal G
662  -2yank
663  normal $vp
664  call assert_equal(['', 'aaa', 'bbb', 'cc', 'aaa', ''], getline(1, '$'))
665
666  " v_p: replace last character with line register at last line selecting
667  " newline
668  call deletebufline('', 1, '$')
669  call append('$', ['aaa', 'bbb', 'ccc'])
670  normal G
671  -2yank
672  normal $v$p
673  call assert_equal(['', 'aaa', 'bbb', 'cc', 'aaa', ''], getline(1, '$'))
674
675  bwipe!
676endfunc
677
678func Test_gv_with_exclusive_selection()
679  new
680
681  " gv with exclusive selection after an operation
682  call append('$', ['zzz ', 'äà '])
683  set selection=exclusive
684  normal Gkv3lyjv3lpgvcxxx
685  call assert_equal(['', 'zzz ', 'xxx '], getline(1, '$'))
686
687  " gv with exclusive selection without an operation
688  call deletebufline('', 1, '$')
689  call append('$', 'zzz ')
690  set selection=exclusive
691  exe "normal G0v3l\<Esc>gvcxxx"
692  call assert_equal(['', 'xxx '], getline(1, '$'))
693
694  set selection&vim
695  bwipe!
696endfunc
697
698" Tests for the visual block mode commands
699func Test_visual_block_mode()
700  new
701  call append(0, '')
702  call setline(1, repeat(['abcdefghijklm'], 5))
703  call cursor(1, 1)
704
705  " Test shift-right of a block
706  exe "normal jllll\<C-V>jj>wll\<C-V>jlll>"
707  " Test shift-left of a block
708  exe "normal G$hhhh\<C-V>kk<"
709  " Test block-insert
710  exe "normal Gkl\<C-V>kkkIxyz"
711  " Test block-replace
712  exe "normal Gllll\<C-V>kkklllrq"
713  " Test block-change
714  exe "normal G$khhh\<C-V>hhkkcmno"
715  call assert_equal(['axyzbcdefghijklm',
716        \ 'axyzqqqq   mno	      ghijklm',
717        \ 'axyzqqqqef mno        ghijklm',
718        \ 'axyzqqqqefgmnoklm',
719        \ 'abcdqqqqijklm'], getline(1, 5))
720
721  " Test 'C' to change till the end of the line
722  call cursor(3, 4)
723  exe "normal! \<C-V>j3lCooo"
724  call assert_equal(['axyooo', 'axyooo'], getline(3, 4))
725
726  " Test 'D' to delete till the end of the line
727  call cursor(3, 3)
728  exe "normal! \<C-V>j2lD"
729  call assert_equal(['ax', 'ax'], getline(3, 4))
730
731  bwipe!
732endfunc
733
734" Test block-insert using cursor keys for movement
735func Test_visual_block_insert_cursor_keys()
736  new
737  call append(0, ['aaaaaa', 'bbbbbb', 'cccccc', 'dddddd'])
738  call cursor(1, 1)
739
740  exe "norm! l\<C-V>jjjlllI\<Right>\<Right>  \<Esc>"
741  call assert_equal(['aaa  aaa', 'bbb  bbb', 'ccc  ccc', 'ddd  ddd'],
742        \ getline(1, 4))
743
744  call deletebufline('', 1, '$')
745  call setline(1, ['xaaa', 'bbbb', 'cccc', 'dddd'])
746  call cursor(1, 1)
747  exe "norm! \<C-V>jjjI<>\<Left>p\<Esc>"
748  call assert_equal(['<p>xaaa', '<p>bbbb', '<p>cccc', '<p>dddd'],
749        \ getline(1, 4))
750  bwipe!
751endfunc
752
753func Test_visual_block_create()
754  new
755  call append(0, '')
756  " Test for Visual block was created with the last <C-v>$
757  call setline(1, ['A23', '4567'])
758  call cursor(1, 1)
759  exe "norm! l\<C-V>j$Aab\<Esc>"
760  call assert_equal(['A23ab', '4567ab'], getline(1, 2))
761
762  " Test for Visual block was created with the middle <C-v>$ (1)
763  call deletebufline('', 1, '$')
764  call setline(1, ['B23', '4567'])
765  call cursor(1, 1)
766  exe "norm! l\<C-V>j$hAab\<Esc>"
767  call assert_equal(['B23 ab', '4567ab'], getline(1, 2))
768
769  " Test for Visual block was created with the middle <C-v>$ (2)
770  call deletebufline('', 1, '$')
771  call setline(1, ['C23', '4567'])
772  call cursor(1, 1)
773  exe "norm! l\<C-V>j$hhAab\<Esc>"
774  call assert_equal(['C23ab', '456ab7'], getline(1, 2))
775  bwipe!
776endfunc
777
778" Test for Visual block insert when virtualedit=all
779func Test_virtualedit_visual_block()
780  set ve=all
781  new
782  call append(0, ["\t\tline1", "\t\tline2", "\t\tline3"])
783  call cursor(1, 1)
784  exe "norm! 07l\<C-V>jjIx\<Esc>"
785  call assert_equal(["       x \tline1",
786        \ "       x \tline2",
787        \ "       x \tline3"], getline(1, 3))
788
789  " Test for Visual block append when virtualedit=all
790  exe "norm! 012l\<C-v>jjAx\<Esc>"
791  call assert_equal(['       x     x   line1',
792        \ '       x     x   line2',
793        \ '       x     x   line3'], getline(1, 3))
794  set ve=
795  bwipe!
796endfunc
797
798" Test for changing case
799func Test_visual_change_case()
800  new
801  " gUe must uppercase a whole word, also when ß changes to SS
802  exe "normal Gothe youtußeuu end\<Esc>Ypk0wgUe\r"
803  " gUfx must uppercase until x, inclusive.
804  exe "normal O- youßtußexu -\<Esc>0fogUfx\r"
805  " VU must uppercase a whole line
806  exe "normal YpkVU\r"
807  " same, when it's the last line in the buffer
808  exe "normal YPGi111\<Esc>VUddP\r"
809  " Uppercase two lines
810  exe "normal Oblah di\rdoh dut\<Esc>VkUj\r"
811  " Uppercase part of two lines
812  exe "normal ddppi333\<Esc>k0i222\<Esc>fyllvjfuUk"
813  call assert_equal(['the YOUTUSSEUU end', '- yOUSSTUSSEXu -',
814        \ 'THE YOUTUSSEUU END', '111THE YOUTUSSEUU END', 'BLAH DI', 'DOH DUT',
815        \ '222the yoUTUSSEUU END', '333THE YOUTUßeuu end'], getline(2, '$'))
816  bwipe!
817endfunc
818
819" Test for Visual replace using Enter or NL
820func Test_visual_replace_crnl()
821  new
822  exe "normal G3o123456789\e2k05l\<C-V>2jr\r"
823  exe "normal G3o98765\e2k02l\<C-V>2jr\<C-V>\r\n"
824  exe "normal G3o123456789\e2k05l\<C-V>2jr\n"
825  exe "normal G3o98765\e2k02l\<C-V>2jr\<C-V>\n"
826  call assert_equal(['12345', '789', '12345', '789', '12345', '789', "98\r65",
827        \ "98\r65", "98\r65", '12345', '789', '12345', '789', '12345', '789',
828        \ "98\n65", "98\n65", "98\n65"], getline(2, '$'))
829  bwipe!
830endfunc
831
832func Test_ve_block_curpos()
833  new
834  " Test cursor position. When ve=block and Visual block mode and $gj
835  call append(0, ['12345', '789'])
836  call cursor(1, 3)
837  set virtualedit=block
838  exe "norm! \<C-V>$gj\<Esc>"
839  call assert_equal([0, 2, 4, 0], getpos("'>"))
840  set virtualedit=
841  bwipe!
842endfunc
843
844" Test for block_insert when replacing spaces in front of the a with tabs
845func Test_block_insert_replace_tabs()
846  new
847  set ts=8 sts=4 sw=4
848  call append(0, ["#define BO_ALL\t    0x0001",
849        \ "#define BO_BS\t    0x0002",
850        \ "#define BO_CRSR\t    0x0004"])
851  call cursor(1, 1)
852  exe "norm! f0\<C-V>2jI\<tab>\<esc>"
853  call assert_equal([
854        \ "#define BO_ALL\t\t0x0001",
855        \ "#define BO_BS\t    \t0x0002",
856        \ "#define BO_CRSR\t    \t0x0004", ''], getline(1, '$'))
857  set ts& sts& sw&
858  bwipe!
859endfunc
860
861" Test for * register in :
862func Test_star_register()
863  call assert_fails('*bfirst', 'E16:')
864  new
865  call setline(1, ['foo', 'bar', 'baz', 'qux'])
866  exe "normal jVj\<ESC>"
867  *yank r
868  call assert_equal("bar\nbaz\n", @r)
869
870  delmarks < >
871  call assert_fails('*yank', 'E20:')
872  close!
873endfunc
874
875" Test for changing text in visual mode with 'exclusive' selection
876func Test_exclusive_selection()
877  new
878  call setline(1, ['one', 'two'])
879  set selection=exclusive
880  call feedkeys("vwcabc", 'xt')
881  call assert_equal('abctwo', getline(1))
882  call setline(1, ["\tone"])
883  set virtualedit=all
884  call feedkeys('0v2lcl', 'xt')
885  call assert_equal('l      one', getline(1))
886  set virtualedit&
887  set selection&
888  close!
889endfunc
890
891" Test for starting visual mode with a count.
892" This test should be run without any previous visual modes. So this should be
893" run as a first test.
894func Test_AAA_start_visual_mode_with_count()
895  new
896  call setline(1, ['aaaaaaa', 'aaaaaaa', 'aaaaaaa', 'aaaaaaa'])
897  normal! gg2Vy
898  call assert_equal("aaaaaaa\naaaaaaa\n", @")
899  close!
900endfunc
901
902" Test for visually selecting an inner block (iB)
903func Test_visual_inner_block()
904  new
905  call setline(1, ['one', '{', 'two', '{', 'three', '}', 'four', '}', 'five'])
906  call cursor(5, 1)
907  " visually select all the lines in the block and then execute iB
908  call feedkeys("ViB\<C-C>", 'xt')
909  call assert_equal([0, 5, 1, 0], getpos("'<"))
910  call assert_equal([0, 5, 6, 0], getpos("'>"))
911  " visually select two inner blocks
912  call feedkeys("ViBiB\<C-C>", 'xt')
913  call assert_equal([0, 3, 1, 0], getpos("'<"))
914  call assert_equal([0, 7, 5, 0], getpos("'>"))
915  " try to select non-existing inner block
916  call cursor(5, 1)
917  call assert_beeps('normal ViBiBiB')
918  " try to select a unclosed inner block
919  8,9d
920  call cursor(5, 1)
921  call assert_beeps('normal ViBiB')
922  close!
923endfunc
924
925func Test_visual_put_in_block()
926  new
927  call setline(1, ['xxxx', 'y∞yy', 'zzzz'])
928  normal 1G2yl
929  exe "normal 1G2l\<C-V>jjlp"
930  call assert_equal(['xxxx', 'y∞xx', 'zzxx'], getline(1, 3))
931  bwipe!
932endfunc
933
934" vim: shiftwidth=2 sts=2 expandtab
935