blob: 202e93e57db0e1990270da5e1a1270b59ea22ddc [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};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020027#if defined(FEAT_LINEBREAK)
28// Note: Keep this in sync with briopt_check()
29static char *(p_briopt_values[]) = {"shift:", "min:", "sbr", "list:", "column:", NULL};
30#endif
31#if defined(FEAT_DIFF)
32// Note: Keep this in sync with diffopt_changed()
33static char *(p_dip_values[]) = {"filler", "context:", "iblank", "icase", "iwhite", "iwhiteall", "iwhiteeol", "horizontal", "vertical", "closeoff", "hiddenoff", "foldcolumn:", "followwrap", "internal", "indent-heuristic", "algorithm:", NULL};
34static char *(p_dip_algorithm_values[]) = {"myers", "minimal", "patience", "histogram", NULL};
35#endif
Bram Moolenaaraaad9952020-05-31 15:08:59 +020036static char *(p_nf_values[]) = {"bin", "octal", "hex", "alpha", "unsigned", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020037static char *(p_ff_values[]) = {FF_UNIX, FF_DOS, FF_MAC, NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020038#ifdef FEAT_CLIPBOARD
39// Note: Keep this in sync with did_set_clipboard()
40static char *(p_cb_values[]) = {"unnamed", "unnamedplus", "autoselect", "autoselectplus", "autoselectml", "html", "exclude:", NULL};
41#endif
Bram Moolenaardac13472019-09-16 21:06:21 +020042#ifdef FEAT_CRYPT
Christian Brabandtf573c6e2021-06-20 14:02:16 +020043static char *(p_cm_values[]) = {"zip", "blowfish", "blowfish2",
44 # ifdef FEAT_SODIUM
Christian Brabandtaae58342023-04-23 17:50:22 +010045 "xchacha20", "xchacha20v2",
Christian Brabandtf573c6e2021-06-20 14:02:16 +020046 # endif
47 NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020048#endif
49static char *(p_cmp_values[]) = {"internal", "keepascii", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020050#ifdef FEAT_SYN_HL
51// Note: Keep this in sync with fill_culopt_flags()
52static char *(p_culopt_values[]) = {"line", "screenline", "number", "both", NULL};
53#endif
Bram Moolenaardac13472019-09-16 21:06:21 +020054static char *(p_dy_values[]) = {"lastline", "truncate", "uhex", NULL};
Yegappan Lakshmanan87018252023-09-20 20:20:04 +020055static char *(p_jop_values[]) = {"stack", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020056#ifdef FEAT_FOLDING
57static char *(p_fdo_values[]) = {"all", "block", "hor", "mark", "percent",
58 "quickfix", "search", "tag", "insert",
59 "undo", "jump", NULL};
60#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +020061// Note: Keep this in sync with match_keyprotocol()
62static char *(p_kpc_protocol_values[]) = {"none", "mok2", "kitty", NULL};
63#ifdef FEAT_PROP_POPUP
64// Note: Keep this in sync with parse_popup_option()
65static char *(p_popup_option_values[]) = {"height:", "width:", "highlight:", "border:", "align:", NULL};
66static char *(p_popup_option_border_values[]) = {"on", "off", NULL};
67static char *(p_popup_option_align_values[]) = {"item", "menu", NULL};
68#endif
69#if defined(FEAT_SPELL)
70// Note: Keep this in sync with spell_check_sps()
71static char *(p_sps_values[]) = {"best", "fast", "double", "expr:", "file:", "timeout:", NULL};
72#endif
Bram Moolenaardac13472019-09-16 21:06:21 +020073#ifdef FEAT_SESSION
Bram Moolenaar635bd602021-04-16 19:58:22 +020074// Also used for 'viewoptions'! Keep in sync with SSOP_ flags.
Bram Moolenaardac13472019-09-16 21:06:21 +020075static char *(p_ssop_values[]) = {"buffers", "winpos", "resize", "winsize",
76 "localoptions", "options", "help", "blank", "globals", "slash", "unix",
Bram Moolenaar635bd602021-04-16 19:58:22 +020077 "sesdir", "curdir", "folds", "cursor", "tabpages", "terminal", "skiprtp",
78 NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020079#endif
Bram Moolenaar539aa6b2019-11-17 18:09:38 +010080// Keep in sync with SWB_ flags in option.h
81static char *(p_swb_values[]) = {"useopen", "usetab", "split", "newtab", "vsplit", "uselast", NULL};
Luuk van Baal13ece2a2022-10-03 15:28:08 +010082static char *(p_spk_values[]) = {"cursor", "screen", "topline", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020083static char *(p_tc_values[]) = {"followic", "ignore", "match", "followscs", "smart", NULL};
84#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
85static char *(p_toolbar_values[]) = {"text", "icons", "tooltips", "horiz", NULL};
86#endif
87#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
88static char *(p_tbis_values[]) = {"tiny", "small", "medium", "large", "huge", "giant", NULL};
89#endif
Bram Moolenaara1cb1d12019-10-17 23:00:07 +020090#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +020091static char *(p_ttym_values[]) = {"xterm", "xterm2", "dec", "netterm", "jsbterm", "pterm", "urxvt", "sgr", NULL};
92#endif
Gary Johnson53ba05b2021-07-26 22:19:10 +020093static char *(p_ve_values[]) = {"block", "insert", "all", "onemore", "none", "NONE", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020094// Note: Keep this in sync with check_opt_wim()
95static char *(p_wim_values[]) = {"full", "longest", "list", "lastused", NULL};
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000096static char *(p_wop_values[]) = {"fuzzy", "tagfile", "pum", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020097#ifdef FEAT_WAK
98static char *(p_wak_values[]) = {"yes", "menu", "no", NULL};
99#endif
100static char *(p_mousem_values[]) = {"extend", "popup", "popup_setpos", "mac", NULL};
101static char *(p_sel_values[]) = {"inclusive", "exclusive", "old", NULL};
102static char *(p_slm_values[]) = {"mouse", "key", "cmd", NULL};
103static char *(p_km_values[]) = {"startsel", "stopsel", NULL};
104#ifdef FEAT_BROWSE
105static char *(p_bsdir_values[]) = {"current", "last", "buffer", NULL};
106#endif
107static char *(p_scbopt_values[]) = {"ver", "hor", "jump", NULL};
108static char *(p_debug_values[]) = {"msg", "throw", "beep", NULL};
109static char *(p_ead_values[]) = {"both", "ver", "hor", NULL};
110static char *(p_buftype_values[]) = {"nofile", "nowrite", "quickfix", "help", "terminal", "acwrite", "prompt", "popup", NULL};
111static char *(p_bufhidden_values[]) = {"hide", "unload", "delete", "wipe", NULL};
Bram Moolenaaraa0489e2020-04-17 19:41:21 +0200112static char *(p_bs_values[]) = {"indent", "eol", "start", "nostop", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200113#ifdef FEAT_FOLDING
114static char *(p_fdm_values[]) = {"manual", "expr", "marker", "indent", "syntax",
115# ifdef FEAT_DIFF
116 "diff",
117# endif
118 NULL};
119static char *(p_fcl_values[]) = {"all", NULL};
120#endif
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200121static char *(p_cot_values[]) = {"menu", "menuone", "longest", "preview", "popup", "popuphidden", "noinsert", "noselect", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200122#ifdef BACKSLASH_IN_FILENAME
123static char *(p_csl_values[]) = {"slash", "backslash", NULL};
124#endif
125#ifdef FEAT_SIGNS
126static char *(p_scl_values[]) = {"yes", "no", "auto", "number", NULL};
127#endif
128#if defined(MSWIN) && defined(FEAT_TERMINAL)
129static char *(p_twt_values[]) = {"winpty", "conpty", "", NULL};
130#endif
Luuk van Baalba936f62022-12-15 13:15:39 +0000131static char *(p_sloc_values[]) = {"last", "statusline", "tabline", NULL};
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +0000132static char *(p_sws_values[]) = {"fsync", "sync", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200133
134static int check_opt_strings(char_u *val, char **values, int list);
135static int opt_strings_flags(char_u *val, char **values, unsigned *flagp, int list);
136
137/*
138 * After setting various option values: recompute variables that depend on
139 * option values.
140 */
141 void
142didset_string_options(void)
143{
144 (void)opt_strings_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
145 (void)opt_strings_flags(p_bkc, p_bkc_values, &bkc_flags, TRUE);
146 (void)opt_strings_flags(p_bo, p_bo_values, &bo_flags, TRUE);
147#ifdef FEAT_SESSION
148 (void)opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE);
149 (void)opt_strings_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
150#endif
151#ifdef FEAT_FOLDING
152 (void)opt_strings_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
153#endif
154 (void)opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE);
Yegappan Lakshmanan87018252023-09-20 20:20:04 +0200155 (void)opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200156 (void)opt_strings_flags(p_tc, p_tc_values, &tc_flags, FALSE);
157 (void)opt_strings_flags(p_ve, p_ve_values, &ve_flags, TRUE);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +0200158#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +0200159 (void)opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE);
160#endif
161#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
162 (void)opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags, TRUE);
163#endif
164#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
165 (void)opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE);
166#endif
Sean Dewar39c46b42022-05-12 17:44:29 +0100167 (void)opt_strings_flags(p_swb, p_swb_values, &swb_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200168}
169
Yegappan Lakshmananf9dc2782023-05-11 15:02:56 +0100170#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200171/*
172 * Trigger the OptionSet autocommand.
173 * "opt_idx" is the index of the option being set.
174 * "opt_flags" can be OPT_LOCAL etc.
175 * "oldval" the old value
176 * "oldval_l" the old local value (only non-NULL if global and local value
177 * are set)
178 * "oldval_g" the old global value (only non-NULL if global and local value
179 * are set)
180 * "newval" the new value
181 */
182 void
zeertzjq269aa2b2022-11-28 11:36:50 +0000183trigger_optionset_string(
Bram Moolenaardac13472019-09-16 21:06:21 +0200184 int opt_idx,
185 int opt_flags,
186 char_u *oldval,
187 char_u *oldval_l,
188 char_u *oldval_g,
189 char_u *newval)
190{
191 // Don't do this recursively.
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000192 if (oldval == NULL || newval == NULL
193 || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
194 return;
Bram Moolenaardac13472019-09-16 21:06:21 +0200195
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000196 char_u buf_type[7];
197
198 sprintf((char *)buf_type, "%s",
Bram Moolenaardac13472019-09-16 21:06:21 +0200199 (opt_flags & OPT_LOCAL) ? "local" : "global");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000200 set_vim_var_string(VV_OPTION_OLD, oldval, -1);
201 set_vim_var_string(VV_OPTION_NEW, newval, -1);
202 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
203 if (opt_flags & OPT_LOCAL)
204 {
205 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
206 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
Bram Moolenaardac13472019-09-16 21:06:21 +0200207 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000208 if (opt_flags & OPT_GLOBAL)
209 {
210 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
211 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval, -1);
212 }
213 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
214 {
215 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
216 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval_l, -1);
217 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval_g, -1);
218 }
219 if (opt_flags & OPT_MODELINE)
220 {
221 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
222 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
223 }
224 apply_autocmds(EVENT_OPTIONSET,
225 get_option_fullname(opt_idx), NULL, FALSE,
226 NULL);
227 reset_v_option_vars();
Bram Moolenaardac13472019-09-16 21:06:21 +0200228}
229#endif
230
231 static char *
232illegal_char(char *errbuf, int c)
233{
234 if (errbuf == NULL)
235 return "";
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000236 sprintf((char *)errbuf, _(e_illegal_character_str), (char *)transchar(c));
Bram Moolenaardac13472019-09-16 21:06:21 +0200237 return errbuf;
238}
239
240/*
241 * Check string options in a buffer for NULL value.
242 */
243 void
244check_buf_options(buf_T *buf)
245{
246 check_string_option(&buf->b_p_bh);
247 check_string_option(&buf->b_p_bt);
248 check_string_option(&buf->b_p_fenc);
249 check_string_option(&buf->b_p_ff);
250#ifdef FEAT_FIND_ID
251 check_string_option(&buf->b_p_def);
252 check_string_option(&buf->b_p_inc);
253# ifdef FEAT_EVAL
254 check_string_option(&buf->b_p_inex);
255# endif
256#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +0100257#if defined(FEAT_EVAL)
Bram Moolenaardac13472019-09-16 21:06:21 +0200258 check_string_option(&buf->b_p_inde);
259 check_string_option(&buf->b_p_indk);
260#endif
261#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
262 check_string_option(&buf->b_p_bexpr);
263#endif
264#if defined(FEAT_CRYPT)
265 check_string_option(&buf->b_p_cm);
266#endif
267 check_string_option(&buf->b_p_fp);
268#if defined(FEAT_EVAL)
269 check_string_option(&buf->b_p_fex);
270#endif
271#ifdef FEAT_CRYPT
272 check_string_option(&buf->b_p_key);
273#endif
274 check_string_option(&buf->b_p_kp);
275 check_string_option(&buf->b_p_mps);
276 check_string_option(&buf->b_p_fo);
277 check_string_option(&buf->b_p_flp);
278 check_string_option(&buf->b_p_isk);
Bram Moolenaardac13472019-09-16 21:06:21 +0200279 check_string_option(&buf->b_p_com);
Bram Moolenaardac13472019-09-16 21:06:21 +0200280#ifdef FEAT_FOLDING
281 check_string_option(&buf->b_p_cms);
282#endif
283 check_string_option(&buf->b_p_nf);
Bram Moolenaardac13472019-09-16 21:06:21 +0200284 check_string_option(&buf->b_p_qe);
Bram Moolenaardac13472019-09-16 21:06:21 +0200285#ifdef FEAT_SYN_HL
286 check_string_option(&buf->b_p_syn);
287 check_string_option(&buf->b_s.b_syn_isk);
288#endif
289#ifdef FEAT_SPELL
290 check_string_option(&buf->b_s.b_p_spc);
291 check_string_option(&buf->b_s.b_p_spf);
292 check_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +0200293 check_string_option(&buf->b_s.b_p_spo);
Bram Moolenaardac13472019-09-16 21:06:21 +0200294#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200295 check_string_option(&buf->b_p_sua);
Bram Moolenaardac13472019-09-16 21:06:21 +0200296 check_string_option(&buf->b_p_cink);
297 check_string_option(&buf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +0100298 check_string_option(&buf->b_p_cinsd);
Bram Moolenaardac13472019-09-16 21:06:21 +0200299 parse_cino(buf);
Bram Moolenaar49846fb2022-10-15 16:05:33 +0100300 check_string_option(&buf->b_p_lop);
Bram Moolenaardac13472019-09-16 21:06:21 +0200301 check_string_option(&buf->b_p_ft);
Bram Moolenaardac13472019-09-16 21:06:21 +0200302 check_string_option(&buf->b_p_cinw);
Bram Moolenaardac13472019-09-16 21:06:21 +0200303 check_string_option(&buf->b_p_cpt);
304#ifdef FEAT_COMPL_FUNC
305 check_string_option(&buf->b_p_cfu);
306 check_string_option(&buf->b_p_ofu);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +0100307 check_string_option(&buf->b_p_tsrfu);
Bram Moolenaardac13472019-09-16 21:06:21 +0200308#endif
309#ifdef FEAT_EVAL
310 check_string_option(&buf->b_p_tfu);
311#endif
312#ifdef FEAT_KEYMAP
313 check_string_option(&buf->b_p_keymap);
314#endif
315#ifdef FEAT_QUICKFIX
316 check_string_option(&buf->b_p_gp);
317 check_string_option(&buf->b_p_mp);
318 check_string_option(&buf->b_p_efm);
319#endif
320 check_string_option(&buf->b_p_ep);
321 check_string_option(&buf->b_p_path);
322 check_string_option(&buf->b_p_tags);
323 check_string_option(&buf->b_p_tc);
324 check_string_option(&buf->b_p_dict);
325 check_string_option(&buf->b_p_tsr);
Bram Moolenaardac13472019-09-16 21:06:21 +0200326 check_string_option(&buf->b_p_lw);
Bram Moolenaardac13472019-09-16 21:06:21 +0200327 check_string_option(&buf->b_p_bkc);
328 check_string_option(&buf->b_p_menc);
329#ifdef FEAT_VARTABS
330 check_string_option(&buf->b_p_vsts);
331 check_string_option(&buf->b_p_vts);
332#endif
333}
334
335/*
336 * Free the string allocated for an option.
337 * Checks for the string being empty_option. This may happen if we're out of
338 * memory, vim_strsave() returned NULL, which was replaced by empty_option by
339 * check_options().
340 * Does NOT check for P_ALLOCED flag!
341 */
342 void
343free_string_option(char_u *p)
344{
345 if (p != empty_option)
346 vim_free(p);
347}
348
349 void
350clear_string_option(char_u **pp)
351{
352 if (*pp != empty_option)
353 vim_free(*pp);
354 *pp = empty_option;
355}
356
357 void
358check_string_option(char_u **pp)
359{
360 if (*pp == NULL)
361 *pp = empty_option;
362}
363
364/*
365 * Set global value for string option when it's a local option.
366 */
367 static void
368set_string_option_global(
369 int opt_idx, // option index
370 char_u **varp) // pointer to option variable
371{
372 char_u **p, *s;
373
374 // the global value is always allocated
375 if (is_window_local_option(opt_idx))
376 p = (char_u **)GLOBAL_WO(varp);
377 else
378 p = (char_u **)get_option_var(opt_idx);
379 if (!is_global_option(opt_idx)
380 && p != varp
381 && (s = vim_strsave(*varp)) != NULL)
382 {
383 free_string_option(*p);
384 *p = s;
385 }
386}
387
388/*
389 * Set a string option to a new value (without checking the effect).
390 * The string is copied into allocated memory.
391 * if ("opt_idx" == -1) "name" is used, otherwise "opt_idx" is used.
392 * When "set_sid" is zero set the scriptID to current_sctx.sc_sid. When
393 * "set_sid" is SID_NONE don't set the scriptID. Otherwise set the scriptID to
394 * "set_sid".
395 */
396 void
397set_string_option_direct(
398 char_u *name,
399 int opt_idx,
400 char_u *val,
401 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
402 int set_sid UNUSED)
403{
404 char_u *s;
405 char_u **varp;
406 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
407 int idx = opt_idx;
408
409 if (idx == -1) // use name
410 {
411 idx = findoption(name);
412 if (idx < 0) // not found (should not happen)
413 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000414 semsg(_(e_internal_error_str), "set_string_option_direct()");
RestorerZ68ebcee2023-05-31 17:12:14 +0100415 siemsg("For option %s", name);
Bram Moolenaardac13472019-09-16 21:06:21 +0200416 return;
417 }
418 }
419
420 if (is_hidden_option(idx)) // can't set hidden option
421 return;
422
423 s = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000424 if (s == NULL)
425 return;
426
427 varp = (char_u **)get_option_varp_scope(idx,
428 both ? OPT_LOCAL : opt_flags);
429 if ((opt_flags & OPT_FREE) && (get_option_flags(idx) & P_ALLOCED))
430 free_string_option(*varp);
431 *varp = s;
432
433 // For buffer/window local option may also set the global value.
434 if (both)
435 set_string_option_global(idx, varp);
436
437 set_option_flag(idx, P_ALLOCED);
438
439 // When setting both values of a global option with a local value,
440 // make the local value empty, so that the global value is used.
441 if (is_global_local_option(idx) && both)
Bram Moolenaardac13472019-09-16 21:06:21 +0200442 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000443 free_string_option(*varp);
444 *varp = empty_option;
Bram Moolenaardac13472019-09-16 21:06:21 +0200445 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000446# ifdef FEAT_EVAL
447 if (set_sid != SID_NONE)
448 {
449 sctx_T script_ctx;
450
451 if (set_sid == 0)
452 script_ctx = current_sctx;
453 else
454 {
455 script_ctx.sc_sid = set_sid;
456 script_ctx.sc_seq = 0;
457 script_ctx.sc_lnum = 0;
458 script_ctx.sc_version = 1;
459 }
460 set_option_sctx_idx(idx, opt_flags, script_ctx);
461 }
462# endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200463}
464
Dominique Pellee764d1b2023-03-12 21:20:59 +0000465#if defined(FEAT_PROP_POPUP) || \
466 (defined(FEAT_DIFF) && defined(FEAT_FOLDING)) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200467/*
468 * Like set_string_option_direct(), but for a window-local option in "wp".
469 * Blocks autocommands to avoid the old curwin becoming invalid.
470 */
471 void
472set_string_option_direct_in_win(
473 win_T *wp,
474 char_u *name,
475 int opt_idx,
476 char_u *val,
477 int opt_flags,
478 int set_sid)
479{
480 win_T *save_curwin = curwin;
481
482 block_autocmds();
483 curwin = wp;
484 curbuf = curwin->w_buffer;
485 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
486 curwin = save_curwin;
487 curbuf = curwin->w_buffer;
488 unblock_autocmds();
489}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000490#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200491
Dominique Pelle748b3082022-01-08 12:41:16 +0000492#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200493/*
494 * Like set_string_option_direct(), but for a buffer-local option in "buf".
495 * Blocks autocommands to avoid the old curbuf becoming invalid.
496 */
497 void
498set_string_option_direct_in_buf(
499 buf_T *buf,
500 char_u *name,
501 int opt_idx,
502 char_u *val,
503 int opt_flags,
504 int set_sid)
505{
506 buf_T *save_curbuf = curbuf;
507
508 block_autocmds();
509 curbuf = buf;
510 curwin->w_buffer = curbuf;
511 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
512 curbuf = save_curbuf;
513 curwin->w_buffer = curbuf;
514 unblock_autocmds();
515}
Dominique Pelle748b3082022-01-08 12:41:16 +0000516#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200517
518/*
519 * Set a string option to a new value, and handle the effects.
520 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100521 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaardac13472019-09-16 21:06:21 +0200522 */
523 char *
524set_string_option(
525 int opt_idx,
526 char_u *value,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +0000527 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
528 char *errbuf)
Bram Moolenaardac13472019-09-16 21:06:21 +0200529{
530 char_u *s;
531 char_u **varp;
532 char_u *oldval;
533#if defined(FEAT_EVAL)
534 char_u *oldval_l = NULL;
535 char_u *oldval_g = NULL;
536 char_u *saved_oldval = NULL;
537 char_u *saved_oldval_l = NULL;
538 char_u *saved_oldval_g = NULL;
539 char_u *saved_newval = NULL;
540#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100541 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +0200542 int value_checked = FALSE;
543
544 if (is_hidden_option(opt_idx)) // don't set hidden option
545 return NULL;
546
Bram Moolenaar7f009df2020-03-16 20:27:38 +0100547 s = vim_strsave(value == NULL ? (char_u *)"" : value);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000548 if (s == NULL)
549 return NULL;
550
551 varp = (char_u **)get_option_varp_scope(opt_idx,
552 (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
553 ? (is_global_local_option(opt_idx)
554 ? OPT_GLOBAL : OPT_LOCAL)
555 : opt_flags);
556 oldval = *varp;
557#if defined(FEAT_EVAL)
558 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +0200559 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000560 oldval_l = *(char_u **)get_option_varp_scope(opt_idx, OPT_LOCAL);
561 oldval_g = *(char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
Bram Moolenaardac13472019-09-16 21:06:21 +0200562 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000563#endif
564 *varp = s;
565
566#if defined(FEAT_EVAL)
567 if (!starting
568# ifdef FEAT_CRYPT
569 && !is_crypt_key_option(opt_idx)
570# endif
571 )
572 {
573 if (oldval_l != NULL)
574 saved_oldval_l = vim_strsave(oldval_l);
575 if (oldval_g != NULL)
576 saved_oldval_g = vim_strsave(oldval_g);
577 saved_oldval = vim_strsave(oldval);
578 saved_newval = vim_strsave(s);
579 }
580#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000581 if ((errmsg = did_set_string_option(opt_idx, varp, oldval, value, errbuf,
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +0200582 opt_flags, OP_NONE, &value_checked)) == NULL)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000583 did_set_option(opt_idx, opt_flags, TRUE, value_checked);
584
585#if defined(FEAT_EVAL)
586 // call autocommand after handling side effects
587 if (errmsg == NULL)
588 trigger_optionset_string(opt_idx, opt_flags,
589 saved_oldval, saved_oldval_l,
590 saved_oldval_g, saved_newval);
591 vim_free(saved_oldval);
592 vim_free(saved_oldval_l);
593 vim_free(saved_oldval_g);
594 vim_free(saved_newval);
595#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100596 return errmsg;
Bram Moolenaardac13472019-09-16 21:06:21 +0200597}
598
599/*
600 * Return TRUE if "val" is a valid 'filetype' name.
601 * Also used for 'syntax' and 'keymap'.
602 */
603 static int
604valid_filetype(char_u *val)
605{
606 return valid_name(val, ".-_");
607}
608
609#ifdef FEAT_STL_OPT
610/*
611 * Check validity of options with the 'statusline' format.
zeertzjq5dc294a2022-04-15 13:17:57 +0100612 * Return an untranslated error message or NULL.
Bram Moolenaardac13472019-09-16 21:06:21 +0200613 */
614 static char *
615check_stl_option(char_u *s)
616{
Bram Moolenaardac13472019-09-16 21:06:21 +0200617 int groupdepth = 0;
618 static char errbuf[80];
619
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100620 while (*s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200621 {
622 // Check for valid keys after % sequences
623 while (*s && *s != '%')
624 s++;
625 if (!*s)
626 break;
627 s++;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +0000628 if (*s == '%' || *s == STL_TRUNCMARK || *s == STL_SEPARATE)
Bram Moolenaardac13472019-09-16 21:06:21 +0200629 {
630 s++;
631 continue;
632 }
633 if (*s == ')')
634 {
635 s++;
636 if (--groupdepth < 0)
637 break;
638 continue;
639 }
640 if (*s == '-')
641 s++;
642 while (VIM_ISDIGIT(*s))
643 s++;
644 if (*s == STL_USER_HL)
645 continue;
646 if (*s == '.')
647 {
648 s++;
649 while (*s && VIM_ISDIGIT(*s))
650 s++;
651 }
652 if (*s == '(')
653 {
654 groupdepth++;
655 continue;
656 }
657 if (vim_strchr(STL_ALL, *s) == NULL)
658 {
659 return illegal_char(errbuf, *s);
660 }
661 if (*s == '{')
662 {
zeertzjq5dc294a2022-04-15 13:17:57 +0100663 int reevaluate = (*++s == '%');
shadmansaleh30e3de22021-05-15 17:23:28 +0200664
zeertzjq5dc294a2022-04-15 13:17:57 +0100665 if (reevaluate && *++s == '}')
666 // "}" is not allowed immediately after "%{%"
667 return illegal_char(errbuf, '}');
shadmansaleh30e3de22021-05-15 17:23:28 +0200668 while ((*s != '}' || (reevaluate && s[-1] != '%')) && *s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200669 s++;
670 if (*s != '}')
zeertzjq5dc294a2022-04-15 13:17:57 +0100671 return e_unclosed_expression_sequence;
Bram Moolenaardac13472019-09-16 21:06:21 +0200672 }
673 }
Bram Moolenaardac13472019-09-16 21:06:21 +0200674 if (groupdepth != 0)
zeertzjq5dc294a2022-04-15 13:17:57 +0100675 return e_unbalanced_groups;
Bram Moolenaardac13472019-09-16 21:06:21 +0200676 return NULL;
677}
678#endif
679
680/*
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +0000681 * Check for a "normal" directory or file name in some options. Disallow a
682 * path separator (slash and/or backslash), wildcards and characters that are
683 * often illegal in a file name. Be more permissive if "secure" is off.
684 */
685 static int
686check_illegal_path_names(int opt_idx, char_u **varp)
687{
688 return (((get_option_flags(opt_idx) & P_NFNAME)
689 && vim_strpbrk(*varp, (char_u *)(secure
690 ? "/\\*?[|;&<>\r\n" : "/\\*?[<>\r\n")) != NULL)
691 || ((get_option_flags(opt_idx) & P_NDNAME)
692 && vim_strpbrk(*varp, (char_u *)"*?[|;&<>\r\n") != NULL));
693}
694
695/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000696 * An option that accepts a list of flags is changed.
697 * e.g. 'viewoptions', 'switchbuf', 'casemap', etc.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000698 */
699 static char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000700did_set_opt_flags(char_u *val, char **values, unsigned *flagp, int list)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000701{
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000702 if (opt_strings_flags(val, values, flagp, list) == FAIL)
703 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000704
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000705 return NULL;
706}
707
708/*
709 * An option that accepts a list of string values is changed.
710 * e.g. 'nrformats', 'scrollopt', 'wildoptions', etc.
711 */
712 static char *
713did_set_opt_strings(char_u *val, char **values, int list)
714{
715 return did_set_opt_flags(val, values, NULL, list);
716}
717
718/*
719 * An option which is a list of flags is set. Valid values are in 'flags'.
720 */
721 static char *
Yegappan Lakshmananc727b192023-03-03 12:26:15 +0000722did_set_option_listflag(char_u *val, char_u *flags, char *errbuf)
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000723{
724 char_u *s;
725
Yegappan Lakshmananc727b192023-03-03 12:26:15 +0000726 for (s = val; *s; ++s)
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000727 if (vim_strchr(flags, *s) == NULL)
728 return illegal_char(errbuf, *s);
729
730 return NULL;
731}
732
733/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200734 * Expand an option that accepts a list of fixed string values with known
735 * number of items.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200736 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200737 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200738expand_set_opt_string(
739 optexpand_T *args,
740 char **values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +0200741 size_t numValues,
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200742 int *numMatches,
743 char_u ***matches)
744{
745 char_u *p;
746 regmatch_T *regmatch = args->oe_regmatch;
747 int include_orig_val = args->oe_include_orig_val;
748 char_u *option_val = args->oe_opt_value;
749
750 // Assume numValues is small since they are fixed enums, so just allocate
751 // upfront instead of needing two passes to calculate output size.
752 *matches = ALLOC_MULT(char_u *, numValues + 1);
753 if (*matches == NULL)
754 return FAIL;
755
756 int count = 0;
757
758 if (include_orig_val && *option_val != NUL)
759 {
760 p = vim_strsave(option_val);
761 if (p == NULL)
762 {
763 VIM_CLEAR(*matches);
764 return FAIL;
765 }
766 (*matches)[count++] = p;
767 }
768
769 for (char **val = values; *val != NULL; val++)
770 {
771 if (include_orig_val && *option_val != NUL)
772 {
773 if (STRCMP((char_u*)*val, option_val) == 0)
774 continue;
775 }
776 if (vim_regexec(regmatch, (char_u*)(*val), (colnr_T)0))
777 {
778 p = vim_strsave((char_u*)*val);
779 if (p == NULL)
780 {
781 if (count == 0)
782 {
783 VIM_CLEAR(*matches);
784 return FAIL;
785 }
786 else
787 break;
788 }
789 (*matches)[count++] = p;
790 }
791 }
792 if (count == 0)
793 {
794 VIM_CLEAR(*matches);
795 return FAIL;
796 }
797 *numMatches = count;
798 return OK;
799}
800
801static char_u *set_opt_callback_orig_option = NULL;
802static char_u *((*set_opt_callback_func)(expand_T *, int));
803
804/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200805 * Callback used by expand_set_opt_generic to also include the original value
806 * as the first item.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200807 */
808 static char_u *
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200809expand_set_opt_generic_cb(expand_T *xp, int idx)
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200810{
811 if (idx == 0)
812 {
813 if (set_opt_callback_orig_option != NULL)
814 return set_opt_callback_orig_option;
815 else
816 return (char_u *)""; // empty strings are ignored
817 }
818 return set_opt_callback_func(xp, idx - 1);
819}
820
821/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200822 * Expand an option with a callback that iterates through a list of possible
823 * names using an index.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200824 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200825 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200826expand_set_opt_generic(
827 optexpand_T *args,
828 char_u *((*func)(expand_T *, int)),
829 int *numMatches,
830 char_u ***matches)
831{
832 int ret;
833
834 set_opt_callback_orig_option = args->oe_include_orig_val ?
835 args->oe_opt_value : NULL;
836 set_opt_callback_func = func;
837
838 ret = ExpandGeneric(
839 (char_u*)"", // not using fuzzy as currently EXPAND_STRING_SETTING doesn't use it
840 args->oe_xp,
841 args->oe_regmatch,
842 matches,
843 numMatches,
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200844 expand_set_opt_generic_cb,
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200845 FALSE);
846
847 set_opt_callback_orig_option = NULL;
848 set_opt_callback_func = NULL;
849 return ret;
850}
851
Christian Brabandt9960ebc2023-10-05 22:17:09 +0200852# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200853static garray_T *expand_cb_ga;
854static optexpand_T *expand_cb_args;
855
856/*
857 * Callback provided to a function in expand_set_opt_callback. Will perform
858 * regex matching against the value and add to the list.
859 *
860 * Returns OK usually. Returns FAIL if it failed to allocate memory, and the
861 * caller should terminate the enumeration.
862 */
863 static int
864expand_set_opt_callback_cb(char_u *val)
865{
866 regmatch_T *regmatch = expand_cb_args->oe_regmatch;
867 expand_T *xp = expand_cb_args->oe_xp;
868 garray_T *ga = expand_cb_ga;
869 char_u *str;
870
871 if (val == NULL || *val == NUL)
872 return OK;
873
874 if (xp->xp_pattern[0] != NUL &&
875 !vim_regexec(regmatch, val, (colnr_T)0))
876 return OK;
877
878 str = vim_strsave_escaped(val, (char_u *)" \t\\");
879
880 if (str == NULL)
881 return FAIL;
882
883 if (ga_grow(ga, 1) == FAIL)
884 {
885 vim_free(str);
886 return FAIL;
887 }
888
889 ((char_u **)ga->ga_data)[ga->ga_len] = str;
890 ++ga->ga_len;
891 return OK;
892}
893
894/*
895 * Expand an option with a provided function that takes a callback. The
896 * function will enumerate through all options and call the callback to add it
897 * to the list.
898 *
899 * "func" is the enumerator function that will generate the list of options.
900 * "func_params" is a single parameter that will be passed to func.
901 */
902 static int
903expand_set_opt_callback(
904 optexpand_T *args,
905 void (*func)(optexpand_T *, void* params, int (*cb)(char_u *val)),
906 void *func_params,
907 int *numMatches,
908 char_u ***matches)
909{
910 garray_T ga;
911 int include_orig_val = args->oe_include_orig_val;
912 char_u *option_val = args->oe_opt_value;
913
914 ga_init2(&ga, sizeof(char *), 30);
915
916 if (include_orig_val && *option_val != NUL)
917 {
918 char_u *p = vim_strsave(option_val);
919 if (p == NULL)
920 return FAIL;
921 if (ga_grow(&ga, 1) == FAIL)
922 {
923 vim_free(p);
924 return FAIL;
925 }
926 ((char_u **)ga.ga_data)[ga.ga_len] = p;
927 ++ga.ga_len;
928 }
929
930 expand_cb_ga = &ga;
931 expand_cb_args = args;
932
933 func(args, func_params, expand_set_opt_callback_cb);
934
935 expand_cb_ga = NULL;
936 expand_cb_args = NULL;
937
938 *matches = ga.ga_data;
939 *numMatches = ga.ga_len;
940 return OK;
941}
Christian Brabandt9960ebc2023-10-05 22:17:09 +0200942#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200943
944/*
945 * Expand an option which is a list of flags.
946 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200947 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200948expand_set_opt_listflag(
949 optexpand_T *args,
950 char_u *flags,
951 int *numMatches,
952 char_u ***matches)
953{
954 char_u *p;
955 char_u *option_val = args->oe_opt_value;
956 char_u *cmdline_val = args->oe_set_arg;
957 int append = args->oe_append;
958 int include_orig_val = args->oe_include_orig_val && (*option_val != NUL);
959
Yee Cheng Chin6d113472023-10-02 21:38:39 +0200960 size_t num_flags = STRLEN(flags);
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200961
962 // Assume we only have small number of flags, so just allocate max size.
963 *matches = ALLOC_MULT(char_u *, num_flags + 1);
964 if (*matches == NULL)
965 return FAIL;
966
967 int count = 0;
968
969 if (include_orig_val)
970 {
971 p = vim_strsave(option_val);
972 if (p == NULL)
973 {
974 VIM_CLEAR(*matches);
975 return FAIL;
976 }
977 (*matches)[count++] = p;
978 }
979
980 for (char_u *flag = flags; *flag != NUL; flag++)
981 {
982 if (append && vim_strchr(option_val, *flag) != NULL)
983 continue;
984
985 if (vim_strchr(cmdline_val, *flag) == NULL)
986 {
987 if (include_orig_val
988 && option_val[1] == NUL
989 && *flag == option_val[0])
990 {
991 // This value is already used as the first choice as it's the
992 // existing flag. Just skip it to avoid duplicate.
993 continue;
994 }
995 p = vim_strnsave(flag, 1);
996 if (p == NULL)
997 {
998 if (count == 0)
999 {
1000 VIM_CLEAR(*matches);
1001 return FAIL;
1002 }
1003 else
1004 break;
1005 }
1006 (*matches)[count++] = p;
1007 }
1008 }
1009
1010 if (count == 0)
1011 {
1012 VIM_CLEAR(*matches);
1013 return FAIL;
1014 }
1015 *numMatches = count;
1016 return OK;
1017}
1018
1019/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001020 * The 'ambiwidth' option is changed.
1021 */
1022 char *
1023did_set_ambiwidth(optset_T *args UNUSED)
1024{
1025 if (check_opt_strings(p_ambw, p_ambw_values, FALSE) != OK)
1026 return e_invalid_argument;
1027
1028 return check_chars_options();
1029}
1030
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001031 int
1032expand_set_ambiwidth(optexpand_T *args, int *numMatches, char_u ***matches)
1033{
1034 return expand_set_opt_string(
1035 args,
1036 p_ambw_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001037 ARRAY_LENGTH(p_ambw_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001038 numMatches,
1039 matches);
1040}
1041
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001042/*
1043 * The 'background' option is changed.
1044 */
1045 char *
1046did_set_background(optset_T *args UNUSED)
1047{
1048 if (check_opt_strings(p_bg, p_bg_values, FALSE) == FAIL)
1049 return e_invalid_argument;
1050
1051#ifdef FEAT_EVAL
1052 int dark = (*p_bg == 'd');
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001053#endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001054
1055 init_highlight(FALSE, FALSE);
1056
1057#ifdef FEAT_EVAL
1058 if (dark != (*p_bg == 'd')
1059 && get_var_value((char_u *)"g:colors_name") != NULL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001060 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001061 // The color scheme must have set 'background' back to another
1062 // value, that's not what we want here. Disable the color
1063 // scheme and set the colors again.
1064 do_unlet((char_u *)"g:colors_name", TRUE);
1065 free_string_option(p_bg);
1066 p_bg = vim_strsave((char_u *)(dark ? "dark" : "light"));
1067 check_string_option(&p_bg);
1068 init_highlight(FALSE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001069 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001070#endif
1071#ifdef FEAT_TERMINAL
1072 term_update_colors_all();
1073#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001074
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001075 return NULL;
1076}
1077
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001078 int
1079expand_set_background(optexpand_T *args, int *numMatches, char_u ***matches)
1080{
1081 return expand_set_opt_string(
1082 args,
1083 p_bg_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001084 ARRAY_LENGTH(p_bg_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001085 numMatches,
1086 matches);
1087}
1088
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001089/*
1090 * The 'backspace' option is changed.
1091 */
1092 char *
1093did_set_backspace(optset_T *args UNUSED)
1094{
1095 if (VIM_ISDIGIT(*p_bs))
1096 {
1097 if (*p_bs > '3' || p_bs[1] != NUL)
1098 return e_invalid_argument;
1099 }
1100 else if (check_opt_strings(p_bs, p_bs_values, TRUE) != OK)
1101 return e_invalid_argument;
1102
1103 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001104}
1105
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001106 int
1107expand_set_backspace(optexpand_T *args, int *numMatches, char_u ***matches)
1108{
1109 return expand_set_opt_string(
1110 args,
1111 p_bs_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001112 ARRAY_LENGTH(p_bs_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001113 numMatches,
1114 matches);
1115}
1116
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001117/*
1118 * The 'backupcopy' option is changed.
1119 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001120 char *
1121did_set_backupcopy(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001122{
1123 char_u *bkc = p_bkc;
1124 unsigned int *flags = &bkc_flags;
1125 char *errmsg = NULL;
1126
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001127 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001128 {
1129 bkc = curbuf->b_p_bkc;
1130 flags = &curbuf->b_bkc_flags;
1131 }
1132
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001133 if ((args->os_flags & OPT_LOCAL) && *bkc == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001134 // make the local value empty: use the global value
1135 *flags = 0;
1136 else
1137 {
1138 if (opt_strings_flags(bkc, p_bkc_values, flags, TRUE) != OK)
1139 errmsg = e_invalid_argument;
1140 if ((((int)*flags & BKC_AUTO) != 0)
1141 + (((int)*flags & BKC_YES) != 0)
1142 + (((int)*flags & BKC_NO) != 0) != 1)
1143 {
1144 // Must have exactly one of "auto", "yes" and "no".
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001145 (void)opt_strings_flags(args->os_oldval.string, p_bkc_values,
1146 flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001147 errmsg = e_invalid_argument;
1148 }
1149 }
1150
1151 return errmsg;
1152}
1153
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001154 int
1155expand_set_backupcopy(optexpand_T *args, int *numMatches, char_u ***matches)
1156{
1157 return expand_set_opt_string(
1158 args,
1159 p_bkc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001160 ARRAY_LENGTH(p_bkc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001161 numMatches,
1162 matches);
1163}
1164
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001165/*
1166 * The 'backupext' or the 'patchmode' option is changed.
1167 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001168 char *
1169did_set_backupext_or_patchmode(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001170{
1171 if (STRCMP(*p_bex == '.' ? p_bex + 1 : p_bex,
1172 *p_pm == '.' ? p_pm + 1 : p_pm) == 0)
1173 return e_backupext_and_patchmode_are_equal;
1174
1175 return NULL;
1176}
1177
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001178/*
1179 * The 'belloff' option is changed.
1180 */
1181 char *
1182did_set_belloff(optset_T *args UNUSED)
1183{
1184 return did_set_opt_flags(p_bo, p_bo_values, &bo_flags, TRUE);
1185}
1186
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001187 int
1188expand_set_belloff(optexpand_T *args, int *numMatches, char_u ***matches)
1189{
1190 return expand_set_opt_string(
1191 args,
1192 p_bo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001193 ARRAY_LENGTH(p_bo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001194 numMatches,
1195 matches);
1196}
1197
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001198#if defined(FEAT_LINEBREAK) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001199/*
1200 * The 'breakindentopt' option is changed.
1201 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001202 char *
1203did_set_breakindentopt(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001204{
1205 char *errmsg = NULL;
1206
1207 if (briopt_check(curwin) == FAIL)
1208 errmsg = e_invalid_argument;
1209 // list setting requires a redraw
1210 if (curwin->w_briopt_list)
1211 redraw_all_later(UPD_NOT_VALID);
1212
1213 return errmsg;
1214}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001215
1216 int
1217expand_set_breakindentopt(optexpand_T *args, int *numMatches, char_u ***matches)
1218{
1219 return expand_set_opt_string(
1220 args,
1221 p_briopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001222 ARRAY_LENGTH(p_briopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001223 numMatches,
1224 matches);
1225}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001226#endif
1227
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001228#if defined(FEAT_BROWSE) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001229/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001230 * The 'browsedir' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001231 */
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00001232 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001233did_set_browsedir(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001234{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001235 if (check_opt_strings(p_bsdir, p_bsdir_values, FALSE) != OK
1236 && !mch_isdir(p_bsdir))
1237 return e_invalid_argument;
1238
1239 return NULL;
1240}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001241
1242 int
1243expand_set_browsedir(optexpand_T *args, int *numMatches, char_u ***matches)
1244{
1245 return expand_set_opt_string(
1246 args,
1247 p_bsdir_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001248 ARRAY_LENGTH(p_bsdir_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001249 numMatches,
1250 matches);
1251}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001252#endif
1253
1254/*
1255 * The 'bufhidden' option is changed.
1256 */
1257 char *
1258did_set_bufhidden(optset_T *args UNUSED)
1259{
1260 return did_set_opt_strings(curbuf->b_p_bh, p_bufhidden_values, FALSE);
1261}
1262
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001263 int
1264expand_set_bufhidden(optexpand_T *args, int *numMatches, char_u ***matches)
1265{
1266 return expand_set_opt_string(
1267 args,
1268 p_bufhidden_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001269 ARRAY_LENGTH(p_bufhidden_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001270 numMatches,
1271 matches);
1272}
1273
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001274/*
1275 * The 'buftype' option is changed.
1276 */
1277 char *
1278did_set_buftype(optset_T *args UNUSED)
1279{
1280 if (check_opt_strings(curbuf->b_p_bt, p_buftype_values, FALSE) != OK)
1281 return e_invalid_argument;
1282
1283 if (curwin->w_status_height)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001284 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001285 curwin->w_redr_status = TRUE;
1286 redraw_later(UPD_VALID);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001287 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001288 curbuf->b_help = (curbuf->b_p_bt[0] == 'h');
1289 redraw_titles();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001290
1291 return NULL;
1292}
1293
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001294 int
1295expand_set_buftype(optexpand_T *args, int *numMatches, char_u ***matches)
1296{
1297 return expand_set_opt_string(
1298 args,
1299 p_buftype_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001300 ARRAY_LENGTH(p_buftype_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001301 numMatches,
1302 matches);
1303}
1304
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001305/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001306 * The 'casemap' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001307 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001308 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001309did_set_casemap(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001310{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001311 return did_set_opt_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
1312}
1313
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001314 int
1315expand_set_casemap(optexpand_T *args, int *numMatches, char_u ***matches)
1316{
1317 return expand_set_opt_string(
1318 args,
1319 p_cmp_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001320 ARRAY_LENGTH(p_cmp_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001321 numMatches,
1322 matches);
1323}
1324
1325#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1326 int
1327expand_set_clipboard(optexpand_T *args, int *numMatches, char_u ***matches)
1328{
1329 return expand_set_opt_string(
1330 args,
1331 p_cb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001332 ARRAY_LENGTH(p_cb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001333 numMatches,
1334 matches);
1335}
1336#endif
1337
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001338/*
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001339 * The global 'listchars' or 'fillchars' option is changed.
1340 */
1341 static char *
1342did_set_global_listfillchars(char_u *val, int opt_lcs, int opt_flags)
1343{
1344 char *errmsg = NULL;
1345 char_u **local_ptr = opt_lcs ? &curwin->w_p_lcs : &curwin->w_p_fcs;
1346
1347 // only apply the global value to "curwin" when it does not have a
1348 // local value
1349 if (opt_lcs)
1350 errmsg = set_listchars_option(curwin, val,
1351 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL));
1352 else
1353 errmsg = set_fillchars_option(curwin, val,
1354 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL));
1355 if (errmsg != NULL)
1356 return errmsg;
1357
1358 tabpage_T *tp;
1359 win_T *wp;
1360
1361 // If the current window is set to use the global
1362 // 'listchars'/'fillchars' value, clear the window-local value.
1363 if (!(opt_flags & OPT_GLOBAL))
1364 clear_string_option(local_ptr);
1365 FOR_ALL_TAB_WINDOWS(tp, wp)
1366 {
1367 // If the current window has a local value need to apply it
1368 // again, it was changed when setting the global value.
1369 // If no error was returned above, we don't expect an error
1370 // here, so ignore the return value.
1371 if (opt_lcs)
1372 {
1373 if (*wp->w_p_lcs == NUL)
1374 (void)set_listchars_option(wp, wp->w_p_lcs, TRUE);
1375 }
1376 else
1377 {
1378 if (*wp->w_p_fcs == NUL)
1379 (void)set_fillchars_option(wp, wp->w_p_fcs, TRUE);
1380 }
1381 }
1382
1383 redraw_all_later(UPD_NOT_VALID);
1384
1385 return NULL;
1386}
1387
1388/*
1389 * The 'fillchars' option or the 'listchars' option is changed.
1390 */
1391 char *
1392did_set_chars_option(optset_T *args)
1393{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001394 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001395 char *errmsg = NULL;
1396
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001397 if ( varp == &p_lcs // global 'listchars'
1398 || varp == &p_fcs) // global 'fillchars'
1399 errmsg = did_set_global_listfillchars(*varp, varp == &p_lcs,
1400 args->os_flags);
1401 else if (varp == &curwin->w_p_lcs) // local 'listchars'
1402 errmsg = set_listchars_option(curwin, *varp, TRUE);
1403 else if (varp == &curwin->w_p_fcs) // local 'fillchars'
1404 errmsg = set_fillchars_option(curwin, *varp, TRUE);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001405
1406 return errmsg;
1407}
1408
1409/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001410 * Expand 'fillchars' or 'listchars' option value.
1411 */
1412 int
1413expand_set_chars_option(optexpand_T *args, int *numMatches, char_u ***matches)
1414{
1415 char_u **varp = (char_u **)args->oe_varp;
1416 int is_lcs = (varp == &p_lcs || varp == &curwin->w_p_lcs);
1417 return expand_set_opt_generic(
1418 args,
1419 is_lcs ? get_listchars_name : get_fillchars_name,
1420 numMatches,
1421 matches);
1422}
1423
1424/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001425 * The 'cinoptions' option is changed.
1426 */
1427 char *
1428did_set_cinoptions(optset_T *args UNUSED)
1429{
1430 // TODO: recognize errors
1431 parse_cino(curbuf);
1432
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001433 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001434}
1435
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00001436#if defined(FEAT_SYN_HL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001437/*
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001438 * The 'colorcolumn' option is changed.
1439 */
1440 char *
1441did_set_colorcolumn(optset_T *args UNUSED)
1442{
1443 return check_colorcolumn(curwin);
1444}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001445#endif
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001446
1447/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001448 * The 'comments' option is changed.
1449 */
1450 char *
1451did_set_comments(optset_T *args)
1452{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001453 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001454 char_u *s;
1455 char *errmsg = NULL;
1456
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001457 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001458 {
1459 while (*s && *s != ':')
1460 {
1461 if (vim_strchr((char_u *)COM_ALL, *s) == NULL
1462 && !VIM_ISDIGIT(*s) && *s != '-')
1463 {
1464 errmsg = illegal_char(args->os_errbuf, *s);
1465 break;
1466 }
1467 ++s;
1468 }
1469 if (*s++ == NUL)
1470 errmsg = e_missing_colon;
1471 else if (*s == ',' || *s == NUL)
1472 errmsg = e_zero_length_string;
1473 if (errmsg != NULL)
1474 break;
1475 while (*s && *s != ',')
1476 {
1477 if (*s == '\\' && s[1] != NUL)
1478 ++s;
1479 ++s;
1480 }
1481 s = skip_to_option_part(s);
1482 }
1483
1484 return errmsg;
1485}
1486
1487#if defined(FEAT_FOLDING) || defined(PROTO)
1488/*
1489 * The 'commentstring' option is changed.
1490 */
1491 char *
1492did_set_commentstring(optset_T *args)
1493{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001494 char_u **varp = (char_u **)args->os_varp;
1495
1496 if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001497 return e_commentstring_must_be_empty_or_contain_str;
1498
1499 return NULL;
1500}
1501#endif
1502
1503/*
1504 * The 'complete' option is changed.
1505 */
1506 char *
1507did_set_complete(optset_T *args)
1508{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001509 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001510 char_u *s;
1511
1512 // check if it is a valid value for 'complete' -- Acevedo
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001513 for (s = *varp; *s;)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001514 {
1515 while (*s == ',' || *s == ' ')
1516 s++;
1517 if (!*s)
1518 break;
1519 if (vim_strchr((char_u *)".wbuksid]tU", *s) == NULL)
1520 return illegal_char(args->os_errbuf, *s);
1521 if (*++s != NUL && *s != ',' && *s != ' ')
1522 {
1523 if (s[-1] == 'k' || s[-1] == 's')
1524 {
1525 // skip optional filename after 'k' and 's'
1526 while (*s && *s != ',' && *s != ' ')
1527 {
1528 if (*s == '\\' && s[1] != NUL)
1529 ++s;
1530 ++s;
1531 }
1532 }
1533 else
1534 {
1535 if (args->os_errbuf != NULL)
1536 {
1537 sprintf((char *)args->os_errbuf,
1538 _(e_illegal_character_after_chr), *--s);
1539 return args->os_errbuf;
1540 }
1541 return "";
1542 }
1543 }
1544 }
1545
1546 return NULL;
1547}
1548
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001549 int
1550expand_set_complete(optexpand_T *args, int *numMatches, char_u ***matches)
1551{
1552 static char *(p_cpt_values[]) = {
1553 ".", "w", "b", "u", "k", "kspell", "s", "i", "d", "]", "t", "U",
1554 NULL};
1555 return expand_set_opt_string(
1556 args,
1557 p_cpt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001558 ARRAY_LENGTH(p_cpt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001559 numMatches,
1560 matches);
1561}
1562
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001563/*
1564 * The 'completeopt' option is changed.
1565 */
1566 char *
1567did_set_completeopt(optset_T *args UNUSED)
1568{
1569 if (check_opt_strings(p_cot, p_cot_values, TRUE) != OK)
1570 return e_invalid_argument;
1571
1572 completeopt_was_set();
1573 return NULL;
1574}
1575
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001576 int
1577expand_set_completeopt(optexpand_T *args, int *numMatches, char_u ***matches)
1578{
1579 return expand_set_opt_string(
1580 args,
1581 p_cot_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001582 ARRAY_LENGTH(p_cot_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001583 numMatches,
1584 matches);
1585}
1586
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001587#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1588/*
1589 * The 'completepopup' option is changed.
1590 */
1591 char *
1592did_set_completepopup(optset_T *args UNUSED)
1593{
1594 if (parse_completepopup(NULL) == FAIL)
1595 return e_invalid_argument;
1596
1597 popup_close_info();
1598 return NULL;
1599}
1600#endif
1601
1602#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1603/*
1604 * The 'completeslash' option is changed.
1605 */
1606 char *
1607did_set_completeslash(optset_T *args UNUSED)
1608{
1609 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1610 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
1611 return e_invalid_argument;
1612
1613 return NULL;
1614}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001615
1616 int
1617expand_set_completeslash(optexpand_T *args, int *numMatches, char_u ***matches)
1618{
1619 return expand_set_opt_string(
1620 args,
1621 p_csl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001622 ARRAY_LENGTH(p_csl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001623 numMatches,
1624 matches);
1625}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001626#endif
1627
1628#if defined(FEAT_CONCEAL) || defined(PROTO)
1629/*
1630 * The 'concealcursor' option is changed.
1631 */
1632 char *
1633did_set_concealcursor(optset_T *args)
1634{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001635 char_u **varp = (char_u **)args->os_varp;
1636
1637 return did_set_option_listflag(*varp, (char_u *)COCU_ALL, args->os_errbuf);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001638}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001639
1640 int
1641expand_set_concealcursor(optexpand_T *args, int *numMatches, char_u ***matches)
1642{
1643 return expand_set_opt_listflag(args, (char_u*)COCU_ALL, numMatches, matches);
1644}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001645#endif
1646
1647/*
1648 * The 'cpoptions' option is changed.
1649 */
1650 char *
1651did_set_cpoptions(optset_T *args)
1652{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001653 char_u **varp = (char_u **)args->os_varp;
1654
1655 return did_set_option_listflag(*varp, (char_u *)CPO_ALL, args->os_errbuf);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001656}
1657
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001658 int
1659expand_set_cpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
1660{
1661 return expand_set_opt_listflag(args, (char_u*)CPO_ALL, numMatches, matches);
1662}
1663
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001664#if defined(FEAT_CRYPT) || defined(PROTO)
1665/*
1666 * The 'cryptkey' option is changed.
1667 */
1668 char *
1669did_set_cryptkey(optset_T *args)
1670{
1671 // Make sure the ":set" command doesn't show the new value in the
1672 // history.
1673 remove_key_from_history();
1674
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02001675 if (args->os_op != OP_NONE)
1676 // Don't allow set+=/-=/^= as they can allow for substring guessing
1677 return e_invalid_argument;
1678
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001679 if (STRCMP(curbuf->b_p_key, args->os_oldval.string) != 0)
1680 {
1681 // Need to update the swapfile.
1682 ml_set_crypt_key(curbuf, args->os_oldval.string,
1683 *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
1684 changed_internal();
1685 }
Christian Brabandt19e6c4f2023-06-27 18:57:10 +01001686# ifdef FEAT_SODIUM
1687 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
1688 crypt_sodium_lock_key(args->os_newval.string);
1689# endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001690
1691 return NULL;
1692}
1693
1694/*
1695 * The 'cryptmethod' option is changed.
1696 */
1697 char *
1698did_set_cryptmethod(optset_T *args)
1699{
1700 char_u *p;
1701 char_u *s;
1702
1703 if (args->os_flags & OPT_LOCAL)
1704 p = curbuf->b_p_cm;
1705 else
1706 p = p_cm;
1707 if (check_opt_strings(p, p_cm_values, TRUE) != OK)
1708 return e_invalid_argument;
1709 else if (crypt_self_test() == FAIL)
1710 return e_invalid_argument;
1711
1712 // When setting the global value to empty, make it "zip".
1713 if (*p_cm == NUL)
1714 {
1715 free_string_option(p_cm);
1716 p_cm = vim_strsave((char_u *)"zip");
1717 }
1718 // When using ":set cm=name" the local value is going to be empty.
1719 // Do that here, otherwise the crypt functions will still use the
1720 // local value.
1721 if ((args->os_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1722 {
1723 free_string_option(curbuf->b_p_cm);
1724 curbuf->b_p_cm = empty_option;
1725 }
1726
1727 // Need to update the swapfile when the effective method changed.
1728 // Set "s" to the effective old value, "p" to the effective new
1729 // method and compare.
1730 if ((args->os_flags & OPT_LOCAL) && *args->os_oldval.string == NUL)
1731 s = p_cm; // was previously using the global value
1732 else
1733 s = args->os_oldval.string;
1734 if (*curbuf->b_p_cm == NUL)
1735 p = p_cm; // is now using the global value
1736 else
1737 p = curbuf->b_p_cm;
1738 if (STRCMP(s, p) != 0)
1739 ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1740
1741 // If the global value changes need to update the swapfile for all
1742 // buffers using that value.
1743 if ((args->os_flags & OPT_GLOBAL)
1744 && STRCMP(p_cm, args->os_oldval.string) != 0)
1745 {
1746 buf_T *buf;
1747
1748 FOR_ALL_BUFFERS(buf)
1749 if (buf != curbuf && *buf->b_p_cm == NUL)
1750 ml_set_crypt_key(buf, buf->b_p_key, args->os_oldval.string);
1751 }
1752 return NULL;
1753}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001754
1755 int
1756expand_set_cryptmethod(optexpand_T *args, int *numMatches, char_u ***matches)
1757{
1758 return expand_set_opt_string(
1759 args,
1760 p_cm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001761 ARRAY_LENGTH(p_cm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001762 numMatches,
1763 matches);
1764}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001765#endif
1766
1767#if (defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1768/*
1769 * The 'cscopequickfix' option is changed.
1770 */
1771 char *
1772did_set_cscopequickfix(optset_T *args UNUSED)
1773{
1774 char_u *p;
1775
1776 if (p_csqf == NULL)
1777 return NULL;
1778
1779 p = p_csqf;
1780 while (*p != NUL)
1781 {
1782 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
1783 || p[1] == NUL
1784 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
1785 || (p[2] != NUL && p[2] != ','))
1786 return e_invalid_argument;
1787 else if (p[2] == NUL)
1788 break;
1789 else
1790 p += 3;
1791 }
1792
1793 return NULL;
1794}
1795#endif
1796
1797#if defined(FEAT_SYN_HL) || defined(PROTO)
1798/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001799 * The 'cursorlineopt' option is changed.
1800 */
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001801 char *
1802did_set_cursorlineopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001803{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001804 char_u **varp = (char_u **)args->os_varp;
1805
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001806 // This could be changed to use opt_strings_flags() instead.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001807 if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001808 return e_invalid_argument;
1809
1810 return NULL;
1811}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001812
1813 int
1814expand_set_cursorlineopt(optexpand_T *args, int *numMatches, char_u ***matches)
1815{
1816 return expand_set_opt_string(
1817 args,
1818 p_culopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001819 ARRAY_LENGTH(p_culopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001820 numMatches,
1821 matches);
1822}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001823#endif
1824
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001825/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001826 * The 'debug' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001827 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001828 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001829did_set_debug(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001830{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001831 return did_set_opt_strings(p_debug, p_debug_values, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001832}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001833
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001834 int
1835expand_set_debug(optexpand_T *args, int *numMatches, char_u ***matches)
1836{
1837 return expand_set_opt_string(
1838 args,
1839 p_debug_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001840 ARRAY_LENGTH(p_debug_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001841 numMatches,
1842 matches);
1843}
1844
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001845#if defined(FEAT_DIFF) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001846/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001847 * The 'diffopt' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001848 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001849 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001850did_set_diffopt(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001851{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001852 if (diffopt_changed() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001853 return e_invalid_argument;
1854
1855 return NULL;
1856}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001857
1858 int
1859expand_set_diffopt(optexpand_T *args, int *numMatches, char_u ***matches)
1860{
1861 expand_T *xp = args->oe_xp;
1862
1863 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
1864 {
1865 // Within "algorithm:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001866 int algo_len = (int)STRLEN("algorithm:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001867 if (xp->xp_pattern - args->oe_set_arg >= algo_len &&
1868 STRNCMP(xp->xp_pattern - algo_len, "algorithm:", algo_len) == 0)
1869 {
1870 return expand_set_opt_string(
1871 args,
1872 p_dip_algorithm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001873 ARRAY_LENGTH(p_dip_algorithm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001874 numMatches,
1875 matches);
1876 }
1877 return FAIL;
1878 }
1879
1880 return expand_set_opt_string(
1881 args,
1882 p_dip_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001883 ARRAY_LENGTH(p_dip_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001884 numMatches,
1885 matches);
1886}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001887#endif
1888
1889/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001890 * The 'display' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001891 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001892 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001893did_set_display(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001894{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001895 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001896 return e_invalid_argument;
1897
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001898 (void)init_chartab();
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001899 return NULL;
1900}
1901
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001902 int
1903expand_set_display(optexpand_T *args, int *numMatches, char_u ***matches)
1904{
1905 return expand_set_opt_string(
1906 args,
1907 p_dy_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001908 ARRAY_LENGTH(p_dy_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001909 numMatches,
1910 matches);
1911}
1912
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001913/*
1914 * The 'eadirection' option is changed.
1915 */
1916 char *
1917did_set_eadirection(optset_T *args UNUSED)
1918{
1919 return did_set_opt_strings(p_ead, p_ead_values, FALSE);
1920}
1921
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001922 int
1923expand_set_eadirection(optexpand_T *args, int *numMatches, char_u ***matches)
1924{
1925 return expand_set_opt_string(
1926 args,
1927 p_ead_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001928 ARRAY_LENGTH(p_ead_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001929 numMatches,
1930 matches);
1931}
1932
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001933/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001934 * One of the 'encoding', 'fileencoding', 'termencoding' or 'makeencoding'
1935 * options is changed.
1936 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001937 char *
1938did_set_encoding(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001939{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001940 char_u **varp = (char_u **)args->os_varp;
1941 char_u **gvarp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001942 char *errmsg = NULL;
1943 char_u *p;
1944
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001945 // Get the global option to compare with, otherwise we would have to check
1946 // two values for all local options.
1947 gvarp = (char_u **)get_option_varp_scope(args->os_idx, OPT_GLOBAL);
1948
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001949 if (gvarp == &p_fenc)
1950 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001951 if (!curbuf->b_p_ma && args->os_flags != OPT_GLOBAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001952 errmsg = e_cannot_make_changes_modifiable_is_off;
1953 else if (vim_strchr(*varp, ',') != NULL)
1954 // No comma allowed in 'fileencoding'; catches confusing it
1955 // with 'fileencodings'.
1956 errmsg = e_invalid_argument;
1957 else
1958 {
1959 // May show a "+" in the title now.
1960 redraw_titles();
1961 // Add 'fileencoding' to the swap file.
1962 ml_setflags(curbuf);
1963 }
1964 }
1965 if (errmsg == NULL)
1966 {
1967 // canonize the value, so that STRCMP() can be used on it
1968 p = enc_canonize(*varp);
1969 if (p != NULL)
1970 {
1971 vim_free(*varp);
1972 *varp = p;
1973 }
1974 if (varp == &p_enc)
1975 {
1976 errmsg = mb_init();
1977 redraw_titles();
1978 }
1979 }
1980
1981#if defined(FEAT_GUI_GTK)
1982 if (errmsg == NULL && varp == &p_tenc && gui.in_use)
1983 {
1984 // GTK uses only a single encoding, and that is UTF-8.
1985 if (STRCMP(p_tenc, "utf-8") != 0)
1986 errmsg = e_cannot_be_changed_in_gtk_GUI;
1987 }
1988#endif
1989
1990 if (errmsg == NULL)
1991 {
1992#ifdef FEAT_KEYMAP
1993 // When 'keymap' is used and 'encoding' changes, reload the keymap
1994 // (with another encoding).
1995 if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
1996 (void)keymap_init();
1997#endif
1998
1999 // When 'termencoding' is not empty and 'encoding' changes or when
2000 // 'termencoding' changes, need to setup for keyboard input and
2001 // display output conversion.
2002 if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
2003 {
2004 if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
2005 || convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
2006 {
2007 semsg(_(e_cannot_convert_between_str_and_str),
2008 p_tenc, p_enc);
2009 errmsg = e_invalid_argument;
2010 }
2011 }
2012
2013#if defined(MSWIN)
K.Takatace3189d2023-02-15 19:13:43 +00002014 // $HOME, $VIM and $VIMRUNTIME may have characters in active code page.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002015 if (varp == &p_enc)
K.Takatace3189d2023-02-15 19:13:43 +00002016 {
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002017 init_homedir();
K.Takatace3189d2023-02-15 19:13:43 +00002018 init_vimdir();
2019 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002020#endif
2021 }
2022
2023 return errmsg;
2024}
2025
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002026 int
2027expand_set_encoding(optexpand_T *args, int *numMatches, char_u ***matches)
2028{
2029 return expand_set_opt_generic(
2030 args,
2031 get_encoding_name,
2032 numMatches,
2033 matches);
2034}
2035
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002036/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002037 * The 'eventignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002038 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002039 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002040did_set_eventignore(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002041{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002042 if (check_ei() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002043 return e_invalid_argument;
2044 return NULL;
2045}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002046
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002047 static char_u *
2048get_eventignore_name(expand_T *xp, int idx)
2049{
2050 // 'eventignore' allows special keyword "all" in addition to
2051 // all event names.
2052 if (idx == 0)
2053 return (char_u *)"all";
2054 return get_event_name_no_group(xp, idx - 1);
2055}
2056
2057 int
2058expand_set_eventignore(optexpand_T *args, int *numMatches, char_u ***matches)
2059{
2060 return expand_set_opt_generic(
2061 args,
2062 get_eventignore_name,
2063 numMatches,
2064 matches);
2065}
2066
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002067/*
2068 * The 'fileformat' option is changed.
2069 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002070 char *
2071did_set_fileformat(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002072{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002073 char_u **varp = (char_u **)args->os_varp;
2074
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002075 if (!curbuf->b_p_ma && !(args->os_flags & OPT_GLOBAL))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002076 return e_cannot_make_changes_modifiable_is_off;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002077 else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002078 return e_invalid_argument;
2079
2080 // may also change 'textmode'
2081 if (get_fileformat(curbuf) == EOL_DOS)
2082 curbuf->b_p_tx = TRUE;
2083 else
2084 curbuf->b_p_tx = FALSE;
2085 redraw_titles();
2086 // update flag in swap file
2087 ml_setflags(curbuf);
2088 // Redraw needed when switching to/from "mac": a CR in the text
2089 // will be displayed differently.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002090 if (get_fileformat(curbuf) == EOL_MAC || *args->os_oldval.string == 'm')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002091 redraw_curbuf_later(UPD_NOT_VALID);
2092
2093 return NULL;
2094}
2095
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002096 int
2097expand_set_fileformat(optexpand_T *args, int *numMatches, char_u ***matches)
2098{
2099 return expand_set_opt_string(
2100 args,
2101 p_ff_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002102 ARRAY_LENGTH(p_ff_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002103 numMatches,
2104 matches);
2105}
2106
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002107/*
2108 * The 'fileformats' option is changed.
2109 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002110 char *
2111did_set_fileformats(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002112{
2113 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
2114 return e_invalid_argument;
2115
2116 // also change 'textauto'
2117 if (*p_ffs == NUL)
2118 p_ta = FALSE;
2119 else
2120 p_ta = TRUE;
2121
2122 return NULL;
2123}
2124
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002125/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002126 * The 'filetype' or the 'syntax' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002127 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002128 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002129did_set_filetype_or_syntax(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002130{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002131 char_u **varp = (char_u **)args->os_varp;
2132
2133 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002134 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002135
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002136 args->os_value_changed = STRCMP(args->os_oldval.string, *varp) != 0;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002137
2138 // Since we check the value, there is no need to set P_INSECURE,
2139 // even when the value comes from a modeline.
2140 args->os_value_checked = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002141
2142 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002143}
2144
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002145#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002146/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002147 * The 'foldclose' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002148 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002149 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002150did_set_foldclose(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002151{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002152 return did_set_opt_strings(p_fcl, p_fcl_values, TRUE);
2153}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002154
2155 int
2156expand_set_foldclose(optexpand_T *args, int *numMatches, char_u ***matches)
2157{
2158 return expand_set_opt_string(
2159 args,
2160 p_fcl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002161 ARRAY_LENGTH(p_fcl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002162 numMatches,
2163 matches);
2164}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002165#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002166
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002167#if (defined(FEAT_EVAL) && defined(FEAT_FOLDING)) || defined(PROTO)
2168/*
2169 * The 'foldexpr' option is changed.
2170 */
2171 char *
2172did_set_foldexpr(optset_T *args)
2173{
2174 (void)did_set_optexpr(args);
2175 if (foldmethodIsExpr(curwin))
2176 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002177 return NULL;
2178}
2179#endif
2180
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002181#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002182/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002183 * The 'foldignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002184 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002185 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002186did_set_foldignore(optset_T *args UNUSED)
2187{
2188 if (foldmethodIsIndent(curwin))
2189 foldUpdateAll(curwin);
2190 return NULL;
2191}
2192
2193/*
2194 * The 'foldmarker' option is changed.
2195 */
2196 char *
2197did_set_foldmarker(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002198{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002199 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002200 char_u *p;
2201
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002202 p = vim_strchr(*varp, ',');
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002203 if (p == NULL)
2204 return e_comma_required;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002205 else if (p == *varp || p[1] == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002206 return e_invalid_argument;
2207 else if (foldmethodIsMarker(curwin))
2208 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002209
2210 return NULL;
2211}
2212
2213/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002214 * The 'foldmethod' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002215 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002216 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002217did_set_foldmethod(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002218{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002219 char_u **varp = (char_u **)args->os_varp;
2220
2221 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002222 || *curwin->w_p_fdm == NUL)
2223 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002224
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002225 foldUpdateAll(curwin);
2226 if (foldmethodIsDiff(curwin))
2227 newFoldLevel();
2228 return NULL;
2229}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002230
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002231 int
2232expand_set_foldmethod(optexpand_T *args, int *numMatches, char_u ***matches)
2233{
2234 return expand_set_opt_string(
2235 args,
2236 p_fdm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002237 ARRAY_LENGTH(p_fdm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002238 numMatches,
2239 matches);
2240}
2241
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002242/*
2243 * The 'foldopen' option is changed.
2244 */
2245 char *
2246did_set_foldopen(optset_T *args UNUSED)
2247{
2248 return did_set_opt_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
2249}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002250
2251 int
2252expand_set_foldopen(optexpand_T *args, int *numMatches, char_u ***matches)
2253{
2254 return expand_set_opt_string(
2255 args,
2256 p_fdo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002257 ARRAY_LENGTH(p_fdo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002258 numMatches,
2259 matches);
2260}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002261#endif
2262
2263/*
2264 * The 'formatoptions' option is changed.
2265 */
2266 char *
2267did_set_formatoptions(optset_T *args)
2268{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002269 char_u **varp = (char_u **)args->os_varp;
2270
2271 return did_set_option_listflag(*varp, (char_u *)FO_ALL, args->os_errbuf);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002272}
2273
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002274 int
2275expand_set_formatoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2276{
2277 return expand_set_opt_listflag(args, (char_u*)FO_ALL, numMatches, matches);
2278}
2279
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002280#if defined(CURSOR_SHAPE) || defined(PROTO)
2281/*
2282 * The 'guicursor' option is changed.
2283 */
2284 char *
2285did_set_guicursor(optset_T *args UNUSED)
2286{
2287 return parse_shape_opt(SHAPE_CURSOR);
2288}
2289#endif
2290
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002291#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002292/*
2293 * The 'guifont' option is changed.
2294 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002295 char *
2296did_set_guifont(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002297{
2298 char_u *p;
2299 char *errmsg = NULL;
2300
2301 if (gui.in_use)
2302 {
2303 p = p_guifont;
2304# if defined(FEAT_GUI_GTK)
2305 // Put up a font dialog and let the user select a new value.
2306 // If this is cancelled go back to the old value but don't
2307 // give an error message.
2308 if (STRCMP(p, "*") == 0)
2309 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002310 p = gui_mch_font_dialog(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002311 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002312 p_guifont = (p != NULL) ? p : vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002313 }
2314# endif
2315 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
2316 {
2317# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
2318 if (STRCMP(p_guifont, "*") == 0)
2319 {
2320 // Dialog was cancelled: Keep the old value without giving
2321 // an error message.
2322 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002323 p_guifont = vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002324 }
2325 else
2326# endif
2327 errmsg = e_invalid_fonts;
2328 }
2329 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002330
2331 return errmsg;
2332}
2333
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002334/*
2335 * Expand the 'guifont' option. Only when GUI is being used. Each platform has
2336 * specific behaviors.
2337 */
2338 int
2339expand_set_guifont(optexpand_T *args, int *numMatches, char_u ***matches)
2340{
2341 if (!gui.in_use)
2342 return FAIL;
2343
2344# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
2345 char_u **varp = (char_u **)args->oe_varp;
2346 int wide = (varp == &p_guifontwide);
2347
2348 return expand_set_opt_callback(
2349 args, gui_mch_expand_font, &wide, numMatches, matches);
2350# else
2351 return FAIL;
2352# endif
2353}
2354
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002355# if defined(FEAT_XFONTSET) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002356/*
2357 * The 'guifontset' option is changed.
2358 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002359 char *
2360did_set_guifontset(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002361{
2362 char *errmsg = NULL;
2363
2364 if (STRCMP(p_guifontset, "*") == 0)
2365 errmsg = e_cant_select_fontset;
2366 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
2367 errmsg = e_invalid_fontset;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002368
2369 return errmsg;
2370}
2371# endif
2372
2373/*
2374 * The 'guifontwide' option is changed.
2375 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002376 char *
2377did_set_guifontwide(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002378{
2379 char *errmsg = NULL;
2380
2381 if (STRCMP(p_guifontwide, "*") == 0)
2382 errmsg = e_cant_select_wide_font;
2383 else if (gui_get_wide_font() == FAIL)
2384 errmsg = e_invalid_wide_font;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002385
2386 return errmsg;
2387}
2388#endif
2389
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002390#if defined(FEAT_GUI_GTK) || defined(PROTO)
2391/*
2392 * The 'guiligatures' option is changed.
2393 */
2394 char *
2395did_set_guiligatures(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002396{
2397 gui_set_ligatures();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002398 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002399}
2400#endif
2401
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002402#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002403/*
2404 * The 'guioptions' option is changed.
2405 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002406 char *
2407did_set_guioptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002408{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002409 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002410 char *errmsg;
2411
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002412 errmsg = did_set_option_listflag(*varp, (char_u *)GO_ALL, args->os_errbuf);
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002413 if (errmsg != NULL)
2414 return errmsg;
2415
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002416 gui_init_which_components(args->os_oldval.string);
2417 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002418}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002419
2420 int
2421expand_set_guioptions(optexpand_T *args, int *numMatches, char_u ***matches)
2422{
2423 return expand_set_opt_listflag(args, (char_u*)GO_ALL, numMatches, matches);
2424}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002425#endif
2426
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002427#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002428/*
2429 * The 'guitablabel' option is changed.
2430 */
2431 char *
2432did_set_guitablabel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002433{
2434 redraw_tabline = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002435 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002436}
2437#endif
2438
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002439/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002440 * The 'helpfile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002441 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002442 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002443did_set_helpfile(optset_T *args UNUSED)
2444{
2445 // May compute new values for $VIM and $VIMRUNTIME
2446 if (didset_vim)
2447 vim_unsetenv_ext((char_u *)"VIM");
2448 if (didset_vimruntime)
2449 vim_unsetenv_ext((char_u *)"VIMRUNTIME");
2450 return NULL;
2451}
2452
2453#if defined(FEAT_MULTI_LANG) || defined(PROTO)
2454/*
2455 * The 'helplang' option is changed.
2456 */
2457 char *
2458did_set_helplang(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002459{
2460 char *errmsg = NULL;
2461
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002462 // Check for "", "ab", "ab,cd", etc.
2463 for (char_u *s = p_hlg; *s != NUL; s += 3)
2464 {
2465 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
2466 {
2467 errmsg = e_invalid_argument;
2468 break;
2469 }
2470 if (s[2] == NUL)
2471 break;
2472 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002473
2474 return errmsg;
2475}
2476#endif
2477
2478/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002479 * The 'highlight' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002480 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002481 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002482did_set_highlight(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002483{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002484 if (highlight_changed() == FAIL)
2485 return e_invalid_argument; // invalid flags
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002486
2487 return NULL;
2488}
2489
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002490/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002491 * Expand 'highlight' option.
2492 */
2493 int
2494expand_set_highlight(optexpand_T *args, int *numMatches, char_u ***matches)
2495{
2496 char_u *p;
2497 expand_T *xp = args->oe_xp;
2498 static char_u hl_flags[HLF_COUNT] = HL_FLAGS;
Christian Brabandt3f168ec2023-10-02 23:21:11 +02002499 size_t i;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002500 int count = 0;
2501
2502 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2503 {
2504 // Right after a ':', meaning we just return all highlight names.
2505 return expand_set_opt_generic(
2506 args,
2507 get_highlight_name,
2508 numMatches,
2509 matches);
2510 }
2511
2512 if (*xp->xp_pattern == NUL)
2513 {
2514 // At beginning of a comma-separated list. Return the specific list of
2515 // supported occasions.
2516 *matches = ALLOC_MULT(char_u *, HLF_COUNT + 1);
2517 if (*matches == NULL)
2518 return FAIL;
2519
2520 // We still want to return the full option if it's requested.
2521 if (args->oe_include_orig_val)
2522 {
2523 p = vim_strsave(args->oe_opt_value);
2524 if (p == NULL)
2525 {
2526 VIM_CLEAR(*matches);
2527 return FAIL;
2528 }
2529 (*matches)[count++] = p;
2530 }
2531
2532 for (i = 0; i < HLF_COUNT; i++)
2533 {
2534 p = vim_strnsave(&hl_flags[i], 1);
2535 if (p == NULL)
2536 {
2537 if (count == 0)
2538 {
2539 VIM_CLEAR(*matches);
2540 return FAIL;
2541 }
2542 else
2543 break;
2544 }
2545 (*matches)[count++] = p;
2546 }
2547
2548 if (count == 0)
2549 {
2550 VIM_CLEAR(*matches);
2551 return FAIL;
2552 }
2553 *numMatches = count;
2554 return OK;
2555 }
2556
2557 // We are after the initial character (which indicates the occasion). We
2558 // already made sure we are not matching after a ':' above, so now we want
2559 // to match against display mode modifiers.
2560 // Since the xp_pattern starts from the beginning, we need to include it in
2561 // the returned match.
2562
2563 // Note: Keep this in sync with highlight_changed()
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002564 static char_u p_hl_mode_values[] =
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002565 {':', 'b', 'i', '-', 'n', 'r', 's', 'u', 'c', '2', 'd', '=', 't'};
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002566 size_t num_hl_modes = ARRAY_LENGTH(p_hl_mode_values);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002567
2568 *matches = ALLOC_MULT(char_u *, num_hl_modes);
2569 if (*matches == NULL)
2570 return FAIL;
2571
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002572 size_t pattern_len = STRLEN(xp->xp_pattern);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002573
2574 for (i = 0; i < num_hl_modes; i++)
2575 {
2576 // Don't allow duplicates as these are unique flags
2577 if (vim_strchr(xp->xp_pattern + 1, p_hl_mode_values[i]) != NULL)
2578 continue;
2579
2580 // ':' only works by itself, not with other flags.
2581 if (pattern_len > 1 && p_hl_mode_values[i] == ':')
2582 continue;
2583
2584 p = vim_strnsave(xp->xp_pattern, pattern_len + 1);
2585 if (p == NULL)
2586 {
2587 if (i == 0)
2588 {
2589 VIM_CLEAR(*matches);
2590 return FAIL;
2591 }
2592 else
2593 break;
2594 }
2595 p[pattern_len] = p_hl_mode_values[i];
2596 p[pattern_len + 1] = NUL;
2597 (*matches)[count++] = p;
2598 }
2599 if (count == 0)
2600 {
2601 VIM_CLEAR(*matches);
2602 return FAIL;
2603 }
2604 *numMatches = count;
2605
2606 return OK;
2607}
2608
2609/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002610 * The 'titlestring' or the 'iconstring' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002611 */
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002612 static char *
2613parse_titleiconstring(optset_T *args UNUSED, int flagval UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002614{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002615#ifdef FEAT_STL_OPT
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002616 char_u **varp = (char_u **)args->os_varp;
2617
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002618 // NULL => statusline syntax
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002619 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002620 stl_syntax |= flagval;
2621 else
2622 stl_syntax &= ~flagval;
2623#endif
2624 did_set_title();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002625
2626 return NULL;
2627}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002628
2629/*
2630 * The 'iconstring' option is changed.
2631 */
2632 char *
2633did_set_iconstring(optset_T *args)
2634{
2635 int flagval = 0;
2636
2637#ifdef FEAT_STL_OPT
2638 flagval = STL_IN_ICON;
2639#endif
2640
2641 return parse_titleiconstring(args, flagval);
2642}
2643
2644#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
2645/*
2646 * The 'imactivatekey' option is changed.
2647 */
2648 char *
2649did_set_imactivatekey(optset_T *args UNUSED)
2650{
2651 if (!im_xim_isvalid_imactivate())
2652 return e_invalid_argument;
2653 return NULL;
2654}
2655#endif
2656
2657/*
2658 * The 'isident' or the 'iskeyword' or the 'isprint' or the 'isfname' option is
2659 * changed.
2660 */
2661 char *
2662did_set_isopt(optset_T *args)
2663{
2664 // 'isident', 'iskeyword', 'isprint or 'isfname' option: refill g_chartab[]
2665 // If the new option is invalid, use old value.
2666 // 'lisp' option: refill g_chartab[] for '-' char.
2667 if (init_chartab() == FAIL)
2668 {
2669 args->os_restore_chartab = TRUE;// need to restore the chartab.
2670 return e_invalid_argument; // error in value
2671 }
2672
2673 return NULL;
2674}
2675
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002676/*
2677 * The 'jumpoptions' option is changed.
2678 */
2679 char *
Yegappan Lakshmanan1926ae42023-09-21 16:36:28 +02002680did_set_jumpoptions(optset_T *args UNUSED)
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002681{
2682 if (opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE) != OK)
2683 return e_invalid_argument;
2684
2685 return NULL;
2686}
2687
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002688 int
2689expand_set_jumpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2690{
2691 return expand_set_opt_string(
2692 args,
2693 p_jop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002694 ARRAY_LENGTH(p_jop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002695 numMatches,
2696 matches);
2697}
2698
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002699#if defined(FEAT_KEYMAP) || defined(PROTO)
2700/*
2701 * The 'keymap' option is changed.
2702 */
2703 char *
2704did_set_keymap(optset_T *args)
2705{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002706 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002707 char *errmsg = NULL;
2708
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002709 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002710 errmsg = e_invalid_argument;
2711 else
2712 {
2713 int secure_save = secure;
2714
2715 // Reset the secure flag, since the value of 'keymap' has
2716 // been checked to be safe.
2717 secure = 0;
2718
2719 // load or unload key mapping tables
2720 errmsg = keymap_init();
2721
2722 secure = secure_save;
2723
2724 // Since we check the value, there is no need to set P_INSECURE,
2725 // even when the value comes from a modeline.
2726 args->os_value_checked = TRUE;
2727 }
2728
2729 if (errmsg == NULL)
2730 {
2731 if (*curbuf->b_p_keymap != NUL)
2732 {
2733 // Installed a new keymap, switch on using it.
2734 curbuf->b_p_iminsert = B_IMODE_LMAP;
2735 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
2736 curbuf->b_p_imsearch = B_IMODE_LMAP;
2737 }
2738 else
2739 {
2740 // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
2741 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
2742 curbuf->b_p_iminsert = B_IMODE_NONE;
2743 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
2744 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
2745 }
2746 if ((args->os_flags & OPT_LOCAL) == 0)
2747 {
2748 set_iminsert_global();
2749 set_imsearch_global();
2750 }
2751 status_redraw_curbuf();
2752 }
2753
2754 return errmsg;
2755}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002756#endif
2757
2758/*
2759 * The 'keymodel' option is changed.
2760 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002761 char *
2762did_set_keymodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002763{
2764 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
2765 return e_invalid_argument;
2766
2767 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
2768 km_startsel = (vim_strchr(p_km, 'a') != NULL);
2769 return NULL;
2770}
2771
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002772 int
2773expand_set_keymodel(optexpand_T *args, int *numMatches, char_u ***matches)
2774{
2775 return expand_set_opt_string(
2776 args,
2777 p_km_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002778 ARRAY_LENGTH(p_km_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002779 numMatches,
2780 matches);
2781}
2782
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002783/*
2784 * The 'keyprotocol' option is changed.
2785 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002786 char *
2787did_set_keyprotocol(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002788{
Gregory Anders3695d0e2023-09-29 20:17:20 +02002789 char_u *term = T_NAME;
2790 keyprot_T kpc = match_keyprotocol(term);
2791 if (kpc == KEYPROTOCOL_FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002792 return e_invalid_argument;
2793
Gregory Anders3695d0e2023-09-29 20:17:20 +02002794 apply_keyprotocol(term, kpc);
2795
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002796 return NULL;
2797}
2798
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002799 int
2800expand_set_keyprotocol(optexpand_T *args, int *numMatches, char_u ***matches)
2801{
2802 expand_T *xp = args->oe_xp;
2803 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2804 {
2805 // 'keyprotocol' only has well-defined terms for completion for the
2806 // protocol part after the colon.
2807 return expand_set_opt_string(
2808 args,
2809 p_kpc_protocol_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002810 ARRAY_LENGTH(p_kpc_protocol_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002811 numMatches,
2812 matches);
2813 }
2814 // Use expand_set_opt_string instead of returning FAIL so that we can
2815 // include the original value if args->oe_include_orig_val is set.
2816 static char *(empty[]) = {NULL};
2817 return expand_set_opt_string(args, empty, 0, numMatches, matches);
2818}
2819
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002820/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002821 * The 'lispoptions' option is changed.
2822 */
2823 char *
2824did_set_lispoptions(optset_T *args)
2825{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002826 char_u **varp = (char_u **)args->os_varp;
2827
2828 if (**varp != NUL
2829 && STRCMP(*varp, "expr:0") != 0
2830 && STRCMP(*varp, "expr:1") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002831 return e_invalid_argument;
2832
2833 return NULL;
2834}
2835
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002836 int
2837expand_set_lispoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2838{
2839 static char *(p_lop_values[]) = {"expr:0", "expr:1", NULL};
2840 return expand_set_opt_string(
2841 args,
2842 p_lop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002843 ARRAY_LENGTH(p_lop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002844 numMatches,
2845 matches);
2846}
2847
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002848/*
2849 * The 'matchpairs' option is changed.
2850 */
2851 char *
2852did_set_matchpairs(optset_T *args)
2853{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002854 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002855 char_u *p;
2856
2857 if (has_mbyte)
2858 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002859 for (p = *varp; *p != NUL; ++p)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002860 {
2861 int x2 = -1;
2862 int x3 = -1;
2863
2864 p += mb_ptr2len(p);
2865 if (*p != NUL)
2866 x2 = *p++;
2867 if (*p != NUL)
2868 {
2869 x3 = mb_ptr2char(p);
2870 p += mb_ptr2len(p);
2871 }
2872 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
2873 return e_invalid_argument;
2874 if (*p == NUL)
2875 break;
2876 }
2877 }
2878 else
2879 {
2880 // Check for "x:y,x:y"
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002881 for (p = *varp; *p != NUL; p += 4)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002882 {
2883 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
2884 return e_invalid_argument;
2885 if (p[3] == NUL)
2886 break;
2887 }
2888 }
2889
2890 return NULL;
2891}
2892
2893#if defined(FEAT_SPELL) || defined(PROTO)
2894/*
2895 * The 'mkspellmem' option is changed.
2896 */
2897 char *
2898did_set_mkspellmem(optset_T *args UNUSED)
2899{
2900 if (spell_check_msm() != OK)
2901 return e_invalid_argument;
2902
2903 return NULL;
2904}
2905#endif
2906
2907/*
2908 * The 'mouse' option is changed.
2909 */
2910 char *
2911did_set_mouse(optset_T *args)
2912{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002913 char_u **varp = (char_u **)args->os_varp;
2914
2915 return did_set_option_listflag(*varp, (char_u *)MOUSE_ALL,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002916 args->os_errbuf);
2917}
2918
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002919 int
2920expand_set_mouse(optexpand_T *args, int *numMatches, char_u ***matches)
2921{
2922 return expand_set_opt_listflag(args, (char_u*)MOUSE_ALL, numMatches, matches);
2923}
2924
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002925/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002926 * The 'mousemodel' option is changed.
2927 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002928 char *
2929did_set_mousemodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002930{
2931 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
2932 return e_invalid_argument;
2933#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
2934 else if (*p_mousem != *oldval)
2935 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
2936 // to create or delete the popup menus.
2937 gui_motif_update_mousemodel(root_menu);
2938#endif
2939
2940 return NULL;
2941}
2942
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002943 int
2944expand_set_mousemodel(optexpand_T *args, int *numMatches, char_u ***matches)
2945{
2946 return expand_set_opt_string(
2947 args,
2948 p_mousem_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002949 ARRAY_LENGTH(p_mousem_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002950 numMatches,
2951 matches);
2952}
2953
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002954#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
2955 char *
2956did_set_mouseshape(optset_T *args UNUSED)
2957{
2958 char *errmsg = NULL;
2959
2960 errmsg = parse_shape_opt(SHAPE_MOUSE);
2961 update_mouseshape(-1);
2962
2963 return errmsg;
2964}
2965#endif
2966
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002967/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002968 * The 'nrformats' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002969 */
2970 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002971did_set_nrformats(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002972{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002973 char_u **varp = (char_u **)args->os_varp;
2974
2975 return did_set_opt_strings(*varp, p_nf_values, TRUE);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002976}
2977
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002978 int
2979expand_set_nrformats(optexpand_T *args, int *numMatches, char_u ***matches)
2980{
2981 return expand_set_opt_string(
2982 args,
2983 p_nf_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002984 ARRAY_LENGTH(p_nf_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002985 numMatches,
2986 matches);
2987}
2988
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002989#if defined(FEAT_EVAL) || defined(PROTO)
2990/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002991 * One of the '*expr' options is changed: 'balloonexpr', 'diffexpr',
2992 * 'foldexpr', 'foldtext', 'formatexpr', 'includeexpr', 'indentexpr',
2993 * 'patchexpr', 'printexpr' and 'charconvert'.
2994 *
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002995 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002996 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002997did_set_optexpr(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002998{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002999 char_u **varp = (char_u **)args->os_varp;
3000
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003001 // If the option value starts with <SID> or s:, then replace that with
3002 // the script identifier.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003003 char_u *name = get_scriptlocal_funcname(*varp);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003004 if (name != NULL)
3005 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003006 free_string_option(*varp);
3007 *varp = name;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003008 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003009
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003010 return NULL;
3011}
3012#endif
3013
3014/*
3015 * The 'pastetoggle' option is changed.
3016 */
3017 char *
3018did_set_pastetoggle(optset_T *args UNUSED)
3019{
3020 char_u *p;
3021
3022 // translate key codes like in a mapping
3023 if (*p_pt)
3024 {
zeertzjq7e0bae02023-08-11 23:15:38 +02003025 (void)replace_termcodes(p_pt, &p, 0,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003026 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
3027 if (p != NULL)
3028 {
3029 free_string_option(p_pt);
3030 p_pt = p;
3031 }
3032 }
3033
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003034 return NULL;
3035}
3036
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003037#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3038/*
3039 * The 'previewpopup' option is changed.
3040 */
3041 char *
3042did_set_previewpopup(optset_T *args UNUSED)
3043{
3044 if (parse_previewpopup(NULL) == FAIL)
3045 return e_invalid_argument;
3046
3047 return NULL;
3048}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003049
3050 int
3051expand_set_popupoption(optexpand_T *args, int *numMatches, char_u ***matches)
3052{
3053 expand_T *xp = args->oe_xp;
3054
3055 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3056 {
3057 // Within "highlight:"/"border:"/"align:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003058 int border_len = (int)STRLEN("border:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003059 if (xp->xp_pattern - args->oe_set_arg >= border_len &&
3060 STRNCMP(xp->xp_pattern - border_len, "border:", border_len) == 0)
3061 {
3062 return expand_set_opt_string(
3063 args,
3064 p_popup_option_border_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003065 ARRAY_LENGTH(p_popup_option_border_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003066 numMatches,
3067 matches);
3068 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003069 int align_len = (int)STRLEN("align:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003070 if (xp->xp_pattern - args->oe_set_arg >= align_len &&
3071 STRNCMP(xp->xp_pattern - align_len, "align:", align_len) == 0)
3072 {
3073 return expand_set_opt_string(
3074 args,
3075 p_popup_option_align_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003076 ARRAY_LENGTH(p_popup_option_align_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003077 numMatches,
3078 matches);
3079 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003080 int highlight_len = (int)STRLEN("highlight:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003081 if (xp->xp_pattern - args->oe_set_arg >= highlight_len &&
3082 STRNCMP(xp->xp_pattern - highlight_len, "highlight:", highlight_len) == 0)
3083 {
3084 // Return the list of all highlight names
3085 return expand_set_opt_generic(
3086 args,
3087 get_highlight_name,
3088 numMatches,
3089 matches);
3090 }
3091 return FAIL;
3092 }
3093
3094 return expand_set_opt_string(
3095 args,
3096 p_popup_option_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003097 ARRAY_LENGTH(p_popup_option_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003098 numMatches,
3099 matches);
3100}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003101#endif
3102
3103#if defined(FEAT_POSTSCRIPT) || defined(PROTO)
3104/*
3105 * The 'printencoding' option is changed.
3106 */
3107 char *
3108did_set_printencoding(optset_T *args UNUSED)
3109{
3110 char_u *s, *p;
3111
3112 // Canonize 'printencoding' if VIM standard one
3113 p = enc_canonize(p_penc);
3114 if (p != NULL)
3115 {
3116 vim_free(p_penc);
3117 p_penc = p;
3118 }
3119 else
3120 {
3121 // Ensure lower case and '-' for '_'
3122 for (s = p_penc; *s != NUL; s++)
3123 {
3124 if (*s == '_')
3125 *s = '-';
3126 else
3127 *s = TOLOWER_ASC(*s);
3128 }
3129 }
3130
3131 return NULL;
3132}
3133#endif
3134
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003135#if defined(FEAT_PRINTER) || defined(PROTO)
3136
3137 static char_u *
3138get_printoptions_names(expand_T *xp UNUSED, int idx)
3139{
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003140 if (idx >= (int)ARRAY_LENGTH(printer_opts))
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003141 return NULL;
3142 return (char_u*)printer_opts[idx].name;
3143}
3144
3145/*
3146 * Expand 'printoptions' option
3147 */
3148 int
3149expand_set_printoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3150{
3151 return expand_set_opt_generic(
3152 args,
3153 get_printoptions_names,
3154 numMatches,
3155 matches);
3156}
3157#endif
3158
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003159#if defined(FEAT_STL_OPT) || defined(PROTO)
3160/*
3161 * The 'statusline' or the 'tabline' or the 'rulerformat' option is changed.
3162 * "rulerformat" is TRUE if the 'rulerformat' option is changed.
3163 */
3164 static char *
3165parse_statustabline_rulerformat(optset_T *args, int rulerformat)
3166{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003167 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003168 char_u *s;
3169 char *errmsg = NULL;
3170 int wid;
3171
3172 if (rulerformat) // reset ru_wid first
3173 ru_wid = 0;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003174 s = *varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003175 if (rulerformat && *s == '%')
3176 {
3177 // set ru_wid if 'ruf' starts with "%99("
3178 if (*++s == '-') // ignore a '-'
3179 s++;
3180 wid = getdigits(&s);
3181 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
3182 ru_wid = wid;
3183 else
3184 errmsg = check_stl_option(p_ruf);
3185 }
3186 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
3187 else if (rulerformat || s[0] != '%' || s[1] != '!')
3188 errmsg = check_stl_option(s);
3189 if (rulerformat && errmsg == NULL)
3190 comp_col();
3191
3192 return errmsg;
3193}
3194#endif
3195
3196#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
3197/*
3198 * The 'renderoptions' option is changed.
3199 */
3200 char *
3201did_set_renderoptions(optset_T *args UNUSED)
3202{
3203 if (!gui_mch_set_rendering_options(p_rop))
3204 return e_invalid_argument;
3205
3206 return NULL;
3207}
3208#endif
3209
3210#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3211/*
3212 * The 'rightleftcmd' option is changed.
3213 */
3214 char *
3215did_set_rightleftcmd(optset_T *args)
3216{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003217 char_u **varp = (char_u **)args->os_varp;
3218
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003219 // Currently only "search" is a supported value.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003220 if (**varp != NUL && STRCMP(*varp, "search") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003221 return e_invalid_argument;
3222
3223 return NULL;
3224}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003225
3226 int
3227expand_set_rightleftcmd(optexpand_T *args, int *numMatches, char_u ***matches)
3228{
3229 static char *(p_rlc_values[]) = {"search", NULL};
3230 return expand_set_opt_string(
3231 args,
3232 p_rlc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003233 ARRAY_LENGTH(p_rlc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003234 numMatches,
3235 matches);
3236}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003237#endif
3238
3239#if defined(FEAT_STL_OPT) || defined(PROTO)
3240/*
3241 * The 'rulerformat' option is changed.
3242 */
3243 char *
3244did_set_rulerformat(optset_T *args)
3245{
3246 return parse_statustabline_rulerformat(args, TRUE);
3247}
3248#endif
3249
3250/*
3251 * The 'scrollopt' option is changed.
3252 */
3253 char *
3254did_set_scrollopt(optset_T *args UNUSED)
3255{
3256 return did_set_opt_strings(p_sbo, p_scbopt_values, TRUE);
3257}
3258
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003259 int
3260expand_set_scrollopt(optexpand_T *args, int *numMatches, char_u ***matches)
3261{
3262 return expand_set_opt_string(
3263 args,
3264 p_scbopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003265 ARRAY_LENGTH(p_scbopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003266 numMatches,
3267 matches);
3268}
3269
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003270/*
3271 * The 'selection' option is changed.
3272 */
3273 char *
3274did_set_selection(optset_T *args UNUSED)
3275{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003276 if (*p_sel == NUL || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003277 return e_invalid_argument;
3278
3279 return NULL;
3280}
3281
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003282 int
3283expand_set_selection(optexpand_T *args, int *numMatches, char_u ***matches)
3284{
3285 return expand_set_opt_string(
3286 args,
3287 p_sel_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003288 ARRAY_LENGTH(p_sel_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003289 numMatches,
3290 matches);
3291}
3292
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003293/*
3294 * The 'selectmode' option is changed.
3295 */
3296 char *
3297did_set_selectmode(optset_T *args UNUSED)
3298{
3299 return did_set_opt_strings(p_slm, p_slm_values, TRUE);
3300}
3301
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003302 int
3303expand_set_selectmode(optexpand_T *args, int *numMatches, char_u ***matches)
3304{
3305 return expand_set_opt_string(
3306 args,
3307 p_slm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003308 ARRAY_LENGTH(p_slm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003309 numMatches,
3310 matches);
3311}
3312
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003313#if defined(FEAT_SESSION) || defined(PROTO)
3314/*
3315 * The 'sessionoptions' option is changed.
3316 */
3317 char *
3318did_set_sessionoptions(optset_T *args)
3319{
3320 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
3321 return e_invalid_argument;
3322 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
3323 {
3324 // Don't allow both "sesdir" and "curdir".
3325 (void)opt_strings_flags(args->os_oldval.string, p_ssop_values,
3326 &ssop_flags, TRUE);
3327 return e_invalid_argument;
3328 }
3329
3330 return NULL;
3331}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003332
3333 int
3334expand_set_sessionoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3335{
3336 return expand_set_opt_string(
3337 args,
3338 p_ssop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003339 ARRAY_LENGTH(p_ssop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003340 numMatches,
3341 matches);
3342}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003343#endif
3344
3345/*
3346 * The 'shortmess' option is changed.
3347 */
3348 char *
3349did_set_shortmess(optset_T *args)
3350{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003351 char_u **varp = (char_u **)args->os_varp;
3352
3353 return did_set_option_listflag(*varp, (char_u *)SHM_ALL, args->os_errbuf);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003354}
3355
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003356 int
3357expand_set_shortmess(optexpand_T *args, int *numMatches, char_u ***matches)
3358{
3359 return expand_set_opt_listflag(args, (char_u*)SHM_ALL, numMatches, matches);
3360}
3361
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003362#if defined(FEAT_LINEBREAK) || defined(PROTO)
3363/*
3364 * The 'showbreak' option is changed.
3365 */
3366 char *
3367did_set_showbreak(optset_T *args)
3368{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003369 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003370 char_u *s;
3371
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003372 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003373 {
3374 if (ptr2cells(s) != 1)
3375 return e_showbreak_contains_unprintable_or_wide_character;
3376 MB_PTR_ADV(s);
3377 }
3378
3379 return NULL;
3380}
3381#endif
3382
3383/*
3384 * The 'showcmdloc' option is changed.
3385 */
3386 char *
3387did_set_showcmdloc(optset_T *args UNUSED)
3388{
3389 return did_set_opt_strings(p_sloc, p_sloc_values, FALSE);
3390}
3391
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003392 int
3393expand_set_showcmdloc(optexpand_T *args, int *numMatches, char_u ***matches)
3394{
3395 return expand_set_opt_string(
3396 args,
3397 p_sloc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003398 ARRAY_LENGTH(p_sloc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003399 numMatches,
3400 matches);
3401}
3402
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003403#if defined(FEAT_SIGNS) || defined(PROTO)
3404/*
3405 * The 'signcolumn' option is changed.
3406 */
3407 char *
3408did_set_signcolumn(optset_T *args)
3409{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003410 char_u **varp = (char_u **)args->os_varp;
3411
3412 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003413 return e_invalid_argument;
3414 // When changing the 'signcolumn' to or from 'number', recompute the
3415 // width of the number column if 'number' or 'relativenumber' is set.
3416 if (((*args->os_oldval.string == 'n' && args->os_oldval.string[1] == 'u')
3417 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
3418 && (curwin->w_p_nu || curwin->w_p_rnu))
3419 curwin->w_nrwidth_line_count = 0;
3420
3421 return NULL;
3422}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003423
3424 int
3425expand_set_signcolumn(optexpand_T *args, int *numMatches, char_u ***matches)
3426{
3427 return expand_set_opt_string(
3428 args,
3429 p_scl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003430 ARRAY_LENGTH(p_scl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003431 numMatches,
3432 matches);
3433}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003434#endif
3435
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003436#if defined(FEAT_SPELL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003437/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003438 * The 'spellcapcheck' option is changed.
3439 */
3440 char *
3441did_set_spellcapcheck(optset_T *args UNUSED)
3442{
3443 // compile the regexp program.
3444 return compile_cap_prog(curwin->w_s);
3445}
3446
3447/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003448 * The 'spellfile' option is changed.
3449 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003450 char *
3451did_set_spellfile(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003452{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003453 char_u **varp = (char_u **)args->os_varp;
3454
3455 if (!valid_spellfile(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003456 return e_invalid_argument;
3457
3458 // If there is a window for this buffer in which 'spell' is set load the
3459 // wordlists.
3460 return did_set_spell_option(TRUE);
3461}
3462
3463/*
3464 * The 'spell' option is changed.
3465 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003466 char *
3467did_set_spelllang(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003468{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003469 char_u **varp = (char_u **)args->os_varp;
3470
3471 if (!valid_spelllang(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003472 return e_invalid_argument;
3473
3474 // If there is a window for this buffer in which 'spell' is set load the
3475 // wordlists.
3476 return did_set_spell_option(FALSE);
3477}
3478
3479/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003480 * The 'spelloptions' option is changed.
3481 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003482 char *
3483did_set_spelloptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003484{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003485 char_u **varp = (char_u **)args->os_varp;
3486
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003487 if (**varp != NUL && STRCMP(*varp, "camel") != 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003488 return e_invalid_argument;
3489
3490 return NULL;
3491}
3492
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003493 int
3494expand_set_spelloptions(optexpand_T *args, int *numMatches, char_u ***matches)
3495{
3496 static char *(p_spo_values[]) = {"camel", NULL};
3497 return expand_set_opt_string(
3498 args,
3499 p_spo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003500 ARRAY_LENGTH(p_spo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003501 numMatches,
3502 matches);
3503}
3504
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003505/*
3506 * The 'spellsuggest' option is changed.
3507 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003508 char *
3509did_set_spellsuggest(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003510{
3511 if (spell_check_sps() != OK)
3512 return e_invalid_argument;
3513
3514 return NULL;
3515}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003516
3517 int
3518expand_set_spellsuggest(optexpand_T *args, int *numMatches, char_u ***matches)
3519{
3520 return expand_set_opt_string(
3521 args,
3522 p_sps_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003523 ARRAY_LENGTH(p_sps_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003524 numMatches,
3525 matches);
3526}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003527#endif
3528
3529/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003530 * The 'splitkeep' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003531 */
3532 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003533did_set_splitkeep(optset_T *args UNUSED)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003534{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003535 return did_set_opt_strings(p_spk, p_spk_values, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003536}
3537
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003538 int
3539expand_set_splitkeep(optexpand_T *args, int *numMatches, char_u ***matches)
3540{
3541 return expand_set_opt_string(
3542 args,
3543 p_spk_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003544 ARRAY_LENGTH(p_spk_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003545 numMatches,
3546 matches);
3547}
3548
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00003549#if defined(FEAT_STL_OPT) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003550/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003551 * The 'statusline' option is changed.
3552 */
3553 char *
3554did_set_statusline(optset_T *args)
3555{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003556 return parse_statustabline_rulerformat(args, FALSE);
3557}
3558#endif
3559
3560/*
3561 * The 'swapsync' option is changed.
3562 */
3563 char *
3564did_set_swapsync(optset_T *args UNUSED)
3565{
3566 return did_set_opt_strings(p_sws, p_sws_values, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003567}
3568
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003569 int
3570expand_set_swapsync(optexpand_T *args, int *numMatches, char_u ***matches)
3571{
3572 return expand_set_opt_string(
3573 args,
3574 p_sws_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003575 ARRAY_LENGTH(p_sws_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003576 numMatches,
3577 matches);
3578}
3579
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003580/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003581 * The 'switchbuf' option is changed.
3582 */
3583 char *
3584did_set_switchbuf(optset_T *args UNUSED)
3585{
3586 return did_set_opt_flags(p_swb, p_swb_values, &swb_flags, TRUE);
3587}
3588
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003589 int
3590expand_set_switchbuf(optexpand_T *args, int *numMatches, char_u ***matches)
3591{
3592 return expand_set_opt_string(
3593 args,
3594 p_swb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003595 ARRAY_LENGTH(p_swb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003596 numMatches,
3597 matches);
3598}
3599
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003600#if defined(FEAT_STL_OPT) || defined(PROTO)
3601/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003602 * The 'tabline' option is changed.
3603 */
3604 char *
3605did_set_tabline(optset_T *args)
3606{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003607 return parse_statustabline_rulerformat(args, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003608}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003609#endif
3610
3611/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003612 * The 'tagcase' option is changed.
3613 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003614 char *
3615did_set_tagcase(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003616{
3617 unsigned int *flags;
3618 char_u *p;
3619
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003620 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003621 {
3622 p = curbuf->b_p_tc;
3623 flags = &curbuf->b_tc_flags;
3624 }
3625 else
3626 {
3627 p = p_tc;
3628 flags = &tc_flags;
3629 }
3630
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003631 if ((args->os_flags & OPT_LOCAL) && *p == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003632 // make the local value empty: use the global value
3633 *flags = 0;
3634 else if (*p == NUL
3635 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
3636 return e_invalid_argument;
3637
3638 return NULL;
3639}
3640
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003641 int
3642expand_set_tagcase(optexpand_T *args, int *numMatches, char_u ***matches)
3643{
3644 return expand_set_opt_string(
3645 args,
3646 p_tc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003647 ARRAY_LENGTH(p_tc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003648 numMatches,
3649 matches);
3650}
3651
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003652/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003653 * The 'term' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003654 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003655 char *
3656did_set_term(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003657{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003658 if (T_NAME[0] == NUL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003659 return e_cannot_set_term_to_empty_string;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003660#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003661 if (gui.in_use)
3662 return e_cannot_change_term_in_GUI;
3663 if (term_is_gui(T_NAME))
3664 return e_use_gui_to_start_GUI;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003665#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003666 if (set_termname(T_NAME) == FAIL)
3667 return e_not_found_in_termcap;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003668
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003669 // Screen colors may have changed.
3670 redraw_later_clear();
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003671
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003672 return NULL;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003673}
3674
3675/*
3676 * Some terminal option (t_xxx) is changed
3677 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003678 char *
3679did_set_term_option(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003680{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003681 char_u **varp = (char_u **)args->os_varp;
3682
3683 if (!full_screen)
3684 return NULL;
3685
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003686 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
3687 if (varp == &T_CCO)
3688 {
3689 int colors = atoi((char *)T_CCO);
3690
3691 // Only reinitialize colors if t_Co value has really changed to
3692 // avoid expensive reload of colorscheme if t_Co is set to the
3693 // same value multiple times.
3694 if (colors != t_colors)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003695 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003696 t_colors = colors;
3697 if (t_colors <= 1)
3698 {
3699 vim_free(T_CCO);
3700 T_CCO = empty_option;
3701 }
3702#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3703 if (is_term_win32())
3704 {
3705 swap_tcap();
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003706 args->os_did_swaptcap = TRUE;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003707 }
3708#endif
3709 // We now have a different color setup, initialize it again.
3710 init_highlight(TRUE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003711 }
3712 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003713 ttest(FALSE);
3714 if (varp == &T_ME)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003715 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003716 out_str(T_ME);
3717 redraw_later(UPD_CLEAR);
3718#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3719 // Since t_me has been set, this probably means that the user
3720 // wants to use this as default colors. Need to reset default
3721 // background/foreground colors.
3722# ifdef VIMDLL
3723 if (!gui.in_use && !gui.starting)
3724# endif
3725 mch_set_normal_colors();
3726#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003727 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003728 if (varp == &T_BE && termcap_active)
3729 {
3730 MAY_WANT_TO_LOG_THIS;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003731
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003732 if (*T_BE == NUL)
3733 // When clearing t_BE we assume the user no longer wants
3734 // bracketed paste, thus disable it by writing t_BD.
3735 out_str(T_BD);
3736 else
3737 out_str(T_BE);
3738 }
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003739
3740 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003741}
3742
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003743#if defined(FEAT_TERMINAL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003744/*
3745 * The 'termwinkey' option is changed.
3746 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003747 char *
3748did_set_termwinkey(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003749{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003750 if (*curwin->w_p_twk != NUL && string_to_key(curwin->w_p_twk, TRUE) == 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003751 return e_invalid_argument;
3752
3753 return NULL;
3754}
3755
3756/*
3757 * The 'termwinsize' option is changed.
3758 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003759 char *
3760did_set_termwinsize(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003761{
3762 char_u *p;
3763
3764 if (*curwin->w_p_tws == NUL)
3765 return NULL;
3766
3767 p = skipdigits(curwin->w_p_tws);
3768 if (p == curwin->w_p_tws
3769 || (*p != 'x' && *p != '*')
3770 || *skipdigits(p + 1) != NUL)
3771 return e_invalid_argument;
3772
3773 return NULL;
3774}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003775
3776# if defined(MSWIN) || defined(PROTO)
3777/*
3778 * The 'termwintype' option is changed.
3779 */
3780 char *
3781did_set_termwintype(optset_T *args UNUSED)
3782{
3783 return did_set_opt_strings(p_twt, p_twt_values, FALSE);
3784}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003785
3786 int
3787expand_set_termwintype(optexpand_T *args, int *numMatches, char_u ***matches)
3788{
3789 return expand_set_opt_string(
3790 args,
3791 p_twt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003792 ARRAY_LENGTH(p_twt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003793 numMatches,
3794 matches);
3795}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003796# endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003797#endif
3798
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003799/*
3800 * The 'titlestring' option is changed.
3801 */
3802 char *
3803did_set_titlestring(optset_T *args)
3804{
3805 int flagval = 0;
3806
3807#ifdef FEAT_STL_OPT
3808 flagval = STL_IN_TITLE;
3809#endif
3810 return parse_titleiconstring(args, flagval);
3811}
3812
3813#if (defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)) || defined(PROTO)
3814/*
3815 * The 'toolbar' option is changed.
3816 */
3817 char *
3818did_set_toolbar(optset_T *args UNUSED)
3819{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003820 if (opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags,
3821 TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003822 return e_invalid_argument;
3823
3824 out_flush();
3825 gui_mch_show_toolbar((toolbar_flags &
3826 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
3827 return NULL;
3828}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003829
3830 int
3831expand_set_toolbar(optexpand_T *args, int *numMatches, char_u ***matches)
3832{
3833 return expand_set_opt_string(
3834 args,
3835 p_toolbar_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003836 ARRAY_LENGTH(p_toolbar_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003837 numMatches,
3838 matches);
3839}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003840#endif
3841
3842#if (defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)) || defined(PROTO)
3843/*
3844 * The 'toolbariconsize' option is changed. GTK+ 2 only.
3845 */
3846 char *
3847did_set_toolbariconsize(optset_T *args UNUSED)
3848{
3849 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
3850 return e_invalid_argument;
3851
3852 out_flush();
3853 gui_mch_show_toolbar((toolbar_flags &
3854 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
3855 return NULL;
3856}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003857
3858 int
3859expand_set_toolbariconsize(optexpand_T *args, int *numMatches, char_u ***matches)
3860{
3861 return expand_set_opt_string(
3862 args,
3863 p_tbis_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003864 ARRAY_LENGTH(p_tbis_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003865 numMatches,
3866 matches);
3867}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003868#endif
3869
3870#if defined(UNIX) || defined(VMS) || defined(PROTO)
3871/*
3872 * The 'ttymouse' option is changed.
3873 */
3874 char *
3875did_set_ttymouse(optset_T *args UNUSED)
3876{
3877 char *errmsg = NULL;
3878
3879 // Switch the mouse off before changing the escape sequences used for
3880 // that.
3881 mch_setmouse(FALSE);
3882 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
3883 errmsg = e_invalid_argument;
3884 else
3885 check_mouse_termcode();
3886 if (termcap_active)
3887 setmouse(); // may switch it on again
3888
3889 return errmsg;
3890}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003891
3892 int
3893expand_set_ttymouse(optexpand_T *args, int *numMatches, char_u ***matches)
3894{
3895 return expand_set_opt_string(
3896 args,
3897 p_ttym_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003898 ARRAY_LENGTH(p_ttym_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003899 numMatches,
3900 matches);
3901}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003902#endif
3903
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003904#if defined(FEAT_VARTABS) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003905/*
3906 * The 'varsofttabstop' option is changed.
3907 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003908 char *
3909did_set_varsofttabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003910{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003911 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003912 char_u *cp;
3913
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003914 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00003915 VIM_CLEAR(curbuf->b_p_vsts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003916 else
3917 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003918 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003919 {
3920 if (vim_isdigit(*cp))
3921 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003922 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003923 continue;
3924 return e_invalid_argument;
3925 }
3926
3927 int *oldarray = curbuf->b_p_vsts_array;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003928 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003929 {
3930 if (oldarray)
3931 vim_free(oldarray);
3932 }
3933 else
3934 return e_invalid_argument;
3935 }
3936
3937 return NULL;
3938}
3939
3940/*
3941 * The 'vartabstop' option is changed.
3942 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003943 char *
3944did_set_vartabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003945{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003946 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003947 char_u *cp;
3948
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003949 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00003950 VIM_CLEAR(curbuf->b_p_vts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003951 else
3952 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003953 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003954 {
3955 if (vim_isdigit(*cp))
3956 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003957 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003958 continue;
3959 return e_invalid_argument;
3960 }
3961
3962 int *oldarray = curbuf->b_p_vts_array;
3963
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003964 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003965 {
3966 vim_free(oldarray);
3967# ifdef FEAT_FOLDING
3968 if (foldmethodIsIndent(curwin))
3969 foldUpdateAll(curwin);
3970# endif
3971 }
3972 else
3973 return e_invalid_argument;
3974 }
3975
3976 return NULL;
3977}
3978#endif
3979
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003980/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003981 * The 'verbosefile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003982 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003983 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003984did_set_verbosefile(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003985{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003986 verbose_stop();
3987 if (*p_vfile != NUL && verbose_open() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003988 return e_invalid_argument;
3989
3990 return NULL;
3991}
3992
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003993#if defined(FEAT_SESSION) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003994/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003995 * The 'viewoptions' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003996 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003997 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003998did_set_viewoptions(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003999{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004000 return did_set_opt_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004001}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004002#endif
4003
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004004#if defined(FEAT_VIMINFO) || defined(PROTO)
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004005/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004006 * The 'viminfo' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004007 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004008 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004009did_set_viminfo(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004010{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004011 char_u *s;
4012 char *errmsg = NULL;
4013
4014 for (s = p_viminfo; *s;)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004015 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004016 // Check it's a valid character
4017 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
4018 {
4019 errmsg = illegal_char(args->os_errbuf, *s);
4020 break;
4021 }
4022 if (*s == 'n') // name is always last one
4023 break;
4024 else if (*s == 'r') // skip until next ','
4025 {
4026 while (*++s && *s != ',')
4027 ;
4028 }
4029 else if (*s == '%')
4030 {
4031 // optional number
4032 while (vim_isdigit(*++s))
4033 ;
4034 }
4035 else if (*s == '!' || *s == 'h' || *s == 'c')
4036 ++s; // no extra chars
4037 else // must have a number
4038 {
4039 while (vim_isdigit(*++s))
4040 ;
4041
4042 if (!VIM_ISDIGIT(*(s - 1)))
4043 {
4044 if (args->os_errbuf != NULL)
4045 {
4046 sprintf(args->os_errbuf,
4047 _(e_missing_number_after_angle_str_angle),
4048 transchar_byte(*(s - 1)));
4049 errmsg = args->os_errbuf;
4050 }
4051 else
4052 errmsg = "";
4053 break;
4054 }
4055 }
4056 if (*s == ',')
4057 ++s;
4058 else if (*s)
4059 {
4060 if (args->os_errbuf != NULL)
4061 errmsg = e_missing_comma;
4062 else
4063 errmsg = "";
4064 break;
4065 }
4066 }
4067 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
4068 errmsg = e_must_specify_a_value;
4069
4070 return errmsg;
4071}
4072#endif
4073
4074/*
4075 * The 'virtualedit' option is changed.
4076 */
4077 char *
4078did_set_virtualedit(optset_T *args)
4079{
4080 char_u *ve = p_ve;
4081 unsigned int *flags = &ve_flags;
4082
4083 if (args->os_flags & OPT_LOCAL)
4084 {
4085 ve = curwin->w_p_ve;
4086 flags = &curwin->w_ve_flags;
4087 }
4088
4089 if ((args->os_flags & OPT_LOCAL) && *ve == NUL)
4090 // make the local value empty: use the global value
4091 *flags = 0;
4092 else
4093 {
4094 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
4095 return e_invalid_argument;
4096 else if (STRCMP(ve, args->os_oldval.string) != 0)
4097 {
4098 // Recompute cursor position in case the new 've' setting
4099 // changes something.
4100 validate_virtcol();
4101 coladvance(curwin->w_virtcol);
4102 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004103 }
4104
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004105 return NULL;
4106}
4107
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004108 int
4109expand_set_virtualedit(optexpand_T *args, int *numMatches, char_u ***matches)
4110{
4111 return expand_set_opt_string(
4112 args,
4113 p_ve_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004114 ARRAY_LENGTH(p_ve_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004115 numMatches,
4116 matches);
4117}
4118
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004119/*
4120 * The 'whichwrap' option is changed.
4121 */
4122 char *
4123did_set_whichwrap(optset_T *args)
4124{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004125 char_u **varp = (char_u **)args->os_varp;
4126
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004127 // Add ',' to the list flags because 'whichwrap' is a flag
4128 // list that is comma-separated.
4129 return did_set_option_listflag(*varp, (char_u *)(WW_ALL ","), args->os_errbuf);
4130}
4131
4132 int
4133expand_set_whichwrap(optexpand_T *args, int *numMatches, char_u ***matches)
4134{
4135 return expand_set_opt_listflag(args, (char_u*)WW_ALL, numMatches, matches);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004136}
4137
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004138/*
4139 * The 'wildmode' option is changed.
4140 */
4141 char *
4142did_set_wildmode(optset_T *args UNUSED)
4143{
4144 if (check_opt_wim() == FAIL)
4145 return e_invalid_argument;
4146 return NULL;
4147}
4148
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004149 int
4150expand_set_wildmode(optexpand_T *args, int *numMatches, char_u ***matches)
4151{
4152 return expand_set_opt_string(
4153 args,
4154 p_wim_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004155 ARRAY_LENGTH(p_wim_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004156 numMatches,
4157 matches);
4158}
4159
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004160/*
4161 * The 'wildoptions' option is changed.
4162 */
4163 char *
4164did_set_wildoptions(optset_T *args UNUSED)
4165{
4166 return did_set_opt_strings(p_wop, p_wop_values, TRUE);
4167}
4168
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004169 int
4170expand_set_wildoptions(optexpand_T *args, int *numMatches, char_u ***matches)
4171{
4172 return expand_set_opt_string(
4173 args,
4174 p_wop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004175 ARRAY_LENGTH(p_wop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004176 numMatches,
4177 matches);
4178}
4179
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004180#if defined(FEAT_WAK) || defined(PROTO)
4181/*
4182 * The 'winaltkeys' option is changed.
4183 */
4184 char *
4185did_set_winaltkeys(optset_T *args UNUSED)
4186{
4187 char *errmsg = NULL;
4188
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004189 if (*p_wak == NUL || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004190 errmsg = e_invalid_argument;
4191# ifdef FEAT_MENU
4192# if defined(FEAT_GUI_MOTIF)
4193 else if (gui.in_use)
4194 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4195# elif defined(FEAT_GUI_GTK)
4196 else if (gui.in_use)
4197 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4198# endif
4199# endif
4200 return errmsg;
4201}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004202
4203 int
4204expand_set_winaltkeys(optexpand_T *args, int *numMatches, char_u ***matches)
4205{
4206 return expand_set_opt_string(
4207 args,
4208 p_wak_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004209 ARRAY_LENGTH(p_wak_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004210 numMatches,
4211 matches);
4212}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004213#endif
4214
4215/*
4216 * The 'wincolor' option is changed.
4217 */
4218 char *
4219did_set_wincolor(optset_T *args UNUSED)
4220{
4221#ifdef FEAT_TERMINAL
4222 term_update_wincolor(curwin);
4223#endif
4224 return NULL;
4225}
4226
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004227 int
4228expand_set_wincolor(optexpand_T *args, int *numMatches, char_u ***matches)
4229{
4230 return expand_set_opt_generic(
4231 args,
4232 get_highlight_name,
4233 numMatches,
4234 matches);
4235}
4236
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004237#ifdef FEAT_SYN_HL
4238/*
4239 * When the 'syntax' option is set, load the syntax of that name.
4240 */
4241 static void
4242do_syntax_autocmd(int value_changed)
4243{
4244 static int syn_recursive = 0;
4245
4246 ++syn_recursive;
4247 // Only pass TRUE for "force" when the value changed or not used
4248 // recursively, to avoid endless recurrence.
4249 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
4250 value_changed || syn_recursive == 1, curbuf);
4251 curbuf->b_flags |= BF_SYN_SET;
4252 --syn_recursive;
4253}
4254#endif
4255
4256/*
4257 * When the 'filetype' option is set, trigger the FileType autocommand.
4258 */
4259 static void
4260do_filetype_autocmd(char_u **varp, int opt_flags, int value_changed)
4261{
4262 // Skip this when called from a modeline and the filetype was already set
4263 // to this value.
4264 if ((opt_flags & OPT_MODELINE) && !value_changed)
4265 return;
4266
4267 static int ft_recursive = 0;
4268 int secure_save = secure;
4269
4270 // Reset the secure flag, since the value of 'filetype' has
4271 // been checked to be safe.
4272 secure = 0;
4273
4274 ++ft_recursive;
4275 did_filetype = TRUE;
4276 // Only pass TRUE for "force" when the value changed or not
4277 // used recursively, to avoid endless recurrence.
4278 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
4279 value_changed || ft_recursive == 1, curbuf);
4280 --ft_recursive;
4281 // Just in case the old "curbuf" is now invalid.
4282 if (varp != &(curbuf->b_p_ft))
4283 varp = NULL;
4284
4285 secure = secure_save;
4286}
4287
4288#ifdef FEAT_SPELL
4289/*
4290 * When the 'spelllang' option is set, source the spell/LANG.vim file in
4291 * 'runtimepath'.
4292 */
4293 static void
4294do_spelllang_source(void)
4295{
4296 char_u fname[200];
4297 char_u *p;
4298 char_u *q = curwin->w_s->b_p_spl;
4299
4300 // Skip the first name if it is "cjk".
4301 if (STRNCMP(q, "cjk,", 4) == 0)
4302 q += 4;
4303
4304 // They could set 'spellcapcheck' depending on the language. Use the first
4305 // name in 'spelllang' up to '_region' or '.encoding'.
4306 for (p = q; *p != NUL; ++p)
4307 if (!ASCII_ISALNUM(*p) && *p != '-')
4308 break;
4309 if (p > q)
4310 {
4311 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
4312 (int)(p - q), q);
4313 source_runtime(fname, DIP_ALL);
4314 }
4315}
4316#endif
4317
4318/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004319 * Handle string options that need some action to perform when changed.
zeertzjqf6782732022-07-27 18:26:03 +01004320 * The new value must be allocated.
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004321 * Returns NULL for success, or an untranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +02004322 */
4323 char *
4324did_set_string_option(
4325 int opt_idx, // index in options[] table
4326 char_u **varp, // pointer to the option variable
Bram Moolenaardac13472019-09-16 21:06:21 +02004327 char_u *oldval, // previous value of the option
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004328 char_u *value, // new value of the option
Bram Moolenaardac13472019-09-16 21:06:21 +02004329 char *errbuf, // buffer for errors, or NULL
4330 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004331 set_op_T op, // OP_ADDING/OP_PREPENDING/OP_REMOVING
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004332 int *value_checked) // value was checked to be safe, no
Bram Moolenaardac13472019-09-16 21:06:21 +02004333 // need to set P_INSECURE
4334{
4335 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +02004336 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004337 opt_did_set_cb_T did_set_cb = get_option_did_set_cb(opt_idx);
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004338 optset_T args;
4339
4340 // 'ttytype' is an alias for 'term'. Both 'term' and 'ttytype' point to
4341 // T_NAME. If 'term' or 'ttytype' is modified, then use the index for the
4342 // 'term' option. Only set the P_ALLOCED flag on 'term'.
4343 if (varp == &T_NAME)
4344 {
4345 opt_idx = findoption((char_u *)"term");
4346 if (opt_idx >= 0)
4347 {
4348 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
4349 did_set_cb = get_option_did_set_cb(opt_idx);
4350 }
4351 }
4352
4353 CLEAR_FIELD(args);
Bram Moolenaardac13472019-09-16 21:06:21 +02004354
Bram Moolenaardac13472019-09-16 21:06:21 +02004355 // Disallow changing some options from secure mode
4356 if ((secure
4357#ifdef HAVE_SANDBOX
4358 || sandbox != 0
4359#endif
4360 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00004361 errmsg = e_not_allowed_here;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004362 // Check for a "normal" directory or file name in some options.
4363 else if (check_illegal_path_names(opt_idx, varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004364 errmsg = e_invalid_argument;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004365 else if (did_set_cb != NULL)
4366 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004367 args.os_varp = (char_u *)varp;
4368 args.os_idx = opt_idx;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004369 args.os_flags = opt_flags;
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004370 args.os_op = op;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004371 args.os_oldval.string = oldval;
4372 args.os_newval.string = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004373 args.os_errbuf = errbuf;
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004374 // Invoke the option specific callback function to validate and apply
4375 // the new option value.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004376 errmsg = did_set_cb(&args);
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004377
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004378 // The 'keymap', 'filetype' and 'syntax' option callback functions
4379 // may change the os_value_checked field.
4380 *value_checked = args.os_value_checked;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004381 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004382
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004383 // If an error is detected, restore the previous value.
Bram Moolenaardac13472019-09-16 21:06:21 +02004384 if (errmsg != NULL)
4385 {
zeertzjqf6782732022-07-27 18:26:03 +01004386 free_string_option(*varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02004387 *varp = oldval;
4388 // When resetting some values, need to act on it.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004389 if (args.os_restore_chartab)
Bram Moolenaardac13472019-09-16 21:06:21 +02004390 (void)init_chartab();
4391 if (varp == &p_hl)
4392 (void)highlight_changed();
4393 }
4394 else
4395 {
4396#ifdef FEAT_EVAL
4397 // Remember where the option was set.
4398 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4399#endif
4400 // Free string options that are in allocated memory.
4401 // Use "free_oldval", because recursiveness may change the flags under
4402 // our fingers (esp. init_highlight()).
4403 if (free_oldval)
4404 free_string_option(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01004405 set_option_flag(opt_idx, P_ALLOCED);
Bram Moolenaardac13472019-09-16 21:06:21 +02004406
4407 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
4408 && is_global_local_option(opt_idx))
4409 {
4410 // global option with local value set to use global value; free
4411 // the local value and make it empty
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004412 char_u *p = get_option_varp_scope(opt_idx, OPT_LOCAL);
Bram Moolenaardac13472019-09-16 21:06:21 +02004413 free_string_option(*(char_u **)p);
4414 *(char_u **)p = empty_option;
4415 }
4416
4417 // May set global value for local option.
4418 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
4419 set_string_option_global(opt_idx, varp);
4420
4421 // Trigger the autocommand only after setting the flags.
4422#ifdef FEAT_SYN_HL
Bram Moolenaardac13472019-09-16 21:06:21 +02004423 if (varp == &(curbuf->b_p_syn))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004424 do_syntax_autocmd(args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004425#endif
4426 else if (varp == &(curbuf->b_p_ft))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004427 do_filetype_autocmd(varp, opt_flags, args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004428#ifdef FEAT_SPELL
4429 if (varp == &(curwin->w_s->b_p_spl))
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004430 do_spelllang_source();
Bram Moolenaardac13472019-09-16 21:06:21 +02004431#endif
4432 }
4433
Bram Moolenaardac13472019-09-16 21:06:21 +02004434 if (varp == &p_mouse)
4435 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004436 if (*p_mouse == NUL)
4437 mch_setmouse(FALSE); // switch mouse off
4438 else
Bram Moolenaardac13472019-09-16 21:06:21 +02004439 setmouse(); // in case 'mouse' changed
4440 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004441
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004442#if defined(FEAT_LUA) || defined(PROTO)
4443 if (varp == &p_rtp)
4444 update_package_paths_in_lua();
4445#endif
4446
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004447#if defined(FEAT_LINEBREAK)
4448 // Changing Formatlistpattern when briopt includes the list setting:
4449 // redraw
4450 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
4451 && curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004452 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004453#endif
4454
Bram Moolenaardac13472019-09-16 21:06:21 +02004455 if (curwin->w_curswant != MAXCOL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004456 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02004457 curwin->w_set_curswant = TRUE;
4458
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004459 if ((opt_flags & OPT_NO_REDRAW) == 0)
4460 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004461#ifdef FEAT_GUI
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004462 // set when changing an option that only requires a redraw in the GUI
4463 int redraw_gui_only = FALSE;
4464
4465 if (varp == &p_go // 'guioptions'
4466 || varp == &p_guifont // 'guifont'
4467# ifdef FEAT_GUI_TABLINE
4468 || varp == &p_gtl // 'guitablabel'
4469 || varp == &p_gtt // 'guitabtooltip'
4470# endif
4471# ifdef FEAT_XFONTSET
4472 || varp == &p_guifontset // 'guifontset'
4473# endif
4474 || varp == &p_guifontwide // 'guifontwide'
4475# ifdef FEAT_GUI_GTK
4476 || varp == &p_guiligatures // 'guiligatures'
4477# endif
4478 )
4479 redraw_gui_only = TRUE;
4480
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004481 // check redraw when it's not a GUI option or the GUI is active.
4482 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02004483#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004484 check_redraw(get_option_flags(opt_idx));
4485 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004486
4487#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004488 if (args.os_did_swaptcap)
Bram Moolenaardac13472019-09-16 21:06:21 +02004489 {
4490 set_termname((char_u *)"win32");
4491 init_highlight(TRUE, FALSE);
4492 }
4493#endif
4494
4495 return errmsg;
4496}
4497
4498/*
4499 * Check an option that can be a range of string values.
4500 *
4501 * Return OK for correct value, FAIL otherwise.
4502 * Empty is always OK.
4503 */
4504 static int
4505check_opt_strings(
4506 char_u *val,
4507 char **values,
4508 int list) // when TRUE: accept a list of values
4509{
4510 return opt_strings_flags(val, values, NULL, list);
4511}
4512
4513/*
4514 * Handle an option that can be a range of string values.
4515 * Set a flag in "*flagp" for each string present.
4516 *
4517 * Return OK for correct value, FAIL otherwise.
4518 * Empty is always OK.
4519 */
4520 static int
4521opt_strings_flags(
4522 char_u *val, // new value
4523 char **values, // array of valid string values
4524 unsigned *flagp,
4525 int list) // when TRUE: accept a list of values
4526{
4527 int i;
4528 int len;
4529 unsigned new_flags = 0;
4530
4531 while (*val)
4532 {
4533 for (i = 0; ; ++i)
4534 {
4535 if (values[i] == NULL) // val not found in values[]
4536 return FAIL;
4537
4538 len = (int)STRLEN(values[i]);
4539 if (STRNCMP(values[i], val, len) == 0
4540 && ((list && val[len] == ',') || val[len] == NUL))
4541 {
4542 val += len + (val[len] == ',');
4543 new_flags |= (1 << i);
4544 break; // check next item in val list
4545 }
4546 }
4547 }
4548 if (flagp != NULL)
4549 *flagp = new_flags;
4550
4551 return OK;
4552}
4553
4554/*
4555 * return OK if "p" is a valid fileformat name, FAIL otherwise.
4556 */
4557 int
4558check_ff_value(char_u *p)
4559{
4560 return check_opt_strings(p, p_ff_values, FALSE);
4561}
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004562
4563/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004564 * Save the actual shortmess Flags and clear them temporarily to avoid that
4565 * file messages overwrites any output from the following commands.
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004566 *
4567 * Caller must make sure to first call save_clear_shm_value() and then
4568 * restore_shm_value() exactly the same number of times.
4569 */
4570 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004571save_clear_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004572{
4573 if (STRLEN(p_shm) >= SHM_LEN)
4574 {
4575 iemsg(e_internal_error_shortmess_too_long);
4576 return;
4577 }
4578
4579 if (++set_shm_recursive == 1)
4580 {
4581 STRCPY(shm_buf, p_shm);
4582 set_option_value_give_err((char_u *)"shm", 0L, (char_u *)"", 0);
4583 }
4584}
4585
4586/*
4587 * Restore the shortmess Flags set from the save_clear_shm_value() function.
4588 */
4589 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004590restore_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004591{
4592 if (--set_shm_recursive == 0)
4593 {
4594 set_option_value_give_err((char_u *)"shm", 0L, shm_buf, 0);
4595 vim_memset(shm_buf, 0, SHM_LEN);
4596 }
4597}