blob: 17947460b851a89251c8d19aad66574a4047520e [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 *
Millya441a3e2024-10-22 22:43:01 +02001481did_set_colorcolumn(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001482{
Millya441a3e2024-10-22 22:43:01 +02001483 char_u **varp = (char_u **)args->os_varp;
1484
1485 return check_colorcolumn(*varp, varp == &curwin->w_p_cc ? curwin : NULL);
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001486}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001487#endif
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001488
1489/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001490 * The 'comments' option is changed.
1491 */
1492 char *
1493did_set_comments(optset_T *args)
1494{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001495 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001496 char_u *s;
1497 char *errmsg = NULL;
1498
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001499 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001500 {
1501 while (*s && *s != ':')
1502 {
1503 if (vim_strchr((char_u *)COM_ALL, *s) == NULL
1504 && !VIM_ISDIGIT(*s) && *s != '-')
1505 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01001506 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001507 break;
1508 }
1509 ++s;
1510 }
1511 if (*s++ == NUL)
1512 errmsg = e_missing_colon;
1513 else if (*s == ',' || *s == NUL)
1514 errmsg = e_zero_length_string;
1515 if (errmsg != NULL)
1516 break;
1517 while (*s && *s != ',')
1518 {
1519 if (*s == '\\' && s[1] != NUL)
1520 ++s;
1521 ++s;
1522 }
1523 s = skip_to_option_part(s);
1524 }
1525
1526 return errmsg;
1527}
1528
1529#if defined(FEAT_FOLDING) || defined(PROTO)
1530/*
1531 * The 'commentstring' option is changed.
1532 */
1533 char *
1534did_set_commentstring(optset_T *args)
1535{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001536 char_u **varp = (char_u **)args->os_varp;
1537
1538 if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001539 return e_commentstring_must_be_empty_or_contain_str;
1540
1541 return NULL;
1542}
1543#endif
1544
1545/*
1546 * The 'complete' option is changed.
1547 */
1548 char *
1549did_set_complete(optset_T *args)
1550{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001551 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001552 char_u *s;
1553
1554 // check if it is a valid value for 'complete' -- Acevedo
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001555 for (s = *varp; *s;)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001556 {
1557 while (*s == ',' || *s == ' ')
1558 s++;
1559 if (!*s)
1560 break;
1561 if (vim_strchr((char_u *)".wbuksid]tU", *s) == NULL)
Christian Brabandtb39b2402023-11-29 11:34:05 +01001562 return illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001563 if (*++s != NUL && *s != ',' && *s != ' ')
1564 {
1565 if (s[-1] == 'k' || s[-1] == 's')
1566 {
1567 // skip optional filename after 'k' and 's'
1568 while (*s && *s != ',' && *s != ' ')
1569 {
1570 if (*s == '\\' && s[1] != NUL)
1571 ++s;
1572 ++s;
1573 }
1574 }
1575 else
1576 {
1577 if (args->os_errbuf != NULL)
1578 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01001579 vim_snprintf((char *)args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001580 _(e_illegal_character_after_chr), *--s);
1581 return args->os_errbuf;
1582 }
1583 return "";
1584 }
1585 }
1586 }
1587
1588 return NULL;
1589}
1590
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001591 int
1592expand_set_complete(optexpand_T *args, int *numMatches, char_u ***matches)
1593{
1594 static char *(p_cpt_values[]) = {
1595 ".", "w", "b", "u", "k", "kspell", "s", "i", "d", "]", "t", "U",
1596 NULL};
1597 return expand_set_opt_string(
1598 args,
1599 p_cpt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001600 ARRAY_LENGTH(p_cpt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001601 numMatches,
1602 matches);
1603}
1604
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001605/*
1606 * The 'completeopt' option is changed.
1607 */
1608 char *
1609did_set_completeopt(optset_T *args UNUSED)
1610{
zeertzjq529b9ad2024-06-05 20:27:06 +02001611 char_u *cot = p_cot;
1612 unsigned *flags = &cot_flags;
1613
1614 if (args->os_flags & OPT_LOCAL)
1615 {
1616 cot = curbuf->b_p_cot;
1617 flags = &curbuf->b_cot_flags;
1618 }
1619
1620 if (check_opt_strings(cot, p_cot_values, TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001621 return e_invalid_argument;
1622
zeertzjq529b9ad2024-06-05 20:27:06 +02001623 if (opt_strings_flags(cot, p_cot_values, flags, TRUE) != OK)
1624 return e_invalid_argument;
1625
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001626 return NULL;
1627}
1628
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001629 int
1630expand_set_completeopt(optexpand_T *args, int *numMatches, char_u ***matches)
1631{
1632 return expand_set_opt_string(
1633 args,
1634 p_cot_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001635 ARRAY_LENGTH(p_cot_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001636 numMatches,
1637 matches);
1638}
1639
glepnir6a89c942024-10-01 20:32:12 +02001640/*
1641 * The 'completeitemalign' option is changed.
1642 */
1643 char *
1644did_set_completeitemalign(optset_T *args UNUSED)
1645{
1646 char_u *p = p_cia;
1647 unsigned new_cia_flags = 0;
1648 int seen[3] = { FALSE, FALSE, FALSE };
1649 int count = 0;
1650 char_u buf[10];
1651
1652 while (*p)
1653 {
1654 copy_option_part(&p, buf, sizeof(buf), ",");
1655 if (count >= 3)
1656 return e_invalid_argument;
1657
1658 if (STRCMP(buf, "abbr") == 0)
1659 {
1660 if (seen[CPT_ABBR])
1661 return e_invalid_argument;
1662 new_cia_flags = new_cia_flags * 10 + CPT_ABBR;
1663 seen[CPT_ABBR] = TRUE;
1664 count++;
1665 }
1666 else if (STRCMP(buf, "kind") == 0)
1667 {
1668 if (seen[CPT_KIND])
1669 return e_invalid_argument;
1670 new_cia_flags = new_cia_flags * 10 + CPT_KIND;
1671 seen[CPT_KIND] = TRUE;
1672 count++;
1673 }
1674 else if (STRCMP(buf, "menu") == 0)
1675 {
1676 if (seen[CPT_MENU])
1677 return e_invalid_argument;
1678 new_cia_flags = new_cia_flags * 10 + CPT_MENU;
1679 seen[CPT_MENU] = TRUE;
1680 count++;
1681 }
1682 else
1683 return e_invalid_argument;
1684 }
1685 if (new_cia_flags == 0 || count != 3)
1686 return e_invalid_argument;
1687
1688 cia_flags = new_cia_flags;
1689 return NULL;
1690}
1691
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001692#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1693/*
1694 * The 'completepopup' option is changed.
1695 */
1696 char *
1697did_set_completepopup(optset_T *args UNUSED)
1698{
1699 if (parse_completepopup(NULL) == FAIL)
1700 return e_invalid_argument;
1701
1702 popup_close_info();
1703 return NULL;
1704}
1705#endif
1706
1707#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1708/*
1709 * The 'completeslash' option is changed.
1710 */
1711 char *
1712did_set_completeslash(optset_T *args UNUSED)
1713{
1714 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1715 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
1716 return e_invalid_argument;
1717
1718 return NULL;
1719}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001720
1721 int
1722expand_set_completeslash(optexpand_T *args, int *numMatches, char_u ***matches)
1723{
1724 return expand_set_opt_string(
1725 args,
1726 p_csl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001727 ARRAY_LENGTH(p_csl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001728 numMatches,
1729 matches);
1730}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001731#endif
1732
1733#if defined(FEAT_CONCEAL) || defined(PROTO)
1734/*
1735 * The 'concealcursor' option is changed.
1736 */
1737 char *
1738did_set_concealcursor(optset_T *args)
1739{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001740 char_u **varp = (char_u **)args->os_varp;
1741
Christian Brabandtb39b2402023-11-29 11:34:05 +01001742 return did_set_option_listflag(*varp, (char_u *)COCU_ALL, args->os_errbuf,
1743 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001744}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001745
1746 int
1747expand_set_concealcursor(optexpand_T *args, int *numMatches, char_u ***matches)
1748{
1749 return expand_set_opt_listflag(args, (char_u*)COCU_ALL, numMatches, matches);
1750}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001751#endif
1752
1753/*
1754 * The 'cpoptions' option is changed.
1755 */
1756 char *
1757did_set_cpoptions(optset_T *args)
1758{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001759 char_u **varp = (char_u **)args->os_varp;
1760
Christian Brabandtb39b2402023-11-29 11:34:05 +01001761 return did_set_option_listflag(*varp, (char_u *)CPO_ALL, args->os_errbuf,
1762 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001763}
1764
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001765 int
1766expand_set_cpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
1767{
1768 return expand_set_opt_listflag(args, (char_u*)CPO_ALL, numMatches, matches);
1769}
1770
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001771#if defined(FEAT_CRYPT) || defined(PROTO)
1772/*
1773 * The 'cryptkey' option is changed.
1774 */
1775 char *
1776did_set_cryptkey(optset_T *args)
1777{
1778 // Make sure the ":set" command doesn't show the new value in the
1779 // history.
1780 remove_key_from_history();
1781
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02001782 if (args->os_op != OP_NONE)
1783 // Don't allow set+=/-=/^= as they can allow for substring guessing
1784 return e_invalid_argument;
1785
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001786 if (STRCMP(curbuf->b_p_key, args->os_oldval.string) != 0)
1787 {
1788 // Need to update the swapfile.
1789 ml_set_crypt_key(curbuf, args->os_oldval.string,
1790 *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
1791 changed_internal();
1792 }
Christian Brabandt19e6c4f2023-06-27 18:57:10 +01001793# ifdef FEAT_SODIUM
1794 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
1795 crypt_sodium_lock_key(args->os_newval.string);
1796# endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001797
1798 return NULL;
1799}
1800
1801/*
1802 * The 'cryptmethod' option is changed.
1803 */
1804 char *
1805did_set_cryptmethod(optset_T *args)
1806{
1807 char_u *p;
1808 char_u *s;
1809
1810 if (args->os_flags & OPT_LOCAL)
1811 p = curbuf->b_p_cm;
1812 else
1813 p = p_cm;
1814 if (check_opt_strings(p, p_cm_values, TRUE) != OK)
1815 return e_invalid_argument;
1816 else if (crypt_self_test() == FAIL)
1817 return e_invalid_argument;
1818
1819 // When setting the global value to empty, make it "zip".
1820 if (*p_cm == NUL)
1821 {
1822 free_string_option(p_cm);
1823 p_cm = vim_strsave((char_u *)"zip");
1824 }
1825 // When using ":set cm=name" the local value is going to be empty.
1826 // Do that here, otherwise the crypt functions will still use the
1827 // local value.
1828 if ((args->os_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1829 {
1830 free_string_option(curbuf->b_p_cm);
1831 curbuf->b_p_cm = empty_option;
1832 }
1833
1834 // Need to update the swapfile when the effective method changed.
1835 // Set "s" to the effective old value, "p" to the effective new
1836 // method and compare.
1837 if ((args->os_flags & OPT_LOCAL) && *args->os_oldval.string == NUL)
1838 s = p_cm; // was previously using the global value
1839 else
1840 s = args->os_oldval.string;
1841 if (*curbuf->b_p_cm == NUL)
1842 p = p_cm; // is now using the global value
1843 else
1844 p = curbuf->b_p_cm;
1845 if (STRCMP(s, p) != 0)
1846 ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1847
1848 // If the global value changes need to update the swapfile for all
1849 // buffers using that value.
1850 if ((args->os_flags & OPT_GLOBAL)
1851 && STRCMP(p_cm, args->os_oldval.string) != 0)
1852 {
1853 buf_T *buf;
1854
1855 FOR_ALL_BUFFERS(buf)
1856 if (buf != curbuf && *buf->b_p_cm == NUL)
1857 ml_set_crypt_key(buf, buf->b_p_key, args->os_oldval.string);
1858 }
1859 return NULL;
1860}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001861
1862 int
1863expand_set_cryptmethod(optexpand_T *args, int *numMatches, char_u ***matches)
1864{
1865 return expand_set_opt_string(
1866 args,
1867 p_cm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001868 ARRAY_LENGTH(p_cm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001869 numMatches,
1870 matches);
1871}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001872#endif
1873
1874#if (defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1875/*
1876 * The 'cscopequickfix' option is changed.
1877 */
1878 char *
1879did_set_cscopequickfix(optset_T *args UNUSED)
1880{
1881 char_u *p;
1882
1883 if (p_csqf == NULL)
1884 return NULL;
1885
1886 p = p_csqf;
1887 while (*p != NUL)
1888 {
1889 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
1890 || p[1] == NUL
1891 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
1892 || (p[2] != NUL && p[2] != ','))
1893 return e_invalid_argument;
1894 else if (p[2] == NUL)
1895 break;
1896 else
1897 p += 3;
1898 }
1899
1900 return NULL;
1901}
1902#endif
1903
1904#if defined(FEAT_SYN_HL) || defined(PROTO)
1905/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001906 * The 'cursorlineopt' option is changed.
1907 */
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001908 char *
1909did_set_cursorlineopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001910{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001911 char_u **varp = (char_u **)args->os_varp;
1912
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001913 // This could be changed to use opt_strings_flags() instead.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001914 if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001915 return e_invalid_argument;
1916
1917 return NULL;
1918}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001919
1920 int
1921expand_set_cursorlineopt(optexpand_T *args, int *numMatches, char_u ***matches)
1922{
1923 return expand_set_opt_string(
1924 args,
1925 p_culopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001926 ARRAY_LENGTH(p_culopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001927 numMatches,
1928 matches);
1929}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001930#endif
1931
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001932/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001933 * The 'debug' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001934 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001935 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001936did_set_debug(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001937{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001938 return did_set_opt_strings(p_debug, p_debug_values, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001939}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001940
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001941 int
1942expand_set_debug(optexpand_T *args, int *numMatches, char_u ***matches)
1943{
1944 return expand_set_opt_string(
1945 args,
1946 p_debug_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001947 ARRAY_LENGTH(p_debug_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001948 numMatches,
1949 matches);
1950}
1951
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001952#if defined(FEAT_DIFF) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001953/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001954 * The 'diffopt' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001955 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001956 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001957did_set_diffopt(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001958{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001959 if (diffopt_changed() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001960 return e_invalid_argument;
1961
1962 return NULL;
1963}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001964
1965 int
1966expand_set_diffopt(optexpand_T *args, int *numMatches, char_u ***matches)
1967{
1968 expand_T *xp = args->oe_xp;
1969
1970 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
1971 {
1972 // Within "algorithm:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001973 int algo_len = (int)STRLEN("algorithm:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001974 if (xp->xp_pattern - args->oe_set_arg >= algo_len &&
1975 STRNCMP(xp->xp_pattern - algo_len, "algorithm:", algo_len) == 0)
1976 {
1977 return expand_set_opt_string(
1978 args,
1979 p_dip_algorithm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001980 ARRAY_LENGTH(p_dip_algorithm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001981 numMatches,
1982 matches);
1983 }
1984 return FAIL;
1985 }
1986
1987 return expand_set_opt_string(
1988 args,
1989 p_dip_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001990 ARRAY_LENGTH(p_dip_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001991 numMatches,
1992 matches);
1993}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001994#endif
1995
1996/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001997 * The 'display' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001998 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001999 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002000did_set_display(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002001{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002002 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002003 return e_invalid_argument;
2004
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002005 (void)init_chartab();
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002006 return NULL;
2007}
2008
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002009 int
2010expand_set_display(optexpand_T *args, int *numMatches, char_u ***matches)
2011{
2012 return expand_set_opt_string(
2013 args,
2014 p_dy_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002015 ARRAY_LENGTH(p_dy_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002016 numMatches,
2017 matches);
2018}
2019
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002020/*
2021 * The 'eadirection' option is changed.
2022 */
2023 char *
2024did_set_eadirection(optset_T *args UNUSED)
2025{
2026 return did_set_opt_strings(p_ead, p_ead_values, FALSE);
2027}
2028
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002029 int
2030expand_set_eadirection(optexpand_T *args, int *numMatches, char_u ***matches)
2031{
2032 return expand_set_opt_string(
2033 args,
2034 p_ead_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002035 ARRAY_LENGTH(p_ead_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002036 numMatches,
2037 matches);
2038}
2039
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002040/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002041 * One of the 'encoding', 'fileencoding', 'termencoding' or 'makeencoding'
2042 * options is changed.
2043 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002044 char *
2045did_set_encoding(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002046{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002047 char_u **varp = (char_u **)args->os_varp;
2048 char_u **gvarp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002049 char *errmsg = NULL;
2050 char_u *p;
2051
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002052 // Get the global option to compare with, otherwise we would have to check
2053 // two values for all local options.
2054 gvarp = (char_u **)get_option_varp_scope(args->os_idx, OPT_GLOBAL);
2055
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002056 if (gvarp == &p_fenc)
2057 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002058 if (!curbuf->b_p_ma && args->os_flags != OPT_GLOBAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002059 errmsg = e_cannot_make_changes_modifiable_is_off;
2060 else if (vim_strchr(*varp, ',') != NULL)
2061 // No comma allowed in 'fileencoding'; catches confusing it
2062 // with 'fileencodings'.
2063 errmsg = e_invalid_argument;
2064 else
2065 {
2066 // May show a "+" in the title now.
2067 redraw_titles();
2068 // Add 'fileencoding' to the swap file.
2069 ml_setflags(curbuf);
2070 }
2071 }
2072 if (errmsg == NULL)
2073 {
2074 // canonize the value, so that STRCMP() can be used on it
2075 p = enc_canonize(*varp);
2076 if (p != NULL)
2077 {
2078 vim_free(*varp);
2079 *varp = p;
2080 }
2081 if (varp == &p_enc)
2082 {
2083 errmsg = mb_init();
2084 redraw_titles();
2085 }
2086 }
2087
2088#if defined(FEAT_GUI_GTK)
2089 if (errmsg == NULL && varp == &p_tenc && gui.in_use)
2090 {
2091 // GTK uses only a single encoding, and that is UTF-8.
2092 if (STRCMP(p_tenc, "utf-8") != 0)
2093 errmsg = e_cannot_be_changed_in_gtk_GUI;
2094 }
2095#endif
2096
2097 if (errmsg == NULL)
2098 {
2099#ifdef FEAT_KEYMAP
2100 // When 'keymap' is used and 'encoding' changes, reload the keymap
2101 // (with another encoding).
2102 if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
2103 (void)keymap_init();
2104#endif
2105
2106 // When 'termencoding' is not empty and 'encoding' changes or when
2107 // 'termencoding' changes, need to setup for keyboard input and
2108 // display output conversion.
2109 if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
2110 {
2111 if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
2112 || convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
2113 {
2114 semsg(_(e_cannot_convert_between_str_and_str),
2115 p_tenc, p_enc);
2116 errmsg = e_invalid_argument;
2117 }
2118 }
2119
2120#if defined(MSWIN)
K.Takatace3189d2023-02-15 19:13:43 +00002121 // $HOME, $VIM and $VIMRUNTIME may have characters in active code page.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002122 if (varp == &p_enc)
K.Takatace3189d2023-02-15 19:13:43 +00002123 {
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002124 init_homedir();
K.Takatace3189d2023-02-15 19:13:43 +00002125 init_vimdir();
2126 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002127#endif
2128 }
2129
2130 return errmsg;
2131}
2132
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002133 int
2134expand_set_encoding(optexpand_T *args, int *numMatches, char_u ***matches)
2135{
2136 return expand_set_opt_generic(
2137 args,
2138 get_encoding_name,
2139 numMatches,
2140 matches);
2141}
2142
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002143/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002144 * The 'eventignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002145 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002146 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002147did_set_eventignore(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002148{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002149 if (check_ei() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002150 return e_invalid_argument;
2151 return NULL;
2152}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002153
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002154 static char_u *
2155get_eventignore_name(expand_T *xp, int idx)
2156{
2157 // 'eventignore' allows special keyword "all" in addition to
2158 // all event names.
2159 if (idx == 0)
2160 return (char_u *)"all";
2161 return get_event_name_no_group(xp, idx - 1);
2162}
2163
2164 int
2165expand_set_eventignore(optexpand_T *args, int *numMatches, char_u ***matches)
2166{
2167 return expand_set_opt_generic(
2168 args,
2169 get_eventignore_name,
2170 numMatches,
2171 matches);
2172}
2173
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002174/*
2175 * The 'fileformat' option is changed.
2176 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002177 char *
2178did_set_fileformat(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002179{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002180 char_u **varp = (char_u **)args->os_varp;
2181
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002182 if (!curbuf->b_p_ma && !(args->os_flags & OPT_GLOBAL))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002183 return e_cannot_make_changes_modifiable_is_off;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002184 else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002185 return e_invalid_argument;
2186
2187 // may also change 'textmode'
2188 if (get_fileformat(curbuf) == EOL_DOS)
2189 curbuf->b_p_tx = TRUE;
2190 else
2191 curbuf->b_p_tx = FALSE;
2192 redraw_titles();
2193 // update flag in swap file
2194 ml_setflags(curbuf);
2195 // Redraw needed when switching to/from "mac": a CR in the text
2196 // will be displayed differently.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002197 if (get_fileformat(curbuf) == EOL_MAC || *args->os_oldval.string == 'm')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002198 redraw_curbuf_later(UPD_NOT_VALID);
2199
2200 return NULL;
2201}
2202
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002203 int
2204expand_set_fileformat(optexpand_T *args, int *numMatches, char_u ***matches)
2205{
2206 return expand_set_opt_string(
2207 args,
2208 p_ff_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002209 ARRAY_LENGTH(p_ff_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002210 numMatches,
2211 matches);
2212}
2213
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002214/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002215 * Function given to ExpandGeneric() to obtain the possible arguments of the
2216 * fileformat options.
2217 */
2218 char_u *
2219get_fileformat_name(expand_T *xp UNUSED, int idx)
2220{
2221 if (idx >= (int)ARRAY_LENGTH(p_ff_values))
2222 return NULL;
2223
2224 return (char_u*)p_ff_values[idx];
2225}
2226
2227/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002228 * The 'fileformats' option is changed.
2229 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002230 char *
2231did_set_fileformats(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002232{
2233 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
2234 return e_invalid_argument;
2235
2236 // also change 'textauto'
2237 if (*p_ffs == NUL)
2238 p_ta = FALSE;
2239 else
2240 p_ta = TRUE;
2241
2242 return NULL;
2243}
2244
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002245/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002246 * The 'filetype' or the 'syntax' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002247 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002248 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002249did_set_filetype_or_syntax(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002250{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002251 char_u **varp = (char_u **)args->os_varp;
2252
2253 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002254 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002255
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002256 args->os_value_changed = STRCMP(args->os_oldval.string, *varp) != 0;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002257
2258 // Since we check the value, there is no need to set P_INSECURE,
2259 // even when the value comes from a modeline.
2260 args->os_value_checked = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002261
2262 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002263}
2264
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002265#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002266/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002267 * The 'foldclose' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002268 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002269 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002270did_set_foldclose(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002271{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002272 return did_set_opt_strings(p_fcl, p_fcl_values, TRUE);
2273}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002274
2275 int
2276expand_set_foldclose(optexpand_T *args, int *numMatches, char_u ***matches)
2277{
2278 return expand_set_opt_string(
2279 args,
2280 p_fcl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002281 ARRAY_LENGTH(p_fcl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002282 numMatches,
2283 matches);
2284}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002285#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002286
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002287#if (defined(FEAT_EVAL) && defined(FEAT_FOLDING)) || defined(PROTO)
2288/*
2289 * The 'foldexpr' option is changed.
2290 */
2291 char *
2292did_set_foldexpr(optset_T *args)
2293{
2294 (void)did_set_optexpr(args);
2295 if (foldmethodIsExpr(curwin))
2296 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002297 return NULL;
2298}
2299#endif
2300
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002301#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002302/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002303 * The 'foldignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002304 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002305 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002306did_set_foldignore(optset_T *args UNUSED)
2307{
2308 if (foldmethodIsIndent(curwin))
2309 foldUpdateAll(curwin);
2310 return NULL;
2311}
2312
2313/*
2314 * The 'foldmarker' option is changed.
2315 */
2316 char *
2317did_set_foldmarker(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002318{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002319 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002320 char_u *p;
2321
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002322 p = vim_strchr(*varp, ',');
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002323 if (p == NULL)
2324 return e_comma_required;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002325 else if (p == *varp || p[1] == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002326 return e_invalid_argument;
2327 else if (foldmethodIsMarker(curwin))
2328 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002329
2330 return NULL;
2331}
2332
2333/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002334 * The 'foldmethod' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002335 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002336 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002337did_set_foldmethod(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002338{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002339 char_u **varp = (char_u **)args->os_varp;
2340
Milly142cad12024-10-22 22:11:51 +02002341 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK || **varp == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002342 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002343
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002344 foldUpdateAll(curwin);
2345 if (foldmethodIsDiff(curwin))
2346 newFoldLevel();
2347 return NULL;
2348}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002349
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002350 int
2351expand_set_foldmethod(optexpand_T *args, int *numMatches, char_u ***matches)
2352{
2353 return expand_set_opt_string(
2354 args,
2355 p_fdm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002356 ARRAY_LENGTH(p_fdm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002357 numMatches,
2358 matches);
2359}
2360
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002361/*
2362 * The 'foldopen' option is changed.
2363 */
2364 char *
2365did_set_foldopen(optset_T *args UNUSED)
2366{
2367 return did_set_opt_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
2368}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002369
2370 int
2371expand_set_foldopen(optexpand_T *args, int *numMatches, char_u ***matches)
2372{
2373 return expand_set_opt_string(
2374 args,
2375 p_fdo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002376 ARRAY_LENGTH(p_fdo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002377 numMatches,
2378 matches);
2379}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002380#endif
2381
2382/*
2383 * The 'formatoptions' option is changed.
2384 */
2385 char *
2386did_set_formatoptions(optset_T *args)
2387{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002388 char_u **varp = (char_u **)args->os_varp;
2389
Christian Brabandtb39b2402023-11-29 11:34:05 +01002390 return did_set_option_listflag(*varp, (char_u *)FO_ALL, args->os_errbuf,
2391 args->os_errbuflen);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002392}
2393
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002394 int
2395expand_set_formatoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2396{
2397 return expand_set_opt_listflag(args, (char_u*)FO_ALL, numMatches, matches);
2398}
2399
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002400#if defined(CURSOR_SHAPE) || defined(PROTO)
2401/*
2402 * The 'guicursor' option is changed.
2403 */
2404 char *
2405did_set_guicursor(optset_T *args UNUSED)
2406{
2407 return parse_shape_opt(SHAPE_CURSOR);
2408}
2409#endif
2410
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002411#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002412/*
2413 * The 'guifont' option is changed.
2414 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002415 char *
2416did_set_guifont(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002417{
2418 char_u *p;
2419 char *errmsg = NULL;
2420
2421 if (gui.in_use)
2422 {
2423 p = p_guifont;
2424# if defined(FEAT_GUI_GTK)
2425 // Put up a font dialog and let the user select a new value.
2426 // If this is cancelled go back to the old value but don't
2427 // give an error message.
2428 if (STRCMP(p, "*") == 0)
2429 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002430 p = gui_mch_font_dialog(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002431 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002432 p_guifont = (p != NULL) ? p : vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002433 }
2434# endif
2435 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
2436 {
2437# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
2438 if (STRCMP(p_guifont, "*") == 0)
2439 {
2440 // Dialog was cancelled: Keep the old value without giving
2441 // an error message.
2442 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002443 p_guifont = vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002444 }
2445 else
2446# endif
2447 errmsg = e_invalid_fonts;
2448 }
2449 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002450
2451 return errmsg;
2452}
2453
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002454/*
2455 * Expand the 'guifont' option. Only when GUI is being used. Each platform has
2456 * specific behaviors.
2457 */
2458 int
2459expand_set_guifont(optexpand_T *args, int *numMatches, char_u ***matches)
2460{
2461 if (!gui.in_use)
2462 return FAIL;
2463
2464# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
2465 char_u **varp = (char_u **)args->oe_varp;
2466 int wide = (varp == &p_guifontwide);
2467
2468 return expand_set_opt_callback(
2469 args, gui_mch_expand_font, &wide, numMatches, matches);
2470# else
2471 return FAIL;
2472# endif
2473}
2474
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002475# if defined(FEAT_XFONTSET) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002476/*
2477 * The 'guifontset' option is changed.
2478 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002479 char *
2480did_set_guifontset(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002481{
2482 char *errmsg = NULL;
2483
2484 if (STRCMP(p_guifontset, "*") == 0)
2485 errmsg = e_cant_select_fontset;
2486 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
2487 errmsg = e_invalid_fontset;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002488
2489 return errmsg;
2490}
2491# endif
2492
2493/*
2494 * The 'guifontwide' option is changed.
2495 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002496 char *
2497did_set_guifontwide(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002498{
2499 char *errmsg = NULL;
2500
2501 if (STRCMP(p_guifontwide, "*") == 0)
2502 errmsg = e_cant_select_wide_font;
2503 else if (gui_get_wide_font() == FAIL)
2504 errmsg = e_invalid_wide_font;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002505
2506 return errmsg;
2507}
2508#endif
2509
Erik S. V. Jansson8b1e7492024-02-24 14:26:52 +01002510#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002511/*
2512 * The 'guiligatures' option is changed.
2513 */
2514 char *
2515did_set_guiligatures(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002516{
2517 gui_set_ligatures();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002518 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002519}
2520#endif
2521
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002522#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002523/*
2524 * The 'guioptions' option is changed.
2525 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002526 char *
2527did_set_guioptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002528{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002529 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002530 char *errmsg;
2531
Christian Brabandtb39b2402023-11-29 11:34:05 +01002532 errmsg = did_set_option_listflag(*varp, (char_u *)GO_ALL, args->os_errbuf,
2533 args->os_errbuflen);
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002534 if (errmsg != NULL)
2535 return errmsg;
2536
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002537 gui_init_which_components(args->os_oldval.string);
2538 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002539}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002540
2541 int
2542expand_set_guioptions(optexpand_T *args, int *numMatches, char_u ***matches)
2543{
2544 return expand_set_opt_listflag(args, (char_u*)GO_ALL, numMatches, matches);
2545}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002546#endif
2547
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002548#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002549/*
2550 * The 'guitablabel' option is changed.
2551 */
2552 char *
2553did_set_guitablabel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002554{
2555 redraw_tabline = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002556 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002557}
2558#endif
2559
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002560/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002561 * The 'helpfile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002562 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002563 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002564did_set_helpfile(optset_T *args UNUSED)
2565{
2566 // May compute new values for $VIM and $VIMRUNTIME
2567 if (didset_vim)
2568 vim_unsetenv_ext((char_u *)"VIM");
2569 if (didset_vimruntime)
2570 vim_unsetenv_ext((char_u *)"VIMRUNTIME");
2571 return NULL;
2572}
2573
2574#if defined(FEAT_MULTI_LANG) || defined(PROTO)
2575/*
2576 * The 'helplang' option is changed.
2577 */
2578 char *
2579did_set_helplang(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002580{
2581 char *errmsg = NULL;
2582
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002583 // Check for "", "ab", "ab,cd", etc.
2584 for (char_u *s = p_hlg; *s != NUL; s += 3)
2585 {
2586 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
2587 {
2588 errmsg = e_invalid_argument;
2589 break;
2590 }
2591 if (s[2] == NUL)
2592 break;
2593 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002594
2595 return errmsg;
2596}
2597#endif
2598
2599/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002600 * The 'highlight' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002601 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002602 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002603did_set_highlight(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002604{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002605 if (highlight_changed() == FAIL)
2606 return e_invalid_argument; // invalid flags
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002607
2608 return NULL;
2609}
2610
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002611/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002612 * Expand 'highlight' option.
2613 */
2614 int
2615expand_set_highlight(optexpand_T *args, int *numMatches, char_u ***matches)
2616{
2617 char_u *p;
2618 expand_T *xp = args->oe_xp;
2619 static char_u hl_flags[HLF_COUNT] = HL_FLAGS;
Christian Brabandt3f168ec2023-10-02 23:21:11 +02002620 size_t i;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002621 int count = 0;
2622
2623 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2624 {
2625 // Right after a ':', meaning we just return all highlight names.
2626 return expand_set_opt_generic(
2627 args,
2628 get_highlight_name,
2629 numMatches,
2630 matches);
2631 }
2632
2633 if (*xp->xp_pattern == NUL)
2634 {
2635 // At beginning of a comma-separated list. Return the specific list of
2636 // supported occasions.
2637 *matches = ALLOC_MULT(char_u *, HLF_COUNT + 1);
2638 if (*matches == NULL)
2639 return FAIL;
2640
2641 // We still want to return the full option if it's requested.
2642 if (args->oe_include_orig_val)
2643 {
2644 p = vim_strsave(args->oe_opt_value);
2645 if (p == NULL)
2646 {
2647 VIM_CLEAR(*matches);
2648 return FAIL;
2649 }
2650 (*matches)[count++] = p;
2651 }
2652
2653 for (i = 0; i < HLF_COUNT; i++)
2654 {
2655 p = vim_strnsave(&hl_flags[i], 1);
2656 if (p == NULL)
2657 {
2658 if (count == 0)
2659 {
2660 VIM_CLEAR(*matches);
2661 return FAIL;
2662 }
2663 else
2664 break;
2665 }
2666 (*matches)[count++] = p;
2667 }
2668
2669 if (count == 0)
2670 {
2671 VIM_CLEAR(*matches);
2672 return FAIL;
2673 }
2674 *numMatches = count;
2675 return OK;
2676 }
2677
2678 // We are after the initial character (which indicates the occasion). We
2679 // already made sure we are not matching after a ':' above, so now we want
2680 // to match against display mode modifiers.
2681 // Since the xp_pattern starts from the beginning, we need to include it in
2682 // the returned match.
2683
2684 // Note: Keep this in sync with highlight_changed()
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002685 static char_u p_hl_mode_values[] =
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002686 {':', 'b', 'i', '-', 'n', 'r', 's', 'u', 'c', '2', 'd', '=', 't'};
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002687 size_t num_hl_modes = ARRAY_LENGTH(p_hl_mode_values);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002688
2689 *matches = ALLOC_MULT(char_u *, num_hl_modes);
2690 if (*matches == NULL)
2691 return FAIL;
2692
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002693 int pattern_len = xp->xp_pattern_len;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002694
2695 for (i = 0; i < num_hl_modes; i++)
2696 {
2697 // Don't allow duplicates as these are unique flags
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002698 char_u *dup = vim_strchr(xp->xp_pattern + 1, p_hl_mode_values[i]);
2699 if (dup != NULL && (int)(dup - xp->xp_pattern) < pattern_len)
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002700 continue;
2701
2702 // ':' only works by itself, not with other flags.
2703 if (pattern_len > 1 && p_hl_mode_values[i] == ':')
2704 continue;
2705
2706 p = vim_strnsave(xp->xp_pattern, pattern_len + 1);
2707 if (p == NULL)
2708 {
2709 if (i == 0)
2710 {
2711 VIM_CLEAR(*matches);
2712 return FAIL;
2713 }
2714 else
2715 break;
2716 }
2717 p[pattern_len] = p_hl_mode_values[i];
2718 p[pattern_len + 1] = NUL;
2719 (*matches)[count++] = p;
2720 }
2721 if (count == 0)
2722 {
2723 VIM_CLEAR(*matches);
2724 return FAIL;
2725 }
2726 *numMatches = count;
2727
2728 return OK;
2729}
2730
2731/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002732 * The 'titlestring' or the 'iconstring' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002733 */
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002734 static char *
2735parse_titleiconstring(optset_T *args UNUSED, int flagval UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002736{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002737#ifdef FEAT_STL_OPT
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002738 char_u **varp = (char_u **)args->os_varp;
2739
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002740 // NULL => statusline syntax
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002741 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002742 stl_syntax |= flagval;
2743 else
2744 stl_syntax &= ~flagval;
2745#endif
2746 did_set_title();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002747
2748 return NULL;
2749}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002750
2751/*
2752 * The 'iconstring' option is changed.
2753 */
2754 char *
2755did_set_iconstring(optset_T *args)
2756{
2757 int flagval = 0;
2758
2759#ifdef FEAT_STL_OPT
2760 flagval = STL_IN_ICON;
2761#endif
2762
2763 return parse_titleiconstring(args, flagval);
2764}
2765
2766#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
2767/*
2768 * The 'imactivatekey' option is changed.
2769 */
2770 char *
2771did_set_imactivatekey(optset_T *args UNUSED)
2772{
2773 if (!im_xim_isvalid_imactivate())
2774 return e_invalid_argument;
2775 return NULL;
2776}
2777#endif
2778
2779/*
Milly5e7a6a42024-10-22 22:27:19 +02002780 * The 'iskeyword' option is changed.
2781 */
2782 char *
2783did_set_iskeyword(optset_T *args)
2784{
2785 char_u **varp = (char_u **)args->os_varp;
2786
2787 if (varp == &p_isk) // only check for global-value
2788 {
2789 if (check_isopt(*varp) == FAIL)
2790 return e_invalid_argument;
2791 }
2792 else // fallthrough for local-value
2793 return did_set_isopt(args);
2794
2795 return NULL;
2796}
2797
2798/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002799 * The 'isident' or the 'iskeyword' or the 'isprint' or the 'isfname' option is
2800 * changed.
2801 */
2802 char *
2803did_set_isopt(optset_T *args)
2804{
Milly5e7a6a42024-10-22 22:27:19 +02002805 // 'isident', 'iskeyword', 'isprint' or 'isfname' option: refill g_chartab[]
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002806 // If the new option is invalid, use old value.
2807 // 'lisp' option: refill g_chartab[] for '-' char.
2808 if (init_chartab() == FAIL)
2809 {
2810 args->os_restore_chartab = TRUE;// need to restore the chartab.
2811 return e_invalid_argument; // error in value
2812 }
2813
2814 return NULL;
2815}
2816
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002817/*
2818 * The 'jumpoptions' option is changed.
2819 */
2820 char *
Yegappan Lakshmanan1926ae42023-09-21 16:36:28 +02002821did_set_jumpoptions(optset_T *args UNUSED)
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002822{
2823 if (opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE) != OK)
2824 return e_invalid_argument;
2825
2826 return NULL;
2827}
2828
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002829 int
2830expand_set_jumpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2831{
2832 return expand_set_opt_string(
2833 args,
2834 p_jop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002835 ARRAY_LENGTH(p_jop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002836 numMatches,
2837 matches);
2838}
2839
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002840#if defined(FEAT_KEYMAP) || defined(PROTO)
2841/*
2842 * The 'keymap' option is changed.
2843 */
2844 char *
2845did_set_keymap(optset_T *args)
2846{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002847 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002848 char *errmsg = NULL;
2849
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002850 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002851 errmsg = e_invalid_argument;
2852 else
2853 {
2854 int secure_save = secure;
2855
2856 // Reset the secure flag, since the value of 'keymap' has
2857 // been checked to be safe.
2858 secure = 0;
2859
2860 // load or unload key mapping tables
2861 errmsg = keymap_init();
2862
2863 secure = secure_save;
2864
2865 // Since we check the value, there is no need to set P_INSECURE,
2866 // even when the value comes from a modeline.
2867 args->os_value_checked = TRUE;
2868 }
2869
2870 if (errmsg == NULL)
2871 {
2872 if (*curbuf->b_p_keymap != NUL)
2873 {
2874 // Installed a new keymap, switch on using it.
2875 curbuf->b_p_iminsert = B_IMODE_LMAP;
2876 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
2877 curbuf->b_p_imsearch = B_IMODE_LMAP;
2878 }
2879 else
2880 {
2881 // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
2882 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
2883 curbuf->b_p_iminsert = B_IMODE_NONE;
2884 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
2885 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
2886 }
2887 if ((args->os_flags & OPT_LOCAL) == 0)
2888 {
2889 set_iminsert_global();
2890 set_imsearch_global();
2891 }
2892 status_redraw_curbuf();
2893 }
2894
2895 return errmsg;
2896}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002897#endif
2898
2899/*
2900 * The 'keymodel' option is changed.
2901 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002902 char *
2903did_set_keymodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002904{
2905 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
2906 return e_invalid_argument;
2907
2908 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
2909 km_startsel = (vim_strchr(p_km, 'a') != NULL);
2910 return NULL;
2911}
2912
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002913 int
2914expand_set_keymodel(optexpand_T *args, int *numMatches, char_u ***matches)
2915{
2916 return expand_set_opt_string(
2917 args,
2918 p_km_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002919 ARRAY_LENGTH(p_km_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002920 numMatches,
2921 matches);
2922}
2923
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002924/*
2925 * The 'keyprotocol' option is changed.
2926 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002927 char *
2928did_set_keyprotocol(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002929{
Gregory Anders3695d0e2023-09-29 20:17:20 +02002930 char_u *term = T_NAME;
2931 keyprot_T kpc = match_keyprotocol(term);
2932 if (kpc == KEYPROTOCOL_FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002933 return e_invalid_argument;
2934
Gregory Anders3695d0e2023-09-29 20:17:20 +02002935 apply_keyprotocol(term, kpc);
2936
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002937 return NULL;
2938}
2939
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002940 int
2941expand_set_keyprotocol(optexpand_T *args, int *numMatches, char_u ***matches)
2942{
2943 expand_T *xp = args->oe_xp;
2944 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2945 {
2946 // 'keyprotocol' only has well-defined terms for completion for the
2947 // protocol part after the colon.
2948 return expand_set_opt_string(
2949 args,
2950 p_kpc_protocol_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002951 ARRAY_LENGTH(p_kpc_protocol_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002952 numMatches,
2953 matches);
2954 }
2955 // Use expand_set_opt_string instead of returning FAIL so that we can
2956 // include the original value if args->oe_include_orig_val is set.
2957 static char *(empty[]) = {NULL};
2958 return expand_set_opt_string(args, empty, 0, numMatches, matches);
2959}
2960
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002961/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002962 * The 'lispoptions' option is changed.
2963 */
2964 char *
2965did_set_lispoptions(optset_T *args)
2966{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002967 char_u **varp = (char_u **)args->os_varp;
2968
2969 if (**varp != NUL
2970 && STRCMP(*varp, "expr:0") != 0
2971 && STRCMP(*varp, "expr:1") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002972 return e_invalid_argument;
2973
2974 return NULL;
2975}
2976
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002977 int
2978expand_set_lispoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2979{
2980 static char *(p_lop_values[]) = {"expr:0", "expr:1", NULL};
2981 return expand_set_opt_string(
2982 args,
2983 p_lop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002984 ARRAY_LENGTH(p_lop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002985 numMatches,
2986 matches);
2987}
2988
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002989/*
2990 * The 'matchpairs' option is changed.
2991 */
2992 char *
2993did_set_matchpairs(optset_T *args)
2994{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002995 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002996 char_u *p;
2997
2998 if (has_mbyte)
2999 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003000 for (p = *varp; *p != NUL; ++p)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003001 {
3002 int x2 = -1;
3003 int x3 = -1;
3004
3005 p += mb_ptr2len(p);
3006 if (*p != NUL)
3007 x2 = *p++;
3008 if (*p != NUL)
3009 {
3010 x3 = mb_ptr2char(p);
3011 p += mb_ptr2len(p);
3012 }
3013 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
3014 return e_invalid_argument;
3015 if (*p == NUL)
3016 break;
3017 }
3018 }
3019 else
3020 {
3021 // Check for "x:y,x:y"
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003022 for (p = *varp; *p != NUL; p += 4)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003023 {
3024 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
3025 return e_invalid_argument;
3026 if (p[3] == NUL)
3027 break;
3028 }
3029 }
3030
3031 return NULL;
3032}
3033
3034#if defined(FEAT_SPELL) || defined(PROTO)
3035/*
3036 * The 'mkspellmem' option is changed.
3037 */
3038 char *
3039did_set_mkspellmem(optset_T *args UNUSED)
3040{
3041 if (spell_check_msm() != OK)
3042 return e_invalid_argument;
3043
3044 return NULL;
3045}
3046#endif
3047
3048/*
3049 * The 'mouse' option is changed.
3050 */
3051 char *
3052did_set_mouse(optset_T *args)
3053{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003054 char_u **varp = (char_u **)args->os_varp;
3055
Christian Brabandtb39b2402023-11-29 11:34:05 +01003056 return did_set_option_listflag(*varp, (char_u *)MOUSE_ALL, args->os_errbuf,
3057 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003058}
3059
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003060 int
3061expand_set_mouse(optexpand_T *args, int *numMatches, char_u ***matches)
3062{
3063 return expand_set_opt_listflag(args, (char_u*)MOUSE_ALL, numMatches, matches);
3064}
3065
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003066/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003067 * The 'mousemodel' option is changed.
3068 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003069 char *
3070did_set_mousemodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003071{
3072 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
3073 return e_invalid_argument;
3074#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
Ken Takata18d0d292023-12-19 20:12:29 +01003075 else if (*p_mousem != *args->os_oldval.string)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003076 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
3077 // to create or delete the popup menus.
3078 gui_motif_update_mousemodel(root_menu);
3079#endif
3080
3081 return NULL;
3082}
3083
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003084 int
3085expand_set_mousemodel(optexpand_T *args, int *numMatches, char_u ***matches)
3086{
3087 return expand_set_opt_string(
3088 args,
3089 p_mousem_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003090 ARRAY_LENGTH(p_mousem_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003091 numMatches,
3092 matches);
3093}
3094
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003095#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3096 char *
3097did_set_mouseshape(optset_T *args UNUSED)
3098{
3099 char *errmsg = NULL;
3100
3101 errmsg = parse_shape_opt(SHAPE_MOUSE);
3102 update_mouseshape(-1);
3103
3104 return errmsg;
3105}
3106#endif
3107
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003108/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003109 * The 'nrformats' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003110 */
3111 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003112did_set_nrformats(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003113{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003114 char_u **varp = (char_u **)args->os_varp;
3115
3116 return did_set_opt_strings(*varp, p_nf_values, TRUE);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003117}
3118
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003119 int
3120expand_set_nrformats(optexpand_T *args, int *numMatches, char_u ***matches)
3121{
3122 return expand_set_opt_string(
3123 args,
3124 p_nf_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003125 ARRAY_LENGTH(p_nf_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003126 numMatches,
3127 matches);
3128}
3129
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003130#if defined(FEAT_EVAL) || defined(PROTO)
3131/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003132 * One of the '*expr' options is changed: 'balloonexpr', 'diffexpr',
3133 * 'foldexpr', 'foldtext', 'formatexpr', 'includeexpr', 'indentexpr',
3134 * 'patchexpr', 'printexpr' and 'charconvert'.
3135 *
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003136 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003137 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003138did_set_optexpr(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003139{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003140 char_u **varp = (char_u **)args->os_varp;
3141
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003142 // If the option value starts with <SID> or s:, then replace that with
3143 // the script identifier.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003144 char_u *name = get_scriptlocal_funcname(*varp);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003145 if (name != NULL)
3146 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003147 free_string_option(*varp);
3148 *varp = name;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003149 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003150
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003151 return NULL;
3152}
3153#endif
3154
3155/*
3156 * The 'pastetoggle' option is changed.
3157 */
3158 char *
3159did_set_pastetoggle(optset_T *args UNUSED)
3160{
3161 char_u *p;
3162
3163 // translate key codes like in a mapping
3164 if (*p_pt)
3165 {
zeertzjq7e0bae02023-08-11 23:15:38 +02003166 (void)replace_termcodes(p_pt, &p, 0,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003167 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
3168 if (p != NULL)
3169 {
3170 free_string_option(p_pt);
3171 p_pt = p;
3172 }
3173 }
3174
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003175 return NULL;
3176}
3177
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003178#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3179/*
3180 * The 'previewpopup' option is changed.
3181 */
3182 char *
3183did_set_previewpopup(optset_T *args UNUSED)
3184{
3185 if (parse_previewpopup(NULL) == FAIL)
3186 return e_invalid_argument;
3187
3188 return NULL;
3189}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003190
3191 int
3192expand_set_popupoption(optexpand_T *args, int *numMatches, char_u ***matches)
3193{
3194 expand_T *xp = args->oe_xp;
3195
3196 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3197 {
3198 // Within "highlight:"/"border:"/"align:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003199 int border_len = (int)STRLEN("border:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003200 if (xp->xp_pattern - args->oe_set_arg >= border_len &&
3201 STRNCMP(xp->xp_pattern - border_len, "border:", border_len) == 0)
3202 {
3203 return expand_set_opt_string(
3204 args,
3205 p_popup_option_border_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003206 ARRAY_LENGTH(p_popup_option_border_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003207 numMatches,
3208 matches);
3209 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003210 int align_len = (int)STRLEN("align:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003211 if (xp->xp_pattern - args->oe_set_arg >= align_len &&
3212 STRNCMP(xp->xp_pattern - align_len, "align:", align_len) == 0)
3213 {
3214 return expand_set_opt_string(
3215 args,
3216 p_popup_option_align_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003217 ARRAY_LENGTH(p_popup_option_align_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003218 numMatches,
3219 matches);
3220 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003221 int highlight_len = (int)STRLEN("highlight:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003222 if (xp->xp_pattern - args->oe_set_arg >= highlight_len &&
3223 STRNCMP(xp->xp_pattern - highlight_len, "highlight:", highlight_len) == 0)
3224 {
3225 // Return the list of all highlight names
3226 return expand_set_opt_generic(
3227 args,
3228 get_highlight_name,
3229 numMatches,
3230 matches);
3231 }
3232 return FAIL;
3233 }
3234
3235 return expand_set_opt_string(
3236 args,
3237 p_popup_option_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003238 ARRAY_LENGTH(p_popup_option_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003239 numMatches,
3240 matches);
3241}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003242#endif
3243
3244#if defined(FEAT_POSTSCRIPT) || defined(PROTO)
3245/*
3246 * The 'printencoding' option is changed.
3247 */
3248 char *
3249did_set_printencoding(optset_T *args UNUSED)
3250{
3251 char_u *s, *p;
3252
3253 // Canonize 'printencoding' if VIM standard one
3254 p = enc_canonize(p_penc);
3255 if (p != NULL)
3256 {
3257 vim_free(p_penc);
3258 p_penc = p;
3259 }
3260 else
3261 {
3262 // Ensure lower case and '-' for '_'
3263 for (s = p_penc; *s != NUL; s++)
3264 {
3265 if (*s == '_')
3266 *s = '-';
3267 else
3268 *s = TOLOWER_ASC(*s);
3269 }
3270 }
3271
3272 return NULL;
3273}
3274#endif
3275
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003276#if defined(FEAT_PRINTER) || defined(PROTO)
3277
3278 static char_u *
3279get_printoptions_names(expand_T *xp UNUSED, int idx)
3280{
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003281 if (idx >= (int)ARRAY_LENGTH(printer_opts))
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003282 return NULL;
3283 return (char_u*)printer_opts[idx].name;
3284}
3285
3286/*
3287 * Expand 'printoptions' option
3288 */
3289 int
3290expand_set_printoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3291{
3292 return expand_set_opt_generic(
3293 args,
3294 get_printoptions_names,
3295 numMatches,
3296 matches);
3297}
3298#endif
3299
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003300#if defined(FEAT_STL_OPT) || defined(PROTO)
3301/*
3302 * The 'statusline' or the 'tabline' or the 'rulerformat' option is changed.
3303 * "rulerformat" is TRUE if the 'rulerformat' option is changed.
3304 */
3305 static char *
3306parse_statustabline_rulerformat(optset_T *args, int rulerformat)
3307{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003308 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003309 char_u *s;
3310 char *errmsg = NULL;
3311 int wid;
3312
3313 if (rulerformat) // reset ru_wid first
3314 ru_wid = 0;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003315 s = *varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003316 if (rulerformat && *s == '%')
3317 {
3318 // set ru_wid if 'ruf' starts with "%99("
3319 if (*++s == '-') // ignore a '-'
3320 s++;
3321 wid = getdigits(&s);
3322 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
3323 ru_wid = wid;
3324 else
3325 errmsg = check_stl_option(p_ruf);
3326 }
3327 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
3328 else if (rulerformat || s[0] != '%' || s[1] != '!')
3329 errmsg = check_stl_option(s);
3330 if (rulerformat && errmsg == NULL)
3331 comp_col();
3332
3333 return errmsg;
3334}
3335#endif
3336
3337#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
3338/*
3339 * The 'renderoptions' option is changed.
3340 */
3341 char *
3342did_set_renderoptions(optset_T *args UNUSED)
3343{
3344 if (!gui_mch_set_rendering_options(p_rop))
3345 return e_invalid_argument;
3346
3347 return NULL;
3348}
3349#endif
3350
3351#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3352/*
3353 * The 'rightleftcmd' option is changed.
3354 */
3355 char *
3356did_set_rightleftcmd(optset_T *args)
3357{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003358 char_u **varp = (char_u **)args->os_varp;
3359
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003360 // Currently only "search" is a supported value.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003361 if (**varp != NUL && STRCMP(*varp, "search") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003362 return e_invalid_argument;
3363
3364 return NULL;
3365}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003366
3367 int
3368expand_set_rightleftcmd(optexpand_T *args, int *numMatches, char_u ***matches)
3369{
3370 static char *(p_rlc_values[]) = {"search", NULL};
3371 return expand_set_opt_string(
3372 args,
3373 p_rlc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003374 ARRAY_LENGTH(p_rlc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003375 numMatches,
3376 matches);
3377}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003378#endif
3379
3380#if defined(FEAT_STL_OPT) || defined(PROTO)
3381/*
3382 * The 'rulerformat' option is changed.
3383 */
3384 char *
3385did_set_rulerformat(optset_T *args)
3386{
3387 return parse_statustabline_rulerformat(args, TRUE);
3388}
3389#endif
3390
3391/*
3392 * The 'scrollopt' option is changed.
3393 */
3394 char *
3395did_set_scrollopt(optset_T *args UNUSED)
3396{
3397 return did_set_opt_strings(p_sbo, p_scbopt_values, TRUE);
3398}
3399
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003400 int
3401expand_set_scrollopt(optexpand_T *args, int *numMatches, char_u ***matches)
3402{
3403 return expand_set_opt_string(
3404 args,
3405 p_scbopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003406 ARRAY_LENGTH(p_scbopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003407 numMatches,
3408 matches);
3409}
3410
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003411/*
3412 * The 'selection' option is changed.
3413 */
3414 char *
3415did_set_selection(optset_T *args UNUSED)
3416{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003417 if (*p_sel == NUL || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003418 return e_invalid_argument;
3419
3420 return NULL;
3421}
3422
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003423 int
3424expand_set_selection(optexpand_T *args, int *numMatches, char_u ***matches)
3425{
3426 return expand_set_opt_string(
3427 args,
3428 p_sel_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003429 ARRAY_LENGTH(p_sel_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003430 numMatches,
3431 matches);
3432}
3433
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003434/*
3435 * The 'selectmode' option is changed.
3436 */
3437 char *
3438did_set_selectmode(optset_T *args UNUSED)
3439{
3440 return did_set_opt_strings(p_slm, p_slm_values, TRUE);
3441}
3442
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003443 int
3444expand_set_selectmode(optexpand_T *args, int *numMatches, char_u ***matches)
3445{
3446 return expand_set_opt_string(
3447 args,
3448 p_slm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003449 ARRAY_LENGTH(p_slm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003450 numMatches,
3451 matches);
3452}
3453
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003454#if defined(FEAT_SESSION) || defined(PROTO)
3455/*
3456 * The 'sessionoptions' option is changed.
3457 */
3458 char *
3459did_set_sessionoptions(optset_T *args)
3460{
3461 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
3462 return e_invalid_argument;
3463 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
3464 {
3465 // Don't allow both "sesdir" and "curdir".
3466 (void)opt_strings_flags(args->os_oldval.string, p_ssop_values,
3467 &ssop_flags, TRUE);
3468 return e_invalid_argument;
3469 }
3470
3471 return NULL;
3472}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003473
3474 int
3475expand_set_sessionoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3476{
3477 return expand_set_opt_string(
3478 args,
3479 p_ssop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003480 ARRAY_LENGTH(p_ssop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003481 numMatches,
3482 matches);
3483}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003484#endif
3485
3486/*
3487 * The 'shortmess' option is changed.
3488 */
3489 char *
3490did_set_shortmess(optset_T *args)
3491{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003492 char_u **varp = (char_u **)args->os_varp;
3493
Christian Brabandtb39b2402023-11-29 11:34:05 +01003494 return did_set_option_listflag(*varp, (char_u *)SHM_ALL, args->os_errbuf,
3495 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003496}
3497
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003498 int
3499expand_set_shortmess(optexpand_T *args, int *numMatches, char_u ***matches)
3500{
3501 return expand_set_opt_listflag(args, (char_u*)SHM_ALL, numMatches, matches);
3502}
3503
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003504#if defined(FEAT_LINEBREAK) || defined(PROTO)
3505/*
3506 * The 'showbreak' option is changed.
3507 */
3508 char *
3509did_set_showbreak(optset_T *args)
3510{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003511 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003512 char_u *s;
3513
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003514 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003515 {
3516 if (ptr2cells(s) != 1)
3517 return e_showbreak_contains_unprintable_or_wide_character;
3518 MB_PTR_ADV(s);
3519 }
3520
3521 return NULL;
3522}
3523#endif
3524
3525/*
3526 * The 'showcmdloc' option is changed.
3527 */
3528 char *
3529did_set_showcmdloc(optset_T *args UNUSED)
3530{
zeertzjqc27fcf42024-03-01 23:01:43 +01003531 char *errmsg = did_set_opt_strings(p_sloc, p_sloc_values, FALSE);
3532
3533 if (errmsg == NULL)
3534 comp_col();
3535
3536 return errmsg;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003537}
3538
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003539 int
3540expand_set_showcmdloc(optexpand_T *args, int *numMatches, char_u ***matches)
3541{
3542 return expand_set_opt_string(
3543 args,
3544 p_sloc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003545 ARRAY_LENGTH(p_sloc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003546 numMatches,
3547 matches);
3548}
3549
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003550#if defined(FEAT_SIGNS) || defined(PROTO)
3551/*
3552 * The 'signcolumn' option is changed.
3553 */
3554 char *
3555did_set_signcolumn(optset_T *args)
3556{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003557 char_u **varp = (char_u **)args->os_varp;
3558
3559 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003560 return e_invalid_argument;
3561 // When changing the 'signcolumn' to or from 'number', recompute the
3562 // width of the number column if 'number' or 'relativenumber' is set.
3563 if (((*args->os_oldval.string == 'n' && args->os_oldval.string[1] == 'u')
3564 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
3565 && (curwin->w_p_nu || curwin->w_p_rnu))
3566 curwin->w_nrwidth_line_count = 0;
3567
3568 return NULL;
3569}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003570
3571 int
3572expand_set_signcolumn(optexpand_T *args, int *numMatches, char_u ***matches)
3573{
3574 return expand_set_opt_string(
3575 args,
3576 p_scl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003577 ARRAY_LENGTH(p_scl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003578 numMatches,
3579 matches);
3580}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003581#endif
3582
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003583#if defined(FEAT_SPELL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003584/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003585 * The 'spellcapcheck' option is changed.
3586 */
3587 char *
3588did_set_spellcapcheck(optset_T *args UNUSED)
3589{
3590 // compile the regexp program.
3591 return compile_cap_prog(curwin->w_s);
3592}
3593
3594/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003595 * The 'spellfile' option is changed.
3596 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003597 char *
3598did_set_spellfile(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003599{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003600 char_u **varp = (char_u **)args->os_varp;
3601
3602 if (!valid_spellfile(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003603 return e_invalid_argument;
3604
3605 // If there is a window for this buffer in which 'spell' is set load the
3606 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003607 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003608}
3609
3610/*
3611 * The 'spell' option is changed.
3612 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003613 char *
3614did_set_spelllang(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003615{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003616 char_u **varp = (char_u **)args->os_varp;
3617
3618 if (!valid_spelllang(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003619 return e_invalid_argument;
3620
3621 // If there is a window for this buffer in which 'spell' is set load the
3622 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003623 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003624}
3625
3626/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003627 * The 'spelloptions' option is changed.
3628 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003629 char *
3630did_set_spelloptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003631{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003632 char_u **varp = (char_u **)args->os_varp;
3633
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003634 if (**varp != NUL && STRCMP(*varp, "camel") != 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003635 return e_invalid_argument;
3636
3637 return NULL;
3638}
3639
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003640 int
3641expand_set_spelloptions(optexpand_T *args, int *numMatches, char_u ***matches)
3642{
3643 static char *(p_spo_values[]) = {"camel", NULL};
3644 return expand_set_opt_string(
3645 args,
3646 p_spo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003647 ARRAY_LENGTH(p_spo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003648 numMatches,
3649 matches);
3650}
3651
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003652/*
3653 * The 'spellsuggest' option is changed.
3654 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003655 char *
3656did_set_spellsuggest(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003657{
3658 if (spell_check_sps() != OK)
3659 return e_invalid_argument;
3660
3661 return NULL;
3662}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003663
3664 int
3665expand_set_spellsuggest(optexpand_T *args, int *numMatches, char_u ***matches)
3666{
3667 return expand_set_opt_string(
3668 args,
3669 p_sps_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003670 ARRAY_LENGTH(p_sps_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003671 numMatches,
3672 matches);
3673}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003674#endif
3675
3676/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003677 * The 'splitkeep' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003678 */
3679 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003680did_set_splitkeep(optset_T *args UNUSED)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003681{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003682 return did_set_opt_strings(p_spk, p_spk_values, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003683}
3684
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003685 int
3686expand_set_splitkeep(optexpand_T *args, int *numMatches, char_u ***matches)
3687{
3688 return expand_set_opt_string(
3689 args,
3690 p_spk_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003691 ARRAY_LENGTH(p_spk_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003692 numMatches,
3693 matches);
3694}
3695
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00003696#if defined(FEAT_STL_OPT) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003697/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003698 * The 'statusline' option is changed.
3699 */
3700 char *
3701did_set_statusline(optset_T *args)
3702{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003703 return parse_statustabline_rulerformat(args, FALSE);
3704}
3705#endif
3706
3707/*
3708 * The 'swapsync' option is changed.
3709 */
3710 char *
3711did_set_swapsync(optset_T *args UNUSED)
3712{
3713 return did_set_opt_strings(p_sws, p_sws_values, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003714}
3715
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003716 int
3717expand_set_swapsync(optexpand_T *args, int *numMatches, char_u ***matches)
3718{
3719 return expand_set_opt_string(
3720 args,
3721 p_sws_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003722 ARRAY_LENGTH(p_sws_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003723 numMatches,
3724 matches);
3725}
3726
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003727/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003728 * The 'switchbuf' option is changed.
3729 */
3730 char *
3731did_set_switchbuf(optset_T *args UNUSED)
3732{
3733 return did_set_opt_flags(p_swb, p_swb_values, &swb_flags, TRUE);
3734}
3735
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003736 int
3737expand_set_switchbuf(optexpand_T *args, int *numMatches, char_u ***matches)
3738{
3739 return expand_set_opt_string(
3740 args,
3741 p_swb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003742 ARRAY_LENGTH(p_swb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003743 numMatches,
3744 matches);
3745}
3746
LemonBoy5247b0b2024-07-12 19:30:58 +02003747/*
3748 * The 'tabclose' option is changed.
3749 */
3750 char *
3751did_set_tabclose(optset_T *args UNUSED)
3752{
3753 return did_set_opt_flags(p_tcl, p_tcl_values, &tcl_flags, TRUE);
3754}
3755
3756 int
3757expand_set_tabclose(optexpand_T *args, int *numMatches, char_u ***matches)
3758{
3759 return expand_set_opt_string(
3760 args,
3761 p_tcl_values,
3762 ARRAY_LENGTH(p_tcl_values) - 1,
3763 numMatches,
3764 matches);
3765}
3766
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003767#if defined(FEAT_STL_OPT) || defined(PROTO)
3768/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003769 * The 'tabline' option is changed.
3770 */
3771 char *
3772did_set_tabline(optset_T *args)
3773{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003774 return parse_statustabline_rulerformat(args, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003775}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003776#endif
3777
3778/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003779 * The 'tagcase' option is changed.
3780 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003781 char *
3782did_set_tagcase(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003783{
3784 unsigned int *flags;
3785 char_u *p;
3786
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003787 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003788 {
3789 p = curbuf->b_p_tc;
3790 flags = &curbuf->b_tc_flags;
3791 }
3792 else
3793 {
3794 p = p_tc;
3795 flags = &tc_flags;
3796 }
3797
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003798 if ((args->os_flags & OPT_LOCAL) && *p == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003799 // make the local value empty: use the global value
3800 *flags = 0;
3801 else if (*p == NUL
3802 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
3803 return e_invalid_argument;
3804
3805 return NULL;
3806}
3807
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003808 int
3809expand_set_tagcase(optexpand_T *args, int *numMatches, char_u ***matches)
3810{
3811 return expand_set_opt_string(
3812 args,
3813 p_tc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003814 ARRAY_LENGTH(p_tc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003815 numMatches,
3816 matches);
3817}
3818
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003819/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003820 * The 'term' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003821 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003822 char *
3823did_set_term(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003824{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003825 if (T_NAME[0] == NUL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003826 return e_cannot_set_term_to_empty_string;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003827#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003828 if (gui.in_use)
3829 return e_cannot_change_term_in_GUI;
3830 if (term_is_gui(T_NAME))
3831 return e_use_gui_to_start_GUI;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003832#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003833 if (set_termname(T_NAME) == FAIL)
3834 return e_not_found_in_termcap;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003835
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003836 // Screen colors may have changed.
3837 redraw_later_clear();
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003838
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003839 return NULL;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003840}
3841
3842/*
3843 * Some terminal option (t_xxx) is changed
3844 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003845 char *
3846did_set_term_option(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003847{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003848 char_u **varp = (char_u **)args->os_varp;
3849
3850 if (!full_screen)
3851 return NULL;
3852
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003853 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
3854 if (varp == &T_CCO)
3855 {
3856 int colors = atoi((char *)T_CCO);
3857
3858 // Only reinitialize colors if t_Co value has really changed to
3859 // avoid expensive reload of colorscheme if t_Co is set to the
3860 // same value multiple times.
3861 if (colors != t_colors)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003862 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003863 t_colors = colors;
3864 if (t_colors <= 1)
3865 {
3866 vim_free(T_CCO);
3867 T_CCO = empty_option;
3868 }
3869#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3870 if (is_term_win32())
3871 {
3872 swap_tcap();
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003873 args->os_did_swaptcap = TRUE;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003874 }
3875#endif
3876 // We now have a different color setup, initialize it again.
3877 init_highlight(TRUE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003878 }
3879 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003880 ttest(FALSE);
3881 if (varp == &T_ME)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003882 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003883 out_str(T_ME);
3884 redraw_later(UPD_CLEAR);
3885#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3886 // Since t_me has been set, this probably means that the user
3887 // wants to use this as default colors. Need to reset default
3888 // background/foreground colors.
3889# ifdef VIMDLL
3890 if (!gui.in_use && !gui.starting)
3891# endif
3892 mch_set_normal_colors();
3893#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003894 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003895 if (varp == &T_BE && termcap_active)
3896 {
3897 MAY_WANT_TO_LOG_THIS;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003898
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003899 if (*T_BE == NUL)
3900 // When clearing t_BE we assume the user no longer wants
3901 // bracketed paste, thus disable it by writing t_BD.
3902 out_str(T_BD);
3903 else
3904 out_str(T_BE);
3905 }
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003906
3907 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003908}
3909
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003910#if defined(FEAT_TERMINAL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003911/*
3912 * The 'termwinkey' option is changed.
3913 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003914 char *
Milly94606f72024-10-22 22:07:52 +02003915did_set_termwinkey(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003916{
Milly94606f72024-10-22 22:07:52 +02003917 char_u **varp = (char_u **)args->os_varp;
3918
3919 if ((*varp)[0] != NUL && string_to_key(*varp, TRUE) == 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003920 return e_invalid_argument;
3921
3922 return NULL;
3923}
3924
3925/*
3926 * The 'termwinsize' option is changed.
3927 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003928 char *
Milly8be10aa2024-10-22 22:01:46 +02003929did_set_termwinsize(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003930{
Milly8be10aa2024-10-22 22:01:46 +02003931 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003932 char_u *p;
3933
Milly8be10aa2024-10-22 22:01:46 +02003934 if ((*varp)[0] == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003935 return NULL;
3936
Milly8be10aa2024-10-22 22:01:46 +02003937 p = skipdigits(*varp);
3938 if (p == *varp || (*p != 'x' && *p != '*') || *skipdigits(p + 1) != NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003939 return e_invalid_argument;
3940
3941 return NULL;
3942}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003943
3944# if defined(MSWIN) || defined(PROTO)
3945/*
3946 * The 'termwintype' option is changed.
3947 */
3948 char *
3949did_set_termwintype(optset_T *args UNUSED)
3950{
3951 return did_set_opt_strings(p_twt, p_twt_values, FALSE);
3952}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003953
3954 int
3955expand_set_termwintype(optexpand_T *args, int *numMatches, char_u ***matches)
3956{
3957 return expand_set_opt_string(
3958 args,
3959 p_twt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003960 ARRAY_LENGTH(p_twt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003961 numMatches,
3962 matches);
3963}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003964# endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003965#endif
3966
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003967/*
3968 * The 'titlestring' option is changed.
3969 */
3970 char *
3971did_set_titlestring(optset_T *args)
3972{
3973 int flagval = 0;
3974
3975#ifdef FEAT_STL_OPT
3976 flagval = STL_IN_TITLE;
3977#endif
3978 return parse_titleiconstring(args, flagval);
3979}
3980
3981#if (defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)) || defined(PROTO)
3982/*
3983 * The 'toolbar' option is changed.
3984 */
3985 char *
3986did_set_toolbar(optset_T *args UNUSED)
3987{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003988 if (opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags,
3989 TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003990 return e_invalid_argument;
3991
3992 out_flush();
3993 gui_mch_show_toolbar((toolbar_flags &
3994 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
3995 return NULL;
3996}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003997
3998 int
3999expand_set_toolbar(optexpand_T *args, int *numMatches, char_u ***matches)
4000{
4001 return expand_set_opt_string(
4002 args,
4003 p_toolbar_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004004 ARRAY_LENGTH(p_toolbar_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004005 numMatches,
4006 matches);
4007}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004008#endif
4009
4010#if (defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)) || defined(PROTO)
4011/*
4012 * The 'toolbariconsize' option is changed. GTK+ 2 only.
4013 */
4014 char *
4015did_set_toolbariconsize(optset_T *args UNUSED)
4016{
4017 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
4018 return e_invalid_argument;
4019
4020 out_flush();
4021 gui_mch_show_toolbar((toolbar_flags &
4022 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4023 return NULL;
4024}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004025
4026 int
4027expand_set_toolbariconsize(optexpand_T *args, int *numMatches, char_u ***matches)
4028{
4029 return expand_set_opt_string(
4030 args,
4031 p_tbis_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004032 ARRAY_LENGTH(p_tbis_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004033 numMatches,
4034 matches);
4035}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004036#endif
4037
4038#if defined(UNIX) || defined(VMS) || defined(PROTO)
4039/*
4040 * The 'ttymouse' option is changed.
4041 */
4042 char *
4043did_set_ttymouse(optset_T *args UNUSED)
4044{
4045 char *errmsg = NULL;
4046
4047 // Switch the mouse off before changing the escape sequences used for
4048 // that.
4049 mch_setmouse(FALSE);
4050 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
4051 errmsg = e_invalid_argument;
4052 else
4053 check_mouse_termcode();
4054 if (termcap_active)
4055 setmouse(); // may switch it on again
4056
4057 return errmsg;
4058}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004059
4060 int
4061expand_set_ttymouse(optexpand_T *args, int *numMatches, char_u ***matches)
4062{
4063 return expand_set_opt_string(
4064 args,
4065 p_ttym_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004066 ARRAY_LENGTH(p_ttym_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004067 numMatches,
4068 matches);
4069}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004070#endif
4071
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004072#if defined(FEAT_VARTABS) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004073/*
4074 * The 'varsofttabstop' option is changed.
4075 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004076 char *
4077did_set_varsofttabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004078{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004079 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004080 char_u *cp;
4081
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004082 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004083 VIM_CLEAR(curbuf->b_p_vsts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004084 else
4085 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004086 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004087 {
4088 if (vim_isdigit(*cp))
4089 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004090 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004091 continue;
4092 return e_invalid_argument;
4093 }
4094
4095 int *oldarray = curbuf->b_p_vsts_array;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004096 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004097 {
4098 if (oldarray)
4099 vim_free(oldarray);
4100 }
4101 else
4102 return e_invalid_argument;
4103 }
4104
4105 return NULL;
4106}
4107
4108/*
4109 * The 'vartabstop' option is changed.
4110 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004111 char *
4112did_set_vartabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004113{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004114 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004115 char_u *cp;
4116
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004117 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004118 VIM_CLEAR(curbuf->b_p_vts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004119 else
4120 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004121 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004122 {
4123 if (vim_isdigit(*cp))
4124 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004125 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004126 continue;
4127 return e_invalid_argument;
4128 }
4129
4130 int *oldarray = curbuf->b_p_vts_array;
4131
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004132 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004133 {
4134 vim_free(oldarray);
4135# ifdef FEAT_FOLDING
4136 if (foldmethodIsIndent(curwin))
4137 foldUpdateAll(curwin);
4138# endif
4139 }
4140 else
4141 return e_invalid_argument;
4142 }
4143
4144 return NULL;
4145}
4146#endif
4147
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004148/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004149 * The 'verbosefile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004150 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004151 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004152did_set_verbosefile(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004153{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004154 verbose_stop();
4155 if (*p_vfile != NUL && verbose_open() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004156 return e_invalid_argument;
4157
4158 return NULL;
4159}
4160
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004161#if defined(FEAT_SESSION) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004162/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004163 * The 'viewoptions' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004164 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004165 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004166did_set_viewoptions(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004167{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004168 return did_set_opt_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004169}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004170#endif
4171
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004172#if defined(FEAT_VIMINFO) || defined(PROTO)
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004173/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004174 * The 'viminfo' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004175 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004176 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004177did_set_viminfo(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004178{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004179 char_u *s;
4180 char *errmsg = NULL;
4181
4182 for (s = p_viminfo; *s;)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004183 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004184 // Check it's a valid character
4185 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
4186 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01004187 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004188 break;
4189 }
4190 if (*s == 'n') // name is always last one
4191 break;
4192 else if (*s == 'r') // skip until next ','
4193 {
4194 while (*++s && *s != ',')
4195 ;
4196 }
4197 else if (*s == '%')
4198 {
4199 // optional number
4200 while (vim_isdigit(*++s))
4201 ;
4202 }
4203 else if (*s == '!' || *s == 'h' || *s == 'c')
4204 ++s; // no extra chars
4205 else // must have a number
4206 {
4207 while (vim_isdigit(*++s))
4208 ;
4209
4210 if (!VIM_ISDIGIT(*(s - 1)))
4211 {
4212 if (args->os_errbuf != NULL)
4213 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01004214 vim_snprintf(args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004215 _(e_missing_number_after_angle_str_angle),
4216 transchar_byte(*(s - 1)));
4217 errmsg = args->os_errbuf;
4218 }
4219 else
4220 errmsg = "";
4221 break;
4222 }
4223 }
4224 if (*s == ',')
4225 ++s;
4226 else if (*s)
4227 {
4228 if (args->os_errbuf != NULL)
4229 errmsg = e_missing_comma;
4230 else
4231 errmsg = "";
4232 break;
4233 }
4234 }
4235 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
4236 errmsg = e_must_specify_a_value;
4237
4238 return errmsg;
4239}
4240#endif
4241
4242/*
4243 * The 'virtualedit' option is changed.
4244 */
4245 char *
4246did_set_virtualedit(optset_T *args)
4247{
4248 char_u *ve = p_ve;
4249 unsigned int *flags = &ve_flags;
4250
4251 if (args->os_flags & OPT_LOCAL)
4252 {
4253 ve = curwin->w_p_ve;
4254 flags = &curwin->w_ve_flags;
4255 }
4256
4257 if ((args->os_flags & OPT_LOCAL) && *ve == NUL)
4258 // make the local value empty: use the global value
4259 *flags = 0;
4260 else
4261 {
4262 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
4263 return e_invalid_argument;
4264 else if (STRCMP(ve, args->os_oldval.string) != 0)
4265 {
4266 // Recompute cursor position in case the new 've' setting
4267 // changes something.
4268 validate_virtcol();
4269 coladvance(curwin->w_virtcol);
4270 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004271 }
4272
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004273 return NULL;
4274}
4275
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004276 int
4277expand_set_virtualedit(optexpand_T *args, int *numMatches, char_u ***matches)
4278{
4279 return expand_set_opt_string(
4280 args,
4281 p_ve_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004282 ARRAY_LENGTH(p_ve_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004283 numMatches,
4284 matches);
4285}
4286
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004287/*
4288 * The 'whichwrap' option is changed.
4289 */
4290 char *
4291did_set_whichwrap(optset_T *args)
4292{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004293 char_u **varp = (char_u **)args->os_varp;
4294
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004295 // Add ',' to the list flags because 'whichwrap' is a flag
4296 // list that is comma-separated.
Christian Brabandtb39b2402023-11-29 11:34:05 +01004297 return did_set_option_listflag(*varp, (char_u *)(WW_ALL ","),
4298 args->os_errbuf, args->os_errbuflen);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004299}
4300
4301 int
4302expand_set_whichwrap(optexpand_T *args, int *numMatches, char_u ***matches)
4303{
4304 return expand_set_opt_listflag(args, (char_u*)WW_ALL, numMatches, matches);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004305}
4306
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004307/*
4308 * The 'wildmode' option is changed.
4309 */
4310 char *
4311did_set_wildmode(optset_T *args UNUSED)
4312{
4313 if (check_opt_wim() == FAIL)
4314 return e_invalid_argument;
4315 return NULL;
4316}
4317
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004318 int
4319expand_set_wildmode(optexpand_T *args, int *numMatches, char_u ***matches)
4320{
4321 return expand_set_opt_string(
4322 args,
4323 p_wim_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004324 ARRAY_LENGTH(p_wim_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004325 numMatches,
4326 matches);
4327}
4328
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004329/*
4330 * The 'wildoptions' option is changed.
4331 */
4332 char *
4333did_set_wildoptions(optset_T *args UNUSED)
4334{
4335 return did_set_opt_strings(p_wop, p_wop_values, TRUE);
4336}
4337
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004338 int
4339expand_set_wildoptions(optexpand_T *args, int *numMatches, char_u ***matches)
4340{
4341 return expand_set_opt_string(
4342 args,
4343 p_wop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004344 ARRAY_LENGTH(p_wop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004345 numMatches,
4346 matches);
4347}
4348
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004349#if defined(FEAT_WAK) || defined(PROTO)
4350/*
4351 * The 'winaltkeys' option is changed.
4352 */
4353 char *
4354did_set_winaltkeys(optset_T *args UNUSED)
4355{
4356 char *errmsg = NULL;
4357
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004358 if (*p_wak == NUL || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004359 errmsg = e_invalid_argument;
4360# ifdef FEAT_MENU
4361# if defined(FEAT_GUI_MOTIF)
4362 else if (gui.in_use)
4363 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4364# elif defined(FEAT_GUI_GTK)
4365 else if (gui.in_use)
4366 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4367# endif
4368# endif
4369 return errmsg;
4370}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004371
4372 int
4373expand_set_winaltkeys(optexpand_T *args, int *numMatches, char_u ***matches)
4374{
4375 return expand_set_opt_string(
4376 args,
4377 p_wak_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004378 ARRAY_LENGTH(p_wak_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004379 numMatches,
4380 matches);
4381}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004382#endif
4383
4384/*
4385 * The 'wincolor' option is changed.
4386 */
4387 char *
4388did_set_wincolor(optset_T *args UNUSED)
4389{
4390#ifdef FEAT_TERMINAL
4391 term_update_wincolor(curwin);
4392#endif
4393 return NULL;
4394}
4395
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004396 int
4397expand_set_wincolor(optexpand_T *args, int *numMatches, char_u ***matches)
4398{
4399 return expand_set_opt_generic(
4400 args,
4401 get_highlight_name,
4402 numMatches,
4403 matches);
4404}
4405
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004406#ifdef FEAT_SYN_HL
4407/*
4408 * When the 'syntax' option is set, load the syntax of that name.
4409 */
4410 static void
4411do_syntax_autocmd(int value_changed)
4412{
4413 static int syn_recursive = 0;
4414
4415 ++syn_recursive;
4416 // Only pass TRUE for "force" when the value changed or not used
4417 // recursively, to avoid endless recurrence.
4418 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
4419 value_changed || syn_recursive == 1, curbuf);
4420 curbuf->b_flags |= BF_SYN_SET;
4421 --syn_recursive;
4422}
4423#endif
4424
4425/*
4426 * When the 'filetype' option is set, trigger the FileType autocommand.
4427 */
4428 static void
4429do_filetype_autocmd(char_u **varp, int opt_flags, int value_changed)
4430{
4431 // Skip this when called from a modeline and the filetype was already set
4432 // to this value.
4433 if ((opt_flags & OPT_MODELINE) && !value_changed)
4434 return;
4435
4436 static int ft_recursive = 0;
4437 int secure_save = secure;
4438
4439 // Reset the secure flag, since the value of 'filetype' has
4440 // been checked to be safe.
4441 secure = 0;
4442
4443 ++ft_recursive;
zeertzjq5bf6c212024-03-31 18:41:27 +02004444 curbuf->b_did_filetype = TRUE;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004445 // Only pass TRUE for "force" when the value changed or not
4446 // used recursively, to avoid endless recurrence.
4447 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
4448 value_changed || ft_recursive == 1, curbuf);
4449 --ft_recursive;
4450 // Just in case the old "curbuf" is now invalid.
4451 if (varp != &(curbuf->b_p_ft))
4452 varp = NULL;
4453
4454 secure = secure_save;
4455}
4456
4457#ifdef FEAT_SPELL
4458/*
4459 * When the 'spelllang' option is set, source the spell/LANG.vim file in
4460 * 'runtimepath'.
4461 */
4462 static void
4463do_spelllang_source(void)
4464{
4465 char_u fname[200];
4466 char_u *p;
4467 char_u *q = curwin->w_s->b_p_spl;
4468
4469 // Skip the first name if it is "cjk".
4470 if (STRNCMP(q, "cjk,", 4) == 0)
4471 q += 4;
4472
4473 // They could set 'spellcapcheck' depending on the language. Use the first
4474 // name in 'spelllang' up to '_region' or '.encoding'.
4475 for (p = q; *p != NUL; ++p)
4476 if (!ASCII_ISALNUM(*p) && *p != '-')
4477 break;
4478 if (p > q)
4479 {
4480 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
4481 (int)(p - q), q);
4482 source_runtime(fname, DIP_ALL);
4483 }
4484}
4485#endif
4486
4487/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004488 * Handle string options that need some action to perform when changed.
zeertzjqf6782732022-07-27 18:26:03 +01004489 * The new value must be allocated.
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004490 * Returns NULL for success, or an untranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +02004491 */
4492 char *
4493did_set_string_option(
4494 int opt_idx, // index in options[] table
4495 char_u **varp, // pointer to the option variable
Bram Moolenaardac13472019-09-16 21:06:21 +02004496 char_u *oldval, // previous value of the option
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004497 char_u *value, // new value of the option
Bram Moolenaardac13472019-09-16 21:06:21 +02004498 char *errbuf, // buffer for errors, or NULL
Mike Williams620f0112023-12-05 15:36:06 +01004499 size_t errbuflen, // length of error buffer
Bram Moolenaardac13472019-09-16 21:06:21 +02004500 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004501 set_op_T op, // OP_ADDING/OP_PREPENDING/OP_REMOVING
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004502 int *value_checked) // value was checked to be safe, no
Bram Moolenaardac13472019-09-16 21:06:21 +02004503 // need to set P_INSECURE
4504{
4505 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +02004506 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004507 opt_did_set_cb_T did_set_cb = get_option_did_set_cb(opt_idx);
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004508 optset_T args;
4509
4510 // 'ttytype' is an alias for 'term'. Both 'term' and 'ttytype' point to
4511 // T_NAME. If 'term' or 'ttytype' is modified, then use the index for the
4512 // 'term' option. Only set the P_ALLOCED flag on 'term'.
4513 if (varp == &T_NAME)
4514 {
4515 opt_idx = findoption((char_u *)"term");
4516 if (opt_idx >= 0)
4517 {
4518 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
4519 did_set_cb = get_option_did_set_cb(opt_idx);
4520 }
4521 }
4522
4523 CLEAR_FIELD(args);
Bram Moolenaardac13472019-09-16 21:06:21 +02004524
Bram Moolenaardac13472019-09-16 21:06:21 +02004525 // Disallow changing some options from secure mode
4526 if ((secure
4527#ifdef HAVE_SANDBOX
4528 || sandbox != 0
4529#endif
4530 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00004531 errmsg = e_not_allowed_here;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004532 // Check for a "normal" directory or file name in some options.
4533 else if (check_illegal_path_names(opt_idx, varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004534 errmsg = e_invalid_argument;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004535 else if (did_set_cb != NULL)
4536 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004537 args.os_varp = (char_u *)varp;
4538 args.os_idx = opt_idx;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004539 args.os_flags = opt_flags;
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004540 args.os_op = op;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004541 args.os_oldval.string = oldval;
4542 args.os_newval.string = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004543 args.os_errbuf = errbuf;
Christian Brabandtb39b2402023-11-29 11:34:05 +01004544 args.os_errbuflen = errbuflen;
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004545 // Invoke the option specific callback function to validate and apply
4546 // the new option value.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004547 errmsg = did_set_cb(&args);
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004548
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004549 // The 'keymap', 'filetype' and 'syntax' option callback functions
4550 // may change the os_value_checked field.
4551 *value_checked = args.os_value_checked;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004552 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004553
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004554 // If an error is detected, restore the previous value.
Bram Moolenaardac13472019-09-16 21:06:21 +02004555 if (errmsg != NULL)
4556 {
zeertzjqf6782732022-07-27 18:26:03 +01004557 free_string_option(*varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02004558 *varp = oldval;
4559 // When resetting some values, need to act on it.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004560 if (args.os_restore_chartab)
Bram Moolenaardac13472019-09-16 21:06:21 +02004561 (void)init_chartab();
4562 if (varp == &p_hl)
4563 (void)highlight_changed();
4564 }
4565 else
4566 {
4567#ifdef FEAT_EVAL
4568 // Remember where the option was set.
4569 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4570#endif
4571 // Free string options that are in allocated memory.
4572 // Use "free_oldval", because recursiveness may change the flags under
4573 // our fingers (esp. init_highlight()).
4574 if (free_oldval)
4575 free_string_option(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01004576 set_option_flag(opt_idx, P_ALLOCED);
Bram Moolenaardac13472019-09-16 21:06:21 +02004577
4578 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
4579 && is_global_local_option(opt_idx))
4580 {
4581 // global option with local value set to use global value; free
4582 // the local value and make it empty
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004583 char_u *p = get_option_varp_scope(opt_idx, OPT_LOCAL);
Bram Moolenaardac13472019-09-16 21:06:21 +02004584 free_string_option(*(char_u **)p);
4585 *(char_u **)p = empty_option;
4586 }
4587
4588 // May set global value for local option.
4589 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
4590 set_string_option_global(opt_idx, varp);
4591
4592 // Trigger the autocommand only after setting the flags.
4593#ifdef FEAT_SYN_HL
Bram Moolenaardac13472019-09-16 21:06:21 +02004594 if (varp == &(curbuf->b_p_syn))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004595 do_syntax_autocmd(args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004596#endif
4597 else if (varp == &(curbuf->b_p_ft))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004598 do_filetype_autocmd(varp, opt_flags, args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004599#ifdef FEAT_SPELL
4600 if (varp == &(curwin->w_s->b_p_spl))
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004601 do_spelllang_source();
Bram Moolenaardac13472019-09-16 21:06:21 +02004602#endif
4603 }
4604
Bram Moolenaardac13472019-09-16 21:06:21 +02004605 if (varp == &p_mouse)
4606 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004607 if (*p_mouse == NUL)
4608 mch_setmouse(FALSE); // switch mouse off
4609 else
Bram Moolenaardac13472019-09-16 21:06:21 +02004610 setmouse(); // in case 'mouse' changed
4611 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004612
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004613#if defined(FEAT_LUA) || defined(PROTO)
4614 if (varp == &p_rtp)
4615 update_package_paths_in_lua();
4616#endif
4617
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004618#if defined(FEAT_LINEBREAK)
4619 // Changing Formatlistpattern when briopt includes the list setting:
4620 // redraw
4621 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
4622 && curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004623 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004624#endif
4625
Bram Moolenaardac13472019-09-16 21:06:21 +02004626 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004627 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0
4628 && (get_option_flags(opt_idx) & P_HLONLY) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02004629 curwin->w_set_curswant = TRUE;
4630
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004631 if ((opt_flags & OPT_NO_REDRAW) == 0)
4632 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004633#ifdef FEAT_GUI
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004634 // set when changing an option that only requires a redraw in the GUI
4635 int redraw_gui_only = FALSE;
4636
4637 if (varp == &p_go // 'guioptions'
4638 || varp == &p_guifont // 'guifont'
4639# ifdef FEAT_GUI_TABLINE
4640 || varp == &p_gtl // 'guitablabel'
4641 || varp == &p_gtt // 'guitabtooltip'
4642# endif
4643# ifdef FEAT_XFONTSET
4644 || varp == &p_guifontset // 'guifontset'
4645# endif
4646 || varp == &p_guifontwide // 'guifontwide'
Erik S. V. Jansson2f026382024-02-26 22:23:05 +01004647# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004648 || varp == &p_guiligatures // 'guiligatures'
4649# endif
4650 )
4651 redraw_gui_only = TRUE;
4652
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004653 // check redraw when it's not a GUI option or the GUI is active.
4654 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02004655#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004656 check_redraw(get_option_flags(opt_idx));
4657 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004658
4659#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004660 if (args.os_did_swaptcap)
Bram Moolenaardac13472019-09-16 21:06:21 +02004661 {
4662 set_termname((char_u *)"win32");
4663 init_highlight(TRUE, FALSE);
4664 }
4665#endif
4666
4667 return errmsg;
4668}
4669
4670/*
4671 * Check an option that can be a range of string values.
4672 *
4673 * Return OK for correct value, FAIL otherwise.
4674 * Empty is always OK.
4675 */
4676 static int
4677check_opt_strings(
4678 char_u *val,
4679 char **values,
4680 int list) // when TRUE: accept a list of values
4681{
4682 return opt_strings_flags(val, values, NULL, list);
4683}
4684
4685/*
4686 * Handle an option that can be a range of string values.
4687 * Set a flag in "*flagp" for each string present.
4688 *
4689 * Return OK for correct value, FAIL otherwise.
4690 * Empty is always OK.
4691 */
4692 static int
4693opt_strings_flags(
4694 char_u *val, // new value
4695 char **values, // array of valid string values
4696 unsigned *flagp,
4697 int list) // when TRUE: accept a list of values
4698{
4699 int i;
4700 int len;
4701 unsigned new_flags = 0;
4702
4703 while (*val)
4704 {
4705 for (i = 0; ; ++i)
4706 {
4707 if (values[i] == NULL) // val not found in values[]
4708 return FAIL;
4709
4710 len = (int)STRLEN(values[i]);
4711 if (STRNCMP(values[i], val, len) == 0
4712 && ((list && val[len] == ',') || val[len] == NUL))
4713 {
4714 val += len + (val[len] == ',');
4715 new_flags |= (1 << i);
4716 break; // check next item in val list
4717 }
4718 }
4719 }
4720 if (flagp != NULL)
4721 *flagp = new_flags;
4722
4723 return OK;
4724}
4725
4726/*
4727 * return OK if "p" is a valid fileformat name, FAIL otherwise.
4728 */
4729 int
4730check_ff_value(char_u *p)
4731{
4732 return check_opt_strings(p, p_ff_values, FALSE);
4733}
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004734
4735/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004736 * Save the actual shortmess Flags and clear them temporarily to avoid that
4737 * file messages overwrites any output from the following commands.
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004738 *
4739 * Caller must make sure to first call save_clear_shm_value() and then
4740 * restore_shm_value() exactly the same number of times.
4741 */
4742 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004743save_clear_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004744{
4745 if (STRLEN(p_shm) >= SHM_LEN)
4746 {
4747 iemsg(e_internal_error_shortmess_too_long);
4748 return;
4749 }
4750
4751 if (++set_shm_recursive == 1)
4752 {
4753 STRCPY(shm_buf, p_shm);
4754 set_option_value_give_err((char_u *)"shm", 0L, (char_u *)"", 0);
4755 }
4756}
4757
4758/*
4759 * Restore the shortmess Flags set from the save_clear_shm_value() function.
4760 */
4761 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004762restore_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004763{
4764 if (--set_shm_recursive == 0)
4765 {
4766 set_option_value_give_err((char_u *)"shm", 0L, shm_buf, 0);
4767 vim_memset(shm_buf, 0, SHM_LEN);
4768 }
4769}