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