📄 option.c
字号:
check_options();
/*
* initialize the table for 'iskeyword' et.al.
* Must be before option_expand(), because that one needs vim_isIDc()
*/
init_chartab();
/*
* initialize the table for 'breakat'.
*/
fill_breakat_flags();
/*
* 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.
*/
for (opt_idx = 0; !istermoption(&options[opt_idx]); opt_idx++)
{
p = option_expand(opt_idx);
if (p != NULL)
{
*(char_u **)options[opt_idx].var = p;
/* VIMEXP
* Defaults for all expanded options are currently the same for Vi
* and Vim. When this changes, add some code here! Also need to
* split P_DEF_ALLOCED in two.
*/
options[opt_idx].def_val[VI_DEFAULT] = p;
options[opt_idx].flags |= P_DEF_ALLOCED;
}
}
/* Initialize the highlight_attr[] table. */
highlight_changed();
curbuf->b_start_ffc = *curbuf->b_p_ff; /* Buffer is unchanged */
/* Parse default for 'wildmode' */
check_opt_wim();
}
/*
* Set an option to its default value.
*/
static void
set_option_default(opt_idx, dofree)
int opt_idx;
int dofree; /* TRUE when old value can be freed */
{
char_u *varp; /* pointer to variable for current option */
int dvi; /* index in def_val[] */
int flags;
varp = get_varp(&(options[opt_idx]));
flags = options[opt_idx].flags;
if (varp != NULL) /* nothing to do for hidden option */
{
if ((flags & P_VI_DEF) || p_cp)
dvi = VI_DEFAULT;
else
dvi = VIM_DEFAULT;
if (flags & P_STRING)
{
/* indirect options are always in allocated memory */
if (flags & P_IND)
set_string_option_direct(NULL, opt_idx,
options[opt_idx].def_val[dvi], dofree);
else
{
if (dofree && (flags & P_ALLOCED))
free_string_option(*(char_u **)(varp));
*(char_u **)varp = options[opt_idx].def_val[dvi];
options[opt_idx].flags &= ~P_ALLOCED;
}
}
else if (flags & P_NUM)
*(long *)varp = (long)options[opt_idx].def_val[dvi];
else /* P_BOOL */
/* the cast to long is required for Manx C */
*(int *)varp = (int)(long)options[opt_idx].def_val[dvi];
}
}
/*
* Set all options (except terminal options) to their default value.
*/
static void
set_options_default(dofree)
int dofree; /* may free old value */
{
int i;
for (i = 0; !istermoption(&options[i]); i++)
if (!(options[i].flags & P_NODEFAULT))
set_option_default(i, dofree);
}
/*
* Set the Vi-default value of a string option.
* Used for 'sh' and 'term'.
*/
void
set_string_default(name, val)
char *name;
char_u *val;
{
char_u *p;
int opt_idx;
p = vim_strsave(val);
if (p != NULL) /* we don't want a NULL */
{
opt_idx = findoption((char_u *)name);
if (options[opt_idx].flags & P_DEF_ALLOCED)
vim_free(options[opt_idx].def_val[VI_DEFAULT]);
options[opt_idx].def_val[VI_DEFAULT] = p;
options[opt_idx].flags |= P_DEF_ALLOCED;
}
}
/*
* Set the Vi-default value of a number option.
* Used for 'lines' and 'columns'.
*/
void
set_number_default(name, val)
char *name;
long val;
{
options[findoption((char_u *)name)].def_val[VI_DEFAULT] = (char_u *)val;
}
/*
* Initialize the options, part two: After getting Rows and Columns and
* setting 'term'.
*/
void
set_init_2()
{
/*
* 'scroll' defaults to half the window height. Note that this default is
* wrong when the window height changes.
*/
options[findoption((char_u *)"scroll")].def_val[VI_DEFAULT]
= (char_u *)((long_u)Rows >> 1);
comp_col();
#if !((defined(MSDOS) || defined(OS2) || defined(WIN32)) && !defined(USE_GUI))
{
int idx4;
/*
* Try guessing the value of 'background', depending on the terminal
* name. Only need to check for terminals with a dark background,
* that can handle color.
*/
idx4 = findoption((char_u *)"bg");
if (!(options[idx4].flags & P_WAS_SET))
{
if (STRCMP(T_NAME, "linux") == 0) /* linux console */
set_string_option_direct(NULL, idx4, (char_u *)"dark", TRUE);
}
}
#endif
}
/*
* Initialize the options, part three: After reading the .vimrc
*/
void
set_init_3()
{
#if defined(UNIX) || defined(OS2)
/*
* Set 'shellpipe' and 'shellredir', depending on the 'shell' option.
* This is done after other initializations, where 'shell' might have been
* set, but only if they have not been set before.
*/
char_u *p;
int idx1;
int idx2;
int do_sp;
int do_srr;
idx1 = findoption((char_u *)"sp");
idx2 = findoption((char_u *)"srr");
do_sp = !(options[idx1].flags & P_WAS_SET);
do_srr = !(options[idx2].flags & P_WAS_SET);
/*
* Isolate the name of the shell:
* - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
* - Remove any argument. E.g., "csh -f" -> "csh".
*/
p = gettail(p_sh);
p = vim_strnsave(p, skiptowhite(p) - p);
if (p != NULL)
{
/*
* Default for p_sp is "| tee", for p_srr is ">".
* For known shells it is changed here to include stderr.
*/
if ( fnamecmp(p, "csh") == 0
|| fnamecmp(p, "tcsh") == 0
# ifdef OS2 /* also check with .exe extension */
|| fnamecmp(p, "csh.exe") == 0
|| fnamecmp(p, "tcsh.exe") == 0
# endif
)
{
if (do_sp)
{
p_sp = (char_u *)"|& tee";
options[idx1].def_val[VI_DEFAULT] = p_sp;
}
if (do_srr)
{
p_srr = (char_u *)">&";
options[idx2].def_val[VI_DEFAULT] = p_srr;
}
}
else
# ifndef OS2 /* Always use bourne shell style redirection if we reach this */
if ( STRCMP(p, "sh") == 0
|| STRCMP(p, "ksh") == 0
|| STRCMP(p, "zsh") == 0
|| STRCMP(p, "bash") == 0)
# endif
{
if (do_sp)
{
p_sp = (char_u *)"2>&1| tee";
options[idx1].def_val[VI_DEFAULT] = p_sp;
}
if (do_srr)
{
p_srr = (char_u *)">%s 2>&1";
options[idx2].def_val[VI_DEFAULT] = p_srr;
}
}
vim_free(p);
}
#endif
#if defined(MSDOS) || defined(WIN32) || defined(OS2)
/*
* Set 'shellcmdflag and 'shellquote' depending on the 'shell' option.
* This is done after other initializations, where 'shell' might have been
* set, but only if they have not been set before. Default for p_shcf is
* "/c", for p_shq is "". For "sh" like shells it is changed here to
* "-c" and "\"", but not for DJGPP, because it starts the shell without
* command.com. And for Win32 we need to set p_sxq instead.
*/
if (strstr((char *)p_sh, "sh") != NULL)
{
int idx3;
idx3 = findoption((char_u *)"shcf");
if (!(options[idx3].flags & P_WAS_SET))
{
p_shcf = (char_u *)"-c";
options[idx3].def_val[VI_DEFAULT] = p_shcf;
}
# ifndef DJGPP
# ifdef WIN32
/* Somehow Win32 requires the quotes around the redirection too */
idx3 = findoption((char_u *)"sxq");
if (!(options[idx3].flags & P_WAS_SET))
{
p_sxq = (char_u *)"\"";
options[idx3].def_val[VI_DEFAULT] = p_sxq;
}
# else
idx3 = findoption((char_u *)"shq");
if (!(options[idx3].flags & P_WAS_SET))
{
p_shq = (char_u *)"\"";
options[idx3].def_val[VI_DEFAULT] = p_shq;
}
# endif
# endif
}
#endif
set_title_defaults();
}
#ifdef USE_GUI
/*
* Option initializations that can only be done after opening the GUI window.
*/
void
init_gui_options()
{
int tt;
/* Set the 'background' option according to the lightness of the
* background color. */
if (!option_was_set((char_u *)"bg")
&& gui_mch_get_lightness(gui.back_pixel) < 127)
{
/* Full_screen must be TRUE to get the side effect of changing the
* defaults for the highlighting. Restore it just in case. */
tt = full_screen;
full_screen = TRUE;
set_option_value((char_u *)"bg", 0L, (char_u *)"dark");
highlight_changed();
full_screen = tt;
}
}
#endif
/*
* 'title' and 'icon' only default to true if they have not been set or reset
* in .vimrc and we can read the old value.
* When 'title' and 'icon' have been reset in .vimrc, we won't even check if
* they can be reset. This reduces startup time when using X on a remote
* machine.
*/
void
set_title_defaults()
{
int idx1;
long val;
idx1 = findoption((char_u *)"title");
if (!(options[idx1].flags & P_WAS_SET))
{
val = ui_can_restore_title();
options[idx1].def_val[VI_DEFAULT] = (char_u *)val;
p_title = val;
}
idx1 = findoption((char_u *)"icon");
if (!(options[idx1].flags & P_WAS_SET))
{
val = ui_can_restore_icon();
options[idx1].def_val[VI_DEFAULT] = (char_u *)val;
p_icon = val;
}
}
/*
* Parse 'arg' for option settings.
*
* 'arg' may be IObuff, but only when no errors can be present and option
* does not need to be expanded with option_expand().
*
* returns FAIL if an error is detected, OK otherwise
*/
int
do_set(arg)
char_u *arg; /* option string (may be written to!) */
{
int opt_idx;
char_u *errmsg;
char_u errbuf[80];
char_u *startarg;
int prefix; /* 1: nothing, 0: "no", 2: "inv" in front of name */
int nextchar; /* next non-white char after option name */
int afterchar; /* character just after option name */
int len;
int i;
long value;
int key;
int flags; /* flags for current option */
char_u *varp = NULL; /* pointer to variable for current option */
int did_show = FALSE; /* already showed one value */
int adding; /* "opt+=arg" */
int prepending; /* "opt^=arg" */
int removing; /* "opt-=arg" */
if (*arg == NUL)
{
showoptions(0);
return OK;
}
while (*arg) /* loop to process all options */
{
errmsg = NULL;
startarg = arg; /* remember for error message */
if (STRNCMP(arg, "all", 3) == 0 && !isalpha(arg[3]))
{
/*
* ":set all" show all options.
* ":set all&" set all options to their default value.
*/
arg += 3;
if (*arg == '&')
{
++arg;
set_options_default(TRUE);
}
else
showoptions(1);
}
else if (STRNCMP(arg, "termcap", 7) == 0)
{
showoptions(2);
show_termcodes();
arg += 7;
}
else
{
prefix = 1;
if (STRNCMP(arg, "no", 2) == 0)
{
prefix = 0;
arg += 2;
}
else if (STRNCMP(arg, "inv", 3) == 0)
{
prefix = 2;
arg += 3;
}
/* find end of name */
key = 0;
if (*arg == '<')
{
opt_idx = -1;
/* look out for <t_>;> */
if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4])
len = 5;
else
{
len = 1;
while (arg[len] != NUL && arg[len] != '>')
++len;
}
if (arg[len] != '>')
{
errmsg = e_invarg;
goto skip;
}
arg[len] = NUL; /* put NUL after name */
if (arg[1] == 't' && arg[2] == '_') /* could be term code */
opt_idx = findoption(arg + 1);
arg[len++] = '>'; /* restore '>' */
if (opt_idx == -1)
key = find_key_option(arg + 1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -