123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- local t = require('test.testutil')
- local n = require('test.functional.testnvim')()
- local clear, eval, eq = n.clear, n.eval, t.eq
- local feed, command, expect = n.feed, n.command, n.expect
- local api, fn, neq = n.api, n.fn, t.neq
- describe('TextYankPost', function()
- before_each(function()
- clear()
- -- emulate the clipboard so system clipboard isn't affected
- command('set rtp^=test/functional/fixtures')
- command('let g:count = 0')
- command('autocmd TextYankPost * let g:event = copy(v:event)')
- command('autocmd TextYankPost * let g:count += 1')
- api.nvim_buf_set_lines(0, 0, -1, true, {
- 'foo\0bar',
- 'baz text',
- })
- end)
- it('is executed after yank and handles register types', function()
- feed('yy')
- eq({
- inclusive = false,
- operator = 'y',
- regcontents = { 'foo\nbar' },
- regname = '',
- regtype = 'V',
- visual = false,
- }, eval('g:event'))
- eq(1, eval('g:count'))
- -- v:event is cleared after the autocommand is done
- eq({}, eval('v:event'))
- feed('+yw')
- eq({
- inclusive = false,
- operator = 'y',
- regcontents = { 'baz ' },
- regname = '',
- regtype = 'v',
- visual = false,
- }, eval('g:event'))
- eq(2, eval('g:count'))
- feed('<c-v>eky')
- eq({
- inclusive = true,
- operator = 'y',
- regcontents = { 'foo', 'baz' },
- regname = '',
- regtype = '\0223', -- ^V + block width
- visual = true,
- }, eval('g:event'))
- eq(3, eval('g:count'))
- end)
- it('makes v:event immutable', function()
- feed('yy')
- eq({
- inclusive = false,
- operator = 'y',
- regcontents = { 'foo\nbar' },
- regname = '',
- regtype = 'V',
- visual = false,
- }, eval('g:event'))
- command('set debug=msg')
- -- the regcontents should not be changed without copy.
- local status, err = pcall(command, 'call extend(g:event.regcontents, ["more text"])')
- eq(false, status)
- neq(nil, string.find(err, ':E742:'))
- -- can't mutate keys inside the autocommand
- command('autocmd! TextYankPost * let v:event.regcontents = 0')
- status, err = pcall(command, 'normal yy')
- eq(false, status)
- neq(nil, string.find(err, ':E46:'))
- -- can't add keys inside the autocommand
- command('autocmd! TextYankPost * let v:event.mykey = 0')
- status, err = pcall(command, 'normal yy')
- eq(false, status)
- neq(nil, string.find(err, ':E742:'))
- end)
- it('is not invoked recursively', function()
- command('autocmd TextYankPost * normal "+yy')
- feed('yy')
- eq({
- inclusive = false,
- operator = 'y',
- regcontents = { 'foo\nbar' },
- regname = '',
- regtype = 'V',
- visual = false,
- }, eval('g:event'))
- eq(1, eval('g:count'))
- eq({ 'foo\nbar' }, fn.getreg('+', 1, 1))
- end)
- it('is executed after delete and change', function()
- feed('dw')
- eq({
- inclusive = false,
- operator = 'd',
- regcontents = { 'foo' },
- regname = '',
- regtype = 'v',
- visual = false,
- }, eval('g:event'))
- eq(1, eval('g:count'))
- feed('dd')
- eq({
- inclusive = false,
- operator = 'd',
- regcontents = { '\nbar' },
- regname = '',
- regtype = 'V',
- visual = false,
- }, eval('g:event'))
- eq(2, eval('g:count'))
- feed('cwspam<esc>')
- eq({
- inclusive = true,
- operator = 'c',
- regcontents = { 'baz' },
- regname = '',
- regtype = 'v',
- visual = false,
- }, eval('g:event'))
- eq(3, eval('g:count'))
- end)
- it('is not executed after black-hole operation', function()
- feed('"_dd')
- eq(0, eval('g:count'))
- feed('"_cwgood<esc>')
- eq(0, eval('g:count'))
- expect([[
- good text]])
- feed('"_yy')
- eq(0, eval('g:count'))
- command('delete _')
- eq(0, eval('g:count'))
- end)
- it('gives the correct register name', function()
- feed('$"byiw')
- eq({
- inclusive = true,
- operator = 'y',
- regcontents = { 'bar' },
- regname = 'b',
- regtype = 'v',
- visual = false,
- }, eval('g:event'))
- feed('"*yy')
- eq({
- inclusive = true,
- operator = 'y',
- regcontents = { 'foo\nbar' },
- regname = '*',
- regtype = 'V',
- visual = false,
- }, eval('g:event'))
- command('set clipboard=unnamed')
- -- regname still shows the name the user requested
- feed('yy')
- eq({
- inclusive = true,
- operator = 'y',
- regcontents = { 'foo\nbar' },
- regname = '',
- regtype = 'V',
- visual = false,
- }, eval('g:event'))
- feed('"*yy')
- eq({
- inclusive = true,
- operator = 'y',
- regcontents = { 'foo\nbar' },
- regname = '*',
- regtype = 'V',
- visual = false,
- }, eval('g:event'))
- end)
- it('works with Ex commands', function()
- command('1delete +')
- eq({
- inclusive = false,
- operator = 'd',
- regcontents = { 'foo\nbar' },
- regname = '+',
- regtype = 'V',
- visual = false,
- }, eval('g:event'))
- eq(1, eval('g:count'))
- command('yank')
- eq({
- inclusive = false,
- operator = 'y',
- regcontents = { 'baz text' },
- regname = '',
- regtype = 'V',
- visual = false,
- }, eval('g:event'))
- eq(2, eval('g:count'))
- command('normal yw')
- eq({
- inclusive = false,
- operator = 'y',
- regcontents = { 'baz ' },
- regname = '',
- regtype = 'v',
- visual = false,
- }, eval('g:event'))
- eq(3, eval('g:count'))
- command('normal! dd')
- eq({
- inclusive = false,
- operator = 'd',
- regcontents = { 'baz text' },
- regname = '',
- regtype = 'V',
- visual = false,
- }, eval('g:event'))
- eq(4, eval('g:count'))
- end)
- it('updates numbered registers correctly #10225', function()
- command('autocmd TextYankPost * let g:reg = getreg("1")')
- feed('"adj')
- eq('foo\nbar\nbaz text\n', eval('g:reg'))
- end)
- end)
|