aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/lua/executor.c
diff options
context:
space:
mode:
authorFamiu Haque <famiuhaque@protonmail.com>2022-04-20 17:02:18 +0600
committerFamiu Haque <famiuhaque@protonmail.com>2022-05-31 20:55:05 +0600
commit46536f53e82967dcac8d030ee3394cdb156f9603 (patch)
tree4444067831639a6a1eb1916ca9cd5002261932ec /src/nvim/lua/executor.c
parente9803e1de6497ee21f77f45cf2670c2fe4e8ab22 (diff)
downloadrneovim-46536f53e82967dcac8d030ee3394cdb156f9603.tar.gz
rneovim-46536f53e82967dcac8d030ee3394cdb156f9603.tar.bz2
rneovim-46536f53e82967dcac8d030ee3394cdb156f9603.zip
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.
Diffstat (limited to 'src/nvim/lua/executor.c')
-rw-r--r--src/nvim/lua/executor.c31
1 files changed, 28 insertions, 3 deletions
diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c
index a826dd07d3..deff2347e0 100644
--- a/src/nvim/lua/executor.c
+++ b/src/nvim/lua/executor.c
@@ -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)) {
+ 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;
}