fix: make nvim_cmd not suppress errors inside key mapping

Closes #18632
This commit is contained in:
Famiu Haque 2022-05-19 21:49:12 +06:00
parent 95580f31b3
commit fb8fa004d8
3 changed files with 35 additions and 17 deletions

View File

@ -1304,6 +1304,7 @@ String nvim_cmd(uint64_t channel_id, Dict(cmd) *cmd, Dict(cmd_opts) *opts, Error
capture_ga = &capture_local; capture_ga = &capture_local;
} }
TRY_WRAP({
try_start(); try_start();
if (output) { if (output) {
msg_silent++; msg_silent++;
@ -1317,7 +1318,9 @@ String nvim_cmd(uint64_t channel_id, Dict(cmd) *cmd, Dict(cmd_opts) *opts, Error
capture_ga = save_capture_ga; capture_ga = save_capture_ga;
msg_silent = save_msg_silent; msg_silent = save_msg_silent;
} }
try_end(err); try_end(err);
});
if (ERROR_SET(err)) { if (ERROR_SET(err)) {
goto clear_ga; goto clear_ga;

View File

@ -1583,13 +1583,14 @@ bool parse_cmdline(char *cmdline, exarg_T *eap, CmdParseInfo *cmdinfo, char **er
/// @param cmdinfo Command parse information /// @param cmdinfo Command parse information
void execute_cmd(exarg_T *eap, CmdParseInfo *cmdinfo) void execute_cmd(exarg_T *eap, CmdParseInfo *cmdinfo)
{ {
char *errormsg = NULL;
#define ERROR(msg) \ #define ERROR(msg) \
do { \ do { \
emsg(msg); \ errormsg = msg; \
goto end; \ goto end; \
} while (0) } while (0)
char *errormsg = NULL;
cmdmod_T save_cmdmod = cmdmod; cmdmod_T save_cmdmod = cmdmod;
cmdmod = cmdinfo->cmdmod; cmdmod = cmdinfo->cmdmod;
@ -1648,7 +1649,7 @@ void execute_cmd(exarg_T *eap, CmdParseInfo *cmdinfo)
// If filename expansion is enabled, expand filenames // If filename expansion is enabled, expand filenames
if (cmdinfo->magic.file) { if (cmdinfo->magic.file) {
if (expand_filename(eap, (char_u **)eap->cmdlinep, &errormsg) == FAIL) { if (expand_filename(eap, (char_u **)eap->cmdlinep, &errormsg) == FAIL) {
ERROR(errormsg); goto end;
} }
} }
@ -1706,14 +1707,20 @@ void execute_cmd(exarg_T *eap, CmdParseInfo *cmdinfo)
eap->errmsg = NULL; eap->errmsg = NULL;
(cmdnames[eap->cmdidx].cmd_func)(eap); (cmdnames[eap->cmdidx].cmd_func)(eap);
if (eap->errmsg != NULL) { if (eap->errmsg != NULL) {
ERROR(_(eap->errmsg)); errormsg = _(eap->errmsg);
} }
} }
end: end:
if (errormsg != NULL && *errormsg != NUL) {
emsg(errormsg);
}
// Undo command modifiers // Undo command modifiers
undo_cmdmod(eap, msg_scroll); undo_cmdmod(eap, msg_scroll);
cmdmod = save_cmdmod; cmdmod = save_cmdmod;
if (eap->did_sandbox) {
sandbox--;
}
#undef ERROR #undef ERROR
} }

View File

@ -3636,5 +3636,13 @@ describe('API', function()
meths.cmd({ cmd = "update" }, {}) meths.cmd({ cmd = "update" }, {})
meths.cmd({ cmd = "buffer", count = 0 }, {}) meths.cmd({ cmd = "buffer", count = 0 }, {})
end) end)
it('doesn\'t suppress errors when used in keymapping', function()
meths.exec_lua([[
vim.keymap.set("n", "[l",
function() vim.api.nvim_cmd({ cmd = "echo", args = {"foo"} }, {}) end)
]], {})
feed("[l")
neq(nil, string.find(eval("v:errmsg"), "E5108:"))
end)
end) end)
end) end)