diff options
author | dm1try <me@dmitry.it> | 2020-05-03 23:49:11 +0300 |
---|---|---|
committer | Björn Linse <bjorn.linse@gmail.com> | 2020-12-01 10:51:25 +0100 |
commit | 767cd8b17b71f78bdd4c2e0dd8d3f4f0f1551381 (patch) | |
tree | 9d6d1ac306c0727e5f37c0c0cb58d665a8cf4c82 | |
parent | 13b88573005d84cc0ebcd7e7bf4dd488673919d3 (diff) | |
download | rneovim-767cd8b17b71f78bdd4c2e0dd8d3f4f0f1551381.tar.gz rneovim-767cd8b17b71f78bdd4c2e0dd8d3f4f0f1551381.tar.bz2 rneovim-767cd8b17b71f78bdd4c2e0dd8d3f4f0f1551381.zip |
startup: add init.lua as an alternative user config, fixes #7895
-rw-r--r-- | src/nvim/lua/executor.c | 17 | ||||
-rw-r--r-- | src/nvim/main.c | 25 | ||||
-rw-r--r-- | test/functional/core/startup_spec.lua | 61 |
3 files changed, 101 insertions, 2 deletions
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) { diff --git a/test/functional/core/startup_spec.lua b/test/functional/core/startup_spec.lua index 27793ab936..22880a1fe8 100644 --- a/test/functional/core/startup_spec.lua +++ b/test/functional/core/startup_spec.lua @@ -432,3 +432,64 @@ describe('clean', function() clear('--clean') ok(string.match(meths.get_option('runtimepath'), funcs.stdpath('config')) == nil) end) + +describe('user config init', function() + local xhome = 'Xhome' + local pathsep = helpers.get_pathsep() + local xconfig = xhome .. pathsep .. 'Xconfig' + local init_lua_path = table.concat({xconfig, 'nvim', 'init.lua'}, pathsep) + + before_each(function() + rmdir(xhome) + + -- TODO, make mkdir_p helper + mkdir(xhome) + mkdir(xconfig) + mkdir(xconfig .. pathsep .. 'nvim') + + write_file(init_lua_path, [[ + vim.g.lua_rc = 1 + ]]) + end) + + after_each(function() + rmdir(xhome) + end) + + it('loads init.lua from XDG config home by default', function() + clear{ args_rm={'-u' }, env={ XDG_CONFIG_HOME=xconfig }} + + eq(1, eval('g:lua_rc')) + eq(init_lua_path, eval('$MYVIMRC')) + end) + + describe 'with explicitly provided config'(function() + local custom_lua_path = table.concat({xhome, 'custom.lua'}, pathsep) + before_each(function() + write_file(custom_lua_path, [[ + vim.g.custom_lua_rc = 1 + ]]) + end) + + it('loads custom lua config and does not set $MYVIMRC', function() + clear{ args={'-u', custom_lua_path }, env={ XDG_CONFIG_HOME=xconfig }} + eq(1, eval('g:custom_lua_rc')) + eq('', eval('$MYVIMRC')) + end) + end) + + describe 'VIMRC also exists'(function() + before_each(function() + write_file(table.concat({xconfig, 'nvim', 'init.vim'}, pathsep), [[ + let g:vim_rc = 1 + ]]) + end) + + it('loads default lua config, but shows an error', function() + clear{ args_rm={'-u'}, env={ XDG_CONFIG_HOME=xconfig }} + feed('<cr>') -- TODO check this, test execution is blocked without it + eq(1, eval('g:lua_rc')) + matches('Conflicting configs', meths.exec('messages', true)) + end) + end) +end) |