0
0
mirror of https://github.com/vim/vim.git synced 2025-09-24 03:44:06 -04:00

patch 9.0.1295: the option initialization function is too long

Problem:    The option initialization function is too long.
Solution:   Move code to separate functions. (Yegappan Lakshmanan,
            closes #11966)
This commit is contained in:
Yegappan Lakshmanan
2023-02-10 14:50:31 +00:00
committed by Bram Moolenaar
parent 80b817b749
commit 6c41bedeed
2 changed files with 397 additions and 299 deletions

View File

@@ -70,36 +70,15 @@ static void paste_option_changed(void);
static void compatible_set(void); static void compatible_set(void);
/* /*
* Initialize the options, first part. * Initialize the 'shell' option to a default value.
*
* Called only once from main(), just after creating the first buffer.
* If "clean_arg" is TRUE Vim was started with --clean.
*/ */
void static void
set_init_1(int clean_arg) set_init_default_shell(void)
{ {
char_u *p; char_u *p;
int opt_idx;
long_u n;
#ifdef FEAT_LANGMAP // Find default value for 'shell' option.
langmap_init(); // Don't use it if it is empty.
#endif
// Be Vi compatible by default
p_cp = TRUE;
// Use POSIX compatibility when $VIM_POSIX is set.
if (mch_getenv((char_u *)"VIM_POSIX") != NULL)
{
set_string_default("cpo", (char_u *)CPO_ALL);
set_string_default("shm", (char_u *)SHM_POSIX);
}
/*
* Find default value for 'shell' option.
* Don't use it if it is empty.
*/
if (((p = mch_getenv((char_u *)"SHELL")) != NULL && *p != NUL) if (((p = mch_getenv((char_u *)"SHELL")) != NULL && *p != NUL)
#if defined(MSWIN) #if defined(MSWIN)
|| ((p = mch_getenv((char_u *)"COMSPEC")) != NULL && *p != NUL) || ((p = mch_getenv((char_u *)"COMSPEC")) != NULL && *p != NUL)
@@ -129,12 +108,18 @@ set_init_1(int clean_arg)
#else #else
set_string_default_esc("sh", p, TRUE); set_string_default_esc("sh", p, TRUE);
#endif #endif
}
/* /*
* 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)
{ {
int opt_idx;
long_u n;
char_u *p;
#ifdef UNIX #ifdef UNIX
static char *(names[4]) = {"", "TMPDIR", "TEMP", "TMP"}; static char *(names[4]) = {"", "TMPDIR", "TEMP", "TMP"};
#else #else
@@ -191,8 +176,15 @@ set_init_1(int clean_arg)
} }
/* /*
* 'maxmemtot' and 'maxmem' may have to be adjusted for available memory * Initialize the 'maxmemtot' and 'maxmem' options to a default value.
* 'maxmemtot' and 'maxmem' may have to be adjusted for available memory.
*/ */
static void
set_init_default_maxmemtot(void)
{
int opt_idx;
long_u n;
opt_idx = findoption((char_u *)"maxmemtot"); opt_idx = findoption((char_u *)"maxmemtot");
if (opt_idx >= 0) if (opt_idx >= 0)
{ {
@@ -221,18 +213,25 @@ set_init_1(int clean_arg)
} }
} }
} }
}
/*
* Initialize the 'cdpath' option to a default value.
*/
static void
set_init_default_cdpath(void)
{ {
int opt_idx;
char_u *cdpath; char_u *cdpath;
char_u *buf; char_u *buf;
int i; int i;
int j; int j;
int mustfree = FALSE; int mustfree = FALSE;
// Initialize the 'cdpath' option's default value.
cdpath = vim_getenv((char_u *)"CDPATH", &mustfree); cdpath = vim_getenv((char_u *)"CDPATH", &mustfree);
if (cdpath != NULL) if (cdpath == NULL)
{ return;
buf = alloc((STRLEN(cdpath) << 1) + 2); buf = alloc((STRLEN(cdpath) << 1) + 2);
if (buf != NULL) if (buf != NULL)
{ {
@@ -262,8 +261,13 @@ set_init_1(int clean_arg)
if (mustfree) if (mustfree)
vim_free(cdpath); vim_free(cdpath);
} }
}
/*
* Initialize the 'printencoding' option to a default value.
*/
static void
set_init_default_printencoding(void)
{
#if defined(FEAT_POSTSCRIPT) && \ #if defined(FEAT_POSTSCRIPT) && \
(defined(MSWIN) || defined(VMS) || defined(MAC) || defined(hpux)) (defined(MSWIN) || defined(VMS) || defined(MAC) || defined(hpux))
// Set print encoding on platforms that don't default to latin1 // Set print encoding on platforms that don't default to latin1
@@ -279,8 +283,15 @@ set_init_1(int clean_arg)
# endif # endif
); );
#endif #endif
}
#ifdef FEAT_POSTSCRIPT #ifdef FEAT_POSTSCRIPT
/*
* Initialize the 'printexpr' option to a default value.
*/
static void
set_init_default_printexpr(void)
{
// 'printexpr' must be allocated to be able to evaluate it. // 'printexpr' must be allocated to be able to evaluate it.
set_string_default("pexpr", set_string_default("pexpr",
# if defined(MSWIN) # if defined(MSWIN)
@@ -292,16 +303,18 @@ set_init_1(int clean_arg)
(char_u *)"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error" (char_u *)"system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error"
# endif # endif
); );
}
#endif #endif
/*
* Set all the options (except the terminal options) to their default
* value. Also set the global value for local options.
*/
set_options_default(0);
#ifdef UNIX #ifdef UNIX
// Force restricted-mode on for "nologin" or "false" $SHELL /*
* Force restricted-mode on for "nologin" or "false" $SHELL
*/
static void
set_init_restricted_mode(void)
{
char_u *p;
p = get_isolated_shell_name(); p = get_isolated_shell_name();
if (p != NULL) if (p != NULL)
{ {
@@ -309,11 +322,19 @@ set_init_1(int clean_arg)
restricted = TRUE; restricted = TRUE;
vim_free(p); vim_free(p);
} }
}
#endif #endif
#ifdef CLEAN_RUNTIMEPATH #ifdef CLEAN_RUNTIMEPATH
if (clean_arg) /*
* When Vim is started with the "--clean" argument, set the default value
* for the 'runtimepath' and 'packpath' options.
*/
static void
set_init_clean_rtp(void)
{ {
int opt_idx;
opt_idx = findoption((char_u *)"runtimepath"); opt_idx = findoption((char_u *)"runtimepath");
if (opt_idx >= 0) if (opt_idx >= 0)
{ {
@@ -329,27 +350,6 @@ set_init_1(int clean_arg)
} }
#endif #endif
#ifdef FEAT_GUI
if (found_reverse_arg)
set_option_value_give_err((char_u *)"bg", 0L, (char_u *)"dark", 0);
#endif
curbuf->b_p_initialized = TRUE;
curbuf->b_p_ar = -1; // no local 'autoread' value
curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
check_buf_options(curbuf);
check_win_options(curwin);
check_options();
// Must be before option_expand(), because that one needs vim_isIDc()
didset_options();
#ifdef FEAT_SPELL
// Use the current chartab for the generic chartab. This is not in
// didset_options() because it only depends on 'encoding'.
init_spell_chartab();
#endif
/* /*
* 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 * If option_expand() returns non-NULL the variable is expanded. This can
@@ -359,6 +359,12 @@ set_init_1(int clean_arg)
* Don't set the P_ALLOCED flag, because we don't want to free the * Don't set the P_ALLOCED flag, because we don't want to free the
* default. * default.
*/ */
static void
set_init_expand_env(void)
{
int opt_idx;
char_u *p;
for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++) for (opt_idx = 0; !istermoption_idx(opt_idx); opt_idx++)
{ {
if ((options[opt_idx].flags & P_GETTEXT) if ((options[opt_idx].flags & P_GETTEXT)
@@ -379,29 +385,21 @@ set_init_1(int clean_arg)
options[opt_idx].flags |= P_DEF_ALLOCED; options[opt_idx].flags |= P_DEF_ALLOCED;
} }
} }
}
save_file_ff(curbuf); // Buffer is unchanged
#if defined(FEAT_ARABIC)
// Detect use of mlterm.
// Mlterm is a terminal emulator akin to xterm that has some special
// abilities (bidi namely).
// NOTE: mlterm's author is being asked to 'set' a variable
// instead of an environment variable due to inheritance.
if (mch_getenv((char_u *)"MLTERM") != NULL)
set_option_value_give_err((char_u *)"tbidi", 1L, NULL, 0);
#endif
didset_options2();
# if defined(MSWIN) && defined(FEAT_GETTEXT)
/* /*
* If $LANG isn't set, try to get a good value for it. This makes the * Initialize the 'LANG' environment variable to a default value.
* right language be used automatically. Don't do this for English.
*/ */
static void
set_init_lang_env(void)
{
#if defined(MSWIN) && defined(FEAT_GETTEXT)
// If $LANG isn't set, try to get a good value for it. This makes the
// right language be used automatically. Don't do this for English.
if (mch_getenv((char_u *)"LANG") == NULL) if (mch_getenv((char_u *)"LANG") == NULL)
{ {
char buf[20]; char buf[20];
long_u n;
// Could use LOCALE_SISO639LANGNAME, but it's not in Win95. // Could use LOCALE_SISO639LANGNAME, but it's not in Win95.
// LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use // LOCALE_SABBREVLANGNAME gives us three letters, like "enu", we use
@@ -427,6 +425,16 @@ set_init_1(int clean_arg)
// Moved to os_mac_conv.c to avoid dependency problems. // Moved to os_mac_conv.c to avoid dependency problems.
mac_lang_init(); mac_lang_init();
#endif #endif
}
/*
* Initialize the 'encoding' option to a default value.
*/
static void
set_init_default_encoding(void)
{
char_u *p;
int opt_idx;
# ifdef MSWIN # ifdef MSWIN
// MS-Windows has builtin support for conversion to and from Unicode, using // MS-Windows has builtin support for conversion to and from Unicode, using
@@ -437,13 +445,12 @@ set_init_1(int clean_arg)
// This works best for properly configured systems, old and new. // This works best for properly configured systems, old and new.
p = enc_locale(); p = enc_locale();
# endif # endif
if (p != NULL) if (p == NULL)
{ return;
char_u *save_enc;
// Try setting 'encoding' and check if the value is valid. // Try setting 'encoding' and check if the value is valid.
// If not, go back to the default encoding. // If not, go back to the default encoding.
save_enc = p_enc; char_u *save_enc = p_enc;
p_enc = p; p_enc = p;
if (STRCMP(p_enc, "gb18030") == 0) if (STRCMP(p_enc, "gb18030") == 0)
{ {
@@ -525,8 +532,97 @@ set_init_1(int clean_arg)
vim_free(p_enc); vim_free(p_enc);
p_enc = save_enc; p_enc = save_enc;
} }
} }
/*
* Initialize the options, first part.
*
* Called only once from main(), just after creating the first buffer.
* If "clean_arg" is TRUE Vim was started with --clean.
*/
void
set_init_1(int clean_arg)
{
#ifdef FEAT_LANGMAP
langmap_init();
#endif
// Be Vi compatible by default
p_cp = TRUE;
// Use POSIX compatibility when $VIM_POSIX is set.
if (mch_getenv((char_u *)"VIM_POSIX") != NULL)
{
set_string_default("cpo", (char_u *)CPO_ALL);
set_string_default("shm", (char_u *)SHM_POSIX);
}
set_init_default_shell();
set_init_default_backupskip();
set_init_default_maxmemtot();
set_init_default_cdpath();
set_init_default_printencoding();
#ifdef FEAT_POSTSCRIPT
set_init_default_printexpr();
#endif
/*
* Set all the options (except the terminal options) to their default
* value. Also set the global value for local options.
*/
set_options_default(0);
#ifdef UNIX
set_init_restricted_mode();
#endif
#ifdef CLEAN_RUNTIMEPATH
if (clean_arg)
set_init_clean_rtp();
#endif
#ifdef FEAT_GUI
if (found_reverse_arg)
set_option_value_give_err((char_u *)"bg", 0L, (char_u *)"dark", 0);
#endif
curbuf->b_p_initialized = TRUE;
curbuf->b_p_ar = -1; // no local 'autoread' value
curbuf->b_p_ul = NO_LOCAL_UNDOLEVEL;
check_buf_options(curbuf);
check_win_options(curwin);
check_options();
// Must be before option_expand(), because that one needs vim_isIDc()
didset_options();
#ifdef FEAT_SPELL
// Use the current chartab for the generic chartab. This is not in
// didset_options() because it only depends on 'encoding'.
init_spell_chartab();
#endif
// Expand environment variables and things like "~" for the defaults.
set_init_expand_env();
save_file_ff(curbuf); // Buffer is unchanged
#if defined(FEAT_ARABIC)
// Detect use of mlterm.
// Mlterm is a terminal emulator akin to xterm that has some special
// abilities (bidi namely).
// NOTE: mlterm's author is being asked to 'set' a variable
// instead of an environment variable due to inheritance.
if (mch_getenv((char_u *)"MLTERM") != NULL)
set_option_value_give_err((char_u *)"tbidi", 1L, NULL, 0);
#endif
didset_options2();
set_init_lang_env();
set_init_default_encoding();
#ifdef FEAT_MULTI_LANG #ifdef FEAT_MULTI_LANG
// Set the default for 'helplang'. // Set the default for 'helplang'.
set_helplang_default(get_mess_lang()); set_helplang_default(get_mess_lang());

View File

@@ -695,6 +695,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
1295,
/**/ /**/
1294, 1294,
/**/ /**/