robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
100.2 KiB3149 linesraw
1" Tests for various Visual modes.
2
3source shared.vim
4source check.vim
5source screendump.vim
6source vim9.vim
7
8func Test_block_shift_multibyte()
9  " Uses double-wide character.
10  split
11  call setline(1, ['xヹxxx', 'ヹxxx'])
12  exe "normal 1G0l\<C-V>jl>"
13  call assert_equal('x	 ヹxxx', getline(1))
14  call assert_equal('	ヹxxx', getline(2))
15  q!
16endfunc
17
18func Test_block_shift_overflow()
19  " This used to cause a multiplication overflow followed by a crash.
20  new
21  normal ii
22  exe "normal \<C-V>876543210>"
23  q!
24endfunc
25
26func Test_dotregister_paste()
27  new
28  exe "norm! ihello world\<esc>"
29  norm! 0ve".p
30  call assert_equal('hello world world', getline(1))
31  q!
32endfunc
33
34func Test_Visual_ctrl_o()
35  new
36  call setline(1, ['one', 'two', 'three'])
37  call cursor(1,2)
38  set noshowmode
39  set tw=0
40  call feedkeys("\<c-v>jjlIa\<c-\>\<c-o>:set tw=88\<cr>\<esc>", 'tx')
41  call assert_equal(['oane', 'tawo', 'tahree'], getline(1, 3))
42  call assert_equal(88, &tw)
43  set tw&
44  bw!
45endfu
46
47func Test_Visual_vapo()
48  new
49  normal oxx
50  normal vapo
51  bwipe!
52endfunc
53
54func Test_Visual_inner_quote()
55  new
56  normal oxX
57  normal vki'
58  bwipe!
59endfunc
60
61" Test for Visual mode not being reset causing E315 error.
62func TriggerTheProblem()
63  " At this point there is no visual selection because :call reset it.
64  " Let's restore the selection:
65  normal gv
66  '<,'>del _
67  try
68      exe "normal \<Esc>"
69  catch /^Vim\%((\a\+)\)\=:E315/
70      echom 'Snap! E315 error!'
71      let g:msg = 'Snap! E315 error!'
72  endtry
73endfunc
74
75func Test_visual_mode_reset()
76  enew
77  let g:msg = "Everything's fine."
78  enew
79  setl buftype=nofile
80  call append(line('$'), 'Delete this line.')
81
82  " NOTE: this has to be done by a call to a function because executing :del
83  " the ex-way will require the colon operator which resets the visual mode
84  " thus preventing the problem:
85  exe "normal! GV:call TriggerTheProblem()\<CR>"
86  call assert_equal("Everything's fine.", g:msg)
87endfunc
88
89" Test for visual block shift and tab characters.
90func Test_block_shift_tab()
91  new
92  call append(0, repeat(['one two three'], 5))
93  call cursor(1,1)
94  exe "normal i\<C-G>u"
95  exe "normal fe\<C-V>4jR\<Esc>ugvr1"
96  call assert_equal('on1 two three', getline(1))
97  call assert_equal('on1 two three', getline(2))
98  call assert_equal('on1 two three', getline(5))
99
100  %d _
101  call append(0, repeat(['abcdefghijklmnopqrstuvwxyz'], 5))
102  call cursor(1,1)
103  exe "normal \<C-V>4jI    \<Esc>j<<11|D"
104  exe "normal j7|a\<Tab>\<Tab>"
105  exe "normal j7|a\<Tab>\<Tab>   "
106  exe "normal j7|a\<Tab>       \<Tab>\<Esc>4k13|\<C-V>4j<"
107  call assert_equal('    abcdefghijklmnopqrstuvwxyz', getline(1))
108  call assert_equal('abcdefghij', getline(2))
109  call assert_equal("    abc\<Tab>    defghijklmnopqrstuvwxyz", getline(3))
110  call assert_equal("    abc\<Tab>    defghijklmnopqrstuvwxyz", getline(4))
111  call assert_equal("    abc\<Tab>    defghijklmnopqrstuvwxyz", getline(5))
112
113  %s/\s\+//g
114  call cursor(1,1)
115  exe "normal \<C-V>4jI    \<Esc>j<<"
116  exe "normal j7|a\<Tab>\<Tab>"
117  exe "normal j7|a\<Tab>\<Tab>\<Tab>\<Tab>\<Tab>"
118  exe "normal j7|a\<Tab>       \<Tab>\<Tab>\<Esc>4k13|\<C-V>4j3<"
119  call assert_equal('    abcdefghijklmnopqrstuvwxyz', getline(1))
120  call assert_equal('abcdefghij', getline(2))
121  call assert_equal("    abc\<Tab>    defghijklmnopqrstuvwxyz", getline(3))
122  call assert_equal("    abc\<Tab>\<Tab>defghijklmnopqrstuvwxyz", getline(4))
123  call assert_equal("    abc\<Tab>    defghijklmnopqrstuvwxyz", getline(5))
124
125  " Test for block shift with space characters at the beginning and with
126  " 'noexpandtab' and 'expandtab'
127  %d _
128  call setline(1, ["      1", "      2", "      3"])
129  setlocal shiftwidth=2 noexpandtab
130  exe "normal gg\<C-V>3j>"
131  call assert_equal(["\t1", "\t2", "\t3"], getline(1, '$'))
132  %d _
133  call setline(1, ["      1", "      2", "      3"])
134  setlocal shiftwidth=2 expandtab
135  exe "normal gg\<C-V>3j>"
136  call assert_equal(["        1", "        2", "        3"], getline(1, '$'))
137  setlocal shiftwidth&
138
139  bw!
140endfunc
141
142" Tests Blockwise Visual when there are TABs before the text.
143func Test_blockwise_visual()
144  new
145  call append(0, ['123456',
146	      \ '234567',
147	      \ '345678',
148	      \ '',
149	      \ 'test text test tex start here',
150	      \ "\t\tsome text",
151	      \ "\t\ttest text",
152	      \ 'test text'])
153  call cursor(1,1)
154  exe "normal /start here$\<CR>"
155  exe 'normal "by$' . "\<C-V>jjlld"
156  exe "normal /456$\<CR>"
157  exe "normal \<C-V>jj" . '"bP'
158  call assert_equal(['123start here56',
159	      \ '234start here67',
160	      \ '345start here78',
161	      \ '',
162	      \ 'test text test tex rt here',
163	      \ "\t\tsomext",
164	      \ "\t\ttesext"], getline(1, 7))
165
166  bw!
167endfunc
168
169" Test swapping corners in blockwise visual mode with o and O
170func Test_blockwise_visual_o_O()
171  new
172
173  exe "norm! 10i.\<Esc>Y4P3lj\<C-V>4l2jr "
174  exe "norm! gvO\<Esc>ra"
175  exe "norm! gvO\<Esc>rb"
176  exe "norm! gvo\<C-c>rc"
177  exe "norm! gvO\<C-c>rd"
178  set selection=exclusive
179  exe "norm! gvOo\<C-c>re"
180  call assert_equal('...a   be.', getline(4))
181  exe "norm! gvOO\<C-c>rf"
182  set selection&
183
184  call assert_equal(['..........',
185        \            '...c   d..',
186        \            '...     ..',
187        \            '...a   bf.',
188        \            '..........'], getline(1, '$'))
189
190  bw!
191endfun
192
193" Test Virtual replace mode.
194func Test_virtual_replace()
195  if exists('&t_kD')
196    let save_t_kD = &t_kD
197  endif
198  if exists('&t_kb')
199    let save_t_kb = &t_kb
200  endif
201  exe "set t_kD=\<C-V>x7f t_kb=\<C-V>x08"
202  enew!
203  exe "normal a\nabcdefghi\njk\tlmn\n    opq	rst\n\<C-D>uvwxyz"
204  call cursor(1,1)
205  set ai bs=2
206  exe "normal gR0\<C-D> 1\nA\nBCDEFGHIJ\n\tKL\nMNO\nPQR"
207  call assert_equal([' 1',
208	      \ ' A',
209	      \ ' BCDEFGHIJ',
210	      \ ' 	KL',
211	      \ '	MNO',
212	      \ '	PQR',
213	      \ ], getline(1, 6))
214  normal G
215  mark a
216  exe "normal o0\<C-D>\nabcdefghi\njk\tlmn\n    opq\trst\n\<C-D>uvwxyz\n"
217  exe "normal 'ajgR0\<C-D> 1\nA\nBCDEFGHIJ\n\tKL\nMNO\nPQR" . repeat("\<BS>", 29)
218  call assert_equal([' 1',
219	      \ 'abcdefghi',
220	      \ 'jk	lmn',
221	      \ '    opq	rst',
222	      \ 'uvwxyz'], getline(7, 11))
223  normal G
224  exe "normal iab\tcdefghi\tjkl"
225  exe "normal 0gRAB......CDEFGHI.J\<Esc>o"
226  exe "normal iabcdefghijklmnopqrst\<Esc>0gRAB\tIJKLMNO\tQR"
227  call assert_equal(['AB......CDEFGHI.Jkl',
228	      \ 'AB	IJKLMNO	QRst'], getline(12, 13))
229
230  " Test inserting Tab with 'noexpandtab' and 'softabstop' set to 4
231  %d
232  call setline(1, 'aaaaaaaaaaaaa')
233  set softtabstop=4
234  exe "normal gggR\<Tab>\<Tab>x"
235  call assert_equal("\txaaaa", getline(1))
236  set softtabstop&
237
238  enew!
239  set noai bs&vim
240  if exists('save_t_kD')
241    let &t_kD = save_t_kD
242  endif
243  if exists('save_t_kb')
244    let &t_kb = save_t_kb
245  endif
246endfunc
247
248" Test Virtual replace mode.
249func Test_virtual_replace2()
250  enew!
251  set bs=2
252  exe "normal a\nabcdefghi\njk\tlmn\n    opq	rst\n\<C-D>uvwxyz"
253  call cursor(1,1)
254  " Test 1: Test that del deletes the newline
255  exe "normal gR0\<del> 1\nA\nBCDEFGHIJ\n\tKL\nMNO\nPQR"
256  call assert_equal(['0 1',
257	      \ 'A',
258	      \ 'BCDEFGHIJ',
259	      \ '	KL',
260	      \ 'MNO',
261	      \ 'PQR',
262	      \ ], getline(1, 6))
263  " Test 2:
264  " a newline is not deleted, if no newline has been added in virtual replace mode
265  %d_
266  call setline(1, ['abcd', 'efgh', 'ijkl'])
267  call cursor(2,1)
268  exe "norm! gR1234\<cr>5\<bs>\<bs>\<bs>"
269  call assert_equal(['abcd',
270        \ '123h',
271        \ 'ijkl'], getline(1, '$'))
272  " Test 3:
273  " a newline is deleted, if a newline has been inserted before in virtual replace mode
274  %d_
275  call setline(1, ['abcd', 'efgh', 'ijkl'])
276  call cursor(2,1)
277  exe "norm! gR1234\<cr>\<cr>56\<bs>\<bs>\<bs>"
278  call assert_equal(['abcd',
279        \ '1234',
280        \ 'ijkl'], getline(1, '$'))
281  " Test 4:
282  " delete add a newline, delete it, add it again and check undo
283  %d_
284  call setline(1, ['abcd', 'efgh', 'ijkl'])
285  call cursor(2,1)
286  " break undo sequence explicitly
287  let &ul = &ul
288  exe "norm! gR1234\<cr>\<bs>\<del>56\<cr>"
289  let &ul = &ul
290  call assert_equal(['abcd',
291        \ '123456',
292        \ ''], getline(1, '$'))
293  norm! u
294  call assert_equal(['abcd',
295        \ 'efgh',
296        \ 'ijkl'], getline(1, '$'))
297
298  " Test for truncating spaces in a newly added line using 'autoindent' if
299  " characters are not added to that line.
300  %d_
301  call setline(1, ['    app', '    bee', '    cat'])
302  setlocal autoindent
303  exe "normal gg$gRt\n\nr"
304  call assert_equal(['    apt', '', '    rat'], getline(1, '$'))
305
306  " clean up
307  %d_
308  set bs&vim
309endfunc
310
311func Test_Visual_word_textobject()
312  new
313  call setline(1, ['First sentence. Second sentence.'])
314
315  " When start and end of visual area are identical, 'aw' or 'iw' select
316  " the whole word.
317  norm! 1go2fcvawy
318  call assert_equal('Second ', @")
319  norm! 1go2fcviwy
320  call assert_equal('Second', @")
321
322  " When start and end of visual area are not identical, 'aw' or 'iw'
323  " extend the word in direction of the end of the visual area.
324  norm! 1go2fcvlawy
325  call assert_equal('cond ', @")
326  norm! gv2awy
327  call assert_equal('cond sentence.', @")
328
329  norm! 1go2fcvliwy
330  call assert_equal('cond', @")
331  norm! gv2iwy
332  call assert_equal('cond sentence', @")
333
334  " Extend visual area in opposite direction.
335  norm! 1go2fcvhawy
336  call assert_equal(' Sec', @")
337  norm! gv2awy
338  call assert_equal(' sentence. Sec', @")
339
340  norm! 1go2fcvhiwy
341  call assert_equal('Sec', @")
342  norm! gv2iwy
343  call assert_equal('. Sec', @")
344
345  bwipe!
346endfunc
347
348func Test_Visual_sentence_textobject()
349  new
350  call setline(1, ['First sentence. Second sentence. Third', 'sentence. Fourth sentence'])
351
352  " When start and end of visual area are identical, 'as' or 'is' select
353  " the whole sentence.
354  norm! 1gofdvasy
355  call assert_equal('Second sentence. ', @")
356  norm! 1gofdvisy
357  call assert_equal('Second sentence.', @")
358
359  " When start and end of visual area are not identical, 'as' or 'is'
360  " extend the sentence in direction of the end of the visual area.
361  norm! 1gofdvlasy
362  call assert_equal('d sentence. ', @")
363  norm! gvasy
364  call assert_equal("d sentence. Third\nsentence. ", @")
365
366  norm! 1gofdvlisy
367  call assert_equal('d sentence.', @")
368  norm! gvisy
369  call assert_equal('d sentence. ', @")
370  norm! gvisy
371  call assert_equal("d sentence. Third\nsentence.", @")
372
373  " Extend visual area in opposite direction.
374  norm! 1gofdvhasy
375  call assert_equal(' Second', @")
376  norm! gvasy
377  call assert_equal("First sentence. Second", @")
378
379  norm! 1gofdvhisy
380  call assert_equal('Second', @")
381  norm! gvisy
382  call assert_equal(' Second', @")
383  norm! gvisy
384  call assert_equal('First sentence. Second', @")
385
386  bwipe!
387endfunc
388
389func Test_Visual_paragraph_textobject()
390  new
391  let lines =<< trim [END]
392    First line.
393
394    Second line.
395    Third line.
396    Fourth line.
397    Fifth line.
398
399    Sixth line.
400  [END]
401  call setline(1, lines)
402
403  " When start and end of visual area are identical, 'ap' or 'ip' select
404  " the whole paragraph.
405  norm! 4ggvapy
406  call assert_equal("Second line.\nThird line.\nFourth line.\nFifth line.\n\n", @")
407  norm! 4ggvipy
408  call assert_equal("Second line.\nThird line.\nFourth line.\nFifth line.\n", @")
409
410  " When start and end of visual area are not identical, 'ap' or 'ip'
411  " extend the sentence in direction of the end of the visual area.
412  " FIXME: actually, it is not sufficient to have different start and
413  " end of visual selection, the start line and end line have to differ,
414  " which is not consistent with the documentation.
415  norm! 4ggVjapy
416  call assert_equal("Third line.\nFourth line.\nFifth line.\n\n", @")
417  norm! gvapy
418  call assert_equal("Third line.\nFourth line.\nFifth line.\n\nSixth line.\n", @")
419  norm! 4ggVjipy
420  call assert_equal("Third line.\nFourth line.\nFifth line.\n", @")
421  norm! gvipy
422  call assert_equal("Third line.\nFourth line.\nFifth line.\n\n", @")
423  norm! gvipy
424  call assert_equal("Third line.\nFourth line.\nFifth line.\n\nSixth line.\n", @")
425
426  " Extend visual area in opposite direction.
427  norm! 5ggVkapy
428  call assert_equal("\nSecond line.\nThird line.\nFourth line.\n", @")
429  norm! gvapy
430  call assert_equal("First line.\n\nSecond line.\nThird line.\nFourth line.\n", @")
431  norm! 5ggVkipy
432  call assert_equal("Second line.\nThird line.\nFourth line.\n", @")
433  norma gvipy
434  call assert_equal("\nSecond line.\nThird line.\nFourth line.\n", @")
435  norm! gvipy
436  call assert_equal("First line.\n\nSecond line.\nThird line.\nFourth line.\n", @")
437
438  bwipe!
439endfunc
440
441func Test_curswant_not_changed()
442  new
443  call setline(1, ['one', 'two'])
444  au InsertLeave * call getcurpos()
445  call feedkeys("gg0\<C-V>jI123 \<Esc>j", 'xt')
446  call assert_equal([0, 2, 1, 0, 1], getcurpos())
447
448  bwipe!
449  au! InsertLeave
450endfunc
451
452" Tests for "vaBiB", end could be wrong.
453func Test_Visual_Block()
454  new
455  a
456- Bug in "vPPPP" on this text:
457	{
458		cmd;
459		{
460			cmd;\t/* <-- Start cursor here */
461			{
462			}
463		}
464	}
465.
466  normal gg
467  call search('Start cursor here')
468  normal vaBiBD
469  call assert_equal(['- Bug in "vPPPP" on this text:',
470	      \ "\t{",
471	      \ "\t}"], getline(1, '$'))
472
473  bw!
474endfunc
475
476" Test for 'p'ut in visual block mode
477func Test_visual_block_put()
478  new
479  call append(0, ['One', 'Two', 'Three'])
480  normal gg
481  yank
482  call feedkeys("jl\<C-V>ljp", 'xt')
483  call assert_equal(['One', 'T', 'Tee', 'One', ''], getline(1, '$'))
484  bw!
485endfunc
486
487func Test_visual_block_put_invalid()
488  enew!
489  " behave mswin
490  set selection=exclusive
491  norm yy
492  norm v)Ps/^/	
493  " this was causing the column to become negative
494  silent norm ggv)P
495
496  bwipe!
497  " behave xterm
498  set selection&
499endfunc
500
501" Visual modes (v V CTRL-V) followed by an operator; count; repeating
502func Test_visual_mode_op()
503  new
504  call append(0, '')
505
506  call setline(1, 'apple banana cherry')
507  call cursor(1, 1)
508  " Nvim: "." re-executes the captured keysequence (|visual-repeat|): the final
509  " "." replays "3vd", multiplying the previous (grown) selection again.
510  normal lvld.l3vd.
511  call assert_equal('a ', getline(1))
512
513  call setline(1, ['line 1 line 1', 'line 2 line 2', 'line 3 line 3',
514        \ 'line 4 line 4', 'line 5 line 5', 'line 6 line 6'])
515  call cursor(1, 1)
516  exe "normal Vcnewline\<Esc>j.j2Vd."
517  call assert_equal(['newline', 'newline'], getline(1, '$'))
518
519  call deletebufline('', 1, '$')
520  call setline(1, ['xxxxxxxxxxxxx', 'xxxxxxxxxxxxx', 'xxxxxxxxxxxxx',
521        \ 'xxxxxxxxxxxxx'])
522  exe "normal \<C-V>jlc  \<Esc>l.l2\<C-V>c----\<Esc>l."
523  call assert_equal(['    --------',
524        \ '    --------',
525        \ 'xxxx--------',
526        \ 'xxxx--------'], getline(1, '$'))
527
528  bwipe!
529endfunc
530
531" Visual mode maps (movement and text object)
532" Visual mode maps; count; repeating
533"   - Simple
534"   - With an Ex command (custom text object)
535func Test_visual_mode_maps()
536  new
537  call append(0, '')
538
539  func SelectInCaps()
540    let [line1, col1] = searchpos('\u', 'bcnW')
541    let [line2, col2] = searchpos('.\u', 'nW')
542    call setpos("'<", [0, line1, col1, 0])
543    call setpos("'>", [0, line2, col2, 0])
544    normal! gv
545  endfunction
546
547  vnoremap W /\u/s-1<CR>
548  vnoremap iW :<C-U>call SelectInCaps()<CR>
549
550  " Nvim: a selection extended by a search or Ex motion is not replayable:
551  " "." falls back to an equal-size reselect ("1v"), like Vim. But "." after
552  " "2vd" re-executes "2vd", multiplying the previous (grown) area again.
553  call setline(1, 'KiwiRaspberryDateWatermelonPeach')
554  call cursor(1, 1)
555  exe "normal vWcNo\<Esc>l.fD2vd."
556  call assert_equal('NoNo', getline(1))
557
558  call setline(1, 'JambuRambutanBananaTangerineMango')
559  call cursor(1, 1)
560  exe "normal llviWc-\<Esc>l.l2vdl."
561  call assert_equal('--a', getline(1))
562
563  vunmap W
564  vunmap iW
565  bwipe!
566  delfunc SelectInCaps
567endfunc
568
569" Operator-pending mode maps (movement and text object)
570"   - Simple
571"   - With Ex command moving the cursor
572"   - With Ex command and Visual selection (custom text object)
573func Test_visual_oper_pending_mode_maps()
574  new
575  call append(0, '')
576
577  func MoveToCap()
578    call search('\u', 'W')
579  endfunction
580
581  func SelectInCaps()
582    let [line1, col1] = searchpos('\u', 'bcnW')
583    let [line2, col2] = searchpos('.\u', 'nW')
584    call setpos("'<", [0, line1, col1, 0])
585    call setpos("'>", [0, line2, col2, 0])
586    normal! gv
587  endfunction
588
589  onoremap W /\u/<CR>
590  onoremap <Leader>W :<C-U>call MoveToCap()<CR>
591  onoremap iW :<C-U>call SelectInCaps()<CR>
592
593  call setline(1, 'PineappleQuinceLoganberryOrangeGrapefruitKiwiZ')
594  call cursor(1, 1)
595  exe "normal cW-\<Esc>l.l2.l."
596  call assert_equal('----Z', getline(1))
597
598  call setline(1, 'JuniperDurianZ')
599  call cursor(1, 1)
600  exe "normal g?\WfD."
601  call assert_equal('WhavcreQhevnaZ', getline(1))
602
603  call setline(1, 'LemonNectarineZ')
604  call cursor(1, 1)
605  exe "normal yiWPlciWNew\<Esc>fr."
606  call assert_equal('LemonNewNewZ', getline(1))
607
608  ounmap W
609  ounmap <Leader>W
610  ounmap iW
611  bwipe!
612  delfunc MoveToCap
613  delfunc SelectInCaps
614endfunc
615
616" Patch 7.3.879: Properly abort Operator-pending mode for "dv:<Esc>" etc.
617func Test_op_pend_mode_abort()
618  new
619  call append(0, '')
620
621  call setline(1, ['zzzz', 'zzzz'])
622  call cursor(1, 1)
623
624  exe "normal dV:\<CR>dv:\<CR>"
625  call assert_equal(['zzz'], getline(1, 2))
626  set nomodifiable
627  call assert_fails('exe "normal d:\<CR>"', 'E21:')
628  set modifiable
629  call feedkeys("dv:\<Esc>dV:\<Esc>", 'xt')
630  call assert_equal(['zzz'], getline(1, 2))
631  set nomodifiable
632  let v:errmsg = ''
633  call feedkeys("d:\<Esc>", 'xt')
634  call assert_true(v:errmsg !~# '^E21:')
635  set modifiable
636
637  bwipe!
638endfunc
639
640func Test_characterwise_visual_mode()
641  new
642
643  " characterwise visual mode: replace last line
644  $put ='a'
645  let @" = 'x'
646  normal v$p
647  call assert_equal('x', getline('$'))
648
649  " characterwise visual mode: delete middle line
650  call deletebufline('', 1, '$')
651  call append('$', ['a', 'b', 'c'])
652  normal G
653  normal kkv$d
654  call assert_equal(['', 'b', 'c'], getline(1, '$'))
655
656  " characterwise visual mode: delete middle two lines
657  call deletebufline('', 1, '$')
658  call append('$', ['a', 'b', 'c'])
659  normal Gkkvj$d
660  call assert_equal(['', 'c'], getline(1, '$'))
661
662  " characterwise visual mode: delete last line
663  call deletebufline('', 1, '$')
664  call append('$', ['a', 'b', 'c'])
665  normal Gv$d
666  call assert_equal(['', 'a', 'b', ''], getline(1, '$'))
667
668  " characterwise visual mode: delete last two lines
669  call deletebufline('', 1, '$')
670  call append('$', ['a', 'b', 'c'])
671  normal Gkvj$d
672  call assert_equal(['', 'a', ''], getline(1, '$'))
673
674  " characterwise visual mode: use a count with the visual mode from the last
675  " line in the buffer
676  %d _
677  call setline(1, ['one', 'two', 'three', 'four'])
678  norm! vj$y
679  norm! G1vy
680  call assert_equal('four', @")
681
682  " characterwise visual mode: replace a single character line and the eol
683  %d _
684  call setline(1, "a")
685  normal v$rx
686  call assert_equal(['x'], getline(1, '$'))
687
688  " replace a character with composing characters
689  call setline(1, "xã̳x")
690  normal gg0lvrb
691  call assert_equal("xbx", getline(1))
692
693  bwipe!
694endfunc
695
696func Test_visual_mode_put()
697  new
698
699  " v_p: replace last character with line register at middle line
700  call append('$', ['aaa', 'bbb', 'ccc'])
701  normal G
702  -2yank
703  normal k$vp
704  call assert_equal(['', 'aaa', 'bb', 'aaa', '', 'ccc'], getline(1, '$'))
705
706  " v_p: replace last character with line register at middle line selecting
707  " newline
708  call deletebufline('', 1, '$')
709  call append('$', ['aaa', 'bbb', 'ccc'])
710  normal G
711  -2yank
712  normal k$v$p
713  call assert_equal(['', 'aaa', 'bb', 'aaa', 'ccc'], getline(1, '$'))
714
715  " v_p: replace last character with line register at last line
716  call deletebufline('', 1, '$')
717  call append('$', ['aaa', 'bbb', 'ccc'])
718  normal G
719  -2yank
720  normal $vp
721  call assert_equal(['', 'aaa', 'bbb', 'cc', 'aaa', ''], getline(1, '$'))
722
723  " v_p: replace last character with line register at last line selecting
724  " newline
725  call deletebufline('', 1, '$')
726  call append('$', ['aaa', 'bbb', 'ccc'])
727  normal G
728  -2yank
729  normal $v$p
730  call assert_equal(['', 'aaa', 'bbb', 'cc', 'aaa', ''], getline(1, '$'))
731
732  bwipe!
733endfunc
734
735func Test_gv_with_exclusive_selection()
736  new
737
738  " gv with exclusive selection after an operation
739  call append('$', ['zzz ', 'äà '])
740  set selection=exclusive
741  normal Gkv3lyjv3lpgvcxxx
742  call assert_equal(['', 'zzz ', 'xxx '], getline(1, '$'))
743
744  " gv with exclusive selection without an operation
745  call deletebufline('', 1, '$')
746  call append('$', 'zzz ')
747  set selection=exclusive
748  exe "normal G0v3l\<Esc>gvcxxx"
749  call assert_equal(['', 'xxx '], getline(1, '$'))
750
751  set selection&vim
752  bwipe!
753endfunc
754
755" Tests for the visual block mode commands
756func Test_visual_block_mode()
757  new
758  call append(0, '')
759  call setline(1, repeat(['abcdefghijklm'], 5))
760  call cursor(1, 1)
761
762  " Test shift-right of a block
763  exe "normal jllll\<C-V>jj>wll\<C-V>jlll>"
764  " Test shift-left of a block
765  exe "normal G$hhhh\<C-V>kk<"
766  " Test block-insert
767  exe "normal Gkl\<C-V>kkkIxyz"
768  " Test block-replace
769  exe "normal Gllll\<C-V>kkklllrq"
770  " Test block-change
771  exe "normal G$khhh\<C-V>hhkkcmno"
772  call assert_equal(['axyzbcdefghijklm',
773        \ 'axyzqqqq   mno	      ghijklm',
774        \ 'axyzqqqqef mno        ghijklm',
775        \ 'axyzqqqqefgmnoklm',
776        \ 'abcdqqqqijklm'], getline(1, 5))
777
778  " Test 'C' to change till the end of the line
779  call cursor(3, 4)
780  exe "normal! \<C-V>j3lCooo"
781  call assert_equal(['axyooo', 'axyooo'], getline(3, 4))
782
783  " Test 'D' to delete till the end of the line
784  call cursor(3, 3)
785  exe "normal! \<C-V>j2lD"
786  call assert_equal(['ax', 'ax'], getline(3, 4))
787
788  " Test block insert with a short line that ends before the block
789  %d _
790  call setline(1, ["  one", "a", "  two"])
791  exe "normal gg\<C-V>2jIx"
792  call assert_equal(["  xone", "a", "  xtwo"], getline(1, '$'))
793
794  " Test block append at EOL with '$' and without '$'
795  %d _
796  call setline(1, ["one", "a", "two"])
797  exe "normal gg$\<C-V>2jAx"
798  call assert_equal(["onex", "ax", "twox"], getline(1, '$'))
799  %d _
800  call setline(1, ["one", "a", "two"])
801  exe "normal gg3l\<C-V>2jAx"
802  call assert_equal(["onex", "a  x", "twox"], getline(1, '$'))
803
804  " Test block replace with an empty line in the middle and use $ to jump to
805  " the end of the line.
806  %d _
807  call setline(1, ['one', '', 'two'])
808  exe "normal gg$\<C-V>2jrx"
809  call assert_equal(["onx", "", "twx"], getline(1, '$'))
810
811  " Test block replace with an empty line in the middle and move cursor to the
812  " end of the line
813  %d _
814  call setline(1, ['one', '', 'two'])
815  exe "normal gg2l\<C-V>2jrx"
816  call assert_equal(["onx", "", "twx"], getline(1, '$'))
817
818  " Replace odd number of characters with a multibyte character
819  %d _
820  call setline(1, ['abcd', 'efgh'])
821  exe "normal ggl\<C-V>2ljr\u1100"
822  call assert_equal(["a\u1100 ", "e\u1100 "], getline(1, '$'))
823
824  " During visual block append, if the cursor moved outside of the selected
825  " range, then the edit should not be applied to the block.
826  %d _
827  call setline(1, ['aaa', 'bbb', 'ccc'])
828  exe "normal 2G\<C-V>jAx\<Up>"
829  call assert_equal(['aaa', 'bxbb', 'ccc'], getline(1, '$'))
830
831  " During visual block append, if the cursor is moved before the start of the
832  " block, then the new text should be appended there.
833  %d _
834  call setline(1, ['aaa', 'bbb', 'ccc'])
835  exe "normal $\<C-V>2jA\<Left>x"
836  call assert_equal(['aaxa', 'bbxb', 'ccxc'], getline(1, '$'))
837  " Repeat the previous test but use 'l' to move the cursor instead of '$'
838  call setline(1, ['aaa', 'bbb', 'ccc'])
839  exe "normal! gg2l\<C-V>2jA\<Left>x"
840  call assert_equal(['aaxa', 'bbxb', 'ccxc'], getline(1, '$'))
841
842  " Change a characterwise motion to a blockwise motion using CTRL-V
843  %d _
844  call setline(1, ['123', '456', '789'])
845  exe "normal ld\<C-V>j"
846  call assert_equal(['13', '46', '789'], getline(1, '$'))
847
848  " Test from ':help v_b_I_example'
849  %d _
850  setlocal tabstop=8 shiftwidth=4
851  let lines =<< trim END
852    abcdefghijklmnopqrstuvwxyz
853    abc		defghijklmnopqrstuvwxyz
854    abcdef  ghi		jklmnopqrstuvwxyz
855    abcdefghijklmnopqrstuvwxyz
856  END
857  call setline(1, lines)
858  exe "normal ggfo\<C-V>3jISTRING"
859  let expected =<< trim END
860    abcdefghijklmnSTRINGopqrstuvwxyz
861    abc	      STRING  defghijklmnopqrstuvwxyz
862    abcdef  ghi   STRING  	jklmnopqrstuvwxyz
863    abcdefghijklmnSTRINGopqrstuvwxyz
864  END
865  call assert_equal(expected, getline(1, '$'))
866
867  " Test from ':help v_b_A_example'
868  %d _
869  let lines =<< trim END
870    abcdefghijklmnopqrstuvwxyz
871    abc		defghijklmnopqrstuvwxyz
872    abcdef  ghi		jklmnopqrstuvwxyz
873    abcdefghijklmnopqrstuvwxyz
874  END
875  call setline(1, lines)
876  exe "normal ggfo\<C-V>3j$ASTRING"
877  let expected =<< trim END
878    abcdefghijklmnopqrstuvwxyzSTRING
879    abc		defghijklmnopqrstuvwxyzSTRING
880    abcdef  ghi		jklmnopqrstuvwxyzSTRING
881    abcdefghijklmnopqrstuvwxyzSTRING
882  END
883  call assert_equal(expected, getline(1, '$'))
884
885  " Test from ':help v_b_<_example'
886  %d _
887  let lines =<< trim END
888    abcdefghijklmnopqrstuvwxyz
889    abc		defghijklmnopqrstuvwxyz
890    abcdef  ghi		jklmnopqrstuvwxyz
891    abcdefghijklmnopqrstuvwxyz
892  END
893  call setline(1, lines)
894  exe "normal ggfo\<C-V>3j3l<.."
895  let expected =<< trim END
896    abcdefghijklmnopqrstuvwxyz
897    abc	      defghijklmnopqrstuvwxyz
898    abcdef  ghi   jklmnopqrstuvwxyz
899    abcdefghijklmnopqrstuvwxyz
900  END
901  call assert_equal(expected, getline(1, '$'))
902
903  " Test from ':help v_b_>_example'
904  %d _
905  let lines =<< trim END
906    abcdefghijklmnopqrstuvwxyz
907    abc		defghijklmnopqrstuvwxyz
908    abcdef  ghi		jklmnopqrstuvwxyz
909    abcdefghijklmnopqrstuvwxyz
910  END
911  call setline(1, lines)
912  exe "normal ggfo\<C-V>3j>.."
913  let expected =<< trim END
914    abcdefghijklmn		  opqrstuvwxyz
915    abc			    defghijklmnopqrstuvwxyz
916    abcdef  ghi			    jklmnopqrstuvwxyz
917    abcdefghijklmn		  opqrstuvwxyz
918  END
919  call assert_equal(expected, getline(1, '$'))
920
921  " Test from ':help v_b_r_example'
922  %d _
923  let lines =<< trim END
924    abcdefghijklmnopqrstuvwxyz
925    abc		defghijklmnopqrstuvwxyz
926    abcdef  ghi		jklmnopqrstuvwxyz
927    abcdefghijklmnopqrstuvwxyz
928  END
929  call setline(1, lines)
930  exe "normal ggfo\<C-V>5l3jrX"
931  let expected =<< trim END
932    abcdefghijklmnXXXXXXuvwxyz
933    abc	      XXXXXXhijklmnopqrstuvwxyz
934    abcdef  ghi   XXXXXX    jklmnopqrstuvwxyz
935    abcdefghijklmnXXXXXXuvwxyz
936  END
937  call assert_equal(expected, getline(1, '$'))
938
939  bwipe!
940  set tabstop& shiftwidth&
941endfunc
942
943func Test_visual_force_motion_feedkeys()
944    onoremap <expr> i- execute('let g:mode = mode(1)')->slice(0, 0)
945    call feedkeys('dvi-', 'x')
946    call assert_equal('nov', g:mode)
947    call feedkeys('di-', 'x')
948    call assert_equal('no', g:mode)
949    ounmap i-
950endfunc
951
952" Test block-insert using cursor keys for movement
953func Test_visual_block_insert_cursor_keys()
954  new
955  call append(0, ['aaaaaa', 'bbbbbb', 'cccccc', 'dddddd'])
956  call cursor(1, 1)
957
958  exe "norm! l\<C-V>jjjlllI\<Right>\<Right>  \<Esc>"
959  call assert_equal(['aaa  aaa', 'bbb  bbb', 'ccc  ccc', 'ddd  ddd'],
960        \ getline(1, 4))
961
962  call deletebufline('', 1, '$')
963  call setline(1, ['xaaa', 'bbbb', 'cccc', 'dddd'])
964  call cursor(1, 1)
965  exe "norm! \<C-V>jjjI<>\<Left>p\<Esc>"
966  call assert_equal(['<p>xaaa', '<p>bbbb', '<p>cccc', '<p>dddd'],
967        \ getline(1, 4))
968  bwipe!
969endfunc
970
971func Test_visual_block_create()
972  new
973  call append(0, '')
974  " Test for Visual block was created with the last <C-v>$
975  call setline(1, ['A23', '4567'])
976  call cursor(1, 1)
977  exe "norm! l\<C-V>j$Aab\<Esc>"
978  call assert_equal(['A23ab', '4567ab'], getline(1, 2))
979
980  " Test for Visual block was created with the middle <C-v>$ (1)
981  call deletebufline('', 1, '$')
982  call setline(1, ['B23', '4567'])
983  call cursor(1, 1)
984  exe "norm! l\<C-V>j$hAab\<Esc>"
985  call assert_equal(['B23 ab', '4567ab'], getline(1, 2))
986
987  " Test for Visual block was created with the middle <C-v>$ (2)
988  call deletebufline('', 1, '$')
989  call setline(1, ['C23', '4567'])
990  call cursor(1, 1)
991  exe "norm! l\<C-V>j$hhAab\<Esc>"
992  call assert_equal(['C23ab', '456ab7'], getline(1, 2))
993  bwipe!
994endfunc
995
996" Test for Visual block insert when virtualedit=all
997func Test_virtualedit_visual_block()
998  set ve=all
999  new
1000  call append(0, ["\t\tline1", "\t\tline2", "\t\tline3"])
1001  call cursor(1, 1)
1002  exe "norm! 07l\<C-V>jjIx\<Esc>"
1003  call assert_equal(["       x \tline1",
1004        \ "       x \tline2",
1005        \ "       x \tline3"], getline(1, 3))
1006
1007  " Test for Visual block append when virtualedit=all
1008  exe "norm! 012l\<C-v>jjAx\<Esc>"
1009  call assert_equal(['       x     x   line1',
1010        \ '       x     x   line2',
1011        \ '       x     x   line3'], getline(1, 3))
1012  set ve=
1013  bwipe!
1014endfunc
1015
1016func Test_virtualedit_visual_block_reselect()
1017  set ve=all
1018  new
1019  call append(0, ['###', '###', '###', '#####'])
1020  call cursor(1, 1)
1021  exe "norm! \<C-V>lljj"
1022  call assert_equal([0, 3, 3, 0], getpos('.'))
1023  call assert_equal([0, 1, 1, 0], getpos('v'))
1024  norm! y
1025  call assert_equal(['###', '###', '###'], getreg('"', v:true, v:true))
1026  call cursor(2, 6)
1027  call assert_equal([0, 2, 4, 2], getpos('.'))
1028  norm! 1v
1029  call assert_equal([0, 4, 6, 2], getpos('.'))
1030  call assert_equal([0, 2, 4, 2], getpos('v'))
1031  norm! r!
1032  call assert_equal(['###  !!!', '###  !!!', '#####!!!'], getline(2, 4))
1033
1034  bwipe!
1035  set ve&
1036endfunc
1037
1038" Test for changing case
1039func Test_visual_change_case()
1040  new
1041  " gUe must uppercase a whole word, also when ß changes to ẞ
1042  exe "normal Gothe youtußeuu end\<Esc>Ypk0wgUe\r"
1043  " gUfx must uppercase until x, inclusive.
1044  exe "normal O- youßtußexu -\<Esc>0fogUfx\r"
1045  " VU must uppercase a whole line
1046  exe "normal YpkVU\r"
1047  " same, when it's the last line in the buffer
1048  exe "normal YPGi111\<Esc>VUddP\r"
1049  " Uppercase two lines
1050  exe "normal Oblah di\rdoh dut\<Esc>VkUj\r"
1051  " Uppercase part of two lines
1052  exe "normal ddppi333\<Esc>k0i222\<Esc>fyllvjfuUk"
1053  call assert_equal(['the YOUTUẞEUU end', '- yOUẞTUẞEXu -',
1054        \ 'THE YOUTUẞEUU END', '111THE YOUTUẞEUU END', 'BLAH DI', 'DOH DUT',
1055        \ '222the yoUTUẞEUU END', '333THE YOUTUßeuu end'], getline(2, '$'))
1056  bwipe!
1057endfunc
1058
1059" Test for Visual replace using Enter or NL
1060func Test_visual_replace_crnl()
1061  new
1062  exe "normal G3o123456789\e2k05l\<C-V>2jr\r"
1063  exe "normal G3o98765\e2k02l\<C-V>2jr\<C-V>\r\n"
1064  exe "normal G3o123456789\e2k05l\<C-V>2jr\n"
1065  exe "normal G3o98765\e2k02l\<C-V>2jr\<C-V>\n"
1066  call assert_equal(['12345', '789', '12345', '789', '12345', '789', "98\r65",
1067        \ "98\r65", "98\r65", '12345', '789', '12345', '789', '12345', '789',
1068        \ "98\n65", "98\n65", "98\n65"], getline(2, '$'))
1069  bwipe!
1070endfunc
1071
1072func Test_ve_block_curpos()
1073  new
1074  " Test cursor position. When ve=block and Visual block mode and $gj
1075  call append(0, ['12345', '789'])
1076  call cursor(1, 3)
1077  set virtualedit=block
1078  exe "norm! \<C-V>$gj\<Esc>"
1079  call assert_equal([0, 2, 4, 0], getpos("'>"))
1080  set virtualedit=
1081  bwipe!
1082endfunc
1083
1084" Test for block_insert when replacing spaces in front of the a with tabs
1085func Test_block_insert_replace_tabs()
1086  new
1087  set ts=8 sts=4 sw=4
1088  call append(0, ["#define BO_ALL\t    0x0001",
1089        \ "#define BO_BS\t    0x0002",
1090        \ "#define BO_CRSR\t    0x0004"])
1091  call cursor(1, 1)
1092  exe "norm! f0\<C-V>2jI\<tab>\<esc>"
1093  call assert_equal([
1094        \ "#define BO_ALL\t\t0x0001",
1095        \ "#define BO_BS\t    \t0x0002",
1096        \ "#define BO_CRSR\t    \t0x0004", ''], getline(1, '$'))
1097  set ts& sts& sw&
1098  bwipe!
1099endfunc
1100
1101" Test for * register in :
1102func Test_star_register()
1103  call assert_fails('*bfirst', 'E16:')
1104  new
1105  call setline(1, ['foo', 'bar', 'baz', 'qux'])
1106  exe "normal jVj\<ESC>"
1107  *yank r
1108  call assert_equal("bar\nbaz\n", @r)
1109
1110  delmarks < >
1111  call assert_fails('*yank', 'E20:')
1112  bw!
1113endfunc
1114
1115" Test for changing text in visual mode with 'exclusive' selection
1116func Test_exclusive_selection()
1117  new
1118  call setline(1, ['one', 'two'])
1119  set selection=exclusive
1120  call feedkeys("vwcabc", 'xt')
1121  call assert_equal('abctwo', getline(1))
1122  call setline(1, ["\tone"])
1123  set virtualedit=all
1124  call feedkeys('0v2lcl', 'xt')
1125  call assert_equal('l      one', getline(1))
1126  set virtualedit&
1127  set selection&
1128  bw!
1129endfunc
1130
1131" Test for inclusive motion in visual mode with 'exclusive' selection
1132func Test_inclusive_motion_selection_exclusive()
1133  func s:compare_exclu_inclu(line, keys, expected_exclu)
1134    let msg = "data: '" . a:line . "' operation: '" . a:keys . "'"
1135    call setline(1, a:line)
1136    set selection=exclusive
1137    call feedkeys(a:keys, 'xt')
1138    call assert_equal(a:expected_exclu, getpos('.'), msg)
1139    let pos_ex = col('.')
1140    set selection=inclusive
1141    call feedkeys(a:keys, 'xt')
1142    let pos_in = col('.')
1143    call assert_equal(1, pos_ex - pos_in, msg)
1144  endfunc
1145
1146  new
1147  " Test 'e' motion
1148  set selection=exclusive
1149  call setline(1, 'eins zwei drei')
1150  norm! ggvey
1151  call assert_equal('eins', @")
1152  call setline(1, 'abc(abc)abc')
1153  norm! ggveeed
1154  call assert_equal(')abc', getline(1))
1155  call setline(1, 'abc(abc)abc')
1156  norm! gg3lvey
1157  call assert_equal('(abc', @")
1158  call s:compare_exclu_inclu('abc(abc)abc', 'ggveee', [0, 1, 8, 0])
1159  " Test 'f' motion
1160  call s:compare_exclu_inclu('geschwindigkeit', 'ggvfefe', [0, 1, 14, 0])
1161  call s:compare_exclu_inclu('loooooooooooong', 'ggv2fo2fo2fo', [0, 1, 8, 0])
1162  " Test 't' motion
1163  call s:compare_exclu_inclu('geschwindigkeit', 'ggv2te', [0, 1, 13, 0])
1164  call s:compare_exclu_inclu('loooooooooooong', 'gglv2to2to2to', [0, 1, 6, 0])
1165  " Test ';' motion
1166  call s:compare_exclu_inclu('geschwindigkeit', 'ggvfi;;', [0, 1, 15, 0])
1167  call s:compare_exclu_inclu('geschwindigkeit', 'ggvti;;', [0, 1, 14, 0])
1168  call s:compare_exclu_inclu('loooooooooooong', 'ggv2fo;;', [0, 1, 6, 0])
1169  call s:compare_exclu_inclu('loooooooooooong', 'ggvl2to;;', [0, 1, 6, 0])
1170  " Clean up
1171  set selection&
1172  bw!
1173endfunc
1174
1175" Test for starting linewise visual with a count.
1176" This test needs to be run without any previous visual mode. Otherwise the
1177" count will use the count from the previous visual mode.
1178func Test_linewise_visual_with_count()
1179  let after =<< trim [CODE]
1180    call setline(1, ['one', 'two', 'three', 'four'])
1181    norm! 3Vy
1182    call assert_equal("one\ntwo\nthree\n", @")
1183    call writefile(v:errors, 'Xtestout')
1184    qall!
1185  [CODE]
1186  if RunVim([], after, '')
1187    call assert_equal([], readfile('Xtestout'))
1188    call delete('Xtestout')
1189  endif
1190endfunc
1191
1192" Test for starting characterwise visual with a count.
1193" This test needs to be run without any previous visual mode. Otherwise the
1194" count will use the count from the previous visual mode.
1195func Test_characterwise_visual_with_count()
1196  let after =<< trim [CODE]
1197    call setline(1, ['one two', 'three'])
1198    norm! l5vy
1199    call assert_equal("ne tw", @")
1200    call writefile(v:errors, 'Xtestout')
1201    qall!
1202  [CODE]
1203  if RunVim([], after, '')
1204    call assert_equal([], readfile('Xtestout'))
1205    call delete('Xtestout')
1206  endif
1207endfunc
1208
1209" Test for visually selecting an inner block (iB)
1210func Test_visual_inner_block()
1211  new
1212  call setline(1, ['one', '{', 'two', '{', 'three', '}', 'four', '}', 'five'])
1213  call cursor(5, 1)
1214  " visually select all the lines in the block and then execute iB
1215  call feedkeys("ViB\<C-C>", 'xt')
1216  call assert_equal([0, 5, 1, 0], getpos("'<"))
1217  call assert_equal([0, 5, 6, 0], getpos("'>"))
1218  " visually select two inner blocks
1219  call feedkeys("ViBiB\<C-C>", 'xt')
1220  call assert_equal([0, 3, 1, 0], getpos("'<"))
1221  call assert_equal([0, 7, 5, 0], getpos("'>"))
1222  " try to select non-existing inner block
1223  call cursor(5, 1)
1224  call assert_beeps('normal ViBiBiB')
1225  " try to select an unclosed inner block
1226  8,9d
1227  call cursor(5, 1)
1228  call assert_beeps('normal ViBiB')
1229  bw!
1230endfunc
1231
1232func Test_visual_put_in_block()
1233  new
1234  call setline(1, ['xxxx', 'y∞yy', 'zzzz'])
1235  normal 1G2yl
1236  exe "normal 1G2l\<C-V>jjlp"
1237  call assert_equal(['xxxx', 'y∞xx', 'zzxx'], getline(1, 3))
1238  bwipe!
1239endfunc
1240
1241func Test_visual_put_in_block_using_zp()
1242  new
1243  " paste using zP
1244  call setline(1, ['/path;text', '/path;text', '/path;text', '',
1245    \ '/subdir',
1246    \ '/longsubdir',
1247    \ '/longlongsubdir'])
1248  exe "normal! 5G\<c-v>2j$y"
1249  norm! 1Gf;zP
1250  call assert_equal(['/path/subdir;text', '/path/longsubdir;text', '/path/longlongsubdir;text'], getline(1, 3))
1251  %d
1252  " paste using zP
1253  call setline(1, ['/path;text', '/path;text', '/path;text', '',
1254    \ '/subdir',
1255    \ '/longsubdir',
1256    \ '/longlongsubdir'])
1257  exe "normal! 5G\<c-v>2j$y"
1258  norm! 1Gf;hzp
1259  call assert_equal(['/path/subdir;text', '/path/longsubdir;text', '/path/longlongsubdir;text'], getline(1, 3))
1260  bwipe!
1261endfunc
1262
1263func Test_visual_put_in_block_using_zy_and_zp()
1264  new
1265
1266  " Test 1) Paste using zp - after the cursor without trailing spaces
1267  call setline(1, ['/path;text', '/path;text', '/path;text', '',
1268    \ 'texttext  /subdir           columntext',
1269		\ 'texttext  /longsubdir       columntext',
1270    \ 'texttext  /longlongsubdir   columntext'])
1271  exe "normal! 5G0f/\<c-v>2jezy"
1272  norm! 1G0f;hzp
1273  call assert_equal(['/path/subdir;text', '/path/longsubdir;text', '/path/longlongsubdir;text'], getline(1, 3))
1274
1275  " Test 2) Paste using zP - in front of the cursor without trailing spaces
1276  %d
1277  call setline(1, ['/path;text', '/path;text', '/path;text', '',
1278    \ 'texttext  /subdir           columntext',
1279		\ 'texttext  /longsubdir       columntext',
1280    \ 'texttext  /longlongsubdir   columntext'])
1281  exe "normal! 5G0f/\<c-v>2jezy"
1282  norm! 1G0f;zP
1283  call assert_equal(['/path/subdir;text', '/path/longsubdir;text', '/path/longlongsubdir;text'], getline(1, 3))
1284
1285  " Test 3) Paste using p - with trailing spaces
1286  %d
1287  call setline(1, ['/path;text', '/path;text', '/path;text', '',
1288    \ 'texttext  /subdir           columntext',
1289		\ 'texttext  /longsubdir       columntext',
1290    \ 'texttext  /longlongsubdir   columntext'])
1291  exe "normal! 5G0f/\<c-v>2jezy"
1292  norm! 1G0f;hp
1293  call assert_equal(['/path/subdir        ;text', '/path/longsubdir    ;text', '/path/longlongsubdir;text'], getline(1, 3))
1294
1295  " Test 4) Paste using P - with trailing spaces
1296  %d
1297  call setline(1, ['/path;text', '/path;text', '/path;text', '',
1298    \ 'texttext  /subdir           columntext',
1299		\ 'texttext  /longsubdir       columntext',
1300    \ 'texttext  /longlongsubdir   columntext'])
1301  exe "normal! 5G0f/\<c-v>2jezy"
1302  norm! 1G0f;P
1303  call assert_equal(['/path/subdir        ;text', '/path/longsubdir    ;text', '/path/longlongsubdir;text'], getline(1, 3))
1304
1305  " Test 5) Yank with spaces inside the block
1306  %d
1307  call setline(1, ['/path;text', '/path;text', '/path;text', '',
1308    \ 'texttext  /sub    dir/           columntext',
1309    \ 'texttext  /lon    gsubdir/       columntext',
1310    \ 'texttext  /lon    glongsubdir/   columntext'])
1311  exe "normal! 5G0f/\<c-v>2jf/zy"
1312  norm! 1G0f;zP
1313  call assert_equal(['/path/sub    dir/;text', '/path/lon    gsubdir/;text', '/path/lon    glongsubdir/;text'], getline(1, 3))
1314  bwipe!
1315endfunc
1316
1317func Test_visual_put_blockedit_zy_and_zp()
1318  new
1319
1320  call setline(1, ['aa', 'bbbbb', 'ccc', '', 'XX', 'GGHHJ', 'RTZU'])
1321  exe "normal! gg0\<c-v>2j$zy"
1322  norm! 5gg0zP
1323  call assert_equal(['aa', 'bbbbb', 'ccc', '', 'aaXX', 'bbbbbGGHHJ', 'cccRTZU'], getline(1, 7))
1324  "
1325  " now with blockmode editing
1326  sil %d
1327  :set ve=block
1328  call setline(1, ['aa', 'bbbbb', 'ccc', '', 'XX', 'GGHHJ', 'RTZU'])
1329  exe "normal! gg0\<c-v>2j$zy"
1330  norm! 5gg0zP
1331  call assert_equal(['aa', 'bbbbb', 'ccc', '', 'aaXX', 'bbbbbGGHHJ', 'cccRTZU'], getline(1, 7))
1332  set ve&vim
1333  bw!
1334endfunc
1335
1336func Test_visual_block_yank_zy()
1337  new
1338  " this was reading before the start of the line
1339  exe "norm o\<C-T>\<Esc>\<C-V>zy"
1340  bwipe!
1341endfunc
1342
1343func Test_visual_block_with_virtualedit()
1344  CheckScreendump
1345
1346  let lines =<< trim END
1347    call setline(1, ['aaaaaa', 'bbbb', 'cc'])
1348    set virtualedit=block
1349    normal G
1350  END
1351  call writefile(lines, 'XTest_block', 'D')
1352
1353  let buf = RunVimInTerminal('-S XTest_block', {'rows': 8, 'cols': 50})
1354  call term_sendkeys(buf, "\<C-V>gg$")
1355  call WaitForAssert({-> assert_match('VISUAL.*\dx\d', term_getline(buf, 8))}, 1000)
1356  call VerifyScreenDump(buf, 'Test_visual_block_with_virtualedit', {})
1357
1358  call term_sendkeys(buf, "\<Esc>gg\<C-V>G$")
1359  call VerifyScreenDump(buf, 'Test_visual_block_with_virtualedit2', {})
1360
1361  " clean up
1362  call term_sendkeys(buf, "\<Esc>")
1363  call StopVimInTerminal(buf)
1364endfunc
1365
1366func Test_visual_block_ctrl_w_f()
1367  " Empty block selected in new buffer should not result in an error.
1368  au! BufNew foo sil norm f
1369  edit foo
1370
1371  au! BufNew
1372endfunc
1373
1374func Test_visual_block_append_invalid_char()
1375  " this was going over the end of the line
1376  set isprint=@,161-255
1377  new
1378  call setline(1, ['	   let xxx', 'xxxxxˆ', 'xxxxxxxxxxx'])
1379  exe "normal 0\<C-V>jjA-\<Esc>"
1380  call assert_equal(['	-   let xxx', 'xxxxx   -ˆ', 'xxxxxxxx-xxx'], getline(1, 3))
1381  bwipe!
1382  set isprint&
1383endfunc
1384
1385func Test_visual_block_with_substitute()
1386  " this was reading beyond the end of the line
1387  new
1388  norm a0)
1389  sil! norm  O
1390  s/)
1391  sil! norm 
1392  bwipe!
1393endfunc
1394
1395func Test_visual_reselect_with_count()
1396  enew
1397  call setline(1, ['aaaaaa', '✗ bbbb', '✗ bbbb'])
1398  exe "normal! 2Gw\<C-V>jed"
1399  exe "normal! gg0lP"
1400  call assert_equal(['abbbbaaaaa', '✗bbbb ', '✗ '], getline(1, '$'))
1401
1402  exe "normal! 1vr."
1403  call assert_equal(['a....aaaaa', '✗.... ', '✗ '], getline(1, '$'))
1404
1405  bwipe!
1406
1407  " this was causing an illegal memory access
1408  let lines =<< trim END
1409
1410
1411
1412      :
1413      r<sfile>
1414      exe "%norm e3\<c-v>kr\t"
1415      :
1416
1417      :
1418  END
1419  call writefile(lines, 'XvisualReselect')
1420  source XvisualReselect
1421
1422  bwipe!
1423  call delete('XvisualReselect')
1424endfunc
1425
1426func Test_visual_reselect_exclusive()
1427  new
1428  call setline(1, ['abcde', 'abcde'])
1429  set selection=exclusive
1430  normal 1G0viwd
1431  normal 2G01vd
1432  call assert_equal(['', ''], getline(1, 2))
1433
1434  set selection&
1435  bwipe!
1436endfunc
1437
1438func Test_visual_block_insert_round_off()
1439  new
1440  " The number of characters are tuned to fill a 4096 byte allocated block,
1441  " so that valgrind reports going over the end.
1442  call setline(1, ['xxxxx', repeat('0', 1350), "\t", repeat('x', 60)])
1443  exe "normal gg0\<C-V>GI" .. repeat('0', 1320) .. "\<Esc>"
1444  bwipe!
1445endfunc
1446
1447" this was causing an ml_get error
1448func Test_visual_exchange_windows()
1449  enew!
1450  new
1451  call setline(1, ['foo', 'bar'])
1452  exe "normal G\<C-V>gg\<C-W>\<C-X>OO\<Esc>"
1453  bwipe!
1454  bwipe!
1455endfunc
1456
1457" this was leaving the end of the Visual area beyond the end of a line
1458func Test_visual_ex_copy_line()
1459  new
1460  call setline(1, ["aaa", "bbbbbbbbbxbb"])
1461  /x
1462  exe "normal ggvjfxO"
1463  t0
1464  normal gNU
1465  bwipe!
1466endfunc
1467
1468" This was leaving the end of the Visual area beyond the end of a line.
1469" Set 'undolevels' to start a new undo block.
1470func Test_visual_undo_deletes_last_line()
1471  new
1472  call setline(1, ["aaa", "ccc", "dyd"])
1473  set undolevels=100
1474  exe "normal obbbbbbbbbxbb\<Esc>"
1475  set undolevels=100
1476  /y
1477  exe "normal ggvjfxO"
1478  undo
1479  normal gNU
1480
1481  bwipe!
1482endfunc
1483
1484func Test_visual_paste()
1485  new
1486
1487  " v_p overwrites unnamed register.
1488  call setline(1, ['xxxx'])
1489  call setreg('"', 'foo')
1490  call setreg('-', 'bar')
1491  normal gg0vp
1492  call assert_equal('x', @")
1493  call assert_equal('x', @-)
1494  call assert_equal('fooxxx', getline(1))
1495  normal $vp
1496  call assert_equal('x', @")
1497  call assert_equal('x', @-)
1498  call assert_equal('fooxxx', getline(1))
1499  " Test with a different register as unnamed register.
1500  call setline(2, ['baz'])
1501  normal 2gg0"rD
1502  call assert_equal('baz', @")
1503  normal gg0vp
1504  call assert_equal('f', @")
1505  call assert_equal('f', @-)
1506  call assert_equal('bazooxxx', getline(1))
1507  normal $vp
1508  call assert_equal('x', @")
1509  call assert_equal('x', @-)
1510  call assert_equal('bazooxxf', getline(1))
1511
1512  bwipe!
1513endfunc
1514
1515func Test_visual_paste_clipboard()
1516  CheckFeature clipboard_working
1517
1518  if has('gui')
1519    " auto select feature breaks tests
1520    set guioptions-=a
1521  endif
1522
1523  " v_P does not overwrite unnamed register.
1524  call setline(1, ['xxxx'])
1525  call setreg('"', 'foo')
1526  call setreg('-', 'bar')
1527  normal gg0vP
1528  call assert_equal('foo', @")
1529  call assert_equal('bar', @-)
1530  call assert_equal('fooxxx', getline(1))
1531  normal $vP
1532  call assert_equal('foo', @")
1533  call assert_equal('bar', @-)
1534  call assert_equal('fooxxfoo', getline(1))
1535  " Test with a different register as unnamed register.
1536  call setline(2, ['baz'])
1537  normal 2gg0"rD
1538  call assert_equal('baz', @")
1539  normal gg0vP
1540  call assert_equal('baz', @")
1541  call assert_equal('bar', @-)
1542  call assert_equal('bazooxxfoo', getline(1))
1543  normal $vP
1544  call assert_equal('baz', @")
1545  call assert_equal('bar', @-)
1546  call assert_equal('bazooxxfobaz', getline(1))
1547
1548  " Test for unnamed clipboard
1549  set clipboard=unnamed
1550  call setline(1, ['xxxx'])
1551  call setreg('"', 'foo')
1552  call setreg('-', 'bar')
1553  call setreg('*', 'baz')
1554  normal gg0vP
1555  call assert_equal('foo', @")
1556  call assert_equal('bar', @-)
1557  call assert_equal('baz', @*)
1558  call assert_equal('bazxxx', getline(1))
1559
1560  " Test for unnamedplus clipboard
1561  if has('unnamedplus')
1562    set clipboard=unnamedplus
1563    call setline(1, ['xxxx'])
1564    call setreg('"', 'foo')
1565    call setreg('-', 'bar')
1566    call setreg('+', 'baz')
1567    normal gg0vP
1568    call assert_equal('foo', @")
1569    call assert_equal('bar', @-)
1570    call assert_equal('baz', @+)
1571    call assert_equal('bazxxx', getline(1))
1572  endif
1573
1574  set clipboard&
1575  if has('gui')
1576    set guioptions&
1577  endif
1578  bwipe!
1579endfunc
1580
1581func Test_visual_area_adjusted_when_hiding()
1582  " The Visual area ended after the end of the line after :hide
1583  call setline(1, 'xxx')
1584  vsplit Xfile
1585  call setline(1, 'xxxxxxxx')
1586  norm! $o
1587  hid
1588  norm! zW
1589  bwipe!
1590  bwipe!
1591endfunc
1592
1593func Test_switch_buffer_ends_visual_mode()
1594  enew
1595  call setline(1, 'foo')
1596  set hidden
1597  set virtualedit=all
1598  let buf1 = bufnr()
1599  enew
1600  let buf2 = bufnr()
1601  call setline(1, ['', '', '', ''])
1602  call cursor(4, 5)
1603  call feedkeys("\<C-V>3k4h", 'xt')
1604  exe 'buffer' buf1
1605  call assert_equal('n', mode())
1606
1607  set nohidden
1608  set virtualedit=
1609  bwipe!
1610  exe 'bwipe!' buf2
1611endfunc
1612
1613" Check fix for the heap-based buffer overflow bug found in the function
1614" utfc_ptr2len and reported at
1615" https://huntr.dev/bounties/ae933869-a1ec-402a-bbea-d51764c6618e
1616func Test_heap_buffer_overflow()
1617  enew
1618  set updatecount=0
1619
1620  norm R0
1621  split other
1622  norm R000
1623  exe "norm \<C-V>l"
1624  ball
1625  call assert_equal(getpos("."), getpos("v"))
1626  call assert_equal('n', mode())
1627  norm zW
1628
1629  %bwipe!
1630  set updatecount&
1631endfunc
1632
1633func s:ScreenAttr(row, col)
1634  redraw
1635  return screenattr(a:row, a:col)
1636endfunc
1637
1638func Test_charwise_visual_hl_inclusive_and_exclusive_selection()
1639  new
1640  call setline(1, '12345')
1641
1642  normal! V$
1643  const visual_attr = s:ScreenAttr(1, 1)
1644  let VisualCols = {-> range(1, strlen(getline(1)))->filter({_, c ->
1645        \ s:ScreenAttr(1, c) == visual_attr})}
1646
1647  exe "normal! \<Esc>"
1648  call assert_true(visual_attr != s:ScreenAttr(1, 1),
1649        \ "sanity check: Visual displayed differently to Normal")
1650
1651  set selection=inclusive
1652  normal! ggf2vf4
1653  call assert_equal([2, 3, 4], VisualCols())
1654
1655  set selection=exclusive
1656  exe "normal! \<Esc>ggf2vll"
1657  call assert_equal([2, 3], VisualCols())  " (Character under cursor excluded.)
1658  normal! o
1659  call assert_equal(2, col('.'), "sanity check: cursor column after 'o'")
1660  call assert_equal([2, 3], VisualCols())
1661  normal! y
1662  call assert_equal('23', @",
1663        \ "sanity check: yanked text matches visual selection")
1664
1665  set selection&
1666  bwipe!
1667endfunc
1668
1669" Test Visual highlight with cursor at end of screen line and 'showbreak'
1670func Test_visual_hl_with_showbreak()
1671  CheckScreendump
1672
1673  let lines =<< trim END
1674    setlocal showbreak=+
1675    call setline(1, repeat('a', &columns + 10))
1676    normal g$v4lo
1677  END
1678  call writefile(lines, 'XTest_visual_sbr', 'D')
1679
1680  let buf = RunVimInTerminal('-S XTest_visual_sbr', {'rows': 6, 'cols': 50})
1681  call VerifyScreenDump(buf, 'Test_visual_hl_with_showbreak', {})
1682
1683  " clean up
1684  call term_sendkeys(buf, "\<Esc>")
1685  call StopVimInTerminal(buf)
1686endfunc
1687
1688func Test_Visual_r_CTRL_C()
1689  new
1690  " visual r_cmd
1691  call setline(1, ['   '])
1692  call feedkeys("\<c-v>$r\<c-c>", 'tx')
1693  call assert_equal([''], getline(1, 1))
1694
1695  " visual gr_cmd
1696  call setline(1, ['   '])
1697  call feedkeys("\<c-v>$gr\<c-c>", 'tx')
1698  call assert_equal([''], getline(1, 1))
1699  bw!
1700endfunc
1701
1702func Test_visual_drag_out_of_window()
1703  rightbelow vnew
1704  call setline(1, '123456789')
1705  set mouse=a
1706  func ClickExpr(off)
1707    call Ntest_setmouse(1, getwininfo(win_getid())[0].wincol + a:off)
1708    return "\<LeftMouse>"
1709  endfunc
1710  func DragExpr(off)
1711    call Ntest_setmouse(1, getwininfo(win_getid())[0].wincol + a:off)
1712    return "\<LeftDrag>"
1713  endfunc
1714
1715  nnoremap <expr> <F2> ClickExpr(5)
1716  nnoremap <expr> <F3> DragExpr(-1)
1717  redraw
1718  call feedkeys("\<F2>\<F3>\<LeftRelease>", 'tx')
1719  call assert_equal([1, 6], [col('.'), col('v')])
1720  call feedkeys("\<Esc>", 'tx')
1721
1722  nnoremap <expr> <F2> ClickExpr(6)
1723  nnoremap <expr> <F3> DragExpr(-2)
1724  redraw
1725  call feedkeys("\<F2>\<F3>\<LeftRelease>", 'tx')
1726  call assert_equal([1, 7], [col('.'), col('v')])
1727  call feedkeys("\<Esc>", 'tx')
1728
1729  nunmap <F2>
1730  nunmap <F3>
1731  delfunc ClickExpr
1732  delfunc DragExpr
1733  set mouse&
1734  bwipe!
1735endfunc
1736
1737func Test_visual_substitute_visual()
1738  new
1739  call setline(1, ['one', 'two', 'three'])
1740  call feedkeys("Gk\<C-V>j$:s/\\%V\\_.*\\%V/foobar\<CR>", 'tx')
1741  call assert_equal(['one', 'foobar'], getline(1, '$'))
1742  bwipe!
1743endfunc
1744
1745func Test_virtualedit_exclusive_selection()
1746  new
1747  set virtualedit=all selection=exclusive
1748
1749  call setline(1, "a\tb")
1750  normal! 0v8ly
1751  call assert_equal("a\t", getreg('"'))
1752  normal! 0v6ly
1753  call assert_equal('a     ', getreg('"'))
1754  normal! 06lv2ly
1755  call assert_equal('  ', getreg('"'))
1756
1757  set virtualedit& selection&
1758  bwipe!
1759endfunc
1760
1761func Test_visual_getregion()
1762  let lines =<< trim END
1763    new
1764
1765    call setline(1, ['one', 'two', 'three'])
1766
1767    #" Visual mode
1768    call cursor(1, 1)
1769    call feedkeys("\<ESC>vjl", 'tx')
1770
1771    call assert_equal(['one', 'tw'],
1772          \ 'v'->getpos()->getregion(getpos('.')))
1773    call assert_equal([
1774          \   [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 3, 0]],
1775          \   [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 2, 0]]
1776          \ ],
1777          \ 'v'->getpos()->getregionpos(getpos('.')))
1778
1779    call assert_equal(['one', 'tw'],
1780          \ '.'->getpos()->getregion(getpos('v')))
1781    call assert_equal([
1782          \   [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 3, 0]],
1783          \   [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 2, 0]]
1784          \ ],
1785          \ '.'->getpos()->getregionpos(getpos('v')))
1786
1787    call assert_equal(['o'],
1788          \ 'v'->getpos()->getregion(getpos('v')))
1789    call assert_equal([
1790          \   [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 1, 0]],
1791          \ ],
1792          \ 'v'->getpos()->getregionpos(getpos('v')))
1793
1794    call assert_equal(['w'],
1795          \ '.'->getpos()->getregion(getpos('.'), {'type': 'v' }))
1796    call assert_equal([
1797          \   [[bufnr('%'), 2, 2, 0], [bufnr('%'), 2, 2, 0]],
1798          \ ],
1799          \ '.'->getpos()->getregionpos(getpos('.'), {'type': 'v' }))
1800
1801    call assert_equal(['one', 'two'],
1802          \ getpos('.')->getregion(getpos('v'), {'type': 'V' }))
1803    call assert_equal([
1804          \   [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 3, 0]],
1805          \   [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 3, 0]],
1806          \ ],
1807          \ getpos('.')->getregionpos(getpos('v'), {'type': 'V' }))
1808
1809    call assert_equal(['on', 'tw'],
1810          \ getpos('.')->getregion(getpos('v'), {'type': "\<C-v>" }))
1811    call assert_equal([
1812          \   [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 2, 0]],
1813          \   [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 2, 0]],
1814          \ ],
1815          \ getpos('.')->getregionpos(getpos('v'), {'type': "\<C-v>" }))
1816
1817    #" Line visual mode
1818    call cursor(1, 1)
1819    call feedkeys("\<ESC>Vl", 'tx')
1820    call assert_equal(['one'],
1821          \ getregion(getpos('v'), getpos('.'), {'type': 'V' }))
1822    call assert_equal(['one'],
1823          \ getregion(getpos('.'), getpos('v'), {'type': 'V' }))
1824    call assert_equal(['one'],
1825          \ getregion(getpos('v'), getpos('v'), {'type': 'V' }))
1826    call assert_equal(['one'],
1827          \ getregion(getpos('.'), getpos('.'), {'type': 'V' }))
1828    call assert_equal(['on'],
1829          \ getpos('.')->getregion(getpos('v'), {'type': 'v' }))
1830    call assert_equal(['on'],
1831          \ getpos('.')->getregion(getpos('v'), {'type': "\<C-v>" }))
1832
1833    #" Block visual mode
1834    call cursor(1, 1)
1835    call feedkeys("\<ESC>\<C-v>ll", 'tx')
1836    call assert_equal(['one'],
1837          \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
1838    call assert_equal(['one'],
1839          \ getregion(getpos('.'), getpos('v'), {'type': "\<C-v>" }))
1840    call assert_equal(['o'],
1841          \ getregion(getpos('v'), getpos('v'), {'type': "\<C-v>" }))
1842    call assert_equal(['e'],
1843          \ getregion(getpos('.'), getpos('.'), {'type': "\<C-v>" }))
1844    call assert_equal(['one'],
1845          \ '.'->getpos()->getregion(getpos('v'), {'type': 'V' }))
1846    call assert_equal(['one'],
1847          \ '.'->getpos()->getregion(getpos('v'), {'type': 'v' }))
1848
1849    #" Using Marks
1850    call setpos("'a", [0, 2, 3, 0])
1851    call cursor(1, 1)
1852    call assert_equal(['one', 'two'],
1853          \ "'a"->getpos()->getregion(getpos('.'), {'type': 'v' }))
1854    call assert_equal(['one', 'two'],
1855          \ "."->getpos()->getregion(getpos("'a"), {'type': 'v' }))
1856    call assert_equal(['one', 'two'],
1857          \ "."->getpos()->getregion(getpos("'a"), {'type': 'V' }))
1858    call assert_equal(['two'],
1859          \ "'a"->getpos()->getregion(getpos("'a"), {'type': 'V' }))
1860    call assert_equal(['one', 'two'],
1861          \ "."->getpos()->getregion(getpos("'a"), {'type': "\<c-v>" }))
1862    call feedkeys("\<ESC>jVj\<ESC>", 'tx')
1863    call assert_equal(['two', 'three'], getregion(getpos("'<"), getpos("'>")))
1864    call assert_equal(['two', 'three'], getregion(getpos("'>"), getpos("'<")))
1865
1866    #" Using List
1867    call cursor(1, 1)
1868    call assert_equal(['one', 'two'],
1869          \ [0, 2, 3, 0]->getregion(getpos('.'), {'type': 'v' }))
1870    call assert_equal(['one', 'two'],
1871          \ '.'->getpos()->getregion([0, 2, 3, 0], {'type': 'v' }))
1872    call assert_equal(['one', 'two'],
1873          \ '.'->getpos()->getregion([0, 2, 3, 0], {'type': 'V' }))
1874    call assert_equal(['two'],
1875          \ [0, 2, 3, 0]->getregion([0, 2, 3, 0], {'type': 'V' }))
1876    call assert_equal(['one', 'two'],
1877          \ '.'->getpos()->getregion([0, 2, 3, 0], {'type': "\<c-v>" }))
1878
1879    #" Multiline with line visual mode
1880    call cursor(1, 1)
1881    call feedkeys("\<ESC>Vjj", 'tx')
1882    call assert_equal(['one', 'two', 'three'],
1883          \ getregion(getpos('v'), getpos('.'), {'type': 'V' }))
1884    call assert_equal([
1885          \   [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 3, 0]],
1886          \   [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 3, 0]],
1887          \   [[bufnr('%'), 3, 1, 0], [bufnr('%'), 3, 5, 0]],
1888          \ ],
1889          \ getregionpos(getpos('v'), getpos('.'), {'type': 'V' }))
1890    call assert_equal([
1891          \   [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 4, 0]],
1892          \   [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 4, 0]],
1893          \   [[bufnr('%'), 3, 1, 0], [bufnr('%'), 3, 6, 0]],
1894          \ ],
1895          \ getregionpos(getpos('v'), getpos('.'),
1896          \              {'type': 'V', 'eol': v:true }))
1897
1898    #" Multiline with block visual mode
1899    call cursor(1, 1)
1900    call feedkeys("\<ESC>\<C-v>jj", 'tx')
1901    call assert_equal(['o', 't', 't'],
1902          \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
1903    call assert_equal([
1904          \   [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 1, 0]],
1905          \   [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 1, 0]],
1906          \   [[bufnr('%'), 3, 1, 0], [bufnr('%'), 3, 1, 0]],
1907          \ ],
1908          \ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
1909
1910    call cursor(1, 1)
1911    call feedkeys("\<ESC>\<C-v>jj$", 'tx')
1912    call assert_equal(['one', 'two', 'three'],
1913          \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
1914    call assert_equal([
1915          \   [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 3, 0]],
1916          \   [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 3, 0]],
1917          \   [[bufnr('%'), 3, 1, 0], [bufnr('%'), 3, 5, 0]],
1918          \ ],
1919          \ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
1920    call assert_equal([
1921          \   [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 3, 0]],
1922          \   [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 3, 0]],
1923          \   [[bufnr('%'), 3, 1, 0], [bufnr('%'), 3, 5, 0]],
1924          \ ],
1925          \ getregionpos(getpos('v'), getpos('.'),
1926          \              {'type': "\<C-v>", 'eol': v:true }))
1927
1928    #" 'virtualedit'
1929    set virtualedit=all
1930
1931    call cursor(1, 1)
1932    call feedkeys("\<ESC>\<C-v>10ljj$", 'tx')
1933    call assert_equal(['one   ', 'two   ', 'three '],
1934          \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
1935    call assert_equal([
1936          \   [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 3, 0]],
1937          \   [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 3, 0]],
1938          \   [[bufnr('%'), 3, 1, 0], [bufnr('%'), 3, 5, 0]],
1939          \ ],
1940          \ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
1941    call assert_equal([
1942          \   [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 4, 3]],
1943          \   [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 4, 3]],
1944          \   [[bufnr('%'), 3, 1, 0], [bufnr('%'), 3, 6, 1]],
1945          \ ],
1946          \ getregionpos(getpos('v'), getpos('.'),
1947          \              {'type': "\<C-v>", 'eol': v:true }))
1948
1949    call cursor(3, 5)
1950    call feedkeys("\<ESC>\<C-v>hkk", 'tx')
1951    call assert_equal(['  ', '  ', 'ee'],
1952          \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
1953    call assert_equal([
1954          \   [[bufnr('%'), 1, 0, 0], [bufnr('%'), 1, 0, 0]],
1955          \   [[bufnr('%'), 2, 0, 0], [bufnr('%'), 2, 0, 0]],
1956          \   [[bufnr('%'), 3, 4, 0], [bufnr('%'), 3, 5, 0]],
1957          \ ],
1958          \ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
1959    call assert_equal([
1960          \   [[bufnr('%'), 1, 4, 0], [bufnr('%'), 1, 4, 2]],
1961          \   [[bufnr('%'), 2, 4, 0], [bufnr('%'), 2, 4, 2]],
1962          \   [[bufnr('%'), 3, 4, 0], [bufnr('%'), 3, 5, 0]],
1963          \ ],
1964          \ getregionpos(getpos('v'), getpos('.'),
1965          \              {'type': "\<C-v>", 'eol': v:true }))
1966
1967    call cursor(3, 5)
1968    call feedkeys("\<ESC>\<C-v>kk", 'tx')
1969    call assert_equal([' ', ' ', 'e'],
1970          \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
1971    call assert_equal([
1972          \   [[bufnr('%'), 1, 0, 0], [bufnr('%'), 1, 0, 0]],
1973          \   [[bufnr('%'), 2, 0, 0], [bufnr('%'), 2, 0, 0]],
1974          \   [[bufnr('%'), 3, 5, 0], [bufnr('%'), 3, 5, 0]],
1975          \ ],
1976          \ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
1977    call assert_equal([
1978          \   [[bufnr('%'), 1, 4, 1], [bufnr('%'), 1, 4, 2]],
1979          \   [[bufnr('%'), 2, 4, 1], [bufnr('%'), 2, 4, 2]],
1980          \   [[bufnr('%'), 3, 5, 0], [bufnr('%'), 3, 5, 0]],
1981          \ ],
1982          \ getregionpos(getpos('v'), getpos('.'),
1983          \              {'type': "\<C-v>", 'eol': v:true }))
1984
1985    call cursor(1, 3)
1986    call feedkeys("\<ESC>vjj4l", 'tx')
1987    call assert_equal(['e', 'two', 'three  '],
1988          \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
1989    call assert_equal([
1990          \   [[bufnr('%'), 1, 3, 0], [bufnr('%'), 1, 3, 0]],
1991          \   [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 3, 0]],
1992          \   [[bufnr('%'), 3, 1, 0], [bufnr('%'), 3, 5, 0]],
1993          \ ],
1994          \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
1995    call assert_equal([
1996          \   [[bufnr('%'), 1, 3, 0], [bufnr('%'), 1, 4, 0]],
1997          \   [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 4, 0]],
1998          \   [[bufnr('%'), 3, 1, 0], [bufnr('%'), 3, 6, 2]],
1999          \ ],
2000          \ getregionpos(getpos('v'), getpos('.'),
2001          \              {'type': 'v', 'eol': v:true }))
2002
2003    call cursor(1, 3)
2004    call feedkeys("\<ESC>lvjj3l", 'tx')
2005    call assert_equal(['', 'two', 'three  '],
2006          \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
2007    call assert_equal([
2008          \   [[bufnr('%'), 1, 0, 0], [bufnr('%'), 1, 0, 0]],
2009          \   [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 3, 0]],
2010          \   [[bufnr('%'), 3, 1, 0], [bufnr('%'), 3, 5, 0]],
2011          \ ],
2012          \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
2013    call assert_equal([
2014          \   [[bufnr('%'), 1, 4, 0], [bufnr('%'), 1, 4, 0]],
2015          \   [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 4, 0]],
2016          \   [[bufnr('%'), 3, 1, 0], [bufnr('%'), 3, 6, 2]],
2017          \ ],
2018          \ getregionpos(getpos('v'), getpos('.'),
2019          \              {'type': 'v', 'eol': v:true }))
2020
2021    call cursor(3, 5)
2022    call feedkeys("\<ESC>v3l", 'tx')
2023    call assert_equal(['e   '],
2024          \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
2025    call assert_equal([
2026          \   [[bufnr('%'), 3, 5, 0], [bufnr('%'), 3, 5, 0]],
2027          \ ],
2028          \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
2029    call assert_equal([
2030          \   [[bufnr('%'), 3, 5, 0], [bufnr('%'), 3, 6, 3]],
2031          \ ],
2032          \ getregionpos(getpos('v'), getpos('.'),
2033          \              {'type': 'v', 'eol': v:true }))
2034
2035    call cursor(3, 5)
2036    call feedkeys("\<ESC>lv3l", 'tx')
2037    call assert_equal(['    '],
2038          \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
2039    call assert_equal([
2040          \   [[bufnr('%'), 3, 0, 0], [bufnr('%'), 3, 0, 0]],
2041          \ ],
2042          \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
2043    call assert_equal([
2044          \   [[bufnr('%'), 3, 6, 0], [bufnr('%'), 3, 6, 4]],
2045          \ ],
2046          \ getregionpos(getpos('v'), getpos('.'),
2047          \              {'type': 'v', 'eol': v:true }))
2048
2049    call cursor(3, 5)
2050    call feedkeys("\<ESC>3lv3l", 'tx')
2051    call assert_equal(['    '],
2052          \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
2053    call assert_equal([
2054          \   [[bufnr('%'), 3, 0, 0], [bufnr('%'), 3, 0, 0]],
2055          \ ],
2056          \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
2057    call assert_equal([
2058          \   [[bufnr('%'), 3, 6, 2], [bufnr('%'), 3, 6, 6]],
2059          \ ],
2060          \ getregionpos(getpos('v'), getpos('.'),
2061          \              {'type': 'v', 'eol': v:true }))
2062
2063    set virtualedit&
2064
2065    #" using wrong types for positions
2066    call cursor(1, 1)
2067    call feedkeys("\<ESC>vjj$", 'tx')
2068    call assert_fails("call getregion(1, 2)", 'E1211:')
2069    call assert_fails("call getregion(getpos('.'), {})", 'E1211:')
2070    call assert_fails(':echo "."->getpos()->getregion("$", [])', 'E1211:')
2071    call assert_fails("call getregionpos(1, 2)", 'E1211:')
2072    call assert_fails("call getregionpos(getpos('.'), {})", 'E1211:')
2073    call assert_fails(':echo "."->getpos()->getregionpos("$", [])', 'E1211:')
2074
2075    #" using invalid value for "type"
2076    call assert_fails("call getregion(getpos('.'), getpos('.'), {'type': '' })", 'E475:')
2077    call assert_fails("call getregionpos(getpos('.'), getpos('.'), {'type': '' })", 'E475:')
2078    call assert_fails("call getregion(getpos('.'), getpos('.'), {'type': 'v0' })", 'E475:')
2079    call assert_fails("call getregionpos(getpos('.'), getpos('.'), {'type': 'v0' })", 'E475:')
2080    call assert_fails("call getregion(getpos('.'), getpos('.'), {'type': 'V0' })", 'E475:')
2081    call assert_fails("call getregionpos(getpos('.'), getpos('.'), {'type': 'V0' })", 'E475:')
2082    call assert_fails("call getregion(getpos('.'), getpos('.'), {'type': '\<C-v>0' })", 'E475:')
2083    call assert_fails("call getregionpos(getpos('.'), getpos('.'), {'type': '\<C-v>0' })", 'E475:')
2084    call assert_fails("call getregion(getpos('.'), getpos('.'), {'type': '\<C-v>1:' })", 'E475:')
2085    call assert_fails("call getregionpos(getpos('.'), getpos('.'), {'type': '\<C-v>1:' })", 'E475:')
2086
2087    #" using a mark from another buffer to current buffer
2088    new
2089    LET g:buf = bufnr()
2090    call setline(1, range(10))
2091    normal! GmA
2092    wincmd p
2093    call assert_equal([g:buf, 10, 1, 0], getpos("'A"))
2094    call assert_equal([], getregion(getpos('.'), getpos("'A"), {'type': 'v' }))
2095    call assert_equal([], getregion(getpos("'A"), getpos('.'), {'type': 'v' }))
2096    call assert_equal([], getregionpos(getpos('.'), getpos("'A"), {'type': 'v' }))
2097    call assert_equal([], getregionpos(getpos("'A"), getpos('.'), {'type': 'v' }))
2098
2099    #" using two marks from another buffer
2100    wincmd p
2101    normal! GmB
2102    wincmd p
2103    call assert_equal([g:buf, 10, 1, 0], getpos("'B"))
2104    call assert_equal(['9'],
2105          \ getregion(getpos("'B"), getpos("'A"), {'type': 'v' }))
2106    call assert_equal([
2107          \   [[g:buf, 10, 1, 0], [g:buf, 10, 1, 0]],
2108          \ ],
2109          \ getregionpos(getpos("'B"), getpos("'A"), {'type': 'v' }))
2110
2111    #" using two positions from another buffer
2112    for type in ['v', 'V', "\<C-V>"]
2113      for exclusive in [v:false, v:true]
2114        call assert_equal(range(10)->mapnew('string(v:val)'),
2115              \ getregion([g:buf, 1, 1, 0], [g:buf, 10, 2, 0],
2116              \ {'type': type, 'exclusive': exclusive }))
2117        call assert_equal(range(10)->mapnew('string(v:val)'),
2118              \ getregion([g:buf, 10, 2, 0], [g:buf, 1, 1, 0],
2119              \ {'type': type, 'exclusive': exclusive }))
2120        call assert_equal(range(1, 10)->mapnew('repeat([[g:buf, v:val, 1, 0]], 2)'),
2121              \ getregionpos([g:buf, 1, 1, 0], [g:buf, 10, 2, 0],
2122              \ {'type': type, 'exclusive': exclusive }))
2123        call assert_equal(range(1, 10)->mapnew('repeat([[g:buf, v:val, 1, 0]], 2)'),
2124              \ getregionpos([g:buf, 10, 2, 0], [g:buf, 1, 1, 0],
2125              \ {'type': type, 'exclusive': exclusive }))
2126      endfor
2127    endfor
2128
2129    #" using invalid positions in buffer
2130    call assert_fails('call getregion([g:buf, 0, 1, 0], [g:buf, 10, 2, 0])', 'E966:')
2131    call assert_fails('call getregion([g:buf, 10, 2, 0], [g:buf, 0, 1, 0])', 'E966:')
2132    call assert_fails('call getregion([g:buf, 1, 1, 0], [g:buf, 11, 2, 0])', 'E966:')
2133    call assert_fails('call getregion([g:buf, 11, 2, 0], [g:buf, 1, 1, 0])', 'E966:')
2134    call assert_fails('call getregion([g:buf, 1, 1, 0], [g:buf, 10, 0, 0])', 'E964:')
2135    call assert_fails('call getregion([g:buf, 10, 0, 0], [g:buf, 1, 1, 0])', 'E964:')
2136    call assert_fails('call getregion([g:buf, 1, 1, 0], [g:buf, 10, 3, 0])', 'E964:')
2137    call assert_fails('call getregion([g:buf, 10, 3, 0], [g:buf, 1, 1, 0])', 'E964:')
2138    call assert_fails('call getregion([g:buf, 1, 0, 0], [g:buf, 1, 1, 0])', 'E964:')
2139    call assert_fails('call getregion([g:buf, 1, 1, 0], [g:buf, 1, 0, 0])', 'E964:')
2140
2141    #" using invalid buffer
2142    call assert_fails('call getregion([10000, 10, 1, 0], [10000, 10, 1, 0])', 'E681:')
2143
2144    exe $':{g:buf}bwipe!'
2145    unlet g:buf
2146    bwipe!
2147  END
2148  call CheckLegacyAndVim9Success(lines)
2149
2150  let lines =<< trim END
2151    #" Selection in starts or ends in the middle of a multibyte character
2152    new
2153    call setline(1, [
2154          \   "abcdefghijk\u00ab",
2155          \   "\U0001f1e6\u00ab\U0001f1e7\u00ab\U0001f1e8\u00ab\U0001f1e9",
2156          \   "1234567890"
2157          \ ])
2158
2159    call cursor(1, 3)
2160    call feedkeys("\<Esc>\<C-v>ljj", 'xt')
2161    call assert_equal(['cd', "\u00ab ", '34'],
2162          \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
2163    call assert_equal([
2164          \   [[bufnr('%'), 1, 3, 0], [bufnr('%'), 1, 4, 0]],
2165          \   [[bufnr('%'), 2, 5, 0], [bufnr('%'), 2, 7, 1]],
2166          \   [[bufnr('%'), 3, 3, 0], [bufnr('%'), 3, 4, 0]],
2167          \ ],
2168          \ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
2169
2170    call cursor(1, 4)
2171    call feedkeys("\<Esc>\<C-v>ljj", 'xt')
2172    call assert_equal(['de', "\U0001f1e7", '45'],
2173          \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
2174    call assert_equal([
2175          \   [[bufnr('%'), 1, 4, 0], [bufnr('%'), 1, 5, 0]],
2176          \   [[bufnr('%'), 2, 7, 0], [bufnr('%'), 2, 10, 0]],
2177          \   [[bufnr('%'), 3, 4, 0], [bufnr('%'), 3, 5, 0]],
2178          \ ],
2179          \ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
2180
2181    call cursor(1, 5)
2182    call feedkeys("\<Esc>\<C-v>jj", 'xt')
2183    call assert_equal(['e', ' ', '5'],
2184          \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
2185    call assert_equal([
2186          \   [[bufnr('%'), 1, 5, 0], [bufnr('%'), 1, 5, 0]],
2187          \   [[bufnr('%'), 2, 7, 1], [bufnr('%'), 2, 7, 2]],
2188          \   [[bufnr('%'), 3, 5, 0], [bufnr('%'), 3, 5, 0]],
2189          \ ],
2190          \ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
2191    call assert_equal(['efghijk«', '🇦«🇧«🇨«🇩', '12345'],
2192          \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
2193    call assert_equal([
2194          \   [[bufnr('%'), 1, 5, 0], [bufnr('%'), 1, 13, 0]],
2195          \   [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 22, 0]],
2196          \   [[bufnr('%'), 3, 1, 0], [bufnr('%'), 3, 5, 0]],
2197          \ ],
2198          \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
2199
2200    call cursor(1, 5)
2201    call feedkeys("\<Esc>\<C-v>5l2j", 'xt')
2202    call assert_equal(['efghij', ' «🇨« ', '567890'],
2203          \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
2204    call assert_equal([
2205          \   [[bufnr('%'), 1, 5, 0], [bufnr('%'), 1, 10, 0]],
2206          \   [[bufnr('%'), 2, 7, 1], [bufnr('%'), 2, 19, 1]],
2207          \   [[bufnr('%'), 3, 5, 0], [bufnr('%'), 3, 10, 0]],
2208          \ ],
2209          \ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
2210
2211    call cursor(1, 4)
2212    call feedkeys("\<Esc>\<C-v>02j", 'xt')
2213    call assert_equal(['abcd', '🇦« ', '1234'],
2214          \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
2215    call assert_equal([
2216          \   [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 4, 0]],
2217          \   [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 7, 1]],
2218          \   [[bufnr('%'), 3, 1, 0], [bufnr('%'), 3, 4, 0]],
2219          \ ],
2220          \ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
2221
2222    #" characterwise selection with multibyte chars
2223    call cursor(1, 1)
2224    call feedkeys("\<Esc>vj", 'xt')
2225    call assert_equal(['abcdefghijk«', "\U0001f1e6"],
2226          \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
2227    call assert_equal([
2228          \   [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 13, 0]],
2229          \   [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 4, 0]],
2230          \ ],
2231          \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
2232
2233    set selection=exclusive
2234    call feedkeys('l', 'xt')
2235    call assert_equal(['abcdefghijk«', "\U0001f1e6"],
2236          \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
2237    call assert_equal([
2238          \   [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 13, 0]],
2239          \   [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 4, 0]],
2240          \ ],
2241          \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
2242
2243    #" marks on multibyte chars
2244    call setpos("'a", [0, 1, 11, 0])
2245    call setpos("'b", [0, 2, 16, 0])
2246    call setpos("'c", [0, 2, 0, 0])
2247    call cursor(1, 1)
2248
2249    call assert_equal(['ghijk', '🇨«🇩'],
2250          \ getregion(getpos("'a"), getpos("'b"), {'type': "\<C-v>" }))
2251    call assert_equal([
2252          \   [[bufnr('%'), 1, 7, 0], [bufnr('%'), 1, 11, 0]],
2253          \   [[bufnr('%'), 2, 13, 0], [bufnr('%'), 2, 22, 0]],
2254          \ ],
2255          \ getregionpos(getpos("'a"), getpos("'b"), {'type': "\<C-v>" }))
2256
2257    call assert_equal(['k«', '🇦«🇧«🇨'],
2258          \ getregion(getpos("'a"), getpos("'b"), {'type': 'v' }))
2259    call assert_equal([
2260          \   [[bufnr('%'), 1, 11, 0], [bufnr('%'), 1, 13, 0]],
2261          \   [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 16, 0]],
2262          \ ],
2263          \ getregionpos(getpos("'a"), getpos("'b"), {'type': 'v' }))
2264
2265    call assert_equal(['k«'],
2266          \ getregion(getpos("'a"), getpos("'c"), {'type': 'v' }))
2267    call assert_equal([
2268          \   [[bufnr('%'), 1, 11, 0], [bufnr('%'), 1, 13, 0]],
2269          \ ],
2270          \ getregionpos(getpos("'a"), getpos("'c"), {'type': 'v' }))
2271
2272    #" use inclusive selection, although 'selection' is exclusive
2273    call setpos("'a", [0, 1, 11, 0])
2274    call setpos("'b", [0, 1, 1, 0])
2275    call assert_equal(['abcdefghijk'],
2276          \ getregion(getpos("'a"), getpos("'b"),
2277          \ {'type': "\<c-v>", 'exclusive': v:false }))
2278    call assert_equal(['abcdefghij'],
2279          \ getregion(getpos("'a"), getpos("'b"),
2280          \ {'type': "\<c-v>", 'exclusive': v:true }))
2281    call assert_equal(['abcdefghijk'],
2282          \ getregion(getpos("'a"), getpos("'b"),
2283          \ {'type': 'v', 'exclusive': 0 }))
2284    call assert_equal(['abcdefghij'],
2285          \ getregion(getpos("'a"), getpos("'b"),
2286          \ {'type': 'v', 'exclusive': 1 }))
2287    call assert_equal(['abcdefghijk«'],
2288          \ getregion(getpos("'a"), getpos("'b"),
2289          \ {'type': 'V', 'exclusive': 0 }))
2290    call assert_equal(['abcdefghijk«'],
2291          \ getregion(getpos("'a"), getpos("'b"),
2292          \ {'type': 'V', 'exclusive': 1 }))
2293
2294    set selection&
2295    bwipe!
2296  END
2297  call CheckLegacyAndVim9Success(lines)
2298
2299  let lines =<< trim END
2300    #" Exclusive selection
2301    new
2302    set selection=exclusive
2303    call setline(1, ["a\tc", "x\tz", '', ''])
2304    call cursor(1, 1)
2305    call feedkeys("\<Esc>v2l", 'xt')
2306    call assert_equal(["a\t"],
2307          \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
2308    call cursor(1, 1)
2309    call feedkeys("\<Esc>v$G", 'xt')
2310    call assert_equal(["a\tc", "x\tz", ''],
2311          \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
2312    call cursor(1, 1)
2313    call feedkeys("\<Esc>v$j", 'xt')
2314    call assert_equal(["a\tc", "x\tz"],
2315          \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
2316    call cursor(1, 1)
2317    call feedkeys("\<Esc>\<C-v>$j", 'xt')
2318    call assert_equal(["a\tc", "x\tz"],
2319          \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
2320    call cursor(1, 1)
2321    call feedkeys("\<Esc>\<C-v>$G", 'xt')
2322    call assert_equal(["a", "x", '', ''],
2323          \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
2324    call cursor(1, 1)
2325    call feedkeys("\<Esc>wv2j", 'xt')
2326    call assert_equal(["c", "x\tz"],
2327          \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
2328    set selection&
2329    bwipe!
2330
2331    #" Exclusive selection 2
2332    new
2333    call setline(1, ["a\tc", "x\tz", '', ''])
2334
2335    call cursor(1, 1)
2336    call feedkeys("\<Esc>v2l", 'xt')
2337    call assert_equal(["a\t"],
2338          \ getregion(getpos('v'), getpos('.'), {'exclusive': v:true }))
2339    call assert_equal([
2340          \   [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 2, 0]],
2341          \ ],
2342          \ getregionpos(getpos('v'), getpos('.'), {'exclusive': v:true }))
2343
2344    call cursor(1, 1)
2345    call feedkeys("\<Esc>v$G", 'xt')
2346    call assert_equal(["a\tc", "x\tz", ''],
2347          \ getregion(getpos('v'), getpos('.'), {'exclusive': v:true }))
2348    call assert_equal([
2349          \   [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 3, 0]],
2350          \   [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 3, 0]],
2351          \   [[bufnr('%'), 3, 0, 0], [bufnr('%'), 3, 0, 0]],
2352          \ ],
2353          \ getregionpos(getpos('v'), getpos('.'), {'exclusive': v:true }))
2354
2355    call cursor(1, 1)
2356    call feedkeys("\<Esc>v$j", 'xt')
2357    call assert_equal(["a\tc", "x\tz"],
2358          \ getregion(getpos('v'), getpos('.'), {'exclusive': v:true }))
2359    call assert_equal([
2360          \   [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 3, 0]],
2361          \   [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 3, 0]],
2362          \ ],
2363          \ getregionpos(getpos('v'), getpos('.'), {'exclusive': v:true }))
2364
2365    call cursor(1, 1)
2366    call feedkeys("\<Esc>\<C-v>$j", 'xt')
2367    call assert_equal(["a\tc", "x\tz"],
2368          \ getregion(getpos('v'), getpos('.'),
2369          \           {'exclusive': v:true, 'type': "\<C-v>" }))
2370    call assert_equal([
2371          \   [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 3, 0]],
2372          \   [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 3, 0]],
2373          \ ],
2374          \ getregionpos(getpos('v'), getpos('.'),
2375          \              {'exclusive': v:true, 'type': "\<C-v>" }))
2376
2377    call cursor(1, 1)
2378    call feedkeys("\<Esc>\<C-v>$G", 'xt')
2379    call assert_equal(["a", "x", '', ''],
2380          \ getregion(getpos('v'), getpos('.'),
2381          \           {'exclusive': v:true, 'type': "\<C-v>" }))
2382    call assert_equal([
2383          \   [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 1, 0]],
2384          \   [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 1, 0]],
2385          \   [[bufnr('%'), 3, 0, 0], [bufnr('%'), 3, 0, 0]],
2386          \   [[bufnr('%'), 4, 0, 0], [bufnr('%'), 4, 0, 0]],
2387          \ ],
2388          \ getregionpos(getpos('v'), getpos('.'),
2389          \              {'exclusive': v:true, 'type': "\<C-v>" }))
2390
2391    call cursor(1, 1)
2392    call feedkeys("\<Esc>wv2j", 'xt')
2393    call assert_equal(["c", "x\tz"],
2394          \ getregion(getpos('v'), getpos('.'), {'exclusive': v:true }))
2395    call assert_equal([
2396          \   [[bufnr('%'), 1, 3, 0], [bufnr('%'), 1, 3, 0]],
2397          \   [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 3, 0]],
2398          \ ],
2399          \ getregionpos(getpos('v'), getpos('.'), {'exclusive': v:true }))
2400
2401    #" 'virtualedit' with exclusive selection
2402    set selection=exclusive
2403    set virtualedit=all
2404
2405    call cursor(1, 1)
2406    call feedkeys("\<Esc>vj", 'xt')
2407    call assert_equal(["a\tc"],
2408          \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
2409    call assert_equal([
2410          \   [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 3, 0]],
2411          \ ],
2412          \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
2413
2414    call cursor(1, 1)
2415    call feedkeys("\<Esc>v8l", 'xt')
2416    call assert_equal(["a\t"],
2417          \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
2418    call assert_equal([
2419          \   [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 2, 0]],
2420          \ ],
2421          \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
2422
2423    call cursor(1, 1)
2424    call feedkeys("\<Esc>v6l", 'xt')
2425    call assert_equal(['a     '],
2426          \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
2427    call assert_equal([
2428          \   [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 2, 5]],
2429          \ ],
2430          \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
2431
2432    call cursor(1, 1)
2433    call feedkeys("\<Esc>6lv2l", 'xt')
2434    call assert_equal(['  '],
2435          \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
2436    call assert_equal([
2437          \   [[bufnr('%'), 1, 2, 5], [bufnr('%'), 1, 2, 0]],
2438          \ ],
2439          \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
2440
2441    call cursor(1, 1)
2442    call feedkeys("\<Esc>lv2l", 'xt')
2443    call assert_equal(['  '],
2444          \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
2445    call assert_equal([
2446          \   [[bufnr('%'), 1, 2, 0], [bufnr('%'), 1, 2, 2]],
2447          \ ],
2448          \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
2449
2450    call cursor(1, 1)
2451    call feedkeys("\<Esc>2lv2l", 'xt')
2452    call assert_equal(['  '],
2453          \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
2454    call assert_equal([
2455          \   [[bufnr('%'), 1, 2, 1], [bufnr('%'), 1, 2, 3]],
2456          \ ],
2457          \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
2458
2459    call feedkeys('j', 'xt')
2460    call assert_equal(['      c', 'x   '],
2461          \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
2462    call assert_equal([
2463          \   [[bufnr('%'), 1, 2, 1], [bufnr('%'), 1, 3, 0]],
2464          \   [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 2, 3]],
2465          \ ],
2466          \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
2467
2468    call cursor(1, 1)
2469    call feedkeys("\<Esc>6l\<C-v>2lj", 'xt')
2470    call assert_equal(['  ', '  '],
2471          \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
2472    call assert_equal([
2473          \   [[bufnr('%'), 1, 2, 5], [bufnr('%'), 1, 2, 7]],
2474          \   [[bufnr('%'), 2, 2, 5], [bufnr('%'), 2, 2, 7]],
2475          \ ],
2476          \ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
2477
2478    call cursor(1, 1)
2479    call feedkeys("\<Esc>l\<C-v>2l2j", 'xt')
2480    call assert_equal(['  ', '  ', '  '],
2481          \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
2482    call assert_equal([
2483          \   [[bufnr('%'), 1, 2, 0], [bufnr('%'), 1, 2, 2]],
2484          \   [[bufnr('%'), 2, 2, 0], [bufnr('%'), 2, 2, 2]],
2485          \   [[bufnr('%'), 3, 0, 0], [bufnr('%'), 3, 0, 0]],
2486          \ ],
2487          \ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
2488    call assert_equal([
2489          \   [[bufnr('%'), 1, 2, 0], [bufnr('%'), 1, 2, 2]],
2490          \   [[bufnr('%'), 2, 2, 0], [bufnr('%'), 2, 2, 2]],
2491          \   [[bufnr('%'), 3, 1, 1], [bufnr('%'), 3, 1, 3]],
2492          \ ],
2493          \ getregionpos(getpos('v'), getpos('.'),
2494          \              {'type': "\<C-v>", "eol": v:true }))
2495
2496    call cursor(1, 1)
2497    call feedkeys("\<Esc>2l\<C-v>2l2j", 'xt')
2498    call assert_equal(['  ', '  ', '  '],
2499          \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
2500    call assert_equal([
2501          \   [[bufnr('%'), 1, 2, 1], [bufnr('%'), 1, 2, 3]],
2502          \   [[bufnr('%'), 2, 2, 1], [bufnr('%'), 2, 2, 3]],
2503          \   [[bufnr('%'), 3, 0, 0], [bufnr('%'), 3, 0, 0]],
2504          \ ],
2505          \ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
2506    call assert_equal([
2507          \   [[bufnr('%'), 1, 2, 1], [bufnr('%'), 1, 2, 3]],
2508          \   [[bufnr('%'), 2, 2, 1], [bufnr('%'), 2, 2, 3]],
2509          \   [[bufnr('%'), 3, 1, 2], [bufnr('%'), 3, 1, 4]],
2510          \ ],
2511          \ getregionpos(getpos('v'), getpos('.'),
2512          \              {'type': "\<C-v>", "eol": v:true }))
2513
2514    #" 'virtualedit' with inclusive selection
2515    set selection&
2516
2517    call cursor(1, 1)
2518    call feedkeys("\<Esc>vj", 'xt')
2519    call assert_equal(["a\tc", 'x'],
2520          \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
2521    call assert_equal([
2522          \   [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 3, 0]],
2523          \   [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 1, 0]],
2524          \ ],
2525          \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
2526
2527    call cursor(1, 1)
2528    call feedkeys("\<Esc>v8l", 'xt')
2529    call assert_equal(["a\tc"],
2530          \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
2531    call assert_equal([
2532          \   [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 3, 0]],
2533          \ ],
2534          \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
2535
2536    call cursor(1, 1)
2537    call feedkeys("\<Esc>v6l", 'xt')
2538    call assert_equal(['a      '],
2539          \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
2540    call assert_equal([
2541          \   [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 2, 6]],
2542          \ ],
2543          \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
2544
2545    call cursor(1, 1)
2546    call feedkeys("\<Esc>6lv2l", 'xt')
2547    call assert_equal(['  c'],
2548          \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
2549    call assert_equal([
2550          \   [[bufnr('%'), 1, 2, 5], [bufnr('%'), 1, 3, 0]],
2551          \ ],
2552          \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
2553
2554    call cursor(1, 1)
2555    call feedkeys("\<Esc>lv2l", 'xt')
2556    call assert_equal(['   '],
2557          \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
2558    call assert_equal([
2559          \   [[bufnr('%'), 1, 2, 0], [bufnr('%'), 1, 2, 3]],
2560          \ ],
2561          \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
2562
2563    call cursor(1, 1)
2564    call feedkeys("\<Esc>2lv2l", 'xt')
2565    call assert_equal(['   '],
2566          \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
2567    call assert_equal([
2568          \   [[bufnr('%'), 1, 2, 1], [bufnr('%'), 1, 2, 4]],
2569          \ ],
2570          \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
2571
2572    call feedkeys('j', 'xt')
2573    call assert_equal(['      c', 'x    '],
2574          \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
2575    call assert_equal([
2576          \   [[bufnr('%'), 1, 2, 1], [bufnr('%'), 1, 3, 0]],
2577          \   [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 2, 4]],
2578          \ ],
2579          \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
2580
2581    call cursor(1, 1)
2582    call feedkeys("\<Esc>6l\<C-v>2lj", 'xt')
2583    call assert_equal(['  c', '  z'],
2584          \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
2585    call assert_equal([
2586          \   [[bufnr('%'), 1, 2, 5], [bufnr('%'), 1, 3, 0]],
2587          \   [[bufnr('%'), 2, 2, 5], [bufnr('%'), 2, 3, 0]],
2588          \ ],
2589          \ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
2590
2591    call cursor(1, 1)
2592    call feedkeys("\<Esc>l\<C-v>2l2j", 'xt')
2593    call assert_equal(['   ', '   ', '   '],
2594          \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
2595    call assert_equal([
2596          \   [[bufnr('%'), 1, 2, 0], [bufnr('%'), 1, 2, 3]],
2597          \   [[bufnr('%'), 2, 2, 0], [bufnr('%'), 2, 2, 3]],
2598          \   [[bufnr('%'), 3, 0, 0], [bufnr('%'), 3, 0, 0]],
2599          \ ],
2600          \ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
2601    call assert_equal([
2602          \   [[bufnr('%'), 1, 2, 0], [bufnr('%'), 1, 2, 3]],
2603          \   [[bufnr('%'), 2, 2, 0], [bufnr('%'), 2, 2, 3]],
2604          \   [[bufnr('%'), 3, 1, 1], [bufnr('%'), 3, 1, 4]],
2605          \ ],
2606          \ getregionpos(getpos('v'), getpos('.'),
2607          \              {'type': "\<C-v>", "eol": v:true }))
2608
2609    call cursor(1, 1)
2610    call feedkeys("\<Esc>2l\<C-v>2l2j", 'xt')
2611    call assert_equal(['   ', '   ', '   '],
2612          \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
2613    call assert_equal([
2614          \   [[bufnr('%'), 1, 2, 1], [bufnr('%'), 1, 2, 4]],
2615          \   [[bufnr('%'), 2, 2, 1], [bufnr('%'), 2, 2, 4]],
2616          \   [[bufnr('%'), 3, 0, 0], [bufnr('%'), 3, 0, 0]],
2617          \ ],
2618          \ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
2619    call assert_equal([
2620          \   [[bufnr('%'), 1, 2, 1], [bufnr('%'), 1, 2, 4]],
2621          \   [[bufnr('%'), 2, 2, 1], [bufnr('%'), 2, 2, 4]],
2622          \   [[bufnr('%'), 3, 1, 2], [bufnr('%'), 3, 1, 5]],
2623          \ ],
2624          \ getregionpos(getpos('v'), getpos('.'),
2625          \              {'type': "\<C-v>", "eol": v:true }))
2626
2627    set virtualedit&
2628    bwipe!
2629  END
2630  call CheckLegacyAndVim9Success(lines)
2631
2632  let lines =<< trim END
2633    #" 'virtualedit' with TABs at end of line
2634    new
2635    set virtualedit=all
2636    call setline(1, ["\t", "a\t", "aa\t"])
2637
2638    call feedkeys("gg06l\<C-v>3l2j", 'xt')
2639    call assert_equal(['    ', '    ', '    '],
2640          \ getregion(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
2641    call assert_equal([
2642          \   [[bufnr('%'), 1, 1, 6], [bufnr('%'), 1, 1, 0]],
2643          \   [[bufnr('%'), 2, 2, 5], [bufnr('%'), 2, 2, 0]],
2644          \   [[bufnr('%'), 3, 3, 4], [bufnr('%'), 3, 3, 0]],
2645          \ ],
2646          \ getregionpos(getpos('v'), getpos('.'), {'type': "\<C-v>" }))
2647    call assert_equal([
2648          \   [[bufnr('%'), 1, 1, 6], [bufnr('%'), 1, 2, 2]],
2649          \   [[bufnr('%'), 2, 2, 5], [bufnr('%'), 2, 3, 2]],
2650          \   [[bufnr('%'), 3, 3, 4], [bufnr('%'), 3, 4, 2]],
2651          \ ],
2652          \ getregionpos(getpos('v'), getpos('.'),
2653          \              {'type': "\<C-v>", "eol": v:true }))
2654
2655    call feedkeys("gg06lv3l", 'xt')
2656    call assert_equal(['    '],
2657          \ getregion(getpos('v'), getpos('.'), {'type': 'v' }))
2658    call assert_equal([
2659          \   [[bufnr('%'), 1, 1, 6], [bufnr('%'), 1, 1, 0]],
2660          \ ],
2661          \ getregionpos(getpos('v'), getpos('.'), {'type': 'v' }))
2662    call assert_equal([
2663          \   [[bufnr('%'), 1, 1, 6], [bufnr('%'), 1, 2, 2]],
2664          \ ],
2665          \ getregionpos(getpos('v'), getpos('.'),
2666          \              {'type': 'v', "eol": v:true }))
2667
2668    set virtualedit&
2669    bwipe!
2670  END
2671  call CheckLegacyAndVim9Success(lines)
2672endfunc
2673
2674" Test the "bounds" option of getregionpos(), which returns only the outer
2675" endpoints of the region as a single pair.
2676func Test_visual_getregionpos_bounds()
2677  new
2678  call setline(1, [
2679        \ 'one two',
2680        \ '',
2681        \ "a\tb\tc",
2682        \ 'ábĉdé',
2683        \ "e\u0301x\u0301",
2684        \ "\U0001f1e6\u00ab\U0001f1e7\u00ab\U0001f1e8",
2685        \ 'last line',
2686        \ ])
2687  let b = bufnr('%')
2688
2689  " The pair is the start on the first line and the end on the last line.
2690  call assert_equal([[[b, 1, 5, 0], [b, 3, 3, 0]]],
2691        \ getregionpos([0, 1, 5, 0], [0, 3, 3, 0], #{bounds: v:true}))
2692  call assert_equal([[[b, 1, 1, 0], [b, 3, 5, 0]]],
2693        \ getregionpos([0, 1, 5, 0], [0, 3, 3, 0],
2694        \              #{type: 'V', bounds: v:true}))
2695
2696  " For a blockwise region that is a diagonal of the block, not its corners.
2697  call assert_equal([[[b, 1, 3, 0], [b, 3, 5, 0]]],
2698        \ getregionpos([0, 1, 3, 0], [0, 3, 5, 0],
2699        \              #{type: "\<C-v>", bounds: v:true}))
2700  call assert_equal([[[b, 1, 3, 0], [b, 3, 2, 5]]],
2701        \ getregionpos([0, 1, 3, 0], [0, 3, 5, 0],
2702        \              #{type: "\<C-v>4", bounds: v:true}))
2703
2704  " When the first line is empty and "eol" is false its "col" is 0, while the
2705  " "col" of the end position is not.
2706  call assert_equal([[[b, 2, 0, 0], [b, 4, 5, 0]]],
2707        \ getregionpos([0, 2, 1, 0], [0, 4, 5, 0], #{bounds: v:true}))
2708  call assert_equal([[[b, 2, 1, 0], [b, 4, 5, 0]]],
2709        \ getregionpos([0, 2, 1, 0], [0, 4, 5, 0],
2710        \              #{eol: v:true, bounds: v:true}))
2711
2712  " Region within one line, also with the positions reversed.
2713  call assert_equal([[[b, 4, 3, 0], [b, 4, 8, 0]]],
2714        \ getregionpos([0, 4, 3, 0], [0, 4, 7, 0], #{bounds: v:true}))
2715  call assert_equal([[[b, 4, 3, 0], [b, 4, 8, 0]]],
2716        \ getregionpos([0, 4, 7, 0], [0, 4, 3, 0], #{bounds: v:true}))
2717
2718  " Combining characters and v:maxcol.
2719  call assert_equal([[[b, 5, 1, 0], [b, 6, 16, 0]]],
2720        \ getregionpos([0, 5, 1, 0], [0, 6, v:maxcol, 0], #{bounds: v:true}))
2721
2722  " The pair is always what indexing the full result gives.
2723  for ve in ['', 'all', 'block', 'onemore']
2724    let &virtualedit = ve
2725    for sel in ['inclusive', 'exclusive']
2726      let &selection = sel
2727      for type in ['v', 'V', "\<C-v>", "\<C-v>3"]
2728        for excl in [#{}, #{exclusive: v:false}, #{exclusive: v:true}]
2729          for eol in [v:false, v:true]
2730            let opts = extend(#{type: type, eol: eol}, excl)
2731            for p1 in [[0, 1, 3, 0], [0, 2, 1, 0], [0, 3, 4, 0], [0, 5, 1, 0]]
2732              for p2 in [[0, 3, 2, 0], [0, 4, 6, 0],
2733                    \    [0, 6, v:maxcol, 0], [0, 7, 10, 0]]
2734                for args in [[p1, p2], [p2, p1]]
2735                  let full = getregionpos(args[0], args[1], opts)
2736                  call assert_equal([[full[0][0], full[-1][1]]],
2737                        \ getregionpos(args[0], args[1],
2738                        \              extend(copy(opts), #{bounds: v:true})),
2739                        \ printf('%s ve=%s sel=%s %s',
2740                        \        string(args), ve, sel, string(opts)))
2741                endfor
2742              endfor
2743            endfor
2744          endfor
2745        endfor
2746      endfor
2747    endfor
2748  endfor
2749  set virtualedit& selection&
2750
2751  " "bounds" does not change the errors for invalid arguments.
2752  call assert_fails('call getregionpos(1, 2, #{bounds: v:true})', 'E1211:')
2753  call assert_fails('call getregionpos([0, 1, 1, 0], [0, 1, 1, 0],
2754        \ #{type: "", bounds: v:true})', 'E475:')
2755  call assert_fails('call getregionpos([0, 0, 1, 0], [0, 1, 1, 0],
2756        \ #{bounds: v:true})', 'E966:')
2757  call assert_fails('call getregionpos([0, 1, 0, 0], [0, 1, 1, 0],
2758        \ #{bounds: v:true})', 'E964:')
2759
2760  " Positions in two different buffers still give an empty list, not a pair.
2761  new
2762  call setline(1, ['other buffer'])
2763  let other = bufnr('%')
2764  normal! mA
2765  wincmd p
2766  call assert_equal([],
2767        \ getregionpos(getpos('.'), getpos("'A"), #{bounds: v:true}))
2768  call assert_equal([],
2769        \ getregionpos(getpos("'A"), getpos('.'), #{bounds: v:true}))
2770
2771  exe other .. 'bwipe!'
2772  bwipe!
2773endfunc
2774
2775func Test_getregion_invalid_buf()
2776  new
2777  help index
2778  call cursor(7, 6)
2779  norm! mA
2780  call cursor(7, 18)
2781  norm! mB
2782  call assert_equal(['file contains'], getregion(getpos("'A"), getpos("'B")))
2783  " close the help window
2784  q
2785  call assert_fails("call getregion(getpos(\"'A\"), getpos(\"'B\"))", 'E681:')
2786  bwipe!
2787endfunc
2788
2789func Test_getregion_after_yank()
2790  func! Check_Results(type)
2791    call assert_equal(g:expected_region,
2792          \ getregion(getpos("'["), getpos("']"), #{ type: a:type }))
2793    call assert_equal(g:expected_regionpos,
2794          \ getregionpos(getpos("'["), getpos("']"), #{ type: a:type }))
2795    call assert_equal(g:expected_region,
2796          \ getregion(getpos("']"), getpos("'["), #{ type: a:type }))
2797    call assert_equal(g:expected_regionpos,
2798          \ getregionpos(getpos("']"), getpos("'["), #{ type: a:type }))
2799    let g:checked = 1
2800  endfunc
2801
2802  autocmd TextYankPost *
2803        \ : if v:event.operator ==? 'y'
2804        \ | call Check_Results(v:event.regtype)
2805        \ | endif
2806
2807  new
2808  call setline(1, ['abcd', 'efghijk', 'lmn'])
2809
2810  let g:expected_region = ['abcd']
2811  let g:expected_regionpos = [
2812        \   [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 4, 0]],
2813        \ ]
2814  let g:checked = 0
2815  normal yy
2816  call assert_equal(1, g:checked)
2817  call Check_Results(getregtype('"'))
2818
2819  let g:expected_region = ['cd', 'ghijk', 'n']
2820  let g:expected_regionpos = [
2821        \   [[bufnr('%'), 1, 3, 0], [bufnr('%'), 1, 4, 0]],
2822        \   [[bufnr('%'), 2, 3, 0], [bufnr('%'), 2, 7, 0]],
2823        \   [[bufnr('%'), 3, 3, 0], [bufnr('%'), 3, 3, 0]],
2824        \ ]
2825  let g:checked = 0
2826  call feedkeys("gg0ll\<C-V>jj$y", 'tx')
2827  call assert_equal(1, g:checked)
2828  call Check_Results(getregtype('"'))
2829  call assert_equal(g:expected_region, getreg('"', v:true, v:true))
2830
2831  let g:expected_region = ['bc', 'fg', 'mn']
2832  let g:expected_regionpos = [
2833        \   [[bufnr('%'), 1, 2, 0], [bufnr('%'), 1, 3, 0]],
2834        \   [[bufnr('%'), 2, 2, 0], [bufnr('%'), 2, 3, 0]],
2835        \   [[bufnr('%'), 3, 2, 0], [bufnr('%'), 3, 3, 0]],
2836        \ ]
2837  let g:checked = 0
2838  call feedkeys("gg0l\<C-V>jjly", 'tx')
2839  call assert_equal(1, g:checked)
2840  call Check_Results(getregtype('"'))
2841  call assert_equal(g:expected_region, getreg('"', v:true, v:true))
2842
2843  bwipe!
2844
2845  new
2846  let lines = ['asdfghjkl', '«口=口»', 'qwertyuiop', '口口=口口', 'zxcvbnm']
2847  call setline(1, lines)
2848
2849  let g:expected_region = lines
2850  let g:expected_regionpos = [
2851        \   [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 9, 0]],
2852        \   [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 11, 0]],
2853        \   [[bufnr('%'), 3, 1, 0], [bufnr('%'), 3, 10, 0]],
2854        \   [[bufnr('%'), 4, 1, 0], [bufnr('%'), 4, 13, 0]],
2855        \   [[bufnr('%'), 5, 1, 0], [bufnr('%'), 5, 7, 0]],
2856        \ ]
2857  let g:checked = 0
2858  call feedkeys('ggyG', 'tx')
2859  call assert_equal(1, g:checked)
2860  call Check_Results(getregtype('"'))
2861  call assert_equal(g:expected_region, getreg('"', v:true, v:true))
2862
2863  let g:expected_region = ['=口»', 'qwertyuiop', '口口=口']
2864  let g:expected_regionpos = [
2865        \   [[bufnr('%'), 2, 6, 0], [bufnr('%'), 2, 11, 0]],
2866        \   [[bufnr('%'), 3, 1, 0], [bufnr('%'), 3, 10, 0]],
2867        \   [[bufnr('%'), 4, 1, 0], [bufnr('%'), 4, 10, 0]],
2868        \ ]
2869  let g:checked = 0
2870  call feedkeys('2gg02lv2j2ly', 'tx')
2871  call assert_equal(1, g:checked)
2872  call Check_Results(getregtype('"'))
2873  call assert_equal(g:expected_region, getreg('"', v:true, v:true))
2874
2875  let g:expected_region = ['asdf', '«口=', 'qwer', '口口', 'zxcv']
2876  let g:expected_regionpos = [
2877        \   [[bufnr('%'), 1, 1, 0], [bufnr('%'), 1, 4, 0]],
2878        \   [[bufnr('%'), 2, 1, 0], [bufnr('%'), 2, 6, 0]],
2879        \   [[bufnr('%'), 3, 1, 0], [bufnr('%'), 3, 4, 0]],
2880        \   [[bufnr('%'), 4, 1, 0], [bufnr('%'), 4, 6, 0]],
2881        \   [[bufnr('%'), 5, 1, 0], [bufnr('%'), 5, 4, 0]],
2882        \ ]
2883  let g:checked = 0
2884  call feedkeys("G0\<C-V>3l4ky", 'tx')
2885  call assert_equal(1, g:checked)
2886  call Check_Results(getregtype('"'))
2887  call assert_equal(g:expected_region, getreg('"', v:true, v:true))
2888
2889  let g:expected_region = ['ghjkl', '口»', 'tyuiop', '=口口', 'bnm']
2890  let g:expected_regionpos = [
2891        \   [[bufnr('%'), 1, 5, 0], [bufnr('%'), 1, 9, 0]],
2892        \   [[bufnr('%'), 2, 7, 0], [bufnr('%'), 2, 11, 0]],
2893        \   [[bufnr('%'), 3, 5, 0], [bufnr('%'), 3, 10, 0]],
2894        \   [[bufnr('%'), 4, 7, 0], [bufnr('%'), 4, 13, 0]],
2895        \   [[bufnr('%'), 5, 5, 0], [bufnr('%'), 5, 7, 0]],
2896        \ ]
2897  let g:checked = 0
2898  call feedkeys("G04l\<C-V>$4ky", 'tx')
2899  call assert_equal(1, g:checked)
2900  call Check_Results(getregtype('"'))
2901  call assert_equal(g:expected_region, getreg('"', v:true, v:true))
2902
2903  bwipe!
2904
2905  unlet g:expected_region
2906  unlet g:expected_regionpos
2907  unlet g:checked
2908  autocmd! TextYankPost
2909  delfunc Check_Results
2910endfunc
2911
2912func Test_visual_block_cursor_delete()
2913  new
2914  call setline(1, 'ab')
2915  exe ":norm! $\<c-v>hI\<Del>\<ESC>"
2916  call assert_equal(['b'], getline(1, 1))
2917  bwipe!
2918endfunc
2919
2920func Test_visual_block_cursor_insert_enter()
2921  new
2922  call setline(1, ['asdf asdf', 'asdf asdf', 'asdf asdf', 'asdf asdf'])
2923  call cursor(1, 5)
2924  exe ":norm! \<c-v>3jcw\<cr>"
2925  call assert_equal(['asdfw', 'asdf', 'asdfasdf', 'asdfasdf', 'asdfasdf'], getline(1, '$'))
2926  bwipe!
2927endfunc
2928
2929func Test_visual_block_exclusive_selection()
2930  new
2931  set selection=exclusive
2932  call setline(1, ['asöd asdf', 'asdf asdf', 'as€d asdf', 'asdf asdf'])
2933  call cursor(1, 1)
2934  exe ":norm! \<c-v>eh3j~"
2935  call assert_equal(['ASÖd asdf', 'ASDf asdf', 'AS€d asdf', 'ASDf asdf'], getline(1, '$'))
2936  exe ":norm! 1v~"
2937  call assert_equal(['asöd asdf', 'asdf asdf', 'as€d asdf', 'asdf asdf'], getline(1, '$'))
2938  bwipe!
2939  set selection&vim
2940endfunc
2941
2942func Test_visual_block_exclusive_selection_adjusted()
2943  new
2944  " Test that the end-position of the visual selection is adjusted for exclusive selection
2945  set selection=exclusive
2946  call setline(1, ['asöd asdf  ', 'asdf asdf  ', 'as€d asdf  ', 'asdf asdf  '])
2947  call cursor(1, 1)
2948  " inclusive motion
2949  exe ":norm! \<c-v>e3jy"
2950  call assert_equal([0, 4, 5, 0], getpos("'>"))
2951  " exclusive motion
2952  exe ":norm! \<c-v>ta3jy"
2953  call assert_equal([0, 4, 6, 0], getpos("'>"))
2954  " another inclusive motion
2955  exe ":norm! \<c-v>g_3jy"
2956  call assert_equal([0, 4, 10, 0], getpos("'>"))
2957
2958  " Reset selection option to Vim default
2959  set selection&vim
2960  call cursor(1, 1)
2961
2962  " inclusive motion
2963  exe ":norm! \<c-v>e3jy"
2964  call assert_equal([0, 4, 4, 0], getpos("'>"))
2965  " exclusive motion
2966  exe ":norm! \<c-v>ta3jy"
2967  call assert_equal([0, 4, 5, 0], getpos("'>"))
2968  " another inclusive motion
2969  exe ":norm! \<c-v>g_3jy"
2970  call assert_equal([0, 4, 9, 0], getpos("'>"))
2971  bwipe!
2972  set selection&vim
2973endfunc
2974
2975" the following caused a Heap-Overflow, because Vim was accessing outside of a
2976" line end
2977func Test_visual_pos_buffer_heap_overflow()
2978  set virtualedit=all
2979  args Xa Xb
2980  all
2981  call setline(1, ['', '', ''])
2982  call cursor(3, 1)
2983  wincmd w
2984  call setline(1, 'foobar')
2985  normal! $lv0
2986  all
2987  call setreg('"', 'baz')
2988  normal! [P
2989  set virtualedit=
2990  bw! Xa Xb
2991endfunc
2992
2993" Test visual block pos update after block insert and gv
2994func Test_visual_block_pos_update()
2995  new
2996  set virtualedit=block
2997  call setline(1, ['aacccc', 'bb'])
2998  exe "norm! e\<C-v>jAa\<Esc>gv"
2999  call assert_equal([[0, 1, 6, 0], [0 , 2, 6, 0]], [getpos("v"), getpos(".")])
3000  normal! kj
3001  call assert_equal([[0, 1, 6, 0], [0 , 2, 6, 0]], [getpos("v"), getpos(".")])
3002  set virtualedit=
3003  bw!
3004endfunc
3005
3006" Test that blockwise end position matches getpos('.')
3007" when 'wrap' and 'linebreak' are set
3008func Test_getregionpos_block_linebreak_matches_getpos()
3009  CheckFeature linebreak
3010
3011  new
3012  setlocal buftype=
3013  setlocal bufhidden=wipe
3014  setlocal noswapfile
3015
3016  setlocal wrap
3017  setlocal linebreak
3018  setlocal breakat=\ \t
3019  setlocal nonumber norelativenumber
3020  setlocal signcolumn=no
3021  setlocal foldcolumn=0
3022
3023  call setline(1, '1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888')
3024
3025  " Force wrapping deterministically by shrinking the screen width.
3026  let save_columns = &columns
3027  let moved = 0
3028  for c in [30, 20, 15, 10]
3029    execute 'set columns=' .. c
3030    redraw!
3031    normal! gg0
3032    let row0 = winline()
3033    normal! gj
3034    let row1 = winline()
3035    if row1 > row0
3036      let moved = 1
3037      break
3038    endif
3039  endfor
3040  call assert_true(moved)
3041
3042  " Move a bit right so we are not at column 1, then go back up one screen line.
3043  normal! 5l
3044  normal! gk
3045  let row2 = winline()
3046  call assert_equal(row0, row2)
3047
3048  " Start Visual block and move down one screen line to the previous position.
3049  execute "normal! \<C-V>"
3050  normal! gj
3051  let row3 = winline()
3052  call assert_equal(row1, row3)
3053
3054  let p1 = getpos('v')
3055  let p2 = getpos('.')
3056
3057  " Sanity: block selection is within the same wrapped buffer line.
3058  call assert_equal(1, p1[1])
3059  call assert_equal(1, p2[1])
3060
3061  " For blockwise region, getregionpos() should not report an end position
3062  " different from the {pos2} we passed in.
3063  let segs = getregionpos(p1, p2, #{ type: "\<C-V>", exclusive: v:false })
3064
3065  call assert_equal(1, len(segs))
3066  let endp = segs[0][1]
3067
3068  call assert_equal(p2[1], endp[1])  " lnum
3069  call assert_equal(p2[2], endp[2])  " col
3070  call assert_equal(p2[3], endp[3])  " off
3071
3072  let &columns = save_columns
3073  bw!
3074endfunc
3075
3076func Test_visual_ended_in_wiped_buffer()
3077  edit Xfoo
3078  edit Xbar
3079  setlocal bufhidden=wipe
3080  augroup testing
3081    autocmd BufWipeout * ++once normal! v
3082  augroup END
3083  " Must be the last window.
3084  call assert_equal(1, winnr('$'))
3085  call assert_equal(1, tabpagenr('$'))
3086  " Was a member access on a NULL curbuf from Vim ending Visual mode.
3087  buffer #
3088  call assert_equal(0, bufexists('Xbar'))
3089  call assert_equal('n', mode())
3090
3091  autocmd! testing
3092  %bw!
3093endfunc
3094
3095func Test_visual_ended_in_unloaded_buffer()
3096  throw 'Skipped: needs clipboard=autoselect'
3097  CheckFeature clipboard
3098  CheckNotGui
3099  set clipboard+=autoselect
3100  edit Xfoo
3101  edit Xbar
3102  call setline(1, 'hi')
3103  setlocal nomodified
3104  let s:fired = 0
3105  augroup testing
3106    autocmd BufUnload Xbar call assert_equal('Xbar', bufname())
3107          \| execute 'normal! V'
3108          \| call assert_equal('V', mode())
3109
3110    " From Vim ending Visual mode.  Used to occur too late, after the buffer was
3111    " unloaded, so @* didn't contain the selection.  Window also had a NULL
3112    " w_buffer here!
3113    autocmd TextYankPost * ++once let s:fired = 1
3114          \| if has('clipboard_working') | call assert_equal("hi\n", @*) | endif
3115          \| call tabpagebuflist() " was a NULL member access on w_buffer
3116  augroup END
3117
3118  buffer Xfoo
3119  call assert_equal(0, bufloaded('Xbar'))
3120  call assert_equal('n', mode())
3121  call assert_equal(1, s:fired)
3122
3123  augroup testing
3124    autocmd!
3125    autocmd BufHidden Xfoo ++once call assert_equal('Xfoo', bufname())
3126          \| execute 'normal! v'
3127          \| call assert_equal('v', mode())
3128
3129    " Check b_nwindows is not decremented too early when Visual mode ends in a
3130    " loaded buffer.  Buffer should not be considered hidden in TextYankPost, as
3131    " by that point it's still loaded and displayed in the current window.
3132    autocmd TextYankPost * ++once let s:fired = 2
3133          \| call assert_equal(1, bufloaded(bufnr()))
3134          \| call assert_equal(0, getbufinfo(bufnr())[0].hidden)
3135  augroup END
3136  edit Xbar
3137  edit Xfoo
3138  only
3139  hide buffer #
3140  call assert_equal('n', mode())
3141  call assert_equal(2, s:fired)
3142
3143  autocmd! testing
3144  unlet! s:fired
3145  set clipboard&
3146  %bw!
3147endfunc
3148
3149" vim: shiftwidth=2 sts=2 expandtab