blob: 6c9fd3ab32cbce9e107e3e04b1e542151fbc1a14 [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.
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000158 if (oldval == NULL || newval == NULL
159 || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
160 return;
Bram Moolenaardac13472019-09-16 21:06:21 +0200161
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000162 char_u buf_type[7];
163
164 sprintf((char *)buf_type, "%s",
Bram Moolenaardac13472019-09-16 21:06:21 +0200165 (opt_flags & OPT_LOCAL) ? "local" : "global");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000166 set_vim_var_string(VV_OPTION_OLD, oldval, -1);
167 set_vim_var_string(VV_OPTION_NEW, newval, -1);
168 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
169 if (opt_flags & OPT_LOCAL)
170 {
171 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
172 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
Bram Moolenaardac13472019-09-16 21:06:21 +0200173 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000174 if (opt_flags & OPT_GLOBAL)
175 {
176 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
177 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval, -1);
178 }
179 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
180 {
181 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
182 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval_l, -1);
183 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval_g, -1);
184 }
185 if (opt_flags & OPT_MODELINE)
186 {
187 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
188 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
189 }
190 apply_autocmds(EVENT_OPTIONSET,
191 get_option_fullname(opt_idx), NULL, FALSE,
192 NULL);
193 reset_v_option_vars();
Bram Moolenaardac13472019-09-16 21:06:21 +0200194}
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);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000390 if (s == NULL)
391 return;
392
393 varp = (char_u **)get_option_varp_scope(idx,
394 both ? OPT_LOCAL : opt_flags);
395 if ((opt_flags & OPT_FREE) && (get_option_flags(idx) & P_ALLOCED))
396 free_string_option(*varp);
397 *varp = s;
398
399 // For buffer/window local option may also set the global value.
400 if (both)
401 set_string_option_global(idx, varp);
402
403 set_option_flag(idx, P_ALLOCED);
404
405 // When setting both values of a global option with a local value,
406 // make the local value empty, so that the global value is used.
407 if (is_global_local_option(idx) && both)
Bram Moolenaardac13472019-09-16 21:06:21 +0200408 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000409 free_string_option(*varp);
410 *varp = empty_option;
Bram Moolenaardac13472019-09-16 21:06:21 +0200411 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000412# ifdef FEAT_EVAL
413 if (set_sid != SID_NONE)
414 {
415 sctx_T script_ctx;
416
417 if (set_sid == 0)
418 script_ctx = current_sctx;
419 else
420 {
421 script_ctx.sc_sid = set_sid;
422 script_ctx.sc_seq = 0;
423 script_ctx.sc_lnum = 0;
424 script_ctx.sc_version = 1;
425 }
426 set_option_sctx_idx(idx, opt_flags, script_ctx);
427 }
428# endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200429}
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,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +0000490 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
491 char *errbuf)
Bram Moolenaardac13472019-09-16 21:06:21 +0200492{
493 char_u *s;
494 char_u **varp;
495 char_u *oldval;
496#if defined(FEAT_EVAL)
497 char_u *oldval_l = NULL;
498 char_u *oldval_g = NULL;
499 char_u *saved_oldval = NULL;
500 char_u *saved_oldval_l = NULL;
501 char_u *saved_oldval_g = NULL;
502 char_u *saved_newval = NULL;
503#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100504 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +0200505 int value_checked = FALSE;
506
507 if (is_hidden_option(opt_idx)) // don't set hidden option
508 return NULL;
509
Bram Moolenaar7f009df2020-03-16 20:27:38 +0100510 s = vim_strsave(value == NULL ? (char_u *)"" : value);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000511 if (s == NULL)
512 return NULL;
513
514 varp = (char_u **)get_option_varp_scope(opt_idx,
515 (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
516 ? (is_global_local_option(opt_idx)
517 ? OPT_GLOBAL : OPT_LOCAL)
518 : opt_flags);
519 oldval = *varp;
520#if defined(FEAT_EVAL)
521 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +0200522 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000523 oldval_l = *(char_u **)get_option_varp_scope(opt_idx, OPT_LOCAL);
524 oldval_g = *(char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
Bram Moolenaardac13472019-09-16 21:06:21 +0200525 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000526#endif
527 *varp = s;
528
529#if defined(FEAT_EVAL)
530 if (!starting
531# ifdef FEAT_CRYPT
532 && !is_crypt_key_option(opt_idx)
533# endif
534 )
535 {
536 if (oldval_l != NULL)
537 saved_oldval_l = vim_strsave(oldval_l);
538 if (oldval_g != NULL)
539 saved_oldval_g = vim_strsave(oldval_g);
540 saved_oldval = vim_strsave(oldval);
541 saved_newval = vim_strsave(s);
542 }
543#endif
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +0000544 if ((errmsg = did_set_string_option(opt_idx, varp, oldval, errbuf,
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000545 opt_flags, &value_checked)) == NULL)
546 did_set_option(opt_idx, opt_flags, TRUE, value_checked);
547
548#if defined(FEAT_EVAL)
549 // call autocommand after handling side effects
550 if (errmsg == NULL)
551 trigger_optionset_string(opt_idx, opt_flags,
552 saved_oldval, saved_oldval_l,
553 saved_oldval_g, saved_newval);
554 vim_free(saved_oldval);
555 vim_free(saved_oldval_l);
556 vim_free(saved_oldval_g);
557 vim_free(saved_newval);
558#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100559 return errmsg;
Bram Moolenaardac13472019-09-16 21:06:21 +0200560}
561
562/*
563 * Return TRUE if "val" is a valid 'filetype' name.
564 * Also used for 'syntax' and 'keymap'.
565 */
566 static int
567valid_filetype(char_u *val)
568{
569 return valid_name(val, ".-_");
570}
571
572#ifdef FEAT_STL_OPT
573/*
574 * Check validity of options with the 'statusline' format.
zeertzjq5dc294a2022-04-15 13:17:57 +0100575 * Return an untranslated error message or NULL.
Bram Moolenaardac13472019-09-16 21:06:21 +0200576 */
577 static char *
578check_stl_option(char_u *s)
579{
Bram Moolenaardac13472019-09-16 21:06:21 +0200580 int groupdepth = 0;
581 static char errbuf[80];
582
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100583 while (*s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200584 {
585 // Check for valid keys after % sequences
586 while (*s && *s != '%')
587 s++;
588 if (!*s)
589 break;
590 s++;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +0000591 if (*s == '%' || *s == STL_TRUNCMARK || *s == STL_SEPARATE)
Bram Moolenaardac13472019-09-16 21:06:21 +0200592 {
593 s++;
594 continue;
595 }
596 if (*s == ')')
597 {
598 s++;
599 if (--groupdepth < 0)
600 break;
601 continue;
602 }
603 if (*s == '-')
604 s++;
605 while (VIM_ISDIGIT(*s))
606 s++;
607 if (*s == STL_USER_HL)
608 continue;
609 if (*s == '.')
610 {
611 s++;
612 while (*s && VIM_ISDIGIT(*s))
613 s++;
614 }
615 if (*s == '(')
616 {
617 groupdepth++;
618 continue;
619 }
620 if (vim_strchr(STL_ALL, *s) == NULL)
621 {
622 return illegal_char(errbuf, *s);
623 }
624 if (*s == '{')
625 {
zeertzjq5dc294a2022-04-15 13:17:57 +0100626 int reevaluate = (*++s == '%');
shadmansaleh30e3de22021-05-15 17:23:28 +0200627
zeertzjq5dc294a2022-04-15 13:17:57 +0100628 if (reevaluate && *++s == '}')
629 // "}" is not allowed immediately after "%{%"
630 return illegal_char(errbuf, '}');
shadmansaleh30e3de22021-05-15 17:23:28 +0200631 while ((*s != '}' || (reevaluate && s[-1] != '%')) && *s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200632 s++;
633 if (*s != '}')
zeertzjq5dc294a2022-04-15 13:17:57 +0100634 return e_unclosed_expression_sequence;
Bram Moolenaardac13472019-09-16 21:06:21 +0200635 }
636 }
Bram Moolenaardac13472019-09-16 21:06:21 +0200637 if (groupdepth != 0)
zeertzjq5dc294a2022-04-15 13:17:57 +0100638 return e_unbalanced_groups;
Bram Moolenaardac13472019-09-16 21:06:21 +0200639 return NULL;
640}
641#endif
642
643/*
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +0000644 * Check for a "normal" directory or file name in some options. Disallow a
645 * path separator (slash and/or backslash), wildcards and characters that are
646 * often illegal in a file name. Be more permissive if "secure" is off.
647 */
648 static int
649check_illegal_path_names(int opt_idx, char_u **varp)
650{
651 return (((get_option_flags(opt_idx) & P_NFNAME)
652 && vim_strpbrk(*varp, (char_u *)(secure
653 ? "/\\*?[|;&<>\r\n" : "/\\*?[<>\r\n")) != NULL)
654 || ((get_option_flags(opt_idx) & P_NDNAME)
655 && vim_strpbrk(*varp, (char_u *)"*?[|;&<>\r\n") != NULL));
656}
657
658/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000659 * The 'term' option is changed.
660 */
661 static char *
662did_set_term(int *opt_idx, long_u *free_oldval)
663{
664 char *errmsg = NULL;
665
666 if (T_NAME[0] == NUL)
667 errmsg = e_cannot_set_term_to_empty_string;
668#ifdef FEAT_GUI
669 else if (gui.in_use)
670 errmsg = e_cannot_change_term_in_GUI;
671 else if (term_is_gui(T_NAME))
672 errmsg = e_use_gui_to_start_GUI;
673#endif
674 else if (set_termname(T_NAME) == FAIL)
675 errmsg = e_not_found_in_termcap;
676 else
677 {
678 // Screen colors may have changed.
679 redraw_later_clear();
680
681 // Both 'term' and 'ttytype' point to T_NAME, only set the
682 // P_ALLOCED flag on 'term'.
683 *opt_idx = findoption((char_u *)"term");
Yegappan Lakshmanan78012f52023-02-02 16:34:11 +0000684 if (*opt_idx >= 0)
685 *free_oldval = (get_option_flags(*opt_idx) & P_ALLOCED);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000686 }
687
688 return errmsg;
689}
690
691/*
692 * The 'backupcopy' option is changed.
693 */
694 static char *
695did_set_backupcopy(
696 char_u *oldval,
697 int opt_flags)
698{
699 char_u *bkc = p_bkc;
700 unsigned int *flags = &bkc_flags;
701 char *errmsg = NULL;
702
703 if (opt_flags & OPT_LOCAL)
704 {
705 bkc = curbuf->b_p_bkc;
706 flags = &curbuf->b_bkc_flags;
707 }
708
709 if ((opt_flags & OPT_LOCAL) && *bkc == NUL)
710 // make the local value empty: use the global value
711 *flags = 0;
712 else
713 {
714 if (opt_strings_flags(bkc, p_bkc_values, flags, TRUE) != OK)
715 errmsg = e_invalid_argument;
716 if ((((int)*flags & BKC_AUTO) != 0)
717 + (((int)*flags & BKC_YES) != 0)
718 + (((int)*flags & BKC_NO) != 0) != 1)
719 {
720 // Must have exactly one of "auto", "yes" and "no".
721 (void)opt_strings_flags(oldval, p_bkc_values, flags, TRUE);
722 errmsg = e_invalid_argument;
723 }
724 }
725
726 return errmsg;
727}
728
729/*
730 * The 'backupext' or the 'patchmode' option is changed.
731 */
732 static char *
733did_set_backupext_or_patchmode(void)
734{
735 if (STRCMP(*p_bex == '.' ? p_bex + 1 : p_bex,
736 *p_pm == '.' ? p_pm + 1 : p_pm) == 0)
737 return e_backupext_and_patchmode_are_equal;
738
739 return NULL;
740}
741
742#ifdef FEAT_LINEBREAK
743/*
744 * The 'breakindentopt' option is changed.
745 */
746 static char *
747did_set_breakindentopt(void)
748{
749 char *errmsg = NULL;
750
751 if (briopt_check(curwin) == FAIL)
752 errmsg = e_invalid_argument;
753 // list setting requires a redraw
754 if (curwin->w_briopt_list)
755 redraw_all_later(UPD_NOT_VALID);
756
757 return errmsg;
758}
759#endif
760
761/*
762 * The 'isident' or the 'iskeyword' or the 'isprint' or the 'isfname' option is
763 * changed.
764 */
765 static char *
766did_set_isopt(int *did_chartab)
767{
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +0000768 // 'isident', 'iskeyword', 'isprint or 'isfname' option: refill g_chartab[]
769 // If the new option is invalid, use old value.
770 // 'lisp' option: refill g_chartab[] for '-' char.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000771 if (init_chartab() == FAIL)
772 {
773 *did_chartab = TRUE; // need to restore it below
774 return e_invalid_argument; // error in value
775 }
776
777 return NULL;
778}
779
780/*
781 * The 'helpfile' option is changed.
782 */
783 static void
784did_set_helpfile(void)
785{
786 // May compute new values for $VIM and $VIMRUNTIME
787 if (didset_vim)
788 vim_unsetenv_ext((char_u *)"VIM");
789 if (didset_vimruntime)
790 vim_unsetenv_ext((char_u *)"VIMRUNTIME");
791}
792
793#ifdef FEAT_SYN_HL
794/*
795 * The 'cursorlineopt' option is changed.
796 */
797 static char *
798did_set_cursorlineopt(char_u **varp)
799{
800 if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
801 return e_invalid_argument;
802
803 return NULL;
804}
805#endif
806
807#ifdef FEAT_MULTI_LANG
808/*
809 * The 'helplang' option is changed.
810 */
811 static char *
812did_set_helplang(void)
813{
814 char *errmsg = NULL;
815
816 // Check for "", "ab", "ab,cd", etc.
817 for (char_u *s = p_hlg; *s != NUL; s += 3)
818 {
819 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
820 {
821 errmsg = e_invalid_argument;
822 break;
823 }
824 if (s[2] == NUL)
825 break;
826 }
827
828 return errmsg;
829}
830#endif
831
832/*
833 * The 'highlight' option is changed.
834 */
835 static char *
836did_set_highlight(void)
837{
838 if (highlight_changed() == FAIL)
839 return e_invalid_argument; // invalid flags
840
841 return NULL;
842}
843
844/*
845 * An option that accepts a list of flags is changed.
846 * e.g. 'viewoptions', 'switchbuf', 'casemap', etc.
847 */
848 static char *
849did_set_opt_flags(char_u *val, char **values, unsigned *flagp, int list)
850{
851 if (opt_strings_flags(val, values, flagp, list) == FAIL)
852 return e_invalid_argument;
853
854 return NULL;
855}
856
857/*
858 * An option that accepts a list of string values is changed.
859 * e.g. 'nrformats', 'scrollopt', 'wildoptions', etc.
860 */
861 static char *
862did_set_opt_strings(char_u *val, char **values, int list)
863{
864 return did_set_opt_flags(val, values, NULL, list);
865}
866
867#ifdef FEAT_SESSION
868/*
869 * The 'sessionoptions' option is changed.
870 */
871 static char *
872did_set_sessionoptions(char_u *oldval)
873{
874 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
875 return e_invalid_argument;
876 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
877 {
878 // Don't allow both "sesdir" and "curdir".
879 (void)opt_strings_flags(oldval, p_ssop_values, &ssop_flags, TRUE);
880 return e_invalid_argument;
881 }
882
883 return NULL;
884}
885#endif
886
887/*
888 * The 'ambiwidth' option is changed.
889 */
890 static char *
891did_set_ambiwidth(void)
892{
893 if (check_opt_strings(p_ambw, p_ambw_values, FALSE) != OK)
894 return e_invalid_argument;
895
896 return check_chars_options();
897}
898
899/*
900 * The 'background' option is changed.
901 */
902 static char *
903did_set_background(void)
904{
905 if (check_opt_strings(p_bg, p_bg_values, FALSE) == FAIL)
906 return e_invalid_argument;
907
908#ifdef FEAT_EVAL
909 int dark = (*p_bg == 'd');
910#endif
911
912 init_highlight(FALSE, FALSE);
913
914#ifdef FEAT_EVAL
915 if (dark != (*p_bg == 'd')
916 && get_var_value((char_u *)"g:colors_name") != NULL)
917 {
918 // The color scheme must have set 'background' back to another
919 // value, that's not what we want here. Disable the color
920 // scheme and set the colors again.
921 do_unlet((char_u *)"g:colors_name", TRUE);
922 free_string_option(p_bg);
923 p_bg = vim_strsave((char_u *)(dark ? "dark" : "light"));
924 check_string_option(&p_bg);
925 init_highlight(FALSE, FALSE);
926 }
927#endif
928#ifdef FEAT_TERMINAL
929 term_update_colors_all();
930#endif
931
932 return NULL;
933}
934
935/*
936 * The 'wildmode' option is changed.
937 */
938 static char *
939did_set_wildmode(void)
940{
941 if (check_opt_wim() == FAIL)
942 return e_invalid_argument;
943 return NULL;
944}
945
946#ifdef FEAT_WAK
947/*
948 * The 'winaltkeys' option is changed.
949 */
950 static char *
951did_set_winaltkeys(void)
952{
953 char *errmsg = NULL;
954
955 if (*p_wak == NUL
956 || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
957 errmsg = e_invalid_argument;
958# ifdef FEAT_MENU
959# if defined(FEAT_GUI_MOTIF)
960 else if (gui.in_use)
961 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
962# elif defined(FEAT_GUI_GTK)
963 else if (gui.in_use)
964 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
965# endif
966# endif
967 return errmsg;
968}
969#endif
970
971/*
972 * The 'eventignore' option is changed.
973 */
974 static char *
975did_set_eventignore(void)
976{
977 if (check_ei() == FAIL)
978 return e_invalid_argument;
979 return NULL;
980}
981
982/*
983 * One of the 'encoding', 'fileencoding', 'termencoding' or 'makeencoding'
984 * options is changed.
985 */
986 static char *
987did_set_encoding(char_u **varp, char_u **gvarp, int opt_flags)
988{
989 char *errmsg = NULL;
990 char_u *p;
991
992 if (gvarp == &p_fenc)
993 {
994 if (!curbuf->b_p_ma && opt_flags != OPT_GLOBAL)
995 errmsg = e_cannot_make_changes_modifiable_is_off;
996 else if (vim_strchr(*varp, ',') != NULL)
997 // No comma allowed in 'fileencoding'; catches confusing it
998 // with 'fileencodings'.
999 errmsg = e_invalid_argument;
1000 else
1001 {
1002 // May show a "+" in the title now.
1003 redraw_titles();
1004 // Add 'fileencoding' to the swap file.
1005 ml_setflags(curbuf);
1006 }
1007 }
1008 if (errmsg == NULL)
1009 {
1010 // canonize the value, so that STRCMP() can be used on it
1011 p = enc_canonize(*varp);
1012 if (p != NULL)
1013 {
1014 vim_free(*varp);
1015 *varp = p;
1016 }
1017 if (varp == &p_enc)
1018 {
1019 errmsg = mb_init();
1020 redraw_titles();
1021 }
1022 }
1023
1024#if defined(FEAT_GUI_GTK)
1025 if (errmsg == NULL && varp == &p_tenc && gui.in_use)
1026 {
1027 // GTK uses only a single encoding, and that is UTF-8.
1028 if (STRCMP(p_tenc, "utf-8") != 0)
1029 errmsg = e_cannot_be_changed_in_gtk_GUI;
1030 }
1031#endif
1032
1033 if (errmsg == NULL)
1034 {
1035#ifdef FEAT_KEYMAP
1036 // When 'keymap' is used and 'encoding' changes, reload the keymap
1037 // (with another encoding).
1038 if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
1039 (void)keymap_init();
1040#endif
1041
1042 // When 'termencoding' is not empty and 'encoding' changes or when
1043 // 'termencoding' changes, need to setup for keyboard input and
1044 // display output conversion.
1045 if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
1046 {
1047 if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
1048 || convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
1049 {
1050 semsg(_(e_cannot_convert_between_str_and_str),
1051 p_tenc, p_enc);
1052 errmsg = e_invalid_argument;
1053 }
1054 }
1055
1056#if defined(MSWIN)
1057 // $HOME may have characters in active code page.
1058 if (varp == &p_enc)
1059 init_homedir();
1060#endif
1061 }
1062
1063 return errmsg;
1064}
1065
1066#if defined(FEAT_POSTSCRIPT)
1067/*
1068 * The 'printencoding' option is changed.
1069 */
1070 static void
1071did_set_printencoding(void)
1072{
1073 char_u *s, *p;
1074
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00001075 // Canonize 'printencoding' if VIM standard one
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001076 p = enc_canonize(p_penc);
1077 if (p != NULL)
1078 {
1079 vim_free(p_penc);
1080 p_penc = p;
1081 }
1082 else
1083 {
1084 // Ensure lower case and '-' for '_'
1085 for (s = p_penc; *s != NUL; s++)
1086 {
1087 if (*s == '_')
1088 *s = '-';
1089 else
1090 *s = TOLOWER_ASC(*s);
1091 }
1092 }
1093}
1094#endif
1095
1096#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
1097/*
1098 * The 'imactivatekey' option is changed.
1099 */
1100 static char *
1101did_set_imactivatekey(void)
1102{
1103 if (!im_xim_isvalid_imactivate())
1104 return e_invalid_argument;
1105 return NULL;
1106}
1107#endif
1108
1109#ifdef FEAT_KEYMAP
1110/*
1111 * The 'keymap' option is changed.
1112 */
1113 static char *
1114did_set_keymap(char_u **varp, int opt_flags, int *value_checked)
1115{
1116 char *errmsg = NULL;
1117
1118 if (!valid_filetype(*varp))
1119 errmsg = e_invalid_argument;
1120 else
1121 {
1122 int secure_save = secure;
1123
1124 // Reset the secure flag, since the value of 'keymap' has
1125 // been checked to be safe.
1126 secure = 0;
1127
1128 // load or unload key mapping tables
1129 errmsg = keymap_init();
1130
1131 secure = secure_save;
1132
1133 // Since we check the value, there is no need to set P_INSECURE,
1134 // even when the value comes from a modeline.
1135 *value_checked = TRUE;
1136 }
1137
1138 if (errmsg == NULL)
1139 {
1140 if (*curbuf->b_p_keymap != NUL)
1141 {
1142 // Installed a new keymap, switch on using it.
1143 curbuf->b_p_iminsert = B_IMODE_LMAP;
1144 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
1145 curbuf->b_p_imsearch = B_IMODE_LMAP;
1146 }
1147 else
1148 {
1149 // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
1150 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
1151 curbuf->b_p_iminsert = B_IMODE_NONE;
1152 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
1153 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
1154 }
1155 if ((opt_flags & OPT_LOCAL) == 0)
1156 {
1157 set_iminsert_global();
1158 set_imsearch_global();
1159 }
1160 status_redraw_curbuf();
1161 }
1162
1163 return errmsg;
1164}
1165#endif
1166
1167/*
1168 * The 'fileformat' option is changed.
1169 */
1170 static char *
1171did_set_fileformat(char_u **varp, char_u *oldval, int opt_flags)
1172{
1173 if (!curbuf->b_p_ma && !(opt_flags & OPT_GLOBAL))
1174 return e_cannot_make_changes_modifiable_is_off;
1175 else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
1176 return e_invalid_argument;
1177
1178 // may also change 'textmode'
1179 if (get_fileformat(curbuf) == EOL_DOS)
1180 curbuf->b_p_tx = TRUE;
1181 else
1182 curbuf->b_p_tx = FALSE;
1183 redraw_titles();
1184 // update flag in swap file
1185 ml_setflags(curbuf);
1186 // Redraw needed when switching to/from "mac": a CR in the text
1187 // will be displayed differently.
1188 if (get_fileformat(curbuf) == EOL_MAC || *oldval == 'm')
1189 redraw_curbuf_later(UPD_NOT_VALID);
1190
1191 return NULL;
1192}
1193
1194/*
1195 * The 'fileformats' option is changed.
1196 */
1197 static char *
1198did_set_fileformats(void)
1199{
1200 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
1201 return e_invalid_argument;
1202
1203 // also change 'textauto'
1204 if (*p_ffs == NUL)
1205 p_ta = FALSE;
1206 else
1207 p_ta = TRUE;
1208
1209 return NULL;
1210}
1211
1212#if defined(FEAT_CRYPT)
1213/*
1214 * The 'cryptkey' option is changed.
1215 */
1216 static void
1217did_set_cryptkey(char_u *oldval)
1218{
1219 // Make sure the ":set" command doesn't show the new value in the
1220 // history.
1221 remove_key_from_history();
1222
1223 if (STRCMP(curbuf->b_p_key, oldval) != 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001224 {
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00001225 // Need to update the swapfile.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001226 ml_set_crypt_key(curbuf, oldval,
1227 *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
1228 changed_internal();
1229 }
1230}
1231
1232/*
1233 * The 'cryptmethod' option is changed.
1234 */
1235 static char *
1236did_set_cryptmethod(char_u *oldval, int opt_flags)
1237{
1238 char_u *p;
1239 char_u *s;
1240
1241 if (opt_flags & OPT_LOCAL)
1242 p = curbuf->b_p_cm;
1243 else
1244 p = p_cm;
1245 if (check_opt_strings(p, p_cm_values, TRUE) != OK)
1246 return e_invalid_argument;
1247 else if (crypt_self_test() == FAIL)
1248 return e_invalid_argument;
1249
1250 // When setting the global value to empty, make it "zip".
1251 if (*p_cm == NUL)
1252 {
1253 free_string_option(p_cm);
1254 p_cm = vim_strsave((char_u *)"zip");
1255 }
1256 // When using ":set cm=name" the local value is going to be empty.
1257 // Do that here, otherwise the crypt functions will still use the
1258 // local value.
1259 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1260 {
1261 free_string_option(curbuf->b_p_cm);
1262 curbuf->b_p_cm = empty_option;
1263 }
1264
1265 // Need to update the swapfile when the effective method changed.
1266 // Set "s" to the effective old value, "p" to the effective new
1267 // method and compare.
1268 if ((opt_flags & OPT_LOCAL) && *oldval == NUL)
1269 s = p_cm; // was previously using the global value
1270 else
1271 s = oldval;
1272 if (*curbuf->b_p_cm == NUL)
1273 p = p_cm; // is now using the global value
1274 else
1275 p = curbuf->b_p_cm;
1276 if (STRCMP(s, p) != 0)
1277 ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1278
1279 // If the global value changes need to update the swapfile for all
1280 // buffers using that value.
1281 if ((opt_flags & OPT_GLOBAL) && STRCMP(p_cm, oldval) != 0)
1282 {
1283 buf_T *buf;
1284
1285 FOR_ALL_BUFFERS(buf)
1286 if (buf != curbuf && *buf->b_p_cm == NUL)
1287 ml_set_crypt_key(buf, buf->b_p_key, oldval);
1288 }
1289 return NULL;
1290}
1291#endif
1292
1293/*
1294 * The 'matchpairs' option is changed.
1295 */
1296 static char *
1297did_set_matchpairs(char_u **varp)
1298{
1299 char_u *p;
1300
1301 if (has_mbyte)
1302 {
1303 for (p = *varp; *p != NUL; ++p)
1304 {
1305 int x2 = -1;
1306 int x3 = -1;
1307
1308 p += mb_ptr2len(p);
1309 if (*p != NUL)
1310 x2 = *p++;
1311 if (*p != NUL)
1312 {
1313 x3 = mb_ptr2char(p);
1314 p += mb_ptr2len(p);
1315 }
1316 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
1317 return e_invalid_argument;
1318 if (*p == NUL)
1319 break;
1320 }
1321 }
1322 else
1323 {
1324 // Check for "x:y,x:y"
1325 for (p = *varp; *p != NUL; p += 4)
1326 {
1327 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
1328 return e_invalid_argument;
1329 if (p[3] == NUL)
1330 break;
1331 }
1332 }
1333
1334 return NULL;
1335}
1336
1337/*
1338 * The 'comments' option is changed.
1339 */
1340 static char *
1341did_set_comments(char_u **varp, char *errbuf)
1342{
1343 char_u *s;
1344 char *errmsg = NULL;
1345
1346 for (s = *varp; *s; )
1347 {
1348 while (*s && *s != ':')
1349 {
1350 if (vim_strchr((char_u *)COM_ALL, *s) == NULL
1351 && !VIM_ISDIGIT(*s) && *s != '-')
1352 {
1353 errmsg = illegal_char(errbuf, *s);
1354 break;
1355 }
1356 ++s;
1357 }
1358 if (*s++ == NUL)
1359 errmsg = e_missing_colon;
1360 else if (*s == ',' || *s == NUL)
1361 errmsg = e_zero_length_string;
1362 if (errmsg != NULL)
1363 break;
1364 while (*s && *s != ',')
1365 {
1366 if (*s == '\\' && s[1] != NUL)
1367 ++s;
1368 ++s;
1369 }
1370 s = skip_to_option_part(s);
1371 }
1372
1373 return errmsg;
1374}
1375
1376/*
1377 * The global 'listchars' or 'fillchars' option is changed.
1378 */
1379 static char *
1380did_set_global_listfillchars(char_u **varp, int opt_flags)
1381{
1382 char *errmsg = NULL;
1383 char_u **local_ptr = varp == &p_lcs
1384 ? &curwin->w_p_lcs : &curwin->w_p_fcs;
1385
1386 // only apply the global value to "curwin" when it does not have a
1387 // local value
1388 errmsg = set_chars_option(curwin, varp,
1389 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL));
1390 if (errmsg != NULL)
1391 return errmsg;
1392
1393 tabpage_T *tp;
1394 win_T *wp;
1395
1396 // If the current window is set to use the global
1397 // 'listchars'/'fillchars' value, clear the window-local value.
1398 if (!(opt_flags & OPT_GLOBAL))
1399 clear_string_option(local_ptr);
1400 FOR_ALL_TAB_WINDOWS(tp, wp)
1401 {
1402 // If the current window has a local value need to apply it
1403 // again, it was changed when setting the global value.
1404 // If no error was returned above, we don't expect an error
1405 // here, so ignore the return value.
1406 local_ptr = varp == &p_lcs ? &wp->w_p_lcs : &wp->w_p_fcs;
1407 if (**local_ptr == NUL)
1408 (void)set_chars_option(wp, local_ptr, TRUE);
1409 }
1410
1411 redraw_all_later(UPD_NOT_VALID);
1412
1413 return NULL;
1414}
1415
1416/*
1417 * The 'verbosefile' option is changed.
1418 */
1419 static char *
1420did_set_verbosefile(void)
1421{
1422 verbose_stop();
1423 if (*p_vfile != NUL && verbose_open() == FAIL)
1424 return e_invalid_argument;
1425
1426 return NULL;
1427}
1428
1429#ifdef FEAT_VIMINFO
1430/*
1431 * The 'viminfo' option is changed.
1432 */
1433 static char *
1434did_set_viminfo(char *errbuf)
1435{
1436 char_u *s;
1437 char *errmsg = NULL;
1438
1439 for (s = p_viminfo; *s;)
1440 {
1441 // Check it's a valid character
1442 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
1443 {
1444 errmsg = illegal_char(errbuf, *s);
1445 break;
1446 }
1447 if (*s == 'n') // name is always last one
1448 break;
1449 else if (*s == 'r') // skip until next ','
1450 {
1451 while (*++s && *s != ',')
1452 ;
1453 }
1454 else if (*s == '%')
1455 {
1456 // optional number
1457 while (vim_isdigit(*++s))
1458 ;
1459 }
1460 else if (*s == '!' || *s == 'h' || *s == 'c')
1461 ++s; // no extra chars
1462 else // must have a number
1463 {
1464 while (vim_isdigit(*++s))
1465 ;
1466
1467 if (!VIM_ISDIGIT(*(s - 1)))
1468 {
1469 if (errbuf != NULL)
1470 {
1471 sprintf(errbuf,
1472 _(e_missing_number_after_angle_str_angle),
1473 transchar_byte(*(s - 1)));
1474 errmsg = errbuf;
1475 }
1476 else
1477 errmsg = "";
1478 break;
1479 }
1480 }
1481 if (*s == ',')
1482 ++s;
1483 else if (*s)
1484 {
1485 if (errbuf != NULL)
1486 errmsg = e_missing_comma;
1487 else
1488 errmsg = "";
1489 break;
1490 }
1491 }
1492 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
1493 errmsg = e_must_specify_a_value;
1494
1495 return errmsg;
1496}
1497#endif
1498
1499/*
1500 * Some terminal option (t_xxx) is changed
1501 */
1502 static void
1503did_set_term_option(char_u **varp, int *did_swaptcap UNUSED)
1504{
1505 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
1506 if (varp == &T_CCO)
1507 {
1508 int colors = atoi((char *)T_CCO);
1509
1510 // Only reinitialize colors if t_Co value has really changed to
1511 // avoid expensive reload of colorscheme if t_Co is set to the
1512 // same value multiple times.
1513 if (colors != t_colors)
1514 {
1515 t_colors = colors;
1516 if (t_colors <= 1)
1517 {
1518 vim_free(T_CCO);
1519 T_CCO = empty_option;
1520 }
1521#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1522 if (is_term_win32())
1523 {
1524 swap_tcap();
1525 *did_swaptcap = TRUE;
1526 }
1527#endif
1528 // We now have a different color setup, initialize it again.
1529 init_highlight(TRUE, FALSE);
1530 }
1531 }
1532 ttest(FALSE);
1533 if (varp == &T_ME)
1534 {
1535 out_str(T_ME);
1536 redraw_later(UPD_CLEAR);
1537#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
1538 // Since t_me has been set, this probably means that the user
1539 // wants to use this as default colors. Need to reset default
1540 // background/foreground colors.
1541# ifdef VIMDLL
1542 if (!gui.in_use && !gui.starting)
1543# endif
1544 mch_set_normal_colors();
1545#endif
1546 }
1547 if (varp == &T_BE && termcap_active)
1548 {
1549 MAY_WANT_TO_LOG_THIS;
1550
1551 if (*T_BE == NUL)
1552 // When clearing t_BE we assume the user no longer wants
1553 // bracketed paste, thus disable it by writing t_BD.
1554 out_str(T_BD);
1555 else
1556 out_str(T_BE);
1557 }
1558}
1559
1560#ifdef FEAT_LINEBREAK
1561/*
1562 * The 'showbreak' option is changed.
1563 */
1564 static char *
1565did_set_showbreak(char_u **varp)
1566{
1567 char_u *s;
1568
1569 for (s = *varp; *s; )
1570 {
1571 if (ptr2cells(s) != 1)
1572 return e_showbreak_contains_unprintable_or_wide_character;
1573 MB_PTR_ADV(s);
1574 }
1575
1576 return NULL;
1577}
1578#endif
1579
1580#ifdef FEAT_GUI
1581/*
1582 * The 'guifont' option is changed.
1583 */
1584 static char *
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00001585did_set_guifont(char_u *oldval UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001586{
1587 char_u *p;
1588 char *errmsg = NULL;
1589
1590 if (gui.in_use)
1591 {
1592 p = p_guifont;
1593# if defined(FEAT_GUI_GTK)
1594 // Put up a font dialog and let the user select a new value.
1595 // If this is cancelled go back to the old value but don't
1596 // give an error message.
1597 if (STRCMP(p, "*") == 0)
1598 {
1599 p = gui_mch_font_dialog(oldval);
1600 free_string_option(p_guifont);
1601 p_guifont = (p != NULL) ? p : vim_strsave(oldval);
1602 }
1603# endif
1604 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
1605 {
1606# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
1607 if (STRCMP(p_guifont, "*") == 0)
1608 {
1609 // Dialog was cancelled: Keep the old value without giving
1610 // an error message.
1611 free_string_option(p_guifont);
1612 p_guifont = vim_strsave(oldval);
1613 }
1614 else
1615# endif
1616 errmsg = e_invalid_fonts;
1617 }
1618 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001619
1620 return errmsg;
1621}
1622
1623# ifdef FEAT_XFONTSET
1624/*
1625 * The 'guifontset' option is changed.
1626 */
1627 static char *
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00001628did_set_guifontset()
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001629{
1630 char *errmsg = NULL;
1631
1632 if (STRCMP(p_guifontset, "*") == 0)
1633 errmsg = e_cant_select_fontset;
1634 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
1635 errmsg = e_invalid_fontset;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001636
1637 return errmsg;
1638}
1639# endif
1640
1641/*
1642 * The 'guifontwide' option is changed.
1643 */
1644 static char *
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00001645did_set_guifontwide(void)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001646{
1647 char *errmsg = NULL;
1648
1649 if (STRCMP(p_guifontwide, "*") == 0)
1650 errmsg = e_cant_select_wide_font;
1651 else if (gui_get_wide_font() == FAIL)
1652 errmsg = e_invalid_wide_font;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001653
1654 return errmsg;
1655}
1656#endif
1657
1658#if defined(FEAT_GUI_GTK)
1659 static void
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00001660did_set_guiligatures(void)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001661{
1662 gui_set_ligatures();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001663}
1664#endif
1665
1666#ifdef FEAT_MOUSESHAPE
1667 static char *
1668did_set_mouseshape(void)
1669{
1670 char *errmsg = NULL;
1671
1672 errmsg = parse_shape_opt(SHAPE_MOUSE);
1673 update_mouseshape(-1);
1674
1675 return errmsg;
1676}
1677#endif
1678
1679/*
1680 * The 'titlestring' or the 'iconstring' option is changed.
1681 */
1682 static void
1683did_set_titleiconstring(char_u **varp UNUSED)
1684{
1685#ifdef FEAT_STL_OPT
1686 int flagval = (varp == &p_titlestring) ? STL_IN_TITLE : STL_IN_ICON;
1687
1688 // NULL => statusline syntax
1689 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
1690 stl_syntax |= flagval;
1691 else
1692 stl_syntax &= ~flagval;
1693#endif
1694 did_set_title();
1695}
1696
1697#ifdef FEAT_GUI
1698/*
1699 * The 'guioptions' option is changed.
1700 */
1701 static void
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00001702did_set_guioptions(char_u *oldval)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001703{
1704 gui_init_which_components(oldval);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001705}
1706#endif
1707
1708#if defined(FEAT_GUI_TABLINE)
1709 static void
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00001710did_set_guitablabel()
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001711{
1712 redraw_tabline = TRUE;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001713}
1714#endif
1715
1716#if defined(UNIX) || defined(VMS)
1717/*
1718 * The 'ttymouse' option is changed.
1719 */
1720 static char *
1721did_set_ttymouse(void)
1722{
1723 char *errmsg = NULL;
1724
1725 // Switch the mouse off before changing the escape sequences used for
1726 // that.
1727 mch_setmouse(FALSE);
1728 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
1729 errmsg = e_invalid_argument;
1730 else
1731 check_mouse_termcode();
1732 if (termcap_active)
1733 setmouse(); // may switch it on again
1734
1735 return errmsg;
1736}
1737#endif
1738
1739/*
1740 * The 'selection' option is changed.
1741 */
1742 static char *
1743did_set_selection(void)
1744{
1745 if (*p_sel == NUL
1746 || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
1747 return e_invalid_argument;
1748
1749 return NULL;
1750}
1751
1752#ifdef FEAT_BROWSE
1753/*
1754 * The 'browsedir' option is changed.
1755 */
1756 static char *
1757did_set_browsedir(void)
1758{
1759 if (check_opt_strings(p_bsdir, p_bsdir_values, FALSE) != OK
1760 && !mch_isdir(p_bsdir))
1761 return e_invalid_argument;
1762
1763 return NULL;
1764}
1765#endif
1766
1767/*
1768 * The 'keymodel' option is changed.
1769 */
1770 static char *
1771did_set_keymodel(void)
1772{
1773 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
1774 return e_invalid_argument;
1775
1776 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
1777 km_startsel = (vim_strchr(p_km, 'a') != NULL);
1778 return NULL;
1779}
1780
1781/*
1782 * The 'keyprotocol' option is changed.
1783 */
1784 static char *
1785did_set_keyprotocol(void)
1786{
1787 if (match_keyprotocol(NULL) == KEYPROTOCOL_FAIL)
1788 return e_invalid_argument;
1789
1790 return NULL;
1791}
1792
1793/*
1794 * The 'mousemodel' option is changed.
1795 */
1796 static char *
1797did_set_mousemodel(void)
1798{
1799 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
1800 return e_invalid_argument;
1801#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
1802 else if (*p_mousem != *oldval)
1803 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
1804 // to create or delete the popup menus.
1805 gui_motif_update_mousemodel(root_menu);
1806#endif
1807
1808 return NULL;
1809}
1810
1811/*
1812 * The 'display' option is changed.
1813 */
1814 static char *
1815did_set_display(void)
1816{
1817 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
1818 return e_invalid_argument;
1819
1820 (void)init_chartab();
1821 return NULL;
1822}
1823
1824#ifdef FEAT_SPELL
1825/*
1826 * The 'spellfile' option is changed.
1827 */
1828 static char *
1829did_set_spellfile(char_u **varp)
1830{
1831 if (!valid_spellfile(*varp))
1832 return e_invalid_argument;
1833
1834 // If there is a window for this buffer in which 'spell' is set load the
1835 // wordlists.
1836 return did_set_spell_option(TRUE);
1837}
1838
1839/*
1840 * The 'spell' option is changed.
1841 */
1842 static char *
1843did_set_spell(char_u **varp)
1844{
1845 if (!valid_spelllang(*varp))
1846 return e_invalid_argument;
1847
1848 // If there is a window for this buffer in which 'spell' is set load the
1849 // wordlists.
1850 return did_set_spell_option(FALSE);
1851}
1852
1853/*
1854 * The 'spellcapcheck' option is changed.
1855 */
1856 static char *
1857did_set_spellcapcheck(void)
1858{
1859 // compile the regexp program.
1860 return compile_cap_prog(curwin->w_s);
1861}
1862
1863/*
1864 * The 'spelloptions' option is changed.
1865 */
1866 static char *
1867did_set_spelloptions(char_u **varp)
1868{
1869 if (**varp != NUL && STRCMP("camel", *varp) != 0)
1870 return e_invalid_argument;
1871
1872 return NULL;
1873}
1874
1875/*
1876 * The 'spellsuggest' option is changed.
1877 */
1878 static char *
1879did_set_spellsuggest(void)
1880{
1881 if (spell_check_sps() != OK)
1882 return e_invalid_argument;
1883
1884 return NULL;
1885}
1886
1887/*
1888 * The 'mkspellmem' option is changed.
1889 */
1890 static char *
1891did_set_mkspellmem(void)
1892{
1893 if (spell_check_msm() != OK)
1894 return e_invalid_argument;
1895
1896 return NULL;
1897}
1898#endif
1899
1900/*
1901 * The 'buftype' option is changed.
1902 */
1903 static char *
1904did_set_buftype(void)
1905{
1906 if (check_opt_strings(curbuf->b_p_bt, p_buftype_values, FALSE) != OK)
1907 return e_invalid_argument;
1908
1909 if (curwin->w_status_height)
1910 {
1911 curwin->w_redr_status = TRUE;
1912 redraw_later(UPD_VALID);
1913 }
1914 curbuf->b_help = (curbuf->b_p_bt[0] == 'h');
1915 redraw_titles();
1916
1917 return NULL;
1918}
1919
1920#ifdef FEAT_STL_OPT
1921/*
1922 * The 'statusline' or the 'tabline' or the 'rulerformat' option is changed.
1923 */
1924 static char *
1925did_set_statusline(char_u **varp)
1926{
1927 char_u *s;
1928 char *errmsg = NULL;
1929 int wid;
1930
1931 if (varp == &p_ruf) // reset ru_wid first
1932 ru_wid = 0;
1933 s = *varp;
1934 if (varp == &p_ruf && *s == '%')
1935 {
1936 // set ru_wid if 'ruf' starts with "%99("
1937 if (*++s == '-') // ignore a '-'
1938 s++;
1939 wid = getdigits(&s);
1940 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
1941 ru_wid = wid;
1942 else
1943 errmsg = check_stl_option(p_ruf);
1944 }
1945 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
1946 else if (varp == &p_ruf || s[0] != '%' || s[1] != '!')
1947 errmsg = check_stl_option(s);
1948 if (varp == &p_ruf && errmsg == NULL)
1949 comp_col();
1950
1951 return errmsg;
1952}
1953#endif
1954
1955/*
1956 * The 'complete' option is changed.
1957 */
1958 static char *
1959did_set_complete(char_u **varp, char *errbuf)
1960{
1961 char_u *s;
1962
1963 // check if it is a valid value for 'complete' -- Acevedo
1964 for (s = *varp; *s;)
1965 {
1966 while (*s == ',' || *s == ' ')
1967 s++;
1968 if (!*s)
1969 break;
1970 if (vim_strchr((char_u *)".wbuksid]tU", *s) == NULL)
1971 return illegal_char(errbuf, *s);
1972 if (*++s != NUL && *s != ',' && *s != ' ')
1973 {
1974 if (s[-1] == 'k' || s[-1] == 's')
1975 {
1976 // skip optional filename after 'k' and 's'
1977 while (*s && *s != ',' && *s != ' ')
1978 {
1979 if (*s == '\\' && s[1] != NUL)
1980 ++s;
1981 ++s;
1982 }
1983 }
1984 else
1985 {
1986 if (errbuf != NULL)
1987 {
1988 sprintf((char *)errbuf,
1989 _(e_illegal_character_after_chr), *--s);
1990 return errbuf;
1991 }
1992 return "";
1993 }
1994 }
1995 }
1996
1997 return NULL;
1998}
1999
2000/*
2001 * The 'completeopt' option is changed.
2002 */
2003 static char *
2004did_set_completeopt(void)
2005{
2006 if (check_opt_strings(p_cot, p_cot_values, TRUE) != OK)
2007 return e_invalid_argument;
2008
2009 completeopt_was_set();
2010 return NULL;
2011}
2012
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002013#ifdef BACKSLASH_IN_FILENAME
2014/*
2015 * The 'completeslash' option is changed.
2016 */
2017 static char *
2018did_set_completeslash(void)
2019{
2020 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
2021 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
2022 return e_invalid_argument;
2023
2024 return NULL;
2025}
2026#endif
2027
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002028#ifdef FEAT_SIGNS
2029/*
2030 * The 'signcolumn' option is changed.
2031 */
2032 static char *
2033did_set_signcolumn(char_u **varp, char_u *oldval)
2034{
2035 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
2036 return e_invalid_argument;
2037 // When changing the 'signcolumn' to or from 'number', recompute the
2038 // width of the number column if 'number' or 'relativenumber' is set.
2039 if (((*oldval == 'n' && *(oldval + 1) == 'u')
2040 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
2041 && (curwin->w_p_nu || curwin->w_p_rnu))
2042 curwin->w_nrwidth_line_count = 0;
2043
2044 return NULL;
2045}
2046#endif
2047
2048#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
2049/*
2050 * The 'toolbar' option is changed.
2051 */
2052 static char *
2053did_set_toolbar(void)
2054{
2055 if (opt_strings_flags(p_toolbar, p_toolbar_values,
2056 &toolbar_flags, TRUE) != OK)
2057 return e_invalid_argument;
2058
2059 out_flush();
2060 gui_mch_show_toolbar((toolbar_flags &
2061 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
2062 return NULL;
2063}
2064#endif
2065
2066#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
2067/*
2068 * The 'toolbariconsize' option is changed. GTK+ 2 only.
2069 */
2070 static char *
2071did_set_toolbariconsize(void)
2072{
2073 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
2074 return e_invalid_argument;
2075
2076 out_flush();
2077 gui_mch_show_toolbar((toolbar_flags &
2078 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
2079 return NULL;
2080}
2081#endif
2082
2083/*
2084 * The 'pastetoggle' option is changed.
2085 */
2086 static void
2087did_set_pastetoggle(void)
2088{
2089 char_u *p;
2090
2091 // translate key codes like in a mapping
2092 if (*p_pt)
2093 {
2094 (void)replace_termcodes(p_pt, &p,
2095 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
2096 if (p != NULL)
2097 {
2098 free_string_option(p_pt);
2099 p_pt = p;
2100 }
2101 }
2102}
2103
2104/*
2105 * The 'backspace' option is changed.
2106 */
2107 static char *
2108did_set_backspace(void)
2109{
2110 if (VIM_ISDIGIT(*p_bs))
2111 {
2112 if (*p_bs > '3' || p_bs[1] != NUL)
2113 return e_invalid_argument;
2114 }
2115 else if (check_opt_strings(p_bs, p_bs_values, TRUE) != OK)
2116 return e_invalid_argument;
2117
2118 return NULL;
2119}
2120
2121/*
2122 * The 'tagcase' option is changed.
2123 */
2124 static char *
2125did_set_tagcase(int opt_flags)
2126{
2127 unsigned int *flags;
2128 char_u *p;
2129
2130 if (opt_flags & OPT_LOCAL)
2131 {
2132 p = curbuf->b_p_tc;
2133 flags = &curbuf->b_tc_flags;
2134 }
2135 else
2136 {
2137 p = p_tc;
2138 flags = &tc_flags;
2139 }
2140
2141 if ((opt_flags & OPT_LOCAL) && *p == NUL)
2142 // make the local value empty: use the global value
2143 *flags = 0;
2144 else if (*p == NUL
2145 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
2146 return e_invalid_argument;
2147
2148 return NULL;
2149}
2150
2151#ifdef FEAT_DIFF
2152/*
2153 * The 'diffopt' option is changed.
2154 */
2155 static char *
2156did_set_diffopt(void)
2157{
2158 if (diffopt_changed() == FAIL)
2159 return e_invalid_argument;
2160
2161 return NULL;
2162}
2163#endif
2164
2165#ifdef FEAT_FOLDING
2166/*
2167 * The 'foldmethod' option is changed.
2168 */
2169 static char *
2170did_set_foldmethod(char_u **varp)
2171{
2172 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK
2173 || *curwin->w_p_fdm == NUL)
2174 return e_invalid_argument;
2175
2176 foldUpdateAll(curwin);
2177 if (foldmethodIsDiff(curwin))
2178 newFoldLevel();
2179 return NULL;
2180}
2181
2182/*
2183 * The 'foldmarker' option is changed.
2184 */
2185 static char *
2186did_set_foldmarker(char_u **varp)
2187{
2188 char_u *p;
2189
2190 p = vim_strchr(*varp, ',');
2191 if (p == NULL)
2192 return e_comma_required;
2193 else if (p == *varp || p[1] == NUL)
2194 return e_invalid_argument;
2195 else if (foldmethodIsMarker(curwin))
2196 foldUpdateAll(curwin);
2197
2198 return NULL;
2199}
2200
2201/*
2202 * The 'commentstring' option is changed.
2203 */
2204 static char *
2205did_set_commentstring(char_u **varp)
2206{
2207 if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
2208 return e_commentstring_must_be_empty_or_contain_str;
2209
2210 return NULL;
2211}
2212
2213/*
2214 * The 'foldignore' option is changed.
2215 */
2216 static void
2217did_set_foldignore(void)
2218{
2219 if (foldmethodIsIndent(curwin))
2220 foldUpdateAll(curwin);
2221}
2222#endif
2223
2224/*
2225 * The 'virtualedit' option is changed.
2226 */
2227 static char *
2228did_set_virtualedit(char_u *oldval, int opt_flags)
2229{
2230 char_u *ve = p_ve;
2231 unsigned int *flags = &ve_flags;
2232
2233 if (opt_flags & OPT_LOCAL)
2234 {
2235 ve = curwin->w_p_ve;
2236 flags = &curwin->w_ve_flags;
2237 }
2238
2239 if ((opt_flags & OPT_LOCAL) && *ve == NUL)
2240 // make the local value empty: use the global value
2241 *flags = 0;
2242 else
2243 {
2244 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
2245 return e_invalid_argument;
2246 else if (STRCMP(ve, oldval) != 0)
2247 {
2248 // Recompute cursor position in case the new 've' setting
2249 // changes something.
2250 validate_virtcol();
2251 coladvance(curwin->w_virtcol);
2252 }
2253 }
2254
2255 return NULL;
2256}
2257
2258#if defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)
2259/*
2260 * The 'cscopequickfix' option is changed.
2261 */
2262 static char *
2263did_set_cscopequickfix(void)
2264{
2265 char_u *p;
2266
2267 if (p_csqf == NULL)
2268 return NULL;
2269
2270 p = p_csqf;
2271 while (*p != NUL)
2272 {
2273 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
2274 || p[1] == NUL
2275 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
2276 || (p[2] != NUL && p[2] != ','))
2277 return e_invalid_argument;
2278 else if (p[2] == NUL)
2279 break;
2280 else
2281 p += 3;
2282 }
2283
2284 return NULL;
2285}
2286#endif
2287
2288/*
2289 * The 'cinoptions' option is changed.
2290 */
2291 static void
2292did_set_cinoptions(void)
2293{
2294 // TODO: recognize errors
2295 parse_cino(curbuf);
2296}
2297
2298/*
2299 * The 'lispoptions' option is changed.
2300 */
2301 static char *
2302did_set_lispoptions(char_u **varp)
2303{
2304 if (**varp != NUL && STRCMP(*varp, "expr:0") != 0
2305 && STRCMP(*varp, "expr:1") != 0)
2306 return e_invalid_argument;
2307
2308 return NULL;
2309}
2310
2311#if defined(FEAT_RENDER_OPTIONS)
2312/*
2313 * The 'renderoptions' option is changed.
2314 */
2315 static char *
2316did_set_renderoptions(void)
2317{
2318 if (!gui_mch_set_rendering_options(p_rop))
2319 return e_invalid_argument;
2320
2321 return NULL;
2322}
2323#endif
2324
2325/*
2326 * The 'filetype' or the 'syntax' option is changed.
2327 */
2328 static char *
2329did_set_filetype_or_syntax(
2330 char_u **varp,
2331 char_u *oldval,
2332 int *value_checked,
2333 int *value_changed)
2334{
2335 if (!valid_filetype(*varp))
2336 return e_invalid_argument;
2337
2338 *value_changed = STRCMP(oldval, *varp) != 0;
2339
2340 // Since we check the value, there is no need to set P_INSECURE,
2341 // even when the value comes from a modeline.
2342 *value_checked = TRUE;
2343
2344 return NULL;
2345}
2346
2347#ifdef FEAT_TERMINAL
2348/*
2349 * The 'termwinkey' option is changed.
2350 */
2351 static char *
2352did_set_termwinkey(void)
2353{
2354 if (*curwin->w_p_twk != NUL
2355 && string_to_key(curwin->w_p_twk, TRUE) == 0)
2356 return e_invalid_argument;
2357
2358 return NULL;
2359}
2360
2361/*
2362 * The 'termwinsize' option is changed.
2363 */
2364 static char *
2365did_set_termwinsize(void)
2366{
2367 char_u *p;
2368
2369 if (*curwin->w_p_tws == NUL)
2370 return NULL;
2371
2372 p = skipdigits(curwin->w_p_tws);
2373 if (p == curwin->w_p_tws
2374 || (*p != 'x' && *p != '*')
2375 || *skipdigits(p + 1) != NUL)
2376 return e_invalid_argument;
2377
2378 return NULL;
2379}
2380#endif
2381
2382#ifdef FEAT_VARTABS
2383/*
2384 * The 'varsofttabstop' option is changed.
2385 */
2386 static char *
2387did_set_varsofttabstop(char_u **varp)
2388{
2389 char_u *cp;
2390
2391 if (!(*varp)[0] || ((*varp)[0] == '0' && !(*varp)[1]))
2392 {
2393 if (curbuf->b_p_vsts_array)
2394 {
2395 vim_free(curbuf->b_p_vsts_array);
2396 curbuf->b_p_vsts_array = 0;
2397 }
2398 }
2399 else
2400 {
2401 for (cp = *varp; *cp; ++cp)
2402 {
2403 if (vim_isdigit(*cp))
2404 continue;
2405 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
2406 continue;
2407 return e_invalid_argument;
2408 }
2409
2410 int *oldarray = curbuf->b_p_vsts_array;
2411 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
2412 {
2413 if (oldarray)
2414 vim_free(oldarray);
2415 }
2416 else
2417 return e_invalid_argument;
2418 }
2419
2420 return NULL;
2421}
2422
2423/*
2424 * The 'vartabstop' option is changed.
2425 */
2426 static char *
2427did_set_vartabstop(char_u **varp)
2428{
2429 char_u *cp;
2430
2431 if (!(*varp)[0] || ((*varp)[0] == '0' && !(*varp)[1]))
2432 {
2433 if (curbuf->b_p_vts_array)
2434 {
2435 vim_free(curbuf->b_p_vts_array);
2436 curbuf->b_p_vts_array = NULL;
2437 }
2438 }
2439 else
2440 {
2441 for (cp = *varp; *cp; ++cp)
2442 {
2443 if (vim_isdigit(*cp))
2444 continue;
2445 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
2446 continue;
2447 return e_invalid_argument;
2448 }
2449
2450 int *oldarray = curbuf->b_p_vts_array;
2451
2452 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
2453 {
2454 vim_free(oldarray);
2455# ifdef FEAT_FOLDING
2456 if (foldmethodIsIndent(curwin))
2457 foldUpdateAll(curwin);
2458# endif
2459 }
2460 else
2461 return e_invalid_argument;
2462 }
2463
2464 return NULL;
2465}
2466#endif
2467
2468#ifdef FEAT_PROP_POPUP
2469/*
2470 * The 'previewpopup' option is changed.
2471 */
2472 static char *
2473did_set_previewpopup(void)
2474{
2475 if (parse_previewpopup(NULL) == FAIL)
2476 return e_invalid_argument;
2477
2478 return NULL;
2479}
2480
2481# ifdef FEAT_QUICKFIX
2482/*
2483 * The 'completepopup' option is changed.
2484 */
2485 static char *
2486did_set_completepopup(void)
2487{
2488 if (parse_completepopup(NULL) == FAIL)
2489 return e_invalid_argument;
2490
2491 popup_close_info();
2492 return NULL;
2493}
2494# endif
2495#endif
2496
2497#ifdef FEAT_EVAL
2498/*
2499 * One of the '*expr' options is changed: 'balloonexpr', 'diffexpr',
2500 * 'foldexpr', 'foldtext', 'formatexpr', 'includeexpr', 'indentexpr',
2501 * 'patchexpr', 'printexpr' and 'charconvert'.
2502 *
2503 */
2504 static void
2505did_set_optexpr(char_u **varp)
2506{
2507 // If the option value starts with <SID> or s:, then replace that with
2508 // the script identifier.
2509 char_u *name = get_scriptlocal_funcname(*varp);
2510 if (name != NULL)
2511 {
2512 free_string_option(*varp);
2513 *varp = name;
2514 }
2515
2516# ifdef FEAT_FOLDING
2517 if (varp == &curwin->w_p_fde && foldmethodIsExpr(curwin))
2518 foldUpdateAll(curwin);
2519# endif
2520}
2521#endif
2522
2523/*
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002524 * An option which is a list of flags is set. Valid values are in 'flags'.
2525 */
2526 static char *
2527did_set_option_listflag(char_u **varp, char_u *flags, char *errbuf)
2528{
2529 char_u *s;
2530
2531 for (s = *varp; *s; ++s)
2532 if (vim_strchr(flags, *s) == NULL)
2533 return illegal_char(errbuf, *s);
2534
2535 return NULL;
2536}
2537
2538#ifdef FEAT_SYN_HL
2539/*
2540 * When the 'syntax' option is set, load the syntax of that name.
2541 */
2542 static void
2543do_syntax_autocmd(int value_changed)
2544{
2545 static int syn_recursive = 0;
2546
2547 ++syn_recursive;
2548 // Only pass TRUE for "force" when the value changed or not used
2549 // recursively, to avoid endless recurrence.
2550 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
2551 value_changed || syn_recursive == 1, curbuf);
2552 curbuf->b_flags |= BF_SYN_SET;
2553 --syn_recursive;
2554}
2555#endif
2556
2557/*
2558 * When the 'filetype' option is set, trigger the FileType autocommand.
2559 */
2560 static void
2561do_filetype_autocmd(char_u **varp, int opt_flags, int value_changed)
2562{
2563 // Skip this when called from a modeline and the filetype was already set
2564 // to this value.
2565 if ((opt_flags & OPT_MODELINE) && !value_changed)
2566 return;
2567
2568 static int ft_recursive = 0;
2569 int secure_save = secure;
2570
2571 // Reset the secure flag, since the value of 'filetype' has
2572 // been checked to be safe.
2573 secure = 0;
2574
2575 ++ft_recursive;
2576 did_filetype = TRUE;
2577 // Only pass TRUE for "force" when the value changed or not
2578 // used recursively, to avoid endless recurrence.
2579 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
2580 value_changed || ft_recursive == 1, curbuf);
2581 --ft_recursive;
2582 // Just in case the old "curbuf" is now invalid.
2583 if (varp != &(curbuf->b_p_ft))
2584 varp = NULL;
2585
2586 secure = secure_save;
2587}
2588
2589#ifdef FEAT_SPELL
2590/*
2591 * When the 'spelllang' option is set, source the spell/LANG.vim file in
2592 * 'runtimepath'.
2593 */
2594 static void
2595do_spelllang_source(void)
2596{
2597 char_u fname[200];
2598 char_u *p;
2599 char_u *q = curwin->w_s->b_p_spl;
2600
2601 // Skip the first name if it is "cjk".
2602 if (STRNCMP(q, "cjk,", 4) == 0)
2603 q += 4;
2604
2605 // They could set 'spellcapcheck' depending on the language. Use the first
2606 // name in 'spelllang' up to '_region' or '.encoding'.
2607 for (p = q; *p != NUL; ++p)
2608 if (!ASCII_ISALNUM(*p) && *p != '-')
2609 break;
2610 if (p > q)
2611 {
2612 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
2613 (int)(p - q), q);
2614 source_runtime(fname, DIP_ALL);
2615 }
2616}
2617#endif
2618
2619/*
Bram Moolenaardac13472019-09-16 21:06:21 +02002620 * Handle string options that need some action to perform when changed.
zeertzjqf6782732022-07-27 18:26:03 +01002621 * The new value must be allocated.
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002622 * Returns NULL for success, or an untranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +02002623 */
2624 char *
2625did_set_string_option(
2626 int opt_idx, // index in options[] table
2627 char_u **varp, // pointer to the option variable
Bram Moolenaardac13472019-09-16 21:06:21 +02002628 char_u *oldval, // previous value of the option
2629 char *errbuf, // buffer for errors, or NULL
2630 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002631 int *value_checked) // value was checked to be safe, no
Bram Moolenaardac13472019-09-16 21:06:21 +02002632 // need to set P_INSECURE
2633{
2634 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +02002635 int did_chartab = FALSE;
2636 char_u **gvarp;
2637 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
2638#ifdef FEAT_GUI
2639 // set when changing an option that only requires a redraw in the GUI
2640 int redraw_gui_only = FALSE;
2641#endif
2642 int value_changed = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02002643 int did_swaptcap = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02002644
2645 // Get the global option to compare with, otherwise we would have to check
2646 // two values for all local options.
2647 gvarp = (char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
2648
2649 // Disallow changing some options from secure mode
2650 if ((secure
2651#ifdef HAVE_SANDBOX
2652 || sandbox != 0
2653#endif
2654 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00002655 errmsg = e_not_allowed_here;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002656 // Check for a "normal" directory or file name in some options.
2657 else if (check_illegal_path_names(opt_idx, varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002658 errmsg = e_invalid_argument;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002659 else if (varp == &T_NAME) // 'term'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002660 errmsg = did_set_term(&opt_idx, &free_oldval);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002661 else if (gvarp == &p_bkc) // 'backupcopy'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002662 errmsg = did_set_backupcopy(oldval, opt_flags);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002663 else if ( varp == &p_bex // 'backupext'
2664 || varp == &p_pm) // 'patchmode'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002665 errmsg = did_set_backupext_or_patchmode();
Bram Moolenaardac13472019-09-16 21:06:21 +02002666#ifdef FEAT_LINEBREAK
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002667 else if (varp == &curwin->w_p_briopt) // 'breakindentopt'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002668 errmsg = did_set_breakindentopt();
Bram Moolenaardac13472019-09-16 21:06:21 +02002669#endif
2670
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002671 else if ( varp == &p_isi // 'isident'
2672 || varp == &(curbuf->b_p_isk) // 'iskeyword'
2673 || varp == &p_isp // 'isprint'
2674 || varp == &p_isf) // 'isfname'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002675 errmsg = did_set_isopt(&did_chartab);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002676 else if (varp == &p_hf) // 'helpfile'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002677 did_set_helpfile();
Bram Moolenaardac13472019-09-16 21:06:21 +02002678#ifdef FEAT_SYN_HL
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002679 else if ( varp == &curwin->w_p_culopt // 'cursorlineopt'
2680 || gvarp == &curwin->w_allbuf_opt.wo_culopt)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002681 errmsg = did_set_cursorlineopt(varp);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002682 else if (varp == &curwin->w_p_cc) // 'colorcolumn'
Bram Moolenaardac13472019-09-16 21:06:21 +02002683 errmsg = check_colorcolumn(curwin);
2684#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002685#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002686 else if (varp == &p_hlg) // 'helplang'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002687 errmsg = did_set_helplang();
Bram Moolenaardac13472019-09-16 21:06:21 +02002688#endif
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002689 else if (varp == &p_hl) // 'highlight'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002690 errmsg = did_set_highlight();
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002691 else if (gvarp == &p_nf) // 'nrformats'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002692 errmsg = did_set_opt_strings(*varp, p_nf_values, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +02002693#ifdef FEAT_SESSION
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002694 else if (varp == &p_ssop) // 'sessionoptions'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002695 errmsg = did_set_sessionoptions(oldval);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002696 else if (varp == &p_vop) // 'viewoptions'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002697 errmsg = did_set_opt_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +02002698#endif
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002699 else if (varp == &p_sbo) // 'scrollopt'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002700 errmsg = did_set_opt_strings(p_sbo, p_scbopt_values, TRUE);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002701 else if ( varp == &p_ambw // 'ambiwidth'
2702 || varp == &p_emoji) // 'emoji'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002703 errmsg = did_set_ambiwidth();
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002704 else if (varp == &p_bg) // 'background'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002705 errmsg = did_set_background();
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002706 else if (varp == &p_wim) // 'wildmode'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002707 errmsg = did_set_wildmode();
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002708 else if (varp == &p_wop) // 'wildoptions'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002709 errmsg = did_set_opt_strings(p_wop, p_wop_values, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +02002710#ifdef FEAT_WAK
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002711 else if (varp == &p_wak) // 'winaltkeys'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002712 errmsg = did_set_winaltkeys();
Bram Moolenaardac13472019-09-16 21:06:21 +02002713#endif
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002714 else if (varp == &p_ei) // 'eventignore'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002715 errmsg = did_set_eventignore();
Bram Moolenaardac13472019-09-16 21:06:21 +02002716
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002717 else if ( varp == &p_enc // 'encoding'
2718 || gvarp == &p_fenc // 'fileencoding'
2719 || varp == &p_tenc // 'termencoding'
2720 || gvarp == &p_menc) // 'makeencoding'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002721 errmsg = did_set_encoding(varp, gvarp, opt_flags);
Bram Moolenaardac13472019-09-16 21:06:21 +02002722#if defined(FEAT_POSTSCRIPT)
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002723 else if (varp == &p_penc) // 'printencoding'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002724 did_set_printencoding();
Bram Moolenaardac13472019-09-16 21:06:21 +02002725#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002726#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002727 else if (varp == &p_imak) // 'imactivatekey'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002728 errmsg = did_set_imactivatekey();
Bram Moolenaardac13472019-09-16 21:06:21 +02002729#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002730#ifdef FEAT_KEYMAP
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002731 else if (varp == &curbuf->b_p_keymap) // 'keymap'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002732 errmsg = did_set_keymap(varp, opt_flags, value_checked);
Bram Moolenaardac13472019-09-16 21:06:21 +02002733#endif
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002734 else if (gvarp == &p_ff) // 'fileformat'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002735 errmsg = did_set_fileformat(varp, oldval, opt_flags);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002736 else if (varp == &p_ffs) // 'fileformats'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002737 errmsg = did_set_fileformats();
Bram Moolenaardac13472019-09-16 21:06:21 +02002738#if defined(FEAT_CRYPT)
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002739 else if (gvarp == &p_key) // 'cryptkey'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002740 did_set_cryptkey(oldval);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002741 else if (gvarp == &p_cm) // 'cryptmethod'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002742 errmsg = did_set_cryptmethod(oldval, opt_flags);
Bram Moolenaardac13472019-09-16 21:06:21 +02002743#endif
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002744 else if (gvarp == &p_mps) // 'matchpairs'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002745 errmsg = did_set_matchpairs(varp);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002746 else if (gvarp == &p_com) // 'comments'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002747 errmsg = did_set_comments(varp, errbuf);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002748 else if ( varp == &p_lcs // global 'listchars'
2749 || varp == &p_fcs) // global 'fillchars'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002750 errmsg = did_set_global_listfillchars(varp, opt_flags);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002751 else if (varp == &curwin->w_p_lcs) // local 'listchars'
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01002752 errmsg = set_chars_option(curwin, varp, TRUE);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002753 else if (varp == &curwin->w_p_fcs) // local 'fillchars'
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01002754 errmsg = set_chars_option(curwin, varp, TRUE);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002755 else if (varp == &p_cedit) // 'cedit'
Bram Moolenaardac13472019-09-16 21:06:21 +02002756 errmsg = check_cedit();
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002757 else if (varp == &p_vfile) // 'verbosefile'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002758 errmsg = did_set_verbosefile();
Bram Moolenaardac13472019-09-16 21:06:21 +02002759#ifdef FEAT_VIMINFO
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002760 else if (varp == &p_viminfo) // 'viminfo'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002761 errmsg = did_set_viminfo(errbuf);
Bram Moolenaardac13472019-09-16 21:06:21 +02002762#endif // FEAT_VIMINFO
Bram Moolenaardac13472019-09-16 21:06:21 +02002763 // terminal options
2764 else if (istermoption_idx(opt_idx) && full_screen)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002765 did_set_term_option(varp, &did_swaptcap);
Bram Moolenaardac13472019-09-16 21:06:21 +02002766#ifdef FEAT_LINEBREAK
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002767 else if (gvarp == &p_sbr) // 'showbreak'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002768 errmsg = did_set_showbreak(varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02002769#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002770#ifdef FEAT_GUI
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002771 else if (varp == &p_guifont) // 'guifont'
2772 {
2773 errmsg = did_set_guifont(oldval);
2774 redraw_gui_only = TRUE;
2775 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002776# ifdef FEAT_XFONTSET
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002777 else if (varp == &p_guifontset) // 'guifontset'
2778 {
2779 errmsg = did_set_guifontset();
2780 redraw_gui_only = TRUE;
2781 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002782# endif
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002783 else if (varp == &p_guifontwide) // 'guifontwide'
2784 {
2785 errmsg = did_set_guifontwide();
2786 redraw_gui_only = TRUE;
2787 }
Bram Moolenaardac13472019-09-16 21:06:21 +02002788#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002789#if defined(FEAT_GUI_GTK)
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002790 else if (varp == &p_guiligatures) // 'guiligatures'
2791 {
2792 did_set_guiligatures();
2793 redraw_gui_only = TRUE;
2794 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002795#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002796#ifdef CURSOR_SHAPE
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002797 else if (varp == &p_guicursor) // 'guicursor'
Bram Moolenaardac13472019-09-16 21:06:21 +02002798 errmsg = parse_shape_opt(SHAPE_CURSOR);
2799#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002800#ifdef FEAT_MOUSESHAPE
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002801 else if (varp == &p_mouseshape) // 'mouseshape'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002802 errmsg = did_set_mouseshape();
Bram Moolenaardac13472019-09-16 21:06:21 +02002803#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002804#ifdef FEAT_PRINTER
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002805 else if (varp == &p_popt) // 'printoptions'
Bram Moolenaardac13472019-09-16 21:06:21 +02002806 errmsg = parse_printoptions();
2807# if defined(FEAT_POSTSCRIPT)
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002808 else if (varp == &p_pmfn) // 'printmbfont'
Bram Moolenaardac13472019-09-16 21:06:21 +02002809 errmsg = parse_printmbfont();
2810# endif
2811#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002812#ifdef FEAT_LANGMAP
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002813 else if (varp == &p_langmap) // 'langmap'
Bram Moolenaardac13472019-09-16 21:06:21 +02002814 langmap_set();
2815#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002816#ifdef FEAT_LINEBREAK
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002817 else if (varp == &p_breakat) // 'breakat'
Bram Moolenaardac13472019-09-16 21:06:21 +02002818 fill_breakat_flags();
2819#endif
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002820 else if ( varp == &p_titlestring // 'titlestring'
2821 || varp == &p_iconstring) // 'iconstring'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002822 did_set_titleiconstring(varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02002823#ifdef FEAT_GUI
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002824 else if (varp == &p_go) // 'guioptions'
Bram Moolenaardac13472019-09-16 21:06:21 +02002825 {
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002826 did_set_guioptions(oldval);
2827 redraw_gui_only = TRUE;
Bram Moolenaardac13472019-09-16 21:06:21 +02002828 }
2829#endif
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002830#if defined(FEAT_GUI_TABLINE)
2831 else if (varp == &p_gtl) // 'guitablabel'
2832 {
2833 did_set_guitablabel();
2834 redraw_gui_only = TRUE;
2835 }
2836 else if (varp == &p_gtt) // 'guitabtooltip'
2837 redraw_gui_only = TRUE;
2838#endif
2839#if defined(UNIX) || defined(VMS)
2840 else if (varp == &p_ttym) // 'ttymouse'
2841 errmsg = did_set_ttymouse();
2842#endif
2843 else if (varp == &p_sel) // 'selection'
2844 errmsg = did_set_selection();
2845 else if (varp == &p_slm) // 'selectmode'
2846 errmsg = did_set_opt_strings(p_slm, p_slm_values, TRUE);
2847#ifdef FEAT_BROWSE
2848 else if (varp == &p_bsdir) // 'browsedir'
2849 errmsg = did_set_browsedir();
2850#endif
2851 else if (varp == &p_km) // 'keymodel'
2852 errmsg = did_set_keymodel();
2853 else if (varp == &p_kpc) // 'keyprotocol'
2854 errmsg = did_set_keyprotocol();
2855 else if (varp == &p_mousem) // 'mousemodel'
2856 errmsg = did_set_mousemodel();
2857 else if (varp == &p_swb) // 'switchbuf'
2858 errmsg = did_set_opt_flags(p_swb, p_swb_values, &swb_flags, TRUE);
2859 else if (varp == &p_spk) // 'splitkeep'
2860 errmsg = did_set_opt_strings(p_spk, p_spk_values, FALSE);
2861 else if (varp == &p_debug) // 'debug'
2862 errmsg = did_set_opt_strings(p_debug, p_debug_values, TRUE);
2863 else if (varp == &p_dy) // 'display'
2864 errmsg = did_set_display();
2865 else if (varp == &p_ead) // 'eadirection'
2866 errmsg = did_set_opt_strings(p_ead, p_ead_values, FALSE);
2867#ifdef FEAT_CLIPBOARD
2868 else if (varp == &p_cb) // 'clipboard'
2869 errmsg = check_clipboard_option();
2870#endif
2871#ifdef FEAT_SPELL
2872 else if (varp == &(curwin->w_s->b_p_spf)) // 'spellfile'
2873 errmsg = did_set_spellfile(varp);
2874 else if (varp == &(curwin->w_s->b_p_spl)) // 'spell'
2875 errmsg = did_set_spell(varp);
2876 else if (varp == &(curwin->w_s->b_p_spc)) // 'spellcapcheck'
2877 errmsg = did_set_spellcapcheck();
2878 else if (varp == &(curwin->w_s->b_p_spo)) // 'spelloptions'
2879 errmsg = did_set_spelloptions(varp);
2880 else if (varp == &p_sps) // 'spellsuggest'
2881 errmsg = did_set_spellsuggest();
2882 else if (varp == &p_msm) // 'mkspellmem'
2883 errmsg = did_set_mkspellmem();
2884#endif
2885 else if (gvarp == &p_bh) // 'bufhidden'
2886 errmsg = did_set_opt_strings(curbuf->b_p_bh, p_bufhidden_values,
2887 FALSE);
2888 else if (gvarp == &p_bt) // 'buftype'
2889 errmsg = did_set_buftype();
2890#ifdef FEAT_STL_OPT
2891 else if ( gvarp == &p_stl // 'statusline'
2892 || varp == &p_tal // 'tabline'
2893 || varp == &p_ruf) // 'rulerformat'
2894 errmsg = did_set_statusline(varp);
2895#endif
2896 else if (gvarp == &p_cpt) // 'complete'
2897 errmsg = did_set_complete(varp, errbuf);
2898 else if (varp == &p_cot) // 'completeopt'
2899 errmsg = did_set_completeopt();
2900#ifdef BACKSLASH_IN_FILENAME
2901 else if (gvarp == &p_csl) // 'completeslash'
2902 errmsg = did_set_completeslash();
2903#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002904#ifdef FEAT_SIGNS
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002905 else if (varp == &curwin->w_p_scl) // 'signcolumn'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002906 errmsg = did_set_signcolumn(varp, oldval);
Bram Moolenaardac13472019-09-16 21:06:21 +02002907#endif
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002908 else if (varp == &p_sloc) // 'showcmdloc'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002909 errmsg = did_set_opt_strings(p_sloc, p_sloc_values, FALSE);
Bram Moolenaardac13472019-09-16 21:06:21 +02002910#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002911 else if (varp == &p_toolbar) // 'toolbar'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002912 errmsg = did_set_toolbar();
Bram Moolenaardac13472019-09-16 21:06:21 +02002913#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002914#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002915 else if (varp == &p_tbis) // 'toolbariconsize'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002916 errmsg = did_set_toolbariconsize();
Bram Moolenaardac13472019-09-16 21:06:21 +02002917#endif
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002918 else if (varp == &p_pt) // 'pastetoggle'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002919 did_set_pastetoggle();
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002920 else if (varp == &p_bs) // 'backspace'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002921 errmsg = did_set_backspace();
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002922 else if (varp == &p_bo) // 'belloff'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002923 errmsg = did_set_opt_flags(p_bo, p_bo_values, &bo_flags, TRUE);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002924 else if (gvarp == &p_tc) // 'tagcase'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002925 errmsg = did_set_tagcase(opt_flags);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002926 else if (varp == &p_cmp) // 'casemap'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002927 errmsg = did_set_opt_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +02002928#ifdef FEAT_DIFF
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002929 else if (varp == &p_dip) // 'diffopt'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002930 errmsg = did_set_diffopt();
Bram Moolenaardac13472019-09-16 21:06:21 +02002931#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002932#ifdef FEAT_FOLDING
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002933 else if (gvarp == &curwin->w_allbuf_opt.wo_fdm) // 'foldmethod'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002934 errmsg = did_set_foldmethod(varp);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002935 else if (gvarp == &curwin->w_allbuf_opt.wo_fmr) // 'foldmarker'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002936 errmsg = did_set_foldmarker(varp);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002937 else if (gvarp == &p_cms) // 'commentstring'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002938 errmsg = did_set_commentstring(varp);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002939 else if (varp == &p_fdo) // 'foldopen'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002940 errmsg = did_set_opt_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002941 else if (varp == &p_fcl) // 'foldclose'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002942 errmsg = did_set_opt_strings(p_fcl, p_fcl_values, TRUE);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002943 else if (gvarp == &curwin->w_allbuf_opt.wo_fdi) // 'foldignore'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002944 did_set_foldignore();
Bram Moolenaardac13472019-09-16 21:06:21 +02002945#endif
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002946 else if (gvarp == &p_ve) // 'virtualedit'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002947 errmsg = did_set_virtualedit(oldval, opt_flags);
Bram Moolenaardac13472019-09-16 21:06:21 +02002948#if defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002949 else if (varp == &p_csqf) // 'cscopequickfix'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002950 errmsg = did_set_cscopequickfix();
Bram Moolenaardac13472019-09-16 21:06:21 +02002951#endif
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002952 else if (gvarp == &p_cino) // 'cinoptions'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002953 did_set_cinoptions();
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002954 else if (gvarp == &p_lop) // 'lispoptions'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002955 errmsg = did_set_lispoptions(varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02002956#if defined(FEAT_RENDER_OPTIONS)
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002957 else if (varp == &p_rop) // 'renderoptions'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002958 errmsg = did_set_renderoptions();
Bram Moolenaardac13472019-09-16 21:06:21 +02002959#endif
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002960 else if (gvarp == &p_ft) // 'filetype'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002961 errmsg = did_set_filetype_or_syntax(varp, oldval, value_checked,
2962 &value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02002963#ifdef FEAT_SYN_HL
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002964 else if (gvarp == &p_syn) // 'syntax'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002965 errmsg = did_set_filetype_or_syntax(varp, oldval, value_checked,
2966 &value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02002967#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002968#ifdef FEAT_TERMINAL
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002969 else if (varp == &curwin->w_p_twk) // 'termwinkey'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002970 errmsg = did_set_termwinkey();
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002971 else if (varp == &curwin->w_p_tws) // 'termwinsize'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002972 errmsg = did_set_termwinsize();
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002973 else if (varp == &curwin->w_p_wcr) // 'wincolor'
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002974 term_update_wincolor(curwin);
Bram Moolenaardac13472019-09-16 21:06:21 +02002975# if defined(MSWIN)
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002976 else if (varp == &p_twt) // 'termwintype'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002977 errmsg = did_set_opt_strings(p_twt, p_twt_values, FALSE);
Bram Moolenaardac13472019-09-16 21:06:21 +02002978# endif
2979#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002980#ifdef FEAT_VARTABS
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002981 else if (varp == &(curbuf->b_p_vsts)) // 'varsofttabstop'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002982 errmsg = did_set_varsofttabstop(varp);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002983 else if (varp == &(curbuf->b_p_vts)) // 'vartabstop'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002984 errmsg = did_set_vartabstop(varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02002985#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002986#ifdef FEAT_PROP_POPUP
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002987 else if (varp == &p_pvp) // 'previewpopup'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002988 errmsg = did_set_previewpopup();
Bram Moolenaardac13472019-09-16 21:06:21 +02002989# ifdef FEAT_QUICKFIX
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002990 else if (varp == &p_cpp) // 'completepopup'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002991 errmsg = did_set_completepopup();
Bram Moolenaardac13472019-09-16 21:06:21 +02002992# endif
2993#endif
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002994#ifdef FEAT_EVAL
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002995 else if (
2996# ifdef FEAT_BEVAL
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00002997 varp == &p_bexpr || // 'balloonexpr'
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00002998# endif
2999# ifdef FEAT_DIFF
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00003000 varp == &p_dex || // 'diffexpr'
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00003001# endif
3002# ifdef FEAT_FOLDING
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00003003 gvarp == &curwin->w_allbuf_opt.wo_fde || // 'foldexpr'
3004 gvarp == &curwin->w_allbuf_opt.wo_fdt || // 'foldtext'
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00003005# endif
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00003006 gvarp == &p_fex || // 'formatexpr'
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00003007# ifdef FEAT_FIND_ID
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00003008 gvarp == &p_inex || // 'includeexpr'
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00003009# endif
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00003010 gvarp == &p_inde || // 'indentexpr'
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00003011# ifdef FEAT_DIFF
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00003012 varp == &p_pex || // 'patchexpr'
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00003013# endif
3014# ifdef FEAT_POSTSCRIPT
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00003015 varp == &p_pexpr || // 'printexpr'
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00003016# endif
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00003017 varp == &p_ccv) // 'charconvert'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003018 did_set_optexpr(varp);
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00003019#endif
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003020#ifdef FEAT_COMPL_FUNC
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00003021 else if (gvarp == &p_cfu) // 'completefunc'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003022 errmsg = set_completefunc_option();
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00003023 else if (gvarp == &p_ofu) // 'omnifunc'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003024 errmsg = set_omnifunc_option();
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00003025 else if (gvarp == &p_tsrfu) // 'thesaurusfunc'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003026 errmsg = set_thesaurusfunc_option();
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003027#endif
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00003028#if defined(FEAT_EVAL) && \
3029 (defined(FEAT_XIM) || defined(IME_WITHOUT_XIM) || defined(VIMDLL))
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00003030 else if (gvarp == &p_imaf) // 'imactivatefunc'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003031 errmsg = set_imactivatefunc_option();
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00003032 else if (gvarp == &p_imsf) // 'imstatusfunc'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003033 errmsg = set_imstatusfunc_option();
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00003034#endif
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00003035 else if (varp == &p_opfunc) // 'operatorfunc'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003036 errmsg = set_operatorfunc_option();
Bram Moolenaard43906d2020-07-20 21:31:32 +02003037#ifdef FEAT_QUICKFIX
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00003038 else if (varp == &p_qftf) // 'quickfixtextfunc'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003039 errmsg = qf_process_qftf_option();
Bram Moolenaard43906d2020-07-20 21:31:32 +02003040#endif
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00003041#ifdef FEAT_EVAL
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00003042 else if (gvarp == &p_tfu) // 'tagfunc'
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003043 errmsg = set_tagfunc_option();
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00003044#endif
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00003045 else if (varp == &p_ww) // 'whichwrap'
3046 errmsg = did_set_option_listflag(varp, (char_u *)WW_ALL, errbuf);
3047 else if (varp == &p_shm) // 'shortmess'
3048 errmsg = did_set_option_listflag(varp, (char_u *)SHM_ALL, errbuf);
3049 else if (varp == &(p_cpo)) // 'cpoptions'
3050 errmsg = did_set_option_listflag(varp, (char_u *)CPO_ALL, errbuf);
3051 else if (varp == &(curbuf->b_p_fo)) // 'formatoptions'
3052 errmsg = did_set_option_listflag(varp, (char_u *)FO_ALL, errbuf);
Bram Moolenaardac13472019-09-16 21:06:21 +02003053#ifdef FEAT_CONCEAL
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00003054 else if (varp == &curwin->w_p_cocu) // 'concealcursor'
3055 errmsg = did_set_option_listflag(varp, (char_u *)COCU_ALL, errbuf);
Bram Moolenaardac13472019-09-16 21:06:21 +02003056#endif
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00003057 else if (varp == &p_mouse) // 'mouse'
3058 errmsg = did_set_option_listflag(varp, (char_u *)MOUSE_ALL, errbuf);
Bram Moolenaardac13472019-09-16 21:06:21 +02003059#if defined(FEAT_GUI)
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00003060 else if (varp == &p_go) // 'guioptions'
3061 errmsg = did_set_option_listflag(varp, (char_u *)GO_ALL, errbuf);
Bram Moolenaardac13472019-09-16 21:06:21 +02003062#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02003063
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00003064 // If an error is detected, restore the previous value.
Bram Moolenaardac13472019-09-16 21:06:21 +02003065 if (errmsg != NULL)
3066 {
zeertzjqf6782732022-07-27 18:26:03 +01003067 free_string_option(*varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02003068 *varp = oldval;
3069 // When resetting some values, need to act on it.
3070 if (did_chartab)
3071 (void)init_chartab();
3072 if (varp == &p_hl)
3073 (void)highlight_changed();
3074 }
3075 else
3076 {
3077#ifdef FEAT_EVAL
3078 // Remember where the option was set.
3079 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
3080#endif
3081 // Free string options that are in allocated memory.
3082 // Use "free_oldval", because recursiveness may change the flags under
3083 // our fingers (esp. init_highlight()).
3084 if (free_oldval)
3085 free_string_option(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01003086 set_option_flag(opt_idx, P_ALLOCED);
Bram Moolenaardac13472019-09-16 21:06:21 +02003087
3088 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
3089 && is_global_local_option(opt_idx))
3090 {
3091 // global option with local value set to use global value; free
3092 // the local value and make it empty
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00003093 char_u *p = get_option_varp_scope(opt_idx, OPT_LOCAL);
Bram Moolenaardac13472019-09-16 21:06:21 +02003094 free_string_option(*(char_u **)p);
3095 *(char_u **)p = empty_option;
3096 }
3097
3098 // May set global value for local option.
3099 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
3100 set_string_option_global(opt_idx, varp);
3101
3102 // Trigger the autocommand only after setting the flags.
3103#ifdef FEAT_SYN_HL
Bram Moolenaardac13472019-09-16 21:06:21 +02003104 if (varp == &(curbuf->b_p_syn))
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00003105 do_syntax_autocmd(value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02003106#endif
3107 else if (varp == &(curbuf->b_p_ft))
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00003108 do_filetype_autocmd(varp, opt_flags, value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02003109#ifdef FEAT_SPELL
3110 if (varp == &(curwin->w_s->b_p_spl))
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00003111 do_spelllang_source();
Bram Moolenaardac13472019-09-16 21:06:21 +02003112#endif
3113 }
3114
Bram Moolenaardac13472019-09-16 21:06:21 +02003115 if (varp == &p_mouse)
3116 {
Bram Moolenaardac13472019-09-16 21:06:21 +02003117 if (*p_mouse == NUL)
3118 mch_setmouse(FALSE); // switch mouse off
3119 else
Bram Moolenaardac13472019-09-16 21:06:21 +02003120 setmouse(); // in case 'mouse' changed
3121 }
Bram Moolenaardac13472019-09-16 21:06:21 +02003122
Bram Moolenaar788fbb42020-05-31 14:08:12 +02003123#if defined(FEAT_LUA) || defined(PROTO)
3124 if (varp == &p_rtp)
3125 update_package_paths_in_lua();
3126#endif
3127
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00003128#if defined(FEAT_LINEBREAK)
3129 // Changing Formatlistpattern when briopt includes the list setting:
3130 // redraw
3131 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
3132 && curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003133 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00003134#endif
3135
Bram Moolenaardac13472019-09-16 21:06:21 +02003136 if (curwin->w_curswant != MAXCOL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003137 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02003138 curwin->w_set_curswant = TRUE;
3139
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003140 if ((opt_flags & OPT_NO_REDRAW) == 0)
3141 {
Bram Moolenaardac13472019-09-16 21:06:21 +02003142#ifdef FEAT_GUI
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003143 // check redraw when it's not a GUI option or the GUI is active.
3144 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02003145#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003146 check_redraw(get_option_flags(opt_idx));
3147 }
Bram Moolenaardac13472019-09-16 21:06:21 +02003148
3149#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3150 if (did_swaptcap)
3151 {
3152 set_termname((char_u *)"win32");
3153 init_highlight(TRUE, FALSE);
3154 }
3155#endif
3156
3157 return errmsg;
3158}
3159
3160/*
3161 * Check an option that can be a range of string values.
3162 *
3163 * Return OK for correct value, FAIL otherwise.
3164 * Empty is always OK.
3165 */
3166 static int
3167check_opt_strings(
3168 char_u *val,
3169 char **values,
3170 int list) // when TRUE: accept a list of values
3171{
3172 return opt_strings_flags(val, values, NULL, list);
3173}
3174
3175/*
3176 * Handle an option that can be a range of string values.
3177 * Set a flag in "*flagp" for each string present.
3178 *
3179 * Return OK for correct value, FAIL otherwise.
3180 * Empty is always OK.
3181 */
3182 static int
3183opt_strings_flags(
3184 char_u *val, // new value
3185 char **values, // array of valid string values
3186 unsigned *flagp,
3187 int list) // when TRUE: accept a list of values
3188{
3189 int i;
3190 int len;
3191 unsigned new_flags = 0;
3192
3193 while (*val)
3194 {
3195 for (i = 0; ; ++i)
3196 {
3197 if (values[i] == NULL) // val not found in values[]
3198 return FAIL;
3199
3200 len = (int)STRLEN(values[i]);
3201 if (STRNCMP(values[i], val, len) == 0
3202 && ((list && val[len] == ',') || val[len] == NUL))
3203 {
3204 val += len + (val[len] == ',');
3205 new_flags |= (1 << i);
3206 break; // check next item in val list
3207 }
3208 }
3209 }
3210 if (flagp != NULL)
3211 *flagp = new_flags;
3212
3213 return OK;
3214}
3215
3216/*
3217 * return OK if "p" is a valid fileformat name, FAIL otherwise.
3218 */
3219 int
3220check_ff_value(char_u *p)
3221{
3222 return check_opt_strings(p, p_ff_values, FALSE);
3223}
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00003224
3225/*
3226 * Save the acutal shortmess Flags and clear them
3227 * temporarily to avoid that file messages
3228 * overwrites any output from the following commands.
3229 *
3230 * Caller must make sure to first call save_clear_shm_value() and then
3231 * restore_shm_value() exactly the same number of times.
3232 */
3233 void
3234save_clear_shm_value()
3235{
3236 if (STRLEN(p_shm) >= SHM_LEN)
3237 {
3238 iemsg(e_internal_error_shortmess_too_long);
3239 return;
3240 }
3241
3242 if (++set_shm_recursive == 1)
3243 {
3244 STRCPY(shm_buf, p_shm);
3245 set_option_value_give_err((char_u *)"shm", 0L, (char_u *)"", 0);
3246 }
3247}
3248
3249/*
3250 * Restore the shortmess Flags set from the save_clear_shm_value() function.
3251 */
3252 void
3253restore_shm_value()
3254{
3255 if (--set_shm_recursive == 0)
3256 {
3257 set_option_value_give_err((char_u *)"shm", 0L, shm_buf, 0);
3258 vim_memset(shm_buf, 0, SHM_LEN);
3259 }
3260}