diff options
author | Justin M. Keyes <justinkz@gmail.com> | 2019-08-30 00:16:19 +0200 |
---|---|---|
committer | Justin M. Keyes <justinkz@gmail.com> | 2019-08-30 08:33:14 +0200 |
commit | b6192a9920a75d787e256453a841b9742a7f0599 (patch) | |
tree | 79f2a4fd4a9eec18c1ae650b418f28def3e4de0b | |
parent | 4b8a16153e79644a5df9f6dc94215ac009c26c33 (diff) | |
download | rneovim-b6192a9920a75d787e256453a841b9742a7f0599.tar.gz rneovim-b6192a9920a75d787e256453a841b9742a7f0599.tar.bz2 rneovim-b6192a9920a75d787e256453a841b9742a7f0599.zip |
API: nvim_paste: add `crlf` parameter
-rw-r--r-- | runtime/doc/api.txt | 4 | ||||
-rw-r--r-- | src/nvim/api/vim.c | 7 | ||||
-rw-r--r-- | src/nvim/tui/input.c | 2 | ||||
-rw-r--r-- | test/functional/api/vim_spec.lua | 23 |
4 files changed, 22 insertions, 14 deletions
diff --git a/runtime/doc/api.txt b/runtime/doc/api.txt index 32d7f5eb1e..710ab0cf33 100644 --- a/runtime/doc/api.txt +++ b/runtime/doc/api.txt @@ -793,11 +793,12 @@ nvim_get_namespaces() *nvim_get_namespaces()* Return: ~ dict that maps from names to namespace ids. -nvim_paste({data}, {phase}) *nvim_paste()* +nvim_paste({data}, {crlf}, {phase}) *nvim_paste()* Pastes at cursor, in any mode. Invokes the `vim.paste` handler, which handles each mode appropriately. Sets redo/undo. Faster than |nvim_input()|. + Lines break at LF ("\n"). Errors ('nomodifiable', `vim.paste()` failure, …) are reflected in `err` but do not affect the return value (which @@ -808,6 +809,7 @@ nvim_paste({data}, {phase}) *nvim_paste()* Parameters: ~ {data} Multiline input. May be binary (containing NUL bytes). + {crlf} Also break lines at CR and CRLF. {phase} -1: paste in a single call (i.e. without streaming). To "stream" a paste, call `nvim_paste` sequentially with these `phase` values: • 1: starts the paste (exactly once) diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index 8063a6f1fd..a39ee5d038 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -1211,7 +1211,7 @@ Dictionary nvim_get_namespaces(void) /// Pastes at cursor, in any mode. /// /// Invokes the `vim.paste` handler, which handles each mode appropriately. -/// Sets redo/undo. Faster than |nvim_input()|. +/// Sets redo/undo. Faster than |nvim_input()|. Lines break at LF ("\n"). /// /// Errors ('nomodifiable', `vim.paste()` failure, …) are reflected in `err` /// but do not affect the return value (which is strictly decided by @@ -1219,6 +1219,7 @@ Dictionary nvim_get_namespaces(void) /// the next paste is initiated (phase 1 or -1). /// /// @param data Multiline input. May be binary (containing NUL bytes). +/// @param crlf Also break lines at CR and CRLF. /// @param phase -1: paste in a single call (i.e. without streaming). /// To "stream" a paste, call `nvim_paste` sequentially with /// these `phase` values: @@ -1229,7 +1230,7 @@ Dictionary nvim_get_namespaces(void) /// @return /// - true: Client may continue pasting. /// - false: Client must cancel the paste. -Boolean nvim_paste(String data, Integer phase, Error *err) +Boolean nvim_paste(String data, Boolean crlf, Integer phase, Error *err) FUNC_API_SINCE(6) { static bool draining = false; @@ -1247,7 +1248,7 @@ Boolean nvim_paste(String data, Integer phase, Error *err) // Skip remaining chunks. Report error only once per "stream". goto theend; } - Array lines = string_to_array(data, true); + Array lines = string_to_array(data, crlf); ADD(args, ARRAY_OBJ(lines)); ADD(args, INTEGER_OBJ(phase)); rv = nvim_execute_lua(STATIC_CSTR_AS_STRING("return vim.paste(...)"), args, diff --git a/src/nvim/tui/input.c b/src/nvim/tui/input.c index c74ef58ba1..1f67e6ce13 100644 --- a/src/nvim/tui/input.c +++ b/src/nvim/tui/input.c @@ -108,7 +108,7 @@ static void tinput_wait_enqueue(void **argv) if (input->paste) { Error err = ERROR_INIT; // Paste phase: "continue" (unless handler canceled). - input->paste = !nvim_paste(keys, input->paste, &err) + input->paste = !nvim_paste(keys, true, input->paste, &err) ? 0 : (1 == input->paste ? 2 : input->paste); rbuffer_consumed(input->key_buffer, len); rbuffer_reset(input->key_buffer); diff --git a/test/functional/api/vim_spec.lua b/test/functional/api/vim_spec.lua index cf7e479e15..6f7661dd76 100644 --- a/test/functional/api/vim_spec.lua +++ b/test/functional/api/vim_spec.lua @@ -369,13 +369,13 @@ describe('API', function() describe('nvim_paste', function() it('validates args', function() expect_err('Invalid phase: %-2', request, - 'nvim_paste', 'foo', -2) + 'nvim_paste', 'foo', true, -2) expect_err('Invalid phase: 4', request, - 'nvim_paste', 'foo', 4) + 'nvim_paste', 'foo', true, 4) end) it('non-streaming', function() -- With final "\n". - nvim('paste', 'line 1\nline 2\nline 3\n', -1) + nvim('paste', 'line 1\nline 2\nline 3\n', true, -1) expect([[ line 1 line 2 @@ -385,7 +385,7 @@ describe('API', function() eq(false, nvim('get_option', 'paste')) command('%delete _') -- Without final "\n". - nvim('paste', 'line 1\nline 2\nline 3', -1) + nvim('paste', 'line 1\nline 2\nline 3', true, -1) expect([[ line 1 line 2 @@ -393,7 +393,7 @@ describe('API', function() eq({0,3,6,0}, funcs.getpos('.')) command('%delete _') -- CRLF #10872 - nvim('paste', 'line 1\r\nline 2\r\nline 3\r\n', -1) + nvim('paste', 'line 1\r\nline 2\r\nline 3\r\n', true, -1) expect([[ line 1 line 2 @@ -402,7 +402,7 @@ describe('API', function() eq({0,4,1,0}, funcs.getpos('.')) command('%delete _') -- CRLF without final "\n". - nvim('paste', 'line 1\r\nline 2\r\nline 3\r', -1) + nvim('paste', 'line 1\r\nline 2\r\nline 3\r', true, -1) expect([[ line 1 line 2 @@ -411,7 +411,7 @@ describe('API', function() eq({0,4,1,0}, funcs.getpos('.')) command('%delete _') -- CRLF without final "\r\n". - nvim('paste', 'line 1\r\nline 2\r\nline 3', -1) + nvim('paste', 'line 1\r\nline 2\r\nline 3', true, -1) expect([[ line 1 line 2 @@ -419,15 +419,20 @@ describe('API', function() eq({0,3,6,0}, funcs.getpos('.')) command('%delete _') -- Various other junk. - nvim('paste', 'line 1\r\n\r\rline 2\nline 3\rline 4\r', -1) + nvim('paste', 'line 1\r\n\r\rline 2\nline 3\rline 4\r', true, -1) expect('line 1\n\n\nline 2\nline 3\nline 4\n') eq({0,7,1,0}, funcs.getpos('.')) eq(false, nvim('get_option', 'paste')) end) + it('crlf=false does not break lines at CR, CRLF', function() + nvim('paste', 'line 1\r\n\r\rline 2\nline 3\rline 4\r', false, -1) + expect('line 1\r\n\r\rline 2\nline 3\rline 4\r') + eq({0,3,14,0}, funcs.getpos('.')) + end) it('vim.paste() failure', function() nvim('execute_lua', 'vim.paste = (function(lines, phase) error("fake fail") end)', {}) expect_err([[Error executing lua: %[string "%<nvim>"]:1: fake fail]], - request, 'nvim_paste', 'line 1\nline 2\nline 3', 1) + request, 'nvim_paste', 'line 1\nline 2\nline 3', false, 1) end) end) |