blob: 0554c0eb248b49d93ad951e38298508e53f28e17 [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
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +0100316 check_string_option(&buf->b_p_ffu);
Bram Moolenaardac13472019-09-16 21:06:21 +0200317 check_string_option(&buf->b_p_tfu);
318#endif
319#ifdef FEAT_KEYMAP
320 check_string_option(&buf->b_p_keymap);
321#endif
322#ifdef FEAT_QUICKFIX
323 check_string_option(&buf->b_p_gp);
324 check_string_option(&buf->b_p_mp);
325 check_string_option(&buf->b_p_efm);
326#endif
327 check_string_option(&buf->b_p_ep);
328 check_string_option(&buf->b_p_path);
329 check_string_option(&buf->b_p_tags);
330 check_string_option(&buf->b_p_tc);
331 check_string_option(&buf->b_p_dict);
332 check_string_option(&buf->b_p_tsr);
Bram Moolenaardac13472019-09-16 21:06:21 +0200333 check_string_option(&buf->b_p_lw);
Bram Moolenaardac13472019-09-16 21:06:21 +0200334 check_string_option(&buf->b_p_bkc);
335 check_string_option(&buf->b_p_menc);
336#ifdef FEAT_VARTABS
337 check_string_option(&buf->b_p_vsts);
338 check_string_option(&buf->b_p_vts);
339#endif
340}
341
342/*
343 * Free the string allocated for an option.
344 * Checks for the string being empty_option. This may happen if we're out of
345 * memory, vim_strsave() returned NULL, which was replaced by empty_option by
346 * check_options().
347 * Does NOT check for P_ALLOCED flag!
348 */
349 void
350free_string_option(char_u *p)
351{
352 if (p != empty_option)
353 vim_free(p);
354}
355
356 void
357clear_string_option(char_u **pp)
358{
359 if (*pp != empty_option)
360 vim_free(*pp);
361 *pp = empty_option;
362}
363
364 void
365check_string_option(char_u **pp)
366{
367 if (*pp == NULL)
368 *pp = empty_option;
369}
370
371/*
372 * Set global value for string option when it's a local option.
373 */
374 static void
375set_string_option_global(
376 int opt_idx, // option index
377 char_u **varp) // pointer to option variable
378{
379 char_u **p, *s;
380
381 // the global value is always allocated
382 if (is_window_local_option(opt_idx))
383 p = (char_u **)GLOBAL_WO(varp);
384 else
385 p = (char_u **)get_option_var(opt_idx);
386 if (!is_global_option(opt_idx)
387 && p != varp
388 && (s = vim_strsave(*varp)) != NULL)
389 {
390 free_string_option(*p);
391 *p = s;
392 }
393}
394
395/*
396 * Set a string option to a new value (without checking the effect).
397 * The string is copied into allocated memory.
398 * if ("opt_idx" == -1) "name" is used, otherwise "opt_idx" is used.
399 * When "set_sid" is zero set the scriptID to current_sctx.sc_sid. When
400 * "set_sid" is SID_NONE don't set the scriptID. Otherwise set the scriptID to
401 * "set_sid".
402 */
403 void
404set_string_option_direct(
405 char_u *name,
406 int opt_idx,
407 char_u *val,
408 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
409 int set_sid UNUSED)
410{
411 char_u *s;
412 char_u **varp;
413 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
414 int idx = opt_idx;
415
416 if (idx == -1) // use name
417 {
418 idx = findoption(name);
419 if (idx < 0) // not found (should not happen)
420 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000421 semsg(_(e_internal_error_str), "set_string_option_direct()");
RestorerZ68ebcee2023-05-31 17:12:14 +0100422 siemsg("For option %s", name);
Bram Moolenaardac13472019-09-16 21:06:21 +0200423 return;
424 }
425 }
426
427 if (is_hidden_option(idx)) // can't set hidden option
428 return;
429
430 s = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000431 if (s == NULL)
432 return;
433
434 varp = (char_u **)get_option_varp_scope(idx,
435 both ? OPT_LOCAL : opt_flags);
436 if ((opt_flags & OPT_FREE) && (get_option_flags(idx) & P_ALLOCED))
437 free_string_option(*varp);
438 *varp = s;
439
440 // For buffer/window local option may also set the global value.
441 if (both)
442 set_string_option_global(idx, varp);
443
444 set_option_flag(idx, P_ALLOCED);
445
446 // When setting both values of a global option with a local value,
447 // make the local value empty, so that the global value is used.
448 if (is_global_local_option(idx) && both)
Bram Moolenaardac13472019-09-16 21:06:21 +0200449 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000450 free_string_option(*varp);
451 *varp = empty_option;
Bram Moolenaardac13472019-09-16 21:06:21 +0200452 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000453# ifdef FEAT_EVAL
454 if (set_sid != SID_NONE)
455 {
456 sctx_T script_ctx;
457
458 if (set_sid == 0)
459 script_ctx = current_sctx;
460 else
461 {
462 script_ctx.sc_sid = set_sid;
463 script_ctx.sc_seq = 0;
464 script_ctx.sc_lnum = 0;
465 script_ctx.sc_version = 1;
466 }
467 set_option_sctx_idx(idx, opt_flags, script_ctx);
468 }
469# endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200470}
471
Dominique Pellee764d1b2023-03-12 21:20:59 +0000472#if defined(FEAT_PROP_POPUP) || \
473 (defined(FEAT_DIFF) && defined(FEAT_FOLDING)) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200474/*
475 * Like set_string_option_direct(), but for a window-local option in "wp".
476 * Blocks autocommands to avoid the old curwin becoming invalid.
477 */
478 void
479set_string_option_direct_in_win(
480 win_T *wp,
481 char_u *name,
482 int opt_idx,
483 char_u *val,
484 int opt_flags,
485 int set_sid)
486{
487 win_T *save_curwin = curwin;
488
489 block_autocmds();
490 curwin = wp;
491 curbuf = curwin->w_buffer;
492 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
493 curwin = save_curwin;
494 curbuf = curwin->w_buffer;
495 unblock_autocmds();
496}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000497#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200498
Dominique Pelle748b3082022-01-08 12:41:16 +0000499#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200500/*
501 * Like set_string_option_direct(), but for a buffer-local option in "buf".
502 * Blocks autocommands to avoid the old curbuf becoming invalid.
503 */
504 void
505set_string_option_direct_in_buf(
506 buf_T *buf,
507 char_u *name,
508 int opt_idx,
509 char_u *val,
510 int opt_flags,
511 int set_sid)
512{
513 buf_T *save_curbuf = curbuf;
514
515 block_autocmds();
516 curbuf = buf;
517 curwin->w_buffer = curbuf;
518 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
519 curbuf = save_curbuf;
520 curwin->w_buffer = curbuf;
521 unblock_autocmds();
522}
Dominique Pelle748b3082022-01-08 12:41:16 +0000523#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200524
525/*
526 * Set a string option to a new value, and handle the effects.
527 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100528 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaardac13472019-09-16 21:06:21 +0200529 */
530 char *
531set_string_option(
532 int opt_idx,
533 char_u *value,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +0000534 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Christian Brabandtb39b2402023-11-29 11:34:05 +0100535 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +0100536 size_t errbuflen)
Bram Moolenaardac13472019-09-16 21:06:21 +0200537{
538 char_u *s;
539 char_u **varp;
540 char_u *oldval;
541#if defined(FEAT_EVAL)
542 char_u *oldval_l = NULL;
543 char_u *oldval_g = NULL;
544 char_u *saved_oldval = NULL;
545 char_u *saved_oldval_l = NULL;
546 char_u *saved_oldval_g = NULL;
547 char_u *saved_newval = NULL;
548#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100549 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +0200550 int value_checked = FALSE;
551
552 if (is_hidden_option(opt_idx)) // don't set hidden option
553 return NULL;
554
Bram Moolenaar7f009df2020-03-16 20:27:38 +0100555 s = vim_strsave(value == NULL ? (char_u *)"" : value);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000556 if (s == NULL)
557 return NULL;
558
559 varp = (char_u **)get_option_varp_scope(opt_idx,
560 (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
561 ? (is_global_local_option(opt_idx)
562 ? OPT_GLOBAL : OPT_LOCAL)
563 : opt_flags);
564 oldval = *varp;
565#if defined(FEAT_EVAL)
566 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +0200567 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000568 oldval_l = *(char_u **)get_option_varp_scope(opt_idx, OPT_LOCAL);
569 oldval_g = *(char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
Bram Moolenaardac13472019-09-16 21:06:21 +0200570 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000571#endif
572 *varp = s;
573
574#if defined(FEAT_EVAL)
575 if (!starting
576# ifdef FEAT_CRYPT
577 && !is_crypt_key_option(opt_idx)
578# endif
579 )
580 {
581 if (oldval_l != NULL)
582 saved_oldval_l = vim_strsave(oldval_l);
583 if (oldval_g != NULL)
584 saved_oldval_g = vim_strsave(oldval_g);
585 saved_oldval = vim_strsave(oldval);
586 saved_newval = vim_strsave(s);
587 }
588#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000589 if ((errmsg = did_set_string_option(opt_idx, varp, oldval, value, errbuf,
Christian Brabandtb39b2402023-11-29 11:34:05 +0100590 errbuflen, opt_flags, OP_NONE, &value_checked)) == NULL)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000591 did_set_option(opt_idx, opt_flags, TRUE, value_checked);
592
593#if defined(FEAT_EVAL)
594 // call autocommand after handling side effects
595 if (errmsg == NULL)
596 trigger_optionset_string(opt_idx, opt_flags,
597 saved_oldval, saved_oldval_l,
598 saved_oldval_g, saved_newval);
599 vim_free(saved_oldval);
600 vim_free(saved_oldval_l);
601 vim_free(saved_oldval_g);
602 vim_free(saved_newval);
603#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100604 return errmsg;
Bram Moolenaardac13472019-09-16 21:06:21 +0200605}
606
607/*
608 * Return TRUE if "val" is a valid 'filetype' name.
609 * Also used for 'syntax' and 'keymap'.
610 */
611 static int
612valid_filetype(char_u *val)
613{
614 return valid_name(val, ".-_");
615}
616
617#ifdef FEAT_STL_OPT
618/*
619 * Check validity of options with the 'statusline' format.
zeertzjq5dc294a2022-04-15 13:17:57 +0100620 * Return an untranslated error message or NULL.
Bram Moolenaardac13472019-09-16 21:06:21 +0200621 */
622 static char *
623check_stl_option(char_u *s)
624{
Bram Moolenaardac13472019-09-16 21:06:21 +0200625 int groupdepth = 0;
Christian Brabandtb39b2402023-11-29 11:34:05 +0100626 static char errbuf[ERR_BUFLEN];
627 int errbuflen = ERR_BUFLEN;
Bram Moolenaardac13472019-09-16 21:06:21 +0200628
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100629 while (*s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200630 {
631 // Check for valid keys after % sequences
632 while (*s && *s != '%')
633 s++;
634 if (!*s)
635 break;
636 s++;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +0000637 if (*s == '%' || *s == STL_TRUNCMARK || *s == STL_SEPARATE)
Bram Moolenaardac13472019-09-16 21:06:21 +0200638 {
639 s++;
640 continue;
641 }
642 if (*s == ')')
643 {
644 s++;
645 if (--groupdepth < 0)
646 break;
647 continue;
648 }
649 if (*s == '-')
650 s++;
651 while (VIM_ISDIGIT(*s))
652 s++;
653 if (*s == STL_USER_HL)
654 continue;
655 if (*s == '.')
656 {
657 s++;
658 while (*s && VIM_ISDIGIT(*s))
659 s++;
660 }
661 if (*s == '(')
662 {
663 groupdepth++;
664 continue;
665 }
666 if (vim_strchr(STL_ALL, *s) == NULL)
667 {
Christian Brabandtb39b2402023-11-29 11:34:05 +0100668 return illegal_char(errbuf, errbuflen, *s);
Bram Moolenaardac13472019-09-16 21:06:21 +0200669 }
670 if (*s == '{')
671 {
zeertzjq5dc294a2022-04-15 13:17:57 +0100672 int reevaluate = (*++s == '%');
shadmansaleh30e3de22021-05-15 17:23:28 +0200673
zeertzjq5dc294a2022-04-15 13:17:57 +0100674 if (reevaluate && *++s == '}')
675 // "}" is not allowed immediately after "%{%"
Christian Brabandtb39b2402023-11-29 11:34:05 +0100676 return illegal_char(errbuf, errbuflen, '}');
shadmansaleh30e3de22021-05-15 17:23:28 +0200677 while ((*s != '}' || (reevaluate && s[-1] != '%')) && *s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200678 s++;
679 if (*s != '}')
zeertzjq5dc294a2022-04-15 13:17:57 +0100680 return e_unclosed_expression_sequence;
Bram Moolenaardac13472019-09-16 21:06:21 +0200681 }
682 }
Bram Moolenaardac13472019-09-16 21:06:21 +0200683 if (groupdepth != 0)
zeertzjq5dc294a2022-04-15 13:17:57 +0100684 return e_unbalanced_groups;
Bram Moolenaardac13472019-09-16 21:06:21 +0200685 return NULL;
686}
687#endif
688
689/*
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +0000690 * Check for a "normal" directory or file name in some options. Disallow a
691 * path separator (slash and/or backslash), wildcards and characters that are
692 * often illegal in a file name. Be more permissive if "secure" is off.
693 */
694 static int
695check_illegal_path_names(int opt_idx, char_u **varp)
696{
697 return (((get_option_flags(opt_idx) & P_NFNAME)
698 && vim_strpbrk(*varp, (char_u *)(secure
699 ? "/\\*?[|;&<>\r\n" : "/\\*?[<>\r\n")) != NULL)
700 || ((get_option_flags(opt_idx) & P_NDNAME)
701 && vim_strpbrk(*varp, (char_u *)"*?[|;&<>\r\n") != NULL));
702}
703
704/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000705 * An option that accepts a list of flags is changed.
706 * e.g. 'viewoptions', 'switchbuf', 'casemap', etc.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000707 */
708 static char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000709did_set_opt_flags(char_u *val, char **values, unsigned *flagp, int list)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000710{
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000711 if (opt_strings_flags(val, values, flagp, list) == FAIL)
712 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000713
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000714 return NULL;
715}
716
717/*
718 * An option that accepts a list of string values is changed.
719 * e.g. 'nrformats', 'scrollopt', 'wildoptions', etc.
720 */
721 static char *
722did_set_opt_strings(char_u *val, char **values, int list)
723{
724 return did_set_opt_flags(val, values, NULL, list);
725}
726
727/*
728 * An option which is a list of flags is set. Valid values are in 'flags'.
729 */
730 static char *
Christian Brabandtb39b2402023-11-29 11:34:05 +0100731did_set_option_listflag(
732 char_u *val,
733 char_u *flags,
734 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +0100735 size_t errbuflen)
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000736{
737 char_u *s;
738
Yegappan Lakshmananc727b192023-03-03 12:26:15 +0000739 for (s = val; *s; ++s)
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000740 if (vim_strchr(flags, *s) == NULL)
Christian Brabandtb39b2402023-11-29 11:34:05 +0100741 return illegal_char(errbuf, errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000742
743 return NULL;
744}
745
746/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200747 * Expand an option that accepts a list of fixed string values with known
748 * number of items.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200749 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200750 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200751expand_set_opt_string(
752 optexpand_T *args,
753 char **values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +0200754 size_t numValues,
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200755 int *numMatches,
756 char_u ***matches)
757{
758 char_u *p;
759 regmatch_T *regmatch = args->oe_regmatch;
760 int include_orig_val = args->oe_include_orig_val;
761 char_u *option_val = args->oe_opt_value;
762
763 // Assume numValues is small since they are fixed enums, so just allocate
764 // upfront instead of needing two passes to calculate output size.
765 *matches = ALLOC_MULT(char_u *, numValues + 1);
766 if (*matches == NULL)
767 return FAIL;
768
769 int count = 0;
770
771 if (include_orig_val && *option_val != NUL)
772 {
773 p = vim_strsave(option_val);
774 if (p == NULL)
775 {
776 VIM_CLEAR(*matches);
777 return FAIL;
778 }
779 (*matches)[count++] = p;
780 }
781
782 for (char **val = values; *val != NULL; val++)
783 {
784 if (include_orig_val && *option_val != NUL)
785 {
786 if (STRCMP((char_u*)*val, option_val) == 0)
787 continue;
788 }
789 if (vim_regexec(regmatch, (char_u*)(*val), (colnr_T)0))
790 {
791 p = vim_strsave((char_u*)*val);
792 if (p == NULL)
793 {
794 if (count == 0)
795 {
796 VIM_CLEAR(*matches);
797 return FAIL;
798 }
799 else
800 break;
801 }
802 (*matches)[count++] = p;
803 }
804 }
805 if (count == 0)
806 {
807 VIM_CLEAR(*matches);
808 return FAIL;
809 }
810 *numMatches = count;
811 return OK;
812}
813
814static char_u *set_opt_callback_orig_option = NULL;
815static char_u *((*set_opt_callback_func)(expand_T *, int));
816
817/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200818 * Callback used by expand_set_opt_generic to also include the original value
819 * as the first item.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200820 */
821 static char_u *
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200822expand_set_opt_generic_cb(expand_T *xp, int idx)
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200823{
824 if (idx == 0)
825 {
826 if (set_opt_callback_orig_option != NULL)
827 return set_opt_callback_orig_option;
828 else
829 return (char_u *)""; // empty strings are ignored
830 }
831 return set_opt_callback_func(xp, idx - 1);
832}
833
834/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200835 * Expand an option with a callback that iterates through a list of possible
836 * names using an index.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200837 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200838 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200839expand_set_opt_generic(
840 optexpand_T *args,
841 char_u *((*func)(expand_T *, int)),
842 int *numMatches,
843 char_u ***matches)
844{
845 int ret;
846
847 set_opt_callback_orig_option = args->oe_include_orig_val ?
848 args->oe_opt_value : NULL;
849 set_opt_callback_func = func;
850
851 ret = ExpandGeneric(
852 (char_u*)"", // not using fuzzy as currently EXPAND_STRING_SETTING doesn't use it
853 args->oe_xp,
854 args->oe_regmatch,
855 matches,
856 numMatches,
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200857 expand_set_opt_generic_cb,
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200858 FALSE);
859
860 set_opt_callback_orig_option = NULL;
861 set_opt_callback_func = NULL;
862 return ret;
863}
864
Christian Brabandt9960ebc2023-10-05 22:17:09 +0200865# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200866static garray_T *expand_cb_ga;
867static optexpand_T *expand_cb_args;
868
869/*
870 * Callback provided to a function in expand_set_opt_callback. Will perform
871 * regex matching against the value and add to the list.
872 *
873 * Returns OK usually. Returns FAIL if it failed to allocate memory, and the
874 * caller should terminate the enumeration.
875 */
876 static int
877expand_set_opt_callback_cb(char_u *val)
878{
879 regmatch_T *regmatch = expand_cb_args->oe_regmatch;
880 expand_T *xp = expand_cb_args->oe_xp;
881 garray_T *ga = expand_cb_ga;
882 char_u *str;
883
884 if (val == NULL || *val == NUL)
885 return OK;
886
887 if (xp->xp_pattern[0] != NUL &&
888 !vim_regexec(regmatch, val, (colnr_T)0))
889 return OK;
890
891 str = vim_strsave_escaped(val, (char_u *)" \t\\");
892
893 if (str == NULL)
894 return FAIL;
895
896 if (ga_grow(ga, 1) == FAIL)
897 {
898 vim_free(str);
899 return FAIL;
900 }
901
902 ((char_u **)ga->ga_data)[ga->ga_len] = str;
903 ++ga->ga_len;
904 return OK;
905}
906
907/*
908 * Expand an option with a provided function that takes a callback. The
909 * function will enumerate through all options and call the callback to add it
910 * to the list.
911 *
912 * "func" is the enumerator function that will generate the list of options.
913 * "func_params" is a single parameter that will be passed to func.
914 */
915 static int
916expand_set_opt_callback(
917 optexpand_T *args,
918 void (*func)(optexpand_T *, void* params, int (*cb)(char_u *val)),
919 void *func_params,
920 int *numMatches,
921 char_u ***matches)
922{
923 garray_T ga;
924 int include_orig_val = args->oe_include_orig_val;
925 char_u *option_val = args->oe_opt_value;
926
927 ga_init2(&ga, sizeof(char *), 30);
928
929 if (include_orig_val && *option_val != NUL)
930 {
931 char_u *p = vim_strsave(option_val);
932 if (p == NULL)
933 return FAIL;
934 if (ga_grow(&ga, 1) == FAIL)
935 {
936 vim_free(p);
937 return FAIL;
938 }
939 ((char_u **)ga.ga_data)[ga.ga_len] = p;
940 ++ga.ga_len;
941 }
942
943 expand_cb_ga = &ga;
944 expand_cb_args = args;
945
946 func(args, func_params, expand_set_opt_callback_cb);
947
948 expand_cb_ga = NULL;
949 expand_cb_args = NULL;
950
951 *matches = ga.ga_data;
952 *numMatches = ga.ga_len;
953 return OK;
954}
Christian Brabandt9960ebc2023-10-05 22:17:09 +0200955#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200956
957/*
958 * Expand an option which is a list of flags.
959 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200960 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200961expand_set_opt_listflag(
962 optexpand_T *args,
963 char_u *flags,
964 int *numMatches,
965 char_u ***matches)
966{
967 char_u *p;
968 char_u *option_val = args->oe_opt_value;
969 char_u *cmdline_val = args->oe_set_arg;
970 int append = args->oe_append;
971 int include_orig_val = args->oe_include_orig_val && (*option_val != NUL);
972
Yee Cheng Chin6d113472023-10-02 21:38:39 +0200973 size_t num_flags = STRLEN(flags);
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200974
975 // Assume we only have small number of flags, so just allocate max size.
976 *matches = ALLOC_MULT(char_u *, num_flags + 1);
977 if (*matches == NULL)
978 return FAIL;
979
980 int count = 0;
981
982 if (include_orig_val)
983 {
984 p = vim_strsave(option_val);
985 if (p == NULL)
986 {
987 VIM_CLEAR(*matches);
988 return FAIL;
989 }
990 (*matches)[count++] = p;
991 }
992
993 for (char_u *flag = flags; *flag != NUL; flag++)
994 {
995 if (append && vim_strchr(option_val, *flag) != NULL)
996 continue;
997
998 if (vim_strchr(cmdline_val, *flag) == NULL)
999 {
1000 if (include_orig_val
1001 && option_val[1] == NUL
1002 && *flag == option_val[0])
1003 {
1004 // This value is already used as the first choice as it's the
1005 // existing flag. Just skip it to avoid duplicate.
1006 continue;
1007 }
1008 p = vim_strnsave(flag, 1);
1009 if (p == NULL)
1010 {
1011 if (count == 0)
1012 {
1013 VIM_CLEAR(*matches);
1014 return FAIL;
1015 }
1016 else
1017 break;
1018 }
1019 (*matches)[count++] = p;
1020 }
1021 }
1022
1023 if (count == 0)
1024 {
1025 VIM_CLEAR(*matches);
1026 return FAIL;
1027 }
1028 *numMatches = count;
1029 return OK;
1030}
1031
1032/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001033 * The 'ambiwidth' option is changed.
1034 */
1035 char *
1036did_set_ambiwidth(optset_T *args UNUSED)
1037{
1038 if (check_opt_strings(p_ambw, p_ambw_values, FALSE) != OK)
1039 return e_invalid_argument;
1040
1041 return check_chars_options();
1042}
1043
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001044 int
1045expand_set_ambiwidth(optexpand_T *args, int *numMatches, char_u ***matches)
1046{
1047 return expand_set_opt_string(
1048 args,
1049 p_ambw_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001050 ARRAY_LENGTH(p_ambw_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001051 numMatches,
1052 matches);
1053}
1054
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001055/*
1056 * The 'background' option is changed.
1057 */
1058 char *
Gregory Anders83ad2722024-01-03 19:48:51 +01001059did_set_background(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001060{
1061 if (check_opt_strings(p_bg, p_bg_values, FALSE) == FAIL)
1062 return e_invalid_argument;
1063
Gregory Anders83ad2722024-01-03 19:48:51 +01001064 if (args->os_oldval.string != NULL && args->os_oldval.string[0] == *p_bg)
1065 // Value was not changed
1066 return NULL;
1067
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001068#ifdef FEAT_EVAL
1069 int dark = (*p_bg == 'd');
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001070#endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001071
1072 init_highlight(FALSE, FALSE);
1073
1074#ifdef FEAT_EVAL
1075 if (dark != (*p_bg == 'd')
1076 && get_var_value((char_u *)"g:colors_name") != NULL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001077 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001078 // The color scheme must have set 'background' back to another
1079 // value, that's not what we want here. Disable the color
1080 // scheme and set the colors again.
1081 do_unlet((char_u *)"g:colors_name", TRUE);
1082 free_string_option(p_bg);
1083 p_bg = vim_strsave((char_u *)(dark ? "dark" : "light"));
1084 check_string_option(&p_bg);
1085 init_highlight(FALSE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001086 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001087#endif
1088#ifdef FEAT_TERMINAL
1089 term_update_colors_all();
1090#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001091
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001092 return NULL;
1093}
1094
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001095 int
1096expand_set_background(optexpand_T *args, int *numMatches, char_u ***matches)
1097{
1098 return expand_set_opt_string(
1099 args,
1100 p_bg_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001101 ARRAY_LENGTH(p_bg_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001102 numMatches,
1103 matches);
1104}
1105
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001106/*
1107 * The 'backspace' option is changed.
1108 */
1109 char *
1110did_set_backspace(optset_T *args UNUSED)
1111{
1112 if (VIM_ISDIGIT(*p_bs))
1113 {
1114 if (*p_bs > '3' || p_bs[1] != NUL)
1115 return e_invalid_argument;
1116 }
1117 else if (check_opt_strings(p_bs, p_bs_values, TRUE) != OK)
1118 return e_invalid_argument;
1119
1120 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001121}
1122
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001123 int
1124expand_set_backspace(optexpand_T *args, int *numMatches, char_u ***matches)
1125{
1126 return expand_set_opt_string(
1127 args,
1128 p_bs_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001129 ARRAY_LENGTH(p_bs_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001130 numMatches,
1131 matches);
1132}
1133
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001134/*
1135 * The 'backupcopy' option is changed.
1136 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001137 char *
1138did_set_backupcopy(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001139{
1140 char_u *bkc = p_bkc;
1141 unsigned int *flags = &bkc_flags;
1142 char *errmsg = NULL;
1143
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001144 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001145 {
1146 bkc = curbuf->b_p_bkc;
1147 flags = &curbuf->b_bkc_flags;
1148 }
1149
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001150 if ((args->os_flags & OPT_LOCAL) && *bkc == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001151 // make the local value empty: use the global value
1152 *flags = 0;
1153 else
1154 {
1155 if (opt_strings_flags(bkc, p_bkc_values, flags, TRUE) != OK)
1156 errmsg = e_invalid_argument;
1157 if ((((int)*flags & BKC_AUTO) != 0)
1158 + (((int)*flags & BKC_YES) != 0)
1159 + (((int)*flags & BKC_NO) != 0) != 1)
1160 {
1161 // Must have exactly one of "auto", "yes" and "no".
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001162 (void)opt_strings_flags(args->os_oldval.string, p_bkc_values,
1163 flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001164 errmsg = e_invalid_argument;
1165 }
1166 }
1167
1168 return errmsg;
1169}
1170
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001171 int
1172expand_set_backupcopy(optexpand_T *args, int *numMatches, char_u ***matches)
1173{
1174 return expand_set_opt_string(
1175 args,
1176 p_bkc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001177 ARRAY_LENGTH(p_bkc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001178 numMatches,
1179 matches);
1180}
1181
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001182/*
1183 * The 'backupext' or the 'patchmode' option is changed.
1184 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001185 char *
1186did_set_backupext_or_patchmode(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001187{
1188 if (STRCMP(*p_bex == '.' ? p_bex + 1 : p_bex,
1189 *p_pm == '.' ? p_pm + 1 : p_pm) == 0)
1190 return e_backupext_and_patchmode_are_equal;
1191
1192 return NULL;
1193}
1194
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001195/*
1196 * The 'belloff' option is changed.
1197 */
1198 char *
1199did_set_belloff(optset_T *args UNUSED)
1200{
1201 return did_set_opt_flags(p_bo, p_bo_values, &bo_flags, TRUE);
1202}
1203
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001204 int
1205expand_set_belloff(optexpand_T *args, int *numMatches, char_u ***matches)
1206{
1207 return expand_set_opt_string(
1208 args,
1209 p_bo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001210 ARRAY_LENGTH(p_bo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001211 numMatches,
1212 matches);
1213}
1214
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001215#if defined(FEAT_LINEBREAK) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001216/*
zeertzjqeac3fdc2024-02-03 18:08:09 +01001217 * The 'breakat' option is changed.
1218 */
1219 char *
1220did_set_breakat(optset_T *args UNUSED)
1221{
1222 char_u *p;
1223 int i;
1224
1225 for (i = 0; i < 256; i++)
1226 breakat_flags[i] = FALSE;
1227
1228 if (p_breakat != NULL)
1229 for (p = p_breakat; *p; p++)
1230 breakat_flags[*p] = TRUE;
1231
1232 return NULL;
1233}
1234
1235/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001236 * The 'breakindentopt' option is changed.
1237 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001238 char *
Millyb38700a2024-10-22 22:59:39 +02001239did_set_breakindentopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001240{
Millyb38700a2024-10-22 22:59:39 +02001241 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001242
Millyb38700a2024-10-22 22:59:39 +02001243 if (briopt_check(*varp, varp == &curwin->w_p_briopt ? curwin : NULL)
1244 == FAIL)
1245 return e_invalid_argument;
1246
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001247 // list setting requires a redraw
Millyb38700a2024-10-22 22:59:39 +02001248 if (varp == &curwin->w_p_briopt && curwin->w_briopt_list)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001249 redraw_all_later(UPD_NOT_VALID);
1250
Millyb38700a2024-10-22 22:59:39 +02001251 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001252}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001253
1254 int
1255expand_set_breakindentopt(optexpand_T *args, int *numMatches, char_u ***matches)
1256{
1257 return expand_set_opt_string(
1258 args,
1259 p_briopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001260 ARRAY_LENGTH(p_briopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001261 numMatches,
1262 matches);
1263}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001264#endif
1265
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001266#if defined(FEAT_BROWSE) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001267/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001268 * The 'browsedir' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001269 */
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00001270 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001271did_set_browsedir(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001272{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001273 if (check_opt_strings(p_bsdir, p_bsdir_values, FALSE) != OK
1274 && !mch_isdir(p_bsdir))
1275 return e_invalid_argument;
1276
1277 return NULL;
1278}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001279
1280 int
1281expand_set_browsedir(optexpand_T *args, int *numMatches, char_u ***matches)
1282{
1283 return expand_set_opt_string(
1284 args,
1285 p_bsdir_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001286 ARRAY_LENGTH(p_bsdir_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001287 numMatches,
1288 matches);
1289}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001290#endif
1291
1292/*
1293 * The 'bufhidden' option is changed.
1294 */
1295 char *
1296did_set_bufhidden(optset_T *args UNUSED)
1297{
1298 return did_set_opt_strings(curbuf->b_p_bh, p_bufhidden_values, FALSE);
1299}
1300
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001301 int
1302expand_set_bufhidden(optexpand_T *args, int *numMatches, char_u ***matches)
1303{
1304 return expand_set_opt_string(
1305 args,
1306 p_bufhidden_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001307 ARRAY_LENGTH(p_bufhidden_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001308 numMatches,
1309 matches);
1310}
1311
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001312/*
1313 * The 'buftype' option is changed.
1314 */
1315 char *
1316did_set_buftype(optset_T *args UNUSED)
1317{
1318 if (check_opt_strings(curbuf->b_p_bt, p_buftype_values, FALSE) != OK)
1319 return e_invalid_argument;
1320
1321 if (curwin->w_status_height)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001322 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001323 curwin->w_redr_status = TRUE;
1324 redraw_later(UPD_VALID);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001325 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001326 curbuf->b_help = (curbuf->b_p_bt[0] == 'h');
1327 redraw_titles();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001328
1329 return NULL;
1330}
1331
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001332 int
1333expand_set_buftype(optexpand_T *args, int *numMatches, char_u ***matches)
1334{
1335 return expand_set_opt_string(
1336 args,
1337 p_buftype_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001338 ARRAY_LENGTH(p_buftype_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001339 numMatches,
1340 matches);
1341}
1342
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001343/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001344 * The 'casemap' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001345 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001346 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001347did_set_casemap(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001348{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001349 return did_set_opt_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
1350}
1351
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001352 int
1353expand_set_casemap(optexpand_T *args, int *numMatches, char_u ***matches)
1354{
1355 return expand_set_opt_string(
1356 args,
1357 p_cmp_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001358 ARRAY_LENGTH(p_cmp_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001359 numMatches,
1360 matches);
1361}
1362
1363#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1364 int
1365expand_set_clipboard(optexpand_T *args, int *numMatches, char_u ***matches)
1366{
1367 return expand_set_opt_string(
1368 args,
1369 p_cb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001370 ARRAY_LENGTH(p_cb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001371 numMatches,
1372 matches);
1373}
1374#endif
1375
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001376/*
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001377 * The global 'listchars' or 'fillchars' option is changed.
1378 */
1379 static char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01001380did_set_global_listfillchars(char_u *val, int opt_lcs, int opt_flags,
1381 char *errbuf, size_t errbuflen)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001382{
1383 char *errmsg = NULL;
1384 char_u **local_ptr = opt_lcs ? &curwin->w_p_lcs : &curwin->w_p_fcs;
1385
1386 // only apply the global value to "curwin" when it does not have a
1387 // local value
1388 if (opt_lcs)
1389 errmsg = set_listchars_option(curwin, val,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001390 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL),
1391 errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001392 else
1393 errmsg = set_fillchars_option(curwin, val,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001394 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL),
1395 errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001396 if (errmsg != NULL)
1397 return errmsg;
1398
1399 tabpage_T *tp;
1400 win_T *wp;
1401
1402 // If the current window is set to use the global
1403 // 'listchars'/'fillchars' value, clear the window-local value.
1404 if (!(opt_flags & OPT_GLOBAL))
1405 clear_string_option(local_ptr);
1406 FOR_ALL_TAB_WINDOWS(tp, wp)
1407 {
1408 // If the current window has a local value need to apply it
1409 // again, it was changed when setting the global value.
1410 // If no error was returned above, we don't expect an error
1411 // here, so ignore the return value.
1412 if (opt_lcs)
1413 {
1414 if (*wp->w_p_lcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001415 (void)set_listchars_option(wp, wp->w_p_lcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001416 }
1417 else
1418 {
1419 if (*wp->w_p_fcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001420 (void)set_fillchars_option(wp, wp->w_p_fcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001421 }
1422 }
1423
1424 redraw_all_later(UPD_NOT_VALID);
1425
1426 return NULL;
1427}
1428
1429/*
1430 * The 'fillchars' option or the 'listchars' option is changed.
1431 */
1432 char *
1433did_set_chars_option(optset_T *args)
1434{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001435 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001436 char *errmsg = NULL;
1437
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001438 if ( varp == &p_lcs // global 'listchars'
1439 || varp == &p_fcs) // global 'fillchars'
1440 errmsg = did_set_global_listfillchars(*varp, varp == &p_lcs,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001441 args->os_flags, args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001442 else if (varp == &curwin->w_p_lcs) // local 'listchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001443 errmsg = set_listchars_option(curwin, *varp, TRUE,
1444 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001445 else if (varp == &curwin->w_p_fcs) // local 'fillchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001446 errmsg = set_fillchars_option(curwin, *varp, TRUE,
1447 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001448
1449 return errmsg;
1450}
1451
1452/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001453 * Expand 'fillchars' or 'listchars' option value.
1454 */
1455 int
1456expand_set_chars_option(optexpand_T *args, int *numMatches, char_u ***matches)
1457{
1458 char_u **varp = (char_u **)args->oe_varp;
1459 int is_lcs = (varp == &p_lcs || varp == &curwin->w_p_lcs);
1460 return expand_set_opt_generic(
1461 args,
1462 is_lcs ? get_listchars_name : get_fillchars_name,
1463 numMatches,
1464 matches);
1465}
1466
1467/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001468 * The 'cinoptions' option is changed.
1469 */
1470 char *
1471did_set_cinoptions(optset_T *args UNUSED)
1472{
1473 // TODO: recognize errors
1474 parse_cino(curbuf);
1475
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001476 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001477}
1478
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00001479#if defined(FEAT_SYN_HL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001480/*
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001481 * The 'colorcolumn' option is changed.
1482 */
1483 char *
Millya441a3e2024-10-22 22:43:01 +02001484did_set_colorcolumn(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001485{
Millya441a3e2024-10-22 22:43:01 +02001486 char_u **varp = (char_u **)args->os_varp;
1487
1488 return check_colorcolumn(*varp, varp == &curwin->w_p_cc ? curwin : NULL);
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001489}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001490#endif
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001491
1492/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001493 * The 'comments' option is changed.
1494 */
1495 char *
1496did_set_comments(optset_T *args)
1497{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001498 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001499 char_u *s;
1500 char *errmsg = NULL;
1501
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001502 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001503 {
1504 while (*s && *s != ':')
1505 {
1506 if (vim_strchr((char_u *)COM_ALL, *s) == NULL
1507 && !VIM_ISDIGIT(*s) && *s != '-')
1508 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01001509 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001510 break;
1511 }
1512 ++s;
1513 }
1514 if (*s++ == NUL)
1515 errmsg = e_missing_colon;
1516 else if (*s == ',' || *s == NUL)
1517 errmsg = e_zero_length_string;
1518 if (errmsg != NULL)
1519 break;
1520 while (*s && *s != ',')
1521 {
1522 if (*s == '\\' && s[1] != NUL)
1523 ++s;
1524 ++s;
1525 }
1526 s = skip_to_option_part(s);
1527 }
1528
1529 return errmsg;
1530}
1531
1532#if defined(FEAT_FOLDING) || defined(PROTO)
1533/*
1534 * The 'commentstring' option is changed.
1535 */
1536 char *
1537did_set_commentstring(optset_T *args)
1538{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001539 char_u **varp = (char_u **)args->os_varp;
1540
1541 if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001542 return e_commentstring_must_be_empty_or_contain_str;
1543
1544 return NULL;
1545}
1546#endif
1547
1548/*
1549 * The 'complete' option is changed.
1550 */
1551 char *
1552did_set_complete(optset_T *args)
1553{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001554 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001555 char_u *s;
1556
1557 // check if it is a valid value for 'complete' -- Acevedo
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001558 for (s = *varp; *s;)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001559 {
1560 while (*s == ',' || *s == ' ')
1561 s++;
1562 if (!*s)
1563 break;
1564 if (vim_strchr((char_u *)".wbuksid]tU", *s) == NULL)
Christian Brabandtb39b2402023-11-29 11:34:05 +01001565 return illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001566 if (*++s != NUL && *s != ',' && *s != ' ')
1567 {
1568 if (s[-1] == 'k' || s[-1] == 's')
1569 {
1570 // skip optional filename after 'k' and 's'
1571 while (*s && *s != ',' && *s != ' ')
1572 {
1573 if (*s == '\\' && s[1] != NUL)
1574 ++s;
1575 ++s;
1576 }
1577 }
1578 else
1579 {
1580 if (args->os_errbuf != NULL)
1581 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01001582 vim_snprintf((char *)args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001583 _(e_illegal_character_after_chr), *--s);
1584 return args->os_errbuf;
1585 }
1586 return "";
1587 }
1588 }
1589 }
1590
1591 return NULL;
1592}
1593
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001594 int
1595expand_set_complete(optexpand_T *args, int *numMatches, char_u ***matches)
1596{
1597 static char *(p_cpt_values[]) = {
1598 ".", "w", "b", "u", "k", "kspell", "s", "i", "d", "]", "t", "U",
1599 NULL};
1600 return expand_set_opt_string(
1601 args,
1602 p_cpt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001603 ARRAY_LENGTH(p_cpt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001604 numMatches,
1605 matches);
1606}
1607
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001608/*
1609 * The 'completeopt' option is changed.
1610 */
1611 char *
1612did_set_completeopt(optset_T *args UNUSED)
1613{
zeertzjq529b9ad2024-06-05 20:27:06 +02001614 char_u *cot = p_cot;
1615 unsigned *flags = &cot_flags;
1616
1617 if (args->os_flags & OPT_LOCAL)
1618 {
1619 cot = curbuf->b_p_cot;
1620 flags = &curbuf->b_cot_flags;
1621 }
1622
1623 if (check_opt_strings(cot, p_cot_values, TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001624 return e_invalid_argument;
1625
zeertzjq529b9ad2024-06-05 20:27:06 +02001626 if (opt_strings_flags(cot, p_cot_values, flags, TRUE) != OK)
1627 return e_invalid_argument;
1628
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001629 return NULL;
1630}
1631
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001632 int
1633expand_set_completeopt(optexpand_T *args, int *numMatches, char_u ***matches)
1634{
1635 return expand_set_opt_string(
1636 args,
1637 p_cot_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001638 ARRAY_LENGTH(p_cot_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001639 numMatches,
1640 matches);
1641}
1642
glepnir6a89c942024-10-01 20:32:12 +02001643/*
1644 * The 'completeitemalign' option is changed.
1645 */
1646 char *
1647did_set_completeitemalign(optset_T *args UNUSED)
1648{
1649 char_u *p = p_cia;
1650 unsigned new_cia_flags = 0;
1651 int seen[3] = { FALSE, FALSE, FALSE };
1652 int count = 0;
1653 char_u buf[10];
1654
1655 while (*p)
1656 {
1657 copy_option_part(&p, buf, sizeof(buf), ",");
1658 if (count >= 3)
1659 return e_invalid_argument;
1660
1661 if (STRCMP(buf, "abbr") == 0)
1662 {
1663 if (seen[CPT_ABBR])
1664 return e_invalid_argument;
1665 new_cia_flags = new_cia_flags * 10 + CPT_ABBR;
1666 seen[CPT_ABBR] = TRUE;
1667 count++;
1668 }
1669 else if (STRCMP(buf, "kind") == 0)
1670 {
1671 if (seen[CPT_KIND])
1672 return e_invalid_argument;
1673 new_cia_flags = new_cia_flags * 10 + CPT_KIND;
1674 seen[CPT_KIND] = TRUE;
1675 count++;
1676 }
1677 else if (STRCMP(buf, "menu") == 0)
1678 {
1679 if (seen[CPT_MENU])
1680 return e_invalid_argument;
1681 new_cia_flags = new_cia_flags * 10 + CPT_MENU;
1682 seen[CPT_MENU] = TRUE;
1683 count++;
1684 }
1685 else
1686 return e_invalid_argument;
1687 }
1688 if (new_cia_flags == 0 || count != 3)
1689 return e_invalid_argument;
1690
1691 cia_flags = new_cia_flags;
1692 return NULL;
1693}
1694
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001695#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1696/*
1697 * The 'completepopup' option is changed.
1698 */
1699 char *
1700did_set_completepopup(optset_T *args UNUSED)
1701{
1702 if (parse_completepopup(NULL) == FAIL)
1703 return e_invalid_argument;
1704
1705 popup_close_info();
1706 return NULL;
1707}
1708#endif
1709
1710#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1711/*
1712 * The 'completeslash' option is changed.
1713 */
1714 char *
1715did_set_completeslash(optset_T *args UNUSED)
1716{
1717 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1718 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
1719 return e_invalid_argument;
1720
1721 return NULL;
1722}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001723
1724 int
1725expand_set_completeslash(optexpand_T *args, int *numMatches, char_u ***matches)
1726{
1727 return expand_set_opt_string(
1728 args,
1729 p_csl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001730 ARRAY_LENGTH(p_csl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001731 numMatches,
1732 matches);
1733}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001734#endif
1735
1736#if defined(FEAT_CONCEAL) || defined(PROTO)
1737/*
1738 * The 'concealcursor' option is changed.
1739 */
1740 char *
1741did_set_concealcursor(optset_T *args)
1742{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001743 char_u **varp = (char_u **)args->os_varp;
1744
Christian Brabandtb39b2402023-11-29 11:34:05 +01001745 return did_set_option_listflag(*varp, (char_u *)COCU_ALL, args->os_errbuf,
1746 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001747}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001748
1749 int
1750expand_set_concealcursor(optexpand_T *args, int *numMatches, char_u ***matches)
1751{
1752 return expand_set_opt_listflag(args, (char_u*)COCU_ALL, numMatches, matches);
1753}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001754#endif
1755
1756/*
1757 * The 'cpoptions' option is changed.
1758 */
1759 char *
1760did_set_cpoptions(optset_T *args)
1761{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001762 char_u **varp = (char_u **)args->os_varp;
1763
Christian Brabandtb39b2402023-11-29 11:34:05 +01001764 return did_set_option_listflag(*varp, (char_u *)CPO_ALL, args->os_errbuf,
1765 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001766}
1767
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001768 int
1769expand_set_cpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
1770{
1771 return expand_set_opt_listflag(args, (char_u*)CPO_ALL, numMatches, matches);
1772}
1773
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001774#if defined(FEAT_CRYPT) || defined(PROTO)
1775/*
1776 * The 'cryptkey' option is changed.
1777 */
1778 char *
1779did_set_cryptkey(optset_T *args)
1780{
1781 // Make sure the ":set" command doesn't show the new value in the
1782 // history.
1783 remove_key_from_history();
1784
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02001785 if (args->os_op != OP_NONE)
1786 // Don't allow set+=/-=/^= as they can allow for substring guessing
1787 return e_invalid_argument;
1788
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001789 if (STRCMP(curbuf->b_p_key, args->os_oldval.string) != 0)
1790 {
1791 // Need to update the swapfile.
1792 ml_set_crypt_key(curbuf, args->os_oldval.string,
1793 *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
1794 changed_internal();
1795 }
Christian Brabandt19e6c4f2023-06-27 18:57:10 +01001796# ifdef FEAT_SODIUM
1797 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
1798 crypt_sodium_lock_key(args->os_newval.string);
1799# endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001800
1801 return NULL;
1802}
1803
1804/*
1805 * The 'cryptmethod' option is changed.
1806 */
1807 char *
1808did_set_cryptmethod(optset_T *args)
1809{
1810 char_u *p;
1811 char_u *s;
1812
1813 if (args->os_flags & OPT_LOCAL)
1814 p = curbuf->b_p_cm;
1815 else
1816 p = p_cm;
1817 if (check_opt_strings(p, p_cm_values, TRUE) != OK)
1818 return e_invalid_argument;
1819 else if (crypt_self_test() == FAIL)
1820 return e_invalid_argument;
1821
1822 // When setting the global value to empty, make it "zip".
1823 if (*p_cm == NUL)
1824 {
1825 free_string_option(p_cm);
1826 p_cm = vim_strsave((char_u *)"zip");
1827 }
1828 // When using ":set cm=name" the local value is going to be empty.
1829 // Do that here, otherwise the crypt functions will still use the
1830 // local value.
1831 if ((args->os_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1832 {
1833 free_string_option(curbuf->b_p_cm);
1834 curbuf->b_p_cm = empty_option;
1835 }
1836
1837 // Need to update the swapfile when the effective method changed.
1838 // Set "s" to the effective old value, "p" to the effective new
1839 // method and compare.
1840 if ((args->os_flags & OPT_LOCAL) && *args->os_oldval.string == NUL)
1841 s = p_cm; // was previously using the global value
1842 else
1843 s = args->os_oldval.string;
1844 if (*curbuf->b_p_cm == NUL)
1845 p = p_cm; // is now using the global value
1846 else
1847 p = curbuf->b_p_cm;
1848 if (STRCMP(s, p) != 0)
1849 ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1850
1851 // If the global value changes need to update the swapfile for all
1852 // buffers using that value.
1853 if ((args->os_flags & OPT_GLOBAL)
1854 && STRCMP(p_cm, args->os_oldval.string) != 0)
1855 {
1856 buf_T *buf;
1857
1858 FOR_ALL_BUFFERS(buf)
1859 if (buf != curbuf && *buf->b_p_cm == NUL)
1860 ml_set_crypt_key(buf, buf->b_p_key, args->os_oldval.string);
1861 }
1862 return NULL;
1863}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001864
1865 int
1866expand_set_cryptmethod(optexpand_T *args, int *numMatches, char_u ***matches)
1867{
1868 return expand_set_opt_string(
1869 args,
1870 p_cm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001871 ARRAY_LENGTH(p_cm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001872 numMatches,
1873 matches);
1874}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001875#endif
1876
1877#if (defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1878/*
1879 * The 'cscopequickfix' option is changed.
1880 */
1881 char *
1882did_set_cscopequickfix(optset_T *args UNUSED)
1883{
1884 char_u *p;
1885
1886 if (p_csqf == NULL)
1887 return NULL;
1888
1889 p = p_csqf;
1890 while (*p != NUL)
1891 {
1892 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
1893 || p[1] == NUL
1894 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
1895 || (p[2] != NUL && p[2] != ','))
1896 return e_invalid_argument;
1897 else if (p[2] == NUL)
1898 break;
1899 else
1900 p += 3;
1901 }
1902
1903 return NULL;
1904}
1905#endif
1906
1907#if defined(FEAT_SYN_HL) || defined(PROTO)
1908/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001909 * The 'cursorlineopt' option is changed.
1910 */
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001911 char *
1912did_set_cursorlineopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001913{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001914 char_u **varp = (char_u **)args->os_varp;
1915
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001916 // This could be changed to use opt_strings_flags() instead.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001917 if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001918 return e_invalid_argument;
1919
1920 return NULL;
1921}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001922
1923 int
1924expand_set_cursorlineopt(optexpand_T *args, int *numMatches, char_u ***matches)
1925{
1926 return expand_set_opt_string(
1927 args,
1928 p_culopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001929 ARRAY_LENGTH(p_culopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001930 numMatches,
1931 matches);
1932}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001933#endif
1934
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001935/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001936 * The 'debug' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001937 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001938 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001939did_set_debug(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001940{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001941 return did_set_opt_strings(p_debug, p_debug_values, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001942}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001943
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001944 int
1945expand_set_debug(optexpand_T *args, int *numMatches, char_u ***matches)
1946{
1947 return expand_set_opt_string(
1948 args,
1949 p_debug_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001950 ARRAY_LENGTH(p_debug_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001951 numMatches,
1952 matches);
1953}
1954
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001955#if defined(FEAT_DIFF) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001956/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001957 * The 'diffopt' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001958 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001959 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001960did_set_diffopt(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001961{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001962 if (diffopt_changed() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001963 return e_invalid_argument;
1964
1965 return NULL;
1966}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001967
1968 int
1969expand_set_diffopt(optexpand_T *args, int *numMatches, char_u ***matches)
1970{
1971 expand_T *xp = args->oe_xp;
1972
1973 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
1974 {
1975 // Within "algorithm:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001976 int algo_len = (int)STRLEN("algorithm:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001977 if (xp->xp_pattern - args->oe_set_arg >= algo_len &&
1978 STRNCMP(xp->xp_pattern - algo_len, "algorithm:", algo_len) == 0)
1979 {
1980 return expand_set_opt_string(
1981 args,
1982 p_dip_algorithm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001983 ARRAY_LENGTH(p_dip_algorithm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001984 numMatches,
1985 matches);
1986 }
1987 return FAIL;
1988 }
1989
1990 return expand_set_opt_string(
1991 args,
1992 p_dip_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001993 ARRAY_LENGTH(p_dip_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001994 numMatches,
1995 matches);
1996}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001997#endif
1998
1999/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002000 * The 'display' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002001 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002002 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002003did_set_display(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002004{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002005 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002006 return e_invalid_argument;
2007
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002008 (void)init_chartab();
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002009 return NULL;
2010}
2011
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002012 int
2013expand_set_display(optexpand_T *args, int *numMatches, char_u ***matches)
2014{
2015 return expand_set_opt_string(
2016 args,
2017 p_dy_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002018 ARRAY_LENGTH(p_dy_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002019 numMatches,
2020 matches);
2021}
2022
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002023/*
2024 * The 'eadirection' option is changed.
2025 */
2026 char *
2027did_set_eadirection(optset_T *args UNUSED)
2028{
2029 return did_set_opt_strings(p_ead, p_ead_values, FALSE);
2030}
2031
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002032 int
2033expand_set_eadirection(optexpand_T *args, int *numMatches, char_u ***matches)
2034{
2035 return expand_set_opt_string(
2036 args,
2037 p_ead_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002038 ARRAY_LENGTH(p_ead_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002039 numMatches,
2040 matches);
2041}
2042
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002043/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002044 * One of the 'encoding', 'fileencoding', 'termencoding' or 'makeencoding'
2045 * options is changed.
2046 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002047 char *
2048did_set_encoding(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002049{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002050 char_u **varp = (char_u **)args->os_varp;
2051 char_u **gvarp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002052 char *errmsg = NULL;
2053 char_u *p;
2054
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002055 // Get the global option to compare with, otherwise we would have to check
2056 // two values for all local options.
2057 gvarp = (char_u **)get_option_varp_scope(args->os_idx, OPT_GLOBAL);
2058
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002059 if (gvarp == &p_fenc)
2060 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002061 if (!curbuf->b_p_ma && args->os_flags != OPT_GLOBAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002062 errmsg = e_cannot_make_changes_modifiable_is_off;
2063 else if (vim_strchr(*varp, ',') != NULL)
2064 // No comma allowed in 'fileencoding'; catches confusing it
2065 // with 'fileencodings'.
2066 errmsg = e_invalid_argument;
2067 else
2068 {
2069 // May show a "+" in the title now.
2070 redraw_titles();
2071 // Add 'fileencoding' to the swap file.
2072 ml_setflags(curbuf);
2073 }
2074 }
2075 if (errmsg == NULL)
2076 {
2077 // canonize the value, so that STRCMP() can be used on it
2078 p = enc_canonize(*varp);
2079 if (p != NULL)
2080 {
2081 vim_free(*varp);
2082 *varp = p;
2083 }
2084 if (varp == &p_enc)
2085 {
2086 errmsg = mb_init();
2087 redraw_titles();
2088 }
2089 }
2090
2091#if defined(FEAT_GUI_GTK)
2092 if (errmsg == NULL && varp == &p_tenc && gui.in_use)
2093 {
2094 // GTK uses only a single encoding, and that is UTF-8.
2095 if (STRCMP(p_tenc, "utf-8") != 0)
2096 errmsg = e_cannot_be_changed_in_gtk_GUI;
2097 }
2098#endif
2099
2100 if (errmsg == NULL)
2101 {
2102#ifdef FEAT_KEYMAP
2103 // When 'keymap' is used and 'encoding' changes, reload the keymap
2104 // (with another encoding).
2105 if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
2106 (void)keymap_init();
2107#endif
2108
2109 // When 'termencoding' is not empty and 'encoding' changes or when
2110 // 'termencoding' changes, need to setup for keyboard input and
2111 // display output conversion.
2112 if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
2113 {
2114 if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
2115 || convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
2116 {
2117 semsg(_(e_cannot_convert_between_str_and_str),
2118 p_tenc, p_enc);
2119 errmsg = e_invalid_argument;
2120 }
2121 }
2122
2123#if defined(MSWIN)
K.Takatace3189d2023-02-15 19:13:43 +00002124 // $HOME, $VIM and $VIMRUNTIME may have characters in active code page.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002125 if (varp == &p_enc)
K.Takatace3189d2023-02-15 19:13:43 +00002126 {
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002127 init_homedir();
K.Takatace3189d2023-02-15 19:13:43 +00002128 init_vimdir();
2129 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002130#endif
2131 }
2132
2133 return errmsg;
2134}
2135
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002136 int
2137expand_set_encoding(optexpand_T *args, int *numMatches, char_u ***matches)
2138{
2139 return expand_set_opt_generic(
2140 args,
2141 get_encoding_name,
2142 numMatches,
2143 matches);
2144}
2145
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002146/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002147 * The 'eventignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002148 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002149 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002150did_set_eventignore(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002151{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002152 if (check_ei() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002153 return e_invalid_argument;
2154 return NULL;
2155}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002156
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002157 static char_u *
2158get_eventignore_name(expand_T *xp, int idx)
2159{
2160 // 'eventignore' allows special keyword "all" in addition to
2161 // all event names.
2162 if (idx == 0)
2163 return (char_u *)"all";
2164 return get_event_name_no_group(xp, idx - 1);
2165}
2166
2167 int
2168expand_set_eventignore(optexpand_T *args, int *numMatches, char_u ***matches)
2169{
2170 return expand_set_opt_generic(
2171 args,
2172 get_eventignore_name,
2173 numMatches,
2174 matches);
2175}
2176
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002177/*
2178 * The 'fileformat' option is changed.
2179 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002180 char *
2181did_set_fileformat(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002182{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002183 char_u **varp = (char_u **)args->os_varp;
2184
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002185 if (!curbuf->b_p_ma && !(args->os_flags & OPT_GLOBAL))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002186 return e_cannot_make_changes_modifiable_is_off;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002187 else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002188 return e_invalid_argument;
2189
2190 // may also change 'textmode'
2191 if (get_fileformat(curbuf) == EOL_DOS)
2192 curbuf->b_p_tx = TRUE;
2193 else
2194 curbuf->b_p_tx = FALSE;
2195 redraw_titles();
2196 // update flag in swap file
2197 ml_setflags(curbuf);
2198 // Redraw needed when switching to/from "mac": a CR in the text
2199 // will be displayed differently.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002200 if (get_fileformat(curbuf) == EOL_MAC || *args->os_oldval.string == 'm')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002201 redraw_curbuf_later(UPD_NOT_VALID);
2202
2203 return NULL;
2204}
2205
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002206 int
2207expand_set_fileformat(optexpand_T *args, int *numMatches, char_u ***matches)
2208{
2209 return expand_set_opt_string(
2210 args,
2211 p_ff_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002212 ARRAY_LENGTH(p_ff_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002213 numMatches,
2214 matches);
2215}
2216
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002217/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002218 * Function given to ExpandGeneric() to obtain the possible arguments of the
2219 * fileformat options.
2220 */
2221 char_u *
2222get_fileformat_name(expand_T *xp UNUSED, int idx)
2223{
2224 if (idx >= (int)ARRAY_LENGTH(p_ff_values))
2225 return NULL;
2226
2227 return (char_u*)p_ff_values[idx];
2228}
2229
2230/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002231 * The 'fileformats' option is changed.
2232 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002233 char *
2234did_set_fileformats(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002235{
2236 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
2237 return e_invalid_argument;
2238
2239 // also change 'textauto'
2240 if (*p_ffs == NUL)
2241 p_ta = FALSE;
2242 else
2243 p_ta = TRUE;
2244
2245 return NULL;
2246}
2247
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002248/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002249 * The 'filetype' or the 'syntax' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002250 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002251 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002252did_set_filetype_or_syntax(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002253{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002254 char_u **varp = (char_u **)args->os_varp;
2255
2256 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002257 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002258
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002259 args->os_value_changed = STRCMP(args->os_oldval.string, *varp) != 0;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002260
2261 // Since we check the value, there is no need to set P_INSECURE,
2262 // even when the value comes from a modeline.
2263 args->os_value_checked = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002264
2265 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002266}
2267
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002268#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002269/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002270 * The 'foldclose' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002271 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002272 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002273did_set_foldclose(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002274{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002275 return did_set_opt_strings(p_fcl, p_fcl_values, TRUE);
2276}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002277
2278 int
2279expand_set_foldclose(optexpand_T *args, int *numMatches, char_u ***matches)
2280{
2281 return expand_set_opt_string(
2282 args,
2283 p_fcl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002284 ARRAY_LENGTH(p_fcl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002285 numMatches,
2286 matches);
2287}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002288#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002289
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002290#if (defined(FEAT_EVAL) && defined(FEAT_FOLDING)) || defined(PROTO)
2291/*
2292 * The 'foldexpr' option is changed.
2293 */
2294 char *
2295did_set_foldexpr(optset_T *args)
2296{
2297 (void)did_set_optexpr(args);
2298 if (foldmethodIsExpr(curwin))
2299 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002300 return NULL;
2301}
2302#endif
2303
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002304#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002305/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002306 * The 'foldignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002307 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002308 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002309did_set_foldignore(optset_T *args UNUSED)
2310{
2311 if (foldmethodIsIndent(curwin))
2312 foldUpdateAll(curwin);
2313 return NULL;
2314}
2315
2316/*
2317 * The 'foldmarker' option is changed.
2318 */
2319 char *
2320did_set_foldmarker(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002321{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002322 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002323 char_u *p;
2324
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002325 p = vim_strchr(*varp, ',');
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002326 if (p == NULL)
2327 return e_comma_required;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002328 else if (p == *varp || p[1] == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002329 return e_invalid_argument;
2330 else if (foldmethodIsMarker(curwin))
2331 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002332
2333 return NULL;
2334}
2335
2336/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002337 * The 'foldmethod' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002338 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002339 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002340did_set_foldmethod(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002341{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002342 char_u **varp = (char_u **)args->os_varp;
2343
Milly142cad12024-10-22 22:11:51 +02002344 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK || **varp == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002345 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002346
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002347 foldUpdateAll(curwin);
2348 if (foldmethodIsDiff(curwin))
2349 newFoldLevel();
2350 return NULL;
2351}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002352
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002353 int
2354expand_set_foldmethod(optexpand_T *args, int *numMatches, char_u ***matches)
2355{
2356 return expand_set_opt_string(
2357 args,
2358 p_fdm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002359 ARRAY_LENGTH(p_fdm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002360 numMatches,
2361 matches);
2362}
2363
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002364/*
2365 * The 'foldopen' option is changed.
2366 */
2367 char *
2368did_set_foldopen(optset_T *args UNUSED)
2369{
2370 return did_set_opt_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
2371}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002372
2373 int
2374expand_set_foldopen(optexpand_T *args, int *numMatches, char_u ***matches)
2375{
2376 return expand_set_opt_string(
2377 args,
2378 p_fdo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002379 ARRAY_LENGTH(p_fdo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002380 numMatches,
2381 matches);
2382}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002383#endif
2384
2385/*
2386 * The 'formatoptions' option is changed.
2387 */
2388 char *
2389did_set_formatoptions(optset_T *args)
2390{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002391 char_u **varp = (char_u **)args->os_varp;
2392
Christian Brabandtb39b2402023-11-29 11:34:05 +01002393 return did_set_option_listflag(*varp, (char_u *)FO_ALL, args->os_errbuf,
2394 args->os_errbuflen);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002395}
2396
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002397 int
2398expand_set_formatoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2399{
2400 return expand_set_opt_listflag(args, (char_u*)FO_ALL, numMatches, matches);
2401}
2402
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002403#if defined(CURSOR_SHAPE) || defined(PROTO)
2404/*
2405 * The 'guicursor' option is changed.
2406 */
2407 char *
2408did_set_guicursor(optset_T *args UNUSED)
2409{
2410 return parse_shape_opt(SHAPE_CURSOR);
2411}
2412#endif
2413
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002414#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002415/*
2416 * The 'guifont' option is changed.
2417 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002418 char *
2419did_set_guifont(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002420{
2421 char_u *p;
2422 char *errmsg = NULL;
2423
2424 if (gui.in_use)
2425 {
2426 p = p_guifont;
2427# if defined(FEAT_GUI_GTK)
2428 // Put up a font dialog and let the user select a new value.
2429 // If this is cancelled go back to the old value but don't
2430 // give an error message.
2431 if (STRCMP(p, "*") == 0)
2432 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002433 p = gui_mch_font_dialog(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002434 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002435 p_guifont = (p != NULL) ? p : vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002436 }
2437# endif
2438 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
2439 {
2440# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
2441 if (STRCMP(p_guifont, "*") == 0)
2442 {
2443 // Dialog was cancelled: Keep the old value without giving
2444 // an error message.
2445 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002446 p_guifont = vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002447 }
2448 else
2449# endif
2450 errmsg = e_invalid_fonts;
2451 }
2452 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002453
2454 return errmsg;
2455}
2456
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002457/*
2458 * Expand the 'guifont' option. Only when GUI is being used. Each platform has
2459 * specific behaviors.
2460 */
2461 int
2462expand_set_guifont(optexpand_T *args, int *numMatches, char_u ***matches)
2463{
2464 if (!gui.in_use)
2465 return FAIL;
2466
2467# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
2468 char_u **varp = (char_u **)args->oe_varp;
2469 int wide = (varp == &p_guifontwide);
2470
2471 return expand_set_opt_callback(
2472 args, gui_mch_expand_font, &wide, numMatches, matches);
2473# else
2474 return FAIL;
2475# endif
2476}
2477
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002478# if defined(FEAT_XFONTSET) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002479/*
2480 * The 'guifontset' option is changed.
2481 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002482 char *
2483did_set_guifontset(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002484{
2485 char *errmsg = NULL;
2486
2487 if (STRCMP(p_guifontset, "*") == 0)
2488 errmsg = e_cant_select_fontset;
2489 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
2490 errmsg = e_invalid_fontset;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002491
2492 return errmsg;
2493}
2494# endif
2495
2496/*
2497 * The 'guifontwide' option is changed.
2498 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002499 char *
2500did_set_guifontwide(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002501{
2502 char *errmsg = NULL;
2503
2504 if (STRCMP(p_guifontwide, "*") == 0)
2505 errmsg = e_cant_select_wide_font;
2506 else if (gui_get_wide_font() == FAIL)
2507 errmsg = e_invalid_wide_font;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002508
2509 return errmsg;
2510}
2511#endif
2512
Erik S. V. Jansson8b1e7492024-02-24 14:26:52 +01002513#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002514/*
2515 * The 'guiligatures' option is changed.
2516 */
2517 char *
2518did_set_guiligatures(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002519{
2520 gui_set_ligatures();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002521 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002522}
2523#endif
2524
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002525#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002526/*
2527 * The 'guioptions' option is changed.
2528 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002529 char *
2530did_set_guioptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002531{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002532 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002533 char *errmsg;
2534
Christian Brabandtb39b2402023-11-29 11:34:05 +01002535 errmsg = did_set_option_listflag(*varp, (char_u *)GO_ALL, args->os_errbuf,
2536 args->os_errbuflen);
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002537 if (errmsg != NULL)
2538 return errmsg;
2539
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002540 gui_init_which_components(args->os_oldval.string);
2541 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002542}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002543
2544 int
2545expand_set_guioptions(optexpand_T *args, int *numMatches, char_u ***matches)
2546{
2547 return expand_set_opt_listflag(args, (char_u*)GO_ALL, numMatches, matches);
2548}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002549#endif
2550
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002551#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002552/*
2553 * The 'guitablabel' option is changed.
2554 */
2555 char *
2556did_set_guitablabel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002557{
2558 redraw_tabline = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002559 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002560}
2561#endif
2562
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002563/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002564 * The 'helpfile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002565 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002566 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002567did_set_helpfile(optset_T *args UNUSED)
2568{
2569 // May compute new values for $VIM and $VIMRUNTIME
2570 if (didset_vim)
2571 vim_unsetenv_ext((char_u *)"VIM");
2572 if (didset_vimruntime)
2573 vim_unsetenv_ext((char_u *)"VIMRUNTIME");
2574 return NULL;
2575}
2576
2577#if defined(FEAT_MULTI_LANG) || defined(PROTO)
2578/*
2579 * The 'helplang' option is changed.
2580 */
2581 char *
2582did_set_helplang(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002583{
2584 char *errmsg = NULL;
2585
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002586 // Check for "", "ab", "ab,cd", etc.
2587 for (char_u *s = p_hlg; *s != NUL; s += 3)
2588 {
2589 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
2590 {
2591 errmsg = e_invalid_argument;
2592 break;
2593 }
2594 if (s[2] == NUL)
2595 break;
2596 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002597
2598 return errmsg;
2599}
2600#endif
2601
2602/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002603 * The 'highlight' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002604 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002605 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002606did_set_highlight(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002607{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002608 if (highlight_changed() == FAIL)
2609 return e_invalid_argument; // invalid flags
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002610
2611 return NULL;
2612}
2613
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002614/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002615 * Expand 'highlight' option.
2616 */
2617 int
2618expand_set_highlight(optexpand_T *args, int *numMatches, char_u ***matches)
2619{
2620 char_u *p;
2621 expand_T *xp = args->oe_xp;
2622 static char_u hl_flags[HLF_COUNT] = HL_FLAGS;
Christian Brabandt3f168ec2023-10-02 23:21:11 +02002623 size_t i;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002624 int count = 0;
2625
2626 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2627 {
2628 // Right after a ':', meaning we just return all highlight names.
2629 return expand_set_opt_generic(
2630 args,
2631 get_highlight_name,
2632 numMatches,
2633 matches);
2634 }
2635
2636 if (*xp->xp_pattern == NUL)
2637 {
2638 // At beginning of a comma-separated list. Return the specific list of
2639 // supported occasions.
2640 *matches = ALLOC_MULT(char_u *, HLF_COUNT + 1);
2641 if (*matches == NULL)
2642 return FAIL;
2643
2644 // We still want to return the full option if it's requested.
2645 if (args->oe_include_orig_val)
2646 {
2647 p = vim_strsave(args->oe_opt_value);
2648 if (p == NULL)
2649 {
2650 VIM_CLEAR(*matches);
2651 return FAIL;
2652 }
2653 (*matches)[count++] = p;
2654 }
2655
2656 for (i = 0; i < HLF_COUNT; i++)
2657 {
2658 p = vim_strnsave(&hl_flags[i], 1);
2659 if (p == NULL)
2660 {
2661 if (count == 0)
2662 {
2663 VIM_CLEAR(*matches);
2664 return FAIL;
2665 }
2666 else
2667 break;
2668 }
2669 (*matches)[count++] = p;
2670 }
2671
2672 if (count == 0)
2673 {
2674 VIM_CLEAR(*matches);
2675 return FAIL;
2676 }
2677 *numMatches = count;
2678 return OK;
2679 }
2680
2681 // We are after the initial character (which indicates the occasion). We
2682 // already made sure we are not matching after a ':' above, so now we want
2683 // to match against display mode modifiers.
2684 // Since the xp_pattern starts from the beginning, we need to include it in
2685 // the returned match.
2686
2687 // Note: Keep this in sync with highlight_changed()
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002688 static char_u p_hl_mode_values[] =
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002689 {':', 'b', 'i', '-', 'n', 'r', 's', 'u', 'c', '2', 'd', '=', 't'};
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002690 size_t num_hl_modes = ARRAY_LENGTH(p_hl_mode_values);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002691
2692 *matches = ALLOC_MULT(char_u *, num_hl_modes);
2693 if (*matches == NULL)
2694 return FAIL;
2695
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002696 int pattern_len = xp->xp_pattern_len;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002697
2698 for (i = 0; i < num_hl_modes; i++)
2699 {
2700 // Don't allow duplicates as these are unique flags
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002701 char_u *dup = vim_strchr(xp->xp_pattern + 1, p_hl_mode_values[i]);
2702 if (dup != NULL && (int)(dup - xp->xp_pattern) < pattern_len)
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002703 continue;
2704
2705 // ':' only works by itself, not with other flags.
2706 if (pattern_len > 1 && p_hl_mode_values[i] == ':')
2707 continue;
2708
2709 p = vim_strnsave(xp->xp_pattern, pattern_len + 1);
2710 if (p == NULL)
2711 {
2712 if (i == 0)
2713 {
2714 VIM_CLEAR(*matches);
2715 return FAIL;
2716 }
2717 else
2718 break;
2719 }
2720 p[pattern_len] = p_hl_mode_values[i];
2721 p[pattern_len + 1] = NUL;
2722 (*matches)[count++] = p;
2723 }
2724 if (count == 0)
2725 {
2726 VIM_CLEAR(*matches);
2727 return FAIL;
2728 }
2729 *numMatches = count;
2730
2731 return OK;
2732}
2733
2734/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002735 * The 'titlestring' or the 'iconstring' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002736 */
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002737 static char *
2738parse_titleiconstring(optset_T *args UNUSED, int flagval UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002739{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002740#ifdef FEAT_STL_OPT
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002741 char_u **varp = (char_u **)args->os_varp;
2742
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002743 // NULL => statusline syntax
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002744 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002745 stl_syntax |= flagval;
2746 else
2747 stl_syntax &= ~flagval;
2748#endif
2749 did_set_title();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002750
2751 return NULL;
2752}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002753
2754/*
2755 * The 'iconstring' option is changed.
2756 */
2757 char *
2758did_set_iconstring(optset_T *args)
2759{
2760 int flagval = 0;
2761
2762#ifdef FEAT_STL_OPT
2763 flagval = STL_IN_ICON;
2764#endif
2765
2766 return parse_titleiconstring(args, flagval);
2767}
2768
2769#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
2770/*
2771 * The 'imactivatekey' option is changed.
2772 */
2773 char *
2774did_set_imactivatekey(optset_T *args UNUSED)
2775{
2776 if (!im_xim_isvalid_imactivate())
2777 return e_invalid_argument;
2778 return NULL;
2779}
2780#endif
2781
2782/*
Milly5e7a6a42024-10-22 22:27:19 +02002783 * The 'iskeyword' option is changed.
2784 */
2785 char *
2786did_set_iskeyword(optset_T *args)
2787{
2788 char_u **varp = (char_u **)args->os_varp;
2789
2790 if (varp == &p_isk) // only check for global-value
2791 {
2792 if (check_isopt(*varp) == FAIL)
2793 return e_invalid_argument;
2794 }
2795 else // fallthrough for local-value
2796 return did_set_isopt(args);
2797
2798 return NULL;
2799}
2800
2801/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002802 * The 'isident' or the 'iskeyword' or the 'isprint' or the 'isfname' option is
2803 * changed.
2804 */
2805 char *
2806did_set_isopt(optset_T *args)
2807{
Milly5e7a6a42024-10-22 22:27:19 +02002808 // 'isident', 'iskeyword', 'isprint' or 'isfname' option: refill g_chartab[]
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002809 // If the new option is invalid, use old value.
2810 // 'lisp' option: refill g_chartab[] for '-' char.
2811 if (init_chartab() == FAIL)
2812 {
2813 args->os_restore_chartab = TRUE;// need to restore the chartab.
2814 return e_invalid_argument; // error in value
2815 }
2816
2817 return NULL;
2818}
2819
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002820/*
2821 * The 'jumpoptions' option is changed.
2822 */
2823 char *
Yegappan Lakshmanan1926ae42023-09-21 16:36:28 +02002824did_set_jumpoptions(optset_T *args UNUSED)
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002825{
2826 if (opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE) != OK)
2827 return e_invalid_argument;
2828
2829 return NULL;
2830}
2831
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002832 int
2833expand_set_jumpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2834{
2835 return expand_set_opt_string(
2836 args,
2837 p_jop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002838 ARRAY_LENGTH(p_jop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002839 numMatches,
2840 matches);
2841}
2842
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002843#if defined(FEAT_KEYMAP) || defined(PROTO)
2844/*
2845 * The 'keymap' option is changed.
2846 */
2847 char *
2848did_set_keymap(optset_T *args)
2849{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002850 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002851 char *errmsg = NULL;
2852
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002853 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002854 errmsg = e_invalid_argument;
2855 else
2856 {
2857 int secure_save = secure;
2858
2859 // Reset the secure flag, since the value of 'keymap' has
2860 // been checked to be safe.
2861 secure = 0;
2862
2863 // load or unload key mapping tables
2864 errmsg = keymap_init();
2865
2866 secure = secure_save;
2867
2868 // Since we check the value, there is no need to set P_INSECURE,
2869 // even when the value comes from a modeline.
2870 args->os_value_checked = TRUE;
2871 }
2872
2873 if (errmsg == NULL)
2874 {
2875 if (*curbuf->b_p_keymap != NUL)
2876 {
2877 // Installed a new keymap, switch on using it.
2878 curbuf->b_p_iminsert = B_IMODE_LMAP;
2879 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
2880 curbuf->b_p_imsearch = B_IMODE_LMAP;
2881 }
2882 else
2883 {
2884 // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
2885 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
2886 curbuf->b_p_iminsert = B_IMODE_NONE;
2887 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
2888 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
2889 }
2890 if ((args->os_flags & OPT_LOCAL) == 0)
2891 {
2892 set_iminsert_global();
2893 set_imsearch_global();
2894 }
2895 status_redraw_curbuf();
2896 }
2897
2898 return errmsg;
2899}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002900#endif
2901
2902/*
2903 * The 'keymodel' option is changed.
2904 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002905 char *
2906did_set_keymodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002907{
2908 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
2909 return e_invalid_argument;
2910
2911 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
2912 km_startsel = (vim_strchr(p_km, 'a') != NULL);
2913 return NULL;
2914}
2915
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002916 int
2917expand_set_keymodel(optexpand_T *args, int *numMatches, char_u ***matches)
2918{
2919 return expand_set_opt_string(
2920 args,
2921 p_km_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002922 ARRAY_LENGTH(p_km_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002923 numMatches,
2924 matches);
2925}
2926
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002927/*
2928 * The 'keyprotocol' option is changed.
2929 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002930 char *
2931did_set_keyprotocol(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002932{
Gregory Anders3695d0e2023-09-29 20:17:20 +02002933 char_u *term = T_NAME;
2934 keyprot_T kpc = match_keyprotocol(term);
2935 if (kpc == KEYPROTOCOL_FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002936 return e_invalid_argument;
2937
Gregory Anders3695d0e2023-09-29 20:17:20 +02002938 apply_keyprotocol(term, kpc);
2939
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002940 return NULL;
2941}
2942
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002943 int
2944expand_set_keyprotocol(optexpand_T *args, int *numMatches, char_u ***matches)
2945{
2946 expand_T *xp = args->oe_xp;
2947 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2948 {
2949 // 'keyprotocol' only has well-defined terms for completion for the
2950 // protocol part after the colon.
2951 return expand_set_opt_string(
2952 args,
2953 p_kpc_protocol_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002954 ARRAY_LENGTH(p_kpc_protocol_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002955 numMatches,
2956 matches);
2957 }
2958 // Use expand_set_opt_string instead of returning FAIL so that we can
2959 // include the original value if args->oe_include_orig_val is set.
2960 static char *(empty[]) = {NULL};
2961 return expand_set_opt_string(args, empty, 0, numMatches, matches);
2962}
2963
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002964/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002965 * The 'lispoptions' option is changed.
2966 */
2967 char *
2968did_set_lispoptions(optset_T *args)
2969{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002970 char_u **varp = (char_u **)args->os_varp;
2971
2972 if (**varp != NUL
2973 && STRCMP(*varp, "expr:0") != 0
2974 && STRCMP(*varp, "expr:1") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002975 return e_invalid_argument;
2976
2977 return NULL;
2978}
2979
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002980 int
2981expand_set_lispoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2982{
2983 static char *(p_lop_values[]) = {"expr:0", "expr:1", NULL};
2984 return expand_set_opt_string(
2985 args,
2986 p_lop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002987 ARRAY_LENGTH(p_lop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002988 numMatches,
2989 matches);
2990}
2991
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002992/*
2993 * The 'matchpairs' option is changed.
2994 */
2995 char *
2996did_set_matchpairs(optset_T *args)
2997{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002998 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002999 char_u *p;
3000
3001 if (has_mbyte)
3002 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003003 for (p = *varp; *p != NUL; ++p)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003004 {
3005 int x2 = -1;
3006 int x3 = -1;
3007
3008 p += mb_ptr2len(p);
3009 if (*p != NUL)
3010 x2 = *p++;
3011 if (*p != NUL)
3012 {
3013 x3 = mb_ptr2char(p);
3014 p += mb_ptr2len(p);
3015 }
3016 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
3017 return e_invalid_argument;
3018 if (*p == NUL)
3019 break;
3020 }
3021 }
3022 else
3023 {
3024 // Check for "x:y,x:y"
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003025 for (p = *varp; *p != NUL; p += 4)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003026 {
3027 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
3028 return e_invalid_argument;
3029 if (p[3] == NUL)
3030 break;
3031 }
3032 }
3033
3034 return NULL;
3035}
3036
3037#if defined(FEAT_SPELL) || defined(PROTO)
3038/*
3039 * The 'mkspellmem' option is changed.
3040 */
3041 char *
3042did_set_mkspellmem(optset_T *args UNUSED)
3043{
3044 if (spell_check_msm() != OK)
3045 return e_invalid_argument;
3046
3047 return NULL;
3048}
3049#endif
3050
3051/*
3052 * The 'mouse' option is changed.
3053 */
3054 char *
3055did_set_mouse(optset_T *args)
3056{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003057 char_u **varp = (char_u **)args->os_varp;
3058
Christian Brabandtb39b2402023-11-29 11:34:05 +01003059 return did_set_option_listflag(*varp, (char_u *)MOUSE_ALL, args->os_errbuf,
3060 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003061}
3062
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003063 int
3064expand_set_mouse(optexpand_T *args, int *numMatches, char_u ***matches)
3065{
3066 return expand_set_opt_listflag(args, (char_u*)MOUSE_ALL, numMatches, matches);
3067}
3068
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003069/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003070 * The 'mousemodel' option is changed.
3071 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003072 char *
3073did_set_mousemodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003074{
3075 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
3076 return e_invalid_argument;
3077#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
Ken Takata18d0d292023-12-19 20:12:29 +01003078 else if (*p_mousem != *args->os_oldval.string)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003079 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
3080 // to create or delete the popup menus.
3081 gui_motif_update_mousemodel(root_menu);
3082#endif
3083
3084 return NULL;
3085}
3086
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003087 int
3088expand_set_mousemodel(optexpand_T *args, int *numMatches, char_u ***matches)
3089{
3090 return expand_set_opt_string(
3091 args,
3092 p_mousem_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003093 ARRAY_LENGTH(p_mousem_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003094 numMatches,
3095 matches);
3096}
3097
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003098#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3099 char *
3100did_set_mouseshape(optset_T *args UNUSED)
3101{
3102 char *errmsg = NULL;
3103
3104 errmsg = parse_shape_opt(SHAPE_MOUSE);
3105 update_mouseshape(-1);
3106
3107 return errmsg;
3108}
3109#endif
3110
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003111/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003112 * The 'nrformats' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003113 */
3114 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003115did_set_nrformats(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003116{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003117 char_u **varp = (char_u **)args->os_varp;
3118
3119 return did_set_opt_strings(*varp, p_nf_values, TRUE);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003120}
3121
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003122 int
3123expand_set_nrformats(optexpand_T *args, int *numMatches, char_u ***matches)
3124{
3125 return expand_set_opt_string(
3126 args,
3127 p_nf_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003128 ARRAY_LENGTH(p_nf_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003129 numMatches,
3130 matches);
3131}
3132
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003133#if defined(FEAT_EVAL) || defined(PROTO)
3134/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003135 * One of the '*expr' options is changed: 'balloonexpr', 'diffexpr',
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003136 * 'foldexpr', 'foldtext', 'formatexpr', 'includeexpr', 'indentexpr',
3137 * 'patchexpr', 'printexpr' and 'charconvert'.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003138 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003139 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003140did_set_optexpr(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003141{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003142 char_u **varp = (char_u **)args->os_varp;
3143
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003144 // If the option value starts with <SID> or s:, then replace that with
3145 // the script identifier.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003146 char_u *name = get_scriptlocal_funcname(*varp);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003147 if (name != NULL)
3148 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003149 free_string_option(*varp);
3150 *varp = name;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003151 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003152
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003153 return NULL;
3154}
3155#endif
3156
3157/*
3158 * The 'pastetoggle' option is changed.
3159 */
3160 char *
3161did_set_pastetoggle(optset_T *args UNUSED)
3162{
3163 char_u *p;
3164
3165 // translate key codes like in a mapping
3166 if (*p_pt)
3167 {
zeertzjq7e0bae02023-08-11 23:15:38 +02003168 (void)replace_termcodes(p_pt, &p, 0,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003169 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
3170 if (p != NULL)
3171 {
3172 free_string_option(p_pt);
3173 p_pt = p;
3174 }
3175 }
3176
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003177 return NULL;
3178}
3179
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003180#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3181/*
3182 * The 'previewpopup' option is changed.
3183 */
3184 char *
3185did_set_previewpopup(optset_T *args UNUSED)
3186{
3187 if (parse_previewpopup(NULL) == FAIL)
3188 return e_invalid_argument;
3189
3190 return NULL;
3191}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003192
3193 int
3194expand_set_popupoption(optexpand_T *args, int *numMatches, char_u ***matches)
3195{
3196 expand_T *xp = args->oe_xp;
3197
3198 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3199 {
3200 // Within "highlight:"/"border:"/"align:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003201 int border_len = (int)STRLEN("border:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003202 if (xp->xp_pattern - args->oe_set_arg >= border_len &&
3203 STRNCMP(xp->xp_pattern - border_len, "border:", border_len) == 0)
3204 {
3205 return expand_set_opt_string(
3206 args,
3207 p_popup_option_border_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003208 ARRAY_LENGTH(p_popup_option_border_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003209 numMatches,
3210 matches);
3211 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003212 int align_len = (int)STRLEN("align:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003213 if (xp->xp_pattern - args->oe_set_arg >= align_len &&
3214 STRNCMP(xp->xp_pattern - align_len, "align:", align_len) == 0)
3215 {
3216 return expand_set_opt_string(
3217 args,
3218 p_popup_option_align_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003219 ARRAY_LENGTH(p_popup_option_align_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003220 numMatches,
3221 matches);
3222 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003223 int highlight_len = (int)STRLEN("highlight:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003224 if (xp->xp_pattern - args->oe_set_arg >= highlight_len &&
3225 STRNCMP(xp->xp_pattern - highlight_len, "highlight:", highlight_len) == 0)
3226 {
3227 // Return the list of all highlight names
3228 return expand_set_opt_generic(
3229 args,
3230 get_highlight_name,
3231 numMatches,
3232 matches);
3233 }
3234 return FAIL;
3235 }
3236
3237 return expand_set_opt_string(
3238 args,
3239 p_popup_option_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003240 ARRAY_LENGTH(p_popup_option_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003241 numMatches,
3242 matches);
3243}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003244#endif
3245
3246#if defined(FEAT_POSTSCRIPT) || defined(PROTO)
3247/*
3248 * The 'printencoding' option is changed.
3249 */
3250 char *
3251did_set_printencoding(optset_T *args UNUSED)
3252{
3253 char_u *s, *p;
3254
3255 // Canonize 'printencoding' if VIM standard one
3256 p = enc_canonize(p_penc);
3257 if (p != NULL)
3258 {
3259 vim_free(p_penc);
3260 p_penc = p;
3261 }
3262 else
3263 {
3264 // Ensure lower case and '-' for '_'
3265 for (s = p_penc; *s != NUL; s++)
3266 {
3267 if (*s == '_')
3268 *s = '-';
3269 else
3270 *s = TOLOWER_ASC(*s);
3271 }
3272 }
3273
3274 return NULL;
3275}
3276#endif
3277
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003278#if defined(FEAT_PRINTER) || defined(PROTO)
3279
3280 static char_u *
3281get_printoptions_names(expand_T *xp UNUSED, int idx)
3282{
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003283 if (idx >= (int)ARRAY_LENGTH(printer_opts))
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003284 return NULL;
3285 return (char_u*)printer_opts[idx].name;
3286}
3287
3288/*
3289 * Expand 'printoptions' option
3290 */
3291 int
3292expand_set_printoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3293{
3294 return expand_set_opt_generic(
3295 args,
3296 get_printoptions_names,
3297 numMatches,
3298 matches);
3299}
3300#endif
3301
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003302#if defined(FEAT_STL_OPT) || defined(PROTO)
3303/*
3304 * The 'statusline' or the 'tabline' or the 'rulerformat' option is changed.
3305 * "rulerformat" is TRUE if the 'rulerformat' option is changed.
3306 */
3307 static char *
3308parse_statustabline_rulerformat(optset_T *args, int rulerformat)
3309{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003310 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003311 char_u *s;
3312 char *errmsg = NULL;
3313 int wid;
3314
3315 if (rulerformat) // reset ru_wid first
3316 ru_wid = 0;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003317 s = *varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003318 if (rulerformat && *s == '%')
3319 {
3320 // set ru_wid if 'ruf' starts with "%99("
3321 if (*++s == '-') // ignore a '-'
3322 s++;
3323 wid = getdigits(&s);
3324 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
3325 ru_wid = wid;
3326 else
3327 errmsg = check_stl_option(p_ruf);
3328 }
3329 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
3330 else if (rulerformat || s[0] != '%' || s[1] != '!')
3331 errmsg = check_stl_option(s);
3332 if (rulerformat && errmsg == NULL)
3333 comp_col();
3334
3335 return errmsg;
3336}
3337#endif
3338
3339#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
3340/*
3341 * The 'renderoptions' option is changed.
3342 */
3343 char *
3344did_set_renderoptions(optset_T *args UNUSED)
3345{
3346 if (!gui_mch_set_rendering_options(p_rop))
3347 return e_invalid_argument;
3348
3349 return NULL;
3350}
3351#endif
3352
3353#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3354/*
3355 * The 'rightleftcmd' option is changed.
3356 */
3357 char *
3358did_set_rightleftcmd(optset_T *args)
3359{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003360 char_u **varp = (char_u **)args->os_varp;
3361
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003362 // Currently only "search" is a supported value.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003363 if (**varp != NUL && STRCMP(*varp, "search") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003364 return e_invalid_argument;
3365
3366 return NULL;
3367}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003368
3369 int
3370expand_set_rightleftcmd(optexpand_T *args, int *numMatches, char_u ***matches)
3371{
3372 static char *(p_rlc_values[]) = {"search", NULL};
3373 return expand_set_opt_string(
3374 args,
3375 p_rlc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003376 ARRAY_LENGTH(p_rlc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003377 numMatches,
3378 matches);
3379}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003380#endif
3381
3382#if defined(FEAT_STL_OPT) || defined(PROTO)
3383/*
3384 * The 'rulerformat' option is changed.
3385 */
3386 char *
3387did_set_rulerformat(optset_T *args)
3388{
3389 return parse_statustabline_rulerformat(args, TRUE);
3390}
3391#endif
3392
3393/*
3394 * The 'scrollopt' option is changed.
3395 */
3396 char *
3397did_set_scrollopt(optset_T *args UNUSED)
3398{
3399 return did_set_opt_strings(p_sbo, p_scbopt_values, TRUE);
3400}
3401
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003402 int
3403expand_set_scrollopt(optexpand_T *args, int *numMatches, char_u ***matches)
3404{
3405 return expand_set_opt_string(
3406 args,
3407 p_scbopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003408 ARRAY_LENGTH(p_scbopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003409 numMatches,
3410 matches);
3411}
3412
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003413/*
3414 * The 'selection' option is changed.
3415 */
3416 char *
3417did_set_selection(optset_T *args UNUSED)
3418{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003419 if (*p_sel == NUL || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003420 return e_invalid_argument;
3421
3422 return NULL;
3423}
3424
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003425 int
3426expand_set_selection(optexpand_T *args, int *numMatches, char_u ***matches)
3427{
3428 return expand_set_opt_string(
3429 args,
3430 p_sel_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003431 ARRAY_LENGTH(p_sel_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003432 numMatches,
3433 matches);
3434}
3435
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003436/*
3437 * The 'selectmode' option is changed.
3438 */
3439 char *
3440did_set_selectmode(optset_T *args UNUSED)
3441{
3442 return did_set_opt_strings(p_slm, p_slm_values, TRUE);
3443}
3444
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003445 int
3446expand_set_selectmode(optexpand_T *args, int *numMatches, char_u ***matches)
3447{
3448 return expand_set_opt_string(
3449 args,
3450 p_slm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003451 ARRAY_LENGTH(p_slm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003452 numMatches,
3453 matches);
3454}
3455
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003456#if defined(FEAT_SESSION) || defined(PROTO)
3457/*
3458 * The 'sessionoptions' option is changed.
3459 */
3460 char *
3461did_set_sessionoptions(optset_T *args)
3462{
3463 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
3464 return e_invalid_argument;
3465 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
3466 {
3467 // Don't allow both "sesdir" and "curdir".
3468 (void)opt_strings_flags(args->os_oldval.string, p_ssop_values,
3469 &ssop_flags, TRUE);
3470 return e_invalid_argument;
3471 }
3472
3473 return NULL;
3474}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003475
3476 int
3477expand_set_sessionoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3478{
3479 return expand_set_opt_string(
3480 args,
3481 p_ssop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003482 ARRAY_LENGTH(p_ssop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003483 numMatches,
3484 matches);
3485}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003486#endif
3487
3488/*
3489 * The 'shortmess' option is changed.
3490 */
3491 char *
3492did_set_shortmess(optset_T *args)
3493{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003494 char_u **varp = (char_u **)args->os_varp;
3495
Christian Brabandtb39b2402023-11-29 11:34:05 +01003496 return did_set_option_listflag(*varp, (char_u *)SHM_ALL, args->os_errbuf,
3497 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003498}
3499
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003500 int
3501expand_set_shortmess(optexpand_T *args, int *numMatches, char_u ***matches)
3502{
3503 return expand_set_opt_listflag(args, (char_u*)SHM_ALL, numMatches, matches);
3504}
3505
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003506#if defined(FEAT_LINEBREAK) || defined(PROTO)
3507/*
3508 * The 'showbreak' option is changed.
3509 */
3510 char *
3511did_set_showbreak(optset_T *args)
3512{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003513 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003514 char_u *s;
3515
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003516 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003517 {
3518 if (ptr2cells(s) != 1)
3519 return e_showbreak_contains_unprintable_or_wide_character;
3520 MB_PTR_ADV(s);
3521 }
3522
3523 return NULL;
3524}
3525#endif
3526
3527/*
3528 * The 'showcmdloc' option is changed.
3529 */
3530 char *
3531did_set_showcmdloc(optset_T *args UNUSED)
3532{
zeertzjqc27fcf42024-03-01 23:01:43 +01003533 char *errmsg = did_set_opt_strings(p_sloc, p_sloc_values, FALSE);
3534
3535 if (errmsg == NULL)
3536 comp_col();
3537
3538 return errmsg;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003539}
3540
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003541 int
3542expand_set_showcmdloc(optexpand_T *args, int *numMatches, char_u ***matches)
3543{
3544 return expand_set_opt_string(
3545 args,
3546 p_sloc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003547 ARRAY_LENGTH(p_sloc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003548 numMatches,
3549 matches);
3550}
3551
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003552#if defined(FEAT_SIGNS) || defined(PROTO)
3553/*
3554 * The 'signcolumn' option is changed.
3555 */
3556 char *
3557did_set_signcolumn(optset_T *args)
3558{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003559 char_u **varp = (char_u **)args->os_varp;
3560
3561 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003562 return e_invalid_argument;
3563 // When changing the 'signcolumn' to or from 'number', recompute the
3564 // width of the number column if 'number' or 'relativenumber' is set.
3565 if (((*args->os_oldval.string == 'n' && args->os_oldval.string[1] == 'u')
3566 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
3567 && (curwin->w_p_nu || curwin->w_p_rnu))
3568 curwin->w_nrwidth_line_count = 0;
3569
3570 return NULL;
3571}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003572
3573 int
3574expand_set_signcolumn(optexpand_T *args, int *numMatches, char_u ***matches)
3575{
3576 return expand_set_opt_string(
3577 args,
3578 p_scl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003579 ARRAY_LENGTH(p_scl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003580 numMatches,
3581 matches);
3582}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003583#endif
3584
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003585#if defined(FEAT_SPELL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003586/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003587 * The 'spellcapcheck' option is changed.
3588 */
3589 char *
3590did_set_spellcapcheck(optset_T *args UNUSED)
3591{
3592 // compile the regexp program.
3593 return compile_cap_prog(curwin->w_s);
3594}
3595
3596/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003597 * The 'spellfile' option is changed.
3598 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003599 char *
3600did_set_spellfile(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003601{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003602 char_u **varp = (char_u **)args->os_varp;
3603
3604 if (!valid_spellfile(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003605 return e_invalid_argument;
3606
3607 // If there is a window for this buffer in which 'spell' is set load the
3608 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003609 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003610}
3611
3612/*
3613 * The 'spell' option is changed.
3614 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003615 char *
3616did_set_spelllang(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003617{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003618 char_u **varp = (char_u **)args->os_varp;
3619
3620 if (!valid_spelllang(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003621 return e_invalid_argument;
3622
3623 // If there is a window for this buffer in which 'spell' is set load the
3624 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003625 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003626}
3627
3628/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003629 * The 'spelloptions' option is changed.
3630 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003631 char *
3632did_set_spelloptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003633{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003634 char_u **varp = (char_u **)args->os_varp;
3635
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003636 if (**varp != NUL && STRCMP(*varp, "camel") != 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003637 return e_invalid_argument;
3638
3639 return NULL;
3640}
3641
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003642 int
3643expand_set_spelloptions(optexpand_T *args, int *numMatches, char_u ***matches)
3644{
3645 static char *(p_spo_values[]) = {"camel", NULL};
3646 return expand_set_opt_string(
3647 args,
3648 p_spo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003649 ARRAY_LENGTH(p_spo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003650 numMatches,
3651 matches);
3652}
3653
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003654/*
3655 * The 'spellsuggest' option is changed.
3656 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003657 char *
3658did_set_spellsuggest(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003659{
3660 if (spell_check_sps() != OK)
3661 return e_invalid_argument;
3662
3663 return NULL;
3664}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003665
3666 int
3667expand_set_spellsuggest(optexpand_T *args, int *numMatches, char_u ***matches)
3668{
3669 return expand_set_opt_string(
3670 args,
3671 p_sps_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003672 ARRAY_LENGTH(p_sps_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003673 numMatches,
3674 matches);
3675}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003676#endif
3677
3678/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003679 * The 'splitkeep' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003680 */
3681 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003682did_set_splitkeep(optset_T *args UNUSED)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003683{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003684 return did_set_opt_strings(p_spk, p_spk_values, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003685}
3686
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003687 int
3688expand_set_splitkeep(optexpand_T *args, int *numMatches, char_u ***matches)
3689{
3690 return expand_set_opt_string(
3691 args,
3692 p_spk_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003693 ARRAY_LENGTH(p_spk_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003694 numMatches,
3695 matches);
3696}
3697
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00003698#if defined(FEAT_STL_OPT) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003699/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003700 * The 'statusline' option is changed.
3701 */
3702 char *
3703did_set_statusline(optset_T *args)
3704{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003705 return parse_statustabline_rulerformat(args, FALSE);
3706}
3707#endif
3708
3709/*
3710 * The 'swapsync' option is changed.
3711 */
3712 char *
3713did_set_swapsync(optset_T *args UNUSED)
3714{
3715 return did_set_opt_strings(p_sws, p_sws_values, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003716}
3717
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003718 int
3719expand_set_swapsync(optexpand_T *args, int *numMatches, char_u ***matches)
3720{
3721 return expand_set_opt_string(
3722 args,
3723 p_sws_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003724 ARRAY_LENGTH(p_sws_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003725 numMatches,
3726 matches);
3727}
3728
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003729/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003730 * The 'switchbuf' option is changed.
3731 */
3732 char *
3733did_set_switchbuf(optset_T *args UNUSED)
3734{
3735 return did_set_opt_flags(p_swb, p_swb_values, &swb_flags, TRUE);
3736}
3737
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003738 int
3739expand_set_switchbuf(optexpand_T *args, int *numMatches, char_u ***matches)
3740{
3741 return expand_set_opt_string(
3742 args,
3743 p_swb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003744 ARRAY_LENGTH(p_swb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003745 numMatches,
3746 matches);
3747}
3748
LemonBoy5247b0b2024-07-12 19:30:58 +02003749/*
3750 * The 'tabclose' option is changed.
3751 */
3752 char *
3753did_set_tabclose(optset_T *args UNUSED)
3754{
3755 return did_set_opt_flags(p_tcl, p_tcl_values, &tcl_flags, TRUE);
3756}
3757
3758 int
3759expand_set_tabclose(optexpand_T *args, int *numMatches, char_u ***matches)
3760{
3761 return expand_set_opt_string(
3762 args,
3763 p_tcl_values,
3764 ARRAY_LENGTH(p_tcl_values) - 1,
3765 numMatches,
3766 matches);
3767}
3768
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003769#if defined(FEAT_STL_OPT) || defined(PROTO)
3770/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003771 * The 'tabline' option is changed.
3772 */
3773 char *
3774did_set_tabline(optset_T *args)
3775{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003776 return parse_statustabline_rulerformat(args, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003777}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003778#endif
3779
3780/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003781 * The 'tagcase' option is changed.
3782 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003783 char *
3784did_set_tagcase(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003785{
3786 unsigned int *flags;
3787 char_u *p;
3788
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003789 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003790 {
3791 p = curbuf->b_p_tc;
3792 flags = &curbuf->b_tc_flags;
3793 }
3794 else
3795 {
3796 p = p_tc;
3797 flags = &tc_flags;
3798 }
3799
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003800 if ((args->os_flags & OPT_LOCAL) && *p == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003801 // make the local value empty: use the global value
3802 *flags = 0;
3803 else if (*p == NUL
3804 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
3805 return e_invalid_argument;
3806
3807 return NULL;
3808}
3809
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003810 int
3811expand_set_tagcase(optexpand_T *args, int *numMatches, char_u ***matches)
3812{
3813 return expand_set_opt_string(
3814 args,
3815 p_tc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003816 ARRAY_LENGTH(p_tc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003817 numMatches,
3818 matches);
3819}
3820
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003821/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003822 * The 'term' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003823 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003824 char *
3825did_set_term(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003826{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003827 if (T_NAME[0] == NUL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003828 return e_cannot_set_term_to_empty_string;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003829#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003830 if (gui.in_use)
3831 return e_cannot_change_term_in_GUI;
3832 if (term_is_gui(T_NAME))
3833 return e_use_gui_to_start_GUI;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003834#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003835 if (set_termname(T_NAME) == FAIL)
3836 return e_not_found_in_termcap;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003837
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003838 // Screen colors may have changed.
3839 redraw_later_clear();
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003840
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003841 return NULL;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003842}
3843
3844/*
3845 * Some terminal option (t_xxx) is changed
3846 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003847 char *
3848did_set_term_option(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003849{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003850 char_u **varp = (char_u **)args->os_varp;
3851
3852 if (!full_screen)
3853 return NULL;
3854
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003855 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
3856 if (varp == &T_CCO)
3857 {
3858 int colors = atoi((char *)T_CCO);
3859
3860 // Only reinitialize colors if t_Co value has really changed to
3861 // avoid expensive reload of colorscheme if t_Co is set to the
3862 // same value multiple times.
3863 if (colors != t_colors)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003864 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003865 t_colors = colors;
3866 if (t_colors <= 1)
3867 {
3868 vim_free(T_CCO);
3869 T_CCO = empty_option;
3870 }
3871#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3872 if (is_term_win32())
3873 {
3874 swap_tcap();
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003875 args->os_did_swaptcap = TRUE;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003876 }
3877#endif
3878 // We now have a different color setup, initialize it again.
3879 init_highlight(TRUE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003880 }
3881 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003882 ttest(FALSE);
3883 if (varp == &T_ME)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003884 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003885 out_str(T_ME);
3886 redraw_later(UPD_CLEAR);
3887#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3888 // Since t_me has been set, this probably means that the user
3889 // wants to use this as default colors. Need to reset default
3890 // background/foreground colors.
3891# ifdef VIMDLL
3892 if (!gui.in_use && !gui.starting)
3893# endif
3894 mch_set_normal_colors();
3895#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003896 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003897 if (varp == &T_BE && termcap_active)
3898 {
3899 MAY_WANT_TO_LOG_THIS;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003900
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003901 if (*T_BE == NUL)
3902 // When clearing t_BE we assume the user no longer wants
3903 // bracketed paste, thus disable it by writing t_BD.
3904 out_str(T_BD);
3905 else
3906 out_str(T_BE);
3907 }
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003908
3909 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003910}
3911
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003912#if defined(FEAT_TERMINAL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003913/*
3914 * The 'termwinkey' option is changed.
3915 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003916 char *
Milly94606f72024-10-22 22:07:52 +02003917did_set_termwinkey(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003918{
Milly94606f72024-10-22 22:07:52 +02003919 char_u **varp = (char_u **)args->os_varp;
3920
3921 if ((*varp)[0] != NUL && string_to_key(*varp, TRUE) == 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003922 return e_invalid_argument;
3923
3924 return NULL;
3925}
3926
3927/*
3928 * The 'termwinsize' option is changed.
3929 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003930 char *
Milly8be10aa2024-10-22 22:01:46 +02003931did_set_termwinsize(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003932{
Milly8be10aa2024-10-22 22:01:46 +02003933 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003934 char_u *p;
3935
Milly8be10aa2024-10-22 22:01:46 +02003936 if ((*varp)[0] == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003937 return NULL;
3938
Milly8be10aa2024-10-22 22:01:46 +02003939 p = skipdigits(*varp);
3940 if (p == *varp || (*p != 'x' && *p != '*') || *skipdigits(p + 1) != NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003941 return e_invalid_argument;
3942
3943 return NULL;
3944}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003945
3946# if defined(MSWIN) || defined(PROTO)
3947/*
3948 * The 'termwintype' option is changed.
3949 */
3950 char *
3951did_set_termwintype(optset_T *args UNUSED)
3952{
3953 return did_set_opt_strings(p_twt, p_twt_values, FALSE);
3954}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003955
3956 int
3957expand_set_termwintype(optexpand_T *args, int *numMatches, char_u ***matches)
3958{
3959 return expand_set_opt_string(
3960 args,
3961 p_twt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003962 ARRAY_LENGTH(p_twt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003963 numMatches,
3964 matches);
3965}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003966# endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003967#endif
3968
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003969/*
3970 * The 'titlestring' option is changed.
3971 */
3972 char *
3973did_set_titlestring(optset_T *args)
3974{
3975 int flagval = 0;
3976
3977#ifdef FEAT_STL_OPT
3978 flagval = STL_IN_TITLE;
3979#endif
3980 return parse_titleiconstring(args, flagval);
3981}
3982
3983#if (defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)) || defined(PROTO)
3984/*
3985 * The 'toolbar' option is changed.
3986 */
3987 char *
3988did_set_toolbar(optset_T *args UNUSED)
3989{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003990 if (opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags,
3991 TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003992 return e_invalid_argument;
3993
3994 out_flush();
3995 gui_mch_show_toolbar((toolbar_flags &
3996 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
3997 return NULL;
3998}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003999
4000 int
4001expand_set_toolbar(optexpand_T *args, int *numMatches, char_u ***matches)
4002{
4003 return expand_set_opt_string(
4004 args,
4005 p_toolbar_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004006 ARRAY_LENGTH(p_toolbar_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004007 numMatches,
4008 matches);
4009}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004010#endif
4011
4012#if (defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)) || defined(PROTO)
4013/*
4014 * The 'toolbariconsize' option is changed. GTK+ 2 only.
4015 */
4016 char *
4017did_set_toolbariconsize(optset_T *args UNUSED)
4018{
4019 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
4020 return e_invalid_argument;
4021
4022 out_flush();
4023 gui_mch_show_toolbar((toolbar_flags &
4024 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4025 return NULL;
4026}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004027
4028 int
4029expand_set_toolbariconsize(optexpand_T *args, int *numMatches, char_u ***matches)
4030{
4031 return expand_set_opt_string(
4032 args,
4033 p_tbis_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004034 ARRAY_LENGTH(p_tbis_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004035 numMatches,
4036 matches);
4037}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004038#endif
4039
4040#if defined(UNIX) || defined(VMS) || defined(PROTO)
4041/*
4042 * The 'ttymouse' option is changed.
4043 */
4044 char *
4045did_set_ttymouse(optset_T *args UNUSED)
4046{
4047 char *errmsg = NULL;
4048
4049 // Switch the mouse off before changing the escape sequences used for
4050 // that.
4051 mch_setmouse(FALSE);
4052 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
4053 errmsg = e_invalid_argument;
4054 else
4055 check_mouse_termcode();
4056 if (termcap_active)
4057 setmouse(); // may switch it on again
4058
4059 return errmsg;
4060}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004061
4062 int
4063expand_set_ttymouse(optexpand_T *args, int *numMatches, char_u ***matches)
4064{
4065 return expand_set_opt_string(
4066 args,
4067 p_ttym_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004068 ARRAY_LENGTH(p_ttym_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004069 numMatches,
4070 matches);
4071}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004072#endif
4073
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004074#if defined(FEAT_VARTABS) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004075/*
4076 * The 'varsofttabstop' option is changed.
4077 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004078 char *
4079did_set_varsofttabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004080{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004081 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004082 char_u *cp;
4083
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004084 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004085 VIM_CLEAR(curbuf->b_p_vsts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004086 else
4087 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004088 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004089 {
4090 if (vim_isdigit(*cp))
4091 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004092 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004093 continue;
4094 return e_invalid_argument;
4095 }
4096
4097 int *oldarray = curbuf->b_p_vsts_array;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004098 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004099 {
4100 if (oldarray)
4101 vim_free(oldarray);
4102 }
4103 else
4104 return e_invalid_argument;
4105 }
4106
4107 return NULL;
4108}
4109
4110/*
4111 * The 'vartabstop' option is changed.
4112 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004113 char *
4114did_set_vartabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004115{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004116 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004117 char_u *cp;
4118
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004119 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004120 VIM_CLEAR(curbuf->b_p_vts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004121 else
4122 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004123 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004124 {
4125 if (vim_isdigit(*cp))
4126 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004127 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004128 continue;
4129 return e_invalid_argument;
4130 }
4131
4132 int *oldarray = curbuf->b_p_vts_array;
4133
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004134 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004135 {
4136 vim_free(oldarray);
4137# ifdef FEAT_FOLDING
4138 if (foldmethodIsIndent(curwin))
4139 foldUpdateAll(curwin);
4140# endif
4141 }
4142 else
4143 return e_invalid_argument;
4144 }
4145
4146 return NULL;
4147}
4148#endif
4149
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004150/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004151 * The 'verbosefile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004152 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004153 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004154did_set_verbosefile(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004155{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004156 verbose_stop();
4157 if (*p_vfile != NUL && verbose_open() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004158 return e_invalid_argument;
4159
4160 return NULL;
4161}
4162
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004163#if defined(FEAT_SESSION) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004164/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004165 * The 'viewoptions' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004166 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004167 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004168did_set_viewoptions(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004169{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004170 return did_set_opt_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004171}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004172#endif
4173
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004174#if defined(FEAT_VIMINFO) || defined(PROTO)
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004175/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004176 * The 'viminfo' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004177 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004178 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004179did_set_viminfo(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004180{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004181 char_u *s;
4182 char *errmsg = NULL;
4183
4184 for (s = p_viminfo; *s;)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004185 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004186 // Check it's a valid character
4187 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
4188 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01004189 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004190 break;
4191 }
4192 if (*s == 'n') // name is always last one
4193 break;
4194 else if (*s == 'r') // skip until next ','
4195 {
4196 while (*++s && *s != ',')
4197 ;
4198 }
4199 else if (*s == '%')
4200 {
4201 // optional number
4202 while (vim_isdigit(*++s))
4203 ;
4204 }
4205 else if (*s == '!' || *s == 'h' || *s == 'c')
4206 ++s; // no extra chars
4207 else // must have a number
4208 {
4209 while (vim_isdigit(*++s))
4210 ;
4211
4212 if (!VIM_ISDIGIT(*(s - 1)))
4213 {
4214 if (args->os_errbuf != NULL)
4215 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01004216 vim_snprintf(args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004217 _(e_missing_number_after_angle_str_angle),
4218 transchar_byte(*(s - 1)));
4219 errmsg = args->os_errbuf;
4220 }
4221 else
4222 errmsg = "";
4223 break;
4224 }
4225 }
4226 if (*s == ',')
4227 ++s;
4228 else if (*s)
4229 {
4230 if (args->os_errbuf != NULL)
4231 errmsg = e_missing_comma;
4232 else
4233 errmsg = "";
4234 break;
4235 }
4236 }
4237 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
4238 errmsg = e_must_specify_a_value;
4239
4240 return errmsg;
4241}
4242#endif
4243
4244/*
4245 * The 'virtualedit' option is changed.
4246 */
4247 char *
4248did_set_virtualedit(optset_T *args)
4249{
4250 char_u *ve = p_ve;
4251 unsigned int *flags = &ve_flags;
4252
4253 if (args->os_flags & OPT_LOCAL)
4254 {
4255 ve = curwin->w_p_ve;
4256 flags = &curwin->w_ve_flags;
4257 }
4258
4259 if ((args->os_flags & OPT_LOCAL) && *ve == NUL)
4260 // make the local value empty: use the global value
4261 *flags = 0;
4262 else
4263 {
4264 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
4265 return e_invalid_argument;
4266 else if (STRCMP(ve, args->os_oldval.string) != 0)
4267 {
4268 // Recompute cursor position in case the new 've' setting
4269 // changes something.
4270 validate_virtcol();
4271 coladvance(curwin->w_virtcol);
4272 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004273 }
4274
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004275 return NULL;
4276}
4277
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004278 int
4279expand_set_virtualedit(optexpand_T *args, int *numMatches, char_u ***matches)
4280{
4281 return expand_set_opt_string(
4282 args,
4283 p_ve_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004284 ARRAY_LENGTH(p_ve_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004285 numMatches,
4286 matches);
4287}
4288
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004289/*
4290 * The 'whichwrap' option is changed.
4291 */
4292 char *
4293did_set_whichwrap(optset_T *args)
4294{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004295 char_u **varp = (char_u **)args->os_varp;
4296
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004297 // Add ',' to the list flags because 'whichwrap' is a flag
4298 // list that is comma-separated.
Christian Brabandtb39b2402023-11-29 11:34:05 +01004299 return did_set_option_listflag(*varp, (char_u *)(WW_ALL ","),
4300 args->os_errbuf, args->os_errbuflen);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004301}
4302
4303 int
4304expand_set_whichwrap(optexpand_T *args, int *numMatches, char_u ***matches)
4305{
4306 return expand_set_opt_listflag(args, (char_u*)WW_ALL, numMatches, matches);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004307}
4308
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004309/*
4310 * The 'wildmode' option is changed.
4311 */
4312 char *
4313did_set_wildmode(optset_T *args UNUSED)
4314{
4315 if (check_opt_wim() == FAIL)
4316 return e_invalid_argument;
4317 return NULL;
4318}
4319
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004320 int
4321expand_set_wildmode(optexpand_T *args, int *numMatches, char_u ***matches)
4322{
4323 return expand_set_opt_string(
4324 args,
4325 p_wim_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004326 ARRAY_LENGTH(p_wim_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004327 numMatches,
4328 matches);
4329}
4330
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004331/*
4332 * The 'wildoptions' option is changed.
4333 */
4334 char *
4335did_set_wildoptions(optset_T *args UNUSED)
4336{
4337 return did_set_opt_strings(p_wop, p_wop_values, TRUE);
4338}
4339
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004340 int
4341expand_set_wildoptions(optexpand_T *args, int *numMatches, char_u ***matches)
4342{
4343 return expand_set_opt_string(
4344 args,
4345 p_wop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004346 ARRAY_LENGTH(p_wop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004347 numMatches,
4348 matches);
4349}
4350
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004351#if defined(FEAT_WAK) || defined(PROTO)
4352/*
4353 * The 'winaltkeys' option is changed.
4354 */
4355 char *
4356did_set_winaltkeys(optset_T *args UNUSED)
4357{
4358 char *errmsg = NULL;
4359
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004360 if (*p_wak == NUL || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004361 errmsg = e_invalid_argument;
4362# ifdef FEAT_MENU
4363# if defined(FEAT_GUI_MOTIF)
4364 else if (gui.in_use)
4365 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4366# elif defined(FEAT_GUI_GTK)
4367 else if (gui.in_use)
4368 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4369# endif
4370# endif
4371 return errmsg;
4372}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004373
4374 int
4375expand_set_winaltkeys(optexpand_T *args, int *numMatches, char_u ***matches)
4376{
4377 return expand_set_opt_string(
4378 args,
4379 p_wak_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004380 ARRAY_LENGTH(p_wak_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004381 numMatches,
4382 matches);
4383}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004384#endif
4385
4386/*
4387 * The 'wincolor' option is changed.
4388 */
4389 char *
4390did_set_wincolor(optset_T *args UNUSED)
4391{
4392#ifdef FEAT_TERMINAL
4393 term_update_wincolor(curwin);
4394#endif
4395 return NULL;
4396}
4397
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004398 int
4399expand_set_wincolor(optexpand_T *args, int *numMatches, char_u ***matches)
4400{
4401 return expand_set_opt_generic(
4402 args,
4403 get_highlight_name,
4404 numMatches,
4405 matches);
4406}
4407
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004408#ifdef FEAT_SYN_HL
4409/*
4410 * When the 'syntax' option is set, load the syntax of that name.
4411 */
4412 static void
4413do_syntax_autocmd(int value_changed)
4414{
4415 static int syn_recursive = 0;
4416
4417 ++syn_recursive;
4418 // Only pass TRUE for "force" when the value changed or not used
4419 // recursively, to avoid endless recurrence.
4420 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
4421 value_changed || syn_recursive == 1, curbuf);
4422 curbuf->b_flags |= BF_SYN_SET;
4423 --syn_recursive;
4424}
4425#endif
4426
4427/*
4428 * When the 'filetype' option is set, trigger the FileType autocommand.
4429 */
4430 static void
4431do_filetype_autocmd(char_u **varp, int opt_flags, int value_changed)
4432{
4433 // Skip this when called from a modeline and the filetype was already set
4434 // to this value.
4435 if ((opt_flags & OPT_MODELINE) && !value_changed)
4436 return;
4437
4438 static int ft_recursive = 0;
4439 int secure_save = secure;
4440
4441 // Reset the secure flag, since the value of 'filetype' has
4442 // been checked to be safe.
4443 secure = 0;
4444
4445 ++ft_recursive;
zeertzjq5bf6c212024-03-31 18:41:27 +02004446 curbuf->b_did_filetype = TRUE;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004447 // Only pass TRUE for "force" when the value changed or not
4448 // used recursively, to avoid endless recurrence.
4449 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
4450 value_changed || ft_recursive == 1, curbuf);
4451 --ft_recursive;
4452 // Just in case the old "curbuf" is now invalid.
4453 if (varp != &(curbuf->b_p_ft))
4454 varp = NULL;
4455
4456 secure = secure_save;
4457}
4458
4459#ifdef FEAT_SPELL
4460/*
4461 * When the 'spelllang' option is set, source the spell/LANG.vim file in
4462 * 'runtimepath'.
4463 */
4464 static void
4465do_spelllang_source(void)
4466{
4467 char_u fname[200];
4468 char_u *p;
4469 char_u *q = curwin->w_s->b_p_spl;
4470
4471 // Skip the first name if it is "cjk".
4472 if (STRNCMP(q, "cjk,", 4) == 0)
4473 q += 4;
4474
4475 // They could set 'spellcapcheck' depending on the language. Use the first
4476 // name in 'spelllang' up to '_region' or '.encoding'.
4477 for (p = q; *p != NUL; ++p)
4478 if (!ASCII_ISALNUM(*p) && *p != '-')
4479 break;
4480 if (p > q)
4481 {
4482 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
4483 (int)(p - q), q);
4484 source_runtime(fname, DIP_ALL);
4485 }
4486}
4487#endif
4488
4489/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004490 * Handle string options that need some action to perform when changed.
zeertzjqf6782732022-07-27 18:26:03 +01004491 * The new value must be allocated.
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004492 * Returns NULL for success, or an untranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +02004493 */
4494 char *
4495did_set_string_option(
4496 int opt_idx, // index in options[] table
4497 char_u **varp, // pointer to the option variable
Bram Moolenaardac13472019-09-16 21:06:21 +02004498 char_u *oldval, // previous value of the option
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004499 char_u *value, // new value of the option
Bram Moolenaardac13472019-09-16 21:06:21 +02004500 char *errbuf, // buffer for errors, or NULL
Mike Williams620f0112023-12-05 15:36:06 +01004501 size_t errbuflen, // length of error buffer
Bram Moolenaardac13472019-09-16 21:06:21 +02004502 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004503 set_op_T op, // OP_ADDING/OP_PREPENDING/OP_REMOVING
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004504 int *value_checked) // value was checked to be safe, no
Bram Moolenaardac13472019-09-16 21:06:21 +02004505 // need to set P_INSECURE
4506{
4507 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +02004508 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004509 opt_did_set_cb_T did_set_cb = get_option_did_set_cb(opt_idx);
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004510 optset_T args;
4511
4512 // 'ttytype' is an alias for 'term'. Both 'term' and 'ttytype' point to
4513 // T_NAME. If 'term' or 'ttytype' is modified, then use the index for the
4514 // 'term' option. Only set the P_ALLOCED flag on 'term'.
4515 if (varp == &T_NAME)
4516 {
4517 opt_idx = findoption((char_u *)"term");
4518 if (opt_idx >= 0)
4519 {
4520 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
4521 did_set_cb = get_option_did_set_cb(opt_idx);
4522 }
4523 }
4524
4525 CLEAR_FIELD(args);
Bram Moolenaardac13472019-09-16 21:06:21 +02004526
Bram Moolenaardac13472019-09-16 21:06:21 +02004527 // Disallow changing some options from secure mode
4528 if ((secure
4529#ifdef HAVE_SANDBOX
4530 || sandbox != 0
4531#endif
4532 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00004533 errmsg = e_not_allowed_here;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004534 // Check for a "normal" directory or file name in some options.
4535 else if (check_illegal_path_names(opt_idx, varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004536 errmsg = e_invalid_argument;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004537 else if (did_set_cb != NULL)
4538 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004539 args.os_varp = (char_u *)varp;
4540 args.os_idx = opt_idx;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004541 args.os_flags = opt_flags;
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004542 args.os_op = op;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004543 args.os_oldval.string = oldval;
4544 args.os_newval.string = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004545 args.os_errbuf = errbuf;
Christian Brabandtb39b2402023-11-29 11:34:05 +01004546 args.os_errbuflen = errbuflen;
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004547 // Invoke the option specific callback function to validate and apply
4548 // the new option value.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004549 errmsg = did_set_cb(&args);
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004550
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004551 // The 'keymap', 'filetype' and 'syntax' option callback functions
4552 // may change the os_value_checked field.
4553 *value_checked = args.os_value_checked;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004554 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004555
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004556 // If an error is detected, restore the previous value.
Bram Moolenaardac13472019-09-16 21:06:21 +02004557 if (errmsg != NULL)
4558 {
zeertzjqf6782732022-07-27 18:26:03 +01004559 free_string_option(*varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02004560 *varp = oldval;
4561 // When resetting some values, need to act on it.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004562 if (args.os_restore_chartab)
Bram Moolenaardac13472019-09-16 21:06:21 +02004563 (void)init_chartab();
4564 if (varp == &p_hl)
4565 (void)highlight_changed();
4566 }
4567 else
4568 {
4569#ifdef FEAT_EVAL
4570 // Remember where the option was set.
4571 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4572#endif
4573 // Free string options that are in allocated memory.
4574 // Use "free_oldval", because recursiveness may change the flags under
4575 // our fingers (esp. init_highlight()).
4576 if (free_oldval)
4577 free_string_option(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01004578 set_option_flag(opt_idx, P_ALLOCED);
Bram Moolenaardac13472019-09-16 21:06:21 +02004579
4580 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
4581 && is_global_local_option(opt_idx))
4582 {
4583 // global option with local value set to use global value; free
4584 // the local value and make it empty
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004585 char_u *p = get_option_varp_scope(opt_idx, OPT_LOCAL);
Bram Moolenaardac13472019-09-16 21:06:21 +02004586 free_string_option(*(char_u **)p);
4587 *(char_u **)p = empty_option;
4588 }
4589
4590 // May set global value for local option.
4591 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
4592 set_string_option_global(opt_idx, varp);
4593
4594 // Trigger the autocommand only after setting the flags.
4595#ifdef FEAT_SYN_HL
Bram Moolenaardac13472019-09-16 21:06:21 +02004596 if (varp == &(curbuf->b_p_syn))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004597 do_syntax_autocmd(args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004598#endif
4599 else if (varp == &(curbuf->b_p_ft))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004600 do_filetype_autocmd(varp, opt_flags, args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004601#ifdef FEAT_SPELL
4602 if (varp == &(curwin->w_s->b_p_spl))
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004603 do_spelllang_source();
Bram Moolenaardac13472019-09-16 21:06:21 +02004604#endif
4605 }
4606
Bram Moolenaardac13472019-09-16 21:06:21 +02004607 if (varp == &p_mouse)
4608 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004609 if (*p_mouse == NUL)
4610 mch_setmouse(FALSE); // switch mouse off
4611 else
Bram Moolenaardac13472019-09-16 21:06:21 +02004612 setmouse(); // in case 'mouse' changed
4613 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004614
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004615#if defined(FEAT_LUA) || defined(PROTO)
4616 if (varp == &p_rtp)
4617 update_package_paths_in_lua();
4618#endif
4619
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004620#if defined(FEAT_LINEBREAK)
4621 // Changing Formatlistpattern when briopt includes the list setting:
4622 // redraw
4623 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
4624 && curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004625 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004626#endif
4627
Bram Moolenaardac13472019-09-16 21:06:21 +02004628 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004629 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0
4630 && (get_option_flags(opt_idx) & P_HLONLY) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02004631 curwin->w_set_curswant = TRUE;
4632
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004633 if ((opt_flags & OPT_NO_REDRAW) == 0)
4634 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004635#ifdef FEAT_GUI
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004636 // set when changing an option that only requires a redraw in the GUI
4637 int redraw_gui_only = FALSE;
4638
4639 if (varp == &p_go // 'guioptions'
4640 || varp == &p_guifont // 'guifont'
4641# ifdef FEAT_GUI_TABLINE
4642 || varp == &p_gtl // 'guitablabel'
4643 || varp == &p_gtt // 'guitabtooltip'
4644# endif
4645# ifdef FEAT_XFONTSET
4646 || varp == &p_guifontset // 'guifontset'
4647# endif
4648 || varp == &p_guifontwide // 'guifontwide'
Erik S. V. Jansson2f026382024-02-26 22:23:05 +01004649# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004650 || varp == &p_guiligatures // 'guiligatures'
4651# endif
4652 )
4653 redraw_gui_only = TRUE;
4654
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004655 // check redraw when it's not a GUI option or the GUI is active.
4656 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02004657#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004658 check_redraw(get_option_flags(opt_idx));
4659 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004660
4661#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004662 if (args.os_did_swaptcap)
Bram Moolenaardac13472019-09-16 21:06:21 +02004663 {
4664 set_termname((char_u *)"win32");
4665 init_highlight(TRUE, FALSE);
4666 }
4667#endif
4668
4669 return errmsg;
4670}
4671
4672/*
4673 * Check an option that can be a range of string values.
4674 *
4675 * Return OK for correct value, FAIL otherwise.
4676 * Empty is always OK.
4677 */
4678 static int
4679check_opt_strings(
4680 char_u *val,
4681 char **values,
4682 int list) // when TRUE: accept a list of values
4683{
4684 return opt_strings_flags(val, values, NULL, list);
4685}
4686
4687/*
4688 * Handle an option that can be a range of string values.
4689 * Set a flag in "*flagp" for each string present.
4690 *
4691 * Return OK for correct value, FAIL otherwise.
4692 * Empty is always OK.
4693 */
4694 static int
4695opt_strings_flags(
4696 char_u *val, // new value
4697 char **values, // array of valid string values
4698 unsigned *flagp,
4699 int list) // when TRUE: accept a list of values
4700{
4701 int i;
4702 int len;
4703 unsigned new_flags = 0;
4704
4705 while (*val)
4706 {
4707 for (i = 0; ; ++i)
4708 {
4709 if (values[i] == NULL) // val not found in values[]
4710 return FAIL;
4711
4712 len = (int)STRLEN(values[i]);
4713 if (STRNCMP(values[i], val, len) == 0
4714 && ((list && val[len] == ',') || val[len] == NUL))
4715 {
4716 val += len + (val[len] == ',');
4717 new_flags |= (1 << i);
4718 break; // check next item in val list
4719 }
4720 }
4721 }
4722 if (flagp != NULL)
4723 *flagp = new_flags;
4724
4725 return OK;
4726}
4727
4728/*
4729 * return OK if "p" is a valid fileformat name, FAIL otherwise.
4730 */
4731 int
4732check_ff_value(char_u *p)
4733{
4734 return check_opt_strings(p, p_ff_values, FALSE);
4735}
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004736
4737/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004738 * Save the actual shortmess Flags and clear them temporarily to avoid that
4739 * file messages overwrites any output from the following commands.
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004740 *
4741 * Caller must make sure to first call save_clear_shm_value() and then
4742 * restore_shm_value() exactly the same number of times.
4743 */
4744 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004745save_clear_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004746{
4747 if (STRLEN(p_shm) >= SHM_LEN)
4748 {
4749 iemsg(e_internal_error_shortmess_too_long);
4750 return;
4751 }
4752
4753 if (++set_shm_recursive == 1)
4754 {
4755 STRCPY(shm_buf, p_shm);
4756 set_option_value_give_err((char_u *)"shm", 0L, (char_u *)"", 0);
4757 }
4758}
4759
4760/*
4761 * Restore the shortmess Flags set from the save_clear_shm_value() function.
4762 */
4763 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004764restore_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004765{
4766 if (--set_shm_recursive == 0)
4767 {
4768 set_option_value_give_err((char_u *)"shm", 0L, shm_buf, 0);
4769 vim_memset(shm_buf, 0, SHM_LEN);
4770 }
4771}