paste: make vim.paste() "public"

This commit is contained in:
Justin M. Keyes
2019-08-27 05:19:25 +02:00
parent ed60015266
commit 87389c6a57
6 changed files with 89 additions and 56 deletions

View File

@@ -93,47 +93,6 @@ local function _os_proc_children(ppid)
return children
end
-- Default nvim_paste() handler.
local _paste = (function()
local tdots, tredraw, tick, got_line1 = 0, 0, 0, false
return function(lines, phase)
local call = vim.api.nvim_call_function
local now = vim.loop.now()
local mode = call('mode', {}):sub(1,1)
if phase < 2 then -- Reset flags.
tdots, tredraw, tick, got_line1 = now, now, 0, false
end
if mode == 'c' 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, _ = string.gsub(lines[1], '[\r\n\012\027]', ' ')
vim.api.nvim_input(line1) -- Scrub "\r".
elseif mode == 'i' or mode == 'R' then
vim.api.nvim_put(lines, 'c', false, true)
else
vim.api.nvim_put(lines, 'c', true, true)
end
if (now - tredraw >= 1000) or phase == -1 or phase > 2 then
tredraw = now
vim.api.nvim_command('redraw')
vim.api.nvim_command('redrawstatus')
end
if phase ~= -1 and (now - tdots >= 100) then
local dots = ('.'):rep(tick % 4)
tdots = now
tick = tick + 1
-- Use :echo because Lua print('') is a no-op, and we want to clear the
-- message when there are zero dots.
vim.api.nvim_command(('echo "%s"'):format(dots))
end
if phase == -1 or phase == 3 then
vim.api.nvim_command('echo ""')
vim.api.nvim_set_option('paste', false)
end
return true -- Paste will not continue if not returning `true`.
end
end)()
-- TODO(ZyX-I): Create compatibility layer.
--{{{1 package.path updater function
-- Last inserted paths. Used to clear out items from package.[c]path when they
@@ -202,6 +161,59 @@ local function inspect(object, options) -- luacheck: no unused
error(object, options) -- Stub for gen_vimdoc.py
end
--- Paste handler, invoked by |nvim_paste()| when a conforming UI
--- (such as the |TUI|) pastes text into the editor.
---
--@see |paste|
---
--@param lines |readfile()|-style list of lines to paste. |channel-lines|
--@param phase -1: "non-streaming" paste: the call contains all lines.
--- If paste is "streamed", `phase` indicates the stream state:
--- - 1: starts the paste (exactly once)
--- - 2: continues the paste (zero or more times)
--- - 3: ends the paste (exactly once)
--@returns false if client should cancel the paste.
local function paste(lines, phase) end -- luacheck: no unused
paste = (function()
local tdots, tredraw, tick, got_line1 = 0, 0, 0, false
return function(lines, phase)
local call = vim.api.nvim_call_function
local now = vim.loop.now()
local mode = call('mode', {}):sub(1,1)
if phase < 2 then -- Reset flags.
tdots, tredraw, tick, got_line1 = now, now, 0, false
end
if mode == 'c' 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, _ = string.gsub(lines[1], '[\r\n\012\027]', ' ')
vim.api.nvim_input(line1) -- Scrub "\r".
elseif mode == 'i' or mode == 'R' then
vim.api.nvim_put(lines, 'c', false, true)
else
vim.api.nvim_put(lines, 'c', true, true)
end
if (now - tredraw >= 1000) or phase == -1 or phase > 2 then
tredraw = now
vim.api.nvim_command('redraw')
vim.api.nvim_command('redrawstatus')
end
if phase ~= -1 and (now - tdots >= 100) then
local dots = ('.'):rep(tick % 4)
tdots = now
tick = tick + 1
-- Use :echo because Lua print('') is a no-op, and we want to clear the
-- message when there are zero dots.
vim.api.nvim_command(('echo "%s"'):format(dots))
end
if phase == -1 or phase == 3 then
vim.api.nvim_command('echo ""')
vim.api.nvim_set_option('paste', false)
end
return true -- Paste will not continue if not returning `true`.
end
end)()
--- Defers the wrapped callback until the Nvim API is safe to call.
---
--@see |vim-loop-callbacks|
@@ -227,8 +239,8 @@ local module = {
_update_package_paths = _update_package_paths,
_os_proc_children = _os_proc_children,
_os_proc_info = _os_proc_info,
_paste = _paste,
_system = _system,
paste = paste,
schedule_wrap = schedule_wrap,
}