mirror of
https://github.com/neovim/neovim.git
synced 2025-09-08 04:18:18 +00:00
feat(lua)!: execute Lua with "nvim -l"
Problem: Nvim has Lua but the "nvim" CLI can't easily be used to execute Lua scripts, especially scripts that take arguments or produce output. Solution: - support "nvim -l [args...]" for running scripts. closes #15749 - exit without +q - remove lua2dox_filter - remove Doxyfile. This wasn't used anyway, because the doxygen config is inlined in gen_vimdoc.py (`Doxyfile` variable). - use "nvim -l" in docs-gen CI job Examples: $ nvim -l scripts/lua2dox.lua --help Lua2DoX (0.2 20130128) ... $ echo "print(vim.inspect(_G.arg))" | nvim -l - --arg1 --arg2 $ echo 'print(vim.inspect(vim.api.nvim_buf_get_text(1,0,0,-1,-1,{})))' | nvim +"put ='text'" -l - TODO? -e executes Lua code -l loads a module -i enters REPL _after running the other arguments_.
This commit is contained in:
@@ -323,6 +323,34 @@ static int nlua_thr_api_nvim__get_runtime(lua_State *lstate)
|
||||
return 1;
|
||||
}
|
||||
|
||||
/// Copies all args into the Lua `arg` global.
|
||||
///
|
||||
/// Example:
|
||||
/// nvim -l foo.lua -- -e "sin=math.sin" script a b
|
||||
///
|
||||
/// @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)
|
||||
{
|
||||
lua_State *const L = global_lstate;
|
||||
lua_newtable(L);
|
||||
int i = 0;
|
||||
for (; i < argc; i++) {
|
||||
if (strequal("--", argv[i])) {
|
||||
i--;
|
||||
break;
|
||||
}
|
||||
lua_pushstring(L, argv[i]);
|
||||
lua_rawseti(L, -2, i + 1);
|
||||
}
|
||||
lua_setglobal(L, "arg");
|
||||
return i + 1;
|
||||
}
|
||||
|
||||
static void nlua_schedule_event(void **argv)
|
||||
{
|
||||
LuaRef cb = (LuaRef)(ptrdiff_t)argv[0];
|
||||
|
Reference in New Issue
Block a user