blob: 13302efbdc7f8db8a6f83a0fccd9b575042cbc6e [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};
Bram Moolenaardac13472019-09-16 21:06:21 +020049static char *(p_tc_values[]) = {"followic", "ignore", "match", "followscs", "smart", NULL};
50#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
51static char *(p_toolbar_values[]) = {"text", "icons", "tooltips", "horiz", NULL};
52#endif
53#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
54static char *(p_tbis_values[]) = {"tiny", "small", "medium", "large", "huge", "giant", NULL};
55#endif
Bram Moolenaara1cb1d12019-10-17 23:00:07 +020056#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +020057static char *(p_ttym_values[]) = {"xterm", "xterm2", "dec", "netterm", "jsbterm", "pterm", "urxvt", "sgr", NULL};
58#endif
Gary Johnson53ba05b2021-07-26 22:19:10 +020059static char *(p_ve_values[]) = {"block", "insert", "all", "onemore", "none", "NONE", NULL};
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000060static char *(p_wop_values[]) = {"fuzzy", "tagfile", "pum", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020061#ifdef FEAT_WAK
62static char *(p_wak_values[]) = {"yes", "menu", "no", NULL};
63#endif
64static char *(p_mousem_values[]) = {"extend", "popup", "popup_setpos", "mac", NULL};
65static char *(p_sel_values[]) = {"inclusive", "exclusive", "old", NULL};
66static char *(p_slm_values[]) = {"mouse", "key", "cmd", NULL};
67static char *(p_km_values[]) = {"startsel", "stopsel", NULL};
68#ifdef FEAT_BROWSE
69static char *(p_bsdir_values[]) = {"current", "last", "buffer", NULL};
70#endif
71static char *(p_scbopt_values[]) = {"ver", "hor", "jump", NULL};
72static char *(p_debug_values[]) = {"msg", "throw", "beep", NULL};
73static char *(p_ead_values[]) = {"both", "ver", "hor", NULL};
74static char *(p_buftype_values[]) = {"nofile", "nowrite", "quickfix", "help", "terminal", "acwrite", "prompt", "popup", NULL};
75static char *(p_bufhidden_values[]) = {"hide", "unload", "delete", "wipe", NULL};
Bram Moolenaaraa0489e2020-04-17 19:41:21 +020076static char *(p_bs_values[]) = {"indent", "eol", "start", "nostop", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020077#ifdef FEAT_FOLDING
78static char *(p_fdm_values[]) = {"manual", "expr", "marker", "indent", "syntax",
79# ifdef FEAT_DIFF
80 "diff",
81# endif
82 NULL};
83static char *(p_fcl_values[]) = {"all", NULL};
84#endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +020085static char *(p_cot_values[]) = {"menu", "menuone", "longest", "preview", "popup", "popuphidden", "noinsert", "noselect", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020086#ifdef BACKSLASH_IN_FILENAME
87static char *(p_csl_values[]) = {"slash", "backslash", NULL};
88#endif
89#ifdef FEAT_SIGNS
90static char *(p_scl_values[]) = {"yes", "no", "auto", "number", NULL};
91#endif
92#if defined(MSWIN) && defined(FEAT_TERMINAL)
93static char *(p_twt_values[]) = {"winpty", "conpty", "", NULL};
94#endif
95
96static int check_opt_strings(char_u *val, char **values, int list);
97static int opt_strings_flags(char_u *val, char **values, unsigned *flagp, int list);
98
99/*
100 * After setting various option values: recompute variables that depend on
101 * option values.
102 */
103 void
104didset_string_options(void)
105{
106 (void)opt_strings_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
107 (void)opt_strings_flags(p_bkc, p_bkc_values, &bkc_flags, TRUE);
108 (void)opt_strings_flags(p_bo, p_bo_values, &bo_flags, TRUE);
109#ifdef FEAT_SESSION
110 (void)opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE);
111 (void)opt_strings_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
112#endif
113#ifdef FEAT_FOLDING
114 (void)opt_strings_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
115#endif
116 (void)opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE);
117 (void)opt_strings_flags(p_tc, p_tc_values, &tc_flags, FALSE);
118 (void)opt_strings_flags(p_ve, p_ve_values, &ve_flags, TRUE);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +0200119#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +0200120 (void)opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE);
121#endif
122#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
123 (void)opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags, TRUE);
124#endif
125#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
126 (void)opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE);
127#endif
Sean Dewar39c46b42022-05-12 17:44:29 +0100128 (void)opt_strings_flags(p_swb, p_swb_values, &swb_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200129}
130
131#if defined(FEAT_EVAL)
132/*
133 * Trigger the OptionSet autocommand.
134 * "opt_idx" is the index of the option being set.
135 * "opt_flags" can be OPT_LOCAL etc.
136 * "oldval" the old value
137 * "oldval_l" the old local value (only non-NULL if global and local value
138 * are set)
139 * "oldval_g" the old global value (only non-NULL if global and local value
140 * are set)
141 * "newval" the new value
142 */
143 void
144trigger_optionsset_string(
145 int opt_idx,
146 int opt_flags,
147 char_u *oldval,
148 char_u *oldval_l,
149 char_u *oldval_g,
150 char_u *newval)
151{
152 // Don't do this recursively.
153 if (oldval != NULL && newval != NULL
154 && *get_vim_var_str(VV_OPTION_TYPE) == NUL)
155 {
156 char_u buf_type[7];
157
158 sprintf((char *)buf_type, "%s",
159 (opt_flags & OPT_LOCAL) ? "local" : "global");
160 set_vim_var_string(VV_OPTION_OLD, oldval, -1);
161 set_vim_var_string(VV_OPTION_NEW, newval, -1);
162 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
163 if (opt_flags & OPT_LOCAL)
164 {
165 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
166 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
167 }
168 if (opt_flags & OPT_GLOBAL)
169 {
170 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
171 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval, -1);
172 }
173 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
174 {
175 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
176 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval_l, -1);
177 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval_g, -1);
178 }
179 if (opt_flags & OPT_MODELINE)
180 {
181 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
182 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
183 }
184 apply_autocmds(EVENT_OPTIONSET,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000185 get_option_fullname(opt_idx), NULL, FALSE,
Bram Moolenaardac13472019-09-16 21:06:21 +0200186 NULL);
187 reset_v_option_vars();
188 }
189}
190#endif
191
192 static char *
193illegal_char(char *errbuf, int c)
194{
195 if (errbuf == NULL)
196 return "";
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000197 sprintf((char *)errbuf, _(e_illegal_character_str), (char *)transchar(c));
Bram Moolenaardac13472019-09-16 21:06:21 +0200198 return errbuf;
199}
200
201/*
202 * Check string options in a buffer for NULL value.
203 */
204 void
205check_buf_options(buf_T *buf)
206{
207 check_string_option(&buf->b_p_bh);
208 check_string_option(&buf->b_p_bt);
209 check_string_option(&buf->b_p_fenc);
210 check_string_option(&buf->b_p_ff);
211#ifdef FEAT_FIND_ID
212 check_string_option(&buf->b_p_def);
213 check_string_option(&buf->b_p_inc);
214# ifdef FEAT_EVAL
215 check_string_option(&buf->b_p_inex);
216# endif
217#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +0100218#if defined(FEAT_EVAL)
Bram Moolenaardac13472019-09-16 21:06:21 +0200219 check_string_option(&buf->b_p_inde);
220 check_string_option(&buf->b_p_indk);
221#endif
222#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
223 check_string_option(&buf->b_p_bexpr);
224#endif
225#if defined(FEAT_CRYPT)
226 check_string_option(&buf->b_p_cm);
227#endif
228 check_string_option(&buf->b_p_fp);
229#if defined(FEAT_EVAL)
230 check_string_option(&buf->b_p_fex);
231#endif
232#ifdef FEAT_CRYPT
233 check_string_option(&buf->b_p_key);
234#endif
235 check_string_option(&buf->b_p_kp);
236 check_string_option(&buf->b_p_mps);
237 check_string_option(&buf->b_p_fo);
238 check_string_option(&buf->b_p_flp);
239 check_string_option(&buf->b_p_isk);
Bram Moolenaardac13472019-09-16 21:06:21 +0200240 check_string_option(&buf->b_p_com);
Bram Moolenaardac13472019-09-16 21:06:21 +0200241#ifdef FEAT_FOLDING
242 check_string_option(&buf->b_p_cms);
243#endif
244 check_string_option(&buf->b_p_nf);
Bram Moolenaardac13472019-09-16 21:06:21 +0200245 check_string_option(&buf->b_p_qe);
Bram Moolenaardac13472019-09-16 21:06:21 +0200246#ifdef FEAT_SYN_HL
247 check_string_option(&buf->b_p_syn);
248 check_string_option(&buf->b_s.b_syn_isk);
249#endif
250#ifdef FEAT_SPELL
251 check_string_option(&buf->b_s.b_p_spc);
252 check_string_option(&buf->b_s.b_p_spf);
253 check_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +0200254 check_string_option(&buf->b_s.b_p_spo);
Bram Moolenaardac13472019-09-16 21:06:21 +0200255#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200256 check_string_option(&buf->b_p_sua);
Bram Moolenaardac13472019-09-16 21:06:21 +0200257 check_string_option(&buf->b_p_cink);
258 check_string_option(&buf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +0100259 check_string_option(&buf->b_p_cinsd);
Bram Moolenaardac13472019-09-16 21:06:21 +0200260 parse_cino(buf);
Bram Moolenaardac13472019-09-16 21:06:21 +0200261 check_string_option(&buf->b_p_ft);
Bram Moolenaardac13472019-09-16 21:06:21 +0200262 check_string_option(&buf->b_p_cinw);
Bram Moolenaardac13472019-09-16 21:06:21 +0200263 check_string_option(&buf->b_p_cpt);
264#ifdef FEAT_COMPL_FUNC
265 check_string_option(&buf->b_p_cfu);
266 check_string_option(&buf->b_p_ofu);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +0100267 check_string_option(&buf->b_p_tsrfu);
Bram Moolenaardac13472019-09-16 21:06:21 +0200268#endif
269#ifdef FEAT_EVAL
270 check_string_option(&buf->b_p_tfu);
271#endif
272#ifdef FEAT_KEYMAP
273 check_string_option(&buf->b_p_keymap);
274#endif
275#ifdef FEAT_QUICKFIX
276 check_string_option(&buf->b_p_gp);
277 check_string_option(&buf->b_p_mp);
278 check_string_option(&buf->b_p_efm);
279#endif
280 check_string_option(&buf->b_p_ep);
281 check_string_option(&buf->b_p_path);
282 check_string_option(&buf->b_p_tags);
283 check_string_option(&buf->b_p_tc);
284 check_string_option(&buf->b_p_dict);
285 check_string_option(&buf->b_p_tsr);
Bram Moolenaardac13472019-09-16 21:06:21 +0200286 check_string_option(&buf->b_p_lw);
Bram Moolenaardac13472019-09-16 21:06:21 +0200287 check_string_option(&buf->b_p_bkc);
288 check_string_option(&buf->b_p_menc);
289#ifdef FEAT_VARTABS
290 check_string_option(&buf->b_p_vsts);
291 check_string_option(&buf->b_p_vts);
292#endif
293}
294
295/*
296 * Free the string allocated for an option.
297 * Checks for the string being empty_option. This may happen if we're out of
298 * memory, vim_strsave() returned NULL, which was replaced by empty_option by
299 * check_options().
300 * Does NOT check for P_ALLOCED flag!
301 */
302 void
303free_string_option(char_u *p)
304{
305 if (p != empty_option)
306 vim_free(p);
307}
308
309 void
310clear_string_option(char_u **pp)
311{
312 if (*pp != empty_option)
313 vim_free(*pp);
314 *pp = empty_option;
315}
316
317 void
318check_string_option(char_u **pp)
319{
320 if (*pp == NULL)
321 *pp = empty_option;
322}
323
324/*
325 * Set global value for string option when it's a local option.
326 */
327 static void
328set_string_option_global(
329 int opt_idx, // option index
330 char_u **varp) // pointer to option variable
331{
332 char_u **p, *s;
333
334 // the global value is always allocated
335 if (is_window_local_option(opt_idx))
336 p = (char_u **)GLOBAL_WO(varp);
337 else
338 p = (char_u **)get_option_var(opt_idx);
339 if (!is_global_option(opt_idx)
340 && p != varp
341 && (s = vim_strsave(*varp)) != NULL)
342 {
343 free_string_option(*p);
344 *p = s;
345 }
346}
347
348/*
349 * Set a string option to a new value (without checking the effect).
350 * The string is copied into allocated memory.
351 * if ("opt_idx" == -1) "name" is used, otherwise "opt_idx" is used.
352 * When "set_sid" is zero set the scriptID to current_sctx.sc_sid. When
353 * "set_sid" is SID_NONE don't set the scriptID. Otherwise set the scriptID to
354 * "set_sid".
355 */
356 void
357set_string_option_direct(
358 char_u *name,
359 int opt_idx,
360 char_u *val,
361 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
362 int set_sid UNUSED)
363{
364 char_u *s;
365 char_u **varp;
366 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
367 int idx = opt_idx;
368
369 if (idx == -1) // use name
370 {
371 idx = findoption(name);
372 if (idx < 0) // not found (should not happen)
373 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000374 semsg(_(e_internal_error_str), "set_string_option_direct()");
Bram Moolenaardac13472019-09-16 21:06:21 +0200375 siemsg(_("For option %s"), name);
376 return;
377 }
378 }
379
380 if (is_hidden_option(idx)) // can't set hidden option
381 return;
382
383 s = vim_strsave(val);
384 if (s != NULL)
385 {
386 varp = (char_u **)get_option_varp_scope(idx,
387 both ? OPT_LOCAL : opt_flags);
388 if ((opt_flags & OPT_FREE) && (get_option_flags(idx) & P_ALLOCED))
389 free_string_option(*varp);
390 *varp = s;
391
392 // For buffer/window local option may also set the global value.
393 if (both)
394 set_string_option_global(idx, varp);
395
396 set_option_flag(idx, P_ALLOCED);
397
398 // When setting both values of a global option with a local value,
399 // make the local value empty, so that the global value is used.
400 if (is_global_local_option(idx) && both)
401 {
402 free_string_option(*varp);
403 *varp = empty_option;
404 }
405# ifdef FEAT_EVAL
406 if (set_sid != SID_NONE)
407 {
408 sctx_T script_ctx;
409
410 if (set_sid == 0)
411 script_ctx = current_sctx;
412 else
413 {
414 script_ctx.sc_sid = set_sid;
415 script_ctx.sc_seq = 0;
416 script_ctx.sc_lnum = 0;
417 script_ctx.sc_version = 1;
418 }
419 set_option_sctx_idx(idx, opt_flags, script_ctx);
420 }
421# endif
422 }
423}
424
425/*
426 * Like set_string_option_direct(), but for a window-local option in "wp".
427 * Blocks autocommands to avoid the old curwin becoming invalid.
428 */
429 void
430set_string_option_direct_in_win(
431 win_T *wp,
432 char_u *name,
433 int opt_idx,
434 char_u *val,
435 int opt_flags,
436 int set_sid)
437{
438 win_T *save_curwin = curwin;
439
440 block_autocmds();
441 curwin = wp;
442 curbuf = curwin->w_buffer;
443 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
444 curwin = save_curwin;
445 curbuf = curwin->w_buffer;
446 unblock_autocmds();
447}
448
Dominique Pelle748b3082022-01-08 12:41:16 +0000449#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200450/*
451 * Like set_string_option_direct(), but for a buffer-local option in "buf".
452 * Blocks autocommands to avoid the old curbuf becoming invalid.
453 */
454 void
455set_string_option_direct_in_buf(
456 buf_T *buf,
457 char_u *name,
458 int opt_idx,
459 char_u *val,
460 int opt_flags,
461 int set_sid)
462{
463 buf_T *save_curbuf = curbuf;
464
465 block_autocmds();
466 curbuf = buf;
467 curwin->w_buffer = curbuf;
468 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
469 curbuf = save_curbuf;
470 curwin->w_buffer = curbuf;
471 unblock_autocmds();
472}
Dominique Pelle748b3082022-01-08 12:41:16 +0000473#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200474
475/*
476 * Set a string option to a new value, and handle the effects.
477 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100478 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaardac13472019-09-16 21:06:21 +0200479 */
480 char *
481set_string_option(
482 int opt_idx,
483 char_u *value,
484 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
485{
486 char_u *s;
487 char_u **varp;
488 char_u *oldval;
489#if defined(FEAT_EVAL)
490 char_u *oldval_l = NULL;
491 char_u *oldval_g = NULL;
492 char_u *saved_oldval = NULL;
493 char_u *saved_oldval_l = NULL;
494 char_u *saved_oldval_g = NULL;
495 char_u *saved_newval = NULL;
496#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100497 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +0200498 int value_checked = FALSE;
499
500 if (is_hidden_option(opt_idx)) // don't set hidden option
501 return NULL;
502
Bram Moolenaar7f009df2020-03-16 20:27:38 +0100503 s = vim_strsave(value == NULL ? (char_u *)"" : value);
Bram Moolenaardac13472019-09-16 21:06:21 +0200504 if (s != NULL)
505 {
506 varp = (char_u **)get_option_varp_scope(opt_idx,
507 (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
508 ? (is_global_local_option(opt_idx)
509 ? OPT_GLOBAL : OPT_LOCAL)
510 : opt_flags);
511 oldval = *varp;
512#if defined(FEAT_EVAL)
513 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
514 {
515 oldval_l = *(char_u **)get_option_varp_scope(opt_idx, OPT_LOCAL);
516 oldval_g = *(char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
517 }
518#endif
519 *varp = s;
520
521#if defined(FEAT_EVAL)
522 if (!starting
523# ifdef FEAT_CRYPT
524 && !is_crypt_key_option(opt_idx)
525# endif
526 )
527 {
528 if (oldval_l != NULL)
529 saved_oldval_l = vim_strsave(oldval_l);
530 if (oldval_g != NULL)
531 saved_oldval_g = vim_strsave(oldval_g);
532 saved_oldval = vim_strsave(oldval);
533 saved_newval = vim_strsave(s);
534 }
535#endif
zeertzjqf6782732022-07-27 18:26:03 +0100536 if ((errmsg = did_set_string_option(opt_idx, varp, oldval, NULL,
Bram Moolenaardac13472019-09-16 21:06:21 +0200537 opt_flags, &value_checked)) == NULL)
538 did_set_option(opt_idx, opt_flags, TRUE, value_checked);
539
540#if defined(FEAT_EVAL)
541 // call autocommand after handling side effects
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100542 if (errmsg == NULL)
Bram Moolenaardac13472019-09-16 21:06:21 +0200543 trigger_optionsset_string(opt_idx, opt_flags,
544 saved_oldval, saved_oldval_l,
545 saved_oldval_g, saved_newval);
546 vim_free(saved_oldval);
547 vim_free(saved_oldval_l);
548 vim_free(saved_oldval_g);
549 vim_free(saved_newval);
550#endif
551 }
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100552 return errmsg;
Bram Moolenaardac13472019-09-16 21:06:21 +0200553}
554
555/*
556 * Return TRUE if "val" is a valid 'filetype' name.
557 * Also used for 'syntax' and 'keymap'.
558 */
559 static int
560valid_filetype(char_u *val)
561{
562 return valid_name(val, ".-_");
563}
564
565#ifdef FEAT_STL_OPT
566/*
567 * Check validity of options with the 'statusline' format.
zeertzjq5dc294a2022-04-15 13:17:57 +0100568 * Return an untranslated error message or NULL.
Bram Moolenaardac13472019-09-16 21:06:21 +0200569 */
570 static char *
571check_stl_option(char_u *s)
572{
Bram Moolenaardac13472019-09-16 21:06:21 +0200573 int groupdepth = 0;
574 static char errbuf[80];
575
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100576 while (*s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200577 {
578 // Check for valid keys after % sequences
579 while (*s && *s != '%')
580 s++;
581 if (!*s)
582 break;
583 s++;
Bram Moolenaardac13472019-09-16 21:06:21 +0200584 if (*s == '%' || *s == STL_TRUNCMARK || *s == STL_MIDDLEMARK)
585 {
586 s++;
587 continue;
588 }
589 if (*s == ')')
590 {
591 s++;
592 if (--groupdepth < 0)
593 break;
594 continue;
595 }
596 if (*s == '-')
597 s++;
598 while (VIM_ISDIGIT(*s))
599 s++;
600 if (*s == STL_USER_HL)
601 continue;
602 if (*s == '.')
603 {
604 s++;
605 while (*s && VIM_ISDIGIT(*s))
606 s++;
607 }
608 if (*s == '(')
609 {
610 groupdepth++;
611 continue;
612 }
613 if (vim_strchr(STL_ALL, *s) == NULL)
614 {
615 return illegal_char(errbuf, *s);
616 }
617 if (*s == '{')
618 {
zeertzjq5dc294a2022-04-15 13:17:57 +0100619 int reevaluate = (*++s == '%');
shadmansaleh30e3de22021-05-15 17:23:28 +0200620
zeertzjq5dc294a2022-04-15 13:17:57 +0100621 if (reevaluate && *++s == '}')
622 // "}" is not allowed immediately after "%{%"
623 return illegal_char(errbuf, '}');
shadmansaleh30e3de22021-05-15 17:23:28 +0200624 while ((*s != '}' || (reevaluate && s[-1] != '%')) && *s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200625 s++;
626 if (*s != '}')
zeertzjq5dc294a2022-04-15 13:17:57 +0100627 return e_unclosed_expression_sequence;
Bram Moolenaardac13472019-09-16 21:06:21 +0200628 }
629 }
Bram Moolenaardac13472019-09-16 21:06:21 +0200630 if (groupdepth != 0)
zeertzjq5dc294a2022-04-15 13:17:57 +0100631 return e_unbalanced_groups;
Bram Moolenaardac13472019-09-16 21:06:21 +0200632 return NULL;
633}
634#endif
635
636/*
637 * Handle string options that need some action to perform when changed.
zeertzjqf6782732022-07-27 18:26:03 +0100638 * The new value must be allocated.
LemonBoy77142312022-04-15 20:50:46 +0100639 * Returns NULL for success, or an unstranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +0200640 */
641 char *
642did_set_string_option(
643 int opt_idx, // index in options[] table
644 char_u **varp, // pointer to the option variable
Bram Moolenaardac13472019-09-16 21:06:21 +0200645 char_u *oldval, // previous value of the option
646 char *errbuf, // buffer for errors, or NULL
647 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
648 int *value_checked) // value was checked to be save, no
649 // need to set P_INSECURE
650{
651 char *errmsg = NULL;
652 char_u *s, *p;
653 int did_chartab = FALSE;
654 char_u **gvarp;
655 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
656#ifdef FEAT_GUI
657 // set when changing an option that only requires a redraw in the GUI
658 int redraw_gui_only = FALSE;
659#endif
660 int value_changed = FALSE;
661#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
662 int did_swaptcap = FALSE;
663#endif
664
665 // Get the global option to compare with, otherwise we would have to check
666 // two values for all local options.
667 gvarp = (char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
668
669 // Disallow changing some options from secure mode
670 if ((secure
671#ifdef HAVE_SANDBOX
672 || sandbox != 0
673#endif
674 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +0000675 errmsg = e_not_allowed_here;
Bram Moolenaardac13472019-09-16 21:06:21 +0200676
677 // Check for a "normal" directory or file name in some options. Disallow a
678 // path separator (slash and/or backslash), wildcards and characters that
679 // are often illegal in a file name. Be more permissive if "secure" is off.
680 else if (((get_option_flags(opt_idx) & P_NFNAME)
681 && vim_strpbrk(*varp, (char_u *)(secure
682 ? "/\\*?[|;&<>\r\n" : "/\\*?[<>\r\n")) != NULL)
683 || ((get_option_flags(opt_idx) & P_NDNAME)
684 && vim_strpbrk(*varp, (char_u *)"*?[|;&<>\r\n") != NULL))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000685 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200686
687 // 'term'
688 else if (varp == &T_NAME)
689 {
690 if (T_NAME[0] == NUL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000691 errmsg = e_cannot_set_term_to_empty_string;
Bram Moolenaardac13472019-09-16 21:06:21 +0200692#ifdef FEAT_GUI
Bram Moolenaar5daa9112021-02-01 18:39:47 +0100693 else if (gui.in_use)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000694 errmsg = e_cannot_change_term_in_GUI;
Bram Moolenaardac13472019-09-16 21:06:21 +0200695 else if (term_is_gui(T_NAME))
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000696 errmsg = e_use_gui_to_start_GUI;
Bram Moolenaardac13472019-09-16 21:06:21 +0200697#endif
698 else if (set_termname(T_NAME) == FAIL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000699 errmsg = e_not_found_in_termcap;
Bram Moolenaardac13472019-09-16 21:06:21 +0200700 else
701 {
702 // Screen colors may have changed.
703 redraw_later_clear();
704
705 // Both 'term' and 'ttytype' point to T_NAME, only set the
706 // P_ALLOCED flag on 'term'.
707 opt_idx = findoption((char_u *)"term");
708 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
709 }
710 }
711
712 // 'backupcopy'
713 else if (gvarp == &p_bkc)
714 {
715 char_u *bkc = p_bkc;
716 unsigned int *flags = &bkc_flags;
717
718 if (opt_flags & OPT_LOCAL)
719 {
720 bkc = curbuf->b_p_bkc;
721 flags = &curbuf->b_bkc_flags;
722 }
723
724 if ((opt_flags & OPT_LOCAL) && *bkc == NUL)
725 // make the local value empty: use the global value
726 *flags = 0;
727 else
728 {
729 if (opt_strings_flags(bkc, p_bkc_values, flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000730 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200731 if ((((int)*flags & BKC_AUTO) != 0)
732 + (((int)*flags & BKC_YES) != 0)
733 + (((int)*flags & BKC_NO) != 0) != 1)
734 {
735 // Must have exactly one of "auto", "yes" and "no".
736 (void)opt_strings_flags(oldval, p_bkc_values, flags, TRUE);
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000737 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200738 }
739 }
740 }
741
742 // 'backupext' and 'patchmode'
743 else if (varp == &p_bex || varp == &p_pm)
744 {
745 if (STRCMP(*p_bex == '.' ? p_bex + 1 : p_bex,
746 *p_pm == '.' ? p_pm + 1 : p_pm) == 0)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100747 errmsg = e_backupext_and_patchmode_are_equal;
Bram Moolenaardac13472019-09-16 21:06:21 +0200748 }
749#ifdef FEAT_LINEBREAK
750 // 'breakindentopt'
751 else if (varp == &curwin->w_p_briopt)
752 {
753 if (briopt_check(curwin) == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000754 errmsg = e_invalid_argument;
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000755 // list setting requires a redraw
756 if (curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100757 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaardac13472019-09-16 21:06:21 +0200758 }
759#endif
760
761 // 'isident', 'iskeyword', 'isprint or 'isfname' option: refill g_chartab[]
762 // If the new option is invalid, use old value. 'lisp' option: refill
763 // g_chartab[] for '-' char
764 else if ( varp == &p_isi
765 || varp == &(curbuf->b_p_isk)
766 || varp == &p_isp
767 || varp == &p_isf)
768 {
769 if (init_chartab() == FAIL)
770 {
771 did_chartab = TRUE; // need to restore it below
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000772 errmsg = e_invalid_argument; // error in value
Bram Moolenaardac13472019-09-16 21:06:21 +0200773 }
774 }
775
776 // 'helpfile'
777 else if (varp == &p_hf)
778 {
779 // May compute new values for $VIM and $VIMRUNTIME
780 if (didset_vim)
LemonBoy77142312022-04-15 20:50:46 +0100781 vim_unsetenv_ext((char_u *)"VIM");
Bram Moolenaardac13472019-09-16 21:06:21 +0200782 if (didset_vimruntime)
LemonBoy77142312022-04-15 20:50:46 +0100783 vim_unsetenv_ext((char_u *)"VIMRUNTIME");
Bram Moolenaardac13472019-09-16 21:06:21 +0200784 }
785
786#ifdef FEAT_SYN_HL
787 // 'cursorlineopt'
788 else if (varp == &curwin->w_p_culopt
789 || gvarp == &curwin->w_allbuf_opt.wo_culopt)
790 {
791 if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000792 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200793 }
794
795 // 'colorcolumn'
796 else if (varp == &curwin->w_p_cc)
797 errmsg = check_colorcolumn(curwin);
798#endif
799
800#ifdef FEAT_MULTI_LANG
801 // 'helplang'
802 else if (varp == &p_hlg)
803 {
804 // Check for "", "ab", "ab,cd", etc.
805 for (s = p_hlg; *s != NUL; s += 3)
806 {
807 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
808 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000809 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200810 break;
811 }
812 if (s[2] == NUL)
813 break;
814 }
815 }
816#endif
817
818 // 'highlight'
819 else if (varp == &p_hl)
820 {
821 if (highlight_changed() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000822 errmsg = e_invalid_argument; // invalid flags
Bram Moolenaardac13472019-09-16 21:06:21 +0200823 }
824
825 // 'nrformats'
826 else if (gvarp == &p_nf)
827 {
828 if (check_opt_strings(*varp, p_nf_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000829 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200830 }
831
832#ifdef FEAT_SESSION
833 // 'sessionoptions'
834 else if (varp == &p_ssop)
835 {
836 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000837 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200838 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
839 {
840 // Don't allow both "sesdir" and "curdir".
841 (void)opt_strings_flags(oldval, p_ssop_values, &ssop_flags, TRUE);
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000842 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200843 }
844 }
845 // 'viewoptions'
846 else if (varp == &p_vop)
847 {
848 if (opt_strings_flags(p_vop, p_ssop_values, &vop_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000849 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200850 }
851#endif
852
853 // 'scrollopt'
854 else if (varp == &p_sbo)
855 {
856 if (check_opt_strings(p_sbo, p_scbopt_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000857 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200858 }
859
860 // 'ambiwidth'
861 else if (varp == &p_ambw || varp == &p_emoji)
862 {
863 if (check_opt_strings(p_ambw, p_ambw_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000864 errmsg = e_invalid_argument;
Bram Moolenaareed9d462021-02-15 20:38:25 +0100865 else
zeertzjq8ca29b62022-08-09 12:53:14 +0100866 errmsg = check_chars_options();
Bram Moolenaardac13472019-09-16 21:06:21 +0200867 }
868
869 // 'background'
870 else if (varp == &p_bg)
871 {
872 if (check_opt_strings(p_bg, p_bg_values, FALSE) == OK)
873 {
874#ifdef FEAT_EVAL
875 int dark = (*p_bg == 'd');
876#endif
877
878 init_highlight(FALSE, FALSE);
879
880#ifdef FEAT_EVAL
881 if (dark != (*p_bg == 'd')
882 && get_var_value((char_u *)"g:colors_name") != NULL)
883 {
884 // The color scheme must have set 'background' back to another
885 // value, that's not what we want here. Disable the color
886 // scheme and set the colors again.
887 do_unlet((char_u *)"g:colors_name", TRUE);
888 free_string_option(p_bg);
889 p_bg = vim_strsave((char_u *)(dark ? "dark" : "light"));
890 check_string_option(&p_bg);
891 init_highlight(FALSE, FALSE);
892 }
893#endif
Bram Moolenaarad431992021-05-03 20:40:38 +0200894#ifdef FEAT_TERMINAL
895 term_update_colors_all();
896#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200897 }
898 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000899 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200900 }
901
902 // 'wildmode'
903 else if (varp == &p_wim)
904 {
905 if (check_opt_wim() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000906 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200907 }
908
909 // 'wildoptions'
910 else if (varp == &p_wop)
911 {
912 if (check_opt_strings(p_wop, p_wop_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000913 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200914 }
915
916#ifdef FEAT_WAK
917 // 'winaltkeys'
918 else if (varp == &p_wak)
919 {
920 if (*p_wak == NUL
921 || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000922 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200923# ifdef FEAT_MENU
ichizok02560422022-04-05 14:18:44 +0100924# if defined(FEAT_GUI_MOTIF)
Bram Moolenaardac13472019-09-16 21:06:21 +0200925 else if (gui.in_use)
926 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
ichizok02560422022-04-05 14:18:44 +0100927# elif defined(FEAT_GUI_GTK)
Bram Moolenaardac13472019-09-16 21:06:21 +0200928 else if (gui.in_use)
929 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
Bram Moolenaardac13472019-09-16 21:06:21 +0200930# endif
931# endif
932 }
933#endif
934
935 // 'eventignore'
936 else if (varp == &p_ei)
937 {
938 if (check_ei() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000939 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200940 }
941
942 // 'encoding', 'fileencoding', 'termencoding' and 'makeencoding'
943 else if (varp == &p_enc || gvarp == &p_fenc || varp == &p_tenc
944 || gvarp == &p_menc)
945 {
946 if (gvarp == &p_fenc)
947 {
948 if (!curbuf->b_p_ma && opt_flags != OPT_GLOBAL)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200949 errmsg = e_cannot_make_changes_modifiable_is_off;
Bram Moolenaardac13472019-09-16 21:06:21 +0200950 else if (vim_strchr(*varp, ',') != NULL)
951 // No comma allowed in 'fileencoding'; catches confusing it
952 // with 'fileencodings'.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000953 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200954 else
955 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200956 // May show a "+" in the title now.
957 redraw_titles();
Bram Moolenaardac13472019-09-16 21:06:21 +0200958 // Add 'fileencoding' to the swap file.
959 ml_setflags(curbuf);
960 }
961 }
962 if (errmsg == NULL)
963 {
964 // canonize the value, so that STRCMP() can be used on it
965 p = enc_canonize(*varp);
966 if (p != NULL)
967 {
968 vim_free(*varp);
969 *varp = p;
970 }
971 if (varp == &p_enc)
972 {
973 errmsg = mb_init();
Bram Moolenaardac13472019-09-16 21:06:21 +0200974 redraw_titles();
Bram Moolenaardac13472019-09-16 21:06:21 +0200975 }
976 }
977
978#if defined(FEAT_GUI_GTK)
979 if (errmsg == NULL && varp == &p_tenc && gui.in_use)
980 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +0000981 // GTK uses only a single encoding, and that is UTF-8.
Bram Moolenaardac13472019-09-16 21:06:21 +0200982 if (STRCMP(p_tenc, "utf-8") != 0)
Bram Moolenaard88be5b2022-01-04 19:57:55 +0000983 errmsg = e_cannot_be_changed_in_gtk_GUI;
Bram Moolenaardac13472019-09-16 21:06:21 +0200984 }
985#endif
986
987 if (errmsg == NULL)
988 {
989#ifdef FEAT_KEYMAP
990 // When 'keymap' is used and 'encoding' changes, reload the keymap
991 // (with another encoding).
992 if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
993 (void)keymap_init();
994#endif
995
996 // When 'termencoding' is not empty and 'encoding' changes or when
997 // 'termencoding' changes, need to setup for keyboard input and
998 // display output conversion.
999 if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
1000 {
1001 if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
1002 || convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
1003 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001004 semsg(_(e_cannot_convert_between_str_and_str),
1005 p_tenc, p_enc);
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001006 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001007 }
1008 }
1009
1010#if defined(MSWIN)
1011 // $HOME may have characters in active code page.
1012 if (varp == &p_enc)
1013 init_homedir();
1014#endif
1015 }
1016 }
1017
1018#if defined(FEAT_POSTSCRIPT)
1019 else if (varp == &p_penc)
1020 {
1021 // Canonize printencoding if VIM standard one
1022 p = enc_canonize(p_penc);
1023 if (p != NULL)
1024 {
1025 vim_free(p_penc);
1026 p_penc = p;
1027 }
1028 else
1029 {
1030 // Ensure lower case and '-' for '_'
1031 for (s = p_penc; *s != NUL; s++)
1032 {
1033 if (*s == '_')
1034 *s = '-';
1035 else
1036 *s = TOLOWER_ASC(*s);
1037 }
1038 }
1039 }
1040#endif
1041
1042#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
1043 else if (varp == &p_imak)
1044 {
1045 if (!im_xim_isvalid_imactivate())
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001046 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001047 }
1048#endif
1049
1050#ifdef FEAT_KEYMAP
1051 else if (varp == &curbuf->b_p_keymap)
1052 {
1053 if (!valid_filetype(*varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001054 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001055 else
1056 {
1057 int secure_save = secure;
1058
1059 // Reset the secure flag, since the value of 'keymap' has
1060 // been checked to be safe.
1061 secure = 0;
1062
1063 // load or unload key mapping tables
1064 errmsg = keymap_init();
1065
1066 secure = secure_save;
1067
1068 // Since we check the value, there is no need to set P_INSECURE,
1069 // even when the value comes from a modeline.
1070 *value_checked = TRUE;
1071 }
1072
1073 if (errmsg == NULL)
1074 {
1075 if (*curbuf->b_p_keymap != NUL)
1076 {
1077 // Installed a new keymap, switch on using it.
1078 curbuf->b_p_iminsert = B_IMODE_LMAP;
1079 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
1080 curbuf->b_p_imsearch = B_IMODE_LMAP;
1081 }
1082 else
1083 {
1084 // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
1085 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
1086 curbuf->b_p_iminsert = B_IMODE_NONE;
1087 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
1088 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
1089 }
1090 if ((opt_flags & OPT_LOCAL) == 0)
1091 {
1092 set_iminsert_global();
1093 set_imsearch_global();
1094 }
1095 status_redraw_curbuf();
1096 }
1097 }
1098#endif
1099
1100 // 'fileformat'
1101 else if (gvarp == &p_ff)
1102 {
1103 if (!curbuf->b_p_ma && !(opt_flags & OPT_GLOBAL))
Bram Moolenaar108010a2021-06-27 22:03:33 +02001104 errmsg = e_cannot_make_changes_modifiable_is_off;
Bram Moolenaardac13472019-09-16 21:06:21 +02001105 else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001106 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001107 else
1108 {
1109 // may also change 'textmode'
1110 if (get_fileformat(curbuf) == EOL_DOS)
1111 curbuf->b_p_tx = TRUE;
1112 else
1113 curbuf->b_p_tx = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02001114 redraw_titles();
Bram Moolenaardac13472019-09-16 21:06:21 +02001115 // update flag in swap file
1116 ml_setflags(curbuf);
1117 // Redraw needed when switching to/from "mac": a CR in the text
1118 // will be displayed differently.
1119 if (get_fileformat(curbuf) == EOL_MAC || *oldval == 'm')
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001120 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaardac13472019-09-16 21:06:21 +02001121 }
1122 }
1123
1124 // 'fileformats'
1125 else if (varp == &p_ffs)
1126 {
1127 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001128 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001129 else
1130 {
1131 // also change 'textauto'
1132 if (*p_ffs == NUL)
1133 p_ta = FALSE;
1134 else
1135 p_ta = TRUE;
1136 }
1137 }
1138
1139#if defined(FEAT_CRYPT)
1140 // 'cryptkey'
1141 else if (gvarp == &p_key)
1142 {
1143 // Make sure the ":set" command doesn't show the new value in the
1144 // history.
1145 remove_key_from_history();
1146
1147 if (STRCMP(curbuf->b_p_key, oldval) != 0)
1148 // Need to update the swapfile.
Bram Moolenaar76cb6832020-05-15 22:30:38 +02001149 {
Bram Moolenaardac13472019-09-16 21:06:21 +02001150 ml_set_crypt_key(curbuf, oldval,
1151 *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
Bram Moolenaar76cb6832020-05-15 22:30:38 +02001152 changed_internal();
1153 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001154 }
1155
1156 else if (gvarp == &p_cm)
1157 {
1158 if (opt_flags & OPT_LOCAL)
1159 p = curbuf->b_p_cm;
1160 else
1161 p = p_cm;
1162 if (check_opt_strings(p, p_cm_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001163 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001164 else if (crypt_self_test() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001165 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001166 else
1167 {
1168 // When setting the global value to empty, make it "zip".
1169 if (*p_cm == NUL)
1170 {
zeertzjqf6782732022-07-27 18:26:03 +01001171 free_string_option(p_cm);
Bram Moolenaardac13472019-09-16 21:06:21 +02001172 p_cm = vim_strsave((char_u *)"zip");
Bram Moolenaardac13472019-09-16 21:06:21 +02001173 }
1174 // When using ":set cm=name" the local value is going to be empty.
1175 // Do that here, otherwise the crypt functions will still use the
1176 // local value.
1177 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1178 {
1179 free_string_option(curbuf->b_p_cm);
1180 curbuf->b_p_cm = empty_option;
1181 }
1182
1183 // Need to update the swapfile when the effective method changed.
1184 // Set "s" to the effective old value, "p" to the effective new
1185 // method and compare.
1186 if ((opt_flags & OPT_LOCAL) && *oldval == NUL)
1187 s = p_cm; // was previously using the global value
1188 else
1189 s = oldval;
1190 if (*curbuf->b_p_cm == NUL)
1191 p = p_cm; // is now using the global value
1192 else
1193 p = curbuf->b_p_cm;
1194 if (STRCMP(s, p) != 0)
1195 ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1196
1197 // If the global value changes need to update the swapfile for all
1198 // buffers using that value.
1199 if ((opt_flags & OPT_GLOBAL) && STRCMP(p_cm, oldval) != 0)
1200 {
1201 buf_T *buf;
1202
1203 FOR_ALL_BUFFERS(buf)
1204 if (buf != curbuf && *buf->b_p_cm == NUL)
1205 ml_set_crypt_key(buf, buf->b_p_key, oldval);
1206 }
1207 }
1208 }
1209#endif
1210
1211 // 'matchpairs'
1212 else if (gvarp == &p_mps)
1213 {
1214 if (has_mbyte)
1215 {
1216 for (p = *varp; *p != NUL; ++p)
1217 {
1218 int x2 = -1;
1219 int x3 = -1;
1220
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01001221 p += mb_ptr2len(p);
Bram Moolenaardac13472019-09-16 21:06:21 +02001222 if (*p != NUL)
1223 x2 = *p++;
1224 if (*p != NUL)
1225 {
1226 x3 = mb_ptr2char(p);
1227 p += mb_ptr2len(p);
1228 }
1229 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
1230 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001231 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001232 break;
1233 }
1234 if (*p == NUL)
1235 break;
1236 }
1237 }
1238 else
1239 {
1240 // Check for "x:y,x:y"
1241 for (p = *varp; *p != NUL; p += 4)
1242 {
1243 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
1244 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001245 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001246 break;
1247 }
1248 if (p[3] == NUL)
1249 break;
1250 }
1251 }
1252 }
1253
Bram Moolenaardac13472019-09-16 21:06:21 +02001254 // 'comments'
1255 else if (gvarp == &p_com)
1256 {
1257 for (s = *varp; *s; )
1258 {
1259 while (*s && *s != ':')
1260 {
1261 if (vim_strchr((char_u *)COM_ALL, *s) == NULL
1262 && !VIM_ISDIGIT(*s) && *s != '-')
1263 {
1264 errmsg = illegal_char(errbuf, *s);
1265 break;
1266 }
1267 ++s;
1268 }
1269 if (*s++ == NUL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001270 errmsg = e_missing_colon;
Bram Moolenaardac13472019-09-16 21:06:21 +02001271 else if (*s == ',' || *s == NUL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001272 errmsg = e_zero_length_string;
Bram Moolenaardac13472019-09-16 21:06:21 +02001273 if (errmsg != NULL)
1274 break;
1275 while (*s && *s != ',')
1276 {
1277 if (*s == '\\' && s[1] != NUL)
1278 ++s;
1279 ++s;
1280 }
1281 s = skip_to_option_part(s);
1282 }
1283 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001284
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01001285 // global 'listchars' or 'fillchars'
1286 else if (varp == &p_lcs || varp == &p_fcs)
Bram Moolenaardac13472019-09-16 21:06:21 +02001287 {
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01001288 char_u **local_ptr = varp == &p_lcs
1289 ? &curwin->w_p_lcs : &curwin->w_p_fcs;
1290
1291 // only apply the global value to "curwin" when it does not have a
1292 // local value
1293 errmsg = set_chars_option(curwin, varp,
1294 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL));
Bram Moolenaareed9d462021-02-15 20:38:25 +01001295 if (errmsg == NULL)
1296 {
1297 tabpage_T *tp;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001298 win_T *wp;
Bram Moolenaareed9d462021-02-15 20:38:25 +01001299
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01001300 // If the current window is set to use the global
1301 // 'listchars'/'fillchars' value, clear the window-local value.
Bram Moolenaareed9d462021-02-15 20:38:25 +01001302 if (!(opt_flags & OPT_GLOBAL))
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01001303 clear_string_option(local_ptr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01001304 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01001305 {
Bram Moolenaar5ed26fa2022-07-04 18:05:51 +01001306 // If the current window has a local value need to apply it
1307 // again, it was changed when setting the global value.
Bram Moolenaar606efc72021-11-12 19:52:47 +00001308 // If no error was returned above, we don't expect an error
1309 // here, so ignore the return value.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01001310 local_ptr = varp == &p_lcs ? &wp->w_p_lcs : &wp->w_p_fcs;
1311 if (**local_ptr == NUL)
1312 (void)set_chars_option(wp, local_ptr, TRUE);
1313 }
Bram Moolenaar606efc72021-11-12 19:52:47 +00001314
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001315 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01001316 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001317 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001318 // local 'listchars'
1319 else if (varp == &curwin->w_p_lcs)
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01001320 errmsg = set_chars_option(curwin, varp, TRUE);
Bram Moolenaareed9d462021-02-15 20:38:25 +01001321
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001322 // local 'fillchars'
1323 else if (varp == &curwin->w_p_fcs)
1324 {
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01001325 errmsg = set_chars_option(curwin, varp, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +02001326 }
1327
1328#ifdef FEAT_CMDWIN
1329 // 'cedit'
1330 else if (varp == &p_cedit)
1331 {
1332 errmsg = check_cedit();
1333 }
1334#endif
1335
1336 // 'verbosefile'
1337 else if (varp == &p_vfile)
1338 {
1339 verbose_stop();
1340 if (*p_vfile != NUL && verbose_open() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001341 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001342 }
1343
1344#ifdef FEAT_VIMINFO
1345 // 'viminfo'
1346 else if (varp == &p_viminfo)
1347 {
1348 for (s = p_viminfo; *s;)
1349 {
1350 // Check it's a valid character
1351 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
1352 {
1353 errmsg = illegal_char(errbuf, *s);
1354 break;
1355 }
1356 if (*s == 'n') // name is always last one
1357 break;
1358 else if (*s == 'r') // skip until next ','
1359 {
1360 while (*++s && *s != ',')
1361 ;
1362 }
1363 else if (*s == '%')
1364 {
1365 // optional number
1366 while (vim_isdigit(*++s))
1367 ;
1368 }
1369 else if (*s == '!' || *s == 'h' || *s == 'c')
1370 ++s; // no extra chars
1371 else // must have a number
1372 {
1373 while (vim_isdigit(*++s))
1374 ;
1375
1376 if (!VIM_ISDIGIT(*(s - 1)))
1377 {
1378 if (errbuf != NULL)
1379 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001380 sprintf(errbuf,
1381 _(e_missing_number_after_angle_str_angle),
Bram Moolenaardac13472019-09-16 21:06:21 +02001382 transchar_byte(*(s - 1)));
1383 errmsg = errbuf;
1384 }
1385 else
1386 errmsg = "";
1387 break;
1388 }
1389 }
1390 if (*s == ',')
1391 ++s;
1392 else if (*s)
1393 {
1394 if (errbuf != NULL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001395 errmsg = e_missing_comma;
Bram Moolenaardac13472019-09-16 21:06:21 +02001396 else
1397 errmsg = "";
1398 break;
1399 }
1400 }
1401 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001402 errmsg = e_must_specify_a_value;
Bram Moolenaardac13472019-09-16 21:06:21 +02001403 }
1404#endif // FEAT_VIMINFO
1405
1406 // terminal options
1407 else if (istermoption_idx(opt_idx) && full_screen)
1408 {
1409 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
1410 if (varp == &T_CCO)
1411 {
1412 int colors = atoi((char *)T_CCO);
1413
1414 // Only reinitialize colors if t_Co value has really changed to
1415 // avoid expensive reload of colorscheme if t_Co is set to the
1416 // same value multiple times.
1417 if (colors != t_colors)
1418 {
1419 t_colors = colors;
1420 if (t_colors <= 1)
1421 {
zeertzjqf6782732022-07-27 18:26:03 +01001422 vim_free(T_CCO);
Bram Moolenaardac13472019-09-16 21:06:21 +02001423 T_CCO = empty_option;
1424 }
1425#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1426 if (is_term_win32())
1427 {
1428 swap_tcap();
1429 did_swaptcap = TRUE;
1430 }
1431#endif
1432 // We now have a different color setup, initialize it again.
1433 init_highlight(TRUE, FALSE);
1434 }
1435 }
1436 ttest(FALSE);
1437 if (varp == &T_ME)
1438 {
1439 out_str(T_ME);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001440 redraw_later(UPD_CLEAR);
Bram Moolenaardac13472019-09-16 21:06:21 +02001441#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
1442 // Since t_me has been set, this probably means that the user
1443 // wants to use this as default colors. Need to reset default
1444 // background/foreground colors.
1445# ifdef VIMDLL
1446 if (!gui.in_use && !gui.starting)
1447# endif
1448 mch_set_normal_colors();
1449#endif
1450 }
1451 if (varp == &T_BE && termcap_active)
1452 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01001453 MAY_WANT_TO_LOG_THIS;
1454
Bram Moolenaardac13472019-09-16 21:06:21 +02001455 if (*T_BE == NUL)
1456 // When clearing t_BE we assume the user no longer wants
1457 // bracketed paste, thus disable it by writing t_BD.
1458 out_str(T_BD);
1459 else
1460 out_str(T_BE);
1461 }
1462 }
1463
1464#ifdef FEAT_LINEBREAK
1465 // 'showbreak'
Bram Moolenaaree857022019-11-09 23:26:40 +01001466 else if (gvarp == &p_sbr)
Bram Moolenaardac13472019-09-16 21:06:21 +02001467 {
Bram Moolenaaree857022019-11-09 23:26:40 +01001468 for (s = *varp; *s; )
Bram Moolenaardac13472019-09-16 21:06:21 +02001469 {
1470 if (ptr2cells(s) != 1)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001471 errmsg = e_showbreak_contains_unprintable_or_wide_character;
Bram Moolenaardac13472019-09-16 21:06:21 +02001472 MB_PTR_ADV(s);
1473 }
1474 }
1475#endif
1476
1477#ifdef FEAT_GUI
1478 // 'guifont'
1479 else if (varp == &p_guifont)
1480 {
1481 if (gui.in_use)
1482 {
1483 p = p_guifont;
1484# if defined(FEAT_GUI_GTK)
1485 // Put up a font dialog and let the user select a new value.
1486 // If this is cancelled go back to the old value but don't
1487 // give an error message.
1488 if (STRCMP(p, "*") == 0)
1489 {
1490 p = gui_mch_font_dialog(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01001491 free_string_option(p_guifont);
Bram Moolenaardac13472019-09-16 21:06:21 +02001492 p_guifont = (p != NULL) ? p : vim_strsave(oldval);
Bram Moolenaardac13472019-09-16 21:06:21 +02001493 }
1494# endif
1495 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
1496 {
1497# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
1498 if (STRCMP(p_guifont, "*") == 0)
1499 {
1500 // Dialog was cancelled: Keep the old value without giving
1501 // an error message.
zeertzjqf6782732022-07-27 18:26:03 +01001502 free_string_option(p_guifont);
Bram Moolenaardac13472019-09-16 21:06:21 +02001503 p_guifont = vim_strsave(oldval);
Bram Moolenaardac13472019-09-16 21:06:21 +02001504 }
1505 else
1506# endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001507 errmsg = e_invalid_fonts;
Bram Moolenaardac13472019-09-16 21:06:21 +02001508 }
1509 }
1510 redraw_gui_only = TRUE;
1511 }
1512# ifdef FEAT_XFONTSET
1513 else if (varp == &p_guifontset)
1514 {
1515 if (STRCMP(p_guifontset, "*") == 0)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001516 errmsg = e_cant_select_fontset;
Bram Moolenaardac13472019-09-16 21:06:21 +02001517 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001518 errmsg = e_invalid_fontset;
Bram Moolenaardac13472019-09-16 21:06:21 +02001519 redraw_gui_only = TRUE;
1520 }
1521# endif
1522 else if (varp == &p_guifontwide)
1523 {
1524 if (STRCMP(p_guifontwide, "*") == 0)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001525 errmsg = e_cant_select_wide_font;
Bram Moolenaardac13472019-09-16 21:06:21 +02001526 else if (gui_get_wide_font() == FAIL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001527 errmsg = e_invalid_wide_font;
Bram Moolenaardac13472019-09-16 21:06:21 +02001528 redraw_gui_only = TRUE;
1529 }
1530#endif
Dusan Popovic4eeedc02021-10-16 20:52:05 +01001531# if defined(FEAT_GUI_GTK)
1532 else if (varp == &p_guiligatures)
1533 {
1534 gui_set_ligatures();
1535 redraw_gui_only = TRUE;
1536 }
1537# endif
Bram Moolenaardac13472019-09-16 21:06:21 +02001538
1539#ifdef CURSOR_SHAPE
1540 // 'guicursor'
1541 else if (varp == &p_guicursor)
1542 errmsg = parse_shape_opt(SHAPE_CURSOR);
1543#endif
1544
1545#ifdef FEAT_MOUSESHAPE
1546 // 'mouseshape'
1547 else if (varp == &p_mouseshape)
1548 {
1549 errmsg = parse_shape_opt(SHAPE_MOUSE);
1550 update_mouseshape(-1);
1551 }
1552#endif
1553
1554#ifdef FEAT_PRINTER
1555 else if (varp == &p_popt)
1556 errmsg = parse_printoptions();
1557# if defined(FEAT_POSTSCRIPT)
1558 else if (varp == &p_pmfn)
1559 errmsg = parse_printmbfont();
1560# endif
1561#endif
1562
1563#ifdef FEAT_LANGMAP
1564 // 'langmap'
1565 else if (varp == &p_langmap)
1566 langmap_set();
1567#endif
1568
1569#ifdef FEAT_LINEBREAK
1570 // 'breakat'
1571 else if (varp == &p_breakat)
1572 fill_breakat_flags();
1573#endif
1574
Bram Moolenaardac13472019-09-16 21:06:21 +02001575 // 'titlestring' and 'iconstring'
1576 else if (varp == &p_titlestring || varp == &p_iconstring)
1577 {
Bram Moolenaar651fca82021-11-29 20:39:38 +00001578#ifdef FEAT_STL_OPT
Bram Moolenaardac13472019-09-16 21:06:21 +02001579 int flagval = (varp == &p_titlestring) ? STL_IN_TITLE : STL_IN_ICON;
1580
1581 // NULL => statusline syntax
1582 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
1583 stl_syntax |= flagval;
1584 else
1585 stl_syntax &= ~flagval;
Bram Moolenaar651fca82021-11-29 20:39:38 +00001586#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02001587 did_set_title();
1588 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001589
1590#ifdef FEAT_GUI
1591 // 'guioptions'
1592 else if (varp == &p_go)
1593 {
1594 gui_init_which_components(oldval);
1595 redraw_gui_only = TRUE;
1596 }
1597#endif
1598
1599#if defined(FEAT_GUI_TABLINE)
1600 // 'guitablabel'
1601 else if (varp == &p_gtl)
1602 {
1603 redraw_tabline = TRUE;
1604 redraw_gui_only = TRUE;
1605 }
1606 // 'guitabtooltip'
1607 else if (varp == &p_gtt)
1608 {
1609 redraw_gui_only = TRUE;
1610 }
1611#endif
1612
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001613#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +02001614 // 'ttymouse'
1615 else if (varp == &p_ttym)
1616 {
1617 // Switch the mouse off before changing the escape sequences used for
1618 // that.
1619 mch_setmouse(FALSE);
1620 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001621 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001622 else
1623 check_mouse_termcode();
1624 if (termcap_active)
1625 setmouse(); // may switch it on again
1626 }
1627#endif
1628
1629 // 'selection'
1630 else if (varp == &p_sel)
1631 {
1632 if (*p_sel == NUL
1633 || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001634 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001635 }
1636
1637 // 'selectmode'
1638 else if (varp == &p_slm)
1639 {
1640 if (check_opt_strings(p_slm, p_slm_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001641 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001642 }
1643
1644#ifdef FEAT_BROWSE
1645 // 'browsedir'
1646 else if (varp == &p_bsdir)
1647 {
1648 if (check_opt_strings(p_bsdir, p_bsdir_values, FALSE) != OK
1649 && !mch_isdir(p_bsdir))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001650 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001651 }
1652#endif
1653
1654 // 'keymodel'
1655 else if (varp == &p_km)
1656 {
1657 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001658 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001659 else
1660 {
1661 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
1662 km_startsel = (vim_strchr(p_km, 'a') != NULL);
1663 }
1664 }
1665
1666 // 'mousemodel'
1667 else if (varp == &p_mousem)
1668 {
1669 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001670 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001671#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
1672 else if (*p_mousem != *oldval)
1673 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
1674 // to create or delete the popup menus.
1675 gui_motif_update_mousemodel(root_menu);
1676#endif
1677 }
1678
1679 // 'switchbuf'
1680 else if (varp == &p_swb)
1681 {
1682 if (opt_strings_flags(p_swb, p_swb_values, &swb_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001683 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001684 }
1685
1686 // 'debug'
1687 else if (varp == &p_debug)
1688 {
1689 if (check_opt_strings(p_debug, p_debug_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001690 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001691 }
1692
1693 // 'display'
1694 else if (varp == &p_dy)
1695 {
1696 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001697 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001698 else
1699 (void)init_chartab();
1700
1701 }
1702
1703 // 'eadirection'
1704 else if (varp == &p_ead)
1705 {
1706 if (check_opt_strings(p_ead, p_ead_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001707 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001708 }
1709
1710#ifdef FEAT_CLIPBOARD
1711 // 'clipboard'
1712 else if (varp == &p_cb)
1713 errmsg = check_clipboard_option();
1714#endif
1715
1716#ifdef FEAT_SPELL
1717 // When 'spelllang' or 'spellfile' is set and there is a window for this
1718 // buffer in which 'spell' is set load the wordlists.
1719 else if (varp == &(curwin->w_s->b_p_spl)
1720 || varp == &(curwin->w_s->b_p_spf))
1721 {
1722 int is_spellfile = varp == &(curwin->w_s->b_p_spf);
1723
1724 if ((is_spellfile && !valid_spellfile(*varp))
Bram Moolenaarf154f3a2020-06-08 18:54:49 +02001725 || (!is_spellfile && !valid_spelllang(*varp)))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001726 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001727 else
1728 errmsg = did_set_spell_option(is_spellfile);
1729 }
1730 // When 'spellcapcheck' is set compile the regexp program.
1731 else if (varp == &(curwin->w_s->b_p_spc))
1732 {
1733 errmsg = compile_cap_prog(curwin->w_s);
1734 }
Bram Moolenaar362b44b2020-06-10 21:47:00 +02001735 // 'spelloptions'
1736 else if (varp == &(curwin->w_s->b_p_spo))
1737 {
1738 if (**varp != NUL && STRCMP("camel", *varp) != 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001739 errmsg = e_invalid_argument;
Bram Moolenaar362b44b2020-06-10 21:47:00 +02001740 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001741 // 'spellsuggest'
1742 else if (varp == &p_sps)
1743 {
1744 if (spell_check_sps() != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001745 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001746 }
1747 // 'mkspellmem'
1748 else if (varp == &p_msm)
1749 {
1750 if (spell_check_msm() != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001751 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001752 }
1753#endif
1754
1755 // When 'bufhidden' is set, check for valid value.
1756 else if (gvarp == &p_bh)
1757 {
1758 if (check_opt_strings(curbuf->b_p_bh, p_bufhidden_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001759 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001760 }
1761
1762 // When 'buftype' is set, check for valid value.
1763 else if (gvarp == &p_bt)
1764 {
1765 if (check_opt_strings(curbuf->b_p_bt, p_buftype_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001766 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001767 else
1768 {
1769 if (curwin->w_status_height)
1770 {
1771 curwin->w_redr_status = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001772 redraw_later(UPD_VALID);
Bram Moolenaardac13472019-09-16 21:06:21 +02001773 }
1774 curbuf->b_help = (curbuf->b_p_bt[0] == 'h');
Bram Moolenaardac13472019-09-16 21:06:21 +02001775 redraw_titles();
Bram Moolenaardac13472019-09-16 21:06:21 +02001776 }
1777 }
1778
1779#ifdef FEAT_STL_OPT
zeertzjq5dc294a2022-04-15 13:17:57 +01001780 // 'statusline', 'tabline' or 'rulerformat'
1781 else if (gvarp == &p_stl || varp == &p_tal || varp == &p_ruf)
Bram Moolenaardac13472019-09-16 21:06:21 +02001782 {
1783 int wid;
1784
1785 if (varp == &p_ruf) // reset ru_wid first
1786 ru_wid = 0;
1787 s = *varp;
1788 if (varp == &p_ruf && *s == '%')
1789 {
1790 // set ru_wid if 'ruf' starts with "%99("
1791 if (*++s == '-') // ignore a '-'
1792 s++;
1793 wid = getdigits(&s);
1794 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
1795 ru_wid = wid;
1796 else
1797 errmsg = check_stl_option(p_ruf);
1798 }
zeertzjq5dc294a2022-04-15 13:17:57 +01001799 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
Bram Moolenaardac13472019-09-16 21:06:21 +02001800 else if (varp == &p_ruf || s[0] != '%' || s[1] != '!')
1801 errmsg = check_stl_option(s);
1802 if (varp == &p_ruf && errmsg == NULL)
1803 comp_col();
1804 }
1805#endif
1806
1807 // check if it is a valid value for 'complete' -- Acevedo
1808 else if (gvarp == &p_cpt)
1809 {
1810 for (s = *varp; *s;)
1811 {
1812 while (*s == ',' || *s == ' ')
1813 s++;
1814 if (!*s)
1815 break;
1816 if (vim_strchr((char_u *)".wbuksid]tU", *s) == NULL)
1817 {
1818 errmsg = illegal_char(errbuf, *s);
1819 break;
1820 }
1821 if (*++s != NUL && *s != ',' && *s != ' ')
1822 {
1823 if (s[-1] == 'k' || s[-1] == 's')
1824 {
1825 // skip optional filename after 'k' and 's'
1826 while (*s && *s != ',' && *s != ' ')
1827 {
1828 if (*s == '\\' && s[1] != NUL)
1829 ++s;
1830 ++s;
1831 }
1832 }
1833 else
1834 {
1835 if (errbuf != NULL)
1836 {
1837 sprintf((char *)errbuf,
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001838 _(e_illegal_character_after_chr), *--s);
Bram Moolenaardac13472019-09-16 21:06:21 +02001839 errmsg = errbuf;
1840 }
1841 else
1842 errmsg = "";
1843 break;
1844 }
1845 }
1846 }
1847 }
1848
1849 // 'completeopt'
1850 else if (varp == &p_cot)
1851 {
1852 if (check_opt_strings(p_cot, p_cot_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001853 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001854 else
1855 completeopt_was_set();
1856 }
1857
1858#ifdef BACKSLASH_IN_FILENAME
1859 // 'completeslash'
1860 else if (gvarp == &p_csl)
1861 {
1862 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1863 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001864 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001865 }
1866#endif
1867
1868#ifdef FEAT_SIGNS
1869 // 'signcolumn'
1870 else if (varp == &curwin->w_p_scl)
1871 {
1872 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001873 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001874 // When changing the 'signcolumn' to or from 'number', recompute the
1875 // width of the number column if 'number' or 'relativenumber' is set.
1876 if (((*oldval == 'n' && *(oldval + 1) == 'u')
1877 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
1878 && (curwin->w_p_nu || curwin->w_p_rnu))
1879 curwin->w_nrwidth_line_count = 0;
1880 }
1881#endif
1882
1883
1884#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
1885 // 'toolbar'
1886 else if (varp == &p_toolbar)
1887 {
1888 if (opt_strings_flags(p_toolbar, p_toolbar_values,
1889 &toolbar_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001890 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001891 else
1892 {
1893 out_flush();
1894 gui_mch_show_toolbar((toolbar_flags &
1895 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
1896 }
1897 }
1898#endif
1899
1900#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
1901 // 'toolbariconsize': GTK+ 2 only
1902 else if (varp == &p_tbis)
1903 {
1904 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001905 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001906 else
1907 {
1908 out_flush();
1909 gui_mch_show_toolbar((toolbar_flags &
1910 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
1911 }
1912 }
1913#endif
1914
1915 // 'pastetoggle': translate key codes like in a mapping
1916 else if (varp == &p_pt)
1917 {
1918 if (*p_pt)
1919 {
Bram Moolenaar1e7b52a2019-10-13 16:59:08 +02001920 (void)replace_termcodes(p_pt, &p,
1921 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
Bram Moolenaardac13472019-09-16 21:06:21 +02001922 if (p != NULL)
1923 {
zeertzjqf6782732022-07-27 18:26:03 +01001924 free_string_option(p_pt);
Bram Moolenaardac13472019-09-16 21:06:21 +02001925 p_pt = p;
Bram Moolenaardac13472019-09-16 21:06:21 +02001926 }
1927 }
1928 }
1929
1930 // 'backspace'
1931 else if (varp == &p_bs)
1932 {
1933 if (VIM_ISDIGIT(*p_bs))
1934 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02001935 if (*p_bs > '3' || p_bs[1] != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001936 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001937 }
1938 else if (check_opt_strings(p_bs, p_bs_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001939 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001940 }
1941 else if (varp == &p_bo)
1942 {
1943 if (opt_strings_flags(p_bo, p_bo_values, &bo_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001944 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001945 }
1946
1947 // 'tagcase'
1948 else if (gvarp == &p_tc)
1949 {
1950 unsigned int *flags;
1951
1952 if (opt_flags & OPT_LOCAL)
1953 {
1954 p = curbuf->b_p_tc;
1955 flags = &curbuf->b_tc_flags;
1956 }
1957 else
1958 {
1959 p = p_tc;
1960 flags = &tc_flags;
1961 }
1962
1963 if ((opt_flags & OPT_LOCAL) && *p == NUL)
1964 // make the local value empty: use the global value
1965 *flags = 0;
1966 else if (*p == NUL
1967 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001968 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001969 }
1970
1971 // 'casemap'
1972 else if (varp == &p_cmp)
1973 {
1974 if (opt_strings_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001975 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001976 }
1977
1978#ifdef FEAT_DIFF
1979 // 'diffopt'
1980 else if (varp == &p_dip)
1981 {
1982 if (diffopt_changed() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001983 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001984 }
1985#endif
1986
1987#ifdef FEAT_FOLDING
1988 // 'foldmethod'
1989 else if (gvarp == &curwin->w_allbuf_opt.wo_fdm)
1990 {
1991 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK
1992 || *curwin->w_p_fdm == NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001993 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001994 else
1995 {
1996 foldUpdateAll(curwin);
1997 if (foldmethodIsDiff(curwin))
1998 newFoldLevel();
1999 }
2000 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002001 // 'foldmarker'
2002 else if (gvarp == &curwin->w_allbuf_opt.wo_fmr)
2003 {
2004 p = vim_strchr(*varp, ',');
2005 if (p == NULL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002006 errmsg = e_comma_required;
Bram Moolenaardac13472019-09-16 21:06:21 +02002007 else if (p == *varp || p[1] == NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002008 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002009 else if (foldmethodIsMarker(curwin))
2010 foldUpdateAll(curwin);
2011 }
2012 // 'commentstring'
2013 else if (gvarp == &p_cms)
2014 {
2015 if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002016 errmsg = e_commentstring_must_be_empty_or_contain_str;
Bram Moolenaardac13472019-09-16 21:06:21 +02002017 }
2018 // 'foldopen'
2019 else if (varp == &p_fdo)
2020 {
2021 if (opt_strings_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002022 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002023 }
2024 // 'foldclose'
2025 else if (varp == &p_fcl)
2026 {
2027 if (check_opt_strings(p_fcl, p_fcl_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002028 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002029 }
2030 // 'foldignore'
2031 else if (gvarp == &curwin->w_allbuf_opt.wo_fdi)
2032 {
2033 if (foldmethodIsIndent(curwin))
2034 foldUpdateAll(curwin);
2035 }
2036#endif
2037
2038 // 'virtualedit'
Gary Johnson53ba05b2021-07-26 22:19:10 +02002039 else if (gvarp == &p_ve)
Bram Moolenaardac13472019-09-16 21:06:21 +02002040 {
Gary Johnson53ba05b2021-07-26 22:19:10 +02002041 char_u *ve = p_ve;
2042 unsigned int *flags = &ve_flags;
2043
2044 if (opt_flags & OPT_LOCAL)
Bram Moolenaardac13472019-09-16 21:06:21 +02002045 {
Gary Johnson51ad8502021-08-03 18:33:08 +02002046 ve = curwin->w_p_ve;
2047 flags = &curwin->w_ve_flags;
Gary Johnson53ba05b2021-07-26 22:19:10 +02002048 }
2049
2050 if ((opt_flags & OPT_LOCAL) && *ve == NUL)
2051 // make the local value empty: use the global value
2052 *flags = 0;
2053 else
2054 {
2055 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002056 errmsg = e_invalid_argument;
Gary Johnson53ba05b2021-07-26 22:19:10 +02002057 else if (STRCMP(p_ve, oldval) != 0)
2058 {
2059 // Recompute cursor position in case the new 've' setting
2060 // changes something.
2061 validate_virtcol();
2062 coladvance(curwin->w_virtcol);
2063 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002064 }
2065 }
2066
2067#if defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)
2068 else if (varp == &p_csqf)
2069 {
2070 if (p_csqf != NULL)
2071 {
2072 p = p_csqf;
2073 while (*p != NUL)
2074 {
2075 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
2076 || p[1] == NUL
2077 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
2078 || (p[2] != NUL && p[2] != ','))
2079 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002080 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002081 break;
2082 }
2083 else if (p[2] == NUL)
2084 break;
2085 else
2086 p += 3;
2087 }
2088 }
2089 }
2090#endif
2091
Bram Moolenaardac13472019-09-16 21:06:21 +02002092 // 'cinoptions'
2093 else if (gvarp == &p_cino)
2094 {
2095 // TODO: recognize errors
2096 parse_cino(curbuf);
2097 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002098
2099#if defined(FEAT_RENDER_OPTIONS)
2100 // 'renderoptions'
2101 else if (varp == &p_rop)
2102 {
2103 if (!gui_mch_set_rendering_options(p_rop))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002104 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002105 }
2106#endif
2107
2108 else if (gvarp == &p_ft)
2109 {
2110 if (!valid_filetype(*varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002111 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002112 else
2113 {
2114 value_changed = STRCMP(oldval, *varp) != 0;
2115
2116 // Since we check the value, there is no need to set P_INSECURE,
2117 // even when the value comes from a modeline.
2118 *value_checked = TRUE;
2119 }
2120 }
2121
2122#ifdef FEAT_SYN_HL
2123 else if (gvarp == &p_syn)
2124 {
2125 if (!valid_filetype(*varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002126 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002127 else
2128 {
2129 value_changed = STRCMP(oldval, *varp) != 0;
2130
2131 // Since we check the value, there is no need to set P_INSECURE,
2132 // even when the value comes from a modeline.
2133 *value_checked = TRUE;
2134 }
2135 }
2136#endif
2137
2138#ifdef FEAT_TERMINAL
2139 // 'termwinkey'
2140 else if (varp == &curwin->w_p_twk)
2141 {
2142 if (*curwin->w_p_twk != NUL
2143 && string_to_key(curwin->w_p_twk, TRUE) == 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002144 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002145 }
2146 // 'termwinsize'
2147 else if (varp == &curwin->w_p_tws)
2148 {
2149 if (*curwin->w_p_tws != NUL)
2150 {
2151 p = skipdigits(curwin->w_p_tws);
2152 if (p == curwin->w_p_tws
2153 || (*p != 'x' && *p != '*')
2154 || *skipdigits(p + 1) != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002155 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002156 }
2157 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002158 // 'wincolor'
2159 else if (varp == &curwin->w_p_wcr)
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002160 term_update_wincolor(curwin);
Bram Moolenaardac13472019-09-16 21:06:21 +02002161# if defined(MSWIN)
2162 // 'termwintype'
2163 else if (varp == &p_twt)
2164 {
2165 if (check_opt_strings(*varp, p_twt_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002166 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002167 }
2168# endif
2169#endif
2170
2171#ifdef FEAT_VARTABS
2172 // 'varsofttabstop'
2173 else if (varp == &(curbuf->b_p_vsts))
2174 {
2175 char_u *cp;
2176
2177 if (!(*varp)[0] || ((*varp)[0] == '0' && !(*varp)[1]))
2178 {
2179 if (curbuf->b_p_vsts_array)
2180 {
2181 vim_free(curbuf->b_p_vsts_array);
2182 curbuf->b_p_vsts_array = 0;
2183 }
2184 }
2185 else
2186 {
2187 for (cp = *varp; *cp; ++cp)
2188 {
2189 if (vim_isdigit(*cp))
2190 continue;
2191 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
2192 continue;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002193 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002194 break;
2195 }
2196 if (errmsg == NULL)
2197 {
2198 int *oldarray = curbuf->b_p_vsts_array;
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002199 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Bram Moolenaardac13472019-09-16 21:06:21 +02002200 {
2201 if (oldarray)
2202 vim_free(oldarray);
2203 }
2204 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002205 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002206 }
2207 }
2208 }
2209
2210 // 'vartabstop'
2211 else if (varp == &(curbuf->b_p_vts))
2212 {
2213 char_u *cp;
2214
2215 if (!(*varp)[0] || ((*varp)[0] == '0' && !(*varp)[1]))
2216 {
2217 if (curbuf->b_p_vts_array)
2218 {
2219 vim_free(curbuf->b_p_vts_array);
2220 curbuf->b_p_vts_array = NULL;
2221 }
2222 }
2223 else
2224 {
2225 for (cp = *varp; *cp; ++cp)
2226 {
2227 if (vim_isdigit(*cp))
2228 continue;
2229 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
2230 continue;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002231 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002232 break;
2233 }
2234 if (errmsg == NULL)
2235 {
2236 int *oldarray = curbuf->b_p_vts_array;
2237
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002238 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Bram Moolenaardac13472019-09-16 21:06:21 +02002239 {
2240 vim_free(oldarray);
2241#ifdef FEAT_FOLDING
2242 if (foldmethodIsIndent(curwin))
2243 foldUpdateAll(curwin);
2244#endif
2245 }
2246 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002247 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002248 }
2249 }
2250 }
2251#endif
2252
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002253#ifdef FEAT_PROP_POPUP
Bram Moolenaardac13472019-09-16 21:06:21 +02002254 // 'previewpopup'
2255 else if (varp == &p_pvp)
2256 {
2257 if (parse_previewpopup(NULL) == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002258 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002259 }
2260# ifdef FEAT_QUICKFIX
2261 // 'completepopup'
2262 else if (varp == &p_cpp)
2263 {
2264 if (parse_completepopup(NULL) == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002265 errmsg = e_invalid_argument;
Bram Moolenaar447bfba2020-07-18 16:07:16 +02002266 else
2267 popup_close_info();
Bram Moolenaardac13472019-09-16 21:06:21 +02002268 }
2269# endif
2270#endif
2271
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002272#ifdef FEAT_EVAL
2273 // '*expr' options
2274 else if (
2275# ifdef FEAT_BEVAL
2276 varp == &p_bexpr ||
2277# endif
2278# ifdef FEAT_DIFF
2279 varp == &p_dex ||
2280# endif
2281# ifdef FEAT_FOLDING
2282 varp == &curwin->w_p_fde ||
Yegappan Lakshmanan27708e62021-12-26 21:54:43 +00002283 varp == &curwin->w_p_fdt ||
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002284# endif
2285 gvarp == &p_fex ||
2286# ifdef FEAT_FIND_ID
2287 gvarp == &p_inex ||
2288# endif
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002289 gvarp == &p_inde ||
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002290# ifdef FEAT_DIFF
2291 varp == &p_pex ||
2292# endif
2293# ifdef FEAT_POSTSCRIPT
2294 varp == &p_pexpr ||
2295# endif
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00002296 varp == &p_ccv)
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002297 {
2298 char_u **p_opt = NULL;
2299 char_u *name;
2300
2301 // If the option value starts with <SID> or s:, then replace that with
2302 // the script identifier.
2303# ifdef FEAT_BEVAL
2304 if (varp == &p_bexpr) // 'balloonexpr'
2305 p_opt = (opt_flags & OPT_LOCAL) ? &curbuf->b_p_bexpr : &p_bexpr;
2306# endif
2307# ifdef FEAT_DIFF
2308 if (varp == &p_dex) // 'diffexpr'
2309 p_opt = &p_dex;
2310# endif
2311# ifdef FEAT_FOLDING
Yegappan Lakshmanan27708e62021-12-26 21:54:43 +00002312 if (varp == &curwin->w_p_fde) // 'foldexpr'
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002313 p_opt = &curwin->w_p_fde;
Yegappan Lakshmanan27708e62021-12-26 21:54:43 +00002314 if (varp == &curwin->w_p_fdt) // 'foldtext'
2315 p_opt = &curwin->w_p_fdt;
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002316# endif
2317 if (gvarp == &p_fex) // 'formatexpr'
2318 p_opt = &curbuf->b_p_fex;
2319# ifdef FEAT_FIND_ID
2320 if (gvarp == &p_inex) // 'includeexpr'
2321 p_opt = &curbuf->b_p_inex;
2322# endif
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002323 if (gvarp == &p_inde) // 'indentexpr'
2324 p_opt = &curbuf->b_p_inde;
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002325# ifdef FEAT_DIFF
2326 if (varp == &p_pex) // 'patchexpr'
2327 p_opt = &p_pex;
2328# endif
2329# ifdef FEAT_POSTSCRIPT
2330 if (varp == &p_pexpr) // 'printexpr'
2331 p_opt = &p_pexpr;
2332# endif
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00002333 if (varp == &p_ccv) // 'charconvert'
2334 p_opt = &p_ccv;
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002335
2336 if (p_opt != NULL)
2337 {
2338 name = get_scriptlocal_funcname(*p_opt);
2339 if (name != NULL)
2340 {
zeertzjqf6782732022-07-27 18:26:03 +01002341 free_string_option(*p_opt);
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002342 *p_opt = name;
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002343 }
2344 }
2345
2346# ifdef FEAT_FOLDING
2347 if (varp == &curwin->w_p_fde && foldmethodIsExpr(curwin))
2348 foldUpdateAll(curwin);
2349# endif
2350 }
2351#endif
2352
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002353#ifdef FEAT_COMPL_FUNC
2354 // 'completefunc'
2355 else if (gvarp == &p_cfu)
2356 {
2357 if (set_completefunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002358 errmsg = e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002359 }
2360
2361 // 'omnifunc'
2362 else if (gvarp == &p_ofu)
2363 {
2364 if (set_omnifunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002365 errmsg = e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002366 }
2367
2368 // 'thesaurusfunc'
2369 else if (gvarp == &p_tsrfu)
2370 {
2371 if (set_thesaurusfunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002372 errmsg = e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002373 }
2374#endif
2375
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00002376#if defined(FEAT_EVAL) && \
2377 (defined(FEAT_XIM) || defined(IME_WITHOUT_XIM) || defined(VIMDLL))
2378 // 'imactivatefunc'
2379 else if (gvarp == &p_imaf)
2380 {
2381 if (set_imactivatefunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002382 errmsg = e_invalid_argument;
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00002383 }
2384
2385 // 'imstatusfunc'
2386 else if (gvarp == &p_imsf)
2387 {
2388 if (set_imstatusfunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002389 errmsg = e_invalid_argument;
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00002390 }
2391#endif
2392
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00002393 // 'operatorfunc'
2394 else if (varp == &p_opfunc)
2395 {
2396 if (set_operatorfunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002397 errmsg = e_invalid_argument;
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00002398 }
2399
Bram Moolenaard43906d2020-07-20 21:31:32 +02002400#ifdef FEAT_QUICKFIX
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00002401 // 'quickfixtextfunc'
Bram Moolenaard43906d2020-07-20 21:31:32 +02002402 else if (varp == &p_qftf)
2403 {
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00002404 if (qf_process_qftf_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002405 errmsg = e_invalid_argument;
Bram Moolenaard43906d2020-07-20 21:31:32 +02002406 }
2407#endif
2408
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00002409#ifdef FEAT_EVAL
2410 // 'tagfunc'
2411 else if (gvarp == &p_tfu)
2412 {
2413 if (set_tagfunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002414 errmsg = e_invalid_argument;
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00002415 }
2416#endif
2417
Bram Moolenaardac13472019-09-16 21:06:21 +02002418 // Options that are a list of flags.
2419 else
2420 {
2421 p = NULL;
2422 if (varp == &p_ww) // 'whichwrap'
2423 p = (char_u *)WW_ALL;
2424 if (varp == &p_shm) // 'shortmess'
2425 p = (char_u *)SHM_ALL;
2426 else if (varp == &(p_cpo)) // 'cpoptions'
2427 p = (char_u *)CPO_ALL;
2428 else if (varp == &(curbuf->b_p_fo)) // 'formatoptions'
2429 p = (char_u *)FO_ALL;
2430#ifdef FEAT_CONCEAL
2431 else if (varp == &curwin->w_p_cocu) // 'concealcursor'
2432 p = (char_u *)COCU_ALL;
2433#endif
2434 else if (varp == &p_mouse) // 'mouse'
2435 {
Bram Moolenaardac13472019-09-16 21:06:21 +02002436 p = (char_u *)MOUSE_ALL;
Bram Moolenaardac13472019-09-16 21:06:21 +02002437 }
2438#if defined(FEAT_GUI)
2439 else if (varp == &p_go) // 'guioptions'
2440 p = (char_u *)GO_ALL;
2441#endif
2442 if (p != NULL)
2443 {
2444 for (s = *varp; *s; ++s)
2445 if (vim_strchr(p, *s) == NULL)
2446 {
2447 errmsg = illegal_char(errbuf, *s);
2448 break;
2449 }
2450 }
2451 }
2452
2453 // If error detected, restore the previous value.
2454 if (errmsg != NULL)
2455 {
zeertzjqf6782732022-07-27 18:26:03 +01002456 free_string_option(*varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02002457 *varp = oldval;
2458 // When resetting some values, need to act on it.
2459 if (did_chartab)
2460 (void)init_chartab();
2461 if (varp == &p_hl)
2462 (void)highlight_changed();
2463 }
2464 else
2465 {
2466#ifdef FEAT_EVAL
2467 // Remember where the option was set.
2468 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
2469#endif
2470 // Free string options that are in allocated memory.
2471 // Use "free_oldval", because recursiveness may change the flags under
2472 // our fingers (esp. init_highlight()).
2473 if (free_oldval)
2474 free_string_option(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01002475 set_option_flag(opt_idx, P_ALLOCED);
Bram Moolenaardac13472019-09-16 21:06:21 +02002476
2477 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
2478 && is_global_local_option(opt_idx))
2479 {
2480 // global option with local value set to use global value; free
2481 // the local value and make it empty
2482 p = get_option_varp_scope(opt_idx, OPT_LOCAL);
2483 free_string_option(*(char_u **)p);
2484 *(char_u **)p = empty_option;
2485 }
2486
2487 // May set global value for local option.
2488 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
2489 set_string_option_global(opt_idx, varp);
2490
2491 // Trigger the autocommand only after setting the flags.
2492#ifdef FEAT_SYN_HL
2493 // When 'syntax' is set, load the syntax of that name
2494 if (varp == &(curbuf->b_p_syn))
2495 {
2496 static int syn_recursive = 0;
2497
2498 ++syn_recursive;
2499 // Only pass TRUE for "force" when the value changed or not used
2500 // recursively, to avoid endless recurrence.
2501 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
2502 value_changed || syn_recursive == 1, curbuf);
2503 curbuf->b_flags |= BF_SYN_SET;
2504 --syn_recursive;
2505 }
2506#endif
2507 else if (varp == &(curbuf->b_p_ft))
2508 {
2509 // 'filetype' is set, trigger the FileType autocommand.
2510 // Skip this when called from a modeline and the filetype was
2511 // already set to this value.
2512 if (!(opt_flags & OPT_MODELINE) || value_changed)
2513 {
2514 static int ft_recursive = 0;
2515 int secure_save = secure;
2516
2517 // Reset the secure flag, since the value of 'filetype' has
2518 // been checked to be safe.
2519 secure = 0;
2520
2521 ++ft_recursive;
2522 did_filetype = TRUE;
2523 // Only pass TRUE for "force" when the value changed or not
2524 // used recursively, to avoid endless recurrence.
2525 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2526 value_changed || ft_recursive == 1, curbuf);
2527 --ft_recursive;
2528 // Just in case the old "curbuf" is now invalid.
2529 if (varp != &(curbuf->b_p_ft))
2530 varp = NULL;
2531
2532 secure = secure_save;
2533 }
2534 }
2535#ifdef FEAT_SPELL
2536 if (varp == &(curwin->w_s->b_p_spl))
2537 {
2538 char_u fname[200];
2539 char_u *q = curwin->w_s->b_p_spl;
2540
2541 // Skip the first name if it is "cjk".
2542 if (STRNCMP(q, "cjk,", 4) == 0)
2543 q += 4;
2544
2545 // Source the spell/LANG.vim in 'runtimepath'.
2546 // They could set 'spellcapcheck' depending on the language.
2547 // Use the first name in 'spelllang' up to '_region' or
2548 // '.encoding'.
2549 for (p = q; *p != NUL; ++p)
2550 if (!ASCII_ISALNUM(*p) && *p != '-')
2551 break;
2552 if (p > q)
2553 {
2554 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
2555 (int)(p - q), q);
2556 source_runtime(fname, DIP_ALL);
2557 }
2558 }
2559#endif
2560 }
2561
Bram Moolenaardac13472019-09-16 21:06:21 +02002562 if (varp == &p_mouse)
2563 {
Bram Moolenaardac13472019-09-16 21:06:21 +02002564 if (*p_mouse == NUL)
2565 mch_setmouse(FALSE); // switch mouse off
2566 else
Bram Moolenaardac13472019-09-16 21:06:21 +02002567 setmouse(); // in case 'mouse' changed
2568 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002569
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002570#if defined(FEAT_LUA) || defined(PROTO)
2571 if (varp == &p_rtp)
2572 update_package_paths_in_lua();
2573#endif
2574
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00002575#if defined(FEAT_LINEBREAK)
2576 // Changing Formatlistpattern when briopt includes the list setting:
2577 // redraw
2578 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
2579 && curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002580 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00002581#endif
2582
Bram Moolenaardac13472019-09-16 21:06:21 +02002583 if (curwin->w_curswant != MAXCOL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002584 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02002585 curwin->w_set_curswant = TRUE;
2586
Bram Moolenaar37294bd2021-03-10 13:40:08 +01002587 if ((opt_flags & OPT_NO_REDRAW) == 0)
2588 {
Bram Moolenaardac13472019-09-16 21:06:21 +02002589#ifdef FEAT_GUI
Bram Moolenaar37294bd2021-03-10 13:40:08 +01002590 // check redraw when it's not a GUI option or the GUI is active.
2591 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02002592#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01002593 check_redraw(get_option_flags(opt_idx));
2594 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002595
2596#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2597 if (did_swaptcap)
2598 {
2599 set_termname((char_u *)"win32");
2600 init_highlight(TRUE, FALSE);
2601 }
2602#endif
2603
2604 return errmsg;
2605}
2606
2607/*
2608 * Check an option that can be a range of string values.
2609 *
2610 * Return OK for correct value, FAIL otherwise.
2611 * Empty is always OK.
2612 */
2613 static int
2614check_opt_strings(
2615 char_u *val,
2616 char **values,
2617 int list) // when TRUE: accept a list of values
2618{
2619 return opt_strings_flags(val, values, NULL, list);
2620}
2621
2622/*
2623 * Handle an option that can be a range of string values.
2624 * Set a flag in "*flagp" for each string present.
2625 *
2626 * Return OK for correct value, FAIL otherwise.
2627 * Empty is always OK.
2628 */
2629 static int
2630opt_strings_flags(
2631 char_u *val, // new value
2632 char **values, // array of valid string values
2633 unsigned *flagp,
2634 int list) // when TRUE: accept a list of values
2635{
2636 int i;
2637 int len;
2638 unsigned new_flags = 0;
2639
2640 while (*val)
2641 {
2642 for (i = 0; ; ++i)
2643 {
2644 if (values[i] == NULL) // val not found in values[]
2645 return FAIL;
2646
2647 len = (int)STRLEN(values[i]);
2648 if (STRNCMP(values[i], val, len) == 0
2649 && ((list && val[len] == ',') || val[len] == NUL))
2650 {
2651 val += len + (val[len] == ',');
2652 new_flags |= (1 << i);
2653 break; // check next item in val list
2654 }
2655 }
2656 }
2657 if (flagp != NULL)
2658 *flagp = new_flags;
2659
2660 return OK;
2661}
2662
2663/*
2664 * return OK if "p" is a valid fileformat name, FAIL otherwise.
2665 */
2666 int
2667check_ff_value(char_u *p)
2668{
2669 return check_opt_strings(p, p_ff_values, FALSE);
2670}