From 308ccb6f5e40ba1dbe4abfebc9df3399d7f17504 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Sat, 18 Feb 2017 02:39:07 +0100 Subject: [PATCH 1/2] cmdline: CTRL-R: instead of CR between lines. ^M isn't any more "correct" than space: the "technically correct" interpretation is to execute the first line that is seen (and this is what happens on middle-click paste in Vim). ^M is only intended to defuse the newline, so that the user can review the command. We can do that with a space instead, and then the command can be executed without having to fix it up first. --- runtime/doc/vim_diff.txt | 3 ++ src/nvim/ex_getln.c | 6 ++-- src/nvim/ops.c | 13 ++++--- test/functional/cmdline/ctrl_r_spec.lua | 34 +++++++++++++++++++ .../{ex_getln => cmdline}/history_spec.lua | 0 5 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 test/functional/cmdline/ctrl_r_spec.lua rename test/functional/{ex_getln => cmdline}/history_spec.lua (100%) diff --git a/runtime/doc/vim_diff.txt b/runtime/doc/vim_diff.txt index eeb5e85036..7fbd957a22 100644 --- a/runtime/doc/vim_diff.txt +++ b/runtime/doc/vim_diff.txt @@ -238,6 +238,9 @@ newly allocated memory all over the place) and fail on types which cannot be coerced to strings. See |id()| for more details, currently it uses `printf("%p", {expr})` internally. +|c_CTRL-R| pasting a non-special register into the |cmdline| separates lines +by instead of . + ============================================================================== 5. Missing legacy features *nvim-features-missing* *if_lua* *if_perl* *if_mzscheme* *if_tcl* diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index d99c8d02f7..2a4fc067db 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -2471,10 +2471,10 @@ void restore_cmdline_alloc(char_u *p) /// /// @param regname Register name. /// @param literally Insert text literally instead of "as typed". -/// @param remcr When true, remove trailing CR. +/// @param remspc When true, remove trailing . /// /// @returns FAIL for failure, OK otherwise -static bool cmdline_paste(int regname, bool literally, bool remcr) +static bool cmdline_paste(int regname, bool literally, bool remspc) { long i; char_u *arg; @@ -2539,7 +2539,7 @@ static bool cmdline_paste(int regname, bool literally, bool remcr) return OK; } - return cmdline_paste_reg(regname, literally, remcr); + return cmdline_paste_reg(regname, literally, remspc); } /* diff --git a/src/nvim/ops.c b/src/nvim/ops.c index d58c8700ca..2f45795812 100644 --- a/src/nvim/ops.c +++ b/src/nvim/ops.c @@ -1260,16 +1260,16 @@ int get_spec_reg( /// Paste a yank register into the command line. /// Only for non-special registers. -/// Used by CTRL-R command in command-line mode +/// Used by CTRL-R in command-line mode. /// insert_reg() can't be used here, because special characters from the /// register contents will be interpreted as commands. /// /// @param regname Register name. /// @param literally Insert text literally instead of "as typed". -/// @param remcr When true, don't add CR characters. +/// @param remspc When true, don't add characters. /// /// @returns FAIL for failure, OK otherwise -bool cmdline_paste_reg(int regname, bool literally, bool remcr) +bool cmdline_paste_reg(int regname, bool literally, bool remspc) { yankreg_T *reg = get_yank_register(regname, YREG_PASTE); if (reg->y_array == NULL) @@ -1278,10 +1278,9 @@ bool cmdline_paste_reg(int regname, bool literally, bool remcr) for (size_t i = 0; i < reg->y_size; i++) { cmdline_paste_str(reg->y_array[i], literally); - // Insert ^M between lines and after last line if type is kMTLineWise. - // Don't do this when "remcr" is true. - if ((reg->y_type == kMTLineWise || i < reg->y_size - 1) && !remcr) { - cmdline_paste_str((char_u *)"\r", literally); + // Insert space between lines, unless `remspc` is true. + if (i < reg->y_size - 1 && !remspc) { + cmdline_paste_str((char_u *)" ", literally); } /* Check for CTRL-C, in case someone tries to paste a few thousand diff --git a/test/functional/cmdline/ctrl_r_spec.lua b/test/functional/cmdline/ctrl_r_spec.lua new file mode 100644 index 0000000000..1bb3174737 --- /dev/null +++ b/test/functional/cmdline/ctrl_r_spec.lua @@ -0,0 +1,34 @@ +local helpers = require('test.functional.helpers')(after_each) +local clear, insert, funcs, eq, feed = + helpers.clear, helpers.insert, helpers.funcs, helpers.eq, helpers.feed + +describe('cmdline CTRL-R', function() + before_each(clear) + + it('pasting non-special register inserts between lines', function() + insert([[ + line1abc + line2somemoretext + ]]) + -- Yank 2 lines linewise, then paste to cmdline. + feed([[gg0yj:0]]) + -- inserted *between* lines, not after the final line. + eq('line1abc line2somemoretext', funcs.getcmdline()) + + -- Yank 2 lines characterwise, then paste to cmdline. + feed([[gg05lyvj:0]]) + -- inserted *between* lines, not after the final line. + eq('abc line2', funcs.getcmdline()) + + -- Yank 1 line linewise, then paste to cmdline. + feed([[ggyy:0]]) + -- No spaces inserted. + eq('line1abc', funcs.getcmdline()) + end) + + it('pasting special register inserts , ', function() + feed([[:="foo\nbar\rbaz"]]) + eq('foo\nbar\rbaz', funcs.getcmdline()) + end) +end) + diff --git a/test/functional/ex_getln/history_spec.lua b/test/functional/cmdline/history_spec.lua similarity index 100% rename from test/functional/ex_getln/history_spec.lua rename to test/functional/cmdline/history_spec.lua From baab49ee89a927f63bfefdb432155a1037afa93a Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Sat, 18 Feb 2017 23:15:27 +0100 Subject: [PATCH 2/2] cmdline: CTRL-R: Omit trailing . The "technically correct" interpretation is to execute the first line that is seen (and this is what happens on middle-click paste in Vim). ^M is only intended to "defuse" the newline, so the user can review it. The parent commit changed the behavior to insert between lines, but that's a higher-risk change: it is arguably possible that some user *wants* the literal ^M chars when e.g. assigning to a register: :let @a='b' To avoid that risk, keep the old behavior and only omit the last ^M. This makes `yy:0` nicer at no cost. --- runtime/doc/vim_diff.txt | 3 +-- src/nvim/ex_getln.c | 6 +++--- src/nvim/ops.c | 10 +++++----- test/functional/cmdline/ctrl_r_spec.lua | 12 ++++++------ 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/runtime/doc/vim_diff.txt b/runtime/doc/vim_diff.txt index 7fbd957a22..cfe6308663 100644 --- a/runtime/doc/vim_diff.txt +++ b/runtime/doc/vim_diff.txt @@ -238,8 +238,7 @@ newly allocated memory all over the place) and fail on types which cannot be coerced to strings. See |id()| for more details, currently it uses `printf("%p", {expr})` internally. -|c_CTRL-R| pasting a non-special register into the |cmdline| separates lines -by instead of . +|c_CTRL-R| pasting a non-special register into |cmdline| omits the last . ============================================================================== 5. Missing legacy features *nvim-features-missing* diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index 2a4fc067db..d99c8d02f7 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -2471,10 +2471,10 @@ void restore_cmdline_alloc(char_u *p) /// /// @param regname Register name. /// @param literally Insert text literally instead of "as typed". -/// @param remspc When true, remove trailing . +/// @param remcr When true, remove trailing CR. /// /// @returns FAIL for failure, OK otherwise -static bool cmdline_paste(int regname, bool literally, bool remspc) +static bool cmdline_paste(int regname, bool literally, bool remcr) { long i; char_u *arg; @@ -2539,7 +2539,7 @@ static bool cmdline_paste(int regname, bool literally, bool remspc) return OK; } - return cmdline_paste_reg(regname, literally, remspc); + return cmdline_paste_reg(regname, literally, remcr); } /* diff --git a/src/nvim/ops.c b/src/nvim/ops.c index 2f45795812..8bfda3c193 100644 --- a/src/nvim/ops.c +++ b/src/nvim/ops.c @@ -1266,10 +1266,10 @@ int get_spec_reg( /// /// @param regname Register name. /// @param literally Insert text literally instead of "as typed". -/// @param remspc When true, don't add characters. +/// @param remcr When true, don't add CR characters. /// /// @returns FAIL for failure, OK otherwise -bool cmdline_paste_reg(int regname, bool literally, bool remspc) +bool cmdline_paste_reg(int regname, bool literally, bool remcr) { yankreg_T *reg = get_yank_register(regname, YREG_PASTE); if (reg->y_array == NULL) @@ -1278,9 +1278,9 @@ bool cmdline_paste_reg(int regname, bool literally, bool remspc) for (size_t i = 0; i < reg->y_size; i++) { cmdline_paste_str(reg->y_array[i], literally); - // Insert space between lines, unless `remspc` is true. - if (i < reg->y_size - 1 && !remspc) { - cmdline_paste_str((char_u *)" ", literally); + // Insert ^M between lines, unless `remcr` is true. + if (i < reg->y_size - 1 && !remcr) { + cmdline_paste_str((char_u *)"\r", literally); } /* Check for CTRL-C, in case someone tries to paste a few thousand diff --git a/test/functional/cmdline/ctrl_r_spec.lua b/test/functional/cmdline/ctrl_r_spec.lua index 1bb3174737..d2dad23e98 100644 --- a/test/functional/cmdline/ctrl_r_spec.lua +++ b/test/functional/cmdline/ctrl_r_spec.lua @@ -5,24 +5,24 @@ local clear, insert, funcs, eq, feed = describe('cmdline CTRL-R', function() before_each(clear) - it('pasting non-special register inserts between lines', function() + it('pasting non-special register inserts *between* lines', function() insert([[ line1abc line2somemoretext ]]) -- Yank 2 lines linewise, then paste to cmdline. feed([[gg0yj:0]]) - -- inserted *between* lines, not after the final line. - eq('line1abc line2somemoretext', funcs.getcmdline()) + -- inserted between lines, NOT after the final line. + eq('line1abc\rline2somemoretext', funcs.getcmdline()) -- Yank 2 lines characterwise, then paste to cmdline. feed([[gg05lyvj:0]]) - -- inserted *between* lines, not after the final line. - eq('abc line2', funcs.getcmdline()) + -- inserted between lines, NOT after the final line. + eq('abc\rline2', funcs.getcmdline()) -- Yank 1 line linewise, then paste to cmdline. feed([[ggyy:0]]) - -- No spaces inserted. + -- No inserted. eq('line1abc', funcs.getcmdline()) end)