blob: 24896cc233608d4850e810a18943ed1fc4d73f45 [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",
LemonBoy77771d32022-04-13 11:47:25 +010023 "spell", "term", "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};
Luuk van Baal13ece2a2022-10-03 15:28:08 +010049static char *(p_spk_values[]) = {"cursor", "screen", "topline", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020050static char *(p_tc_values[]) = {"followic", "ignore", "match", "followscs", "smart", NULL};
51#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
52static char *(p_toolbar_values[]) = {"text", "icons", "tooltips", "horiz", NULL};
53#endif
54#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
55static char *(p_tbis_values[]) = {"tiny", "small", "medium", "large", "huge", "giant", NULL};
56#endif
Bram Moolenaara1cb1d12019-10-17 23:00:07 +020057#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +020058static char *(p_ttym_values[]) = {"xterm", "xterm2", "dec", "netterm", "jsbterm", "pterm", "urxvt", "sgr", NULL};
59#endif
Gary Johnson53ba05b2021-07-26 22:19:10 +020060static char *(p_ve_values[]) = {"block", "insert", "all", "onemore", "none", "NONE", NULL};
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000061static char *(p_wop_values[]) = {"fuzzy", "tagfile", "pum", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020062#ifdef FEAT_WAK
63static char *(p_wak_values[]) = {"yes", "menu", "no", NULL};
64#endif
65static char *(p_mousem_values[]) = {"extend", "popup", "popup_setpos", "mac", NULL};
66static char *(p_sel_values[]) = {"inclusive", "exclusive", "old", NULL};
67static char *(p_slm_values[]) = {"mouse", "key", "cmd", NULL};
68static char *(p_km_values[]) = {"startsel", "stopsel", NULL};
69#ifdef FEAT_BROWSE
70static char *(p_bsdir_values[]) = {"current", "last", "buffer", NULL};
71#endif
72static char *(p_scbopt_values[]) = {"ver", "hor", "jump", NULL};
73static char *(p_debug_values[]) = {"msg", "throw", "beep", NULL};
74static char *(p_ead_values[]) = {"both", "ver", "hor", NULL};
75static char *(p_buftype_values[]) = {"nofile", "nowrite", "quickfix", "help", "terminal", "acwrite", "prompt", "popup", NULL};
76static char *(p_bufhidden_values[]) = {"hide", "unload", "delete", "wipe", NULL};
Bram Moolenaaraa0489e2020-04-17 19:41:21 +020077static char *(p_bs_values[]) = {"indent", "eol", "start", "nostop", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020078#ifdef FEAT_FOLDING
79static char *(p_fdm_values[]) = {"manual", "expr", "marker", "indent", "syntax",
80# ifdef FEAT_DIFF
81 "diff",
82# endif
83 NULL};
84static char *(p_fcl_values[]) = {"all", NULL};
85#endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +020086static char *(p_cot_values[]) = {"menu", "menuone", "longest", "preview", "popup", "popuphidden", "noinsert", "noselect", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020087#ifdef BACKSLASH_IN_FILENAME
88static char *(p_csl_values[]) = {"slash", "backslash", NULL};
89#endif
90#ifdef FEAT_SIGNS
91static char *(p_scl_values[]) = {"yes", "no", "auto", "number", NULL};
92#endif
93#if defined(MSWIN) && defined(FEAT_TERMINAL)
94static char *(p_twt_values[]) = {"winpty", "conpty", "", NULL};
95#endif
96
97static int check_opt_strings(char_u *val, char **values, int list);
98static int opt_strings_flags(char_u *val, char **values, unsigned *flagp, int list);
99
100/*
101 * After setting various option values: recompute variables that depend on
102 * option values.
103 */
104 void
105didset_string_options(void)
106{
107 (void)opt_strings_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
108 (void)opt_strings_flags(p_bkc, p_bkc_values, &bkc_flags, TRUE);
109 (void)opt_strings_flags(p_bo, p_bo_values, &bo_flags, TRUE);
110#ifdef FEAT_SESSION
111 (void)opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE);
112 (void)opt_strings_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
113#endif
114#ifdef FEAT_FOLDING
115 (void)opt_strings_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
116#endif
117 (void)opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE);
118 (void)opt_strings_flags(p_tc, p_tc_values, &tc_flags, FALSE);
119 (void)opt_strings_flags(p_ve, p_ve_values, &ve_flags, TRUE);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +0200120#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +0200121 (void)opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE);
122#endif
123#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
124 (void)opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags, TRUE);
125#endif
126#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
127 (void)opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE);
128#endif
Sean Dewar39c46b42022-05-12 17:44:29 +0100129 (void)opt_strings_flags(p_swb, p_swb_values, &swb_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200130}
131
132#if defined(FEAT_EVAL)
133/*
134 * Trigger the OptionSet autocommand.
135 * "opt_idx" is the index of the option being set.
136 * "opt_flags" can be OPT_LOCAL etc.
137 * "oldval" the old value
138 * "oldval_l" the old local value (only non-NULL if global and local value
139 * are set)
140 * "oldval_g" the old global value (only non-NULL if global and local value
141 * are set)
142 * "newval" the new value
143 */
144 void
145trigger_optionsset_string(
146 int opt_idx,
147 int opt_flags,
148 char_u *oldval,
149 char_u *oldval_l,
150 char_u *oldval_g,
151 char_u *newval)
152{
153 // Don't do this recursively.
154 if (oldval != NULL && newval != NULL
155 && *get_vim_var_str(VV_OPTION_TYPE) == NUL)
156 {
157 char_u buf_type[7];
158
159 sprintf((char *)buf_type, "%s",
160 (opt_flags & OPT_LOCAL) ? "local" : "global");
161 set_vim_var_string(VV_OPTION_OLD, oldval, -1);
162 set_vim_var_string(VV_OPTION_NEW, newval, -1);
163 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
164 if (opt_flags & OPT_LOCAL)
165 {
166 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
167 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
168 }
169 if (opt_flags & OPT_GLOBAL)
170 {
171 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
172 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval, -1);
173 }
174 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
175 {
176 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
177 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval_l, -1);
178 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval_g, -1);
179 }
180 if (opt_flags & OPT_MODELINE)
181 {
182 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
183 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
184 }
185 apply_autocmds(EVENT_OPTIONSET,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000186 get_option_fullname(opt_idx), NULL, FALSE,
Bram Moolenaardac13472019-09-16 21:06:21 +0200187 NULL);
188 reset_v_option_vars();
189 }
190}
191#endif
192
193 static char *
194illegal_char(char *errbuf, int c)
195{
196 if (errbuf == NULL)
197 return "";
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000198 sprintf((char *)errbuf, _(e_illegal_character_str), (char *)transchar(c));
Bram Moolenaardac13472019-09-16 21:06:21 +0200199 return errbuf;
200}
201
202/*
203 * Check string options in a buffer for NULL value.
204 */
205 void
206check_buf_options(buf_T *buf)
207{
208 check_string_option(&buf->b_p_bh);
209 check_string_option(&buf->b_p_bt);
210 check_string_option(&buf->b_p_fenc);
211 check_string_option(&buf->b_p_ff);
212#ifdef FEAT_FIND_ID
213 check_string_option(&buf->b_p_def);
214 check_string_option(&buf->b_p_inc);
215# ifdef FEAT_EVAL
216 check_string_option(&buf->b_p_inex);
217# endif
218#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +0100219#if defined(FEAT_EVAL)
Bram Moolenaardac13472019-09-16 21:06:21 +0200220 check_string_option(&buf->b_p_inde);
221 check_string_option(&buf->b_p_indk);
222#endif
223#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
224 check_string_option(&buf->b_p_bexpr);
225#endif
226#if defined(FEAT_CRYPT)
227 check_string_option(&buf->b_p_cm);
228#endif
229 check_string_option(&buf->b_p_fp);
230#if defined(FEAT_EVAL)
231 check_string_option(&buf->b_p_fex);
232#endif
233#ifdef FEAT_CRYPT
234 check_string_option(&buf->b_p_key);
235#endif
236 check_string_option(&buf->b_p_kp);
237 check_string_option(&buf->b_p_mps);
238 check_string_option(&buf->b_p_fo);
239 check_string_option(&buf->b_p_flp);
240 check_string_option(&buf->b_p_isk);
Bram Moolenaardac13472019-09-16 21:06:21 +0200241 check_string_option(&buf->b_p_com);
Bram Moolenaardac13472019-09-16 21:06:21 +0200242#ifdef FEAT_FOLDING
243 check_string_option(&buf->b_p_cms);
244#endif
245 check_string_option(&buf->b_p_nf);
Bram Moolenaardac13472019-09-16 21:06:21 +0200246 check_string_option(&buf->b_p_qe);
Bram Moolenaardac13472019-09-16 21:06:21 +0200247#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
Bram Moolenaardac13472019-09-16 21:06:21 +0200257 check_string_option(&buf->b_p_sua);
Bram Moolenaardac13472019-09-16 21:06:21 +0200258 check_string_option(&buf->b_p_cink);
259 check_string_option(&buf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +0100260 check_string_option(&buf->b_p_cinsd);
Bram Moolenaardac13472019-09-16 21:06:21 +0200261 parse_cino(buf);
Bram Moolenaardac13472019-09-16 21:06:21 +0200262 check_string_option(&buf->b_p_ft);
Bram Moolenaardac13472019-09-16 21:06:21 +0200263 check_string_option(&buf->b_p_cinw);
Bram Moolenaardac13472019-09-16 21:06:21 +0200264 check_string_option(&buf->b_p_cpt);
265#ifdef FEAT_COMPL_FUNC
266 check_string_option(&buf->b_p_cfu);
267 check_string_option(&buf->b_p_ofu);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +0100268 check_string_option(&buf->b_p_tsrfu);
Bram Moolenaardac13472019-09-16 21:06:21 +0200269#endif
270#ifdef FEAT_EVAL
271 check_string_option(&buf->b_p_tfu);
272#endif
273#ifdef FEAT_KEYMAP
274 check_string_option(&buf->b_p_keymap);
275#endif
276#ifdef FEAT_QUICKFIX
277 check_string_option(&buf->b_p_gp);
278 check_string_option(&buf->b_p_mp);
279 check_string_option(&buf->b_p_efm);
280#endif
281 check_string_option(&buf->b_p_ep);
282 check_string_option(&buf->b_p_path);
283 check_string_option(&buf->b_p_tags);
284 check_string_option(&buf->b_p_tc);
285 check_string_option(&buf->b_p_dict);
286 check_string_option(&buf->b_p_tsr);
Bram Moolenaardac13472019-09-16 21:06:21 +0200287 check_string_option(&buf->b_p_lw);
Bram Moolenaardac13472019-09-16 21:06:21 +0200288 check_string_option(&buf->b_p_bkc);
289 check_string_option(&buf->b_p_menc);
290#ifdef FEAT_VARTABS
291 check_string_option(&buf->b_p_vsts);
292 check_string_option(&buf->b_p_vts);
293#endif
294}
295
296/*
297 * Free the string allocated for an option.
298 * Checks for the string being empty_option. This may happen if we're out of
299 * memory, vim_strsave() returned NULL, which was replaced by empty_option by
300 * check_options().
301 * Does NOT check for P_ALLOCED flag!
302 */
303 void
304free_string_option(char_u *p)
305{
306 if (p != empty_option)
307 vim_free(p);
308}
309
310 void
311clear_string_option(char_u **pp)
312{
313 if (*pp != empty_option)
314 vim_free(*pp);
315 *pp = empty_option;
316}
317
318 void
319check_string_option(char_u **pp)
320{
321 if (*pp == NULL)
322 *pp = empty_option;
323}
324
325/*
326 * Set global value for string option when it's a local option.
327 */
328 static void
329set_string_option_global(
330 int opt_idx, // option index
331 char_u **varp) // pointer to option variable
332{
333 char_u **p, *s;
334
335 // the global value is always allocated
336 if (is_window_local_option(opt_idx))
337 p = (char_u **)GLOBAL_WO(varp);
338 else
339 p = (char_u **)get_option_var(opt_idx);
340 if (!is_global_option(opt_idx)
341 && p != varp
342 && (s = vim_strsave(*varp)) != NULL)
343 {
344 free_string_option(*p);
345 *p = s;
346 }
347}
348
349/*
350 * Set a string option to a new value (without checking the effect).
351 * The string is copied into allocated memory.
352 * if ("opt_idx" == -1) "name" is used, otherwise "opt_idx" is used.
353 * When "set_sid" is zero set the scriptID to current_sctx.sc_sid. When
354 * "set_sid" is SID_NONE don't set the scriptID. Otherwise set the scriptID to
355 * "set_sid".
356 */
357 void
358set_string_option_direct(
359 char_u *name,
360 int opt_idx,
361 char_u *val,
362 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
363 int set_sid UNUSED)
364{
365 char_u *s;
366 char_u **varp;
367 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
368 int idx = opt_idx;
369
370 if (idx == -1) // use name
371 {
372 idx = findoption(name);
373 if (idx < 0) // not found (should not happen)
374 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000375 semsg(_(e_internal_error_str), "set_string_option_direct()");
Bram Moolenaardac13472019-09-16 21:06:21 +0200376 siemsg(_("For option %s"), name);
377 return;
378 }
379 }
380
381 if (is_hidden_option(idx)) // can't set hidden option
382 return;
383
384 s = vim_strsave(val);
385 if (s != NULL)
386 {
387 varp = (char_u **)get_option_varp_scope(idx,
388 both ? OPT_LOCAL : opt_flags);
389 if ((opt_flags & OPT_FREE) && (get_option_flags(idx) & P_ALLOCED))
390 free_string_option(*varp);
391 *varp = s;
392
393 // For buffer/window local option may also set the global value.
394 if (both)
395 set_string_option_global(idx, varp);
396
397 set_option_flag(idx, P_ALLOCED);
398
399 // When setting both values of a global option with a local value,
400 // make the local value empty, so that the global value is used.
401 if (is_global_local_option(idx) && both)
402 {
403 free_string_option(*varp);
404 *varp = empty_option;
405 }
406# ifdef FEAT_EVAL
407 if (set_sid != SID_NONE)
408 {
409 sctx_T script_ctx;
410
411 if (set_sid == 0)
412 script_ctx = current_sctx;
413 else
414 {
415 script_ctx.sc_sid = set_sid;
416 script_ctx.sc_seq = 0;
417 script_ctx.sc_lnum = 0;
418 script_ctx.sc_version = 1;
419 }
420 set_option_sctx_idx(idx, opt_flags, script_ctx);
421 }
422# endif
423 }
424}
425
426/*
427 * Like set_string_option_direct(), but for a window-local option in "wp".
428 * Blocks autocommands to avoid the old curwin becoming invalid.
429 */
430 void
431set_string_option_direct_in_win(
432 win_T *wp,
433 char_u *name,
434 int opt_idx,
435 char_u *val,
436 int opt_flags,
437 int set_sid)
438{
439 win_T *save_curwin = curwin;
440
441 block_autocmds();
442 curwin = wp;
443 curbuf = curwin->w_buffer;
444 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
445 curwin = save_curwin;
446 curbuf = curwin->w_buffer;
447 unblock_autocmds();
448}
449
Dominique Pelle748b3082022-01-08 12:41:16 +0000450#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200451/*
452 * Like set_string_option_direct(), but for a buffer-local option in "buf".
453 * Blocks autocommands to avoid the old curbuf becoming invalid.
454 */
455 void
456set_string_option_direct_in_buf(
457 buf_T *buf,
458 char_u *name,
459 int opt_idx,
460 char_u *val,
461 int opt_flags,
462 int set_sid)
463{
464 buf_T *save_curbuf = curbuf;
465
466 block_autocmds();
467 curbuf = buf;
468 curwin->w_buffer = curbuf;
469 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
470 curbuf = save_curbuf;
471 curwin->w_buffer = curbuf;
472 unblock_autocmds();
473}
Dominique Pelle748b3082022-01-08 12:41:16 +0000474#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200475
476/*
477 * Set a string option to a new value, and handle the effects.
478 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100479 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaardac13472019-09-16 21:06:21 +0200480 */
481 char *
482set_string_option(
483 int opt_idx,
484 char_u *value,
485 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
486{
487 char_u *s;
488 char_u **varp;
489 char_u *oldval;
490#if defined(FEAT_EVAL)
491 char_u *oldval_l = NULL;
492 char_u *oldval_g = NULL;
493 char_u *saved_oldval = NULL;
494 char_u *saved_oldval_l = NULL;
495 char_u *saved_oldval_g = NULL;
496 char_u *saved_newval = NULL;
497#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100498 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +0200499 int value_checked = FALSE;
500
501 if (is_hidden_option(opt_idx)) // don't set hidden option
502 return NULL;
503
Bram Moolenaar7f009df2020-03-16 20:27:38 +0100504 s = vim_strsave(value == NULL ? (char_u *)"" : value);
Bram Moolenaardac13472019-09-16 21:06:21 +0200505 if (s != NULL)
506 {
507 varp = (char_u **)get_option_varp_scope(opt_idx,
508 (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
509 ? (is_global_local_option(opt_idx)
510 ? OPT_GLOBAL : OPT_LOCAL)
511 : opt_flags);
512 oldval = *varp;
513#if defined(FEAT_EVAL)
514 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
515 {
516 oldval_l = *(char_u **)get_option_varp_scope(opt_idx, OPT_LOCAL);
517 oldval_g = *(char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
518 }
519#endif
520 *varp = s;
521
522#if defined(FEAT_EVAL)
523 if (!starting
524# ifdef FEAT_CRYPT
525 && !is_crypt_key_option(opt_idx)
526# endif
527 )
528 {
529 if (oldval_l != NULL)
530 saved_oldval_l = vim_strsave(oldval_l);
531 if (oldval_g != NULL)
532 saved_oldval_g = vim_strsave(oldval_g);
533 saved_oldval = vim_strsave(oldval);
534 saved_newval = vim_strsave(s);
535 }
536#endif
zeertzjqf6782732022-07-27 18:26:03 +0100537 if ((errmsg = did_set_string_option(opt_idx, varp, oldval, NULL,
Bram Moolenaardac13472019-09-16 21:06:21 +0200538 opt_flags, &value_checked)) == NULL)
539 did_set_option(opt_idx, opt_flags, TRUE, value_checked);
540
541#if defined(FEAT_EVAL)
542 // call autocommand after handling side effects
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100543 if (errmsg == NULL)
Bram Moolenaardac13472019-09-16 21:06:21 +0200544 trigger_optionsset_string(opt_idx, opt_flags,
545 saved_oldval, saved_oldval_l,
546 saved_oldval_g, saved_newval);
547 vim_free(saved_oldval);
548 vim_free(saved_oldval_l);
549 vim_free(saved_oldval_g);
550 vim_free(saved_newval);
551#endif
552 }
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100553 return errmsg;
Bram Moolenaardac13472019-09-16 21:06:21 +0200554}
555
556/*
557 * Return TRUE if "val" is a valid 'filetype' name.
558 * Also used for 'syntax' and 'keymap'.
559 */
560 static int
561valid_filetype(char_u *val)
562{
563 return valid_name(val, ".-_");
564}
565
566#ifdef FEAT_STL_OPT
567/*
568 * Check validity of options with the 'statusline' format.
zeertzjq5dc294a2022-04-15 13:17:57 +0100569 * Return an untranslated error message or NULL.
Bram Moolenaardac13472019-09-16 21:06:21 +0200570 */
571 static char *
572check_stl_option(char_u *s)
573{
Bram Moolenaardac13472019-09-16 21:06:21 +0200574 int groupdepth = 0;
575 static char errbuf[80];
576
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100577 while (*s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200578 {
579 // Check for valid keys after % sequences
580 while (*s && *s != '%')
581 s++;
582 if (!*s)
583 break;
584 s++;
Bram Moolenaardac13472019-09-16 21:06:21 +0200585 if (*s == '%' || *s == STL_TRUNCMARK || *s == STL_MIDDLEMARK)
586 {
587 s++;
588 continue;
589 }
590 if (*s == ')')
591 {
592 s++;
593 if (--groupdepth < 0)
594 break;
595 continue;
596 }
597 if (*s == '-')
598 s++;
599 while (VIM_ISDIGIT(*s))
600 s++;
601 if (*s == STL_USER_HL)
602 continue;
603 if (*s == '.')
604 {
605 s++;
606 while (*s && VIM_ISDIGIT(*s))
607 s++;
608 }
609 if (*s == '(')
610 {
611 groupdepth++;
612 continue;
613 }
614 if (vim_strchr(STL_ALL, *s) == NULL)
615 {
616 return illegal_char(errbuf, *s);
617 }
618 if (*s == '{')
619 {
zeertzjq5dc294a2022-04-15 13:17:57 +0100620 int reevaluate = (*++s == '%');
shadmansaleh30e3de22021-05-15 17:23:28 +0200621
zeertzjq5dc294a2022-04-15 13:17:57 +0100622 if (reevaluate && *++s == '}')
623 // "}" is not allowed immediately after "%{%"
624 return illegal_char(errbuf, '}');
shadmansaleh30e3de22021-05-15 17:23:28 +0200625 while ((*s != '}' || (reevaluate && s[-1] != '%')) && *s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200626 s++;
627 if (*s != '}')
zeertzjq5dc294a2022-04-15 13:17:57 +0100628 return e_unclosed_expression_sequence;
Bram Moolenaardac13472019-09-16 21:06:21 +0200629 }
630 }
Bram Moolenaardac13472019-09-16 21:06:21 +0200631 if (groupdepth != 0)
zeertzjq5dc294a2022-04-15 13:17:57 +0100632 return e_unbalanced_groups;
Bram Moolenaardac13472019-09-16 21:06:21 +0200633 return NULL;
634}
635#endif
636
637/*
638 * Handle string options that need some action to perform when changed.
zeertzjqf6782732022-07-27 18:26:03 +0100639 * The new value must be allocated.
LemonBoy77142312022-04-15 20:50:46 +0100640 * Returns NULL for success, or an unstranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +0200641 */
642 char *
643did_set_string_option(
644 int opt_idx, // index in options[] table
645 char_u **varp, // pointer to the option variable
Bram Moolenaardac13472019-09-16 21:06:21 +0200646 char_u *oldval, // previous value of the option
647 char *errbuf, // buffer for errors, or NULL
648 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
649 int *value_checked) // value was checked to be save, no
650 // need to set P_INSECURE
651{
652 char *errmsg = NULL;
653 char_u *s, *p;
654 int did_chartab = FALSE;
655 char_u **gvarp;
656 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
657#ifdef FEAT_GUI
658 // set when changing an option that only requires a redraw in the GUI
659 int redraw_gui_only = FALSE;
660#endif
661 int value_changed = FALSE;
662#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
663 int did_swaptcap = FALSE;
664#endif
665
666 // Get the global option to compare with, otherwise we would have to check
667 // two values for all local options.
668 gvarp = (char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
669
670 // Disallow changing some options from secure mode
671 if ((secure
672#ifdef HAVE_SANDBOX
673 || sandbox != 0
674#endif
675 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +0000676 errmsg = e_not_allowed_here;
Bram Moolenaardac13472019-09-16 21:06:21 +0200677
678 // Check for a "normal" directory or file name in some options. Disallow a
679 // path separator (slash and/or backslash), wildcards and characters that
680 // are often illegal in a file name. Be more permissive if "secure" is off.
681 else if (((get_option_flags(opt_idx) & P_NFNAME)
682 && vim_strpbrk(*varp, (char_u *)(secure
683 ? "/\\*?[|;&<>\r\n" : "/\\*?[<>\r\n")) != NULL)
684 || ((get_option_flags(opt_idx) & P_NDNAME)
685 && vim_strpbrk(*varp, (char_u *)"*?[|;&<>\r\n") != NULL))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000686 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200687
688 // 'term'
689 else if (varp == &T_NAME)
690 {
691 if (T_NAME[0] == NUL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000692 errmsg = e_cannot_set_term_to_empty_string;
Bram Moolenaardac13472019-09-16 21:06:21 +0200693#ifdef FEAT_GUI
Bram Moolenaar5daa9112021-02-01 18:39:47 +0100694 else if (gui.in_use)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000695 errmsg = e_cannot_change_term_in_GUI;
Bram Moolenaardac13472019-09-16 21:06:21 +0200696 else if (term_is_gui(T_NAME))
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000697 errmsg = e_use_gui_to_start_GUI;
Bram Moolenaardac13472019-09-16 21:06:21 +0200698#endif
699 else if (set_termname(T_NAME) == FAIL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000700 errmsg = e_not_found_in_termcap;
Bram Moolenaardac13472019-09-16 21:06:21 +0200701 else
702 {
703 // Screen colors may have changed.
704 redraw_later_clear();
705
706 // Both 'term' and 'ttytype' point to T_NAME, only set the
707 // P_ALLOCED flag on 'term'.
708 opt_idx = findoption((char_u *)"term");
709 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
710 }
711 }
712
713 // 'backupcopy'
714 else if (gvarp == &p_bkc)
715 {
716 char_u *bkc = p_bkc;
717 unsigned int *flags = &bkc_flags;
718
719 if (opt_flags & OPT_LOCAL)
720 {
721 bkc = curbuf->b_p_bkc;
722 flags = &curbuf->b_bkc_flags;
723 }
724
725 if ((opt_flags & OPT_LOCAL) && *bkc == NUL)
726 // make the local value empty: use the global value
727 *flags = 0;
728 else
729 {
730 if (opt_strings_flags(bkc, p_bkc_values, flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000731 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200732 if ((((int)*flags & BKC_AUTO) != 0)
733 + (((int)*flags & BKC_YES) != 0)
734 + (((int)*flags & BKC_NO) != 0) != 1)
735 {
736 // Must have exactly one of "auto", "yes" and "no".
737 (void)opt_strings_flags(oldval, p_bkc_values, flags, TRUE);
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000738 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200739 }
740 }
741 }
742
743 // 'backupext' and 'patchmode'
744 else if (varp == &p_bex || varp == &p_pm)
745 {
746 if (STRCMP(*p_bex == '.' ? p_bex + 1 : p_bex,
747 *p_pm == '.' ? p_pm + 1 : p_pm) == 0)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100748 errmsg = e_backupext_and_patchmode_are_equal;
Bram Moolenaardac13472019-09-16 21:06:21 +0200749 }
750#ifdef FEAT_LINEBREAK
751 // 'breakindentopt'
752 else if (varp == &curwin->w_p_briopt)
753 {
754 if (briopt_check(curwin) == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000755 errmsg = e_invalid_argument;
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000756 // list setting requires a redraw
757 if (curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100758 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaardac13472019-09-16 21:06:21 +0200759 }
760#endif
761
762 // 'isident', 'iskeyword', 'isprint or 'isfname' option: refill g_chartab[]
763 // If the new option is invalid, use old value. 'lisp' option: refill
764 // g_chartab[] for '-' char
765 else if ( varp == &p_isi
766 || varp == &(curbuf->b_p_isk)
767 || varp == &p_isp
768 || varp == &p_isf)
769 {
770 if (init_chartab() == FAIL)
771 {
772 did_chartab = TRUE; // need to restore it below
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000773 errmsg = e_invalid_argument; // error in value
Bram Moolenaardac13472019-09-16 21:06:21 +0200774 }
775 }
776
777 // 'helpfile'
778 else if (varp == &p_hf)
779 {
780 // May compute new values for $VIM and $VIMRUNTIME
781 if (didset_vim)
LemonBoy77142312022-04-15 20:50:46 +0100782 vim_unsetenv_ext((char_u *)"VIM");
Bram Moolenaardac13472019-09-16 21:06:21 +0200783 if (didset_vimruntime)
LemonBoy77142312022-04-15 20:50:46 +0100784 vim_unsetenv_ext((char_u *)"VIMRUNTIME");
Bram Moolenaardac13472019-09-16 21:06:21 +0200785 }
786
787#ifdef FEAT_SYN_HL
788 // 'cursorlineopt'
789 else if (varp == &curwin->w_p_culopt
790 || gvarp == &curwin->w_allbuf_opt.wo_culopt)
791 {
792 if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000793 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200794 }
795
796 // 'colorcolumn'
797 else if (varp == &curwin->w_p_cc)
798 errmsg = check_colorcolumn(curwin);
799#endif
800
801#ifdef FEAT_MULTI_LANG
802 // 'helplang'
803 else if (varp == &p_hlg)
804 {
805 // Check for "", "ab", "ab,cd", etc.
806 for (s = p_hlg; *s != NUL; s += 3)
807 {
808 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
809 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000810 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200811 break;
812 }
813 if (s[2] == NUL)
814 break;
815 }
816 }
817#endif
818
819 // 'highlight'
820 else if (varp == &p_hl)
821 {
822 if (highlight_changed() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000823 errmsg = e_invalid_argument; // invalid flags
Bram Moolenaardac13472019-09-16 21:06:21 +0200824 }
825
826 // 'nrformats'
827 else if (gvarp == &p_nf)
828 {
829 if (check_opt_strings(*varp, p_nf_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000830 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200831 }
832
833#ifdef FEAT_SESSION
834 // 'sessionoptions'
835 else if (varp == &p_ssop)
836 {
837 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000838 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200839 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
840 {
841 // Don't allow both "sesdir" and "curdir".
842 (void)opt_strings_flags(oldval, p_ssop_values, &ssop_flags, TRUE);
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000843 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200844 }
845 }
846 // 'viewoptions'
847 else if (varp == &p_vop)
848 {
849 if (opt_strings_flags(p_vop, p_ssop_values, &vop_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000850 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200851 }
852#endif
853
854 // 'scrollopt'
855 else if (varp == &p_sbo)
856 {
857 if (check_opt_strings(p_sbo, p_scbopt_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000858 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200859 }
860
861 // 'ambiwidth'
862 else if (varp == &p_ambw || varp == &p_emoji)
863 {
864 if (check_opt_strings(p_ambw, p_ambw_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000865 errmsg = e_invalid_argument;
Bram Moolenaareed9d462021-02-15 20:38:25 +0100866 else
zeertzjq8ca29b62022-08-09 12:53:14 +0100867 errmsg = check_chars_options();
Bram Moolenaardac13472019-09-16 21:06:21 +0200868 }
869
870 // 'background'
871 else if (varp == &p_bg)
872 {
873 if (check_opt_strings(p_bg, p_bg_values, FALSE) == OK)
874 {
875#ifdef FEAT_EVAL
876 int dark = (*p_bg == 'd');
877#endif
878
879 init_highlight(FALSE, FALSE);
880
881#ifdef FEAT_EVAL
882 if (dark != (*p_bg == 'd')
883 && get_var_value((char_u *)"g:colors_name") != NULL)
884 {
885 // The color scheme must have set 'background' back to another
886 // value, that's not what we want here. Disable the color
887 // scheme and set the colors again.
888 do_unlet((char_u *)"g:colors_name", TRUE);
889 free_string_option(p_bg);
890 p_bg = vim_strsave((char_u *)(dark ? "dark" : "light"));
891 check_string_option(&p_bg);
892 init_highlight(FALSE, FALSE);
893 }
894#endif
Bram Moolenaarad431992021-05-03 20:40:38 +0200895#ifdef FEAT_TERMINAL
896 term_update_colors_all();
897#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200898 }
899 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000900 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200901 }
902
903 // 'wildmode'
904 else if (varp == &p_wim)
905 {
906 if (check_opt_wim() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000907 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200908 }
909
910 // 'wildoptions'
911 else if (varp == &p_wop)
912 {
913 if (check_opt_strings(p_wop, p_wop_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000914 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200915 }
916
917#ifdef FEAT_WAK
918 // 'winaltkeys'
919 else if (varp == &p_wak)
920 {
921 if (*p_wak == NUL
922 || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000923 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200924# ifdef FEAT_MENU
ichizok02560422022-04-05 14:18:44 +0100925# if defined(FEAT_GUI_MOTIF)
Bram Moolenaardac13472019-09-16 21:06:21 +0200926 else if (gui.in_use)
927 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
ichizok02560422022-04-05 14:18:44 +0100928# elif defined(FEAT_GUI_GTK)
Bram Moolenaardac13472019-09-16 21:06:21 +0200929 else if (gui.in_use)
930 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
Bram Moolenaardac13472019-09-16 21:06:21 +0200931# endif
932# endif
933 }
934#endif
935
936 // 'eventignore'
937 else if (varp == &p_ei)
938 {
939 if (check_ei() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000940 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200941 }
942
943 // 'encoding', 'fileencoding', 'termencoding' and 'makeencoding'
944 else if (varp == &p_enc || gvarp == &p_fenc || varp == &p_tenc
945 || gvarp == &p_menc)
946 {
947 if (gvarp == &p_fenc)
948 {
949 if (!curbuf->b_p_ma && opt_flags != OPT_GLOBAL)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200950 errmsg = e_cannot_make_changes_modifiable_is_off;
Bram Moolenaardac13472019-09-16 21:06:21 +0200951 else if (vim_strchr(*varp, ',') != NULL)
952 // No comma allowed in 'fileencoding'; catches confusing it
953 // with 'fileencodings'.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000954 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200955 else
956 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200957 // May show a "+" in the title now.
958 redraw_titles();
Bram Moolenaardac13472019-09-16 21:06:21 +0200959 // Add 'fileencoding' to the swap file.
960 ml_setflags(curbuf);
961 }
962 }
963 if (errmsg == NULL)
964 {
965 // canonize the value, so that STRCMP() can be used on it
966 p = enc_canonize(*varp);
967 if (p != NULL)
968 {
969 vim_free(*varp);
970 *varp = p;
971 }
972 if (varp == &p_enc)
973 {
974 errmsg = mb_init();
Bram Moolenaardac13472019-09-16 21:06:21 +0200975 redraw_titles();
Bram Moolenaardac13472019-09-16 21:06:21 +0200976 }
977 }
978
979#if defined(FEAT_GUI_GTK)
980 if (errmsg == NULL && varp == &p_tenc && gui.in_use)
981 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +0000982 // GTK uses only a single encoding, and that is UTF-8.
Bram Moolenaardac13472019-09-16 21:06:21 +0200983 if (STRCMP(p_tenc, "utf-8") != 0)
Bram Moolenaard88be5b2022-01-04 19:57:55 +0000984 errmsg = e_cannot_be_changed_in_gtk_GUI;
Bram Moolenaardac13472019-09-16 21:06:21 +0200985 }
986#endif
987
988 if (errmsg == NULL)
989 {
990#ifdef FEAT_KEYMAP
991 // When 'keymap' is used and 'encoding' changes, reload the keymap
992 // (with another encoding).
993 if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
994 (void)keymap_init();
995#endif
996
997 // When 'termencoding' is not empty and 'encoding' changes or when
998 // 'termencoding' changes, need to setup for keyboard input and
999 // display output conversion.
1000 if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
1001 {
1002 if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
1003 || convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
1004 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001005 semsg(_(e_cannot_convert_between_str_and_str),
1006 p_tenc, p_enc);
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001007 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001008 }
1009 }
1010
1011#if defined(MSWIN)
1012 // $HOME may have characters in active code page.
1013 if (varp == &p_enc)
1014 init_homedir();
1015#endif
1016 }
1017 }
1018
1019#if defined(FEAT_POSTSCRIPT)
1020 else if (varp == &p_penc)
1021 {
1022 // Canonize printencoding if VIM standard one
1023 p = enc_canonize(p_penc);
1024 if (p != NULL)
1025 {
1026 vim_free(p_penc);
1027 p_penc = p;
1028 }
1029 else
1030 {
1031 // Ensure lower case and '-' for '_'
1032 for (s = p_penc; *s != NUL; s++)
1033 {
1034 if (*s == '_')
1035 *s = '-';
1036 else
1037 *s = TOLOWER_ASC(*s);
1038 }
1039 }
1040 }
1041#endif
1042
1043#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
1044 else if (varp == &p_imak)
1045 {
1046 if (!im_xim_isvalid_imactivate())
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001047 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001048 }
1049#endif
1050
1051#ifdef FEAT_KEYMAP
1052 else if (varp == &curbuf->b_p_keymap)
1053 {
1054 if (!valid_filetype(*varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001055 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001056 else
1057 {
1058 int secure_save = secure;
1059
1060 // Reset the secure flag, since the value of 'keymap' has
1061 // been checked to be safe.
1062 secure = 0;
1063
1064 // load or unload key mapping tables
1065 errmsg = keymap_init();
1066
1067 secure = secure_save;
1068
1069 // Since we check the value, there is no need to set P_INSECURE,
1070 // even when the value comes from a modeline.
1071 *value_checked = TRUE;
1072 }
1073
1074 if (errmsg == NULL)
1075 {
1076 if (*curbuf->b_p_keymap != NUL)
1077 {
1078 // Installed a new keymap, switch on using it.
1079 curbuf->b_p_iminsert = B_IMODE_LMAP;
1080 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
1081 curbuf->b_p_imsearch = B_IMODE_LMAP;
1082 }
1083 else
1084 {
1085 // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
1086 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
1087 curbuf->b_p_iminsert = B_IMODE_NONE;
1088 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
1089 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
1090 }
1091 if ((opt_flags & OPT_LOCAL) == 0)
1092 {
1093 set_iminsert_global();
1094 set_imsearch_global();
1095 }
1096 status_redraw_curbuf();
1097 }
1098 }
1099#endif
1100
1101 // 'fileformat'
1102 else if (gvarp == &p_ff)
1103 {
1104 if (!curbuf->b_p_ma && !(opt_flags & OPT_GLOBAL))
Bram Moolenaar108010a2021-06-27 22:03:33 +02001105 errmsg = e_cannot_make_changes_modifiable_is_off;
Bram Moolenaardac13472019-09-16 21:06:21 +02001106 else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001107 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001108 else
1109 {
1110 // may also change 'textmode'
1111 if (get_fileformat(curbuf) == EOL_DOS)
1112 curbuf->b_p_tx = TRUE;
1113 else
1114 curbuf->b_p_tx = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02001115 redraw_titles();
Bram Moolenaardac13472019-09-16 21:06:21 +02001116 // update flag in swap file
1117 ml_setflags(curbuf);
1118 // Redraw needed when switching to/from "mac": a CR in the text
1119 // will be displayed differently.
1120 if (get_fileformat(curbuf) == EOL_MAC || *oldval == 'm')
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001121 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaardac13472019-09-16 21:06:21 +02001122 }
1123 }
1124
1125 // 'fileformats'
1126 else if (varp == &p_ffs)
1127 {
1128 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001129 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001130 else
1131 {
1132 // also change 'textauto'
1133 if (*p_ffs == NUL)
1134 p_ta = FALSE;
1135 else
1136 p_ta = TRUE;
1137 }
1138 }
1139
1140#if defined(FEAT_CRYPT)
1141 // 'cryptkey'
1142 else if (gvarp == &p_key)
1143 {
1144 // Make sure the ":set" command doesn't show the new value in the
1145 // history.
1146 remove_key_from_history();
1147
1148 if (STRCMP(curbuf->b_p_key, oldval) != 0)
1149 // Need to update the swapfile.
Bram Moolenaar76cb6832020-05-15 22:30:38 +02001150 {
Bram Moolenaardac13472019-09-16 21:06:21 +02001151 ml_set_crypt_key(curbuf, oldval,
1152 *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
Bram Moolenaar76cb6832020-05-15 22:30:38 +02001153 changed_internal();
1154 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001155 }
1156
1157 else if (gvarp == &p_cm)
1158 {
1159 if (opt_flags & OPT_LOCAL)
1160 p = curbuf->b_p_cm;
1161 else
1162 p = p_cm;
1163 if (check_opt_strings(p, p_cm_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001164 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001165 else if (crypt_self_test() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001166 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001167 else
1168 {
1169 // When setting the global value to empty, make it "zip".
1170 if (*p_cm == NUL)
1171 {
zeertzjqf6782732022-07-27 18:26:03 +01001172 free_string_option(p_cm);
Bram Moolenaardac13472019-09-16 21:06:21 +02001173 p_cm = vim_strsave((char_u *)"zip");
Bram Moolenaardac13472019-09-16 21:06:21 +02001174 }
1175 // When using ":set cm=name" the local value is going to be empty.
1176 // Do that here, otherwise the crypt functions will still use the
1177 // local value.
1178 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1179 {
1180 free_string_option(curbuf->b_p_cm);
1181 curbuf->b_p_cm = empty_option;
1182 }
1183
1184 // Need to update the swapfile when the effective method changed.
1185 // Set "s" to the effective old value, "p" to the effective new
1186 // method and compare.
1187 if ((opt_flags & OPT_LOCAL) && *oldval == NUL)
1188 s = p_cm; // was previously using the global value
1189 else
1190 s = oldval;
1191 if (*curbuf->b_p_cm == NUL)
1192 p = p_cm; // is now using the global value
1193 else
1194 p = curbuf->b_p_cm;
1195 if (STRCMP(s, p) != 0)
1196 ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1197
1198 // If the global value changes need to update the swapfile for all
1199 // buffers using that value.
1200 if ((opt_flags & OPT_GLOBAL) && STRCMP(p_cm, oldval) != 0)
1201 {
1202 buf_T *buf;
1203
1204 FOR_ALL_BUFFERS(buf)
1205 if (buf != curbuf && *buf->b_p_cm == NUL)
1206 ml_set_crypt_key(buf, buf->b_p_key, oldval);
1207 }
1208 }
1209 }
1210#endif
1211
1212 // 'matchpairs'
1213 else if (gvarp == &p_mps)
1214 {
1215 if (has_mbyte)
1216 {
1217 for (p = *varp; *p != NUL; ++p)
1218 {
1219 int x2 = -1;
1220 int x3 = -1;
1221
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01001222 p += mb_ptr2len(p);
Bram Moolenaardac13472019-09-16 21:06:21 +02001223 if (*p != NUL)
1224 x2 = *p++;
1225 if (*p != NUL)
1226 {
1227 x3 = mb_ptr2char(p);
1228 p += mb_ptr2len(p);
1229 }
1230 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
1231 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001232 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001233 break;
1234 }
1235 if (*p == NUL)
1236 break;
1237 }
1238 }
1239 else
1240 {
1241 // Check for "x:y,x:y"
1242 for (p = *varp; *p != NUL; p += 4)
1243 {
1244 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
1245 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001246 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001247 break;
1248 }
1249 if (p[3] == NUL)
1250 break;
1251 }
1252 }
1253 }
1254
Bram Moolenaardac13472019-09-16 21:06:21 +02001255 // 'comments'
1256 else if (gvarp == &p_com)
1257 {
1258 for (s = *varp; *s; )
1259 {
1260 while (*s && *s != ':')
1261 {
1262 if (vim_strchr((char_u *)COM_ALL, *s) == NULL
1263 && !VIM_ISDIGIT(*s) && *s != '-')
1264 {
1265 errmsg = illegal_char(errbuf, *s);
1266 break;
1267 }
1268 ++s;
1269 }
1270 if (*s++ == NUL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001271 errmsg = e_missing_colon;
Bram Moolenaardac13472019-09-16 21:06:21 +02001272 else if (*s == ',' || *s == NUL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001273 errmsg = e_zero_length_string;
Bram Moolenaardac13472019-09-16 21:06:21 +02001274 if (errmsg != NULL)
1275 break;
1276 while (*s && *s != ',')
1277 {
1278 if (*s == '\\' && s[1] != NUL)
1279 ++s;
1280 ++s;
1281 }
1282 s = skip_to_option_part(s);
1283 }
1284 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001285
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01001286 // global 'listchars' or 'fillchars'
1287 else if (varp == &p_lcs || varp == &p_fcs)
Bram Moolenaardac13472019-09-16 21:06:21 +02001288 {
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01001289 char_u **local_ptr = varp == &p_lcs
1290 ? &curwin->w_p_lcs : &curwin->w_p_fcs;
1291
1292 // only apply the global value to "curwin" when it does not have a
1293 // local value
1294 errmsg = set_chars_option(curwin, varp,
1295 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL));
Bram Moolenaareed9d462021-02-15 20:38:25 +01001296 if (errmsg == NULL)
1297 {
1298 tabpage_T *tp;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001299 win_T *wp;
Bram Moolenaareed9d462021-02-15 20:38:25 +01001300
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01001301 // If the current window is set to use the global
1302 // 'listchars'/'fillchars' value, clear the window-local value.
Bram Moolenaareed9d462021-02-15 20:38:25 +01001303 if (!(opt_flags & OPT_GLOBAL))
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01001304 clear_string_option(local_ptr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01001305 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01001306 {
Bram Moolenaar5ed26fa2022-07-04 18:05:51 +01001307 // If the current window has a local value need to apply it
1308 // again, it was changed when setting the global value.
Bram Moolenaar606efc72021-11-12 19:52:47 +00001309 // If no error was returned above, we don't expect an error
1310 // here, so ignore the return value.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01001311 local_ptr = varp == &p_lcs ? &wp->w_p_lcs : &wp->w_p_fcs;
1312 if (**local_ptr == NUL)
1313 (void)set_chars_option(wp, local_ptr, TRUE);
1314 }
Bram Moolenaar606efc72021-11-12 19:52:47 +00001315
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001316 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01001317 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001318 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001319 // local 'listchars'
1320 else if (varp == &curwin->w_p_lcs)
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01001321 errmsg = set_chars_option(curwin, varp, TRUE);
Bram Moolenaareed9d462021-02-15 20:38:25 +01001322
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001323 // local 'fillchars'
1324 else if (varp == &curwin->w_p_fcs)
1325 {
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01001326 errmsg = set_chars_option(curwin, varp, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +02001327 }
1328
1329#ifdef FEAT_CMDWIN
1330 // 'cedit'
1331 else if (varp == &p_cedit)
1332 {
1333 errmsg = check_cedit();
1334 }
1335#endif
1336
1337 // 'verbosefile'
1338 else if (varp == &p_vfile)
1339 {
1340 verbose_stop();
1341 if (*p_vfile != NUL && verbose_open() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001342 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001343 }
1344
1345#ifdef FEAT_VIMINFO
1346 // 'viminfo'
1347 else if (varp == &p_viminfo)
1348 {
1349 for (s = p_viminfo; *s;)
1350 {
1351 // Check it's a valid character
1352 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
1353 {
1354 errmsg = illegal_char(errbuf, *s);
1355 break;
1356 }
1357 if (*s == 'n') // name is always last one
1358 break;
1359 else if (*s == 'r') // skip until next ','
1360 {
1361 while (*++s && *s != ',')
1362 ;
1363 }
1364 else if (*s == '%')
1365 {
1366 // optional number
1367 while (vim_isdigit(*++s))
1368 ;
1369 }
1370 else if (*s == '!' || *s == 'h' || *s == 'c')
1371 ++s; // no extra chars
1372 else // must have a number
1373 {
1374 while (vim_isdigit(*++s))
1375 ;
1376
1377 if (!VIM_ISDIGIT(*(s - 1)))
1378 {
1379 if (errbuf != NULL)
1380 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001381 sprintf(errbuf,
1382 _(e_missing_number_after_angle_str_angle),
Bram Moolenaardac13472019-09-16 21:06:21 +02001383 transchar_byte(*(s - 1)));
1384 errmsg = errbuf;
1385 }
1386 else
1387 errmsg = "";
1388 break;
1389 }
1390 }
1391 if (*s == ',')
1392 ++s;
1393 else if (*s)
1394 {
1395 if (errbuf != NULL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001396 errmsg = e_missing_comma;
Bram Moolenaardac13472019-09-16 21:06:21 +02001397 else
1398 errmsg = "";
1399 break;
1400 }
1401 }
1402 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001403 errmsg = e_must_specify_a_value;
Bram Moolenaardac13472019-09-16 21:06:21 +02001404 }
1405#endif // FEAT_VIMINFO
1406
1407 // terminal options
1408 else if (istermoption_idx(opt_idx) && full_screen)
1409 {
1410 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
1411 if (varp == &T_CCO)
1412 {
1413 int colors = atoi((char *)T_CCO);
1414
1415 // Only reinitialize colors if t_Co value has really changed to
1416 // avoid expensive reload of colorscheme if t_Co is set to the
1417 // same value multiple times.
1418 if (colors != t_colors)
1419 {
1420 t_colors = colors;
1421 if (t_colors <= 1)
1422 {
zeertzjqf6782732022-07-27 18:26:03 +01001423 vim_free(T_CCO);
Bram Moolenaardac13472019-09-16 21:06:21 +02001424 T_CCO = empty_option;
1425 }
1426#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1427 if (is_term_win32())
1428 {
1429 swap_tcap();
1430 did_swaptcap = TRUE;
1431 }
1432#endif
1433 // We now have a different color setup, initialize it again.
1434 init_highlight(TRUE, FALSE);
1435 }
1436 }
1437 ttest(FALSE);
1438 if (varp == &T_ME)
1439 {
1440 out_str(T_ME);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001441 redraw_later(UPD_CLEAR);
Bram Moolenaardac13472019-09-16 21:06:21 +02001442#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
1443 // Since t_me has been set, this probably means that the user
1444 // wants to use this as default colors. Need to reset default
1445 // background/foreground colors.
1446# ifdef VIMDLL
1447 if (!gui.in_use && !gui.starting)
1448# endif
1449 mch_set_normal_colors();
1450#endif
1451 }
1452 if (varp == &T_BE && termcap_active)
1453 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01001454 MAY_WANT_TO_LOG_THIS;
1455
Bram Moolenaardac13472019-09-16 21:06:21 +02001456 if (*T_BE == NUL)
1457 // When clearing t_BE we assume the user no longer wants
1458 // bracketed paste, thus disable it by writing t_BD.
1459 out_str(T_BD);
1460 else
1461 out_str(T_BE);
1462 }
1463 }
1464
1465#ifdef FEAT_LINEBREAK
1466 // 'showbreak'
Bram Moolenaaree857022019-11-09 23:26:40 +01001467 else if (gvarp == &p_sbr)
Bram Moolenaardac13472019-09-16 21:06:21 +02001468 {
Bram Moolenaaree857022019-11-09 23:26:40 +01001469 for (s = *varp; *s; )
Bram Moolenaardac13472019-09-16 21:06:21 +02001470 {
1471 if (ptr2cells(s) != 1)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001472 errmsg = e_showbreak_contains_unprintable_or_wide_character;
Bram Moolenaardac13472019-09-16 21:06:21 +02001473 MB_PTR_ADV(s);
1474 }
1475 }
1476#endif
1477
1478#ifdef FEAT_GUI
1479 // 'guifont'
1480 else if (varp == &p_guifont)
1481 {
1482 if (gui.in_use)
1483 {
1484 p = p_guifont;
1485# if defined(FEAT_GUI_GTK)
1486 // Put up a font dialog and let the user select a new value.
1487 // If this is cancelled go back to the old value but don't
1488 // give an error message.
1489 if (STRCMP(p, "*") == 0)
1490 {
1491 p = gui_mch_font_dialog(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01001492 free_string_option(p_guifont);
Bram Moolenaardac13472019-09-16 21:06:21 +02001493 p_guifont = (p != NULL) ? p : vim_strsave(oldval);
Bram Moolenaardac13472019-09-16 21:06:21 +02001494 }
1495# endif
1496 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
1497 {
1498# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
1499 if (STRCMP(p_guifont, "*") == 0)
1500 {
1501 // Dialog was cancelled: Keep the old value without giving
1502 // an error message.
zeertzjqf6782732022-07-27 18:26:03 +01001503 free_string_option(p_guifont);
Bram Moolenaardac13472019-09-16 21:06:21 +02001504 p_guifont = vim_strsave(oldval);
Bram Moolenaardac13472019-09-16 21:06:21 +02001505 }
1506 else
1507# endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001508 errmsg = e_invalid_fonts;
Bram Moolenaardac13472019-09-16 21:06:21 +02001509 }
1510 }
1511 redraw_gui_only = TRUE;
1512 }
1513# ifdef FEAT_XFONTSET
1514 else if (varp == &p_guifontset)
1515 {
1516 if (STRCMP(p_guifontset, "*") == 0)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001517 errmsg = e_cant_select_fontset;
Bram Moolenaardac13472019-09-16 21:06:21 +02001518 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001519 errmsg = e_invalid_fontset;
Bram Moolenaardac13472019-09-16 21:06:21 +02001520 redraw_gui_only = TRUE;
1521 }
1522# endif
1523 else if (varp == &p_guifontwide)
1524 {
1525 if (STRCMP(p_guifontwide, "*") == 0)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001526 errmsg = e_cant_select_wide_font;
Bram Moolenaardac13472019-09-16 21:06:21 +02001527 else if (gui_get_wide_font() == FAIL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001528 errmsg = e_invalid_wide_font;
Bram Moolenaardac13472019-09-16 21:06:21 +02001529 redraw_gui_only = TRUE;
1530 }
1531#endif
Dusan Popovic4eeedc02021-10-16 20:52:05 +01001532# if defined(FEAT_GUI_GTK)
1533 else if (varp == &p_guiligatures)
1534 {
1535 gui_set_ligatures();
1536 redraw_gui_only = TRUE;
1537 }
1538# endif
Bram Moolenaardac13472019-09-16 21:06:21 +02001539
1540#ifdef CURSOR_SHAPE
1541 // 'guicursor'
1542 else if (varp == &p_guicursor)
1543 errmsg = parse_shape_opt(SHAPE_CURSOR);
1544#endif
1545
1546#ifdef FEAT_MOUSESHAPE
1547 // 'mouseshape'
1548 else if (varp == &p_mouseshape)
1549 {
1550 errmsg = parse_shape_opt(SHAPE_MOUSE);
1551 update_mouseshape(-1);
1552 }
1553#endif
1554
1555#ifdef FEAT_PRINTER
1556 else if (varp == &p_popt)
1557 errmsg = parse_printoptions();
1558# if defined(FEAT_POSTSCRIPT)
1559 else if (varp == &p_pmfn)
1560 errmsg = parse_printmbfont();
1561# endif
1562#endif
1563
1564#ifdef FEAT_LANGMAP
1565 // 'langmap'
1566 else if (varp == &p_langmap)
1567 langmap_set();
1568#endif
1569
1570#ifdef FEAT_LINEBREAK
1571 // 'breakat'
1572 else if (varp == &p_breakat)
1573 fill_breakat_flags();
1574#endif
1575
Bram Moolenaardac13472019-09-16 21:06:21 +02001576 // 'titlestring' and 'iconstring'
1577 else if (varp == &p_titlestring || varp == &p_iconstring)
1578 {
Bram Moolenaar651fca82021-11-29 20:39:38 +00001579#ifdef FEAT_STL_OPT
Bram Moolenaardac13472019-09-16 21:06:21 +02001580 int flagval = (varp == &p_titlestring) ? STL_IN_TITLE : STL_IN_ICON;
1581
1582 // NULL => statusline syntax
1583 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
1584 stl_syntax |= flagval;
1585 else
1586 stl_syntax &= ~flagval;
Bram Moolenaar651fca82021-11-29 20:39:38 +00001587#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02001588 did_set_title();
1589 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001590
1591#ifdef FEAT_GUI
1592 // 'guioptions'
1593 else if (varp == &p_go)
1594 {
1595 gui_init_which_components(oldval);
1596 redraw_gui_only = TRUE;
1597 }
1598#endif
1599
1600#if defined(FEAT_GUI_TABLINE)
1601 // 'guitablabel'
1602 else if (varp == &p_gtl)
1603 {
1604 redraw_tabline = TRUE;
1605 redraw_gui_only = TRUE;
1606 }
1607 // 'guitabtooltip'
1608 else if (varp == &p_gtt)
1609 {
1610 redraw_gui_only = TRUE;
1611 }
1612#endif
1613
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001614#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +02001615 // 'ttymouse'
1616 else if (varp == &p_ttym)
1617 {
1618 // Switch the mouse off before changing the escape sequences used for
1619 // that.
1620 mch_setmouse(FALSE);
1621 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001622 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001623 else
1624 check_mouse_termcode();
1625 if (termcap_active)
1626 setmouse(); // may switch it on again
1627 }
1628#endif
1629
1630 // 'selection'
1631 else if (varp == &p_sel)
1632 {
1633 if (*p_sel == NUL
1634 || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001635 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001636 }
1637
1638 // 'selectmode'
1639 else if (varp == &p_slm)
1640 {
1641 if (check_opt_strings(p_slm, p_slm_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001642 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001643 }
1644
1645#ifdef FEAT_BROWSE
1646 // 'browsedir'
1647 else if (varp == &p_bsdir)
1648 {
1649 if (check_opt_strings(p_bsdir, p_bsdir_values, FALSE) != OK
1650 && !mch_isdir(p_bsdir))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001651 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001652 }
1653#endif
1654
1655 // 'keymodel'
1656 else if (varp == &p_km)
1657 {
1658 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001659 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001660 else
1661 {
1662 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
1663 km_startsel = (vim_strchr(p_km, 'a') != NULL);
1664 }
1665 }
1666
1667 // 'mousemodel'
1668 else if (varp == &p_mousem)
1669 {
1670 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001671 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001672#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
1673 else if (*p_mousem != *oldval)
1674 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
1675 // to create or delete the popup menus.
1676 gui_motif_update_mousemodel(root_menu);
1677#endif
1678 }
1679
1680 // 'switchbuf'
1681 else if (varp == &p_swb)
1682 {
1683 if (opt_strings_flags(p_swb, p_swb_values, &swb_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001684 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001685 }
1686
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001687 // 'splitkeep'
1688 else if (varp == &p_spk)
1689 {
1690 if (check_opt_strings(p_spk, p_spk_values, FALSE) != OK)
1691 errmsg = e_invalid_argument;
1692 }
1693
Bram Moolenaardac13472019-09-16 21:06:21 +02001694 // 'debug'
1695 else if (varp == &p_debug)
1696 {
1697 if (check_opt_strings(p_debug, p_debug_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001698 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001699 }
1700
1701 // 'display'
1702 else if (varp == &p_dy)
1703 {
1704 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001705 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001706 else
1707 (void)init_chartab();
1708
1709 }
1710
1711 // 'eadirection'
1712 else if (varp == &p_ead)
1713 {
1714 if (check_opt_strings(p_ead, p_ead_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001715 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001716 }
1717
1718#ifdef FEAT_CLIPBOARD
1719 // 'clipboard'
1720 else if (varp == &p_cb)
1721 errmsg = check_clipboard_option();
1722#endif
1723
1724#ifdef FEAT_SPELL
1725 // When 'spelllang' or 'spellfile' is set and there is a window for this
1726 // buffer in which 'spell' is set load the wordlists.
1727 else if (varp == &(curwin->w_s->b_p_spl)
1728 || varp == &(curwin->w_s->b_p_spf))
1729 {
1730 int is_spellfile = varp == &(curwin->w_s->b_p_spf);
1731
1732 if ((is_spellfile && !valid_spellfile(*varp))
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001733 || (!is_spellfile && !valid_spelllang(*varp)))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001734 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001735 else
1736 errmsg = did_set_spell_option(is_spellfile);
1737 }
1738 // When 'spellcapcheck' is set compile the regexp program.
1739 else if (varp == &(curwin->w_s->b_p_spc))
1740 {
1741 errmsg = compile_cap_prog(curwin->w_s);
1742 }
Bram Moolenaar362b44b2020-06-10 21:47:00 +02001743 // 'spelloptions'
1744 else if (varp == &(curwin->w_s->b_p_spo))
1745 {
1746 if (**varp != NUL && STRCMP("camel", *varp) != 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001747 errmsg = e_invalid_argument;
Bram Moolenaar362b44b2020-06-10 21:47:00 +02001748 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001749 // 'spellsuggest'
1750 else if (varp == &p_sps)
1751 {
1752 if (spell_check_sps() != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001753 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001754 }
1755 // 'mkspellmem'
1756 else if (varp == &p_msm)
1757 {
1758 if (spell_check_msm() != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001759 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001760 }
1761#endif
1762
1763 // When 'bufhidden' is set, check for valid value.
1764 else if (gvarp == &p_bh)
1765 {
1766 if (check_opt_strings(curbuf->b_p_bh, p_bufhidden_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001767 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001768 }
1769
1770 // When 'buftype' is set, check for valid value.
1771 else if (gvarp == &p_bt)
1772 {
1773 if (check_opt_strings(curbuf->b_p_bt, p_buftype_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001774 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001775 else
1776 {
1777 if (curwin->w_status_height)
1778 {
1779 curwin->w_redr_status = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001780 redraw_later(UPD_VALID);
Bram Moolenaardac13472019-09-16 21:06:21 +02001781 }
1782 curbuf->b_help = (curbuf->b_p_bt[0] == 'h');
Bram Moolenaardac13472019-09-16 21:06:21 +02001783 redraw_titles();
Bram Moolenaardac13472019-09-16 21:06:21 +02001784 }
1785 }
1786
1787#ifdef FEAT_STL_OPT
zeertzjq5dc294a2022-04-15 13:17:57 +01001788 // 'statusline', 'tabline' or 'rulerformat'
1789 else if (gvarp == &p_stl || varp == &p_tal || varp == &p_ruf)
Bram Moolenaardac13472019-09-16 21:06:21 +02001790 {
1791 int wid;
1792
1793 if (varp == &p_ruf) // reset ru_wid first
1794 ru_wid = 0;
1795 s = *varp;
1796 if (varp == &p_ruf && *s == '%')
1797 {
1798 // set ru_wid if 'ruf' starts with "%99("
1799 if (*++s == '-') // ignore a '-'
1800 s++;
1801 wid = getdigits(&s);
1802 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
1803 ru_wid = wid;
1804 else
1805 errmsg = check_stl_option(p_ruf);
1806 }
zeertzjq5dc294a2022-04-15 13:17:57 +01001807 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
Bram Moolenaardac13472019-09-16 21:06:21 +02001808 else if (varp == &p_ruf || s[0] != '%' || s[1] != '!')
1809 errmsg = check_stl_option(s);
1810 if (varp == &p_ruf && errmsg == NULL)
1811 comp_col();
1812 }
1813#endif
1814
1815 // check if it is a valid value for 'complete' -- Acevedo
1816 else if (gvarp == &p_cpt)
1817 {
1818 for (s = *varp; *s;)
1819 {
1820 while (*s == ',' || *s == ' ')
1821 s++;
1822 if (!*s)
1823 break;
1824 if (vim_strchr((char_u *)".wbuksid]tU", *s) == NULL)
1825 {
1826 errmsg = illegal_char(errbuf, *s);
1827 break;
1828 }
1829 if (*++s != NUL && *s != ',' && *s != ' ')
1830 {
1831 if (s[-1] == 'k' || s[-1] == 's')
1832 {
1833 // skip optional filename after 'k' and 's'
1834 while (*s && *s != ',' && *s != ' ')
1835 {
1836 if (*s == '\\' && s[1] != NUL)
1837 ++s;
1838 ++s;
1839 }
1840 }
1841 else
1842 {
1843 if (errbuf != NULL)
1844 {
1845 sprintf((char *)errbuf,
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001846 _(e_illegal_character_after_chr), *--s);
Bram Moolenaardac13472019-09-16 21:06:21 +02001847 errmsg = errbuf;
1848 }
1849 else
1850 errmsg = "";
1851 break;
1852 }
1853 }
1854 }
1855 }
1856
1857 // 'completeopt'
1858 else if (varp == &p_cot)
1859 {
1860 if (check_opt_strings(p_cot, p_cot_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001861 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001862 else
1863 completeopt_was_set();
1864 }
1865
1866#ifdef BACKSLASH_IN_FILENAME
1867 // 'completeslash'
1868 else if (gvarp == &p_csl)
1869 {
1870 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1871 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001872 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001873 }
1874#endif
1875
1876#ifdef FEAT_SIGNS
1877 // 'signcolumn'
1878 else if (varp == &curwin->w_p_scl)
1879 {
1880 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001881 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001882 // When changing the 'signcolumn' to or from 'number', recompute the
1883 // width of the number column if 'number' or 'relativenumber' is set.
1884 if (((*oldval == 'n' && *(oldval + 1) == 'u')
1885 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
1886 && (curwin->w_p_nu || curwin->w_p_rnu))
1887 curwin->w_nrwidth_line_count = 0;
1888 }
1889#endif
1890
1891
1892#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
1893 // 'toolbar'
1894 else if (varp == &p_toolbar)
1895 {
1896 if (opt_strings_flags(p_toolbar, p_toolbar_values,
1897 &toolbar_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001898 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001899 else
1900 {
1901 out_flush();
1902 gui_mch_show_toolbar((toolbar_flags &
1903 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
1904 }
1905 }
1906#endif
1907
1908#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
1909 // 'toolbariconsize': GTK+ 2 only
1910 else if (varp == &p_tbis)
1911 {
1912 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001913 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001914 else
1915 {
1916 out_flush();
1917 gui_mch_show_toolbar((toolbar_flags &
1918 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
1919 }
1920 }
1921#endif
1922
1923 // 'pastetoggle': translate key codes like in a mapping
1924 else if (varp == &p_pt)
1925 {
1926 if (*p_pt)
1927 {
Bram Moolenaar1e7b52a2019-10-13 16:59:08 +02001928 (void)replace_termcodes(p_pt, &p,
1929 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
Bram Moolenaardac13472019-09-16 21:06:21 +02001930 if (p != NULL)
1931 {
zeertzjqf6782732022-07-27 18:26:03 +01001932 free_string_option(p_pt);
Bram Moolenaardac13472019-09-16 21:06:21 +02001933 p_pt = p;
Bram Moolenaardac13472019-09-16 21:06:21 +02001934 }
1935 }
1936 }
1937
1938 // 'backspace'
1939 else if (varp == &p_bs)
1940 {
1941 if (VIM_ISDIGIT(*p_bs))
1942 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02001943 if (*p_bs > '3' || p_bs[1] != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001944 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001945 }
1946 else if (check_opt_strings(p_bs, p_bs_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001947 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001948 }
1949 else if (varp == &p_bo)
1950 {
1951 if (opt_strings_flags(p_bo, p_bo_values, &bo_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001952 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001953 }
1954
1955 // 'tagcase'
1956 else if (gvarp == &p_tc)
1957 {
1958 unsigned int *flags;
1959
1960 if (opt_flags & OPT_LOCAL)
1961 {
1962 p = curbuf->b_p_tc;
1963 flags = &curbuf->b_tc_flags;
1964 }
1965 else
1966 {
1967 p = p_tc;
1968 flags = &tc_flags;
1969 }
1970
1971 if ((opt_flags & OPT_LOCAL) && *p == NUL)
1972 // make the local value empty: use the global value
1973 *flags = 0;
1974 else if (*p == NUL
1975 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001976 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001977 }
1978
1979 // 'casemap'
1980 else if (varp == &p_cmp)
1981 {
1982 if (opt_strings_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001983 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001984 }
1985
1986#ifdef FEAT_DIFF
1987 // 'diffopt'
1988 else if (varp == &p_dip)
1989 {
1990 if (diffopt_changed() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001991 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001992 }
1993#endif
1994
1995#ifdef FEAT_FOLDING
1996 // 'foldmethod'
1997 else if (gvarp == &curwin->w_allbuf_opt.wo_fdm)
1998 {
1999 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK
2000 || *curwin->w_p_fdm == NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002001 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002002 else
2003 {
2004 foldUpdateAll(curwin);
2005 if (foldmethodIsDiff(curwin))
2006 newFoldLevel();
2007 }
2008 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002009 // 'foldmarker'
2010 else if (gvarp == &curwin->w_allbuf_opt.wo_fmr)
2011 {
2012 p = vim_strchr(*varp, ',');
2013 if (p == NULL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002014 errmsg = e_comma_required;
Bram Moolenaardac13472019-09-16 21:06:21 +02002015 else if (p == *varp || p[1] == NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002016 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002017 else if (foldmethodIsMarker(curwin))
2018 foldUpdateAll(curwin);
2019 }
2020 // 'commentstring'
2021 else if (gvarp == &p_cms)
2022 {
2023 if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002024 errmsg = e_commentstring_must_be_empty_or_contain_str;
Bram Moolenaardac13472019-09-16 21:06:21 +02002025 }
2026 // 'foldopen'
2027 else if (varp == &p_fdo)
2028 {
2029 if (opt_strings_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002030 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002031 }
2032 // 'foldclose'
2033 else if (varp == &p_fcl)
2034 {
2035 if (check_opt_strings(p_fcl, p_fcl_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002036 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002037 }
2038 // 'foldignore'
2039 else if (gvarp == &curwin->w_allbuf_opt.wo_fdi)
2040 {
2041 if (foldmethodIsIndent(curwin))
2042 foldUpdateAll(curwin);
2043 }
2044#endif
2045
2046 // 'virtualedit'
Gary Johnson53ba05b2021-07-26 22:19:10 +02002047 else if (gvarp == &p_ve)
Bram Moolenaardac13472019-09-16 21:06:21 +02002048 {
Gary Johnson53ba05b2021-07-26 22:19:10 +02002049 char_u *ve = p_ve;
2050 unsigned int *flags = &ve_flags;
2051
2052 if (opt_flags & OPT_LOCAL)
Bram Moolenaardac13472019-09-16 21:06:21 +02002053 {
Gary Johnson51ad8502021-08-03 18:33:08 +02002054 ve = curwin->w_p_ve;
2055 flags = &curwin->w_ve_flags;
Gary Johnson53ba05b2021-07-26 22:19:10 +02002056 }
2057
2058 if ((opt_flags & OPT_LOCAL) && *ve == NUL)
2059 // make the local value empty: use the global value
2060 *flags = 0;
2061 else
2062 {
2063 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002064 errmsg = e_invalid_argument;
Gary Johnson53ba05b2021-07-26 22:19:10 +02002065 else if (STRCMP(p_ve, oldval) != 0)
2066 {
2067 // Recompute cursor position in case the new 've' setting
2068 // changes something.
2069 validate_virtcol();
2070 coladvance(curwin->w_virtcol);
2071 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002072 }
2073 }
2074
2075#if defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)
2076 else if (varp == &p_csqf)
2077 {
2078 if (p_csqf != NULL)
2079 {
2080 p = p_csqf;
2081 while (*p != NUL)
2082 {
2083 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
2084 || p[1] == NUL
2085 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
2086 || (p[2] != NUL && p[2] != ','))
2087 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002088 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002089 break;
2090 }
2091 else if (p[2] == NUL)
2092 break;
2093 else
2094 p += 3;
2095 }
2096 }
2097 }
2098#endif
2099
Bram Moolenaardac13472019-09-16 21:06:21 +02002100 // 'cinoptions'
2101 else if (gvarp == &p_cino)
2102 {
2103 // TODO: recognize errors
2104 parse_cino(curbuf);
2105 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002106
2107#if defined(FEAT_RENDER_OPTIONS)
2108 // 'renderoptions'
2109 else if (varp == &p_rop)
2110 {
2111 if (!gui_mch_set_rendering_options(p_rop))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002112 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002113 }
2114#endif
2115
2116 else if (gvarp == &p_ft)
2117 {
2118 if (!valid_filetype(*varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002119 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002120 else
2121 {
2122 value_changed = STRCMP(oldval, *varp) != 0;
2123
2124 // Since we check the value, there is no need to set P_INSECURE,
2125 // even when the value comes from a modeline.
2126 *value_checked = TRUE;
2127 }
2128 }
2129
2130#ifdef FEAT_SYN_HL
2131 else if (gvarp == &p_syn)
2132 {
2133 if (!valid_filetype(*varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002134 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002135 else
2136 {
2137 value_changed = STRCMP(oldval, *varp) != 0;
2138
2139 // Since we check the value, there is no need to set P_INSECURE,
2140 // even when the value comes from a modeline.
2141 *value_checked = TRUE;
2142 }
2143 }
2144#endif
2145
2146#ifdef FEAT_TERMINAL
2147 // 'termwinkey'
2148 else if (varp == &curwin->w_p_twk)
2149 {
2150 if (*curwin->w_p_twk != NUL
2151 && string_to_key(curwin->w_p_twk, TRUE) == 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002152 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002153 }
2154 // 'termwinsize'
2155 else if (varp == &curwin->w_p_tws)
2156 {
2157 if (*curwin->w_p_tws != NUL)
2158 {
2159 p = skipdigits(curwin->w_p_tws);
2160 if (p == curwin->w_p_tws
2161 || (*p != 'x' && *p != '*')
2162 || *skipdigits(p + 1) != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002163 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002164 }
2165 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002166 // 'wincolor'
2167 else if (varp == &curwin->w_p_wcr)
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002168 term_update_wincolor(curwin);
Bram Moolenaardac13472019-09-16 21:06:21 +02002169# if defined(MSWIN)
2170 // 'termwintype'
2171 else if (varp == &p_twt)
2172 {
2173 if (check_opt_strings(*varp, p_twt_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002174 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002175 }
2176# endif
2177#endif
2178
2179#ifdef FEAT_VARTABS
2180 // 'varsofttabstop'
2181 else if (varp == &(curbuf->b_p_vsts))
2182 {
2183 char_u *cp;
2184
2185 if (!(*varp)[0] || ((*varp)[0] == '0' && !(*varp)[1]))
2186 {
2187 if (curbuf->b_p_vsts_array)
2188 {
2189 vim_free(curbuf->b_p_vsts_array);
2190 curbuf->b_p_vsts_array = 0;
2191 }
2192 }
2193 else
2194 {
2195 for (cp = *varp; *cp; ++cp)
2196 {
2197 if (vim_isdigit(*cp))
2198 continue;
2199 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
2200 continue;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002201 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002202 break;
2203 }
2204 if (errmsg == NULL)
2205 {
2206 int *oldarray = curbuf->b_p_vsts_array;
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002207 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Bram Moolenaardac13472019-09-16 21:06:21 +02002208 {
2209 if (oldarray)
2210 vim_free(oldarray);
2211 }
2212 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002213 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002214 }
2215 }
2216 }
2217
2218 // 'vartabstop'
2219 else if (varp == &(curbuf->b_p_vts))
2220 {
2221 char_u *cp;
2222
2223 if (!(*varp)[0] || ((*varp)[0] == '0' && !(*varp)[1]))
2224 {
2225 if (curbuf->b_p_vts_array)
2226 {
2227 vim_free(curbuf->b_p_vts_array);
2228 curbuf->b_p_vts_array = NULL;
2229 }
2230 }
2231 else
2232 {
2233 for (cp = *varp; *cp; ++cp)
2234 {
2235 if (vim_isdigit(*cp))
2236 continue;
2237 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
2238 continue;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002239 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002240 break;
2241 }
2242 if (errmsg == NULL)
2243 {
2244 int *oldarray = curbuf->b_p_vts_array;
2245
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002246 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Bram Moolenaardac13472019-09-16 21:06:21 +02002247 {
2248 vim_free(oldarray);
2249#ifdef FEAT_FOLDING
2250 if (foldmethodIsIndent(curwin))
2251 foldUpdateAll(curwin);
2252#endif
2253 }
2254 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002255 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002256 }
2257 }
2258 }
2259#endif
2260
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002261#ifdef FEAT_PROP_POPUP
Bram Moolenaardac13472019-09-16 21:06:21 +02002262 // 'previewpopup'
2263 else if (varp == &p_pvp)
2264 {
2265 if (parse_previewpopup(NULL) == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002266 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002267 }
2268# ifdef FEAT_QUICKFIX
2269 // 'completepopup'
2270 else if (varp == &p_cpp)
2271 {
2272 if (parse_completepopup(NULL) == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002273 errmsg = e_invalid_argument;
Bram Moolenaar447bfba2020-07-18 16:07:16 +02002274 else
2275 popup_close_info();
Bram Moolenaardac13472019-09-16 21:06:21 +02002276 }
2277# endif
2278#endif
2279
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002280#ifdef FEAT_EVAL
2281 // '*expr' options
2282 else if (
2283# ifdef FEAT_BEVAL
2284 varp == &p_bexpr ||
2285# endif
2286# ifdef FEAT_DIFF
2287 varp == &p_dex ||
2288# endif
2289# ifdef FEAT_FOLDING
2290 varp == &curwin->w_p_fde ||
Yegappan Lakshmanan27708e62021-12-26 21:54:43 +00002291 varp == &curwin->w_p_fdt ||
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002292# endif
2293 gvarp == &p_fex ||
2294# ifdef FEAT_FIND_ID
2295 gvarp == &p_inex ||
2296# endif
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002297 gvarp == &p_inde ||
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002298# ifdef FEAT_DIFF
2299 varp == &p_pex ||
2300# endif
2301# ifdef FEAT_POSTSCRIPT
2302 varp == &p_pexpr ||
2303# endif
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00002304 varp == &p_ccv)
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002305 {
2306 char_u **p_opt = NULL;
2307 char_u *name;
2308
2309 // If the option value starts with <SID> or s:, then replace that with
2310 // the script identifier.
2311# ifdef FEAT_BEVAL
2312 if (varp == &p_bexpr) // 'balloonexpr'
2313 p_opt = (opt_flags & OPT_LOCAL) ? &curbuf->b_p_bexpr : &p_bexpr;
2314# endif
2315# ifdef FEAT_DIFF
2316 if (varp == &p_dex) // 'diffexpr'
2317 p_opt = &p_dex;
2318# endif
2319# ifdef FEAT_FOLDING
Yegappan Lakshmanan27708e62021-12-26 21:54:43 +00002320 if (varp == &curwin->w_p_fde) // 'foldexpr'
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002321 p_opt = &curwin->w_p_fde;
Yegappan Lakshmanan27708e62021-12-26 21:54:43 +00002322 if (varp == &curwin->w_p_fdt) // 'foldtext'
2323 p_opt = &curwin->w_p_fdt;
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002324# endif
2325 if (gvarp == &p_fex) // 'formatexpr'
2326 p_opt = &curbuf->b_p_fex;
2327# ifdef FEAT_FIND_ID
2328 if (gvarp == &p_inex) // 'includeexpr'
2329 p_opt = &curbuf->b_p_inex;
2330# endif
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002331 if (gvarp == &p_inde) // 'indentexpr'
2332 p_opt = &curbuf->b_p_inde;
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002333# ifdef FEAT_DIFF
2334 if (varp == &p_pex) // 'patchexpr'
2335 p_opt = &p_pex;
2336# endif
2337# ifdef FEAT_POSTSCRIPT
2338 if (varp == &p_pexpr) // 'printexpr'
2339 p_opt = &p_pexpr;
2340# endif
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00002341 if (varp == &p_ccv) // 'charconvert'
2342 p_opt = &p_ccv;
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002343
2344 if (p_opt != NULL)
2345 {
2346 name = get_scriptlocal_funcname(*p_opt);
2347 if (name != NULL)
2348 {
zeertzjqf6782732022-07-27 18:26:03 +01002349 free_string_option(*p_opt);
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002350 *p_opt = name;
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002351 }
2352 }
2353
2354# ifdef FEAT_FOLDING
2355 if (varp == &curwin->w_p_fde && foldmethodIsExpr(curwin))
2356 foldUpdateAll(curwin);
2357# endif
2358 }
2359#endif
2360
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002361#ifdef FEAT_COMPL_FUNC
2362 // 'completefunc'
2363 else if (gvarp == &p_cfu)
2364 {
2365 if (set_completefunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002366 errmsg = e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002367 }
2368
2369 // 'omnifunc'
2370 else if (gvarp == &p_ofu)
2371 {
2372 if (set_omnifunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002373 errmsg = e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002374 }
2375
2376 // 'thesaurusfunc'
2377 else if (gvarp == &p_tsrfu)
2378 {
2379 if (set_thesaurusfunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002380 errmsg = e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002381 }
2382#endif
2383
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00002384#if defined(FEAT_EVAL) && \
2385 (defined(FEAT_XIM) || defined(IME_WITHOUT_XIM) || defined(VIMDLL))
2386 // 'imactivatefunc'
2387 else if (gvarp == &p_imaf)
2388 {
2389 if (set_imactivatefunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002390 errmsg = e_invalid_argument;
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00002391 }
2392
2393 // 'imstatusfunc'
2394 else if (gvarp == &p_imsf)
2395 {
2396 if (set_imstatusfunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002397 errmsg = e_invalid_argument;
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00002398 }
2399#endif
2400
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00002401 // 'operatorfunc'
2402 else if (varp == &p_opfunc)
2403 {
2404 if (set_operatorfunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002405 errmsg = e_invalid_argument;
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00002406 }
2407
Bram Moolenaard43906d2020-07-20 21:31:32 +02002408#ifdef FEAT_QUICKFIX
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00002409 // 'quickfixtextfunc'
Bram Moolenaard43906d2020-07-20 21:31:32 +02002410 else if (varp == &p_qftf)
2411 {
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00002412 if (qf_process_qftf_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002413 errmsg = e_invalid_argument;
Bram Moolenaard43906d2020-07-20 21:31:32 +02002414 }
2415#endif
2416
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00002417#ifdef FEAT_EVAL
2418 // 'tagfunc'
2419 else if (gvarp == &p_tfu)
2420 {
2421 if (set_tagfunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002422 errmsg = e_invalid_argument;
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00002423 }
2424#endif
2425
Bram Moolenaardac13472019-09-16 21:06:21 +02002426 // Options that are a list of flags.
2427 else
2428 {
2429 p = NULL;
2430 if (varp == &p_ww) // 'whichwrap'
2431 p = (char_u *)WW_ALL;
2432 if (varp == &p_shm) // 'shortmess'
2433 p = (char_u *)SHM_ALL;
2434 else if (varp == &(p_cpo)) // 'cpoptions'
2435 p = (char_u *)CPO_ALL;
2436 else if (varp == &(curbuf->b_p_fo)) // 'formatoptions'
2437 p = (char_u *)FO_ALL;
2438#ifdef FEAT_CONCEAL
2439 else if (varp == &curwin->w_p_cocu) // 'concealcursor'
2440 p = (char_u *)COCU_ALL;
2441#endif
2442 else if (varp == &p_mouse) // 'mouse'
2443 {
Bram Moolenaardac13472019-09-16 21:06:21 +02002444 p = (char_u *)MOUSE_ALL;
Bram Moolenaardac13472019-09-16 21:06:21 +02002445 }
2446#if defined(FEAT_GUI)
2447 else if (varp == &p_go) // 'guioptions'
2448 p = (char_u *)GO_ALL;
2449#endif
2450 if (p != NULL)
2451 {
2452 for (s = *varp; *s; ++s)
2453 if (vim_strchr(p, *s) == NULL)
2454 {
2455 errmsg = illegal_char(errbuf, *s);
2456 break;
2457 }
2458 }
2459 }
2460
2461 // If error detected, restore the previous value.
2462 if (errmsg != NULL)
2463 {
zeertzjqf6782732022-07-27 18:26:03 +01002464 free_string_option(*varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02002465 *varp = oldval;
2466 // When resetting some values, need to act on it.
2467 if (did_chartab)
2468 (void)init_chartab();
2469 if (varp == &p_hl)
2470 (void)highlight_changed();
2471 }
2472 else
2473 {
2474#ifdef FEAT_EVAL
2475 // Remember where the option was set.
2476 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
2477#endif
2478 // Free string options that are in allocated memory.
2479 // Use "free_oldval", because recursiveness may change the flags under
2480 // our fingers (esp. init_highlight()).
2481 if (free_oldval)
2482 free_string_option(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01002483 set_option_flag(opt_idx, P_ALLOCED);
Bram Moolenaardac13472019-09-16 21:06:21 +02002484
2485 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
2486 && is_global_local_option(opt_idx))
2487 {
2488 // global option with local value set to use global value; free
2489 // the local value and make it empty
2490 p = get_option_varp_scope(opt_idx, OPT_LOCAL);
2491 free_string_option(*(char_u **)p);
2492 *(char_u **)p = empty_option;
2493 }
2494
2495 // May set global value for local option.
2496 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
2497 set_string_option_global(opt_idx, varp);
2498
2499 // Trigger the autocommand only after setting the flags.
2500#ifdef FEAT_SYN_HL
2501 // When 'syntax' is set, load the syntax of that name
2502 if (varp == &(curbuf->b_p_syn))
2503 {
2504 static int syn_recursive = 0;
2505
2506 ++syn_recursive;
2507 // Only pass TRUE for "force" when the value changed or not used
2508 // recursively, to avoid endless recurrence.
2509 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
2510 value_changed || syn_recursive == 1, curbuf);
2511 curbuf->b_flags |= BF_SYN_SET;
2512 --syn_recursive;
2513 }
2514#endif
2515 else if (varp == &(curbuf->b_p_ft))
2516 {
2517 // 'filetype' is set, trigger the FileType autocommand.
2518 // Skip this when called from a modeline and the filetype was
2519 // already set to this value.
2520 if (!(opt_flags & OPT_MODELINE) || value_changed)
2521 {
2522 static int ft_recursive = 0;
2523 int secure_save = secure;
2524
2525 // Reset the secure flag, since the value of 'filetype' has
2526 // been checked to be safe.
2527 secure = 0;
2528
2529 ++ft_recursive;
2530 did_filetype = TRUE;
2531 // Only pass TRUE for "force" when the value changed or not
2532 // used recursively, to avoid endless recurrence.
2533 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2534 value_changed || ft_recursive == 1, curbuf);
2535 --ft_recursive;
2536 // Just in case the old "curbuf" is now invalid.
2537 if (varp != &(curbuf->b_p_ft))
2538 varp = NULL;
2539
2540 secure = secure_save;
2541 }
2542 }
2543#ifdef FEAT_SPELL
2544 if (varp == &(curwin->w_s->b_p_spl))
2545 {
2546 char_u fname[200];
2547 char_u *q = curwin->w_s->b_p_spl;
2548
2549 // Skip the first name if it is "cjk".
2550 if (STRNCMP(q, "cjk,", 4) == 0)
2551 q += 4;
2552
2553 // Source the spell/LANG.vim in 'runtimepath'.
2554 // They could set 'spellcapcheck' depending on the language.
2555 // Use the first name in 'spelllang' up to '_region' or
2556 // '.encoding'.
2557 for (p = q; *p != NUL; ++p)
2558 if (!ASCII_ISALNUM(*p) && *p != '-')
2559 break;
2560 if (p > q)
2561 {
2562 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
2563 (int)(p - q), q);
2564 source_runtime(fname, DIP_ALL);
2565 }
2566 }
2567#endif
2568 }
2569
Bram Moolenaardac13472019-09-16 21:06:21 +02002570 if (varp == &p_mouse)
2571 {
Bram Moolenaardac13472019-09-16 21:06:21 +02002572 if (*p_mouse == NUL)
2573 mch_setmouse(FALSE); // switch mouse off
2574 else
Bram Moolenaardac13472019-09-16 21:06:21 +02002575 setmouse(); // in case 'mouse' changed
2576 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002577
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002578#if defined(FEAT_LUA) || defined(PROTO)
2579 if (varp == &p_rtp)
2580 update_package_paths_in_lua();
2581#endif
2582
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00002583#if defined(FEAT_LINEBREAK)
2584 // Changing Formatlistpattern when briopt includes the list setting:
2585 // redraw
2586 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
2587 && curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002588 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00002589#endif
2590
Bram Moolenaardac13472019-09-16 21:06:21 +02002591 if (curwin->w_curswant != MAXCOL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002592 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02002593 curwin->w_set_curswant = TRUE;
2594
Bram Moolenaar37294bd2021-03-10 13:40:08 +01002595 if ((opt_flags & OPT_NO_REDRAW) == 0)
2596 {
Bram Moolenaardac13472019-09-16 21:06:21 +02002597#ifdef FEAT_GUI
Bram Moolenaar37294bd2021-03-10 13:40:08 +01002598 // check redraw when it's not a GUI option or the GUI is active.
2599 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02002600#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01002601 check_redraw(get_option_flags(opt_idx));
2602 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002603
2604#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2605 if (did_swaptcap)
2606 {
2607 set_termname((char_u *)"win32");
2608 init_highlight(TRUE, FALSE);
2609 }
2610#endif
2611
2612 return errmsg;
2613}
2614
2615/*
2616 * Check an option that can be a range of string values.
2617 *
2618 * Return OK for correct value, FAIL otherwise.
2619 * Empty is always OK.
2620 */
2621 static int
2622check_opt_strings(
2623 char_u *val,
2624 char **values,
2625 int list) // when TRUE: accept a list of values
2626{
2627 return opt_strings_flags(val, values, NULL, list);
2628}
2629
2630/*
2631 * Handle an option that can be a range of string values.
2632 * Set a flag in "*flagp" for each string present.
2633 *
2634 * Return OK for correct value, FAIL otherwise.
2635 * Empty is always OK.
2636 */
2637 static int
2638opt_strings_flags(
2639 char_u *val, // new value
2640 char **values, // array of valid string values
2641 unsigned *flagp,
2642 int list) // when TRUE: accept a list of values
2643{
2644 int i;
2645 int len;
2646 unsigned new_flags = 0;
2647
2648 while (*val)
2649 {
2650 for (i = 0; ; ++i)
2651 {
2652 if (values[i] == NULL) // val not found in values[]
2653 return FAIL;
2654
2655 len = (int)STRLEN(values[i]);
2656 if (STRNCMP(values[i], val, len) == 0
2657 && ((list && val[len] == ',') || val[len] == NUL))
2658 {
2659 val += len + (val[len] == ',');
2660 new_flags |= (1 << i);
2661 break; // check next item in val list
2662 }
2663 }
2664 }
2665 if (flagp != NULL)
2666 *flagp = new_flags;
2667
2668 return OK;
2669}
2670
2671/*
2672 * return OK if "p" is a valid fileformat name, FAIL otherwise.
2673 */
2674 int
2675check_ff_value(char_u *p)
2676{
2677 return check_opt_strings(p, p_ff_values, FALSE);
2678}