blob: f3cc2602510d377a1f79e2be91fbb3fcb6ba6507 [file] [log] [blame]
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001" Tests for 'virtualedit'.
2
3func Test_yank_move_change()
Bram Moolenaard41babe2017-08-30 17:01:35 +02004 new
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005 call setline(1, [
6 \ "func foo() error {",
7 \ "\tif n, err := bar();",
8 \ "\terr != nil {",
9 \ "\t\treturn err",
10 \ "\t}",
11 \ "\tn = n * n",
12 \ ])
13 set virtualedit=all
14 set ts=4
15 function! MoveSelectionDown(count) abort
16 normal! m`
17 silent! exe "'<,'>move'>+".a:count
18 norm! ``
19 endfunction
20
21 xmap ]e :<C-U>call MoveSelectionDown(v:count1)<CR>
22 2
23 normal 2gg
24 normal J
25 normal jVj
26 normal ]e
27 normal ce
28 bwipe!
29 set virtualedit=
30 set ts=8
31endfunc
Bram Moolenaard41babe2017-08-30 17:01:35 +020032
33func Test_paste_end_of_line()
34 new
35 set virtualedit=all
36 call setline(1, ['456', '123'])
37 normal! gg0"ay$
38 exe "normal! 2G$lllA\<C-O>:normal! \"agP\r"
39 call assert_equal('123456', getline(2))
40
41 bwipe!
42 set virtualedit=
43endfunc
Bram Moolenaardb0eede2018-04-25 22:38:17 +020044
Bram Moolenaar630afe82018-06-28 19:26:28 +020045func Test_replace_end_of_line()
46 new
47 set virtualedit=all
48 call setline(1, range(20))
49 exe "normal! gg2jv10lr-"
50 call assert_equal(["1", "-----------", "3"], getline(2,4))
Bram Moolenaar30276f22019-01-24 17:59:39 +010051 call setline(1, range(20))
52 exe "normal! gg2jv10lr\<c-k>hh"
53 call assert_equal(["1", "───────────", "3"], getline(2,4))
Bram Moolenaar630afe82018-06-28 19:26:28 +020054
55 bwipe!
56 set virtualedit=
57endfunc
58
Bram Moolenaardb0eede2018-04-25 22:38:17 +020059func Test_edit_CTRL_G()
60 new
61 set virtualedit=insert
62 call setline(1, ['123', '1', '12'])
63 exe "normal! ggA\<c-g>jx\<c-g>jx"
64 call assert_equal(['123', '1 x', '12 x'], getline(1,'$'))
65
66 set virtualedit=all
67 %d_
68 call setline(1, ['1', '12'])
69 exe "normal! ggllix\<c-g>jx"
70 call assert_equal(['1 x', '12x'], getline(1,'$'))
71
72
73 bwipe!
74 set virtualedit=
75endfunc
Bram Moolenaar77ccc002019-10-31 03:21:25 +010076
77func Test_edit_change()
78 new
79 set virtualedit=all
80 call setline(1, "\t⒌")
81 normal Cx
82 call assert_equal('x', getline(1))
Bram Moolenaar3e72dca2021-05-29 16:30:12 +020083 " Do a visual block change
84 call setline(1, ['a', 'b', 'c'])
85 exe "normal gg3l\<C-V>2jcx"
86 call assert_equal(['a x', 'b x', 'c x'], getline(1, '$'))
Bram Moolenaar77ccc002019-10-31 03:21:25 +010087 bwipe!
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +010088 set virtualedit=
89endfunc
90
Bram Moolenaar54c8d222019-12-02 20:41:39 +010091" Tests for pasting at the beginning, end and middle of a tab character
92" in virtual edit mode.
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +010093func Test_paste_in_tab()
94 new
Bram Moolenaar54c8d222019-12-02 20:41:39 +010095 call append(0, '')
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +010096 set virtualedit=all
Bram Moolenaar54c8d222019-12-02 20:41:39 +010097
98 " Tests for pasting a register with characterwise mode type
99 call setreg('"', 'xyz', 'c')
100
101 " paste (p) unnamed register at the beginning of a tab
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +0100102 call setline(1, "a\tb")
Bram Moolenaar54c8d222019-12-02 20:41:39 +0100103 call cursor(1, 2, 0)
104 normal p
105 call assert_equal('a xyz b', getline(1))
106
107 " paste (P) unnamed register at the beginning of a tab
108 call setline(1, "a\tb")
109 call cursor(1, 2, 0)
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +0100110 normal P
111 call assert_equal("axyz\tb", getline(1))
112
Bram Moolenaar54c8d222019-12-02 20:41:39 +0100113 " paste (p) unnamed register at the end of a tab
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +0100114 call setline(1, "a\tb")
115 call cursor(1, 2, 6)
116 normal p
117 call assert_equal("a\txyzb", getline(1))
Bram Moolenaar54c8d222019-12-02 20:41:39 +0100118
119 " paste (P) unnamed register at the end of a tab
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +0100120 call setline(1, "a\tb")
121 call cursor(1, 2, 6)
122 normal P
Bram Moolenaar54c8d222019-12-02 20:41:39 +0100123 call assert_equal('a xyz b', getline(1))
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +0100124
Bram Moolenaar54c8d222019-12-02 20:41:39 +0100125 " Tests for pasting a register with blockwise mode type
126 call setreg('"', 'xyz', 'b')
127
128 " paste (p) unnamed register at the beginning of a tab
129 call setline(1, "a\tb")
130 call cursor(1, 2, 0)
131 normal p
132 call assert_equal('a xyz b', getline(1))
133
134 " paste (P) unnamed register at the beginning of a tab
135 call setline(1, "a\tb")
136 call cursor(1, 2, 0)
137 normal P
138 call assert_equal("axyz\tb", getline(1))
139
140 " paste (p) unnamed register at the end of a tab
141 call setline(1, "a\tb")
142 call cursor(1, 2, 6)
143 normal p
144 call assert_equal("a\txyzb", getline(1))
145
146 " paste (P) unnamed register at the end of a tab
147 call setline(1, "a\tb")
148 call cursor(1, 2, 6)
149 normal P
150 call assert_equal('a xyz b', getline(1))
151
152 " Tests for pasting with gp and gP in virtual edit mode
153
154 " paste (gp) unnamed register at the beginning of a tab
155 call setline(1, "a\tb")
156 call cursor(1, 2, 0)
157 normal gp
158 call assert_equal('a xyz b', getline(1))
159 call assert_equal([0, 1, 12, 0, 12], getcurpos())
160
161 " paste (gP) unnamed register at the beginning of a tab
162 call setline(1, "a\tb")
163 call cursor(1, 2, 0)
164 normal gP
165 call assert_equal("axyz\tb", getline(1))
166 call assert_equal([0, 1, 5, 0, 5], getcurpos())
167
168 " paste (gp) unnamed register at the end of a tab
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +0100169 call setline(1, "a\tb")
170 call cursor(1, 2, 6)
171 normal gp
172 call assert_equal("a\txyzb", getline(1))
173 call assert_equal([0, 1, 6, 0, 12], getcurpos())
Bram Moolenaar54c8d222019-12-02 20:41:39 +0100174
175 " paste (gP) unnamed register at the end of a tab
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +0100176 call setline(1, "a\tb")
177 call cursor(1, 2, 6)
178 normal gP
Bram Moolenaar54c8d222019-12-02 20:41:39 +0100179 call assert_equal('a xyz b', getline(1))
180 call assert_equal([0, 1, 12, 0, 12], getcurpos())
181
182 " Tests for pasting a named register
183 let @r = 'xyz'
184
185 " paste (gp) named register in the middle of a tab
186 call setline(1, "a\tb")
187 call cursor(1, 2, 2)
188 normal "rgp
189 call assert_equal('a xyz b', getline(1))
190 call assert_equal([0, 1, 8, 0, 8], getcurpos())
191
192 " paste (gP) named register in the middle of a tab
193 call setline(1, "a\tb")
194 call cursor(1, 2, 2)
195 normal "rgP
196 call assert_equal('a xyz b', getline(1))
197 call assert_equal([0, 1, 7, 0, 7], getcurpos())
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +0100198
199 bwipe!
200 set virtualedit=
Bram Moolenaar77ccc002019-10-31 03:21:25 +0100201endfunc
Bram Moolenaar54c8d222019-12-02 20:41:39 +0100202
203" Test for yanking a few spaces within a tab to a register
204func Test_yank_in_tab()
205 new
206 let @r = ''
207 call setline(1, "a\tb")
208 set virtualedit=all
209 call cursor(1, 2, 2)
210 normal "ry5l
211 call assert_equal(' ', @r)
212
213 bwipe!
214 set virtualedit=
215endfunc
216
Bram Moolenaar079119b2019-12-03 22:59:23 +0100217" Insert "keyword keyw", ESC, C CTRL-N, shows "keyword ykeyword".
218" Repeating CTRL-N fixes it. (Mary Ellen Foster)
219func Test_ve_completion()
220 new
221 set completeopt&vim
222 set virtualedit=all
223 exe "normal ikeyword keyw\<Esc>C\<C-N>"
224 call assert_equal('keyword keyword', getline(1))
225 bwipe!
226 set virtualedit=
227endfunc
228
229" Using "C" then then <CR> moves the last remaining character to the next
230" line. (Mary Ellen Foster)
231func Test_ve_del_to_eol()
232 new
233 set virtualedit=all
234 call append(0, 'all your base are belong to us')
235 call search('are', 'w')
236 exe "normal C\<CR>are belong to vim"
237 call assert_equal(['all your base ', 'are belong to vim'], getline(1, 2))
238 bwipe!
239 set virtualedit=
240endfunc
241
242" When past the end of a line that ends in a single character "b" skips
243" that word.
244func Test_ve_b_past_eol()
245 new
246 set virtualedit=all
247 call append(0, '1 2 3 4 5 6')
248 normal gg^$15lbC7
249 call assert_equal('1 2 3 4 5 7', getline(1))
250 bwipe!
251 set virtualedit=
252endfunc
253
254" Make sure 'i', 'C', 'a', 'A' and 'D' works
255func Test_ve_ins_del()
256 new
257 set virtualedit=all
258 call append(0, ["'i'", "'C'", "'a'", "'A'", "'D'"])
259 call cursor(1, 1)
260 normal $4lix
261 call assert_equal("'i' x", getline(1))
262 call cursor(2, 1)
263 normal $4lCx
264 call assert_equal("'C' x", getline(2))
265 call cursor(3, 1)
266 normal $4lax
267 call assert_equal("'a' x", getline(3))
268 call cursor(4, 1)
269 normal $4lAx
270 call assert_equal("'A'x", getline(4))
271 call cursor(5, 1)
272 normal $4lDix
273 call assert_equal("'D' x", getline(5))
274 bwipe!
275 set virtualedit=
276endfunc
277
278" Test for yank bug reported by Mark Waggoner.
279func Test_yank_block()
280 new
281 set virtualedit=block
282 call append(0, repeat(['this is a test'], 3))
283 exe "normal gg^2w\<C-V>3jy"
284 call assert_equal("a\na\na\n ", @")
285 bwipe!
286 set virtualedit=
287endfunc
288
289" Test "r" beyond the end of the line
290func Test_replace_after_eol()
291 new
292 set virtualedit=all
293 call append(0, '"r"')
294 normal gg$5lrxa
295 call assert_equal('"r" x', getline(1))
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200296 " visual block replace
297 %d _
298 call setline(1, ['a', '', 'b'])
299 exe "normal 2l\<C-V>2jrx"
300 call assert_equal(['a x', ' x', 'b x'], getline(1, '$'))
301 " visual characterwise selection replace after eol
302 %d _
303 call setline(1, 'a')
304 normal 4lv2lrx
305 call assert_equal('a xxx', getline(1))
Bram Moolenaar079119b2019-12-03 22:59:23 +0100306 bwipe!
307 set virtualedit=
308endfunc
309
310" Test "r" on a tab
311" Note that for this test, 'ts' must be 8 (the default).
312func Test_replace_on_tab()
313 new
314 set virtualedit=all
315 call append(0, "'r'\t")
316 normal gg^5lrxAy
317 call assert_equal("'r' x y", getline(1))
Bram Moolenaarf5f1e102020-03-08 05:13:15 +0100318 call setline(1, 'aaaaaaaaaaaa')
319 exe "normal! gg2lgR\<Tab>"
320 call assert_equal("aa\taaaa", getline(1))
Bram Moolenaar079119b2019-12-03 22:59:23 +0100321 bwipe!
322 set virtualedit=
323endfunc
324
325" Test to make sure 'x' can delete control characters
326func Test_ve_del_ctrl_chars()
327 new
328 set virtualedit=all
329 call append(0, "a\<C-V>b\<CR>sd")
330 set display=uhex
331 normal gg^xxxxxxi[text]
332 set display=
333 call assert_equal('[text]', getline(1))
334 bwipe!
335 set virtualedit=
336endfunc
337
338" Test for ^Y/^E due to bad w_virtcol value, reported by
339" Roy <royl@netropolis.net>.
340func Test_ins_copy_char()
341 new
342 set virtualedit=all
343 call append(0, 'abcv8efi.him2kl')
344 exe "normal gg^O\<Esc>3li\<C-E>\<Esc>4li\<C-E>\<Esc>4li\<C-E> <--"
345 exe "normal j^o\<Esc>4li\<C-Y>\<Esc>4li\<C-Y>\<Esc>4li\<C-Y> <--"
346 call assert_equal(' v i m <--', getline(1))
347 call assert_equal(' 8 . 2 <--', getline(3))
348 bwipe!
349 set virtualedit=
350endfunc
351
352" Test for yanking and pasting using the small delete register
353func Test_yank_paste_small_del_reg()
354 new
355 set virtualedit=all
356 call append(0, "foo, bar")
357 normal ggdewve"-p
358 call assert_equal(', foo', getline(1))
359 bwipe!
360 set virtualedit=
361endfunc
362
Bram Moolenaar004a6782020-04-11 17:09:31 +0200363" Test for delete that breaks a tab into spaces
364func Test_delete_break_tab()
365 new
366 call setline(1, "one\ttwo")
367 set virtualedit=all
368 normal v3ld
369 call assert_equal(' two', getline(1))
370 set virtualedit&
371 close!
372endfunc
373
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200374" Test for using <BS>, <C-W> and <C-U> in virtual edit mode
375" to erase character, word and line.
376func Test_ve_backspace()
377 new
378 call setline(1, 'sample')
379 set virtualedit=all
380 set backspace=indent,eol,start
381 exe "normal 15|i\<BS>\<BS>"
382 call assert_equal([0, 1, 7, 5], getpos('.'))
383 exe "normal 15|i\<C-W>"
384 call assert_equal([0, 1, 6, 0], getpos('.'))
385 exe "normal 15|i\<C-U>"
386 call assert_equal([0, 1, 1, 0], getpos('.'))
387 set backspace&
388 set virtualedit&
389 close!
390endfunc
391
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200392" Test for delete (x) on EOL character and after EOL
393func Test_delete_past_eol()
394 new
395 call setline(1, "ab")
396 set virtualedit=all
397 exe "normal 2lx"
398 call assert_equal('ab', getline(1))
399 exe "normal 10lx"
400 call assert_equal('ab', getline(1))
401 set virtualedit&
402 bw!
403endfunc
404
Gary Johnson53ba05b2021-07-26 22:19:10 +0200405" After calling s:TryVirtualeditReplace(), line 1 will contain one of these
406" two strings, depending on whether virtual editing is on or off.
407let s:result_ve_on = 'a x'
408let s:result_ve_off = 'x'
409
410" Utility function for Test_global_local()
411func s:TryVirtualeditReplace()
412 call setline(1, 'a')
413 normal gg7l
414 normal rx
415endfunc
416
417" Test for :set and :setlocal
418func Test_global_local()
419 new
420
421 " Verify that 'virtualedit' is initialized to empty, can be set globally to
422 " all and to empty, and can be set locally to all and to empty.
423 call s:TryVirtualeditReplace()
424 call assert_equal(s:result_ve_off, getline(1))
425 set ve=all
426 call s:TryVirtualeditReplace()
427 call assert_equal(s:result_ve_on, getline(1))
428 set ve=
429 call s:TryVirtualeditReplace()
430 call assert_equal(s:result_ve_off, getline(1))
431 setlocal ve=all
432 call s:TryVirtualeditReplace()
433 call assert_equal(s:result_ve_on, getline(1))
434 setlocal ve=
435 call s:TryVirtualeditReplace()
436 call assert_equal(s:result_ve_off, getline(1))
437
438 " Verify that :set affects multiple buffers
439 new
440 set ve=all
441 call s:TryVirtualeditReplace()
442 call assert_equal(s:result_ve_on, getline(1))
443 wincmd p
444 call s:TryVirtualeditReplace()
445 call assert_equal(s:result_ve_on, getline(1))
446 set ve=
447 wincmd p
448 call s:TryVirtualeditReplace()
449 call assert_equal(s:result_ve_off, getline(1))
450 bwipe!
451
452 " Verify that :setlocal affects only the current buffer
453 setlocal ve=all
454 new
455 call s:TryVirtualeditReplace()
456 call assert_equal(s:result_ve_off, getline(1))
457 setlocal ve=all
458 wincmd p
459 setlocal ve=
460 wincmd p
461 call s:TryVirtualeditReplace()
462 call assert_equal(s:result_ve_on, getline(1))
463 bwipe!
464 call s:TryVirtualeditReplace()
465 call assert_equal(s:result_ve_off, getline(1))
466
467 " Verify that the buffer 'virtualedit' state follows the global value only
468 " when empty and that "none" works as expected.
469 "
470 " 'virtualedit' State
471 " +--------+--------------------------+
472 " | Local | Global |
473 " | | |
474 " +--------+--------+--------+--------+
475 " | | "" | "all" | "none" |
476 " +--------+--------+--------+--------+
477 " | "" | off | on | off |
478 " | "all" | on | on | on |
479 " | "none" | off | off | off |
480 " +--------+--------+--------+--------+
481 new
482
483 setglobal ve=
484 setlocal ve=
485 call s:TryVirtualeditReplace()
486 call assert_equal(s:result_ve_off, getline(1))
487 setlocal ve=all
488 call s:TryVirtualeditReplace()
489 call assert_equal(s:result_ve_on, getline(1))
490 setlocal ve=none
491 call s:TryVirtualeditReplace()
492 call assert_equal(s:result_ve_off, getline(1))
493
494 setglobal ve=all
495 setlocal ve=
496 call s:TryVirtualeditReplace()
497 call assert_equal(s:result_ve_on, getline(1))
498 setlocal ve=all
499 call s:TryVirtualeditReplace()
500 call assert_equal(s:result_ve_on, getline(1))
501 setlocal ve=none
502 call s:TryVirtualeditReplace()
503 call assert_equal(s:result_ve_off, getline(1))
504 setlocal ve=NONE
505 call s:TryVirtualeditReplace()
506 call assert_equal(s:result_ve_off, getline(1))
507
508 setglobal ve=none
509 setlocal ve=
510 call s:TryVirtualeditReplace()
511 call assert_equal(s:result_ve_off, getline(1))
512 setlocal ve=all
513 call s:TryVirtualeditReplace()
514 call assert_equal(s:result_ve_on, getline(1))
515 setlocal ve=none
516 call s:TryVirtualeditReplace()
517 call assert_equal(s:result_ve_off, getline(1))
518
519 bwipe!
520
521 setlocal virtualedit&
522 set virtualedit&
523endfunc
524
Bram Moolenaar54c8d222019-12-02 20:41:39 +0100525" vim: shiftwidth=2 sts=2 expandtab