diff --git a/runtime/doc/autocmd.txt b/runtime/doc/autocmd.txt index fe49d0593e..d1db1a0936 100644 --- a/runtime/doc/autocmd.txt +++ b/runtime/doc/autocmd.txt @@ -1277,7 +1277,8 @@ TextPutPost After text has been put in the current buffer. of the expression. regname Name of the register or empty string for the unnamed - register. + register. For |nvim_put()| + this is set to '_'. regtype Type of the register, see |getregtype()|. visual True if the operation is diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index a425c52f9c..d57994f2f0 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -1462,7 +1462,7 @@ void nvim_put(ArrayOf(String) lines, String type, Boolean after, Boolean follow, TRY_WRAP(err, { bool VIsual_was_active = VIsual_active; msg_silent++; // Avoid "N more lines" message. - do_put(0, reg, after ? FORWARD : BACKWARD, 1, follow ? PUT_CURSEND : 0); + do_put('_', reg, after ? FORWARD : BACKWARD, 1, follow ? PUT_CURSEND : 0); msg_silent--; VIsual_active = VIsual_was_active; }); diff --git a/src/nvim/register.c b/src/nvim/register.c index 1df0f0a62a..38755b67d5 100644 --- a/src/nvim/register.c +++ b/src/nvim/register.c @@ -1272,7 +1272,7 @@ static void put_do_autocmd(int regname, yankreg_T *reg, const String *insert, bo { static bool recursive = false; - if (recursive || regname == '_') { + if (recursive || (regname == '_' && reg == NULL)) { return; } diff --git a/test/functional/api/vim_spec.lua b/test/functional/api/vim_spec.lua index a59ac2d521..581aaaa301 100644 --- a/test/functional/api/vim_spec.lua +++ b/test/functional/api/vim_spec.lua @@ -1555,29 +1555,53 @@ describe('API', function() ) end) it('inserts text', function() + exec([[ + let g:pre_event = [] + let g:post_event = [] + au TextPutPre * let g:pre_event = copy(v:event) + au TextPutPost * let g:post_event = copy(v:event) + ]]) -- linewise - api.nvim_put({ 'line 1', 'line 2', 'line 3' }, 'l', true, true) + local lines = { 'line 1', 'line 2', 'line 3' } + local expected_event = { + regcontents = lines, + regname = '_', + operator = 'p', + regtype = 'V', + visual = false, + } + api.nvim_put(lines, 'l', true, true) expect([[ line 1 line 2 line 3]]) eq({ 0, 4, 1, 0 }, fn.getpos('.')) + eq(expected_event, api.nvim_get_var('pre_event')) + eq(expected_event, api.nvim_get_var('post_event')) command('%delete _') -- charwise + expected_event.regtype = 'v' api.nvim_put({ 'line 1', 'line 2', 'line 3' }, 'c', true, false) expect([[ line 1 line 2 line 3]]) eq({ 0, 1, 1, 0 }, fn.getpos('.')) -- follow=false + eq(expected_event, api.nvim_get_var('pre_event')) + eq(expected_event, api.nvim_get_var('post_event')) -- blockwise - api.nvim_put({ 'AA', 'BB' }, 'b', true, true) + lines = { 'AA', 'BB' } + expected_event.regcontents = lines + expected_event.regtype = '\0222' + api.nvim_put(lines, 'b', true, true) expect([[ lAAine 1 lBBine 2 line 3]]) eq({ 0, 2, 4, 0 }, fn.getpos('.')) + eq(expected_event, api.nvim_get_var('pre_event')) + eq(expected_event, api.nvim_get_var('post_event')) command('%delete _') -- Empty lines list. api.nvim_put({}, 'c', true, true) @@ -1590,19 +1614,27 @@ describe('API', function() ]]) api.nvim_put({ 'AB' }, 'c', true, true) -- after=false, follow=true - api.nvim_put({ 'line 1', 'line 2' }, 'c', false, true) + lines = { 'line 1', 'line 2' } + expected_event.regcontents = lines + expected_event.regtype = 'v' + expected_event.operator = 'P' + api.nvim_put(lines, 'c', false, true) expect([[ Aline 1 line 2B]]) eq({ 0, 2, 7, 0 }, fn.getpos('.')) + eq(expected_event, api.nvim_get_var('pre_event')) + eq(expected_event, api.nvim_get_var('post_event')) command('%delete _') api.nvim_put({ 'AB' }, 'c', true, true) -- after=false, follow=false - api.nvim_put({ 'line 1', 'line 2' }, 'c', false, false) + api.nvim_put(lines, 'c', false, false) expect([[ Aline 1 line 2B]]) eq({ 0, 1, 2, 0 }, fn.getpos('.')) + eq(expected_event, api.nvim_get_var('pre_event')) + eq(expected_event, api.nvim_get_var('post_event')) eq('', api.nvim_eval('v:errmsg')) end)