blob: 32663a03e7832e17e1456d710fb324a625c277a6 [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
Bram Moolenaardac13472019-09-16 21:06:21 +02001329 // 'cedit'
1330 else if (varp == &p_cedit)
1331 {
1332 errmsg = check_cedit();
1333 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001334
1335 // 'verbosefile'
1336 else if (varp == &p_vfile)
1337 {
1338 verbose_stop();
1339 if (*p_vfile != NUL && verbose_open() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001340 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001341 }
1342
1343#ifdef FEAT_VIMINFO
1344 // 'viminfo'
1345 else if (varp == &p_viminfo)
1346 {
1347 for (s = p_viminfo; *s;)
1348 {
1349 // Check it's a valid character
1350 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
1351 {
1352 errmsg = illegal_char(errbuf, *s);
1353 break;
1354 }
1355 if (*s == 'n') // name is always last one
1356 break;
1357 else if (*s == 'r') // skip until next ','
1358 {
1359 while (*++s && *s != ',')
1360 ;
1361 }
1362 else if (*s == '%')
1363 {
1364 // optional number
1365 while (vim_isdigit(*++s))
1366 ;
1367 }
1368 else if (*s == '!' || *s == 'h' || *s == 'c')
1369 ++s; // no extra chars
1370 else // must have a number
1371 {
1372 while (vim_isdigit(*++s))
1373 ;
1374
1375 if (!VIM_ISDIGIT(*(s - 1)))
1376 {
1377 if (errbuf != NULL)
1378 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001379 sprintf(errbuf,
1380 _(e_missing_number_after_angle_str_angle),
Bram Moolenaardac13472019-09-16 21:06:21 +02001381 transchar_byte(*(s - 1)));
1382 errmsg = errbuf;
1383 }
1384 else
1385 errmsg = "";
1386 break;
1387 }
1388 }
1389 if (*s == ',')
1390 ++s;
1391 else if (*s)
1392 {
1393 if (errbuf != NULL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001394 errmsg = e_missing_comma;
Bram Moolenaardac13472019-09-16 21:06:21 +02001395 else
1396 errmsg = "";
1397 break;
1398 }
1399 }
1400 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001401 errmsg = e_must_specify_a_value;
Bram Moolenaardac13472019-09-16 21:06:21 +02001402 }
1403#endif // FEAT_VIMINFO
1404
1405 // terminal options
1406 else if (istermoption_idx(opt_idx) && full_screen)
1407 {
1408 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
1409 if (varp == &T_CCO)
1410 {
1411 int colors = atoi((char *)T_CCO);
1412
1413 // Only reinitialize colors if t_Co value has really changed to
1414 // avoid expensive reload of colorscheme if t_Co is set to the
1415 // same value multiple times.
1416 if (colors != t_colors)
1417 {
1418 t_colors = colors;
1419 if (t_colors <= 1)
1420 {
zeertzjqf6782732022-07-27 18:26:03 +01001421 vim_free(T_CCO);
Bram Moolenaardac13472019-09-16 21:06:21 +02001422 T_CCO = empty_option;
1423 }
1424#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1425 if (is_term_win32())
1426 {
1427 swap_tcap();
1428 did_swaptcap = TRUE;
1429 }
1430#endif
1431 // We now have a different color setup, initialize it again.
1432 init_highlight(TRUE, FALSE);
1433 }
1434 }
1435 ttest(FALSE);
1436 if (varp == &T_ME)
1437 {
1438 out_str(T_ME);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001439 redraw_later(UPD_CLEAR);
Bram Moolenaardac13472019-09-16 21:06:21 +02001440#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
1441 // Since t_me has been set, this probably means that the user
1442 // wants to use this as default colors. Need to reset default
1443 // background/foreground colors.
1444# ifdef VIMDLL
1445 if (!gui.in_use && !gui.starting)
1446# endif
1447 mch_set_normal_colors();
1448#endif
1449 }
1450 if (varp == &T_BE && termcap_active)
1451 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01001452 MAY_WANT_TO_LOG_THIS;
1453
Bram Moolenaardac13472019-09-16 21:06:21 +02001454 if (*T_BE == NUL)
1455 // When clearing t_BE we assume the user no longer wants
1456 // bracketed paste, thus disable it by writing t_BD.
1457 out_str(T_BD);
1458 else
1459 out_str(T_BE);
1460 }
1461 }
1462
1463#ifdef FEAT_LINEBREAK
1464 // 'showbreak'
Bram Moolenaaree857022019-11-09 23:26:40 +01001465 else if (gvarp == &p_sbr)
Bram Moolenaardac13472019-09-16 21:06:21 +02001466 {
Bram Moolenaaree857022019-11-09 23:26:40 +01001467 for (s = *varp; *s; )
Bram Moolenaardac13472019-09-16 21:06:21 +02001468 {
1469 if (ptr2cells(s) != 1)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001470 errmsg = e_showbreak_contains_unprintable_or_wide_character;
Bram Moolenaardac13472019-09-16 21:06:21 +02001471 MB_PTR_ADV(s);
1472 }
1473 }
1474#endif
1475
1476#ifdef FEAT_GUI
1477 // 'guifont'
1478 else if (varp == &p_guifont)
1479 {
1480 if (gui.in_use)
1481 {
1482 p = p_guifont;
1483# if defined(FEAT_GUI_GTK)
1484 // Put up a font dialog and let the user select a new value.
1485 // If this is cancelled go back to the old value but don't
1486 // give an error message.
1487 if (STRCMP(p, "*") == 0)
1488 {
1489 p = gui_mch_font_dialog(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01001490 free_string_option(p_guifont);
Bram Moolenaardac13472019-09-16 21:06:21 +02001491 p_guifont = (p != NULL) ? p : vim_strsave(oldval);
Bram Moolenaardac13472019-09-16 21:06:21 +02001492 }
1493# endif
1494 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
1495 {
1496# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
1497 if (STRCMP(p_guifont, "*") == 0)
1498 {
1499 // Dialog was cancelled: Keep the old value without giving
1500 // an error message.
zeertzjqf6782732022-07-27 18:26:03 +01001501 free_string_option(p_guifont);
Bram Moolenaardac13472019-09-16 21:06:21 +02001502 p_guifont = vim_strsave(oldval);
Bram Moolenaardac13472019-09-16 21:06:21 +02001503 }
1504 else
1505# endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001506 errmsg = e_invalid_fonts;
Bram Moolenaardac13472019-09-16 21:06:21 +02001507 }
1508 }
1509 redraw_gui_only = TRUE;
1510 }
1511# ifdef FEAT_XFONTSET
1512 else if (varp == &p_guifontset)
1513 {
1514 if (STRCMP(p_guifontset, "*") == 0)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001515 errmsg = e_cant_select_fontset;
Bram Moolenaardac13472019-09-16 21:06:21 +02001516 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001517 errmsg = e_invalid_fontset;
Bram Moolenaardac13472019-09-16 21:06:21 +02001518 redraw_gui_only = TRUE;
1519 }
1520# endif
1521 else if (varp == &p_guifontwide)
1522 {
1523 if (STRCMP(p_guifontwide, "*") == 0)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001524 errmsg = e_cant_select_wide_font;
Bram Moolenaardac13472019-09-16 21:06:21 +02001525 else if (gui_get_wide_font() == FAIL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001526 errmsg = e_invalid_wide_font;
Bram Moolenaardac13472019-09-16 21:06:21 +02001527 redraw_gui_only = TRUE;
1528 }
1529#endif
Dusan Popovic4eeedc02021-10-16 20:52:05 +01001530# if defined(FEAT_GUI_GTK)
1531 else if (varp == &p_guiligatures)
1532 {
1533 gui_set_ligatures();
1534 redraw_gui_only = TRUE;
1535 }
1536# endif
Bram Moolenaardac13472019-09-16 21:06:21 +02001537
1538#ifdef CURSOR_SHAPE
1539 // 'guicursor'
1540 else if (varp == &p_guicursor)
1541 errmsg = parse_shape_opt(SHAPE_CURSOR);
1542#endif
1543
1544#ifdef FEAT_MOUSESHAPE
1545 // 'mouseshape'
1546 else if (varp == &p_mouseshape)
1547 {
1548 errmsg = parse_shape_opt(SHAPE_MOUSE);
1549 update_mouseshape(-1);
1550 }
1551#endif
1552
1553#ifdef FEAT_PRINTER
1554 else if (varp == &p_popt)
1555 errmsg = parse_printoptions();
1556# if defined(FEAT_POSTSCRIPT)
1557 else if (varp == &p_pmfn)
1558 errmsg = parse_printmbfont();
1559# endif
1560#endif
1561
1562#ifdef FEAT_LANGMAP
1563 // 'langmap'
1564 else if (varp == &p_langmap)
1565 langmap_set();
1566#endif
1567
1568#ifdef FEAT_LINEBREAK
1569 // 'breakat'
1570 else if (varp == &p_breakat)
1571 fill_breakat_flags();
1572#endif
1573
Bram Moolenaardac13472019-09-16 21:06:21 +02001574 // 'titlestring' and 'iconstring'
1575 else if (varp == &p_titlestring || varp == &p_iconstring)
1576 {
Bram Moolenaar651fca82021-11-29 20:39:38 +00001577#ifdef FEAT_STL_OPT
Bram Moolenaardac13472019-09-16 21:06:21 +02001578 int flagval = (varp == &p_titlestring) ? STL_IN_TITLE : STL_IN_ICON;
1579
1580 // NULL => statusline syntax
1581 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
1582 stl_syntax |= flagval;
1583 else
1584 stl_syntax &= ~flagval;
Bram Moolenaar651fca82021-11-29 20:39:38 +00001585#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02001586 did_set_title();
1587 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001588
1589#ifdef FEAT_GUI
1590 // 'guioptions'
1591 else if (varp == &p_go)
1592 {
1593 gui_init_which_components(oldval);
1594 redraw_gui_only = TRUE;
1595 }
1596#endif
1597
1598#if defined(FEAT_GUI_TABLINE)
1599 // 'guitablabel'
1600 else if (varp == &p_gtl)
1601 {
1602 redraw_tabline = TRUE;
1603 redraw_gui_only = TRUE;
1604 }
1605 // 'guitabtooltip'
1606 else if (varp == &p_gtt)
1607 {
1608 redraw_gui_only = TRUE;
1609 }
1610#endif
1611
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001612#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +02001613 // 'ttymouse'
1614 else if (varp == &p_ttym)
1615 {
1616 // Switch the mouse off before changing the escape sequences used for
1617 // that.
1618 mch_setmouse(FALSE);
1619 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001620 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001621 else
1622 check_mouse_termcode();
1623 if (termcap_active)
1624 setmouse(); // may switch it on again
1625 }
1626#endif
1627
1628 // 'selection'
1629 else if (varp == &p_sel)
1630 {
1631 if (*p_sel == NUL
1632 || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001633 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001634 }
1635
1636 // 'selectmode'
1637 else if (varp == &p_slm)
1638 {
1639 if (check_opt_strings(p_slm, p_slm_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001640 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001641 }
1642
1643#ifdef FEAT_BROWSE
1644 // 'browsedir'
1645 else if (varp == &p_bsdir)
1646 {
1647 if (check_opt_strings(p_bsdir, p_bsdir_values, FALSE) != OK
1648 && !mch_isdir(p_bsdir))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001649 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001650 }
1651#endif
1652
1653 // 'keymodel'
1654 else if (varp == &p_km)
1655 {
1656 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001657 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001658 else
1659 {
1660 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
1661 km_startsel = (vim_strchr(p_km, 'a') != NULL);
1662 }
1663 }
1664
1665 // 'mousemodel'
1666 else if (varp == &p_mousem)
1667 {
1668 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001669 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001670#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
1671 else if (*p_mousem != *oldval)
1672 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
1673 // to create or delete the popup menus.
1674 gui_motif_update_mousemodel(root_menu);
1675#endif
1676 }
1677
1678 // 'switchbuf'
1679 else if (varp == &p_swb)
1680 {
1681 if (opt_strings_flags(p_swb, p_swb_values, &swb_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001682 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001683 }
1684
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001685 // 'splitkeep'
1686 else if (varp == &p_spk)
1687 {
1688 if (check_opt_strings(p_spk, p_spk_values, FALSE) != OK)
1689 errmsg = e_invalid_argument;
1690 }
1691
Bram Moolenaardac13472019-09-16 21:06:21 +02001692 // 'debug'
1693 else if (varp == &p_debug)
1694 {
1695 if (check_opt_strings(p_debug, p_debug_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001696 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001697 }
1698
1699 // 'display'
1700 else if (varp == &p_dy)
1701 {
1702 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001703 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001704 else
1705 (void)init_chartab();
1706
1707 }
1708
1709 // 'eadirection'
1710 else if (varp == &p_ead)
1711 {
1712 if (check_opt_strings(p_ead, p_ead_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001713 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001714 }
1715
1716#ifdef FEAT_CLIPBOARD
1717 // 'clipboard'
1718 else if (varp == &p_cb)
1719 errmsg = check_clipboard_option();
1720#endif
1721
1722#ifdef FEAT_SPELL
1723 // When 'spelllang' or 'spellfile' is set and there is a window for this
1724 // buffer in which 'spell' is set load the wordlists.
1725 else if (varp == &(curwin->w_s->b_p_spl)
1726 || varp == &(curwin->w_s->b_p_spf))
1727 {
1728 int is_spellfile = varp == &(curwin->w_s->b_p_spf);
1729
1730 if ((is_spellfile && !valid_spellfile(*varp))
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001731 || (!is_spellfile && !valid_spelllang(*varp)))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001732 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001733 else
1734 errmsg = did_set_spell_option(is_spellfile);
1735 }
1736 // When 'spellcapcheck' is set compile the regexp program.
1737 else if (varp == &(curwin->w_s->b_p_spc))
1738 {
1739 errmsg = compile_cap_prog(curwin->w_s);
1740 }
Bram Moolenaar362b44b2020-06-10 21:47:00 +02001741 // 'spelloptions'
1742 else if (varp == &(curwin->w_s->b_p_spo))
1743 {
1744 if (**varp != NUL && STRCMP("camel", *varp) != 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001745 errmsg = e_invalid_argument;
Bram Moolenaar362b44b2020-06-10 21:47:00 +02001746 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001747 // 'spellsuggest'
1748 else if (varp == &p_sps)
1749 {
1750 if (spell_check_sps() != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001751 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001752 }
1753 // 'mkspellmem'
1754 else if (varp == &p_msm)
1755 {
1756 if (spell_check_msm() != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001757 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001758 }
1759#endif
1760
1761 // When 'bufhidden' is set, check for valid value.
1762 else if (gvarp == &p_bh)
1763 {
1764 if (check_opt_strings(curbuf->b_p_bh, p_bufhidden_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001765 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001766 }
1767
1768 // When 'buftype' is set, check for valid value.
1769 else if (gvarp == &p_bt)
1770 {
1771 if (check_opt_strings(curbuf->b_p_bt, p_buftype_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001772 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001773 else
1774 {
1775 if (curwin->w_status_height)
1776 {
1777 curwin->w_redr_status = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001778 redraw_later(UPD_VALID);
Bram Moolenaardac13472019-09-16 21:06:21 +02001779 }
1780 curbuf->b_help = (curbuf->b_p_bt[0] == 'h');
Bram Moolenaardac13472019-09-16 21:06:21 +02001781 redraw_titles();
Bram Moolenaardac13472019-09-16 21:06:21 +02001782 }
1783 }
1784
1785#ifdef FEAT_STL_OPT
zeertzjq5dc294a2022-04-15 13:17:57 +01001786 // 'statusline', 'tabline' or 'rulerformat'
1787 else if (gvarp == &p_stl || varp == &p_tal || varp == &p_ruf)
Bram Moolenaardac13472019-09-16 21:06:21 +02001788 {
1789 int wid;
1790
1791 if (varp == &p_ruf) // reset ru_wid first
1792 ru_wid = 0;
1793 s = *varp;
1794 if (varp == &p_ruf && *s == '%')
1795 {
1796 // set ru_wid if 'ruf' starts with "%99("
1797 if (*++s == '-') // ignore a '-'
1798 s++;
1799 wid = getdigits(&s);
1800 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
1801 ru_wid = wid;
1802 else
1803 errmsg = check_stl_option(p_ruf);
1804 }
zeertzjq5dc294a2022-04-15 13:17:57 +01001805 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
Bram Moolenaardac13472019-09-16 21:06:21 +02001806 else if (varp == &p_ruf || s[0] != '%' || s[1] != '!')
1807 errmsg = check_stl_option(s);
1808 if (varp == &p_ruf && errmsg == NULL)
1809 comp_col();
1810 }
1811#endif
1812
1813 // check if it is a valid value for 'complete' -- Acevedo
1814 else if (gvarp == &p_cpt)
1815 {
1816 for (s = *varp; *s;)
1817 {
1818 while (*s == ',' || *s == ' ')
1819 s++;
1820 if (!*s)
1821 break;
1822 if (vim_strchr((char_u *)".wbuksid]tU", *s) == NULL)
1823 {
1824 errmsg = illegal_char(errbuf, *s);
1825 break;
1826 }
1827 if (*++s != NUL && *s != ',' && *s != ' ')
1828 {
1829 if (s[-1] == 'k' || s[-1] == 's')
1830 {
1831 // skip optional filename after 'k' and 's'
1832 while (*s && *s != ',' && *s != ' ')
1833 {
1834 if (*s == '\\' && s[1] != NUL)
1835 ++s;
1836 ++s;
1837 }
1838 }
1839 else
1840 {
1841 if (errbuf != NULL)
1842 {
1843 sprintf((char *)errbuf,
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001844 _(e_illegal_character_after_chr), *--s);
Bram Moolenaardac13472019-09-16 21:06:21 +02001845 errmsg = errbuf;
1846 }
1847 else
1848 errmsg = "";
1849 break;
1850 }
1851 }
1852 }
1853 }
1854
1855 // 'completeopt'
1856 else if (varp == &p_cot)
1857 {
1858 if (check_opt_strings(p_cot, p_cot_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001859 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001860 else
1861 completeopt_was_set();
1862 }
1863
1864#ifdef BACKSLASH_IN_FILENAME
1865 // 'completeslash'
1866 else if (gvarp == &p_csl)
1867 {
1868 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1869 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001870 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001871 }
1872#endif
1873
1874#ifdef FEAT_SIGNS
1875 // 'signcolumn'
1876 else if (varp == &curwin->w_p_scl)
1877 {
1878 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001879 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001880 // When changing the 'signcolumn' to or from 'number', recompute the
1881 // width of the number column if 'number' or 'relativenumber' is set.
1882 if (((*oldval == 'n' && *(oldval + 1) == 'u')
1883 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
1884 && (curwin->w_p_nu || curwin->w_p_rnu))
1885 curwin->w_nrwidth_line_count = 0;
1886 }
1887#endif
1888
1889
1890#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
1891 // 'toolbar'
1892 else if (varp == &p_toolbar)
1893 {
1894 if (opt_strings_flags(p_toolbar, p_toolbar_values,
1895 &toolbar_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001896 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001897 else
1898 {
1899 out_flush();
1900 gui_mch_show_toolbar((toolbar_flags &
1901 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
1902 }
1903 }
1904#endif
1905
1906#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
1907 // 'toolbariconsize': GTK+ 2 only
1908 else if (varp == &p_tbis)
1909 {
1910 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001911 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001912 else
1913 {
1914 out_flush();
1915 gui_mch_show_toolbar((toolbar_flags &
1916 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
1917 }
1918 }
1919#endif
1920
1921 // 'pastetoggle': translate key codes like in a mapping
1922 else if (varp == &p_pt)
1923 {
1924 if (*p_pt)
1925 {
Bram Moolenaar1e7b52a2019-10-13 16:59:08 +02001926 (void)replace_termcodes(p_pt, &p,
1927 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
Bram Moolenaardac13472019-09-16 21:06:21 +02001928 if (p != NULL)
1929 {
zeertzjqf6782732022-07-27 18:26:03 +01001930 free_string_option(p_pt);
Bram Moolenaardac13472019-09-16 21:06:21 +02001931 p_pt = p;
Bram Moolenaardac13472019-09-16 21:06:21 +02001932 }
1933 }
1934 }
1935
1936 // 'backspace'
1937 else if (varp == &p_bs)
1938 {
1939 if (VIM_ISDIGIT(*p_bs))
1940 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02001941 if (*p_bs > '3' || p_bs[1] != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001942 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001943 }
1944 else if (check_opt_strings(p_bs, p_bs_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001945 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001946 }
1947 else if (varp == &p_bo)
1948 {
1949 if (opt_strings_flags(p_bo, p_bo_values, &bo_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001950 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001951 }
1952
1953 // 'tagcase'
1954 else if (gvarp == &p_tc)
1955 {
1956 unsigned int *flags;
1957
1958 if (opt_flags & OPT_LOCAL)
1959 {
1960 p = curbuf->b_p_tc;
1961 flags = &curbuf->b_tc_flags;
1962 }
1963 else
1964 {
1965 p = p_tc;
1966 flags = &tc_flags;
1967 }
1968
1969 if ((opt_flags & OPT_LOCAL) && *p == NUL)
1970 // make the local value empty: use the global value
1971 *flags = 0;
1972 else if (*p == NUL
1973 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001974 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001975 }
1976
1977 // 'casemap'
1978 else if (varp == &p_cmp)
1979 {
1980 if (opt_strings_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001981 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001982 }
1983
1984#ifdef FEAT_DIFF
1985 // 'diffopt'
1986 else if (varp == &p_dip)
1987 {
1988 if (diffopt_changed() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001989 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001990 }
1991#endif
1992
1993#ifdef FEAT_FOLDING
1994 // 'foldmethod'
1995 else if (gvarp == &curwin->w_allbuf_opt.wo_fdm)
1996 {
1997 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK
1998 || *curwin->w_p_fdm == NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001999 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002000 else
2001 {
2002 foldUpdateAll(curwin);
2003 if (foldmethodIsDiff(curwin))
2004 newFoldLevel();
2005 }
2006 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002007 // 'foldmarker'
2008 else if (gvarp == &curwin->w_allbuf_opt.wo_fmr)
2009 {
2010 p = vim_strchr(*varp, ',');
2011 if (p == NULL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002012 errmsg = e_comma_required;
Bram Moolenaardac13472019-09-16 21:06:21 +02002013 else if (p == *varp || p[1] == NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002014 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002015 else if (foldmethodIsMarker(curwin))
2016 foldUpdateAll(curwin);
2017 }
2018 // 'commentstring'
2019 else if (gvarp == &p_cms)
2020 {
2021 if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002022 errmsg = e_commentstring_must_be_empty_or_contain_str;
Bram Moolenaardac13472019-09-16 21:06:21 +02002023 }
2024 // 'foldopen'
2025 else if (varp == &p_fdo)
2026 {
2027 if (opt_strings_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002028 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002029 }
2030 // 'foldclose'
2031 else if (varp == &p_fcl)
2032 {
2033 if (check_opt_strings(p_fcl, p_fcl_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002034 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002035 }
2036 // 'foldignore'
2037 else if (gvarp == &curwin->w_allbuf_opt.wo_fdi)
2038 {
2039 if (foldmethodIsIndent(curwin))
2040 foldUpdateAll(curwin);
2041 }
2042#endif
2043
2044 // 'virtualedit'
Gary Johnson53ba05b2021-07-26 22:19:10 +02002045 else if (gvarp == &p_ve)
Bram Moolenaardac13472019-09-16 21:06:21 +02002046 {
Gary Johnson53ba05b2021-07-26 22:19:10 +02002047 char_u *ve = p_ve;
2048 unsigned int *flags = &ve_flags;
2049
2050 if (opt_flags & OPT_LOCAL)
Bram Moolenaardac13472019-09-16 21:06:21 +02002051 {
Gary Johnson51ad8502021-08-03 18:33:08 +02002052 ve = curwin->w_p_ve;
2053 flags = &curwin->w_ve_flags;
Gary Johnson53ba05b2021-07-26 22:19:10 +02002054 }
2055
2056 if ((opt_flags & OPT_LOCAL) && *ve == NUL)
2057 // make the local value empty: use the global value
2058 *flags = 0;
2059 else
2060 {
2061 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002062 errmsg = e_invalid_argument;
Gary Johnson53ba05b2021-07-26 22:19:10 +02002063 else if (STRCMP(p_ve, oldval) != 0)
2064 {
2065 // Recompute cursor position in case the new 've' setting
2066 // changes something.
2067 validate_virtcol();
2068 coladvance(curwin->w_virtcol);
2069 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002070 }
2071 }
2072
2073#if defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)
2074 else if (varp == &p_csqf)
2075 {
2076 if (p_csqf != NULL)
2077 {
2078 p = p_csqf;
2079 while (*p != NUL)
2080 {
2081 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
2082 || p[1] == NUL
2083 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
2084 || (p[2] != NUL && p[2] != ','))
2085 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002086 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002087 break;
2088 }
2089 else if (p[2] == NUL)
2090 break;
2091 else
2092 p += 3;
2093 }
2094 }
2095 }
2096#endif
2097
Bram Moolenaardac13472019-09-16 21:06:21 +02002098 // 'cinoptions'
2099 else if (gvarp == &p_cino)
2100 {
2101 // TODO: recognize errors
2102 parse_cino(curbuf);
2103 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002104
2105#if defined(FEAT_RENDER_OPTIONS)
2106 // 'renderoptions'
2107 else if (varp == &p_rop)
2108 {
2109 if (!gui_mch_set_rendering_options(p_rop))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002110 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002111 }
2112#endif
2113
2114 else if (gvarp == &p_ft)
2115 {
2116 if (!valid_filetype(*varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002117 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002118 else
2119 {
2120 value_changed = STRCMP(oldval, *varp) != 0;
2121
2122 // Since we check the value, there is no need to set P_INSECURE,
2123 // even when the value comes from a modeline.
2124 *value_checked = TRUE;
2125 }
2126 }
2127
2128#ifdef FEAT_SYN_HL
2129 else if (gvarp == &p_syn)
2130 {
2131 if (!valid_filetype(*varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002132 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002133 else
2134 {
2135 value_changed = STRCMP(oldval, *varp) != 0;
2136
2137 // Since we check the value, there is no need to set P_INSECURE,
2138 // even when the value comes from a modeline.
2139 *value_checked = TRUE;
2140 }
2141 }
2142#endif
2143
2144#ifdef FEAT_TERMINAL
2145 // 'termwinkey'
2146 else if (varp == &curwin->w_p_twk)
2147 {
2148 if (*curwin->w_p_twk != NUL
2149 && string_to_key(curwin->w_p_twk, TRUE) == 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002150 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002151 }
2152 // 'termwinsize'
2153 else if (varp == &curwin->w_p_tws)
2154 {
2155 if (*curwin->w_p_tws != NUL)
2156 {
2157 p = skipdigits(curwin->w_p_tws);
2158 if (p == curwin->w_p_tws
2159 || (*p != 'x' && *p != '*')
2160 || *skipdigits(p + 1) != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002161 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002162 }
2163 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002164 // 'wincolor'
2165 else if (varp == &curwin->w_p_wcr)
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002166 term_update_wincolor(curwin);
Bram Moolenaardac13472019-09-16 21:06:21 +02002167# if defined(MSWIN)
2168 // 'termwintype'
2169 else if (varp == &p_twt)
2170 {
2171 if (check_opt_strings(*varp, p_twt_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002172 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002173 }
2174# endif
2175#endif
2176
2177#ifdef FEAT_VARTABS
2178 // 'varsofttabstop'
2179 else if (varp == &(curbuf->b_p_vsts))
2180 {
2181 char_u *cp;
2182
2183 if (!(*varp)[0] || ((*varp)[0] == '0' && !(*varp)[1]))
2184 {
2185 if (curbuf->b_p_vsts_array)
2186 {
2187 vim_free(curbuf->b_p_vsts_array);
2188 curbuf->b_p_vsts_array = 0;
2189 }
2190 }
2191 else
2192 {
2193 for (cp = *varp; *cp; ++cp)
2194 {
2195 if (vim_isdigit(*cp))
2196 continue;
2197 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
2198 continue;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002199 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002200 break;
2201 }
2202 if (errmsg == NULL)
2203 {
2204 int *oldarray = curbuf->b_p_vsts_array;
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002205 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Bram Moolenaardac13472019-09-16 21:06:21 +02002206 {
2207 if (oldarray)
2208 vim_free(oldarray);
2209 }
2210 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002211 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002212 }
2213 }
2214 }
2215
2216 // 'vartabstop'
2217 else if (varp == &(curbuf->b_p_vts))
2218 {
2219 char_u *cp;
2220
2221 if (!(*varp)[0] || ((*varp)[0] == '0' && !(*varp)[1]))
2222 {
2223 if (curbuf->b_p_vts_array)
2224 {
2225 vim_free(curbuf->b_p_vts_array);
2226 curbuf->b_p_vts_array = NULL;
2227 }
2228 }
2229 else
2230 {
2231 for (cp = *varp; *cp; ++cp)
2232 {
2233 if (vim_isdigit(*cp))
2234 continue;
2235 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
2236 continue;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002237 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002238 break;
2239 }
2240 if (errmsg == NULL)
2241 {
2242 int *oldarray = curbuf->b_p_vts_array;
2243
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002244 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Bram Moolenaardac13472019-09-16 21:06:21 +02002245 {
2246 vim_free(oldarray);
2247#ifdef FEAT_FOLDING
2248 if (foldmethodIsIndent(curwin))
2249 foldUpdateAll(curwin);
2250#endif
2251 }
2252 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002253 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002254 }
2255 }
2256 }
2257#endif
2258
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002259#ifdef FEAT_PROP_POPUP
Bram Moolenaardac13472019-09-16 21:06:21 +02002260 // 'previewpopup'
2261 else if (varp == &p_pvp)
2262 {
2263 if (parse_previewpopup(NULL) == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002264 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002265 }
2266# ifdef FEAT_QUICKFIX
2267 // 'completepopup'
2268 else if (varp == &p_cpp)
2269 {
2270 if (parse_completepopup(NULL) == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002271 errmsg = e_invalid_argument;
Bram Moolenaar447bfba2020-07-18 16:07:16 +02002272 else
2273 popup_close_info();
Bram Moolenaardac13472019-09-16 21:06:21 +02002274 }
2275# endif
2276#endif
2277
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002278#ifdef FEAT_EVAL
2279 // '*expr' options
2280 else if (
2281# ifdef FEAT_BEVAL
2282 varp == &p_bexpr ||
2283# endif
2284# ifdef FEAT_DIFF
2285 varp == &p_dex ||
2286# endif
2287# ifdef FEAT_FOLDING
2288 varp == &curwin->w_p_fde ||
Yegappan Lakshmanan27708e62021-12-26 21:54:43 +00002289 varp == &curwin->w_p_fdt ||
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002290# endif
2291 gvarp == &p_fex ||
2292# ifdef FEAT_FIND_ID
2293 gvarp == &p_inex ||
2294# endif
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002295 gvarp == &p_inde ||
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002296# ifdef FEAT_DIFF
2297 varp == &p_pex ||
2298# endif
2299# ifdef FEAT_POSTSCRIPT
2300 varp == &p_pexpr ||
2301# endif
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00002302 varp == &p_ccv)
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002303 {
2304 char_u **p_opt = NULL;
2305 char_u *name;
2306
2307 // If the option value starts with <SID> or s:, then replace that with
2308 // the script identifier.
2309# ifdef FEAT_BEVAL
2310 if (varp == &p_bexpr) // 'balloonexpr'
2311 p_opt = (opt_flags & OPT_LOCAL) ? &curbuf->b_p_bexpr : &p_bexpr;
2312# endif
2313# ifdef FEAT_DIFF
2314 if (varp == &p_dex) // 'diffexpr'
2315 p_opt = &p_dex;
2316# endif
2317# ifdef FEAT_FOLDING
Yegappan Lakshmanan27708e62021-12-26 21:54:43 +00002318 if (varp == &curwin->w_p_fde) // 'foldexpr'
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002319 p_opt = &curwin->w_p_fde;
Yegappan Lakshmanan27708e62021-12-26 21:54:43 +00002320 if (varp == &curwin->w_p_fdt) // 'foldtext'
2321 p_opt = &curwin->w_p_fdt;
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002322# endif
2323 if (gvarp == &p_fex) // 'formatexpr'
2324 p_opt = &curbuf->b_p_fex;
2325# ifdef FEAT_FIND_ID
2326 if (gvarp == &p_inex) // 'includeexpr'
2327 p_opt = &curbuf->b_p_inex;
2328# endif
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002329 if (gvarp == &p_inde) // 'indentexpr'
2330 p_opt = &curbuf->b_p_inde;
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002331# ifdef FEAT_DIFF
2332 if (varp == &p_pex) // 'patchexpr'
2333 p_opt = &p_pex;
2334# endif
2335# ifdef FEAT_POSTSCRIPT
2336 if (varp == &p_pexpr) // 'printexpr'
2337 p_opt = &p_pexpr;
2338# endif
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00002339 if (varp == &p_ccv) // 'charconvert'
2340 p_opt = &p_ccv;
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002341
2342 if (p_opt != NULL)
2343 {
2344 name = get_scriptlocal_funcname(*p_opt);
2345 if (name != NULL)
2346 {
zeertzjqf6782732022-07-27 18:26:03 +01002347 free_string_option(*p_opt);
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002348 *p_opt = name;
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002349 }
2350 }
2351
2352# ifdef FEAT_FOLDING
2353 if (varp == &curwin->w_p_fde && foldmethodIsExpr(curwin))
2354 foldUpdateAll(curwin);
2355# endif
2356 }
2357#endif
2358
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002359#ifdef FEAT_COMPL_FUNC
2360 // 'completefunc'
2361 else if (gvarp == &p_cfu)
2362 {
2363 if (set_completefunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002364 errmsg = e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002365 }
2366
2367 // 'omnifunc'
2368 else if (gvarp == &p_ofu)
2369 {
2370 if (set_omnifunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002371 errmsg = e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002372 }
2373
2374 // 'thesaurusfunc'
2375 else if (gvarp == &p_tsrfu)
2376 {
2377 if (set_thesaurusfunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002378 errmsg = e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002379 }
2380#endif
2381
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00002382#if defined(FEAT_EVAL) && \
2383 (defined(FEAT_XIM) || defined(IME_WITHOUT_XIM) || defined(VIMDLL))
2384 // 'imactivatefunc'
2385 else if (gvarp == &p_imaf)
2386 {
2387 if (set_imactivatefunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002388 errmsg = e_invalid_argument;
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00002389 }
2390
2391 // 'imstatusfunc'
2392 else if (gvarp == &p_imsf)
2393 {
2394 if (set_imstatusfunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002395 errmsg = e_invalid_argument;
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00002396 }
2397#endif
2398
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00002399 // 'operatorfunc'
2400 else if (varp == &p_opfunc)
2401 {
2402 if (set_operatorfunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002403 errmsg = e_invalid_argument;
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00002404 }
2405
Bram Moolenaard43906d2020-07-20 21:31:32 +02002406#ifdef FEAT_QUICKFIX
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00002407 // 'quickfixtextfunc'
Bram Moolenaard43906d2020-07-20 21:31:32 +02002408 else if (varp == &p_qftf)
2409 {
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00002410 if (qf_process_qftf_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002411 errmsg = e_invalid_argument;
Bram Moolenaard43906d2020-07-20 21:31:32 +02002412 }
2413#endif
2414
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00002415#ifdef FEAT_EVAL
2416 // 'tagfunc'
2417 else if (gvarp == &p_tfu)
2418 {
2419 if (set_tagfunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002420 errmsg = e_invalid_argument;
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00002421 }
2422#endif
2423
Bram Moolenaardac13472019-09-16 21:06:21 +02002424 // Options that are a list of flags.
2425 else
2426 {
2427 p = NULL;
2428 if (varp == &p_ww) // 'whichwrap'
2429 p = (char_u *)WW_ALL;
2430 if (varp == &p_shm) // 'shortmess'
2431 p = (char_u *)SHM_ALL;
2432 else if (varp == &(p_cpo)) // 'cpoptions'
2433 p = (char_u *)CPO_ALL;
2434 else if (varp == &(curbuf->b_p_fo)) // 'formatoptions'
2435 p = (char_u *)FO_ALL;
2436#ifdef FEAT_CONCEAL
2437 else if (varp == &curwin->w_p_cocu) // 'concealcursor'
2438 p = (char_u *)COCU_ALL;
2439#endif
2440 else if (varp == &p_mouse) // 'mouse'
2441 {
Bram Moolenaardac13472019-09-16 21:06:21 +02002442 p = (char_u *)MOUSE_ALL;
Bram Moolenaardac13472019-09-16 21:06:21 +02002443 }
2444#if defined(FEAT_GUI)
2445 else if (varp == &p_go) // 'guioptions'
2446 p = (char_u *)GO_ALL;
2447#endif
2448 if (p != NULL)
2449 {
2450 for (s = *varp; *s; ++s)
2451 if (vim_strchr(p, *s) == NULL)
2452 {
2453 errmsg = illegal_char(errbuf, *s);
2454 break;
2455 }
2456 }
2457 }
2458
2459 // If error detected, restore the previous value.
2460 if (errmsg != NULL)
2461 {
zeertzjqf6782732022-07-27 18:26:03 +01002462 free_string_option(*varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02002463 *varp = oldval;
2464 // When resetting some values, need to act on it.
2465 if (did_chartab)
2466 (void)init_chartab();
2467 if (varp == &p_hl)
2468 (void)highlight_changed();
2469 }
2470 else
2471 {
2472#ifdef FEAT_EVAL
2473 // Remember where the option was set.
2474 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
2475#endif
2476 // Free string options that are in allocated memory.
2477 // Use "free_oldval", because recursiveness may change the flags under
2478 // our fingers (esp. init_highlight()).
2479 if (free_oldval)
2480 free_string_option(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01002481 set_option_flag(opt_idx, P_ALLOCED);
Bram Moolenaardac13472019-09-16 21:06:21 +02002482
2483 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
2484 && is_global_local_option(opt_idx))
2485 {
2486 // global option with local value set to use global value; free
2487 // the local value and make it empty
2488 p = get_option_varp_scope(opt_idx, OPT_LOCAL);
2489 free_string_option(*(char_u **)p);
2490 *(char_u **)p = empty_option;
2491 }
2492
2493 // May set global value for local option.
2494 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
2495 set_string_option_global(opt_idx, varp);
2496
2497 // Trigger the autocommand only after setting the flags.
2498#ifdef FEAT_SYN_HL
2499 // When 'syntax' is set, load the syntax of that name
2500 if (varp == &(curbuf->b_p_syn))
2501 {
2502 static int syn_recursive = 0;
2503
2504 ++syn_recursive;
2505 // Only pass TRUE for "force" when the value changed or not used
2506 // recursively, to avoid endless recurrence.
2507 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
2508 value_changed || syn_recursive == 1, curbuf);
2509 curbuf->b_flags |= BF_SYN_SET;
2510 --syn_recursive;
2511 }
2512#endif
2513 else if (varp == &(curbuf->b_p_ft))
2514 {
2515 // 'filetype' is set, trigger the FileType autocommand.
2516 // Skip this when called from a modeline and the filetype was
2517 // already set to this value.
2518 if (!(opt_flags & OPT_MODELINE) || value_changed)
2519 {
2520 static int ft_recursive = 0;
2521 int secure_save = secure;
2522
2523 // Reset the secure flag, since the value of 'filetype' has
2524 // been checked to be safe.
2525 secure = 0;
2526
2527 ++ft_recursive;
2528 did_filetype = TRUE;
2529 // Only pass TRUE for "force" when the value changed or not
2530 // used recursively, to avoid endless recurrence.
2531 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2532 value_changed || ft_recursive == 1, curbuf);
2533 --ft_recursive;
2534 // Just in case the old "curbuf" is now invalid.
2535 if (varp != &(curbuf->b_p_ft))
2536 varp = NULL;
2537
2538 secure = secure_save;
2539 }
2540 }
2541#ifdef FEAT_SPELL
2542 if (varp == &(curwin->w_s->b_p_spl))
2543 {
2544 char_u fname[200];
2545 char_u *q = curwin->w_s->b_p_spl;
2546
2547 // Skip the first name if it is "cjk".
2548 if (STRNCMP(q, "cjk,", 4) == 0)
2549 q += 4;
2550
2551 // Source the spell/LANG.vim in 'runtimepath'.
2552 // They could set 'spellcapcheck' depending on the language.
2553 // Use the first name in 'spelllang' up to '_region' or
2554 // '.encoding'.
2555 for (p = q; *p != NUL; ++p)
2556 if (!ASCII_ISALNUM(*p) && *p != '-')
2557 break;
2558 if (p > q)
2559 {
2560 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
2561 (int)(p - q), q);
2562 source_runtime(fname, DIP_ALL);
2563 }
2564 }
2565#endif
2566 }
2567
Bram Moolenaardac13472019-09-16 21:06:21 +02002568 if (varp == &p_mouse)
2569 {
Bram Moolenaardac13472019-09-16 21:06:21 +02002570 if (*p_mouse == NUL)
2571 mch_setmouse(FALSE); // switch mouse off
2572 else
Bram Moolenaardac13472019-09-16 21:06:21 +02002573 setmouse(); // in case 'mouse' changed
2574 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002575
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002576#if defined(FEAT_LUA) || defined(PROTO)
2577 if (varp == &p_rtp)
2578 update_package_paths_in_lua();
2579#endif
2580
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00002581#if defined(FEAT_LINEBREAK)
2582 // Changing Formatlistpattern when briopt includes the list setting:
2583 // redraw
2584 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
2585 && curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002586 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00002587#endif
2588
Bram Moolenaardac13472019-09-16 21:06:21 +02002589 if (curwin->w_curswant != MAXCOL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002590 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02002591 curwin->w_set_curswant = TRUE;
2592
Bram Moolenaar37294bd2021-03-10 13:40:08 +01002593 if ((opt_flags & OPT_NO_REDRAW) == 0)
2594 {
Bram Moolenaardac13472019-09-16 21:06:21 +02002595#ifdef FEAT_GUI
Bram Moolenaar37294bd2021-03-10 13:40:08 +01002596 // check redraw when it's not a GUI option or the GUI is active.
2597 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02002598#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01002599 check_redraw(get_option_flags(opt_idx));
2600 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002601
2602#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2603 if (did_swaptcap)
2604 {
2605 set_termname((char_u *)"win32");
2606 init_highlight(TRUE, FALSE);
2607 }
2608#endif
2609
2610 return errmsg;
2611}
2612
2613/*
2614 * Check an option that can be a range of string values.
2615 *
2616 * Return OK for correct value, FAIL otherwise.
2617 * Empty is always OK.
2618 */
2619 static int
2620check_opt_strings(
2621 char_u *val,
2622 char **values,
2623 int list) // when TRUE: accept a list of values
2624{
2625 return opt_strings_flags(val, values, NULL, list);
2626}
2627
2628/*
2629 * Handle an option that can be a range of string values.
2630 * Set a flag in "*flagp" for each string present.
2631 *
2632 * Return OK for correct value, FAIL otherwise.
2633 * Empty is always OK.
2634 */
2635 static int
2636opt_strings_flags(
2637 char_u *val, // new value
2638 char **values, // array of valid string values
2639 unsigned *flagp,
2640 int list) // when TRUE: accept a list of values
2641{
2642 int i;
2643 int len;
2644 unsigned new_flags = 0;
2645
2646 while (*val)
2647 {
2648 for (i = 0; ; ++i)
2649 {
2650 if (values[i] == NULL) // val not found in values[]
2651 return FAIL;
2652
2653 len = (int)STRLEN(values[i]);
2654 if (STRNCMP(values[i], val, len) == 0
2655 && ((list && val[len] == ',') || val[len] == NUL))
2656 {
2657 val += len + (val[len] == ',');
2658 new_flags |= (1 << i);
2659 break; // check next item in val list
2660 }
2661 }
2662 }
2663 if (flagp != NULL)
2664 *flagp = new_flags;
2665
2666 return OK;
2667}
2668
2669/*
2670 * return OK if "p" is a valid fileformat name, FAIL otherwise.
2671 */
2672 int
2673check_ff_value(char_u *p)
2674{
2675 return check_opt_strings(p, p_ff_values, FALSE);
2676}