diff options
author | Justin M. Keyes <justinkz@gmail.com> | 2019-08-27 05:19:25 +0200 |
---|---|---|
committer | Justin M. Keyes <justinkz@gmail.com> | 2019-08-27 22:14:52 +0200 |
commit | 87389c6a57cf9fa91746503c479cdbea348030b9 (patch) | |
tree | 97f9cfa3d464b62add16fd2994a44935c567b005 /src/nvim/lua/vim.lua | |
parent | ed60015266356b3c0c42aa34698d9287f22fcba1 (diff) | |
download | rneovim-87389c6a57cf9fa91746503c479cdbea348030b9.tar.gz rneovim-87389c6a57cf9fa91746503c479cdbea348030b9.tar.bz2 rneovim-87389c6a57cf9fa91746503c479cdbea348030b9.zip |
paste: make vim.paste() "public"
Diffstat (limited to 'src/nvim/lua/vim.lua')
-rw-r--r-- | src/nvim/lua/vim.lua | 96 |
1 files changed, 54 insertions, 42 deletions
diff --git a/src/nvim/lua/vim.lua b/src/nvim/lua/vim.lua index 05951fbd0f..fd34b8545d 100644 --- a/src/nvim/lua/vim.lua +++ b/src/nvim/lua/vim.lua @@ -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, } |