Merge pull request #19203 from zeertzjq/api-cmd-unsilent

feat(api): add `unsilent` to command APIs
This commit is contained in:
zeertzjq
2022-07-03 05:00:02 +08:00
committed by GitHub
8 changed files with 93 additions and 63 deletions

View File

@@ -1951,6 +1951,7 @@ nvim_parse_cmd({str}, {opts}) *nvim_parse_cmd()*
• silent: (boolean) |:silent|. • silent: (boolean) |:silent|.
• emsg_silent: (boolean) |:silent!|. • emsg_silent: (boolean) |:silent!|.
• unsilent: (boolean) |:unsilent|.
• sandbox: (boolean) |:sandbox|. • sandbox: (boolean) |:sandbox|.
• noautocmd: (boolean) |:noautocmd|. • noautocmd: (boolean) |:noautocmd|.
• browse: (boolean) |:browse|. • browse: (boolean) |:browse|.

View File

@@ -54,6 +54,7 @@
/// - force: (boolean) Whether filter is inverted or not. /// - force: (boolean) Whether filter is inverted or not.
/// - silent: (boolean) |:silent|. /// - silent: (boolean) |:silent|.
/// - emsg_silent: (boolean) |:silent!|. /// - emsg_silent: (boolean) |:silent!|.
/// - unsilent: (boolean) |:unsilent|.
/// - sandbox: (boolean) |:sandbox|. /// - sandbox: (boolean) |:sandbox|.
/// - noautocmd: (boolean) |:noautocmd|. /// - noautocmd: (boolean) |:noautocmd|.
/// - browse: (boolean) |:browse|. /// - browse: (boolean) |:browse|.
@@ -232,6 +233,7 @@ Dictionary nvim_parse_cmd(String str, Dictionary opts, Error *err)
PUT(mods, "silent", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_SILENT)); PUT(mods, "silent", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_SILENT));
PUT(mods, "emsg_silent", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_ERRSILENT)); PUT(mods, "emsg_silent", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_ERRSILENT));
PUT(mods, "unsilent", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_UNSILENT));
PUT(mods, "sandbox", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_SANDBOX)); PUT(mods, "sandbox", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_SANDBOX));
PUT(mods, "noautocmd", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_NOAUTOCMD)); PUT(mods, "noautocmd", BOOLEAN_OBJ(cmdinfo.cmdmod.cmod_flags & CMOD_NOAUTOCMD));
PUT(mods, "tab", INTEGER_OBJ(cmdinfo.cmdmod.cmod_tab)); PUT(mods, "tab", INTEGER_OBJ(cmdinfo.cmdmod.cmod_tab));
@@ -598,6 +600,7 @@ String nvim_cmd(uint64_t channel_id, Dict(cmd) *cmd, Dict(cmd_opts) *opts, Error
OBJ_TO_CMOD_FLAG(CMOD_SILENT, mods.silent, false, "'mods.silent'"); OBJ_TO_CMOD_FLAG(CMOD_SILENT, mods.silent, false, "'mods.silent'");
OBJ_TO_CMOD_FLAG(CMOD_ERRSILENT, mods.emsg_silent, false, "'mods.emsg_silent'"); OBJ_TO_CMOD_FLAG(CMOD_ERRSILENT, mods.emsg_silent, false, "'mods.emsg_silent'");
OBJ_TO_CMOD_FLAG(CMOD_UNSILENT, mods.silent, false, "'mods.unsilent'");
OBJ_TO_CMOD_FLAG(CMOD_SANDBOX, mods.sandbox, false, "'mods.sandbox'"); OBJ_TO_CMOD_FLAG(CMOD_SANDBOX, mods.sandbox, false, "'mods.sandbox'");
OBJ_TO_CMOD_FLAG(CMOD_NOAUTOCMD, mods.noautocmd, false, "'mods.noautocmd'"); OBJ_TO_CMOD_FLAG(CMOD_NOAUTOCMD, mods.noautocmd, false, "'mods.noautocmd'");
OBJ_TO_CMOD_FLAG(CMOD_BROWSE, mods.browse, false, "'mods.browse'"); OBJ_TO_CMOD_FLAG(CMOD_BROWSE, mods.browse, false, "'mods.browse'");
@@ -722,6 +725,10 @@ static void build_cmdline_str(char **cmdlinep, exarg_T *eap, CmdParseInfo *cmdin
kv_concat(cmdline, "silent "); kv_concat(cmdline, "silent ");
} }
if (cmdinfo->cmdmod.cmod_flags & CMOD_UNSILENT) {
kv_concat(cmdline, "unsilent ");
}
switch (cmdinfo->cmdmod.cmod_split & (WSP_ABOVE | WSP_BELOW | WSP_TOP | WSP_BOT)) { switch (cmdinfo->cmdmod.cmod_split & (WSP_ABOVE | WSP_BELOW | WSP_TOP | WSP_BOT)) {
case WSP_ABOVE: case WSP_ABOVE:
kv_concat(cmdline, "aboveleft "); kv_concat(cmdline, "aboveleft ");

View File

@@ -181,6 +181,7 @@ return {
cmd_mods = { cmd_mods = {
"silent"; "silent";
"emsg_silent"; "emsg_silent";
"unsilent";
"filter"; "filter";
"sandbox"; "sandbox";
"noautocmd"; "noautocmd";

View File

@@ -1640,7 +1640,7 @@ void ex_options(exarg_T *eap)
bool multi_mods = 0; bool multi_mods = 0;
buf[0] = NUL; buf[0] = NUL;
(void)add_win_cmd_modifers(buf, &multi_mods); (void)add_win_cmd_modifers(buf, &cmdmod, &multi_mods);
os_setenv("OPTWIN_CMD", buf, 1); os_setenv("OPTWIN_CMD", buf, 1);
cmd_source(SYS_OPTWIN_FILE, NULL); cmd_source(SYS_OPTWIN_FILE, NULL);

View File

@@ -6455,20 +6455,7 @@ static size_t uc_check_code(char *code, size_t len, char *buf, ucmd_T *cmd, exar
} }
case ct_MODS: case ct_MODS:
result = quote ? 2 : 0; result = uc_mods(buf, &cmdmod, quote);
if (buf != NULL) {
if (quote) {
*buf++ = '"';
}
*buf = '\0';
}
result += uc_mods(buf);
if (quote && buf != NULL) {
buf += result - 2;
*buf = '"';
}
break; break;
case ct_REGISTER: case ct_REGISTER:
@@ -6508,43 +6495,45 @@ static size_t uc_check_code(char *code, size_t len, char *buf, ucmd_T *cmd, exar
return result; return result;
} }
/// Add modifiers from "cmdmod.cmod_split" to "buf". Set "multi_mods" when one /// Add modifiers from "cmod->cmod_split" to "buf". Set "multi_mods" when one
/// was added. /// was added.
/// ///
/// @return the number of bytes added /// @return the number of bytes added
size_t add_win_cmd_modifers(char *buf, bool *multi_mods) size_t add_win_cmd_modifers(char *buf, const cmdmod_T *cmod, bool *multi_mods)
{ {
size_t result = 0; size_t result = 0;
// :aboveleft and :leftabove // :aboveleft and :leftabove
if (cmdmod.cmod_split & WSP_ABOVE) { if (cmod->cmod_split & WSP_ABOVE) {
result += add_cmd_modifier(buf, "aboveleft", multi_mods); result += add_cmd_modifier(buf, "aboveleft", multi_mods);
} }
// :belowright and :rightbelow // :belowright and :rightbelow
if (cmdmod.cmod_split & WSP_BELOW) { if (cmod->cmod_split & WSP_BELOW) {
result += add_cmd_modifier(buf, "belowright", multi_mods); result += add_cmd_modifier(buf, "belowright", multi_mods);
} }
// :botright // :botright
if (cmdmod.cmod_split & WSP_BOT) { if (cmod->cmod_split & WSP_BOT) {
result += add_cmd_modifier(buf, "botright", multi_mods); result += add_cmd_modifier(buf, "botright", multi_mods);
} }
// :tab // :tab
if (cmdmod.cmod_tab > 0) { if (cmod->cmod_tab > 0) {
result += add_cmd_modifier(buf, "tab", multi_mods); result += add_cmd_modifier(buf, "tab", multi_mods);
} }
// :topleft // :topleft
if (cmdmod.cmod_split & WSP_TOP) { if (cmod->cmod_split & WSP_TOP) {
result += add_cmd_modifier(buf, "topleft", multi_mods); result += add_cmd_modifier(buf, "topleft", multi_mods);
} }
// :vertical // :vertical
if (cmdmod.cmod_split & WSP_VERT) { if (cmod->cmod_split & WSP_VERT) {
result += add_cmd_modifier(buf, "vertical", multi_mods); result += add_cmd_modifier(buf, "vertical", multi_mods);
} }
return result; return result;
} }
size_t uc_mods(char *buf) /// Generate text for the "cmod" command modifiers.
/// If "buf" is NULL just return the length.
size_t uc_mods(char *buf, const cmdmod_T *cmod, bool quote)
{ {
size_t result = 0; size_t result = 0;
bool multi_mods = false; bool multi_mods = false;
@@ -6562,8 +6551,20 @@ size_t uc_mods(char *buf)
{ CMOD_KEEPMARKS, "keepmarks" }, { CMOD_KEEPMARKS, "keepmarks" },
{ CMOD_KEEPPATTERNS, "keeppatterns" }, { CMOD_KEEPPATTERNS, "keeppatterns" },
{ CMOD_LOCKMARKS, "lockmarks" }, { CMOD_LOCKMARKS, "lockmarks" },
{ CMOD_NOSWAPFILE, "noswapfile" } { CMOD_NOSWAPFILE, "noswapfile" },
{ CMOD_UNSILENT, "unsilent" },
{ CMOD_NOAUTOCMD, "noautocmd" },
{ CMOD_SANDBOX, "sandbox" },
}; };
result = quote ? 2 : 0;
if (buf != NULL) {
if (quote) {
*buf++ = '"';
}
*buf = '\0';
}
// the modifiers that are simple flags // the modifiers that are simple flags
for (size_t i = 0; i < ARRAY_SIZE(mod_entries); i++) { for (size_t i = 0; i < ARRAY_SIZE(mod_entries); i++) {
if (cmdmod.cmod_flags & mod_entries[i].flag) { if (cmdmod.cmod_flags & mod_entries[i].flag) {
@@ -6571,23 +6572,23 @@ size_t uc_mods(char *buf)
} }
} }
// TODO(vim): How to support :noautocmd?
// TODO(vim): How to support :sandbox?
// :silent // :silent
if (msg_silent > 0) { if (msg_silent > 0) {
result += add_cmd_modifier(buf, emsg_silent > 0 ? "silent!" : "silent", &multi_mods); result += add_cmd_modifier(buf,
(cmod->cmod_flags & CMOD_ERRSILENT) ? "silent!" : "silent",
&multi_mods);
} }
// TODO(vim): How to support :unsilent?
// :verbose // :verbose
if (p_verbose > 0) { if (p_verbose > 0) {
result += add_cmd_modifier(buf, "verbose", &multi_mods); result += add_cmd_modifier(buf, "verbose", &multi_mods);
} }
// flags from cmdmod.cmod_split // flags from cmod->cmod_split
result += add_win_cmd_modifers(buf, &multi_mods); result += add_win_cmd_modifers(buf, cmod, &multi_mods);
if (quote && buf != NULL) {
buf += result - 2;
*buf = '"';
}
return result; return result;
} }

View File

@@ -1915,7 +1915,7 @@ int nlua_do_ucmd(ucmd_T *cmd, exarg_T *eap, bool preview)
// every possible modifier (with room to spare). If the list of possible // every possible modifier (with room to spare). If the list of possible
// modifiers grows this may need to be updated. // modifiers grows this may need to be updated.
char buf[200] = { 0 }; char buf[200] = { 0 };
(void)uc_mods(buf); (void)uc_mods(buf, &cmdmod, false);
lua_pushstring(lstate, buf); lua_pushstring(lstate, buf);
lua_setfield(lstate, -2, "mods"); lua_setfield(lstate, -2, "mods");
@@ -1946,6 +1946,8 @@ int nlua_do_ucmd(ucmd_T *cmd, exarg_T *eap, bool preview)
lua_setfield(lstate, -2, "silent"); lua_setfield(lstate, -2, "silent");
lua_pushboolean(lstate, cmdmod.cmod_flags & CMOD_ERRSILENT); lua_pushboolean(lstate, cmdmod.cmod_flags & CMOD_ERRSILENT);
lua_setfield(lstate, -2, "emsg_silent"); lua_setfield(lstate, -2, "emsg_silent");
lua_pushboolean(lstate, cmdmod.cmod_flags & CMOD_UNSILENT);
lua_setfield(lstate, -2, "unsilent");
lua_pushboolean(lstate, cmdmod.cmod_flags & CMOD_SANDBOX); lua_pushboolean(lstate, cmdmod.cmod_flags & CMOD_SANDBOX);
lua_setfield(lstate, -2, "sandbox"); lua_setfield(lstate, -2, "sandbox");
lua_pushboolean(lstate, cmdmod.cmod_flags & CMOD_NOAUTOCMD); lua_pushboolean(lstate, cmdmod.cmod_flags & CMOD_NOAUTOCMD);

View File

@@ -136,6 +136,7 @@ describe('nvim_create_user_command', function()
silent = false, silent = false,
split = "", split = "",
tab = 0, tab = 0,
unsilent = false,
verbose = -1, verbose = -1,
vertical = false, vertical = false,
}, },
@@ -170,6 +171,7 @@ describe('nvim_create_user_command', function()
silent = false, silent = false,
split = "", split = "",
tab = 0, tab = 0,
unsilent = false,
verbose = -1, verbose = -1,
vertical = false, vertical = false,
}, },
@@ -204,6 +206,7 @@ describe('nvim_create_user_command', function()
silent = false, silent = false,
split = "", split = "",
tab = 0, tab = 0,
unsilent = false,
verbose = -1, verbose = -1,
vertical = false, vertical = false,
}, },
@@ -221,10 +224,10 @@ describe('nvim_create_user_command', function()
bang = true, bang = true,
line1 = 10, line1 = 10,
line2 = 10, line2 = 10,
mods = "botright", mods = "confirm unsilent botright",
smods = { smods = {
browse = false, browse = false,
confirm = false, confirm = true,
emsg_silent = false, emsg_silent = false,
hide = false, hide = false,
keepalt = false, keepalt = false,
@@ -238,6 +241,7 @@ describe('nvim_create_user_command', function()
silent = false, silent = false,
split = "botright", split = "botright",
tab = 0, tab = 0,
unsilent = true,
verbose = -1, verbose = -1,
vertical = false, vertical = false,
}, },
@@ -245,7 +249,7 @@ describe('nvim_create_user_command', function()
count = 10, count = 10,
reg = "", reg = "",
}, exec_lua [=[ }, exec_lua [=[
vim.api.nvim_command('botright 10CommandWithLuaCallback! h\tey ') vim.api.nvim_command('unsilent botright confirm 10CommandWithLuaCallback! h\tey ')
return result return result
]=]) ]=])
@@ -272,6 +276,7 @@ describe('nvim_create_user_command', function()
silent = false, silent = false,
split = "", split = "",
tab = 0, tab = 0,
unsilent = false,
verbose = -1, verbose = -1,
vertical = false, vertical = false,
}, },
@@ -306,6 +311,7 @@ describe('nvim_create_user_command', function()
silent = false, silent = false,
split = "", split = "",
tab = 0, tab = 0,
unsilent = false,
verbose = -1, verbose = -1,
vertical = false, vertical = false,
}, },
@@ -352,6 +358,7 @@ describe('nvim_create_user_command', function()
silent = false, silent = false,
split = "", split = "",
tab = 0, tab = 0,
unsilent = false,
verbose = -1, verbose = -1,
vertical = false, vertical = false,
}, },

View File

@@ -3181,10 +3181,11 @@ describe('API', function()
noswapfile = false, noswapfile = false,
sandbox = false, sandbox = false,
silent = false, silent = false,
vertical = false,
split = "", split = "",
tab = 0, tab = 0,
verbose = -1 unsilent = false,
verbose = -1,
vertical = false,
} }
}, meths.parse_cmd('echo foo', {})) }, meths.parse_cmd('echo foo', {}))
end) end)
@@ -3221,10 +3222,11 @@ describe('API', function()
noswapfile = false, noswapfile = false,
sandbox = false, sandbox = false,
silent = false, silent = false,
vertical = false,
split = "", split = "",
tab = 0, tab = 0,
verbose = -1 unsilent = false,
verbose = -1,
vertical = false,
} }
}, meths.parse_cmd('4,6s/math.random/math.max/', {})) }, meths.parse_cmd('4,6s/math.random/math.max/', {}))
end) end)
@@ -3261,10 +3263,11 @@ describe('API', function()
noswapfile = false, noswapfile = false,
sandbox = false, sandbox = false,
silent = false, silent = false,
vertical = false,
split = "", split = "",
tab = 0, tab = 0,
verbose = -1 unsilent = false,
verbose = -1,
vertical = false,
} }
}, meths.parse_cmd('buffer 1', {})) }, meths.parse_cmd('buffer 1', {}))
end) end)
@@ -3301,10 +3304,11 @@ describe('API', function()
noswapfile = false, noswapfile = false,
sandbox = false, sandbox = false,
silent = false, silent = false,
vertical = false,
split = "", split = "",
tab = 0, tab = 0,
verbose = -1 unsilent = false,
verbose = -1,
vertical = false,
} }
}, meths.parse_cmd('put +', {})) }, meths.parse_cmd('put +', {}))
end) end)
@@ -3341,10 +3345,11 @@ describe('API', function()
noswapfile = false, noswapfile = false,
sandbox = false, sandbox = false,
silent = false, silent = false,
vertical = false,
split = "", split = "",
tab = 0, tab = 0,
verbose = -1 unsilent = false,
verbose = -1,
vertical = false,
} }
}, meths.parse_cmd('1,3delete * 5', {})) }, meths.parse_cmd('1,3delete * 5', {}))
end) end)
@@ -3381,10 +3386,11 @@ describe('API', function()
noswapfile = false, noswapfile = false,
sandbox = false, sandbox = false,
silent = false, silent = false,
vertical = false,
split = "", split = "",
tab = 0, tab = 0,
verbose = -1 unsilent = false,
verbose = -1,
vertical = false,
}, },
}, meths.parse_cmd('w!', {})) }, meths.parse_cmd('w!', {}))
end) end)
@@ -3421,10 +3427,11 @@ describe('API', function()
noswapfile = false, noswapfile = false,
sandbox = false, sandbox = false,
silent = true, silent = true,
vertical = false,
split = "topleft", split = "topleft",
tab = 2, tab = 2,
verbose = 15 unsilent = false,
verbose = 15,
vertical = false,
}, },
}, meths.parse_cmd('15verbose silent! aboveleft topleft tab filter /foo/ split foo.txt', {})) }, meths.parse_cmd('15verbose silent! aboveleft topleft tab filter /foo/ split foo.txt', {}))
eq({ eq({
@@ -3443,7 +3450,7 @@ describe('API', function()
nextcmd = '', nextcmd = '',
mods = { mods = {
browse = false, browse = false,
confirm = false, confirm = true,
emsg_silent = false, emsg_silent = false,
filter = { filter = {
pattern = "foo", pattern = "foo",
@@ -3459,12 +3466,13 @@ describe('API', function()
noswapfile = false, noswapfile = false,
sandbox = false, sandbox = false,
silent = false, silent = false,
vertical = false, split = "botright",
split = "",
tab = 0, tab = 0,
verbose = -1 unsilent = true,
verbose = 0,
vertical = false,
}, },
}, meths.parse_cmd('filter! /foo/ split foo.txt', {})) }, meths.parse_cmd('0verbose unsilent botright confirm filter! /foo/ split foo.txt', {}))
end) end)
it('works with user commands', function() it('works with user commands', function()
command('command -bang -nargs=+ -range -addr=lines MyCommand echo foo') command('command -bang -nargs=+ -range -addr=lines MyCommand echo foo')
@@ -3500,10 +3508,11 @@ describe('API', function()
noswapfile = false, noswapfile = false,
sandbox = false, sandbox = false,
silent = false, silent = false,
vertical = false,
split = "", split = "",
tab = 0, tab = 0,
verbose = -1 unsilent = false,
verbose = -1,
vertical = false,
} }
}, meths.parse_cmd('4,6MyCommand! test it', {})) }, meths.parse_cmd('4,6MyCommand! test it', {}))
end) end)
@@ -3540,10 +3549,11 @@ describe('API', function()
noswapfile = false, noswapfile = false,
sandbox = false, sandbox = false,
silent = false, silent = false,
vertical = false,
split = "", split = "",
tab = 0, tab = 0,
verbose = -1 unsilent = false,
verbose = -1,
vertical = false,
} }
}, meths.parse_cmd('argadd a.txt | argadd b.txt', {})) }, meths.parse_cmd('argadd a.txt | argadd b.txt', {}))
end) end)
@@ -3581,10 +3591,11 @@ describe('API', function()
noswapfile = false, noswapfile = false,
sandbox = false, sandbox = false,
silent = false, silent = false,
vertical = false,
split = "", split = "",
tab = 0, tab = 0,
verbose = -1 unsilent = false,
verbose = -1,
vertical = false,
} }
}, meths.parse_cmd('MyCommand test it', {})) }, meths.parse_cmd('MyCommand test it', {}))
end) end)