From 45549f031ee52a01601c33acc411f3111cfc4e95 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Sun, 1 Jan 2023 03:14:13 +0100 Subject: feat(lua): send "--" literally to Lua "-l" script Problem: When "-l" is followed by "--", we stop sending args to the Lua script and treat "--" in the usual way. This was for flexibility but didn't have a strong use-case, and has these problems: - prevents Lua "-l" scripts from handling "--" in their own way. - complicates the startup logic (must call nlua_init before command_line_scan) Solution: Don't treat "--" specially if it follows "-l". --- src/nvim/lua/executor.c | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) (limited to 'src/nvim/lua/executor.c') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 1bedb70efb..20c901004a 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -323,32 +323,28 @@ static int nlua_thr_api_nvim__get_runtime(lua_State *lstate) return 1; } -/// Copies all args into the Lua `arg` global. +/// Copies args starting at `lua_arg0` into the Lua `arg` global. /// /// Example: -/// nvim -l foo.lua -- -e "sin=math.sin" script a b +/// nvim -l foo.lua --arg1 --arg2 /// /// @note `lua` CLI sets _negative_ `arg` indices to the arguments upto "-e". /// /// @see https://www.lua.org/pil/1.4.html /// @see https://github.com/premake/premake-core/blob/1c1304637f4f5e50ba8c57aae8d1d80ec3b7aaf2/src/host/premake.c#L563-L594 /// -/// @returns number of args (stops at "--") -int nlua_set_argv(char **argv, int argc) +/// @returns number of args +int nlua_set_argv(char **argv, int argc, int lua_arg0) { lua_State *const L = global_lstate; - lua_newtable(L); + lua_newtable(L); // _G.arg int i = 0; - for (; i < argc; i++) { - if (strequal("--", argv[i])) { - i--; - break; - } - lua_pushstring(L, argv[i]); - lua_rawseti(L, -2, i + 1); + for (; lua_arg0 >= 0 && i + lua_arg0 < argc; i++) { + lua_pushstring(L, argv[i + lua_arg0]); + lua_rawseti(L, -2, i + 1); // _G.arg[i+1] = "arg1" } lua_setglobal(L, "arg"); - return i + 1; + return i; } static void nlua_schedule_event(void **argv) -- cgit