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