From 13b88573005d84cc0ebcd7e7bf4dd488673919d3 Mon Sep 17 00:00:00 2001 From: dm1try Date: Sun, 3 May 2020 23:46:55 +0300 Subject: path: add helper for checking a file extension --- src/nvim/path.c | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'src') diff --git a/src/nvim/path.c b/src/nvim/path.c index f52fbbd5c8..2de7e00ddb 100644 --- a/src/nvim/path.c +++ b/src/nvim/path.c @@ -1704,6 +1704,13 @@ int path_with_url(const char *fname) return path_is_url(p); } +bool path_with_extension(const char *path, const char *extension) +{ + const char *last_dot = strrchr(path, '.'); + if (!last_dot) { return false; } + return strcmp(last_dot + 1, extension) == 0; +} + /* * Return TRUE if "name" is a full (absolute) path name or URL. */ -- cgit From 767cd8b17b71f78bdd4c2e0dd8d3f4f0f1551381 Mon Sep 17 00:00:00 2001 From: dm1try Date: Sun, 3 May 2020 23:49:11 +0300 Subject: startup: add init.lua as an alternative user config, fixes #7895 --- src/nvim/lua/executor.c | 17 +++++++++++++++++ src/nvim/main.c | 25 +++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 0a3c30134b..97f6d5bd31 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -1244,6 +1244,23 @@ void ex_luafile(exarg_T *const eap) } } +bool load_init_lua(const char *script_path) +{ + lua_State *const lstate = nlua_enter(); + + if (luaL_loadfile(lstate, script_path)) { + nlua_error(lstate, _("E5112: Error while creating lua chunk: %.*s")); + return false; + } + + if (lua_pcall(lstate, 0, 0, 0)) { + nlua_error(lstate, _("E5113: Error while calling lua chunk: %.*s")); + return false; + } + + return true; +} + static void nlua_add_treesitter(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL { tslua_init(lstate); diff --git a/src/nvim/main.c b/src/nvim/main.c index 63249416b1..2835174ac6 100644 --- a/src/nvim/main.c +++ b/src/nvim/main.c @@ -1770,6 +1770,23 @@ static bool do_user_initialization(void) do_exrc = p_exrc; return do_exrc; } + + char_u *init_lua_path = (char_u *)stdpaths_user_conf_subpath("init.lua"); + if (os_path_exists(init_lua_path) + && load_init_lua((const char *)init_lua_path)) { + os_setenv("MYVIMRC", (const char *)init_lua_path, 1); + char_u *vimrc_path = (char_u *)stdpaths_user_conf_subpath("init.vim"); + + if (os_path_exists(vimrc_path)) { + EMSG3(_("Conflicting configs: \"%s\" \"%s\""), init_lua_path, vimrc_path); + } + + xfree(vimrc_path); + xfree(init_lua_path); + return false; + } + xfree(init_lua_path); + char_u *user_vimrc = (char_u *)stdpaths_user_conf_subpath("init.vim"); if (do_source(user_vimrc, true, DOSO_VIMRC) != FAIL) { do_exrc = p_exrc; @@ -1829,8 +1846,12 @@ static void source_startup_scripts(const mparm_T *const parmp) || strequal(parmp->use_vimrc, "NORC")) { // Do nothing. } else { - if (do_source((char_u *)parmp->use_vimrc, false, DOSO_NONE) != OK) { - EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc); + if (path_with_extension(parmp->use_vimrc, "lua")) { + load_init_lua(parmp->use_vimrc); + } else { + if (do_source((char_u *)parmp->use_vimrc, false, DOSO_NONE) != OK) { + EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc); + } } } } else if (!silent_mode) { -- cgit From 33f324796cd760fb2ceb17a276660268bface9b6 Mon Sep 17 00:00:00 2001 From: dm1try Date: Sun, 3 May 2020 23:57:47 +0300 Subject: startup: allow lua files as session one --- src/nvim/main.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/nvim/main.c b/src/nvim/main.c index 2835174ac6..a564a8ed9d 100644 --- a/src/nvim/main.c +++ b/src/nvim/main.c @@ -1069,9 +1069,14 @@ static void command_line_scan(mparm_T *parmp) } else { a = argv[0]; } - size_t s_size = STRLEN(a) + 4; + + size_t s_size = STRLEN(a) + 9; char *s = xmalloc(s_size); - snprintf(s, s_size, "so %s", a); + if (path_with_extension(a, "lua")) { + snprintf(s, s_size, "luafile %s", a); + } else { + snprintf(s, s_size, "so %s", a); + } parmp->cmds_tofree[parmp->n_commands] = true; parmp->commands[parmp->n_commands++] = s; } else { -- cgit From eb387ae53009b4da93fbbb3423e5ef01023dc32a Mon Sep 17 00:00:00 2001 From: Björn Linse Date: Thu, 17 Sep 2020 14:36:55 +0200 Subject: executor: use new nlua_ name pattern --- src/nvim/lua/executor.c | 34 +++++++++++++++------------------- src/nvim/main.c | 4 ++-- 2 files changed, 17 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 97f6d5bd31..344a2387d6 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -927,7 +927,7 @@ void nlua_typval_eval(const String str, typval_T *const arg, memcpy(lcmd + sizeof(EVALHEADER) - 1, str.data, str.size); lcmd[lcmd_len - 1] = ')'; #undef EVALHEADER - typval_exec_lua(lcmd, lcmd_len, "luaeval()", arg, 1, true, ret_tv); + nlua_typval_exec(lcmd, lcmd_len, "luaeval()", arg, 1, true, ret_tv); if (lcmd != (char *)IObuff) { xfree(lcmd); @@ -954,16 +954,16 @@ void nlua_typval_call(const char *str, size_t len, typval_T *const args, #undef CALLHEADER #undef CALLSUFFIX - typval_exec_lua(lcmd, lcmd_len, "v:lua", args, argcount, false, ret_tv); + nlua_typval_exec(lcmd, lcmd_len, "v:lua", args, argcount, false, ret_tv); if (lcmd != (char *)IObuff) { xfree(lcmd); } } -static void typval_exec_lua(const char *lcmd, size_t lcmd_len, const char *name, - typval_T *const args, int argcount, bool special, - typval_T *ret_tv) +static void nlua_typval_exec(const char *lcmd, size_t lcmd_len, + const char *name, typval_T *const args, + int argcount, bool special, typval_T *ret_tv) { if (check_secure()) { if (ret_tv) { @@ -1140,7 +1140,7 @@ void ex_lua(exarg_T *const eap) xfree(code); return; } - typval_exec_lua(code, len, ":lua", NULL, 0, false, NULL); + nlua_typval_exec(code, len, ":lua", NULL, 0, false, NULL); xfree(code); } @@ -1231,24 +1231,20 @@ void ex_luado(exarg_T *const eap) void ex_luafile(exarg_T *const eap) FUNC_ATTR_NONNULL_ALL { - lua_State *const lstate = nlua_enter(); - - if (luaL_loadfile(lstate, (const char *)eap->arg)) { - nlua_error(lstate, _("E5112: Error while creating lua chunk: %.*s")); - return; - } - - if (lua_pcall(lstate, 0, 0, 0)) { - nlua_error(lstate, _("E5113: Error while calling lua chunk: %.*s")); - return; - } + nlua_exec_file((const char *)eap->arg); } -bool load_init_lua(const char *script_path) +/// execute lua code from a file. +/// +/// @param path path of the file +/// +/// @return true if everything ok, false if there was an error (echoed) +bool nlua_exec_file(const char *path) + FUNC_ATTR_NONNULL_ALL { lua_State *const lstate = nlua_enter(); - if (luaL_loadfile(lstate, script_path)) { + if (luaL_loadfile(lstate, path)) { nlua_error(lstate, _("E5112: Error while creating lua chunk: %.*s")); return false; } diff --git a/src/nvim/main.c b/src/nvim/main.c index a564a8ed9d..79c165419e 100644 --- a/src/nvim/main.c +++ b/src/nvim/main.c @@ -1778,7 +1778,7 @@ static bool do_user_initialization(void) char_u *init_lua_path = (char_u *)stdpaths_user_conf_subpath("init.lua"); if (os_path_exists(init_lua_path) - && load_init_lua((const char *)init_lua_path)) { + && nlua_exec_file((const char *)init_lua_path)) { os_setenv("MYVIMRC", (const char *)init_lua_path, 1); char_u *vimrc_path = (char_u *)stdpaths_user_conf_subpath("init.vim"); @@ -1852,7 +1852,7 @@ static void source_startup_scripts(const mparm_T *const parmp) // Do nothing. } else { if (path_with_extension(parmp->use_vimrc, "lua")) { - load_init_lua(parmp->use_vimrc); + nlua_exec_file(parmp->use_vimrc); } else { if (do_source((char_u *)parmp->use_vimrc, false, DOSO_NONE) != OK) { EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc); -- cgit