xref: /vim-8.2.3635/src/testdir/test_visual.vim (revision afde13b6)
1" Tests for various Visual mode.
2
3func Test_block_shift_multibyte()
4  " Uses double-wide character.
5  split
6  call setline(1, ['xヹxxx', 'ヹxxx'])
7  exe "normal 1G0l\<C-V>jl>"
8  call assert_equal('x	 ヹxxx', getline(1))
9  call assert_equal('	ヹxxx', getline(2))
10  q!
11endfunc
12
13func Test_block_shift_overflow()
14  " This used to cause a multiplication overflow followed by a crash.
15  new
16  normal ii
17  exe "normal \<C-V>876543210>"
18  q!
19endfunc
20
21func Test_dotregister_paste()
22  new
23  exe "norm! ihello world\<esc>"
24  norm! 0ve".p
25  call assert_equal('hello world world', getline(1))
26  q!
27endfunc
28
29func Test_Visual_ctrl_o()
30  new
31  call setline(1, ['one', 'two', 'three'])
32  call cursor(1,2)
33  set noshowmode
34  set tw=0
35  call feedkeys("\<c-v>jjlIa\<c-\>\<c-o>:set tw=88\<cr>\<esc>", 'tx')
36  call assert_equal(['oane', 'tawo', 'tahree'], getline(1, 3))
37  call assert_equal(88, &tw)
38  set tw&
39  bw!
40endfu
41
42func Test_Visual_vapo()
43  new
44  normal oxx
45  normal vapo
46  bwipe!
47endfunc
48
49func Test_Visual_inner_quote()
50  new
51  normal oxX
52  normal vki'
53  bwipe!
54endfunc
55
56" Test for Visual mode not being reset causing E315 error.
57func TriggerTheProblem()
58  " At this point there is no visual selection because :call reset it.
59  " Let's restore the selection:
60  normal gv
61  '<,'>del _
62  try
63      exe "normal \<Esc>"
64  catch /^Vim\%((\a\+)\)\=:E315/
65      echom 'Snap! E315 error!'
66      let g:msg = 'Snap! E315 error!'
67  endtry
68endfunc
69
70func Test_visual_mode_reset()
71  enew
72  let g:msg = "Everything's fine."
73  enew
74  setl buftype=nofile
75  call append(line('$'), 'Delete this line.')
76
77  " NOTE: this has to be done by a call to a function because executing :del
78  " the ex-way will require the colon operator which resets the visual mode
79  " thus preventing the problem:
80  exe "normal! GV:call TriggerTheProblem()\<CR>"
81  call assert_equal("Everything's fine.", g:msg)
82
83endfunc
84
85" Test for visual block shift and tab characters.
86func Test_block_shift_tab()
87  enew!
88  call append(0, repeat(['one two three'], 5))
89  call cursor(1,1)
90  exe "normal i\<C-G>u"
91  exe "normal fe\<C-V>4jR\<Esc>ugvr1"
92  call assert_equal('on1 two three', getline(1))
93  call assert_equal('on1 two three', getline(2))
94  call assert_equal('on1 two three', getline(5))
95
96  enew!
97  call append(0, repeat(['abcdefghijklmnopqrstuvwxyz'], 5))
98  call cursor(1,1)
99  exe "normal \<C-V>4jI    \<Esc>j<<11|D"
100  exe "normal j7|a\<Tab>\<Tab>"
101  exe "normal j7|a\<Tab>\<Tab>   "
102  exe "normal j7|a\<Tab>       \<Tab>\<Esc>4k13|\<C-V>4j<"
103  call assert_equal('    abcdefghijklmnopqrstuvwxyz', getline(1))
104  call assert_equal('abcdefghij', getline(2))
105  call assert_equal("    abc\<Tab>    defghijklmnopqrstuvwxyz", getline(3))
106  call assert_equal("    abc\<Tab>    defghijklmnopqrstuvwxyz", getline(4))
107  call assert_equal("    abc\<Tab>    defghijklmnopqrstuvwxyz", getline(5))
108
109  %s/\s\+//g
110  call cursor(1,1)
111  exe "normal \<C-V>4jI    \<Esc>j<<"
112  exe "normal j7|a\<Tab>\<Tab>"
113  exe "normal j7|a\<Tab>\<Tab>\<Tab>\<Tab>\<Tab>"
114  exe "normal j7|a\<Tab>       \<Tab>\<Tab>\<Esc>4k13|\<C-V>4j3<"
115  call assert_equal('    abcdefghijklmnopqrstuvwxyz', getline(1))
116  call assert_equal('abcdefghij', getline(2))
117  call assert_equal("    abc\<Tab>    defghijklmnopqrstuvwxyz", getline(3))
118  call assert_equal("    abc\<Tab>\<Tab>defghijklmnopqrstuvwxyz", getline(4))
119  call assert_equal("    abc\<Tab>    defghijklmnopqrstuvwxyz", getline(5))
120
121  enew!
122endfunc
123
124" Tests Blockwise Visual when there are TABs before the text.
125func Test_blockwise_visual()
126  enew!
127  call append(0, ['123456',
128	      \ '234567',
129	      \ '345678',
130	      \ '',
131	      \ 'test text test tex start here',
132	      \ "\t\tsome text",
133	      \ "\t\ttest text",
134	      \ 'test text'])
135  call cursor(1,1)
136  exe "normal /start here$\<CR>"
137  exe 'normal "by$' . "\<C-V>jjlld"
138  exe "normal /456$\<CR>"
139  exe "normal \<C-V>jj" . '"bP'
140  call assert_equal(['123start here56',
141	      \ '234start here67',
142	      \ '345start here78',
143	      \ '',
144	      \ 'test text test tex rt here',
145	      \ "\t\tsomext",
146	      \ "\t\ttesext"], getline(1, 7))
147
148  enew!
149endfunc
150
151" Test swapping corners in blockwise visual mode with o and O
152func Test_blockwise_visual_o_O()
153  enew!
154
155  exe "norm! 10i.\<Esc>Y4P3lj\<C-V>4l2jr "
156  exe "norm! gvO\<Esc>ra"
157  exe "norm! gvO\<Esc>rb"
158  exe "norm! gvo\<C-c>rc"
159  exe "norm! gvO\<C-c>rd"
160
161  call assert_equal(['..........',
162        \            '...c   d..',
163        \            '...     ..',
164        \            '...a   b..',
165        \            '..........'], getline(1, '$'))
166
167  enew!
168endfun
169
170" Test Virtual replace mode.
171func Test_virtual_replace()
172  if exists('&t_kD')
173    let save_t_kD = &t_kD
174  endif
175  if exists('&t_kb')
176    let save_t_kb = &t_kb
177  endif
178  exe "set t_kD=\<C-V>x7f t_kb=\<C-V>x08"
179  enew!
180  exe "normal a\nabcdefghi\njk\tlmn\n    opq	rst\n\<C-D>uvwxyz"
181  call cursor(1,1)
182  set ai bs=2
183  exe "normal gR0\<C-D> 1\nA\nBCDEFGHIJ\n\tKL\nMNO\nPQR"
184  call assert_equal([' 1',
185	      \ ' A',
186	      \ ' BCDEFGHIJ',
187	      \ ' 	KL',
188	      \ '	MNO',
189	      \ '	PQR',
190	      \ ], getline(1, 6))
191  normal G
192  mark a
193  exe "normal o0\<C-D>\nabcdefghi\njk\tlmn\n    opq\trst\n\<C-D>uvwxyz\n"
194  exe "normal 'ajgR0\<C-D> 1\nA\nBCDEFGHIJ\n\tKL\nMNO\nPQR" . repeat("\<BS>", 29)
195  call assert_equal([' 1',
196	      \ 'abcdefghi',
197	      \ 'jk	lmn',
198	      \ '    opq	rst',
199	      \ 'uvwxyz'], getline(7, 11))
200  normal G
201  exe "normal iab\tcdefghi\tjkl"
202  exe "normal 0gRAB......CDEFGHI.J\<Esc>o"
203  exe "normal iabcdefghijklmnopqrst\<Esc>0gRAB\tIJKLMNO\tQR"
204  call assert_equal(['AB......CDEFGHI.Jkl',
205	      \ 'AB	IJKLMNO	QRst'], getline(12, 13))
206  enew!
207  set noai bs&vim
208  if exists('save_t_kD')
209    let &t_kD = save_t_kD
210  endif
211  if exists('save_t_kb')
212    let &t_kb = save_t_kb
213  endif
214endfunc
215
216" Test Virtual replace mode.
217func Test_virtual_replace2()
218  enew!
219  set bs=2
220  exe "normal a\nabcdefghi\njk\tlmn\n    opq	rst\n\<C-D>uvwxyz"
221  call cursor(1,1)
222  " Test 1: Test that del deletes the newline
223  exe "normal gR0\<del> 1\nA\nBCDEFGHIJ\n\tKL\nMNO\nPQR"
224  call assert_equal(['0 1',
225	      \ 'A',
226	      \ 'BCDEFGHIJ',
227	      \ '	KL',
228	      \ 'MNO',
229	      \ 'PQR',
230	      \ ], getline(1, 6))
231  " Test 2:
232  " a newline is not deleted, if no newline has been added in virtual replace mode
233  %d_
234  call setline(1, ['abcd', 'efgh', 'ijkl'])
235  call cursor(2,1)
236  exe "norm! gR1234\<cr>5\<bs>\<bs>\<bs>"
237  call assert_equal(['abcd',
238        \ '123h',
239        \ 'ijkl'], getline(1, '$'))
240  " Test 3:
241  " a newline is deleted, if a newline has been inserted before in virtual replace mode
242  %d_
243  call setline(1, ['abcd', 'efgh', 'ijkl'])
244  call cursor(2,1)
245  exe "norm! gR1234\<cr>\<cr>56\<bs>\<bs>\<bs>"
246  call assert_equal(['abcd',
247        \ '1234',
248        \ 'ijkl'], getline(1, '$'))
249  " Test 4:
250  " delete add a newline, delete it, add it again and check undo
251  %d_
252  call setline(1, ['abcd', 'efgh', 'ijkl'])
253  call cursor(2,1)
254  " break undo sequence explicitly
255  let &ul = &ul
256  exe "norm! gR1234\<cr>\<bs>\<del>56\<cr>"
257  let &ul = &ul
258  call assert_equal(['abcd',
259        \ '123456',
260        \ ''], getline(1, '$'))
261  norm! u
262  call assert_equal(['abcd',
263        \ 'efgh',
264        \ 'ijkl'], getline(1, '$'))
265  " clean up
266  %d_
267  set bs&vim
268endfunc
269
270func Test_Visual_word_textobject()
271  new
272  call setline(1, ['First sentence. Second sentence.'])
273
274  " When start and end of visual area are identical, 'aw' or 'iw' select
275  " the whole word.
276  norm! 1go2fcvawy
277  call assert_equal('Second ', @")
278  norm! 1go2fcviwy
279  call assert_equal('Second', @")
280
281  " When start and end of visual area are not identical, 'aw' or 'iw'
282  " extend the word in direction of the end of the visual area.
283  norm! 1go2fcvlawy
284  call assert_equal('cond ', @")
285  norm! gv2awy
286  call assert_equal('cond sentence.', @")
287
288  norm! 1go2fcvliwy
289  call assert_equal('cond', @")
290  norm! gv2iwy
291  call assert_equal('cond sentence', @")
292
293  " Extend visual area in opposite direction.
294  norm! 1go2fcvhawy
295  call assert_equal(' Sec', @")
296  norm! gv2awy
297  call assert_equal(' sentence. Sec', @")
298
299  norm! 1go2fcvhiwy
300  call assert_equal('Sec', @")
301  norm! gv2iwy
302  call assert_equal('. Sec', @")
303
304  bwipe!
305endfunc
306
307func Test_Visual_sentence_textobject()
308  new
309  call setline(1, ['First sentence. Second sentence. Third', 'sentence. Fourth sentence'])
310
311  " When start and end of visual area are identical, 'as' or 'is' select
312  " the whole sentence.
313  norm! 1gofdvasy
314  call assert_equal('Second sentence. ', @")
315  norm! 1gofdvisy
316  call assert_equal('Second sentence.', @")
317
318  " When start and end of visual area are not identical, 'as' or 'is'
319  " extend the sentence in direction of the end of the visual area.
320  norm! 1gofdvlasy
321  call assert_equal('d sentence. ', @")
322  norm! gvasy
323  call assert_equal("d sentence. Third\nsentence. ", @")
324
325  norm! 1gofdvlisy
326  call assert_equal('d sentence.', @")
327  norm! gvisy
328  call assert_equal('d sentence. ', @")
329  norm! gvisy
330  call assert_equal("d sentence. Third\nsentence.", @")
331
332  " Extend visual area in opposite direction.
333  norm! 1gofdvhasy
334  call assert_equal(' Second', @")
335  norm! gvasy
336  call assert_equal("First sentence. Second", @")
337
338  norm! 1gofdvhisy
339  call assert_equal('Second', @")
340  norm! gvisy
341  call assert_equal(' Second', @")
342  norm! gvisy
343  call assert_equal('First sentence. Second', @")
344
345  bwipe!
346endfunc
347
348func Test_Visual_paragraph_textobject()
349  new
350  call setline(1, ['First line.',
351  \                '',
352  \                'Second line.',
353  \                'Third line.',
354  \                'Fourth line.',
355  \                'Fifth line.',
356  \                '',
357  \                'Sixth line.'])
358
359  " When start and end of visual area are identical, 'ap' or 'ip' select
360  " the whole paragraph.
361  norm! 4ggvapy
362  call assert_equal("Second line.\nThird line.\nFourth line.\nFifth line.\n\n", @")
363  norm! 4ggvipy
364  call assert_equal("Second line.\nThird line.\nFourth line.\nFifth line.\n", @")
365
366  " When start and end of visual area are not identical, 'ap' or 'ip'
367  " extend the sentence in direction of the end of the visual area.
368  " FIXME: actually, it is not sufficient to have different start and
369  " end of visual selection, the start line and end line have to differ,
370  " which is not consistent with the documentation.
371  norm! 4ggVjapy
372  call assert_equal("Third line.\nFourth line.\nFifth line.\n\n", @")
373  norm! gvapy
374  call assert_equal("Third line.\nFourth line.\nFifth line.\n\nSixth line.\n", @")
375  norm! 4ggVjipy
376  call assert_equal("Third line.\nFourth line.\nFifth line.\n", @")
377  norm! gvipy
378  call assert_equal("Third line.\nFourth line.\nFifth line.\n\n", @")
379  norm! gvipy
380  call assert_equal("Third line.\nFourth line.\nFifth line.\n\nSixth line.\n", @")
381
382  " Extend visual area in opposite direction.
383  norm! 5ggVkapy
384  call assert_equal("\nSecond line.\nThird line.\nFourth line.\n", @")
385  norm! gvapy
386  call assert_equal("First line.\n\nSecond line.\nThird line.\nFourth line.\n", @")
387  norm! 5ggVkipy
388  call assert_equal("Second line.\nThird line.\nFourth line.\n", @")
389  norma gvipy
390  call assert_equal("\nSecond line.\nThird line.\nFourth line.\n", @")
391  norm! gvipy
392  call assert_equal("First line.\n\nSecond line.\nThird line.\nFourth line.\n", @")
393
394  bwipe!
395endfunc
396
397func Test_curswant_not_changed()
398  new
399  call setline(1, ['one', 'two'])
400  au InsertLeave * call getcurpos()
401  call feedkeys("gg0\<C-V>jI123 \<Esc>j", 'xt')
402  call assert_equal([0, 2, 1, 0, 1], getcurpos())
403
404  bwipe!
405  au! InsertLeave
406endfunc
407
408" Tests for "vaBiB", end could be wrong.
409func Test_Visual_Block()
410  new
411  a
412- Bug in "vPPPP" on this text:
413	{
414		cmd;
415		{
416			cmd;\t/* <-- Start cursor here */
417			{
418			}
419		}
420	}
421.
422  normal gg
423  call search('Start cursor here')
424  normal vaBiBD
425  call assert_equal(['- Bug in "vPPPP" on this text:',
426	      \ "\t{",
427	      \ "\t}"], getline(1, '$'))
428
429  close!
430endfunc
431