API: nvim_put: "follow" parameter

This commit is contained in:
Justin M. Keyes 2019-08-20 23:53:13 +02:00
parent 613296936b
commit 93e5f0235b
3 changed files with 34 additions and 13 deletions

View File

@ -1206,7 +1206,7 @@ Dictionary nvim_get_namespaces(void)
return retval; return retval;
} }
/// Inserts text at cursor. /// Puts text at cursor.
/// ///
/// Compare |:put| and |p| which are always linewise. /// Compare |:put| and |p| which are always linewise.
/// ///
@ -1216,10 +1216,11 @@ Dictionary nvim_get_namespaces(void)
/// - "c" |characterwise| mode /// - "c" |characterwise| mode
/// - "l" |linewise| mode /// - "l" |linewise| mode
/// - "" guess by contents /// - "" guess by contents
/// @param direction Behave like |P| instead of |p| /// @param after Insert after cursor (like |p|), or before (like |P|).
/// @param follow Place cursor at end of inserted text.
/// @param[out] err Error details, if any /// @param[out] err Error details, if any
void nvim_put(ArrayOf(String) lines, String type, Boolean direction, void nvim_put(ArrayOf(String) lines, String type, Boolean after,
Error *err) Boolean follow, Error *err)
FUNC_API_SINCE(6) FUNC_API_SINCE(6)
{ {
yankreg_T *reg = xcalloc(sizeof(yankreg_T), 1); yankreg_T *reg = xcalloc(sizeof(yankreg_T), 1);
@ -1249,9 +1250,10 @@ void nvim_put(ArrayOf(String) lines, String type, Boolean direction,
finish_yankreg_from_object(reg, false); finish_yankreg_from_object(reg, false);
bool VIsual_was_active = VIsual_active; bool VIsual_was_active = VIsual_active;
int flags = PUT_CURSEND;
msg_silent++; // Avoid "N more lines" message. msg_silent++; // Avoid "N more lines" message.
do_put(0, reg, direction ? BACKWARD : FORWARD, 1, flags); do_put(0, reg,
after ? FORWARD : BACKWARD, 1,
follow ? PUT_CURSEND : 0);
msg_silent--; msg_silent--;
VIsual_active = VIsual_was_active; VIsual_active = VIsual_was_active;

View File

@ -100,7 +100,7 @@ local function _paste(lines)
local mode = call('mode', {}) local mode = call('mode', {})
local curline = call('line', {'.'}) local curline = call('line', {'.'})
-- vim.api.nvim_set_option('paste', true) -- vim.api.nvim_set_option('paste', true)
vim.api.nvim_put(lines, 'c', false) vim.api.nvim_put(lines, 'c', true, true)
-- vim.api.nvim_set_option('paste', false) -- vim.api.nvim_set_option('paste', false)
-- TODO: do not redraw (slow!) until paste is finished. -- TODO: do not redraw (slow!) until paste is finished.
-- if eof then -- if eof then

View File

@ -369,34 +369,53 @@ describe('API', function()
describe('nvim_put', function() describe('nvim_put', function()
it('inserts text', function() it('inserts text', function()
-- linewise -- linewise
nvim('put', {'line 1','line 2','line 3'}, 'l', false) nvim('put', {'line 1','line 2','line 3'}, 'l', true, true)
expect([[ expect([[
line 1 line 1
line 2 line 2
line 3]]) line 3]])
eq({0,4,1,0}, funcs.getpos('.'))
command('%delete _') command('%delete _')
-- charwise -- charwise
nvim('put', {'line 1','line 2','line 3'}, 'c', false) nvim('put', {'line 1','line 2','line 3'}, 'c', true, false)
expect([[ expect([[
line 1 line 1
line 2 line 2
line 3]]) line 3]])
command('1') eq({0,1,1,0}, funcs.getpos('.')) -- follow=false
-- blockwise -- blockwise
nvim('put', {'AA','BB'}, 'b', false) nvim('put', {'AA','BB'}, 'b', true, true)
expect([[ expect([[
lAAine 1 lAAine 1
lBBine 2 lBBine 2
line 3]]) line 3]])
eq({0,2,4,0}, funcs.getpos('.'))
command('%delete _') command('%delete _')
-- Empty lines list. -- Empty lines list.
nvim('put', {}, 'c', false) nvim('put', {}, 'c', true, true)
eq({0,1,1,0}, funcs.getpos('.'))
expect([[]]) expect([[]])
-- Single empty line. -- Single empty line.
nvim('put', {''}, 'c', false) nvim('put', {''}, 'c', true, true)
eq({0,1,1,0}, funcs.getpos('.'))
expect([[ expect([[
]]) ]])
nvim('put', {'AB'}, 'c', true, true)
-- after=false, follow=true
nvim('put', {'line 1','line 2'}, 'c', false, true)
expect([[
Aline 1
line 2B]])
eq({0,2,7,0}, funcs.getpos('.'))
command('%delete _')
nvim('put', {'AB'}, 'c', true, true)
-- after=false, follow=false
nvim('put', {'line 1','line 2'}, 'c', false, false)
expect([[
Aline 1
line 2B]])
eq({0,1,2,0}, funcs.getpos('.'))
eq('', nvim('eval', 'v:errmsg')) eq('', nvim('eval', 'v:errmsg'))
end) end)
end) end)