mirror of
https://github.com/neovim/neovim.git
synced 2025-09-07 03:48:18 +00:00
feat(lua): store "nvim -l" scriptname in _G.arg[0]
This commit is contained in:
@@ -218,15 +218,13 @@ argument.
|
|||||||
-l {script} [args]
|
-l {script} [args]
|
||||||
Executes Lua {script} non-interactively (no UI) with optional
|
Executes Lua {script} non-interactively (no UI) with optional
|
||||||
[args] after processing any preceding Nvim |cli-arguments|,
|
[args] after processing any preceding Nvim |cli-arguments|,
|
||||||
then exits. See |-S| to run multiple Lua scripts without args,
|
then exits. Exits 1 on Lua error. See |-S| to run multiple Lua
|
||||||
or in an interactive session.
|
scripts without args, with a UI.
|
||||||
*lua-args*
|
*lua-args*
|
||||||
All [args] are treated as {script} arguments and passed
|
All [args] are treated as {script} arguments and stored in the
|
||||||
literally to Lua (in the conventional `_G.arg` global table),
|
Lua `_G.arg` global table, thus "-l" ends processing of Nvim
|
||||||
thus "-l" ends processing of Nvim arguments.
|
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
|
Sets 'verbose' to 1 (like "-V1"), so Lua `print()` writes to
|
||||||
output.
|
output.
|
||||||
|
|
||||||
|
@@ -323,13 +323,12 @@ static int nlua_thr_api_nvim__get_runtime(lua_State *lstate)
|
|||||||
return 1;
|
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
|
/// nvim -l foo.lua --arg1 --arg2
|
||||||
///
|
///
|
||||||
/// @note Lua CLI sets arguments upto "-e" as _negative_ `_G.arg` indices, but we currently don't
|
/// @note Lua CLI sets args before "-e" as _negative_ `_G.arg` indices, but we currently don't.
|
||||||
/// follow that convention.
|
|
||||||
///
|
///
|
||||||
/// @see https://www.lua.org/pil/1.4.html
|
/// @see https://www.lua.org/pil/1.4.html
|
||||||
/// @see https://github.com/premake/premake-core/blob/1c1304637f4f5e50ba8c57aae8d1d80ec3b7aaf2/src/host/premake.c#L563-L594
|
/// @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
|
/// @returns number of args
|
||||||
static int nlua_init_argv(lua_State *const L, char **argv, int argc, int lua_arg0)
|
static int nlua_init_argv(lua_State *const L, char **argv, int argc, int lua_arg0)
|
||||||
{
|
{
|
||||||
lua_newtable(L); // _G.arg
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (; lua_arg0 >= 0 && i + lua_arg0 < argc; i++) {
|
lua_newtable(L); // _G.arg
|
||||||
lua_pushstring(L, argv[i + lua_arg0]);
|
|
||||||
lua_rawseti(L, -2, i + 1); // _G.arg[i+1] = "arg1"
|
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");
|
lua_setglobal(L, "arg");
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
@@ -108,7 +108,9 @@ describe('startup', function()
|
|||||||
assert_l_out([[
|
assert_l_out([[
|
||||||
bufs:
|
bufs:
|
||||||
nvim args: 7
|
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' }
|
{ '-arg1', "--exitcode", "73", '--arg2' }
|
||||||
)
|
)
|
||||||
@@ -126,11 +128,11 @@ describe('startup', function()
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
it('executes stdin "-"', function()
|
it('executes stdin "-"', function()
|
||||||
assert_l_out('args=2 whoa',
|
assert_l_out('arg0=- args=2 whoa',
|
||||||
nil,
|
nil,
|
||||||
{ 'arg1', 'arg 2' },
|
{ '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',
|
assert_l_out('biiig input: 1000042',
|
||||||
nil,
|
nil,
|
||||||
nil,
|
nil,
|
||||||
@@ -140,23 +142,15 @@ describe('startup', function()
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
it('sets _G.arg', function()
|
it('sets _G.arg', function()
|
||||||
-- nvim -l foo.lua -arg1 -- a b c
|
-- nvim -l foo.lua [args]
|
||||||
assert_l_out([[
|
assert_l_out([[
|
||||||
bufs:
|
bufs:
|
||||||
nvim args: 6
|
nvim args: 7
|
||||||
lua args: { "-arg1", "--arg2", "arg3" }]],
|
lua args: { "-arg1", "--arg2", "--", "arg3",
|
||||||
|
[0] = "test/functional/fixtures/startup.lua"
|
||||||
|
}]],
|
||||||
{},
|
{},
|
||||||
{ '-arg1', '--arg2', 'arg3' }
|
{ '-arg1', '--arg2', '--', 'arg3' }
|
||||||
)
|
|
||||||
eq(0, eval('v:shell_error'))
|
|
||||||
|
|
||||||
-- nvim -l foo.lua --
|
|
||||||
assert_l_out([[
|
|
||||||
bufs:
|
|
||||||
nvim args: 4
|
|
||||||
lua args: { "--" }]],
|
|
||||||
{},
|
|
||||||
{ '--' }
|
|
||||||
)
|
)
|
||||||
eq(0, eval('v:shell_error'))
|
eq(0, eval('v:shell_error'))
|
||||||
|
|
||||||
@@ -164,27 +158,21 @@ describe('startup', function()
|
|||||||
assert_l_out([[
|
assert_l_out([[
|
||||||
bufs: file1 file2
|
bufs: file1 file2
|
||||||
nvim args: 10
|
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', },
|
{ 'file1', 'file2', },
|
||||||
{ '-arg1', 'arg 2', '--', 'file3', 'file4' }
|
{ '-arg1', 'arg 2', '--', 'file3', 'file4' }
|
||||||
)
|
)
|
||||||
eq(0, eval('v:shell_error'))
|
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>
|
-- nvim -l foo.lua <vim args>
|
||||||
assert_l_out([[
|
assert_l_out([[
|
||||||
bufs:
|
bufs:
|
||||||
nvim args: 5
|
nvim args: 5
|
||||||
lua args: { "-c", "set wrap?" }]],
|
lua args: { "-c", "set wrap?",
|
||||||
|
[0] = "test/functional/fixtures/startup.lua"
|
||||||
|
}]],
|
||||||
{},
|
{},
|
||||||
{ '-c', 'set wrap?' }
|
{ '-c', 'set wrap?' }
|
||||||
)
|
)
|
||||||
@@ -198,7 +186,9 @@ describe('startup', function()
|
|||||||
|
|
||||||
bufs:
|
bufs:
|
||||||
nvim args: 7
|
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?' },
|
||||||
{ '-c', 'set wrap?' }
|
{ '-c', 'set wrap?' }
|
||||||
)
|
)
|
||||||
|
Reference in New Issue
Block a user