blob: f3bc285823ed0e1481225dded0e11ffdbcba1045 [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
Christian Brabandt9aee8ec2022-12-16 16:41:23 +000016static char_u shm_buf[SHM_LEN];
17static int set_shm_recursive = 0;
18
Bram Moolenaardac13472019-09-16 21:06:21 +020019static char *(p_ambw_values[]) = {"single", "double", NULL};
20static char *(p_bg_values[]) = {"light", "dark", NULL};
21static char *(p_bkc_values[]) = {"yes", "auto", "no", "breaksymlink", "breakhardlink", NULL};
22static char *(p_bo_values[]) = {"all", "backspace", "cursor", "complete",
23 "copy", "ctrlg", "error", "esc", "ex",
24 "hangul", "insertmode", "lang", "mess",
25 "showmatch", "operator", "register", "shell",
LemonBoy77771d32022-04-13 11:47:25 +010026 "spell", "term", "wildmode", NULL};
Bram Moolenaaraaad9952020-05-31 15:08:59 +020027static char *(p_nf_values[]) = {"bin", "octal", "hex", "alpha", "unsigned", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020028static char *(p_ff_values[]) = {FF_UNIX, FF_DOS, FF_MAC, NULL};
29#ifdef FEAT_CRYPT
Christian Brabandtf573c6e2021-06-20 14:02:16 +020030static char *(p_cm_values[]) = {"zip", "blowfish", "blowfish2",
31 # ifdef FEAT_SODIUM
32 "xchacha20",
33 # endif
34 NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020035#endif
36static char *(p_cmp_values[]) = {"internal", "keepascii", NULL};
37static char *(p_dy_values[]) = {"lastline", "truncate", "uhex", NULL};
38#ifdef FEAT_FOLDING
39static char *(p_fdo_values[]) = {"all", "block", "hor", "mark", "percent",
40 "quickfix", "search", "tag", "insert",
41 "undo", "jump", NULL};
42#endif
43#ifdef FEAT_SESSION
Bram Moolenaar635bd602021-04-16 19:58:22 +020044// Also used for 'viewoptions'! Keep in sync with SSOP_ flags.
Bram Moolenaardac13472019-09-16 21:06:21 +020045static char *(p_ssop_values[]) = {"buffers", "winpos", "resize", "winsize",
46 "localoptions", "options", "help", "blank", "globals", "slash", "unix",
Bram Moolenaar635bd602021-04-16 19:58:22 +020047 "sesdir", "curdir", "folds", "cursor", "tabpages", "terminal", "skiprtp",
48 NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020049#endif
Bram Moolenaar539aa6b2019-11-17 18:09:38 +010050// Keep in sync with SWB_ flags in option.h
51static char *(p_swb_values[]) = {"useopen", "usetab", "split", "newtab", "vsplit", "uselast", NULL};
Luuk van Baal13ece2a2022-10-03 15:28:08 +010052static char *(p_spk_values[]) = {"cursor", "screen", "topline", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020053static char *(p_tc_values[]) = {"followic", "ignore", "match", "followscs", "smart", NULL};
54#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
55static char *(p_toolbar_values[]) = {"text", "icons", "tooltips", "horiz", NULL};
56#endif
57#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
58static char *(p_tbis_values[]) = {"tiny", "small", "medium", "large", "huge", "giant", NULL};
59#endif
Bram Moolenaara1cb1d12019-10-17 23:00:07 +020060#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +020061static char *(p_ttym_values[]) = {"xterm", "xterm2", "dec", "netterm", "jsbterm", "pterm", "urxvt", "sgr", NULL};
62#endif
Gary Johnson53ba05b2021-07-26 22:19:10 +020063static char *(p_ve_values[]) = {"block", "insert", "all", "onemore", "none", "NONE", NULL};
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000064static char *(p_wop_values[]) = {"fuzzy", "tagfile", "pum", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020065#ifdef FEAT_WAK
66static char *(p_wak_values[]) = {"yes", "menu", "no", NULL};
67#endif
68static char *(p_mousem_values[]) = {"extend", "popup", "popup_setpos", "mac", NULL};
69static char *(p_sel_values[]) = {"inclusive", "exclusive", "old", NULL};
70static char *(p_slm_values[]) = {"mouse", "key", "cmd", NULL};
71static char *(p_km_values[]) = {"startsel", "stopsel", NULL};
72#ifdef FEAT_BROWSE
73static char *(p_bsdir_values[]) = {"current", "last", "buffer", NULL};
74#endif
75static char *(p_scbopt_values[]) = {"ver", "hor", "jump", NULL};
76static char *(p_debug_values[]) = {"msg", "throw", "beep", NULL};
77static char *(p_ead_values[]) = {"both", "ver", "hor", NULL};
78static char *(p_buftype_values[]) = {"nofile", "nowrite", "quickfix", "help", "terminal", "acwrite", "prompt", "popup", NULL};
79static char *(p_bufhidden_values[]) = {"hide", "unload", "delete", "wipe", NULL};
Bram Moolenaaraa0489e2020-04-17 19:41:21 +020080static char *(p_bs_values[]) = {"indent", "eol", "start", "nostop", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020081#ifdef FEAT_FOLDING
82static char *(p_fdm_values[]) = {"manual", "expr", "marker", "indent", "syntax",
83# ifdef FEAT_DIFF
84 "diff",
85# endif
86 NULL};
87static char *(p_fcl_values[]) = {"all", NULL};
88#endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +020089static char *(p_cot_values[]) = {"menu", "menuone", "longest", "preview", "popup", "popuphidden", "noinsert", "noselect", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020090#ifdef BACKSLASH_IN_FILENAME
91static char *(p_csl_values[]) = {"slash", "backslash", NULL};
92#endif
93#ifdef FEAT_SIGNS
94static char *(p_scl_values[]) = {"yes", "no", "auto", "number", NULL};
95#endif
96#if defined(MSWIN) && defined(FEAT_TERMINAL)
97static char *(p_twt_values[]) = {"winpty", "conpty", "", NULL};
98#endif
Luuk van Baalba936f62022-12-15 13:15:39 +000099static char *(p_sloc_values[]) = {"last", "statusline", "tabline", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200100
101static int check_opt_strings(char_u *val, char **values, int list);
102static int opt_strings_flags(char_u *val, char **values, unsigned *flagp, int list);
103
104/*
105 * After setting various option values: recompute variables that depend on
106 * option values.
107 */
108 void
109didset_string_options(void)
110{
111 (void)opt_strings_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
112 (void)opt_strings_flags(p_bkc, p_bkc_values, &bkc_flags, TRUE);
113 (void)opt_strings_flags(p_bo, p_bo_values, &bo_flags, TRUE);
114#ifdef FEAT_SESSION
115 (void)opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE);
116 (void)opt_strings_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
117#endif
118#ifdef FEAT_FOLDING
119 (void)opt_strings_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
120#endif
121 (void)opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE);
122 (void)opt_strings_flags(p_tc, p_tc_values, &tc_flags, FALSE);
123 (void)opt_strings_flags(p_ve, p_ve_values, &ve_flags, TRUE);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +0200124#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +0200125 (void)opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE);
126#endif
127#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
128 (void)opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags, TRUE);
129#endif
130#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
131 (void)opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE);
132#endif
Sean Dewar39c46b42022-05-12 17:44:29 +0100133 (void)opt_strings_flags(p_swb, p_swb_values, &swb_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200134}
135
136#if defined(FEAT_EVAL)
137/*
138 * Trigger the OptionSet autocommand.
139 * "opt_idx" is the index of the option being set.
140 * "opt_flags" can be OPT_LOCAL etc.
141 * "oldval" the old value
142 * "oldval_l" the old local value (only non-NULL if global and local value
143 * are set)
144 * "oldval_g" the old global value (only non-NULL if global and local value
145 * are set)
146 * "newval" the new value
147 */
148 void
zeertzjq269aa2b2022-11-28 11:36:50 +0000149trigger_optionset_string(
Bram Moolenaardac13472019-09-16 21:06:21 +0200150 int opt_idx,
151 int opt_flags,
152 char_u *oldval,
153 char_u *oldval_l,
154 char_u *oldval_g,
155 char_u *newval)
156{
157 // Don't do this recursively.
158 if (oldval != NULL && newval != NULL
159 && *get_vim_var_str(VV_OPTION_TYPE) == NUL)
160 {
161 char_u buf_type[7];
162
163 sprintf((char *)buf_type, "%s",
164 (opt_flags & OPT_LOCAL) ? "local" : "global");
165 set_vim_var_string(VV_OPTION_OLD, oldval, -1);
166 set_vim_var_string(VV_OPTION_NEW, newval, -1);
167 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
168 if (opt_flags & OPT_LOCAL)
169 {
170 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
171 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
172 }
173 if (opt_flags & OPT_GLOBAL)
174 {
175 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
176 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval, -1);
177 }
178 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
179 {
180 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
181 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval_l, -1);
182 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval_g, -1);
183 }
184 if (opt_flags & OPT_MODELINE)
185 {
186 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
187 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
188 }
189 apply_autocmds(EVENT_OPTIONSET,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000190 get_option_fullname(opt_idx), NULL, FALSE,
Bram Moolenaardac13472019-09-16 21:06:21 +0200191 NULL);
192 reset_v_option_vars();
193 }
194}
195#endif
196
197 static char *
198illegal_char(char *errbuf, int c)
199{
200 if (errbuf == NULL)
201 return "";
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000202 sprintf((char *)errbuf, _(e_illegal_character_str), (char *)transchar(c));
Bram Moolenaardac13472019-09-16 21:06:21 +0200203 return errbuf;
204}
205
206/*
207 * Check string options in a buffer for NULL value.
208 */
209 void
210check_buf_options(buf_T *buf)
211{
212 check_string_option(&buf->b_p_bh);
213 check_string_option(&buf->b_p_bt);
214 check_string_option(&buf->b_p_fenc);
215 check_string_option(&buf->b_p_ff);
216#ifdef FEAT_FIND_ID
217 check_string_option(&buf->b_p_def);
218 check_string_option(&buf->b_p_inc);
219# ifdef FEAT_EVAL
220 check_string_option(&buf->b_p_inex);
221# endif
222#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +0100223#if defined(FEAT_EVAL)
Bram Moolenaardac13472019-09-16 21:06:21 +0200224 check_string_option(&buf->b_p_inde);
225 check_string_option(&buf->b_p_indk);
226#endif
227#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
228 check_string_option(&buf->b_p_bexpr);
229#endif
230#if defined(FEAT_CRYPT)
231 check_string_option(&buf->b_p_cm);
232#endif
233 check_string_option(&buf->b_p_fp);
234#if defined(FEAT_EVAL)
235 check_string_option(&buf->b_p_fex);
236#endif
237#ifdef FEAT_CRYPT
238 check_string_option(&buf->b_p_key);
239#endif
240 check_string_option(&buf->b_p_kp);
241 check_string_option(&buf->b_p_mps);
242 check_string_option(&buf->b_p_fo);
243 check_string_option(&buf->b_p_flp);
244 check_string_option(&buf->b_p_isk);
Bram Moolenaardac13472019-09-16 21:06:21 +0200245 check_string_option(&buf->b_p_com);
Bram Moolenaardac13472019-09-16 21:06:21 +0200246#ifdef FEAT_FOLDING
247 check_string_option(&buf->b_p_cms);
248#endif
249 check_string_option(&buf->b_p_nf);
Bram Moolenaardac13472019-09-16 21:06:21 +0200250 check_string_option(&buf->b_p_qe);
Bram Moolenaardac13472019-09-16 21:06:21 +0200251#ifdef FEAT_SYN_HL
252 check_string_option(&buf->b_p_syn);
253 check_string_option(&buf->b_s.b_syn_isk);
254#endif
255#ifdef FEAT_SPELL
256 check_string_option(&buf->b_s.b_p_spc);
257 check_string_option(&buf->b_s.b_p_spf);
258 check_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +0200259 check_string_option(&buf->b_s.b_p_spo);
Bram Moolenaardac13472019-09-16 21:06:21 +0200260#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200261 check_string_option(&buf->b_p_sua);
Bram Moolenaardac13472019-09-16 21:06:21 +0200262 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);
Bram Moolenaar49846fb2022-10-15 16:05:33 +0100266 check_string_option(&buf->b_p_lop);
Bram Moolenaardac13472019-09-16 21:06:21 +0200267 check_string_option(&buf->b_p_ft);
Bram Moolenaardac13472019-09-16 21:06:21 +0200268 check_string_option(&buf->b_p_cinw);
Bram Moolenaardac13472019-09-16 21:06:21 +0200269 check_string_option(&buf->b_p_cpt);
270#ifdef FEAT_COMPL_FUNC
271 check_string_option(&buf->b_p_cfu);
272 check_string_option(&buf->b_p_ofu);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +0100273 check_string_option(&buf->b_p_tsrfu);
Bram Moolenaardac13472019-09-16 21:06:21 +0200274#endif
275#ifdef FEAT_EVAL
276 check_string_option(&buf->b_p_tfu);
277#endif
278#ifdef FEAT_KEYMAP
279 check_string_option(&buf->b_p_keymap);
280#endif
281#ifdef FEAT_QUICKFIX
282 check_string_option(&buf->b_p_gp);
283 check_string_option(&buf->b_p_mp);
284 check_string_option(&buf->b_p_efm);
285#endif
286 check_string_option(&buf->b_p_ep);
287 check_string_option(&buf->b_p_path);
288 check_string_option(&buf->b_p_tags);
289 check_string_option(&buf->b_p_tc);
290 check_string_option(&buf->b_p_dict);
291 check_string_option(&buf->b_p_tsr);
Bram Moolenaardac13472019-09-16 21:06:21 +0200292 check_string_option(&buf->b_p_lw);
Bram Moolenaardac13472019-09-16 21:06:21 +0200293 check_string_option(&buf->b_p_bkc);
294 check_string_option(&buf->b_p_menc);
295#ifdef FEAT_VARTABS
296 check_string_option(&buf->b_p_vsts);
297 check_string_option(&buf->b_p_vts);
298#endif
299}
300
301/*
302 * Free the string allocated for an option.
303 * Checks for the string being empty_option. This may happen if we're out of
304 * memory, vim_strsave() returned NULL, which was replaced by empty_option by
305 * check_options().
306 * Does NOT check for P_ALLOCED flag!
307 */
308 void
309free_string_option(char_u *p)
310{
311 if (p != empty_option)
312 vim_free(p);
313}
314
315 void
316clear_string_option(char_u **pp)
317{
318 if (*pp != empty_option)
319 vim_free(*pp);
320 *pp = empty_option;
321}
322
323 void
324check_string_option(char_u **pp)
325{
326 if (*pp == NULL)
327 *pp = empty_option;
328}
329
330/*
331 * Set global value for string option when it's a local option.
332 */
333 static void
334set_string_option_global(
335 int opt_idx, // option index
336 char_u **varp) // pointer to option variable
337{
338 char_u **p, *s;
339
340 // the global value is always allocated
341 if (is_window_local_option(opt_idx))
342 p = (char_u **)GLOBAL_WO(varp);
343 else
344 p = (char_u **)get_option_var(opt_idx);
345 if (!is_global_option(opt_idx)
346 && p != varp
347 && (s = vim_strsave(*varp)) != NULL)
348 {
349 free_string_option(*p);
350 *p = s;
351 }
352}
353
354/*
355 * Set a string option to a new value (without checking the effect).
356 * The string is copied into allocated memory.
357 * if ("opt_idx" == -1) "name" is used, otherwise "opt_idx" is used.
358 * When "set_sid" is zero set the scriptID to current_sctx.sc_sid. When
359 * "set_sid" is SID_NONE don't set the scriptID. Otherwise set the scriptID to
360 * "set_sid".
361 */
362 void
363set_string_option_direct(
364 char_u *name,
365 int opt_idx,
366 char_u *val,
367 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
368 int set_sid UNUSED)
369{
370 char_u *s;
371 char_u **varp;
372 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
373 int idx = opt_idx;
374
375 if (idx == -1) // use name
376 {
377 idx = findoption(name);
378 if (idx < 0) // not found (should not happen)
379 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000380 semsg(_(e_internal_error_str), "set_string_option_direct()");
Bram Moolenaardac13472019-09-16 21:06:21 +0200381 siemsg(_("For option %s"), name);
382 return;
383 }
384 }
385
386 if (is_hidden_option(idx)) // can't set hidden option
387 return;
388
389 s = vim_strsave(val);
390 if (s != NULL)
391 {
392 varp = (char_u **)get_option_varp_scope(idx,
393 both ? OPT_LOCAL : opt_flags);
394 if ((opt_flags & OPT_FREE) && (get_option_flags(idx) & P_ALLOCED))
395 free_string_option(*varp);
396 *varp = s;
397
398 // For buffer/window local option may also set the global value.
399 if (both)
400 set_string_option_global(idx, varp);
401
402 set_option_flag(idx, P_ALLOCED);
403
404 // When setting both values of a global option with a local value,
405 // make the local value empty, so that the global value is used.
406 if (is_global_local_option(idx) && both)
407 {
408 free_string_option(*varp);
409 *varp = empty_option;
410 }
411# ifdef FEAT_EVAL
412 if (set_sid != SID_NONE)
413 {
414 sctx_T script_ctx;
415
416 if (set_sid == 0)
417 script_ctx = current_sctx;
418 else
419 {
420 script_ctx.sc_sid = set_sid;
421 script_ctx.sc_seq = 0;
422 script_ctx.sc_lnum = 0;
423 script_ctx.sc_version = 1;
424 }
425 set_option_sctx_idx(idx, opt_flags, script_ctx);
426 }
427# endif
428 }
429}
430
431/*
432 * Like set_string_option_direct(), but for a window-local option in "wp".
433 * Blocks autocommands to avoid the old curwin becoming invalid.
434 */
435 void
436set_string_option_direct_in_win(
437 win_T *wp,
438 char_u *name,
439 int opt_idx,
440 char_u *val,
441 int opt_flags,
442 int set_sid)
443{
444 win_T *save_curwin = curwin;
445
446 block_autocmds();
447 curwin = wp;
448 curbuf = curwin->w_buffer;
449 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
450 curwin = save_curwin;
451 curbuf = curwin->w_buffer;
452 unblock_autocmds();
453}
454
Dominique Pelle748b3082022-01-08 12:41:16 +0000455#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200456/*
457 * Like set_string_option_direct(), but for a buffer-local option in "buf".
458 * Blocks autocommands to avoid the old curbuf becoming invalid.
459 */
460 void
461set_string_option_direct_in_buf(
462 buf_T *buf,
463 char_u *name,
464 int opt_idx,
465 char_u *val,
466 int opt_flags,
467 int set_sid)
468{
469 buf_T *save_curbuf = curbuf;
470
471 block_autocmds();
472 curbuf = buf;
473 curwin->w_buffer = curbuf;
474 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
475 curbuf = save_curbuf;
476 curwin->w_buffer = curbuf;
477 unblock_autocmds();
478}
Dominique Pelle748b3082022-01-08 12:41:16 +0000479#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200480
481/*
482 * Set a string option to a new value, and handle the effects.
483 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100484 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaardac13472019-09-16 21:06:21 +0200485 */
486 char *
487set_string_option(
488 int opt_idx,
489 char_u *value,
490 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
491{
492 char_u *s;
493 char_u **varp;
494 char_u *oldval;
495#if defined(FEAT_EVAL)
496 char_u *oldval_l = NULL;
497 char_u *oldval_g = NULL;
498 char_u *saved_oldval = NULL;
499 char_u *saved_oldval_l = NULL;
500 char_u *saved_oldval_g = NULL;
501 char_u *saved_newval = NULL;
502#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100503 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +0200504 int value_checked = FALSE;
505
506 if (is_hidden_option(opt_idx)) // don't set hidden option
507 return NULL;
508
Bram Moolenaar7f009df2020-03-16 20:27:38 +0100509 s = vim_strsave(value == NULL ? (char_u *)"" : value);
Bram Moolenaardac13472019-09-16 21:06:21 +0200510 if (s != NULL)
511 {
512 varp = (char_u **)get_option_varp_scope(opt_idx,
513 (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
514 ? (is_global_local_option(opt_idx)
515 ? OPT_GLOBAL : OPT_LOCAL)
516 : opt_flags);
517 oldval = *varp;
518#if defined(FEAT_EVAL)
519 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
520 {
521 oldval_l = *(char_u **)get_option_varp_scope(opt_idx, OPT_LOCAL);
522 oldval_g = *(char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
523 }
524#endif
525 *varp = s;
526
527#if defined(FEAT_EVAL)
528 if (!starting
529# ifdef FEAT_CRYPT
530 && !is_crypt_key_option(opt_idx)
531# endif
532 )
533 {
534 if (oldval_l != NULL)
535 saved_oldval_l = vim_strsave(oldval_l);
536 if (oldval_g != NULL)
537 saved_oldval_g = vim_strsave(oldval_g);
538 saved_oldval = vim_strsave(oldval);
539 saved_newval = vim_strsave(s);
540 }
541#endif
zeertzjqf6782732022-07-27 18:26:03 +0100542 if ((errmsg = did_set_string_option(opt_idx, varp, oldval, NULL,
Bram Moolenaardac13472019-09-16 21:06:21 +0200543 opt_flags, &value_checked)) == NULL)
544 did_set_option(opt_idx, opt_flags, TRUE, value_checked);
545
546#if defined(FEAT_EVAL)
547 // call autocommand after handling side effects
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100548 if (errmsg == NULL)
zeertzjq269aa2b2022-11-28 11:36:50 +0000549 trigger_optionset_string(opt_idx, opt_flags,
Bram Moolenaardac13472019-09-16 21:06:21 +0200550 saved_oldval, saved_oldval_l,
551 saved_oldval_g, saved_newval);
552 vim_free(saved_oldval);
553 vim_free(saved_oldval_l);
554 vim_free(saved_oldval_g);
555 vim_free(saved_newval);
556#endif
557 }
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100558 return errmsg;
Bram Moolenaardac13472019-09-16 21:06:21 +0200559}
560
561/*
562 * Return TRUE if "val" is a valid 'filetype' name.
563 * Also used for 'syntax' and 'keymap'.
564 */
565 static int
566valid_filetype(char_u *val)
567{
568 return valid_name(val, ".-_");
569}
570
571#ifdef FEAT_STL_OPT
572/*
573 * Check validity of options with the 'statusline' format.
zeertzjq5dc294a2022-04-15 13:17:57 +0100574 * Return an untranslated error message or NULL.
Bram Moolenaardac13472019-09-16 21:06:21 +0200575 */
576 static char *
577check_stl_option(char_u *s)
578{
Bram Moolenaardac13472019-09-16 21:06:21 +0200579 int groupdepth = 0;
580 static char errbuf[80];
581
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100582 while (*s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200583 {
584 // Check for valid keys after % sequences
585 while (*s && *s != '%')
586 s++;
587 if (!*s)
588 break;
589 s++;
Bram Moolenaardac13472019-09-16 21:06:21 +0200590 if (*s == '%' || *s == STL_TRUNCMARK || *s == STL_MIDDLEMARK)
591 {
592 s++;
593 continue;
594 }
595 if (*s == ')')
596 {
597 s++;
598 if (--groupdepth < 0)
599 break;
600 continue;
601 }
602 if (*s == '-')
603 s++;
604 while (VIM_ISDIGIT(*s))
605 s++;
606 if (*s == STL_USER_HL)
607 continue;
608 if (*s == '.')
609 {
610 s++;
611 while (*s && VIM_ISDIGIT(*s))
612 s++;
613 }
614 if (*s == '(')
615 {
616 groupdepth++;
617 continue;
618 }
619 if (vim_strchr(STL_ALL, *s) == NULL)
620 {
621 return illegal_char(errbuf, *s);
622 }
623 if (*s == '{')
624 {
zeertzjq5dc294a2022-04-15 13:17:57 +0100625 int reevaluate = (*++s == '%');
shadmansaleh30e3de22021-05-15 17:23:28 +0200626
zeertzjq5dc294a2022-04-15 13:17:57 +0100627 if (reevaluate && *++s == '}')
628 // "}" is not allowed immediately after "%{%"
629 return illegal_char(errbuf, '}');
shadmansaleh30e3de22021-05-15 17:23:28 +0200630 while ((*s != '}' || (reevaluate && s[-1] != '%')) && *s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200631 s++;
632 if (*s != '}')
zeertzjq5dc294a2022-04-15 13:17:57 +0100633 return e_unclosed_expression_sequence;
Bram Moolenaardac13472019-09-16 21:06:21 +0200634 }
635 }
Bram Moolenaardac13472019-09-16 21:06:21 +0200636 if (groupdepth != 0)
zeertzjq5dc294a2022-04-15 13:17:57 +0100637 return e_unbalanced_groups;
Bram Moolenaardac13472019-09-16 21:06:21 +0200638 return NULL;
639}
640#endif
641
642/*
643 * Handle string options that need some action to perform when changed.
zeertzjqf6782732022-07-27 18:26:03 +0100644 * The new value must be allocated.
LemonBoy77142312022-04-15 20:50:46 +0100645 * Returns NULL for success, or an unstranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +0200646 */
647 char *
648did_set_string_option(
649 int opt_idx, // index in options[] table
650 char_u **varp, // pointer to the option variable
Bram Moolenaardac13472019-09-16 21:06:21 +0200651 char_u *oldval, // previous value of the option
652 char *errbuf, // buffer for errors, or NULL
653 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
654 int *value_checked) // value was checked to be save, no
655 // need to set P_INSECURE
656{
657 char *errmsg = NULL;
658 char_u *s, *p;
659 int did_chartab = FALSE;
660 char_u **gvarp;
661 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
662#ifdef FEAT_GUI
663 // set when changing an option that only requires a redraw in the GUI
664 int redraw_gui_only = FALSE;
665#endif
666 int value_changed = FALSE;
667#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
668 int did_swaptcap = FALSE;
669#endif
670
671 // Get the global option to compare with, otherwise we would have to check
672 // two values for all local options.
673 gvarp = (char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
674
675 // Disallow changing some options from secure mode
676 if ((secure
677#ifdef HAVE_SANDBOX
678 || sandbox != 0
679#endif
680 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +0000681 errmsg = e_not_allowed_here;
Bram Moolenaardac13472019-09-16 21:06:21 +0200682
683 // Check for a "normal" directory or file name in some options. Disallow a
684 // path separator (slash and/or backslash), wildcards and characters that
685 // are often illegal in a file name. Be more permissive if "secure" is off.
686 else if (((get_option_flags(opt_idx) & P_NFNAME)
687 && vim_strpbrk(*varp, (char_u *)(secure
688 ? "/\\*?[|;&<>\r\n" : "/\\*?[<>\r\n")) != NULL)
689 || ((get_option_flags(opt_idx) & P_NDNAME)
690 && vim_strpbrk(*varp, (char_u *)"*?[|;&<>\r\n") != NULL))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000691 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200692
693 // 'term'
694 else if (varp == &T_NAME)
695 {
696 if (T_NAME[0] == NUL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000697 errmsg = e_cannot_set_term_to_empty_string;
Bram Moolenaardac13472019-09-16 21:06:21 +0200698#ifdef FEAT_GUI
Bram Moolenaar5daa9112021-02-01 18:39:47 +0100699 else if (gui.in_use)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000700 errmsg = e_cannot_change_term_in_GUI;
Bram Moolenaardac13472019-09-16 21:06:21 +0200701 else if (term_is_gui(T_NAME))
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000702 errmsg = e_use_gui_to_start_GUI;
Bram Moolenaardac13472019-09-16 21:06:21 +0200703#endif
704 else if (set_termname(T_NAME) == FAIL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000705 errmsg = e_not_found_in_termcap;
Bram Moolenaardac13472019-09-16 21:06:21 +0200706 else
707 {
708 // Screen colors may have changed.
709 redraw_later_clear();
710
711 // Both 'term' and 'ttytype' point to T_NAME, only set the
712 // P_ALLOCED flag on 'term'.
713 opt_idx = findoption((char_u *)"term");
714 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
715 }
716 }
717
718 // 'backupcopy'
719 else if (gvarp == &p_bkc)
720 {
721 char_u *bkc = p_bkc;
722 unsigned int *flags = &bkc_flags;
723
724 if (opt_flags & OPT_LOCAL)
725 {
726 bkc = curbuf->b_p_bkc;
727 flags = &curbuf->b_bkc_flags;
728 }
729
730 if ((opt_flags & OPT_LOCAL) && *bkc == NUL)
731 // make the local value empty: use the global value
732 *flags = 0;
733 else
734 {
735 if (opt_strings_flags(bkc, p_bkc_values, flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000736 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200737 if ((((int)*flags & BKC_AUTO) != 0)
738 + (((int)*flags & BKC_YES) != 0)
739 + (((int)*flags & BKC_NO) != 0) != 1)
740 {
741 // Must have exactly one of "auto", "yes" and "no".
742 (void)opt_strings_flags(oldval, p_bkc_values, flags, TRUE);
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000743 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200744 }
745 }
746 }
747
748 // 'backupext' and 'patchmode'
749 else if (varp == &p_bex || varp == &p_pm)
750 {
751 if (STRCMP(*p_bex == '.' ? p_bex + 1 : p_bex,
752 *p_pm == '.' ? p_pm + 1 : p_pm) == 0)
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100753 errmsg = e_backupext_and_patchmode_are_equal;
Bram Moolenaardac13472019-09-16 21:06:21 +0200754 }
755#ifdef FEAT_LINEBREAK
756 // 'breakindentopt'
757 else if (varp == &curwin->w_p_briopt)
758 {
759 if (briopt_check(curwin) == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000760 errmsg = e_invalid_argument;
Bram Moolenaarb2d85e32022-01-07 16:55:32 +0000761 // list setting requires a redraw
762 if (curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100763 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaardac13472019-09-16 21:06:21 +0200764 }
765#endif
766
767 // 'isident', 'iskeyword', 'isprint or 'isfname' option: refill g_chartab[]
768 // If the new option is invalid, use old value. 'lisp' option: refill
769 // g_chartab[] for '-' char
770 else if ( varp == &p_isi
771 || varp == &(curbuf->b_p_isk)
772 || varp == &p_isp
773 || varp == &p_isf)
774 {
775 if (init_chartab() == FAIL)
776 {
777 did_chartab = TRUE; // need to restore it below
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000778 errmsg = e_invalid_argument; // error in value
Bram Moolenaardac13472019-09-16 21:06:21 +0200779 }
780 }
781
782 // 'helpfile'
783 else if (varp == &p_hf)
784 {
785 // May compute new values for $VIM and $VIMRUNTIME
786 if (didset_vim)
LemonBoy77142312022-04-15 20:50:46 +0100787 vim_unsetenv_ext((char_u *)"VIM");
Bram Moolenaardac13472019-09-16 21:06:21 +0200788 if (didset_vimruntime)
LemonBoy77142312022-04-15 20:50:46 +0100789 vim_unsetenv_ext((char_u *)"VIMRUNTIME");
Bram Moolenaardac13472019-09-16 21:06:21 +0200790 }
791
792#ifdef FEAT_SYN_HL
793 // 'cursorlineopt'
794 else if (varp == &curwin->w_p_culopt
795 || gvarp == &curwin->w_allbuf_opt.wo_culopt)
796 {
797 if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000798 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200799 }
800
801 // 'colorcolumn'
802 else if (varp == &curwin->w_p_cc)
803 errmsg = check_colorcolumn(curwin);
804#endif
805
806#ifdef FEAT_MULTI_LANG
807 // 'helplang'
808 else if (varp == &p_hlg)
809 {
810 // Check for "", "ab", "ab,cd", etc.
811 for (s = p_hlg; *s != NUL; s += 3)
812 {
813 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
814 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000815 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200816 break;
817 }
818 if (s[2] == NUL)
819 break;
820 }
821 }
822#endif
823
824 // 'highlight'
825 else if (varp == &p_hl)
826 {
827 if (highlight_changed() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000828 errmsg = e_invalid_argument; // invalid flags
Bram Moolenaardac13472019-09-16 21:06:21 +0200829 }
830
831 // 'nrformats'
832 else if (gvarp == &p_nf)
833 {
834 if (check_opt_strings(*varp, p_nf_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000835 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200836 }
837
838#ifdef FEAT_SESSION
839 // 'sessionoptions'
840 else if (varp == &p_ssop)
841 {
842 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000843 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200844 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
845 {
846 // Don't allow both "sesdir" and "curdir".
847 (void)opt_strings_flags(oldval, p_ssop_values, &ssop_flags, TRUE);
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000848 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200849 }
850 }
851 // 'viewoptions'
852 else if (varp == &p_vop)
853 {
854 if (opt_strings_flags(p_vop, p_ssop_values, &vop_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000855 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200856 }
857#endif
858
859 // 'scrollopt'
860 else if (varp == &p_sbo)
861 {
862 if (check_opt_strings(p_sbo, p_scbopt_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000863 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200864 }
865
866 // 'ambiwidth'
867 else if (varp == &p_ambw || varp == &p_emoji)
868 {
869 if (check_opt_strings(p_ambw, p_ambw_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000870 errmsg = e_invalid_argument;
Bram Moolenaareed9d462021-02-15 20:38:25 +0100871 else
zeertzjq8ca29b62022-08-09 12:53:14 +0100872 errmsg = check_chars_options();
Bram Moolenaardac13472019-09-16 21:06:21 +0200873 }
874
875 // 'background'
876 else if (varp == &p_bg)
877 {
878 if (check_opt_strings(p_bg, p_bg_values, FALSE) == OK)
879 {
880#ifdef FEAT_EVAL
881 int dark = (*p_bg == 'd');
882#endif
883
884 init_highlight(FALSE, FALSE);
885
886#ifdef FEAT_EVAL
887 if (dark != (*p_bg == 'd')
888 && get_var_value((char_u *)"g:colors_name") != NULL)
889 {
890 // The color scheme must have set 'background' back to another
891 // value, that's not what we want here. Disable the color
892 // scheme and set the colors again.
893 do_unlet((char_u *)"g:colors_name", TRUE);
894 free_string_option(p_bg);
895 p_bg = vim_strsave((char_u *)(dark ? "dark" : "light"));
896 check_string_option(&p_bg);
897 init_highlight(FALSE, FALSE);
898 }
899#endif
Bram Moolenaarad431992021-05-03 20:40:38 +0200900#ifdef FEAT_TERMINAL
901 term_update_colors_all();
902#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200903 }
904 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000905 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200906 }
907
908 // 'wildmode'
909 else if (varp == &p_wim)
910 {
911 if (check_opt_wim() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000912 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200913 }
914
915 // 'wildoptions'
916 else if (varp == &p_wop)
917 {
918 if (check_opt_strings(p_wop, p_wop_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000919 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200920 }
921
922#ifdef FEAT_WAK
923 // 'winaltkeys'
924 else if (varp == &p_wak)
925 {
926 if (*p_wak == NUL
927 || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000928 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200929# ifdef FEAT_MENU
ichizok02560422022-04-05 14:18:44 +0100930# if defined(FEAT_GUI_MOTIF)
Bram Moolenaardac13472019-09-16 21:06:21 +0200931 else if (gui.in_use)
932 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
ichizok02560422022-04-05 14:18:44 +0100933# elif defined(FEAT_GUI_GTK)
Bram Moolenaardac13472019-09-16 21:06:21 +0200934 else if (gui.in_use)
935 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
Bram Moolenaardac13472019-09-16 21:06:21 +0200936# endif
937# endif
938 }
939#endif
940
941 // 'eventignore'
942 else if (varp == &p_ei)
943 {
944 if (check_ei() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000945 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200946 }
947
948 // 'encoding', 'fileencoding', 'termencoding' and 'makeencoding'
949 else if (varp == &p_enc || gvarp == &p_fenc || varp == &p_tenc
950 || gvarp == &p_menc)
951 {
952 if (gvarp == &p_fenc)
953 {
954 if (!curbuf->b_p_ma && opt_flags != OPT_GLOBAL)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200955 errmsg = e_cannot_make_changes_modifiable_is_off;
Bram Moolenaardac13472019-09-16 21:06:21 +0200956 else if (vim_strchr(*varp, ',') != NULL)
957 // No comma allowed in 'fileencoding'; catches confusing it
958 // with 'fileencodings'.
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000959 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +0200960 else
961 {
Bram Moolenaardac13472019-09-16 21:06:21 +0200962 // May show a "+" in the title now.
963 redraw_titles();
Bram Moolenaardac13472019-09-16 21:06:21 +0200964 // Add 'fileencoding' to the swap file.
965 ml_setflags(curbuf);
966 }
967 }
968 if (errmsg == NULL)
969 {
970 // canonize the value, so that STRCMP() can be used on it
971 p = enc_canonize(*varp);
972 if (p != NULL)
973 {
974 vim_free(*varp);
975 *varp = p;
976 }
977 if (varp == &p_enc)
978 {
979 errmsg = mb_init();
Bram Moolenaardac13472019-09-16 21:06:21 +0200980 redraw_titles();
Bram Moolenaardac13472019-09-16 21:06:21 +0200981 }
982 }
983
984#if defined(FEAT_GUI_GTK)
985 if (errmsg == NULL && varp == &p_tenc && gui.in_use)
986 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +0000987 // GTK uses only a single encoding, and that is UTF-8.
Bram Moolenaardac13472019-09-16 21:06:21 +0200988 if (STRCMP(p_tenc, "utf-8") != 0)
Bram Moolenaard88be5b2022-01-04 19:57:55 +0000989 errmsg = e_cannot_be_changed_in_gtk_GUI;
Bram Moolenaardac13472019-09-16 21:06:21 +0200990 }
991#endif
992
993 if (errmsg == NULL)
994 {
995#ifdef FEAT_KEYMAP
996 // When 'keymap' is used and 'encoding' changes, reload the keymap
997 // (with another encoding).
998 if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
999 (void)keymap_init();
1000#endif
1001
1002 // When 'termencoding' is not empty and 'encoding' changes or when
1003 // 'termencoding' changes, need to setup for keyboard input and
1004 // display output conversion.
1005 if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
1006 {
1007 if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
1008 || convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
1009 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001010 semsg(_(e_cannot_convert_between_str_and_str),
1011 p_tenc, p_enc);
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001012 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001013 }
1014 }
1015
1016#if defined(MSWIN)
1017 // $HOME may have characters in active code page.
1018 if (varp == &p_enc)
1019 init_homedir();
1020#endif
1021 }
1022 }
1023
1024#if defined(FEAT_POSTSCRIPT)
1025 else if (varp == &p_penc)
1026 {
1027 // Canonize printencoding if VIM standard one
1028 p = enc_canonize(p_penc);
1029 if (p != NULL)
1030 {
1031 vim_free(p_penc);
1032 p_penc = p;
1033 }
1034 else
1035 {
1036 // Ensure lower case and '-' for '_'
1037 for (s = p_penc; *s != NUL; s++)
1038 {
1039 if (*s == '_')
1040 *s = '-';
1041 else
1042 *s = TOLOWER_ASC(*s);
1043 }
1044 }
1045 }
1046#endif
1047
1048#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
1049 else if (varp == &p_imak)
1050 {
1051 if (!im_xim_isvalid_imactivate())
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001052 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001053 }
1054#endif
1055
1056#ifdef FEAT_KEYMAP
1057 else if (varp == &curbuf->b_p_keymap)
1058 {
1059 if (!valid_filetype(*varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001060 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001061 else
1062 {
1063 int secure_save = secure;
1064
1065 // Reset the secure flag, since the value of 'keymap' has
1066 // been checked to be safe.
1067 secure = 0;
1068
1069 // load or unload key mapping tables
1070 errmsg = keymap_init();
1071
1072 secure = secure_save;
1073
1074 // Since we check the value, there is no need to set P_INSECURE,
1075 // even when the value comes from a modeline.
1076 *value_checked = TRUE;
1077 }
1078
1079 if (errmsg == NULL)
1080 {
1081 if (*curbuf->b_p_keymap != NUL)
1082 {
1083 // Installed a new keymap, switch on using it.
1084 curbuf->b_p_iminsert = B_IMODE_LMAP;
1085 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
1086 curbuf->b_p_imsearch = B_IMODE_LMAP;
1087 }
1088 else
1089 {
1090 // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
1091 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
1092 curbuf->b_p_iminsert = B_IMODE_NONE;
1093 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
1094 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
1095 }
1096 if ((opt_flags & OPT_LOCAL) == 0)
1097 {
1098 set_iminsert_global();
1099 set_imsearch_global();
1100 }
1101 status_redraw_curbuf();
1102 }
1103 }
1104#endif
1105
1106 // 'fileformat'
1107 else if (gvarp == &p_ff)
1108 {
1109 if (!curbuf->b_p_ma && !(opt_flags & OPT_GLOBAL))
Bram Moolenaar108010a2021-06-27 22:03:33 +02001110 errmsg = e_cannot_make_changes_modifiable_is_off;
Bram Moolenaardac13472019-09-16 21:06:21 +02001111 else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001112 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001113 else
1114 {
1115 // may also change 'textmode'
1116 if (get_fileformat(curbuf) == EOL_DOS)
1117 curbuf->b_p_tx = TRUE;
1118 else
1119 curbuf->b_p_tx = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02001120 redraw_titles();
Bram Moolenaardac13472019-09-16 21:06:21 +02001121 // update flag in swap file
1122 ml_setflags(curbuf);
1123 // Redraw needed when switching to/from "mac": a CR in the text
1124 // will be displayed differently.
1125 if (get_fileformat(curbuf) == EOL_MAC || *oldval == 'm')
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001126 redraw_curbuf_later(UPD_NOT_VALID);
Bram Moolenaardac13472019-09-16 21:06:21 +02001127 }
1128 }
1129
1130 // 'fileformats'
1131 else if (varp == &p_ffs)
1132 {
1133 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001134 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001135 else
1136 {
1137 // also change 'textauto'
1138 if (*p_ffs == NUL)
1139 p_ta = FALSE;
1140 else
1141 p_ta = TRUE;
1142 }
1143 }
1144
1145#if defined(FEAT_CRYPT)
1146 // 'cryptkey'
1147 else if (gvarp == &p_key)
1148 {
1149 // Make sure the ":set" command doesn't show the new value in the
1150 // history.
1151 remove_key_from_history();
1152
1153 if (STRCMP(curbuf->b_p_key, oldval) != 0)
1154 // Need to update the swapfile.
Bram Moolenaar76cb6832020-05-15 22:30:38 +02001155 {
Bram Moolenaardac13472019-09-16 21:06:21 +02001156 ml_set_crypt_key(curbuf, oldval,
1157 *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
Bram Moolenaar76cb6832020-05-15 22:30:38 +02001158 changed_internal();
1159 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001160 }
1161
1162 else if (gvarp == &p_cm)
1163 {
1164 if (opt_flags & OPT_LOCAL)
1165 p = curbuf->b_p_cm;
1166 else
1167 p = p_cm;
1168 if (check_opt_strings(p, p_cm_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001169 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001170 else if (crypt_self_test() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001171 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001172 else
1173 {
1174 // When setting the global value to empty, make it "zip".
1175 if (*p_cm == NUL)
1176 {
zeertzjqf6782732022-07-27 18:26:03 +01001177 free_string_option(p_cm);
Bram Moolenaardac13472019-09-16 21:06:21 +02001178 p_cm = vim_strsave((char_u *)"zip");
Bram Moolenaardac13472019-09-16 21:06:21 +02001179 }
1180 // When using ":set cm=name" the local value is going to be empty.
1181 // Do that here, otherwise the crypt functions will still use the
1182 // local value.
1183 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1184 {
1185 free_string_option(curbuf->b_p_cm);
1186 curbuf->b_p_cm = empty_option;
1187 }
1188
1189 // Need to update the swapfile when the effective method changed.
1190 // Set "s" to the effective old value, "p" to the effective new
1191 // method and compare.
1192 if ((opt_flags & OPT_LOCAL) && *oldval == NUL)
1193 s = p_cm; // was previously using the global value
1194 else
1195 s = oldval;
1196 if (*curbuf->b_p_cm == NUL)
1197 p = p_cm; // is now using the global value
1198 else
1199 p = curbuf->b_p_cm;
1200 if (STRCMP(s, p) != 0)
1201 ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1202
1203 // If the global value changes need to update the swapfile for all
1204 // buffers using that value.
1205 if ((opt_flags & OPT_GLOBAL) && STRCMP(p_cm, oldval) != 0)
1206 {
1207 buf_T *buf;
1208
1209 FOR_ALL_BUFFERS(buf)
1210 if (buf != curbuf && *buf->b_p_cm == NUL)
1211 ml_set_crypt_key(buf, buf->b_p_key, oldval);
1212 }
1213 }
1214 }
1215#endif
1216
1217 // 'matchpairs'
1218 else if (gvarp == &p_mps)
1219 {
1220 if (has_mbyte)
1221 {
1222 for (p = *varp; *p != NUL; ++p)
1223 {
1224 int x2 = -1;
1225 int x3 = -1;
1226
=?UTF-8?q?Dundar=20G=C3=B6c?=b8366582022-04-14 20:43:56 +01001227 p += mb_ptr2len(p);
Bram Moolenaardac13472019-09-16 21:06:21 +02001228 if (*p != NUL)
1229 x2 = *p++;
1230 if (*p != NUL)
1231 {
1232 x3 = mb_ptr2char(p);
1233 p += mb_ptr2len(p);
1234 }
1235 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
1236 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001237 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001238 break;
1239 }
1240 if (*p == NUL)
1241 break;
1242 }
1243 }
1244 else
1245 {
1246 // Check for "x:y,x:y"
1247 for (p = *varp; *p != NUL; p += 4)
1248 {
1249 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
1250 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001251 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001252 break;
1253 }
1254 if (p[3] == NUL)
1255 break;
1256 }
1257 }
1258 }
1259
Bram Moolenaardac13472019-09-16 21:06:21 +02001260 // 'comments'
1261 else if (gvarp == &p_com)
1262 {
1263 for (s = *varp; *s; )
1264 {
1265 while (*s && *s != ':')
1266 {
1267 if (vim_strchr((char_u *)COM_ALL, *s) == NULL
1268 && !VIM_ISDIGIT(*s) && *s != '-')
1269 {
1270 errmsg = illegal_char(errbuf, *s);
1271 break;
1272 }
1273 ++s;
1274 }
1275 if (*s++ == NUL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001276 errmsg = e_missing_colon;
Bram Moolenaardac13472019-09-16 21:06:21 +02001277 else if (*s == ',' || *s == NUL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001278 errmsg = e_zero_length_string;
Bram Moolenaardac13472019-09-16 21:06:21 +02001279 if (errmsg != NULL)
1280 break;
1281 while (*s && *s != ',')
1282 {
1283 if (*s == '\\' && s[1] != NUL)
1284 ++s;
1285 ++s;
1286 }
1287 s = skip_to_option_part(s);
1288 }
1289 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001290
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01001291 // global 'listchars' or 'fillchars'
1292 else if (varp == &p_lcs || varp == &p_fcs)
Bram Moolenaardac13472019-09-16 21:06:21 +02001293 {
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01001294 char_u **local_ptr = varp == &p_lcs
1295 ? &curwin->w_p_lcs : &curwin->w_p_fcs;
1296
1297 // only apply the global value to "curwin" when it does not have a
1298 // local value
1299 errmsg = set_chars_option(curwin, varp,
1300 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL));
Bram Moolenaareed9d462021-02-15 20:38:25 +01001301 if (errmsg == NULL)
1302 {
1303 tabpage_T *tp;
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001304 win_T *wp;
Bram Moolenaareed9d462021-02-15 20:38:25 +01001305
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01001306 // If the current window is set to use the global
1307 // 'listchars'/'fillchars' value, clear the window-local value.
Bram Moolenaareed9d462021-02-15 20:38:25 +01001308 if (!(opt_flags & OPT_GLOBAL))
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01001309 clear_string_option(local_ptr);
Bram Moolenaareed9d462021-02-15 20:38:25 +01001310 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01001311 {
Bram Moolenaar5ed26fa2022-07-04 18:05:51 +01001312 // If the current window has a local value need to apply it
1313 // again, it was changed when setting the global value.
Bram Moolenaar606efc72021-11-12 19:52:47 +00001314 // If no error was returned above, we don't expect an error
1315 // here, so ignore the return value.
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01001316 local_ptr = varp == &p_lcs ? &wp->w_p_lcs : &wp->w_p_fcs;
1317 if (**local_ptr == NUL)
1318 (void)set_chars_option(wp, local_ptr, TRUE);
1319 }
Bram Moolenaar606efc72021-11-12 19:52:47 +00001320
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001321 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaareed9d462021-02-15 20:38:25 +01001322 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001323 }
Bram Moolenaareed9d462021-02-15 20:38:25 +01001324 // local 'listchars'
1325 else if (varp == &curwin->w_p_lcs)
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01001326 errmsg = set_chars_option(curwin, varp, TRUE);
Bram Moolenaareed9d462021-02-15 20:38:25 +01001327
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01001328 // local 'fillchars'
1329 else if (varp == &curwin->w_p_fcs)
1330 {
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01001331 errmsg = set_chars_option(curwin, varp, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +02001332 }
1333
Bram Moolenaardac13472019-09-16 21:06:21 +02001334 // 'cedit'
1335 else if (varp == &p_cedit)
1336 {
1337 errmsg = check_cedit();
1338 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001339
1340 // 'verbosefile'
1341 else if (varp == &p_vfile)
1342 {
1343 verbose_stop();
1344 if (*p_vfile != NUL && verbose_open() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001345 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001346 }
1347
1348#ifdef FEAT_VIMINFO
1349 // 'viminfo'
1350 else if (varp == &p_viminfo)
1351 {
1352 for (s = p_viminfo; *s;)
1353 {
1354 // Check it's a valid character
1355 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
1356 {
1357 errmsg = illegal_char(errbuf, *s);
1358 break;
1359 }
1360 if (*s == 'n') // name is always last one
1361 break;
1362 else if (*s == 'r') // skip until next ','
1363 {
1364 while (*++s && *s != ',')
1365 ;
1366 }
1367 else if (*s == '%')
1368 {
1369 // optional number
1370 while (vim_isdigit(*++s))
1371 ;
1372 }
1373 else if (*s == '!' || *s == 'h' || *s == 'c')
1374 ++s; // no extra chars
1375 else // must have a number
1376 {
1377 while (vim_isdigit(*++s))
1378 ;
1379
1380 if (!VIM_ISDIGIT(*(s - 1)))
1381 {
1382 if (errbuf != NULL)
1383 {
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001384 sprintf(errbuf,
1385 _(e_missing_number_after_angle_str_angle),
Bram Moolenaardac13472019-09-16 21:06:21 +02001386 transchar_byte(*(s - 1)));
1387 errmsg = errbuf;
1388 }
1389 else
1390 errmsg = "";
1391 break;
1392 }
1393 }
1394 if (*s == ',')
1395 ++s;
1396 else if (*s)
1397 {
1398 if (errbuf != NULL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001399 errmsg = e_missing_comma;
Bram Moolenaardac13472019-09-16 21:06:21 +02001400 else
1401 errmsg = "";
1402 break;
1403 }
1404 }
1405 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001406 errmsg = e_must_specify_a_value;
Bram Moolenaardac13472019-09-16 21:06:21 +02001407 }
1408#endif // FEAT_VIMINFO
1409
1410 // terminal options
1411 else if (istermoption_idx(opt_idx) && full_screen)
1412 {
1413 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
1414 if (varp == &T_CCO)
1415 {
1416 int colors = atoi((char *)T_CCO);
1417
1418 // Only reinitialize colors if t_Co value has really changed to
1419 // avoid expensive reload of colorscheme if t_Co is set to the
1420 // same value multiple times.
1421 if (colors != t_colors)
1422 {
1423 t_colors = colors;
1424 if (t_colors <= 1)
1425 {
zeertzjqf6782732022-07-27 18:26:03 +01001426 vim_free(T_CCO);
Bram Moolenaardac13472019-09-16 21:06:21 +02001427 T_CCO = empty_option;
1428 }
1429#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1430 if (is_term_win32())
1431 {
1432 swap_tcap();
1433 did_swaptcap = TRUE;
1434 }
1435#endif
1436 // We now have a different color setup, initialize it again.
1437 init_highlight(TRUE, FALSE);
1438 }
1439 }
1440 ttest(FALSE);
1441 if (varp == &T_ME)
1442 {
1443 out_str(T_ME);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001444 redraw_later(UPD_CLEAR);
Bram Moolenaardac13472019-09-16 21:06:21 +02001445#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
1446 // Since t_me has been set, this probably means that the user
1447 // wants to use this as default colors. Need to reset default
1448 // background/foreground colors.
1449# ifdef VIMDLL
1450 if (!gui.in_use && !gui.starting)
1451# endif
1452 mch_set_normal_colors();
1453#endif
1454 }
1455 if (varp == &T_BE && termcap_active)
1456 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +01001457 MAY_WANT_TO_LOG_THIS;
1458
Bram Moolenaardac13472019-09-16 21:06:21 +02001459 if (*T_BE == NUL)
1460 // When clearing t_BE we assume the user no longer wants
1461 // bracketed paste, thus disable it by writing t_BD.
1462 out_str(T_BD);
1463 else
1464 out_str(T_BE);
1465 }
1466 }
1467
1468#ifdef FEAT_LINEBREAK
1469 // 'showbreak'
Bram Moolenaaree857022019-11-09 23:26:40 +01001470 else if (gvarp == &p_sbr)
Bram Moolenaardac13472019-09-16 21:06:21 +02001471 {
Bram Moolenaaree857022019-11-09 23:26:40 +01001472 for (s = *varp; *s; )
Bram Moolenaardac13472019-09-16 21:06:21 +02001473 {
1474 if (ptr2cells(s) != 1)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001475 errmsg = e_showbreak_contains_unprintable_or_wide_character;
Bram Moolenaardac13472019-09-16 21:06:21 +02001476 MB_PTR_ADV(s);
1477 }
1478 }
1479#endif
1480
1481#ifdef FEAT_GUI
1482 // 'guifont'
1483 else if (varp == &p_guifont)
1484 {
1485 if (gui.in_use)
1486 {
1487 p = p_guifont;
1488# if defined(FEAT_GUI_GTK)
1489 // Put up a font dialog and let the user select a new value.
1490 // If this is cancelled go back to the old value but don't
1491 // give an error message.
1492 if (STRCMP(p, "*") == 0)
1493 {
1494 p = gui_mch_font_dialog(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01001495 free_string_option(p_guifont);
Bram Moolenaardac13472019-09-16 21:06:21 +02001496 p_guifont = (p != NULL) ? p : vim_strsave(oldval);
Bram Moolenaardac13472019-09-16 21:06:21 +02001497 }
1498# endif
1499 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
1500 {
1501# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
1502 if (STRCMP(p_guifont, "*") == 0)
1503 {
1504 // Dialog was cancelled: Keep the old value without giving
1505 // an error message.
zeertzjqf6782732022-07-27 18:26:03 +01001506 free_string_option(p_guifont);
Bram Moolenaardac13472019-09-16 21:06:21 +02001507 p_guifont = vim_strsave(oldval);
Bram Moolenaardac13472019-09-16 21:06:21 +02001508 }
1509 else
1510# endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001511 errmsg = e_invalid_fonts;
Bram Moolenaardac13472019-09-16 21:06:21 +02001512 }
1513 }
1514 redraw_gui_only = TRUE;
1515 }
1516# ifdef FEAT_XFONTSET
1517 else if (varp == &p_guifontset)
1518 {
1519 if (STRCMP(p_guifontset, "*") == 0)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001520 errmsg = e_cant_select_fontset;
Bram Moolenaardac13472019-09-16 21:06:21 +02001521 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01001522 errmsg = e_invalid_fontset;
Bram Moolenaardac13472019-09-16 21:06:21 +02001523 redraw_gui_only = TRUE;
1524 }
1525# endif
1526 else if (varp == &p_guifontwide)
1527 {
1528 if (STRCMP(p_guifontwide, "*") == 0)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001529 errmsg = e_cant_select_wide_font;
Bram Moolenaardac13472019-09-16 21:06:21 +02001530 else if (gui_get_wide_font() == FAIL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001531 errmsg = e_invalid_wide_font;
Bram Moolenaardac13472019-09-16 21:06:21 +02001532 redraw_gui_only = TRUE;
1533 }
1534#endif
Dusan Popovic4eeedc02021-10-16 20:52:05 +01001535# if defined(FEAT_GUI_GTK)
1536 else if (varp == &p_guiligatures)
1537 {
1538 gui_set_ligatures();
1539 redraw_gui_only = TRUE;
1540 }
1541# endif
Bram Moolenaardac13472019-09-16 21:06:21 +02001542
1543#ifdef CURSOR_SHAPE
1544 // 'guicursor'
1545 else if (varp == &p_guicursor)
1546 errmsg = parse_shape_opt(SHAPE_CURSOR);
1547#endif
1548
1549#ifdef FEAT_MOUSESHAPE
1550 // 'mouseshape'
1551 else if (varp == &p_mouseshape)
1552 {
1553 errmsg = parse_shape_opt(SHAPE_MOUSE);
1554 update_mouseshape(-1);
1555 }
1556#endif
1557
1558#ifdef FEAT_PRINTER
1559 else if (varp == &p_popt)
1560 errmsg = parse_printoptions();
1561# if defined(FEAT_POSTSCRIPT)
1562 else if (varp == &p_pmfn)
1563 errmsg = parse_printmbfont();
1564# endif
1565#endif
1566
1567#ifdef FEAT_LANGMAP
1568 // 'langmap'
1569 else if (varp == &p_langmap)
1570 langmap_set();
1571#endif
1572
1573#ifdef FEAT_LINEBREAK
1574 // 'breakat'
1575 else if (varp == &p_breakat)
1576 fill_breakat_flags();
1577#endif
1578
Bram Moolenaardac13472019-09-16 21:06:21 +02001579 // 'titlestring' and 'iconstring'
1580 else if (varp == &p_titlestring || varp == &p_iconstring)
1581 {
Bram Moolenaar651fca82021-11-29 20:39:38 +00001582#ifdef FEAT_STL_OPT
Bram Moolenaardac13472019-09-16 21:06:21 +02001583 int flagval = (varp == &p_titlestring) ? STL_IN_TITLE : STL_IN_ICON;
1584
1585 // NULL => statusline syntax
1586 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
1587 stl_syntax |= flagval;
1588 else
1589 stl_syntax &= ~flagval;
Bram Moolenaar651fca82021-11-29 20:39:38 +00001590#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02001591 did_set_title();
1592 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001593
1594#ifdef FEAT_GUI
1595 // 'guioptions'
1596 else if (varp == &p_go)
1597 {
1598 gui_init_which_components(oldval);
1599 redraw_gui_only = TRUE;
1600 }
1601#endif
1602
1603#if defined(FEAT_GUI_TABLINE)
1604 // 'guitablabel'
1605 else if (varp == &p_gtl)
1606 {
1607 redraw_tabline = TRUE;
1608 redraw_gui_only = TRUE;
1609 }
1610 // 'guitabtooltip'
1611 else if (varp == &p_gtt)
1612 {
1613 redraw_gui_only = TRUE;
1614 }
1615#endif
1616
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001617#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +02001618 // 'ttymouse'
1619 else if (varp == &p_ttym)
1620 {
1621 // Switch the mouse off before changing the escape sequences used for
1622 // that.
1623 mch_setmouse(FALSE);
1624 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001625 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001626 else
1627 check_mouse_termcode();
1628 if (termcap_active)
1629 setmouse(); // may switch it on again
1630 }
1631#endif
1632
1633 // 'selection'
1634 else if (varp == &p_sel)
1635 {
1636 if (*p_sel == NUL
1637 || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001638 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001639 }
1640
1641 // 'selectmode'
1642 else if (varp == &p_slm)
1643 {
1644 if (check_opt_strings(p_slm, p_slm_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001645 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001646 }
1647
1648#ifdef FEAT_BROWSE
1649 // 'browsedir'
1650 else if (varp == &p_bsdir)
1651 {
1652 if (check_opt_strings(p_bsdir, p_bsdir_values, FALSE) != OK
1653 && !mch_isdir(p_bsdir))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001654 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001655 }
1656#endif
1657
1658 // 'keymodel'
1659 else if (varp == &p_km)
1660 {
1661 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001662 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001663 else
1664 {
1665 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
1666 km_startsel = (vim_strchr(p_km, 'a') != NULL);
1667 }
1668 }
1669
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001670 // 'keyprotocol'
1671 else if (varp == &p_kpc)
1672 {
1673 if (match_keyprotocol(NULL) == KEYPROTOCOL_FAIL)
1674 errmsg = e_invalid_argument;
1675 }
1676
Bram Moolenaardac13472019-09-16 21:06:21 +02001677 // 'mousemodel'
1678 else if (varp == &p_mousem)
1679 {
1680 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001681 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001682#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
1683 else if (*p_mousem != *oldval)
1684 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
1685 // to create or delete the popup menus.
1686 gui_motif_update_mousemodel(root_menu);
1687#endif
1688 }
1689
1690 // 'switchbuf'
1691 else if (varp == &p_swb)
1692 {
1693 if (opt_strings_flags(p_swb, p_swb_values, &swb_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001694 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001695 }
1696
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001697 // 'splitkeep'
1698 else if (varp == &p_spk)
1699 {
1700 if (check_opt_strings(p_spk, p_spk_values, FALSE) != OK)
1701 errmsg = e_invalid_argument;
1702 }
1703
Bram Moolenaardac13472019-09-16 21:06:21 +02001704 // 'debug'
1705 else if (varp == &p_debug)
1706 {
1707 if (check_opt_strings(p_debug, p_debug_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001708 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001709 }
1710
1711 // 'display'
1712 else if (varp == &p_dy)
1713 {
1714 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001715 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001716 else
1717 (void)init_chartab();
1718
1719 }
1720
1721 // 'eadirection'
1722 else if (varp == &p_ead)
1723 {
1724 if (check_opt_strings(p_ead, p_ead_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001725 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001726 }
1727
1728#ifdef FEAT_CLIPBOARD
1729 // 'clipboard'
1730 else if (varp == &p_cb)
1731 errmsg = check_clipboard_option();
1732#endif
1733
1734#ifdef FEAT_SPELL
1735 // When 'spelllang' or 'spellfile' is set and there is a window for this
1736 // buffer in which 'spell' is set load the wordlists.
1737 else if (varp == &(curwin->w_s->b_p_spl)
1738 || varp == &(curwin->w_s->b_p_spf))
1739 {
1740 int is_spellfile = varp == &(curwin->w_s->b_p_spf);
1741
1742 if ((is_spellfile && !valid_spellfile(*varp))
Luuk van Baal13ece2a2022-10-03 15:28:08 +01001743 || (!is_spellfile && !valid_spelllang(*varp)))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001744 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001745 else
1746 errmsg = did_set_spell_option(is_spellfile);
1747 }
1748 // When 'spellcapcheck' is set compile the regexp program.
1749 else if (varp == &(curwin->w_s->b_p_spc))
1750 {
1751 errmsg = compile_cap_prog(curwin->w_s);
1752 }
Bram Moolenaar362b44b2020-06-10 21:47:00 +02001753 // 'spelloptions'
1754 else if (varp == &(curwin->w_s->b_p_spo))
1755 {
1756 if (**varp != NUL && STRCMP("camel", *varp) != 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001757 errmsg = e_invalid_argument;
Bram Moolenaar362b44b2020-06-10 21:47:00 +02001758 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001759 // 'spellsuggest'
1760 else if (varp == &p_sps)
1761 {
1762 if (spell_check_sps() != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001763 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001764 }
1765 // 'mkspellmem'
1766 else if (varp == &p_msm)
1767 {
1768 if (spell_check_msm() != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001769 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001770 }
1771#endif
1772
1773 // When 'bufhidden' is set, check for valid value.
1774 else if (gvarp == &p_bh)
1775 {
1776 if (check_opt_strings(curbuf->b_p_bh, p_bufhidden_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001777 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001778 }
1779
1780 // When 'buftype' is set, check for valid value.
1781 else if (gvarp == &p_bt)
1782 {
1783 if (check_opt_strings(curbuf->b_p_bt, p_buftype_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001784 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001785 else
1786 {
1787 if (curwin->w_status_height)
1788 {
1789 curwin->w_redr_status = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001790 redraw_later(UPD_VALID);
Bram Moolenaardac13472019-09-16 21:06:21 +02001791 }
1792 curbuf->b_help = (curbuf->b_p_bt[0] == 'h');
Bram Moolenaardac13472019-09-16 21:06:21 +02001793 redraw_titles();
Bram Moolenaardac13472019-09-16 21:06:21 +02001794 }
1795 }
1796
1797#ifdef FEAT_STL_OPT
zeertzjq5dc294a2022-04-15 13:17:57 +01001798 // 'statusline', 'tabline' or 'rulerformat'
1799 else if (gvarp == &p_stl || varp == &p_tal || varp == &p_ruf)
Bram Moolenaardac13472019-09-16 21:06:21 +02001800 {
1801 int wid;
1802
1803 if (varp == &p_ruf) // reset ru_wid first
1804 ru_wid = 0;
1805 s = *varp;
1806 if (varp == &p_ruf && *s == '%')
1807 {
1808 // set ru_wid if 'ruf' starts with "%99("
1809 if (*++s == '-') // ignore a '-'
1810 s++;
1811 wid = getdigits(&s);
1812 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
1813 ru_wid = wid;
1814 else
1815 errmsg = check_stl_option(p_ruf);
1816 }
zeertzjq5dc294a2022-04-15 13:17:57 +01001817 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
Bram Moolenaardac13472019-09-16 21:06:21 +02001818 else if (varp == &p_ruf || s[0] != '%' || s[1] != '!')
1819 errmsg = check_stl_option(s);
1820 if (varp == &p_ruf && errmsg == NULL)
1821 comp_col();
1822 }
1823#endif
1824
1825 // check if it is a valid value for 'complete' -- Acevedo
1826 else if (gvarp == &p_cpt)
1827 {
1828 for (s = *varp; *s;)
1829 {
1830 while (*s == ',' || *s == ' ')
1831 s++;
1832 if (!*s)
1833 break;
1834 if (vim_strchr((char_u *)".wbuksid]tU", *s) == NULL)
1835 {
1836 errmsg = illegal_char(errbuf, *s);
1837 break;
1838 }
1839 if (*++s != NUL && *s != ',' && *s != ' ')
1840 {
1841 if (s[-1] == 'k' || s[-1] == 's')
1842 {
1843 // skip optional filename after 'k' and 's'
1844 while (*s && *s != ',' && *s != ' ')
1845 {
1846 if (*s == '\\' && s[1] != NUL)
1847 ++s;
1848 ++s;
1849 }
1850 }
1851 else
1852 {
1853 if (errbuf != NULL)
1854 {
1855 sprintf((char *)errbuf,
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00001856 _(e_illegal_character_after_chr), *--s);
Bram Moolenaardac13472019-09-16 21:06:21 +02001857 errmsg = errbuf;
1858 }
1859 else
1860 errmsg = "";
1861 break;
1862 }
1863 }
1864 }
1865 }
1866
1867 // 'completeopt'
1868 else if (varp == &p_cot)
1869 {
1870 if (check_opt_strings(p_cot, p_cot_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001871 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001872 else
1873 completeopt_was_set();
1874 }
1875
1876#ifdef BACKSLASH_IN_FILENAME
1877 // 'completeslash'
1878 else if (gvarp == &p_csl)
1879 {
1880 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1881 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001882 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001883 }
1884#endif
1885
1886#ifdef FEAT_SIGNS
1887 // 'signcolumn'
1888 else if (varp == &curwin->w_p_scl)
1889 {
1890 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001891 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001892 // When changing the 'signcolumn' to or from 'number', recompute the
1893 // width of the number column if 'number' or 'relativenumber' is set.
1894 if (((*oldval == 'n' && *(oldval + 1) == 'u')
1895 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
1896 && (curwin->w_p_nu || curwin->w_p_rnu))
1897 curwin->w_nrwidth_line_count = 0;
1898 }
1899#endif
1900
Luuk van Baalba936f62022-12-15 13:15:39 +00001901 // 'showcmdloc'
1902 else if (varp == &p_sloc)
1903 {
1904 if (check_opt_strings(p_sloc, p_sloc_values, FALSE) != OK)
1905 errmsg = e_invalid_argument;
1906 }
Bram Moolenaardac13472019-09-16 21:06:21 +02001907
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 {
zeertzjqf6782732022-07-27 18:26:03 +01001948 free_string_option(p_pt);
Bram Moolenaardac13472019-09-16 21:06:21 +02001949 p_pt = p;
Bram Moolenaardac13472019-09-16 21:06:21 +02001950 }
1951 }
1952 }
1953
1954 // 'backspace'
1955 else if (varp == &p_bs)
1956 {
1957 if (VIM_ISDIGIT(*p_bs))
1958 {
Bram Moolenaaraa0489e2020-04-17 19:41:21 +02001959 if (*p_bs > '3' || p_bs[1] != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001960 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001961 }
1962 else if (check_opt_strings(p_bs, p_bs_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001963 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001964 }
1965 else if (varp == &p_bo)
1966 {
1967 if (opt_strings_flags(p_bo, p_bo_values, &bo_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001968 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001969 }
1970
1971 // 'tagcase'
1972 else if (gvarp == &p_tc)
1973 {
1974 unsigned int *flags;
1975
1976 if (opt_flags & OPT_LOCAL)
1977 {
1978 p = curbuf->b_p_tc;
1979 flags = &curbuf->b_tc_flags;
1980 }
1981 else
1982 {
1983 p = p_tc;
1984 flags = &tc_flags;
1985 }
1986
1987 if ((opt_flags & OPT_LOCAL) && *p == NUL)
1988 // make the local value empty: use the global value
1989 *flags = 0;
1990 else if (*p == NUL
1991 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001992 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02001993 }
1994
1995 // 'casemap'
1996 else if (varp == &p_cmp)
1997 {
1998 if (opt_strings_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001999 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002000 }
2001
2002#ifdef FEAT_DIFF
2003 // 'diffopt'
2004 else if (varp == &p_dip)
2005 {
2006 if (diffopt_changed() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002007 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002008 }
2009#endif
2010
2011#ifdef FEAT_FOLDING
2012 // 'foldmethod'
2013 else if (gvarp == &curwin->w_allbuf_opt.wo_fdm)
2014 {
2015 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK
2016 || *curwin->w_p_fdm == NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002017 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002018 else
2019 {
2020 foldUpdateAll(curwin);
2021 if (foldmethodIsDiff(curwin))
2022 newFoldLevel();
2023 }
2024 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002025 // 'foldmarker'
2026 else if (gvarp == &curwin->w_allbuf_opt.wo_fmr)
2027 {
2028 p = vim_strchr(*varp, ',');
2029 if (p == NULL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002030 errmsg = e_comma_required;
Bram Moolenaardac13472019-09-16 21:06:21 +02002031 else if (p == *varp || p[1] == NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002032 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002033 else if (foldmethodIsMarker(curwin))
2034 foldUpdateAll(curwin);
2035 }
2036 // 'commentstring'
2037 else if (gvarp == &p_cms)
2038 {
2039 if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
Bram Moolenaar1d423ef2022-01-02 21:26:16 +00002040 errmsg = e_commentstring_must_be_empty_or_contain_str;
Bram Moolenaardac13472019-09-16 21:06:21 +02002041 }
2042 // 'foldopen'
2043 else if (varp == &p_fdo)
2044 {
2045 if (opt_strings_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002046 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002047 }
2048 // 'foldclose'
2049 else if (varp == &p_fcl)
2050 {
2051 if (check_opt_strings(p_fcl, p_fcl_values, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002052 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002053 }
2054 // 'foldignore'
2055 else if (gvarp == &curwin->w_allbuf_opt.wo_fdi)
2056 {
2057 if (foldmethodIsIndent(curwin))
2058 foldUpdateAll(curwin);
2059 }
2060#endif
2061
2062 // 'virtualedit'
Gary Johnson53ba05b2021-07-26 22:19:10 +02002063 else if (gvarp == &p_ve)
Bram Moolenaardac13472019-09-16 21:06:21 +02002064 {
Gary Johnson53ba05b2021-07-26 22:19:10 +02002065 char_u *ve = p_ve;
2066 unsigned int *flags = &ve_flags;
2067
2068 if (opt_flags & OPT_LOCAL)
Bram Moolenaardac13472019-09-16 21:06:21 +02002069 {
Gary Johnson51ad8502021-08-03 18:33:08 +02002070 ve = curwin->w_p_ve;
2071 flags = &curwin->w_ve_flags;
Gary Johnson53ba05b2021-07-26 22:19:10 +02002072 }
2073
2074 if ((opt_flags & OPT_LOCAL) && *ve == NUL)
2075 // make the local value empty: use the global value
2076 *flags = 0;
2077 else
2078 {
2079 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002080 errmsg = e_invalid_argument;
Gary Johnson53ba05b2021-07-26 22:19:10 +02002081 else if (STRCMP(p_ve, oldval) != 0)
2082 {
2083 // Recompute cursor position in case the new 've' setting
2084 // changes something.
2085 validate_virtcol();
2086 coladvance(curwin->w_virtcol);
2087 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002088 }
2089 }
2090
2091#if defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)
2092 else if (varp == &p_csqf)
2093 {
2094 if (p_csqf != NULL)
2095 {
2096 p = p_csqf;
2097 while (*p != NUL)
2098 {
2099 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
2100 || p[1] == NUL
2101 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
2102 || (p[2] != NUL && p[2] != ','))
2103 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002104 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002105 break;
2106 }
2107 else if (p[2] == NUL)
2108 break;
2109 else
2110 p += 3;
2111 }
2112 }
2113 }
2114#endif
2115
Bram Moolenaardac13472019-09-16 21:06:21 +02002116 // 'cinoptions'
2117 else if (gvarp == &p_cino)
2118 {
2119 // TODO: recognize errors
2120 parse_cino(curbuf);
2121 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002122
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002123 // 'lispoptions'
2124 else if (gvarp == &p_lop)
2125 {
2126 if (**varp != NUL && STRCMP(*varp, "expr:0") != 0
2127 && STRCMP(*varp, "expr:1") != 0)
2128 errmsg = e_invalid_argument;
2129 }
2130
Bram Moolenaardac13472019-09-16 21:06:21 +02002131#if defined(FEAT_RENDER_OPTIONS)
2132 // 'renderoptions'
2133 else if (varp == &p_rop)
2134 {
2135 if (!gui_mch_set_rendering_options(p_rop))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002136 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002137 }
2138#endif
2139
2140 else if (gvarp == &p_ft)
2141 {
2142 if (!valid_filetype(*varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002143 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002144 else
2145 {
2146 value_changed = STRCMP(oldval, *varp) != 0;
2147
2148 // Since we check the value, there is no need to set P_INSECURE,
2149 // even when the value comes from a modeline.
2150 *value_checked = TRUE;
2151 }
2152 }
2153
2154#ifdef FEAT_SYN_HL
2155 else if (gvarp == &p_syn)
2156 {
2157 if (!valid_filetype(*varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002158 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002159 else
2160 {
2161 value_changed = STRCMP(oldval, *varp) != 0;
2162
2163 // Since we check the value, there is no need to set P_INSECURE,
2164 // even when the value comes from a modeline.
2165 *value_checked = TRUE;
2166 }
2167 }
2168#endif
2169
2170#ifdef FEAT_TERMINAL
2171 // 'termwinkey'
2172 else if (varp == &curwin->w_p_twk)
2173 {
2174 if (*curwin->w_p_twk != NUL
2175 && string_to_key(curwin->w_p_twk, TRUE) == 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002176 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002177 }
2178 // 'termwinsize'
2179 else if (varp == &curwin->w_p_tws)
2180 {
2181 if (*curwin->w_p_tws != NUL)
2182 {
2183 p = skipdigits(curwin->w_p_tws);
2184 if (p == curwin->w_p_tws
2185 || (*p != 'x' && *p != '*')
2186 || *skipdigits(p + 1) != NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002187 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002188 }
2189 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002190 // 'wincolor'
2191 else if (varp == &curwin->w_p_wcr)
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002192 term_update_wincolor(curwin);
Bram Moolenaardac13472019-09-16 21:06:21 +02002193# if defined(MSWIN)
2194 // 'termwintype'
2195 else if (varp == &p_twt)
2196 {
2197 if (check_opt_strings(*varp, p_twt_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002198 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002199 }
2200# endif
2201#endif
2202
2203#ifdef FEAT_VARTABS
2204 // 'varsofttabstop'
2205 else if (varp == &(curbuf->b_p_vsts))
2206 {
2207 char_u *cp;
2208
2209 if (!(*varp)[0] || ((*varp)[0] == '0' && !(*varp)[1]))
2210 {
2211 if (curbuf->b_p_vsts_array)
2212 {
2213 vim_free(curbuf->b_p_vsts_array);
2214 curbuf->b_p_vsts_array = 0;
2215 }
2216 }
2217 else
2218 {
2219 for (cp = *varp; *cp; ++cp)
2220 {
2221 if (vim_isdigit(*cp))
2222 continue;
2223 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
2224 continue;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002225 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002226 break;
2227 }
2228 if (errmsg == NULL)
2229 {
2230 int *oldarray = curbuf->b_p_vsts_array;
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002231 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Bram Moolenaardac13472019-09-16 21:06:21 +02002232 {
2233 if (oldarray)
2234 vim_free(oldarray);
2235 }
2236 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002237 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002238 }
2239 }
2240 }
2241
2242 // 'vartabstop'
2243 else if (varp == &(curbuf->b_p_vts))
2244 {
2245 char_u *cp;
2246
2247 if (!(*varp)[0] || ((*varp)[0] == '0' && !(*varp)[1]))
2248 {
2249 if (curbuf->b_p_vts_array)
2250 {
2251 vim_free(curbuf->b_p_vts_array);
2252 curbuf->b_p_vts_array = NULL;
2253 }
2254 }
2255 else
2256 {
2257 for (cp = *varp; *cp; ++cp)
2258 {
2259 if (vim_isdigit(*cp))
2260 continue;
2261 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
2262 continue;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002263 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002264 break;
2265 }
2266 if (errmsg == NULL)
2267 {
2268 int *oldarray = curbuf->b_p_vts_array;
2269
Bram Moolenaarb7081e12021-09-04 18:47:28 +02002270 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Bram Moolenaardac13472019-09-16 21:06:21 +02002271 {
2272 vim_free(oldarray);
2273#ifdef FEAT_FOLDING
2274 if (foldmethodIsIndent(curwin))
2275 foldUpdateAll(curwin);
2276#endif
2277 }
2278 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002279 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002280 }
2281 }
2282 }
2283#endif
2284
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002285#ifdef FEAT_PROP_POPUP
Bram Moolenaardac13472019-09-16 21:06:21 +02002286 // 'previewpopup'
2287 else if (varp == &p_pvp)
2288 {
2289 if (parse_previewpopup(NULL) == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002290 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002291 }
2292# ifdef FEAT_QUICKFIX
2293 // 'completepopup'
2294 else if (varp == &p_cpp)
2295 {
2296 if (parse_completepopup(NULL) == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002297 errmsg = e_invalid_argument;
Bram Moolenaar447bfba2020-07-18 16:07:16 +02002298 else
2299 popup_close_info();
Bram Moolenaardac13472019-09-16 21:06:21 +02002300 }
2301# endif
2302#endif
2303
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002304#ifdef FEAT_EVAL
2305 // '*expr' options
2306 else if (
2307# ifdef FEAT_BEVAL
2308 varp == &p_bexpr ||
2309# endif
2310# ifdef FEAT_DIFF
2311 varp == &p_dex ||
2312# endif
2313# ifdef FEAT_FOLDING
2314 varp == &curwin->w_p_fde ||
Yegappan Lakshmanan27708e62021-12-26 21:54:43 +00002315 varp == &curwin->w_p_fdt ||
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002316# endif
2317 gvarp == &p_fex ||
2318# ifdef FEAT_FIND_ID
2319 gvarp == &p_inex ||
2320# endif
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002321 gvarp == &p_inde ||
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002322# ifdef FEAT_DIFF
2323 varp == &p_pex ||
2324# endif
2325# ifdef FEAT_POSTSCRIPT
2326 varp == &p_pexpr ||
2327# endif
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00002328 varp == &p_ccv)
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002329 {
2330 char_u **p_opt = NULL;
2331 char_u *name;
2332
2333 // If the option value starts with <SID> or s:, then replace that with
2334 // the script identifier.
2335# ifdef FEAT_BEVAL
2336 if (varp == &p_bexpr) // 'balloonexpr'
2337 p_opt = (opt_flags & OPT_LOCAL) ? &curbuf->b_p_bexpr : &p_bexpr;
2338# endif
2339# ifdef FEAT_DIFF
2340 if (varp == &p_dex) // 'diffexpr'
2341 p_opt = &p_dex;
2342# endif
2343# ifdef FEAT_FOLDING
Yegappan Lakshmanan27708e62021-12-26 21:54:43 +00002344 if (varp == &curwin->w_p_fde) // 'foldexpr'
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002345 p_opt = &curwin->w_p_fde;
Yegappan Lakshmanan27708e62021-12-26 21:54:43 +00002346 if (varp == &curwin->w_p_fdt) // 'foldtext'
2347 p_opt = &curwin->w_p_fdt;
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002348# endif
2349 if (gvarp == &p_fex) // 'formatexpr'
2350 p_opt = &curbuf->b_p_fex;
2351# ifdef FEAT_FIND_ID
2352 if (gvarp == &p_inex) // 'includeexpr'
2353 p_opt = &curbuf->b_p_inex;
2354# endif
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002355 if (gvarp == &p_inde) // 'indentexpr'
2356 p_opt = &curbuf->b_p_inde;
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002357# 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 {
zeertzjqf6782732022-07-27 18:26:03 +01002373 free_string_option(*p_opt);
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002374 *p_opt = name;
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002375 }
2376 }
2377
2378# ifdef FEAT_FOLDING
2379 if (varp == &curwin->w_p_fde && foldmethodIsExpr(curwin))
2380 foldUpdateAll(curwin);
2381# endif
2382 }
2383#endif
2384
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002385#ifdef FEAT_COMPL_FUNC
2386 // 'completefunc'
2387 else if (gvarp == &p_cfu)
2388 {
2389 if (set_completefunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002390 errmsg = e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002391 }
2392
2393 // 'omnifunc'
2394 else if (gvarp == &p_ofu)
2395 {
2396 if (set_omnifunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002397 errmsg = e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002398 }
2399
2400 // 'thesaurusfunc'
2401 else if (gvarp == &p_tsrfu)
2402 {
2403 if (set_thesaurusfunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002404 errmsg = e_invalid_argument;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002405 }
2406#endif
2407
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00002408#if defined(FEAT_EVAL) && \
2409 (defined(FEAT_XIM) || defined(IME_WITHOUT_XIM) || defined(VIMDLL))
2410 // 'imactivatefunc'
2411 else if (gvarp == &p_imaf)
2412 {
2413 if (set_imactivatefunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002414 errmsg = e_invalid_argument;
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00002415 }
2416
2417 // 'imstatusfunc'
2418 else if (gvarp == &p_imsf)
2419 {
2420 if (set_imstatusfunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002421 errmsg = e_invalid_argument;
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00002422 }
2423#endif
2424
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00002425 // 'operatorfunc'
2426 else if (varp == &p_opfunc)
2427 {
2428 if (set_operatorfunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002429 errmsg = e_invalid_argument;
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00002430 }
2431
Bram Moolenaard43906d2020-07-20 21:31:32 +02002432#ifdef FEAT_QUICKFIX
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00002433 // 'quickfixtextfunc'
Bram Moolenaard43906d2020-07-20 21:31:32 +02002434 else if (varp == &p_qftf)
2435 {
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00002436 if (qf_process_qftf_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002437 errmsg = e_invalid_argument;
Bram Moolenaard43906d2020-07-20 21:31:32 +02002438 }
2439#endif
2440
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00002441#ifdef FEAT_EVAL
2442 // 'tagfunc'
2443 else if (gvarp == &p_tfu)
2444 {
2445 if (set_tagfunc_option() == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002446 errmsg = e_invalid_argument;
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00002447 }
2448#endif
2449
Bram Moolenaardac13472019-09-16 21:06:21 +02002450 // Options that are a list of flags.
2451 else
2452 {
2453 p = NULL;
2454 if (varp == &p_ww) // 'whichwrap'
2455 p = (char_u *)WW_ALL;
2456 if (varp == &p_shm) // 'shortmess'
2457 p = (char_u *)SHM_ALL;
2458 else if (varp == &(p_cpo)) // 'cpoptions'
2459 p = (char_u *)CPO_ALL;
2460 else if (varp == &(curbuf->b_p_fo)) // 'formatoptions'
2461 p = (char_u *)FO_ALL;
2462#ifdef FEAT_CONCEAL
2463 else if (varp == &curwin->w_p_cocu) // 'concealcursor'
2464 p = (char_u *)COCU_ALL;
2465#endif
2466 else if (varp == &p_mouse) // 'mouse'
2467 {
Bram Moolenaardac13472019-09-16 21:06:21 +02002468 p = (char_u *)MOUSE_ALL;
Bram Moolenaardac13472019-09-16 21:06:21 +02002469 }
2470#if defined(FEAT_GUI)
2471 else if (varp == &p_go) // 'guioptions'
2472 p = (char_u *)GO_ALL;
2473#endif
2474 if (p != NULL)
2475 {
2476 for (s = *varp; *s; ++s)
2477 if (vim_strchr(p, *s) == NULL)
2478 {
2479 errmsg = illegal_char(errbuf, *s);
2480 break;
2481 }
2482 }
2483 }
2484
2485 // If error detected, restore the previous value.
2486 if (errmsg != NULL)
2487 {
zeertzjqf6782732022-07-27 18:26:03 +01002488 free_string_option(*varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02002489 *varp = oldval;
2490 // When resetting some values, need to act on it.
2491 if (did_chartab)
2492 (void)init_chartab();
2493 if (varp == &p_hl)
2494 (void)highlight_changed();
2495 }
2496 else
2497 {
2498#ifdef FEAT_EVAL
2499 // Remember where the option was set.
2500 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
2501#endif
2502 // Free string options that are in allocated memory.
2503 // Use "free_oldval", because recursiveness may change the flags under
2504 // our fingers (esp. init_highlight()).
2505 if (free_oldval)
2506 free_string_option(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01002507 set_option_flag(opt_idx, P_ALLOCED);
Bram Moolenaardac13472019-09-16 21:06:21 +02002508
2509 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
2510 && is_global_local_option(opt_idx))
2511 {
2512 // global option with local value set to use global value; free
2513 // the local value and make it empty
2514 p = get_option_varp_scope(opt_idx, OPT_LOCAL);
2515 free_string_option(*(char_u **)p);
2516 *(char_u **)p = empty_option;
2517 }
2518
2519 // May set global value for local option.
2520 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
2521 set_string_option_global(opt_idx, varp);
2522
2523 // Trigger the autocommand only after setting the flags.
2524#ifdef FEAT_SYN_HL
2525 // When 'syntax' is set, load the syntax of that name
2526 if (varp == &(curbuf->b_p_syn))
2527 {
2528 static int syn_recursive = 0;
2529
2530 ++syn_recursive;
2531 // Only pass TRUE for "force" when the value changed or not used
2532 // recursively, to avoid endless recurrence.
2533 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
2534 value_changed || syn_recursive == 1, curbuf);
2535 curbuf->b_flags |= BF_SYN_SET;
2536 --syn_recursive;
2537 }
2538#endif
2539 else if (varp == &(curbuf->b_p_ft))
2540 {
2541 // 'filetype' is set, trigger the FileType autocommand.
2542 // Skip this when called from a modeline and the filetype was
2543 // already set to this value.
2544 if (!(opt_flags & OPT_MODELINE) || value_changed)
2545 {
2546 static int ft_recursive = 0;
2547 int secure_save = secure;
2548
2549 // Reset the secure flag, since the value of 'filetype' has
2550 // been checked to be safe.
2551 secure = 0;
2552
2553 ++ft_recursive;
2554 did_filetype = TRUE;
2555 // Only pass TRUE for "force" when the value changed or not
2556 // used recursively, to avoid endless recurrence.
2557 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2558 value_changed || ft_recursive == 1, curbuf);
2559 --ft_recursive;
2560 // Just in case the old "curbuf" is now invalid.
2561 if (varp != &(curbuf->b_p_ft))
2562 varp = NULL;
2563
2564 secure = secure_save;
2565 }
2566 }
2567#ifdef FEAT_SPELL
2568 if (varp == &(curwin->w_s->b_p_spl))
2569 {
2570 char_u fname[200];
2571 char_u *q = curwin->w_s->b_p_spl;
2572
2573 // Skip the first name if it is "cjk".
2574 if (STRNCMP(q, "cjk,", 4) == 0)
2575 q += 4;
2576
2577 // Source the spell/LANG.vim in 'runtimepath'.
2578 // They could set 'spellcapcheck' depending on the language.
2579 // Use the first name in 'spelllang' up to '_region' or
2580 // '.encoding'.
2581 for (p = q; *p != NUL; ++p)
2582 if (!ASCII_ISALNUM(*p) && *p != '-')
2583 break;
2584 if (p > q)
2585 {
2586 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
2587 (int)(p - q), q);
2588 source_runtime(fname, DIP_ALL);
2589 }
2590 }
2591#endif
2592 }
2593
Bram Moolenaardac13472019-09-16 21:06:21 +02002594 if (varp == &p_mouse)
2595 {
Bram Moolenaardac13472019-09-16 21:06:21 +02002596 if (*p_mouse == NUL)
2597 mch_setmouse(FALSE); // switch mouse off
2598 else
Bram Moolenaardac13472019-09-16 21:06:21 +02002599 setmouse(); // in case 'mouse' changed
2600 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002601
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002602#if defined(FEAT_LUA) || defined(PROTO)
2603 if (varp == &p_rtp)
2604 update_package_paths_in_lua();
2605#endif
2606
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00002607#if defined(FEAT_LINEBREAK)
2608 // Changing Formatlistpattern when briopt includes the list setting:
2609 // redraw
2610 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
2611 && curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002612 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00002613#endif
2614
Bram Moolenaardac13472019-09-16 21:06:21 +02002615 if (curwin->w_curswant != MAXCOL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002616 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02002617 curwin->w_set_curswant = TRUE;
2618
Bram Moolenaar37294bd2021-03-10 13:40:08 +01002619 if ((opt_flags & OPT_NO_REDRAW) == 0)
2620 {
Bram Moolenaardac13472019-09-16 21:06:21 +02002621#ifdef FEAT_GUI
Bram Moolenaar37294bd2021-03-10 13:40:08 +01002622 // check redraw when it's not a GUI option or the GUI is active.
2623 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02002624#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01002625 check_redraw(get_option_flags(opt_idx));
2626 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002627
2628#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
2629 if (did_swaptcap)
2630 {
2631 set_termname((char_u *)"win32");
2632 init_highlight(TRUE, FALSE);
2633 }
2634#endif
2635
2636 return errmsg;
2637}
2638
2639/*
2640 * Check an option that can be a range of string values.
2641 *
2642 * Return OK for correct value, FAIL otherwise.
2643 * Empty is always OK.
2644 */
2645 static int
2646check_opt_strings(
2647 char_u *val,
2648 char **values,
2649 int list) // when TRUE: accept a list of values
2650{
2651 return opt_strings_flags(val, values, NULL, list);
2652}
2653
2654/*
2655 * Handle an option that can be a range of string values.
2656 * Set a flag in "*flagp" for each string present.
2657 *
2658 * Return OK for correct value, FAIL otherwise.
2659 * Empty is always OK.
2660 */
2661 static int
2662opt_strings_flags(
2663 char_u *val, // new value
2664 char **values, // array of valid string values
2665 unsigned *flagp,
2666 int list) // when TRUE: accept a list of values
2667{
2668 int i;
2669 int len;
2670 unsigned new_flags = 0;
2671
2672 while (*val)
2673 {
2674 for (i = 0; ; ++i)
2675 {
2676 if (values[i] == NULL) // val not found in values[]
2677 return FAIL;
2678
2679 len = (int)STRLEN(values[i]);
2680 if (STRNCMP(values[i], val, len) == 0
2681 && ((list && val[len] == ',') || val[len] == NUL))
2682 {
2683 val += len + (val[len] == ',');
2684 new_flags |= (1 << i);
2685 break; // check next item in val list
2686 }
2687 }
2688 }
2689 if (flagp != NULL)
2690 *flagp = new_flags;
2691
2692 return OK;
2693}
2694
2695/*
2696 * return OK if "p" is a valid fileformat name, FAIL otherwise.
2697 */
2698 int
2699check_ff_value(char_u *p)
2700{
2701 return check_opt_strings(p, p_ff_values, FALSE);
2702}
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00002703
2704/*
2705 * Save the acutal shortmess Flags and clear them
2706 * temporarily to avoid that file messages
2707 * overwrites any output from the following commands.
2708 *
2709 * Caller must make sure to first call save_clear_shm_value() and then
2710 * restore_shm_value() exactly the same number of times.
2711 */
2712 void
2713save_clear_shm_value()
2714{
2715 if (STRLEN(p_shm) >= SHM_LEN)
2716 {
2717 iemsg(e_internal_error_shortmess_too_long);
2718 return;
2719 }
2720
2721 if (++set_shm_recursive == 1)
2722 {
2723 STRCPY(shm_buf, p_shm);
2724 set_option_value_give_err((char_u *)"shm", 0L, (char_u *)"", 0);
2725 }
2726}
2727
2728/*
2729 * Restore the shortmess Flags set from the save_clear_shm_value() function.
2730 */
2731 void
2732restore_shm_value()
2733{
2734 if (--set_shm_recursive == 0)
2735 {
2736 set_option_value_give_err((char_u *)"shm", 0L, shm_buf, 0);
2737 vim_memset(shm_buf, 0, SHM_LEN);
2738 }
2739}