blob: ed8efc5dedf479bd4628af2a01aefd8498ba94d9 [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
Bram Moolenaar7d7bcc62021-06-28 21:54:27 +02004source check.vim
Bram Moolenaar9cee4a12021-07-03 15:08:37 +02005source screendump.vim
Bram Moolenaard1ad99b2020-10-04 16:16:54 +02006
Bram Moolenaar019b9c62016-03-05 17:26:00 +01007func Test_block_shift_multibyte()
Bram Moolenaarf8eb9c52017-01-02 17:31:24 +01008 " Uses double-wide character.
Bram Moolenaar019b9c62016-03-05 17:26:00 +01009 split
10 call setline(1, ['xヹxxx', 'ヹxxx'])
11 exe "normal 1G0l\<C-V>jl>"
12 call assert_equal('x ヹxxx', getline(1))
13 call assert_equal(' ヹxxx', getline(2))
14 q!
15endfunc
Bram Moolenaarf8eb9c52017-01-02 17:31:24 +010016
Bram Moolenaarbae5a172017-08-06 15:42:06 +020017func Test_block_shift_overflow()
18 " This used to cause a multiplication overflow followed by a crash.
19 new
20 normal ii
21 exe "normal \<C-V>876543210>"
22 q!
23endfunc
24
Bram Moolenaarf8eb9c52017-01-02 17:31:24 +010025func Test_dotregister_paste()
26 new
27 exe "norm! ihello world\<esc>"
28 norm! 0ve".p
29 call assert_equal('hello world world', getline(1))
30 q!
31endfunc
Bram Moolenaar23fa81d2017-02-01 21:50:21 +010032
33func Test_Visual_ctrl_o()
34 new
35 call setline(1, ['one', 'two', 'three'])
36 call cursor(1,2)
37 set noshowmode
38 set tw=0
39 call feedkeys("\<c-v>jjlIa\<c-\>\<c-o>:set tw=88\<cr>\<esc>", 'tx')
40 call assert_equal(['oane', 'tawo', 'tahree'], getline(1, 3))
41 call assert_equal(88, &tw)
42 set tw&
43 bw!
44endfu
Bram Moolenaar84b2a382017-02-17 11:40:00 +010045
46func Test_Visual_vapo()
47 new
48 normal oxx
49 normal vapo
50 bwipe!
51endfunc
Bram Moolenaar46522af2017-02-18 23:12:01 +010052
53func Test_Visual_inner_quote()
54 new
55 normal oxX
56 normal vki'
57 bwipe!
58endfunc
Bram Moolenaar75373f32017-08-07 22:02:30 +020059
60" Test for Visual mode not being reset causing E315 error.
61func TriggerTheProblem()
62 " At this point there is no visual selection because :call reset it.
63 " Let's restore the selection:
64 normal gv
65 '<,'>del _
66 try
67 exe "normal \<Esc>"
68 catch /^Vim\%((\a\+)\)\=:E315/
69 echom 'Snap! E315 error!'
Bram Moolenaar63e82db2018-03-06 12:10:48 +010070 let g:msg = 'Snap! E315 error!'
Bram Moolenaar75373f32017-08-07 22:02:30 +020071 endtry
72endfunc
73
74func Test_visual_mode_reset()
Bram Moolenaar75373f32017-08-07 22:02:30 +020075 enew
Bram Moolenaar63e82db2018-03-06 12:10:48 +010076 let g:msg = "Everything's fine."
Bram Moolenaar75373f32017-08-07 22:02:30 +020077 enew
78 setl buftype=nofile
79 call append(line('$'), 'Delete this line.')
80
81 " NOTE: this has to be done by a call to a function because executing :del
82 " the ex-way will require the colon operator which resets the visual mode
83 " thus preventing the problem:
84 exe "normal! GV:call TriggerTheProblem()\<CR>"
85 call assert_equal("Everything's fine.", g:msg)
Bram Moolenaar75373f32017-08-07 22:02:30 +020086endfunc
Bram Moolenaar67418d92017-10-15 22:07:39 +020087
Bram Moolenaar15993ce2017-10-26 20:21:44 +020088" Test for visual block shift and tab characters.
89func Test_block_shift_tab()
Bram Moolenaar3e72dca2021-05-29 16:30:12 +020090 new
Bram Moolenaar15993ce2017-10-26 20:21:44 +020091 call append(0, repeat(['one two three'], 5))
92 call cursor(1,1)
93 exe "normal i\<C-G>u"
94 exe "normal fe\<C-V>4jR\<Esc>ugvr1"
95 call assert_equal('on1 two three', getline(1))
96 call assert_equal('on1 two three', getline(2))
97 call assert_equal('on1 two three', getline(5))
98
Bram Moolenaar3e72dca2021-05-29 16:30:12 +020099 %d _
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200100 call append(0, repeat(['abcdefghijklmnopqrstuvwxyz'], 5))
101 call cursor(1,1)
102 exe "normal \<C-V>4jI \<Esc>j<<11|D"
103 exe "normal j7|a\<Tab>\<Tab>"
104 exe "normal j7|a\<Tab>\<Tab> "
105 exe "normal j7|a\<Tab> \<Tab>\<Esc>4k13|\<C-V>4j<"
106 call assert_equal(' abcdefghijklmnopqrstuvwxyz', getline(1))
107 call assert_equal('abcdefghij', getline(2))
108 call assert_equal(" abc\<Tab> defghijklmnopqrstuvwxyz", getline(3))
109 call assert_equal(" abc\<Tab> defghijklmnopqrstuvwxyz", getline(4))
110 call assert_equal(" abc\<Tab> defghijklmnopqrstuvwxyz", getline(5))
111
112 %s/\s\+//g
113 call cursor(1,1)
114 exe "normal \<C-V>4jI \<Esc>j<<"
115 exe "normal j7|a\<Tab>\<Tab>"
116 exe "normal j7|a\<Tab>\<Tab>\<Tab>\<Tab>\<Tab>"
117 exe "normal j7|a\<Tab> \<Tab>\<Tab>\<Esc>4k13|\<C-V>4j3<"
118 call assert_equal(' abcdefghijklmnopqrstuvwxyz', getline(1))
119 call assert_equal('abcdefghij', getline(2))
120 call assert_equal(" abc\<Tab> defghijklmnopqrstuvwxyz", getline(3))
121 call assert_equal(" abc\<Tab>\<Tab>defghijklmnopqrstuvwxyz", getline(4))
122 call assert_equal(" abc\<Tab> defghijklmnopqrstuvwxyz", getline(5))
123
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200124 " Test for block shift with space characters at the beginning and with
125 " 'noexpandtab' and 'expandtab'
126 %d _
127 call setline(1, [" 1", " 2", " 3"])
128 setlocal shiftwidth=2 noexpandtab
129 exe "normal gg\<C-V>3j>"
130 call assert_equal(["\t1", "\t2", "\t3"], getline(1, '$'))
131 %d _
132 call setline(1, [" 1", " 2", " 3"])
133 setlocal shiftwidth=2 expandtab
134 exe "normal gg\<C-V>3j>"
135 call assert_equal([" 1", " 2", " 3"], getline(1, '$'))
136 setlocal shiftwidth&
137
138 bw!
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200139endfunc
140
141" Tests Blockwise Visual when there are TABs before the text.
142func Test_blockwise_visual()
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200143 new
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200144 call append(0, ['123456',
145 \ '234567',
146 \ '345678',
147 \ '',
148 \ 'test text test tex start here',
149 \ "\t\tsome text",
150 \ "\t\ttest text",
151 \ 'test text'])
152 call cursor(1,1)
153 exe "normal /start here$\<CR>"
154 exe 'normal "by$' . "\<C-V>jjlld"
155 exe "normal /456$\<CR>"
156 exe "normal \<C-V>jj" . '"bP'
157 call assert_equal(['123start here56',
158 \ '234start here67',
159 \ '345start here78',
160 \ '',
161 \ 'test text test tex rt here',
162 \ "\t\tsomext",
163 \ "\t\ttesext"], getline(1, 7))
164
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200165 bw!
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200166endfunc
167
Bram Moolenaar2e949762018-05-20 14:06:38 +0200168" Test swapping corners in blockwise visual mode with o and O
169func Test_blockwise_visual_o_O()
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200170 new
Bram Moolenaar2e949762018-05-20 14:06:38 +0200171
172 exe "norm! 10i.\<Esc>Y4P3lj\<C-V>4l2jr "
173 exe "norm! gvO\<Esc>ra"
174 exe "norm! gvO\<Esc>rb"
175 exe "norm! gvo\<C-c>rc"
176 exe "norm! gvO\<C-c>rd"
Bram Moolenaar1671f442020-03-10 07:48:13 +0100177 set selection=exclusive
178 exe "norm! gvOo\<C-c>re"
179 call assert_equal('...a be.', getline(4))
180 exe "norm! gvOO\<C-c>rf"
181 set selection&
Bram Moolenaar2e949762018-05-20 14:06:38 +0200182
183 call assert_equal(['..........',
184 \ '...c d..',
185 \ '... ..',
Bram Moolenaar1671f442020-03-10 07:48:13 +0100186 \ '...a bf.',
Bram Moolenaar2e949762018-05-20 14:06:38 +0200187 \ '..........'], getline(1, '$'))
188
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200189 bw!
Bram Moolenaar2e949762018-05-20 14:06:38 +0200190endfun
191
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200192" Test Virtual replace mode.
193func Test_virtual_replace()
Bram Moolenaardf0d24b2018-03-06 14:22:58 +0100194 if exists('&t_kD')
195 let save_t_kD = &t_kD
196 endif
197 if exists('&t_kb')
198 let save_t_kb = &t_kb
199 endif
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200200 exe "set t_kD=\<C-V>x7f t_kb=\<C-V>x08"
201 enew!
202 exe "normal a\nabcdefghi\njk\tlmn\n opq rst\n\<C-D>uvwxyz"
203 call cursor(1,1)
204 set ai bs=2
205 exe "normal gR0\<C-D> 1\nA\nBCDEFGHIJ\n\tKL\nMNO\nPQR"
206 call assert_equal([' 1',
207 \ ' A',
208 \ ' BCDEFGHIJ',
209 \ ' KL',
210 \ ' MNO',
211 \ ' PQR',
212 \ ], getline(1, 6))
213 normal G
214 mark a
215 exe "normal o0\<C-D>\nabcdefghi\njk\tlmn\n opq\trst\n\<C-D>uvwxyz\n"
216 exe "normal 'ajgR0\<C-D> 1\nA\nBCDEFGHIJ\n\tKL\nMNO\nPQR" . repeat("\<BS>", 29)
217 call assert_equal([' 1',
218 \ 'abcdefghi',
219 \ 'jk lmn',
220 \ ' opq rst',
221 \ 'uvwxyz'], getline(7, 11))
222 normal G
223 exe "normal iab\tcdefghi\tjkl"
224 exe "normal 0gRAB......CDEFGHI.J\<Esc>o"
225 exe "normal iabcdefghijklmnopqrst\<Esc>0gRAB\tIJKLMNO\tQR"
226 call assert_equal(['AB......CDEFGHI.Jkl',
227 \ 'AB IJKLMNO QRst'], getline(12, 13))
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200228
229 " Test inserting Tab with 'noexpandtab' and 'softabstop' set to 4
230 %d
231 call setline(1, 'aaaaaaaaaaaaa')
232 set softtabstop=4
233 exe "normal gggR\<Tab>\<Tab>x"
234 call assert_equal("\txaaaa", getline(1))
235 set softtabstop&
236
Bram Moolenaar8ee60282021-12-15 21:08:50 +0000237 call setline(1, "xã̳x")
238 normal gg0lvrb
239 call assert_equal("xbx", getline(1))
240
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200241 enew!
Bram Moolenaare7808482018-03-06 13:17:23 +0100242 set noai bs&vim
Bram Moolenaardf0d24b2018-03-06 14:22:58 +0100243 if exists('save_t_kD')
244 let &t_kD = save_t_kD
245 endif
246 if exists('save_t_kb')
247 let &t_kb = save_t_kb
248 endif
Bram Moolenaar63e82db2018-03-06 12:10:48 +0100249endfunc
250
251" Test Virtual replace mode.
252func Test_virtual_replace2()
253 enew!
254 set bs=2
255 exe "normal a\nabcdefghi\njk\tlmn\n opq rst\n\<C-D>uvwxyz"
256 call cursor(1,1)
257 " Test 1: Test that del deletes the newline
258 exe "normal gR0\<del> 1\nA\nBCDEFGHIJ\n\tKL\nMNO\nPQR"
259 call assert_equal(['0 1',
260 \ 'A',
261 \ 'BCDEFGHIJ',
262 \ ' KL',
263 \ 'MNO',
264 \ 'PQR',
265 \ ], getline(1, 6))
266 " Test 2:
267 " a newline is not deleted, if no newline has been added in virtual replace mode
268 %d_
269 call setline(1, ['abcd', 'efgh', 'ijkl'])
270 call cursor(2,1)
271 exe "norm! gR1234\<cr>5\<bs>\<bs>\<bs>"
272 call assert_equal(['abcd',
273 \ '123h',
274 \ 'ijkl'], getline(1, '$'))
275 " Test 3:
276 " a newline is deleted, if a newline has been inserted before in virtual replace mode
277 %d_
278 call setline(1, ['abcd', 'efgh', 'ijkl'])
279 call cursor(2,1)
280 exe "norm! gR1234\<cr>\<cr>56\<bs>\<bs>\<bs>"
281 call assert_equal(['abcd',
282 \ '1234',
283 \ 'ijkl'], getline(1, '$'))
284 " Test 4:
285 " delete add a newline, delete it, add it again and check undo
286 %d_
287 call setline(1, ['abcd', 'efgh', 'ijkl'])
288 call cursor(2,1)
289 " break undo sequence explicitly
290 let &ul = &ul
291 exe "norm! gR1234\<cr>\<bs>\<del>56\<cr>"
292 let &ul = &ul
293 call assert_equal(['abcd',
294 \ '123456',
295 \ ''], getline(1, '$'))
296 norm! u
297 call assert_equal(['abcd',
298 \ 'efgh',
299 \ 'ijkl'], getline(1, '$'))
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200300
301 " Test for truncating spaces in a newly added line using 'autoindent' if
302 " characters are not added to that line.
303 %d_
304 call setline(1, [' app', ' bee', ' cat'])
305 setlocal autoindent
306 exe "normal gg$gRt\n\nr"
307 call assert_equal([' apt', '', ' rat'], getline(1, '$'))
308
Bram Moolenaar63e82db2018-03-06 12:10:48 +0100309 " clean up
310 %d_
311 set bs&vim
Bram Moolenaar15993ce2017-10-26 20:21:44 +0200312endfunc
Bram Moolenaar6d3a1942019-01-03 23:10:32 +0100313
Bram Moolenaar81b1ba42019-01-13 16:12:40 +0100314func Test_Visual_word_textobject()
315 new
316 call setline(1, ['First sentence. Second sentence.'])
317
318 " When start and end of visual area are identical, 'aw' or 'iw' select
319 " the whole word.
320 norm! 1go2fcvawy
321 call assert_equal('Second ', @")
322 norm! 1go2fcviwy
323 call assert_equal('Second', @")
324
325 " When start and end of visual area are not identical, 'aw' or 'iw'
326 " extend the word in direction of the end of the visual area.
327 norm! 1go2fcvlawy
328 call assert_equal('cond ', @")
329 norm! gv2awy
330 call assert_equal('cond sentence.', @")
331
332 norm! 1go2fcvliwy
333 call assert_equal('cond', @")
334 norm! gv2iwy
335 call assert_equal('cond sentence', @")
336
337 " Extend visual area in opposite direction.
338 norm! 1go2fcvhawy
339 call assert_equal(' Sec', @")
340 norm! gv2awy
341 call assert_equal(' sentence. Sec', @")
342
343 norm! 1go2fcvhiwy
344 call assert_equal('Sec', @")
345 norm! gv2iwy
346 call assert_equal('. Sec', @")
347
348 bwipe!
349endfunc
350
Bram Moolenaar6d3a1942019-01-03 23:10:32 +0100351func Test_Visual_sentence_textobject()
352 new
Bram Moolenaar81b1ba42019-01-13 16:12:40 +0100353 call setline(1, ['First sentence. Second sentence. Third', 'sentence. Fourth sentence'])
Bram Moolenaar6d3a1942019-01-03 23:10:32 +0100354
355 " When start and end of visual area are identical, 'as' or 'is' select
356 " the whole sentence.
357 norm! 1gofdvasy
358 call assert_equal('Second sentence. ', @")
359 norm! 1gofdvisy
360 call assert_equal('Second sentence.', @")
361
362 " When start and end of visual area are not identical, 'as' or 'is'
363 " extend the sentence in direction of the end of the visual area.
364 norm! 1gofdvlasy
365 call assert_equal('d sentence. ', @")
366 norm! gvasy
367 call assert_equal("d sentence. Third\nsentence. ", @")
368
369 norm! 1gofdvlisy
370 call assert_equal('d sentence.', @")
371 norm! gvisy
372 call assert_equal('d sentence. ', @")
373 norm! gvisy
374 call assert_equal("d sentence. Third\nsentence.", @")
375
376 " Extend visual area in opposite direction.
377 norm! 1gofdvhasy
378 call assert_equal(' Second', @")
379 norm! gvasy
380 call assert_equal("First sentence. Second", @")
381
382 norm! 1gofdvhisy
383 call assert_equal('Second', @")
384 norm! gvisy
385 call assert_equal(' Second', @")
386 norm! gvisy
387 call assert_equal('First sentence. Second', @")
388
389 bwipe!
390endfunc
Bram Moolenaar81b1ba42019-01-13 16:12:40 +0100391
392func Test_Visual_paragraph_textobject()
393 new
Bram Moolenaar8a9bc952020-10-02 18:48:07 +0200394 let lines =<< trim [END]
395 First line.
396
397 Second line.
398 Third line.
399 Fourth line.
400 Fifth line.
401
402 Sixth line.
403 [END]
404 call setline(1, lines)
Bram Moolenaar81b1ba42019-01-13 16:12:40 +0100405
406 " When start and end of visual area are identical, 'ap' or 'ip' select
407 " the whole paragraph.
408 norm! 4ggvapy
409 call assert_equal("Second line.\nThird line.\nFourth line.\nFifth line.\n\n", @")
410 norm! 4ggvipy
411 call assert_equal("Second line.\nThird line.\nFourth line.\nFifth line.\n", @")
412
413 " When start and end of visual area are not identical, 'ap' or 'ip'
414 " extend the sentence in direction of the end of the visual area.
415 " FIXME: actually, it is not sufficient to have different start and
416 " end of visual selection, the start line and end line have to differ,
417 " which is not consistent with the documentation.
418 norm! 4ggVjapy
419 call assert_equal("Third line.\nFourth line.\nFifth line.\n\n", @")
420 norm! gvapy
421 call assert_equal("Third line.\nFourth line.\nFifth line.\n\nSixth line.\n", @")
422 norm! 4ggVjipy
423 call assert_equal("Third line.\nFourth line.\nFifth line.\n", @")
424 norm! gvipy
425 call assert_equal("Third line.\nFourth line.\nFifth line.\n\n", @")
426 norm! gvipy
427 call assert_equal("Third line.\nFourth line.\nFifth line.\n\nSixth line.\n", @")
428
429 " Extend visual area in opposite direction.
430 norm! 5ggVkapy
431 call assert_equal("\nSecond line.\nThird line.\nFourth line.\n", @")
432 norm! gvapy
433 call assert_equal("First line.\n\nSecond line.\nThird line.\nFourth line.\n", @")
434 norm! 5ggVkipy
435 call assert_equal("Second line.\nThird line.\nFourth line.\n", @")
436 norma gvipy
437 call assert_equal("\nSecond line.\nThird line.\nFourth line.\n", @")
438 norm! gvipy
439 call assert_equal("First line.\n\nSecond line.\nThird line.\nFourth line.\n", @")
440
441 bwipe!
442endfunc
Bram Moolenaar19a66852019-03-07 11:25:32 +0100443
444func Test_curswant_not_changed()
445 new
446 call setline(1, ['one', 'two'])
447 au InsertLeave * call getcurpos()
448 call feedkeys("gg0\<C-V>jI123 \<Esc>j", 'xt')
449 call assert_equal([0, 2, 1, 0, 1], getcurpos())
450
451 bwipe!
452 au! InsertLeave
453endfunc
Bram Moolenaarc6b37db2019-04-27 18:00:34 +0200454
455" Tests for "vaBiB", end could be wrong.
456func Test_Visual_Block()
457 new
458 a
459- Bug in "vPPPP" on this text:
460 {
461 cmd;
462 {
463 cmd;\t/* <-- Start cursor here */
464 {
465 }
466 }
467 }
468.
469 normal gg
470 call search('Start cursor here')
471 normal vaBiBD
472 call assert_equal(['- Bug in "vPPPP" on this text:',
473 \ "\t{",
474 \ "\t}"], getline(1, '$'))
475
476 close!
477endfunc
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +0100478
479" Test for 'p'ut in visual block mode
480func Test_visual_block_put()
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200481 new
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +0100482 call append(0, ['One', 'Two', 'Three'])
483 normal gg
484 yank
485 call feedkeys("jl\<C-V>ljp", 'xt')
486 call assert_equal(['One', 'T', 'Tee', 'One', ''], getline(1, '$'))
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200487 bw!
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +0100488endfunc
489
Bram Moolenaar309976e2019-12-05 18:16:33 +0100490" Visual modes (v V CTRL-V) followed by an operator; count; repeating
491func Test_visual_mode_op()
492 new
493 call append(0, '')
494
495 call setline(1, 'apple banana cherry')
496 call cursor(1, 1)
497 normal lvld.l3vd.
498 call assert_equal('a y', getline(1))
499
500 call setline(1, ['line 1 line 1', 'line 2 line 2', 'line 3 line 3',
501 \ 'line 4 line 4', 'line 5 line 5', 'line 6 line 6'])
502 call cursor(1, 1)
503 exe "normal Vcnewline\<Esc>j.j2Vd."
504 call assert_equal(['newline', 'newline'], getline(1, '$'))
505
506 call deletebufline('', 1, '$')
507 call setline(1, ['xxxxxxxxxxxxx', 'xxxxxxxxxxxxx', 'xxxxxxxxxxxxx',
508 \ 'xxxxxxxxxxxxx'])
509 exe "normal \<C-V>jlc \<Esc>l.l2\<C-V>c----\<Esc>l."
510 call assert_equal([' --------x',
511 \ ' --------x',
512 \ 'xxxx--------x',
513 \ 'xxxx--------x'], getline(1, '$'))
514
515 bwipe!
516endfunc
517
518" Visual mode maps (movement and text object)
519" Visual mode maps; count; repeating
520" - Simple
521" - With an Ex command (custom text object)
522func Test_visual_mode_maps()
523 new
524 call append(0, '')
525
526 func SelectInCaps()
527 let [line1, col1] = searchpos('\u', 'bcnW')
528 let [line2, col2] = searchpos('.\u', 'nW')
529 call setpos("'<", [0, line1, col1, 0])
530 call setpos("'>", [0, line2, col2, 0])
531 normal! gv
532 endfunction
533
534 vnoremap W /\u/s-1<CR>
535 vnoremap iW :<C-U>call SelectInCaps()<CR>
536
537 call setline(1, 'KiwiRaspberryDateWatermelonPeach')
538 call cursor(1, 1)
539 exe "normal vWcNo\<Esc>l.fD2vd."
540 call assert_equal('NoNoberryach', getline(1))
541
542 call setline(1, 'JambuRambutanBananaTangerineMango')
543 call cursor(1, 1)
544 exe "normal llviWc-\<Esc>l.l2vdl."
545 call assert_equal('--ago', getline(1))
546
547 vunmap W
548 vunmap iW
549 bwipe!
550 delfunc SelectInCaps
551endfunc
552
553" Operator-pending mode maps (movement and text object)
554" - Simple
555" - With Ex command moving the cursor
556" - With Ex command and Visual selection (custom text object)
557func Test_visual_oper_pending_mode_maps()
558 new
559 call append(0, '')
560
561 func MoveToCap()
562 call search('\u', 'W')
563 endfunction
564
565 func SelectInCaps()
566 let [line1, col1] = searchpos('\u', 'bcnW')
567 let [line2, col2] = searchpos('.\u', 'nW')
568 call setpos("'<", [0, line1, col1, 0])
569 call setpos("'>", [0, line2, col2, 0])
570 normal! gv
571 endfunction
572
573 onoremap W /\u/<CR>
574 onoremap <Leader>W :<C-U>call MoveToCap()<CR>
575 onoremap iW :<C-U>call SelectInCaps()<CR>
576
577 call setline(1, 'PineappleQuinceLoganberryOrangeGrapefruitKiwiZ')
578 call cursor(1, 1)
579 exe "normal cW-\<Esc>l.l2.l."
580 call assert_equal('----Z', getline(1))
581
582 call setline(1, 'JuniperDurianZ')
583 call cursor(1, 1)
584 exe "normal g?\WfD."
585 call assert_equal('WhavcreQhevnaZ', getline(1))
586
587 call setline(1, 'LemonNectarineZ')
588 call cursor(1, 1)
589 exe "normal yiWPlciWNew\<Esc>fr."
590 call assert_equal('LemonNewNewZ', getline(1))
591
592 ounmap W
593 ounmap <Leader>W
594 ounmap iW
595 bwipe!
596 delfunc MoveToCap
597 delfunc SelectInCaps
598endfunc
599
600" Patch 7.3.879: Properly abort Operator-pending mode for "dv:<Esc>" etc.
601func Test_op_pend_mode_abort()
602 new
603 call append(0, '')
604
605 call setline(1, ['zzzz', 'zzzz'])
606 call cursor(1, 1)
607
608 exe "normal dV:\<CR>dv:\<CR>"
609 call assert_equal(['zzz'], getline(1, 2))
610 set nomodifiable
611 call assert_fails('exe "normal d:\<CR>"', 'E21:')
612 set modifiable
613 call feedkeys("dv:\<Esc>dV:\<Esc>", 'xt')
614 call assert_equal(['zzz'], getline(1, 2))
615 set nomodifiable
616 let v:errmsg = ''
617 call feedkeys("d:\<Esc>", 'xt')
618 call assert_true(v:errmsg !~# '^E21:')
619 set modifiable
620
621 bwipe!
622endfunc
623
624func Test_characterwise_visual_mode()
625 new
626
627 " characterwise visual mode: replace last line
628 $put ='a'
629 let @" = 'x'
630 normal v$p
631 call assert_equal('x', getline('$'))
632
633 " characterwise visual mode: delete middle line
634 call deletebufline('', 1, '$')
635 call append('$', ['a', 'b', 'c'])
636 normal G
637 normal kkv$d
638 call assert_equal(['', 'b', 'c'], getline(1, '$'))
639
640 " characterwise visual mode: delete middle two lines
641 call deletebufline('', 1, '$')
642 call append('$', ['a', 'b', 'c'])
643 normal Gkkvj$d
644 call assert_equal(['', 'c'], getline(1, '$'))
645
646 " characterwise visual mode: delete last line
647 call deletebufline('', 1, '$')
648 call append('$', ['a', 'b', 'c'])
649 normal Gv$d
650 call assert_equal(['', 'a', 'b', ''], getline(1, '$'))
651
652 " characterwise visual mode: delete last two lines
653 call deletebufline('', 1, '$')
654 call append('$', ['a', 'b', 'c'])
655 normal Gkvj$d
656 call assert_equal(['', 'a', ''], getline(1, '$'))
657
Bram Moolenaar8a9bc952020-10-02 18:48:07 +0200658 " characterwise visual mode: use a count with the visual mode from the last
659 " line in the buffer
660 %d _
661 call setline(1, ['one', 'two', 'three', 'four'])
662 norm! vj$y
663 norm! G1vy
664 call assert_equal('four', @")
665
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200666 " characterwise visual mode: replace a single character line and the eol
667 %d _
668 call setline(1, "a")
669 normal v$rx
670 call assert_equal(['x'], getline(1, '$'))
671
Bram Moolenaar309976e2019-12-05 18:16:33 +0100672 bwipe!
673endfunc
674
Bram Moolenaar309976e2019-12-05 18:16:33 +0100675func Test_visual_mode_put()
676 new
677
678 " v_p: replace last character with line register at middle line
679 call append('$', ['aaa', 'bbb', 'ccc'])
680 normal G
681 -2yank
682 normal k$vp
683 call assert_equal(['', 'aaa', 'bb', 'aaa', '', 'ccc'], getline(1, '$'))
684
685 " v_p: replace last character with line register at middle line selecting
686 " newline
687 call deletebufline('', 1, '$')
688 call append('$', ['aaa', 'bbb', 'ccc'])
689 normal G
690 -2yank
691 normal k$v$p
692 call assert_equal(['', 'aaa', 'bb', 'aaa', 'ccc'], getline(1, '$'))
693
694 " v_p: replace last character with line register at last line
695 call deletebufline('', 1, '$')
696 call append('$', ['aaa', 'bbb', 'ccc'])
697 normal G
698 -2yank
699 normal $vp
700 call assert_equal(['', 'aaa', 'bbb', 'cc', 'aaa', ''], getline(1, '$'))
701
702 " v_p: replace last character with line register at last line selecting
703 " newline
704 call deletebufline('', 1, '$')
705 call append('$', ['aaa', 'bbb', 'ccc'])
706 normal G
707 -2yank
708 normal $v$p
709 call assert_equal(['', 'aaa', 'bbb', 'cc', 'aaa', ''], getline(1, '$'))
710
711 bwipe!
712endfunc
713
Bram Moolenaar515545e2020-03-22 14:08:59 +0100714func Test_gv_with_exclusive_selection()
Bram Moolenaar309976e2019-12-05 18:16:33 +0100715 new
716
Bram Moolenaar515545e2020-03-22 14:08:59 +0100717 " gv with exclusive selection after an operation
Bram Moolenaar309976e2019-12-05 18:16:33 +0100718 call append('$', ['zzz ', 'äà '])
719 set selection=exclusive
720 normal Gkv3lyjv3lpgvcxxx
721 call assert_equal(['', 'zzz ', 'xxx '], getline(1, '$'))
722
Bram Moolenaar515545e2020-03-22 14:08:59 +0100723 " gv with exclusive selection without an operation
Bram Moolenaar309976e2019-12-05 18:16:33 +0100724 call deletebufline('', 1, '$')
725 call append('$', 'zzz ')
726 set selection=exclusive
727 exe "normal G0v3l\<Esc>gvcxxx"
728 call assert_equal(['', 'xxx '], getline(1, '$'))
729
730 set selection&vim
731 bwipe!
732endfunc
733
Bram Moolenaar1f3e7d32019-12-06 20:43:36 +0100734" Tests for the visual block mode commands
735func Test_visual_block_mode()
736 new
737 call append(0, '')
Bram Moolenaar1671f442020-03-10 07:48:13 +0100738 call setline(1, repeat(['abcdefghijklm'], 5))
Bram Moolenaar1f3e7d32019-12-06 20:43:36 +0100739 call cursor(1, 1)
740
741 " Test shift-right of a block
742 exe "normal jllll\<C-V>jj>wll\<C-V>jlll>"
743 " Test shift-left of a block
744 exe "normal G$hhhh\<C-V>kk<"
745 " Test block-insert
746 exe "normal Gkl\<C-V>kkkIxyz"
747 " Test block-replace
748 exe "normal Gllll\<C-V>kkklllrq"
749 " Test block-change
750 exe "normal G$khhh\<C-V>hhkkcmno"
751 call assert_equal(['axyzbcdefghijklm',
752 \ 'axyzqqqq mno ghijklm',
753 \ 'axyzqqqqef mno ghijklm',
754 \ 'axyzqqqqefgmnoklm',
755 \ 'abcdqqqqijklm'], getline(1, 5))
756
Bram Moolenaar1671f442020-03-10 07:48:13 +0100757 " Test 'C' to change till the end of the line
758 call cursor(3, 4)
759 exe "normal! \<C-V>j3lCooo"
760 call assert_equal(['axyooo', 'axyooo'], getline(3, 4))
761
762 " Test 'D' to delete till the end of the line
763 call cursor(3, 3)
764 exe "normal! \<C-V>j2lD"
765 call assert_equal(['ax', 'ax'], getline(3, 4))
766
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200767 " Test block insert with a short line that ends before the block
768 %d _
769 call setline(1, [" one", "a", " two"])
770 exe "normal gg\<C-V>2jIx"
771 call assert_equal([" xone", "a", " xtwo"], getline(1, '$'))
772
773 " Test block append at EOL with '$' and without '$'
774 %d _
775 call setline(1, ["one", "a", "two"])
776 exe "normal gg$\<C-V>2jAx"
777 call assert_equal(["onex", "ax", "twox"], getline(1, '$'))
778 %d _
779 call setline(1, ["one", "a", "two"])
780 exe "normal gg3l\<C-V>2jAx"
781 call assert_equal(["onex", "a x", "twox"], getline(1, '$'))
782
783 " Test block replace with an empty line in the middle and use $ to jump to
784 " the end of the line.
785 %d _
786 call setline(1, ['one', '', 'two'])
787 exe "normal gg$\<C-V>2jrx"
788 call assert_equal(["onx", "", "twx"], getline(1, '$'))
789
790 " Test block replace with an empty line in the middle and move cursor to the
791 " end of the line
792 %d _
793 call setline(1, ['one', '', 'two'])
794 exe "normal gg2l\<C-V>2jrx"
795 call assert_equal(["onx", "", "twx"], getline(1, '$'))
796
797 " Replace odd number of characters with a multibyte character
798 %d _
799 call setline(1, ['abcd', 'efgh'])
800 exe "normal ggl\<C-V>2ljr\u1100"
801 call assert_equal(["a\u1100 ", "e\u1100 "], getline(1, '$'))
802
803 " During visual block append, if the cursor moved outside of the selected
804 " range, then the edit should not be applied to the block.
805 %d _
806 call setline(1, ['aaa', 'bbb', 'ccc'])
807 exe "normal 2G\<C-V>jAx\<Up>"
808 call assert_equal(['aaa', 'bxbb', 'ccc'], getline(1, '$'))
809
810 " During visual block append, if the cursor is moved before the start of the
811 " block, then the new text should be appended there.
812 %d _
813 call setline(1, ['aaa', 'bbb', 'ccc'])
814 exe "normal $\<C-V>2jA\<Left>x"
Bram Moolenaar4067bd32021-06-29 18:54:35 +0200815 call assert_equal(['aaxa', 'bbxb', 'ccxc'], getline(1, '$'))
Yegappan Lakshmanan59b26232021-06-05 20:59:22 +0200816 " Repeat the previous test but use 'l' to move the cursor instead of '$'
817 call setline(1, ['aaa', 'bbb', 'ccc'])
818 exe "normal! gg2l\<C-V>2jA\<Left>x"
819 call assert_equal(['aaxa', 'bbxb', 'ccxc'], getline(1, '$'))
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200820
821 " Change a characterwise motion to a blockwise motion using CTRL-V
822 %d _
823 call setline(1, ['123', '456', '789'])
824 exe "normal ld\<C-V>j"
825 call assert_equal(['13', '46', '789'], getline(1, '$'))
826
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +0200827 " Test from ':help v_b_I_example'
828 %d _
829 setlocal tabstop=8 shiftwidth=4
830 let lines =<< trim END
831 abcdefghijklmnopqrstuvwxyz
832 abc defghijklmnopqrstuvwxyz
833 abcdef ghi jklmnopqrstuvwxyz
834 abcdefghijklmnopqrstuvwxyz
835 END
836 call setline(1, lines)
837 exe "normal ggfo\<C-V>3jISTRING"
838 let expected =<< trim END
839 abcdefghijklmnSTRINGopqrstuvwxyz
840 abc STRING defghijklmnopqrstuvwxyz
841 abcdef ghi STRING jklmnopqrstuvwxyz
842 abcdefghijklmnSTRINGopqrstuvwxyz
843 END
844 call assert_equal(expected, getline(1, '$'))
845
846 " Test from ':help v_b_A_example'
847 %d _
848 let lines =<< trim END
849 abcdefghijklmnopqrstuvwxyz
850 abc defghijklmnopqrstuvwxyz
851 abcdef ghi jklmnopqrstuvwxyz
852 abcdefghijklmnopqrstuvwxyz
853 END
854 call setline(1, lines)
855 exe "normal ggfo\<C-V>3j$ASTRING"
856 let expected =<< trim END
857 abcdefghijklmnopqrstuvwxyzSTRING
858 abc defghijklmnopqrstuvwxyzSTRING
859 abcdef ghi jklmnopqrstuvwxyzSTRING
860 abcdefghijklmnopqrstuvwxyzSTRING
861 END
862 call assert_equal(expected, getline(1, '$'))
863
864 " Test from ':help v_b_<_example'
865 %d _
866 let lines =<< trim END
867 abcdefghijklmnopqrstuvwxyz
868 abc defghijklmnopqrstuvwxyz
869 abcdef ghi jklmnopqrstuvwxyz
870 abcdefghijklmnopqrstuvwxyz
871 END
872 call setline(1, lines)
873 exe "normal ggfo\<C-V>3j3l<.."
874 let expected =<< trim END
875 abcdefghijklmnopqrstuvwxyz
876 abc defghijklmnopqrstuvwxyz
877 abcdef ghi jklmnopqrstuvwxyz
878 abcdefghijklmnopqrstuvwxyz
879 END
880 call assert_equal(expected, getline(1, '$'))
881
882 " Test from ':help v_b_>_example'
883 %d _
884 let lines =<< trim END
885 abcdefghijklmnopqrstuvwxyz
886 abc defghijklmnopqrstuvwxyz
887 abcdef ghi jklmnopqrstuvwxyz
888 abcdefghijklmnopqrstuvwxyz
889 END
890 call setline(1, lines)
891 exe "normal ggfo\<C-V>3j>.."
892 let expected =<< trim END
893 abcdefghijklmn opqrstuvwxyz
894 abc defghijklmnopqrstuvwxyz
895 abcdef ghi jklmnopqrstuvwxyz
896 abcdefghijklmn opqrstuvwxyz
897 END
898 call assert_equal(expected, getline(1, '$'))
899
900 " Test from ':help v_b_r_example'
901 %d _
902 let lines =<< trim END
903 abcdefghijklmnopqrstuvwxyz
904 abc defghijklmnopqrstuvwxyz
905 abcdef ghi jklmnopqrstuvwxyz
906 abcdefghijklmnopqrstuvwxyz
907 END
908 call setline(1, lines)
909 exe "normal ggfo\<C-V>5l3jrX"
910 let expected =<< trim END
911 abcdefghijklmnXXXXXXuvwxyz
912 abc XXXXXXhijklmnopqrstuvwxyz
913 abcdef ghi XXXXXX jklmnopqrstuvwxyz
914 abcdefghijklmnXXXXXXuvwxyz
915 END
916 call assert_equal(expected, getline(1, '$'))
917
Bram Moolenaar1f3e7d32019-12-06 20:43:36 +0100918 bwipe!
Yegappan Lakshmanan2ac71842021-05-31 19:23:01 +0200919 set tabstop& shiftwidth&
Bram Moolenaar1f3e7d32019-12-06 20:43:36 +0100920endfunc
921
Bram Moolenaar21492742021-06-04 21:57:57 +0200922func Test_visual_force_motion_feedkeys()
923 onoremap <expr> i- execute('let g:mode = mode(1)')->slice(0, 0)
924 call feedkeys('dvi-', 'x')
925 call assert_equal('nov', g:mode)
926 call feedkeys('di-', 'x')
927 call assert_equal('no', g:mode)
928 ounmap i-
929endfunc
930
Bram Moolenaar1f3e7d32019-12-06 20:43:36 +0100931" Test block-insert using cursor keys for movement
932func Test_visual_block_insert_cursor_keys()
933 new
934 call append(0, ['aaaaaa', 'bbbbbb', 'cccccc', 'dddddd'])
935 call cursor(1, 1)
936
937 exe "norm! l\<C-V>jjjlllI\<Right>\<Right> \<Esc>"
938 call assert_equal(['aaa aaa', 'bbb bbb', 'ccc ccc', 'ddd ddd'],
939 \ getline(1, 4))
940
941 call deletebufline('', 1, '$')
942 call setline(1, ['xaaa', 'bbbb', 'cccc', 'dddd'])
943 call cursor(1, 1)
944 exe "norm! \<C-V>jjjI<>\<Left>p\<Esc>"
945 call assert_equal(['<p>xaaa', '<p>bbbb', '<p>cccc', '<p>dddd'],
946 \ getline(1, 4))
947 bwipe!
948endfunc
949
950func Test_visual_block_create()
951 new
952 call append(0, '')
953 " Test for Visual block was created with the last <C-v>$
954 call setline(1, ['A23', '4567'])
955 call cursor(1, 1)
956 exe "norm! l\<C-V>j$Aab\<Esc>"
957 call assert_equal(['A23ab', '4567ab'], getline(1, 2))
958
959 " Test for Visual block was created with the middle <C-v>$ (1)
960 call deletebufline('', 1, '$')
961 call setline(1, ['B23', '4567'])
962 call cursor(1, 1)
963 exe "norm! l\<C-V>j$hAab\<Esc>"
964 call assert_equal(['B23 ab', '4567ab'], getline(1, 2))
965
966 " Test for Visual block was created with the middle <C-v>$ (2)
967 call deletebufline('', 1, '$')
968 call setline(1, ['C23', '4567'])
969 call cursor(1, 1)
970 exe "norm! l\<C-V>j$hhAab\<Esc>"
971 call assert_equal(['C23ab', '456ab7'], getline(1, 2))
972 bwipe!
973endfunc
974
975" Test for Visual block insert when virtualedit=all
976func Test_virtualedit_visual_block()
977 set ve=all
978 new
979 call append(0, ["\t\tline1", "\t\tline2", "\t\tline3"])
980 call cursor(1, 1)
981 exe "norm! 07l\<C-V>jjIx\<Esc>"
982 call assert_equal([" x \tline1",
983 \ " x \tline2",
984 \ " x \tline3"], getline(1, 3))
985
986 " Test for Visual block append when virtualedit=all
987 exe "norm! 012l\<C-v>jjAx\<Esc>"
988 call assert_equal([' x x line1',
989 \ ' x x line2',
990 \ ' x x line3'], getline(1, 3))
991 set ve=
992 bwipe!
993endfunc
994
995" Test for changing case
996func Test_visual_change_case()
997 new
998 " gUe must uppercase a whole word, also when ß changes to SS
999 exe "normal Gothe youtußeuu end\<Esc>Ypk0wgUe\r"
1000 " gUfx must uppercase until x, inclusive.
1001 exe "normal O- youßtußexu -\<Esc>0fogUfx\r"
1002 " VU must uppercase a whole line
1003 exe "normal YpkVU\r"
1004 " same, when it's the last line in the buffer
1005 exe "normal YPGi111\<Esc>VUddP\r"
1006 " Uppercase two lines
1007 exe "normal Oblah di\rdoh dut\<Esc>VkUj\r"
1008 " Uppercase part of two lines
1009 exe "normal ddppi333\<Esc>k0i222\<Esc>fyllvjfuUk"
1010 call assert_equal(['the YOUTUSSEUU end', '- yOUSSTUSSEXu -',
1011 \ 'THE YOUTUSSEUU END', '111THE YOUTUSSEUU END', 'BLAH DI', 'DOH DUT',
1012 \ '222the yoUTUSSEUU END', '333THE YOUTUßeuu end'], getline(2, '$'))
1013 bwipe!
1014endfunc
1015
1016" Test for Visual replace using Enter or NL
1017func Test_visual_replace_crnl()
1018 new
1019 exe "normal G3o123456789\e2k05l\<C-V>2jr\r"
1020 exe "normal G3o98765\e2k02l\<C-V>2jr\<C-V>\r\n"
1021 exe "normal G3o123456789\e2k05l\<C-V>2jr\n"
1022 exe "normal G3o98765\e2k02l\<C-V>2jr\<C-V>\n"
1023 call assert_equal(['12345', '789', '12345', '789', '12345', '789', "98\r65",
1024 \ "98\r65", "98\r65", '12345', '789', '12345', '789', '12345', '789',
1025 \ "98\n65", "98\n65", "98\n65"], getline(2, '$'))
1026 bwipe!
1027endfunc
1028
1029func Test_ve_block_curpos()
1030 new
1031 " Test cursor position. When ve=block and Visual block mode and $gj
1032 call append(0, ['12345', '789'])
1033 call cursor(1, 3)
1034 set virtualedit=block
1035 exe "norm! \<C-V>$gj\<Esc>"
1036 call assert_equal([0, 2, 4, 0], getpos("'>"))
1037 set virtualedit=
1038 bwipe!
1039endfunc
1040
1041" Test for block_insert when replacing spaces in front of the a with tabs
1042func Test_block_insert_replace_tabs()
1043 new
1044 set ts=8 sts=4 sw=4
1045 call append(0, ["#define BO_ALL\t 0x0001",
1046 \ "#define BO_BS\t 0x0002",
1047 \ "#define BO_CRSR\t 0x0004"])
1048 call cursor(1, 1)
1049 exe "norm! f0\<C-V>2jI\<tab>\<esc>"
1050 call assert_equal([
1051 \ "#define BO_ALL\t\t0x0001",
1052 \ "#define BO_BS\t \t0x0002",
1053 \ "#define BO_CRSR\t \t0x0004", ''], getline(1, '$'))
1054 set ts& sts& sw&
1055 bwipe!
1056endfunc
1057
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01001058" Test for * register in :
1059func Test_star_register()
1060 call assert_fails('*bfirst', 'E16:')
1061 new
1062 call setline(1, ['foo', 'bar', 'baz', 'qux'])
1063 exe "normal jVj\<ESC>"
1064 *yank r
1065 call assert_equal("bar\nbaz\n", @r)
1066
1067 delmarks < >
1068 call assert_fails('*yank', 'E20:')
1069 close!
1070endfunc
1071
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01001072" Test for changing text in visual mode with 'exclusive' selection
1073func Test_exclusive_selection()
1074 new
1075 call setline(1, ['one', 'two'])
1076 set selection=exclusive
1077 call feedkeys("vwcabc", 'xt')
1078 call assert_equal('abctwo', getline(1))
1079 call setline(1, ["\tone"])
1080 set virtualedit=all
1081 call feedkeys('0v2lcl', 'xt')
1082 call assert_equal('l one', getline(1))
1083 set virtualedit&
1084 set selection&
1085 close!
1086endfunc
1087
Bram Moolenaard1ad99b2020-10-04 16:16:54 +02001088" Test for starting linewise visual with a count.
1089" This test needs to be run without any previous visual mode. Otherwise the
1090" count will use the count from the previous visual mode.
1091func Test_linewise_visual_with_count()
1092 let after =<< trim [CODE]
1093 call setline(1, ['one', 'two', 'three', 'four'])
1094 norm! 3Vy
1095 call assert_equal("one\ntwo\nthree\n", @")
1096 call writefile(v:errors, 'Xtestout')
1097 qall!
1098 [CODE]
1099 if RunVim([], after, '')
1100 call assert_equal([], readfile('Xtestout'))
1101 call delete('Xtestout')
1102 endif
1103endfunc
1104
1105" Test for starting characterwise visual with a count.
1106" This test needs to be run without any previous visual mode. Otherwise the
1107" count will use the count from the previous visual mode.
1108func Test_characterwise_visual_with_count()
1109 let after =<< trim [CODE]
1110 call setline(1, ['one two', 'three'])
1111 norm! l5vy
1112 call assert_equal("ne tw", @")
1113 call writefile(v:errors, 'Xtestout')
1114 qall!
1115 [CODE]
1116 if RunVim([], after, '')
1117 call assert_equal([], readfile('Xtestout'))
1118 call delete('Xtestout')
1119 endif
Bram Moolenaarf5f1e102020-03-08 05:13:15 +01001120endfunc
1121
Bram Moolenaar224a5f12020-04-28 20:29:07 +02001122" Test for visually selecting an inner block (iB)
1123func Test_visual_inner_block()
1124 new
1125 call setline(1, ['one', '{', 'two', '{', 'three', '}', 'four', '}', 'five'])
1126 call cursor(5, 1)
1127 " visually select all the lines in the block and then execute iB
1128 call feedkeys("ViB\<C-C>", 'xt')
1129 call assert_equal([0, 5, 1, 0], getpos("'<"))
1130 call assert_equal([0, 5, 6, 0], getpos("'>"))
1131 " visually select two inner blocks
1132 call feedkeys("ViBiB\<C-C>", 'xt')
1133 call assert_equal([0, 3, 1, 0], getpos("'<"))
1134 call assert_equal([0, 7, 5, 0], getpos("'>"))
1135 " try to select non-existing inner block
1136 call cursor(5, 1)
1137 call assert_beeps('normal ViBiBiB')
1138 " try to select a unclosed inner block
1139 8,9d
1140 call cursor(5, 1)
1141 call assert_beeps('normal ViBiB')
1142 close!
1143endfunc
1144
Bram Moolenaarcd942772020-08-22 21:08:44 +02001145func Test_visual_put_in_block()
1146 new
1147 call setline(1, ['xxxx', 'yyy', 'zzzz'])
1148 normal 1G2yl
1149 exe "normal 1G2l\<C-V>jjlp"
1150 call assert_equal(['xxxx', 'yxx', 'zzxx'], getline(1, 3))
1151 bwipe!
1152endfunc
1153
Christian Brabandt2fa93842021-05-30 22:17:25 +02001154func Test_visual_put_in_block_using_zp()
1155 new
1156 " paste using zP
1157 call setline(1, ['/path;text', '/path;text', '/path;text', '',
1158 \ '/subdir',
1159 \ '/longsubdir',
1160 \ '/longlongsubdir'])
1161 exe "normal! 5G\<c-v>2j$y"
1162 norm! 1Gf;zP
1163 call assert_equal(['/path/subdir;text', '/path/longsubdir;text', '/path/longlongsubdir;text'], getline(1, 3))
1164 %d
1165 " paste using zP
1166 call setline(1, ['/path;text', '/path;text', '/path;text', '',
1167 \ '/subdir',
1168 \ '/longsubdir',
1169 \ '/longlongsubdir'])
1170 exe "normal! 5G\<c-v>2j$y"
1171 norm! 1Gf;hzp
1172 call assert_equal(['/path/subdir;text', '/path/longsubdir;text', '/path/longlongsubdir;text'], getline(1, 3))
1173 bwipe!
1174endfunc
1175
Christian Brabandt544a38e2021-06-10 19:39:11 +02001176func Test_visual_put_in_block_using_zy_and_zp()
1177 new
1178
1179 " Test 1) Paste using zp - after the cursor without trailing spaces
1180 call setline(1, ['/path;text', '/path;text', '/path;text', '',
1181 \ 'texttext /subdir columntext',
1182 \ 'texttext /longsubdir columntext',
1183 \ 'texttext /longlongsubdir columntext'])
1184 exe "normal! 5G0f/\<c-v>2jezy"
1185 norm! 1G0f;hzp
1186 call assert_equal(['/path/subdir;text', '/path/longsubdir;text', '/path/longlongsubdir;text'], getline(1, 3))
1187
1188 " Test 2) Paste using zP - in front of the cursor without trailing spaces
1189 %d
1190 call setline(1, ['/path;text', '/path;text', '/path;text', '',
1191 \ 'texttext /subdir columntext',
1192 \ 'texttext /longsubdir columntext',
1193 \ 'texttext /longlongsubdir columntext'])
1194 exe "normal! 5G0f/\<c-v>2jezy"
1195 norm! 1G0f;zP
1196 call assert_equal(['/path/subdir;text', '/path/longsubdir;text', '/path/longlongsubdir;text'], getline(1, 3))
1197
1198 " Test 3) Paste using p - with trailing spaces
1199 %d
1200 call setline(1, ['/path;text', '/path;text', '/path;text', '',
1201 \ 'texttext /subdir columntext',
1202 \ 'texttext /longsubdir columntext',
1203 \ 'texttext /longlongsubdir columntext'])
1204 exe "normal! 5G0f/\<c-v>2jezy"
1205 norm! 1G0f;hp
1206 call assert_equal(['/path/subdir ;text', '/path/longsubdir ;text', '/path/longlongsubdir;text'], getline(1, 3))
1207
1208 " Test 4) Paste using P - with trailing spaces
1209 %d
1210 call setline(1, ['/path;text', '/path;text', '/path;text', '',
1211 \ 'texttext /subdir columntext',
1212 \ 'texttext /longsubdir columntext',
1213 \ 'texttext /longlongsubdir columntext'])
1214 exe "normal! 5G0f/\<c-v>2jezy"
1215 norm! 1G0f;P
1216 call assert_equal(['/path/subdir ;text', '/path/longsubdir ;text', '/path/longlongsubdir;text'], getline(1, 3))
1217
1218 " Test 5) Yank with spaces inside the block
1219 %d
1220 call setline(1, ['/path;text', '/path;text', '/path;text', '',
1221 \ 'texttext /sub dir/ columntext',
1222 \ 'texttext /lon gsubdir/ columntext',
1223 \ 'texttext /lon glongsubdir/ columntext'])
1224 exe "normal! 5G0f/\<c-v>2jf/zy"
1225 norm! 1G0f;zP
1226 call assert_equal(['/path/sub dir/;text', '/path/lon gsubdir/;text', '/path/lon glongsubdir/;text'], getline(1, 3))
1227 bwipe!
1228endfunc
1229
Bram Moolenaar7d7bcc62021-06-28 21:54:27 +02001230func Test_visual_put_blockedit_zy_and_zp()
1231 new
1232
1233 call setline(1, ['aa', 'bbbbb', 'ccc', '', 'XX', 'GGHHJ', 'RTZU'])
1234 exe "normal! gg0\<c-v>2j$zy"
1235 norm! 5gg0zP
1236 call assert_equal(['aa', 'bbbbb', 'ccc', '', 'aaXX', 'bbbbbGGHHJ', 'cccRTZU'], getline(1, 7))
1237 "
1238 " now with blockmode editing
1239 sil %d
1240 :set ve=block
1241 call setline(1, ['aa', 'bbbbb', 'ccc', '', 'XX', 'GGHHJ', 'RTZU'])
1242 exe "normal! gg0\<c-v>2j$zy"
1243 norm! 5gg0zP
1244 call assert_equal(['aa', 'bbbbb', 'ccc', '', 'aaXX', 'bbbbbGGHHJ', 'cccRTZU'], getline(1, 7))
1245 set ve&vim
1246 bw!
1247endfunc
1248
Bram Moolenaar9cee4a12021-07-03 15:08:37 +02001249func Test_visual_block_with_virtualedit()
1250 CheckScreendump
1251
1252 let lines =<< trim END
1253 call setline(1, ['aaaaaa', 'bbbb', 'cc'])
1254 set virtualedit=block
1255 normal G
1256 END
1257 call writefile(lines, 'XTest_block')
1258
1259 let buf = RunVimInTerminal('-S XTest_block', {'rows': 8, 'cols': 50})
1260 call term_sendkeys(buf, "\<C-V>gg$")
1261 call VerifyScreenDump(buf, 'Test_visual_block_with_virtualedit', {})
1262
Bram Moolenaarb17ab862021-07-03 22:15:17 +02001263 call term_sendkeys(buf, "\<Esc>gg\<C-V>G$")
1264 call VerifyScreenDump(buf, 'Test_visual_block_with_virtualedit2', {})
1265
Bram Moolenaar9cee4a12021-07-03 15:08:37 +02001266 " clean up
1267 call term_sendkeys(buf, "\<Esc>")
1268 call StopVimInTerminal(buf)
Dominique Pelle6c72fd52021-07-04 12:30:06 +02001269 call delete('XTest_block')
Bram Moolenaar9cee4a12021-07-03 15:08:37 +02001270endfunc
1271
Bram Moolenaar615ddd52021-11-17 18:00:31 +00001272func Test_visual_block_ctrl_w_f()
1273 " Emtpy block selected in new buffer should not result in an error.
1274 au! BufNew foo sil norm f
1275 edit foo
1276
1277 au! BufNew
1278endfunc
1279
Bram Moolenaarb07626d2021-10-11 15:40:43 +01001280func Test_visual_reselect_with_count()
1281 " this was causing an illegal memory access
1282 let lines =<< trim END
1283
1284
1285
1286 :
1287 r<sfile>
1288 exe "%norm e3\<c-v>kr\t"
1289 :
1290
1291 :
1292 END
1293 call writefile(lines, 'XvisualReselect')
1294 source XvisualReselect
1295
1296 bwipe!
1297 call delete('XvisualReselect')
1298endfunc
1299
Christian Brabandt544a38e2021-06-10 19:39:11 +02001300
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +01001301" vim: shiftwidth=2 sts=2 expandtab