blob: 73a57703b82e4cf96ab9a156bddf4a172fff87af [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
218#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
219 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);
245#ifdef FEAT_TEXTOBJ
246 check_string_option(&buf->b_p_qe);
247#endif
248#ifdef FEAT_SYN_HL
249 check_string_option(&buf->b_p_syn);
250 check_string_option(&buf->b_s.b_syn_isk);
251#endif
252#ifdef FEAT_SPELL
253 check_string_option(&buf->b_s.b_p_spc);
254 check_string_option(&buf->b_s.b_p_spf);
255 check_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +0200256 check_string_option(&buf->b_s.b_p_spo);
Bram Moolenaardac13472019-09-16 21:06:21 +0200257#endif
258#ifdef FEAT_SEARCHPATH
259 check_string_option(&buf->b_p_sua);
260#endif
261#ifdef FEAT_CINDENT
262 check_string_option(&buf->b_p_cink);
263 check_string_option(&buf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +0100264 check_string_option(&buf->b_p_cinsd);
Bram Moolenaardac13472019-09-16 21:06:21 +0200265 parse_cino(buf);
266#endif
267 check_string_option(&buf->b_p_ft);
268#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
269 check_string_option(&buf->b_p_cinw);
270#endif
271 check_string_option(&buf->b_p_cpt);
272#ifdef FEAT_COMPL_FUNC
273 check_string_option(&buf->b_p_cfu);
274 check_string_option(&buf->b_p_ofu);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +0100275 check_string_option(&buf->b_p_tsrfu);
Bram Moolenaardac13472019-09-16 21:06:21 +0200276#endif
277#ifdef FEAT_EVAL
278 check_string_option(&buf->b_p_tfu);
279#endif
280#ifdef FEAT_KEYMAP
281 check_string_option(&buf->b_p_keymap);
282#endif
283#ifdef FEAT_QUICKFIX
284 check_string_option(&buf->b_p_gp);
285 check_string_option(&buf->b_p_mp);
286 check_string_option(&buf->b_p_efm);
287#endif
288 check_string_option(&buf->b_p_ep);
289 check_string_option(&buf->b_p_path);
290 check_string_option(&buf->b_p_tags);
291 check_string_option(&buf->b_p_tc);
292 check_string_option(&buf->b_p_dict);
293 check_string_option(&buf->b_p_tsr);
294#ifdef FEAT_LISP
295 check_string_option(&buf->b_p_lw);
296#endif
297 check_string_option(&buf->b_p_bkc);
298 check_string_option(&buf->b_p_menc);
299#ifdef FEAT_VARTABS
300 check_string_option(&buf->b_p_vsts);
301 check_string_option(&buf->b_p_vts);
302#endif
303}
304
305/*
306 * Free the string allocated for an option.
307 * Checks for the string being empty_option. This may happen if we're out of
308 * memory, vim_strsave() returned NULL, which was replaced by empty_option by
309 * check_options().
310 * Does NOT check for P_ALLOCED flag!
311 */
312 void
313free_string_option(char_u *p)
314{
315 if (p != empty_option)
316 vim_free(p);
317}
318
319 void
320clear_string_option(char_u **pp)
321{
322 if (*pp != empty_option)
323 vim_free(*pp);
324 *pp = empty_option;
325}
326
327 void
328check_string_option(char_u **pp)
329{
330 if (*pp == NULL)
331 *pp = empty_option;
332}
333
334/*
335 * Set global value for string option when it's a local option.
336 */
337 static void
338set_string_option_global(
339 int opt_idx, // option index
340 char_u **varp) // pointer to option variable
341{
342 char_u **p, *s;
343
344 // the global value is always allocated
345 if (is_window_local_option(opt_idx))
346 p = (char_u **)GLOBAL_WO(varp);
347 else
348 p = (char_u **)get_option_var(opt_idx);
349 if (!is_global_option(opt_idx)
350 && p != varp
351 && (s = vim_strsave(*varp)) != NULL)
352 {
353 free_string_option(*p);
354 *p = s;
355 }
356}
357
358/*
359 * Set a string option to a new value (without checking the effect).
360 * The string is copied into allocated memory.
361 * if ("opt_idx" == -1) "name" is used, otherwise "opt_idx" is used.
362 * When "set_sid" is zero set the scriptID to current_sctx.sc_sid. When
363 * "set_sid" is SID_NONE don't set the scriptID. Otherwise set the scriptID to
364 * "set_sid".
365 */
366 void
367set_string_option_direct(
368 char_u *name,
369 int opt_idx,
370 char_u *val,
371 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
372 int set_sid UNUSED)
373{
374 char_u *s;
375 char_u **varp;
376 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
377 int idx = opt_idx;
378
379 if (idx == -1) // use name
380 {
381 idx = findoption(name);
382 if (idx < 0) // not found (should not happen)
383 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000384 semsg(_(e_internal_error_str), "set_string_option_direct()");
Bram Moolenaardac13472019-09-16 21:06:21 +0200385 siemsg(_("For option %s"), name);
386 return;
387 }
388 }
389
390 if (is_hidden_option(idx)) // can't set hidden option
391 return;
392
393 s = vim_strsave(val);
394 if (s != NULL)
395 {
396 varp = (char_u **)get_option_varp_scope(idx,
397 both ? OPT_LOCAL : opt_flags);
398 if ((opt_flags & OPT_FREE) && (get_option_flags(idx) & P_ALLOCED))
399 free_string_option(*varp);
400 *varp = s;
401
402 // For buffer/window local option may also set the global value.
403 if (both)
404 set_string_option_global(idx, varp);
405
406 set_option_flag(idx, P_ALLOCED);
407
408 // When setting both values of a global option with a local value,
409 // make the local value empty, so that the global value is used.
410 if (is_global_local_option(idx) && both)
411 {
412 free_string_option(*varp);
413 *varp = empty_option;
414 }
415# ifdef FEAT_EVAL
416 if (set_sid != SID_NONE)
417 {
418 sctx_T script_ctx;
419
420 if (set_sid == 0)
421 script_ctx = current_sctx;
422 else
423 {
424 script_ctx.sc_sid = set_sid;
425 script_ctx.sc_seq = 0;
426 script_ctx.sc_lnum = 0;
427 script_ctx.sc_version = 1;
428 }
429 set_option_sctx_idx(idx, opt_flags, script_ctx);
430 }
431# endif
432 }
433}
434
435/*
436 * Like set_string_option_direct(), but for a window-local option in "wp".
437 * Blocks autocommands to avoid the old curwin becoming invalid.
438 */
439 void
440set_string_option_direct_in_win(
441 win_T *wp,
442 char_u *name,
443 int opt_idx,
444 char_u *val,
445 int opt_flags,
446 int set_sid)
447{
448 win_T *save_curwin = curwin;
449
450 block_autocmds();
451 curwin = wp;
452 curbuf = curwin->w_buffer;
453 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
454 curwin = save_curwin;
455 curbuf = curwin->w_buffer;
456 unblock_autocmds();
457}
458
Dominique Pelle748b3082022-01-08 12:41:16 +0000459#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200460/*
461 * Like set_string_option_direct(), but for a buffer-local option in "buf".
462 * Blocks autocommands to avoid the old curbuf becoming invalid.
463 */
464 void
465set_string_option_direct_in_buf(
466 buf_T *buf,
467 char_u *name,
468 int opt_idx,
469 char_u *val,
470 int opt_flags,
471 int set_sid)
472{
473 buf_T *save_curbuf = curbuf;
474
475 block_autocmds();
476 curbuf = buf;
477 curwin->w_buffer = curbuf;
478 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
479 curbuf = save_curbuf;
480 curwin->w_buffer = curbuf;
481 unblock_autocmds();
482}
Dominique Pelle748b3082022-01-08 12:41:16 +0000483#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200484
485/*
486 * Set a string option to a new value, and handle the effects.
487 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100488 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaardac13472019-09-16 21:06:21 +0200489 */
490 char *
491set_string_option(
492 int opt_idx,
493 char_u *value,
494 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
495{
496 char_u *s;
497 char_u **varp;
498 char_u *oldval;
499#if defined(FEAT_EVAL)
500 char_u *oldval_l = NULL;
501 char_u *oldval_g = NULL;
502 char_u *saved_oldval = NULL;
503 char_u *saved_oldval_l = NULL;
504 char_u *saved_oldval_g = NULL;
505 char_u *saved_newval = NULL;
506#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100507 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +0200508 int value_checked = FALSE;
509
510 if (is_hidden_option(opt_idx)) // don't set hidden option
511 return NULL;
512
Bram Moolenaar7f009df2020-03-16 20:27:38 +0100513 s = vim_strsave(value == NULL ? (char_u *)"" : value);
Bram Moolenaardac13472019-09-16 21:06:21 +0200514 if (s != NULL)
515 {
516 varp = (char_u **)get_option_varp_scope(opt_idx,
517 (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
518 ? (is_global_local_option(opt_idx)
519 ? OPT_GLOBAL : OPT_LOCAL)
520 : opt_flags);
521 oldval = *varp;
522#if defined(FEAT_EVAL)
523 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
524 {
525 oldval_l = *(char_u **)get_option_varp_scope(opt_idx, OPT_LOCAL);
526 oldval_g = *(char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
527 }
528#endif
529 *varp = s;
530
531#if defined(FEAT_EVAL)
532 if (!starting
533# ifdef FEAT_CRYPT
534 && !is_crypt_key_option(opt_idx)
535# endif
536 )
537 {
538 if (oldval_l != NULL)
539 saved_oldval_l = vim_strsave(oldval_l);
540 if (oldval_g != NULL)
541 saved_oldval_g = vim_strsave(oldval_g);
542 saved_oldval = vim_strsave(oldval);
543 saved_newval = vim_strsave(s);
544 }
545#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100546 if ((errmsg = did_set_string_option(opt_idx, varp, TRUE, oldval, NULL,
Bram Moolenaardac13472019-09-16 21:06:21 +0200547 opt_flags, &value_checked)) == NULL)
548 did_set_option(opt_idx, opt_flags, TRUE, value_checked);
549
550#if defined(FEAT_EVAL)
551 // call autocommand after handling side effects
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100552 if (errmsg == NULL)
Bram Moolenaardac13472019-09-16 21:06:21 +0200553 trigger_optionsset_string(opt_idx, opt_flags,
554 saved_oldval, saved_oldval_l,
555 saved_oldval_g, saved_newval);
556 vim_free(saved_oldval);
557 vim_free(saved_oldval_l);
558 vim_free(saved_oldval_g);
559 vim_free(saved_newval);
560#endif
561 }
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100562 return errmsg;
Bram Moolenaardac13472019-09-16 21:06:21 +0200563}
564
565/*
566 * Return TRUE if "val" is a valid 'filetype' name.
567 * Also used for 'syntax' and 'keymap'.
568 */
569 static int
570valid_filetype(char_u *val)
571{
572 return valid_name(val, ".-_");
573}
574
575#ifdef FEAT_STL_OPT
576/*
577 * Check validity of options with the 'statusline' format.
zeertzjq5dc294a2022-04-15 13:17:57 +0100578 * Return an untranslated error message or NULL.
Bram Moolenaardac13472019-09-16 21:06:21 +0200579 */
580 static char *
581check_stl_option(char_u *s)
582{
Bram Moolenaardac13472019-09-16 21:06:21 +0200583 int groupdepth = 0;
584 static char errbuf[80];
585
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100586 while (*s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200587 {
588 // Check for valid keys after % sequences
589 while (*s && *s != '%')
590 s++;
591 if (!*s)
592 break;
593 s++;
Bram Moolenaardac13472019-09-16 21:06:21 +0200594 if (*s == '%' || *s == STL_TRUNCMARK || *s == STL_MIDDLEMARK)
595 {
596 s++;
597 continue;
598 }
599 if (*s == ')')
600 {
601 s++;
602 if (--groupdepth < 0)
603 break;
604 continue;
605 }
606 if (*s == '-')
607 s++;
608 while (VIM_ISDIGIT(*s))
609 s++;
610 if (*s == STL_USER_HL)
611 continue;
612 if (*s == '.')
613 {
614 s++;
615 while (*s && VIM_ISDIGIT(*s))
616 s++;
617 }
618 if (*s == '(')
619 {
620 groupdepth++;
621 continue;
622 }
623 if (vim_strchr(STL_ALL, *s) == NULL)
624 {
625 return illegal_char(errbuf, *s);
626 }
627 if (*s == '{')
628 {
zeertzjq5dc294a2022-04-15 13:17:57 +0100629 int reevaluate = (*++s == '%');
shadmansaleh30e3de22021-05-15 17:23:28 +0200630
zeertzjq5dc294a2022-04-15 13:17:57 +0100631 if (reevaluate && *++s == '}')
632 // "}" is not allowed immediately after "%{%"
633 return illegal_char(errbuf, '}');
shadmansaleh30e3de22021-05-15 17:23:28 +0200634 while ((*s != '}' || (reevaluate && s[-1] != '%')) && *s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200635 s++;
636 if (*s != '}')
zeertzjq5dc294a2022-04-15 13:17:57 +0100637 return e_unclosed_expression_sequence;
Bram Moolenaardac13472019-09-16 21:06:21 +0200638 }
639 }
Bram Moolenaardac13472019-09-16 21:06:21 +0200640 if (groupdepth != 0)
zeertzjq5dc294a2022-04-15 13:17:57 +0100641 return e_unbalanced_groups;
Bram Moolenaardac13472019-09-16 21:06:21 +0200642 return NULL;
643}
644#endif
645
646/*
647 * Handle string options that need some action to perform when changed.
LemonBoy77142312022-04-15 20:50:46 +0100648 * Returns NULL for success, or an unstranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +0200649 */
650 char *
651did_set_string_option(
652 int opt_idx, // index in options[] table
653 char_u **varp, // pointer to the option variable
654 int new_value_alloced, // new value was allocated
655 char_u *oldval, // previous value of the option
656 char *errbuf, // buffer for errors, or NULL
657 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
658 int *value_checked) // value was checked to be save, no
659 // need to set P_INSECURE
660{
661 char *errmsg = NULL;
662 char_u *s, *p;
663 int did_chartab = FALSE;
664 char_u **gvarp;
665 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
666#ifdef FEAT_GUI
667 // set when changing an option that only requires a redraw in the GUI
668 int redraw_gui_only = FALSE;
669#endif
670 int value_changed = FALSE;
671#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
672 int did_swaptcap = FALSE;
673#endif
674
675 // Get the global option to compare with, otherwise we would have to check
676 // two values for all local options.
677 gvarp = (char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
678
679 // Disallow changing some options from secure mode
680 if ((secure
681#ifdef HAVE_SANDBOX
682 || sandbox != 0
683#endif
684 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +0000685 errmsg = e_not_allowed_here;
Bram Moolenaardac13472019-09-16 21:06:21 +0200686
687 // Check for a "normal" directory or file name in some options. Disallow a
688 // path separator (slash and/or backslash), wildcards and characters that
689 // are often illegal in a file name. Be more permissive if "secure" is off.
690 else if (((get_option_flags(opt_idx) & P_NFNAME)
691 && vim_strpbrk(*varp, (char_u *)(secure
692 ? "/\\*?[|;&<>\r\n" : "/\\*?[<>\r\n")) != NULL)
693 || ((get_option_flags(opt_idx) & P_NDNAME)
694 && vim_strpbrk(*varp, (char_u *)"*?[|;&<>\r\n") != NULL))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000695 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200696
697 // 'term'
698 else if (varp == &T_NAME)
699 {
700 if (T_NAME[0] == NUL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000701 errmsg = e_cannot_set_term_to_empty_string;
Bram Moolenaardac13472019-09-16 21:06:21 +0200702#ifdef FEAT_GUI
Bram Moolenaar5daa9112021-02-01 18:39:47 +0100703 else if (gui.in_use)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000704 errmsg = e_cannot_change_term_in_GUI;
Bram Moolenaardac13472019-09-16 21:06:21 +0200705 else if (term_is_gui(T_NAME))
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000706 errmsg = e_use_gui_to_start_GUI;
Bram Moolenaardac13472019-09-16 21:06:21 +0200707#endif
708 else if (set_termname(T_NAME) == FAIL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000709 errmsg = e_not_found_in_termcap;
Bram Moolenaardac13472019-09-16 21:06:21 +0200710 else
711 {
712 // Screen colors may have changed.
713 redraw_later_clear();
714
715 // Both 'term' and 'ttytype' point to T_NAME, only set the
716 // P_ALLOCED flag on 'term'.
717 opt_idx = findoption((char_u *)"term");
718 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
719 }
720 }
721
722 // 'backupcopy'
723 else if (gvarp == &p_bkc)
724 {
725 char_u *bkc = p_bkc;
726 unsigned int *flags = &bkc_flags;
727
728 if (opt_flags & OPT_LOCAL)
729 {
730 bkc = curbuf->b_p_bkc;
731 flags = &curbuf->b_bkc_flags;
732 }
733
734 if ((opt_flags & OPT_LOCAL) && *bkc == NUL)
735 // make the local value empty: use the global value
736 *flags = 0;
737 else
738 {
739 if (opt_strings_flags(bkc, p_bkc_values, flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000740 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200741 if ((((int)*flags & BKC_AUTO) != 0)
742 + (((int)*flags & BKC_YES) != 0)
743 + (((int)*flags & BKC_NO) != 0) != 1)
744 {
745 // Must have exactly one of "auto", "yes" and "no".
746 (void)opt_strings_flags(oldval, p_bkc_values, flags, TRUE);
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000747 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200748 }
749 }
750 }
751
752 // 'backupext' and 'patchmode'
753 else if (varp == &p_bex || varp == &p_pm)
754 {
755 if (STRCMP(*p_bex == '.' ? p_bex + 1 : p_bex,
756 *p_pm == '.' ? p_pm + 1 : p_pm) == 0)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100757 errmsg = e_backupext_and_patchmode_are_equal;
Bram Moolenaardac13472019-09-16 21:06:21 +0200758 }
759#ifdef FEAT_LINEBREAK
760 // 'breakindentopt'
761 else if (varp == &curwin->w_p_briopt)
762 {
763 if (briopt_check(curwin) == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000764 errmsg = e_invalid_argument;
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000765 // list setting requires a redraw
766 if (curwin->w_briopt_list)
767 redraw_all_later(NOT_VALID);
Bram Moolenaardac13472019-09-16 21:06:21 +0200768 }
769#endif
770
771 // 'isident', 'iskeyword', 'isprint or 'isfname' option: refill g_chartab[]
772 // If the new option is invalid, use old value. 'lisp' option: refill
773 // g_chartab[] for '-' char
774 else if ( varp == &p_isi
775 || varp == &(curbuf->b_p_isk)
776 || varp == &p_isp
777 || varp == &p_isf)
778 {
779 if (init_chartab() == FAIL)
780 {
781 did_chartab = TRUE; // need to restore it below
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000782 errmsg = e_invalid_argument; // error in value
Bram Moolenaardac13472019-09-16 21:06:21 +0200783 }
784 }
785
786 // 'helpfile'
787 else if (varp == &p_hf)
788 {
789 // May compute new values for $VIM and $VIMRUNTIME
790 if (didset_vim)
LemonBoy77142312022-04-15 20:50:46 +0100791 vim_unsetenv_ext((char_u *)"VIM");
Bram Moolenaardac13472019-09-16 21:06:21 +0200792 if (didset_vimruntime)
LemonBoy77142312022-04-15 20:50:46 +0100793 vim_unsetenv_ext((char_u *)"VIMRUNTIME");
Bram Moolenaardac13472019-09-16 21:06:21 +0200794 }
795
796#ifdef FEAT_SYN_HL
797 // 'cursorlineopt'
798 else if (varp == &curwin->w_p_culopt
799 || gvarp == &curwin->w_allbuf_opt.wo_culopt)
800 {
801 if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000802 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200803 }
804
805 // 'colorcolumn'
806 else if (varp == &curwin->w_p_cc)
807 errmsg = check_colorcolumn(curwin);
808#endif
809
810#ifdef FEAT_MULTI_LANG
811 // 'helplang'
812 else if (varp == &p_hlg)
813 {
814 // Check for "", "ab", "ab,cd", etc.
815 for (s = p_hlg; *s != NUL; s += 3)
816 {
817 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
818 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000819 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200820 break;
821 }
822 if (s[2] == NUL)
823 break;
824 }
825 }
826#endif
827
828 // 'highlight'
829 else if (varp == &p_hl)
830 {
831 if (highlight_changed() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000832 errmsg = e_invalid_argument; // invalid flags
Bram Moolenaardac13472019-09-16 21:06:21 +0200833 }
834
835 // 'nrformats'
836 else if (gvarp == &p_nf)
837 {
838 if (check_opt_strings(*varp, p_nf_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000839 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200840 }
841
842#ifdef FEAT_SESSION
843 // 'sessionoptions'
844 else if (varp == &p_ssop)
845 {
846 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000847 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200848 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
849 {
850 // Don't allow both "sesdir" and "curdir".
851 (void)opt_strings_flags(oldval, p_ssop_values, &ssop_flags, TRUE);
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000852 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200853 }
854 }
855 // 'viewoptions'
856 else if (varp == &p_vop)
857 {
858 if (opt_strings_flags(p_vop, p_ssop_values, &vop_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000859 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200860 }
861#endif
862
863 // 'scrollopt'
864 else if (varp == &p_sbo)
865 {
866 if (check_opt_strings(p_sbo, p_scbopt_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000867 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200868 }
869
870 // 'ambiwidth'
871 else if (varp == &p_ambw || varp == &p_emoji)
872 {
873 if (check_opt_strings(p_ambw, p_ambw_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000874 errmsg = e_invalid_argument;
Bram Moolenaareed9d462021-02-15 20:38:25 +0100875 else if (set_chars_option(curwin, &p_fcs) != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100876 errmsg = e_conflicts_with_value_of_fillchars;
Bram Moolenaareed9d462021-02-15 20:38:25 +0100877 else
878 {
879 tabpage_T *tp;
880 win_T *wp;
881
882 FOR_ALL_TAB_WINDOWS(tp, wp)
883 {
884 if (set_chars_option(wp, &wp->w_p_lcs) != NULL)
885 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100886 errmsg = e_conflicts_with_value_of_listchars;
Bram Moolenaareed9d462021-02-15 20:38:25 +0100887 goto ambw_end;
888 }
889 }
890 }
891ambw_end:
892 {}
Bram Moolenaardac13472019-09-16 21:06:21 +0200893 }
894
895 // 'background'
896 else if (varp == &p_bg)
897 {
898 if (check_opt_strings(p_bg, p_bg_values, FALSE) == OK)
899 {
900#ifdef FEAT_EVAL
901 int dark = (*p_bg == 'd');
902#endif
903
904 init_highlight(FALSE, FALSE);
905
906#ifdef FEAT_EVAL
907 if (dark != (*p_bg == 'd')
908 && get_var_value((char_u *)"g:colors_name") != NULL)
909 {
910 // The color scheme must have set 'background' back to another
911 // value, that's not what we want here. Disable the color
912 // scheme and set the colors again.
913 do_unlet((char_u *)"g:colors_name", TRUE);
914 free_string_option(p_bg);
915 p_bg = vim_strsave((char_u *)(dark ? "dark" : "light"));
916 check_string_option(&p_bg);
917 init_highlight(FALSE, FALSE);
918 }
919#endif
Bram Moolenaarad431992021-05-03 20:40:38 +0200920#ifdef FEAT_TERMINAL
921 term_update_colors_all();
922#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200923 }
924 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000925 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200926 }
927
928 // 'wildmode'
929 else if (varp == &p_wim)
930 {
931 if (check_opt_wim() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000932 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200933 }
934
935 // 'wildoptions'
936 else if (varp == &p_wop)
937 {
938 if (check_opt_strings(p_wop, p_wop_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000939 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200940 }
941
942#ifdef FEAT_WAK
943 // 'winaltkeys'
944 else if (varp == &p_wak)
945 {
946 if (*p_wak == NUL
947 || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000948 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200949# ifdef FEAT_MENU
ichizok02560422022-04-05 14:18:44 +0100950# if defined(FEAT_GUI_MOTIF)
Bram Moolenaardac13472019-09-16 21:06:21 +0200951 else if (gui.in_use)
952 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
ichizok02560422022-04-05 14:18:44 +0100953# elif defined(FEAT_GUI_GTK)
Bram Moolenaardac13472019-09-16 21:06:21 +0200954 else if (gui.in_use)
955 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
Bram Moolenaardac13472019-09-16 21:06:21 +0200956# endif
957# endif
958 }
959#endif
960
961 // 'eventignore'
962 else if (varp == &p_ei)
963 {
964 if (check_ei() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000965 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200966 }
967
968 // 'encoding', 'fileencoding', 'termencoding' and 'makeencoding'
969 else if (varp == &p_enc || gvarp == &p_fenc || varp == &p_tenc
970 || gvarp == &p_menc)
971 {
972 if (gvarp == &p_fenc)
973 {
974 if (!curbuf->b_p_ma && opt_flags != OPT_GLOBAL)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200975 errmsg = e_cannot_make_changes_modifiable_is_off;
Bram Moolenaardac13472019-09-16 21:06:21 +0200976 else if (vim_strchr(*varp, ',') != NULL)
977 // No comma allowed in 'fileencoding'; catches confusing it
978 // with 'fileencodings'.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000979 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200980 else
981 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200982 // May show a "+" in the title now.
983 redraw_titles();
Bram Moolenaardac13472019-09-16 21:06:21 +0200984 // Add 'fileencoding' to the swap file.
985 ml_setflags(curbuf);
986 }
987 }
988 if (errmsg == NULL)
989 {
990 // canonize the value, so that STRCMP() can be used on it
991 p = enc_canonize(*varp);
992 if (p != NULL)
993 {
994 vim_free(*varp);
995 *varp = p;
996 }
997 if (varp == &p_enc)
998 {
999 errmsg = mb_init();
Bram Moolenaardac13472019-09-16 21:06:21 +02001000 redraw_titles();
Bram Moolenaardac13472019-09-16 21:06:21 +02001001 }
1002 }
1003
1004#if defined(FEAT_GUI_GTK)
1005 if (errmsg == NULL && varp == &p_tenc && gui.in_use)
1006 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001007 // GTK uses only a single encoding, and that is UTF-8.
Bram Moolenaardac13472019-09-16 21:06:21 +02001008 if (STRCMP(p_tenc, "utf-8") != 0)
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001009 errmsg = e_cannot_be_changed_in_gtk_GUI;
Bram Moolenaardac13472019-09-16 21:06:21 +02001010 }
1011#endif
1012
1013 if (errmsg == NULL)
1014 {
1015#ifdef FEAT_KEYMAP
1016 // When 'keymap' is used and 'encoding' changes, reload the keymap
1017 // (with another encoding).
1018 if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
1019 (void)keymap_init();
1020#endif
1021
1022 // When 'termencoding' is not empty and 'encoding' changes or when
1023 // 'termencoding' changes, need to setup for keyboard input and
1024 // display output conversion.
1025 if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
1026 {
1027 if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
1028 || convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
1029 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001030 semsg(_(e_cannot_convert_between_str_and_str),
1031 p_tenc, p_enc);
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001032 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001033 }
1034 }
1035
1036#if defined(MSWIN)
1037 // $HOME may have characters in active code page.
1038 if (varp == &p_enc)
1039 init_homedir();
1040#endif
1041 }
1042 }
1043
1044#if defined(FEAT_POSTSCRIPT)
1045 else if (varp == &p_penc)
1046 {
1047 // Canonize printencoding if VIM standard one
1048 p = enc_canonize(p_penc);
1049 if (p != NULL)
1050 {
1051 vim_free(p_penc);
1052 p_penc = p;
1053 }
1054 else
1055 {
1056 // Ensure lower case and '-' for '_'
1057 for (s = p_penc; *s != NUL; s++)
1058 {
1059 if (*s == '_')
1060 *s = '-';
1061 else
1062 *s = TOLOWER_ASC(*s);
1063 }
1064 }
1065 }
1066#endif
1067
1068#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
1069 else if (varp == &p_imak)
1070 {
1071 if (!im_xim_isvalid_imactivate())
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001072 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001073 }
1074#endif
1075
1076#ifdef FEAT_KEYMAP
1077 else if (varp == &curbuf->b_p_keymap)
1078 {
1079 if (!valid_filetype(*varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001080 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001081 else
1082 {
1083 int secure_save = secure;
1084
1085 // Reset the secure flag, since the value of 'keymap' has
1086 // been checked to be safe.
1087 secure = 0;
1088
1089 // load or unload key mapping tables
1090 errmsg = keymap_init();
1091
1092 secure = secure_save;
1093
1094 // Since we check the value, there is no need to set P_INSECURE,
1095 // even when the value comes from a modeline.
1096 *value_checked = TRUE;
1097 }
1098
1099 if (errmsg == NULL)
1100 {
1101 if (*curbuf->b_p_keymap != NUL)
1102 {
1103 // Installed a new keymap, switch on using it.
1104 curbuf->b_p_iminsert = B_IMODE_LMAP;
1105 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
1106 curbuf->b_p_imsearch = B_IMODE_LMAP;
1107 }
1108 else
1109 {
1110 // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
1111 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
1112 curbuf->b_p_iminsert = B_IMODE_NONE;
1113 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
1114 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
1115 }
1116 if ((opt_flags & OPT_LOCAL) == 0)
1117 {
1118 set_iminsert_global();
1119 set_imsearch_global();
1120 }
1121 status_redraw_curbuf();
1122 }
1123 }
1124#endif
1125
1126 // 'fileformat'
1127 else if (gvarp == &p_ff)
1128 {
1129 if (!curbuf->b_p_ma && !(opt_flags & OPT_GLOBAL))
Bram Moolenaar108010a2021-06-27 22:03:33 +02001130 errmsg = e_cannot_make_changes_modifiable_is_off;
Bram Moolenaardac13472019-09-16 21:06:21 +02001131 else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001132 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001133 else
1134 {
1135 // may also change 'textmode'
1136 if (get_fileformat(curbuf) == EOL_DOS)
1137 curbuf->b_p_tx = TRUE;
1138 else
1139 curbuf->b_p_tx = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02001140 redraw_titles();
Bram Moolenaardac13472019-09-16 21:06:21 +02001141 // update flag in swap file
1142 ml_setflags(curbuf);
1143 // Redraw needed when switching to/from "mac": a CR in the text
1144 // will be displayed differently.
1145 if (get_fileformat(curbuf) == EOL_MAC || *oldval == 'm')
1146 redraw_curbuf_later(NOT_VALID);
1147 }
1148 }
1149
1150 // 'fileformats'
1151 else if (varp == &p_ffs)
1152 {
1153 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001154 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001155 else
1156 {
1157 // also change 'textauto'
1158 if (*p_ffs == NUL)
1159 p_ta = FALSE;
1160 else
1161 p_ta = TRUE;
1162 }
1163 }
1164
1165#if defined(FEAT_CRYPT)
1166 // 'cryptkey'
1167 else if (gvarp == &p_key)
1168 {
1169 // Make sure the ":set" command doesn't show the new value in the
1170 // history.
1171 remove_key_from_history();
1172
1173 if (STRCMP(curbuf->b_p_key, oldval) != 0)
1174 // Need to update the swapfile.
Bram Moolenaar76cb6832020-05-15 22:30:38 +02001175 {
Bram Moolenaardac13472019-09-16 21:06:21 +02001176 ml_set_crypt_key(curbuf, oldval,
1177 *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
Bram Moolenaar76cb6832020-05-15 22:30:38 +02001178 changed_internal();
1179 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001180 }
1181
1182 else if (gvarp == &p_cm)
1183 {
1184 if (opt_flags & OPT_LOCAL)
1185 p = curbuf->b_p_cm;
1186 else
1187 p = p_cm;
1188 if (check_opt_strings(p, p_cm_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001189 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001190 else if (crypt_self_test() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001191 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001192 else
1193 {
1194 // When setting the global value to empty, make it "zip".
1195 if (*p_cm == NUL)
1196 {
1197 if (new_value_alloced)
1198 free_string_option(p_cm);
1199 p_cm = vim_strsave((char_u *)"zip");
1200 new_value_alloced = TRUE;
1201 }
1202 // When using ":set cm=name" the local value is going to be empty.
1203 // Do that here, otherwise the crypt functions will still use the
1204 // local value.
1205 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1206 {
1207 free_string_option(curbuf->b_p_cm);
1208 curbuf->b_p_cm = empty_option;
1209 }
1210
1211 // Need to update the swapfile when the effective method changed.
1212 // Set "s" to the effective old value, "p" to the effective new
1213 // method and compare.
1214 if ((opt_flags & OPT_LOCAL) && *oldval == NUL)
1215 s = p_cm; // was previously using the global value
1216 else
1217 s = oldval;
1218 if (*curbuf->b_p_cm == NUL)
1219 p = p_cm; // is now using the global value
1220 else
1221 p = curbuf->b_p_cm;
1222 if (STRCMP(s, p) != 0)
1223 ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1224
1225 // If the global value changes need to update the swapfile for all
1226 // buffers using that value.
1227 if ((opt_flags & OPT_GLOBAL) && STRCMP(p_cm, oldval) != 0)
1228 {
1229 buf_T *buf;
1230
1231 FOR_ALL_BUFFERS(buf)
1232 if (buf != curbuf && *buf->b_p_cm == NUL)
1233 ml_set_crypt_key(buf, buf->b_p_key, oldval);
1234 }
1235 }
1236 }
1237#endif
1238
1239 // 'matchpairs'
1240 else if (gvarp == &p_mps)
1241 {
1242 if (has_mbyte)
1243 {
1244 for (p = *varp; *p != NUL; ++p)
1245 {
1246 int x2 = -1;
1247 int x3 = -1;
1248
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01001249 p += mb_ptr2len(p);
Bram Moolenaardac13472019-09-16 21:06:21 +02001250 if (*p != NUL)
1251 x2 = *p++;
1252 if (*p != NUL)
1253 {
1254 x3 = mb_ptr2char(p);
1255 p += mb_ptr2len(p);
1256 }
1257 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
1258 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001259 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001260 break;
1261 }
1262 if (*p == NUL)
1263 break;
1264 }
1265 }
1266 else
1267 {
1268 // Check for "x:y,x:y"
1269 for (p = *varp; *p != NUL; p += 4)
1270 {
1271 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
1272 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001273 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001274 break;
1275 }
1276 if (p[3] == NUL)
1277 break;
1278 }
1279 }
1280 }
1281
Bram Moolenaardac13472019-09-16 21:06:21 +02001282 // 'comments'
1283 else if (gvarp == &p_com)
1284 {
1285 for (s = *varp; *s; )
1286 {
1287 while (*s && *s != ':')
1288 {
1289 if (vim_strchr((char_u *)COM_ALL, *s) == NULL
1290 && !VIM_ISDIGIT(*s) && *s != '-')
1291 {
1292 errmsg = illegal_char(errbuf, *s);
1293 break;
1294 }
1295 ++s;
1296 }
1297 if (*s++ == NUL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001298 errmsg = e_missing_colon;
Bram Moolenaardac13472019-09-16 21:06:21 +02001299 else if (*s == ',' || *s == NUL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001300 errmsg = e_zero_length_string;
Bram Moolenaardac13472019-09-16 21:06:21 +02001301 if (errmsg != NULL)
1302 break;
1303 while (*s && *s != ',')
1304 {
1305 if (*s == '\\' && s[1] != NUL)
1306 ++s;
1307 ++s;
1308 }
1309 s = skip_to_option_part(s);
1310 }
1311 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001312
Bram Moolenaareed9d462021-02-15 20:38:25 +01001313 // global 'listchars'
Bram Moolenaardac13472019-09-16 21:06:21 +02001314 else if (varp == &p_lcs)
1315 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001316 errmsg = set_chars_option(curwin, varp);
1317 if (errmsg == NULL)
1318 {
1319 tabpage_T *tp;
1320 win_T *wp;
1321
1322 // The current window is set to use the global 'listchars' value.
1323 // So clear the window-local value.
1324 if (!(opt_flags & OPT_GLOBAL))
1325 clear_string_option(&curwin->w_p_lcs);
1326 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar606efc72021-11-12 19:52:47 +00001327 // If no error was returned above, we don't expect an error
1328 // here, so ignore the return value.
1329 (void)set_chars_option(wp, &wp->w_p_lcs);
1330
Bram Moolenaareed9d462021-02-15 20:38:25 +01001331 redraw_all_later(NOT_VALID);
1332 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001333 }
1334
Bram Moolenaareed9d462021-02-15 20:38:25 +01001335 // local 'listchars'
1336 else if (varp == &curwin->w_p_lcs)
1337 errmsg = set_chars_option(curwin, varp);
1338
Bram Moolenaardac13472019-09-16 21:06:21 +02001339 // 'fillchars'
1340 else if (varp == &p_fcs)
1341 {
Bram Moolenaareed9d462021-02-15 20:38:25 +01001342 errmsg = set_chars_option(curwin, varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02001343 }
1344
1345#ifdef FEAT_CMDWIN
1346 // 'cedit'
1347 else if (varp == &p_cedit)
1348 {
1349 errmsg = check_cedit();
1350 }
1351#endif
1352
1353 // 'verbosefile'
1354 else if (varp == &p_vfile)
1355 {
1356 verbose_stop();
1357 if (*p_vfile != NUL && verbose_open() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001358 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001359 }
1360
1361#ifdef FEAT_VIMINFO
1362 // 'viminfo'
1363 else if (varp == &p_viminfo)
1364 {
1365 for (s = p_viminfo; *s;)
1366 {
1367 // Check it's a valid character
1368 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
1369 {
1370 errmsg = illegal_char(errbuf, *s);
1371 break;
1372 }
1373 if (*s == 'n') // name is always last one
1374 break;
1375 else if (*s == 'r') // skip until next ','
1376 {
1377 while (*++s && *s != ',')
1378 ;
1379 }
1380 else if (*s == '%')
1381 {
1382 // optional number
1383 while (vim_isdigit(*++s))
1384 ;
1385 }
1386 else if (*s == '!' || *s == 'h' || *s == 'c')
1387 ++s; // no extra chars
1388 else // must have a number
1389 {
1390 while (vim_isdigit(*++s))
1391 ;
1392
1393 if (!VIM_ISDIGIT(*(s - 1)))
1394 {
1395 if (errbuf != NULL)
1396 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001397 sprintf(errbuf,
1398 _(e_missing_number_after_angle_str_angle),
Bram Moolenaardac13472019-09-16 21:06:21 +02001399 transchar_byte(*(s - 1)));
1400 errmsg = errbuf;
1401 }
1402 else
1403 errmsg = "";
1404 break;
1405 }
1406 }
1407 if (*s == ',')
1408 ++s;
1409 else if (*s)
1410 {
1411 if (errbuf != NULL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001412 errmsg = e_missing_comma;
Bram Moolenaardac13472019-09-16 21:06:21 +02001413 else
1414 errmsg = "";
1415 break;
1416 }
1417 }
1418 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001419 errmsg = e_must_specify_a_value;
Bram Moolenaardac13472019-09-16 21:06:21 +02001420 }
1421#endif // FEAT_VIMINFO
1422
1423 // terminal options
1424 else if (istermoption_idx(opt_idx) && full_screen)
1425 {
1426 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
1427 if (varp == &T_CCO)
1428 {
1429 int colors = atoi((char *)T_CCO);
1430
1431 // Only reinitialize colors if t_Co value has really changed to
1432 // avoid expensive reload of colorscheme if t_Co is set to the
1433 // same value multiple times.
1434 if (colors != t_colors)
1435 {
1436 t_colors = colors;
1437 if (t_colors <= 1)
1438 {
1439 if (new_value_alloced)
1440 vim_free(T_CCO);
1441 T_CCO = empty_option;
1442 }
1443#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1444 if (is_term_win32())
1445 {
1446 swap_tcap();
1447 did_swaptcap = TRUE;
1448 }
1449#endif
1450 // We now have a different color setup, initialize it again.
1451 init_highlight(TRUE, FALSE);
1452 }
1453 }
1454 ttest(FALSE);
1455 if (varp == &T_ME)
1456 {
1457 out_str(T_ME);
1458 redraw_later(CLEAR);
1459#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
1460 // Since t_me has been set, this probably means that the user
1461 // wants to use this as default colors. Need to reset default
1462 // background/foreground colors.
1463# ifdef VIMDLL
1464 if (!gui.in_use && !gui.starting)
1465# endif
1466 mch_set_normal_colors();
1467#endif
1468 }
1469 if (varp == &T_BE && termcap_active)
1470 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +02001471#ifdef FEAT_JOB_CHANNEL
1472 ch_log_output = TRUE;
1473#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02001474 if (*T_BE == NUL)
1475 // When clearing t_BE we assume the user no longer wants
1476 // bracketed paste, thus disable it by writing t_BD.
1477 out_str(T_BD);
1478 else
1479 out_str(T_BE);
1480 }
1481 }
1482
1483#ifdef FEAT_LINEBREAK
1484 // 'showbreak'
Bram Moolenaaree857022019-11-09 23:26:40 +01001485 else if (gvarp == &p_sbr)
Bram Moolenaardac13472019-09-16 21:06:21 +02001486 {
Bram Moolenaaree857022019-11-09 23:26:40 +01001487 for (s = *varp; *s; )
Bram Moolenaardac13472019-09-16 21:06:21 +02001488 {
1489 if (ptr2cells(s) != 1)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001490 errmsg = e_showbreak_contains_unprintable_or_wide_character;
Bram Moolenaardac13472019-09-16 21:06:21 +02001491 MB_PTR_ADV(s);
1492 }
1493 }
1494#endif
1495
1496#ifdef FEAT_GUI
1497 // 'guifont'
1498 else if (varp == &p_guifont)
1499 {
1500 if (gui.in_use)
1501 {
1502 p = p_guifont;
1503# if defined(FEAT_GUI_GTK)
1504 // Put up a font dialog and let the user select a new value.
1505 // If this is cancelled go back to the old value but don't
1506 // give an error message.
1507 if (STRCMP(p, "*") == 0)
1508 {
1509 p = gui_mch_font_dialog(oldval);
1510
1511 if (new_value_alloced)
1512 free_string_option(p_guifont);
1513
1514 p_guifont = (p != NULL) ? p : vim_strsave(oldval);
1515 new_value_alloced = TRUE;
1516 }
1517# endif
1518 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
1519 {
1520# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
1521 if (STRCMP(p_guifont, "*") == 0)
1522 {
1523 // Dialog was cancelled: Keep the old value without giving
1524 // an error message.
1525 if (new_value_alloced)
1526 free_string_option(p_guifont);
1527 p_guifont = vim_strsave(oldval);
1528 new_value_alloced = TRUE;
1529 }
1530 else
1531# endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001532 errmsg = e_invalid_fonts;
Bram Moolenaardac13472019-09-16 21:06:21 +02001533 }
1534 }
1535 redraw_gui_only = TRUE;
1536 }
1537# ifdef FEAT_XFONTSET
1538 else if (varp == &p_guifontset)
1539 {
1540 if (STRCMP(p_guifontset, "*") == 0)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001541 errmsg = e_cant_select_fontset;
Bram Moolenaardac13472019-09-16 21:06:21 +02001542 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001543 errmsg = e_invalid_fontset;
Bram Moolenaardac13472019-09-16 21:06:21 +02001544 redraw_gui_only = TRUE;
1545 }
1546# endif
1547 else if (varp == &p_guifontwide)
1548 {
1549 if (STRCMP(p_guifontwide, "*") == 0)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001550 errmsg = e_cant_select_wide_font;
Bram Moolenaardac13472019-09-16 21:06:21 +02001551 else if (gui_get_wide_font() == FAIL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001552 errmsg = e_invalid_wide_font;
Bram Moolenaardac13472019-09-16 21:06:21 +02001553 redraw_gui_only = TRUE;
1554 }
1555#endif
Dusan Popovic4eeedc02021-10-16 20:52:05 +01001556# if defined(FEAT_GUI_GTK)
1557 else if (varp == &p_guiligatures)
1558 {
1559 gui_set_ligatures();
1560 redraw_gui_only = TRUE;
1561 }
1562# endif
Bram Moolenaardac13472019-09-16 21:06:21 +02001563
1564#ifdef CURSOR_SHAPE
1565 // 'guicursor'
1566 else if (varp == &p_guicursor)
1567 errmsg = parse_shape_opt(SHAPE_CURSOR);
1568#endif
1569
1570#ifdef FEAT_MOUSESHAPE
1571 // 'mouseshape'
1572 else if (varp == &p_mouseshape)
1573 {
1574 errmsg = parse_shape_opt(SHAPE_MOUSE);
1575 update_mouseshape(-1);
1576 }
1577#endif
1578
1579#ifdef FEAT_PRINTER
1580 else if (varp == &p_popt)
1581 errmsg = parse_printoptions();
1582# if defined(FEAT_POSTSCRIPT)
1583 else if (varp == &p_pmfn)
1584 errmsg = parse_printmbfont();
1585# endif
1586#endif
1587
1588#ifdef FEAT_LANGMAP
1589 // 'langmap'
1590 else if (varp == &p_langmap)
1591 langmap_set();
1592#endif
1593
1594#ifdef FEAT_LINEBREAK
1595 // 'breakat'
1596 else if (varp == &p_breakat)
1597 fill_breakat_flags();
1598#endif
1599
Bram Moolenaardac13472019-09-16 21:06:21 +02001600 // 'titlestring' and 'iconstring'
1601 else if (varp == &p_titlestring || varp == &p_iconstring)
1602 {
Bram Moolenaar651fca82021-11-29 20:39:38 +00001603#ifdef FEAT_STL_OPT
Bram Moolenaardac13472019-09-16 21:06:21 +02001604 int flagval = (varp == &p_titlestring) ? STL_IN_TITLE : STL_IN_ICON;
1605
1606 // NULL => statusline syntax
1607 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
1608 stl_syntax |= flagval;
1609 else
1610 stl_syntax &= ~flagval;
Bram Moolenaar651fca82021-11-29 20:39:38 +00001611#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02001612 did_set_title();
1613 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001614
1615#ifdef FEAT_GUI
1616 // 'guioptions'
1617 else if (varp == &p_go)
1618 {
1619 gui_init_which_components(oldval);
1620 redraw_gui_only = TRUE;
1621 }
1622#endif
1623
1624#if defined(FEAT_GUI_TABLINE)
1625 // 'guitablabel'
1626 else if (varp == &p_gtl)
1627 {
1628 redraw_tabline = TRUE;
1629 redraw_gui_only = TRUE;
1630 }
1631 // 'guitabtooltip'
1632 else if (varp == &p_gtt)
1633 {
1634 redraw_gui_only = TRUE;
1635 }
1636#endif
1637
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001638#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +02001639 // 'ttymouse'
1640 else if (varp == &p_ttym)
1641 {
1642 // Switch the mouse off before changing the escape sequences used for
1643 // that.
1644 mch_setmouse(FALSE);
1645 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001646 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001647 else
1648 check_mouse_termcode();
1649 if (termcap_active)
1650 setmouse(); // may switch it on again
1651 }
1652#endif
1653
1654 // 'selection'
1655 else if (varp == &p_sel)
1656 {
1657 if (*p_sel == NUL
1658 || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001659 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001660 }
1661
1662 // 'selectmode'
1663 else if (varp == &p_slm)
1664 {
1665 if (check_opt_strings(p_slm, p_slm_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001666 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001667 }
1668
1669#ifdef FEAT_BROWSE
1670 // 'browsedir'
1671 else if (varp == &p_bsdir)
1672 {
1673 if (check_opt_strings(p_bsdir, p_bsdir_values, FALSE) != OK
1674 && !mch_isdir(p_bsdir))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001675 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001676 }
1677#endif
1678
1679 // 'keymodel'
1680 else if (varp == &p_km)
1681 {
1682 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001683 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001684 else
1685 {
1686 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
1687 km_startsel = (vim_strchr(p_km, 'a') != NULL);
1688 }
1689 }
1690
1691 // 'mousemodel'
1692 else if (varp == &p_mousem)
1693 {
1694 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001695 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001696#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
1697 else if (*p_mousem != *oldval)
1698 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
1699 // to create or delete the popup menus.
1700 gui_motif_update_mousemodel(root_menu);
1701#endif
1702 }
1703
1704 // 'switchbuf'
1705 else if (varp == &p_swb)
1706 {
1707 if (opt_strings_flags(p_swb, p_swb_values, &swb_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001708 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001709 }
1710
1711 // 'debug'
1712 else if (varp == &p_debug)
1713 {
1714 if (check_opt_strings(p_debug, p_debug_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001715 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001716 }
1717
1718 // 'display'
1719 else if (varp == &p_dy)
1720 {
1721 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001722 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001723 else
1724 (void)init_chartab();
1725
1726 }
1727
1728 // 'eadirection'
1729 else if (varp == &p_ead)
1730 {
1731 if (check_opt_strings(p_ead, p_ead_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001732 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001733 }
1734
1735#ifdef FEAT_CLIPBOARD
1736 // 'clipboard'
1737 else if (varp == &p_cb)
1738 errmsg = check_clipboard_option();
1739#endif
1740
1741#ifdef FEAT_SPELL
1742 // When 'spelllang' or 'spellfile' is set and there is a window for this
1743 // buffer in which 'spell' is set load the wordlists.
1744 else if (varp == &(curwin->w_s->b_p_spl)
1745 || varp == &(curwin->w_s->b_p_spf))
1746 {
1747 int is_spellfile = varp == &(curwin->w_s->b_p_spf);
1748
1749 if ((is_spellfile && !valid_spellfile(*varp))
Bram Moolenaarf154f3a2020-06-08 18:54:49 +02001750 || (!is_spellfile && !valid_spelllang(*varp)))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001751 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001752 else
1753 errmsg = did_set_spell_option(is_spellfile);
1754 }
1755 // When 'spellcapcheck' is set compile the regexp program.
1756 else if (varp == &(curwin->w_s->b_p_spc))
1757 {
1758 errmsg = compile_cap_prog(curwin->w_s);
1759 }
Bram Moolenaar362b44b2020-06-10 21:47:00 +02001760 // 'spelloptions'
1761 else if (varp == &(curwin->w_s->b_p_spo))
1762 {
1763 if (**varp != NUL && STRCMP("camel", *varp) != 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001764 errmsg = e_invalid_argument;
Bram Moolenaar362b44b2020-06-10 21:47:00 +02001765 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001766 // 'spellsuggest'
1767 else if (varp == &p_sps)
1768 {
1769 if (spell_check_sps() != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001770 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001771 }
1772 // 'mkspellmem'
1773 else if (varp == &p_msm)
1774 {
1775 if (spell_check_msm() != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001776 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001777 }
1778#endif
1779
1780 // When 'bufhidden' is set, check for valid value.
1781 else if (gvarp == &p_bh)
1782 {
1783 if (check_opt_strings(curbuf->b_p_bh, p_bufhidden_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001784 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001785 }
1786
1787 // When 'buftype' is set, check for valid value.
1788 else if (gvarp == &p_bt)
1789 {
1790 if (check_opt_strings(curbuf->b_p_bt, p_buftype_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001791 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001792 else
1793 {
1794 if (curwin->w_status_height)
1795 {
1796 curwin->w_redr_status = TRUE;
1797 redraw_later(VALID);
1798 }
1799 curbuf->b_help = (curbuf->b_p_bt[0] == 'h');
Bram Moolenaardac13472019-09-16 21:06:21 +02001800 redraw_titles();
Bram Moolenaardac13472019-09-16 21:06:21 +02001801 }
1802 }
1803
1804#ifdef FEAT_STL_OPT
zeertzjq5dc294a2022-04-15 13:17:57 +01001805 // 'statusline', 'tabline' or 'rulerformat'
1806 else if (gvarp == &p_stl || varp == &p_tal || varp == &p_ruf)
Bram Moolenaardac13472019-09-16 21:06:21 +02001807 {
1808 int wid;
1809
1810 if (varp == &p_ruf) // reset ru_wid first
1811 ru_wid = 0;
1812 s = *varp;
1813 if (varp == &p_ruf && *s == '%')
1814 {
1815 // set ru_wid if 'ruf' starts with "%99("
1816 if (*++s == '-') // ignore a '-'
1817 s++;
1818 wid = getdigits(&s);
1819 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
1820 ru_wid = wid;
1821 else
1822 errmsg = check_stl_option(p_ruf);
1823 }
zeertzjq5dc294a2022-04-15 13:17:57 +01001824 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
Bram Moolenaardac13472019-09-16 21:06:21 +02001825 else if (varp == &p_ruf || s[0] != '%' || s[1] != '!')
1826 errmsg = check_stl_option(s);
1827 if (varp == &p_ruf && errmsg == NULL)
1828 comp_col();
1829 }
1830#endif
1831
1832 // check if it is a valid value for 'complete' -- Acevedo
1833 else if (gvarp == &p_cpt)
1834 {
1835 for (s = *varp; *s;)
1836 {
1837 while (*s == ',' || *s == ' ')
1838 s++;
1839 if (!*s)
1840 break;
1841 if (vim_strchr((char_u *)".wbuksid]tU", *s) == NULL)
1842 {
1843 errmsg = illegal_char(errbuf, *s);
1844 break;
1845 }
1846 if (*++s != NUL && *s != ',' && *s != ' ')
1847 {
1848 if (s[-1] == 'k' || s[-1] == 's')
1849 {
1850 // skip optional filename after 'k' and 's'
1851 while (*s && *s != ',' && *s != ' ')
1852 {
1853 if (*s == '\\' && s[1] != NUL)
1854 ++s;
1855 ++s;
1856 }
1857 }
1858 else
1859 {
1860 if (errbuf != NULL)
1861 {
1862 sprintf((char *)errbuf,
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001863 _(e_illegal_character_after_chr), *--s);
Bram Moolenaardac13472019-09-16 21:06:21 +02001864 errmsg = errbuf;
1865 }
1866 else
1867 errmsg = "";
1868 break;
1869 }
1870 }
1871 }
1872 }
1873
1874 // 'completeopt'
1875 else if (varp == &p_cot)
1876 {
1877 if (check_opt_strings(p_cot, p_cot_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001878 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001879 else
1880 completeopt_was_set();
1881 }
1882
1883#ifdef BACKSLASH_IN_FILENAME
1884 // 'completeslash'
1885 else if (gvarp == &p_csl)
1886 {
1887 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1888 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001889 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001890 }
1891#endif
1892
1893#ifdef FEAT_SIGNS
1894 // 'signcolumn'
1895 else if (varp == &curwin->w_p_scl)
1896 {
1897 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001898 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001899 // When changing the 'signcolumn' to or from 'number', recompute the
1900 // width of the number column if 'number' or 'relativenumber' is set.
1901 if (((*oldval == 'n' && *(oldval + 1) == 'u')
1902 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
1903 && (curwin->w_p_nu || curwin->w_p_rnu))
1904 curwin->w_nrwidth_line_count = 0;
1905 }
1906#endif
1907
1908
1909#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
1910 // 'toolbar'
1911 else if (varp == &p_toolbar)
1912 {
1913 if (opt_strings_flags(p_toolbar, p_toolbar_values,
1914 &toolbar_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001915 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001916 else
1917 {
1918 out_flush();
1919 gui_mch_show_toolbar((toolbar_flags &
1920 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
1921 }
1922 }
1923#endif
1924
1925#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
1926 // 'toolbariconsize': GTK+ 2 only
1927 else if (varp == &p_tbis)
1928 {
1929 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001930 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001931 else
1932 {
1933 out_flush();
1934 gui_mch_show_toolbar((toolbar_flags &
1935 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
1936 }
1937 }
1938#endif
1939
1940 // 'pastetoggle': translate key codes like in a mapping
1941 else if (varp == &p_pt)
1942 {
1943 if (*p_pt)
1944 {
Bram Moolenaar1e7b52a2019-10-13 16:59:08 +02001945 (void)replace_termcodes(p_pt, &p,
1946 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
Bram Moolenaardac13472019-09-16 21:06:21 +02001947 if (p != NULL)
1948 {
1949 if (new_value_alloced)
1950 free_string_option(p_pt);
1951 p_pt = p;
1952 new_value_alloced = TRUE;
1953 }
1954 }
1955 }
1956
1957 // 'backspace'
1958 else if (varp == &p_bs)
1959 {
1960 if (VIM_ISDIGIT(*p_bs))
1961 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02001962 if (*p_bs > '3' || p_bs[1] != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001963 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001964 }
1965 else if (check_opt_strings(p_bs, p_bs_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001966 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001967 }
1968 else if (varp == &p_bo)
1969 {
1970 if (opt_strings_flags(p_bo, p_bo_values, &bo_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001971 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001972 }
1973
1974 // 'tagcase'
1975 else if (gvarp == &p_tc)
1976 {
1977 unsigned int *flags;
1978
1979 if (opt_flags & OPT_LOCAL)
1980 {
1981 p = curbuf->b_p_tc;
1982 flags = &curbuf->b_tc_flags;
1983 }
1984 else
1985 {
1986 p = p_tc;
1987 flags = &tc_flags;
1988 }
1989
1990 if ((opt_flags & OPT_LOCAL) && *p == NUL)
1991 // make the local value empty: use the global value
1992 *flags = 0;
1993 else if (*p == NUL
1994 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001995 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001996 }
1997
1998 // 'casemap'
1999 else if (varp == &p_cmp)
2000 {
2001 if (opt_strings_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002002 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002003 }
2004
2005#ifdef FEAT_DIFF
2006 // 'diffopt'
2007 else if (varp == &p_dip)
2008 {
2009 if (diffopt_changed() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002010 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002011 }
2012#endif
2013
2014#ifdef FEAT_FOLDING
2015 // 'foldmethod'
2016 else if (gvarp == &curwin->w_allbuf_opt.wo_fdm)
2017 {
2018 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK
2019 || *curwin->w_p_fdm == NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002020 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002021 else
2022 {
2023 foldUpdateAll(curwin);
2024 if (foldmethodIsDiff(curwin))
2025 newFoldLevel();
2026 }
2027 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002028 // 'foldmarker'
2029 else if (gvarp == &curwin->w_allbuf_opt.wo_fmr)
2030 {
2031 p = vim_strchr(*varp, ',');
2032 if (p == NULL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002033 errmsg = e_comma_required;
Bram Moolenaardac13472019-09-16 21:06:21 +02002034 else if (p == *varp || p[1] == NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002035 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002036 else if (foldmethodIsMarker(curwin))
2037 foldUpdateAll(curwin);
2038 }
2039 // 'commentstring'
2040 else if (gvarp == &p_cms)
2041 {
2042 if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002043 errmsg = e_commentstring_must_be_empty_or_contain_str;
Bram Moolenaardac13472019-09-16 21:06:21 +02002044 }
2045 // 'foldopen'
2046 else if (varp == &p_fdo)
2047 {
2048 if (opt_strings_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002049 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002050 }
2051 // 'foldclose'
2052 else if (varp == &p_fcl)
2053 {
2054 if (check_opt_strings(p_fcl, p_fcl_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002055 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002056 }
2057 // 'foldignore'
2058 else if (gvarp == &curwin->w_allbuf_opt.wo_fdi)
2059 {
2060 if (foldmethodIsIndent(curwin))
2061 foldUpdateAll(curwin);
2062 }
2063#endif
2064
2065 // 'virtualedit'
Gary Johnson53ba05b2021-07-26 22:19:10 +02002066 else if (gvarp == &p_ve)
Bram Moolenaardac13472019-09-16 21:06:21 +02002067 {
Gary Johnson53ba05b2021-07-26 22:19:10 +02002068 char_u *ve = p_ve;
2069 unsigned int *flags = &ve_flags;
2070
2071 if (opt_flags & OPT_LOCAL)
Bram Moolenaardac13472019-09-16 21:06:21 +02002072 {
Gary Johnson51ad8502021-08-03 18:33:08 +02002073 ve = curwin->w_p_ve;
2074 flags = &curwin->w_ve_flags;
Gary Johnson53ba05b2021-07-26 22:19:10 +02002075 }
2076
2077 if ((opt_flags & OPT_LOCAL) && *ve == NUL)
2078 // make the local value empty: use the global value
2079 *flags = 0;
2080 else
2081 {
2082 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002083 errmsg = e_invalid_argument;
Gary Johnson53ba05b2021-07-26 22:19:10 +02002084 else if (STRCMP(p_ve, oldval) != 0)
2085 {
2086 // Recompute cursor position in case the new 've' setting
2087 // changes something.
2088 validate_virtcol();
2089 coladvance(curwin->w_virtcol);
2090 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002091 }
2092 }
2093
2094#if defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)
2095 else if (varp == &p_csqf)
2096 {
2097 if (p_csqf != NULL)
2098 {
2099 p = p_csqf;
2100 while (*p != NUL)
2101 {
2102 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
2103 || p[1] == NUL
2104 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
2105 || (p[2] != NUL && p[2] != ','))
2106 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002107 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002108 break;
2109 }
2110 else if (p[2] == NUL)
2111 break;
2112 else
2113 p += 3;
2114 }
2115 }
2116 }
2117#endif
2118
2119#ifdef FEAT_CINDENT
2120 // 'cinoptions'
2121 else if (gvarp == &p_cino)
2122 {
2123 // TODO: recognize errors
2124 parse_cino(curbuf);
2125 }
2126#endif
2127
2128#if defined(FEAT_RENDER_OPTIONS)
2129 // 'renderoptions'
2130 else if (varp == &p_rop)
2131 {
2132 if (!gui_mch_set_rendering_options(p_rop))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002133 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002134 }
2135#endif
2136
2137 else if (gvarp == &p_ft)
2138 {
2139 if (!valid_filetype(*varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002140 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002141 else
2142 {
2143 value_changed = STRCMP(oldval, *varp) != 0;
2144
2145 // Since we check the value, there is no need to set P_INSECURE,
2146 // even when the value comes from a modeline.
2147 *value_checked = TRUE;
2148 }
2149 }
2150
2151#ifdef FEAT_SYN_HL
2152 else if (gvarp == &p_syn)
2153 {
2154 if (!valid_filetype(*varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002155 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002156 else
2157 {
2158 value_changed = STRCMP(oldval, *varp) != 0;
2159
2160 // Since we check the value, there is no need to set P_INSECURE,
2161 // even when the value comes from a modeline.
2162 *value_checked = TRUE;
2163 }
2164 }
2165#endif
2166
2167#ifdef FEAT_TERMINAL
2168 // 'termwinkey'
2169 else if (varp == &curwin->w_p_twk)
2170 {
2171 if (*curwin->w_p_twk != NUL
2172 && string_to_key(curwin->w_p_twk, TRUE) == 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002173 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002174 }
2175 // 'termwinsize'
2176 else if (varp == &curwin->w_p_tws)
2177 {
2178 if (*curwin->w_p_tws != NUL)
2179 {
2180 p = skipdigits(curwin->w_p_tws);
2181 if (p == curwin->w_p_tws
2182 || (*p != 'x' && *p != '*')
2183 || *skipdigits(p + 1) != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002184 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002185 }
2186 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002187 // 'wincolor'
2188 else if (varp == &curwin->w_p_wcr)
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002189 term_update_wincolor(curwin);
Bram Moolenaardac13472019-09-16 21:06:21 +02002190# if defined(MSWIN)
2191 // 'termwintype'
2192 else if (varp == &p_twt)
2193 {
2194 if (check_opt_strings(*varp, p_twt_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002195 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002196 }
2197# endif
2198#endif
2199
2200#ifdef FEAT_VARTABS
2201 // 'varsofttabstop'
2202 else if (varp == &(curbuf->b_p_vsts))
2203 {
2204 char_u *cp;
2205
2206 if (!(*varp)[0] || ((*varp)[0] == '0' && !(*varp)[1]))
2207 {
2208 if (curbuf->b_p_vsts_array)
2209 {
2210 vim_free(curbuf->b_p_vsts_array);
2211 curbuf->b_p_vsts_array = 0;
2212 }
2213 }
2214 else
2215 {
2216 for (cp = *varp; *cp; ++cp)
2217 {
2218 if (vim_isdigit(*cp))
2219 continue;
2220 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
2221 continue;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002222 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002223 break;
2224 }
2225 if (errmsg == NULL)
2226 {
2227 int *oldarray = curbuf->b_p_vsts_array;
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002228 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Bram Moolenaardac13472019-09-16 21:06:21 +02002229 {
2230 if (oldarray)
2231 vim_free(oldarray);
2232 }
2233 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002234 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002235 }
2236 }
2237 }
2238
2239 // 'vartabstop'
2240 else if (varp == &(curbuf->b_p_vts))
2241 {
2242 char_u *cp;
2243
2244 if (!(*varp)[0] || ((*varp)[0] == '0' && !(*varp)[1]))
2245 {
2246 if (curbuf->b_p_vts_array)
2247 {
2248 vim_free(curbuf->b_p_vts_array);
2249 curbuf->b_p_vts_array = NULL;
2250 }
2251 }
2252 else
2253 {
2254 for (cp = *varp; *cp; ++cp)
2255 {
2256 if (vim_isdigit(*cp))
2257 continue;
2258 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
2259 continue;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002260 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002261 break;
2262 }
2263 if (errmsg == NULL)
2264 {
2265 int *oldarray = curbuf->b_p_vts_array;
2266
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002267 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Bram Moolenaardac13472019-09-16 21:06:21 +02002268 {
2269 vim_free(oldarray);
2270#ifdef FEAT_FOLDING
2271 if (foldmethodIsIndent(curwin))
2272 foldUpdateAll(curwin);
2273#endif
2274 }
2275 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002276 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002277 }
2278 }
2279 }
2280#endif
2281
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002282#ifdef FEAT_PROP_POPUP
Bram Moolenaardac13472019-09-16 21:06:21 +02002283 // 'previewpopup'
2284 else if (varp == &p_pvp)
2285 {
2286 if (parse_previewpopup(NULL) == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002287 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002288 }
2289# ifdef FEAT_QUICKFIX
2290 // 'completepopup'
2291 else if (varp == &p_cpp)
2292 {
2293 if (parse_completepopup(NULL) == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002294 errmsg = e_invalid_argument;
Bram Moolenaar447bfba2020-07-18 16:07:16 +02002295 else
2296 popup_close_info();
Bram Moolenaardac13472019-09-16 21:06:21 +02002297 }
2298# endif
2299#endif
2300
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002301#ifdef FEAT_EVAL
2302 // '*expr' options
2303 else if (
2304# ifdef FEAT_BEVAL
2305 varp == &p_bexpr ||
2306# endif
2307# ifdef FEAT_DIFF
2308 varp == &p_dex ||
2309# endif
2310# ifdef FEAT_FOLDING
2311 varp == &curwin->w_p_fde ||
Yegappan Lakshmanan27708e62021-12-26 21:54:43 +00002312 varp == &curwin->w_p_fdt ||
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002313# endif
2314 gvarp == &p_fex ||
2315# ifdef FEAT_FIND_ID
2316 gvarp == &p_inex ||
2317# endif
2318# ifdef FEAT_CINDENT
2319 gvarp == &p_inde ||
2320# endif
2321# ifdef FEAT_DIFF
2322 varp == &p_pex ||
2323# endif
2324# ifdef FEAT_POSTSCRIPT
2325 varp == &p_pexpr ||
2326# endif
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00002327 varp == &p_ccv)
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002328 {
2329 char_u **p_opt = NULL;
2330 char_u *name;
2331
2332 // If the option value starts with <SID> or s:, then replace that with
2333 // the script identifier.
2334# ifdef FEAT_BEVAL
2335 if (varp == &p_bexpr) // 'balloonexpr'
2336 p_opt = (opt_flags & OPT_LOCAL) ? &curbuf->b_p_bexpr : &p_bexpr;
2337# endif
2338# ifdef FEAT_DIFF
2339 if (varp == &p_dex) // 'diffexpr'
2340 p_opt = &p_dex;
2341# endif
2342# ifdef FEAT_FOLDING
Yegappan Lakshmanan27708e62021-12-26 21:54:43 +00002343 if (varp == &curwin->w_p_fde) // 'foldexpr'
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002344 p_opt = &curwin->w_p_fde;
Yegappan Lakshmanan27708e62021-12-26 21:54:43 +00002345 if (varp == &curwin->w_p_fdt) // 'foldtext'
2346 p_opt = &curwin->w_p_fdt;
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002347# endif
2348 if (gvarp == &p_fex) // 'formatexpr'
2349 p_opt = &curbuf->b_p_fex;
2350# ifdef FEAT_FIND_ID
2351 if (gvarp == &p_inex) // 'includeexpr'
2352 p_opt = &curbuf->b_p_inex;
2353# endif
2354# ifdef FEAT_CINDENT
2355 if (gvarp == &p_inde) // 'indentexpr'
2356 p_opt = &curbuf->b_p_inde;
2357# endif
2358# ifdef FEAT_DIFF
2359 if (varp == &p_pex) // 'patchexpr'
2360 p_opt = &p_pex;
2361# endif
2362# ifdef FEAT_POSTSCRIPT
2363 if (varp == &p_pexpr) // 'printexpr'
2364 p_opt = &p_pexpr;
2365# endif
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00002366 if (varp == &p_ccv) // 'charconvert'
2367 p_opt = &p_ccv;
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002368
2369 if (p_opt != NULL)
2370 {
2371 name = get_scriptlocal_funcname(*p_opt);
2372 if (name != NULL)
2373 {
2374 if (new_value_alloced)
2375 free_string_option(*p_opt);
2376 *p_opt = name;
2377 new_value_alloced = TRUE;
2378 }
2379 }
2380
2381# ifdef FEAT_FOLDING
2382 if (varp == &curwin->w_p_fde && foldmethodIsExpr(curwin))
2383 foldUpdateAll(curwin);
2384# endif
2385 }
2386#endif
2387
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002388#ifdef FEAT_COMPL_FUNC
2389 // 'completefunc'
2390 else if (gvarp == &p_cfu)
2391 {
2392 if (set_completefunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002393 errmsg = e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002394 }
2395
2396 // 'omnifunc'
2397 else if (gvarp == &p_ofu)
2398 {
2399 if (set_omnifunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002400 errmsg = e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002401 }
2402
2403 // 'thesaurusfunc'
2404 else if (gvarp == &p_tsrfu)
2405 {
2406 if (set_thesaurusfunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002407 errmsg = e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002408 }
2409#endif
2410
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00002411#if defined(FEAT_EVAL) && \
2412 (defined(FEAT_XIM) || defined(IME_WITHOUT_XIM) || defined(VIMDLL))
2413 // 'imactivatefunc'
2414 else if (gvarp == &p_imaf)
2415 {
2416 if (set_imactivatefunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002417 errmsg = e_invalid_argument;
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00002418 }
2419
2420 // 'imstatusfunc'
2421 else if (gvarp == &p_imsf)
2422 {
2423 if (set_imstatusfunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002424 errmsg = e_invalid_argument;
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00002425 }
2426#endif
2427
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00002428 // 'operatorfunc'
2429 else if (varp == &p_opfunc)
2430 {
2431 if (set_operatorfunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002432 errmsg = e_invalid_argument;
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00002433 }
2434
Bram Moolenaard43906d2020-07-20 21:31:32 +02002435#ifdef FEAT_QUICKFIX
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00002436 // 'quickfixtextfunc'
Bram Moolenaard43906d2020-07-20 21:31:32 +02002437 else if (varp == &p_qftf)
2438 {
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00002439 if (qf_process_qftf_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002440 errmsg = e_invalid_argument;
Bram Moolenaard43906d2020-07-20 21:31:32 +02002441 }
2442#endif
2443
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00002444#ifdef FEAT_EVAL
2445 // 'tagfunc'
2446 else if (gvarp == &p_tfu)
2447 {
2448 if (set_tagfunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002449 errmsg = e_invalid_argument;
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00002450 }
2451#endif
2452
Bram Moolenaardac13472019-09-16 21:06:21 +02002453 // Options that are a list of flags.
2454 else
2455 {
2456 p = NULL;
2457 if (varp == &p_ww) // 'whichwrap'
2458 p = (char_u *)WW_ALL;
2459 if (varp == &p_shm) // 'shortmess'
2460 p = (char_u *)SHM_ALL;
2461 else if (varp == &(p_cpo)) // 'cpoptions'
2462 p = (char_u *)CPO_ALL;
2463 else if (varp == &(curbuf->b_p_fo)) // 'formatoptions'
2464 p = (char_u *)FO_ALL;
2465#ifdef FEAT_CONCEAL
2466 else if (varp == &curwin->w_p_cocu) // 'concealcursor'
2467 p = (char_u *)COCU_ALL;
2468#endif
2469 else if (varp == &p_mouse) // 'mouse'
2470 {
Bram Moolenaardac13472019-09-16 21:06:21 +02002471 p = (char_u *)MOUSE_ALL;
Bram Moolenaardac13472019-09-16 21:06:21 +02002472 }
2473#if defined(FEAT_GUI)
2474 else if (varp == &p_go) // 'guioptions'
2475 p = (char_u *)GO_ALL;
2476#endif
2477 if (p != NULL)
2478 {
2479 for (s = *varp; *s; ++s)
2480 if (vim_strchr(p, *s) == NULL)
2481 {
2482 errmsg = illegal_char(errbuf, *s);
2483 break;
2484 }
2485 }
2486 }
2487
2488 // If error detected, restore the previous value.
2489 if (errmsg != NULL)
2490 {
2491 if (new_value_alloced)
2492 free_string_option(*varp);
2493 *varp = oldval;
2494 // When resetting some values, need to act on it.
2495 if (did_chartab)
2496 (void)init_chartab();
2497 if (varp == &p_hl)
2498 (void)highlight_changed();
2499 }
2500 else
2501 {
2502#ifdef FEAT_EVAL
2503 // Remember where the option was set.
2504 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
2505#endif
2506 // Free string options that are in allocated memory.
2507 // Use "free_oldval", because recursiveness may change the flags under
2508 // our fingers (esp. init_highlight()).
2509 if (free_oldval)
2510 free_string_option(oldval);
2511 if (new_value_alloced)
2512 set_option_flag(opt_idx, P_ALLOCED);
2513 else
2514 clear_option_flag(opt_idx, P_ALLOCED);
2515
2516 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
2517 && is_global_local_option(opt_idx))
2518 {
2519 // global option with local value set to use global value; free
2520 // the local value and make it empty
2521 p = get_option_varp_scope(opt_idx, OPT_LOCAL);
2522 free_string_option(*(char_u **)p);
2523 *(char_u **)p = empty_option;
2524 }
2525
2526 // May set global value for local option.
2527 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
2528 set_string_option_global(opt_idx, varp);
2529
2530 // Trigger the autocommand only after setting the flags.
2531#ifdef FEAT_SYN_HL
2532 // When 'syntax' is set, load the syntax of that name
2533 if (varp == &(curbuf->b_p_syn))
2534 {
2535 static int syn_recursive = 0;
2536
2537 ++syn_recursive;
2538 // Only pass TRUE for "force" when the value changed or not used
2539 // recursively, to avoid endless recurrence.
2540 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
2541 value_changed || syn_recursive == 1, curbuf);
2542 curbuf->b_flags |= BF_SYN_SET;
2543 --syn_recursive;
2544 }
2545#endif
2546 else if (varp == &(curbuf->b_p_ft))
2547 {
2548 // 'filetype' is set, trigger the FileType autocommand.
2549 // Skip this when called from a modeline and the filetype was
2550 // already set to this value.
2551 if (!(opt_flags & OPT_MODELINE) || value_changed)
2552 {
2553 static int ft_recursive = 0;
2554 int secure_save = secure;
2555
2556 // Reset the secure flag, since the value of 'filetype' has
2557 // been checked to be safe.
2558 secure = 0;
2559
2560 ++ft_recursive;
2561 did_filetype = TRUE;
2562 // Only pass TRUE for "force" when the value changed or not
2563 // used recursively, to avoid endless recurrence.
2564 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2565 value_changed || ft_recursive == 1, curbuf);
2566 --ft_recursive;
2567 // Just in case the old "curbuf" is now invalid.
2568 if (varp != &(curbuf->b_p_ft))
2569 varp = NULL;
2570
2571 secure = secure_save;
2572 }
2573 }
2574#ifdef FEAT_SPELL
2575 if (varp == &(curwin->w_s->b_p_spl))
2576 {
2577 char_u fname[200];
2578 char_u *q = curwin->w_s->b_p_spl;
2579
2580 // Skip the first name if it is "cjk".
2581 if (STRNCMP(q, "cjk,", 4) == 0)
2582 q += 4;
2583
2584 // Source the spell/LANG.vim in 'runtimepath'.
2585 // They could set 'spellcapcheck' depending on the language.
2586 // Use the first name in 'spelllang' up to '_region' or
2587 // '.encoding'.
2588 for (p = q; *p != NUL; ++p)
2589 if (!ASCII_ISALNUM(*p) && *p != '-')
2590 break;
2591 if (p > q)
2592 {
2593 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
2594 (int)(p - q), q);
2595 source_runtime(fname, DIP_ALL);
2596 }
2597 }
2598#endif
2599 }
2600
Bram Moolenaardac13472019-09-16 21:06:21 +02002601 if (varp == &p_mouse)
2602 {
Bram Moolenaardac13472019-09-16 21:06:21 +02002603 if (*p_mouse == NUL)
2604 mch_setmouse(FALSE); // switch mouse off
2605 else
Bram Moolenaardac13472019-09-16 21:06:21 +02002606 setmouse(); // in case 'mouse' changed
2607 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002608
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002609#if defined(FEAT_LUA) || defined(PROTO)
2610 if (varp == &p_rtp)
2611 update_package_paths_in_lua();
2612#endif
2613
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00002614#if defined(FEAT_LINEBREAK)
2615 // Changing Formatlistpattern when briopt includes the list setting:
2616 // redraw
2617 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
2618 && curwin->w_briopt_list)
2619 redraw_all_later(NOT_VALID);
2620#endif
2621
Bram Moolenaardac13472019-09-16 21:06:21 +02002622 if (curwin->w_curswant != MAXCOL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002623 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02002624 curwin->w_set_curswant = TRUE;
2625
Bram Moolenaar37294bd2021-03-10 13:40:08 +01002626 if ((opt_flags & OPT_NO_REDRAW) == 0)
2627 {
Bram Moolenaardac13472019-09-16 21:06:21 +02002628#ifdef FEAT_GUI
Bram Moolenaar37294bd2021-03-10 13:40:08 +01002629 // check redraw when it's not a GUI option or the GUI is active.
2630 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02002631#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01002632 check_redraw(get_option_flags(opt_idx));
2633 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002634
2635#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2636 if (did_swaptcap)
2637 {
2638 set_termname((char_u *)"win32");
2639 init_highlight(TRUE, FALSE);
2640 }
2641#endif
2642
2643 return errmsg;
2644}
2645
2646/*
2647 * Check an option that can be a range of string values.
2648 *
2649 * Return OK for correct value, FAIL otherwise.
2650 * Empty is always OK.
2651 */
2652 static int
2653check_opt_strings(
2654 char_u *val,
2655 char **values,
2656 int list) // when TRUE: accept a list of values
2657{
2658 return opt_strings_flags(val, values, NULL, list);
2659}
2660
2661/*
2662 * Handle an option that can be a range of string values.
2663 * Set a flag in "*flagp" for each string present.
2664 *
2665 * Return OK for correct value, FAIL otherwise.
2666 * Empty is always OK.
2667 */
2668 static int
2669opt_strings_flags(
2670 char_u *val, // new value
2671 char **values, // array of valid string values
2672 unsigned *flagp,
2673 int list) // when TRUE: accept a list of values
2674{
2675 int i;
2676 int len;
2677 unsigned new_flags = 0;
2678
2679 while (*val)
2680 {
2681 for (i = 0; ; ++i)
2682 {
2683 if (values[i] == NULL) // val not found in values[]
2684 return FAIL;
2685
2686 len = (int)STRLEN(values[i]);
2687 if (STRNCMP(values[i], val, len) == 0
2688 && ((list && val[len] == ',') || val[len] == NUL))
2689 {
2690 val += len + (val[len] == ',');
2691 new_flags |= (1 << i);
2692 break; // check next item in val list
2693 }
2694 }
2695 }
2696 if (flagp != NULL)
2697 *flagp = new_flags;
2698
2699 return OK;
2700}
2701
2702/*
2703 * return OK if "p" is a valid fileformat name, FAIL otherwise.
2704 */
2705 int
2706check_ff_value(char_u *p)
2707{
2708 return check_opt_strings(p, p_ff_values, FALSE);
2709}