robin/laufey

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

zeertzjqvim-patch:9.2.0705: :delete # silently fails to update "# and clobbers "0 (#40371)8832c381e1

master
31.7 KiB1193 linesraw
1" Tests for register operations
2
3source check.vim
4source view_util.vim
5
6" This test must be executed first to check for empty and unset registers.
7func Test_aaa_empty_reg_test()
8  call assert_fails('normal @@', 'E748:')
9  call assert_fails('normal @%', 'E354:')
10  call assert_fails('normal @#', 'E354:')
11  call assert_fails('normal @!', 'E354:')
12  call assert_fails('normal @:', 'E30:')
13  call assert_fails('normal @.', 'E29:')
14  call assert_fails('put /', 'E35:')
15  call assert_fails('put .', 'E29:')
16endfunc
17
18func Test_yank_shows_register()
19    enew
20    set report=0
21    call setline(1, ['foo', 'bar'])
22    " Line-wise
23    exe 'norm! yy'
24    call assert_equal('1 line yanked', v:statusmsg)
25    exe 'norm! "zyy'
26    call assert_equal('1 line yanked into "z', v:statusmsg)
27    exe 'norm! yj'
28    call assert_equal('2 lines yanked', v:statusmsg)
29    exe 'norm! "zyj'
30    call assert_equal('2 lines yanked into "z', v:statusmsg)
31
32    " Block-wise
33    exe "norm! \<C-V>y"
34    call assert_equal('block of 1 line yanked', v:statusmsg)
35    exe "norm! \<C-V>\"zy"
36    call assert_equal('block of 1 line yanked into "z', v:statusmsg)
37    exe "norm! \<C-V>jy"
38    call assert_equal('block of 2 lines yanked', v:statusmsg)
39    exe "norm! \<C-V>j\"zy"
40    call assert_equal('block of 2 lines yanked into "z', v:statusmsg)
41
42    bwipe!
43endfunc
44
45func Test_display_registers()
46    " Disable clipboard
47    let save_clipboard = get(g:, 'clipboard', {})
48    let g:clipboard = {}
49    defer execute('let g:clipboard = save_clipboard')
50
51    e file1
52    e file2
53    call setline(1, ['foo', 'bar'])
54    /bar
55    exe 'norm! y2l"axx'
56    call feedkeys("i\<C-R>=2*4\n\<esc>")
57    call feedkeys(":ls\n", 'xt')
58
59    " these commands work in the sandbox
60    let a = execute('sandbox display')
61    " When X11 connection is not available, there is a warning W23
62    " filter this out (we could also run the :display command twice)
63    let a = substitute(a, 'W23.*0\n', '', '')
64    let b = execute('sandbox registers')
65
66    call assert_equal(a, b)
67    call assert_match('^\nType Name Content\n'
68          \ .         '  c  ""   a\n'
69          \ .         '  c  "0   ba\n'
70          \ .         '  c  "a   b\n'
71          \ .         '.*'
72          \ .         '  c  "-   a\n'
73          \ .         '.*'
74          \ .         '  c  ":   ls\n'
75          \ .         '  c  "%   file2\n'
76          \ .         '  c  "#   file1\n'
77          \ .         '  c  "/   bar\n'
78          \ .         '  c  "=   2\*4', a)
79
80    let a = execute('registers a')
81    call assert_match('^\nType Name Content\n'
82          \ .         '  c  "a   b', a)
83
84    let a = execute('registers :')
85    call assert_match('^\nType Name Content\n'
86          \ .         '  c  ":   ls', a)
87
88    let a = execute('registers %')
89    call assert_match('^\nType Name Content\n'
90          \ .         '  c  "%   file2', a)
91
92    let a = execute('registers #')
93    call assert_match('^\nType Name Content\n'
94          \ .         '  c  "#   file1', a)
95
96    bwipe!
97endfunc
98
99func Test_register_one()
100  " delete a line goes into register one
101  new
102  call setline(1, "one")
103  normal dd
104  call assert_equal("one\n", @1)
105
106  " delete a word does not change register one, does change "-
107  call setline(1, "two")
108  normal de
109  call assert_equal("one\n", @1)
110  call assert_equal("two", @-)
111
112  " delete a word with a register does not change register one
113  call setline(1, "three")
114  normal "ade
115  call assert_equal("three", @a)
116  call assert_equal("one\n", @1)
117
118  " delete a word with register DOES change register one with one of a list of
119  " operators
120  " %
121  call setline(1, ["(12)3"])
122  normal "ad%
123  call assert_equal("(12)", @a)
124  call assert_equal("(12)", @1)
125
126  " (
127  call setline(1, ["first second"])
128  normal $"ad(
129  call assert_equal("first secon", @a)
130  call assert_equal("first secon", @1)
131
132  " )
133  call setline(1, ["First Second."])
134  normal gg0"ad)
135  call assert_equal("First Second.", @a)
136  call assert_equal("First Second.", @1)
137
138  " `
139  call setline(1, ["start here."])
140  normal gg0fhmx0"ad`x
141  call assert_equal("start ", @a)
142  call assert_equal("start ", @1)
143
144  " /
145  call setline(1, ["searchX"])
146  exe "normal gg0\"ad/X\<CR>"
147  call assert_equal("search", @a)
148  call assert_equal("search", @1)
149
150  " ?
151  call setline(1, ["Ysearch"])
152  exe "normal gg$\"ad?Y\<CR>"
153  call assert_equal("Ysearc", @a)
154  call assert_equal("Ysearc", @1)
155
156  " n
157  call setline(1, ["Ynext"])
158  normal gg$"adn
159  call assert_equal("Ynex", @a)
160  call assert_equal("Ynex", @1)
161
162  " N
163  call setline(1, ["prevY"])
164  normal gg0"adN
165  call assert_equal("prev", @a)
166  call assert_equal("prev", @1)
167
168  " }
169  call setline(1, ["one", ""])
170  normal gg0"ad}
171  call assert_equal("one\n", @a)
172  call assert_equal("one\n", @1)
173
174  " {
175  call setline(1, ["", "two"])
176  normal 2G$"ad{
177  call assert_equal("\ntw", @a)
178  call assert_equal("\ntw", @1)
179
180  bwipe!
181endfunc
182
183func Test_recording_status_in_ex_line()
184  set noruler
185  norm qx
186  redraw!
187  call assert_equal('recording @x', Screenline(&lines))
188  set shortmess=q
189  redraw!
190  call assert_equal('', Screenline(&lines)) " Nvim: shm+=q fully hides message
191  set shortmess&
192  norm q
193  redraw!
194  call assert_equal('', Screenline(&lines))
195  set ruler
196  norm qx
197  redraw!
198  call assert_match('recording @x\s*0,0-1\s*All', Screenline(&lines))
199  set shortmess=q
200  redraw!
201  call assert_match('\s*0,0-1\s*All', Screenline(&lines)) " Nvim: shm+=q fully hides
202  set shortmess&
203  norm q
204  redraw!
205  call assert_match('\s*0,0-1\s*All', Screenline(&lines))
206endfunc
207
208" Check that replaying a typed sequence does not use an Esc and following
209" characters as an escape sequence.
210func Test_recording_esc_sequence()
211  new
212  try
213    let save_F2 = &t_F2
214  catch
215  endtry
216  let t_F2 = "\<Esc>OQ"
217  call feedkeys("qqiTest\<Esc>", "xt")
218  call feedkeys("OQuirk\<Esc>q", "xt")
219  call feedkeys("Go\<Esc>@q", "xt")
220  call assert_equal(['Quirk', 'Test', 'Quirk', 'Test'], getline(1, 4))
221  bwipe!
222  if exists('save_F2')
223    let &t_F2 = save_F2
224  else
225    set t_F2=
226  endif
227endfunc
228
229func Test_recording_with_select_mode()
230  new
231  call feedkeys("qacc12345\<Esc>gH98765\<Esc>q", "tx")
232  call assert_equal("98765", getline(1))
233  call assert_equal("cc12345\<Esc>gH98765\<Esc>", @a)
234  call setline(1, 'asdf')
235  normal! @a
236  call assert_equal("98765", getline(1))
237  bwipe!
238endfunc
239
240func Run_test_recording_with_select_mode_utf8()
241  new
242
243  " Test with different text lengths: 5, 7, 9, 11, 13, 15, to check that
244  " a character isn't split between two buffer blocks.
245  for s in ['12345', '口=口', '口口口', '口=口=口', '口口=口口', '口口口口口']
246    " 0x80 is K_SPECIAL
247    " 0x9B is CSI
248    " 哦: 0xE5 0x93 0xA6
249    " 洛: 0xE6 0xB4 0x9B
250    " 固: 0xE5 0x9B 0xBA
251    " 四: 0xE5 0x9B 0x9B
252    " 最: 0xE6 0x9C 0x80
253    " 倒: 0xE5 0x80 0x92
254    " 倀: 0xE5 0x80 0x80
255    for c in ['哦', '洛', '固', '四', '最', '倒', '倀']
256      call setline(1, 'asdf')
257      call feedkeys($"qacc{s}\<Esc>gH{c}\<Esc>q", "tx")
258      call assert_equal(c, getline(1))
259      call assert_equal($"cc{s}\<Esc>gH{c}\<Esc>", @a)
260      call setline(1, 'asdf')
261      normal! @a
262      call assert_equal(c, getline(1))
263
264      " Test with Shift modifier.
265      let shift_c = eval($'"\<S-{c}>"')
266      call setline(1, 'asdf')
267      call feedkeys($"qacc{s}\<Esc>gH{shift_c}\<Esc>q", "tx")
268      call assert_equal(c, getline(1))
269      call assert_equal($"cc{s}\<Esc>gH{shift_c}\<Esc>", @a)
270      call setline(1, 'asdf')
271      normal! @a
272      call assert_equal(c, getline(1))
273    endfor
274  endfor
275
276  bwipe!
277endfunc
278
279func Test_recording_with_select_mode_utf8()
280  call Run_test_recording_with_select_mode_utf8()
281endfunc
282
283" This must be done as one of the last tests, because it starts the GUI, which
284" cannot be undone.
285func Test_zz_recording_with_select_mode_utf8_gui()
286  CheckCanRunGui
287
288  gui -f
289  call Run_test_recording_with_select_mode_utf8()
290endfunc
291
292func Test_recording_append_utf8()
293  new
294
295  let keys = "cc哦洛固四最倒倀\<Esc>0"
296  call feedkeys($'qr{keys}q', 'xt')
297  call assert_equal(keys, @r)
298
299  let morekeys = "A…foobar\<Esc>0"
300  call feedkeys($'qR{morekeys}q', 'xt')
301  call assert_equal(keys .. morekeys, @r)
302
303  bwipe!
304endfunc
305
306func Test_recording_with_super_mod()
307  if "\<D-j>"[-1:] == '>'
308    throw 'Skipped: <D- modifier not supported'
309  endif
310
311  nnoremap <D-j> <Ignore>
312  let s = repeat("\<D-j>", 1000)
313  " This used to crash Vim
314  call feedkeys($'qr{s}q', 'tx')
315  call assert_equal(s, @r)
316  nunmap <D-j>
317endfunc
318
319" Test for executing the last used register (@)
320func Test_last_used_exec_reg()
321  " Test for the @: command
322  let a = ''
323  call feedkeys(":let a ..= 'Vim'\<CR>", 'xt')
324  normal @:
325  call assert_equal('VimVim', a)
326
327  " Test for the @= command
328  let x = ''
329  let a = ":let x ..= 'Vim'\<CR>"
330  exe "normal @=a\<CR>"
331  normal @@
332  call assert_equal('VimVim', x)
333
334  " Test for the @. command
335  let a = ''
336  call feedkeys("i:let a ..= 'Edit'\<CR>", 'xt')
337  normal @.
338  normal @@
339  call assert_equal('EditEdit', a)
340
341  " Test for repeating the last command-line in visual mode
342  call append(0, 'register')
343  normal gg
344  let @r = ''
345  call feedkeys("v:yank R\<CR>", 'xt')
346  call feedkeys("v@:", 'xt')
347  call assert_equal("\nregister\nregister\n", @r)
348
349  enew!
350endfunc
351
352func Test_get_register()
353  enew
354  edit Xfile1
355  edit Xfile2
356  call assert_equal('Xfile2', getreg('%'))
357  call assert_equal('Xfile1', getreg('#'))
358
359  call feedkeys("iTwo\<Esc>", 'xt')
360  call assert_equal('Two', getreg('.'))
361  call assert_equal('', getreg('_'))
362  call assert_beeps('normal ":yy')
363  call assert_beeps('normal "%yy')
364  call assert_beeps('normal ".yy')
365
366  call assert_equal('', getreg("\<C-F>"))
367  call assert_equal('', getreg("\<C-W>"))
368  call assert_equal('', getreg("\<C-L>"))
369  " Change the last used register to '"' for the next test
370  normal! ""yy
371  let @" = 'happy'
372  call assert_equal('happy', getreg())
373  call assert_equal('happy', getreg(''))
374
375  call assert_equal('', getregtype('!'))
376  call assert_fails('echo getregtype([])', 'E730:')
377  call assert_equal('v', getregtype())
378  call assert_equal('v', getregtype(''))
379
380  " Test for inserting an invalid register content
381  call assert_beeps('exe "normal i\<C-R>!"')
382
383  " Test for inserting a register with multiple lines
384  call deletebufline('', 1, '$')
385  call setreg('r', ['a', 'b'])
386  exe "normal i\<C-R>r"
387  call assert_equal(['a', 'b', ''], getline(1, '$'))
388
389  " Test for inserting a multi-line register in the command line
390  call feedkeys(":\<C-R>r\<Esc>", 'xt')
391  " Nvim: no trailing CR because of #6137
392  " call assert_equal("a\rb\r", histget(':', -1))
393  call assert_equal("a\rb", histget(':', -1))
394
395  call assert_fails('let r = getreg("=", [])', 'E745:')
396  call assert_fails('let r = getreg("=", 1, [])', 'E745:')
397  enew!
398
399  " Using a register in operator-pending mode should fail
400  call assert_beeps('norm! c"')
401endfunc
402
403func Test_set_register()
404  call assert_fails("call setreg('#', 200)", 'E86:')
405  " call assert_fails("call setreg('a', test_unknown())", 'E908:')
406
407  edit Xfile_alt_1
408  let b1 = bufnr('')
409  edit Xfile_alt_2
410  let b2 = bufnr('')
411  edit Xfile_alt_3
412  let b3 = bufnr('')
413  call setreg('#', 'alt_1')
414  call assert_equal('Xfile_alt_1', getreg('#'))
415  call setreg('#', b2)
416  call assert_equal('Xfile_alt_2', getreg('#'))
417  call setreg('#', '')
418  call assert_equal('', getreg('#'))
419  call setreg('#', 'alt_1')
420  let @# = ''
421  call assert_equal('', getreg('#'))
422
423  let ab = 'regwrite'
424  call setreg('=', '')
425  call setreg('=', 'a', 'a')
426  call setreg('=', 'b', 'a')
427  call assert_equal('regwrite', getreg('='))
428
429  " Test for setting a list of lines to special registers
430  call setreg('/', [])
431  call assert_equal('', @/)
432  call setreg('=', [])
433  call assert_equal('', @=)
434  call assert_fails("call setreg('/', ['a', 'b'])", 'E883:')
435  call assert_fails("call setreg('=', ['a', 'b'])", 'E883:')
436  call assert_fails("call setreg('#', ['a', 'b'])", 'E883:')
437  call assert_equal(0, setreg('_', ['a', 'b']))
438
439  " Test for recording to a invalid register
440  call assert_beeps('normal q$')
441
442  " Appending to a register when recording
443  call append(0, "text for clipboard test")
444  normal gg
445  call feedkeys('qrllq', 'xt')
446  call feedkeys('qRhhq', 'xt')
447  call assert_equal('llhh', getreg('r'))
448
449  " Appending a list of characters to a register from different lines
450  let @r = ''
451  call append(0, ['abcdef', '123456'])
452  normal gg"ry3l
453  call cursor(2, 4)
454  normal "Ry3l
455  call assert_equal('abc456', @r)
456
457  " Test for gP with multiple lines selected using characterwise motion
458  %delete
459  call append(0, ['vim editor', 'vim editor'])
460  let @r = ''
461  exe "normal ggwy/vim /e\<CR>gP"
462  call assert_equal(['vim editor', 'vim editor', 'vim editor'], getline(1, 3))
463
464  " Test for gP with . register
465  %delete
466  normal iabc
467  normal ".gp
468  call assert_equal('abcabc', getline(1))
469  normal 0".gP
470  call assert_equal('abcabcabc', getline(1))
471
472  let @"=''
473  call setreg('', '1')
474  call assert_equal('1', @")
475  call setreg('@', '2')
476  call assert_equal('2', @")
477
478  enew!
479endfunc
480
481" Test for blockwise register width calculations
482func Test_set_register_blockwise_width()
483  " Test for regular calculations and overriding the width
484  call setreg('a', "12\n1234\n123", 'b')
485  call assert_equal("\<c-v>4", getreginfo('a').regtype)
486  call setreg('a', "12\n1234\n123", 'b1')
487  call assert_equal("\<c-v>1", getreginfo('a').regtype)
488  call setreg('a', "12\n1234\n123", 'b6')
489  call assert_equal("\<c-v>6", getreginfo('a').regtype)
490
491  " Test for Unicode parsing
492  call setreg('a', "z😅😅z\n12345", 'b')
493  call assert_equal("\<c-v>6", getreginfo('a').regtype)
494  call setreg('a', ["z😅😅z", "12345"], 'b')
495  call assert_equal("\<c-v>6", getreginfo('a').regtype)
496endfunc
497
498" Test for clipboard registers (* and +)
499func Test_clipboard_regs()
500  throw 'skipped: needs clipboard=autoselect,autoselectplus'
501
502  CheckNotGui
503  CheckFeature clipboard_working
504
505  new
506  call append(0, "text for clipboard test")
507  normal gg"*yiw
508  call assert_equal('text', getreg('*'))
509  normal gg2w"+yiw
510  call assert_equal('clipboard', getreg('+'))
511
512  " Test for replacing the clipboard register contents
513  set clipboard=unnamed
514  let @* = 'food'
515  normal ggviw"*p
516  call assert_equal('text', getreg('*'))
517  call assert_equal('food for clipboard test', getline(1))
518  normal ggviw"*p
519  call assert_equal('food', getreg('*'))
520  call assert_equal('text for clipboard test', getline(1))
521
522  " Test for replacing the selection register contents
523  set clipboard=unnamedplus
524  let @+ = 'food'
525  normal ggviw"+p
526  call assert_equal('text', getreg('+'))
527  call assert_equal('food for clipboard test', getline(1))
528  normal ggviw"+p
529  call assert_equal('food', getreg('+'))
530  call assert_equal('text for clipboard test', getline(1))
531
532  " Test for auto copying visually selected text to clipboard register
533  call setline(1, "text for clipboard test")
534  let @* = ''
535  set clipboard=autoselect
536  normal ggwwviwy
537  call assert_equal('clipboard', @*)
538
539  " Test for auto copying visually selected text to selection register
540  let @+ = ''
541  set clipboard=autoselectplus
542  normal ggwviwy
543  call assert_equal('for', @+)
544
545  set clipboard&vim
546  bwipe!
547endfunc
548
549" Test for restarting the current mode (insert or (virtual) replace) after
550" executing the contents of a register
551func Test_exec_reg_restart_mode()
552  new
553
554  let @r = "ivim \<Esc>"
555
556  call setline(1, 'editor')
557  normal gg
558  call feedkeys("i\<C-O>@r\<C-R>=mode(1)\<CR>", 'xt')
559  call assert_equal('vimi editor', getline(1))
560
561  call setline(1, 'editor')
562  normal gg
563  call feedkeys("R\<C-O>@r\<C-R>=mode(1)\<CR>", 'xt')
564  call assert_equal('vimReditor', getline(1))
565
566  call setline(1, 'editor')
567  normal gg
568  call feedkeys("gR\<C-O>@r\<C-R>=mode(1)\<CR>", 'xt')
569  call assert_equal('vimRvditor', getline(1))
570
571  " If the register doesn't return to Normal mode, Vim should stay in whatever
572  " mode the register ends up with, and should not insert extra text. #20085
573  for [s0, expected] in
574        \ [['i', 'vim editor,i'], ['R', 'vim or,R'], ['gR', 'vim or,Rv']]
575    let @r = $"{s0}vim "
576    for s1 in ['i', 'R', 'gR']
577      call setline(1, 'editor')
578      normal gg
579      call feedkeys($"{s1}\<C-O>@r\<End>,\<C-R>=mode(1)\<CR>", 'xt')
580      call assert_equal(expected, getline(1))
581    endfor
582  endfor
583
584  bwipe!
585endfunc
586
587" Test for executing a register using :@ command
588func Test_execute_register()
589  call setreg('r', [])
590  call assert_beeps('@r')
591  let i = 1
592  let @q = 'let i+= 1'
593  @q
594  @
595  call assert_equal(3, i)
596
597  " try to execute expression register and use a backspace to cancel it
598  new
599  call feedkeys("@=\<BS>ax\<CR>y", 'xt')
600  call assert_equal(['x', 'y'], getline(1, '$'))
601  bw!
602
603  " cannot execute a register in operator pending mode
604  call assert_beeps('normal! c@r')
605endfunc
606
607" Test for getting register info
608func Test_get_reginfo()
609  enew
610  call setline(1, ['foo', 'bar'])
611
612  exe 'norm! "zyy'
613  let info = getreginfo('"')
614  call assert_equal('z', info.points_to)
615  call setreg('y', 'baz')
616  call assert_equal('z', getreginfo('').points_to)
617  call setreg('y', { 'isunnamed': v:true })
618  call assert_equal('y', getreginfo('"').points_to)
619
620  exe '$put'
621  call assert_equal(getreg('y'), getline(3))
622  call setreg('', 'qux')
623  call assert_equal('0', getreginfo('').points_to)
624  call setreg('x', 'quux')
625  call assert_equal('0', getreginfo('').points_to)
626
627  let info = getreginfo('')
628  call assert_equal(getreg('', 1, 1), info.regcontents)
629  call assert_equal(getregtype(''), info.regtype)
630
631  exe "norm! 0\<c-v>e" .. '"zy'
632  let info = getreginfo('z')
633  call assert_equal(getreg('z', 1, 1), info.regcontents)
634  call assert_equal(getregtype('z'), info.regtype)
635  call assert_equal(1, +info.isunnamed)
636
637  let info = getreginfo('"')
638  call assert_equal('z', info.points_to)
639
640  let @a="a1b2"
641  nnoremap <F2> <Cmd>let g:RegInfo = getreginfo()<CR>
642  exe "normal \"a\<F2>"
643  call assert_equal({'regcontents': ['a1b2'], 'isunnamed': v:false,
644        \ 'regtype': 'v'}, g:RegInfo)
645  nunmap <F2>
646  unlet g:RegInfo
647
648  " The type of "isunnamed" was VAR_SPECIAL but should be VAR_BOOL.  Can only
649  " be noticed when using json_encod().
650  call setreg('a', 'foo')
651  let reginfo = getreginfo('a')
652  let expected = #{regcontents: ['foo'], isunnamed: v:false, regtype: 'v'}
653  call assert_equal(json_encode(expected), json_encode(reginfo))
654
655  bwipe!
656endfunc
657
658" Test for restoring register with dict from getreginfo
659func Test_set_register_dict()
660  enew!
661
662  call setreg('"', #{ regcontents: ['one', 'two'],
663        \ regtype: 'V', points_to: 'z' })
664  call assert_equal(['one', 'two'], getreg('"', 1, 1))
665  let info = getreginfo('"')
666  call assert_equal('z', info.points_to)
667  call assert_equal('V', info.regtype)
668  call assert_equal(1, +getreginfo('z').isunnamed)
669
670  call setreg('x', #{ regcontents: ['three', 'four'],
671        \ regtype: 'v', isunnamed: v:true })
672  call assert_equal(['three', 'four'], getreg('"', 1, 1))
673  let info = getreginfo('"')
674  call assert_equal('x', info.points_to)
675  call assert_equal('v', info.regtype)
676  call assert_equal(1, +getreginfo('x').isunnamed)
677
678  call setreg('y', #{ regcontents: 'five',
679        \ regtype: "\<c-v>", isunnamed: v:false })
680  call assert_equal("\<c-v>4", getreginfo('y').regtype)
681  call assert_equal(0, +getreginfo('y').isunnamed)
682  call assert_equal(['three', 'four'], getreg('"', 1, 1))
683  call assert_equal('x', getreginfo('"').points_to)
684
685  call setreg('"', #{ regcontents: 'six' })
686  call assert_equal('0', getreginfo('"').points_to)
687  call assert_equal(1, +getreginfo('0').isunnamed)
688  call assert_equal(['six'], getreginfo('0').regcontents)
689  call assert_equal(['six'], getreginfo('"').regcontents)
690
691  let @x = 'one'
692  call setreg('x', {})
693  call assert_equal(1, len(split(execute('reg x'), '\n')))
694
695  call assert_fails("call setreg('0', #{regtype: 'V'}, 'v')", 'E118:')
696  call assert_fails("call setreg('0', #{regtype: 'X'})", 'E475:')
697  call assert_fails("call setreg('0', #{regtype: 'vy'})", 'E475:')
698
699  bwipe!
700endfunc
701
702func Test_v_register()
703  enew
704  call setline(1, 'nothing')
705
706  func s:Put()
707    let s:register = v:register
708    exec 'normal! "' .. v:register .. 'P'
709  endfunc
710  nnoremap <buffer> <plug>(test) :<c-u>call s:Put()<cr>
711  xnoremap <buffer> <plug>(test) :<c-u>call s:Put()<cr>
712  nmap <buffer> S <plug>(test)
713  xmap <buffer> S <plug>(test)
714
715  let @z = "testz\n"
716  let @" = "test@\n"
717
718  let s:register = ''
719  call feedkeys('"_ddS', 'mx')
720  call assert_equal('"', s:register)        " fails before 8.2.0929
721  call assert_equal('test@', getline('.'))  " fails before 8.2.0929
722
723  let s:register = ''
724  call feedkeys('V"_dS', 'mx')
725  call assert_equal('"', s:register)
726  call assert_equal('test@', getline('.'))
727
728  let s:register = ''
729  call feedkeys('"zS', 'mx')
730  call assert_equal('z', s:register)
731  call assert_equal('testz', getline('.'))
732
733  let s:register = ''
734  call feedkeys('"zSS', 'mx')
735  call assert_equal('"', s:register)
736  call assert_equal('test@', getline('.'))
737
738  let s:register = ''
739  call feedkeys("\"z\<Ignore>S", 'mx')
740  call assert_equal('z', s:register)
741  call assert_equal('testz', getline('.'))
742
743  let s:register = ''
744  call feedkeys('"_S', 'mx')
745  call assert_equal('_', s:register)
746
747  let s:register = ''
748  call feedkeys('V"zS', 'mx')
749  call assert_equal('z', s:register)
750  call assert_equal('testz', getline('.'))
751
752  let s:register = ''
753  call feedkeys('V"zSS', 'mx')
754  call assert_equal('"', s:register)
755  call assert_equal('test@', getline('.'))
756
757  let s:register = ''
758  call feedkeys("V\"z\<Ignore>S", 'mx')
759  call assert_equal('z', s:register)
760  call assert_equal('testz', getline('.'))
761
762  let s:register = ''
763  call feedkeys('V"_S', 'mx')
764  call assert_equal('_', s:register)
765
766  let s:register = ''
767  normal "_ddS
768  call assert_equal('"', s:register)        " fails before 8.2.0929
769  call assert_equal('test@', getline('.'))  " fails before 8.2.0929
770
771  let s:register = ''
772  normal V"_dS
773  call assert_equal('"', s:register)
774  call assert_equal('test@', getline('.'))
775
776  let s:register = ''
777  execute 'normal "z:call' "s:Put()\n"
778  call assert_equal('z', s:register)
779  call assert_equal('testz', getline('.'))
780
781  " Test operator and omap
782  let @b = 'testb'
783  func s:OpFunc(...)
784    let s:register2 = v:register
785  endfunc
786  set opfunc=s:OpFunc
787
788  normal "bg@l
789  normal S
790  call assert_equal('"', s:register)        " fails before 8.2.0929
791  call assert_equal('b', s:register2)
792
793  func s:Motion()
794    let s:register1 = v:register
795    normal! l
796  endfunc
797  onoremap <buffer> Q :<c-u>call s:Motion()<cr>
798
799  normal "bg@Q
800  normal S
801  call assert_equal('"', s:register)
802  call assert_equal('b', s:register1)
803  call assert_equal('"', s:register2)
804
805  set opfunc&
806  bwipe!
807endfunc
808
809" Test for executing the contents of a register as an Ex command with line
810" continuation.
811func Test_execute_reg_as_ex_cmd()
812  " Line continuation with just two lines
813  let code =<< trim END
814    let l = [
815      \ 1]
816  END
817  let @r = code->join("\n")
818  let l = []
819  @r
820  call assert_equal([1], l)
821
822  " Line continuation with more than two lines
823  let code =<< trim END
824    let l = [
825      \ 1,
826      \ 2,
827      \ 3]
828  END
829  let @r = code->join("\n")
830  let l = []
831  @r
832  call assert_equal([1, 2, 3], l)
833
834  " use comments interspersed with code
835  let code =<< trim END
836    let l = [
837      "\ one
838      \ 1,
839      "\ two
840      \ 2,
841      "\ three
842      \ 3]
843  END
844  let @r = code->join("\n")
845  let l = []
846  @r
847  call assert_equal([1, 2, 3], l)
848
849  " use line continuation in the middle
850  let code =<< trim END
851    let a = "one"
852    let l = [
853      \ 1,
854      \ 2]
855    let b = "two"
856  END
857  let @r = code->join("\n")
858  let l = []
859  @r
860  call assert_equal([1, 2], l)
861  call assert_equal("one", a)
862  call assert_equal("two", b)
863
864  " only one line with a \
865  let @r = "\\let l = 1"
866  call assert_fails('@r', 'E10:')
867
868  " only one line with a "\
869  let @r = '   "\ let i = 1'
870  @r
871  call assert_false(exists('i'))
872
873  " first line also begins with a \
874  let @r = "\\let l = [\n\\ 1]"
875  call assert_fails('@r', 'E10:')
876
877  " Test with a large number of lines
878  let @r = "let str = \n"
879  let @r ..= repeat("  \\ 'abcdefghijklmnopqrstuvwxyz' ..\n", 312)
880  let @r ..= '  \ ""'
881  @r
882  call assert_equal(repeat('abcdefghijklmnopqrstuvwxyz', 312), str)
883endfunc
884
885func Test_ve_blockpaste()
886  new
887  set ve=all
888  0put =['QWERTZ','ASDFGH']
889  call cursor(1,1)
890  exe ":norm! \<C-V>3ljdP"
891  call assert_equal(1, col('.'))
892  call assert_equal(['QWERTZ', 'ASDFGH'], getline(1, 2))
893  call cursor(1,1)
894  exe ":norm! \<C-V>3ljd"
895  call cursor(1,1)
896  norm! $3lP
897  call assert_equal(5, col('.'))
898  call assert_equal(['TZ  QWER', 'GH  ASDF'], getline(1, 2))
899  set ve&vim
900  bwipe!
901endfunc
902
903func Test_insert_small_delete()
904  new
905  call setline(1, ['foo foobar bar'])
906  call cursor(1,1)
907  exe ":norm! ciw'\<C-R>-'"
908  call assert_equal("'foo' foobar bar", getline(1))
909  exe ":norm! w.w."
910  call assert_equal("'foo' 'foobar' 'bar'", getline(1))
911  bwipe!
912endfunc
913
914" Record in insert mode using CTRL-O
915func Test_record_in_insert_mode()
916  new
917  let @r = ''
918  call setline(1, ['foo'])
919  call feedkeys("i\<C-O>qrbaz\<C-O>q", 'xt')
920  call assert_equal('baz', @r)
921  bwipe!
922endfunc
923
924func Test_record_in_select_mode()
925  new
926  call setline(1, 'text')
927  sil norm q00
928  sil norm q
929  call assert_equal('0ext', getline(1))
930
931  %delete
932  let @r = ''
933  call setline(1, ['abc', 'abc', 'abc'])
934  smap <F2> <Right><Right>,
935  call feedkeys("qrgh\<F2>Dk\<Esc>q", 'xt')
936  call assert_equal("gh\<F2>Dk\<Esc>", @r)
937  norm j0@rj0@@
938  call assert_equal([',Dk', ',Dk', ',Dk'], getline(1, 3))
939  sunmap <F2>
940
941  bwipe!
942endfunc
943
944" A mapping that ends recording should be removed from the recorded register.
945func Test_end_record_using_mapping()
946  new
947  call setline(1, 'aaa')
948  nnoremap s q
949  call feedkeys('safas', 'tx')
950  call assert_equal('fa', @a)
951  nunmap s
952
953  nnoremap xx q
954  call feedkeys('0xxafaxx', 'tx')
955  call assert_equal('fa', @a)
956  nunmap xx
957
958  nnoremap xsx q
959  call feedkeys('0qafaxsx', 'tx')
960  call assert_equal('fa', @a)
961  nunmap xsx
962
963  bwipe!
964endfunc
965
966" Starting a new recording should work immediately after replaying a recording
967" that ends with a <Nop> mapping or a character search.
968func Test_end_reg_executing()
969  new
970  nnoremap s <Nop>
971  let @a = 's'
972  call feedkeys("@aqaq\<Esc>", 'tx')
973  call assert_equal('', @a)
974  call assert_equal('', getline(1))
975
976  call setline(1, 'aaa')
977  nnoremap s qa
978  let @a = 'fa'
979  call feedkeys("@asq\<Esc>", 'tx')
980  call assert_equal('', @a)
981  call assert_equal('aaa', getline(1))
982
983  nunmap s
984  bwipe!
985endfunc
986
987func Test_reg_executing_in_range_normal()
988  new
989  set showcmd
990  call setline(1, range(10))
991  let g:log = []
992  nnoremap s <Cmd>let g:log += [reg_executing()]<CR>
993  let @r = 's'
994
995  %normal @r
996  call assert_equal(repeat(['r'], 10), g:log)
997
998  nunmap s
999  unlet g:log
1000  set showcmd&
1001  bwipe!
1002endfunc
1003
1004" An operator-pending mode mapping shouldn't be applied to keys typed in
1005" Insert mode immediately after a character search when replaying.
1006func Test_replay_charsearch_omap()
1007  CheckFeature timers
1008
1009  new
1010  call setline(1, 'foo[blah]')
1011  onoremap , k
1012  call timer_start(10, {-> feedkeys(",bar\<Esc>q", 't')})
1013  call feedkeys('qrct[', 'xt!')
1014  call assert_equal(',bar[blah]', getline(1))
1015  call assert_equal("ct[\<Ignore>,bar\<Esc>", @r)
1016  call assert_equal('ct[<Ignore>,bar<Esc>', keytrans(@r))
1017  undo
1018  call assert_equal('foo[blah]', getline(1))
1019  call feedkeys('@r', 'xt!')
1020  call assert_equal(',bar[blah]', getline(1))
1021
1022  ounmap ,
1023  bwipe!
1024endfunc
1025
1026" Make sure that y_append is correctly reset
1027" and the previous register is working as expected
1028func Test_register_y_append_reset()
1029  new
1030  call setline(1, ['1',
1031    \ '2 ----------------------------------------------------',
1032    \ '3',
1033    \ '4',
1034    \ '5 ----------------------------------------------------',
1035    \ '6',
1036    \ '7',
1037    \ '8 ----------------------------------------------------',
1038    \ '9',
1039    \ '10 aaaaaaa 4.',
1040    \ '11 Game Dbl-Figures Leaders:',
1041    \ '12 Player Pts FG% 3P% FT% RB AS BL ST TO PF EFF',
1042    \ '13 bbbbbbbbb 12 (50 /0 /67 )/ 7/ 3/ 0/ 2/ 3/ 4/+15',
1043    \ '14 cccccc 12 (57 /67 /100)/ 2/ 1/ 1/ 0/ 1/ 3/+12',
1044    \ '15 ddddddd 10 (63 /0 /0 )/ 1/ 3/ 0/ 3/ 5/ 3/ +9',
1045    \ '16 4 5-15 0-3 2-2 5-12 1-1 3-4 33.3 0.0 100 41.7 100 75 12 14',
1046    \ '17 F 23-55 2-10 9-11 23-52 3-13 26-29 41.8 20 81.8 44.2 23.1 89.7 57 75',
1047    \ '18 4 3 6 3 2 3 3 4 3 3 7 3 1 4 6 -1 -1 +2 -1 -2',
1048    \ '19 F 13 19 5 10 4 17 22 9 14 32 13 4 20 17 -1 -13 -4 -3 -3 +5'])
1049  11
1050  exe "norm! \"a5dd"
1051  norm! j
1052  exe "norm! \"bY"
1053  norm! 2j
1054  exe "norm! \"BY"
1055  norm! 4k
1056  norm! 5dd
1057  norm! 3k
1058  " The next put should put the content of the unnamed register, not of
1059  " register b!
1060  norm! p
1061  call assert_equal(['1',
1062    \ '2 ----------------------------------------------------',
1063    \ '3',
1064    \ '4',
1065    \ '5 ----------------------------------------------------',
1066    \ '6',
1067    \ '10 aaaaaaa 4.',
1068    \ '16 4 5-15 0-3 2-2 5-12 1-1 3-4 33.3 0.0 100 41.7 100 75 12 14',
1069    \ '17 F 23-55 2-10 9-11 23-52 3-13 26-29 41.8 20 81.8 44.2 23.1 89.7 57 75',
1070    \ '18 4 3 6 3 2 3 3 4 3 3 7 3 1 4 6 -1 -1 +2 -1 -2',
1071    \ '19 F 13 19 5 10 4 17 22 9 14 32 13 4 20 17 -1 -13 -4 -3 -3 +5',
1072    \ '7',
1073    \ '8 ----------------------------------------------------',
1074    \ '9'], getline(1,'$'))
1075  bwipe!
1076endfunc
1077
1078func Test_insert_small_delete_replace_mode()
1079  new
1080  call setline(1, ['foo', 'bar', 'foobar',  'bar'])
1081  let @- = 'foo'
1082  call cursor(2, 1)
1083  exe ":norm! R\<C-R>-\<C-R>-"
1084  call assert_equal('foofoo', getline(2))
1085  call cursor(3, 1)
1086  norm! D
1087  call assert_equal(['foo', 'foofoo', '',  'bar'], getline(1, 4))
1088  call cursor(4, 2)
1089  exe ":norm! R\<C-R>-ZZZZ"
1090  call assert_equal(['foo', 'foofoo', '',  'bfoobarZZZZ'], getline(1, 4))
1091  call cursor(1, 1)
1092  let @- = ''
1093  exe ":norm! R\<C-R>-ZZZ"
1094  call assert_equal(['ZZZ', 'foofoo', '',  'bfoobarZZZZ'], getline(1, 4))
1095  let @- = 'βbβ'
1096  call cursor(4, 1)
1097  exe ":norm! R\<C-R>-"
1098  call assert_equal(['ZZZ', 'foofoo', '',  'βbβobarZZZZ'], getline(1, 4))
1099  let @- = 'bβb'
1100  call cursor(4, 1)
1101  exe ":norm! R\<C-R>-"
1102  call assert_equal(['ZZZ', 'foofoo', '',  'bβbobarZZZZ'], getline(1, 4))
1103  let @- = 'βbβ'
1104  call cursor(4, 1)
1105  exe ":norm! R\<C-R>-"
1106  call assert_equal(['ZZZ', 'foofoo', '',  'βbβobarZZZZ'], getline(1, 4))
1107  bwipe!
1108endfunc
1109
1110" this caused an illegal memory access and a crash
1111func Test_register_cursor_column_negative()
1112  CheckRunVimInTerminal
1113  let script =<< trim END
1114    f XREGISTER
1115    call setline(1, 'abcdef a')
1116    call setreg("a", "\n", 'c')
1117    call cursor(1, 7)
1118    call feedkeys("i\<C-R>\<C-P>azyx$#\<esc>", 't')
1119  END
1120  call writefile(script, 'XRegister123', 'D')
1121  let buf = RunVimInTerminal('-S XRegister123', {})
1122  call term_sendkeys(buf, "\<c-g>")
1123  call WaitForAssert({-> assert_match('XREGISTER', term_getline(buf, 19))})
1124  call StopVimInTerminal(buf)
1125endfunc
1126
1127" test '] mark generated by op_yank
1128func Test_mark_from_yank()
1129  new
1130  " double quote object
1131  call setline(1, 'test "yank"  mark')
1132  normal! yi"
1133  call assert_equal([0, 1, 10, 0], getpos("']"))
1134  normal! ya"
1135  call assert_equal([0, 1, 13, 0], getpos("']"))
1136  " single quote object
1137  call setline(1, 'test ''yank''  mark')
1138  normal! yi'
1139  call assert_equal([0, 1, 10, 0], getpos("']"))
1140  normal! ya'
1141  call assert_equal([0, 1, 13, 0], getpos("']"))
1142  " paren object
1143  call setline(1, 'test (yank)  mark')
1144  call cursor(1, 9)
1145  normal! yi(
1146  call assert_equal([0, 1, 10, 0], getpos("']"))
1147  call cursor(1, 9)
1148  normal! ya(
1149  call assert_equal([0, 1, 11, 0], getpos("']"))
1150  " brace object
1151  call setline(1, 'test {yank}  mark')
1152  call cursor(1, 9)
1153  normal! yi{
1154  call assert_equal([0, 1, 10, 0], getpos("']"))
1155  call cursor(1, 9)
1156  normal! ya{
1157  call assert_equal([0, 1, 11, 0], getpos("']"))
1158  " bracket object
1159  call setline(1, 'test [yank]  mark')
1160  call cursor(1, 9)
1161  normal! yi[
1162  call assert_equal([0, 1, 10, 0], getpos("']"))
1163  call cursor(1, 9)
1164  normal! ya[
1165  call assert_equal([0, 1, 11, 0], getpos("']"))
1166  " block object
1167  call setline(1, 'test <yank>  mark')
1168  call cursor(1, 9)
1169  normal! yi<
1170  call assert_equal([0, 1, 10, 0], getpos("']"))
1171  call cursor(1, 9)
1172  normal! ya<
1173  call assert_equal([0, 1, 11, 0], getpos("']"))
1174  bw!
1175endfunc
1176
1177func Test_insert_small_delete_linewise()
1178  new
1179  call setline(1, ['foo'])
1180  call cursor(1, 1)
1181  exe ":norm! \"-cc\<C-R>-"
1182  call assert_equal(['foo', ''], getline(1, '$'))
1183  bwipe!
1184endfunc
1185
1186func Test_writing_readonly_regs()
1187  call assert_fails('let @. = "foo"', 'E354:')
1188  call assert_fails('let @% = "foo"', 'E354:')
1189  call assert_fails('let @: = "foo"', 'E354:')
1190  call assert_fails('let @~ = "foo"', 'E354:')
1191endfunc
1192
1193" vim: shiftwidth=2 sts=2 expandtab