summaryrefslogtreecommitdiffstats
path: root/src/optionstr.c
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2019-09-16 21:06:21 +0200
committerBram Moolenaar <Bram@vim.org>2019-09-16 21:06:21 +0200
commitdac1347b4d9c1a1aef6aa73fdea08a9d1077d6ea (patch)
tree4c9124f2de51bedb339bc5cdb47a3086c0e92af9 /src/optionstr.c
parent8aeec40207b5adcd3a155277dc4f29189343b963 (diff)
patch 8.1.2045: the option.c file is too bigv8.1.2045
Problem: The option.c file is too big. Solution: Split off the code dealing with strings. (Yegappan Lakshmanan, closes #4937)
Diffstat (limited to 'src/optionstr.c')
-rw-r--r--src/optionstr.c2491
1 files changed, 2491 insertions, 0 deletions
diff --git a/src/optionstr.c b/src/optionstr.c
new file mode 100644
index 0000000000..44f0759a9e
--- /dev/null
+++ b/src/optionstr.c
@@ -0,0 +1,2491 @@
+/* vi:set ts=8 sts=4 sw=4 noet:
+ *
+ * VIM - Vi IMproved by Bram Moolenaar
+ *
+ * Do ":help uganda" in Vim to read copying and usage conditions.
+ * Do ":help credits" in Vim to see a list of people who contributed.
+ * See README.txt for an overview of the Vim source code.
+ */
+
+/*
+ * optionstr.c: Functions related to string options
+ */
+
+#include "vim.h"
+
+static char *(p_ambw_values[]) = {"single", "double", NULL};
+static char *(p_bg_values[]) = {"light", "dark", NULL};
+static char *(p_bkc_values[]) = {"yes", "auto", "no", "breaksymlink", "breakhardlink", NULL};
+static char *(p_bo_values[]) = {"all", "backspace", "cursor", "complete",
+ "copy", "ctrlg", "error", "esc", "ex",
+ "hangul", "insertmode", "lang", "mess",
+ "showmatch", "operator", "register", "shell",
+ "spell", "wildmode", NULL};
+static char *(p_nf_values[]) = {"bin", "octal", "hex", "alpha", NULL};
+static char *(p_ff_values[]) = {FF_UNIX, FF_DOS, FF_MAC, NULL};
+#ifdef FEAT_CRYPT
+static char *(p_cm_values[]) = {"zip", "blowfish", "blowfish2", NULL};
+#endif
+static char *(p_cmp_values[]) = {"internal", "keepascii", NULL};
+static char *(p_dy_values[]) = {"lastline", "truncate", "uhex", NULL};
+#ifdef FEAT_FOLDING
+static char *(p_fdo_values[]) = {"all", "block", "hor", "mark", "percent",
+ "quickfix", "search", "tag", "insert",
+ "undo", "jump", NULL};
+#endif
+#ifdef FEAT_SESSION
+// Also used for 'viewoptions'!
+static char *(p_ssop_values[]) = {"buffers", "winpos", "resize", "winsize",
+ "localoptions", "options", "help", "blank", "globals", "slash", "unix",
+ "sesdir", "curdir", "folds", "cursor", "tabpages", "terminal", NULL};
+#endif
+static char *(p_swb_values[]) = {"useopen", "usetab", "split", "newtab", "vsplit", NULL};
+static char *(p_tc_values[]) = {"followic", "ignore", "match", "followscs", "smart", NULL};
+#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
+static char *(p_toolbar_values[]) = {"text", "icons", "tooltips", "horiz", NULL};
+#endif
+#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
+static char *(p_tbis_values[]) = {"tiny", "small", "medium", "large", "huge", "giant", NULL};
+#endif
+#if defined(FEAT_MOUSE) && (defined(UNIX) || defined(VMS))
+static char *(p_ttym_values[]) = {"xterm", "xterm2", "dec", "netterm", "jsbterm", "pterm", "urxvt", "sgr", NULL};
+#endif
+static char *(p_ve_values[]) = {"block", "insert", "all", "onemore", NULL};
+static char *(p_wop_values[]) = {"tagfile", NULL};
+#ifdef FEAT_WAK
+static char *(p_wak_values[]) = {"yes", "menu", "no", NULL};
+#endif
+static char *(p_mousem_values[]) = {"extend", "popup", "popup_setpos", "mac", NULL};
+static char *(p_sel_values[]) = {"inclusive", "exclusive", "old", NULL};
+static char *(p_slm_values[]) = {"mouse", "key", "cmd", NULL};
+static char *(p_km_values[]) = {"startsel", "stopsel", NULL};
+#ifdef FEAT_BROWSE
+static char *(p_bsdir_values[]) = {"current", "last", "buffer", NULL};
+#endif
+static char *(p_scbopt_values[]) = {"ver", "hor", "jump", NULL};
+static char *(p_debug_values[]) = {"msg", "throw", "beep", NULL};
+static char *(p_ead_values[]) = {"both", "ver", "hor", NULL};
+static char *(p_buftype_values[]) = {"nofile", "nowrite", "quickfix", "help", "terminal", "acwrite", "prompt", "popup", NULL};
+static char *(p_bufhidden_values[]) = {"hide", "unload", "delete", "wipe", NULL};
+static char *(p_bs_values[]) = {"indent", "eol", "start", NULL};
+#ifdef FEAT_FOLDING
+static char *(p_fdm_values[]) = {"manual", "expr", "marker", "indent", "syntax",
+# ifdef FEAT_DIFF
+ "diff",
+# endif
+ NULL};
+static char *(p_fcl_values[]) = {"all", NULL};
+#endif
+static char *(p_cot_values[]) = {"menu", "menuone", "longest", "preview", "popup", "noinsert", "noselect", NULL};
+#ifdef BACKSLASH_IN_FILENAME
+static char *(p_csl_values[]) = {"slash", "backslash", NULL};
+#endif
+#ifdef FEAT_SIGNS
+static char *(p_scl_values[]) = {"yes", "no", "auto", "number", NULL};
+#endif
+#if defined(MSWIN) && defined(FEAT_TERMINAL)
+static char *(p_twt_values[]) = {"winpty", "conpty", "", NULL};
+#endif
+
+static int check_opt_strings(char_u *val, char **values, int list);
+static int opt_strings_flags(char_u *val, char **values, unsigned *flagp, int list);
+
+/*
+ * After setting various option values: recompute variables that depend on
+ * option values.
+ */
+ void
+didset_string_options(void)
+{
+ (void)opt_strings_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
+ (void)opt_strings_flags(p_bkc, p_bkc_values, &bkc_flags, TRUE);
+ (void)opt_strings_flags(p_bo, p_bo_values, &bo_flags, TRUE);
+#ifdef FEAT_SESSION
+ (void)opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE);
+ (void)opt_strings_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
+#endif
+#ifdef FEAT_FOLDING
+ (void)opt_strings_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
+#endif
+ (void)opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE);
+ (void)opt_strings_flags(p_tc, p_tc_values, &tc_flags, FALSE);
+ (void)opt_strings_flags(p_ve, p_ve_values, &ve_flags, TRUE);
+#if defined(FEAT_MOUSE) && (defined(UNIX) || defined(VMS))
+ (void)opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE);
+#endif
+#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
+ (void)opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags, TRUE);
+#endif
+#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
+ (void)opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE);
+#endif
+}
+
+#if defined(FEAT_EVAL)
+/*
+ * Trigger the OptionSet autocommand.
+ * "opt_idx" is the index of the option being set.
+ * "opt_flags" can be OPT_LOCAL etc.
+ * "oldval" the old value
+ * "oldval_l" the old local value (only non-NULL if global and local value
+ * are set)
+ * "oldval_g" the old global value (only non-NULL if global and local value
+ * are set)
+ * "newval" the new value
+ */
+ void
+trigger_optionsset_string(
+ int opt_idx,
+ int opt_flags,
+ char_u *oldval,
+ char_u *oldval_l,
+ char_u *oldval_g,
+ char_u *newval)
+{
+ // Don't do this recursively.
+ if (oldval != NULL && newval != NULL
+ && *get_vim_var_str(VV_OPTION_TYPE) == NUL)
+ {
+ char_u buf_type[7];
+
+ sprintf((char *)buf_type, "%s",
+ (opt_flags & OPT_LOCAL) ? "local" : "global");
+ set_vim_var_string(VV_OPTION_OLD, oldval, -1);
+ set_vim_var_string(VV_OPTION_NEW, newval, -1);
+ set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
+ if (opt_flags & OPT_LOCAL)
+ {
+ set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
+ set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
+ }
+ if (opt_flags & OPT_GLOBAL)
+ {
+ set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
+ set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval, -1);
+ }
+ if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
+ {
+ set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
+ set_vim_var_string(VV_OPTION_OLDLOCAL, oldval_l, -1);
+ set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval_g, -1);
+ }
+ if (opt_flags & OPT_MODELINE)
+ {
+ set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
+ set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
+ }
+ apply_autocmds(EVENT_OPTIONSET,
+ (char_u *)get_option_fullname(opt_idx), NULL, FALSE,
+ NULL);
+ reset_v_option_vars();
+ }
+}
+#endif
+
+ static char *
+illegal_char(char *errbuf, int c)
+{
+ if (errbuf == NULL)
+ return "";
+ sprintf((char *)errbuf, _("E539: Illegal character <%s>"),
+ (char *)transchar(c));
+ return errbuf;
+}
+
+/*
+ * Check string options in a buffer for NULL value.
+ */
+ void
+check_buf_options(buf_T *buf)
+{
+ check_string_option(&buf->b_p_bh);
+ check_string_option(&buf->b_p_bt);
+ check_string_option(&buf->b_p_fenc);
+ check_string_option(&buf->b_p_ff);
+#ifdef FEAT_FIND_ID
+ check_string_option(&buf->b_p_def);
+ check_string_option(&buf->b_p_inc);
+# ifdef FEAT_EVAL
+ check_string_option(&buf->b_p_inex);
+# endif
+#endif
+#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
+ check_string_option(&buf->b_p_inde);
+ check_string_option(&buf->b_p_indk);
+#endif
+#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
+ check_string_option(&buf->b_p_bexpr);
+#endif
+#if defined(FEAT_CRYPT)
+ check_string_option(&buf->b_p_cm);
+#endif
+ check_string_option(&buf->b_p_fp);
+#if defined(FEAT_EVAL)
+ check_string_option(&buf->b_p_fex);
+#endif
+#ifdef FEAT_CRYPT
+ check_string_option(&buf->b_p_key);
+#endif
+ check_string_option(&buf->b_p_kp);
+ check_string_option(&buf->b_p_mps);
+ check_string_option(&buf->b_p_fo);
+ check_string_option(&buf->b_p_flp);
+ check_string_option(&buf->b_p_isk);
+#ifdef FEAT_COMMENTS
+ check_string_option(&buf->b_p_com);
+#endif
+#ifdef FEAT_FOLDING
+ check_string_option(&buf->b_p_cms);
+#endif
+ check_string_option(&buf->b_p_nf);
+#ifdef FEAT_TEXTOBJ
+ check_string_option(&buf->b_p_qe);
+#endif
+#ifdef FEAT_SYN_HL
+ check_string_option(&buf->b_p_syn);
+ check_string_option(&buf->b_s.b_syn_isk);
+#endif
+#ifdef FEAT_SPELL
+ check_string_option(&buf->b_s.b_p_spc);
+ check_string_option(&buf->b_s.b_p_spf);
+ check_string_option(&buf->b_s.b_p_spl);
+#endif
+#ifdef FEAT_SEARCHPATH
+ check_string_option(&buf->b_p_sua);
+#endif
+#ifdef FEAT_CINDENT
+ check_string_option(&buf->b_p_cink);
+ check_string_option(&buf->b_p_cino);
+ parse_cino(buf);
+#endif
+ check_string_option(&buf->b_p_ft);
+#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
+ check_string_option(&buf->b_p_cinw);
+#endif
+ check_string_option(&buf->b_p_cpt);
+#ifdef FEAT_COMPL_FUNC
+ check_string_option(&buf->b_p_cfu);
+ check_string_option(&buf->b_p_ofu);
+#endif
+#ifdef FEAT_EVAL
+ check_string_option(&buf->b_p_tfu);
+#endif
+#ifdef FEAT_KEYMAP
+ check_string_option(&buf->b_p_keymap);
+#endif
+#ifdef FEAT_QUICKFIX
+ check_string_option(&buf->b_p_gp);
+ check_string_option(&buf->b_p_mp);
+ check_string_option(&buf->b_p_efm);
+#endif
+ check_string_option(&buf->b_p_ep);
+ check_string_option(&buf->b_p_path);
+ check_string_option(&buf->b_p_tags);
+ check_string_option(&buf->b_p_tc);
+ check_string_option(&buf->b_p_dict);
+ check_string_option(&buf->b_p_tsr);
+#ifdef FEAT_LISP
+ check_string_option(&buf->b_p_lw);
+#endif
+ check_string_option(&buf->b_p_bkc);
+ check_string_option(&buf->b_p_menc);
+#ifdef FEAT_VARTABS
+ check_string_option(&buf->b_p_vsts);
+ check_string_option(&buf->b_p_vts);
+#endif
+}
+
+/*
+ * Free the string allocated for an option.
+ * Checks for the string being empty_option. This may happen if we're out of
+ * memory, vim_strsave() returned NULL, which was replaced by empty_option by
+ * check_options().
+ * Does NOT check for P_ALLOCED flag!
+ */
+ void
+free_string_option(char_u *p)
+{
+ if (p != empty_option)
+ vim_free(p);
+}
+
+ void
+clear_string_option(char_u **pp)
+{
+ if (*pp != empty_option)
+ vim_free(*pp);
+ *pp = empty_option;
+}
+
+ void
+check_string_option(char_u **pp)
+{
+ if (*pp == NULL)
+ *pp = empty_option;
+}
+
+/*
+ * Set global value for string option when it's a local option.
+ */
+ static void
+set_string_option_global(
+ int opt_idx, // option index
+ char_u **varp) // pointer to option variable
+{
+ char_u **p, *s;
+
+ // the global value is always allocated
+ if (is_window_local_option(opt_idx))
+ p = (char_u **)GLOBAL_WO(varp);
+ else
+ p = (char_u **)get_option_var(opt_idx);
+ if (!is_global_option(opt_idx)
+ && p != varp
+ && (s = vim_strsave(*varp)) != NULL)
+ {
+ free_string_option(*p);
+ *p = s;
+ }
+}
+
+/*
+ * Set a string option to a new value (without checking the effect).
+ * The string is copied into allocated memory.
+ * if ("opt_idx" == -1) "name" is used, otherwise "opt_idx" is used.
+ * When "set_sid" is zero set the scriptID to current_sctx.sc_sid. When
+ * "set_sid" is SID_NONE don't set the scriptID. Otherwise set the scriptID to
+ * "set_sid".
+ */
+ void
+set_string_option_direct(
+ char_u *name,
+ int opt_idx,
+ char_u *val,
+ int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
+ int set_sid UNUSED)
+{
+ char_u *s;
+ char_u **varp;
+ int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
+ int idx = opt_idx;
+
+ if (idx == -1) // use name
+ {
+ idx = findoption(name);
+ if (idx < 0) // not found (should not happen)
+ {
+ semsg(_(e_intern2), "set_string_option_direct()");
+ siemsg(_("For option %s"), name);
+ return;
+ }
+ }
+
+ if (is_hidden_option(idx)) // can't set hidden option
+ return;
+
+ s = vim_strsave(val);
+ if (s != NULL)
+ {
+ varp = (char_u **)get_option_varp_scope(idx,
+ both ? OPT_LOCAL : opt_flags);
+ if ((opt_flags & OPT_FREE) && (get_option_flags(idx) & P_ALLOCED))
+ free_string_option(*varp);
+ *varp = s;
+
+ // For buffer/window local option may also set the global value.
+ if (both)
+ set_string_option_global(idx, varp);
+
+ set_option_flag(idx, P_ALLOCED);
+
+ // When setting both values of a global option with a local value,
+ // make the local value empty, so that the global value is used.
+ if (is_global_local_option(idx) && both)
+ {
+ free_string_option(*varp);
+ *varp = empty_option;
+ }
+# ifdef FEAT_EVAL
+ if (set_sid != SID_NONE)
+ {
+ sctx_T script_ctx;
+
+ if (set_sid == 0)
+ script_ctx = current_sctx;
+ else
+ {
+ script_ctx.sc_sid = set_sid;
+ script_ctx.sc_seq = 0;
+ script_ctx.sc_lnum = 0;
+ script_ctx.sc_version = 1;
+ }
+ set_option_sctx_idx(idx, opt_flags, script_ctx);
+ }
+# endif
+ }
+}
+
+/*
+ * Like set_string_option_direct(), but for a window-local option in "wp".
+ * Blocks autocommands to avoid the old curwin becoming invalid.
+ */
+ void
+set_string_option_direct_in_win(
+ win_T *wp,
+ char_u *name,
+ int opt_idx,
+ char_u *val,
+ int opt_flags,
+ int set_sid)
+{
+ win_T *save_curwin = curwin;
+
+ block_autocmds();
+ curwin = wp;
+ curbuf = curwin->w_buffer;
+ set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
+ curwin = save_curwin;
+ curbuf = curwin->w_buffer;
+ unblock_autocmds();
+}
+
+/*
+ * Like set_string_option_direct(), but for a buffer-local option in "buf".
+ * Blocks autocommands to avoid the old curbuf becoming invalid.
+ */
+ void
+set_string_option_direct_in_buf(
+ buf_T *buf,
+ char_u *name,
+ int opt_idx,
+ char_u *val,
+ int opt_flags,
+ int set_sid)
+{
+ buf_T *save_curbuf = curbuf;
+
+ block_autocmds();
+ curbuf = buf;
+ curwin->w_buffer = curbuf;
+ set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
+ curbuf = save_curbuf;
+ curwin->w_buffer = curbuf;
+ unblock_autocmds();
+}
+
+/*
+ * Set a string option to a new value, and handle the effects.
+ *
+ * Returns NULL on success or error message on error.
+ */
+ char *
+set_string_option(
+ int opt_idx,
+ char_u *value,
+ int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
+{
+ char_u *s;
+ char_u **varp;
+ char_u *oldval;
+#if defined(FEAT_EVAL)
+ char_u *oldval_l = NULL;
+ char_u *oldval_g = NULL;
+ char_u *saved_oldval = NULL;
+ char_u *saved_oldval_l = NULL;
+ char_u *saved_oldval_g = NULL;
+ char_u *saved_newval = NULL;
+#endif
+ char *r = NULL;
+ int value_checked = FALSE;
+
+ if (is_hidden_option(opt_idx)) // don't set hidden option
+ return NULL;
+
+ s = vim_strsave(value);
+ if (s != NULL)
+ {
+ varp = (char_u **)get_option_varp_scope(opt_idx,
+ (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
+ ? (is_global_local_option(opt_idx)
+ ? OPT_GLOBAL : OPT_LOCAL)
+ : opt_flags);
+ oldval = *varp;
+#if defined(FEAT_EVAL)
+ if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
+ {
+ oldval_l = *(char_u **)get_option_varp_scope(opt_idx, OPT_LOCAL);
+ oldval_g = *(char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
+ }
+#endif
+ *varp = s;
+
+#if defined(FEAT_EVAL)
+ if (!starting
+# ifdef FEAT_CRYPT
+ && !is_crypt_key_option(opt_idx)
+# endif
+ )
+ {
+ if (oldval_l != NULL)
+ saved_oldval_l = vim_strsave(oldval_l);
+ if (oldval_g != NULL)
+ saved_oldval_g = vim_strsave(oldval_g);
+ saved_oldval = vim_strsave(oldval);
+ saved_newval = vim_strsave(s);
+ }
+#endif
+ if ((r = did_set_string_option(opt_idx, varp, TRUE, oldval, NULL,
+ opt_flags, &value_checked)) == NULL)
+ did_set_option(opt_idx, opt_flags, TRUE, value_checked);
+
+#if defined(FEAT_EVAL)
+ // call autocommand after handling side effects
+ if (r == NULL)
+ trigger_optionsset_string(opt_idx, opt_flags,
+ saved_oldval, saved_oldval_l,
+ saved_oldval_g, saved_newval);
+ vim_free(saved_oldval);
+ vim_free(saved_oldval_l);
+ vim_free(saved_oldval_g);
+ vim_free(saved_newval);
+#endif
+ }
+ return r;
+}
+
+/*
+ * Return TRUE if "val" is a valid 'filetype' name.
+ * Also used for 'syntax' and 'keymap'.
+ */
+ static int
+valid_filetype(char_u *val)
+{
+ return valid_name(val, ".-_");
+}
+
+#ifdef FEAT_STL_OPT
+/*
+ * Check validity of options with the 'statusline' format.
+ * Return error message or NULL.
+ */
+ static char *
+check_stl_option(char_u *s)
+{
+ int itemcnt = 0;
+ int groupdepth = 0;
+ static char errbuf[80];
+
+ while (*s && itemcnt < STL_MAX_ITEM)
+ {
+ // Check for valid keys after % sequences
+ while (*s && *s != '%')
+ s++;
+ if (!*s)
+ break;
+ s++;
+ if (*s != '%' && *s != ')')
+ ++itemcnt;
+ if (*s == '%' || *s == STL_TRUNCMARK || *s == STL_MIDDLEMARK)
+ {
+ s++;
+ continue;
+ }
+ if (*s == ')')
+ {
+ s++;
+ if (--groupdepth < 0)
+ break;
+ continue;
+ }
+ if (*s == '-')
+ s++;
+ while (VIM_ISDIGIT(*s))
+ s++;
+ if (*s == STL_USER_HL)
+ continue;
+ if (*s == '.')
+ {
+ s++;
+ while (*s && VIM_ISDIGIT(*s))
+ s++;
+ }
+ if (*s == '(')
+ {
+ groupdepth++;
+ continue;
+ }
+ if (vim_strchr(STL_ALL, *s) == NULL)
+ {
+ return illegal_char(errbuf, *s);
+ }
+ if (*s == '{')
+ {
+ s++;
+ while (*s != '}' && *s)
+ s++;
+ if (*s != '}')
+ return N_("E540: Unclosed expression sequence");
+ }
+ }
+ if (itemcnt >= STL_MAX_ITEM)
+ return N_("E541: too many items");
+ if (groupdepth != 0)
+ return N_("E542: unbalanced groups");
+ return NULL;
+}
+#endif
+
+/*
+ * Handle string options that need some action to perform when changed.
+ * Returns NULL for success, or an error message for an error.
+ */
+ char *
+did_set_string_option(
+ int opt_idx, // index in options[] table
+ char_u **varp, // pointer to the option variable
+ int new_value_alloced, // new value was allocated
+ char_u *oldval, // previous value of the option
+ char *errbuf, // buffer for errors, or NULL
+ int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
+ int *value_checked) // value was checked to be save, no
+ // need to set P_INSECURE
+{
+ char *errmsg = NULL;
+ char_u *s, *p;
+ int did_chartab = FALSE;
+ char_u **gvarp;
+ long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
+#ifdef FEAT_GUI
+ // set when changing an option that only requires a redraw in the GUI
+ int redraw_gui_only = FALSE;
+#endif
+ int value_changed = FALSE;
+#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
+ int did_swaptcap = FALSE;
+#endif
+
+ // Get the global option to compare with, otherwise we would have to check
+ // two values for all local options.
+ gvarp = (char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
+
+ // Disallow changing some options from secure mode
+ if ((secure
+#ifdef HAVE_SANDBOX
+ || sandbox != 0
+#endif
+ ) && (get_option_flags(opt_idx) & P_SECURE))
+ errmsg = e_secure;
+
+ // Check for a "normal" directory or file name in some options. Disallow a
+ // path separator (slash and/or backslash), wildcards and characters that
+ // are often illegal in a file name. Be more permissive if "secure" is off.
+ else if (((get_option_flags(opt_idx) & P_NFNAME)
+ && vim_strpbrk(*varp, (char_u *)(secure
+ ? "/\\*?[|;&<>\r\n" : "/\\*?[<>\r\n")) != NULL)
+ || ((get_option_flags(opt_idx) & P_NDNAME)
+ && vim_strpbrk(*varp, (char_u *)"*?[|;&<>\r\n") != NULL))
+ errmsg = e_invarg;
+
+ // 'term'
+ else if (varp == &T_NAME)
+ {
+ if (T_NAME[0] == NUL)
+ errmsg = N_("E529: Cannot set 'term' to empty string");
+#ifdef FEAT_GUI
+ if (gui.in_use)
+ errmsg = N_("E530: Cannot change term in GUI");
+ else if (term_is_gui(T_NAME))
+ errmsg = N_("E531: Use \":gui\" to start the GUI");
+#endif
+ else if (set_termname(T_NAME) == FAIL)
+ errmsg = N_("E522: Not found in termcap");
+ else
+ {
+ // Screen colors may have changed.
+ redraw_later_clear();
+
+ // Both 'term' and 'ttytype' point to T_NAME, only set the
+ // P_ALLOCED flag on 'term'.
+ opt_idx = findoption((char_u *)"term");
+ free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
+ }
+ }
+
+ // 'backupcopy'
+ else if (gvarp == &p_bkc)
+ {
+ char_u *bkc = p_bkc;
+ unsigned int *flags = &bkc_flags;
+
+ if (opt_flags & OPT_LOCAL)
+ {
+ bkc = curbuf->b_p_bkc;
+ flags = &curbuf->b_bkc_flags;
+ }
+
+ if ((opt_flags & OPT_LOCAL) && *bkc == NUL)
+ // make the local value empty: use the global value
+ *flags = 0;
+ else
+ {
+ if (opt_strings_flags(bkc, p_bkc_values, flags, TRUE) != OK)
+ errmsg = e_invarg;
+ if ((((int)*flags & BKC_AUTO) != 0)
+ + (((int)*flags & BKC_YES) != 0)
+ + (((int)*flags & BKC_NO) != 0) != 1)
+ {
+ // Must have exactly one of "auto", "yes" and "no".
+ (void)opt_strings_flags(oldval, p_bkc_values, flags, TRUE);
+ errmsg = e_invarg;
+ }
+ }
+ }
+
+ // 'backupext' and 'patchmode'
+ else if (varp == &p_bex || varp == &p_pm)
+ {
+ if (STRCMP(*p_bex == '.' ? p_bex + 1 : p_bex,
+ *p_pm == '.' ? p_pm + 1 : p_pm) == 0)
+ errmsg = N_("E589: 'backupext' and 'patchmode' are equal");
+ }
+#ifdef FEAT_LINEBREAK
+ // 'breakindentopt'
+ else if (varp == &curwin->w_p_briopt)
+ {
+ if (briopt_check(curwin) == FAIL)
+ errmsg = e_invarg;
+ }
+#endif
+
+ // 'isident', 'iskeyword', 'isprint or 'isfname' option: refill g_chartab[]
+ // If the new option is invalid, use old value. 'lisp' option: refill
+ // g_chartab[] for '-' char
+ else if ( varp == &p_isi
+ || varp == &(curbuf->b_p_isk)
+ || varp == &p_isp
+ || varp == &p_isf)
+ {
+ if (init_chartab() == FAIL)
+ {
+ did_chartab = TRUE; // need to restore it below
+ errmsg = e_invarg; // error in value
+ }
+ }
+
+ // 'helpfile'
+ else if (varp == &p_hf)
+ {
+ // May compute new values for $VIM and $VIMRUNTIME
+ if (didset_vim)
+ {
+ vim_setenv((char_u *)"VIM", (char_u *)"");
+ didset_vim = FALSE;
+ }
+ if (didset_vimruntime)
+ {
+ vim_setenv((char_u *)"VIMRUNTIME", (char_u *)"");
+ didset_vimruntime = FALSE;
+ }
+ }
+
+#ifdef FEAT_SYN_HL
+ // 'cursorlineopt'
+ else if (varp == &curwin->w_p_culopt
+ || gvarp == &curwin->w_allbuf_opt.wo_culopt)
+ {
+ if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
+ errmsg = e_invarg;
+ }
+
+ // 'colorcolumn'
+ else if (varp == &curwin->w_p_cc)
+ errmsg = check_colorcolumn(curwin);
+#endif
+
+#ifdef FEAT_MULTI_LANG
+ // 'helplang'
+ else if (varp == &p_hlg)
+ {
+ // Check for "", "ab", "ab,cd", etc.
+ for (s = p_hlg; *s != NUL; s += 3)
+ {
+ if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
+ {
+ errmsg = e_invarg;
+ break;
+ }
+ if (s[2] == NUL)
+ break;
+ }
+ }
+#endif
+
+ // 'highlight'
+ else if (varp == &p_hl)
+ {
+ if (highlight_changed() == FAIL)
+ errmsg = e_invarg; // invalid flags
+ }
+
+ // 'nrformats'
+ else if (gvarp == &p_nf)
+ {
+ if (check_opt_strings(*varp, p_nf_values, TRUE) != OK)
+ errmsg = e_invarg;
+ }
+
+#ifdef FEAT_SESSION
+ // 'sessionoptions'
+ else if (varp == &p_ssop)
+ {
+ if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
+ errmsg = e_invarg;
+ if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
+ {
+ // Don't allow both "sesdir" and "curdir".
+ (void)opt_strings_flags(oldval, p_ssop_values, &ssop_flags, TRUE);
+ errmsg = e_invarg;
+ }
+ }
+ // 'viewoptions'
+ else if (varp == &p_vop)
+ {
+ if (opt_strings_flags(p_vop, p_ssop_values, &vop_flags, TRUE) != OK)
+ errmsg = e_invarg;
+ }
+#endif
+
+ // 'scrollopt'
+ else if (varp == &p_sbo)
+ {
+ if (check_opt_strings(p_sbo, p_scbopt_values, TRUE) != OK)
+ errmsg = e_invarg;
+ }
+
+ // 'ambiwidth'
+ else if (varp == &p_ambw || varp == &p_emoji)
+ {
+ if (check_opt_strings(p_ambw, p_ambw_values, FALSE) != OK)
+ errmsg = e_invarg;
+ else if (set_chars_option(&p_lcs) != NULL)
+ errmsg = _("E834: Conflicts with value of 'listchars'");
+ else if (set_chars_option(&p_fcs) != NULL)
+ errmsg = _("E835: Conflicts with value of 'fillchars'");
+ }
+
+ // 'background'
+ else if (varp == &p_bg)
+ {
+ if (check_opt_strings(p_bg, p_bg_values, FALSE) == OK)
+ {
+#ifdef FEAT_EVAL
+ int dark = (*p_bg == 'd');
+#endif
+
+ init_highlight(FALSE, FALSE);
+
+#ifdef FEAT_EVAL
+ if (dark != (*p_bg == 'd')
+ && get_var_value((char_u *)"g:colors_name") != NULL)
+ {
+ // The color scheme must have set 'background' back to another
+ // value, that's not what we want here. Disable the color
+ // scheme and set the colors again.
+ do_unlet((char_u *)"g:colors_name", TRUE);
+ free_string_option(p_bg);
+ p_bg = vim_strsave((char_u *)(dark ? "dark" : "light"));
+ check_string_option(&p_bg);
+ init_highlight(FALSE, FALSE);
+ }
+#endif
+ }
+ else
+ errmsg = e_invarg;
+ }
+
+ // 'wildmode'
+ else if (varp == &p_wim)
+ {
+ if (check_opt_wim() == FAIL)
+ errmsg = e_invarg;
+ }
+
+ // 'wildoptions'
+ else if (varp == &p_wop)
+ {
+ if (check_opt_strings(p_wop, p_wop_values, TRUE) != OK)
+ errmsg = e_invarg;
+ }
+
+#ifdef FEAT_WAK
+ // 'winaltkeys'
+ else if (varp == &p_wak)
+ {
+ if (*p_wak == NUL
+ || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
+ errmsg = e_invarg;
+# ifdef FEAT_MENU
+# ifdef FEAT_GUI_MOTIF
+ else if (gui.in_use)
+ gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
+# else
+# ifdef FEAT_GUI_GTK
+ else if (gui.in_use)
+ gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
+# endif
+# endif
+# endif
+ }
+#endif
+
+ // 'eventignore'
+ else if (varp == &p_ei)
+ {
+ if (check_ei() == FAIL)
+ errmsg = e_invarg;
+ }
+
+ // 'encoding', 'fileencoding', 'termencoding' and 'makeencoding'
+ else if (varp == &p_enc || gvarp == &p_fenc || varp == &p_tenc
+ || gvarp == &p_menc)
+ {
+ if (gvarp == &p_fenc)
+ {
+ if (!curbuf->b_p_ma && opt_flags != OPT_GLOBAL)
+ errmsg = e_modifiable;
+ else if (vim_strchr(*varp, ',') != NULL)
+ // No comma allowed in 'fileencoding'; catches confusing it
+ // with 'fileencodings'.
+ errmsg = e_invarg;
+ else
+ {
+#ifdef FEAT_TITLE
+ // May show a "+" in the title now.
+ redraw_titles();
+#endif
+ // Add 'fileencoding' to the swap file.
+ ml_setflags(curbuf);
+ }
+ }
+ if (errmsg == NULL)
+ {
+ // canonize the value, so that STRCMP() can be used on it
+ p = enc_canonize(*varp);
+ if (p != NULL)
+ {
+ vim_free(*varp);
+ *varp = p;
+ }
+ if (varp == &p_enc)
+ {
+ errmsg = mb_init();
+#ifdef FEAT_TITLE
+ redraw_titles();
+#endif
+ }
+ }
+
+#if defined(FEAT_GUI_GTK)
+ if (errmsg == NULL && varp == &p_tenc && gui.in_use)
+ {
+ // GTK+ 2 uses only a single encoding, and that is UTF-8.
+ if (STRCMP(p_tenc, "utf-8") != 0)
+ errmsg = N_("E617: Cannot be changed in the GTK+ 2 GUI");
+ }
+#endif
+
+ if (errmsg == NULL)
+ {
+#ifdef FEAT_KEYMAP
+ // When 'keymap' is used and 'encoding' changes, reload the keymap
+ // (with another encoding).
+ if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
+ (void)keymap_init();
+#endif
+
+ // When 'termencoding' is not empty and 'encoding' changes or when
+ // 'termencoding' changes, need to setup for keyboard input and
+ // display output conversion.
+ if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
+ {
+ if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
+ || convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
+ {
+ semsg(_("E950: Cannot convert between %s and %s"),
+ p_tenc, p_enc);
+ errmsg = e_invarg;
+ }
+ }
+
+#if defined(MSWIN)
+ // $HOME may have characters in active code page.
+ if (varp == &p_enc)
+ init_homedir();
+#endif
+ }
+ }
+
+#if defined(FEAT_POSTSCRIPT)
+ else if (varp == &p_penc)
+ {
+ // Canonize printencoding if VIM standard one
+ p = enc_canonize(p_penc);
+ if (p != NULL)
+ {
+ vim_free(p_penc);
+ p_penc = p;
+ }
+ else
+ {
+ // Ensure lower case and '-' for '_'
+ for (s = p_penc; *s != NUL; s++)
+ {
+ if (*s == '_')
+ *s = '-';
+ else
+ *s = TOLOWER_ASC(*s);
+ }
+ }
+ }
+#endif
+
+#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
+ else if (varp == &p_imak)
+ {
+ if (!im_xim_isvalid_imactivate())
+ errmsg = e_invarg;
+ }
+#endif
+
+#ifdef FEAT_KEYMAP
+ else if (varp == &curbuf->b_p_keymap)
+ {
+ if (!valid_filetype(*varp))
+ errmsg = e_invarg;
+ else
+ {
+ int secure_save = secure;
+
+ // Reset the secure flag, since the value of 'keymap' has
+ // been checked to be safe.
+ secure = 0;
+
+ // load or unload key mapping tables
+ errmsg = keymap_init();
+
+ secure = secure_save;
+
+ // Since we check the value, there is no need to set P_INSECURE,
+ // even when the value comes from a modeline.
+ *value_checked = TRUE;
+ }
+
+ if (errmsg == NULL)
+ {
+ if (*curbuf->b_p_keymap != NUL)
+ {
+ // Installed a new keymap, switch on using it.
+ curbuf->b_p_iminsert = B_IMODE_LMAP;
+ if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
+ curbuf->b_p_imsearch = B_IMODE_LMAP;
+ }
+ else
+ {
+ // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
+ if (curbuf->b_p_iminsert == B_IMODE_LMAP)
+ curbuf->b_p_iminsert = B_IMODE_NONE;
+ if (curbuf->b_p_imsearch == B_IMODE_LMAP)
+ curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
+ }
+ if ((opt_flags & OPT_LOCAL) == 0)
+ {
+ set_iminsert_global();
+ set_imsearch_global();
+ }
+ status_redraw_curbuf();
+ }
+ }
+#endif
+
+ // 'fileformat'
+ else if (gvarp == &p_ff)
+ {
+ if (!curbuf->b_p_ma && !(opt_flags & OPT_GLOBAL))
+ errmsg = e_modifiable;
+ else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
+ errmsg = e_invarg;
+ else
+ {
+ // may also change 'textmode'
+ if (get_fileformat(curbuf) == EOL_DOS)
+ curbuf->b_p_tx = TRUE;
+ else
+ curbuf->b_p_tx = FALSE;
+#ifdef FEAT_TITLE
+ redraw_titles();
+#endif
+ // update flag in swap file
+ ml_setflags(curbuf);
+ // Redraw needed when switching to/from "mac": a CR in the text
+ // will be displayed differently.
+ if (get_fileformat(curbuf) == EOL_MAC || *oldval == 'm')
+ redraw_curbuf_later(NOT_VALID);
+ }
+ }
+
+ // 'fileformats'
+ else if (varp == &p_ffs)
+ {
+ if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
+ errmsg = e_invarg;
+ else
+ {
+ // also change 'textauto'
+ if (*p_ffs == NUL)
+ p_ta = FALSE;
+ else
+ p_ta = TRUE;
+ }
+ }
+
+#if defined(FEAT_CRYPT)
+ // 'cryptkey'
+ else if (gvarp == &p_key)
+ {
+ // Make sure the ":set" command doesn't show the new value in the
+ // history.