aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin M. Keyes <justinkz@gmail.com>2023-01-07 02:12:54 +0100
committerJustin M. Keyes <justinkz@gmail.com>2023-01-07 03:03:38 +0100
commite17430c1cd97db5624e27515a4f5da17f9d926d6 (patch)
tree44196a84757ee1130827451635e4a5090e7fc8b0
parent7fc5d6ea50f5d04ad1d4a71fd0429bfd72f2c66e (diff)
downloadrneovim-e17430c1cd97db5624e27515a4f5da17f9d926d6.tar.gz
rneovim-e17430c1cd97db5624e27515a4f5da17f9d926d6.tar.bz2
rneovim-e17430c1cd97db5624e27515a4f5da17f9d926d6.zip
feat(lua): store "nvim -l" scriptname in _G.arg[0]
-rw-r--r--runtime/doc/starting.txt12
-rw-r--r--src/nvim/lua/executor.c22
-rw-r--r--test/functional/core/startup_spec.lua50
3 files changed, 39 insertions, 45 deletions
diff --git a/runtime/doc/starting.txt b/runtime/doc/starting.txt
index fa9e23eb00..179bacdb24 100644
--- a/runtime/doc/starting.txt
+++ b/runtime/doc/starting.txt
@@ -218,15 +218,13 @@ argument.
-l {script} [args]
Executes Lua {script} non-interactively (no UI) with optional
[args] after processing any preceding Nvim |cli-arguments|,
- then exits. See |-S| to run multiple Lua scripts without args,
- or in an interactive session.
+ then exits. Exits 1 on Lua error. See |-S| to run multiple Lua
+ scripts without args, with a UI.
*lua-args*
- All [args] are treated as {script} arguments and passed
- literally to Lua (in the conventional `_G.arg` global table),
- thus "-l" ends processing of Nvim arguments.
+ All [args] are treated as {script} arguments and stored in the
+ Lua `_G.arg` global table, thus "-l" ends processing of Nvim
+ arguments. The {script} name is stored at `_G.arg[0]`.
- Exits with code 1 on Lua error.
-
Sets 'verbose' to 1 (like "-V1"), so Lua `print()` writes to
output.
diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c
index 4ac3c78b0a..cd022068ce 100644
--- a/src/nvim/lua/executor.c
+++ b/src/nvim/lua/executor.c
@@ -323,13 +323,12 @@ static int nlua_thr_api_nvim__get_runtime(lua_State *lstate)
return 1;
}
-/// Copies args starting at `lua_arg0` into the Lua `arg` global.
+/// Copies args starting at `lua_arg0` to Lua `_G.arg`, and sets `_G.arg[0]` to the scriptname.
///
-/// Example (`lua_arg0` points to "--arg1"):
+/// Example (arg[0] => "foo.lua", arg[1] => "--arg1", …):
/// nvim -l foo.lua --arg1 --arg2
///
-/// @note Lua CLI sets arguments upto "-e" as _negative_ `_G.arg` indices, but we currently don't
-/// follow that convention.
+/// @note Lua CLI sets args before "-e" as _negative_ `_G.arg` indices, but we currently don't.
///
/// @see https://www.lua.org/pil/1.4.html
/// @see https://github.com/premake/premake-core/blob/1c1304637f4f5e50ba8c57aae8d1d80ec3b7aaf2/src/host/premake.c#L563-L594
@@ -337,12 +336,19 @@ static int nlua_thr_api_nvim__get_runtime(lua_State *lstate)
/// @returns number of args
static int nlua_init_argv(lua_State *const L, char **argv, int argc, int lua_arg0)
{
- lua_newtable(L); // _G.arg
int i = 0;
- 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_newtable(L); // _G.arg
+
+ if (lua_arg0 > 0) {
+ lua_pushstring(L, argv[lua_arg0 - 1]);
+ lua_rawseti(L, -2, 0); // _G.arg[0] = "foo.lua"
+
+ 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] = "--foo"
+ }
}
+
lua_setglobal(L, "arg");
return i;
}
diff --git a/test/functional/core/startup_spec.lua b/test/functional/core/startup_spec.lua
index d7e64c6b54..9dabcd28b3 100644
--- a/test/functional/core/startup_spec.lua
+++ b/test/functional/core/startup_spec.lua
@@ -108,7 +108,9 @@ describe('startup', function()
assert_l_out([[
bufs:
nvim args: 7
- lua args: { "-arg1", "--exitcode", "73", "--arg2" }]],
+ lua args: { "-arg1", "--exitcode", "73", "--arg2",
+ [0] = "test/functional/fixtures/startup.lua"
+ }]],
{},
{ '-arg1', "--exitcode", "73", '--arg2' }
)
@@ -126,11 +128,11 @@ describe('startup', function()
end)
it('executes stdin "-"', function()
- assert_l_out('args=2 whoa',
+ assert_l_out('arg0=- args=2 whoa',
nil,
{ 'arg1', 'arg 2' },
'-',
- "print(('args=%d %s'):format(#_G.arg, 'whoa'))")
+ "print(('arg0=%s args=%d %s'):format(_G.arg[0], #_G.arg, 'whoa'))")
assert_l_out('biiig input: 1000042',
nil,
nil,
@@ -140,23 +142,15 @@ describe('startup', function()
end)
it('sets _G.arg', function()
- -- nvim -l foo.lua -arg1 -- a b c
- assert_l_out([[
- bufs:
- nvim args: 6
- lua args: { "-arg1", "--arg2", "arg3" }]],
- {},
- { '-arg1', '--arg2', 'arg3' }
- )
- eq(0, eval('v:shell_error'))
-
- -- nvim -l foo.lua --
+ -- nvim -l foo.lua [args]
assert_l_out([[
bufs:
- nvim args: 4
- lua args: { "--" }]],
+ nvim args: 7
+ lua args: { "-arg1", "--arg2", "--", "arg3",
+ [0] = "test/functional/fixtures/startup.lua"
+ }]],
{},
- { '--' }
+ { '-arg1', '--arg2', '--', 'arg3' }
)
eq(0, eval('v:shell_error'))
@@ -164,27 +158,21 @@ describe('startup', function()
assert_l_out([[
bufs: file1 file2
nvim args: 10
- lua args: { "-arg1", "arg 2", "--", "file3", "file4" }]],
+ lua args: { "-arg1", "arg 2", "--", "file3", "file4",
+ [0] = "test/functional/fixtures/startup.lua"
+ }]],
{ 'file1', 'file2', },
{ '-arg1', 'arg 2', '--', 'file3', 'file4' }
)
eq(0, eval('v:shell_error'))
- -- nvim file1 file2 -l foo.lua -arg1 --
- assert_l_out([[
- bufs: file1 file2
- nvim args: 7
- lua args: { "-arg1", "--" }]],
- { 'file1', 'file2', },
- { '-arg1', '--' }
- )
- eq(0, eval('v:shell_error'))
-
-- nvim -l foo.lua <vim args>
assert_l_out([[
bufs:
nvim args: 5
- lua args: { "-c", "set wrap?" }]],
+ lua args: { "-c", "set wrap?",
+ [0] = "test/functional/fixtures/startup.lua"
+ }]],
{},
{ '-c', 'set wrap?' }
)
@@ -198,7 +186,9 @@ describe('startup', function()
bufs:
nvim args: 7
- lua args: { "-c", "set wrap?" }]],
+ lua args: { "-c", "set wrap?",
+ [0] = "test/functional/fixtures/startup.lua"
+ }]],
{ '-c', 'set wrap?' },
{ '-c', 'set wrap?' }
)