blob: 8458f2a4a4cbd20a299e22ced06af6bf8dae716e [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/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002108 * Function given to ExpandGeneric() to obtain the possible arguments of the
2109 * fileformat options.
2110 */
2111 char_u *
2112get_fileformat_name(expand_T *xp UNUSED, int idx)
2113{
2114 if (idx >= (int)ARRAY_LENGTH(p_ff_values))
2115 return NULL;
2116
2117 return (char_u*)p_ff_values[idx];
2118}
2119
2120/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002121 * The 'fileformats' option is changed.
2122 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002123 char *
2124did_set_fileformats(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002125{
2126 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
2127 return e_invalid_argument;
2128
2129 // also change 'textauto'
2130 if (*p_ffs == NUL)
2131 p_ta = FALSE;
2132 else
2133 p_ta = TRUE;
2134
2135 return NULL;
2136}
2137
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002138/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002139 * The 'filetype' or the 'syntax' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002140 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002141 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002142did_set_filetype_or_syntax(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002143{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002144 char_u **varp = (char_u **)args->os_varp;
2145
2146 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002147 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002148
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002149 args->os_value_changed = STRCMP(args->os_oldval.string, *varp) != 0;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002150
2151 // Since we check the value, there is no need to set P_INSECURE,
2152 // even when the value comes from a modeline.
2153 args->os_value_checked = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002154
2155 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002156}
2157
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002158#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002159/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002160 * The 'foldclose' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002161 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002162 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002163did_set_foldclose(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002164{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002165 return did_set_opt_strings(p_fcl, p_fcl_values, TRUE);
2166}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002167
2168 int
2169expand_set_foldclose(optexpand_T *args, int *numMatches, char_u ***matches)
2170{
2171 return expand_set_opt_string(
2172 args,
2173 p_fcl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002174 ARRAY_LENGTH(p_fcl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002175 numMatches,
2176 matches);
2177}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002178#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002179
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002180#if (defined(FEAT_EVAL) && defined(FEAT_FOLDING)) || defined(PROTO)
2181/*
2182 * The 'foldexpr' option is changed.
2183 */
2184 char *
2185did_set_foldexpr(optset_T *args)
2186{
2187 (void)did_set_optexpr(args);
2188 if (foldmethodIsExpr(curwin))
2189 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002190 return NULL;
2191}
2192#endif
2193
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002194#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002195/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002196 * The 'foldignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002197 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002198 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002199did_set_foldignore(optset_T *args UNUSED)
2200{
2201 if (foldmethodIsIndent(curwin))
2202 foldUpdateAll(curwin);
2203 return NULL;
2204}
2205
2206/*
2207 * The 'foldmarker' option is changed.
2208 */
2209 char *
2210did_set_foldmarker(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002211{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002212 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002213 char_u *p;
2214
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002215 p = vim_strchr(*varp, ',');
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002216 if (p == NULL)
2217 return e_comma_required;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002218 else if (p == *varp || p[1] == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002219 return e_invalid_argument;
2220 else if (foldmethodIsMarker(curwin))
2221 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002222
2223 return NULL;
2224}
2225
2226/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002227 * The 'foldmethod' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002228 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002229 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002230did_set_foldmethod(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002231{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002232 char_u **varp = (char_u **)args->os_varp;
2233
2234 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002235 || *curwin->w_p_fdm == NUL)
2236 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002237
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002238 foldUpdateAll(curwin);
2239 if (foldmethodIsDiff(curwin))
2240 newFoldLevel();
2241 return NULL;
2242}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002243
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002244 int
2245expand_set_foldmethod(optexpand_T *args, int *numMatches, char_u ***matches)
2246{
2247 return expand_set_opt_string(
2248 args,
2249 p_fdm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002250 ARRAY_LENGTH(p_fdm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002251 numMatches,
2252 matches);
2253}
2254
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002255/*
2256 * The 'foldopen' option is changed.
2257 */
2258 char *
2259did_set_foldopen(optset_T *args UNUSED)
2260{
2261 return did_set_opt_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
2262}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002263
2264 int
2265expand_set_foldopen(optexpand_T *args, int *numMatches, char_u ***matches)
2266{
2267 return expand_set_opt_string(
2268 args,
2269 p_fdo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002270 ARRAY_LENGTH(p_fdo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002271 numMatches,
2272 matches);
2273}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002274#endif
2275
2276/*
2277 * The 'formatoptions' option is changed.
2278 */
2279 char *
2280did_set_formatoptions(optset_T *args)
2281{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002282 char_u **varp = (char_u **)args->os_varp;
2283
2284 return did_set_option_listflag(*varp, (char_u *)FO_ALL, args->os_errbuf);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002285}
2286
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002287 int
2288expand_set_formatoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2289{
2290 return expand_set_opt_listflag(args, (char_u*)FO_ALL, numMatches, matches);
2291}
2292
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002293#if defined(CURSOR_SHAPE) || defined(PROTO)
2294/*
2295 * The 'guicursor' option is changed.
2296 */
2297 char *
2298did_set_guicursor(optset_T *args UNUSED)
2299{
2300 return parse_shape_opt(SHAPE_CURSOR);
2301}
2302#endif
2303
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002304#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002305/*
2306 * The 'guifont' option is changed.
2307 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002308 char *
2309did_set_guifont(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002310{
2311 char_u *p;
2312 char *errmsg = NULL;
2313
2314 if (gui.in_use)
2315 {
2316 p = p_guifont;
2317# if defined(FEAT_GUI_GTK)
2318 // Put up a font dialog and let the user select a new value.
2319 // If this is cancelled go back to the old value but don't
2320 // give an error message.
2321 if (STRCMP(p, "*") == 0)
2322 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002323 p = gui_mch_font_dialog(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002324 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002325 p_guifont = (p != NULL) ? p : vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002326 }
2327# endif
2328 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
2329 {
2330# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
2331 if (STRCMP(p_guifont, "*") == 0)
2332 {
2333 // Dialog was cancelled: Keep the old value without giving
2334 // an error message.
2335 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002336 p_guifont = vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002337 }
2338 else
2339# endif
2340 errmsg = e_invalid_fonts;
2341 }
2342 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002343
2344 return errmsg;
2345}
2346
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002347/*
2348 * Expand the 'guifont' option. Only when GUI is being used. Each platform has
2349 * specific behaviors.
2350 */
2351 int
2352expand_set_guifont(optexpand_T *args, int *numMatches, char_u ***matches)
2353{
2354 if (!gui.in_use)
2355 return FAIL;
2356
2357# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
2358 char_u **varp = (char_u **)args->oe_varp;
2359 int wide = (varp == &p_guifontwide);
2360
2361 return expand_set_opt_callback(
2362 args, gui_mch_expand_font, &wide, numMatches, matches);
2363# else
2364 return FAIL;
2365# endif
2366}
2367
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002368# if defined(FEAT_XFONTSET) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002369/*
2370 * The 'guifontset' option is changed.
2371 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002372 char *
2373did_set_guifontset(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002374{
2375 char *errmsg = NULL;
2376
2377 if (STRCMP(p_guifontset, "*") == 0)
2378 errmsg = e_cant_select_fontset;
2379 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
2380 errmsg = e_invalid_fontset;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002381
2382 return errmsg;
2383}
2384# endif
2385
2386/*
2387 * The 'guifontwide' option is changed.
2388 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002389 char *
2390did_set_guifontwide(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002391{
2392 char *errmsg = NULL;
2393
2394 if (STRCMP(p_guifontwide, "*") == 0)
2395 errmsg = e_cant_select_wide_font;
2396 else if (gui_get_wide_font() == FAIL)
2397 errmsg = e_invalid_wide_font;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002398
2399 return errmsg;
2400}
2401#endif
2402
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002403#if defined(FEAT_GUI_GTK) || defined(PROTO)
2404/*
2405 * The 'guiligatures' option is changed.
2406 */
2407 char *
2408did_set_guiligatures(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002409{
2410 gui_set_ligatures();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002411 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002412}
2413#endif
2414
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002415#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002416/*
2417 * The 'guioptions' option is changed.
2418 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002419 char *
2420did_set_guioptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002421{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002422 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002423 char *errmsg;
2424
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002425 errmsg = did_set_option_listflag(*varp, (char_u *)GO_ALL, args->os_errbuf);
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002426 if (errmsg != NULL)
2427 return errmsg;
2428
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002429 gui_init_which_components(args->os_oldval.string);
2430 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002431}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002432
2433 int
2434expand_set_guioptions(optexpand_T *args, int *numMatches, char_u ***matches)
2435{
2436 return expand_set_opt_listflag(args, (char_u*)GO_ALL, numMatches, matches);
2437}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002438#endif
2439
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002440#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002441/*
2442 * The 'guitablabel' option is changed.
2443 */
2444 char *
2445did_set_guitablabel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002446{
2447 redraw_tabline = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002448 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002449}
2450#endif
2451
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002452/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002453 * The 'helpfile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002454 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002455 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002456did_set_helpfile(optset_T *args UNUSED)
2457{
2458 // May compute new values for $VIM and $VIMRUNTIME
2459 if (didset_vim)
2460 vim_unsetenv_ext((char_u *)"VIM");
2461 if (didset_vimruntime)
2462 vim_unsetenv_ext((char_u *)"VIMRUNTIME");
2463 return NULL;
2464}
2465
2466#if defined(FEAT_MULTI_LANG) || defined(PROTO)
2467/*
2468 * The 'helplang' option is changed.
2469 */
2470 char *
2471did_set_helplang(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002472{
2473 char *errmsg = NULL;
2474
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002475 // Check for "", "ab", "ab,cd", etc.
2476 for (char_u *s = p_hlg; *s != NUL; s += 3)
2477 {
2478 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
2479 {
2480 errmsg = e_invalid_argument;
2481 break;
2482 }
2483 if (s[2] == NUL)
2484 break;
2485 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002486
2487 return errmsg;
2488}
2489#endif
2490
2491/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002492 * The 'highlight' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002493 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002494 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002495did_set_highlight(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002496{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002497 if (highlight_changed() == FAIL)
2498 return e_invalid_argument; // invalid flags
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002499
2500 return NULL;
2501}
2502
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002503/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002504 * Expand 'highlight' option.
2505 */
2506 int
2507expand_set_highlight(optexpand_T *args, int *numMatches, char_u ***matches)
2508{
2509 char_u *p;
2510 expand_T *xp = args->oe_xp;
2511 static char_u hl_flags[HLF_COUNT] = HL_FLAGS;
Christian Brabandt3f168ec2023-10-02 23:21:11 +02002512 size_t i;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002513 int count = 0;
2514
2515 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2516 {
2517 // Right after a ':', meaning we just return all highlight names.
2518 return expand_set_opt_generic(
2519 args,
2520 get_highlight_name,
2521 numMatches,
2522 matches);
2523 }
2524
2525 if (*xp->xp_pattern == NUL)
2526 {
2527 // At beginning of a comma-separated list. Return the specific list of
2528 // supported occasions.
2529 *matches = ALLOC_MULT(char_u *, HLF_COUNT + 1);
2530 if (*matches == NULL)
2531 return FAIL;
2532
2533 // We still want to return the full option if it's requested.
2534 if (args->oe_include_orig_val)
2535 {
2536 p = vim_strsave(args->oe_opt_value);
2537 if (p == NULL)
2538 {
2539 VIM_CLEAR(*matches);
2540 return FAIL;
2541 }
2542 (*matches)[count++] = p;
2543 }
2544
2545 for (i = 0; i < HLF_COUNT; i++)
2546 {
2547 p = vim_strnsave(&hl_flags[i], 1);
2548 if (p == NULL)
2549 {
2550 if (count == 0)
2551 {
2552 VIM_CLEAR(*matches);
2553 return FAIL;
2554 }
2555 else
2556 break;
2557 }
2558 (*matches)[count++] = p;
2559 }
2560
2561 if (count == 0)
2562 {
2563 VIM_CLEAR(*matches);
2564 return FAIL;
2565 }
2566 *numMatches = count;
2567 return OK;
2568 }
2569
2570 // We are after the initial character (which indicates the occasion). We
2571 // already made sure we are not matching after a ':' above, so now we want
2572 // to match against display mode modifiers.
2573 // Since the xp_pattern starts from the beginning, we need to include it in
2574 // the returned match.
2575
2576 // Note: Keep this in sync with highlight_changed()
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002577 static char_u p_hl_mode_values[] =
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002578 {':', 'b', 'i', '-', 'n', 'r', 's', 'u', 'c', '2', 'd', '=', 't'};
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002579 size_t num_hl_modes = ARRAY_LENGTH(p_hl_mode_values);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002580
2581 *matches = ALLOC_MULT(char_u *, num_hl_modes);
2582 if (*matches == NULL)
2583 return FAIL;
2584
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002585 size_t pattern_len = STRLEN(xp->xp_pattern);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002586
2587 for (i = 0; i < num_hl_modes; i++)
2588 {
2589 // Don't allow duplicates as these are unique flags
2590 if (vim_strchr(xp->xp_pattern + 1, p_hl_mode_values[i]) != NULL)
2591 continue;
2592
2593 // ':' only works by itself, not with other flags.
2594 if (pattern_len > 1 && p_hl_mode_values[i] == ':')
2595 continue;
2596
2597 p = vim_strnsave(xp->xp_pattern, pattern_len + 1);
2598 if (p == NULL)
2599 {
2600 if (i == 0)
2601 {
2602 VIM_CLEAR(*matches);
2603 return FAIL;
2604 }
2605 else
2606 break;
2607 }
2608 p[pattern_len] = p_hl_mode_values[i];
2609 p[pattern_len + 1] = NUL;
2610 (*matches)[count++] = p;
2611 }
2612 if (count == 0)
2613 {
2614 VIM_CLEAR(*matches);
2615 return FAIL;
2616 }
2617 *numMatches = count;
2618
2619 return OK;
2620}
2621
2622/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002623 * The 'titlestring' or the 'iconstring' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002624 */
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002625 static char *
2626parse_titleiconstring(optset_T *args UNUSED, int flagval UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002627{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002628#ifdef FEAT_STL_OPT
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002629 char_u **varp = (char_u **)args->os_varp;
2630
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002631 // NULL => statusline syntax
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002632 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002633 stl_syntax |= flagval;
2634 else
2635 stl_syntax &= ~flagval;
2636#endif
2637 did_set_title();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002638
2639 return NULL;
2640}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002641
2642/*
2643 * The 'iconstring' option is changed.
2644 */
2645 char *
2646did_set_iconstring(optset_T *args)
2647{
2648 int flagval = 0;
2649
2650#ifdef FEAT_STL_OPT
2651 flagval = STL_IN_ICON;
2652#endif
2653
2654 return parse_titleiconstring(args, flagval);
2655}
2656
2657#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
2658/*
2659 * The 'imactivatekey' option is changed.
2660 */
2661 char *
2662did_set_imactivatekey(optset_T *args UNUSED)
2663{
2664 if (!im_xim_isvalid_imactivate())
2665 return e_invalid_argument;
2666 return NULL;
2667}
2668#endif
2669
2670/*
2671 * The 'isident' or the 'iskeyword' or the 'isprint' or the 'isfname' option is
2672 * changed.
2673 */
2674 char *
2675did_set_isopt(optset_T *args)
2676{
2677 // 'isident', 'iskeyword', 'isprint or 'isfname' option: refill g_chartab[]
2678 // If the new option is invalid, use old value.
2679 // 'lisp' option: refill g_chartab[] for '-' char.
2680 if (init_chartab() == FAIL)
2681 {
2682 args->os_restore_chartab = TRUE;// need to restore the chartab.
2683 return e_invalid_argument; // error in value
2684 }
2685
2686 return NULL;
2687}
2688
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002689/*
2690 * The 'jumpoptions' option is changed.
2691 */
2692 char *
Yegappan Lakshmanan1926ae42023-09-21 16:36:28 +02002693did_set_jumpoptions(optset_T *args UNUSED)
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002694{
2695 if (opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE) != OK)
2696 return e_invalid_argument;
2697
2698 return NULL;
2699}
2700
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002701 int
2702expand_set_jumpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2703{
2704 return expand_set_opt_string(
2705 args,
2706 p_jop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002707 ARRAY_LENGTH(p_jop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002708 numMatches,
2709 matches);
2710}
2711
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002712#if defined(FEAT_KEYMAP) || defined(PROTO)
2713/*
2714 * The 'keymap' option is changed.
2715 */
2716 char *
2717did_set_keymap(optset_T *args)
2718{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002719 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002720 char *errmsg = NULL;
2721
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002722 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002723 errmsg = e_invalid_argument;
2724 else
2725 {
2726 int secure_save = secure;
2727
2728 // Reset the secure flag, since the value of 'keymap' has
2729 // been checked to be safe.
2730 secure = 0;
2731
2732 // load or unload key mapping tables
2733 errmsg = keymap_init();
2734
2735 secure = secure_save;
2736
2737 // Since we check the value, there is no need to set P_INSECURE,
2738 // even when the value comes from a modeline.
2739 args->os_value_checked = TRUE;
2740 }
2741
2742 if (errmsg == NULL)
2743 {
2744 if (*curbuf->b_p_keymap != NUL)
2745 {
2746 // Installed a new keymap, switch on using it.
2747 curbuf->b_p_iminsert = B_IMODE_LMAP;
2748 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
2749 curbuf->b_p_imsearch = B_IMODE_LMAP;
2750 }
2751 else
2752 {
2753 // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
2754 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
2755 curbuf->b_p_iminsert = B_IMODE_NONE;
2756 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
2757 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
2758 }
2759 if ((args->os_flags & OPT_LOCAL) == 0)
2760 {
2761 set_iminsert_global();
2762 set_imsearch_global();
2763 }
2764 status_redraw_curbuf();
2765 }
2766
2767 return errmsg;
2768}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002769#endif
2770
2771/*
2772 * The 'keymodel' option is changed.
2773 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002774 char *
2775did_set_keymodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002776{
2777 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
2778 return e_invalid_argument;
2779
2780 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
2781 km_startsel = (vim_strchr(p_km, 'a') != NULL);
2782 return NULL;
2783}
2784
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002785 int
2786expand_set_keymodel(optexpand_T *args, int *numMatches, char_u ***matches)
2787{
2788 return expand_set_opt_string(
2789 args,
2790 p_km_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002791 ARRAY_LENGTH(p_km_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002792 numMatches,
2793 matches);
2794}
2795
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002796/*
2797 * The 'keyprotocol' option is changed.
2798 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002799 char *
2800did_set_keyprotocol(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002801{
Gregory Anders3695d0e2023-09-29 20:17:20 +02002802 char_u *term = T_NAME;
2803 keyprot_T kpc = match_keyprotocol(term);
2804 if (kpc == KEYPROTOCOL_FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002805 return e_invalid_argument;
2806
Gregory Anders3695d0e2023-09-29 20:17:20 +02002807 apply_keyprotocol(term, kpc);
2808
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002809 return NULL;
2810}
2811
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002812 int
2813expand_set_keyprotocol(optexpand_T *args, int *numMatches, char_u ***matches)
2814{
2815 expand_T *xp = args->oe_xp;
2816 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2817 {
2818 // 'keyprotocol' only has well-defined terms for completion for the
2819 // protocol part after the colon.
2820 return expand_set_opt_string(
2821 args,
2822 p_kpc_protocol_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002823 ARRAY_LENGTH(p_kpc_protocol_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002824 numMatches,
2825 matches);
2826 }
2827 // Use expand_set_opt_string instead of returning FAIL so that we can
2828 // include the original value if args->oe_include_orig_val is set.
2829 static char *(empty[]) = {NULL};
2830 return expand_set_opt_string(args, empty, 0, numMatches, matches);
2831}
2832
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002833/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002834 * The 'lispoptions' option is changed.
2835 */
2836 char *
2837did_set_lispoptions(optset_T *args)
2838{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002839 char_u **varp = (char_u **)args->os_varp;
2840
2841 if (**varp != NUL
2842 && STRCMP(*varp, "expr:0") != 0
2843 && STRCMP(*varp, "expr:1") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002844 return e_invalid_argument;
2845
2846 return NULL;
2847}
2848
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002849 int
2850expand_set_lispoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2851{
2852 static char *(p_lop_values[]) = {"expr:0", "expr:1", NULL};
2853 return expand_set_opt_string(
2854 args,
2855 p_lop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002856 ARRAY_LENGTH(p_lop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002857 numMatches,
2858 matches);
2859}
2860
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002861/*
2862 * The 'matchpairs' option is changed.
2863 */
2864 char *
2865did_set_matchpairs(optset_T *args)
2866{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002867 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002868 char_u *p;
2869
2870 if (has_mbyte)
2871 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002872 for (p = *varp; *p != NUL; ++p)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002873 {
2874 int x2 = -1;
2875 int x3 = -1;
2876
2877 p += mb_ptr2len(p);
2878 if (*p != NUL)
2879 x2 = *p++;
2880 if (*p != NUL)
2881 {
2882 x3 = mb_ptr2char(p);
2883 p += mb_ptr2len(p);
2884 }
2885 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
2886 return e_invalid_argument;
2887 if (*p == NUL)
2888 break;
2889 }
2890 }
2891 else
2892 {
2893 // Check for "x:y,x:y"
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002894 for (p = *varp; *p != NUL; p += 4)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002895 {
2896 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
2897 return e_invalid_argument;
2898 if (p[3] == NUL)
2899 break;
2900 }
2901 }
2902
2903 return NULL;
2904}
2905
2906#if defined(FEAT_SPELL) || defined(PROTO)
2907/*
2908 * The 'mkspellmem' option is changed.
2909 */
2910 char *
2911did_set_mkspellmem(optset_T *args UNUSED)
2912{
2913 if (spell_check_msm() != OK)
2914 return e_invalid_argument;
2915
2916 return NULL;
2917}
2918#endif
2919
2920/*
2921 * The 'mouse' option is changed.
2922 */
2923 char *
2924did_set_mouse(optset_T *args)
2925{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002926 char_u **varp = (char_u **)args->os_varp;
2927
2928 return did_set_option_listflag(*varp, (char_u *)MOUSE_ALL,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002929 args->os_errbuf);
2930}
2931
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002932 int
2933expand_set_mouse(optexpand_T *args, int *numMatches, char_u ***matches)
2934{
2935 return expand_set_opt_listflag(args, (char_u*)MOUSE_ALL, numMatches, matches);
2936}
2937
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002938/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002939 * The 'mousemodel' option is changed.
2940 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002941 char *
2942did_set_mousemodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002943{
2944 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
2945 return e_invalid_argument;
2946#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
2947 else if (*p_mousem != *oldval)
2948 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
2949 // to create or delete the popup menus.
2950 gui_motif_update_mousemodel(root_menu);
2951#endif
2952
2953 return NULL;
2954}
2955
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002956 int
2957expand_set_mousemodel(optexpand_T *args, int *numMatches, char_u ***matches)
2958{
2959 return expand_set_opt_string(
2960 args,
2961 p_mousem_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002962 ARRAY_LENGTH(p_mousem_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002963 numMatches,
2964 matches);
2965}
2966
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002967#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
2968 char *
2969did_set_mouseshape(optset_T *args UNUSED)
2970{
2971 char *errmsg = NULL;
2972
2973 errmsg = parse_shape_opt(SHAPE_MOUSE);
2974 update_mouseshape(-1);
2975
2976 return errmsg;
2977}
2978#endif
2979
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002980/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002981 * The 'nrformats' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002982 */
2983 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002984did_set_nrformats(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002985{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002986 char_u **varp = (char_u **)args->os_varp;
2987
2988 return did_set_opt_strings(*varp, p_nf_values, TRUE);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002989}
2990
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002991 int
2992expand_set_nrformats(optexpand_T *args, int *numMatches, char_u ***matches)
2993{
2994 return expand_set_opt_string(
2995 args,
2996 p_nf_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002997 ARRAY_LENGTH(p_nf_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002998 numMatches,
2999 matches);
3000}
3001
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003002#if defined(FEAT_EVAL) || defined(PROTO)
3003/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003004 * One of the '*expr' options is changed: 'balloonexpr', 'diffexpr',
3005 * 'foldexpr', 'foldtext', 'formatexpr', 'includeexpr', 'indentexpr',
3006 * 'patchexpr', 'printexpr' and 'charconvert'.
3007 *
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003008 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003009 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003010did_set_optexpr(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003011{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003012 char_u **varp = (char_u **)args->os_varp;
3013
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003014 // If the option value starts with <SID> or s:, then replace that with
3015 // the script identifier.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003016 char_u *name = get_scriptlocal_funcname(*varp);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003017 if (name != NULL)
3018 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003019 free_string_option(*varp);
3020 *varp = name;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003021 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003022
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003023 return NULL;
3024}
3025#endif
3026
3027/*
3028 * The 'pastetoggle' option is changed.
3029 */
3030 char *
3031did_set_pastetoggle(optset_T *args UNUSED)
3032{
3033 char_u *p;
3034
3035 // translate key codes like in a mapping
3036 if (*p_pt)
3037 {
zeertzjq7e0bae02023-08-11 23:15:38 +02003038 (void)replace_termcodes(p_pt, &p, 0,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003039 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
3040 if (p != NULL)
3041 {
3042 free_string_option(p_pt);
3043 p_pt = p;
3044 }
3045 }
3046
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003047 return NULL;
3048}
3049
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003050#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3051/*
3052 * The 'previewpopup' option is changed.
3053 */
3054 char *
3055did_set_previewpopup(optset_T *args UNUSED)
3056{
3057 if (parse_previewpopup(NULL) == FAIL)
3058 return e_invalid_argument;
3059
3060 return NULL;
3061}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003062
3063 int
3064expand_set_popupoption(optexpand_T *args, int *numMatches, char_u ***matches)
3065{
3066 expand_T *xp = args->oe_xp;
3067
3068 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3069 {
3070 // Within "highlight:"/"border:"/"align:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003071 int border_len = (int)STRLEN("border:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003072 if (xp->xp_pattern - args->oe_set_arg >= border_len &&
3073 STRNCMP(xp->xp_pattern - border_len, "border:", border_len) == 0)
3074 {
3075 return expand_set_opt_string(
3076 args,
3077 p_popup_option_border_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003078 ARRAY_LENGTH(p_popup_option_border_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003079 numMatches,
3080 matches);
3081 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003082 int align_len = (int)STRLEN("align:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003083 if (xp->xp_pattern - args->oe_set_arg >= align_len &&
3084 STRNCMP(xp->xp_pattern - align_len, "align:", align_len) == 0)
3085 {
3086 return expand_set_opt_string(
3087 args,
3088 p_popup_option_align_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003089 ARRAY_LENGTH(p_popup_option_align_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003090 numMatches,
3091 matches);
3092 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003093 int highlight_len = (int)STRLEN("highlight:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003094 if (xp->xp_pattern - args->oe_set_arg >= highlight_len &&
3095 STRNCMP(xp->xp_pattern - highlight_len, "highlight:", highlight_len) == 0)
3096 {
3097 // Return the list of all highlight names
3098 return expand_set_opt_generic(
3099 args,
3100 get_highlight_name,
3101 numMatches,
3102 matches);
3103 }
3104 return FAIL;
3105 }
3106
3107 return expand_set_opt_string(
3108 args,
3109 p_popup_option_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003110 ARRAY_LENGTH(p_popup_option_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003111 numMatches,
3112 matches);
3113}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003114#endif
3115
3116#if defined(FEAT_POSTSCRIPT) || defined(PROTO)
3117/*
3118 * The 'printencoding' option is changed.
3119 */
3120 char *
3121did_set_printencoding(optset_T *args UNUSED)
3122{
3123 char_u *s, *p;
3124
3125 // Canonize 'printencoding' if VIM standard one
3126 p = enc_canonize(p_penc);
3127 if (p != NULL)
3128 {
3129 vim_free(p_penc);
3130 p_penc = p;
3131 }
3132 else
3133 {
3134 // Ensure lower case and '-' for '_'
3135 for (s = p_penc; *s != NUL; s++)
3136 {
3137 if (*s == '_')
3138 *s = '-';
3139 else
3140 *s = TOLOWER_ASC(*s);
3141 }
3142 }
3143
3144 return NULL;
3145}
3146#endif
3147
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003148#if defined(FEAT_PRINTER) || defined(PROTO)
3149
3150 static char_u *
3151get_printoptions_names(expand_T *xp UNUSED, int idx)
3152{
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003153 if (idx >= (int)ARRAY_LENGTH(printer_opts))
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003154 return NULL;
3155 return (char_u*)printer_opts[idx].name;
3156}
3157
3158/*
3159 * Expand 'printoptions' option
3160 */
3161 int
3162expand_set_printoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3163{
3164 return expand_set_opt_generic(
3165 args,
3166 get_printoptions_names,
3167 numMatches,
3168 matches);
3169}
3170#endif
3171
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003172#if defined(FEAT_STL_OPT) || defined(PROTO)
3173/*
3174 * The 'statusline' or the 'tabline' or the 'rulerformat' option is changed.
3175 * "rulerformat" is TRUE if the 'rulerformat' option is changed.
3176 */
3177 static char *
3178parse_statustabline_rulerformat(optset_T *args, int rulerformat)
3179{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003180 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003181 char_u *s;
3182 char *errmsg = NULL;
3183 int wid;
3184
3185 if (rulerformat) // reset ru_wid first
3186 ru_wid = 0;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003187 s = *varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003188 if (rulerformat && *s == '%')
3189 {
3190 // set ru_wid if 'ruf' starts with "%99("
3191 if (*++s == '-') // ignore a '-'
3192 s++;
3193 wid = getdigits(&s);
3194 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
3195 ru_wid = wid;
3196 else
3197 errmsg = check_stl_option(p_ruf);
3198 }
3199 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
3200 else if (rulerformat || s[0] != '%' || s[1] != '!')
3201 errmsg = check_stl_option(s);
3202 if (rulerformat && errmsg == NULL)
3203 comp_col();
3204
3205 return errmsg;
3206}
3207#endif
3208
3209#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
3210/*
3211 * The 'renderoptions' option is changed.
3212 */
3213 char *
3214did_set_renderoptions(optset_T *args UNUSED)
3215{
3216 if (!gui_mch_set_rendering_options(p_rop))
3217 return e_invalid_argument;
3218
3219 return NULL;
3220}
3221#endif
3222
3223#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3224/*
3225 * The 'rightleftcmd' option is changed.
3226 */
3227 char *
3228did_set_rightleftcmd(optset_T *args)
3229{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003230 char_u **varp = (char_u **)args->os_varp;
3231
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003232 // Currently only "search" is a supported value.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003233 if (**varp != NUL && STRCMP(*varp, "search") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003234 return e_invalid_argument;
3235
3236 return NULL;
3237}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003238
3239 int
3240expand_set_rightleftcmd(optexpand_T *args, int *numMatches, char_u ***matches)
3241{
3242 static char *(p_rlc_values[]) = {"search", NULL};
3243 return expand_set_opt_string(
3244 args,
3245 p_rlc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003246 ARRAY_LENGTH(p_rlc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003247 numMatches,
3248 matches);
3249}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003250#endif
3251
3252#if defined(FEAT_STL_OPT) || defined(PROTO)
3253/*
3254 * The 'rulerformat' option is changed.
3255 */
3256 char *
3257did_set_rulerformat(optset_T *args)
3258{
3259 return parse_statustabline_rulerformat(args, TRUE);
3260}
3261#endif
3262
3263/*
3264 * The 'scrollopt' option is changed.
3265 */
3266 char *
3267did_set_scrollopt(optset_T *args UNUSED)
3268{
3269 return did_set_opt_strings(p_sbo, p_scbopt_values, TRUE);
3270}
3271
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003272 int
3273expand_set_scrollopt(optexpand_T *args, int *numMatches, char_u ***matches)
3274{
3275 return expand_set_opt_string(
3276 args,
3277 p_scbopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003278 ARRAY_LENGTH(p_scbopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003279 numMatches,
3280 matches);
3281}
3282
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003283/*
3284 * The 'selection' option is changed.
3285 */
3286 char *
3287did_set_selection(optset_T *args UNUSED)
3288{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003289 if (*p_sel == NUL || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003290 return e_invalid_argument;
3291
3292 return NULL;
3293}
3294
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003295 int
3296expand_set_selection(optexpand_T *args, int *numMatches, char_u ***matches)
3297{
3298 return expand_set_opt_string(
3299 args,
3300 p_sel_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003301 ARRAY_LENGTH(p_sel_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003302 numMatches,
3303 matches);
3304}
3305
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003306/*
3307 * The 'selectmode' option is changed.
3308 */
3309 char *
3310did_set_selectmode(optset_T *args UNUSED)
3311{
3312 return did_set_opt_strings(p_slm, p_slm_values, TRUE);
3313}
3314
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003315 int
3316expand_set_selectmode(optexpand_T *args, int *numMatches, char_u ***matches)
3317{
3318 return expand_set_opt_string(
3319 args,
3320 p_slm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003321 ARRAY_LENGTH(p_slm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003322 numMatches,
3323 matches);
3324}
3325
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003326#if defined(FEAT_SESSION) || defined(PROTO)
3327/*
3328 * The 'sessionoptions' option is changed.
3329 */
3330 char *
3331did_set_sessionoptions(optset_T *args)
3332{
3333 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
3334 return e_invalid_argument;
3335 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
3336 {
3337 // Don't allow both "sesdir" and "curdir".
3338 (void)opt_strings_flags(args->os_oldval.string, p_ssop_values,
3339 &ssop_flags, TRUE);
3340 return e_invalid_argument;
3341 }
3342
3343 return NULL;
3344}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003345
3346 int
3347expand_set_sessionoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3348{
3349 return expand_set_opt_string(
3350 args,
3351 p_ssop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003352 ARRAY_LENGTH(p_ssop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003353 numMatches,
3354 matches);
3355}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003356#endif
3357
3358/*
3359 * The 'shortmess' option is changed.
3360 */
3361 char *
3362did_set_shortmess(optset_T *args)
3363{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003364 char_u **varp = (char_u **)args->os_varp;
3365
3366 return did_set_option_listflag(*varp, (char_u *)SHM_ALL, args->os_errbuf);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003367}
3368
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003369 int
3370expand_set_shortmess(optexpand_T *args, int *numMatches, char_u ***matches)
3371{
3372 return expand_set_opt_listflag(args, (char_u*)SHM_ALL, numMatches, matches);
3373}
3374
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003375#if defined(FEAT_LINEBREAK) || defined(PROTO)
3376/*
3377 * The 'showbreak' option is changed.
3378 */
3379 char *
3380did_set_showbreak(optset_T *args)
3381{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003382 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003383 char_u *s;
3384
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003385 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003386 {
3387 if (ptr2cells(s) != 1)
3388 return e_showbreak_contains_unprintable_or_wide_character;
3389 MB_PTR_ADV(s);
3390 }
3391
3392 return NULL;
3393}
3394#endif
3395
3396/*
3397 * The 'showcmdloc' option is changed.
3398 */
3399 char *
3400did_set_showcmdloc(optset_T *args UNUSED)
3401{
3402 return did_set_opt_strings(p_sloc, p_sloc_values, FALSE);
3403}
3404
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003405 int
3406expand_set_showcmdloc(optexpand_T *args, int *numMatches, char_u ***matches)
3407{
3408 return expand_set_opt_string(
3409 args,
3410 p_sloc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003411 ARRAY_LENGTH(p_sloc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003412 numMatches,
3413 matches);
3414}
3415
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003416#if defined(FEAT_SIGNS) || defined(PROTO)
3417/*
3418 * The 'signcolumn' option is changed.
3419 */
3420 char *
3421did_set_signcolumn(optset_T *args)
3422{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003423 char_u **varp = (char_u **)args->os_varp;
3424
3425 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003426 return e_invalid_argument;
3427 // When changing the 'signcolumn' to or from 'number', recompute the
3428 // width of the number column if 'number' or 'relativenumber' is set.
3429 if (((*args->os_oldval.string == 'n' && args->os_oldval.string[1] == 'u')
3430 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
3431 && (curwin->w_p_nu || curwin->w_p_rnu))
3432 curwin->w_nrwidth_line_count = 0;
3433
3434 return NULL;
3435}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003436
3437 int
3438expand_set_signcolumn(optexpand_T *args, int *numMatches, char_u ***matches)
3439{
3440 return expand_set_opt_string(
3441 args,
3442 p_scl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003443 ARRAY_LENGTH(p_scl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003444 numMatches,
3445 matches);
3446}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003447#endif
3448
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003449#if defined(FEAT_SPELL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003450/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003451 * The 'spellcapcheck' option is changed.
3452 */
3453 char *
3454did_set_spellcapcheck(optset_T *args UNUSED)
3455{
3456 // compile the regexp program.
3457 return compile_cap_prog(curwin->w_s);
3458}
3459
3460/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003461 * The 'spellfile' option is changed.
3462 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003463 char *
3464did_set_spellfile(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003465{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003466 char_u **varp = (char_u **)args->os_varp;
3467
3468 if (!valid_spellfile(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003469 return e_invalid_argument;
3470
3471 // If there is a window for this buffer in which 'spell' is set load the
3472 // wordlists.
3473 return did_set_spell_option(TRUE);
3474}
3475
3476/*
3477 * The 'spell' option is changed.
3478 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003479 char *
3480did_set_spelllang(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003481{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003482 char_u **varp = (char_u **)args->os_varp;
3483
3484 if (!valid_spelllang(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003485 return e_invalid_argument;
3486
3487 // If there is a window for this buffer in which 'spell' is set load the
3488 // wordlists.
3489 return did_set_spell_option(FALSE);
3490}
3491
3492/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003493 * The 'spelloptions' option is changed.
3494 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003495 char *
3496did_set_spelloptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003497{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003498 char_u **varp = (char_u **)args->os_varp;
3499
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003500 if (**varp != NUL && STRCMP(*varp, "camel") != 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003501 return e_invalid_argument;
3502
3503 return NULL;
3504}
3505
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003506 int
3507expand_set_spelloptions(optexpand_T *args, int *numMatches, char_u ***matches)
3508{
3509 static char *(p_spo_values[]) = {"camel", NULL};
3510 return expand_set_opt_string(
3511 args,
3512 p_spo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003513 ARRAY_LENGTH(p_spo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003514 numMatches,
3515 matches);
3516}
3517
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003518/*
3519 * The 'spellsuggest' option is changed.
3520 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003521 char *
3522did_set_spellsuggest(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003523{
3524 if (spell_check_sps() != OK)
3525 return e_invalid_argument;
3526
3527 return NULL;
3528}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003529
3530 int
3531expand_set_spellsuggest(optexpand_T *args, int *numMatches, char_u ***matches)
3532{
3533 return expand_set_opt_string(
3534 args,
3535 p_sps_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003536 ARRAY_LENGTH(p_sps_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003537 numMatches,
3538 matches);
3539}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003540#endif
3541
3542/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003543 * The 'splitkeep' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003544 */
3545 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003546did_set_splitkeep(optset_T *args UNUSED)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003547{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003548 return did_set_opt_strings(p_spk, p_spk_values, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003549}
3550
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003551 int
3552expand_set_splitkeep(optexpand_T *args, int *numMatches, char_u ***matches)
3553{
3554 return expand_set_opt_string(
3555 args,
3556 p_spk_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003557 ARRAY_LENGTH(p_spk_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003558 numMatches,
3559 matches);
3560}
3561
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00003562#if defined(FEAT_STL_OPT) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003563/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003564 * The 'statusline' option is changed.
3565 */
3566 char *
3567did_set_statusline(optset_T *args)
3568{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003569 return parse_statustabline_rulerformat(args, FALSE);
3570}
3571#endif
3572
3573/*
3574 * The 'swapsync' option is changed.
3575 */
3576 char *
3577did_set_swapsync(optset_T *args UNUSED)
3578{
3579 return did_set_opt_strings(p_sws, p_sws_values, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003580}
3581
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003582 int
3583expand_set_swapsync(optexpand_T *args, int *numMatches, char_u ***matches)
3584{
3585 return expand_set_opt_string(
3586 args,
3587 p_sws_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003588 ARRAY_LENGTH(p_sws_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003589 numMatches,
3590 matches);
3591}
3592
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003593/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003594 * The 'switchbuf' option is changed.
3595 */
3596 char *
3597did_set_switchbuf(optset_T *args UNUSED)
3598{
3599 return did_set_opt_flags(p_swb, p_swb_values, &swb_flags, TRUE);
3600}
3601
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003602 int
3603expand_set_switchbuf(optexpand_T *args, int *numMatches, char_u ***matches)
3604{
3605 return expand_set_opt_string(
3606 args,
3607 p_swb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003608 ARRAY_LENGTH(p_swb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003609 numMatches,
3610 matches);
3611}
3612
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003613#if defined(FEAT_STL_OPT) || defined(PROTO)
3614/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003615 * The 'tabline' option is changed.
3616 */
3617 char *
3618did_set_tabline(optset_T *args)
3619{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003620 return parse_statustabline_rulerformat(args, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003621}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003622#endif
3623
3624/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003625 * The 'tagcase' option is changed.
3626 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003627 char *
3628did_set_tagcase(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003629{
3630 unsigned int *flags;
3631 char_u *p;
3632
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003633 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003634 {
3635 p = curbuf->b_p_tc;
3636 flags = &curbuf->b_tc_flags;
3637 }
3638 else
3639 {
3640 p = p_tc;
3641 flags = &tc_flags;
3642 }
3643
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003644 if ((args->os_flags & OPT_LOCAL) && *p == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003645 // make the local value empty: use the global value
3646 *flags = 0;
3647 else if (*p == NUL
3648 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
3649 return e_invalid_argument;
3650
3651 return NULL;
3652}
3653
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003654 int
3655expand_set_tagcase(optexpand_T *args, int *numMatches, char_u ***matches)
3656{
3657 return expand_set_opt_string(
3658 args,
3659 p_tc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003660 ARRAY_LENGTH(p_tc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003661 numMatches,
3662 matches);
3663}
3664
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003665/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003666 * The 'term' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003667 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003668 char *
3669did_set_term(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003670{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003671 if (T_NAME[0] == NUL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003672 return e_cannot_set_term_to_empty_string;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003673#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003674 if (gui.in_use)
3675 return e_cannot_change_term_in_GUI;
3676 if (term_is_gui(T_NAME))
3677 return e_use_gui_to_start_GUI;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003678#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003679 if (set_termname(T_NAME) == FAIL)
3680 return e_not_found_in_termcap;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003681
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003682 // Screen colors may have changed.
3683 redraw_later_clear();
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003684
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003685 return NULL;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003686}
3687
3688/*
3689 * Some terminal option (t_xxx) is changed
3690 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003691 char *
3692did_set_term_option(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003693{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003694 char_u **varp = (char_u **)args->os_varp;
3695
3696 if (!full_screen)
3697 return NULL;
3698
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003699 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
3700 if (varp == &T_CCO)
3701 {
3702 int colors = atoi((char *)T_CCO);
3703
3704 // Only reinitialize colors if t_Co value has really changed to
3705 // avoid expensive reload of colorscheme if t_Co is set to the
3706 // same value multiple times.
3707 if (colors != t_colors)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003708 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003709 t_colors = colors;
3710 if (t_colors <= 1)
3711 {
3712 vim_free(T_CCO);
3713 T_CCO = empty_option;
3714 }
3715#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3716 if (is_term_win32())
3717 {
3718 swap_tcap();
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003719 args->os_did_swaptcap = TRUE;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003720 }
3721#endif
3722 // We now have a different color setup, initialize it again.
3723 init_highlight(TRUE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003724 }
3725 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003726 ttest(FALSE);
3727 if (varp == &T_ME)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003728 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003729 out_str(T_ME);
3730 redraw_later(UPD_CLEAR);
3731#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3732 // Since t_me has been set, this probably means that the user
3733 // wants to use this as default colors. Need to reset default
3734 // background/foreground colors.
3735# ifdef VIMDLL
3736 if (!gui.in_use && !gui.starting)
3737# endif
3738 mch_set_normal_colors();
3739#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003740 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003741 if (varp == &T_BE && termcap_active)
3742 {
3743 MAY_WANT_TO_LOG_THIS;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003744
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003745 if (*T_BE == NUL)
3746 // When clearing t_BE we assume the user no longer wants
3747 // bracketed paste, thus disable it by writing t_BD.
3748 out_str(T_BD);
3749 else
3750 out_str(T_BE);
3751 }
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003752
3753 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003754}
3755
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003756#if defined(FEAT_TERMINAL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003757/*
3758 * The 'termwinkey' option is changed.
3759 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003760 char *
3761did_set_termwinkey(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003762{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003763 if (*curwin->w_p_twk != NUL && string_to_key(curwin->w_p_twk, TRUE) == 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003764 return e_invalid_argument;
3765
3766 return NULL;
3767}
3768
3769/*
3770 * The 'termwinsize' option is changed.
3771 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003772 char *
3773did_set_termwinsize(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003774{
3775 char_u *p;
3776
3777 if (*curwin->w_p_tws == NUL)
3778 return NULL;
3779
3780 p = skipdigits(curwin->w_p_tws);
3781 if (p == curwin->w_p_tws
3782 || (*p != 'x' && *p != '*')
3783 || *skipdigits(p + 1) != NUL)
3784 return e_invalid_argument;
3785
3786 return NULL;
3787}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003788
3789# if defined(MSWIN) || defined(PROTO)
3790/*
3791 * The 'termwintype' option is changed.
3792 */
3793 char *
3794did_set_termwintype(optset_T *args UNUSED)
3795{
3796 return did_set_opt_strings(p_twt, p_twt_values, FALSE);
3797}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003798
3799 int
3800expand_set_termwintype(optexpand_T *args, int *numMatches, char_u ***matches)
3801{
3802 return expand_set_opt_string(
3803 args,
3804 p_twt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003805 ARRAY_LENGTH(p_twt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003806 numMatches,
3807 matches);
3808}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003809# endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003810#endif
3811
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003812/*
3813 * The 'titlestring' option is changed.
3814 */
3815 char *
3816did_set_titlestring(optset_T *args)
3817{
3818 int flagval = 0;
3819
3820#ifdef FEAT_STL_OPT
3821 flagval = STL_IN_TITLE;
3822#endif
3823 return parse_titleiconstring(args, flagval);
3824}
3825
3826#if (defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)) || defined(PROTO)
3827/*
3828 * The 'toolbar' option is changed.
3829 */
3830 char *
3831did_set_toolbar(optset_T *args UNUSED)
3832{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003833 if (opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags,
3834 TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003835 return e_invalid_argument;
3836
3837 out_flush();
3838 gui_mch_show_toolbar((toolbar_flags &
3839 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
3840 return NULL;
3841}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003842
3843 int
3844expand_set_toolbar(optexpand_T *args, int *numMatches, char_u ***matches)
3845{
3846 return expand_set_opt_string(
3847 args,
3848 p_toolbar_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003849 ARRAY_LENGTH(p_toolbar_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003850 numMatches,
3851 matches);
3852}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003853#endif
3854
3855#if (defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)) || defined(PROTO)
3856/*
3857 * The 'toolbariconsize' option is changed. GTK+ 2 only.
3858 */
3859 char *
3860did_set_toolbariconsize(optset_T *args UNUSED)
3861{
3862 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
3863 return e_invalid_argument;
3864
3865 out_flush();
3866 gui_mch_show_toolbar((toolbar_flags &
3867 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
3868 return NULL;
3869}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003870
3871 int
3872expand_set_toolbariconsize(optexpand_T *args, int *numMatches, char_u ***matches)
3873{
3874 return expand_set_opt_string(
3875 args,
3876 p_tbis_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003877 ARRAY_LENGTH(p_tbis_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003878 numMatches,
3879 matches);
3880}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003881#endif
3882
3883#if defined(UNIX) || defined(VMS) || defined(PROTO)
3884/*
3885 * The 'ttymouse' option is changed.
3886 */
3887 char *
3888did_set_ttymouse(optset_T *args UNUSED)
3889{
3890 char *errmsg = NULL;
3891
3892 // Switch the mouse off before changing the escape sequences used for
3893 // that.
3894 mch_setmouse(FALSE);
3895 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
3896 errmsg = e_invalid_argument;
3897 else
3898 check_mouse_termcode();
3899 if (termcap_active)
3900 setmouse(); // may switch it on again
3901
3902 return errmsg;
3903}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003904
3905 int
3906expand_set_ttymouse(optexpand_T *args, int *numMatches, char_u ***matches)
3907{
3908 return expand_set_opt_string(
3909 args,
3910 p_ttym_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003911 ARRAY_LENGTH(p_ttym_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003912 numMatches,
3913 matches);
3914}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003915#endif
3916
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003917#if defined(FEAT_VARTABS) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003918/*
3919 * The 'varsofttabstop' option is changed.
3920 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003921 char *
3922did_set_varsofttabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003923{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003924 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003925 char_u *cp;
3926
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003927 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00003928 VIM_CLEAR(curbuf->b_p_vsts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003929 else
3930 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003931 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003932 {
3933 if (vim_isdigit(*cp))
3934 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003935 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003936 continue;
3937 return e_invalid_argument;
3938 }
3939
3940 int *oldarray = curbuf->b_p_vsts_array;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003941 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003942 {
3943 if (oldarray)
3944 vim_free(oldarray);
3945 }
3946 else
3947 return e_invalid_argument;
3948 }
3949
3950 return NULL;
3951}
3952
3953/*
3954 * The 'vartabstop' option is changed.
3955 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003956 char *
3957did_set_vartabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003958{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003959 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003960 char_u *cp;
3961
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003962 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00003963 VIM_CLEAR(curbuf->b_p_vts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003964 else
3965 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003966 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003967 {
3968 if (vim_isdigit(*cp))
3969 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003970 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003971 continue;
3972 return e_invalid_argument;
3973 }
3974
3975 int *oldarray = curbuf->b_p_vts_array;
3976
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003977 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003978 {
3979 vim_free(oldarray);
3980# ifdef FEAT_FOLDING
3981 if (foldmethodIsIndent(curwin))
3982 foldUpdateAll(curwin);
3983# endif
3984 }
3985 else
3986 return e_invalid_argument;
3987 }
3988
3989 return NULL;
3990}
3991#endif
3992
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003993/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003994 * The 'verbosefile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003995 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003996 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003997did_set_verbosefile(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003998{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003999 verbose_stop();
4000 if (*p_vfile != NUL && verbose_open() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004001 return e_invalid_argument;
4002
4003 return NULL;
4004}
4005
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004006#if defined(FEAT_SESSION) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004007/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004008 * The 'viewoptions' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004009 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004010 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004011did_set_viewoptions(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004012{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004013 return did_set_opt_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004014}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004015#endif
4016
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004017#if defined(FEAT_VIMINFO) || defined(PROTO)
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004018/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004019 * The 'viminfo' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004020 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004021 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004022did_set_viminfo(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004023{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004024 char_u *s;
4025 char *errmsg = NULL;
4026
4027 for (s = p_viminfo; *s;)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004028 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004029 // Check it's a valid character
4030 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
4031 {
4032 errmsg = illegal_char(args->os_errbuf, *s);
4033 break;
4034 }
4035 if (*s == 'n') // name is always last one
4036 break;
4037 else if (*s == 'r') // skip until next ','
4038 {
4039 while (*++s && *s != ',')
4040 ;
4041 }
4042 else if (*s == '%')
4043 {
4044 // optional number
4045 while (vim_isdigit(*++s))
4046 ;
4047 }
4048 else if (*s == '!' || *s == 'h' || *s == 'c')
4049 ++s; // no extra chars
4050 else // must have a number
4051 {
4052 while (vim_isdigit(*++s))
4053 ;
4054
4055 if (!VIM_ISDIGIT(*(s - 1)))
4056 {
4057 if (args->os_errbuf != NULL)
4058 {
4059 sprintf(args->os_errbuf,
4060 _(e_missing_number_after_angle_str_angle),
4061 transchar_byte(*(s - 1)));
4062 errmsg = args->os_errbuf;
4063 }
4064 else
4065 errmsg = "";
4066 break;
4067 }
4068 }
4069 if (*s == ',')
4070 ++s;
4071 else if (*s)
4072 {
4073 if (args->os_errbuf != NULL)
4074 errmsg = e_missing_comma;
4075 else
4076 errmsg = "";
4077 break;
4078 }
4079 }
4080 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
4081 errmsg = e_must_specify_a_value;
4082
4083 return errmsg;
4084}
4085#endif
4086
4087/*
4088 * The 'virtualedit' option is changed.
4089 */
4090 char *
4091did_set_virtualedit(optset_T *args)
4092{
4093 char_u *ve = p_ve;
4094 unsigned int *flags = &ve_flags;
4095
4096 if (args->os_flags & OPT_LOCAL)
4097 {
4098 ve = curwin->w_p_ve;
4099 flags = &curwin->w_ve_flags;
4100 }
4101
4102 if ((args->os_flags & OPT_LOCAL) && *ve == NUL)
4103 // make the local value empty: use the global value
4104 *flags = 0;
4105 else
4106 {
4107 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
4108 return e_invalid_argument;
4109 else if (STRCMP(ve, args->os_oldval.string) != 0)
4110 {
4111 // Recompute cursor position in case the new 've' setting
4112 // changes something.
4113 validate_virtcol();
4114 coladvance(curwin->w_virtcol);
4115 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004116 }
4117
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004118 return NULL;
4119}
4120
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004121 int
4122expand_set_virtualedit(optexpand_T *args, int *numMatches, char_u ***matches)
4123{
4124 return expand_set_opt_string(
4125 args,
4126 p_ve_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004127 ARRAY_LENGTH(p_ve_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004128 numMatches,
4129 matches);
4130}
4131
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004132/*
4133 * The 'whichwrap' option is changed.
4134 */
4135 char *
4136did_set_whichwrap(optset_T *args)
4137{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004138 char_u **varp = (char_u **)args->os_varp;
4139
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004140 // Add ',' to the list flags because 'whichwrap' is a flag
4141 // list that is comma-separated.
4142 return did_set_option_listflag(*varp, (char_u *)(WW_ALL ","), args->os_errbuf);
4143}
4144
4145 int
4146expand_set_whichwrap(optexpand_T *args, int *numMatches, char_u ***matches)
4147{
4148 return expand_set_opt_listflag(args, (char_u*)WW_ALL, numMatches, matches);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004149}
4150
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004151/*
4152 * The 'wildmode' option is changed.
4153 */
4154 char *
4155did_set_wildmode(optset_T *args UNUSED)
4156{
4157 if (check_opt_wim() == FAIL)
4158 return e_invalid_argument;
4159 return NULL;
4160}
4161
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004162 int
4163expand_set_wildmode(optexpand_T *args, int *numMatches, char_u ***matches)
4164{
4165 return expand_set_opt_string(
4166 args,
4167 p_wim_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004168 ARRAY_LENGTH(p_wim_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004169 numMatches,
4170 matches);
4171}
4172
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004173/*
4174 * The 'wildoptions' option is changed.
4175 */
4176 char *
4177did_set_wildoptions(optset_T *args UNUSED)
4178{
4179 return did_set_opt_strings(p_wop, p_wop_values, TRUE);
4180}
4181
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004182 int
4183expand_set_wildoptions(optexpand_T *args, int *numMatches, char_u ***matches)
4184{
4185 return expand_set_opt_string(
4186 args,
4187 p_wop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004188 ARRAY_LENGTH(p_wop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004189 numMatches,
4190 matches);
4191}
4192
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004193#if defined(FEAT_WAK) || defined(PROTO)
4194/*
4195 * The 'winaltkeys' option is changed.
4196 */
4197 char *
4198did_set_winaltkeys(optset_T *args UNUSED)
4199{
4200 char *errmsg = NULL;
4201
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004202 if (*p_wak == NUL || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004203 errmsg = e_invalid_argument;
4204# ifdef FEAT_MENU
4205# if defined(FEAT_GUI_MOTIF)
4206 else if (gui.in_use)
4207 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4208# elif defined(FEAT_GUI_GTK)
4209 else if (gui.in_use)
4210 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4211# endif
4212# endif
4213 return errmsg;
4214}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004215
4216 int
4217expand_set_winaltkeys(optexpand_T *args, int *numMatches, char_u ***matches)
4218{
4219 return expand_set_opt_string(
4220 args,
4221 p_wak_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004222 ARRAY_LENGTH(p_wak_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004223 numMatches,
4224 matches);
4225}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004226#endif
4227
4228/*
4229 * The 'wincolor' option is changed.
4230 */
4231 char *
4232did_set_wincolor(optset_T *args UNUSED)
4233{
4234#ifdef FEAT_TERMINAL
4235 term_update_wincolor(curwin);
4236#endif
4237 return NULL;
4238}
4239
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004240 int
4241expand_set_wincolor(optexpand_T *args, int *numMatches, char_u ***matches)
4242{
4243 return expand_set_opt_generic(
4244 args,
4245 get_highlight_name,
4246 numMatches,
4247 matches);
4248}
4249
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004250#ifdef FEAT_SYN_HL
4251/*
4252 * When the 'syntax' option is set, load the syntax of that name.
4253 */
4254 static void
4255do_syntax_autocmd(int value_changed)
4256{
4257 static int syn_recursive = 0;
4258
4259 ++syn_recursive;
4260 // Only pass TRUE for "force" when the value changed or not used
4261 // recursively, to avoid endless recurrence.
4262 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
4263 value_changed || syn_recursive == 1, curbuf);
4264 curbuf->b_flags |= BF_SYN_SET;
4265 --syn_recursive;
4266}
4267#endif
4268
4269/*
4270 * When the 'filetype' option is set, trigger the FileType autocommand.
4271 */
4272 static void
4273do_filetype_autocmd(char_u **varp, int opt_flags, int value_changed)
4274{
4275 // Skip this when called from a modeline and the filetype was already set
4276 // to this value.
4277 if ((opt_flags & OPT_MODELINE) && !value_changed)
4278 return;
4279
4280 static int ft_recursive = 0;
4281 int secure_save = secure;
4282
4283 // Reset the secure flag, since the value of 'filetype' has
4284 // been checked to be safe.
4285 secure = 0;
4286
4287 ++ft_recursive;
4288 did_filetype = TRUE;
4289 // Only pass TRUE for "force" when the value changed or not
4290 // used recursively, to avoid endless recurrence.
4291 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
4292 value_changed || ft_recursive == 1, curbuf);
4293 --ft_recursive;
4294 // Just in case the old "curbuf" is now invalid.
4295 if (varp != &(curbuf->b_p_ft))
4296 varp = NULL;
4297
4298 secure = secure_save;
4299}
4300
4301#ifdef FEAT_SPELL
4302/*
4303 * When the 'spelllang' option is set, source the spell/LANG.vim file in
4304 * 'runtimepath'.
4305 */
4306 static void
4307do_spelllang_source(void)
4308{
4309 char_u fname[200];
4310 char_u *p;
4311 char_u *q = curwin->w_s->b_p_spl;
4312
4313 // Skip the first name if it is "cjk".
4314 if (STRNCMP(q, "cjk,", 4) == 0)
4315 q += 4;
4316
4317 // They could set 'spellcapcheck' depending on the language. Use the first
4318 // name in 'spelllang' up to '_region' or '.encoding'.
4319 for (p = q; *p != NUL; ++p)
4320 if (!ASCII_ISALNUM(*p) && *p != '-')
4321 break;
4322 if (p > q)
4323 {
4324 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
4325 (int)(p - q), q);
4326 source_runtime(fname, DIP_ALL);
4327 }
4328}
4329#endif
4330
4331/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004332 * Handle string options that need some action to perform when changed.
zeertzjqf6782732022-07-27 18:26:03 +01004333 * The new value must be allocated.
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004334 * Returns NULL for success, or an untranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +02004335 */
4336 char *
4337did_set_string_option(
4338 int opt_idx, // index in options[] table
4339 char_u **varp, // pointer to the option variable
Bram Moolenaardac13472019-09-16 21:06:21 +02004340 char_u *oldval, // previous value of the option
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004341 char_u *value, // new value of the option
Bram Moolenaardac13472019-09-16 21:06:21 +02004342 char *errbuf, // buffer for errors, or NULL
4343 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004344 set_op_T op, // OP_ADDING/OP_PREPENDING/OP_REMOVING
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004345 int *value_checked) // value was checked to be safe, no
Bram Moolenaardac13472019-09-16 21:06:21 +02004346 // need to set P_INSECURE
4347{
4348 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +02004349 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004350 opt_did_set_cb_T did_set_cb = get_option_did_set_cb(opt_idx);
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004351 optset_T args;
4352
4353 // 'ttytype' is an alias for 'term'. Both 'term' and 'ttytype' point to
4354 // T_NAME. If 'term' or 'ttytype' is modified, then use the index for the
4355 // 'term' option. Only set the P_ALLOCED flag on 'term'.
4356 if (varp == &T_NAME)
4357 {
4358 opt_idx = findoption((char_u *)"term");
4359 if (opt_idx >= 0)
4360 {
4361 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
4362 did_set_cb = get_option_did_set_cb(opt_idx);
4363 }
4364 }
4365
4366 CLEAR_FIELD(args);
Bram Moolenaardac13472019-09-16 21:06:21 +02004367
Bram Moolenaardac13472019-09-16 21:06:21 +02004368 // Disallow changing some options from secure mode
4369 if ((secure
4370#ifdef HAVE_SANDBOX
4371 || sandbox != 0
4372#endif
4373 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00004374 errmsg = e_not_allowed_here;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004375 // Check for a "normal" directory or file name in some options.
4376 else if (check_illegal_path_names(opt_idx, varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004377 errmsg = e_invalid_argument;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004378 else if (did_set_cb != NULL)
4379 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004380 args.os_varp = (char_u *)varp;
4381 args.os_idx = opt_idx;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004382 args.os_flags = opt_flags;
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004383 args.os_op = op;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004384 args.os_oldval.string = oldval;
4385 args.os_newval.string = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004386 args.os_errbuf = errbuf;
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004387 // Invoke the option specific callback function to validate and apply
4388 // the new option value.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004389 errmsg = did_set_cb(&args);
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004390
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004391 // The 'keymap', 'filetype' and 'syntax' option callback functions
4392 // may change the os_value_checked field.
4393 *value_checked = args.os_value_checked;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004394 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004395
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004396 // If an error is detected, restore the previous value.
Bram Moolenaardac13472019-09-16 21:06:21 +02004397 if (errmsg != NULL)
4398 {
zeertzjqf6782732022-07-27 18:26:03 +01004399 free_string_option(*varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02004400 *varp = oldval;
4401 // When resetting some values, need to act on it.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004402 if (args.os_restore_chartab)
Bram Moolenaardac13472019-09-16 21:06:21 +02004403 (void)init_chartab();
4404 if (varp == &p_hl)
4405 (void)highlight_changed();
4406 }
4407 else
4408 {
4409#ifdef FEAT_EVAL
4410 // Remember where the option was set.
4411 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4412#endif
4413 // Free string options that are in allocated memory.
4414 // Use "free_oldval", because recursiveness may change the flags under
4415 // our fingers (esp. init_highlight()).
4416 if (free_oldval)
4417 free_string_option(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01004418 set_option_flag(opt_idx, P_ALLOCED);
Bram Moolenaardac13472019-09-16 21:06:21 +02004419
4420 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
4421 && is_global_local_option(opt_idx))
4422 {
4423 // global option with local value set to use global value; free
4424 // the local value and make it empty
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004425 char_u *p = get_option_varp_scope(opt_idx, OPT_LOCAL);
Bram Moolenaardac13472019-09-16 21:06:21 +02004426 free_string_option(*(char_u **)p);
4427 *(char_u **)p = empty_option;
4428 }
4429
4430 // May set global value for local option.
4431 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
4432 set_string_option_global(opt_idx, varp);
4433
4434 // Trigger the autocommand only after setting the flags.
4435#ifdef FEAT_SYN_HL
Bram Moolenaardac13472019-09-16 21:06:21 +02004436 if (varp == &(curbuf->b_p_syn))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004437 do_syntax_autocmd(args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004438#endif
4439 else if (varp == &(curbuf->b_p_ft))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004440 do_filetype_autocmd(varp, opt_flags, args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004441#ifdef FEAT_SPELL
4442 if (varp == &(curwin->w_s->b_p_spl))
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004443 do_spelllang_source();
Bram Moolenaardac13472019-09-16 21:06:21 +02004444#endif
4445 }
4446
Bram Moolenaardac13472019-09-16 21:06:21 +02004447 if (varp == &p_mouse)
4448 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004449 if (*p_mouse == NUL)
4450 mch_setmouse(FALSE); // switch mouse off
4451 else
Bram Moolenaardac13472019-09-16 21:06:21 +02004452 setmouse(); // in case 'mouse' changed
4453 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004454
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004455#if defined(FEAT_LUA) || defined(PROTO)
4456 if (varp == &p_rtp)
4457 update_package_paths_in_lua();
4458#endif
4459
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004460#if defined(FEAT_LINEBREAK)
4461 // Changing Formatlistpattern when briopt includes the list setting:
4462 // redraw
4463 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
4464 && curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004465 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004466#endif
4467
Bram Moolenaardac13472019-09-16 21:06:21 +02004468 if (curwin->w_curswant != MAXCOL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004469 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02004470 curwin->w_set_curswant = TRUE;
4471
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004472 if ((opt_flags & OPT_NO_REDRAW) == 0)
4473 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004474#ifdef FEAT_GUI
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004475 // set when changing an option that only requires a redraw in the GUI
4476 int redraw_gui_only = FALSE;
4477
4478 if (varp == &p_go // 'guioptions'
4479 || varp == &p_guifont // 'guifont'
4480# ifdef FEAT_GUI_TABLINE
4481 || varp == &p_gtl // 'guitablabel'
4482 || varp == &p_gtt // 'guitabtooltip'
4483# endif
4484# ifdef FEAT_XFONTSET
4485 || varp == &p_guifontset // 'guifontset'
4486# endif
4487 || varp == &p_guifontwide // 'guifontwide'
4488# ifdef FEAT_GUI_GTK
4489 || varp == &p_guiligatures // 'guiligatures'
4490# endif
4491 )
4492 redraw_gui_only = TRUE;
4493
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004494 // check redraw when it's not a GUI option or the GUI is active.
4495 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02004496#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004497 check_redraw(get_option_flags(opt_idx));
4498 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004499
4500#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004501 if (args.os_did_swaptcap)
Bram Moolenaardac13472019-09-16 21:06:21 +02004502 {
4503 set_termname((char_u *)"win32");
4504 init_highlight(TRUE, FALSE);
4505 }
4506#endif
4507
4508 return errmsg;
4509}
4510
4511/*
4512 * Check an option that can be a range of string values.
4513 *
4514 * Return OK for correct value, FAIL otherwise.
4515 * Empty is always OK.
4516 */
4517 static int
4518check_opt_strings(
4519 char_u *val,
4520 char **values,
4521 int list) // when TRUE: accept a list of values
4522{
4523 return opt_strings_flags(val, values, NULL, list);
4524}
4525
4526/*
4527 * Handle an option that can be a range of string values.
4528 * Set a flag in "*flagp" for each string present.
4529 *
4530 * Return OK for correct value, FAIL otherwise.
4531 * Empty is always OK.
4532 */
4533 static int
4534opt_strings_flags(
4535 char_u *val, // new value
4536 char **values, // array of valid string values
4537 unsigned *flagp,
4538 int list) // when TRUE: accept a list of values
4539{
4540 int i;
4541 int len;
4542 unsigned new_flags = 0;
4543
4544 while (*val)
4545 {
4546 for (i = 0; ; ++i)
4547 {
4548 if (values[i] == NULL) // val not found in values[]
4549 return FAIL;
4550
4551 len = (int)STRLEN(values[i]);
4552 if (STRNCMP(values[i], val, len) == 0
4553 && ((list && val[len] == ',') || val[len] == NUL))
4554 {
4555 val += len + (val[len] == ',');
4556 new_flags |= (1 << i);
4557 break; // check next item in val list
4558 }
4559 }
4560 }
4561 if (flagp != NULL)
4562 *flagp = new_flags;
4563
4564 return OK;
4565}
4566
4567/*
4568 * return OK if "p" is a valid fileformat name, FAIL otherwise.
4569 */
4570 int
4571check_ff_value(char_u *p)
4572{
4573 return check_opt_strings(p, p_ff_values, FALSE);
4574}
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004575
4576/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004577 * Save the actual shortmess Flags and clear them temporarily to avoid that
4578 * file messages overwrites any output from the following commands.
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004579 *
4580 * Caller must make sure to first call save_clear_shm_value() and then
4581 * restore_shm_value() exactly the same number of times.
4582 */
4583 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004584save_clear_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004585{
4586 if (STRLEN(p_shm) >= SHM_LEN)
4587 {
4588 iemsg(e_internal_error_shortmess_too_long);
4589 return;
4590 }
4591
4592 if (++set_shm_recursive == 1)
4593 {
4594 STRCPY(shm_buf, p_shm);
4595 set_option_value_give_err((char_u *)"shm", 0L, (char_u *)"", 0);
4596 }
4597}
4598
4599/*
4600 * Restore the shortmess Flags set from the save_clear_shm_value() function.
4601 */
4602 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004603restore_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004604{
4605 if (--set_shm_recursive == 0)
4606 {
4607 set_option_value_give_err((char_u *)"shm", 0L, shm_buf, 0);
4608 vim_memset(shm_buf, 0, SHM_LEN);
4609 }
4610}