blob: 1665509df933829a9b48a744bc5b1873c31ed76b [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,
490 int opt_flags) // OPT_LOCAL and/or OPT_GLOBAL
491{
492 char_u *s;
493 char_u **varp;
494 char_u *oldval;
495#if defined(FEAT_EVAL)
496 char_u *oldval_l = NULL;
497 char_u *oldval_g = NULL;
498 char_u *saved_oldval = NULL;
499 char_u *saved_oldval_l = NULL;
500 char_u *saved_oldval_g = NULL;
501 char_u *saved_newval = NULL;
502#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100503 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +0200504 int value_checked = FALSE;
505
506 if (is_hidden_option(opt_idx)) // don't set hidden option
507 return NULL;
508
Bram Moolenaar7f009df2020-03-16 20:27:38 +0100509 s = vim_strsave(value == NULL ? (char_u *)"" : value);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000510 if (s == NULL)
511 return NULL;
512
513 varp = (char_u **)get_option_varp_scope(opt_idx,
514 (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
515 ? (is_global_local_option(opt_idx)
516 ? OPT_GLOBAL : OPT_LOCAL)
517 : opt_flags);
518 oldval = *varp;
519#if defined(FEAT_EVAL)
520 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +0200521 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000522 oldval_l = *(char_u **)get_option_varp_scope(opt_idx, OPT_LOCAL);
523 oldval_g = *(char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
Bram Moolenaardac13472019-09-16 21:06:21 +0200524 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000525#endif
526 *varp = s;
527
528#if defined(FEAT_EVAL)
529 if (!starting
530# ifdef FEAT_CRYPT
531 && !is_crypt_key_option(opt_idx)
532# endif
533 )
534 {
535 if (oldval_l != NULL)
536 saved_oldval_l = vim_strsave(oldval_l);
537 if (oldval_g != NULL)
538 saved_oldval_g = vim_strsave(oldval_g);
539 saved_oldval = vim_strsave(oldval);
540 saved_newval = vim_strsave(s);
541 }
542#endif
543 if ((errmsg = did_set_string_option(opt_idx, varp, oldval, NULL,
544 opt_flags, &value_checked)) == NULL)
545 did_set_option(opt_idx, opt_flags, TRUE, value_checked);
546
547#if defined(FEAT_EVAL)
548 // call autocommand after handling side effects
549 if (errmsg == NULL)
550 trigger_optionset_string(opt_idx, opt_flags,
551 saved_oldval, saved_oldval_l,
552 saved_oldval_g, saved_newval);
553 vim_free(saved_oldval);
554 vim_free(saved_oldval_l);
555 vim_free(saved_oldval_g);
556 vim_free(saved_newval);
557#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100558 return errmsg;
Bram Moolenaardac13472019-09-16 21:06:21 +0200559}
560
561/*
562 * Return TRUE if "val" is a valid 'filetype' name.
563 * Also used for 'syntax' and 'keymap'.
564 */
565 static int
566valid_filetype(char_u *val)
567{
568 return valid_name(val, ".-_");
569}
570
571#ifdef FEAT_STL_OPT
572/*
573 * Check validity of options with the 'statusline' format.
zeertzjq5dc294a2022-04-15 13:17:57 +0100574 * Return an untranslated error message or NULL.
Bram Moolenaardac13472019-09-16 21:06:21 +0200575 */
576 static char *
577check_stl_option(char_u *s)
578{
Bram Moolenaardac13472019-09-16 21:06:21 +0200579 int groupdepth = 0;
580 static char errbuf[80];
581
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100582 while (*s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200583 {
584 // Check for valid keys after % sequences
585 while (*s && *s != '%')
586 s++;
587 if (!*s)
588 break;
589 s++;
Bram Moolenaardac13472019-09-16 21:06:21 +0200590 if (*s == '%' || *s == STL_TRUNCMARK || *s == STL_MIDDLEMARK)
591 {
592 s++;
593 continue;
594 }
595 if (*s == ')')
596 {
597 s++;
598 if (--groupdepth < 0)
599 break;
600 continue;
601 }
602 if (*s == '-')
603 s++;
604 while (VIM_ISDIGIT(*s))
605 s++;
606 if (*s == STL_USER_HL)
607 continue;
608 if (*s == '.')
609 {
610 s++;
611 while (*s && VIM_ISDIGIT(*s))
612 s++;
613 }
614 if (*s == '(')
615 {
616 groupdepth++;
617 continue;
618 }
619 if (vim_strchr(STL_ALL, *s) == NULL)
620 {
621 return illegal_char(errbuf, *s);
622 }
623 if (*s == '{')
624 {
zeertzjq5dc294a2022-04-15 13:17:57 +0100625 int reevaluate = (*++s == '%');
shadmansaleh30e3de22021-05-15 17:23:28 +0200626
zeertzjq5dc294a2022-04-15 13:17:57 +0100627 if (reevaluate && *++s == '}')
628 // "}" is not allowed immediately after "%{%"
629 return illegal_char(errbuf, '}');
shadmansaleh30e3de22021-05-15 17:23:28 +0200630 while ((*s != '}' || (reevaluate && s[-1] != '%')) && *s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200631 s++;
632 if (*s != '}')
zeertzjq5dc294a2022-04-15 13:17:57 +0100633 return e_unclosed_expression_sequence;
Bram Moolenaardac13472019-09-16 21:06:21 +0200634 }
635 }
Bram Moolenaardac13472019-09-16 21:06:21 +0200636 if (groupdepth != 0)
zeertzjq5dc294a2022-04-15 13:17:57 +0100637 return e_unbalanced_groups;
Bram Moolenaardac13472019-09-16 21:06:21 +0200638 return NULL;
639}
640#endif
641
642/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000643 * The 'term' option is changed.
644 */
645 static char *
646did_set_term(int *opt_idx, long_u *free_oldval)
647{
648 char *errmsg = NULL;
649
650 if (T_NAME[0] == NUL)
651 errmsg = e_cannot_set_term_to_empty_string;
652#ifdef FEAT_GUI
653 else if (gui.in_use)
654 errmsg = e_cannot_change_term_in_GUI;
655 else if (term_is_gui(T_NAME))
656 errmsg = e_use_gui_to_start_GUI;
657#endif
658 else if (set_termname(T_NAME) == FAIL)
659 errmsg = e_not_found_in_termcap;
660 else
661 {
662 // Screen colors may have changed.
663 redraw_later_clear();
664
665 // Both 'term' and 'ttytype' point to T_NAME, only set the
666 // P_ALLOCED flag on 'term'.
667 *opt_idx = findoption((char_u *)"term");
668 *free_oldval = (get_option_flags(*opt_idx) & P_ALLOCED);
669 }
670
671 return errmsg;
672}
673
674/*
675 * The 'backupcopy' option is changed.
676 */
677 static char *
678did_set_backupcopy(
679 char_u *oldval,
680 int opt_flags)
681{
682 char_u *bkc = p_bkc;
683 unsigned int *flags = &bkc_flags;
684 char *errmsg = NULL;
685
686 if (opt_flags & OPT_LOCAL)
687 {
688 bkc = curbuf->b_p_bkc;
689 flags = &curbuf->b_bkc_flags;
690 }
691
692 if ((opt_flags & OPT_LOCAL) && *bkc == NUL)
693 // make the local value empty: use the global value
694 *flags = 0;
695 else
696 {
697 if (opt_strings_flags(bkc, p_bkc_values, flags, TRUE) != OK)
698 errmsg = e_invalid_argument;
699 if ((((int)*flags & BKC_AUTO) != 0)
700 + (((int)*flags & BKC_YES) != 0)
701 + (((int)*flags & BKC_NO) != 0) != 1)
702 {
703 // Must have exactly one of "auto", "yes" and "no".
704 (void)opt_strings_flags(oldval, p_bkc_values, flags, TRUE);
705 errmsg = e_invalid_argument;
706 }
707 }
708
709 return errmsg;
710}
711
712/*
713 * The 'backupext' or the 'patchmode' option is changed.
714 */
715 static char *
716did_set_backupext_or_patchmode(void)
717{
718 if (STRCMP(*p_bex == '.' ? p_bex + 1 : p_bex,
719 *p_pm == '.' ? p_pm + 1 : p_pm) == 0)
720 return e_backupext_and_patchmode_are_equal;
721
722 return NULL;
723}
724
725#ifdef FEAT_LINEBREAK
726/*
727 * The 'breakindentopt' option is changed.
728 */
729 static char *
730did_set_breakindentopt(void)
731{
732 char *errmsg = NULL;
733
734 if (briopt_check(curwin) == FAIL)
735 errmsg = e_invalid_argument;
736 // list setting requires a redraw
737 if (curwin->w_briopt_list)
738 redraw_all_later(UPD_NOT_VALID);
739
740 return errmsg;
741}
742#endif
743
744/*
745 * The 'isident' or the 'iskeyword' or the 'isprint' or the 'isfname' option is
746 * changed.
747 */
748 static char *
749did_set_isopt(int *did_chartab)
750{
751 if (init_chartab() == FAIL)
752 {
753 *did_chartab = TRUE; // need to restore it below
754 return e_invalid_argument; // error in value
755 }
756
757 return NULL;
758}
759
760/*
761 * The 'helpfile' option is changed.
762 */
763 static void
764did_set_helpfile(void)
765{
766 // May compute new values for $VIM and $VIMRUNTIME
767 if (didset_vim)
768 vim_unsetenv_ext((char_u *)"VIM");
769 if (didset_vimruntime)
770 vim_unsetenv_ext((char_u *)"VIMRUNTIME");
771}
772
773#ifdef FEAT_SYN_HL
774/*
775 * The 'cursorlineopt' option is changed.
776 */
777 static char *
778did_set_cursorlineopt(char_u **varp)
779{
780 if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
781 return e_invalid_argument;
782
783 return NULL;
784}
785#endif
786
787#ifdef FEAT_MULTI_LANG
788/*
789 * The 'helplang' option is changed.
790 */
791 static char *
792did_set_helplang(void)
793{
794 char *errmsg = NULL;
795
796 // Check for "", "ab", "ab,cd", etc.
797 for (char_u *s = p_hlg; *s != NUL; s += 3)
798 {
799 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
800 {
801 errmsg = e_invalid_argument;
802 break;
803 }
804 if (s[2] == NUL)
805 break;
806 }
807
808 return errmsg;
809}
810#endif
811
812/*
813 * The 'highlight' option is changed.
814 */
815 static char *
816did_set_highlight(void)
817{
818 if (highlight_changed() == FAIL)
819 return e_invalid_argument; // invalid flags
820
821 return NULL;
822}
823
824/*
825 * An option that accepts a list of flags is changed.
826 * e.g. 'viewoptions', 'switchbuf', 'casemap', etc.
827 */
828 static char *
829did_set_opt_flags(char_u *val, char **values, unsigned *flagp, int list)
830{
831 if (opt_strings_flags(val, values, flagp, list) == FAIL)
832 return e_invalid_argument;
833
834 return NULL;
835}
836
837/*
838 * An option that accepts a list of string values is changed.
839 * e.g. 'nrformats', 'scrollopt', 'wildoptions', etc.
840 */
841 static char *
842did_set_opt_strings(char_u *val, char **values, int list)
843{
844 return did_set_opt_flags(val, values, NULL, list);
845}
846
847#ifdef FEAT_SESSION
848/*
849 * The 'sessionoptions' option is changed.
850 */
851 static char *
852did_set_sessionoptions(char_u *oldval)
853{
854 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
855 return e_invalid_argument;
856 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
857 {
858 // Don't allow both "sesdir" and "curdir".
859 (void)opt_strings_flags(oldval, p_ssop_values, &ssop_flags, TRUE);
860 return e_invalid_argument;
861 }
862
863 return NULL;
864}
865#endif
866
867/*
868 * The 'ambiwidth' option is changed.
869 */
870 static char *
871did_set_ambiwidth(void)
872{
873 if (check_opt_strings(p_ambw, p_ambw_values, FALSE) != OK)
874 return e_invalid_argument;
875
876 return check_chars_options();
877}
878
879/*
880 * The 'background' option is changed.
881 */
882 static char *
883did_set_background(void)
884{
885 if (check_opt_strings(p_bg, p_bg_values, FALSE) == FAIL)
886 return e_invalid_argument;
887
888#ifdef FEAT_EVAL
889 int dark = (*p_bg == 'd');
890#endif
891
892 init_highlight(FALSE, FALSE);
893
894#ifdef FEAT_EVAL
895 if (dark != (*p_bg == 'd')
896 && get_var_value((char_u *)"g:colors_name") != NULL)
897 {
898 // The color scheme must have set 'background' back to another
899 // value, that's not what we want here. Disable the color
900 // scheme and set the colors again.
901 do_unlet((char_u *)"g:colors_name", TRUE);
902 free_string_option(p_bg);
903 p_bg = vim_strsave((char_u *)(dark ? "dark" : "light"));
904 check_string_option(&p_bg);
905 init_highlight(FALSE, FALSE);
906 }
907#endif
908#ifdef FEAT_TERMINAL
909 term_update_colors_all();
910#endif
911
912 return NULL;
913}
914
915/*
916 * The 'wildmode' option is changed.
917 */
918 static char *
919did_set_wildmode(void)
920{
921 if (check_opt_wim() == FAIL)
922 return e_invalid_argument;
923 return NULL;
924}
925
926#ifdef FEAT_WAK
927/*
928 * The 'winaltkeys' option is changed.
929 */
930 static char *
931did_set_winaltkeys(void)
932{
933 char *errmsg = NULL;
934
935 if (*p_wak == NUL
936 || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
937 errmsg = e_invalid_argument;
938# ifdef FEAT_MENU
939# if defined(FEAT_GUI_MOTIF)
940 else if (gui.in_use)
941 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
942# elif defined(FEAT_GUI_GTK)
943 else if (gui.in_use)
944 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
945# endif
946# endif
947 return errmsg;
948}
949#endif
950
951/*
952 * The 'eventignore' option is changed.
953 */
954 static char *
955did_set_eventignore(void)
956{
957 if (check_ei() == FAIL)
958 return e_invalid_argument;
959 return NULL;
960}
961
962/*
963 * One of the 'encoding', 'fileencoding', 'termencoding' or 'makeencoding'
964 * options is changed.
965 */
966 static char *
967did_set_encoding(char_u **varp, char_u **gvarp, int opt_flags)
968{
969 char *errmsg = NULL;
970 char_u *p;
971
972 if (gvarp == &p_fenc)
973 {
974 if (!curbuf->b_p_ma && opt_flags != OPT_GLOBAL)
975 errmsg = e_cannot_make_changes_modifiable_is_off;
976 else if (vim_strchr(*varp, ',') != NULL)
977 // No comma allowed in 'fileencoding'; catches confusing it
978 // with 'fileencodings'.
979 errmsg = e_invalid_argument;
980 else
981 {
982 // May show a "+" in the title now.
983 redraw_titles();
984 // Add 'fileencoding' to the swap file.
985 ml_setflags(curbuf);
986 }
987 }
988 if (errmsg == NULL)
989 {
990 // canonize the value, so that STRCMP() can be used on it
991 p = enc_canonize(*varp);
992 if (p != NULL)
993 {
994 vim_free(*varp);
995 *varp = p;
996 }
997 if (varp == &p_enc)
998 {
999 errmsg = mb_init();
1000 redraw_titles();
1001 }
1002 }
1003
1004#if defined(FEAT_GUI_GTK)
1005 if (errmsg == NULL && varp == &p_tenc && gui.in_use)
1006 {
1007 // GTK uses only a single encoding, and that is UTF-8.
1008 if (STRCMP(p_tenc, "utf-8") != 0)
1009 errmsg = e_cannot_be_changed_in_gtk_GUI;
1010 }
1011#endif
1012
1013 if (errmsg == NULL)
1014 {
1015#ifdef FEAT_KEYMAP
1016 // When 'keymap' is used and 'encoding' changes, reload the keymap
1017 // (with another encoding).
1018 if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
1019 (void)keymap_init();
1020#endif
1021
1022 // When 'termencoding' is not empty and 'encoding' changes or when
1023 // 'termencoding' changes, need to setup for keyboard input and
1024 // display output conversion.
1025 if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
1026 {
1027 if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
1028 || convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
1029 {
1030 semsg(_(e_cannot_convert_between_str_and_str),
1031 p_tenc, p_enc);
1032 errmsg = e_invalid_argument;
1033 }
1034 }
1035
1036#if defined(MSWIN)
1037 // $HOME may have characters in active code page.
1038 if (varp == &p_enc)
1039 init_homedir();
1040#endif
1041 }
1042
1043 return errmsg;
1044}
1045
1046#if defined(FEAT_POSTSCRIPT)
1047/*
1048 * The 'printencoding' option is changed.
1049 */
1050 static void
1051did_set_printencoding(void)
1052{
1053 char_u *s, *p;
1054
1055 // Canonize printencoding if VIM standard one
1056 p = enc_canonize(p_penc);
1057 if (p != NULL)
1058 {
1059 vim_free(p_penc);
1060 p_penc = p;
1061 }
1062 else
1063 {
1064 // Ensure lower case and '-' for '_'
1065 for (s = p_penc; *s != NUL; s++)
1066 {
1067 if (*s == '_')
1068 *s = '-';
1069 else
1070 *s = TOLOWER_ASC(*s);
1071 }
1072 }
1073}
1074#endif
1075
1076#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
1077/*
1078 * The 'imactivatekey' option is changed.
1079 */
1080 static char *
1081did_set_imactivatekey(void)
1082{
1083 if (!im_xim_isvalid_imactivate())
1084 return e_invalid_argument;
1085 return NULL;
1086}
1087#endif
1088
1089#ifdef FEAT_KEYMAP
1090/*
1091 * The 'keymap' option is changed.
1092 */
1093 static char *
1094did_set_keymap(char_u **varp, int opt_flags, int *value_checked)
1095{
1096 char *errmsg = NULL;
1097
1098 if (!valid_filetype(*varp))
1099 errmsg = e_invalid_argument;
1100 else
1101 {
1102 int secure_save = secure;
1103
1104 // Reset the secure flag, since the value of 'keymap' has
1105 // been checked to be safe.
1106 secure = 0;
1107
1108 // load or unload key mapping tables
1109 errmsg = keymap_init();
1110
1111 secure = secure_save;
1112
1113 // Since we check the value, there is no need to set P_INSECURE,
1114 // even when the value comes from a modeline.
1115 *value_checked = TRUE;
1116 }
1117
1118 if (errmsg == NULL)
1119 {
1120 if (*curbuf->b_p_keymap != NUL)
1121 {
1122 // Installed a new keymap, switch on using it.
1123 curbuf->b_p_iminsert = B_IMODE_LMAP;
1124 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
1125 curbuf->b_p_imsearch = B_IMODE_LMAP;
1126 }
1127 else
1128 {
1129 // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
1130 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
1131 curbuf->b_p_iminsert = B_IMODE_NONE;
1132 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
1133 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
1134 }
1135 if ((opt_flags & OPT_LOCAL) == 0)
1136 {
1137 set_iminsert_global();
1138 set_imsearch_global();
1139 }
1140 status_redraw_curbuf();
1141 }
1142
1143 return errmsg;
1144}
1145#endif
1146
1147/*
1148 * The 'fileformat' option is changed.
1149 */
1150 static char *
1151did_set_fileformat(char_u **varp, char_u *oldval, int opt_flags)
1152{
1153 if (!curbuf->b_p_ma && !(opt_flags & OPT_GLOBAL))
1154 return e_cannot_make_changes_modifiable_is_off;
1155 else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
1156 return e_invalid_argument;
1157
1158 // may also change 'textmode'
1159 if (get_fileformat(curbuf) == EOL_DOS)
1160 curbuf->b_p_tx = TRUE;
1161 else
1162 curbuf->b_p_tx = FALSE;
1163 redraw_titles();
1164 // update flag in swap file
1165 ml_setflags(curbuf);
1166 // Redraw needed when switching to/from "mac": a CR in the text
1167 // will be displayed differently.
1168 if (get_fileformat(curbuf) == EOL_MAC || *oldval == 'm')
1169 redraw_curbuf_later(UPD_NOT_VALID);
1170
1171 return NULL;
1172}
1173
1174/*
1175 * The 'fileformats' option is changed.
1176 */
1177 static char *
1178did_set_fileformats(void)
1179{
1180 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
1181 return e_invalid_argument;
1182
1183 // also change 'textauto'
1184 if (*p_ffs == NUL)
1185 p_ta = FALSE;
1186 else
1187 p_ta = TRUE;
1188
1189 return NULL;
1190}
1191
1192#if defined(FEAT_CRYPT)
1193/*
1194 * The 'cryptkey' option is changed.
1195 */
1196 static void
1197did_set_cryptkey(char_u *oldval)
1198{
1199 // Make sure the ":set" command doesn't show the new value in the
1200 // history.
1201 remove_key_from_history();
1202
1203 if (STRCMP(curbuf->b_p_key, oldval) != 0)
1204 // Need to update the swapfile.
1205 {
1206 ml_set_crypt_key(curbuf, oldval,
1207 *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
1208 changed_internal();
1209 }
1210}
1211
1212/*
1213 * The 'cryptmethod' option is changed.
1214 */
1215 static char *
1216did_set_cryptmethod(char_u *oldval, int opt_flags)
1217{
1218 char_u *p;
1219 char_u *s;
1220
1221 if (opt_flags & OPT_LOCAL)
1222 p = curbuf->b_p_cm;
1223 else
1224 p = p_cm;
1225 if (check_opt_strings(p, p_cm_values, TRUE) != OK)
1226 return e_invalid_argument;
1227 else if (crypt_self_test() == FAIL)
1228 return e_invalid_argument;
1229
1230 // When setting the global value to empty, make it "zip".
1231 if (*p_cm == NUL)
1232 {
1233 free_string_option(p_cm);
1234 p_cm = vim_strsave((char_u *)"zip");
1235 }
1236 // When using ":set cm=name" the local value is going to be empty.
1237 // Do that here, otherwise the crypt functions will still use the
1238 // local value.
1239 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1240 {
1241 free_string_option(curbuf->b_p_cm);
1242 curbuf->b_p_cm = empty_option;
1243 }
1244
1245 // Need to update the swapfile when the effective method changed.
1246 // Set "s" to the effective old value, "p" to the effective new
1247 // method and compare.
1248 if ((opt_flags & OPT_LOCAL) && *oldval == NUL)
1249 s = p_cm; // was previously using the global value
1250 else
1251 s = oldval;
1252 if (*curbuf->b_p_cm == NUL)
1253 p = p_cm; // is now using the global value
1254 else
1255 p = curbuf->b_p_cm;
1256 if (STRCMP(s, p) != 0)
1257 ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1258
1259 // If the global value changes need to update the swapfile for all
1260 // buffers using that value.
1261 if ((opt_flags & OPT_GLOBAL) && STRCMP(p_cm, oldval) != 0)
1262 {
1263 buf_T *buf;
1264
1265 FOR_ALL_BUFFERS(buf)
1266 if (buf != curbuf && *buf->b_p_cm == NUL)
1267 ml_set_crypt_key(buf, buf->b_p_key, oldval);
1268 }
1269 return NULL;
1270}
1271#endif
1272
1273/*
1274 * The 'matchpairs' option is changed.
1275 */
1276 static char *
1277did_set_matchpairs(char_u **varp)
1278{
1279 char_u *p;
1280
1281 if (has_mbyte)
1282 {
1283 for (p = *varp; *p != NUL; ++p)
1284 {
1285 int x2 = -1;
1286 int x3 = -1;
1287
1288 p += mb_ptr2len(p);
1289 if (*p != NUL)
1290 x2 = *p++;
1291 if (*p != NUL)
1292 {
1293 x3 = mb_ptr2char(p);
1294 p += mb_ptr2len(p);
1295 }
1296 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
1297 return e_invalid_argument;
1298 if (*p == NUL)
1299 break;
1300 }
1301 }
1302 else
1303 {
1304 // Check for "x:y,x:y"
1305 for (p = *varp; *p != NUL; p += 4)
1306 {
1307 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
1308 return e_invalid_argument;
1309 if (p[3] == NUL)
1310 break;
1311 }
1312 }
1313
1314 return NULL;
1315}
1316
1317/*
1318 * The 'comments' option is changed.
1319 */
1320 static char *
1321did_set_comments(char_u **varp, char *errbuf)
1322{
1323 char_u *s;
1324 char *errmsg = NULL;
1325
1326 for (s = *varp; *s; )
1327 {
1328 while (*s && *s != ':')
1329 {
1330 if (vim_strchr((char_u *)COM_ALL, *s) == NULL
1331 && !VIM_ISDIGIT(*s) && *s != '-')
1332 {
1333 errmsg = illegal_char(errbuf, *s);
1334 break;
1335 }
1336 ++s;
1337 }
1338 if (*s++ == NUL)
1339 errmsg = e_missing_colon;
1340 else if (*s == ',' || *s == NUL)
1341 errmsg = e_zero_length_string;
1342 if (errmsg != NULL)
1343 break;
1344 while (*s && *s != ',')
1345 {
1346 if (*s == '\\' && s[1] != NUL)
1347 ++s;
1348 ++s;
1349 }
1350 s = skip_to_option_part(s);
1351 }
1352
1353 return errmsg;
1354}
1355
1356/*
1357 * The global 'listchars' or 'fillchars' option is changed.
1358 */
1359 static char *
1360did_set_global_listfillchars(char_u **varp, int opt_flags)
1361{
1362 char *errmsg = NULL;
1363 char_u **local_ptr = varp == &p_lcs
1364 ? &curwin->w_p_lcs : &curwin->w_p_fcs;
1365
1366 // only apply the global value to "curwin" when it does not have a
1367 // local value
1368 errmsg = set_chars_option(curwin, varp,
1369 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL));
1370 if (errmsg != NULL)
1371 return errmsg;
1372
1373 tabpage_T *tp;
1374 win_T *wp;
1375
1376 // If the current window is set to use the global
1377 // 'listchars'/'fillchars' value, clear the window-local value.
1378 if (!(opt_flags & OPT_GLOBAL))
1379 clear_string_option(local_ptr);
1380 FOR_ALL_TAB_WINDOWS(tp, wp)
1381 {
1382 // If the current window has a local value need to apply it
1383 // again, it was changed when setting the global value.
1384 // If no error was returned above, we don't expect an error
1385 // here, so ignore the return value.
1386 local_ptr = varp == &p_lcs ? &wp->w_p_lcs : &wp->w_p_fcs;
1387 if (**local_ptr == NUL)
1388 (void)set_chars_option(wp, local_ptr, TRUE);
1389 }
1390
1391 redraw_all_later(UPD_NOT_VALID);
1392
1393 return NULL;
1394}
1395
1396/*
1397 * The 'verbosefile' option is changed.
1398 */
1399 static char *
1400did_set_verbosefile(void)
1401{
1402 verbose_stop();
1403 if (*p_vfile != NUL && verbose_open() == FAIL)
1404 return e_invalid_argument;
1405
1406 return NULL;
1407}
1408
1409#ifdef FEAT_VIMINFO
1410/*
1411 * The 'viminfo' option is changed.
1412 */
1413 static char *
1414did_set_viminfo(char *errbuf)
1415{
1416 char_u *s;
1417 char *errmsg = NULL;
1418
1419 for (s = p_viminfo; *s;)
1420 {
1421 // Check it's a valid character
1422 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
1423 {
1424 errmsg = illegal_char(errbuf, *s);
1425 break;
1426 }
1427 if (*s == 'n') // name is always last one
1428 break;
1429 else if (*s == 'r') // skip until next ','
1430 {
1431 while (*++s && *s != ',')
1432 ;
1433 }
1434 else if (*s == '%')
1435 {
1436 // optional number
1437 while (vim_isdigit(*++s))
1438 ;
1439 }
1440 else if (*s == '!' || *s == 'h' || *s == 'c')
1441 ++s; // no extra chars
1442 else // must have a number
1443 {
1444 while (vim_isdigit(*++s))
1445 ;
1446
1447 if (!VIM_ISDIGIT(*(s - 1)))
1448 {
1449 if (errbuf != NULL)
1450 {
1451 sprintf(errbuf,
1452 _(e_missing_number_after_angle_str_angle),
1453 transchar_byte(*(s - 1)));
1454 errmsg = errbuf;
1455 }
1456 else
1457 errmsg = "";
1458 break;
1459 }
1460 }
1461 if (*s == ',')
1462 ++s;
1463 else if (*s)
1464 {
1465 if (errbuf != NULL)
1466 errmsg = e_missing_comma;
1467 else
1468 errmsg = "";
1469 break;
1470 }
1471 }
1472 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
1473 errmsg = e_must_specify_a_value;
1474
1475 return errmsg;
1476}
1477#endif
1478
1479/*
1480 * Some terminal option (t_xxx) is changed
1481 */
1482 static void
1483did_set_term_option(char_u **varp, int *did_swaptcap UNUSED)
1484{
1485 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
1486 if (varp == &T_CCO)
1487 {
1488 int colors = atoi((char *)T_CCO);
1489
1490 // Only reinitialize colors if t_Co value has really changed to
1491 // avoid expensive reload of colorscheme if t_Co is set to the
1492 // same value multiple times.
1493 if (colors != t_colors)
1494 {
1495 t_colors = colors;
1496 if (t_colors <= 1)
1497 {
1498 vim_free(T_CCO);
1499 T_CCO = empty_option;
1500 }
1501#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
1502 if (is_term_win32())
1503 {
1504 swap_tcap();
1505 *did_swaptcap = TRUE;
1506 }
1507#endif
1508 // We now have a different color setup, initialize it again.
1509 init_highlight(TRUE, FALSE);
1510 }
1511 }
1512 ttest(FALSE);
1513 if (varp == &T_ME)
1514 {
1515 out_str(T_ME);
1516 redraw_later(UPD_CLEAR);
1517#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
1518 // Since t_me has been set, this probably means that the user
1519 // wants to use this as default colors. Need to reset default
1520 // background/foreground colors.
1521# ifdef VIMDLL
1522 if (!gui.in_use && !gui.starting)
1523# endif
1524 mch_set_normal_colors();
1525#endif
1526 }
1527 if (varp == &T_BE && termcap_active)
1528 {
1529 MAY_WANT_TO_LOG_THIS;
1530
1531 if (*T_BE == NUL)
1532 // When clearing t_BE we assume the user no longer wants
1533 // bracketed paste, thus disable it by writing t_BD.
1534 out_str(T_BD);
1535 else
1536 out_str(T_BE);
1537 }
1538}
1539
1540#ifdef FEAT_LINEBREAK
1541/*
1542 * The 'showbreak' option is changed.
1543 */
1544 static char *
1545did_set_showbreak(char_u **varp)
1546{
1547 char_u *s;
1548
1549 for (s = *varp; *s; )
1550 {
1551 if (ptr2cells(s) != 1)
1552 return e_showbreak_contains_unprintable_or_wide_character;
1553 MB_PTR_ADV(s);
1554 }
1555
1556 return NULL;
1557}
1558#endif
1559
1560#ifdef FEAT_GUI
1561/*
1562 * The 'guifont' option is changed.
1563 */
1564 static char *
Bram Moolenaareb45ad22023-01-30 19:26:24 +00001565did_set_guifont(char_u *oldval UNUSED, int *redraw_gui_only)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001566{
1567 char_u *p;
1568 char *errmsg = NULL;
1569
1570 if (gui.in_use)
1571 {
1572 p = p_guifont;
1573# if defined(FEAT_GUI_GTK)
1574 // Put up a font dialog and let the user select a new value.
1575 // If this is cancelled go back to the old value but don't
1576 // give an error message.
1577 if (STRCMP(p, "*") == 0)
1578 {
1579 p = gui_mch_font_dialog(oldval);
1580 free_string_option(p_guifont);
1581 p_guifont = (p != NULL) ? p : vim_strsave(oldval);
1582 }
1583# endif
1584 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
1585 {
1586# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
1587 if (STRCMP(p_guifont, "*") == 0)
1588 {
1589 // Dialog was cancelled: Keep the old value without giving
1590 // an error message.
1591 free_string_option(p_guifont);
1592 p_guifont = vim_strsave(oldval);
1593 }
1594 else
1595# endif
1596 errmsg = e_invalid_fonts;
1597 }
1598 }
1599 *redraw_gui_only = TRUE;
1600
1601 return errmsg;
1602}
1603
1604# ifdef FEAT_XFONTSET
1605/*
1606 * The 'guifontset' option is changed.
1607 */
1608 static char *
1609did_set_guifontset(int *redraw_gui_only)
1610{
1611 char *errmsg = NULL;
1612
1613 if (STRCMP(p_guifontset, "*") == 0)
1614 errmsg = e_cant_select_fontset;
1615 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
1616 errmsg = e_invalid_fontset;
1617 *redraw_gui_only = TRUE;
1618
1619 return errmsg;
1620}
1621# endif
1622
1623/*
1624 * The 'guifontwide' option is changed.
1625 */
1626 static char *
1627did_set_guifontwide(int *redraw_gui_only)
1628{
1629 char *errmsg = NULL;
1630
1631 if (STRCMP(p_guifontwide, "*") == 0)
1632 errmsg = e_cant_select_wide_font;
1633 else if (gui_get_wide_font() == FAIL)
1634 errmsg = e_invalid_wide_font;
1635 *redraw_gui_only = TRUE;
1636
1637 return errmsg;
1638}
1639#endif
1640
1641#if defined(FEAT_GUI_GTK)
1642 static void
1643did_set_guiligatures(int *redraw_gui_only)
1644{
1645 gui_set_ligatures();
1646 *redraw_gui_only = TRUE;
1647}
1648#endif
1649
1650#ifdef FEAT_MOUSESHAPE
1651 static char *
1652did_set_mouseshape(void)
1653{
1654 char *errmsg = NULL;
1655
1656 errmsg = parse_shape_opt(SHAPE_MOUSE);
1657 update_mouseshape(-1);
1658
1659 return errmsg;
1660}
1661#endif
1662
1663/*
1664 * The 'titlestring' or the 'iconstring' option is changed.
1665 */
1666 static void
1667did_set_titleiconstring(char_u **varp UNUSED)
1668{
1669#ifdef FEAT_STL_OPT
1670 int flagval = (varp == &p_titlestring) ? STL_IN_TITLE : STL_IN_ICON;
1671
1672 // NULL => statusline syntax
1673 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
1674 stl_syntax |= flagval;
1675 else
1676 stl_syntax &= ~flagval;
1677#endif
1678 did_set_title();
1679}
1680
1681#ifdef FEAT_GUI
1682/*
1683 * The 'guioptions' option is changed.
1684 */
1685 static void
1686did_set_guioptions(char_u *oldval, int *redraw_gui_only)
1687{
1688 gui_init_which_components(oldval);
1689 *redraw_gui_only = TRUE;
1690}
1691#endif
1692
1693#if defined(FEAT_GUI_TABLINE)
1694 static void
1695did_set_guitablabel(int *redraw_gui_only)
1696{
1697 redraw_tabline = TRUE;
1698 *redraw_gui_only = TRUE;
1699}
1700#endif
1701
1702#if defined(UNIX) || defined(VMS)
1703/*
1704 * The 'ttymouse' option is changed.
1705 */
1706 static char *
1707did_set_ttymouse(void)
1708{
1709 char *errmsg = NULL;
1710
1711 // Switch the mouse off before changing the escape sequences used for
1712 // that.
1713 mch_setmouse(FALSE);
1714 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
1715 errmsg = e_invalid_argument;
1716 else
1717 check_mouse_termcode();
1718 if (termcap_active)
1719 setmouse(); // may switch it on again
1720
1721 return errmsg;
1722}
1723#endif
1724
1725/*
1726 * The 'selection' option is changed.
1727 */
1728 static char *
1729did_set_selection(void)
1730{
1731 if (*p_sel == NUL
1732 || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
1733 return e_invalid_argument;
1734
1735 return NULL;
1736}
1737
1738#ifdef FEAT_BROWSE
1739/*
1740 * The 'browsedir' option is changed.
1741 */
1742 static char *
1743did_set_browsedir(void)
1744{
1745 if (check_opt_strings(p_bsdir, p_bsdir_values, FALSE) != OK
1746 && !mch_isdir(p_bsdir))
1747 return e_invalid_argument;
1748
1749 return NULL;
1750}
1751#endif
1752
1753/*
1754 * The 'keymodel' option is changed.
1755 */
1756 static char *
1757did_set_keymodel(void)
1758{
1759 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
1760 return e_invalid_argument;
1761
1762 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
1763 km_startsel = (vim_strchr(p_km, 'a') != NULL);
1764 return NULL;
1765}
1766
1767/*
1768 * The 'keyprotocol' option is changed.
1769 */
1770 static char *
1771did_set_keyprotocol(void)
1772{
1773 if (match_keyprotocol(NULL) == KEYPROTOCOL_FAIL)
1774 return e_invalid_argument;
1775
1776 return NULL;
1777}
1778
1779/*
1780 * The 'mousemodel' option is changed.
1781 */
1782 static char *
1783did_set_mousemodel(void)
1784{
1785 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
1786 return e_invalid_argument;
1787#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
1788 else if (*p_mousem != *oldval)
1789 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
1790 // to create or delete the popup menus.
1791 gui_motif_update_mousemodel(root_menu);
1792#endif
1793
1794 return NULL;
1795}
1796
1797/*
1798 * The 'display' option is changed.
1799 */
1800 static char *
1801did_set_display(void)
1802{
1803 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
1804 return e_invalid_argument;
1805
1806 (void)init_chartab();
1807 return NULL;
1808}
1809
1810#ifdef FEAT_SPELL
1811/*
1812 * The 'spellfile' option is changed.
1813 */
1814 static char *
1815did_set_spellfile(char_u **varp)
1816{
1817 if (!valid_spellfile(*varp))
1818 return e_invalid_argument;
1819
1820 // If there is a window for this buffer in which 'spell' is set load the
1821 // wordlists.
1822 return did_set_spell_option(TRUE);
1823}
1824
1825/*
1826 * The 'spell' option is changed.
1827 */
1828 static char *
1829did_set_spell(char_u **varp)
1830{
1831 if (!valid_spelllang(*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(FALSE);
1837}
1838
1839/*
1840 * The 'spellcapcheck' option is changed.
1841 */
1842 static char *
1843did_set_spellcapcheck(void)
1844{
1845 // compile the regexp program.
1846 return compile_cap_prog(curwin->w_s);
1847}
1848
1849/*
1850 * The 'spelloptions' option is changed.
1851 */
1852 static char *
1853did_set_spelloptions(char_u **varp)
1854{
1855 if (**varp != NUL && STRCMP("camel", *varp) != 0)
1856 return e_invalid_argument;
1857
1858 return NULL;
1859}
1860
1861/*
1862 * The 'spellsuggest' option is changed.
1863 */
1864 static char *
1865did_set_spellsuggest(void)
1866{
1867 if (spell_check_sps() != OK)
1868 return e_invalid_argument;
1869
1870 return NULL;
1871}
1872
1873/*
1874 * The 'mkspellmem' option is changed.
1875 */
1876 static char *
1877did_set_mkspellmem(void)
1878{
1879 if (spell_check_msm() != OK)
1880 return e_invalid_argument;
1881
1882 return NULL;
1883}
1884#endif
1885
1886/*
1887 * The 'buftype' option is changed.
1888 */
1889 static char *
1890did_set_buftype(void)
1891{
1892 if (check_opt_strings(curbuf->b_p_bt, p_buftype_values, FALSE) != OK)
1893 return e_invalid_argument;
1894
1895 if (curwin->w_status_height)
1896 {
1897 curwin->w_redr_status = TRUE;
1898 redraw_later(UPD_VALID);
1899 }
1900 curbuf->b_help = (curbuf->b_p_bt[0] == 'h');
1901 redraw_titles();
1902
1903 return NULL;
1904}
1905
1906#ifdef FEAT_STL_OPT
1907/*
1908 * The 'statusline' or the 'tabline' or the 'rulerformat' option is changed.
1909 */
1910 static char *
1911did_set_statusline(char_u **varp)
1912{
1913 char_u *s;
1914 char *errmsg = NULL;
1915 int wid;
1916
1917 if (varp == &p_ruf) // reset ru_wid first
1918 ru_wid = 0;
1919 s = *varp;
1920 if (varp == &p_ruf && *s == '%')
1921 {
1922 // set ru_wid if 'ruf' starts with "%99("
1923 if (*++s == '-') // ignore a '-'
1924 s++;
1925 wid = getdigits(&s);
1926 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
1927 ru_wid = wid;
1928 else
1929 errmsg = check_stl_option(p_ruf);
1930 }
1931 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
1932 else if (varp == &p_ruf || s[0] != '%' || s[1] != '!')
1933 errmsg = check_stl_option(s);
1934 if (varp == &p_ruf && errmsg == NULL)
1935 comp_col();
1936
1937 return errmsg;
1938}
1939#endif
1940
1941/*
1942 * The 'complete' option is changed.
1943 */
1944 static char *
1945did_set_complete(char_u **varp, char *errbuf)
1946{
1947 char_u *s;
1948
1949 // check if it is a valid value for 'complete' -- Acevedo
1950 for (s = *varp; *s;)
1951 {
1952 while (*s == ',' || *s == ' ')
1953 s++;
1954 if (!*s)
1955 break;
1956 if (vim_strchr((char_u *)".wbuksid]tU", *s) == NULL)
1957 return illegal_char(errbuf, *s);
1958 if (*++s != NUL && *s != ',' && *s != ' ')
1959 {
1960 if (s[-1] == 'k' || s[-1] == 's')
1961 {
1962 // skip optional filename after 'k' and 's'
1963 while (*s && *s != ',' && *s != ' ')
1964 {
1965 if (*s == '\\' && s[1] != NUL)
1966 ++s;
1967 ++s;
1968 }
1969 }
1970 else
1971 {
1972 if (errbuf != NULL)
1973 {
1974 sprintf((char *)errbuf,
1975 _(e_illegal_character_after_chr), *--s);
1976 return errbuf;
1977 }
1978 return "";
1979 }
1980 }
1981 }
1982
1983 return NULL;
1984}
1985
1986/*
1987 * The 'completeopt' option is changed.
1988 */
1989 static char *
1990did_set_completeopt(void)
1991{
1992 if (check_opt_strings(p_cot, p_cot_values, TRUE) != OK)
1993 return e_invalid_argument;
1994
1995 completeopt_was_set();
1996 return NULL;
1997}
1998
1999#ifdef FEAT_SIGNS
2000/*
2001 * The 'signcolumn' option is changed.
2002 */
2003 static char *
2004did_set_signcolumn(char_u **varp, char_u *oldval)
2005{
2006 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
2007 return e_invalid_argument;
2008 // When changing the 'signcolumn' to or from 'number', recompute the
2009 // width of the number column if 'number' or 'relativenumber' is set.
2010 if (((*oldval == 'n' && *(oldval + 1) == 'u')
2011 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
2012 && (curwin->w_p_nu || curwin->w_p_rnu))
2013 curwin->w_nrwidth_line_count = 0;
2014
2015 return NULL;
2016}
2017#endif
2018
2019#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
2020/*
2021 * The 'toolbar' option is changed.
2022 */
2023 static char *
2024did_set_toolbar(void)
2025{
2026 if (opt_strings_flags(p_toolbar, p_toolbar_values,
2027 &toolbar_flags, TRUE) != OK)
2028 return e_invalid_argument;
2029
2030 out_flush();
2031 gui_mch_show_toolbar((toolbar_flags &
2032 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
2033 return NULL;
2034}
2035#endif
2036
2037#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
2038/*
2039 * The 'toolbariconsize' option is changed. GTK+ 2 only.
2040 */
2041 static char *
2042did_set_toolbariconsize(void)
2043{
2044 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
2045 return e_invalid_argument;
2046
2047 out_flush();
2048 gui_mch_show_toolbar((toolbar_flags &
2049 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
2050 return NULL;
2051}
2052#endif
2053
2054/*
2055 * The 'pastetoggle' option is changed.
2056 */
2057 static void
2058did_set_pastetoggle(void)
2059{
2060 char_u *p;
2061
2062 // translate key codes like in a mapping
2063 if (*p_pt)
2064 {
2065 (void)replace_termcodes(p_pt, &p,
2066 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
2067 if (p != NULL)
2068 {
2069 free_string_option(p_pt);
2070 p_pt = p;
2071 }
2072 }
2073}
2074
2075/*
2076 * The 'backspace' option is changed.
2077 */
2078 static char *
2079did_set_backspace(void)
2080{
2081 if (VIM_ISDIGIT(*p_bs))
2082 {
2083 if (*p_bs > '3' || p_bs[1] != NUL)
2084 return e_invalid_argument;
2085 }
2086 else if (check_opt_strings(p_bs, p_bs_values, TRUE) != OK)
2087 return e_invalid_argument;
2088
2089 return NULL;
2090}
2091
2092/*
2093 * The 'tagcase' option is changed.
2094 */
2095 static char *
2096did_set_tagcase(int opt_flags)
2097{
2098 unsigned int *flags;
2099 char_u *p;
2100
2101 if (opt_flags & OPT_LOCAL)
2102 {
2103 p = curbuf->b_p_tc;
2104 flags = &curbuf->b_tc_flags;
2105 }
2106 else
2107 {
2108 p = p_tc;
2109 flags = &tc_flags;
2110 }
2111
2112 if ((opt_flags & OPT_LOCAL) && *p == NUL)
2113 // make the local value empty: use the global value
2114 *flags = 0;
2115 else if (*p == NUL
2116 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
2117 return e_invalid_argument;
2118
2119 return NULL;
2120}
2121
2122#ifdef FEAT_DIFF
2123/*
2124 * The 'diffopt' option is changed.
2125 */
2126 static char *
2127did_set_diffopt(void)
2128{
2129 if (diffopt_changed() == FAIL)
2130 return e_invalid_argument;
2131
2132 return NULL;
2133}
2134#endif
2135
2136#ifdef FEAT_FOLDING
2137/*
2138 * The 'foldmethod' option is changed.
2139 */
2140 static char *
2141did_set_foldmethod(char_u **varp)
2142{
2143 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK
2144 || *curwin->w_p_fdm == NUL)
2145 return e_invalid_argument;
2146
2147 foldUpdateAll(curwin);
2148 if (foldmethodIsDiff(curwin))
2149 newFoldLevel();
2150 return NULL;
2151}
2152
2153/*
2154 * The 'foldmarker' option is changed.
2155 */
2156 static char *
2157did_set_foldmarker(char_u **varp)
2158{
2159 char_u *p;
2160
2161 p = vim_strchr(*varp, ',');
2162 if (p == NULL)
2163 return e_comma_required;
2164 else if (p == *varp || p[1] == NUL)
2165 return e_invalid_argument;
2166 else if (foldmethodIsMarker(curwin))
2167 foldUpdateAll(curwin);
2168
2169 return NULL;
2170}
2171
2172/*
2173 * The 'commentstring' option is changed.
2174 */
2175 static char *
2176did_set_commentstring(char_u **varp)
2177{
2178 if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
2179 return e_commentstring_must_be_empty_or_contain_str;
2180
2181 return NULL;
2182}
2183
2184/*
2185 * The 'foldignore' option is changed.
2186 */
2187 static void
2188did_set_foldignore(void)
2189{
2190 if (foldmethodIsIndent(curwin))
2191 foldUpdateAll(curwin);
2192}
2193#endif
2194
2195/*
2196 * The 'virtualedit' option is changed.
2197 */
2198 static char *
2199did_set_virtualedit(char_u *oldval, int opt_flags)
2200{
2201 char_u *ve = p_ve;
2202 unsigned int *flags = &ve_flags;
2203
2204 if (opt_flags & OPT_LOCAL)
2205 {
2206 ve = curwin->w_p_ve;
2207 flags = &curwin->w_ve_flags;
2208 }
2209
2210 if ((opt_flags & OPT_LOCAL) && *ve == NUL)
2211 // make the local value empty: use the global value
2212 *flags = 0;
2213 else
2214 {
2215 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
2216 return e_invalid_argument;
2217 else if (STRCMP(ve, oldval) != 0)
2218 {
2219 // Recompute cursor position in case the new 've' setting
2220 // changes something.
2221 validate_virtcol();
2222 coladvance(curwin->w_virtcol);
2223 }
2224 }
2225
2226 return NULL;
2227}
2228
2229#if defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)
2230/*
2231 * The 'cscopequickfix' option is changed.
2232 */
2233 static char *
2234did_set_cscopequickfix(void)
2235{
2236 char_u *p;
2237
2238 if (p_csqf == NULL)
2239 return NULL;
2240
2241 p = p_csqf;
2242 while (*p != NUL)
2243 {
2244 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
2245 || p[1] == NUL
2246 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
2247 || (p[2] != NUL && p[2] != ','))
2248 return e_invalid_argument;
2249 else if (p[2] == NUL)
2250 break;
2251 else
2252 p += 3;
2253 }
2254
2255 return NULL;
2256}
2257#endif
2258
2259/*
2260 * The 'cinoptions' option is changed.
2261 */
2262 static void
2263did_set_cinoptions(void)
2264{
2265 // TODO: recognize errors
2266 parse_cino(curbuf);
2267}
2268
2269/*
2270 * The 'lispoptions' option is changed.
2271 */
2272 static char *
2273did_set_lispoptions(char_u **varp)
2274{
2275 if (**varp != NUL && STRCMP(*varp, "expr:0") != 0
2276 && STRCMP(*varp, "expr:1") != 0)
2277 return e_invalid_argument;
2278
2279 return NULL;
2280}
2281
2282#if defined(FEAT_RENDER_OPTIONS)
2283/*
2284 * The 'renderoptions' option is changed.
2285 */
2286 static char *
2287did_set_renderoptions(void)
2288{
2289 if (!gui_mch_set_rendering_options(p_rop))
2290 return e_invalid_argument;
2291
2292 return NULL;
2293}
2294#endif
2295
2296/*
2297 * The 'filetype' or the 'syntax' option is changed.
2298 */
2299 static char *
2300did_set_filetype_or_syntax(
2301 char_u **varp,
2302 char_u *oldval,
2303 int *value_checked,
2304 int *value_changed)
2305{
2306 if (!valid_filetype(*varp))
2307 return e_invalid_argument;
2308
2309 *value_changed = STRCMP(oldval, *varp) != 0;
2310
2311 // Since we check the value, there is no need to set P_INSECURE,
2312 // even when the value comes from a modeline.
2313 *value_checked = TRUE;
2314
2315 return NULL;
2316}
2317
2318#ifdef FEAT_TERMINAL
2319/*
2320 * The 'termwinkey' option is changed.
2321 */
2322 static char *
2323did_set_termwinkey(void)
2324{
2325 if (*curwin->w_p_twk != NUL
2326 && string_to_key(curwin->w_p_twk, TRUE) == 0)
2327 return e_invalid_argument;
2328
2329 return NULL;
2330}
2331
2332/*
2333 * The 'termwinsize' option is changed.
2334 */
2335 static char *
2336did_set_termwinsize(void)
2337{
2338 char_u *p;
2339
2340 if (*curwin->w_p_tws == NUL)
2341 return NULL;
2342
2343 p = skipdigits(curwin->w_p_tws);
2344 if (p == curwin->w_p_tws
2345 || (*p != 'x' && *p != '*')
2346 || *skipdigits(p + 1) != NUL)
2347 return e_invalid_argument;
2348
2349 return NULL;
2350}
2351#endif
2352
2353#ifdef FEAT_VARTABS
2354/*
2355 * The 'varsofttabstop' option is changed.
2356 */
2357 static char *
2358did_set_varsofttabstop(char_u **varp)
2359{
2360 char_u *cp;
2361
2362 if (!(*varp)[0] || ((*varp)[0] == '0' && !(*varp)[1]))
2363 {
2364 if (curbuf->b_p_vsts_array)
2365 {
2366 vim_free(curbuf->b_p_vsts_array);
2367 curbuf->b_p_vsts_array = 0;
2368 }
2369 }
2370 else
2371 {
2372 for (cp = *varp; *cp; ++cp)
2373 {
2374 if (vim_isdigit(*cp))
2375 continue;
2376 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
2377 continue;
2378 return e_invalid_argument;
2379 }
2380
2381 int *oldarray = curbuf->b_p_vsts_array;
2382 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
2383 {
2384 if (oldarray)
2385 vim_free(oldarray);
2386 }
2387 else
2388 return e_invalid_argument;
2389 }
2390
2391 return NULL;
2392}
2393
2394/*
2395 * The 'vartabstop' option is changed.
2396 */
2397 static char *
2398did_set_vartabstop(char_u **varp)
2399{
2400 char_u *cp;
2401
2402 if (!(*varp)[0] || ((*varp)[0] == '0' && !(*varp)[1]))
2403 {
2404 if (curbuf->b_p_vts_array)
2405 {
2406 vim_free(curbuf->b_p_vts_array);
2407 curbuf->b_p_vts_array = NULL;
2408 }
2409 }
2410 else
2411 {
2412 for (cp = *varp; *cp; ++cp)
2413 {
2414 if (vim_isdigit(*cp))
2415 continue;
2416 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
2417 continue;
2418 return e_invalid_argument;
2419 }
2420
2421 int *oldarray = curbuf->b_p_vts_array;
2422
2423 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
2424 {
2425 vim_free(oldarray);
2426# ifdef FEAT_FOLDING
2427 if (foldmethodIsIndent(curwin))
2428 foldUpdateAll(curwin);
2429# endif
2430 }
2431 else
2432 return e_invalid_argument;
2433 }
2434
2435 return NULL;
2436}
2437#endif
2438
2439#ifdef FEAT_PROP_POPUP
2440/*
2441 * The 'previewpopup' option is changed.
2442 */
2443 static char *
2444did_set_previewpopup(void)
2445{
2446 if (parse_previewpopup(NULL) == FAIL)
2447 return e_invalid_argument;
2448
2449 return NULL;
2450}
2451
2452# ifdef FEAT_QUICKFIX
2453/*
2454 * The 'completepopup' option is changed.
2455 */
2456 static char *
2457did_set_completepopup(void)
2458{
2459 if (parse_completepopup(NULL) == FAIL)
2460 return e_invalid_argument;
2461
2462 popup_close_info();
2463 return NULL;
2464}
2465# endif
2466#endif
2467
2468#ifdef FEAT_EVAL
2469/*
2470 * One of the '*expr' options is changed: 'balloonexpr', 'diffexpr',
2471 * 'foldexpr', 'foldtext', 'formatexpr', 'includeexpr', 'indentexpr',
2472 * 'patchexpr', 'printexpr' and 'charconvert'.
2473 *
2474 */
2475 static void
2476did_set_optexpr(char_u **varp)
2477{
2478 // If the option value starts with <SID> or s:, then replace that with
2479 // the script identifier.
2480 char_u *name = get_scriptlocal_funcname(*varp);
2481 if (name != NULL)
2482 {
2483 free_string_option(*varp);
2484 *varp = name;
2485 }
2486
2487# ifdef FEAT_FOLDING
2488 if (varp == &curwin->w_p_fde && foldmethodIsExpr(curwin))
2489 foldUpdateAll(curwin);
2490# endif
2491}
2492#endif
2493
2494/*
Bram Moolenaardac13472019-09-16 21:06:21 +02002495 * Handle string options that need some action to perform when changed.
zeertzjqf6782732022-07-27 18:26:03 +01002496 * The new value must be allocated.
LemonBoy77142312022-04-15 20:50:46 +01002497 * Returns NULL for success, or an unstranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +02002498 */
2499 char *
2500did_set_string_option(
2501 int opt_idx, // index in options[] table
2502 char_u **varp, // pointer to the option variable
Bram Moolenaardac13472019-09-16 21:06:21 +02002503 char_u *oldval, // previous value of the option
2504 char *errbuf, // buffer for errors, or NULL
2505 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
2506 int *value_checked) // value was checked to be save, no
2507 // need to set P_INSECURE
2508{
2509 char *errmsg = NULL;
2510 char_u *s, *p;
2511 int did_chartab = FALSE;
2512 char_u **gvarp;
2513 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
2514#ifdef FEAT_GUI
2515 // set when changing an option that only requires a redraw in the GUI
2516 int redraw_gui_only = FALSE;
2517#endif
2518 int value_changed = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02002519 int did_swaptcap = FALSE;
Bram Moolenaardac13472019-09-16 21:06:21 +02002520
2521 // Get the global option to compare with, otherwise we would have to check
2522 // two values for all local options.
2523 gvarp = (char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
2524
2525 // Disallow changing some options from secure mode
2526 if ((secure
2527#ifdef HAVE_SANDBOX
2528 || sandbox != 0
2529#endif
2530 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00002531 errmsg = e_not_allowed_here;
Bram Moolenaardac13472019-09-16 21:06:21 +02002532
2533 // Check for a "normal" directory or file name in some options. Disallow a
2534 // path separator (slash and/or backslash), wildcards and characters that
2535 // are often illegal in a file name. Be more permissive if "secure" is off.
2536 else if (((get_option_flags(opt_idx) & P_NFNAME)
2537 && vim_strpbrk(*varp, (char_u *)(secure
2538 ? "/\\*?[|;&<>\r\n" : "/\\*?[<>\r\n")) != NULL)
2539 || ((get_option_flags(opt_idx) & P_NDNAME)
2540 && vim_strpbrk(*varp, (char_u *)"*?[|;&<>\r\n") != NULL))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002541 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002542
2543 // 'term'
2544 else if (varp == &T_NAME)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002545 errmsg = did_set_term(&opt_idx, &free_oldval);
Bram Moolenaardac13472019-09-16 21:06:21 +02002546
2547 // 'backupcopy'
2548 else if (gvarp == &p_bkc)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002549 errmsg = did_set_backupcopy(oldval, opt_flags);
Bram Moolenaardac13472019-09-16 21:06:21 +02002550
2551 // 'backupext' and 'patchmode'
2552 else if (varp == &p_bex || varp == &p_pm)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002553 errmsg = did_set_backupext_or_patchmode();
2554
Bram Moolenaardac13472019-09-16 21:06:21 +02002555#ifdef FEAT_LINEBREAK
2556 // 'breakindentopt'
2557 else if (varp == &curwin->w_p_briopt)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002558 errmsg = did_set_breakindentopt();
Bram Moolenaardac13472019-09-16 21:06:21 +02002559#endif
2560
2561 // 'isident', 'iskeyword', 'isprint or 'isfname' option: refill g_chartab[]
2562 // If the new option is invalid, use old value. 'lisp' option: refill
2563 // g_chartab[] for '-' char
2564 else if ( varp == &p_isi
2565 || varp == &(curbuf->b_p_isk)
2566 || varp == &p_isp
2567 || varp == &p_isf)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002568 errmsg = did_set_isopt(&did_chartab);
Bram Moolenaardac13472019-09-16 21:06:21 +02002569
2570 // 'helpfile'
2571 else if (varp == &p_hf)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002572 did_set_helpfile();
Bram Moolenaardac13472019-09-16 21:06:21 +02002573
2574#ifdef FEAT_SYN_HL
2575 // 'cursorlineopt'
2576 else if (varp == &curwin->w_p_culopt
2577 || gvarp == &curwin->w_allbuf_opt.wo_culopt)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002578 errmsg = did_set_cursorlineopt(varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02002579
2580 // 'colorcolumn'
2581 else if (varp == &curwin->w_p_cc)
2582 errmsg = check_colorcolumn(curwin);
2583#endif
2584
2585#ifdef FEAT_MULTI_LANG
2586 // 'helplang'
2587 else if (varp == &p_hlg)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002588 errmsg = did_set_helplang();
Bram Moolenaardac13472019-09-16 21:06:21 +02002589#endif
2590
2591 // 'highlight'
2592 else if (varp == &p_hl)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002593 errmsg = did_set_highlight();
Bram Moolenaardac13472019-09-16 21:06:21 +02002594
2595 // 'nrformats'
2596 else if (gvarp == &p_nf)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002597 errmsg = did_set_opt_strings(*varp, p_nf_values, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +02002598
2599#ifdef FEAT_SESSION
2600 // 'sessionoptions'
2601 else if (varp == &p_ssop)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002602 errmsg = did_set_sessionoptions(oldval);
2603
Bram Moolenaardac13472019-09-16 21:06:21 +02002604 // 'viewoptions'
2605 else if (varp == &p_vop)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002606 errmsg = did_set_opt_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +02002607#endif
2608
2609 // 'scrollopt'
2610 else if (varp == &p_sbo)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002611 errmsg = did_set_opt_strings(p_sbo, p_scbopt_values, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +02002612
2613 // 'ambiwidth'
2614 else if (varp == &p_ambw || varp == &p_emoji)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002615 errmsg = did_set_ambiwidth();
Bram Moolenaardac13472019-09-16 21:06:21 +02002616
2617 // 'background'
2618 else if (varp == &p_bg)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002619 errmsg = did_set_background();
Bram Moolenaardac13472019-09-16 21:06:21 +02002620
2621 // 'wildmode'
2622 else if (varp == &p_wim)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002623 errmsg = did_set_wildmode();
Bram Moolenaardac13472019-09-16 21:06:21 +02002624
2625 // 'wildoptions'
2626 else if (varp == &p_wop)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002627 errmsg = did_set_opt_strings(p_wop, p_wop_values, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +02002628
2629#ifdef FEAT_WAK
2630 // 'winaltkeys'
2631 else if (varp == &p_wak)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002632 errmsg = did_set_winaltkeys();
Bram Moolenaardac13472019-09-16 21:06:21 +02002633#endif
2634
2635 // 'eventignore'
2636 else if (varp == &p_ei)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002637 errmsg = did_set_eventignore();
Bram Moolenaardac13472019-09-16 21:06:21 +02002638
2639 // 'encoding', 'fileencoding', 'termencoding' and 'makeencoding'
2640 else if (varp == &p_enc || gvarp == &p_fenc || varp == &p_tenc
2641 || gvarp == &p_menc)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002642 errmsg = did_set_encoding(varp, gvarp, opt_flags);
Bram Moolenaardac13472019-09-16 21:06:21 +02002643
2644#if defined(FEAT_POSTSCRIPT)
2645 else if (varp == &p_penc)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002646 did_set_printencoding();
Bram Moolenaardac13472019-09-16 21:06:21 +02002647#endif
2648
2649#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2650 else if (varp == &p_imak)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002651 errmsg = did_set_imactivatekey();
Bram Moolenaardac13472019-09-16 21:06:21 +02002652#endif
2653
2654#ifdef FEAT_KEYMAP
2655 else if (varp == &curbuf->b_p_keymap)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002656 errmsg = did_set_keymap(varp, opt_flags, value_checked);
Bram Moolenaardac13472019-09-16 21:06:21 +02002657#endif
2658
2659 // 'fileformat'
2660 else if (gvarp == &p_ff)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002661 errmsg = did_set_fileformat(varp, oldval, opt_flags);
Bram Moolenaardac13472019-09-16 21:06:21 +02002662
2663 // 'fileformats'
2664 else if (varp == &p_ffs)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002665 errmsg = did_set_fileformats();
Bram Moolenaardac13472019-09-16 21:06:21 +02002666
2667#if defined(FEAT_CRYPT)
2668 // 'cryptkey'
2669 else if (gvarp == &p_key)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002670 did_set_cryptkey(oldval);
Bram Moolenaardac13472019-09-16 21:06:21 +02002671
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002672 // 'cryptmethod'
Bram Moolenaardac13472019-09-16 21:06:21 +02002673 else if (gvarp == &p_cm)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002674 errmsg = did_set_cryptmethod(oldval, opt_flags);
Bram Moolenaardac13472019-09-16 21:06:21 +02002675#endif
2676
2677 // 'matchpairs'
2678 else if (gvarp == &p_mps)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002679 errmsg = did_set_matchpairs(varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02002680
Bram Moolenaardac13472019-09-16 21:06:21 +02002681 // 'comments'
2682 else if (gvarp == &p_com)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002683 errmsg = did_set_comments(varp, errbuf);
Bram Moolenaardac13472019-09-16 21:06:21 +02002684
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01002685 // global 'listchars' or 'fillchars'
2686 else if (varp == &p_lcs || varp == &p_fcs)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002687 errmsg = did_set_global_listfillchars(varp, opt_flags);
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01002688
Bram Moolenaareed9d462021-02-15 20:38:25 +01002689 // local 'listchars'
2690 else if (varp == &curwin->w_p_lcs)
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01002691 errmsg = set_chars_option(curwin, varp, TRUE);
Bram Moolenaareed9d462021-02-15 20:38:25 +01002692
Bram Moolenaar96ba25a2022-07-04 17:34:33 +01002693 // local 'fillchars'
2694 else if (varp == &curwin->w_p_fcs)
Bram Moolenaarb67f0c82022-07-04 21:03:36 +01002695 errmsg = set_chars_option(curwin, varp, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +02002696
Bram Moolenaardac13472019-09-16 21:06:21 +02002697 // 'cedit'
2698 else if (varp == &p_cedit)
Bram Moolenaardac13472019-09-16 21:06:21 +02002699 errmsg = check_cedit();
Bram Moolenaardac13472019-09-16 21:06:21 +02002700
2701 // 'verbosefile'
2702 else if (varp == &p_vfile)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002703 errmsg = did_set_verbosefile();
Bram Moolenaardac13472019-09-16 21:06:21 +02002704
2705#ifdef FEAT_VIMINFO
2706 // 'viminfo'
2707 else if (varp == &p_viminfo)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002708 errmsg = did_set_viminfo(errbuf);
Bram Moolenaardac13472019-09-16 21:06:21 +02002709#endif // FEAT_VIMINFO
2710
2711 // terminal options
2712 else if (istermoption_idx(opt_idx) && full_screen)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002713 did_set_term_option(varp, &did_swaptcap);
Bram Moolenaardac13472019-09-16 21:06:21 +02002714
2715#ifdef FEAT_LINEBREAK
2716 // 'showbreak'
Bram Moolenaaree857022019-11-09 23:26:40 +01002717 else if (gvarp == &p_sbr)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002718 errmsg = did_set_showbreak(varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02002719#endif
2720
2721#ifdef FEAT_GUI
2722 // 'guifont'
2723 else if (varp == &p_guifont)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002724 errmsg = did_set_guifont(oldval, &redraw_gui_only);
2725
Bram Moolenaardac13472019-09-16 21:06:21 +02002726# ifdef FEAT_XFONTSET
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002727 // 'guifontset'
Bram Moolenaardac13472019-09-16 21:06:21 +02002728 else if (varp == &p_guifontset)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002729 errmsg = did_set_guifontset(&redraw_gui_only);
Bram Moolenaardac13472019-09-16 21:06:21 +02002730# endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002731
2732 // 'guifontwide'
Bram Moolenaardac13472019-09-16 21:06:21 +02002733 else if (varp == &p_guifontwide)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002734 errmsg = did_set_guifontwide(&redraw_gui_only);
Bram Moolenaardac13472019-09-16 21:06:21 +02002735#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002736#if defined(FEAT_GUI_GTK)
Dusan Popovic4eeedc02021-10-16 20:52:05 +01002737 else if (varp == &p_guiligatures)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002738 did_set_guiligatures(&redraw_gui_only);
2739#endif
Bram Moolenaardac13472019-09-16 21:06:21 +02002740
2741#ifdef CURSOR_SHAPE
2742 // 'guicursor'
2743 else if (varp == &p_guicursor)
2744 errmsg = parse_shape_opt(SHAPE_CURSOR);
2745#endif
2746
2747#ifdef FEAT_MOUSESHAPE
2748 // 'mouseshape'
2749 else if (varp == &p_mouseshape)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002750 errmsg = did_set_mouseshape();
Bram Moolenaardac13472019-09-16 21:06:21 +02002751#endif
2752
2753#ifdef FEAT_PRINTER
2754 else if (varp == &p_popt)
2755 errmsg = parse_printoptions();
2756# if defined(FEAT_POSTSCRIPT)
2757 else if (varp == &p_pmfn)
2758 errmsg = parse_printmbfont();
2759# endif
2760#endif
2761
2762#ifdef FEAT_LANGMAP
2763 // 'langmap'
2764 else if (varp == &p_langmap)
2765 langmap_set();
2766#endif
2767
2768#ifdef FEAT_LINEBREAK
2769 // 'breakat'
2770 else if (varp == &p_breakat)
2771 fill_breakat_flags();
2772#endif
2773
Bram Moolenaardac13472019-09-16 21:06:21 +02002774 // 'titlestring' and 'iconstring'
2775 else if (varp == &p_titlestring || varp == &p_iconstring)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002776 did_set_titleiconstring(varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02002777
2778#ifdef FEAT_GUI
2779 // 'guioptions'
2780 else if (varp == &p_go)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002781 did_set_guioptions(oldval, &redraw_gui_only);
Bram Moolenaardac13472019-09-16 21:06:21 +02002782#endif
2783
2784#if defined(FEAT_GUI_TABLINE)
2785 // 'guitablabel'
2786 else if (varp == &p_gtl)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002787 did_set_guitablabel(&redraw_gui_only);
Bram Moolenaardac13472019-09-16 21:06:21 +02002788 // 'guitabtooltip'
2789 else if (varp == &p_gtt)
Bram Moolenaardac13472019-09-16 21:06:21 +02002790 redraw_gui_only = TRUE;
Bram Moolenaardac13472019-09-16 21:06:21 +02002791#endif
2792
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002793#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +02002794 // 'ttymouse'
2795 else if (varp == &p_ttym)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002796 errmsg = did_set_ttymouse();
Bram Moolenaardac13472019-09-16 21:06:21 +02002797#endif
2798
2799 // 'selection'
2800 else if (varp == &p_sel)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002801 errmsg = did_set_selection();
Bram Moolenaardac13472019-09-16 21:06:21 +02002802
2803 // 'selectmode'
2804 else if (varp == &p_slm)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002805 errmsg = did_set_opt_strings(p_slm, p_slm_values, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +02002806
2807#ifdef FEAT_BROWSE
2808 // 'browsedir'
2809 else if (varp == &p_bsdir)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002810 errmsg = did_set_browsedir();
Bram Moolenaardac13472019-09-16 21:06:21 +02002811#endif
2812
2813 // 'keymodel'
2814 else if (varp == &p_km)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002815 errmsg = did_set_keymodel();
Bram Moolenaardac13472019-09-16 21:06:21 +02002816
Bram Moolenaar63a2e362022-11-23 20:20:18 +00002817 // 'keyprotocol'
2818 else if (varp == &p_kpc)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002819 errmsg = did_set_keyprotocol();
Bram Moolenaar63a2e362022-11-23 20:20:18 +00002820
Bram Moolenaardac13472019-09-16 21:06:21 +02002821 // 'mousemodel'
2822 else if (varp == &p_mousem)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002823 errmsg = did_set_mousemodel();
Bram Moolenaardac13472019-09-16 21:06:21 +02002824
2825 // 'switchbuf'
2826 else if (varp == &p_swb)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002827 errmsg = did_set_opt_flags(p_swb, p_swb_values, &swb_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +02002828
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002829 // 'splitkeep'
2830 else if (varp == &p_spk)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002831 errmsg = did_set_opt_strings(p_spk, p_spk_values, FALSE);
Luuk van Baal13ece2a2022-10-03 15:28:08 +01002832
Bram Moolenaardac13472019-09-16 21:06:21 +02002833 // 'debug'
2834 else if (varp == &p_debug)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002835 errmsg = did_set_opt_strings(p_debug, p_debug_values, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +02002836
2837 // 'display'
2838 else if (varp == &p_dy)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002839 errmsg = did_set_display();
Bram Moolenaardac13472019-09-16 21:06:21 +02002840
2841 // 'eadirection'
2842 else if (varp == &p_ead)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002843 errmsg = did_set_opt_strings(p_ead, p_ead_values, FALSE);
Bram Moolenaardac13472019-09-16 21:06:21 +02002844
2845#ifdef FEAT_CLIPBOARD
2846 // 'clipboard'
2847 else if (varp == &p_cb)
2848 errmsg = check_clipboard_option();
2849#endif
2850
2851#ifdef FEAT_SPELL
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002852 // 'spellfile'
2853 else if (varp == &(curwin->w_s->b_p_spf))
2854 errmsg = did_set_spellfile(varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02002855
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002856 // 'spell'
2857 else if (varp == &(curwin->w_s->b_p_spl))
2858 errmsg = did_set_spell(varp);
2859
2860 // 'spellcapcheck'
Bram Moolenaardac13472019-09-16 21:06:21 +02002861 else if (varp == &(curwin->w_s->b_p_spc))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002862 errmsg = did_set_spellcapcheck();
2863
Bram Moolenaar362b44b2020-06-10 21:47:00 +02002864 // 'spelloptions'
2865 else if (varp == &(curwin->w_s->b_p_spo))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002866 errmsg = did_set_spelloptions(varp);
2867
Bram Moolenaardac13472019-09-16 21:06:21 +02002868 // 'spellsuggest'
2869 else if (varp == &p_sps)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002870 errmsg = did_set_spellsuggest();
2871
Bram Moolenaardac13472019-09-16 21:06:21 +02002872 // 'mkspellmem'
2873 else if (varp == &p_msm)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002874 errmsg = did_set_mkspellmem();
Bram Moolenaardac13472019-09-16 21:06:21 +02002875#endif
2876
2877 // When 'bufhidden' is set, check for valid value.
2878 else if (gvarp == &p_bh)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002879 errmsg = did_set_opt_strings(curbuf->b_p_bh, p_bufhidden_values,
2880 FALSE);
Bram Moolenaardac13472019-09-16 21:06:21 +02002881
2882 // When 'buftype' is set, check for valid value.
2883 else if (gvarp == &p_bt)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002884 errmsg = did_set_buftype();
Bram Moolenaardac13472019-09-16 21:06:21 +02002885
2886#ifdef FEAT_STL_OPT
zeertzjq5dc294a2022-04-15 13:17:57 +01002887 // 'statusline', 'tabline' or 'rulerformat'
2888 else if (gvarp == &p_stl || varp == &p_tal || varp == &p_ruf)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002889 errmsg = did_set_statusline(varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02002890#endif
2891
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002892 // 'complete'
Bram Moolenaardac13472019-09-16 21:06:21 +02002893 else if (gvarp == &p_cpt)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002894 errmsg = did_set_complete(varp, errbuf);
Bram Moolenaardac13472019-09-16 21:06:21 +02002895
2896 // 'completeopt'
2897 else if (varp == &p_cot)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002898 errmsg = did_set_completeopt();
Bram Moolenaardac13472019-09-16 21:06:21 +02002899
2900#ifdef BACKSLASH_IN_FILENAME
2901 // 'completeslash'
2902 else if (gvarp == &p_csl)
2903 {
2904 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
2905 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002906 errmsg = e_invalid_argument;
Bram Moolenaardac13472019-09-16 21:06:21 +02002907 }
2908#endif
2909
2910#ifdef FEAT_SIGNS
2911 // 'signcolumn'
2912 else if (varp == &curwin->w_p_scl)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002913 errmsg = did_set_signcolumn(varp, oldval);
Bram Moolenaardac13472019-09-16 21:06:21 +02002914#endif
2915
Luuk van Baalba936f62022-12-15 13:15:39 +00002916 // 'showcmdloc'
2917 else if (varp == &p_sloc)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002918 errmsg = did_set_opt_strings(p_sloc, p_sloc_values, FALSE);
Bram Moolenaardac13472019-09-16 21:06:21 +02002919
2920#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
2921 // 'toolbar'
2922 else if (varp == &p_toolbar)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002923 errmsg = did_set_toolbar();
Bram Moolenaardac13472019-09-16 21:06:21 +02002924#endif
2925
2926#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
2927 // 'toolbariconsize': GTK+ 2 only
2928 else if (varp == &p_tbis)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002929 errmsg = did_set_toolbariconsize();
Bram Moolenaardac13472019-09-16 21:06:21 +02002930#endif
2931
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002932 // 'pastetoggle'
Bram Moolenaardac13472019-09-16 21:06:21 +02002933 else if (varp == &p_pt)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002934 did_set_pastetoggle();
Bram Moolenaardac13472019-09-16 21:06:21 +02002935
2936 // 'backspace'
2937 else if (varp == &p_bs)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002938 errmsg = did_set_backspace();
2939
2940 // 'belloff'
Bram Moolenaardac13472019-09-16 21:06:21 +02002941 else if (varp == &p_bo)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002942 errmsg = did_set_opt_flags(p_bo, p_bo_values, &bo_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +02002943
2944 // 'tagcase'
2945 else if (gvarp == &p_tc)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002946 errmsg = did_set_tagcase(opt_flags);
Bram Moolenaardac13472019-09-16 21:06:21 +02002947
2948 // 'casemap'
2949 else if (varp == &p_cmp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002950 errmsg = did_set_opt_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +02002951
2952#ifdef FEAT_DIFF
2953 // 'diffopt'
2954 else if (varp == &p_dip)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002955 errmsg = did_set_diffopt();
Bram Moolenaardac13472019-09-16 21:06:21 +02002956#endif
2957
2958#ifdef FEAT_FOLDING
2959 // 'foldmethod'
2960 else if (gvarp == &curwin->w_allbuf_opt.wo_fdm)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002961 errmsg = did_set_foldmethod(varp);
2962
Bram Moolenaardac13472019-09-16 21:06:21 +02002963 // 'foldmarker'
2964 else if (gvarp == &curwin->w_allbuf_opt.wo_fmr)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002965 errmsg = did_set_foldmarker(varp);
2966
Bram Moolenaardac13472019-09-16 21:06:21 +02002967 // 'commentstring'
2968 else if (gvarp == &p_cms)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002969 errmsg = did_set_commentstring(varp);
2970
Bram Moolenaardac13472019-09-16 21:06:21 +02002971 // 'foldopen'
2972 else if (varp == &p_fdo)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002973 errmsg = did_set_opt_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
2974
Bram Moolenaardac13472019-09-16 21:06:21 +02002975 // 'foldclose'
2976 else if (varp == &p_fcl)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002977 errmsg = did_set_opt_strings(p_fcl, p_fcl_values, TRUE);
2978
Bram Moolenaardac13472019-09-16 21:06:21 +02002979 // 'foldignore'
2980 else if (gvarp == &curwin->w_allbuf_opt.wo_fdi)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002981 did_set_foldignore();
Bram Moolenaardac13472019-09-16 21:06:21 +02002982#endif
2983
2984 // 'virtualedit'
Gary Johnson53ba05b2021-07-26 22:19:10 +02002985 else if (gvarp == &p_ve)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002986 errmsg = did_set_virtualedit(oldval, opt_flags);
Bram Moolenaardac13472019-09-16 21:06:21 +02002987
2988#if defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002989 // 'cscopequickfix'
Bram Moolenaardac13472019-09-16 21:06:21 +02002990 else if (varp == &p_csqf)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002991 errmsg = did_set_cscopequickfix();
Bram Moolenaardac13472019-09-16 21:06:21 +02002992#endif
2993
Bram Moolenaardac13472019-09-16 21:06:21 +02002994 // 'cinoptions'
2995 else if (gvarp == &p_cino)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002996 did_set_cinoptions();
Bram Moolenaardac13472019-09-16 21:06:21 +02002997
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002998 // 'lispoptions'
2999 else if (gvarp == &p_lop)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003000 errmsg = did_set_lispoptions(varp);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01003001
Bram Moolenaardac13472019-09-16 21:06:21 +02003002#if defined(FEAT_RENDER_OPTIONS)
3003 // 'renderoptions'
3004 else if (varp == &p_rop)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003005 errmsg = did_set_renderoptions();
Bram Moolenaardac13472019-09-16 21:06:21 +02003006#endif
3007
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003008 // 'filetype'
Bram Moolenaardac13472019-09-16 21:06:21 +02003009 else if (gvarp == &p_ft)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003010 errmsg = did_set_filetype_or_syntax(varp, oldval, value_checked,
3011 &value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02003012
3013#ifdef FEAT_SYN_HL
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003014 // 'syntax'
Bram Moolenaardac13472019-09-16 21:06:21 +02003015 else if (gvarp == &p_syn)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003016 errmsg = did_set_filetype_or_syntax(varp, oldval, value_checked,
3017 &value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02003018#endif
3019
3020#ifdef FEAT_TERMINAL
3021 // 'termwinkey'
3022 else if (varp == &curwin->w_p_twk)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003023 errmsg = did_set_termwinkey();
3024
Bram Moolenaardac13472019-09-16 21:06:21 +02003025 // 'termwinsize'
3026 else if (varp == &curwin->w_p_tws)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003027 errmsg = did_set_termwinsize();
3028
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003029 // 'wincolor'
3030 else if (varp == &curwin->w_p_wcr)
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003031 term_update_wincolor(curwin);
Bram Moolenaardac13472019-09-16 21:06:21 +02003032# if defined(MSWIN)
3033 // 'termwintype'
3034 else if (varp == &p_twt)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003035 errmsg = did_set_opt_strings(p_twt, p_twt_values, FALSE);
Bram Moolenaardac13472019-09-16 21:06:21 +02003036# endif
3037#endif
3038
3039#ifdef FEAT_VARTABS
3040 // 'varsofttabstop'
3041 else if (varp == &(curbuf->b_p_vsts))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003042 errmsg = did_set_varsofttabstop(varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02003043
3044 // 'vartabstop'
3045 else if (varp == &(curbuf->b_p_vts))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003046 errmsg = did_set_vartabstop(varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02003047#endif
3048
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003049#ifdef FEAT_PROP_POPUP
Bram Moolenaardac13472019-09-16 21:06:21 +02003050 // 'previewpopup'
3051 else if (varp == &p_pvp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003052 errmsg = did_set_previewpopup();
Bram Moolenaardac13472019-09-16 21:06:21 +02003053# ifdef FEAT_QUICKFIX
3054 // 'completepopup'
3055 else if (varp == &p_cpp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003056 errmsg = did_set_completepopup();
Bram Moolenaardac13472019-09-16 21:06:21 +02003057# endif
3058#endif
3059
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00003060#ifdef FEAT_EVAL
3061 // '*expr' options
3062 else if (
3063# ifdef FEAT_BEVAL
3064 varp == &p_bexpr ||
3065# endif
3066# ifdef FEAT_DIFF
3067 varp == &p_dex ||
3068# endif
3069# ifdef FEAT_FOLDING
zeertzjq01d4efe2023-01-25 15:31:28 +00003070 gvarp == &curwin->w_allbuf_opt.wo_fde ||
3071 gvarp == &curwin->w_allbuf_opt.wo_fdt ||
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00003072# endif
3073 gvarp == &p_fex ||
3074# ifdef FEAT_FIND_ID
3075 gvarp == &p_inex ||
3076# endif
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00003077 gvarp == &p_inde ||
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00003078# ifdef FEAT_DIFF
3079 varp == &p_pex ||
3080# endif
3081# ifdef FEAT_POSTSCRIPT
3082 varp == &p_pexpr ||
3083# endif
Bram Moolenaarf4e88f22022-01-23 14:17:28 +00003084 varp == &p_ccv)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003085 did_set_optexpr(varp);
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00003086#endif
3087
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003088#ifdef FEAT_COMPL_FUNC
3089 // 'completefunc'
3090 else if (gvarp == &p_cfu)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003091 errmsg = set_completefunc_option();
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003092
3093 // 'omnifunc'
3094 else if (gvarp == &p_ofu)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003095 errmsg = set_omnifunc_option();
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003096
3097 // 'thesaurusfunc'
3098 else if (gvarp == &p_tsrfu)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003099 errmsg = set_thesaurusfunc_option();
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003100#endif
3101
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00003102#if defined(FEAT_EVAL) && \
3103 (defined(FEAT_XIM) || defined(IME_WITHOUT_XIM) || defined(VIMDLL))
3104 // 'imactivatefunc'
3105 else if (gvarp == &p_imaf)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003106 errmsg = set_imactivatefunc_option();
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00003107
3108 // 'imstatusfunc'
3109 else if (gvarp == &p_imsf)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003110 errmsg = set_imstatusfunc_option();
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00003111#endif
3112
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003113 // 'operatorfunc'
3114 else if (varp == &p_opfunc)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003115 errmsg = set_operatorfunc_option();
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003116
Bram Moolenaard43906d2020-07-20 21:31:32 +02003117#ifdef FEAT_QUICKFIX
Yegappan Lakshmanan777175b2021-11-18 22:08:57 +00003118 // 'quickfixtextfunc'
Bram Moolenaard43906d2020-07-20 21:31:32 +02003119 else if (varp == &p_qftf)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003120 errmsg = qf_process_qftf_option();
Bram Moolenaard43906d2020-07-20 21:31:32 +02003121#endif
3122
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00003123#ifdef FEAT_EVAL
3124 // 'tagfunc'
3125 else if (gvarp == &p_tfu)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003126 errmsg = set_tagfunc_option();
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00003127#endif
3128
Bram Moolenaardac13472019-09-16 21:06:21 +02003129 // Options that are a list of flags.
3130 else
3131 {
3132 p = NULL;
3133 if (varp == &p_ww) // 'whichwrap'
3134 p = (char_u *)WW_ALL;
3135 if (varp == &p_shm) // 'shortmess'
3136 p = (char_u *)SHM_ALL;
3137 else if (varp == &(p_cpo)) // 'cpoptions'
3138 p = (char_u *)CPO_ALL;
3139 else if (varp == &(curbuf->b_p_fo)) // 'formatoptions'
3140 p = (char_u *)FO_ALL;
3141#ifdef FEAT_CONCEAL
3142 else if (varp == &curwin->w_p_cocu) // 'concealcursor'
3143 p = (char_u *)COCU_ALL;
3144#endif
3145 else if (varp == &p_mouse) // 'mouse'
3146 {
Bram Moolenaardac13472019-09-16 21:06:21 +02003147 p = (char_u *)MOUSE_ALL;
Bram Moolenaardac13472019-09-16 21:06:21 +02003148 }
3149#if defined(FEAT_GUI)
3150 else if (varp == &p_go) // 'guioptions'
3151 p = (char_u *)GO_ALL;
3152#endif
3153 if (p != NULL)
3154 {
3155 for (s = *varp; *s; ++s)
3156 if (vim_strchr(p, *s) == NULL)
3157 {
3158 errmsg = illegal_char(errbuf, *s);
3159 break;
3160 }
3161 }
3162 }
3163
3164 // If error detected, restore the previous value.
3165 if (errmsg != NULL)
3166 {
zeertzjqf6782732022-07-27 18:26:03 +01003167 free_string_option(*varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02003168 *varp = oldval;
3169 // When resetting some values, need to act on it.
3170 if (did_chartab)
3171 (void)init_chartab();
3172 if (varp == &p_hl)
3173 (void)highlight_changed();
3174 }
3175 else
3176 {
3177#ifdef FEAT_EVAL
3178 // Remember where the option was set.
3179 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
3180#endif
3181 // Free string options that are in allocated memory.
3182 // Use "free_oldval", because recursiveness may change the flags under
3183 // our fingers (esp. init_highlight()).
3184 if (free_oldval)
3185 free_string_option(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01003186 set_option_flag(opt_idx, P_ALLOCED);
Bram Moolenaardac13472019-09-16 21:06:21 +02003187
3188 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
3189 && is_global_local_option(opt_idx))
3190 {
3191 // global option with local value set to use global value; free
3192 // the local value and make it empty
3193 p = get_option_varp_scope(opt_idx, OPT_LOCAL);
3194 free_string_option(*(char_u **)p);
3195 *(char_u **)p = empty_option;
3196 }
3197
3198 // May set global value for local option.
3199 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
3200 set_string_option_global(opt_idx, varp);
3201
3202 // Trigger the autocommand only after setting the flags.
3203#ifdef FEAT_SYN_HL
3204 // When 'syntax' is set, load the syntax of that name
3205 if (varp == &(curbuf->b_p_syn))
3206 {
3207 static int syn_recursive = 0;
3208
3209 ++syn_recursive;
3210 // Only pass TRUE for "force" when the value changed or not used
3211 // recursively, to avoid endless recurrence.
3212 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
3213 value_changed || syn_recursive == 1, curbuf);
3214 curbuf->b_flags |= BF_SYN_SET;
3215 --syn_recursive;
3216 }
3217#endif
3218 else if (varp == &(curbuf->b_p_ft))
3219 {
3220 // 'filetype' is set, trigger the FileType autocommand.
3221 // Skip this when called from a modeline and the filetype was
3222 // already set to this value.
3223 if (!(opt_flags & OPT_MODELINE) || value_changed)
3224 {
3225 static int ft_recursive = 0;
3226 int secure_save = secure;
3227
3228 // Reset the secure flag, since the value of 'filetype' has
3229 // been checked to be safe.
3230 secure = 0;
3231
3232 ++ft_recursive;
3233 did_filetype = TRUE;
3234 // Only pass TRUE for "force" when the value changed or not
3235 // used recursively, to avoid endless recurrence.
3236 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
3237 value_changed || ft_recursive == 1, curbuf);
3238 --ft_recursive;
3239 // Just in case the old "curbuf" is now invalid.
3240 if (varp != &(curbuf->b_p_ft))
3241 varp = NULL;
3242
3243 secure = secure_save;
3244 }
3245 }
3246#ifdef FEAT_SPELL
3247 if (varp == &(curwin->w_s->b_p_spl))
3248 {
3249 char_u fname[200];
3250 char_u *q = curwin->w_s->b_p_spl;
3251
3252 // Skip the first name if it is "cjk".
3253 if (STRNCMP(q, "cjk,", 4) == 0)
3254 q += 4;
3255
3256 // Source the spell/LANG.vim in 'runtimepath'.
3257 // They could set 'spellcapcheck' depending on the language.
3258 // Use the first name in 'spelllang' up to '_region' or
3259 // '.encoding'.
3260 for (p = q; *p != NUL; ++p)
3261 if (!ASCII_ISALNUM(*p) && *p != '-')
3262 break;
3263 if (p > q)
3264 {
3265 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
3266 (int)(p - q), q);
3267 source_runtime(fname, DIP_ALL);
3268 }
3269 }
3270#endif
3271 }
3272
Bram Moolenaardac13472019-09-16 21:06:21 +02003273 if (varp == &p_mouse)
3274 {
Bram Moolenaardac13472019-09-16 21:06:21 +02003275 if (*p_mouse == NUL)
3276 mch_setmouse(FALSE); // switch mouse off
3277 else
Bram Moolenaardac13472019-09-16 21:06:21 +02003278 setmouse(); // in case 'mouse' changed
3279 }
Bram Moolenaardac13472019-09-16 21:06:21 +02003280
Bram Moolenaar788fbb42020-05-31 14:08:12 +02003281#if defined(FEAT_LUA) || defined(PROTO)
3282 if (varp == &p_rtp)
3283 update_package_paths_in_lua();
3284#endif
3285
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00003286#if defined(FEAT_LINEBREAK)
3287 // Changing Formatlistpattern when briopt includes the list setting:
3288 // redraw
3289 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
3290 && curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003291 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00003292#endif
3293
Bram Moolenaardac13472019-09-16 21:06:21 +02003294 if (curwin->w_curswant != MAXCOL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02003295 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02003296 curwin->w_set_curswant = TRUE;
3297
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003298 if ((opt_flags & OPT_NO_REDRAW) == 0)
3299 {
Bram Moolenaardac13472019-09-16 21:06:21 +02003300#ifdef FEAT_GUI
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003301 // check redraw when it's not a GUI option or the GUI is active.
3302 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02003303#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01003304 check_redraw(get_option_flags(opt_idx));
3305 }
Bram Moolenaardac13472019-09-16 21:06:21 +02003306
3307#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3308 if (did_swaptcap)
3309 {
3310 set_termname((char_u *)"win32");
3311 init_highlight(TRUE, FALSE);
3312 }
3313#endif
3314
3315 return errmsg;
3316}
3317
3318/*
3319 * Check an option that can be a range of string values.
3320 *
3321 * Return OK for correct value, FAIL otherwise.
3322 * Empty is always OK.
3323 */
3324 static int
3325check_opt_strings(
3326 char_u *val,
3327 char **values,
3328 int list) // when TRUE: accept a list of values
3329{
3330 return opt_strings_flags(val, values, NULL, list);
3331}
3332
3333/*
3334 * Handle an option that can be a range of string values.
3335 * Set a flag in "*flagp" for each string present.
3336 *
3337 * Return OK for correct value, FAIL otherwise.
3338 * Empty is always OK.
3339 */
3340 static int
3341opt_strings_flags(
3342 char_u *val, // new value
3343 char **values, // array of valid string values
3344 unsigned *flagp,
3345 int list) // when TRUE: accept a list of values
3346{
3347 int i;
3348 int len;
3349 unsigned new_flags = 0;
3350
3351 while (*val)
3352 {
3353 for (i = 0; ; ++i)
3354 {
3355 if (values[i] == NULL) // val not found in values[]
3356 return FAIL;
3357
3358 len = (int)STRLEN(values[i]);
3359 if (STRNCMP(values[i], val, len) == 0
3360 && ((list && val[len] == ',') || val[len] == NUL))
3361 {
3362 val += len + (val[len] == ',');
3363 new_flags |= (1 << i);
3364 break; // check next item in val list
3365 }
3366 }
3367 }
3368 if (flagp != NULL)
3369 *flagp = new_flags;
3370
3371 return OK;
3372}
3373
3374/*
3375 * return OK if "p" is a valid fileformat name, FAIL otherwise.
3376 */
3377 int
3378check_ff_value(char_u *p)
3379{
3380 return check_opt_strings(p, p_ff_values, FALSE);
3381}
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00003382
3383/*
3384 * Save the acutal shortmess Flags and clear them
3385 * temporarily to avoid that file messages
3386 * overwrites any output from the following commands.
3387 *
3388 * Caller must make sure to first call save_clear_shm_value() and then
3389 * restore_shm_value() exactly the same number of times.
3390 */
3391 void
3392save_clear_shm_value()
3393{
3394 if (STRLEN(p_shm) >= SHM_LEN)
3395 {
3396 iemsg(e_internal_error_shortmess_too_long);
3397 return;
3398 }
3399
3400 if (++set_shm_recursive == 1)
3401 {
3402 STRCPY(shm_buf, p_shm);
3403 set_option_value_give_err((char_u *)"shm", 0L, (char_u *)"", 0);
3404 }
3405}
3406
3407/*
3408 * Restore the shortmess Flags set from the save_clear_shm_value() function.
3409 */
3410 void
3411restore_shm_value()
3412{
3413 if (--set_shm_recursive == 0)
3414 {
3415 set_option_value_give_err((char_u *)"shm", 0L, shm_buf, 0);
3416 vim_memset(shm_buf, 0, SHM_LEN);
3417 }
3418}