blob: 26dd55120b45e25b2a0b718b9287b8c66150609e [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
128}
129
130#if defined(FEAT_EVAL)
131/*
132 * Trigger the OptionSet autocommand.
133 * "opt_idx" is the index of the option being set.
134 * "opt_flags" can be OPT_LOCAL etc.
135 * "oldval" the old value
136 * "oldval_l" the old local value (only non-NULL if global and local value
137 * are set)
138 * "oldval_g" the old global value (only non-NULL if global and local value
139 * are set)
140 * "newval" the new value
141 */
142 void
143trigger_optionsset_string(
144 int opt_idx,
145 int opt_flags,
146 char_u *oldval,
147 char_u *oldval_l,
148 char_u *oldval_g,
149 char_u *newval)
150{
151 // Don't do this recursively.
152 if (oldval != NULL && newval != NULL
153 && *get_vim_var_str(VV_OPTION_TYPE) == NUL)
154 {
155 char_u buf_type[7];
156
157 sprintf((char *)buf_type, "%s",
158 (opt_flags & OPT_LOCAL) ? "local" : "global");
159 set_vim_var_string(VV_OPTION_OLD, oldval, -1);
160 set_vim_var_string(VV_OPTION_NEW, newval, -1);
161 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
162 if (opt_flags & OPT_LOCAL)
163 {
164 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
165 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
166 }
167 if (opt_flags & OPT_GLOBAL)
168 {
169 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
170 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval, -1);
171 }
172 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
173 {
174 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
175 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval_l, -1);
176 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval_g, -1);
177 }
178 if (opt_flags & OPT_MODELINE)
179 {
180 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
181 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
182 }
183 apply_autocmds(EVENT_OPTIONSET,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000184 get_option_fullname(opt_idx), NULL, FALSE,
Bram Moolenaardac13472019-09-16 21:06:21 +0200185 NULL);
186 reset_v_option_vars();
187 }
188}
189#endif
190
191 static char *
192illegal_char(char *errbuf, int c)
193{
194 if (errbuf == NULL)
195 return "";
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000196 sprintf((char *)errbuf, _(e_illegal_character_str), (char *)transchar(c));
Bram Moolenaardac13472019-09-16 21:06:21 +0200197 return errbuf;
198}
199
200/*
201 * Check string options in a buffer for NULL value.
202 */
203 void
204check_buf_options(buf_T *buf)
205{
206 check_string_option(&buf->b_p_bh);
207 check_string_option(&buf->b_p_bt);
208 check_string_option(&buf->b_p_fenc);
209 check_string_option(&buf->b_p_ff);
210#ifdef FEAT_FIND_ID
211 check_string_option(&buf->b_p_def);
212 check_string_option(&buf->b_p_inc);
213# ifdef FEAT_EVAL
214 check_string_option(&buf->b_p_inex);
215# endif
216#endif
217#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
218 check_string_option(&buf->b_p_inde);
219 check_string_option(&buf->b_p_indk);
220#endif
221#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
222 check_string_option(&buf->b_p_bexpr);
223#endif
224#if defined(FEAT_CRYPT)
225 check_string_option(&buf->b_p_cm);
226#endif
227 check_string_option(&buf->b_p_fp);
228#if defined(FEAT_EVAL)
229 check_string_option(&buf->b_p_fex);
230#endif
231#ifdef FEAT_CRYPT
232 check_string_option(&buf->b_p_key);
233#endif
234 check_string_option(&buf->b_p_kp);
235 check_string_option(&buf->b_p_mps);
236 check_string_option(&buf->b_p_fo);
237 check_string_option(&buf->b_p_flp);
238 check_string_option(&buf->b_p_isk);
Bram Moolenaardac13472019-09-16 21:06:21 +0200239 check_string_option(&buf->b_p_com);
Bram Moolenaardac13472019-09-16 21:06:21 +0200240#ifdef FEAT_FOLDING
241 check_string_option(&buf->b_p_cms);
242#endif
243 check_string_option(&buf->b_p_nf);
244#ifdef FEAT_TEXTOBJ
245 check_string_option(&buf->b_p_qe);
246#endif
247#ifdef FEAT_SYN_HL
248 check_string_option(&buf->b_p_syn);
249 check_string_option(&buf->b_s.b_syn_isk);
250#endif
251#ifdef FEAT_SPELL
252 check_string_option(&buf->b_s.b_p_spc);
253 check_string_option(&buf->b_s.b_p_spf);
254 check_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +0200255 check_string_option(&buf->b_s.b_p_spo);
Bram Moolenaardac13472019-09-16 21:06:21 +0200256#endif
257#ifdef FEAT_SEARCHPATH
258 check_string_option(&buf->b_p_sua);
259#endif
260#ifdef FEAT_CINDENT
261 check_string_option(&buf->b_p_cink);
262 check_string_option(&buf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +0100263 check_string_option(&buf->b_p_cinsd);
Bram Moolenaardac13472019-09-16 21:06:21 +0200264 parse_cino(buf);
265#endif
266 check_string_option(&buf->b_p_ft);
267#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
268 check_string_option(&buf->b_p_cinw);
269#endif
270 check_string_option(&buf->b_p_cpt);
271#ifdef FEAT_COMPL_FUNC
272 check_string_option(&buf->b_p_cfu);
273 check_string_option(&buf->b_p_ofu);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +0100274 check_string_option(&buf->b_p_tsrfu);
Bram Moolenaardac13472019-09-16 21:06:21 +0200275#endif
276#ifdef FEAT_EVAL
277 check_string_option(&buf->b_p_tfu);
278#endif
279#ifdef FEAT_KEYMAP
280 check_string_option(&buf->b_p_keymap);
281#endif
282#ifdef FEAT_QUICKFIX
283 check_string_option(&buf->b_p_gp);
284 check_string_option(&buf->b_p_mp);
285 check_string_option(&buf->b_p_efm);
286#endif
287 check_string_option(&buf->b_p_ep);
288 check_string_option(&buf->b_p_path);
289 check_string_option(&buf->b_p_tags);
290 check_string_option(&buf->b_p_tc);
291 check_string_option(&buf->b_p_dict);
292 check_string_option(&buf->b_p_tsr);
293#ifdef FEAT_LISP
294 check_string_option(&buf->b_p_lw);
295#endif
296 check_string_option(&buf->b_p_bkc);
297 check_string_option(&buf->b_p_menc);
298#ifdef FEAT_VARTABS
299 check_string_option(&buf->b_p_vsts);
300 check_string_option(&buf->b_p_vts);
301#endif
302}
303
304/*
305 * Free the string allocated for an option.
306 * Checks for the string being empty_option. This may happen if we're out of
307 * memory, vim_strsave() returned NULL, which was replaced by empty_option by
308 * check_options().
309 * Does NOT check for P_ALLOCED flag!
310 */
311 void
312free_string_option(char_u *p)
313{
314 if (p != empty_option)
315 vim_free(p);
316}
317
318 void
319clear_string_option(char_u **pp)
320{
321 if (*pp != empty_option)
322 vim_free(*pp);
323 *pp = empty_option;
324}
325
326 void
327check_string_option(char_u **pp)
328{
329 if (*pp == NULL)
330 *pp = empty_option;
331}
332
333/*
334 * Set global value for string option when it's a local option.
335 */
336 static void
337set_string_option_global(
338 int opt_idx, // option index
339 char_u **varp) // pointer to option variable
340{
341 char_u **p, *s;
342
343 // the global value is always allocated
344 if (is_window_local_option(opt_idx))
345 p = (char_u **)GLOBAL_WO(varp);
346 else
347 p = (char_u **)get_option_var(opt_idx);
348 if (!is_global_option(opt_idx)
349 && p != varp
350 && (s = vim_strsave(*varp)) != NULL)
351 {
352 free_string_option(*p);
353 *p = s;
354 }
355}
356
357/*
358 * Set a string option to a new value (without checking the effect).
359 * The string is copied into allocated memory.
360 * if ("opt_idx" == -1) "name" is used, otherwise "opt_idx" is used.
361 * When "set_sid" is zero set the scriptID to current_sctx.sc_sid. When
362 * "set_sid" is SID_NONE don't set the scriptID. Otherwise set the scriptID to
363 * "set_sid".
364 */
365 void
366set_string_option_direct(
367 char_u *name,
368 int opt_idx,
369 char_u *val,
370 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
371 int set_sid UNUSED)
372{
373 char_u *s;
374 char_u **varp;
375 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
376 int idx = opt_idx;
377
378 if (idx == -1) // use name
379 {
380 idx = findoption(name);
381 if (idx < 0) // not found (should not happen)
382 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000383 semsg(_(e_internal_error_str), "set_string_option_direct()");
Bram Moolenaardac13472019-09-16 21:06:21 +0200384 siemsg(_("For option %s"), name);
385 return;
386 }
387 }
388
389 if (is_hidden_option(idx)) // can't set hidden option
390 return;
391
392 s = vim_strsave(val);
393 if (s != NULL)
394 {
395 varp = (char_u **)get_option_varp_scope(idx,
396 both ? OPT_LOCAL : opt_flags);
397 if ((opt_flags & OPT_FREE) && (get_option_flags(idx) & P_ALLOCED))
398 free_string_option(*varp);
399 *varp = s;
400
401 // For buffer/window local option may also set the global value.
402 if (both)
403 set_string_option_global(idx, varp);
404
405 set_option_flag(idx, P_ALLOCED);
406
407 // When setting both values of a global option with a local value,
408 // make the local value empty, so that the global value is used.
409 if (is_global_local_option(idx) && both)
410 {
411 free_string_option(*varp);
412 *varp = empty_option;
413 }
414# ifdef FEAT_EVAL
415 if (set_sid != SID_NONE)
416 {
417 sctx_T script_ctx;
418
419 if (set_sid == 0)
420 script_ctx = current_sctx;
421 else
422 {
423 script_ctx.sc_sid = set_sid;
424 script_ctx.sc_seq = 0;
425 script_ctx.sc_lnum = 0;
426 script_ctx.sc_version = 1;
427 }
428 set_option_sctx_idx(idx, opt_flags, script_ctx);
429 }
430# endif
431 }
432}
433
434/*
435 * Like set_string_option_direct(), but for a window-local option in "wp".
436 * Blocks autocommands to avoid the old curwin becoming invalid.
437 */
438 void
439set_string_option_direct_in_win(
440 win_T *wp,
441 char_u *name,
442 int opt_idx,
443 char_u *val,
444 int opt_flags,
445 int set_sid)
446{
447 win_T *save_curwin = curwin;
448
449 block_autocmds();
450 curwin = wp;
451 curbuf = curwin->w_buffer;
452 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
453 curwin = save_curwin;
454 curbuf = curwin->w_buffer;
455 unblock_autocmds();
456}
457
Dominique Pelle748b3082022-01-08 12:41:16 +0000458#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200459/*
460 * Like set_string_option_direct(), but for a buffer-local option in "buf".
461 * Blocks autocommands to avoid the old curbuf becoming invalid.
462 */
463 void
464set_string_option_direct_in_buf(
465 buf_T *buf,
466 char_u *name,
467 int opt_idx,
468 char_u *val,
469 int opt_flags,
470 int set_sid)
471{
472 buf_T *save_curbuf = curbuf;
473
474 block_autocmds();
475 curbuf = buf;
476 curwin->w_buffer = curbuf;
477 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
478 curbuf = save_curbuf;
479 curwin->w_buffer = curbuf;
480 unblock_autocmds();
481}
Dominique Pelle748b3082022-01-08 12:41:16 +0000482#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200483
484/*
485 * Set a string option to a new value, and handle the effects.
486 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100487 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaardac13472019-09-16 21:06:21 +0200488 */
489 char *
490set_string_option(
491 int opt_idx,
492 char_u *value,
493 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
494{
495 char_u *s;
496 char_u **varp;
497 char_u *oldval;
498#if defined(FEAT_EVAL)
499 char_u *oldval_l = NULL;
500 char_u *oldval_g = NULL;
501 char_u *saved_oldval = NULL;
502 char_u *saved_oldval_l = NULL;
503 char_u *saved_oldval_g = NULL;
504 char_u *saved_newval = NULL;
505#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100506 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +0200507 int value_checked = FALSE;
508
509 if (is_hidden_option(opt_idx)) // don't set hidden option
510 return NULL;
511
Bram Moolenaar7f009df2020-03-16 20:27:38 +0100512 s = vim_strsave(value == NULL ? (char_u *)"" : value);
Bram Moolenaardac13472019-09-16 21:06:21 +0200513 if (s != NULL)
514 {
515 varp = (char_u **)get_option_varp_scope(opt_idx,
516 (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
517 ? (is_global_local_option(opt_idx)
518 ? OPT_GLOBAL : OPT_LOCAL)
519 : opt_flags);
520 oldval = *varp;
521#if defined(FEAT_EVAL)
522 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
523 {
524 oldval_l = *(char_u **)get_option_varp_scope(opt_idx, OPT_LOCAL);
525 oldval_g = *(char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
526 }
527#endif
528 *varp = s;
529
530#if defined(FEAT_EVAL)
531 if (!starting
532# ifdef FEAT_CRYPT
533 && !is_crypt_key_option(opt_idx)
534# endif
535 )
536 {
537 if (oldval_l != NULL)
538 saved_oldval_l = vim_strsave(oldval_l);
539 if (oldval_g != NULL)
540 saved_oldval_g = vim_strsave(oldval_g);
541 saved_oldval = vim_strsave(oldval);
542 saved_newval = vim_strsave(s);
543 }
544#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100545 if ((errmsg = did_set_string_option(opt_idx, varp, TRUE, oldval, NULL,
Bram Moolenaardac13472019-09-16 21:06:21 +0200546 opt_flags, &value_checked)) == NULL)
547 did_set_option(opt_idx, opt_flags, TRUE, value_checked);
548
549#if defined(FEAT_EVAL)
550 // call autocommand after handling side effects
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100551 if (errmsg == NULL)
Bram Moolenaardac13472019-09-16 21:06:21 +0200552 trigger_optionsset_string(opt_idx, opt_flags,
553 saved_oldval, saved_oldval_l,
554 saved_oldval_g, saved_newval);
555 vim_free(saved_oldval);
556 vim_free(saved_oldval_l);
557 vim_free(saved_oldval_g);
558 vim_free(saved_newval);
559#endif
560 }
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100561 return errmsg;
Bram Moolenaardac13472019-09-16 21:06:21 +0200562}
563
564/*
565 * Return TRUE if "val" is a valid 'filetype' name.
566 * Also used for 'syntax' and 'keymap'.
567 */
568 static int
569valid_filetype(char_u *val)
570{
571 return valid_name(val, ".-_");
572}
573
574#ifdef FEAT_STL_OPT
575/*
576 * Check validity of options with the 'statusline' format.
zeertzjq5dc294a2022-04-15 13:17:57 +0100577 * Return an untranslated error message or NULL.
Bram Moolenaardac13472019-09-16 21:06:21 +0200578 */
579 static char *
580check_stl_option(char_u *s)
581{
Bram Moolenaardac13472019-09-16 21:06:21 +0200582 int groupdepth = 0;
583 static char errbuf[80];
584
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100585 while (*s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200586 {
587 // Check for valid keys after % sequences
588 while (*s && *s != '%')
589 s++;
590 if (!*s)
591 break;
592 s++;
Bram Moolenaardac13472019-09-16 21:06:21 +0200593 if (*s == '%' || *s == STL_TRUNCMARK || *s == STL_MIDDLEMARK)
594 {
595 s++;
596 continue;
597 }
598 if (*s == ')')
599 {
600 s++;
601 if (--groupdepth < 0)
602 break;
603 continue;
604 }
605 if (*s == '-')
606 s++;
607 while (VIM_ISDIGIT(*s))
608 s++;
609 if (*s == STL_USER_HL)
610 continue;
611 if (*s == '.')
612 {
613 s++;
614 while (*s && VIM_ISDIGIT(*s))
615 s++;
616 }
617 if (*s == '(')
618 {
619 groupdepth++;
620 continue;
621 }
622 if (vim_strchr(STL_ALL, *s) == NULL)
623 {
624 return illegal_char(errbuf, *s);
625 }
626 if (*s == '{')
627 {
zeertzjq5dc294a2022-04-15 13:17:57 +0100628 int reevaluate = (*++s == '%');
shadmansaleh30e3de22021-05-15 17:23:28 +0200629
zeertzjq5dc294a2022-04-15 13:17:57 +0100630 if (reevaluate && *++s == '}')
631 // "}" is not allowed immediately after "%{%"
632 return illegal_char(errbuf, '}');
shadmansaleh30e3de22021-05-15 17:23:28 +0200633 while ((*s != '}' || (reevaluate && s[-1] != '%')) && *s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200634 s++;
635 if (*s != '}')
zeertzjq5dc294a2022-04-15 13:17:57 +0100636 return e_unclosed_expression_sequence;
Bram Moolenaardac13472019-09-16 21:06:21 +0200637 }
638 }
Bram Moolenaardac13472019-09-16 21:06:21 +0200639 if (groupdepth != 0)
zeertzjq5dc294a2022-04-15 13:17:57 +0100640 return e_unbalanced_groups;
Bram Moolenaardac13472019-09-16 21:06:21 +0200641 return NULL;
642}
643#endif
644
645/*
646 * Handle string options that need some action to perform when changed.
LemonBoy77142312022-04-15 20:50:46 +0100647 * Returns NULL for success, or an unstranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +0200648 */
649 char *
650did_set_string_option(
651 int opt_idx, // index in options[] table
652 char_u **varp, // pointer to the option variable
653 int new_value_alloced, // new value was allocated
654 char_u *oldval, // previous value of the option
655 char *errbuf, // buffer for errors, or NULL
656 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
657 int *value_checked) // value was checked to be save, no
658 // need to set P_INSECURE
659{
660 char *errmsg = NULL;
661 char_u *s, *p;
662 int did_chartab = FALSE;
663 char_u **gvarp;
664 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
665#ifdef FEAT_GUI
666 // set when changing an option that only requires a redraw in the GUI
667 int redraw_gui_only = FALSE;
668#endif
669 int value_changed = FALSE;
670#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
671 int did_swaptcap = FALSE;
672#endif
673
674 // Get the global option to compare with, otherwise we would have to check
675 // two values for all local options.
676 gvarp = (char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
677
678 // Disallow changing some options from secure mode
679 if ((secure
680#ifdef HAVE_SANDBOX
681 || sandbox != 0
682#endif
683 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +0000684 errmsg = e_not_allowed_here;
Bram Moolenaardac13472019-09-16 21:06:21 +0200685
686 // Check for a "normal" directory or file name in some options. Disallow a
687 // path separator (slash and/or backslash), wildcards and characters that
688 // are often illegal in a file name. Be more permissive if "secure" is off.
689 else if (((get_option_flags(opt_idx) & P_NFNAME)
690 && vim_strpbrk(*varp, (char_u *)(secure
691 ? "/\\*?[|;&<>\r\n" : "/\\*?[<>\r\n")) != NULL)
692 || ((get_option_flags(opt_idx) & P_NDNAME)
693 && vim_strpbrk(*varp, (char_u *)"*?[|;&<>\r\n") != NULL))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000694 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200695
696 // 'term'
697 else if (varp == &T_NAME)
698 {
699 if (T_NAME[0] == NUL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000700 errmsg = e_cannot_set_term_to_empty_string;
Bram Moolenaardac13472019-09-16 21:06:21 +0200701#ifdef FEAT_GUI
Bram Moolenaar5daa9112021-02-01 18:39:47 +0100702 else if (gui.in_use)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000703 errmsg = e_cannot_change_term_in_GUI;
Bram Moolenaardac13472019-09-16 21:06:21 +0200704 else if (term_is_gui(T_NAME))
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000705 errmsg = e_use_gui_to_start_GUI;
Bram Moolenaardac13472019-09-16 21:06:21 +0200706#endif
707 else if (set_termname(T_NAME) == FAIL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000708 errmsg = e_not_found_in_termcap;
Bram Moolenaardac13472019-09-16 21:06:21 +0200709 else
710 {
711 // Screen colors may have changed.
712 redraw_later_clear();
713
714 // Both 'term' and 'ttytype' point to T_NAME, only set the
715 // P_ALLOCED flag on 'term'.
716 opt_idx = findoption((char_u *)"term");
717 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
718 }
719 }
720
721 // 'backupcopy'
722 else if (gvarp == &p_bkc)
723 {
724 char_u *bkc = p_bkc;
725 unsigned int *flags = &bkc_flags;
726
727 if (opt_flags & OPT_LOCAL)
728 {
729 bkc = curbuf->b_p_bkc;
730 flags = &curbuf->b_bkc_flags;
731 }
732
733 if ((opt_flags & OPT_LOCAL) && *bkc == NUL)
734 // make the local value empty: use the global value
735 *flags = 0;
736 else
737 {
738 if (opt_strings_flags(bkc, p_bkc_values, flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000739 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200740 if ((((int)*flags & BKC_AUTO) != 0)
741 + (((int)*flags & BKC_YES) != 0)
742 + (((int)*flags & BKC_NO) != 0) != 1)
743 {
744 // Must have exactly one of "auto", "yes" and "no".
745 (void)opt_strings_flags(oldval, p_bkc_values, flags, TRUE);
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000746 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200747 }
748 }
749 }
750
751 // 'backupext' and 'patchmode'
752 else if (varp == &p_bex || varp == &p_pm)
753 {
754 if (STRCMP(*p_bex == '.' ? p_bex + 1 : p_bex,
755 *p_pm == '.' ? p_pm + 1 : p_pm) == 0)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100756 errmsg = e_backupext_and_patchmode_are_equal;
Bram Moolenaardac13472019-09-16 21:06:21 +0200757 }
758#ifdef FEAT_LINEBREAK
759 // 'breakindentopt'
760 else if (varp == &curwin->w_p_briopt)
761 {
762 if (briopt_check(curwin) == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000763 errmsg = e_invalid_argument;
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000764 // list setting requires a redraw
765 if (curwin->w_briopt_list)
766 redraw_all_later(NOT_VALID);
Bram Moolenaardac13472019-09-16 21:06:21 +0200767 }
768#endif
769
770 // 'isident', 'iskeyword', 'isprint or 'isfname' option: refill g_chartab[]
771 // If the new option is invalid, use old value. 'lisp' option: refill
772 // g_chartab[] for '-' char
773 else if ( varp == &p_isi
774 || varp == &(curbuf->b_p_isk)
775 || varp == &p_isp
776 || varp == &p_isf)
777 {
778 if (init_chartab() == FAIL)
779 {
780 did_chartab = TRUE; // need to restore it below
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000781 errmsg = e_invalid_argument; // error in value
Bram Moolenaardac13472019-09-16 21:06:21 +0200782 }
783 }
784
785 // 'helpfile'
786 else if (varp == &p_hf)
787 {
788 // May compute new values for $VIM and $VIMRUNTIME
789 if (didset_vim)
LemonBoy77142312022-04-15 20:50:46 +0100790 vim_unsetenv_ext((char_u *)"VIM");
Bram Moolenaardac13472019-09-16 21:06:21 +0200791 if (didset_vimruntime)
LemonBoy77142312022-04-15 20:50:46 +0100792 vim_unsetenv_ext((char_u *)"VIMRUNTIME");
Bram Moolenaardac13472019-09-16 21:06:21 +0200793 }
794
795#ifdef FEAT_SYN_HL
796 // 'cursorlineopt'
797 else if (varp == &curwin->w_p_culopt
798 || gvarp == &curwin->w_allbuf_opt.wo_culopt)
799 {
800 if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000801 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200802 }
803
804 // 'colorcolumn'
805 else if (varp == &curwin->w_p_cc)
806 errmsg = check_colorcolumn(curwin);
807#endif
808
809#ifdef FEAT_MULTI_LANG
810 // 'helplang'
811 else if (varp == &p_hlg)
812 {
813 // Check for "", "ab", "ab,cd", etc.
814 for (s = p_hlg; *s != NUL; s += 3)
815 {
816 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
817 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000818 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200819 break;
820 }
821 if (s[2] == NUL)
822 break;
823 }
824 }
825#endif
826
827 // 'highlight'
828 else if (varp == &p_hl)
829 {
830 if (highlight_changed() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000831 errmsg = e_invalid_argument; // invalid flags
Bram Moolenaardac13472019-09-16 21:06:21 +0200832 }
833
834 // 'nrformats'
835 else if (gvarp == &p_nf)
836 {
837 if (check_opt_strings(*varp, p_nf_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000838 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200839 }
840
841#ifdef FEAT_SESSION
842 // 'sessionoptions'
843 else if (varp == &p_ssop)
844 {
845 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000846 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200847 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
848 {
849 // Don't allow both "sesdir" and "curdir".
850 (void)opt_strings_flags(oldval, p_ssop_values, &ssop_flags, TRUE);
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000851 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200852 }
853 }
854 // 'viewoptions'
855 else if (varp == &p_vop)
856 {
857 if (opt_strings_flags(p_vop, p_ssop_values, &vop_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000858 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200859 }
860#endif
861
862 // 'scrollopt'
863 else if (varp == &p_sbo)
864 {
865 if (check_opt_strings(p_sbo, p_scbopt_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000866 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200867 }
868
869 // 'ambiwidth'
870 else if (varp == &p_ambw || varp == &p_emoji)
871 {
872 if (check_opt_strings(p_ambw, p_ambw_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000873 errmsg = e_invalid_argument;
Bram Moolenaareed9d462021-02-15 20:38:25 +0100874 else if (set_chars_option(curwin, &p_fcs) != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100875 errmsg = e_conflicts_with_value_of_fillchars;
Bram Moolenaareed9d462021-02-15 20:38:25 +0100876 else
877 {
878 tabpage_T *tp;
879 win_T *wp;
880
881 FOR_ALL_TAB_WINDOWS(tp, wp)
882 {
883 if (set_chars_option(wp, &wp->w_p_lcs) != NULL)
884 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100885 errmsg = e_conflicts_with_value_of_listchars;
Bram Moolenaareed9d462021-02-15 20:38:25 +0100886 goto ambw_end;
887 }
888 }
889 }
890ambw_end:
891 {}
Bram Moolenaardac13472019-09-16 21:06:21 +0200892 }
893
894 // 'background'
895 else if (varp == &p_bg)
896 {
897 if (check_opt_strings(p_bg, p_bg_values, FALSE) == OK)
898 {
899#ifdef FEAT_EVAL
900 int dark = (*p_bg == 'd');
901#endif
902
903 init_highlight(FALSE, FALSE);
904
905#ifdef FEAT_EVAL
906 if (dark != (*p_bg == 'd')
907 && get_var_value((char_u *)"g:colors_name") != NULL)
908 {
909 // The color scheme must have set 'background' back to another
910 // value, that's not what we want here. Disable the color
911 // scheme and set the colors again.
912 do_unlet((char_u *)"g:colors_name", TRUE);
913 free_string_option(p_bg);
914 p_bg = vim_strsave((char_u *)(dark ? "dark" : "light"));
915 check_string_option(&p_bg);
916 init_highlight(FALSE, FALSE);
917 }
918#endif
Bram Moolenaarad431992021-05-03 20:40:38 +0200919#ifdef FEAT_TERMINAL
920 term_update_colors_all();
921#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200922 }
923 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000924 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200925 }
926
927 // 'wildmode'
928 else if (varp == &p_wim)
929 {
930 if (check_opt_wim() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000931 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200932 }
933
934 // 'wildoptions'
935 else if (varp == &p_wop)
936 {
937 if (check_opt_strings(p_wop, p_wop_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000938 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200939 }
940
941#ifdef FEAT_WAK
942 // 'winaltkeys'
943 else if (varp == &p_wak)
944 {
945 if (*p_wak == NUL
946 || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000947 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200948# ifdef FEAT_MENU
ichizok02560422022-04-05 14:18:44 +0100949# if defined(FEAT_GUI_MOTIF)
Bram Moolenaardac13472019-09-16 21:06:21 +0200950 else if (gui.in_use)
951 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
ichizok02560422022-04-05 14:18:44 +0100952# elif defined(FEAT_GUI_GTK)
Bram Moolenaardac13472019-09-16 21:06:21 +0200953 else if (gui.in_use)
954 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
Bram Moolenaardac13472019-09-16 21:06:21 +0200955# endif
956# endif
957 }
958#endif
959
960 // 'eventignore'
961 else if (varp == &p_ei)
962 {
963 if (check_ei() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000964 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200965 }
966
967 // 'encoding', 'fileencoding', 'termencoding' and 'makeencoding'
968 else if (varp == &p_enc || gvarp == &p_fenc || varp == &p_tenc
969 || gvarp == &p_menc)
970 {
971 if (gvarp == &p_fenc)
972 {
973 if (!curbuf->b_p_ma && opt_flags != OPT_GLOBAL)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200974 errmsg = e_cannot_make_changes_modifiable_is_off;
Bram Moolenaardac13472019-09-16 21:06:21 +0200975 else if (vim_strchr(*varp, ',') != NULL)
976 // No comma allowed in 'fileencoding'; catches confusing it
977 // with 'fileencodings'.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000978 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200979 else
980 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200981 // May show a "+" in the title now.
982 redraw_titles();
Bram Moolenaardac13472019-09-16 21:06:21 +0200983 // Add 'fileencoding' to the swap file.
984 ml_setflags(curbuf);
985 }
986 }
987 if (errmsg == NULL)
988 {
989 // canonize the value, so that STRCMP() can be used on it
990 p = enc_canonize(*varp);
991 if (p != NULL)
992 {
993 vim_free(*varp);
994 *varp = p;
995 }
996 if (varp == &p_enc)
997 {
998 errmsg = mb_init();
Bram Moolenaardac13472019-09-16 21:06:21 +0200999 redraw_titles();
Bram Moolenaardac13472019-09-16 21:06:21 +02001000 }
1001 }
1002
1003#if defined(FEAT_GUI_GTK)
1004 if (errmsg == NULL && varp == &p_tenc && gui.in_use)
1005 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001006 // GTK uses only a single encoding, and that is UTF-8.
Bram Moolenaardac13472019-09-16 21:06:21 +02001007 if (STRCMP(p_tenc, "utf-8") != 0)
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001008 errmsg = e_cannot_be_changed_in_gtk_GUI;
Bram Moolenaardac13472019-09-16 21:06:21 +02001009 }
1010#endif
1011
1012 if (errmsg == NULL)
1013 {
1014#ifdef FEAT_KEYMAP
1015 // When 'keymap' is used and 'encoding' changes, reload the keymap
1016 // (with another encoding).
1017 if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
1018 (void)keymap_init();
1019#endif
1020
1021 // When 'termencoding' is not empty and 'encoding' changes or when
1022 // 'termencoding' changes, need to setup for keyboard input and
1023 // display output conversion.
1024 if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
1025 {
1026 if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
1027 || convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
1028 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001029 semsg(_(e_cannot_convert_between_str_and_str),
1030 p_tenc, p_enc);
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001031 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001032 }
1033 }
1034
1035#if defined(MSWIN)
1036 // $HOME may have characters in active code page.
1037 if (varp == &p_enc)
1038 init_homedir();
1039#endif
1040 }
1041 }
1042
1043#if defined(FEAT_POSTSCRIPT)
1044 else if (varp == &p_penc)
1045 {
1046 // Canonize printencoding if VIM standard one
1047 p = enc_canonize(p_penc);
1048 if (p != NULL)
1049 {
1050 vim_free(p_penc);
1051 p_penc = p;
1052 }
1053 else
1054 {
1055 // Ensure lower case and '-' for '_'
1056 for (s = p_penc; *s != NUL; s++)
1057 {
1058 if (*s == '_')
1059 *s = '-';
1060 else
1061 *s = TOLOWER_ASC(*s);
1062 }
1063 }
1064 }
1065#endif
1066
1067#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
1068 else if (varp == &p_imak)
1069 {
1070 if (!im_xim_isvalid_imactivate())
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001071 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001072 }
1073#endif
1074
1075#ifdef FEAT_KEYMAP
1076 else if (varp == &curbuf->b_p_keymap)
1077 {
1078 if (!valid_filetype(*varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001079 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001080 else
1081 {
1082 int secure_save = secure;
1083
1084 // Reset the secure flag, since the value of 'keymap' has
1085 // been checked to be safe.
1086 secure = 0;
1087
1088 // load or unload key mapping tables
1089 errmsg = keymap_init();
1090
1091 secure = secure_save;
1092
1093 // Since we check the value, there is no need to set P_INSECURE,
1094 // even when the value comes from a modeline.
1095 *value_checked = TRUE;
1096 }
1097
1098 if (errmsg == NULL)
1099 {
1100 if (*curbuf->b_p_keymap != NUL)
1101 {
1102 // Installed a new keymap, switch on using it.
1103 curbuf->b_p_iminsert = B_IMODE_LMAP;
1104 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
1105 curbuf->b_p_imsearch = B_IMODE_LMAP;
1106 }
1107 else
1108 {
1109 // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
1110 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
1111 curbuf->b_p_iminsert = B_IMODE_NONE;
1112 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
1113 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
1114 }
1115 if ((opt_flags & OPT_LOCAL) == 0)
1116 {
1117 set_iminsert_global();
1118 set_imsearch_global();
1119 }
1120 status_redraw_curbuf();
1121 }
1122 }
1123#endif
1124
1125 // 'fileformat'
1126 else if (gvarp == &p_ff)
1127 {
1128 if (!curbuf->b_p_ma && !(opt_flags & OPT_GLOBAL))
Bram Moolenaar108010a2021-06-27 22:03:33 +02001129 errmsg = e_cannot_make_changes_modifiable_is_off;
Bram Moolenaardac13472019-09-16 21:06:21 +02001130 else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001131 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001132 else
1133 {
1134 // may also change 'textmode'
1135 if (get_fileformat(curbuf) == EOL_DOS)
1136 curbuf->b_p_tx = TRUE;
1137 else
1138 curbuf->b_p_tx = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02001139 redraw_titles();
Bram Moolenaardac13472019-09-16 21:06:21 +02001140 // update flag in swap file
1141 ml_setflags(curbuf);
1142 // Redraw needed when switching to/from "mac": a CR in the text
1143 // will be displayed differently.
1144 if (get_fileformat(curbuf) == EOL_MAC || *oldval == 'm')
1145 redraw_curbuf_later(NOT_VALID);
1146 }
1147 }
1148
1149 // 'fileformats'
1150 else if (varp == &p_ffs)
1151 {
1152 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001153 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001154 else
1155 {
1156 // also change 'textauto'
1157 if (*p_ffs == NUL)
1158 p_ta = FALSE;
1159 else
1160 p_ta = TRUE;
1161 }
1162 }
1163
1164#if defined(FEAT_CRYPT)
1165 // 'cryptkey'
1166 else if (gvarp == &p_key)
1167 {
1168 // Make sure the ":set" command doesn't show the new value in the
1169 // history.
1170 remove_key_from_history();
1171
1172 if (STRCMP(curbuf->b_p_key, oldval) != 0)
1173 // Need to update the swapfile.
Bram Moolenaar76cb6832020-05-15 22:30:38 +02001174 {
Bram Moolenaardac13472019-09-16 21:06:21 +02001175 ml_set_crypt_key(curbuf, oldval,
1176 *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
Bram Moolenaar76cb6832020-05-15 22:30:38 +02001177 changed_internal();
1178 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001179 }
1180
1181 else if (gvarp == &p_cm)
1182 {
1183 if (opt_flags & OPT_LOCAL)
1184 p = curbuf->b_p_cm;
1185 else
1186 p = p_cm;
1187 if (check_opt_strings(p, p_cm_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001188 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001189 else if (crypt_self_test() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001190 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001191 else
1192 {
1193 // When setting the global value to empty, make it "zip".
1194 if (*p_cm == NUL)
1195 {
1196 if (new_value_alloced)
1197 free_string_option(p_cm);
1198 p_cm = vim_strsave((char_u *)"zip");
1199 new_value_alloced = TRUE;
1200 }
1201 // When using ":set cm=name" the local value is going to be empty.
1202 // Do that here, otherwise the crypt functions will still use the
1203 // local value.
1204 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1205 {
1206 free_string_option(curbuf->b_p_cm);
1207 curbuf->b_p_cm = empty_option;
1208 }
1209
1210 // Need to update the swapfile when the effective method changed.
1211 // Set "s" to the effective old value, "p" to the effective new
1212 // method and compare.
1213 if ((opt_flags & OPT_LOCAL) && *oldval == NUL)
1214 s = p_cm; // was previously using the global value
1215 else
1216 s = oldval;
1217 if (*curbuf->b_p_cm == NUL)
1218 p = p_cm; // is now using the global value
1219 else
1220 p = curbuf->b_p_cm;
1221 if (STRCMP(s, p) != 0)
1222 ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1223
1224 // If the global value changes need to update the swapfile for all
1225 // buffers using that value.
1226 if ((opt_flags & OPT_GLOBAL) && STRCMP(p_cm, oldval) != 0)
1227 {
1228 buf_T *buf;
1229
1230 FOR_ALL_BUFFERS(buf)
1231 if (buf != curbuf && *buf->b_p_cm == NUL)
1232 ml_set_crypt_key(buf, buf->b_p_key, oldval);
1233 }
1234 }
1235 }
1236#endif
1237
1238 // 'matchpairs'
1239 else if (gvarp == &p_mps)
1240 {
1241 if (has_mbyte)
1242 {
1243 for (p = *varp; *p != NUL; ++p)
1244 {
1245 int x2 = -1;
1246 int x3 = -1;
1247
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01001248 p += mb_ptr2len(p);
Bram Moolenaardac13472019-09-16 21:06:21 +02001249 if (*p != NUL)
1250 x2 = *p++;
1251 if (*p != NUL)
1252 {
1253 x3 = mb_ptr2char(p);
1254 p += mb_ptr2len(p);
1255 }
1256 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
1257 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001258 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001259 break;
1260 }
1261 if (*p == NUL)
1262 break;
1263 }
1264 }
1265 else
1266 {
1267 // Check for "x:y,x:y"
1268 for (p = *varp; *p != NUL; p += 4)
1269 {
1270 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
1271 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001272 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001273 break;
1274 }
1275 if (p[3] == NUL)
1276 break;
1277 }
1278 }
1279 }
1280
Bram Moolenaardac13472019-09-16 21:06:21 +02001281 // 'comments'
1282 else if (gvarp == &p_com)
1283 {
1284 for (s = *varp; *s; )
1285 {
1286 while (*s && *s != ':')
1287 {
1288 if (vim_strchr((char_u *)COM_ALL, *s) == NULL
1289 && !VIM_ISDIGIT(*s) && *s != '-')
1290 {
1291 errmsg = illegal_char(errbuf, *s);
1292 break;
1293 }
1294 ++s;
1295 }
1296 if (*s++ == NUL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001297 errmsg = e_missing_colon;
Bram Moolenaardac13472019-09-16 21:06:21 +02001298 else if (*s == ',' || *s == NUL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001299 errmsg = e_zero_length_string;
Bram Moolenaardac13472019-09-16 21:06:21 +02001300 if (errmsg != NULL)
1301 break;
1302 while (*s && *s != ',')
1303 {
1304 if (*s == '\\' && s[1] != NUL)
1305 ++s;
1306 ++s;
1307 }
1308 s = skip_to_option_part(s);
1309 }
1310 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001311
Bram Moolenaareed9d462021-02-15 20:38:25 +01001312 // global 'listchars'
Bram Moolenaardac13472019-09-16 21:06:21 +02001313 else if (varp == &p_lcs)
1314 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001315 errmsg = set_chars_option(curwin, varp);
1316 if (errmsg == NULL)
1317 {
1318 tabpage_T *tp;
1319 win_T *wp;
1320
1321 // The current window is set to use the global 'listchars' value.
1322 // So clear the window-local value.
1323 if (!(opt_flags & OPT_GLOBAL))
1324 clear_string_option(&curwin->w_p_lcs);
1325 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar606efc72021-11-12 19:52:47 +00001326 // If no error was returned above, we don't expect an error
1327 // here, so ignore the return value.
1328 (void)set_chars_option(wp, &wp->w_p_lcs);
1329
Bram Moolenaareed9d462021-02-15 20:38:25 +01001330 redraw_all_later(NOT_VALID);
1331 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001332 }
1333
Bram Moolenaareed9d462021-02-15 20:38:25 +01001334 // local 'listchars'
1335 else if (varp == &curwin->w_p_lcs)
1336 errmsg = set_chars_option(curwin, varp);
1337
Bram Moolenaardac13472019-09-16 21:06:21 +02001338 // 'fillchars'
1339 else if (varp == &p_fcs)
1340 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001341 errmsg = set_chars_option(curwin, varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02001342 }
1343
1344#ifdef FEAT_CMDWIN
1345 // 'cedit'
1346 else if (varp == &p_cedit)
1347 {
1348 errmsg = check_cedit();
1349 }
1350#endif
1351
1352 // 'verbosefile'
1353 else if (varp == &p_vfile)
1354 {
1355 verbose_stop();
1356 if (*p_vfile != NUL && verbose_open() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001357 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001358 }
1359
1360#ifdef FEAT_VIMINFO
1361 // 'viminfo'
1362 else if (varp == &p_viminfo)
1363 {
1364 for (s = p_viminfo; *s;)
1365 {
1366 // Check it's a valid character
1367 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
1368 {
1369 errmsg = illegal_char(errbuf, *s);
1370 break;
1371 }
1372 if (*s == 'n') // name is always last one
1373 break;
1374 else if (*s == 'r') // skip until next ','
1375 {
1376 while (*++s && *s != ',')
1377 ;
1378 }
1379 else if (*s == '%')
1380 {
1381 // optional number
1382 while (vim_isdigit(*++s))
1383 ;
1384 }
1385 else if (*s == '!' || *s == 'h' || *s == 'c')
1386 ++s; // no extra chars
1387 else // must have a number
1388 {
1389 while (vim_isdigit(*++s))
1390 ;
1391
1392 if (!VIM_ISDIGIT(*(s - 1)))
1393 {
1394 if (errbuf != NULL)
1395 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001396 sprintf(errbuf,
1397 _(e_missing_number_after_angle_str_angle),
Bram Moolenaardac13472019-09-16 21:06:21 +02001398 transchar_byte(*(s - 1)));
1399 errmsg = errbuf;
1400 }
1401 else
1402 errmsg = "";
1403 break;
1404 }
1405 }
1406 if (*s == ',')
1407 ++s;
1408 else if (*s)
1409 {
1410 if (errbuf != NULL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001411 errmsg = e_missing_comma;
Bram Moolenaardac13472019-09-16 21:06:21 +02001412 else
1413 errmsg = "";
1414 break;
1415 }
1416 }
1417 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001418 errmsg = e_must_specify_a_value;
Bram Moolenaardac13472019-09-16 21:06:21 +02001419 }
1420#endif // FEAT_VIMINFO
1421
1422 // terminal options
1423 else if (istermoption_idx(opt_idx) && full_screen)
1424 {
1425 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
1426 if (varp == &T_CCO)
1427 {
1428 int colors = atoi((char *)T_CCO);
1429
1430 // Only reinitialize colors if t_Co value has really changed to
1431 // avoid expensive reload of colorscheme if t_Co is set to the
1432 // same value multiple times.
1433 if (colors != t_colors)
1434 {
1435 t_colors = colors;
1436 if (t_colors <= 1)
1437 {
1438 if (new_value_alloced)
1439 vim_free(T_CCO);
1440 T_CCO = empty_option;
1441 }
1442#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1443 if (is_term_win32())
1444 {
1445 swap_tcap();
1446 did_swaptcap = TRUE;
1447 }
1448#endif
1449 // We now have a different color setup, initialize it again.
1450 init_highlight(TRUE, FALSE);
1451 }
1452 }
1453 ttest(FALSE);
1454 if (varp == &T_ME)
1455 {
1456 out_str(T_ME);
1457 redraw_later(CLEAR);
1458#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
1459 // Since t_me has been set, this probably means that the user
1460 // wants to use this as default colors. Need to reset default
1461 // background/foreground colors.
1462# ifdef VIMDLL
1463 if (!gui.in_use && !gui.starting)
1464# endif
1465 mch_set_normal_colors();
1466#endif
1467 }
1468 if (varp == &T_BE && termcap_active)
1469 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02001470#ifdef FEAT_JOB_CHANNEL
1471 ch_log_output = TRUE;
1472#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02001473 if (*T_BE == NUL)
1474 // When clearing t_BE we assume the user no longer wants
1475 // bracketed paste, thus disable it by writing t_BD.
1476 out_str(T_BD);
1477 else
1478 out_str(T_BE);
1479 }
1480 }
1481
1482#ifdef FEAT_LINEBREAK
1483 // 'showbreak'
Bram Moolenaaree857022019-11-09 23:26:40 +01001484 else if (gvarp == &p_sbr)
Bram Moolenaardac13472019-09-16 21:06:21 +02001485 {
Bram Moolenaaree857022019-11-09 23:26:40 +01001486 for (s = *varp; *s; )
Bram Moolenaardac13472019-09-16 21:06:21 +02001487 {
1488 if (ptr2cells(s) != 1)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001489 errmsg = e_showbreak_contains_unprintable_or_wide_character;
Bram Moolenaardac13472019-09-16 21:06:21 +02001490 MB_PTR_ADV(s);
1491 }
1492 }
1493#endif
1494
1495#ifdef FEAT_GUI
1496 // 'guifont'
1497 else if (varp == &p_guifont)
1498 {
1499 if (gui.in_use)
1500 {
1501 p = p_guifont;
1502# if defined(FEAT_GUI_GTK)
1503 // Put up a font dialog and let the user select a new value.
1504 // If this is cancelled go back to the old value but don't
1505 // give an error message.
1506 if (STRCMP(p, "*") == 0)
1507 {
1508 p = gui_mch_font_dialog(oldval);
1509
1510 if (new_value_alloced)
1511 free_string_option(p_guifont);
1512
1513 p_guifont = (p != NULL) ? p : vim_strsave(oldval);
1514 new_value_alloced = TRUE;
1515 }
1516# endif
1517 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
1518 {
1519# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
1520 if (STRCMP(p_guifont, "*") == 0)
1521 {
1522 // Dialog was cancelled: Keep the old value without giving
1523 // an error message.
1524 if (new_value_alloced)
1525 free_string_option(p_guifont);
1526 p_guifont = vim_strsave(oldval);
1527 new_value_alloced = TRUE;
1528 }
1529 else
1530# endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001531 errmsg = e_invalid_fonts;
Bram Moolenaardac13472019-09-16 21:06:21 +02001532 }
1533 }
1534 redraw_gui_only = TRUE;
1535 }
1536# ifdef FEAT_XFONTSET
1537 else if (varp == &p_guifontset)
1538 {
1539 if (STRCMP(p_guifontset, "*") == 0)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001540 errmsg = e_cant_select_fontset;
Bram Moolenaardac13472019-09-16 21:06:21 +02001541 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001542 errmsg = e_invalid_fontset;
Bram Moolenaardac13472019-09-16 21:06:21 +02001543 redraw_gui_only = TRUE;
1544 }
1545# endif
1546 else if (varp == &p_guifontwide)
1547 {
1548 if (STRCMP(p_guifontwide, "*") == 0)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001549 errmsg = e_cant_select_wide_font;
Bram Moolenaardac13472019-09-16 21:06:21 +02001550 else if (gui_get_wide_font() == FAIL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001551 errmsg = e_invalid_wide_font;
Bram Moolenaardac13472019-09-16 21:06:21 +02001552 redraw_gui_only = TRUE;
1553 }
1554#endif
Dusan Popovic4eeedc02021-10-16 20:52:05 +01001555# if defined(FEAT_GUI_GTK)
1556 else if (varp == &p_guiligatures)
1557 {
1558 gui_set_ligatures();
1559 redraw_gui_only = TRUE;
1560 }
1561# endif
Bram Moolenaardac13472019-09-16 21:06:21 +02001562
1563#ifdef CURSOR_SHAPE
1564 // 'guicursor'
1565 else if (varp == &p_guicursor)
1566 errmsg = parse_shape_opt(SHAPE_CURSOR);
1567#endif
1568
1569#ifdef FEAT_MOUSESHAPE
1570 // 'mouseshape'
1571 else if (varp == &p_mouseshape)
1572 {
1573 errmsg = parse_shape_opt(SHAPE_MOUSE);
1574 update_mouseshape(-1);
1575 }
1576#endif
1577
1578#ifdef FEAT_PRINTER
1579 else if (varp == &p_popt)
1580 errmsg = parse_printoptions();
1581# if defined(FEAT_POSTSCRIPT)
1582 else if (varp == &p_pmfn)
1583 errmsg = parse_printmbfont();
1584# endif
1585#endif
1586
1587#ifdef FEAT_LANGMAP
1588 // 'langmap'
1589 else if (varp == &p_langmap)
1590 langmap_set();
1591#endif
1592
1593#ifdef FEAT_LINEBREAK
1594 // 'breakat'
1595 else if (varp == &p_breakat)
1596 fill_breakat_flags();
1597#endif
1598
Bram Moolenaardac13472019-09-16 21:06:21 +02001599 // 'titlestring' and 'iconstring'
1600 else if (varp == &p_titlestring || varp == &p_iconstring)
1601 {
Bram Moolenaar651fca82021-11-29 20:39:38 +00001602#ifdef FEAT_STL_OPT
Bram Moolenaardac13472019-09-16 21:06:21 +02001603 int flagval = (varp == &p_titlestring) ? STL_IN_TITLE : STL_IN_ICON;
1604
1605 // NULL => statusline syntax
1606 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
1607 stl_syntax |= flagval;
1608 else
1609 stl_syntax &= ~flagval;
Bram Moolenaar651fca82021-11-29 20:39:38 +00001610#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02001611 did_set_title();
1612 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001613
1614#ifdef FEAT_GUI
1615 // 'guioptions'
1616 else if (varp == &p_go)
1617 {
1618 gui_init_which_components(oldval);
1619 redraw_gui_only = TRUE;
1620 }
1621#endif
1622
1623#if defined(FEAT_GUI_TABLINE)
1624 // 'guitablabel'
1625 else if (varp == &p_gtl)
1626 {
1627 redraw_tabline = TRUE;
1628 redraw_gui_only = TRUE;
1629 }
1630 // 'guitabtooltip'
1631 else if (varp == &p_gtt)
1632 {
1633 redraw_gui_only = TRUE;
1634 }
1635#endif
1636
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001637#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +02001638 // 'ttymouse'
1639 else if (varp == &p_ttym)
1640 {
1641 // Switch the mouse off before changing the escape sequences used for
1642 // that.
1643 mch_setmouse(FALSE);
1644 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001645 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001646 else
1647 check_mouse_termcode();
1648 if (termcap_active)
1649 setmouse(); // may switch it on again
1650 }
1651#endif
1652
1653 // 'selection'
1654 else if (varp == &p_sel)
1655 {
1656 if (*p_sel == NUL
1657 || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001658 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001659 }
1660
1661 // 'selectmode'
1662 else if (varp == &p_slm)
1663 {
1664 if (check_opt_strings(p_slm, p_slm_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001665 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001666 }
1667
1668#ifdef FEAT_BROWSE
1669 // 'browsedir'
1670 else if (varp == &p_bsdir)
1671 {
1672 if (check_opt_strings(p_bsdir, p_bsdir_values, FALSE) != OK
1673 && !mch_isdir(p_bsdir))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001674 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001675 }
1676#endif
1677
1678 // 'keymodel'
1679 else if (varp == &p_km)
1680 {
1681 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001682 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001683 else
1684 {
1685 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
1686 km_startsel = (vim_strchr(p_km, 'a') != NULL);
1687 }
1688 }
1689
1690 // 'mousemodel'
1691 else if (varp == &p_mousem)
1692 {
1693 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001694 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001695#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
1696 else if (*p_mousem != *oldval)
1697 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
1698 // to create or delete the popup menus.
1699 gui_motif_update_mousemodel(root_menu);
1700#endif
1701 }
1702
1703 // 'switchbuf'
1704 else if (varp == &p_swb)
1705 {
1706 if (opt_strings_flags(p_swb, p_swb_values, &swb_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001707 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001708 }
1709
1710 // 'debug'
1711 else if (varp == &p_debug)
1712 {
1713 if (check_opt_strings(p_debug, p_debug_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001714 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001715 }
1716
1717 // 'display'
1718 else if (varp == &p_dy)
1719 {
1720 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001721 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001722 else
1723 (void)init_chartab();
1724
1725 }
1726
1727 // 'eadirection'
1728 else if (varp == &p_ead)
1729 {
1730 if (check_opt_strings(p_ead, p_ead_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001731 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001732 }
1733
1734#ifdef FEAT_CLIPBOARD
1735 // 'clipboard'
1736 else if (varp == &p_cb)
1737 errmsg = check_clipboard_option();
1738#endif
1739
1740#ifdef FEAT_SPELL
1741 // When 'spelllang' or 'spellfile' is set and there is a window for this
1742 // buffer in which 'spell' is set load the wordlists.
1743 else if (varp == &(curwin->w_s->b_p_spl)
1744 || varp == &(curwin->w_s->b_p_spf))
1745 {
1746 int is_spellfile = varp == &(curwin->w_s->b_p_spf);
1747
1748 if ((is_spellfile && !valid_spellfile(*varp))
Bram Moolenaarf154f3a2020-06-08 18:54:49 +02001749 || (!is_spellfile && !valid_spelllang(*varp)))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001750 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001751 else
1752 errmsg = did_set_spell_option(is_spellfile);
1753 }
1754 // When 'spellcapcheck' is set compile the regexp program.
1755 else if (varp == &(curwin->w_s->b_p_spc))
1756 {
1757 errmsg = compile_cap_prog(curwin->w_s);
1758 }
Bram Moolenaar362b44b2020-06-10 21:47:00 +02001759 // 'spelloptions'
1760 else if (varp == &(curwin->w_s->b_p_spo))
1761 {
1762 if (**varp != NUL && STRCMP("camel", *varp) != 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001763 errmsg = e_invalid_argument;
Bram Moolenaar362b44b2020-06-10 21:47:00 +02001764 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001765 // 'spellsuggest'
1766 else if (varp == &p_sps)
1767 {
1768 if (spell_check_sps() != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001769 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001770 }
1771 // 'mkspellmem'
1772 else if (varp == &p_msm)
1773 {
1774 if (spell_check_msm() != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001775 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001776 }
1777#endif
1778
1779 // When 'bufhidden' is set, check for valid value.
1780 else if (gvarp == &p_bh)
1781 {
1782 if (check_opt_strings(curbuf->b_p_bh, p_bufhidden_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001783 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001784 }
1785
1786 // When 'buftype' is set, check for valid value.
1787 else if (gvarp == &p_bt)
1788 {
1789 if (check_opt_strings(curbuf->b_p_bt, p_buftype_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001790 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001791 else
1792 {
1793 if (curwin->w_status_height)
1794 {
1795 curwin->w_redr_status = TRUE;
1796 redraw_later(VALID);
1797 }
1798 curbuf->b_help = (curbuf->b_p_bt[0] == 'h');
Bram Moolenaardac13472019-09-16 21:06:21 +02001799 redraw_titles();
Bram Moolenaardac13472019-09-16 21:06:21 +02001800 }
1801 }
1802
1803#ifdef FEAT_STL_OPT
zeertzjq5dc294a2022-04-15 13:17:57 +01001804 // 'statusline', 'tabline' or 'rulerformat'
1805 else if (gvarp == &p_stl || varp == &p_tal || varp == &p_ruf)
Bram Moolenaardac13472019-09-16 21:06:21 +02001806 {
1807 int wid;
1808
1809 if (varp == &p_ruf) // reset ru_wid first
1810 ru_wid = 0;
1811 s = *varp;
1812 if (varp == &p_ruf && *s == '%')
1813 {
1814 // set ru_wid if 'ruf' starts with "%99("
1815 if (*++s == '-') // ignore a '-'
1816 s++;
1817 wid = getdigits(&s);
1818 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
1819 ru_wid = wid;
1820 else
1821 errmsg = check_stl_option(p_ruf);
1822 }
zeertzjq5dc294a2022-04-15 13:17:57 +01001823 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
Bram Moolenaardac13472019-09-16 21:06:21 +02001824 else if (varp == &p_ruf || s[0] != '%' || s[1] != '!')
1825 errmsg = check_stl_option(s);
1826 if (varp == &p_ruf && errmsg == NULL)
1827 comp_col();
1828 }
1829#endif
1830
1831 // check if it is a valid value for 'complete' -- Acevedo
1832 else if (gvarp == &p_cpt)
1833 {
1834 for (s = *varp; *s;)
1835 {
1836 while (*s == ',' || *s == ' ')
1837 s++;
1838 if (!*s)
1839 break;
1840 if (vim_strchr((char_u *)".wbuksid]tU", *s) == NULL)
1841 {
1842 errmsg = illegal_char(errbuf, *s);
1843 break;
1844 }
1845 if (*++s != NUL && *s != ',' && *s != ' ')
1846 {
1847 if (s[-1] == 'k' || s[-1] == 's')
1848 {
1849 // skip optional filename after 'k' and 's'
1850 while (*s && *s != ',' && *s != ' ')
1851 {
1852 if (*s == '\\' && s[1] != NUL)
1853 ++s;
1854 ++s;
1855 }
1856 }
1857 else
1858 {
1859 if (errbuf != NULL)
1860 {
1861 sprintf((char *)errbuf,
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001862 _(e_illegal_character_after_chr), *--s);
Bram Moolenaardac13472019-09-16 21:06:21 +02001863 errmsg = errbuf;
1864 }
1865 else
1866 errmsg = "";
1867 break;
1868 }
1869 }
1870 }
1871 }
1872
1873 // 'completeopt'
1874 else if (varp == &p_cot)
1875 {
1876 if (check_opt_strings(p_cot, p_cot_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001877 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001878 else
1879 completeopt_was_set();
1880 }
1881
1882#ifdef BACKSLASH_IN_FILENAME
1883 // 'completeslash'
1884 else if (gvarp == &p_csl)
1885 {
1886 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1887 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001888 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001889 }
1890#endif
1891
1892#ifdef FEAT_SIGNS
1893 // 'signcolumn'
1894 else if (varp == &curwin->w_p_scl)
1895 {
1896 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001897 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001898 // When changing the 'signcolumn' to or from 'number', recompute the
1899 // width of the number column if 'number' or 'relativenumber' is set.
1900 if (((*oldval == 'n' && *(oldval + 1) == 'u')
1901 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
1902 && (curwin->w_p_nu || curwin->w_p_rnu))
1903 curwin->w_nrwidth_line_count = 0;
1904 }
1905#endif
1906
1907
1908#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
1909 // 'toolbar'
1910 else if (varp == &p_toolbar)
1911 {
1912 if (opt_strings_flags(p_toolbar, p_toolbar_values,
1913 &toolbar_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001914 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001915 else
1916 {
1917 out_flush();
1918 gui_mch_show_toolbar((toolbar_flags &
1919 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
1920 }
1921 }
1922#endif
1923
1924#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
1925 // 'toolbariconsize': GTK+ 2 only
1926 else if (varp == &p_tbis)
1927 {
1928 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001929 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001930 else
1931 {
1932 out_flush();
1933 gui_mch_show_toolbar((toolbar_flags &
1934 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
1935 }
1936 }
1937#endif
1938
1939 // 'pastetoggle': translate key codes like in a mapping
1940 else if (varp == &p_pt)
1941 {
1942 if (*p_pt)
1943 {
Bram Moolenaar1e7b52a2019-10-13 16:59:08 +02001944 (void)replace_termcodes(p_pt, &p,
1945 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
Bram Moolenaardac13472019-09-16 21:06:21 +02001946 if (p != NULL)
1947 {
1948 if (new_value_alloced)
1949 free_string_option(p_pt);
1950 p_pt = p;
1951 new_value_alloced = TRUE;
1952 }
1953 }
1954 }
1955
1956 // 'backspace'
1957 else if (varp == &p_bs)
1958 {
1959 if (VIM_ISDIGIT(*p_bs))
1960 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02001961 if (*p_bs > '3' || p_bs[1] != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001962 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001963 }
1964 else if (check_opt_strings(p_bs, p_bs_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001965 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001966 }
1967 else if (varp == &p_bo)
1968 {
1969 if (opt_strings_flags(p_bo, p_bo_values, &bo_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001970 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001971 }
1972
1973 // 'tagcase'
1974 else if (gvarp == &p_tc)
1975 {
1976 unsigned int *flags;
1977
1978 if (opt_flags & OPT_LOCAL)
1979 {
1980 p = curbuf->b_p_tc;
1981 flags = &curbuf->b_tc_flags;
1982 }
1983 else
1984 {
1985 p = p_tc;
1986 flags = &tc_flags;
1987 }
1988
1989 if ((opt_flags & OPT_LOCAL) && *p == NUL)
1990 // make the local value empty: use the global value
1991 *flags = 0;
1992 else if (*p == NUL
1993 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001994 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001995 }
1996
1997 // 'casemap'
1998 else if (varp == &p_cmp)
1999 {
2000 if (opt_strings_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002001 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002002 }
2003
2004#ifdef FEAT_DIFF
2005 // 'diffopt'
2006 else if (varp == &p_dip)
2007 {
2008 if (diffopt_changed() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002009 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002010 }
2011#endif
2012
2013#ifdef FEAT_FOLDING
2014 // 'foldmethod'
2015 else if (gvarp == &curwin->w_allbuf_opt.wo_fdm)
2016 {
2017 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK
2018 || *curwin->w_p_fdm == NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002019 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002020 else
2021 {
2022 foldUpdateAll(curwin);
2023 if (foldmethodIsDiff(curwin))
2024 newFoldLevel();
2025 }
2026 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002027 // 'foldmarker'
2028 else if (gvarp == &curwin->w_allbuf_opt.wo_fmr)
2029 {
2030 p = vim_strchr(*varp, ',');
2031 if (p == NULL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002032 errmsg = e_comma_required;
Bram Moolenaardac13472019-09-16 21:06:21 +02002033 else if (p == *varp || p[1] == NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002034 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002035 else if (foldmethodIsMarker(curwin))
2036 foldUpdateAll(curwin);
2037 }
2038 // 'commentstring'
2039 else if (gvarp == &p_cms)
2040 {
2041 if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002042 errmsg = e_commentstring_must_be_empty_or_contain_str;
Bram Moolenaardac13472019-09-16 21:06:21 +02002043 }
2044 // 'foldopen'
2045 else if (varp == &p_fdo)
2046 {
2047 if (opt_strings_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002048 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002049 }
2050 // 'foldclose'
2051 else if (varp == &p_fcl)
2052 {
2053 if (check_opt_strings(p_fcl, p_fcl_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002054 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002055 }
2056 // 'foldignore'
2057 else if (gvarp == &curwin->w_allbuf_opt.wo_fdi)
2058 {
2059 if (foldmethodIsIndent(curwin))
2060 foldUpdateAll(curwin);
2061 }
2062#endif
2063
2064 // 'virtualedit'
Gary Johnson53ba05b2021-07-26 22:19:10 +02002065 else if (gvarp == &p_ve)
Bram Moolenaardac13472019-09-16 21:06:21 +02002066 {
Gary Johnson53ba05b2021-07-26 22:19:10 +02002067 char_u *ve = p_ve;
2068 unsigned int *flags = &ve_flags;
2069
2070 if (opt_flags & OPT_LOCAL)
Bram Moolenaardac13472019-09-16 21:06:21 +02002071 {
Gary Johnson51ad8502021-08-03 18:33:08 +02002072 ve = curwin->w_p_ve;
2073 flags = &curwin->w_ve_flags;
Gary Johnson53ba05b2021-07-26 22:19:10 +02002074 }
2075
2076 if ((opt_flags & OPT_LOCAL) && *ve == NUL)
2077 // make the local value empty: use the global value
2078 *flags = 0;
2079 else
2080 {
2081 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002082 errmsg = e_invalid_argument;
Gary Johnson53ba05b2021-07-26 22:19:10 +02002083 else if (STRCMP(p_ve, oldval) != 0)
2084 {
2085 // Recompute cursor position in case the new 've' setting
2086 // changes something.
2087 validate_virtcol();
2088 coladvance(curwin->w_virtcol);
2089 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002090 }
2091 }
2092
2093#if defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)
2094 else if (varp == &p_csqf)
2095 {
2096 if (p_csqf != NULL)
2097 {
2098 p = p_csqf;
2099 while (*p != NUL)
2100 {
2101 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
2102 || p[1] == NUL
2103 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
2104 || (p[2] != NUL && p[2] != ','))
2105 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002106 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002107 break;
2108 }
2109 else if (p[2] == NUL)
2110 break;
2111 else
2112 p += 3;
2113 }
2114 }
2115 }
2116#endif
2117
2118#ifdef FEAT_CINDENT
2119 // 'cinoptions'
2120 else if (gvarp == &p_cino)
2121 {
2122 // TODO: recognize errors
2123 parse_cino(curbuf);
2124 }
2125#endif
2126
2127#if defined(FEAT_RENDER_OPTIONS)
2128 // 'renderoptions'
2129 else if (varp == &p_rop)
2130 {
2131 if (!gui_mch_set_rendering_options(p_rop))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002132 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002133 }
2134#endif
2135
2136 else if (gvarp == &p_ft)
2137 {
2138 if (!valid_filetype(*varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002139 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002140 else
2141 {
2142 value_changed = STRCMP(oldval, *varp) != 0;
2143
2144 // Since we check the value, there is no need to set P_INSECURE,
2145 // even when the value comes from a modeline.
2146 *value_checked = TRUE;
2147 }
2148 }
2149
2150#ifdef FEAT_SYN_HL
2151 else if (gvarp == &p_syn)
2152 {
2153 if (!valid_filetype(*varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002154 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002155 else
2156 {
2157 value_changed = STRCMP(oldval, *varp) != 0;
2158
2159 // Since we check the value, there is no need to set P_INSECURE,
2160 // even when the value comes from a modeline.
2161 *value_checked = TRUE;
2162 }
2163 }
2164#endif
2165
2166#ifdef FEAT_TERMINAL
2167 // 'termwinkey'
2168 else if (varp == &curwin->w_p_twk)
2169 {
2170 if (*curwin->w_p_twk != NUL
2171 && string_to_key(curwin->w_p_twk, TRUE) == 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002172 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002173 }
2174 // 'termwinsize'
2175 else if (varp == &curwin->w_p_tws)
2176 {
2177 if (*curwin->w_p_tws != NUL)
2178 {
2179 p = skipdigits(curwin->w_p_tws);
2180 if (p == curwin->w_p_tws
2181 || (*p != 'x' && *p != '*')
2182 || *skipdigits(p + 1) != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002183 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002184 }
2185 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002186 // 'wincolor'
2187 else if (varp == &curwin->w_p_wcr)
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002188 term_update_wincolor(curwin);
Bram Moolenaardac13472019-09-16 21:06:21 +02002189# if defined(MSWIN)
2190 // 'termwintype'
2191 else if (varp == &p_twt)
2192 {
2193 if (check_opt_strings(*varp, p_twt_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002194 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002195 }
2196# endif
2197#endif
2198
2199#ifdef FEAT_VARTABS
2200 // 'varsofttabstop'
2201 else if (varp == &(curbuf->b_p_vsts))
2202 {
2203 char_u *cp;
2204
2205 if (!(*varp)[0] || ((*varp)[0] == '0' && !(*varp)[1]))
2206 {
2207 if (curbuf->b_p_vsts_array)
2208 {
2209 vim_free(curbuf->b_p_vsts_array);
2210 curbuf->b_p_vsts_array = 0;
2211 }
2212 }
2213 else
2214 {
2215 for (cp = *varp; *cp; ++cp)
2216 {
2217 if (vim_isdigit(*cp))
2218 continue;
2219 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
2220 continue;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002221 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002222 break;
2223 }
2224 if (errmsg == NULL)
2225 {
2226 int *oldarray = curbuf->b_p_vsts_array;
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002227 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Bram Moolenaardac13472019-09-16 21:06:21 +02002228 {
2229 if (oldarray)
2230 vim_free(oldarray);
2231 }
2232 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002233 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002234 }
2235 }
2236 }
2237
2238 // 'vartabstop'
2239 else if (varp == &(curbuf->b_p_vts))
2240 {
2241 char_u *cp;
2242
2243 if (!(*varp)[0] || ((*varp)[0] == '0' && !(*varp)[1]))
2244 {
2245 if (curbuf->b_p_vts_array)
2246 {
2247 vim_free(curbuf->b_p_vts_array);
2248 curbuf->b_p_vts_array = NULL;
2249 }
2250 }
2251 else
2252 {
2253 for (cp = *varp; *cp; ++cp)
2254 {
2255 if (vim_isdigit(*cp))
2256 continue;
2257 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
2258 continue;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002259 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002260 break;
2261 }
2262 if (errmsg == NULL)
2263 {
2264 int *oldarray = curbuf->b_p_vts_array;
2265
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002266 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Bram Moolenaardac13472019-09-16 21:06:21 +02002267 {
2268 vim_free(oldarray);
2269#ifdef FEAT_FOLDING
2270 if (foldmethodIsIndent(curwin))
2271 foldUpdateAll(curwin);
2272#endif
2273 }
2274 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002275 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002276 }
2277 }
2278 }
2279#endif
2280
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002281#ifdef FEAT_PROP_POPUP
Bram Moolenaardac13472019-09-16 21:06:21 +02002282 // 'previewpopup'
2283 else if (varp == &p_pvp)
2284 {
2285 if (parse_previewpopup(NULL) == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002286 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002287 }
2288# ifdef FEAT_QUICKFIX
2289 // 'completepopup'
2290 else if (varp == &p_cpp)
2291 {
2292 if (parse_completepopup(NULL) == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002293 errmsg = e_invalid_argument;
Bram Moolenaar447bfba2020-07-18 16:07:16 +02002294 else
2295 popup_close_info();
Bram Moolenaardac13472019-09-16 21:06:21 +02002296 }
2297# endif
2298#endif
2299
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002300#ifdef FEAT_EVAL
2301 // '*expr' options
2302 else if (
2303# ifdef FEAT_BEVAL
2304 varp == &p_bexpr ||
2305# endif
2306# ifdef FEAT_DIFF
2307 varp == &p_dex ||
2308# endif
2309# ifdef FEAT_FOLDING
2310 varp == &curwin->w_p_fde ||
Yegappan Lakshmanan27708e62021-12-26 21:54:43 +00002311 varp == &curwin->w_p_fdt ||
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002312# endif
2313 gvarp == &p_fex ||
2314# ifdef FEAT_FIND_ID
2315 gvarp == &p_inex ||
2316# endif
2317# ifdef FEAT_CINDENT
2318 gvarp == &p_inde ||
2319# endif
2320# ifdef FEAT_DIFF
2321 varp == &p_pex ||
2322# endif
2323# ifdef FEAT_POSTSCRIPT
2324 varp == &p_pexpr ||
2325# endif
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00002326 varp == &p_ccv)
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002327 {
2328 char_u **p_opt = NULL;
2329 char_u *name;
2330
2331 // If the option value starts with <SID> or s:, then replace that with
2332 // the script identifier.
2333# ifdef FEAT_BEVAL
2334 if (varp == &p_bexpr) // 'balloonexpr'
2335 p_opt = (opt_flags & OPT_LOCAL) ? &curbuf->b_p_bexpr : &p_bexpr;
2336# endif
2337# ifdef FEAT_DIFF
2338 if (varp == &p_dex) // 'diffexpr'
2339 p_opt = &p_dex;
2340# endif
2341# ifdef FEAT_FOLDING
Yegappan Lakshmanan27708e62021-12-26 21:54:43 +00002342 if (varp == &curwin->w_p_fde) // 'foldexpr'
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002343 p_opt = &curwin->w_p_fde;
Yegappan Lakshmanan27708e62021-12-26 21:54:43 +00002344 if (varp == &curwin->w_p_fdt) // 'foldtext'
2345 p_opt = &curwin->w_p_fdt;
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002346# endif
2347 if (gvarp == &p_fex) // 'formatexpr'
2348 p_opt = &curbuf->b_p_fex;
2349# ifdef FEAT_FIND_ID
2350 if (gvarp == &p_inex) // 'includeexpr'
2351 p_opt = &curbuf->b_p_inex;
2352# endif
2353# ifdef FEAT_CINDENT
2354 if (gvarp == &p_inde) // 'indentexpr'
2355 p_opt = &curbuf->b_p_inde;
2356# endif
2357# ifdef FEAT_DIFF
2358 if (varp == &p_pex) // 'patchexpr'
2359 p_opt = &p_pex;
2360# endif
2361# ifdef FEAT_POSTSCRIPT
2362 if (varp == &p_pexpr) // 'printexpr'
2363 p_opt = &p_pexpr;
2364# endif
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00002365 if (varp == &p_ccv) // 'charconvert'
2366 p_opt = &p_ccv;
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002367
2368 if (p_opt != NULL)
2369 {
2370 name = get_scriptlocal_funcname(*p_opt);
2371 if (name != NULL)
2372 {
2373 if (new_value_alloced)
2374 free_string_option(*p_opt);
2375 *p_opt = name;
2376 new_value_alloced = TRUE;
2377 }
2378 }
2379
2380# ifdef FEAT_FOLDING
2381 if (varp == &curwin->w_p_fde && foldmethodIsExpr(curwin))
2382 foldUpdateAll(curwin);
2383# endif
2384 }
2385#endif
2386
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002387#ifdef FEAT_COMPL_FUNC
2388 // 'completefunc'
2389 else if (gvarp == &p_cfu)
2390 {
2391 if (set_completefunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002392 errmsg = e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002393 }
2394
2395 // 'omnifunc'
2396 else if (gvarp == &p_ofu)
2397 {
2398 if (set_omnifunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002399 errmsg = e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002400 }
2401
2402 // 'thesaurusfunc'
2403 else if (gvarp == &p_tsrfu)
2404 {
2405 if (set_thesaurusfunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002406 errmsg = e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002407 }
2408#endif
2409
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00002410#if defined(FEAT_EVAL) && \
2411 (defined(FEAT_XIM) || defined(IME_WITHOUT_XIM) || defined(VIMDLL))
2412 // 'imactivatefunc'
2413 else if (gvarp == &p_imaf)
2414 {
2415 if (set_imactivatefunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002416 errmsg = e_invalid_argument;
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00002417 }
2418
2419 // 'imstatusfunc'
2420 else if (gvarp == &p_imsf)
2421 {
2422 if (set_imstatusfunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002423 errmsg = e_invalid_argument;
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00002424 }
2425#endif
2426
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00002427 // 'operatorfunc'
2428 else if (varp == &p_opfunc)
2429 {
2430 if (set_operatorfunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002431 errmsg = e_invalid_argument;
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00002432 }
2433
Bram Moolenaard43906d2020-07-20 21:31:32 +02002434#ifdef FEAT_QUICKFIX
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00002435 // 'quickfixtextfunc'
Bram Moolenaard43906d2020-07-20 21:31:32 +02002436 else if (varp == &p_qftf)
2437 {
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00002438 if (qf_process_qftf_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002439 errmsg = e_invalid_argument;
Bram Moolenaard43906d2020-07-20 21:31:32 +02002440 }
2441#endif
2442
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00002443#ifdef FEAT_EVAL
2444 // 'tagfunc'
2445 else if (gvarp == &p_tfu)
2446 {
2447 if (set_tagfunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002448 errmsg = e_invalid_argument;
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00002449 }
2450#endif
2451
Bram Moolenaardac13472019-09-16 21:06:21 +02002452 // Options that are a list of flags.
2453 else
2454 {
2455 p = NULL;
2456 if (varp == &p_ww) // 'whichwrap'
2457 p = (char_u *)WW_ALL;
2458 if (varp == &p_shm) // 'shortmess'
2459 p = (char_u *)SHM_ALL;
2460 else if (varp == &(p_cpo)) // 'cpoptions'
2461 p = (char_u *)CPO_ALL;
2462 else if (varp == &(curbuf->b_p_fo)) // 'formatoptions'
2463 p = (char_u *)FO_ALL;
2464#ifdef FEAT_CONCEAL
2465 else if (varp == &curwin->w_p_cocu) // 'concealcursor'
2466 p = (char_u *)COCU_ALL;
2467#endif
2468 else if (varp == &p_mouse) // 'mouse'
2469 {
Bram Moolenaardac13472019-09-16 21:06:21 +02002470 p = (char_u *)MOUSE_ALL;
Bram Moolenaardac13472019-09-16 21:06:21 +02002471 }
2472#if defined(FEAT_GUI)
2473 else if (varp == &p_go) // 'guioptions'
2474 p = (char_u *)GO_ALL;
2475#endif
2476 if (p != NULL)
2477 {
2478 for (s = *varp; *s; ++s)
2479 if (vim_strchr(p, *s) == NULL)
2480 {
2481 errmsg = illegal_char(errbuf, *s);
2482 break;
2483 }
2484 }
2485 }
2486
2487 // If error detected, restore the previous value.
2488 if (errmsg != NULL)
2489 {
2490 if (new_value_alloced)
2491 free_string_option(*varp);
2492 *varp = oldval;
2493 // When resetting some values, need to act on it.
2494 if (did_chartab)
2495 (void)init_chartab();
2496 if (varp == &p_hl)
2497 (void)highlight_changed();
2498 }
2499 else
2500 {
2501#ifdef FEAT_EVAL
2502 // Remember where the option was set.
2503 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
2504#endif
2505 // Free string options that are in allocated memory.
2506 // Use "free_oldval", because recursiveness may change the flags under
2507 // our fingers (esp. init_highlight()).
2508 if (free_oldval)
2509 free_string_option(oldval);
2510 if (new_value_alloced)
2511 set_option_flag(opt_idx, P_ALLOCED);
2512 else
2513 clear_option_flag(opt_idx, P_ALLOCED);
2514
2515 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
2516 && is_global_local_option(opt_idx))
2517 {
2518 // global option with local value set to use global value; free
2519 // the local value and make it empty
2520 p = get_option_varp_scope(opt_idx, OPT_LOCAL);
2521 free_string_option(*(char_u **)p);
2522 *(char_u **)p = empty_option;
2523 }
2524
2525 // May set global value for local option.
2526 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
2527 set_string_option_global(opt_idx, varp);
2528
2529 // Trigger the autocommand only after setting the flags.
2530#ifdef FEAT_SYN_HL
2531 // When 'syntax' is set, load the syntax of that name
2532 if (varp == &(curbuf->b_p_syn))
2533 {
2534 static int syn_recursive = 0;
2535
2536 ++syn_recursive;
2537 // Only pass TRUE for "force" when the value changed or not used
2538 // recursively, to avoid endless recurrence.
2539 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
2540 value_changed || syn_recursive == 1, curbuf);
2541 curbuf->b_flags |= BF_SYN_SET;
2542 --syn_recursive;
2543 }
2544#endif
2545 else if (varp == &(curbuf->b_p_ft))
2546 {
2547 // 'filetype' is set, trigger the FileType autocommand.
2548 // Skip this when called from a modeline and the filetype was
2549 // already set to this value.
2550 if (!(opt_flags & OPT_MODELINE) || value_changed)
2551 {
2552 static int ft_recursive = 0;
2553 int secure_save = secure;
2554
2555 // Reset the secure flag, since the value of 'filetype' has
2556 // been checked to be safe.
2557 secure = 0;
2558
2559 ++ft_recursive;
2560 did_filetype = TRUE;
2561 // Only pass TRUE for "force" when the value changed or not
2562 // used recursively, to avoid endless recurrence.
2563 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2564 value_changed || ft_recursive == 1, curbuf);
2565 --ft_recursive;
2566 // Just in case the old "curbuf" is now invalid.
2567 if (varp != &(curbuf->b_p_ft))
2568 varp = NULL;
2569
2570 secure = secure_save;
2571 }
2572 }
2573#ifdef FEAT_SPELL
2574 if (varp == &(curwin->w_s->b_p_spl))
2575 {
2576 char_u fname[200];
2577 char_u *q = curwin->w_s->b_p_spl;
2578
2579 // Skip the first name if it is "cjk".
2580 if (STRNCMP(q, "cjk,", 4) == 0)
2581 q += 4;
2582
2583 // Source the spell/LANG.vim in 'runtimepath'.
2584 // They could set 'spellcapcheck' depending on the language.
2585 // Use the first name in 'spelllang' up to '_region' or
2586 // '.encoding'.
2587 for (p = q; *p != NUL; ++p)
2588 if (!ASCII_ISALNUM(*p) && *p != '-')
2589 break;
2590 if (p > q)
2591 {
2592 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
2593 (int)(p - q), q);
2594 source_runtime(fname, DIP_ALL);
2595 }
2596 }
2597#endif
2598 }
2599
Bram Moolenaardac13472019-09-16 21:06:21 +02002600 if (varp == &p_mouse)
2601 {
Bram Moolenaardac13472019-09-16 21:06:21 +02002602 if (*p_mouse == NUL)
2603 mch_setmouse(FALSE); // switch mouse off
2604 else
Bram Moolenaardac13472019-09-16 21:06:21 +02002605 setmouse(); // in case 'mouse' changed
2606 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002607
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002608#if defined(FEAT_LUA) || defined(PROTO)
2609 if (varp == &p_rtp)
2610 update_package_paths_in_lua();
2611#endif
2612
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00002613#if defined(FEAT_LINEBREAK)
2614 // Changing Formatlistpattern when briopt includes the list setting:
2615 // redraw
2616 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
2617 && curwin->w_briopt_list)
2618 redraw_all_later(NOT_VALID);
2619#endif
2620
Bram Moolenaardac13472019-09-16 21:06:21 +02002621 if (curwin->w_curswant != MAXCOL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002622 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02002623 curwin->w_set_curswant = TRUE;
2624
Bram Moolenaar37294bd2021-03-10 13:40:08 +01002625 if ((opt_flags & OPT_NO_REDRAW) == 0)
2626 {
Bram Moolenaardac13472019-09-16 21:06:21 +02002627#ifdef FEAT_GUI
Bram Moolenaar37294bd2021-03-10 13:40:08 +01002628 // check redraw when it's not a GUI option or the GUI is active.
2629 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02002630#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01002631 check_redraw(get_option_flags(opt_idx));
2632 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002633
2634#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2635 if (did_swaptcap)
2636 {
2637 set_termname((char_u *)"win32");
2638 init_highlight(TRUE, FALSE);
2639 }
2640#endif
2641
2642 return errmsg;
2643}
2644
2645/*
2646 * Check an option that can be a range of string values.
2647 *
2648 * Return OK for correct value, FAIL otherwise.
2649 * Empty is always OK.
2650 */
2651 static int
2652check_opt_strings(
2653 char_u *val,
2654 char **values,
2655 int list) // when TRUE: accept a list of values
2656{
2657 return opt_strings_flags(val, values, NULL, list);
2658}
2659
2660/*
2661 * Handle an option that can be a range of string values.
2662 * Set a flag in "*flagp" for each string present.
2663 *
2664 * Return OK for correct value, FAIL otherwise.
2665 * Empty is always OK.
2666 */
2667 static int
2668opt_strings_flags(
2669 char_u *val, // new value
2670 char **values, // array of valid string values
2671 unsigned *flagp,
2672 int list) // when TRUE: accept a list of values
2673{
2674 int i;
2675 int len;
2676 unsigned new_flags = 0;
2677
2678 while (*val)
2679 {
2680 for (i = 0; ; ++i)
2681 {
2682 if (values[i] == NULL) // val not found in values[]
2683 return FAIL;
2684
2685 len = (int)STRLEN(values[i]);
2686 if (STRNCMP(values[i], val, len) == 0
2687 && ((list && val[len] == ',') || val[len] == NUL))
2688 {
2689 val += len + (val[len] == ',');
2690 new_flags |= (1 << i);
2691 break; // check next item in val list
2692 }
2693 }
2694 }
2695 if (flagp != NULL)
2696 *flagp = new_flags;
2697
2698 return OK;
2699}
2700
2701/*
2702 * return OK if "p" is a valid fileformat name, FAIL otherwise.
2703 */
2704 int
2705check_ff_value(char_u *p)
2706{
2707 return check_opt_strings(p, p_ff_values, FALSE);
2708}