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