mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
feat(lua): store "nvim -l" scriptname in _G.arg[0]
This commit is contained in:
parent
7fc5d6ea50
commit
e17430c1cd
@ -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.
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
-- nvim -l foo.lua [args]
|
||||
assert_l_out([[
|
||||
bufs:
|
||||
nvim args: 6
|
||||
lua args: { "-arg1", "--arg2", "arg3" }]],
|
||||
nvim args: 7
|
||||
lua args: { "-arg1", "--arg2", "--", "arg3",
|
||||
[0] = "test/functional/fixtures/startup.lua"
|
||||
}]],
|
||||
{},
|
||||
{ '-arg1', '--arg2', 'arg3' }
|
||||
)
|
||||
eq(0, eval('v:shell_error'))
|
||||
|
||||
-- nvim -l foo.lua --
|
||||
assert_l_out([[
|
||||
bufs:
|
||||
nvim args: 4
|
||||
lua args: { "--" }]],
|
||||
{},
|
||||
{ '--' }
|
||||
{ '-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?' }
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user