blob: 259c64cefed88a919e883f9e6a24d87701b1f82a [file] [log] [blame]
Bram Moolenaardac13472019-09-16 21:06:21 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * optionstr.c: Functions related to string options
12 */
13
14#include "vim.h"
15
Christian Brabandt9aee8ec2022-12-16 16:41:23 +000016static char_u shm_buf[SHM_LEN];
17static int set_shm_recursive = 0;
18
Bram Moolenaardac13472019-09-16 21:06:21 +020019static char *(p_ambw_values[]) = {"single", "double", NULL};
20static char *(p_bg_values[]) = {"light", "dark", NULL};
21static char *(p_bkc_values[]) = {"yes", "auto", "no", "breaksymlink", "breakhardlink", NULL};
22static char *(p_bo_values[]) = {"all", "backspace", "cursor", "complete",
23 "copy", "ctrlg", "error", "esc", "ex",
24 "hangul", "insertmode", "lang", "mess",
25 "showmatch", "operator", "register", "shell",
LemonBoy77771d32022-04-13 11:47:25 +010026 "spell", "term", "wildmode", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020027#if defined(FEAT_LINEBREAK)
28// Note: Keep this in sync with briopt_check()
29static char *(p_briopt_values[]) = {"shift:", "min:", "sbr", "list:", "column:", NULL};
30#endif
31#if defined(FEAT_DIFF)
32// Note: Keep this in sync with diffopt_changed()
33static char *(p_dip_values[]) = {"filler", "context:", "iblank", "icase", "iwhite", "iwhiteall", "iwhiteeol", "horizontal", "vertical", "closeoff", "hiddenoff", "foldcolumn:", "followwrap", "internal", "indent-heuristic", "algorithm:", NULL};
34static char *(p_dip_algorithm_values[]) = {"myers", "minimal", "patience", "histogram", NULL};
35#endif
distobs25ac6d62024-07-06 17:50:09 +020036static char *(p_nf_values[]) = {"bin", "octal", "hex", "alpha", "unsigned", "blank", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020037static char *(p_ff_values[]) = {FF_UNIX, FF_DOS, FF_MAC, NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020038#ifdef FEAT_CLIPBOARD
39// Note: Keep this in sync with did_set_clipboard()
40static char *(p_cb_values[]) = {"unnamed", "unnamedplus", "autoselect", "autoselectplus", "autoselectml", "html", "exclude:", NULL};
41#endif
Bram Moolenaardac13472019-09-16 21:06:21 +020042#ifdef FEAT_CRYPT
Christian Brabandtf573c6e2021-06-20 14:02:16 +020043static char *(p_cm_values[]) = {"zip", "blowfish", "blowfish2",
44 # ifdef FEAT_SODIUM
Christian Brabandtaae58342023-04-23 17:50:22 +010045 "xchacha20", "xchacha20v2",
Christian Brabandtf573c6e2021-06-20 14:02:16 +020046 # endif
47 NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020048#endif
49static char *(p_cmp_values[]) = {"internal", "keepascii", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020050#ifdef FEAT_SYN_HL
51// Note: Keep this in sync with fill_culopt_flags()
52static char *(p_culopt_values[]) = {"line", "screenline", "number", "both", NULL};
53#endif
Bram Moolenaardac13472019-09-16 21:06:21 +020054static char *(p_dy_values[]) = {"lastline", "truncate", "uhex", NULL};
Yegappan Lakshmanan87018252023-09-20 20:20:04 +020055static char *(p_jop_values[]) = {"stack", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020056#ifdef FEAT_FOLDING
57static char *(p_fdo_values[]) = {"all", "block", "hor", "mark", "percent",
58 "quickfix", "search", "tag", "insert",
59 "undo", "jump", NULL};
60#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +020061// Note: Keep this in sync with match_keyprotocol()
62static char *(p_kpc_protocol_values[]) = {"none", "mok2", "kitty", NULL};
63#ifdef FEAT_PROP_POPUP
64// Note: Keep this in sync with parse_popup_option()
65static char *(p_popup_option_values[]) = {"height:", "width:", "highlight:", "border:", "align:", NULL};
66static char *(p_popup_option_border_values[]) = {"on", "off", NULL};
67static char *(p_popup_option_align_values[]) = {"item", "menu", NULL};
68#endif
69#if defined(FEAT_SPELL)
70// Note: Keep this in sync with spell_check_sps()
71static char *(p_sps_values[]) = {"best", "fast", "double", "expr:", "file:", "timeout:", NULL};
72#endif
Bram Moolenaardac13472019-09-16 21:06:21 +020073#ifdef FEAT_SESSION
Bram Moolenaar635bd602021-04-16 19:58:22 +020074// Also used for 'viewoptions'! Keep in sync with SSOP_ flags.
Bram Moolenaardac13472019-09-16 21:06:21 +020075static char *(p_ssop_values[]) = {"buffers", "winpos", "resize", "winsize",
76 "localoptions", "options", "help", "blank", "globals", "slash", "unix",
Bram Moolenaar635bd602021-04-16 19:58:22 +020077 "sesdir", "curdir", "folds", "cursor", "tabpages", "terminal", "skiprtp",
78 NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020079#endif
Bram Moolenaar539aa6b2019-11-17 18:09:38 +010080// Keep in sync with SWB_ flags in option.h
81static char *(p_swb_values[]) = {"useopen", "usetab", "split", "newtab", "vsplit", "uselast", NULL};
Luuk van Baal13ece2a2022-10-03 15:28:08 +010082static char *(p_spk_values[]) = {"cursor", "screen", "topline", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020083static char *(p_tc_values[]) = {"followic", "ignore", "match", "followscs", "smart", NULL};
LemonBoy5247b0b2024-07-12 19:30:58 +020084// Keep in sync with TCL_ flags in option.h
85static char *(p_tcl_values[]) = {"left", "uselast", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020086#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
87static char *(p_toolbar_values[]) = {"text", "icons", "tooltips", "horiz", NULL};
88#endif
89#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
90static char *(p_tbis_values[]) = {"tiny", "small", "medium", "large", "huge", "giant", NULL};
91#endif
Bram Moolenaara1cb1d12019-10-17 23:00:07 +020092#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +020093static char *(p_ttym_values[]) = {"xterm", "xterm2", "dec", "netterm", "jsbterm", "pterm", "urxvt", "sgr", NULL};
94#endif
Gary Johnson53ba05b2021-07-26 22:19:10 +020095static char *(p_ve_values[]) = {"block", "insert", "all", "onemore", "none", "NONE", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020096// Note: Keep this in sync with check_opt_wim()
97static char *(p_wim_values[]) = {"full", "longest", "list", "lastused", NULL};
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000098static char *(p_wop_values[]) = {"fuzzy", "tagfile", "pum", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020099#ifdef FEAT_WAK
100static char *(p_wak_values[]) = {"yes", "menu", "no", NULL};
101#endif
102static char *(p_mousem_values[]) = {"extend", "popup", "popup_setpos", "mac", NULL};
103static char *(p_sel_values[]) = {"inclusive", "exclusive", "old", NULL};
104static char *(p_slm_values[]) = {"mouse", "key", "cmd", NULL};
105static char *(p_km_values[]) = {"startsel", "stopsel", NULL};
106#ifdef FEAT_BROWSE
107static char *(p_bsdir_values[]) = {"current", "last", "buffer", NULL};
108#endif
109static char *(p_scbopt_values[]) = {"ver", "hor", "jump", NULL};
110static char *(p_debug_values[]) = {"msg", "throw", "beep", NULL};
111static char *(p_ead_values[]) = {"both", "ver", "hor", NULL};
112static char *(p_buftype_values[]) = {"nofile", "nowrite", "quickfix", "help", "terminal", "acwrite", "prompt", "popup", NULL};
113static char *(p_bufhidden_values[]) = {"hide", "unload", "delete", "wipe", NULL};
Bram Moolenaaraa0489e2020-04-17 19:41:21 +0200114static char *(p_bs_values[]) = {"indent", "eol", "start", "nostop", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200115#ifdef FEAT_FOLDING
116static char *(p_fdm_values[]) = {"manual", "expr", "marker", "indent", "syntax",
117# ifdef FEAT_DIFF
118 "diff",
119# endif
120 NULL};
121static char *(p_fcl_values[]) = {"all", NULL};
122#endif
glepnir600a12d2024-07-09 18:51:29 +0200123static char *(p_cot_values[]) = {"menu", "menuone", "longest", "preview", "popup", "popuphidden", "noinsert", "noselect", "fuzzy", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200124#ifdef BACKSLASH_IN_FILENAME
125static char *(p_csl_values[]) = {"slash", "backslash", NULL};
126#endif
127#ifdef FEAT_SIGNS
128static char *(p_scl_values[]) = {"yes", "no", "auto", "number", NULL};
129#endif
130#if defined(MSWIN) && defined(FEAT_TERMINAL)
131static char *(p_twt_values[]) = {"winpty", "conpty", "", NULL};
132#endif
Luuk van Baalba936f62022-12-15 13:15:39 +0000133static char *(p_sloc_values[]) = {"last", "statusline", "tabline", NULL};
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +0000134static char *(p_sws_values[]) = {"fsync", "sync", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200135
136static int check_opt_strings(char_u *val, char **values, int list);
137static int opt_strings_flags(char_u *val, char **values, unsigned *flagp, int list);
138
139/*
140 * After setting various option values: recompute variables that depend on
141 * option values.
142 */
143 void
144didset_string_options(void)
145{
146 (void)opt_strings_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
147 (void)opt_strings_flags(p_bkc, p_bkc_values, &bkc_flags, TRUE);
148 (void)opt_strings_flags(p_bo, p_bo_values, &bo_flags, TRUE);
zeertzjq529b9ad2024-06-05 20:27:06 +0200149 (void)opt_strings_flags(p_cot, p_cot_values, &cot_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200150#ifdef FEAT_SESSION
151 (void)opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE);
152 (void)opt_strings_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
153#endif
154#ifdef FEAT_FOLDING
155 (void)opt_strings_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
156#endif
157 (void)opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE);
Yegappan Lakshmanan87018252023-09-20 20:20:04 +0200158 (void)opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200159 (void)opt_strings_flags(p_tc, p_tc_values, &tc_flags, FALSE);
160 (void)opt_strings_flags(p_ve, p_ve_values, &ve_flags, TRUE);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +0200161#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +0200162 (void)opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE);
163#endif
164#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
165 (void)opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags, TRUE);
166#endif
167#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
168 (void)opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE);
169#endif
Sean Dewar39c46b42022-05-12 17:44:29 +0100170 (void)opt_strings_flags(p_swb, p_swb_values, &swb_flags, TRUE);
LemonBoy5247b0b2024-07-12 19:30:58 +0200171 (void)opt_strings_flags(p_tcl, p_tcl_values, &tcl_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200172}
173
Yegappan Lakshmananf9dc2782023-05-11 15:02:56 +0100174#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200175/*
176 * Trigger the OptionSet autocommand.
177 * "opt_idx" is the index of the option being set.
178 * "opt_flags" can be OPT_LOCAL etc.
179 * "oldval" the old value
180 * "oldval_l" the old local value (only non-NULL if global and local value
181 * are set)
182 * "oldval_g" the old global value (only non-NULL if global and local value
183 * are set)
184 * "newval" the new value
185 */
186 void
zeertzjq269aa2b2022-11-28 11:36:50 +0000187trigger_optionset_string(
Bram Moolenaardac13472019-09-16 21:06:21 +0200188 int opt_idx,
189 int opt_flags,
190 char_u *oldval,
191 char_u *oldval_l,
192 char_u *oldval_g,
193 char_u *newval)
194{
195 // Don't do this recursively.
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000196 if (oldval == NULL || newval == NULL
197 || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
198 return;
Bram Moolenaardac13472019-09-16 21:06:21 +0200199
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000200 char_u buf_type[7];
201
202 sprintf((char *)buf_type, "%s",
Bram Moolenaardac13472019-09-16 21:06:21 +0200203 (opt_flags & OPT_LOCAL) ? "local" : "global");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000204 set_vim_var_string(VV_OPTION_OLD, oldval, -1);
205 set_vim_var_string(VV_OPTION_NEW, newval, -1);
206 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
207 if (opt_flags & OPT_LOCAL)
208 {
209 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
210 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
Bram Moolenaardac13472019-09-16 21:06:21 +0200211 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000212 if (opt_flags & OPT_GLOBAL)
213 {
214 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
215 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval, -1);
216 }
217 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
218 {
219 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
220 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval_l, -1);
221 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval_g, -1);
222 }
223 if (opt_flags & OPT_MODELINE)
224 {
225 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
226 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
227 }
228 apply_autocmds(EVENT_OPTIONSET,
229 get_option_fullname(opt_idx), NULL, FALSE,
230 NULL);
231 reset_v_option_vars();
Bram Moolenaardac13472019-09-16 21:06:21 +0200232}
233#endif
234
235 static char *
Mike Williams620f0112023-12-05 15:36:06 +0100236illegal_char(char *errbuf, size_t errbuflen, int c)
Bram Moolenaardac13472019-09-16 21:06:21 +0200237{
238 if (errbuf == NULL)
239 return "";
zeertzjq6a8d2e12024-01-17 20:54:49 +0100240 vim_snprintf(errbuf, errbuflen, _(e_illegal_character_str),
Christian Brabandtb39b2402023-11-29 11:34:05 +0100241 (char *)transchar(c));
Bram Moolenaardac13472019-09-16 21:06:21 +0200242 return errbuf;
243}
244
245/*
246 * Check string options in a buffer for NULL value.
247 */
248 void
249check_buf_options(buf_T *buf)
250{
251 check_string_option(&buf->b_p_bh);
252 check_string_option(&buf->b_p_bt);
253 check_string_option(&buf->b_p_fenc);
254 check_string_option(&buf->b_p_ff);
255#ifdef FEAT_FIND_ID
256 check_string_option(&buf->b_p_def);
257 check_string_option(&buf->b_p_inc);
258# ifdef FEAT_EVAL
259 check_string_option(&buf->b_p_inex);
260# endif
261#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +0100262#if defined(FEAT_EVAL)
Bram Moolenaardac13472019-09-16 21:06:21 +0200263 check_string_option(&buf->b_p_inde);
264 check_string_option(&buf->b_p_indk);
265#endif
266#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
267 check_string_option(&buf->b_p_bexpr);
268#endif
269#if defined(FEAT_CRYPT)
270 check_string_option(&buf->b_p_cm);
271#endif
272 check_string_option(&buf->b_p_fp);
273#if defined(FEAT_EVAL)
274 check_string_option(&buf->b_p_fex);
275#endif
276#ifdef FEAT_CRYPT
277 check_string_option(&buf->b_p_key);
278#endif
279 check_string_option(&buf->b_p_kp);
280 check_string_option(&buf->b_p_mps);
281 check_string_option(&buf->b_p_fo);
282 check_string_option(&buf->b_p_flp);
283 check_string_option(&buf->b_p_isk);
Bram Moolenaardac13472019-09-16 21:06:21 +0200284 check_string_option(&buf->b_p_com);
Bram Moolenaardac13472019-09-16 21:06:21 +0200285#ifdef FEAT_FOLDING
286 check_string_option(&buf->b_p_cms);
287#endif
288 check_string_option(&buf->b_p_nf);
Bram Moolenaardac13472019-09-16 21:06:21 +0200289 check_string_option(&buf->b_p_qe);
Bram Moolenaardac13472019-09-16 21:06:21 +0200290#ifdef FEAT_SYN_HL
291 check_string_option(&buf->b_p_syn);
292 check_string_option(&buf->b_s.b_syn_isk);
293#endif
294#ifdef FEAT_SPELL
295 check_string_option(&buf->b_s.b_p_spc);
296 check_string_option(&buf->b_s.b_p_spf);
297 check_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +0200298 check_string_option(&buf->b_s.b_p_spo);
Bram Moolenaardac13472019-09-16 21:06:21 +0200299#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200300 check_string_option(&buf->b_p_sua);
Bram Moolenaardac13472019-09-16 21:06:21 +0200301 check_string_option(&buf->b_p_cink);
302 check_string_option(&buf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +0100303 check_string_option(&buf->b_p_cinsd);
Bram Moolenaardac13472019-09-16 21:06:21 +0200304 parse_cino(buf);
Bram Moolenaar49846fb2022-10-15 16:05:33 +0100305 check_string_option(&buf->b_p_lop);
Bram Moolenaardac13472019-09-16 21:06:21 +0200306 check_string_option(&buf->b_p_ft);
Bram Moolenaardac13472019-09-16 21:06:21 +0200307 check_string_option(&buf->b_p_cinw);
zeertzjq529b9ad2024-06-05 20:27:06 +0200308 check_string_option(&buf->b_p_cot);
Bram Moolenaardac13472019-09-16 21:06:21 +0200309 check_string_option(&buf->b_p_cpt);
310#ifdef FEAT_COMPL_FUNC
311 check_string_option(&buf->b_p_cfu);
312 check_string_option(&buf->b_p_ofu);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +0100313 check_string_option(&buf->b_p_tsrfu);
Bram Moolenaardac13472019-09-16 21:06:21 +0200314#endif
315#ifdef FEAT_EVAL
316 check_string_option(&buf->b_p_tfu);
317#endif
318#ifdef FEAT_KEYMAP
319 check_string_option(&buf->b_p_keymap);
320#endif
321#ifdef FEAT_QUICKFIX
322 check_string_option(&buf->b_p_gp);
323 check_string_option(&buf->b_p_mp);
324 check_string_option(&buf->b_p_efm);
325#endif
326 check_string_option(&buf->b_p_ep);
327 check_string_option(&buf->b_p_path);
328 check_string_option(&buf->b_p_tags);
329 check_string_option(&buf->b_p_tc);
330 check_string_option(&buf->b_p_dict);
331 check_string_option(&buf->b_p_tsr);
Bram Moolenaardac13472019-09-16 21:06:21 +0200332 check_string_option(&buf->b_p_lw);
Bram Moolenaardac13472019-09-16 21:06:21 +0200333 check_string_option(&buf->b_p_bkc);
334 check_string_option(&buf->b_p_menc);
335#ifdef FEAT_VARTABS
336 check_string_option(&buf->b_p_vsts);
337 check_string_option(&buf->b_p_vts);
338#endif
339}
340
341/*
342 * Free the string allocated for an option.
343 * Checks for the string being empty_option. This may happen if we're out of
344 * memory, vim_strsave() returned NULL, which was replaced by empty_option by
345 * check_options().
346 * Does NOT check for P_ALLOCED flag!
347 */
348 void
349free_string_option(char_u *p)
350{
351 if (p != empty_option)
352 vim_free(p);
353}
354
355 void
356clear_string_option(char_u **pp)
357{
358 if (*pp != empty_option)
359 vim_free(*pp);
360 *pp = empty_option;
361}
362
363 void
364check_string_option(char_u **pp)
365{
366 if (*pp == NULL)
367 *pp = empty_option;
368}
369
370/*
371 * Set global value for string option when it's a local option.
372 */
373 static void
374set_string_option_global(
375 int opt_idx, // option index
376 char_u **varp) // pointer to option variable
377{
378 char_u **p, *s;
379
380 // the global value is always allocated
381 if (is_window_local_option(opt_idx))
382 p = (char_u **)GLOBAL_WO(varp);
383 else
384 p = (char_u **)get_option_var(opt_idx);
385 if (!is_global_option(opt_idx)
386 && p != varp
387 && (s = vim_strsave(*varp)) != NULL)
388 {
389 free_string_option(*p);
390 *p = s;
391 }
392}
393
394/*
395 * Set a string option to a new value (without checking the effect).
396 * The string is copied into allocated memory.
397 * if ("opt_idx" == -1) "name" is used, otherwise "opt_idx" is used.
398 * When "set_sid" is zero set the scriptID to current_sctx.sc_sid. When
399 * "set_sid" is SID_NONE don't set the scriptID. Otherwise set the scriptID to
400 * "set_sid".
401 */
402 void
403set_string_option_direct(
404 char_u *name,
405 int opt_idx,
406 char_u *val,
407 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
408 int set_sid UNUSED)
409{
410 char_u *s;
411 char_u **varp;
412 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
413 int idx = opt_idx;
414
415 if (idx == -1) // use name
416 {
417 idx = findoption(name);
418 if (idx < 0) // not found (should not happen)
419 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000420 semsg(_(e_internal_error_str), "set_string_option_direct()");
RestorerZ68ebcee2023-05-31 17:12:14 +0100421 siemsg("For option %s", name);
Bram Moolenaardac13472019-09-16 21:06:21 +0200422 return;
423 }
424 }
425
426 if (is_hidden_option(idx)) // can't set hidden option
427 return;
428
429 s = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000430 if (s == NULL)
431 return;
432
433 varp = (char_u **)get_option_varp_scope(idx,
434 both ? OPT_LOCAL : opt_flags);
435 if ((opt_flags & OPT_FREE) && (get_option_flags(idx) & P_ALLOCED))
436 free_string_option(*varp);
437 *varp = s;
438
439 // For buffer/window local option may also set the global value.
440 if (both)
441 set_string_option_global(idx, varp);
442
443 set_option_flag(idx, P_ALLOCED);
444
445 // When setting both values of a global option with a local value,
446 // make the local value empty, so that the global value is used.
447 if (is_global_local_option(idx) && both)
Bram Moolenaardac13472019-09-16 21:06:21 +0200448 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000449 free_string_option(*varp);
450 *varp = empty_option;
Bram Moolenaardac13472019-09-16 21:06:21 +0200451 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000452# ifdef FEAT_EVAL
453 if (set_sid != SID_NONE)
454 {
455 sctx_T script_ctx;
456
457 if (set_sid == 0)
458 script_ctx = current_sctx;
459 else
460 {
461 script_ctx.sc_sid = set_sid;
462 script_ctx.sc_seq = 0;
463 script_ctx.sc_lnum = 0;
464 script_ctx.sc_version = 1;
465 }
466 set_option_sctx_idx(idx, opt_flags, script_ctx);
467 }
468# endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200469}
470
Dominique Pellee764d1b2023-03-12 21:20:59 +0000471#if defined(FEAT_PROP_POPUP) || \
472 (defined(FEAT_DIFF) && defined(FEAT_FOLDING)) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200473/*
474 * Like set_string_option_direct(), but for a window-local option in "wp".
475 * Blocks autocommands to avoid the old curwin becoming invalid.
476 */
477 void
478set_string_option_direct_in_win(
479 win_T *wp,
480 char_u *name,
481 int opt_idx,
482 char_u *val,
483 int opt_flags,
484 int set_sid)
485{
486 win_T *save_curwin = curwin;
487
488 block_autocmds();
489 curwin = wp;
490 curbuf = curwin->w_buffer;
491 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
492 curwin = save_curwin;
493 curbuf = curwin->w_buffer;
494 unblock_autocmds();
495}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000496#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200497
Dominique Pelle748b3082022-01-08 12:41:16 +0000498#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200499/*
500 * Like set_string_option_direct(), but for a buffer-local option in "buf".
501 * Blocks autocommands to avoid the old curbuf becoming invalid.
502 */
503 void
504set_string_option_direct_in_buf(
505 buf_T *buf,
506 char_u *name,
507 int opt_idx,
508 char_u *val,
509 int opt_flags,
510 int set_sid)
511{
512 buf_T *save_curbuf = curbuf;
513
514 block_autocmds();
515 curbuf = buf;
516 curwin->w_buffer = curbuf;
517 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
518 curbuf = save_curbuf;
519 curwin->w_buffer = curbuf;
520 unblock_autocmds();
521}
Dominique Pelle748b3082022-01-08 12:41:16 +0000522#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200523
524/*
525 * Set a string option to a new value, and handle the effects.
526 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100527 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaardac13472019-09-16 21:06:21 +0200528 */
529 char *
530set_string_option(
531 int opt_idx,
532 char_u *value,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +0000533 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Christian Brabandtb39b2402023-11-29 11:34:05 +0100534 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +0100535 size_t errbuflen)
Bram Moolenaardac13472019-09-16 21:06:21 +0200536{
537 char_u *s;
538 char_u **varp;
539 char_u *oldval;
540#if defined(FEAT_EVAL)
541 char_u *oldval_l = NULL;
542 char_u *oldval_g = NULL;
543 char_u *saved_oldval = NULL;
544 char_u *saved_oldval_l = NULL;
545 char_u *saved_oldval_g = NULL;
546 char_u *saved_newval = NULL;
547#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100548 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +0200549 int value_checked = FALSE;
550
551 if (is_hidden_option(opt_idx)) // don't set hidden option
552 return NULL;
553
Bram Moolenaar7f009df2020-03-16 20:27:38 +0100554 s = vim_strsave(value == NULL ? (char_u *)"" : value);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000555 if (s == NULL)
556 return NULL;
557
558 varp = (char_u **)get_option_varp_scope(opt_idx,
559 (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
560 ? (is_global_local_option(opt_idx)
561 ? OPT_GLOBAL : OPT_LOCAL)
562 : opt_flags);
563 oldval = *varp;
564#if defined(FEAT_EVAL)
565 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +0200566 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000567 oldval_l = *(char_u **)get_option_varp_scope(opt_idx, OPT_LOCAL);
568 oldval_g = *(char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
Bram Moolenaardac13472019-09-16 21:06:21 +0200569 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000570#endif
571 *varp = s;
572
573#if defined(FEAT_EVAL)
574 if (!starting
575# ifdef FEAT_CRYPT
576 && !is_crypt_key_option(opt_idx)
577# endif
578 )
579 {
580 if (oldval_l != NULL)
581 saved_oldval_l = vim_strsave(oldval_l);
582 if (oldval_g != NULL)
583 saved_oldval_g = vim_strsave(oldval_g);
584 saved_oldval = vim_strsave(oldval);
585 saved_newval = vim_strsave(s);
586 }
587#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000588 if ((errmsg = did_set_string_option(opt_idx, varp, oldval, value, errbuf,
Christian Brabandtb39b2402023-11-29 11:34:05 +0100589 errbuflen, opt_flags, OP_NONE, &value_checked)) == NULL)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000590 did_set_option(opt_idx, opt_flags, TRUE, value_checked);
591
592#if defined(FEAT_EVAL)
593 // call autocommand after handling side effects
594 if (errmsg == NULL)
595 trigger_optionset_string(opt_idx, opt_flags,
596 saved_oldval, saved_oldval_l,
597 saved_oldval_g, saved_newval);
598 vim_free(saved_oldval);
599 vim_free(saved_oldval_l);
600 vim_free(saved_oldval_g);
601 vim_free(saved_newval);
602#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100603 return errmsg;
Bram Moolenaardac13472019-09-16 21:06:21 +0200604}
605
606/*
607 * Return TRUE if "val" is a valid 'filetype' name.
608 * Also used for 'syntax' and 'keymap'.
609 */
610 static int
611valid_filetype(char_u *val)
612{
613 return valid_name(val, ".-_");
614}
615
616#ifdef FEAT_STL_OPT
617/*
618 * Check validity of options with the 'statusline' format.
zeertzjq5dc294a2022-04-15 13:17:57 +0100619 * Return an untranslated error message or NULL.
Bram Moolenaardac13472019-09-16 21:06:21 +0200620 */
621 static char *
622check_stl_option(char_u *s)
623{
Bram Moolenaardac13472019-09-16 21:06:21 +0200624 int groupdepth = 0;
Christian Brabandtb39b2402023-11-29 11:34:05 +0100625 static char errbuf[ERR_BUFLEN];
626 int errbuflen = ERR_BUFLEN;
Bram Moolenaardac13472019-09-16 21:06:21 +0200627
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100628 while (*s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200629 {
630 // Check for valid keys after % sequences
631 while (*s && *s != '%')
632 s++;
633 if (!*s)
634 break;
635 s++;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +0000636 if (*s == '%' || *s == STL_TRUNCMARK || *s == STL_SEPARATE)
Bram Moolenaardac13472019-09-16 21:06:21 +0200637 {
638 s++;
639 continue;
640 }
641 if (*s == ')')
642 {
643 s++;
644 if (--groupdepth < 0)
645 break;
646 continue;
647 }
648 if (*s == '-')
649 s++;
650 while (VIM_ISDIGIT(*s))
651 s++;
652 if (*s == STL_USER_HL)
653 continue;
654 if (*s == '.')
655 {
656 s++;
657 while (*s && VIM_ISDIGIT(*s))
658 s++;
659 }
660 if (*s == '(')
661 {
662 groupdepth++;
663 continue;
664 }
665 if (vim_strchr(STL_ALL, *s) == NULL)
666 {
Christian Brabandtb39b2402023-11-29 11:34:05 +0100667 return illegal_char(errbuf, errbuflen, *s);
Bram Moolenaardac13472019-09-16 21:06:21 +0200668 }
669 if (*s == '{')
670 {
zeertzjq5dc294a2022-04-15 13:17:57 +0100671 int reevaluate = (*++s == '%');
shadmansaleh30e3de22021-05-15 17:23:28 +0200672
zeertzjq5dc294a2022-04-15 13:17:57 +0100673 if (reevaluate && *++s == '}')
674 // "}" is not allowed immediately after "%{%"
Christian Brabandtb39b2402023-11-29 11:34:05 +0100675 return illegal_char(errbuf, errbuflen, '}');
shadmansaleh30e3de22021-05-15 17:23:28 +0200676 while ((*s != '}' || (reevaluate && s[-1] != '%')) && *s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200677 s++;
678 if (*s != '}')
zeertzjq5dc294a2022-04-15 13:17:57 +0100679 return e_unclosed_expression_sequence;
Bram Moolenaardac13472019-09-16 21:06:21 +0200680 }
681 }
Bram Moolenaardac13472019-09-16 21:06:21 +0200682 if (groupdepth != 0)
zeertzjq5dc294a2022-04-15 13:17:57 +0100683 return e_unbalanced_groups;
Bram Moolenaardac13472019-09-16 21:06:21 +0200684 return NULL;
685}
686#endif
687
688/*
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +0000689 * Check for a "normal" directory or file name in some options. Disallow a
690 * path separator (slash and/or backslash), wildcards and characters that are
691 * often illegal in a file name. Be more permissive if "secure" is off.
692 */
693 static int
694check_illegal_path_names(int opt_idx, char_u **varp)
695{
696 return (((get_option_flags(opt_idx) & P_NFNAME)
697 && vim_strpbrk(*varp, (char_u *)(secure
698 ? "/\\*?[|;&<>\r\n" : "/\\*?[<>\r\n")) != NULL)
699 || ((get_option_flags(opt_idx) & P_NDNAME)
700 && vim_strpbrk(*varp, (char_u *)"*?[|;&<>\r\n") != NULL));
701}
702
703/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000704 * An option that accepts a list of flags is changed.
705 * e.g. 'viewoptions', 'switchbuf', 'casemap', etc.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000706 */
707 static char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000708did_set_opt_flags(char_u *val, char **values, unsigned *flagp, int list)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000709{
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000710 if (opt_strings_flags(val, values, flagp, list) == FAIL)
711 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000712
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000713 return NULL;
714}
715
716/*
717 * An option that accepts a list of string values is changed.
718 * e.g. 'nrformats', 'scrollopt', 'wildoptions', etc.
719 */
720 static char *
721did_set_opt_strings(char_u *val, char **values, int list)
722{
723 return did_set_opt_flags(val, values, NULL, list);
724}
725
726/*
727 * An option which is a list of flags is set. Valid values are in 'flags'.
728 */
729 static char *
Christian Brabandtb39b2402023-11-29 11:34:05 +0100730did_set_option_listflag(
731 char_u *val,
732 char_u *flags,
733 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +0100734 size_t errbuflen)
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000735{
736 char_u *s;
737
Yegappan Lakshmananc727b192023-03-03 12:26:15 +0000738 for (s = val; *s; ++s)
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000739 if (vim_strchr(flags, *s) == NULL)
Christian Brabandtb39b2402023-11-29 11:34:05 +0100740 return illegal_char(errbuf, errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000741
742 return NULL;
743}
744
745/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200746 * Expand an option that accepts a list of fixed string values with known
747 * number of items.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200748 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200749 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200750expand_set_opt_string(
751 optexpand_T *args,
752 char **values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +0200753 size_t numValues,
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200754 int *numMatches,
755 char_u ***matches)
756{
757 char_u *p;
758 regmatch_T *regmatch = args->oe_regmatch;
759 int include_orig_val = args->oe_include_orig_val;
760 char_u *option_val = args->oe_opt_value;
761
762 // Assume numValues is small since they are fixed enums, so just allocate
763 // upfront instead of needing two passes to calculate output size.
764 *matches = ALLOC_MULT(char_u *, numValues + 1);
765 if (*matches == NULL)
766 return FAIL;
767
768 int count = 0;
769
770 if (include_orig_val && *option_val != NUL)
771 {
772 p = vim_strsave(option_val);
773 if (p == NULL)
774 {
775 VIM_CLEAR(*matches);
776 return FAIL;
777 }
778 (*matches)[count++] = p;
779 }
780
781 for (char **val = values; *val != NULL; val++)
782 {
783 if (include_orig_val && *option_val != NUL)
784 {
785 if (STRCMP((char_u*)*val, option_val) == 0)
786 continue;
787 }
788 if (vim_regexec(regmatch, (char_u*)(*val), (colnr_T)0))
789 {
790 p = vim_strsave((char_u*)*val);
791 if (p == NULL)
792 {
793 if (count == 0)
794 {
795 VIM_CLEAR(*matches);
796 return FAIL;
797 }
798 else
799 break;
800 }
801 (*matches)[count++] = p;
802 }
803 }
804 if (count == 0)
805 {
806 VIM_CLEAR(*matches);
807 return FAIL;
808 }
809 *numMatches = count;
810 return OK;
811}
812
813static char_u *set_opt_callback_orig_option = NULL;
814static char_u *((*set_opt_callback_func)(expand_T *, int));
815
816/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200817 * Callback used by expand_set_opt_generic to also include the original value
818 * as the first item.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200819 */
820 static char_u *
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200821expand_set_opt_generic_cb(expand_T *xp, int idx)
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200822{
823 if (idx == 0)
824 {
825 if (set_opt_callback_orig_option != NULL)
826 return set_opt_callback_orig_option;
827 else
828 return (char_u *)""; // empty strings are ignored
829 }
830 return set_opt_callback_func(xp, idx - 1);
831}
832
833/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200834 * Expand an option with a callback that iterates through a list of possible
835 * names using an index.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200836 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200837 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200838expand_set_opt_generic(
839 optexpand_T *args,
840 char_u *((*func)(expand_T *, int)),
841 int *numMatches,
842 char_u ***matches)
843{
844 int ret;
845
846 set_opt_callback_orig_option = args->oe_include_orig_val ?
847 args->oe_opt_value : NULL;
848 set_opt_callback_func = func;
849
850 ret = ExpandGeneric(
851 (char_u*)"", // not using fuzzy as currently EXPAND_STRING_SETTING doesn't use it
852 args->oe_xp,
853 args->oe_regmatch,
854 matches,
855 numMatches,
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200856 expand_set_opt_generic_cb,
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200857 FALSE);
858
859 set_opt_callback_orig_option = NULL;
860 set_opt_callback_func = NULL;
861 return ret;
862}
863
Christian Brabandt9960ebc2023-10-05 22:17:09 +0200864# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200865static garray_T *expand_cb_ga;
866static optexpand_T *expand_cb_args;
867
868/*
869 * Callback provided to a function in expand_set_opt_callback. Will perform
870 * regex matching against the value and add to the list.
871 *
872 * Returns OK usually. Returns FAIL if it failed to allocate memory, and the
873 * caller should terminate the enumeration.
874 */
875 static int
876expand_set_opt_callback_cb(char_u *val)
877{
878 regmatch_T *regmatch = expand_cb_args->oe_regmatch;
879 expand_T *xp = expand_cb_args->oe_xp;
880 garray_T *ga = expand_cb_ga;
881 char_u *str;
882
883 if (val == NULL || *val == NUL)
884 return OK;
885
886 if (xp->xp_pattern[0] != NUL &&
887 !vim_regexec(regmatch, val, (colnr_T)0))
888 return OK;
889
890 str = vim_strsave_escaped(val, (char_u *)" \t\\");
891
892 if (str == NULL)
893 return FAIL;
894
895 if (ga_grow(ga, 1) == FAIL)
896 {
897 vim_free(str);
898 return FAIL;
899 }
900
901 ((char_u **)ga->ga_data)[ga->ga_len] = str;
902 ++ga->ga_len;
903 return OK;
904}
905
906/*
907 * Expand an option with a provided function that takes a callback. The
908 * function will enumerate through all options and call the callback to add it
909 * to the list.
910 *
911 * "func" is the enumerator function that will generate the list of options.
912 * "func_params" is a single parameter that will be passed to func.
913 */
914 static int
915expand_set_opt_callback(
916 optexpand_T *args,
917 void (*func)(optexpand_T *, void* params, int (*cb)(char_u *val)),
918 void *func_params,
919 int *numMatches,
920 char_u ***matches)
921{
922 garray_T ga;
923 int include_orig_val = args->oe_include_orig_val;
924 char_u *option_val = args->oe_opt_value;
925
926 ga_init2(&ga, sizeof(char *), 30);
927
928 if (include_orig_val && *option_val != NUL)
929 {
930 char_u *p = vim_strsave(option_val);
931 if (p == NULL)
932 return FAIL;
933 if (ga_grow(&ga, 1) == FAIL)
934 {
935 vim_free(p);
936 return FAIL;
937 }
938 ((char_u **)ga.ga_data)[ga.ga_len] = p;
939 ++ga.ga_len;
940 }
941
942 expand_cb_ga = &ga;
943 expand_cb_args = args;
944
945 func(args, func_params, expand_set_opt_callback_cb);
946
947 expand_cb_ga = NULL;
948 expand_cb_args = NULL;
949
950 *matches = ga.ga_data;
951 *numMatches = ga.ga_len;
952 return OK;
953}
Christian Brabandt9960ebc2023-10-05 22:17:09 +0200954#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200955
956/*
957 * Expand an option which is a list of flags.
958 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200959 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200960expand_set_opt_listflag(
961 optexpand_T *args,
962 char_u *flags,
963 int *numMatches,
964 char_u ***matches)
965{
966 char_u *p;
967 char_u *option_val = args->oe_opt_value;
968 char_u *cmdline_val = args->oe_set_arg;
969 int append = args->oe_append;
970 int include_orig_val = args->oe_include_orig_val && (*option_val != NUL);
971
Yee Cheng Chin6d113472023-10-02 21:38:39 +0200972 size_t num_flags = STRLEN(flags);
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200973
974 // Assume we only have small number of flags, so just allocate max size.
975 *matches = ALLOC_MULT(char_u *, num_flags + 1);
976 if (*matches == NULL)
977 return FAIL;
978
979 int count = 0;
980
981 if (include_orig_val)
982 {
983 p = vim_strsave(option_val);
984 if (p == NULL)
985 {
986 VIM_CLEAR(*matches);
987 return FAIL;
988 }
989 (*matches)[count++] = p;
990 }
991
992 for (char_u *flag = flags; *flag != NUL; flag++)
993 {
994 if (append && vim_strchr(option_val, *flag) != NULL)
995 continue;
996
997 if (vim_strchr(cmdline_val, *flag) == NULL)
998 {
999 if (include_orig_val
1000 && option_val[1] == NUL
1001 && *flag == option_val[0])
1002 {
1003 // This value is already used as the first choice as it's the
1004 // existing flag. Just skip it to avoid duplicate.
1005 continue;
1006 }
1007 p = vim_strnsave(flag, 1);
1008 if (p == NULL)
1009 {
1010 if (count == 0)
1011 {
1012 VIM_CLEAR(*matches);
1013 return FAIL;
1014 }
1015 else
1016 break;
1017 }
1018 (*matches)[count++] = p;
1019 }
1020 }
1021
1022 if (count == 0)
1023 {
1024 VIM_CLEAR(*matches);
1025 return FAIL;
1026 }
1027 *numMatches = count;
1028 return OK;
1029}
1030
1031/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001032 * The 'ambiwidth' option is changed.
1033 */
1034 char *
1035did_set_ambiwidth(optset_T *args UNUSED)
1036{
1037 if (check_opt_strings(p_ambw, p_ambw_values, FALSE) != OK)
1038 return e_invalid_argument;
1039
1040 return check_chars_options();
1041}
1042
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001043 int
1044expand_set_ambiwidth(optexpand_T *args, int *numMatches, char_u ***matches)
1045{
1046 return expand_set_opt_string(
1047 args,
1048 p_ambw_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001049 ARRAY_LENGTH(p_ambw_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001050 numMatches,
1051 matches);
1052}
1053
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001054/*
1055 * The 'background' option is changed.
1056 */
1057 char *
Gregory Anders83ad2722024-01-03 19:48:51 +01001058did_set_background(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001059{
1060 if (check_opt_strings(p_bg, p_bg_values, FALSE) == FAIL)
1061 return e_invalid_argument;
1062
Gregory Anders83ad2722024-01-03 19:48:51 +01001063 if (args->os_oldval.string != NULL && args->os_oldval.string[0] == *p_bg)
1064 // Value was not changed
1065 return NULL;
1066
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001067#ifdef FEAT_EVAL
1068 int dark = (*p_bg == 'd');
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001069#endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001070
1071 init_highlight(FALSE, FALSE);
1072
1073#ifdef FEAT_EVAL
1074 if (dark != (*p_bg == 'd')
1075 && get_var_value((char_u *)"g:colors_name") != NULL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001076 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001077 // The color scheme must have set 'background' back to another
1078 // value, that's not what we want here. Disable the color
1079 // scheme and set the colors again.
1080 do_unlet((char_u *)"g:colors_name", TRUE);
1081 free_string_option(p_bg);
1082 p_bg = vim_strsave((char_u *)(dark ? "dark" : "light"));
1083 check_string_option(&p_bg);
1084 init_highlight(FALSE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001085 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001086#endif
1087#ifdef FEAT_TERMINAL
1088 term_update_colors_all();
1089#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001090
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001091 return NULL;
1092}
1093
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001094 int
1095expand_set_background(optexpand_T *args, int *numMatches, char_u ***matches)
1096{
1097 return expand_set_opt_string(
1098 args,
1099 p_bg_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001100 ARRAY_LENGTH(p_bg_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001101 numMatches,
1102 matches);
1103}
1104
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001105/*
1106 * The 'backspace' option is changed.
1107 */
1108 char *
1109did_set_backspace(optset_T *args UNUSED)
1110{
1111 if (VIM_ISDIGIT(*p_bs))
1112 {
1113 if (*p_bs > '3' || p_bs[1] != NUL)
1114 return e_invalid_argument;
1115 }
1116 else if (check_opt_strings(p_bs, p_bs_values, TRUE) != OK)
1117 return e_invalid_argument;
1118
1119 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001120}
1121
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001122 int
1123expand_set_backspace(optexpand_T *args, int *numMatches, char_u ***matches)
1124{
1125 return expand_set_opt_string(
1126 args,
1127 p_bs_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001128 ARRAY_LENGTH(p_bs_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001129 numMatches,
1130 matches);
1131}
1132
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001133/*
1134 * The 'backupcopy' option is changed.
1135 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001136 char *
1137did_set_backupcopy(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001138{
1139 char_u *bkc = p_bkc;
1140 unsigned int *flags = &bkc_flags;
1141 char *errmsg = NULL;
1142
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001143 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001144 {
1145 bkc = curbuf->b_p_bkc;
1146 flags = &curbuf->b_bkc_flags;
1147 }
1148
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001149 if ((args->os_flags & OPT_LOCAL) && *bkc == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001150 // make the local value empty: use the global value
1151 *flags = 0;
1152 else
1153 {
1154 if (opt_strings_flags(bkc, p_bkc_values, flags, TRUE) != OK)
1155 errmsg = e_invalid_argument;
1156 if ((((int)*flags & BKC_AUTO) != 0)
1157 + (((int)*flags & BKC_YES) != 0)
1158 + (((int)*flags & BKC_NO) != 0) != 1)
1159 {
1160 // Must have exactly one of "auto", "yes" and "no".
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001161 (void)opt_strings_flags(args->os_oldval.string, p_bkc_values,
1162 flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001163 errmsg = e_invalid_argument;
1164 }
1165 }
1166
1167 return errmsg;
1168}
1169
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001170 int
1171expand_set_backupcopy(optexpand_T *args, int *numMatches, char_u ***matches)
1172{
1173 return expand_set_opt_string(
1174 args,
1175 p_bkc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001176 ARRAY_LENGTH(p_bkc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001177 numMatches,
1178 matches);
1179}
1180
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001181/*
1182 * The 'backupext' or the 'patchmode' option is changed.
1183 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001184 char *
1185did_set_backupext_or_patchmode(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001186{
1187 if (STRCMP(*p_bex == '.' ? p_bex + 1 : p_bex,
1188 *p_pm == '.' ? p_pm + 1 : p_pm) == 0)
1189 return e_backupext_and_patchmode_are_equal;
1190
1191 return NULL;
1192}
1193
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001194/*
1195 * The 'belloff' option is changed.
1196 */
1197 char *
1198did_set_belloff(optset_T *args UNUSED)
1199{
1200 return did_set_opt_flags(p_bo, p_bo_values, &bo_flags, TRUE);
1201}
1202
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001203 int
1204expand_set_belloff(optexpand_T *args, int *numMatches, char_u ***matches)
1205{
1206 return expand_set_opt_string(
1207 args,
1208 p_bo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001209 ARRAY_LENGTH(p_bo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001210 numMatches,
1211 matches);
1212}
1213
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001214#if defined(FEAT_LINEBREAK) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001215/*
zeertzjqeac3fdc2024-02-03 18:08:09 +01001216 * The 'breakat' option is changed.
1217 */
1218 char *
1219did_set_breakat(optset_T *args UNUSED)
1220{
1221 char_u *p;
1222 int i;
1223
1224 for (i = 0; i < 256; i++)
1225 breakat_flags[i] = FALSE;
1226
1227 if (p_breakat != NULL)
1228 for (p = p_breakat; *p; p++)
1229 breakat_flags[*p] = TRUE;
1230
1231 return NULL;
1232}
1233
1234/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001235 * The 'breakindentopt' option is changed.
1236 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001237 char *
1238did_set_breakindentopt(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001239{
1240 char *errmsg = NULL;
1241
1242 if (briopt_check(curwin) == FAIL)
1243 errmsg = e_invalid_argument;
1244 // list setting requires a redraw
1245 if (curwin->w_briopt_list)
1246 redraw_all_later(UPD_NOT_VALID);
1247
1248 return errmsg;
1249}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001250
1251 int
1252expand_set_breakindentopt(optexpand_T *args, int *numMatches, char_u ***matches)
1253{
1254 return expand_set_opt_string(
1255 args,
1256 p_briopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001257 ARRAY_LENGTH(p_briopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001258 numMatches,
1259 matches);
1260}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001261#endif
1262
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001263#if defined(FEAT_BROWSE) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001264/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001265 * The 'browsedir' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001266 */
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00001267 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001268did_set_browsedir(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001269{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001270 if (check_opt_strings(p_bsdir, p_bsdir_values, FALSE) != OK
1271 && !mch_isdir(p_bsdir))
1272 return e_invalid_argument;
1273
1274 return NULL;
1275}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001276
1277 int
1278expand_set_browsedir(optexpand_T *args, int *numMatches, char_u ***matches)
1279{
1280 return expand_set_opt_string(
1281 args,
1282 p_bsdir_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001283 ARRAY_LENGTH(p_bsdir_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001284 numMatches,
1285 matches);
1286}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001287#endif
1288
1289/*
1290 * The 'bufhidden' option is changed.
1291 */
1292 char *
1293did_set_bufhidden(optset_T *args UNUSED)
1294{
1295 return did_set_opt_strings(curbuf->b_p_bh, p_bufhidden_values, FALSE);
1296}
1297
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001298 int
1299expand_set_bufhidden(optexpand_T *args, int *numMatches, char_u ***matches)
1300{
1301 return expand_set_opt_string(
1302 args,
1303 p_bufhidden_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001304 ARRAY_LENGTH(p_bufhidden_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001305 numMatches,
1306 matches);
1307}
1308
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001309/*
1310 * The 'buftype' option is changed.
1311 */
1312 char *
1313did_set_buftype(optset_T *args UNUSED)
1314{
1315 if (check_opt_strings(curbuf->b_p_bt, p_buftype_values, FALSE) != OK)
1316 return e_invalid_argument;
1317
1318 if (curwin->w_status_height)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001319 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001320 curwin->w_redr_status = TRUE;
1321 redraw_later(UPD_VALID);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001322 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001323 curbuf->b_help = (curbuf->b_p_bt[0] == 'h');
1324 redraw_titles();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001325
1326 return NULL;
1327}
1328
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001329 int
1330expand_set_buftype(optexpand_T *args, int *numMatches, char_u ***matches)
1331{
1332 return expand_set_opt_string(
1333 args,
1334 p_buftype_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001335 ARRAY_LENGTH(p_buftype_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001336 numMatches,
1337 matches);
1338}
1339
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001340/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001341 * The 'casemap' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001342 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001343 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001344did_set_casemap(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001345{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001346 return did_set_opt_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
1347}
1348
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001349 int
1350expand_set_casemap(optexpand_T *args, int *numMatches, char_u ***matches)
1351{
1352 return expand_set_opt_string(
1353 args,
1354 p_cmp_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001355 ARRAY_LENGTH(p_cmp_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001356 numMatches,
1357 matches);
1358}
1359
1360#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1361 int
1362expand_set_clipboard(optexpand_T *args, int *numMatches, char_u ***matches)
1363{
1364 return expand_set_opt_string(
1365 args,
1366 p_cb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001367 ARRAY_LENGTH(p_cb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001368 numMatches,
1369 matches);
1370}
1371#endif
1372
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001373/*
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001374 * The global 'listchars' or 'fillchars' option is changed.
1375 */
1376 static char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01001377did_set_global_listfillchars(char_u *val, int opt_lcs, int opt_flags,
1378 char *errbuf, size_t errbuflen)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001379{
1380 char *errmsg = NULL;
1381 char_u **local_ptr = opt_lcs ? &curwin->w_p_lcs : &curwin->w_p_fcs;
1382
1383 // only apply the global value to "curwin" when it does not have a
1384 // local value
1385 if (opt_lcs)
1386 errmsg = set_listchars_option(curwin, val,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001387 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL),
1388 errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001389 else
1390 errmsg = set_fillchars_option(curwin, val,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001391 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL),
1392 errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001393 if (errmsg != NULL)
1394 return errmsg;
1395
1396 tabpage_T *tp;
1397 win_T *wp;
1398
1399 // If the current window is set to use the global
1400 // 'listchars'/'fillchars' value, clear the window-local value.
1401 if (!(opt_flags & OPT_GLOBAL))
1402 clear_string_option(local_ptr);
1403 FOR_ALL_TAB_WINDOWS(tp, wp)
1404 {
1405 // If the current window has a local value need to apply it
1406 // again, it was changed when setting the global value.
1407 // If no error was returned above, we don't expect an error
1408 // here, so ignore the return value.
1409 if (opt_lcs)
1410 {
1411 if (*wp->w_p_lcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001412 (void)set_listchars_option(wp, wp->w_p_lcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001413 }
1414 else
1415 {
1416 if (*wp->w_p_fcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001417 (void)set_fillchars_option(wp, wp->w_p_fcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001418 }
1419 }
1420
1421 redraw_all_later(UPD_NOT_VALID);
1422
1423 return NULL;
1424}
1425
1426/*
1427 * The 'fillchars' option or the 'listchars' option is changed.
1428 */
1429 char *
1430did_set_chars_option(optset_T *args)
1431{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001432 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001433 char *errmsg = NULL;
1434
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001435 if ( varp == &p_lcs // global 'listchars'
1436 || varp == &p_fcs) // global 'fillchars'
1437 errmsg = did_set_global_listfillchars(*varp, varp == &p_lcs,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001438 args->os_flags, args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001439 else if (varp == &curwin->w_p_lcs) // local 'listchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001440 errmsg = set_listchars_option(curwin, *varp, TRUE,
1441 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001442 else if (varp == &curwin->w_p_fcs) // local 'fillchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001443 errmsg = set_fillchars_option(curwin, *varp, TRUE,
1444 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001445
1446 return errmsg;
1447}
1448
1449/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001450 * Expand 'fillchars' or 'listchars' option value.
1451 */
1452 int
1453expand_set_chars_option(optexpand_T *args, int *numMatches, char_u ***matches)
1454{
1455 char_u **varp = (char_u **)args->oe_varp;
1456 int is_lcs = (varp == &p_lcs || varp == &curwin->w_p_lcs);
1457 return expand_set_opt_generic(
1458 args,
1459 is_lcs ? get_listchars_name : get_fillchars_name,
1460 numMatches,
1461 matches);
1462}
1463
1464/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001465 * The 'cinoptions' option is changed.
1466 */
1467 char *
1468did_set_cinoptions(optset_T *args UNUSED)
1469{
1470 // TODO: recognize errors
1471 parse_cino(curbuf);
1472
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001473 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001474}
1475
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00001476#if defined(FEAT_SYN_HL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001477/*
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001478 * The 'colorcolumn' option is changed.
1479 */
1480 char *
1481did_set_colorcolumn(optset_T *args UNUSED)
1482{
1483 return check_colorcolumn(curwin);
1484}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001485#endif
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001486
1487/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001488 * The 'comments' option is changed.
1489 */
1490 char *
1491did_set_comments(optset_T *args)
1492{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001493 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001494 char_u *s;
1495 char *errmsg = NULL;
1496
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001497 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001498 {
1499 while (*s && *s != ':')
1500 {
1501 if (vim_strchr((char_u *)COM_ALL, *s) == NULL
1502 && !VIM_ISDIGIT(*s) && *s != '-')
1503 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01001504 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001505 break;
1506 }
1507 ++s;
1508 }
1509 if (*s++ == NUL)
1510 errmsg = e_missing_colon;
1511 else if (*s == ',' || *s == NUL)
1512 errmsg = e_zero_length_string;
1513 if (errmsg != NULL)
1514 break;
1515 while (*s && *s != ',')
1516 {
1517 if (*s == '\\' && s[1] != NUL)
1518 ++s;
1519 ++s;
1520 }
1521 s = skip_to_option_part(s);
1522 }
1523
1524 return errmsg;
1525}
1526
1527#if defined(FEAT_FOLDING) || defined(PROTO)
1528/*
1529 * The 'commentstring' option is changed.
1530 */
1531 char *
1532did_set_commentstring(optset_T *args)
1533{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001534 char_u **varp = (char_u **)args->os_varp;
1535
1536 if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001537 return e_commentstring_must_be_empty_or_contain_str;
1538
1539 return NULL;
1540}
1541#endif
1542
1543/*
1544 * The 'complete' option is changed.
1545 */
1546 char *
1547did_set_complete(optset_T *args)
1548{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001549 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001550 char_u *s;
1551
1552 // check if it is a valid value for 'complete' -- Acevedo
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001553 for (s = *varp; *s;)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001554 {
1555 while (*s == ',' || *s == ' ')
1556 s++;
1557 if (!*s)
1558 break;
1559 if (vim_strchr((char_u *)".wbuksid]tU", *s) == NULL)
Christian Brabandtb39b2402023-11-29 11:34:05 +01001560 return illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001561 if (*++s != NUL && *s != ',' && *s != ' ')
1562 {
1563 if (s[-1] == 'k' || s[-1] == 's')
1564 {
1565 // skip optional filename after 'k' and 's'
1566 while (*s && *s != ',' && *s != ' ')
1567 {
1568 if (*s == '\\' && s[1] != NUL)
1569 ++s;
1570 ++s;
1571 }
1572 }
1573 else
1574 {
1575 if (args->os_errbuf != NULL)
1576 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01001577 vim_snprintf((char *)args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001578 _(e_illegal_character_after_chr), *--s);
1579 return args->os_errbuf;
1580 }
1581 return "";
1582 }
1583 }
1584 }
1585
1586 return NULL;
1587}
1588
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001589 int
1590expand_set_complete(optexpand_T *args, int *numMatches, char_u ***matches)
1591{
1592 static char *(p_cpt_values[]) = {
1593 ".", "w", "b", "u", "k", "kspell", "s", "i", "d", "]", "t", "U",
1594 NULL};
1595 return expand_set_opt_string(
1596 args,
1597 p_cpt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001598 ARRAY_LENGTH(p_cpt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001599 numMatches,
1600 matches);
1601}
1602
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001603/*
1604 * The 'completeopt' option is changed.
1605 */
1606 char *
1607did_set_completeopt(optset_T *args UNUSED)
1608{
zeertzjq529b9ad2024-06-05 20:27:06 +02001609 char_u *cot = p_cot;
1610 unsigned *flags = &cot_flags;
1611
1612 if (args->os_flags & OPT_LOCAL)
1613 {
1614 cot = curbuf->b_p_cot;
1615 flags = &curbuf->b_cot_flags;
1616 }
1617
1618 if (check_opt_strings(cot, p_cot_values, TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001619 return e_invalid_argument;
1620
zeertzjq529b9ad2024-06-05 20:27:06 +02001621 if (opt_strings_flags(cot, p_cot_values, flags, TRUE) != OK)
1622 return e_invalid_argument;
1623
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001624 return NULL;
1625}
1626
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001627 int
1628expand_set_completeopt(optexpand_T *args, int *numMatches, char_u ***matches)
1629{
1630 return expand_set_opt_string(
1631 args,
1632 p_cot_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001633 ARRAY_LENGTH(p_cot_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001634 numMatches,
1635 matches);
1636}
1637
glepnir6a89c942024-10-01 20:32:12 +02001638/*
1639 * The 'completeitemalign' option is changed.
1640 */
1641 char *
1642did_set_completeitemalign(optset_T *args UNUSED)
1643{
1644 char_u *p = p_cia;
1645 unsigned new_cia_flags = 0;
1646 int seen[3] = { FALSE, FALSE, FALSE };
1647 int count = 0;
1648 char_u buf[10];
1649
1650 while (*p)
1651 {
1652 copy_option_part(&p, buf, sizeof(buf), ",");
1653 if (count >= 3)
1654 return e_invalid_argument;
1655
1656 if (STRCMP(buf, "abbr") == 0)
1657 {
1658 if (seen[CPT_ABBR])
1659 return e_invalid_argument;
1660 new_cia_flags = new_cia_flags * 10 + CPT_ABBR;
1661 seen[CPT_ABBR] = TRUE;
1662 count++;
1663 }
1664 else if (STRCMP(buf, "kind") == 0)
1665 {
1666 if (seen[CPT_KIND])
1667 return e_invalid_argument;
1668 new_cia_flags = new_cia_flags * 10 + CPT_KIND;
1669 seen[CPT_KIND] = TRUE;
1670 count++;
1671 }
1672 else if (STRCMP(buf, "menu") == 0)
1673 {
1674 if (seen[CPT_MENU])
1675 return e_invalid_argument;
1676 new_cia_flags = new_cia_flags * 10 + CPT_MENU;
1677 seen[CPT_MENU] = TRUE;
1678 count++;
1679 }
1680 else
1681 return e_invalid_argument;
1682 }
1683 if (new_cia_flags == 0 || count != 3)
1684 return e_invalid_argument;
1685
1686 cia_flags = new_cia_flags;
1687 return NULL;
1688}
1689
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001690#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1691/*
1692 * The 'completepopup' option is changed.
1693 */
1694 char *
1695did_set_completepopup(optset_T *args UNUSED)
1696{
1697 if (parse_completepopup(NULL) == FAIL)
1698 return e_invalid_argument;
1699
1700 popup_close_info();
1701 return NULL;
1702}
1703#endif
1704
1705#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1706/*
1707 * The 'completeslash' option is changed.
1708 */
1709 char *
1710did_set_completeslash(optset_T *args UNUSED)
1711{
1712 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1713 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
1714 return e_invalid_argument;
1715
1716 return NULL;
1717}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001718
1719 int
1720expand_set_completeslash(optexpand_T *args, int *numMatches, char_u ***matches)
1721{
1722 return expand_set_opt_string(
1723 args,
1724 p_csl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001725 ARRAY_LENGTH(p_csl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001726 numMatches,
1727 matches);
1728}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001729#endif
1730
1731#if defined(FEAT_CONCEAL) || defined(PROTO)
1732/*
1733 * The 'concealcursor' option is changed.
1734 */
1735 char *
1736did_set_concealcursor(optset_T *args)
1737{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001738 char_u **varp = (char_u **)args->os_varp;
1739
Christian Brabandtb39b2402023-11-29 11:34:05 +01001740 return did_set_option_listflag(*varp, (char_u *)COCU_ALL, args->os_errbuf,
1741 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001742}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001743
1744 int
1745expand_set_concealcursor(optexpand_T *args, int *numMatches, char_u ***matches)
1746{
1747 return expand_set_opt_listflag(args, (char_u*)COCU_ALL, numMatches, matches);
1748}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001749#endif
1750
1751/*
1752 * The 'cpoptions' option is changed.
1753 */
1754 char *
1755did_set_cpoptions(optset_T *args)
1756{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001757 char_u **varp = (char_u **)args->os_varp;
1758
Christian Brabandtb39b2402023-11-29 11:34:05 +01001759 return did_set_option_listflag(*varp, (char_u *)CPO_ALL, args->os_errbuf,
1760 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001761}
1762
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001763 int
1764expand_set_cpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
1765{
1766 return expand_set_opt_listflag(args, (char_u*)CPO_ALL, numMatches, matches);
1767}
1768
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001769#if defined(FEAT_CRYPT) || defined(PROTO)
1770/*
1771 * The 'cryptkey' option is changed.
1772 */
1773 char *
1774did_set_cryptkey(optset_T *args)
1775{
1776 // Make sure the ":set" command doesn't show the new value in the
1777 // history.
1778 remove_key_from_history();
1779
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02001780 if (args->os_op != OP_NONE)
1781 // Don't allow set+=/-=/^= as they can allow for substring guessing
1782 return e_invalid_argument;
1783
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001784 if (STRCMP(curbuf->b_p_key, args->os_oldval.string) != 0)
1785 {
1786 // Need to update the swapfile.
1787 ml_set_crypt_key(curbuf, args->os_oldval.string,
1788 *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
1789 changed_internal();
1790 }
Christian Brabandt19e6c4f2023-06-27 18:57:10 +01001791# ifdef FEAT_SODIUM
1792 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
1793 crypt_sodium_lock_key(args->os_newval.string);
1794# endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001795
1796 return NULL;
1797}
1798
1799/*
1800 * The 'cryptmethod' option is changed.
1801 */
1802 char *
1803did_set_cryptmethod(optset_T *args)
1804{
1805 char_u *p;
1806 char_u *s;
1807
1808 if (args->os_flags & OPT_LOCAL)
1809 p = curbuf->b_p_cm;
1810 else
1811 p = p_cm;
1812 if (check_opt_strings(p, p_cm_values, TRUE) != OK)
1813 return e_invalid_argument;
1814 else if (crypt_self_test() == FAIL)
1815 return e_invalid_argument;
1816
1817 // When setting the global value to empty, make it "zip".
1818 if (*p_cm == NUL)
1819 {
1820 free_string_option(p_cm);
1821 p_cm = vim_strsave((char_u *)"zip");
1822 }
1823 // When using ":set cm=name" the local value is going to be empty.
1824 // Do that here, otherwise the crypt functions will still use the
1825 // local value.
1826 if ((args->os_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1827 {
1828 free_string_option(curbuf->b_p_cm);
1829 curbuf->b_p_cm = empty_option;
1830 }
1831
1832 // Need to update the swapfile when the effective method changed.
1833 // Set "s" to the effective old value, "p" to the effective new
1834 // method and compare.
1835 if ((args->os_flags & OPT_LOCAL) && *args->os_oldval.string == NUL)
1836 s = p_cm; // was previously using the global value
1837 else
1838 s = args->os_oldval.string;
1839 if (*curbuf->b_p_cm == NUL)
1840 p = p_cm; // is now using the global value
1841 else
1842 p = curbuf->b_p_cm;
1843 if (STRCMP(s, p) != 0)
1844 ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1845
1846 // If the global value changes need to update the swapfile for all
1847 // buffers using that value.
1848 if ((args->os_flags & OPT_GLOBAL)
1849 && STRCMP(p_cm, args->os_oldval.string) != 0)
1850 {
1851 buf_T *buf;
1852
1853 FOR_ALL_BUFFERS(buf)
1854 if (buf != curbuf && *buf->b_p_cm == NUL)
1855 ml_set_crypt_key(buf, buf->b_p_key, args->os_oldval.string);
1856 }
1857 return NULL;
1858}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001859
1860 int
1861expand_set_cryptmethod(optexpand_T *args, int *numMatches, char_u ***matches)
1862{
1863 return expand_set_opt_string(
1864 args,
1865 p_cm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001866 ARRAY_LENGTH(p_cm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001867 numMatches,
1868 matches);
1869}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001870#endif
1871
1872#if (defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1873/*
1874 * The 'cscopequickfix' option is changed.
1875 */
1876 char *
1877did_set_cscopequickfix(optset_T *args UNUSED)
1878{
1879 char_u *p;
1880
1881 if (p_csqf == NULL)
1882 return NULL;
1883
1884 p = p_csqf;
1885 while (*p != NUL)
1886 {
1887 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
1888 || p[1] == NUL
1889 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
1890 || (p[2] != NUL && p[2] != ','))
1891 return e_invalid_argument;
1892 else if (p[2] == NUL)
1893 break;
1894 else
1895 p += 3;
1896 }
1897
1898 return NULL;
1899}
1900#endif
1901
1902#if defined(FEAT_SYN_HL) || defined(PROTO)
1903/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001904 * The 'cursorlineopt' option is changed.
1905 */
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001906 char *
1907did_set_cursorlineopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001908{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001909 char_u **varp = (char_u **)args->os_varp;
1910
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001911 // This could be changed to use opt_strings_flags() instead.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001912 if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001913 return e_invalid_argument;
1914
1915 return NULL;
1916}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001917
1918 int
1919expand_set_cursorlineopt(optexpand_T *args, int *numMatches, char_u ***matches)
1920{
1921 return expand_set_opt_string(
1922 args,
1923 p_culopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001924 ARRAY_LENGTH(p_culopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001925 numMatches,
1926 matches);
1927}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001928#endif
1929
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001930/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001931 * The 'debug' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001932 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001933 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001934did_set_debug(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001935{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001936 return did_set_opt_strings(p_debug, p_debug_values, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001937}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001938
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001939 int
1940expand_set_debug(optexpand_T *args, int *numMatches, char_u ***matches)
1941{
1942 return expand_set_opt_string(
1943 args,
1944 p_debug_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001945 ARRAY_LENGTH(p_debug_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001946 numMatches,
1947 matches);
1948}
1949
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001950#if defined(FEAT_DIFF) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001951/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001952 * The 'diffopt' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001953 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001954 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001955did_set_diffopt(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001956{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001957 if (diffopt_changed() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001958 return e_invalid_argument;
1959
1960 return NULL;
1961}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001962
1963 int
1964expand_set_diffopt(optexpand_T *args, int *numMatches, char_u ***matches)
1965{
1966 expand_T *xp = args->oe_xp;
1967
1968 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
1969 {
1970 // Within "algorithm:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001971 int algo_len = (int)STRLEN("algorithm:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001972 if (xp->xp_pattern - args->oe_set_arg >= algo_len &&
1973 STRNCMP(xp->xp_pattern - algo_len, "algorithm:", algo_len) == 0)
1974 {
1975 return expand_set_opt_string(
1976 args,
1977 p_dip_algorithm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001978 ARRAY_LENGTH(p_dip_algorithm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001979 numMatches,
1980 matches);
1981 }
1982 return FAIL;
1983 }
1984
1985 return expand_set_opt_string(
1986 args,
1987 p_dip_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001988 ARRAY_LENGTH(p_dip_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001989 numMatches,
1990 matches);
1991}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001992#endif
1993
1994/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001995 * The 'display' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001996 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001997 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001998did_set_display(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001999{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002000 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002001 return e_invalid_argument;
2002
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002003 (void)init_chartab();
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002004 return NULL;
2005}
2006
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002007 int
2008expand_set_display(optexpand_T *args, int *numMatches, char_u ***matches)
2009{
2010 return expand_set_opt_string(
2011 args,
2012 p_dy_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002013 ARRAY_LENGTH(p_dy_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002014 numMatches,
2015 matches);
2016}
2017
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002018/*
2019 * The 'eadirection' option is changed.
2020 */
2021 char *
2022did_set_eadirection(optset_T *args UNUSED)
2023{
2024 return did_set_opt_strings(p_ead, p_ead_values, FALSE);
2025}
2026
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002027 int
2028expand_set_eadirection(optexpand_T *args, int *numMatches, char_u ***matches)
2029{
2030 return expand_set_opt_string(
2031 args,
2032 p_ead_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002033 ARRAY_LENGTH(p_ead_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002034 numMatches,
2035 matches);
2036}
2037
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002038/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002039 * One of the 'encoding', 'fileencoding', 'termencoding' or 'makeencoding'
2040 * options is changed.
2041 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002042 char *
2043did_set_encoding(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002044{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002045 char_u **varp = (char_u **)args->os_varp;
2046 char_u **gvarp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002047 char *errmsg = NULL;
2048 char_u *p;
2049
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002050 // Get the global option to compare with, otherwise we would have to check
2051 // two values for all local options.
2052 gvarp = (char_u **)get_option_varp_scope(args->os_idx, OPT_GLOBAL);
2053
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002054 if (gvarp == &p_fenc)
2055 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002056 if (!curbuf->b_p_ma && args->os_flags != OPT_GLOBAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002057 errmsg = e_cannot_make_changes_modifiable_is_off;
2058 else if (vim_strchr(*varp, ',') != NULL)
2059 // No comma allowed in 'fileencoding'; catches confusing it
2060 // with 'fileencodings'.
2061 errmsg = e_invalid_argument;
2062 else
2063 {
2064 // May show a "+" in the title now.
2065 redraw_titles();
2066 // Add 'fileencoding' to the swap file.
2067 ml_setflags(curbuf);
2068 }
2069 }
2070 if (errmsg == NULL)
2071 {
2072 // canonize the value, so that STRCMP() can be used on it
2073 p = enc_canonize(*varp);
2074 if (p != NULL)
2075 {
2076 vim_free(*varp);
2077 *varp = p;
2078 }
2079 if (varp == &p_enc)
2080 {
2081 errmsg = mb_init();
2082 redraw_titles();
2083 }
2084 }
2085
2086#if defined(FEAT_GUI_GTK)
2087 if (errmsg == NULL && varp == &p_tenc && gui.in_use)
2088 {
2089 // GTK uses only a single encoding, and that is UTF-8.
2090 if (STRCMP(p_tenc, "utf-8") != 0)
2091 errmsg = e_cannot_be_changed_in_gtk_GUI;
2092 }
2093#endif
2094
2095 if (errmsg == NULL)
2096 {
2097#ifdef FEAT_KEYMAP
2098 // When 'keymap' is used and 'encoding' changes, reload the keymap
2099 // (with another encoding).
2100 if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
2101 (void)keymap_init();
2102#endif
2103
2104 // When 'termencoding' is not empty and 'encoding' changes or when
2105 // 'termencoding' changes, need to setup for keyboard input and
2106 // display output conversion.
2107 if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
2108 {
2109 if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
2110 || convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
2111 {
2112 semsg(_(e_cannot_convert_between_str_and_str),
2113 p_tenc, p_enc);
2114 errmsg = e_invalid_argument;
2115 }
2116 }
2117
2118#if defined(MSWIN)
K.Takatace3189d2023-02-15 19:13:43 +00002119 // $HOME, $VIM and $VIMRUNTIME may have characters in active code page.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002120 if (varp == &p_enc)
K.Takatace3189d2023-02-15 19:13:43 +00002121 {
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002122 init_homedir();
K.Takatace3189d2023-02-15 19:13:43 +00002123 init_vimdir();
2124 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002125#endif
2126 }
2127
2128 return errmsg;
2129}
2130
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002131 int
2132expand_set_encoding(optexpand_T *args, int *numMatches, char_u ***matches)
2133{
2134 return expand_set_opt_generic(
2135 args,
2136 get_encoding_name,
2137 numMatches,
2138 matches);
2139}
2140
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002141/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002142 * The 'eventignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002143 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002144 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002145did_set_eventignore(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002146{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002147 if (check_ei() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002148 return e_invalid_argument;
2149 return NULL;
2150}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002151
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002152 static char_u *
2153get_eventignore_name(expand_T *xp, int idx)
2154{
2155 // 'eventignore' allows special keyword "all" in addition to
2156 // all event names.
2157 if (idx == 0)
2158 return (char_u *)"all";
2159 return get_event_name_no_group(xp, idx - 1);
2160}
2161
2162 int
2163expand_set_eventignore(optexpand_T *args, int *numMatches, char_u ***matches)
2164{
2165 return expand_set_opt_generic(
2166 args,
2167 get_eventignore_name,
2168 numMatches,
2169 matches);
2170}
2171
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002172/*
2173 * The 'fileformat' option is changed.
2174 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002175 char *
2176did_set_fileformat(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002177{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002178 char_u **varp = (char_u **)args->os_varp;
2179
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002180 if (!curbuf->b_p_ma && !(args->os_flags & OPT_GLOBAL))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002181 return e_cannot_make_changes_modifiable_is_off;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002182 else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002183 return e_invalid_argument;
2184
2185 // may also change 'textmode'
2186 if (get_fileformat(curbuf) == EOL_DOS)
2187 curbuf->b_p_tx = TRUE;
2188 else
2189 curbuf->b_p_tx = FALSE;
2190 redraw_titles();
2191 // update flag in swap file
2192 ml_setflags(curbuf);
2193 // Redraw needed when switching to/from "mac": a CR in the text
2194 // will be displayed differently.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002195 if (get_fileformat(curbuf) == EOL_MAC || *args->os_oldval.string == 'm')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002196 redraw_curbuf_later(UPD_NOT_VALID);
2197
2198 return NULL;
2199}
2200
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002201 int
2202expand_set_fileformat(optexpand_T *args, int *numMatches, char_u ***matches)
2203{
2204 return expand_set_opt_string(
2205 args,
2206 p_ff_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002207 ARRAY_LENGTH(p_ff_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002208 numMatches,
2209 matches);
2210}
2211
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002212/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002213 * Function given to ExpandGeneric() to obtain the possible arguments of the
2214 * fileformat options.
2215 */
2216 char_u *
2217get_fileformat_name(expand_T *xp UNUSED, int idx)
2218{
2219 if (idx >= (int)ARRAY_LENGTH(p_ff_values))
2220 return NULL;
2221
2222 return (char_u*)p_ff_values[idx];
2223}
2224
2225/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002226 * The 'fileformats' option is changed.
2227 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002228 char *
2229did_set_fileformats(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002230{
2231 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
2232 return e_invalid_argument;
2233
2234 // also change 'textauto'
2235 if (*p_ffs == NUL)
2236 p_ta = FALSE;
2237 else
2238 p_ta = TRUE;
2239
2240 return NULL;
2241}
2242
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002243/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002244 * The 'filetype' or the 'syntax' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002245 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002246 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002247did_set_filetype_or_syntax(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002248{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002249 char_u **varp = (char_u **)args->os_varp;
2250
2251 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002252 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002253
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002254 args->os_value_changed = STRCMP(args->os_oldval.string, *varp) != 0;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002255
2256 // Since we check the value, there is no need to set P_INSECURE,
2257 // even when the value comes from a modeline.
2258 args->os_value_checked = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002259
2260 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002261}
2262
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002263#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002264/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002265 * The 'foldclose' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002266 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002267 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002268did_set_foldclose(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002269{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002270 return did_set_opt_strings(p_fcl, p_fcl_values, TRUE);
2271}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002272
2273 int
2274expand_set_foldclose(optexpand_T *args, int *numMatches, char_u ***matches)
2275{
2276 return expand_set_opt_string(
2277 args,
2278 p_fcl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002279 ARRAY_LENGTH(p_fcl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002280 numMatches,
2281 matches);
2282}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002283#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002284
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002285#if (defined(FEAT_EVAL) && defined(FEAT_FOLDING)) || defined(PROTO)
2286/*
2287 * The 'foldexpr' option is changed.
2288 */
2289 char *
2290did_set_foldexpr(optset_T *args)
2291{
2292 (void)did_set_optexpr(args);
2293 if (foldmethodIsExpr(curwin))
2294 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002295 return NULL;
2296}
2297#endif
2298
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002299#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002300/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002301 * The 'foldignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002302 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002303 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002304did_set_foldignore(optset_T *args UNUSED)
2305{
2306 if (foldmethodIsIndent(curwin))
2307 foldUpdateAll(curwin);
2308 return NULL;
2309}
2310
2311/*
2312 * The 'foldmarker' option is changed.
2313 */
2314 char *
2315did_set_foldmarker(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002316{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002317 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002318 char_u *p;
2319
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002320 p = vim_strchr(*varp, ',');
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002321 if (p == NULL)
2322 return e_comma_required;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002323 else if (p == *varp || p[1] == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002324 return e_invalid_argument;
2325 else if (foldmethodIsMarker(curwin))
2326 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002327
2328 return NULL;
2329}
2330
2331/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002332 * The 'foldmethod' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002333 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002334 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002335did_set_foldmethod(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002336{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002337 char_u **varp = (char_u **)args->os_varp;
2338
2339 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002340 || *curwin->w_p_fdm == NUL)
2341 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002342
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002343 foldUpdateAll(curwin);
2344 if (foldmethodIsDiff(curwin))
2345 newFoldLevel();
2346 return NULL;
2347}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002348
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002349 int
2350expand_set_foldmethod(optexpand_T *args, int *numMatches, char_u ***matches)
2351{
2352 return expand_set_opt_string(
2353 args,
2354 p_fdm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002355 ARRAY_LENGTH(p_fdm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002356 numMatches,
2357 matches);
2358}
2359
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002360/*
2361 * The 'foldopen' option is changed.
2362 */
2363 char *
2364did_set_foldopen(optset_T *args UNUSED)
2365{
2366 return did_set_opt_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
2367}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002368
2369 int
2370expand_set_foldopen(optexpand_T *args, int *numMatches, char_u ***matches)
2371{
2372 return expand_set_opt_string(
2373 args,
2374 p_fdo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002375 ARRAY_LENGTH(p_fdo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002376 numMatches,
2377 matches);
2378}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002379#endif
2380
2381/*
2382 * The 'formatoptions' option is changed.
2383 */
2384 char *
2385did_set_formatoptions(optset_T *args)
2386{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002387 char_u **varp = (char_u **)args->os_varp;
2388
Christian Brabandtb39b2402023-11-29 11:34:05 +01002389 return did_set_option_listflag(*varp, (char_u *)FO_ALL, args->os_errbuf,
2390 args->os_errbuflen);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002391}
2392
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002393 int
2394expand_set_formatoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2395{
2396 return expand_set_opt_listflag(args, (char_u*)FO_ALL, numMatches, matches);
2397}
2398
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002399#if defined(CURSOR_SHAPE) || defined(PROTO)
2400/*
2401 * The 'guicursor' option is changed.
2402 */
2403 char *
2404did_set_guicursor(optset_T *args UNUSED)
2405{
2406 return parse_shape_opt(SHAPE_CURSOR);
2407}
2408#endif
2409
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002410#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002411/*
2412 * The 'guifont' option is changed.
2413 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002414 char *
2415did_set_guifont(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002416{
2417 char_u *p;
2418 char *errmsg = NULL;
2419
2420 if (gui.in_use)
2421 {
2422 p = p_guifont;
2423# if defined(FEAT_GUI_GTK)
2424 // Put up a font dialog and let the user select a new value.
2425 // If this is cancelled go back to the old value but don't
2426 // give an error message.
2427 if (STRCMP(p, "*") == 0)
2428 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002429 p = gui_mch_font_dialog(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002430 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002431 p_guifont = (p != NULL) ? p : vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002432 }
2433# endif
2434 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
2435 {
2436# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
2437 if (STRCMP(p_guifont, "*") == 0)
2438 {
2439 // Dialog was cancelled: Keep the old value without giving
2440 // an error message.
2441 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002442 p_guifont = vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002443 }
2444 else
2445# endif
2446 errmsg = e_invalid_fonts;
2447 }
2448 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002449
2450 return errmsg;
2451}
2452
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002453/*
2454 * Expand the 'guifont' option. Only when GUI is being used. Each platform has
2455 * specific behaviors.
2456 */
2457 int
2458expand_set_guifont(optexpand_T *args, int *numMatches, char_u ***matches)
2459{
2460 if (!gui.in_use)
2461 return FAIL;
2462
2463# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
2464 char_u **varp = (char_u **)args->oe_varp;
2465 int wide = (varp == &p_guifontwide);
2466
2467 return expand_set_opt_callback(
2468 args, gui_mch_expand_font, &wide, numMatches, matches);
2469# else
2470 return FAIL;
2471# endif
2472}
2473
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002474# if defined(FEAT_XFONTSET) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002475/*
2476 * The 'guifontset' option is changed.
2477 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002478 char *
2479did_set_guifontset(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002480{
2481 char *errmsg = NULL;
2482
2483 if (STRCMP(p_guifontset, "*") == 0)
2484 errmsg = e_cant_select_fontset;
2485 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
2486 errmsg = e_invalid_fontset;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002487
2488 return errmsg;
2489}
2490# endif
2491
2492/*
2493 * The 'guifontwide' option is changed.
2494 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002495 char *
2496did_set_guifontwide(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002497{
2498 char *errmsg = NULL;
2499
2500 if (STRCMP(p_guifontwide, "*") == 0)
2501 errmsg = e_cant_select_wide_font;
2502 else if (gui_get_wide_font() == FAIL)
2503 errmsg = e_invalid_wide_font;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002504
2505 return errmsg;
2506}
2507#endif
2508
Erik S. V. Jansson8b1e7492024-02-24 14:26:52 +01002509#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002510/*
2511 * The 'guiligatures' option is changed.
2512 */
2513 char *
2514did_set_guiligatures(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002515{
2516 gui_set_ligatures();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002517 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002518}
2519#endif
2520
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002521#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002522/*
2523 * The 'guioptions' option is changed.
2524 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002525 char *
2526did_set_guioptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002527{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002528 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002529 char *errmsg;
2530
Christian Brabandtb39b2402023-11-29 11:34:05 +01002531 errmsg = did_set_option_listflag(*varp, (char_u *)GO_ALL, args->os_errbuf,
2532 args->os_errbuflen);
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002533 if (errmsg != NULL)
2534 return errmsg;
2535
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002536 gui_init_which_components(args->os_oldval.string);
2537 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002538}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002539
2540 int
2541expand_set_guioptions(optexpand_T *args, int *numMatches, char_u ***matches)
2542{
2543 return expand_set_opt_listflag(args, (char_u*)GO_ALL, numMatches, matches);
2544}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002545#endif
2546
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002547#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002548/*
2549 * The 'guitablabel' option is changed.
2550 */
2551 char *
2552did_set_guitablabel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002553{
2554 redraw_tabline = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002555 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002556}
2557#endif
2558
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002559/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002560 * The 'helpfile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002561 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002562 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002563did_set_helpfile(optset_T *args UNUSED)
2564{
2565 // May compute new values for $VIM and $VIMRUNTIME
2566 if (didset_vim)
2567 vim_unsetenv_ext((char_u *)"VIM");
2568 if (didset_vimruntime)
2569 vim_unsetenv_ext((char_u *)"VIMRUNTIME");
2570 return NULL;
2571}
2572
2573#if defined(FEAT_MULTI_LANG) || defined(PROTO)
2574/*
2575 * The 'helplang' option is changed.
2576 */
2577 char *
2578did_set_helplang(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002579{
2580 char *errmsg = NULL;
2581
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002582 // Check for "", "ab", "ab,cd", etc.
2583 for (char_u *s = p_hlg; *s != NUL; s += 3)
2584 {
2585 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
2586 {
2587 errmsg = e_invalid_argument;
2588 break;
2589 }
2590 if (s[2] == NUL)
2591 break;
2592 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002593
2594 return errmsg;
2595}
2596#endif
2597
2598/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002599 * The 'highlight' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002600 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002601 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002602did_set_highlight(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002603{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002604 if (highlight_changed() == FAIL)
2605 return e_invalid_argument; // invalid flags
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002606
2607 return NULL;
2608}
2609
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002610/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002611 * Expand 'highlight' option.
2612 */
2613 int
2614expand_set_highlight(optexpand_T *args, int *numMatches, char_u ***matches)
2615{
2616 char_u *p;
2617 expand_T *xp = args->oe_xp;
2618 static char_u hl_flags[HLF_COUNT] = HL_FLAGS;
Christian Brabandt3f168ec2023-10-02 23:21:11 +02002619 size_t i;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002620 int count = 0;
2621
2622 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2623 {
2624 // Right after a ':', meaning we just return all highlight names.
2625 return expand_set_opt_generic(
2626 args,
2627 get_highlight_name,
2628 numMatches,
2629 matches);
2630 }
2631
2632 if (*xp->xp_pattern == NUL)
2633 {
2634 // At beginning of a comma-separated list. Return the specific list of
2635 // supported occasions.
2636 *matches = ALLOC_MULT(char_u *, HLF_COUNT + 1);
2637 if (*matches == NULL)
2638 return FAIL;
2639
2640 // We still want to return the full option if it's requested.
2641 if (args->oe_include_orig_val)
2642 {
2643 p = vim_strsave(args->oe_opt_value);
2644 if (p == NULL)
2645 {
2646 VIM_CLEAR(*matches);
2647 return FAIL;
2648 }
2649 (*matches)[count++] = p;
2650 }
2651
2652 for (i = 0; i < HLF_COUNT; i++)
2653 {
2654 p = vim_strnsave(&hl_flags[i], 1);
2655 if (p == NULL)
2656 {
2657 if (count == 0)
2658 {
2659 VIM_CLEAR(*matches);
2660 return FAIL;
2661 }
2662 else
2663 break;
2664 }
2665 (*matches)[count++] = p;
2666 }
2667
2668 if (count == 0)
2669 {
2670 VIM_CLEAR(*matches);
2671 return FAIL;
2672 }
2673 *numMatches = count;
2674 return OK;
2675 }
2676
2677 // We are after the initial character (which indicates the occasion). We
2678 // already made sure we are not matching after a ':' above, so now we want
2679 // to match against display mode modifiers.
2680 // Since the xp_pattern starts from the beginning, we need to include it in
2681 // the returned match.
2682
2683 // Note: Keep this in sync with highlight_changed()
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002684 static char_u p_hl_mode_values[] =
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002685 {':', 'b', 'i', '-', 'n', 'r', 's', 'u', 'c', '2', 'd', '=', 't'};
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002686 size_t num_hl_modes = ARRAY_LENGTH(p_hl_mode_values);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002687
2688 *matches = ALLOC_MULT(char_u *, num_hl_modes);
2689 if (*matches == NULL)
2690 return FAIL;
2691
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002692 int pattern_len = xp->xp_pattern_len;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002693
2694 for (i = 0; i < num_hl_modes; i++)
2695 {
2696 // Don't allow duplicates as these are unique flags
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002697 char_u *dup = vim_strchr(xp->xp_pattern + 1, p_hl_mode_values[i]);
2698 if (dup != NULL && (int)(dup - xp->xp_pattern) < pattern_len)
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002699 continue;
2700
2701 // ':' only works by itself, not with other flags.
2702 if (pattern_len > 1 && p_hl_mode_values[i] == ':')
2703 continue;
2704
2705 p = vim_strnsave(xp->xp_pattern, pattern_len + 1);
2706 if (p == NULL)
2707 {
2708 if (i == 0)
2709 {
2710 VIM_CLEAR(*matches);
2711 return FAIL;
2712 }
2713 else
2714 break;
2715 }
2716 p[pattern_len] = p_hl_mode_values[i];
2717 p[pattern_len + 1] = NUL;
2718 (*matches)[count++] = p;
2719 }
2720 if (count == 0)
2721 {
2722 VIM_CLEAR(*matches);
2723 return FAIL;
2724 }
2725 *numMatches = count;
2726
2727 return OK;
2728}
2729
2730/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002731 * The 'titlestring' or the 'iconstring' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002732 */
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002733 static char *
2734parse_titleiconstring(optset_T *args UNUSED, int flagval UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002735{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002736#ifdef FEAT_STL_OPT
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002737 char_u **varp = (char_u **)args->os_varp;
2738
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002739 // NULL => statusline syntax
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002740 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002741 stl_syntax |= flagval;
2742 else
2743 stl_syntax &= ~flagval;
2744#endif
2745 did_set_title();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002746
2747 return NULL;
2748}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002749
2750/*
2751 * The 'iconstring' option is changed.
2752 */
2753 char *
2754did_set_iconstring(optset_T *args)
2755{
2756 int flagval = 0;
2757
2758#ifdef FEAT_STL_OPT
2759 flagval = STL_IN_ICON;
2760#endif
2761
2762 return parse_titleiconstring(args, flagval);
2763}
2764
2765#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
2766/*
2767 * The 'imactivatekey' option is changed.
2768 */
2769 char *
2770did_set_imactivatekey(optset_T *args UNUSED)
2771{
2772 if (!im_xim_isvalid_imactivate())
2773 return e_invalid_argument;
2774 return NULL;
2775}
2776#endif
2777
2778/*
2779 * The 'isident' or the 'iskeyword' or the 'isprint' or the 'isfname' option is
2780 * changed.
2781 */
2782 char *
2783did_set_isopt(optset_T *args)
2784{
2785 // 'isident', 'iskeyword', 'isprint or 'isfname' option: refill g_chartab[]
2786 // If the new option is invalid, use old value.
2787 // 'lisp' option: refill g_chartab[] for '-' char.
2788 if (init_chartab() == FAIL)
2789 {
2790 args->os_restore_chartab = TRUE;// need to restore the chartab.
2791 return e_invalid_argument; // error in value
2792 }
2793
2794 return NULL;
2795}
2796
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002797/*
2798 * The 'jumpoptions' option is changed.
2799 */
2800 char *
Yegappan Lakshmanan1926ae42023-09-21 16:36:28 +02002801did_set_jumpoptions(optset_T *args UNUSED)
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002802{
2803 if (opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE) != OK)
2804 return e_invalid_argument;
2805
2806 return NULL;
2807}
2808
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002809 int
2810expand_set_jumpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2811{
2812 return expand_set_opt_string(
2813 args,
2814 p_jop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002815 ARRAY_LENGTH(p_jop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002816 numMatches,
2817 matches);
2818}
2819
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002820#if defined(FEAT_KEYMAP) || defined(PROTO)
2821/*
2822 * The 'keymap' option is changed.
2823 */
2824 char *
2825did_set_keymap(optset_T *args)
2826{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002827 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002828 char *errmsg = NULL;
2829
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002830 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002831 errmsg = e_invalid_argument;
2832 else
2833 {
2834 int secure_save = secure;
2835
2836 // Reset the secure flag, since the value of 'keymap' has
2837 // been checked to be safe.
2838 secure = 0;
2839
2840 // load or unload key mapping tables
2841 errmsg = keymap_init();
2842
2843 secure = secure_save;
2844
2845 // Since we check the value, there is no need to set P_INSECURE,
2846 // even when the value comes from a modeline.
2847 args->os_value_checked = TRUE;
2848 }
2849
2850 if (errmsg == NULL)
2851 {
2852 if (*curbuf->b_p_keymap != NUL)
2853 {
2854 // Installed a new keymap, switch on using it.
2855 curbuf->b_p_iminsert = B_IMODE_LMAP;
2856 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
2857 curbuf->b_p_imsearch = B_IMODE_LMAP;
2858 }
2859 else
2860 {
2861 // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
2862 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
2863 curbuf->b_p_iminsert = B_IMODE_NONE;
2864 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
2865 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
2866 }
2867 if ((args->os_flags & OPT_LOCAL) == 0)
2868 {
2869 set_iminsert_global();
2870 set_imsearch_global();
2871 }
2872 status_redraw_curbuf();
2873 }
2874
2875 return errmsg;
2876}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002877#endif
2878
2879/*
2880 * The 'keymodel' option is changed.
2881 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002882 char *
2883did_set_keymodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002884{
2885 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
2886 return e_invalid_argument;
2887
2888 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
2889 km_startsel = (vim_strchr(p_km, 'a') != NULL);
2890 return NULL;
2891}
2892
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002893 int
2894expand_set_keymodel(optexpand_T *args, int *numMatches, char_u ***matches)
2895{
2896 return expand_set_opt_string(
2897 args,
2898 p_km_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002899 ARRAY_LENGTH(p_km_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002900 numMatches,
2901 matches);
2902}
2903
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002904/*
2905 * The 'keyprotocol' option is changed.
2906 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002907 char *
2908did_set_keyprotocol(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002909{
Gregory Anders3695d0e2023-09-29 20:17:20 +02002910 char_u *term = T_NAME;
2911 keyprot_T kpc = match_keyprotocol(term);
2912 if (kpc == KEYPROTOCOL_FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002913 return e_invalid_argument;
2914
Gregory Anders3695d0e2023-09-29 20:17:20 +02002915 apply_keyprotocol(term, kpc);
2916
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002917 return NULL;
2918}
2919
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002920 int
2921expand_set_keyprotocol(optexpand_T *args, int *numMatches, char_u ***matches)
2922{
2923 expand_T *xp = args->oe_xp;
2924 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2925 {
2926 // 'keyprotocol' only has well-defined terms for completion for the
2927 // protocol part after the colon.
2928 return expand_set_opt_string(
2929 args,
2930 p_kpc_protocol_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002931 ARRAY_LENGTH(p_kpc_protocol_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002932 numMatches,
2933 matches);
2934 }
2935 // Use expand_set_opt_string instead of returning FAIL so that we can
2936 // include the original value if args->oe_include_orig_val is set.
2937 static char *(empty[]) = {NULL};
2938 return expand_set_opt_string(args, empty, 0, numMatches, matches);
2939}
2940
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002941/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002942 * The 'lispoptions' option is changed.
2943 */
2944 char *
2945did_set_lispoptions(optset_T *args)
2946{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002947 char_u **varp = (char_u **)args->os_varp;
2948
2949 if (**varp != NUL
2950 && STRCMP(*varp, "expr:0") != 0
2951 && STRCMP(*varp, "expr:1") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002952 return e_invalid_argument;
2953
2954 return NULL;
2955}
2956
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002957 int
2958expand_set_lispoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2959{
2960 static char *(p_lop_values[]) = {"expr:0", "expr:1", NULL};
2961 return expand_set_opt_string(
2962 args,
2963 p_lop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002964 ARRAY_LENGTH(p_lop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002965 numMatches,
2966 matches);
2967}
2968
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002969/*
2970 * The 'matchpairs' option is changed.
2971 */
2972 char *
2973did_set_matchpairs(optset_T *args)
2974{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002975 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002976 char_u *p;
2977
2978 if (has_mbyte)
2979 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002980 for (p = *varp; *p != NUL; ++p)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002981 {
2982 int x2 = -1;
2983 int x3 = -1;
2984
2985 p += mb_ptr2len(p);
2986 if (*p != NUL)
2987 x2 = *p++;
2988 if (*p != NUL)
2989 {
2990 x3 = mb_ptr2char(p);
2991 p += mb_ptr2len(p);
2992 }
2993 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
2994 return e_invalid_argument;
2995 if (*p == NUL)
2996 break;
2997 }
2998 }
2999 else
3000 {
3001 // Check for "x:y,x:y"
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003002 for (p = *varp; *p != NUL; p += 4)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003003 {
3004 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
3005 return e_invalid_argument;
3006 if (p[3] == NUL)
3007 break;
3008 }
3009 }
3010
3011 return NULL;
3012}
3013
3014#if defined(FEAT_SPELL) || defined(PROTO)
3015/*
3016 * The 'mkspellmem' option is changed.
3017 */
3018 char *
3019did_set_mkspellmem(optset_T *args UNUSED)
3020{
3021 if (spell_check_msm() != OK)
3022 return e_invalid_argument;
3023
3024 return NULL;
3025}
3026#endif
3027
3028/*
3029 * The 'mouse' option is changed.
3030 */
3031 char *
3032did_set_mouse(optset_T *args)
3033{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003034 char_u **varp = (char_u **)args->os_varp;
3035
Christian Brabandtb39b2402023-11-29 11:34:05 +01003036 return did_set_option_listflag(*varp, (char_u *)MOUSE_ALL, args->os_errbuf,
3037 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003038}
3039
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003040 int
3041expand_set_mouse(optexpand_T *args, int *numMatches, char_u ***matches)
3042{
3043 return expand_set_opt_listflag(args, (char_u*)MOUSE_ALL, numMatches, matches);
3044}
3045
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003046/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003047 * The 'mousemodel' option is changed.
3048 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003049 char *
3050did_set_mousemodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003051{
3052 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
3053 return e_invalid_argument;
3054#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
Ken Takata18d0d292023-12-19 20:12:29 +01003055 else if (*p_mousem != *args->os_oldval.string)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003056 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
3057 // to create or delete the popup menus.
3058 gui_motif_update_mousemodel(root_menu);
3059#endif
3060
3061 return NULL;
3062}
3063
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003064 int
3065expand_set_mousemodel(optexpand_T *args, int *numMatches, char_u ***matches)
3066{
3067 return expand_set_opt_string(
3068 args,
3069 p_mousem_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003070 ARRAY_LENGTH(p_mousem_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003071 numMatches,
3072 matches);
3073}
3074
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003075#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3076 char *
3077did_set_mouseshape(optset_T *args UNUSED)
3078{
3079 char *errmsg = NULL;
3080
3081 errmsg = parse_shape_opt(SHAPE_MOUSE);
3082 update_mouseshape(-1);
3083
3084 return errmsg;
3085}
3086#endif
3087
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003088/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003089 * The 'nrformats' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003090 */
3091 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003092did_set_nrformats(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003093{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003094 char_u **varp = (char_u **)args->os_varp;
3095
3096 return did_set_opt_strings(*varp, p_nf_values, TRUE);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003097}
3098
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003099 int
3100expand_set_nrformats(optexpand_T *args, int *numMatches, char_u ***matches)
3101{
3102 return expand_set_opt_string(
3103 args,
3104 p_nf_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003105 ARRAY_LENGTH(p_nf_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003106 numMatches,
3107 matches);
3108}
3109
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003110#if defined(FEAT_EVAL) || defined(PROTO)
3111/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003112 * One of the '*expr' options is changed: 'balloonexpr', 'diffexpr',
3113 * 'foldexpr', 'foldtext', 'formatexpr', 'includeexpr', 'indentexpr',
3114 * 'patchexpr', 'printexpr' and 'charconvert'.
3115 *
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003116 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003117 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003118did_set_optexpr(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003119{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003120 char_u **varp = (char_u **)args->os_varp;
3121
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003122 // If the option value starts with <SID> or s:, then replace that with
3123 // the script identifier.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003124 char_u *name = get_scriptlocal_funcname(*varp);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003125 if (name != NULL)
3126 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003127 free_string_option(*varp);
3128 *varp = name;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003129 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003130
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003131 return NULL;
3132}
3133#endif
3134
3135/*
3136 * The 'pastetoggle' option is changed.
3137 */
3138 char *
3139did_set_pastetoggle(optset_T *args UNUSED)
3140{
3141 char_u *p;
3142
3143 // translate key codes like in a mapping
3144 if (*p_pt)
3145 {
zeertzjq7e0bae02023-08-11 23:15:38 +02003146 (void)replace_termcodes(p_pt, &p, 0,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003147 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
3148 if (p != NULL)
3149 {
3150 free_string_option(p_pt);
3151 p_pt = p;
3152 }
3153 }
3154
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003155 return NULL;
3156}
3157
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003158#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3159/*
3160 * The 'previewpopup' option is changed.
3161 */
3162 char *
3163did_set_previewpopup(optset_T *args UNUSED)
3164{
3165 if (parse_previewpopup(NULL) == FAIL)
3166 return e_invalid_argument;
3167
3168 return NULL;
3169}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003170
3171 int
3172expand_set_popupoption(optexpand_T *args, int *numMatches, char_u ***matches)
3173{
3174 expand_T *xp = args->oe_xp;
3175
3176 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3177 {
3178 // Within "highlight:"/"border:"/"align:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003179 int border_len = (int)STRLEN("border:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003180 if (xp->xp_pattern - args->oe_set_arg >= border_len &&
3181 STRNCMP(xp->xp_pattern - border_len, "border:", border_len) == 0)
3182 {
3183 return expand_set_opt_string(
3184 args,
3185 p_popup_option_border_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003186 ARRAY_LENGTH(p_popup_option_border_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003187 numMatches,
3188 matches);
3189 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003190 int align_len = (int)STRLEN("align:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003191 if (xp->xp_pattern - args->oe_set_arg >= align_len &&
3192 STRNCMP(xp->xp_pattern - align_len, "align:", align_len) == 0)
3193 {
3194 return expand_set_opt_string(
3195 args,
3196 p_popup_option_align_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003197 ARRAY_LENGTH(p_popup_option_align_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003198 numMatches,
3199 matches);
3200 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003201 int highlight_len = (int)STRLEN("highlight:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003202 if (xp->xp_pattern - args->oe_set_arg >= highlight_len &&
3203 STRNCMP(xp->xp_pattern - highlight_len, "highlight:", highlight_len) == 0)
3204 {
3205 // Return the list of all highlight names
3206 return expand_set_opt_generic(
3207 args,
3208 get_highlight_name,
3209 numMatches,
3210 matches);
3211 }
3212 return FAIL;
3213 }
3214
3215 return expand_set_opt_string(
3216 args,
3217 p_popup_option_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003218 ARRAY_LENGTH(p_popup_option_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003219 numMatches,
3220 matches);
3221}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003222#endif
3223
3224#if defined(FEAT_POSTSCRIPT) || defined(PROTO)
3225/*
3226 * The 'printencoding' option is changed.
3227 */
3228 char *
3229did_set_printencoding(optset_T *args UNUSED)
3230{
3231 char_u *s, *p;
3232
3233 // Canonize 'printencoding' if VIM standard one
3234 p = enc_canonize(p_penc);
3235 if (p != NULL)
3236 {
3237 vim_free(p_penc);
3238 p_penc = p;
3239 }
3240 else
3241 {
3242 // Ensure lower case and '-' for '_'
3243 for (s = p_penc; *s != NUL; s++)
3244 {
3245 if (*s == '_')
3246 *s = '-';
3247 else
3248 *s = TOLOWER_ASC(*s);
3249 }
3250 }
3251
3252 return NULL;
3253}
3254#endif
3255
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003256#if defined(FEAT_PRINTER) || defined(PROTO)
3257
3258 static char_u *
3259get_printoptions_names(expand_T *xp UNUSED, int idx)
3260{
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003261 if (idx >= (int)ARRAY_LENGTH(printer_opts))
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003262 return NULL;
3263 return (char_u*)printer_opts[idx].name;
3264}
3265
3266/*
3267 * Expand 'printoptions' option
3268 */
3269 int
3270expand_set_printoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3271{
3272 return expand_set_opt_generic(
3273 args,
3274 get_printoptions_names,
3275 numMatches,
3276 matches);
3277}
3278#endif
3279
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003280#if defined(FEAT_STL_OPT) || defined(PROTO)
3281/*
3282 * The 'statusline' or the 'tabline' or the 'rulerformat' option is changed.
3283 * "rulerformat" is TRUE if the 'rulerformat' option is changed.
3284 */
3285 static char *
3286parse_statustabline_rulerformat(optset_T *args, int rulerformat)
3287{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003288 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003289 char_u *s;
3290 char *errmsg = NULL;
3291 int wid;
3292
3293 if (rulerformat) // reset ru_wid first
3294 ru_wid = 0;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003295 s = *varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003296 if (rulerformat && *s == '%')
3297 {
3298 // set ru_wid if 'ruf' starts with "%99("
3299 if (*++s == '-') // ignore a '-'
3300 s++;
3301 wid = getdigits(&s);
3302 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
3303 ru_wid = wid;
3304 else
3305 errmsg = check_stl_option(p_ruf);
3306 }
3307 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
3308 else if (rulerformat || s[0] != '%' || s[1] != '!')
3309 errmsg = check_stl_option(s);
3310 if (rulerformat && errmsg == NULL)
3311 comp_col();
3312
3313 return errmsg;
3314}
3315#endif
3316
3317#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
3318/*
3319 * The 'renderoptions' option is changed.
3320 */
3321 char *
3322did_set_renderoptions(optset_T *args UNUSED)
3323{
3324 if (!gui_mch_set_rendering_options(p_rop))
3325 return e_invalid_argument;
3326
3327 return NULL;
3328}
3329#endif
3330
3331#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3332/*
3333 * The 'rightleftcmd' option is changed.
3334 */
3335 char *
3336did_set_rightleftcmd(optset_T *args)
3337{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003338 char_u **varp = (char_u **)args->os_varp;
3339
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003340 // Currently only "search" is a supported value.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003341 if (**varp != NUL && STRCMP(*varp, "search") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003342 return e_invalid_argument;
3343
3344 return NULL;
3345}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003346
3347 int
3348expand_set_rightleftcmd(optexpand_T *args, int *numMatches, char_u ***matches)
3349{
3350 static char *(p_rlc_values[]) = {"search", NULL};
3351 return expand_set_opt_string(
3352 args,
3353 p_rlc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003354 ARRAY_LENGTH(p_rlc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003355 numMatches,
3356 matches);
3357}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003358#endif
3359
3360#if defined(FEAT_STL_OPT) || defined(PROTO)
3361/*
3362 * The 'rulerformat' option is changed.
3363 */
3364 char *
3365did_set_rulerformat(optset_T *args)
3366{
3367 return parse_statustabline_rulerformat(args, TRUE);
3368}
3369#endif
3370
3371/*
3372 * The 'scrollopt' option is changed.
3373 */
3374 char *
3375did_set_scrollopt(optset_T *args UNUSED)
3376{
3377 return did_set_opt_strings(p_sbo, p_scbopt_values, TRUE);
3378}
3379
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003380 int
3381expand_set_scrollopt(optexpand_T *args, int *numMatches, char_u ***matches)
3382{
3383 return expand_set_opt_string(
3384 args,
3385 p_scbopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003386 ARRAY_LENGTH(p_scbopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003387 numMatches,
3388 matches);
3389}
3390
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003391/*
3392 * The 'selection' option is changed.
3393 */
3394 char *
3395did_set_selection(optset_T *args UNUSED)
3396{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003397 if (*p_sel == NUL || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003398 return e_invalid_argument;
3399
3400 return NULL;
3401}
3402
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003403 int
3404expand_set_selection(optexpand_T *args, int *numMatches, char_u ***matches)
3405{
3406 return expand_set_opt_string(
3407 args,
3408 p_sel_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003409 ARRAY_LENGTH(p_sel_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003410 numMatches,
3411 matches);
3412}
3413
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003414/*
3415 * The 'selectmode' option is changed.
3416 */
3417 char *
3418did_set_selectmode(optset_T *args UNUSED)
3419{
3420 return did_set_opt_strings(p_slm, p_slm_values, TRUE);
3421}
3422
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003423 int
3424expand_set_selectmode(optexpand_T *args, int *numMatches, char_u ***matches)
3425{
3426 return expand_set_opt_string(
3427 args,
3428 p_slm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003429 ARRAY_LENGTH(p_slm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003430 numMatches,
3431 matches);
3432}
3433
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003434#if defined(FEAT_SESSION) || defined(PROTO)
3435/*
3436 * The 'sessionoptions' option is changed.
3437 */
3438 char *
3439did_set_sessionoptions(optset_T *args)
3440{
3441 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
3442 return e_invalid_argument;
3443 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
3444 {
3445 // Don't allow both "sesdir" and "curdir".
3446 (void)opt_strings_flags(args->os_oldval.string, p_ssop_values,
3447 &ssop_flags, TRUE);
3448 return e_invalid_argument;
3449 }
3450
3451 return NULL;
3452}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003453
3454 int
3455expand_set_sessionoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3456{
3457 return expand_set_opt_string(
3458 args,
3459 p_ssop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003460 ARRAY_LENGTH(p_ssop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003461 numMatches,
3462 matches);
3463}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003464#endif
3465
3466/*
3467 * The 'shortmess' option is changed.
3468 */
3469 char *
3470did_set_shortmess(optset_T *args)
3471{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003472 char_u **varp = (char_u **)args->os_varp;
3473
Christian Brabandtb39b2402023-11-29 11:34:05 +01003474 return did_set_option_listflag(*varp, (char_u *)SHM_ALL, args->os_errbuf,
3475 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003476}
3477
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003478 int
3479expand_set_shortmess(optexpand_T *args, int *numMatches, char_u ***matches)
3480{
3481 return expand_set_opt_listflag(args, (char_u*)SHM_ALL, numMatches, matches);
3482}
3483
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003484#if defined(FEAT_LINEBREAK) || defined(PROTO)
3485/*
3486 * The 'showbreak' option is changed.
3487 */
3488 char *
3489did_set_showbreak(optset_T *args)
3490{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003491 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003492 char_u *s;
3493
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003494 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003495 {
3496 if (ptr2cells(s) != 1)
3497 return e_showbreak_contains_unprintable_or_wide_character;
3498 MB_PTR_ADV(s);
3499 }
3500
3501 return NULL;
3502}
3503#endif
3504
3505/*
3506 * The 'showcmdloc' option is changed.
3507 */
3508 char *
3509did_set_showcmdloc(optset_T *args UNUSED)
3510{
zeertzjqc27fcf42024-03-01 23:01:43 +01003511 char *errmsg = did_set_opt_strings(p_sloc, p_sloc_values, FALSE);
3512
3513 if (errmsg == NULL)
3514 comp_col();
3515
3516 return errmsg;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003517}
3518
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003519 int
3520expand_set_showcmdloc(optexpand_T *args, int *numMatches, char_u ***matches)
3521{
3522 return expand_set_opt_string(
3523 args,
3524 p_sloc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003525 ARRAY_LENGTH(p_sloc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003526 numMatches,
3527 matches);
3528}
3529
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003530#if defined(FEAT_SIGNS) || defined(PROTO)
3531/*
3532 * The 'signcolumn' option is changed.
3533 */
3534 char *
3535did_set_signcolumn(optset_T *args)
3536{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003537 char_u **varp = (char_u **)args->os_varp;
3538
3539 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003540 return e_invalid_argument;
3541 // When changing the 'signcolumn' to or from 'number', recompute the
3542 // width of the number column if 'number' or 'relativenumber' is set.
3543 if (((*args->os_oldval.string == 'n' && args->os_oldval.string[1] == 'u')
3544 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
3545 && (curwin->w_p_nu || curwin->w_p_rnu))
3546 curwin->w_nrwidth_line_count = 0;
3547
3548 return NULL;
3549}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003550
3551 int
3552expand_set_signcolumn(optexpand_T *args, int *numMatches, char_u ***matches)
3553{
3554 return expand_set_opt_string(
3555 args,
3556 p_scl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003557 ARRAY_LENGTH(p_scl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003558 numMatches,
3559 matches);
3560}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003561#endif
3562
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003563#if defined(FEAT_SPELL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003564/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003565 * The 'spellcapcheck' option is changed.
3566 */
3567 char *
3568did_set_spellcapcheck(optset_T *args UNUSED)
3569{
3570 // compile the regexp program.
3571 return compile_cap_prog(curwin->w_s);
3572}
3573
3574/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003575 * The 'spellfile' option is changed.
3576 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003577 char *
3578did_set_spellfile(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003579{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003580 char_u **varp = (char_u **)args->os_varp;
3581
3582 if (!valid_spellfile(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003583 return e_invalid_argument;
3584
3585 // If there is a window for this buffer in which 'spell' is set load the
3586 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003587 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003588}
3589
3590/*
3591 * The 'spell' option is changed.
3592 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003593 char *
3594did_set_spelllang(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003595{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003596 char_u **varp = (char_u **)args->os_varp;
3597
3598 if (!valid_spelllang(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003599 return e_invalid_argument;
3600
3601 // If there is a window for this buffer in which 'spell' is set load the
3602 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003603 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003604}
3605
3606/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003607 * The 'spelloptions' option is changed.
3608 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003609 char *
3610did_set_spelloptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003611{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003612 char_u **varp = (char_u **)args->os_varp;
3613
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003614 if (**varp != NUL && STRCMP(*varp, "camel") != 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003615 return e_invalid_argument;
3616
3617 return NULL;
3618}
3619
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003620 int
3621expand_set_spelloptions(optexpand_T *args, int *numMatches, char_u ***matches)
3622{
3623 static char *(p_spo_values[]) = {"camel", NULL};
3624 return expand_set_opt_string(
3625 args,
3626 p_spo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003627 ARRAY_LENGTH(p_spo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003628 numMatches,
3629 matches);
3630}
3631
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003632/*
3633 * The 'spellsuggest' option is changed.
3634 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003635 char *
3636did_set_spellsuggest(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003637{
3638 if (spell_check_sps() != OK)
3639 return e_invalid_argument;
3640
3641 return NULL;
3642}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003643
3644 int
3645expand_set_spellsuggest(optexpand_T *args, int *numMatches, char_u ***matches)
3646{
3647 return expand_set_opt_string(
3648 args,
3649 p_sps_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003650 ARRAY_LENGTH(p_sps_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003651 numMatches,
3652 matches);
3653}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003654#endif
3655
3656/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003657 * The 'splitkeep' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003658 */
3659 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003660did_set_splitkeep(optset_T *args UNUSED)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003661{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003662 return did_set_opt_strings(p_spk, p_spk_values, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003663}
3664
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003665 int
3666expand_set_splitkeep(optexpand_T *args, int *numMatches, char_u ***matches)
3667{
3668 return expand_set_opt_string(
3669 args,
3670 p_spk_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003671 ARRAY_LENGTH(p_spk_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003672 numMatches,
3673 matches);
3674}
3675
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00003676#if defined(FEAT_STL_OPT) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003677/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003678 * The 'statusline' option is changed.
3679 */
3680 char *
3681did_set_statusline(optset_T *args)
3682{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003683 return parse_statustabline_rulerformat(args, FALSE);
3684}
3685#endif
3686
3687/*
3688 * The 'swapsync' option is changed.
3689 */
3690 char *
3691did_set_swapsync(optset_T *args UNUSED)
3692{
3693 return did_set_opt_strings(p_sws, p_sws_values, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003694}
3695
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003696 int
3697expand_set_swapsync(optexpand_T *args, int *numMatches, char_u ***matches)
3698{
3699 return expand_set_opt_string(
3700 args,
3701 p_sws_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003702 ARRAY_LENGTH(p_sws_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003703 numMatches,
3704 matches);
3705}
3706
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003707/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003708 * The 'switchbuf' option is changed.
3709 */
3710 char *
3711did_set_switchbuf(optset_T *args UNUSED)
3712{
3713 return did_set_opt_flags(p_swb, p_swb_values, &swb_flags, TRUE);
3714}
3715
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003716 int
3717expand_set_switchbuf(optexpand_T *args, int *numMatches, char_u ***matches)
3718{
3719 return expand_set_opt_string(
3720 args,
3721 p_swb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003722 ARRAY_LENGTH(p_swb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003723 numMatches,
3724 matches);
3725}
3726
LemonBoy5247b0b2024-07-12 19:30:58 +02003727/*
3728 * The 'tabclose' option is changed.
3729 */
3730 char *
3731did_set_tabclose(optset_T *args UNUSED)
3732{
3733 return did_set_opt_flags(p_tcl, p_tcl_values, &tcl_flags, TRUE);
3734}
3735
3736 int
3737expand_set_tabclose(optexpand_T *args, int *numMatches, char_u ***matches)
3738{
3739 return expand_set_opt_string(
3740 args,
3741 p_tcl_values,
3742 ARRAY_LENGTH(p_tcl_values) - 1,
3743 numMatches,
3744 matches);
3745}
3746
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003747#if defined(FEAT_STL_OPT) || defined(PROTO)
3748/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003749 * The 'tabline' option is changed.
3750 */
3751 char *
3752did_set_tabline(optset_T *args)
3753{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003754 return parse_statustabline_rulerformat(args, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003755}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003756#endif
3757
3758/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003759 * The 'tagcase' option is changed.
3760 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003761 char *
3762did_set_tagcase(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003763{
3764 unsigned int *flags;
3765 char_u *p;
3766
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003767 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003768 {
3769 p = curbuf->b_p_tc;
3770 flags = &curbuf->b_tc_flags;
3771 }
3772 else
3773 {
3774 p = p_tc;
3775 flags = &tc_flags;
3776 }
3777
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003778 if ((args->os_flags & OPT_LOCAL) && *p == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003779 // make the local value empty: use the global value
3780 *flags = 0;
3781 else if (*p == NUL
3782 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
3783 return e_invalid_argument;
3784
3785 return NULL;
3786}
3787
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003788 int
3789expand_set_tagcase(optexpand_T *args, int *numMatches, char_u ***matches)
3790{
3791 return expand_set_opt_string(
3792 args,
3793 p_tc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003794 ARRAY_LENGTH(p_tc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003795 numMatches,
3796 matches);
3797}
3798
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003799/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003800 * The 'term' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003801 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003802 char *
3803did_set_term(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003804{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003805 if (T_NAME[0] == NUL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003806 return e_cannot_set_term_to_empty_string;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003807#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003808 if (gui.in_use)
3809 return e_cannot_change_term_in_GUI;
3810 if (term_is_gui(T_NAME))
3811 return e_use_gui_to_start_GUI;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003812#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003813 if (set_termname(T_NAME) == FAIL)
3814 return e_not_found_in_termcap;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003815
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003816 // Screen colors may have changed.
3817 redraw_later_clear();
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003818
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003819 return NULL;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003820}
3821
3822/*
3823 * Some terminal option (t_xxx) is changed
3824 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003825 char *
3826did_set_term_option(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003827{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003828 char_u **varp = (char_u **)args->os_varp;
3829
3830 if (!full_screen)
3831 return NULL;
3832
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003833 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
3834 if (varp == &T_CCO)
3835 {
3836 int colors = atoi((char *)T_CCO);
3837
3838 // Only reinitialize colors if t_Co value has really changed to
3839 // avoid expensive reload of colorscheme if t_Co is set to the
3840 // same value multiple times.
3841 if (colors != t_colors)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003842 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003843 t_colors = colors;
3844 if (t_colors <= 1)
3845 {
3846 vim_free(T_CCO);
3847 T_CCO = empty_option;
3848 }
3849#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3850 if (is_term_win32())
3851 {
3852 swap_tcap();
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003853 args->os_did_swaptcap = TRUE;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003854 }
3855#endif
3856 // We now have a different color setup, initialize it again.
3857 init_highlight(TRUE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003858 }
3859 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003860 ttest(FALSE);
3861 if (varp == &T_ME)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003862 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003863 out_str(T_ME);
3864 redraw_later(UPD_CLEAR);
3865#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3866 // Since t_me has been set, this probably means that the user
3867 // wants to use this as default colors. Need to reset default
3868 // background/foreground colors.
3869# ifdef VIMDLL
3870 if (!gui.in_use && !gui.starting)
3871# endif
3872 mch_set_normal_colors();
3873#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003874 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003875 if (varp == &T_BE && termcap_active)
3876 {
3877 MAY_WANT_TO_LOG_THIS;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003878
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003879 if (*T_BE == NUL)
3880 // When clearing t_BE we assume the user no longer wants
3881 // bracketed paste, thus disable it by writing t_BD.
3882 out_str(T_BD);
3883 else
3884 out_str(T_BE);
3885 }
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003886
3887 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003888}
3889
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003890#if defined(FEAT_TERMINAL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003891/*
3892 * The 'termwinkey' option is changed.
3893 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003894 char *
Milly94606f72024-10-22 22:07:52 +02003895did_set_termwinkey(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003896{
Milly94606f72024-10-22 22:07:52 +02003897 char_u **varp = (char_u **)args->os_varp;
3898
3899 if ((*varp)[0] != NUL && string_to_key(*varp, TRUE) == 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003900 return e_invalid_argument;
3901
3902 return NULL;
3903}
3904
3905/*
3906 * The 'termwinsize' option is changed.
3907 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003908 char *
Milly8be10aa2024-10-22 22:01:46 +02003909did_set_termwinsize(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003910{
Milly8be10aa2024-10-22 22:01:46 +02003911 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003912 char_u *p;
3913
Milly8be10aa2024-10-22 22:01:46 +02003914 if ((*varp)[0] == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003915 return NULL;
3916
Milly8be10aa2024-10-22 22:01:46 +02003917 p = skipdigits(*varp);
3918 if (p == *varp || (*p != 'x' && *p != '*') || *skipdigits(p + 1) != NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003919 return e_invalid_argument;
3920
3921 return NULL;
3922}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003923
3924# if defined(MSWIN) || defined(PROTO)
3925/*
3926 * The 'termwintype' option is changed.
3927 */
3928 char *
3929did_set_termwintype(optset_T *args UNUSED)
3930{
3931 return did_set_opt_strings(p_twt, p_twt_values, FALSE);
3932}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003933
3934 int
3935expand_set_termwintype(optexpand_T *args, int *numMatches, char_u ***matches)
3936{
3937 return expand_set_opt_string(
3938 args,
3939 p_twt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003940 ARRAY_LENGTH(p_twt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003941 numMatches,
3942 matches);
3943}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003944# endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003945#endif
3946
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003947/*
3948 * The 'titlestring' option is changed.
3949 */
3950 char *
3951did_set_titlestring(optset_T *args)
3952{
3953 int flagval = 0;
3954
3955#ifdef FEAT_STL_OPT
3956 flagval = STL_IN_TITLE;
3957#endif
3958 return parse_titleiconstring(args, flagval);
3959}
3960
3961#if (defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)) || defined(PROTO)
3962/*
3963 * The 'toolbar' option is changed.
3964 */
3965 char *
3966did_set_toolbar(optset_T *args UNUSED)
3967{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003968 if (opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags,
3969 TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003970 return e_invalid_argument;
3971
3972 out_flush();
3973 gui_mch_show_toolbar((toolbar_flags &
3974 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
3975 return NULL;
3976}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003977
3978 int
3979expand_set_toolbar(optexpand_T *args, int *numMatches, char_u ***matches)
3980{
3981 return expand_set_opt_string(
3982 args,
3983 p_toolbar_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003984 ARRAY_LENGTH(p_toolbar_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003985 numMatches,
3986 matches);
3987}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003988#endif
3989
3990#if (defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)) || defined(PROTO)
3991/*
3992 * The 'toolbariconsize' option is changed. GTK+ 2 only.
3993 */
3994 char *
3995did_set_toolbariconsize(optset_T *args UNUSED)
3996{
3997 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
3998 return e_invalid_argument;
3999
4000 out_flush();
4001 gui_mch_show_toolbar((toolbar_flags &
4002 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4003 return NULL;
4004}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004005
4006 int
4007expand_set_toolbariconsize(optexpand_T *args, int *numMatches, char_u ***matches)
4008{
4009 return expand_set_opt_string(
4010 args,
4011 p_tbis_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004012 ARRAY_LENGTH(p_tbis_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004013 numMatches,
4014 matches);
4015}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004016#endif
4017
4018#if defined(UNIX) || defined(VMS) || defined(PROTO)
4019/*
4020 * The 'ttymouse' option is changed.
4021 */
4022 char *
4023did_set_ttymouse(optset_T *args UNUSED)
4024{
4025 char *errmsg = NULL;
4026
4027 // Switch the mouse off before changing the escape sequences used for
4028 // that.
4029 mch_setmouse(FALSE);
4030 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
4031 errmsg = e_invalid_argument;
4032 else
4033 check_mouse_termcode();
4034 if (termcap_active)
4035 setmouse(); // may switch it on again
4036
4037 return errmsg;
4038}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004039
4040 int
4041expand_set_ttymouse(optexpand_T *args, int *numMatches, char_u ***matches)
4042{
4043 return expand_set_opt_string(
4044 args,
4045 p_ttym_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004046 ARRAY_LENGTH(p_ttym_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004047 numMatches,
4048 matches);
4049}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004050#endif
4051
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004052#if defined(FEAT_VARTABS) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004053/*
4054 * The 'varsofttabstop' option is changed.
4055 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004056 char *
4057did_set_varsofttabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004058{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004059 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004060 char_u *cp;
4061
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004062 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004063 VIM_CLEAR(curbuf->b_p_vsts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004064 else
4065 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004066 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004067 {
4068 if (vim_isdigit(*cp))
4069 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004070 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004071 continue;
4072 return e_invalid_argument;
4073 }
4074
4075 int *oldarray = curbuf->b_p_vsts_array;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004076 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004077 {
4078 if (oldarray)
4079 vim_free(oldarray);
4080 }
4081 else
4082 return e_invalid_argument;
4083 }
4084
4085 return NULL;
4086}
4087
4088/*
4089 * The 'vartabstop' option is changed.
4090 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004091 char *
4092did_set_vartabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004093{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004094 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004095 char_u *cp;
4096
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004097 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004098 VIM_CLEAR(curbuf->b_p_vts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004099 else
4100 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004101 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004102 {
4103 if (vim_isdigit(*cp))
4104 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004105 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004106 continue;
4107 return e_invalid_argument;
4108 }
4109
4110 int *oldarray = curbuf->b_p_vts_array;
4111
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004112 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004113 {
4114 vim_free(oldarray);
4115# ifdef FEAT_FOLDING
4116 if (foldmethodIsIndent(curwin))
4117 foldUpdateAll(curwin);
4118# endif
4119 }
4120 else
4121 return e_invalid_argument;
4122 }
4123
4124 return NULL;
4125}
4126#endif
4127
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004128/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004129 * The 'verbosefile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004130 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004131 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004132did_set_verbosefile(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004133{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004134 verbose_stop();
4135 if (*p_vfile != NUL && verbose_open() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004136 return e_invalid_argument;
4137
4138 return NULL;
4139}
4140
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004141#if defined(FEAT_SESSION) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004142/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004143 * The 'viewoptions' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004144 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004145 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004146did_set_viewoptions(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004147{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004148 return did_set_opt_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004149}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004150#endif
4151
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004152#if defined(FEAT_VIMINFO) || defined(PROTO)
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004153/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004154 * The 'viminfo' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004155 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004156 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004157did_set_viminfo(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004158{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004159 char_u *s;
4160 char *errmsg = NULL;
4161
4162 for (s = p_viminfo; *s;)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004163 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004164 // Check it's a valid character
4165 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
4166 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01004167 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004168 break;
4169 }
4170 if (*s == 'n') // name is always last one
4171 break;
4172 else if (*s == 'r') // skip until next ','
4173 {
4174 while (*++s && *s != ',')
4175 ;
4176 }
4177 else if (*s == '%')
4178 {
4179 // optional number
4180 while (vim_isdigit(*++s))
4181 ;
4182 }
4183 else if (*s == '!' || *s == 'h' || *s == 'c')
4184 ++s; // no extra chars
4185 else // must have a number
4186 {
4187 while (vim_isdigit(*++s))
4188 ;
4189
4190 if (!VIM_ISDIGIT(*(s - 1)))
4191 {
4192 if (args->os_errbuf != NULL)
4193 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01004194 vim_snprintf(args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004195 _(e_missing_number_after_angle_str_angle),
4196 transchar_byte(*(s - 1)));
4197 errmsg = args->os_errbuf;
4198 }
4199 else
4200 errmsg = "";
4201 break;
4202 }
4203 }
4204 if (*s == ',')
4205 ++s;
4206 else if (*s)
4207 {
4208 if (args->os_errbuf != NULL)
4209 errmsg = e_missing_comma;
4210 else
4211 errmsg = "";
4212 break;
4213 }
4214 }
4215 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
4216 errmsg = e_must_specify_a_value;
4217
4218 return errmsg;
4219}
4220#endif
4221
4222/*
4223 * The 'virtualedit' option is changed.
4224 */
4225 char *
4226did_set_virtualedit(optset_T *args)
4227{
4228 char_u *ve = p_ve;
4229 unsigned int *flags = &ve_flags;
4230
4231 if (args->os_flags & OPT_LOCAL)
4232 {
4233 ve = curwin->w_p_ve;
4234 flags = &curwin->w_ve_flags;
4235 }
4236
4237 if ((args->os_flags & OPT_LOCAL) && *ve == NUL)
4238 // make the local value empty: use the global value
4239 *flags = 0;
4240 else
4241 {
4242 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
4243 return e_invalid_argument;
4244 else if (STRCMP(ve, args->os_oldval.string) != 0)
4245 {
4246 // Recompute cursor position in case the new 've' setting
4247 // changes something.
4248 validate_virtcol();
4249 coladvance(curwin->w_virtcol);
4250 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004251 }
4252
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004253 return NULL;
4254}
4255
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004256 int
4257expand_set_virtualedit(optexpand_T *args, int *numMatches, char_u ***matches)
4258{
4259 return expand_set_opt_string(
4260 args,
4261 p_ve_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004262 ARRAY_LENGTH(p_ve_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004263 numMatches,
4264 matches);
4265}
4266
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004267/*
4268 * The 'whichwrap' option is changed.
4269 */
4270 char *
4271did_set_whichwrap(optset_T *args)
4272{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004273 char_u **varp = (char_u **)args->os_varp;
4274
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004275 // Add ',' to the list flags because 'whichwrap' is a flag
4276 // list that is comma-separated.
Christian Brabandtb39b2402023-11-29 11:34:05 +01004277 return did_set_option_listflag(*varp, (char_u *)(WW_ALL ","),
4278 args->os_errbuf, args->os_errbuflen);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004279}
4280
4281 int
4282expand_set_whichwrap(optexpand_T *args, int *numMatches, char_u ***matches)
4283{
4284 return expand_set_opt_listflag(args, (char_u*)WW_ALL, numMatches, matches);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004285}
4286
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004287/*
4288 * The 'wildmode' option is changed.
4289 */
4290 char *
4291did_set_wildmode(optset_T *args UNUSED)
4292{
4293 if (check_opt_wim() == FAIL)
4294 return e_invalid_argument;
4295 return NULL;
4296}
4297
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004298 int
4299expand_set_wildmode(optexpand_T *args, int *numMatches, char_u ***matches)
4300{
4301 return expand_set_opt_string(
4302 args,
4303 p_wim_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004304 ARRAY_LENGTH(p_wim_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004305 numMatches,
4306 matches);
4307}
4308
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004309/*
4310 * The 'wildoptions' option is changed.
4311 */
4312 char *
4313did_set_wildoptions(optset_T *args UNUSED)
4314{
4315 return did_set_opt_strings(p_wop, p_wop_values, TRUE);
4316}
4317
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004318 int
4319expand_set_wildoptions(optexpand_T *args, int *numMatches, char_u ***matches)
4320{
4321 return expand_set_opt_string(
4322 args,
4323 p_wop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004324 ARRAY_LENGTH(p_wop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004325 numMatches,
4326 matches);
4327}
4328
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004329#if defined(FEAT_WAK) || defined(PROTO)
4330/*
4331 * The 'winaltkeys' option is changed.
4332 */
4333 char *
4334did_set_winaltkeys(optset_T *args UNUSED)
4335{
4336 char *errmsg = NULL;
4337
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004338 if (*p_wak == NUL || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004339 errmsg = e_invalid_argument;
4340# ifdef FEAT_MENU
4341# if defined(FEAT_GUI_MOTIF)
4342 else if (gui.in_use)
4343 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4344# elif defined(FEAT_GUI_GTK)
4345 else if (gui.in_use)
4346 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4347# endif
4348# endif
4349 return errmsg;
4350}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004351
4352 int
4353expand_set_winaltkeys(optexpand_T *args, int *numMatches, char_u ***matches)
4354{
4355 return expand_set_opt_string(
4356 args,
4357 p_wak_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004358 ARRAY_LENGTH(p_wak_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004359 numMatches,
4360 matches);
4361}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004362#endif
4363
4364/*
4365 * The 'wincolor' option is changed.
4366 */
4367 char *
4368did_set_wincolor(optset_T *args UNUSED)
4369{
4370#ifdef FEAT_TERMINAL
4371 term_update_wincolor(curwin);
4372#endif
4373 return NULL;
4374}
4375
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004376 int
4377expand_set_wincolor(optexpand_T *args, int *numMatches, char_u ***matches)
4378{
4379 return expand_set_opt_generic(
4380 args,
4381 get_highlight_name,
4382 numMatches,
4383 matches);
4384}
4385
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004386#ifdef FEAT_SYN_HL
4387/*
4388 * When the 'syntax' option is set, load the syntax of that name.
4389 */
4390 static void
4391do_syntax_autocmd(int value_changed)
4392{
4393 static int syn_recursive = 0;
4394
4395 ++syn_recursive;
4396 // Only pass TRUE for "force" when the value changed or not used
4397 // recursively, to avoid endless recurrence.
4398 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
4399 value_changed || syn_recursive == 1, curbuf);
4400 curbuf->b_flags |= BF_SYN_SET;
4401 --syn_recursive;
4402}
4403#endif
4404
4405/*
4406 * When the 'filetype' option is set, trigger the FileType autocommand.
4407 */
4408 static void
4409do_filetype_autocmd(char_u **varp, int opt_flags, int value_changed)
4410{
4411 // Skip this when called from a modeline and the filetype was already set
4412 // to this value.
4413 if ((opt_flags & OPT_MODELINE) && !value_changed)
4414 return;
4415
4416 static int ft_recursive = 0;
4417 int secure_save = secure;
4418
4419 // Reset the secure flag, since the value of 'filetype' has
4420 // been checked to be safe.
4421 secure = 0;
4422
4423 ++ft_recursive;
zeertzjq5bf6c212024-03-31 18:41:27 +02004424 curbuf->b_did_filetype = TRUE;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004425 // Only pass TRUE for "force" when the value changed or not
4426 // used recursively, to avoid endless recurrence.
4427 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
4428 value_changed || ft_recursive == 1, curbuf);
4429 --ft_recursive;
4430 // Just in case the old "curbuf" is now invalid.
4431 if (varp != &(curbuf->b_p_ft))
4432 varp = NULL;
4433
4434 secure = secure_save;
4435}
4436
4437#ifdef FEAT_SPELL
4438/*
4439 * When the 'spelllang' option is set, source the spell/LANG.vim file in
4440 * 'runtimepath'.
4441 */
4442 static void
4443do_spelllang_source(void)
4444{
4445 char_u fname[200];
4446 char_u *p;
4447 char_u *q = curwin->w_s->b_p_spl;
4448
4449 // Skip the first name if it is "cjk".
4450 if (STRNCMP(q, "cjk,", 4) == 0)
4451 q += 4;
4452
4453 // They could set 'spellcapcheck' depending on the language. Use the first
4454 // name in 'spelllang' up to '_region' or '.encoding'.
4455 for (p = q; *p != NUL; ++p)
4456 if (!ASCII_ISALNUM(*p) && *p != '-')
4457 break;
4458 if (p > q)
4459 {
4460 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
4461 (int)(p - q), q);
4462 source_runtime(fname, DIP_ALL);
4463 }
4464}
4465#endif
4466
4467/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004468 * Handle string options that need some action to perform when changed.
zeertzjqf6782732022-07-27 18:26:03 +01004469 * The new value must be allocated.
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004470 * Returns NULL for success, or an untranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +02004471 */
4472 char *
4473did_set_string_option(
4474 int opt_idx, // index in options[] table
4475 char_u **varp, // pointer to the option variable
Bram Moolenaardac13472019-09-16 21:06:21 +02004476 char_u *oldval, // previous value of the option
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004477 char_u *value, // new value of the option
Bram Moolenaardac13472019-09-16 21:06:21 +02004478 char *errbuf, // buffer for errors, or NULL
Mike Williams620f0112023-12-05 15:36:06 +01004479 size_t errbuflen, // length of error buffer
Bram Moolenaardac13472019-09-16 21:06:21 +02004480 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004481 set_op_T op, // OP_ADDING/OP_PREPENDING/OP_REMOVING
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004482 int *value_checked) // value was checked to be safe, no
Bram Moolenaardac13472019-09-16 21:06:21 +02004483 // need to set P_INSECURE
4484{
4485 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +02004486 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004487 opt_did_set_cb_T did_set_cb = get_option_did_set_cb(opt_idx);
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004488 optset_T args;
4489
4490 // 'ttytype' is an alias for 'term'. Both 'term' and 'ttytype' point to
4491 // T_NAME. If 'term' or 'ttytype' is modified, then use the index for the
4492 // 'term' option. Only set the P_ALLOCED flag on 'term'.
4493 if (varp == &T_NAME)
4494 {
4495 opt_idx = findoption((char_u *)"term");
4496 if (opt_idx >= 0)
4497 {
4498 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
4499 did_set_cb = get_option_did_set_cb(opt_idx);
4500 }
4501 }
4502
4503 CLEAR_FIELD(args);
Bram Moolenaardac13472019-09-16 21:06:21 +02004504
Bram Moolenaardac13472019-09-16 21:06:21 +02004505 // Disallow changing some options from secure mode
4506 if ((secure
4507#ifdef HAVE_SANDBOX
4508 || sandbox != 0
4509#endif
4510 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00004511 errmsg = e_not_allowed_here;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004512 // Check for a "normal" directory or file name in some options.
4513 else if (check_illegal_path_names(opt_idx, varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004514 errmsg = e_invalid_argument;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004515 else if (did_set_cb != NULL)
4516 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004517 args.os_varp = (char_u *)varp;
4518 args.os_idx = opt_idx;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004519 args.os_flags = opt_flags;
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004520 args.os_op = op;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004521 args.os_oldval.string = oldval;
4522 args.os_newval.string = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004523 args.os_errbuf = errbuf;
Christian Brabandtb39b2402023-11-29 11:34:05 +01004524 args.os_errbuflen = errbuflen;
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004525 // Invoke the option specific callback function to validate and apply
4526 // the new option value.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004527 errmsg = did_set_cb(&args);
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004528
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004529 // The 'keymap', 'filetype' and 'syntax' option callback functions
4530 // may change the os_value_checked field.
4531 *value_checked = args.os_value_checked;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004532 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004533
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004534 // If an error is detected, restore the previous value.
Bram Moolenaardac13472019-09-16 21:06:21 +02004535 if (errmsg != NULL)
4536 {
zeertzjqf6782732022-07-27 18:26:03 +01004537 free_string_option(*varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02004538 *varp = oldval;
4539 // When resetting some values, need to act on it.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004540 if (args.os_restore_chartab)
Bram Moolenaardac13472019-09-16 21:06:21 +02004541 (void)init_chartab();
4542 if (varp == &p_hl)
4543 (void)highlight_changed();
4544 }
4545 else
4546 {
4547#ifdef FEAT_EVAL
4548 // Remember where the option was set.
4549 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4550#endif
4551 // Free string options that are in allocated memory.
4552 // Use "free_oldval", because recursiveness may change the flags under
4553 // our fingers (esp. init_highlight()).
4554 if (free_oldval)
4555 free_string_option(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01004556 set_option_flag(opt_idx, P_ALLOCED);
Bram Moolenaardac13472019-09-16 21:06:21 +02004557
4558 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
4559 && is_global_local_option(opt_idx))
4560 {
4561 // global option with local value set to use global value; free
4562 // the local value and make it empty
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004563 char_u *p = get_option_varp_scope(opt_idx, OPT_LOCAL);
Bram Moolenaardac13472019-09-16 21:06:21 +02004564 free_string_option(*(char_u **)p);
4565 *(char_u **)p = empty_option;
4566 }
4567
4568 // May set global value for local option.
4569 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
4570 set_string_option_global(opt_idx, varp);
4571
4572 // Trigger the autocommand only after setting the flags.
4573#ifdef FEAT_SYN_HL
Bram Moolenaardac13472019-09-16 21:06:21 +02004574 if (varp == &(curbuf->b_p_syn))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004575 do_syntax_autocmd(args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004576#endif
4577 else if (varp == &(curbuf->b_p_ft))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004578 do_filetype_autocmd(varp, opt_flags, args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004579#ifdef FEAT_SPELL
4580 if (varp == &(curwin->w_s->b_p_spl))
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004581 do_spelllang_source();
Bram Moolenaardac13472019-09-16 21:06:21 +02004582#endif
4583 }
4584
Bram Moolenaardac13472019-09-16 21:06:21 +02004585 if (varp == &p_mouse)
4586 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004587 if (*p_mouse == NUL)
4588 mch_setmouse(FALSE); // switch mouse off
4589 else
Bram Moolenaardac13472019-09-16 21:06:21 +02004590 setmouse(); // in case 'mouse' changed
4591 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004592
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004593#if defined(FEAT_LUA) || defined(PROTO)
4594 if (varp == &p_rtp)
4595 update_package_paths_in_lua();
4596#endif
4597
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004598#if defined(FEAT_LINEBREAK)
4599 // Changing Formatlistpattern when briopt includes the list setting:
4600 // redraw
4601 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
4602 && curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004603 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004604#endif
4605
Bram Moolenaardac13472019-09-16 21:06:21 +02004606 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004607 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0
4608 && (get_option_flags(opt_idx) & P_HLONLY) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02004609 curwin->w_set_curswant = TRUE;
4610
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004611 if ((opt_flags & OPT_NO_REDRAW) == 0)
4612 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004613#ifdef FEAT_GUI
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004614 // set when changing an option that only requires a redraw in the GUI
4615 int redraw_gui_only = FALSE;
4616
4617 if (varp == &p_go // 'guioptions'
4618 || varp == &p_guifont // 'guifont'
4619# ifdef FEAT_GUI_TABLINE
4620 || varp == &p_gtl // 'guitablabel'
4621 || varp == &p_gtt // 'guitabtooltip'
4622# endif
4623# ifdef FEAT_XFONTSET
4624 || varp == &p_guifontset // 'guifontset'
4625# endif
4626 || varp == &p_guifontwide // 'guifontwide'
Erik S. V. Jansson2f026382024-02-26 22:23:05 +01004627# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004628 || varp == &p_guiligatures // 'guiligatures'
4629# endif
4630 )
4631 redraw_gui_only = TRUE;
4632
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004633 // check redraw when it's not a GUI option or the GUI is active.
4634 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02004635#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004636 check_redraw(get_option_flags(opt_idx));
4637 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004638
4639#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004640 if (args.os_did_swaptcap)
Bram Moolenaardac13472019-09-16 21:06:21 +02004641 {
4642 set_termname((char_u *)"win32");
4643 init_highlight(TRUE, FALSE);
4644 }
4645#endif
4646
4647 return errmsg;
4648}
4649
4650/*
4651 * Check an option that can be a range of string values.
4652 *
4653 * Return OK for correct value, FAIL otherwise.
4654 * Empty is always OK.
4655 */
4656 static int
4657check_opt_strings(
4658 char_u *val,
4659 char **values,
4660 int list) // when TRUE: accept a list of values
4661{
4662 return opt_strings_flags(val, values, NULL, list);
4663}
4664
4665/*
4666 * Handle an option that can be a range of string values.
4667 * Set a flag in "*flagp" for each string present.
4668 *
4669 * Return OK for correct value, FAIL otherwise.
4670 * Empty is always OK.
4671 */
4672 static int
4673opt_strings_flags(
4674 char_u *val, // new value
4675 char **values, // array of valid string values
4676 unsigned *flagp,
4677 int list) // when TRUE: accept a list of values
4678{
4679 int i;
4680 int len;
4681 unsigned new_flags = 0;
4682
4683 while (*val)
4684 {
4685 for (i = 0; ; ++i)
4686 {
4687 if (values[i] == NULL) // val not found in values[]
4688 return FAIL;
4689
4690 len = (int)STRLEN(values[i]);
4691 if (STRNCMP(values[i], val, len) == 0
4692 && ((list && val[len] == ',') || val[len] == NUL))
4693 {
4694 val += len + (val[len] == ',');
4695 new_flags |= (1 << i);
4696 break; // check next item in val list
4697 }
4698 }
4699 }
4700 if (flagp != NULL)
4701 *flagp = new_flags;
4702
4703 return OK;
4704}
4705
4706/*
4707 * return OK if "p" is a valid fileformat name, FAIL otherwise.
4708 */
4709 int
4710check_ff_value(char_u *p)
4711{
4712 return check_opt_strings(p, p_ff_values, FALSE);
4713}
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004714
4715/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004716 * Save the actual shortmess Flags and clear them temporarily to avoid that
4717 * file messages overwrites any output from the following commands.
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004718 *
4719 * Caller must make sure to first call save_clear_shm_value() and then
4720 * restore_shm_value() exactly the same number of times.
4721 */
4722 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004723save_clear_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004724{
4725 if (STRLEN(p_shm) >= SHM_LEN)
4726 {
4727 iemsg(e_internal_error_shortmess_too_long);
4728 return;
4729 }
4730
4731 if (++set_shm_recursive == 1)
4732 {
4733 STRCPY(shm_buf, p_shm);
4734 set_option_value_give_err((char_u *)"shm", 0L, (char_u *)"", 0);
4735 }
4736}
4737
4738/*
4739 * Restore the shortmess Flags set from the save_clear_shm_value() function.
4740 */
4741 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004742restore_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004743{
4744 if (--set_shm_recursive == 0)
4745 {
4746 set_option_value_give_err((char_u *)"shm", 0L, shm_buf, 0);
4747 vim_memset(shm_buf, 0, SHM_LEN);
4748 }
4749}