blob: 2049633f8e018d461d3460dd26e4cba9ecc75b11 [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 *
1238did_set_breakindentopt(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001239{
1240 char *errmsg = NULL;
1241
1242 if (briopt_check(curwin) == FAIL)
1243 errmsg = e_invalid_argument;
1244 // list setting requires a redraw
1245 if (curwin->w_briopt_list)
1246 redraw_all_later(UPD_NOT_VALID);
1247
1248 return errmsg;
1249}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001250
1251 int
1252expand_set_breakindentopt(optexpand_T *args, int *numMatches, char_u ***matches)
1253{
1254 return expand_set_opt_string(
1255 args,
1256 p_briopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001257 ARRAY_LENGTH(p_briopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001258 numMatches,
1259 matches);
1260}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001261#endif
1262
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001263#if defined(FEAT_BROWSE) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001264/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001265 * The 'browsedir' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001266 */
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00001267 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001268did_set_browsedir(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001269{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001270 if (check_opt_strings(p_bsdir, p_bsdir_values, FALSE) != OK
1271 && !mch_isdir(p_bsdir))
1272 return e_invalid_argument;
1273
1274 return NULL;
1275}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001276
1277 int
1278expand_set_browsedir(optexpand_T *args, int *numMatches, char_u ***matches)
1279{
1280 return expand_set_opt_string(
1281 args,
1282 p_bsdir_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001283 ARRAY_LENGTH(p_bsdir_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001284 numMatches,
1285 matches);
1286}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001287#endif
1288
1289/*
1290 * The 'bufhidden' option is changed.
1291 */
1292 char *
1293did_set_bufhidden(optset_T *args UNUSED)
1294{
1295 return did_set_opt_strings(curbuf->b_p_bh, p_bufhidden_values, FALSE);
1296}
1297
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001298 int
1299expand_set_bufhidden(optexpand_T *args, int *numMatches, char_u ***matches)
1300{
1301 return expand_set_opt_string(
1302 args,
1303 p_bufhidden_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001304 ARRAY_LENGTH(p_bufhidden_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001305 numMatches,
1306 matches);
1307}
1308
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001309/*
1310 * The 'buftype' option is changed.
1311 */
1312 char *
1313did_set_buftype(optset_T *args UNUSED)
1314{
1315 if (check_opt_strings(curbuf->b_p_bt, p_buftype_values, FALSE) != OK)
1316 return e_invalid_argument;
1317
1318 if (curwin->w_status_height)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001319 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001320 curwin->w_redr_status = TRUE;
1321 redraw_later(UPD_VALID);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001322 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001323 curbuf->b_help = (curbuf->b_p_bt[0] == 'h');
1324 redraw_titles();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001325
1326 return NULL;
1327}
1328
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001329 int
1330expand_set_buftype(optexpand_T *args, int *numMatches, char_u ***matches)
1331{
1332 return expand_set_opt_string(
1333 args,
1334 p_buftype_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001335 ARRAY_LENGTH(p_buftype_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001336 numMatches,
1337 matches);
1338}
1339
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001340/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001341 * The 'casemap' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001342 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001343 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001344did_set_casemap(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001345{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001346 return did_set_opt_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
1347}
1348
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001349 int
1350expand_set_casemap(optexpand_T *args, int *numMatches, char_u ***matches)
1351{
1352 return expand_set_opt_string(
1353 args,
1354 p_cmp_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001355 ARRAY_LENGTH(p_cmp_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001356 numMatches,
1357 matches);
1358}
1359
1360#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1361 int
1362expand_set_clipboard(optexpand_T *args, int *numMatches, char_u ***matches)
1363{
1364 return expand_set_opt_string(
1365 args,
1366 p_cb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001367 ARRAY_LENGTH(p_cb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001368 numMatches,
1369 matches);
1370}
1371#endif
1372
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001373/*
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001374 * The global 'listchars' or 'fillchars' option is changed.
1375 */
1376 static char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01001377did_set_global_listfillchars(char_u *val, int opt_lcs, int opt_flags,
1378 char *errbuf, size_t errbuflen)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001379{
1380 char *errmsg = NULL;
1381 char_u **local_ptr = opt_lcs ? &curwin->w_p_lcs : &curwin->w_p_fcs;
1382
1383 // only apply the global value to "curwin" when it does not have a
1384 // local value
1385 if (opt_lcs)
1386 errmsg = set_listchars_option(curwin, val,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001387 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL),
1388 errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001389 else
1390 errmsg = set_fillchars_option(curwin, val,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001391 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL),
1392 errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001393 if (errmsg != NULL)
1394 return errmsg;
1395
1396 tabpage_T *tp;
1397 win_T *wp;
1398
1399 // If the current window is set to use the global
1400 // 'listchars'/'fillchars' value, clear the window-local value.
1401 if (!(opt_flags & OPT_GLOBAL))
1402 clear_string_option(local_ptr);
1403 FOR_ALL_TAB_WINDOWS(tp, wp)
1404 {
1405 // If the current window has a local value need to apply it
1406 // again, it was changed when setting the global value.
1407 // If no error was returned above, we don't expect an error
1408 // here, so ignore the return value.
1409 if (opt_lcs)
1410 {
1411 if (*wp->w_p_lcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001412 (void)set_listchars_option(wp, wp->w_p_lcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001413 }
1414 else
1415 {
1416 if (*wp->w_p_fcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001417 (void)set_fillchars_option(wp, wp->w_p_fcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001418 }
1419 }
1420
1421 redraw_all_later(UPD_NOT_VALID);
1422
1423 return NULL;
1424}
1425
1426/*
1427 * The 'fillchars' option or the 'listchars' option is changed.
1428 */
1429 char *
1430did_set_chars_option(optset_T *args)
1431{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001432 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001433 char *errmsg = NULL;
1434
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001435 if ( varp == &p_lcs // global 'listchars'
1436 || varp == &p_fcs) // global 'fillchars'
1437 errmsg = did_set_global_listfillchars(*varp, varp == &p_lcs,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001438 args->os_flags, args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001439 else if (varp == &curwin->w_p_lcs) // local 'listchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001440 errmsg = set_listchars_option(curwin, *varp, TRUE,
1441 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001442 else if (varp == &curwin->w_p_fcs) // local 'fillchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001443 errmsg = set_fillchars_option(curwin, *varp, TRUE,
1444 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001445
1446 return errmsg;
1447}
1448
1449/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001450 * Expand 'fillchars' or 'listchars' option value.
1451 */
1452 int
1453expand_set_chars_option(optexpand_T *args, int *numMatches, char_u ***matches)
1454{
1455 char_u **varp = (char_u **)args->oe_varp;
1456 int is_lcs = (varp == &p_lcs || varp == &curwin->w_p_lcs);
1457 return expand_set_opt_generic(
1458 args,
1459 is_lcs ? get_listchars_name : get_fillchars_name,
1460 numMatches,
1461 matches);
1462}
1463
1464/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001465 * The 'cinoptions' option is changed.
1466 */
1467 char *
1468did_set_cinoptions(optset_T *args UNUSED)
1469{
1470 // TODO: recognize errors
1471 parse_cino(curbuf);
1472
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001473 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001474}
1475
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00001476#if defined(FEAT_SYN_HL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001477/*
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001478 * The 'colorcolumn' option is changed.
1479 */
1480 char *
1481did_set_colorcolumn(optset_T *args UNUSED)
1482{
1483 return check_colorcolumn(curwin);
1484}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001485#endif
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001486
1487/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001488 * The 'comments' option is changed.
1489 */
1490 char *
1491did_set_comments(optset_T *args)
1492{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001493 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001494 char_u *s;
1495 char *errmsg = NULL;
1496
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001497 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001498 {
1499 while (*s && *s != ':')
1500 {
1501 if (vim_strchr((char_u *)COM_ALL, *s) == NULL
1502 && !VIM_ISDIGIT(*s) && *s != '-')
1503 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01001504 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001505 break;
1506 }
1507 ++s;
1508 }
1509 if (*s++ == NUL)
1510 errmsg = e_missing_colon;
1511 else if (*s == ',' || *s == NUL)
1512 errmsg = e_zero_length_string;
1513 if (errmsg != NULL)
1514 break;
1515 while (*s && *s != ',')
1516 {
1517 if (*s == '\\' && s[1] != NUL)
1518 ++s;
1519 ++s;
1520 }
1521 s = skip_to_option_part(s);
1522 }
1523
1524 return errmsg;
1525}
1526
1527#if defined(FEAT_FOLDING) || defined(PROTO)
1528/*
1529 * The 'commentstring' option is changed.
1530 */
1531 char *
1532did_set_commentstring(optset_T *args)
1533{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001534 char_u **varp = (char_u **)args->os_varp;
1535
1536 if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001537 return e_commentstring_must_be_empty_or_contain_str;
1538
1539 return NULL;
1540}
1541#endif
1542
1543/*
1544 * The 'complete' option is changed.
1545 */
1546 char *
1547did_set_complete(optset_T *args)
1548{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001549 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001550 char_u *s;
1551
1552 // check if it is a valid value for 'complete' -- Acevedo
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001553 for (s = *varp; *s;)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001554 {
1555 while (*s == ',' || *s == ' ')
1556 s++;
1557 if (!*s)
1558 break;
1559 if (vim_strchr((char_u *)".wbuksid]tU", *s) == NULL)
Christian Brabandtb39b2402023-11-29 11:34:05 +01001560 return illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001561 if (*++s != NUL && *s != ',' && *s != ' ')
1562 {
1563 if (s[-1] == 'k' || s[-1] == 's')
1564 {
1565 // skip optional filename after 'k' and 's'
1566 while (*s && *s != ',' && *s != ' ')
1567 {
1568 if (*s == '\\' && s[1] != NUL)
1569 ++s;
1570 ++s;
1571 }
1572 }
1573 else
1574 {
1575 if (args->os_errbuf != NULL)
1576 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01001577 vim_snprintf((char *)args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001578 _(e_illegal_character_after_chr), *--s);
1579 return args->os_errbuf;
1580 }
1581 return "";
1582 }
1583 }
1584 }
1585
1586 return NULL;
1587}
1588
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001589 int
1590expand_set_complete(optexpand_T *args, int *numMatches, char_u ***matches)
1591{
1592 static char *(p_cpt_values[]) = {
1593 ".", "w", "b", "u", "k", "kspell", "s", "i", "d", "]", "t", "U",
1594 NULL};
1595 return expand_set_opt_string(
1596 args,
1597 p_cpt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001598 ARRAY_LENGTH(p_cpt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001599 numMatches,
1600 matches);
1601}
1602
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001603/*
1604 * The 'completeopt' option is changed.
1605 */
1606 char *
1607did_set_completeopt(optset_T *args UNUSED)
1608{
zeertzjq529b9ad2024-06-05 20:27:06 +02001609 char_u *cot = p_cot;
1610 unsigned *flags = &cot_flags;
1611
1612 if (args->os_flags & OPT_LOCAL)
1613 {
1614 cot = curbuf->b_p_cot;
1615 flags = &curbuf->b_cot_flags;
1616 }
1617
1618 if (check_opt_strings(cot, p_cot_values, TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001619 return e_invalid_argument;
1620
zeertzjq529b9ad2024-06-05 20:27:06 +02001621 if (opt_strings_flags(cot, p_cot_values, flags, TRUE) != OK)
1622 return e_invalid_argument;
1623
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001624 return NULL;
1625}
1626
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001627 int
1628expand_set_completeopt(optexpand_T *args, int *numMatches, char_u ***matches)
1629{
1630 return expand_set_opt_string(
1631 args,
1632 p_cot_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001633 ARRAY_LENGTH(p_cot_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001634 numMatches,
1635 matches);
1636}
1637
glepnir6a89c942024-10-01 20:32:12 +02001638/*
1639 * The 'completeitemalign' option is changed.
1640 */
1641 char *
1642did_set_completeitemalign(optset_T *args UNUSED)
1643{
1644 char_u *p = p_cia;
1645 unsigned new_cia_flags = 0;
1646 int seen[3] = { FALSE, FALSE, FALSE };
1647 int count = 0;
1648 char_u buf[10];
1649
1650 while (*p)
1651 {
1652 copy_option_part(&p, buf, sizeof(buf), ",");
1653 if (count >= 3)
1654 return e_invalid_argument;
1655
1656 if (STRCMP(buf, "abbr") == 0)
1657 {
1658 if (seen[CPT_ABBR])
1659 return e_invalid_argument;
1660 new_cia_flags = new_cia_flags * 10 + CPT_ABBR;
1661 seen[CPT_ABBR] = TRUE;
1662 count++;
1663 }
1664 else if (STRCMP(buf, "kind") == 0)
1665 {
1666 if (seen[CPT_KIND])
1667 return e_invalid_argument;
1668 new_cia_flags = new_cia_flags * 10 + CPT_KIND;
1669 seen[CPT_KIND] = TRUE;
1670 count++;
1671 }
1672 else if (STRCMP(buf, "menu") == 0)
1673 {
1674 if (seen[CPT_MENU])
1675 return e_invalid_argument;
1676 new_cia_flags = new_cia_flags * 10 + CPT_MENU;
1677 seen[CPT_MENU] = TRUE;
1678 count++;
1679 }
1680 else
1681 return e_invalid_argument;
1682 }
1683 if (new_cia_flags == 0 || count != 3)
1684 return e_invalid_argument;
1685
1686 cia_flags = new_cia_flags;
1687 return NULL;
1688}
1689
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001690#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1691/*
1692 * The 'completepopup' option is changed.
1693 */
1694 char *
1695did_set_completepopup(optset_T *args UNUSED)
1696{
1697 if (parse_completepopup(NULL) == FAIL)
1698 return e_invalid_argument;
1699
1700 popup_close_info();
1701 return NULL;
1702}
1703#endif
1704
1705#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1706/*
1707 * The 'completeslash' option is changed.
1708 */
1709 char *
1710did_set_completeslash(optset_T *args UNUSED)
1711{
1712 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1713 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
1714 return e_invalid_argument;
1715
1716 return NULL;
1717}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001718
1719 int
1720expand_set_completeslash(optexpand_T *args, int *numMatches, char_u ***matches)
1721{
1722 return expand_set_opt_string(
1723 args,
1724 p_csl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001725 ARRAY_LENGTH(p_csl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001726 numMatches,
1727 matches);
1728}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001729#endif
1730
1731#if defined(FEAT_CONCEAL) || defined(PROTO)
1732/*
1733 * The 'concealcursor' option is changed.
1734 */
1735 char *
1736did_set_concealcursor(optset_T *args)
1737{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001738 char_u **varp = (char_u **)args->os_varp;
1739
Christian Brabandtb39b2402023-11-29 11:34:05 +01001740 return did_set_option_listflag(*varp, (char_u *)COCU_ALL, args->os_errbuf,
1741 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001742}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001743
1744 int
1745expand_set_concealcursor(optexpand_T *args, int *numMatches, char_u ***matches)
1746{
1747 return expand_set_opt_listflag(args, (char_u*)COCU_ALL, numMatches, matches);
1748}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001749#endif
1750
1751/*
1752 * The 'cpoptions' option is changed.
1753 */
1754 char *
1755did_set_cpoptions(optset_T *args)
1756{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001757 char_u **varp = (char_u **)args->os_varp;
1758
Christian Brabandtb39b2402023-11-29 11:34:05 +01001759 return did_set_option_listflag(*varp, (char_u *)CPO_ALL, args->os_errbuf,
1760 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001761}
1762
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001763 int
1764expand_set_cpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
1765{
1766 return expand_set_opt_listflag(args, (char_u*)CPO_ALL, numMatches, matches);
1767}
1768
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001769#if defined(FEAT_CRYPT) || defined(PROTO)
1770/*
1771 * The 'cryptkey' option is changed.
1772 */
1773 char *
1774did_set_cryptkey(optset_T *args)
1775{
1776 // Make sure the ":set" command doesn't show the new value in the
1777 // history.
1778 remove_key_from_history();
1779
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02001780 if (args->os_op != OP_NONE)
1781 // Don't allow set+=/-=/^= as they can allow for substring guessing
1782 return e_invalid_argument;
1783
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001784 if (STRCMP(curbuf->b_p_key, args->os_oldval.string) != 0)
1785 {
1786 // Need to update the swapfile.
1787 ml_set_crypt_key(curbuf, args->os_oldval.string,
1788 *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
1789 changed_internal();
1790 }
Christian Brabandt19e6c4f2023-06-27 18:57:10 +01001791# ifdef FEAT_SODIUM
1792 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
1793 crypt_sodium_lock_key(args->os_newval.string);
1794# endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001795
1796 return NULL;
1797}
1798
1799/*
1800 * The 'cryptmethod' option is changed.
1801 */
1802 char *
1803did_set_cryptmethod(optset_T *args)
1804{
1805 char_u *p;
1806 char_u *s;
1807
1808 if (args->os_flags & OPT_LOCAL)
1809 p = curbuf->b_p_cm;
1810 else
1811 p = p_cm;
1812 if (check_opt_strings(p, p_cm_values, TRUE) != OK)
1813 return e_invalid_argument;
1814 else if (crypt_self_test() == FAIL)
1815 return e_invalid_argument;
1816
1817 // When setting the global value to empty, make it "zip".
1818 if (*p_cm == NUL)
1819 {
1820 free_string_option(p_cm);
1821 p_cm = vim_strsave((char_u *)"zip");
1822 }
1823 // When using ":set cm=name" the local value is going to be empty.
1824 // Do that here, otherwise the crypt functions will still use the
1825 // local value.
1826 if ((args->os_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1827 {
1828 free_string_option(curbuf->b_p_cm);
1829 curbuf->b_p_cm = empty_option;
1830 }
1831
1832 // Need to update the swapfile when the effective method changed.
1833 // Set "s" to the effective old value, "p" to the effective new
1834 // method and compare.
1835 if ((args->os_flags & OPT_LOCAL) && *args->os_oldval.string == NUL)
1836 s = p_cm; // was previously using the global value
1837 else
1838 s = args->os_oldval.string;
1839 if (*curbuf->b_p_cm == NUL)
1840 p = p_cm; // is now using the global value
1841 else
1842 p = curbuf->b_p_cm;
1843 if (STRCMP(s, p) != 0)
1844 ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1845
1846 // If the global value changes need to update the swapfile for all
1847 // buffers using that value.
1848 if ((args->os_flags & OPT_GLOBAL)
1849 && STRCMP(p_cm, args->os_oldval.string) != 0)
1850 {
1851 buf_T *buf;
1852
1853 FOR_ALL_BUFFERS(buf)
1854 if (buf != curbuf && *buf->b_p_cm == NUL)
1855 ml_set_crypt_key(buf, buf->b_p_key, args->os_oldval.string);
1856 }
1857 return NULL;
1858}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001859
1860 int
1861expand_set_cryptmethod(optexpand_T *args, int *numMatches, char_u ***matches)
1862{
1863 return expand_set_opt_string(
1864 args,
1865 p_cm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001866 ARRAY_LENGTH(p_cm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001867 numMatches,
1868 matches);
1869}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001870#endif
1871
1872#if (defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1873/*
1874 * The 'cscopequickfix' option is changed.
1875 */
1876 char *
1877did_set_cscopequickfix(optset_T *args UNUSED)
1878{
1879 char_u *p;
1880
1881 if (p_csqf == NULL)
1882 return NULL;
1883
1884 p = p_csqf;
1885 while (*p != NUL)
1886 {
1887 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
1888 || p[1] == NUL
1889 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
1890 || (p[2] != NUL && p[2] != ','))
1891 return e_invalid_argument;
1892 else if (p[2] == NUL)
1893 break;
1894 else
1895 p += 3;
1896 }
1897
1898 return NULL;
1899}
1900#endif
1901
1902#if defined(FEAT_SYN_HL) || defined(PROTO)
1903/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001904 * The 'cursorlineopt' option is changed.
1905 */
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001906 char *
1907did_set_cursorlineopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001908{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001909 char_u **varp = (char_u **)args->os_varp;
1910
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001911 // This could be changed to use opt_strings_flags() instead.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001912 if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001913 return e_invalid_argument;
1914
1915 return NULL;
1916}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001917
1918 int
1919expand_set_cursorlineopt(optexpand_T *args, int *numMatches, char_u ***matches)
1920{
1921 return expand_set_opt_string(
1922 args,
1923 p_culopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001924 ARRAY_LENGTH(p_culopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001925 numMatches,
1926 matches);
1927}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001928#endif
1929
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001930/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001931 * The 'debug' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001932 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001933 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001934did_set_debug(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001935{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001936 return did_set_opt_strings(p_debug, p_debug_values, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001937}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001938
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001939 int
1940expand_set_debug(optexpand_T *args, int *numMatches, char_u ***matches)
1941{
1942 return expand_set_opt_string(
1943 args,
1944 p_debug_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001945 ARRAY_LENGTH(p_debug_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001946 numMatches,
1947 matches);
1948}
1949
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001950#if defined(FEAT_DIFF) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001951/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001952 * The 'diffopt' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001953 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001954 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001955did_set_diffopt(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001956{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001957 if (diffopt_changed() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001958 return e_invalid_argument;
1959
1960 return NULL;
1961}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001962
1963 int
1964expand_set_diffopt(optexpand_T *args, int *numMatches, char_u ***matches)
1965{
1966 expand_T *xp = args->oe_xp;
1967
1968 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
1969 {
1970 // Within "algorithm:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001971 int algo_len = (int)STRLEN("algorithm:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001972 if (xp->xp_pattern - args->oe_set_arg >= algo_len &&
1973 STRNCMP(xp->xp_pattern - algo_len, "algorithm:", algo_len) == 0)
1974 {
1975 return expand_set_opt_string(
1976 args,
1977 p_dip_algorithm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001978 ARRAY_LENGTH(p_dip_algorithm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001979 numMatches,
1980 matches);
1981 }
1982 return FAIL;
1983 }
1984
1985 return expand_set_opt_string(
1986 args,
1987 p_dip_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001988 ARRAY_LENGTH(p_dip_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001989 numMatches,
1990 matches);
1991}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001992#endif
1993
1994/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001995 * The 'display' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001996 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001997 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001998did_set_display(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001999{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002000 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002001 return e_invalid_argument;
2002
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002003 (void)init_chartab();
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002004 return NULL;
2005}
2006
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002007 int
2008expand_set_display(optexpand_T *args, int *numMatches, char_u ***matches)
2009{
2010 return expand_set_opt_string(
2011 args,
2012 p_dy_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002013 ARRAY_LENGTH(p_dy_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002014 numMatches,
2015 matches);
2016}
2017
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002018/*
2019 * The 'eadirection' option is changed.
2020 */
2021 char *
2022did_set_eadirection(optset_T *args UNUSED)
2023{
2024 return did_set_opt_strings(p_ead, p_ead_values, FALSE);
2025}
2026
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002027 int
2028expand_set_eadirection(optexpand_T *args, int *numMatches, char_u ***matches)
2029{
2030 return expand_set_opt_string(
2031 args,
2032 p_ead_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002033 ARRAY_LENGTH(p_ead_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002034 numMatches,
2035 matches);
2036}
2037
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002038/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002039 * One of the 'encoding', 'fileencoding', 'termencoding' or 'makeencoding'
2040 * options is changed.
2041 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002042 char *
2043did_set_encoding(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002044{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002045 char_u **varp = (char_u **)args->os_varp;
2046 char_u **gvarp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002047 char *errmsg = NULL;
2048 char_u *p;
2049
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002050 // Get the global option to compare with, otherwise we would have to check
2051 // two values for all local options.
2052 gvarp = (char_u **)get_option_varp_scope(args->os_idx, OPT_GLOBAL);
2053
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002054 if (gvarp == &p_fenc)
2055 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002056 if (!curbuf->b_p_ma && args->os_flags != OPT_GLOBAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002057 errmsg = e_cannot_make_changes_modifiable_is_off;
2058 else if (vim_strchr(*varp, ',') != NULL)
2059 // No comma allowed in 'fileencoding'; catches confusing it
2060 // with 'fileencodings'.
2061 errmsg = e_invalid_argument;
2062 else
2063 {
2064 // May show a "+" in the title now.
2065 redraw_titles();
2066 // Add 'fileencoding' to the swap file.
2067 ml_setflags(curbuf);
2068 }
2069 }
2070 if (errmsg == NULL)
2071 {
2072 // canonize the value, so that STRCMP() can be used on it
2073 p = enc_canonize(*varp);
2074 if (p != NULL)
2075 {
2076 vim_free(*varp);
2077 *varp = p;
2078 }
2079 if (varp == &p_enc)
2080 {
2081 errmsg = mb_init();
2082 redraw_titles();
2083 }
2084 }
2085
2086#if defined(FEAT_GUI_GTK)
2087 if (errmsg == NULL && varp == &p_tenc && gui.in_use)
2088 {
2089 // GTK uses only a single encoding, and that is UTF-8.
2090 if (STRCMP(p_tenc, "utf-8") != 0)
2091 errmsg = e_cannot_be_changed_in_gtk_GUI;
2092 }
2093#endif
2094
2095 if (errmsg == NULL)
2096 {
2097#ifdef FEAT_KEYMAP
2098 // When 'keymap' is used and 'encoding' changes, reload the keymap
2099 // (with another encoding).
2100 if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
2101 (void)keymap_init();
2102#endif
2103
2104 // When 'termencoding' is not empty and 'encoding' changes or when
2105 // 'termencoding' changes, need to setup for keyboard input and
2106 // display output conversion.
2107 if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
2108 {
2109 if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
2110 || convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
2111 {
2112 semsg(_(e_cannot_convert_between_str_and_str),
2113 p_tenc, p_enc);
2114 errmsg = e_invalid_argument;
2115 }
2116 }
2117
2118#if defined(MSWIN)
K.Takatace3189d2023-02-15 19:13:43 +00002119 // $HOME, $VIM and $VIMRUNTIME may have characters in active code page.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002120 if (varp == &p_enc)
K.Takatace3189d2023-02-15 19:13:43 +00002121 {
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002122 init_homedir();
K.Takatace3189d2023-02-15 19:13:43 +00002123 init_vimdir();
2124 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002125#endif
2126 }
2127
2128 return errmsg;
2129}
2130
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002131 int
2132expand_set_encoding(optexpand_T *args, int *numMatches, char_u ***matches)
2133{
2134 return expand_set_opt_generic(
2135 args,
2136 get_encoding_name,
2137 numMatches,
2138 matches);
2139}
2140
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002141/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002142 * The 'eventignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002143 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002144 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002145did_set_eventignore(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002146{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002147 if (check_ei() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002148 return e_invalid_argument;
2149 return NULL;
2150}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002151
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002152 static char_u *
2153get_eventignore_name(expand_T *xp, int idx)
2154{
2155 // 'eventignore' allows special keyword "all" in addition to
2156 // all event names.
2157 if (idx == 0)
2158 return (char_u *)"all";
2159 return get_event_name_no_group(xp, idx - 1);
2160}
2161
2162 int
2163expand_set_eventignore(optexpand_T *args, int *numMatches, char_u ***matches)
2164{
2165 return expand_set_opt_generic(
2166 args,
2167 get_eventignore_name,
2168 numMatches,
2169 matches);
2170}
2171
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002172/*
2173 * The 'fileformat' option is changed.
2174 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002175 char *
2176did_set_fileformat(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002177{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002178 char_u **varp = (char_u **)args->os_varp;
2179
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002180 if (!curbuf->b_p_ma && !(args->os_flags & OPT_GLOBAL))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002181 return e_cannot_make_changes_modifiable_is_off;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002182 else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002183 return e_invalid_argument;
2184
2185 // may also change 'textmode'
2186 if (get_fileformat(curbuf) == EOL_DOS)
2187 curbuf->b_p_tx = TRUE;
2188 else
2189 curbuf->b_p_tx = FALSE;
2190 redraw_titles();
2191 // update flag in swap file
2192 ml_setflags(curbuf);
2193 // Redraw needed when switching to/from "mac": a CR in the text
2194 // will be displayed differently.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002195 if (get_fileformat(curbuf) == EOL_MAC || *args->os_oldval.string == 'm')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002196 redraw_curbuf_later(UPD_NOT_VALID);
2197
2198 return NULL;
2199}
2200
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002201 int
2202expand_set_fileformat(optexpand_T *args, int *numMatches, char_u ***matches)
2203{
2204 return expand_set_opt_string(
2205 args,
2206 p_ff_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002207 ARRAY_LENGTH(p_ff_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002208 numMatches,
2209 matches);
2210}
2211
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002212/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002213 * Function given to ExpandGeneric() to obtain the possible arguments of the
2214 * fileformat options.
2215 */
2216 char_u *
2217get_fileformat_name(expand_T *xp UNUSED, int idx)
2218{
2219 if (idx >= (int)ARRAY_LENGTH(p_ff_values))
2220 return NULL;
2221
2222 return (char_u*)p_ff_values[idx];
2223}
2224
2225/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002226 * The 'fileformats' option is changed.
2227 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002228 char *
2229did_set_fileformats(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002230{
2231 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
2232 return e_invalid_argument;
2233
2234 // also change 'textauto'
2235 if (*p_ffs == NUL)
2236 p_ta = FALSE;
2237 else
2238 p_ta = TRUE;
2239
2240 return NULL;
2241}
2242
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002243/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002244 * The 'filetype' or the 'syntax' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002245 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002246 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002247did_set_filetype_or_syntax(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002248{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002249 char_u **varp = (char_u **)args->os_varp;
2250
2251 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002252 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002253
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002254 args->os_value_changed = STRCMP(args->os_oldval.string, *varp) != 0;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002255
2256 // Since we check the value, there is no need to set P_INSECURE,
2257 // even when the value comes from a modeline.
2258 args->os_value_checked = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002259
2260 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002261}
2262
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002263#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002264/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002265 * The 'foldclose' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002266 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002267 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002268did_set_foldclose(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002269{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002270 return did_set_opt_strings(p_fcl, p_fcl_values, TRUE);
2271}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002272
2273 int
2274expand_set_foldclose(optexpand_T *args, int *numMatches, char_u ***matches)
2275{
2276 return expand_set_opt_string(
2277 args,
2278 p_fcl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002279 ARRAY_LENGTH(p_fcl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002280 numMatches,
2281 matches);
2282}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002283#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002284
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002285#if (defined(FEAT_EVAL) && defined(FEAT_FOLDING)) || defined(PROTO)
2286/*
2287 * The 'foldexpr' option is changed.
2288 */
2289 char *
2290did_set_foldexpr(optset_T *args)
2291{
2292 (void)did_set_optexpr(args);
2293 if (foldmethodIsExpr(curwin))
2294 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002295 return NULL;
2296}
2297#endif
2298
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002299#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002300/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002301 * The 'foldignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002302 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002303 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002304did_set_foldignore(optset_T *args UNUSED)
2305{
2306 if (foldmethodIsIndent(curwin))
2307 foldUpdateAll(curwin);
2308 return NULL;
2309}
2310
2311/*
2312 * The 'foldmarker' option is changed.
2313 */
2314 char *
2315did_set_foldmarker(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002316{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002317 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002318 char_u *p;
2319
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002320 p = vim_strchr(*varp, ',');
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002321 if (p == NULL)
2322 return e_comma_required;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002323 else if (p == *varp || p[1] == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002324 return e_invalid_argument;
2325 else if (foldmethodIsMarker(curwin))
2326 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002327
2328 return NULL;
2329}
2330
2331/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002332 * The 'foldmethod' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002333 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002334 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002335did_set_foldmethod(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002336{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002337 char_u **varp = (char_u **)args->os_varp;
2338
Milly142cad12024-10-22 22:11:51 +02002339 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK || **varp == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002340 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002341
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002342 foldUpdateAll(curwin);
2343 if (foldmethodIsDiff(curwin))
2344 newFoldLevel();
2345 return NULL;
2346}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002347
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002348 int
2349expand_set_foldmethod(optexpand_T *args, int *numMatches, char_u ***matches)
2350{
2351 return expand_set_opt_string(
2352 args,
2353 p_fdm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002354 ARRAY_LENGTH(p_fdm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002355 numMatches,
2356 matches);
2357}
2358
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002359/*
2360 * The 'foldopen' option is changed.
2361 */
2362 char *
2363did_set_foldopen(optset_T *args UNUSED)
2364{
2365 return did_set_opt_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
2366}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002367
2368 int
2369expand_set_foldopen(optexpand_T *args, int *numMatches, char_u ***matches)
2370{
2371 return expand_set_opt_string(
2372 args,
2373 p_fdo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002374 ARRAY_LENGTH(p_fdo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002375 numMatches,
2376 matches);
2377}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002378#endif
2379
2380/*
2381 * The 'formatoptions' option is changed.
2382 */
2383 char *
2384did_set_formatoptions(optset_T *args)
2385{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002386 char_u **varp = (char_u **)args->os_varp;
2387
Christian Brabandtb39b2402023-11-29 11:34:05 +01002388 return did_set_option_listflag(*varp, (char_u *)FO_ALL, args->os_errbuf,
2389 args->os_errbuflen);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002390}
2391
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002392 int
2393expand_set_formatoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2394{
2395 return expand_set_opt_listflag(args, (char_u*)FO_ALL, numMatches, matches);
2396}
2397
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002398#if defined(CURSOR_SHAPE) || defined(PROTO)
2399/*
2400 * The 'guicursor' option is changed.
2401 */
2402 char *
2403did_set_guicursor(optset_T *args UNUSED)
2404{
2405 return parse_shape_opt(SHAPE_CURSOR);
2406}
2407#endif
2408
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002409#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002410/*
2411 * The 'guifont' option is changed.
2412 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002413 char *
2414did_set_guifont(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002415{
2416 char_u *p;
2417 char *errmsg = NULL;
2418
2419 if (gui.in_use)
2420 {
2421 p = p_guifont;
2422# if defined(FEAT_GUI_GTK)
2423 // Put up a font dialog and let the user select a new value.
2424 // If this is cancelled go back to the old value but don't
2425 // give an error message.
2426 if (STRCMP(p, "*") == 0)
2427 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002428 p = gui_mch_font_dialog(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002429 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002430 p_guifont = (p != NULL) ? p : vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002431 }
2432# endif
2433 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
2434 {
2435# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
2436 if (STRCMP(p_guifont, "*") == 0)
2437 {
2438 // Dialog was cancelled: Keep the old value without giving
2439 // an error message.
2440 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002441 p_guifont = vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002442 }
2443 else
2444# endif
2445 errmsg = e_invalid_fonts;
2446 }
2447 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002448
2449 return errmsg;
2450}
2451
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002452/*
2453 * Expand the 'guifont' option. Only when GUI is being used. Each platform has
2454 * specific behaviors.
2455 */
2456 int
2457expand_set_guifont(optexpand_T *args, int *numMatches, char_u ***matches)
2458{
2459 if (!gui.in_use)
2460 return FAIL;
2461
2462# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
2463 char_u **varp = (char_u **)args->oe_varp;
2464 int wide = (varp == &p_guifontwide);
2465
2466 return expand_set_opt_callback(
2467 args, gui_mch_expand_font, &wide, numMatches, matches);
2468# else
2469 return FAIL;
2470# endif
2471}
2472
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002473# if defined(FEAT_XFONTSET) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002474/*
2475 * The 'guifontset' option is changed.
2476 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002477 char *
2478did_set_guifontset(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002479{
2480 char *errmsg = NULL;
2481
2482 if (STRCMP(p_guifontset, "*") == 0)
2483 errmsg = e_cant_select_fontset;
2484 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
2485 errmsg = e_invalid_fontset;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002486
2487 return errmsg;
2488}
2489# endif
2490
2491/*
2492 * The 'guifontwide' option is changed.
2493 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002494 char *
2495did_set_guifontwide(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002496{
2497 char *errmsg = NULL;
2498
2499 if (STRCMP(p_guifontwide, "*") == 0)
2500 errmsg = e_cant_select_wide_font;
2501 else if (gui_get_wide_font() == FAIL)
2502 errmsg = e_invalid_wide_font;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002503
2504 return errmsg;
2505}
2506#endif
2507
Erik S. V. Jansson8b1e7492024-02-24 14:26:52 +01002508#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002509/*
2510 * The 'guiligatures' option is changed.
2511 */
2512 char *
2513did_set_guiligatures(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002514{
2515 gui_set_ligatures();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002516 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002517}
2518#endif
2519
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002520#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002521/*
2522 * The 'guioptions' option is changed.
2523 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002524 char *
2525did_set_guioptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002526{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002527 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002528 char *errmsg;
2529
Christian Brabandtb39b2402023-11-29 11:34:05 +01002530 errmsg = did_set_option_listflag(*varp, (char_u *)GO_ALL, args->os_errbuf,
2531 args->os_errbuflen);
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002532 if (errmsg != NULL)
2533 return errmsg;
2534
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002535 gui_init_which_components(args->os_oldval.string);
2536 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002537}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002538
2539 int
2540expand_set_guioptions(optexpand_T *args, int *numMatches, char_u ***matches)
2541{
2542 return expand_set_opt_listflag(args, (char_u*)GO_ALL, numMatches, matches);
2543}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002544#endif
2545
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002546#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002547/*
2548 * The 'guitablabel' option is changed.
2549 */
2550 char *
2551did_set_guitablabel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002552{
2553 redraw_tabline = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002554 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002555}
2556#endif
2557
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002558/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002559 * The 'helpfile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002560 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002561 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002562did_set_helpfile(optset_T *args UNUSED)
2563{
2564 // May compute new values for $VIM and $VIMRUNTIME
2565 if (didset_vim)
2566 vim_unsetenv_ext((char_u *)"VIM");
2567 if (didset_vimruntime)
2568 vim_unsetenv_ext((char_u *)"VIMRUNTIME");
2569 return NULL;
2570}
2571
2572#if defined(FEAT_MULTI_LANG) || defined(PROTO)
2573/*
2574 * The 'helplang' option is changed.
2575 */
2576 char *
2577did_set_helplang(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002578{
2579 char *errmsg = NULL;
2580
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002581 // Check for "", "ab", "ab,cd", etc.
2582 for (char_u *s = p_hlg; *s != NUL; s += 3)
2583 {
2584 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
2585 {
2586 errmsg = e_invalid_argument;
2587 break;
2588 }
2589 if (s[2] == NUL)
2590 break;
2591 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002592
2593 return errmsg;
2594}
2595#endif
2596
2597/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002598 * The 'highlight' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002599 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002600 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002601did_set_highlight(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002602{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002603 if (highlight_changed() == FAIL)
2604 return e_invalid_argument; // invalid flags
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002605
2606 return NULL;
2607}
2608
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002609/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002610 * Expand 'highlight' option.
2611 */
2612 int
2613expand_set_highlight(optexpand_T *args, int *numMatches, char_u ***matches)
2614{
2615 char_u *p;
2616 expand_T *xp = args->oe_xp;
2617 static char_u hl_flags[HLF_COUNT] = HL_FLAGS;
Christian Brabandt3f168ec2023-10-02 23:21:11 +02002618 size_t i;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002619 int count = 0;
2620
2621 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2622 {
2623 // Right after a ':', meaning we just return all highlight names.
2624 return expand_set_opt_generic(
2625 args,
2626 get_highlight_name,
2627 numMatches,
2628 matches);
2629 }
2630
2631 if (*xp->xp_pattern == NUL)
2632 {
2633 // At beginning of a comma-separated list. Return the specific list of
2634 // supported occasions.
2635 *matches = ALLOC_MULT(char_u *, HLF_COUNT + 1);
2636 if (*matches == NULL)
2637 return FAIL;
2638
2639 // We still want to return the full option if it's requested.
2640 if (args->oe_include_orig_val)
2641 {
2642 p = vim_strsave(args->oe_opt_value);
2643 if (p == NULL)
2644 {
2645 VIM_CLEAR(*matches);
2646 return FAIL;
2647 }
2648 (*matches)[count++] = p;
2649 }
2650
2651 for (i = 0; i < HLF_COUNT; i++)
2652 {
2653 p = vim_strnsave(&hl_flags[i], 1);
2654 if (p == NULL)
2655 {
2656 if (count == 0)
2657 {
2658 VIM_CLEAR(*matches);
2659 return FAIL;
2660 }
2661 else
2662 break;
2663 }
2664 (*matches)[count++] = p;
2665 }
2666
2667 if (count == 0)
2668 {
2669 VIM_CLEAR(*matches);
2670 return FAIL;
2671 }
2672 *numMatches = count;
2673 return OK;
2674 }
2675
2676 // We are after the initial character (which indicates the occasion). We
2677 // already made sure we are not matching after a ':' above, so now we want
2678 // to match against display mode modifiers.
2679 // Since the xp_pattern starts from the beginning, we need to include it in
2680 // the returned match.
2681
2682 // Note: Keep this in sync with highlight_changed()
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002683 static char_u p_hl_mode_values[] =
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002684 {':', 'b', 'i', '-', 'n', 'r', 's', 'u', 'c', '2', 'd', '=', 't'};
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002685 size_t num_hl_modes = ARRAY_LENGTH(p_hl_mode_values);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002686
2687 *matches = ALLOC_MULT(char_u *, num_hl_modes);
2688 if (*matches == NULL)
2689 return FAIL;
2690
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002691 int pattern_len = xp->xp_pattern_len;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002692
2693 for (i = 0; i < num_hl_modes; i++)
2694 {
2695 // Don't allow duplicates as these are unique flags
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002696 char_u *dup = vim_strchr(xp->xp_pattern + 1, p_hl_mode_values[i]);
2697 if (dup != NULL && (int)(dup - xp->xp_pattern) < pattern_len)
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002698 continue;
2699
2700 // ':' only works by itself, not with other flags.
2701 if (pattern_len > 1 && p_hl_mode_values[i] == ':')
2702 continue;
2703
2704 p = vim_strnsave(xp->xp_pattern, pattern_len + 1);
2705 if (p == NULL)
2706 {
2707 if (i == 0)
2708 {
2709 VIM_CLEAR(*matches);
2710 return FAIL;
2711 }
2712 else
2713 break;
2714 }
2715 p[pattern_len] = p_hl_mode_values[i];
2716 p[pattern_len + 1] = NUL;
2717 (*matches)[count++] = p;
2718 }
2719 if (count == 0)
2720 {
2721 VIM_CLEAR(*matches);
2722 return FAIL;
2723 }
2724 *numMatches = count;
2725
2726 return OK;
2727}
2728
2729/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002730 * The 'titlestring' or the 'iconstring' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002731 */
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002732 static char *
2733parse_titleiconstring(optset_T *args UNUSED, int flagval UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002734{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002735#ifdef FEAT_STL_OPT
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002736 char_u **varp = (char_u **)args->os_varp;
2737
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002738 // NULL => statusline syntax
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002739 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002740 stl_syntax |= flagval;
2741 else
2742 stl_syntax &= ~flagval;
2743#endif
2744 did_set_title();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002745
2746 return NULL;
2747}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002748
2749/*
2750 * The 'iconstring' option is changed.
2751 */
2752 char *
2753did_set_iconstring(optset_T *args)
2754{
2755 int flagval = 0;
2756
2757#ifdef FEAT_STL_OPT
2758 flagval = STL_IN_ICON;
2759#endif
2760
2761 return parse_titleiconstring(args, flagval);
2762}
2763
2764#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
2765/*
2766 * The 'imactivatekey' option is changed.
2767 */
2768 char *
2769did_set_imactivatekey(optset_T *args UNUSED)
2770{
2771 if (!im_xim_isvalid_imactivate())
2772 return e_invalid_argument;
2773 return NULL;
2774}
2775#endif
2776
2777/*
Milly5e7a6a42024-10-22 22:27:19 +02002778 * The 'iskeyword' option is changed.
2779 */
2780 char *
2781did_set_iskeyword(optset_T *args)
2782{
2783 char_u **varp = (char_u **)args->os_varp;
2784
2785 if (varp == &p_isk) // only check for global-value
2786 {
2787 if (check_isopt(*varp) == FAIL)
2788 return e_invalid_argument;
2789 }
2790 else // fallthrough for local-value
2791 return did_set_isopt(args);
2792
2793 return NULL;
2794}
2795
2796/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002797 * The 'isident' or the 'iskeyword' or the 'isprint' or the 'isfname' option is
2798 * changed.
2799 */
2800 char *
2801did_set_isopt(optset_T *args)
2802{
Milly5e7a6a42024-10-22 22:27:19 +02002803 // 'isident', 'iskeyword', 'isprint' or 'isfname' option: refill g_chartab[]
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002804 // If the new option is invalid, use old value.
2805 // 'lisp' option: refill g_chartab[] for '-' char.
2806 if (init_chartab() == FAIL)
2807 {
2808 args->os_restore_chartab = TRUE;// need to restore the chartab.
2809 return e_invalid_argument; // error in value
2810 }
2811
2812 return NULL;
2813}
2814
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002815/*
2816 * The 'jumpoptions' option is changed.
2817 */
2818 char *
Yegappan Lakshmanan1926ae42023-09-21 16:36:28 +02002819did_set_jumpoptions(optset_T *args UNUSED)
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002820{
2821 if (opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE) != OK)
2822 return e_invalid_argument;
2823
2824 return NULL;
2825}
2826
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002827 int
2828expand_set_jumpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2829{
2830 return expand_set_opt_string(
2831 args,
2832 p_jop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002833 ARRAY_LENGTH(p_jop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002834 numMatches,
2835 matches);
2836}
2837
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002838#if defined(FEAT_KEYMAP) || defined(PROTO)
2839/*
2840 * The 'keymap' option is changed.
2841 */
2842 char *
2843did_set_keymap(optset_T *args)
2844{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002845 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002846 char *errmsg = NULL;
2847
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002848 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002849 errmsg = e_invalid_argument;
2850 else
2851 {
2852 int secure_save = secure;
2853
2854 // Reset the secure flag, since the value of 'keymap' has
2855 // been checked to be safe.
2856 secure = 0;
2857
2858 // load or unload key mapping tables
2859 errmsg = keymap_init();
2860
2861 secure = secure_save;
2862
2863 // Since we check the value, there is no need to set P_INSECURE,
2864 // even when the value comes from a modeline.
2865 args->os_value_checked = TRUE;
2866 }
2867
2868 if (errmsg == NULL)
2869 {
2870 if (*curbuf->b_p_keymap != NUL)
2871 {
2872 // Installed a new keymap, switch on using it.
2873 curbuf->b_p_iminsert = B_IMODE_LMAP;
2874 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
2875 curbuf->b_p_imsearch = B_IMODE_LMAP;
2876 }
2877 else
2878 {
2879 // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
2880 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
2881 curbuf->b_p_iminsert = B_IMODE_NONE;
2882 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
2883 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
2884 }
2885 if ((args->os_flags & OPT_LOCAL) == 0)
2886 {
2887 set_iminsert_global();
2888 set_imsearch_global();
2889 }
2890 status_redraw_curbuf();
2891 }
2892
2893 return errmsg;
2894}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002895#endif
2896
2897/*
2898 * The 'keymodel' option is changed.
2899 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002900 char *
2901did_set_keymodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002902{
2903 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
2904 return e_invalid_argument;
2905
2906 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
2907 km_startsel = (vim_strchr(p_km, 'a') != NULL);
2908 return NULL;
2909}
2910
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002911 int
2912expand_set_keymodel(optexpand_T *args, int *numMatches, char_u ***matches)
2913{
2914 return expand_set_opt_string(
2915 args,
2916 p_km_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002917 ARRAY_LENGTH(p_km_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002918 numMatches,
2919 matches);
2920}
2921
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002922/*
2923 * The 'keyprotocol' option is changed.
2924 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002925 char *
2926did_set_keyprotocol(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002927{
Gregory Anders3695d0e2023-09-29 20:17:20 +02002928 char_u *term = T_NAME;
2929 keyprot_T kpc = match_keyprotocol(term);
2930 if (kpc == KEYPROTOCOL_FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002931 return e_invalid_argument;
2932
Gregory Anders3695d0e2023-09-29 20:17:20 +02002933 apply_keyprotocol(term, kpc);
2934
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002935 return NULL;
2936}
2937
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002938 int
2939expand_set_keyprotocol(optexpand_T *args, int *numMatches, char_u ***matches)
2940{
2941 expand_T *xp = args->oe_xp;
2942 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2943 {
2944 // 'keyprotocol' only has well-defined terms for completion for the
2945 // protocol part after the colon.
2946 return expand_set_opt_string(
2947 args,
2948 p_kpc_protocol_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002949 ARRAY_LENGTH(p_kpc_protocol_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002950 numMatches,
2951 matches);
2952 }
2953 // Use expand_set_opt_string instead of returning FAIL so that we can
2954 // include the original value if args->oe_include_orig_val is set.
2955 static char *(empty[]) = {NULL};
2956 return expand_set_opt_string(args, empty, 0, numMatches, matches);
2957}
2958
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002959/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002960 * The 'lispoptions' option is changed.
2961 */
2962 char *
2963did_set_lispoptions(optset_T *args)
2964{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002965 char_u **varp = (char_u **)args->os_varp;
2966
2967 if (**varp != NUL
2968 && STRCMP(*varp, "expr:0") != 0
2969 && STRCMP(*varp, "expr:1") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002970 return e_invalid_argument;
2971
2972 return NULL;
2973}
2974
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002975 int
2976expand_set_lispoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2977{
2978 static char *(p_lop_values[]) = {"expr:0", "expr:1", NULL};
2979 return expand_set_opt_string(
2980 args,
2981 p_lop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002982 ARRAY_LENGTH(p_lop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002983 numMatches,
2984 matches);
2985}
2986
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002987/*
2988 * The 'matchpairs' option is changed.
2989 */
2990 char *
2991did_set_matchpairs(optset_T *args)
2992{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002993 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002994 char_u *p;
2995
2996 if (has_mbyte)
2997 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002998 for (p = *varp; *p != NUL; ++p)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002999 {
3000 int x2 = -1;
3001 int x3 = -1;
3002
3003 p += mb_ptr2len(p);
3004 if (*p != NUL)
3005 x2 = *p++;
3006 if (*p != NUL)
3007 {
3008 x3 = mb_ptr2char(p);
3009 p += mb_ptr2len(p);
3010 }
3011 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
3012 return e_invalid_argument;
3013 if (*p == NUL)
3014 break;
3015 }
3016 }
3017 else
3018 {
3019 // Check for "x:y,x:y"
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003020 for (p = *varp; *p != NUL; p += 4)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003021 {
3022 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
3023 return e_invalid_argument;
3024 if (p[3] == NUL)
3025 break;
3026 }
3027 }
3028
3029 return NULL;
3030}
3031
3032#if defined(FEAT_SPELL) || defined(PROTO)
3033/*
3034 * The 'mkspellmem' option is changed.
3035 */
3036 char *
3037did_set_mkspellmem(optset_T *args UNUSED)
3038{
3039 if (spell_check_msm() != OK)
3040 return e_invalid_argument;
3041
3042 return NULL;
3043}
3044#endif
3045
3046/*
3047 * The 'mouse' option is changed.
3048 */
3049 char *
3050did_set_mouse(optset_T *args)
3051{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003052 char_u **varp = (char_u **)args->os_varp;
3053
Christian Brabandtb39b2402023-11-29 11:34:05 +01003054 return did_set_option_listflag(*varp, (char_u *)MOUSE_ALL, args->os_errbuf,
3055 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003056}
3057
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003058 int
3059expand_set_mouse(optexpand_T *args, int *numMatches, char_u ***matches)
3060{
3061 return expand_set_opt_listflag(args, (char_u*)MOUSE_ALL, numMatches, matches);
3062}
3063
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003064/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003065 * The 'mousemodel' option is changed.
3066 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003067 char *
3068did_set_mousemodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003069{
3070 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
3071 return e_invalid_argument;
3072#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
Ken Takata18d0d292023-12-19 20:12:29 +01003073 else if (*p_mousem != *args->os_oldval.string)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003074 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
3075 // to create or delete the popup menus.
3076 gui_motif_update_mousemodel(root_menu);
3077#endif
3078
3079 return NULL;
3080}
3081
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003082 int
3083expand_set_mousemodel(optexpand_T *args, int *numMatches, char_u ***matches)
3084{
3085 return expand_set_opt_string(
3086 args,
3087 p_mousem_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003088 ARRAY_LENGTH(p_mousem_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003089 numMatches,
3090 matches);
3091}
3092
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003093#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3094 char *
3095did_set_mouseshape(optset_T *args UNUSED)
3096{
3097 char *errmsg = NULL;
3098
3099 errmsg = parse_shape_opt(SHAPE_MOUSE);
3100 update_mouseshape(-1);
3101
3102 return errmsg;
3103}
3104#endif
3105
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003106/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003107 * The 'nrformats' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003108 */
3109 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003110did_set_nrformats(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003111{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003112 char_u **varp = (char_u **)args->os_varp;
3113
3114 return did_set_opt_strings(*varp, p_nf_values, TRUE);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003115}
3116
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003117 int
3118expand_set_nrformats(optexpand_T *args, int *numMatches, char_u ***matches)
3119{
3120 return expand_set_opt_string(
3121 args,
3122 p_nf_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003123 ARRAY_LENGTH(p_nf_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003124 numMatches,
3125 matches);
3126}
3127
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003128#if defined(FEAT_EVAL) || defined(PROTO)
3129/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003130 * One of the '*expr' options is changed: 'balloonexpr', 'diffexpr',
3131 * 'foldexpr', 'foldtext', 'formatexpr', 'includeexpr', 'indentexpr',
3132 * 'patchexpr', 'printexpr' and 'charconvert'.
3133 *
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003134 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003135 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003136did_set_optexpr(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003137{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003138 char_u **varp = (char_u **)args->os_varp;
3139
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003140 // If the option value starts with <SID> or s:, then replace that with
3141 // the script identifier.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003142 char_u *name = get_scriptlocal_funcname(*varp);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003143 if (name != NULL)
3144 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003145 free_string_option(*varp);
3146 *varp = name;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003147 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003148
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003149 return NULL;
3150}
3151#endif
3152
3153/*
3154 * The 'pastetoggle' option is changed.
3155 */
3156 char *
3157did_set_pastetoggle(optset_T *args UNUSED)
3158{
3159 char_u *p;
3160
3161 // translate key codes like in a mapping
3162 if (*p_pt)
3163 {
zeertzjq7e0bae02023-08-11 23:15:38 +02003164 (void)replace_termcodes(p_pt, &p, 0,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003165 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
3166 if (p != NULL)
3167 {
3168 free_string_option(p_pt);
3169 p_pt = p;
3170 }
3171 }
3172
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003173 return NULL;
3174}
3175
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003176#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3177/*
3178 * The 'previewpopup' option is changed.
3179 */
3180 char *
3181did_set_previewpopup(optset_T *args UNUSED)
3182{
3183 if (parse_previewpopup(NULL) == FAIL)
3184 return e_invalid_argument;
3185
3186 return NULL;
3187}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003188
3189 int
3190expand_set_popupoption(optexpand_T *args, int *numMatches, char_u ***matches)
3191{
3192 expand_T *xp = args->oe_xp;
3193
3194 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3195 {
3196 // Within "highlight:"/"border:"/"align:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003197 int border_len = (int)STRLEN("border:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003198 if (xp->xp_pattern - args->oe_set_arg >= border_len &&
3199 STRNCMP(xp->xp_pattern - border_len, "border:", border_len) == 0)
3200 {
3201 return expand_set_opt_string(
3202 args,
3203 p_popup_option_border_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003204 ARRAY_LENGTH(p_popup_option_border_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003205 numMatches,
3206 matches);
3207 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003208 int align_len = (int)STRLEN("align:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003209 if (xp->xp_pattern - args->oe_set_arg >= align_len &&
3210 STRNCMP(xp->xp_pattern - align_len, "align:", align_len) == 0)
3211 {
3212 return expand_set_opt_string(
3213 args,
3214 p_popup_option_align_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003215 ARRAY_LENGTH(p_popup_option_align_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003216 numMatches,
3217 matches);
3218 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003219 int highlight_len = (int)STRLEN("highlight:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003220 if (xp->xp_pattern - args->oe_set_arg >= highlight_len &&
3221 STRNCMP(xp->xp_pattern - highlight_len, "highlight:", highlight_len) == 0)
3222 {
3223 // Return the list of all highlight names
3224 return expand_set_opt_generic(
3225 args,
3226 get_highlight_name,
3227 numMatches,
3228 matches);
3229 }
3230 return FAIL;
3231 }
3232
3233 return expand_set_opt_string(
3234 args,
3235 p_popup_option_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003236 ARRAY_LENGTH(p_popup_option_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003237 numMatches,
3238 matches);
3239}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003240#endif
3241
3242#if defined(FEAT_POSTSCRIPT) || defined(PROTO)
3243/*
3244 * The 'printencoding' option is changed.
3245 */
3246 char *
3247did_set_printencoding(optset_T *args UNUSED)
3248{
3249 char_u *s, *p;
3250
3251 // Canonize 'printencoding' if VIM standard one
3252 p = enc_canonize(p_penc);
3253 if (p != NULL)
3254 {
3255 vim_free(p_penc);
3256 p_penc = p;
3257 }
3258 else
3259 {
3260 // Ensure lower case and '-' for '_'
3261 for (s = p_penc; *s != NUL; s++)
3262 {
3263 if (*s == '_')
3264 *s = '-';
3265 else
3266 *s = TOLOWER_ASC(*s);
3267 }
3268 }
3269
3270 return NULL;
3271}
3272#endif
3273
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003274#if defined(FEAT_PRINTER) || defined(PROTO)
3275
3276 static char_u *
3277get_printoptions_names(expand_T *xp UNUSED, int idx)
3278{
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003279 if (idx >= (int)ARRAY_LENGTH(printer_opts))
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003280 return NULL;
3281 return (char_u*)printer_opts[idx].name;
3282}
3283
3284/*
3285 * Expand 'printoptions' option
3286 */
3287 int
3288expand_set_printoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3289{
3290 return expand_set_opt_generic(
3291 args,
3292 get_printoptions_names,
3293 numMatches,
3294 matches);
3295}
3296#endif
3297
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003298#if defined(FEAT_STL_OPT) || defined(PROTO)
3299/*
3300 * The 'statusline' or the 'tabline' or the 'rulerformat' option is changed.
3301 * "rulerformat" is TRUE if the 'rulerformat' option is changed.
3302 */
3303 static char *
3304parse_statustabline_rulerformat(optset_T *args, int rulerformat)
3305{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003306 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003307 char_u *s;
3308 char *errmsg = NULL;
3309 int wid;
3310
3311 if (rulerformat) // reset ru_wid first
3312 ru_wid = 0;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003313 s = *varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003314 if (rulerformat && *s == '%')
3315 {
3316 // set ru_wid if 'ruf' starts with "%99("
3317 if (*++s == '-') // ignore a '-'
3318 s++;
3319 wid = getdigits(&s);
3320 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
3321 ru_wid = wid;
3322 else
3323 errmsg = check_stl_option(p_ruf);
3324 }
3325 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
3326 else if (rulerformat || s[0] != '%' || s[1] != '!')
3327 errmsg = check_stl_option(s);
3328 if (rulerformat && errmsg == NULL)
3329 comp_col();
3330
3331 return errmsg;
3332}
3333#endif
3334
3335#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
3336/*
3337 * The 'renderoptions' option is changed.
3338 */
3339 char *
3340did_set_renderoptions(optset_T *args UNUSED)
3341{
3342 if (!gui_mch_set_rendering_options(p_rop))
3343 return e_invalid_argument;
3344
3345 return NULL;
3346}
3347#endif
3348
3349#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3350/*
3351 * The 'rightleftcmd' option is changed.
3352 */
3353 char *
3354did_set_rightleftcmd(optset_T *args)
3355{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003356 char_u **varp = (char_u **)args->os_varp;
3357
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003358 // Currently only "search" is a supported value.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003359 if (**varp != NUL && STRCMP(*varp, "search") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003360 return e_invalid_argument;
3361
3362 return NULL;
3363}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003364
3365 int
3366expand_set_rightleftcmd(optexpand_T *args, int *numMatches, char_u ***matches)
3367{
3368 static char *(p_rlc_values[]) = {"search", NULL};
3369 return expand_set_opt_string(
3370 args,
3371 p_rlc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003372 ARRAY_LENGTH(p_rlc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003373 numMatches,
3374 matches);
3375}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003376#endif
3377
3378#if defined(FEAT_STL_OPT) || defined(PROTO)
3379/*
3380 * The 'rulerformat' option is changed.
3381 */
3382 char *
3383did_set_rulerformat(optset_T *args)
3384{
3385 return parse_statustabline_rulerformat(args, TRUE);
3386}
3387#endif
3388
3389/*
3390 * The 'scrollopt' option is changed.
3391 */
3392 char *
3393did_set_scrollopt(optset_T *args UNUSED)
3394{
3395 return did_set_opt_strings(p_sbo, p_scbopt_values, TRUE);
3396}
3397
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003398 int
3399expand_set_scrollopt(optexpand_T *args, int *numMatches, char_u ***matches)
3400{
3401 return expand_set_opt_string(
3402 args,
3403 p_scbopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003404 ARRAY_LENGTH(p_scbopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003405 numMatches,
3406 matches);
3407}
3408
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003409/*
3410 * The 'selection' option is changed.
3411 */
3412 char *
3413did_set_selection(optset_T *args UNUSED)
3414{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003415 if (*p_sel == NUL || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003416 return e_invalid_argument;
3417
3418 return NULL;
3419}
3420
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003421 int
3422expand_set_selection(optexpand_T *args, int *numMatches, char_u ***matches)
3423{
3424 return expand_set_opt_string(
3425 args,
3426 p_sel_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003427 ARRAY_LENGTH(p_sel_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003428 numMatches,
3429 matches);
3430}
3431
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003432/*
3433 * The 'selectmode' option is changed.
3434 */
3435 char *
3436did_set_selectmode(optset_T *args UNUSED)
3437{
3438 return did_set_opt_strings(p_slm, p_slm_values, TRUE);
3439}
3440
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003441 int
3442expand_set_selectmode(optexpand_T *args, int *numMatches, char_u ***matches)
3443{
3444 return expand_set_opt_string(
3445 args,
3446 p_slm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003447 ARRAY_LENGTH(p_slm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003448 numMatches,
3449 matches);
3450}
3451
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003452#if defined(FEAT_SESSION) || defined(PROTO)
3453/*
3454 * The 'sessionoptions' option is changed.
3455 */
3456 char *
3457did_set_sessionoptions(optset_T *args)
3458{
3459 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
3460 return e_invalid_argument;
3461 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
3462 {
3463 // Don't allow both "sesdir" and "curdir".
3464 (void)opt_strings_flags(args->os_oldval.string, p_ssop_values,
3465 &ssop_flags, TRUE);
3466 return e_invalid_argument;
3467 }
3468
3469 return NULL;
3470}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003471
3472 int
3473expand_set_sessionoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3474{
3475 return expand_set_opt_string(
3476 args,
3477 p_ssop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003478 ARRAY_LENGTH(p_ssop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003479 numMatches,
3480 matches);
3481}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003482#endif
3483
3484/*
3485 * The 'shortmess' option is changed.
3486 */
3487 char *
3488did_set_shortmess(optset_T *args)
3489{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003490 char_u **varp = (char_u **)args->os_varp;
3491
Christian Brabandtb39b2402023-11-29 11:34:05 +01003492 return did_set_option_listflag(*varp, (char_u *)SHM_ALL, args->os_errbuf,
3493 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003494}
3495
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003496 int
3497expand_set_shortmess(optexpand_T *args, int *numMatches, char_u ***matches)
3498{
3499 return expand_set_opt_listflag(args, (char_u*)SHM_ALL, numMatches, matches);
3500}
3501
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003502#if defined(FEAT_LINEBREAK) || defined(PROTO)
3503/*
3504 * The 'showbreak' option is changed.
3505 */
3506 char *
3507did_set_showbreak(optset_T *args)
3508{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003509 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003510 char_u *s;
3511
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003512 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003513 {
3514 if (ptr2cells(s) != 1)
3515 return e_showbreak_contains_unprintable_or_wide_character;
3516 MB_PTR_ADV(s);
3517 }
3518
3519 return NULL;
3520}
3521#endif
3522
3523/*
3524 * The 'showcmdloc' option is changed.
3525 */
3526 char *
3527did_set_showcmdloc(optset_T *args UNUSED)
3528{
zeertzjqc27fcf42024-03-01 23:01:43 +01003529 char *errmsg = did_set_opt_strings(p_sloc, p_sloc_values, FALSE);
3530
3531 if (errmsg == NULL)
3532 comp_col();
3533
3534 return errmsg;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003535}
3536
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003537 int
3538expand_set_showcmdloc(optexpand_T *args, int *numMatches, char_u ***matches)
3539{
3540 return expand_set_opt_string(
3541 args,
3542 p_sloc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003543 ARRAY_LENGTH(p_sloc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003544 numMatches,
3545 matches);
3546}
3547
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003548#if defined(FEAT_SIGNS) || defined(PROTO)
3549/*
3550 * The 'signcolumn' option is changed.
3551 */
3552 char *
3553did_set_signcolumn(optset_T *args)
3554{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003555 char_u **varp = (char_u **)args->os_varp;
3556
3557 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003558 return e_invalid_argument;
3559 // When changing the 'signcolumn' to or from 'number', recompute the
3560 // width of the number column if 'number' or 'relativenumber' is set.
3561 if (((*args->os_oldval.string == 'n' && args->os_oldval.string[1] == 'u')
3562 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
3563 && (curwin->w_p_nu || curwin->w_p_rnu))
3564 curwin->w_nrwidth_line_count = 0;
3565
3566 return NULL;
3567}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003568
3569 int
3570expand_set_signcolumn(optexpand_T *args, int *numMatches, char_u ***matches)
3571{
3572 return expand_set_opt_string(
3573 args,
3574 p_scl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003575 ARRAY_LENGTH(p_scl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003576 numMatches,
3577 matches);
3578}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003579#endif
3580
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003581#if defined(FEAT_SPELL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003582/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003583 * The 'spellcapcheck' option is changed.
3584 */
3585 char *
3586did_set_spellcapcheck(optset_T *args UNUSED)
3587{
3588 // compile the regexp program.
3589 return compile_cap_prog(curwin->w_s);
3590}
3591
3592/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003593 * The 'spellfile' option is changed.
3594 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003595 char *
3596did_set_spellfile(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003597{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003598 char_u **varp = (char_u **)args->os_varp;
3599
3600 if (!valid_spellfile(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003601 return e_invalid_argument;
3602
3603 // If there is a window for this buffer in which 'spell' is set load the
3604 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003605 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003606}
3607
3608/*
3609 * The 'spell' option is changed.
3610 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003611 char *
3612did_set_spelllang(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003613{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003614 char_u **varp = (char_u **)args->os_varp;
3615
3616 if (!valid_spelllang(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003617 return e_invalid_argument;
3618
3619 // If there is a window for this buffer in which 'spell' is set load the
3620 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003621 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003622}
3623
3624/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003625 * The 'spelloptions' option is changed.
3626 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003627 char *
3628did_set_spelloptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003629{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003630 char_u **varp = (char_u **)args->os_varp;
3631
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003632 if (**varp != NUL && STRCMP(*varp, "camel") != 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003633 return e_invalid_argument;
3634
3635 return NULL;
3636}
3637
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003638 int
3639expand_set_spelloptions(optexpand_T *args, int *numMatches, char_u ***matches)
3640{
3641 static char *(p_spo_values[]) = {"camel", NULL};
3642 return expand_set_opt_string(
3643 args,
3644 p_spo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003645 ARRAY_LENGTH(p_spo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003646 numMatches,
3647 matches);
3648}
3649
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003650/*
3651 * The 'spellsuggest' option is changed.
3652 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003653 char *
3654did_set_spellsuggest(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003655{
3656 if (spell_check_sps() != OK)
3657 return e_invalid_argument;
3658
3659 return NULL;
3660}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003661
3662 int
3663expand_set_spellsuggest(optexpand_T *args, int *numMatches, char_u ***matches)
3664{
3665 return expand_set_opt_string(
3666 args,
3667 p_sps_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003668 ARRAY_LENGTH(p_sps_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003669 numMatches,
3670 matches);
3671}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003672#endif
3673
3674/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003675 * The 'splitkeep' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003676 */
3677 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003678did_set_splitkeep(optset_T *args UNUSED)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003679{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003680 return did_set_opt_strings(p_spk, p_spk_values, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003681}
3682
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003683 int
3684expand_set_splitkeep(optexpand_T *args, int *numMatches, char_u ***matches)
3685{
3686 return expand_set_opt_string(
3687 args,
3688 p_spk_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003689 ARRAY_LENGTH(p_spk_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003690 numMatches,
3691 matches);
3692}
3693
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00003694#if defined(FEAT_STL_OPT) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003695/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003696 * The 'statusline' option is changed.
3697 */
3698 char *
3699did_set_statusline(optset_T *args)
3700{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003701 return parse_statustabline_rulerformat(args, FALSE);
3702}
3703#endif
3704
3705/*
3706 * The 'swapsync' option is changed.
3707 */
3708 char *
3709did_set_swapsync(optset_T *args UNUSED)
3710{
3711 return did_set_opt_strings(p_sws, p_sws_values, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003712}
3713
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003714 int
3715expand_set_swapsync(optexpand_T *args, int *numMatches, char_u ***matches)
3716{
3717 return expand_set_opt_string(
3718 args,
3719 p_sws_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003720 ARRAY_LENGTH(p_sws_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003721 numMatches,
3722 matches);
3723}
3724
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003725/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003726 * The 'switchbuf' option is changed.
3727 */
3728 char *
3729did_set_switchbuf(optset_T *args UNUSED)
3730{
3731 return did_set_opt_flags(p_swb, p_swb_values, &swb_flags, TRUE);
3732}
3733
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003734 int
3735expand_set_switchbuf(optexpand_T *args, int *numMatches, char_u ***matches)
3736{
3737 return expand_set_opt_string(
3738 args,
3739 p_swb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003740 ARRAY_LENGTH(p_swb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003741 numMatches,
3742 matches);
3743}
3744
LemonBoy5247b0b2024-07-12 19:30:58 +02003745/*
3746 * The 'tabclose' option is changed.
3747 */
3748 char *
3749did_set_tabclose(optset_T *args UNUSED)
3750{
3751 return did_set_opt_flags(p_tcl, p_tcl_values, &tcl_flags, TRUE);
3752}
3753
3754 int
3755expand_set_tabclose(optexpand_T *args, int *numMatches, char_u ***matches)
3756{
3757 return expand_set_opt_string(
3758 args,
3759 p_tcl_values,
3760 ARRAY_LENGTH(p_tcl_values) - 1,
3761 numMatches,
3762 matches);
3763}
3764
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003765#if defined(FEAT_STL_OPT) || defined(PROTO)
3766/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003767 * The 'tabline' option is changed.
3768 */
3769 char *
3770did_set_tabline(optset_T *args)
3771{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003772 return parse_statustabline_rulerformat(args, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003773}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003774#endif
3775
3776/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003777 * The 'tagcase' option is changed.
3778 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003779 char *
3780did_set_tagcase(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003781{
3782 unsigned int *flags;
3783 char_u *p;
3784
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003785 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003786 {
3787 p = curbuf->b_p_tc;
3788 flags = &curbuf->b_tc_flags;
3789 }
3790 else
3791 {
3792 p = p_tc;
3793 flags = &tc_flags;
3794 }
3795
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003796 if ((args->os_flags & OPT_LOCAL) && *p == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003797 // make the local value empty: use the global value
3798 *flags = 0;
3799 else if (*p == NUL
3800 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
3801 return e_invalid_argument;
3802
3803 return NULL;
3804}
3805
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003806 int
3807expand_set_tagcase(optexpand_T *args, int *numMatches, char_u ***matches)
3808{
3809 return expand_set_opt_string(
3810 args,
3811 p_tc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003812 ARRAY_LENGTH(p_tc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003813 numMatches,
3814 matches);
3815}
3816
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003817/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003818 * The 'term' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003819 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003820 char *
3821did_set_term(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003822{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003823 if (T_NAME[0] == NUL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003824 return e_cannot_set_term_to_empty_string;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003825#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003826 if (gui.in_use)
3827 return e_cannot_change_term_in_GUI;
3828 if (term_is_gui(T_NAME))
3829 return e_use_gui_to_start_GUI;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003830#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003831 if (set_termname(T_NAME) == FAIL)
3832 return e_not_found_in_termcap;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003833
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003834 // Screen colors may have changed.
3835 redraw_later_clear();
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003836
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003837 return NULL;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003838}
3839
3840/*
3841 * Some terminal option (t_xxx) is changed
3842 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003843 char *
3844did_set_term_option(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003845{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003846 char_u **varp = (char_u **)args->os_varp;
3847
3848 if (!full_screen)
3849 return NULL;
3850
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003851 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
3852 if (varp == &T_CCO)
3853 {
3854 int colors = atoi((char *)T_CCO);
3855
3856 // Only reinitialize colors if t_Co value has really changed to
3857 // avoid expensive reload of colorscheme if t_Co is set to the
3858 // same value multiple times.
3859 if (colors != t_colors)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003860 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003861 t_colors = colors;
3862 if (t_colors <= 1)
3863 {
3864 vim_free(T_CCO);
3865 T_CCO = empty_option;
3866 }
3867#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3868 if (is_term_win32())
3869 {
3870 swap_tcap();
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003871 args->os_did_swaptcap = TRUE;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003872 }
3873#endif
3874 // We now have a different color setup, initialize it again.
3875 init_highlight(TRUE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003876 }
3877 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003878 ttest(FALSE);
3879 if (varp == &T_ME)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003880 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003881 out_str(T_ME);
3882 redraw_later(UPD_CLEAR);
3883#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3884 // Since t_me has been set, this probably means that the user
3885 // wants to use this as default colors. Need to reset default
3886 // background/foreground colors.
3887# ifdef VIMDLL
3888 if (!gui.in_use && !gui.starting)
3889# endif
3890 mch_set_normal_colors();
3891#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003892 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003893 if (varp == &T_BE && termcap_active)
3894 {
3895 MAY_WANT_TO_LOG_THIS;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003896
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003897 if (*T_BE == NUL)
3898 // When clearing t_BE we assume the user no longer wants
3899 // bracketed paste, thus disable it by writing t_BD.
3900 out_str(T_BD);
3901 else
3902 out_str(T_BE);
3903 }
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003904
3905 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003906}
3907
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003908#if defined(FEAT_TERMINAL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003909/*
3910 * The 'termwinkey' option is changed.
3911 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003912 char *
Milly94606f72024-10-22 22:07:52 +02003913did_set_termwinkey(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003914{
Milly94606f72024-10-22 22:07:52 +02003915 char_u **varp = (char_u **)args->os_varp;
3916
3917 if ((*varp)[0] != NUL && string_to_key(*varp, TRUE) == 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003918 return e_invalid_argument;
3919
3920 return NULL;
3921}
3922
3923/*
3924 * The 'termwinsize' option is changed.
3925 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003926 char *
Milly8be10aa2024-10-22 22:01:46 +02003927did_set_termwinsize(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003928{
Milly8be10aa2024-10-22 22:01:46 +02003929 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003930 char_u *p;
3931
Milly8be10aa2024-10-22 22:01:46 +02003932 if ((*varp)[0] == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003933 return NULL;
3934
Milly8be10aa2024-10-22 22:01:46 +02003935 p = skipdigits(*varp);
3936 if (p == *varp || (*p != 'x' && *p != '*') || *skipdigits(p + 1) != NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003937 return e_invalid_argument;
3938
3939 return NULL;
3940}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003941
3942# if defined(MSWIN) || defined(PROTO)
3943/*
3944 * The 'termwintype' option is changed.
3945 */
3946 char *
3947did_set_termwintype(optset_T *args UNUSED)
3948{
3949 return did_set_opt_strings(p_twt, p_twt_values, FALSE);
3950}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003951
3952 int
3953expand_set_termwintype(optexpand_T *args, int *numMatches, char_u ***matches)
3954{
3955 return expand_set_opt_string(
3956 args,
3957 p_twt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003958 ARRAY_LENGTH(p_twt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003959 numMatches,
3960 matches);
3961}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003962# endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003963#endif
3964
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003965/*
3966 * The 'titlestring' option is changed.
3967 */
3968 char *
3969did_set_titlestring(optset_T *args)
3970{
3971 int flagval = 0;
3972
3973#ifdef FEAT_STL_OPT
3974 flagval = STL_IN_TITLE;
3975#endif
3976 return parse_titleiconstring(args, flagval);
3977}
3978
3979#if (defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)) || defined(PROTO)
3980/*
3981 * The 'toolbar' option is changed.
3982 */
3983 char *
3984did_set_toolbar(optset_T *args UNUSED)
3985{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003986 if (opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags,
3987 TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003988 return e_invalid_argument;
3989
3990 out_flush();
3991 gui_mch_show_toolbar((toolbar_flags &
3992 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
3993 return NULL;
3994}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003995
3996 int
3997expand_set_toolbar(optexpand_T *args, int *numMatches, char_u ***matches)
3998{
3999 return expand_set_opt_string(
4000 args,
4001 p_toolbar_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004002 ARRAY_LENGTH(p_toolbar_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004003 numMatches,
4004 matches);
4005}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004006#endif
4007
4008#if (defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)) || defined(PROTO)
4009/*
4010 * The 'toolbariconsize' option is changed. GTK+ 2 only.
4011 */
4012 char *
4013did_set_toolbariconsize(optset_T *args UNUSED)
4014{
4015 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
4016 return e_invalid_argument;
4017
4018 out_flush();
4019 gui_mch_show_toolbar((toolbar_flags &
4020 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4021 return NULL;
4022}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004023
4024 int
4025expand_set_toolbariconsize(optexpand_T *args, int *numMatches, char_u ***matches)
4026{
4027 return expand_set_opt_string(
4028 args,
4029 p_tbis_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004030 ARRAY_LENGTH(p_tbis_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004031 numMatches,
4032 matches);
4033}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004034#endif
4035
4036#if defined(UNIX) || defined(VMS) || defined(PROTO)
4037/*
4038 * The 'ttymouse' option is changed.
4039 */
4040 char *
4041did_set_ttymouse(optset_T *args UNUSED)
4042{
4043 char *errmsg = NULL;
4044
4045 // Switch the mouse off before changing the escape sequences used for
4046 // that.
4047 mch_setmouse(FALSE);
4048 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
4049 errmsg = e_invalid_argument;
4050 else
4051 check_mouse_termcode();
4052 if (termcap_active)
4053 setmouse(); // may switch it on again
4054
4055 return errmsg;
4056}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004057
4058 int
4059expand_set_ttymouse(optexpand_T *args, int *numMatches, char_u ***matches)
4060{
4061 return expand_set_opt_string(
4062 args,
4063 p_ttym_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004064 ARRAY_LENGTH(p_ttym_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004065 numMatches,
4066 matches);
4067}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004068#endif
4069
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004070#if defined(FEAT_VARTABS) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004071/*
4072 * The 'varsofttabstop' option is changed.
4073 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004074 char *
4075did_set_varsofttabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004076{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004077 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004078 char_u *cp;
4079
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004080 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004081 VIM_CLEAR(curbuf->b_p_vsts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004082 else
4083 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004084 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004085 {
4086 if (vim_isdigit(*cp))
4087 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004088 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004089 continue;
4090 return e_invalid_argument;
4091 }
4092
4093 int *oldarray = curbuf->b_p_vsts_array;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004094 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004095 {
4096 if (oldarray)
4097 vim_free(oldarray);
4098 }
4099 else
4100 return e_invalid_argument;
4101 }
4102
4103 return NULL;
4104}
4105
4106/*
4107 * The 'vartabstop' option is changed.
4108 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004109 char *
4110did_set_vartabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004111{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004112 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004113 char_u *cp;
4114
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004115 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004116 VIM_CLEAR(curbuf->b_p_vts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004117 else
4118 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004119 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004120 {
4121 if (vim_isdigit(*cp))
4122 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004123 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004124 continue;
4125 return e_invalid_argument;
4126 }
4127
4128 int *oldarray = curbuf->b_p_vts_array;
4129
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004130 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004131 {
4132 vim_free(oldarray);
4133# ifdef FEAT_FOLDING
4134 if (foldmethodIsIndent(curwin))
4135 foldUpdateAll(curwin);
4136# endif
4137 }
4138 else
4139 return e_invalid_argument;
4140 }
4141
4142 return NULL;
4143}
4144#endif
4145
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004146/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004147 * The 'verbosefile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004148 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004149 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004150did_set_verbosefile(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004151{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004152 verbose_stop();
4153 if (*p_vfile != NUL && verbose_open() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004154 return e_invalid_argument;
4155
4156 return NULL;
4157}
4158
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004159#if defined(FEAT_SESSION) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004160/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004161 * The 'viewoptions' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004162 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004163 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004164did_set_viewoptions(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004165{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004166 return did_set_opt_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004167}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004168#endif
4169
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004170#if defined(FEAT_VIMINFO) || defined(PROTO)
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004171/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004172 * The 'viminfo' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004173 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004174 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004175did_set_viminfo(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004176{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004177 char_u *s;
4178 char *errmsg = NULL;
4179
4180 for (s = p_viminfo; *s;)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004181 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004182 // Check it's a valid character
4183 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
4184 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01004185 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004186 break;
4187 }
4188 if (*s == 'n') // name is always last one
4189 break;
4190 else if (*s == 'r') // skip until next ','
4191 {
4192 while (*++s && *s != ',')
4193 ;
4194 }
4195 else if (*s == '%')
4196 {
4197 // optional number
4198 while (vim_isdigit(*++s))
4199 ;
4200 }
4201 else if (*s == '!' || *s == 'h' || *s == 'c')
4202 ++s; // no extra chars
4203 else // must have a number
4204 {
4205 while (vim_isdigit(*++s))
4206 ;
4207
4208 if (!VIM_ISDIGIT(*(s - 1)))
4209 {
4210 if (args->os_errbuf != NULL)
4211 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01004212 vim_snprintf(args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004213 _(e_missing_number_after_angle_str_angle),
4214 transchar_byte(*(s - 1)));
4215 errmsg = args->os_errbuf;
4216 }
4217 else
4218 errmsg = "";
4219 break;
4220 }
4221 }
4222 if (*s == ',')
4223 ++s;
4224 else if (*s)
4225 {
4226 if (args->os_errbuf != NULL)
4227 errmsg = e_missing_comma;
4228 else
4229 errmsg = "";
4230 break;
4231 }
4232 }
4233 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
4234 errmsg = e_must_specify_a_value;
4235
4236 return errmsg;
4237}
4238#endif
4239
4240/*
4241 * The 'virtualedit' option is changed.
4242 */
4243 char *
4244did_set_virtualedit(optset_T *args)
4245{
4246 char_u *ve = p_ve;
4247 unsigned int *flags = &ve_flags;
4248
4249 if (args->os_flags & OPT_LOCAL)
4250 {
4251 ve = curwin->w_p_ve;
4252 flags = &curwin->w_ve_flags;
4253 }
4254
4255 if ((args->os_flags & OPT_LOCAL) && *ve == NUL)
4256 // make the local value empty: use the global value
4257 *flags = 0;
4258 else
4259 {
4260 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
4261 return e_invalid_argument;
4262 else if (STRCMP(ve, args->os_oldval.string) != 0)
4263 {
4264 // Recompute cursor position in case the new 've' setting
4265 // changes something.
4266 validate_virtcol();
4267 coladvance(curwin->w_virtcol);
4268 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004269 }
4270
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004271 return NULL;
4272}
4273
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004274 int
4275expand_set_virtualedit(optexpand_T *args, int *numMatches, char_u ***matches)
4276{
4277 return expand_set_opt_string(
4278 args,
4279 p_ve_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004280 ARRAY_LENGTH(p_ve_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004281 numMatches,
4282 matches);
4283}
4284
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004285/*
4286 * The 'whichwrap' option is changed.
4287 */
4288 char *
4289did_set_whichwrap(optset_T *args)
4290{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004291 char_u **varp = (char_u **)args->os_varp;
4292
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004293 // Add ',' to the list flags because 'whichwrap' is a flag
4294 // list that is comma-separated.
Christian Brabandtb39b2402023-11-29 11:34:05 +01004295 return did_set_option_listflag(*varp, (char_u *)(WW_ALL ","),
4296 args->os_errbuf, args->os_errbuflen);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004297}
4298
4299 int
4300expand_set_whichwrap(optexpand_T *args, int *numMatches, char_u ***matches)
4301{
4302 return expand_set_opt_listflag(args, (char_u*)WW_ALL, numMatches, matches);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004303}
4304
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004305/*
4306 * The 'wildmode' option is changed.
4307 */
4308 char *
4309did_set_wildmode(optset_T *args UNUSED)
4310{
4311 if (check_opt_wim() == FAIL)
4312 return e_invalid_argument;
4313 return NULL;
4314}
4315
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004316 int
4317expand_set_wildmode(optexpand_T *args, int *numMatches, char_u ***matches)
4318{
4319 return expand_set_opt_string(
4320 args,
4321 p_wim_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004322 ARRAY_LENGTH(p_wim_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004323 numMatches,
4324 matches);
4325}
4326
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004327/*
4328 * The 'wildoptions' option is changed.
4329 */
4330 char *
4331did_set_wildoptions(optset_T *args UNUSED)
4332{
4333 return did_set_opt_strings(p_wop, p_wop_values, TRUE);
4334}
4335
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004336 int
4337expand_set_wildoptions(optexpand_T *args, int *numMatches, char_u ***matches)
4338{
4339 return expand_set_opt_string(
4340 args,
4341 p_wop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004342 ARRAY_LENGTH(p_wop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004343 numMatches,
4344 matches);
4345}
4346
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004347#if defined(FEAT_WAK) || defined(PROTO)
4348/*
4349 * The 'winaltkeys' option is changed.
4350 */
4351 char *
4352did_set_winaltkeys(optset_T *args UNUSED)
4353{
4354 char *errmsg = NULL;
4355
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004356 if (*p_wak == NUL || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004357 errmsg = e_invalid_argument;
4358# ifdef FEAT_MENU
4359# if defined(FEAT_GUI_MOTIF)
4360 else if (gui.in_use)
4361 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4362# elif defined(FEAT_GUI_GTK)
4363 else if (gui.in_use)
4364 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4365# endif
4366# endif
4367 return errmsg;
4368}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004369
4370 int
4371expand_set_winaltkeys(optexpand_T *args, int *numMatches, char_u ***matches)
4372{
4373 return expand_set_opt_string(
4374 args,
4375 p_wak_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004376 ARRAY_LENGTH(p_wak_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004377 numMatches,
4378 matches);
4379}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004380#endif
4381
4382/*
4383 * The 'wincolor' option is changed.
4384 */
4385 char *
4386did_set_wincolor(optset_T *args UNUSED)
4387{
4388#ifdef FEAT_TERMINAL
4389 term_update_wincolor(curwin);
4390#endif
4391 return NULL;
4392}
4393
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004394 int
4395expand_set_wincolor(optexpand_T *args, int *numMatches, char_u ***matches)
4396{
4397 return expand_set_opt_generic(
4398 args,
4399 get_highlight_name,
4400 numMatches,
4401 matches);
4402}
4403
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004404#ifdef FEAT_SYN_HL
4405/*
4406 * When the 'syntax' option is set, load the syntax of that name.
4407 */
4408 static void
4409do_syntax_autocmd(int value_changed)
4410{
4411 static int syn_recursive = 0;
4412
4413 ++syn_recursive;
4414 // Only pass TRUE for "force" when the value changed or not used
4415 // recursively, to avoid endless recurrence.
4416 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
4417 value_changed || syn_recursive == 1, curbuf);
4418 curbuf->b_flags |= BF_SYN_SET;
4419 --syn_recursive;
4420}
4421#endif
4422
4423/*
4424 * When the 'filetype' option is set, trigger the FileType autocommand.
4425 */
4426 static void
4427do_filetype_autocmd(char_u **varp, int opt_flags, int value_changed)
4428{
4429 // Skip this when called from a modeline and the filetype was already set
4430 // to this value.
4431 if ((opt_flags & OPT_MODELINE) && !value_changed)
4432 return;
4433
4434 static int ft_recursive = 0;
4435 int secure_save = secure;
4436
4437 // Reset the secure flag, since the value of 'filetype' has
4438 // been checked to be safe.
4439 secure = 0;
4440
4441 ++ft_recursive;
zeertzjq5bf6c212024-03-31 18:41:27 +02004442 curbuf->b_did_filetype = TRUE;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004443 // Only pass TRUE for "force" when the value changed or not
4444 // used recursively, to avoid endless recurrence.
4445 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
4446 value_changed || ft_recursive == 1, curbuf);
4447 --ft_recursive;
4448 // Just in case the old "curbuf" is now invalid.
4449 if (varp != &(curbuf->b_p_ft))
4450 varp = NULL;
4451
4452 secure = secure_save;
4453}
4454
4455#ifdef FEAT_SPELL
4456/*
4457 * When the 'spelllang' option is set, source the spell/LANG.vim file in
4458 * 'runtimepath'.
4459 */
4460 static void
4461do_spelllang_source(void)
4462{
4463 char_u fname[200];
4464 char_u *p;
4465 char_u *q = curwin->w_s->b_p_spl;
4466
4467 // Skip the first name if it is "cjk".
4468 if (STRNCMP(q, "cjk,", 4) == 0)
4469 q += 4;
4470
4471 // They could set 'spellcapcheck' depending on the language. Use the first
4472 // name in 'spelllang' up to '_region' or '.encoding'.
4473 for (p = q; *p != NUL; ++p)
4474 if (!ASCII_ISALNUM(*p) && *p != '-')
4475 break;
4476 if (p > q)
4477 {
4478 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
4479 (int)(p - q), q);
4480 source_runtime(fname, DIP_ALL);
4481 }
4482}
4483#endif
4484
4485/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004486 * Handle string options that need some action to perform when changed.
zeertzjqf6782732022-07-27 18:26:03 +01004487 * The new value must be allocated.
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004488 * Returns NULL for success, or an untranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +02004489 */
4490 char *
4491did_set_string_option(
4492 int opt_idx, // index in options[] table
4493 char_u **varp, // pointer to the option variable
Bram Moolenaardac13472019-09-16 21:06:21 +02004494 char_u *oldval, // previous value of the option
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004495 char_u *value, // new value of the option
Bram Moolenaardac13472019-09-16 21:06:21 +02004496 char *errbuf, // buffer for errors, or NULL
Mike Williams620f0112023-12-05 15:36:06 +01004497 size_t errbuflen, // length of error buffer
Bram Moolenaardac13472019-09-16 21:06:21 +02004498 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004499 set_op_T op, // OP_ADDING/OP_PREPENDING/OP_REMOVING
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004500 int *value_checked) // value was checked to be safe, no
Bram Moolenaardac13472019-09-16 21:06:21 +02004501 // need to set P_INSECURE
4502{
4503 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +02004504 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004505 opt_did_set_cb_T did_set_cb = get_option_did_set_cb(opt_idx);
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004506 optset_T args;
4507
4508 // 'ttytype' is an alias for 'term'. Both 'term' and 'ttytype' point to
4509 // T_NAME. If 'term' or 'ttytype' is modified, then use the index for the
4510 // 'term' option. Only set the P_ALLOCED flag on 'term'.
4511 if (varp == &T_NAME)
4512 {
4513 opt_idx = findoption((char_u *)"term");
4514 if (opt_idx >= 0)
4515 {
4516 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
4517 did_set_cb = get_option_did_set_cb(opt_idx);
4518 }
4519 }
4520
4521 CLEAR_FIELD(args);
Bram Moolenaardac13472019-09-16 21:06:21 +02004522
Bram Moolenaardac13472019-09-16 21:06:21 +02004523 // Disallow changing some options from secure mode
4524 if ((secure
4525#ifdef HAVE_SANDBOX
4526 || sandbox != 0
4527#endif
4528 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00004529 errmsg = e_not_allowed_here;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004530 // Check for a "normal" directory or file name in some options.
4531 else if (check_illegal_path_names(opt_idx, varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004532 errmsg = e_invalid_argument;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004533 else if (did_set_cb != NULL)
4534 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004535 args.os_varp = (char_u *)varp;
4536 args.os_idx = opt_idx;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004537 args.os_flags = opt_flags;
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004538 args.os_op = op;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004539 args.os_oldval.string = oldval;
4540 args.os_newval.string = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004541 args.os_errbuf = errbuf;
Christian Brabandtb39b2402023-11-29 11:34:05 +01004542 args.os_errbuflen = errbuflen;
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004543 // Invoke the option specific callback function to validate and apply
4544 // the new option value.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004545 errmsg = did_set_cb(&args);
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004546
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004547 // The 'keymap', 'filetype' and 'syntax' option callback functions
4548 // may change the os_value_checked field.
4549 *value_checked = args.os_value_checked;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004550 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004551
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004552 // If an error is detected, restore the previous value.
Bram Moolenaardac13472019-09-16 21:06:21 +02004553 if (errmsg != NULL)
4554 {
zeertzjqf6782732022-07-27 18:26:03 +01004555 free_string_option(*varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02004556 *varp = oldval;
4557 // When resetting some values, need to act on it.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004558 if (args.os_restore_chartab)
Bram Moolenaardac13472019-09-16 21:06:21 +02004559 (void)init_chartab();
4560 if (varp == &p_hl)
4561 (void)highlight_changed();
4562 }
4563 else
4564 {
4565#ifdef FEAT_EVAL
4566 // Remember where the option was set.
4567 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4568#endif
4569 // Free string options that are in allocated memory.
4570 // Use "free_oldval", because recursiveness may change the flags under
4571 // our fingers (esp. init_highlight()).
4572 if (free_oldval)
4573 free_string_option(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01004574 set_option_flag(opt_idx, P_ALLOCED);
Bram Moolenaardac13472019-09-16 21:06:21 +02004575
4576 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
4577 && is_global_local_option(opt_idx))
4578 {
4579 // global option with local value set to use global value; free
4580 // the local value and make it empty
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004581 char_u *p = get_option_varp_scope(opt_idx, OPT_LOCAL);
Bram Moolenaardac13472019-09-16 21:06:21 +02004582 free_string_option(*(char_u **)p);
4583 *(char_u **)p = empty_option;
4584 }
4585
4586 // May set global value for local option.
4587 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
4588 set_string_option_global(opt_idx, varp);
4589
4590 // Trigger the autocommand only after setting the flags.
4591#ifdef FEAT_SYN_HL
Bram Moolenaardac13472019-09-16 21:06:21 +02004592 if (varp == &(curbuf->b_p_syn))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004593 do_syntax_autocmd(args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004594#endif
4595 else if (varp == &(curbuf->b_p_ft))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004596 do_filetype_autocmd(varp, opt_flags, args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004597#ifdef FEAT_SPELL
4598 if (varp == &(curwin->w_s->b_p_spl))
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004599 do_spelllang_source();
Bram Moolenaardac13472019-09-16 21:06:21 +02004600#endif
4601 }
4602
Bram Moolenaardac13472019-09-16 21:06:21 +02004603 if (varp == &p_mouse)
4604 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004605 if (*p_mouse == NUL)
4606 mch_setmouse(FALSE); // switch mouse off
4607 else
Bram Moolenaardac13472019-09-16 21:06:21 +02004608 setmouse(); // in case 'mouse' changed
4609 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004610
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004611#if defined(FEAT_LUA) || defined(PROTO)
4612 if (varp == &p_rtp)
4613 update_package_paths_in_lua();
4614#endif
4615
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004616#if defined(FEAT_LINEBREAK)
4617 // Changing Formatlistpattern when briopt includes the list setting:
4618 // redraw
4619 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
4620 && curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004621 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004622#endif
4623
Bram Moolenaardac13472019-09-16 21:06:21 +02004624 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004625 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0
4626 && (get_option_flags(opt_idx) & P_HLONLY) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02004627 curwin->w_set_curswant = TRUE;
4628
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004629 if ((opt_flags & OPT_NO_REDRAW) == 0)
4630 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004631#ifdef FEAT_GUI
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004632 // set when changing an option that only requires a redraw in the GUI
4633 int redraw_gui_only = FALSE;
4634
4635 if (varp == &p_go // 'guioptions'
4636 || varp == &p_guifont // 'guifont'
4637# ifdef FEAT_GUI_TABLINE
4638 || varp == &p_gtl // 'guitablabel'
4639 || varp == &p_gtt // 'guitabtooltip'
4640# endif
4641# ifdef FEAT_XFONTSET
4642 || varp == &p_guifontset // 'guifontset'
4643# endif
4644 || varp == &p_guifontwide // 'guifontwide'
Erik S. V. Jansson2f026382024-02-26 22:23:05 +01004645# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004646 || varp == &p_guiligatures // 'guiligatures'
4647# endif
4648 )
4649 redraw_gui_only = TRUE;
4650
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004651 // check redraw when it's not a GUI option or the GUI is active.
4652 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02004653#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004654 check_redraw(get_option_flags(opt_idx));
4655 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004656
4657#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004658 if (args.os_did_swaptcap)
Bram Moolenaardac13472019-09-16 21:06:21 +02004659 {
4660 set_termname((char_u *)"win32");
4661 init_highlight(TRUE, FALSE);
4662 }
4663#endif
4664
4665 return errmsg;
4666}
4667
4668/*
4669 * Check an option that can be a range of string values.
4670 *
4671 * Return OK for correct value, FAIL otherwise.
4672 * Empty is always OK.
4673 */
4674 static int
4675check_opt_strings(
4676 char_u *val,
4677 char **values,
4678 int list) // when TRUE: accept a list of values
4679{
4680 return opt_strings_flags(val, values, NULL, list);
4681}
4682
4683/*
4684 * Handle an option that can be a range of string values.
4685 * Set a flag in "*flagp" for each string present.
4686 *
4687 * Return OK for correct value, FAIL otherwise.
4688 * Empty is always OK.
4689 */
4690 static int
4691opt_strings_flags(
4692 char_u *val, // new value
4693 char **values, // array of valid string values
4694 unsigned *flagp,
4695 int list) // when TRUE: accept a list of values
4696{
4697 int i;
4698 int len;
4699 unsigned new_flags = 0;
4700
4701 while (*val)
4702 {
4703 for (i = 0; ; ++i)
4704 {
4705 if (values[i] == NULL) // val not found in values[]
4706 return FAIL;
4707
4708 len = (int)STRLEN(values[i]);
4709 if (STRNCMP(values[i], val, len) == 0
4710 && ((list && val[len] == ',') || val[len] == NUL))
4711 {
4712 val += len + (val[len] == ',');
4713 new_flags |= (1 << i);
4714 break; // check next item in val list
4715 }
4716 }
4717 }
4718 if (flagp != NULL)
4719 *flagp = new_flags;
4720
4721 return OK;
4722}
4723
4724/*
4725 * return OK if "p" is a valid fileformat name, FAIL otherwise.
4726 */
4727 int
4728check_ff_value(char_u *p)
4729{
4730 return check_opt_strings(p, p_ff_values, FALSE);
4731}
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004732
4733/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004734 * Save the actual shortmess Flags and clear them temporarily to avoid that
4735 * file messages overwrites any output from the following commands.
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004736 *
4737 * Caller must make sure to first call save_clear_shm_value() and then
4738 * restore_shm_value() exactly the same number of times.
4739 */
4740 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004741save_clear_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004742{
4743 if (STRLEN(p_shm) >= SHM_LEN)
4744 {
4745 iemsg(e_internal_error_shortmess_too_long);
4746 return;
4747 }
4748
4749 if (++set_shm_recursive == 1)
4750 {
4751 STRCPY(shm_buf, p_shm);
4752 set_option_value_give_err((char_u *)"shm", 0L, (char_u *)"", 0);
4753 }
4754}
4755
4756/*
4757 * Restore the shortmess Flags set from the save_clear_shm_value() function.
4758 */
4759 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004760restore_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004761{
4762 if (--set_shm_recursive == 0)
4763 {
4764 set_option_value_give_err((char_u *)"shm", 0L, shm_buf, 0);
4765 vim_memset(shm_buf, 0, SHM_LEN);
4766 }
4767}