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