Skip to content

Commit

Permalink
fix(inccommand): never preview if parsing command failed
Browse files Browse the repository at this point in the history
`errormsg` is not always set when parsing the command failed (e.g. when
the range contains invalid marks). Check the return value is better.
  • Loading branch information
zeertzjq committed Jun 13, 2022
1 parent e420cd6 commit 608cd2f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/nvim/ex_getln.c
Original file line number Diff line number Diff line change
Expand Up @@ -2375,15 +2375,16 @@ static void cmdpreview_close_win(void)
/// 6. Revert all changes made by the preview callback.
static void cmdpreview_show(CommandLineState *s)
{
emsg_off++; // Block error reporting as parsing the command may produce error messages

// Parse the command line and return if it fails.
exarg_T ea;
CmdParseInfo cmdinfo;
// Copy the command line so we can modify it.
char *cmdline = xstrdup((char *)ccline.cmdbuff);
char *errormsg = NULL;

parse_cmdline(cmdline, &ea, &cmdinfo, &errormsg);
if (errormsg != NULL) {
if (!parse_cmdline(cmdline, &ea, &cmdinfo, &errormsg)) {
goto end;
}

Expand All @@ -2409,7 +2410,6 @@ static void cmdpreview_show(CommandLineState *s)
cmdmod_T save_cmdmod = cmdmod;

cmdpreview = true;
emsg_silent++; // Block error reporting as the command may be incomplete
msg_silent++; // Block messages, namely ones that prompt
block_autocmds(); // Block events
garray_T save_view;
Expand Down Expand Up @@ -2496,7 +2496,6 @@ static void cmdpreview_show(CommandLineState *s)
ga_clear(&save_view);
unblock_autocmds(); // Unblock events
msg_silent--; // Unblock messages
emsg_silent--; // Unblock error reporting

// Restore the window "view".
curwin->w_cursor = s->is_state.save_cursor;
Expand All @@ -2512,6 +2511,7 @@ static void cmdpreview_show(CommandLineState *s)
}
end:
xfree(cmdline);
emsg_off--; // Unblock error reporting
}

static int command_line_changed(CommandLineState *s)
Expand Down
26 changes: 26 additions & 0 deletions test/functional/ui/inccommand_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2947,6 +2947,32 @@ it(':substitute with inccommand, allows :redraw before first separator is typed
]])
end)

it(':substitute with inccommand, does nothing if range contains invalid marks', function()
local screen = Screen.new(30, 6)
clear()
command('set undolevels=-1')
common_setup(screen, 'split', 'test')
feed([[:'a,'bs]])
screen:expect([[
test |
{15:~ }|
{15:~ }|
{15:~ }|
{15:~ }|
:'a,'bs^ |
]])
feed('/')
screen:expect([[
test |
{15:~ }|
{15:~ }|
{15:~ }|
{15:~ }|
:'a,'bs/^ |
]])
eq('', eval('v:errmsg'))
end)

it(":substitute doesn't crash with inccommand, if undo is empty #12932", function()
local screen = Screen.new(10,5)
clear()
Expand Down

0 comments on commit 608cd2f

Please sign in to comment.