1 /* vi:set ts=8 sts=4 sw=4 noet:
2  *
3  * VIM - Vi IMproved	by Bram Moolenaar
4  *
5  * Do ":help uganda"  in Vim to read copying and usage conditions.
6  * Do ":help credits" in Vim to see a list of people who contributed.
7  * See README.txt for an overview of the Vim source code.
8  */
9 
10 /*
11  * optionstr.c: Functions related to string options
12  */
13 
14 #include "vim.h"
15 
16 static char *(p_ambw_values[]) = {"single", "double", NULL};
17 static char *(p_bg_values[]) = {"light", "dark", NULL};
18 static char *(p_bkc_values[]) = {"yes", "auto", "no", "breaksymlink", "breakhardlink", NULL};
19 static char *(p_bo_values[]) = {"all", "backspace", "cursor", "complete",
20 				 "copy", "ctrlg", "error", "esc", "ex",
21 				 "hangul", "insertmode", "lang", "mess",
22 				 "showmatch", "operator", "register", "shell",
23 				 "spell", "wildmode", NULL};
24 static char *(p_nf_values[]) = {"bin", "octal", "hex", "alpha", "unsigned", NULL};
25 static char *(p_ff_values[]) = {FF_UNIX, FF_DOS, FF_MAC, NULL};
26 #ifdef FEAT_CRYPT
27 static char *(p_cm_values[]) = {"zip", "blowfish", "blowfish2",
28  # ifdef FEAT_SODIUM
29     "xchacha20",
30  # endif
31     NULL};
32 #endif
33 static char *(p_cmp_values[]) = {"internal", "keepascii", NULL};
34 static char *(p_dy_values[]) = {"lastline", "truncate", "uhex", NULL};
35 #ifdef FEAT_FOLDING
36 static char *(p_fdo_values[]) = {"all", "block", "hor", "mark", "percent",
37 				 "quickfix", "search", "tag", "insert",
38 				 "undo", "jump", NULL};
39 #endif
40 #ifdef FEAT_SESSION
41 // Also used for 'viewoptions'!  Keep in sync with SSOP_ flags.
42 static char *(p_ssop_values[]) = {"buffers", "winpos", "resize", "winsize",
43     "localoptions", "options", "help", "blank", "globals", "slash", "unix",
44     "sesdir", "curdir", "folds", "cursor", "tabpages", "terminal", "skiprtp",
45     NULL};
46 #endif
47 // Keep in sync with SWB_ flags in option.h
48 static char *(p_swb_values[]) = {"useopen", "usetab", "split", "newtab", "vsplit", "uselast", NULL};
49 static char *(p_tc_values[]) = {"followic", "ignore", "match", "followscs", "smart", NULL};
50 #if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
51 static char *(p_toolbar_values[]) = {"text", "icons", "tooltips", "horiz", NULL};
52 #endif
53 #if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
54 static char *(p_tbis_values[]) = {"tiny", "small", "medium", "large", "huge", "giant", NULL};
55 #endif
56 #if defined(UNIX) || defined(VMS)
57 static char *(p_ttym_values[]) = {"xterm", "xterm2", "dec", "netterm", "jsbterm", "pterm", "urxvt", "sgr", NULL};
58 #endif
59 static char *(p_ve_values[]) = {"block", "insert", "all", "onemore", "none", "NONE", NULL};
60 static char *(p_wop_values[]) = {"tagfile", NULL};
61 #ifdef FEAT_WAK
62 static char *(p_wak_values[]) = {"yes", "menu", "no", NULL};
63 #endif
64 static char *(p_mousem_values[]) = {"extend", "popup", "popup_setpos", "mac", NULL};
65 static char *(p_sel_values[]) = {"inclusive", "exclusive", "old", NULL};
66 static char *(p_slm_values[]) = {"mouse", "key", "cmd", NULL};
67 static char *(p_km_values[]) = {"startsel", "stopsel", NULL};
68 #ifdef FEAT_BROWSE
69 static char *(p_bsdir_values[]) = {"current", "last", "buffer", NULL};
70 #endif
71 static char *(p_scbopt_values[]) = {"ver", "hor", "jump", NULL};
72 static char *(p_debug_values[]) = {"msg", "throw", "beep", NULL};
73 static char *(p_ead_values[]) = {"both", "ver", "hor", NULL};
74 static char *(p_buftype_values[]) = {"nofile", "nowrite", "quickfix", "help", "terminal", "acwrite", "prompt", "popup", NULL};
75 static char *(p_bufhidden_values[]) = {"hide", "unload", "delete", "wipe", NULL};
76 static char *(p_bs_values[]) = {"indent", "eol", "start", "nostop", NULL};
77 #ifdef FEAT_FOLDING
78 static char *(p_fdm_values[]) = {"manual", "expr", "marker", "indent", "syntax",
79 # ifdef FEAT_DIFF
80 				"diff",
81 # endif
82 				NULL};
83 static char *(p_fcl_values[]) = {"all", NULL};
84 #endif
85 static char *(p_cot_values[]) = {"menu", "menuone", "longest", "preview", "popup", "popuphidden", "noinsert", "noselect", NULL};
86 #ifdef BACKSLASH_IN_FILENAME
87 static char *(p_csl_values[]) = {"slash", "backslash", NULL};
88 #endif
89 #ifdef FEAT_SIGNS
90 static char *(p_scl_values[]) = {"yes", "no", "auto", "number", NULL};
91 #endif
92 #if defined(MSWIN) && defined(FEAT_TERMINAL)
93 static char *(p_twt_values[]) = {"winpty", "conpty", "", NULL};
94 #endif
95 
96 static int check_opt_strings(char_u *val, char **values, int list);
97 static int opt_strings_flags(char_u *val, char **values, unsigned *flagp, int list);
98 
99 /*
100  * After setting various option values: recompute variables that depend on
101  * option values.
102  */
103     void
didset_string_options(void)104 didset_string_options(void)
105 {
106     (void)opt_strings_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
107     (void)opt_strings_flags(p_bkc, p_bkc_values, &bkc_flags, TRUE);
108     (void)opt_strings_flags(p_bo, p_bo_values, &bo_flags, TRUE);
109 #ifdef FEAT_SESSION
110     (void)opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE);
111     (void)opt_strings_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
112 #endif
113 #ifdef FEAT_FOLDING
114     (void)opt_strings_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
115 #endif
116     (void)opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE);
117     (void)opt_strings_flags(p_tc, p_tc_values, &tc_flags, FALSE);
118     (void)opt_strings_flags(p_ve, p_ve_values, &ve_flags, TRUE);
119 #if defined(UNIX) || defined(VMS)
120     (void)opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE);
121 #endif
122 #if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
123     (void)opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags, TRUE);
124 #endif
125 #if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
126     (void)opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE);
127 #endif
128 }
129 
130 #if defined(FEAT_EVAL)
131 /*
132  * Trigger the OptionSet autocommand.
133  * "opt_idx"	is the index of the option being set.
134  * "opt_flags"	can be OPT_LOCAL etc.
135  * "oldval"	the old value
136  *  "oldval_l"  the old local value (only non-NULL if global and local value
137  *		are set)
138  * "oldval_g"   the old global value (only non-NULL if global and local value
139  *		are set)
140  * "newval"	the new value
141  */
142     void
trigger_optionsset_string(int opt_idx,int opt_flags,char_u * oldval,char_u * oldval_l,char_u * oldval_g,char_u * newval)143 trigger_optionsset_string(
144 	int	opt_idx,
145 	int	opt_flags,
146 	char_u  *oldval,
147 	char_u  *oldval_l,
148 	char_u  *oldval_g,
149 	char_u  *newval)
150 {
151     // Don't do this recursively.
152     if (oldval != NULL && newval != NULL
153 				    && *get_vim_var_str(VV_OPTION_TYPE) == NUL)
154     {
155 	char_u buf_type[7];
156 
157 	sprintf((char *)buf_type, "%s",
158 	    (opt_flags & OPT_LOCAL) ? "local" : "global");
159 	set_vim_var_string(VV_OPTION_OLD, oldval, -1);
160 	set_vim_var_string(VV_OPTION_NEW, newval, -1);
161 	set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
162 	if (opt_flags & OPT_LOCAL)
163 	{
164 	    set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
165 	    set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
166 	}
167 	if (opt_flags & OPT_GLOBAL)
168 	{
169 	    set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
170 	    set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval, -1);
171 	}
172 	if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
173 	{
174 	    set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
175 	    set_vim_var_string(VV_OPTION_OLDLOCAL, oldval_l, -1);
176 	    set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval_g, -1);
177 	}
178 	if (opt_flags & OPT_MODELINE)
179 	{
180 	    set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
181 	    set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
182 	}
183 	apply_autocmds(EVENT_OPTIONSET,
184 		       (char_u *)get_option_fullname(opt_idx), NULL, FALSE,
185 		       NULL);
186 	reset_v_option_vars();
187     }
188 }
189 #endif
190 
191     static char *
illegal_char(char * errbuf,int c)192 illegal_char(char *errbuf, int c)
193 {
194     if (errbuf == NULL)
195 	return "";
196     sprintf((char *)errbuf, _("E539: Illegal character <%s>"),
197 							(char *)transchar(c));
198     return errbuf;
199 }
200 
201 /*
202  * Check string options in a buffer for NULL value.
203  */
204     void
check_buf_options(buf_T * buf)205 check_buf_options(buf_T *buf)
206 {
207     check_string_option(&buf->b_p_bh);
208     check_string_option(&buf->b_p_bt);
209     check_string_option(&buf->b_p_fenc);
210     check_string_option(&buf->b_p_ff);
211 #ifdef FEAT_FIND_ID
212     check_string_option(&buf->b_p_def);
213     check_string_option(&buf->b_p_inc);
214 # ifdef FEAT_EVAL
215     check_string_option(&buf->b_p_inex);
216 # endif
217 #endif
218 #if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
219     check_string_option(&buf->b_p_inde);
220     check_string_option(&buf->b_p_indk);
221 #endif
222 #if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
223     check_string_option(&buf->b_p_bexpr);
224 #endif
225 #if defined(FEAT_CRYPT)
226     check_string_option(&buf->b_p_cm);
227 #endif
228     check_string_option(&buf->b_p_fp);
229 #if defined(FEAT_EVAL)
230     check_string_option(&buf->b_p_fex);
231 #endif
232 #ifdef FEAT_CRYPT
233     check_string_option(&buf->b_p_key);
234 #endif
235     check_string_option(&buf->b_p_kp);
236     check_string_option(&buf->b_p_mps);
237     check_string_option(&buf->b_p_fo);
238     check_string_option(&buf->b_p_flp);
239     check_string_option(&buf->b_p_isk);
240     check_string_option(&buf->b_p_com);
241 #ifdef FEAT_FOLDING
242     check_string_option(&buf->b_p_cms);
243 #endif
244     check_string_option(&buf->b_p_nf);
245 #ifdef FEAT_TEXTOBJ
246     check_string_option(&buf->b_p_qe);
247 #endif
248 #ifdef FEAT_SYN_HL
249     check_string_option(&buf->b_p_syn);
250     check_string_option(&buf->b_s.b_syn_isk);
251 #endif
252 #ifdef FEAT_SPELL
253     check_string_option(&buf->b_s.b_p_spc);
254     check_string_option(&buf->b_s.b_p_spf);
255     check_string_option(&buf->b_s.b_p_spl);
256     check_string_option(&buf->b_s.b_p_spo);
257 #endif
258 #ifdef FEAT_SEARCHPATH
259     check_string_option(&buf->b_p_sua);
260 #endif
261 #ifdef FEAT_CINDENT
262     check_string_option(&buf->b_p_cink);
263     check_string_option(&buf->b_p_cino);
264     parse_cino(buf);
265 #endif
266     check_string_option(&buf->b_p_ft);
267 #if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
268     check_string_option(&buf->b_p_cinw);
269 #endif
270     check_string_option(&buf->b_p_cpt);
271 #ifdef FEAT_COMPL_FUNC
272     check_string_option(&buf->b_p_cfu);
273     check_string_option(&buf->b_p_ofu);
274     check_string_option(&buf->b_p_tsrfu);
275 #endif
276 #ifdef FEAT_EVAL
277     check_string_option(&buf->b_p_tfu);
278 #endif
279 #ifdef FEAT_KEYMAP
280     check_string_option(&buf->b_p_keymap);
281 #endif
282 #ifdef FEAT_QUICKFIX
283     check_string_option(&buf->b_p_gp);
284     check_string_option(&buf->b_p_mp);
285     check_string_option(&buf->b_p_efm);
286 #endif
287     check_string_option(&buf->b_p_ep);
288     check_string_option(&buf->b_p_path);
289     check_string_option(&buf->b_p_tags);
290     check_string_option(&buf->b_p_tc);
291     check_string_option(&buf->b_p_dict);
292     check_string_option(&buf->b_p_tsr);
293 #ifdef FEAT_LISP
294     check_string_option(&buf->b_p_lw);
295 #endif
296     check_string_option(&buf->b_p_bkc);
297     check_string_option(&buf->b_p_menc);
298 #ifdef FEAT_VARTABS
299     check_string_option(&buf->b_p_vsts);
300     check_string_option(&buf->b_p_vts);
301 #endif
302 }
303 
304 /*
305  * Free the string allocated for an option.
306  * Checks for the string being empty_option. This may happen if we're out of
307  * memory, vim_strsave() returned NULL, which was replaced by empty_option by
308  * check_options().
309  * Does NOT check for P_ALLOCED flag!
310  */
311     void
free_string_option(char_u * p)312 free_string_option(char_u *p)
313 {
314     if (p != empty_option)
315 	vim_free(p);
316 }
317 
318     void
clear_string_option(char_u ** pp)319 clear_string_option(char_u **pp)
320 {
321     if (*pp != empty_option)
322 	vim_free(*pp);
323     *pp = empty_option;
324 }
325 
326     void
check_string_option(char_u ** pp)327 check_string_option(char_u **pp)
328 {
329     if (*pp == NULL)
330 	*pp = empty_option;
331 }
332 
333 /*
334  * Set global value for string option when it's a local option.
335  */
336     static void
set_string_option_global(int opt_idx,char_u ** varp)337 set_string_option_global(
338     int		opt_idx,	// option index
339     char_u	**varp)		// pointer to option variable
340 {
341     char_u	**p, *s;
342 
343     // the global value is always allocated
344     if (is_window_local_option(opt_idx))
345 	p = (char_u **)GLOBAL_WO(varp);
346     else
347 	p = (char_u **)get_option_var(opt_idx);
348     if (!is_global_option(opt_idx)
349 	    && p != varp
350 	    && (s = vim_strsave(*varp)) != NULL)
351     {
352 	free_string_option(*p);
353 	*p = s;
354     }
355 }
356 
357 /*
358  * Set a string option to a new value (without checking the effect).
359  * The string is copied into allocated memory.
360  * if ("opt_idx" == -1) "name" is used, otherwise "opt_idx" is used.
361  * When "set_sid" is zero set the scriptID to current_sctx.sc_sid.  When
362  * "set_sid" is SID_NONE don't set the scriptID.  Otherwise set the scriptID to
363  * "set_sid".
364  */
365     void
set_string_option_direct(char_u * name,int opt_idx,char_u * val,int opt_flags,int set_sid UNUSED)366 set_string_option_direct(
367     char_u	*name,
368     int		opt_idx,
369     char_u	*val,
370     int		opt_flags,	// OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
371     int		set_sid UNUSED)
372 {
373     char_u	*s;
374     char_u	**varp;
375     int		both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
376     int		idx = opt_idx;
377 
378     if (idx == -1)		// use name
379     {
380 	idx = findoption(name);
381 	if (idx < 0)	// not found (should not happen)
382 	{
383 	    semsg(_(e_intern2), "set_string_option_direct()");
384 	    siemsg(_("For option %s"), name);
385 	    return;
386 	}
387     }
388 
389     if (is_hidden_option(idx))		// can't set hidden option
390 	return;
391 
392     s = vim_strsave(val);
393     if (s != NULL)
394     {
395 	varp = (char_u **)get_option_varp_scope(idx,
396 					       both ? OPT_LOCAL : opt_flags);
397 	if ((opt_flags & OPT_FREE) && (get_option_flags(idx) & P_ALLOCED))
398 	    free_string_option(*varp);
399 	*varp = s;
400 
401 	// For buffer/window local option may also set the global value.
402 	if (both)
403 	    set_string_option_global(idx, varp);
404 
405 	set_option_flag(idx, P_ALLOCED);
406 
407 	// When setting both values of a global option with a local value,
408 	// make the local value empty, so that the global value is used.
409 	if (is_global_local_option(idx) && both)
410 	{
411 	    free_string_option(*varp);
412 	    *varp = empty_option;
413 	}
414 # ifdef FEAT_EVAL
415 	if (set_sid != SID_NONE)
416 	{
417 	    sctx_T script_ctx;
418 
419 	    if (set_sid == 0)
420 		script_ctx = current_sctx;
421 	    else
422 	    {
423 		script_ctx.sc_sid = set_sid;
424 		script_ctx.sc_seq = 0;
425 		script_ctx.sc_lnum = 0;
426 		script_ctx.sc_version = 1;
427 	    }
428 	    set_option_sctx_idx(idx, opt_flags, script_ctx);
429 	}
430 # endif
431     }
432 }
433 
434 /*
435  * Like set_string_option_direct(), but for a window-local option in "wp".
436  * Blocks autocommands to avoid the old curwin becoming invalid.
437  */
438     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)439 set_string_option_direct_in_win(
440 	win_T		*wp,
441 	char_u		*name,
442 	int		opt_idx,
443 	char_u		*val,
444 	int		opt_flags,
445 	int		set_sid)
446 {
447     win_T	*save_curwin = curwin;
448 
449     block_autocmds();
450     curwin = wp;
451     curbuf = curwin->w_buffer;
452     set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
453     curwin = save_curwin;
454     curbuf = curwin->w_buffer;
455     unblock_autocmds();
456 }
457 
458 /*
459  * Like set_string_option_direct(), but for a buffer-local option in "buf".
460  * Blocks autocommands to avoid the old curbuf becoming invalid.
461  */
462     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)463 set_string_option_direct_in_buf(
464 	buf_T		*buf,
465 	char_u		*name,
466 	int		opt_idx,
467 	char_u		*val,
468 	int		opt_flags,
469 	int		set_sid)
470 {
471     buf_T	*save_curbuf = curbuf;
472 
473     block_autocmds();
474     curbuf = buf;
475     curwin->w_buffer = curbuf;
476     set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
477     curbuf = save_curbuf;
478     curwin->w_buffer = curbuf;
479     unblock_autocmds();
480 }
481 
482 /*
483  * Set a string option to a new value, and handle the effects.
484  *
485  * Returns NULL on success or error message on error.
486  */
487     char *
set_string_option(int opt_idx,char_u * value,int opt_flags)488 set_string_option(
489     int		opt_idx,
490     char_u	*value,
491     int		opt_flags)	// OPT_LOCAL and/or OPT_GLOBAL
492 {
493     char_u	*s;
494     char_u	**varp;
495     char_u	*oldval;
496 #if defined(FEAT_EVAL)
497     char_u	*oldval_l = NULL;
498     char_u	*oldval_g = NULL;
499     char_u	*saved_oldval = NULL;
500     char_u	*saved_oldval_l = NULL;
501     char_u	*saved_oldval_g = NULL;
502     char_u	*saved_newval = NULL;
503 #endif
504     char	*r = NULL;
505     int		value_checked = FALSE;
506 
507     if (is_hidden_option(opt_idx))	// don't set hidden option
508 	return NULL;
509 
510     s = vim_strsave(value == NULL ? (char_u *)"" : value);
511     if (s != NULL)
512     {
513 	varp = (char_u **)get_option_varp_scope(opt_idx,
514 		(opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
515 		    ? (is_global_local_option(opt_idx)
516 			? OPT_GLOBAL : OPT_LOCAL)
517 		    : opt_flags);
518 	oldval = *varp;
519 #if defined(FEAT_EVAL)
520 	if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
521 	{
522 	    oldval_l = *(char_u **)get_option_varp_scope(opt_idx, OPT_LOCAL);
523 	    oldval_g = *(char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
524 	}
525 #endif
526 	*varp = s;
527 
528 #if defined(FEAT_EVAL)
529 	if (!starting
530 # ifdef FEAT_CRYPT
531 		&& !is_crypt_key_option(opt_idx)
532 # endif
533 		)
534 	{
535 	    if (oldval_l != NULL)
536 		saved_oldval_l = vim_strsave(oldval_l);
537 	    if (oldval_g != NULL)
538 		saved_oldval_g = vim_strsave(oldval_g);
539 	    saved_oldval = vim_strsave(oldval);
540 	    saved_newval = vim_strsave(s);
541 	}
542 #endif
543 	if ((r = did_set_string_option(opt_idx, varp, TRUE, oldval, NULL,
544 					   opt_flags, &value_checked)) == NULL)
545 	    did_set_option(opt_idx, opt_flags, TRUE, value_checked);
546 
547 #if defined(FEAT_EVAL)
548 	// call autocommand after handling side effects
549 	if (r == NULL)
550 	    trigger_optionsset_string(opt_idx, opt_flags,
551 				   saved_oldval, saved_oldval_l,
552 				   saved_oldval_g, saved_newval);
553 	vim_free(saved_oldval);
554 	vim_free(saved_oldval_l);
555 	vim_free(saved_oldval_g);
556 	vim_free(saved_newval);
557 #endif
558     }
559     return r;
560 }
561 
562 /*
563  * Return TRUE if "val" is a valid 'filetype' name.
564  * Also used for 'syntax' and 'keymap'.
565  */
566     static int
valid_filetype(char_u * val)567 valid_filetype(char_u *val)
568 {
569     return valid_name(val, ".-_");
570 }
571 
572 #ifdef FEAT_STL_OPT
573 /*
574  * Check validity of options with the 'statusline' format.
575  * Return error message or NULL.
576  */
577     static char *
check_stl_option(char_u * s)578 check_stl_option(char_u *s)
579 {
580     int		groupdepth = 0;
581     static char errbuf[80];
582 
583     while (*s)
584     {
585 	// Check for valid keys after % sequences
586 	while (*s && *s != '%')
587 	    s++;
588 	if (!*s)
589 	    break;
590 	s++;
591 	if (*s == '%' || *s == STL_TRUNCMARK || *s == STL_MIDDLEMARK)
592 	{
593 	    s++;
594 	    continue;
595 	}
596 	if (*s == ')')
597 	{
598 	    s++;
599 	    if (--groupdepth < 0)
600 		break;
601 	    continue;
602 	}
603 	if (*s == '-')
604 	    s++;
605 	while (VIM_ISDIGIT(*s))
606 	    s++;
607 	if (*s == STL_USER_HL)
608 	    continue;
609 	if (*s == '.')
610 	{
611 	    s++;
612 	    while (*s && VIM_ISDIGIT(*s))
613 		s++;
614 	}
615 	if (*s == '(')
616 	{
617 	    groupdepth++;
618 	    continue;
619 	}
620 	if (vim_strchr(STL_ALL, *s) == NULL)
621 	{
622 	    return illegal_char(errbuf, *s);
623 	}
624 	if (*s == '{')
625 	{
626 	    int reevaluate = (*s == '%');
627 
628 	    s++;
629 	    while ((*s != '}' || (reevaluate && s[-1] != '%')) && *s)
630 		s++;
631 	    if (*s != '}')
632 		return N_("E540: Unclosed expression sequence");
633 	}
634     }
635     if (groupdepth != 0)
636 	return N_("E542: unbalanced groups");
637     return NULL;
638 }
639 #endif
640 
641 /*
642  * Handle string options that need some action to perform when changed.
643  * Returns NULL for success, or an error message for an error.
644  */
645     char *
did_set_string_option(int opt_idx,char_u ** varp,int new_value_alloced,char_u * oldval,char * errbuf,int opt_flags,int * value_checked)646 did_set_string_option(
647     int		opt_idx,		// index in options[] table
648     char_u	**varp,			// pointer to the option variable
649     int		new_value_alloced,	// new value was allocated
650     char_u	*oldval,		// previous value of the option
651     char	*errbuf,		// buffer for errors, or NULL
652     int		opt_flags,		// OPT_LOCAL and/or OPT_GLOBAL
653     int		*value_checked)		// value was checked to be save, no
654 					// need to set P_INSECURE
655 {
656     char	*errmsg = NULL;
657     char_u	*s, *p;
658     int		did_chartab = FALSE;
659     char_u	**gvarp;
660     long_u	free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
661 #ifdef FEAT_GUI
662     // set when changing an option that only requires a redraw in the GUI
663     int		redraw_gui_only = FALSE;
664 #endif
665     int		value_changed = FALSE;
666 #if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
667     int		did_swaptcap = FALSE;
668 #endif
669 
670     // Get the global option to compare with, otherwise we would have to check
671     // two values for all local options.
672     gvarp = (char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
673 
674     // Disallow changing some options from secure mode
675     if ((secure
676 #ifdef HAVE_SANDBOX
677 		|| sandbox != 0
678 #endif
679 		) && (get_option_flags(opt_idx) & P_SECURE))
680 	errmsg = e_secure;
681 
682     // Check for a "normal" directory or file name in some options.  Disallow a
683     // path separator (slash and/or backslash), wildcards and characters that
684     // are often illegal in a file name. Be more permissive if "secure" is off.
685     else if (((get_option_flags(opt_idx) & P_NFNAME)
686 		    && vim_strpbrk(*varp, (char_u *)(secure
687 			    ? "/\\*?[|;&<>\r\n" : "/\\*?[<>\r\n")) != NULL)
688 	  || ((get_option_flags(opt_idx) & P_NDNAME)
689 		    && vim_strpbrk(*varp, (char_u *)"*?[|;&<>\r\n") != NULL))
690 	errmsg = e_invarg;
691 
692     // 'term'
693     else if (varp == &T_NAME)
694     {
695 	if (T_NAME[0] == NUL)
696 	    errmsg = N_("E529: Cannot set 'term' to empty string");
697 #ifdef FEAT_GUI
698 	else if (gui.in_use)
699 	    errmsg = N_("E530: Cannot change term in GUI");
700 	else if (term_is_gui(T_NAME))
701 	    errmsg = N_("E531: Use \":gui\" to start the GUI");
702 #endif
703 	else if (set_termname(T_NAME) == FAIL)
704 	    errmsg = N_("E522: Not found in termcap");
705 	else
706 	{
707 	    // Screen colors may have changed.
708 	    redraw_later_clear();
709 
710 	    // Both 'term' and 'ttytype' point to T_NAME, only set the
711 	    // P_ALLOCED flag on 'term'.
712 	    opt_idx = findoption((char_u *)"term");
713 	    free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
714 	}
715     }
716 
717     // 'backupcopy'
718     else if (gvarp == &p_bkc)
719     {
720 	char_u		*bkc = p_bkc;
721 	unsigned int	*flags = &bkc_flags;
722 
723 	if (opt_flags & OPT_LOCAL)
724 	{
725 	    bkc = curbuf->b_p_bkc;
726 	    flags = &curbuf->b_bkc_flags;
727 	}
728 
729 	if ((opt_flags & OPT_LOCAL) && *bkc == NUL)
730 	    // make the local value empty: use the global value
731 	    *flags = 0;
732 	else
733 	{
734 	    if (opt_strings_flags(bkc, p_bkc_values, flags, TRUE) != OK)
735 		errmsg = e_invarg;
736 	    if ((((int)*flags & BKC_AUTO) != 0)
737 		    + (((int)*flags & BKC_YES) != 0)
738 		    + (((int)*flags & BKC_NO) != 0) != 1)
739 	    {
740 		// Must have exactly one of "auto", "yes"  and "no".
741 		(void)opt_strings_flags(oldval, p_bkc_values, flags, TRUE);
742 		errmsg = e_invarg;
743 	    }
744 	}
745     }
746 
747     // 'backupext' and 'patchmode'
748     else if (varp == &p_bex || varp == &p_pm)
749     {
750 	if (STRCMP(*p_bex == '.' ? p_bex + 1 : p_bex,
751 		     *p_pm == '.' ? p_pm + 1 : p_pm) == 0)
752 	    errmsg = N_("E589: 'backupext' and 'patchmode' are equal");
753     }
754 #ifdef FEAT_LINEBREAK
755     // 'breakindentopt'
756     else if (varp == &curwin->w_p_briopt)
757     {
758 	if (briopt_check(curwin) == FAIL)
759 	    errmsg = e_invarg;
760     }
761 #endif
762 
763     // 'isident', 'iskeyword', 'isprint or 'isfname' option: refill g_chartab[]
764     // If the new option is invalid, use old value.  'lisp' option: refill
765     // g_chartab[] for '-' char
766     else if (  varp == &p_isi
767 	    || varp == &(curbuf->b_p_isk)
768 	    || varp == &p_isp
769 	    || varp == &p_isf)
770     {
771 	if (init_chartab() == FAIL)
772 	{
773 	    did_chartab = TRUE;	    // need to restore it below
774 	    errmsg = e_invarg;	    // error in value
775 	}
776     }
777 
778     // 'helpfile'
779     else if (varp == &p_hf)
780     {
781 	// May compute new values for $VIM and $VIMRUNTIME
782 	if (didset_vim)
783 	{
784 	    vim_setenv((char_u *)"VIM", (char_u *)"");
785 	    didset_vim = FALSE;
786 	}
787 	if (didset_vimruntime)
788 	{
789 	    vim_setenv((char_u *)"VIMRUNTIME", (char_u *)"");
790 	    didset_vimruntime = FALSE;
791 	}
792     }
793 
794 #ifdef FEAT_SYN_HL
795     // 'cursorlineopt'
796     else if (varp == &curwin->w_p_culopt
797 				  || gvarp == &curwin->w_allbuf_opt.wo_culopt)
798     {
799 	if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
800 	    errmsg = e_invarg;
801     }
802 
803     // 'colorcolumn'
804     else if (varp == &curwin->w_p_cc)
805 	errmsg = check_colorcolumn(curwin);
806 #endif
807 
808 #ifdef FEAT_MULTI_LANG
809     // 'helplang'
810     else if (varp == &p_hlg)
811     {
812 	// Check for "", "ab", "ab,cd", etc.
813 	for (s = p_hlg; *s != NUL; s += 3)
814 	{
815 	    if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
816 	    {
817 		errmsg = e_invarg;
818 		break;
819 	    }
820 	    if (s[2] == NUL)
821 		break;
822 	}
823     }
824 #endif
825 
826     // 'highlight'
827     else if (varp == &p_hl)
828     {
829 	if (highlight_changed() == FAIL)
830 	    errmsg = e_invarg;	// invalid flags
831     }
832 
833     // 'nrformats'
834     else if (gvarp == &p_nf)
835     {
836 	if (check_opt_strings(*varp, p_nf_values, TRUE) != OK)
837 	    errmsg = e_invarg;
838     }
839 
840 #ifdef FEAT_SESSION
841     // 'sessionoptions'
842     else if (varp == &p_ssop)
843     {
844 	if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
845 	    errmsg = e_invarg;
846 	if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
847 	{
848 	    // Don't allow both "sesdir" and "curdir".
849 	    (void)opt_strings_flags(oldval, p_ssop_values, &ssop_flags, TRUE);
850 	    errmsg = e_invarg;
851 	}
852     }
853     // 'viewoptions'
854     else if (varp == &p_vop)
855     {
856 	if (opt_strings_flags(p_vop, p_ssop_values, &vop_flags, TRUE) != OK)
857 	    errmsg = e_invarg;
858     }
859 #endif
860 
861     // 'scrollopt'
862     else if (varp == &p_sbo)
863     {
864 	if (check_opt_strings(p_sbo, p_scbopt_values, TRUE) != OK)
865 	    errmsg = e_invarg;
866     }
867 
868     // 'ambiwidth'
869     else if (varp == &p_ambw || varp == &p_emoji)
870     {
871 	if (check_opt_strings(p_ambw, p_ambw_values, FALSE) != OK)
872 	    errmsg = e_invarg;
873 	else if (set_chars_option(curwin, &p_fcs) != NULL)
874 	    errmsg = _(e_conflicts_with_value_of_fillchars);
875 	else
876 	{
877 	    tabpage_T	*tp;
878 	    win_T	*wp;
879 
880 	    FOR_ALL_TAB_WINDOWS(tp, wp)
881 	    {
882 		if (set_chars_option(wp, &wp->w_p_lcs) != NULL)
883 		{
884 		    errmsg = _(e_conflicts_with_value_of_listchars);
885 		    goto ambw_end;
886 		}
887 	    }
888 	}
889 ambw_end:
890 	{}
891     }
892 
893     // 'background'
894     else if (varp == &p_bg)
895     {
896 	if (check_opt_strings(p_bg, p_bg_values, FALSE) == OK)
897 	{
898 #ifdef FEAT_EVAL
899 	    int dark = (*p_bg == 'd');
900 #endif
901 
902 	    init_highlight(FALSE, FALSE);
903 
904 #ifdef FEAT_EVAL
905 	    if (dark != (*p_bg == 'd')
906 			  && get_var_value((char_u *)"g:colors_name") != NULL)
907 	    {
908 		// The color scheme must have set 'background' back to another
909 		// value, that's not what we want here.  Disable the color
910 		// scheme and set the colors again.
911 		do_unlet((char_u *)"g:colors_name", TRUE);
912 		free_string_option(p_bg);
913 		p_bg = vim_strsave((char_u *)(dark ? "dark" : "light"));
914 		check_string_option(&p_bg);
915 		init_highlight(FALSE, FALSE);
916 	    }
917 #endif
918 #ifdef FEAT_TERMINAL
919 	    term_update_colors_all();
920 #endif
921 	}
922 	else
923 	    errmsg = e_invarg;
924     }
925 
926     // 'wildmode'
927     else if (varp == &p_wim)
928     {
929 	if (check_opt_wim() == FAIL)
930 	    errmsg = e_invarg;
931     }
932 
933     // 'wildoptions'
934     else if (varp == &p_wop)
935     {
936 	if (check_opt_strings(p_wop, p_wop_values, TRUE) != OK)
937 	    errmsg = e_invarg;
938     }
939 
940 #ifdef FEAT_WAK
941     // 'winaltkeys'
942     else if (varp == &p_wak)
943     {
944 	if (*p_wak == NUL
945 		|| check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
946 	    errmsg = e_invarg;
947 # ifdef FEAT_MENU
948 #  ifdef FEAT_GUI_MOTIF
949 	else if (gui.in_use)
950 	    gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
951 #  else
952 #   ifdef FEAT_GUI_GTK
953 	else if (gui.in_use)
954 	    gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
955 #   endif
956 #  endif
957 # endif
958     }
959 #endif
960 
961     // 'eventignore'
962     else if (varp == &p_ei)
963     {
964 	if (check_ei() == FAIL)
965 	    errmsg = e_invarg;
966     }
967 
968     // 'encoding', 'fileencoding', 'termencoding' and 'makeencoding'
969     else if (varp == &p_enc || gvarp == &p_fenc || varp == &p_tenc
970 							   || gvarp == &p_menc)
971     {
972 	if (gvarp == &p_fenc)
973 	{
974 	    if (!curbuf->b_p_ma && opt_flags != OPT_GLOBAL)
975 		errmsg = e_cannot_make_changes_modifiable_is_off;
976 	    else if (vim_strchr(*varp, ',') != NULL)
977 		// No comma allowed in 'fileencoding'; catches confusing it
978 		// with 'fileencodings'.
979 		errmsg = e_invarg;
980 	    else
981 	    {
982 		// May show a "+" in the title now.
983 		redraw_titles();
984 		// Add 'fileencoding' to the swap file.
985 		ml_setflags(curbuf);
986 	    }
987 	}
988 	if (errmsg == NULL)
989 	{
990 	    // canonize the value, so that STRCMP() can be used on it
991 	    p = enc_canonize(*varp);
992 	    if (p != NULL)
993 	    {
994 		vim_free(*varp);
995 		*varp = p;
996 	    }
997 	    if (varp == &p_enc)
998 	    {
999 		errmsg = mb_init();
1000 		redraw_titles();
1001 	    }
1002 	}
1003 
1004 #if defined(FEAT_GUI_GTK)
1005 	if (errmsg == NULL && varp == &p_tenc && gui.in_use)
1006 	{
1007 	    // GTK+ 2 uses only a single encoding, and that is UTF-8.
1008 	    if (STRCMP(p_tenc, "utf-8") != 0)
1009 		errmsg = N_("E617: Cannot be changed in the GTK+ 2 GUI");
1010 	}
1011 #endif
1012 
1013 	if (errmsg == NULL)
1014 	{
1015 #ifdef FEAT_KEYMAP
1016 	    // When 'keymap' is used and 'encoding' changes, reload the keymap
1017 	    // (with another encoding).
1018 	    if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
1019 		(void)keymap_init();
1020 #endif
1021 
1022 	    // When 'termencoding' is not empty and 'encoding' changes or when
1023 	    // 'termencoding' changes, need to setup for keyboard input and
1024 	    // display output conversion.
1025 	    if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
1026 	    {
1027 		if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
1028 			|| convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
1029 		{
1030 		    semsg(_("E950: Cannot convert between %s and %s"),
1031 			    p_tenc, p_enc);
1032 		    errmsg = e_invarg;
1033 		}
1034 	    }
1035 
1036 #if defined(MSWIN)
1037 	    // $HOME may have characters in active code page.
1038 	    if (varp == &p_enc)
1039 		init_homedir();
1040 #endif
1041 	}
1042     }
1043 
1044 #if defined(FEAT_POSTSCRIPT)
1045     else if (varp == &p_penc)
1046     {
1047 	// Canonize printencoding if VIM standard one
1048 	p = enc_canonize(p_penc);
1049 	if (p != NULL)
1050 	{
1051 	    vim_free(p_penc);
1052 	    p_penc = p;
1053 	}
1054 	else
1055 	{
1056 	    // Ensure lower case and '-' for '_'
1057 	    for (s = p_penc; *s != NUL; s++)
1058 	    {
1059 		if (*s == '_')
1060 		    *s = '-';
1061 		else
1062 		    *s = TOLOWER_ASC(*s);
1063 	    }
1064 	}
1065     }
1066 #endif
1067 
1068 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
1069     else if (varp == &p_imak)
1070     {
1071 	if (!im_xim_isvalid_imactivate())
1072 	    errmsg = e_invarg;
1073     }
1074 #endif
1075 
1076 #ifdef FEAT_KEYMAP
1077     else if (varp == &curbuf->b_p_keymap)
1078     {
1079 	if (!valid_filetype(*varp))
1080 	    errmsg = e_invarg;
1081 	else
1082 	{
1083 	    int	    secure_save = secure;
1084 
1085 	    // Reset the secure flag, since the value of 'keymap' has
1086 	    // been checked to be safe.
1087 	    secure = 0;
1088 
1089 	    // load or unload key mapping tables
1090 	    errmsg = keymap_init();
1091 
1092 	    secure = secure_save;
1093 
1094 	    // Since we check the value, there is no need to set P_INSECURE,
1095 	    // even when the value comes from a modeline.
1096 	    *value_checked = TRUE;
1097 	}
1098 
1099 	if (errmsg == NULL)
1100 	{
1101 	    if (*curbuf->b_p_keymap != NUL)
1102 	    {
1103 		// Installed a new keymap, switch on using it.
1104 		curbuf->b_p_iminsert = B_IMODE_LMAP;
1105 		if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
1106 		    curbuf->b_p_imsearch = B_IMODE_LMAP;
1107 	    }
1108 	    else
1109 	    {
1110 		// Cleared the keymap, may reset 'iminsert' and 'imsearch'.
1111 		if (curbuf->b_p_iminsert == B_IMODE_LMAP)
1112 		    curbuf->b_p_iminsert = B_IMODE_NONE;
1113 		if (curbuf->b_p_imsearch == B_IMODE_LMAP)
1114 		    curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
1115 	    }
1116 	    if ((opt_flags & OPT_LOCAL) == 0)
1117 	    {
1118 		set_iminsert_global();
1119 		set_imsearch_global();
1120 	    }
1121 	    status_redraw_curbuf();
1122 	}
1123     }
1124 #endif
1125 
1126     // 'fileformat'
1127     else if (gvarp == &p_ff)
1128     {
1129 	if (!curbuf->b_p_ma && !(opt_flags & OPT_GLOBAL))
1130 	    errmsg = e_cannot_make_changes_modifiable_is_off;
1131 	else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
1132 	    errmsg = e_invarg;
1133 	else
1134 	{
1135 	    // may also change 'textmode'
1136 	    if (get_fileformat(curbuf) == EOL_DOS)
1137 		curbuf->b_p_tx = TRUE;
1138 	    else
1139 		curbuf->b_p_tx = FALSE;
1140 	    redraw_titles();
1141 	    // update flag in swap file
1142 	    ml_setflags(curbuf);
1143 	    // Redraw needed when switching to/from "mac": a CR in the text
1144 	    // will be displayed differently.
1145 	    if (get_fileformat(curbuf) == EOL_MAC || *oldval == 'm')
1146 		redraw_curbuf_later(NOT_VALID);
1147 	}
1148     }
1149 
1150     // 'fileformats'
1151     else if (varp == &p_ffs)
1152     {
1153 	if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
1154 	    errmsg = e_invarg;
1155 	else
1156 	{
1157 	    // also change 'textauto'
1158 	    if (*p_ffs == NUL)
1159 		p_ta = FALSE;
1160 	    else
1161 		p_ta = TRUE;
1162 	}
1163     }
1164 
1165 #if defined(FEAT_CRYPT)
1166     // 'cryptkey'
1167     else if (gvarp == &p_key)
1168     {
1169 	// Make sure the ":set" command doesn't show the new value in the
1170 	// history.
1171 	remove_key_from_history();
1172 
1173 	if (STRCMP(curbuf->b_p_key, oldval) != 0)
1174 	    // Need to update the swapfile.
1175 	{
1176 	    ml_set_crypt_key(curbuf, oldval,
1177 			      *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
1178 	    changed_internal();
1179 	}
1180     }
1181 
1182     else if (gvarp == &p_cm)
1183     {
1184 	if (opt_flags & OPT_LOCAL)
1185 	    p = curbuf->b_p_cm;
1186 	else
1187 	    p = p_cm;
1188 	if (check_opt_strings(p, p_cm_values, TRUE) != OK)
1189 	    errmsg = e_invarg;
1190 	else if (crypt_self_test() == FAIL)
1191 	    errmsg = e_invarg;
1192 	else
1193 	{
1194 	    // When setting the global value to empty, make it "zip".
1195 	    if (*p_cm == NUL)
1196 	    {
1197 		if (new_value_alloced)
1198 		    free_string_option(p_cm);
1199 		p_cm = vim_strsave((char_u *)"zip");
1200 		new_value_alloced = TRUE;
1201 	    }
1202 	    // When using ":set cm=name" the local value is going to be empty.
1203 	    // Do that here, otherwise the crypt functions will still use the
1204 	    // local value.
1205 	    if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1206 	    {
1207 		free_string_option(curbuf->b_p_cm);
1208 		curbuf->b_p_cm = empty_option;
1209 	    }
1210 
1211 	    // Need to update the swapfile when the effective method changed.
1212 	    // Set "s" to the effective old value, "p" to the effective new
1213 	    // method and compare.
1214 	    if ((opt_flags & OPT_LOCAL) && *oldval == NUL)
1215 		s = p_cm;  // was previously using the global value
1216 	    else
1217 		s = oldval;
1218 	    if (*curbuf->b_p_cm == NUL)
1219 		p = p_cm;  // is now using the global value
1220 	    else
1221 		p = curbuf->b_p_cm;
1222 	    if (STRCMP(s, p) != 0)
1223 		ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1224 
1225 	    // If the global value changes need to update the swapfile for all
1226 	    // buffers using that value.
1227 	    if ((opt_flags & OPT_GLOBAL) && STRCMP(p_cm, oldval) != 0)
1228 	    {
1229 		buf_T	*buf;
1230 
1231 		FOR_ALL_BUFFERS(buf)
1232 		    if (buf != curbuf && *buf->b_p_cm == NUL)
1233 			ml_set_crypt_key(buf, buf->b_p_key, oldval);
1234 	    }
1235 	}
1236     }
1237 #endif
1238 
1239     // 'matchpairs'
1240     else if (gvarp == &p_mps)
1241     {
1242 	if (has_mbyte)
1243 	{
1244 	    for (p = *varp; *p != NUL; ++p)
1245 	    {
1246 		int x2 = -1;
1247 		int x3 = -1;
1248 
1249 		if (*p != NUL)
1250 		    p += mb_ptr2len(p);
1251 		if (*p != NUL)
1252 		    x2 = *p++;
1253 		if (*p != NUL)
1254 		{
1255 		    x3 = mb_ptr2char(p);
1256 		    p += mb_ptr2len(p);
1257 		}
1258 		if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
1259 		{
1260 		    errmsg = e_invarg;
1261 		    break;
1262 		}
1263 		if (*p == NUL)
1264 		    break;
1265 	    }
1266 	}
1267 	else
1268 	{
1269 	    // Check for "x:y,x:y"
1270 	    for (p = *varp; *p != NUL; p += 4)
1271 	    {
1272 		if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
1273 		{
1274 		    errmsg = e_invarg;
1275 		    break;
1276 		}
1277 		if (p[3] == NUL)
1278 		    break;
1279 	    }
1280 	}
1281     }
1282 
1283     // 'comments'
1284     else if (gvarp == &p_com)
1285     {
1286 	for (s = *varp; *s; )
1287 	{
1288 	    while (*s && *s != ':')
1289 	    {
1290 		if (vim_strchr((char_u *)COM_ALL, *s) == NULL
1291 					     && !VIM_ISDIGIT(*s) && *s != '-')
1292 		{
1293 		    errmsg = illegal_char(errbuf, *s);
1294 		    break;
1295 		}
1296 		++s;
1297 	    }
1298 	    if (*s++ == NUL)
1299 		errmsg = N_("E524: Missing colon");
1300 	    else if (*s == ',' || *s == NUL)
1301 		errmsg = N_("E525: Zero length string");
1302 	    if (errmsg != NULL)
1303 		break;
1304 	    while (*s && *s != ',')
1305 	    {
1306 		if (*s == '\\' && s[1] != NUL)
1307 		    ++s;
1308 		++s;
1309 	    }
1310 	    s = skip_to_option_part(s);
1311 	}
1312     }
1313 
1314     // global 'listchars'
1315     else if (varp == &p_lcs)
1316     {
1317 	errmsg = set_chars_option(curwin, varp);
1318 	if (errmsg == NULL)
1319 	{
1320 	    tabpage_T	*tp;
1321 	    win_T		*wp;
1322 
1323 	    // The current window is set to use the global 'listchars' value.
1324 	    // So clear the window-local value.
1325 	    if (!(opt_flags & OPT_GLOBAL))
1326 		clear_string_option(&curwin->w_p_lcs);
1327 	    FOR_ALL_TAB_WINDOWS(tp, wp)
1328 		// If no error was returned above, we don't expect an error
1329 		// here, so ignore the return value.
1330 		(void)set_chars_option(wp, &wp->w_p_lcs);
1331 
1332 	    redraw_all_later(NOT_VALID);
1333 	}
1334     }
1335 
1336     // local 'listchars'
1337     else if (varp == &curwin->w_p_lcs)
1338 	errmsg = set_chars_option(curwin, varp);
1339 
1340     // 'fillchars'
1341     else if (varp == &p_fcs)
1342     {
1343 	errmsg = set_chars_option(curwin, varp);
1344     }
1345 
1346 #ifdef FEAT_CMDWIN
1347     // 'cedit'
1348     else if (varp == &p_cedit)
1349     {
1350 	errmsg = check_cedit();
1351     }
1352 #endif
1353 
1354     // 'verbosefile'
1355     else if (varp == &p_vfile)
1356     {
1357 	verbose_stop();
1358 	if (*p_vfile != NUL && verbose_open() == FAIL)
1359 	    errmsg = e_invarg;
1360     }
1361 
1362 #ifdef FEAT_VIMINFO
1363     // 'viminfo'
1364     else if (varp == &p_viminfo)
1365     {
1366 	for (s = p_viminfo; *s;)
1367 	{
1368 	    // Check it's a valid character
1369 	    if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
1370 	    {
1371 		errmsg = illegal_char(errbuf, *s);
1372 		break;
1373 	    }
1374 	    if (*s == 'n')	// name is always last one
1375 		break;
1376 	    else if (*s == 'r') // skip until next ','
1377 	    {
1378 		while (*++s && *s != ',')
1379 		    ;
1380 	    }
1381 	    else if (*s == '%')
1382 	    {
1383 		// optional number
1384 		while (vim_isdigit(*++s))
1385 		    ;
1386 	    }
1387 	    else if (*s == '!' || *s == 'h' || *s == 'c')
1388 		++s;		// no extra chars
1389 	    else		// must have a number
1390 	    {
1391 		while (vim_isdigit(*++s))
1392 		    ;
1393 
1394 		if (!VIM_ISDIGIT(*(s - 1)))
1395 		{
1396 		    if (errbuf != NULL)
1397 		    {
1398 			sprintf(errbuf, _("E526: Missing number after <%s>"),
1399 						    transchar_byte(*(s - 1)));
1400 			errmsg = errbuf;
1401 		    }
1402 		    else
1403 			errmsg = "";
1404 		    break;
1405 		}
1406 	    }
1407 	    if (*s == ',')
1408 		++s;
1409 	    else if (*s)
1410 	    {
1411 		if (errbuf != NULL)
1412 		    errmsg = N_("E527: Missing comma");
1413 		else
1414 		    errmsg = "";
1415 		break;
1416 	    }
1417 	}
1418 	if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
1419 	    errmsg = N_("E528: Must specify a ' value");
1420     }
1421 #endif // FEAT_VIMINFO
1422 
1423     // terminal options
1424     else if (istermoption_idx(opt_idx) && full_screen)
1425     {
1426 	// ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
1427 	if (varp == &T_CCO)
1428 	{
1429 	    int colors = atoi((char *)T_CCO);
1430 
1431 	    // Only reinitialize colors if t_Co value has really changed to
1432 	    // avoid expensive reload of colorscheme if t_Co is set to the
1433 	    // same value multiple times.
1434 	    if (colors != t_colors)
1435 	    {
1436 		t_colors = colors;
1437 		if (t_colors <= 1)
1438 		{
1439 		    if (new_value_alloced)
1440 			vim_free(T_CCO);
1441 		    T_CCO = empty_option;
1442 		}
1443 #if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1444 		if (is_term_win32())
1445 		{
1446 		    swap_tcap();
1447 		    did_swaptcap = TRUE;
1448 		}
1449 #endif
1450 		// We now have a different color setup, initialize it again.
1451 		init_highlight(TRUE, FALSE);
1452 	    }
1453 	}
1454 	ttest(FALSE);
1455 	if (varp == &T_ME)
1456 	{
1457 	    out_str(T_ME);
1458 	    redraw_later(CLEAR);
1459 #if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
1460 	    // Since t_me has been set, this probably means that the user
1461 	    // wants to use this as default colors.  Need to reset default
1462 	    // background/foreground colors.
1463 # ifdef VIMDLL
1464 	    if (!gui.in_use && !gui.starting)
1465 # endif
1466 		mch_set_normal_colors();
1467 #endif
1468 	}
1469 	if (varp == &T_BE && termcap_active)
1470 	{
1471 #ifdef FEAT_JOB_CHANNEL
1472 	    ch_log_output = TRUE;
1473 #endif
1474 	    if (*T_BE == NUL)
1475 		// When clearing t_BE we assume the user no longer wants
1476 		// bracketed paste, thus disable it by writing t_BD.
1477 		out_str(T_BD);
1478 	    else
1479 		out_str(T_BE);
1480 	}
1481     }
1482 
1483 #ifdef FEAT_LINEBREAK
1484     // 'showbreak'
1485     else if (gvarp == &p_sbr)
1486     {
1487 	for (s = *varp; *s; )
1488 	{
1489 	    if (ptr2cells(s) != 1)
1490 		errmsg = N_("E595: 'showbreak' contains unprintable or wide character");
1491 	    MB_PTR_ADV(s);
1492 	}
1493     }
1494 #endif
1495 
1496 #ifdef FEAT_GUI
1497     // 'guifont'
1498     else if (varp == &p_guifont)
1499     {
1500 	if (gui.in_use)
1501 	{
1502 	    p = p_guifont;
1503 # if defined(FEAT_GUI_GTK)
1504 	    // Put up a font dialog and let the user select a new value.
1505 	    // If this is cancelled go back to the old value but don't
1506 	    // give an error message.
1507 	    if (STRCMP(p, "*") == 0)
1508 	    {
1509 		p = gui_mch_font_dialog(oldval);
1510 
1511 		if (new_value_alloced)
1512 		    free_string_option(p_guifont);
1513 
1514 		p_guifont = (p != NULL) ? p : vim_strsave(oldval);
1515 		new_value_alloced = TRUE;
1516 	    }
1517 # endif
1518 	    if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
1519 	    {
1520 # if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
1521 		if (STRCMP(p_guifont, "*") == 0)
1522 		{
1523 		    // Dialog was cancelled: Keep the old value without giving
1524 		    // an error message.
1525 		    if (new_value_alloced)
1526 			free_string_option(p_guifont);
1527 		    p_guifont = vim_strsave(oldval);
1528 		    new_value_alloced = TRUE;
1529 		}
1530 		else
1531 # endif
1532 		    errmsg = N_("E596: Invalid font(s)");
1533 	    }
1534 	}
1535 	redraw_gui_only = TRUE;
1536     }
1537 # ifdef FEAT_XFONTSET
1538     else if (varp == &p_guifontset)
1539     {
1540 	if (STRCMP(p_guifontset, "*") == 0)
1541 	    errmsg = N_("E597: can't select fontset");
1542 	else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
1543 	    errmsg = N_("E598: Invalid fontset");
1544 	redraw_gui_only = TRUE;
1545     }
1546 # endif
1547     else if (varp == &p_guifontwide)
1548     {
1549 	if (STRCMP(p_guifontwide, "*") == 0)
1550 	    errmsg = N_("E533: can't select wide font");
1551 	else if (gui_get_wide_font() == FAIL)
1552 	    errmsg = N_("E534: Invalid wide font");
1553 	redraw_gui_only = TRUE;
1554     }
1555 #endif
1556 # if defined(FEAT_GUI_GTK)
1557     else if (varp == &p_guiligatures)
1558     {
1559 	gui_set_ligatures();
1560 	redraw_gui_only = TRUE;
1561     }
1562 # endif
1563 
1564 #ifdef CURSOR_SHAPE
1565     // 'guicursor'
1566     else if (varp == &p_guicursor)
1567 	errmsg = parse_shape_opt(SHAPE_CURSOR);
1568 #endif
1569 
1570 #ifdef FEAT_MOUSESHAPE
1571     // 'mouseshape'
1572     else if (varp == &p_mouseshape)
1573     {
1574 	errmsg = parse_shape_opt(SHAPE_MOUSE);
1575 	update_mouseshape(-1);
1576     }
1577 #endif
1578 
1579 #ifdef FEAT_PRINTER
1580     else if (varp == &p_popt)
1581 	errmsg = parse_printoptions();
1582 # if defined(FEAT_POSTSCRIPT)
1583     else if (varp == &p_pmfn)
1584 	errmsg = parse_printmbfont();
1585 # endif
1586 #endif
1587 
1588 #ifdef FEAT_LANGMAP
1589     // 'langmap'
1590     else if (varp == &p_langmap)
1591 	langmap_set();
1592 #endif
1593 
1594 #ifdef FEAT_LINEBREAK
1595     // 'breakat'
1596     else if (varp == &p_breakat)
1597 	fill_breakat_flags();
1598 #endif
1599 
1600     // 'titlestring' and 'iconstring'
1601     else if (varp == &p_titlestring || varp == &p_iconstring)
1602     {
1603 #ifdef FEAT_STL_OPT
1604 	int	flagval = (varp == &p_titlestring) ? STL_IN_TITLE : STL_IN_ICON;
1605 
1606 	// NULL => statusline syntax
1607 	if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
1608 	    stl_syntax |= flagval;
1609 	else
1610 	    stl_syntax &= ~flagval;
1611 #endif
1612 	did_set_title();
1613     }
1614 
1615 #ifdef FEAT_GUI
1616     // 'guioptions'
1617     else if (varp == &p_go)
1618     {
1619 	gui_init_which_components(oldval);
1620 	redraw_gui_only = TRUE;
1621     }
1622 #endif
1623 
1624 #if defined(FEAT_GUI_TABLINE)
1625     // 'guitablabel'
1626     else if (varp == &p_gtl)
1627     {
1628 	redraw_tabline = TRUE;
1629 	redraw_gui_only = TRUE;
1630     }
1631     // 'guitabtooltip'
1632     else if (varp == &p_gtt)
1633     {
1634 	redraw_gui_only = TRUE;
1635     }
1636 #endif
1637 
1638 #if defined(UNIX) || defined(VMS)
1639     // 'ttymouse'
1640     else if (varp == &p_ttym)
1641     {
1642 	// Switch the mouse off before changing the escape sequences used for
1643 	// that.
1644 	mch_setmouse(FALSE);
1645 	if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
1646 	    errmsg = e_invarg;
1647 	else
1648 	    check_mouse_termcode();
1649 	if (termcap_active)
1650 	    setmouse();		// may switch it on again
1651     }
1652 #endif
1653 
1654     // 'selection'
1655     else if (varp == &p_sel)
1656     {
1657 	if (*p_sel == NUL
1658 		|| check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
1659 	    errmsg = e_invarg;
1660     }
1661 
1662     // 'selectmode'
1663     else if (varp == &p_slm)
1664     {
1665 	if (check_opt_strings(p_slm, p_slm_values, TRUE) != OK)
1666 	    errmsg = e_invarg;
1667     }
1668 
1669 #ifdef FEAT_BROWSE
1670     // 'browsedir'
1671     else if (varp == &p_bsdir)
1672     {
1673 	if (check_opt_strings(p_bsdir, p_bsdir_values, FALSE) != OK
1674 		&& !mch_isdir(p_bsdir))
1675 	    errmsg = e_invarg;
1676     }
1677 #endif
1678 
1679     // 'keymodel'
1680     else if (varp == &p_km)
1681     {
1682 	if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
1683 	    errmsg = e_invarg;
1684 	else
1685 	{
1686 	    km_stopsel = (vim_strchr(p_km, 'o') != NULL);
1687 	    km_startsel = (vim_strchr(p_km, 'a') != NULL);
1688 	}
1689     }
1690 
1691     // 'mousemodel'
1692     else if (varp == &p_mousem)
1693     {
1694 	if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
1695 	    errmsg = e_invarg;
1696 #if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
1697 	else if (*p_mousem != *oldval)
1698 	    // Changed from "extend" to "popup" or "popup_setpos" or vv: need
1699 	    // to create or delete the popup menus.
1700 	    gui_motif_update_mousemodel(root_menu);
1701 #endif
1702     }
1703 
1704     // 'switchbuf'
1705     else if (varp == &p_swb)
1706     {
1707 	if (opt_strings_flags(p_swb, p_swb_values, &swb_flags, TRUE) != OK)
1708 	    errmsg = e_invarg;
1709     }
1710 
1711     // 'debug'
1712     else if (varp == &p_debug)
1713     {
1714 	if (check_opt_strings(p_debug, p_debug_values, TRUE) != OK)
1715 	    errmsg = e_invarg;
1716     }
1717 
1718     // 'display'
1719     else if (varp == &p_dy)
1720     {
1721 	if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
1722 	    errmsg = e_invarg;
1723 	else
1724 	    (void)init_chartab();
1725 
1726     }
1727 
1728     // 'eadirection'
1729     else if (varp == &p_ead)
1730     {
1731 	if (check_opt_strings(p_ead, p_ead_values, FALSE) != OK)
1732 	    errmsg = e_invarg;
1733     }
1734 
1735 #ifdef FEAT_CLIPBOARD
1736     // 'clipboard'
1737     else if (varp == &p_cb)
1738 	errmsg = check_clipboard_option();
1739 #endif
1740 
1741 #ifdef FEAT_SPELL
1742     // When 'spelllang' or 'spellfile' is set and there is a window for this
1743     // buffer in which 'spell' is set load the wordlists.
1744     else if (varp == &(curwin->w_s->b_p_spl)
1745 	    || varp == &(curwin->w_s->b_p_spf))
1746     {
1747 	int	is_spellfile = varp == &(curwin->w_s->b_p_spf);
1748 
1749 	if ((is_spellfile && !valid_spellfile(*varp))
1750 	    || (!is_spellfile && !valid_spelllang(*varp)))
1751 	    errmsg = e_invarg;
1752 	else
1753 	    errmsg = did_set_spell_option(is_spellfile);
1754     }
1755     // When 'spellcapcheck' is set compile the regexp program.
1756     else if (varp == &(curwin->w_s->b_p_spc))
1757     {
1758 	errmsg = compile_cap_prog(curwin->w_s);
1759     }
1760     // 'spelloptions'
1761     else if (varp == &(curwin->w_s->b_p_spo))
1762     {
1763 	if (**varp != NUL && STRCMP("camel", *varp) != 0)
1764 	    errmsg = e_invarg;
1765     }
1766     // 'spellsuggest'
1767     else if (varp == &p_sps)
1768     {
1769 	if (spell_check_sps() != OK)
1770 	    errmsg = e_invarg;
1771     }
1772     // 'mkspellmem'
1773     else if (varp == &p_msm)
1774     {
1775 	if (spell_check_msm() != OK)
1776 	    errmsg = e_invarg;
1777     }
1778 #endif
1779 
1780     // When 'bufhidden' is set, check for valid value.
1781     else if (gvarp == &p_bh)
1782     {
1783 	if (check_opt_strings(curbuf->b_p_bh, p_bufhidden_values, FALSE) != OK)
1784 	    errmsg = e_invarg;
1785     }
1786 
1787     // When 'buftype' is set, check for valid value.
1788     else if (gvarp == &p_bt)
1789     {
1790 	if (check_opt_strings(curbuf->b_p_bt, p_buftype_values, FALSE) != OK)
1791 	    errmsg = e_invarg;
1792 	else
1793 	{
1794 	    if (curwin->w_status_height)
1795 	    {
1796 		curwin->w_redr_status = TRUE;
1797 		redraw_later(VALID);
1798 	    }
1799 	    curbuf->b_help = (curbuf->b_p_bt[0] == 'h');
1800 	    redraw_titles();
1801 	}
1802     }
1803 
1804 #ifdef FEAT_STL_OPT
1805     // 'statusline' or 'rulerformat'
1806     else if (gvarp == &p_stl || varp == &p_ruf)
1807     {
1808 	int wid;
1809 
1810 	if (varp == &p_ruf)	// reset ru_wid first
1811 	    ru_wid = 0;
1812 	s = *varp;
1813 	if (varp == &p_ruf && *s == '%')
1814 	{
1815 	    // set ru_wid if 'ruf' starts with "%99("
1816 	    if (*++s == '-')	// ignore a '-'
1817 		s++;
1818 	    wid = getdigits(&s);
1819 	    if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
1820 		ru_wid = wid;
1821 	    else
1822 		errmsg = check_stl_option(p_ruf);
1823 	}
1824 	// check 'statusline' only if it doesn't start with "%!"
1825 	else if (varp == &p_ruf || s[0] != '%' || s[1] != '!')
1826 	    errmsg = check_stl_option(s);
1827 	if (varp == &p_ruf && errmsg == NULL)
1828 	    comp_col();
1829     }
1830 #endif
1831 
1832     // check if it is a valid value for 'complete' -- Acevedo
1833     else if (gvarp == &p_cpt)
1834     {
1835 	for (s = *varp; *s;)
1836 	{
1837 	    while (*s == ',' || *s == ' ')
1838 		s++;
1839 	    if (!*s)
1840 		break;
1841 	    if (vim_strchr((char_u *)".wbuksid]tU", *s) == NULL)
1842 	    {
1843 		errmsg = illegal_char(errbuf, *s);
1844 		break;
1845 	    }
1846 	    if (*++s != NUL && *s != ',' && *s != ' ')
1847 	    {
1848 		if (s[-1] == 'k' || s[-1] == 's')
1849 		{
1850 		    // skip optional filename after 'k' and 's'
1851 		    while (*s && *s != ',' && *s != ' ')
1852 		    {
1853 			if (*s == '\\' && s[1] != NUL)
1854 			    ++s;
1855 			++s;
1856 		    }
1857 		}
1858 		else
1859 		{
1860 		    if (errbuf != NULL)
1861 		    {
1862 			sprintf((char *)errbuf,
1863 				     _("E535: Illegal character after <%c>"),
1864 				     *--s);
1865 			errmsg = errbuf;
1866 		    }
1867 		    else
1868 			errmsg = "";
1869 		    break;
1870 		}
1871 	    }
1872 	}
1873     }
1874 
1875     // 'completeopt'
1876     else if (varp == &p_cot)
1877     {
1878 	if (check_opt_strings(p_cot, p_cot_values, TRUE) != OK)
1879 	    errmsg = e_invarg;
1880 	else
1881 	    completeopt_was_set();
1882     }
1883 
1884 #ifdef BACKSLASH_IN_FILENAME
1885     // 'completeslash'
1886     else if (gvarp == &p_csl)
1887     {
1888 	if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1889 		|| check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
1890 	    errmsg = e_invarg;
1891     }
1892 #endif
1893 
1894 #ifdef FEAT_SIGNS
1895     // 'signcolumn'
1896     else if (varp == &curwin->w_p_scl)
1897     {
1898 	if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
1899 	    errmsg = e_invarg;
1900 	// When changing the 'signcolumn' to or from 'number', recompute the
1901 	// width of the number column if 'number' or 'relativenumber' is set.
1902 	if (((*oldval == 'n' && *(oldval + 1) == 'u')
1903 		|| (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
1904 		&& (curwin->w_p_nu || curwin->w_p_rnu))
1905 	    curwin->w_nrwidth_line_count = 0;
1906     }
1907 #endif
1908 
1909 
1910 #if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
1911     // 'toolbar'
1912     else if (varp == &p_toolbar)
1913     {
1914 	if (opt_strings_flags(p_toolbar, p_toolbar_values,
1915 			      &toolbar_flags, TRUE) != OK)
1916 	    errmsg = e_invarg;
1917 	else
1918 	{
1919 	    out_flush();
1920 	    gui_mch_show_toolbar((toolbar_flags &
1921 				  (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
1922 	}
1923     }
1924 #endif
1925 
1926 #if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
1927     // 'toolbariconsize': GTK+ 2 only
1928     else if (varp == &p_tbis)
1929     {
1930 	if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
1931 	    errmsg = e_invarg;
1932 	else
1933 	{
1934 	    out_flush();
1935 	    gui_mch_show_toolbar((toolbar_flags &
1936 				  (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
1937 	}
1938     }
1939 #endif
1940 
1941     // 'pastetoggle': translate key codes like in a mapping
1942     else if (varp == &p_pt)
1943     {
1944 	if (*p_pt)
1945 	{
1946 	    (void)replace_termcodes(p_pt, &p,
1947 				      REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
1948 	    if (p != NULL)
1949 	    {
1950 		if (new_value_alloced)
1951 		    free_string_option(p_pt);
1952 		p_pt = p;
1953 		new_value_alloced = TRUE;
1954 	    }
1955 	}
1956     }
1957 
1958     // 'backspace'
1959     else if (varp == &p_bs)
1960     {
1961 	if (VIM_ISDIGIT(*p_bs))
1962 	{
1963 	    if (*p_bs > '3' || p_bs[1] != NUL)
1964 		errmsg = e_invarg;
1965 	}
1966 	else if (check_opt_strings(p_bs, p_bs_values, TRUE) != OK)
1967 	    errmsg = e_invarg;
1968     }
1969     else if (varp == &p_bo)
1970     {
1971 	if (opt_strings_flags(p_bo, p_bo_values, &bo_flags, TRUE) != OK)
1972 	    errmsg = e_invarg;
1973     }
1974 
1975     // 'tagcase'
1976     else if (gvarp == &p_tc)
1977     {
1978 	unsigned int	*flags;
1979 
1980 	if (opt_flags & OPT_LOCAL)
1981 	{
1982 	    p = curbuf->b_p_tc;
1983 	    flags = &curbuf->b_tc_flags;
1984 	}
1985 	else
1986 	{
1987 	    p = p_tc;
1988 	    flags = &tc_flags;
1989 	}
1990 
1991 	if ((opt_flags & OPT_LOCAL) && *p == NUL)
1992 	    // make the local value empty: use the global value
1993 	    *flags = 0;
1994 	else if (*p == NUL
1995 		|| opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
1996 	    errmsg = e_invarg;
1997     }
1998 
1999     // 'casemap'
2000     else if (varp == &p_cmp)
2001     {
2002 	if (opt_strings_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE) != OK)
2003 	    errmsg = e_invarg;
2004     }
2005 
2006 #ifdef FEAT_DIFF
2007     // 'diffopt'
2008     else if (varp == &p_dip)
2009     {
2010 	if (diffopt_changed() == FAIL)
2011 	    errmsg = e_invarg;
2012     }
2013 #endif
2014 
2015 #ifdef FEAT_FOLDING
2016     // 'foldmethod'
2017     else if (gvarp == &curwin->w_allbuf_opt.wo_fdm)
2018     {
2019 	if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK
2020 		|| *curwin->w_p_fdm == NUL)
2021 	    errmsg = e_invarg;
2022 	else
2023 	{
2024 	    foldUpdateAll(curwin);
2025 	    if (foldmethodIsDiff(curwin))
2026 		newFoldLevel();
2027 	}
2028     }
2029 # ifdef FEAT_EVAL
2030     // 'foldexpr'
2031     else if (varp == &curwin->w_p_fde)
2032     {
2033 	if (foldmethodIsExpr(curwin))
2034 	    foldUpdateAll(curwin);
2035     }
2036 # endif
2037     // 'foldmarker'
2038     else if (gvarp == &curwin->w_allbuf_opt.wo_fmr)
2039     {
2040 	p = vim_strchr(*varp, ',');
2041 	if (p == NULL)
2042 	    errmsg = N_("E536: comma required");
2043 	else if (p == *varp || p[1] == NUL)
2044 	    errmsg = e_invarg;
2045 	else if (foldmethodIsMarker(curwin))
2046 	    foldUpdateAll(curwin);
2047     }
2048     // 'commentstring'
2049     else if (gvarp == &p_cms)
2050     {
2051 	if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
2052 	    errmsg = N_("E537: 'commentstring' must be empty or contain %s");
2053     }
2054     // 'foldopen'
2055     else if (varp == &p_fdo)
2056     {
2057 	if (opt_strings_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE) != OK)
2058 	    errmsg = e_invarg;
2059     }
2060     // 'foldclose'
2061     else if (varp == &p_fcl)
2062     {
2063 	if (check_opt_strings(p_fcl, p_fcl_values, TRUE) != OK)
2064 	    errmsg = e_invarg;
2065     }
2066     // 'foldignore'
2067     else if (gvarp == &curwin->w_allbuf_opt.wo_fdi)
2068     {
2069 	if (foldmethodIsIndent(curwin))
2070 	    foldUpdateAll(curwin);
2071     }
2072 #endif
2073 
2074     // 'virtualedit'
2075     else if (gvarp == &p_ve)
2076     {
2077 	char_u		*ve = p_ve;
2078 	unsigned int	*flags = &ve_flags;
2079 
2080 	if (opt_flags & OPT_LOCAL)
2081 	{
2082 	    ve = curwin->w_p_ve;
2083 	    flags = &curwin->w_ve_flags;
2084 	}
2085 
2086 	if ((opt_flags & OPT_LOCAL) && *ve == NUL)
2087 	    // make the local value empty: use the global value
2088 	    *flags = 0;
2089 	else
2090 	{
2091 	    if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
2092 		errmsg = e_invarg;
2093 	    else if (STRCMP(p_ve, oldval) != 0)
2094 	    {
2095 		// Recompute cursor position in case the new 've' setting
2096 		// changes something.
2097 		validate_virtcol();
2098 		coladvance(curwin->w_virtcol);
2099 	    }
2100 	}
2101     }
2102 
2103 #if defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)
2104     else if (varp == &p_csqf)
2105     {
2106 	if (p_csqf != NULL)
2107 	{
2108 	    p = p_csqf;
2109 	    while (*p != NUL)
2110 	    {
2111 		if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
2112 			|| p[1] == NUL
2113 			|| vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
2114 			|| (p[2] != NUL && p[2] != ','))
2115 		{
2116 		    errmsg = e_invarg;
2117 		    break;
2118 		}
2119 		else if (p[2] == NUL)
2120 		    break;
2121 		else
2122 		    p += 3;
2123 	    }
2124 	}
2125     }
2126 #endif
2127 
2128 #ifdef FEAT_CINDENT
2129     // 'cinoptions'
2130     else if (gvarp == &p_cino)
2131     {
2132 	// TODO: recognize errors
2133 	parse_cino(curbuf);
2134     }
2135 #endif
2136 
2137 #if defined(FEAT_RENDER_OPTIONS)
2138     // 'renderoptions'
2139     else if (varp == &p_rop)
2140     {
2141 	if (!gui_mch_set_rendering_options(p_rop))
2142 	    errmsg = e_invarg;
2143     }
2144 #endif
2145 
2146     else if (gvarp == &p_ft)
2147     {
2148 	if (!valid_filetype(*varp))
2149 	    errmsg = e_invarg;
2150 	else
2151 	{
2152 	    value_changed = STRCMP(oldval, *varp) != 0;
2153 
2154 	    // Since we check the value, there is no need to set P_INSECURE,
2155 	    // even when the value comes from a modeline.
2156 	    *value_checked = TRUE;
2157 	}
2158     }
2159 
2160 #ifdef FEAT_SYN_HL
2161     else if (gvarp == &p_syn)
2162     {
2163 	if (!valid_filetype(*varp))
2164 	    errmsg = e_invarg;
2165 	else
2166 	{
2167 	    value_changed = STRCMP(oldval, *varp) != 0;
2168 
2169 	    // Since we check the value, there is no need to set P_INSECURE,
2170 	    // even when the value comes from a modeline.
2171 	    *value_checked = TRUE;
2172 	}
2173     }
2174 #endif
2175 
2176 #ifdef FEAT_TERMINAL
2177     // 'termwinkey'
2178     else if (varp == &curwin->w_p_twk)
2179     {
2180 	if (*curwin->w_p_twk != NUL
2181 				  && string_to_key(curwin->w_p_twk, TRUE) == 0)
2182 	    errmsg = e_invarg;
2183     }
2184     // 'termwinsize'
2185     else if (varp == &curwin->w_p_tws)
2186     {
2187 	if (*curwin->w_p_tws != NUL)
2188 	{
2189 	    p = skipdigits(curwin->w_p_tws);
2190 	    if (p == curwin->w_p_tws
2191 		    || (*p != 'x' && *p != '*')
2192 		    || *skipdigits(p + 1) != NUL)
2193 		errmsg = e_invarg;
2194 	}
2195     }
2196     // 'wincolor'
2197     else if (varp == &curwin->w_p_wcr)
2198 	term_update_wincolor(curwin);
2199 # if defined(MSWIN)
2200     // 'termwintype'
2201     else if (varp == &p_twt)
2202     {
2203 	if (check_opt_strings(*varp, p_twt_values, FALSE) != OK)
2204 	    errmsg = e_invarg;
2205     }
2206 # endif
2207 #endif
2208 
2209 #ifdef FEAT_VARTABS
2210     // 'varsofttabstop'
2211     else if (varp == &(curbuf->b_p_vsts))
2212     {
2213 	char_u *cp;
2214 
2215 	if (!(*varp)[0] || ((*varp)[0] == '0' && !(*varp)[1]))
2216 	{
2217 	    if (curbuf->b_p_vsts_array)
2218 	    {
2219 		vim_free(curbuf->b_p_vsts_array);
2220 		curbuf->b_p_vsts_array = 0;
2221 	    }
2222 	}
2223 	else
2224 	{
2225 	    for (cp = *varp; *cp; ++cp)
2226 	    {
2227 		if (vim_isdigit(*cp))
2228 		    continue;
2229 		if (*cp == ',' && cp > *varp && *(cp-1) != ',')
2230 		    continue;
2231 		errmsg = e_invarg;
2232 		break;
2233 	    }
2234 	    if (errmsg == NULL)
2235 	    {
2236 		int *oldarray = curbuf->b_p_vsts_array;
2237 		if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
2238 		{
2239 		    if (oldarray)
2240 			vim_free(oldarray);
2241 		}
2242 		else
2243 		    errmsg = e_invarg;
2244 	    }
2245 	}
2246     }
2247 
2248     // 'vartabstop'
2249     else if (varp == &(curbuf->b_p_vts))
2250     {
2251 	char_u *cp;
2252 
2253 	if (!(*varp)[0] || ((*varp)[0] == '0' && !(*varp)[1]))
2254 	{
2255 	    if (curbuf->b_p_vts_array)
2256 	    {
2257 		vim_free(curbuf->b_p_vts_array);
2258 		curbuf->b_p_vts_array = NULL;
2259 	    }
2260 	}
2261 	else
2262 	{
2263 	    for (cp = *varp; *cp; ++cp)
2264 	    {
2265 		if (vim_isdigit(*cp))
2266 		    continue;
2267 		if (*cp == ',' && cp > *varp && *(cp-1) != ',')
2268 		    continue;
2269 		errmsg = e_invarg;
2270 		break;
2271 	    }
2272 	    if (errmsg == NULL)
2273 	    {
2274 		int *oldarray = curbuf->b_p_vts_array;
2275 
2276 		if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
2277 		{
2278 		    vim_free(oldarray);
2279 #ifdef FEAT_FOLDING
2280 		    if (foldmethodIsIndent(curwin))
2281 			foldUpdateAll(curwin);
2282 #endif
2283 		}
2284 		else
2285 		    errmsg = e_invarg;
2286 	    }
2287 	}
2288     }
2289 #endif
2290 
2291 #ifdef FEAT_PROP_POPUP
2292     // 'previewpopup'
2293     else if (varp == &p_pvp)
2294     {
2295 	if (parse_previewpopup(NULL) == FAIL)
2296 	    errmsg = e_invarg;
2297     }
2298 # ifdef FEAT_QUICKFIX
2299     // 'completepopup'
2300     else if (varp == &p_cpp)
2301     {
2302 	if (parse_completepopup(NULL) == FAIL)
2303 	    errmsg = e_invarg;
2304 	else
2305 	    popup_close_info();
2306     }
2307 # endif
2308 #endif
2309 
2310 #ifdef FEAT_COMPL_FUNC
2311     // 'completefunc'
2312     else if (gvarp == &p_cfu)
2313     {
2314 	if (set_completefunc_option() == FAIL)
2315 	    errmsg = e_invarg;
2316     }
2317 
2318     // 'omnifunc'
2319     else if (gvarp == &p_ofu)
2320     {
2321 	if (set_omnifunc_option() == FAIL)
2322 	    errmsg = e_invarg;
2323     }
2324 
2325     // 'thesaurusfunc'
2326     else if (gvarp == &p_tsrfu)
2327     {
2328 	if (set_thesaurusfunc_option() == FAIL)
2329 	    errmsg = e_invarg;
2330     }
2331 #endif
2332 
2333 #if defined(FEAT_EVAL) && \
2334      (defined(FEAT_XIM) || defined(IME_WITHOUT_XIM) || defined(VIMDLL))
2335     // 'imactivatefunc'
2336     else if (gvarp == &p_imaf)
2337     {
2338 	if (set_imactivatefunc_option() == FAIL)
2339 	    errmsg = e_invarg;
2340     }
2341 
2342     // 'imstatusfunc'
2343     else if (gvarp == &p_imsf)
2344     {
2345 	if (set_imstatusfunc_option() == FAIL)
2346 	    errmsg = e_invarg;
2347     }
2348 #endif
2349 
2350     // 'operatorfunc'
2351     else if (varp == &p_opfunc)
2352     {
2353 	if (set_operatorfunc_option() == FAIL)
2354 	    errmsg = e_invarg;
2355     }
2356 
2357 #ifdef FEAT_QUICKFIX
2358     // 'quickfixtextfunc'
2359     else if (varp == &p_qftf)
2360     {
2361 	if (qf_process_qftf_option() == FAIL)
2362 	    errmsg = e_invarg;
2363     }
2364 #endif
2365 
2366 #ifdef FEAT_EVAL
2367     // 'tagfunc'
2368     else if (gvarp == &p_tfu)
2369     {
2370 	if (set_tagfunc_option() == FAIL)
2371 	    errmsg = e_invarg;
2372     }
2373 #endif
2374 
2375     // Options that are a list of flags.
2376     else
2377     {
2378 	p = NULL;
2379 	if (varp == &p_ww) // 'whichwrap'
2380 	    p = (char_u *)WW_ALL;
2381 	if (varp == &p_shm) // 'shortmess'
2382 	    p = (char_u *)SHM_ALL;
2383 	else if (varp == &(p_cpo)) // 'cpoptions'
2384 	    p = (char_u *)CPO_ALL;
2385 	else if (varp == &(curbuf->b_p_fo)) // 'formatoptions'
2386 	    p = (char_u *)FO_ALL;
2387 #ifdef FEAT_CONCEAL
2388 	else if (varp == &curwin->w_p_cocu) // 'concealcursor'
2389 	    p = (char_u *)COCU_ALL;
2390 #endif
2391 	else if (varp == &p_mouse) // 'mouse'
2392 	{
2393 	    p = (char_u *)MOUSE_ALL;
2394 	}
2395 #if defined(FEAT_GUI)
2396 	else if (varp == &p_go) // 'guioptions'
2397 	    p = (char_u *)GO_ALL;
2398 #endif
2399 	if (p != NULL)
2400 	{
2401 	    for (s = *varp; *s; ++s)
2402 		if (vim_strchr(p, *s) == NULL)
2403 		{
2404 		    errmsg = illegal_char(errbuf, *s);
2405 		    break;
2406 		}
2407 	}
2408     }
2409 
2410     // If error detected, restore the previous value.
2411     if (errmsg != NULL)
2412     {
2413 	if (new_value_alloced)
2414 	    free_string_option(*varp);
2415 	*varp = oldval;
2416 	// When resetting some values, need to act on it.
2417 	if (did_chartab)
2418 	    (void)init_chartab();
2419 	if (varp == &p_hl)
2420 	    (void)highlight_changed();
2421     }
2422     else
2423     {
2424 #ifdef FEAT_EVAL
2425 	// Remember where the option was set.
2426 	set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
2427 #endif
2428 	// Free string options that are in allocated memory.
2429 	// Use "free_oldval", because recursiveness may change the flags under
2430 	// our fingers (esp. init_highlight()).
2431 	if (free_oldval)
2432 	    free_string_option(oldval);
2433 	if (new_value_alloced)
2434 	    set_option_flag(opt_idx, P_ALLOCED);
2435 	else
2436 	    clear_option_flag(opt_idx, P_ALLOCED);
2437 
2438 	if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
2439 		&& is_global_local_option(opt_idx))
2440 	{
2441 	    // global option with local value set to use global value; free
2442 	    // the local value and make it empty
2443 	    p = get_option_varp_scope(opt_idx, OPT_LOCAL);
2444 	    free_string_option(*(char_u **)p);
2445 	    *(char_u **)p = empty_option;
2446 	}
2447 
2448 	// May set global value for local option.
2449 	else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
2450 	    set_string_option_global(opt_idx, varp);
2451 
2452 	// Trigger the autocommand only after setting the flags.
2453 #ifdef FEAT_SYN_HL
2454 	// When 'syntax' is set, load the syntax of that name
2455 	if (varp == &(curbuf->b_p_syn))
2456 	{
2457 	    static int syn_recursive = 0;
2458 
2459 	    ++syn_recursive;
2460 	    // Only pass TRUE for "force" when the value changed or not used
2461 	    // recursively, to avoid endless recurrence.
2462 	    apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
2463 		    value_changed || syn_recursive == 1, curbuf);
2464 	    curbuf->b_flags |= BF_SYN_SET;
2465 	    --syn_recursive;
2466 	}
2467 #endif
2468 	else if (varp == &(curbuf->b_p_ft))
2469 	{
2470 	    // 'filetype' is set, trigger the FileType autocommand.
2471 	    // Skip this when called from a modeline and the filetype was
2472 	    // already set to this value.
2473 	    if (!(opt_flags & OPT_MODELINE) || value_changed)
2474 	    {
2475 		static int  ft_recursive = 0;
2476 		int	    secure_save = secure;
2477 
2478 		// Reset the secure flag, since the value of 'filetype' has
2479 		// been checked to be safe.
2480 		secure = 0;
2481 
2482 		++ft_recursive;
2483 		did_filetype = TRUE;
2484 		// Only pass TRUE for "force" when the value changed or not
2485 		// used recursively, to avoid endless recurrence.
2486 		apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2487 				   value_changed || ft_recursive == 1, curbuf);
2488 		--ft_recursive;
2489 		// Just in case the old "curbuf" is now invalid.
2490 		if (varp != &(curbuf->b_p_ft))
2491 		    varp = NULL;
2492 
2493 		secure = secure_save;
2494 	    }
2495 	}
2496 #ifdef FEAT_SPELL
2497 	if (varp == &(curwin->w_s->b_p_spl))
2498 	{
2499 	    char_u	fname[200];
2500 	    char_u	*q = curwin->w_s->b_p_spl;
2501 
2502 	    // Skip the first name if it is "cjk".
2503 	    if (STRNCMP(q, "cjk,", 4) == 0)
2504 		q += 4;
2505 
2506 	    // Source the spell/LANG.vim in 'runtimepath'.
2507 	    // They could set 'spellcapcheck' depending on the language.
2508 	    // Use the first name in 'spelllang' up to '_region' or
2509 	    // '.encoding'.
2510 	    for (p = q; *p != NUL; ++p)
2511 		if (!ASCII_ISALNUM(*p) && *p != '-')
2512 		    break;
2513 	    if (p > q)
2514 	    {
2515 		vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
2516 							      (int)(p - q), q);
2517 		source_runtime(fname, DIP_ALL);
2518 	    }
2519 	}
2520 #endif
2521     }
2522 
2523     if (varp == &p_mouse)
2524     {
2525 	if (*p_mouse == NUL)
2526 	    mch_setmouse(FALSE);    // switch mouse off
2527 	else
2528 	    setmouse();		    // in case 'mouse' changed
2529     }
2530 
2531 #if defined(FEAT_LUA) || defined(PROTO)
2532     if (varp == &p_rtp)
2533 	update_package_paths_in_lua();
2534 #endif
2535 
2536     if (curwin->w_curswant != MAXCOL
2537 		   && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0)
2538 	curwin->w_set_curswant = TRUE;
2539 
2540     if ((opt_flags & OPT_NO_REDRAW) == 0)
2541     {
2542 #ifdef FEAT_GUI
2543 	// check redraw when it's not a GUI option or the GUI is active.
2544 	if (!redraw_gui_only || gui.in_use)
2545 #endif
2546 	    check_redraw(get_option_flags(opt_idx));
2547     }
2548 
2549 #if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2550     if (did_swaptcap)
2551     {
2552 	set_termname((char_u *)"win32");
2553 	init_highlight(TRUE, FALSE);
2554     }
2555 #endif
2556 
2557     return errmsg;
2558 }
2559 
2560 /*
2561  * Check an option that can be a range of string values.
2562  *
2563  * Return OK for correct value, FAIL otherwise.
2564  * Empty is always OK.
2565  */
2566     static int
check_opt_strings(char_u * val,char ** values,int list)2567 check_opt_strings(
2568     char_u	*val,
2569     char	**values,
2570     int		list)	    // when TRUE: accept a list of values
2571 {
2572     return opt_strings_flags(val, values, NULL, list);
2573 }
2574 
2575 /*
2576  * Handle an option that can be a range of string values.
2577  * Set a flag in "*flagp" for each string present.
2578  *
2579  * Return OK for correct value, FAIL otherwise.
2580  * Empty is always OK.
2581  */
2582     static int
opt_strings_flags(char_u * val,char ** values,unsigned * flagp,int list)2583 opt_strings_flags(
2584     char_u	*val,		// new value
2585     char	**values,	// array of valid string values
2586     unsigned	*flagp,
2587     int		list)		// when TRUE: accept a list of values
2588 {
2589     int		i;
2590     int		len;
2591     unsigned	new_flags = 0;
2592 
2593     while (*val)
2594     {
2595 	for (i = 0; ; ++i)
2596 	{
2597 	    if (values[i] == NULL)	// val not found in values[]
2598 		return FAIL;
2599 
2600 	    len = (int)STRLEN(values[i]);
2601 	    if (STRNCMP(values[i], val, len) == 0
2602 		    && ((list && val[len] == ',') || val[len] == NUL))
2603 	    {
2604 		val += len + (val[len] == ',');
2605 		new_flags |= (1 << i);
2606 		break;		// check next item in val list
2607 	    }
2608 	}
2609     }
2610     if (flagp != NULL)
2611 	*flagp = new_flags;
2612 
2613     return OK;
2614 }
2615 
2616 /*
2617  * return OK if "p" is a valid fileformat name, FAIL otherwise.
2618  */
2619     int
check_ff_value(char_u * p)2620 check_ff_value(char_u *p)
2621 {
2622     return check_opt_strings(p, p_ff_values, FALSE);
2623 }
2624