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