diff options
author | Josh Rahm <joshuarahm@gmail.com> | 2023-11-29 22:40:31 +0000 |
---|---|---|
committer | Josh Rahm <joshuarahm@gmail.com> | 2023-11-29 22:40:31 +0000 |
commit | 339e2d15cc26fe86988ea06468d912a46c8d6f29 (patch) | |
tree | a6167fc8fcfc6ae2dc102f57b2473858eac34063 /src/nvim/option.c | |
parent | 067dc73729267c0262438a6fdd66e586f8496946 (diff) | |
parent | 4a8bf24ac690004aedf5540fa440e788459e5e34 (diff) | |
download | rneovim-339e2d15cc26fe86988ea06468d912a46c8d6f29.tar.gz rneovim-339e2d15cc26fe86988ea06468d912a46c8d6f29.tar.bz2 rneovim-339e2d15cc26fe86988ea06468d912a46c8d6f29.zip |
Merge remote-tracking branch 'upstream/master' into fix_repeatcmdline
Diffstat (limited to 'src/nvim/option.c')
-rw-r--r-- | src/nvim/option.c | 5333 |
1 files changed, 3027 insertions, 2306 deletions
diff --git a/src/nvim/option.c b/src/nvim/option.c index 01a5c7677f..96d6d8e01e 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -1,6 +1,3 @@ -// This is an open source non-commercial project. Dear PVS-Studio, please check -// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com - // User-settable options. Checklist for adding a new option: // - Put it in options.lua // - For a global option: Add a variable for it in option_defs.h. @@ -13,15 +10,13 @@ // add some code to didset_window_options(). // - For a buffer option, add some code to buf_copy_options(). // - For a buffer string option, add code to check_buf_options(). -// - If it's a numeric option, add any necessary bounds checks to -// set_num_option(). +// - If it's a numeric option, add any necessary bounds checks to check_num_option_bounds(). // - If it's a list of flags, add some code in do_set(), search for WW_ALL. // - Add documentation! doc/options.txt, and any other related places. // - Add an entry in runtime/optwin.vim. #define IN_OPTION_C #include <assert.h> -#include <ctype.h> #include <inttypes.h> #include <limits.h> #include <stdbool.h> @@ -30,38 +25,43 @@ #include <string.h> #include "auto/config.h" +#include "klib/kvec.h" +#include "nvim/api/extmark.h" #include "nvim/api/private/defs.h" -#include "nvim/ascii.h" +#include "nvim/api/private/helpers.h" +#include "nvim/api/private/validate.h" +#include "nvim/ascii_defs.h" #include "nvim/autocmd.h" #include "nvim/buffer.h" #include "nvim/change.h" #include "nvim/charset.h" #include "nvim/cmdexpand.h" +#include "nvim/cmdexpand_defs.h" #include "nvim/cursor_shape.h" #include "nvim/decoration_provider.h" #include "nvim/diff.h" #include "nvim/drawscreen.h" #include "nvim/eval.h" #include "nvim/eval/typval.h" -#include "nvim/eval/typval_defs.h" +#include "nvim/eval/vars.h" #include "nvim/ex_cmds_defs.h" #include "nvim/ex_docmd.h" #include "nvim/ex_getln.h" #include "nvim/ex_session.h" #include "nvim/fold.h" +#include "nvim/func_attr.h" #include "nvim/garray.h" #include "nvim/gettext.h" #include "nvim/globals.h" -#include "nvim/grid_defs.h" #include "nvim/highlight.h" #include "nvim/highlight_group.h" #include "nvim/indent.h" #include "nvim/indent_c.h" #include "nvim/insexpand.h" #include "nvim/keycodes.h" -#include "nvim/locale.h" #include "nvim/log.h" -#include "nvim/macros.h" +#include "nvim/lua/executor.h" +#include "nvim/macros_defs.h" #include "nvim/mapping.h" #include "nvim/mbyte.h" #include "nvim/memfile.h" @@ -74,47 +74,46 @@ #include "nvim/ops.h" #include "nvim/option.h" #include "nvim/option_defs.h" +#include "nvim/option_vars.h" #include "nvim/optionstr.h" +#include "nvim/os/input.h" +#include "nvim/os/lang.h" #include "nvim/os/os.h" #include "nvim/path.h" #include "nvim/popupmenu.h" -#include "nvim/pos.h" +#include "nvim/pos_defs.h" #include "nvim/regexp.h" #include "nvim/runtime.h" -#include "nvim/screen.h" #include "nvim/search.h" #include "nvim/sign_defs.h" #include "nvim/spell.h" #include "nvim/spellfile.h" #include "nvim/spellsuggest.h" +#include "nvim/state_defs.h" #include "nvim/strings.h" #include "nvim/tag.h" #include "nvim/terminal.h" -#include "nvim/types.h" +#include "nvim/types_defs.h" #include "nvim/ui.h" #include "nvim/undo.h" -#include "nvim/vim.h" +#include "nvim/vim_defs.h" #include "nvim/window.h" -#ifdef MSWIN -# include "nvim/os/pty_conpty_win.h" + +#ifdef BACKSLASH_IN_FILENAME +# include "nvim/arglist.h" #endif -#include "nvim/api/extmark.h" -#include "nvim/api/private/helpers.h" -#include "nvim/lua/executor.h" -#include "nvim/os/input.h" -#include "nvim/os/lang.h" -static char e_unknown_option[] +static const char e_unknown_option[] = N_("E518: Unknown option"); -static char e_not_allowed_in_modeline[] +static const char e_not_allowed_in_modeline[] = N_("E520: Not allowed in a modeline"); -static char e_not_allowed_in_modeline_when_modelineexpr_is_off[] +static const char e_not_allowed_in_modeline_when_modelineexpr_is_off[] = N_("E992: Not allowed in a modeline when 'modelineexpr' is off"); -static char e_key_code_not_set[] +static const char e_key_code_not_set[] = N_("E846: Key code not set"); -static char e_number_required_after_equal[] +static const char e_number_required_after_equal[] = N_("E521: Number required after ="); -static char e_preview_window_already_exists[] +static const char e_preview_window_already_exists[] = N_("E590: A preview window already exists"); static char *p_term = NULL; @@ -123,17 +122,30 @@ static char *p_ttytype = NULL; // Saved values for when 'bin' is set. static int p_et_nobin; static int p_ml_nobin; -static long p_tw_nobin; -static long p_wm_nobin; +static OptInt p_tw_nobin; +static OptInt p_wm_nobin; // Saved values for when 'paste' is set. static int p_ai_nopaste; static int p_et_nopaste; -static long p_sts_nopaste; -static long p_tw_nopaste; -static long p_wm_nopaste; +static OptInt p_sts_nopaste; +static OptInt p_tw_nopaste; +static OptInt p_wm_nopaste; static char *p_vsts_nopaste; +#define OPTION_COUNT ARRAY_SIZE(options) + +/// :set boolean option prefix +typedef enum { + PREFIX_NO = 0, ///< "no" prefix + PREFIX_NONE, ///< no prefix + PREFIX_INV, ///< "inv" prefix +} set_prefix_T; + +#ifdef INCLUDE_GENERATED_DECLARATIONS +# include "option.c.generated.h" +#endif + // options[] is initialized here. // The order of the options MUST be alphabetic for ":set all" and findoption(). // All option names MUST start with a lowercase letter (for findoption()). @@ -145,141 +157,190 @@ static char *p_vsts_nopaste; # include "options.generated.h" #endif -#define OPTION_COUNT ARRAY_SIZE(options) - -typedef enum { - OP_NONE = 0, - OP_ADDING, ///< "opt+=arg" - OP_PREPENDING, ///< "opt^=arg" - OP_REMOVING, ///< "opt-=arg" -} set_op_T; - -#ifdef INCLUDE_GENERATED_DECLARATIONS -# include "option.c.generated.h" -#endif +static char *(p_bin_dep_opts[]) = { + "textwidth", "wrapmargin", "modeline", "expandtab", NULL +}; +static char *(p_paste_dep_opts[]) = { + "autoindent", "expandtab", "ruler", "showmatch", "smarttab", + "softtabstop", "textwidth", "wrapmargin", "revins", "varsofttabstop", NULL +}; void set_init_tablocal(void) { // susy baka: cmdheight calls itself OPT_GLOBAL but is really tablocal! int ch_idx = findoption("cmdheight"); - p_ch = (long)options[ch_idx].def_val; + p_ch = (OptInt)(intptr_t)options[ch_idx].def_val; } -/// 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) +/// Initialize the 'shell' option to a default value. +static void set_init_default_shell(void) { - langmap_init(); - // Find default value for 'shell' option. // Don't use it if it is empty. - { - const char *shell = os_getenv("SHELL"); - if (shell != NULL) { - if (vim_strchr(shell, ' ') != NULL) { - const size_t len = strlen(shell) + 3; // two quotes and a trailing NUL - char *const cmd = xmalloc(len); - snprintf(cmd, len, "\"%s\"", shell); - set_string_default("sh", cmd, true); - } else { - set_string_default("sh", (char *)shell, false); - } + const char *shell = os_getenv("SHELL"); + if (shell != NULL) { + if (vim_strchr(shell, ' ') != NULL) { + const size_t len = strlen(shell) + 3; // two quotes and a trailing NUL + char *const cmd = xmalloc(len); + snprintf(cmd, len, "\"%s\"", shell); + set_string_default("sh", cmd, true); + } else { + set_string_default("sh", (char *)shell, false); } } +} - // Set the default for 'backupskip' to include environment variables for - // temp files. - { +/// Set the default for 'backupskip' to include environment variables for +/// temp files. +static void set_init_default_backupskip(void) +{ #ifdef UNIX - static char *(names[4]) = { "", "TMPDIR", "TEMP", "TMP" }; + static char *(names[4]) = { "", "TMPDIR", "TEMP", "TMP" }; #else - static char *(names[3]) = { "TMPDIR", "TEMP", "TMP" }; + static char *(names[3]) = { "TMPDIR", "TEMP", "TMP" }; #endif - garray_T ga; - int opt_idx = findoption("backupskip"); + garray_T ga; + int opt_idx = findoption("backupskip"); - ga_init(&ga, 1, 100); - for (size_t n = 0; n < ARRAY_SIZE(names); n++) { - bool mustfree = true; - char *p; + ga_init(&ga, 1, 100); + for (size_t n = 0; n < ARRAY_SIZE(names); n++) { + bool mustfree = true; + char *p; #ifdef UNIX - if (*names[n] == NUL) { + if (*names[n] == NUL) { # ifdef __APPLE__ - p = "/private/tmp"; + p = "/private/tmp"; # else - p = "/tmp"; + p = "/tmp"; # endif - mustfree = false; - } else // NOLINT(readability/braces) + mustfree = false; + } else // NOLINT(readability/braces) #endif - { - p = vim_getenv(names[n]); - } - if (p != NULL && *p != NUL) { - // First time count the NUL, otherwise count the ','. - const size_t len = strlen(p) + 3; - char *item = xmalloc(len); - xstrlcpy(item, p, len); - add_pathsep(item); - xstrlcat(item, "*", len); - if (find_dup_item(ga.ga_data, item, options[opt_idx].flags) - == NULL) { - ga_grow(&ga, (int)len); - if (!GA_EMPTY(&ga)) { - STRCAT(ga.ga_data, ","); - } - STRCAT(ga.ga_data, p); - add_pathsep(ga.ga_data); - STRCAT(ga.ga_data, "*"); - ga.ga_len += (int)len; + { + p = vim_getenv(names[n]); + } + if (p != NULL && *p != NUL) { + // First time count the NUL, otherwise count the ','. + const size_t len = strlen(p) + 3; + char *item = xmalloc(len); + xstrlcpy(item, p, len); + add_pathsep(item); + xstrlcat(item, "*", len); + if (find_dup_item(ga.ga_data, item, options[opt_idx].flags) + == NULL) { + ga_grow(&ga, (int)len); + if (!GA_EMPTY(&ga)) { + STRCAT(ga.ga_data, ","); } - xfree(item); - } - if (mustfree) { - xfree(p); + STRCAT(ga.ga_data, p); + add_pathsep(ga.ga_data); + STRCAT(ga.ga_data, "*"); + ga.ga_len += (int)len; } + xfree(item); + } + if (mustfree) { + xfree(p); } - if (ga.ga_data != NULL) { - set_string_default("bsk", ga.ga_data, true); + } + if (ga.ga_data != NULL) { + set_string_default("bsk", ga.ga_data, true); + } +} + +/// Initialize the 'cdpath' option to a default value. +static void set_init_default_cdpath(void) +{ + char *cdpath = vim_getenv("CDPATH"); + if (cdpath == NULL) { + return; + } + + char *buf = xmalloc(2 * strlen(cdpath) + 2); + buf[0] = ','; // start with ",", current dir first + int j = 1; + for (int i = 0; cdpath[i] != NUL; i++) { + if (vim_ispathlistsep(cdpath[i])) { + buf[j++] = ','; + } else { + if (cdpath[i] == ' ' || cdpath[i] == ',') { + buf[j++] = '\\'; + } + buf[j++] = cdpath[i]; } } + buf[j] = NUL; + int opt_idx = findoption("cdpath"); + if (opt_idx >= 0) { + options[opt_idx].def_val = buf; + options[opt_idx].flags |= P_DEF_ALLOCED; + } else { + xfree(buf); // cannot happen + } + xfree(cdpath); +} - { - // Initialize the 'cdpath' option's default value. - char *cdpath = vim_getenv("CDPATH"); - if (cdpath != NULL) { - char *buf = xmalloc(2 * strlen(cdpath) + 2); - { - buf[0] = ','; // start with ",", current dir first - int j = 1; - for (int i = 0; cdpath[i] != NUL; i++) { - if (vim_ispathlistsep(cdpath[i])) { - buf[j++] = ','; - } else { - if (cdpath[i] == ' ' || cdpath[i] == ',') { - buf[j++] = '\\'; - } - buf[j++] = cdpath[i]; - } - } - buf[j] = NUL; - int opt_idx = findoption("cdpath"); - if (opt_idx >= 0) { - options[opt_idx].def_val = buf; - options[opt_idx].flags |= P_DEF_ALLOCED; - } else { - xfree(buf); // cannot happen - } +/// 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); } - xfree(cdpath); + 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); const size_t backupdir_len = strlen(backupdir); @@ -328,33 +389,7 @@ void set_init_1(bool clean_arg) init_spell_chartab(); // 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 (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; - } - } + set_init_expand_env(); save_file_ff(curbuf); // Buffer is unchanged @@ -364,22 +399,13 @@ void set_init_1(bool clean_arg) // NOTE: mlterm's author is being asked to 'set' a variable // instead of an environment variable due to inheritance. if (os_env_exists("MLTERM")) { - set_option_value_give_err("tbidi", 1L, NULL, 0); + set_option_value_give_err("tbidi", BOOLEAN_OPTVAL(true), 0); } didset_options2(); lang_init(); - - // 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; + set_init_fenc_default(); #ifdef HAVE_WORKING_LIBINTL // GNU gettext 0.10.37 supports this feature: set the codeset used for @@ -401,7 +427,7 @@ static void set_option_default(const int opt_idx, int opt_flags) // pointer to variable for current option vimoption_T *opt = &options[opt_idx]; - char_u *varp = (char_u *)get_varp_scope(opt, both ? OPT_LOCAL : opt_flags); + void *varp = get_varp_scope(opt, both ? OPT_LOCAL : opt_flags); uint32_t flags = opt->flags; if (varp != NULL) { // skip hidden option, nothing to do for it if (flags & P_STRING) { @@ -420,19 +446,18 @@ static void set_option_default(const int opt_idx, int opt_flags) if (opt->indir == PV_SCROLL) { win_comp_scroll(curwin); } else { - long def_val = (long)opt->def_val; - if ((long *)varp == &curwin->w_p_so - || (long *)varp == &curwin->w_p_siso) { + OptInt def_val = (OptInt)(intptr_t)opt->def_val; + if ((OptInt *)varp == &curwin->w_p_so + || (OptInt *)varp == &curwin->w_p_siso) { // 'scrolloff' and 'sidescrolloff' local values have a // different default value than the global default. - *(long *)varp = -1; + *(OptInt *)varp = -1; } else { - *(long *)varp = def_val; + *(OptInt *)varp = def_val; } // May also set global value for local option. if (both) { - *(long *)get_varp_scope(opt, OPT_GLOBAL) = - def_val; + *(OptInt *)get_varp_scope(opt, OPT_GLOBAL) = def_val; } } } else { // P_BOOL @@ -531,11 +556,11 @@ static char *find_dup_item(char *origval, const char *newval, uint32_t flags) /// Set the Vi-default value of a number option. /// Used for 'lines' and 'columns'. -void set_number_default(char *name, long val) +void set_number_default(char *name, OptInt val) { int opt_idx = findoption(name); if (opt_idx >= 0) { - options[opt_idx].def_val = (char *)(intptr_t)val; + options[opt_idx].def_val = (void *)(intptr_t)val; } } @@ -547,14 +572,14 @@ void free_all_options(void) if (options[i].indir == PV_NONE) { // global option: free value and default value. if ((options[i].flags & P_ALLOCED) && options[i].var != NULL) { - free_string_option(*(char **)options[i].var); + optval_free(optval_from_varp(i, options[i].var)); } if (options[i].flags & P_DEF_ALLOCED) { - free_string_option(options[i].def_val); + optval_free(optval_from_varp(i, &options[i].def_val)); } - } else if (options[i].var != VAR_WIN && (options[i].flags & P_STRING)) { + } else if (options[i].var != VAR_WIN) { // buffer-local option: free global value - clear_string_option((char **)options[i].var); + optval_free(optval_from_varp(i, options[i].var)); } } free_operatorfunc_option(); @@ -594,16 +619,16 @@ void set_init_3(void) // set, but only if they have not been set before. int idx_srr = findoption("srr"); int do_srr = (idx_srr < 0) - ? false - : !(options[idx_srr].flags & P_WAS_SET); + ? false + : !(options[idx_srr].flags & P_WAS_SET); int idx_sp = findoption("sp"); int do_sp = (idx_sp < 0) - ? false - : !(options[idx_sp].flags & P_WAS_SET); + ? false + : !(options[idx_sp].flags & P_WAS_SET); size_t len = 0; char *p = (char *)invocation_path_tail(p_sh, &len); - p = xstrnsave(p, len); + p = xmemdupz(p, len); { // @@ -726,69 +751,187 @@ void ex_set(exarg_T *eap) (void)do_set(eap->arg, flags); } -/// Part of do_set() for string options. -/// @return FAIL on failure, do not process further options. -static int do_set_string(int opt_idx, int opt_flags, char **argp, int nextchar, set_op_T op_arg, - uint32_t flags, char *varp_arg, char *errbuf, size_t errbuflen, - int *value_checked, char **errmsg) +/// Get the default value for a string option. +static char *stropt_get_default_val(int opt_idx, uint64_t flags) +{ + char *newval = options[opt_idx].def_val; + // expand environment variables and ~ since the default value was + // already expanded, only required when an environment variable was set + // later + if (newval == NULL) { + newval = empty_string_option; + } else if (!(options[opt_idx].flags & P_NO_DEF_EXP)) { + char *s = option_expand(opt_idx, newval); + if (s == NULL) { + s = newval; + } + newval = xstrdup(s); + } else { + newval = xstrdup(newval); + } + return newval; +} + +/// Copy the new string value into allocated memory for the option. +/// Can't use set_string_option_direct(), because we need to remove the +/// backslashes. +static char *stropt_copy_value(char *origval, char **argp, set_op_T op, + uint32_t flags FUNC_ATTR_UNUSED) { char *arg = *argp; - set_op_T op = op_arg; - char *varp = varp_arg; - char *save_arg = NULL; - char *s = NULL; - char_u *origval_l = NULL; - char_u *origval_g = NULL; - char whichwrap[80]; - // When using ":set opt=val" for a global option - // with a local value the local value will be - // reset, use the global value here. - if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0 - && ((int)options[opt_idx].indir & PV_BOTH)) { - varp = (char *)options[opt_idx].var; + // get a bit too much + size_t newlen = strlen(arg) + 1; + if (op != OP_NONE) { + newlen += strlen(origval) + 1; + } + char *newval = xmalloc(newlen); + char *s = newval; + + // Copy the string, skip over escaped chars. + // For MS-Windows backslashes before normal file name characters + // are not removed, and keep backslash at start, for "\\machine\path", + // but do remove it for "\\\\machine\\path". + // The reverse is found in escape_option_str_cmdline(). + while (*arg != NUL && !ascii_iswhite(*arg)) { + if (*arg == '\\' && arg[1] != NUL +#ifdef BACKSLASH_IN_FILENAME + && !((flags & P_EXPAND) + && vim_isfilec((uint8_t)arg[1]) + && !ascii_iswhite(arg[1]) + && (arg[1] != '\\' + || (s == newval && arg[2] != '\\'))) +#endif + ) { + arg++; // remove backslash + } + int i = utfc_ptr2len(arg); + if (i > 1) { + // copy multibyte char + memmove(s, arg, (size_t)i); + arg += i; + s += i; + } else { + *s++ = *arg++; + } } + *s = NUL; - // The old value is kept until we are sure that the new value is valid. - char *oldval = *(char **)varp; + *argp = arg; + return newval; +} - if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0) { - origval_l = *(char_u **)get_varp_scope(&(options[opt_idx]), OPT_LOCAL); - origval_g = *(char_u **)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL); +/// Expand environment variables and ~ in string option value 'newval'. +static char *stropt_expand_envvar(int opt_idx, char *origval, char *newval, set_op_T op) +{ + char *s = option_expand(opt_idx, newval); + if (s == NULL) { + return newval; + } - // A global-local string option might have an empty option as value to - // indicate that the global value should be used. - if (((int)options[opt_idx].indir & PV_BOTH) && origval_l == (char_u *)empty_option) { - origval_l = origval_g; - } + xfree(newval); + uint32_t newlen = (unsigned)strlen(s) + 1; + if (op != OP_NONE) { + newlen += (unsigned)strlen(origval) + 1; } + newval = xmalloc(newlen); + STRCPY(newval, s); - char *origval; - // When setting the local value of a global option, the old value may be - // the global value. - if (((int)options[opt_idx].indir & PV_BOTH) && (opt_flags & OPT_LOCAL)) { - origval = *(char **)get_varp(&options[opt_idx]); + return newval; +} + +/// Concatenate the original and new values of a string option, adding a "," if +/// needed. +static void stropt_concat_with_comma(char *origval, char *newval, set_op_T op, uint32_t flags) +{ + int len = 0; + int comma = ((flags & P_COMMA) && *origval != NUL && *newval != NUL); + if (op == OP_ADDING) { + len = (int)strlen(origval); + // Strip a trailing comma, would get 2. + if (comma && len > 1 + && (flags & P_ONECOMMA) == P_ONECOMMA + && origval[len - 1] == ',' + && origval[len - 2] != '\\') { + len--; + } + memmove(newval + len + comma, newval, strlen(newval) + 1); + memmove(newval, origval, (size_t)len); } else { - origval = oldval; + len = (int)strlen(newval); + STRMOVE(newval + len + comma, origval); } + if (comma) { + newval[len] = ','; + } +} - char *newval; - if (nextchar == '&') { // set to default val - newval = options[opt_idx].def_val; - // expand environment variables and ~ since the default value was - // already expanded, only required when an environment variable was set - // later - if (newval == NULL) { - newval = empty_option; - } else if (!(options[opt_idx].flags & P_NO_DEF_EXP)) { - s = option_expand(opt_idx, newval); - if (s == NULL) { - s = newval; +/// Remove a value from a string option. Copy string option value in "origval" +/// to "newval" and then remove the string "strval" of length "len". +static void stropt_remove_val(char *origval, char *newval, uint32_t flags, char *strval, int len) +{ + // Remove newval[] from origval[]. (Note: "len" has been set above + // and is used here). + STRCPY(newval, origval); + if (*strval) { + // may need to remove a comma + if (flags & P_COMMA) { + if (strval == origval) { + // include comma after string + if (strval[len] == ',') { + len++; + } + } else { + // include comma before string + strval--; + len++; + } + } + STRMOVE(newval + (strval - origval), strval + len); + } +} + +/// Remove flags that appear twice in the string option value 'newval'. +static void stropt_remove_dupflags(char *newval, uint32_t flags) +{ + char *s = newval; + // Remove flags that appear twice. + for (s = newval; *s;) { + // if options have P_FLAGLIST and P_ONECOMMA such as 'whichwrap' + if (flags & P_ONECOMMA) { + if (*s != ',' && *(s + 1) == ',' + && vim_strchr(s + 2, (uint8_t)(*s)) != NULL) { + // Remove the duplicated value and the next comma. + STRMOVE(s, s + 2); + continue; } - newval = xstrdup(s); } else { - newval = xstrdup(newval); + if ((!(flags & P_COMMA) || *s != ',') + && vim_strchr(s + 1, (uint8_t)(*s)) != NULL) { + STRMOVE(s, s + 1); + continue; + } } + s++; + } +} + +/// Get the string value specified for a ":set" command. The following set +/// options are supported: +/// set {opt}& +/// set {opt}< +/// set {opt}={val} +/// set {opt}:{val} +static char *stropt_get_newval(int nextchar, int opt_idx, char **argp, void *varp, char *origval, + set_op_T *op_arg, uint32_t flags) +{ + char *arg = *argp; + set_op_T op = *op_arg; + char *save_arg = NULL; + char *newval; + char *s = NULL; + if (nextchar == '&') { // set to default val + newval = stropt_get_default_val(opt_idx, flags); } else if (nextchar == '<') { // set to global val newval = xstrdup(*(char **)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL)); } else { @@ -796,123 +939,18 @@ static int do_set_string(int opt_idx, int opt_flags, char **argp, int nextchar, // Set 'keywordprg' to ":help" if an empty // value was passed to :set by the user. - if (varp == (char *)&p_kp && (*arg == NUL || *arg == ' ')) { + if (varp == &p_kp && (*arg == NUL || *arg == ' ')) { save_arg = arg; arg = ":help"; - } else if (varp == (char *)&p_bs && ascii_isdigit(**(char_u **)varp)) { - // Convert 'backspace' number to string, for - // adding, prepending and removing string. - int i = getdigits_int((char **)varp, true, 0); - switch (i) { - case 0: - *(char **)varp = empty_option; - break; - case 1: - *(char_u **)varp = (char_u *)xstrdup("indent,eol"); - break; - case 2: - *(char_u **)varp = (char_u *)xstrdup("indent,eol,start"); - break; - case 3: - *(char_u **)varp = (char_u *)xstrdup("indent,eol,nostop"); - break; - } - xfree(oldval); - if (origval == oldval) { - origval = *(char **)varp; - } - if (origval_l == (char_u *)oldval) { - origval_l = *(char_u **)varp; - } - if (origval_g == (char_u *)oldval) { - origval_g = *(char_u **)varp; - } - oldval = *(char **)varp; - } else if (varp == (char *)&p_ww && ascii_isdigit(*arg)) { - // Convert 'whichwrap' number to string, for backwards compatibility - // with Vim 3.0. - *whichwrap = NUL; - int i = getdigits_int(&arg, true, 0); - if (i & 1) { - xstrlcat(whichwrap, "b,", sizeof(whichwrap)); - } - if (i & 2) { - xstrlcat(whichwrap, "s,", sizeof(whichwrap)); - } - if (i & 4) { - xstrlcat(whichwrap, "h,l,", sizeof(whichwrap)); - } - if (i & 8) { - xstrlcat(whichwrap, "<,>,", sizeof(whichwrap)); - } - if (i & 16) { - xstrlcat(whichwrap, "[,],", sizeof(whichwrap)); - } - if (*whichwrap != NUL) { // remove trailing , - whichwrap[strlen(whichwrap) - 1] = NUL; - } - save_arg = arg; - arg = whichwrap; - } else if (*arg == '>' && (varp == (char *)&p_dir || varp == (char *)&p_bdir)) { - // Remove '>' before 'dir' and 'bdir', for backwards compatibility with - // version 3.0 - arg++; } // Copy the new string into allocated memory. - // Can't use set_string_option_direct(), because we need to remove the - // backslashes. - - // get a bit too much - size_t newlen = strlen(arg) + 1; - if (op != OP_NONE) { - newlen += strlen(origval) + 1; - } - newval = xmalloc(newlen); - s = newval; - - // Copy the string, skip over escaped chars. - // For MS-Windows backslashes before normal file name characters - // are not removed, and keep backslash at start, for "\\machine\path", - // but do remove it for "\\\\machine\\path". - // The reverse is found in ExpandOldSetting(). - while (*arg != NUL && !ascii_iswhite(*arg)) { - if (*arg == '\\' && arg[1] != NUL -#ifdef BACKSLASH_IN_FILENAME - && !((flags & P_EXPAND) - && vim_isfilec((uint8_t)arg[1]) - && !ascii_iswhite(arg[1]) - && (arg[1] != '\\' - || (s == newval && arg[2] != '\\'))) -#endif - ) { - arg++; // remove backslash - } - int i = utfc_ptr2len(arg); - if (i > 1) { - // copy multibyte char - memmove(s, arg, (size_t)i); - arg += i; - s += i; - } else { - *s++ = *arg++; - } - } - *s = NUL; + newval = stropt_copy_value(origval, &arg, op, flags); // Expand environment variables and ~. // Don't do it when adding without inserting a comma. if (op == OP_NONE || (flags & P_COMMA)) { - s = option_expand(opt_idx, newval); - if (s != NULL) { - xfree(newval); - newlen = (unsigned)strlen(s) + 1; - if (op != OP_NONE) { - newlen += (unsigned)strlen(origval) + 1; - } - newval = xmalloc(newlen); - STRCPY(newval, s); - } + newval = stropt_expand_envvar(opt_idx, origval, newval, op); } // locate newval[] in origval[] when removing it @@ -936,130 +974,439 @@ static int do_set_string(int opt_idx, int opt_flags, char **argp, int nextchar, // concatenate the two strings; add a ',' if needed if (op == OP_ADDING || op == OP_PREPENDING) { - int comma = ((flags & P_COMMA) && *origval != NUL && *newval != NUL); - if (op == OP_ADDING) { - len = (int)strlen(origval); - // Strip a trailing comma, would get 2. - if (comma && len > 1 - && (flags & P_ONECOMMA) == P_ONECOMMA - && origval[len - 1] == ',' - && origval[len - 2] != '\\') { - len--; - } - memmove(newval + len + comma, newval, strlen(newval) + 1); - memmove(newval, origval, (size_t)len); - } else { - len = (int)strlen(newval); - STRMOVE(newval + len + comma, origval); + stropt_concat_with_comma(origval, newval, op, flags); + } else if (op == OP_REMOVING) { + // Remove newval[] from origval[]. (Note: "len" has been set above + // and is used here). + stropt_remove_val(origval, newval, flags, s, len); + } + + if (flags & P_FLAGLIST) { + // Remove flags that appear twice. + stropt_remove_dupflags(newval, flags); + } + } + + if (save_arg != NULL) { + arg = save_arg; // arg was temporarily changed, restore it + } + *argp = arg; + *op_arg = op; + + return newval; +} + +static set_op_T get_op(const char *arg) +{ + set_op_T op = OP_NONE; + if (*arg != NUL && *(arg + 1) == '=') { + if (*arg == '+') { + op = OP_ADDING; // "+=" + } else if (*arg == '^') { + op = OP_PREPENDING; // "^=" + } else if (*arg == '-') { + op = OP_REMOVING; // "-=" + } + } + return op; +} + +static set_prefix_T get_option_prefix(char **argp) +{ + if (strncmp(*argp, "no", 2) == 0) { + *argp += 2; + return PREFIX_NO; + } else if (strncmp(*argp, "inv", 3) == 0) { + *argp += 3; + return PREFIX_INV; + } + + return PREFIX_NONE; +} + +/// @param[in] arg Pointer to start option name +/// @param[out] opt_idxp Option index in options[] table. +/// @param[out] keyp +/// @param[out] len Length of option name +/// @return FAIL if an error is detected, OK otherwise +static int parse_option_name(char *arg, int *keyp, int *lenp, int *opt_idxp) +{ + // find end of name + int key = 0; + int len; + int opt_idx; + + 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 (comma) { - newval[len] = ','; + } + if (arg[len] != '>') { + return FAIL; + } + if (arg[1] == 't' && arg[2] == '_') { // could be term code + opt_idx = findoption_len(arg + 1, (size_t)(len - 1)); + } + len++; + if (opt_idx == -1) { + key = find_key_option(arg + 1, true); + } + } else { + // The two characters after "t_" may not be alphanumeric. + if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3]) { + len = 4; + } else { + len = 0; + while (ASCII_ISALNUM(arg[len]) || arg[len] == '_') { + len++; } } + opt_idx = findoption_len(arg, (size_t)len); + if (opt_idx == -1) { + key = find_key_option(arg, false); + } + } - // Remove newval[] from origval[]. (Note: "len" has been set above and - // is used here). - if (op == OP_REMOVING) { - STRCPY(newval, origval); - if (*s) { - // may need to remove a comma - if (flags & P_COMMA) { - if (s == origval) { - // include comma after string - if (s[len] == ',') { - len++; - } - } else { - // include comma before string - s--; - len++; - } - } - STRMOVE(newval + (s - origval), s + len); + *keyp = key; + *lenp = len; + *opt_idxp = opt_idx; + + return OK; +} + +static int validate_opt_idx(win_T *win, int opt_idx, int opt_flags, uint32_t flags, + set_prefix_T prefix, const char **errmsg) +{ + // Only bools can have a prefix of 'inv' or 'no' + if (!(flags & P_BOOL) && prefix != PREFIX_NONE) { + *errmsg = e_invarg; + return FAIL; + } + + // Skip all options that are not window-local (used when showing + // an already loaded buffer in a window). + if ((opt_flags & OPT_WINONLY) + && (opt_idx < 0 || options[opt_idx].var != VAR_WIN)) { + return FAIL; + } + + // Skip all options that are window-local (used for :vimgrep). + if ((opt_flags & OPT_NOWIN) && opt_idx >= 0 + && options[opt_idx].var == VAR_WIN) { + return FAIL; + } + + // Disallow changing some options from modelines. + if (opt_flags & OPT_MODELINE) { + if (flags & (P_SECURE | P_NO_ML)) { + *errmsg = e_not_allowed_in_modeline; + return FAIL; + } + if ((flags & P_MLE) && !p_mle) { + *errmsg = e_not_allowed_in_modeline_when_modelineexpr_is_off; + return FAIL; + } + // In diff mode some options are overruled. This avoids that + // 'foldmethod' becomes "marker" instead of "diff" and that + // "wrap" gets set. + if (win->w_p_diff + && opt_idx >= 0 // shut up coverity warning + && (options[opt_idx].indir == PV_FDM + || options[opt_idx].indir == PV_WRAP)) { + return FAIL; + } + } + + // Disallow changing some options in the sandbox + if (sandbox != 0 && (flags & P_SECURE)) { + *errmsg = e_sandbox; + return FAIL; + } + + return OK; +} + +/// Get new option value from argp. Allocated OptVal must be freed by caller. +static OptVal get_option_newval(int opt_idx, int opt_flags, set_prefix_T prefix, char **argp, + int nextchar, set_op_T op, uint32_t flags, void *varp, char *errbuf, + const size_t errbuflen, const char **errmsg) + FUNC_ATTR_WARN_UNUSED_RESULT +{ + assert(varp != NULL); + + vimoption_T *opt = &options[opt_idx]; + char *arg = *argp; + // When setting the local value of a global option, the old value may be the global value. + const bool oldval_is_global = ((int)opt->indir & PV_BOTH) && (opt_flags & OPT_LOCAL); + OptVal oldval = optval_from_varp(opt_idx, oldval_is_global ? get_varp(opt) : varp); + OptVal newval = NIL_OPTVAL; + + switch (oldval.type) { + case kOptValTypeNil: + abort(); + case kOptValTypeBoolean: { + TriState newval_bool; + + // ":set opt!": invert + // ":set opt&": reset to default value + // ":set opt<": reset to global value + if (nextchar == '!') { + switch (oldval.data.boolean) { + case kNone: + newval_bool = kNone; + break; + case kTrue: + newval_bool = kFalse; + break; + case kFalse: + newval_bool = kTrue; + break; + } + } else if (nextchar == '&') { + newval_bool = TRISTATE_FROM_INT((int)(intptr_t)options[opt_idx].def_val); + } else if (nextchar == '<') { + // For 'autoread', kNone means to use global value. + if ((int *)varp == &curbuf->b_p_ar && opt_flags == OPT_LOCAL) { + newval_bool = kNone; + } else { + newval_bool = TRISTATE_FROM_INT(*(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL)); + } + } else { + // ":set invopt": invert + // ":set opt" or ":set noopt": set or reset + if (prefix == PREFIX_INV) { + newval_bool = *(int *)varp ^ 1; + } else { + newval_bool = prefix == PREFIX_NO ? 0 : 1; } } - if (flags & P_FLAGLIST) { - // Remove flags that appear twice. - for (s = newval; *s;) { - // if options have P_FLAGLIST and P_ONECOMMA such as - // 'whichwrap' - if (flags & P_ONECOMMA) { - if (*s != ',' && *(s + 1) == ',' - && vim_strchr(s + 2, (uint8_t)(*s)) != NULL) { - // Remove the duplicated value and the next comma. - STRMOVE(s, s + 2); - continue; - } - } else { - if ((!(flags & P_COMMA) || *s != ',') - && vim_strchr(s + 1, (uint8_t)(*s)) != NULL) { - STRMOVE(s, s + 1); - continue; - } - } - s++; + newval = BOOLEAN_OPTVAL(newval_bool); + break; + } + case kOptValTypeNumber: { + OptInt oldval_num = oldval.data.number; + OptInt newval_num; + + // Different ways to set a number option: + // & set to default value + // < set to global value + // <xx> accept special key codes for 'wildchar' + // c accept any non-digit for 'wildchar' + // [-]0-9 set number + // other error + arg++; + if (nextchar == '&') { + newval_num = (OptInt)(intptr_t)options[opt_idx].def_val; + } else if (nextchar == '<') { + if ((OptInt *)varp == &curbuf->b_p_ul && opt_flags == OPT_LOCAL) { + // for 'undolevels' NO_LOCAL_UNDOLEVEL means using the global newval_num + newval_num = NO_LOCAL_UNDOLEVEL; + } else if (opt_flags == OPT_LOCAL + && ((OptInt *)varp == &curwin->w_p_siso || (OptInt *)varp == &curwin->w_p_so)) { + // for 'scrolloff'/'sidescrolloff' -1 means using the global newval_num + newval_num = -1; + } else { + newval_num = *(OptInt *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL); + } + } else if (((OptInt *)varp == &p_wc || (OptInt *)varp == &p_wcm) + && (*arg == '<' || *arg == '^' + || (*arg != NUL && (!arg[1] || ascii_iswhite(arg[1])) + && !ascii_isdigit(*arg)))) { + newval_num = string_to_key(arg); + if (newval_num == 0 && (OptInt *)varp != &p_wcm) { + *errmsg = e_invarg; + return newval; } + } else if (*arg == '-' || ascii_isdigit(*arg)) { + int i; + // Allow negative, octal and hex numbers. + vim_str2nr(arg, NULL, &i, STR2NR_ALL, &newval_num, NULL, 0, true, NULL); + if (i == 0 || (arg[i] != NUL && !ascii_iswhite(arg[i]))) { + *errmsg = e_number_required_after_equal; + return newval; + } + } else { + *errmsg = e_number_required_after_equal; + return newval; } - if (save_arg != NULL) { - arg = save_arg; // arg was temporarily changed, restore it + if (op == OP_ADDING) { + newval_num = oldval_num + newval_num; } + if (op == OP_PREPENDING) { + newval_num = oldval_num * newval_num; + } + if (op == OP_REMOVING) { + newval_num = oldval_num - newval_num; + } + + newval = NUMBER_OPTVAL(newval_num); + break; + } + case kOptValTypeString: { + char *oldval_str = oldval.data.string.data; + // Get the new value for the option + char *newval_str = stropt_get_newval(nextchar, opt_idx, argp, varp, oldval_str, &op, flags); + newval = CSTR_AS_OPTVAL(newval_str); + break; + } } - // Set the new value. - *(char_u **)(varp) = (char_u *)newval; + return newval; +} - // origval may be freed by did_set_string_option(), make a copy. - char *saved_origval = (origval != NULL) ? xstrdup(origval) : NULL; - char *saved_origval_l = (origval_l != NULL) ? xstrdup((char *)origval_l) : NULL; - char *saved_origval_g = (origval_g != NULL) ? xstrdup((char *)origval_g) : NULL; +static void do_one_set_option(int opt_flags, char **argp, bool *did_show, char *errbuf, + size_t errbuflen, const char **errmsg) +{ + // 1: nothing, 0: "no", 2: "inv" in front of name + set_prefix_T prefix = get_option_prefix(argp); - // newval (and varp) may become invalid if the buffer is closed by - // autocommands. - char *saved_newval = (newval != NULL) ? xstrdup(newval) : NULL; + char *arg = *argp; - { - uint32_t *p = insecure_flag(curwin, opt_idx, opt_flags); - const int secure_saved = secure; + // find end of name + int key = 0; + int len; + int opt_idx; + if (parse_option_name(arg, &key, &len, &opt_idx) == FAIL) { + *errmsg = e_invarg; + return; + } + + // remember character after option name + int afterchar = (uint8_t)arg[len]; - // When an option is set in the sandbox, from a modeline or in secure - // mode, then deal with side effects in secure mode. Also when the - // value was set with the P_INSECURE flag and is not completely - // replaced. - if ((opt_flags & OPT_MODELINE) - || sandbox != 0 - || (op != OP_NONE && (*p & P_INSECURE))) { - secure = 1; + // skip white space, allow ":set ai ?" + while (ascii_iswhite(arg[len])) { + len++; + } + + set_op_T op = get_op(arg + len); + if (op != OP_NONE) { + len++; + } + + uint8_t nextchar = (uint8_t)arg[len]; // next non-white char after option name + + if (opt_idx == -1 && key == 0) { // found a mismatch: skip + *errmsg = e_unknown_option; + return; + } + + uint32_t flags; // flags for current option + void *varp = NULL; // pointer to variable for current option + + if (opt_idx >= 0) { + if (options[opt_idx].var == NULL) { // hidden option: skip + // Only give an error message when requesting the value of + // a hidden option, ignore setting it. + if (vim_strchr("=:!&<", nextchar) == NULL + && (!(options[opt_idx].flags & P_BOOL) + || nextchar == '?')) { + *errmsg = e_unsupportedoption; + } + return; } - // Handle side effects, and set the global value for ":set" on local - // options. Note: when setting 'syntax' or 'filetype' autocommands may - // be triggered that can cause havoc. - *errmsg = did_set_string_option(opt_idx, (char **)varp, oldval, - errbuf, errbuflen, - opt_flags, value_checked); + flags = options[opt_idx].flags; + varp = get_varp_scope(&(options[opt_idx]), opt_flags); + } else { + flags = P_STRING; + } - secure = secure_saved; + if (validate_opt_idx(curwin, opt_idx, opt_flags, flags, prefix, errmsg) == FAIL) { + return; } - if (*errmsg == NULL) { - if (!starting) { - trigger_optionset_string(opt_idx, opt_flags, saved_origval, saved_origval_l, - saved_origval_g, saved_newval); + if (vim_strchr("?=:!&<", nextchar) != NULL) { + *argp += len; + if (nextchar == '&' && (*argp)[1] == 'v' && (*argp)[2] == 'i') { + if ((*argp)[3] == 'm') { // "opt&vim": set to Vim default + *argp += 3; + } else { // "opt&vi": set to Vi default + *argp += 2; + } } - if (options[opt_idx].flags & P_UI_OPTION) { - ui_call_option_set(cstr_as_string(options[opt_idx].fullname), - STRING_OBJ(cstr_as_string(saved_newval))); + if (vim_strchr("?!&<", nextchar) != NULL + && (*argp)[1] != NUL && !ascii_iswhite((*argp)[1])) { + *errmsg = e_trailing; + return; } } - xfree(saved_origval); - xfree(saved_origval_l); - xfree(saved_origval_g); - xfree(saved_newval); - *argp = arg; - return *errmsg == NULL ? OK : FAIL; + // + // allow '=' and ':' as MS-DOS command.com allows only one + // '=' character per "set" command line. grrr. (jw) + // + if (nextchar == '?' + || (prefix == PREFIX_NONE + && vim_strchr("=:&<", nextchar) == NULL + && !(flags & P_BOOL))) { + // print value + if (*did_show) { + msg_putchar('\n'); // cursor below last one + } else { + gotocmdline(true); // cursor at status line + *did_show = true; // remember that we did a line + } + if (opt_idx >= 0) { + showoneopt(&options[opt_idx], opt_flags); + if (p_verbose > 0) { + // Mention where the option was last set. + if (varp == options[opt_idx].var) { + option_last_set_msg(options[opt_idx].last_set); + } else if ((int)options[opt_idx].indir & PV_WIN) { + option_last_set_msg(curwin->w_p_script_ctx[(int)options[opt_idx].indir & PV_MASK]); + } else if ((int)options[opt_idx].indir & PV_BUF) { + option_last_set_msg(curbuf->b_p_script_ctx[(int)options[opt_idx].indir & PV_MASK]); + } + } + } else { + *errmsg = e_key_code_not_set; + return; + } + if (nextchar != '?' && nextchar != NUL && !ascii_iswhite(afterchar)) { + *errmsg = e_trailing; + } + return; + } + + if (flags & P_BOOL) { + if (vim_strchr("=:", nextchar) != NULL) { + *errmsg = e_invarg; + return; + } + + if (vim_strchr("!&<", nextchar) == NULL && nextchar != NUL && !ascii_iswhite(afterchar)) { + *errmsg = e_trailing; + return; + } + } else { + if (vim_strchr("=:&<", nextchar) == NULL) { + *errmsg = e_invarg; + return; + } + } + + // Don't try to change hidden option. + if (varp == NULL) { + return; + } + + OptVal newval = get_option_newval(opt_idx, opt_flags, prefix, argp, nextchar, op, flags, varp, + errbuf, errbuflen, errmsg); + + if (newval.type == kOptValTypeNil || *errmsg != NULL) { + return; + } + + *errmsg = set_option(opt_idx, varp, newval, opt_flags, op == OP_NONE, errbuf, errbuflen); } /// Parse 'arg' for option settings. @@ -1079,402 +1426,79 @@ static int do_set_string(int opt_idx, int opt_flags, char **argp, int nextchar, /// @return FAIL if an error is detected, OK otherwise int do_set(char *arg, int opt_flags) { - int did_show = false; // already showed one value + bool did_show = false; // already showed one value if (*arg == NUL) { - showoptions(0, opt_flags); + showoptions(false, opt_flags); did_show = true; - goto theend; - } - - char errbuf[80]; - - while (*arg != NUL) { // loop to process all options - char *errmsg = NULL; - char *startarg = arg; // remember for error message - - if (strncmp(arg, "all", 3) == 0 && !ASCII_ISALPHA(arg[3]) - && !(opt_flags & OPT_MODELINE)) { - // ":set all" show all options. - // ":set all&" set all options to their default value. - arg += 3; - if (*arg == '&') { - arg++; - // Only for :set command set global value of local options. - set_options_default(OPT_FREE | opt_flags); - didset_options(); - didset_options2(); - ui_refresh_options(); - redraw_all_later(UPD_CLEAR); - } else { - showoptions(1, opt_flags); - did_show = true; - } - } else { - int prefix = 1; // 1: nothing, 0: "no", 2: "inv" in front of name - if (strncmp(arg, "no", 2) == 0) { - prefix = 0; - arg += 2; - } else if (strncmp(arg, "inv", 3) == 0) { - prefix = 2; + } else { + while (*arg != NUL) { // loop to process all options + if (strncmp(arg, "all", 3) == 0 && !ASCII_ISALPHA(arg[3]) + && !(opt_flags & OPT_MODELINE)) { + // ":set all" show all options. + // ":set all&" set all options to their default value. arg += 3; - } - - // find end of name - int key = 0; - int len; - int opt_idx; - if (*arg == '<') { - opt_idx = -1; - // look out for <t_>;> - if (arg[1] == 't' && arg[2] == '_' && arg[3] && arg[4]) { - len = 5; + if (*arg == '&') { + arg++; + // Only for :set command set global value of local options. + set_options_default(OPT_FREE | opt_flags); + didset_options(); + didset_options2(); + ui_refresh_options(); + redraw_all_later(UPD_CLEAR); } else { - len = 1; - while (arg[len] != NUL && arg[len] != '>') { - len++; - } - } - if (arg[len] != '>') { - errmsg = e_invarg; - goto skip; - } - if (arg[1] == 't' && arg[2] == '_') { // could be term code - opt_idx = findoption_len((const char *)arg + 1, (size_t)(len - 1)); - } - len++; - if (opt_idx == -1) { - key = find_key_option(arg + 1, true); + showoptions(true, opt_flags); + did_show = true; } } else { - len = 0; - // The two characters after "t_" may not be alphanumeric. - if (arg[0] == 't' && arg[1] == '_' && arg[2] && arg[3]) { - len = 4; - } else { - while (ASCII_ISALNUM(arg[len]) || arg[len] == '_') { - len++; + char *startarg = arg; // remember for error message + const char *errmsg = NULL; + char errbuf[80]; + + do_one_set_option(opt_flags, &arg, &did_show, errbuf, sizeof(errbuf), &errmsg); + + // Advance to next argument. + // - skip until a blank found, taking care of backslashes + // - skip blanks + // - skip one "=val" argument (for hidden options ":set gfn =xx") + for (int i = 0; i < 2; i++) { + arg = skiptowhite_esc(arg); + arg = skipwhite(arg); + if (*arg != '=') { + break; } } - opt_idx = findoption_len((const char *)arg, (size_t)len); - if (opt_idx == -1) { - key = find_key_option(arg, false); - } - } - - // remember character after option name - int afterchar = (uint8_t)arg[len]; - - // skip white space, allow ":set ai ?" - while (ascii_iswhite(arg[len])) { - len++; - } - - set_op_T op = OP_NONE; - if (arg[len] != NUL && arg[len + 1] == '=') { - if (arg[len] == '+') { - op = OP_ADDING; // "+=" - len++; - } else if (arg[len] == '^') { - op = OP_PREPENDING; // "^=" - len++; - } else if (arg[len] == '-') { - op = OP_REMOVING; // "-=" - len++; - } - } - char_u nextchar = (uint8_t)arg[len]; // next non-white char after option name - if (opt_idx == -1 && key == 0) { // found a mismatch: skip - errmsg = e_unknown_option; - goto skip; - } - - uint32_t flags; // flags for current option - char *varp = NULL; // pointer to variable for current option - - if (opt_idx >= 0) { - if (options[opt_idx].var == NULL) { // hidden option: skip - // Only give an error message when requesting the value of - // a hidden option, ignore setting it. - if (vim_strchr("=:!&<", (uint8_t)nextchar) == NULL - && (!(options[opt_idx].flags & P_BOOL) - || nextchar == '?')) { - errmsg = e_unsupportedoption; + if (errmsg != NULL) { + xstrlcpy(IObuff, _(errmsg), IOSIZE); + int i = (int)strlen(IObuff) + 2; + if (i + (arg - startarg) < IOSIZE) { + // append the argument with the error + xstrlcat(IObuff, ": ", IOSIZE); + assert(arg >= startarg); + memmove(IObuff + i, startarg, (size_t)(arg - startarg)); + IObuff[i + (arg - startarg)] = NUL; } - goto skip; - } + // make sure all characters are printable + trans_characters(IObuff, IOSIZE); - flags = options[opt_idx].flags; - varp = get_varp_scope(&(options[opt_idx]), opt_flags); - } else { - flags = P_STRING; - } - - // Skip all options that are not window-local (used when showing - // an already loaded buffer in a window). - if ((opt_flags & OPT_WINONLY) - && (opt_idx < 0 || options[opt_idx].var != VAR_WIN)) { - goto skip; - } + no_wait_return++; // wait_return() done later + emsg(IObuff); // show error highlighted + no_wait_return--; - // Skip all options that are window-local (used for :vimgrep). - if ((opt_flags & OPT_NOWIN) && opt_idx >= 0 - && options[opt_idx].var == VAR_WIN) { - goto skip; - } - - // Disallow changing some options from modelines. - if (opt_flags & OPT_MODELINE) { - if (flags & (P_SECURE | P_NO_ML)) { - errmsg = e_not_allowed_in_modeline; - goto skip; - } - if ((flags & P_MLE) && !p_mle) { - errmsg = e_not_allowed_in_modeline_when_modelineexpr_is_off; - goto skip; - } - // In diff mode some options are overruled. This avoids that - // 'foldmethod' becomes "marker" instead of "diff" and that - // "wrap" gets set. - if (curwin->w_p_diff - && opt_idx >= 0 // shut up coverity warning - && (options[opt_idx].indir == PV_FDM - || options[opt_idx].indir == PV_WRAP)) { - goto skip; - } - } - - // Disallow changing some options in the sandbox - if (sandbox != 0 && (flags & P_SECURE)) { - errmsg = e_sandbox; - goto skip; - } - - if (vim_strchr("?=:!&<", (uint8_t)nextchar) != NULL) { - arg += len; - if (nextchar == '&' && arg[1] == 'v' && arg[2] == 'i') { - if (arg[3] == 'm') { // "opt&vim": set to Vim default - arg += 3; - } else { // "opt&vi": set to Vi default - arg += 2; - } - } - if (vim_strchr("?!&<", (uint8_t)nextchar) != NULL - && arg[1] != NUL && !ascii_iswhite(arg[1])) { - errmsg = e_trailing; - goto skip; - } - } - - // - // allow '=' and ':' as MS-DOS command.com allows only one - // '=' character per "set" command line. grrr. (jw) - // - if (nextchar == '?' - || (prefix == 1 - && vim_strchr("=:&<", (uint8_t)nextchar) == NULL - && !(flags & P_BOOL))) { - // print value - if (did_show) { - msg_putchar('\n'); // cursor below last one - } else { - gotocmdline(true); // cursor at status line - did_show = true; // remember that we did a line - } - if (opt_idx >= 0) { - showoneopt(&options[opt_idx], opt_flags); - if (p_verbose > 0) { - // Mention where the option was last set. - if (varp == (char *)options[opt_idx].var) { - option_last_set_msg(options[opt_idx].last_set); - } else if ((int)options[opt_idx].indir & PV_WIN) { - option_last_set_msg(curwin->w_p_script_ctx[ - (int)options[opt_idx].indir & PV_MASK]); - } else if ((int)options[opt_idx].indir & PV_BUF) { - option_last_set_msg(curbuf->b_p_script_ctx[ - (int)options[opt_idx].indir & PV_MASK]); - } - } - } else { - errmsg = e_key_code_not_set; - goto skip; - } - if (nextchar != '?' - && nextchar != NUL && !ascii_iswhite(afterchar)) { - errmsg = e_trailing; - } - } else { - int value_checked = false; - varnumber_T value; - - if (flags & P_BOOL) { // boolean - if (nextchar == '=' || nextchar == ':') { - errmsg = e_invarg; - goto skip; - } - - // ":set opt!": invert - // ":set opt&": reset to default value - // ":set opt<": reset to global value - if (nextchar == '!') { - value = *(int *)(varp) ^ 1; - } else if (nextchar == '&') { - value = (int)(intptr_t)options[opt_idx].def_val; - } else if (nextchar == '<') { - // For 'autoread' -1 means to use global value. - if ((int *)varp == &curbuf->b_p_ar - && opt_flags == OPT_LOCAL) { - value = -1; - } else { - value = *(int *)get_varp_scope(&(options[opt_idx]), - OPT_GLOBAL); - } - } else { - // ":set invopt": invert - // ":set opt" or ":set noopt": set or reset - if (nextchar != NUL && !ascii_iswhite(afterchar)) { - errmsg = e_trailing; - goto skip; - } - if (prefix == 2) { // inv - value = *(int *)(varp) ^ 1; - } else { - value = prefix; - } - } - - errmsg = set_bool_option(opt_idx, (char_u *)varp, (int)value, opt_flags); - } else { // Numeric or string. - if (vim_strchr("=:&<", (uint8_t)nextchar) == NULL - || prefix != 1) { - errmsg = e_invarg; - goto skip; - } - - if (flags & P_NUM) { // numeric - // Different ways to set a number option: - // & set to default value - // < set to global value - // <xx> accept special key codes for 'wildchar' - // c accept any non-digit for 'wildchar' - // [-]0-9 set number - // other error - arg++; - if (nextchar == '&') { - value = (long)(intptr_t)options[opt_idx].def_val; - } else if (nextchar == '<') { - // For 'undolevels' NO_LOCAL_UNDOLEVEL means to - // use the global value. - if ((long *)varp == &curbuf->b_p_ul && opt_flags == OPT_LOCAL) { - value = NO_LOCAL_UNDOLEVEL; - } else { - value = *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL); - } - } else if (((long *)varp == &p_wc - || (long *)varp == &p_wcm) - && (*arg == '<' - || *arg == '^' - || (*arg != NUL && (!arg[1] || ascii_iswhite(arg[1])) - && !ascii_isdigit(*arg)))) { - value = string_to_key(arg); - if (value == 0 && (long *)varp != &p_wcm) { - errmsg = e_invarg; - goto skip; - } - } else if (*arg == '-' || ascii_isdigit(*arg)) { - int i; - // Allow negative, octal and hex numbers. - vim_str2nr(arg, NULL, &i, STR2NR_ALL, &value, NULL, 0, true); - if (i == 0 || (arg[i] != NUL && !ascii_iswhite(arg[i]))) { - errmsg = e_number_required_after_equal; - goto skip; - } - } else { - errmsg = e_number_required_after_equal; - goto skip; - } - - if (op == OP_ADDING) { - value = *(long *)varp + value; - } - if (op == OP_PREPENDING) { - value = *(long *)varp * value; - } - if (op == OP_REMOVING) { - value = *(long *)varp - value; - } - errmsg = set_num_option(opt_idx, (char_u *)varp, (long)value, - errbuf, sizeof(errbuf), - opt_flags); - } else if (opt_idx >= 0) { // String. - if (do_set_string(opt_idx, opt_flags, &arg, nextchar, - op, flags, varp, errbuf, sizeof(errbuf), - &value_checked, &errmsg) == FAIL) { - if (errmsg != NULL) { - goto skip; - } - break; - } - } else { - // key code option(FIXME(tarruda): Show a warning or something - // similar) - } - } - - if (opt_idx >= 0) { - did_set_option(opt_idx, opt_flags, op == OP_NONE, value_checked); - } - } - -skip: - // Advance to next argument. - // - skip until a blank found, taking care of backslashes - // - skip blanks - // - skip one "=val" argument (for hidden options ":set gfn =xx") - for (int i = 0; i < 2; i++) { - while (*arg != NUL && !ascii_iswhite(*arg)) { - if (*arg++ == '\\' && *arg != NUL) { - arg++; - } - } - arg = skipwhite(arg); - if (*arg != '=') { - break; + return FAIL; } } - } - - if (errmsg != NULL) { - xstrlcpy(IObuff, _(errmsg), IOSIZE); - int i = (int)strlen(IObuff) + 2; - if (i + (arg - startarg) < IOSIZE) { - // append the argument with the error - STRCAT(IObuff, ": "); - assert(arg >= startarg); - memmove(IObuff + i, startarg, (size_t)(arg - startarg)); - IObuff[i + (arg - startarg)] = NUL; - } - // make sure all characters are printable - trans_characters(IObuff, IOSIZE); - - no_wait_return++; // wait_return() done later - emsg(IObuff); // show error highlighted - no_wait_return--; - return FAIL; + arg = skipwhite(arg); } - - arg = skipwhite(arg); } -theend: if (silent_mode && did_show) { // After displaying option values in silent mode. silent_mode = false; info_message = true; // use os_msg(), not os_errmsg() msg_putchar('\n'); - ui_flush(); silent_mode = true; info_message = false; // use os_msg(), not os_errmsg() } @@ -1482,29 +1506,6 @@ theend: return OK; } -/// Call this when an option has been given a new value through a user command. -/// Sets the P_WAS_SET flag and takes care of the P_INSECURE flag. -/// -/// @param opt_flags possibly with OPT_MODELINE -/// @param new_value value was replaced completely -/// @param value_checked value was checked to be safe, no need to set P_INSECURE -void did_set_option(int opt_idx, int opt_flags, int new_value, int value_checked) -{ - options[opt_idx].flags |= P_WAS_SET; - - // When an option is set in the sandbox, from a modeline or in secure mode - // set the P_INSECURE flag. Otherwise, if a new value is stored reset the - // flag. - uint32_t *p = insecure_flag(curwin, opt_idx, opt_flags); - if (!value_checked && (secure - || sandbox != 0 - || (opt_flags & OPT_MODELINE))) { - *p = *p | P_INSECURE; - } else if (new_value) { - *p = *p & ~P_INSECURE; - } -} - /// Convert a key name or string into a key value. /// Used for 'wildchar' and 'cedit' options. int string_to_key(char *arg) @@ -1579,6 +1580,9 @@ void set_options_bin(int oldval, int newval, int opt_flags) p_et = p_et_nobin; } } + + // Remember where the dependent option were reset + didset_options_sctx(opt_flags, p_bin_dep_opts); } /// Find the parameter represented by the given character (eg ', :, ", or /), @@ -1642,8 +1646,8 @@ static char *option_expand(int opt_idx, char *val) // For 'spellsuggest' expand after "file:". expand_env_esc(val, NameBuff, MAXPATHL, (char **)options[opt_idx].var == &p_tags, false, - (char_u **)options[opt_idx].var == (char_u **)&p_sps ? "file:" : - NULL); + (char **)options[opt_idx].var == &p_sps ? "file:" + : NULL); if (strcmp(NameBuff, val) == 0) { // they are the same return NULL; } @@ -1665,9 +1669,9 @@ static void didset_options(void) (void)compile_cap_prog(curwin->w_s); (void)did_set_spell_option(true); // set cedit_key - (void)check_cedit(); + (void)did_set_cedit(NULL); // initialize the table for 'breakat'. - fill_breakat_flags(); + (void)did_set_breakat(NULL); didset_window_options(curwin, true); } @@ -1678,10 +1682,10 @@ static void didset_options2(void) highlight_changed(); // Parse default for 'fillchars'. - (void)set_chars_option(curwin, &curwin->w_p_fcs, true); + (void)set_fillchars_option(curwin, curwin->w_p_fcs, true); // Parse default for 'listchars'. - (void)set_chars_option(curwin, &curwin->w_p_lcs, true); + (void)set_listchars_option(curwin, curwin->w_p_lcs, true); // Parse default for 'wildmode'. check_opt_wim(); @@ -1720,7 +1724,7 @@ int was_set_insecurely(win_T *const wp, char *opt, int opt_flags) /// "opt_idx". For some local options a local flags field is used. /// NOTE: Caller must make sure that "wp" is set to the window from which /// the option is used. -static uint32_t *insecure_flag(win_T *const wp, int opt_idx, int opt_flags) +uint32_t *insecure_flag(win_T *const wp, int opt_idx, int opt_flags) { if (opt_flags & OPT_LOCAL) { assert(wp != NULL); @@ -1776,7 +1780,7 @@ void check_blending(win_T *wp) /// Handle setting `winhighlight' in window "wp" bool parse_winhl_opt(win_T *wp) { - const char *p = (const char *)wp->w_p_winhl; + const char *p = wp->w_p_winhl; if (!*p) { if (wp->w_ns_hl_winhl && wp->w_ns_hl == wp->w_ns_hl_winhl) { @@ -1807,6 +1811,9 @@ bool parse_winhl_opt(win_T *wp) char *commap = xstrchrnul(hi, ','); size_t len = (size_t)(commap - hi); int hl_id = len ? syn_check_group(hi, len) : -1; + if (hl_id == 0) { + return false; + } int hl_id_link = nlen ? syn_check_group(p, nlen) : 0; HlAttrs attrs = HLATTRS_INIT; @@ -1820,6 +1827,18 @@ bool parse_winhl_opt(win_T *wp) return true; } +/// Get the script context of global option "name". +sctx_T *get_option_sctx(const char *const name) +{ + int idx = findoption(name); + + if (idx >= 0) { + return &options[idx].last_set.script_ctx; + } + siemsg("no such option: %s", name); + return NULL; +} + /// Set the script_ctx for an option, taking care of setting the buffer- or /// window-local value. void set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx) @@ -1847,738 +1866,1127 @@ void set_option_sctx_idx(int opt_idx, int opt_flags, sctx_T script_ctx) curbuf->b_p_script_ctx[indir & PV_MASK] = last_set; } else if (indir & PV_WIN) { curwin->w_p_script_ctx[indir & PV_MASK] = last_set; + if (both) { + // also setting the "all buffers" value + curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] = last_set; + } } } } /// Apply the OptionSet autocommand. -static void apply_optionset_autocmd(int opt_idx, long opt_flags, long oldval, long oldval_g, - long newval, const char *errmsg) +static void apply_optionset_autocmd(int opt_idx, int opt_flags, OptVal oldval, OptVal oldval_g, + OptVal oldval_l, OptVal newval, const char *errmsg) { // Don't do this while starting up, failure or recursively. if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL) { return; } - char buf_old[12], buf_old_global[12], buf_new[12], buf_type[12]; + char buf_type[7]; + typval_T oldval_tv = optval_as_tv(oldval); + typval_T oldval_g_tv = optval_as_tv(oldval_g); + typval_T oldval_l_tv = optval_as_tv(oldval_l); + typval_T newval_tv = optval_as_tv(newval); - vim_snprintf(buf_old, sizeof(buf_old), "%ld", oldval); - vim_snprintf(buf_old_global, sizeof(buf_old_global), "%ld", oldval_g); - vim_snprintf(buf_new, sizeof(buf_new), "%ld", newval); - vim_snprintf(buf_type, sizeof(buf_type), "%s", - (opt_flags & OPT_LOCAL) ? "local" : "global"); - set_vim_var_string(VV_OPTION_NEW, buf_new, -1); - set_vim_var_string(VV_OPTION_OLD, buf_old, -1); + vim_snprintf(buf_type, sizeof(buf_type), "%s", (opt_flags & OPT_LOCAL) ? "local" : "global"); + set_vim_var_tv(VV_OPTION_NEW, &newval_tv); + set_vim_var_tv(VV_OPTION_OLD, &oldval_tv); set_vim_var_string(VV_OPTION_TYPE, buf_type, -1); if (opt_flags & OPT_LOCAL) { set_vim_var_string(VV_OPTION_COMMAND, "setlocal", -1); - set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1); + set_vim_var_tv(VV_OPTION_OLDLOCAL, &oldval_tv); } if (opt_flags & OPT_GLOBAL) { set_vim_var_string(VV_OPTION_COMMAND, "setglobal", -1); - set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1); + set_vim_var_tv(VV_OPTION_OLDGLOBAL, &oldval_tv); } if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0) { set_vim_var_string(VV_OPTION_COMMAND, "set", -1); - set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1); - set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1); + set_vim_var_tv(VV_OPTION_OLDLOCAL, &oldval_l_tv); + set_vim_var_tv(VV_OPTION_OLDGLOBAL, &oldval_g_tv); } if (opt_flags & OPT_MODELINE) { set_vim_var_string(VV_OPTION_COMMAND, "modeline", -1); - set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1); + set_vim_var_tv(VV_OPTION_OLDLOCAL, &oldval_tv); } apply_autocmds(EVENT_OPTIONSET, options[opt_idx].fullname, NULL, false, NULL); reset_v_option_vars(); } -/// Set the value of a boolean option, taking care of side effects -/// -/// @param[in] opt_idx Option index in options[] table. -/// @param[out] varp Pointer to the option variable. -/// @param[in] value New value. -/// @param[in] opt_flags OPT_LOCAL and/or OPT_GLOBAL. -/// -/// @return NULL on success, error message on error. -static char *set_bool_option(const int opt_idx, char_u *const varp, const int value, - const int opt_flags) +/// Process the updated 'arabic' option value. +static const char *did_set_arabic(optset_T *args) { - int old_value = *(int *)varp; - int old_global_value = 0; - char *errmsg = NULL; + win_T *win = (win_T *)args->os_win; + const char *errmsg = NULL; + + if (win->w_p_arab) { + // 'arabic' is set, handle various sub-settings. + if (!p_tbidi) { + // set rightleft mode + if (!win->w_p_rl) { + win->w_p_rl = true; + changed_window_setting(); + } - // Disallow changing some options from secure mode - if ((secure || sandbox != 0) - && (options[opt_idx].flags & P_SECURE)) { - return (char *)e_secure; + // Enable Arabic shaping (major part of what Arabic requires) + if (!p_arshape) { + p_arshape = true; + redraw_all_later(UPD_NOT_VALID); + } + } + + // Arabic requires a utf-8 encoding, inform the user if it's not + // set. + if (strcmp(p_enc, "utf-8") != 0) { + static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'"); + + msg_source(HL_ATTR(HLF_W)); + msg(_(w_arabic), HL_ATTR(HLF_W)); + set_vim_var_string(VV_WARNINGMSG, _(w_arabic), -1); + } + + // set 'delcombine' + p_deco = true; + + // Force-set the necessary keymap for arabic. + errmsg = set_option_value("keymap", STATIC_CSTR_AS_OPTVAL("arabic"), OPT_LOCAL); + } else { + // 'arabic' is reset, handle various sub-settings. + if (!p_tbidi) { + // reset rightleft mode + if (win->w_p_rl) { + win->w_p_rl = false; + changed_window_setting(); + } + + // 'arabicshape' isn't reset, it is a global option and + // another window may still need it "on". + } + + // 'delcombine' isn't reset, it is a global option and another + // window may still want it "on". + + // Revert to the default keymap + curbuf->b_p_iminsert = B_IMODE_NONE; + curbuf->b_p_imsearch = B_IMODE_USE_INSERT; } - // Save the global value before changing anything. This is needed as for - // a global-only option setting the "local value" in fact sets the global - // value (since there is only one value). - if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0) { - old_global_value = *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL); + return errmsg; +} + +/// Process the updated 'autochdir' option value. +static const char *did_set_autochdir(optset_T *args FUNC_ATTR_UNUSED) +{ + // Change directories when the 'acd' option is set now. + do_autochdir(); + return NULL; +} + +/// Process the updated 'binary' option value. +static const char *did_set_binary(optset_T *args) +{ + buf_T *buf = (buf_T *)args->os_buf; + + // when 'bin' is set also set some other options + set_options_bin((int)args->os_oldval.boolean, buf->b_p_bin, args->os_flags); + redraw_titles(); + + return NULL; +} + +/// Called when the 'breakat' option changes value. +static const char *did_set_breakat(optset_T *args FUNC_ATTR_UNUSED) +{ + for (int i = 0; i < 256; i++) { + breakat_flags[i] = false; } - *(int *)varp = value; // set the new value - // Remember where the option was set. - set_option_sctx_idx(opt_idx, opt_flags, current_sctx); + if (p_breakat != NULL) { + for (char *p = p_breakat; *p; p++) { + breakat_flags[(uint8_t)(*p)] = true; + } + } - // May set global value for local option. - if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0) { - *(int *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = value; - } - - // Ensure that options set to p_force_on cannot be disabled. - if ((int *)varp == &p_force_on && p_force_on == false) { - p_force_on = true; - return e_unsupportedoption; - // Ensure that options set to p_force_off cannot be enabled. - } else if ((int *)varp == &p_force_off && p_force_off == true) { - p_force_off = false; - return (char *)e_unsupportedoption; - } else if ((int *)varp == &p_lrm) { - // 'langremap' -> !'langnoremap' - p_lnr = !p_lrm; - } else if ((int *)varp == &p_lnr) { - // 'langnoremap' -> !'langremap' - p_lrm = !p_lnr; - } else if ((int *)varp == &curbuf->b_p_udf || (int *)varp == &p_udf) { - // 'undofile' - // Only take action when the option was set. When reset we do not - // delete the undo file, the option may be set again without making - // any changes in between. - if (curbuf->b_p_udf || p_udf) { - char_u hash[UNDO_HASH_SIZE]; - - FOR_ALL_BUFFERS(bp) { - // When 'undofile' is set globally: for every buffer, otherwise - // only for the current buffer: Try to read in the undofile, - // if one exists, the buffer wasn't changed and the buffer was - // loaded - if ((curbuf == bp - || (opt_flags & OPT_GLOBAL) || opt_flags == 0) - && !bufIsChanged(bp) && bp->b_ml.ml_mfp != NULL) { - u_compute_hash(bp, hash); - u_read_undo(NULL, hash, bp->b_fname); - } - } + return NULL; +} + +/// Process the updated 'buflisted' option value. +static const char *did_set_buflisted(optset_T *args) +{ + buf_T *buf = (buf_T *)args->os_buf; + + // when 'buflisted' changes, trigger autocommands + if (args->os_oldval.boolean != buf->b_p_bl) { + apply_autocmds(buf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE, + NULL, NULL, true, buf); + } + return NULL; +} + +/// Process the new 'cmdheight' option value. +static const char *did_set_cmdheight(optset_T *args) +{ + OptInt old_value = args->os_oldval.number; + + if (ui_has(kUIMessages)) { + p_ch = 0; + } + if (p_ch > Rows - min_rows() + 1) { + p_ch = Rows - min_rows() + 1; + } + + // if p_ch changed value, change the command line height + // Only compute the new window layout when startup has been + // completed. Otherwise the frame sizes may be wrong. + if ((p_ch != old_value + || tabline_height() + global_stl_height() + topframe->fr_height != Rows - p_ch) + && full_screen) { + command_height(); + } + + return NULL; +} + +/// Process the updated 'diff' option value. +static const char *did_set_diff(optset_T *args) +{ + win_T *win = (win_T *)args->os_win; + // May add or remove the buffer from the list of diff buffers. + diff_buf_adjust(win); + if (foldmethodIsDiff(win)) { + foldUpdateAll(win); + } + return NULL; +} + +/// Process the updated 'endoffile' or 'endofline' or 'fixendofline' or 'bomb' +/// option value. +static const char *did_set_eof_eol_fixeol_bomb(optset_T *args FUNC_ATTR_UNUSED) +{ + // redraw the window title and tab page text + redraw_titles(); + return NULL; +} + +/// Process the updated 'equalalways' option value. +static const char *did_set_equalalways(optset_T *args) +{ + win_T *win = (win_T *)args->os_win; + if (p_ea && !args->os_oldval.boolean) { + win_equal(win, false, 0); + } + + return NULL; +} + +/// Process the new 'foldlevel' option value. +static const char *did_set_foldlevel(optset_T *args FUNC_ATTR_UNUSED) +{ + newFoldLevel(); + return NULL; +} + +/// Process the new 'foldminlines' option value. +static const char *did_set_foldminlines(optset_T *args) +{ + win_T *win = (win_T *)args->os_win; + foldUpdateAll(win); + return NULL; +} + +/// Process the new 'foldnestmax' option value. +static const char *did_set_foldnestmax(optset_T *args) +{ + win_T *win = (win_T *)args->os_win; + if (foldmethodIsSyntax(win) || foldmethodIsIndent(win)) { + foldUpdateAll(win); + } + return NULL; +} + +/// Process the new 'helpheight' option value. +static const char *did_set_helpheight(optset_T *args) +{ + // Change window height NOW + if (!ONE_WINDOW) { + buf_T *buf = (buf_T *)args->os_buf; + win_T *win = (win_T *)args->os_win; + if (buf->b_help && win->w_height < p_hh) { + win_setheight((int)p_hh); } - } else if ((int *)varp == &curbuf->b_p_ro) { - // when 'readonly' is reset globally, also reset readonlymode - if (!curbuf->b_p_ro && (opt_flags & OPT_LOCAL) == 0) { - readonlymode = false; - } - - // when 'readonly' is set may give W10 again - if (curbuf->b_p_ro) { - curbuf->b_did_warn = false; - } - - redraw_titles(); - } else if ((int *)varp == &curbuf->b_p_ma) { - // when 'modifiable' is changed, redraw the window title - redraw_titles(); - } else if ((int *)varp == &curbuf->b_p_eof - || (int *)varp == &curbuf->b_p_eol - || (int *)varp == &curbuf->b_p_fixeol - || (int *)varp == &curbuf->b_p_bomb) { - // redraw the window title and tab page text when 'endoffile', 'endofline', - // 'fixeol' or 'bomb' is changed - redraw_titles(); - } else if ((int *)varp == &curbuf->b_p_bin) { - // when 'bin' is set also set some other options - set_options_bin(old_value, curbuf->b_p_bin, opt_flags); - redraw_titles(); - } else if ((int *)varp == &curbuf->b_p_bl && old_value != curbuf->b_p_bl) { - // when 'buflisted' changes, trigger autocommands - apply_autocmds(curbuf->b_p_bl ? EVENT_BUFADD : EVENT_BUFDELETE, - NULL, NULL, true, curbuf); - } else if ((int *)varp == &curbuf->b_p_swf) { - // when 'swf' is set, create swapfile, when reset remove swapfile - if (curbuf->b_p_swf && p_uc) { - ml_open_file(curbuf); // create the swap file - } else { - // no need to reset curbuf->b_may_swap, ml_open_file() will check - // buf->b_p_swf - mf_close_file(curbuf, true); // remove the swap file - } - } else if ((int *)varp == &p_paste) { - // when 'paste' is set or reset also change other options - paste_option_changed(); - } else if ((int *)varp == &p_ic && p_hls) { - // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw + } + + return NULL; +} + +/// Process the updated 'hlsearch' option value. +static const char *did_set_hlsearch(optset_T *args FUNC_ATTR_UNUSED) +{ + // when 'hlsearch' is set or reset: reset no_hlsearch + set_no_hlsearch(false); + return NULL; +} + +/// Process the updated 'ignorecase' option value. +static const char *did_set_ignorecase(optset_T *args FUNC_ATTR_UNUSED) +{ + // when 'ignorecase' is set or reset and 'hlsearch' is set, redraw + if (p_hls) { redraw_all_later(UPD_SOME_VALID); - } else if ((int *)varp == &p_hls) { - // when 'hlsearch' is set or reset: reset no_hlsearch - set_no_hlsearch(false); - } else if ((int *)varp == &curwin->w_p_scb) { - // when 'scrollbind' is set: snapshot the current position to avoid a jump - // at the end of normal_cmd() - if (curwin->w_p_scb) { - do_check_scrollbind(false); - curwin->w_scbind_pos = curwin->w_topline; - } - } else if ((int *)varp == &curwin->w_p_pvw) { - // There can be only one window with 'previewwindow' set. - if (curwin->w_p_pvw) { - FOR_ALL_WINDOWS_IN_TAB(win, curtab) { - if (win->w_p_pvw && win != curwin) { - curwin->w_p_pvw = false; - return e_preview_window_already_exists; + } + return NULL; +} + +/// Process the new 'iminset' option value. +static const char *did_set_iminsert(optset_T *args FUNC_ATTR_UNUSED) +{ + showmode(); + // Show/unshow value of 'keymap' in status lines. + status_redraw_curbuf(); + + return NULL; +} + +/// Process the updated 'langnoremap' option value. +static const char *did_set_langnoremap(optset_T *args FUNC_ATTR_UNUSED) +{ + // 'langnoremap' -> !'langremap' + p_lrm = !p_lnr; + return NULL; +} + +/// Process the updated 'langremap' option value. +static const char *did_set_langremap(optset_T *args FUNC_ATTR_UNUSED) +{ + // 'langremap' -> !'langnoremap' + p_lnr = !p_lrm; + return NULL; +} + +/// Process the new 'laststatus' option value. +static const char *did_set_laststatus(optset_T *args) +{ + OptInt old_value = args->os_oldval.number; + OptInt value = args->os_newval.number; + + // When switching to global statusline, decrease topframe height + // Also clear the cmdline to remove the ruler if there is one + if (value == 3 && old_value != 3) { + frame_new_height(topframe, topframe->fr_height - STATUS_HEIGHT, false, false); + (void)win_comp_pos(); + clear_cmdline = true; + } + // When switching from global statusline, increase height of topframe by STATUS_HEIGHT + // in order to to re-add the space that was previously taken by the global statusline + if (old_value == 3 && value != 3) { + frame_new_height(topframe, topframe->fr_height + STATUS_HEIGHT, false, false); + (void)win_comp_pos(); + } + + last_status(false); // (re)set last window status line. + return NULL; +} + +/// Process the updated 'lisp' option value. +static const char *did_set_lisp(optset_T *args) +{ + buf_T *buf = (buf_T *)args->os_buf; + // When 'lisp' option changes include/exclude '-' in keyword characters. + (void)buf_init_chartab(buf, false); // ignore errors + return NULL; +} + +/// Process the updated 'modifiable' option value. +static const char *did_set_modifiable(optset_T *args FUNC_ATTR_UNUSED) +{ + // when 'modifiable' is changed, redraw the window title + redraw_titles(); + + return NULL; +} + +/// Process the updated 'modified' option value. +static const char *did_set_modified(optset_T *args) +{ + buf_T *buf = (buf_T *)args->os_buf; + if (!args->os_newval.boolean) { + save_file_ff(buf); // Buffer is unchanged + } + redraw_titles(); + modified_was_set = (int)args->os_newval.boolean; + return NULL; +} + +/// Process the updated 'number' or 'relativenumber' option value. +static const char *did_set_number_relativenumber(optset_T *args) +{ + win_T *win = (win_T *)args->os_win; + if (*win->w_p_stc != NUL) { + // When 'relativenumber'/'number' is changed and 'statuscolumn' is set, reset width. + win->w_nrwidth_line_count = 0; + } + check_signcolumn(win); + return NULL; +} + +/// Process the new 'numberwidth' option value. +static const char *did_set_numberwidth(optset_T *args) +{ + win_T *win = (win_T *)args->os_win; + win->w_nrwidth_line_count = 0; // trigger a redraw + + return NULL; +} + +/// Process the updated 'paste' option value. +static const char *did_set_paste(optset_T *args FUNC_ATTR_UNUSED) +{ + static int old_p_paste = false; + static int save_sm = 0; + static int save_sta = 0; + static int save_ru = 0; + static int save_ri = 0; + + if (p_paste) { + // Paste switched from off to on. + // Save the current values, so they can be restored later. + if (!old_p_paste) { + // save options for each buffer + FOR_ALL_BUFFERS(buf) { + buf->b_p_tw_nopaste = buf->b_p_tw; + buf->b_p_wm_nopaste = buf->b_p_wm; + buf->b_p_sts_nopaste = buf->b_p_sts; + buf->b_p_ai_nopaste = buf->b_p_ai; + buf->b_p_et_nopaste = buf->b_p_et; + if (buf->b_p_vsts_nopaste) { + xfree(buf->b_p_vsts_nopaste); } + buf->b_p_vsts_nopaste = buf->b_p_vsts && buf->b_p_vsts != empty_string_option + ? xstrdup(buf->b_p_vsts) + : NULL; + } + + // save global options + save_sm = p_sm; + save_sta = p_sta; + save_ru = p_ru; + save_ri = p_ri; + // save global values for local buffer options + p_ai_nopaste = p_ai; + p_et_nopaste = p_et; + p_sts_nopaste = p_sts; + p_tw_nopaste = p_tw; + p_wm_nopaste = p_wm; + if (p_vsts_nopaste) { + xfree(p_vsts_nopaste); } + p_vsts_nopaste = p_vsts && p_vsts != empty_string_option ? xstrdup(p_vsts) : NULL; } - } else if (varp == (char_u *)&(curbuf->b_p_lisp)) { - // When 'lisp' option changes include/exclude '-' in - // keyword characters. - (void)buf_init_chartab(curbuf, false); // ignore errors - } else if ((int *)varp == &p_title) { - // when 'title' changed, may need to change the title; same for 'icon' - did_set_title(); - } else if ((int *)varp == &p_icon) { - did_set_title(); - } else if ((int *)varp == &curbuf->b_changed) { - if (!value) { - save_file_ff(curbuf); // Buffer is unchanged - } - redraw_titles(); - modified_was_set = value; -#ifdef BACKSLASH_IN_FILENAME - } else if ((int *)varp == &p_ssl) { - if (p_ssl) { - psepc = '/'; - psepcN = '\\'; - pseps[0] = '/'; - } else { - psepc = '\\'; - psepcN = '/'; - pseps[0] = '\\'; + // Always set the option values, also when 'paste' is set when it is + // already on. + // set options for each buffer + FOR_ALL_BUFFERS(buf) { + buf->b_p_tw = 0; // textwidth is 0 + buf->b_p_wm = 0; // wrapmargin is 0 + buf->b_p_sts = 0; // softtabstop is 0 + buf->b_p_ai = 0; // no auto-indent + buf->b_p_et = 0; // no expandtab + if (buf->b_p_vsts) { + free_string_option(buf->b_p_vsts); + } + buf->b_p_vsts = empty_string_option; + XFREE_CLEAR(buf->b_p_vsts_array); } - // need to adjust the file name arguments and buffer names. - buflist_slash_adjust(); - alist_slash_adjust(); - scriptnames_slash_adjust(); -#endif - } else if ((int *)varp == &curwin->w_p_wrap) { - // If 'wrap' is set, set w_leftcol to zero. - if (curwin->w_p_wrap) { - curwin->w_leftcol = 0; - } - } else if ((int *)varp == &p_ea) { - if (p_ea && !old_value) { - win_equal(curwin, false, 0); - } - } else if ((int *)varp == &p_acd) { - // Change directories when the 'acd' option is set now. - do_autochdir(); - } else if ((int *)varp == &curwin->w_p_diff) { // 'diff' - // May add or remove the buffer from the list of diff buffers. - diff_buf_adjust(curwin); - if (foldmethodIsDiff(curwin)) { - foldUpdateAll(curwin); - } - } else if ((int *)varp == &curwin->w_p_spell) { // 'spell' - if (curwin->w_p_spell) { - errmsg = did_set_spelllang(curwin); - } - } else if (((int *)varp == &curwin->w_p_nu || (int *)varp == &curwin->w_p_rnu) - && *curwin->w_p_stc != NUL) { // '(relative)number' + 'statuscolumn' - curwin->w_nrwidth_line_count = 0; - } - - if ((int *)varp == &curwin->w_p_arab) { - if (curwin->w_p_arab) { - // 'arabic' is set, handle various sub-settings. - if (!p_tbidi) { - // set rightleft mode - if (!curwin->w_p_rl) { - curwin->w_p_rl = true; - changed_window_setting(); - } + // set global options + p_sm = 0; // no showmatch + p_sta = 0; // no smarttab + if (p_ru) { + status_redraw_all(); // redraw to remove the ruler + } + p_ru = 0; // no ruler + p_ri = 0; // no reverse insert + // set global values for local buffer options + p_tw = 0; + p_wm = 0; + p_sts = 0; + p_ai = 0; + p_et = 0; + if (p_vsts) { + free_string_option(p_vsts); + } + p_vsts = empty_string_option; + } else if (old_p_paste) { + // Paste switched from on to off: Restore saved values. - // Enable Arabic shaping (major part of what Arabic requires) - if (!p_arshape) { - p_arshape = true; - redraw_all_later(UPD_NOT_VALID); - } + // restore options for each buffer + FOR_ALL_BUFFERS(buf) { + buf->b_p_tw = buf->b_p_tw_nopaste; + buf->b_p_wm = buf->b_p_wm_nopaste; + buf->b_p_sts = buf->b_p_sts_nopaste; + buf->b_p_ai = buf->b_p_ai_nopaste; + buf->b_p_et = buf->b_p_et_nopaste; + if (buf->b_p_vsts) { + free_string_option(buf->b_p_vsts); } + buf->b_p_vsts = buf->b_p_vsts_nopaste ? xstrdup(buf->b_p_vsts_nopaste) : empty_string_option; + xfree(buf->b_p_vsts_array); + if (buf->b_p_vsts && buf->b_p_vsts != empty_string_option) { + (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array); + } else { + buf->b_p_vsts_array = NULL; + } + } - // Arabic requires a utf-8 encoding, inform the user if it's not - // set. - if (strcmp(p_enc, "utf-8") != 0) { - static char *w_arabic = N_("W17: Arabic requires UTF-8, do ':set encoding=utf-8'"); + // restore global options + p_sm = save_sm; + p_sta = save_sta; + if (p_ru != save_ru) { + status_redraw_all(); // redraw to draw the ruler + } + p_ru = save_ru; + p_ri = save_ri; + // set global values for local buffer options + p_ai = p_ai_nopaste; + p_et = p_et_nopaste; + p_sts = p_sts_nopaste; + p_tw = p_tw_nopaste; + p_wm = p_wm_nopaste; + if (p_vsts) { + free_string_option(p_vsts); + } + p_vsts = p_vsts_nopaste ? xstrdup(p_vsts_nopaste) : empty_string_option; + } - msg_source(HL_ATTR(HLF_W)); - msg_attr(_(w_arabic), HL_ATTR(HLF_W)); - set_vim_var_string(VV_WARNINGMSG, _(w_arabic), -1); - } + old_p_paste = p_paste; - // set 'delcombine' - p_deco = true; + // Remember where the dependent options were reset + didset_options_sctx((OPT_LOCAL | OPT_GLOBAL), p_paste_dep_opts); - // Force-set the necessary keymap for arabic. - errmsg = set_option_value("keymap", 0L, "arabic", OPT_LOCAL); - } else { - // 'arabic' is reset, handle various sub-settings. - if (!p_tbidi) { - // reset rightleft mode - if (curwin->w_p_rl) { - curwin->w_p_rl = false; - changed_window_setting(); - } + return NULL; +} - // 'arabicshape' isn't reset, it is a global option and - // another window may still need it "on". - } +/// Process the updated 'previewwindow' option value. +static const char *did_set_previewwindow(optset_T *args) +{ + win_T *win = (win_T *)args->os_win; - // 'delcombine' isn't reset, it is a global option and another - // window may still want it "on". + if (!win->w_p_pvw) { + return NULL; + } - // Revert to the default keymap - curbuf->b_p_iminsert = B_IMODE_NONE; - curbuf->b_p_imsearch = B_IMODE_USE_INSERT; + // There can be only one window with 'previewwindow' set. + FOR_ALL_WINDOWS_IN_TAB(wp, curtab) { + if (wp->w_p_pvw && wp != win) { + win->w_p_pvw = false; + return e_preview_window_already_exists; } } - // End of handling side effects for bool options. + return NULL; +} - // after handling side effects, call autocommand +/// Process the new 'pumblend' option value. +static const char *did_set_pumblend(optset_T *args FUNC_ATTR_UNUSED) +{ + p_pb = MAX(MIN(p_pb, 100), 0); + hl_invalidate_blends(); + pum_grid.blending = (p_pb > 0); + if (pum_drawn()) { + pum_redraw(); + } - options[opt_idx].flags |= P_WAS_SET; + return NULL; +} - apply_optionset_autocmd(opt_idx, opt_flags, - (long)(old_value ? true : false), - (long)(old_global_value ? true : false), - (long)(value ? true : false), NULL); +/// Process the updated 'readonly' option value. +static const char *did_set_readonly(optset_T *args) +{ + // when 'readonly' is reset globally, also reset readonlymode + if (!curbuf->b_p_ro && (args->os_flags & OPT_LOCAL) == 0) { + readonlymode = false; + } - if (options[opt_idx].flags & P_UI_OPTION) { - ui_call_option_set(cstr_as_string(options[opt_idx].fullname), - BOOLEAN_OBJ(*varp)); + // when 'readonly' is set may give W10 again + if (curbuf->b_p_ro) { + curbuf->b_did_warn = false; } - comp_col(); // in case 'ruler' or 'showcmd' changed - if (curwin->w_curswant != MAXCOL - && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0) { - curwin->w_set_curswant = true; + redraw_titles(); + + return NULL; +} + +/// Process the new 'scrollback' option value. +static const char *did_set_scrollback(optset_T *args) +{ + buf_T *buf = (buf_T *)args->os_buf; + OptInt old_value = args->os_oldval.number; + OptInt value = args->os_newval.number; + + if (buf->terminal && value < old_value) { + // Force the scrollback to take immediate effect only when decreasing it. + on_scrollback_option_changed(buf->terminal); } - check_redraw(options[opt_idx].flags); + return NULL; +} - return errmsg; +/// Process the updated 'scrollbind' option value. +static const char *did_set_scrollbind(optset_T *args) +{ + win_T *win = (win_T *)args->os_win; + + // when 'scrollbind' is set: snapshot the current position to avoid a jump + // at the end of normal_cmd() + if (!win->w_p_scb) { + return NULL; + } + do_check_scrollbind(false); + win->w_scbind_pos = win->w_topline; + return NULL; } -/// Set the value of a number option, taking care of side effects -/// -/// @param[in] opt_idx Option index in options[] table. -/// @param[out] varp Pointer to the option variable. -/// @param[in] value New value. -/// @param errbuf Buffer for error messages. -/// @param[in] errbuflen Length of `errbuf`. -/// @param[in] opt_flags OPT_LOCAL, OPT_GLOBAL or OPT_MODELINE. -/// -/// @return NULL on success, error message on error. -static char *set_num_option(int opt_idx, char_u *varp, long value, char *errbuf, size_t errbuflen, - int opt_flags) +#ifdef BACKSLASH_IN_FILENAME +/// Process the updated 'shellslash' option value. +static const char *did_set_shellslash(optset_T *args FUNC_ATTR_UNUSED) { - char *errmsg = NULL; - long old_value = *(long *)varp; - long old_global_value = 0; // only used when setting a local and global option - long old_Rows = Rows; // remember old Rows - long *pp = (long *)varp; + if (p_ssl) { + psepc = '/'; + psepcN = '\\'; + pseps[0] = '/'; + } else { + psepc = '\\'; + psepcN = '/'; + pseps[0] = '\\'; + } - // Disallow changing some options from secure mode. - if ((secure || sandbox != 0) - && (options[opt_idx].flags & P_SECURE)) { - return e_secure; + // need to adjust the file name arguments and buffer names. + buflist_slash_adjust(); + alist_slash_adjust(); + scriptnames_slash_adjust(); + return NULL; +} +#endif + +/// Process the new 'shiftwidth' or the 'tabstop' option value. +static const char *did_set_shiftwidth_tabstop(optset_T *args) +{ + buf_T *buf = (buf_T *)args->os_buf; + win_T *win = (win_T *)args->os_win; + OptInt *pp = (OptInt *)args->os_varp; + + if (foldmethodIsIndent(win)) { + foldUpdateAll(win); + } + // When 'shiftwidth' changes, or it's zero and 'tabstop' changes: + // parse 'cinoptions'. + if (pp == &buf->b_p_sw || buf->b_p_sw == 0) { + parse_cino(buf); } - // Save the global value before changing anything. This is needed as for - // a global-only option setting the "local value" in fact sets the global - // value (since there is only one value). - if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0) { - old_global_value = *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL); + return NULL; +} + +/// Process the new 'showtabline' option value. +static const char *did_set_showtabline(optset_T *args FUNC_ATTR_UNUSED) +{ + // (re)set tab page line + win_new_screen_rows(); // recompute window positions and heights + return NULL; +} + +/// Process the updated 'smoothscroll' option value. +static const char *did_set_smoothscroll(optset_T *args FUNC_ATTR_UNUSED) +{ + win_T *win = (win_T *)args->os_win; + if (!win->w_p_sms) { + win->w_skipcol = 0; + } + + return NULL; +} + +/// Process the updated 'spell' option value. +static const char *did_set_spell(optset_T *args) +{ + win_T *win = (win_T *)args->os_win; + if (win->w_p_spell) { + return parse_spelllang(win); + } + + return NULL; +} + +/// Process the updated 'swapfile' option value. +static const char *did_set_swapfile(optset_T *args) +{ + buf_T *buf = (buf_T *)args->os_buf; + // when 'swf' is set, create swapfile, when reset remove swapfile + if (buf->b_p_swf && p_uc) { + ml_open_file(buf); // create the swap file + } else { + // no need to reset curbuf->b_may_swap, ml_open_file() will check + // buf->b_p_swf + mf_close_file(buf, true); // remove the swap file + } + return NULL; +} + +/// Process the new 'textwidth' option value. +static const char *did_set_textwidth(optset_T *args FUNC_ATTR_UNUSED) +{ + FOR_ALL_TAB_WINDOWS(tp, wp) { + check_colorcolumn(wp); + } + + return NULL; +} + +/// Process the updated 'title' or the 'icon' option value. +static const char *did_set_title_icon(optset_T *args FUNC_ATTR_UNUSED) +{ + // when 'title' changed, may need to change the title; same for 'icon' + did_set_title(); + return NULL; +} + +/// Process the new 'titlelen' option value. +static const char *did_set_titlelen(optset_T *args) +{ + OptInt old_value = args->os_oldval.number; + + // if 'titlelen' has changed, redraw the title + if (starting != NO_SCREEN && old_value != p_titlelen) { + need_maketitle = true; } + return NULL; +} + +/// Process the updated 'undofile' option value. +static const char *did_set_undofile(optset_T *args) +{ + // Only take action when the option was set. + if (!curbuf->b_p_udf && !p_udf) { + return NULL; + } + + // When reset we do not delete the undo file, the option may be set again + // without making any changes in between. + uint8_t hash[UNDO_HASH_SIZE]; + + FOR_ALL_BUFFERS(bp) { + // When 'undofile' is set globally: for every buffer, otherwise + // only for the current buffer: Try to read in the undofile, + // if one exists, the buffer wasn't changed and the buffer was + // loaded + if ((curbuf == bp + || (args->os_flags & OPT_GLOBAL) || args->os_flags == 0) + && !bufIsChanged(bp) && bp->b_ml.ml_mfp != NULL) { + u_compute_hash(bp, hash); + u_read_undo(NULL, hash, bp->b_fname); + } + } + + return NULL; +} + +/// Process the new global 'undolevels' option value. +const char *did_set_global_undolevels(OptInt value, OptInt old_value) +{ + // sync undo before 'undolevels' changes + // use the old value, otherwise u_sync() may not work properly + p_ul = old_value; + u_sync(true); + p_ul = value; + return NULL; +} + +/// Process the new buffer local 'undolevels' option value. +const char *did_set_buflocal_undolevels(buf_T *buf, OptInt value, OptInt old_value) +{ + // use the old value, otherwise u_sync() may not work properly + buf->b_p_ul = old_value; + u_sync(true); + buf->b_p_ul = value; + return NULL; +} + +/// Process the new 'undolevels' option value. +static const char *did_set_undolevels(optset_T *args) +{ + buf_T *buf = (buf_T *)args->os_buf; + OptInt *pp = (OptInt *)args->os_varp; + + if (pp == &p_ul) { // global 'undolevels' + did_set_global_undolevels(args->os_newval.number, args->os_oldval.number); + } else if (pp == &curbuf->b_p_ul) { // buffer local 'undolevels' + did_set_buflocal_undolevels(buf, args->os_newval.number, args->os_oldval.number); + } + + return NULL; +} + +/// Process the new 'updatecount' option value. +static const char *did_set_updatecount(optset_T *args) +{ + OptInt old_value = args->os_oldval.number; + + // when 'updatecount' changes from zero to non-zero, open swap files + if (p_uc && !old_value) { + ml_open_files(); + } + + return NULL; +} + +/// Process the new 'wildchar' / 'wildcharm' option value. +static const char *did_set_wildchar(optset_T *args) +{ + OptInt c = *(OptInt *)args->os_varp; + + // Don't allow key values that wouldn't work as wildchar. + if (c == Ctrl_C || c == '\n' || c == '\r' || c == K_KENTER) { + return e_invarg; + } + + return NULL; +} + +/// Process the new 'winblend' option value. +static const char *did_set_winblend(optset_T *args) +{ + win_T *win = (win_T *)args->os_win; + OptInt old_value = args->os_oldval.number; + OptInt value = args->os_newval.number; + + if (value != old_value) { + win->w_p_winbl = MAX(MIN(win->w_p_winbl, 100), 0); + win->w_hl_needs_update = true; + check_blending(win); + } + + return NULL; +} + +/// Process the new 'window' option value. +static const char *did_set_window(optset_T *args FUNC_ATTR_UNUSED) +{ + if (p_window < 1) { + p_window = Rows - 1; + } else if (p_window >= Rows) { + p_window = Rows - 1; + } + return NULL; +} + +/// Process the new 'winheight' value. +static const char *did_set_winheight(optset_T *args) +{ + // Change window height NOW + if (!ONE_WINDOW) { + win_T *win = (win_T *)args->os_win; + if (win->w_height < p_wh) { + win_setheight((int)p_wh); + } + } + + return NULL; +} + +/// Process the new 'winwidth' option value. +static const char *did_set_winwidth(optset_T *args) +{ + win_T *win = (win_T *)args->os_win; + + if (!ONE_WINDOW && win->w_width < p_wiw) { + win_setwidth((int)p_wiw); + } + return NULL; +} + +/// Process the updated 'wrap' option value. +static const char *did_set_wrap(optset_T *args) +{ + win_T *win = (win_T *)args->os_win; + // Set w_leftcol or w_skipcol to zero. + if (win->w_p_wrap) { + win->w_leftcol = 0; + } else { + win->w_skipcol = 0; + } + + return NULL; +} + +// When 'syntax' is set, load the syntax of that name +static void do_syntax_autocmd(buf_T *buf, bool value_changed) +{ + static int syn_recursive = 0; + + syn_recursive++; + // Only pass true for "force" when the value changed or not used + // recursively, to avoid endless recurrence. + apply_autocmds(EVENT_SYNTAX, buf->b_p_syn, buf->b_fname, + value_changed || syn_recursive == 1, buf); + buf->b_flags |= BF_SYN_SET; + syn_recursive--; +} + +static void do_spelllang_source(win_T *win) +{ + char fname[200]; + char *q = win->w_s->b_p_spl; + + // Skip the first name if it is "cjk". + if (strncmp(q, "cjk,", 4) == 0) { + q += 4; + } + + // Source the spell/LANG.{vim,lua} in 'runtimepath'. + // They could set 'spellcapcheck' depending on the language. + // Use the first name in 'spelllang' up to '_region' or + // '.encoding'. + char *p; + for (p = q; *p != NUL; p++) { + if (!ASCII_ISALNUM(*p) && *p != '-') { + break; + } + } + if (p > q) { + vim_snprintf(fname, sizeof(fname), "spell/%.*s.*", (int)(p - q), q); + source_runtime_vim_lua(fname, DIP_ALL); + } +} + +/// Check the bounds of numeric options. +static const char *check_num_option_bounds(OptInt *pp, OptInt old_value, char *errbuf, + size_t errbuflen, const char *errmsg) +{ + int old_Rows = Rows; // remember old Rows + // Check the (new) bounds for Rows and Columns here. + if (p_lines < min_rows() && full_screen) { + if (errbuf != NULL) { + vim_snprintf(errbuf, errbuflen, _("E593: Need at least %d lines"), min_rows()); + errmsg = errbuf; + } + p_lines = min_rows(); + } + if (p_columns < MIN_COLUMNS && full_screen) { + if (errbuf != NULL) { + vim_snprintf(errbuf, errbuflen, _("E594: Need at least %d columns"), MIN_COLUMNS); + errmsg = errbuf; + } + p_columns = MIN_COLUMNS; + } + + // True max size is defined by check_screensize() + p_lines = MIN(p_lines, INT_MAX); + p_columns = MIN(p_columns, INT_MAX); + + // If the screen (shell) height has been changed, assume it is the + // physical screenheight. + if (p_lines != Rows || p_columns != Columns) { + // Changing the screen size is not allowed while updating the screen. + if (updating_screen) { + *pp = old_value; + } else if (full_screen) { + screen_resize((int)p_columns, (int)p_lines); + } else { + // TODO(bfredl): is this branch ever needed? + // Postpone the resizing; check the size and cmdline position for + // messages. + Rows = (int)p_lines; + Columns = (int)p_columns; + check_screensize(); + int new_row = (int)(Rows - MAX(p_ch, 1)); + if (cmdline_row > new_row && Rows > p_ch) { + assert(p_ch >= 0 && new_row <= INT_MAX); + cmdline_row = new_row; + } + } + if (p_window >= Rows || !option_was_set("window")) { + p_window = Rows - 1; + } + } + + if ((curwin->w_p_scr <= 0 || (curwin->w_p_scr > curwin->w_height && curwin->w_height > 0)) + && full_screen) { + if (pp == &(curwin->w_p_scr)) { + if (curwin->w_p_scr != 0) { + errmsg = e_scroll; + } + win_comp_scroll(curwin); + } else if (curwin->w_p_scr <= 0) { + // If 'scroll' became invalid because of a side effect silently adjust it. + curwin->w_p_scr = 1; + } else { // curwin->w_p_scr > curwin->w_height + curwin->w_p_scr = curwin->w_height; + } + } + if ((p_sj < -100 || p_sj >= Rows) && full_screen) { + if (Rows != old_Rows) { // Rows changed, just adjust p_sj + p_sj = Rows / 2; + } else { + errmsg = e_scroll; + p_sj = 1; + } + } + + return errmsg; +} + +/// Options that need some validation. +static const char *validate_num_option(const OptInt *pp, OptInt *valuep) +{ + OptInt value = *valuep; + // Many number options assume their value is in the signed int range. if (value < INT_MIN || value > INT_MAX) { return e_invarg; } - // Options that need some validation. if (pp == &p_wh) { if (value < 1) { - errmsg = e_positive; + return e_positive; } else if (p_wmh > value) { - errmsg = e_winheight; + return e_winheight; } } else if (pp == &p_hh) { if (value < 0) { - errmsg = e_positive; + return e_positive; } } else if (pp == &p_wmh) { if (value < 0) { - errmsg = e_positive; + return e_positive; } else if (value > p_wh) { - errmsg = e_winheight; + return e_winheight; } } else if (pp == &p_wiw) { if (value < 1) { - errmsg = e_positive; + return e_positive; } else if (p_wmw > value) { - errmsg = e_winwidth; + return e_winwidth; } } else if (pp == &p_wmw) { if (value < 0) { - errmsg = e_positive; + return e_positive; } else if (value > p_wiw) { - errmsg = e_winwidth; + return e_winwidth; } } else if (pp == &p_mco) { - value = MAX_MCO; + *valuep = MAX_MCO; } else if (pp == &p_titlelen) { if (value < 0) { - errmsg = e_positive; + return e_positive; } } else if (pp == &p_uc) { if (value < 0) { - errmsg = e_positive; + return e_positive; } } else if (pp == &p_ch) { - int minval = 0; - if (value < minval) { - errmsg = e_positive; + if (value < 0) { + return e_positive; } else { p_ch_was_zero = value == 0; } } else if (pp == &p_tm) { if (value < 0) { - errmsg = e_positive; + return e_positive; } } else if (pp == &p_hi) { if (value < 0) { - errmsg = e_positive; + return e_positive; } else if (value > 10000) { - errmsg = e_invarg; + return e_invarg; } } else if (pp == &p_pyx) { if (value == 0) { - value = 3; + *valuep = 3; } else if (value != 3) { - errmsg = e_invarg; + return e_invarg; } } else if (pp == &p_re) { if (value < 0 || value > 2) { - errmsg = e_invarg; + return e_invarg; } } else if (pp == &p_report) { if (value < 0) { - errmsg = e_positive; + return e_positive; } } else if (pp == &p_so) { if (value < 0 && full_screen) { - errmsg = e_positive; + return e_positive; } } else if (pp == &p_siso) { if (value < 0 && full_screen) { - errmsg = e_positive; + return e_positive; } } else if (pp == &p_cwh) { if (value < 1) { - errmsg = e_positive; + return e_positive; } } else if (pp == &p_ut) { if (value < 0) { - errmsg = e_positive; + return e_positive; } } else if (pp == &p_ss) { if (value < 0) { - errmsg = e_positive; + return e_positive; } } else if (pp == &curwin->w_p_fdl || pp == &curwin->w_allbuf_opt.wo_fdl) { if (value < 0) { - errmsg = e_positive; + return e_positive; } } else if (pp == &curwin->w_p_cole || pp == &curwin->w_allbuf_opt.wo_cole) { if (value < 0) { - errmsg = e_positive; + return e_positive; } else if (value > 3) { - errmsg = e_invarg; + return e_invarg; } } else if (pp == &curwin->w_p_nuw || pp == &curwin->w_allbuf_opt.wo_nuw) { if (value < 1) { - errmsg = e_positive; + return e_positive; } else if (value > MAX_NUMBERWIDTH) { - errmsg = e_invarg; + return e_invarg; } } else if (pp == &curbuf->b_p_iminsert || pp == &p_iminsert) { if (value < 0 || value > B_IMODE_LAST) { - errmsg = e_invarg; + return e_invarg; } } else if (pp == &curbuf->b_p_imsearch || pp == &p_imsearch) { if (value < -1 || value > B_IMODE_LAST) { - errmsg = e_invarg; + return e_invarg; } } else if (pp == &curbuf->b_p_channel || pp == &p_channel) { - errmsg = e_invarg; + return e_invarg; } else if (pp == &curbuf->b_p_scbk || pp == &p_scbk) { if (value < -1 || value > SB_MAX) { - errmsg = e_invarg; + return e_invarg; } } else if (pp == &curbuf->b_p_sw || pp == &p_sw) { if (value < 0) { - errmsg = e_positive; + return e_positive; } } else if (pp == &curbuf->b_p_ts || pp == &p_ts) { if (value < 1) { - errmsg = e_positive; + return e_positive; } else if (value > TABSTOP_MAX) { - errmsg = e_invarg; + return e_invarg; } } else if (pp == &curbuf->b_p_tw || pp == &p_tw) { if (value < 0) { - errmsg = e_positive; + return e_positive; } } else if (pp == &p_wd) { if (value < 0) { - errmsg = e_positive; + return e_positive; } } - // Don't change the value and return early if validation failed. - if (errmsg != NULL) { - return errmsg; - } - - *pp = value; - // Remember where the option was set. - set_option_sctx_idx(opt_idx, opt_flags, current_sctx); - - // For these options we want to fix some invalid values. - if (pp == &p_window) { - if (p_window < 1) { - p_window = Rows - 1; - } else if (p_window >= Rows) { - p_window = Rows - 1; - } - } else if (pp == &p_ch) { - if (ui_has(kUIMessages)) { - p_ch = 0; - } - if (p_ch > Rows - min_rows() + 1) { - p_ch = Rows - min_rows() + 1; - } - } - - // Number options that need some action when changed - if (pp == &p_wh) { - // 'winheight' - if (!ONE_WINDOW && curwin->w_height < p_wh) { - win_setheight((int)p_wh); - } - } else if (pp == &p_hh) { - // 'helpheight' - if (!ONE_WINDOW && curbuf->b_help && curwin->w_height < p_hh) { - win_setheight((int)p_hh); - } - } else if (pp == &p_wmh) { - // 'winminheight' - win_setminheight(); - } else if (pp == &p_wiw) { - // 'winwidth' - if (!ONE_WINDOW && curwin->w_width < p_wiw) { - win_setwidth((int)p_wiw); - } - } else if (pp == &p_wmw) { - // 'winminwidth' - win_setminwidth(); - } else if (pp == &p_ls) { - // When switching to global statusline, decrease topframe height - // Also clear the cmdline to remove the ruler if there is one - if (value == 3 && old_value != 3) { - frame_new_height(topframe, topframe->fr_height - STATUS_HEIGHT, false, false); - (void)win_comp_pos(); - clear_cmdline = true; - } - // When switching from global statusline, increase height of topframe by STATUS_HEIGHT - // in order to to re-add the space that was previously taken by the global statusline - if (old_value == 3 && value != 3) { - frame_new_height(topframe, topframe->fr_height + STATUS_HEIGHT, false, false); - (void)win_comp_pos(); - } - - last_status(false); // (re)set last window status line. - } else if (pp == &p_stal) { - // (re)set tab page line - win_new_screen_rows(); // recompute window positions and heights - } else if (pp == &curwin->w_p_fdl) { - newFoldLevel(); - } else if (pp == &curwin->w_p_fml) { - foldUpdateAll(curwin); - } else if (pp == &curwin->w_p_fdn) { - if (foldmethodIsSyntax(curwin) || foldmethodIsIndent(curwin)) { - foldUpdateAll(curwin); - } - } else if (pp == &curbuf->b_p_sw || pp == &curbuf->b_p_ts) { - // 'shiftwidth' or 'tabstop' - if (foldmethodIsIndent(curwin)) { - foldUpdateAll(curwin); - } - // When 'shiftwidth' changes, or it's zero and 'tabstop' changes: - // parse 'cinoptions'. - if (pp == &curbuf->b_p_sw || curbuf->b_p_sw == 0) { - parse_cino(curbuf); - } - } else if (pp == &curbuf->b_p_iminsert) { - showmode(); - // Show/unshow value of 'keymap' in status lines. - status_redraw_curbuf(); - } else if (pp == &p_titlelen) { - // if 'titlelen' has changed, redraw the title - if (starting != NO_SCREEN && old_value != p_titlelen) { - need_maketitle = true; - } - } else if (pp == &p_ch) { - // if p_ch changed value, change the command line height - // Only compute the new window layout when startup has been - // completed. Otherwise the frame sizes may be wrong. - if ((p_ch != old_value - || tabline_height() + global_stl_height() + topframe->fr_height != Rows - p_ch) - && full_screen) { - command_height(); - } - } else if (pp == &p_uc) { - // when 'updatecount' changes from zero to non-zero, open swap files - if (p_uc && !old_value) { - ml_open_files(); - } - } else if (pp == &p_pb) { - p_pb = MAX(MIN(p_pb, 100), 0); - hl_invalidate_blends(); - pum_grid.blending = (p_pb > 0); - if (pum_drawn()) { - pum_redraw(); - } - } else if (pp == &p_ul || pp == &curbuf->b_p_ul) { - // sync undo before 'undolevels' changes - // use the old value, otherwise u_sync() may not work properly - *pp = old_value; - u_sync(true); - *pp = value; - } else if (pp == &curbuf->b_p_tw) { - FOR_ALL_TAB_WINDOWS(tp, wp) { - check_colorcolumn(wp); - } - } else if (pp == &curbuf->b_p_scbk || pp == &p_scbk) { - if (curbuf->terminal && value < old_value) { - // Force the scrollback to take immediate effect only when decreasing it. - on_scrollback_option_changed(curbuf->terminal); - } - } else if (pp == &curwin->w_p_nuw) { - curwin->w_nrwidth_line_count = 0; - } else if (pp == &curwin->w_p_winbl && value != old_value) { - // 'winblend' - curwin->w_p_winbl = MAX(MIN(curwin->w_p_winbl, 100), 0); - curwin->w_hl_needs_update = true; - check_blending(curwin); - } - - // Check the (new) bounds for Rows and Columns here. - if (p_lines < min_rows() && full_screen) { - if (errbuf != NULL) { - vim_snprintf(errbuf, errbuflen, - _("E593: Need at least %d lines"), min_rows()); - errmsg = errbuf; - } - p_lines = min_rows(); - } - if (p_columns < MIN_COLUMNS && full_screen) { - if (errbuf != NULL) { - vim_snprintf(errbuf, errbuflen, - _("E594: Need at least %d columns"), MIN_COLUMNS); - errmsg = errbuf; - } - p_columns = MIN_COLUMNS; - } - - // True max size is defined by check_screensize() - p_lines = MIN(p_lines, INT_MAX); - p_columns = MIN(p_columns, INT_MAX); - - // If the screen (shell) height has been changed, assume it is the - // physical screenheight. - if (p_lines != Rows || p_columns != Columns) { - // Changing the screen size is not allowed while updating the screen. - if (updating_screen) { - *pp = old_value; - } else if (full_screen) { - screen_resize((int)p_columns, (int)p_lines); - } else { - // TODO(bfredl): is this branch ever needed? - // Postpone the resizing; check the size and cmdline position for - // messages. - Rows = (int)p_lines; - Columns = (int)p_columns; - check_screensize(); - int new_row = (int)(Rows - MAX(p_ch, 1)); - if (cmdline_row > new_row && Rows > p_ch) { - assert(p_ch >= 0 && new_row <= INT_MAX); - cmdline_row = new_row; - } - } - if (p_window >= Rows || !option_was_set("window")) { - p_window = Rows - 1; - } - } - - if ((curwin->w_p_scr <= 0 - || (curwin->w_p_scr > curwin->w_height - && curwin->w_height > 0)) - && full_screen) { - if (pp == &(curwin->w_p_scr)) { - if (curwin->w_p_scr != 0) { - errmsg = e_scroll; - } - win_comp_scroll(curwin); - } else if (curwin->w_p_scr <= 0) { - // If 'scroll' became invalid because of a side effect silently adjust it. - curwin->w_p_scr = 1; - } else { // curwin->w_p_scr > curwin->w_height - curwin->w_p_scr = curwin->w_height; - } - } - if ((p_sj < -100 || p_sj >= Rows) && full_screen) { - if (Rows != old_Rows) { // Rows changed, just adjust p_sj - p_sj = Rows / 2; - } else { - errmsg = e_scroll; - p_sj = 1; - } - } - - // May set global value for local option. - if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0) { - *(long *)get_varp_scope(&(options[opt_idx]), OPT_GLOBAL) = *pp; - } - - options[opt_idx].flags |= P_WAS_SET; - - apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value, - value, errmsg); - - if (errmsg == NULL && options[opt_idx].flags & P_UI_OPTION) { - ui_call_option_set(cstr_as_string(options[opt_idx].fullname), - INTEGER_OBJ(*pp)); - } - - comp_col(); // in case 'columns' or 'ls' changed - if (curwin->w_curswant != MAXCOL - && (options[opt_idx].flags & (P_CURSWANT | P_RALL)) != 0) { - curwin->w_set_curswant = true; - } - check_redraw(options[opt_idx].flags); - - return errmsg; + return NULL; } /// Called after an option changed: check if something needs to be redrawn. @@ -2757,23 +3165,6 @@ bool set_tty_option(const char *name, char *value) return false; } -void set_tty_background(const char *value) -{ - if (option_was_set("bg") || strequal(p_bg, value)) { - // background is already set... ignore - return; - } - if (starting) { - // Wait until after startup, so OptionSet is triggered. - do_cmdline_cmd((value[0] == 'l') - ? "autocmd VimEnter * ++once ++nested set bg=light" - : "autocmd VimEnter * ++once ++nested set bg=dark"); - } else { - set_option_value_give_err("bg", 0L, value, 0); - reset_option_was_set("bg"); - } -} - /// Find index for an option /// /// @param[in] arg Option name. @@ -2785,283 +3176,702 @@ int findoption(const char *const arg) return findoption_len(arg, strlen(arg)); } -/// Gets the value for an option. +/// Free an allocated OptVal. +void optval_free(OptVal o) +{ + switch (o.type) { + case kOptValTypeNil: + case kOptValTypeBoolean: + case kOptValTypeNumber: + break; + case kOptValTypeString: + // Don't free empty string option + if (o.data.string.data != empty_string_option) { + api_free_string(o.data.string); + } + break; + } +} + +/// Copy an OptVal. +OptVal optval_copy(OptVal o) +{ + switch (o.type) { + case kOptValTypeNil: + case kOptValTypeBoolean: + case kOptValTypeNumber: + return o; + case kOptValTypeString: + return STRING_OPTVAL(copy_string(o.data.string, NULL)); + } + UNREACHABLE; +} + +/// Check if two option values are equal. +bool optval_equal(OptVal o1, OptVal o2) +{ + if (o1.type != o2.type) { + return false; + } + + switch (o1.type) { + case kOptValTypeNil: + return true; + case kOptValTypeBoolean: + return o1.data.boolean == o2.data.boolean; + case kOptValTypeNumber: + return o1.data.number == o2.data.number; + case kOptValTypeString: + return o1.data.string.size == o2.data.string.size + && strequal(o1.data.string.data, o2.data.string.data); + } + UNREACHABLE; +} + +/// Match type of OptVal with the type of the target option. Returns true if the types match and +/// false otherwise. +static bool optval_match_type(OptVal o, int opt_idx) +{ + assert(opt_idx >= 0); + uint32_t flags = options[opt_idx].flags; + + switch (o.type) { + case kOptValTypeNil: + return false; + case kOptValTypeBoolean: + return flags & P_BOOL; + case kOptValTypeNumber: + return flags & P_NUM; + case kOptValTypeString: + return flags & P_STRING; + } + UNREACHABLE; +} + +/// Create OptVal from var pointer. /// -/// @param stringval NULL when only checking existence -/// @param flagsp set to the option flags (P_xxxx) (if not NULL) +/// @param opt_idx Option index in options[] table. +/// @param[out] varp Pointer to option variable. +OptVal optval_from_varp(int opt_idx, void *varp) +{ + // Special case: 'modified' is b_changed, but we also want to consider it set when 'ff' or 'fenc' + // changed. + if ((int *)varp == &curbuf->b_changed) { + return BOOLEAN_OPTVAL(curbufIsChanged()); + } + + uint32_t flags = options[opt_idx].flags; + + OptValType type = kOptValTypeNil; + if (flags & P_BOOL) { + type = kOptValTypeBoolean; + } else if (flags & P_NUM) { + type = kOptValTypeNumber; + } else if (flags & P_STRING) { + type = kOptValTypeString; + } else { + abort(); + } + + switch (type) { + case kOptValTypeNil: + return NIL_OPTVAL; + case kOptValTypeBoolean: + return BOOLEAN_OPTVAL(varp == NULL ? false : TRISTATE_FROM_INT(*(int *)varp)); + case kOptValTypeNumber: + return NUMBER_OPTVAL(varp == NULL ? 0 : *(OptInt *)varp); + case kOptValTypeString: + return STRING_OPTVAL(varp == NULL ? (String)STRING_INIT : cstr_as_string(*(char **)varp)); + } + UNREACHABLE; +} + +/// Set option var pointer value from Optval. /// -/// @returns: -/// Number option: gov_number, *numval gets value. -/// Tottle option: gov_bool, *numval gets value. -/// String option: gov_string, *stringval gets allocated string. -/// Hidden Number option: gov_hidden_number. -/// Hidden Toggle option: gov_hidden_bool. -/// Hidden String option: gov_hidden_string. -/// Unknown option: gov_unknown. -getoption_T get_option_value(const char *name, long *numval, char **stringval, uint32_t *flagsp, - int scope) +/// @param opt_idx Option index in options[] table. +/// @param[out] varp Pointer to option variable. +/// @param[in] value New option value. +/// @param free_oldval Free old value. +static void set_option_varp(int opt_idx, void *varp, OptVal value, bool free_oldval) + FUNC_ATTR_NONNULL_ARG(2) { - if (get_tty_option(name, stringval)) { - return gov_string; + assert(optval_match_type(value, opt_idx)); + + if (free_oldval) { + optval_free(optval_from_varp(opt_idx, varp)); } - int opt_idx = findoption(name); - if (opt_idx < 0) { // option not in the table - return gov_unknown; + switch (value.type) { + case kOptValTypeNil: + return; + case kOptValTypeBoolean: + *(int *)varp = value.data.boolean; + return; + case kOptValTypeNumber: + *(OptInt *)varp = value.data.number; + return; + case kOptValTypeString: + *(char **)varp = value.data.string.data; + return; } + UNREACHABLE; +} - char_u *varp = (char_u *)get_varp_scope(&(options[opt_idx]), scope); +/// Return C-string representation of OptVal. Caller must free the returned C-string. +static char *optval_to_cstr(OptVal o) +{ + switch (o.type) { + case kOptValTypeNil: + return xstrdup(""); + case kOptValTypeBoolean: + return xstrdup(o.data.boolean ? "true" : "false"); + case kOptValTypeNumber: { + char *buf = xmalloc(NUMBUFLEN); + snprintf(buf, NUMBUFLEN, "%" PRId64, o.data.number); + return buf; + } + case kOptValTypeString: { + char *buf = xmalloc(o.data.string.size + 3); + snprintf(buf, o.data.string.size + 3, "\"%s\"", o.data.string.data); + return buf; + } + } + UNREACHABLE; +} - if (flagsp != NULL) { - // Return the P_xxxx option flags. - *flagsp = options[opt_idx].flags; +/// Convert an OptVal to an API Object. +Object optval_as_object(OptVal o) +{ + switch (o.type) { + case kOptValTypeNil: + return NIL; + case kOptValTypeBoolean: + switch (o.data.boolean) { + case kFalse: + case kTrue: + return BOOLEAN_OBJ(o.data.boolean); + case kNone: + return NIL; + } + UNREACHABLE; + case kOptValTypeNumber: + return INTEGER_OBJ(o.data.number); + case kOptValTypeString: + return STRING_OBJ(o.data.string); + } + UNREACHABLE; +} + +/// Convert an API Object to an OptVal. +OptVal object_as_optval(Object o, bool *error) +{ + switch (o.type) { + case kObjectTypeNil: + return NIL_OPTVAL; + case kObjectTypeBoolean: + return BOOLEAN_OPTVAL(o.data.boolean); + case kObjectTypeInteger: + return NUMBER_OPTVAL((OptInt)o.data.integer); + case kObjectTypeString: + return STRING_OPTVAL(o.data.string); + default: + *error = true; + return NIL_OPTVAL; } + UNREACHABLE; +} - if (options[opt_idx].flags & P_STRING) { - if (varp == NULL) { // hidden option - return gov_hidden_string; +/// Unset the local value of an option. The exact semantics of this depend on the option. +/// TODO(famiu): Remove this once we have a dedicated OptVal type for unset local options. +/// +/// @param opt_idx Option index in options[] table. +/// @param[in] varp Pointer to option variable. +/// +/// @return [allocated] Option value equal to the unset value for the option. +static OptVal optval_unset_local(int opt_idx, void *varp) +{ + vimoption_T *opt = &options[opt_idx]; + // For global-local options, use the unset value of the local value. + if (opt->indir & PV_BOTH) { + // String global-local options always use an empty string for the unset value. + if (opt->flags & P_STRING) { + return STATIC_CSTR_TO_OPTVAL(""); } - if (stringval != NULL) { - if ((char **)varp == &p_pt) { // 'pastetoggle' - *stringval = str2special_save(*(char **)(varp), false, false); - } else { - *stringval = xstrdup(*(char **)(varp)); - } + + if ((int *)varp == &curbuf->b_p_ar) { + return BOOLEAN_OPTVAL(kNone); + } else if ((OptInt *)varp == &curwin->w_p_so || (OptInt *)varp == &curwin->w_p_siso) { + return NUMBER_OPTVAL(-1); + } else if ((OptInt *)varp == &curbuf->b_p_ul) { + return NUMBER_OPTVAL(NO_LOCAL_UNDOLEVEL); + } else { + // This should never happen. + abort(); } - return gov_string; } + // For options that aren't global-local, just set the local value to the global value. + return get_option_value(opt->fullname, NULL, OPT_GLOBAL, NULL); +} + +/// Get an allocated string containing a list of valid types for an option. +/// For options with a singular type, it returns the name of the type. For options with multiple +/// possible types, it returns a slash separated list of types. For example, if an option can be a +/// number, boolean or string, the function returns "Number/Boolean/String" +static char *option_get_valid_types(int opt_idx) +{ + uint32_t flags = options[opt_idx].flags; + uint32_t type_count = 0; - if (varp == NULL) { // hidden option - return (options[opt_idx].flags & P_NUM) ? gov_hidden_number : gov_hidden_bool; + StringBuilder str = KV_INITIAL_VALUE; + kv_resize(str, 32); + +#define OPTION_ADD_TYPE(typename) \ + do { \ + if (type_count == 0) { \ + kv_concat(str, typename); \ + } else { \ + kv_printf(str, "/%s", typename); \ + } \ + type_count++; \ + } while (0); + + if (flags & P_NUM) { + OPTION_ADD_TYPE("Number"); } - if (options[opt_idx].flags & P_NUM) { - *numval = *(long *)varp; - } else { - // Special case: 'modified' is b_changed, but we also want to consider - // it set when 'ff' or 'fenc' changed. - if ((int *)varp == &curbuf->b_changed) { - *numval = curbufIsChanged(); - } else { - *numval = (long)(*(int *)varp); - } + if (flags & P_BOOL) { + OPTION_ADD_TYPE("Boolean"); } - return (options[opt_idx].flags & P_NUM) ? gov_number : gov_bool; + if (flags & P_STRING) { + OPTION_ADD_TYPE("String"); + } + + if (type_count == 0) { + abort(); + } + + // Ensure that the string is NUL-terminated. + kv_push(str, NUL); + return str.items; + +#undef OPTION_ADD_TYPE } -// Returns the option attributes and its value. Unlike the above function it -// will return either global value or local value of the option depending on -// what was requested, but it will never return global value if it was -// requested to return local one and vice versa. Neither it will return -// buffer-local value if it was requested to return window-local one. -// -// Pretends that option is absent if it is not present in the requested scope -// (i.e. has no global, window-local or buffer-local value depending on -// opt_type). -// -// Returned flags: -// 0 hidden or unknown option, also option that does not have requested -// type (see SREQ_* in option_defs.h) -// see SOPT_* in option_defs.h for other flags -// -// Possible opt_type values: see SREQ_* in option_defs.h -int get_option_value_strict(char *name, int64_t *numval, char **stringval, int opt_type, void *from) +/// Gets the value for an option. +/// +/// @param[in] name Option name. +/// @param[out] flagsp Set to the option flags (P_xxxx) (if not NULL). +/// @param[in] scope Option scope (can be OPT_LOCAL, OPT_GLOBAL or a combination). +/// @param[out] hidden Whether option is hidden. +/// +/// @return [allocated] Option value. Returns NIL_OPTVAL for invalid options. +OptVal get_option_value(const char *name, uint32_t *flagsp, int scope, bool *hidden) { - if (get_tty_option(name, stringval)) { - return SOPT_STRING | SOPT_GLOBAL; + // Make sure that hidden and flagsp are never returned uninitialized + if (hidden != NULL) { + *hidden = false; + } + if (flagsp != NULL) { + *flagsp = 0; + } + + char *str; + if (get_tty_option(name, &str)) { + return CSTR_AS_OPTVAL(str); } - int rv = 0; int opt_idx = findoption(name); - if (opt_idx < 0) { - return 0; + if (opt_idx < 0) { // option not in the table + return NIL_OPTVAL; } - vimoption_T *p = &options[opt_idx]; + vimoption_T *opt = &options[opt_idx]; + void *varp = get_varp_scope(opt, scope); - // Hidden option - if (p->var == NULL) { - return 0; + if (hidden != NULL) { + *hidden = varp == NULL; } - if (p->flags & P_BOOL) { - rv |= SOPT_BOOL; - } else if (p->flags & P_NUM) { - rv |= SOPT_NUM; - } else if (p->flags & P_STRING) { - rv |= SOPT_STRING; + if (flagsp != NULL) { + // Return the P_xxxx option flags. + *flagsp = opt->flags; } - if (p->indir == PV_NONE) { - if (opt_type == SREQ_GLOBAL) { - rv |= SOPT_GLOBAL; - } else { - return 0; // Did not request global-only option - } - } else { - if (p->indir & PV_BOTH) { - rv |= SOPT_GLOBAL; - } + return optval_copy(optval_from_varp(opt_idx, varp)); +} - if (p->indir & PV_WIN) { - if (opt_type == SREQ_BUF) { - return 0; // Requested buffer-local, not window-local option - } - rv |= SOPT_WIN; - } else if (p->indir & PV_BUF) { - if (opt_type == SREQ_WIN) { - return 0; // Requested window-local, not buffer-local option - } - rv |= SOPT_BUF; +/// Return information for option at 'opt_idx' +vimoption_T *get_option(int opt_idx) +{ + return &options[opt_idx]; +} + +/// Check if local value of global-local option is unset for current buffer / window. +/// Always returns false for options that aren't global-local. +/// +/// TODO(famiu): Remove this once we have an OptVal type to indicate an unset local value. +static bool is_option_local_value_unset(vimoption_T *opt, buf_T *buf, win_T *win) +{ + // Local value of option that isn't global-local is always considered set. + if (!((int)opt->indir & PV_BOTH)) { + return false; + } + + // Get pointer to local value in varp_local, and a pointer to the currently used value in varp. + // If the local value is the one currently being used, that indicates that it's set. + // Otherwise it indicates the local value is unset. + void *varp = get_varp_from(opt, buf, win); + void *varp_local = get_varp_scope_from(opt, OPT_LOCAL, buf, win); + + return varp != varp_local; +} + +/// Handle side-effects of setting an option. +/// +/// @param opt_idx Index in options[] table. Must be >= 0. +/// @param[in] varp Option variable pointer, cannot be NULL. +/// @param old_value Old option value. +/// @param new_value New option value. +/// @param opt_flags Option flags. +/// @param[out] value_checked Value was checked to be safe, no need to set P_INSECURE. +/// @param value_replaced Value was replaced completely. +/// @param[out] errbuf Buffer for error message. +/// @param errbuflen Length of error buffer. +/// +/// @return NULL on success, an untranslated error message on error. +static const char *did_set_option(int opt_idx, void *varp, OptVal old_value, OptVal new_value, + int opt_flags, bool *value_checked, bool value_replaced, + char *errbuf, size_t errbuflen) +{ + vimoption_T *opt = &options[opt_idx]; + const char *errmsg = NULL; + bool restore_chartab = false; + bool free_oldval = (opt->flags & P_ALLOCED); + bool value_changed = false; + + optset_T did_set_cb_args = { + .os_varp = varp, + .os_idx = opt_idx, + .os_flags = opt_flags, + .os_oldval = old_value.data, + .os_newval = new_value.data, + .os_value_checked = false, + .os_value_changed = false, + .os_restore_chartab = false, + .os_errbuf = errbuf, + .os_errbuflen = errbuflen, + .os_buf = curbuf, + .os_win = curwin + }; + + // Disallow changing immutable options. + if (opt->immutable && !optval_equal(old_value, new_value)) { + errmsg = e_unsupportedoption; + } + // Disallow changing some options from secure mode. + else if ((secure || sandbox != 0) && (opt->flags & P_SECURE)) { + errmsg = e_secure; + } + // Check for a "normal" directory or file name in some string options. + else if (new_value.type == kOptValTypeString + && check_illegal_path_names(*(char **)varp, opt->flags)) { + errmsg = e_invarg; + } else if (opt->opt_did_set_cb != NULL) { + // Invoke the option specific callback function to validate and apply the new value. + errmsg = opt->opt_did_set_cb(&did_set_cb_args); + // The 'filetype' and 'syntax' option callback functions may change the os_value_changed field. + value_changed = did_set_cb_args.os_value_changed; + // The 'keymap', 'filetype' and 'syntax' option callback functions may change the + // os_value_checked field. + *value_checked = did_set_cb_args.os_value_checked; + // The 'isident', 'iskeyword', 'isprint' and 'isfname' options may change the character table. + // On failure, this needs to be restored. + restore_chartab = did_set_cb_args.os_restore_chartab; + } + + // If an error is detected, restore the previous value and don't do any further processing. + if (errmsg != NULL) { + set_option_varp(opt_idx, varp, old_value, true); + // When resetting some values, need to act on it. + if (restore_chartab) { + (void)buf_init_chartab(curbuf, true); } + + // Unset new_value as it is no longer valid. + new_value = NIL_OPTVAL; // NOLINT(clang-analyzer-deadcode.DeadStores) + return errmsg; + } + + // Re-assign the new value as its value may get freed or modified by the option callback. + new_value = optval_from_varp(opt_idx, varp); + + // Remember where the option was set. + set_option_sctx_idx(opt_idx, opt_flags, current_sctx); + // Free options that are in allocated memory. + // Use "free_oldval", because recursiveness may change the flags (esp. init_highlight()). + if (free_oldval) { + optval_free(old_value); + } + opt->flags |= P_ALLOCED; + + // Check the bound for num options. + if (new_value.type == kOptValTypeNumber) { + errmsg = check_num_option_bounds((OptInt *)varp, old_value.data.number, errbuf, errbuflen, + errmsg); + // Re-assign new_value because the new value was modified by the bound check. + new_value = optval_from_varp(opt_idx, varp); + } + + if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0 && (opt->indir & PV_BOTH)) { + // Global option with local value set to use global value. + // Free the local value and clear it. + void *varp_local = get_varp_scope(opt, OPT_LOCAL); + OptVal local_unset_value = optval_unset_local(opt_idx, varp_local); + set_option_varp(opt_idx, varp_local, local_unset_value, true); + } else if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0) { + // May set global value for local option. + void *varp_global = get_varp_scope(opt, OPT_GLOBAL); + set_option_varp(opt_idx, varp_global, optval_copy(new_value), true); + } + + // Trigger the autocommand only after setting the flags. + if (varp == &curbuf->b_p_syn) { + do_syntax_autocmd(curbuf, value_changed); + } else if (varp == &curbuf->b_p_ft) { + // 'filetype' is set, trigger the FileType autocommand + // Skip this when called from a modeline + // Force autocmd when the filetype was changed + if (!(opt_flags & OPT_MODELINE) || value_changed) { + do_filetype_autocmd(curbuf, value_changed); + } + } else if (varp == &curwin->w_s->b_p_spl) { + do_spelllang_source(curwin); + } + + // In case 'columns' or 'ls' changed. + comp_col(); + + if (varp == &p_mouse) { + setmouse(); // in case 'mouse' changed + } else if ((varp == &p_flp || varp == &(curbuf->b_p_flp)) && curwin->w_briopt_list) { + // Changing Formatlistpattern when briopt includes the list setting: + // redraw + redraw_all_later(UPD_NOT_VALID); + } else if (varp == &p_wbr || varp == &(curwin->w_p_wbr)) { + // add / remove window bars for 'winbar' + set_winbar(true); } - if (stringval == NULL) { - return rv; + if (curwin->w_curswant != MAXCOL && (opt->flags & (P_CURSWANT | P_RALL)) != 0) { + curwin->w_set_curswant = true; } - char_u *varp = NULL; + check_redraw(opt->flags); - if (opt_type == SREQ_GLOBAL) { - if (p->var == VAR_WIN) { - return 0; + if (errmsg == NULL) { + uint32_t *p = insecure_flag(curwin, opt_idx, opt_flags); + opt->flags |= P_WAS_SET; + + // When an option is set in the sandbox, from a modeline or in secure mode set the P_INSECURE + // flag. Otherwise, if a new value is stored reset the flag. + if (!value_checked && (secure || sandbox != 0 || (opt_flags & OPT_MODELINE))) { + *p |= P_INSECURE; + } else if (value_replaced) { + *p &= ~P_INSECURE; } - varp = p->var; - } else { - if (opt_type == SREQ_BUF) { - // Special case: 'modified' is b_changed, but we also want to - // consider it set when 'ff' or 'fenc' changed. - if (p->indir == PV_MOD) { - *numval = bufIsChanged((buf_T *)from); - varp = NULL; - } else { - buf_T *save_curbuf = curbuf; - - // only getting a pointer, no need to use aucmd_prepbuf() - curbuf = (buf_T *)from; - curwin->w_buffer = curbuf; - varp = (char_u *)get_varp_scope(p, OPT_LOCAL); - curbuf = save_curbuf; - curwin->w_buffer = curbuf; - } - } else if (opt_type == SREQ_WIN) { - win_T *save_curwin = curwin; - curwin = (win_T *)from; - curbuf = curwin->w_buffer; - varp = (char_u *)get_varp_scope(p, OPT_LOCAL); - curwin = save_curwin; - curbuf = curwin->w_buffer; + } + + return errmsg; +} + +/// Set the value of an option using an OptVal. +/// +/// @param opt_idx Index in options[] table. Must be >= 0. +/// @param[in] varp Option variable pointer, cannot be NULL. +/// @param value New option value. Might get freed. +/// @param opt_flags Option flags. +/// @param value_replaced Value was replaced completely. +/// @param[out] errbuf Buffer for error message. +/// @param errbuflen Length of error buffer. +/// +/// @return NULL on success, an untranslated error message on error. +static const char *set_option(const int opt_idx, void *varp, OptVal value, int opt_flags, + const bool value_replaced, char *errbuf, size_t errbuflen) +{ + assert(opt_idx >= 0 && varp != NULL); + + const char *errmsg = NULL; + bool value_checked = false; + + vimoption_T *opt = &options[opt_idx]; + + static const char *optval_type_names[] = { + [kOptValTypeNil] = "Nil", + [kOptValTypeBoolean] = "Boolean", + [kOptValTypeNumber] = "Number", + [kOptValTypeString] = "String" + }; + + if (value.type == kOptValTypeNil) { + // Don't try to unset local value if scope is global. + // TODO(famiu): Change this to forbid changing all non-local scopes when the API scope bug is + // fixed. + if (opt_flags == OPT_GLOBAL) { + errmsg = _("Cannot unset global option value"); + } else { + optval_free(value); + value = optval_unset_local(opt_idx, varp); } + } else if (!optval_match_type(value, opt_idx)) { + char *rep = optval_to_cstr(value); + char *valid_types = option_get_valid_types(opt_idx); + snprintf(errbuf, IOSIZE, _("Invalid value for option '%s': expected %s, got %s %s"), + opt->fullname, valid_types, optval_type_names[value.type], rep); + xfree(rep); + xfree(valid_types); + errmsg = errbuf; + } - if (varp == p->var) { - return (rv | SOPT_UNSET); + if (errmsg != NULL) { + goto err; + } + + // When using ":set opt=val" for a global option with a local value the local value will be reset, + // use the global value here. + if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0 && ((int)opt->indir & PV_BOTH)) { + varp = opt->var; + } + + OptVal old_value = optval_from_varp(opt_idx, varp); + OptVal old_global_value = NIL_OPTVAL; + OptVal old_local_value = NIL_OPTVAL; + + // Save the local and global values before changing anything. This is needed as for a global-only + // option setting the "local value" in fact sets the global value (since there is only one value). + // + // TODO(famiu): This needs to be changed to use the current type of the old value instead of + // value.type, when multi-type options are added. + if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0) { + old_global_value = optval_from_varp(opt_idx, get_varp_scope(opt, OPT_GLOBAL)); + old_local_value = optval_from_varp(opt_idx, get_varp_scope(opt, OPT_LOCAL)); + + // If local value of global-local option is unset, use global value as local value. + if (is_option_local_value_unset(opt, curbuf, curwin)) { + old_local_value = old_global_value; } } - if (varp != NULL) { - if (p->flags & P_STRING) { - *stringval = *(char **)(varp); - } else if (p->flags & P_NUM) { - *numval = *(long *)varp; - } else { - *numval = *(int *)varp; + // Value that's actually being used. + // For local scope of a global-local option, it is equal to the global value. + // In every other case, it is the same as old_value. + const bool oldval_is_global = ((int)opt->indir & PV_BOTH) && (opt_flags & OPT_LOCAL); + OptVal used_old_value = oldval_is_global ? optval_from_varp(opt_idx, get_varp(opt)) : old_value; + + if (value.type == kOptValTypeNumber) { + errmsg = validate_num_option((OptInt *)varp, &value.data.number); + + // Don't change the value and return early if validation failed. + if (errmsg != NULL) { + goto err; } } - return rv; -} + set_option_varp(opt_idx, varp, value, false); -// Return information for option at 'opt_idx' -vimoption_T *get_option(int opt_idx) -{ - return &options[opt_idx]; + OptVal saved_used_value = optval_copy(used_old_value); + OptVal saved_old_global_value = optval_copy(old_global_value); + OptVal saved_old_local_value = optval_copy(old_local_value); + // New value (and varp) may become invalid if the buffer is closed by autocommands. + OptVal saved_new_value = optval_copy(value); + + uint32_t *p = insecure_flag(curwin, opt_idx, opt_flags); + const int secure_saved = secure; + + // When an option is set in the sandbox, from a modeline or in secure mode, then deal with side + // effects in secure mode. Also when the value was set with the P_INSECURE flag and is not + // completely replaced. + if ((opt_flags & OPT_MODELINE) || sandbox != 0 || (!value_replaced && (*p & P_INSECURE))) { + secure = 1; + } + + errmsg = did_set_option(opt_idx, varp, old_value, value, opt_flags, &value_checked, + value_replaced, errbuf, errbuflen); + + secure = secure_saved; + + if (errmsg == NULL) { + if (!starting) { + apply_optionset_autocmd(opt_idx, opt_flags, saved_used_value, saved_old_global_value, + saved_old_local_value, saved_new_value, errmsg); + } + if (opt->flags & P_UI_OPTION) { + // Calculate saved_new_value again as its value might be changed by bound checks. + // NOTE: Currently there are no buffer/window local UI options, but if there ever are buffer + // or window local UI options added in the future, varp might become invalid if the buffer or + // window is closed during an autocommand, and a check would have to be added for it. + optval_free(saved_new_value); + saved_new_value = optval_copy(optval_from_varp(opt_idx, varp)); + ui_call_option_set(cstr_as_string(opt->fullname), optval_as_object(saved_new_value)); + } + } + + // Free copied values as they are not needed anymore + optval_free(saved_used_value); + optval_free(saved_old_local_value); + optval_free(saved_old_global_value); + optval_free(saved_new_value); + return errmsg; +err: + optval_free(value); + return errmsg; } /// Set the value of an option /// -/// @param[in] name Option name. -/// @param[in] number New value for the number or boolean option. -/// @param[in] string New value for string option. +/// @param[in] name Option name. +/// @param[in] value Option value. If NIL_OPTVAL, the option value is cleared. /// @param[in] opt_flags Flags: OPT_LOCAL, OPT_GLOBAL, or 0 (both). -/// If OPT_CLEAR is set, the value of the option -/// is cleared (the exact semantics of this depend -/// on the option). /// -/// @return NULL on success, an untranslated error message on error. -char *set_option_value(const char *const name, const long number, const char *const string, - const int opt_flags) +/// @return NULL on success, an untranslated error message on error. +const char *set_option_value(const char *const name, const OptVal value, int opt_flags) FUNC_ATTR_NONNULL_ARG(1) { + static char errbuf[IOSIZE]; + if (is_tty_option(name)) { return NULL; // Fail silently; many old vimrcs set t_xx options. } int opt_idx = findoption(name); if (opt_idx < 0) { - semsg(_("E355: Unknown option: %s"), name); - return NULL; + snprintf(errbuf, IOSIZE, _(e_unknown_option2), name); + return errbuf; } uint32_t flags = options[opt_idx].flags; // Disallow changing some options in the sandbox if (sandbox > 0 && (flags & P_SECURE)) { - emsg(_(e_sandbox)); - return NULL; - } - - if (flags & P_STRING) { - const char *s = string; - if (s == NULL || opt_flags & OPT_CLEAR) { - s = ""; - } - return set_string_option(opt_idx, s, opt_flags); + return _(e_sandbox); } - char_u *varp = (char_u *)get_varp_scope(&(options[opt_idx]), opt_flags); + void *varp = get_varp_scope(&(options[opt_idx]), opt_flags); if (varp == NULL) { // hidden option is not changed return NULL; } - if (number == 0 && string != NULL) { - int idx; + const char *errmsg = NULL; - // Either we are given a string or we are setting option - // to zero. - for (idx = 0; string[idx] == '0'; idx++) {} - if (string[idx] != NUL || idx == 0) { - // There's another character after zeros or the string - // is empty. In both cases, we are trying to set a - // num option using a string. - semsg(_("E521: Number required: &%s = '%s'"), - name, string); - return NULL; // do nothing as we hit an error - } - } - long numval = number; - if (opt_flags & OPT_CLEAR) { - if ((int *)varp == &curbuf->b_p_ar) { - numval = -1; - } else if ((long *)varp == &curbuf->b_p_ul) { - numval = NO_LOCAL_UNDOLEVEL; - } else if ((long *)varp == &curwin->w_p_so || (long *)varp == &curwin->w_p_siso) { - numval = -1; - } else { - char *s = NULL; - (void)get_option_value(name, &numval, &s, NULL, OPT_GLOBAL); - } - } - if (flags & P_NUM) { - return set_num_option(opt_idx, varp, numval, NULL, 0, opt_flags); - } - return set_bool_option(opt_idx, varp, (int)numval, opt_flags); + errmsg = set_option(opt_idx, varp, optval_copy(value), opt_flags, true, errbuf, sizeof(errbuf)); + + return errmsg; } /// Call set_option_value() and when an error is returned report it. /// /// @param opt_flags OPT_LOCAL or 0 (both) -void set_option_value_give_err(const char *name, long number, const char *string, int opt_flags) +void set_option_value_give_err(const char *name, OptVal value, int opt_flags) { - char *errmsg = set_option_value(name, number, string, opt_flags); + const char *errmsg = set_option_value(name, value, opt_flags); if (errmsg != NULL) { emsg(_(errmsg)); @@ -3074,14 +3884,6 @@ bool is_option_allocated(const char *name) return idx >= 0 && (options[idx].flags & P_ALLOCED); } -/// Return true if "name" is a string option. -/// Returns false if option "name" does not exist. -bool is_string_option(const char *name) -{ - int idx = findoption(name); - return idx >= 0 && (options[idx].flags & P_STRING); -} - // Translate a string like "t_xx", "<t_xx>" or "<S-Tab>" to a key number. // When "has_lt" is true there is a '<' before "*arg_arg". // Returns 0 when the key is not recognized. @@ -3111,11 +3913,11 @@ static int find_key_option(const char *arg, bool has_lt) return find_key_option_len(arg, strlen(arg), has_lt); } -/// if 'all' == 0: show changed options -/// if 'all' == 1: show all normal options +/// if 'all' == false: show changed options +/// if 'all' == true: show all normal options /// /// @param opt_flags OPT_LOCAL and/or OPT_GLOBAL -static void showoptions(int all, int opt_flags) +static void showoptions(bool all, int opt_flags) { #define INC 20 #define GAP 3 @@ -3144,16 +3946,15 @@ static void showoptions(int all, int opt_flags) continue; } - char_u *varp = NULL; + void *varp = NULL; if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0) { if (p->indir != PV_NONE) { - varp = (char_u *)get_varp_scope(p, opt_flags); + varp = get_varp_scope(p, opt_flags); } } else { varp = get_varp(p); } - if (varp != NULL - && (all == 1 || (all == 0 && !optval_default(p, varp)))) { + if (varp != NULL && (all || !optval_default(p, varp))) { int len; if (opt_flags & OPT_ONECOLUMN) { len = Columns; @@ -3204,13 +4005,13 @@ static void showoptions(int all, int opt_flags) } /// Return true if option "p" has its default value. -static int optval_default(vimoption_T *p, const char_u *varp) +static int optval_default(vimoption_T *p, const void *varp) { if (varp == NULL) { return true; // hidden option is always at default } if (p->flags & P_NUM) { - return *(long *)varp == (long)(intptr_t)p->def_val; + return *(OptInt *)varp == (OptInt)(intptr_t)p->def_val; } if (p->flags & P_BOOL) { return *(int *)varp == (int)(intptr_t)p->def_val; @@ -3233,10 +4034,10 @@ void ui_refresh_options(void) if (flags & P_BOOL) { value = BOOLEAN_OBJ(*(int *)varp); } else if (flags & P_NUM) { - value = INTEGER_OBJ(*(long *)varp); + value = INTEGER_OBJ(*(OptInt *)varp); } else if (flags & P_STRING) { // cstr_as_string handles NULL string - value = STRING_OBJ(cstr_as_string(*(char **)varp)); + value = CSTR_AS_OBJ(*(char **)varp); } ui_call_option_set(name, value); } @@ -3256,7 +4057,7 @@ static void showoneopt(vimoption_T *p, int opt_flags) silent_mode = false; info_message = true; // use os_msg(), not os_errmsg() - char_u *varp = (char_u *)get_varp_scope(p, opt_flags); + void *varp = get_varp_scope(p, opt_flags); // for 'modified' we also need to check if 'ff' or 'fenc' changed. if ((p->flags & P_BOOL) && ((int *)varp == &curbuf->b_changed @@ -3272,7 +4073,7 @@ static void showoneopt(vimoption_T *p, int opt_flags) msg_putchar('='); // put value string in NameBuff option_value2string(p, opt_flags); - msg_outtrans(NameBuff); + msg_outtrans(NameBuff, 0); } silent_mode = save_silent; @@ -3323,23 +4124,23 @@ int makeset(FILE *fd, int opt_flags, int local_only) continue; } - char *varp = get_varp_scope(p, opt_flags); // currently used value + void *varp = get_varp_scope(p, opt_flags); // currently used value // Hidden options are never written. if (!varp) { continue; } // Global values are only written when not at the default value. - if ((opt_flags & OPT_GLOBAL) && optval_default(p, (char_u *)varp)) { + if ((opt_flags & OPT_GLOBAL) && optval_default(p, varp)) { continue; } if ((opt_flags & OPT_SKIPRTP) - && (p->var == (char_u *)&p_rtp || p->var == (char_u *)&p_pp)) { + && (p->var == &p_rtp || p->var == &p_pp)) { continue; } int round = 2; - char_u *varp_local = NULL; // fresh value + void *varp_local = NULL; // fresh value if (p->indir != PV_NONE) { if (p->var == VAR_WIN) { // skip window-local option when only doing globals @@ -3349,11 +4150,11 @@ int makeset(FILE *fd, int opt_flags, int local_only) // When fresh value of window-local option is not at the // default, need to write it too. if (!(opt_flags & OPT_GLOBAL) && !local_only) { - char_u *varp_fresh = (char_u *)get_varp_scope(p, OPT_GLOBAL); // local value + void *varp_fresh = get_varp_scope(p, OPT_GLOBAL); // local value if (!optval_default(p, varp_fresh)) { round = 1; - varp_local = (char_u *)varp; - varp = (char *)varp_fresh; + varp_local = varp; + varp = varp_fresh; } } } @@ -3361,7 +4162,7 @@ int makeset(FILE *fd, int opt_flags, int local_only) // Round 1: fresh value for window-local options. // Round 2: other values - for (; round <= 2; varp = (char *)varp_local, round++) { + for (; round <= 2; varp = varp_local, round++) { char *cmd; if (round == 1 || (opt_flags & OPT_GLOBAL)) { cmd = "set"; @@ -3374,7 +4175,7 @@ int makeset(FILE *fd, int opt_flags, int local_only) return FAIL; } } else if (p->flags & P_NUM) { - if (put_setnum(fd, cmd, p->fullname, (long *)varp) == FAIL) { + if (put_setnum(fd, cmd, p->fullname, (OptInt *)varp) == FAIL) { return FAIL; } } else { // P_STRING @@ -3384,7 +4185,7 @@ int makeset(FILE *fd, int opt_flags, int local_only) // already right, avoids reloading the syntax file. if (p->indir == PV_SYN || p->indir == PV_FT) { if (fprintf(fd, "if &%s != '%s'", p->fullname, - *(char_u **)(varp)) < 0 + *(char **)(varp)) < 0 || put_eol(fd) < 0) { return FAIL; } @@ -3431,20 +4232,10 @@ static int put_setstring(FILE *fd, char *cmd, char *name, char **valuep, uint64_ } char *buf = NULL; - char_u *part = NULL; + char *part = NULL; if (*valuep != NULL) { - // Output 'pastetoggle' as key names. For other - // options some characters have to be escaped with - // CTRL-V or backslash - if (valuep == &p_pt) { - char_u *s = (char_u *)(*valuep); - while (*s != NUL) { - if (put_escstr(fd, (char *)str2special((const char **)&s, false, false), 2) == FAIL) { - return FAIL; - } - } - } else if ((flags & P_EXPAND) != 0) { + if ((flags & P_EXPAND) != 0) { size_t size = (size_t)strlen(*valuep) + 1; // replace home directory in the whole option value into "buf" @@ -3469,8 +4260,8 @@ static int put_setstring(FILE *fd, char *cmd, char *name, char **valuep, uint64_ if (fprintf(fd, "%s %s+=", cmd, name) < 0) { goto fail; } - (void)copy_option_part(&p, (char *)part, size, ","); - if (put_escstr(fd, (char *)part, 2) == FAIL || put_eol(fd) == FAIL) { + (void)copy_option_part(&p, part, size, ","); + if (put_escstr(fd, part, 2) == FAIL || put_eol(fd) == FAIL) { goto fail; } } @@ -3497,15 +4288,15 @@ fail: return FAIL; } -static int put_setnum(FILE *fd, char *cmd, char *name, long *valuep) +static int put_setnum(FILE *fd, char *cmd, char *name, OptInt *valuep) { if (fprintf(fd, "%s %s=", cmd, name) < 0) { return FAIL; } - long wc; - if (wc_use_keyname((char_u *)valuep, &wc)) { + OptInt wc; + if (wc_use_keyname(valuep, &wc)) { // print 'wildchar' and 'wildcharm' as a key name - if (fputs((char *)get_special_key_name((int)wc, 0), fd) < 0) { + if (fputs(get_special_key_name((int)wc, 0), fd) < 0) { return FAIL; } } else if (fprintf(fd, "%" PRId64, (int64_t)(*valuep)) < 0) { @@ -3529,196 +4320,94 @@ static int put_setbool(FILE *fd, char *cmd, char *name, int value) return OK; } -// Unset local option value, similar to ":set opt<". -void unset_global_local_option(char *name, void *from) -{ - vimoption_T *p; - buf_T *buf = (buf_T *)from; - - int opt_idx = findoption(name); - if (opt_idx < 0) { - semsg(_("E355: Unknown option: %s"), name); - return; - } - p = &(options[opt_idx]); - - switch ((int)p->indir) { - // global option with local value: use local value if it's been set - case PV_EP: - clear_string_option(&buf->b_p_ep); - break; - case PV_KP: - clear_string_option(&buf->b_p_kp); - break; - case PV_PATH: - clear_string_option(&buf->b_p_path); - break; - case PV_AR: - buf->b_p_ar = -1; - break; - case PV_BKC: - clear_string_option(&buf->b_p_bkc); - buf->b_bkc_flags = 0; - break; - case PV_TAGS: - clear_string_option(&buf->b_p_tags); - break; - case PV_TC: - clear_string_option(&buf->b_p_tc); - buf->b_tc_flags = 0; - break; - case PV_SISO: - curwin->w_p_siso = -1; - break; - case PV_SO: - curwin->w_p_so = -1; - break; - case PV_DEF: - clear_string_option(&buf->b_p_def); - break; - case PV_INC: - clear_string_option(&buf->b_p_inc); - break; - case PV_DICT: - clear_string_option(&buf->b_p_dict); - break; - case PV_TSR: - clear_string_option(&buf->b_p_tsr); - break; - case PV_TSRFU: - clear_string_option(&buf->b_p_tsrfu); - break; - case PV_FP: - clear_string_option(&buf->b_p_fp); - break; - case PV_EFM: - clear_string_option(&buf->b_p_efm); - break; - case PV_GP: - clear_string_option(&buf->b_p_gp); - break; - case PV_MP: - clear_string_option(&buf->b_p_mp); - break; - case PV_SBR: - clear_string_option(&((win_T *)from)->w_p_sbr); - break; - case PV_STL: - clear_string_option(&((win_T *)from)->w_p_stl); - break; - case PV_WBR: - clear_string_option(&((win_T *)from)->w_p_wbr); - break; - case PV_UL: - buf->b_p_ul = NO_LOCAL_UNDOLEVEL; - break; - case PV_LW: - clear_string_option(&buf->b_p_lw); - break; - case PV_MENC: - clear_string_option(&buf->b_p_menc); - break; - case PV_LCS: - clear_string_option(&((win_T *)from)->w_p_lcs); - set_chars_option((win_T *)from, &((win_T *)from)->w_p_lcs, true); - redraw_later((win_T *)from, UPD_NOT_VALID); - break; - case PV_FCS: - clear_string_option(&((win_T *)from)->w_p_fcs); - set_chars_option((win_T *)from, &((win_T *)from)->w_p_fcs, true); - redraw_later((win_T *)from, UPD_NOT_VALID); - break; - case PV_VE: - clear_string_option(&((win_T *)from)->w_p_ve); - ((win_T *)from)->w_ve_flags = 0; - break; - case PV_STC: - clear_string_option(&((win_T *)from)->w_p_stc); - break; - } -} - -char *get_varp_scope_from(vimoption_T *p, int scope, buf_T *buf, win_T *win) +void *get_varp_scope_from(vimoption_T *p, int scope, buf_T *buf, win_T *win) { if ((scope & OPT_GLOBAL) && p->indir != PV_NONE) { if (p->var == VAR_WIN) { return GLOBAL_WO(get_varp_from(p, buf, win)); } - return (char *)p->var; + return p->var; } if ((scope & OPT_LOCAL) && ((int)p->indir & PV_BOTH)) { switch ((int)p->indir) { case PV_FP: - return (char *)&(buf->b_p_fp); + return &(buf->b_p_fp); case PV_EFM: - return (char *)&(buf->b_p_efm); + return &(buf->b_p_efm); case PV_GP: - return (char *)&(buf->b_p_gp); + return &(buf->b_p_gp); case PV_MP: - return (char *)&(buf->b_p_mp); + return &(buf->b_p_mp); case PV_EP: - return (char *)&(buf->b_p_ep); + return &(buf->b_p_ep); case PV_KP: - return (char *)&(buf->b_p_kp); + return &(buf->b_p_kp); case PV_PATH: - return (char *)&(buf->b_p_path); + return &(buf->b_p_path); case PV_AR: - return (char *)&(buf->b_p_ar); + return &(buf->b_p_ar); case PV_TAGS: - return (char *)&(buf->b_p_tags); + return &(buf->b_p_tags); case PV_TC: - return (char *)&(buf->b_p_tc); + return &(buf->b_p_tc); case PV_SISO: - return (char *)&(win->w_p_siso); + return &(win->w_p_siso); case PV_SO: - return (char *)&(win->w_p_so); + return &(win->w_p_so); case PV_DEF: - return (char *)&(buf->b_p_def); + return &(buf->b_p_def); case PV_INC: - return (char *)&(buf->b_p_inc); + return &(buf->b_p_inc); case PV_DICT: - return (char *)&(buf->b_p_dict); + return &(buf->b_p_dict); case PV_TSR: - return (char *)&(buf->b_p_tsr); + return &(buf->b_p_tsr); case PV_TSRFU: - return (char *)&(buf->b_p_tsrfu); + return &(buf->b_p_tsrfu); case PV_TFU: - return (char *)&(buf->b_p_tfu); + return &(buf->b_p_tfu); case PV_SBR: - return (char *)&(win->w_p_sbr); + return &(win->w_p_sbr); case PV_STL: - return (char *)&(win->w_p_stl); + return &(win->w_p_stl); case PV_WBR: - return (char *)&(win->w_p_wbr); + return &(win->w_p_wbr); case PV_UL: - return (char *)&(buf->b_p_ul); + return &(buf->b_p_ul); case PV_LW: - return (char *)&(buf->b_p_lw); + return &(buf->b_p_lw); case PV_BKC: - return (char *)&(buf->b_p_bkc); + return &(buf->b_p_bkc); case PV_MENC: - return (char *)&(buf->b_p_menc); + return &(buf->b_p_menc); case PV_FCS: - return (char *)&(win->w_p_fcs); + return &(win->w_p_fcs); case PV_LCS: - return (char *)&(win->w_p_lcs); + return &(win->w_p_lcs); case PV_VE: - return (char *)&(win->w_p_ve); + return &(win->w_p_ve); } return NULL; // "cannot happen" } - return (char *)get_varp_from(p, buf, win); + return get_varp_from(p, buf, win); } /// Get pointer to option variable, depending on local or global scope. /// /// @param scope can be OPT_LOCAL, OPT_GLOBAL or a combination. -char *get_varp_scope(vimoption_T *p, int scope) +void *get_varp_scope(vimoption_T *p, int scope) { return get_varp_scope_from(p, scope, curbuf, curwin); } -static char_u *get_varp_from(vimoption_T *p, buf_T *buf, win_T *win) +/// Get pointer to option variable at 'opt_idx', depending on local or global +/// scope. +void *get_option_varp_scope_from(int opt_idx, int scope, buf_T *buf, win_T *win) +{ + return get_varp_scope_from(&(options[opt_idx]), scope, buf, win); +} + +void *get_varp_from(vimoption_T *p, buf_T *buf, win_T *win) { // hidden option, always return NULL if (p->var == NULL) { @@ -3731,309 +4420,284 @@ static char_u *get_varp_from(vimoption_T *p, buf_T *buf, win_T *win) // global option with local value: use local value if it's been set case PV_EP: - return *buf->b_p_ep != NUL - ? (char_u *)&buf->b_p_ep : p->var; + return *buf->b_p_ep != NUL ? &buf->b_p_ep : p->var; case PV_KP: - return *buf->b_p_kp != NUL - ? (char_u *)&buf->b_p_kp : p->var; + return *buf->b_p_kp != NUL ? &buf->b_p_kp : p->var; case PV_PATH: - return *buf->b_p_path != NUL - ? (char_u *)&(buf->b_p_path) : p->var; + return *buf->b_p_path != NUL ? &(buf->b_p_path) : p->var; case PV_AR: - return buf->b_p_ar >= 0 - ? (char_u *)&(buf->b_p_ar) : p->var; + return buf->b_p_ar >= 0 ? &(buf->b_p_ar) : p->var; case PV_TAGS: - return *buf->b_p_tags != NUL - ? (char_u *)&(buf->b_p_tags) : p->var; + return *buf->b_p_tags != NUL ? &(buf->b_p_tags) : p->var; case PV_TC: - return *buf->b_p_tc != NUL - ? (char_u *)&(buf->b_p_tc) : p->var; + return *buf->b_p_tc != NUL ? &(buf->b_p_tc) : p->var; case PV_SISO: - return win->w_p_siso >= 0 - ? (char_u *)&(win->w_p_siso) : p->var; + return win->w_p_siso >= 0 ? &(win->w_p_siso) : p->var; case PV_SO: - return win->w_p_so >= 0 - ? (char_u *)&(win->w_p_so) : p->var; + return win->w_p_so >= 0 ? &(win->w_p_so) : p->var; case PV_BKC: - return *buf->b_p_bkc != NUL - ? (char_u *)&(buf->b_p_bkc) : p->var; + return *buf->b_p_bkc != NUL ? &(buf->b_p_bkc) : p->var; case PV_DEF: - return *buf->b_p_def != NUL - ? (char_u *)&(buf->b_p_def) : p->var; + return *buf->b_p_def != NUL ? &(buf->b_p_def) : p->var; case PV_INC: - return *buf->b_p_inc != NUL - ? (char_u *)&(buf->b_p_inc) : p->var; + return *buf->b_p_inc != NUL ? &(buf->b_p_inc) : p->var; case PV_DICT: - return *buf->b_p_dict != NUL - ? (char_u *)&(buf->b_p_dict) : p->var; + return *buf->b_p_dict != NUL ? &(buf->b_p_dict) : p->var; case PV_TSR: - return *buf->b_p_tsr != NUL - ? (char_u *)&(buf->b_p_tsr) : p->var; + return *buf->b_p_tsr != NUL ? &(buf->b_p_tsr) : p->var; case PV_TSRFU: - return *buf->b_p_tsrfu != NUL - ? (char_u *)&(buf->b_p_tsrfu) : p->var; + return *buf->b_p_tsrfu != NUL ? &(buf->b_p_tsrfu) : p->var; case PV_FP: - return *buf->b_p_fp != NUL - ? (char_u *)&(buf->b_p_fp) : p->var; + return *buf->b_p_fp != NUL ? &(buf->b_p_fp) : p->var; case PV_EFM: - return *buf->b_p_efm != NUL - ? (char_u *)&(buf->b_p_efm) : p->var; + return *buf->b_p_efm != NUL ? &(buf->b_p_efm) : p->var; case PV_GP: - return *buf->b_p_gp != NUL - ? (char_u *)&(buf->b_p_gp) : p->var; + return *buf->b_p_gp != NUL ? &(buf->b_p_gp) : p->var; case PV_MP: - return *buf->b_p_mp != NUL - ? (char_u *)&(buf->b_p_mp) : p->var; + return *buf->b_p_mp != NUL ? &(buf->b_p_mp) : p->var; case PV_SBR: - return *win->w_p_sbr != NUL - ? (char_u *)&(win->w_p_sbr) : p->var; + return *win->w_p_sbr != NUL ? &(win->w_p_sbr) : p->var; case PV_STL: - return *win->w_p_stl != NUL - ? (char_u *)&(win->w_p_stl) : p->var; + return *win->w_p_stl != NUL ? &(win->w_p_stl) : p->var; case PV_WBR: - return *win->w_p_wbr != NUL - ? (char_u *)&(win->w_p_wbr) : p->var; + return *win->w_p_wbr != NUL ? &(win->w_p_wbr) : p->var; case PV_UL: - return buf->b_p_ul != NO_LOCAL_UNDOLEVEL - ? (char_u *)&(buf->b_p_ul) : p->var; + return buf->b_p_ul != NO_LOCAL_UNDOLEVEL ? &(buf->b_p_ul) : p->var; case PV_LW: - return *buf->b_p_lw != NUL - ? (char_u *)&(buf->b_p_lw) : p->var; + return *buf->b_p_lw != NUL ? &(buf->b_p_lw) : p->var; case PV_MENC: - return *buf->b_p_menc != NUL - ? (char_u *)&(buf->b_p_menc) : p->var; + return *buf->b_p_menc != NUL ? &(buf->b_p_menc) : p->var; case PV_FCS: - return *win->w_p_fcs != NUL - ? (char_u *)&(win->w_p_fcs) : p->var; + return *win->w_p_fcs != NUL ? &(win->w_p_fcs) : p->var; case PV_LCS: - return *win->w_p_lcs != NUL - ? (char_u *)&(win->w_p_lcs) : p->var; + return *win->w_p_lcs != NUL ? &(win->w_p_lcs) : p->var; case PV_VE: - return *win->w_p_ve != NUL - ? (char_u *)&win->w_p_ve : p->var; + return *win->w_p_ve != NUL ? &win->w_p_ve : p->var; case PV_ARAB: - return (char_u *)&(win->w_p_arab); + return &(win->w_p_arab); case PV_LIST: - return (char_u *)&(win->w_p_list); + return &(win->w_p_list); case PV_SPELL: - return (char_u *)&(win->w_p_spell); + return &(win->w_p_spell); case PV_CUC: - return (char_u *)&(win->w_p_cuc); + return &(win->w_p_cuc); case PV_CUL: - return (char_u *)&(win->w_p_cul); + return &(win->w_p_cul); case PV_CULOPT: - return (char_u *)&(win->w_p_culopt); + return &(win->w_p_culopt); case PV_CC: - return (char_u *)&(win->w_p_cc); + return &(win->w_p_cc); case PV_DIFF: - return (char_u *)&(win->w_p_diff); + return &(win->w_p_diff); case PV_FDC: - return (char_u *)&(win->w_p_fdc); + return &(win->w_p_fdc); case PV_FEN: - return (char_u *)&(win->w_p_fen); + return &(win->w_p_fen); case PV_FDI: - return (char_u *)&(win->w_p_fdi); + return &(win->w_p_fdi); case PV_FDL: - return (char_u *)&(win->w_p_fdl); + return &(win->w_p_fdl); case PV_FDM: - return (char_u *)&(win->w_p_fdm); + return &(win->w_p_fdm); case PV_FML: - return (char_u *)&(win->w_p_fml); + return &(win->w_p_fml); case PV_FDN: - return (char_u *)&(win->w_p_fdn); + return &(win->w_p_fdn); case PV_FDE: - return (char_u *)&(win->w_p_fde); + return &(win->w_p_fde); case PV_FDT: - return (char_u *)&(win->w_p_fdt); + return &(win->w_p_fdt); case PV_FMR: - return (char_u *)&(win->w_p_fmr); + return &(win->w_p_fmr); case PV_NU: - return (char_u *)&(win->w_p_nu); + return &(win->w_p_nu); case PV_RNU: - return (char_u *)&(win->w_p_rnu); + return &(win->w_p_rnu); case PV_NUW: - return (char_u *)&(win->w_p_nuw); + return &(win->w_p_nuw); case PV_WFH: - return (char_u *)&(win->w_p_wfh); + return &(win->w_p_wfh); case PV_WFW: - return (char_u *)&(win->w_p_wfw); + return &(win->w_p_wfw); case PV_PVW: - return (char_u *)&(win->w_p_pvw); + return &(win->w_p_pvw); case PV_RL: - return (char_u *)&(win->w_p_rl); + return &(win->w_p_rl); case PV_RLC: - return (char_u *)&(win->w_p_rlc); + return &(win->w_p_rlc); case PV_SCROLL: - return (char_u *)&(win->w_p_scr); + return &(win->w_p_scr); + case PV_SMS: + return &(win->w_p_sms); case PV_WRAP: - return (char_u *)&(win->w_p_wrap); + return &(win->w_p_wrap); case PV_LBR: - return (char_u *)&(win->w_p_lbr); + return &(win->w_p_lbr); case PV_BRI: - return (char_u *)&(win->w_p_bri); + return &(win->w_p_bri); case PV_BRIOPT: - return (char_u *)&(win->w_p_briopt); + return &(win->w_p_briopt); case PV_SCBIND: - return (char_u *)&(win->w_p_scb); + return &(win->w_p_scb); case PV_CRBIND: - return (char_u *)&(win->w_p_crb); + return &(win->w_p_crb); case PV_COCU: - return (char_u *)&(win->w_p_cocu); + return &(win->w_p_cocu); case PV_COLE: - return (char_u *)&(win->w_p_cole); + return &(win->w_p_cole); case PV_AI: - return (char_u *)&(buf->b_p_ai); + return &(buf->b_p_ai); case PV_BIN: - return (char_u *)&(buf->b_p_bin); + return &(buf->b_p_bin); case PV_BOMB: - return (char_u *)&(buf->b_p_bomb); + return &(buf->b_p_bomb); case PV_BH: - return (char_u *)&(buf->b_p_bh); + return &(buf->b_p_bh); case PV_BT: - return (char_u *)&(buf->b_p_bt); + return &(buf->b_p_bt); case PV_BL: - return (char_u *)&(buf->b_p_bl); + return &(buf->b_p_bl); case PV_CHANNEL: - return (char_u *)&(buf->b_p_channel); + return &(buf->b_p_channel); case PV_CI: - return (char_u *)&(buf->b_p_ci); + return &(buf->b_p_ci); case PV_CIN: - return (char_u *)&(buf->b_p_cin); + return &(buf->b_p_cin); case PV_CINK: - return (char_u *)&(buf->b_p_cink); + return &(buf->b_p_cink); case PV_CINO: - return (char_u *)&(buf->b_p_cino); + return &(buf->b_p_cino); case PV_CINSD: - return (char_u *)&(buf->b_p_cinsd); + return &(buf->b_p_cinsd); case PV_CINW: - return (char_u *)&(buf->b_p_cinw); + return &(buf->b_p_cinw); case PV_COM: - return (char_u *)&(buf->b_p_com); + return &(buf->b_p_com); case PV_CMS: - return (char_u *)&(buf->b_p_cms); + return &(buf->b_p_cms); case PV_CPT: - return (char_u *)&(buf->b_p_cpt); + return &(buf->b_p_cpt); #ifdef BACKSLASH_IN_FILENAME case PV_CSL: - return (char_u *)&(buf->b_p_csl); + return &(buf->b_p_csl); #endif case PV_CFU: - return (char_u *)&(buf->b_p_cfu); + return &(buf->b_p_cfu); case PV_OFU: - return (char_u *)&(buf->b_p_ofu); + return &(buf->b_p_ofu); case PV_EOF: - return (char_u *)&(buf->b_p_eof); + return &(buf->b_p_eof); case PV_EOL: - return (char_u *)&(buf->b_p_eol); + return &(buf->b_p_eol); case PV_FIXEOL: - return (char_u *)&(buf->b_p_fixeol); + return &(buf->b_p_fixeol); case PV_ET: - return (char_u *)&(buf->b_p_et); + return &(buf->b_p_et); case PV_FENC: - return (char_u *)&(buf->b_p_fenc); + return &(buf->b_p_fenc); case PV_FF: - return (char_u *)&(buf->b_p_ff); + return &(buf->b_p_ff); case PV_FT: - return (char_u *)&(buf->b_p_ft); + return &(buf->b_p_ft); case PV_FO: - return (char_u *)&(buf->b_p_fo); + return &(buf->b_p_fo); case PV_FLP: - return (char_u *)&(buf->b_p_flp); + return &(buf->b_p_flp); case PV_IMI: - return (char_u *)&(buf->b_p_iminsert); + return &(buf->b_p_iminsert); case PV_IMS: - return (char_u *)&(buf->b_p_imsearch); + return &(buf->b_p_imsearch); case PV_INF: - return (char_u *)&(buf->b_p_inf); + return &(buf->b_p_inf); case PV_ISK: - return (char_u *)&(buf->b_p_isk); + return &(buf->b_p_isk); case PV_INEX: - return (char_u *)&(buf->b_p_inex); + return &(buf->b_p_inex); case PV_INDE: - return (char_u *)&(buf->b_p_inde); + return &(buf->b_p_inde); case PV_INDK: - return (char_u *)&(buf->b_p_indk); + return &(buf->b_p_indk); case PV_FEX: - return (char_u *)&(buf->b_p_fex); + return &(buf->b_p_fex); case PV_LISP: - return (char_u *)&(buf->b_p_lisp); + return &(buf->b_p_lisp); case PV_LOP: - return (char_u *)&(buf->b_p_lop); + return &(buf->b_p_lop); case PV_ML: - return (char_u *)&(buf->b_p_ml); + return &(buf->b_p_ml); case PV_MPS: - return (char_u *)&(buf->b_p_mps); + return &(buf->b_p_mps); case PV_MA: - return (char_u *)&(buf->b_p_ma); + return &(buf->b_p_ma); case PV_MOD: - return (char_u *)&(buf->b_changed); + return &(buf->b_changed); case PV_NF: - return (char_u *)&(buf->b_p_nf); + return &(buf->b_p_nf); case PV_PI: - return (char_u *)&(buf->b_p_pi); + return &(buf->b_p_pi); case PV_QE: - return (char_u *)&(buf->b_p_qe); + return &(buf->b_p_qe); case PV_RO: - return (char_u *)&(buf->b_p_ro); + return &(buf->b_p_ro); case PV_SCBK: - return (char_u *)&(buf->b_p_scbk); + return &(buf->b_p_scbk); case PV_SI: - return (char_u *)&(buf->b_p_si); + return &(buf->b_p_si); case PV_STS: - return (char_u *)&(buf->b_p_sts); + return &(buf->b_p_sts); case PV_SUA: - return (char_u *)&(buf->b_p_sua); + return &(buf->b_p_sua); case PV_SWF: - return (char_u *)&(buf->b_p_swf); + return &(buf->b_p_swf); case PV_SMC: - return (char_u *)&(buf->b_p_smc); + return &(buf->b_p_smc); case PV_SYN: - return (char_u *)&(buf->b_p_syn); + return &(buf->b_p_syn); case PV_SPC: - return (char_u *)&(win->w_s->b_p_spc); + return &(win->w_s->b_p_spc); case PV_SPF: - return (char_u *)&(win->w_s->b_p_spf); + return &(win->w_s->b_p_spf); case PV_SPL: - return (char_u *)&(win->w_s->b_p_spl); + return &(win->w_s->b_p_spl); case PV_SPO: - return (char_u *)&(win->w_s->b_p_spo); + return &(win->w_s->b_p_spo); case PV_SW: - return (char_u *)&(buf->b_p_sw); + return &(buf->b_p_sw); case PV_TFU: - return (char_u *)&(buf->b_p_tfu); + return &(buf->b_p_tfu); case PV_TS: - return (char_u *)&(buf->b_p_ts); + return &(buf->b_p_ts); case PV_TW: - return (char_u *)&(buf->b_p_tw); + return &(buf->b_p_tw); case PV_UDF: - return (char_u *)&(buf->b_p_udf); + return &(buf->b_p_udf); case PV_WM: - return (char_u *)&(buf->b_p_wm); + return &(buf->b_p_wm); case PV_VSTS: - return (char_u *)&(buf->b_p_vsts); + return &(buf->b_p_vsts); case PV_VTS: - return (char_u *)&(buf->b_p_vts); + return &(buf->b_p_vts); case PV_KMAP: - return (char_u *)&(buf->b_p_keymap); + return &(buf->b_p_keymap); case PV_SCL: - return (char_u *)&(win->w_p_scl); + return &(win->w_p_scl); case PV_WINHL: - return (char_u *)&(win->w_p_winhl); + return &(win->w_p_winhl); case PV_WINBL: - return (char_u *)&(win->w_p_winbl); + return &(win->w_p_winbl); case PV_STC: - return (char_u *)&(win->w_p_stc); + return &(win->w_p_stc); default: iemsg(_("E356: get_varp ERROR")); } // always return a valid pointer to avoid a crash! - return (char_u *)&(buf->b_p_wm); + return &(buf->b_p_wm); } /// Get pointer to option variable. -static inline char_u *get_varp(vimoption_T *p) +static inline void *get_varp(vimoption_T *p) { return get_varp_from(p, curbuf, curwin); } @@ -4058,8 +4722,8 @@ void win_copy_options(win_T *wp_from, win_T *wp_to) static char *copy_option_val(const char *val) { - if (val == empty_option) { - return empty_option; // no need to allocate memory + if (val == empty_string_option) { + return empty_string_option; // no need to allocate memory } return xstrdup(val); } @@ -4079,7 +4743,7 @@ void copy_winopt(winopt_T *from, winopt_T *to) to->wo_ve = copy_option_val(from->wo_ve); to->wo_ve_flags = from->wo_ve_flags; to->wo_nuw = from->wo_nuw; - to->wo_rl = from->wo_rl; + to->wo_rl = from->wo_rl; to->wo_rlc = copy_option_val(from->wo_rlc); to->wo_sbr = copy_option_val(from->wo_sbr); to->wo_stl = copy_option_val(from->wo_stl); @@ -4091,8 +4755,11 @@ void copy_winopt(winopt_T *from, winopt_T *to) to->wo_briopt = copy_option_val(from->wo_briopt); to->wo_scb = from->wo_scb; to->wo_scb_save = from->wo_scb_save; + to->wo_sms = from->wo_sms; to->wo_crb = from->wo_crb; to->wo_crb_save = from->wo_crb_save; + to->wo_siso = from->wo_siso; + to->wo_so = from->wo_so; to->wo_spell = from->wo_spell; to->wo_cuc = from->wo_cuc; to->wo_cul = from->wo_cul; @@ -4103,7 +4770,7 @@ void copy_winopt(winopt_T *from, winopt_T *to) to->wo_cocu = copy_option_val(from->wo_cocu); to->wo_cole = from->wo_cole; to->wo_fdc = copy_option_val(from->wo_fdc); - to->wo_fdc_save = from->wo_diff_saved ? xstrdup(from->wo_fdc_save) : empty_option; + to->wo_fdc_save = from->wo_diff_saved ? xstrdup(from->wo_fdc_save) : empty_string_option; to->wo_fen = from->wo_fen; to->wo_fen_save = from->wo_fen_save; to->wo_fdi = copy_option_val(from->wo_fdi); @@ -4111,7 +4778,7 @@ void copy_winopt(winopt_T *from, winopt_T *to) to->wo_fdl = from->wo_fdl; to->wo_fdl_save = from->wo_fdl_save; to->wo_fdm = copy_option_val(from->wo_fdm); - to->wo_fdm_save = from->wo_diff_saved ? xstrdup(from->wo_fdm_save) : empty_option; + to->wo_fdm_save = from->wo_diff_saved ? xstrdup(from->wo_fdm_save) : empty_string_option; to->wo_fdn = from->wo_fdn; to->wo_fde = copy_option_val(from->wo_fde); to->wo_fdt = copy_option_val(from->wo_fdt); @@ -4133,7 +4800,7 @@ void check_win_options(win_T *win) check_winopt(&win->w_allbuf_opt); } -/// Check for NULL pointers in a winopt_T and replace them with empty_option. +/// Check for NULL pointers in a winopt_T and replace them with empty_string_option. static void check_winopt(winopt_T *wop) { check_string_option(&wop->wo_fdc); @@ -4192,11 +4859,12 @@ void didset_window_options(win_T *wp, bool valid_cursor) check_colorcolumn(wp); briopt_check(wp); fill_culopt_flags(NULL, wp); - set_chars_option(wp, &wp->w_p_fcs, true); - set_chars_option(wp, &wp->w_p_lcs, true); + set_fillchars_option(wp, wp->w_p_fcs, true); + set_listchars_option(wp, wp->w_p_lcs, true); parse_winhl_opt(wp); // sets w_hl_needs_update also for w_p_winbl check_blending(wp); set_winbar_win(wp, false, valid_cursor); + check_signcolumn(wp); wp->w_grid_alloc.blending = wp->w_p_winbl > 0; } @@ -4286,8 +4954,8 @@ void buf_copy_options(buf_T *buf, int flags) buf->b_p_ff = xstrdup(p_ff); break; } - buf->b_p_bh = empty_option; - buf->b_p_bt = empty_option; + buf->b_p_bh = empty_string_option; + buf->b_p_bt = empty_string_option; } else { free_buf_options(buf, false); } @@ -4348,7 +5016,7 @@ void buf_copy_options(buf_T *buf, int flags) buf->b_p_sts_nopaste = p_sts_nopaste; buf->b_p_vsts = xstrdup(p_vsts); COPY_OPT_SCTX(buf, BV_VSTS); - if (p_vsts && p_vsts != empty_option) { + if (p_vsts && p_vsts != empty_string_option) { (void)tabstop_set(p_vsts, &buf->b_p_vsts_array); } else { buf->b_p_vsts_array = NULL; @@ -4384,7 +5052,7 @@ void buf_copy_options(buf_T *buf, int flags) COPY_OPT_SCTX(buf, BV_LOP); // Don't copy 'filetype', it must be detected - buf->b_p_ft = empty_option; + buf->b_p_ft = empty_string_option; buf->b_p_pi = p_pi; COPY_OPT_SCTX(buf, BV_PI); buf->b_p_cinw = xstrdup(p_cinw); @@ -4392,10 +5060,10 @@ void buf_copy_options(buf_T *buf, int flags) buf->b_p_lisp = p_lisp; COPY_OPT_SCTX(buf, BV_LISP); // Don't copy 'syntax', it must be set - buf->b_p_syn = empty_option; + buf->b_p_syn = empty_string_option; buf->b_p_smc = p_smc; COPY_OPT_SCTX(buf, BV_SMC); - buf->b_s.b_syn_isk = empty_option; + buf->b_s.b_syn_isk = empty_string_option; buf->b_s.b_p_spc = xstrdup(p_spc); COPY_OPT_SCTX(buf, BV_SPC); (void)compile_cap_prog(&buf->b_s); @@ -4409,7 +5077,7 @@ void buf_copy_options(buf_T *buf, int flags) COPY_OPT_SCTX(buf, BV_INDE); buf->b_p_indk = xstrdup(p_indk); COPY_OPT_SCTX(buf, BV_INDK); - buf->b_p_fp = empty_option; + buf->b_p_fp = empty_string_option; buf->b_p_fex = xstrdup(p_fex); COPY_OPT_SCTX(buf, BV_FEX); buf->b_p_sua = xstrdup(p_sua); @@ -4428,30 +5096,30 @@ void buf_copy_options(buf_T *buf, int flags) // are not copied, start using the global value buf->b_p_ar = -1; buf->b_p_ul = NO_LOCAL_UNDOLEVEL; - buf->b_p_bkc = empty_option; + buf->b_p_bkc = empty_string_option; buf->b_bkc_flags = 0; - buf->b_p_gp = empty_option; - buf->b_p_mp = empty_option; - buf->b_p_efm = empty_option; - buf->b_p_ep = empty_option; - buf->b_p_kp = empty_option; - buf->b_p_path = empty_option; - buf->b_p_tags = empty_option; - buf->b_p_tc = empty_option; + buf->b_p_gp = empty_string_option; + buf->b_p_mp = empty_string_option; + buf->b_p_efm = empty_string_option; + buf->b_p_ep = empty_string_option; + buf->b_p_kp = empty_string_option; + buf->b_p_path = empty_string_option; + buf->b_p_tags = empty_string_option; + buf->b_p_tc = empty_string_option; buf->b_tc_flags = 0; - buf->b_p_def = empty_option; - buf->b_p_inc = empty_option; + buf->b_p_def = empty_string_option; + buf->b_p_inc = empty_string_option; buf->b_p_inex = xstrdup(p_inex); COPY_OPT_SCTX(buf, BV_INEX); - buf->b_p_dict = empty_option; - buf->b_p_tsr = empty_option; - buf->b_p_tsrfu = empty_option; + buf->b_p_dict = empty_string_option; + buf->b_p_tsr = empty_string_option; + buf->b_p_tsrfu = empty_string_option; buf->b_p_qe = xstrdup(p_qe); COPY_OPT_SCTX(buf, BV_QE); buf->b_p_udf = p_udf; COPY_OPT_SCTX(buf, BV_UDF); - buf->b_p_lw = empty_option; - buf->b_p_menc = empty_option; + buf->b_p_lw = empty_string_option; + buf->b_p_menc = empty_string_option; // Don't copy the options set by ex_help(), use the saved values, // when going from a help buffer to a non-help buffer. @@ -4459,7 +5127,7 @@ void buf_copy_options(buf_T *buf, int flags) // or to a help buffer. if (dont_do_help) { buf->b_p_isk = save_p_isk; - if (p_vts && p_vts != empty_option && !buf->b_p_vts_array) { + if (p_vts && p_vts != empty_string_option && !buf->b_p_vts_array) { (void)tabstop_set(p_vts, &buf->b_p_vts_array); } else { buf->b_p_vts_array = NULL; @@ -4472,7 +5140,7 @@ void buf_copy_options(buf_T *buf, int flags) COPY_OPT_SCTX(buf, BV_TS); buf->b_p_vts = xstrdup(p_vts); COPY_OPT_SCTX(buf, BV_VTS); - if (p_vts && p_vts != empty_option && !buf->b_p_vts_array) { + if (p_vts && p_vts != empty_string_option && !buf->b_p_vts_array) { (void)tabstop_set(p_vts, &buf->b_p_vts_array); } else { buf->b_p_vts_array = NULL; @@ -4525,8 +5193,10 @@ void set_imsearch_global(buf_T *buf) } static int expand_option_idx = -1; -static char_u expand_option_name[5] = { 't', '_', NUL, NUL, NUL }; +static int expand_option_start_col = 0; +static char expand_option_name[5] = { 't', '_', NUL, NUL, NUL }; static int expand_option_flags = 0; +static bool expand_option_append = false; /// @param opt_flags OPT_GLOBAL and/or OPT_LOCAL void set_context_in_set_cmd(expand_T *xp, char *arg, int opt_flags) @@ -4560,10 +5230,11 @@ void set_context_in_set_cmd(expand_T *xp, char *arg, int opt_flags) } if (strncmp(p, "no", 2) == 0) { xp->xp_context = EXPAND_BOOL_SETTINGS; + xp->xp_prefix = XP_PREFIX_NO; p += 2; - } - if (strncmp(p, "inv", 3) == 0) { + } else if (strncmp(p, "inv", 3) == 0) { xp->xp_context = EXPAND_BOOL_SETTINGS; + xp->xp_prefix = XP_PREFIX_INV; p += 3; } xp->xp_pattern = p; @@ -4580,15 +5251,15 @@ void set_context_in_set_cmd(expand_T *xp, char *arg, int opt_flags) return; } } - int key = get_special_key_code((char_u *)arg + 1); + int key = get_special_key_code(arg + 1); if (key == 0) { // unknown name xp->xp_context = EXPAND_NOTHING; return; } nextchar = *++p; is_term_option = true; - expand_option_name[2] = (char_u)KEY2TERMCAP0(key); - expand_option_name[3] = KEY2TERMCAP1(key); + expand_option_name[2] = (char)(uint8_t)KEY2TERMCAP0(key); + expand_option_name[3] = (char)(uint8_t)KEY2TERMCAP1(key); } else { if (p[0] == 't' && p[1] == '_') { p += 2; @@ -4600,8 +5271,8 @@ void set_context_in_set_cmd(expand_T *xp, char *arg, int opt_flags) } nextchar = *++p; is_term_option = true; - expand_option_name[2] = (char_u)p[-2]; - expand_option_name[3] = (char_u)p[-1]; + expand_option_name[2] = p[-2]; + expand_option_name[3] = p[-1]; } else { // Allow * wildcard. while (ASCII_ISALNUM(*p) || *p == '_' || *p == '*') { @@ -4611,7 +5282,7 @@ void set_context_in_set_cmd(expand_T *xp, char *arg, int opt_flags) return; } nextchar = *p; - opt_idx = findoption_len((const char *)arg, (size_t)(p - arg)); + opt_idx = findoption_len(arg, (size_t)(p - arg)); if (opt_idx == -1 || options[opt_idx].var == NULL) { xp->xp_context = EXPAND_NOTHING; return; @@ -4624,7 +5295,15 @@ void set_context_in_set_cmd(expand_T *xp, char *arg, int opt_flags) } } // handle "-=" and "+=" + expand_option_append = false; + bool expand_option_subtract = false; if ((nextchar == '-' || nextchar == '+' || nextchar == '^') && p[1] == '=') { + if (nextchar == '-') { + expand_option_subtract = true; + } + if (nextchar == '+' || nextchar == '^') { + expand_option_append = true; + } p++; nextchar = '='; } @@ -4633,25 +5312,53 @@ void set_context_in_set_cmd(expand_T *xp, char *arg, int opt_flags) xp->xp_context = EXPAND_UNSUCCESSFUL; return; } - if (p[1] == NUL) { + + // Below are for handling expanding a specific option's value after the '=' or ':' + + if (is_term_option) { + expand_option_idx = -1; + } else { + expand_option_idx = opt_idx; + } + + xp->xp_pattern = p + 1; + expand_option_start_col = (int)(p + 1 - xp->xp_line); + + // Certain options currently have special case handling to reuse the + // expansion logic with other commands. + if (options[opt_idx].var == &p_syn) { + xp->xp_context = EXPAND_OWNSYNTAX; + return; + } + if (options[opt_idx].var == &p_ft) { + xp->xp_context = EXPAND_FILETYPE; + return; + } + + // Now pick. If the option has a custom expander, use that. Otherwise, just + // fill with the existing option value. + if (expand_option_subtract) { + xp->xp_context = EXPAND_SETTING_SUBTRACT; + return; + } else if (expand_option_idx >= 0 + && options[expand_option_idx].opt_expand_cb != NULL) { + xp->xp_context = EXPAND_STRING_SETTING; + } else if (*xp->xp_pattern == NUL) { xp->xp_context = EXPAND_OLD_SETTING; - if (is_term_option) { - expand_option_idx = -1; - } else { - expand_option_idx = opt_idx; - } - xp->xp_pattern = p + 1; return; + } else { + xp->xp_context = EXPAND_NOTHING; } - xp->xp_context = EXPAND_NOTHING; + if (is_term_option || (flags & P_NUM)) { return; } - xp->xp_pattern = p + 1; + // Only string options below + // Options that have P_EXPAND are considered to all use file/dir expansion. if (flags & P_EXPAND) { - p = (char *)options[opt_idx].var; + p = options[opt_idx].var; if (p == (char *)&p_bdir || p == (char *)&p_dir || p == (char *)&p_path @@ -4665,8 +5372,6 @@ void set_context_in_set_cmd(expand_T *xp, char *arg, int opt_flags) } else { xp->xp_backslash = XP_BS_ONE; } - } else if (p == (char *)&p_ft) { - xp->xp_context = EXPAND_FILETYPE; } else { xp->xp_context = EXPAND_FILES; // for 'tags' need three backslashes for a space @@ -4676,29 +5381,54 @@ void set_context_in_set_cmd(expand_T *xp, char *arg, int opt_flags) xp->xp_backslash = XP_BS_ONE; } } + if (flags & P_COMMA) { + xp->xp_backslash |= XP_BS_COMMA; + } } - // For an option that is a list of file names, find the start of the - // last file name. - for (p = arg + strlen(arg) - 1; p > xp->xp_pattern; p--) { - // count number of backslashes before ' ' or ',' - if (*p == ' ' || *p == ',') { - char *s = p; - while (s > xp->xp_pattern && *(s - 1) == '\\') { - s--; - } - if ((*p == ' ' && (xp->xp_backslash == XP_BS_THREE && (p - s) < 3)) - || (*p == ',' && (flags & P_COMMA) && ((p - s) & 1) == 0)) { - xp->xp_pattern = p + 1; - break; + // For an option that is a list of file names, or comma/colon-separated + // values, split it by the delimiter and find the start of the current + // pattern, while accounting for backslash-escaped space/commas/colons. + // Triple-backslashed escaped file names (e.g. 'path') can also be + // delimited by space. + if ((flags & P_EXPAND) || (flags & P_COMMA) || (flags & P_COLON)) { + for (p = arg + strlen(arg) - 1; p > xp->xp_pattern; p--) { + // count number of backslashes before ' ' or ',' + if (*p == ' ' || *p == ',' || (*p == ':' && (flags & P_COLON))) { + char *s = p; + while (s > xp->xp_pattern && *(s - 1) == '\\') { + s--; + } + if ((*p == ' ' && ((xp->xp_backslash & XP_BS_THREE) && (p - s) < 3)) +#if defined(BACKSLASH_IN_FILENAME) + || (*p == ',' && (flags & P_COMMA) && (p - s) < 1) +#else + || (*p == ',' && (flags & P_COMMA) && (p - s) < 2) +#endif + || (*p == ':' && (flags & P_COLON))) { + xp->xp_pattern = p + 1; + break; + } } } + } - // for 'spellsuggest' start at "file:" - if (options[opt_idx].var == (char_u *)&p_sps - && strncmp(p, "file:", 5) == 0) { - xp->xp_pattern = p + 5; - break; + // An option that is a list of single-character flags should always start + // at the end as we don't complete words. + if (flags & P_FLAGLIST) { + xp->xp_pattern = arg + strlen(arg); + } + + // Some options can either be using file/dir expansions, or custom value + // expansion depending on what the user typed. Unfortunately we have to + // manually handle it here to make sure we have the correct xp_context set. + // for 'spellsuggest' start at "file:" + if (options[opt_idx].var == &p_sps) { + if (strncmp(xp->xp_pattern, "file:", 5) == 0) { + xp->xp_pattern += 5; + return; + } else if (options[expand_option_idx].opt_expand_cb != NULL) { + xp->xp_context = EXPAND_STRING_SETTING; } } } @@ -4722,7 +5452,7 @@ static bool match_str(char *const str, regmatch_T *const regmatch, char **const const char *const fuzzystr, fuzmatch_str_T *const fuzmatch) { if (!fuzzy) { - if (vim_regexec(regmatch, str, (colnr_T)0)) { + if (vim_regexec(regmatch, str, 0)) { if (!test_only) { matches[idx] = xstrdup(str); } @@ -4757,10 +5487,9 @@ int ExpandSettings(expand_T *xp, regmatch_T *regmatch, char *fuzzystr, int *numM // loop == 0: count the number of matching options // loop == 1: copy the matching options into allocated memory for (int loop = 0; loop <= 1; loop++) { - int match; regmatch->rm_ic = ic; if (xp->xp_context != EXPAND_BOOL_SETTINGS) { - for (match = 0; match < (int)ARRAY_SIZE(names); + for (int match = 0; match < (int)ARRAY_SIZE(names); match++) { if (match_str(names[match], regmatch, *matches, count, (loop == 0), fuzzy, fuzzystr, fuzmatch)) { @@ -4791,7 +5520,7 @@ int ExpandSettings(expand_T *xp, regmatch_T *regmatch, char *fuzzystr, int *numM count++; } } else if (!fuzzy && options[opt_idx].shortname != NULL - && vim_regexec(regmatch, options[opt_idx].shortname, (colnr_T)0)) { + && vim_regexec(regmatch, options[opt_idx].shortname, 0)) { // Compare against the abbreviated option name (for regular // expression match). Fuzzy matching (previous if) already // matches against both the expanded and abbreviated names. @@ -4824,16 +5553,42 @@ int ExpandSettings(expand_T *xp, regmatch_T *regmatch, char *fuzzystr, int *numM return OK; } -void ExpandOldSetting(int *num_file, char ***file) +/// Escape an option value that can be used on the command-line with :set. +/// Caller needs to free the returned string, unless NULL is returned. +static char *escape_option_str_cmdline(char *var) +{ + // A backslash is required before some characters. This is the reverse of + // what happens in do_set(). + char *buf = vim_strsave_escaped(var, escape_chars); + +#ifdef BACKSLASH_IN_FILENAME + // For MS-Windows et al. we don't double backslashes at the start and + // before a file name character. + // The reverse is found at stropt_copy_value(). + for (var = buf; *var != NUL; MB_PTR_ADV(var)) { + if (var[0] == '\\' && var[1] == '\\' + && expand_option_idx >= 0 + && (options[expand_option_idx].flags & P_EXPAND) + && vim_isfilec((uint8_t)var[2]) + && (var[2] != '\\' || (var == buf && var[4] != '\\'))) { + STRMOVE(var, var + 1); + } + } +#endif + return buf; +} + +/// Expansion handler for :set= when we just want to fill in with the existing value. +int ExpandOldSetting(int *numMatches, char ***matches) { char *var = NULL; - *num_file = 0; - *file = xmalloc(sizeof(char_u *)); + *numMatches = 0; + *matches = xmalloc(sizeof(char *)); // For a terminal key code expand_option_idx is < 0. if (expand_option_idx < 0) { - expand_option_idx = findoption((const char *)expand_option_name); + expand_option_idx = findoption(expand_option_name); } if (expand_option_idx >= 0) { @@ -4844,26 +5599,149 @@ void ExpandOldSetting(int *num_file, char ***file) var = ""; } - // A backslash is required before some characters. This is the reverse of - // what happens in do_set(). - char *buf = vim_strsave_escaped(var, escape_chars); + char *buf = escape_option_str_cmdline(var); -#ifdef BACKSLASH_IN_FILENAME - // For MS-Windows et al. we don't double backslashes at the start and - // before a file name character. - for (var = buf; *var != NUL; MB_PTR_ADV(var)) { - if (var[0] == '\\' && var[1] == '\\' - && expand_option_idx >= 0 - && (options[expand_option_idx].flags & P_EXPAND) - && vim_isfilec((uint8_t)var[2]) - && (var[2] != '\\' || (var == buf && var[4] != '\\'))) { - STRMOVE(var, var + 1); + (*matches)[0] = buf; + *numMatches = 1; + return OK; +} + +/// Expansion handler for :set=/:set+= when the option has a custom expansion handler. +int ExpandStringSetting(expand_T *xp, regmatch_T *regmatch, int *numMatches, char ***matches) +{ + if (expand_option_idx < 0 + || options[expand_option_idx].opt_expand_cb == NULL) { + // Not supposed to reach this. This function is only for options with + // custom expansion callbacks. + return FAIL; + } + + optexpand_T args = { + .oe_varp = get_varp_scope(&options[expand_option_idx], expand_option_flags), + .oe_append = expand_option_append, + .oe_regmatch = regmatch, + .oe_xp = xp, + .oe_set_arg = xp->xp_line + expand_option_start_col, + }; + args.oe_include_orig_val = !expand_option_append && (*args.oe_set_arg == NUL); + + // Retrieve the existing value, but escape it as a reverse of setting it. + // We technically only need to do this when oe_append or + // oe_include_orig_val is true. + option_value2string(&options[expand_option_idx], expand_option_flags); + char *var = NameBuff; + char *buf = escape_option_str_cmdline(var); + args.oe_opt_value = buf; + + int num_ret = options[expand_option_idx].opt_expand_cb(&args, numMatches, matches); + + xfree(buf); + return num_ret; +} + +/// Expansion handler for :set-= +int ExpandSettingSubtract(expand_T *xp, regmatch_T *regmatch, int *numMatches, char ***matches) +{ + if (expand_option_idx < 0) { + // term option + return ExpandOldSetting(numMatches, matches); + } + + char *option_val = *(char **)get_option_varp_scope_from(expand_option_idx, + expand_option_flags, + curbuf, curwin); + + uint32_t option_flags = options[expand_option_idx].flags; + + if (option_flags & P_NUM) { + return ExpandOldSetting(numMatches, matches); + } else if (option_flags & P_COMMA) { + // Split the option by comma, then present each option to the user if + // it matches the pattern. + // This condition needs to go first, because 'whichwrap' has both + // P_COMMA and P_FLAGLIST. + + if (*option_val == NUL) { + return FAIL; } + + // Make a copy as we need to inject null characters destructively. + char *option_copy = xstrdup(option_val); + char *next_val = option_copy; + + garray_T ga; + ga_init(&ga, sizeof(char *), 10); + + do { + char *item = next_val; + char *comma = vim_strchr(next_val, ','); + while (comma != NULL && comma != next_val && *(comma - 1) == '\\') { + // "\," is interpreted as a literal comma rather than option + // separator when reading options in copy_option_part(). Skip + // it. + comma = vim_strchr(comma + 1, ','); + } + if (comma != NULL) { + *comma = NUL; // null-terminate this value, required by later functions + next_val = comma + 1; + } else { + next_val = NULL; + } + + if (*item == NUL) { + // empty value, don't add to list + continue; + } + + if (!vim_regexec(regmatch, item, 0)) { + continue; + } + + char *buf = escape_option_str_cmdline(item); + GA_APPEND(char *, &ga, buf); + } while (next_val != NULL); + + xfree(option_copy); + + *matches = ga.ga_data; + *numMatches = ga.ga_len; + return OK; + } else if (option_flags & P_FLAGLIST) { + // Only present the flags that are set on the option as the other flags + // are not meaningful to do set-= on. + + if (*xp->xp_pattern != NUL) { + // Don't suggest anything if cmdline is non-empty. Vim's set-= + // behavior requires consecutive strings and it's usually + // unintuitive to users if they try to subtract multiple flags at + // once. + return FAIL; + } + + size_t num_flags = strlen(option_val); + if (num_flags == 0) { + return FAIL; + } + + *matches = xmalloc(sizeof(char *) * (num_flags + 1)); + + int count = 0; + + (*matches)[count++] = xstrdup(option_val); + + if (num_flags > 1) { + // If more than one flags, split the flags up and expose each + // character as individual choice. + for (char *flag = option_val; *flag != NUL; flag++) { + (*matches)[count++] = xmemdupz(flag, 1); + } + } + + *numMatches = count; + return OK; } -#endif - *file[0] = buf; - *num_file = 1; + return ExpandOldSetting(numMatches, matches); } /// Get the value for the numeric or string option///opp in a nice format into @@ -4872,30 +5750,27 @@ void ExpandOldSetting(int *num_file, char ***file) /// @param opt_flags OPT_GLOBAL and/or OPT_LOCAL static void option_value2string(vimoption_T *opp, int scope) { - char *varp = get_varp_scope(opp, scope); + void *varp = get_varp_scope(opp, scope); if (opp->flags & P_NUM) { - long wc = 0; + OptInt wc = 0; - if (wc_use_keyname((char_u *)varp, &wc)) { - xstrlcpy(NameBuff, (char *)get_special_key_name((int)wc, 0), sizeof(NameBuff)); + if (wc_use_keyname(varp, &wc)) { + xstrlcpy(NameBuff, get_special_key_name((int)wc, 0), sizeof(NameBuff)); } else if (wc != 0) { xstrlcpy(NameBuff, transchar((int)wc), sizeof(NameBuff)); } else { snprintf(NameBuff, sizeof(NameBuff), "%" PRId64, - (int64_t)(*(long *)varp)); + (int64_t)(*(OptInt *)varp)); } } else { // P_STRING varp = *(char **)(varp); if (varp == NULL) { // Just in case. NameBuff[0] = NUL; } else if (opp->flags & P_EXPAND) { - home_replace(NULL, varp, (char *)NameBuff, MAXPATHL, false); - // Translate 'pastetoggle' into special key names. - } else if ((char **)opp->var == &p_pt) { - str2specialbuf((const char *)p_pt, NameBuff, MAXPATHL); + home_replace(NULL, varp, NameBuff, MAXPATHL, false); } else { xstrlcpy(NameBuff, varp, MAXPATHL); } @@ -4905,10 +5780,10 @@ static void option_value2string(vimoption_T *opp, int scope) /// Return true if "varp" points to 'wildchar' or 'wildcharm' and it can be /// printed as a keyname. /// "*wcp" is set to the value of the option if it's 'wildchar' or 'wildcharm'. -static int wc_use_keyname(const char_u *varp, long *wcp) +static int wc_use_keyname(const void *varp, OptInt *wcp) { - if (((long *)varp == &p_wc) || ((long *)varp == &p_wcm)) { - *wcp = *(long *)varp; + if (((OptInt *)varp == &p_wc) || ((OptInt *)varp == &p_wcm)) { + *wcp = *(OptInt *)varp; if (IS_SPECIAL(*wcp) || find_special_key_in_table((int)(*wcp)) >= 0) { return true; } @@ -4926,133 +5801,6 @@ bool shortmess(int x) && vim_strchr(SHM_ALL_ABBREVIATIONS, x) != NULL))); } -/// paste_option_changed() - Called after p_paste was set or reset. -static void paste_option_changed(void) -{ - static int old_p_paste = false; - static int save_sm = 0; - static int save_sta = 0; - static int save_ru = 0; - static int save_ri = 0; - static int save_hkmap = 0; - - if (p_paste) { - // Paste switched from off to on. - // Save the current values, so they can be restored later. - if (!old_p_paste) { - // save options for each buffer - FOR_ALL_BUFFERS(buf) { - buf->b_p_tw_nopaste = buf->b_p_tw; - buf->b_p_wm_nopaste = buf->b_p_wm; - buf->b_p_sts_nopaste = buf->b_p_sts; - buf->b_p_ai_nopaste = buf->b_p_ai; - buf->b_p_et_nopaste = buf->b_p_et; - if (buf->b_p_vsts_nopaste) { - xfree(buf->b_p_vsts_nopaste); - } - buf->b_p_vsts_nopaste = buf->b_p_vsts && buf->b_p_vsts != empty_option - ? xstrdup(buf->b_p_vsts) - : NULL; - } - - // save global options - save_sm = p_sm; - save_sta = p_sta; - save_ru = p_ru; - save_ri = p_ri; - save_hkmap = p_hkmap; - // save global values for local buffer options - p_ai_nopaste = p_ai; - p_et_nopaste = p_et; - p_sts_nopaste = p_sts; - p_tw_nopaste = p_tw; - p_wm_nopaste = p_wm; - if (p_vsts_nopaste) { - xfree(p_vsts_nopaste); - } - p_vsts_nopaste = p_vsts && p_vsts != empty_option ? xstrdup(p_vsts) : NULL; - } - - // Always set the option values, also when 'paste' is set when it is - // already on. - // set options for each buffer - FOR_ALL_BUFFERS(buf) { - buf->b_p_tw = 0; // textwidth is 0 - buf->b_p_wm = 0; // wrapmargin is 0 - buf->b_p_sts = 0; // softtabstop is 0 - buf->b_p_ai = 0; // no auto-indent - buf->b_p_et = 0; // no expandtab - if (buf->b_p_vsts) { - free_string_option(buf->b_p_vsts); - } - buf->b_p_vsts = empty_option; - XFREE_CLEAR(buf->b_p_vsts_array); - } - - // set global options - p_sm = 0; // no showmatch - p_sta = 0; // no smarttab - if (p_ru) { - status_redraw_all(); // redraw to remove the ruler - } - p_ru = 0; // no ruler - p_ri = 0; // no reverse insert - p_hkmap = 0; // no Hebrew keyboard - // set global values for local buffer options - p_tw = 0; - p_wm = 0; - p_sts = 0; - p_ai = 0; - if (p_vsts) { - free_string_option(p_vsts); - } - p_vsts = empty_option; - } else if (old_p_paste) { - // Paste switched from on to off: Restore saved values. - - // restore options for each buffer - FOR_ALL_BUFFERS(buf) { - buf->b_p_tw = buf->b_p_tw_nopaste; - buf->b_p_wm = buf->b_p_wm_nopaste; - buf->b_p_sts = buf->b_p_sts_nopaste; - buf->b_p_ai = buf->b_p_ai_nopaste; - buf->b_p_et = buf->b_p_et_nopaste; - if (buf->b_p_vsts) { - free_string_option(buf->b_p_vsts); - } - buf->b_p_vsts = buf->b_p_vsts_nopaste ? xstrdup(buf->b_p_vsts_nopaste) : empty_option; - xfree(buf->b_p_vsts_array); - if (buf->b_p_vsts && buf->b_p_vsts != empty_option) { - (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array); - } else { - buf->b_p_vsts_array = NULL; - } - } - - // restore global options - p_sm = save_sm; - p_sta = save_sta; - if (p_ru != save_ru) { - status_redraw_all(); // redraw to draw the ruler - } - p_ru = save_ru; - p_ri = save_ri; - p_hkmap = save_hkmap; - // set global values for local buffer options - p_ai = p_ai_nopaste; - p_et = p_et_nopaste; - p_sts = p_sts_nopaste; - p_tw = p_tw_nopaste; - p_wm = p_wm_nopaste; - if (p_vsts) { - free_string_option(p_vsts); - } - p_vsts = p_vsts_nopaste ? xstrdup(p_vsts_nopaste) : empty_option; - } - - old_p_paste = p_paste; -} - /// vimrc_found() - Called when a vimrc or "VIMINIT" has been found. /// /// Set the values for options that didn't get set yet to the defaults. @@ -5105,25 +5853,11 @@ void reset_option_was_set(const char *name) options[idx].flags &= ~P_WAS_SET; } -/// fill_breakat_flags() -- called when 'breakat' changes value. -void fill_breakat_flags(void) -{ - for (int i = 0; i < 256; i++) { - breakat_flags[i] = false; - } - - if (p_breakat != NULL) { - for (char_u *p = (char_u *)p_breakat; *p; p++) { - breakat_flags[*p] = true; - } - } -} - /// fill_culopt_flags() -- called when 'culopt' changes value int fill_culopt_flags(char *val, win_T *wp) { char *p; - char_u culopt_flags_new = 0; + uint8_t culopt_flags_new = 0; if (val == NULL) { p = wp->w_p_culopt; @@ -5131,6 +5865,7 @@ int fill_culopt_flags(char *val, win_T *wp) p = val; } while (*p != NUL) { + // Note: Keep this in sync with p_culopt_values. if (strncmp(p, "line", 4) == 0) { p += 4; culopt_flags_new |= CULOPT_LINE; @@ -5191,7 +5926,7 @@ int option_set_callback_func(char *optval, Callback *optcb) || (strncmp(optval, "function(", 9) == 0) || (strncmp(optval, "funcref(", 8) == 0)) { // Lambda expression or a funcref - tv = eval_expr(optval); + tv = eval_expr(optval, NULL); if (tv == NULL) { return FAIL; } @@ -5214,6 +5949,20 @@ int option_set_callback_func(char *optval, Callback *optcb) return OK; } +static void didset_options_sctx(int opt_flags, char **buf) +{ + for (int i = 0;; i++) { + if (buf[i] == NULL) { + break; + } + + int idx = findoption(buf[i]); + if (idx >= 0) { + set_option_sctx_idx(idx, opt_flags, current_sctx); + } + } +} + /// Check if backspacing over something is allowed. /// @param what BS_INDENT, BS_EOL, BS_START, or BS_NOSTOP bool can_bs(int what) @@ -5221,23 +5970,20 @@ bool can_bs(int what) if (what == BS_START && bt_prompt(curbuf)) { return false; } - switch (*p_bs) { - case '3': - return true; - case '2': + + // support for number values was removed but we keep '2' since it is used in + // legacy tests + if (*p_bs == '2') { return what != BS_NOSTOP; - case '1': - return what != BS_START; - case '0': - return false; } + return vim_strchr(p_bs, what) != NULL; } /// Get the local or global value of 'backupcopy'. /// /// @param buf The buffer. -unsigned int get_bkc_value(buf_T *buf) +unsigned get_bkc_value(buf_T *buf) { return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags; } @@ -5254,7 +6000,7 @@ char *get_flp_value(buf_T *buf) } /// Get the local or global value of the 'virtualedit' flags. -unsigned int get_ve_flags(void) +unsigned get_ve_flags(void) { return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags) & ~(VE_NONE | VE_NONEU); } @@ -5270,7 +6016,7 @@ char *get_showbreak_value(win_T *const win) return p_sbr; } if (strcmp(win->w_p_sbr, "NONE") == 0) { - return empty_option; + return empty_string_option; } return win->w_p_sbr; } @@ -5428,53 +6174,12 @@ bool fish_like_shell(void) /// buffer signs and on user configuration. int win_signcol_count(win_T *wp) { - return win_signcol_configured(wp, NULL); -} - -/// Return the number of requested sign columns, based on user / configuration. -int win_signcol_configured(win_T *wp, int *is_fixed) -{ - const char *scl = (const char *)wp->w_p_scl; - - if (is_fixed) { - *is_fixed = 1; - } - - // Note: It checks "no" or "number" in 'signcolumn' option - if (*scl == 'n' - && (*(scl + 1) == 'o' || (*(scl + 1) == 'u' - && (wp->w_p_nu || wp->w_p_rnu)))) { + if (wp->w_minscwidth <= SCL_NO) { return 0; } - // yes or yes - if (!strncmp(scl, "yes:", 4)) { - // Fixed amount of columns - return scl[4] - '0'; - } - if (*scl == 'y') { - return 1; - } - - if (is_fixed) { - // auto or auto:<NUM> - *is_fixed = 0; - } - - int minimum = 0, maximum = 1; - - if (!strncmp(scl, "auto:", 5)) { - // Variable depending on a configuration - maximum = scl[5] - '0'; - // auto:<NUM>-<NUM> - if (strlen(scl) == 8 && *(scl + 6) == '-') { - minimum = maximum; - maximum = scl[7] - '0'; - } - } - - int needed_signcols = buf_signcols(wp->w_buffer, maximum); - int ret = MAX(minimum, MIN(maximum, needed_signcols)); + int needed_signcols = buf_signcols(wp->w_buffer, wp->w_maxscwidth); + int ret = MAX(wp->w_minscwidth, MIN(wp->w_maxscwidth, needed_signcols)); assert(ret <= SIGN_SHOW_MAX); return ret; } @@ -5490,7 +6195,7 @@ dict_T *get_winbuf_options(const int bufopt) if ((bufopt && (opt->indir & PV_BUF)) || (!bufopt && (opt->indir & PV_WIN))) { - char_u *varp = get_varp(opt); + void *varp = get_varp(opt); if (varp != NULL) { if (opt->flags & P_STRING) { @@ -5498,7 +6203,7 @@ dict_T *get_winbuf_options(const int bufopt) *(const char **)varp); } else if (opt->flags & P_NUM) { tv_dict_add_nr(d, opt->fullname, strlen(opt->fullname), - *(long *)varp); + *(OptInt *)varp); } else { tv_dict_add_nr(d, opt->fullname, strlen(opt->fullname), *(int *)varp); } @@ -5511,43 +6216,43 @@ dict_T *get_winbuf_options(const int bufopt) /// Return the effective 'scrolloff' value for the current window, using the /// global value when appropriate. -long get_scrolloff_value(win_T *wp) +int get_scrolloff_value(win_T *wp) { // Disallow scrolloff in terminal-mode. #11915 if (State & MODE_TERMINAL) { return 0; } - return wp->w_p_so < 0 ? p_so : wp->w_p_so; + return (int)(wp->w_p_so < 0 ? p_so : wp->w_p_so); } /// Return the effective 'sidescrolloff' value for the current window, using the /// global value when appropriate. -long get_sidescrolloff_value(win_T *wp) +int get_sidescrolloff_value(win_T *wp) { - return wp->w_p_siso < 0 ? p_siso : wp->w_p_siso; + return (int)(wp->w_p_siso < 0 ? p_siso : wp->w_p_siso); } -Dictionary get_vimoption(String name, Error *err) +Dictionary get_vimoption(String name, int scope, buf_T *buf, win_T *win, Error *err) { - int opt_idx = findoption_len((const char *)name.data, name.size); - if (opt_idx < 0) { - api_set_error(err, kErrorTypeValidation, "no such option: '%s'", name.data); + int opt_idx = findoption_len(name.data, name.size); + VALIDATE_S(opt_idx >= 0, "option (not found)", name.data, { return (Dictionary)ARRAY_DICT_INIT; - } - return vimoption2dict(&options[opt_idx]); + }); + + return vimoption2dict(&options[opt_idx], scope, buf, win); } Dictionary get_all_vimoptions(void) { Dictionary retval = ARRAY_DICT_INIT; for (size_t i = 0; options[i].fullname != NULL; i++) { - Dictionary opt_dict = vimoption2dict(&options[i]); + Dictionary opt_dict = vimoption2dict(&options[i], OPT_GLOBAL, curbuf, curwin); PUT(retval, options[i].fullname, DICTIONARY_OBJ(opt_dict)); } return retval; } -static Dictionary vimoption2dict(vimoption_T *opt) +static Dictionary vimoption2dict(vimoption_T *opt, int req_scope, buf_T *buf, win_T *win) { Dictionary dict = ARRAY_DICT_INIT; @@ -5566,35 +6271,51 @@ static Dictionary vimoption2dict(vimoption_T *opt) PUT(dict, "scope", CSTR_TO_OBJ(scope)); // welcome to the jungle - PUT(dict, "global_local", BOOL(opt->indir & PV_BOTH)); - PUT(dict, "commalist", BOOL(opt->flags & P_COMMA)); - PUT(dict, "flaglist", BOOL(opt->flags & P_FLAGLIST)); + PUT(dict, "global_local", BOOLEAN_OBJ(opt->indir & PV_BOTH)); + PUT(dict, "commalist", BOOLEAN_OBJ(opt->flags & P_COMMA)); + PUT(dict, "flaglist", BOOLEAN_OBJ(opt->flags & P_FLAGLIST)); - PUT(dict, "was_set", BOOL(opt->flags & P_WAS_SET)); + PUT(dict, "was_set", BOOLEAN_OBJ(opt->flags & P_WAS_SET)); + + LastSet last_set = { .channel_id = 0 }; + if (req_scope == OPT_GLOBAL) { + last_set = opt->last_set; + } else { + // Scope is either OPT_LOCAL or a fallback mode was requested. + if (opt->indir & PV_BUF) { + last_set = buf->b_p_script_ctx[opt->indir & PV_MASK]; + } + if (opt->indir & PV_WIN) { + last_set = win->w_p_script_ctx[opt->indir & PV_MASK]; + } + if (req_scope != OPT_LOCAL && last_set.script_ctx.sc_sid == 0) { + last_set = opt->last_set; + } + } - PUT(dict, "last_set_sid", INTEGER_OBJ(opt->last_set.script_ctx.sc_sid)); - PUT(dict, "last_set_linenr", INTEGER_OBJ(opt->last_set.script_ctx.sc_lnum)); - PUT(dict, "last_set_chan", INTEGER_OBJ((int64_t)opt->last_set.channel_id)); + PUT(dict, "last_set_sid", INTEGER_OBJ(last_set.script_ctx.sc_sid)); + PUT(dict, "last_set_linenr", INTEGER_OBJ(last_set.script_ctx.sc_lnum)); + PUT(dict, "last_set_chan", INTEGER_OBJ((int64_t)last_set.channel_id)); const char *type; Object def; // TODO(bfredl): do you even nocp? - char_u *def_val = (char_u *)opt->def_val; + char *def_val = opt->def_val; if (opt->flags & P_STRING) { type = "string"; - def = CSTR_TO_OBJ(def_val ? (char *)def_val : ""); + def = CSTR_TO_OBJ(def_val ? def_val : ""); } else if (opt->flags & P_NUM) { type = "number"; def = INTEGER_OBJ((Integer)(intptr_t)def_val); } else if (opt->flags & P_BOOL) { type = "boolean"; - def = BOOL((intptr_t)def_val); + def = BOOLEAN_OBJ((intptr_t)def_val); } else { type = ""; def = NIL; } PUT(dict, "type", CSTR_TO_OBJ(type)); PUT(dict, "default", def); - PUT(dict, "allows_duplicates", BOOL(!(opt->flags & P_NODUP))); + PUT(dict, "allows_duplicates", BOOLEAN_OBJ(!(opt->flags & P_NODUP))); return dict; } |