blob: b6249a2f38840a184946e20fb68be4227f84c461 [file] [log] [blame]
Bram Moolenaardac13472019-09-16 21:06:21 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * optionstr.c: Functions related to string options
12 */
13
14#include "vim.h"
15
Christian Brabandt9aee8ec2022-12-16 16:41:23 +000016static char_u shm_buf[SHM_LEN];
17static int set_shm_recursive = 0;
18
Bram Moolenaardac13472019-09-16 21:06:21 +020019static char *(p_ambw_values[]) = {"single", "double", NULL};
20static char *(p_bg_values[]) = {"light", "dark", NULL};
21static char *(p_bkc_values[]) = {"yes", "auto", "no", "breaksymlink", "breakhardlink", NULL};
22static char *(p_bo_values[]) = {"all", "backspace", "cursor", "complete",
23 "copy", "ctrlg", "error", "esc", "ex",
24 "hangul", "insertmode", "lang", "mess",
25 "showmatch", "operator", "register", "shell",
LemonBoy77771d32022-04-13 11:47:25 +010026 "spell", "term", "wildmode", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020027#if defined(FEAT_LINEBREAK)
28// Note: Keep this in sync with briopt_check()
29static char *(p_briopt_values[]) = {"shift:", "min:", "sbr", "list:", "column:", NULL};
30#endif
31#if defined(FEAT_DIFF)
32// Note: Keep this in sync with diffopt_changed()
33static char *(p_dip_values[]) = {"filler", "context:", "iblank", "icase", "iwhite", "iwhiteall", "iwhiteeol", "horizontal", "vertical", "closeoff", "hiddenoff", "foldcolumn:", "followwrap", "internal", "indent-heuristic", "algorithm:", NULL};
34static char *(p_dip_algorithm_values[]) = {"myers", "minimal", "patience", "histogram", NULL};
35#endif
distobs25ac6d62024-07-06 17:50:09 +020036static char *(p_nf_values[]) = {"bin", "octal", "hex", "alpha", "unsigned", "blank", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020037static char *(p_ff_values[]) = {FF_UNIX, FF_DOS, FF_MAC, NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020038#ifdef FEAT_CLIPBOARD
39// Note: Keep this in sync with did_set_clipboard()
40static char *(p_cb_values[]) = {"unnamed", "unnamedplus", "autoselect", "autoselectplus", "autoselectml", "html", "exclude:", NULL};
41#endif
Bram Moolenaardac13472019-09-16 21:06:21 +020042#ifdef FEAT_CRYPT
Christian Brabandtf573c6e2021-06-20 14:02:16 +020043static char *(p_cm_values[]) = {"zip", "blowfish", "blowfish2",
44 # ifdef FEAT_SODIUM
Christian Brabandtaae58342023-04-23 17:50:22 +010045 "xchacha20", "xchacha20v2",
Christian Brabandtf573c6e2021-06-20 14:02:16 +020046 # endif
47 NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020048#endif
49static char *(p_cmp_values[]) = {"internal", "keepascii", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020050#ifdef FEAT_SYN_HL
51// Note: Keep this in sync with fill_culopt_flags()
52static char *(p_culopt_values[]) = {"line", "screenline", "number", "both", NULL};
53#endif
Bram Moolenaardac13472019-09-16 21:06:21 +020054static char *(p_dy_values[]) = {"lastline", "truncate", "uhex", NULL};
Yegappan Lakshmanan87018252023-09-20 20:20:04 +020055static char *(p_jop_values[]) = {"stack", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020056#ifdef FEAT_FOLDING
57static char *(p_fdo_values[]) = {"all", "block", "hor", "mark", "percent",
58 "quickfix", "search", "tag", "insert",
59 "undo", "jump", NULL};
60#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +020061// Note: Keep this in sync with match_keyprotocol()
62static char *(p_kpc_protocol_values[]) = {"none", "mok2", "kitty", NULL};
63#ifdef FEAT_PROP_POPUP
64// Note: Keep this in sync with parse_popup_option()
65static char *(p_popup_option_values[]) = {"height:", "width:", "highlight:", "border:", "align:", NULL};
66static char *(p_popup_option_border_values[]) = {"on", "off", NULL};
67static char *(p_popup_option_align_values[]) = {"item", "menu", NULL};
68#endif
69#if defined(FEAT_SPELL)
70// Note: Keep this in sync with spell_check_sps()
71static char *(p_sps_values[]) = {"best", "fast", "double", "expr:", "file:", "timeout:", NULL};
72#endif
Bram Moolenaardac13472019-09-16 21:06:21 +020073#ifdef FEAT_SESSION
Bram Moolenaar635bd602021-04-16 19:58:22 +020074// Also used for 'viewoptions'! Keep in sync with SSOP_ flags.
Bram Moolenaardac13472019-09-16 21:06:21 +020075static char *(p_ssop_values[]) = {"buffers", "winpos", "resize", "winsize",
76 "localoptions", "options", "help", "blank", "globals", "slash", "unix",
Bram Moolenaar635bd602021-04-16 19:58:22 +020077 "sesdir", "curdir", "folds", "cursor", "tabpages", "terminal", "skiprtp",
78 NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020079#endif
Bram Moolenaar539aa6b2019-11-17 18:09:38 +010080// Keep in sync with SWB_ flags in option.h
81static char *(p_swb_values[]) = {"useopen", "usetab", "split", "newtab", "vsplit", "uselast", NULL};
Luuk van Baal13ece2a2022-10-03 15:28:08 +010082static char *(p_spk_values[]) = {"cursor", "screen", "topline", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020083static char *(p_tc_values[]) = {"followic", "ignore", "match", "followscs", "smart", NULL};
LemonBoy5247b0b2024-07-12 19:30:58 +020084// Keep in sync with TCL_ flags in option.h
85static char *(p_tcl_values[]) = {"left", "uselast", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020086#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
87static char *(p_toolbar_values[]) = {"text", "icons", "tooltips", "horiz", NULL};
88#endif
89#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
90static char *(p_tbis_values[]) = {"tiny", "small", "medium", "large", "huge", "giant", NULL};
91#endif
Bram Moolenaara1cb1d12019-10-17 23:00:07 +020092#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +020093static char *(p_ttym_values[]) = {"xterm", "xterm2", "dec", "netterm", "jsbterm", "pterm", "urxvt", "sgr", NULL};
94#endif
Gary Johnson53ba05b2021-07-26 22:19:10 +020095static char *(p_ve_values[]) = {"block", "insert", "all", "onemore", "none", "NONE", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020096// Note: Keep this in sync with check_opt_wim()
97static char *(p_wim_values[]) = {"full", "longest", "list", "lastused", NULL};
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000098static char *(p_wop_values[]) = {"fuzzy", "tagfile", "pum", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020099#ifdef FEAT_WAK
100static char *(p_wak_values[]) = {"yes", "menu", "no", NULL};
101#endif
102static char *(p_mousem_values[]) = {"extend", "popup", "popup_setpos", "mac", NULL};
103static char *(p_sel_values[]) = {"inclusive", "exclusive", "old", NULL};
104static char *(p_slm_values[]) = {"mouse", "key", "cmd", NULL};
105static char *(p_km_values[]) = {"startsel", "stopsel", NULL};
106#ifdef FEAT_BROWSE
107static char *(p_bsdir_values[]) = {"current", "last", "buffer", NULL};
108#endif
109static char *(p_scbopt_values[]) = {"ver", "hor", "jump", NULL};
110static char *(p_debug_values[]) = {"msg", "throw", "beep", NULL};
111static char *(p_ead_values[]) = {"both", "ver", "hor", NULL};
112static char *(p_buftype_values[]) = {"nofile", "nowrite", "quickfix", "help", "terminal", "acwrite", "prompt", "popup", NULL};
113static char *(p_bufhidden_values[]) = {"hide", "unload", "delete", "wipe", NULL};
Bram Moolenaaraa0489e2020-04-17 19:41:21 +0200114static char *(p_bs_values[]) = {"indent", "eol", "start", "nostop", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200115#ifdef FEAT_FOLDING
116static char *(p_fdm_values[]) = {"manual", "expr", "marker", "indent", "syntax",
117# ifdef FEAT_DIFF
118 "diff",
119# endif
120 NULL};
121static char *(p_fcl_values[]) = {"all", NULL};
122#endif
glepnir600a12d2024-07-09 18:51:29 +0200123static char *(p_cot_values[]) = {"menu", "menuone", "longest", "preview", "popup", "popuphidden", "noinsert", "noselect", "fuzzy", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200124#ifdef BACKSLASH_IN_FILENAME
125static char *(p_csl_values[]) = {"slash", "backslash", NULL};
126#endif
127#ifdef FEAT_SIGNS
128static char *(p_scl_values[]) = {"yes", "no", "auto", "number", NULL};
129#endif
130#if defined(MSWIN) && defined(FEAT_TERMINAL)
131static char *(p_twt_values[]) = {"winpty", "conpty", "", NULL};
132#endif
Luuk van Baalba936f62022-12-15 13:15:39 +0000133static char *(p_sloc_values[]) = {"last", "statusline", "tabline", NULL};
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +0000134static char *(p_sws_values[]) = {"fsync", "sync", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200135
136static int check_opt_strings(char_u *val, char **values, int list);
137static int opt_strings_flags(char_u *val, char **values, unsigned *flagp, int list);
138
139/*
140 * After setting various option values: recompute variables that depend on
141 * option values.
142 */
143 void
144didset_string_options(void)
145{
146 (void)opt_strings_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
147 (void)opt_strings_flags(p_bkc, p_bkc_values, &bkc_flags, TRUE);
148 (void)opt_strings_flags(p_bo, p_bo_values, &bo_flags, TRUE);
zeertzjq529b9ad2024-06-05 20:27:06 +0200149 (void)opt_strings_flags(p_cot, p_cot_values, &cot_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200150#ifdef FEAT_SESSION
151 (void)opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE);
152 (void)opt_strings_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
153#endif
154#ifdef FEAT_FOLDING
155 (void)opt_strings_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
156#endif
157 (void)opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE);
Yegappan Lakshmanan87018252023-09-20 20:20:04 +0200158 (void)opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200159 (void)opt_strings_flags(p_tc, p_tc_values, &tc_flags, FALSE);
160 (void)opt_strings_flags(p_ve, p_ve_values, &ve_flags, TRUE);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +0200161#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +0200162 (void)opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE);
163#endif
164#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
165 (void)opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags, TRUE);
166#endif
167#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
168 (void)opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE);
169#endif
Sean Dewar39c46b42022-05-12 17:44:29 +0100170 (void)opt_strings_flags(p_swb, p_swb_values, &swb_flags, TRUE);
LemonBoy5247b0b2024-07-12 19:30:58 +0200171 (void)opt_strings_flags(p_tcl, p_tcl_values, &tcl_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200172}
173
Yegappan Lakshmananf9dc2782023-05-11 15:02:56 +0100174#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200175/*
176 * Trigger the OptionSet autocommand.
177 * "opt_idx" is the index of the option being set.
178 * "opt_flags" can be OPT_LOCAL etc.
179 * "oldval" the old value
180 * "oldval_l" the old local value (only non-NULL if global and local value
181 * are set)
182 * "oldval_g" the old global value (only non-NULL if global and local value
183 * are set)
184 * "newval" the new value
185 */
186 void
zeertzjq269aa2b2022-11-28 11:36:50 +0000187trigger_optionset_string(
Bram Moolenaardac13472019-09-16 21:06:21 +0200188 int opt_idx,
189 int opt_flags,
190 char_u *oldval,
191 char_u *oldval_l,
192 char_u *oldval_g,
193 char_u *newval)
194{
195 // Don't do this recursively.
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000196 if (oldval == NULL || newval == NULL
197 || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
198 return;
Bram Moolenaardac13472019-09-16 21:06:21 +0200199
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000200 char_u buf_type[7];
201
202 sprintf((char *)buf_type, "%s",
Bram Moolenaardac13472019-09-16 21:06:21 +0200203 (opt_flags & OPT_LOCAL) ? "local" : "global");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000204 set_vim_var_string(VV_OPTION_OLD, oldval, -1);
205 set_vim_var_string(VV_OPTION_NEW, newval, -1);
206 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
207 if (opt_flags & OPT_LOCAL)
208 {
209 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
210 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
Bram Moolenaardac13472019-09-16 21:06:21 +0200211 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000212 if (opt_flags & OPT_GLOBAL)
213 {
214 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
215 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval, -1);
216 }
217 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
218 {
219 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
220 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval_l, -1);
221 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval_g, -1);
222 }
223 if (opt_flags & OPT_MODELINE)
224 {
225 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
226 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
227 }
228 apply_autocmds(EVENT_OPTIONSET,
229 get_option_fullname(opt_idx), NULL, FALSE,
230 NULL);
231 reset_v_option_vars();
Bram Moolenaardac13472019-09-16 21:06:21 +0200232}
233#endif
234
235 static char *
Mike Williams620f0112023-12-05 15:36:06 +0100236illegal_char(char *errbuf, size_t errbuflen, int c)
Bram Moolenaardac13472019-09-16 21:06:21 +0200237{
238 if (errbuf == NULL)
239 return "";
zeertzjq6a8d2e12024-01-17 20:54:49 +0100240 vim_snprintf(errbuf, errbuflen, _(e_illegal_character_str),
Christian Brabandtb39b2402023-11-29 11:34:05 +0100241 (char *)transchar(c));
Bram Moolenaardac13472019-09-16 21:06:21 +0200242 return errbuf;
243}
244
245/*
246 * Check string options in a buffer for NULL value.
247 */
248 void
249check_buf_options(buf_T *buf)
250{
251 check_string_option(&buf->b_p_bh);
252 check_string_option(&buf->b_p_bt);
253 check_string_option(&buf->b_p_fenc);
254 check_string_option(&buf->b_p_ff);
255#ifdef FEAT_FIND_ID
256 check_string_option(&buf->b_p_def);
257 check_string_option(&buf->b_p_inc);
258# ifdef FEAT_EVAL
259 check_string_option(&buf->b_p_inex);
260# endif
261#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +0100262#if defined(FEAT_EVAL)
Bram Moolenaardac13472019-09-16 21:06:21 +0200263 check_string_option(&buf->b_p_inde);
264 check_string_option(&buf->b_p_indk);
265#endif
266#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
267 check_string_option(&buf->b_p_bexpr);
268#endif
269#if defined(FEAT_CRYPT)
270 check_string_option(&buf->b_p_cm);
271#endif
272 check_string_option(&buf->b_p_fp);
273#if defined(FEAT_EVAL)
274 check_string_option(&buf->b_p_fex);
275#endif
276#ifdef FEAT_CRYPT
277 check_string_option(&buf->b_p_key);
278#endif
279 check_string_option(&buf->b_p_kp);
280 check_string_option(&buf->b_p_mps);
281 check_string_option(&buf->b_p_fo);
282 check_string_option(&buf->b_p_flp);
283 check_string_option(&buf->b_p_isk);
Bram Moolenaardac13472019-09-16 21:06:21 +0200284 check_string_option(&buf->b_p_com);
Bram Moolenaardac13472019-09-16 21:06:21 +0200285#ifdef FEAT_FOLDING
286 check_string_option(&buf->b_p_cms);
287#endif
288 check_string_option(&buf->b_p_nf);
Bram Moolenaardac13472019-09-16 21:06:21 +0200289 check_string_option(&buf->b_p_qe);
Bram Moolenaardac13472019-09-16 21:06:21 +0200290#ifdef FEAT_SYN_HL
291 check_string_option(&buf->b_p_syn);
292 check_string_option(&buf->b_s.b_syn_isk);
293#endif
294#ifdef FEAT_SPELL
295 check_string_option(&buf->b_s.b_p_spc);
296 check_string_option(&buf->b_s.b_p_spf);
297 check_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +0200298 check_string_option(&buf->b_s.b_p_spo);
Bram Moolenaardac13472019-09-16 21:06:21 +0200299#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200300 check_string_option(&buf->b_p_sua);
Bram Moolenaardac13472019-09-16 21:06:21 +0200301 check_string_option(&buf->b_p_cink);
302 check_string_option(&buf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +0100303 check_string_option(&buf->b_p_cinsd);
Bram Moolenaardac13472019-09-16 21:06:21 +0200304 parse_cino(buf);
Bram Moolenaar49846fb2022-10-15 16:05:33 +0100305 check_string_option(&buf->b_p_lop);
Bram Moolenaardac13472019-09-16 21:06:21 +0200306 check_string_option(&buf->b_p_ft);
Bram Moolenaardac13472019-09-16 21:06:21 +0200307 check_string_option(&buf->b_p_cinw);
zeertzjq529b9ad2024-06-05 20:27:06 +0200308 check_string_option(&buf->b_p_cot);
Bram Moolenaardac13472019-09-16 21:06:21 +0200309 check_string_option(&buf->b_p_cpt);
310#ifdef FEAT_COMPL_FUNC
311 check_string_option(&buf->b_p_cfu);
312 check_string_option(&buf->b_p_ofu);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +0100313 check_string_option(&buf->b_p_tsrfu);
Bram Moolenaardac13472019-09-16 21:06:21 +0200314#endif
315#ifdef FEAT_EVAL
316 check_string_option(&buf->b_p_tfu);
317#endif
318#ifdef FEAT_KEYMAP
319 check_string_option(&buf->b_p_keymap);
320#endif
321#ifdef FEAT_QUICKFIX
322 check_string_option(&buf->b_p_gp);
323 check_string_option(&buf->b_p_mp);
324 check_string_option(&buf->b_p_efm);
325#endif
326 check_string_option(&buf->b_p_ep);
327 check_string_option(&buf->b_p_path);
328 check_string_option(&buf->b_p_tags);
329 check_string_option(&buf->b_p_tc);
330 check_string_option(&buf->b_p_dict);
331 check_string_option(&buf->b_p_tsr);
Bram Moolenaardac13472019-09-16 21:06:21 +0200332 check_string_option(&buf->b_p_lw);
Bram Moolenaardac13472019-09-16 21:06:21 +0200333 check_string_option(&buf->b_p_bkc);
334 check_string_option(&buf->b_p_menc);
335#ifdef FEAT_VARTABS
336 check_string_option(&buf->b_p_vsts);
337 check_string_option(&buf->b_p_vts);
338#endif
339}
340
341/*
342 * Free the string allocated for an option.
343 * Checks for the string being empty_option. This may happen if we're out of
344 * memory, vim_strsave() returned NULL, which was replaced by empty_option by
345 * check_options().
346 * Does NOT check for P_ALLOCED flag!
347 */
348 void
349free_string_option(char_u *p)
350{
351 if (p != empty_option)
352 vim_free(p);
353}
354
355 void
356clear_string_option(char_u **pp)
357{
358 if (*pp != empty_option)
359 vim_free(*pp);
360 *pp = empty_option;
361}
362
363 void
364check_string_option(char_u **pp)
365{
366 if (*pp == NULL)
367 *pp = empty_option;
368}
369
370/*
371 * Set global value for string option when it's a local option.
372 */
373 static void
374set_string_option_global(
375 int opt_idx, // option index
376 char_u **varp) // pointer to option variable
377{
378 char_u **p, *s;
379
380 // the global value is always allocated
381 if (is_window_local_option(opt_idx))
382 p = (char_u **)GLOBAL_WO(varp);
383 else
384 p = (char_u **)get_option_var(opt_idx);
385 if (!is_global_option(opt_idx)
386 && p != varp
387 && (s = vim_strsave(*varp)) != NULL)
388 {
389 free_string_option(*p);
390 *p = s;
391 }
392}
393
394/*
395 * Set a string option to a new value (without checking the effect).
396 * The string is copied into allocated memory.
397 * if ("opt_idx" == -1) "name" is used, otherwise "opt_idx" is used.
398 * When "set_sid" is zero set the scriptID to current_sctx.sc_sid. When
399 * "set_sid" is SID_NONE don't set the scriptID. Otherwise set the scriptID to
400 * "set_sid".
401 */
402 void
403set_string_option_direct(
404 char_u *name,
405 int opt_idx,
406 char_u *val,
407 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
408 int set_sid UNUSED)
409{
410 char_u *s;
411 char_u **varp;
412 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
413 int idx = opt_idx;
414
415 if (idx == -1) // use name
416 {
417 idx = findoption(name);
418 if (idx < 0) // not found (should not happen)
419 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000420 semsg(_(e_internal_error_str), "set_string_option_direct()");
RestorerZ68ebcee2023-05-31 17:12:14 +0100421 siemsg("For option %s", name);
Bram Moolenaardac13472019-09-16 21:06:21 +0200422 return;
423 }
424 }
425
426 if (is_hidden_option(idx)) // can't set hidden option
427 return;
428
429 s = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000430 if (s == NULL)
431 return;
432
433 varp = (char_u **)get_option_varp_scope(idx,
434 both ? OPT_LOCAL : opt_flags);
435 if ((opt_flags & OPT_FREE) && (get_option_flags(idx) & P_ALLOCED))
436 free_string_option(*varp);
437 *varp = s;
438
439 // For buffer/window local option may also set the global value.
440 if (both)
441 set_string_option_global(idx, varp);
442
443 set_option_flag(idx, P_ALLOCED);
444
445 // When setting both values of a global option with a local value,
446 // make the local value empty, so that the global value is used.
447 if (is_global_local_option(idx) && both)
Bram Moolenaardac13472019-09-16 21:06:21 +0200448 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000449 free_string_option(*varp);
450 *varp = empty_option;
Bram Moolenaardac13472019-09-16 21:06:21 +0200451 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000452# ifdef FEAT_EVAL
453 if (set_sid != SID_NONE)
454 {
455 sctx_T script_ctx;
456
457 if (set_sid == 0)
458 script_ctx = current_sctx;
459 else
460 {
461 script_ctx.sc_sid = set_sid;
462 script_ctx.sc_seq = 0;
463 script_ctx.sc_lnum = 0;
464 script_ctx.sc_version = 1;
465 }
466 set_option_sctx_idx(idx, opt_flags, script_ctx);
467 }
468# endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200469}
470
Dominique Pellee764d1b2023-03-12 21:20:59 +0000471#if defined(FEAT_PROP_POPUP) || \
472 (defined(FEAT_DIFF) && defined(FEAT_FOLDING)) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200473/*
474 * Like set_string_option_direct(), but for a window-local option in "wp".
475 * Blocks autocommands to avoid the old curwin becoming invalid.
476 */
477 void
478set_string_option_direct_in_win(
479 win_T *wp,
480 char_u *name,
481 int opt_idx,
482 char_u *val,
483 int opt_flags,
484 int set_sid)
485{
486 win_T *save_curwin = curwin;
487
488 block_autocmds();
489 curwin = wp;
490 curbuf = curwin->w_buffer;
491 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
492 curwin = save_curwin;
493 curbuf = curwin->w_buffer;
494 unblock_autocmds();
495}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000496#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200497
Dominique Pelle748b3082022-01-08 12:41:16 +0000498#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200499/*
500 * Like set_string_option_direct(), but for a buffer-local option in "buf".
501 * Blocks autocommands to avoid the old curbuf becoming invalid.
502 */
503 void
504set_string_option_direct_in_buf(
505 buf_T *buf,
506 char_u *name,
507 int opt_idx,
508 char_u *val,
509 int opt_flags,
510 int set_sid)
511{
512 buf_T *save_curbuf = curbuf;
513
514 block_autocmds();
515 curbuf = buf;
516 curwin->w_buffer = curbuf;
517 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
518 curbuf = save_curbuf;
519 curwin->w_buffer = curbuf;
520 unblock_autocmds();
521}
Dominique Pelle748b3082022-01-08 12:41:16 +0000522#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200523
524/*
525 * Set a string option to a new value, and handle the effects.
526 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100527 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaardac13472019-09-16 21:06:21 +0200528 */
529 char *
530set_string_option(
531 int opt_idx,
532 char_u *value,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +0000533 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Christian Brabandtb39b2402023-11-29 11:34:05 +0100534 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +0100535 size_t errbuflen)
Bram Moolenaardac13472019-09-16 21:06:21 +0200536{
537 char_u *s;
538 char_u **varp;
539 char_u *oldval;
540#if defined(FEAT_EVAL)
541 char_u *oldval_l = NULL;
542 char_u *oldval_g = NULL;
543 char_u *saved_oldval = NULL;
544 char_u *saved_oldval_l = NULL;
545 char_u *saved_oldval_g = NULL;
546 char_u *saved_newval = NULL;
547#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100548 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +0200549 int value_checked = FALSE;
550
551 if (is_hidden_option(opt_idx)) // don't set hidden option
552 return NULL;
553
Bram Moolenaar7f009df2020-03-16 20:27:38 +0100554 s = vim_strsave(value == NULL ? (char_u *)"" : value);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000555 if (s == NULL)
556 return NULL;
557
558 varp = (char_u **)get_option_varp_scope(opt_idx,
559 (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
560 ? (is_global_local_option(opt_idx)
561 ? OPT_GLOBAL : OPT_LOCAL)
562 : opt_flags);
563 oldval = *varp;
564#if defined(FEAT_EVAL)
565 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +0200566 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000567 oldval_l = *(char_u **)get_option_varp_scope(opt_idx, OPT_LOCAL);
568 oldval_g = *(char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
Bram Moolenaardac13472019-09-16 21:06:21 +0200569 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000570#endif
571 *varp = s;
572
573#if defined(FEAT_EVAL)
574 if (!starting
575# ifdef FEAT_CRYPT
576 && !is_crypt_key_option(opt_idx)
577# endif
578 )
579 {
580 if (oldval_l != NULL)
581 saved_oldval_l = vim_strsave(oldval_l);
582 if (oldval_g != NULL)
583 saved_oldval_g = vim_strsave(oldval_g);
584 saved_oldval = vim_strsave(oldval);
585 saved_newval = vim_strsave(s);
586 }
587#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000588 if ((errmsg = did_set_string_option(opt_idx, varp, oldval, value, errbuf,
Christian Brabandtb39b2402023-11-29 11:34:05 +0100589 errbuflen, opt_flags, OP_NONE, &value_checked)) == NULL)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000590 did_set_option(opt_idx, opt_flags, TRUE, value_checked);
591
592#if defined(FEAT_EVAL)
593 // call autocommand after handling side effects
594 if (errmsg == NULL)
595 trigger_optionset_string(opt_idx, opt_flags,
596 saved_oldval, saved_oldval_l,
597 saved_oldval_g, saved_newval);
598 vim_free(saved_oldval);
599 vim_free(saved_oldval_l);
600 vim_free(saved_oldval_g);
601 vim_free(saved_newval);
602#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100603 return errmsg;
Bram Moolenaardac13472019-09-16 21:06:21 +0200604}
605
606/*
607 * Return TRUE if "val" is a valid 'filetype' name.
608 * Also used for 'syntax' and 'keymap'.
609 */
610 static int
611valid_filetype(char_u *val)
612{
613 return valid_name(val, ".-_");
614}
615
616#ifdef FEAT_STL_OPT
617/*
618 * Check validity of options with the 'statusline' format.
zeertzjq5dc294a2022-04-15 13:17:57 +0100619 * Return an untranslated error message or NULL.
Bram Moolenaardac13472019-09-16 21:06:21 +0200620 */
621 static char *
622check_stl_option(char_u *s)
623{
Bram Moolenaardac13472019-09-16 21:06:21 +0200624 int groupdepth = 0;
Christian Brabandtb39b2402023-11-29 11:34:05 +0100625 static char errbuf[ERR_BUFLEN];
626 int errbuflen = ERR_BUFLEN;
Bram Moolenaardac13472019-09-16 21:06:21 +0200627
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100628 while (*s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200629 {
630 // Check for valid keys after % sequences
631 while (*s && *s != '%')
632 s++;
633 if (!*s)
634 break;
635 s++;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +0000636 if (*s == '%' || *s == STL_TRUNCMARK || *s == STL_SEPARATE)
Bram Moolenaardac13472019-09-16 21:06:21 +0200637 {
638 s++;
639 continue;
640 }
641 if (*s == ')')
642 {
643 s++;
644 if (--groupdepth < 0)
645 break;
646 continue;
647 }
648 if (*s == '-')
649 s++;
650 while (VIM_ISDIGIT(*s))
651 s++;
652 if (*s == STL_USER_HL)
653 continue;
654 if (*s == '.')
655 {
656 s++;
657 while (*s && VIM_ISDIGIT(*s))
658 s++;
659 }
660 if (*s == '(')
661 {
662 groupdepth++;
663 continue;
664 }
665 if (vim_strchr(STL_ALL, *s) == NULL)
666 {
Christian Brabandtb39b2402023-11-29 11:34:05 +0100667 return illegal_char(errbuf, errbuflen, *s);
Bram Moolenaardac13472019-09-16 21:06:21 +0200668 }
669 if (*s == '{')
670 {
zeertzjq5dc294a2022-04-15 13:17:57 +0100671 int reevaluate = (*++s == '%');
shadmansaleh30e3de22021-05-15 17:23:28 +0200672
zeertzjq5dc294a2022-04-15 13:17:57 +0100673 if (reevaluate && *++s == '}')
674 // "}" is not allowed immediately after "%{%"
Christian Brabandtb39b2402023-11-29 11:34:05 +0100675 return illegal_char(errbuf, errbuflen, '}');
shadmansaleh30e3de22021-05-15 17:23:28 +0200676 while ((*s != '}' || (reevaluate && s[-1] != '%')) && *s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200677 s++;
678 if (*s != '}')
zeertzjq5dc294a2022-04-15 13:17:57 +0100679 return e_unclosed_expression_sequence;
Bram Moolenaardac13472019-09-16 21:06:21 +0200680 }
681 }
Bram Moolenaardac13472019-09-16 21:06:21 +0200682 if (groupdepth != 0)
zeertzjq5dc294a2022-04-15 13:17:57 +0100683 return e_unbalanced_groups;
Bram Moolenaardac13472019-09-16 21:06:21 +0200684 return NULL;
685}
686#endif
687
688/*
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +0000689 * Check for a "normal" directory or file name in some options. Disallow a
690 * path separator (slash and/or backslash), wildcards and characters that are
691 * often illegal in a file name. Be more permissive if "secure" is off.
692 */
693 static int
694check_illegal_path_names(int opt_idx, char_u **varp)
695{
696 return (((get_option_flags(opt_idx) & P_NFNAME)
697 && vim_strpbrk(*varp, (char_u *)(secure
698 ? "/\\*?[|;&<>\r\n" : "/\\*?[<>\r\n")) != NULL)
699 || ((get_option_flags(opt_idx) & P_NDNAME)
700 && vim_strpbrk(*varp, (char_u *)"*?[|;&<>\r\n") != NULL));
701}
702
703/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000704 * An option that accepts a list of flags is changed.
705 * e.g. 'viewoptions', 'switchbuf', 'casemap', etc.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000706 */
707 static char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000708did_set_opt_flags(char_u *val, char **values, unsigned *flagp, int list)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000709{
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000710 if (opt_strings_flags(val, values, flagp, list) == FAIL)
711 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000712
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000713 return NULL;
714}
715
716/*
717 * An option that accepts a list of string values is changed.
718 * e.g. 'nrformats', 'scrollopt', 'wildoptions', etc.
719 */
720 static char *
721did_set_opt_strings(char_u *val, char **values, int list)
722{
723 return did_set_opt_flags(val, values, NULL, list);
724}
725
726/*
727 * An option which is a list of flags is set. Valid values are in 'flags'.
728 */
729 static char *
Christian Brabandtb39b2402023-11-29 11:34:05 +0100730did_set_option_listflag(
731 char_u *val,
732 char_u *flags,
733 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +0100734 size_t errbuflen)
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000735{
736 char_u *s;
737
Yegappan Lakshmananc727b192023-03-03 12:26:15 +0000738 for (s = val; *s; ++s)
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000739 if (vim_strchr(flags, *s) == NULL)
Christian Brabandtb39b2402023-11-29 11:34:05 +0100740 return illegal_char(errbuf, errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000741
742 return NULL;
743}
744
745/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200746 * Expand an option that accepts a list of fixed string values with known
747 * number of items.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200748 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200749 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200750expand_set_opt_string(
751 optexpand_T *args,
752 char **values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +0200753 size_t numValues,
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200754 int *numMatches,
755 char_u ***matches)
756{
757 char_u *p;
758 regmatch_T *regmatch = args->oe_regmatch;
759 int include_orig_val = args->oe_include_orig_val;
760 char_u *option_val = args->oe_opt_value;
761
762 // Assume numValues is small since they are fixed enums, so just allocate
763 // upfront instead of needing two passes to calculate output size.
764 *matches = ALLOC_MULT(char_u *, numValues + 1);
765 if (*matches == NULL)
766 return FAIL;
767
768 int count = 0;
769
770 if (include_orig_val && *option_val != NUL)
771 {
772 p = vim_strsave(option_val);
773 if (p == NULL)
774 {
775 VIM_CLEAR(*matches);
776 return FAIL;
777 }
778 (*matches)[count++] = p;
779 }
780
781 for (char **val = values; *val != NULL; val++)
782 {
783 if (include_orig_val && *option_val != NUL)
784 {
785 if (STRCMP((char_u*)*val, option_val) == 0)
786 continue;
787 }
788 if (vim_regexec(regmatch, (char_u*)(*val), (colnr_T)0))
789 {
790 p = vim_strsave((char_u*)*val);
791 if (p == NULL)
792 {
793 if (count == 0)
794 {
795 VIM_CLEAR(*matches);
796 return FAIL;
797 }
798 else
799 break;
800 }
801 (*matches)[count++] = p;
802 }
803 }
804 if (count == 0)
805 {
806 VIM_CLEAR(*matches);
807 return FAIL;
808 }
809 *numMatches = count;
810 return OK;
811}
812
813static char_u *set_opt_callback_orig_option = NULL;
814static char_u *((*set_opt_callback_func)(expand_T *, int));
815
816/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200817 * Callback used by expand_set_opt_generic to also include the original value
818 * as the first item.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200819 */
820 static char_u *
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200821expand_set_opt_generic_cb(expand_T *xp, int idx)
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200822{
823 if (idx == 0)
824 {
825 if (set_opt_callback_orig_option != NULL)
826 return set_opt_callback_orig_option;
827 else
828 return (char_u *)""; // empty strings are ignored
829 }
830 return set_opt_callback_func(xp, idx - 1);
831}
832
833/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200834 * Expand an option with a callback that iterates through a list of possible
835 * names using an index.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200836 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200837 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200838expand_set_opt_generic(
839 optexpand_T *args,
840 char_u *((*func)(expand_T *, int)),
841 int *numMatches,
842 char_u ***matches)
843{
844 int ret;
845
846 set_opt_callback_orig_option = args->oe_include_orig_val ?
847 args->oe_opt_value : NULL;
848 set_opt_callback_func = func;
849
850 ret = ExpandGeneric(
851 (char_u*)"", // not using fuzzy as currently EXPAND_STRING_SETTING doesn't use it
852 args->oe_xp,
853 args->oe_regmatch,
854 matches,
855 numMatches,
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200856 expand_set_opt_generic_cb,
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200857 FALSE);
858
859 set_opt_callback_orig_option = NULL;
860 set_opt_callback_func = NULL;
861 return ret;
862}
863
Christian Brabandt9960ebc2023-10-05 22:17:09 +0200864# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200865static garray_T *expand_cb_ga;
866static optexpand_T *expand_cb_args;
867
868/*
869 * Callback provided to a function in expand_set_opt_callback. Will perform
870 * regex matching against the value and add to the list.
871 *
872 * Returns OK usually. Returns FAIL if it failed to allocate memory, and the
873 * caller should terminate the enumeration.
874 */
875 static int
876expand_set_opt_callback_cb(char_u *val)
877{
878 regmatch_T *regmatch = expand_cb_args->oe_regmatch;
879 expand_T *xp = expand_cb_args->oe_xp;
880 garray_T *ga = expand_cb_ga;
881 char_u *str;
882
883 if (val == NULL || *val == NUL)
884 return OK;
885
886 if (xp->xp_pattern[0] != NUL &&
887 !vim_regexec(regmatch, val, (colnr_T)0))
888 return OK;
889
890 str = vim_strsave_escaped(val, (char_u *)" \t\\");
891
892 if (str == NULL)
893 return FAIL;
894
895 if (ga_grow(ga, 1) == FAIL)
896 {
897 vim_free(str);
898 return FAIL;
899 }
900
901 ((char_u **)ga->ga_data)[ga->ga_len] = str;
902 ++ga->ga_len;
903 return OK;
904}
905
906/*
907 * Expand an option with a provided function that takes a callback. The
908 * function will enumerate through all options and call the callback to add it
909 * to the list.
910 *
911 * "func" is the enumerator function that will generate the list of options.
912 * "func_params" is a single parameter that will be passed to func.
913 */
914 static int
915expand_set_opt_callback(
916 optexpand_T *args,
917 void (*func)(optexpand_T *, void* params, int (*cb)(char_u *val)),
918 void *func_params,
919 int *numMatches,
920 char_u ***matches)
921{
922 garray_T ga;
923 int include_orig_val = args->oe_include_orig_val;
924 char_u *option_val = args->oe_opt_value;
925
926 ga_init2(&ga, sizeof(char *), 30);
927
928 if (include_orig_val && *option_val != NUL)
929 {
930 char_u *p = vim_strsave(option_val);
931 if (p == NULL)
932 return FAIL;
933 if (ga_grow(&ga, 1) == FAIL)
934 {
935 vim_free(p);
936 return FAIL;
937 }
938 ((char_u **)ga.ga_data)[ga.ga_len] = p;
939 ++ga.ga_len;
940 }
941
942 expand_cb_ga = &ga;
943 expand_cb_args = args;
944
945 func(args, func_params, expand_set_opt_callback_cb);
946
947 expand_cb_ga = NULL;
948 expand_cb_args = NULL;
949
950 *matches = ga.ga_data;
951 *numMatches = ga.ga_len;
952 return OK;
953}
Christian Brabandt9960ebc2023-10-05 22:17:09 +0200954#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200955
956/*
957 * Expand an option which is a list of flags.
958 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200959 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200960expand_set_opt_listflag(
961 optexpand_T *args,
962 char_u *flags,
963 int *numMatches,
964 char_u ***matches)
965{
966 char_u *p;
967 char_u *option_val = args->oe_opt_value;
968 char_u *cmdline_val = args->oe_set_arg;
969 int append = args->oe_append;
970 int include_orig_val = args->oe_include_orig_val && (*option_val != NUL);
971
Yee Cheng Chin6d113472023-10-02 21:38:39 +0200972 size_t num_flags = STRLEN(flags);
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200973
974 // Assume we only have small number of flags, so just allocate max size.
975 *matches = ALLOC_MULT(char_u *, num_flags + 1);
976 if (*matches == NULL)
977 return FAIL;
978
979 int count = 0;
980
981 if (include_orig_val)
982 {
983 p = vim_strsave(option_val);
984 if (p == NULL)
985 {
986 VIM_CLEAR(*matches);
987 return FAIL;
988 }
989 (*matches)[count++] = p;
990 }
991
992 for (char_u *flag = flags; *flag != NUL; flag++)
993 {
994 if (append && vim_strchr(option_val, *flag) != NULL)
995 continue;
996
997 if (vim_strchr(cmdline_val, *flag) == NULL)
998 {
999 if (include_orig_val
1000 && option_val[1] == NUL
1001 && *flag == option_val[0])
1002 {
1003 // This value is already used as the first choice as it's the
1004 // existing flag. Just skip it to avoid duplicate.
1005 continue;
1006 }
1007 p = vim_strnsave(flag, 1);
1008 if (p == NULL)
1009 {
1010 if (count == 0)
1011 {
1012 VIM_CLEAR(*matches);
1013 return FAIL;
1014 }
1015 else
1016 break;
1017 }
1018 (*matches)[count++] = p;
1019 }
1020 }
1021
1022 if (count == 0)
1023 {
1024 VIM_CLEAR(*matches);
1025 return FAIL;
1026 }
1027 *numMatches = count;
1028 return OK;
1029}
1030
1031/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001032 * The 'ambiwidth' option is changed.
1033 */
1034 char *
1035did_set_ambiwidth(optset_T *args UNUSED)
1036{
1037 if (check_opt_strings(p_ambw, p_ambw_values, FALSE) != OK)
1038 return e_invalid_argument;
1039
1040 return check_chars_options();
1041}
1042
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001043 int
1044expand_set_ambiwidth(optexpand_T *args, int *numMatches, char_u ***matches)
1045{
1046 return expand_set_opt_string(
1047 args,
1048 p_ambw_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001049 ARRAY_LENGTH(p_ambw_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001050 numMatches,
1051 matches);
1052}
1053
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001054/*
1055 * The 'background' option is changed.
1056 */
1057 char *
Gregory Anders83ad2722024-01-03 19:48:51 +01001058did_set_background(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001059{
1060 if (check_opt_strings(p_bg, p_bg_values, FALSE) == FAIL)
1061 return e_invalid_argument;
1062
Gregory Anders83ad2722024-01-03 19:48:51 +01001063 if (args->os_oldval.string != NULL && args->os_oldval.string[0] == *p_bg)
1064 // Value was not changed
1065 return NULL;
1066
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001067#ifdef FEAT_EVAL
1068 int dark = (*p_bg == 'd');
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001069#endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001070
1071 init_highlight(FALSE, FALSE);
1072
1073#ifdef FEAT_EVAL
1074 if (dark != (*p_bg == 'd')
1075 && get_var_value((char_u *)"g:colors_name") != NULL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001076 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001077 // The color scheme must have set 'background' back to another
1078 // value, that's not what we want here. Disable the color
1079 // scheme and set the colors again.
1080 do_unlet((char_u *)"g:colors_name", TRUE);
1081 free_string_option(p_bg);
1082 p_bg = vim_strsave((char_u *)(dark ? "dark" : "light"));
1083 check_string_option(&p_bg);
1084 init_highlight(FALSE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001085 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001086#endif
1087#ifdef FEAT_TERMINAL
1088 term_update_colors_all();
1089#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001090
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001091 return NULL;
1092}
1093
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001094 int
1095expand_set_background(optexpand_T *args, int *numMatches, char_u ***matches)
1096{
1097 return expand_set_opt_string(
1098 args,
1099 p_bg_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001100 ARRAY_LENGTH(p_bg_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001101 numMatches,
1102 matches);
1103}
1104
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001105/*
1106 * The 'backspace' option is changed.
1107 */
1108 char *
1109did_set_backspace(optset_T *args UNUSED)
1110{
1111 if (VIM_ISDIGIT(*p_bs))
1112 {
1113 if (*p_bs > '3' || p_bs[1] != NUL)
1114 return e_invalid_argument;
1115 }
1116 else if (check_opt_strings(p_bs, p_bs_values, TRUE) != OK)
1117 return e_invalid_argument;
1118
1119 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001120}
1121
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001122 int
1123expand_set_backspace(optexpand_T *args, int *numMatches, char_u ***matches)
1124{
1125 return expand_set_opt_string(
1126 args,
1127 p_bs_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001128 ARRAY_LENGTH(p_bs_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001129 numMatches,
1130 matches);
1131}
1132
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001133/*
1134 * The 'backupcopy' option is changed.
1135 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001136 char *
1137did_set_backupcopy(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001138{
1139 char_u *bkc = p_bkc;
1140 unsigned int *flags = &bkc_flags;
1141 char *errmsg = NULL;
1142
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001143 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001144 {
1145 bkc = curbuf->b_p_bkc;
1146 flags = &curbuf->b_bkc_flags;
1147 }
1148
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001149 if ((args->os_flags & OPT_LOCAL) && *bkc == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001150 // make the local value empty: use the global value
1151 *flags = 0;
1152 else
1153 {
1154 if (opt_strings_flags(bkc, p_bkc_values, flags, TRUE) != OK)
1155 errmsg = e_invalid_argument;
1156 if ((((int)*flags & BKC_AUTO) != 0)
1157 + (((int)*flags & BKC_YES) != 0)
1158 + (((int)*flags & BKC_NO) != 0) != 1)
1159 {
1160 // Must have exactly one of "auto", "yes" and "no".
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001161 (void)opt_strings_flags(args->os_oldval.string, p_bkc_values,
1162 flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001163 errmsg = e_invalid_argument;
1164 }
1165 }
1166
1167 return errmsg;
1168}
1169
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001170 int
1171expand_set_backupcopy(optexpand_T *args, int *numMatches, char_u ***matches)
1172{
1173 return expand_set_opt_string(
1174 args,
1175 p_bkc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001176 ARRAY_LENGTH(p_bkc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001177 numMatches,
1178 matches);
1179}
1180
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001181/*
1182 * The 'backupext' or the 'patchmode' option is changed.
1183 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001184 char *
1185did_set_backupext_or_patchmode(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001186{
1187 if (STRCMP(*p_bex == '.' ? p_bex + 1 : p_bex,
1188 *p_pm == '.' ? p_pm + 1 : p_pm) == 0)
1189 return e_backupext_and_patchmode_are_equal;
1190
1191 return NULL;
1192}
1193
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001194/*
1195 * The 'belloff' option is changed.
1196 */
1197 char *
1198did_set_belloff(optset_T *args UNUSED)
1199{
1200 return did_set_opt_flags(p_bo, p_bo_values, &bo_flags, TRUE);
1201}
1202
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001203 int
1204expand_set_belloff(optexpand_T *args, int *numMatches, char_u ***matches)
1205{
1206 return expand_set_opt_string(
1207 args,
1208 p_bo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001209 ARRAY_LENGTH(p_bo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001210 numMatches,
1211 matches);
1212}
1213
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001214#if defined(FEAT_LINEBREAK) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001215/*
zeertzjqeac3fdc2024-02-03 18:08:09 +01001216 * The 'breakat' option is changed.
1217 */
1218 char *
1219did_set_breakat(optset_T *args UNUSED)
1220{
1221 char_u *p;
1222 int i;
1223
1224 for (i = 0; i < 256; i++)
1225 breakat_flags[i] = FALSE;
1226
1227 if (p_breakat != NULL)
1228 for (p = p_breakat; *p; p++)
1229 breakat_flags[*p] = TRUE;
1230
1231 return NULL;
1232}
1233
1234/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001235 * The 'breakindentopt' option is changed.
1236 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001237 char *
1238did_set_breakindentopt(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001239{
1240 char *errmsg = NULL;
1241
1242 if (briopt_check(curwin) == FAIL)
1243 errmsg = e_invalid_argument;
1244 // list setting requires a redraw
1245 if (curwin->w_briopt_list)
1246 redraw_all_later(UPD_NOT_VALID);
1247
1248 return errmsg;
1249}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001250
1251 int
1252expand_set_breakindentopt(optexpand_T *args, int *numMatches, char_u ***matches)
1253{
1254 return expand_set_opt_string(
1255 args,
1256 p_briopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001257 ARRAY_LENGTH(p_briopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001258 numMatches,
1259 matches);
1260}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001261#endif
1262
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001263#if defined(FEAT_BROWSE) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001264/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001265 * The 'browsedir' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001266 */
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00001267 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001268did_set_browsedir(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001269{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001270 if (check_opt_strings(p_bsdir, p_bsdir_values, FALSE) != OK
1271 && !mch_isdir(p_bsdir))
1272 return e_invalid_argument;
1273
1274 return NULL;
1275}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001276
1277 int
1278expand_set_browsedir(optexpand_T *args, int *numMatches, char_u ***matches)
1279{
1280 return expand_set_opt_string(
1281 args,
1282 p_bsdir_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001283 ARRAY_LENGTH(p_bsdir_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001284 numMatches,
1285 matches);
1286}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001287#endif
1288
1289/*
1290 * The 'bufhidden' option is changed.
1291 */
1292 char *
1293did_set_bufhidden(optset_T *args UNUSED)
1294{
1295 return did_set_opt_strings(curbuf->b_p_bh, p_bufhidden_values, FALSE);
1296}
1297
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001298 int
1299expand_set_bufhidden(optexpand_T *args, int *numMatches, char_u ***matches)
1300{
1301 return expand_set_opt_string(
1302 args,
1303 p_bufhidden_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001304 ARRAY_LENGTH(p_bufhidden_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001305 numMatches,
1306 matches);
1307}
1308
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001309/*
1310 * The 'buftype' option is changed.
1311 */
1312 char *
1313did_set_buftype(optset_T *args UNUSED)
1314{
1315 if (check_opt_strings(curbuf->b_p_bt, p_buftype_values, FALSE) != OK)
1316 return e_invalid_argument;
1317
1318 if (curwin->w_status_height)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001319 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001320 curwin->w_redr_status = TRUE;
1321 redraw_later(UPD_VALID);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001322 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001323 curbuf->b_help = (curbuf->b_p_bt[0] == 'h');
1324 redraw_titles();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001325
1326 return NULL;
1327}
1328
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001329 int
1330expand_set_buftype(optexpand_T *args, int *numMatches, char_u ***matches)
1331{
1332 return expand_set_opt_string(
1333 args,
1334 p_buftype_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001335 ARRAY_LENGTH(p_buftype_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001336 numMatches,
1337 matches);
1338}
1339
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001340/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001341 * The 'casemap' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001342 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001343 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001344did_set_casemap(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001345{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001346 return did_set_opt_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
1347}
1348
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001349 int
1350expand_set_casemap(optexpand_T *args, int *numMatches, char_u ***matches)
1351{
1352 return expand_set_opt_string(
1353 args,
1354 p_cmp_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001355 ARRAY_LENGTH(p_cmp_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001356 numMatches,
1357 matches);
1358}
1359
1360#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1361 int
1362expand_set_clipboard(optexpand_T *args, int *numMatches, char_u ***matches)
1363{
1364 return expand_set_opt_string(
1365 args,
1366 p_cb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001367 ARRAY_LENGTH(p_cb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001368 numMatches,
1369 matches);
1370}
1371#endif
1372
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001373/*
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001374 * The global 'listchars' or 'fillchars' option is changed.
1375 */
1376 static char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01001377did_set_global_listfillchars(char_u *val, int opt_lcs, int opt_flags,
1378 char *errbuf, size_t errbuflen)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001379{
1380 char *errmsg = NULL;
1381 char_u **local_ptr = opt_lcs ? &curwin->w_p_lcs : &curwin->w_p_fcs;
1382
1383 // only apply the global value to "curwin" when it does not have a
1384 // local value
1385 if (opt_lcs)
1386 errmsg = set_listchars_option(curwin, val,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001387 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL),
1388 errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001389 else
1390 errmsg = set_fillchars_option(curwin, val,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001391 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL),
1392 errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001393 if (errmsg != NULL)
1394 return errmsg;
1395
1396 tabpage_T *tp;
1397 win_T *wp;
1398
1399 // If the current window is set to use the global
1400 // 'listchars'/'fillchars' value, clear the window-local value.
1401 if (!(opt_flags & OPT_GLOBAL))
1402 clear_string_option(local_ptr);
1403 FOR_ALL_TAB_WINDOWS(tp, wp)
1404 {
1405 // If the current window has a local value need to apply it
1406 // again, it was changed when setting the global value.
1407 // If no error was returned above, we don't expect an error
1408 // here, so ignore the return value.
1409 if (opt_lcs)
1410 {
1411 if (*wp->w_p_lcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001412 (void)set_listchars_option(wp, wp->w_p_lcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001413 }
1414 else
1415 {
1416 if (*wp->w_p_fcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001417 (void)set_fillchars_option(wp, wp->w_p_fcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001418 }
1419 }
1420
1421 redraw_all_later(UPD_NOT_VALID);
1422
1423 return NULL;
1424}
1425
1426/*
1427 * The 'fillchars' option or the 'listchars' option is changed.
1428 */
1429 char *
1430did_set_chars_option(optset_T *args)
1431{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001432 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001433 char *errmsg = NULL;
1434
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001435 if ( varp == &p_lcs // global 'listchars'
1436 || varp == &p_fcs) // global 'fillchars'
1437 errmsg = did_set_global_listfillchars(*varp, varp == &p_lcs,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001438 args->os_flags, args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001439 else if (varp == &curwin->w_p_lcs) // local 'listchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001440 errmsg = set_listchars_option(curwin, *varp, TRUE,
1441 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001442 else if (varp == &curwin->w_p_fcs) // local 'fillchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001443 errmsg = set_fillchars_option(curwin, *varp, TRUE,
1444 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001445
1446 return errmsg;
1447}
1448
1449/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001450 * Expand 'fillchars' or 'listchars' option value.
1451 */
1452 int
1453expand_set_chars_option(optexpand_T *args, int *numMatches, char_u ***matches)
1454{
1455 char_u **varp = (char_u **)args->oe_varp;
1456 int is_lcs = (varp == &p_lcs || varp == &curwin->w_p_lcs);
1457 return expand_set_opt_generic(
1458 args,
1459 is_lcs ? get_listchars_name : get_fillchars_name,
1460 numMatches,
1461 matches);
1462}
1463
1464/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001465 * The 'cinoptions' option is changed.
1466 */
1467 char *
1468did_set_cinoptions(optset_T *args UNUSED)
1469{
1470 // TODO: recognize errors
1471 parse_cino(curbuf);
1472
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001473 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001474}
1475
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00001476#if defined(FEAT_SYN_HL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001477/*
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001478 * The 'colorcolumn' option is changed.
1479 */
1480 char *
1481did_set_colorcolumn(optset_T *args UNUSED)
1482{
1483 return check_colorcolumn(curwin);
1484}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001485#endif
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001486
1487/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001488 * The 'comments' option is changed.
1489 */
1490 char *
1491did_set_comments(optset_T *args)
1492{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001493 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001494 char_u *s;
1495 char *errmsg = NULL;
1496
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001497 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001498 {
1499 while (*s && *s != ':')
1500 {
1501 if (vim_strchr((char_u *)COM_ALL, *s) == NULL
1502 && !VIM_ISDIGIT(*s) && *s != '-')
1503 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01001504 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001505 break;
1506 }
1507 ++s;
1508 }
1509 if (*s++ == NUL)
1510 errmsg = e_missing_colon;
1511 else if (*s == ',' || *s == NUL)
1512 errmsg = e_zero_length_string;
1513 if (errmsg != NULL)
1514 break;
1515 while (*s && *s != ',')
1516 {
1517 if (*s == '\\' && s[1] != NUL)
1518 ++s;
1519 ++s;
1520 }
1521 s = skip_to_option_part(s);
1522 }
1523
1524 return errmsg;
1525}
1526
1527#if defined(FEAT_FOLDING) || defined(PROTO)
1528/*
1529 * The 'commentstring' option is changed.
1530 */
1531 char *
1532did_set_commentstring(optset_T *args)
1533{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001534 char_u **varp = (char_u **)args->os_varp;
1535
1536 if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001537 return e_commentstring_must_be_empty_or_contain_str;
1538
1539 return NULL;
1540}
1541#endif
1542
1543/*
1544 * The 'complete' option is changed.
1545 */
1546 char *
1547did_set_complete(optset_T *args)
1548{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001549 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001550 char_u *s;
1551
1552 // check if it is a valid value for 'complete' -- Acevedo
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001553 for (s = *varp; *s;)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001554 {
1555 while (*s == ',' || *s == ' ')
1556 s++;
1557 if (!*s)
1558 break;
1559 if (vim_strchr((char_u *)".wbuksid]tU", *s) == NULL)
Christian Brabandtb39b2402023-11-29 11:34:05 +01001560 return illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001561 if (*++s != NUL && *s != ',' && *s != ' ')
1562 {
1563 if (s[-1] == 'k' || s[-1] == 's')
1564 {
1565 // skip optional filename after 'k' and 's'
1566 while (*s && *s != ',' && *s != ' ')
1567 {
1568 if (*s == '\\' && s[1] != NUL)
1569 ++s;
1570 ++s;
1571 }
1572 }
1573 else
1574 {
1575 if (args->os_errbuf != NULL)
1576 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01001577 vim_snprintf((char *)args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001578 _(e_illegal_character_after_chr), *--s);
1579 return args->os_errbuf;
1580 }
1581 return "";
1582 }
1583 }
1584 }
1585
1586 return NULL;
1587}
1588
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001589 int
1590expand_set_complete(optexpand_T *args, int *numMatches, char_u ***matches)
1591{
1592 static char *(p_cpt_values[]) = {
1593 ".", "w", "b", "u", "k", "kspell", "s", "i", "d", "]", "t", "U",
1594 NULL};
1595 return expand_set_opt_string(
1596 args,
1597 p_cpt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001598 ARRAY_LENGTH(p_cpt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001599 numMatches,
1600 matches);
1601}
1602
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001603/*
1604 * The 'completeopt' option is changed.
1605 */
1606 char *
1607did_set_completeopt(optset_T *args UNUSED)
1608{
zeertzjq529b9ad2024-06-05 20:27:06 +02001609 char_u *cot = p_cot;
1610 unsigned *flags = &cot_flags;
1611
1612 if (args->os_flags & OPT_LOCAL)
1613 {
1614 cot = curbuf->b_p_cot;
1615 flags = &curbuf->b_cot_flags;
1616 }
1617
1618 if (check_opt_strings(cot, p_cot_values, TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001619 return e_invalid_argument;
1620
zeertzjq529b9ad2024-06-05 20:27:06 +02001621 if (opt_strings_flags(cot, p_cot_values, flags, TRUE) != OK)
1622 return e_invalid_argument;
1623
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001624 return NULL;
1625}
1626
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001627 int
1628expand_set_completeopt(optexpand_T *args, int *numMatches, char_u ***matches)
1629{
1630 return expand_set_opt_string(
1631 args,
1632 p_cot_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001633 ARRAY_LENGTH(p_cot_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001634 numMatches,
1635 matches);
1636}
1637
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001638#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1639/*
1640 * The 'completepopup' option is changed.
1641 */
1642 char *
1643did_set_completepopup(optset_T *args UNUSED)
1644{
1645 if (parse_completepopup(NULL) == FAIL)
1646 return e_invalid_argument;
1647
1648 popup_close_info();
1649 return NULL;
1650}
1651#endif
1652
1653#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1654/*
1655 * The 'completeslash' option is changed.
1656 */
1657 char *
1658did_set_completeslash(optset_T *args UNUSED)
1659{
1660 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1661 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
1662 return e_invalid_argument;
1663
1664 return NULL;
1665}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001666
1667 int
1668expand_set_completeslash(optexpand_T *args, int *numMatches, char_u ***matches)
1669{
1670 return expand_set_opt_string(
1671 args,
1672 p_csl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001673 ARRAY_LENGTH(p_csl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001674 numMatches,
1675 matches);
1676}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001677#endif
1678
1679#if defined(FEAT_CONCEAL) || defined(PROTO)
1680/*
1681 * The 'concealcursor' option is changed.
1682 */
1683 char *
1684did_set_concealcursor(optset_T *args)
1685{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001686 char_u **varp = (char_u **)args->os_varp;
1687
Christian Brabandtb39b2402023-11-29 11:34:05 +01001688 return did_set_option_listflag(*varp, (char_u *)COCU_ALL, args->os_errbuf,
1689 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001690}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001691
1692 int
1693expand_set_concealcursor(optexpand_T *args, int *numMatches, char_u ***matches)
1694{
1695 return expand_set_opt_listflag(args, (char_u*)COCU_ALL, numMatches, matches);
1696}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001697#endif
1698
1699/*
1700 * The 'cpoptions' option is changed.
1701 */
1702 char *
1703did_set_cpoptions(optset_T *args)
1704{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001705 char_u **varp = (char_u **)args->os_varp;
1706
Christian Brabandtb39b2402023-11-29 11:34:05 +01001707 return did_set_option_listflag(*varp, (char_u *)CPO_ALL, args->os_errbuf,
1708 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001709}
1710
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001711 int
1712expand_set_cpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
1713{
1714 return expand_set_opt_listflag(args, (char_u*)CPO_ALL, numMatches, matches);
1715}
1716
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001717#if defined(FEAT_CRYPT) || defined(PROTO)
1718/*
1719 * The 'cryptkey' option is changed.
1720 */
1721 char *
1722did_set_cryptkey(optset_T *args)
1723{
1724 // Make sure the ":set" command doesn't show the new value in the
1725 // history.
1726 remove_key_from_history();
1727
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02001728 if (args->os_op != OP_NONE)
1729 // Don't allow set+=/-=/^= as they can allow for substring guessing
1730 return e_invalid_argument;
1731
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001732 if (STRCMP(curbuf->b_p_key, args->os_oldval.string) != 0)
1733 {
1734 // Need to update the swapfile.
1735 ml_set_crypt_key(curbuf, args->os_oldval.string,
1736 *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
1737 changed_internal();
1738 }
Christian Brabandt19e6c4f2023-06-27 18:57:10 +01001739# ifdef FEAT_SODIUM
1740 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
1741 crypt_sodium_lock_key(args->os_newval.string);
1742# endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001743
1744 return NULL;
1745}
1746
1747/*
1748 * The 'cryptmethod' option is changed.
1749 */
1750 char *
1751did_set_cryptmethod(optset_T *args)
1752{
1753 char_u *p;
1754 char_u *s;
1755
1756 if (args->os_flags & OPT_LOCAL)
1757 p = curbuf->b_p_cm;
1758 else
1759 p = p_cm;
1760 if (check_opt_strings(p, p_cm_values, TRUE) != OK)
1761 return e_invalid_argument;
1762 else if (crypt_self_test() == FAIL)
1763 return e_invalid_argument;
1764
1765 // When setting the global value to empty, make it "zip".
1766 if (*p_cm == NUL)
1767 {
1768 free_string_option(p_cm);
1769 p_cm = vim_strsave((char_u *)"zip");
1770 }
1771 // When using ":set cm=name" the local value is going to be empty.
1772 // Do that here, otherwise the crypt functions will still use the
1773 // local value.
1774 if ((args->os_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1775 {
1776 free_string_option(curbuf->b_p_cm);
1777 curbuf->b_p_cm = empty_option;
1778 }
1779
1780 // Need to update the swapfile when the effective method changed.
1781 // Set "s" to the effective old value, "p" to the effective new
1782 // method and compare.
1783 if ((args->os_flags & OPT_LOCAL) && *args->os_oldval.string == NUL)
1784 s = p_cm; // was previously using the global value
1785 else
1786 s = args->os_oldval.string;
1787 if (*curbuf->b_p_cm == NUL)
1788 p = p_cm; // is now using the global value
1789 else
1790 p = curbuf->b_p_cm;
1791 if (STRCMP(s, p) != 0)
1792 ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1793
1794 // If the global value changes need to update the swapfile for all
1795 // buffers using that value.
1796 if ((args->os_flags & OPT_GLOBAL)
1797 && STRCMP(p_cm, args->os_oldval.string) != 0)
1798 {
1799 buf_T *buf;
1800
1801 FOR_ALL_BUFFERS(buf)
1802 if (buf != curbuf && *buf->b_p_cm == NUL)
1803 ml_set_crypt_key(buf, buf->b_p_key, args->os_oldval.string);
1804 }
1805 return NULL;
1806}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001807
1808 int
1809expand_set_cryptmethod(optexpand_T *args, int *numMatches, char_u ***matches)
1810{
1811 return expand_set_opt_string(
1812 args,
1813 p_cm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001814 ARRAY_LENGTH(p_cm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001815 numMatches,
1816 matches);
1817}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001818#endif
1819
1820#if (defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1821/*
1822 * The 'cscopequickfix' option is changed.
1823 */
1824 char *
1825did_set_cscopequickfix(optset_T *args UNUSED)
1826{
1827 char_u *p;
1828
1829 if (p_csqf == NULL)
1830 return NULL;
1831
1832 p = p_csqf;
1833 while (*p != NUL)
1834 {
1835 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
1836 || p[1] == NUL
1837 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
1838 || (p[2] != NUL && p[2] != ','))
1839 return e_invalid_argument;
1840 else if (p[2] == NUL)
1841 break;
1842 else
1843 p += 3;
1844 }
1845
1846 return NULL;
1847}
1848#endif
1849
1850#if defined(FEAT_SYN_HL) || defined(PROTO)
1851/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001852 * The 'cursorlineopt' option is changed.
1853 */
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001854 char *
1855did_set_cursorlineopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001856{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001857 char_u **varp = (char_u **)args->os_varp;
1858
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001859 // This could be changed to use opt_strings_flags() instead.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001860 if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001861 return e_invalid_argument;
1862
1863 return NULL;
1864}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001865
1866 int
1867expand_set_cursorlineopt(optexpand_T *args, int *numMatches, char_u ***matches)
1868{
1869 return expand_set_opt_string(
1870 args,
1871 p_culopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001872 ARRAY_LENGTH(p_culopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001873 numMatches,
1874 matches);
1875}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001876#endif
1877
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001878/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001879 * The 'debug' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001880 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001881 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001882did_set_debug(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001883{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001884 return did_set_opt_strings(p_debug, p_debug_values, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001885}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001886
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001887 int
1888expand_set_debug(optexpand_T *args, int *numMatches, char_u ***matches)
1889{
1890 return expand_set_opt_string(
1891 args,
1892 p_debug_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001893 ARRAY_LENGTH(p_debug_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001894 numMatches,
1895 matches);
1896}
1897
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001898#if defined(FEAT_DIFF) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001899/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001900 * The 'diffopt' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001901 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001902 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001903did_set_diffopt(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001904{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001905 if (diffopt_changed() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001906 return e_invalid_argument;
1907
1908 return NULL;
1909}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001910
1911 int
1912expand_set_diffopt(optexpand_T *args, int *numMatches, char_u ***matches)
1913{
1914 expand_T *xp = args->oe_xp;
1915
1916 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
1917 {
1918 // Within "algorithm:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001919 int algo_len = (int)STRLEN("algorithm:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001920 if (xp->xp_pattern - args->oe_set_arg >= algo_len &&
1921 STRNCMP(xp->xp_pattern - algo_len, "algorithm:", algo_len) == 0)
1922 {
1923 return expand_set_opt_string(
1924 args,
1925 p_dip_algorithm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001926 ARRAY_LENGTH(p_dip_algorithm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001927 numMatches,
1928 matches);
1929 }
1930 return FAIL;
1931 }
1932
1933 return expand_set_opt_string(
1934 args,
1935 p_dip_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001936 ARRAY_LENGTH(p_dip_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001937 numMatches,
1938 matches);
1939}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001940#endif
1941
1942/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001943 * The 'display' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001944 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001945 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001946did_set_display(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001947{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001948 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001949 return e_invalid_argument;
1950
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001951 (void)init_chartab();
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001952 return NULL;
1953}
1954
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001955 int
1956expand_set_display(optexpand_T *args, int *numMatches, char_u ***matches)
1957{
1958 return expand_set_opt_string(
1959 args,
1960 p_dy_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001961 ARRAY_LENGTH(p_dy_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001962 numMatches,
1963 matches);
1964}
1965
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001966/*
1967 * The 'eadirection' option is changed.
1968 */
1969 char *
1970did_set_eadirection(optset_T *args UNUSED)
1971{
1972 return did_set_opt_strings(p_ead, p_ead_values, FALSE);
1973}
1974
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001975 int
1976expand_set_eadirection(optexpand_T *args, int *numMatches, char_u ***matches)
1977{
1978 return expand_set_opt_string(
1979 args,
1980 p_ead_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001981 ARRAY_LENGTH(p_ead_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001982 numMatches,
1983 matches);
1984}
1985
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001986/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001987 * One of the 'encoding', 'fileencoding', 'termencoding' or 'makeencoding'
1988 * options is changed.
1989 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001990 char *
1991did_set_encoding(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001992{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001993 char_u **varp = (char_u **)args->os_varp;
1994 char_u **gvarp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001995 char *errmsg = NULL;
1996 char_u *p;
1997
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001998 // Get the global option to compare with, otherwise we would have to check
1999 // two values for all local options.
2000 gvarp = (char_u **)get_option_varp_scope(args->os_idx, OPT_GLOBAL);
2001
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002002 if (gvarp == &p_fenc)
2003 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002004 if (!curbuf->b_p_ma && args->os_flags != OPT_GLOBAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002005 errmsg = e_cannot_make_changes_modifiable_is_off;
2006 else if (vim_strchr(*varp, ',') != NULL)
2007 // No comma allowed in 'fileencoding'; catches confusing it
2008 // with 'fileencodings'.
2009 errmsg = e_invalid_argument;
2010 else
2011 {
2012 // May show a "+" in the title now.
2013 redraw_titles();
2014 // Add 'fileencoding' to the swap file.
2015 ml_setflags(curbuf);
2016 }
2017 }
2018 if (errmsg == NULL)
2019 {
2020 // canonize the value, so that STRCMP() can be used on it
2021 p = enc_canonize(*varp);
2022 if (p != NULL)
2023 {
2024 vim_free(*varp);
2025 *varp = p;
2026 }
2027 if (varp == &p_enc)
2028 {
2029 errmsg = mb_init();
2030 redraw_titles();
2031 }
2032 }
2033
2034#if defined(FEAT_GUI_GTK)
2035 if (errmsg == NULL && varp == &p_tenc && gui.in_use)
2036 {
2037 // GTK uses only a single encoding, and that is UTF-8.
2038 if (STRCMP(p_tenc, "utf-8") != 0)
2039 errmsg = e_cannot_be_changed_in_gtk_GUI;
2040 }
2041#endif
2042
2043 if (errmsg == NULL)
2044 {
2045#ifdef FEAT_KEYMAP
2046 // When 'keymap' is used and 'encoding' changes, reload the keymap
2047 // (with another encoding).
2048 if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
2049 (void)keymap_init();
2050#endif
2051
2052 // When 'termencoding' is not empty and 'encoding' changes or when
2053 // 'termencoding' changes, need to setup for keyboard input and
2054 // display output conversion.
2055 if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
2056 {
2057 if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
2058 || convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
2059 {
2060 semsg(_(e_cannot_convert_between_str_and_str),
2061 p_tenc, p_enc);
2062 errmsg = e_invalid_argument;
2063 }
2064 }
2065
2066#if defined(MSWIN)
K.Takatace3189d2023-02-15 19:13:43 +00002067 // $HOME, $VIM and $VIMRUNTIME may have characters in active code page.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002068 if (varp == &p_enc)
K.Takatace3189d2023-02-15 19:13:43 +00002069 {
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002070 init_homedir();
K.Takatace3189d2023-02-15 19:13:43 +00002071 init_vimdir();
2072 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002073#endif
2074 }
2075
2076 return errmsg;
2077}
2078
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002079 int
2080expand_set_encoding(optexpand_T *args, int *numMatches, char_u ***matches)
2081{
2082 return expand_set_opt_generic(
2083 args,
2084 get_encoding_name,
2085 numMatches,
2086 matches);
2087}
2088
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002089/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002090 * The 'eventignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002091 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002092 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002093did_set_eventignore(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002094{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002095 if (check_ei() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002096 return e_invalid_argument;
2097 return NULL;
2098}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002099
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002100 static char_u *
2101get_eventignore_name(expand_T *xp, int idx)
2102{
2103 // 'eventignore' allows special keyword "all" in addition to
2104 // all event names.
2105 if (idx == 0)
2106 return (char_u *)"all";
2107 return get_event_name_no_group(xp, idx - 1);
2108}
2109
2110 int
2111expand_set_eventignore(optexpand_T *args, int *numMatches, char_u ***matches)
2112{
2113 return expand_set_opt_generic(
2114 args,
2115 get_eventignore_name,
2116 numMatches,
2117 matches);
2118}
2119
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002120/*
2121 * The 'fileformat' option is changed.
2122 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002123 char *
2124did_set_fileformat(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002125{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002126 char_u **varp = (char_u **)args->os_varp;
2127
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002128 if (!curbuf->b_p_ma && !(args->os_flags & OPT_GLOBAL))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002129 return e_cannot_make_changes_modifiable_is_off;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002130 else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002131 return e_invalid_argument;
2132
2133 // may also change 'textmode'
2134 if (get_fileformat(curbuf) == EOL_DOS)
2135 curbuf->b_p_tx = TRUE;
2136 else
2137 curbuf->b_p_tx = FALSE;
2138 redraw_titles();
2139 // update flag in swap file
2140 ml_setflags(curbuf);
2141 // Redraw needed when switching to/from "mac": a CR in the text
2142 // will be displayed differently.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002143 if (get_fileformat(curbuf) == EOL_MAC || *args->os_oldval.string == 'm')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002144 redraw_curbuf_later(UPD_NOT_VALID);
2145
2146 return NULL;
2147}
2148
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002149 int
2150expand_set_fileformat(optexpand_T *args, int *numMatches, char_u ***matches)
2151{
2152 return expand_set_opt_string(
2153 args,
2154 p_ff_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002155 ARRAY_LENGTH(p_ff_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002156 numMatches,
2157 matches);
2158}
2159
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002160/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002161 * Function given to ExpandGeneric() to obtain the possible arguments of the
2162 * fileformat options.
2163 */
2164 char_u *
2165get_fileformat_name(expand_T *xp UNUSED, int idx)
2166{
2167 if (idx >= (int)ARRAY_LENGTH(p_ff_values))
2168 return NULL;
2169
2170 return (char_u*)p_ff_values[idx];
2171}
2172
2173/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002174 * The 'fileformats' option is changed.
2175 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002176 char *
2177did_set_fileformats(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002178{
2179 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
2180 return e_invalid_argument;
2181
2182 // also change 'textauto'
2183 if (*p_ffs == NUL)
2184 p_ta = FALSE;
2185 else
2186 p_ta = TRUE;
2187
2188 return NULL;
2189}
2190
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002191/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002192 * The 'filetype' or the 'syntax' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002193 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002194 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002195did_set_filetype_or_syntax(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002196{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002197 char_u **varp = (char_u **)args->os_varp;
2198
2199 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002200 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002201
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002202 args->os_value_changed = STRCMP(args->os_oldval.string, *varp) != 0;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002203
2204 // Since we check the value, there is no need to set P_INSECURE,
2205 // even when the value comes from a modeline.
2206 args->os_value_checked = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002207
2208 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002209}
2210
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002211#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002212/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002213 * The 'foldclose' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002214 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002215 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002216did_set_foldclose(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002217{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002218 return did_set_opt_strings(p_fcl, p_fcl_values, TRUE);
2219}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002220
2221 int
2222expand_set_foldclose(optexpand_T *args, int *numMatches, char_u ***matches)
2223{
2224 return expand_set_opt_string(
2225 args,
2226 p_fcl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002227 ARRAY_LENGTH(p_fcl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002228 numMatches,
2229 matches);
2230}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002231#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002232
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002233#if (defined(FEAT_EVAL) && defined(FEAT_FOLDING)) || defined(PROTO)
2234/*
2235 * The 'foldexpr' option is changed.
2236 */
2237 char *
2238did_set_foldexpr(optset_T *args)
2239{
2240 (void)did_set_optexpr(args);
2241 if (foldmethodIsExpr(curwin))
2242 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002243 return NULL;
2244}
2245#endif
2246
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002247#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002248/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002249 * The 'foldignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002250 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002251 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002252did_set_foldignore(optset_T *args UNUSED)
2253{
2254 if (foldmethodIsIndent(curwin))
2255 foldUpdateAll(curwin);
2256 return NULL;
2257}
2258
2259/*
2260 * The 'foldmarker' option is changed.
2261 */
2262 char *
2263did_set_foldmarker(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002264{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002265 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002266 char_u *p;
2267
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002268 p = vim_strchr(*varp, ',');
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002269 if (p == NULL)
2270 return e_comma_required;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002271 else if (p == *varp || p[1] == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002272 return e_invalid_argument;
2273 else if (foldmethodIsMarker(curwin))
2274 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002275
2276 return NULL;
2277}
2278
2279/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002280 * The 'foldmethod' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002281 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002282 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002283did_set_foldmethod(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002284{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002285 char_u **varp = (char_u **)args->os_varp;
2286
2287 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002288 || *curwin->w_p_fdm == NUL)
2289 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002290
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002291 foldUpdateAll(curwin);
2292 if (foldmethodIsDiff(curwin))
2293 newFoldLevel();
2294 return NULL;
2295}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002296
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002297 int
2298expand_set_foldmethod(optexpand_T *args, int *numMatches, char_u ***matches)
2299{
2300 return expand_set_opt_string(
2301 args,
2302 p_fdm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002303 ARRAY_LENGTH(p_fdm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002304 numMatches,
2305 matches);
2306}
2307
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002308/*
2309 * The 'foldopen' option is changed.
2310 */
2311 char *
2312did_set_foldopen(optset_T *args UNUSED)
2313{
2314 return did_set_opt_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
2315}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002316
2317 int
2318expand_set_foldopen(optexpand_T *args, int *numMatches, char_u ***matches)
2319{
2320 return expand_set_opt_string(
2321 args,
2322 p_fdo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002323 ARRAY_LENGTH(p_fdo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002324 numMatches,
2325 matches);
2326}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002327#endif
2328
2329/*
2330 * The 'formatoptions' option is changed.
2331 */
2332 char *
2333did_set_formatoptions(optset_T *args)
2334{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002335 char_u **varp = (char_u **)args->os_varp;
2336
Christian Brabandtb39b2402023-11-29 11:34:05 +01002337 return did_set_option_listflag(*varp, (char_u *)FO_ALL, args->os_errbuf,
2338 args->os_errbuflen);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002339}
2340
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002341 int
2342expand_set_formatoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2343{
2344 return expand_set_opt_listflag(args, (char_u*)FO_ALL, numMatches, matches);
2345}
2346
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002347#if defined(CURSOR_SHAPE) || defined(PROTO)
2348/*
2349 * The 'guicursor' option is changed.
2350 */
2351 char *
2352did_set_guicursor(optset_T *args UNUSED)
2353{
2354 return parse_shape_opt(SHAPE_CURSOR);
2355}
2356#endif
2357
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002358#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002359/*
2360 * The 'guifont' option is changed.
2361 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002362 char *
2363did_set_guifont(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002364{
2365 char_u *p;
2366 char *errmsg = NULL;
2367
2368 if (gui.in_use)
2369 {
2370 p = p_guifont;
2371# if defined(FEAT_GUI_GTK)
2372 // Put up a font dialog and let the user select a new value.
2373 // If this is cancelled go back to the old value but don't
2374 // give an error message.
2375 if (STRCMP(p, "*") == 0)
2376 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002377 p = gui_mch_font_dialog(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002378 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002379 p_guifont = (p != NULL) ? p : vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002380 }
2381# endif
2382 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
2383 {
2384# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
2385 if (STRCMP(p_guifont, "*") == 0)
2386 {
2387 // Dialog was cancelled: Keep the old value without giving
2388 // an error message.
2389 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002390 p_guifont = vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002391 }
2392 else
2393# endif
2394 errmsg = e_invalid_fonts;
2395 }
2396 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002397
2398 return errmsg;
2399}
2400
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002401/*
2402 * Expand the 'guifont' option. Only when GUI is being used. Each platform has
2403 * specific behaviors.
2404 */
2405 int
2406expand_set_guifont(optexpand_T *args, int *numMatches, char_u ***matches)
2407{
2408 if (!gui.in_use)
2409 return FAIL;
2410
2411# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
2412 char_u **varp = (char_u **)args->oe_varp;
2413 int wide = (varp == &p_guifontwide);
2414
2415 return expand_set_opt_callback(
2416 args, gui_mch_expand_font, &wide, numMatches, matches);
2417# else
2418 return FAIL;
2419# endif
2420}
2421
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002422# if defined(FEAT_XFONTSET) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002423/*
2424 * The 'guifontset' option is changed.
2425 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002426 char *
2427did_set_guifontset(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002428{
2429 char *errmsg = NULL;
2430
2431 if (STRCMP(p_guifontset, "*") == 0)
2432 errmsg = e_cant_select_fontset;
2433 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
2434 errmsg = e_invalid_fontset;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002435
2436 return errmsg;
2437}
2438# endif
2439
2440/*
2441 * The 'guifontwide' option is changed.
2442 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002443 char *
2444did_set_guifontwide(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002445{
2446 char *errmsg = NULL;
2447
2448 if (STRCMP(p_guifontwide, "*") == 0)
2449 errmsg = e_cant_select_wide_font;
2450 else if (gui_get_wide_font() == FAIL)
2451 errmsg = e_invalid_wide_font;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002452
2453 return errmsg;
2454}
2455#endif
2456
Erik S. V. Jansson8b1e7492024-02-24 14:26:52 +01002457#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002458/*
2459 * The 'guiligatures' option is changed.
2460 */
2461 char *
2462did_set_guiligatures(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002463{
2464 gui_set_ligatures();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002465 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002466}
2467#endif
2468
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002469#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002470/*
2471 * The 'guioptions' option is changed.
2472 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002473 char *
2474did_set_guioptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002475{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002476 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002477 char *errmsg;
2478
Christian Brabandtb39b2402023-11-29 11:34:05 +01002479 errmsg = did_set_option_listflag(*varp, (char_u *)GO_ALL, args->os_errbuf,
2480 args->os_errbuflen);
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002481 if (errmsg != NULL)
2482 return errmsg;
2483
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002484 gui_init_which_components(args->os_oldval.string);
2485 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002486}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002487
2488 int
2489expand_set_guioptions(optexpand_T *args, int *numMatches, char_u ***matches)
2490{
2491 return expand_set_opt_listflag(args, (char_u*)GO_ALL, numMatches, matches);
2492}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002493#endif
2494
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002495#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002496/*
2497 * The 'guitablabel' option is changed.
2498 */
2499 char *
2500did_set_guitablabel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002501{
2502 redraw_tabline = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002503 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002504}
2505#endif
2506
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002507/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002508 * The 'helpfile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002509 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002510 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002511did_set_helpfile(optset_T *args UNUSED)
2512{
2513 // May compute new values for $VIM and $VIMRUNTIME
2514 if (didset_vim)
2515 vim_unsetenv_ext((char_u *)"VIM");
2516 if (didset_vimruntime)
2517 vim_unsetenv_ext((char_u *)"VIMRUNTIME");
2518 return NULL;
2519}
2520
2521#if defined(FEAT_MULTI_LANG) || defined(PROTO)
2522/*
2523 * The 'helplang' option is changed.
2524 */
2525 char *
2526did_set_helplang(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002527{
2528 char *errmsg = NULL;
2529
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002530 // Check for "", "ab", "ab,cd", etc.
2531 for (char_u *s = p_hlg; *s != NUL; s += 3)
2532 {
2533 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
2534 {
2535 errmsg = e_invalid_argument;
2536 break;
2537 }
2538 if (s[2] == NUL)
2539 break;
2540 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002541
2542 return errmsg;
2543}
2544#endif
2545
2546/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002547 * The 'highlight' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002548 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002549 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002550did_set_highlight(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002551{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002552 if (highlight_changed() == FAIL)
2553 return e_invalid_argument; // invalid flags
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002554
2555 return NULL;
2556}
2557
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002558/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002559 * Expand 'highlight' option.
2560 */
2561 int
2562expand_set_highlight(optexpand_T *args, int *numMatches, char_u ***matches)
2563{
2564 char_u *p;
2565 expand_T *xp = args->oe_xp;
2566 static char_u hl_flags[HLF_COUNT] = HL_FLAGS;
Christian Brabandt3f168ec2023-10-02 23:21:11 +02002567 size_t i;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002568 int count = 0;
2569
2570 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2571 {
2572 // Right after a ':', meaning we just return all highlight names.
2573 return expand_set_opt_generic(
2574 args,
2575 get_highlight_name,
2576 numMatches,
2577 matches);
2578 }
2579
2580 if (*xp->xp_pattern == NUL)
2581 {
2582 // At beginning of a comma-separated list. Return the specific list of
2583 // supported occasions.
2584 *matches = ALLOC_MULT(char_u *, HLF_COUNT + 1);
2585 if (*matches == NULL)
2586 return FAIL;
2587
2588 // We still want to return the full option if it's requested.
2589 if (args->oe_include_orig_val)
2590 {
2591 p = vim_strsave(args->oe_opt_value);
2592 if (p == NULL)
2593 {
2594 VIM_CLEAR(*matches);
2595 return FAIL;
2596 }
2597 (*matches)[count++] = p;
2598 }
2599
2600 for (i = 0; i < HLF_COUNT; i++)
2601 {
2602 p = vim_strnsave(&hl_flags[i], 1);
2603 if (p == NULL)
2604 {
2605 if (count == 0)
2606 {
2607 VIM_CLEAR(*matches);
2608 return FAIL;
2609 }
2610 else
2611 break;
2612 }
2613 (*matches)[count++] = p;
2614 }
2615
2616 if (count == 0)
2617 {
2618 VIM_CLEAR(*matches);
2619 return FAIL;
2620 }
2621 *numMatches = count;
2622 return OK;
2623 }
2624
2625 // We are after the initial character (which indicates the occasion). We
2626 // already made sure we are not matching after a ':' above, so now we want
2627 // to match against display mode modifiers.
2628 // Since the xp_pattern starts from the beginning, we need to include it in
2629 // the returned match.
2630
2631 // Note: Keep this in sync with highlight_changed()
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002632 static char_u p_hl_mode_values[] =
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002633 {':', 'b', 'i', '-', 'n', 'r', 's', 'u', 'c', '2', 'd', '=', 't'};
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002634 size_t num_hl_modes = ARRAY_LENGTH(p_hl_mode_values);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002635
2636 *matches = ALLOC_MULT(char_u *, num_hl_modes);
2637 if (*matches == NULL)
2638 return FAIL;
2639
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002640 int pattern_len = xp->xp_pattern_len;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002641
2642 for (i = 0; i < num_hl_modes; i++)
2643 {
2644 // Don't allow duplicates as these are unique flags
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002645 char_u *dup = vim_strchr(xp->xp_pattern + 1, p_hl_mode_values[i]);
2646 if (dup != NULL && (int)(dup - xp->xp_pattern) < pattern_len)
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002647 continue;
2648
2649 // ':' only works by itself, not with other flags.
2650 if (pattern_len > 1 && p_hl_mode_values[i] == ':')
2651 continue;
2652
2653 p = vim_strnsave(xp->xp_pattern, pattern_len + 1);
2654 if (p == NULL)
2655 {
2656 if (i == 0)
2657 {
2658 VIM_CLEAR(*matches);
2659 return FAIL;
2660 }
2661 else
2662 break;
2663 }
2664 p[pattern_len] = p_hl_mode_values[i];
2665 p[pattern_len + 1] = NUL;
2666 (*matches)[count++] = p;
2667 }
2668 if (count == 0)
2669 {
2670 VIM_CLEAR(*matches);
2671 return FAIL;
2672 }
2673 *numMatches = count;
2674
2675 return OK;
2676}
2677
2678/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002679 * The 'titlestring' or the 'iconstring' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002680 */
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002681 static char *
2682parse_titleiconstring(optset_T *args UNUSED, int flagval UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002683{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002684#ifdef FEAT_STL_OPT
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002685 char_u **varp = (char_u **)args->os_varp;
2686
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002687 // NULL => statusline syntax
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002688 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002689 stl_syntax |= flagval;
2690 else
2691 stl_syntax &= ~flagval;
2692#endif
2693 did_set_title();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002694
2695 return NULL;
2696}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002697
2698/*
2699 * The 'iconstring' option is changed.
2700 */
2701 char *
2702did_set_iconstring(optset_T *args)
2703{
2704 int flagval = 0;
2705
2706#ifdef FEAT_STL_OPT
2707 flagval = STL_IN_ICON;
2708#endif
2709
2710 return parse_titleiconstring(args, flagval);
2711}
2712
2713#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
2714/*
2715 * The 'imactivatekey' option is changed.
2716 */
2717 char *
2718did_set_imactivatekey(optset_T *args UNUSED)
2719{
2720 if (!im_xim_isvalid_imactivate())
2721 return e_invalid_argument;
2722 return NULL;
2723}
2724#endif
2725
2726/*
2727 * The 'isident' or the 'iskeyword' or the 'isprint' or the 'isfname' option is
2728 * changed.
2729 */
2730 char *
2731did_set_isopt(optset_T *args)
2732{
2733 // 'isident', 'iskeyword', 'isprint or 'isfname' option: refill g_chartab[]
2734 // If the new option is invalid, use old value.
2735 // 'lisp' option: refill g_chartab[] for '-' char.
2736 if (init_chartab() == FAIL)
2737 {
2738 args->os_restore_chartab = TRUE;// need to restore the chartab.
2739 return e_invalid_argument; // error in value
2740 }
2741
2742 return NULL;
2743}
2744
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002745/*
2746 * The 'jumpoptions' option is changed.
2747 */
2748 char *
Yegappan Lakshmanan1926ae42023-09-21 16:36:28 +02002749did_set_jumpoptions(optset_T *args UNUSED)
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002750{
2751 if (opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE) != OK)
2752 return e_invalid_argument;
2753
2754 return NULL;
2755}
2756
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002757 int
2758expand_set_jumpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2759{
2760 return expand_set_opt_string(
2761 args,
2762 p_jop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002763 ARRAY_LENGTH(p_jop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002764 numMatches,
2765 matches);
2766}
2767
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002768#if defined(FEAT_KEYMAP) || defined(PROTO)
2769/*
2770 * The 'keymap' option is changed.
2771 */
2772 char *
2773did_set_keymap(optset_T *args)
2774{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002775 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002776 char *errmsg = NULL;
2777
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002778 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002779 errmsg = e_invalid_argument;
2780 else
2781 {
2782 int secure_save = secure;
2783
2784 // Reset the secure flag, since the value of 'keymap' has
2785 // been checked to be safe.
2786 secure = 0;
2787
2788 // load or unload key mapping tables
2789 errmsg = keymap_init();
2790
2791 secure = secure_save;
2792
2793 // Since we check the value, there is no need to set P_INSECURE,
2794 // even when the value comes from a modeline.
2795 args->os_value_checked = TRUE;
2796 }
2797
2798 if (errmsg == NULL)
2799 {
2800 if (*curbuf->b_p_keymap != NUL)
2801 {
2802 // Installed a new keymap, switch on using it.
2803 curbuf->b_p_iminsert = B_IMODE_LMAP;
2804 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
2805 curbuf->b_p_imsearch = B_IMODE_LMAP;
2806 }
2807 else
2808 {
2809 // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
2810 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
2811 curbuf->b_p_iminsert = B_IMODE_NONE;
2812 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
2813 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
2814 }
2815 if ((args->os_flags & OPT_LOCAL) == 0)
2816 {
2817 set_iminsert_global();
2818 set_imsearch_global();
2819 }
2820 status_redraw_curbuf();
2821 }
2822
2823 return errmsg;
2824}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002825#endif
2826
2827/*
2828 * The 'keymodel' option is changed.
2829 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002830 char *
2831did_set_keymodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002832{
2833 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
2834 return e_invalid_argument;
2835
2836 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
2837 km_startsel = (vim_strchr(p_km, 'a') != NULL);
2838 return NULL;
2839}
2840
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002841 int
2842expand_set_keymodel(optexpand_T *args, int *numMatches, char_u ***matches)
2843{
2844 return expand_set_opt_string(
2845 args,
2846 p_km_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002847 ARRAY_LENGTH(p_km_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002848 numMatches,
2849 matches);
2850}
2851
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002852/*
2853 * The 'keyprotocol' option is changed.
2854 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002855 char *
2856did_set_keyprotocol(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002857{
Gregory Anders3695d0e2023-09-29 20:17:20 +02002858 char_u *term = T_NAME;
2859 keyprot_T kpc = match_keyprotocol(term);
2860 if (kpc == KEYPROTOCOL_FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002861 return e_invalid_argument;
2862
Gregory Anders3695d0e2023-09-29 20:17:20 +02002863 apply_keyprotocol(term, kpc);
2864
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002865 return NULL;
2866}
2867
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002868 int
2869expand_set_keyprotocol(optexpand_T *args, int *numMatches, char_u ***matches)
2870{
2871 expand_T *xp = args->oe_xp;
2872 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2873 {
2874 // 'keyprotocol' only has well-defined terms for completion for the
2875 // protocol part after the colon.
2876 return expand_set_opt_string(
2877 args,
2878 p_kpc_protocol_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002879 ARRAY_LENGTH(p_kpc_protocol_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002880 numMatches,
2881 matches);
2882 }
2883 // Use expand_set_opt_string instead of returning FAIL so that we can
2884 // include the original value if args->oe_include_orig_val is set.
2885 static char *(empty[]) = {NULL};
2886 return expand_set_opt_string(args, empty, 0, numMatches, matches);
2887}
2888
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002889/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002890 * The 'lispoptions' option is changed.
2891 */
2892 char *
2893did_set_lispoptions(optset_T *args)
2894{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002895 char_u **varp = (char_u **)args->os_varp;
2896
2897 if (**varp != NUL
2898 && STRCMP(*varp, "expr:0") != 0
2899 && STRCMP(*varp, "expr:1") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002900 return e_invalid_argument;
2901
2902 return NULL;
2903}
2904
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002905 int
2906expand_set_lispoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2907{
2908 static char *(p_lop_values[]) = {"expr:0", "expr:1", NULL};
2909 return expand_set_opt_string(
2910 args,
2911 p_lop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002912 ARRAY_LENGTH(p_lop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002913 numMatches,
2914 matches);
2915}
2916
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002917/*
2918 * The 'matchpairs' option is changed.
2919 */
2920 char *
2921did_set_matchpairs(optset_T *args)
2922{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002923 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002924 char_u *p;
2925
2926 if (has_mbyte)
2927 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002928 for (p = *varp; *p != NUL; ++p)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002929 {
2930 int x2 = -1;
2931 int x3 = -1;
2932
2933 p += mb_ptr2len(p);
2934 if (*p != NUL)
2935 x2 = *p++;
2936 if (*p != NUL)
2937 {
2938 x3 = mb_ptr2char(p);
2939 p += mb_ptr2len(p);
2940 }
2941 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
2942 return e_invalid_argument;
2943 if (*p == NUL)
2944 break;
2945 }
2946 }
2947 else
2948 {
2949 // Check for "x:y,x:y"
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002950 for (p = *varp; *p != NUL; p += 4)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002951 {
2952 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
2953 return e_invalid_argument;
2954 if (p[3] == NUL)
2955 break;
2956 }
2957 }
2958
2959 return NULL;
2960}
2961
2962#if defined(FEAT_SPELL) || defined(PROTO)
2963/*
2964 * The 'mkspellmem' option is changed.
2965 */
2966 char *
2967did_set_mkspellmem(optset_T *args UNUSED)
2968{
2969 if (spell_check_msm() != OK)
2970 return e_invalid_argument;
2971
2972 return NULL;
2973}
2974#endif
2975
2976/*
2977 * The 'mouse' option is changed.
2978 */
2979 char *
2980did_set_mouse(optset_T *args)
2981{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002982 char_u **varp = (char_u **)args->os_varp;
2983
Christian Brabandtb39b2402023-11-29 11:34:05 +01002984 return did_set_option_listflag(*varp, (char_u *)MOUSE_ALL, args->os_errbuf,
2985 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002986}
2987
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002988 int
2989expand_set_mouse(optexpand_T *args, int *numMatches, char_u ***matches)
2990{
2991 return expand_set_opt_listflag(args, (char_u*)MOUSE_ALL, numMatches, matches);
2992}
2993
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002994/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002995 * The 'mousemodel' option is changed.
2996 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002997 char *
2998did_set_mousemodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002999{
3000 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
3001 return e_invalid_argument;
3002#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
Ken Takata18d0d292023-12-19 20:12:29 +01003003 else if (*p_mousem != *args->os_oldval.string)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003004 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
3005 // to create or delete the popup menus.
3006 gui_motif_update_mousemodel(root_menu);
3007#endif
3008
3009 return NULL;
3010}
3011
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003012 int
3013expand_set_mousemodel(optexpand_T *args, int *numMatches, char_u ***matches)
3014{
3015 return expand_set_opt_string(
3016 args,
3017 p_mousem_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003018 ARRAY_LENGTH(p_mousem_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003019 numMatches,
3020 matches);
3021}
3022
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003023#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3024 char *
3025did_set_mouseshape(optset_T *args UNUSED)
3026{
3027 char *errmsg = NULL;
3028
3029 errmsg = parse_shape_opt(SHAPE_MOUSE);
3030 update_mouseshape(-1);
3031
3032 return errmsg;
3033}
3034#endif
3035
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003036/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003037 * The 'nrformats' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003038 */
3039 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003040did_set_nrformats(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003041{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003042 char_u **varp = (char_u **)args->os_varp;
3043
3044 return did_set_opt_strings(*varp, p_nf_values, TRUE);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003045}
3046
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003047 int
3048expand_set_nrformats(optexpand_T *args, int *numMatches, char_u ***matches)
3049{
3050 return expand_set_opt_string(
3051 args,
3052 p_nf_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003053 ARRAY_LENGTH(p_nf_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003054 numMatches,
3055 matches);
3056}
3057
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003058#if defined(FEAT_EVAL) || defined(PROTO)
3059/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003060 * One of the '*expr' options is changed: 'balloonexpr', 'diffexpr',
3061 * 'foldexpr', 'foldtext', 'formatexpr', 'includeexpr', 'indentexpr',
3062 * 'patchexpr', 'printexpr' and 'charconvert'.
3063 *
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003064 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003065 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003066did_set_optexpr(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003067{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003068 char_u **varp = (char_u **)args->os_varp;
3069
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003070 // If the option value starts with <SID> or s:, then replace that with
3071 // the script identifier.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003072 char_u *name = get_scriptlocal_funcname(*varp);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003073 if (name != NULL)
3074 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003075 free_string_option(*varp);
3076 *varp = name;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003077 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003078
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003079 return NULL;
3080}
3081#endif
3082
3083/*
3084 * The 'pastetoggle' option is changed.
3085 */
3086 char *
3087did_set_pastetoggle(optset_T *args UNUSED)
3088{
3089 char_u *p;
3090
3091 // translate key codes like in a mapping
3092 if (*p_pt)
3093 {
zeertzjq7e0bae02023-08-11 23:15:38 +02003094 (void)replace_termcodes(p_pt, &p, 0,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003095 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
3096 if (p != NULL)
3097 {
3098 free_string_option(p_pt);
3099 p_pt = p;
3100 }
3101 }
3102
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003103 return NULL;
3104}
3105
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003106#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3107/*
3108 * The 'previewpopup' option is changed.
3109 */
3110 char *
3111did_set_previewpopup(optset_T *args UNUSED)
3112{
3113 if (parse_previewpopup(NULL) == FAIL)
3114 return e_invalid_argument;
3115
3116 return NULL;
3117}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003118
3119 int
3120expand_set_popupoption(optexpand_T *args, int *numMatches, char_u ***matches)
3121{
3122 expand_T *xp = args->oe_xp;
3123
3124 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3125 {
3126 // Within "highlight:"/"border:"/"align:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003127 int border_len = (int)STRLEN("border:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003128 if (xp->xp_pattern - args->oe_set_arg >= border_len &&
3129 STRNCMP(xp->xp_pattern - border_len, "border:", border_len) == 0)
3130 {
3131 return expand_set_opt_string(
3132 args,
3133 p_popup_option_border_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003134 ARRAY_LENGTH(p_popup_option_border_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003135 numMatches,
3136 matches);
3137 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003138 int align_len = (int)STRLEN("align:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003139 if (xp->xp_pattern - args->oe_set_arg >= align_len &&
3140 STRNCMP(xp->xp_pattern - align_len, "align:", align_len) == 0)
3141 {
3142 return expand_set_opt_string(
3143 args,
3144 p_popup_option_align_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003145 ARRAY_LENGTH(p_popup_option_align_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003146 numMatches,
3147 matches);
3148 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003149 int highlight_len = (int)STRLEN("highlight:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003150 if (xp->xp_pattern - args->oe_set_arg >= highlight_len &&
3151 STRNCMP(xp->xp_pattern - highlight_len, "highlight:", highlight_len) == 0)
3152 {
3153 // Return the list of all highlight names
3154 return expand_set_opt_generic(
3155 args,
3156 get_highlight_name,
3157 numMatches,
3158 matches);
3159 }
3160 return FAIL;
3161 }
3162
3163 return expand_set_opt_string(
3164 args,
3165 p_popup_option_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003166 ARRAY_LENGTH(p_popup_option_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003167 numMatches,
3168 matches);
3169}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003170#endif
3171
3172#if defined(FEAT_POSTSCRIPT) || defined(PROTO)
3173/*
3174 * The 'printencoding' option is changed.
3175 */
3176 char *
3177did_set_printencoding(optset_T *args UNUSED)
3178{
3179 char_u *s, *p;
3180
3181 // Canonize 'printencoding' if VIM standard one
3182 p = enc_canonize(p_penc);
3183 if (p != NULL)
3184 {
3185 vim_free(p_penc);
3186 p_penc = p;
3187 }
3188 else
3189 {
3190 // Ensure lower case and '-' for '_'
3191 for (s = p_penc; *s != NUL; s++)
3192 {
3193 if (*s == '_')
3194 *s = '-';
3195 else
3196 *s = TOLOWER_ASC(*s);
3197 }
3198 }
3199
3200 return NULL;
3201}
3202#endif
3203
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003204#if defined(FEAT_PRINTER) || defined(PROTO)
3205
3206 static char_u *
3207get_printoptions_names(expand_T *xp UNUSED, int idx)
3208{
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003209 if (idx >= (int)ARRAY_LENGTH(printer_opts))
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003210 return NULL;
3211 return (char_u*)printer_opts[idx].name;
3212}
3213
3214/*
3215 * Expand 'printoptions' option
3216 */
3217 int
3218expand_set_printoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3219{
3220 return expand_set_opt_generic(
3221 args,
3222 get_printoptions_names,
3223 numMatches,
3224 matches);
3225}
3226#endif
3227
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003228#if defined(FEAT_STL_OPT) || defined(PROTO)
3229/*
3230 * The 'statusline' or the 'tabline' or the 'rulerformat' option is changed.
3231 * "rulerformat" is TRUE if the 'rulerformat' option is changed.
3232 */
3233 static char *
3234parse_statustabline_rulerformat(optset_T *args, int rulerformat)
3235{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003236 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003237 char_u *s;
3238 char *errmsg = NULL;
3239 int wid;
3240
3241 if (rulerformat) // reset ru_wid first
3242 ru_wid = 0;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003243 s = *varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003244 if (rulerformat && *s == '%')
3245 {
3246 // set ru_wid if 'ruf' starts with "%99("
3247 if (*++s == '-') // ignore a '-'
3248 s++;
3249 wid = getdigits(&s);
3250 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
3251 ru_wid = wid;
3252 else
3253 errmsg = check_stl_option(p_ruf);
3254 }
3255 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
3256 else if (rulerformat || s[0] != '%' || s[1] != '!')
3257 errmsg = check_stl_option(s);
3258 if (rulerformat && errmsg == NULL)
3259 comp_col();
3260
3261 return errmsg;
3262}
3263#endif
3264
3265#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
3266/*
3267 * The 'renderoptions' option is changed.
3268 */
3269 char *
3270did_set_renderoptions(optset_T *args UNUSED)
3271{
3272 if (!gui_mch_set_rendering_options(p_rop))
3273 return e_invalid_argument;
3274
3275 return NULL;
3276}
3277#endif
3278
3279#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3280/*
3281 * The 'rightleftcmd' option is changed.
3282 */
3283 char *
3284did_set_rightleftcmd(optset_T *args)
3285{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003286 char_u **varp = (char_u **)args->os_varp;
3287
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003288 // Currently only "search" is a supported value.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003289 if (**varp != NUL && STRCMP(*varp, "search") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003290 return e_invalid_argument;
3291
3292 return NULL;
3293}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003294
3295 int
3296expand_set_rightleftcmd(optexpand_T *args, int *numMatches, char_u ***matches)
3297{
3298 static char *(p_rlc_values[]) = {"search", NULL};
3299 return expand_set_opt_string(
3300 args,
3301 p_rlc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003302 ARRAY_LENGTH(p_rlc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003303 numMatches,
3304 matches);
3305}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003306#endif
3307
3308#if defined(FEAT_STL_OPT) || defined(PROTO)
3309/*
3310 * The 'rulerformat' option is changed.
3311 */
3312 char *
3313did_set_rulerformat(optset_T *args)
3314{
3315 return parse_statustabline_rulerformat(args, TRUE);
3316}
3317#endif
3318
3319/*
3320 * The 'scrollopt' option is changed.
3321 */
3322 char *
3323did_set_scrollopt(optset_T *args UNUSED)
3324{
3325 return did_set_opt_strings(p_sbo, p_scbopt_values, TRUE);
3326}
3327
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003328 int
3329expand_set_scrollopt(optexpand_T *args, int *numMatches, char_u ***matches)
3330{
3331 return expand_set_opt_string(
3332 args,
3333 p_scbopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003334 ARRAY_LENGTH(p_scbopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003335 numMatches,
3336 matches);
3337}
3338
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003339/*
3340 * The 'selection' option is changed.
3341 */
3342 char *
3343did_set_selection(optset_T *args UNUSED)
3344{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003345 if (*p_sel == NUL || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003346 return e_invalid_argument;
3347
3348 return NULL;
3349}
3350
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003351 int
3352expand_set_selection(optexpand_T *args, int *numMatches, char_u ***matches)
3353{
3354 return expand_set_opt_string(
3355 args,
3356 p_sel_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003357 ARRAY_LENGTH(p_sel_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003358 numMatches,
3359 matches);
3360}
3361
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003362/*
3363 * The 'selectmode' option is changed.
3364 */
3365 char *
3366did_set_selectmode(optset_T *args UNUSED)
3367{
3368 return did_set_opt_strings(p_slm, p_slm_values, TRUE);
3369}
3370
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003371 int
3372expand_set_selectmode(optexpand_T *args, int *numMatches, char_u ***matches)
3373{
3374 return expand_set_opt_string(
3375 args,
3376 p_slm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003377 ARRAY_LENGTH(p_slm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003378 numMatches,
3379 matches);
3380}
3381
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003382#if defined(FEAT_SESSION) || defined(PROTO)
3383/*
3384 * The 'sessionoptions' option is changed.
3385 */
3386 char *
3387did_set_sessionoptions(optset_T *args)
3388{
3389 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
3390 return e_invalid_argument;
3391 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
3392 {
3393 // Don't allow both "sesdir" and "curdir".
3394 (void)opt_strings_flags(args->os_oldval.string, p_ssop_values,
3395 &ssop_flags, TRUE);
3396 return e_invalid_argument;
3397 }
3398
3399 return NULL;
3400}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003401
3402 int
3403expand_set_sessionoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3404{
3405 return expand_set_opt_string(
3406 args,
3407 p_ssop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003408 ARRAY_LENGTH(p_ssop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003409 numMatches,
3410 matches);
3411}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003412#endif
3413
3414/*
3415 * The 'shortmess' option is changed.
3416 */
3417 char *
3418did_set_shortmess(optset_T *args)
3419{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003420 char_u **varp = (char_u **)args->os_varp;
3421
Christian Brabandtb39b2402023-11-29 11:34:05 +01003422 return did_set_option_listflag(*varp, (char_u *)SHM_ALL, args->os_errbuf,
3423 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003424}
3425
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003426 int
3427expand_set_shortmess(optexpand_T *args, int *numMatches, char_u ***matches)
3428{
3429 return expand_set_opt_listflag(args, (char_u*)SHM_ALL, numMatches, matches);
3430}
3431
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003432#if defined(FEAT_LINEBREAK) || defined(PROTO)
3433/*
3434 * The 'showbreak' option is changed.
3435 */
3436 char *
3437did_set_showbreak(optset_T *args)
3438{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003439 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003440 char_u *s;
3441
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003442 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003443 {
3444 if (ptr2cells(s) != 1)
3445 return e_showbreak_contains_unprintable_or_wide_character;
3446 MB_PTR_ADV(s);
3447 }
3448
3449 return NULL;
3450}
3451#endif
3452
3453/*
3454 * The 'showcmdloc' option is changed.
3455 */
3456 char *
3457did_set_showcmdloc(optset_T *args UNUSED)
3458{
zeertzjqc27fcf42024-03-01 23:01:43 +01003459 char *errmsg = did_set_opt_strings(p_sloc, p_sloc_values, FALSE);
3460
3461 if (errmsg == NULL)
3462 comp_col();
3463
3464 return errmsg;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003465}
3466
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003467 int
3468expand_set_showcmdloc(optexpand_T *args, int *numMatches, char_u ***matches)
3469{
3470 return expand_set_opt_string(
3471 args,
3472 p_sloc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003473 ARRAY_LENGTH(p_sloc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003474 numMatches,
3475 matches);
3476}
3477
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003478#if defined(FEAT_SIGNS) || defined(PROTO)
3479/*
3480 * The 'signcolumn' option is changed.
3481 */
3482 char *
3483did_set_signcolumn(optset_T *args)
3484{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003485 char_u **varp = (char_u **)args->os_varp;
3486
3487 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003488 return e_invalid_argument;
3489 // When changing the 'signcolumn' to or from 'number', recompute the
3490 // width of the number column if 'number' or 'relativenumber' is set.
3491 if (((*args->os_oldval.string == 'n' && args->os_oldval.string[1] == 'u')
3492 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
3493 && (curwin->w_p_nu || curwin->w_p_rnu))
3494 curwin->w_nrwidth_line_count = 0;
3495
3496 return NULL;
3497}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003498
3499 int
3500expand_set_signcolumn(optexpand_T *args, int *numMatches, char_u ***matches)
3501{
3502 return expand_set_opt_string(
3503 args,
3504 p_scl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003505 ARRAY_LENGTH(p_scl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003506 numMatches,
3507 matches);
3508}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003509#endif
3510
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003511#if defined(FEAT_SPELL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003512/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003513 * The 'spellcapcheck' option is changed.
3514 */
3515 char *
3516did_set_spellcapcheck(optset_T *args UNUSED)
3517{
3518 // compile the regexp program.
3519 return compile_cap_prog(curwin->w_s);
3520}
3521
3522/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003523 * The 'spellfile' option is changed.
3524 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003525 char *
3526did_set_spellfile(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003527{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003528 char_u **varp = (char_u **)args->os_varp;
3529
3530 if (!valid_spellfile(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003531 return e_invalid_argument;
3532
3533 // If there is a window for this buffer in which 'spell' is set load the
3534 // wordlists.
3535 return did_set_spell_option(TRUE);
3536}
3537
3538/*
3539 * The 'spell' option is changed.
3540 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003541 char *
3542did_set_spelllang(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003543{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003544 char_u **varp = (char_u **)args->os_varp;
3545
3546 if (!valid_spelllang(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003547 return e_invalid_argument;
3548
3549 // If there is a window for this buffer in which 'spell' is set load the
3550 // wordlists.
3551 return did_set_spell_option(FALSE);
3552}
3553
3554/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003555 * The 'spelloptions' option is changed.
3556 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003557 char *
3558did_set_spelloptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003559{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003560 char_u **varp = (char_u **)args->os_varp;
3561
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003562 if (**varp != NUL && STRCMP(*varp, "camel") != 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003563 return e_invalid_argument;
3564
3565 return NULL;
3566}
3567
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003568 int
3569expand_set_spelloptions(optexpand_T *args, int *numMatches, char_u ***matches)
3570{
3571 static char *(p_spo_values[]) = {"camel", NULL};
3572 return expand_set_opt_string(
3573 args,
3574 p_spo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003575 ARRAY_LENGTH(p_spo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003576 numMatches,
3577 matches);
3578}
3579
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003580/*
3581 * The 'spellsuggest' option is changed.
3582 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003583 char *
3584did_set_spellsuggest(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003585{
3586 if (spell_check_sps() != OK)
3587 return e_invalid_argument;
3588
3589 return NULL;
3590}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003591
3592 int
3593expand_set_spellsuggest(optexpand_T *args, int *numMatches, char_u ***matches)
3594{
3595 return expand_set_opt_string(
3596 args,
3597 p_sps_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003598 ARRAY_LENGTH(p_sps_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003599 numMatches,
3600 matches);
3601}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003602#endif
3603
3604/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003605 * The 'splitkeep' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003606 */
3607 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003608did_set_splitkeep(optset_T *args UNUSED)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003609{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003610 return did_set_opt_strings(p_spk, p_spk_values, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003611}
3612
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003613 int
3614expand_set_splitkeep(optexpand_T *args, int *numMatches, char_u ***matches)
3615{
3616 return expand_set_opt_string(
3617 args,
3618 p_spk_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003619 ARRAY_LENGTH(p_spk_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003620 numMatches,
3621 matches);
3622}
3623
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00003624#if defined(FEAT_STL_OPT) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003625/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003626 * The 'statusline' option is changed.
3627 */
3628 char *
3629did_set_statusline(optset_T *args)
3630{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003631 return parse_statustabline_rulerformat(args, FALSE);
3632}
3633#endif
3634
3635/*
3636 * The 'swapsync' option is changed.
3637 */
3638 char *
3639did_set_swapsync(optset_T *args UNUSED)
3640{
3641 return did_set_opt_strings(p_sws, p_sws_values, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003642}
3643
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003644 int
3645expand_set_swapsync(optexpand_T *args, int *numMatches, char_u ***matches)
3646{
3647 return expand_set_opt_string(
3648 args,
3649 p_sws_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003650 ARRAY_LENGTH(p_sws_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003651 numMatches,
3652 matches);
3653}
3654
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003655/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003656 * The 'switchbuf' option is changed.
3657 */
3658 char *
3659did_set_switchbuf(optset_T *args UNUSED)
3660{
3661 return did_set_opt_flags(p_swb, p_swb_values, &swb_flags, TRUE);
3662}
3663
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003664 int
3665expand_set_switchbuf(optexpand_T *args, int *numMatches, char_u ***matches)
3666{
3667 return expand_set_opt_string(
3668 args,
3669 p_swb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003670 ARRAY_LENGTH(p_swb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003671 numMatches,
3672 matches);
3673}
3674
LemonBoy5247b0b2024-07-12 19:30:58 +02003675/*
3676 * The 'tabclose' option is changed.
3677 */
3678 char *
3679did_set_tabclose(optset_T *args UNUSED)
3680{
3681 return did_set_opt_flags(p_tcl, p_tcl_values, &tcl_flags, TRUE);
3682}
3683
3684 int
3685expand_set_tabclose(optexpand_T *args, int *numMatches, char_u ***matches)
3686{
3687 return expand_set_opt_string(
3688 args,
3689 p_tcl_values,
3690 ARRAY_LENGTH(p_tcl_values) - 1,
3691 numMatches,
3692 matches);
3693}
3694
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003695#if defined(FEAT_STL_OPT) || defined(PROTO)
3696/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003697 * The 'tabline' option is changed.
3698 */
3699 char *
3700did_set_tabline(optset_T *args)
3701{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003702 return parse_statustabline_rulerformat(args, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003703}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003704#endif
3705
3706/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003707 * The 'tagcase' option is changed.
3708 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003709 char *
3710did_set_tagcase(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003711{
3712 unsigned int *flags;
3713 char_u *p;
3714
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003715 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003716 {
3717 p = curbuf->b_p_tc;
3718 flags = &curbuf->b_tc_flags;
3719 }
3720 else
3721 {
3722 p = p_tc;
3723 flags = &tc_flags;
3724 }
3725
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003726 if ((args->os_flags & OPT_LOCAL) && *p == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003727 // make the local value empty: use the global value
3728 *flags = 0;
3729 else if (*p == NUL
3730 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
3731 return e_invalid_argument;
3732
3733 return NULL;
3734}
3735
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003736 int
3737expand_set_tagcase(optexpand_T *args, int *numMatches, char_u ***matches)
3738{
3739 return expand_set_opt_string(
3740 args,
3741 p_tc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003742 ARRAY_LENGTH(p_tc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003743 numMatches,
3744 matches);
3745}
3746
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003747/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003748 * The 'term' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003749 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003750 char *
3751did_set_term(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003752{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003753 if (T_NAME[0] == NUL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003754 return e_cannot_set_term_to_empty_string;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003755#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003756 if (gui.in_use)
3757 return e_cannot_change_term_in_GUI;
3758 if (term_is_gui(T_NAME))
3759 return e_use_gui_to_start_GUI;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003760#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003761 if (set_termname(T_NAME) == FAIL)
3762 return e_not_found_in_termcap;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003763
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003764 // Screen colors may have changed.
3765 redraw_later_clear();
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003766
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003767 return NULL;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003768}
3769
3770/*
3771 * Some terminal option (t_xxx) is changed
3772 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003773 char *
3774did_set_term_option(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003775{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003776 char_u **varp = (char_u **)args->os_varp;
3777
3778 if (!full_screen)
3779 return NULL;
3780
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003781 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
3782 if (varp == &T_CCO)
3783 {
3784 int colors = atoi((char *)T_CCO);
3785
3786 // Only reinitialize colors if t_Co value has really changed to
3787 // avoid expensive reload of colorscheme if t_Co is set to the
3788 // same value multiple times.
3789 if (colors != t_colors)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003790 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003791 t_colors = colors;
3792 if (t_colors <= 1)
3793 {
3794 vim_free(T_CCO);
3795 T_CCO = empty_option;
3796 }
3797#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3798 if (is_term_win32())
3799 {
3800 swap_tcap();
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003801 args->os_did_swaptcap = TRUE;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003802 }
3803#endif
3804 // We now have a different color setup, initialize it again.
3805 init_highlight(TRUE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003806 }
3807 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003808 ttest(FALSE);
3809 if (varp == &T_ME)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003810 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003811 out_str(T_ME);
3812 redraw_later(UPD_CLEAR);
3813#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3814 // Since t_me has been set, this probably means that the user
3815 // wants to use this as default colors. Need to reset default
3816 // background/foreground colors.
3817# ifdef VIMDLL
3818 if (!gui.in_use && !gui.starting)
3819# endif
3820 mch_set_normal_colors();
3821#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003822 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003823 if (varp == &T_BE && termcap_active)
3824 {
3825 MAY_WANT_TO_LOG_THIS;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003826
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003827 if (*T_BE == NUL)
3828 // When clearing t_BE we assume the user no longer wants
3829 // bracketed paste, thus disable it by writing t_BD.
3830 out_str(T_BD);
3831 else
3832 out_str(T_BE);
3833 }
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003834
3835 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003836}
3837
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003838#if defined(FEAT_TERMINAL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003839/*
3840 * The 'termwinkey' option is changed.
3841 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003842 char *
3843did_set_termwinkey(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003844{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003845 if (*curwin->w_p_twk != NUL && string_to_key(curwin->w_p_twk, TRUE) == 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003846 return e_invalid_argument;
3847
3848 return NULL;
3849}
3850
3851/*
3852 * The 'termwinsize' option is changed.
3853 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003854 char *
3855did_set_termwinsize(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003856{
3857 char_u *p;
3858
3859 if (*curwin->w_p_tws == NUL)
3860 return NULL;
3861
3862 p = skipdigits(curwin->w_p_tws);
3863 if (p == curwin->w_p_tws
3864 || (*p != 'x' && *p != '*')
3865 || *skipdigits(p + 1) != NUL)
3866 return e_invalid_argument;
3867
3868 return NULL;
3869}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003870
3871# if defined(MSWIN) || defined(PROTO)
3872/*
3873 * The 'termwintype' option is changed.
3874 */
3875 char *
3876did_set_termwintype(optset_T *args UNUSED)
3877{
3878 return did_set_opt_strings(p_twt, p_twt_values, FALSE);
3879}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003880
3881 int
3882expand_set_termwintype(optexpand_T *args, int *numMatches, char_u ***matches)
3883{
3884 return expand_set_opt_string(
3885 args,
3886 p_twt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003887 ARRAY_LENGTH(p_twt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003888 numMatches,
3889 matches);
3890}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003891# endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003892#endif
3893
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003894/*
3895 * The 'titlestring' option is changed.
3896 */
3897 char *
3898did_set_titlestring(optset_T *args)
3899{
3900 int flagval = 0;
3901
3902#ifdef FEAT_STL_OPT
3903 flagval = STL_IN_TITLE;
3904#endif
3905 return parse_titleiconstring(args, flagval);
3906}
3907
3908#if (defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)) || defined(PROTO)
3909/*
3910 * The 'toolbar' option is changed.
3911 */
3912 char *
3913did_set_toolbar(optset_T *args UNUSED)
3914{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003915 if (opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags,
3916 TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003917 return e_invalid_argument;
3918
3919 out_flush();
3920 gui_mch_show_toolbar((toolbar_flags &
3921 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
3922 return NULL;
3923}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003924
3925 int
3926expand_set_toolbar(optexpand_T *args, int *numMatches, char_u ***matches)
3927{
3928 return expand_set_opt_string(
3929 args,
3930 p_toolbar_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003931 ARRAY_LENGTH(p_toolbar_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003932 numMatches,
3933 matches);
3934}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003935#endif
3936
3937#if (defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)) || defined(PROTO)
3938/*
3939 * The 'toolbariconsize' option is changed. GTK+ 2 only.
3940 */
3941 char *
3942did_set_toolbariconsize(optset_T *args UNUSED)
3943{
3944 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
3945 return e_invalid_argument;
3946
3947 out_flush();
3948 gui_mch_show_toolbar((toolbar_flags &
3949 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
3950 return NULL;
3951}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003952
3953 int
3954expand_set_toolbariconsize(optexpand_T *args, int *numMatches, char_u ***matches)
3955{
3956 return expand_set_opt_string(
3957 args,
3958 p_tbis_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003959 ARRAY_LENGTH(p_tbis_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003960 numMatches,
3961 matches);
3962}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003963#endif
3964
3965#if defined(UNIX) || defined(VMS) || defined(PROTO)
3966/*
3967 * The 'ttymouse' option is changed.
3968 */
3969 char *
3970did_set_ttymouse(optset_T *args UNUSED)
3971{
3972 char *errmsg = NULL;
3973
3974 // Switch the mouse off before changing the escape sequences used for
3975 // that.
3976 mch_setmouse(FALSE);
3977 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
3978 errmsg = e_invalid_argument;
3979 else
3980 check_mouse_termcode();
3981 if (termcap_active)
3982 setmouse(); // may switch it on again
3983
3984 return errmsg;
3985}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003986
3987 int
3988expand_set_ttymouse(optexpand_T *args, int *numMatches, char_u ***matches)
3989{
3990 return expand_set_opt_string(
3991 args,
3992 p_ttym_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003993 ARRAY_LENGTH(p_ttym_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003994 numMatches,
3995 matches);
3996}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003997#endif
3998
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003999#if defined(FEAT_VARTABS) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004000/*
4001 * The 'varsofttabstop' option is changed.
4002 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004003 char *
4004did_set_varsofttabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004005{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004006 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004007 char_u *cp;
4008
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004009 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004010 VIM_CLEAR(curbuf->b_p_vsts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004011 else
4012 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004013 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004014 {
4015 if (vim_isdigit(*cp))
4016 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004017 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004018 continue;
4019 return e_invalid_argument;
4020 }
4021
4022 int *oldarray = curbuf->b_p_vsts_array;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004023 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004024 {
4025 if (oldarray)
4026 vim_free(oldarray);
4027 }
4028 else
4029 return e_invalid_argument;
4030 }
4031
4032 return NULL;
4033}
4034
4035/*
4036 * The 'vartabstop' option is changed.
4037 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004038 char *
4039did_set_vartabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004040{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004041 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004042 char_u *cp;
4043
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004044 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004045 VIM_CLEAR(curbuf->b_p_vts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004046 else
4047 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004048 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004049 {
4050 if (vim_isdigit(*cp))
4051 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004052 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004053 continue;
4054 return e_invalid_argument;
4055 }
4056
4057 int *oldarray = curbuf->b_p_vts_array;
4058
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004059 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004060 {
4061 vim_free(oldarray);
4062# ifdef FEAT_FOLDING
4063 if (foldmethodIsIndent(curwin))
4064 foldUpdateAll(curwin);
4065# endif
4066 }
4067 else
4068 return e_invalid_argument;
4069 }
4070
4071 return NULL;
4072}
4073#endif
4074
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004075/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004076 * The 'verbosefile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004077 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004078 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004079did_set_verbosefile(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004080{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004081 verbose_stop();
4082 if (*p_vfile != NUL && verbose_open() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004083 return e_invalid_argument;
4084
4085 return NULL;
4086}
4087
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004088#if defined(FEAT_SESSION) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004089/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004090 * The 'viewoptions' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004091 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004092 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004093did_set_viewoptions(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004094{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004095 return did_set_opt_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004096}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004097#endif
4098
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004099#if defined(FEAT_VIMINFO) || defined(PROTO)
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004100/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004101 * The 'viminfo' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004102 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004103 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004104did_set_viminfo(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004105{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004106 char_u *s;
4107 char *errmsg = NULL;
4108
4109 for (s = p_viminfo; *s;)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004110 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004111 // Check it's a valid character
4112 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
4113 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01004114 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004115 break;
4116 }
4117 if (*s == 'n') // name is always last one
4118 break;
4119 else if (*s == 'r') // skip until next ','
4120 {
4121 while (*++s && *s != ',')
4122 ;
4123 }
4124 else if (*s == '%')
4125 {
4126 // optional number
4127 while (vim_isdigit(*++s))
4128 ;
4129 }
4130 else if (*s == '!' || *s == 'h' || *s == 'c')
4131 ++s; // no extra chars
4132 else // must have a number
4133 {
4134 while (vim_isdigit(*++s))
4135 ;
4136
4137 if (!VIM_ISDIGIT(*(s - 1)))
4138 {
4139 if (args->os_errbuf != NULL)
4140 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01004141 vim_snprintf(args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004142 _(e_missing_number_after_angle_str_angle),
4143 transchar_byte(*(s - 1)));
4144 errmsg = args->os_errbuf;
4145 }
4146 else
4147 errmsg = "";
4148 break;
4149 }
4150 }
4151 if (*s == ',')
4152 ++s;
4153 else if (*s)
4154 {
4155 if (args->os_errbuf != NULL)
4156 errmsg = e_missing_comma;
4157 else
4158 errmsg = "";
4159 break;
4160 }
4161 }
4162 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
4163 errmsg = e_must_specify_a_value;
4164
4165 return errmsg;
4166}
4167#endif
4168
4169/*
4170 * The 'virtualedit' option is changed.
4171 */
4172 char *
4173did_set_virtualedit(optset_T *args)
4174{
4175 char_u *ve = p_ve;
4176 unsigned int *flags = &ve_flags;
4177
4178 if (args->os_flags & OPT_LOCAL)
4179 {
4180 ve = curwin->w_p_ve;
4181 flags = &curwin->w_ve_flags;
4182 }
4183
4184 if ((args->os_flags & OPT_LOCAL) && *ve == NUL)
4185 // make the local value empty: use the global value
4186 *flags = 0;
4187 else
4188 {
4189 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
4190 return e_invalid_argument;
4191 else if (STRCMP(ve, args->os_oldval.string) != 0)
4192 {
4193 // Recompute cursor position in case the new 've' setting
4194 // changes something.
4195 validate_virtcol();
4196 coladvance(curwin->w_virtcol);
4197 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004198 }
4199
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004200 return NULL;
4201}
4202
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004203 int
4204expand_set_virtualedit(optexpand_T *args, int *numMatches, char_u ***matches)
4205{
4206 return expand_set_opt_string(
4207 args,
4208 p_ve_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004209 ARRAY_LENGTH(p_ve_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004210 numMatches,
4211 matches);
4212}
4213
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004214/*
4215 * The 'whichwrap' option is changed.
4216 */
4217 char *
4218did_set_whichwrap(optset_T *args)
4219{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004220 char_u **varp = (char_u **)args->os_varp;
4221
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004222 // Add ',' to the list flags because 'whichwrap' is a flag
4223 // list that is comma-separated.
Christian Brabandtb39b2402023-11-29 11:34:05 +01004224 return did_set_option_listflag(*varp, (char_u *)(WW_ALL ","),
4225 args->os_errbuf, args->os_errbuflen);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004226}
4227
4228 int
4229expand_set_whichwrap(optexpand_T *args, int *numMatches, char_u ***matches)
4230{
4231 return expand_set_opt_listflag(args, (char_u*)WW_ALL, numMatches, matches);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004232}
4233
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004234/*
4235 * The 'wildmode' option is changed.
4236 */
4237 char *
4238did_set_wildmode(optset_T *args UNUSED)
4239{
4240 if (check_opt_wim() == FAIL)
4241 return e_invalid_argument;
4242 return NULL;
4243}
4244
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004245 int
4246expand_set_wildmode(optexpand_T *args, int *numMatches, char_u ***matches)
4247{
4248 return expand_set_opt_string(
4249 args,
4250 p_wim_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004251 ARRAY_LENGTH(p_wim_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004252 numMatches,
4253 matches);
4254}
4255
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004256/*
4257 * The 'wildoptions' option is changed.
4258 */
4259 char *
4260did_set_wildoptions(optset_T *args UNUSED)
4261{
4262 return did_set_opt_strings(p_wop, p_wop_values, TRUE);
4263}
4264
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004265 int
4266expand_set_wildoptions(optexpand_T *args, int *numMatches, char_u ***matches)
4267{
4268 return expand_set_opt_string(
4269 args,
4270 p_wop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004271 ARRAY_LENGTH(p_wop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004272 numMatches,
4273 matches);
4274}
4275
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004276#if defined(FEAT_WAK) || defined(PROTO)
4277/*
4278 * The 'winaltkeys' option is changed.
4279 */
4280 char *
4281did_set_winaltkeys(optset_T *args UNUSED)
4282{
4283 char *errmsg = NULL;
4284
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004285 if (*p_wak == NUL || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004286 errmsg = e_invalid_argument;
4287# ifdef FEAT_MENU
4288# if defined(FEAT_GUI_MOTIF)
4289 else if (gui.in_use)
4290 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4291# elif defined(FEAT_GUI_GTK)
4292 else if (gui.in_use)
4293 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4294# endif
4295# endif
4296 return errmsg;
4297}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004298
4299 int
4300expand_set_winaltkeys(optexpand_T *args, int *numMatches, char_u ***matches)
4301{
4302 return expand_set_opt_string(
4303 args,
4304 p_wak_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004305 ARRAY_LENGTH(p_wak_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004306 numMatches,
4307 matches);
4308}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004309#endif
4310
4311/*
4312 * The 'wincolor' option is changed.
4313 */
4314 char *
4315did_set_wincolor(optset_T *args UNUSED)
4316{
4317#ifdef FEAT_TERMINAL
4318 term_update_wincolor(curwin);
4319#endif
4320 return NULL;
4321}
4322
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004323 int
4324expand_set_wincolor(optexpand_T *args, int *numMatches, char_u ***matches)
4325{
4326 return expand_set_opt_generic(
4327 args,
4328 get_highlight_name,
4329 numMatches,
4330 matches);
4331}
4332
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004333#ifdef FEAT_SYN_HL
4334/*
4335 * When the 'syntax' option is set, load the syntax of that name.
4336 */
4337 static void
4338do_syntax_autocmd(int value_changed)
4339{
4340 static int syn_recursive = 0;
4341
4342 ++syn_recursive;
4343 // Only pass TRUE for "force" when the value changed or not used
4344 // recursively, to avoid endless recurrence.
4345 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
4346 value_changed || syn_recursive == 1, curbuf);
4347 curbuf->b_flags |= BF_SYN_SET;
4348 --syn_recursive;
4349}
4350#endif
4351
4352/*
4353 * When the 'filetype' option is set, trigger the FileType autocommand.
4354 */
4355 static void
4356do_filetype_autocmd(char_u **varp, int opt_flags, int value_changed)
4357{
4358 // Skip this when called from a modeline and the filetype was already set
4359 // to this value.
4360 if ((opt_flags & OPT_MODELINE) && !value_changed)
4361 return;
4362
4363 static int ft_recursive = 0;
4364 int secure_save = secure;
4365
4366 // Reset the secure flag, since the value of 'filetype' has
4367 // been checked to be safe.
4368 secure = 0;
4369
4370 ++ft_recursive;
zeertzjq5bf6c212024-03-31 18:41:27 +02004371 curbuf->b_did_filetype = TRUE;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004372 // Only pass TRUE for "force" when the value changed or not
4373 // used recursively, to avoid endless recurrence.
4374 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
4375 value_changed || ft_recursive == 1, curbuf);
4376 --ft_recursive;
4377 // Just in case the old "curbuf" is now invalid.
4378 if (varp != &(curbuf->b_p_ft))
4379 varp = NULL;
4380
4381 secure = secure_save;
4382}
4383
4384#ifdef FEAT_SPELL
4385/*
4386 * When the 'spelllang' option is set, source the spell/LANG.vim file in
4387 * 'runtimepath'.
4388 */
4389 static void
4390do_spelllang_source(void)
4391{
4392 char_u fname[200];
4393 char_u *p;
4394 char_u *q = curwin->w_s->b_p_spl;
4395
4396 // Skip the first name if it is "cjk".
4397 if (STRNCMP(q, "cjk,", 4) == 0)
4398 q += 4;
4399
4400 // They could set 'spellcapcheck' depending on the language. Use the first
4401 // name in 'spelllang' up to '_region' or '.encoding'.
4402 for (p = q; *p != NUL; ++p)
4403 if (!ASCII_ISALNUM(*p) && *p != '-')
4404 break;
4405 if (p > q)
4406 {
4407 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
4408 (int)(p - q), q);
4409 source_runtime(fname, DIP_ALL);
4410 }
4411}
4412#endif
4413
4414/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004415 * Handle string options that need some action to perform when changed.
zeertzjqf6782732022-07-27 18:26:03 +01004416 * The new value must be allocated.
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004417 * Returns NULL for success, or an untranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +02004418 */
4419 char *
4420did_set_string_option(
4421 int opt_idx, // index in options[] table
4422 char_u **varp, // pointer to the option variable
Bram Moolenaardac13472019-09-16 21:06:21 +02004423 char_u *oldval, // previous value of the option
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004424 char_u *value, // new value of the option
Bram Moolenaardac13472019-09-16 21:06:21 +02004425 char *errbuf, // buffer for errors, or NULL
Mike Williams620f0112023-12-05 15:36:06 +01004426 size_t errbuflen, // length of error buffer
Bram Moolenaardac13472019-09-16 21:06:21 +02004427 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004428 set_op_T op, // OP_ADDING/OP_PREPENDING/OP_REMOVING
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004429 int *value_checked) // value was checked to be safe, no
Bram Moolenaardac13472019-09-16 21:06:21 +02004430 // need to set P_INSECURE
4431{
4432 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +02004433 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004434 opt_did_set_cb_T did_set_cb = get_option_did_set_cb(opt_idx);
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004435 optset_T args;
4436
4437 // 'ttytype' is an alias for 'term'. Both 'term' and 'ttytype' point to
4438 // T_NAME. If 'term' or 'ttytype' is modified, then use the index for the
4439 // 'term' option. Only set the P_ALLOCED flag on 'term'.
4440 if (varp == &T_NAME)
4441 {
4442 opt_idx = findoption((char_u *)"term");
4443 if (opt_idx >= 0)
4444 {
4445 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
4446 did_set_cb = get_option_did_set_cb(opt_idx);
4447 }
4448 }
4449
4450 CLEAR_FIELD(args);
Bram Moolenaardac13472019-09-16 21:06:21 +02004451
Bram Moolenaardac13472019-09-16 21:06:21 +02004452 // Disallow changing some options from secure mode
4453 if ((secure
4454#ifdef HAVE_SANDBOX
4455 || sandbox != 0
4456#endif
4457 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00004458 errmsg = e_not_allowed_here;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004459 // Check for a "normal" directory or file name in some options.
4460 else if (check_illegal_path_names(opt_idx, varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004461 errmsg = e_invalid_argument;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004462 else if (did_set_cb != NULL)
4463 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004464 args.os_varp = (char_u *)varp;
4465 args.os_idx = opt_idx;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004466 args.os_flags = opt_flags;
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004467 args.os_op = op;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004468 args.os_oldval.string = oldval;
4469 args.os_newval.string = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004470 args.os_errbuf = errbuf;
Christian Brabandtb39b2402023-11-29 11:34:05 +01004471 args.os_errbuflen = errbuflen;
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004472 // Invoke the option specific callback function to validate and apply
4473 // the new option value.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004474 errmsg = did_set_cb(&args);
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004475
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004476 // The 'keymap', 'filetype' and 'syntax' option callback functions
4477 // may change the os_value_checked field.
4478 *value_checked = args.os_value_checked;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004479 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004480
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004481 // If an error is detected, restore the previous value.
Bram Moolenaardac13472019-09-16 21:06:21 +02004482 if (errmsg != NULL)
4483 {
zeertzjqf6782732022-07-27 18:26:03 +01004484 free_string_option(*varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02004485 *varp = oldval;
4486 // When resetting some values, need to act on it.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004487 if (args.os_restore_chartab)
Bram Moolenaardac13472019-09-16 21:06:21 +02004488 (void)init_chartab();
4489 if (varp == &p_hl)
4490 (void)highlight_changed();
4491 }
4492 else
4493 {
4494#ifdef FEAT_EVAL
4495 // Remember where the option was set.
4496 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4497#endif
4498 // Free string options that are in allocated memory.
4499 // Use "free_oldval", because recursiveness may change the flags under
4500 // our fingers (esp. init_highlight()).
4501 if (free_oldval)
4502 free_string_option(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01004503 set_option_flag(opt_idx, P_ALLOCED);
Bram Moolenaardac13472019-09-16 21:06:21 +02004504
4505 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
4506 && is_global_local_option(opt_idx))
4507 {
4508 // global option with local value set to use global value; free
4509 // the local value and make it empty
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004510 char_u *p = get_option_varp_scope(opt_idx, OPT_LOCAL);
Bram Moolenaardac13472019-09-16 21:06:21 +02004511 free_string_option(*(char_u **)p);
4512 *(char_u **)p = empty_option;
4513 }
4514
4515 // May set global value for local option.
4516 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
4517 set_string_option_global(opt_idx, varp);
4518
4519 // Trigger the autocommand only after setting the flags.
4520#ifdef FEAT_SYN_HL
Bram Moolenaardac13472019-09-16 21:06:21 +02004521 if (varp == &(curbuf->b_p_syn))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004522 do_syntax_autocmd(args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004523#endif
4524 else if (varp == &(curbuf->b_p_ft))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004525 do_filetype_autocmd(varp, opt_flags, args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004526#ifdef FEAT_SPELL
4527 if (varp == &(curwin->w_s->b_p_spl))
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004528 do_spelllang_source();
Bram Moolenaardac13472019-09-16 21:06:21 +02004529#endif
4530 }
4531
Bram Moolenaardac13472019-09-16 21:06:21 +02004532 if (varp == &p_mouse)
4533 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004534 if (*p_mouse == NUL)
4535 mch_setmouse(FALSE); // switch mouse off
4536 else
Bram Moolenaardac13472019-09-16 21:06:21 +02004537 setmouse(); // in case 'mouse' changed
4538 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004539
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004540#if defined(FEAT_LUA) || defined(PROTO)
4541 if (varp == &p_rtp)
4542 update_package_paths_in_lua();
4543#endif
4544
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004545#if defined(FEAT_LINEBREAK)
4546 // Changing Formatlistpattern when briopt includes the list setting:
4547 // redraw
4548 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
4549 && curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004550 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004551#endif
4552
Bram Moolenaardac13472019-09-16 21:06:21 +02004553 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004554 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0
4555 && (get_option_flags(opt_idx) & P_HLONLY) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02004556 curwin->w_set_curswant = TRUE;
4557
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004558 if ((opt_flags & OPT_NO_REDRAW) == 0)
4559 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004560#ifdef FEAT_GUI
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004561 // set when changing an option that only requires a redraw in the GUI
4562 int redraw_gui_only = FALSE;
4563
4564 if (varp == &p_go // 'guioptions'
4565 || varp == &p_guifont // 'guifont'
4566# ifdef FEAT_GUI_TABLINE
4567 || varp == &p_gtl // 'guitablabel'
4568 || varp == &p_gtt // 'guitabtooltip'
4569# endif
4570# ifdef FEAT_XFONTSET
4571 || varp == &p_guifontset // 'guifontset'
4572# endif
4573 || varp == &p_guifontwide // 'guifontwide'
Erik S. V. Jansson2f026382024-02-26 22:23:05 +01004574# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004575 || varp == &p_guiligatures // 'guiligatures'
4576# endif
4577 )
4578 redraw_gui_only = TRUE;
4579
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004580 // check redraw when it's not a GUI option or the GUI is active.
4581 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02004582#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004583 check_redraw(get_option_flags(opt_idx));
4584 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004585
4586#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004587 if (args.os_did_swaptcap)
Bram Moolenaardac13472019-09-16 21:06:21 +02004588 {
4589 set_termname((char_u *)"win32");
4590 init_highlight(TRUE, FALSE);
4591 }
4592#endif
4593
4594 return errmsg;
4595}
4596
4597/*
4598 * Check an option that can be a range of string values.
4599 *
4600 * Return OK for correct value, FAIL otherwise.
4601 * Empty is always OK.
4602 */
4603 static int
4604check_opt_strings(
4605 char_u *val,
4606 char **values,
4607 int list) // when TRUE: accept a list of values
4608{
4609 return opt_strings_flags(val, values, NULL, list);
4610}
4611
4612/*
4613 * Handle an option that can be a range of string values.
4614 * Set a flag in "*flagp" for each string present.
4615 *
4616 * Return OK for correct value, FAIL otherwise.
4617 * Empty is always OK.
4618 */
4619 static int
4620opt_strings_flags(
4621 char_u *val, // new value
4622 char **values, // array of valid string values
4623 unsigned *flagp,
4624 int list) // when TRUE: accept a list of values
4625{
4626 int i;
4627 int len;
4628 unsigned new_flags = 0;
4629
4630 while (*val)
4631 {
4632 for (i = 0; ; ++i)
4633 {
4634 if (values[i] == NULL) // val not found in values[]
4635 return FAIL;
4636
4637 len = (int)STRLEN(values[i]);
4638 if (STRNCMP(values[i], val, len) == 0
4639 && ((list && val[len] == ',') || val[len] == NUL))
4640 {
4641 val += len + (val[len] == ',');
4642 new_flags |= (1 << i);
4643 break; // check next item in val list
4644 }
4645 }
4646 }
4647 if (flagp != NULL)
4648 *flagp = new_flags;
4649
4650 return OK;
4651}
4652
4653/*
4654 * return OK if "p" is a valid fileformat name, FAIL otherwise.
4655 */
4656 int
4657check_ff_value(char_u *p)
4658{
4659 return check_opt_strings(p, p_ff_values, FALSE);
4660}
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004661
4662/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004663 * Save the actual shortmess Flags and clear them temporarily to avoid that
4664 * file messages overwrites any output from the following commands.
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004665 *
4666 * Caller must make sure to first call save_clear_shm_value() and then
4667 * restore_shm_value() exactly the same number of times.
4668 */
4669 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004670save_clear_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004671{
4672 if (STRLEN(p_shm) >= SHM_LEN)
4673 {
4674 iemsg(e_internal_error_shortmess_too_long);
4675 return;
4676 }
4677
4678 if (++set_shm_recursive == 1)
4679 {
4680 STRCPY(shm_buf, p_shm);
4681 set_option_value_give_err((char_u *)"shm", 0L, (char_u *)"", 0);
4682 }
4683}
4684
4685/*
4686 * Restore the shortmess Flags set from the save_clear_shm_value() function.
4687 */
4688 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004689restore_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004690{
4691 if (--set_shm_recursive == 0)
4692 {
4693 set_option_value_give_err((char_u *)"shm", 0L, shm_buf, 0);
4694 vim_memset(shm_buf, 0, SHM_LEN);
4695 }
4696}