blob: cd039c09fcddbc768af905adef6f2ae53972e1e6 [file] [log] [blame]
Bram Moolenaar1f3e7d32019-12-06 20:43:36 +01001" Tests for various Visual modes.
Bram Moolenaarc3c766e2017-03-08 22:55:19 +01002
Bram Moolenaard1ad99b2020-10-04 16:16:54 +02003source shared.vim
4
Bram Moolenaar019b9c62016-03-05 17:26:00 +01005func Test_block_shift_multibyte()
Bram Moolenaarf8eb9c52017-01-02 17:31:24 +01006 " Uses double-wide character.
Bram Moolenaar019b9c62016-03-05 17:26:00 +01007 split
8 call setline(1, ['xヹxxx', 'ヹxxx'])
9 exe "normal 1G0l\<C-V>jl>"
10 call assert_equal('x ヹxxx', getline(1))
11 call assert_equal(' ヹxxx', getline(2))
12 q!
13endfunc
Bram Moolenaarf8eb9c52017-01-02 17:31:24 +010014
Bram Moolenaarbae5a172017-08-06 15:42:06 +020015func Test_block_shift_overflow()
16 " This used to cause a multiplication overflow followed by a crash.
17 new
18 normal ii
19 exe "normal \<C-V>876543210>"
20 q!
21endfunc
22
Bram Moolenaarf8eb9c52017-01-02 17:31:24 +010023func Test_dotregister_paste()
24 new
25 exe "norm! ihello world\<esc>"
26 norm! 0ve".p
27 call assert_equal('hello world world', getline(1))
28 q!
29endfunc
Bram Moolenaar23fa81d2017-02-01 21:50:21 +010030
31func Test_Visual_ctrl_o()
32 new
33 call setline(1, ['one', 'two', 'three'])
34 call cursor(1,2)
35 set noshowmode
36 set tw=0
37 call feedkeys("\<c-v>jjlIa\<c-\>\<c-o>:set tw=88\<cr>\<esc>", 'tx')
38 call assert_equal(['oane', 'tawo', 'tahree'], getline(1, 3))
39 call assert_equal(88, &tw)
40 set tw&
41 bw!
42endfu
Bram Moolenaar84b2a382017-02-17 11:40:00 +010043
44func Test_Visual_vapo()
45 new
46 normal oxx
47 normal vapo
48 bwipe!
49endfunc
Bram Moolenaar46522af2017-02-18 23:12:01 +010050
51func Test_Visual_inner_quote()
52 new
53 normal oxX
54 normal vki'
55 bwipe!
56endfunc
Bram Moolenaar75373f32017-08-07 22:02:30 +020057
58" Test for Visual mode not being reset causing E315 error.
59func TriggerTheProblem()
60 " At this point there is no visual selection because :call reset it.
61 " Let's restore the selection:
62 normal gv
63 '<,'>del _
64 try
65 exe "normal \<Esc>"
66 catch /^Vim\%((\a\+)\)\=:E315/
67 echom 'Snap! E315 error!'
Bram Moolenaar63e82db2018-03-06 12:10:48 +010068 let g:msg = 'Snap! E315 error!'
Bram Moolenaar75373f32017-08-07 22:02:30 +020069 endtry
70endfunc
71
72func Test_visual_mode_reset()
Bram Moolenaar75373f32017-08-07 22:02:30 +020073 enew
Bram Moolenaar63e82db2018-03-06 12:10:48 +010074 let g:msg = "Everything's fine."
Bram Moolenaar75373f32017-08-07 22:02:30 +020075 enew
76 setl buftype=nofile
77 call append(line('$'), 'Delete this line.')
78
79 " NOTE: this has to be done by a call to a function because executing :del
80 " the ex-way will require the colon operator which resets the visual mode
81 " thus preventing the problem:
82 exe "normal! GV:call TriggerTheProblem()\<CR>"
83 call assert_equal("Everything's fine.", g:msg)
Bram Moolenaar75373f32017-08-07 22:02:30 +020084endfunc
Bram Moolenaar67418d92017-10-15 22:07:39 +020085
Bram Moolenaar15993ce2017-10-26 20:21:44 +020086" Test for visual block shift and tab characters.
87func Test_block_shift_tab()
Bram Moolenaar3e72dca2021-05-29 16:30:12 +020088 new
Bram Moolenaar15993ce2017-10-26 20:21:44 +020089 call append(0, repeat(['one two three'], 5))
90 call cursor(1,1)
91 exe "normal i\<C-G>u"
92 exe "normal fe\<C-V>4jR\<Esc>ugvr1"
93 call assert_equal('on1 two three', getline(1))
94 call assert_equal('on1 two three', getline(2))
95 call assert_equal('on1 two three', getline(5))
96
Bram Moolenaar3e72dca2021-05-29 16:30:12 +020097 %d _
Bram Moolenaar15993ce2017-10-26 20:21:44 +020098 call append(0, repeat(['abcdefghijklmnopqrstuvwxyz'], 5))
99 call cursor(1,1)
100 exe "normal \<C-V>4jI \<Esc>j<<11|D"
101 exe "normal j7|a\<Tab>\<Tab>"
102 exe "normal j7|a\<Tab>\<Tab> "
103 exe "normal j7|a\<Tab> \<Tab>\<Esc>4k13|\<C-V>4j<"
104 call assert_equal(' abcdefghijklmnopqrstuvwxyz', getline(1))
105 call assert_equal('abcdefghij', getline(2))
106 call assert_equal(" abc\<Tab> defghijklmnopqrstuvwxyz", getline(3))
107 call assert_equal(" abc\<Tab> defghijklmnopqrstuvwxyz", getline(4))
108 call assert_equal(" abc\<Tab> defghijklmnopqrstuvwxyz", getline(5))
109
110 %s/\s\+//g
111 call cursor(1,1)
112 exe "normal \<C-V>4jI \<Esc>j<<"
113 exe "normal j7|a\<Tab>\<Tab>"
114 exe "normal j7|a\<Tab>\<Tab>\<Tab>\<Tab>\<Tab>"
115 exe "normal j7|a\<Tab> \<Tab>\<Tab>\<Esc>4k13|\<C-V>4j3<"
116 call assert_equal(' abcdefghijklmnopqrstuvwxyz', getline(1))
117 call assert_equal('abcdefghij', getline(2))
118 call assert_equal(" abc\<Tab> defghijklmnopqrstuvwxyz", getline(3))
119 call assert_equal(" abc\<Tab>\<Tab>defghijklmnopqrstuvwxyz", getline(4))
120 call assert_equal(" abc\<Tab> defghijklmnopqrstuvwxyz", getline(5))
121
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200122 " Test for block shift with space characters at the beginning and with
123 " 'noexpandtab' and 'expandtab'
124 %d _
125 call setline(1, [" 1", " 2", " 3"])
126 setlocal shiftwidth=2 noexpandtab
127 exe "normal gg\<C-V>3j>"
128 call assert_equal(["\t1", "\t2", "\t3"], getline(1, '$'))
129 %d _
130 call setline(1, [" 1", " 2", " 3"])
131 setlocal shiftwidth=2 expandtab
132 exe "normal gg\<C-V>3j>"
133 call assert_equal([" 1", " 2", " 3"], getline(1, '$'))
134 setlocal shiftwidth&
135
136 bw!
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200137endfunc
138
139" Tests Blockwise Visual when there are TABs before the text.
140func Test_blockwise_visual()
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200141 new
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200142 call append(0, ['123456',
143 \ '234567',
144 \ '345678',
145 \ '',
146 \ 'test text test tex start here',
147 \ "\t\tsome text",
148 \ "\t\ttest text",
149 \ 'test text'])
150 call cursor(1,1)
151 exe "normal /start here$\<CR>"
152 exe 'normal "by$' . "\<C-V>jjlld"
153 exe "normal /456$\<CR>"
154 exe "normal \<C-V>jj" . '"bP'
155 call assert_equal(['123start here56',
156 \ '234start here67',
157 \ '345start here78',
158 \ '',
159 \ 'test text test tex rt here',
160 \ "\t\tsomext",
161 \ "\t\ttesext"], getline(1, 7))
162
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200163 bw!
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200164endfunc
165
Bram Moolenaar2e949762018-05-20 14:06:38 +0200166" Test swapping corners in blockwise visual mode with o and O
167func Test_blockwise_visual_o_O()
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200168 new
Bram Moolenaar2e949762018-05-20 14:06:38 +0200169
170 exe "norm! 10i.\<Esc>Y4P3lj\<C-V>4l2jr "
171 exe "norm! gvO\<Esc>ra"
172 exe "norm! gvO\<Esc>rb"
173 exe "norm! gvo\<C-c>rc"
174 exe "norm! gvO\<C-c>rd"
Bram Moolenaar1671f442020-03-10 07:48:13 +0100175 set selection=exclusive
176 exe "norm! gvOo\<C-c>re"
177 call assert_equal('...a be.', getline(4))
178 exe "norm! gvOO\<C-c>rf"
179 set selection&
Bram Moolenaar2e949762018-05-20 14:06:38 +0200180
181 call assert_equal(['..........',
182 \ '...c d..',
183 \ '... ..',
Bram Moolenaar1671f442020-03-10 07:48:13 +0100184 \ '...a bf.',
Bram Moolenaar2e949762018-05-20 14:06:38 +0200185 \ '..........'], getline(1, '$'))
186
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200187 bw!
Bram Moolenaar2e949762018-05-20 14:06:38 +0200188endfun
189
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200190" Test Virtual replace mode.
191func Test_virtual_replace()
Bram Moolenaardf0d24b2018-03-06 14:22:58 +0100192 if exists('&t_kD')
193 let save_t_kD = &t_kD
194 endif
195 if exists('&t_kb')
196 let save_t_kb = &t_kb
197 endif
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200198 exe "set t_kD=\<C-V>x7f t_kb=\<C-V>x08"
199 enew!
200 exe "normal a\nabcdefghi\njk\tlmn\n opq rst\n\<C-D>uvwxyz"
201 call cursor(1,1)
202 set ai bs=2
203 exe "normal gR0\<C-D> 1\nA\nBCDEFGHIJ\n\tKL\nMNO\nPQR"
204 call assert_equal([' 1',
205 \ ' A',
206 \ ' BCDEFGHIJ',
207 \ ' KL',
208 \ ' MNO',
209 \ ' PQR',
210 \ ], getline(1, 6))
211 normal G
212 mark a
213 exe "normal o0\<C-D>\nabcdefghi\njk\tlmn\n opq\trst\n\<C-D>uvwxyz\n"
214 exe "normal 'ajgR0\<C-D> 1\nA\nBCDEFGHIJ\n\tKL\nMNO\nPQR" . repeat("\<BS>", 29)
215 call assert_equal([' 1',
216 \ 'abcdefghi',
217 \ 'jk lmn',
218 \ ' opq rst',
219 \ 'uvwxyz'], getline(7, 11))
220 normal G
221 exe "normal iab\tcdefghi\tjkl"
222 exe "normal 0gRAB......CDEFGHI.J\<Esc>o"
223 exe "normal iabcdefghijklmnopqrst\<Esc>0gRAB\tIJKLMNO\tQR"
224 call assert_equal(['AB......CDEFGHI.Jkl',
225 \ 'AB IJKLMNO QRst'], getline(12, 13))
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200226
227 " Test inserting Tab with 'noexpandtab' and 'softabstop' set to 4
228 %d
229 call setline(1, 'aaaaaaaaaaaaa')
230 set softtabstop=4
231 exe "normal gggR\<Tab>\<Tab>x"
232 call assert_equal("\txaaaa", getline(1))
233 set softtabstop&
234
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200235 enew!
Bram Moolenaare7808482018-03-06 13:17:23 +0100236 set noai bs&vim
Bram Moolenaardf0d24b2018-03-06 14:22:58 +0100237 if exists('save_t_kD')
238 let &t_kD = save_t_kD
239 endif
240 if exists('save_t_kb')
241 let &t_kb = save_t_kb
242 endif
Bram Moolenaar63e82db2018-03-06 12:10:48 +0100243endfunc
244
245" Test Virtual replace mode.
246func Test_virtual_replace2()
247 enew!
248 set bs=2
249 exe "normal a\nabcdefghi\njk\tlmn\n opq rst\n\<C-D>uvwxyz"
250 call cursor(1,1)
251 " Test 1: Test that del deletes the newline
252 exe "normal gR0\<del> 1\nA\nBCDEFGHIJ\n\tKL\nMNO\nPQR"
253 call assert_equal(['0 1',
254 \ 'A',
255 \ 'BCDEFGHIJ',
256 \ ' KL',
257 \ 'MNO',
258 \ 'PQR',
259 \ ], getline(1, 6))
260 " Test 2:
261 " a newline is not deleted, if no newline has been added in virtual replace mode
262 %d_
263 call setline(1, ['abcd', 'efgh', 'ijkl'])
264 call cursor(2,1)
265 exe "norm! gR1234\<cr>5\<bs>\<bs>\<bs>"
266 call assert_equal(['abcd',
267 \ '123h',
268 \ 'ijkl'], getline(1, '$'))
269 " Test 3:
270 " a newline is deleted, if a newline has been inserted before in virtual replace mode
271 %d_
272 call setline(1, ['abcd', 'efgh', 'ijkl'])
273 call cursor(2,1)
274 exe "norm! gR1234\<cr>\<cr>56\<bs>\<bs>\<bs>"
275 call assert_equal(['abcd',
276 \ '1234',
277 \ 'ijkl'], getline(1, '$'))
278 " Test 4:
279 " delete add a newline, delete it, add it again and check undo
280 %d_
281 call setline(1, ['abcd', 'efgh', 'ijkl'])
282 call cursor(2,1)
283 " break undo sequence explicitly
284 let &ul = &ul
285 exe "norm! gR1234\<cr>\<bs>\<del>56\<cr>"
286 let &ul = &ul
287 call assert_equal(['abcd',
288 \ '123456',
289 \ ''], getline(1, '$'))
290 norm! u
291 call assert_equal(['abcd',
292 \ 'efgh',
293 \ 'ijkl'], getline(1, '$'))
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200294
295 " Test for truncating spaces in a newly added line using 'autoindent' if
296 " characters are not added to that line.
297 %d_
298 call setline(1, [' app', ' bee', ' cat'])
299 setlocal autoindent
300 exe "normal gg$gRt\n\nr"
301 call assert_equal([' apt', '', ' rat'], getline(1, '$'))
302
Bram Moolenaar63e82db2018-03-06 12:10:48 +0100303 " clean up
304 %d_
305 set bs&vim
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200306endfunc
Bram Moolenaar6d3a1942019-01-03 23:10:32 +0100307
Bram Moolenaar81b1ba42019-01-13 16:12:40 +0100308func Test_Visual_word_textobject()
309 new
310 call setline(1, ['First sentence. Second sentence.'])
311
312 " When start and end of visual area are identical, 'aw' or 'iw' select
313 " the whole word.
314 norm! 1go2fcvawy
315 call assert_equal('Second ', @")
316 norm! 1go2fcviwy
317 call assert_equal('Second', @")
318
319 " When start and end of visual area are not identical, 'aw' or 'iw'
320 " extend the word in direction of the end of the visual area.
321 norm! 1go2fcvlawy
322 call assert_equal('cond ', @")
323 norm! gv2awy
324 call assert_equal('cond sentence.', @")
325
326 norm! 1go2fcvliwy
327 call assert_equal('cond', @")
328 norm! gv2iwy
329 call assert_equal('cond sentence', @")
330
331 " Extend visual area in opposite direction.
332 norm! 1go2fcvhawy
333 call assert_equal(' Sec', @")
334 norm! gv2awy
335 call assert_equal(' sentence. Sec', @")
336
337 norm! 1go2fcvhiwy
338 call assert_equal('Sec', @")
339 norm! gv2iwy
340 call assert_equal('. Sec', @")
341
342 bwipe!
343endfunc
344
Bram Moolenaar6d3a1942019-01-03 23:10:32 +0100345func Test_Visual_sentence_textobject()
346 new
Bram Moolenaar81b1ba42019-01-13 16:12:40 +0100347 call setline(1, ['First sentence. Second sentence. Third', 'sentence. Fourth sentence'])
Bram Moolenaar6d3a1942019-01-03 23:10:32 +0100348
349 " When start and end of visual area are identical, 'as' or 'is' select
350 " the whole sentence.
351 norm! 1gofdvasy
352 call assert_equal('Second sentence. ', @")
353 norm! 1gofdvisy
354 call assert_equal('Second sentence.', @")
355
356 " When start and end of visual area are not identical, 'as' or 'is'
357 " extend the sentence in direction of the end of the visual area.
358 norm! 1gofdvlasy
359 call assert_equal('d sentence. ', @")
360 norm! gvasy
361 call assert_equal("d sentence. Third\nsentence. ", @")
362
363 norm! 1gofdvlisy
364 call assert_equal('d sentence.', @")
365 norm! gvisy
366 call assert_equal('d sentence. ', @")
367 norm! gvisy
368 call assert_equal("d sentence. Third\nsentence.", @")
369
370 " Extend visual area in opposite direction.
371 norm! 1gofdvhasy
372 call assert_equal(' Second', @")
373 norm! gvasy
374 call assert_equal("First sentence. Second", @")
375
376 norm! 1gofdvhisy
377 call assert_equal('Second', @")
378 norm! gvisy
379 call assert_equal(' Second', @")
380 norm! gvisy
381 call assert_equal('First sentence. Second', @")
382
383 bwipe!
384endfunc
Bram Moolenaar81b1ba42019-01-13 16:12:40 +0100385
386func Test_Visual_paragraph_textobject()
387 new
Bram Moolenaar8a9bc952020-10-02 18:48:07 +0200388 let lines =<< trim [END]
389 First line.
390
391 Second line.
392 Third line.
393 Fourth line.
394 Fifth line.
395
396 Sixth line.
397 [END]
398 call setline(1, lines)
Bram Moolenaar81b1ba42019-01-13 16:12:40 +0100399
400 " When start and end of visual area are identical, 'ap' or 'ip' select
401 " the whole paragraph.
402 norm! 4ggvapy
403 call assert_equal("Second line.\nThird line.\nFourth line.\nFifth line.\n\n", @")
404 norm! 4ggvipy
405 call assert_equal("Second line.\nThird line.\nFourth line.\nFifth line.\n", @")
406
407 " When start and end of visual area are not identical, 'ap' or 'ip'
408 " extend the sentence in direction of the end of the visual area.
409 " FIXME: actually, it is not sufficient to have different start and
410 " end of visual selection, the start line and end line have to differ,
411 " which is not consistent with the documentation.
412 norm! 4ggVjapy
413 call assert_equal("Third line.\nFourth line.\nFifth line.\n\n", @")
414 norm! gvapy
415 call assert_equal("Third line.\nFourth line.\nFifth line.\n\nSixth line.\n", @")
416 norm! 4ggVjipy
417 call assert_equal("Third line.\nFourth line.\nFifth line.\n", @")
418 norm! gvipy
419 call assert_equal("Third line.\nFourth line.\nFifth line.\n\n", @")
420 norm! gvipy
421 call assert_equal("Third line.\nFourth line.\nFifth line.\n\nSixth line.\n", @")
422
423 " Extend visual area in opposite direction.
424 norm! 5ggVkapy
425 call assert_equal("\nSecond line.\nThird line.\nFourth line.\n", @")
426 norm! gvapy
427 call assert_equal("First line.\n\nSecond line.\nThird line.\nFourth line.\n", @")
428 norm! 5ggVkipy
429 call assert_equal("Second line.\nThird line.\nFourth line.\n", @")
430 norma gvipy
431 call assert_equal("\nSecond line.\nThird line.\nFourth line.\n", @")
432 norm! gvipy
433 call assert_equal("First line.\n\nSecond line.\nThird line.\nFourth line.\n", @")
434
435 bwipe!
436endfunc
Bram Moolenaar19a66852019-03-07 11:25:32 +0100437
438func Test_curswant_not_changed()
439 new
440 call setline(1, ['one', 'two'])
441 au InsertLeave * call getcurpos()
442 call feedkeys("gg0\<C-V>jI123 \<Esc>j", 'xt')
443 call assert_equal([0, 2, 1, 0, 1], getcurpos())
444
445 bwipe!
446 au! InsertLeave
447endfunc
Bram Moolenaarc6b37db2019-04-27 18:00:34 +0200448
449" Tests for "vaBiB", end could be wrong.
450func Test_Visual_Block()
451 new
452 a
453- Bug in "vPPPP" on this text:
454 {
455 cmd;
456 {
457 cmd;\t/* <-- Start cursor here */
458 {
459 }
460 }
461 }
462.
463 normal gg
464 call search('Start cursor here')
465 normal vaBiBD
466 call assert_equal(['- Bug in "vPPPP" on this text:',
467 \ "\t{",
468 \ "\t}"], getline(1, '$'))
469
470 close!
471endfunc
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +0100472
473" Test for 'p'ut in visual block mode
474func Test_visual_block_put()
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200475 new
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +0100476 call append(0, ['One', 'Two', 'Three'])
477 normal gg
478 yank
479 call feedkeys("jl\<C-V>ljp", 'xt')
480 call assert_equal(['One', 'T', 'Tee', 'One', ''], getline(1, '$'))
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200481 bw!
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +0100482endfunc
483
Bram Moolenaar309976e2019-12-05 18:16:33 +0100484" Visual modes (v V CTRL-V) followed by an operator; count; repeating
485func Test_visual_mode_op()
486 new
487 call append(0, '')
488
489 call setline(1, 'apple banana cherry')
490 call cursor(1, 1)
491 normal lvld.l3vd.
492 call assert_equal('a y', getline(1))
493
494 call setline(1, ['line 1 line 1', 'line 2 line 2', 'line 3 line 3',
495 \ 'line 4 line 4', 'line 5 line 5', 'line 6 line 6'])
496 call cursor(1, 1)
497 exe "normal Vcnewline\<Esc>j.j2Vd."
498 call assert_equal(['newline', 'newline'], getline(1, '$'))
499
500 call deletebufline('', 1, '$')
501 call setline(1, ['xxxxxxxxxxxxx', 'xxxxxxxxxxxxx', 'xxxxxxxxxxxxx',
502 \ 'xxxxxxxxxxxxx'])
503 exe "normal \<C-V>jlc \<Esc>l.l2\<C-V>c----\<Esc>l."
504 call assert_equal([' --------x',
505 \ ' --------x',
506 \ 'xxxx--------x',
507 \ 'xxxx--------x'], getline(1, '$'))
508
509 bwipe!
510endfunc
511
512" Visual mode maps (movement and text object)
513" Visual mode maps; count; repeating
514" - Simple
515" - With an Ex command (custom text object)
516func Test_visual_mode_maps()
517 new
518 call append(0, '')
519
520 func SelectInCaps()
521 let [line1, col1] = searchpos('\u', 'bcnW')
522 let [line2, col2] = searchpos('.\u', 'nW')
523 call setpos("'<", [0, line1, col1, 0])
524 call setpos("'>", [0, line2, col2, 0])
525 normal! gv
526 endfunction
527
528 vnoremap W /\u/s-1<CR>
529 vnoremap iW :<C-U>call SelectInCaps()<CR>
530
531 call setline(1, 'KiwiRaspberryDateWatermelonPeach')
532 call cursor(1, 1)
533 exe "normal vWcNo\<Esc>l.fD2vd."
534 call assert_equal('NoNoberryach', getline(1))
535
536 call setline(1, 'JambuRambutanBananaTangerineMango')
537 call cursor(1, 1)
538 exe "normal llviWc-\<Esc>l.l2vdl."
539 call assert_equal('--ago', getline(1))
540
541 vunmap W
542 vunmap iW
543 bwipe!
544 delfunc SelectInCaps
545endfunc
546
547" Operator-pending mode maps (movement and text object)
548" - Simple
549" - With Ex command moving the cursor
550" - With Ex command and Visual selection (custom text object)
551func Test_visual_oper_pending_mode_maps()
552 new
553 call append(0, '')
554
555 func MoveToCap()
556 call search('\u', 'W')
557 endfunction
558
559 func SelectInCaps()
560 let [line1, col1] = searchpos('\u', 'bcnW')
561 let [line2, col2] = searchpos('.\u', 'nW')
562 call setpos("'<", [0, line1, col1, 0])
563 call setpos("'>", [0, line2, col2, 0])
564 normal! gv
565 endfunction
566
567 onoremap W /\u/<CR>
568 onoremap <Leader>W :<C-U>call MoveToCap()<CR>
569 onoremap iW :<C-U>call SelectInCaps()<CR>
570
571 call setline(1, 'PineappleQuinceLoganberryOrangeGrapefruitKiwiZ')
572 call cursor(1, 1)
573 exe "normal cW-\<Esc>l.l2.l."
574 call assert_equal('----Z', getline(1))
575
576 call setline(1, 'JuniperDurianZ')
577 call cursor(1, 1)
578 exe "normal g?\WfD."
579 call assert_equal('WhavcreQhevnaZ', getline(1))
580
581 call setline(1, 'LemonNectarineZ')
582 call cursor(1, 1)
583 exe "normal yiWPlciWNew\<Esc>fr."
584 call assert_equal('LemonNewNewZ', getline(1))
585
586 ounmap W
587 ounmap <Leader>W
588 ounmap iW
589 bwipe!
590 delfunc MoveToCap
591 delfunc SelectInCaps
592endfunc
593
594" Patch 7.3.879: Properly abort Operator-pending mode for "dv:<Esc>" etc.
595func Test_op_pend_mode_abort()
596 new
597 call append(0, '')
598
599 call setline(1, ['zzzz', 'zzzz'])
600 call cursor(1, 1)
601
602 exe "normal dV:\<CR>dv:\<CR>"
603 call assert_equal(['zzz'], getline(1, 2))
604 set nomodifiable
605 call assert_fails('exe "normal d:\<CR>"', 'E21:')
606 set modifiable
607 call feedkeys("dv:\<Esc>dV:\<Esc>", 'xt')
608 call assert_equal(['zzz'], getline(1, 2))
609 set nomodifiable
610 let v:errmsg = ''
611 call feedkeys("d:\<Esc>", 'xt')
612 call assert_true(v:errmsg !~# '^E21:')
613 set modifiable
614
615 bwipe!
616endfunc
617
618func Test_characterwise_visual_mode()
619 new
620
621 " characterwise visual mode: replace last line
622 $put ='a'
623 let @" = 'x'
624 normal v$p
625 call assert_equal('x', getline('$'))
626
627 " characterwise visual mode: delete middle line
628 call deletebufline('', 1, '$')
629 call append('$', ['a', 'b', 'c'])
630 normal G
631 normal kkv$d
632 call assert_equal(['', 'b', 'c'], getline(1, '$'))
633
634 " characterwise visual mode: delete middle two lines
635 call deletebufline('', 1, '$')
636 call append('$', ['a', 'b', 'c'])
637 normal Gkkvj$d
638 call assert_equal(['', 'c'], getline(1, '$'))
639
640 " characterwise visual mode: delete last line
641 call deletebufline('', 1, '$')
642 call append('$', ['a', 'b', 'c'])
643 normal Gv$d
644 call assert_equal(['', 'a', 'b', ''], getline(1, '$'))
645
646 " characterwise visual mode: delete last two lines
647 call deletebufline('', 1, '$')
648 call append('$', ['a', 'b', 'c'])
649 normal Gkvj$d
650 call assert_equal(['', 'a', ''], getline(1, '$'))
651
Bram Moolenaar8a9bc952020-10-02 18:48:07 +0200652 " characterwise visual mode: use a count with the visual mode from the last
653 " line in the buffer
654 %d _
655 call setline(1, ['one', 'two', 'three', 'four'])
656 norm! vj$y
657 norm! G1vy
658 call assert_equal('four', @")
659
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200660 " characterwise visual mode: replace a single character line and the eol
661 %d _
662 call setline(1, "a")
663 normal v$rx
664 call assert_equal(['x'], getline(1, '$'))
665
Bram Moolenaar309976e2019-12-05 18:16:33 +0100666 bwipe!
667endfunc
668
Bram Moolenaar309976e2019-12-05 18:16:33 +0100669func Test_visual_mode_put()
670 new
671
672 " v_p: replace last character with line register at middle line
673 call append('$', ['aaa', 'bbb', 'ccc'])
674 normal G
675 -2yank
676 normal k$vp
677 call assert_equal(['', 'aaa', 'bb', 'aaa', '', 'ccc'], getline(1, '$'))
678
679 " v_p: replace last character with line register at middle line selecting
680 " newline
681 call deletebufline('', 1, '$')
682 call append('$', ['aaa', 'bbb', 'ccc'])
683 normal G
684 -2yank
685 normal k$v$p
686 call assert_equal(['', 'aaa', 'bb', 'aaa', 'ccc'], getline(1, '$'))
687
688 " v_p: replace last character with line register at last line
689 call deletebufline('', 1, '$')
690 call append('$', ['aaa', 'bbb', 'ccc'])
691 normal G
692 -2yank
693 normal $vp
694 call assert_equal(['', 'aaa', 'bbb', 'cc', 'aaa', ''], getline(1, '$'))
695
696 " v_p: replace last character with line register at last line selecting
697 " newline
698 call deletebufline('', 1, '$')
699 call append('$', ['aaa', 'bbb', 'ccc'])
700 normal G
701 -2yank
702 normal $v$p
703 call assert_equal(['', 'aaa', 'bbb', 'cc', 'aaa', ''], getline(1, '$'))
704
705 bwipe!
706endfunc
707
Bram Moolenaar515545e2020-03-22 14:08:59 +0100708func Test_gv_with_exclusive_selection()
Bram Moolenaar309976e2019-12-05 18:16:33 +0100709 new
710
Bram Moolenaar515545e2020-03-22 14:08:59 +0100711 " gv with exclusive selection after an operation
Bram Moolenaar309976e2019-12-05 18:16:33 +0100712 call append('$', ['zzz ', 'äà '])
713 set selection=exclusive
714 normal Gkv3lyjv3lpgvcxxx
715 call assert_equal(['', 'zzz ', 'xxx '], getline(1, '$'))
716
Bram Moolenaar515545e2020-03-22 14:08:59 +0100717 " gv with exclusive selection without an operation
Bram Moolenaar309976e2019-12-05 18:16:33 +0100718 call deletebufline('', 1, '$')
719 call append('$', 'zzz ')
720 set selection=exclusive
721 exe "normal G0v3l\<Esc>gvcxxx"
722 call assert_equal(['', 'xxx '], getline(1, '$'))
723
724 set selection&vim
725 bwipe!
726endfunc
727
Bram Moolenaar1f3e7d32019-12-06 20:43:36 +0100728" Tests for the visual block mode commands
729func Test_visual_block_mode()
730 new
731 call append(0, '')
Bram Moolenaar1671f442020-03-10 07:48:13 +0100732 call setline(1, repeat(['abcdefghijklm'], 5))
Bram Moolenaar1f3e7d32019-12-06 20:43:36 +0100733 call cursor(1, 1)
734
735 " Test shift-right of a block
736 exe "normal jllll\<C-V>jj>wll\<C-V>jlll>"
737 " Test shift-left of a block
738 exe "normal G$hhhh\<C-V>kk<"
739 " Test block-insert
740 exe "normal Gkl\<C-V>kkkIxyz"
741 " Test block-replace
742 exe "normal Gllll\<C-V>kkklllrq"
743 " Test block-change
744 exe "normal G$khhh\<C-V>hhkkcmno"
745 call assert_equal(['axyzbcdefghijklm',
746 \ 'axyzqqqq mno ghijklm',
747 \ 'axyzqqqqef mno ghijklm',
748 \ 'axyzqqqqefgmnoklm',
749 \ 'abcdqqqqijklm'], getline(1, 5))
750
Bram Moolenaar1671f442020-03-10 07:48:13 +0100751 " Test 'C' to change till the end of the line
752 call cursor(3, 4)
753 exe "normal! \<C-V>j3lCooo"
754 call assert_equal(['axyooo', 'axyooo'], getline(3, 4))
755
756 " Test 'D' to delete till the end of the line
757 call cursor(3, 3)
758 exe "normal! \<C-V>j2lD"
759 call assert_equal(['ax', 'ax'], getline(3, 4))
760
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200761 " Test block insert with a short line that ends before the block
762 %d _
763 call setline(1, [" one", "a", " two"])
764 exe "normal gg\<C-V>2jIx"
765 call assert_equal([" xone", "a", " xtwo"], getline(1, '$'))
766
767 " Test block append at EOL with '$' and without '$'
768 %d _
769 call setline(1, ["one", "a", "two"])
770 exe "normal gg$\<C-V>2jAx"
771 call assert_equal(["onex", "ax", "twox"], getline(1, '$'))
772 %d _
773 call setline(1, ["one", "a", "two"])
774 exe "normal gg3l\<C-V>2jAx"
775 call assert_equal(["onex", "a x", "twox"], getline(1, '$'))
776
777 " Test block replace with an empty line in the middle and use $ to jump to
778 " the end of the line.
779 %d _
780 call setline(1, ['one', '', 'two'])
781 exe "normal gg$\<C-V>2jrx"
782 call assert_equal(["onx", "", "twx"], getline(1, '$'))
783
784 " Test block replace with an empty line in the middle and move cursor to the
785 " end of the line
786 %d _
787 call setline(1, ['one', '', 'two'])
788 exe "normal gg2l\<C-V>2jrx"
789 call assert_equal(["onx", "", "twx"], getline(1, '$'))
790
791 " Replace odd number of characters with a multibyte character
792 %d _
793 call setline(1, ['abcd', 'efgh'])
794 exe "normal ggl\<C-V>2ljr\u1100"
795 call assert_equal(["a\u1100 ", "e\u1100 "], getline(1, '$'))
796
797 " During visual block append, if the cursor moved outside of the selected
798 " range, then the edit should not be applied to the block.
799 %d _
800 call setline(1, ['aaa', 'bbb', 'ccc'])
801 exe "normal 2G\<C-V>jAx\<Up>"
802 call assert_equal(['aaa', 'bxbb', 'ccc'], getline(1, '$'))
803
804 " During visual block append, if the cursor is moved before the start of the
805 " block, then the new text should be appended there.
806 %d _
807 call setline(1, ['aaa', 'bbb', 'ccc'])
808 exe "normal $\<C-V>2jA\<Left>x"
809 " BUG: Instead of adding x as the third character in all the three lines,
810 " 'a' is added in the second and third lines at the end. This bug is not
811 " reproducible if this operation is performed manually.
812 "call assert_equal(['aaxa', 'bbxb', 'ccxc'], getline(1, '$'))
813 call assert_equal(['aaxa', 'bbba', 'ccca'], getline(1, '$'))
814
815 " Change a characterwise motion to a blockwise motion using CTRL-V
816 %d _
817 call setline(1, ['123', '456', '789'])
818 exe "normal ld\<C-V>j"
819 call assert_equal(['13', '46', '789'], getline(1, '$'))
820
Bram Moolenaar1f3e7d32019-12-06 20:43:36 +0100821 bwipe!
822endfunc
823
824" Test block-insert using cursor keys for movement
825func Test_visual_block_insert_cursor_keys()
826 new
827 call append(0, ['aaaaaa', 'bbbbbb', 'cccccc', 'dddddd'])
828 call cursor(1, 1)
829
830 exe "norm! l\<C-V>jjjlllI\<Right>\<Right> \<Esc>"
831 call assert_equal(['aaa aaa', 'bbb bbb', 'ccc ccc', 'ddd ddd'],
832 \ getline(1, 4))
833
834 call deletebufline('', 1, '$')
835 call setline(1, ['xaaa', 'bbbb', 'cccc', 'dddd'])
836 call cursor(1, 1)
837 exe "norm! \<C-V>jjjI<>\<Left>p\<Esc>"
838 call assert_equal(['<p>xaaa', '<p>bbbb', '<p>cccc', '<p>dddd'],
839 \ getline(1, 4))
840 bwipe!
841endfunc
842
843func Test_visual_block_create()
844 new
845 call append(0, '')
846 " Test for Visual block was created with the last <C-v>$
847 call setline(1, ['A23', '4567'])
848 call cursor(1, 1)
849 exe "norm! l\<C-V>j$Aab\<Esc>"
850 call assert_equal(['A23ab', '4567ab'], getline(1, 2))
851
852 " Test for Visual block was created with the middle <C-v>$ (1)
853 call deletebufline('', 1, '$')
854 call setline(1, ['B23', '4567'])
855 call cursor(1, 1)
856 exe "norm! l\<C-V>j$hAab\<Esc>"
857 call assert_equal(['B23 ab', '4567ab'], getline(1, 2))
858
859 " Test for Visual block was created with the middle <C-v>$ (2)
860 call deletebufline('', 1, '$')
861 call setline(1, ['C23', '4567'])
862 call cursor(1, 1)
863 exe "norm! l\<C-V>j$hhAab\<Esc>"
864 call assert_equal(['C23ab', '456ab7'], getline(1, 2))
865 bwipe!
866endfunc
867
868" Test for Visual block insert when virtualedit=all
869func Test_virtualedit_visual_block()
870 set ve=all
871 new
872 call append(0, ["\t\tline1", "\t\tline2", "\t\tline3"])
873 call cursor(1, 1)
874 exe "norm! 07l\<C-V>jjIx\<Esc>"
875 call assert_equal([" x \tline1",
876 \ " x \tline2",
877 \ " x \tline3"], getline(1, 3))
878
879 " Test for Visual block append when virtualedit=all
880 exe "norm! 012l\<C-v>jjAx\<Esc>"
881 call assert_equal([' x x line1',
882 \ ' x x line2',
883 \ ' x x line3'], getline(1, 3))
884 set ve=
885 bwipe!
886endfunc
887
888" Test for changing case
889func Test_visual_change_case()
890 new
891 " gUe must uppercase a whole word, also when ß changes to SS
892 exe "normal Gothe youtußeuu end\<Esc>Ypk0wgUe\r"
893 " gUfx must uppercase until x, inclusive.
894 exe "normal O- youßtußexu -\<Esc>0fogUfx\r"
895 " VU must uppercase a whole line
896 exe "normal YpkVU\r"
897 " same, when it's the last line in the buffer
898 exe "normal YPGi111\<Esc>VUddP\r"
899 " Uppercase two lines
900 exe "normal Oblah di\rdoh dut\<Esc>VkUj\r"
901 " Uppercase part of two lines
902 exe "normal ddppi333\<Esc>k0i222\<Esc>fyllvjfuUk"
903 call assert_equal(['the YOUTUSSEUU end', '- yOUSSTUSSEXu -',
904 \ 'THE YOUTUSSEUU END', '111THE YOUTUSSEUU END', 'BLAH DI', 'DOH DUT',
905 \ '222the yoUTUSSEUU END', '333THE YOUTUßeuu end'], getline(2, '$'))
906 bwipe!
907endfunc
908
909" Test for Visual replace using Enter or NL
910func Test_visual_replace_crnl()
911 new
912 exe "normal G3o123456789\e2k05l\<C-V>2jr\r"
913 exe "normal G3o98765\e2k02l\<C-V>2jr\<C-V>\r\n"
914 exe "normal G3o123456789\e2k05l\<C-V>2jr\n"
915 exe "normal G3o98765\e2k02l\<C-V>2jr\<C-V>\n"
916 call assert_equal(['12345', '789', '12345', '789', '12345', '789', "98\r65",
917 \ "98\r65", "98\r65", '12345', '789', '12345', '789', '12345', '789',
918 \ "98\n65", "98\n65", "98\n65"], getline(2, '$'))
919 bwipe!
920endfunc
921
922func Test_ve_block_curpos()
923 new
924 " Test cursor position. When ve=block and Visual block mode and $gj
925 call append(0, ['12345', '789'])
926 call cursor(1, 3)
927 set virtualedit=block
928 exe "norm! \<C-V>$gj\<Esc>"
929 call assert_equal([0, 2, 4, 0], getpos("'>"))
930 set virtualedit=
931 bwipe!
932endfunc
933
934" Test for block_insert when replacing spaces in front of the a with tabs
935func Test_block_insert_replace_tabs()
936 new
937 set ts=8 sts=4 sw=4
938 call append(0, ["#define BO_ALL\t 0x0001",
939 \ "#define BO_BS\t 0x0002",
940 \ "#define BO_CRSR\t 0x0004"])
941 call cursor(1, 1)
942 exe "norm! f0\<C-V>2jI\<tab>\<esc>"
943 call assert_equal([
944 \ "#define BO_ALL\t\t0x0001",
945 \ "#define BO_BS\t \t0x0002",
946 \ "#define BO_CRSR\t \t0x0004", ''], getline(1, '$'))
947 set ts& sts& sw&
948 bwipe!
949endfunc
950
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100951" Test for * register in :
952func Test_star_register()
953 call assert_fails('*bfirst', 'E16:')
954 new
955 call setline(1, ['foo', 'bar', 'baz', 'qux'])
956 exe "normal jVj\<ESC>"
957 *yank r
958 call assert_equal("bar\nbaz\n", @r)
959
960 delmarks < >
961 call assert_fails('*yank', 'E20:')
962 close!
963endfunc
964
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100965" Test for changing text in visual mode with 'exclusive' selection
966func Test_exclusive_selection()
967 new
968 call setline(1, ['one', 'two'])
969 set selection=exclusive
970 call feedkeys("vwcabc", 'xt')
971 call assert_equal('abctwo', getline(1))
972 call setline(1, ["\tone"])
973 set virtualedit=all
974 call feedkeys('0v2lcl', 'xt')
975 call assert_equal('l one', getline(1))
976 set virtualedit&
977 set selection&
978 close!
979endfunc
980
Bram Moolenaard1ad99b2020-10-04 16:16:54 +0200981" Test for starting linewise visual with a count.
982" This test needs to be run without any previous visual mode. Otherwise the
983" count will use the count from the previous visual mode.
984func Test_linewise_visual_with_count()
985 let after =<< trim [CODE]
986 call setline(1, ['one', 'two', 'three', 'four'])
987 norm! 3Vy
988 call assert_equal("one\ntwo\nthree\n", @")
989 call writefile(v:errors, 'Xtestout')
990 qall!
991 [CODE]
992 if RunVim([], after, '')
993 call assert_equal([], readfile('Xtestout'))
994 call delete('Xtestout')
995 endif
996endfunc
997
998" Test for starting characterwise visual with a count.
999" This test needs to be run without any previous visual mode. Otherwise the
1000" count will use the count from the previous visual mode.
1001func Test_characterwise_visual_with_count()
1002 let after =<< trim [CODE]
1003 call setline(1, ['one two', 'three'])
1004 norm! l5vy
1005 call assert_equal("ne tw", @")
1006 call writefile(v:errors, 'Xtestout')
1007 qall!
1008 [CODE]
1009 if RunVim([], after, '')
1010 call assert_equal([], readfile('Xtestout'))
1011 call delete('Xtestout')
1012 endif
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01001013endfunc
1014
Bram Moolenaar224a5f12020-04-28 20:29:07 +02001015" Test for visually selecting an inner block (iB)
1016func Test_visual_inner_block()
1017 new
1018 call setline(1, ['one', '{', 'two', '{', 'three', '}', 'four', '}', 'five'])
1019 call cursor(5, 1)
1020 " visually select all the lines in the block and then execute iB
1021 call feedkeys("ViB\<C-C>", 'xt')
1022 call assert_equal([0, 5, 1, 0], getpos("'<"))
1023 call assert_equal([0, 5, 6, 0], getpos("'>"))
1024 " visually select two inner blocks
1025 call feedkeys("ViBiB\<C-C>", 'xt')
1026 call assert_equal([0, 3, 1, 0], getpos("'<"))
1027 call assert_equal([0, 7, 5, 0], getpos("'>"))
1028 " try to select non-existing inner block
1029 call cursor(5, 1)
1030 call assert_beeps('normal ViBiBiB')
1031 " try to select a unclosed inner block
1032 8,9d
1033 call cursor(5, 1)
1034 call assert_beeps('normal ViBiB')
1035 close!
1036endfunc
1037
Bram Moolenaarcd942772020-08-22 21:08:44 +02001038func Test_visual_put_in_block()
1039 new
1040 call setline(1, ['xxxx', 'yyy', 'zzzz'])
1041 normal 1G2yl
1042 exe "normal 1G2l\<C-V>jjlp"
1043 call assert_equal(['xxxx', 'yxx', 'zzxx'], getline(1, 3))
1044 bwipe!
1045endfunc
1046
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +01001047" vim: shiftwidth=2 sts=2 expandtab