blob: 35bd1e0a3da832018ebe31dbca70ade59eb9381a [file] [log] [blame]
Bram Moolenaar71136db2019-11-30 19:48:46 +01001" Tests for register operations
Bram Moolenaar71136db2019-11-30 19:48:46 +01002
Bram Moolenaar54c8d222019-12-02 20:41:39 +01003source check.vim
Bram Moolenaar11a5b192020-07-10 21:17:51 +02004source view_util.vim
Bram Moolenaar54c8d222019-12-02 20:41:39 +01005
Bram Moolenaar71136db2019-11-30 19:48:46 +01006" 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:')
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +010014 call assert_fails('put /', 'E35:')
15 call assert_fails('put .', 'E29:')
Bram Moolenaar71136db2019-11-30 19:48:46 +010016endfunc
Bram Moolenaare45deb72017-07-16 17:56:16 +020017
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
Bram Moolenaar7ce551f2018-05-06 17:32:19 +020044
45func Test_display_registers()
46 e file1
47 e file2
48 call setline(1, ['foo', 'bar'])
49 /bar
50 exe 'norm! y2l"axx'
51 call feedkeys("i\<C-R>=2*4\n\<esc>")
52 call feedkeys(":ls\n", 'xt')
53
54 let a = execute('display')
55 let b = execute('registers')
56
57 call assert_equal(a, b)
Bram Moolenaar3691f1e2019-10-24 20:17:00 +020058 call assert_match('^\nType Name Content\n'
59 \ . ' c "" a\n'
60 \ . ' c "0 ba\n'
61 \ . ' c "a b\n'
Bram Moolenaar7ce551f2018-05-06 17:32:19 +020062 \ . '.*'
Bram Moolenaar3691f1e2019-10-24 20:17:00 +020063 \ . ' c "- a\n'
Bram Moolenaar7ce551f2018-05-06 17:32:19 +020064 \ . '.*'
Bram Moolenaar3691f1e2019-10-24 20:17:00 +020065 \ . ' c ": ls\n'
66 \ . ' c "% file2\n'
67 \ . ' c "# file1\n'
68 \ . ' c "/ bar\n'
69 \ . ' c "= 2\*4', a)
Bram Moolenaar7ce551f2018-05-06 17:32:19 +020070
71 let a = execute('registers a')
Bram Moolenaar3691f1e2019-10-24 20:17:00 +020072 call assert_match('^\nType Name Content\n'
73 \ . ' c "a b', a)
Bram Moolenaar7ce551f2018-05-06 17:32:19 +020074
75 let a = execute('registers :')
Bram Moolenaar3691f1e2019-10-24 20:17:00 +020076 call assert_match('^\nType Name Content\n'
77 \ . ' c ": ls', a)
Bram Moolenaar7ce551f2018-05-06 17:32:19 +020078
79 bwipe!
80endfunc
Bram Moolenaar9d7fdd42019-03-08 09:50:52 +010081
82func Test_register_one()
83 " delete a line goes into register one
84 new
85 call setline(1, "one")
86 normal dd
87 call assert_equal("one\n", @1)
88
89 " delete a word does not change register one, does change "-
90 call setline(1, "two")
91 normal de
92 call assert_equal("one\n", @1)
93 call assert_equal("two", @-)
94
95 " delete a word with a register does not change register one
96 call setline(1, "three")
97 normal "ade
98 call assert_equal("three", @a)
99 call assert_equal("one\n", @1)
100
101 " delete a word with register DOES change register one with one of a list of
102 " operators
103 " %
104 call setline(1, ["(12)3"])
105 normal "ad%
106 call assert_equal("(12)", @a)
107 call assert_equal("(12)", @1)
108
109 " (
110 call setline(1, ["first second"])
111 normal $"ad(
112 call assert_equal("first secon", @a)
113 call assert_equal("first secon", @1)
114
115 " )
116 call setline(1, ["First Second."])
117 normal gg0"ad)
118 call assert_equal("First Second.", @a)
119 call assert_equal("First Second.", @1)
120
121 " `
122 call setline(1, ["start here."])
123 normal gg0fhmx0"ad`x
124 call assert_equal("start ", @a)
125 call assert_equal("start ", @1)
126
127 " /
128 call setline(1, ["searchX"])
129 exe "normal gg0\"ad/X\<CR>"
130 call assert_equal("search", @a)
131 call assert_equal("search", @1)
132
133 " ?
134 call setline(1, ["Ysearch"])
135 exe "normal gg$\"ad?Y\<CR>"
136 call assert_equal("Ysearc", @a)
137 call assert_equal("Ysearc", @1)
138
139 " n
140 call setline(1, ["Ynext"])
141 normal gg$"adn
142 call assert_equal("Ynex", @a)
143 call assert_equal("Ynex", @1)
144
145 " N
146 call setline(1, ["prevY"])
147 normal gg0"adN
148 call assert_equal("prev", @a)
149 call assert_equal("prev", @1)
150
151 " }
152 call setline(1, ["one", ""])
153 normal gg0"ad}
154 call assert_equal("one\n", @a)
155 call assert_equal("one\n", @1)
156
157 " {
158 call setline(1, ["", "two"])
159 normal 2G$"ad{
160 call assert_equal("\ntw", @a)
161 call assert_equal("\ntw", @1)
162
163 bwipe!
164endfunc
Bram Moolenaar6edbbd82019-03-10 09:41:51 +0100165
Bram Moolenaar11a5b192020-07-10 21:17:51 +0200166func Test_recording_status_in_ex_line()
167 norm qx
168 redraw!
169 call assert_equal('recording @x', Screenline(&lines))
170 set shortmess=q
171 redraw!
172 call assert_equal('recording', Screenline(&lines))
173 set shortmess&
174 norm q
175 redraw!
176 call assert_equal('', Screenline(&lines))
177endfunc
178
Bram Moolenaar6edbbd82019-03-10 09:41:51 +0100179" Check that replaying a typed sequence does not use an Esc and following
180" characters as an escape sequence.
181func Test_recording_esc_sequence()
182 new
Bram Moolenaar55d81cd2019-03-11 08:05:50 +0100183 try
184 let save_F2 = &t_F2
185 catch
186 endtry
Bram Moolenaar6edbbd82019-03-10 09:41:51 +0100187 let t_F2 = "\<Esc>OQ"
188 call feedkeys("qqiTest\<Esc>", "xt")
189 call feedkeys("OQuirk\<Esc>q", "xt")
190 call feedkeys("Go\<Esc>@q", "xt")
191 call assert_equal(['Quirk', 'Test', 'Quirk', 'Test'], getline(1, 4))
192 bwipe!
Bram Moolenaar55d81cd2019-03-11 08:05:50 +0100193 if exists('save_F2')
Bram Moolenaara5e44602019-05-25 21:52:30 +0200194 let &t_F2 = save_F2
Bram Moolenaar037c54f2019-04-20 23:47:46 +0200195 else
196 set t_F2=
Bram Moolenaar55d81cd2019-03-11 08:05:50 +0100197 endif
Bram Moolenaar6edbbd82019-03-10 09:41:51 +0100198endfunc
Bram Moolenaar71136db2019-11-30 19:48:46 +0100199
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000200func Test_recording_with_select_mode()
201 new
202 call feedkeys("qacc12345\<Esc>gH98765\<Esc>q", "tx")
203 call assert_equal("98765", getline(1))
204 call assert_equal("cc12345\<Esc>gH98765\<Esc>", @a)
205 call setline(1, 'asdf')
206 normal! @a
207 call assert_equal("98765", getline(1))
208 bwipe!
209endfunc
210
Bram Moolenaar71136db2019-11-30 19:48:46 +0100211" Test for executing the last used register (@)
212func Test_last_used_exec_reg()
213 " Test for the @: command
214 let a = ''
215 call feedkeys(":let a ..= 'Vim'\<CR>", 'xt')
216 normal @:
217 call assert_equal('VimVim', a)
218
219 " Test for the @= command
220 let x = ''
221 let a = ":let x ..= 'Vim'\<CR>"
222 exe "normal @=a\<CR>"
223 normal @@
224 call assert_equal('VimVim', x)
225
226 " Test for the @. command
227 let a = ''
228 call feedkeys("i:let a ..= 'Edit'\<CR>", 'xt')
229 normal @.
230 normal @@
231 call assert_equal('EditEdit', a)
232
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +0100233 " Test for repeating the last command-line in visual mode
234 call append(0, 'register')
235 normal gg
236 let @r = ''
237 call feedkeys("v:yank R\<CR>", 'xt')
238 call feedkeys("v@:", 'xt')
239 call assert_equal("\nregister\nregister\n", @r)
240
Bram Moolenaar71136db2019-11-30 19:48:46 +0100241 enew!
242endfunc
243
244func Test_get_register()
245 enew
246 edit Xfile1
247 edit Xfile2
248 call assert_equal('Xfile2', getreg('%'))
249 call assert_equal('Xfile1', getreg('#'))
250
251 call feedkeys("iTwo\<Esc>", 'xt')
252 call assert_equal('Two', getreg('.'))
253 call assert_equal('', getreg('_'))
254 call assert_beeps('normal ":yy')
255 call assert_beeps('normal "%yy')
256 call assert_beeps('normal ".yy')
257
258 call assert_equal('', getreg("\<C-F>"))
259 call assert_equal('', getreg("\<C-W>"))
260 call assert_equal('', getreg("\<C-L>"))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100261 " Change the last used register to '"' for the next test
262 normal! ""yy
263 let @" = 'happy'
264 call assert_equal('happy', getreg())
265 call assert_equal('happy', getreg(''))
Bram Moolenaar71136db2019-11-30 19:48:46 +0100266
267 call assert_equal('', getregtype('!'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100268 call assert_fails('echo getregtype([])', 'E730:')
269 call assert_equal('v', getregtype())
270 call assert_equal('v', getregtype(''))
Bram Moolenaar71136db2019-11-30 19:48:46 +0100271
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +0100272 " Test for inserting an invalid register content
273 call assert_beeps('exe "normal i\<C-R>!"')
274
275 " Test for inserting a register with multiple lines
276 call deletebufline('', 1, '$')
277 call setreg('r', ['a', 'b'])
278 exe "normal i\<C-R>r"
279 call assert_equal(['a', 'b', ''], getline(1, '$'))
280
281 " Test for inserting a multi-line register in the command line
282 call feedkeys(":\<C-R>r\<Esc>", 'xt')
283 call assert_equal("a\rb\r", histget(':', -1))
284
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200285 call assert_fails('let r = getreg("=", [])', 'E745:')
286 call assert_fails('let r = getreg("=", 1, [])', 'E745:')
Bram Moolenaar71136db2019-11-30 19:48:46 +0100287 enew!
Bram Moolenaard1ad99b2020-10-04 16:16:54 +0200288
289 " Using a register in operator-pending mode should fail
290 call assert_beeps('norm! c"')
Bram Moolenaar71136db2019-11-30 19:48:46 +0100291endfunc
292
293func Test_set_register()
294 call assert_fails("call setreg('#', 200)", 'E86:')
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +0200295 call assert_fails("call setreg('a', test_unknown())", 'E908:')
Bram Moolenaar71136db2019-11-30 19:48:46 +0100296
297 edit Xfile_alt_1
298 let b1 = bufnr('')
299 edit Xfile_alt_2
300 let b2 = bufnr('')
301 edit Xfile_alt_3
302 let b3 = bufnr('')
303 call setreg('#', 'alt_1')
304 call assert_equal('Xfile_alt_1', getreg('#'))
305 call setreg('#', b2)
306 call assert_equal('Xfile_alt_2', getreg('#'))
307
308 let ab = 'regwrite'
309 call setreg('=', '')
310 call setreg('=', 'a', 'a')
311 call setreg('=', 'b', 'a')
312 call assert_equal('regwrite', getreg('='))
313
Bram Moolenaar4b96df52020-01-26 22:00:26 +0100314 " Test for setting a list of lines to special registers
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +0100315 call setreg('/', [])
316 call assert_equal('', @/)
317 call setreg('=', [])
318 call assert_equal('', @=)
319 call assert_fails("call setreg('/', ['a', 'b'])", 'E883:')
320 call assert_fails("call setreg('=', ['a', 'b'])", 'E883:')
321 call assert_equal(0, setreg('_', ['a', 'b']))
322
323 " Test for recording to a invalid register
324 call assert_beeps('normal q$')
325
326 " Appending to a register when recording
327 call append(0, "text for clipboard test")
328 normal gg
329 call feedkeys('qrllq', 'xt')
330 call feedkeys('qRhhq', 'xt')
331 call assert_equal('llhh', getreg('r'))
332
Bram Moolenaar54c8d222019-12-02 20:41:39 +0100333 " Appending a list of characters to a register from different lines
334 let @r = ''
335 call append(0, ['abcdef', '123456'])
336 normal gg"ry3l
337 call cursor(2, 4)
338 normal "Ry3l
339 call assert_equal('abc456', @r)
340
341 " Test for gP with multiple lines selected using characterwise motion
342 %delete
343 call append(0, ['vim editor', 'vim editor'])
344 let @r = ''
345 exe "normal ggwy/vim /e\<CR>gP"
346 call assert_equal(['vim editor', 'vim editor', 'vim editor'], getline(1, 3))
347
348 " Test for gP with . register
349 %delete
350 normal iabc
351 normal ".gp
352 call assert_equal('abcabc', getline(1))
353 normal 0".gP
354 call assert_equal('abcabcabc', getline(1))
355
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100356 let @"=''
357 call setreg('', '1')
358 call assert_equal('1', @")
359 call setreg('@', '2')
360 call assert_equal('2', @")
361
Bram Moolenaar71136db2019-11-30 19:48:46 +0100362 enew!
363endfunc
364
Bram Moolenaar54c8d222019-12-02 20:41:39 +0100365" Test for clipboard registers (* and +)
366func Test_clipboard_regs()
367 CheckNotGui
368 CheckFeature clipboard_working
369
370 new
371 call append(0, "text for clipboard test")
372 normal gg"*yiw
373 call assert_equal('text', getreg('*'))
374 normal gg2w"+yiw
375 call assert_equal('clipboard', getreg('+'))
376
377 " Test for replacing the clipboard register contents
378 set clipboard=unnamed
379 let @* = 'food'
380 normal ggviw"*p
381 call assert_equal('text', getreg('*'))
382 call assert_equal('food for clipboard test', getline(1))
383 normal ggviw"*p
384 call assert_equal('food', getreg('*'))
385 call assert_equal('text for clipboard test', getline(1))
386
387 " Test for replacing the selection register contents
388 set clipboard=unnamedplus
389 let @+ = 'food'
390 normal ggviw"+p
391 call assert_equal('text', getreg('+'))
392 call assert_equal('food for clipboard test', getline(1))
393 normal ggviw"+p
394 call assert_equal('food', getreg('+'))
395 call assert_equal('text for clipboard test', getline(1))
396
397 " Test for auto copying visually selected text to clipboard register
398 call setline(1, "text for clipboard test")
399 let @* = ''
400 set clipboard=autoselect
401 normal ggwwviwy
402 call assert_equal('clipboard', @*)
403
404 " Test for auto copying visually selected text to selection register
405 let @+ = ''
406 set clipboard=autoselectplus
407 normal ggwviwy
408 call assert_equal('for', @+)
409
410 set clipboard&vim
411 bwipe!
412endfunc
413
Ernie Rael559f2302022-07-26 14:44:36 +0100414" Test unnamed for both clipboard registers (* and +)
415func Test_clipboard_regs_both_unnamed()
416 CheckNotGui
417 CheckFeature clipboard_working
418 CheckTwoClipboards
419
420 let @* = 'xxx'
421 let @+ = 'xxx'
422
423 new
424
425 set clipboard=unnamed,unnamedplus
426 call setline(1, ['foo', 'bar'])
427
428 " op_yank copies to both
429 :1
430 :normal yw
431 call assert_equal('foo', getreg('*'))
432 call assert_equal('foo', getreg('+'))
433
434 " op_delete only copies to '+'
435 :2
436 :normal dw
437 call assert_equal('foo', getreg('*'))
438 call assert_equal('bar', getreg('+'))
439
440 set clipboard&vim
441 bwipe!
442endfunc
443
Bram Moolenaar54c8d222019-12-02 20:41:39 +0100444" Test for restarting the current mode (insert or virtual replace) after
445" executing the contents of a register
446func Test_put_reg_restart_mode()
447 new
448 call append(0, 'editor')
449 normal gg
450 let @r = "ivim \<Esc>"
451 call feedkeys("i\<C-O>@r\<C-R>=mode()\<CR>", 'xt')
452 call assert_equal('vimi editor', getline(1))
453
454 call setline(1, 'editor')
455 normal gg
456 call feedkeys("gR\<C-O>@r\<C-R>=mode()\<CR>", 'xt')
457 call assert_equal('vimReditor', getline(1))
458
459 bwipe!
460endfunc
461
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100462" Test for executing a register using :@ command
463func Test_execute_register()
464 call setreg('r', [])
465 call assert_beeps('@r')
466 let i = 1
467 let @q = 'let i+= 1'
468 @q
469 @
470 call assert_equal(3, i)
Bram Moolenaar1671f442020-03-10 07:48:13 +0100471
Bram Moolenaar8a9bc952020-10-02 18:48:07 +0200472 " try to execute expression register and use a backspace to cancel it
473 new
474 call feedkeys("@=\<BS>ax\<CR>y", 'xt')
475 call assert_equal(['x', 'y'], getline(1, '$'))
476 close!
477
Bram Moolenaar1671f442020-03-10 07:48:13 +0100478 " cannot execute a register in operator pending mode
479 call assert_beeps('normal! c@r')
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100480endfunc
481
Bram Moolenaarbb861e22020-06-07 18:16:36 +0200482" Test for getting register info
483func Test_get_reginfo()
484 enew
485 call setline(1, ['foo', 'bar'])
486
487 exe 'norm! "zyy'
488 let info = getreginfo('"')
489 call assert_equal('z', info.points_to)
490 call setreg('y', 'baz')
491 call assert_equal('z', getreginfo('').points_to)
492 call setreg('y', { 'isunnamed': v:true })
493 call assert_equal('y', getreginfo('"').points_to)
494
495 exe '$put'
496 call assert_equal(getreg('y'), getline(3))
497 call setreg('', 'qux')
498 call assert_equal('0', getreginfo('').points_to)
499 call setreg('x', 'quux')
500 call assert_equal('0', getreginfo('').points_to)
501
502 let info = getreginfo('')
503 call assert_equal(getreg('', 1, 1), info.regcontents)
504 call assert_equal(getregtype(''), info.regtype)
505
506 exe "norm! 0\<c-v>e" .. '"zy'
507 let info = getreginfo('z')
508 call assert_equal(getreg('z', 1, 1), info.regcontents)
509 call assert_equal(getregtype('z'), info.regtype)
510 call assert_equal(1, +info.isunnamed)
511
512 let info = getreginfo('"')
513 call assert_equal('z', info.points_to)
514
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +0200515 let @a="a1b2"
516 nnoremap <F2> <Cmd>let g:RegInfo = getreginfo()<CR>
517 exe "normal \"a\<F2>"
518 call assert_equal({'regcontents': ['a1b2'], 'isunnamed': v:false,
519 \ 'regtype': 'v'}, g:RegInfo)
520 nunmap <F2>
521 unlet g:RegInfo
522
Bram Moolenaarbb861e22020-06-07 18:16:36 +0200523 bwipe!
524endfunc
525
526" Test for restoring register with dict from getreginfo
527func Test_set_register_dict()
528 enew!
529
530 call setreg('"', #{ regcontents: ['one', 'two'],
531 \ regtype: 'V', points_to: 'z' })
532 call assert_equal(['one', 'two'], getreg('"', 1, 1))
533 let info = getreginfo('"')
534 call assert_equal('z', info.points_to)
535 call assert_equal('V', info.regtype)
536 call assert_equal(1, +getreginfo('z').isunnamed)
537
538 call setreg('x', #{ regcontents: ['three', 'four'],
539 \ regtype: 'v', isunnamed: v:true })
540 call assert_equal(['three', 'four'], getreg('"', 1, 1))
541 let info = getreginfo('"')
542 call assert_equal('x', info.points_to)
543 call assert_equal('v', info.regtype)
544 call assert_equal(1, +getreginfo('x').isunnamed)
545
546 call setreg('y', #{ regcontents: 'five',
547 \ regtype: "\<c-v>", isunnamed: v:false })
548 call assert_equal("\<c-v>4", getreginfo('y').regtype)
549 call assert_equal(0, +getreginfo('y').isunnamed)
550 call assert_equal(['three', 'four'], getreg('"', 1, 1))
551 call assert_equal('x', getreginfo('"').points_to)
552
553 call setreg('"', #{ regcontents: 'six' })
554 call assert_equal('0', getreginfo('"').points_to)
555 call assert_equal(1, +getreginfo('0').isunnamed)
556 call assert_equal(['six'], getreginfo('0').regcontents)
557 call assert_equal(['six'], getreginfo('"').regcontents)
558
Bram Moolenaar7633fe52020-06-22 19:10:56 +0200559 let @x = 'one'
560 call setreg('x', {})
561 call assert_equal(1, len(split(execute('reg x'), '\n')))
562
563 call assert_fails("call setreg('0', #{regtype: 'V'}, 'v')", 'E118:')
564 call assert_fails("call setreg('0', #{regtype: 'X'})", 'E475:')
565 call assert_fails("call setreg('0', #{regtype: 'vy'})", 'E475:')
566
Bram Moolenaarbb861e22020-06-07 18:16:36 +0200567 bwipe!
568endfunc
569
Bram Moolenaarcc613032020-06-07 21:31:18 +0200570func Test_v_register()
571 enew
572 call setline(1, 'nothing')
573
574 func s:Put()
575 let s:register = v:register
576 exec 'normal! "' .. v:register .. 'P'
577 endfunc
578 nnoremap <buffer> <plug>(test) :<c-u>call s:Put()<cr>
579 nmap <buffer> S <plug>(test)
580
581 let @z = "testz\n"
582 let @" = "test@\n"
583
584 let s:register = ''
585 call feedkeys('"_ddS', 'mx')
586 call assert_equal('test@', getline('.')) " fails before 8.2.0929
587 call assert_equal('"', s:register) " fails before 8.2.0929
588
589 let s:register = ''
590 call feedkeys('"zS', 'mx')
591 call assert_equal('z', s:register)
592
593 let s:register = ''
594 call feedkeys('"zSS', 'mx')
595 call assert_equal('"', s:register)
596
597 let s:register = ''
598 call feedkeys('"_S', 'mx')
599 call assert_equal('_', s:register)
600
601 let s:register = ''
602 normal "_ddS
603 call assert_equal('"', s:register) " fails before 8.2.0929
604 call assert_equal('test@', getline('.')) " fails before 8.2.0929
605
606 let s:register = ''
607 execute 'normal "z:call' "s:Put()\n"
608 call assert_equal('z', s:register)
609 call assert_equal('testz', getline('.'))
610
611 " Test operator and omap
612 let @b = 'testb'
613 func s:OpFunc(...)
614 let s:register2 = v:register
615 endfunc
616 set opfunc=s:OpFunc
617
618 normal "bg@l
619 normal S
620 call assert_equal('"', s:register) " fails before 8.2.0929
621 call assert_equal('b', s:register2)
622
623 func s:Motion()
624 let s:register1 = v:register
625 normal! l
626 endfunc
627 onoremap <buffer> Q :<c-u>call s:Motion()<cr>
628
629 normal "bg@Q
630 normal S
631 call assert_equal('"', s:register)
632 call assert_equal('b', s:register1)
633 call assert_equal('"', s:register2)
634
635 set opfunc&
636 bwipe!
637endfunc
638
Bram Moolenaar856c1112020-06-17 21:47:23 +0200639" Test for executing the contents of a register as an Ex command with line
640" continuation.
641func Test_execute_reg_as_ex_cmd()
642 " Line continuation with just two lines
643 let code =<< trim END
644 let l = [
645 \ 1]
646 END
647 let @r = code->join("\n")
648 let l = []
649 @r
650 call assert_equal([1], l)
651
652 " Line continuation with more than two lines
653 let code =<< trim END
654 let l = [
655 \ 1,
656 \ 2,
657 \ 3]
658 END
659 let @r = code->join("\n")
660 let l = []
661 @r
662 call assert_equal([1, 2, 3], l)
663
664 " use comments interspersed with code
665 let code =<< trim END
666 let l = [
667 "\ one
668 \ 1,
669 "\ two
670 \ 2,
671 "\ three
672 \ 3]
673 END
674 let @r = code->join("\n")
675 let l = []
676 @r
677 call assert_equal([1, 2, 3], l)
678
679 " use line continuation in the middle
680 let code =<< trim END
681 let a = "one"
682 let l = [
683 \ 1,
684 \ 2]
685 let b = "two"
686 END
687 let @r = code->join("\n")
688 let l = []
689 @r
690 call assert_equal([1, 2], l)
691 call assert_equal("one", a)
692 call assert_equal("two", b)
693
694 " only one line with a \
695 let @r = "\\let l = 1"
696 call assert_fails('@r', 'E10:')
697
698 " only one line with a "\
699 let @r = ' "\ let i = 1'
700 @r
701 call assert_false(exists('i'))
702
703 " first line also begins with a \
704 let @r = "\\let l = [\n\\ 1]"
705 call assert_fails('@r', 'E10:')
706
707 " Test with a large number of lines
708 let @r = "let str = \n"
709 let @r ..= repeat(" \\ 'abcdefghijklmnopqrstuvwxyz' ..\n", 312)
710 let @r ..= ' \ ""'
711 @r
712 call assert_equal(repeat('abcdefghijklmnopqrstuvwxyz', 312), str)
713endfunc
714
Bram Moolenaar25fd2672020-06-22 20:30:27 +0200715" Test for clipboard registers with ASCII NUL
716func Test_clipboard_nul()
717 CheckFeature clipboard_working
718 new
719
720 " Test for putting ASCII NUL into the clipboard
721 set clipboard=unnamed
722 call append(0, "\ntest")
723 normal ggyyp
724 call assert_equal("^@test^@", strtrans(getreg('*')))
725 call assert_equal(getline(1), getline(2))
726 let b = split(execute(":reg *"), "\n")
727 call assert_match('"\*\s*\^@test\^J',b[1])
728
729 set clipboard&vim
730 bwipe!
731endfunc
732
Bram Moolenaaref85a9b2020-07-10 20:24:07 +0200733func Test_ve_blockpaste()
734 new
735 set ve=all
736 0put =['QWERTZ','ASDFGH']
737 call cursor(1,1)
738 exe ":norm! \<C-V>3ljdP"
739 call assert_equal(1, col('.'))
740 call assert_equal(getline(1, 2), ['QWERTZ', 'ASDFGH'])
741 call cursor(1,1)
742 exe ":norm! \<C-V>3ljd"
743 call cursor(1,1)
744 norm! $3lP
745 call assert_equal(5, col('.'))
746 call assert_equal(getline(1, 2), ['TZ QWER', 'GH ASDF'])
747 set ve&vim
748 bwipe!
749endfunc
750
Bram Moolenaar032a2d02020-12-22 17:59:35 +0100751func Test_insert_small_delete()
752 new
753 call setline(1, ['foo foobar bar'])
754 call cursor(1,1)
755 exe ":norm! ciw'\<C-R>-'"
Bram Moolenaar9f63a652020-12-23 12:50:20 +0100756 call assert_equal("'foo' foobar bar", getline(1))
Bram Moolenaar032a2d02020-12-22 17:59:35 +0100757 exe ":norm! w.w."
Bram Moolenaar9f63a652020-12-23 12:50:20 +0100758 call assert_equal("'foo' 'foobar' 'bar'", getline(1))
Bram Moolenaar032a2d02020-12-22 17:59:35 +0100759 bwipe!
760endfunc
761
Bram Moolenaarf4fcedc2021-03-15 18:36:20 +0100762" Record in insert mode using CTRL-O
763func Test_record_in_insert_mode()
764 new
765 let @r = ''
766 call setline(1, ['foo'])
767 call feedkeys("i\<C-O>qrbaz\<C-O>q", 'xt')
768 call assert_equal('baz', @r)
769 bwipe!
770endfunc
771
Bram Moolenaara4bc2dd2022-01-27 19:27:16 +0000772func Test_record_in_select_mode()
773 new
774 call setline(1, 'text')
775 sil norm q00
776 sil norm q
777 call assert_equal('0ext', getline(1))
zeertzjqfbf4f1c2022-01-28 12:50:43 +0000778
779 %delete
780 let @r = ''
781 call setline(1, ['abc', 'abc', 'abc'])
782 smap <F2> <Right><Right>,
783 call feedkeys("qrgh\<F2>Dk\<Esc>q", 'xt')
784 call assert_equal("gh\<F2>Dk\<Esc>", @r)
785 norm j0@rj0@@
786 call assert_equal([',Dk', ',Dk', ',Dk'], getline(1, 3))
787 sunmap <F2>
788
Bram Moolenaara4bc2dd2022-01-27 19:27:16 +0000789 bwipe!
790endfunc
791
zeertzjq81b46a62022-04-09 17:58:49 +0100792" mapping that ends macro recording should be removed from recorded macro
793func Test_end_record_using_mapping()
794 call setline(1, 'aaa')
795 nnoremap s q
796 call feedkeys('safas', 'tx')
797 call assert_equal('fa', @a)
798 nunmap s
799
800 nnoremap xx q
801 call feedkeys('0xxafaxx', 'tx')
802 call assert_equal('fa', @a)
803 nunmap xx
804
805 nnoremap xsx q
806 call feedkeys('0qafaxsx', 'tx')
807 call assert_equal('fa', @a)
808 nunmap xsx
809
810 bwipe!
811endfunc
812
zeertzjq6d4e7252022-04-07 13:58:04 +0100813func Test_end_reg_executing()
814 nnoremap s <Nop>
815 let @a = 's'
816 call feedkeys("@aqaq\<Esc>", 'tx')
817 call assert_equal('', @a)
818 call assert_equal('', getline(1))
819
820 call setline(1, 'aaa')
821 nnoremap s qa
822 let @a = 'fa'
823 call feedkeys("@asq\<Esc>", 'tx')
824 call assert_equal('', @a)
825 call assert_equal('aaa', getline(1))
826
827 nunmap s
828 bwipe!
829endfunc
830
Christian Brabandt78eb9cc2021-09-14 18:55:51 +0200831" Make sure that y_append is correctly reset
832" and the previous register is working as expected
833func Test_register_y_append_reset()
834 new
835 call setline(1, ['1',
836 \ '2 ----------------------------------------------------',
837 \ '3',
838 \ '4',
839 \ '5 ----------------------------------------------------',
840 \ '6',
841 \ '7',
842 \ '8 ----------------------------------------------------',
843 \ '9',
844 \ '10 aaaaaaa 4.',
845 \ '11 Game Dbl-Figures Leaders:',
846 \ '12 Player Pts FG% 3P% FT% RB AS BL ST TO PF EFF',
847 \ '13 bbbbbbbbb 12 (50 /0 /67 )/ 7/ 3/ 0/ 2/ 3/ 4/+15',
848 \ '14 cccccc 12 (57 /67 /100)/ 2/ 1/ 1/ 0/ 1/ 3/+12',
849 \ '15 ddddddd 10 (63 /0 /0 )/ 1/ 3/ 0/ 3/ 5/ 3/ +9',
850 \ '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',
851 \ '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',
852 \ '18 4 3 6 3 2 3 3 4 3 3 7 3 1 4 6 -1 -1 +2 -1 -2',
853 \ '19 F 13 19 5 10 4 17 22 9 14 32 13 4 20 17 -1 -13 -4 -3 -3 +5'])
854 11
855 exe "norm! \"a5dd"
856 norm! j
857 exe "norm! \"bY"
858 norm! 2j
859 exe "norm! \"BY"
860 norm! 4k
861 norm! 5dd
862 norm! 3k
863 " The next put should put the content of the unnamed register, not of
864 " register b!
865 norm! p
866 call assert_equal(['1',
867 \ '2 ----------------------------------------------------',
868 \ '3',
869 \ '4',
870 \ '5 ----------------------------------------------------',
871 \ '6',
872 \ '10 aaaaaaa 4.',
873 \ '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',
874 \ '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',
875 \ '18 4 3 6 3 2 3 3 4 3 3 7 3 1 4 6 -1 -1 +2 -1 -2',
876 \ '19 F 13 19 5 10 4 17 22 9 14 32 13 4 20 17 -1 -13 -4 -3 -3 +5',
877 \ '7',
878 \ '8 ----------------------------------------------------',
879 \ '9'], getline(1,'$'))
880 bwipe!
881endfunc
882
Bram Moolenaar71136db2019-11-30 19:48:46 +0100883" vim: shiftwidth=2 sts=2 expandtab