blob: 5c3f76dc9e7e6ce3811fd2e5443d50a873dd5574 [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};
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000060static char *(p_wop_values[]) = {"fuzzy", "tagfile", "pum", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020061#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,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000184 get_option_fullname(opt_idx), NULL, FALSE,
Bram Moolenaardac13472019-09-16 21:06:21 +0200185 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 "";
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000196 sprintf((char *)errbuf, _(e_illegal_character_str), (char *)transchar(c));
Bram Moolenaardac13472019-09-16 21:06:21 +0200197 return errbuf;
198}
199
200/*
201 * Check string options in a buffer for NULL value.
202 */
203 void
204check_buf_options(buf_T *buf)
205{
206 check_string_option(&buf->b_p_bh);
207 check_string_option(&buf->b_p_bt);
208 check_string_option(&buf->b_p_fenc);
209 check_string_option(&buf->b_p_ff);
210#ifdef FEAT_FIND_ID
211 check_string_option(&buf->b_p_def);
212 check_string_option(&buf->b_p_inc);
213# ifdef FEAT_EVAL
214 check_string_option(&buf->b_p_inex);
215# endif
216#endif
217#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
218 check_string_option(&buf->b_p_inde);
219 check_string_option(&buf->b_p_indk);
220#endif
221#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
222 check_string_option(&buf->b_p_bexpr);
223#endif
224#if defined(FEAT_CRYPT)
225 check_string_option(&buf->b_p_cm);
226#endif
227 check_string_option(&buf->b_p_fp);
228#if defined(FEAT_EVAL)
229 check_string_option(&buf->b_p_fex);
230#endif
231#ifdef FEAT_CRYPT
232 check_string_option(&buf->b_p_key);
233#endif
234 check_string_option(&buf->b_p_kp);
235 check_string_option(&buf->b_p_mps);
236 check_string_option(&buf->b_p_fo);
237 check_string_option(&buf->b_p_flp);
238 check_string_option(&buf->b_p_isk);
Bram Moolenaardac13472019-09-16 21:06:21 +0200239 check_string_option(&buf->b_p_com);
Bram Moolenaardac13472019-09-16 21:06:21 +0200240#ifdef FEAT_FOLDING
241 check_string_option(&buf->b_p_cms);
242#endif
243 check_string_option(&buf->b_p_nf);
244#ifdef FEAT_TEXTOBJ
245 check_string_option(&buf->b_p_qe);
246#endif
247#ifdef FEAT_SYN_HL
248 check_string_option(&buf->b_p_syn);
249 check_string_option(&buf->b_s.b_syn_isk);
250#endif
251#ifdef FEAT_SPELL
252 check_string_option(&buf->b_s.b_p_spc);
253 check_string_option(&buf->b_s.b_p_spf);
254 check_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +0200255 check_string_option(&buf->b_s.b_p_spo);
Bram Moolenaardac13472019-09-16 21:06:21 +0200256#endif
257#ifdef FEAT_SEARCHPATH
258 check_string_option(&buf->b_p_sua);
259#endif
260#ifdef FEAT_CINDENT
261 check_string_option(&buf->b_p_cink);
262 check_string_option(&buf->b_p_cino);
263 parse_cino(buf);
264#endif
265 check_string_option(&buf->b_p_ft);
266#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
267 check_string_option(&buf->b_p_cinw);
268#endif
269 check_string_option(&buf->b_p_cpt);
270#ifdef FEAT_COMPL_FUNC
271 check_string_option(&buf->b_p_cfu);
272 check_string_option(&buf->b_p_ofu);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +0100273 check_string_option(&buf->b_p_tsrfu);
Bram Moolenaardac13472019-09-16 21:06:21 +0200274#endif
275#ifdef FEAT_EVAL
276 check_string_option(&buf->b_p_tfu);
277#endif
278#ifdef FEAT_KEYMAP
279 check_string_option(&buf->b_p_keymap);
280#endif
281#ifdef FEAT_QUICKFIX
282 check_string_option(&buf->b_p_gp);
283 check_string_option(&buf->b_p_mp);
284 check_string_option(&buf->b_p_efm);
285#endif
286 check_string_option(&buf->b_p_ep);
287 check_string_option(&buf->b_p_path);
288 check_string_option(&buf->b_p_tags);
289 check_string_option(&buf->b_p_tc);
290 check_string_option(&buf->b_p_dict);
291 check_string_option(&buf->b_p_tsr);
292#ifdef FEAT_LISP
293 check_string_option(&buf->b_p_lw);
294#endif
295 check_string_option(&buf->b_p_bkc);
296 check_string_option(&buf->b_p_menc);
297#ifdef FEAT_VARTABS
298 check_string_option(&buf->b_p_vsts);
299 check_string_option(&buf->b_p_vts);
300#endif
301}
302
303/*
304 * Free the string allocated for an option.
305 * Checks for the string being empty_option. This may happen if we're out of
306 * memory, vim_strsave() returned NULL, which was replaced by empty_option by
307 * check_options().
308 * Does NOT check for P_ALLOCED flag!
309 */
310 void
311free_string_option(char_u *p)
312{
313 if (p != empty_option)
314 vim_free(p);
315}
316
317 void
318clear_string_option(char_u **pp)
319{
320 if (*pp != empty_option)
321 vim_free(*pp);
322 *pp = empty_option;
323}
324
325 void
326check_string_option(char_u **pp)
327{
328 if (*pp == NULL)
329 *pp = empty_option;
330}
331
332/*
333 * Set global value for string option when it's a local option.
334 */
335 static void
336set_string_option_global(
337 int opt_idx, // option index
338 char_u **varp) // pointer to option variable
339{
340 char_u **p, *s;
341
342 // the global value is always allocated
343 if (is_window_local_option(opt_idx))
344 p = (char_u **)GLOBAL_WO(varp);
345 else
346 p = (char_u **)get_option_var(opt_idx);
347 if (!is_global_option(opt_idx)
348 && p != varp
349 && (s = vim_strsave(*varp)) != NULL)
350 {
351 free_string_option(*p);
352 *p = s;
353 }
354}
355
356/*
357 * Set a string option to a new value (without checking the effect).
358 * The string is copied into allocated memory.
359 * if ("opt_idx" == -1) "name" is used, otherwise "opt_idx" is used.
360 * When "set_sid" is zero set the scriptID to current_sctx.sc_sid. When
361 * "set_sid" is SID_NONE don't set the scriptID. Otherwise set the scriptID to
362 * "set_sid".
363 */
364 void
365set_string_option_direct(
366 char_u *name,
367 int opt_idx,
368 char_u *val,
369 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
370 int set_sid UNUSED)
371{
372 char_u *s;
373 char_u **varp;
374 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
375 int idx = opt_idx;
376
377 if (idx == -1) // use name
378 {
379 idx = findoption(name);
380 if (idx < 0) // not found (should not happen)
381 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000382 semsg(_(e_internal_error_str), "set_string_option_direct()");
Bram Moolenaardac13472019-09-16 21:06:21 +0200383 siemsg(_("For option %s"), name);
384 return;
385 }
386 }
387
388 if (is_hidden_option(idx)) // can't set hidden option
389 return;
390
391 s = vim_strsave(val);
392 if (s != NULL)
393 {
394 varp = (char_u **)get_option_varp_scope(idx,
395 both ? OPT_LOCAL : opt_flags);
396 if ((opt_flags & OPT_FREE) && (get_option_flags(idx) & P_ALLOCED))
397 free_string_option(*varp);
398 *varp = s;
399
400 // For buffer/window local option may also set the global value.
401 if (both)
402 set_string_option_global(idx, varp);
403
404 set_option_flag(idx, P_ALLOCED);
405
406 // When setting both values of a global option with a local value,
407 // make the local value empty, so that the global value is used.
408 if (is_global_local_option(idx) && both)
409 {
410 free_string_option(*varp);
411 *varp = empty_option;
412 }
413# ifdef FEAT_EVAL
414 if (set_sid != SID_NONE)
415 {
416 sctx_T script_ctx;
417
418 if (set_sid == 0)
419 script_ctx = current_sctx;
420 else
421 {
422 script_ctx.sc_sid = set_sid;
423 script_ctx.sc_seq = 0;
424 script_ctx.sc_lnum = 0;
425 script_ctx.sc_version = 1;
426 }
427 set_option_sctx_idx(idx, opt_flags, script_ctx);
428 }
429# endif
430 }
431}
432
433/*
434 * Like set_string_option_direct(), but for a window-local option in "wp".
435 * Blocks autocommands to avoid the old curwin becoming invalid.
436 */
437 void
438set_string_option_direct_in_win(
439 win_T *wp,
440 char_u *name,
441 int opt_idx,
442 char_u *val,
443 int opt_flags,
444 int set_sid)
445{
446 win_T *save_curwin = curwin;
447
448 block_autocmds();
449 curwin = wp;
450 curbuf = curwin->w_buffer;
451 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
452 curwin = save_curwin;
453 curbuf = curwin->w_buffer;
454 unblock_autocmds();
455}
456
Dominique Pelle748b3082022-01-08 12:41:16 +0000457#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200458/*
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}
Dominique Pelle748b3082022-01-08 12:41:16 +0000481#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200482
483/*
484 * Set a string option to a new value, and handle the effects.
485 *
486 * Returns NULL on success or error message on error.
487 */
488 char *
489set_string_option(
490 int opt_idx,
491 char_u *value,
492 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
493{
494 char_u *s;
495 char_u **varp;
496 char_u *oldval;
497#if defined(FEAT_EVAL)
498 char_u *oldval_l = NULL;
499 char_u *oldval_g = NULL;
500 char_u *saved_oldval = NULL;
501 char_u *saved_oldval_l = NULL;
502 char_u *saved_oldval_g = NULL;
503 char_u *saved_newval = NULL;
504#endif
505 char *r = NULL;
506 int value_checked = FALSE;
507
508 if (is_hidden_option(opt_idx)) // don't set hidden option
509 return NULL;
510
Bram Moolenaar7f009df2020-03-16 20:27:38 +0100511 s = vim_strsave(value == NULL ? (char_u *)"" : value);
Bram Moolenaardac13472019-09-16 21:06:21 +0200512 if (s != NULL)
513 {
514 varp = (char_u **)get_option_varp_scope(opt_idx,
515 (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
516 ? (is_global_local_option(opt_idx)
517 ? OPT_GLOBAL : OPT_LOCAL)
518 : opt_flags);
519 oldval = *varp;
520#if defined(FEAT_EVAL)
521 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
522 {
523 oldval_l = *(char_u **)get_option_varp_scope(opt_idx, OPT_LOCAL);
524 oldval_g = *(char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
525 }
526#endif
527 *varp = s;
528
529#if defined(FEAT_EVAL)
530 if (!starting
531# ifdef FEAT_CRYPT
532 && !is_crypt_key_option(opt_idx)
533# endif
534 )
535 {
536 if (oldval_l != NULL)
537 saved_oldval_l = vim_strsave(oldval_l);
538 if (oldval_g != NULL)
539 saved_oldval_g = vim_strsave(oldval_g);
540 saved_oldval = vim_strsave(oldval);
541 saved_newval = vim_strsave(s);
542 }
543#endif
544 if ((r = did_set_string_option(opt_idx, varp, TRUE, oldval, NULL,
545 opt_flags, &value_checked)) == NULL)
546 did_set_option(opt_idx, opt_flags, TRUE, value_checked);
547
548#if defined(FEAT_EVAL)
549 // call autocommand after handling side effects
550 if (r == NULL)
551 trigger_optionsset_string(opt_idx, opt_flags,
552 saved_oldval, saved_oldval_l,
553 saved_oldval_g, saved_newval);
554 vim_free(saved_oldval);
555 vim_free(saved_oldval_l);
556 vim_free(saved_oldval_g);
557 vim_free(saved_newval);
558#endif
559 }
560 return r;
561}
562
563/*
564 * Return TRUE if "val" is a valid 'filetype' name.
565 * Also used for 'syntax' and 'keymap'.
566 */
567 static int
568valid_filetype(char_u *val)
569{
570 return valid_name(val, ".-_");
571}
572
573#ifdef FEAT_STL_OPT
574/*
575 * Check validity of options with the 'statusline' format.
576 * Return error message or NULL.
577 */
578 static char *
579check_stl_option(char_u *s)
580{
Bram Moolenaardac13472019-09-16 21:06:21 +0200581 int groupdepth = 0;
582 static char errbuf[80];
583
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100584 while (*s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200585 {
586 // Check for valid keys after % sequences
587 while (*s && *s != '%')
588 s++;
589 if (!*s)
590 break;
591 s++;
Bram Moolenaardac13472019-09-16 21:06:21 +0200592 if (*s == '%' || *s == STL_TRUNCMARK || *s == STL_MIDDLEMARK)
593 {
594 s++;
595 continue;
596 }
597 if (*s == ')')
598 {
599 s++;
600 if (--groupdepth < 0)
601 break;
602 continue;
603 }
604 if (*s == '-')
605 s++;
606 while (VIM_ISDIGIT(*s))
607 s++;
608 if (*s == STL_USER_HL)
609 continue;
610 if (*s == '.')
611 {
612 s++;
613 while (*s && VIM_ISDIGIT(*s))
614 s++;
615 }
616 if (*s == '(')
617 {
618 groupdepth++;
619 continue;
620 }
621 if (vim_strchr(STL_ALL, *s) == NULL)
622 {
623 return illegal_char(errbuf, *s);
624 }
625 if (*s == '{')
626 {
shadmansaleh30e3de22021-05-15 17:23:28 +0200627 int reevaluate = (*s == '%');
628
Bram Moolenaardac13472019-09-16 21:06:21 +0200629 s++;
shadmansaleh30e3de22021-05-15 17:23:28 +0200630 while ((*s != '}' || (reevaluate && s[-1] != '%')) && *s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200631 s++;
632 if (*s != '}')
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000633 return N_(e_unclosed_expression_sequence);
Bram Moolenaardac13472019-09-16 21:06:21 +0200634 }
635 }
Bram Moolenaardac13472019-09-16 21:06:21 +0200636 if (groupdepth != 0)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000637 return N_(e_unbalanced_groups);
Bram Moolenaardac13472019-09-16 21:06:21 +0200638 return NULL;
639}
640#endif
641
642/*
643 * Handle string options that need some action to perform when changed.
644 * Returns NULL for success, or an error message for an error.
645 */
646 char *
647did_set_string_option(
648 int opt_idx, // index in options[] table
649 char_u **varp, // pointer to the option variable
650 int new_value_alloced, // new value was allocated
651 char_u *oldval, // previous value of the option
652 char *errbuf, // buffer for errors, or NULL
653 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
654 int *value_checked) // value was checked to be save, no
655 // need to set P_INSECURE
656{
657 char *errmsg = NULL;
658 char_u *s, *p;
659 int did_chartab = FALSE;
660 char_u **gvarp;
661 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
662#ifdef FEAT_GUI
663 // set when changing an option that only requires a redraw in the GUI
664 int redraw_gui_only = FALSE;
665#endif
666 int value_changed = FALSE;
667#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
668 int did_swaptcap = FALSE;
669#endif
670
671 // Get the global option to compare with, otherwise we would have to check
672 // two values for all local options.
673 gvarp = (char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
674
675 // Disallow changing some options from secure mode
676 if ((secure
677#ifdef HAVE_SANDBOX
678 || sandbox != 0
679#endif
680 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +0000681 errmsg = e_not_allowed_here;
Bram Moolenaardac13472019-09-16 21:06:21 +0200682
683 // Check for a "normal" directory or file name in some options. Disallow a
684 // path separator (slash and/or backslash), wildcards and characters that
685 // are often illegal in a file name. Be more permissive if "secure" is off.
686 else if (((get_option_flags(opt_idx) & P_NFNAME)
687 && vim_strpbrk(*varp, (char_u *)(secure
688 ? "/\\*?[|;&<>\r\n" : "/\\*?[<>\r\n")) != NULL)
689 || ((get_option_flags(opt_idx) & P_NDNAME)
690 && vim_strpbrk(*varp, (char_u *)"*?[|;&<>\r\n") != NULL))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000691 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200692
693 // 'term'
694 else if (varp == &T_NAME)
695 {
696 if (T_NAME[0] == NUL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000697 errmsg = e_cannot_set_term_to_empty_string;
Bram Moolenaardac13472019-09-16 21:06:21 +0200698#ifdef FEAT_GUI
Bram Moolenaar5daa9112021-02-01 18:39:47 +0100699 else if (gui.in_use)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000700 errmsg = e_cannot_change_term_in_GUI;
Bram Moolenaardac13472019-09-16 21:06:21 +0200701 else if (term_is_gui(T_NAME))
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000702 errmsg = e_use_gui_to_start_GUI;
Bram Moolenaardac13472019-09-16 21:06:21 +0200703#endif
704 else if (set_termname(T_NAME) == FAIL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000705 errmsg = e_not_found_in_termcap;
Bram Moolenaardac13472019-09-16 21:06:21 +0200706 else
707 {
708 // Screen colors may have changed.
709 redraw_later_clear();
710
711 // Both 'term' and 'ttytype' point to T_NAME, only set the
712 // P_ALLOCED flag on 'term'.
713 opt_idx = findoption((char_u *)"term");
714 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
715 }
716 }
717
718 // 'backupcopy'
719 else if (gvarp == &p_bkc)
720 {
721 char_u *bkc = p_bkc;
722 unsigned int *flags = &bkc_flags;
723
724 if (opt_flags & OPT_LOCAL)
725 {
726 bkc = curbuf->b_p_bkc;
727 flags = &curbuf->b_bkc_flags;
728 }
729
730 if ((opt_flags & OPT_LOCAL) && *bkc == NUL)
731 // make the local value empty: use the global value
732 *flags = 0;
733 else
734 {
735 if (opt_strings_flags(bkc, p_bkc_values, flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000736 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200737 if ((((int)*flags & BKC_AUTO) != 0)
738 + (((int)*flags & BKC_YES) != 0)
739 + (((int)*flags & BKC_NO) != 0) != 1)
740 {
741 // Must have exactly one of "auto", "yes" and "no".
742 (void)opt_strings_flags(oldval, p_bkc_values, flags, TRUE);
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000743 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200744 }
745 }
746 }
747
748 // 'backupext' and 'patchmode'
749 else if (varp == &p_bex || varp == &p_pm)
750 {
751 if (STRCMP(*p_bex == '.' ? p_bex + 1 : p_bex,
752 *p_pm == '.' ? p_pm + 1 : p_pm) == 0)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000753 errmsg = N_(e_backupext_and_patchmode_are_equal);
Bram Moolenaardac13472019-09-16 21:06:21 +0200754 }
755#ifdef FEAT_LINEBREAK
756 // 'breakindentopt'
757 else if (varp == &curwin->w_p_briopt)
758 {
759 if (briopt_check(curwin) == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000760 errmsg = e_invalid_argument;
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000761 // list setting requires a redraw
762 if (curwin->w_briopt_list)
763 redraw_all_later(NOT_VALID);
Bram Moolenaardac13472019-09-16 21:06:21 +0200764 }
765#endif
766
767 // 'isident', 'iskeyword', 'isprint or 'isfname' option: refill g_chartab[]
768 // If the new option is invalid, use old value. 'lisp' option: refill
769 // g_chartab[] for '-' char
770 else if ( varp == &p_isi
771 || varp == &(curbuf->b_p_isk)
772 || varp == &p_isp
773 || varp == &p_isf)
774 {
775 if (init_chartab() == FAIL)
776 {
777 did_chartab = TRUE; // need to restore it below
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000778 errmsg = e_invalid_argument; // error in value
Bram Moolenaardac13472019-09-16 21:06:21 +0200779 }
780 }
781
782 // 'helpfile'
783 else if (varp == &p_hf)
784 {
785 // May compute new values for $VIM and $VIMRUNTIME
786 if (didset_vim)
787 {
788 vim_setenv((char_u *)"VIM", (char_u *)"");
789 didset_vim = FALSE;
790 }
791 if (didset_vimruntime)
792 {
793 vim_setenv((char_u *)"VIMRUNTIME", (char_u *)"");
794 didset_vimruntime = FALSE;
795 }
796 }
797
798#ifdef FEAT_SYN_HL
799 // 'cursorlineopt'
800 else if (varp == &curwin->w_p_culopt
801 || gvarp == &curwin->w_allbuf_opt.wo_culopt)
802 {
803 if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000804 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200805 }
806
807 // 'colorcolumn'
808 else if (varp == &curwin->w_p_cc)
809 errmsg = check_colorcolumn(curwin);
810#endif
811
812#ifdef FEAT_MULTI_LANG
813 // 'helplang'
814 else if (varp == &p_hlg)
815 {
816 // Check for "", "ab", "ab,cd", etc.
817 for (s = p_hlg; *s != NUL; s += 3)
818 {
819 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
820 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000821 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200822 break;
823 }
824 if (s[2] == NUL)
825 break;
826 }
827 }
828#endif
829
830 // 'highlight'
831 else if (varp == &p_hl)
832 {
833 if (highlight_changed() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000834 errmsg = e_invalid_argument; // invalid flags
Bram Moolenaardac13472019-09-16 21:06:21 +0200835 }
836
837 // 'nrformats'
838 else if (gvarp == &p_nf)
839 {
840 if (check_opt_strings(*varp, p_nf_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000841 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200842 }
843
844#ifdef FEAT_SESSION
845 // 'sessionoptions'
846 else if (varp == &p_ssop)
847 {
848 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000849 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200850 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
851 {
852 // Don't allow both "sesdir" and "curdir".
853 (void)opt_strings_flags(oldval, p_ssop_values, &ssop_flags, TRUE);
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000854 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200855 }
856 }
857 // 'viewoptions'
858 else if (varp == &p_vop)
859 {
860 if (opt_strings_flags(p_vop, p_ssop_values, &vop_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000861 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200862 }
863#endif
864
865 // 'scrollopt'
866 else if (varp == &p_sbo)
867 {
868 if (check_opt_strings(p_sbo, p_scbopt_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000869 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200870 }
871
872 // 'ambiwidth'
873 else if (varp == &p_ambw || varp == &p_emoji)
874 {
875 if (check_opt_strings(p_ambw, p_ambw_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000876 errmsg = e_invalid_argument;
Bram Moolenaareed9d462021-02-15 20:38:25 +0100877 else if (set_chars_option(curwin, &p_fcs) != NULL)
zeertzjq94358a12021-10-20 11:01:15 +0100878 errmsg = _(e_conflicts_with_value_of_fillchars);
Bram Moolenaareed9d462021-02-15 20:38:25 +0100879 else
880 {
881 tabpage_T *tp;
882 win_T *wp;
883
884 FOR_ALL_TAB_WINDOWS(tp, wp)
885 {
886 if (set_chars_option(wp, &wp->w_p_lcs) != NULL)
887 {
zeertzjq94358a12021-10-20 11:01:15 +0100888 errmsg = _(e_conflicts_with_value_of_listchars);
Bram Moolenaareed9d462021-02-15 20:38:25 +0100889 goto ambw_end;
890 }
891 }
892 }
893ambw_end:
894 {}
Bram Moolenaardac13472019-09-16 21:06:21 +0200895 }
896
897 // 'background'
898 else if (varp == &p_bg)
899 {
900 if (check_opt_strings(p_bg, p_bg_values, FALSE) == OK)
901 {
902#ifdef FEAT_EVAL
903 int dark = (*p_bg == 'd');
904#endif
905
906 init_highlight(FALSE, FALSE);
907
908#ifdef FEAT_EVAL
909 if (dark != (*p_bg == 'd')
910 && get_var_value((char_u *)"g:colors_name") != NULL)
911 {
912 // The color scheme must have set 'background' back to another
913 // value, that's not what we want here. Disable the color
914 // scheme and set the colors again.
915 do_unlet((char_u *)"g:colors_name", TRUE);
916 free_string_option(p_bg);
917 p_bg = vim_strsave((char_u *)(dark ? "dark" : "light"));
918 check_string_option(&p_bg);
919 init_highlight(FALSE, FALSE);
920 }
921#endif
Bram Moolenaarad431992021-05-03 20:40:38 +0200922#ifdef FEAT_TERMINAL
923 term_update_colors_all();
924#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200925 }
926 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000927 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200928 }
929
930 // 'wildmode'
931 else if (varp == &p_wim)
932 {
933 if (check_opt_wim() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000934 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200935 }
936
937 // 'wildoptions'
938 else if (varp == &p_wop)
939 {
940 if (check_opt_strings(p_wop, p_wop_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000941 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200942 }
943
944#ifdef FEAT_WAK
945 // 'winaltkeys'
946 else if (varp == &p_wak)
947 {
948 if (*p_wak == NUL
949 || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000950 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200951# ifdef FEAT_MENU
952# ifdef FEAT_GUI_MOTIF
953 else if (gui.in_use)
954 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
955# else
956# ifdef FEAT_GUI_GTK
957 else if (gui.in_use)
958 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
959# endif
960# endif
961# endif
962 }
963#endif
964
965 // 'eventignore'
966 else if (varp == &p_ei)
967 {
968 if (check_ei() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000969 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200970 }
971
972 // 'encoding', 'fileencoding', 'termencoding' and 'makeencoding'
973 else if (varp == &p_enc || gvarp == &p_fenc || varp == &p_tenc
974 || gvarp == &p_menc)
975 {
976 if (gvarp == &p_fenc)
977 {
978 if (!curbuf->b_p_ma && opt_flags != OPT_GLOBAL)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200979 errmsg = e_cannot_make_changes_modifiable_is_off;
Bram Moolenaardac13472019-09-16 21:06:21 +0200980 else if (vim_strchr(*varp, ',') != NULL)
981 // No comma allowed in 'fileencoding'; catches confusing it
982 // with 'fileencodings'.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000983 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200984 else
985 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200986 // May show a "+" in the title now.
987 redraw_titles();
Bram Moolenaardac13472019-09-16 21:06:21 +0200988 // Add 'fileencoding' to the swap file.
989 ml_setflags(curbuf);
990 }
991 }
992 if (errmsg == NULL)
993 {
994 // canonize the value, so that STRCMP() can be used on it
995 p = enc_canonize(*varp);
996 if (p != NULL)
997 {
998 vim_free(*varp);
999 *varp = p;
1000 }
1001 if (varp == &p_enc)
1002 {
1003 errmsg = mb_init();
Bram Moolenaardac13472019-09-16 21:06:21 +02001004 redraw_titles();
Bram Moolenaardac13472019-09-16 21:06:21 +02001005 }
1006 }
1007
1008#if defined(FEAT_GUI_GTK)
1009 if (errmsg == NULL && varp == &p_tenc && gui.in_use)
1010 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001011 // GTK uses only a single encoding, and that is UTF-8.
Bram Moolenaardac13472019-09-16 21:06:21 +02001012 if (STRCMP(p_tenc, "utf-8") != 0)
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001013 errmsg = e_cannot_be_changed_in_gtk_GUI;
Bram Moolenaardac13472019-09-16 21:06:21 +02001014 }
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 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001034 semsg(_(e_cannot_convert_between_str_and_str),
1035 p_tenc, p_enc);
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001036 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001037 }
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())
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001076 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001077 }
1078#endif
1079
1080#ifdef FEAT_KEYMAP
1081 else if (varp == &curbuf->b_p_keymap)
1082 {
1083 if (!valid_filetype(*varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001084 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001085 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)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001136 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001137 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;
Bram Moolenaardac13472019-09-16 21:06:21 +02001144 redraw_titles();
Bram Moolenaardac13472019-09-16 21:06:21 +02001145 // update flag in swap file
1146 ml_setflags(curbuf);
1147 // Redraw needed when switching to/from "mac": a CR in the text
1148 // will be displayed differently.
1149 if (get_fileformat(curbuf) == EOL_MAC || *oldval == 'm')
1150 redraw_curbuf_later(NOT_VALID);
1151 }
1152 }
1153
1154 // 'fileformats'
1155 else if (varp == &p_ffs)
1156 {
1157 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001158 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001159 else
1160 {
1161 // also change 'textauto'
1162 if (*p_ffs == NUL)
1163 p_ta = FALSE;
1164 else
1165 p_ta = TRUE;
1166 }
1167 }
1168
1169#if defined(FEAT_CRYPT)
1170 // 'cryptkey'
1171 else if (gvarp == &p_key)
1172 {
1173 // Make sure the ":set" command doesn't show the new value in the
1174 // history.
1175 remove_key_from_history();
1176
1177 if (STRCMP(curbuf->b_p_key, oldval) != 0)
1178 // Need to update the swapfile.
Bram Moolenaar76cb6832020-05-15 22:30:38 +02001179 {
Bram Moolenaardac13472019-09-16 21:06:21 +02001180 ml_set_crypt_key(curbuf, oldval,
1181 *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
Bram Moolenaar76cb6832020-05-15 22:30:38 +02001182 changed_internal();
1183 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001184 }
1185
1186 else if (gvarp == &p_cm)
1187 {
1188 if (opt_flags & OPT_LOCAL)
1189 p = curbuf->b_p_cm;
1190 else
1191 p = p_cm;
1192 if (check_opt_strings(p, p_cm_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001193 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001194 else if (crypt_self_test() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001195 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001196 else
1197 {
1198 // When setting the global value to empty, make it "zip".
1199 if (*p_cm == NUL)
1200 {
1201 if (new_value_alloced)
1202 free_string_option(p_cm);
1203 p_cm = vim_strsave((char_u *)"zip");
1204 new_value_alloced = TRUE;
1205 }
1206 // When using ":set cm=name" the local value is going to be empty.
1207 // Do that here, otherwise the crypt functions will still use the
1208 // local value.
1209 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1210 {
1211 free_string_option(curbuf->b_p_cm);
1212 curbuf->b_p_cm = empty_option;
1213 }
1214
1215 // Need to update the swapfile when the effective method changed.
1216 // Set "s" to the effective old value, "p" to the effective new
1217 // method and compare.
1218 if ((opt_flags & OPT_LOCAL) && *oldval == NUL)
1219 s = p_cm; // was previously using the global value
1220 else
1221 s = oldval;
1222 if (*curbuf->b_p_cm == NUL)
1223 p = p_cm; // is now using the global value
1224 else
1225 p = curbuf->b_p_cm;
1226 if (STRCMP(s, p) != 0)
1227 ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1228
1229 // If the global value changes need to update the swapfile for all
1230 // buffers using that value.
1231 if ((opt_flags & OPT_GLOBAL) && STRCMP(p_cm, oldval) != 0)
1232 {
1233 buf_T *buf;
1234
1235 FOR_ALL_BUFFERS(buf)
1236 if (buf != curbuf && *buf->b_p_cm == NUL)
1237 ml_set_crypt_key(buf, buf->b_p_key, oldval);
1238 }
1239 }
1240 }
1241#endif
1242
1243 // 'matchpairs'
1244 else if (gvarp == &p_mps)
1245 {
1246 if (has_mbyte)
1247 {
1248 for (p = *varp; *p != NUL; ++p)
1249 {
1250 int x2 = -1;
1251 int x3 = -1;
1252
1253 if (*p != NUL)
1254 p += mb_ptr2len(p);
1255 if (*p != NUL)
1256 x2 = *p++;
1257 if (*p != NUL)
1258 {
1259 x3 = mb_ptr2char(p);
1260 p += mb_ptr2len(p);
1261 }
1262 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
1263 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001264 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001265 break;
1266 }
1267 if (*p == NUL)
1268 break;
1269 }
1270 }
1271 else
1272 {
1273 // Check for "x:y,x:y"
1274 for (p = *varp; *p != NUL; p += 4)
1275 {
1276 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
1277 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001278 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001279 break;
1280 }
1281 if (p[3] == NUL)
1282 break;
1283 }
1284 }
1285 }
1286
Bram Moolenaardac13472019-09-16 21:06:21 +02001287 // 'comments'
1288 else if (gvarp == &p_com)
1289 {
1290 for (s = *varp; *s; )
1291 {
1292 while (*s && *s != ':')
1293 {
1294 if (vim_strchr((char_u *)COM_ALL, *s) == NULL
1295 && !VIM_ISDIGIT(*s) && *s != '-')
1296 {
1297 errmsg = illegal_char(errbuf, *s);
1298 break;
1299 }
1300 ++s;
1301 }
1302 if (*s++ == NUL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001303 errmsg = e_missing_colon;
Bram Moolenaardac13472019-09-16 21:06:21 +02001304 else if (*s == ',' || *s == NUL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001305 errmsg = e_zero_length_string;
Bram Moolenaardac13472019-09-16 21:06:21 +02001306 if (errmsg != NULL)
1307 break;
1308 while (*s && *s != ',')
1309 {
1310 if (*s == '\\' && s[1] != NUL)
1311 ++s;
1312 ++s;
1313 }
1314 s = skip_to_option_part(s);
1315 }
1316 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001317
Bram Moolenaareed9d462021-02-15 20:38:25 +01001318 // global 'listchars'
Bram Moolenaardac13472019-09-16 21:06:21 +02001319 else if (varp == &p_lcs)
1320 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001321 errmsg = set_chars_option(curwin, varp);
1322 if (errmsg == NULL)
1323 {
1324 tabpage_T *tp;
1325 win_T *wp;
1326
1327 // The current window is set to use the global 'listchars' value.
1328 // So clear the window-local value.
1329 if (!(opt_flags & OPT_GLOBAL))
1330 clear_string_option(&curwin->w_p_lcs);
1331 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar606efc72021-11-12 19:52:47 +00001332 // If no error was returned above, we don't expect an error
1333 // here, so ignore the return value.
1334 (void)set_chars_option(wp, &wp->w_p_lcs);
1335
Bram Moolenaareed9d462021-02-15 20:38:25 +01001336 redraw_all_later(NOT_VALID);
1337 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001338 }
1339
Bram Moolenaareed9d462021-02-15 20:38:25 +01001340 // local 'listchars'
1341 else if (varp == &curwin->w_p_lcs)
1342 errmsg = set_chars_option(curwin, varp);
1343
Bram Moolenaardac13472019-09-16 21:06:21 +02001344 // 'fillchars'
1345 else if (varp == &p_fcs)
1346 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001347 errmsg = set_chars_option(curwin, varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02001348 }
1349
1350#ifdef FEAT_CMDWIN
1351 // 'cedit'
1352 else if (varp == &p_cedit)
1353 {
1354 errmsg = check_cedit();
1355 }
1356#endif
1357
1358 // 'verbosefile'
1359 else if (varp == &p_vfile)
1360 {
1361 verbose_stop();
1362 if (*p_vfile != NUL && verbose_open() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001363 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001364 }
1365
1366#ifdef FEAT_VIMINFO
1367 // 'viminfo'
1368 else if (varp == &p_viminfo)
1369 {
1370 for (s = p_viminfo; *s;)
1371 {
1372 // Check it's a valid character
1373 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
1374 {
1375 errmsg = illegal_char(errbuf, *s);
1376 break;
1377 }
1378 if (*s == 'n') // name is always last one
1379 break;
1380 else if (*s == 'r') // skip until next ','
1381 {
1382 while (*++s && *s != ',')
1383 ;
1384 }
1385 else if (*s == '%')
1386 {
1387 // optional number
1388 while (vim_isdigit(*++s))
1389 ;
1390 }
1391 else if (*s == '!' || *s == 'h' || *s == 'c')
1392 ++s; // no extra chars
1393 else // must have a number
1394 {
1395 while (vim_isdigit(*++s))
1396 ;
1397
1398 if (!VIM_ISDIGIT(*(s - 1)))
1399 {
1400 if (errbuf != NULL)
1401 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001402 sprintf(errbuf,
1403 _(e_missing_number_after_angle_str_angle),
Bram Moolenaardac13472019-09-16 21:06:21 +02001404 transchar_byte(*(s - 1)));
1405 errmsg = errbuf;
1406 }
1407 else
1408 errmsg = "";
1409 break;
1410 }
1411 }
1412 if (*s == ',')
1413 ++s;
1414 else if (*s)
1415 {
1416 if (errbuf != NULL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001417 errmsg = e_missing_comma;
Bram Moolenaardac13472019-09-16 21:06:21 +02001418 else
1419 errmsg = "";
1420 break;
1421 }
1422 }
1423 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001424 errmsg = e_must_specify_a_value;
Bram Moolenaardac13472019-09-16 21:06:21 +02001425 }
1426#endif // FEAT_VIMINFO
1427
1428 // terminal options
1429 else if (istermoption_idx(opt_idx) && full_screen)
1430 {
1431 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
1432 if (varp == &T_CCO)
1433 {
1434 int colors = atoi((char *)T_CCO);
1435
1436 // Only reinitialize colors if t_Co value has really changed to
1437 // avoid expensive reload of colorscheme if t_Co is set to the
1438 // same value multiple times.
1439 if (colors != t_colors)
1440 {
1441 t_colors = colors;
1442 if (t_colors <= 1)
1443 {
1444 if (new_value_alloced)
1445 vim_free(T_CCO);
1446 T_CCO = empty_option;
1447 }
1448#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1449 if (is_term_win32())
1450 {
1451 swap_tcap();
1452 did_swaptcap = TRUE;
1453 }
1454#endif
1455 // We now have a different color setup, initialize it again.
1456 init_highlight(TRUE, FALSE);
1457 }
1458 }
1459 ttest(FALSE);
1460 if (varp == &T_ME)
1461 {
1462 out_str(T_ME);
1463 redraw_later(CLEAR);
1464#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
1465 // Since t_me has been set, this probably means that the user
1466 // wants to use this as default colors. Need to reset default
1467 // background/foreground colors.
1468# ifdef VIMDLL
1469 if (!gui.in_use && !gui.starting)
1470# endif
1471 mch_set_normal_colors();
1472#endif
1473 }
1474 if (varp == &T_BE && termcap_active)
1475 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02001476#ifdef FEAT_JOB_CHANNEL
1477 ch_log_output = TRUE;
1478#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02001479 if (*T_BE == NUL)
1480 // When clearing t_BE we assume the user no longer wants
1481 // bracketed paste, thus disable it by writing t_BD.
1482 out_str(T_BD);
1483 else
1484 out_str(T_BE);
1485 }
1486 }
1487
1488#ifdef FEAT_LINEBREAK
1489 // 'showbreak'
Bram Moolenaaree857022019-11-09 23:26:40 +01001490 else if (gvarp == &p_sbr)
Bram Moolenaardac13472019-09-16 21:06:21 +02001491 {
Bram Moolenaaree857022019-11-09 23:26:40 +01001492 for (s = *varp; *s; )
Bram Moolenaardac13472019-09-16 21:06:21 +02001493 {
1494 if (ptr2cells(s) != 1)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001495 errmsg = N_(e_showbreak_contains_unprintable_or_wide_character);
Bram Moolenaardac13472019-09-16 21:06:21 +02001496 MB_PTR_ADV(s);
1497 }
1498 }
1499#endif
1500
1501#ifdef FEAT_GUI
1502 // 'guifont'
1503 else if (varp == &p_guifont)
1504 {
1505 if (gui.in_use)
1506 {
1507 p = p_guifont;
1508# if defined(FEAT_GUI_GTK)
1509 // Put up a font dialog and let the user select a new value.
1510 // If this is cancelled go back to the old value but don't
1511 // give an error message.
1512 if (STRCMP(p, "*") == 0)
1513 {
1514 p = gui_mch_font_dialog(oldval);
1515
1516 if (new_value_alloced)
1517 free_string_option(p_guifont);
1518
1519 p_guifont = (p != NULL) ? p : vim_strsave(oldval);
1520 new_value_alloced = TRUE;
1521 }
1522# endif
1523 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
1524 {
1525# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
1526 if (STRCMP(p_guifont, "*") == 0)
1527 {
1528 // Dialog was cancelled: Keep the old value without giving
1529 // an error message.
1530 if (new_value_alloced)
1531 free_string_option(p_guifont);
1532 p_guifont = vim_strsave(oldval);
1533 new_value_alloced = TRUE;
1534 }
1535 else
1536# endif
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001537 errmsg = N_(e_invalid_fonts);
Bram Moolenaardac13472019-09-16 21:06:21 +02001538 }
1539 }
1540 redraw_gui_only = TRUE;
1541 }
1542# ifdef FEAT_XFONTSET
1543 else if (varp == &p_guifontset)
1544 {
1545 if (STRCMP(p_guifontset, "*") == 0)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001546 errmsg = N_(e_cant_select_fontset);
Bram Moolenaardac13472019-09-16 21:06:21 +02001547 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001548 errmsg = N_(e_invalid_fontset);
Bram Moolenaardac13472019-09-16 21:06:21 +02001549 redraw_gui_only = TRUE;
1550 }
1551# endif
1552 else if (varp == &p_guifontwide)
1553 {
1554 if (STRCMP(p_guifontwide, "*") == 0)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001555 errmsg = e_cant_select_wide_font;
Bram Moolenaardac13472019-09-16 21:06:21 +02001556 else if (gui_get_wide_font() == FAIL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001557 errmsg = e_invalid_wide_font;
Bram Moolenaardac13472019-09-16 21:06:21 +02001558 redraw_gui_only = TRUE;
1559 }
1560#endif
Dusan Popovic4eeedc02021-10-16 20:52:05 +01001561# if defined(FEAT_GUI_GTK)
1562 else if (varp == &p_guiligatures)
1563 {
1564 gui_set_ligatures();
1565 redraw_gui_only = TRUE;
1566 }
1567# endif
Bram Moolenaardac13472019-09-16 21:06:21 +02001568
1569#ifdef CURSOR_SHAPE
1570 // 'guicursor'
1571 else if (varp == &p_guicursor)
1572 errmsg = parse_shape_opt(SHAPE_CURSOR);
1573#endif
1574
1575#ifdef FEAT_MOUSESHAPE
1576 // 'mouseshape'
1577 else if (varp == &p_mouseshape)
1578 {
1579 errmsg = parse_shape_opt(SHAPE_MOUSE);
1580 update_mouseshape(-1);
1581 }
1582#endif
1583
1584#ifdef FEAT_PRINTER
1585 else if (varp == &p_popt)
1586 errmsg = parse_printoptions();
1587# if defined(FEAT_POSTSCRIPT)
1588 else if (varp == &p_pmfn)
1589 errmsg = parse_printmbfont();
1590# endif
1591#endif
1592
1593#ifdef FEAT_LANGMAP
1594 // 'langmap'
1595 else if (varp == &p_langmap)
1596 langmap_set();
1597#endif
1598
1599#ifdef FEAT_LINEBREAK
1600 // 'breakat'
1601 else if (varp == &p_breakat)
1602 fill_breakat_flags();
1603#endif
1604
Bram Moolenaardac13472019-09-16 21:06:21 +02001605 // 'titlestring' and 'iconstring'
1606 else if (varp == &p_titlestring || varp == &p_iconstring)
1607 {
Bram Moolenaar651fca82021-11-29 20:39:38 +00001608#ifdef FEAT_STL_OPT
Bram Moolenaardac13472019-09-16 21:06:21 +02001609 int flagval = (varp == &p_titlestring) ? STL_IN_TITLE : STL_IN_ICON;
1610
1611 // NULL => statusline syntax
1612 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
1613 stl_syntax |= flagval;
1614 else
1615 stl_syntax &= ~flagval;
Bram Moolenaar651fca82021-11-29 20:39:38 +00001616#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02001617 did_set_title();
1618 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001619
1620#ifdef FEAT_GUI
1621 // 'guioptions'
1622 else if (varp == &p_go)
1623 {
1624 gui_init_which_components(oldval);
1625 redraw_gui_only = TRUE;
1626 }
1627#endif
1628
1629#if defined(FEAT_GUI_TABLINE)
1630 // 'guitablabel'
1631 else if (varp == &p_gtl)
1632 {
1633 redraw_tabline = TRUE;
1634 redraw_gui_only = TRUE;
1635 }
1636 // 'guitabtooltip'
1637 else if (varp == &p_gtt)
1638 {
1639 redraw_gui_only = TRUE;
1640 }
1641#endif
1642
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001643#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +02001644 // 'ttymouse'
1645 else if (varp == &p_ttym)
1646 {
1647 // Switch the mouse off before changing the escape sequences used for
1648 // that.
1649 mch_setmouse(FALSE);
1650 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001651 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001652 else
1653 check_mouse_termcode();
1654 if (termcap_active)
1655 setmouse(); // may switch it on again
1656 }
1657#endif
1658
1659 // 'selection'
1660 else if (varp == &p_sel)
1661 {
1662 if (*p_sel == NUL
1663 || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001664 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001665 }
1666
1667 // 'selectmode'
1668 else if (varp == &p_slm)
1669 {
1670 if (check_opt_strings(p_slm, p_slm_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001671 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001672 }
1673
1674#ifdef FEAT_BROWSE
1675 // 'browsedir'
1676 else if (varp == &p_bsdir)
1677 {
1678 if (check_opt_strings(p_bsdir, p_bsdir_values, FALSE) != OK
1679 && !mch_isdir(p_bsdir))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001680 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001681 }
1682#endif
1683
1684 // 'keymodel'
1685 else if (varp == &p_km)
1686 {
1687 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001688 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001689 else
1690 {
1691 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
1692 km_startsel = (vim_strchr(p_km, 'a') != NULL);
1693 }
1694 }
1695
1696 // 'mousemodel'
1697 else if (varp == &p_mousem)
1698 {
1699 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001700 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001701#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
1702 else if (*p_mousem != *oldval)
1703 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
1704 // to create or delete the popup menus.
1705 gui_motif_update_mousemodel(root_menu);
1706#endif
1707 }
1708
1709 // 'switchbuf'
1710 else if (varp == &p_swb)
1711 {
1712 if (opt_strings_flags(p_swb, p_swb_values, &swb_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001713 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001714 }
1715
1716 // 'debug'
1717 else if (varp == &p_debug)
1718 {
1719 if (check_opt_strings(p_debug, p_debug_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001720 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001721 }
1722
1723 // 'display'
1724 else if (varp == &p_dy)
1725 {
1726 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001727 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001728 else
1729 (void)init_chartab();
1730
1731 }
1732
1733 // 'eadirection'
1734 else if (varp == &p_ead)
1735 {
1736 if (check_opt_strings(p_ead, p_ead_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001737 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001738 }
1739
1740#ifdef FEAT_CLIPBOARD
1741 // 'clipboard'
1742 else if (varp == &p_cb)
1743 errmsg = check_clipboard_option();
1744#endif
1745
1746#ifdef FEAT_SPELL
1747 // When 'spelllang' or 'spellfile' is set and there is a window for this
1748 // buffer in which 'spell' is set load the wordlists.
1749 else if (varp == &(curwin->w_s->b_p_spl)
1750 || varp == &(curwin->w_s->b_p_spf))
1751 {
1752 int is_spellfile = varp == &(curwin->w_s->b_p_spf);
1753
1754 if ((is_spellfile && !valid_spellfile(*varp))
Bram Moolenaarf154f3a2020-06-08 18:54:49 +02001755 || (!is_spellfile && !valid_spelllang(*varp)))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001756 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001757 else
1758 errmsg = did_set_spell_option(is_spellfile);
1759 }
1760 // When 'spellcapcheck' is set compile the regexp program.
1761 else if (varp == &(curwin->w_s->b_p_spc))
1762 {
1763 errmsg = compile_cap_prog(curwin->w_s);
1764 }
Bram Moolenaar362b44b2020-06-10 21:47:00 +02001765 // 'spelloptions'
1766 else if (varp == &(curwin->w_s->b_p_spo))
1767 {
1768 if (**varp != NUL && STRCMP("camel", *varp) != 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001769 errmsg = e_invalid_argument;
Bram Moolenaar362b44b2020-06-10 21:47:00 +02001770 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001771 // 'spellsuggest'
1772 else if (varp == &p_sps)
1773 {
1774 if (spell_check_sps() != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001775 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001776 }
1777 // 'mkspellmem'
1778 else if (varp == &p_msm)
1779 {
1780 if (spell_check_msm() != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001781 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001782 }
1783#endif
1784
1785 // When 'bufhidden' is set, check for valid value.
1786 else if (gvarp == &p_bh)
1787 {
1788 if (check_opt_strings(curbuf->b_p_bh, p_bufhidden_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001789 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001790 }
1791
1792 // When 'buftype' is set, check for valid value.
1793 else if (gvarp == &p_bt)
1794 {
1795 if (check_opt_strings(curbuf->b_p_bt, p_buftype_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001796 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001797 else
1798 {
1799 if (curwin->w_status_height)
1800 {
1801 curwin->w_redr_status = TRUE;
1802 redraw_later(VALID);
1803 }
1804 curbuf->b_help = (curbuf->b_p_bt[0] == 'h');
Bram Moolenaardac13472019-09-16 21:06:21 +02001805 redraw_titles();
Bram Moolenaardac13472019-09-16 21:06:21 +02001806 }
1807 }
1808
1809#ifdef FEAT_STL_OPT
1810 // 'statusline' or 'rulerformat'
1811 else if (gvarp == &p_stl || varp == &p_ruf)
1812 {
1813 int wid;
1814
1815 if (varp == &p_ruf) // reset ru_wid first
1816 ru_wid = 0;
1817 s = *varp;
1818 if (varp == &p_ruf && *s == '%')
1819 {
1820 // set ru_wid if 'ruf' starts with "%99("
1821 if (*++s == '-') // ignore a '-'
1822 s++;
1823 wid = getdigits(&s);
1824 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
1825 ru_wid = wid;
1826 else
1827 errmsg = check_stl_option(p_ruf);
1828 }
1829 // check 'statusline' only if it doesn't start with "%!"
1830 else if (varp == &p_ruf || s[0] != '%' || s[1] != '!')
1831 errmsg = check_stl_option(s);
1832 if (varp == &p_ruf && errmsg == NULL)
1833 comp_col();
1834 }
1835#endif
1836
1837 // check if it is a valid value for 'complete' -- Acevedo
1838 else if (gvarp == &p_cpt)
1839 {
1840 for (s = *varp; *s;)
1841 {
1842 while (*s == ',' || *s == ' ')
1843 s++;
1844 if (!*s)
1845 break;
1846 if (vim_strchr((char_u *)".wbuksid]tU", *s) == NULL)
1847 {
1848 errmsg = illegal_char(errbuf, *s);
1849 break;
1850 }
1851 if (*++s != NUL && *s != ',' && *s != ' ')
1852 {
1853 if (s[-1] == 'k' || s[-1] == 's')
1854 {
1855 // skip optional filename after 'k' and 's'
1856 while (*s && *s != ',' && *s != ' ')
1857 {
1858 if (*s == '\\' && s[1] != NUL)
1859 ++s;
1860 ++s;
1861 }
1862 }
1863 else
1864 {
1865 if (errbuf != NULL)
1866 {
1867 sprintf((char *)errbuf,
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001868 _(e_illegal_character_after_chr), *--s);
Bram Moolenaardac13472019-09-16 21:06:21 +02001869 errmsg = errbuf;
1870 }
1871 else
1872 errmsg = "";
1873 break;
1874 }
1875 }
1876 }
1877 }
1878
1879 // 'completeopt'
1880 else if (varp == &p_cot)
1881 {
1882 if (check_opt_strings(p_cot, p_cot_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001883 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001884 else
1885 completeopt_was_set();
1886 }
1887
1888#ifdef BACKSLASH_IN_FILENAME
1889 // 'completeslash'
1890 else if (gvarp == &p_csl)
1891 {
1892 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1893 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001894 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001895 }
1896#endif
1897
1898#ifdef FEAT_SIGNS
1899 // 'signcolumn'
1900 else if (varp == &curwin->w_p_scl)
1901 {
1902 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001903 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001904 // When changing the 'signcolumn' to or from 'number', recompute the
1905 // width of the number column if 'number' or 'relativenumber' is set.
1906 if (((*oldval == 'n' && *(oldval + 1) == 'u')
1907 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
1908 && (curwin->w_p_nu || curwin->w_p_rnu))
1909 curwin->w_nrwidth_line_count = 0;
1910 }
1911#endif
1912
1913
1914#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
1915 // 'toolbar'
1916 else if (varp == &p_toolbar)
1917 {
1918 if (opt_strings_flags(p_toolbar, p_toolbar_values,
1919 &toolbar_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001920 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001921 else
1922 {
1923 out_flush();
1924 gui_mch_show_toolbar((toolbar_flags &
1925 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
1926 }
1927 }
1928#endif
1929
1930#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
1931 // 'toolbariconsize': GTK+ 2 only
1932 else if (varp == &p_tbis)
1933 {
1934 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001935 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001936 else
1937 {
1938 out_flush();
1939 gui_mch_show_toolbar((toolbar_flags &
1940 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
1941 }
1942 }
1943#endif
1944
1945 // 'pastetoggle': translate key codes like in a mapping
1946 else if (varp == &p_pt)
1947 {
1948 if (*p_pt)
1949 {
Bram Moolenaar1e7b52a2019-10-13 16:59:08 +02001950 (void)replace_termcodes(p_pt, &p,
1951 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
Bram Moolenaardac13472019-09-16 21:06:21 +02001952 if (p != NULL)
1953 {
1954 if (new_value_alloced)
1955 free_string_option(p_pt);
1956 p_pt = p;
1957 new_value_alloced = TRUE;
1958 }
1959 }
1960 }
1961
1962 // 'backspace'
1963 else if (varp == &p_bs)
1964 {
1965 if (VIM_ISDIGIT(*p_bs))
1966 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02001967 if (*p_bs > '3' || p_bs[1] != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001968 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001969 }
1970 else if (check_opt_strings(p_bs, p_bs_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001971 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001972 }
1973 else if (varp == &p_bo)
1974 {
1975 if (opt_strings_flags(p_bo, p_bo_values, &bo_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001976 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001977 }
1978
1979 // 'tagcase'
1980 else if (gvarp == &p_tc)
1981 {
1982 unsigned int *flags;
1983
1984 if (opt_flags & OPT_LOCAL)
1985 {
1986 p = curbuf->b_p_tc;
1987 flags = &curbuf->b_tc_flags;
1988 }
1989 else
1990 {
1991 p = p_tc;
1992 flags = &tc_flags;
1993 }
1994
1995 if ((opt_flags & OPT_LOCAL) && *p == NUL)
1996 // make the local value empty: use the global value
1997 *flags = 0;
1998 else if (*p == NUL
1999 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002000 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002001 }
2002
2003 // 'casemap'
2004 else if (varp == &p_cmp)
2005 {
2006 if (opt_strings_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002007 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002008 }
2009
2010#ifdef FEAT_DIFF
2011 // 'diffopt'
2012 else if (varp == &p_dip)
2013 {
2014 if (diffopt_changed() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002015 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002016 }
2017#endif
2018
2019#ifdef FEAT_FOLDING
2020 // 'foldmethod'
2021 else if (gvarp == &curwin->w_allbuf_opt.wo_fdm)
2022 {
2023 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK
2024 || *curwin->w_p_fdm == NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002025 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002026 else
2027 {
2028 foldUpdateAll(curwin);
2029 if (foldmethodIsDiff(curwin))
2030 newFoldLevel();
2031 }
2032 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002033 // 'foldmarker'
2034 else if (gvarp == &curwin->w_allbuf_opt.wo_fmr)
2035 {
2036 p = vim_strchr(*varp, ',');
2037 if (p == NULL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002038 errmsg = e_comma_required;
Bram Moolenaardac13472019-09-16 21:06:21 +02002039 else if (p == *varp || p[1] == NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002040 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002041 else if (foldmethodIsMarker(curwin))
2042 foldUpdateAll(curwin);
2043 }
2044 // 'commentstring'
2045 else if (gvarp == &p_cms)
2046 {
2047 if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002048 errmsg = e_commentstring_must_be_empty_or_contain_str;
Bram Moolenaardac13472019-09-16 21:06:21 +02002049 }
2050 // 'foldopen'
2051 else if (varp == &p_fdo)
2052 {
2053 if (opt_strings_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002054 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002055 }
2056 // 'foldclose'
2057 else if (varp == &p_fcl)
2058 {
2059 if (check_opt_strings(p_fcl, p_fcl_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002060 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002061 }
2062 // 'foldignore'
2063 else if (gvarp == &curwin->w_allbuf_opt.wo_fdi)
2064 {
2065 if (foldmethodIsIndent(curwin))
2066 foldUpdateAll(curwin);
2067 }
2068#endif
2069
2070 // 'virtualedit'
Gary Johnson53ba05b2021-07-26 22:19:10 +02002071 else if (gvarp == &p_ve)
Bram Moolenaardac13472019-09-16 21:06:21 +02002072 {
Gary Johnson53ba05b2021-07-26 22:19:10 +02002073 char_u *ve = p_ve;
2074 unsigned int *flags = &ve_flags;
2075
2076 if (opt_flags & OPT_LOCAL)
Bram Moolenaardac13472019-09-16 21:06:21 +02002077 {
Gary Johnson51ad8502021-08-03 18:33:08 +02002078 ve = curwin->w_p_ve;
2079 flags = &curwin->w_ve_flags;
Gary Johnson53ba05b2021-07-26 22:19:10 +02002080 }
2081
2082 if ((opt_flags & OPT_LOCAL) && *ve == NUL)
2083 // make the local value empty: use the global value
2084 *flags = 0;
2085 else
2086 {
2087 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002088 errmsg = e_invalid_argument;
Gary Johnson53ba05b2021-07-26 22:19:10 +02002089 else if (STRCMP(p_ve, oldval) != 0)
2090 {
2091 // Recompute cursor position in case the new 've' setting
2092 // changes something.
2093 validate_virtcol();
2094 coladvance(curwin->w_virtcol);
2095 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002096 }
2097 }
2098
2099#if defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)
2100 else if (varp == &p_csqf)
2101 {
2102 if (p_csqf != NULL)
2103 {
2104 p = p_csqf;
2105 while (*p != NUL)
2106 {
2107 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
2108 || p[1] == NUL
2109 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
2110 || (p[2] != NUL && p[2] != ','))
2111 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002112 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002113 break;
2114 }
2115 else if (p[2] == NUL)
2116 break;
2117 else
2118 p += 3;
2119 }
2120 }
2121 }
2122#endif
2123
2124#ifdef FEAT_CINDENT
2125 // 'cinoptions'
2126 else if (gvarp == &p_cino)
2127 {
2128 // TODO: recognize errors
2129 parse_cino(curbuf);
2130 }
2131#endif
2132
2133#if defined(FEAT_RENDER_OPTIONS)
2134 // 'renderoptions'
2135 else if (varp == &p_rop)
2136 {
2137 if (!gui_mch_set_rendering_options(p_rop))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002138 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002139 }
2140#endif
2141
2142 else if (gvarp == &p_ft)
2143 {
2144 if (!valid_filetype(*varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002145 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002146 else
2147 {
2148 value_changed = STRCMP(oldval, *varp) != 0;
2149
2150 // Since we check the value, there is no need to set P_INSECURE,
2151 // even when the value comes from a modeline.
2152 *value_checked = TRUE;
2153 }
2154 }
2155
2156#ifdef FEAT_SYN_HL
2157 else if (gvarp == &p_syn)
2158 {
2159 if (!valid_filetype(*varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002160 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002161 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#endif
2171
2172#ifdef FEAT_TERMINAL
2173 // 'termwinkey'
2174 else if (varp == &curwin->w_p_twk)
2175 {
2176 if (*curwin->w_p_twk != NUL
2177 && string_to_key(curwin->w_p_twk, TRUE) == 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002178 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002179 }
2180 // 'termwinsize'
2181 else if (varp == &curwin->w_p_tws)
2182 {
2183 if (*curwin->w_p_tws != NUL)
2184 {
2185 p = skipdigits(curwin->w_p_tws);
2186 if (p == curwin->w_p_tws
2187 || (*p != 'x' && *p != '*')
2188 || *skipdigits(p + 1) != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002189 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002190 }
2191 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002192 // 'wincolor'
2193 else if (varp == &curwin->w_p_wcr)
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002194 term_update_wincolor(curwin);
Bram Moolenaardac13472019-09-16 21:06:21 +02002195# if defined(MSWIN)
2196 // 'termwintype'
2197 else if (varp == &p_twt)
2198 {
2199 if (check_opt_strings(*varp, p_twt_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002200 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002201 }
2202# endif
2203#endif
2204
2205#ifdef FEAT_VARTABS
2206 // 'varsofttabstop'
2207 else if (varp == &(curbuf->b_p_vsts))
2208 {
2209 char_u *cp;
2210
2211 if (!(*varp)[0] || ((*varp)[0] == '0' && !(*varp)[1]))
2212 {
2213 if (curbuf->b_p_vsts_array)
2214 {
2215 vim_free(curbuf->b_p_vsts_array);
2216 curbuf->b_p_vsts_array = 0;
2217 }
2218 }
2219 else
2220 {
2221 for (cp = *varp; *cp; ++cp)
2222 {
2223 if (vim_isdigit(*cp))
2224 continue;
2225 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
2226 continue;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002227 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002228 break;
2229 }
2230 if (errmsg == NULL)
2231 {
2232 int *oldarray = curbuf->b_p_vsts_array;
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002233 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Bram Moolenaardac13472019-09-16 21:06:21 +02002234 {
2235 if (oldarray)
2236 vim_free(oldarray);
2237 }
2238 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002239 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002240 }
2241 }
2242 }
2243
2244 // 'vartabstop'
2245 else if (varp == &(curbuf->b_p_vts))
2246 {
2247 char_u *cp;
2248
2249 if (!(*varp)[0] || ((*varp)[0] == '0' && !(*varp)[1]))
2250 {
2251 if (curbuf->b_p_vts_array)
2252 {
2253 vim_free(curbuf->b_p_vts_array);
2254 curbuf->b_p_vts_array = NULL;
2255 }
2256 }
2257 else
2258 {
2259 for (cp = *varp; *cp; ++cp)
2260 {
2261 if (vim_isdigit(*cp))
2262 continue;
2263 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
2264 continue;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002265 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002266 break;
2267 }
2268 if (errmsg == NULL)
2269 {
2270 int *oldarray = curbuf->b_p_vts_array;
2271
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002272 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Bram Moolenaardac13472019-09-16 21:06:21 +02002273 {
2274 vim_free(oldarray);
2275#ifdef FEAT_FOLDING
2276 if (foldmethodIsIndent(curwin))
2277 foldUpdateAll(curwin);
2278#endif
2279 }
2280 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002281 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002282 }
2283 }
2284 }
2285#endif
2286
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002287#ifdef FEAT_PROP_POPUP
Bram Moolenaardac13472019-09-16 21:06:21 +02002288 // 'previewpopup'
2289 else if (varp == &p_pvp)
2290 {
2291 if (parse_previewpopup(NULL) == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002292 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002293 }
2294# ifdef FEAT_QUICKFIX
2295 // 'completepopup'
2296 else if (varp == &p_cpp)
2297 {
2298 if (parse_completepopup(NULL) == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002299 errmsg = e_invalid_argument;
Bram Moolenaar447bfba2020-07-18 16:07:16 +02002300 else
2301 popup_close_info();
Bram Moolenaardac13472019-09-16 21:06:21 +02002302 }
2303# endif
2304#endif
2305
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002306#ifdef FEAT_EVAL
2307 // '*expr' options
2308 else if (
2309# ifdef FEAT_BEVAL
2310 varp == &p_bexpr ||
2311# endif
2312# ifdef FEAT_DIFF
2313 varp == &p_dex ||
2314# endif
2315# ifdef FEAT_FOLDING
2316 varp == &curwin->w_p_fde ||
Yegappan Lakshmanan27708e62021-12-26 21:54:43 +00002317 varp == &curwin->w_p_fdt ||
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002318# endif
2319 gvarp == &p_fex ||
2320# ifdef FEAT_FIND_ID
2321 gvarp == &p_inex ||
2322# endif
2323# ifdef FEAT_CINDENT
2324 gvarp == &p_inde ||
2325# endif
2326# ifdef FEAT_DIFF
2327 varp == &p_pex ||
2328# endif
2329# ifdef FEAT_POSTSCRIPT
2330 varp == &p_pexpr ||
2331# endif
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00002332 varp == &p_ccv)
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002333 {
2334 char_u **p_opt = NULL;
2335 char_u *name;
2336
2337 // If the option value starts with <SID> or s:, then replace that with
2338 // the script identifier.
2339# ifdef FEAT_BEVAL
2340 if (varp == &p_bexpr) // 'balloonexpr'
2341 p_opt = (opt_flags & OPT_LOCAL) ? &curbuf->b_p_bexpr : &p_bexpr;
2342# endif
2343# ifdef FEAT_DIFF
2344 if (varp == &p_dex) // 'diffexpr'
2345 p_opt = &p_dex;
2346# endif
2347# ifdef FEAT_FOLDING
Yegappan Lakshmanan27708e62021-12-26 21:54:43 +00002348 if (varp == &curwin->w_p_fde) // 'foldexpr'
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002349 p_opt = &curwin->w_p_fde;
Yegappan Lakshmanan27708e62021-12-26 21:54:43 +00002350 if (varp == &curwin->w_p_fdt) // 'foldtext'
2351 p_opt = &curwin->w_p_fdt;
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002352# endif
2353 if (gvarp == &p_fex) // 'formatexpr'
2354 p_opt = &curbuf->b_p_fex;
2355# ifdef FEAT_FIND_ID
2356 if (gvarp == &p_inex) // 'includeexpr'
2357 p_opt = &curbuf->b_p_inex;
2358# endif
2359# ifdef FEAT_CINDENT
2360 if (gvarp == &p_inde) // 'indentexpr'
2361 p_opt = &curbuf->b_p_inde;
2362# endif
2363# ifdef FEAT_DIFF
2364 if (varp == &p_pex) // 'patchexpr'
2365 p_opt = &p_pex;
2366# endif
2367# ifdef FEAT_POSTSCRIPT
2368 if (varp == &p_pexpr) // 'printexpr'
2369 p_opt = &p_pexpr;
2370# endif
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00002371 if (varp == &p_ccv) // 'charconvert'
2372 p_opt = &p_ccv;
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002373
2374 if (p_opt != NULL)
2375 {
2376 name = get_scriptlocal_funcname(*p_opt);
2377 if (name != NULL)
2378 {
2379 if (new_value_alloced)
2380 free_string_option(*p_opt);
2381 *p_opt = name;
2382 new_value_alloced = TRUE;
2383 }
2384 }
2385
2386# ifdef FEAT_FOLDING
2387 if (varp == &curwin->w_p_fde && foldmethodIsExpr(curwin))
2388 foldUpdateAll(curwin);
2389# endif
2390 }
2391#endif
2392
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002393#ifdef FEAT_COMPL_FUNC
2394 // 'completefunc'
2395 else if (gvarp == &p_cfu)
2396 {
2397 if (set_completefunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002398 errmsg = e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002399 }
2400
2401 // 'omnifunc'
2402 else if (gvarp == &p_ofu)
2403 {
2404 if (set_omnifunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002405 errmsg = e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002406 }
2407
2408 // 'thesaurusfunc'
2409 else if (gvarp == &p_tsrfu)
2410 {
2411 if (set_thesaurusfunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002412 errmsg = e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002413 }
2414#endif
2415
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00002416#if defined(FEAT_EVAL) && \
2417 (defined(FEAT_XIM) || defined(IME_WITHOUT_XIM) || defined(VIMDLL))
2418 // 'imactivatefunc'
2419 else if (gvarp == &p_imaf)
2420 {
2421 if (set_imactivatefunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002422 errmsg = e_invalid_argument;
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00002423 }
2424
2425 // 'imstatusfunc'
2426 else if (gvarp == &p_imsf)
2427 {
2428 if (set_imstatusfunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002429 errmsg = e_invalid_argument;
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00002430 }
2431#endif
2432
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00002433 // 'operatorfunc'
2434 else if (varp == &p_opfunc)
2435 {
2436 if (set_operatorfunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002437 errmsg = e_invalid_argument;
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00002438 }
2439
Bram Moolenaard43906d2020-07-20 21:31:32 +02002440#ifdef FEAT_QUICKFIX
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00002441 // 'quickfixtextfunc'
Bram Moolenaard43906d2020-07-20 21:31:32 +02002442 else if (varp == &p_qftf)
2443 {
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00002444 if (qf_process_qftf_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002445 errmsg = e_invalid_argument;
Bram Moolenaard43906d2020-07-20 21:31:32 +02002446 }
2447#endif
2448
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00002449#ifdef FEAT_EVAL
2450 // 'tagfunc'
2451 else if (gvarp == &p_tfu)
2452 {
2453 if (set_tagfunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002454 errmsg = e_invalid_argument;
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00002455 }
2456#endif
2457
Bram Moolenaardac13472019-09-16 21:06:21 +02002458 // Options that are a list of flags.
2459 else
2460 {
2461 p = NULL;
2462 if (varp == &p_ww) // 'whichwrap'
2463 p = (char_u *)WW_ALL;
2464 if (varp == &p_shm) // 'shortmess'
2465 p = (char_u *)SHM_ALL;
2466 else if (varp == &(p_cpo)) // 'cpoptions'
2467 p = (char_u *)CPO_ALL;
2468 else if (varp == &(curbuf->b_p_fo)) // 'formatoptions'
2469 p = (char_u *)FO_ALL;
2470#ifdef FEAT_CONCEAL
2471 else if (varp == &curwin->w_p_cocu) // 'concealcursor'
2472 p = (char_u *)COCU_ALL;
2473#endif
2474 else if (varp == &p_mouse) // 'mouse'
2475 {
Bram Moolenaardac13472019-09-16 21:06:21 +02002476 p = (char_u *)MOUSE_ALL;
Bram Moolenaardac13472019-09-16 21:06:21 +02002477 }
2478#if defined(FEAT_GUI)
2479 else if (varp == &p_go) // 'guioptions'
2480 p = (char_u *)GO_ALL;
2481#endif
2482 if (p != NULL)
2483 {
2484 for (s = *varp; *s; ++s)
2485 if (vim_strchr(p, *s) == NULL)
2486 {
2487 errmsg = illegal_char(errbuf, *s);
2488 break;
2489 }
2490 }
2491 }
2492
2493 // If error detected, restore the previous value.
2494 if (errmsg != NULL)
2495 {
2496 if (new_value_alloced)
2497 free_string_option(*varp);
2498 *varp = oldval;
2499 // When resetting some values, need to act on it.
2500 if (did_chartab)
2501 (void)init_chartab();
2502 if (varp == &p_hl)
2503 (void)highlight_changed();
2504 }
2505 else
2506 {
2507#ifdef FEAT_EVAL
2508 // Remember where the option was set.
2509 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
2510#endif
2511 // Free string options that are in allocated memory.
2512 // Use "free_oldval", because recursiveness may change the flags under
2513 // our fingers (esp. init_highlight()).
2514 if (free_oldval)
2515 free_string_option(oldval);
2516 if (new_value_alloced)
2517 set_option_flag(opt_idx, P_ALLOCED);
2518 else
2519 clear_option_flag(opt_idx, P_ALLOCED);
2520
2521 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
2522 && is_global_local_option(opt_idx))
2523 {
2524 // global option with local value set to use global value; free
2525 // the local value and make it empty
2526 p = get_option_varp_scope(opt_idx, OPT_LOCAL);
2527 free_string_option(*(char_u **)p);
2528 *(char_u **)p = empty_option;
2529 }
2530
2531 // May set global value for local option.
2532 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
2533 set_string_option_global(opt_idx, varp);
2534
2535 // Trigger the autocommand only after setting the flags.
2536#ifdef FEAT_SYN_HL
2537 // When 'syntax' is set, load the syntax of that name
2538 if (varp == &(curbuf->b_p_syn))
2539 {
2540 static int syn_recursive = 0;
2541
2542 ++syn_recursive;
2543 // Only pass TRUE for "force" when the value changed or not used
2544 // recursively, to avoid endless recurrence.
2545 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
2546 value_changed || syn_recursive == 1, curbuf);
2547 curbuf->b_flags |= BF_SYN_SET;
2548 --syn_recursive;
2549 }
2550#endif
2551 else if (varp == &(curbuf->b_p_ft))
2552 {
2553 // 'filetype' is set, trigger the FileType autocommand.
2554 // Skip this when called from a modeline and the filetype was
2555 // already set to this value.
2556 if (!(opt_flags & OPT_MODELINE) || value_changed)
2557 {
2558 static int ft_recursive = 0;
2559 int secure_save = secure;
2560
2561 // Reset the secure flag, since the value of 'filetype' has
2562 // been checked to be safe.
2563 secure = 0;
2564
2565 ++ft_recursive;
2566 did_filetype = TRUE;
2567 // Only pass TRUE for "force" when the value changed or not
2568 // used recursively, to avoid endless recurrence.
2569 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2570 value_changed || ft_recursive == 1, curbuf);
2571 --ft_recursive;
2572 // Just in case the old "curbuf" is now invalid.
2573 if (varp != &(curbuf->b_p_ft))
2574 varp = NULL;
2575
2576 secure = secure_save;
2577 }
2578 }
2579#ifdef FEAT_SPELL
2580 if (varp == &(curwin->w_s->b_p_spl))
2581 {
2582 char_u fname[200];
2583 char_u *q = curwin->w_s->b_p_spl;
2584
2585 // Skip the first name if it is "cjk".
2586 if (STRNCMP(q, "cjk,", 4) == 0)
2587 q += 4;
2588
2589 // Source the spell/LANG.vim in 'runtimepath'.
2590 // They could set 'spellcapcheck' depending on the language.
2591 // Use the first name in 'spelllang' up to '_region' or
2592 // '.encoding'.
2593 for (p = q; *p != NUL; ++p)
2594 if (!ASCII_ISALNUM(*p) && *p != '-')
2595 break;
2596 if (p > q)
2597 {
2598 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
2599 (int)(p - q), q);
2600 source_runtime(fname, DIP_ALL);
2601 }
2602 }
2603#endif
2604 }
2605
Bram Moolenaardac13472019-09-16 21:06:21 +02002606 if (varp == &p_mouse)
2607 {
Bram Moolenaardac13472019-09-16 21:06:21 +02002608 if (*p_mouse == NUL)
2609 mch_setmouse(FALSE); // switch mouse off
2610 else
Bram Moolenaardac13472019-09-16 21:06:21 +02002611 setmouse(); // in case 'mouse' changed
2612 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002613
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002614#if defined(FEAT_LUA) || defined(PROTO)
2615 if (varp == &p_rtp)
2616 update_package_paths_in_lua();
2617#endif
2618
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00002619#if defined(FEAT_LINEBREAK)
2620 // Changing Formatlistpattern when briopt includes the list setting:
2621 // redraw
2622 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
2623 && curwin->w_briopt_list)
2624 redraw_all_later(NOT_VALID);
2625#endif
2626
Bram Moolenaardac13472019-09-16 21:06:21 +02002627 if (curwin->w_curswant != MAXCOL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002628 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02002629 curwin->w_set_curswant = TRUE;
2630
Bram Moolenaar37294bd2021-03-10 13:40:08 +01002631 if ((opt_flags & OPT_NO_REDRAW) == 0)
2632 {
Bram Moolenaardac13472019-09-16 21:06:21 +02002633#ifdef FEAT_GUI
Bram Moolenaar37294bd2021-03-10 13:40:08 +01002634 // check redraw when it's not a GUI option or the GUI is active.
2635 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02002636#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01002637 check_redraw(get_option_flags(opt_idx));
2638 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002639
2640#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2641 if (did_swaptcap)
2642 {
2643 set_termname((char_u *)"win32");
2644 init_highlight(TRUE, FALSE);
2645 }
2646#endif
2647
2648 return errmsg;
2649}
2650
2651/*
2652 * Check an option that can be a range of string values.
2653 *
2654 * Return OK for correct value, FAIL otherwise.
2655 * Empty is always OK.
2656 */
2657 static int
2658check_opt_strings(
2659 char_u *val,
2660 char **values,
2661 int list) // when TRUE: accept a list of values
2662{
2663 return opt_strings_flags(val, values, NULL, list);
2664}
2665
2666/*
2667 * Handle an option that can be a range of string values.
2668 * Set a flag in "*flagp" for each string present.
2669 *
2670 * Return OK for correct value, FAIL otherwise.
2671 * Empty is always OK.
2672 */
2673 static int
2674opt_strings_flags(
2675 char_u *val, // new value
2676 char **values, // array of valid string values
2677 unsigned *flagp,
2678 int list) // when TRUE: accept a list of values
2679{
2680 int i;
2681 int len;
2682 unsigned new_flags = 0;
2683
2684 while (*val)
2685 {
2686 for (i = 0; ; ++i)
2687 {
2688 if (values[i] == NULL) // val not found in values[]
2689 return FAIL;
2690
2691 len = (int)STRLEN(values[i]);
2692 if (STRNCMP(values[i], val, len) == 0
2693 && ((list && val[len] == ',') || val[len] == NUL))
2694 {
2695 val += len + (val[len] == ',');
2696 new_flags |= (1 << i);
2697 break; // check next item in val list
2698 }
2699 }
2700 }
2701 if (flagp != NULL)
2702 *flagp = new_flags;
2703
2704 return OK;
2705}
2706
2707/*
2708 * return OK if "p" is a valid fileformat name, FAIL otherwise.
2709 */
2710 int
2711check_ff_value(char_u *p)
2712{
2713 return check_opt_strings(p, p_ff_values, FALSE);
2714}