mirror of
				https://github.com/neovim/neovim.git
				synced 2025-10-26 12:27:24 +00:00 
			
		
		
		
	startup: add init.lua as an alternative user config, fixes #7895
This commit is contained in:
		| @@ -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); | ||||
|   | ||||
| @@ -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) { | ||||
|   | ||||
| @@ -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) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 dm1try
					dm1try