aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/_editor.lua
diff options
context:
space:
mode:
authorzeertzjq <zeertzjq@outlook.com>2022-03-06 06:56:24 +0800
committerzeertzjq <zeertzjq@outlook.com>2022-03-15 18:15:18 +0800
commita6eafc77ceaf2d7036aed89361b6556f46131b17 (patch)
tree0d019d1bb94125849652aa80f468e223a8a52a64 /runtime/lua/vim/_editor.lua
parentfcc6f66cf2a67cf85e72727a08e19d0f800badb9 (diff)
downloadrneovim-a6eafc77ceaf2d7036aed89361b6556f46131b17.tar.gz
rneovim-a6eafc77ceaf2d7036aed89361b6556f46131b17.tar.bz2
rneovim-a6eafc77ceaf2d7036aed89361b6556f46131b17.zip
fix(paste): deal with trailing new line in chunk
Diffstat (limited to 'runtime/lua/vim/_editor.lua')
-rw-r--r--runtime/lua/vim/_editor.lua10
1 files changed, 7 insertions, 3 deletions
diff --git a/runtime/lua/vim/_editor.lua b/runtime/lua/vim/_editor.lua
index 42adda6e7f..26b9693189 100644
--- a/runtime/lua/vim/_editor.lua
+++ b/runtime/lua/vim/_editor.lua
@@ -128,7 +128,7 @@ local function inspect(object, options) -- luacheck: no unused
end
do
- local tdots, tick, got_line1, undo_started = 0, 0, false, false
+ local tdots, tick, got_line1, undo_started, trailing_nl = 0, 0, false, false, false
--- Paste handler, invoked by |nvim_paste()| when a conforming UI
--- (such as the |TUI|) pastes text into the editor.
@@ -160,7 +160,7 @@ do
local is_first_chunk = phase < 2
local is_last_chunk = phase == -1 or phase == 3
if is_first_chunk then -- Reset flags.
- tdots, tick, got_line1, undo_started = now, 0, false, false
+ tdots, tick, got_line1, undo_started, trailing_nl = now, 0, false, false, false
end
if #lines == 0 then
lines = {''}
@@ -203,7 +203,10 @@ do
vim.api.nvim_buf_set_lines(0, row-1, row, false, lines)
elseif mode:find('^[nvV\22sS\19]') then -- Normal or Visual or Select mode
if mode:find('^n') then -- Normal mode
- vim.api.nvim_put(lines, 'c', true, false)
+ -- When there was a trailing new line in the previous chunk,
+ -- the cursor is on the first character of the next line,
+ -- so paste before the cursor instead of after it.
+ vim.api.nvim_put(lines, 'c', not trailing_nl, false)
else -- Visual or Select mode
vim.api.nvim_command([[exe "silent normal! \<Del>"]])
local del_start = vim.fn.getpos("'[")
@@ -221,6 +224,7 @@ do
end
-- put cursor at the end of the text instead of one character after it
vim.fn.setpos('.', vim.fn.getpos("']"))
+ trailing_nl = lines[#lines] == ''
else -- Don't know what to do in other modes
return false
end