blob: c45b3bce7e171e0a2f34e6aa14b7763c15acfa36 [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
distobs25ac6d62024-07-06 17:50:09 +020036static char *(p_nf_values[]) = {"bin", "octal", "hex", "alpha", "unsigned", "blank", 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};
LemonBoy5247b0b2024-07-12 19:30:58 +020084// Keep in sync with TCL_ flags in option.h
85static char *(p_tcl_values[]) = {"left", "uselast", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020086#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
87static char *(p_toolbar_values[]) = {"text", "icons", "tooltips", "horiz", NULL};
88#endif
89#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
90static char *(p_tbis_values[]) = {"tiny", "small", "medium", "large", "huge", "giant", NULL};
91#endif
Bram Moolenaara1cb1d12019-10-17 23:00:07 +020092#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +020093static char *(p_ttym_values[]) = {"xterm", "xterm2", "dec", "netterm", "jsbterm", "pterm", "urxvt", "sgr", NULL};
94#endif
Gary Johnson53ba05b2021-07-26 22:19:10 +020095static char *(p_ve_values[]) = {"block", "insert", "all", "onemore", "none", "NONE", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020096// Note: Keep this in sync with check_opt_wim()
97static char *(p_wim_values[]) = {"full", "longest", "list", "lastused", NULL};
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000098static char *(p_wop_values[]) = {"fuzzy", "tagfile", "pum", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020099#ifdef FEAT_WAK
100static char *(p_wak_values[]) = {"yes", "menu", "no", NULL};
101#endif
102static char *(p_mousem_values[]) = {"extend", "popup", "popup_setpos", "mac", NULL};
103static char *(p_sel_values[]) = {"inclusive", "exclusive", "old", NULL};
104static char *(p_slm_values[]) = {"mouse", "key", "cmd", NULL};
105static char *(p_km_values[]) = {"startsel", "stopsel", NULL};
106#ifdef FEAT_BROWSE
107static char *(p_bsdir_values[]) = {"current", "last", "buffer", NULL};
108#endif
109static char *(p_scbopt_values[]) = {"ver", "hor", "jump", NULL};
110static char *(p_debug_values[]) = {"msg", "throw", "beep", NULL};
111static char *(p_ead_values[]) = {"both", "ver", "hor", NULL};
112static char *(p_buftype_values[]) = {"nofile", "nowrite", "quickfix", "help", "terminal", "acwrite", "prompt", "popup", NULL};
113static char *(p_bufhidden_values[]) = {"hide", "unload", "delete", "wipe", NULL};
Bram Moolenaaraa0489e2020-04-17 19:41:21 +0200114static char *(p_bs_values[]) = {"indent", "eol", "start", "nostop", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200115#ifdef FEAT_FOLDING
116static char *(p_fdm_values[]) = {"manual", "expr", "marker", "indent", "syntax",
117# ifdef FEAT_DIFF
118 "diff",
119# endif
120 NULL};
121static char *(p_fcl_values[]) = {"all", NULL};
122#endif
glepnir600a12d2024-07-09 18:51:29 +0200123static char *(p_cot_values[]) = {"menu", "menuone", "longest", "preview", "popup", "popuphidden", "noinsert", "noselect", "fuzzy", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200124#ifdef BACKSLASH_IN_FILENAME
125static char *(p_csl_values[]) = {"slash", "backslash", NULL};
126#endif
127#ifdef FEAT_SIGNS
128static char *(p_scl_values[]) = {"yes", "no", "auto", "number", NULL};
129#endif
130#if defined(MSWIN) && defined(FEAT_TERMINAL)
131static char *(p_twt_values[]) = {"winpty", "conpty", "", NULL};
132#endif
Luuk van Baalba936f62022-12-15 13:15:39 +0000133static char *(p_sloc_values[]) = {"last", "statusline", "tabline", NULL};
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +0000134static char *(p_sws_values[]) = {"fsync", "sync", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200135
136static int check_opt_strings(char_u *val, char **values, int list);
137static int opt_strings_flags(char_u *val, char **values, unsigned *flagp, int list);
138
139/*
140 * After setting various option values: recompute variables that depend on
141 * option values.
142 */
143 void
144didset_string_options(void)
145{
146 (void)opt_strings_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
147 (void)opt_strings_flags(p_bkc, p_bkc_values, &bkc_flags, TRUE);
148 (void)opt_strings_flags(p_bo, p_bo_values, &bo_flags, TRUE);
zeertzjq529b9ad2024-06-05 20:27:06 +0200149 (void)opt_strings_flags(p_cot, p_cot_values, &cot_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200150#ifdef FEAT_SESSION
151 (void)opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE);
152 (void)opt_strings_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
153#endif
154#ifdef FEAT_FOLDING
155 (void)opt_strings_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
156#endif
157 (void)opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE);
Yegappan Lakshmanan87018252023-09-20 20:20:04 +0200158 (void)opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200159 (void)opt_strings_flags(p_tc, p_tc_values, &tc_flags, FALSE);
160 (void)opt_strings_flags(p_ve, p_ve_values, &ve_flags, TRUE);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +0200161#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +0200162 (void)opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE);
163#endif
164#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
165 (void)opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags, TRUE);
166#endif
167#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
168 (void)opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE);
169#endif
Sean Dewar39c46b42022-05-12 17:44:29 +0100170 (void)opt_strings_flags(p_swb, p_swb_values, &swb_flags, TRUE);
LemonBoy5247b0b2024-07-12 19:30:58 +0200171 (void)opt_strings_flags(p_tcl, p_tcl_values, &tcl_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200172}
173
Yegappan Lakshmananf9dc2782023-05-11 15:02:56 +0100174#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200175/*
176 * Trigger the OptionSet autocommand.
177 * "opt_idx" is the index of the option being set.
178 * "opt_flags" can be OPT_LOCAL etc.
179 * "oldval" the old value
180 * "oldval_l" the old local value (only non-NULL if global and local value
181 * are set)
182 * "oldval_g" the old global value (only non-NULL if global and local value
183 * are set)
184 * "newval" the new value
185 */
186 void
zeertzjq269aa2b2022-11-28 11:36:50 +0000187trigger_optionset_string(
Bram Moolenaardac13472019-09-16 21:06:21 +0200188 int opt_idx,
189 int opt_flags,
190 char_u *oldval,
191 char_u *oldval_l,
192 char_u *oldval_g,
193 char_u *newval)
194{
195 // Don't do this recursively.
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000196 if (oldval == NULL || newval == NULL
197 || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
198 return;
Bram Moolenaardac13472019-09-16 21:06:21 +0200199
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000200 char_u buf_type[7];
201
202 sprintf((char *)buf_type, "%s",
Bram Moolenaardac13472019-09-16 21:06:21 +0200203 (opt_flags & OPT_LOCAL) ? "local" : "global");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000204 set_vim_var_string(VV_OPTION_OLD, oldval, -1);
205 set_vim_var_string(VV_OPTION_NEW, newval, -1);
206 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
207 if (opt_flags & OPT_LOCAL)
208 {
209 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
210 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
Bram Moolenaardac13472019-09-16 21:06:21 +0200211 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000212 if (opt_flags & OPT_GLOBAL)
213 {
214 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
215 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval, -1);
216 }
217 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
218 {
219 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
220 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval_l, -1);
221 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval_g, -1);
222 }
223 if (opt_flags & OPT_MODELINE)
224 {
225 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
226 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
227 }
228 apply_autocmds(EVENT_OPTIONSET,
229 get_option_fullname(opt_idx), NULL, FALSE,
230 NULL);
231 reset_v_option_vars();
Bram Moolenaardac13472019-09-16 21:06:21 +0200232}
233#endif
234
235 static char *
Mike Williams620f0112023-12-05 15:36:06 +0100236illegal_char(char *errbuf, size_t errbuflen, int c)
Bram Moolenaardac13472019-09-16 21:06:21 +0200237{
238 if (errbuf == NULL)
239 return "";
zeertzjq6a8d2e12024-01-17 20:54:49 +0100240 vim_snprintf(errbuf, errbuflen, _(e_illegal_character_str),
Christian Brabandtb39b2402023-11-29 11:34:05 +0100241 (char *)transchar(c));
Bram Moolenaardac13472019-09-16 21:06:21 +0200242 return errbuf;
243}
244
245/*
246 * Check string options in a buffer for NULL value.
247 */
248 void
249check_buf_options(buf_T *buf)
250{
251 check_string_option(&buf->b_p_bh);
252 check_string_option(&buf->b_p_bt);
253 check_string_option(&buf->b_p_fenc);
254 check_string_option(&buf->b_p_ff);
255#ifdef FEAT_FIND_ID
256 check_string_option(&buf->b_p_def);
257 check_string_option(&buf->b_p_inc);
258# ifdef FEAT_EVAL
259 check_string_option(&buf->b_p_inex);
260# endif
261#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +0100262#if defined(FEAT_EVAL)
Bram Moolenaardac13472019-09-16 21:06:21 +0200263 check_string_option(&buf->b_p_inde);
264 check_string_option(&buf->b_p_indk);
265#endif
266#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
267 check_string_option(&buf->b_p_bexpr);
268#endif
269#if defined(FEAT_CRYPT)
270 check_string_option(&buf->b_p_cm);
271#endif
272 check_string_option(&buf->b_p_fp);
273#if defined(FEAT_EVAL)
274 check_string_option(&buf->b_p_fex);
275#endif
276#ifdef FEAT_CRYPT
277 check_string_option(&buf->b_p_key);
278#endif
279 check_string_option(&buf->b_p_kp);
280 check_string_option(&buf->b_p_mps);
281 check_string_option(&buf->b_p_fo);
282 check_string_option(&buf->b_p_flp);
283 check_string_option(&buf->b_p_isk);
Bram Moolenaardac13472019-09-16 21:06:21 +0200284 check_string_option(&buf->b_p_com);
Bram Moolenaardac13472019-09-16 21:06:21 +0200285#ifdef FEAT_FOLDING
286 check_string_option(&buf->b_p_cms);
287#endif
288 check_string_option(&buf->b_p_nf);
Bram Moolenaardac13472019-09-16 21:06:21 +0200289 check_string_option(&buf->b_p_qe);
Bram Moolenaardac13472019-09-16 21:06:21 +0200290#ifdef FEAT_SYN_HL
291 check_string_option(&buf->b_p_syn);
292 check_string_option(&buf->b_s.b_syn_isk);
293#endif
294#ifdef FEAT_SPELL
295 check_string_option(&buf->b_s.b_p_spc);
296 check_string_option(&buf->b_s.b_p_spf);
297 check_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +0200298 check_string_option(&buf->b_s.b_p_spo);
Bram Moolenaardac13472019-09-16 21:06:21 +0200299#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200300 check_string_option(&buf->b_p_sua);
Bram Moolenaardac13472019-09-16 21:06:21 +0200301 check_string_option(&buf->b_p_cink);
302 check_string_option(&buf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +0100303 check_string_option(&buf->b_p_cinsd);
Bram Moolenaardac13472019-09-16 21:06:21 +0200304 parse_cino(buf);
Bram Moolenaar49846fb2022-10-15 16:05:33 +0100305 check_string_option(&buf->b_p_lop);
Bram Moolenaardac13472019-09-16 21:06:21 +0200306 check_string_option(&buf->b_p_ft);
Bram Moolenaardac13472019-09-16 21:06:21 +0200307 check_string_option(&buf->b_p_cinw);
zeertzjq529b9ad2024-06-05 20:27:06 +0200308 check_string_option(&buf->b_p_cot);
Bram Moolenaardac13472019-09-16 21:06:21 +0200309 check_string_option(&buf->b_p_cpt);
310#ifdef FEAT_COMPL_FUNC
311 check_string_option(&buf->b_p_cfu);
312 check_string_option(&buf->b_p_ofu);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +0100313 check_string_option(&buf->b_p_tsrfu);
Bram Moolenaardac13472019-09-16 21:06:21 +0200314#endif
315#ifdef FEAT_EVAL
316 check_string_option(&buf->b_p_tfu);
317#endif
318#ifdef FEAT_KEYMAP
319 check_string_option(&buf->b_p_keymap);
320#endif
321#ifdef FEAT_QUICKFIX
322 check_string_option(&buf->b_p_gp);
323 check_string_option(&buf->b_p_mp);
324 check_string_option(&buf->b_p_efm);
325#endif
326 check_string_option(&buf->b_p_ep);
327 check_string_option(&buf->b_p_path);
328 check_string_option(&buf->b_p_tags);
329 check_string_option(&buf->b_p_tc);
330 check_string_option(&buf->b_p_dict);
331 check_string_option(&buf->b_p_tsr);
Bram Moolenaardac13472019-09-16 21:06:21 +0200332 check_string_option(&buf->b_p_lw);
Bram Moolenaardac13472019-09-16 21:06:21 +0200333 check_string_option(&buf->b_p_bkc);
334 check_string_option(&buf->b_p_menc);
335#ifdef FEAT_VARTABS
336 check_string_option(&buf->b_p_vsts);
337 check_string_option(&buf->b_p_vts);
338#endif
339}
340
341/*
342 * Free the string allocated for an option.
343 * Checks for the string being empty_option. This may happen if we're out of
344 * memory, vim_strsave() returned NULL, which was replaced by empty_option by
345 * check_options().
346 * Does NOT check for P_ALLOCED flag!
347 */
348 void
349free_string_option(char_u *p)
350{
351 if (p != empty_option)
352 vim_free(p);
353}
354
355 void
356clear_string_option(char_u **pp)
357{
358 if (*pp != empty_option)
359 vim_free(*pp);
360 *pp = empty_option;
361}
362
363 void
364check_string_option(char_u **pp)
365{
366 if (*pp == NULL)
367 *pp = empty_option;
368}
369
370/*
371 * Set global value for string option when it's a local option.
372 */
373 static void
374set_string_option_global(
375 int opt_idx, // option index
376 char_u **varp) // pointer to option variable
377{
378 char_u **p, *s;
379
380 // the global value is always allocated
381 if (is_window_local_option(opt_idx))
382 p = (char_u **)GLOBAL_WO(varp);
383 else
384 p = (char_u **)get_option_var(opt_idx);
385 if (!is_global_option(opt_idx)
386 && p != varp
387 && (s = vim_strsave(*varp)) != NULL)
388 {
389 free_string_option(*p);
390 *p = s;
391 }
392}
393
394/*
395 * Set a string option to a new value (without checking the effect).
396 * The string is copied into allocated memory.
397 * if ("opt_idx" == -1) "name" is used, otherwise "opt_idx" is used.
398 * When "set_sid" is zero set the scriptID to current_sctx.sc_sid. When
399 * "set_sid" is SID_NONE don't set the scriptID. Otherwise set the scriptID to
400 * "set_sid".
401 */
402 void
403set_string_option_direct(
404 char_u *name,
405 int opt_idx,
406 char_u *val,
407 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
408 int set_sid UNUSED)
409{
410 char_u *s;
411 char_u **varp;
412 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
413 int idx = opt_idx;
414
415 if (idx == -1) // use name
416 {
417 idx = findoption(name);
418 if (idx < 0) // not found (should not happen)
419 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000420 semsg(_(e_internal_error_str), "set_string_option_direct()");
RestorerZ68ebcee2023-05-31 17:12:14 +0100421 siemsg("For option %s", name);
Bram Moolenaardac13472019-09-16 21:06:21 +0200422 return;
423 }
424 }
425
426 if (is_hidden_option(idx)) // can't set hidden option
427 return;
428
429 s = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000430 if (s == NULL)
431 return;
432
433 varp = (char_u **)get_option_varp_scope(idx,
434 both ? OPT_LOCAL : opt_flags);
435 if ((opt_flags & OPT_FREE) && (get_option_flags(idx) & P_ALLOCED))
436 free_string_option(*varp);
437 *varp = s;
438
439 // For buffer/window local option may also set the global value.
440 if (both)
441 set_string_option_global(idx, varp);
442
443 set_option_flag(idx, P_ALLOCED);
444
445 // When setting both values of a global option with a local value,
446 // make the local value empty, so that the global value is used.
447 if (is_global_local_option(idx) && both)
Bram Moolenaardac13472019-09-16 21:06:21 +0200448 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000449 free_string_option(*varp);
450 *varp = empty_option;
Bram Moolenaardac13472019-09-16 21:06:21 +0200451 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000452# ifdef FEAT_EVAL
453 if (set_sid != SID_NONE)
454 {
455 sctx_T script_ctx;
456
457 if (set_sid == 0)
458 script_ctx = current_sctx;
459 else
460 {
461 script_ctx.sc_sid = set_sid;
462 script_ctx.sc_seq = 0;
463 script_ctx.sc_lnum = 0;
464 script_ctx.sc_version = 1;
465 }
466 set_option_sctx_idx(idx, opt_flags, script_ctx);
467 }
468# endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200469}
470
Dominique Pellee764d1b2023-03-12 21:20:59 +0000471#if defined(FEAT_PROP_POPUP) || \
472 (defined(FEAT_DIFF) && defined(FEAT_FOLDING)) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200473/*
474 * Like set_string_option_direct(), but for a window-local option in "wp".
475 * Blocks autocommands to avoid the old curwin becoming invalid.
476 */
477 void
478set_string_option_direct_in_win(
479 win_T *wp,
480 char_u *name,
481 int opt_idx,
482 char_u *val,
483 int opt_flags,
484 int set_sid)
485{
486 win_T *save_curwin = curwin;
487
488 block_autocmds();
489 curwin = wp;
490 curbuf = curwin->w_buffer;
491 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
492 curwin = save_curwin;
493 curbuf = curwin->w_buffer;
494 unblock_autocmds();
495}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000496#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200497
Dominique Pelle748b3082022-01-08 12:41:16 +0000498#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200499/*
500 * Like set_string_option_direct(), but for a buffer-local option in "buf".
501 * Blocks autocommands to avoid the old curbuf becoming invalid.
502 */
503 void
504set_string_option_direct_in_buf(
505 buf_T *buf,
506 char_u *name,
507 int opt_idx,
508 char_u *val,
509 int opt_flags,
510 int set_sid)
511{
512 buf_T *save_curbuf = curbuf;
513
514 block_autocmds();
515 curbuf = buf;
516 curwin->w_buffer = curbuf;
517 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
518 curbuf = save_curbuf;
519 curwin->w_buffer = curbuf;
520 unblock_autocmds();
521}
Dominique Pelle748b3082022-01-08 12:41:16 +0000522#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200523
524/*
525 * Set a string option to a new value, and handle the effects.
526 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100527 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaardac13472019-09-16 21:06:21 +0200528 */
529 char *
530set_string_option(
531 int opt_idx,
532 char_u *value,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +0000533 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Christian Brabandtb39b2402023-11-29 11:34:05 +0100534 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +0100535 size_t errbuflen)
Bram Moolenaardac13472019-09-16 21:06:21 +0200536{
537 char_u *s;
538 char_u **varp;
539 char_u *oldval;
540#if defined(FEAT_EVAL)
541 char_u *oldval_l = NULL;
542 char_u *oldval_g = NULL;
543 char_u *saved_oldval = NULL;
544 char_u *saved_oldval_l = NULL;
545 char_u *saved_oldval_g = NULL;
546 char_u *saved_newval = NULL;
547#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100548 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +0200549 int value_checked = FALSE;
550
551 if (is_hidden_option(opt_idx)) // don't set hidden option
552 return NULL;
553
Bram Moolenaar7f009df2020-03-16 20:27:38 +0100554 s = vim_strsave(value == NULL ? (char_u *)"" : value);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000555 if (s == NULL)
556 return NULL;
557
558 varp = (char_u **)get_option_varp_scope(opt_idx,
559 (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
560 ? (is_global_local_option(opt_idx)
561 ? OPT_GLOBAL : OPT_LOCAL)
562 : opt_flags);
563 oldval = *varp;
564#if defined(FEAT_EVAL)
565 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +0200566 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000567 oldval_l = *(char_u **)get_option_varp_scope(opt_idx, OPT_LOCAL);
568 oldval_g = *(char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
Bram Moolenaardac13472019-09-16 21:06:21 +0200569 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000570#endif
571 *varp = s;
572
573#if defined(FEAT_EVAL)
574 if (!starting
575# ifdef FEAT_CRYPT
576 && !is_crypt_key_option(opt_idx)
577# endif
578 )
579 {
580 if (oldval_l != NULL)
581 saved_oldval_l = vim_strsave(oldval_l);
582 if (oldval_g != NULL)
583 saved_oldval_g = vim_strsave(oldval_g);
584 saved_oldval = vim_strsave(oldval);
585 saved_newval = vim_strsave(s);
586 }
587#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000588 if ((errmsg = did_set_string_option(opt_idx, varp, oldval, value, errbuf,
Christian Brabandtb39b2402023-11-29 11:34:05 +0100589 errbuflen, opt_flags, OP_NONE, &value_checked)) == NULL)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000590 did_set_option(opt_idx, opt_flags, TRUE, value_checked);
591
592#if defined(FEAT_EVAL)
593 // call autocommand after handling side effects
594 if (errmsg == NULL)
595 trigger_optionset_string(opt_idx, opt_flags,
596 saved_oldval, saved_oldval_l,
597 saved_oldval_g, saved_newval);
598 vim_free(saved_oldval);
599 vim_free(saved_oldval_l);
600 vim_free(saved_oldval_g);
601 vim_free(saved_newval);
602#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100603 return errmsg;
Bram Moolenaardac13472019-09-16 21:06:21 +0200604}
605
606/*
607 * Return TRUE if "val" is a valid 'filetype' name.
608 * Also used for 'syntax' and 'keymap'.
609 */
610 static int
611valid_filetype(char_u *val)
612{
613 return valid_name(val, ".-_");
614}
615
616#ifdef FEAT_STL_OPT
617/*
618 * Check validity of options with the 'statusline' format.
zeertzjq5dc294a2022-04-15 13:17:57 +0100619 * Return an untranslated error message or NULL.
Bram Moolenaardac13472019-09-16 21:06:21 +0200620 */
621 static char *
622check_stl_option(char_u *s)
623{
Bram Moolenaardac13472019-09-16 21:06:21 +0200624 int groupdepth = 0;
Christian Brabandtb39b2402023-11-29 11:34:05 +0100625 static char errbuf[ERR_BUFLEN];
626 int errbuflen = ERR_BUFLEN;
Bram Moolenaardac13472019-09-16 21:06:21 +0200627
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100628 while (*s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200629 {
630 // Check for valid keys after % sequences
631 while (*s && *s != '%')
632 s++;
633 if (!*s)
634 break;
635 s++;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +0000636 if (*s == '%' || *s == STL_TRUNCMARK || *s == STL_SEPARATE)
Bram Moolenaardac13472019-09-16 21:06:21 +0200637 {
638 s++;
639 continue;
640 }
641 if (*s == ')')
642 {
643 s++;
644 if (--groupdepth < 0)
645 break;
646 continue;
647 }
648 if (*s == '-')
649 s++;
650 while (VIM_ISDIGIT(*s))
651 s++;
652 if (*s == STL_USER_HL)
653 continue;
654 if (*s == '.')
655 {
656 s++;
657 while (*s && VIM_ISDIGIT(*s))
658 s++;
659 }
660 if (*s == '(')
661 {
662 groupdepth++;
663 continue;
664 }
665 if (vim_strchr(STL_ALL, *s) == NULL)
666 {
Christian Brabandtb39b2402023-11-29 11:34:05 +0100667 return illegal_char(errbuf, errbuflen, *s);
Bram Moolenaardac13472019-09-16 21:06:21 +0200668 }
669 if (*s == '{')
670 {
zeertzjq5dc294a2022-04-15 13:17:57 +0100671 int reevaluate = (*++s == '%');
shadmansaleh30e3de22021-05-15 17:23:28 +0200672
zeertzjq5dc294a2022-04-15 13:17:57 +0100673 if (reevaluate && *++s == '}')
674 // "}" is not allowed immediately after "%{%"
Christian Brabandtb39b2402023-11-29 11:34:05 +0100675 return illegal_char(errbuf, errbuflen, '}');
shadmansaleh30e3de22021-05-15 17:23:28 +0200676 while ((*s != '}' || (reevaluate && s[-1] != '%')) && *s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200677 s++;
678 if (*s != '}')
zeertzjq5dc294a2022-04-15 13:17:57 +0100679 return e_unclosed_expression_sequence;
Bram Moolenaardac13472019-09-16 21:06:21 +0200680 }
681 }
Bram Moolenaardac13472019-09-16 21:06:21 +0200682 if (groupdepth != 0)
zeertzjq5dc294a2022-04-15 13:17:57 +0100683 return e_unbalanced_groups;
Bram Moolenaardac13472019-09-16 21:06:21 +0200684 return NULL;
685}
686#endif
687
688/*
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +0000689 * Check for a "normal" directory or file name in some options. Disallow a
690 * path separator (slash and/or backslash), wildcards and characters that are
691 * often illegal in a file name. Be more permissive if "secure" is off.
692 */
693 static int
694check_illegal_path_names(int opt_idx, char_u **varp)
695{
696 return (((get_option_flags(opt_idx) & P_NFNAME)
697 && vim_strpbrk(*varp, (char_u *)(secure
698 ? "/\\*?[|;&<>\r\n" : "/\\*?[<>\r\n")) != NULL)
699 || ((get_option_flags(opt_idx) & P_NDNAME)
700 && vim_strpbrk(*varp, (char_u *)"*?[|;&<>\r\n") != NULL));
701}
702
703/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000704 * An option that accepts a list of flags is changed.
705 * e.g. 'viewoptions', 'switchbuf', 'casemap', etc.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000706 */
707 static char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000708did_set_opt_flags(char_u *val, char **values, unsigned *flagp, int list)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000709{
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000710 if (opt_strings_flags(val, values, flagp, list) == FAIL)
711 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000712
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000713 return NULL;
714}
715
716/*
717 * An option that accepts a list of string values is changed.
718 * e.g. 'nrformats', 'scrollopt', 'wildoptions', etc.
719 */
720 static char *
721did_set_opt_strings(char_u *val, char **values, int list)
722{
723 return did_set_opt_flags(val, values, NULL, list);
724}
725
726/*
727 * An option which is a list of flags is set. Valid values are in 'flags'.
728 */
729 static char *
Christian Brabandtb39b2402023-11-29 11:34:05 +0100730did_set_option_listflag(
731 char_u *val,
732 char_u *flags,
733 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +0100734 size_t errbuflen)
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000735{
736 char_u *s;
737
Yegappan Lakshmananc727b192023-03-03 12:26:15 +0000738 for (s = val; *s; ++s)
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000739 if (vim_strchr(flags, *s) == NULL)
Christian Brabandtb39b2402023-11-29 11:34:05 +0100740 return illegal_char(errbuf, errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000741
742 return NULL;
743}
744
745/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200746 * Expand an option that accepts a list of fixed string values with known
747 * number of items.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200748 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200749 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200750expand_set_opt_string(
751 optexpand_T *args,
752 char **values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +0200753 size_t numValues,
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200754 int *numMatches,
755 char_u ***matches)
756{
757 char_u *p;
758 regmatch_T *regmatch = args->oe_regmatch;
759 int include_orig_val = args->oe_include_orig_val;
760 char_u *option_val = args->oe_opt_value;
761
762 // Assume numValues is small since they are fixed enums, so just allocate
763 // upfront instead of needing two passes to calculate output size.
764 *matches = ALLOC_MULT(char_u *, numValues + 1);
765 if (*matches == NULL)
766 return FAIL;
767
768 int count = 0;
769
770 if (include_orig_val && *option_val != NUL)
771 {
772 p = vim_strsave(option_val);
773 if (p == NULL)
774 {
775 VIM_CLEAR(*matches);
776 return FAIL;
777 }
778 (*matches)[count++] = p;
779 }
780
781 for (char **val = values; *val != NULL; val++)
782 {
783 if (include_orig_val && *option_val != NUL)
784 {
785 if (STRCMP((char_u*)*val, option_val) == 0)
786 continue;
787 }
788 if (vim_regexec(regmatch, (char_u*)(*val), (colnr_T)0))
789 {
790 p = vim_strsave((char_u*)*val);
791 if (p == NULL)
792 {
793 if (count == 0)
794 {
795 VIM_CLEAR(*matches);
796 return FAIL;
797 }
798 else
799 break;
800 }
801 (*matches)[count++] = p;
802 }
803 }
804 if (count == 0)
805 {
806 VIM_CLEAR(*matches);
807 return FAIL;
808 }
809 *numMatches = count;
810 return OK;
811}
812
813static char_u *set_opt_callback_orig_option = NULL;
814static char_u *((*set_opt_callback_func)(expand_T *, int));
815
816/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200817 * Callback used by expand_set_opt_generic to also include the original value
818 * as the first item.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200819 */
820 static char_u *
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200821expand_set_opt_generic_cb(expand_T *xp, int idx)
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200822{
823 if (idx == 0)
824 {
825 if (set_opt_callback_orig_option != NULL)
826 return set_opt_callback_orig_option;
827 else
828 return (char_u *)""; // empty strings are ignored
829 }
830 return set_opt_callback_func(xp, idx - 1);
831}
832
833/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200834 * Expand an option with a callback that iterates through a list of possible
835 * names using an index.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200836 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200837 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200838expand_set_opt_generic(
839 optexpand_T *args,
840 char_u *((*func)(expand_T *, int)),
841 int *numMatches,
842 char_u ***matches)
843{
844 int ret;
845
846 set_opt_callback_orig_option = args->oe_include_orig_val ?
847 args->oe_opt_value : NULL;
848 set_opt_callback_func = func;
849
850 ret = ExpandGeneric(
851 (char_u*)"", // not using fuzzy as currently EXPAND_STRING_SETTING doesn't use it
852 args->oe_xp,
853 args->oe_regmatch,
854 matches,
855 numMatches,
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200856 expand_set_opt_generic_cb,
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200857 FALSE);
858
859 set_opt_callback_orig_option = NULL;
860 set_opt_callback_func = NULL;
861 return ret;
862}
863
Christian Brabandt9960ebc2023-10-05 22:17:09 +0200864# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200865static garray_T *expand_cb_ga;
866static optexpand_T *expand_cb_args;
867
868/*
869 * Callback provided to a function in expand_set_opt_callback. Will perform
870 * regex matching against the value and add to the list.
871 *
872 * Returns OK usually. Returns FAIL if it failed to allocate memory, and the
873 * caller should terminate the enumeration.
874 */
875 static int
876expand_set_opt_callback_cb(char_u *val)
877{
878 regmatch_T *regmatch = expand_cb_args->oe_regmatch;
879 expand_T *xp = expand_cb_args->oe_xp;
880 garray_T *ga = expand_cb_ga;
881 char_u *str;
882
883 if (val == NULL || *val == NUL)
884 return OK;
885
886 if (xp->xp_pattern[0] != NUL &&
887 !vim_regexec(regmatch, val, (colnr_T)0))
888 return OK;
889
890 str = vim_strsave_escaped(val, (char_u *)" \t\\");
891
892 if (str == NULL)
893 return FAIL;
894
895 if (ga_grow(ga, 1) == FAIL)
896 {
897 vim_free(str);
898 return FAIL;
899 }
900
901 ((char_u **)ga->ga_data)[ga->ga_len] = str;
902 ++ga->ga_len;
903 return OK;
904}
905
906/*
907 * Expand an option with a provided function that takes a callback. The
908 * function will enumerate through all options and call the callback to add it
909 * to the list.
910 *
911 * "func" is the enumerator function that will generate the list of options.
912 * "func_params" is a single parameter that will be passed to func.
913 */
914 static int
915expand_set_opt_callback(
916 optexpand_T *args,
917 void (*func)(optexpand_T *, void* params, int (*cb)(char_u *val)),
918 void *func_params,
919 int *numMatches,
920 char_u ***matches)
921{
922 garray_T ga;
923 int include_orig_val = args->oe_include_orig_val;
924 char_u *option_val = args->oe_opt_value;
925
926 ga_init2(&ga, sizeof(char *), 30);
927
928 if (include_orig_val && *option_val != NUL)
929 {
930 char_u *p = vim_strsave(option_val);
931 if (p == NULL)
932 return FAIL;
933 if (ga_grow(&ga, 1) == FAIL)
934 {
935 vim_free(p);
936 return FAIL;
937 }
938 ((char_u **)ga.ga_data)[ga.ga_len] = p;
939 ++ga.ga_len;
940 }
941
942 expand_cb_ga = &ga;
943 expand_cb_args = args;
944
945 func(args, func_params, expand_set_opt_callback_cb);
946
947 expand_cb_ga = NULL;
948 expand_cb_args = NULL;
949
950 *matches = ga.ga_data;
951 *numMatches = ga.ga_len;
952 return OK;
953}
Christian Brabandt9960ebc2023-10-05 22:17:09 +0200954#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200955
956/*
957 * Expand an option which is a list of flags.
958 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200959 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200960expand_set_opt_listflag(
961 optexpand_T *args,
962 char_u *flags,
963 int *numMatches,
964 char_u ***matches)
965{
966 char_u *p;
967 char_u *option_val = args->oe_opt_value;
968 char_u *cmdline_val = args->oe_set_arg;
969 int append = args->oe_append;
970 int include_orig_val = args->oe_include_orig_val && (*option_val != NUL);
971
Yee Cheng Chin6d113472023-10-02 21:38:39 +0200972 size_t num_flags = STRLEN(flags);
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200973
974 // Assume we only have small number of flags, so just allocate max size.
975 *matches = ALLOC_MULT(char_u *, num_flags + 1);
976 if (*matches == NULL)
977 return FAIL;
978
979 int count = 0;
980
981 if (include_orig_val)
982 {
983 p = vim_strsave(option_val);
984 if (p == NULL)
985 {
986 VIM_CLEAR(*matches);
987 return FAIL;
988 }
989 (*matches)[count++] = p;
990 }
991
992 for (char_u *flag = flags; *flag != NUL; flag++)
993 {
994 if (append && vim_strchr(option_val, *flag) != NULL)
995 continue;
996
997 if (vim_strchr(cmdline_val, *flag) == NULL)
998 {
999 if (include_orig_val
1000 && option_val[1] == NUL
1001 && *flag == option_val[0])
1002 {
1003 // This value is already used as the first choice as it's the
1004 // existing flag. Just skip it to avoid duplicate.
1005 continue;
1006 }
1007 p = vim_strnsave(flag, 1);
1008 if (p == NULL)
1009 {
1010 if (count == 0)
1011 {
1012 VIM_CLEAR(*matches);
1013 return FAIL;
1014 }
1015 else
1016 break;
1017 }
1018 (*matches)[count++] = p;
1019 }
1020 }
1021
1022 if (count == 0)
1023 {
1024 VIM_CLEAR(*matches);
1025 return FAIL;
1026 }
1027 *numMatches = count;
1028 return OK;
1029}
1030
1031/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001032 * The 'ambiwidth' option is changed.
1033 */
1034 char *
1035did_set_ambiwidth(optset_T *args UNUSED)
1036{
1037 if (check_opt_strings(p_ambw, p_ambw_values, FALSE) != OK)
1038 return e_invalid_argument;
1039
1040 return check_chars_options();
1041}
1042
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001043 int
1044expand_set_ambiwidth(optexpand_T *args, int *numMatches, char_u ***matches)
1045{
1046 return expand_set_opt_string(
1047 args,
1048 p_ambw_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001049 ARRAY_LENGTH(p_ambw_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001050 numMatches,
1051 matches);
1052}
1053
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001054/*
1055 * The 'background' option is changed.
1056 */
1057 char *
Gregory Anders83ad2722024-01-03 19:48:51 +01001058did_set_background(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001059{
1060 if (check_opt_strings(p_bg, p_bg_values, FALSE) == FAIL)
1061 return e_invalid_argument;
1062
Gregory Anders83ad2722024-01-03 19:48:51 +01001063 if (args->os_oldval.string != NULL && args->os_oldval.string[0] == *p_bg)
1064 // Value was not changed
1065 return NULL;
1066
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001067#ifdef FEAT_EVAL
1068 int dark = (*p_bg == 'd');
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001069#endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001070
1071 init_highlight(FALSE, FALSE);
1072
1073#ifdef FEAT_EVAL
1074 if (dark != (*p_bg == 'd')
1075 && get_var_value((char_u *)"g:colors_name") != NULL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001076 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001077 // The color scheme must have set 'background' back to another
1078 // value, that's not what we want here. Disable the color
1079 // scheme and set the colors again.
1080 do_unlet((char_u *)"g:colors_name", TRUE);
1081 free_string_option(p_bg);
1082 p_bg = vim_strsave((char_u *)(dark ? "dark" : "light"));
1083 check_string_option(&p_bg);
1084 init_highlight(FALSE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001085 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001086#endif
1087#ifdef FEAT_TERMINAL
1088 term_update_colors_all();
1089#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001090
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001091 return NULL;
1092}
1093
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001094 int
1095expand_set_background(optexpand_T *args, int *numMatches, char_u ***matches)
1096{
1097 return expand_set_opt_string(
1098 args,
1099 p_bg_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001100 ARRAY_LENGTH(p_bg_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001101 numMatches,
1102 matches);
1103}
1104
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001105/*
1106 * The 'backspace' option is changed.
1107 */
1108 char *
1109did_set_backspace(optset_T *args UNUSED)
1110{
1111 if (VIM_ISDIGIT(*p_bs))
1112 {
1113 if (*p_bs > '3' || p_bs[1] != NUL)
1114 return e_invalid_argument;
1115 }
1116 else if (check_opt_strings(p_bs, p_bs_values, TRUE) != OK)
1117 return e_invalid_argument;
1118
1119 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001120}
1121
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001122 int
1123expand_set_backspace(optexpand_T *args, int *numMatches, char_u ***matches)
1124{
1125 return expand_set_opt_string(
1126 args,
1127 p_bs_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001128 ARRAY_LENGTH(p_bs_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001129 numMatches,
1130 matches);
1131}
1132
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001133/*
1134 * The 'backupcopy' option is changed.
1135 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001136 char *
1137did_set_backupcopy(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001138{
1139 char_u *bkc = p_bkc;
1140 unsigned int *flags = &bkc_flags;
1141 char *errmsg = NULL;
1142
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001143 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001144 {
1145 bkc = curbuf->b_p_bkc;
1146 flags = &curbuf->b_bkc_flags;
1147 }
1148
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001149 if ((args->os_flags & OPT_LOCAL) && *bkc == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001150 // make the local value empty: use the global value
1151 *flags = 0;
1152 else
1153 {
1154 if (opt_strings_flags(bkc, p_bkc_values, flags, TRUE) != OK)
1155 errmsg = e_invalid_argument;
1156 if ((((int)*flags & BKC_AUTO) != 0)
1157 + (((int)*flags & BKC_YES) != 0)
1158 + (((int)*flags & BKC_NO) != 0) != 1)
1159 {
1160 // Must have exactly one of "auto", "yes" and "no".
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001161 (void)opt_strings_flags(args->os_oldval.string, p_bkc_values,
1162 flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001163 errmsg = e_invalid_argument;
1164 }
1165 }
1166
1167 return errmsg;
1168}
1169
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001170 int
1171expand_set_backupcopy(optexpand_T *args, int *numMatches, char_u ***matches)
1172{
1173 return expand_set_opt_string(
1174 args,
1175 p_bkc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001176 ARRAY_LENGTH(p_bkc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001177 numMatches,
1178 matches);
1179}
1180
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001181/*
1182 * The 'backupext' or the 'patchmode' option is changed.
1183 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001184 char *
1185did_set_backupext_or_patchmode(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001186{
1187 if (STRCMP(*p_bex == '.' ? p_bex + 1 : p_bex,
1188 *p_pm == '.' ? p_pm + 1 : p_pm) == 0)
1189 return e_backupext_and_patchmode_are_equal;
1190
1191 return NULL;
1192}
1193
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001194/*
1195 * The 'belloff' option is changed.
1196 */
1197 char *
1198did_set_belloff(optset_T *args UNUSED)
1199{
1200 return did_set_opt_flags(p_bo, p_bo_values, &bo_flags, TRUE);
1201}
1202
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001203 int
1204expand_set_belloff(optexpand_T *args, int *numMatches, char_u ***matches)
1205{
1206 return expand_set_opt_string(
1207 args,
1208 p_bo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001209 ARRAY_LENGTH(p_bo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001210 numMatches,
1211 matches);
1212}
1213
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001214#if defined(FEAT_LINEBREAK) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001215/*
zeertzjqeac3fdc2024-02-03 18:08:09 +01001216 * The 'breakat' option is changed.
1217 */
1218 char *
1219did_set_breakat(optset_T *args UNUSED)
1220{
1221 char_u *p;
1222 int i;
1223
1224 for (i = 0; i < 256; i++)
1225 breakat_flags[i] = FALSE;
1226
1227 if (p_breakat != NULL)
1228 for (p = p_breakat; *p; p++)
1229 breakat_flags[*p] = TRUE;
1230
1231 return NULL;
1232}
1233
1234/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001235 * The 'breakindentopt' option is changed.
1236 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001237 char *
Millyb38700a2024-10-22 22:59:39 +02001238did_set_breakindentopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001239{
Millyb38700a2024-10-22 22:59:39 +02001240 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001241
Millyb38700a2024-10-22 22:59:39 +02001242 if (briopt_check(*varp, varp == &curwin->w_p_briopt ? curwin : NULL)
1243 == FAIL)
1244 return e_invalid_argument;
1245
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001246 // list setting requires a redraw
Millyb38700a2024-10-22 22:59:39 +02001247 if (varp == &curwin->w_p_briopt && curwin->w_briopt_list)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001248 redraw_all_later(UPD_NOT_VALID);
1249
Millyb38700a2024-10-22 22:59:39 +02001250 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001251}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001252
1253 int
1254expand_set_breakindentopt(optexpand_T *args, int *numMatches, char_u ***matches)
1255{
1256 return expand_set_opt_string(
1257 args,
1258 p_briopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001259 ARRAY_LENGTH(p_briopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001260 numMatches,
1261 matches);
1262}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001263#endif
1264
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001265#if defined(FEAT_BROWSE) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001266/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001267 * The 'browsedir' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001268 */
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00001269 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001270did_set_browsedir(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001271{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001272 if (check_opt_strings(p_bsdir, p_bsdir_values, FALSE) != OK
1273 && !mch_isdir(p_bsdir))
1274 return e_invalid_argument;
1275
1276 return NULL;
1277}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001278
1279 int
1280expand_set_browsedir(optexpand_T *args, int *numMatches, char_u ***matches)
1281{
1282 return expand_set_opt_string(
1283 args,
1284 p_bsdir_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001285 ARRAY_LENGTH(p_bsdir_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001286 numMatches,
1287 matches);
1288}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001289#endif
1290
1291/*
1292 * The 'bufhidden' option is changed.
1293 */
1294 char *
1295did_set_bufhidden(optset_T *args UNUSED)
1296{
1297 return did_set_opt_strings(curbuf->b_p_bh, p_bufhidden_values, FALSE);
1298}
1299
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001300 int
1301expand_set_bufhidden(optexpand_T *args, int *numMatches, char_u ***matches)
1302{
1303 return expand_set_opt_string(
1304 args,
1305 p_bufhidden_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001306 ARRAY_LENGTH(p_bufhidden_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001307 numMatches,
1308 matches);
1309}
1310
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001311/*
1312 * The 'buftype' option is changed.
1313 */
1314 char *
1315did_set_buftype(optset_T *args UNUSED)
1316{
1317 if (check_opt_strings(curbuf->b_p_bt, p_buftype_values, FALSE) != OK)
1318 return e_invalid_argument;
1319
1320 if (curwin->w_status_height)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001321 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001322 curwin->w_redr_status = TRUE;
1323 redraw_later(UPD_VALID);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001324 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001325 curbuf->b_help = (curbuf->b_p_bt[0] == 'h');
1326 redraw_titles();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001327
1328 return NULL;
1329}
1330
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001331 int
1332expand_set_buftype(optexpand_T *args, int *numMatches, char_u ***matches)
1333{
1334 return expand_set_opt_string(
1335 args,
1336 p_buftype_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001337 ARRAY_LENGTH(p_buftype_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001338 numMatches,
1339 matches);
1340}
1341
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001342/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001343 * The 'casemap' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001344 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001345 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001346did_set_casemap(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001347{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001348 return did_set_opt_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
1349}
1350
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001351 int
1352expand_set_casemap(optexpand_T *args, int *numMatches, char_u ***matches)
1353{
1354 return expand_set_opt_string(
1355 args,
1356 p_cmp_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001357 ARRAY_LENGTH(p_cmp_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001358 numMatches,
1359 matches);
1360}
1361
1362#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1363 int
1364expand_set_clipboard(optexpand_T *args, int *numMatches, char_u ***matches)
1365{
1366 return expand_set_opt_string(
1367 args,
1368 p_cb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001369 ARRAY_LENGTH(p_cb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001370 numMatches,
1371 matches);
1372}
1373#endif
1374
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001375/*
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001376 * The global 'listchars' or 'fillchars' option is changed.
1377 */
1378 static char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01001379did_set_global_listfillchars(char_u *val, int opt_lcs, int opt_flags,
1380 char *errbuf, size_t errbuflen)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001381{
1382 char *errmsg = NULL;
1383 char_u **local_ptr = opt_lcs ? &curwin->w_p_lcs : &curwin->w_p_fcs;
1384
1385 // only apply the global value to "curwin" when it does not have a
1386 // local value
1387 if (opt_lcs)
1388 errmsg = set_listchars_option(curwin, val,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001389 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL),
1390 errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001391 else
1392 errmsg = set_fillchars_option(curwin, val,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001393 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL),
1394 errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001395 if (errmsg != NULL)
1396 return errmsg;
1397
1398 tabpage_T *tp;
1399 win_T *wp;
1400
1401 // If the current window is set to use the global
1402 // 'listchars'/'fillchars' value, clear the window-local value.
1403 if (!(opt_flags & OPT_GLOBAL))
1404 clear_string_option(local_ptr);
1405 FOR_ALL_TAB_WINDOWS(tp, wp)
1406 {
1407 // If the current window has a local value need to apply it
1408 // again, it was changed when setting the global value.
1409 // If no error was returned above, we don't expect an error
1410 // here, so ignore the return value.
1411 if (opt_lcs)
1412 {
1413 if (*wp->w_p_lcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001414 (void)set_listchars_option(wp, wp->w_p_lcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001415 }
1416 else
1417 {
1418 if (*wp->w_p_fcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001419 (void)set_fillchars_option(wp, wp->w_p_fcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001420 }
1421 }
1422
1423 redraw_all_later(UPD_NOT_VALID);
1424
1425 return NULL;
1426}
1427
1428/*
1429 * The 'fillchars' option or the 'listchars' option is changed.
1430 */
1431 char *
1432did_set_chars_option(optset_T *args)
1433{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001434 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001435 char *errmsg = NULL;
1436
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001437 if ( varp == &p_lcs // global 'listchars'
1438 || varp == &p_fcs) // global 'fillchars'
1439 errmsg = did_set_global_listfillchars(*varp, varp == &p_lcs,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001440 args->os_flags, args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001441 else if (varp == &curwin->w_p_lcs) // local 'listchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001442 errmsg = set_listchars_option(curwin, *varp, TRUE,
1443 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001444 else if (varp == &curwin->w_p_fcs) // local 'fillchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001445 errmsg = set_fillchars_option(curwin, *varp, TRUE,
1446 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001447
1448 return errmsg;
1449}
1450
1451/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001452 * Expand 'fillchars' or 'listchars' option value.
1453 */
1454 int
1455expand_set_chars_option(optexpand_T *args, int *numMatches, char_u ***matches)
1456{
1457 char_u **varp = (char_u **)args->oe_varp;
1458 int is_lcs = (varp == &p_lcs || varp == &curwin->w_p_lcs);
1459 return expand_set_opt_generic(
1460 args,
1461 is_lcs ? get_listchars_name : get_fillchars_name,
1462 numMatches,
1463 matches);
1464}
1465
1466/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001467 * The 'cinoptions' option is changed.
1468 */
1469 char *
1470did_set_cinoptions(optset_T *args UNUSED)
1471{
1472 // TODO: recognize errors
1473 parse_cino(curbuf);
1474
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001475 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001476}
1477
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00001478#if defined(FEAT_SYN_HL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001479/*
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001480 * The 'colorcolumn' option is changed.
1481 */
1482 char *
Millya441a3e2024-10-22 22:43:01 +02001483did_set_colorcolumn(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001484{
Millya441a3e2024-10-22 22:43:01 +02001485 char_u **varp = (char_u **)args->os_varp;
1486
1487 return check_colorcolumn(*varp, varp == &curwin->w_p_cc ? curwin : NULL);
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001488}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001489#endif
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001490
1491/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001492 * The 'comments' option is changed.
1493 */
1494 char *
1495did_set_comments(optset_T *args)
1496{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001497 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001498 char_u *s;
1499 char *errmsg = NULL;
1500
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001501 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001502 {
1503 while (*s && *s != ':')
1504 {
1505 if (vim_strchr((char_u *)COM_ALL, *s) == NULL
1506 && !VIM_ISDIGIT(*s) && *s != '-')
1507 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01001508 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001509 break;
1510 }
1511 ++s;
1512 }
1513 if (*s++ == NUL)
1514 errmsg = e_missing_colon;
1515 else if (*s == ',' || *s == NUL)
1516 errmsg = e_zero_length_string;
1517 if (errmsg != NULL)
1518 break;
1519 while (*s && *s != ',')
1520 {
1521 if (*s == '\\' && s[1] != NUL)
1522 ++s;
1523 ++s;
1524 }
1525 s = skip_to_option_part(s);
1526 }
1527
1528 return errmsg;
1529}
1530
1531#if defined(FEAT_FOLDING) || defined(PROTO)
1532/*
1533 * The 'commentstring' option is changed.
1534 */
1535 char *
1536did_set_commentstring(optset_T *args)
1537{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001538 char_u **varp = (char_u **)args->os_varp;
1539
1540 if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001541 return e_commentstring_must_be_empty_or_contain_str;
1542
1543 return NULL;
1544}
1545#endif
1546
1547/*
1548 * The 'complete' option is changed.
1549 */
1550 char *
1551did_set_complete(optset_T *args)
1552{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001553 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001554 char_u *s;
1555
1556 // check if it is a valid value for 'complete' -- Acevedo
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001557 for (s = *varp; *s;)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001558 {
1559 while (*s == ',' || *s == ' ')
1560 s++;
1561 if (!*s)
1562 break;
1563 if (vim_strchr((char_u *)".wbuksid]tU", *s) == NULL)
Christian Brabandtb39b2402023-11-29 11:34:05 +01001564 return illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001565 if (*++s != NUL && *s != ',' && *s != ' ')
1566 {
1567 if (s[-1] == 'k' || s[-1] == 's')
1568 {
1569 // skip optional filename after 'k' and 's'
1570 while (*s && *s != ',' && *s != ' ')
1571 {
1572 if (*s == '\\' && s[1] != NUL)
1573 ++s;
1574 ++s;
1575 }
1576 }
1577 else
1578 {
1579 if (args->os_errbuf != NULL)
1580 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01001581 vim_snprintf((char *)args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001582 _(e_illegal_character_after_chr), *--s);
1583 return args->os_errbuf;
1584 }
1585 return "";
1586 }
1587 }
1588 }
1589
1590 return NULL;
1591}
1592
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001593 int
1594expand_set_complete(optexpand_T *args, int *numMatches, char_u ***matches)
1595{
1596 static char *(p_cpt_values[]) = {
1597 ".", "w", "b", "u", "k", "kspell", "s", "i", "d", "]", "t", "U",
1598 NULL};
1599 return expand_set_opt_string(
1600 args,
1601 p_cpt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001602 ARRAY_LENGTH(p_cpt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001603 numMatches,
1604 matches);
1605}
1606
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001607/*
1608 * The 'completeopt' option is changed.
1609 */
1610 char *
1611did_set_completeopt(optset_T *args UNUSED)
1612{
zeertzjq529b9ad2024-06-05 20:27:06 +02001613 char_u *cot = p_cot;
1614 unsigned *flags = &cot_flags;
1615
1616 if (args->os_flags & OPT_LOCAL)
1617 {
1618 cot = curbuf->b_p_cot;
1619 flags = &curbuf->b_cot_flags;
1620 }
1621
1622 if (check_opt_strings(cot, p_cot_values, TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001623 return e_invalid_argument;
1624
zeertzjq529b9ad2024-06-05 20:27:06 +02001625 if (opt_strings_flags(cot, p_cot_values, flags, TRUE) != OK)
1626 return e_invalid_argument;
1627
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001628 return NULL;
1629}
1630
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001631 int
1632expand_set_completeopt(optexpand_T *args, int *numMatches, char_u ***matches)
1633{
1634 return expand_set_opt_string(
1635 args,
1636 p_cot_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001637 ARRAY_LENGTH(p_cot_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001638 numMatches,
1639 matches);
1640}
1641
glepnir6a89c942024-10-01 20:32:12 +02001642/*
1643 * The 'completeitemalign' option is changed.
1644 */
1645 char *
1646did_set_completeitemalign(optset_T *args UNUSED)
1647{
1648 char_u *p = p_cia;
1649 unsigned new_cia_flags = 0;
1650 int seen[3] = { FALSE, FALSE, FALSE };
1651 int count = 0;
1652 char_u buf[10];
1653
1654 while (*p)
1655 {
1656 copy_option_part(&p, buf, sizeof(buf), ",");
1657 if (count >= 3)
1658 return e_invalid_argument;
1659
1660 if (STRCMP(buf, "abbr") == 0)
1661 {
1662 if (seen[CPT_ABBR])
1663 return e_invalid_argument;
1664 new_cia_flags = new_cia_flags * 10 + CPT_ABBR;
1665 seen[CPT_ABBR] = TRUE;
1666 count++;
1667 }
1668 else if (STRCMP(buf, "kind") == 0)
1669 {
1670 if (seen[CPT_KIND])
1671 return e_invalid_argument;
1672 new_cia_flags = new_cia_flags * 10 + CPT_KIND;
1673 seen[CPT_KIND] = TRUE;
1674 count++;
1675 }
1676 else if (STRCMP(buf, "menu") == 0)
1677 {
1678 if (seen[CPT_MENU])
1679 return e_invalid_argument;
1680 new_cia_flags = new_cia_flags * 10 + CPT_MENU;
1681 seen[CPT_MENU] = TRUE;
1682 count++;
1683 }
1684 else
1685 return e_invalid_argument;
1686 }
1687 if (new_cia_flags == 0 || count != 3)
1688 return e_invalid_argument;
1689
1690 cia_flags = new_cia_flags;
1691 return NULL;
1692}
1693
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001694#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1695/*
1696 * The 'completepopup' option is changed.
1697 */
1698 char *
1699did_set_completepopup(optset_T *args UNUSED)
1700{
1701 if (parse_completepopup(NULL) == FAIL)
1702 return e_invalid_argument;
1703
1704 popup_close_info();
1705 return NULL;
1706}
1707#endif
1708
1709#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1710/*
1711 * The 'completeslash' option is changed.
1712 */
1713 char *
1714did_set_completeslash(optset_T *args UNUSED)
1715{
1716 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1717 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
1718 return e_invalid_argument;
1719
1720 return NULL;
1721}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001722
1723 int
1724expand_set_completeslash(optexpand_T *args, int *numMatches, char_u ***matches)
1725{
1726 return expand_set_opt_string(
1727 args,
1728 p_csl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001729 ARRAY_LENGTH(p_csl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001730 numMatches,
1731 matches);
1732}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001733#endif
1734
1735#if defined(FEAT_CONCEAL) || defined(PROTO)
1736/*
1737 * The 'concealcursor' option is changed.
1738 */
1739 char *
1740did_set_concealcursor(optset_T *args)
1741{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001742 char_u **varp = (char_u **)args->os_varp;
1743
Christian Brabandtb39b2402023-11-29 11:34:05 +01001744 return did_set_option_listflag(*varp, (char_u *)COCU_ALL, args->os_errbuf,
1745 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001746}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001747
1748 int
1749expand_set_concealcursor(optexpand_T *args, int *numMatches, char_u ***matches)
1750{
1751 return expand_set_opt_listflag(args, (char_u*)COCU_ALL, numMatches, matches);
1752}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001753#endif
1754
1755/*
1756 * The 'cpoptions' option is changed.
1757 */
1758 char *
1759did_set_cpoptions(optset_T *args)
1760{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001761 char_u **varp = (char_u **)args->os_varp;
1762
Christian Brabandtb39b2402023-11-29 11:34:05 +01001763 return did_set_option_listflag(*varp, (char_u *)CPO_ALL, args->os_errbuf,
1764 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001765}
1766
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001767 int
1768expand_set_cpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
1769{
1770 return expand_set_opt_listflag(args, (char_u*)CPO_ALL, numMatches, matches);
1771}
1772
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001773#if defined(FEAT_CRYPT) || defined(PROTO)
1774/*
1775 * The 'cryptkey' option is changed.
1776 */
1777 char *
1778did_set_cryptkey(optset_T *args)
1779{
1780 // Make sure the ":set" command doesn't show the new value in the
1781 // history.
1782 remove_key_from_history();
1783
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02001784 if (args->os_op != OP_NONE)
1785 // Don't allow set+=/-=/^= as they can allow for substring guessing
1786 return e_invalid_argument;
1787
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001788 if (STRCMP(curbuf->b_p_key, args->os_oldval.string) != 0)
1789 {
1790 // Need to update the swapfile.
1791 ml_set_crypt_key(curbuf, args->os_oldval.string,
1792 *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
1793 changed_internal();
1794 }
Christian Brabandt19e6c4f2023-06-27 18:57:10 +01001795# ifdef FEAT_SODIUM
1796 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
1797 crypt_sodium_lock_key(args->os_newval.string);
1798# endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001799
1800 return NULL;
1801}
1802
1803/*
1804 * The 'cryptmethod' option is changed.
1805 */
1806 char *
1807did_set_cryptmethod(optset_T *args)
1808{
1809 char_u *p;
1810 char_u *s;
1811
1812 if (args->os_flags & OPT_LOCAL)
1813 p = curbuf->b_p_cm;
1814 else
1815 p = p_cm;
1816 if (check_opt_strings(p, p_cm_values, TRUE) != OK)
1817 return e_invalid_argument;
1818 else if (crypt_self_test() == FAIL)
1819 return e_invalid_argument;
1820
1821 // When setting the global value to empty, make it "zip".
1822 if (*p_cm == NUL)
1823 {
1824 free_string_option(p_cm);
1825 p_cm = vim_strsave((char_u *)"zip");
1826 }
1827 // When using ":set cm=name" the local value is going to be empty.
1828 // Do that here, otherwise the crypt functions will still use the
1829 // local value.
1830 if ((args->os_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1831 {
1832 free_string_option(curbuf->b_p_cm);
1833 curbuf->b_p_cm = empty_option;
1834 }
1835
1836 // Need to update the swapfile when the effective method changed.
1837 // Set "s" to the effective old value, "p" to the effective new
1838 // method and compare.
1839 if ((args->os_flags & OPT_LOCAL) && *args->os_oldval.string == NUL)
1840 s = p_cm; // was previously using the global value
1841 else
1842 s = args->os_oldval.string;
1843 if (*curbuf->b_p_cm == NUL)
1844 p = p_cm; // is now using the global value
1845 else
1846 p = curbuf->b_p_cm;
1847 if (STRCMP(s, p) != 0)
1848 ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1849
1850 // If the global value changes need to update the swapfile for all
1851 // buffers using that value.
1852 if ((args->os_flags & OPT_GLOBAL)
1853 && STRCMP(p_cm, args->os_oldval.string) != 0)
1854 {
1855 buf_T *buf;
1856
1857 FOR_ALL_BUFFERS(buf)
1858 if (buf != curbuf && *buf->b_p_cm == NUL)
1859 ml_set_crypt_key(buf, buf->b_p_key, args->os_oldval.string);
1860 }
1861 return NULL;
1862}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001863
1864 int
1865expand_set_cryptmethod(optexpand_T *args, int *numMatches, char_u ***matches)
1866{
1867 return expand_set_opt_string(
1868 args,
1869 p_cm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001870 ARRAY_LENGTH(p_cm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001871 numMatches,
1872 matches);
1873}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001874#endif
1875
1876#if (defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1877/*
1878 * The 'cscopequickfix' option is changed.
1879 */
1880 char *
1881did_set_cscopequickfix(optset_T *args UNUSED)
1882{
1883 char_u *p;
1884
1885 if (p_csqf == NULL)
1886 return NULL;
1887
1888 p = p_csqf;
1889 while (*p != NUL)
1890 {
1891 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
1892 || p[1] == NUL
1893 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
1894 || (p[2] != NUL && p[2] != ','))
1895 return e_invalid_argument;
1896 else if (p[2] == NUL)
1897 break;
1898 else
1899 p += 3;
1900 }
1901
1902 return NULL;
1903}
1904#endif
1905
1906#if defined(FEAT_SYN_HL) || defined(PROTO)
1907/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001908 * The 'cursorlineopt' option is changed.
1909 */
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001910 char *
1911did_set_cursorlineopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001912{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001913 char_u **varp = (char_u **)args->os_varp;
1914
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001915 // This could be changed to use opt_strings_flags() instead.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001916 if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001917 return e_invalid_argument;
1918
1919 return NULL;
1920}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001921
1922 int
1923expand_set_cursorlineopt(optexpand_T *args, int *numMatches, char_u ***matches)
1924{
1925 return expand_set_opt_string(
1926 args,
1927 p_culopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001928 ARRAY_LENGTH(p_culopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001929 numMatches,
1930 matches);
1931}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001932#endif
1933
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001934/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001935 * The 'debug' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001936 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001937 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001938did_set_debug(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001939{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001940 return did_set_opt_strings(p_debug, p_debug_values, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001941}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001942
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001943 int
1944expand_set_debug(optexpand_T *args, int *numMatches, char_u ***matches)
1945{
1946 return expand_set_opt_string(
1947 args,
1948 p_debug_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001949 ARRAY_LENGTH(p_debug_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001950 numMatches,
1951 matches);
1952}
1953
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001954#if defined(FEAT_DIFF) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001955/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001956 * The 'diffopt' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001957 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001958 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001959did_set_diffopt(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001960{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001961 if (diffopt_changed() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001962 return e_invalid_argument;
1963
1964 return NULL;
1965}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001966
1967 int
1968expand_set_diffopt(optexpand_T *args, int *numMatches, char_u ***matches)
1969{
1970 expand_T *xp = args->oe_xp;
1971
1972 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
1973 {
1974 // Within "algorithm:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001975 int algo_len = (int)STRLEN("algorithm:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001976 if (xp->xp_pattern - args->oe_set_arg >= algo_len &&
1977 STRNCMP(xp->xp_pattern - algo_len, "algorithm:", algo_len) == 0)
1978 {
1979 return expand_set_opt_string(
1980 args,
1981 p_dip_algorithm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001982 ARRAY_LENGTH(p_dip_algorithm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001983 numMatches,
1984 matches);
1985 }
1986 return FAIL;
1987 }
1988
1989 return expand_set_opt_string(
1990 args,
1991 p_dip_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001992 ARRAY_LENGTH(p_dip_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001993 numMatches,
1994 matches);
1995}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001996#endif
1997
1998/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001999 * The 'display' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002000 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002001 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002002did_set_display(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002003{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002004 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002005 return e_invalid_argument;
2006
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002007 (void)init_chartab();
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002008 return NULL;
2009}
2010
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002011 int
2012expand_set_display(optexpand_T *args, int *numMatches, char_u ***matches)
2013{
2014 return expand_set_opt_string(
2015 args,
2016 p_dy_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002017 ARRAY_LENGTH(p_dy_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002018 numMatches,
2019 matches);
2020}
2021
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002022/*
2023 * The 'eadirection' option is changed.
2024 */
2025 char *
2026did_set_eadirection(optset_T *args UNUSED)
2027{
2028 return did_set_opt_strings(p_ead, p_ead_values, FALSE);
2029}
2030
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002031 int
2032expand_set_eadirection(optexpand_T *args, int *numMatches, char_u ***matches)
2033{
2034 return expand_set_opt_string(
2035 args,
2036 p_ead_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002037 ARRAY_LENGTH(p_ead_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002038 numMatches,
2039 matches);
2040}
2041
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002042/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002043 * One of the 'encoding', 'fileencoding', 'termencoding' or 'makeencoding'
2044 * options is changed.
2045 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002046 char *
2047did_set_encoding(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002048{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002049 char_u **varp = (char_u **)args->os_varp;
2050 char_u **gvarp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002051 char *errmsg = NULL;
2052 char_u *p;
2053
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002054 // Get the global option to compare with, otherwise we would have to check
2055 // two values for all local options.
2056 gvarp = (char_u **)get_option_varp_scope(args->os_idx, OPT_GLOBAL);
2057
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002058 if (gvarp == &p_fenc)
2059 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002060 if (!curbuf->b_p_ma && args->os_flags != OPT_GLOBAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002061 errmsg = e_cannot_make_changes_modifiable_is_off;
2062 else if (vim_strchr(*varp, ',') != NULL)
2063 // No comma allowed in 'fileencoding'; catches confusing it
2064 // with 'fileencodings'.
2065 errmsg = e_invalid_argument;
2066 else
2067 {
2068 // May show a "+" in the title now.
2069 redraw_titles();
2070 // Add 'fileencoding' to the swap file.
2071 ml_setflags(curbuf);
2072 }
2073 }
2074 if (errmsg == NULL)
2075 {
2076 // canonize the value, so that STRCMP() can be used on it
2077 p = enc_canonize(*varp);
2078 if (p != NULL)
2079 {
2080 vim_free(*varp);
2081 *varp = p;
2082 }
2083 if (varp == &p_enc)
2084 {
2085 errmsg = mb_init();
2086 redraw_titles();
2087 }
2088 }
2089
2090#if defined(FEAT_GUI_GTK)
2091 if (errmsg == NULL && varp == &p_tenc && gui.in_use)
2092 {
2093 // GTK uses only a single encoding, and that is UTF-8.
2094 if (STRCMP(p_tenc, "utf-8") != 0)
2095 errmsg = e_cannot_be_changed_in_gtk_GUI;
2096 }
2097#endif
2098
2099 if (errmsg == NULL)
2100 {
2101#ifdef FEAT_KEYMAP
2102 // When 'keymap' is used and 'encoding' changes, reload the keymap
2103 // (with another encoding).
2104 if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
2105 (void)keymap_init();
2106#endif
2107
2108 // When 'termencoding' is not empty and 'encoding' changes or when
2109 // 'termencoding' changes, need to setup for keyboard input and
2110 // display output conversion.
2111 if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
2112 {
2113 if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
2114 || convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
2115 {
2116 semsg(_(e_cannot_convert_between_str_and_str),
2117 p_tenc, p_enc);
2118 errmsg = e_invalid_argument;
2119 }
2120 }
2121
2122#if defined(MSWIN)
K.Takatace3189d2023-02-15 19:13:43 +00002123 // $HOME, $VIM and $VIMRUNTIME may have characters in active code page.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002124 if (varp == &p_enc)
K.Takatace3189d2023-02-15 19:13:43 +00002125 {
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002126 init_homedir();
K.Takatace3189d2023-02-15 19:13:43 +00002127 init_vimdir();
2128 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002129#endif
2130 }
2131
2132 return errmsg;
2133}
2134
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002135 int
2136expand_set_encoding(optexpand_T *args, int *numMatches, char_u ***matches)
2137{
2138 return expand_set_opt_generic(
2139 args,
2140 get_encoding_name,
2141 numMatches,
2142 matches);
2143}
2144
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002145/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002146 * The 'eventignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002147 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002148 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002149did_set_eventignore(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002150{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002151 if (check_ei() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002152 return e_invalid_argument;
2153 return NULL;
2154}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002155
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002156 static char_u *
2157get_eventignore_name(expand_T *xp, int idx)
2158{
2159 // 'eventignore' allows special keyword "all" in addition to
2160 // all event names.
2161 if (idx == 0)
2162 return (char_u *)"all";
2163 return get_event_name_no_group(xp, idx - 1);
2164}
2165
2166 int
2167expand_set_eventignore(optexpand_T *args, int *numMatches, char_u ***matches)
2168{
2169 return expand_set_opt_generic(
2170 args,
2171 get_eventignore_name,
2172 numMatches,
2173 matches);
2174}
2175
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002176/*
2177 * The 'fileformat' option is changed.
2178 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002179 char *
2180did_set_fileformat(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002181{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002182 char_u **varp = (char_u **)args->os_varp;
2183
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002184 if (!curbuf->b_p_ma && !(args->os_flags & OPT_GLOBAL))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002185 return e_cannot_make_changes_modifiable_is_off;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002186 else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002187 return e_invalid_argument;
2188
2189 // may also change 'textmode'
2190 if (get_fileformat(curbuf) == EOL_DOS)
2191 curbuf->b_p_tx = TRUE;
2192 else
2193 curbuf->b_p_tx = FALSE;
2194 redraw_titles();
2195 // update flag in swap file
2196 ml_setflags(curbuf);
2197 // Redraw needed when switching to/from "mac": a CR in the text
2198 // will be displayed differently.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002199 if (get_fileformat(curbuf) == EOL_MAC || *args->os_oldval.string == 'm')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002200 redraw_curbuf_later(UPD_NOT_VALID);
2201
2202 return NULL;
2203}
2204
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002205 int
2206expand_set_fileformat(optexpand_T *args, int *numMatches, char_u ***matches)
2207{
2208 return expand_set_opt_string(
2209 args,
2210 p_ff_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002211 ARRAY_LENGTH(p_ff_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002212 numMatches,
2213 matches);
2214}
2215
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002216/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002217 * Function given to ExpandGeneric() to obtain the possible arguments of the
2218 * fileformat options.
2219 */
2220 char_u *
2221get_fileformat_name(expand_T *xp UNUSED, int idx)
2222{
2223 if (idx >= (int)ARRAY_LENGTH(p_ff_values))
2224 return NULL;
2225
2226 return (char_u*)p_ff_values[idx];
2227}
2228
2229/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002230 * The 'fileformats' option is changed.
2231 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002232 char *
2233did_set_fileformats(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002234{
2235 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
2236 return e_invalid_argument;
2237
2238 // also change 'textauto'
2239 if (*p_ffs == NUL)
2240 p_ta = FALSE;
2241 else
2242 p_ta = TRUE;
2243
2244 return NULL;
2245}
2246
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002247/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002248 * The 'filetype' or the 'syntax' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002249 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002250 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002251did_set_filetype_or_syntax(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002252{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002253 char_u **varp = (char_u **)args->os_varp;
2254
2255 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002256 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002257
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002258 args->os_value_changed = STRCMP(args->os_oldval.string, *varp) != 0;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002259
2260 // Since we check the value, there is no need to set P_INSECURE,
2261 // even when the value comes from a modeline.
2262 args->os_value_checked = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002263
2264 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002265}
2266
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002267#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002268/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002269 * The 'foldclose' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002270 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002271 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002272did_set_foldclose(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002273{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002274 return did_set_opt_strings(p_fcl, p_fcl_values, TRUE);
2275}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002276
2277 int
2278expand_set_foldclose(optexpand_T *args, int *numMatches, char_u ***matches)
2279{
2280 return expand_set_opt_string(
2281 args,
2282 p_fcl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002283 ARRAY_LENGTH(p_fcl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002284 numMatches,
2285 matches);
2286}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002287#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002288
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002289#if (defined(FEAT_EVAL) && defined(FEAT_FOLDING)) || defined(PROTO)
2290/*
2291 * The 'foldexpr' option is changed.
2292 */
2293 char *
2294did_set_foldexpr(optset_T *args)
2295{
2296 (void)did_set_optexpr(args);
2297 if (foldmethodIsExpr(curwin))
2298 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002299 return NULL;
2300}
2301#endif
2302
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002303#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002304/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002305 * The 'foldignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002306 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002307 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002308did_set_foldignore(optset_T *args UNUSED)
2309{
2310 if (foldmethodIsIndent(curwin))
2311 foldUpdateAll(curwin);
2312 return NULL;
2313}
2314
2315/*
2316 * The 'foldmarker' option is changed.
2317 */
2318 char *
2319did_set_foldmarker(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002320{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002321 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002322 char_u *p;
2323
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002324 p = vim_strchr(*varp, ',');
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002325 if (p == NULL)
2326 return e_comma_required;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002327 else if (p == *varp || p[1] == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002328 return e_invalid_argument;
2329 else if (foldmethodIsMarker(curwin))
2330 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002331
2332 return NULL;
2333}
2334
2335/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002336 * The 'foldmethod' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002337 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002338 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002339did_set_foldmethod(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002340{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002341 char_u **varp = (char_u **)args->os_varp;
2342
Milly142cad12024-10-22 22:11:51 +02002343 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK || **varp == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002344 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002345
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002346 foldUpdateAll(curwin);
2347 if (foldmethodIsDiff(curwin))
2348 newFoldLevel();
2349 return NULL;
2350}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002351
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002352 int
2353expand_set_foldmethod(optexpand_T *args, int *numMatches, char_u ***matches)
2354{
2355 return expand_set_opt_string(
2356 args,
2357 p_fdm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002358 ARRAY_LENGTH(p_fdm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002359 numMatches,
2360 matches);
2361}
2362
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002363/*
2364 * The 'foldopen' option is changed.
2365 */
2366 char *
2367did_set_foldopen(optset_T *args UNUSED)
2368{
2369 return did_set_opt_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
2370}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002371
2372 int
2373expand_set_foldopen(optexpand_T *args, int *numMatches, char_u ***matches)
2374{
2375 return expand_set_opt_string(
2376 args,
2377 p_fdo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002378 ARRAY_LENGTH(p_fdo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002379 numMatches,
2380 matches);
2381}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002382#endif
2383
2384/*
2385 * The 'formatoptions' option is changed.
2386 */
2387 char *
2388did_set_formatoptions(optset_T *args)
2389{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002390 char_u **varp = (char_u **)args->os_varp;
2391
Christian Brabandtb39b2402023-11-29 11:34:05 +01002392 return did_set_option_listflag(*varp, (char_u *)FO_ALL, args->os_errbuf,
2393 args->os_errbuflen);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002394}
2395
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002396 int
2397expand_set_formatoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2398{
2399 return expand_set_opt_listflag(args, (char_u*)FO_ALL, numMatches, matches);
2400}
2401
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002402#if defined(CURSOR_SHAPE) || defined(PROTO)
2403/*
2404 * The 'guicursor' option is changed.
2405 */
2406 char *
2407did_set_guicursor(optset_T *args UNUSED)
2408{
2409 return parse_shape_opt(SHAPE_CURSOR);
2410}
2411#endif
2412
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002413#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002414/*
2415 * The 'guifont' option is changed.
2416 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002417 char *
2418did_set_guifont(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002419{
2420 char_u *p;
2421 char *errmsg = NULL;
2422
2423 if (gui.in_use)
2424 {
2425 p = p_guifont;
2426# if defined(FEAT_GUI_GTK)
2427 // Put up a font dialog and let the user select a new value.
2428 // If this is cancelled go back to the old value but don't
2429 // give an error message.
2430 if (STRCMP(p, "*") == 0)
2431 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002432 p = gui_mch_font_dialog(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002433 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002434 p_guifont = (p != NULL) ? p : vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002435 }
2436# endif
2437 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
2438 {
2439# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
2440 if (STRCMP(p_guifont, "*") == 0)
2441 {
2442 // Dialog was cancelled: Keep the old value without giving
2443 // an error message.
2444 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002445 p_guifont = vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002446 }
2447 else
2448# endif
2449 errmsg = e_invalid_fonts;
2450 }
2451 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002452
2453 return errmsg;
2454}
2455
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002456/*
2457 * Expand the 'guifont' option. Only when GUI is being used. Each platform has
2458 * specific behaviors.
2459 */
2460 int
2461expand_set_guifont(optexpand_T *args, int *numMatches, char_u ***matches)
2462{
2463 if (!gui.in_use)
2464 return FAIL;
2465
2466# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
2467 char_u **varp = (char_u **)args->oe_varp;
2468 int wide = (varp == &p_guifontwide);
2469
2470 return expand_set_opt_callback(
2471 args, gui_mch_expand_font, &wide, numMatches, matches);
2472# else
2473 return FAIL;
2474# endif
2475}
2476
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002477# if defined(FEAT_XFONTSET) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002478/*
2479 * The 'guifontset' option is changed.
2480 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002481 char *
2482did_set_guifontset(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002483{
2484 char *errmsg = NULL;
2485
2486 if (STRCMP(p_guifontset, "*") == 0)
2487 errmsg = e_cant_select_fontset;
2488 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
2489 errmsg = e_invalid_fontset;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002490
2491 return errmsg;
2492}
2493# endif
2494
2495/*
2496 * The 'guifontwide' option is changed.
2497 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002498 char *
2499did_set_guifontwide(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002500{
2501 char *errmsg = NULL;
2502
2503 if (STRCMP(p_guifontwide, "*") == 0)
2504 errmsg = e_cant_select_wide_font;
2505 else if (gui_get_wide_font() == FAIL)
2506 errmsg = e_invalid_wide_font;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002507
2508 return errmsg;
2509}
2510#endif
2511
Erik S. V. Jansson8b1e7492024-02-24 14:26:52 +01002512#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002513/*
2514 * The 'guiligatures' option is changed.
2515 */
2516 char *
2517did_set_guiligatures(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002518{
2519 gui_set_ligatures();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002520 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002521}
2522#endif
2523
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002524#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002525/*
2526 * The 'guioptions' option is changed.
2527 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002528 char *
2529did_set_guioptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002530{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002531 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002532 char *errmsg;
2533
Christian Brabandtb39b2402023-11-29 11:34:05 +01002534 errmsg = did_set_option_listflag(*varp, (char_u *)GO_ALL, args->os_errbuf,
2535 args->os_errbuflen);
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002536 if (errmsg != NULL)
2537 return errmsg;
2538
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002539 gui_init_which_components(args->os_oldval.string);
2540 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002541}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002542
2543 int
2544expand_set_guioptions(optexpand_T *args, int *numMatches, char_u ***matches)
2545{
2546 return expand_set_opt_listflag(args, (char_u*)GO_ALL, numMatches, matches);
2547}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002548#endif
2549
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002550#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002551/*
2552 * The 'guitablabel' option is changed.
2553 */
2554 char *
2555did_set_guitablabel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002556{
2557 redraw_tabline = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002558 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002559}
2560#endif
2561
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002562/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002563 * The 'helpfile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002564 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002565 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002566did_set_helpfile(optset_T *args UNUSED)
2567{
2568 // May compute new values for $VIM and $VIMRUNTIME
2569 if (didset_vim)
2570 vim_unsetenv_ext((char_u *)"VIM");
2571 if (didset_vimruntime)
2572 vim_unsetenv_ext((char_u *)"VIMRUNTIME");
2573 return NULL;
2574}
2575
2576#if defined(FEAT_MULTI_LANG) || defined(PROTO)
2577/*
2578 * The 'helplang' option is changed.
2579 */
2580 char *
2581did_set_helplang(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002582{
2583 char *errmsg = NULL;
2584
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002585 // Check for "", "ab", "ab,cd", etc.
2586 for (char_u *s = p_hlg; *s != NUL; s += 3)
2587 {
2588 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
2589 {
2590 errmsg = e_invalid_argument;
2591 break;
2592 }
2593 if (s[2] == NUL)
2594 break;
2595 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002596
2597 return errmsg;
2598}
2599#endif
2600
2601/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002602 * The 'highlight' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002603 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002604 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002605did_set_highlight(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002606{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002607 if (highlight_changed() == FAIL)
2608 return e_invalid_argument; // invalid flags
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002609
2610 return NULL;
2611}
2612
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002613/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002614 * Expand 'highlight' option.
2615 */
2616 int
2617expand_set_highlight(optexpand_T *args, int *numMatches, char_u ***matches)
2618{
2619 char_u *p;
2620 expand_T *xp = args->oe_xp;
2621 static char_u hl_flags[HLF_COUNT] = HL_FLAGS;
Christian Brabandt3f168ec2023-10-02 23:21:11 +02002622 size_t i;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002623 int count = 0;
2624
2625 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2626 {
2627 // Right after a ':', meaning we just return all highlight names.
2628 return expand_set_opt_generic(
2629 args,
2630 get_highlight_name,
2631 numMatches,
2632 matches);
2633 }
2634
2635 if (*xp->xp_pattern == NUL)
2636 {
2637 // At beginning of a comma-separated list. Return the specific list of
2638 // supported occasions.
2639 *matches = ALLOC_MULT(char_u *, HLF_COUNT + 1);
2640 if (*matches == NULL)
2641 return FAIL;
2642
2643 // We still want to return the full option if it's requested.
2644 if (args->oe_include_orig_val)
2645 {
2646 p = vim_strsave(args->oe_opt_value);
2647 if (p == NULL)
2648 {
2649 VIM_CLEAR(*matches);
2650 return FAIL;
2651 }
2652 (*matches)[count++] = p;
2653 }
2654
2655 for (i = 0; i < HLF_COUNT; i++)
2656 {
2657 p = vim_strnsave(&hl_flags[i], 1);
2658 if (p == NULL)
2659 {
2660 if (count == 0)
2661 {
2662 VIM_CLEAR(*matches);
2663 return FAIL;
2664 }
2665 else
2666 break;
2667 }
2668 (*matches)[count++] = p;
2669 }
2670
2671 if (count == 0)
2672 {
2673 VIM_CLEAR(*matches);
2674 return FAIL;
2675 }
2676 *numMatches = count;
2677 return OK;
2678 }
2679
2680 // We are after the initial character (which indicates the occasion). We
2681 // already made sure we are not matching after a ':' above, so now we want
2682 // to match against display mode modifiers.
2683 // Since the xp_pattern starts from the beginning, we need to include it in
2684 // the returned match.
2685
2686 // Note: Keep this in sync with highlight_changed()
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002687 static char_u p_hl_mode_values[] =
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002688 {':', 'b', 'i', '-', 'n', 'r', 's', 'u', 'c', '2', 'd', '=', 't'};
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002689 size_t num_hl_modes = ARRAY_LENGTH(p_hl_mode_values);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002690
2691 *matches = ALLOC_MULT(char_u *, num_hl_modes);
2692 if (*matches == NULL)
2693 return FAIL;
2694
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002695 int pattern_len = xp->xp_pattern_len;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002696
2697 for (i = 0; i < num_hl_modes; i++)
2698 {
2699 // Don't allow duplicates as these are unique flags
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002700 char_u *dup = vim_strchr(xp->xp_pattern + 1, p_hl_mode_values[i]);
2701 if (dup != NULL && (int)(dup - xp->xp_pattern) < pattern_len)
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002702 continue;
2703
2704 // ':' only works by itself, not with other flags.
2705 if (pattern_len > 1 && p_hl_mode_values[i] == ':')
2706 continue;
2707
2708 p = vim_strnsave(xp->xp_pattern, pattern_len + 1);
2709 if (p == NULL)
2710 {
2711 if (i == 0)
2712 {
2713 VIM_CLEAR(*matches);
2714 return FAIL;
2715 }
2716 else
2717 break;
2718 }
2719 p[pattern_len] = p_hl_mode_values[i];
2720 p[pattern_len + 1] = NUL;
2721 (*matches)[count++] = p;
2722 }
2723 if (count == 0)
2724 {
2725 VIM_CLEAR(*matches);
2726 return FAIL;
2727 }
2728 *numMatches = count;
2729
2730 return OK;
2731}
2732
2733/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002734 * The 'titlestring' or the 'iconstring' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002735 */
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002736 static char *
2737parse_titleiconstring(optset_T *args UNUSED, int flagval UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002738{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002739#ifdef FEAT_STL_OPT
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002740 char_u **varp = (char_u **)args->os_varp;
2741
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002742 // NULL => statusline syntax
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002743 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002744 stl_syntax |= flagval;
2745 else
2746 stl_syntax &= ~flagval;
2747#endif
2748 did_set_title();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002749
2750 return NULL;
2751}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002752
2753/*
2754 * The 'iconstring' option is changed.
2755 */
2756 char *
2757did_set_iconstring(optset_T *args)
2758{
2759 int flagval = 0;
2760
2761#ifdef FEAT_STL_OPT
2762 flagval = STL_IN_ICON;
2763#endif
2764
2765 return parse_titleiconstring(args, flagval);
2766}
2767
2768#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
2769/*
2770 * The 'imactivatekey' option is changed.
2771 */
2772 char *
2773did_set_imactivatekey(optset_T *args UNUSED)
2774{
2775 if (!im_xim_isvalid_imactivate())
2776 return e_invalid_argument;
2777 return NULL;
2778}
2779#endif
2780
2781/*
Milly5e7a6a42024-10-22 22:27:19 +02002782 * The 'iskeyword' option is changed.
2783 */
2784 char *
2785did_set_iskeyword(optset_T *args)
2786{
2787 char_u **varp = (char_u **)args->os_varp;
2788
2789 if (varp == &p_isk) // only check for global-value
2790 {
2791 if (check_isopt(*varp) == FAIL)
2792 return e_invalid_argument;
2793 }
2794 else // fallthrough for local-value
2795 return did_set_isopt(args);
2796
2797 return NULL;
2798}
2799
2800/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002801 * The 'isident' or the 'iskeyword' or the 'isprint' or the 'isfname' option is
2802 * changed.
2803 */
2804 char *
2805did_set_isopt(optset_T *args)
2806{
Milly5e7a6a42024-10-22 22:27:19 +02002807 // 'isident', 'iskeyword', 'isprint' or 'isfname' option: refill g_chartab[]
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002808 // If the new option is invalid, use old value.
2809 // 'lisp' option: refill g_chartab[] for '-' char.
2810 if (init_chartab() == FAIL)
2811 {
2812 args->os_restore_chartab = TRUE;// need to restore the chartab.
2813 return e_invalid_argument; // error in value
2814 }
2815
2816 return NULL;
2817}
2818
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002819/*
2820 * The 'jumpoptions' option is changed.
2821 */
2822 char *
Yegappan Lakshmanan1926ae42023-09-21 16:36:28 +02002823did_set_jumpoptions(optset_T *args UNUSED)
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002824{
2825 if (opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE) != OK)
2826 return e_invalid_argument;
2827
2828 return NULL;
2829}
2830
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002831 int
2832expand_set_jumpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2833{
2834 return expand_set_opt_string(
2835 args,
2836 p_jop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002837 ARRAY_LENGTH(p_jop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002838 numMatches,
2839 matches);
2840}
2841
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002842#if defined(FEAT_KEYMAP) || defined(PROTO)
2843/*
2844 * The 'keymap' option is changed.
2845 */
2846 char *
2847did_set_keymap(optset_T *args)
2848{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002849 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002850 char *errmsg = NULL;
2851
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002852 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002853 errmsg = e_invalid_argument;
2854 else
2855 {
2856 int secure_save = secure;
2857
2858 // Reset the secure flag, since the value of 'keymap' has
2859 // been checked to be safe.
2860 secure = 0;
2861
2862 // load or unload key mapping tables
2863 errmsg = keymap_init();
2864
2865 secure = secure_save;
2866
2867 // Since we check the value, there is no need to set P_INSECURE,
2868 // even when the value comes from a modeline.
2869 args->os_value_checked = TRUE;
2870 }
2871
2872 if (errmsg == NULL)
2873 {
2874 if (*curbuf->b_p_keymap != NUL)
2875 {
2876 // Installed a new keymap, switch on using it.
2877 curbuf->b_p_iminsert = B_IMODE_LMAP;
2878 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
2879 curbuf->b_p_imsearch = B_IMODE_LMAP;
2880 }
2881 else
2882 {
2883 // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
2884 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
2885 curbuf->b_p_iminsert = B_IMODE_NONE;
2886 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
2887 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
2888 }
2889 if ((args->os_flags & OPT_LOCAL) == 0)
2890 {
2891 set_iminsert_global();
2892 set_imsearch_global();
2893 }
2894 status_redraw_curbuf();
2895 }
2896
2897 return errmsg;
2898}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002899#endif
2900
2901/*
2902 * The 'keymodel' option is changed.
2903 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002904 char *
2905did_set_keymodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002906{
2907 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
2908 return e_invalid_argument;
2909
2910 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
2911 km_startsel = (vim_strchr(p_km, 'a') != NULL);
2912 return NULL;
2913}
2914
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002915 int
2916expand_set_keymodel(optexpand_T *args, int *numMatches, char_u ***matches)
2917{
2918 return expand_set_opt_string(
2919 args,
2920 p_km_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002921 ARRAY_LENGTH(p_km_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002922 numMatches,
2923 matches);
2924}
2925
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002926/*
2927 * The 'keyprotocol' option is changed.
2928 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002929 char *
2930did_set_keyprotocol(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002931{
Gregory Anders3695d0e2023-09-29 20:17:20 +02002932 char_u *term = T_NAME;
2933 keyprot_T kpc = match_keyprotocol(term);
2934 if (kpc == KEYPROTOCOL_FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002935 return e_invalid_argument;
2936
Gregory Anders3695d0e2023-09-29 20:17:20 +02002937 apply_keyprotocol(term, kpc);
2938
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002939 return NULL;
2940}
2941
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002942 int
2943expand_set_keyprotocol(optexpand_T *args, int *numMatches, char_u ***matches)
2944{
2945 expand_T *xp = args->oe_xp;
2946 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2947 {
2948 // 'keyprotocol' only has well-defined terms for completion for the
2949 // protocol part after the colon.
2950 return expand_set_opt_string(
2951 args,
2952 p_kpc_protocol_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002953 ARRAY_LENGTH(p_kpc_protocol_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002954 numMatches,
2955 matches);
2956 }
2957 // Use expand_set_opt_string instead of returning FAIL so that we can
2958 // include the original value if args->oe_include_orig_val is set.
2959 static char *(empty[]) = {NULL};
2960 return expand_set_opt_string(args, empty, 0, numMatches, matches);
2961}
2962
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002963/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002964 * The 'lispoptions' option is changed.
2965 */
2966 char *
2967did_set_lispoptions(optset_T *args)
2968{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002969 char_u **varp = (char_u **)args->os_varp;
2970
2971 if (**varp != NUL
2972 && STRCMP(*varp, "expr:0") != 0
2973 && STRCMP(*varp, "expr:1") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002974 return e_invalid_argument;
2975
2976 return NULL;
2977}
2978
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002979 int
2980expand_set_lispoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2981{
2982 static char *(p_lop_values[]) = {"expr:0", "expr:1", NULL};
2983 return expand_set_opt_string(
2984 args,
2985 p_lop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002986 ARRAY_LENGTH(p_lop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002987 numMatches,
2988 matches);
2989}
2990
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002991/*
2992 * The 'matchpairs' option is changed.
2993 */
2994 char *
2995did_set_matchpairs(optset_T *args)
2996{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002997 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002998 char_u *p;
2999
3000 if (has_mbyte)
3001 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003002 for (p = *varp; *p != NUL; ++p)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003003 {
3004 int x2 = -1;
3005 int x3 = -1;
3006
3007 p += mb_ptr2len(p);
3008 if (*p != NUL)
3009 x2 = *p++;
3010 if (*p != NUL)
3011 {
3012 x3 = mb_ptr2char(p);
3013 p += mb_ptr2len(p);
3014 }
3015 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
3016 return e_invalid_argument;
3017 if (*p == NUL)
3018 break;
3019 }
3020 }
3021 else
3022 {
3023 // Check for "x:y,x:y"
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003024 for (p = *varp; *p != NUL; p += 4)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003025 {
3026 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
3027 return e_invalid_argument;
3028 if (p[3] == NUL)
3029 break;
3030 }
3031 }
3032
3033 return NULL;
3034}
3035
3036#if defined(FEAT_SPELL) || defined(PROTO)
3037/*
3038 * The 'mkspellmem' option is changed.
3039 */
3040 char *
3041did_set_mkspellmem(optset_T *args UNUSED)
3042{
3043 if (spell_check_msm() != OK)
3044 return e_invalid_argument;
3045
3046 return NULL;
3047}
3048#endif
3049
3050/*
3051 * The 'mouse' option is changed.
3052 */
3053 char *
3054did_set_mouse(optset_T *args)
3055{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003056 char_u **varp = (char_u **)args->os_varp;
3057
Christian Brabandtb39b2402023-11-29 11:34:05 +01003058 return did_set_option_listflag(*varp, (char_u *)MOUSE_ALL, args->os_errbuf,
3059 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003060}
3061
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003062 int
3063expand_set_mouse(optexpand_T *args, int *numMatches, char_u ***matches)
3064{
3065 return expand_set_opt_listflag(args, (char_u*)MOUSE_ALL, numMatches, matches);
3066}
3067
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003068/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003069 * The 'mousemodel' option is changed.
3070 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003071 char *
3072did_set_mousemodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003073{
3074 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
3075 return e_invalid_argument;
3076#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
Ken Takata18d0d292023-12-19 20:12:29 +01003077 else if (*p_mousem != *args->os_oldval.string)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003078 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
3079 // to create or delete the popup menus.
3080 gui_motif_update_mousemodel(root_menu);
3081#endif
3082
3083 return NULL;
3084}
3085
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003086 int
3087expand_set_mousemodel(optexpand_T *args, int *numMatches, char_u ***matches)
3088{
3089 return expand_set_opt_string(
3090 args,
3091 p_mousem_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003092 ARRAY_LENGTH(p_mousem_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003093 numMatches,
3094 matches);
3095}
3096
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003097#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3098 char *
3099did_set_mouseshape(optset_T *args UNUSED)
3100{
3101 char *errmsg = NULL;
3102
3103 errmsg = parse_shape_opt(SHAPE_MOUSE);
3104 update_mouseshape(-1);
3105
3106 return errmsg;
3107}
3108#endif
3109
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003110/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003111 * The 'nrformats' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003112 */
3113 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003114did_set_nrformats(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003115{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003116 char_u **varp = (char_u **)args->os_varp;
3117
3118 return did_set_opt_strings(*varp, p_nf_values, TRUE);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003119}
3120
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003121 int
3122expand_set_nrformats(optexpand_T *args, int *numMatches, char_u ***matches)
3123{
3124 return expand_set_opt_string(
3125 args,
3126 p_nf_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003127 ARRAY_LENGTH(p_nf_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003128 numMatches,
3129 matches);
3130}
3131
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003132#if defined(FEAT_EVAL) || defined(PROTO)
3133/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003134 * One of the '*expr' options is changed: 'balloonexpr', 'diffexpr',
3135 * 'foldexpr', 'foldtext', 'formatexpr', 'includeexpr', 'indentexpr',
3136 * 'patchexpr', 'printexpr' and 'charconvert'.
3137 *
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003138 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003139 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003140did_set_optexpr(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003141{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003142 char_u **varp = (char_u **)args->os_varp;
3143
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003144 // If the option value starts with <SID> or s:, then replace that with
3145 // the script identifier.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003146 char_u *name = get_scriptlocal_funcname(*varp);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003147 if (name != NULL)
3148 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003149 free_string_option(*varp);
3150 *varp = name;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003151 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003152
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003153 return NULL;
3154}
3155#endif
3156
3157/*
3158 * The 'pastetoggle' option is changed.
3159 */
3160 char *
3161did_set_pastetoggle(optset_T *args UNUSED)
3162{
3163 char_u *p;
3164
3165 // translate key codes like in a mapping
3166 if (*p_pt)
3167 {
zeertzjq7e0bae02023-08-11 23:15:38 +02003168 (void)replace_termcodes(p_pt, &p, 0,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003169 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
3170 if (p != NULL)
3171 {
3172 free_string_option(p_pt);
3173 p_pt = p;
3174 }
3175 }
3176
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003177 return NULL;
3178}
3179
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003180#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3181/*
3182 * The 'previewpopup' option is changed.
3183 */
3184 char *
3185did_set_previewpopup(optset_T *args UNUSED)
3186{
3187 if (parse_previewpopup(NULL) == FAIL)
3188 return e_invalid_argument;
3189
3190 return NULL;
3191}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003192
3193 int
3194expand_set_popupoption(optexpand_T *args, int *numMatches, char_u ***matches)
3195{
3196 expand_T *xp = args->oe_xp;
3197
3198 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3199 {
3200 // Within "highlight:"/"border:"/"align:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003201 int border_len = (int)STRLEN("border:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003202 if (xp->xp_pattern - args->oe_set_arg >= border_len &&
3203 STRNCMP(xp->xp_pattern - border_len, "border:", border_len) == 0)
3204 {
3205 return expand_set_opt_string(
3206 args,
3207 p_popup_option_border_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003208 ARRAY_LENGTH(p_popup_option_border_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003209 numMatches,
3210 matches);
3211 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003212 int align_len = (int)STRLEN("align:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003213 if (xp->xp_pattern - args->oe_set_arg >= align_len &&
3214 STRNCMP(xp->xp_pattern - align_len, "align:", align_len) == 0)
3215 {
3216 return expand_set_opt_string(
3217 args,
3218 p_popup_option_align_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003219 ARRAY_LENGTH(p_popup_option_align_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003220 numMatches,
3221 matches);
3222 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003223 int highlight_len = (int)STRLEN("highlight:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003224 if (xp->xp_pattern - args->oe_set_arg >= highlight_len &&
3225 STRNCMP(xp->xp_pattern - highlight_len, "highlight:", highlight_len) == 0)
3226 {
3227 // Return the list of all highlight names
3228 return expand_set_opt_generic(
3229 args,
3230 get_highlight_name,
3231 numMatches,
3232 matches);
3233 }
3234 return FAIL;
3235 }
3236
3237 return expand_set_opt_string(
3238 args,
3239 p_popup_option_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003240 ARRAY_LENGTH(p_popup_option_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003241 numMatches,
3242 matches);
3243}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003244#endif
3245
3246#if defined(FEAT_POSTSCRIPT) || defined(PROTO)
3247/*
3248 * The 'printencoding' option is changed.
3249 */
3250 char *
3251did_set_printencoding(optset_T *args UNUSED)
3252{
3253 char_u *s, *p;
3254
3255 // Canonize 'printencoding' if VIM standard one
3256 p = enc_canonize(p_penc);
3257 if (p != NULL)
3258 {
3259 vim_free(p_penc);
3260 p_penc = p;
3261 }
3262 else
3263 {
3264 // Ensure lower case and '-' for '_'
3265 for (s = p_penc; *s != NUL; s++)
3266 {
3267 if (*s == '_')
3268 *s = '-';
3269 else
3270 *s = TOLOWER_ASC(*s);
3271 }
3272 }
3273
3274 return NULL;
3275}
3276#endif
3277
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003278#if defined(FEAT_PRINTER) || defined(PROTO)
3279
3280 static char_u *
3281get_printoptions_names(expand_T *xp UNUSED, int idx)
3282{
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003283 if (idx >= (int)ARRAY_LENGTH(printer_opts))
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003284 return NULL;
3285 return (char_u*)printer_opts[idx].name;
3286}
3287
3288/*
3289 * Expand 'printoptions' option
3290 */
3291 int
3292expand_set_printoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3293{
3294 return expand_set_opt_generic(
3295 args,
3296 get_printoptions_names,
3297 numMatches,
3298 matches);
3299}
3300#endif
3301
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003302#if defined(FEAT_STL_OPT) || defined(PROTO)
3303/*
3304 * The 'statusline' or the 'tabline' or the 'rulerformat' option is changed.
3305 * "rulerformat" is TRUE if the 'rulerformat' option is changed.
3306 */
3307 static char *
3308parse_statustabline_rulerformat(optset_T *args, int rulerformat)
3309{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003310 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003311 char_u *s;
3312 char *errmsg = NULL;
3313 int wid;
3314
3315 if (rulerformat) // reset ru_wid first
3316 ru_wid = 0;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003317 s = *varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003318 if (rulerformat && *s == '%')
3319 {
3320 // set ru_wid if 'ruf' starts with "%99("
3321 if (*++s == '-') // ignore a '-'
3322 s++;
3323 wid = getdigits(&s);
3324 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
3325 ru_wid = wid;
3326 else
3327 errmsg = check_stl_option(p_ruf);
3328 }
3329 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
3330 else if (rulerformat || s[0] != '%' || s[1] != '!')
3331 errmsg = check_stl_option(s);
3332 if (rulerformat && errmsg == NULL)
3333 comp_col();
3334
3335 return errmsg;
3336}
3337#endif
3338
3339#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
3340/*
3341 * The 'renderoptions' option is changed.
3342 */
3343 char *
3344did_set_renderoptions(optset_T *args UNUSED)
3345{
3346 if (!gui_mch_set_rendering_options(p_rop))
3347 return e_invalid_argument;
3348
3349 return NULL;
3350}
3351#endif
3352
3353#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3354/*
3355 * The 'rightleftcmd' option is changed.
3356 */
3357 char *
3358did_set_rightleftcmd(optset_T *args)
3359{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003360 char_u **varp = (char_u **)args->os_varp;
3361
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003362 // Currently only "search" is a supported value.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003363 if (**varp != NUL && STRCMP(*varp, "search") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003364 return e_invalid_argument;
3365
3366 return NULL;
3367}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003368
3369 int
3370expand_set_rightleftcmd(optexpand_T *args, int *numMatches, char_u ***matches)
3371{
3372 static char *(p_rlc_values[]) = {"search", NULL};
3373 return expand_set_opt_string(
3374 args,
3375 p_rlc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003376 ARRAY_LENGTH(p_rlc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003377 numMatches,
3378 matches);
3379}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003380#endif
3381
3382#if defined(FEAT_STL_OPT) || defined(PROTO)
3383/*
3384 * The 'rulerformat' option is changed.
3385 */
3386 char *
3387did_set_rulerformat(optset_T *args)
3388{
3389 return parse_statustabline_rulerformat(args, TRUE);
3390}
3391#endif
3392
3393/*
3394 * The 'scrollopt' option is changed.
3395 */
3396 char *
3397did_set_scrollopt(optset_T *args UNUSED)
3398{
3399 return did_set_opt_strings(p_sbo, p_scbopt_values, TRUE);
3400}
3401
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003402 int
3403expand_set_scrollopt(optexpand_T *args, int *numMatches, char_u ***matches)
3404{
3405 return expand_set_opt_string(
3406 args,
3407 p_scbopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003408 ARRAY_LENGTH(p_scbopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003409 numMatches,
3410 matches);
3411}
3412
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003413/*
3414 * The 'selection' option is changed.
3415 */
3416 char *
3417did_set_selection(optset_T *args UNUSED)
3418{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003419 if (*p_sel == NUL || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003420 return e_invalid_argument;
3421
3422 return NULL;
3423}
3424
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003425 int
3426expand_set_selection(optexpand_T *args, int *numMatches, char_u ***matches)
3427{
3428 return expand_set_opt_string(
3429 args,
3430 p_sel_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003431 ARRAY_LENGTH(p_sel_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003432 numMatches,
3433 matches);
3434}
3435
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003436/*
3437 * The 'selectmode' option is changed.
3438 */
3439 char *
3440did_set_selectmode(optset_T *args UNUSED)
3441{
3442 return did_set_opt_strings(p_slm, p_slm_values, TRUE);
3443}
3444
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003445 int
3446expand_set_selectmode(optexpand_T *args, int *numMatches, char_u ***matches)
3447{
3448 return expand_set_opt_string(
3449 args,
3450 p_slm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003451 ARRAY_LENGTH(p_slm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003452 numMatches,
3453 matches);
3454}
3455
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003456#if defined(FEAT_SESSION) || defined(PROTO)
3457/*
3458 * The 'sessionoptions' option is changed.
3459 */
3460 char *
3461did_set_sessionoptions(optset_T *args)
3462{
3463 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
3464 return e_invalid_argument;
3465 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
3466 {
3467 // Don't allow both "sesdir" and "curdir".
3468 (void)opt_strings_flags(args->os_oldval.string, p_ssop_values,
3469 &ssop_flags, TRUE);
3470 return e_invalid_argument;
3471 }
3472
3473 return NULL;
3474}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003475
3476 int
3477expand_set_sessionoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3478{
3479 return expand_set_opt_string(
3480 args,
3481 p_ssop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003482 ARRAY_LENGTH(p_ssop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003483 numMatches,
3484 matches);
3485}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003486#endif
3487
3488/*
3489 * The 'shortmess' option is changed.
3490 */
3491 char *
3492did_set_shortmess(optset_T *args)
3493{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003494 char_u **varp = (char_u **)args->os_varp;
3495
Christian Brabandtb39b2402023-11-29 11:34:05 +01003496 return did_set_option_listflag(*varp, (char_u *)SHM_ALL, args->os_errbuf,
3497 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003498}
3499
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003500 int
3501expand_set_shortmess(optexpand_T *args, int *numMatches, char_u ***matches)
3502{
3503 return expand_set_opt_listflag(args, (char_u*)SHM_ALL, numMatches, matches);
3504}
3505
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003506#if defined(FEAT_LINEBREAK) || defined(PROTO)
3507/*
3508 * The 'showbreak' option is changed.
3509 */
3510 char *
3511did_set_showbreak(optset_T *args)
3512{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003513 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003514 char_u *s;
3515
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003516 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003517 {
3518 if (ptr2cells(s) != 1)
3519 return e_showbreak_contains_unprintable_or_wide_character;
3520 MB_PTR_ADV(s);
3521 }
3522
3523 return NULL;
3524}
3525#endif
3526
3527/*
3528 * The 'showcmdloc' option is changed.
3529 */
3530 char *
3531did_set_showcmdloc(optset_T *args UNUSED)
3532{
zeertzjqc27fcf42024-03-01 23:01:43 +01003533 char *errmsg = did_set_opt_strings(p_sloc, p_sloc_values, FALSE);
3534
3535 if (errmsg == NULL)
3536 comp_col();
3537
3538 return errmsg;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003539}
3540
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003541 int
3542expand_set_showcmdloc(optexpand_T *args, int *numMatches, char_u ***matches)
3543{
3544 return expand_set_opt_string(
3545 args,
3546 p_sloc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003547 ARRAY_LENGTH(p_sloc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003548 numMatches,
3549 matches);
3550}
3551
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003552#if defined(FEAT_SIGNS) || defined(PROTO)
3553/*
3554 * The 'signcolumn' option is changed.
3555 */
3556 char *
3557did_set_signcolumn(optset_T *args)
3558{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003559 char_u **varp = (char_u **)args->os_varp;
3560
3561 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003562 return e_invalid_argument;
3563 // When changing the 'signcolumn' to or from 'number', recompute the
3564 // width of the number column if 'number' or 'relativenumber' is set.
3565 if (((*args->os_oldval.string == 'n' && args->os_oldval.string[1] == 'u')
3566 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
3567 && (curwin->w_p_nu || curwin->w_p_rnu))
3568 curwin->w_nrwidth_line_count = 0;
3569
3570 return NULL;
3571}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003572
3573 int
3574expand_set_signcolumn(optexpand_T *args, int *numMatches, char_u ***matches)
3575{
3576 return expand_set_opt_string(
3577 args,
3578 p_scl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003579 ARRAY_LENGTH(p_scl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003580 numMatches,
3581 matches);
3582}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003583#endif
3584
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003585#if defined(FEAT_SPELL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003586/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003587 * The 'spellcapcheck' option is changed.
3588 */
3589 char *
3590did_set_spellcapcheck(optset_T *args UNUSED)
3591{
3592 // compile the regexp program.
3593 return compile_cap_prog(curwin->w_s);
3594}
3595
3596/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003597 * The 'spellfile' option is changed.
3598 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003599 char *
3600did_set_spellfile(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003601{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003602 char_u **varp = (char_u **)args->os_varp;
3603
3604 if (!valid_spellfile(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003605 return e_invalid_argument;
3606
3607 // If there is a window for this buffer in which 'spell' is set load the
3608 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003609 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003610}
3611
3612/*
3613 * The 'spell' option is changed.
3614 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003615 char *
3616did_set_spelllang(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003617{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003618 char_u **varp = (char_u **)args->os_varp;
3619
3620 if (!valid_spelllang(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003621 return e_invalid_argument;
3622
3623 // If there is a window for this buffer in which 'spell' is set load the
3624 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003625 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003626}
3627
3628/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003629 * The 'spelloptions' option is changed.
3630 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003631 char *
3632did_set_spelloptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003633{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003634 char_u **varp = (char_u **)args->os_varp;
3635
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003636 if (**varp != NUL && STRCMP(*varp, "camel") != 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003637 return e_invalid_argument;
3638
3639 return NULL;
3640}
3641
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003642 int
3643expand_set_spelloptions(optexpand_T *args, int *numMatches, char_u ***matches)
3644{
3645 static char *(p_spo_values[]) = {"camel", NULL};
3646 return expand_set_opt_string(
3647 args,
3648 p_spo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003649 ARRAY_LENGTH(p_spo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003650 numMatches,
3651 matches);
3652}
3653
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003654/*
3655 * The 'spellsuggest' option is changed.
3656 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003657 char *
3658did_set_spellsuggest(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003659{
3660 if (spell_check_sps() != OK)
3661 return e_invalid_argument;
3662
3663 return NULL;
3664}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003665
3666 int
3667expand_set_spellsuggest(optexpand_T *args, int *numMatches, char_u ***matches)
3668{
3669 return expand_set_opt_string(
3670 args,
3671 p_sps_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003672 ARRAY_LENGTH(p_sps_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003673 numMatches,
3674 matches);
3675}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003676#endif
3677
3678/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003679 * The 'splitkeep' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003680 */
3681 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003682did_set_splitkeep(optset_T *args UNUSED)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003683{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003684 return did_set_opt_strings(p_spk, p_spk_values, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003685}
3686
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003687 int
3688expand_set_splitkeep(optexpand_T *args, int *numMatches, char_u ***matches)
3689{
3690 return expand_set_opt_string(
3691 args,
3692 p_spk_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003693 ARRAY_LENGTH(p_spk_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003694 numMatches,
3695 matches);
3696}
3697
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00003698#if defined(FEAT_STL_OPT) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003699/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003700 * The 'statusline' option is changed.
3701 */
3702 char *
3703did_set_statusline(optset_T *args)
3704{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003705 return parse_statustabline_rulerformat(args, FALSE);
3706}
3707#endif
3708
3709/*
3710 * The 'swapsync' option is changed.
3711 */
3712 char *
3713did_set_swapsync(optset_T *args UNUSED)
3714{
3715 return did_set_opt_strings(p_sws, p_sws_values, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003716}
3717
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003718 int
3719expand_set_swapsync(optexpand_T *args, int *numMatches, char_u ***matches)
3720{
3721 return expand_set_opt_string(
3722 args,
3723 p_sws_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003724 ARRAY_LENGTH(p_sws_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003725 numMatches,
3726 matches);
3727}
3728
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003729/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003730 * The 'switchbuf' option is changed.
3731 */
3732 char *
3733did_set_switchbuf(optset_T *args UNUSED)
3734{
3735 return did_set_opt_flags(p_swb, p_swb_values, &swb_flags, TRUE);
3736}
3737
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003738 int
3739expand_set_switchbuf(optexpand_T *args, int *numMatches, char_u ***matches)
3740{
3741 return expand_set_opt_string(
3742 args,
3743 p_swb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003744 ARRAY_LENGTH(p_swb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003745 numMatches,
3746 matches);
3747}
3748
LemonBoy5247b0b2024-07-12 19:30:58 +02003749/*
3750 * The 'tabclose' option is changed.
3751 */
3752 char *
3753did_set_tabclose(optset_T *args UNUSED)
3754{
3755 return did_set_opt_flags(p_tcl, p_tcl_values, &tcl_flags, TRUE);
3756}
3757
3758 int
3759expand_set_tabclose(optexpand_T *args, int *numMatches, char_u ***matches)
3760{
3761 return expand_set_opt_string(
3762 args,
3763 p_tcl_values,
3764 ARRAY_LENGTH(p_tcl_values) - 1,
3765 numMatches,
3766 matches);
3767}
3768
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003769#if defined(FEAT_STL_OPT) || defined(PROTO)
3770/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003771 * The 'tabline' option is changed.
3772 */
3773 char *
3774did_set_tabline(optset_T *args)
3775{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003776 return parse_statustabline_rulerformat(args, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003777}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003778#endif
3779
3780/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003781 * The 'tagcase' option is changed.
3782 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003783 char *
3784did_set_tagcase(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003785{
3786 unsigned int *flags;
3787 char_u *p;
3788
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003789 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003790 {
3791 p = curbuf->b_p_tc;
3792 flags = &curbuf->b_tc_flags;
3793 }
3794 else
3795 {
3796 p = p_tc;
3797 flags = &tc_flags;
3798 }
3799
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003800 if ((args->os_flags & OPT_LOCAL) && *p == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003801 // make the local value empty: use the global value
3802 *flags = 0;
3803 else if (*p == NUL
3804 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
3805 return e_invalid_argument;
3806
3807 return NULL;
3808}
3809
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003810 int
3811expand_set_tagcase(optexpand_T *args, int *numMatches, char_u ***matches)
3812{
3813 return expand_set_opt_string(
3814 args,
3815 p_tc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003816 ARRAY_LENGTH(p_tc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003817 numMatches,
3818 matches);
3819}
3820
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003821/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003822 * The 'term' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003823 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003824 char *
3825did_set_term(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003826{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003827 if (T_NAME[0] == NUL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003828 return e_cannot_set_term_to_empty_string;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003829#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003830 if (gui.in_use)
3831 return e_cannot_change_term_in_GUI;
3832 if (term_is_gui(T_NAME))
3833 return e_use_gui_to_start_GUI;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003834#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003835 if (set_termname(T_NAME) == FAIL)
3836 return e_not_found_in_termcap;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003837
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003838 // Screen colors may have changed.
3839 redraw_later_clear();
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003840
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003841 return NULL;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003842}
3843
3844/*
3845 * Some terminal option (t_xxx) is changed
3846 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003847 char *
3848did_set_term_option(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003849{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003850 char_u **varp = (char_u **)args->os_varp;
3851
3852 if (!full_screen)
3853 return NULL;
3854
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003855 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
3856 if (varp == &T_CCO)
3857 {
3858 int colors = atoi((char *)T_CCO);
3859
3860 // Only reinitialize colors if t_Co value has really changed to
3861 // avoid expensive reload of colorscheme if t_Co is set to the
3862 // same value multiple times.
3863 if (colors != t_colors)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003864 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003865 t_colors = colors;
3866 if (t_colors <= 1)
3867 {
3868 vim_free(T_CCO);
3869 T_CCO = empty_option;
3870 }
3871#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3872 if (is_term_win32())
3873 {
3874 swap_tcap();
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003875 args->os_did_swaptcap = TRUE;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003876 }
3877#endif
3878 // We now have a different color setup, initialize it again.
3879 init_highlight(TRUE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003880 }
3881 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003882 ttest(FALSE);
3883 if (varp == &T_ME)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003884 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003885 out_str(T_ME);
3886 redraw_later(UPD_CLEAR);
3887#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3888 // Since t_me has been set, this probably means that the user
3889 // wants to use this as default colors. Need to reset default
3890 // background/foreground colors.
3891# ifdef VIMDLL
3892 if (!gui.in_use && !gui.starting)
3893# endif
3894 mch_set_normal_colors();
3895#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003896 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003897 if (varp == &T_BE && termcap_active)
3898 {
3899 MAY_WANT_TO_LOG_THIS;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003900
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003901 if (*T_BE == NUL)
3902 // When clearing t_BE we assume the user no longer wants
3903 // bracketed paste, thus disable it by writing t_BD.
3904 out_str(T_BD);
3905 else
3906 out_str(T_BE);
3907 }
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003908
3909 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003910}
3911
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003912#if defined(FEAT_TERMINAL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003913/*
3914 * The 'termwinkey' option is changed.
3915 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003916 char *
Milly94606f72024-10-22 22:07:52 +02003917did_set_termwinkey(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003918{
Milly94606f72024-10-22 22:07:52 +02003919 char_u **varp = (char_u **)args->os_varp;
3920
3921 if ((*varp)[0] != NUL && string_to_key(*varp, TRUE) == 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003922 return e_invalid_argument;
3923
3924 return NULL;
3925}
3926
3927/*
3928 * The 'termwinsize' option is changed.
3929 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003930 char *
Milly8be10aa2024-10-22 22:01:46 +02003931did_set_termwinsize(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003932{
Milly8be10aa2024-10-22 22:01:46 +02003933 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003934 char_u *p;
3935
Milly8be10aa2024-10-22 22:01:46 +02003936 if ((*varp)[0] == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003937 return NULL;
3938
Milly8be10aa2024-10-22 22:01:46 +02003939 p = skipdigits(*varp);
3940 if (p == *varp || (*p != 'x' && *p != '*') || *skipdigits(p + 1) != NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003941 return e_invalid_argument;
3942
3943 return NULL;
3944}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003945
3946# if defined(MSWIN) || defined(PROTO)
3947/*
3948 * The 'termwintype' option is changed.
3949 */
3950 char *
3951did_set_termwintype(optset_T *args UNUSED)
3952{
3953 return did_set_opt_strings(p_twt, p_twt_values, FALSE);
3954}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003955
3956 int
3957expand_set_termwintype(optexpand_T *args, int *numMatches, char_u ***matches)
3958{
3959 return expand_set_opt_string(
3960 args,
3961 p_twt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003962 ARRAY_LENGTH(p_twt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003963 numMatches,
3964 matches);
3965}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003966# endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003967#endif
3968
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003969/*
3970 * The 'titlestring' option is changed.
3971 */
3972 char *
3973did_set_titlestring(optset_T *args)
3974{
3975 int flagval = 0;
3976
3977#ifdef FEAT_STL_OPT
3978 flagval = STL_IN_TITLE;
3979#endif
3980 return parse_titleiconstring(args, flagval);
3981}
3982
3983#if (defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)) || defined(PROTO)
3984/*
3985 * The 'toolbar' option is changed.
3986 */
3987 char *
3988did_set_toolbar(optset_T *args UNUSED)
3989{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003990 if (opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags,
3991 TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003992 return e_invalid_argument;
3993
3994 out_flush();
3995 gui_mch_show_toolbar((toolbar_flags &
3996 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
3997 return NULL;
3998}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003999
4000 int
4001expand_set_toolbar(optexpand_T *args, int *numMatches, char_u ***matches)
4002{
4003 return expand_set_opt_string(
4004 args,
4005 p_toolbar_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004006 ARRAY_LENGTH(p_toolbar_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004007 numMatches,
4008 matches);
4009}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004010#endif
4011
4012#if (defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)) || defined(PROTO)
4013/*
4014 * The 'toolbariconsize' option is changed. GTK+ 2 only.
4015 */
4016 char *
4017did_set_toolbariconsize(optset_T *args UNUSED)
4018{
4019 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
4020 return e_invalid_argument;
4021
4022 out_flush();
4023 gui_mch_show_toolbar((toolbar_flags &
4024 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4025 return NULL;
4026}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004027
4028 int
4029expand_set_toolbariconsize(optexpand_T *args, int *numMatches, char_u ***matches)
4030{
4031 return expand_set_opt_string(
4032 args,
4033 p_tbis_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004034 ARRAY_LENGTH(p_tbis_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004035 numMatches,
4036 matches);
4037}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004038#endif
4039
4040#if defined(UNIX) || defined(VMS) || defined(PROTO)
4041/*
4042 * The 'ttymouse' option is changed.
4043 */
4044 char *
4045did_set_ttymouse(optset_T *args UNUSED)
4046{
4047 char *errmsg = NULL;
4048
4049 // Switch the mouse off before changing the escape sequences used for
4050 // that.
4051 mch_setmouse(FALSE);
4052 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
4053 errmsg = e_invalid_argument;
4054 else
4055 check_mouse_termcode();
4056 if (termcap_active)
4057 setmouse(); // may switch it on again
4058
4059 return errmsg;
4060}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004061
4062 int
4063expand_set_ttymouse(optexpand_T *args, int *numMatches, char_u ***matches)
4064{
4065 return expand_set_opt_string(
4066 args,
4067 p_ttym_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004068 ARRAY_LENGTH(p_ttym_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004069 numMatches,
4070 matches);
4071}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004072#endif
4073
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004074#if defined(FEAT_VARTABS) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004075/*
4076 * The 'varsofttabstop' option is changed.
4077 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004078 char *
4079did_set_varsofttabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004080{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004081 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004082 char_u *cp;
4083
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004084 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004085 VIM_CLEAR(curbuf->b_p_vsts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004086 else
4087 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004088 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004089 {
4090 if (vim_isdigit(*cp))
4091 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004092 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004093 continue;
4094 return e_invalid_argument;
4095 }
4096
4097 int *oldarray = curbuf->b_p_vsts_array;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004098 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004099 {
4100 if (oldarray)
4101 vim_free(oldarray);
4102 }
4103 else
4104 return e_invalid_argument;
4105 }
4106
4107 return NULL;
4108}
4109
4110/*
4111 * The 'vartabstop' option is changed.
4112 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004113 char *
4114did_set_vartabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004115{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004116 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004117 char_u *cp;
4118
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004119 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004120 VIM_CLEAR(curbuf->b_p_vts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004121 else
4122 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004123 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004124 {
4125 if (vim_isdigit(*cp))
4126 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004127 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004128 continue;
4129 return e_invalid_argument;
4130 }
4131
4132 int *oldarray = curbuf->b_p_vts_array;
4133
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004134 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004135 {
4136 vim_free(oldarray);
4137# ifdef FEAT_FOLDING
4138 if (foldmethodIsIndent(curwin))
4139 foldUpdateAll(curwin);
4140# endif
4141 }
4142 else
4143 return e_invalid_argument;
4144 }
4145
4146 return NULL;
4147}
4148#endif
4149
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004150/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004151 * The 'verbosefile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004152 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004153 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004154did_set_verbosefile(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004155{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004156 verbose_stop();
4157 if (*p_vfile != NUL && verbose_open() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004158 return e_invalid_argument;
4159
4160 return NULL;
4161}
4162
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004163#if defined(FEAT_SESSION) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004164/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004165 * The 'viewoptions' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004166 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004167 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004168did_set_viewoptions(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004169{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004170 return did_set_opt_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004171}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004172#endif
4173
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004174#if defined(FEAT_VIMINFO) || defined(PROTO)
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004175/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004176 * The 'viminfo' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004177 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004178 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004179did_set_viminfo(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004180{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004181 char_u *s;
4182 char *errmsg = NULL;
4183
4184 for (s = p_viminfo; *s;)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004185 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004186 // Check it's a valid character
4187 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
4188 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01004189 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004190 break;
4191 }
4192 if (*s == 'n') // name is always last one
4193 break;
4194 else if (*s == 'r') // skip until next ','
4195 {
4196 while (*++s && *s != ',')
4197 ;
4198 }
4199 else if (*s == '%')
4200 {
4201 // optional number
4202 while (vim_isdigit(*++s))
4203 ;
4204 }
4205 else if (*s == '!' || *s == 'h' || *s == 'c')
4206 ++s; // no extra chars
4207 else // must have a number
4208 {
4209 while (vim_isdigit(*++s))
4210 ;
4211
4212 if (!VIM_ISDIGIT(*(s - 1)))
4213 {
4214 if (args->os_errbuf != NULL)
4215 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01004216 vim_snprintf(args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004217 _(e_missing_number_after_angle_str_angle),
4218 transchar_byte(*(s - 1)));
4219 errmsg = args->os_errbuf;
4220 }
4221 else
4222 errmsg = "";
4223 break;
4224 }
4225 }
4226 if (*s == ',')
4227 ++s;
4228 else if (*s)
4229 {
4230 if (args->os_errbuf != NULL)
4231 errmsg = e_missing_comma;
4232 else
4233 errmsg = "";
4234 break;
4235 }
4236 }
4237 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
4238 errmsg = e_must_specify_a_value;
4239
4240 return errmsg;
4241}
4242#endif
4243
4244/*
4245 * The 'virtualedit' option is changed.
4246 */
4247 char *
4248did_set_virtualedit(optset_T *args)
4249{
4250 char_u *ve = p_ve;
4251 unsigned int *flags = &ve_flags;
4252
4253 if (args->os_flags & OPT_LOCAL)
4254 {
4255 ve = curwin->w_p_ve;
4256 flags = &curwin->w_ve_flags;
4257 }
4258
4259 if ((args->os_flags & OPT_LOCAL) && *ve == NUL)
4260 // make the local value empty: use the global value
4261 *flags = 0;
4262 else
4263 {
4264 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
4265 return e_invalid_argument;
4266 else if (STRCMP(ve, args->os_oldval.string) != 0)
4267 {
4268 // Recompute cursor position in case the new 've' setting
4269 // changes something.
4270 validate_virtcol();
4271 coladvance(curwin->w_virtcol);
4272 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004273 }
4274
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004275 return NULL;
4276}
4277
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004278 int
4279expand_set_virtualedit(optexpand_T *args, int *numMatches, char_u ***matches)
4280{
4281 return expand_set_opt_string(
4282 args,
4283 p_ve_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004284 ARRAY_LENGTH(p_ve_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004285 numMatches,
4286 matches);
4287}
4288
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004289/*
4290 * The 'whichwrap' option is changed.
4291 */
4292 char *
4293did_set_whichwrap(optset_T *args)
4294{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004295 char_u **varp = (char_u **)args->os_varp;
4296
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004297 // Add ',' to the list flags because 'whichwrap' is a flag
4298 // list that is comma-separated.
Christian Brabandtb39b2402023-11-29 11:34:05 +01004299 return did_set_option_listflag(*varp, (char_u *)(WW_ALL ","),
4300 args->os_errbuf, args->os_errbuflen);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004301}
4302
4303 int
4304expand_set_whichwrap(optexpand_T *args, int *numMatches, char_u ***matches)
4305{
4306 return expand_set_opt_listflag(args, (char_u*)WW_ALL, numMatches, matches);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004307}
4308
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004309/*
4310 * The 'wildmode' option is changed.
4311 */
4312 char *
4313did_set_wildmode(optset_T *args UNUSED)
4314{
4315 if (check_opt_wim() == FAIL)
4316 return e_invalid_argument;
4317 return NULL;
4318}
4319
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004320 int
4321expand_set_wildmode(optexpand_T *args, int *numMatches, char_u ***matches)
4322{
4323 return expand_set_opt_string(
4324 args,
4325 p_wim_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004326 ARRAY_LENGTH(p_wim_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004327 numMatches,
4328 matches);
4329}
4330
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004331/*
4332 * The 'wildoptions' option is changed.
4333 */
4334 char *
4335did_set_wildoptions(optset_T *args UNUSED)
4336{
4337 return did_set_opt_strings(p_wop, p_wop_values, TRUE);
4338}
4339
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004340 int
4341expand_set_wildoptions(optexpand_T *args, int *numMatches, char_u ***matches)
4342{
4343 return expand_set_opt_string(
4344 args,
4345 p_wop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004346 ARRAY_LENGTH(p_wop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004347 numMatches,
4348 matches);
4349}
4350
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004351#if defined(FEAT_WAK) || defined(PROTO)
4352/*
4353 * The 'winaltkeys' option is changed.
4354 */
4355 char *
4356did_set_winaltkeys(optset_T *args UNUSED)
4357{
4358 char *errmsg = NULL;
4359
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004360 if (*p_wak == NUL || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004361 errmsg = e_invalid_argument;
4362# ifdef FEAT_MENU
4363# if defined(FEAT_GUI_MOTIF)
4364 else if (gui.in_use)
4365 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4366# elif defined(FEAT_GUI_GTK)
4367 else if (gui.in_use)
4368 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4369# endif
4370# endif
4371 return errmsg;
4372}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004373
4374 int
4375expand_set_winaltkeys(optexpand_T *args, int *numMatches, char_u ***matches)
4376{
4377 return expand_set_opt_string(
4378 args,
4379 p_wak_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004380 ARRAY_LENGTH(p_wak_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004381 numMatches,
4382 matches);
4383}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004384#endif
4385
4386/*
4387 * The 'wincolor' option is changed.
4388 */
4389 char *
4390did_set_wincolor(optset_T *args UNUSED)
4391{
4392#ifdef FEAT_TERMINAL
4393 term_update_wincolor(curwin);
4394#endif
4395 return NULL;
4396}
4397
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004398 int
4399expand_set_wincolor(optexpand_T *args, int *numMatches, char_u ***matches)
4400{
4401 return expand_set_opt_generic(
4402 args,
4403 get_highlight_name,
4404 numMatches,
4405 matches);
4406}
4407
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004408#ifdef FEAT_SYN_HL
4409/*
4410 * When the 'syntax' option is set, load the syntax of that name.
4411 */
4412 static void
4413do_syntax_autocmd(int value_changed)
4414{
4415 static int syn_recursive = 0;
4416
4417 ++syn_recursive;
4418 // Only pass TRUE for "force" when the value changed or not used
4419 // recursively, to avoid endless recurrence.
4420 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
4421 value_changed || syn_recursive == 1, curbuf);
4422 curbuf->b_flags |= BF_SYN_SET;
4423 --syn_recursive;
4424}
4425#endif
4426
4427/*
4428 * When the 'filetype' option is set, trigger the FileType autocommand.
4429 */
4430 static void
4431do_filetype_autocmd(char_u **varp, int opt_flags, int value_changed)
4432{
4433 // Skip this when called from a modeline and the filetype was already set
4434 // to this value.
4435 if ((opt_flags & OPT_MODELINE) && !value_changed)
4436 return;
4437
4438 static int ft_recursive = 0;
4439 int secure_save = secure;
4440
4441 // Reset the secure flag, since the value of 'filetype' has
4442 // been checked to be safe.
4443 secure = 0;
4444
4445 ++ft_recursive;
zeertzjq5bf6c212024-03-31 18:41:27 +02004446 curbuf->b_did_filetype = TRUE;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004447 // Only pass TRUE for "force" when the value changed or not
4448 // used recursively, to avoid endless recurrence.
4449 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
4450 value_changed || ft_recursive == 1, curbuf);
4451 --ft_recursive;
4452 // Just in case the old "curbuf" is now invalid.
4453 if (varp != &(curbuf->b_p_ft))
4454 varp = NULL;
4455
4456 secure = secure_save;
4457}
4458
4459#ifdef FEAT_SPELL
4460/*
4461 * When the 'spelllang' option is set, source the spell/LANG.vim file in
4462 * 'runtimepath'.
4463 */
4464 static void
4465do_spelllang_source(void)
4466{
4467 char_u fname[200];
4468 char_u *p;
4469 char_u *q = curwin->w_s->b_p_spl;
4470
4471 // Skip the first name if it is "cjk".
4472 if (STRNCMP(q, "cjk,", 4) == 0)
4473 q += 4;
4474
4475 // They could set 'spellcapcheck' depending on the language. Use the first
4476 // name in 'spelllang' up to '_region' or '.encoding'.
4477 for (p = q; *p != NUL; ++p)
4478 if (!ASCII_ISALNUM(*p) && *p != '-')
4479 break;
4480 if (p > q)
4481 {
4482 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
4483 (int)(p - q), q);
4484 source_runtime(fname, DIP_ALL);
4485 }
4486}
4487#endif
4488
4489/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004490 * Handle string options that need some action to perform when changed.
zeertzjqf6782732022-07-27 18:26:03 +01004491 * The new value must be allocated.
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004492 * Returns NULL for success, or an untranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +02004493 */
4494 char *
4495did_set_string_option(
4496 int opt_idx, // index in options[] table
4497 char_u **varp, // pointer to the option variable
Bram Moolenaardac13472019-09-16 21:06:21 +02004498 char_u *oldval, // previous value of the option
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004499 char_u *value, // new value of the option
Bram Moolenaardac13472019-09-16 21:06:21 +02004500 char *errbuf, // buffer for errors, or NULL
Mike Williams620f0112023-12-05 15:36:06 +01004501 size_t errbuflen, // length of error buffer
Bram Moolenaardac13472019-09-16 21:06:21 +02004502 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004503 set_op_T op, // OP_ADDING/OP_PREPENDING/OP_REMOVING
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004504 int *value_checked) // value was checked to be safe, no
Bram Moolenaardac13472019-09-16 21:06:21 +02004505 // need to set P_INSECURE
4506{
4507 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +02004508 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004509 opt_did_set_cb_T did_set_cb = get_option_did_set_cb(opt_idx);
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004510 optset_T args;
4511
4512 // 'ttytype' is an alias for 'term'. Both 'term' and 'ttytype' point to
4513 // T_NAME. If 'term' or 'ttytype' is modified, then use the index for the
4514 // 'term' option. Only set the P_ALLOCED flag on 'term'.
4515 if (varp == &T_NAME)
4516 {
4517 opt_idx = findoption((char_u *)"term");
4518 if (opt_idx >= 0)
4519 {
4520 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
4521 did_set_cb = get_option_did_set_cb(opt_idx);
4522 }
4523 }
4524
4525 CLEAR_FIELD(args);
Bram Moolenaardac13472019-09-16 21:06:21 +02004526
Bram Moolenaardac13472019-09-16 21:06:21 +02004527 // Disallow changing some options from secure mode
4528 if ((secure
4529#ifdef HAVE_SANDBOX
4530 || sandbox != 0
4531#endif
4532 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00004533 errmsg = e_not_allowed_here;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004534 // Check for a "normal" directory or file name in some options.
4535 else if (check_illegal_path_names(opt_idx, varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004536 errmsg = e_invalid_argument;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004537 else if (did_set_cb != NULL)
4538 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004539 args.os_varp = (char_u *)varp;
4540 args.os_idx = opt_idx;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004541 args.os_flags = opt_flags;
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004542 args.os_op = op;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004543 args.os_oldval.string = oldval;
4544 args.os_newval.string = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004545 args.os_errbuf = errbuf;
Christian Brabandtb39b2402023-11-29 11:34:05 +01004546 args.os_errbuflen = errbuflen;
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004547 // Invoke the option specific callback function to validate and apply
4548 // the new option value.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004549 errmsg = did_set_cb(&args);
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004550
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004551 // The 'keymap', 'filetype' and 'syntax' option callback functions
4552 // may change the os_value_checked field.
4553 *value_checked = args.os_value_checked;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004554 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004555
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004556 // If an error is detected, restore the previous value.
Bram Moolenaardac13472019-09-16 21:06:21 +02004557 if (errmsg != NULL)
4558 {
zeertzjqf6782732022-07-27 18:26:03 +01004559 free_string_option(*varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02004560 *varp = oldval;
4561 // When resetting some values, need to act on it.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004562 if (args.os_restore_chartab)
Bram Moolenaardac13472019-09-16 21:06:21 +02004563 (void)init_chartab();
4564 if (varp == &p_hl)
4565 (void)highlight_changed();
4566 }
4567 else
4568 {
4569#ifdef FEAT_EVAL
4570 // Remember where the option was set.
4571 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4572#endif
4573 // Free string options that are in allocated memory.
4574 // Use "free_oldval", because recursiveness may change the flags under
4575 // our fingers (esp. init_highlight()).
4576 if (free_oldval)
4577 free_string_option(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01004578 set_option_flag(opt_idx, P_ALLOCED);
Bram Moolenaardac13472019-09-16 21:06:21 +02004579
4580 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
4581 && is_global_local_option(opt_idx))
4582 {
4583 // global option with local value set to use global value; free
4584 // the local value and make it empty
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004585 char_u *p = get_option_varp_scope(opt_idx, OPT_LOCAL);
Bram Moolenaardac13472019-09-16 21:06:21 +02004586 free_string_option(*(char_u **)p);
4587 *(char_u **)p = empty_option;
4588 }
4589
4590 // May set global value for local option.
4591 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
4592 set_string_option_global(opt_idx, varp);
4593
4594 // Trigger the autocommand only after setting the flags.
4595#ifdef FEAT_SYN_HL
Bram Moolenaardac13472019-09-16 21:06:21 +02004596 if (varp == &(curbuf->b_p_syn))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004597 do_syntax_autocmd(args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004598#endif
4599 else if (varp == &(curbuf->b_p_ft))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004600 do_filetype_autocmd(varp, opt_flags, args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004601#ifdef FEAT_SPELL
4602 if (varp == &(curwin->w_s->b_p_spl))
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004603 do_spelllang_source();
Bram Moolenaardac13472019-09-16 21:06:21 +02004604#endif
4605 }
4606
Bram Moolenaardac13472019-09-16 21:06:21 +02004607 if (varp == &p_mouse)
4608 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004609 if (*p_mouse == NUL)
4610 mch_setmouse(FALSE); // switch mouse off
4611 else
Bram Moolenaardac13472019-09-16 21:06:21 +02004612 setmouse(); // in case 'mouse' changed
4613 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004614
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004615#if defined(FEAT_LUA) || defined(PROTO)
4616 if (varp == &p_rtp)
4617 update_package_paths_in_lua();
4618#endif
4619
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004620#if defined(FEAT_LINEBREAK)
4621 // Changing Formatlistpattern when briopt includes the list setting:
4622 // redraw
4623 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
4624 && curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004625 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004626#endif
4627
Bram Moolenaardac13472019-09-16 21:06:21 +02004628 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004629 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0
4630 && (get_option_flags(opt_idx) & P_HLONLY) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02004631 curwin->w_set_curswant = TRUE;
4632
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004633 if ((opt_flags & OPT_NO_REDRAW) == 0)
4634 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004635#ifdef FEAT_GUI
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004636 // set when changing an option that only requires a redraw in the GUI
4637 int redraw_gui_only = FALSE;
4638
4639 if (varp == &p_go // 'guioptions'
4640 || varp == &p_guifont // 'guifont'
4641# ifdef FEAT_GUI_TABLINE
4642 || varp == &p_gtl // 'guitablabel'
4643 || varp == &p_gtt // 'guitabtooltip'
4644# endif
4645# ifdef FEAT_XFONTSET
4646 || varp == &p_guifontset // 'guifontset'
4647# endif
4648 || varp == &p_guifontwide // 'guifontwide'
Erik S. V. Jansson2f026382024-02-26 22:23:05 +01004649# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004650 || varp == &p_guiligatures // 'guiligatures'
4651# endif
4652 )
4653 redraw_gui_only = TRUE;
4654
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004655 // check redraw when it's not a GUI option or the GUI is active.
4656 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02004657#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004658 check_redraw(get_option_flags(opt_idx));
4659 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004660
4661#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004662 if (args.os_did_swaptcap)
Bram Moolenaardac13472019-09-16 21:06:21 +02004663 {
4664 set_termname((char_u *)"win32");
4665 init_highlight(TRUE, FALSE);
4666 }
4667#endif
4668
4669 return errmsg;
4670}
4671
4672/*
4673 * Check an option that can be a range of string values.
4674 *
4675 * Return OK for correct value, FAIL otherwise.
4676 * Empty is always OK.
4677 */
4678 static int
4679check_opt_strings(
4680 char_u *val,
4681 char **values,
4682 int list) // when TRUE: accept a list of values
4683{
4684 return opt_strings_flags(val, values, NULL, list);
4685}
4686
4687/*
4688 * Handle an option that can be a range of string values.
4689 * Set a flag in "*flagp" for each string present.
4690 *
4691 * Return OK for correct value, FAIL otherwise.
4692 * Empty is always OK.
4693 */
4694 static int
4695opt_strings_flags(
4696 char_u *val, // new value
4697 char **values, // array of valid string values
4698 unsigned *flagp,
4699 int list) // when TRUE: accept a list of values
4700{
4701 int i;
4702 int len;
4703 unsigned new_flags = 0;
4704
4705 while (*val)
4706 {
4707 for (i = 0; ; ++i)
4708 {
4709 if (values[i] == NULL) // val not found in values[]
4710 return FAIL;
4711
4712 len = (int)STRLEN(values[i]);
4713 if (STRNCMP(values[i], val, len) == 0
4714 && ((list && val[len] == ',') || val[len] == NUL))
4715 {
4716 val += len + (val[len] == ',');
4717 new_flags |= (1 << i);
4718 break; // check next item in val list
4719 }
4720 }
4721 }
4722 if (flagp != NULL)
4723 *flagp = new_flags;
4724
4725 return OK;
4726}
4727
4728/*
4729 * return OK if "p" is a valid fileformat name, FAIL otherwise.
4730 */
4731 int
4732check_ff_value(char_u *p)
4733{
4734 return check_opt_strings(p, p_ff_values, FALSE);
4735}
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004736
4737/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004738 * Save the actual shortmess Flags and clear them temporarily to avoid that
4739 * file messages overwrites any output from the following commands.
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004740 *
4741 * Caller must make sure to first call save_clear_shm_value() and then
4742 * restore_shm_value() exactly the same number of times.
4743 */
4744 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004745save_clear_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004746{
4747 if (STRLEN(p_shm) >= SHM_LEN)
4748 {
4749 iemsg(e_internal_error_shortmess_too_long);
4750 return;
4751 }
4752
4753 if (++set_shm_recursive == 1)
4754 {
4755 STRCPY(shm_buf, p_shm);
4756 set_option_value_give_err((char_u *)"shm", 0L, (char_u *)"", 0);
4757 }
4758}
4759
4760/*
4761 * Restore the shortmess Flags set from the save_clear_shm_value() function.
4762 */
4763 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004764restore_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004765{
4766 if (--set_shm_recursive == 0)
4767 {
4768 set_option_value_give_err((char_u *)"shm", 0L, shm_buf, 0);
4769 vim_memset(shm_buf, 0, SHM_LEN);
4770 }
4771}