blob: d7cb38a3aad21a76c7e02da8e89352789406a411 [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
Yegappan Lakshmananac023e82024-11-27 20:55:45 +01003333 {
3334 // Validate the flags in 'rulerformat' only if it doesn't point to
3335 // a custom function ("%!" flag).
3336 if ((*varp)[1] != '!')
3337 errmsg = check_stl_option(p_ruf);
3338 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003339 }
3340 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
3341 else if (rulerformat || s[0] != '%' || s[1] != '!')
3342 errmsg = check_stl_option(s);
3343 if (rulerformat && errmsg == NULL)
3344 comp_col();
3345
3346 return errmsg;
3347}
3348#endif
3349
3350#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
3351/*
3352 * The 'renderoptions' option is changed.
3353 */
3354 char *
3355did_set_renderoptions(optset_T *args UNUSED)
3356{
3357 if (!gui_mch_set_rendering_options(p_rop))
3358 return e_invalid_argument;
3359
3360 return NULL;
3361}
3362#endif
3363
3364#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3365/*
3366 * The 'rightleftcmd' option is changed.
3367 */
3368 char *
3369did_set_rightleftcmd(optset_T *args)
3370{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003371 char_u **varp = (char_u **)args->os_varp;
3372
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003373 // Currently only "search" is a supported value.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003374 if (**varp != NUL && STRCMP(*varp, "search") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003375 return e_invalid_argument;
3376
3377 return NULL;
3378}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003379
3380 int
3381expand_set_rightleftcmd(optexpand_T *args, int *numMatches, char_u ***matches)
3382{
3383 static char *(p_rlc_values[]) = {"search", NULL};
3384 return expand_set_opt_string(
3385 args,
3386 p_rlc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003387 ARRAY_LENGTH(p_rlc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003388 numMatches,
3389 matches);
3390}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003391#endif
3392
3393#if defined(FEAT_STL_OPT) || defined(PROTO)
3394/*
3395 * The 'rulerformat' option is changed.
3396 */
3397 char *
3398did_set_rulerformat(optset_T *args)
3399{
3400 return parse_statustabline_rulerformat(args, TRUE);
3401}
3402#endif
3403
3404/*
3405 * The 'scrollopt' option is changed.
3406 */
3407 char *
3408did_set_scrollopt(optset_T *args UNUSED)
3409{
3410 return did_set_opt_strings(p_sbo, p_scbopt_values, TRUE);
3411}
3412
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003413 int
3414expand_set_scrollopt(optexpand_T *args, int *numMatches, char_u ***matches)
3415{
3416 return expand_set_opt_string(
3417 args,
3418 p_scbopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003419 ARRAY_LENGTH(p_scbopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003420 numMatches,
3421 matches);
3422}
3423
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003424/*
3425 * The 'selection' option is changed.
3426 */
3427 char *
3428did_set_selection(optset_T *args UNUSED)
3429{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003430 if (*p_sel == NUL || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003431 return e_invalid_argument;
3432
3433 return NULL;
3434}
3435
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003436 int
3437expand_set_selection(optexpand_T *args, int *numMatches, char_u ***matches)
3438{
3439 return expand_set_opt_string(
3440 args,
3441 p_sel_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003442 ARRAY_LENGTH(p_sel_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003443 numMatches,
3444 matches);
3445}
3446
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003447/*
3448 * The 'selectmode' option is changed.
3449 */
3450 char *
3451did_set_selectmode(optset_T *args UNUSED)
3452{
3453 return did_set_opt_strings(p_slm, p_slm_values, TRUE);
3454}
3455
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003456 int
3457expand_set_selectmode(optexpand_T *args, int *numMatches, char_u ***matches)
3458{
3459 return expand_set_opt_string(
3460 args,
3461 p_slm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003462 ARRAY_LENGTH(p_slm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003463 numMatches,
3464 matches);
3465}
3466
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003467#if defined(FEAT_SESSION) || defined(PROTO)
3468/*
3469 * The 'sessionoptions' option is changed.
3470 */
3471 char *
3472did_set_sessionoptions(optset_T *args)
3473{
3474 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
3475 return e_invalid_argument;
3476 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
3477 {
3478 // Don't allow both "sesdir" and "curdir".
3479 (void)opt_strings_flags(args->os_oldval.string, p_ssop_values,
3480 &ssop_flags, TRUE);
3481 return e_invalid_argument;
3482 }
3483
3484 return NULL;
3485}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003486
3487 int
3488expand_set_sessionoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3489{
3490 return expand_set_opt_string(
3491 args,
3492 p_ssop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003493 ARRAY_LENGTH(p_ssop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003494 numMatches,
3495 matches);
3496}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003497#endif
3498
3499/*
3500 * The 'shortmess' option is changed.
3501 */
3502 char *
3503did_set_shortmess(optset_T *args)
3504{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003505 char_u **varp = (char_u **)args->os_varp;
3506
Christian Brabandtb39b2402023-11-29 11:34:05 +01003507 return did_set_option_listflag(*varp, (char_u *)SHM_ALL, args->os_errbuf,
3508 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003509}
3510
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003511 int
3512expand_set_shortmess(optexpand_T *args, int *numMatches, char_u ***matches)
3513{
3514 return expand_set_opt_listflag(args, (char_u*)SHM_ALL, numMatches, matches);
3515}
3516
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003517#if defined(FEAT_LINEBREAK) || defined(PROTO)
3518/*
3519 * The 'showbreak' option is changed.
3520 */
3521 char *
3522did_set_showbreak(optset_T *args)
3523{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003524 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003525 char_u *s;
3526
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003527 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003528 {
3529 if (ptr2cells(s) != 1)
3530 return e_showbreak_contains_unprintable_or_wide_character;
3531 MB_PTR_ADV(s);
3532 }
3533
3534 return NULL;
3535}
3536#endif
3537
3538/*
3539 * The 'showcmdloc' option is changed.
3540 */
3541 char *
3542did_set_showcmdloc(optset_T *args UNUSED)
3543{
zeertzjqc27fcf42024-03-01 23:01:43 +01003544 char *errmsg = did_set_opt_strings(p_sloc, p_sloc_values, FALSE);
3545
3546 if (errmsg == NULL)
3547 comp_col();
3548
3549 return errmsg;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003550}
3551
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003552 int
3553expand_set_showcmdloc(optexpand_T *args, int *numMatches, char_u ***matches)
3554{
3555 return expand_set_opt_string(
3556 args,
3557 p_sloc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003558 ARRAY_LENGTH(p_sloc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003559 numMatches,
3560 matches);
3561}
3562
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003563#if defined(FEAT_SIGNS) || defined(PROTO)
3564/*
3565 * The 'signcolumn' option is changed.
3566 */
3567 char *
3568did_set_signcolumn(optset_T *args)
3569{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003570 char_u **varp = (char_u **)args->os_varp;
3571
3572 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003573 return e_invalid_argument;
3574 // When changing the 'signcolumn' to or from 'number', recompute the
3575 // width of the number column if 'number' or 'relativenumber' is set.
3576 if (((*args->os_oldval.string == 'n' && args->os_oldval.string[1] == 'u')
3577 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
3578 && (curwin->w_p_nu || curwin->w_p_rnu))
3579 curwin->w_nrwidth_line_count = 0;
3580
3581 return NULL;
3582}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003583
3584 int
3585expand_set_signcolumn(optexpand_T *args, int *numMatches, char_u ***matches)
3586{
3587 return expand_set_opt_string(
3588 args,
3589 p_scl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003590 ARRAY_LENGTH(p_scl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003591 numMatches,
3592 matches);
3593}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003594#endif
3595
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003596#if defined(FEAT_SPELL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003597/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003598 * The 'spellcapcheck' option is changed.
3599 */
3600 char *
3601did_set_spellcapcheck(optset_T *args UNUSED)
3602{
3603 // compile the regexp program.
3604 return compile_cap_prog(curwin->w_s);
3605}
3606
3607/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003608 * The 'spellfile' option is changed.
3609 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003610 char *
3611did_set_spellfile(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003612{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003613 char_u **varp = (char_u **)args->os_varp;
3614
3615 if (!valid_spellfile(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003616 return e_invalid_argument;
3617
3618 // If there is a window for this buffer in which 'spell' is set load the
3619 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003620 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003621}
3622
3623/*
3624 * The 'spell' option is changed.
3625 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003626 char *
3627did_set_spelllang(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003628{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003629 char_u **varp = (char_u **)args->os_varp;
3630
3631 if (!valid_spelllang(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003632 return e_invalid_argument;
3633
3634 // If there is a window for this buffer in which 'spell' is set load the
3635 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003636 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003637}
3638
3639/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003640 * The 'spelloptions' option is changed.
3641 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003642 char *
3643did_set_spelloptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003644{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003645 char_u **varp = (char_u **)args->os_varp;
3646
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003647 if (**varp != NUL && STRCMP(*varp, "camel") != 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003648 return e_invalid_argument;
3649
3650 return NULL;
3651}
3652
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003653 int
3654expand_set_spelloptions(optexpand_T *args, int *numMatches, char_u ***matches)
3655{
3656 static char *(p_spo_values[]) = {"camel", NULL};
3657 return expand_set_opt_string(
3658 args,
3659 p_spo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003660 ARRAY_LENGTH(p_spo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003661 numMatches,
3662 matches);
3663}
3664
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003665/*
3666 * The 'spellsuggest' option is changed.
3667 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003668 char *
3669did_set_spellsuggest(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003670{
3671 if (spell_check_sps() != OK)
3672 return e_invalid_argument;
3673
3674 return NULL;
3675}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003676
3677 int
3678expand_set_spellsuggest(optexpand_T *args, int *numMatches, char_u ***matches)
3679{
3680 return expand_set_opt_string(
3681 args,
3682 p_sps_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003683 ARRAY_LENGTH(p_sps_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003684 numMatches,
3685 matches);
3686}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003687#endif
3688
3689/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003690 * The 'splitkeep' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003691 */
3692 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003693did_set_splitkeep(optset_T *args UNUSED)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003694{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003695 return did_set_opt_strings(p_spk, p_spk_values, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003696}
3697
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003698 int
3699expand_set_splitkeep(optexpand_T *args, int *numMatches, char_u ***matches)
3700{
3701 return expand_set_opt_string(
3702 args,
3703 p_spk_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003704 ARRAY_LENGTH(p_spk_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003705 numMatches,
3706 matches);
3707}
3708
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00003709#if defined(FEAT_STL_OPT) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003710/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003711 * The 'statusline' option is changed.
3712 */
3713 char *
3714did_set_statusline(optset_T *args)
3715{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003716 return parse_statustabline_rulerformat(args, FALSE);
3717}
3718#endif
3719
3720/*
3721 * The 'swapsync' option is changed.
3722 */
3723 char *
3724did_set_swapsync(optset_T *args UNUSED)
3725{
3726 return did_set_opt_strings(p_sws, p_sws_values, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003727}
3728
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003729 int
3730expand_set_swapsync(optexpand_T *args, int *numMatches, char_u ***matches)
3731{
3732 return expand_set_opt_string(
3733 args,
3734 p_sws_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003735 ARRAY_LENGTH(p_sws_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003736 numMatches,
3737 matches);
3738}
3739
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003740/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003741 * The 'switchbuf' option is changed.
3742 */
3743 char *
3744did_set_switchbuf(optset_T *args UNUSED)
3745{
3746 return did_set_opt_flags(p_swb, p_swb_values, &swb_flags, TRUE);
3747}
3748
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003749 int
3750expand_set_switchbuf(optexpand_T *args, int *numMatches, char_u ***matches)
3751{
3752 return expand_set_opt_string(
3753 args,
3754 p_swb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003755 ARRAY_LENGTH(p_swb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003756 numMatches,
3757 matches);
3758}
3759
LemonBoy5247b0b2024-07-12 19:30:58 +02003760/*
3761 * The 'tabclose' option is changed.
3762 */
3763 char *
3764did_set_tabclose(optset_T *args UNUSED)
3765{
3766 return did_set_opt_flags(p_tcl, p_tcl_values, &tcl_flags, TRUE);
3767}
3768
3769 int
3770expand_set_tabclose(optexpand_T *args, int *numMatches, char_u ***matches)
3771{
3772 return expand_set_opt_string(
3773 args,
3774 p_tcl_values,
3775 ARRAY_LENGTH(p_tcl_values) - 1,
3776 numMatches,
3777 matches);
3778}
3779
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003780#if defined(FEAT_STL_OPT) || defined(PROTO)
3781/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003782 * The 'tabline' option is changed.
3783 */
3784 char *
3785did_set_tabline(optset_T *args)
3786{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003787 return parse_statustabline_rulerformat(args, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003788}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003789#endif
3790
3791/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003792 * The 'tagcase' option is changed.
3793 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003794 char *
3795did_set_tagcase(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003796{
3797 unsigned int *flags;
3798 char_u *p;
3799
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003800 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003801 {
3802 p = curbuf->b_p_tc;
3803 flags = &curbuf->b_tc_flags;
3804 }
3805 else
3806 {
3807 p = p_tc;
3808 flags = &tc_flags;
3809 }
3810
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003811 if ((args->os_flags & OPT_LOCAL) && *p == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003812 // make the local value empty: use the global value
3813 *flags = 0;
3814 else if (*p == NUL
3815 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
3816 return e_invalid_argument;
3817
3818 return NULL;
3819}
3820
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003821 int
3822expand_set_tagcase(optexpand_T *args, int *numMatches, char_u ***matches)
3823{
3824 return expand_set_opt_string(
3825 args,
3826 p_tc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003827 ARRAY_LENGTH(p_tc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003828 numMatches,
3829 matches);
3830}
3831
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003832/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003833 * The 'term' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003834 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003835 char *
3836did_set_term(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003837{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003838 if (T_NAME[0] == NUL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003839 return e_cannot_set_term_to_empty_string;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003840#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003841 if (gui.in_use)
3842 return e_cannot_change_term_in_GUI;
3843 if (term_is_gui(T_NAME))
3844 return e_use_gui_to_start_GUI;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003845#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003846 if (set_termname(T_NAME) == FAIL)
3847 return e_not_found_in_termcap;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003848
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003849 // Screen colors may have changed.
3850 redraw_later_clear();
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003851
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003852 return NULL;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003853}
3854
3855/*
3856 * Some terminal option (t_xxx) is changed
3857 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003858 char *
3859did_set_term_option(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003860{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003861 char_u **varp = (char_u **)args->os_varp;
3862
3863 if (!full_screen)
3864 return NULL;
3865
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003866 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
3867 if (varp == &T_CCO)
3868 {
3869 int colors = atoi((char *)T_CCO);
3870
3871 // Only reinitialize colors if t_Co value has really changed to
3872 // avoid expensive reload of colorscheme if t_Co is set to the
3873 // same value multiple times.
3874 if (colors != t_colors)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003875 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003876 t_colors = colors;
3877 if (t_colors <= 1)
3878 {
3879 vim_free(T_CCO);
3880 T_CCO = empty_option;
3881 }
3882#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3883 if (is_term_win32())
3884 {
3885 swap_tcap();
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003886 args->os_did_swaptcap = TRUE;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003887 }
3888#endif
3889 // We now have a different color setup, initialize it again.
3890 init_highlight(TRUE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003891 }
3892 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003893 ttest(FALSE);
3894 if (varp == &T_ME)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003895 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003896 out_str(T_ME);
3897 redraw_later(UPD_CLEAR);
3898#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3899 // Since t_me has been set, this probably means that the user
3900 // wants to use this as default colors. Need to reset default
3901 // background/foreground colors.
3902# ifdef VIMDLL
3903 if (!gui.in_use && !gui.starting)
3904# endif
3905 mch_set_normal_colors();
3906#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003907 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003908 if (varp == &T_BE && termcap_active)
3909 {
3910 MAY_WANT_TO_LOG_THIS;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003911
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003912 if (*T_BE == NUL)
3913 // When clearing t_BE we assume the user no longer wants
3914 // bracketed paste, thus disable it by writing t_BD.
3915 out_str(T_BD);
3916 else
3917 out_str(T_BE);
3918 }
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003919
3920 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003921}
3922
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003923#if defined(FEAT_TERMINAL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003924/*
3925 * The 'termwinkey' option is changed.
3926 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003927 char *
Milly94606f72024-10-22 22:07:52 +02003928did_set_termwinkey(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003929{
Milly94606f72024-10-22 22:07:52 +02003930 char_u **varp = (char_u **)args->os_varp;
3931
3932 if ((*varp)[0] != NUL && string_to_key(*varp, TRUE) == 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003933 return e_invalid_argument;
3934
3935 return NULL;
3936}
3937
3938/*
3939 * The 'termwinsize' option is changed.
3940 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003941 char *
Milly8be10aa2024-10-22 22:01:46 +02003942did_set_termwinsize(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003943{
Milly8be10aa2024-10-22 22:01:46 +02003944 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003945 char_u *p;
3946
Milly8be10aa2024-10-22 22:01:46 +02003947 if ((*varp)[0] == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003948 return NULL;
3949
Milly8be10aa2024-10-22 22:01:46 +02003950 p = skipdigits(*varp);
3951 if (p == *varp || (*p != 'x' && *p != '*') || *skipdigits(p + 1) != NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003952 return e_invalid_argument;
3953
3954 return NULL;
3955}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003956
3957# if defined(MSWIN) || defined(PROTO)
3958/*
3959 * The 'termwintype' option is changed.
3960 */
3961 char *
3962did_set_termwintype(optset_T *args UNUSED)
3963{
3964 return did_set_opt_strings(p_twt, p_twt_values, FALSE);
3965}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003966
3967 int
3968expand_set_termwintype(optexpand_T *args, int *numMatches, char_u ***matches)
3969{
3970 return expand_set_opt_string(
3971 args,
3972 p_twt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003973 ARRAY_LENGTH(p_twt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003974 numMatches,
3975 matches);
3976}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003977# endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003978#endif
3979
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003980/*
3981 * The 'titlestring' option is changed.
3982 */
3983 char *
3984did_set_titlestring(optset_T *args)
3985{
3986 int flagval = 0;
3987
3988#ifdef FEAT_STL_OPT
3989 flagval = STL_IN_TITLE;
3990#endif
3991 return parse_titleiconstring(args, flagval);
3992}
3993
3994#if (defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)) || defined(PROTO)
3995/*
3996 * The 'toolbar' option is changed.
3997 */
3998 char *
3999did_set_toolbar(optset_T *args UNUSED)
4000{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004001 if (opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags,
4002 TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004003 return e_invalid_argument;
4004
4005 out_flush();
4006 gui_mch_show_toolbar((toolbar_flags &
4007 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4008 return NULL;
4009}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004010
4011 int
4012expand_set_toolbar(optexpand_T *args, int *numMatches, char_u ***matches)
4013{
4014 return expand_set_opt_string(
4015 args,
4016 p_toolbar_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004017 ARRAY_LENGTH(p_toolbar_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004018 numMatches,
4019 matches);
4020}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004021#endif
4022
4023#if (defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)) || defined(PROTO)
4024/*
4025 * The 'toolbariconsize' option is changed. GTK+ 2 only.
4026 */
4027 char *
4028did_set_toolbariconsize(optset_T *args UNUSED)
4029{
4030 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
4031 return e_invalid_argument;
4032
4033 out_flush();
4034 gui_mch_show_toolbar((toolbar_flags &
4035 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4036 return NULL;
4037}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004038
4039 int
4040expand_set_toolbariconsize(optexpand_T *args, int *numMatches, char_u ***matches)
4041{
4042 return expand_set_opt_string(
4043 args,
4044 p_tbis_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004045 ARRAY_LENGTH(p_tbis_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004046 numMatches,
4047 matches);
4048}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004049#endif
4050
4051#if defined(UNIX) || defined(VMS) || defined(PROTO)
4052/*
4053 * The 'ttymouse' option is changed.
4054 */
4055 char *
4056did_set_ttymouse(optset_T *args UNUSED)
4057{
4058 char *errmsg = NULL;
4059
4060 // Switch the mouse off before changing the escape sequences used for
4061 // that.
4062 mch_setmouse(FALSE);
4063 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
4064 errmsg = e_invalid_argument;
4065 else
4066 check_mouse_termcode();
4067 if (termcap_active)
4068 setmouse(); // may switch it on again
4069
4070 return errmsg;
4071}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004072
4073 int
4074expand_set_ttymouse(optexpand_T *args, int *numMatches, char_u ***matches)
4075{
4076 return expand_set_opt_string(
4077 args,
4078 p_ttym_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004079 ARRAY_LENGTH(p_ttym_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004080 numMatches,
4081 matches);
4082}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004083#endif
4084
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004085#if defined(FEAT_VARTABS) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004086/*
4087 * The 'varsofttabstop' option is changed.
4088 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004089 char *
4090did_set_varsofttabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004091{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004092 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004093 char_u *cp;
4094
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004095 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004096 VIM_CLEAR(curbuf->b_p_vsts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004097 else
4098 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004099 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004100 {
4101 if (vim_isdigit(*cp))
4102 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004103 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004104 continue;
4105 return e_invalid_argument;
4106 }
4107
4108 int *oldarray = curbuf->b_p_vsts_array;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004109 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004110 {
4111 if (oldarray)
4112 vim_free(oldarray);
4113 }
4114 else
4115 return e_invalid_argument;
4116 }
4117
4118 return NULL;
4119}
4120
4121/*
4122 * The 'vartabstop' option is changed.
4123 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004124 char *
4125did_set_vartabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004126{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004127 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004128 char_u *cp;
4129
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004130 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004131 VIM_CLEAR(curbuf->b_p_vts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004132 else
4133 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004134 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004135 {
4136 if (vim_isdigit(*cp))
4137 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004138 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004139 continue;
4140 return e_invalid_argument;
4141 }
4142
4143 int *oldarray = curbuf->b_p_vts_array;
4144
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004145 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004146 {
4147 vim_free(oldarray);
4148# ifdef FEAT_FOLDING
4149 if (foldmethodIsIndent(curwin))
4150 foldUpdateAll(curwin);
4151# endif
4152 }
4153 else
4154 return e_invalid_argument;
4155 }
4156
4157 return NULL;
4158}
4159#endif
4160
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004161/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004162 * The 'verbosefile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004163 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004164 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004165did_set_verbosefile(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004166{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004167 verbose_stop();
4168 if (*p_vfile != NUL && verbose_open() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004169 return e_invalid_argument;
4170
4171 return NULL;
4172}
4173
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004174#if defined(FEAT_SESSION) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004175/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004176 * The 'viewoptions' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004177 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004178 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004179did_set_viewoptions(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004180{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004181 return did_set_opt_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004182}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004183#endif
4184
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004185#if defined(FEAT_VIMINFO) || defined(PROTO)
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004186/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004187 * The 'viminfo' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004188 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004189 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004190did_set_viminfo(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004191{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004192 char_u *s;
4193 char *errmsg = NULL;
4194
4195 for (s = p_viminfo; *s;)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004196 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004197 // Check it's a valid character
4198 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
4199 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01004200 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004201 break;
4202 }
4203 if (*s == 'n') // name is always last one
4204 break;
4205 else if (*s == 'r') // skip until next ','
4206 {
4207 while (*++s && *s != ',')
4208 ;
4209 }
4210 else if (*s == '%')
4211 {
4212 // optional number
4213 while (vim_isdigit(*++s))
4214 ;
4215 }
4216 else if (*s == '!' || *s == 'h' || *s == 'c')
4217 ++s; // no extra chars
4218 else // must have a number
4219 {
4220 while (vim_isdigit(*++s))
4221 ;
4222
4223 if (!VIM_ISDIGIT(*(s - 1)))
4224 {
4225 if (args->os_errbuf != NULL)
4226 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01004227 vim_snprintf(args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004228 _(e_missing_number_after_angle_str_angle),
4229 transchar_byte(*(s - 1)));
4230 errmsg = args->os_errbuf;
4231 }
4232 else
4233 errmsg = "";
4234 break;
4235 }
4236 }
4237 if (*s == ',')
4238 ++s;
4239 else if (*s)
4240 {
4241 if (args->os_errbuf != NULL)
4242 errmsg = e_missing_comma;
4243 else
4244 errmsg = "";
4245 break;
4246 }
4247 }
4248 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
4249 errmsg = e_must_specify_a_value;
4250
4251 return errmsg;
4252}
4253#endif
4254
4255/*
4256 * The 'virtualedit' option is changed.
4257 */
4258 char *
4259did_set_virtualedit(optset_T *args)
4260{
4261 char_u *ve = p_ve;
4262 unsigned int *flags = &ve_flags;
4263
4264 if (args->os_flags & OPT_LOCAL)
4265 {
4266 ve = curwin->w_p_ve;
4267 flags = &curwin->w_ve_flags;
4268 }
4269
4270 if ((args->os_flags & OPT_LOCAL) && *ve == NUL)
4271 // make the local value empty: use the global value
4272 *flags = 0;
4273 else
4274 {
4275 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
4276 return e_invalid_argument;
4277 else if (STRCMP(ve, args->os_oldval.string) != 0)
4278 {
4279 // Recompute cursor position in case the new 've' setting
4280 // changes something.
4281 validate_virtcol();
4282 coladvance(curwin->w_virtcol);
4283 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004284 }
4285
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004286 return NULL;
4287}
4288
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004289 int
4290expand_set_virtualedit(optexpand_T *args, int *numMatches, char_u ***matches)
4291{
4292 return expand_set_opt_string(
4293 args,
4294 p_ve_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004295 ARRAY_LENGTH(p_ve_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004296 numMatches,
4297 matches);
4298}
4299
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004300/*
4301 * The 'whichwrap' option is changed.
4302 */
4303 char *
4304did_set_whichwrap(optset_T *args)
4305{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004306 char_u **varp = (char_u **)args->os_varp;
4307
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004308 // Add ',' to the list flags because 'whichwrap' is a flag
4309 // list that is comma-separated.
Christian Brabandtb39b2402023-11-29 11:34:05 +01004310 return did_set_option_listflag(*varp, (char_u *)(WW_ALL ","),
4311 args->os_errbuf, args->os_errbuflen);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004312}
4313
4314 int
4315expand_set_whichwrap(optexpand_T *args, int *numMatches, char_u ***matches)
4316{
4317 return expand_set_opt_listflag(args, (char_u*)WW_ALL, numMatches, matches);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004318}
4319
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004320/*
4321 * The 'wildmode' option is changed.
4322 */
4323 char *
4324did_set_wildmode(optset_T *args UNUSED)
4325{
4326 if (check_opt_wim() == FAIL)
4327 return e_invalid_argument;
4328 return NULL;
4329}
4330
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004331 int
4332expand_set_wildmode(optexpand_T *args, int *numMatches, char_u ***matches)
4333{
4334 return expand_set_opt_string(
4335 args,
4336 p_wim_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004337 ARRAY_LENGTH(p_wim_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004338 numMatches,
4339 matches);
4340}
4341
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004342/*
4343 * The 'wildoptions' option is changed.
4344 */
4345 char *
4346did_set_wildoptions(optset_T *args UNUSED)
4347{
4348 return did_set_opt_strings(p_wop, p_wop_values, TRUE);
4349}
4350
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004351 int
4352expand_set_wildoptions(optexpand_T *args, int *numMatches, char_u ***matches)
4353{
4354 return expand_set_opt_string(
4355 args,
4356 p_wop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004357 ARRAY_LENGTH(p_wop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004358 numMatches,
4359 matches);
4360}
4361
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004362#if defined(FEAT_WAK) || defined(PROTO)
4363/*
4364 * The 'winaltkeys' option is changed.
4365 */
4366 char *
4367did_set_winaltkeys(optset_T *args UNUSED)
4368{
4369 char *errmsg = NULL;
4370
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004371 if (*p_wak == NUL || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004372 errmsg = e_invalid_argument;
4373# ifdef FEAT_MENU
4374# if defined(FEAT_GUI_MOTIF)
4375 else if (gui.in_use)
4376 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4377# elif defined(FEAT_GUI_GTK)
4378 else if (gui.in_use)
4379 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4380# endif
4381# endif
4382 return errmsg;
4383}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004384
4385 int
4386expand_set_winaltkeys(optexpand_T *args, int *numMatches, char_u ***matches)
4387{
4388 return expand_set_opt_string(
4389 args,
4390 p_wak_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004391 ARRAY_LENGTH(p_wak_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004392 numMatches,
4393 matches);
4394}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004395#endif
4396
4397/*
4398 * The 'wincolor' option is changed.
4399 */
4400 char *
4401did_set_wincolor(optset_T *args UNUSED)
4402{
4403#ifdef FEAT_TERMINAL
4404 term_update_wincolor(curwin);
4405#endif
4406 return NULL;
4407}
4408
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004409 int
4410expand_set_wincolor(optexpand_T *args, int *numMatches, char_u ***matches)
4411{
4412 return expand_set_opt_generic(
4413 args,
4414 get_highlight_name,
4415 numMatches,
4416 matches);
4417}
4418
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004419#ifdef FEAT_SYN_HL
4420/*
4421 * When the 'syntax' option is set, load the syntax of that name.
4422 */
4423 static void
4424do_syntax_autocmd(int value_changed)
4425{
4426 static int syn_recursive = 0;
4427
4428 ++syn_recursive;
4429 // Only pass TRUE for "force" when the value changed or not used
4430 // recursively, to avoid endless recurrence.
4431 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
4432 value_changed || syn_recursive == 1, curbuf);
4433 curbuf->b_flags |= BF_SYN_SET;
4434 --syn_recursive;
4435}
4436#endif
4437
4438/*
4439 * When the 'filetype' option is set, trigger the FileType autocommand.
4440 */
4441 static void
4442do_filetype_autocmd(char_u **varp, int opt_flags, int value_changed)
4443{
4444 // Skip this when called from a modeline and the filetype was already set
4445 // to this value.
4446 if ((opt_flags & OPT_MODELINE) && !value_changed)
4447 return;
4448
4449 static int ft_recursive = 0;
4450 int secure_save = secure;
4451
4452 // Reset the secure flag, since the value of 'filetype' has
4453 // been checked to be safe.
4454 secure = 0;
4455
4456 ++ft_recursive;
zeertzjq5bf6c212024-03-31 18:41:27 +02004457 curbuf->b_did_filetype = TRUE;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004458 // Only pass TRUE for "force" when the value changed or not
4459 // used recursively, to avoid endless recurrence.
4460 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
4461 value_changed || ft_recursive == 1, curbuf);
4462 --ft_recursive;
4463 // Just in case the old "curbuf" is now invalid.
4464 if (varp != &(curbuf->b_p_ft))
4465 varp = NULL;
4466
4467 secure = secure_save;
4468}
4469
4470#ifdef FEAT_SPELL
4471/*
4472 * When the 'spelllang' option is set, source the spell/LANG.vim file in
4473 * 'runtimepath'.
4474 */
4475 static void
4476do_spelllang_source(void)
4477{
4478 char_u fname[200];
4479 char_u *p;
4480 char_u *q = curwin->w_s->b_p_spl;
4481
4482 // Skip the first name if it is "cjk".
4483 if (STRNCMP(q, "cjk,", 4) == 0)
4484 q += 4;
4485
4486 // They could set 'spellcapcheck' depending on the language. Use the first
4487 // name in 'spelllang' up to '_region' or '.encoding'.
4488 for (p = q; *p != NUL; ++p)
4489 if (!ASCII_ISALNUM(*p) && *p != '-')
4490 break;
4491 if (p > q)
4492 {
4493 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
4494 (int)(p - q), q);
4495 source_runtime(fname, DIP_ALL);
4496 }
4497}
4498#endif
4499
4500/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004501 * Handle string options that need some action to perform when changed.
zeertzjqf6782732022-07-27 18:26:03 +01004502 * The new value must be allocated.
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004503 * Returns NULL for success, or an untranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +02004504 */
4505 char *
4506did_set_string_option(
4507 int opt_idx, // index in options[] table
4508 char_u **varp, // pointer to the option variable
Bram Moolenaardac13472019-09-16 21:06:21 +02004509 char_u *oldval, // previous value of the option
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004510 char_u *value, // new value of the option
Bram Moolenaardac13472019-09-16 21:06:21 +02004511 char *errbuf, // buffer for errors, or NULL
Mike Williams620f0112023-12-05 15:36:06 +01004512 size_t errbuflen, // length of error buffer
Bram Moolenaardac13472019-09-16 21:06:21 +02004513 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004514 set_op_T op, // OP_ADDING/OP_PREPENDING/OP_REMOVING
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004515 int *value_checked) // value was checked to be safe, no
Bram Moolenaardac13472019-09-16 21:06:21 +02004516 // need to set P_INSECURE
4517{
4518 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +02004519 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004520 opt_did_set_cb_T did_set_cb = get_option_did_set_cb(opt_idx);
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004521 optset_T args;
4522
4523 // 'ttytype' is an alias for 'term'. Both 'term' and 'ttytype' point to
4524 // T_NAME. If 'term' or 'ttytype' is modified, then use the index for the
4525 // 'term' option. Only set the P_ALLOCED flag on 'term'.
4526 if (varp == &T_NAME)
4527 {
4528 opt_idx = findoption((char_u *)"term");
4529 if (opt_idx >= 0)
4530 {
4531 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
4532 did_set_cb = get_option_did_set_cb(opt_idx);
4533 }
4534 }
4535
4536 CLEAR_FIELD(args);
Bram Moolenaardac13472019-09-16 21:06:21 +02004537
Bram Moolenaardac13472019-09-16 21:06:21 +02004538 // Disallow changing some options from secure mode
4539 if ((secure
4540#ifdef HAVE_SANDBOX
4541 || sandbox != 0
4542#endif
4543 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00004544 errmsg = e_not_allowed_here;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004545 // Check for a "normal" directory or file name in some options.
4546 else if (check_illegal_path_names(opt_idx, varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004547 errmsg = e_invalid_argument;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004548 else if (did_set_cb != NULL)
4549 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004550 args.os_varp = (char_u *)varp;
4551 args.os_idx = opt_idx;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004552 args.os_flags = opt_flags;
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004553 args.os_op = op;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004554 args.os_oldval.string = oldval;
4555 args.os_newval.string = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004556 args.os_errbuf = errbuf;
Christian Brabandtb39b2402023-11-29 11:34:05 +01004557 args.os_errbuflen = errbuflen;
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004558 // Invoke the option specific callback function to validate and apply
4559 // the new option value.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004560 errmsg = did_set_cb(&args);
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004561
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004562 // The 'keymap', 'filetype' and 'syntax' option callback functions
4563 // may change the os_value_checked field.
4564 *value_checked = args.os_value_checked;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004565 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004566
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004567 // If an error is detected, restore the previous value.
Bram Moolenaardac13472019-09-16 21:06:21 +02004568 if (errmsg != NULL)
4569 {
zeertzjqf6782732022-07-27 18:26:03 +01004570 free_string_option(*varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02004571 *varp = oldval;
4572 // When resetting some values, need to act on it.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004573 if (args.os_restore_chartab)
Bram Moolenaardac13472019-09-16 21:06:21 +02004574 (void)init_chartab();
4575 if (varp == &p_hl)
4576 (void)highlight_changed();
4577 }
4578 else
4579 {
4580#ifdef FEAT_EVAL
4581 // Remember where the option was set.
4582 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4583#endif
4584 // Free string options that are in allocated memory.
4585 // Use "free_oldval", because recursiveness may change the flags under
4586 // our fingers (esp. init_highlight()).
4587 if (free_oldval)
4588 free_string_option(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01004589 set_option_flag(opt_idx, P_ALLOCED);
Bram Moolenaardac13472019-09-16 21:06:21 +02004590
4591 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
4592 && is_global_local_option(opt_idx))
4593 {
4594 // global option with local value set to use global value; free
4595 // the local value and make it empty
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004596 char_u *p = get_option_varp_scope(opt_idx, OPT_LOCAL);
Bram Moolenaardac13472019-09-16 21:06:21 +02004597 free_string_option(*(char_u **)p);
4598 *(char_u **)p = empty_option;
4599 }
4600
4601 // May set global value for local option.
4602 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
4603 set_string_option_global(opt_idx, varp);
4604
4605 // Trigger the autocommand only after setting the flags.
4606#ifdef FEAT_SYN_HL
Bram Moolenaardac13472019-09-16 21:06:21 +02004607 if (varp == &(curbuf->b_p_syn))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004608 do_syntax_autocmd(args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004609#endif
4610 else if (varp == &(curbuf->b_p_ft))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004611 do_filetype_autocmd(varp, opt_flags, args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004612#ifdef FEAT_SPELL
4613 if (varp == &(curwin->w_s->b_p_spl))
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004614 do_spelllang_source();
Bram Moolenaardac13472019-09-16 21:06:21 +02004615#endif
4616 }
4617
Bram Moolenaardac13472019-09-16 21:06:21 +02004618 if (varp == &p_mouse)
4619 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004620 if (*p_mouse == NUL)
4621 mch_setmouse(FALSE); // switch mouse off
4622 else
Bram Moolenaardac13472019-09-16 21:06:21 +02004623 setmouse(); // in case 'mouse' changed
4624 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004625
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004626#if defined(FEAT_LUA) || defined(PROTO)
4627 if (varp == &p_rtp)
4628 update_package_paths_in_lua();
4629#endif
4630
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004631#if defined(FEAT_LINEBREAK)
4632 // Changing Formatlistpattern when briopt includes the list setting:
4633 // redraw
4634 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
4635 && curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004636 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004637#endif
4638
Bram Moolenaardac13472019-09-16 21:06:21 +02004639 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004640 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0
4641 && (get_option_flags(opt_idx) & P_HLONLY) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02004642 curwin->w_set_curswant = TRUE;
4643
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004644 if ((opt_flags & OPT_NO_REDRAW) == 0)
4645 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004646#ifdef FEAT_GUI
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004647 // set when changing an option that only requires a redraw in the GUI
4648 int redraw_gui_only = FALSE;
4649
4650 if (varp == &p_go // 'guioptions'
4651 || varp == &p_guifont // 'guifont'
4652# ifdef FEAT_GUI_TABLINE
4653 || varp == &p_gtl // 'guitablabel'
4654 || varp == &p_gtt // 'guitabtooltip'
4655# endif
4656# ifdef FEAT_XFONTSET
4657 || varp == &p_guifontset // 'guifontset'
4658# endif
4659 || varp == &p_guifontwide // 'guifontwide'
Erik S. V. Jansson2f026382024-02-26 22:23:05 +01004660# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004661 || varp == &p_guiligatures // 'guiligatures'
4662# endif
4663 )
4664 redraw_gui_only = TRUE;
4665
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004666 // check redraw when it's not a GUI option or the GUI is active.
4667 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02004668#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004669 check_redraw(get_option_flags(opt_idx));
4670 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004671
4672#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004673 if (args.os_did_swaptcap)
Bram Moolenaardac13472019-09-16 21:06:21 +02004674 {
4675 set_termname((char_u *)"win32");
4676 init_highlight(TRUE, FALSE);
4677 }
4678#endif
4679
4680 return errmsg;
4681}
4682
4683/*
4684 * Check an option that can be a range of string values.
4685 *
4686 * Return OK for correct value, FAIL otherwise.
4687 * Empty is always OK.
4688 */
4689 static int
4690check_opt_strings(
4691 char_u *val,
4692 char **values,
4693 int list) // when TRUE: accept a list of values
4694{
4695 return opt_strings_flags(val, values, NULL, list);
4696}
4697
4698/*
4699 * Handle an option that can be a range of string values.
4700 * Set a flag in "*flagp" for each string present.
4701 *
4702 * Return OK for correct value, FAIL otherwise.
4703 * Empty is always OK.
4704 */
4705 static int
4706opt_strings_flags(
4707 char_u *val, // new value
4708 char **values, // array of valid string values
4709 unsigned *flagp,
4710 int list) // when TRUE: accept a list of values
4711{
4712 int i;
4713 int len;
4714 unsigned new_flags = 0;
4715
4716 while (*val)
4717 {
4718 for (i = 0; ; ++i)
4719 {
4720 if (values[i] == NULL) // val not found in values[]
4721 return FAIL;
4722
4723 len = (int)STRLEN(values[i]);
4724 if (STRNCMP(values[i], val, len) == 0
4725 && ((list && val[len] == ',') || val[len] == NUL))
4726 {
4727 val += len + (val[len] == ',');
4728 new_flags |= (1 << i);
4729 break; // check next item in val list
4730 }
4731 }
4732 }
4733 if (flagp != NULL)
4734 *flagp = new_flags;
4735
4736 return OK;
4737}
4738
4739/*
4740 * return OK if "p" is a valid fileformat name, FAIL otherwise.
4741 */
4742 int
4743check_ff_value(char_u *p)
4744{
4745 return check_opt_strings(p, p_ff_values, FALSE);
4746}
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004747
4748/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004749 * Save the actual shortmess Flags and clear them temporarily to avoid that
4750 * file messages overwrites any output from the following commands.
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004751 *
4752 * Caller must make sure to first call save_clear_shm_value() and then
4753 * restore_shm_value() exactly the same number of times.
4754 */
4755 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004756save_clear_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004757{
4758 if (STRLEN(p_shm) >= SHM_LEN)
4759 {
4760 iemsg(e_internal_error_shortmess_too_long);
4761 return;
4762 }
4763
4764 if (++set_shm_recursive == 1)
4765 {
4766 STRCPY(shm_buf, p_shm);
4767 set_option_value_give_err((char_u *)"shm", 0L, (char_u *)"", 0);
4768 }
4769}
4770
4771/*
4772 * Restore the shortmess Flags set from the save_clear_shm_value() function.
4773 */
4774 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004775restore_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004776{
4777 if (--set_shm_recursive == 0)
4778 {
4779 set_option_value_give_err((char_u *)"shm", 0L, shm_buf, 0);
4780 vim_memset(shm_buf, 0, SHM_LEN);
4781 }
4782}