diff options
author | Javier Lopez <graulopezjavier@gmail.com> | 2022-02-27 14:35:06 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-27 12:35:06 -0700 |
commit | 1b5767aa3480c0cdc43f7a4b78f36a14e85a182f (patch) | |
tree | 2329eff263bf6893de4108ab6890515e5f1c3970 /src/nvim/lua/executor.c | |
parent | c65d93e60adcacded822f0ad5d539042e600f523 (diff) | |
download | rneovim-1b5767aa3480c0cdc43f7a4b78f36a14e85a182f.tar.gz rneovim-1b5767aa3480c0cdc43f7a4b78f36a14e85a182f.tar.bz2 rneovim-1b5767aa3480c0cdc43f7a4b78f36a14e85a182f.zip |
feat(lua): add <f-args> to user commands callback (#17522)
Works similar to ex <f-args>. It only splits the arguments if the
command has more than one posible argument. In cases were the command
can only have 1 argument opts.fargs = { opts.args }
Diffstat (limited to 'src/nvim/lua/executor.c')
-rw-r--r-- | src/nvim/lua/executor.c | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index d207f48435..3c1676581c 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -1814,8 +1814,31 @@ void nlua_do_ucmd(ucmd_T *cmd, exarg_T *eap) lua_pushinteger(lstate, eap->line2); lua_setfield(lstate, -2, "line2"); + lua_newtable(lstate); // f-args table lua_pushstring(lstate, (const char *)eap->arg); - lua_setfield(lstate, -2, "args"); + lua_pushvalue(lstate, -1); // Reference for potential use on f-args + lua_setfield(lstate, -4, "args"); + + // Split args by unescaped whitespace |<f-args>| (nargs dependent) + if (cmd->uc_argt & EX_NOSPC) { + // Commands where nargs = 1 or "?" fargs is the same as args + lua_rawseti(lstate, -2, 1); + } else { + // Commands with more than one possible argument we split + lua_pop(lstate, 1); // Pop the reference of opts.args + int length = (int)STRLEN(eap->arg); + int start = 0; + int end = 0; + int i = 1; + bool res = true; + while (res) { + res = uc_split_args_iter(eap->arg, i, &start, &end, length); + lua_pushlstring(lstate, (const char *)eap->arg + start, (size_t)(end - start + 1)); + lua_rawseti(lstate, -2, i); + i++; + } + } + lua_setfield(lstate, -2, "fargs"); lua_pushstring(lstate, (const char *)&eap->regname); lua_setfield(lstate, -2, "reg"); |