feat(exrc): support .nvim.lua (#21436)

This commit is contained in:
Munif Tanjim 2022-12-19 22:33:47 +06:00 committed by GitHub
parent f4d8e992bf
commit 23d8f5b870
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 50 additions and 23 deletions

View File

@ -98,6 +98,7 @@ CHANGED FEATURES *news-changes*
The following changes to existing APIs or features add new behavior. The following changes to existing APIs or features add new behavior.
• 'exrc' now supports `.nvim.lua` file.
• 'exrc' is no longer marked deprecated. • 'exrc' is no longer marked deprecated.
============================================================================== ==============================================================================

View File

@ -2267,7 +2267,7 @@ A jump table for the options with a short description can be found at |Q_op|.
*'exrc'* *'ex'* *'noexrc'* *'noex'* *'exrc'* *'ex'* *'noexrc'* *'noex'*
'exrc' 'ex' boolean (default off) 'exrc' 'ex' boolean (default off)
global global
Enables the reading of .nvimrc and .exrc files in the current Enables the reading of .nvim.lua, .nvimrc, and .exrc files in the current
directory. directory.
The file is only sourced if the user indicates the file is trusted. If The file is only sourced if the user indicates the file is trusted. If

View File

@ -698,7 +698,7 @@ Short explanation of each option: *option-list*
'errorformat' 'efm' description of the lines in the error file 'errorformat' 'efm' description of the lines in the error file
'eventignore' 'ei' autocommand events that are ignored 'eventignore' 'ei' autocommand events that are ignored
'expandtab' 'et' use spaces when <Tab> is inserted 'expandtab' 'et' use spaces when <Tab> is inserted
'exrc' 'ex' read .nvimrc and .exrc in the current directory 'exrc' 'ex' read init files in the current directory
'fileencoding' 'fenc' file encoding for multibyte text 'fileencoding' 'fenc' file encoding for multibyte text
'fileencodings' 'fencs' automatically detected character encodings 'fileencodings' 'fencs' automatically detected character encodings
'fileformat' 'ff' file format used for file I/O 'fileformat' 'ff' file format used for file I/O

View File

@ -453,10 +453,11 @@ accordingly, proceeding as follows:
set or when using $VIMINIT. set or when using $VIMINIT.
c. If the 'exrc' option is on (which is NOT the default), the current c. If the 'exrc' option is on (which is NOT the default), the current
directory is searched for two files. The first that exists is used, directory is searched for the following files, in order of precedence:
the others are ignored. - ".nvim.lua"
- The file ".nvimrc" - ".nvimrc"
- The file ".exrc" - ".exrc"
The first that exists is used, the others are ignored.
8. Enable filetype detection. 8. Enable filetype detection.
This does the same as the command: > This does the same as the command: >

View File

@ -434,8 +434,8 @@ Options:
'jumpoptions' "view" tries to restore the |mark-view| when moving through 'jumpoptions' "view" tries to restore the |mark-view| when moving through
the |jumplist|, |changelist|, |alternate-file| or using |mark-motions|. the |jumplist|, |changelist|, |alternate-file| or using |mark-motions|.
'shortmess' the "F" flag does not affect output from autocommands 'shortmess' the "F" flag does not affect output from autocommands
'exrc' searches for ".nvimrc" or ".exrc" files. The user is prompted whether 'exrc' searches for ".nvim.lua", ".nvimrc", or ".exrc" files. The user is
to trust the file. prompted whether to trust the file.
Shell: Shell:
Shell output (|:!|, |:make|, …) is always routed through the UI, so it Shell output (|:!|, |:make|, …) is always routed through the UI, so it

View File

@ -75,6 +75,10 @@
# define VIMRC_FILE ".nvimrc" # define VIMRC_FILE ".nvimrc"
#endif #endif
#ifndef VIMRC_LUA_FILE
# define VIMRC_LUA_FILE ".nvim.lua"
#endif
EXTERN struct nvim_stats_s { EXTERN struct nvim_stats_s {
int64_t fsync; int64_t fsync;
int64_t redraw; int64_t redraw;

View File

@ -1987,6 +1987,41 @@ static bool do_user_initialization(void)
return do_exrc; return do_exrc;
} }
// Read initialization commands from ".nvim.lua", ".nvimrc", or ".exrc" in
// current directory. This is only done if the 'exrc' option is set.
// Only do this if VIMRC_FILE is not the same as vimrc file sourced in
// do_user_initialization.
static void do_exrc_initialization(void)
{
char *str;
if (os_path_exists(VIMRC_LUA_FILE)) {
str = nlua_read_secure(VIMRC_LUA_FILE);
if (str != NULL) {
Error err = ERROR_INIT;
nlua_exec(cstr_as_string(str), (Array)ARRAY_DICT_INIT, &err);
xfree(str);
if (ERROR_SET(&err)) {
semsg("Error detected while processing %s:", VIMRC_LUA_FILE);
semsg_multiline(err.msg);
api_clear_error(&err);
}
}
} else if (os_path_exists(VIMRC_FILE)) {
str = nlua_read_secure(VIMRC_FILE);
if (str != NULL) {
do_source_str(str, VIMRC_FILE);
xfree(str);
}
} else if (os_path_exists(EXRC_FILE)) {
str = nlua_read_secure(EXRC_FILE);
if (str != NULL) {
do_source_str(str, EXRC_FILE);
xfree(str);
}
}
}
/// Source startup scripts /// Source startup scripts
static void source_startup_scripts(const mparm_T *const parmp) static void source_startup_scripts(const mparm_T *const parmp)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_ALL
@ -2005,21 +2040,7 @@ static void source_startup_scripts(const mparm_T *const parmp)
do_system_initialization(); do_system_initialization();
if (do_user_initialization()) { if (do_user_initialization()) {
// Read initialization commands from ".nvimrc" or ".exrc" in current do_exrc_initialization();
// directory. This is only done if the 'exrc' option is set.
// Only do this if VIMRC_FILE is not the same as vimrc file sourced in
// do_user_initialization.
char *str = nlua_read_secure(VIMRC_FILE);
if (str != NULL) {
do_source_str(str, VIMRC_FILE);
xfree(str);
} else {
str = nlua_read_secure(EXRC_FILE);
if (str != NULL) {
do_source_str(str, EXRC_FILE);
xfree(str);
}
}
} }
} }
TIME_MSG("sourcing vimrc file(s)"); TIME_MSG("sourcing vimrc file(s)");