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