From 9b1e1fbc9f795921afd25ba38be5c79dec8b04d2 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sun, 6 Mar 2022 06:56:24 +0800 Subject: fix(paste): use getcmdtype() to determine whether in cmdline mode --- runtime/lua/vim/_editor.lua | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'runtime/lua/vim/_editor.lua') diff --git a/runtime/lua/vim/_editor.lua b/runtime/lua/vim/_editor.lua index a0c60a7dcf..8000730795 100644 --- a/runtime/lua/vim/_editor.lua +++ b/runtime/lua/vim/_editor.lua @@ -156,21 +156,21 @@ do --- - 3: ends the paste (exactly once) ---@returns false if client should cancel the paste. function vim.paste(lines, phase) - local call = vim.api.nvim_call_function local now = vim.loop.now() - local mode = call('mode', {}):sub(1,1) + local mode = vim.api.nvim_get_mode().mode + local is_cmdline = vim.fn.getcmdtype() ~= '' if phase < 2 then -- Reset flags. tdots, tick, got_line1 = now, 0, false - elseif mode ~= 'c' then + elseif not is_cmdline then vim.api.nvim_command('undojoin') end - if mode == 'c' and not got_line1 then -- cmdline-mode: paste only 1 line. + if is_cmdline and not got_line1 then -- cmdline-mode: paste only 1 line. got_line1 = (#lines > 1) vim.api.nvim_set_option('paste', true) -- For nvim_input(). local line1 = lines[1]:gsub('<', ''):gsub('[\r\n\012\027]', ' ') -- Scrub. vim.api.nvim_input(line1) vim.api.nvim_set_option('paste', false) - elseif mode ~= 'c' then + elseif not is_cmdline then if phase < 2 and mode:find('^[vV\22sS\19]') then vim.api.nvim_command([[exe "normal! \"]]) vim.api.nvim_put(lines, 'c', false, true) @@ -178,7 +178,7 @@ do vim.api.nvim_put(lines, 'c', true, true) -- XXX: Normal-mode: workaround bad cursor-placement after first chunk. vim.api.nvim_command('normal! a') - elseif phase < 2 and mode == 'R' then + elseif phase < 2 and mode:find('^R') then local nchars = 0 for _, line in ipairs(lines) do nchars = nchars + line:len() -- cgit From 2601e0873ff50ed804487dff00bd27e233709beb Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sun, 6 Mar 2022 06:56:24 +0800 Subject: fix(paste): don't move cursor past the end of pasted text in Normal mode --- runtime/lua/vim/_editor.lua | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) (limited to 'runtime/lua/vim/_editor.lua') diff --git a/runtime/lua/vim/_editor.lua b/runtime/lua/vim/_editor.lua index 8000730795..8e49b51cec 100644 --- a/runtime/lua/vim/_editor.lua +++ b/runtime/lua/vim/_editor.lua @@ -171,27 +171,34 @@ do vim.api.nvim_input(line1) vim.api.nvim_set_option('paste', false) elseif not is_cmdline then - if phase < 2 and mode:find('^[vV\22sS\19]') then - vim.api.nvim_command([[exe "normal! \"]]) + if mode:find('^i') or mode:find('^n?t') then -- Insert mode or Terminal buffer vim.api.nvim_put(lines, 'c', false, true) - elseif phase < 2 and not mode:find('^[iRt]') then - vim.api.nvim_put(lines, 'c', true, true) - -- XXX: Normal-mode: workaround bad cursor-placement after first chunk. - vim.api.nvim_command('normal! a') - elseif phase < 2 and mode:find('^R') then + elseif phase < 2 and mode:find('^R') and not mode:find('^Rv') then -- Replace mode + -- TODO: implement Replace mode streamed pasting + -- TODO: support Virtual Replace mode local nchars = 0 for _, line in ipairs(lines) do - nchars = nchars + line:len() + nchars = nchars + line:len() end local row, col = unpack(vim.api.nvim_win_get_cursor(0)) local bufline = vim.api.nvim_buf_get_lines(0, row-1, row, true)[1] local firstline = lines[1] firstline = bufline:sub(1, col)..firstline lines[1] = firstline + -- FIXME: #lines can be 0 lines[#lines] = lines[#lines]..bufline:sub(col + nchars + 1, bufline:len()) vim.api.nvim_buf_set_lines(0, row-1, row, false, lines) - else - vim.api.nvim_put(lines, 'c', false, true) + 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) + else -- Visual or Select mode + vim.api.nvim_command([[exe "normal! \"]]) + vim.api.nvim_put(lines, 'c', false, false) + end + -- put cursor at the end of the text instead of one character after it + vim.fn.setpos('.', vim.fn.getpos("']")) + else -- Don't know what to do in other modes + return false end end if phase ~= -1 and (now - tdots >= 100) then -- cgit From bfb77544425b7cca372cb87f00ef6b6e87c5f6d5 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sun, 6 Mar 2022 06:56:24 +0800 Subject: fix(paste): deal with eol and eof in Visual mode --- runtime/lua/vim/_editor.lua | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'runtime/lua/vim/_editor.lua') diff --git a/runtime/lua/vim/_editor.lua b/runtime/lua/vim/_editor.lua index 8e49b51cec..6b1725c9ff 100644 --- a/runtime/lua/vim/_editor.lua +++ b/runtime/lua/vim/_editor.lua @@ -192,8 +192,19 @@ do if mode:find('^n') then -- Normal mode vim.api.nvim_put(lines, 'c', true, false) else -- Visual or Select mode - vim.api.nvim_command([[exe "normal! \"]]) - vim.api.nvim_put(lines, 'c', false, false) + vim.api.nvim_command([[exe "silent normal! \"]]) + local del_start = vim.fn.getpos("'[") + local cursor_pos = vim.fn.getpos('.') + if mode:find('^[VS]') then -- linewise + if cursor_pos[2] < del_start[2] then -- replacing lines at eof + -- create a new line + vim.api.nvim_put({''}, 'l', true, true) + end + vim.api.nvim_put(lines, 'c', false, false) + else + -- paste after cursor when replacing text at eol, otherwise paste before cursor + vim.api.nvim_put(lines, 'c', cursor_pos[3] < del_start[3], false) + end end -- put cursor at the end of the text instead of one character after it vim.fn.setpos('.', vim.fn.getpos("']")) -- cgit From 21ba2d81a848e7b85739fc3e9aa2eb0b5e35c879 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sun, 6 Mar 2022 06:56:24 +0800 Subject: refactor(paste): do not print dots in cmdline mode --- runtime/lua/vim/_editor.lua | 100 +++++++++++++++++++++++--------------------- 1 file changed, 52 insertions(+), 48 deletions(-) (limited to 'runtime/lua/vim/_editor.lua') diff --git a/runtime/lua/vim/_editor.lua b/runtime/lua/vim/_editor.lua index 6b1725c9ff..a7f8f0e7b6 100644 --- a/runtime/lua/vim/_editor.lua +++ b/runtime/lua/vim/_editor.lua @@ -157,60 +157,64 @@ do ---@returns false if client should cancel the paste. function vim.paste(lines, phase) local now = vim.loop.now() - local mode = vim.api.nvim_get_mode().mode - local is_cmdline = vim.fn.getcmdtype() ~= '' - if phase < 2 then -- Reset flags. + local is_first_chunk = phase < 2 + if is_first_chunk then -- Reset flags. tdots, tick, got_line1 = now, 0, false - elseif not is_cmdline then + end + -- Note: mode doesn't always start with "c" in cmdline mode, so use getcmdtype() instead. + if vim.fn.getcmdtype() ~= '' then -- cmdline-mode: paste only 1 line. + if not got_line1 then + got_line1 = (#lines > 1) + vim.api.nvim_set_option('paste', true) -- For nvim_input(). + local line1 = lines[1]:gsub('<', ''):gsub('[\r\n\012\027]', ' ') -- Scrub. + vim.api.nvim_input(line1) + vim.api.nvim_set_option('paste', false) + end + return true + end + local mode = vim.api.nvim_get_mode().mode + if not is_first_chunk then vim.api.nvim_command('undojoin') end - if is_cmdline and not got_line1 then -- cmdline-mode: paste only 1 line. - got_line1 = (#lines > 1) - vim.api.nvim_set_option('paste', true) -- For nvim_input(). - local line1 = lines[1]:gsub('<', ''):gsub('[\r\n\012\027]', ' ') -- Scrub. - vim.api.nvim_input(line1) - vim.api.nvim_set_option('paste', false) - elseif not is_cmdline then - if mode:find('^i') or mode:find('^n?t') then -- Insert mode or Terminal buffer - vim.api.nvim_put(lines, 'c', false, true) - elseif phase < 2 and mode:find('^R') and not mode:find('^Rv') then -- Replace mode - -- TODO: implement Replace mode streamed pasting - -- TODO: support Virtual Replace mode - local nchars = 0 - for _, line in ipairs(lines) do - nchars = nchars + line:len() - end - local row, col = unpack(vim.api.nvim_win_get_cursor(0)) - local bufline = vim.api.nvim_buf_get_lines(0, row-1, row, true)[1] - local firstline = lines[1] - firstline = bufline:sub(1, col)..firstline - lines[1] = firstline - -- FIXME: #lines can be 0 - lines[#lines] = lines[#lines]..bufline:sub(col + nchars + 1, bufline:len()) - 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) - else -- Visual or Select mode - vim.api.nvim_command([[exe "silent normal! \"]]) - local del_start = vim.fn.getpos("'[") - local cursor_pos = vim.fn.getpos('.') - if mode:find('^[VS]') then -- linewise - if cursor_pos[2] < del_start[2] then -- replacing lines at eof - -- create a new line - vim.api.nvim_put({''}, 'l', true, true) - end - vim.api.nvim_put(lines, 'c', false, false) - else - -- paste after cursor when replacing text at eol, otherwise paste before cursor - vim.api.nvim_put(lines, 'c', cursor_pos[3] < del_start[3], false) + if mode:find('^i') or mode:find('^n?t') then -- Insert mode or Terminal buffer + vim.api.nvim_put(lines, 'c', false, true) + elseif phase < 2 and mode:find('^R') and not mode:find('^Rv') then -- Replace mode + -- TODO: implement Replace mode streamed pasting + -- TODO: support Virtual Replace mode + local nchars = 0 + for _, line in ipairs(lines) do + nchars = nchars + line:len() + end + local row, col = unpack(vim.api.nvim_win_get_cursor(0)) + local bufline = vim.api.nvim_buf_get_lines(0, row-1, row, true)[1] + local firstline = lines[1] + firstline = bufline:sub(1, col)..firstline + lines[1] = firstline + -- FIXME: #lines can be 0 + lines[#lines] = lines[#lines]..bufline:sub(col + nchars + 1, bufline:len()) + 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) + else -- Visual or Select mode + vim.api.nvim_command([[exe "silent normal! \"]]) + local del_start = vim.fn.getpos("'[") + local cursor_pos = vim.fn.getpos('.') + if mode:find('^[VS]') then -- linewise + if cursor_pos[2] < del_start[2] then -- replacing lines at eof + -- create a new line + vim.api.nvim_put({''}, 'l', true, true) end + vim.api.nvim_put(lines, 'c', false, false) + else + -- paste after cursor when replacing text at eol, otherwise paste before cursor + vim.api.nvim_put(lines, 'c', cursor_pos[3] < del_start[3], false) end - -- put cursor at the end of the text instead of one character after it - vim.fn.setpos('.', vim.fn.getpos("']")) - else -- Don't know what to do in other modes - return false end + -- put cursor at the end of the text instead of one character after it + vim.fn.setpos('.', vim.fn.getpos("']")) + else -- Don't know what to do in other modes + return false end if phase ~= -1 and (now - tdots >= 100) then local dots = ('.'):rep(tick % 4) -- cgit From fcc6f66cf2a67cf85e72727a08e19d0f800badb9 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sun, 6 Mar 2022 06:56:24 +0800 Subject: fix(paste): avoid edges cases caused by empty chunk --- runtime/lua/vim/_editor.lua | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'runtime/lua/vim/_editor.lua') diff --git a/runtime/lua/vim/_editor.lua b/runtime/lua/vim/_editor.lua index a7f8f0e7b6..42adda6e7f 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 = 0, 0, false + local tdots, tick, got_line1, undo_started = 0, 0, false, false --- Paste handler, invoked by |nvim_paste()| when a conforming UI --- (such as the |TUI|) pastes text into the editor. @@ -158,8 +158,17 @@ do function vim.paste(lines, phase) local now = vim.loop.now() 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 = now, 0, false + tdots, tick, got_line1, undo_started = now, 0, false, false + end + if #lines == 0 then + lines = {''} + end + if #lines == 1 and lines[1] == '' and not is_last_chunk then + -- An empty chunk can cause some edge cases in streamed pasting, + -- so don't do anything unless it is the last chunk. + return true end -- Note: mode doesn't always start with "c" in cmdline mode, so use getcmdtype() instead. if vim.fn.getcmdtype() ~= '' then -- cmdline-mode: paste only 1 line. @@ -173,7 +182,7 @@ do return true end local mode = vim.api.nvim_get_mode().mode - if not is_first_chunk then + if undo_started then vim.api.nvim_command('undojoin') end if mode:find('^i') or mode:find('^n?t') then -- Insert mode or Terminal buffer @@ -190,7 +199,6 @@ do local firstline = lines[1] firstline = bufline:sub(1, col)..firstline lines[1] = firstline - -- FIXME: #lines can be 0 lines[#lines] = lines[#lines]..bufline:sub(col + nchars + 1, bufline:len()) 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 @@ -216,6 +224,7 @@ do else -- Don't know what to do in other modes return false end + undo_started = true if phase ~= -1 and (now - tdots >= 100) then local dots = ('.'):rep(tick % 4) tdots = now @@ -224,7 +233,7 @@ do -- message when there are zero dots. vim.api.nvim_command(('echo "%s"'):format(dots)) end - if phase == -1 or phase == 3 then + if is_last_chunk then vim.api.nvim_command('redraw'..(tick > 1 and '|echo ""' or '')) end return true -- Paste will not continue if not returning `true`. -- cgit From a6eafc77ceaf2d7036aed89361b6556f46131b17 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sun, 6 Mar 2022 06:56:24 +0800 Subject: fix(paste): deal with trailing new line in chunk --- runtime/lua/vim/_editor.lua | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'runtime/lua/vim/_editor.lua') 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! \"]]) 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 -- cgit From e263afc0e972d11d3b9b663c3ac0b5575c4deb88 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 15 Mar 2022 06:04:50 +0800 Subject: fix(paste): escape control characters in Cmdline mode --- runtime/lua/vim/_editor.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'runtime/lua/vim/_editor.lua') diff --git a/runtime/lua/vim/_editor.lua b/runtime/lua/vim/_editor.lua index 26b9693189..d4db4850bd 100644 --- a/runtime/lua/vim/_editor.lua +++ b/runtime/lua/vim/_editor.lua @@ -175,7 +175,8 @@ do if not got_line1 then got_line1 = (#lines > 1) vim.api.nvim_set_option('paste', true) -- For nvim_input(). - local line1 = lines[1]:gsub('<', ''):gsub('[\r\n\012\027]', ' ') -- Scrub. + -- Escape "<" and control characters + local line1 = lines[1]:gsub('<', ''):gsub('(%c)', '\022%1') vim.api.nvim_input(line1) vim.api.nvim_set_option('paste', false) end -- cgit