2016-04-23 16:53:11 -07:00
|
|
|
local helpers = require('test.functional.helpers')(after_each)
|
2016-04-26 12:14:33 -07:00
|
|
|
local clear, eval, eq = helpers.clear, helpers.eval, helpers.eq
|
2017-04-08 17:31:13 -07:00
|
|
|
local feed, command, expect = helpers.feed, helpers.command, helpers.expect
|
2024-01-12 10:59:57 -07:00
|
|
|
local api, fn, neq = helpers.api, helpers.fn, helpers.neq
|
2016-02-29 07:28:10 -07:00
|
|
|
|
|
|
|
describe('TextYankPost', function()
|
|
|
|
before_each(function()
|
|
|
|
clear()
|
|
|
|
|
|
|
|
-- emulate the clipboard so system clipboard isn't affected
|
2023-05-29 16:18:12 -07:00
|
|
|
command('set rtp^=test/functional/fixtures')
|
2016-02-29 07:28:10 -07:00
|
|
|
|
2017-04-08 14:12:26 -07:00
|
|
|
command('let g:count = 0')
|
|
|
|
command('autocmd TextYankPost * let g:event = copy(v:event)')
|
|
|
|
command('autocmd TextYankPost * let g:count += 1')
|
2016-02-29 07:28:10 -07:00
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_buf_set_lines(0, 0, -1, true, {
|
2016-02-29 07:28:10 -07:00
|
|
|
'foo\0bar',
|
|
|
|
'baz text',
|
|
|
|
})
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('is executed after yank and handles register types', function()
|
|
|
|
feed('yy')
|
|
|
|
eq({
|
2019-03-12 19:24:09 -07:00
|
|
|
inclusive = false,
|
2016-02-29 07:28:10 -07:00
|
|
|
operator = 'y',
|
|
|
|
regcontents = { 'foo\nbar' },
|
|
|
|
regname = '',
|
2020-06-03 07:51:25 -07:00
|
|
|
regtype = 'V',
|
|
|
|
visual = false,
|
2016-02-29 07:28:10 -07:00
|
|
|
}, eval('g:event'))
|
|
|
|
eq(1, eval('g:count'))
|
|
|
|
|
|
|
|
-- v:event is cleared after the autocommand is done
|
|
|
|
eq({}, eval('v:event'))
|
|
|
|
|
|
|
|
feed('+yw')
|
|
|
|
eq({
|
2019-03-12 19:24:09 -07:00
|
|
|
inclusive = false,
|
2016-02-29 07:28:10 -07:00
|
|
|
operator = 'y',
|
|
|
|
regcontents = { 'baz ' },
|
|
|
|
regname = '',
|
2020-06-03 07:51:25 -07:00
|
|
|
regtype = 'v',
|
|
|
|
visual = false,
|
2016-02-29 07:28:10 -07:00
|
|
|
}, eval('g:event'))
|
|
|
|
eq(2, eval('g:count'))
|
|
|
|
|
|
|
|
feed('<c-v>eky')
|
|
|
|
eq({
|
2019-03-12 19:24:09 -07:00
|
|
|
inclusive = true,
|
2016-02-29 07:28:10 -07:00
|
|
|
operator = 'y',
|
|
|
|
regcontents = { 'foo', 'baz' },
|
|
|
|
regname = '',
|
2020-06-03 07:51:25 -07:00
|
|
|
regtype = '\0223', -- ^V + block width
|
|
|
|
visual = true,
|
2016-02-29 07:28:10 -07:00
|
|
|
}, eval('g:event'))
|
|
|
|
eq(3, eval('g:count'))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('makes v:event immutable', function()
|
|
|
|
feed('yy')
|
|
|
|
eq({
|
2019-03-12 19:24:09 -07:00
|
|
|
inclusive = false,
|
2016-02-29 07:28:10 -07:00
|
|
|
operator = 'y',
|
|
|
|
regcontents = { 'foo\nbar' },
|
|
|
|
regname = '',
|
2020-06-03 07:51:25 -07:00
|
|
|
regtype = 'V',
|
|
|
|
visual = false,
|
2016-02-29 07:28:10 -07:00
|
|
|
}, eval('g:event'))
|
|
|
|
|
2017-04-08 14:12:26 -07:00
|
|
|
command('set debug=msg')
|
2016-02-29 07:28:10 -07:00
|
|
|
-- the regcontents should not be changed without copy.
|
|
|
|
local status, err = pcall(command, 'call extend(g:event.regcontents, ["more text"])')
|
|
|
|
eq(status, false)
|
|
|
|
neq(nil, string.find(err, ':E742:'))
|
|
|
|
|
|
|
|
-- can't mutate keys inside the autocommand
|
2017-04-08 14:12:26 -07:00
|
|
|
command('autocmd! TextYankPost * let v:event.regcontents = 0')
|
2016-02-29 07:28:10 -07:00
|
|
|
status, err = pcall(command, 'normal yy')
|
|
|
|
eq(status, false)
|
|
|
|
neq(nil, string.find(err, ':E46:'))
|
|
|
|
|
|
|
|
-- can't add keys inside the autocommand
|
2017-04-08 14:12:26 -07:00
|
|
|
command('autocmd! TextYankPost * let v:event.mykey = 0')
|
2016-02-29 07:28:10 -07:00
|
|
|
status, err = pcall(command, 'normal yy')
|
|
|
|
eq(status, false)
|
|
|
|
neq(nil, string.find(err, ':E742:'))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('is not invoked recursively', function()
|
2017-04-08 14:12:26 -07:00
|
|
|
command('autocmd TextYankPost * normal "+yy')
|
2016-02-29 07:28:10 -07:00
|
|
|
feed('yy')
|
|
|
|
eq({
|
2019-03-12 19:24:09 -07:00
|
|
|
inclusive = false,
|
2016-02-29 07:28:10 -07:00
|
|
|
operator = 'y',
|
|
|
|
regcontents = { 'foo\nbar' },
|
|
|
|
regname = '',
|
2020-06-03 07:51:25 -07:00
|
|
|
regtype = 'V',
|
|
|
|
visual = false,
|
2016-02-29 07:28:10 -07:00
|
|
|
}, eval('g:event'))
|
|
|
|
eq(1, eval('g:count'))
|
2024-01-12 10:59:57 -07:00
|
|
|
eq({ 'foo\nbar' }, fn.getreg('+', 1, 1))
|
2016-02-29 07:28:10 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('is executed after delete and change', function()
|
|
|
|
feed('dw')
|
|
|
|
eq({
|
2019-03-12 19:24:09 -07:00
|
|
|
inclusive = false,
|
2016-02-29 07:28:10 -07:00
|
|
|
operator = 'd',
|
|
|
|
regcontents = { 'foo' },
|
|
|
|
regname = '',
|
2020-06-03 07:51:25 -07:00
|
|
|
regtype = 'v',
|
|
|
|
visual = false,
|
2016-02-29 07:28:10 -07:00
|
|
|
}, eval('g:event'))
|
|
|
|
eq(1, eval('g:count'))
|
|
|
|
|
|
|
|
feed('dd')
|
|
|
|
eq({
|
2019-03-12 19:24:09 -07:00
|
|
|
inclusive = false,
|
2016-02-29 07:28:10 -07:00
|
|
|
operator = 'd',
|
|
|
|
regcontents = { '\nbar' },
|
|
|
|
regname = '',
|
2020-06-03 07:51:25 -07:00
|
|
|
regtype = 'V',
|
|
|
|
visual = false,
|
2016-02-29 07:28:10 -07:00
|
|
|
}, eval('g:event'))
|
|
|
|
eq(2, eval('g:count'))
|
|
|
|
|
|
|
|
feed('cwspam<esc>')
|
|
|
|
eq({
|
2019-03-12 19:24:09 -07:00
|
|
|
inclusive = true,
|
2016-02-29 07:28:10 -07:00
|
|
|
operator = 'c',
|
|
|
|
regcontents = { 'baz' },
|
|
|
|
regname = '',
|
2020-06-03 07:51:25 -07:00
|
|
|
regtype = 'v',
|
|
|
|
visual = false,
|
2016-02-29 07:28:10 -07:00
|
|
|
}, 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'))
|
|
|
|
|
2017-04-08 14:12:26 -07:00
|
|
|
command('delete _')
|
2016-02-29 07:28:10 -07:00
|
|
|
eq(0, eval('g:count'))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('gives the correct register name', function()
|
|
|
|
feed('$"byiw')
|
|
|
|
eq({
|
2019-03-12 19:24:09 -07:00
|
|
|
inclusive = true,
|
2016-02-29 07:28:10 -07:00
|
|
|
operator = 'y',
|
|
|
|
regcontents = { 'bar' },
|
|
|
|
regname = 'b',
|
2020-06-03 07:51:25 -07:00
|
|
|
regtype = 'v',
|
|
|
|
visual = false,
|
2016-02-29 07:28:10 -07:00
|
|
|
}, eval('g:event'))
|
|
|
|
|
|
|
|
feed('"*yy')
|
|
|
|
eq({
|
2019-03-12 19:24:09 -07:00
|
|
|
inclusive = true,
|
2016-02-29 07:28:10 -07:00
|
|
|
operator = 'y',
|
|
|
|
regcontents = { 'foo\nbar' },
|
|
|
|
regname = '*',
|
2020-06-03 07:51:25 -07:00
|
|
|
regtype = 'V',
|
|
|
|
visual = false,
|
2016-02-29 07:28:10 -07:00
|
|
|
}, eval('g:event'))
|
|
|
|
|
2017-04-08 14:12:26 -07:00
|
|
|
command('set clipboard=unnamed')
|
2016-02-29 07:28:10 -07:00
|
|
|
|
|
|
|
-- regname still shows the name the user requested
|
|
|
|
feed('yy')
|
|
|
|
eq({
|
2019-03-12 19:24:09 -07:00
|
|
|
inclusive = true,
|
2016-02-29 07:28:10 -07:00
|
|
|
operator = 'y',
|
|
|
|
regcontents = { 'foo\nbar' },
|
|
|
|
regname = '',
|
2020-06-03 07:51:25 -07:00
|
|
|
regtype = 'V',
|
|
|
|
visual = false,
|
2016-02-29 07:28:10 -07:00
|
|
|
}, eval('g:event'))
|
|
|
|
|
|
|
|
feed('"*yy')
|
|
|
|
eq({
|
2019-03-12 19:24:09 -07:00
|
|
|
inclusive = true,
|
2016-02-29 07:28:10 -07:00
|
|
|
operator = 'y',
|
|
|
|
regcontents = { 'foo\nbar' },
|
|
|
|
regname = '*',
|
2020-06-03 07:51:25 -07:00
|
|
|
regtype = 'V',
|
|
|
|
visual = false,
|
2016-02-29 07:28:10 -07:00
|
|
|
}, eval('g:event'))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('works with Ex commands', function()
|
2017-04-08 14:12:26 -07:00
|
|
|
command('1delete +')
|
2016-02-29 07:28:10 -07:00
|
|
|
eq({
|
2019-03-12 19:24:09 -07:00
|
|
|
inclusive = false,
|
2016-02-29 07:28:10 -07:00
|
|
|
operator = 'd',
|
|
|
|
regcontents = { 'foo\nbar' },
|
|
|
|
regname = '+',
|
2020-06-03 07:51:25 -07:00
|
|
|
regtype = 'V',
|
|
|
|
visual = false,
|
2016-02-29 07:28:10 -07:00
|
|
|
}, eval('g:event'))
|
|
|
|
eq(1, eval('g:count'))
|
|
|
|
|
2017-04-08 14:12:26 -07:00
|
|
|
command('yank')
|
2016-02-29 07:28:10 -07:00
|
|
|
eq({
|
2019-03-12 19:24:09 -07:00
|
|
|
inclusive = false,
|
2016-02-29 07:28:10 -07:00
|
|
|
operator = 'y',
|
|
|
|
regcontents = { 'baz text' },
|
|
|
|
regname = '',
|
2020-06-03 07:51:25 -07:00
|
|
|
regtype = 'V',
|
|
|
|
visual = false,
|
2016-02-29 07:28:10 -07:00
|
|
|
}, eval('g:event'))
|
|
|
|
eq(2, eval('g:count'))
|
|
|
|
|
2017-04-08 14:12:26 -07:00
|
|
|
command('normal yw')
|
2016-02-29 07:28:10 -07:00
|
|
|
eq({
|
2019-03-12 19:24:09 -07:00
|
|
|
inclusive = false,
|
2016-02-29 07:28:10 -07:00
|
|
|
operator = 'y',
|
|
|
|
regcontents = { 'baz ' },
|
|
|
|
regname = '',
|
2020-06-03 07:51:25 -07:00
|
|
|
regtype = 'v',
|
|
|
|
visual = false,
|
2016-02-29 07:28:10 -07:00
|
|
|
}, eval('g:event'))
|
|
|
|
eq(3, eval('g:count'))
|
|
|
|
|
2017-04-08 14:12:26 -07:00
|
|
|
command('normal! dd')
|
2016-02-29 07:28:10 -07:00
|
|
|
eq({
|
2019-03-12 19:24:09 -07:00
|
|
|
inclusive = false,
|
2016-02-29 07:28:10 -07:00
|
|
|
operator = 'd',
|
|
|
|
regcontents = { 'baz text' },
|
|
|
|
regname = '',
|
2020-06-03 07:51:25 -07:00
|
|
|
regtype = 'V',
|
|
|
|
visual = false,
|
2016-02-29 07:28:10 -07:00
|
|
|
}, eval('g:event'))
|
|
|
|
eq(4, eval('g:count'))
|
|
|
|
end)
|
|
|
|
|
2019-07-28 15:50:12 -07:00
|
|
|
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)
|
2016-02-29 07:28:10 -07:00
|
|
|
end)
|