feat: add preview functionality to user commands

Adds a Lua-only `preview` flag to user commands which allows the command to be incrementally previewed like `:substitute` when 'inccommand' is set.
This commit is contained in:
Famiu Haque
2022-04-20 17:02:18 +06:00
parent e9803e1de6
commit 46536f53e8
25 changed files with 960 additions and 346 deletions

View File

@@ -1840,11 +1840,12 @@ cleanup:
xfree(info);
}
void nlua_do_ucmd(ucmd_T *cmd, exarg_T *eap)
/// @param preview Invoke the callback as a |:command-preview| handler.
int nlua_do_ucmd(ucmd_T *cmd, exarg_T *eap, bool preview)
{
lua_State *const lstate = global_lstate;
nlua_pushref(lstate, cmd->uc_luaref);
nlua_pushref(lstate, preview ? cmd->uc_preview_luaref : cmd->uc_luaref);
lua_newtable(lstate);
lua_pushboolean(lstate, eap->forceit == 1);
@@ -1969,7 +1970,31 @@ void nlua_do_ucmd(ucmd_T *cmd, exarg_T *eap)
lua_setfield(lstate, -2, "smods");
if (nlua_pcall(lstate, 1, 0)) {
nlua_error(lstate, _("Error executing Lua callback: %.*s"));
if (preview) {
lua_pushinteger(lstate, cmdpreview_get_ns());
handle_T cmdpreview_bufnr = cmdpreview_get_bufnr();
if (cmdpreview_bufnr != 0) {
lua_pushinteger(lstate, cmdpreview_bufnr);
} else {
lua_pushnil(lstate);
}
}
if (nlua_pcall(lstate, preview ? 3 : 1, preview ? 1 : 0)) {
nlua_error(lstate, _("Error executing Lua callback: %.*s"));
return 0;
}
int retv = 0;
if (preview) {
if (lua_isnumber(lstate, -1) && (retv = (int)lua_tointeger(lstate, -1)) >= 0 && retv <= 2) {
lua_pop(lstate, 1);
} else {
retv = 0;
}
}
return retv;
}