xref: /vim-8.2.3635/src/optionstr.c (revision 87fd0924)
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 #ifdef FEAT_TITLE
983 		// May show a "+" in the title now.
984 		redraw_titles();
985 #endif
986 		// Add 'fileencoding' to the swap file.
987 		ml_setflags(curbuf);
988 	    }
989 	}
990 	if (errmsg == NULL)
991 	{
992 	    // canonize the value, so that STRCMP() can be used on it
993 	    p = enc_canonize(*varp);
994 	    if (p != NULL)
995 	    {
996 		vim_free(*varp);
997 		*varp = p;
998 	    }
999 	    if (varp == &p_enc)
1000 	    {
1001 		errmsg = mb_init();
1002 #ifdef FEAT_TITLE
1003 		redraw_titles();
1004 #endif
1005 	    }
1006 	}
1007 
1008 #if defined(FEAT_GUI_GTK)
1009 	if (errmsg == NULL && varp == &p_tenc && gui.in_use)
1010 	{
1011 	    // GTK+ 2 uses only a single encoding, and that is UTF-8.
1012 	    if (STRCMP(p_tenc, "utf-8") != 0)
1013 		errmsg = N_("E617: Cannot be changed in the GTK+ 2 GUI");
1014 	}
1015 #endif
1016 
1017 	if (errmsg == NULL)
1018 	{
1019 #ifdef FEAT_KEYMAP
1020 	    // When 'keymap' is used and 'encoding' changes, reload the keymap
1021 	    // (with another encoding).
1022 	    if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
1023 		(void)keymap_init();
1024 #endif
1025 
1026 	    // When 'termencoding' is not empty and 'encoding' changes or when
1027 	    // 'termencoding' changes, need to setup for keyboard input and
1028 	    // display output conversion.
1029 	    if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
1030 	    {
1031 		if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
1032 			|| convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
1033 		{
1034 		    semsg(_("E950: Cannot convert between %s and %s"),
1035 			    p_tenc, p_enc);
1036 		    errmsg = e_invarg;
1037 		}
1038 	    }
1039 
1040 #if defined(MSWIN)
1041 	    // $HOME may have characters in active code page.
1042 	    if (varp == &p_enc)
1043 		init_homedir();
1044 #endif
1045 	}
1046     }
1047 
1048 #if defined(FEAT_POSTSCRIPT)
1049     else if (varp == &p_penc)
1050     {
1051 	// Canonize printencoding if VIM standard one
1052 	p = enc_canonize(p_penc);
1053 	if (p != NULL)
1054 	{
1055 	    vim_free(p_penc);
1056 	    p_penc = p;
1057 	}
1058 	else
1059 	{
1060 	    // Ensure lower case and '-' for '_'
1061 	    for (s = p_penc; *s != NUL; s++)
1062 	    {
1063 		if (*s == '_')
1064 		    *s = '-';
1065 		else
1066 		    *s = TOLOWER_ASC(*s);
1067 	    }
1068 	}
1069     }
1070 #endif
1071 
1072 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
1073     else if (varp == &p_imak)
1074     {
1075 	if (!im_xim_isvalid_imactivate())
1076 	    errmsg = e_invarg;
1077     }
1078 #endif
1079 
1080 #ifdef FEAT_KEYMAP
1081     else if (varp == &curbuf->b_p_keymap)
1082     {
1083 	if (!valid_filetype(*varp))
1084 	    errmsg = e_invarg;
1085 	else
1086 	{
1087 	    int	    secure_save = secure;
1088 
1089 	    // Reset the secure flag, since the value of 'keymap' has
1090 	    // been checked to be safe.
1091 	    secure = 0;
1092 
1093 	    // load or unload key mapping tables
1094 	    errmsg = keymap_init();
1095 
1096 	    secure = secure_save;
1097 
1098 	    // Since we check the value, there is no need to set P_INSECURE,
1099 	    // even when the value comes from a modeline.
1100 	    *value_checked = TRUE;
1101 	}
1102 
1103 	if (errmsg == NULL)
1104 	{
1105 	    if (*curbuf->b_p_keymap != NUL)
1106 	    {
1107 		// Installed a new keymap, switch on using it.
1108 		curbuf->b_p_iminsert = B_IMODE_LMAP;
1109 		if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
1110 		    curbuf->b_p_imsearch = B_IMODE_LMAP;
1111 	    }
1112 	    else
1113 	    {
1114 		// Cleared the keymap, may reset 'iminsert' and 'imsearch'.
1115 		if (curbuf->b_p_iminsert == B_IMODE_LMAP)
1116 		    curbuf->b_p_iminsert = B_IMODE_NONE;
1117 		if (curbuf->b_p_imsearch == B_IMODE_LMAP)
1118 		    curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
1119 	    }
1120 	    if ((opt_flags & OPT_LOCAL) == 0)
1121 	    {
1122 		set_iminsert_global();
1123 		set_imsearch_global();
1124 	    }
1125 	    status_redraw_curbuf();
1126 	}
1127     }
1128 #endif
1129 
1130     // 'fileformat'
1131     else if (gvarp == &p_ff)
1132     {
1133 	if (!curbuf->b_p_ma && !(opt_flags & OPT_GLOBAL))
1134 	    errmsg = e_cannot_make_changes_modifiable_is_off;
1135 	else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
1136 	    errmsg = e_invarg;
1137 	else
1138 	{
1139 	    // may also change 'textmode'
1140 	    if (get_fileformat(curbuf) == EOL_DOS)
1141 		curbuf->b_p_tx = TRUE;
1142 	    else
1143 		curbuf->b_p_tx = FALSE;
1144 #ifdef FEAT_TITLE
1145 	    redraw_titles();
1146 #endif
1147 	    // update flag in swap file
1148 	    ml_setflags(curbuf);
1149 	    // Redraw needed when switching to/from "mac": a CR in the text
1150 	    // will be displayed differently.
1151 	    if (get_fileformat(curbuf) == EOL_MAC || *oldval == 'm')
1152 		redraw_curbuf_later(NOT_VALID);
1153 	}
1154     }
1155 
1156     // 'fileformats'
1157     else if (varp == &p_ffs)
1158     {
1159 	if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
1160 	    errmsg = e_invarg;
1161 	else
1162 	{
1163 	    // also change 'textauto'
1164 	    if (*p_ffs == NUL)
1165 		p_ta = FALSE;
1166 	    else
1167 		p_ta = TRUE;
1168 	}
1169     }
1170 
1171 #if defined(FEAT_CRYPT)
1172     // 'cryptkey'
1173     else if (gvarp == &p_key)
1174     {
1175 	// Make sure the ":set" command doesn't show the new value in the
1176 	// history.
1177 	remove_key_from_history();
1178 
1179 	if (STRCMP(curbuf->b_p_key, oldval) != 0)
1180 	    // Need to update the swapfile.
1181 	{
1182 	    ml_set_crypt_key(curbuf, oldval,
1183 			      *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
1184 	    changed_internal();
1185 	}
1186     }
1187 
1188     else if (gvarp == &p_cm)
1189     {
1190 	if (opt_flags & OPT_LOCAL)
1191 	    p = curbuf->b_p_cm;
1192 	else
1193 	    p = p_cm;
1194 	if (check_opt_strings(p, p_cm_values, TRUE) != OK)
1195 	    errmsg = e_invarg;
1196 	else if (crypt_self_test() == FAIL)
1197 	    errmsg = e_invarg;
1198 	else
1199 	{
1200 	    // When setting the global value to empty, make it "zip".
1201 	    if (*p_cm == NUL)
1202 	    {
1203 		if (new_value_alloced)
1204 		    free_string_option(p_cm);
1205 		p_cm = vim_strsave((char_u *)"zip");
1206 		new_value_alloced = TRUE;
1207 	    }
1208 	    // When using ":set cm=name" the local value is going to be empty.
1209 	    // Do that here, otherwise the crypt functions will still use the
1210 	    // local value.
1211 	    if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1212 	    {
1213 		free_string_option(curbuf->b_p_cm);
1214 		curbuf->b_p_cm = empty_option;
1215 	    }
1216 
1217 	    // Need to update the swapfile when the effective method changed.
1218 	    // Set "s" to the effective old value, "p" to the effective new
1219 	    // method and compare.
1220 	    if ((opt_flags & OPT_LOCAL) && *oldval == NUL)
1221 		s = p_cm;  // was previously using the global value
1222 	    else
1223 		s = oldval;
1224 	    if (*curbuf->b_p_cm == NUL)
1225 		p = p_cm;  // is now using the global value
1226 	    else
1227 		p = curbuf->b_p_cm;
1228 	    if (STRCMP(s, p) != 0)
1229 		ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1230 
1231 	    // If the global value changes need to update the swapfile for all
1232 	    // buffers using that value.
1233 	    if ((opt_flags & OPT_GLOBAL) && STRCMP(p_cm, oldval) != 0)
1234 	    {
1235 		buf_T	*buf;
1236 
1237 		FOR_ALL_BUFFERS(buf)
1238 		    if (buf != curbuf && *buf->b_p_cm == NUL)
1239 			ml_set_crypt_key(buf, buf->b_p_key, oldval);
1240 	    }
1241 	}
1242     }
1243 #endif
1244 
1245     // 'matchpairs'
1246     else if (gvarp == &p_mps)
1247     {
1248 	if (has_mbyte)
1249 	{
1250 	    for (p = *varp; *p != NUL; ++p)
1251 	    {
1252 		int x2 = -1;
1253 		int x3 = -1;
1254 
1255 		if (*p != NUL)
1256 		    p += mb_ptr2len(p);
1257 		if (*p != NUL)
1258 		    x2 = *p++;
1259 		if (*p != NUL)
1260 		{
1261 		    x3 = mb_ptr2char(p);
1262 		    p += mb_ptr2len(p);
1263 		}
1264 		if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
1265 		{
1266 		    errmsg = e_invarg;
1267 		    break;
1268 		}
1269 		if (*p == NUL)
1270 		    break;
1271 	    }
1272 	}
1273 	else
1274 	{
1275 	    // Check for "x:y,x:y"
1276 	    for (p = *varp; *p != NUL; p += 4)
1277 	    {
1278 		if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
1279 		{
1280 		    errmsg = e_invarg;
1281 		    break;
1282 		}
1283 		if (p[3] == NUL)
1284 		    break;
1285 	    }
1286 	}
1287     }
1288 
1289     // 'comments'
1290     else if (gvarp == &p_com)
1291     {
1292 	for (s = *varp; *s; )
1293 	{
1294 	    while (*s && *s != ':')
1295 	    {
1296 		if (vim_strchr((char_u *)COM_ALL, *s) == NULL
1297 					     && !VIM_ISDIGIT(*s) && *s != '-')
1298 		{
1299 		    errmsg = illegal_char(errbuf, *s);
1300 		    break;
1301 		}
1302 		++s;
1303 	    }
1304 	    if (*s++ == NUL)
1305 		errmsg = N_("E524: Missing colon");
1306 	    else if (*s == ',' || *s == NUL)
1307 		errmsg = N_("E525: Zero length string");
1308 	    if (errmsg != NULL)
1309 		break;
1310 	    while (*s && *s != ',')
1311 	    {
1312 		if (*s == '\\' && s[1] != NUL)
1313 		    ++s;
1314 		++s;
1315 	    }
1316 	    s = skip_to_option_part(s);
1317 	}
1318     }
1319 
1320     // global 'listchars'
1321     else if (varp == &p_lcs)
1322     {
1323 	errmsg = set_chars_option(curwin, varp);
1324 	if (errmsg == NULL)
1325 	{
1326 	    tabpage_T	*tp;
1327 	    win_T		*wp;
1328 
1329 	    // The current window is set to use the global 'listchars' value.
1330 	    // So clear the window-local value.
1331 	    if (!(opt_flags & OPT_GLOBAL))
1332 		clear_string_option(&curwin->w_p_lcs);
1333 	    FOR_ALL_TAB_WINDOWS(tp, wp)
1334 		// If no error was returned above, we don't expect an error
1335 		// here, so ignore the return value.
1336 		(void)set_chars_option(wp, &wp->w_p_lcs);
1337 
1338 	    redraw_all_later(NOT_VALID);
1339 	}
1340     }
1341 
1342     // local 'listchars'
1343     else if (varp == &curwin->w_p_lcs)
1344 	errmsg = set_chars_option(curwin, varp);
1345 
1346     // 'fillchars'
1347     else if (varp == &p_fcs)
1348     {
1349 	errmsg = set_chars_option(curwin, varp);
1350     }
1351 
1352 #ifdef FEAT_CMDWIN
1353     // 'cedit'
1354     else if (varp == &p_cedit)
1355     {
1356 	errmsg = check_cedit();
1357     }
1358 #endif
1359 
1360     // 'verbosefile'
1361     else if (varp == &p_vfile)
1362     {
1363 	verbose_stop();
1364 	if (*p_vfile != NUL && verbose_open() == FAIL)
1365 	    errmsg = e_invarg;
1366     }
1367 
1368 #ifdef FEAT_VIMINFO
1369     // 'viminfo'
1370     else if (varp == &p_viminfo)
1371     {
1372 	for (s = p_viminfo; *s;)
1373 	{
1374 	    // Check it's a valid character
1375 	    if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
1376 	    {
1377 		errmsg = illegal_char(errbuf, *s);
1378 		break;
1379 	    }
1380 	    if (*s == 'n')	// name is always last one
1381 		break;
1382 	    else if (*s == 'r') // skip until next ','
1383 	    {
1384 		while (*++s && *s != ',')
1385 		    ;
1386 	    }
1387 	    else if (*s == '%')
1388 	    {
1389 		// optional number
1390 		while (vim_isdigit(*++s))
1391 		    ;
1392 	    }
1393 	    else if (*s == '!' || *s == 'h' || *s == 'c')
1394 		++s;		// no extra chars
1395 	    else		// must have a number
1396 	    {
1397 		while (vim_isdigit(*++s))
1398 		    ;
1399 
1400 		if (!VIM_ISDIGIT(*(s - 1)))
1401 		{
1402 		    if (errbuf != NULL)
1403 		    {
1404 			sprintf(errbuf, _("E526: Missing number after <%s>"),
1405 						    transchar_byte(*(s - 1)));
1406 			errmsg = errbuf;
1407 		    }
1408 		    else
1409 			errmsg = "";
1410 		    break;
1411 		}
1412 	    }
1413 	    if (*s == ',')
1414 		++s;
1415 	    else if (*s)
1416 	    {
1417 		if (errbuf != NULL)
1418 		    errmsg = N_("E527: Missing comma");
1419 		else
1420 		    errmsg = "";
1421 		break;
1422 	    }
1423 	}
1424 	if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
1425 	    errmsg = N_("E528: Must specify a ' value");
1426     }
1427 #endif // FEAT_VIMINFO
1428 
1429     // terminal options
1430     else if (istermoption_idx(opt_idx) && full_screen)
1431     {
1432 	// ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
1433 	if (varp == &T_CCO)
1434 	{
1435 	    int colors = atoi((char *)T_CCO);
1436 
1437 	    // Only reinitialize colors if t_Co value has really changed to
1438 	    // avoid expensive reload of colorscheme if t_Co is set to the
1439 	    // same value multiple times.
1440 	    if (colors != t_colors)
1441 	    {
1442 		t_colors = colors;
1443 		if (t_colors <= 1)
1444 		{
1445 		    if (new_value_alloced)
1446 			vim_free(T_CCO);
1447 		    T_CCO = empty_option;
1448 		}
1449 #if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1450 		if (is_term_win32())
1451 		{
1452 		    swap_tcap();
1453 		    did_swaptcap = TRUE;
1454 		}
1455 #endif
1456 		// We now have a different color setup, initialize it again.
1457 		init_highlight(TRUE, FALSE);
1458 	    }
1459 	}
1460 	ttest(FALSE);
1461 	if (varp == &T_ME)
1462 	{
1463 	    out_str(T_ME);
1464 	    redraw_later(CLEAR);
1465 #if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
1466 	    // Since t_me has been set, this probably means that the user
1467 	    // wants to use this as default colors.  Need to reset default
1468 	    // background/foreground colors.
1469 # ifdef VIMDLL
1470 	    if (!gui.in_use && !gui.starting)
1471 # endif
1472 		mch_set_normal_colors();
1473 #endif
1474 	}
1475 	if (varp == &T_BE && termcap_active)
1476 	{
1477 #ifdef FEAT_JOB_CHANNEL
1478 	    ch_log_output = TRUE;
1479 #endif
1480 	    if (*T_BE == NUL)
1481 		// When clearing t_BE we assume the user no longer wants
1482 		// bracketed paste, thus disable it by writing t_BD.
1483 		out_str(T_BD);
1484 	    else
1485 		out_str(T_BE);
1486 	}
1487     }
1488 
1489 #ifdef FEAT_LINEBREAK
1490     // 'showbreak'
1491     else if (gvarp == &p_sbr)
1492     {
1493 	for (s = *varp; *s; )
1494 	{
1495 	    if (ptr2cells(s) != 1)
1496 		errmsg = N_("E595: 'showbreak' contains unprintable or wide character");
1497 	    MB_PTR_ADV(s);
1498 	}
1499     }
1500 #endif
1501 
1502 #ifdef FEAT_GUI
1503     // 'guifont'
1504     else if (varp == &p_guifont)
1505     {
1506 	if (gui.in_use)
1507 	{
1508 	    p = p_guifont;
1509 # if defined(FEAT_GUI_GTK)
1510 	    // Put up a font dialog and let the user select a new value.
1511 	    // If this is cancelled go back to the old value but don't
1512 	    // give an error message.
1513 	    if (STRCMP(p, "*") == 0)
1514 	    {
1515 		p = gui_mch_font_dialog(oldval);
1516 
1517 		if (new_value_alloced)
1518 		    free_string_option(p_guifont);
1519 
1520 		p_guifont = (p != NULL) ? p : vim_strsave(oldval);
1521 		new_value_alloced = TRUE;
1522 	    }
1523 # endif
1524 	    if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
1525 	    {
1526 # if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
1527 		if (STRCMP(p_guifont, "*") == 0)
1528 		{
1529 		    // Dialog was cancelled: Keep the old value without giving
1530 		    // an error message.
1531 		    if (new_value_alloced)
1532 			free_string_option(p_guifont);
1533 		    p_guifont = vim_strsave(oldval);
1534 		    new_value_alloced = TRUE;
1535 		}
1536 		else
1537 # endif
1538 		    errmsg = N_("E596: Invalid font(s)");
1539 	    }
1540 	}
1541 	redraw_gui_only = TRUE;
1542     }
1543 # ifdef FEAT_XFONTSET
1544     else if (varp == &p_guifontset)
1545     {
1546 	if (STRCMP(p_guifontset, "*") == 0)
1547 	    errmsg = N_("E597: can't select fontset");
1548 	else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
1549 	    errmsg = N_("E598: Invalid fontset");
1550 	redraw_gui_only = TRUE;
1551     }
1552 # endif
1553     else if (varp == &p_guifontwide)
1554     {
1555 	if (STRCMP(p_guifontwide, "*") == 0)
1556 	    errmsg = N_("E533: can't select wide font");
1557 	else if (gui_get_wide_font() == FAIL)
1558 	    errmsg = N_("E534: Invalid wide font");
1559 	redraw_gui_only = TRUE;
1560     }
1561 #endif
1562 # if defined(FEAT_GUI_GTK)
1563     else if (varp == &p_guiligatures)
1564     {
1565 	gui_set_ligatures();
1566 	redraw_gui_only = TRUE;
1567     }
1568 # endif
1569 
1570 #ifdef CURSOR_SHAPE
1571     // 'guicursor'
1572     else if (varp == &p_guicursor)
1573 	errmsg = parse_shape_opt(SHAPE_CURSOR);
1574 #endif
1575 
1576 #ifdef FEAT_MOUSESHAPE
1577     // 'mouseshape'
1578     else if (varp == &p_mouseshape)
1579     {
1580 	errmsg = parse_shape_opt(SHAPE_MOUSE);
1581 	update_mouseshape(-1);
1582     }
1583 #endif
1584 
1585 #ifdef FEAT_PRINTER
1586     else if (varp == &p_popt)
1587 	errmsg = parse_printoptions();
1588 # if defined(FEAT_POSTSCRIPT)
1589     else if (varp == &p_pmfn)
1590 	errmsg = parse_printmbfont();
1591 # endif
1592 #endif
1593 
1594 #ifdef FEAT_LANGMAP
1595     // 'langmap'
1596     else if (varp == &p_langmap)
1597 	langmap_set();
1598 #endif
1599 
1600 #ifdef FEAT_LINEBREAK
1601     // 'breakat'
1602     else if (varp == &p_breakat)
1603 	fill_breakat_flags();
1604 #endif
1605 
1606 #ifdef FEAT_TITLE
1607     // 'titlestring' and 'iconstring'
1608     else if (varp == &p_titlestring || varp == &p_iconstring)
1609     {
1610 # ifdef FEAT_STL_OPT
1611 	int	flagval = (varp == &p_titlestring) ? STL_IN_TITLE : STL_IN_ICON;
1612 
1613 	// NULL => statusline syntax
1614 	if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
1615 	    stl_syntax |= flagval;
1616 	else
1617 	    stl_syntax &= ~flagval;
1618 # endif
1619 	did_set_title();
1620     }
1621 #endif
1622 
1623 #ifdef FEAT_GUI
1624     // 'guioptions'
1625     else if (varp == &p_go)
1626     {
1627 	gui_init_which_components(oldval);
1628 	redraw_gui_only = TRUE;
1629     }
1630 #endif
1631 
1632 #if defined(FEAT_GUI_TABLINE)
1633     // 'guitablabel'
1634     else if (varp == &p_gtl)
1635     {
1636 	redraw_tabline = TRUE;
1637 	redraw_gui_only = TRUE;
1638     }
1639     // 'guitabtooltip'
1640     else if (varp == &p_gtt)
1641     {
1642 	redraw_gui_only = TRUE;
1643     }
1644 #endif
1645 
1646 #if defined(UNIX) || defined(VMS)
1647     // 'ttymouse'
1648     else if (varp == &p_ttym)
1649     {
1650 	// Switch the mouse off before changing the escape sequences used for
1651 	// that.
1652 	mch_setmouse(FALSE);
1653 	if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
1654 	    errmsg = e_invarg;
1655 	else
1656 	    check_mouse_termcode();
1657 	if (termcap_active)
1658 	    setmouse();		// may switch it on again
1659     }
1660 #endif
1661 
1662     // 'selection'
1663     else if (varp == &p_sel)
1664     {
1665 	if (*p_sel == NUL
1666 		|| check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
1667 	    errmsg = e_invarg;
1668     }
1669 
1670     // 'selectmode'
1671     else if (varp == &p_slm)
1672     {
1673 	if (check_opt_strings(p_slm, p_slm_values, TRUE) != OK)
1674 	    errmsg = e_invarg;
1675     }
1676 
1677 #ifdef FEAT_BROWSE
1678     // 'browsedir'
1679     else if (varp == &p_bsdir)
1680     {
1681 	if (check_opt_strings(p_bsdir, p_bsdir_values, FALSE) != OK
1682 		&& !mch_isdir(p_bsdir))
1683 	    errmsg = e_invarg;
1684     }
1685 #endif
1686 
1687     // 'keymodel'
1688     else if (varp == &p_km)
1689     {
1690 	if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
1691 	    errmsg = e_invarg;
1692 	else
1693 	{
1694 	    km_stopsel = (vim_strchr(p_km, 'o') != NULL);
1695 	    km_startsel = (vim_strchr(p_km, 'a') != NULL);
1696 	}
1697     }
1698 
1699     // 'mousemodel'
1700     else if (varp == &p_mousem)
1701     {
1702 	if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
1703 	    errmsg = e_invarg;
1704 #if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
1705 	else if (*p_mousem != *oldval)
1706 	    // Changed from "extend" to "popup" or "popup_setpos" or vv: need
1707 	    // to create or delete the popup menus.
1708 	    gui_motif_update_mousemodel(root_menu);
1709 #endif
1710     }
1711 
1712     // 'switchbuf'
1713     else if (varp == &p_swb)
1714     {
1715 	if (opt_strings_flags(p_swb, p_swb_values, &swb_flags, TRUE) != OK)
1716 	    errmsg = e_invarg;
1717     }
1718 
1719     // 'debug'
1720     else if (varp == &p_debug)
1721     {
1722 	if (check_opt_strings(p_debug, p_debug_values, TRUE) != OK)
1723 	    errmsg = e_invarg;
1724     }
1725 
1726     // 'display'
1727     else if (varp == &p_dy)
1728     {
1729 	if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
1730 	    errmsg = e_invarg;
1731 	else
1732 	    (void)init_chartab();
1733 
1734     }
1735 
1736     // 'eadirection'
1737     else if (varp == &p_ead)
1738     {
1739 	if (check_opt_strings(p_ead, p_ead_values, FALSE) != OK)
1740 	    errmsg = e_invarg;
1741     }
1742 
1743 #ifdef FEAT_CLIPBOARD
1744     // 'clipboard'
1745     else if (varp == &p_cb)
1746 	errmsg = check_clipboard_option();
1747 #endif
1748 
1749 #ifdef FEAT_SPELL
1750     // When 'spelllang' or 'spellfile' is set and there is a window for this
1751     // buffer in which 'spell' is set load the wordlists.
1752     else if (varp == &(curwin->w_s->b_p_spl)
1753 	    || varp == &(curwin->w_s->b_p_spf))
1754     {
1755 	int	is_spellfile = varp == &(curwin->w_s->b_p_spf);
1756 
1757 	if ((is_spellfile && !valid_spellfile(*varp))
1758 	    || (!is_spellfile && !valid_spelllang(*varp)))
1759 	    errmsg = e_invarg;
1760 	else
1761 	    errmsg = did_set_spell_option(is_spellfile);
1762     }
1763     // When 'spellcapcheck' is set compile the regexp program.
1764     else if (varp == &(curwin->w_s->b_p_spc))
1765     {
1766 	errmsg = compile_cap_prog(curwin->w_s);
1767     }
1768     // 'spelloptions'
1769     else if (varp == &(curwin->w_s->b_p_spo))
1770     {
1771 	if (**varp != NUL && STRCMP("camel", *varp) != 0)
1772 	    errmsg = e_invarg;
1773     }
1774     // 'spellsuggest'
1775     else if (varp == &p_sps)
1776     {
1777 	if (spell_check_sps() != OK)
1778 	    errmsg = e_invarg;
1779     }
1780     // 'mkspellmem'
1781     else if (varp == &p_msm)
1782     {
1783 	if (spell_check_msm() != OK)
1784 	    errmsg = e_invarg;
1785     }
1786 #endif
1787 
1788     // When 'bufhidden' is set, check for valid value.
1789     else if (gvarp == &p_bh)
1790     {
1791 	if (check_opt_strings(curbuf->b_p_bh, p_bufhidden_values, FALSE) != OK)
1792 	    errmsg = e_invarg;
1793     }
1794 
1795     // When 'buftype' is set, check for valid value.
1796     else if (gvarp == &p_bt)
1797     {
1798 	if (check_opt_strings(curbuf->b_p_bt, p_buftype_values, FALSE) != OK)
1799 	    errmsg = e_invarg;
1800 	else
1801 	{
1802 	    if (curwin->w_status_height)
1803 	    {
1804 		curwin->w_redr_status = TRUE;
1805 		redraw_later(VALID);
1806 	    }
1807 	    curbuf->b_help = (curbuf->b_p_bt[0] == 'h');
1808 #ifdef FEAT_TITLE
1809 	    redraw_titles();
1810 #endif
1811 	}
1812     }
1813 
1814 #ifdef FEAT_STL_OPT
1815     // 'statusline' or 'rulerformat'
1816     else if (gvarp == &p_stl || varp == &p_ruf)
1817     {
1818 	int wid;
1819 
1820 	if (varp == &p_ruf)	// reset ru_wid first
1821 	    ru_wid = 0;
1822 	s = *varp;
1823 	if (varp == &p_ruf && *s == '%')
1824 	{
1825 	    // set ru_wid if 'ruf' starts with "%99("
1826 	    if (*++s == '-')	// ignore a '-'
1827 		s++;
1828 	    wid = getdigits(&s);
1829 	    if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
1830 		ru_wid = wid;
1831 	    else
1832 		errmsg = check_stl_option(p_ruf);
1833 	}
1834 	// check 'statusline' only if it doesn't start with "%!"
1835 	else if (varp == &p_ruf || s[0] != '%' || s[1] != '!')
1836 	    errmsg = check_stl_option(s);
1837 	if (varp == &p_ruf && errmsg == NULL)
1838 	    comp_col();
1839     }
1840 #endif
1841 
1842     // check if it is a valid value for 'complete' -- Acevedo
1843     else if (gvarp == &p_cpt)
1844     {
1845 	for (s = *varp; *s;)
1846 	{
1847 	    while (*s == ',' || *s == ' ')
1848 		s++;
1849 	    if (!*s)
1850 		break;
1851 	    if (vim_strchr((char_u *)".wbuksid]tU", *s) == NULL)
1852 	    {
1853 		errmsg = illegal_char(errbuf, *s);
1854 		break;
1855 	    }
1856 	    if (*++s != NUL && *s != ',' && *s != ' ')
1857 	    {
1858 		if (s[-1] == 'k' || s[-1] == 's')
1859 		{
1860 		    // skip optional filename after 'k' and 's'
1861 		    while (*s && *s != ',' && *s != ' ')
1862 		    {
1863 			if (*s == '\\' && s[1] != NUL)
1864 			    ++s;
1865 			++s;
1866 		    }
1867 		}
1868 		else
1869 		{
1870 		    if (errbuf != NULL)
1871 		    {
1872 			sprintf((char *)errbuf,
1873 				     _("E535: Illegal character after <%c>"),
1874 				     *--s);
1875 			errmsg = errbuf;
1876 		    }
1877 		    else
1878 			errmsg = "";
1879 		    break;
1880 		}
1881 	    }
1882 	}
1883     }
1884 
1885     // 'completeopt'
1886     else if (varp == &p_cot)
1887     {
1888 	if (check_opt_strings(p_cot, p_cot_values, TRUE) != OK)
1889 	    errmsg = e_invarg;
1890 	else
1891 	    completeopt_was_set();
1892     }
1893 
1894 #ifdef BACKSLASH_IN_FILENAME
1895     // 'completeslash'
1896     else if (gvarp == &p_csl)
1897     {
1898 	if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1899 		|| check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
1900 	    errmsg = e_invarg;
1901     }
1902 #endif
1903 
1904 #ifdef FEAT_SIGNS
1905     // 'signcolumn'
1906     else if (varp == &curwin->w_p_scl)
1907     {
1908 	if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
1909 	    errmsg = e_invarg;
1910 	// When changing the 'signcolumn' to or from 'number', recompute the
1911 	// width of the number column if 'number' or 'relativenumber' is set.
1912 	if (((*oldval == 'n' && *(oldval + 1) == 'u')
1913 		|| (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
1914 		&& (curwin->w_p_nu || curwin->w_p_rnu))
1915 	    curwin->w_nrwidth_line_count = 0;
1916     }
1917 #endif
1918 
1919 
1920 #if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
1921     // 'toolbar'
1922     else if (varp == &p_toolbar)
1923     {
1924 	if (opt_strings_flags(p_toolbar, p_toolbar_values,
1925 			      &toolbar_flags, TRUE) != OK)
1926 	    errmsg = e_invarg;
1927 	else
1928 	{
1929 	    out_flush();
1930 	    gui_mch_show_toolbar((toolbar_flags &
1931 				  (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
1932 	}
1933     }
1934 #endif
1935 
1936 #if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
1937     // 'toolbariconsize': GTK+ 2 only
1938     else if (varp == &p_tbis)
1939     {
1940 	if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
1941 	    errmsg = e_invarg;
1942 	else
1943 	{
1944 	    out_flush();
1945 	    gui_mch_show_toolbar((toolbar_flags &
1946 				  (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
1947 	}
1948     }
1949 #endif
1950 
1951     // 'pastetoggle': translate key codes like in a mapping
1952     else if (varp == &p_pt)
1953     {
1954 	if (*p_pt)
1955 	{
1956 	    (void)replace_termcodes(p_pt, &p,
1957 				      REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
1958 	    if (p != NULL)
1959 	    {
1960 		if (new_value_alloced)
1961 		    free_string_option(p_pt);
1962 		p_pt = p;
1963 		new_value_alloced = TRUE;
1964 	    }
1965 	}
1966     }
1967 
1968     // 'backspace'
1969     else if (varp == &p_bs)
1970     {
1971 	if (VIM_ISDIGIT(*p_bs))
1972 	{
1973 	    if (*p_bs > '3' || p_bs[1] != NUL)
1974 		errmsg = e_invarg;
1975 	}
1976 	else if (check_opt_strings(p_bs, p_bs_values, TRUE) != OK)
1977 	    errmsg = e_invarg;
1978     }
1979     else if (varp == &p_bo)
1980     {
1981 	if (opt_strings_flags(p_bo, p_bo_values, &bo_flags, TRUE) != OK)
1982 	    errmsg = e_invarg;
1983     }
1984 
1985     // 'tagcase'
1986     else if (gvarp == &p_tc)
1987     {
1988 	unsigned int	*flags;
1989 
1990 	if (opt_flags & OPT_LOCAL)
1991 	{
1992 	    p = curbuf->b_p_tc;
1993 	    flags = &curbuf->b_tc_flags;
1994 	}
1995 	else
1996 	{
1997 	    p = p_tc;
1998 	    flags = &tc_flags;
1999 	}
2000 
2001 	if ((opt_flags & OPT_LOCAL) && *p == NUL)
2002 	    // make the local value empty: use the global value
2003 	    *flags = 0;
2004 	else if (*p == NUL
2005 		|| opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
2006 	    errmsg = e_invarg;
2007     }
2008 
2009     // 'casemap'
2010     else if (varp == &p_cmp)
2011     {
2012 	if (opt_strings_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE) != OK)
2013 	    errmsg = e_invarg;
2014     }
2015 
2016 #ifdef FEAT_DIFF
2017     // 'diffopt'
2018     else if (varp == &p_dip)
2019     {
2020 	if (diffopt_changed() == FAIL)
2021 	    errmsg = e_invarg;
2022     }
2023 #endif
2024 
2025 #ifdef FEAT_FOLDING
2026     // 'foldmethod'
2027     else if (gvarp == &curwin->w_allbuf_opt.wo_fdm)
2028     {
2029 	if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK
2030 		|| *curwin->w_p_fdm == NUL)
2031 	    errmsg = e_invarg;
2032 	else
2033 	{
2034 	    foldUpdateAll(curwin);
2035 	    if (foldmethodIsDiff(curwin))
2036 		newFoldLevel();
2037 	}
2038     }
2039 # ifdef FEAT_EVAL
2040     // 'foldexpr'
2041     else if (varp == &curwin->w_p_fde)
2042     {
2043 	if (foldmethodIsExpr(curwin))
2044 	    foldUpdateAll(curwin);
2045     }
2046 # endif
2047     // 'foldmarker'
2048     else if (gvarp == &curwin->w_allbuf_opt.wo_fmr)
2049     {
2050 	p = vim_strchr(*varp, ',');
2051 	if (p == NULL)
2052 	    errmsg = N_("E536: comma required");
2053 	else if (p == *varp || p[1] == NUL)
2054 	    errmsg = e_invarg;
2055 	else if (foldmethodIsMarker(curwin))
2056 	    foldUpdateAll(curwin);
2057     }
2058     // 'commentstring'
2059     else if (gvarp == &p_cms)
2060     {
2061 	if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
2062 	    errmsg = N_("E537: 'commentstring' must be empty or contain %s");
2063     }
2064     // 'foldopen'
2065     else if (varp == &p_fdo)
2066     {
2067 	if (opt_strings_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE) != OK)
2068 	    errmsg = e_invarg;
2069     }
2070     // 'foldclose'
2071     else if (varp == &p_fcl)
2072     {
2073 	if (check_opt_strings(p_fcl, p_fcl_values, TRUE) != OK)
2074 	    errmsg = e_invarg;
2075     }
2076     // 'foldignore'
2077     else if (gvarp == &curwin->w_allbuf_opt.wo_fdi)
2078     {
2079 	if (foldmethodIsIndent(curwin))
2080 	    foldUpdateAll(curwin);
2081     }
2082 #endif
2083 
2084     // 'virtualedit'
2085     else if (gvarp == &p_ve)
2086     {
2087 	char_u		*ve = p_ve;
2088 	unsigned int	*flags = &ve_flags;
2089 
2090 	if (opt_flags & OPT_LOCAL)
2091 	{
2092 	    ve = curwin->w_p_ve;
2093 	    flags = &curwin->w_ve_flags;
2094 	}
2095 
2096 	if ((opt_flags & OPT_LOCAL) && *ve == NUL)
2097 	    // make the local value empty: use the global value
2098 	    *flags = 0;
2099 	else
2100 	{
2101 	    if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
2102 		errmsg = e_invarg;
2103 	    else if (STRCMP(p_ve, oldval) != 0)
2104 	    {
2105 		// Recompute cursor position in case the new 've' setting
2106 		// changes something.
2107 		validate_virtcol();
2108 		coladvance(curwin->w_virtcol);
2109 	    }
2110 	}
2111     }
2112 
2113 #if defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)
2114     else if (varp == &p_csqf)
2115     {
2116 	if (p_csqf != NULL)
2117 	{
2118 	    p = p_csqf;
2119 	    while (*p != NUL)
2120 	    {
2121 		if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
2122 			|| p[1] == NUL
2123 			|| vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
2124 			|| (p[2] != NUL && p[2] != ','))
2125 		{
2126 		    errmsg = e_invarg;
2127 		    break;
2128 		}
2129 		else if (p[2] == NUL)
2130 		    break;
2131 		else
2132 		    p += 3;
2133 	    }
2134 	}
2135     }
2136 #endif
2137 
2138 #ifdef FEAT_CINDENT
2139     // 'cinoptions'
2140     else if (gvarp == &p_cino)
2141     {
2142 	// TODO: recognize errors
2143 	parse_cino(curbuf);
2144     }
2145 #endif
2146 
2147 #if defined(FEAT_RENDER_OPTIONS)
2148     // 'renderoptions'
2149     else if (varp == &p_rop)
2150     {
2151 	if (!gui_mch_set_rendering_options(p_rop))
2152 	    errmsg = e_invarg;
2153     }
2154 #endif
2155 
2156     else if (gvarp == &p_ft)
2157     {
2158 	if (!valid_filetype(*varp))
2159 	    errmsg = e_invarg;
2160 	else
2161 	{
2162 	    value_changed = STRCMP(oldval, *varp) != 0;
2163 
2164 	    // Since we check the value, there is no need to set P_INSECURE,
2165 	    // even when the value comes from a modeline.
2166 	    *value_checked = TRUE;
2167 	}
2168     }
2169 
2170 #ifdef FEAT_SYN_HL
2171     else if (gvarp == &p_syn)
2172     {
2173 	if (!valid_filetype(*varp))
2174 	    errmsg = e_invarg;
2175 	else
2176 	{
2177 	    value_changed = STRCMP(oldval, *varp) != 0;
2178 
2179 	    // Since we check the value, there is no need to set P_INSECURE,
2180 	    // even when the value comes from a modeline.
2181 	    *value_checked = TRUE;
2182 	}
2183     }
2184 #endif
2185 
2186 #ifdef FEAT_TERMINAL
2187     // 'termwinkey'
2188     else if (varp == &curwin->w_p_twk)
2189     {
2190 	if (*curwin->w_p_twk != NUL
2191 				  && string_to_key(curwin->w_p_twk, TRUE) == 0)
2192 	    errmsg = e_invarg;
2193     }
2194     // 'termwinsize'
2195     else if (varp == &curwin->w_p_tws)
2196     {
2197 	if (*curwin->w_p_tws != NUL)
2198 	{
2199 	    p = skipdigits(curwin->w_p_tws);
2200 	    if (p == curwin->w_p_tws
2201 		    || (*p != 'x' && *p != '*')
2202 		    || *skipdigits(p + 1) != NUL)
2203 		errmsg = e_invarg;
2204 	}
2205     }
2206     // 'wincolor'
2207     else if (varp == &curwin->w_p_wcr)
2208 	term_update_wincolor(curwin);
2209 # if defined(MSWIN)
2210     // 'termwintype'
2211     else if (varp == &p_twt)
2212     {
2213 	if (check_opt_strings(*varp, p_twt_values, FALSE) != OK)
2214 	    errmsg = e_invarg;
2215     }
2216 # endif
2217 #endif
2218 
2219 #ifdef FEAT_VARTABS
2220     // 'varsofttabstop'
2221     else if (varp == &(curbuf->b_p_vsts))
2222     {
2223 	char_u *cp;
2224 
2225 	if (!(*varp)[0] || ((*varp)[0] == '0' && !(*varp)[1]))
2226 	{
2227 	    if (curbuf->b_p_vsts_array)
2228 	    {
2229 		vim_free(curbuf->b_p_vsts_array);
2230 		curbuf->b_p_vsts_array = 0;
2231 	    }
2232 	}
2233 	else
2234 	{
2235 	    for (cp = *varp; *cp; ++cp)
2236 	    {
2237 		if (vim_isdigit(*cp))
2238 		    continue;
2239 		if (*cp == ',' && cp > *varp && *(cp-1) != ',')
2240 		    continue;
2241 		errmsg = e_invarg;
2242 		break;
2243 	    }
2244 	    if (errmsg == NULL)
2245 	    {
2246 		int *oldarray = curbuf->b_p_vsts_array;
2247 		if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
2248 		{
2249 		    if (oldarray)
2250 			vim_free(oldarray);
2251 		}
2252 		else
2253 		    errmsg = e_invarg;
2254 	    }
2255 	}
2256     }
2257 
2258     // 'vartabstop'
2259     else if (varp == &(curbuf->b_p_vts))
2260     {
2261 	char_u *cp;
2262 
2263 	if (!(*varp)[0] || ((*varp)[0] == '0' && !(*varp)[1]))
2264 	{
2265 	    if (curbuf->b_p_vts_array)
2266 	    {
2267 		vim_free(curbuf->b_p_vts_array);
2268 		curbuf->b_p_vts_array = NULL;
2269 	    }
2270 	}
2271 	else
2272 	{
2273 	    for (cp = *varp; *cp; ++cp)
2274 	    {
2275 		if (vim_isdigit(*cp))
2276 		    continue;
2277 		if (*cp == ',' && cp > *varp && *(cp-1) != ',')
2278 		    continue;
2279 		errmsg = e_invarg;
2280 		break;
2281 	    }
2282 	    if (errmsg == NULL)
2283 	    {
2284 		int *oldarray = curbuf->b_p_vts_array;
2285 
2286 		if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
2287 		{
2288 		    vim_free(oldarray);
2289 #ifdef FEAT_FOLDING
2290 		    if (foldmethodIsIndent(curwin))
2291 			foldUpdateAll(curwin);
2292 #endif
2293 		}
2294 		else
2295 		    errmsg = e_invarg;
2296 	    }
2297 	}
2298     }
2299 #endif
2300 
2301 #ifdef FEAT_PROP_POPUP
2302     // 'previewpopup'
2303     else if (varp == &p_pvp)
2304     {
2305 	if (parse_previewpopup(NULL) == FAIL)
2306 	    errmsg = e_invarg;
2307     }
2308 # ifdef FEAT_QUICKFIX
2309     // 'completepopup'
2310     else if (varp == &p_cpp)
2311     {
2312 	if (parse_completepopup(NULL) == FAIL)
2313 	    errmsg = e_invarg;
2314 	else
2315 	    popup_close_info();
2316     }
2317 # endif
2318 #endif
2319 
2320     // 'operatorfunc'
2321     else if (varp == &p_opfunc)
2322     {
2323 	if (set_operatorfunc_option() == FAIL)
2324 	    errmsg = e_invarg;
2325     }
2326 
2327 #ifdef FEAT_QUICKFIX
2328     // 'quickfixtextfunc'
2329     else if (varp == &p_qftf)
2330     {
2331 	if (qf_process_qftf_option() == FAIL)
2332 	    errmsg = e_invarg;
2333     }
2334 #endif
2335 
2336     // Options that are a list of flags.
2337     else
2338     {
2339 	p = NULL;
2340 	if (varp == &p_ww) // 'whichwrap'
2341 	    p = (char_u *)WW_ALL;
2342 	if (varp == &p_shm) // 'shortmess'
2343 	    p = (char_u *)SHM_ALL;
2344 	else if (varp == &(p_cpo)) // 'cpoptions'
2345 	    p = (char_u *)CPO_ALL;
2346 	else if (varp == &(curbuf->b_p_fo)) // 'formatoptions'
2347 	    p = (char_u *)FO_ALL;
2348 #ifdef FEAT_CONCEAL
2349 	else if (varp == &curwin->w_p_cocu) // 'concealcursor'
2350 	    p = (char_u *)COCU_ALL;
2351 #endif
2352 	else if (varp == &p_mouse) // 'mouse'
2353 	{
2354 	    p = (char_u *)MOUSE_ALL;
2355 	}
2356 #if defined(FEAT_GUI)
2357 	else if (varp == &p_go) // 'guioptions'
2358 	    p = (char_u *)GO_ALL;
2359 #endif
2360 	if (p != NULL)
2361 	{
2362 	    for (s = *varp; *s; ++s)
2363 		if (vim_strchr(p, *s) == NULL)
2364 		{
2365 		    errmsg = illegal_char(errbuf, *s);
2366 		    break;
2367 		}
2368 	}
2369     }
2370 
2371     // If error detected, restore the previous value.
2372     if (errmsg != NULL)
2373     {
2374 	if (new_value_alloced)
2375 	    free_string_option(*varp);
2376 	*varp = oldval;
2377 	// When resetting some values, need to act on it.
2378 	if (did_chartab)
2379 	    (void)init_chartab();
2380 	if (varp == &p_hl)
2381 	    (void)highlight_changed();
2382     }
2383     else
2384     {
2385 #ifdef FEAT_EVAL
2386 	// Remember where the option was set.
2387 	set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
2388 #endif
2389 	// Free string options that are in allocated memory.
2390 	// Use "free_oldval", because recursiveness may change the flags under
2391 	// our fingers (esp. init_highlight()).
2392 	if (free_oldval)
2393 	    free_string_option(oldval);
2394 	if (new_value_alloced)
2395 	    set_option_flag(opt_idx, P_ALLOCED);
2396 	else
2397 	    clear_option_flag(opt_idx, P_ALLOCED);
2398 
2399 	if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
2400 		&& is_global_local_option(opt_idx))
2401 	{
2402 	    // global option with local value set to use global value; free
2403 	    // the local value and make it empty
2404 	    p = get_option_varp_scope(opt_idx, OPT_LOCAL);
2405 	    free_string_option(*(char_u **)p);
2406 	    *(char_u **)p = empty_option;
2407 	}
2408 
2409 	// May set global value for local option.
2410 	else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
2411 	    set_string_option_global(opt_idx, varp);
2412 
2413 	// Trigger the autocommand only after setting the flags.
2414 #ifdef FEAT_SYN_HL
2415 	// When 'syntax' is set, load the syntax of that name
2416 	if (varp == &(curbuf->b_p_syn))
2417 	{
2418 	    static int syn_recursive = 0;
2419 
2420 	    ++syn_recursive;
2421 	    // Only pass TRUE for "force" when the value changed or not used
2422 	    // recursively, to avoid endless recurrence.
2423 	    apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
2424 		    value_changed || syn_recursive == 1, curbuf);
2425 	    curbuf->b_flags |= BF_SYN_SET;
2426 	    --syn_recursive;
2427 	}
2428 #endif
2429 	else if (varp == &(curbuf->b_p_ft))
2430 	{
2431 	    // 'filetype' is set, trigger the FileType autocommand.
2432 	    // Skip this when called from a modeline and the filetype was
2433 	    // already set to this value.
2434 	    if (!(opt_flags & OPT_MODELINE) || value_changed)
2435 	    {
2436 		static int  ft_recursive = 0;
2437 		int	    secure_save = secure;
2438 
2439 		// Reset the secure flag, since the value of 'filetype' has
2440 		// been checked to be safe.
2441 		secure = 0;
2442 
2443 		++ft_recursive;
2444 		did_filetype = TRUE;
2445 		// Only pass TRUE for "force" when the value changed or not
2446 		// used recursively, to avoid endless recurrence.
2447 		apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2448 				   value_changed || ft_recursive == 1, curbuf);
2449 		--ft_recursive;
2450 		// Just in case the old "curbuf" is now invalid.
2451 		if (varp != &(curbuf->b_p_ft))
2452 		    varp = NULL;
2453 
2454 		secure = secure_save;
2455 	    }
2456 	}
2457 #ifdef FEAT_SPELL
2458 	if (varp == &(curwin->w_s->b_p_spl))
2459 	{
2460 	    char_u	fname[200];
2461 	    char_u	*q = curwin->w_s->b_p_spl;
2462 
2463 	    // Skip the first name if it is "cjk".
2464 	    if (STRNCMP(q, "cjk,", 4) == 0)
2465 		q += 4;
2466 
2467 	    // Source the spell/LANG.vim in 'runtimepath'.
2468 	    // They could set 'spellcapcheck' depending on the language.
2469 	    // Use the first name in 'spelllang' up to '_region' or
2470 	    // '.encoding'.
2471 	    for (p = q; *p != NUL; ++p)
2472 		if (!ASCII_ISALNUM(*p) && *p != '-')
2473 		    break;
2474 	    if (p > q)
2475 	    {
2476 		vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
2477 							      (int)(p - q), q);
2478 		source_runtime(fname, DIP_ALL);
2479 	    }
2480 	}
2481 #endif
2482     }
2483 
2484     if (varp == &p_mouse)
2485     {
2486 	if (*p_mouse == NUL)
2487 	    mch_setmouse(FALSE);    // switch mouse off
2488 	else
2489 	    setmouse();		    // in case 'mouse' changed
2490     }
2491 
2492 #if defined(FEAT_LUA) || defined(PROTO)
2493     if (varp == &p_rtp)
2494 	update_package_paths_in_lua();
2495 #endif
2496 
2497     if (curwin->w_curswant != MAXCOL
2498 		   && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0)
2499 	curwin->w_set_curswant = TRUE;
2500 
2501     if ((opt_flags & OPT_NO_REDRAW) == 0)
2502     {
2503 #ifdef FEAT_GUI
2504 	// check redraw when it's not a GUI option or the GUI is active.
2505 	if (!redraw_gui_only || gui.in_use)
2506 #endif
2507 	    check_redraw(get_option_flags(opt_idx));
2508     }
2509 
2510 #if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2511     if (did_swaptcap)
2512     {
2513 	set_termname((char_u *)"win32");
2514 	init_highlight(TRUE, FALSE);
2515     }
2516 #endif
2517 
2518     return errmsg;
2519 }
2520 
2521 /*
2522  * Check an option that can be a range of string values.
2523  *
2524  * Return OK for correct value, FAIL otherwise.
2525  * Empty is always OK.
2526  */
2527     static int
check_opt_strings(char_u * val,char ** values,int list)2528 check_opt_strings(
2529     char_u	*val,
2530     char	**values,
2531     int		list)	    // when TRUE: accept a list of values
2532 {
2533     return opt_strings_flags(val, values, NULL, list);
2534 }
2535 
2536 /*
2537  * Handle an option that can be a range of string values.
2538  * Set a flag in "*flagp" for each string present.
2539  *
2540  * Return OK for correct value, FAIL otherwise.
2541  * Empty is always OK.
2542  */
2543     static int
opt_strings_flags(char_u * val,char ** values,unsigned * flagp,int list)2544 opt_strings_flags(
2545     char_u	*val,		// new value
2546     char	**values,	// array of valid string values
2547     unsigned	*flagp,
2548     int		list)		// when TRUE: accept a list of values
2549 {
2550     int		i;
2551     int		len;
2552     unsigned	new_flags = 0;
2553 
2554     while (*val)
2555     {
2556 	for (i = 0; ; ++i)
2557 	{
2558 	    if (values[i] == NULL)	// val not found in values[]
2559 		return FAIL;
2560 
2561 	    len = (int)STRLEN(values[i]);
2562 	    if (STRNCMP(values[i], val, len) == 0
2563 		    && ((list && val[len] == ',') || val[len] == NUL))
2564 	    {
2565 		val += len + (val[len] == ',');
2566 		new_flags |= (1 << i);
2567 		break;		// check next item in val list
2568 	    }
2569 	}
2570     }
2571     if (flagp != NULL)
2572 	*flagp = new_flags;
2573 
2574     return OK;
2575 }
2576 
2577 /*
2578  * return OK if "p" is a valid fileformat name, FAIL otherwise.
2579  */
2580     int
check_ff_value(char_u * p)2581 check_ff_value(char_u *p)
2582 {
2583     return check_opt_strings(p, p_ff_values, FALSE);
2584 }
2585