blob: bced92d6dd0a80c1470d06fa807f7b5998c2d689 [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);
Yegappan Lakshmanan160e9942021-10-16 15:41:29 +0100274 check_string_option(&buf->b_p_thsfu);
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)
Bram Moolenaardac13472019-09-16 21:06:21 +0200874 errmsg = _("E835: 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 {
884 errmsg = _("E834: Conflicts with value of 'listchars'");
885 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)
1334 {
1335 errmsg = set_chars_option(wp, &wp->w_p_lcs);
1336 if (errmsg)
1337 break;
1338 }
1339 redraw_all_later(NOT_VALID);
1340 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001341 }
1342
Bram Moolenaareed9d462021-02-15 20:38:25 +01001343 // local 'listchars'
1344 else if (varp == &curwin->w_p_lcs)
1345 errmsg = set_chars_option(curwin, varp);
1346
Bram Moolenaardac13472019-09-16 21:06:21 +02001347 // 'fillchars'
1348 else if (varp == &p_fcs)
1349 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001350 errmsg = set_chars_option(curwin, varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02001351 }
1352
1353#ifdef FEAT_CMDWIN
1354 // 'cedit'
1355 else if (varp == &p_cedit)
1356 {
1357 errmsg = check_cedit();
1358 }
1359#endif
1360
1361 // 'verbosefile'
1362 else if (varp == &p_vfile)
1363 {
1364 verbose_stop();
1365 if (*p_vfile != NUL && verbose_open() == FAIL)
1366 errmsg = e_invarg;
1367 }
1368
1369#ifdef FEAT_VIMINFO
1370 // 'viminfo'
1371 else if (varp == &p_viminfo)
1372 {
1373 for (s = p_viminfo; *s;)
1374 {
1375 // Check it's a valid character
1376 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
1377 {
1378 errmsg = illegal_char(errbuf, *s);
1379 break;
1380 }
1381 if (*s == 'n') // name is always last one
1382 break;
1383 else if (*s == 'r') // skip until next ','
1384 {
1385 while (*++s && *s != ',')
1386 ;
1387 }
1388 else if (*s == '%')
1389 {
1390 // optional number
1391 while (vim_isdigit(*++s))
1392 ;
1393 }
1394 else if (*s == '!' || *s == 'h' || *s == 'c')
1395 ++s; // no extra chars
1396 else // must have a number
1397 {
1398 while (vim_isdigit(*++s))
1399 ;
1400
1401 if (!VIM_ISDIGIT(*(s - 1)))
1402 {
1403 if (errbuf != NULL)
1404 {
1405 sprintf(errbuf, _("E526: Missing number after <%s>"),
1406 transchar_byte(*(s - 1)));
1407 errmsg = errbuf;
1408 }
1409 else
1410 errmsg = "";
1411 break;
1412 }
1413 }
1414 if (*s == ',')
1415 ++s;
1416 else if (*s)
1417 {
1418 if (errbuf != NULL)
1419 errmsg = N_("E527: Missing comma");
1420 else
1421 errmsg = "";
1422 break;
1423 }
1424 }
1425 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
1426 errmsg = N_("E528: Must specify a ' value");
1427 }
1428#endif // FEAT_VIMINFO
1429
1430 // terminal options
1431 else if (istermoption_idx(opt_idx) && full_screen)
1432 {
1433 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
1434 if (varp == &T_CCO)
1435 {
1436 int colors = atoi((char *)T_CCO);
1437
1438 // Only reinitialize colors if t_Co value has really changed to
1439 // avoid expensive reload of colorscheme if t_Co is set to the
1440 // same value multiple times.
1441 if (colors != t_colors)
1442 {
1443 t_colors = colors;
1444 if (t_colors <= 1)
1445 {
1446 if (new_value_alloced)
1447 vim_free(T_CCO);
1448 T_CCO = empty_option;
1449 }
1450#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1451 if (is_term_win32())
1452 {
1453 swap_tcap();
1454 did_swaptcap = TRUE;
1455 }
1456#endif
1457 // We now have a different color setup, initialize it again.
1458 init_highlight(TRUE, FALSE);
1459 }
1460 }
1461 ttest(FALSE);
1462 if (varp == &T_ME)
1463 {
1464 out_str(T_ME);
1465 redraw_later(CLEAR);
1466#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
1467 // Since t_me has been set, this probably means that the user
1468 // wants to use this as default colors. Need to reset default
1469 // background/foreground colors.
1470# ifdef VIMDLL
1471 if (!gui.in_use && !gui.starting)
1472# endif
1473 mch_set_normal_colors();
1474#endif
1475 }
1476 if (varp == &T_BE && termcap_active)
1477 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02001478#ifdef FEAT_JOB_CHANNEL
1479 ch_log_output = TRUE;
1480#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02001481 if (*T_BE == NUL)
1482 // When clearing t_BE we assume the user no longer wants
1483 // bracketed paste, thus disable it by writing t_BD.
1484 out_str(T_BD);
1485 else
1486 out_str(T_BE);
1487 }
1488 }
1489
1490#ifdef FEAT_LINEBREAK
1491 // 'showbreak'
Bram Moolenaaree857022019-11-09 23:26:40 +01001492 else if (gvarp == &p_sbr)
Bram Moolenaardac13472019-09-16 21:06:21 +02001493 {
Bram Moolenaaree857022019-11-09 23:26:40 +01001494 for (s = *varp; *s; )
Bram Moolenaardac13472019-09-16 21:06:21 +02001495 {
1496 if (ptr2cells(s) != 1)
Bram Moolenaaree857022019-11-09 23:26:40 +01001497 errmsg = N_("E595: 'showbreak' contains unprintable or wide character");
Bram Moolenaardac13472019-09-16 21:06:21 +02001498 MB_PTR_ADV(s);
1499 }
1500 }
1501#endif
1502
1503#ifdef FEAT_GUI
1504 // 'guifont'
1505 else if (varp == &p_guifont)
1506 {
1507 if (gui.in_use)
1508 {
1509 p = p_guifont;
1510# if defined(FEAT_GUI_GTK)
1511 // Put up a font dialog and let the user select a new value.
1512 // If this is cancelled go back to the old value but don't
1513 // give an error message.
1514 if (STRCMP(p, "*") == 0)
1515 {
1516 p = gui_mch_font_dialog(oldval);
1517
1518 if (new_value_alloced)
1519 free_string_option(p_guifont);
1520
1521 p_guifont = (p != NULL) ? p : vim_strsave(oldval);
1522 new_value_alloced = TRUE;
1523 }
1524# endif
1525 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
1526 {
1527# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
1528 if (STRCMP(p_guifont, "*") == 0)
1529 {
1530 // Dialog was cancelled: Keep the old value without giving
1531 // an error message.
1532 if (new_value_alloced)
1533 free_string_option(p_guifont);
1534 p_guifont = vim_strsave(oldval);
1535 new_value_alloced = TRUE;
1536 }
1537 else
1538# endif
1539 errmsg = N_("E596: Invalid font(s)");
1540 }
1541 }
1542 redraw_gui_only = TRUE;
1543 }
1544# ifdef FEAT_XFONTSET
1545 else if (varp == &p_guifontset)
1546 {
1547 if (STRCMP(p_guifontset, "*") == 0)
1548 errmsg = N_("E597: can't select fontset");
1549 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
1550 errmsg = N_("E598: Invalid fontset");
1551 redraw_gui_only = TRUE;
1552 }
1553# endif
1554 else if (varp == &p_guifontwide)
1555 {
1556 if (STRCMP(p_guifontwide, "*") == 0)
1557 errmsg = N_("E533: can't select wide font");
1558 else if (gui_get_wide_font() == FAIL)
1559 errmsg = N_("E534: Invalid wide font");
1560 redraw_gui_only = TRUE;
1561 }
1562#endif
Dusan Popovic4eeedc02021-10-16 20:52:05 +01001563# if defined(FEAT_GUI_GTK)
1564 else if (varp == &p_guiligatures)
1565 {
1566 gui_set_ligatures();
1567 redraw_gui_only = TRUE;
1568 }
1569# endif
Bram Moolenaardac13472019-09-16 21:06:21 +02001570
1571#ifdef CURSOR_SHAPE
1572 // 'guicursor'
1573 else if (varp == &p_guicursor)
1574 errmsg = parse_shape_opt(SHAPE_CURSOR);
1575#endif
1576
1577#ifdef FEAT_MOUSESHAPE
1578 // 'mouseshape'
1579 else if (varp == &p_mouseshape)
1580 {
1581 errmsg = parse_shape_opt(SHAPE_MOUSE);
1582 update_mouseshape(-1);
1583 }
1584#endif
1585
1586#ifdef FEAT_PRINTER
1587 else if (varp == &p_popt)
1588 errmsg = parse_printoptions();
1589# if defined(FEAT_POSTSCRIPT)
1590 else if (varp == &p_pmfn)
1591 errmsg = parse_printmbfont();
1592# endif
1593#endif
1594
1595#ifdef FEAT_LANGMAP
1596 // 'langmap'
1597 else if (varp == &p_langmap)
1598 langmap_set();
1599#endif
1600
1601#ifdef FEAT_LINEBREAK
1602 // 'breakat'
1603 else if (varp == &p_breakat)
1604 fill_breakat_flags();
1605#endif
1606
1607#ifdef FEAT_TITLE
1608 // 'titlestring' and 'iconstring'
1609 else if (varp == &p_titlestring || varp == &p_iconstring)
1610 {
1611# ifdef FEAT_STL_OPT
1612 int flagval = (varp == &p_titlestring) ? STL_IN_TITLE : STL_IN_ICON;
1613
1614 // NULL => statusline syntax
1615 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
1616 stl_syntax |= flagval;
1617 else
1618 stl_syntax &= ~flagval;
1619# endif
1620 did_set_title();
1621 }
1622#endif
1623
1624#ifdef FEAT_GUI
1625 // 'guioptions'
1626 else if (varp == &p_go)
1627 {
1628 gui_init_which_components(oldval);
1629 redraw_gui_only = TRUE;
1630 }
1631#endif
1632
1633#if defined(FEAT_GUI_TABLINE)
1634 // 'guitablabel'
1635 else if (varp == &p_gtl)
1636 {
1637 redraw_tabline = TRUE;
1638 redraw_gui_only = TRUE;
1639 }
1640 // 'guitabtooltip'
1641 else if (varp == &p_gtt)
1642 {
1643 redraw_gui_only = TRUE;
1644 }
1645#endif
1646
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001647#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +02001648 // 'ttymouse'
1649 else if (varp == &p_ttym)
1650 {
1651 // Switch the mouse off before changing the escape sequences used for
1652 // that.
1653 mch_setmouse(FALSE);
1654 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
1655 errmsg = e_invarg;
1656 else
1657 check_mouse_termcode();
1658 if (termcap_active)
1659 setmouse(); // may switch it on again
1660 }
1661#endif
1662
1663 // 'selection'
1664 else if (varp == &p_sel)
1665 {
1666 if (*p_sel == NUL
1667 || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
1668 errmsg = e_invarg;
1669 }
1670
1671 // 'selectmode'
1672 else if (varp == &p_slm)
1673 {
1674 if (check_opt_strings(p_slm, p_slm_values, TRUE) != OK)
1675 errmsg = e_invarg;
1676 }
1677
1678#ifdef FEAT_BROWSE
1679 // 'browsedir'
1680 else if (varp == &p_bsdir)
1681 {
1682 if (check_opt_strings(p_bsdir, p_bsdir_values, FALSE) != OK
1683 && !mch_isdir(p_bsdir))
1684 errmsg = e_invarg;
1685 }
1686#endif
1687
1688 // 'keymodel'
1689 else if (varp == &p_km)
1690 {
1691 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
1692 errmsg = e_invarg;
1693 else
1694 {
1695 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
1696 km_startsel = (vim_strchr(p_km, 'a') != NULL);
1697 }
1698 }
1699
1700 // 'mousemodel'
1701 else if (varp == &p_mousem)
1702 {
1703 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
1704 errmsg = e_invarg;
1705#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
1706 else if (*p_mousem != *oldval)
1707 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
1708 // to create or delete the popup menus.
1709 gui_motif_update_mousemodel(root_menu);
1710#endif
1711 }
1712
1713 // 'switchbuf'
1714 else if (varp == &p_swb)
1715 {
1716 if (opt_strings_flags(p_swb, p_swb_values, &swb_flags, TRUE) != OK)
1717 errmsg = e_invarg;
1718 }
1719
1720 // 'debug'
1721 else if (varp == &p_debug)
1722 {
1723 if (check_opt_strings(p_debug, p_debug_values, TRUE) != OK)
1724 errmsg = e_invarg;
1725 }
1726
1727 // 'display'
1728 else if (varp == &p_dy)
1729 {
1730 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
1731 errmsg = e_invarg;
1732 else
1733 (void)init_chartab();
1734
1735 }
1736
1737 // 'eadirection'
1738 else if (varp == &p_ead)
1739 {
1740 if (check_opt_strings(p_ead, p_ead_values, FALSE) != OK)
1741 errmsg = e_invarg;
1742 }
1743
1744#ifdef FEAT_CLIPBOARD
1745 // 'clipboard'
1746 else if (varp == &p_cb)
1747 errmsg = check_clipboard_option();
1748#endif
1749
1750#ifdef FEAT_SPELL
1751 // When 'spelllang' or 'spellfile' is set and there is a window for this
1752 // buffer in which 'spell' is set load the wordlists.
1753 else if (varp == &(curwin->w_s->b_p_spl)
1754 || varp == &(curwin->w_s->b_p_spf))
1755 {
1756 int is_spellfile = varp == &(curwin->w_s->b_p_spf);
1757
1758 if ((is_spellfile && !valid_spellfile(*varp))
Bram Moolenaarf154f3a2020-06-08 18:54:49 +02001759 || (!is_spellfile && !valid_spelllang(*varp)))
Bram Moolenaardac13472019-09-16 21:06:21 +02001760 errmsg = e_invarg;
1761 else
1762 errmsg = did_set_spell_option(is_spellfile);
1763 }
1764 // When 'spellcapcheck' is set compile the regexp program.
1765 else if (varp == &(curwin->w_s->b_p_spc))
1766 {
1767 errmsg = compile_cap_prog(curwin->w_s);
1768 }
Bram Moolenaar362b44b2020-06-10 21:47:00 +02001769 // 'spelloptions'
1770 else if (varp == &(curwin->w_s->b_p_spo))
1771 {
1772 if (**varp != NUL && STRCMP("camel", *varp) != 0)
1773 errmsg = e_invarg;
1774 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001775 // 'spellsuggest'
1776 else if (varp == &p_sps)
1777 {
1778 if (spell_check_sps() != OK)
1779 errmsg = e_invarg;
1780 }
1781 // 'mkspellmem'
1782 else if (varp == &p_msm)
1783 {
1784 if (spell_check_msm() != OK)
1785 errmsg = e_invarg;
1786 }
1787#endif
1788
1789 // When 'bufhidden' is set, check for valid value.
1790 else if (gvarp == &p_bh)
1791 {
1792 if (check_opt_strings(curbuf->b_p_bh, p_bufhidden_values, FALSE) != OK)
1793 errmsg = e_invarg;
1794 }
1795
1796 // When 'buftype' is set, check for valid value.
1797 else if (gvarp == &p_bt)
1798 {
1799 if (check_opt_strings(curbuf->b_p_bt, p_buftype_values, FALSE) != OK)
1800 errmsg = e_invarg;
1801 else
1802 {
1803 if (curwin->w_status_height)
1804 {
1805 curwin->w_redr_status = TRUE;
1806 redraw_later(VALID);
1807 }
1808 curbuf->b_help = (curbuf->b_p_bt[0] == 'h');
1809#ifdef FEAT_TITLE
1810 redraw_titles();
1811#endif
1812 }
1813 }
1814
1815#ifdef FEAT_STL_OPT
1816 // 'statusline' or 'rulerformat'
1817 else if (gvarp == &p_stl || varp == &p_ruf)
1818 {
1819 int wid;
1820
1821 if (varp == &p_ruf) // reset ru_wid first
1822 ru_wid = 0;
1823 s = *varp;
1824 if (varp == &p_ruf && *s == '%')
1825 {
1826 // set ru_wid if 'ruf' starts with "%99("
1827 if (*++s == '-') // ignore a '-'
1828 s++;
1829 wid = getdigits(&s);
1830 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
1831 ru_wid = wid;
1832 else
1833 errmsg = check_stl_option(p_ruf);
1834 }
1835 // check 'statusline' only if it doesn't start with "%!"
1836 else if (varp == &p_ruf || s[0] != '%' || s[1] != '!')
1837 errmsg = check_stl_option(s);
1838 if (varp == &p_ruf && errmsg == NULL)
1839 comp_col();
1840 }
1841#endif
1842
1843 // check if it is a valid value for 'complete' -- Acevedo
1844 else if (gvarp == &p_cpt)
1845 {
1846 for (s = *varp; *s;)
1847 {
1848 while (*s == ',' || *s == ' ')
1849 s++;
1850 if (!*s)
1851 break;
1852 if (vim_strchr((char_u *)".wbuksid]tU", *s) == NULL)
1853 {
1854 errmsg = illegal_char(errbuf, *s);
1855 break;
1856 }
1857 if (*++s != NUL && *s != ',' && *s != ' ')
1858 {
1859 if (s[-1] == 'k' || s[-1] == 's')
1860 {
1861 // skip optional filename after 'k' and 's'
1862 while (*s && *s != ',' && *s != ' ')
1863 {
1864 if (*s == '\\' && s[1] != NUL)
1865 ++s;
1866 ++s;
1867 }
1868 }
1869 else
1870 {
1871 if (errbuf != NULL)
1872 {
1873 sprintf((char *)errbuf,
1874 _("E535: Illegal character after <%c>"),
1875 *--s);
1876 errmsg = errbuf;
1877 }
1878 else
1879 errmsg = "";
1880 break;
1881 }
1882 }
1883 }
1884 }
1885
1886 // 'completeopt'
1887 else if (varp == &p_cot)
1888 {
1889 if (check_opt_strings(p_cot, p_cot_values, TRUE) != OK)
1890 errmsg = e_invarg;
1891 else
1892 completeopt_was_set();
1893 }
1894
1895#ifdef BACKSLASH_IN_FILENAME
1896 // 'completeslash'
1897 else if (gvarp == &p_csl)
1898 {
1899 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1900 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
1901 errmsg = e_invarg;
1902 }
1903#endif
1904
1905#ifdef FEAT_SIGNS
1906 // 'signcolumn'
1907 else if (varp == &curwin->w_p_scl)
1908 {
1909 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
1910 errmsg = e_invarg;
1911 // When changing the 'signcolumn' to or from 'number', recompute the
1912 // width of the number column if 'number' or 'relativenumber' is set.
1913 if (((*oldval == 'n' && *(oldval + 1) == 'u')
1914 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
1915 && (curwin->w_p_nu || curwin->w_p_rnu))
1916 curwin->w_nrwidth_line_count = 0;
1917 }
1918#endif
1919
1920
1921#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
1922 // 'toolbar'
1923 else if (varp == &p_toolbar)
1924 {
1925 if (opt_strings_flags(p_toolbar, p_toolbar_values,
1926 &toolbar_flags, TRUE) != OK)
1927 errmsg = e_invarg;
1928 else
1929 {
1930 out_flush();
1931 gui_mch_show_toolbar((toolbar_flags &
1932 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
1933 }
1934 }
1935#endif
1936
1937#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
1938 // 'toolbariconsize': GTK+ 2 only
1939 else if (varp == &p_tbis)
1940 {
1941 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
1942 errmsg = e_invarg;
1943 else
1944 {
1945 out_flush();
1946 gui_mch_show_toolbar((toolbar_flags &
1947 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
1948 }
1949 }
1950#endif
1951
1952 // 'pastetoggle': translate key codes like in a mapping
1953 else if (varp == &p_pt)
1954 {
1955 if (*p_pt)
1956 {
Bram Moolenaar1e7b52a2019-10-13 16:59:08 +02001957 (void)replace_termcodes(p_pt, &p,
1958 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
Bram Moolenaardac13472019-09-16 21:06:21 +02001959 if (p != NULL)
1960 {
1961 if (new_value_alloced)
1962 free_string_option(p_pt);
1963 p_pt = p;
1964 new_value_alloced = TRUE;
1965 }
1966 }
1967 }
1968
1969 // 'backspace'
1970 else if (varp == &p_bs)
1971 {
1972 if (VIM_ISDIGIT(*p_bs))
1973 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02001974 if (*p_bs > '3' || p_bs[1] != NUL)
Bram Moolenaardac13472019-09-16 21:06:21 +02001975 errmsg = e_invarg;
1976 }
1977 else if (check_opt_strings(p_bs, p_bs_values, TRUE) != OK)
1978 errmsg = e_invarg;
1979 }
1980 else if (varp == &p_bo)
1981 {
1982 if (opt_strings_flags(p_bo, p_bo_values, &bo_flags, TRUE) != OK)
1983 errmsg = e_invarg;
1984 }
1985
1986 // 'tagcase'
1987 else if (gvarp == &p_tc)
1988 {
1989 unsigned int *flags;
1990
1991 if (opt_flags & OPT_LOCAL)
1992 {
1993 p = curbuf->b_p_tc;
1994 flags = &curbuf->b_tc_flags;
1995 }
1996 else
1997 {
1998 p = p_tc;
1999 flags = &tc_flags;
2000 }
2001
2002 if ((opt_flags & OPT_LOCAL) && *p == NUL)
2003 // make the local value empty: use the global value
2004 *flags = 0;
2005 else if (*p == NUL
2006 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
2007 errmsg = e_invarg;
2008 }
2009
2010 // 'casemap'
2011 else if (varp == &p_cmp)
2012 {
2013 if (opt_strings_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE) != OK)
2014 errmsg = e_invarg;
2015 }
2016
2017#ifdef FEAT_DIFF
2018 // 'diffopt'
2019 else if (varp == &p_dip)
2020 {
2021 if (diffopt_changed() == FAIL)
2022 errmsg = e_invarg;
2023 }
2024#endif
2025
2026#ifdef FEAT_FOLDING
2027 // 'foldmethod'
2028 else if (gvarp == &curwin->w_allbuf_opt.wo_fdm)
2029 {
2030 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK
2031 || *curwin->w_p_fdm == NUL)
2032 errmsg = e_invarg;
2033 else
2034 {
2035 foldUpdateAll(curwin);
2036 if (foldmethodIsDiff(curwin))
2037 newFoldLevel();
2038 }
2039 }
2040# ifdef FEAT_EVAL
2041 // 'foldexpr'
2042 else if (varp == &curwin->w_p_fde)
2043 {
2044 if (foldmethodIsExpr(curwin))
2045 foldUpdateAll(curwin);
2046 }
2047# endif
2048 // 'foldmarker'
2049 else if (gvarp == &curwin->w_allbuf_opt.wo_fmr)
2050 {
2051 p = vim_strchr(*varp, ',');
2052 if (p == NULL)
2053 errmsg = N_("E536: comma required");
2054 else if (p == *varp || p[1] == NUL)
2055 errmsg = e_invarg;
2056 else if (foldmethodIsMarker(curwin))
2057 foldUpdateAll(curwin);
2058 }
2059 // 'commentstring'
2060 else if (gvarp == &p_cms)
2061 {
2062 if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
2063 errmsg = N_("E537: 'commentstring' must be empty or contain %s");
2064 }
2065 // 'foldopen'
2066 else if (varp == &p_fdo)
2067 {
2068 if (opt_strings_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE) != OK)
2069 errmsg = e_invarg;
2070 }
2071 // 'foldclose'
2072 else if (varp == &p_fcl)
2073 {
2074 if (check_opt_strings(p_fcl, p_fcl_values, TRUE) != OK)
2075 errmsg = e_invarg;
2076 }
2077 // 'foldignore'
2078 else if (gvarp == &curwin->w_allbuf_opt.wo_fdi)
2079 {
2080 if (foldmethodIsIndent(curwin))
2081 foldUpdateAll(curwin);
2082 }
2083#endif
2084
2085 // 'virtualedit'
Gary Johnson53ba05b2021-07-26 22:19:10 +02002086 else if (gvarp == &p_ve)
Bram Moolenaardac13472019-09-16 21:06:21 +02002087 {
Gary Johnson53ba05b2021-07-26 22:19:10 +02002088 char_u *ve = p_ve;
2089 unsigned int *flags = &ve_flags;
2090
2091 if (opt_flags & OPT_LOCAL)
Bram Moolenaardac13472019-09-16 21:06:21 +02002092 {
Gary Johnson51ad8502021-08-03 18:33:08 +02002093 ve = curwin->w_p_ve;
2094 flags = &curwin->w_ve_flags;
Gary Johnson53ba05b2021-07-26 22:19:10 +02002095 }
2096
2097 if ((opt_flags & OPT_LOCAL) && *ve == NUL)
2098 // make the local value empty: use the global value
2099 *flags = 0;
2100 else
2101 {
2102 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
2103 errmsg = e_invarg;
2104 else if (STRCMP(p_ve, oldval) != 0)
2105 {
2106 // Recompute cursor position in case the new 've' setting
2107 // changes something.
2108 validate_virtcol();
2109 coladvance(curwin->w_virtcol);
2110 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002111 }
2112 }
2113
2114#if defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)
2115 else if (varp == &p_csqf)
2116 {
2117 if (p_csqf != NULL)
2118 {
2119 p = p_csqf;
2120 while (*p != NUL)
2121 {
2122 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
2123 || p[1] == NUL
2124 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
2125 || (p[2] != NUL && p[2] != ','))
2126 {
2127 errmsg = e_invarg;
2128 break;
2129 }
2130 else if (p[2] == NUL)
2131 break;
2132 else
2133 p += 3;
2134 }
2135 }
2136 }
2137#endif
2138
2139#ifdef FEAT_CINDENT
2140 // 'cinoptions'
2141 else if (gvarp == &p_cino)
2142 {
2143 // TODO: recognize errors
2144 parse_cino(curbuf);
2145 }
2146#endif
2147
2148#if defined(FEAT_RENDER_OPTIONS)
2149 // 'renderoptions'
2150 else if (varp == &p_rop)
2151 {
2152 if (!gui_mch_set_rendering_options(p_rop))
2153 errmsg = e_invarg;
2154 }
2155#endif
2156
2157 else if (gvarp == &p_ft)
2158 {
2159 if (!valid_filetype(*varp))
2160 errmsg = e_invarg;
2161 else
2162 {
2163 value_changed = STRCMP(oldval, *varp) != 0;
2164
2165 // Since we check the value, there is no need to set P_INSECURE,
2166 // even when the value comes from a modeline.
2167 *value_checked = TRUE;
2168 }
2169 }
2170
2171#ifdef FEAT_SYN_HL
2172 else if (gvarp == &p_syn)
2173 {
2174 if (!valid_filetype(*varp))
2175 errmsg = e_invarg;
2176 else
2177 {
2178 value_changed = STRCMP(oldval, *varp) != 0;
2179
2180 // Since we check the value, there is no need to set P_INSECURE,
2181 // even when the value comes from a modeline.
2182 *value_checked = TRUE;
2183 }
2184 }
2185#endif
2186
2187#ifdef FEAT_TERMINAL
2188 // 'termwinkey'
2189 else if (varp == &curwin->w_p_twk)
2190 {
2191 if (*curwin->w_p_twk != NUL
2192 && string_to_key(curwin->w_p_twk, TRUE) == 0)
2193 errmsg = e_invarg;
2194 }
2195 // 'termwinsize'
2196 else if (varp == &curwin->w_p_tws)
2197 {
2198 if (*curwin->w_p_tws != NUL)
2199 {
2200 p = skipdigits(curwin->w_p_tws);
2201 if (p == curwin->w_p_tws
2202 || (*p != 'x' && *p != '*')
2203 || *skipdigits(p + 1) != NUL)
2204 errmsg = e_invarg;
2205 }
2206 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002207 // 'wincolor'
2208 else if (varp == &curwin->w_p_wcr)
2209 {
2210 if (curwin->w_buffer->b_term != NULL)
Bram Moolenaarad431992021-05-03 20:40:38 +02002211 term_update_colors(curwin->w_buffer->b_term);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002212 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002213# if defined(MSWIN)
2214 // 'termwintype'
2215 else if (varp == &p_twt)
2216 {
2217 if (check_opt_strings(*varp, p_twt_values, FALSE) != OK)
2218 errmsg = e_invarg;
2219 }
2220# endif
2221#endif
2222
2223#ifdef FEAT_VARTABS
2224 // 'varsofttabstop'
2225 else if (varp == &(curbuf->b_p_vsts))
2226 {
2227 char_u *cp;
2228
2229 if (!(*varp)[0] || ((*varp)[0] == '0' && !(*varp)[1]))
2230 {
2231 if (curbuf->b_p_vsts_array)
2232 {
2233 vim_free(curbuf->b_p_vsts_array);
2234 curbuf->b_p_vsts_array = 0;
2235 }
2236 }
2237 else
2238 {
2239 for (cp = *varp; *cp; ++cp)
2240 {
2241 if (vim_isdigit(*cp))
2242 continue;
2243 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
2244 continue;
2245 errmsg = e_invarg;
2246 break;
2247 }
2248 if (errmsg == NULL)
2249 {
2250 int *oldarray = curbuf->b_p_vsts_array;
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002251 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Bram Moolenaardac13472019-09-16 21:06:21 +02002252 {
2253 if (oldarray)
2254 vim_free(oldarray);
2255 }
2256 else
2257 errmsg = e_invarg;
2258 }
2259 }
2260 }
2261
2262 // 'vartabstop'
2263 else if (varp == &(curbuf->b_p_vts))
2264 {
2265 char_u *cp;
2266
2267 if (!(*varp)[0] || ((*varp)[0] == '0' && !(*varp)[1]))
2268 {
2269 if (curbuf->b_p_vts_array)
2270 {
2271 vim_free(curbuf->b_p_vts_array);
2272 curbuf->b_p_vts_array = NULL;
2273 }
2274 }
2275 else
2276 {
2277 for (cp = *varp; *cp; ++cp)
2278 {
2279 if (vim_isdigit(*cp))
2280 continue;
2281 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
2282 continue;
2283 errmsg = e_invarg;
2284 break;
2285 }
2286 if (errmsg == NULL)
2287 {
2288 int *oldarray = curbuf->b_p_vts_array;
2289
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002290 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Bram Moolenaardac13472019-09-16 21:06:21 +02002291 {
2292 vim_free(oldarray);
2293#ifdef FEAT_FOLDING
2294 if (foldmethodIsIndent(curwin))
2295 foldUpdateAll(curwin);
2296#endif
2297 }
2298 else
2299 errmsg = e_invarg;
2300 }
2301 }
2302 }
2303#endif
2304
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002305#ifdef FEAT_PROP_POPUP
Bram Moolenaardac13472019-09-16 21:06:21 +02002306 // 'previewpopup'
2307 else if (varp == &p_pvp)
2308 {
2309 if (parse_previewpopup(NULL) == FAIL)
2310 errmsg = e_invarg;
2311 }
2312# ifdef FEAT_QUICKFIX
2313 // 'completepopup'
2314 else if (varp == &p_cpp)
2315 {
2316 if (parse_completepopup(NULL) == FAIL)
2317 errmsg = e_invarg;
Bram Moolenaar447bfba2020-07-18 16:07:16 +02002318 else
2319 popup_close_info();
Bram Moolenaardac13472019-09-16 21:06:21 +02002320 }
2321# endif
2322#endif
2323
Bram Moolenaard43906d2020-07-20 21:31:32 +02002324#ifdef FEAT_QUICKFIX
2325 else if (varp == &p_qftf)
2326 {
2327 if (qf_process_qftf_option() == FALSE)
2328 errmsg = e_invarg;
2329 }
2330#endif
2331
Bram Moolenaardac13472019-09-16 21:06:21 +02002332 // Options that are a list of flags.
2333 else
2334 {
2335 p = NULL;
2336 if (varp == &p_ww) // 'whichwrap'
2337 p = (char_u *)WW_ALL;
2338 if (varp == &p_shm) // 'shortmess'
2339 p = (char_u *)SHM_ALL;
2340 else if (varp == &(p_cpo)) // 'cpoptions'
2341 p = (char_u *)CPO_ALL;
2342 else if (varp == &(curbuf->b_p_fo)) // 'formatoptions'
2343 p = (char_u *)FO_ALL;
2344#ifdef FEAT_CONCEAL
2345 else if (varp == &curwin->w_p_cocu) // 'concealcursor'
2346 p = (char_u *)COCU_ALL;
2347#endif
2348 else if (varp == &p_mouse) // 'mouse'
2349 {
Bram Moolenaardac13472019-09-16 21:06:21 +02002350 p = (char_u *)MOUSE_ALL;
Bram Moolenaardac13472019-09-16 21:06:21 +02002351 }
2352#if defined(FEAT_GUI)
2353 else if (varp == &p_go) // 'guioptions'
2354 p = (char_u *)GO_ALL;
2355#endif
2356 if (p != NULL)
2357 {
2358 for (s = *varp; *s; ++s)
2359 if (vim_strchr(p, *s) == NULL)
2360 {
2361 errmsg = illegal_char(errbuf, *s);
2362 break;
2363 }
2364 }
2365 }
2366
2367 // If error detected, restore the previous value.
2368 if (errmsg != NULL)
2369 {
2370 if (new_value_alloced)
2371 free_string_option(*varp);
2372 *varp = oldval;
2373 // When resetting some values, need to act on it.
2374 if (did_chartab)
2375 (void)init_chartab();
2376 if (varp == &p_hl)
2377 (void)highlight_changed();
2378 }
2379 else
2380 {
2381#ifdef FEAT_EVAL
2382 // Remember where the option was set.
2383 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
2384#endif
2385 // Free string options that are in allocated memory.
2386 // Use "free_oldval", because recursiveness may change the flags under
2387 // our fingers (esp. init_highlight()).
2388 if (free_oldval)
2389 free_string_option(oldval);
2390 if (new_value_alloced)
2391 set_option_flag(opt_idx, P_ALLOCED);
2392 else
2393 clear_option_flag(opt_idx, P_ALLOCED);
2394
2395 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
2396 && is_global_local_option(opt_idx))
2397 {
2398 // global option with local value set to use global value; free
2399 // the local value and make it empty
2400 p = get_option_varp_scope(opt_idx, OPT_LOCAL);
2401 free_string_option(*(char_u **)p);
2402 *(char_u **)p = empty_option;
2403 }
2404
2405 // May set global value for local option.
2406 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
2407 set_string_option_global(opt_idx, varp);
2408
2409 // Trigger the autocommand only after setting the flags.
2410#ifdef FEAT_SYN_HL
2411 // When 'syntax' is set, load the syntax of that name
2412 if (varp == &(curbuf->b_p_syn))
2413 {
2414 static int syn_recursive = 0;
2415
2416 ++syn_recursive;
2417 // Only pass TRUE for "force" when the value changed or not used
2418 // recursively, to avoid endless recurrence.
2419 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
2420 value_changed || syn_recursive == 1, curbuf);
2421 curbuf->b_flags |= BF_SYN_SET;
2422 --syn_recursive;
2423 }
2424#endif
2425 else if (varp == &(curbuf->b_p_ft))
2426 {
2427 // 'filetype' is set, trigger the FileType autocommand.
2428 // Skip this when called from a modeline and the filetype was
2429 // already set to this value.
2430 if (!(opt_flags & OPT_MODELINE) || value_changed)
2431 {
2432 static int ft_recursive = 0;
2433 int secure_save = secure;
2434
2435 // Reset the secure flag, since the value of 'filetype' has
2436 // been checked to be safe.
2437 secure = 0;
2438
2439 ++ft_recursive;
2440 did_filetype = TRUE;
2441 // Only pass TRUE for "force" when the value changed or not
2442 // used recursively, to avoid endless recurrence.
2443 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2444 value_changed || ft_recursive == 1, curbuf);
2445 --ft_recursive;
2446 // Just in case the old "curbuf" is now invalid.
2447 if (varp != &(curbuf->b_p_ft))
2448 varp = NULL;
2449
2450 secure = secure_save;
2451 }
2452 }
2453#ifdef FEAT_SPELL
2454 if (varp == &(curwin->w_s->b_p_spl))
2455 {
2456 char_u fname[200];
2457 char_u *q = curwin->w_s->b_p_spl;
2458
2459 // Skip the first name if it is "cjk".
2460 if (STRNCMP(q, "cjk,", 4) == 0)
2461 q += 4;
2462
2463 // Source the spell/LANG.vim in 'runtimepath'.
2464 // They could set 'spellcapcheck' depending on the language.
2465 // Use the first name in 'spelllang' up to '_region' or
2466 // '.encoding'.
2467 for (p = q; *p != NUL; ++p)
2468 if (!ASCII_ISALNUM(*p) && *p != '-')
2469 break;
2470 if (p > q)
2471 {
2472 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
2473 (int)(p - q), q);
2474 source_runtime(fname, DIP_ALL);
2475 }
2476 }
2477#endif
2478 }
2479
Bram Moolenaardac13472019-09-16 21:06:21 +02002480 if (varp == &p_mouse)
2481 {
Bram Moolenaardac13472019-09-16 21:06:21 +02002482 if (*p_mouse == NUL)
2483 mch_setmouse(FALSE); // switch mouse off
2484 else
Bram Moolenaardac13472019-09-16 21:06:21 +02002485 setmouse(); // in case 'mouse' changed
2486 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002487
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002488#if defined(FEAT_LUA) || defined(PROTO)
2489 if (varp == &p_rtp)
2490 update_package_paths_in_lua();
2491#endif
2492
Bram Moolenaardac13472019-09-16 21:06:21 +02002493 if (curwin->w_curswant != MAXCOL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002494 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02002495 curwin->w_set_curswant = TRUE;
2496
Bram Moolenaar37294bd2021-03-10 13:40:08 +01002497 if ((opt_flags & OPT_NO_REDRAW) == 0)
2498 {
Bram Moolenaardac13472019-09-16 21:06:21 +02002499#ifdef FEAT_GUI
Bram Moolenaar37294bd2021-03-10 13:40:08 +01002500 // check redraw when it's not a GUI option or the GUI is active.
2501 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02002502#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01002503 check_redraw(get_option_flags(opt_idx));
2504 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002505
2506#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2507 if (did_swaptcap)
2508 {
2509 set_termname((char_u *)"win32");
2510 init_highlight(TRUE, FALSE);
2511 }
2512#endif
2513
2514 return errmsg;
2515}
2516
2517/*
2518 * Check an option that can be a range of string values.
2519 *
2520 * Return OK for correct value, FAIL otherwise.
2521 * Empty is always OK.
2522 */
2523 static int
2524check_opt_strings(
2525 char_u *val,
2526 char **values,
2527 int list) // when TRUE: accept a list of values
2528{
2529 return opt_strings_flags(val, values, NULL, list);
2530}
2531
2532/*
2533 * Handle an option that can be a range of string values.
2534 * Set a flag in "*flagp" for each string present.
2535 *
2536 * Return OK for correct value, FAIL otherwise.
2537 * Empty is always OK.
2538 */
2539 static int
2540opt_strings_flags(
2541 char_u *val, // new value
2542 char **values, // array of valid string values
2543 unsigned *flagp,
2544 int list) // when TRUE: accept a list of values
2545{
2546 int i;
2547 int len;
2548 unsigned new_flags = 0;
2549
2550 while (*val)
2551 {
2552 for (i = 0; ; ++i)
2553 {
2554 if (values[i] == NULL) // val not found in values[]
2555 return FAIL;
2556
2557 len = (int)STRLEN(values[i]);
2558 if (STRNCMP(values[i], val, len) == 0
2559 && ((list && val[len] == ',') || val[len] == NUL))
2560 {
2561 val += len + (val[len] == ',');
2562 new_flags |= (1 << i);
2563 break; // check next item in val list
2564 }
2565 }
2566 }
2567 if (flagp != NULL)
2568 *flagp = new_flags;
2569
2570 return OK;
2571}
2572
2573/*
2574 * return OK if "p" is a valid fileformat name, FAIL otherwise.
2575 */
2576 int
2577check_ff_value(char_u *p)
2578{
2579 return check_opt_strings(p, p_ff_values, FALSE);
2580}