blob: d82b8e116da40385ba7622d934709952673d946b [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);
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +0200327#ifdef FEAT_EVAL
328 check_string_option(&buf->b_p_fexpr);
329#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200330 check_string_option(&buf->b_p_path);
331 check_string_option(&buf->b_p_tags);
332 check_string_option(&buf->b_p_tc);
333 check_string_option(&buf->b_p_dict);
334 check_string_option(&buf->b_p_tsr);
Bram Moolenaardac13472019-09-16 21:06:21 +0200335 check_string_option(&buf->b_p_lw);
Bram Moolenaardac13472019-09-16 21:06:21 +0200336 check_string_option(&buf->b_p_bkc);
337 check_string_option(&buf->b_p_menc);
338#ifdef FEAT_VARTABS
339 check_string_option(&buf->b_p_vsts);
340 check_string_option(&buf->b_p_vts);
341#endif
342}
343
344/*
345 * Free the string allocated for an option.
346 * Checks for the string being empty_option. This may happen if we're out of
347 * memory, vim_strsave() returned NULL, which was replaced by empty_option by
348 * check_options().
349 * Does NOT check for P_ALLOCED flag!
350 */
351 void
352free_string_option(char_u *p)
353{
354 if (p != empty_option)
355 vim_free(p);
356}
357
358 void
359clear_string_option(char_u **pp)
360{
361 if (*pp != empty_option)
362 vim_free(*pp);
363 *pp = empty_option;
364}
365
366 void
367check_string_option(char_u **pp)
368{
369 if (*pp == NULL)
370 *pp = empty_option;
371}
372
373/*
374 * Set global value for string option when it's a local option.
375 */
376 static void
377set_string_option_global(
378 int opt_idx, // option index
379 char_u **varp) // pointer to option variable
380{
381 char_u **p, *s;
382
383 // the global value is always allocated
384 if (is_window_local_option(opt_idx))
385 p = (char_u **)GLOBAL_WO(varp);
386 else
387 p = (char_u **)get_option_var(opt_idx);
388 if (!is_global_option(opt_idx)
389 && p != varp
390 && (s = vim_strsave(*varp)) != NULL)
391 {
392 free_string_option(*p);
393 *p = s;
394 }
395}
396
397/*
398 * Set a string option to a new value (without checking the effect).
399 * The string is copied into allocated memory.
400 * if ("opt_idx" == -1) "name" is used, otherwise "opt_idx" is used.
401 * When "set_sid" is zero set the scriptID to current_sctx.sc_sid. When
402 * "set_sid" is SID_NONE don't set the scriptID. Otherwise set the scriptID to
403 * "set_sid".
404 */
405 void
406set_string_option_direct(
407 char_u *name,
408 int opt_idx,
409 char_u *val,
410 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
411 int set_sid UNUSED)
412{
413 char_u *s;
414 char_u **varp;
415 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
416 int idx = opt_idx;
417
418 if (idx == -1) // use name
419 {
420 idx = findoption(name);
421 if (idx < 0) // not found (should not happen)
422 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000423 semsg(_(e_internal_error_str), "set_string_option_direct()");
RestorerZ68ebcee2023-05-31 17:12:14 +0100424 siemsg("For option %s", name);
Bram Moolenaardac13472019-09-16 21:06:21 +0200425 return;
426 }
427 }
428
429 if (is_hidden_option(idx)) // can't set hidden option
430 return;
431
432 s = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000433 if (s == NULL)
434 return;
435
436 varp = (char_u **)get_option_varp_scope(idx,
437 both ? OPT_LOCAL : opt_flags);
438 if ((opt_flags & OPT_FREE) && (get_option_flags(idx) & P_ALLOCED))
439 free_string_option(*varp);
440 *varp = s;
441
442 // For buffer/window local option may also set the global value.
443 if (both)
444 set_string_option_global(idx, varp);
445
446 set_option_flag(idx, P_ALLOCED);
447
448 // When setting both values of a global option with a local value,
449 // make the local value empty, so that the global value is used.
450 if (is_global_local_option(idx) && both)
Bram Moolenaardac13472019-09-16 21:06:21 +0200451 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000452 free_string_option(*varp);
453 *varp = empty_option;
Bram Moolenaardac13472019-09-16 21:06:21 +0200454 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000455# ifdef FEAT_EVAL
456 if (set_sid != SID_NONE)
457 {
458 sctx_T script_ctx;
459
460 if (set_sid == 0)
461 script_ctx = current_sctx;
462 else
463 {
464 script_ctx.sc_sid = set_sid;
465 script_ctx.sc_seq = 0;
466 script_ctx.sc_lnum = 0;
467 script_ctx.sc_version = 1;
468 }
469 set_option_sctx_idx(idx, opt_flags, script_ctx);
470 }
471# endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200472}
473
Dominique Pellee764d1b2023-03-12 21:20:59 +0000474#if defined(FEAT_PROP_POPUP) || \
475 (defined(FEAT_DIFF) && defined(FEAT_FOLDING)) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200476/*
477 * Like set_string_option_direct(), but for a window-local option in "wp".
478 * Blocks autocommands to avoid the old curwin becoming invalid.
479 */
480 void
481set_string_option_direct_in_win(
482 win_T *wp,
483 char_u *name,
484 int opt_idx,
485 char_u *val,
486 int opt_flags,
487 int set_sid)
488{
489 win_T *save_curwin = curwin;
490
491 block_autocmds();
492 curwin = wp;
493 curbuf = curwin->w_buffer;
494 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
495 curwin = save_curwin;
496 curbuf = curwin->w_buffer;
497 unblock_autocmds();
498}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000499#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200500
Dominique Pelle748b3082022-01-08 12:41:16 +0000501#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200502/*
503 * Like set_string_option_direct(), but for a buffer-local option in "buf".
504 * Blocks autocommands to avoid the old curbuf becoming invalid.
505 */
506 void
507set_string_option_direct_in_buf(
508 buf_T *buf,
509 char_u *name,
510 int opt_idx,
511 char_u *val,
512 int opt_flags,
513 int set_sid)
514{
515 buf_T *save_curbuf = curbuf;
516
517 block_autocmds();
518 curbuf = buf;
519 curwin->w_buffer = curbuf;
520 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
521 curbuf = save_curbuf;
522 curwin->w_buffer = curbuf;
523 unblock_autocmds();
524}
Dominique Pelle748b3082022-01-08 12:41:16 +0000525#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200526
527/*
528 * Set a string option to a new value, and handle the effects.
529 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100530 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaardac13472019-09-16 21:06:21 +0200531 */
532 char *
533set_string_option(
534 int opt_idx,
535 char_u *value,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +0000536 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Christian Brabandtb39b2402023-11-29 11:34:05 +0100537 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +0100538 size_t errbuflen)
Bram Moolenaardac13472019-09-16 21:06:21 +0200539{
540 char_u *s;
541 char_u **varp;
542 char_u *oldval;
543#if defined(FEAT_EVAL)
544 char_u *oldval_l = NULL;
545 char_u *oldval_g = NULL;
546 char_u *saved_oldval = NULL;
547 char_u *saved_oldval_l = NULL;
548 char_u *saved_oldval_g = NULL;
549 char_u *saved_newval = NULL;
550#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100551 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +0200552 int value_checked = FALSE;
553
554 if (is_hidden_option(opt_idx)) // don't set hidden option
555 return NULL;
556
Bram Moolenaar7f009df2020-03-16 20:27:38 +0100557 s = vim_strsave(value == NULL ? (char_u *)"" : value);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000558 if (s == NULL)
559 return NULL;
560
561 varp = (char_u **)get_option_varp_scope(opt_idx,
562 (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
563 ? (is_global_local_option(opt_idx)
564 ? OPT_GLOBAL : OPT_LOCAL)
565 : opt_flags);
566 oldval = *varp;
567#if defined(FEAT_EVAL)
568 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +0200569 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000570 oldval_l = *(char_u **)get_option_varp_scope(opt_idx, OPT_LOCAL);
571 oldval_g = *(char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
Bram Moolenaardac13472019-09-16 21:06:21 +0200572 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000573#endif
574 *varp = s;
575
576#if defined(FEAT_EVAL)
577 if (!starting
578# ifdef FEAT_CRYPT
579 && !is_crypt_key_option(opt_idx)
580# endif
581 )
582 {
583 if (oldval_l != NULL)
584 saved_oldval_l = vim_strsave(oldval_l);
585 if (oldval_g != NULL)
586 saved_oldval_g = vim_strsave(oldval_g);
587 saved_oldval = vim_strsave(oldval);
588 saved_newval = vim_strsave(s);
589 }
590#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000591 if ((errmsg = did_set_string_option(opt_idx, varp, oldval, value, errbuf,
Christian Brabandtb39b2402023-11-29 11:34:05 +0100592 errbuflen, opt_flags, OP_NONE, &value_checked)) == NULL)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000593 did_set_option(opt_idx, opt_flags, TRUE, value_checked);
594
595#if defined(FEAT_EVAL)
596 // call autocommand after handling side effects
597 if (errmsg == NULL)
598 trigger_optionset_string(opt_idx, opt_flags,
599 saved_oldval, saved_oldval_l,
600 saved_oldval_g, saved_newval);
601 vim_free(saved_oldval);
602 vim_free(saved_oldval_l);
603 vim_free(saved_oldval_g);
604 vim_free(saved_newval);
605#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100606 return errmsg;
Bram Moolenaardac13472019-09-16 21:06:21 +0200607}
608
609/*
610 * Return TRUE if "val" is a valid 'filetype' name.
611 * Also used for 'syntax' and 'keymap'.
612 */
613 static int
614valid_filetype(char_u *val)
615{
616 return valid_name(val, ".-_");
617}
618
619#ifdef FEAT_STL_OPT
620/*
621 * Check validity of options with the 'statusline' format.
zeertzjq5dc294a2022-04-15 13:17:57 +0100622 * Return an untranslated error message or NULL.
Bram Moolenaardac13472019-09-16 21:06:21 +0200623 */
624 static char *
625check_stl_option(char_u *s)
626{
Bram Moolenaardac13472019-09-16 21:06:21 +0200627 int groupdepth = 0;
Christian Brabandtb39b2402023-11-29 11:34:05 +0100628 static char errbuf[ERR_BUFLEN];
629 int errbuflen = ERR_BUFLEN;
Bram Moolenaardac13472019-09-16 21:06:21 +0200630
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100631 while (*s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200632 {
633 // Check for valid keys after % sequences
634 while (*s && *s != '%')
635 s++;
636 if (!*s)
637 break;
638 s++;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +0000639 if (*s == '%' || *s == STL_TRUNCMARK || *s == STL_SEPARATE)
Bram Moolenaardac13472019-09-16 21:06:21 +0200640 {
641 s++;
642 continue;
643 }
644 if (*s == ')')
645 {
646 s++;
647 if (--groupdepth < 0)
648 break;
649 continue;
650 }
651 if (*s == '-')
652 s++;
653 while (VIM_ISDIGIT(*s))
654 s++;
655 if (*s == STL_USER_HL)
656 continue;
657 if (*s == '.')
658 {
659 s++;
660 while (*s && VIM_ISDIGIT(*s))
661 s++;
662 }
663 if (*s == '(')
664 {
665 groupdepth++;
666 continue;
667 }
668 if (vim_strchr(STL_ALL, *s) == NULL)
669 {
Christian Brabandtb39b2402023-11-29 11:34:05 +0100670 return illegal_char(errbuf, errbuflen, *s);
Bram Moolenaardac13472019-09-16 21:06:21 +0200671 }
672 if (*s == '{')
673 {
zeertzjq5dc294a2022-04-15 13:17:57 +0100674 int reevaluate = (*++s == '%');
shadmansaleh30e3de22021-05-15 17:23:28 +0200675
zeertzjq5dc294a2022-04-15 13:17:57 +0100676 if (reevaluate && *++s == '}')
677 // "}" is not allowed immediately after "%{%"
Christian Brabandtb39b2402023-11-29 11:34:05 +0100678 return illegal_char(errbuf, errbuflen, '}');
shadmansaleh30e3de22021-05-15 17:23:28 +0200679 while ((*s != '}' || (reevaluate && s[-1] != '%')) && *s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200680 s++;
681 if (*s != '}')
zeertzjq5dc294a2022-04-15 13:17:57 +0100682 return e_unclosed_expression_sequence;
Bram Moolenaardac13472019-09-16 21:06:21 +0200683 }
684 }
Bram Moolenaardac13472019-09-16 21:06:21 +0200685 if (groupdepth != 0)
zeertzjq5dc294a2022-04-15 13:17:57 +0100686 return e_unbalanced_groups;
Bram Moolenaardac13472019-09-16 21:06:21 +0200687 return NULL;
688}
689#endif
690
691/*
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +0000692 * Check for a "normal" directory or file name in some options. Disallow a
693 * path separator (slash and/or backslash), wildcards and characters that are
694 * often illegal in a file name. Be more permissive if "secure" is off.
695 */
696 static int
697check_illegal_path_names(int opt_idx, char_u **varp)
698{
699 return (((get_option_flags(opt_idx) & P_NFNAME)
700 && vim_strpbrk(*varp, (char_u *)(secure
701 ? "/\\*?[|;&<>\r\n" : "/\\*?[<>\r\n")) != NULL)
702 || ((get_option_flags(opt_idx) & P_NDNAME)
703 && vim_strpbrk(*varp, (char_u *)"*?[|;&<>\r\n") != NULL));
704}
705
706/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000707 * An option that accepts a list of flags is changed.
708 * e.g. 'viewoptions', 'switchbuf', 'casemap', etc.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000709 */
710 static char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000711did_set_opt_flags(char_u *val, char **values, unsigned *flagp, int list)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000712{
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000713 if (opt_strings_flags(val, values, flagp, list) == FAIL)
714 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000715
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000716 return NULL;
717}
718
719/*
720 * An option that accepts a list of string values is changed.
721 * e.g. 'nrformats', 'scrollopt', 'wildoptions', etc.
722 */
723 static char *
724did_set_opt_strings(char_u *val, char **values, int list)
725{
726 return did_set_opt_flags(val, values, NULL, list);
727}
728
729/*
730 * An option which is a list of flags is set. Valid values are in 'flags'.
731 */
732 static char *
Christian Brabandtb39b2402023-11-29 11:34:05 +0100733did_set_option_listflag(
734 char_u *val,
735 char_u *flags,
736 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +0100737 size_t errbuflen)
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000738{
739 char_u *s;
740
Yegappan Lakshmananc727b192023-03-03 12:26:15 +0000741 for (s = val; *s; ++s)
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000742 if (vim_strchr(flags, *s) == NULL)
Christian Brabandtb39b2402023-11-29 11:34:05 +0100743 return illegal_char(errbuf, errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000744
745 return NULL;
746}
747
748/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200749 * Expand an option that accepts a list of fixed string values with known
750 * number of items.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200751 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200752 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200753expand_set_opt_string(
754 optexpand_T *args,
755 char **values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +0200756 size_t numValues,
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200757 int *numMatches,
758 char_u ***matches)
759{
760 char_u *p;
761 regmatch_T *regmatch = args->oe_regmatch;
762 int include_orig_val = args->oe_include_orig_val;
763 char_u *option_val = args->oe_opt_value;
764
765 // Assume numValues is small since they are fixed enums, so just allocate
766 // upfront instead of needing two passes to calculate output size.
767 *matches = ALLOC_MULT(char_u *, numValues + 1);
768 if (*matches == NULL)
769 return FAIL;
770
771 int count = 0;
772
773 if (include_orig_val && *option_val != NUL)
774 {
775 p = vim_strsave(option_val);
776 if (p == NULL)
777 {
778 VIM_CLEAR(*matches);
779 return FAIL;
780 }
781 (*matches)[count++] = p;
782 }
783
784 for (char **val = values; *val != NULL; val++)
785 {
786 if (include_orig_val && *option_val != NUL)
787 {
788 if (STRCMP((char_u*)*val, option_val) == 0)
789 continue;
790 }
791 if (vim_regexec(regmatch, (char_u*)(*val), (colnr_T)0))
792 {
793 p = vim_strsave((char_u*)*val);
794 if (p == NULL)
795 {
796 if (count == 0)
797 {
798 VIM_CLEAR(*matches);
799 return FAIL;
800 }
801 else
802 break;
803 }
804 (*matches)[count++] = p;
805 }
806 }
807 if (count == 0)
808 {
809 VIM_CLEAR(*matches);
810 return FAIL;
811 }
812 *numMatches = count;
813 return OK;
814}
815
816static char_u *set_opt_callback_orig_option = NULL;
817static char_u *((*set_opt_callback_func)(expand_T *, int));
818
819/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200820 * Callback used by expand_set_opt_generic to also include the original value
821 * as the first item.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200822 */
823 static char_u *
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200824expand_set_opt_generic_cb(expand_T *xp, int idx)
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200825{
826 if (idx == 0)
827 {
828 if (set_opt_callback_orig_option != NULL)
829 return set_opt_callback_orig_option;
830 else
831 return (char_u *)""; // empty strings are ignored
832 }
833 return set_opt_callback_func(xp, idx - 1);
834}
835
836/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200837 * Expand an option with a callback that iterates through a list of possible
838 * names using an index.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200839 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200840 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200841expand_set_opt_generic(
842 optexpand_T *args,
843 char_u *((*func)(expand_T *, int)),
844 int *numMatches,
845 char_u ***matches)
846{
847 int ret;
848
849 set_opt_callback_orig_option = args->oe_include_orig_val ?
850 args->oe_opt_value : NULL;
851 set_opt_callback_func = func;
852
853 ret = ExpandGeneric(
854 (char_u*)"", // not using fuzzy as currently EXPAND_STRING_SETTING doesn't use it
855 args->oe_xp,
856 args->oe_regmatch,
857 matches,
858 numMatches,
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200859 expand_set_opt_generic_cb,
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200860 FALSE);
861
862 set_opt_callback_orig_option = NULL;
863 set_opt_callback_func = NULL;
864 return ret;
865}
866
Christian Brabandt9960ebc2023-10-05 22:17:09 +0200867# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200868static garray_T *expand_cb_ga;
869static optexpand_T *expand_cb_args;
870
871/*
872 * Callback provided to a function in expand_set_opt_callback. Will perform
873 * regex matching against the value and add to the list.
874 *
875 * Returns OK usually. Returns FAIL if it failed to allocate memory, and the
876 * caller should terminate the enumeration.
877 */
878 static int
879expand_set_opt_callback_cb(char_u *val)
880{
881 regmatch_T *regmatch = expand_cb_args->oe_regmatch;
882 expand_T *xp = expand_cb_args->oe_xp;
883 garray_T *ga = expand_cb_ga;
884 char_u *str;
885
886 if (val == NULL || *val == NUL)
887 return OK;
888
889 if (xp->xp_pattern[0] != NUL &&
890 !vim_regexec(regmatch, val, (colnr_T)0))
891 return OK;
892
893 str = vim_strsave_escaped(val, (char_u *)" \t\\");
894
895 if (str == NULL)
896 return FAIL;
897
898 if (ga_grow(ga, 1) == FAIL)
899 {
900 vim_free(str);
901 return FAIL;
902 }
903
904 ((char_u **)ga->ga_data)[ga->ga_len] = str;
905 ++ga->ga_len;
906 return OK;
907}
908
909/*
910 * Expand an option with a provided function that takes a callback. The
911 * function will enumerate through all options and call the callback to add it
912 * to the list.
913 *
914 * "func" is the enumerator function that will generate the list of options.
915 * "func_params" is a single parameter that will be passed to func.
916 */
917 static int
918expand_set_opt_callback(
919 optexpand_T *args,
920 void (*func)(optexpand_T *, void* params, int (*cb)(char_u *val)),
921 void *func_params,
922 int *numMatches,
923 char_u ***matches)
924{
925 garray_T ga;
926 int include_orig_val = args->oe_include_orig_val;
927 char_u *option_val = args->oe_opt_value;
928
929 ga_init2(&ga, sizeof(char *), 30);
930
931 if (include_orig_val && *option_val != NUL)
932 {
933 char_u *p = vim_strsave(option_val);
934 if (p == NULL)
935 return FAIL;
936 if (ga_grow(&ga, 1) == FAIL)
937 {
938 vim_free(p);
939 return FAIL;
940 }
941 ((char_u **)ga.ga_data)[ga.ga_len] = p;
942 ++ga.ga_len;
943 }
944
945 expand_cb_ga = &ga;
946 expand_cb_args = args;
947
948 func(args, func_params, expand_set_opt_callback_cb);
949
950 expand_cb_ga = NULL;
951 expand_cb_args = NULL;
952
953 *matches = ga.ga_data;
954 *numMatches = ga.ga_len;
955 return OK;
956}
Christian Brabandt9960ebc2023-10-05 22:17:09 +0200957#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200958
959/*
960 * Expand an option which is a list of flags.
961 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200962 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200963expand_set_opt_listflag(
964 optexpand_T *args,
965 char_u *flags,
966 int *numMatches,
967 char_u ***matches)
968{
969 char_u *p;
970 char_u *option_val = args->oe_opt_value;
971 char_u *cmdline_val = args->oe_set_arg;
972 int append = args->oe_append;
973 int include_orig_val = args->oe_include_orig_val && (*option_val != NUL);
974
Yee Cheng Chin6d113472023-10-02 21:38:39 +0200975 size_t num_flags = STRLEN(flags);
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200976
977 // Assume we only have small number of flags, so just allocate max size.
978 *matches = ALLOC_MULT(char_u *, num_flags + 1);
979 if (*matches == NULL)
980 return FAIL;
981
982 int count = 0;
983
984 if (include_orig_val)
985 {
986 p = vim_strsave(option_val);
987 if (p == NULL)
988 {
989 VIM_CLEAR(*matches);
990 return FAIL;
991 }
992 (*matches)[count++] = p;
993 }
994
995 for (char_u *flag = flags; *flag != NUL; flag++)
996 {
997 if (append && vim_strchr(option_val, *flag) != NULL)
998 continue;
999
1000 if (vim_strchr(cmdline_val, *flag) == NULL)
1001 {
1002 if (include_orig_val
1003 && option_val[1] == NUL
1004 && *flag == option_val[0])
1005 {
1006 // This value is already used as the first choice as it's the
1007 // existing flag. Just skip it to avoid duplicate.
1008 continue;
1009 }
1010 p = vim_strnsave(flag, 1);
1011 if (p == NULL)
1012 {
1013 if (count == 0)
1014 {
1015 VIM_CLEAR(*matches);
1016 return FAIL;
1017 }
1018 else
1019 break;
1020 }
1021 (*matches)[count++] = p;
1022 }
1023 }
1024
1025 if (count == 0)
1026 {
1027 VIM_CLEAR(*matches);
1028 return FAIL;
1029 }
1030 *numMatches = count;
1031 return OK;
1032}
1033
1034/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001035 * The 'ambiwidth' option is changed.
1036 */
1037 char *
1038did_set_ambiwidth(optset_T *args UNUSED)
1039{
1040 if (check_opt_strings(p_ambw, p_ambw_values, FALSE) != OK)
1041 return e_invalid_argument;
1042
1043 return check_chars_options();
1044}
1045
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001046 int
1047expand_set_ambiwidth(optexpand_T *args, int *numMatches, char_u ***matches)
1048{
1049 return expand_set_opt_string(
1050 args,
1051 p_ambw_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001052 ARRAY_LENGTH(p_ambw_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001053 numMatches,
1054 matches);
1055}
1056
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001057/*
1058 * The 'background' option is changed.
1059 */
1060 char *
Gregory Anders83ad2722024-01-03 19:48:51 +01001061did_set_background(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001062{
1063 if (check_opt_strings(p_bg, p_bg_values, FALSE) == FAIL)
1064 return e_invalid_argument;
1065
Gregory Anders83ad2722024-01-03 19:48:51 +01001066 if (args->os_oldval.string != NULL && args->os_oldval.string[0] == *p_bg)
1067 // Value was not changed
1068 return NULL;
1069
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001070#ifdef FEAT_EVAL
1071 int dark = (*p_bg == 'd');
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001072#endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001073
1074 init_highlight(FALSE, FALSE);
1075
1076#ifdef FEAT_EVAL
1077 if (dark != (*p_bg == 'd')
1078 && get_var_value((char_u *)"g:colors_name") != NULL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001079 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001080 // The color scheme must have set 'background' back to another
1081 // value, that's not what we want here. Disable the color
1082 // scheme and set the colors again.
1083 do_unlet((char_u *)"g:colors_name", TRUE);
1084 free_string_option(p_bg);
1085 p_bg = vim_strsave((char_u *)(dark ? "dark" : "light"));
1086 check_string_option(&p_bg);
1087 init_highlight(FALSE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001088 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001089#endif
1090#ifdef FEAT_TERMINAL
1091 term_update_colors_all();
1092#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001093
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001094 return NULL;
1095}
1096
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001097 int
1098expand_set_background(optexpand_T *args, int *numMatches, char_u ***matches)
1099{
1100 return expand_set_opt_string(
1101 args,
1102 p_bg_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001103 ARRAY_LENGTH(p_bg_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001104 numMatches,
1105 matches);
1106}
1107
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001108/*
1109 * The 'backspace' option is changed.
1110 */
1111 char *
1112did_set_backspace(optset_T *args UNUSED)
1113{
1114 if (VIM_ISDIGIT(*p_bs))
1115 {
1116 if (*p_bs > '3' || p_bs[1] != NUL)
1117 return e_invalid_argument;
1118 }
1119 else if (check_opt_strings(p_bs, p_bs_values, TRUE) != OK)
1120 return e_invalid_argument;
1121
1122 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001123}
1124
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001125 int
1126expand_set_backspace(optexpand_T *args, int *numMatches, char_u ***matches)
1127{
1128 return expand_set_opt_string(
1129 args,
1130 p_bs_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001131 ARRAY_LENGTH(p_bs_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001132 numMatches,
1133 matches);
1134}
1135
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001136/*
1137 * The 'backupcopy' option is changed.
1138 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001139 char *
1140did_set_backupcopy(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001141{
1142 char_u *bkc = p_bkc;
1143 unsigned int *flags = &bkc_flags;
1144 char *errmsg = NULL;
1145
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001146 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001147 {
1148 bkc = curbuf->b_p_bkc;
1149 flags = &curbuf->b_bkc_flags;
1150 }
1151
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001152 if ((args->os_flags & OPT_LOCAL) && *bkc == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001153 // make the local value empty: use the global value
1154 *flags = 0;
1155 else
1156 {
1157 if (opt_strings_flags(bkc, p_bkc_values, flags, TRUE) != OK)
1158 errmsg = e_invalid_argument;
1159 if ((((int)*flags & BKC_AUTO) != 0)
1160 + (((int)*flags & BKC_YES) != 0)
1161 + (((int)*flags & BKC_NO) != 0) != 1)
1162 {
1163 // Must have exactly one of "auto", "yes" and "no".
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001164 (void)opt_strings_flags(args->os_oldval.string, p_bkc_values,
1165 flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001166 errmsg = e_invalid_argument;
1167 }
1168 }
1169
1170 return errmsg;
1171}
1172
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001173 int
1174expand_set_backupcopy(optexpand_T *args, int *numMatches, char_u ***matches)
1175{
1176 return expand_set_opt_string(
1177 args,
1178 p_bkc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001179 ARRAY_LENGTH(p_bkc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001180 numMatches,
1181 matches);
1182}
1183
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001184/*
1185 * The 'backupext' or the 'patchmode' option is changed.
1186 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001187 char *
1188did_set_backupext_or_patchmode(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001189{
1190 if (STRCMP(*p_bex == '.' ? p_bex + 1 : p_bex,
1191 *p_pm == '.' ? p_pm + 1 : p_pm) == 0)
1192 return e_backupext_and_patchmode_are_equal;
1193
1194 return NULL;
1195}
1196
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001197/*
1198 * The 'belloff' option is changed.
1199 */
1200 char *
1201did_set_belloff(optset_T *args UNUSED)
1202{
1203 return did_set_opt_flags(p_bo, p_bo_values, &bo_flags, TRUE);
1204}
1205
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001206 int
1207expand_set_belloff(optexpand_T *args, int *numMatches, char_u ***matches)
1208{
1209 return expand_set_opt_string(
1210 args,
1211 p_bo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001212 ARRAY_LENGTH(p_bo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001213 numMatches,
1214 matches);
1215}
1216
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001217#if defined(FEAT_LINEBREAK) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001218/*
zeertzjqeac3fdc2024-02-03 18:08:09 +01001219 * The 'breakat' option is changed.
1220 */
1221 char *
1222did_set_breakat(optset_T *args UNUSED)
1223{
1224 char_u *p;
1225 int i;
1226
1227 for (i = 0; i < 256; i++)
1228 breakat_flags[i] = FALSE;
1229
1230 if (p_breakat != NULL)
1231 for (p = p_breakat; *p; p++)
1232 breakat_flags[*p] = TRUE;
1233
1234 return NULL;
1235}
1236
1237/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001238 * The 'breakindentopt' option is changed.
1239 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001240 char *
Millyb38700a2024-10-22 22:59:39 +02001241did_set_breakindentopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001242{
Millyb38700a2024-10-22 22:59:39 +02001243 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001244
Millyb38700a2024-10-22 22:59:39 +02001245 if (briopt_check(*varp, varp == &curwin->w_p_briopt ? curwin : NULL)
1246 == FAIL)
1247 return e_invalid_argument;
1248
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001249 // list setting requires a redraw
Millyb38700a2024-10-22 22:59:39 +02001250 if (varp == &curwin->w_p_briopt && curwin->w_briopt_list)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001251 redraw_all_later(UPD_NOT_VALID);
1252
Millyb38700a2024-10-22 22:59:39 +02001253 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001254}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001255
1256 int
1257expand_set_breakindentopt(optexpand_T *args, int *numMatches, char_u ***matches)
1258{
1259 return expand_set_opt_string(
1260 args,
1261 p_briopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001262 ARRAY_LENGTH(p_briopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001263 numMatches,
1264 matches);
1265}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001266#endif
1267
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001268#if defined(FEAT_BROWSE) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001269/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001270 * The 'browsedir' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001271 */
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00001272 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001273did_set_browsedir(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001274{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001275 if (check_opt_strings(p_bsdir, p_bsdir_values, FALSE) != OK
1276 && !mch_isdir(p_bsdir))
1277 return e_invalid_argument;
1278
1279 return NULL;
1280}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001281
1282 int
1283expand_set_browsedir(optexpand_T *args, int *numMatches, char_u ***matches)
1284{
1285 return expand_set_opt_string(
1286 args,
1287 p_bsdir_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001288 ARRAY_LENGTH(p_bsdir_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001289 numMatches,
1290 matches);
1291}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001292#endif
1293
1294/*
1295 * The 'bufhidden' option is changed.
1296 */
1297 char *
1298did_set_bufhidden(optset_T *args UNUSED)
1299{
1300 return did_set_opt_strings(curbuf->b_p_bh, p_bufhidden_values, FALSE);
1301}
1302
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001303 int
1304expand_set_bufhidden(optexpand_T *args, int *numMatches, char_u ***matches)
1305{
1306 return expand_set_opt_string(
1307 args,
1308 p_bufhidden_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001309 ARRAY_LENGTH(p_bufhidden_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001310 numMatches,
1311 matches);
1312}
1313
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001314/*
1315 * The 'buftype' option is changed.
1316 */
1317 char *
1318did_set_buftype(optset_T *args UNUSED)
1319{
1320 if (check_opt_strings(curbuf->b_p_bt, p_buftype_values, FALSE) != OK)
1321 return e_invalid_argument;
1322
1323 if (curwin->w_status_height)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001324 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001325 curwin->w_redr_status = TRUE;
1326 redraw_later(UPD_VALID);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001327 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001328 curbuf->b_help = (curbuf->b_p_bt[0] == 'h');
1329 redraw_titles();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001330
1331 return NULL;
1332}
1333
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001334 int
1335expand_set_buftype(optexpand_T *args, int *numMatches, char_u ***matches)
1336{
1337 return expand_set_opt_string(
1338 args,
1339 p_buftype_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001340 ARRAY_LENGTH(p_buftype_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001341 numMatches,
1342 matches);
1343}
1344
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001345/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001346 * The 'casemap' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001347 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001348 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001349did_set_casemap(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001350{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001351 return did_set_opt_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
1352}
1353
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001354 int
1355expand_set_casemap(optexpand_T *args, int *numMatches, char_u ***matches)
1356{
1357 return expand_set_opt_string(
1358 args,
1359 p_cmp_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001360 ARRAY_LENGTH(p_cmp_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001361 numMatches,
1362 matches);
1363}
1364
1365#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1366 int
1367expand_set_clipboard(optexpand_T *args, int *numMatches, char_u ***matches)
1368{
1369 return expand_set_opt_string(
1370 args,
1371 p_cb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001372 ARRAY_LENGTH(p_cb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001373 numMatches,
1374 matches);
1375}
1376#endif
1377
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001378/*
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001379 * The global 'listchars' or 'fillchars' option is changed.
1380 */
1381 static char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01001382did_set_global_listfillchars(char_u *val, int opt_lcs, int opt_flags,
1383 char *errbuf, size_t errbuflen)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001384{
1385 char *errmsg = NULL;
1386 char_u **local_ptr = opt_lcs ? &curwin->w_p_lcs : &curwin->w_p_fcs;
1387
1388 // only apply the global value to "curwin" when it does not have a
1389 // local value
1390 if (opt_lcs)
1391 errmsg = set_listchars_option(curwin, val,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001392 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL),
1393 errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001394 else
1395 errmsg = set_fillchars_option(curwin, val,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001396 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL),
1397 errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001398 if (errmsg != NULL)
1399 return errmsg;
1400
1401 tabpage_T *tp;
1402 win_T *wp;
1403
1404 // If the current window is set to use the global
1405 // 'listchars'/'fillchars' value, clear the window-local value.
1406 if (!(opt_flags & OPT_GLOBAL))
1407 clear_string_option(local_ptr);
1408 FOR_ALL_TAB_WINDOWS(tp, wp)
1409 {
1410 // If the current window has a local value need to apply it
1411 // again, it was changed when setting the global value.
1412 // If no error was returned above, we don't expect an error
1413 // here, so ignore the return value.
1414 if (opt_lcs)
1415 {
1416 if (*wp->w_p_lcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001417 (void)set_listchars_option(wp, wp->w_p_lcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001418 }
1419 else
1420 {
1421 if (*wp->w_p_fcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001422 (void)set_fillchars_option(wp, wp->w_p_fcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001423 }
1424 }
1425
1426 redraw_all_later(UPD_NOT_VALID);
1427
1428 return NULL;
1429}
1430
1431/*
1432 * The 'fillchars' option or the 'listchars' option is changed.
1433 */
1434 char *
1435did_set_chars_option(optset_T *args)
1436{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001437 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001438 char *errmsg = NULL;
1439
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001440 if ( varp == &p_lcs // global 'listchars'
1441 || varp == &p_fcs) // global 'fillchars'
1442 errmsg = did_set_global_listfillchars(*varp, varp == &p_lcs,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001443 args->os_flags, args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001444 else if (varp == &curwin->w_p_lcs) // local 'listchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001445 errmsg = set_listchars_option(curwin, *varp, TRUE,
1446 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001447 else if (varp == &curwin->w_p_fcs) // local 'fillchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001448 errmsg = set_fillchars_option(curwin, *varp, TRUE,
1449 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001450
1451 return errmsg;
1452}
1453
1454/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001455 * Expand 'fillchars' or 'listchars' option value.
1456 */
1457 int
1458expand_set_chars_option(optexpand_T *args, int *numMatches, char_u ***matches)
1459{
1460 char_u **varp = (char_u **)args->oe_varp;
1461 int is_lcs = (varp == &p_lcs || varp == &curwin->w_p_lcs);
1462 return expand_set_opt_generic(
1463 args,
1464 is_lcs ? get_listchars_name : get_fillchars_name,
1465 numMatches,
1466 matches);
1467}
1468
1469/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001470 * The 'cinoptions' option is changed.
1471 */
1472 char *
1473did_set_cinoptions(optset_T *args UNUSED)
1474{
1475 // TODO: recognize errors
1476 parse_cino(curbuf);
1477
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001478 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001479}
1480
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00001481#if defined(FEAT_SYN_HL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001482/*
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001483 * The 'colorcolumn' option is changed.
1484 */
1485 char *
Millya441a3e2024-10-22 22:43:01 +02001486did_set_colorcolumn(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001487{
Millya441a3e2024-10-22 22:43:01 +02001488 char_u **varp = (char_u **)args->os_varp;
1489
1490 return check_colorcolumn(*varp, varp == &curwin->w_p_cc ? curwin : NULL);
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001491}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001492#endif
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001493
1494/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001495 * The 'comments' option is changed.
1496 */
1497 char *
1498did_set_comments(optset_T *args)
1499{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001500 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001501 char_u *s;
1502 char *errmsg = NULL;
1503
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001504 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001505 {
1506 while (*s && *s != ':')
1507 {
1508 if (vim_strchr((char_u *)COM_ALL, *s) == NULL
1509 && !VIM_ISDIGIT(*s) && *s != '-')
1510 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01001511 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001512 break;
1513 }
1514 ++s;
1515 }
1516 if (*s++ == NUL)
1517 errmsg = e_missing_colon;
1518 else if (*s == ',' || *s == NUL)
1519 errmsg = e_zero_length_string;
1520 if (errmsg != NULL)
1521 break;
1522 while (*s && *s != ',')
1523 {
1524 if (*s == '\\' && s[1] != NUL)
1525 ++s;
1526 ++s;
1527 }
1528 s = skip_to_option_part(s);
1529 }
1530
1531 return errmsg;
1532}
1533
1534#if defined(FEAT_FOLDING) || defined(PROTO)
1535/*
1536 * The 'commentstring' option is changed.
1537 */
1538 char *
1539did_set_commentstring(optset_T *args)
1540{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001541 char_u **varp = (char_u **)args->os_varp;
1542
1543 if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001544 return e_commentstring_must_be_empty_or_contain_str;
1545
1546 return NULL;
1547}
1548#endif
1549
1550/*
1551 * The 'complete' option is changed.
1552 */
1553 char *
1554did_set_complete(optset_T *args)
1555{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001556 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001557 char_u *s;
1558
1559 // check if it is a valid value for 'complete' -- Acevedo
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001560 for (s = *varp; *s;)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001561 {
1562 while (*s == ',' || *s == ' ')
1563 s++;
1564 if (!*s)
1565 break;
1566 if (vim_strchr((char_u *)".wbuksid]tU", *s) == NULL)
Christian Brabandtb39b2402023-11-29 11:34:05 +01001567 return illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001568 if (*++s != NUL && *s != ',' && *s != ' ')
1569 {
1570 if (s[-1] == 'k' || s[-1] == 's')
1571 {
1572 // skip optional filename after 'k' and 's'
1573 while (*s && *s != ',' && *s != ' ')
1574 {
1575 if (*s == '\\' && s[1] != NUL)
1576 ++s;
1577 ++s;
1578 }
1579 }
1580 else
1581 {
1582 if (args->os_errbuf != NULL)
1583 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01001584 vim_snprintf((char *)args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001585 _(e_illegal_character_after_chr), *--s);
1586 return args->os_errbuf;
1587 }
1588 return "";
1589 }
1590 }
1591 }
1592
1593 return NULL;
1594}
1595
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001596 int
1597expand_set_complete(optexpand_T *args, int *numMatches, char_u ***matches)
1598{
1599 static char *(p_cpt_values[]) = {
1600 ".", "w", "b", "u", "k", "kspell", "s", "i", "d", "]", "t", "U",
1601 NULL};
1602 return expand_set_opt_string(
1603 args,
1604 p_cpt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001605 ARRAY_LENGTH(p_cpt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001606 numMatches,
1607 matches);
1608}
1609
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001610/*
1611 * The 'completeopt' option is changed.
1612 */
1613 char *
1614did_set_completeopt(optset_T *args UNUSED)
1615{
zeertzjq529b9ad2024-06-05 20:27:06 +02001616 char_u *cot = p_cot;
1617 unsigned *flags = &cot_flags;
1618
1619 if (args->os_flags & OPT_LOCAL)
1620 {
1621 cot = curbuf->b_p_cot;
1622 flags = &curbuf->b_cot_flags;
1623 }
1624
1625 if (check_opt_strings(cot, p_cot_values, TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001626 return e_invalid_argument;
1627
zeertzjq529b9ad2024-06-05 20:27:06 +02001628 if (opt_strings_flags(cot, p_cot_values, flags, TRUE) != OK)
1629 return e_invalid_argument;
1630
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001631 return NULL;
1632}
1633
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001634 int
1635expand_set_completeopt(optexpand_T *args, int *numMatches, char_u ***matches)
1636{
1637 return expand_set_opt_string(
1638 args,
1639 p_cot_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001640 ARRAY_LENGTH(p_cot_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001641 numMatches,
1642 matches);
1643}
1644
glepnir6a89c942024-10-01 20:32:12 +02001645/*
1646 * The 'completeitemalign' option is changed.
1647 */
1648 char *
1649did_set_completeitemalign(optset_T *args UNUSED)
1650{
1651 char_u *p = p_cia;
1652 unsigned new_cia_flags = 0;
1653 int seen[3] = { FALSE, FALSE, FALSE };
1654 int count = 0;
1655 char_u buf[10];
1656
1657 while (*p)
1658 {
1659 copy_option_part(&p, buf, sizeof(buf), ",");
1660 if (count >= 3)
1661 return e_invalid_argument;
1662
1663 if (STRCMP(buf, "abbr") == 0)
1664 {
1665 if (seen[CPT_ABBR])
1666 return e_invalid_argument;
1667 new_cia_flags = new_cia_flags * 10 + CPT_ABBR;
1668 seen[CPT_ABBR] = TRUE;
1669 count++;
1670 }
1671 else if (STRCMP(buf, "kind") == 0)
1672 {
1673 if (seen[CPT_KIND])
1674 return e_invalid_argument;
1675 new_cia_flags = new_cia_flags * 10 + CPT_KIND;
1676 seen[CPT_KIND] = TRUE;
1677 count++;
1678 }
1679 else if (STRCMP(buf, "menu") == 0)
1680 {
1681 if (seen[CPT_MENU])
1682 return e_invalid_argument;
1683 new_cia_flags = new_cia_flags * 10 + CPT_MENU;
1684 seen[CPT_MENU] = TRUE;
1685 count++;
1686 }
1687 else
1688 return e_invalid_argument;
1689 }
1690 if (new_cia_flags == 0 || count != 3)
1691 return e_invalid_argument;
1692
1693 cia_flags = new_cia_flags;
1694 return NULL;
1695}
1696
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001697#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1698/*
1699 * The 'completepopup' option is changed.
1700 */
1701 char *
1702did_set_completepopup(optset_T *args UNUSED)
1703{
1704 if (parse_completepopup(NULL) == FAIL)
1705 return e_invalid_argument;
1706
1707 popup_close_info();
1708 return NULL;
1709}
1710#endif
1711
1712#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1713/*
1714 * The 'completeslash' option is changed.
1715 */
1716 char *
1717did_set_completeslash(optset_T *args UNUSED)
1718{
1719 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1720 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
1721 return e_invalid_argument;
1722
1723 return NULL;
1724}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001725
1726 int
1727expand_set_completeslash(optexpand_T *args, int *numMatches, char_u ***matches)
1728{
1729 return expand_set_opt_string(
1730 args,
1731 p_csl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001732 ARRAY_LENGTH(p_csl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001733 numMatches,
1734 matches);
1735}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001736#endif
1737
1738#if defined(FEAT_CONCEAL) || defined(PROTO)
1739/*
1740 * The 'concealcursor' option is changed.
1741 */
1742 char *
1743did_set_concealcursor(optset_T *args)
1744{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001745 char_u **varp = (char_u **)args->os_varp;
1746
Christian Brabandtb39b2402023-11-29 11:34:05 +01001747 return did_set_option_listflag(*varp, (char_u *)COCU_ALL, args->os_errbuf,
1748 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001749}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001750
1751 int
1752expand_set_concealcursor(optexpand_T *args, int *numMatches, char_u ***matches)
1753{
1754 return expand_set_opt_listflag(args, (char_u*)COCU_ALL, numMatches, matches);
1755}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001756#endif
1757
1758/*
1759 * The 'cpoptions' option is changed.
1760 */
1761 char *
1762did_set_cpoptions(optset_T *args)
1763{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001764 char_u **varp = (char_u **)args->os_varp;
1765
Christian Brabandtb39b2402023-11-29 11:34:05 +01001766 return did_set_option_listflag(*varp, (char_u *)CPO_ALL, args->os_errbuf,
1767 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001768}
1769
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001770 int
1771expand_set_cpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
1772{
1773 return expand_set_opt_listflag(args, (char_u*)CPO_ALL, numMatches, matches);
1774}
1775
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001776#if defined(FEAT_CRYPT) || defined(PROTO)
1777/*
1778 * The 'cryptkey' option is changed.
1779 */
1780 char *
1781did_set_cryptkey(optset_T *args)
1782{
1783 // Make sure the ":set" command doesn't show the new value in the
1784 // history.
1785 remove_key_from_history();
1786
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02001787 if (args->os_op != OP_NONE)
1788 // Don't allow set+=/-=/^= as they can allow for substring guessing
1789 return e_invalid_argument;
1790
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001791 if (STRCMP(curbuf->b_p_key, args->os_oldval.string) != 0)
1792 {
1793 // Need to update the swapfile.
1794 ml_set_crypt_key(curbuf, args->os_oldval.string,
1795 *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
1796 changed_internal();
1797 }
Christian Brabandt19e6c4f2023-06-27 18:57:10 +01001798# ifdef FEAT_SODIUM
1799 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
1800 crypt_sodium_lock_key(args->os_newval.string);
1801# endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001802
1803 return NULL;
1804}
1805
1806/*
1807 * The 'cryptmethod' option is changed.
1808 */
1809 char *
1810did_set_cryptmethod(optset_T *args)
1811{
1812 char_u *p;
1813 char_u *s;
1814
1815 if (args->os_flags & OPT_LOCAL)
1816 p = curbuf->b_p_cm;
1817 else
1818 p = p_cm;
1819 if (check_opt_strings(p, p_cm_values, TRUE) != OK)
1820 return e_invalid_argument;
1821 else if (crypt_self_test() == FAIL)
1822 return e_invalid_argument;
1823
1824 // When setting the global value to empty, make it "zip".
1825 if (*p_cm == NUL)
1826 {
1827 free_string_option(p_cm);
1828 p_cm = vim_strsave((char_u *)"zip");
1829 }
1830 // When using ":set cm=name" the local value is going to be empty.
1831 // Do that here, otherwise the crypt functions will still use the
1832 // local value.
1833 if ((args->os_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1834 {
1835 free_string_option(curbuf->b_p_cm);
1836 curbuf->b_p_cm = empty_option;
1837 }
1838
1839 // Need to update the swapfile when the effective method changed.
1840 // Set "s" to the effective old value, "p" to the effective new
1841 // method and compare.
1842 if ((args->os_flags & OPT_LOCAL) && *args->os_oldval.string == NUL)
1843 s = p_cm; // was previously using the global value
1844 else
1845 s = args->os_oldval.string;
1846 if (*curbuf->b_p_cm == NUL)
1847 p = p_cm; // is now using the global value
1848 else
1849 p = curbuf->b_p_cm;
1850 if (STRCMP(s, p) != 0)
1851 ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1852
1853 // If the global value changes need to update the swapfile for all
1854 // buffers using that value.
1855 if ((args->os_flags & OPT_GLOBAL)
1856 && STRCMP(p_cm, args->os_oldval.string) != 0)
1857 {
1858 buf_T *buf;
1859
1860 FOR_ALL_BUFFERS(buf)
1861 if (buf != curbuf && *buf->b_p_cm == NUL)
1862 ml_set_crypt_key(buf, buf->b_p_key, args->os_oldval.string);
1863 }
1864 return NULL;
1865}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001866
1867 int
1868expand_set_cryptmethod(optexpand_T *args, int *numMatches, char_u ***matches)
1869{
1870 return expand_set_opt_string(
1871 args,
1872 p_cm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001873 ARRAY_LENGTH(p_cm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001874 numMatches,
1875 matches);
1876}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001877#endif
1878
1879#if (defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1880/*
1881 * The 'cscopequickfix' option is changed.
1882 */
1883 char *
1884did_set_cscopequickfix(optset_T *args UNUSED)
1885{
1886 char_u *p;
1887
1888 if (p_csqf == NULL)
1889 return NULL;
1890
1891 p = p_csqf;
1892 while (*p != NUL)
1893 {
1894 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
1895 || p[1] == NUL
1896 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
1897 || (p[2] != NUL && p[2] != ','))
1898 return e_invalid_argument;
1899 else if (p[2] == NUL)
1900 break;
1901 else
1902 p += 3;
1903 }
1904
1905 return NULL;
1906}
1907#endif
1908
1909#if defined(FEAT_SYN_HL) || defined(PROTO)
1910/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001911 * The 'cursorlineopt' option is changed.
1912 */
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001913 char *
1914did_set_cursorlineopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001915{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001916 char_u **varp = (char_u **)args->os_varp;
1917
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001918 // This could be changed to use opt_strings_flags() instead.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001919 if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001920 return e_invalid_argument;
1921
1922 return NULL;
1923}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001924
1925 int
1926expand_set_cursorlineopt(optexpand_T *args, int *numMatches, char_u ***matches)
1927{
1928 return expand_set_opt_string(
1929 args,
1930 p_culopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001931 ARRAY_LENGTH(p_culopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001932 numMatches,
1933 matches);
1934}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001935#endif
1936
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001937/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001938 * The 'debug' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001939 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001940 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001941did_set_debug(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001942{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001943 return did_set_opt_strings(p_debug, p_debug_values, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001944}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001945
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001946 int
1947expand_set_debug(optexpand_T *args, int *numMatches, char_u ***matches)
1948{
1949 return expand_set_opt_string(
1950 args,
1951 p_debug_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001952 ARRAY_LENGTH(p_debug_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001953 numMatches,
1954 matches);
1955}
1956
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001957#if defined(FEAT_DIFF) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001958/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001959 * The 'diffopt' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001960 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001961 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001962did_set_diffopt(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001963{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001964 if (diffopt_changed() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001965 return e_invalid_argument;
1966
1967 return NULL;
1968}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001969
1970 int
1971expand_set_diffopt(optexpand_T *args, int *numMatches, char_u ***matches)
1972{
1973 expand_T *xp = args->oe_xp;
1974
1975 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
1976 {
1977 // Within "algorithm:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001978 int algo_len = (int)STRLEN("algorithm:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001979 if (xp->xp_pattern - args->oe_set_arg >= algo_len &&
1980 STRNCMP(xp->xp_pattern - algo_len, "algorithm:", algo_len) == 0)
1981 {
1982 return expand_set_opt_string(
1983 args,
1984 p_dip_algorithm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001985 ARRAY_LENGTH(p_dip_algorithm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001986 numMatches,
1987 matches);
1988 }
1989 return FAIL;
1990 }
1991
1992 return expand_set_opt_string(
1993 args,
1994 p_dip_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001995 ARRAY_LENGTH(p_dip_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001996 numMatches,
1997 matches);
1998}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001999#endif
2000
2001/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002002 * The 'display' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002003 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002004 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002005did_set_display(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002006{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002007 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002008 return e_invalid_argument;
2009
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002010 (void)init_chartab();
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002011 return NULL;
2012}
2013
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002014 int
2015expand_set_display(optexpand_T *args, int *numMatches, char_u ***matches)
2016{
2017 return expand_set_opt_string(
2018 args,
2019 p_dy_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002020 ARRAY_LENGTH(p_dy_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002021 numMatches,
2022 matches);
2023}
2024
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002025/*
2026 * The 'eadirection' option is changed.
2027 */
2028 char *
2029did_set_eadirection(optset_T *args UNUSED)
2030{
2031 return did_set_opt_strings(p_ead, p_ead_values, FALSE);
2032}
2033
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002034 int
2035expand_set_eadirection(optexpand_T *args, int *numMatches, char_u ***matches)
2036{
2037 return expand_set_opt_string(
2038 args,
2039 p_ead_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002040 ARRAY_LENGTH(p_ead_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002041 numMatches,
2042 matches);
2043}
2044
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002045/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002046 * One of the 'encoding', 'fileencoding', 'termencoding' or 'makeencoding'
2047 * options is changed.
2048 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002049 char *
2050did_set_encoding(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002051{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002052 char_u **varp = (char_u **)args->os_varp;
2053 char_u **gvarp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002054 char *errmsg = NULL;
2055 char_u *p;
2056
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002057 // Get the global option to compare with, otherwise we would have to check
2058 // two values for all local options.
2059 gvarp = (char_u **)get_option_varp_scope(args->os_idx, OPT_GLOBAL);
2060
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002061 if (gvarp == &p_fenc)
2062 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002063 if (!curbuf->b_p_ma && args->os_flags != OPT_GLOBAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002064 errmsg = e_cannot_make_changes_modifiable_is_off;
2065 else if (vim_strchr(*varp, ',') != NULL)
2066 // No comma allowed in 'fileencoding'; catches confusing it
2067 // with 'fileencodings'.
2068 errmsg = e_invalid_argument;
2069 else
2070 {
2071 // May show a "+" in the title now.
2072 redraw_titles();
2073 // Add 'fileencoding' to the swap file.
2074 ml_setflags(curbuf);
2075 }
2076 }
2077 if (errmsg == NULL)
2078 {
2079 // canonize the value, so that STRCMP() can be used on it
2080 p = enc_canonize(*varp);
2081 if (p != NULL)
2082 {
2083 vim_free(*varp);
2084 *varp = p;
2085 }
2086 if (varp == &p_enc)
2087 {
2088 errmsg = mb_init();
2089 redraw_titles();
2090 }
2091 }
2092
2093#if defined(FEAT_GUI_GTK)
2094 if (errmsg == NULL && varp == &p_tenc && gui.in_use)
2095 {
2096 // GTK uses only a single encoding, and that is UTF-8.
2097 if (STRCMP(p_tenc, "utf-8") != 0)
2098 errmsg = e_cannot_be_changed_in_gtk_GUI;
2099 }
2100#endif
2101
2102 if (errmsg == NULL)
2103 {
2104#ifdef FEAT_KEYMAP
2105 // When 'keymap' is used and 'encoding' changes, reload the keymap
2106 // (with another encoding).
2107 if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
2108 (void)keymap_init();
2109#endif
2110
2111 // When 'termencoding' is not empty and 'encoding' changes or when
2112 // 'termencoding' changes, need to setup for keyboard input and
2113 // display output conversion.
2114 if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
2115 {
2116 if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
2117 || convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
2118 {
2119 semsg(_(e_cannot_convert_between_str_and_str),
2120 p_tenc, p_enc);
2121 errmsg = e_invalid_argument;
2122 }
2123 }
2124
2125#if defined(MSWIN)
K.Takatace3189d2023-02-15 19:13:43 +00002126 // $HOME, $VIM and $VIMRUNTIME may have characters in active code page.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002127 if (varp == &p_enc)
K.Takatace3189d2023-02-15 19:13:43 +00002128 {
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002129 init_homedir();
K.Takatace3189d2023-02-15 19:13:43 +00002130 init_vimdir();
2131 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002132#endif
2133 }
2134
2135 return errmsg;
2136}
2137
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002138 int
2139expand_set_encoding(optexpand_T *args, int *numMatches, char_u ***matches)
2140{
2141 return expand_set_opt_generic(
2142 args,
2143 get_encoding_name,
2144 numMatches,
2145 matches);
2146}
2147
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002148/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002149 * The 'eventignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002150 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002151 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002152did_set_eventignore(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002153{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002154 if (check_ei() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002155 return e_invalid_argument;
2156 return NULL;
2157}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002158
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002159 static char_u *
2160get_eventignore_name(expand_T *xp, int idx)
2161{
2162 // 'eventignore' allows special keyword "all" in addition to
2163 // all event names.
2164 if (idx == 0)
2165 return (char_u *)"all";
2166 return get_event_name_no_group(xp, idx - 1);
2167}
2168
2169 int
2170expand_set_eventignore(optexpand_T *args, int *numMatches, char_u ***matches)
2171{
2172 return expand_set_opt_generic(
2173 args,
2174 get_eventignore_name,
2175 numMatches,
2176 matches);
2177}
2178
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002179/*
2180 * The 'fileformat' option is changed.
2181 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002182 char *
2183did_set_fileformat(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002184{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002185 char_u **varp = (char_u **)args->os_varp;
2186
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002187 if (!curbuf->b_p_ma && !(args->os_flags & OPT_GLOBAL))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002188 return e_cannot_make_changes_modifiable_is_off;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002189 else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002190 return e_invalid_argument;
2191
2192 // may also change 'textmode'
2193 if (get_fileformat(curbuf) == EOL_DOS)
2194 curbuf->b_p_tx = TRUE;
2195 else
2196 curbuf->b_p_tx = FALSE;
2197 redraw_titles();
2198 // update flag in swap file
2199 ml_setflags(curbuf);
2200 // Redraw needed when switching to/from "mac": a CR in the text
2201 // will be displayed differently.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002202 if (get_fileformat(curbuf) == EOL_MAC || *args->os_oldval.string == 'm')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002203 redraw_curbuf_later(UPD_NOT_VALID);
2204
2205 return NULL;
2206}
2207
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002208 int
2209expand_set_fileformat(optexpand_T *args, int *numMatches, char_u ***matches)
2210{
2211 return expand_set_opt_string(
2212 args,
2213 p_ff_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002214 ARRAY_LENGTH(p_ff_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002215 numMatches,
2216 matches);
2217}
2218
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002219/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002220 * Function given to ExpandGeneric() to obtain the possible arguments of the
2221 * fileformat options.
2222 */
2223 char_u *
2224get_fileformat_name(expand_T *xp UNUSED, int idx)
2225{
2226 if (idx >= (int)ARRAY_LENGTH(p_ff_values))
2227 return NULL;
2228
2229 return (char_u*)p_ff_values[idx];
2230}
2231
2232/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002233 * The 'fileformats' option is changed.
2234 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002235 char *
2236did_set_fileformats(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002237{
2238 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
2239 return e_invalid_argument;
2240
2241 // also change 'textauto'
2242 if (*p_ffs == NUL)
2243 p_ta = FALSE;
2244 else
2245 p_ta = TRUE;
2246
2247 return NULL;
2248}
2249
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002250/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002251 * The 'filetype' or the 'syntax' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002252 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002253 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002254did_set_filetype_or_syntax(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002255{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002256 char_u **varp = (char_u **)args->os_varp;
2257
2258 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002259 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002260
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002261 args->os_value_changed = STRCMP(args->os_oldval.string, *varp) != 0;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002262
2263 // Since we check the value, there is no need to set P_INSECURE,
2264 // even when the value comes from a modeline.
2265 args->os_value_checked = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002266
2267 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002268}
2269
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002270#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002271/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002272 * The 'foldclose' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002273 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002274 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002275did_set_foldclose(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002276{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002277 return did_set_opt_strings(p_fcl, p_fcl_values, TRUE);
2278}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002279
2280 int
2281expand_set_foldclose(optexpand_T *args, int *numMatches, char_u ***matches)
2282{
2283 return expand_set_opt_string(
2284 args,
2285 p_fcl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002286 ARRAY_LENGTH(p_fcl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002287 numMatches,
2288 matches);
2289}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002290#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002291
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002292#if (defined(FEAT_EVAL) && defined(FEAT_FOLDING)) || defined(PROTO)
2293/*
2294 * The 'foldexpr' option is changed.
2295 */
2296 char *
2297did_set_foldexpr(optset_T *args)
2298{
2299 (void)did_set_optexpr(args);
2300 if (foldmethodIsExpr(curwin))
2301 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002302 return NULL;
2303}
2304#endif
2305
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002306#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002307/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002308 * The 'foldignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002309 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002310 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002311did_set_foldignore(optset_T *args UNUSED)
2312{
2313 if (foldmethodIsIndent(curwin))
2314 foldUpdateAll(curwin);
2315 return NULL;
2316}
2317
2318/*
2319 * The 'foldmarker' option is changed.
2320 */
2321 char *
2322did_set_foldmarker(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002323{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002324 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002325 char_u *p;
2326
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002327 p = vim_strchr(*varp, ',');
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002328 if (p == NULL)
2329 return e_comma_required;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002330 else if (p == *varp || p[1] == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002331 return e_invalid_argument;
2332 else if (foldmethodIsMarker(curwin))
2333 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002334
2335 return NULL;
2336}
2337
2338/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002339 * The 'foldmethod' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002340 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002341 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002342did_set_foldmethod(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002343{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002344 char_u **varp = (char_u **)args->os_varp;
2345
Milly142cad12024-10-22 22:11:51 +02002346 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK || **varp == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002347 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002348
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002349 foldUpdateAll(curwin);
2350 if (foldmethodIsDiff(curwin))
2351 newFoldLevel();
2352 return NULL;
2353}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002354
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002355 int
2356expand_set_foldmethod(optexpand_T *args, int *numMatches, char_u ***matches)
2357{
2358 return expand_set_opt_string(
2359 args,
2360 p_fdm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002361 ARRAY_LENGTH(p_fdm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002362 numMatches,
2363 matches);
2364}
2365
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002366/*
2367 * The 'foldopen' option is changed.
2368 */
2369 char *
2370did_set_foldopen(optset_T *args UNUSED)
2371{
2372 return did_set_opt_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
2373}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002374
2375 int
2376expand_set_foldopen(optexpand_T *args, int *numMatches, char_u ***matches)
2377{
2378 return expand_set_opt_string(
2379 args,
2380 p_fdo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002381 ARRAY_LENGTH(p_fdo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002382 numMatches,
2383 matches);
2384}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002385#endif
2386
2387/*
2388 * The 'formatoptions' option is changed.
2389 */
2390 char *
2391did_set_formatoptions(optset_T *args)
2392{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002393 char_u **varp = (char_u **)args->os_varp;
2394
Christian Brabandtb39b2402023-11-29 11:34:05 +01002395 return did_set_option_listflag(*varp, (char_u *)FO_ALL, args->os_errbuf,
2396 args->os_errbuflen);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002397}
2398
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002399 int
2400expand_set_formatoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2401{
2402 return expand_set_opt_listflag(args, (char_u*)FO_ALL, numMatches, matches);
2403}
2404
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002405#if defined(CURSOR_SHAPE) || defined(PROTO)
2406/*
2407 * The 'guicursor' option is changed.
2408 */
2409 char *
2410did_set_guicursor(optset_T *args UNUSED)
2411{
2412 return parse_shape_opt(SHAPE_CURSOR);
2413}
2414#endif
2415
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002416#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002417/*
2418 * The 'guifont' option is changed.
2419 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002420 char *
2421did_set_guifont(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002422{
2423 char_u *p;
2424 char *errmsg = NULL;
2425
2426 if (gui.in_use)
2427 {
2428 p = p_guifont;
2429# if defined(FEAT_GUI_GTK)
2430 // Put up a font dialog and let the user select a new value.
2431 // If this is cancelled go back to the old value but don't
2432 // give an error message.
2433 if (STRCMP(p, "*") == 0)
2434 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002435 p = gui_mch_font_dialog(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002436 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002437 p_guifont = (p != NULL) ? p : vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002438 }
2439# endif
2440 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
2441 {
2442# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
2443 if (STRCMP(p_guifont, "*") == 0)
2444 {
2445 // Dialog was cancelled: Keep the old value without giving
2446 // an error message.
2447 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002448 p_guifont = vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002449 }
2450 else
2451# endif
2452 errmsg = e_invalid_fonts;
2453 }
2454 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002455
2456 return errmsg;
2457}
2458
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002459/*
2460 * Expand the 'guifont' option. Only when GUI is being used. Each platform has
2461 * specific behaviors.
2462 */
2463 int
2464expand_set_guifont(optexpand_T *args, int *numMatches, char_u ***matches)
2465{
2466 if (!gui.in_use)
2467 return FAIL;
2468
2469# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
2470 char_u **varp = (char_u **)args->oe_varp;
2471 int wide = (varp == &p_guifontwide);
2472
2473 return expand_set_opt_callback(
2474 args, gui_mch_expand_font, &wide, numMatches, matches);
2475# else
2476 return FAIL;
2477# endif
2478}
2479
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002480# if defined(FEAT_XFONTSET) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002481/*
2482 * The 'guifontset' option is changed.
2483 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002484 char *
2485did_set_guifontset(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002486{
2487 char *errmsg = NULL;
2488
2489 if (STRCMP(p_guifontset, "*") == 0)
2490 errmsg = e_cant_select_fontset;
2491 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
2492 errmsg = e_invalid_fontset;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002493
2494 return errmsg;
2495}
2496# endif
2497
2498/*
2499 * The 'guifontwide' option is changed.
2500 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002501 char *
2502did_set_guifontwide(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002503{
2504 char *errmsg = NULL;
2505
2506 if (STRCMP(p_guifontwide, "*") == 0)
2507 errmsg = e_cant_select_wide_font;
2508 else if (gui_get_wide_font() == FAIL)
2509 errmsg = e_invalid_wide_font;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002510
2511 return errmsg;
2512}
2513#endif
2514
Erik S. V. Jansson8b1e7492024-02-24 14:26:52 +01002515#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002516/*
2517 * The 'guiligatures' option is changed.
2518 */
2519 char *
2520did_set_guiligatures(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002521{
2522 gui_set_ligatures();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002523 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002524}
2525#endif
2526
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002527#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002528/*
2529 * The 'guioptions' option is changed.
2530 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002531 char *
2532did_set_guioptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002533{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002534 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002535 char *errmsg;
2536
Christian Brabandtb39b2402023-11-29 11:34:05 +01002537 errmsg = did_set_option_listflag(*varp, (char_u *)GO_ALL, args->os_errbuf,
2538 args->os_errbuflen);
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002539 if (errmsg != NULL)
2540 return errmsg;
2541
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002542 gui_init_which_components(args->os_oldval.string);
2543 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002544}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002545
2546 int
2547expand_set_guioptions(optexpand_T *args, int *numMatches, char_u ***matches)
2548{
2549 return expand_set_opt_listflag(args, (char_u*)GO_ALL, numMatches, matches);
2550}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002551#endif
2552
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002553#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002554/*
2555 * The 'guitablabel' option is changed.
2556 */
2557 char *
2558did_set_guitablabel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002559{
2560 redraw_tabline = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002561 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002562}
2563#endif
2564
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002565/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002566 * The 'helpfile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002567 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002568 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002569did_set_helpfile(optset_T *args UNUSED)
2570{
2571 // May compute new values for $VIM and $VIMRUNTIME
2572 if (didset_vim)
2573 vim_unsetenv_ext((char_u *)"VIM");
2574 if (didset_vimruntime)
2575 vim_unsetenv_ext((char_u *)"VIMRUNTIME");
2576 return NULL;
2577}
2578
2579#if defined(FEAT_MULTI_LANG) || defined(PROTO)
2580/*
2581 * The 'helplang' option is changed.
2582 */
2583 char *
2584did_set_helplang(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002585{
2586 char *errmsg = NULL;
2587
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002588 // Check for "", "ab", "ab,cd", etc.
2589 for (char_u *s = p_hlg; *s != NUL; s += 3)
2590 {
2591 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
2592 {
2593 errmsg = e_invalid_argument;
2594 break;
2595 }
2596 if (s[2] == NUL)
2597 break;
2598 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002599
2600 return errmsg;
2601}
2602#endif
2603
2604/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002605 * The 'highlight' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002606 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002607 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002608did_set_highlight(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002609{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002610 if (highlight_changed() == FAIL)
2611 return e_invalid_argument; // invalid flags
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002612
2613 return NULL;
2614}
2615
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002616/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002617 * Expand 'highlight' option.
2618 */
2619 int
2620expand_set_highlight(optexpand_T *args, int *numMatches, char_u ***matches)
2621{
2622 char_u *p;
2623 expand_T *xp = args->oe_xp;
2624 static char_u hl_flags[HLF_COUNT] = HL_FLAGS;
Christian Brabandt3f168ec2023-10-02 23:21:11 +02002625 size_t i;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002626 int count = 0;
2627
2628 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2629 {
2630 // Right after a ':', meaning we just return all highlight names.
2631 return expand_set_opt_generic(
2632 args,
2633 get_highlight_name,
2634 numMatches,
2635 matches);
2636 }
2637
2638 if (*xp->xp_pattern == NUL)
2639 {
2640 // At beginning of a comma-separated list. Return the specific list of
2641 // supported occasions.
2642 *matches = ALLOC_MULT(char_u *, HLF_COUNT + 1);
2643 if (*matches == NULL)
2644 return FAIL;
2645
2646 // We still want to return the full option if it's requested.
2647 if (args->oe_include_orig_val)
2648 {
2649 p = vim_strsave(args->oe_opt_value);
2650 if (p == NULL)
2651 {
2652 VIM_CLEAR(*matches);
2653 return FAIL;
2654 }
2655 (*matches)[count++] = p;
2656 }
2657
2658 for (i = 0; i < HLF_COUNT; i++)
2659 {
2660 p = vim_strnsave(&hl_flags[i], 1);
2661 if (p == NULL)
2662 {
2663 if (count == 0)
2664 {
2665 VIM_CLEAR(*matches);
2666 return FAIL;
2667 }
2668 else
2669 break;
2670 }
2671 (*matches)[count++] = p;
2672 }
2673
2674 if (count == 0)
2675 {
2676 VIM_CLEAR(*matches);
2677 return FAIL;
2678 }
2679 *numMatches = count;
2680 return OK;
2681 }
2682
2683 // We are after the initial character (which indicates the occasion). We
2684 // already made sure we are not matching after a ':' above, so now we want
2685 // to match against display mode modifiers.
2686 // Since the xp_pattern starts from the beginning, we need to include it in
2687 // the returned match.
2688
2689 // Note: Keep this in sync with highlight_changed()
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002690 static char_u p_hl_mode_values[] =
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002691 {':', 'b', 'i', '-', 'n', 'r', 's', 'u', 'c', '2', 'd', '=', 't'};
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002692 size_t num_hl_modes = ARRAY_LENGTH(p_hl_mode_values);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002693
2694 *matches = ALLOC_MULT(char_u *, num_hl_modes);
2695 if (*matches == NULL)
2696 return FAIL;
2697
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002698 int pattern_len = xp->xp_pattern_len;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002699
2700 for (i = 0; i < num_hl_modes; i++)
2701 {
2702 // Don't allow duplicates as these are unique flags
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002703 char_u *dup = vim_strchr(xp->xp_pattern + 1, p_hl_mode_values[i]);
2704 if (dup != NULL && (int)(dup - xp->xp_pattern) < pattern_len)
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002705 continue;
2706
2707 // ':' only works by itself, not with other flags.
2708 if (pattern_len > 1 && p_hl_mode_values[i] == ':')
2709 continue;
2710
2711 p = vim_strnsave(xp->xp_pattern, pattern_len + 1);
2712 if (p == NULL)
2713 {
2714 if (i == 0)
2715 {
2716 VIM_CLEAR(*matches);
2717 return FAIL;
2718 }
2719 else
2720 break;
2721 }
2722 p[pattern_len] = p_hl_mode_values[i];
2723 p[pattern_len + 1] = NUL;
2724 (*matches)[count++] = p;
2725 }
2726 if (count == 0)
2727 {
2728 VIM_CLEAR(*matches);
2729 return FAIL;
2730 }
2731 *numMatches = count;
2732
2733 return OK;
2734}
2735
2736/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002737 * The 'titlestring' or the 'iconstring' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002738 */
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002739 static char *
2740parse_titleiconstring(optset_T *args UNUSED, int flagval UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002741{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002742#ifdef FEAT_STL_OPT
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002743 char_u **varp = (char_u **)args->os_varp;
2744
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002745 // NULL => statusline syntax
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002746 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002747 stl_syntax |= flagval;
2748 else
2749 stl_syntax &= ~flagval;
2750#endif
2751 did_set_title();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002752
2753 return NULL;
2754}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002755
2756/*
2757 * The 'iconstring' option is changed.
2758 */
2759 char *
2760did_set_iconstring(optset_T *args)
2761{
2762 int flagval = 0;
2763
2764#ifdef FEAT_STL_OPT
2765 flagval = STL_IN_ICON;
2766#endif
2767
2768 return parse_titleiconstring(args, flagval);
2769}
2770
2771#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
2772/*
2773 * The 'imactivatekey' option is changed.
2774 */
2775 char *
2776did_set_imactivatekey(optset_T *args UNUSED)
2777{
2778 if (!im_xim_isvalid_imactivate())
2779 return e_invalid_argument;
2780 return NULL;
2781}
2782#endif
2783
2784/*
Milly5e7a6a42024-10-22 22:27:19 +02002785 * The 'iskeyword' option is changed.
2786 */
2787 char *
2788did_set_iskeyword(optset_T *args)
2789{
2790 char_u **varp = (char_u **)args->os_varp;
2791
2792 if (varp == &p_isk) // only check for global-value
2793 {
2794 if (check_isopt(*varp) == FAIL)
2795 return e_invalid_argument;
2796 }
2797 else // fallthrough for local-value
2798 return did_set_isopt(args);
2799
2800 return NULL;
2801}
2802
2803/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002804 * The 'isident' or the 'iskeyword' or the 'isprint' or the 'isfname' option is
2805 * changed.
2806 */
2807 char *
2808did_set_isopt(optset_T *args)
2809{
Milly5e7a6a42024-10-22 22:27:19 +02002810 // 'isident', 'iskeyword', 'isprint' or 'isfname' option: refill g_chartab[]
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002811 // If the new option is invalid, use old value.
2812 // 'lisp' option: refill g_chartab[] for '-' char.
2813 if (init_chartab() == FAIL)
2814 {
2815 args->os_restore_chartab = TRUE;// need to restore the chartab.
2816 return e_invalid_argument; // error in value
2817 }
2818
2819 return NULL;
2820}
2821
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002822/*
2823 * The 'jumpoptions' option is changed.
2824 */
2825 char *
Yegappan Lakshmanan1926ae42023-09-21 16:36:28 +02002826did_set_jumpoptions(optset_T *args UNUSED)
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002827{
2828 if (opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE) != OK)
2829 return e_invalid_argument;
2830
2831 return NULL;
2832}
2833
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002834 int
2835expand_set_jumpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2836{
2837 return expand_set_opt_string(
2838 args,
2839 p_jop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002840 ARRAY_LENGTH(p_jop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002841 numMatches,
2842 matches);
2843}
2844
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002845#if defined(FEAT_KEYMAP) || defined(PROTO)
2846/*
2847 * The 'keymap' option is changed.
2848 */
2849 char *
2850did_set_keymap(optset_T *args)
2851{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002852 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002853 char *errmsg = NULL;
2854
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002855 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002856 errmsg = e_invalid_argument;
2857 else
2858 {
2859 int secure_save = secure;
2860
2861 // Reset the secure flag, since the value of 'keymap' has
2862 // been checked to be safe.
2863 secure = 0;
2864
2865 // load or unload key mapping tables
2866 errmsg = keymap_init();
2867
2868 secure = secure_save;
2869
2870 // Since we check the value, there is no need to set P_INSECURE,
2871 // even when the value comes from a modeline.
2872 args->os_value_checked = TRUE;
2873 }
2874
2875 if (errmsg == NULL)
2876 {
2877 if (*curbuf->b_p_keymap != NUL)
2878 {
2879 // Installed a new keymap, switch on using it.
2880 curbuf->b_p_iminsert = B_IMODE_LMAP;
2881 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
2882 curbuf->b_p_imsearch = B_IMODE_LMAP;
2883 }
2884 else
2885 {
2886 // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
2887 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
2888 curbuf->b_p_iminsert = B_IMODE_NONE;
2889 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
2890 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
2891 }
2892 if ((args->os_flags & OPT_LOCAL) == 0)
2893 {
2894 set_iminsert_global();
2895 set_imsearch_global();
2896 }
2897 status_redraw_curbuf();
2898 }
2899
2900 return errmsg;
2901}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002902#endif
2903
2904/*
2905 * The 'keymodel' option is changed.
2906 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002907 char *
2908did_set_keymodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002909{
2910 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
2911 return e_invalid_argument;
2912
2913 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
2914 km_startsel = (vim_strchr(p_km, 'a') != NULL);
2915 return NULL;
2916}
2917
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002918 int
2919expand_set_keymodel(optexpand_T *args, int *numMatches, char_u ***matches)
2920{
2921 return expand_set_opt_string(
2922 args,
2923 p_km_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002924 ARRAY_LENGTH(p_km_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002925 numMatches,
2926 matches);
2927}
2928
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002929/*
2930 * The 'keyprotocol' option is changed.
2931 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002932 char *
2933did_set_keyprotocol(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002934{
Gregory Anders3695d0e2023-09-29 20:17:20 +02002935 char_u *term = T_NAME;
2936 keyprot_T kpc = match_keyprotocol(term);
2937 if (kpc == KEYPROTOCOL_FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002938 return e_invalid_argument;
2939
Gregory Anders3695d0e2023-09-29 20:17:20 +02002940 apply_keyprotocol(term, kpc);
2941
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002942 return NULL;
2943}
2944
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002945 int
2946expand_set_keyprotocol(optexpand_T *args, int *numMatches, char_u ***matches)
2947{
2948 expand_T *xp = args->oe_xp;
2949 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2950 {
2951 // 'keyprotocol' only has well-defined terms for completion for the
2952 // protocol part after the colon.
2953 return expand_set_opt_string(
2954 args,
2955 p_kpc_protocol_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002956 ARRAY_LENGTH(p_kpc_protocol_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002957 numMatches,
2958 matches);
2959 }
2960 // Use expand_set_opt_string instead of returning FAIL so that we can
2961 // include the original value if args->oe_include_orig_val is set.
2962 static char *(empty[]) = {NULL};
2963 return expand_set_opt_string(args, empty, 0, numMatches, matches);
2964}
2965
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002966/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002967 * The 'lispoptions' option is changed.
2968 */
2969 char *
2970did_set_lispoptions(optset_T *args)
2971{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002972 char_u **varp = (char_u **)args->os_varp;
2973
2974 if (**varp != NUL
2975 && STRCMP(*varp, "expr:0") != 0
2976 && STRCMP(*varp, "expr:1") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002977 return e_invalid_argument;
2978
2979 return NULL;
2980}
2981
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002982 int
2983expand_set_lispoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2984{
2985 static char *(p_lop_values[]) = {"expr:0", "expr:1", NULL};
2986 return expand_set_opt_string(
2987 args,
2988 p_lop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002989 ARRAY_LENGTH(p_lop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002990 numMatches,
2991 matches);
2992}
2993
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002994/*
2995 * The 'matchpairs' option is changed.
2996 */
2997 char *
2998did_set_matchpairs(optset_T *args)
2999{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003000 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003001 char_u *p;
3002
3003 if (has_mbyte)
3004 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003005 for (p = *varp; *p != NUL; ++p)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003006 {
3007 int x2 = -1;
3008 int x3 = -1;
3009
3010 p += mb_ptr2len(p);
3011 if (*p != NUL)
3012 x2 = *p++;
3013 if (*p != NUL)
3014 {
3015 x3 = mb_ptr2char(p);
3016 p += mb_ptr2len(p);
3017 }
3018 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
3019 return e_invalid_argument;
3020 if (*p == NUL)
3021 break;
3022 }
3023 }
3024 else
3025 {
3026 // Check for "x:y,x:y"
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003027 for (p = *varp; *p != NUL; p += 4)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003028 {
3029 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
3030 return e_invalid_argument;
3031 if (p[3] == NUL)
3032 break;
3033 }
3034 }
3035
3036 return NULL;
3037}
3038
3039#if defined(FEAT_SPELL) || defined(PROTO)
3040/*
3041 * The 'mkspellmem' option is changed.
3042 */
3043 char *
3044did_set_mkspellmem(optset_T *args UNUSED)
3045{
3046 if (spell_check_msm() != OK)
3047 return e_invalid_argument;
3048
3049 return NULL;
3050}
3051#endif
3052
3053/*
3054 * The 'mouse' option is changed.
3055 */
3056 char *
3057did_set_mouse(optset_T *args)
3058{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003059 char_u **varp = (char_u **)args->os_varp;
3060
Christian Brabandtb39b2402023-11-29 11:34:05 +01003061 return did_set_option_listflag(*varp, (char_u *)MOUSE_ALL, args->os_errbuf,
3062 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003063}
3064
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003065 int
3066expand_set_mouse(optexpand_T *args, int *numMatches, char_u ***matches)
3067{
3068 return expand_set_opt_listflag(args, (char_u*)MOUSE_ALL, numMatches, matches);
3069}
3070
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003071/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003072 * The 'mousemodel' option is changed.
3073 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003074 char *
3075did_set_mousemodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003076{
3077 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
3078 return e_invalid_argument;
3079#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
Ken Takata18d0d292023-12-19 20:12:29 +01003080 else if (*p_mousem != *args->os_oldval.string)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003081 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
3082 // to create or delete the popup menus.
3083 gui_motif_update_mousemodel(root_menu);
3084#endif
3085
3086 return NULL;
3087}
3088
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003089 int
3090expand_set_mousemodel(optexpand_T *args, int *numMatches, char_u ***matches)
3091{
3092 return expand_set_opt_string(
3093 args,
3094 p_mousem_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003095 ARRAY_LENGTH(p_mousem_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003096 numMatches,
3097 matches);
3098}
3099
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003100#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3101 char *
3102did_set_mouseshape(optset_T *args UNUSED)
3103{
3104 char *errmsg = NULL;
3105
3106 errmsg = parse_shape_opt(SHAPE_MOUSE);
3107 update_mouseshape(-1);
3108
3109 return errmsg;
3110}
3111#endif
3112
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003113/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003114 * The 'nrformats' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003115 */
3116 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003117did_set_nrformats(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003118{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003119 char_u **varp = (char_u **)args->os_varp;
3120
3121 return did_set_opt_strings(*varp, p_nf_values, TRUE);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003122}
3123
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003124 int
3125expand_set_nrformats(optexpand_T *args, int *numMatches, char_u ***matches)
3126{
3127 return expand_set_opt_string(
3128 args,
3129 p_nf_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003130 ARRAY_LENGTH(p_nf_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003131 numMatches,
3132 matches);
3133}
3134
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003135#if defined(FEAT_EVAL) || defined(PROTO)
3136/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003137 * One of the '*expr' options is changed: 'balloonexpr', 'diffexpr',
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +02003138 * 'findexpr', 'foldexpr', 'foldtext', 'formatexpr', 'includeexpr',
3139 * 'indentexpr', 'patchexpr', 'printexpr' and 'charconvert'.
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003140 *
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003141 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003142 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003143did_set_optexpr(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003144{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003145 char_u **varp = (char_u **)args->os_varp;
3146
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003147 // If the option value starts with <SID> or s:, then replace that with
3148 // the script identifier.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003149 char_u *name = get_scriptlocal_funcname(*varp);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003150 if (name != NULL)
3151 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003152 free_string_option(*varp);
3153 *varp = name;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003154 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003155
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003156 return NULL;
3157}
3158#endif
3159
3160/*
3161 * The 'pastetoggle' option is changed.
3162 */
3163 char *
3164did_set_pastetoggle(optset_T *args UNUSED)
3165{
3166 char_u *p;
3167
3168 // translate key codes like in a mapping
3169 if (*p_pt)
3170 {
zeertzjq7e0bae02023-08-11 23:15:38 +02003171 (void)replace_termcodes(p_pt, &p, 0,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003172 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
3173 if (p != NULL)
3174 {
3175 free_string_option(p_pt);
3176 p_pt = p;
3177 }
3178 }
3179
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003180 return NULL;
3181}
3182
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003183#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3184/*
3185 * The 'previewpopup' option is changed.
3186 */
3187 char *
3188did_set_previewpopup(optset_T *args UNUSED)
3189{
3190 if (parse_previewpopup(NULL) == FAIL)
3191 return e_invalid_argument;
3192
3193 return NULL;
3194}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003195
3196 int
3197expand_set_popupoption(optexpand_T *args, int *numMatches, char_u ***matches)
3198{
3199 expand_T *xp = args->oe_xp;
3200
3201 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3202 {
3203 // Within "highlight:"/"border:"/"align:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003204 int border_len = (int)STRLEN("border:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003205 if (xp->xp_pattern - args->oe_set_arg >= border_len &&
3206 STRNCMP(xp->xp_pattern - border_len, "border:", border_len) == 0)
3207 {
3208 return expand_set_opt_string(
3209 args,
3210 p_popup_option_border_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003211 ARRAY_LENGTH(p_popup_option_border_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003212 numMatches,
3213 matches);
3214 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003215 int align_len = (int)STRLEN("align:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003216 if (xp->xp_pattern - args->oe_set_arg >= align_len &&
3217 STRNCMP(xp->xp_pattern - align_len, "align:", align_len) == 0)
3218 {
3219 return expand_set_opt_string(
3220 args,
3221 p_popup_option_align_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003222 ARRAY_LENGTH(p_popup_option_align_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003223 numMatches,
3224 matches);
3225 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003226 int highlight_len = (int)STRLEN("highlight:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003227 if (xp->xp_pattern - args->oe_set_arg >= highlight_len &&
3228 STRNCMP(xp->xp_pattern - highlight_len, "highlight:", highlight_len) == 0)
3229 {
3230 // Return the list of all highlight names
3231 return expand_set_opt_generic(
3232 args,
3233 get_highlight_name,
3234 numMatches,
3235 matches);
3236 }
3237 return FAIL;
3238 }
3239
3240 return expand_set_opt_string(
3241 args,
3242 p_popup_option_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003243 ARRAY_LENGTH(p_popup_option_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003244 numMatches,
3245 matches);
3246}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003247#endif
3248
3249#if defined(FEAT_POSTSCRIPT) || defined(PROTO)
3250/*
3251 * The 'printencoding' option is changed.
3252 */
3253 char *
3254did_set_printencoding(optset_T *args UNUSED)
3255{
3256 char_u *s, *p;
3257
3258 // Canonize 'printencoding' if VIM standard one
3259 p = enc_canonize(p_penc);
3260 if (p != NULL)
3261 {
3262 vim_free(p_penc);
3263 p_penc = p;
3264 }
3265 else
3266 {
3267 // Ensure lower case and '-' for '_'
3268 for (s = p_penc; *s != NUL; s++)
3269 {
3270 if (*s == '_')
3271 *s = '-';
3272 else
3273 *s = TOLOWER_ASC(*s);
3274 }
3275 }
3276
3277 return NULL;
3278}
3279#endif
3280
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003281#if defined(FEAT_PRINTER) || defined(PROTO)
3282
3283 static char_u *
3284get_printoptions_names(expand_T *xp UNUSED, int idx)
3285{
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003286 if (idx >= (int)ARRAY_LENGTH(printer_opts))
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003287 return NULL;
3288 return (char_u*)printer_opts[idx].name;
3289}
3290
3291/*
3292 * Expand 'printoptions' option
3293 */
3294 int
3295expand_set_printoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3296{
3297 return expand_set_opt_generic(
3298 args,
3299 get_printoptions_names,
3300 numMatches,
3301 matches);
3302}
3303#endif
3304
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003305#if defined(FEAT_STL_OPT) || defined(PROTO)
3306/*
3307 * The 'statusline' or the 'tabline' or the 'rulerformat' option is changed.
3308 * "rulerformat" is TRUE if the 'rulerformat' option is changed.
3309 */
3310 static char *
3311parse_statustabline_rulerformat(optset_T *args, int rulerformat)
3312{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003313 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003314 char_u *s;
3315 char *errmsg = NULL;
3316 int wid;
3317
3318 if (rulerformat) // reset ru_wid first
3319 ru_wid = 0;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003320 s = *varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003321 if (rulerformat && *s == '%')
3322 {
3323 // set ru_wid if 'ruf' starts with "%99("
3324 if (*++s == '-') // ignore a '-'
3325 s++;
3326 wid = getdigits(&s);
3327 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
3328 ru_wid = wid;
3329 else
3330 errmsg = check_stl_option(p_ruf);
3331 }
3332 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
3333 else if (rulerformat || s[0] != '%' || s[1] != '!')
3334 errmsg = check_stl_option(s);
3335 if (rulerformat && errmsg == NULL)
3336 comp_col();
3337
3338 return errmsg;
3339}
3340#endif
3341
3342#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
3343/*
3344 * The 'renderoptions' option is changed.
3345 */
3346 char *
3347did_set_renderoptions(optset_T *args UNUSED)
3348{
3349 if (!gui_mch_set_rendering_options(p_rop))
3350 return e_invalid_argument;
3351
3352 return NULL;
3353}
3354#endif
3355
3356#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3357/*
3358 * The 'rightleftcmd' option is changed.
3359 */
3360 char *
3361did_set_rightleftcmd(optset_T *args)
3362{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003363 char_u **varp = (char_u **)args->os_varp;
3364
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003365 // Currently only "search" is a supported value.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003366 if (**varp != NUL && STRCMP(*varp, "search") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003367 return e_invalid_argument;
3368
3369 return NULL;
3370}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003371
3372 int
3373expand_set_rightleftcmd(optexpand_T *args, int *numMatches, char_u ***matches)
3374{
3375 static char *(p_rlc_values[]) = {"search", NULL};
3376 return expand_set_opt_string(
3377 args,
3378 p_rlc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003379 ARRAY_LENGTH(p_rlc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003380 numMatches,
3381 matches);
3382}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003383#endif
3384
3385#if defined(FEAT_STL_OPT) || defined(PROTO)
3386/*
3387 * The 'rulerformat' option is changed.
3388 */
3389 char *
3390did_set_rulerformat(optset_T *args)
3391{
3392 return parse_statustabline_rulerformat(args, TRUE);
3393}
3394#endif
3395
3396/*
3397 * The 'scrollopt' option is changed.
3398 */
3399 char *
3400did_set_scrollopt(optset_T *args UNUSED)
3401{
3402 return did_set_opt_strings(p_sbo, p_scbopt_values, TRUE);
3403}
3404
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003405 int
3406expand_set_scrollopt(optexpand_T *args, int *numMatches, char_u ***matches)
3407{
3408 return expand_set_opt_string(
3409 args,
3410 p_scbopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003411 ARRAY_LENGTH(p_scbopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003412 numMatches,
3413 matches);
3414}
3415
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003416/*
3417 * The 'selection' option is changed.
3418 */
3419 char *
3420did_set_selection(optset_T *args UNUSED)
3421{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003422 if (*p_sel == NUL || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003423 return e_invalid_argument;
3424
3425 return NULL;
3426}
3427
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003428 int
3429expand_set_selection(optexpand_T *args, int *numMatches, char_u ***matches)
3430{
3431 return expand_set_opt_string(
3432 args,
3433 p_sel_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003434 ARRAY_LENGTH(p_sel_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003435 numMatches,
3436 matches);
3437}
3438
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003439/*
3440 * The 'selectmode' option is changed.
3441 */
3442 char *
3443did_set_selectmode(optset_T *args UNUSED)
3444{
3445 return did_set_opt_strings(p_slm, p_slm_values, TRUE);
3446}
3447
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003448 int
3449expand_set_selectmode(optexpand_T *args, int *numMatches, char_u ***matches)
3450{
3451 return expand_set_opt_string(
3452 args,
3453 p_slm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003454 ARRAY_LENGTH(p_slm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003455 numMatches,
3456 matches);
3457}
3458
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003459#if defined(FEAT_SESSION) || defined(PROTO)
3460/*
3461 * The 'sessionoptions' option is changed.
3462 */
3463 char *
3464did_set_sessionoptions(optset_T *args)
3465{
3466 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
3467 return e_invalid_argument;
3468 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
3469 {
3470 // Don't allow both "sesdir" and "curdir".
3471 (void)opt_strings_flags(args->os_oldval.string, p_ssop_values,
3472 &ssop_flags, TRUE);
3473 return e_invalid_argument;
3474 }
3475
3476 return NULL;
3477}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003478
3479 int
3480expand_set_sessionoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3481{
3482 return expand_set_opt_string(
3483 args,
3484 p_ssop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003485 ARRAY_LENGTH(p_ssop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003486 numMatches,
3487 matches);
3488}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003489#endif
3490
3491/*
3492 * The 'shortmess' option is changed.
3493 */
3494 char *
3495did_set_shortmess(optset_T *args)
3496{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003497 char_u **varp = (char_u **)args->os_varp;
3498
Christian Brabandtb39b2402023-11-29 11:34:05 +01003499 return did_set_option_listflag(*varp, (char_u *)SHM_ALL, args->os_errbuf,
3500 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003501}
3502
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003503 int
3504expand_set_shortmess(optexpand_T *args, int *numMatches, char_u ***matches)
3505{
3506 return expand_set_opt_listflag(args, (char_u*)SHM_ALL, numMatches, matches);
3507}
3508
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003509#if defined(FEAT_LINEBREAK) || defined(PROTO)
3510/*
3511 * The 'showbreak' option is changed.
3512 */
3513 char *
3514did_set_showbreak(optset_T *args)
3515{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003516 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003517 char_u *s;
3518
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003519 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003520 {
3521 if (ptr2cells(s) != 1)
3522 return e_showbreak_contains_unprintable_or_wide_character;
3523 MB_PTR_ADV(s);
3524 }
3525
3526 return NULL;
3527}
3528#endif
3529
3530/*
3531 * The 'showcmdloc' option is changed.
3532 */
3533 char *
3534did_set_showcmdloc(optset_T *args UNUSED)
3535{
zeertzjqc27fcf42024-03-01 23:01:43 +01003536 char *errmsg = did_set_opt_strings(p_sloc, p_sloc_values, FALSE);
3537
3538 if (errmsg == NULL)
3539 comp_col();
3540
3541 return errmsg;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003542}
3543
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003544 int
3545expand_set_showcmdloc(optexpand_T *args, int *numMatches, char_u ***matches)
3546{
3547 return expand_set_opt_string(
3548 args,
3549 p_sloc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003550 ARRAY_LENGTH(p_sloc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003551 numMatches,
3552 matches);
3553}
3554
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003555#if defined(FEAT_SIGNS) || defined(PROTO)
3556/*
3557 * The 'signcolumn' option is changed.
3558 */
3559 char *
3560did_set_signcolumn(optset_T *args)
3561{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003562 char_u **varp = (char_u **)args->os_varp;
3563
3564 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003565 return e_invalid_argument;
3566 // When changing the 'signcolumn' to or from 'number', recompute the
3567 // width of the number column if 'number' or 'relativenumber' is set.
3568 if (((*args->os_oldval.string == 'n' && args->os_oldval.string[1] == 'u')
3569 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
3570 && (curwin->w_p_nu || curwin->w_p_rnu))
3571 curwin->w_nrwidth_line_count = 0;
3572
3573 return NULL;
3574}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003575
3576 int
3577expand_set_signcolumn(optexpand_T *args, int *numMatches, char_u ***matches)
3578{
3579 return expand_set_opt_string(
3580 args,
3581 p_scl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003582 ARRAY_LENGTH(p_scl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003583 numMatches,
3584 matches);
3585}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003586#endif
3587
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003588#if defined(FEAT_SPELL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003589/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003590 * The 'spellcapcheck' option is changed.
3591 */
3592 char *
3593did_set_spellcapcheck(optset_T *args UNUSED)
3594{
3595 // compile the regexp program.
3596 return compile_cap_prog(curwin->w_s);
3597}
3598
3599/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003600 * The 'spellfile' option is changed.
3601 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003602 char *
3603did_set_spellfile(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003604{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003605 char_u **varp = (char_u **)args->os_varp;
3606
3607 if (!valid_spellfile(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003608 return e_invalid_argument;
3609
3610 // If there is a window for this buffer in which 'spell' is set load the
3611 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003612 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003613}
3614
3615/*
3616 * The 'spell' option is changed.
3617 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003618 char *
3619did_set_spelllang(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003620{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003621 char_u **varp = (char_u **)args->os_varp;
3622
3623 if (!valid_spelllang(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003624 return e_invalid_argument;
3625
3626 // If there is a window for this buffer in which 'spell' is set load the
3627 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003628 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003629}
3630
3631/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003632 * The 'spelloptions' option is changed.
3633 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003634 char *
3635did_set_spelloptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003636{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003637 char_u **varp = (char_u **)args->os_varp;
3638
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003639 if (**varp != NUL && STRCMP(*varp, "camel") != 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003640 return e_invalid_argument;
3641
3642 return NULL;
3643}
3644
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003645 int
3646expand_set_spelloptions(optexpand_T *args, int *numMatches, char_u ***matches)
3647{
3648 static char *(p_spo_values[]) = {"camel", NULL};
3649 return expand_set_opt_string(
3650 args,
3651 p_spo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003652 ARRAY_LENGTH(p_spo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003653 numMatches,
3654 matches);
3655}
3656
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003657/*
3658 * The 'spellsuggest' option is changed.
3659 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003660 char *
3661did_set_spellsuggest(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003662{
3663 if (spell_check_sps() != OK)
3664 return e_invalid_argument;
3665
3666 return NULL;
3667}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003668
3669 int
3670expand_set_spellsuggest(optexpand_T *args, int *numMatches, char_u ***matches)
3671{
3672 return expand_set_opt_string(
3673 args,
3674 p_sps_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003675 ARRAY_LENGTH(p_sps_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003676 numMatches,
3677 matches);
3678}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003679#endif
3680
3681/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003682 * The 'splitkeep' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003683 */
3684 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003685did_set_splitkeep(optset_T *args UNUSED)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003686{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003687 return did_set_opt_strings(p_spk, p_spk_values, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003688}
3689
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003690 int
3691expand_set_splitkeep(optexpand_T *args, int *numMatches, char_u ***matches)
3692{
3693 return expand_set_opt_string(
3694 args,
3695 p_spk_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003696 ARRAY_LENGTH(p_spk_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003697 numMatches,
3698 matches);
3699}
3700
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00003701#if defined(FEAT_STL_OPT) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003702/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003703 * The 'statusline' option is changed.
3704 */
3705 char *
3706did_set_statusline(optset_T *args)
3707{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003708 return parse_statustabline_rulerformat(args, FALSE);
3709}
3710#endif
3711
3712/*
3713 * The 'swapsync' option is changed.
3714 */
3715 char *
3716did_set_swapsync(optset_T *args UNUSED)
3717{
3718 return did_set_opt_strings(p_sws, p_sws_values, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003719}
3720
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003721 int
3722expand_set_swapsync(optexpand_T *args, int *numMatches, char_u ***matches)
3723{
3724 return expand_set_opt_string(
3725 args,
3726 p_sws_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003727 ARRAY_LENGTH(p_sws_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003728 numMatches,
3729 matches);
3730}
3731
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003732/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003733 * The 'switchbuf' option is changed.
3734 */
3735 char *
3736did_set_switchbuf(optset_T *args UNUSED)
3737{
3738 return did_set_opt_flags(p_swb, p_swb_values, &swb_flags, TRUE);
3739}
3740
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003741 int
3742expand_set_switchbuf(optexpand_T *args, int *numMatches, char_u ***matches)
3743{
3744 return expand_set_opt_string(
3745 args,
3746 p_swb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003747 ARRAY_LENGTH(p_swb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003748 numMatches,
3749 matches);
3750}
3751
LemonBoy5247b0b2024-07-12 19:30:58 +02003752/*
3753 * The 'tabclose' option is changed.
3754 */
3755 char *
3756did_set_tabclose(optset_T *args UNUSED)
3757{
3758 return did_set_opt_flags(p_tcl, p_tcl_values, &tcl_flags, TRUE);
3759}
3760
3761 int
3762expand_set_tabclose(optexpand_T *args, int *numMatches, char_u ***matches)
3763{
3764 return expand_set_opt_string(
3765 args,
3766 p_tcl_values,
3767 ARRAY_LENGTH(p_tcl_values) - 1,
3768 numMatches,
3769 matches);
3770}
3771
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003772#if defined(FEAT_STL_OPT) || defined(PROTO)
3773/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003774 * The 'tabline' option is changed.
3775 */
3776 char *
3777did_set_tabline(optset_T *args)
3778{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003779 return parse_statustabline_rulerformat(args, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003780}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003781#endif
3782
3783/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003784 * The 'tagcase' option is changed.
3785 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003786 char *
3787did_set_tagcase(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003788{
3789 unsigned int *flags;
3790 char_u *p;
3791
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003792 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003793 {
3794 p = curbuf->b_p_tc;
3795 flags = &curbuf->b_tc_flags;
3796 }
3797 else
3798 {
3799 p = p_tc;
3800 flags = &tc_flags;
3801 }
3802
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003803 if ((args->os_flags & OPT_LOCAL) && *p == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003804 // make the local value empty: use the global value
3805 *flags = 0;
3806 else if (*p == NUL
3807 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
3808 return e_invalid_argument;
3809
3810 return NULL;
3811}
3812
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003813 int
3814expand_set_tagcase(optexpand_T *args, int *numMatches, char_u ***matches)
3815{
3816 return expand_set_opt_string(
3817 args,
3818 p_tc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003819 ARRAY_LENGTH(p_tc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003820 numMatches,
3821 matches);
3822}
3823
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003824/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003825 * The 'term' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003826 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003827 char *
3828did_set_term(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003829{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003830 if (T_NAME[0] == NUL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003831 return e_cannot_set_term_to_empty_string;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003832#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003833 if (gui.in_use)
3834 return e_cannot_change_term_in_GUI;
3835 if (term_is_gui(T_NAME))
3836 return e_use_gui_to_start_GUI;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003837#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003838 if (set_termname(T_NAME) == FAIL)
3839 return e_not_found_in_termcap;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003840
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003841 // Screen colors may have changed.
3842 redraw_later_clear();
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003843
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003844 return NULL;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003845}
3846
3847/*
3848 * Some terminal option (t_xxx) is changed
3849 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003850 char *
3851did_set_term_option(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003852{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003853 char_u **varp = (char_u **)args->os_varp;
3854
3855 if (!full_screen)
3856 return NULL;
3857
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003858 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
3859 if (varp == &T_CCO)
3860 {
3861 int colors = atoi((char *)T_CCO);
3862
3863 // Only reinitialize colors if t_Co value has really changed to
3864 // avoid expensive reload of colorscheme if t_Co is set to the
3865 // same value multiple times.
3866 if (colors != t_colors)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003867 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003868 t_colors = colors;
3869 if (t_colors <= 1)
3870 {
3871 vim_free(T_CCO);
3872 T_CCO = empty_option;
3873 }
3874#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3875 if (is_term_win32())
3876 {
3877 swap_tcap();
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003878 args->os_did_swaptcap = TRUE;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003879 }
3880#endif
3881 // We now have a different color setup, initialize it again.
3882 init_highlight(TRUE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003883 }
3884 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003885 ttest(FALSE);
3886 if (varp == &T_ME)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003887 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003888 out_str(T_ME);
3889 redraw_later(UPD_CLEAR);
3890#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3891 // Since t_me has been set, this probably means that the user
3892 // wants to use this as default colors. Need to reset default
3893 // background/foreground colors.
3894# ifdef VIMDLL
3895 if (!gui.in_use && !gui.starting)
3896# endif
3897 mch_set_normal_colors();
3898#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003899 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003900 if (varp == &T_BE && termcap_active)
3901 {
3902 MAY_WANT_TO_LOG_THIS;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003903
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003904 if (*T_BE == NUL)
3905 // When clearing t_BE we assume the user no longer wants
3906 // bracketed paste, thus disable it by writing t_BD.
3907 out_str(T_BD);
3908 else
3909 out_str(T_BE);
3910 }
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003911
3912 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003913}
3914
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003915#if defined(FEAT_TERMINAL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003916/*
3917 * The 'termwinkey' option is changed.
3918 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003919 char *
Milly94606f72024-10-22 22:07:52 +02003920did_set_termwinkey(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003921{
Milly94606f72024-10-22 22:07:52 +02003922 char_u **varp = (char_u **)args->os_varp;
3923
3924 if ((*varp)[0] != NUL && string_to_key(*varp, TRUE) == 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003925 return e_invalid_argument;
3926
3927 return NULL;
3928}
3929
3930/*
3931 * The 'termwinsize' option is changed.
3932 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003933 char *
Milly8be10aa2024-10-22 22:01:46 +02003934did_set_termwinsize(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003935{
Milly8be10aa2024-10-22 22:01:46 +02003936 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003937 char_u *p;
3938
Milly8be10aa2024-10-22 22:01:46 +02003939 if ((*varp)[0] == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003940 return NULL;
3941
Milly8be10aa2024-10-22 22:01:46 +02003942 p = skipdigits(*varp);
3943 if (p == *varp || (*p != 'x' && *p != '*') || *skipdigits(p + 1) != NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003944 return e_invalid_argument;
3945
3946 return NULL;
3947}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003948
3949# if defined(MSWIN) || defined(PROTO)
3950/*
3951 * The 'termwintype' option is changed.
3952 */
3953 char *
3954did_set_termwintype(optset_T *args UNUSED)
3955{
3956 return did_set_opt_strings(p_twt, p_twt_values, FALSE);
3957}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003958
3959 int
3960expand_set_termwintype(optexpand_T *args, int *numMatches, char_u ***matches)
3961{
3962 return expand_set_opt_string(
3963 args,
3964 p_twt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003965 ARRAY_LENGTH(p_twt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003966 numMatches,
3967 matches);
3968}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003969# endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003970#endif
3971
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003972/*
3973 * The 'titlestring' option is changed.
3974 */
3975 char *
3976did_set_titlestring(optset_T *args)
3977{
3978 int flagval = 0;
3979
3980#ifdef FEAT_STL_OPT
3981 flagval = STL_IN_TITLE;
3982#endif
3983 return parse_titleiconstring(args, flagval);
3984}
3985
3986#if (defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)) || defined(PROTO)
3987/*
3988 * The 'toolbar' option is changed.
3989 */
3990 char *
3991did_set_toolbar(optset_T *args UNUSED)
3992{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003993 if (opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags,
3994 TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003995 return e_invalid_argument;
3996
3997 out_flush();
3998 gui_mch_show_toolbar((toolbar_flags &
3999 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4000 return NULL;
4001}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004002
4003 int
4004expand_set_toolbar(optexpand_T *args, int *numMatches, char_u ***matches)
4005{
4006 return expand_set_opt_string(
4007 args,
4008 p_toolbar_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004009 ARRAY_LENGTH(p_toolbar_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004010 numMatches,
4011 matches);
4012}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004013#endif
4014
4015#if (defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)) || defined(PROTO)
4016/*
4017 * The 'toolbariconsize' option is changed. GTK+ 2 only.
4018 */
4019 char *
4020did_set_toolbariconsize(optset_T *args UNUSED)
4021{
4022 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
4023 return e_invalid_argument;
4024
4025 out_flush();
4026 gui_mch_show_toolbar((toolbar_flags &
4027 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4028 return NULL;
4029}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004030
4031 int
4032expand_set_toolbariconsize(optexpand_T *args, int *numMatches, char_u ***matches)
4033{
4034 return expand_set_opt_string(
4035 args,
4036 p_tbis_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004037 ARRAY_LENGTH(p_tbis_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004038 numMatches,
4039 matches);
4040}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004041#endif
4042
4043#if defined(UNIX) || defined(VMS) || defined(PROTO)
4044/*
4045 * The 'ttymouse' option is changed.
4046 */
4047 char *
4048did_set_ttymouse(optset_T *args UNUSED)
4049{
4050 char *errmsg = NULL;
4051
4052 // Switch the mouse off before changing the escape sequences used for
4053 // that.
4054 mch_setmouse(FALSE);
4055 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
4056 errmsg = e_invalid_argument;
4057 else
4058 check_mouse_termcode();
4059 if (termcap_active)
4060 setmouse(); // may switch it on again
4061
4062 return errmsg;
4063}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004064
4065 int
4066expand_set_ttymouse(optexpand_T *args, int *numMatches, char_u ***matches)
4067{
4068 return expand_set_opt_string(
4069 args,
4070 p_ttym_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004071 ARRAY_LENGTH(p_ttym_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004072 numMatches,
4073 matches);
4074}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004075#endif
4076
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004077#if defined(FEAT_VARTABS) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004078/*
4079 * The 'varsofttabstop' option is changed.
4080 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004081 char *
4082did_set_varsofttabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004083{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004084 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004085 char_u *cp;
4086
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004087 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004088 VIM_CLEAR(curbuf->b_p_vsts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004089 else
4090 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004091 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004092 {
4093 if (vim_isdigit(*cp))
4094 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004095 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004096 continue;
4097 return e_invalid_argument;
4098 }
4099
4100 int *oldarray = curbuf->b_p_vsts_array;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004101 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004102 {
4103 if (oldarray)
4104 vim_free(oldarray);
4105 }
4106 else
4107 return e_invalid_argument;
4108 }
4109
4110 return NULL;
4111}
4112
4113/*
4114 * The 'vartabstop' option is changed.
4115 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004116 char *
4117did_set_vartabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004118{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004119 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004120 char_u *cp;
4121
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004122 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004123 VIM_CLEAR(curbuf->b_p_vts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004124 else
4125 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004126 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004127 {
4128 if (vim_isdigit(*cp))
4129 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004130 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004131 continue;
4132 return e_invalid_argument;
4133 }
4134
4135 int *oldarray = curbuf->b_p_vts_array;
4136
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004137 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004138 {
4139 vim_free(oldarray);
4140# ifdef FEAT_FOLDING
4141 if (foldmethodIsIndent(curwin))
4142 foldUpdateAll(curwin);
4143# endif
4144 }
4145 else
4146 return e_invalid_argument;
4147 }
4148
4149 return NULL;
4150}
4151#endif
4152
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004153/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004154 * The 'verbosefile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004155 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004156 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004157did_set_verbosefile(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004158{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004159 verbose_stop();
4160 if (*p_vfile != NUL && verbose_open() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004161 return e_invalid_argument;
4162
4163 return NULL;
4164}
4165
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004166#if defined(FEAT_SESSION) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004167/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004168 * The 'viewoptions' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004169 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004170 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004171did_set_viewoptions(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004172{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004173 return did_set_opt_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004174}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004175#endif
4176
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004177#if defined(FEAT_VIMINFO) || defined(PROTO)
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004178/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004179 * The 'viminfo' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004180 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004181 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004182did_set_viminfo(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004183{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004184 char_u *s;
4185 char *errmsg = NULL;
4186
4187 for (s = p_viminfo; *s;)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004188 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004189 // Check it's a valid character
4190 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
4191 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01004192 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004193 break;
4194 }
4195 if (*s == 'n') // name is always last one
4196 break;
4197 else if (*s == 'r') // skip until next ','
4198 {
4199 while (*++s && *s != ',')
4200 ;
4201 }
4202 else if (*s == '%')
4203 {
4204 // optional number
4205 while (vim_isdigit(*++s))
4206 ;
4207 }
4208 else if (*s == '!' || *s == 'h' || *s == 'c')
4209 ++s; // no extra chars
4210 else // must have a number
4211 {
4212 while (vim_isdigit(*++s))
4213 ;
4214
4215 if (!VIM_ISDIGIT(*(s - 1)))
4216 {
4217 if (args->os_errbuf != NULL)
4218 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01004219 vim_snprintf(args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004220 _(e_missing_number_after_angle_str_angle),
4221 transchar_byte(*(s - 1)));
4222 errmsg = args->os_errbuf;
4223 }
4224 else
4225 errmsg = "";
4226 break;
4227 }
4228 }
4229 if (*s == ',')
4230 ++s;
4231 else if (*s)
4232 {
4233 if (args->os_errbuf != NULL)
4234 errmsg = e_missing_comma;
4235 else
4236 errmsg = "";
4237 break;
4238 }
4239 }
4240 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
4241 errmsg = e_must_specify_a_value;
4242
4243 return errmsg;
4244}
4245#endif
4246
4247/*
4248 * The 'virtualedit' option is changed.
4249 */
4250 char *
4251did_set_virtualedit(optset_T *args)
4252{
4253 char_u *ve = p_ve;
4254 unsigned int *flags = &ve_flags;
4255
4256 if (args->os_flags & OPT_LOCAL)
4257 {
4258 ve = curwin->w_p_ve;
4259 flags = &curwin->w_ve_flags;
4260 }
4261
4262 if ((args->os_flags & OPT_LOCAL) && *ve == NUL)
4263 // make the local value empty: use the global value
4264 *flags = 0;
4265 else
4266 {
4267 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
4268 return e_invalid_argument;
4269 else if (STRCMP(ve, args->os_oldval.string) != 0)
4270 {
4271 // Recompute cursor position in case the new 've' setting
4272 // changes something.
4273 validate_virtcol();
4274 coladvance(curwin->w_virtcol);
4275 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004276 }
4277
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004278 return NULL;
4279}
4280
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004281 int
4282expand_set_virtualedit(optexpand_T *args, int *numMatches, char_u ***matches)
4283{
4284 return expand_set_opt_string(
4285 args,
4286 p_ve_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004287 ARRAY_LENGTH(p_ve_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004288 numMatches,
4289 matches);
4290}
4291
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004292/*
4293 * The 'whichwrap' option is changed.
4294 */
4295 char *
4296did_set_whichwrap(optset_T *args)
4297{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004298 char_u **varp = (char_u **)args->os_varp;
4299
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004300 // Add ',' to the list flags because 'whichwrap' is a flag
4301 // list that is comma-separated.
Christian Brabandtb39b2402023-11-29 11:34:05 +01004302 return did_set_option_listflag(*varp, (char_u *)(WW_ALL ","),
4303 args->os_errbuf, args->os_errbuflen);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004304}
4305
4306 int
4307expand_set_whichwrap(optexpand_T *args, int *numMatches, char_u ***matches)
4308{
4309 return expand_set_opt_listflag(args, (char_u*)WW_ALL, numMatches, matches);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004310}
4311
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004312/*
4313 * The 'wildmode' option is changed.
4314 */
4315 char *
4316did_set_wildmode(optset_T *args UNUSED)
4317{
4318 if (check_opt_wim() == FAIL)
4319 return e_invalid_argument;
4320 return NULL;
4321}
4322
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004323 int
4324expand_set_wildmode(optexpand_T *args, int *numMatches, char_u ***matches)
4325{
4326 return expand_set_opt_string(
4327 args,
4328 p_wim_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004329 ARRAY_LENGTH(p_wim_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004330 numMatches,
4331 matches);
4332}
4333
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004334/*
4335 * The 'wildoptions' option is changed.
4336 */
4337 char *
4338did_set_wildoptions(optset_T *args UNUSED)
4339{
4340 return did_set_opt_strings(p_wop, p_wop_values, TRUE);
4341}
4342
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004343 int
4344expand_set_wildoptions(optexpand_T *args, int *numMatches, char_u ***matches)
4345{
4346 return expand_set_opt_string(
4347 args,
4348 p_wop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004349 ARRAY_LENGTH(p_wop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004350 numMatches,
4351 matches);
4352}
4353
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004354#if defined(FEAT_WAK) || defined(PROTO)
4355/*
4356 * The 'winaltkeys' option is changed.
4357 */
4358 char *
4359did_set_winaltkeys(optset_T *args UNUSED)
4360{
4361 char *errmsg = NULL;
4362
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004363 if (*p_wak == NUL || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004364 errmsg = e_invalid_argument;
4365# ifdef FEAT_MENU
4366# if defined(FEAT_GUI_MOTIF)
4367 else if (gui.in_use)
4368 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4369# elif defined(FEAT_GUI_GTK)
4370 else if (gui.in_use)
4371 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4372# endif
4373# endif
4374 return errmsg;
4375}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004376
4377 int
4378expand_set_winaltkeys(optexpand_T *args, int *numMatches, char_u ***matches)
4379{
4380 return expand_set_opt_string(
4381 args,
4382 p_wak_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004383 ARRAY_LENGTH(p_wak_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004384 numMatches,
4385 matches);
4386}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004387#endif
4388
4389/*
4390 * The 'wincolor' option is changed.
4391 */
4392 char *
4393did_set_wincolor(optset_T *args UNUSED)
4394{
4395#ifdef FEAT_TERMINAL
4396 term_update_wincolor(curwin);
4397#endif
4398 return NULL;
4399}
4400
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004401 int
4402expand_set_wincolor(optexpand_T *args, int *numMatches, char_u ***matches)
4403{
4404 return expand_set_opt_generic(
4405 args,
4406 get_highlight_name,
4407 numMatches,
4408 matches);
4409}
4410
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004411#ifdef FEAT_SYN_HL
4412/*
4413 * When the 'syntax' option is set, load the syntax of that name.
4414 */
4415 static void
4416do_syntax_autocmd(int value_changed)
4417{
4418 static int syn_recursive = 0;
4419
4420 ++syn_recursive;
4421 // Only pass TRUE for "force" when the value changed or not used
4422 // recursively, to avoid endless recurrence.
4423 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
4424 value_changed || syn_recursive == 1, curbuf);
4425 curbuf->b_flags |= BF_SYN_SET;
4426 --syn_recursive;
4427}
4428#endif
4429
4430/*
4431 * When the 'filetype' option is set, trigger the FileType autocommand.
4432 */
4433 static void
4434do_filetype_autocmd(char_u **varp, int opt_flags, int value_changed)
4435{
4436 // Skip this when called from a modeline and the filetype was already set
4437 // to this value.
4438 if ((opt_flags & OPT_MODELINE) && !value_changed)
4439 return;
4440
4441 static int ft_recursive = 0;
4442 int secure_save = secure;
4443
4444 // Reset the secure flag, since the value of 'filetype' has
4445 // been checked to be safe.
4446 secure = 0;
4447
4448 ++ft_recursive;
zeertzjq5bf6c212024-03-31 18:41:27 +02004449 curbuf->b_did_filetype = TRUE;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004450 // Only pass TRUE for "force" when the value changed or not
4451 // used recursively, to avoid endless recurrence.
4452 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
4453 value_changed || ft_recursive == 1, curbuf);
4454 --ft_recursive;
4455 // Just in case the old "curbuf" is now invalid.
4456 if (varp != &(curbuf->b_p_ft))
4457 varp = NULL;
4458
4459 secure = secure_save;
4460}
4461
4462#ifdef FEAT_SPELL
4463/*
4464 * When the 'spelllang' option is set, source the spell/LANG.vim file in
4465 * 'runtimepath'.
4466 */
4467 static void
4468do_spelllang_source(void)
4469{
4470 char_u fname[200];
4471 char_u *p;
4472 char_u *q = curwin->w_s->b_p_spl;
4473
4474 // Skip the first name if it is "cjk".
4475 if (STRNCMP(q, "cjk,", 4) == 0)
4476 q += 4;
4477
4478 // They could set 'spellcapcheck' depending on the language. Use the first
4479 // name in 'spelllang' up to '_region' or '.encoding'.
4480 for (p = q; *p != NUL; ++p)
4481 if (!ASCII_ISALNUM(*p) && *p != '-')
4482 break;
4483 if (p > q)
4484 {
4485 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
4486 (int)(p - q), q);
4487 source_runtime(fname, DIP_ALL);
4488 }
4489}
4490#endif
4491
4492/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004493 * Handle string options that need some action to perform when changed.
zeertzjqf6782732022-07-27 18:26:03 +01004494 * The new value must be allocated.
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004495 * Returns NULL for success, or an untranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +02004496 */
4497 char *
4498did_set_string_option(
4499 int opt_idx, // index in options[] table
4500 char_u **varp, // pointer to the option variable
Bram Moolenaardac13472019-09-16 21:06:21 +02004501 char_u *oldval, // previous value of the option
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004502 char_u *value, // new value of the option
Bram Moolenaardac13472019-09-16 21:06:21 +02004503 char *errbuf, // buffer for errors, or NULL
Mike Williams620f0112023-12-05 15:36:06 +01004504 size_t errbuflen, // length of error buffer
Bram Moolenaardac13472019-09-16 21:06:21 +02004505 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004506 set_op_T op, // OP_ADDING/OP_PREPENDING/OP_REMOVING
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004507 int *value_checked) // value was checked to be safe, no
Bram Moolenaardac13472019-09-16 21:06:21 +02004508 // need to set P_INSECURE
4509{
4510 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +02004511 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004512 opt_did_set_cb_T did_set_cb = get_option_did_set_cb(opt_idx);
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004513 optset_T args;
4514
4515 // 'ttytype' is an alias for 'term'. Both 'term' and 'ttytype' point to
4516 // T_NAME. If 'term' or 'ttytype' is modified, then use the index for the
4517 // 'term' option. Only set the P_ALLOCED flag on 'term'.
4518 if (varp == &T_NAME)
4519 {
4520 opt_idx = findoption((char_u *)"term");
4521 if (opt_idx >= 0)
4522 {
4523 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
4524 did_set_cb = get_option_did_set_cb(opt_idx);
4525 }
4526 }
4527
4528 CLEAR_FIELD(args);
Bram Moolenaardac13472019-09-16 21:06:21 +02004529
Bram Moolenaardac13472019-09-16 21:06:21 +02004530 // Disallow changing some options from secure mode
4531 if ((secure
4532#ifdef HAVE_SANDBOX
4533 || sandbox != 0
4534#endif
4535 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00004536 errmsg = e_not_allowed_here;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004537 // Check for a "normal" directory or file name in some options.
4538 else if (check_illegal_path_names(opt_idx, varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004539 errmsg = e_invalid_argument;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004540 else if (did_set_cb != NULL)
4541 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004542 args.os_varp = (char_u *)varp;
4543 args.os_idx = opt_idx;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004544 args.os_flags = opt_flags;
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004545 args.os_op = op;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004546 args.os_oldval.string = oldval;
4547 args.os_newval.string = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004548 args.os_errbuf = errbuf;
Christian Brabandtb39b2402023-11-29 11:34:05 +01004549 args.os_errbuflen = errbuflen;
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004550 // Invoke the option specific callback function to validate and apply
4551 // the new option value.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004552 errmsg = did_set_cb(&args);
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004553
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004554 // The 'keymap', 'filetype' and 'syntax' option callback functions
4555 // may change the os_value_checked field.
4556 *value_checked = args.os_value_checked;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004557 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004558
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004559 // If an error is detected, restore the previous value.
Bram Moolenaardac13472019-09-16 21:06:21 +02004560 if (errmsg != NULL)
4561 {
zeertzjqf6782732022-07-27 18:26:03 +01004562 free_string_option(*varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02004563 *varp = oldval;
4564 // When resetting some values, need to act on it.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004565 if (args.os_restore_chartab)
Bram Moolenaardac13472019-09-16 21:06:21 +02004566 (void)init_chartab();
4567 if (varp == &p_hl)
4568 (void)highlight_changed();
4569 }
4570 else
4571 {
4572#ifdef FEAT_EVAL
4573 // Remember where the option was set.
4574 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4575#endif
4576 // Free string options that are in allocated memory.
4577 // Use "free_oldval", because recursiveness may change the flags under
4578 // our fingers (esp. init_highlight()).
4579 if (free_oldval)
4580 free_string_option(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01004581 set_option_flag(opt_idx, P_ALLOCED);
Bram Moolenaardac13472019-09-16 21:06:21 +02004582
4583 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
4584 && is_global_local_option(opt_idx))
4585 {
4586 // global option with local value set to use global value; free
4587 // the local value and make it empty
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004588 char_u *p = get_option_varp_scope(opt_idx, OPT_LOCAL);
Bram Moolenaardac13472019-09-16 21:06:21 +02004589 free_string_option(*(char_u **)p);
4590 *(char_u **)p = empty_option;
4591 }
4592
4593 // May set global value for local option.
4594 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
4595 set_string_option_global(opt_idx, varp);
4596
4597 // Trigger the autocommand only after setting the flags.
4598#ifdef FEAT_SYN_HL
Bram Moolenaardac13472019-09-16 21:06:21 +02004599 if (varp == &(curbuf->b_p_syn))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004600 do_syntax_autocmd(args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004601#endif
4602 else if (varp == &(curbuf->b_p_ft))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004603 do_filetype_autocmd(varp, opt_flags, args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004604#ifdef FEAT_SPELL
4605 if (varp == &(curwin->w_s->b_p_spl))
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004606 do_spelllang_source();
Bram Moolenaardac13472019-09-16 21:06:21 +02004607#endif
4608 }
4609
Bram Moolenaardac13472019-09-16 21:06:21 +02004610 if (varp == &p_mouse)
4611 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004612 if (*p_mouse == NUL)
4613 mch_setmouse(FALSE); // switch mouse off
4614 else
Bram Moolenaardac13472019-09-16 21:06:21 +02004615 setmouse(); // in case 'mouse' changed
4616 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004617
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004618#if defined(FEAT_LUA) || defined(PROTO)
4619 if (varp == &p_rtp)
4620 update_package_paths_in_lua();
4621#endif
4622
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004623#if defined(FEAT_LINEBREAK)
4624 // Changing Formatlistpattern when briopt includes the list setting:
4625 // redraw
4626 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
4627 && curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004628 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004629#endif
4630
Bram Moolenaardac13472019-09-16 21:06:21 +02004631 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004632 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0
4633 && (get_option_flags(opt_idx) & P_HLONLY) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02004634 curwin->w_set_curswant = TRUE;
4635
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004636 if ((opt_flags & OPT_NO_REDRAW) == 0)
4637 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004638#ifdef FEAT_GUI
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004639 // set when changing an option that only requires a redraw in the GUI
4640 int redraw_gui_only = FALSE;
4641
4642 if (varp == &p_go // 'guioptions'
4643 || varp == &p_guifont // 'guifont'
4644# ifdef FEAT_GUI_TABLINE
4645 || varp == &p_gtl // 'guitablabel'
4646 || varp == &p_gtt // 'guitabtooltip'
4647# endif
4648# ifdef FEAT_XFONTSET
4649 || varp == &p_guifontset // 'guifontset'
4650# endif
4651 || varp == &p_guifontwide // 'guifontwide'
Erik S. V. Jansson2f026382024-02-26 22:23:05 +01004652# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004653 || varp == &p_guiligatures // 'guiligatures'
4654# endif
4655 )
4656 redraw_gui_only = TRUE;
4657
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004658 // check redraw when it's not a GUI option or the GUI is active.
4659 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02004660#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004661 check_redraw(get_option_flags(opt_idx));
4662 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004663
4664#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004665 if (args.os_did_swaptcap)
Bram Moolenaardac13472019-09-16 21:06:21 +02004666 {
4667 set_termname((char_u *)"win32");
4668 init_highlight(TRUE, FALSE);
4669 }
4670#endif
4671
4672 return errmsg;
4673}
4674
4675/*
4676 * Check an option that can be a range of string values.
4677 *
4678 * Return OK for correct value, FAIL otherwise.
4679 * Empty is always OK.
4680 */
4681 static int
4682check_opt_strings(
4683 char_u *val,
4684 char **values,
4685 int list) // when TRUE: accept a list of values
4686{
4687 return opt_strings_flags(val, values, NULL, list);
4688}
4689
4690/*
4691 * Handle an option that can be a range of string values.
4692 * Set a flag in "*flagp" for each string present.
4693 *
4694 * Return OK for correct value, FAIL otherwise.
4695 * Empty is always OK.
4696 */
4697 static int
4698opt_strings_flags(
4699 char_u *val, // new value
4700 char **values, // array of valid string values
4701 unsigned *flagp,
4702 int list) // when TRUE: accept a list of values
4703{
4704 int i;
4705 int len;
4706 unsigned new_flags = 0;
4707
4708 while (*val)
4709 {
4710 for (i = 0; ; ++i)
4711 {
4712 if (values[i] == NULL) // val not found in values[]
4713 return FAIL;
4714
4715 len = (int)STRLEN(values[i]);
4716 if (STRNCMP(values[i], val, len) == 0
4717 && ((list && val[len] == ',') || val[len] == NUL))
4718 {
4719 val += len + (val[len] == ',');
4720 new_flags |= (1 << i);
4721 break; // check next item in val list
4722 }
4723 }
4724 }
4725 if (flagp != NULL)
4726 *flagp = new_flags;
4727
4728 return OK;
4729}
4730
4731/*
4732 * return OK if "p" is a valid fileformat name, FAIL otherwise.
4733 */
4734 int
4735check_ff_value(char_u *p)
4736{
4737 return check_opt_strings(p, p_ff_values, FALSE);
4738}
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004739
4740/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004741 * Save the actual shortmess Flags and clear them temporarily to avoid that
4742 * file messages overwrites any output from the following commands.
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004743 *
4744 * Caller must make sure to first call save_clear_shm_value() and then
4745 * restore_shm_value() exactly the same number of times.
4746 */
4747 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004748save_clear_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004749{
4750 if (STRLEN(p_shm) >= SHM_LEN)
4751 {
4752 iemsg(e_internal_error_shortmess_too_long);
4753 return;
4754 }
4755
4756 if (++set_shm_recursive == 1)
4757 {
4758 STRCPY(shm_buf, p_shm);
4759 set_option_value_give_err((char_u *)"shm", 0L, (char_u *)"", 0);
4760 }
4761}
4762
4763/*
4764 * Restore the shortmess Flags set from the save_clear_shm_value() function.
4765 */
4766 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004767restore_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004768{
4769 if (--set_shm_recursive == 0)
4770 {
4771 set_option_value_give_err((char_u *)"shm", 0L, shm_buf, 0);
4772 vim_memset(shm_buf, 0, SHM_LEN);
4773 }
4774}