blob: f332294ed83714605dcae23bc8fe4d6552f7d4b7 [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 {
shadmansaleh30e3de22021-05-15 17:23:28 +0200621 int reevaluate = (*s == '%');
622
Bram Moolenaardac13472019-09-16 21:06:21 +0200623 s++;
shadmansaleh30e3de22021-05-15 17:23:28 +0200624 while ((*s != '}' || (reevaluate && s[-1] != '%')) && *s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200625 s++;
626 if (*s != '}')
627 return N_("E540: Unclosed expression sequence");
628 }
629 }
Bram Moolenaardac13472019-09-16 21:06:21 +0200630 if (groupdepth != 0)
631 return N_("E542: unbalanced groups");
632 return NULL;
633}
634#endif
635
636/*
637 * Handle string options that need some action to perform when changed.
638 * Returns NULL for success, or an error message for an error.
639 */
640 char *
641did_set_string_option(
642 int opt_idx, // index in options[] table
643 char_u **varp, // pointer to the option variable
644 int new_value_alloced, // new value was allocated
645 char_u *oldval, // previous value of the option
646 char *errbuf, // buffer for errors, or NULL
647 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
648 int *value_checked) // value was checked to be save, no
649 // need to set P_INSECURE
650{
651 char *errmsg = NULL;
652 char_u *s, *p;
653 int did_chartab = FALSE;
654 char_u **gvarp;
655 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
656#ifdef FEAT_GUI
657 // set when changing an option that only requires a redraw in the GUI
658 int redraw_gui_only = FALSE;
659#endif
660 int value_changed = FALSE;
661#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
662 int did_swaptcap = FALSE;
663#endif
664
665 // Get the global option to compare with, otherwise we would have to check
666 // two values for all local options.
667 gvarp = (char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
668
669 // Disallow changing some options from secure mode
670 if ((secure
671#ifdef HAVE_SANDBOX
672 || sandbox != 0
673#endif
674 ) && (get_option_flags(opt_idx) & P_SECURE))
675 errmsg = e_secure;
676
677 // Check for a "normal" directory or file name in some options. Disallow a
678 // path separator (slash and/or backslash), wildcards and characters that
679 // are often illegal in a file name. Be more permissive if "secure" is off.
680 else if (((get_option_flags(opt_idx) & P_NFNAME)
681 && vim_strpbrk(*varp, (char_u *)(secure
682 ? "/\\*?[|;&<>\r\n" : "/\\*?[<>\r\n")) != NULL)
683 || ((get_option_flags(opt_idx) & P_NDNAME)
684 && vim_strpbrk(*varp, (char_u *)"*?[|;&<>\r\n") != NULL))
685 errmsg = e_invarg;
686
687 // 'term'
688 else if (varp == &T_NAME)
689 {
690 if (T_NAME[0] == NUL)
691 errmsg = N_("E529: Cannot set 'term' to empty string");
692#ifdef FEAT_GUI
Bram Moolenaar5daa9112021-02-01 18:39:47 +0100693 else if (gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +0200694 errmsg = N_("E530: Cannot change term in GUI");
695 else if (term_is_gui(T_NAME))
696 errmsg = N_("E531: Use \":gui\" to start the GUI");
697#endif
698 else if (set_termname(T_NAME) == FAIL)
699 errmsg = N_("E522: Not found in termcap");
700 else
701 {
702 // Screen colors may have changed.
703 redraw_later_clear();
704
705 // Both 'term' and 'ttytype' point to T_NAME, only set the
706 // P_ALLOCED flag on 'term'.
707 opt_idx = findoption((char_u *)"term");
708 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
709 }
710 }
711
712 // 'backupcopy'
713 else if (gvarp == &p_bkc)
714 {
715 char_u *bkc = p_bkc;
716 unsigned int *flags = &bkc_flags;
717
718 if (opt_flags & OPT_LOCAL)
719 {
720 bkc = curbuf->b_p_bkc;
721 flags = &curbuf->b_bkc_flags;
722 }
723
724 if ((opt_flags & OPT_LOCAL) && *bkc == NUL)
725 // make the local value empty: use the global value
726 *flags = 0;
727 else
728 {
729 if (opt_strings_flags(bkc, p_bkc_values, flags, TRUE) != OK)
730 errmsg = e_invarg;
731 if ((((int)*flags & BKC_AUTO) != 0)
732 + (((int)*flags & BKC_YES) != 0)
733 + (((int)*flags & BKC_NO) != 0) != 1)
734 {
735 // Must have exactly one of "auto", "yes" and "no".
736 (void)opt_strings_flags(oldval, p_bkc_values, flags, TRUE);
737 errmsg = e_invarg;
738 }
739 }
740 }
741
742 // 'backupext' and 'patchmode'
743 else if (varp == &p_bex || varp == &p_pm)
744 {
745 if (STRCMP(*p_bex == '.' ? p_bex + 1 : p_bex,
746 *p_pm == '.' ? p_pm + 1 : p_pm) == 0)
747 errmsg = N_("E589: 'backupext' and 'patchmode' are equal");
748 }
749#ifdef FEAT_LINEBREAK
750 // 'breakindentopt'
751 else if (varp == &curwin->w_p_briopt)
752 {
753 if (briopt_check(curwin) == FAIL)
754 errmsg = e_invarg;
755 }
756#endif
757
758 // 'isident', 'iskeyword', 'isprint or 'isfname' option: refill g_chartab[]
759 // If the new option is invalid, use old value. 'lisp' option: refill
760 // g_chartab[] for '-' char
761 else if ( varp == &p_isi
762 || varp == &(curbuf->b_p_isk)
763 || varp == &p_isp
764 || varp == &p_isf)
765 {
766 if (init_chartab() == FAIL)
767 {
768 did_chartab = TRUE; // need to restore it below
769 errmsg = e_invarg; // error in value
770 }
771 }
772
773 // 'helpfile'
774 else if (varp == &p_hf)
775 {
776 // May compute new values for $VIM and $VIMRUNTIME
777 if (didset_vim)
778 {
779 vim_setenv((char_u *)"VIM", (char_u *)"");
780 didset_vim = FALSE;
781 }
782 if (didset_vimruntime)
783 {
784 vim_setenv((char_u *)"VIMRUNTIME", (char_u *)"");
785 didset_vimruntime = FALSE;
786 }
787 }
788
789#ifdef FEAT_SYN_HL
790 // 'cursorlineopt'
791 else if (varp == &curwin->w_p_culopt
792 || gvarp == &curwin->w_allbuf_opt.wo_culopt)
793 {
794 if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
795 errmsg = e_invarg;
796 }
797
798 // 'colorcolumn'
799 else if (varp == &curwin->w_p_cc)
800 errmsg = check_colorcolumn(curwin);
801#endif
802
803#ifdef FEAT_MULTI_LANG
804 // 'helplang'
805 else if (varp == &p_hlg)
806 {
807 // Check for "", "ab", "ab,cd", etc.
808 for (s = p_hlg; *s != NUL; s += 3)
809 {
810 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
811 {
812 errmsg = e_invarg;
813 break;
814 }
815 if (s[2] == NUL)
816 break;
817 }
818 }
819#endif
820
821 // 'highlight'
822 else if (varp == &p_hl)
823 {
824 if (highlight_changed() == FAIL)
825 errmsg = e_invarg; // invalid flags
826 }
827
828 // 'nrformats'
829 else if (gvarp == &p_nf)
830 {
831 if (check_opt_strings(*varp, p_nf_values, TRUE) != OK)
832 errmsg = e_invarg;
833 }
834
835#ifdef FEAT_SESSION
836 // 'sessionoptions'
837 else if (varp == &p_ssop)
838 {
839 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
840 errmsg = e_invarg;
841 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
842 {
843 // Don't allow both "sesdir" and "curdir".
844 (void)opt_strings_flags(oldval, p_ssop_values, &ssop_flags, TRUE);
845 errmsg = e_invarg;
846 }
847 }
848 // 'viewoptions'
849 else if (varp == &p_vop)
850 {
851 if (opt_strings_flags(p_vop, p_ssop_values, &vop_flags, TRUE) != OK)
852 errmsg = e_invarg;
853 }
854#endif
855
856 // 'scrollopt'
857 else if (varp == &p_sbo)
858 {
859 if (check_opt_strings(p_sbo, p_scbopt_values, TRUE) != OK)
860 errmsg = e_invarg;
861 }
862
863 // 'ambiwidth'
864 else if (varp == &p_ambw || varp == &p_emoji)
865 {
866 if (check_opt_strings(p_ambw, p_ambw_values, FALSE) != OK)
867 errmsg = e_invarg;
Bram Moolenaareed9d462021-02-15 20:38:25 +0100868 else if (set_chars_option(curwin, &p_fcs) != NULL)
Bram Moolenaardac13472019-09-16 21:06:21 +0200869 errmsg = _("E835: Conflicts with value of 'fillchars'");
Bram Moolenaareed9d462021-02-15 20:38:25 +0100870 else
871 {
872 tabpage_T *tp;
873 win_T *wp;
874
875 FOR_ALL_TAB_WINDOWS(tp, wp)
876 {
877 if (set_chars_option(wp, &wp->w_p_lcs) != NULL)
878 {
879 errmsg = _("E834: Conflicts with value of 'listchars'");
880 goto ambw_end;
881 }
882 }
883 }
884ambw_end:
885 {}
Bram Moolenaardac13472019-09-16 21:06:21 +0200886 }
887
888 // 'background'
889 else if (varp == &p_bg)
890 {
891 if (check_opt_strings(p_bg, p_bg_values, FALSE) == OK)
892 {
893#ifdef FEAT_EVAL
894 int dark = (*p_bg == 'd');
895#endif
896
897 init_highlight(FALSE, FALSE);
898
899#ifdef FEAT_EVAL
900 if (dark != (*p_bg == 'd')
901 && get_var_value((char_u *)"g:colors_name") != NULL)
902 {
903 // The color scheme must have set 'background' back to another
904 // value, that's not what we want here. Disable the color
905 // scheme and set the colors again.
906 do_unlet((char_u *)"g:colors_name", TRUE);
907 free_string_option(p_bg);
908 p_bg = vim_strsave((char_u *)(dark ? "dark" : "light"));
909 check_string_option(&p_bg);
910 init_highlight(FALSE, FALSE);
911 }
912#endif
Bram Moolenaarad431992021-05-03 20:40:38 +0200913#ifdef FEAT_TERMINAL
914 term_update_colors_all();
915#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200916 }
917 else
918 errmsg = e_invarg;
919 }
920
921 // 'wildmode'
922 else if (varp == &p_wim)
923 {
924 if (check_opt_wim() == FAIL)
925 errmsg = e_invarg;
926 }
927
928 // 'wildoptions'
929 else if (varp == &p_wop)
930 {
931 if (check_opt_strings(p_wop, p_wop_values, TRUE) != OK)
932 errmsg = e_invarg;
933 }
934
935#ifdef FEAT_WAK
936 // 'winaltkeys'
937 else if (varp == &p_wak)
938 {
939 if (*p_wak == NUL
940 || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
941 errmsg = e_invarg;
942# ifdef FEAT_MENU
943# ifdef FEAT_GUI_MOTIF
944 else if (gui.in_use)
945 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
946# else
947# ifdef FEAT_GUI_GTK
948 else if (gui.in_use)
949 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
950# endif
951# endif
952# endif
953 }
954#endif
955
956 // 'eventignore'
957 else if (varp == &p_ei)
958 {
959 if (check_ei() == FAIL)
960 errmsg = e_invarg;
961 }
962
963 // 'encoding', 'fileencoding', 'termencoding' and 'makeencoding'
964 else if (varp == &p_enc || gvarp == &p_fenc || varp == &p_tenc
965 || gvarp == &p_menc)
966 {
967 if (gvarp == &p_fenc)
968 {
969 if (!curbuf->b_p_ma && opt_flags != OPT_GLOBAL)
970 errmsg = e_modifiable;
971 else if (vim_strchr(*varp, ',') != NULL)
972 // No comma allowed in 'fileencoding'; catches confusing it
973 // with 'fileencodings'.
974 errmsg = e_invarg;
975 else
976 {
977#ifdef FEAT_TITLE
978 // May show a "+" in the title now.
979 redraw_titles();
980#endif
981 // Add 'fileencoding' to the swap file.
982 ml_setflags(curbuf);
983 }
984 }
985 if (errmsg == NULL)
986 {
987 // canonize the value, so that STRCMP() can be used on it
988 p = enc_canonize(*varp);
989 if (p != NULL)
990 {
991 vim_free(*varp);
992 *varp = p;
993 }
994 if (varp == &p_enc)
995 {
996 errmsg = mb_init();
997#ifdef FEAT_TITLE
998 redraw_titles();
999#endif
1000 }
1001 }
1002
1003#if defined(FEAT_GUI_GTK)
1004 if (errmsg == NULL && varp == &p_tenc && gui.in_use)
1005 {
1006 // GTK+ 2 uses only a single encoding, and that is UTF-8.
1007 if (STRCMP(p_tenc, "utf-8") != 0)
1008 errmsg = N_("E617: Cannot be changed in the GTK+ 2 GUI");
1009 }
1010#endif
1011
1012 if (errmsg == NULL)
1013 {
1014#ifdef FEAT_KEYMAP
1015 // When 'keymap' is used and 'encoding' changes, reload the keymap
1016 // (with another encoding).
1017 if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
1018 (void)keymap_init();
1019#endif
1020
1021 // When 'termencoding' is not empty and 'encoding' changes or when
1022 // 'termencoding' changes, need to setup for keyboard input and
1023 // display output conversion.
1024 if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
1025 {
1026 if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
1027 || convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
1028 {
1029 semsg(_("E950: Cannot convert between %s and %s"),
1030 p_tenc, p_enc);
1031 errmsg = e_invarg;
1032 }
1033 }
1034
1035#if defined(MSWIN)
1036 // $HOME may have characters in active code page.
1037 if (varp == &p_enc)
1038 init_homedir();
1039#endif
1040 }
1041 }
1042
1043#if defined(FEAT_POSTSCRIPT)
1044 else if (varp == &p_penc)
1045 {
1046 // Canonize printencoding if VIM standard one
1047 p = enc_canonize(p_penc);
1048 if (p != NULL)
1049 {
1050 vim_free(p_penc);
1051 p_penc = p;
1052 }
1053 else
1054 {
1055 // Ensure lower case and '-' for '_'
1056 for (s = p_penc; *s != NUL; s++)
1057 {
1058 if (*s == '_')
1059 *s = '-';
1060 else
1061 *s = TOLOWER_ASC(*s);
1062 }
1063 }
1064 }
1065#endif
1066
1067#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
1068 else if (varp == &p_imak)
1069 {
1070 if (!im_xim_isvalid_imactivate())
1071 errmsg = e_invarg;
1072 }
1073#endif
1074
1075#ifdef FEAT_KEYMAP
1076 else if (varp == &curbuf->b_p_keymap)
1077 {
1078 if (!valid_filetype(*varp))
1079 errmsg = e_invarg;
1080 else
1081 {
1082 int secure_save = secure;
1083
1084 // Reset the secure flag, since the value of 'keymap' has
1085 // been checked to be safe.
1086 secure = 0;
1087
1088 // load or unload key mapping tables
1089 errmsg = keymap_init();
1090
1091 secure = secure_save;
1092
1093 // Since we check the value, there is no need to set P_INSECURE,
1094 // even when the value comes from a modeline.
1095 *value_checked = TRUE;
1096 }
1097
1098 if (errmsg == NULL)
1099 {
1100 if (*curbuf->b_p_keymap != NUL)
1101 {
1102 // Installed a new keymap, switch on using it.
1103 curbuf->b_p_iminsert = B_IMODE_LMAP;
1104 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
1105 curbuf->b_p_imsearch = B_IMODE_LMAP;
1106 }
1107 else
1108 {
1109 // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
1110 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
1111 curbuf->b_p_iminsert = B_IMODE_NONE;
1112 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
1113 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
1114 }
1115 if ((opt_flags & OPT_LOCAL) == 0)
1116 {
1117 set_iminsert_global();
1118 set_imsearch_global();
1119 }
1120 status_redraw_curbuf();
1121 }
1122 }
1123#endif
1124
1125 // 'fileformat'
1126 else if (gvarp == &p_ff)
1127 {
1128 if (!curbuf->b_p_ma && !(opt_flags & OPT_GLOBAL))
1129 errmsg = e_modifiable;
1130 else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
1131 errmsg = e_invarg;
1132 else
1133 {
1134 // may also change 'textmode'
1135 if (get_fileformat(curbuf) == EOL_DOS)
1136 curbuf->b_p_tx = TRUE;
1137 else
1138 curbuf->b_p_tx = FALSE;
1139#ifdef FEAT_TITLE
1140 redraw_titles();
1141#endif
1142 // update flag in swap file
1143 ml_setflags(curbuf);
1144 // Redraw needed when switching to/from "mac": a CR in the text
1145 // will be displayed differently.
1146 if (get_fileformat(curbuf) == EOL_MAC || *oldval == 'm')
1147 redraw_curbuf_later(NOT_VALID);
1148 }
1149 }
1150
1151 // 'fileformats'
1152 else if (varp == &p_ffs)
1153 {
1154 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
1155 errmsg = e_invarg;
1156 else
1157 {
1158 // also change 'textauto'
1159 if (*p_ffs == NUL)
1160 p_ta = FALSE;
1161 else
1162 p_ta = TRUE;
1163 }
1164 }
1165
1166#if defined(FEAT_CRYPT)
1167 // 'cryptkey'
1168 else if (gvarp == &p_key)
1169 {
1170 // Make sure the ":set" command doesn't show the new value in the
1171 // history.
1172 remove_key_from_history();
1173
1174 if (STRCMP(curbuf->b_p_key, oldval) != 0)
1175 // Need to update the swapfile.
Bram Moolenaar76cb6832020-05-15 22:30:38 +02001176 {
Bram Moolenaardac13472019-09-16 21:06:21 +02001177 ml_set_crypt_key(curbuf, oldval,
1178 *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
Bram Moolenaar76cb6832020-05-15 22:30:38 +02001179 changed_internal();
1180 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001181 }
1182
1183 else if (gvarp == &p_cm)
1184 {
1185 if (opt_flags & OPT_LOCAL)
1186 p = curbuf->b_p_cm;
1187 else
1188 p = p_cm;
1189 if (check_opt_strings(p, p_cm_values, TRUE) != OK)
1190 errmsg = e_invarg;
1191 else if (crypt_self_test() == FAIL)
1192 errmsg = e_invarg;
1193 else
1194 {
1195 // When setting the global value to empty, make it "zip".
1196 if (*p_cm == NUL)
1197 {
1198 if (new_value_alloced)
1199 free_string_option(p_cm);
1200 p_cm = vim_strsave((char_u *)"zip");
1201 new_value_alloced = TRUE;
1202 }
1203 // When using ":set cm=name" the local value is going to be empty.
1204 // Do that here, otherwise the crypt functions will still use the
1205 // local value.
1206 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1207 {
1208 free_string_option(curbuf->b_p_cm);
1209 curbuf->b_p_cm = empty_option;
1210 }
1211
1212 // Need to update the swapfile when the effective method changed.
1213 // Set "s" to the effective old value, "p" to the effective new
1214 // method and compare.
1215 if ((opt_flags & OPT_LOCAL) && *oldval == NUL)
1216 s = p_cm; // was previously using the global value
1217 else
1218 s = oldval;
1219 if (*curbuf->b_p_cm == NUL)
1220 p = p_cm; // is now using the global value
1221 else
1222 p = curbuf->b_p_cm;
1223 if (STRCMP(s, p) != 0)
1224 ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1225
1226 // If the global value changes need to update the swapfile for all
1227 // buffers using that value.
1228 if ((opt_flags & OPT_GLOBAL) && STRCMP(p_cm, oldval) != 0)
1229 {
1230 buf_T *buf;
1231
1232 FOR_ALL_BUFFERS(buf)
1233 if (buf != curbuf && *buf->b_p_cm == NUL)
1234 ml_set_crypt_key(buf, buf->b_p_key, oldval);
1235 }
1236 }
1237 }
1238#endif
1239
1240 // 'matchpairs'
1241 else if (gvarp == &p_mps)
1242 {
1243 if (has_mbyte)
1244 {
1245 for (p = *varp; *p != NUL; ++p)
1246 {
1247 int x2 = -1;
1248 int x3 = -1;
1249
1250 if (*p != NUL)
1251 p += mb_ptr2len(p);
1252 if (*p != NUL)
1253 x2 = *p++;
1254 if (*p != NUL)
1255 {
1256 x3 = mb_ptr2char(p);
1257 p += mb_ptr2len(p);
1258 }
1259 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
1260 {
1261 errmsg = e_invarg;
1262 break;
1263 }
1264 if (*p == NUL)
1265 break;
1266 }
1267 }
1268 else
1269 {
1270 // Check for "x:y,x:y"
1271 for (p = *varp; *p != NUL; p += 4)
1272 {
1273 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
1274 {
1275 errmsg = e_invarg;
1276 break;
1277 }
1278 if (p[3] == NUL)
1279 break;
1280 }
1281 }
1282 }
1283
Bram Moolenaardac13472019-09-16 21:06:21 +02001284 // 'comments'
1285 else if (gvarp == &p_com)
1286 {
1287 for (s = *varp; *s; )
1288 {
1289 while (*s && *s != ':')
1290 {
1291 if (vim_strchr((char_u *)COM_ALL, *s) == NULL
1292 && !VIM_ISDIGIT(*s) && *s != '-')
1293 {
1294 errmsg = illegal_char(errbuf, *s);
1295 break;
1296 }
1297 ++s;
1298 }
1299 if (*s++ == NUL)
1300 errmsg = N_("E524: Missing colon");
1301 else if (*s == ',' || *s == NUL)
1302 errmsg = N_("E525: Zero length string");
1303 if (errmsg != NULL)
1304 break;
1305 while (*s && *s != ',')
1306 {
1307 if (*s == '\\' && s[1] != NUL)
1308 ++s;
1309 ++s;
1310 }
1311 s = skip_to_option_part(s);
1312 }
1313 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001314
Bram Moolenaareed9d462021-02-15 20:38:25 +01001315 // global 'listchars'
Bram Moolenaardac13472019-09-16 21:06:21 +02001316 else if (varp == &p_lcs)
1317 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001318 errmsg = set_chars_option(curwin, varp);
1319 if (errmsg == NULL)
1320 {
1321 tabpage_T *tp;
1322 win_T *wp;
1323
1324 // The current window is set to use the global 'listchars' value.
1325 // So clear the window-local value.
1326 if (!(opt_flags & OPT_GLOBAL))
1327 clear_string_option(&curwin->w_p_lcs);
1328 FOR_ALL_TAB_WINDOWS(tp, wp)
1329 {
1330 errmsg = set_chars_option(wp, &wp->w_p_lcs);
1331 if (errmsg)
1332 break;
1333 }
1334 redraw_all_later(NOT_VALID);
1335 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001336 }
1337
Bram Moolenaareed9d462021-02-15 20:38:25 +01001338 // local 'listchars'
1339 else if (varp == &curwin->w_p_lcs)
1340 errmsg = set_chars_option(curwin, varp);
1341
Bram Moolenaardac13472019-09-16 21:06:21 +02001342 // 'fillchars'
1343 else if (varp == &p_fcs)
1344 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001345 errmsg = set_chars_option(curwin, varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02001346 }
1347
1348#ifdef FEAT_CMDWIN
1349 // 'cedit'
1350 else if (varp == &p_cedit)
1351 {
1352 errmsg = check_cedit();
1353 }
1354#endif
1355
1356 // 'verbosefile'
1357 else if (varp == &p_vfile)
1358 {
1359 verbose_stop();
1360 if (*p_vfile != NUL && verbose_open() == FAIL)
1361 errmsg = e_invarg;
1362 }
1363
1364#ifdef FEAT_VIMINFO
1365 // 'viminfo'
1366 else if (varp == &p_viminfo)
1367 {
1368 for (s = p_viminfo; *s;)
1369 {
1370 // Check it's a valid character
1371 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
1372 {
1373 errmsg = illegal_char(errbuf, *s);
1374 break;
1375 }
1376 if (*s == 'n') // name is always last one
1377 break;
1378 else if (*s == 'r') // skip until next ','
1379 {
1380 while (*++s && *s != ',')
1381 ;
1382 }
1383 else if (*s == '%')
1384 {
1385 // optional number
1386 while (vim_isdigit(*++s))
1387 ;
1388 }
1389 else if (*s == '!' || *s == 'h' || *s == 'c')
1390 ++s; // no extra chars
1391 else // must have a number
1392 {
1393 while (vim_isdigit(*++s))
1394 ;
1395
1396 if (!VIM_ISDIGIT(*(s - 1)))
1397 {
1398 if (errbuf != NULL)
1399 {
1400 sprintf(errbuf, _("E526: Missing number after <%s>"),
1401 transchar_byte(*(s - 1)));
1402 errmsg = errbuf;
1403 }
1404 else
1405 errmsg = "";
1406 break;
1407 }
1408 }
1409 if (*s == ',')
1410 ++s;
1411 else if (*s)
1412 {
1413 if (errbuf != NULL)
1414 errmsg = N_("E527: Missing comma");
1415 else
1416 errmsg = "";
1417 break;
1418 }
1419 }
1420 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
1421 errmsg = N_("E528: Must specify a ' value");
1422 }
1423#endif // FEAT_VIMINFO
1424
1425 // terminal options
1426 else if (istermoption_idx(opt_idx) && full_screen)
1427 {
1428 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
1429 if (varp == &T_CCO)
1430 {
1431 int colors = atoi((char *)T_CCO);
1432
1433 // Only reinitialize colors if t_Co value has really changed to
1434 // avoid expensive reload of colorscheme if t_Co is set to the
1435 // same value multiple times.
1436 if (colors != t_colors)
1437 {
1438 t_colors = colors;
1439 if (t_colors <= 1)
1440 {
1441 if (new_value_alloced)
1442 vim_free(T_CCO);
1443 T_CCO = empty_option;
1444 }
1445#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1446 if (is_term_win32())
1447 {
1448 swap_tcap();
1449 did_swaptcap = TRUE;
1450 }
1451#endif
1452 // We now have a different color setup, initialize it again.
1453 init_highlight(TRUE, FALSE);
1454 }
1455 }
1456 ttest(FALSE);
1457 if (varp == &T_ME)
1458 {
1459 out_str(T_ME);
1460 redraw_later(CLEAR);
1461#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
1462 // Since t_me has been set, this probably means that the user
1463 // wants to use this as default colors. Need to reset default
1464 // background/foreground colors.
1465# ifdef VIMDLL
1466 if (!gui.in_use && !gui.starting)
1467# endif
1468 mch_set_normal_colors();
1469#endif
1470 }
1471 if (varp == &T_BE && termcap_active)
1472 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02001473#ifdef FEAT_JOB_CHANNEL
1474 ch_log_output = TRUE;
1475#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02001476 if (*T_BE == NUL)
1477 // When clearing t_BE we assume the user no longer wants
1478 // bracketed paste, thus disable it by writing t_BD.
1479 out_str(T_BD);
1480 else
1481 out_str(T_BE);
1482 }
1483 }
1484
1485#ifdef FEAT_LINEBREAK
1486 // 'showbreak'
Bram Moolenaaree857022019-11-09 23:26:40 +01001487 else if (gvarp == &p_sbr)
Bram Moolenaardac13472019-09-16 21:06:21 +02001488 {
Bram Moolenaaree857022019-11-09 23:26:40 +01001489 for (s = *varp; *s; )
Bram Moolenaardac13472019-09-16 21:06:21 +02001490 {
1491 if (ptr2cells(s) != 1)
Bram Moolenaaree857022019-11-09 23:26:40 +01001492 errmsg = N_("E595: 'showbreak' contains unprintable or wide character");
Bram Moolenaardac13472019-09-16 21:06:21 +02001493 MB_PTR_ADV(s);
1494 }
1495 }
1496#endif
1497
1498#ifdef FEAT_GUI
1499 // 'guifont'
1500 else if (varp == &p_guifont)
1501 {
1502 if (gui.in_use)
1503 {
1504 p = p_guifont;
1505# if defined(FEAT_GUI_GTK)
1506 // Put up a font dialog and let the user select a new value.
1507 // If this is cancelled go back to the old value but don't
1508 // give an error message.
1509 if (STRCMP(p, "*") == 0)
1510 {
1511 p = gui_mch_font_dialog(oldval);
1512
1513 if (new_value_alloced)
1514 free_string_option(p_guifont);
1515
1516 p_guifont = (p != NULL) ? p : vim_strsave(oldval);
1517 new_value_alloced = TRUE;
1518 }
1519# endif
1520 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
1521 {
1522# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
1523 if (STRCMP(p_guifont, "*") == 0)
1524 {
1525 // Dialog was cancelled: Keep the old value without giving
1526 // an error message.
1527 if (new_value_alloced)
1528 free_string_option(p_guifont);
1529 p_guifont = vim_strsave(oldval);
1530 new_value_alloced = TRUE;
1531 }
1532 else
1533# endif
1534 errmsg = N_("E596: Invalid font(s)");
1535 }
1536 }
1537 redraw_gui_only = TRUE;
1538 }
1539# ifdef FEAT_XFONTSET
1540 else if (varp == &p_guifontset)
1541 {
1542 if (STRCMP(p_guifontset, "*") == 0)
1543 errmsg = N_("E597: can't select fontset");
1544 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
1545 errmsg = N_("E598: Invalid fontset");
1546 redraw_gui_only = TRUE;
1547 }
1548# endif
1549 else if (varp == &p_guifontwide)
1550 {
1551 if (STRCMP(p_guifontwide, "*") == 0)
1552 errmsg = N_("E533: can't select wide font");
1553 else if (gui_get_wide_font() == FAIL)
1554 errmsg = N_("E534: Invalid wide font");
1555 redraw_gui_only = TRUE;
1556 }
1557#endif
1558
1559#ifdef CURSOR_SHAPE
1560 // 'guicursor'
1561 else if (varp == &p_guicursor)
1562 errmsg = parse_shape_opt(SHAPE_CURSOR);
1563#endif
1564
1565#ifdef FEAT_MOUSESHAPE
1566 // 'mouseshape'
1567 else if (varp == &p_mouseshape)
1568 {
1569 errmsg = parse_shape_opt(SHAPE_MOUSE);
1570 update_mouseshape(-1);
1571 }
1572#endif
1573
1574#ifdef FEAT_PRINTER
1575 else if (varp == &p_popt)
1576 errmsg = parse_printoptions();
1577# if defined(FEAT_POSTSCRIPT)
1578 else if (varp == &p_pmfn)
1579 errmsg = parse_printmbfont();
1580# endif
1581#endif
1582
1583#ifdef FEAT_LANGMAP
1584 // 'langmap'
1585 else if (varp == &p_langmap)
1586 langmap_set();
1587#endif
1588
1589#ifdef FEAT_LINEBREAK
1590 // 'breakat'
1591 else if (varp == &p_breakat)
1592 fill_breakat_flags();
1593#endif
1594
1595#ifdef FEAT_TITLE
1596 // 'titlestring' and 'iconstring'
1597 else if (varp == &p_titlestring || varp == &p_iconstring)
1598 {
1599# ifdef FEAT_STL_OPT
1600 int flagval = (varp == &p_titlestring) ? STL_IN_TITLE : STL_IN_ICON;
1601
1602 // NULL => statusline syntax
1603 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
1604 stl_syntax |= flagval;
1605 else
1606 stl_syntax &= ~flagval;
1607# endif
1608 did_set_title();
1609 }
1610#endif
1611
1612#ifdef FEAT_GUI
1613 // 'guioptions'
1614 else if (varp == &p_go)
1615 {
1616 gui_init_which_components(oldval);
1617 redraw_gui_only = TRUE;
1618 }
1619#endif
1620
1621#if defined(FEAT_GUI_TABLINE)
1622 // 'guitablabel'
1623 else if (varp == &p_gtl)
1624 {
1625 redraw_tabline = TRUE;
1626 redraw_gui_only = TRUE;
1627 }
1628 // 'guitabtooltip'
1629 else if (varp == &p_gtt)
1630 {
1631 redraw_gui_only = TRUE;
1632 }
1633#endif
1634
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001635#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +02001636 // 'ttymouse'
1637 else if (varp == &p_ttym)
1638 {
1639 // Switch the mouse off before changing the escape sequences used for
1640 // that.
1641 mch_setmouse(FALSE);
1642 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
1643 errmsg = e_invarg;
1644 else
1645 check_mouse_termcode();
1646 if (termcap_active)
1647 setmouse(); // may switch it on again
1648 }
1649#endif
1650
1651 // 'selection'
1652 else if (varp == &p_sel)
1653 {
1654 if (*p_sel == NUL
1655 || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
1656 errmsg = e_invarg;
1657 }
1658
1659 // 'selectmode'
1660 else if (varp == &p_slm)
1661 {
1662 if (check_opt_strings(p_slm, p_slm_values, TRUE) != OK)
1663 errmsg = e_invarg;
1664 }
1665
1666#ifdef FEAT_BROWSE
1667 // 'browsedir'
1668 else if (varp == &p_bsdir)
1669 {
1670 if (check_opt_strings(p_bsdir, p_bsdir_values, FALSE) != OK
1671 && !mch_isdir(p_bsdir))
1672 errmsg = e_invarg;
1673 }
1674#endif
1675
1676 // 'keymodel'
1677 else if (varp == &p_km)
1678 {
1679 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
1680 errmsg = e_invarg;
1681 else
1682 {
1683 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
1684 km_startsel = (vim_strchr(p_km, 'a') != NULL);
1685 }
1686 }
1687
1688 // 'mousemodel'
1689 else if (varp == &p_mousem)
1690 {
1691 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
1692 errmsg = e_invarg;
1693#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
1694 else if (*p_mousem != *oldval)
1695 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
1696 // to create or delete the popup menus.
1697 gui_motif_update_mousemodel(root_menu);
1698#endif
1699 }
1700
1701 // 'switchbuf'
1702 else if (varp == &p_swb)
1703 {
1704 if (opt_strings_flags(p_swb, p_swb_values, &swb_flags, TRUE) != OK)
1705 errmsg = e_invarg;
1706 }
1707
1708 // 'debug'
1709 else if (varp == &p_debug)
1710 {
1711 if (check_opt_strings(p_debug, p_debug_values, TRUE) != OK)
1712 errmsg = e_invarg;
1713 }
1714
1715 // 'display'
1716 else if (varp == &p_dy)
1717 {
1718 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
1719 errmsg = e_invarg;
1720 else
1721 (void)init_chartab();
1722
1723 }
1724
1725 // 'eadirection'
1726 else if (varp == &p_ead)
1727 {
1728 if (check_opt_strings(p_ead, p_ead_values, FALSE) != OK)
1729 errmsg = e_invarg;
1730 }
1731
1732#ifdef FEAT_CLIPBOARD
1733 // 'clipboard'
1734 else if (varp == &p_cb)
1735 errmsg = check_clipboard_option();
1736#endif
1737
1738#ifdef FEAT_SPELL
1739 // When 'spelllang' or 'spellfile' is set and there is a window for this
1740 // buffer in which 'spell' is set load the wordlists.
1741 else if (varp == &(curwin->w_s->b_p_spl)
1742 || varp == &(curwin->w_s->b_p_spf))
1743 {
1744 int is_spellfile = varp == &(curwin->w_s->b_p_spf);
1745
1746 if ((is_spellfile && !valid_spellfile(*varp))
Bram Moolenaarf154f3a2020-06-08 18:54:49 +02001747 || (!is_spellfile && !valid_spelllang(*varp)))
Bram Moolenaardac13472019-09-16 21:06:21 +02001748 errmsg = e_invarg;
1749 else
1750 errmsg = did_set_spell_option(is_spellfile);
1751 }
1752 // When 'spellcapcheck' is set compile the regexp program.
1753 else if (varp == &(curwin->w_s->b_p_spc))
1754 {
1755 errmsg = compile_cap_prog(curwin->w_s);
1756 }
Bram Moolenaar362b44b2020-06-10 21:47:00 +02001757 // 'spelloptions'
1758 else if (varp == &(curwin->w_s->b_p_spo))
1759 {
1760 if (**varp != NUL && STRCMP("camel", *varp) != 0)
1761 errmsg = e_invarg;
1762 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001763 // 'spellsuggest'
1764 else if (varp == &p_sps)
1765 {
1766 if (spell_check_sps() != OK)
1767 errmsg = e_invarg;
1768 }
1769 // 'mkspellmem'
1770 else if (varp == &p_msm)
1771 {
1772 if (spell_check_msm() != OK)
1773 errmsg = e_invarg;
1774 }
1775#endif
1776
1777 // When 'bufhidden' is set, check for valid value.
1778 else if (gvarp == &p_bh)
1779 {
1780 if (check_opt_strings(curbuf->b_p_bh, p_bufhidden_values, FALSE) != OK)
1781 errmsg = e_invarg;
1782 }
1783
1784 // When 'buftype' is set, check for valid value.
1785 else if (gvarp == &p_bt)
1786 {
1787 if (check_opt_strings(curbuf->b_p_bt, p_buftype_values, FALSE) != OK)
1788 errmsg = e_invarg;
1789 else
1790 {
1791 if (curwin->w_status_height)
1792 {
1793 curwin->w_redr_status = TRUE;
1794 redraw_later(VALID);
1795 }
1796 curbuf->b_help = (curbuf->b_p_bt[0] == 'h');
1797#ifdef FEAT_TITLE
1798 redraw_titles();
1799#endif
1800 }
1801 }
1802
1803#ifdef FEAT_STL_OPT
1804 // 'statusline' or 'rulerformat'
1805 else if (gvarp == &p_stl || varp == &p_ruf)
1806 {
1807 int wid;
1808
1809 if (varp == &p_ruf) // reset ru_wid first
1810 ru_wid = 0;
1811 s = *varp;
1812 if (varp == &p_ruf && *s == '%')
1813 {
1814 // set ru_wid if 'ruf' starts with "%99("
1815 if (*++s == '-') // ignore a '-'
1816 s++;
1817 wid = getdigits(&s);
1818 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
1819 ru_wid = wid;
1820 else
1821 errmsg = check_stl_option(p_ruf);
1822 }
1823 // check 'statusline' only if it doesn't start with "%!"
1824 else if (varp == &p_ruf || s[0] != '%' || s[1] != '!')
1825 errmsg = check_stl_option(s);
1826 if (varp == &p_ruf && errmsg == NULL)
1827 comp_col();
1828 }
1829#endif
1830
1831 // check if it is a valid value for 'complete' -- Acevedo
1832 else if (gvarp == &p_cpt)
1833 {
1834 for (s = *varp; *s;)
1835 {
1836 while (*s == ',' || *s == ' ')
1837 s++;
1838 if (!*s)
1839 break;
1840 if (vim_strchr((char_u *)".wbuksid]tU", *s) == NULL)
1841 {
1842 errmsg = illegal_char(errbuf, *s);
1843 break;
1844 }
1845 if (*++s != NUL && *s != ',' && *s != ' ')
1846 {
1847 if (s[-1] == 'k' || s[-1] == 's')
1848 {
1849 // skip optional filename after 'k' and 's'
1850 while (*s && *s != ',' && *s != ' ')
1851 {
1852 if (*s == '\\' && s[1] != NUL)
1853 ++s;
1854 ++s;
1855 }
1856 }
1857 else
1858 {
1859 if (errbuf != NULL)
1860 {
1861 sprintf((char *)errbuf,
1862 _("E535: Illegal character after <%c>"),
1863 *--s);
1864 errmsg = errbuf;
1865 }
1866 else
1867 errmsg = "";
1868 break;
1869 }
1870 }
1871 }
1872 }
1873
1874 // 'completeopt'
1875 else if (varp == &p_cot)
1876 {
1877 if (check_opt_strings(p_cot, p_cot_values, TRUE) != OK)
1878 errmsg = e_invarg;
1879 else
1880 completeopt_was_set();
1881 }
1882
1883#ifdef BACKSLASH_IN_FILENAME
1884 // 'completeslash'
1885 else if (gvarp == &p_csl)
1886 {
1887 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1888 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
1889 errmsg = e_invarg;
1890 }
1891#endif
1892
1893#ifdef FEAT_SIGNS
1894 // 'signcolumn'
1895 else if (varp == &curwin->w_p_scl)
1896 {
1897 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
1898 errmsg = e_invarg;
1899 // When changing the 'signcolumn' to or from 'number', recompute the
1900 // width of the number column if 'number' or 'relativenumber' is set.
1901 if (((*oldval == 'n' && *(oldval + 1) == 'u')
1902 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
1903 && (curwin->w_p_nu || curwin->w_p_rnu))
1904 curwin->w_nrwidth_line_count = 0;
1905 }
1906#endif
1907
1908
1909#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
1910 // 'toolbar'
1911 else if (varp == &p_toolbar)
1912 {
1913 if (opt_strings_flags(p_toolbar, p_toolbar_values,
1914 &toolbar_flags, TRUE) != OK)
1915 errmsg = e_invarg;
1916 else
1917 {
1918 out_flush();
1919 gui_mch_show_toolbar((toolbar_flags &
1920 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
1921 }
1922 }
1923#endif
1924
1925#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
1926 // 'toolbariconsize': GTK+ 2 only
1927 else if (varp == &p_tbis)
1928 {
1929 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
1930 errmsg = e_invarg;
1931 else
1932 {
1933 out_flush();
1934 gui_mch_show_toolbar((toolbar_flags &
1935 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
1936 }
1937 }
1938#endif
1939
1940 // 'pastetoggle': translate key codes like in a mapping
1941 else if (varp == &p_pt)
1942 {
1943 if (*p_pt)
1944 {
Bram Moolenaar1e7b52a2019-10-13 16:59:08 +02001945 (void)replace_termcodes(p_pt, &p,
1946 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
Bram Moolenaardac13472019-09-16 21:06:21 +02001947 if (p != NULL)
1948 {
1949 if (new_value_alloced)
1950 free_string_option(p_pt);
1951 p_pt = p;
1952 new_value_alloced = TRUE;
1953 }
1954 }
1955 }
1956
1957 // 'backspace'
1958 else if (varp == &p_bs)
1959 {
1960 if (VIM_ISDIGIT(*p_bs))
1961 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02001962 if (*p_bs > '3' || p_bs[1] != NUL)
Bram Moolenaardac13472019-09-16 21:06:21 +02001963 errmsg = e_invarg;
1964 }
1965 else if (check_opt_strings(p_bs, p_bs_values, TRUE) != OK)
1966 errmsg = e_invarg;
1967 }
1968 else if (varp == &p_bo)
1969 {
1970 if (opt_strings_flags(p_bo, p_bo_values, &bo_flags, TRUE) != OK)
1971 errmsg = e_invarg;
1972 }
1973
1974 // 'tagcase'
1975 else if (gvarp == &p_tc)
1976 {
1977 unsigned int *flags;
1978
1979 if (opt_flags & OPT_LOCAL)
1980 {
1981 p = curbuf->b_p_tc;
1982 flags = &curbuf->b_tc_flags;
1983 }
1984 else
1985 {
1986 p = p_tc;
1987 flags = &tc_flags;
1988 }
1989
1990 if ((opt_flags & OPT_LOCAL) && *p == NUL)
1991 // make the local value empty: use the global value
1992 *flags = 0;
1993 else if (*p == NUL
1994 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
1995 errmsg = e_invarg;
1996 }
1997
1998 // 'casemap'
1999 else if (varp == &p_cmp)
2000 {
2001 if (opt_strings_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE) != OK)
2002 errmsg = e_invarg;
2003 }
2004
2005#ifdef FEAT_DIFF
2006 // 'diffopt'
2007 else if (varp == &p_dip)
2008 {
2009 if (diffopt_changed() == FAIL)
2010 errmsg = e_invarg;
2011 }
2012#endif
2013
2014#ifdef FEAT_FOLDING
2015 // 'foldmethod'
2016 else if (gvarp == &curwin->w_allbuf_opt.wo_fdm)
2017 {
2018 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK
2019 || *curwin->w_p_fdm == NUL)
2020 errmsg = e_invarg;
2021 else
2022 {
2023 foldUpdateAll(curwin);
2024 if (foldmethodIsDiff(curwin))
2025 newFoldLevel();
2026 }
2027 }
2028# ifdef FEAT_EVAL
2029 // 'foldexpr'
2030 else if (varp == &curwin->w_p_fde)
2031 {
2032 if (foldmethodIsExpr(curwin))
2033 foldUpdateAll(curwin);
2034 }
2035# endif
2036 // 'foldmarker'
2037 else if (gvarp == &curwin->w_allbuf_opt.wo_fmr)
2038 {
2039 p = vim_strchr(*varp, ',');
2040 if (p == NULL)
2041 errmsg = N_("E536: comma required");
2042 else if (p == *varp || p[1] == NUL)
2043 errmsg = e_invarg;
2044 else if (foldmethodIsMarker(curwin))
2045 foldUpdateAll(curwin);
2046 }
2047 // 'commentstring'
2048 else if (gvarp == &p_cms)
2049 {
2050 if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
2051 errmsg = N_("E537: 'commentstring' must be empty or contain %s");
2052 }
2053 // 'foldopen'
2054 else if (varp == &p_fdo)
2055 {
2056 if (opt_strings_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE) != OK)
2057 errmsg = e_invarg;
2058 }
2059 // 'foldclose'
2060 else if (varp == &p_fcl)
2061 {
2062 if (check_opt_strings(p_fcl, p_fcl_values, TRUE) != OK)
2063 errmsg = e_invarg;
2064 }
2065 // 'foldignore'
2066 else if (gvarp == &curwin->w_allbuf_opt.wo_fdi)
2067 {
2068 if (foldmethodIsIndent(curwin))
2069 foldUpdateAll(curwin);
2070 }
2071#endif
2072
2073 // 'virtualedit'
2074 else if (varp == &p_ve)
2075 {
2076 if (opt_strings_flags(p_ve, p_ve_values, &ve_flags, TRUE) != OK)
2077 errmsg = e_invarg;
2078 else if (STRCMP(p_ve, oldval) != 0)
2079 {
2080 // Recompute cursor position in case the new 've' setting
2081 // changes something.
2082 validate_virtcol();
2083 coladvance(curwin->w_virtcol);
2084 }
2085 }
2086
2087#if defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)
2088 else if (varp == &p_csqf)
2089 {
2090 if (p_csqf != NULL)
2091 {
2092 p = p_csqf;
2093 while (*p != NUL)
2094 {
2095 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
2096 || p[1] == NUL
2097 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
2098 || (p[2] != NUL && p[2] != ','))
2099 {
2100 errmsg = e_invarg;
2101 break;
2102 }
2103 else if (p[2] == NUL)
2104 break;
2105 else
2106 p += 3;
2107 }
2108 }
2109 }
2110#endif
2111
2112#ifdef FEAT_CINDENT
2113 // 'cinoptions'
2114 else if (gvarp == &p_cino)
2115 {
2116 // TODO: recognize errors
2117 parse_cino(curbuf);
2118 }
2119#endif
2120
2121#if defined(FEAT_RENDER_OPTIONS)
2122 // 'renderoptions'
2123 else if (varp == &p_rop)
2124 {
2125 if (!gui_mch_set_rendering_options(p_rop))
2126 errmsg = e_invarg;
2127 }
2128#endif
2129
2130 else if (gvarp == &p_ft)
2131 {
2132 if (!valid_filetype(*varp))
2133 errmsg = e_invarg;
2134 else
2135 {
2136 value_changed = STRCMP(oldval, *varp) != 0;
2137
2138 // Since we check the value, there is no need to set P_INSECURE,
2139 // even when the value comes from a modeline.
2140 *value_checked = TRUE;
2141 }
2142 }
2143
2144#ifdef FEAT_SYN_HL
2145 else if (gvarp == &p_syn)
2146 {
2147 if (!valid_filetype(*varp))
2148 errmsg = e_invarg;
2149 else
2150 {
2151 value_changed = STRCMP(oldval, *varp) != 0;
2152
2153 // Since we check the value, there is no need to set P_INSECURE,
2154 // even when the value comes from a modeline.
2155 *value_checked = TRUE;
2156 }
2157 }
2158#endif
2159
2160#ifdef FEAT_TERMINAL
2161 // 'termwinkey'
2162 else if (varp == &curwin->w_p_twk)
2163 {
2164 if (*curwin->w_p_twk != NUL
2165 && string_to_key(curwin->w_p_twk, TRUE) == 0)
2166 errmsg = e_invarg;
2167 }
2168 // 'termwinsize'
2169 else if (varp == &curwin->w_p_tws)
2170 {
2171 if (*curwin->w_p_tws != NUL)
2172 {
2173 p = skipdigits(curwin->w_p_tws);
2174 if (p == curwin->w_p_tws
2175 || (*p != 'x' && *p != '*')
2176 || *skipdigits(p + 1) != NUL)
2177 errmsg = e_invarg;
2178 }
2179 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002180 // 'wincolor'
2181 else if (varp == &curwin->w_p_wcr)
2182 {
2183 if (curwin->w_buffer->b_term != NULL)
Bram Moolenaarad431992021-05-03 20:40:38 +02002184 term_update_colors(curwin->w_buffer->b_term);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002185 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002186# if defined(MSWIN)
2187 // 'termwintype'
2188 else if (varp == &p_twt)
2189 {
2190 if (check_opt_strings(*varp, p_twt_values, FALSE) != OK)
2191 errmsg = e_invarg;
2192 }
2193# endif
2194#endif
2195
2196#ifdef FEAT_VARTABS
2197 // 'varsofttabstop'
2198 else if (varp == &(curbuf->b_p_vsts))
2199 {
2200 char_u *cp;
2201
2202 if (!(*varp)[0] || ((*varp)[0] == '0' && !(*varp)[1]))
2203 {
2204 if (curbuf->b_p_vsts_array)
2205 {
2206 vim_free(curbuf->b_p_vsts_array);
2207 curbuf->b_p_vsts_array = 0;
2208 }
2209 }
2210 else
2211 {
2212 for (cp = *varp; *cp; ++cp)
2213 {
2214 if (vim_isdigit(*cp))
2215 continue;
2216 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
2217 continue;
2218 errmsg = e_invarg;
2219 break;
2220 }
2221 if (errmsg == NULL)
2222 {
2223 int *oldarray = curbuf->b_p_vsts_array;
2224 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)))
2225 {
2226 if (oldarray)
2227 vim_free(oldarray);
2228 }
2229 else
2230 errmsg = e_invarg;
2231 }
2232 }
2233 }
2234
2235 // 'vartabstop'
2236 else if (varp == &(curbuf->b_p_vts))
2237 {
2238 char_u *cp;
2239
2240 if (!(*varp)[0] || ((*varp)[0] == '0' && !(*varp)[1]))
2241 {
2242 if (curbuf->b_p_vts_array)
2243 {
2244 vim_free(curbuf->b_p_vts_array);
2245 curbuf->b_p_vts_array = NULL;
2246 }
2247 }
2248 else
2249 {
2250 for (cp = *varp; *cp; ++cp)
2251 {
2252 if (vim_isdigit(*cp))
2253 continue;
2254 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
2255 continue;
2256 errmsg = e_invarg;
2257 break;
2258 }
2259 if (errmsg == NULL)
2260 {
2261 int *oldarray = curbuf->b_p_vts_array;
2262
2263 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)))
2264 {
2265 vim_free(oldarray);
2266#ifdef FEAT_FOLDING
2267 if (foldmethodIsIndent(curwin))
2268 foldUpdateAll(curwin);
2269#endif
2270 }
2271 else
2272 errmsg = e_invarg;
2273 }
2274 }
2275 }
2276#endif
2277
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002278#ifdef FEAT_PROP_POPUP
Bram Moolenaardac13472019-09-16 21:06:21 +02002279 // 'previewpopup'
2280 else if (varp == &p_pvp)
2281 {
2282 if (parse_previewpopup(NULL) == FAIL)
2283 errmsg = e_invarg;
2284 }
2285# ifdef FEAT_QUICKFIX
2286 // 'completepopup'
2287 else if (varp == &p_cpp)
2288 {
2289 if (parse_completepopup(NULL) == FAIL)
2290 errmsg = e_invarg;
Bram Moolenaar447bfba2020-07-18 16:07:16 +02002291 else
2292 popup_close_info();
Bram Moolenaardac13472019-09-16 21:06:21 +02002293 }
2294# endif
2295#endif
2296
Bram Moolenaard43906d2020-07-20 21:31:32 +02002297#ifdef FEAT_QUICKFIX
2298 else if (varp == &p_qftf)
2299 {
2300 if (qf_process_qftf_option() == FALSE)
2301 errmsg = e_invarg;
2302 }
2303#endif
2304
Bram Moolenaardac13472019-09-16 21:06:21 +02002305 // Options that are a list of flags.
2306 else
2307 {
2308 p = NULL;
2309 if (varp == &p_ww) // 'whichwrap'
2310 p = (char_u *)WW_ALL;
2311 if (varp == &p_shm) // 'shortmess'
2312 p = (char_u *)SHM_ALL;
2313 else if (varp == &(p_cpo)) // 'cpoptions'
2314 p = (char_u *)CPO_ALL;
2315 else if (varp == &(curbuf->b_p_fo)) // 'formatoptions'
2316 p = (char_u *)FO_ALL;
2317#ifdef FEAT_CONCEAL
2318 else if (varp == &curwin->w_p_cocu) // 'concealcursor'
2319 p = (char_u *)COCU_ALL;
2320#endif
2321 else if (varp == &p_mouse) // 'mouse'
2322 {
Bram Moolenaardac13472019-09-16 21:06:21 +02002323 p = (char_u *)MOUSE_ALL;
Bram Moolenaardac13472019-09-16 21:06:21 +02002324 }
2325#if defined(FEAT_GUI)
2326 else if (varp == &p_go) // 'guioptions'
2327 p = (char_u *)GO_ALL;
2328#endif
2329 if (p != NULL)
2330 {
2331 for (s = *varp; *s; ++s)
2332 if (vim_strchr(p, *s) == NULL)
2333 {
2334 errmsg = illegal_char(errbuf, *s);
2335 break;
2336 }
2337 }
2338 }
2339
2340 // If error detected, restore the previous value.
2341 if (errmsg != NULL)
2342 {
2343 if (new_value_alloced)
2344 free_string_option(*varp);
2345 *varp = oldval;
2346 // When resetting some values, need to act on it.
2347 if (did_chartab)
2348 (void)init_chartab();
2349 if (varp == &p_hl)
2350 (void)highlight_changed();
2351 }
2352 else
2353 {
2354#ifdef FEAT_EVAL
2355 // Remember where the option was set.
2356 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
2357#endif
2358 // Free string options that are in allocated memory.
2359 // Use "free_oldval", because recursiveness may change the flags under
2360 // our fingers (esp. init_highlight()).
2361 if (free_oldval)
2362 free_string_option(oldval);
2363 if (new_value_alloced)
2364 set_option_flag(opt_idx, P_ALLOCED);
2365 else
2366 clear_option_flag(opt_idx, P_ALLOCED);
2367
2368 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
2369 && is_global_local_option(opt_idx))
2370 {
2371 // global option with local value set to use global value; free
2372 // the local value and make it empty
2373 p = get_option_varp_scope(opt_idx, OPT_LOCAL);
2374 free_string_option(*(char_u **)p);
2375 *(char_u **)p = empty_option;
2376 }
2377
2378 // May set global value for local option.
2379 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
2380 set_string_option_global(opt_idx, varp);
2381
2382 // Trigger the autocommand only after setting the flags.
2383#ifdef FEAT_SYN_HL
2384 // When 'syntax' is set, load the syntax of that name
2385 if (varp == &(curbuf->b_p_syn))
2386 {
2387 static int syn_recursive = 0;
2388
2389 ++syn_recursive;
2390 // Only pass TRUE for "force" when the value changed or not used
2391 // recursively, to avoid endless recurrence.
2392 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
2393 value_changed || syn_recursive == 1, curbuf);
2394 curbuf->b_flags |= BF_SYN_SET;
2395 --syn_recursive;
2396 }
2397#endif
2398 else if (varp == &(curbuf->b_p_ft))
2399 {
2400 // 'filetype' is set, trigger the FileType autocommand.
2401 // Skip this when called from a modeline and the filetype was
2402 // already set to this value.
2403 if (!(opt_flags & OPT_MODELINE) || value_changed)
2404 {
2405 static int ft_recursive = 0;
2406 int secure_save = secure;
2407
2408 // Reset the secure flag, since the value of 'filetype' has
2409 // been checked to be safe.
2410 secure = 0;
2411
2412 ++ft_recursive;
2413 did_filetype = TRUE;
2414 // Only pass TRUE for "force" when the value changed or not
2415 // used recursively, to avoid endless recurrence.
2416 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2417 value_changed || ft_recursive == 1, curbuf);
2418 --ft_recursive;
2419 // Just in case the old "curbuf" is now invalid.
2420 if (varp != &(curbuf->b_p_ft))
2421 varp = NULL;
2422
2423 secure = secure_save;
2424 }
2425 }
2426#ifdef FEAT_SPELL
2427 if (varp == &(curwin->w_s->b_p_spl))
2428 {
2429 char_u fname[200];
2430 char_u *q = curwin->w_s->b_p_spl;
2431
2432 // Skip the first name if it is "cjk".
2433 if (STRNCMP(q, "cjk,", 4) == 0)
2434 q += 4;
2435
2436 // Source the spell/LANG.vim in 'runtimepath'.
2437 // They could set 'spellcapcheck' depending on the language.
2438 // Use the first name in 'spelllang' up to '_region' or
2439 // '.encoding'.
2440 for (p = q; *p != NUL; ++p)
2441 if (!ASCII_ISALNUM(*p) && *p != '-')
2442 break;
2443 if (p > q)
2444 {
2445 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
2446 (int)(p - q), q);
2447 source_runtime(fname, DIP_ALL);
2448 }
2449 }
2450#endif
2451 }
2452
Bram Moolenaardac13472019-09-16 21:06:21 +02002453 if (varp == &p_mouse)
2454 {
Bram Moolenaardac13472019-09-16 21:06:21 +02002455 if (*p_mouse == NUL)
2456 mch_setmouse(FALSE); // switch mouse off
2457 else
Bram Moolenaardac13472019-09-16 21:06:21 +02002458 setmouse(); // in case 'mouse' changed
2459 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002460
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002461#if defined(FEAT_LUA) || defined(PROTO)
2462 if (varp == &p_rtp)
2463 update_package_paths_in_lua();
2464#endif
2465
Bram Moolenaardac13472019-09-16 21:06:21 +02002466 if (curwin->w_curswant != MAXCOL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002467 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02002468 curwin->w_set_curswant = TRUE;
2469
Bram Moolenaar37294bd2021-03-10 13:40:08 +01002470 if ((opt_flags & OPT_NO_REDRAW) == 0)
2471 {
Bram Moolenaardac13472019-09-16 21:06:21 +02002472#ifdef FEAT_GUI
Bram Moolenaar37294bd2021-03-10 13:40:08 +01002473 // check redraw when it's not a GUI option or the GUI is active.
2474 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02002475#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01002476 check_redraw(get_option_flags(opt_idx));
2477 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002478
2479#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2480 if (did_swaptcap)
2481 {
2482 set_termname((char_u *)"win32");
2483 init_highlight(TRUE, FALSE);
2484 }
2485#endif
2486
2487 return errmsg;
2488}
2489
2490/*
2491 * Check an option that can be a range of string values.
2492 *
2493 * Return OK for correct value, FAIL otherwise.
2494 * Empty is always OK.
2495 */
2496 static int
2497check_opt_strings(
2498 char_u *val,
2499 char **values,
2500 int list) // when TRUE: accept a list of values
2501{
2502 return opt_strings_flags(val, values, NULL, list);
2503}
2504
2505/*
2506 * Handle an option that can be a range of string values.
2507 * Set a flag in "*flagp" for each string present.
2508 *
2509 * Return OK for correct value, FAIL otherwise.
2510 * Empty is always OK.
2511 */
2512 static int
2513opt_strings_flags(
2514 char_u *val, // new value
2515 char **values, // array of valid string values
2516 unsigned *flagp,
2517 int list) // when TRUE: accept a list of values
2518{
2519 int i;
2520 int len;
2521 unsigned new_flags = 0;
2522
2523 while (*val)
2524 {
2525 for (i = 0; ; ++i)
2526 {
2527 if (values[i] == NULL) // val not found in values[]
2528 return FAIL;
2529
2530 len = (int)STRLEN(values[i]);
2531 if (STRNCMP(values[i], val, len) == 0
2532 && ((list && val[len] == ',') || val[len] == NUL))
2533 {
2534 val += len + (val[len] == ',');
2535 new_flags |= (1 << i);
2536 break; // check next item in val list
2537 }
2538 }
2539 }
2540 if (flagp != NULL)
2541 *flagp = new_flags;
2542
2543 return OK;
2544}
2545
2546/*
2547 * return OK if "p" is a valid fileformat name, FAIL otherwise.
2548 */
2549 int
2550check_ff_value(char_u *p)
2551{
2552 return check_opt_strings(p, p_ff_values, FALSE);
2553}