vim-patch:9.0.1295: the option initialization function is too long (#22222)

Problem:    The option initialization function is too long.
Solution:   Move code to separate functions. (Yegappan Lakshmanan,
            closes vim/vim#11966)

6c41bedeed

Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
This commit is contained in:
zeertzjq 2023-02-11 20:56:42 +08:00 committed by GitHub
parent 883ec20d9f
commit 224a3c77ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -166,21 +166,11 @@ void set_init_tablocal(void)
p_ch = (long)options[ch_idx].def_val; p_ch = (long)options[ch_idx].def_val;
} }
/// Initialize the options, first part. /// Initialize the 'shell' option to a default value.
/// static void set_init_default_shell(void)
/// Called only once from main(), just after creating the first buffer.
/// If "clean_arg" is true, Nvim was started with --clean.
///
/// NOTE: ELOG() etc calls are not allowed here, as log location depends on
/// env var expansion which depends on expression evaluation and other
/// editor state initialized here. Do logging in set_init_2 or later.
void set_init_1(bool clean_arg)
{ {
langmap_init();
// Find default value for 'shell' option. // Find default value for 'shell' option.
// Don't use it if it is empty. // Don't use it if it is empty.
{
const char *shell = os_getenv("SHELL"); const char *shell = os_getenv("SHELL");
if (shell != NULL) { if (shell != NULL) {
if (vim_strchr(shell, ' ') != NULL) { if (vim_strchr(shell, ' ') != NULL) {
@ -194,8 +184,9 @@ void set_init_1(bool clean_arg)
} }
} }
// Set the default for 'backupskip' to include environment variables for /// Set the default for 'backupskip' to include environment variables for
// temp files. /// temp files.
static void set_init_default_backupskip(void)
{ {
#ifdef UNIX #ifdef UNIX
static char *(names[4]) = { "", "TMPDIR", "TEMP", "TMP" }; static char *(names[4]) = { "", "TMPDIR", "TEMP", "TMP" };
@ -251,12 +242,15 @@ void set_init_1(bool clean_arg)
} }
} }
/// Initialize the 'cdpath' option to a default value.
static void set_init_default_cdpath(void)
{ {
// Initialize the 'cdpath' option's default value.
char *cdpath = vim_getenv("CDPATH"); char *cdpath = vim_getenv("CDPATH");
if (cdpath != NULL) { if (cdpath == NULL) {
return;
}
char *buf = xmalloc(2 * strlen(cdpath) + 2); char *buf = xmalloc(2 * strlen(cdpath) + 2);
{
buf[0] = ','; // start with ",", current dir first buf[0] = ','; // start with ",", current dir first
int j = 1; int j = 1;
for (int i = 0; cdpath[i] != NUL; i++) { for (int i = 0; cdpath[i] != NUL; i++) {
@ -277,10 +271,70 @@ void set_init_1(bool clean_arg)
} else { } else {
xfree(buf); // cannot happen xfree(buf); // cannot happen
} }
}
xfree(cdpath); xfree(cdpath);
} }
/// Expand environment variables and things like "~" for the defaults.
/// If option_expand() returns non-NULL the variable is expanded. This can
/// only happen for non-indirect options.
/// Also set the default to the expanded value, so ":set" does not list
/// them.
/// Don't set the P_ALLOCED flag, because we don't want to free the
/// default.
static void set_init_expand_env(void)
{
for (int opt_idx = 0; options[opt_idx].fullname; opt_idx++) {
vimoption_T *opt = &options[opt_idx];
if (opt->flags & P_NO_DEF_EXP) {
continue;
} }
char *p;
if ((opt->flags & P_GETTEXT) && opt->var != NULL) {
p = _(*(char **)opt->var);
} else {
p = option_expand(opt_idx, NULL);
}
if (p != NULL) {
p = xstrdup(p);
*(char **)opt->var = p;
if (opt->flags & P_DEF_ALLOCED) {
xfree(opt->def_val);
}
opt->def_val = p;
opt->flags |= P_DEF_ALLOCED;
}
}
}
/// Initialize the encoding used for "default" in 'fileencodings'.
static void set_init_fenc_default(void)
{
// enc_locale() will try to find the encoding of the current locale.
// This will be used when "default" is used as encoding specifier
// in 'fileencodings'.
char *p = enc_locale();
if (p == NULL) {
// Use utf-8 as "default" if locale encoding can't be detected.
p = xmemdupz(S_LEN("utf-8"));
}
fenc_default = p;
}
/// Initialize the options, first part.
///
/// Called only once from main(), just after creating the first buffer.
/// If "clean_arg" is true, Nvim was started with --clean.
///
/// NOTE: ELOG() etc calls are not allowed here, as log location depends on
/// env var expansion which depends on expression evaluation and other
/// editor state initialized here. Do logging in set_init_2 or later.
void set_init_1(bool clean_arg)
{
langmap_init();
set_init_default_shell();
set_init_default_backupskip();
set_init_default_cdpath();
char *backupdir = stdpaths_user_state_subpath("backup", 2, true); char *backupdir = stdpaths_user_state_subpath("backup", 2, true);
const size_t backupdir_len = strlen(backupdir); const size_t backupdir_len = strlen(backupdir);
@ -329,33 +383,7 @@ void set_init_1(bool clean_arg)
init_spell_chartab(); init_spell_chartab();
// Expand environment variables and things like "~" for the defaults. // Expand environment variables and things like "~" for the defaults.
// If option_expand() returns non-NULL the variable is expanded. This can set_init_expand_env();
// only happen for non-indirect options.
// Also set the default to the expanded value, so ":set" does not list
// them.
// Don't set the P_ALLOCED flag, because we don't want to free the
// default.
for (int opt_idx = 0; options[opt_idx].fullname; opt_idx++) {
vimoption_T *opt = &options[opt_idx];
if (opt->flags & P_NO_DEF_EXP) {
continue;
}
char *p;
if ((opt->flags & P_GETTEXT) && opt->var != NULL) {
p = _(*(char **)opt->var);
} else {
p = option_expand(opt_idx, NULL);
}
if (p != NULL) {
p = xstrdup(p);
*(char **)opt->var = p;
if (opt->flags & P_DEF_ALLOCED) {
xfree(opt->def_val);
}
opt->def_val = p;
opt->flags |= P_DEF_ALLOCED;
}
}
save_file_ff(curbuf); // Buffer is unchanged save_file_ff(curbuf); // Buffer is unchanged
@ -371,16 +399,7 @@ void set_init_1(bool clean_arg)
didset_options2(); didset_options2();
lang_init(); lang_init();
set_init_fenc_default();
// enc_locale() will try to find the encoding of the current locale.
// This will be used when 'default' is used as encoding specifier
// in 'fileencodings'
char *p = enc_locale();
if (p == NULL) {
// use utf-8 as 'default' if locale encoding can't be detected.
p = xmemdupz(S_LEN("utf-8"));
}
fenc_default = p;
#ifdef HAVE_WORKING_LIBINTL #ifdef HAVE_WORKING_LIBINTL
// GNU gettext 0.10.37 supports this feature: set the codeset used for // GNU gettext 0.10.37 supports this feature: set the codeset used for