blob: 7c589e9173cfd66077482bb2c73972a30b78a151 [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 }
zeertzjq46dcd842024-11-03 09:10:50 +01001149 else if (!(args->os_flags & OPT_GLOBAL))
1150 // When using :set, clear the local flags.
1151 curbuf->b_bkc_flags = 0;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001152
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001153 if ((args->os_flags & OPT_LOCAL) && *bkc == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001154 // make the local value empty: use the global value
1155 *flags = 0;
1156 else
1157 {
1158 if (opt_strings_flags(bkc, p_bkc_values, flags, TRUE) != OK)
1159 errmsg = e_invalid_argument;
1160 if ((((int)*flags & BKC_AUTO) != 0)
1161 + (((int)*flags & BKC_YES) != 0)
1162 + (((int)*flags & BKC_NO) != 0) != 1)
1163 {
1164 // Must have exactly one of "auto", "yes" and "no".
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001165 (void)opt_strings_flags(args->os_oldval.string, p_bkc_values,
1166 flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001167 errmsg = e_invalid_argument;
1168 }
1169 }
1170
1171 return errmsg;
1172}
1173
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001174 int
1175expand_set_backupcopy(optexpand_T *args, int *numMatches, char_u ***matches)
1176{
1177 return expand_set_opt_string(
1178 args,
1179 p_bkc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001180 ARRAY_LENGTH(p_bkc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001181 numMatches,
1182 matches);
1183}
1184
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001185/*
1186 * The 'backupext' or the 'patchmode' option is changed.
1187 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001188 char *
1189did_set_backupext_or_patchmode(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001190{
1191 if (STRCMP(*p_bex == '.' ? p_bex + 1 : p_bex,
1192 *p_pm == '.' ? p_pm + 1 : p_pm) == 0)
1193 return e_backupext_and_patchmode_are_equal;
1194
1195 return NULL;
1196}
1197
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001198/*
1199 * The 'belloff' option is changed.
1200 */
1201 char *
1202did_set_belloff(optset_T *args UNUSED)
1203{
1204 return did_set_opt_flags(p_bo, p_bo_values, &bo_flags, TRUE);
1205}
1206
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001207 int
1208expand_set_belloff(optexpand_T *args, int *numMatches, char_u ***matches)
1209{
1210 return expand_set_opt_string(
1211 args,
1212 p_bo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001213 ARRAY_LENGTH(p_bo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001214 numMatches,
1215 matches);
1216}
1217
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001218#if defined(FEAT_LINEBREAK) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001219/*
zeertzjqeac3fdc2024-02-03 18:08:09 +01001220 * The 'breakat' option is changed.
1221 */
1222 char *
1223did_set_breakat(optset_T *args UNUSED)
1224{
1225 char_u *p;
1226 int i;
1227
1228 for (i = 0; i < 256; i++)
1229 breakat_flags[i] = FALSE;
1230
1231 if (p_breakat != NULL)
1232 for (p = p_breakat; *p; p++)
1233 breakat_flags[*p] = TRUE;
1234
1235 return NULL;
1236}
1237
1238/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001239 * The 'breakindentopt' option is changed.
1240 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001241 char *
Millyb38700a2024-10-22 22:59:39 +02001242did_set_breakindentopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001243{
Millyb38700a2024-10-22 22:59:39 +02001244 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001245
Millyb38700a2024-10-22 22:59:39 +02001246 if (briopt_check(*varp, varp == &curwin->w_p_briopt ? curwin : NULL)
1247 == FAIL)
1248 return e_invalid_argument;
1249
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001250 // list setting requires a redraw
Millyb38700a2024-10-22 22:59:39 +02001251 if (varp == &curwin->w_p_briopt && curwin->w_briopt_list)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001252 redraw_all_later(UPD_NOT_VALID);
1253
Millyb38700a2024-10-22 22:59:39 +02001254 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001255}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001256
1257 int
1258expand_set_breakindentopt(optexpand_T *args, int *numMatches, char_u ***matches)
1259{
1260 return expand_set_opt_string(
1261 args,
1262 p_briopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001263 ARRAY_LENGTH(p_briopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001264 numMatches,
1265 matches);
1266}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001267#endif
1268
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001269#if defined(FEAT_BROWSE) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001270/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001271 * The 'browsedir' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001272 */
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00001273 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001274did_set_browsedir(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001275{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001276 if (check_opt_strings(p_bsdir, p_bsdir_values, FALSE) != OK
1277 && !mch_isdir(p_bsdir))
1278 return e_invalid_argument;
1279
1280 return NULL;
1281}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001282
1283 int
1284expand_set_browsedir(optexpand_T *args, int *numMatches, char_u ***matches)
1285{
1286 return expand_set_opt_string(
1287 args,
1288 p_bsdir_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001289 ARRAY_LENGTH(p_bsdir_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001290 numMatches,
1291 matches);
1292}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001293#endif
1294
1295/*
1296 * The 'bufhidden' option is changed.
1297 */
1298 char *
1299did_set_bufhidden(optset_T *args UNUSED)
1300{
1301 return did_set_opt_strings(curbuf->b_p_bh, p_bufhidden_values, FALSE);
1302}
1303
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001304 int
1305expand_set_bufhidden(optexpand_T *args, int *numMatches, char_u ***matches)
1306{
1307 return expand_set_opt_string(
1308 args,
1309 p_bufhidden_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001310 ARRAY_LENGTH(p_bufhidden_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001311 numMatches,
1312 matches);
1313}
1314
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001315/*
1316 * The 'buftype' option is changed.
1317 */
1318 char *
1319did_set_buftype(optset_T *args UNUSED)
1320{
1321 if (check_opt_strings(curbuf->b_p_bt, p_buftype_values, FALSE) != OK)
1322 return e_invalid_argument;
1323
1324 if (curwin->w_status_height)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001325 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001326 curwin->w_redr_status = TRUE;
1327 redraw_later(UPD_VALID);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001328 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001329 curbuf->b_help = (curbuf->b_p_bt[0] == 'h');
1330 redraw_titles();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001331
1332 return NULL;
1333}
1334
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001335 int
1336expand_set_buftype(optexpand_T *args, int *numMatches, char_u ***matches)
1337{
1338 return expand_set_opt_string(
1339 args,
1340 p_buftype_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001341 ARRAY_LENGTH(p_buftype_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001342 numMatches,
1343 matches);
1344}
1345
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001346/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001347 * The 'casemap' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001348 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001349 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001350did_set_casemap(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001351{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001352 return did_set_opt_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
1353}
1354
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001355 int
1356expand_set_casemap(optexpand_T *args, int *numMatches, char_u ***matches)
1357{
1358 return expand_set_opt_string(
1359 args,
1360 p_cmp_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001361 ARRAY_LENGTH(p_cmp_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001362 numMatches,
1363 matches);
1364}
1365
1366#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1367 int
1368expand_set_clipboard(optexpand_T *args, int *numMatches, char_u ***matches)
1369{
1370 return expand_set_opt_string(
1371 args,
1372 p_cb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001373 ARRAY_LENGTH(p_cb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001374 numMatches,
1375 matches);
1376}
1377#endif
1378
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001379/*
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001380 * The global 'listchars' or 'fillchars' option is changed.
1381 */
1382 static char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01001383did_set_global_listfillchars(char_u *val, int opt_lcs, int opt_flags,
1384 char *errbuf, size_t errbuflen)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001385{
1386 char *errmsg = NULL;
1387 char_u **local_ptr = opt_lcs ? &curwin->w_p_lcs : &curwin->w_p_fcs;
1388
1389 // only apply the global value to "curwin" when it does not have a
1390 // local value
1391 if (opt_lcs)
1392 errmsg = set_listchars_option(curwin, val,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001393 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL),
1394 errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001395 else
1396 errmsg = set_fillchars_option(curwin, val,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001397 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL),
1398 errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001399 if (errmsg != NULL)
1400 return errmsg;
1401
1402 tabpage_T *tp;
1403 win_T *wp;
1404
1405 // If the current window is set to use the global
1406 // 'listchars'/'fillchars' value, clear the window-local value.
1407 if (!(opt_flags & OPT_GLOBAL))
1408 clear_string_option(local_ptr);
1409 FOR_ALL_TAB_WINDOWS(tp, wp)
1410 {
1411 // If the current window has a local value need to apply it
1412 // again, it was changed when setting the global value.
1413 // If no error was returned above, we don't expect an error
1414 // here, so ignore the return value.
1415 if (opt_lcs)
1416 {
1417 if (*wp->w_p_lcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001418 (void)set_listchars_option(wp, wp->w_p_lcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001419 }
1420 else
1421 {
1422 if (*wp->w_p_fcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001423 (void)set_fillchars_option(wp, wp->w_p_fcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001424 }
1425 }
1426
1427 redraw_all_later(UPD_NOT_VALID);
1428
1429 return NULL;
1430}
1431
1432/*
1433 * The 'fillchars' option or the 'listchars' option is changed.
1434 */
1435 char *
1436did_set_chars_option(optset_T *args)
1437{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001438 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001439 char *errmsg = NULL;
1440
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001441 if ( varp == &p_lcs // global 'listchars'
1442 || varp == &p_fcs) // global 'fillchars'
1443 errmsg = did_set_global_listfillchars(*varp, varp == &p_lcs,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001444 args->os_flags, args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001445 else if (varp == &curwin->w_p_lcs) // local 'listchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001446 errmsg = set_listchars_option(curwin, *varp, TRUE,
1447 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001448 else if (varp == &curwin->w_p_fcs) // local 'fillchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001449 errmsg = set_fillchars_option(curwin, *varp, TRUE,
1450 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001451
1452 return errmsg;
1453}
1454
1455/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001456 * Expand 'fillchars' or 'listchars' option value.
1457 */
1458 int
1459expand_set_chars_option(optexpand_T *args, int *numMatches, char_u ***matches)
1460{
1461 char_u **varp = (char_u **)args->oe_varp;
1462 int is_lcs = (varp == &p_lcs || varp == &curwin->w_p_lcs);
1463 return expand_set_opt_generic(
1464 args,
1465 is_lcs ? get_listchars_name : get_fillchars_name,
1466 numMatches,
1467 matches);
1468}
1469
1470/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001471 * The 'cinoptions' option is changed.
1472 */
1473 char *
1474did_set_cinoptions(optset_T *args UNUSED)
1475{
1476 // TODO: recognize errors
1477 parse_cino(curbuf);
1478
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001479 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001480}
1481
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00001482#if defined(FEAT_SYN_HL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001483/*
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001484 * The 'colorcolumn' option is changed.
1485 */
1486 char *
Millya441a3e2024-10-22 22:43:01 +02001487did_set_colorcolumn(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001488{
Millya441a3e2024-10-22 22:43:01 +02001489 char_u **varp = (char_u **)args->os_varp;
1490
1491 return check_colorcolumn(*varp, varp == &curwin->w_p_cc ? curwin : NULL);
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001492}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001493#endif
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001494
1495/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001496 * The 'comments' option is changed.
1497 */
1498 char *
1499did_set_comments(optset_T *args)
1500{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001501 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001502 char_u *s;
1503 char *errmsg = NULL;
1504
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001505 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001506 {
1507 while (*s && *s != ':')
1508 {
1509 if (vim_strchr((char_u *)COM_ALL, *s) == NULL
1510 && !VIM_ISDIGIT(*s) && *s != '-')
1511 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01001512 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001513 break;
1514 }
1515 ++s;
1516 }
1517 if (*s++ == NUL)
1518 errmsg = e_missing_colon;
1519 else if (*s == ',' || *s == NUL)
1520 errmsg = e_zero_length_string;
1521 if (errmsg != NULL)
1522 break;
1523 while (*s && *s != ',')
1524 {
1525 if (*s == '\\' && s[1] != NUL)
1526 ++s;
1527 ++s;
1528 }
1529 s = skip_to_option_part(s);
1530 }
1531
1532 return errmsg;
1533}
1534
1535#if defined(FEAT_FOLDING) || defined(PROTO)
1536/*
1537 * The 'commentstring' option is changed.
1538 */
1539 char *
1540did_set_commentstring(optset_T *args)
1541{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001542 char_u **varp = (char_u **)args->os_varp;
1543
1544 if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001545 return e_commentstring_must_be_empty_or_contain_str;
1546
1547 return NULL;
1548}
1549#endif
1550
1551/*
1552 * The 'complete' option is changed.
1553 */
1554 char *
1555did_set_complete(optset_T *args)
1556{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001557 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001558 char_u *s;
1559
1560 // check if it is a valid value for 'complete' -- Acevedo
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001561 for (s = *varp; *s;)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001562 {
1563 while (*s == ',' || *s == ' ')
1564 s++;
1565 if (!*s)
1566 break;
1567 if (vim_strchr((char_u *)".wbuksid]tU", *s) == NULL)
Christian Brabandtb39b2402023-11-29 11:34:05 +01001568 return illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001569 if (*++s != NUL && *s != ',' && *s != ' ')
1570 {
1571 if (s[-1] == 'k' || s[-1] == 's')
1572 {
1573 // skip optional filename after 'k' and 's'
1574 while (*s && *s != ',' && *s != ' ')
1575 {
1576 if (*s == '\\' && s[1] != NUL)
1577 ++s;
1578 ++s;
1579 }
1580 }
1581 else
1582 {
1583 if (args->os_errbuf != NULL)
1584 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01001585 vim_snprintf((char *)args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001586 _(e_illegal_character_after_chr), *--s);
1587 return args->os_errbuf;
1588 }
1589 return "";
1590 }
1591 }
1592 }
1593
1594 return NULL;
1595}
1596
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001597 int
1598expand_set_complete(optexpand_T *args, int *numMatches, char_u ***matches)
1599{
1600 static char *(p_cpt_values[]) = {
1601 ".", "w", "b", "u", "k", "kspell", "s", "i", "d", "]", "t", "U",
1602 NULL};
1603 return expand_set_opt_string(
1604 args,
1605 p_cpt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001606 ARRAY_LENGTH(p_cpt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001607 numMatches,
1608 matches);
1609}
1610
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001611/*
1612 * The 'completeopt' option is changed.
1613 */
1614 char *
1615did_set_completeopt(optset_T *args UNUSED)
1616{
zeertzjq529b9ad2024-06-05 20:27:06 +02001617 char_u *cot = p_cot;
1618 unsigned *flags = &cot_flags;
1619
1620 if (args->os_flags & OPT_LOCAL)
1621 {
1622 cot = curbuf->b_p_cot;
1623 flags = &curbuf->b_cot_flags;
1624 }
zeertzjq46dcd842024-11-03 09:10:50 +01001625 else if (!(args->os_flags & OPT_GLOBAL))
1626 // When using :set, clear the local flags.
1627 curbuf->b_cot_flags = 0;
zeertzjq529b9ad2024-06-05 20:27:06 +02001628
1629 if (check_opt_strings(cot, p_cot_values, TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001630 return e_invalid_argument;
1631
zeertzjq529b9ad2024-06-05 20:27:06 +02001632 if (opt_strings_flags(cot, p_cot_values, flags, TRUE) != OK)
1633 return e_invalid_argument;
1634
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001635 return NULL;
1636}
1637
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001638 int
1639expand_set_completeopt(optexpand_T *args, int *numMatches, char_u ***matches)
1640{
1641 return expand_set_opt_string(
1642 args,
1643 p_cot_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001644 ARRAY_LENGTH(p_cot_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001645 numMatches,
1646 matches);
1647}
1648
glepnir6a89c942024-10-01 20:32:12 +02001649/*
1650 * The 'completeitemalign' option is changed.
1651 */
1652 char *
1653did_set_completeitemalign(optset_T *args UNUSED)
1654{
1655 char_u *p = p_cia;
1656 unsigned new_cia_flags = 0;
1657 int seen[3] = { FALSE, FALSE, FALSE };
1658 int count = 0;
1659 char_u buf[10];
1660
1661 while (*p)
1662 {
1663 copy_option_part(&p, buf, sizeof(buf), ",");
1664 if (count >= 3)
1665 return e_invalid_argument;
1666
1667 if (STRCMP(buf, "abbr") == 0)
1668 {
1669 if (seen[CPT_ABBR])
1670 return e_invalid_argument;
1671 new_cia_flags = new_cia_flags * 10 + CPT_ABBR;
1672 seen[CPT_ABBR] = TRUE;
1673 count++;
1674 }
1675 else if (STRCMP(buf, "kind") == 0)
1676 {
1677 if (seen[CPT_KIND])
1678 return e_invalid_argument;
1679 new_cia_flags = new_cia_flags * 10 + CPT_KIND;
1680 seen[CPT_KIND] = TRUE;
1681 count++;
1682 }
1683 else if (STRCMP(buf, "menu") == 0)
1684 {
1685 if (seen[CPT_MENU])
1686 return e_invalid_argument;
1687 new_cia_flags = new_cia_flags * 10 + CPT_MENU;
1688 seen[CPT_MENU] = TRUE;
1689 count++;
1690 }
1691 else
1692 return e_invalid_argument;
1693 }
1694 if (new_cia_flags == 0 || count != 3)
1695 return e_invalid_argument;
1696
1697 cia_flags = new_cia_flags;
1698 return NULL;
1699}
1700
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001701#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1702/*
1703 * The 'completepopup' option is changed.
1704 */
1705 char *
1706did_set_completepopup(optset_T *args UNUSED)
1707{
1708 if (parse_completepopup(NULL) == FAIL)
1709 return e_invalid_argument;
1710
1711 popup_close_info();
1712 return NULL;
1713}
1714#endif
1715
1716#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1717/*
1718 * The 'completeslash' option is changed.
1719 */
1720 char *
1721did_set_completeslash(optset_T *args UNUSED)
1722{
1723 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1724 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
1725 return e_invalid_argument;
1726
1727 return NULL;
1728}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001729
1730 int
1731expand_set_completeslash(optexpand_T *args, int *numMatches, char_u ***matches)
1732{
1733 return expand_set_opt_string(
1734 args,
1735 p_csl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001736 ARRAY_LENGTH(p_csl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001737 numMatches,
1738 matches);
1739}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001740#endif
1741
1742#if defined(FEAT_CONCEAL) || defined(PROTO)
1743/*
1744 * The 'concealcursor' option is changed.
1745 */
1746 char *
1747did_set_concealcursor(optset_T *args)
1748{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001749 char_u **varp = (char_u **)args->os_varp;
1750
Christian Brabandtb39b2402023-11-29 11:34:05 +01001751 return did_set_option_listflag(*varp, (char_u *)COCU_ALL, args->os_errbuf,
1752 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001753}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001754
1755 int
1756expand_set_concealcursor(optexpand_T *args, int *numMatches, char_u ***matches)
1757{
1758 return expand_set_opt_listflag(args, (char_u*)COCU_ALL, numMatches, matches);
1759}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001760#endif
1761
1762/*
1763 * The 'cpoptions' option is changed.
1764 */
1765 char *
1766did_set_cpoptions(optset_T *args)
1767{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001768 char_u **varp = (char_u **)args->os_varp;
1769
Christian Brabandtb39b2402023-11-29 11:34:05 +01001770 return did_set_option_listflag(*varp, (char_u *)CPO_ALL, args->os_errbuf,
1771 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001772}
1773
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001774 int
1775expand_set_cpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
1776{
1777 return expand_set_opt_listflag(args, (char_u*)CPO_ALL, numMatches, matches);
1778}
1779
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001780#if defined(FEAT_CRYPT) || defined(PROTO)
1781/*
1782 * The 'cryptkey' option is changed.
1783 */
1784 char *
1785did_set_cryptkey(optset_T *args)
1786{
1787 // Make sure the ":set" command doesn't show the new value in the
1788 // history.
1789 remove_key_from_history();
1790
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02001791 if (args->os_op != OP_NONE)
1792 // Don't allow set+=/-=/^= as they can allow for substring guessing
1793 return e_invalid_argument;
1794
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001795 if (STRCMP(curbuf->b_p_key, args->os_oldval.string) != 0)
1796 {
1797 // Need to update the swapfile.
1798 ml_set_crypt_key(curbuf, args->os_oldval.string,
1799 *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
1800 changed_internal();
1801 }
Christian Brabandt19e6c4f2023-06-27 18:57:10 +01001802# ifdef FEAT_SODIUM
1803 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
1804 crypt_sodium_lock_key(args->os_newval.string);
1805# endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001806
1807 return NULL;
1808}
1809
1810/*
1811 * The 'cryptmethod' option is changed.
1812 */
1813 char *
1814did_set_cryptmethod(optset_T *args)
1815{
1816 char_u *p;
1817 char_u *s;
1818
1819 if (args->os_flags & OPT_LOCAL)
1820 p = curbuf->b_p_cm;
1821 else
1822 p = p_cm;
1823 if (check_opt_strings(p, p_cm_values, TRUE) != OK)
1824 return e_invalid_argument;
1825 else if (crypt_self_test() == FAIL)
1826 return e_invalid_argument;
1827
1828 // When setting the global value to empty, make it "zip".
1829 if (*p_cm == NUL)
1830 {
1831 free_string_option(p_cm);
1832 p_cm = vim_strsave((char_u *)"zip");
1833 }
1834 // When using ":set cm=name" the local value is going to be empty.
1835 // Do that here, otherwise the crypt functions will still use the
1836 // local value.
1837 if ((args->os_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1838 {
1839 free_string_option(curbuf->b_p_cm);
1840 curbuf->b_p_cm = empty_option;
1841 }
1842
1843 // Need to update the swapfile when the effective method changed.
1844 // Set "s" to the effective old value, "p" to the effective new
1845 // method and compare.
1846 if ((args->os_flags & OPT_LOCAL) && *args->os_oldval.string == NUL)
1847 s = p_cm; // was previously using the global value
1848 else
1849 s = args->os_oldval.string;
1850 if (*curbuf->b_p_cm == NUL)
1851 p = p_cm; // is now using the global value
1852 else
1853 p = curbuf->b_p_cm;
1854 if (STRCMP(s, p) != 0)
1855 ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1856
1857 // If the global value changes need to update the swapfile for all
1858 // buffers using that value.
1859 if ((args->os_flags & OPT_GLOBAL)
1860 && STRCMP(p_cm, args->os_oldval.string) != 0)
1861 {
1862 buf_T *buf;
1863
1864 FOR_ALL_BUFFERS(buf)
1865 if (buf != curbuf && *buf->b_p_cm == NUL)
1866 ml_set_crypt_key(buf, buf->b_p_key, args->os_oldval.string);
1867 }
1868 return NULL;
1869}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001870
1871 int
1872expand_set_cryptmethod(optexpand_T *args, int *numMatches, char_u ***matches)
1873{
1874 return expand_set_opt_string(
1875 args,
1876 p_cm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001877 ARRAY_LENGTH(p_cm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001878 numMatches,
1879 matches);
1880}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001881#endif
1882
1883#if (defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1884/*
1885 * The 'cscopequickfix' option is changed.
1886 */
1887 char *
1888did_set_cscopequickfix(optset_T *args UNUSED)
1889{
1890 char_u *p;
1891
1892 if (p_csqf == NULL)
1893 return NULL;
1894
1895 p = p_csqf;
1896 while (*p != NUL)
1897 {
1898 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
1899 || p[1] == NUL
1900 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
1901 || (p[2] != NUL && p[2] != ','))
1902 return e_invalid_argument;
1903 else if (p[2] == NUL)
1904 break;
1905 else
1906 p += 3;
1907 }
1908
1909 return NULL;
1910}
1911#endif
1912
1913#if defined(FEAT_SYN_HL) || defined(PROTO)
1914/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001915 * The 'cursorlineopt' option is changed.
1916 */
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001917 char *
1918did_set_cursorlineopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001919{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001920 char_u **varp = (char_u **)args->os_varp;
1921
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001922 // This could be changed to use opt_strings_flags() instead.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001923 if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001924 return e_invalid_argument;
1925
1926 return NULL;
1927}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001928
1929 int
1930expand_set_cursorlineopt(optexpand_T *args, int *numMatches, char_u ***matches)
1931{
1932 return expand_set_opt_string(
1933 args,
1934 p_culopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001935 ARRAY_LENGTH(p_culopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001936 numMatches,
1937 matches);
1938}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001939#endif
1940
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001941/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001942 * The 'debug' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001943 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001944 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001945did_set_debug(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001946{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001947 return did_set_opt_strings(p_debug, p_debug_values, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001948}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001949
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001950 int
1951expand_set_debug(optexpand_T *args, int *numMatches, char_u ***matches)
1952{
1953 return expand_set_opt_string(
1954 args,
1955 p_debug_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001956 ARRAY_LENGTH(p_debug_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001957 numMatches,
1958 matches);
1959}
1960
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001961#if defined(FEAT_DIFF) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001962/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001963 * The 'diffopt' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001964 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001965 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001966did_set_diffopt(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001967{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001968 if (diffopt_changed() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001969 return e_invalid_argument;
1970
1971 return NULL;
1972}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001973
1974 int
1975expand_set_diffopt(optexpand_T *args, int *numMatches, char_u ***matches)
1976{
1977 expand_T *xp = args->oe_xp;
1978
1979 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
1980 {
1981 // Within "algorithm:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001982 int algo_len = (int)STRLEN("algorithm:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001983 if (xp->xp_pattern - args->oe_set_arg >= algo_len &&
1984 STRNCMP(xp->xp_pattern - algo_len, "algorithm:", algo_len) == 0)
1985 {
1986 return expand_set_opt_string(
1987 args,
1988 p_dip_algorithm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001989 ARRAY_LENGTH(p_dip_algorithm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001990 numMatches,
1991 matches);
1992 }
1993 return FAIL;
1994 }
1995
1996 return expand_set_opt_string(
1997 args,
1998 p_dip_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001999 ARRAY_LENGTH(p_dip_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002000 numMatches,
2001 matches);
2002}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002003#endif
2004
2005/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002006 * The 'display' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002007 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002008 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002009did_set_display(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002010{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002011 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002012 return e_invalid_argument;
2013
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002014 (void)init_chartab();
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002015 return NULL;
2016}
2017
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002018 int
2019expand_set_display(optexpand_T *args, int *numMatches, char_u ***matches)
2020{
2021 return expand_set_opt_string(
2022 args,
2023 p_dy_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002024 ARRAY_LENGTH(p_dy_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002025 numMatches,
2026 matches);
2027}
2028
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002029/*
2030 * The 'eadirection' option is changed.
2031 */
2032 char *
2033did_set_eadirection(optset_T *args UNUSED)
2034{
2035 return did_set_opt_strings(p_ead, p_ead_values, FALSE);
2036}
2037
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002038 int
2039expand_set_eadirection(optexpand_T *args, int *numMatches, char_u ***matches)
2040{
2041 return expand_set_opt_string(
2042 args,
2043 p_ead_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002044 ARRAY_LENGTH(p_ead_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002045 numMatches,
2046 matches);
2047}
2048
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002049/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002050 * One of the 'encoding', 'fileencoding', 'termencoding' or 'makeencoding'
2051 * options is changed.
2052 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002053 char *
2054did_set_encoding(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002055{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002056 char_u **varp = (char_u **)args->os_varp;
2057 char_u **gvarp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002058 char *errmsg = NULL;
2059 char_u *p;
2060
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002061 // Get the global option to compare with, otherwise we would have to check
2062 // two values for all local options.
2063 gvarp = (char_u **)get_option_varp_scope(args->os_idx, OPT_GLOBAL);
2064
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002065 if (gvarp == &p_fenc)
2066 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002067 if (!curbuf->b_p_ma && args->os_flags != OPT_GLOBAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002068 errmsg = e_cannot_make_changes_modifiable_is_off;
2069 else if (vim_strchr(*varp, ',') != NULL)
2070 // No comma allowed in 'fileencoding'; catches confusing it
2071 // with 'fileencodings'.
2072 errmsg = e_invalid_argument;
2073 else
2074 {
2075 // May show a "+" in the title now.
2076 redraw_titles();
2077 // Add 'fileencoding' to the swap file.
2078 ml_setflags(curbuf);
2079 }
2080 }
2081 if (errmsg == NULL)
2082 {
2083 // canonize the value, so that STRCMP() can be used on it
2084 p = enc_canonize(*varp);
2085 if (p != NULL)
2086 {
2087 vim_free(*varp);
2088 *varp = p;
2089 }
2090 if (varp == &p_enc)
2091 {
2092 errmsg = mb_init();
2093 redraw_titles();
2094 }
2095 }
2096
2097#if defined(FEAT_GUI_GTK)
2098 if (errmsg == NULL && varp == &p_tenc && gui.in_use)
2099 {
2100 // GTK uses only a single encoding, and that is UTF-8.
2101 if (STRCMP(p_tenc, "utf-8") != 0)
2102 errmsg = e_cannot_be_changed_in_gtk_GUI;
2103 }
2104#endif
2105
2106 if (errmsg == NULL)
2107 {
2108#ifdef FEAT_KEYMAP
2109 // When 'keymap' is used and 'encoding' changes, reload the keymap
2110 // (with another encoding).
2111 if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
2112 (void)keymap_init();
2113#endif
2114
2115 // When 'termencoding' is not empty and 'encoding' changes or when
2116 // 'termencoding' changes, need to setup for keyboard input and
2117 // display output conversion.
2118 if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
2119 {
2120 if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
2121 || convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
2122 {
2123 semsg(_(e_cannot_convert_between_str_and_str),
2124 p_tenc, p_enc);
2125 errmsg = e_invalid_argument;
2126 }
2127 }
2128
2129#if defined(MSWIN)
K.Takatace3189d2023-02-15 19:13:43 +00002130 // $HOME, $VIM and $VIMRUNTIME may have characters in active code page.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002131 if (varp == &p_enc)
K.Takatace3189d2023-02-15 19:13:43 +00002132 {
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002133 init_homedir();
K.Takatace3189d2023-02-15 19:13:43 +00002134 init_vimdir();
2135 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002136#endif
2137 }
2138
2139 return errmsg;
2140}
2141
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002142 int
2143expand_set_encoding(optexpand_T *args, int *numMatches, char_u ***matches)
2144{
2145 return expand_set_opt_generic(
2146 args,
2147 get_encoding_name,
2148 numMatches,
2149 matches);
2150}
2151
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002152/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002153 * The 'eventignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002154 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002155 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002156did_set_eventignore(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002157{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002158 if (check_ei() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002159 return e_invalid_argument;
2160 return NULL;
2161}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002162
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002163 static char_u *
2164get_eventignore_name(expand_T *xp, int idx)
2165{
2166 // 'eventignore' allows special keyword "all" in addition to
2167 // all event names.
2168 if (idx == 0)
2169 return (char_u *)"all";
2170 return get_event_name_no_group(xp, idx - 1);
2171}
2172
2173 int
2174expand_set_eventignore(optexpand_T *args, int *numMatches, char_u ***matches)
2175{
2176 return expand_set_opt_generic(
2177 args,
2178 get_eventignore_name,
2179 numMatches,
2180 matches);
2181}
2182
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002183/*
2184 * The 'fileformat' option is changed.
2185 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002186 char *
2187did_set_fileformat(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002188{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002189 char_u **varp = (char_u **)args->os_varp;
2190
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002191 if (!curbuf->b_p_ma && !(args->os_flags & OPT_GLOBAL))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002192 return e_cannot_make_changes_modifiable_is_off;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002193 else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002194 return e_invalid_argument;
2195
2196 // may also change 'textmode'
2197 if (get_fileformat(curbuf) == EOL_DOS)
2198 curbuf->b_p_tx = TRUE;
2199 else
2200 curbuf->b_p_tx = FALSE;
2201 redraw_titles();
2202 // update flag in swap file
2203 ml_setflags(curbuf);
2204 // Redraw needed when switching to/from "mac": a CR in the text
2205 // will be displayed differently.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002206 if (get_fileformat(curbuf) == EOL_MAC || *args->os_oldval.string == 'm')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002207 redraw_curbuf_later(UPD_NOT_VALID);
2208
2209 return NULL;
2210}
2211
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002212 int
2213expand_set_fileformat(optexpand_T *args, int *numMatches, char_u ***matches)
2214{
2215 return expand_set_opt_string(
2216 args,
2217 p_ff_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002218 ARRAY_LENGTH(p_ff_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002219 numMatches,
2220 matches);
2221}
2222
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002223/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002224 * Function given to ExpandGeneric() to obtain the possible arguments of the
2225 * fileformat options.
2226 */
2227 char_u *
2228get_fileformat_name(expand_T *xp UNUSED, int idx)
2229{
2230 if (idx >= (int)ARRAY_LENGTH(p_ff_values))
2231 return NULL;
2232
2233 return (char_u*)p_ff_values[idx];
2234}
2235
2236/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002237 * The 'fileformats' option is changed.
2238 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002239 char *
2240did_set_fileformats(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002241{
2242 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
2243 return e_invalid_argument;
2244
2245 // also change 'textauto'
2246 if (*p_ffs == NUL)
2247 p_ta = FALSE;
2248 else
2249 p_ta = TRUE;
2250
2251 return NULL;
2252}
2253
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002254/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002255 * The 'filetype' or the 'syntax' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002256 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002257 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002258did_set_filetype_or_syntax(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002259{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002260 char_u **varp = (char_u **)args->os_varp;
2261
2262 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002263 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002264
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002265 args->os_value_changed = STRCMP(args->os_oldval.string, *varp) != 0;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002266
2267 // Since we check the value, there is no need to set P_INSECURE,
2268 // even when the value comes from a modeline.
2269 args->os_value_checked = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002270
2271 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002272}
2273
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002274#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002275/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002276 * The 'foldclose' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002277 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002278 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002279did_set_foldclose(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002280{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002281 return did_set_opt_strings(p_fcl, p_fcl_values, TRUE);
2282}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002283
2284 int
2285expand_set_foldclose(optexpand_T *args, int *numMatches, char_u ***matches)
2286{
2287 return expand_set_opt_string(
2288 args,
2289 p_fcl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002290 ARRAY_LENGTH(p_fcl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002291 numMatches,
2292 matches);
2293}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002294#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002295
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002296#if (defined(FEAT_EVAL) && defined(FEAT_FOLDING)) || defined(PROTO)
2297/*
2298 * The 'foldexpr' option is changed.
2299 */
2300 char *
2301did_set_foldexpr(optset_T *args)
2302{
2303 (void)did_set_optexpr(args);
2304 if (foldmethodIsExpr(curwin))
2305 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002306 return NULL;
2307}
2308#endif
2309
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002310#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002311/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002312 * The 'foldignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002313 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002314 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002315did_set_foldignore(optset_T *args UNUSED)
2316{
2317 if (foldmethodIsIndent(curwin))
2318 foldUpdateAll(curwin);
2319 return NULL;
2320}
2321
2322/*
2323 * The 'foldmarker' option is changed.
2324 */
2325 char *
2326did_set_foldmarker(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002327{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002328 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002329 char_u *p;
2330
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002331 p = vim_strchr(*varp, ',');
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002332 if (p == NULL)
2333 return e_comma_required;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002334 else if (p == *varp || p[1] == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002335 return e_invalid_argument;
2336 else if (foldmethodIsMarker(curwin))
2337 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002338
2339 return NULL;
2340}
2341
2342/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002343 * The 'foldmethod' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002344 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002345 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002346did_set_foldmethod(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002347{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002348 char_u **varp = (char_u **)args->os_varp;
2349
Milly142cad12024-10-22 22:11:51 +02002350 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK || **varp == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002351 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002352
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002353 foldUpdateAll(curwin);
2354 if (foldmethodIsDiff(curwin))
2355 newFoldLevel();
2356 return NULL;
2357}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002358
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002359 int
2360expand_set_foldmethod(optexpand_T *args, int *numMatches, char_u ***matches)
2361{
2362 return expand_set_opt_string(
2363 args,
2364 p_fdm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002365 ARRAY_LENGTH(p_fdm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002366 numMatches,
2367 matches);
2368}
2369
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002370/*
2371 * The 'foldopen' option is changed.
2372 */
2373 char *
2374did_set_foldopen(optset_T *args UNUSED)
2375{
2376 return did_set_opt_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
2377}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002378
2379 int
2380expand_set_foldopen(optexpand_T *args, int *numMatches, char_u ***matches)
2381{
2382 return expand_set_opt_string(
2383 args,
2384 p_fdo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002385 ARRAY_LENGTH(p_fdo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002386 numMatches,
2387 matches);
2388}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002389#endif
2390
2391/*
2392 * The 'formatoptions' option is changed.
2393 */
2394 char *
2395did_set_formatoptions(optset_T *args)
2396{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002397 char_u **varp = (char_u **)args->os_varp;
2398
Christian Brabandtb39b2402023-11-29 11:34:05 +01002399 return did_set_option_listflag(*varp, (char_u *)FO_ALL, args->os_errbuf,
2400 args->os_errbuflen);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002401}
2402
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002403 int
2404expand_set_formatoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2405{
2406 return expand_set_opt_listflag(args, (char_u*)FO_ALL, numMatches, matches);
2407}
2408
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002409#if defined(CURSOR_SHAPE) || defined(PROTO)
2410/*
2411 * The 'guicursor' option is changed.
2412 */
2413 char *
2414did_set_guicursor(optset_T *args UNUSED)
2415{
2416 return parse_shape_opt(SHAPE_CURSOR);
2417}
2418#endif
2419
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002420#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002421/*
2422 * The 'guifont' option is changed.
2423 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002424 char *
2425did_set_guifont(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002426{
2427 char_u *p;
2428 char *errmsg = NULL;
2429
2430 if (gui.in_use)
2431 {
2432 p = p_guifont;
2433# if defined(FEAT_GUI_GTK)
2434 // Put up a font dialog and let the user select a new value.
2435 // If this is cancelled go back to the old value but don't
2436 // give an error message.
2437 if (STRCMP(p, "*") == 0)
2438 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002439 p = gui_mch_font_dialog(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002440 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002441 p_guifont = (p != NULL) ? p : vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002442 }
2443# endif
2444 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
2445 {
2446# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
2447 if (STRCMP(p_guifont, "*") == 0)
2448 {
2449 // Dialog was cancelled: Keep the old value without giving
2450 // an error message.
2451 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002452 p_guifont = vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002453 }
2454 else
2455# endif
2456 errmsg = e_invalid_fonts;
2457 }
2458 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002459
2460 return errmsg;
2461}
2462
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002463/*
2464 * Expand the 'guifont' option. Only when GUI is being used. Each platform has
2465 * specific behaviors.
2466 */
2467 int
2468expand_set_guifont(optexpand_T *args, int *numMatches, char_u ***matches)
2469{
2470 if (!gui.in_use)
2471 return FAIL;
2472
2473# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
2474 char_u **varp = (char_u **)args->oe_varp;
2475 int wide = (varp == &p_guifontwide);
2476
2477 return expand_set_opt_callback(
2478 args, gui_mch_expand_font, &wide, numMatches, matches);
2479# else
2480 return FAIL;
2481# endif
2482}
2483
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002484# if defined(FEAT_XFONTSET) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002485/*
2486 * The 'guifontset' option is changed.
2487 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002488 char *
2489did_set_guifontset(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002490{
2491 char *errmsg = NULL;
2492
2493 if (STRCMP(p_guifontset, "*") == 0)
2494 errmsg = e_cant_select_fontset;
2495 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
2496 errmsg = e_invalid_fontset;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002497
2498 return errmsg;
2499}
2500# endif
2501
2502/*
2503 * The 'guifontwide' option is changed.
2504 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002505 char *
2506did_set_guifontwide(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002507{
2508 char *errmsg = NULL;
2509
2510 if (STRCMP(p_guifontwide, "*") == 0)
2511 errmsg = e_cant_select_wide_font;
2512 else if (gui_get_wide_font() == FAIL)
2513 errmsg = e_invalid_wide_font;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002514
2515 return errmsg;
2516}
2517#endif
2518
Erik S. V. Jansson8b1e7492024-02-24 14:26:52 +01002519#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002520/*
2521 * The 'guiligatures' option is changed.
2522 */
2523 char *
2524did_set_guiligatures(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002525{
2526 gui_set_ligatures();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002527 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002528}
2529#endif
2530
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002531#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002532/*
2533 * The 'guioptions' option is changed.
2534 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002535 char *
2536did_set_guioptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002537{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002538 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002539 char *errmsg;
2540
Christian Brabandtb39b2402023-11-29 11:34:05 +01002541 errmsg = did_set_option_listflag(*varp, (char_u *)GO_ALL, args->os_errbuf,
2542 args->os_errbuflen);
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002543 if (errmsg != NULL)
2544 return errmsg;
2545
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002546 gui_init_which_components(args->os_oldval.string);
2547 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002548}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002549
2550 int
2551expand_set_guioptions(optexpand_T *args, int *numMatches, char_u ***matches)
2552{
2553 return expand_set_opt_listflag(args, (char_u*)GO_ALL, numMatches, matches);
2554}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002555#endif
2556
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002557#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002558/*
2559 * The 'guitablabel' option is changed.
2560 */
2561 char *
2562did_set_guitablabel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002563{
2564 redraw_tabline = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002565 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002566}
2567#endif
2568
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002569/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002570 * The 'helpfile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002571 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002572 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002573did_set_helpfile(optset_T *args UNUSED)
2574{
2575 // May compute new values for $VIM and $VIMRUNTIME
2576 if (didset_vim)
2577 vim_unsetenv_ext((char_u *)"VIM");
2578 if (didset_vimruntime)
2579 vim_unsetenv_ext((char_u *)"VIMRUNTIME");
2580 return NULL;
2581}
2582
2583#if defined(FEAT_MULTI_LANG) || defined(PROTO)
2584/*
2585 * The 'helplang' option is changed.
2586 */
2587 char *
2588did_set_helplang(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002589{
2590 char *errmsg = NULL;
2591
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002592 // Check for "", "ab", "ab,cd", etc.
2593 for (char_u *s = p_hlg; *s != NUL; s += 3)
2594 {
2595 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
2596 {
2597 errmsg = e_invalid_argument;
2598 break;
2599 }
2600 if (s[2] == NUL)
2601 break;
2602 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002603
2604 return errmsg;
2605}
2606#endif
2607
2608/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002609 * The 'highlight' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002610 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002611 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002612did_set_highlight(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002613{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002614 if (highlight_changed() == FAIL)
2615 return e_invalid_argument; // invalid flags
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002616
2617 return NULL;
2618}
2619
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002620/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002621 * Expand 'highlight' option.
2622 */
2623 int
2624expand_set_highlight(optexpand_T *args, int *numMatches, char_u ***matches)
2625{
2626 char_u *p;
2627 expand_T *xp = args->oe_xp;
2628 static char_u hl_flags[HLF_COUNT] = HL_FLAGS;
Christian Brabandt3f168ec2023-10-02 23:21:11 +02002629 size_t i;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002630 int count = 0;
2631
2632 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2633 {
2634 // Right after a ':', meaning we just return all highlight names.
2635 return expand_set_opt_generic(
2636 args,
2637 get_highlight_name,
2638 numMatches,
2639 matches);
2640 }
2641
2642 if (*xp->xp_pattern == NUL)
2643 {
2644 // At beginning of a comma-separated list. Return the specific list of
2645 // supported occasions.
2646 *matches = ALLOC_MULT(char_u *, HLF_COUNT + 1);
2647 if (*matches == NULL)
2648 return FAIL;
2649
2650 // We still want to return the full option if it's requested.
2651 if (args->oe_include_orig_val)
2652 {
2653 p = vim_strsave(args->oe_opt_value);
2654 if (p == NULL)
2655 {
2656 VIM_CLEAR(*matches);
2657 return FAIL;
2658 }
2659 (*matches)[count++] = p;
2660 }
2661
2662 for (i = 0; i < HLF_COUNT; i++)
2663 {
2664 p = vim_strnsave(&hl_flags[i], 1);
2665 if (p == NULL)
2666 {
2667 if (count == 0)
2668 {
2669 VIM_CLEAR(*matches);
2670 return FAIL;
2671 }
2672 else
2673 break;
2674 }
2675 (*matches)[count++] = p;
2676 }
2677
2678 if (count == 0)
2679 {
2680 VIM_CLEAR(*matches);
2681 return FAIL;
2682 }
2683 *numMatches = count;
2684 return OK;
2685 }
2686
2687 // We are after the initial character (which indicates the occasion). We
2688 // already made sure we are not matching after a ':' above, so now we want
2689 // to match against display mode modifiers.
2690 // Since the xp_pattern starts from the beginning, we need to include it in
2691 // the returned match.
2692
2693 // Note: Keep this in sync with highlight_changed()
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002694 static char_u p_hl_mode_values[] =
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002695 {':', 'b', 'i', '-', 'n', 'r', 's', 'u', 'c', '2', 'd', '=', 't'};
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002696 size_t num_hl_modes = ARRAY_LENGTH(p_hl_mode_values);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002697
2698 *matches = ALLOC_MULT(char_u *, num_hl_modes);
2699 if (*matches == NULL)
2700 return FAIL;
2701
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002702 int pattern_len = xp->xp_pattern_len;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002703
2704 for (i = 0; i < num_hl_modes; i++)
2705 {
2706 // Don't allow duplicates as these are unique flags
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002707 char_u *dup = vim_strchr(xp->xp_pattern + 1, p_hl_mode_values[i]);
2708 if (dup != NULL && (int)(dup - xp->xp_pattern) < pattern_len)
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002709 continue;
2710
2711 // ':' only works by itself, not with other flags.
2712 if (pattern_len > 1 && p_hl_mode_values[i] == ':')
2713 continue;
2714
2715 p = vim_strnsave(xp->xp_pattern, pattern_len + 1);
2716 if (p == NULL)
2717 {
2718 if (i == 0)
2719 {
2720 VIM_CLEAR(*matches);
2721 return FAIL;
2722 }
2723 else
2724 break;
2725 }
2726 p[pattern_len] = p_hl_mode_values[i];
2727 p[pattern_len + 1] = NUL;
2728 (*matches)[count++] = p;
2729 }
2730 if (count == 0)
2731 {
2732 VIM_CLEAR(*matches);
2733 return FAIL;
2734 }
2735 *numMatches = count;
2736
2737 return OK;
2738}
2739
2740/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002741 * The 'titlestring' or the 'iconstring' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002742 */
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002743 static char *
2744parse_titleiconstring(optset_T *args UNUSED, int flagval UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002745{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002746#ifdef FEAT_STL_OPT
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002747 char_u **varp = (char_u **)args->os_varp;
2748
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002749 // NULL => statusline syntax
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002750 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002751 stl_syntax |= flagval;
2752 else
2753 stl_syntax &= ~flagval;
2754#endif
2755 did_set_title();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002756
2757 return NULL;
2758}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002759
2760/*
2761 * The 'iconstring' option is changed.
2762 */
2763 char *
2764did_set_iconstring(optset_T *args)
2765{
2766 int flagval = 0;
2767
2768#ifdef FEAT_STL_OPT
2769 flagval = STL_IN_ICON;
2770#endif
2771
2772 return parse_titleiconstring(args, flagval);
2773}
2774
2775#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
2776/*
2777 * The 'imactivatekey' option is changed.
2778 */
2779 char *
2780did_set_imactivatekey(optset_T *args UNUSED)
2781{
2782 if (!im_xim_isvalid_imactivate())
2783 return e_invalid_argument;
2784 return NULL;
2785}
2786#endif
2787
2788/*
Milly5e7a6a42024-10-22 22:27:19 +02002789 * The 'iskeyword' option is changed.
2790 */
2791 char *
2792did_set_iskeyword(optset_T *args)
2793{
2794 char_u **varp = (char_u **)args->os_varp;
2795
2796 if (varp == &p_isk) // only check for global-value
2797 {
2798 if (check_isopt(*varp) == FAIL)
2799 return e_invalid_argument;
2800 }
2801 else // fallthrough for local-value
2802 return did_set_isopt(args);
2803
2804 return NULL;
2805}
2806
2807/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002808 * The 'isident' or the 'iskeyword' or the 'isprint' or the 'isfname' option is
2809 * changed.
2810 */
2811 char *
2812did_set_isopt(optset_T *args)
2813{
Milly5e7a6a42024-10-22 22:27:19 +02002814 // 'isident', 'iskeyword', 'isprint' or 'isfname' option: refill g_chartab[]
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002815 // If the new option is invalid, use old value.
2816 // 'lisp' option: refill g_chartab[] for '-' char.
2817 if (init_chartab() == FAIL)
2818 {
2819 args->os_restore_chartab = TRUE;// need to restore the chartab.
2820 return e_invalid_argument; // error in value
2821 }
2822
2823 return NULL;
2824}
2825
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002826/*
2827 * The 'jumpoptions' option is changed.
2828 */
2829 char *
Yegappan Lakshmanan1926ae42023-09-21 16:36:28 +02002830did_set_jumpoptions(optset_T *args UNUSED)
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002831{
2832 if (opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE) != OK)
2833 return e_invalid_argument;
2834
2835 return NULL;
2836}
2837
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002838 int
2839expand_set_jumpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2840{
2841 return expand_set_opt_string(
2842 args,
2843 p_jop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002844 ARRAY_LENGTH(p_jop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002845 numMatches,
2846 matches);
2847}
2848
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002849#if defined(FEAT_KEYMAP) || defined(PROTO)
2850/*
2851 * The 'keymap' option is changed.
2852 */
2853 char *
2854did_set_keymap(optset_T *args)
2855{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002856 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002857 char *errmsg = NULL;
2858
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002859 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002860 errmsg = e_invalid_argument;
2861 else
2862 {
2863 int secure_save = secure;
2864
2865 // Reset the secure flag, since the value of 'keymap' has
2866 // been checked to be safe.
2867 secure = 0;
2868
2869 // load or unload key mapping tables
2870 errmsg = keymap_init();
2871
2872 secure = secure_save;
2873
2874 // Since we check the value, there is no need to set P_INSECURE,
2875 // even when the value comes from a modeline.
2876 args->os_value_checked = TRUE;
2877 }
2878
2879 if (errmsg == NULL)
2880 {
2881 if (*curbuf->b_p_keymap != NUL)
2882 {
2883 // Installed a new keymap, switch on using it.
2884 curbuf->b_p_iminsert = B_IMODE_LMAP;
2885 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
2886 curbuf->b_p_imsearch = B_IMODE_LMAP;
2887 }
2888 else
2889 {
2890 // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
2891 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
2892 curbuf->b_p_iminsert = B_IMODE_NONE;
2893 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
2894 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
2895 }
2896 if ((args->os_flags & OPT_LOCAL) == 0)
2897 {
2898 set_iminsert_global();
2899 set_imsearch_global();
2900 }
2901 status_redraw_curbuf();
2902 }
2903
2904 return errmsg;
2905}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002906#endif
2907
2908/*
2909 * The 'keymodel' option is changed.
2910 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002911 char *
2912did_set_keymodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002913{
2914 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
2915 return e_invalid_argument;
2916
2917 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
2918 km_startsel = (vim_strchr(p_km, 'a') != NULL);
2919 return NULL;
2920}
2921
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002922 int
2923expand_set_keymodel(optexpand_T *args, int *numMatches, char_u ***matches)
2924{
2925 return expand_set_opt_string(
2926 args,
2927 p_km_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002928 ARRAY_LENGTH(p_km_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002929 numMatches,
2930 matches);
2931}
2932
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002933/*
2934 * The 'keyprotocol' option is changed.
2935 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002936 char *
2937did_set_keyprotocol(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002938{
Gregory Anders3695d0e2023-09-29 20:17:20 +02002939 char_u *term = T_NAME;
2940 keyprot_T kpc = match_keyprotocol(term);
2941 if (kpc == KEYPROTOCOL_FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002942 return e_invalid_argument;
2943
Gregory Anders3695d0e2023-09-29 20:17:20 +02002944 apply_keyprotocol(term, kpc);
2945
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002946 return NULL;
2947}
2948
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002949 int
2950expand_set_keyprotocol(optexpand_T *args, int *numMatches, char_u ***matches)
2951{
2952 expand_T *xp = args->oe_xp;
2953 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2954 {
2955 // 'keyprotocol' only has well-defined terms for completion for the
2956 // protocol part after the colon.
2957 return expand_set_opt_string(
2958 args,
2959 p_kpc_protocol_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002960 ARRAY_LENGTH(p_kpc_protocol_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002961 numMatches,
2962 matches);
2963 }
2964 // Use expand_set_opt_string instead of returning FAIL so that we can
2965 // include the original value if args->oe_include_orig_val is set.
2966 static char *(empty[]) = {NULL};
2967 return expand_set_opt_string(args, empty, 0, numMatches, matches);
2968}
2969
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002970/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002971 * The 'lispoptions' option is changed.
2972 */
2973 char *
2974did_set_lispoptions(optset_T *args)
2975{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002976 char_u **varp = (char_u **)args->os_varp;
2977
2978 if (**varp != NUL
2979 && STRCMP(*varp, "expr:0") != 0
2980 && STRCMP(*varp, "expr:1") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002981 return e_invalid_argument;
2982
2983 return NULL;
2984}
2985
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002986 int
2987expand_set_lispoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2988{
2989 static char *(p_lop_values[]) = {"expr:0", "expr:1", NULL};
2990 return expand_set_opt_string(
2991 args,
2992 p_lop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002993 ARRAY_LENGTH(p_lop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002994 numMatches,
2995 matches);
2996}
2997
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002998/*
2999 * The 'matchpairs' option is changed.
3000 */
3001 char *
3002did_set_matchpairs(optset_T *args)
3003{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003004 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003005 char_u *p;
3006
3007 if (has_mbyte)
3008 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003009 for (p = *varp; *p != NUL; ++p)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003010 {
3011 int x2 = -1;
3012 int x3 = -1;
3013
3014 p += mb_ptr2len(p);
3015 if (*p != NUL)
3016 x2 = *p++;
3017 if (*p != NUL)
3018 {
3019 x3 = mb_ptr2char(p);
3020 p += mb_ptr2len(p);
3021 }
3022 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
3023 return e_invalid_argument;
3024 if (*p == NUL)
3025 break;
3026 }
3027 }
3028 else
3029 {
3030 // Check for "x:y,x:y"
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003031 for (p = *varp; *p != NUL; p += 4)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003032 {
3033 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
3034 return e_invalid_argument;
3035 if (p[3] == NUL)
3036 break;
3037 }
3038 }
3039
3040 return NULL;
3041}
3042
3043#if defined(FEAT_SPELL) || defined(PROTO)
3044/*
3045 * The 'mkspellmem' option is changed.
3046 */
3047 char *
3048did_set_mkspellmem(optset_T *args UNUSED)
3049{
3050 if (spell_check_msm() != OK)
3051 return e_invalid_argument;
3052
3053 return NULL;
3054}
3055#endif
3056
3057/*
3058 * The 'mouse' option is changed.
3059 */
3060 char *
3061did_set_mouse(optset_T *args)
3062{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003063 char_u **varp = (char_u **)args->os_varp;
3064
Christian Brabandtb39b2402023-11-29 11:34:05 +01003065 return did_set_option_listflag(*varp, (char_u *)MOUSE_ALL, args->os_errbuf,
3066 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003067}
3068
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003069 int
3070expand_set_mouse(optexpand_T *args, int *numMatches, char_u ***matches)
3071{
3072 return expand_set_opt_listflag(args, (char_u*)MOUSE_ALL, numMatches, matches);
3073}
3074
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003075/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003076 * The 'mousemodel' option is changed.
3077 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003078 char *
3079did_set_mousemodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003080{
3081 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
3082 return e_invalid_argument;
3083#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
Ken Takata18d0d292023-12-19 20:12:29 +01003084 else if (*p_mousem != *args->os_oldval.string)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003085 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
3086 // to create or delete the popup menus.
3087 gui_motif_update_mousemodel(root_menu);
3088#endif
3089
3090 return NULL;
3091}
3092
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003093 int
3094expand_set_mousemodel(optexpand_T *args, int *numMatches, char_u ***matches)
3095{
3096 return expand_set_opt_string(
3097 args,
3098 p_mousem_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003099 ARRAY_LENGTH(p_mousem_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003100 numMatches,
3101 matches);
3102}
3103
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003104#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3105 char *
3106did_set_mouseshape(optset_T *args UNUSED)
3107{
3108 char *errmsg = NULL;
3109
3110 errmsg = parse_shape_opt(SHAPE_MOUSE);
3111 update_mouseshape(-1);
3112
3113 return errmsg;
3114}
3115#endif
3116
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003117/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003118 * The 'nrformats' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003119 */
3120 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003121did_set_nrformats(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003122{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003123 char_u **varp = (char_u **)args->os_varp;
3124
3125 return did_set_opt_strings(*varp, p_nf_values, TRUE);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003126}
3127
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003128 int
3129expand_set_nrformats(optexpand_T *args, int *numMatches, char_u ***matches)
3130{
3131 return expand_set_opt_string(
3132 args,
3133 p_nf_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003134 ARRAY_LENGTH(p_nf_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003135 numMatches,
3136 matches);
3137}
3138
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003139#if defined(FEAT_EVAL) || defined(PROTO)
3140/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003141 * One of the '*expr' options is changed: 'balloonexpr', 'diffexpr',
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003142 * 'foldexpr', 'foldtext', 'formatexpr', 'includeexpr', 'indentexpr',
3143 * 'patchexpr', 'printexpr' and 'charconvert'.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003144 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003145 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003146did_set_optexpr(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003147{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003148 char_u **varp = (char_u **)args->os_varp;
3149
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003150 // If the option value starts with <SID> or s:, then replace that with
3151 // the script identifier.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003152 char_u *name = get_scriptlocal_funcname(*varp);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003153 if (name != NULL)
3154 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003155 free_string_option(*varp);
3156 *varp = name;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003157 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003158
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003159 return NULL;
3160}
3161#endif
3162
3163/*
3164 * The 'pastetoggle' option is changed.
3165 */
3166 char *
3167did_set_pastetoggle(optset_T *args UNUSED)
3168{
3169 char_u *p;
3170
3171 // translate key codes like in a mapping
3172 if (*p_pt)
3173 {
zeertzjq7e0bae02023-08-11 23:15:38 +02003174 (void)replace_termcodes(p_pt, &p, 0,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003175 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
3176 if (p != NULL)
3177 {
3178 free_string_option(p_pt);
3179 p_pt = p;
3180 }
3181 }
3182
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003183 return NULL;
3184}
3185
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003186#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3187/*
3188 * The 'previewpopup' option is changed.
3189 */
3190 char *
3191did_set_previewpopup(optset_T *args UNUSED)
3192{
3193 if (parse_previewpopup(NULL) == FAIL)
3194 return e_invalid_argument;
3195
3196 return NULL;
3197}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003198
3199 int
3200expand_set_popupoption(optexpand_T *args, int *numMatches, char_u ***matches)
3201{
3202 expand_T *xp = args->oe_xp;
3203
3204 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3205 {
3206 // Within "highlight:"/"border:"/"align:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003207 int border_len = (int)STRLEN("border:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003208 if (xp->xp_pattern - args->oe_set_arg >= border_len &&
3209 STRNCMP(xp->xp_pattern - border_len, "border:", border_len) == 0)
3210 {
3211 return expand_set_opt_string(
3212 args,
3213 p_popup_option_border_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003214 ARRAY_LENGTH(p_popup_option_border_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003215 numMatches,
3216 matches);
3217 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003218 int align_len = (int)STRLEN("align:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003219 if (xp->xp_pattern - args->oe_set_arg >= align_len &&
3220 STRNCMP(xp->xp_pattern - align_len, "align:", align_len) == 0)
3221 {
3222 return expand_set_opt_string(
3223 args,
3224 p_popup_option_align_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003225 ARRAY_LENGTH(p_popup_option_align_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003226 numMatches,
3227 matches);
3228 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003229 int highlight_len = (int)STRLEN("highlight:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003230 if (xp->xp_pattern - args->oe_set_arg >= highlight_len &&
3231 STRNCMP(xp->xp_pattern - highlight_len, "highlight:", highlight_len) == 0)
3232 {
3233 // Return the list of all highlight names
3234 return expand_set_opt_generic(
3235 args,
3236 get_highlight_name,
3237 numMatches,
3238 matches);
3239 }
3240 return FAIL;
3241 }
3242
3243 return expand_set_opt_string(
3244 args,
3245 p_popup_option_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003246 ARRAY_LENGTH(p_popup_option_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003247 numMatches,
3248 matches);
3249}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003250#endif
3251
3252#if defined(FEAT_POSTSCRIPT) || defined(PROTO)
3253/*
3254 * The 'printencoding' option is changed.
3255 */
3256 char *
3257did_set_printencoding(optset_T *args UNUSED)
3258{
3259 char_u *s, *p;
3260
3261 // Canonize 'printencoding' if VIM standard one
3262 p = enc_canonize(p_penc);
3263 if (p != NULL)
3264 {
3265 vim_free(p_penc);
3266 p_penc = p;
3267 }
3268 else
3269 {
3270 // Ensure lower case and '-' for '_'
3271 for (s = p_penc; *s != NUL; s++)
3272 {
3273 if (*s == '_')
3274 *s = '-';
3275 else
3276 *s = TOLOWER_ASC(*s);
3277 }
3278 }
3279
3280 return NULL;
3281}
3282#endif
3283
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003284#if defined(FEAT_PRINTER) || defined(PROTO)
3285
3286 static char_u *
3287get_printoptions_names(expand_T *xp UNUSED, int idx)
3288{
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003289 if (idx >= (int)ARRAY_LENGTH(printer_opts))
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003290 return NULL;
3291 return (char_u*)printer_opts[idx].name;
3292}
3293
3294/*
3295 * Expand 'printoptions' option
3296 */
3297 int
3298expand_set_printoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3299{
3300 return expand_set_opt_generic(
3301 args,
3302 get_printoptions_names,
3303 numMatches,
3304 matches);
3305}
3306#endif
3307
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003308#if defined(FEAT_STL_OPT) || defined(PROTO)
3309/*
3310 * The 'statusline' or the 'tabline' or the 'rulerformat' option is changed.
3311 * "rulerformat" is TRUE if the 'rulerformat' option is changed.
3312 */
3313 static char *
3314parse_statustabline_rulerformat(optset_T *args, int rulerformat)
3315{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003316 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003317 char_u *s;
3318 char *errmsg = NULL;
3319 int wid;
3320
3321 if (rulerformat) // reset ru_wid first
3322 ru_wid = 0;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003323 s = *varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003324 if (rulerformat && *s == '%')
3325 {
3326 // set ru_wid if 'ruf' starts with "%99("
3327 if (*++s == '-') // ignore a '-'
3328 s++;
3329 wid = getdigits(&s);
3330 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
3331 ru_wid = wid;
3332 else
3333 errmsg = check_stl_option(p_ruf);
3334 }
3335 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
3336 else if (rulerformat || s[0] != '%' || s[1] != '!')
3337 errmsg = check_stl_option(s);
3338 if (rulerformat && errmsg == NULL)
3339 comp_col();
3340
3341 return errmsg;
3342}
3343#endif
3344
3345#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
3346/*
3347 * The 'renderoptions' option is changed.
3348 */
3349 char *
3350did_set_renderoptions(optset_T *args UNUSED)
3351{
3352 if (!gui_mch_set_rendering_options(p_rop))
3353 return e_invalid_argument;
3354
3355 return NULL;
3356}
3357#endif
3358
3359#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3360/*
3361 * The 'rightleftcmd' option is changed.
3362 */
3363 char *
3364did_set_rightleftcmd(optset_T *args)
3365{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003366 char_u **varp = (char_u **)args->os_varp;
3367
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003368 // Currently only "search" is a supported value.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003369 if (**varp != NUL && STRCMP(*varp, "search") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003370 return e_invalid_argument;
3371
3372 return NULL;
3373}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003374
3375 int
3376expand_set_rightleftcmd(optexpand_T *args, int *numMatches, char_u ***matches)
3377{
3378 static char *(p_rlc_values[]) = {"search", NULL};
3379 return expand_set_opt_string(
3380 args,
3381 p_rlc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003382 ARRAY_LENGTH(p_rlc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003383 numMatches,
3384 matches);
3385}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003386#endif
3387
3388#if defined(FEAT_STL_OPT) || defined(PROTO)
3389/*
3390 * The 'rulerformat' option is changed.
3391 */
3392 char *
3393did_set_rulerformat(optset_T *args)
3394{
3395 return parse_statustabline_rulerformat(args, TRUE);
3396}
3397#endif
3398
3399/*
3400 * The 'scrollopt' option is changed.
3401 */
3402 char *
3403did_set_scrollopt(optset_T *args UNUSED)
3404{
3405 return did_set_opt_strings(p_sbo, p_scbopt_values, TRUE);
3406}
3407
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003408 int
3409expand_set_scrollopt(optexpand_T *args, int *numMatches, char_u ***matches)
3410{
3411 return expand_set_opt_string(
3412 args,
3413 p_scbopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003414 ARRAY_LENGTH(p_scbopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003415 numMatches,
3416 matches);
3417}
3418
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003419/*
3420 * The 'selection' option is changed.
3421 */
3422 char *
3423did_set_selection(optset_T *args UNUSED)
3424{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003425 if (*p_sel == NUL || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003426 return e_invalid_argument;
3427
3428 return NULL;
3429}
3430
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003431 int
3432expand_set_selection(optexpand_T *args, int *numMatches, char_u ***matches)
3433{
3434 return expand_set_opt_string(
3435 args,
3436 p_sel_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003437 ARRAY_LENGTH(p_sel_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003438 numMatches,
3439 matches);
3440}
3441
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003442/*
3443 * The 'selectmode' option is changed.
3444 */
3445 char *
3446did_set_selectmode(optset_T *args UNUSED)
3447{
3448 return did_set_opt_strings(p_slm, p_slm_values, TRUE);
3449}
3450
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003451 int
3452expand_set_selectmode(optexpand_T *args, int *numMatches, char_u ***matches)
3453{
3454 return expand_set_opt_string(
3455 args,
3456 p_slm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003457 ARRAY_LENGTH(p_slm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003458 numMatches,
3459 matches);
3460}
3461
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003462#if defined(FEAT_SESSION) || defined(PROTO)
3463/*
3464 * The 'sessionoptions' option is changed.
3465 */
3466 char *
3467did_set_sessionoptions(optset_T *args)
3468{
3469 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
3470 return e_invalid_argument;
3471 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
3472 {
3473 // Don't allow both "sesdir" and "curdir".
3474 (void)opt_strings_flags(args->os_oldval.string, p_ssop_values,
3475 &ssop_flags, TRUE);
3476 return e_invalid_argument;
3477 }
3478
3479 return NULL;
3480}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003481
3482 int
3483expand_set_sessionoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3484{
3485 return expand_set_opt_string(
3486 args,
3487 p_ssop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003488 ARRAY_LENGTH(p_ssop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003489 numMatches,
3490 matches);
3491}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003492#endif
3493
3494/*
3495 * The 'shortmess' option is changed.
3496 */
3497 char *
3498did_set_shortmess(optset_T *args)
3499{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003500 char_u **varp = (char_u **)args->os_varp;
3501
Christian Brabandtb39b2402023-11-29 11:34:05 +01003502 return did_set_option_listflag(*varp, (char_u *)SHM_ALL, args->os_errbuf,
3503 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003504}
3505
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003506 int
3507expand_set_shortmess(optexpand_T *args, int *numMatches, char_u ***matches)
3508{
3509 return expand_set_opt_listflag(args, (char_u*)SHM_ALL, numMatches, matches);
3510}
3511
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003512#if defined(FEAT_LINEBREAK) || defined(PROTO)
3513/*
3514 * The 'showbreak' option is changed.
3515 */
3516 char *
3517did_set_showbreak(optset_T *args)
3518{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003519 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003520 char_u *s;
3521
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003522 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003523 {
3524 if (ptr2cells(s) != 1)
3525 return e_showbreak_contains_unprintable_or_wide_character;
3526 MB_PTR_ADV(s);
3527 }
3528
3529 return NULL;
3530}
3531#endif
3532
3533/*
3534 * The 'showcmdloc' option is changed.
3535 */
3536 char *
3537did_set_showcmdloc(optset_T *args UNUSED)
3538{
zeertzjqc27fcf42024-03-01 23:01:43 +01003539 char *errmsg = did_set_opt_strings(p_sloc, p_sloc_values, FALSE);
3540
3541 if (errmsg == NULL)
3542 comp_col();
3543
3544 return errmsg;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003545}
3546
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003547 int
3548expand_set_showcmdloc(optexpand_T *args, int *numMatches, char_u ***matches)
3549{
3550 return expand_set_opt_string(
3551 args,
3552 p_sloc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003553 ARRAY_LENGTH(p_sloc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003554 numMatches,
3555 matches);
3556}
3557
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003558#if defined(FEAT_SIGNS) || defined(PROTO)
3559/*
3560 * The 'signcolumn' option is changed.
3561 */
3562 char *
3563did_set_signcolumn(optset_T *args)
3564{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003565 char_u **varp = (char_u **)args->os_varp;
3566
3567 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003568 return e_invalid_argument;
3569 // When changing the 'signcolumn' to or from 'number', recompute the
3570 // width of the number column if 'number' or 'relativenumber' is set.
3571 if (((*args->os_oldval.string == 'n' && args->os_oldval.string[1] == 'u')
3572 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
3573 && (curwin->w_p_nu || curwin->w_p_rnu))
3574 curwin->w_nrwidth_line_count = 0;
3575
3576 return NULL;
3577}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003578
3579 int
3580expand_set_signcolumn(optexpand_T *args, int *numMatches, char_u ***matches)
3581{
3582 return expand_set_opt_string(
3583 args,
3584 p_scl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003585 ARRAY_LENGTH(p_scl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003586 numMatches,
3587 matches);
3588}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003589#endif
3590
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003591#if defined(FEAT_SPELL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003592/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003593 * The 'spellcapcheck' option is changed.
3594 */
3595 char *
3596did_set_spellcapcheck(optset_T *args UNUSED)
3597{
3598 // compile the regexp program.
3599 return compile_cap_prog(curwin->w_s);
3600}
3601
3602/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003603 * The 'spellfile' option is changed.
3604 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003605 char *
3606did_set_spellfile(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003607{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003608 char_u **varp = (char_u **)args->os_varp;
3609
3610 if (!valid_spellfile(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003611 return e_invalid_argument;
3612
3613 // If there is a window for this buffer in which 'spell' is set load the
3614 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003615 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003616}
3617
3618/*
3619 * The 'spell' option is changed.
3620 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003621 char *
3622did_set_spelllang(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003623{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003624 char_u **varp = (char_u **)args->os_varp;
3625
3626 if (!valid_spelllang(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003627 return e_invalid_argument;
3628
3629 // If there is a window for this buffer in which 'spell' is set load the
3630 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003631 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003632}
3633
3634/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003635 * The 'spelloptions' option is changed.
3636 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003637 char *
3638did_set_spelloptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003639{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003640 char_u **varp = (char_u **)args->os_varp;
3641
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003642 if (**varp != NUL && STRCMP(*varp, "camel") != 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003643 return e_invalid_argument;
3644
3645 return NULL;
3646}
3647
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003648 int
3649expand_set_spelloptions(optexpand_T *args, int *numMatches, char_u ***matches)
3650{
3651 static char *(p_spo_values[]) = {"camel", NULL};
3652 return expand_set_opt_string(
3653 args,
3654 p_spo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003655 ARRAY_LENGTH(p_spo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003656 numMatches,
3657 matches);
3658}
3659
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003660/*
3661 * The 'spellsuggest' option is changed.
3662 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003663 char *
3664did_set_spellsuggest(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003665{
3666 if (spell_check_sps() != OK)
3667 return e_invalid_argument;
3668
3669 return NULL;
3670}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003671
3672 int
3673expand_set_spellsuggest(optexpand_T *args, int *numMatches, char_u ***matches)
3674{
3675 return expand_set_opt_string(
3676 args,
3677 p_sps_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003678 ARRAY_LENGTH(p_sps_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003679 numMatches,
3680 matches);
3681}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003682#endif
3683
3684/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003685 * The 'splitkeep' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003686 */
3687 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003688did_set_splitkeep(optset_T *args UNUSED)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003689{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003690 return did_set_opt_strings(p_spk, p_spk_values, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003691}
3692
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003693 int
3694expand_set_splitkeep(optexpand_T *args, int *numMatches, char_u ***matches)
3695{
3696 return expand_set_opt_string(
3697 args,
3698 p_spk_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003699 ARRAY_LENGTH(p_spk_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003700 numMatches,
3701 matches);
3702}
3703
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00003704#if defined(FEAT_STL_OPT) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003705/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003706 * The 'statusline' option is changed.
3707 */
3708 char *
3709did_set_statusline(optset_T *args)
3710{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003711 return parse_statustabline_rulerformat(args, FALSE);
3712}
3713#endif
3714
3715/*
3716 * The 'swapsync' option is changed.
3717 */
3718 char *
3719did_set_swapsync(optset_T *args UNUSED)
3720{
3721 return did_set_opt_strings(p_sws, p_sws_values, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003722}
3723
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003724 int
3725expand_set_swapsync(optexpand_T *args, int *numMatches, char_u ***matches)
3726{
3727 return expand_set_opt_string(
3728 args,
3729 p_sws_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003730 ARRAY_LENGTH(p_sws_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003731 numMatches,
3732 matches);
3733}
3734
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003735/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003736 * The 'switchbuf' option is changed.
3737 */
3738 char *
3739did_set_switchbuf(optset_T *args UNUSED)
3740{
3741 return did_set_opt_flags(p_swb, p_swb_values, &swb_flags, TRUE);
3742}
3743
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003744 int
3745expand_set_switchbuf(optexpand_T *args, int *numMatches, char_u ***matches)
3746{
3747 return expand_set_opt_string(
3748 args,
3749 p_swb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003750 ARRAY_LENGTH(p_swb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003751 numMatches,
3752 matches);
3753}
3754
LemonBoy5247b0b2024-07-12 19:30:58 +02003755/*
3756 * The 'tabclose' option is changed.
3757 */
3758 char *
3759did_set_tabclose(optset_T *args UNUSED)
3760{
3761 return did_set_opt_flags(p_tcl, p_tcl_values, &tcl_flags, TRUE);
3762}
3763
3764 int
3765expand_set_tabclose(optexpand_T *args, int *numMatches, char_u ***matches)
3766{
3767 return expand_set_opt_string(
3768 args,
3769 p_tcl_values,
3770 ARRAY_LENGTH(p_tcl_values) - 1,
3771 numMatches,
3772 matches);
3773}
3774
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003775#if defined(FEAT_STL_OPT) || defined(PROTO)
3776/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003777 * The 'tabline' option is changed.
3778 */
3779 char *
3780did_set_tabline(optset_T *args)
3781{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003782 return parse_statustabline_rulerformat(args, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003783}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003784#endif
3785
3786/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003787 * The 'tagcase' option is changed.
3788 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003789 char *
3790did_set_tagcase(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003791{
3792 unsigned int *flags;
3793 char_u *p;
3794
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003795 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003796 {
3797 p = curbuf->b_p_tc;
3798 flags = &curbuf->b_tc_flags;
3799 }
3800 else
3801 {
3802 p = p_tc;
3803 flags = &tc_flags;
3804 }
3805
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003806 if ((args->os_flags & OPT_LOCAL) && *p == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003807 // make the local value empty: use the global value
3808 *flags = 0;
3809 else if (*p == NUL
3810 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
3811 return e_invalid_argument;
3812
3813 return NULL;
3814}
3815
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003816 int
3817expand_set_tagcase(optexpand_T *args, int *numMatches, char_u ***matches)
3818{
3819 return expand_set_opt_string(
3820 args,
3821 p_tc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003822 ARRAY_LENGTH(p_tc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003823 numMatches,
3824 matches);
3825}
3826
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003827/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003828 * The 'term' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003829 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003830 char *
3831did_set_term(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003832{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003833 if (T_NAME[0] == NUL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003834 return e_cannot_set_term_to_empty_string;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003835#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003836 if (gui.in_use)
3837 return e_cannot_change_term_in_GUI;
3838 if (term_is_gui(T_NAME))
3839 return e_use_gui_to_start_GUI;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003840#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003841 if (set_termname(T_NAME) == FAIL)
3842 return e_not_found_in_termcap;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003843
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003844 // Screen colors may have changed.
3845 redraw_later_clear();
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003846
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003847 return NULL;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003848}
3849
3850/*
3851 * Some terminal option (t_xxx) is changed
3852 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003853 char *
3854did_set_term_option(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003855{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003856 char_u **varp = (char_u **)args->os_varp;
3857
3858 if (!full_screen)
3859 return NULL;
3860
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003861 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
3862 if (varp == &T_CCO)
3863 {
3864 int colors = atoi((char *)T_CCO);
3865
3866 // Only reinitialize colors if t_Co value has really changed to
3867 // avoid expensive reload of colorscheme if t_Co is set to the
3868 // same value multiple times.
3869 if (colors != t_colors)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003870 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003871 t_colors = colors;
3872 if (t_colors <= 1)
3873 {
3874 vim_free(T_CCO);
3875 T_CCO = empty_option;
3876 }
3877#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3878 if (is_term_win32())
3879 {
3880 swap_tcap();
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003881 args->os_did_swaptcap = TRUE;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003882 }
3883#endif
3884 // We now have a different color setup, initialize it again.
3885 init_highlight(TRUE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003886 }
3887 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003888 ttest(FALSE);
3889 if (varp == &T_ME)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003890 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003891 out_str(T_ME);
3892 redraw_later(UPD_CLEAR);
3893#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3894 // Since t_me has been set, this probably means that the user
3895 // wants to use this as default colors. Need to reset default
3896 // background/foreground colors.
3897# ifdef VIMDLL
3898 if (!gui.in_use && !gui.starting)
3899# endif
3900 mch_set_normal_colors();
3901#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003902 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003903 if (varp == &T_BE && termcap_active)
3904 {
3905 MAY_WANT_TO_LOG_THIS;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003906
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003907 if (*T_BE == NUL)
3908 // When clearing t_BE we assume the user no longer wants
3909 // bracketed paste, thus disable it by writing t_BD.
3910 out_str(T_BD);
3911 else
3912 out_str(T_BE);
3913 }
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003914
3915 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003916}
3917
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003918#if defined(FEAT_TERMINAL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003919/*
3920 * The 'termwinkey' option is changed.
3921 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003922 char *
Milly94606f72024-10-22 22:07:52 +02003923did_set_termwinkey(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003924{
Milly94606f72024-10-22 22:07:52 +02003925 char_u **varp = (char_u **)args->os_varp;
3926
3927 if ((*varp)[0] != NUL && string_to_key(*varp, TRUE) == 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003928 return e_invalid_argument;
3929
3930 return NULL;
3931}
3932
3933/*
3934 * The 'termwinsize' option is changed.
3935 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003936 char *
Milly8be10aa2024-10-22 22:01:46 +02003937did_set_termwinsize(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003938{
Milly8be10aa2024-10-22 22:01:46 +02003939 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003940 char_u *p;
3941
Milly8be10aa2024-10-22 22:01:46 +02003942 if ((*varp)[0] == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003943 return NULL;
3944
Milly8be10aa2024-10-22 22:01:46 +02003945 p = skipdigits(*varp);
3946 if (p == *varp || (*p != 'x' && *p != '*') || *skipdigits(p + 1) != NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003947 return e_invalid_argument;
3948
3949 return NULL;
3950}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003951
3952# if defined(MSWIN) || defined(PROTO)
3953/*
3954 * The 'termwintype' option is changed.
3955 */
3956 char *
3957did_set_termwintype(optset_T *args UNUSED)
3958{
3959 return did_set_opt_strings(p_twt, p_twt_values, FALSE);
3960}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003961
3962 int
3963expand_set_termwintype(optexpand_T *args, int *numMatches, char_u ***matches)
3964{
3965 return expand_set_opt_string(
3966 args,
3967 p_twt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003968 ARRAY_LENGTH(p_twt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003969 numMatches,
3970 matches);
3971}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003972# endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003973#endif
3974
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003975/*
3976 * The 'titlestring' option is changed.
3977 */
3978 char *
3979did_set_titlestring(optset_T *args)
3980{
3981 int flagval = 0;
3982
3983#ifdef FEAT_STL_OPT
3984 flagval = STL_IN_TITLE;
3985#endif
3986 return parse_titleiconstring(args, flagval);
3987}
3988
3989#if (defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)) || defined(PROTO)
3990/*
3991 * The 'toolbar' option is changed.
3992 */
3993 char *
3994did_set_toolbar(optset_T *args UNUSED)
3995{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003996 if (opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags,
3997 TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003998 return e_invalid_argument;
3999
4000 out_flush();
4001 gui_mch_show_toolbar((toolbar_flags &
4002 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4003 return NULL;
4004}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004005
4006 int
4007expand_set_toolbar(optexpand_T *args, int *numMatches, char_u ***matches)
4008{
4009 return expand_set_opt_string(
4010 args,
4011 p_toolbar_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004012 ARRAY_LENGTH(p_toolbar_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004013 numMatches,
4014 matches);
4015}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004016#endif
4017
4018#if (defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)) || defined(PROTO)
4019/*
4020 * The 'toolbariconsize' option is changed. GTK+ 2 only.
4021 */
4022 char *
4023did_set_toolbariconsize(optset_T *args UNUSED)
4024{
4025 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
4026 return e_invalid_argument;
4027
4028 out_flush();
4029 gui_mch_show_toolbar((toolbar_flags &
4030 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4031 return NULL;
4032}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004033
4034 int
4035expand_set_toolbariconsize(optexpand_T *args, int *numMatches, char_u ***matches)
4036{
4037 return expand_set_opt_string(
4038 args,
4039 p_tbis_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004040 ARRAY_LENGTH(p_tbis_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004041 numMatches,
4042 matches);
4043}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004044#endif
4045
4046#if defined(UNIX) || defined(VMS) || defined(PROTO)
4047/*
4048 * The 'ttymouse' option is changed.
4049 */
4050 char *
4051did_set_ttymouse(optset_T *args UNUSED)
4052{
4053 char *errmsg = NULL;
4054
4055 // Switch the mouse off before changing the escape sequences used for
4056 // that.
4057 mch_setmouse(FALSE);
4058 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
4059 errmsg = e_invalid_argument;
4060 else
4061 check_mouse_termcode();
4062 if (termcap_active)
4063 setmouse(); // may switch it on again
4064
4065 return errmsg;
4066}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004067
4068 int
4069expand_set_ttymouse(optexpand_T *args, int *numMatches, char_u ***matches)
4070{
4071 return expand_set_opt_string(
4072 args,
4073 p_ttym_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004074 ARRAY_LENGTH(p_ttym_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004075 numMatches,
4076 matches);
4077}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004078#endif
4079
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004080#if defined(FEAT_VARTABS) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004081/*
4082 * The 'varsofttabstop' option is changed.
4083 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004084 char *
4085did_set_varsofttabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004086{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004087 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004088 char_u *cp;
4089
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004090 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004091 VIM_CLEAR(curbuf->b_p_vsts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004092 else
4093 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004094 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004095 {
4096 if (vim_isdigit(*cp))
4097 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004098 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004099 continue;
4100 return e_invalid_argument;
4101 }
4102
4103 int *oldarray = curbuf->b_p_vsts_array;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004104 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004105 {
4106 if (oldarray)
4107 vim_free(oldarray);
4108 }
4109 else
4110 return e_invalid_argument;
4111 }
4112
4113 return NULL;
4114}
4115
4116/*
4117 * The 'vartabstop' option is changed.
4118 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004119 char *
4120did_set_vartabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004121{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004122 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004123 char_u *cp;
4124
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004125 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004126 VIM_CLEAR(curbuf->b_p_vts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004127 else
4128 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004129 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004130 {
4131 if (vim_isdigit(*cp))
4132 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004133 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004134 continue;
4135 return e_invalid_argument;
4136 }
4137
4138 int *oldarray = curbuf->b_p_vts_array;
4139
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004140 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004141 {
4142 vim_free(oldarray);
4143# ifdef FEAT_FOLDING
4144 if (foldmethodIsIndent(curwin))
4145 foldUpdateAll(curwin);
4146# endif
4147 }
4148 else
4149 return e_invalid_argument;
4150 }
4151
4152 return NULL;
4153}
4154#endif
4155
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004156/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004157 * The 'verbosefile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004158 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004159 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004160did_set_verbosefile(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004161{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004162 verbose_stop();
4163 if (*p_vfile != NUL && verbose_open() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004164 return e_invalid_argument;
4165
4166 return NULL;
4167}
4168
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004169#if defined(FEAT_SESSION) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004170/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004171 * The 'viewoptions' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004172 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004173 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004174did_set_viewoptions(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004175{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004176 return did_set_opt_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004177}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004178#endif
4179
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004180#if defined(FEAT_VIMINFO) || defined(PROTO)
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004181/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004182 * The 'viminfo' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004183 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004184 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004185did_set_viminfo(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004186{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004187 char_u *s;
4188 char *errmsg = NULL;
4189
4190 for (s = p_viminfo; *s;)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004191 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004192 // Check it's a valid character
4193 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
4194 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01004195 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004196 break;
4197 }
4198 if (*s == 'n') // name is always last one
4199 break;
4200 else if (*s == 'r') // skip until next ','
4201 {
4202 while (*++s && *s != ',')
4203 ;
4204 }
4205 else if (*s == '%')
4206 {
4207 // optional number
4208 while (vim_isdigit(*++s))
4209 ;
4210 }
4211 else if (*s == '!' || *s == 'h' || *s == 'c')
4212 ++s; // no extra chars
4213 else // must have a number
4214 {
4215 while (vim_isdigit(*++s))
4216 ;
4217
4218 if (!VIM_ISDIGIT(*(s - 1)))
4219 {
4220 if (args->os_errbuf != NULL)
4221 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01004222 vim_snprintf(args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004223 _(e_missing_number_after_angle_str_angle),
4224 transchar_byte(*(s - 1)));
4225 errmsg = args->os_errbuf;
4226 }
4227 else
4228 errmsg = "";
4229 break;
4230 }
4231 }
4232 if (*s == ',')
4233 ++s;
4234 else if (*s)
4235 {
4236 if (args->os_errbuf != NULL)
4237 errmsg = e_missing_comma;
4238 else
4239 errmsg = "";
4240 break;
4241 }
4242 }
4243 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
4244 errmsg = e_must_specify_a_value;
4245
4246 return errmsg;
4247}
4248#endif
4249
4250/*
4251 * The 'virtualedit' option is changed.
4252 */
4253 char *
4254did_set_virtualedit(optset_T *args)
4255{
4256 char_u *ve = p_ve;
4257 unsigned int *flags = &ve_flags;
4258
4259 if (args->os_flags & OPT_LOCAL)
4260 {
4261 ve = curwin->w_p_ve;
4262 flags = &curwin->w_ve_flags;
4263 }
4264
4265 if ((args->os_flags & OPT_LOCAL) && *ve == NUL)
4266 // make the local value empty: use the global value
4267 *flags = 0;
4268 else
4269 {
4270 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
4271 return e_invalid_argument;
4272 else if (STRCMP(ve, args->os_oldval.string) != 0)
4273 {
4274 // Recompute cursor position in case the new 've' setting
4275 // changes something.
4276 validate_virtcol();
4277 coladvance(curwin->w_virtcol);
4278 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004279 }
4280
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004281 return NULL;
4282}
4283
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004284 int
4285expand_set_virtualedit(optexpand_T *args, int *numMatches, char_u ***matches)
4286{
4287 return expand_set_opt_string(
4288 args,
4289 p_ve_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004290 ARRAY_LENGTH(p_ve_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004291 numMatches,
4292 matches);
4293}
4294
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004295/*
4296 * The 'whichwrap' option is changed.
4297 */
4298 char *
4299did_set_whichwrap(optset_T *args)
4300{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004301 char_u **varp = (char_u **)args->os_varp;
4302
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004303 // Add ',' to the list flags because 'whichwrap' is a flag
4304 // list that is comma-separated.
Christian Brabandtb39b2402023-11-29 11:34:05 +01004305 return did_set_option_listflag(*varp, (char_u *)(WW_ALL ","),
4306 args->os_errbuf, args->os_errbuflen);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004307}
4308
4309 int
4310expand_set_whichwrap(optexpand_T *args, int *numMatches, char_u ***matches)
4311{
4312 return expand_set_opt_listflag(args, (char_u*)WW_ALL, numMatches, matches);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004313}
4314
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004315/*
4316 * The 'wildmode' option is changed.
4317 */
4318 char *
4319did_set_wildmode(optset_T *args UNUSED)
4320{
4321 if (check_opt_wim() == FAIL)
4322 return e_invalid_argument;
4323 return NULL;
4324}
4325
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004326 int
4327expand_set_wildmode(optexpand_T *args, int *numMatches, char_u ***matches)
4328{
4329 return expand_set_opt_string(
4330 args,
4331 p_wim_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004332 ARRAY_LENGTH(p_wim_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004333 numMatches,
4334 matches);
4335}
4336
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004337/*
4338 * The 'wildoptions' option is changed.
4339 */
4340 char *
4341did_set_wildoptions(optset_T *args UNUSED)
4342{
4343 return did_set_opt_strings(p_wop, p_wop_values, TRUE);
4344}
4345
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004346 int
4347expand_set_wildoptions(optexpand_T *args, int *numMatches, char_u ***matches)
4348{
4349 return expand_set_opt_string(
4350 args,
4351 p_wop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004352 ARRAY_LENGTH(p_wop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004353 numMatches,
4354 matches);
4355}
4356
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004357#if defined(FEAT_WAK) || defined(PROTO)
4358/*
4359 * The 'winaltkeys' option is changed.
4360 */
4361 char *
4362did_set_winaltkeys(optset_T *args UNUSED)
4363{
4364 char *errmsg = NULL;
4365
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004366 if (*p_wak == NUL || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004367 errmsg = e_invalid_argument;
4368# ifdef FEAT_MENU
4369# if defined(FEAT_GUI_MOTIF)
4370 else if (gui.in_use)
4371 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4372# elif defined(FEAT_GUI_GTK)
4373 else if (gui.in_use)
4374 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4375# endif
4376# endif
4377 return errmsg;
4378}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004379
4380 int
4381expand_set_winaltkeys(optexpand_T *args, int *numMatches, char_u ***matches)
4382{
4383 return expand_set_opt_string(
4384 args,
4385 p_wak_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004386 ARRAY_LENGTH(p_wak_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004387 numMatches,
4388 matches);
4389}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004390#endif
4391
4392/*
4393 * The 'wincolor' option is changed.
4394 */
4395 char *
4396did_set_wincolor(optset_T *args UNUSED)
4397{
4398#ifdef FEAT_TERMINAL
4399 term_update_wincolor(curwin);
4400#endif
4401 return NULL;
4402}
4403
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004404 int
4405expand_set_wincolor(optexpand_T *args, int *numMatches, char_u ***matches)
4406{
4407 return expand_set_opt_generic(
4408 args,
4409 get_highlight_name,
4410 numMatches,
4411 matches);
4412}
4413
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004414#ifdef FEAT_SYN_HL
4415/*
4416 * When the 'syntax' option is set, load the syntax of that name.
4417 */
4418 static void
4419do_syntax_autocmd(int value_changed)
4420{
4421 static int syn_recursive = 0;
4422
4423 ++syn_recursive;
4424 // Only pass TRUE for "force" when the value changed or not used
4425 // recursively, to avoid endless recurrence.
4426 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
4427 value_changed || syn_recursive == 1, curbuf);
4428 curbuf->b_flags |= BF_SYN_SET;
4429 --syn_recursive;
4430}
4431#endif
4432
4433/*
4434 * When the 'filetype' option is set, trigger the FileType autocommand.
4435 */
4436 static void
4437do_filetype_autocmd(char_u **varp, int opt_flags, int value_changed)
4438{
4439 // Skip this when called from a modeline and the filetype was already set
4440 // to this value.
4441 if ((opt_flags & OPT_MODELINE) && !value_changed)
4442 return;
4443
4444 static int ft_recursive = 0;
4445 int secure_save = secure;
4446
4447 // Reset the secure flag, since the value of 'filetype' has
4448 // been checked to be safe.
4449 secure = 0;
4450
4451 ++ft_recursive;
zeertzjq5bf6c212024-03-31 18:41:27 +02004452 curbuf->b_did_filetype = TRUE;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004453 // Only pass TRUE for "force" when the value changed or not
4454 // used recursively, to avoid endless recurrence.
4455 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
4456 value_changed || ft_recursive == 1, curbuf);
4457 --ft_recursive;
4458 // Just in case the old "curbuf" is now invalid.
4459 if (varp != &(curbuf->b_p_ft))
4460 varp = NULL;
4461
4462 secure = secure_save;
4463}
4464
4465#ifdef FEAT_SPELL
4466/*
4467 * When the 'spelllang' option is set, source the spell/LANG.vim file in
4468 * 'runtimepath'.
4469 */
4470 static void
4471do_spelllang_source(void)
4472{
4473 char_u fname[200];
4474 char_u *p;
4475 char_u *q = curwin->w_s->b_p_spl;
4476
4477 // Skip the first name if it is "cjk".
4478 if (STRNCMP(q, "cjk,", 4) == 0)
4479 q += 4;
4480
4481 // They could set 'spellcapcheck' depending on the language. Use the first
4482 // name in 'spelllang' up to '_region' or '.encoding'.
4483 for (p = q; *p != NUL; ++p)
4484 if (!ASCII_ISALNUM(*p) && *p != '-')
4485 break;
4486 if (p > q)
4487 {
4488 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
4489 (int)(p - q), q);
4490 source_runtime(fname, DIP_ALL);
4491 }
4492}
4493#endif
4494
4495/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004496 * Handle string options that need some action to perform when changed.
zeertzjqf6782732022-07-27 18:26:03 +01004497 * The new value must be allocated.
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004498 * Returns NULL for success, or an untranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +02004499 */
4500 char *
4501did_set_string_option(
4502 int opt_idx, // index in options[] table
4503 char_u **varp, // pointer to the option variable
Bram Moolenaardac13472019-09-16 21:06:21 +02004504 char_u *oldval, // previous value of the option
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004505 char_u *value, // new value of the option
Bram Moolenaardac13472019-09-16 21:06:21 +02004506 char *errbuf, // buffer for errors, or NULL
Mike Williams620f0112023-12-05 15:36:06 +01004507 size_t errbuflen, // length of error buffer
Bram Moolenaardac13472019-09-16 21:06:21 +02004508 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004509 set_op_T op, // OP_ADDING/OP_PREPENDING/OP_REMOVING
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004510 int *value_checked) // value was checked to be safe, no
Bram Moolenaardac13472019-09-16 21:06:21 +02004511 // need to set P_INSECURE
4512{
4513 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +02004514 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004515 opt_did_set_cb_T did_set_cb = get_option_did_set_cb(opt_idx);
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004516 optset_T args;
4517
4518 // 'ttytype' is an alias for 'term'. Both 'term' and 'ttytype' point to
4519 // T_NAME. If 'term' or 'ttytype' is modified, then use the index for the
4520 // 'term' option. Only set the P_ALLOCED flag on 'term'.
4521 if (varp == &T_NAME)
4522 {
4523 opt_idx = findoption((char_u *)"term");
4524 if (opt_idx >= 0)
4525 {
4526 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
4527 did_set_cb = get_option_did_set_cb(opt_idx);
4528 }
4529 }
4530
4531 CLEAR_FIELD(args);
Bram Moolenaardac13472019-09-16 21:06:21 +02004532
Bram Moolenaardac13472019-09-16 21:06:21 +02004533 // Disallow changing some options from secure mode
4534 if ((secure
4535#ifdef HAVE_SANDBOX
4536 || sandbox != 0
4537#endif
4538 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00004539 errmsg = e_not_allowed_here;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004540 // Check for a "normal" directory or file name in some options.
4541 else if (check_illegal_path_names(opt_idx, varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004542 errmsg = e_invalid_argument;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004543 else if (did_set_cb != NULL)
4544 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004545 args.os_varp = (char_u *)varp;
4546 args.os_idx = opt_idx;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004547 args.os_flags = opt_flags;
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004548 args.os_op = op;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004549 args.os_oldval.string = oldval;
4550 args.os_newval.string = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004551 args.os_errbuf = errbuf;
Christian Brabandtb39b2402023-11-29 11:34:05 +01004552 args.os_errbuflen = errbuflen;
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004553 // Invoke the option specific callback function to validate and apply
4554 // the new option value.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004555 errmsg = did_set_cb(&args);
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004556
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004557 // The 'keymap', 'filetype' and 'syntax' option callback functions
4558 // may change the os_value_checked field.
4559 *value_checked = args.os_value_checked;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004560 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004561
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004562 // If an error is detected, restore the previous value.
Bram Moolenaardac13472019-09-16 21:06:21 +02004563 if (errmsg != NULL)
4564 {
zeertzjqf6782732022-07-27 18:26:03 +01004565 free_string_option(*varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02004566 *varp = oldval;
4567 // When resetting some values, need to act on it.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004568 if (args.os_restore_chartab)
Bram Moolenaardac13472019-09-16 21:06:21 +02004569 (void)init_chartab();
4570 if (varp == &p_hl)
4571 (void)highlight_changed();
4572 }
4573 else
4574 {
4575#ifdef FEAT_EVAL
4576 // Remember where the option was set.
4577 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4578#endif
4579 // Free string options that are in allocated memory.
4580 // Use "free_oldval", because recursiveness may change the flags under
4581 // our fingers (esp. init_highlight()).
4582 if (free_oldval)
4583 free_string_option(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01004584 set_option_flag(opt_idx, P_ALLOCED);
Bram Moolenaardac13472019-09-16 21:06:21 +02004585
4586 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
4587 && is_global_local_option(opt_idx))
4588 {
4589 // global option with local value set to use global value; free
4590 // the local value and make it empty
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004591 char_u *p = get_option_varp_scope(opt_idx, OPT_LOCAL);
Bram Moolenaardac13472019-09-16 21:06:21 +02004592 free_string_option(*(char_u **)p);
4593 *(char_u **)p = empty_option;
4594 }
4595
4596 // May set global value for local option.
4597 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
4598 set_string_option_global(opt_idx, varp);
4599
4600 // Trigger the autocommand only after setting the flags.
4601#ifdef FEAT_SYN_HL
Bram Moolenaardac13472019-09-16 21:06:21 +02004602 if (varp == &(curbuf->b_p_syn))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004603 do_syntax_autocmd(args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004604#endif
4605 else if (varp == &(curbuf->b_p_ft))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004606 do_filetype_autocmd(varp, opt_flags, args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004607#ifdef FEAT_SPELL
4608 if (varp == &(curwin->w_s->b_p_spl))
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004609 do_spelllang_source();
Bram Moolenaardac13472019-09-16 21:06:21 +02004610#endif
4611 }
4612
Bram Moolenaardac13472019-09-16 21:06:21 +02004613 if (varp == &p_mouse)
4614 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004615 if (*p_mouse == NUL)
4616 mch_setmouse(FALSE); // switch mouse off
4617 else
Bram Moolenaardac13472019-09-16 21:06:21 +02004618 setmouse(); // in case 'mouse' changed
4619 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004620
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004621#if defined(FEAT_LUA) || defined(PROTO)
4622 if (varp == &p_rtp)
4623 update_package_paths_in_lua();
4624#endif
4625
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004626#if defined(FEAT_LINEBREAK)
4627 // Changing Formatlistpattern when briopt includes the list setting:
4628 // redraw
4629 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
4630 && curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004631 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004632#endif
4633
Bram Moolenaardac13472019-09-16 21:06:21 +02004634 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004635 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0
4636 && (get_option_flags(opt_idx) & P_HLONLY) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02004637 curwin->w_set_curswant = TRUE;
4638
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004639 if ((opt_flags & OPT_NO_REDRAW) == 0)
4640 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004641#ifdef FEAT_GUI
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004642 // set when changing an option that only requires a redraw in the GUI
4643 int redraw_gui_only = FALSE;
4644
4645 if (varp == &p_go // 'guioptions'
4646 || varp == &p_guifont // 'guifont'
4647# ifdef FEAT_GUI_TABLINE
4648 || varp == &p_gtl // 'guitablabel'
4649 || varp == &p_gtt // 'guitabtooltip'
4650# endif
4651# ifdef FEAT_XFONTSET
4652 || varp == &p_guifontset // 'guifontset'
4653# endif
4654 || varp == &p_guifontwide // 'guifontwide'
Erik S. V. Jansson2f026382024-02-26 22:23:05 +01004655# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004656 || varp == &p_guiligatures // 'guiligatures'
4657# endif
4658 )
4659 redraw_gui_only = TRUE;
4660
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004661 // check redraw when it's not a GUI option or the GUI is active.
4662 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02004663#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004664 check_redraw(get_option_flags(opt_idx));
4665 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004666
4667#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004668 if (args.os_did_swaptcap)
Bram Moolenaardac13472019-09-16 21:06:21 +02004669 {
4670 set_termname((char_u *)"win32");
4671 init_highlight(TRUE, FALSE);
4672 }
4673#endif
4674
4675 return errmsg;
4676}
4677
4678/*
4679 * Check an option that can be a range of string values.
4680 *
4681 * Return OK for correct value, FAIL otherwise.
4682 * Empty is always OK.
4683 */
4684 static int
4685check_opt_strings(
4686 char_u *val,
4687 char **values,
4688 int list) // when TRUE: accept a list of values
4689{
4690 return opt_strings_flags(val, values, NULL, list);
4691}
4692
4693/*
4694 * Handle an option that can be a range of string values.
4695 * Set a flag in "*flagp" for each string present.
4696 *
4697 * Return OK for correct value, FAIL otherwise.
4698 * Empty is always OK.
4699 */
4700 static int
4701opt_strings_flags(
4702 char_u *val, // new value
4703 char **values, // array of valid string values
4704 unsigned *flagp,
4705 int list) // when TRUE: accept a list of values
4706{
4707 int i;
4708 int len;
4709 unsigned new_flags = 0;
4710
4711 while (*val)
4712 {
4713 for (i = 0; ; ++i)
4714 {
4715 if (values[i] == NULL) // val not found in values[]
4716 return FAIL;
4717
4718 len = (int)STRLEN(values[i]);
4719 if (STRNCMP(values[i], val, len) == 0
4720 && ((list && val[len] == ',') || val[len] == NUL))
4721 {
4722 val += len + (val[len] == ',');
4723 new_flags |= (1 << i);
4724 break; // check next item in val list
4725 }
4726 }
4727 }
4728 if (flagp != NULL)
4729 *flagp = new_flags;
4730
4731 return OK;
4732}
4733
4734/*
4735 * return OK if "p" is a valid fileformat name, FAIL otherwise.
4736 */
4737 int
4738check_ff_value(char_u *p)
4739{
4740 return check_opt_strings(p, p_ff_values, FALSE);
4741}
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004742
4743/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004744 * Save the actual shortmess Flags and clear them temporarily to avoid that
4745 * file messages overwrites any output from the following commands.
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004746 *
4747 * Caller must make sure to first call save_clear_shm_value() and then
4748 * restore_shm_value() exactly the same number of times.
4749 */
4750 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004751save_clear_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004752{
4753 if (STRLEN(p_shm) >= SHM_LEN)
4754 {
4755 iemsg(e_internal_error_shortmess_too_long);
4756 return;
4757 }
4758
4759 if (++set_shm_recursive == 1)
4760 {
4761 STRCPY(shm_buf, p_shm);
4762 set_option_value_give_err((char_u *)"shm", 0L, (char_u *)"", 0);
4763 }
4764}
4765
4766/*
4767 * Restore the shortmess Flags set from the save_clear_shm_value() function.
4768 */
4769 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004770restore_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004771{
4772 if (--set_shm_recursive == 0)
4773 {
4774 set_option_value_give_err((char_u *)"shm", 0L, shm_buf, 0);
4775 vim_memset(shm_buf, 0, SHM_LEN);
4776 }
4777}