blob: 4dda7a0642ce7b131b678fb42017e661aebb621d [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()
Christian Brabandt9162e632025-01-16 19:03:40 +010033static char *(p_dip_values[]) = {"filler", "context:", "iblank", "icase", "iwhite", "iwhiteall", "iwhiteeol", "horizontal", "vertical", "closeoff", "hiddenoff", "foldcolumn:", "followwrap", "internal", "indent-heuristic", "algorithm:", "linematch:", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020034static 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
glepniredd4ac32025-01-29 18:53:51 +0100123static char *(p_cot_values[]) = {"menu", "menuone", "longest", "preview", "popup", "popuphidden", "noinsert", "noselect", "fuzzy", "nosort", "preinsert", 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
Christian Brabandt51d4d842024-12-06 17:26:25 +01003043/*
3044 * Process the updated 'messagesopt' option value.
3045 */
3046 char *
3047did_set_messagesopt(optset_T *args UNUSED)
3048{
3049 if (messagesopt_changed() == FAIL)
3050 return e_invalid_argument;
3051
3052 return NULL;
3053}
3054
3055 int
3056expand_set_messagesopt(optexpand_T *args, int *numMatches, char_u ***matches)
3057{
zeertzjq8cc43da2024-12-07 16:09:08 +01003058 static char *(p_mopt_values[]) = {"hit-enter", "wait:", "history:", NULL};
Christian Brabandt51d4d842024-12-06 17:26:25 +01003059 return expand_set_opt_string(
3060 args,
zeertzjq8cc43da2024-12-07 16:09:08 +01003061 p_mopt_values,
3062 ARRAY_LENGTH(p_mopt_values) - 1,
Christian Brabandt51d4d842024-12-06 17:26:25 +01003063 numMatches,
3064 matches);
3065}
3066
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003067#if defined(FEAT_SPELL) || defined(PROTO)
3068/*
3069 * The 'mkspellmem' option is changed.
3070 */
3071 char *
3072did_set_mkspellmem(optset_T *args UNUSED)
3073{
3074 if (spell_check_msm() != OK)
3075 return e_invalid_argument;
3076
3077 return NULL;
3078}
3079#endif
3080
3081/*
3082 * The 'mouse' option is changed.
3083 */
3084 char *
3085did_set_mouse(optset_T *args)
3086{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003087 char_u **varp = (char_u **)args->os_varp;
3088
Christian Brabandtb39b2402023-11-29 11:34:05 +01003089 return did_set_option_listflag(*varp, (char_u *)MOUSE_ALL, args->os_errbuf,
3090 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003091}
3092
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003093 int
3094expand_set_mouse(optexpand_T *args, int *numMatches, char_u ***matches)
3095{
3096 return expand_set_opt_listflag(args, (char_u*)MOUSE_ALL, numMatches, matches);
3097}
3098
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003099/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003100 * The 'mousemodel' option is changed.
3101 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003102 char *
3103did_set_mousemodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003104{
3105 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
3106 return e_invalid_argument;
3107#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
Ken Takata18d0d292023-12-19 20:12:29 +01003108 else if (*p_mousem != *args->os_oldval.string)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003109 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
3110 // to create or delete the popup menus.
3111 gui_motif_update_mousemodel(root_menu);
3112#endif
3113
3114 return NULL;
3115}
3116
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003117 int
3118expand_set_mousemodel(optexpand_T *args, int *numMatches, char_u ***matches)
3119{
3120 return expand_set_opt_string(
3121 args,
3122 p_mousem_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003123 ARRAY_LENGTH(p_mousem_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003124 numMatches,
3125 matches);
3126}
3127
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003128#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3129 char *
3130did_set_mouseshape(optset_T *args UNUSED)
3131{
3132 char *errmsg = NULL;
3133
3134 errmsg = parse_shape_opt(SHAPE_MOUSE);
3135 update_mouseshape(-1);
3136
3137 return errmsg;
3138}
3139#endif
3140
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003141/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003142 * The 'nrformats' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003143 */
3144 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003145did_set_nrformats(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003146{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003147 char_u **varp = (char_u **)args->os_varp;
3148
3149 return did_set_opt_strings(*varp, p_nf_values, TRUE);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003150}
3151
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003152 int
3153expand_set_nrformats(optexpand_T *args, int *numMatches, char_u ***matches)
3154{
3155 return expand_set_opt_string(
3156 args,
3157 p_nf_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003158 ARRAY_LENGTH(p_nf_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003159 numMatches,
3160 matches);
3161}
3162
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003163#if defined(FEAT_EVAL) || defined(PROTO)
3164/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003165 * One of the '*expr' options is changed: 'balloonexpr', 'diffexpr',
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003166 * 'foldexpr', 'foldtext', 'formatexpr', 'includeexpr', 'indentexpr',
3167 * 'patchexpr', 'printexpr' and 'charconvert'.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003168 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003169 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003170did_set_optexpr(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003171{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003172 char_u **varp = (char_u **)args->os_varp;
3173
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003174 // If the option value starts with <SID> or s:, then replace that with
3175 // the script identifier.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003176 char_u *name = get_scriptlocal_funcname(*varp);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003177 if (name != NULL)
3178 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003179 free_string_option(*varp);
3180 *varp = name;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003181 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003182
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003183 return NULL;
3184}
3185#endif
3186
3187/*
3188 * The 'pastetoggle' option is changed.
3189 */
3190 char *
3191did_set_pastetoggle(optset_T *args UNUSED)
3192{
3193 char_u *p;
3194
3195 // translate key codes like in a mapping
3196 if (*p_pt)
3197 {
zeertzjq7e0bae02023-08-11 23:15:38 +02003198 (void)replace_termcodes(p_pt, &p, 0,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003199 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
3200 if (p != NULL)
3201 {
3202 free_string_option(p_pt);
3203 p_pt = p;
3204 }
3205 }
3206
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003207 return NULL;
3208}
3209
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003210#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3211/*
3212 * The 'previewpopup' option is changed.
3213 */
3214 char *
3215did_set_previewpopup(optset_T *args UNUSED)
3216{
3217 if (parse_previewpopup(NULL) == FAIL)
3218 return e_invalid_argument;
3219
3220 return NULL;
3221}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003222
3223 int
3224expand_set_popupoption(optexpand_T *args, int *numMatches, char_u ***matches)
3225{
3226 expand_T *xp = args->oe_xp;
3227
3228 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3229 {
3230 // Within "highlight:"/"border:"/"align:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003231 int border_len = (int)STRLEN("border:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003232 if (xp->xp_pattern - args->oe_set_arg >= border_len &&
3233 STRNCMP(xp->xp_pattern - border_len, "border:", border_len) == 0)
3234 {
3235 return expand_set_opt_string(
3236 args,
3237 p_popup_option_border_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003238 ARRAY_LENGTH(p_popup_option_border_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003239 numMatches,
3240 matches);
3241 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003242 int align_len = (int)STRLEN("align:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003243 if (xp->xp_pattern - args->oe_set_arg >= align_len &&
3244 STRNCMP(xp->xp_pattern - align_len, "align:", align_len) == 0)
3245 {
3246 return expand_set_opt_string(
3247 args,
3248 p_popup_option_align_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003249 ARRAY_LENGTH(p_popup_option_align_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003250 numMatches,
3251 matches);
3252 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003253 int highlight_len = (int)STRLEN("highlight:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003254 if (xp->xp_pattern - args->oe_set_arg >= highlight_len &&
3255 STRNCMP(xp->xp_pattern - highlight_len, "highlight:", highlight_len) == 0)
3256 {
3257 // Return the list of all highlight names
3258 return expand_set_opt_generic(
3259 args,
3260 get_highlight_name,
3261 numMatches,
3262 matches);
3263 }
3264 return FAIL;
3265 }
3266
3267 return expand_set_opt_string(
3268 args,
3269 p_popup_option_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003270 ARRAY_LENGTH(p_popup_option_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003271 numMatches,
3272 matches);
3273}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003274#endif
3275
3276#if defined(FEAT_POSTSCRIPT) || defined(PROTO)
3277/*
3278 * The 'printencoding' option is changed.
3279 */
3280 char *
3281did_set_printencoding(optset_T *args UNUSED)
3282{
3283 char_u *s, *p;
3284
3285 // Canonize 'printencoding' if VIM standard one
3286 p = enc_canonize(p_penc);
3287 if (p != NULL)
3288 {
3289 vim_free(p_penc);
3290 p_penc = p;
3291 }
3292 else
3293 {
3294 // Ensure lower case and '-' for '_'
3295 for (s = p_penc; *s != NUL; s++)
3296 {
3297 if (*s == '_')
3298 *s = '-';
3299 else
3300 *s = TOLOWER_ASC(*s);
3301 }
3302 }
3303
3304 return NULL;
3305}
3306#endif
3307
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003308#if defined(FEAT_PRINTER) || defined(PROTO)
3309
3310 static char_u *
3311get_printoptions_names(expand_T *xp UNUSED, int idx)
3312{
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003313 if (idx >= (int)ARRAY_LENGTH(printer_opts))
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003314 return NULL;
3315 return (char_u*)printer_opts[idx].name;
3316}
3317
3318/*
3319 * Expand 'printoptions' option
3320 */
3321 int
3322expand_set_printoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3323{
3324 return expand_set_opt_generic(
3325 args,
3326 get_printoptions_names,
3327 numMatches,
3328 matches);
3329}
3330#endif
3331
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003332#if defined(FEAT_STL_OPT) || defined(PROTO)
3333/*
3334 * The 'statusline' or the 'tabline' or the 'rulerformat' option is changed.
3335 * "rulerformat" is TRUE if the 'rulerformat' option is changed.
3336 */
3337 static char *
3338parse_statustabline_rulerformat(optset_T *args, int rulerformat)
3339{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003340 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003341 char_u *s;
3342 char *errmsg = NULL;
3343 int wid;
3344
3345 if (rulerformat) // reset ru_wid first
3346 ru_wid = 0;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003347 s = *varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003348 if (rulerformat && *s == '%')
3349 {
3350 // set ru_wid if 'ruf' starts with "%99("
3351 if (*++s == '-') // ignore a '-'
3352 s++;
3353 wid = getdigits(&s);
3354 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
3355 ru_wid = wid;
3356 else
Yegappan Lakshmananac023e82024-11-27 20:55:45 +01003357 {
3358 // Validate the flags in 'rulerformat' only if it doesn't point to
3359 // a custom function ("%!" flag).
3360 if ((*varp)[1] != '!')
3361 errmsg = check_stl_option(p_ruf);
3362 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003363 }
3364 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
3365 else if (rulerformat || s[0] != '%' || s[1] != '!')
3366 errmsg = check_stl_option(s);
3367 if (rulerformat && errmsg == NULL)
3368 comp_col();
3369
3370 return errmsg;
3371}
3372#endif
3373
3374#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
3375/*
3376 * The 'renderoptions' option is changed.
3377 */
3378 char *
3379did_set_renderoptions(optset_T *args UNUSED)
3380{
3381 if (!gui_mch_set_rendering_options(p_rop))
3382 return e_invalid_argument;
3383
3384 return NULL;
3385}
3386#endif
3387
3388#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3389/*
3390 * The 'rightleftcmd' option is changed.
3391 */
3392 char *
3393did_set_rightleftcmd(optset_T *args)
3394{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003395 char_u **varp = (char_u **)args->os_varp;
3396
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003397 // Currently only "search" is a supported value.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003398 if (**varp != NUL && STRCMP(*varp, "search") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003399 return e_invalid_argument;
3400
3401 return NULL;
3402}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003403
3404 int
3405expand_set_rightleftcmd(optexpand_T *args, int *numMatches, char_u ***matches)
3406{
3407 static char *(p_rlc_values[]) = {"search", NULL};
3408 return expand_set_opt_string(
3409 args,
3410 p_rlc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003411 ARRAY_LENGTH(p_rlc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003412 numMatches,
3413 matches);
3414}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003415#endif
3416
3417#if defined(FEAT_STL_OPT) || defined(PROTO)
3418/*
3419 * The 'rulerformat' option is changed.
3420 */
3421 char *
3422did_set_rulerformat(optset_T *args)
3423{
3424 return parse_statustabline_rulerformat(args, TRUE);
3425}
3426#endif
3427
3428/*
3429 * The 'scrollopt' option is changed.
3430 */
3431 char *
3432did_set_scrollopt(optset_T *args UNUSED)
3433{
3434 return did_set_opt_strings(p_sbo, p_scbopt_values, TRUE);
3435}
3436
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003437 int
3438expand_set_scrollopt(optexpand_T *args, int *numMatches, char_u ***matches)
3439{
3440 return expand_set_opt_string(
3441 args,
3442 p_scbopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003443 ARRAY_LENGTH(p_scbopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003444 numMatches,
3445 matches);
3446}
3447
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003448/*
3449 * The 'selection' option is changed.
3450 */
3451 char *
3452did_set_selection(optset_T *args UNUSED)
3453{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003454 if (*p_sel == NUL || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003455 return e_invalid_argument;
3456
3457 return NULL;
3458}
3459
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003460 int
3461expand_set_selection(optexpand_T *args, int *numMatches, char_u ***matches)
3462{
3463 return expand_set_opt_string(
3464 args,
3465 p_sel_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003466 ARRAY_LENGTH(p_sel_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003467 numMatches,
3468 matches);
3469}
3470
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003471/*
3472 * The 'selectmode' option is changed.
3473 */
3474 char *
3475did_set_selectmode(optset_T *args UNUSED)
3476{
3477 return did_set_opt_strings(p_slm, p_slm_values, TRUE);
3478}
3479
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003480 int
3481expand_set_selectmode(optexpand_T *args, int *numMatches, char_u ***matches)
3482{
3483 return expand_set_opt_string(
3484 args,
3485 p_slm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003486 ARRAY_LENGTH(p_slm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003487 numMatches,
3488 matches);
3489}
3490
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003491#if defined(FEAT_SESSION) || defined(PROTO)
3492/*
3493 * The 'sessionoptions' option is changed.
3494 */
3495 char *
3496did_set_sessionoptions(optset_T *args)
3497{
3498 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
3499 return e_invalid_argument;
3500 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
3501 {
3502 // Don't allow both "sesdir" and "curdir".
3503 (void)opt_strings_flags(args->os_oldval.string, p_ssop_values,
3504 &ssop_flags, TRUE);
3505 return e_invalid_argument;
3506 }
3507
3508 return NULL;
3509}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003510
3511 int
3512expand_set_sessionoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3513{
3514 return expand_set_opt_string(
3515 args,
3516 p_ssop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003517 ARRAY_LENGTH(p_ssop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003518 numMatches,
3519 matches);
3520}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003521#endif
3522
3523/*
3524 * The 'shortmess' option is changed.
3525 */
3526 char *
3527did_set_shortmess(optset_T *args)
3528{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003529 char_u **varp = (char_u **)args->os_varp;
3530
Christian Brabandtb39b2402023-11-29 11:34:05 +01003531 return did_set_option_listflag(*varp, (char_u *)SHM_ALL, args->os_errbuf,
3532 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003533}
3534
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003535 int
3536expand_set_shortmess(optexpand_T *args, int *numMatches, char_u ***matches)
3537{
3538 return expand_set_opt_listflag(args, (char_u*)SHM_ALL, numMatches, matches);
3539}
3540
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003541#if defined(FEAT_LINEBREAK) || defined(PROTO)
3542/*
3543 * The 'showbreak' option is changed.
3544 */
3545 char *
3546did_set_showbreak(optset_T *args)
3547{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003548 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003549 char_u *s;
3550
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003551 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003552 {
3553 if (ptr2cells(s) != 1)
3554 return e_showbreak_contains_unprintable_or_wide_character;
3555 MB_PTR_ADV(s);
3556 }
3557
3558 return NULL;
3559}
3560#endif
3561
3562/*
3563 * The 'showcmdloc' option is changed.
3564 */
3565 char *
3566did_set_showcmdloc(optset_T *args UNUSED)
3567{
zeertzjqc27fcf42024-03-01 23:01:43 +01003568 char *errmsg = did_set_opt_strings(p_sloc, p_sloc_values, FALSE);
3569
3570 if (errmsg == NULL)
3571 comp_col();
3572
3573 return errmsg;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003574}
3575
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003576 int
3577expand_set_showcmdloc(optexpand_T *args, int *numMatches, char_u ***matches)
3578{
3579 return expand_set_opt_string(
3580 args,
3581 p_sloc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003582 ARRAY_LENGTH(p_sloc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003583 numMatches,
3584 matches);
3585}
3586
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003587#if defined(FEAT_SIGNS) || defined(PROTO)
3588/*
3589 * The 'signcolumn' option is changed.
3590 */
3591 char *
3592did_set_signcolumn(optset_T *args)
3593{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003594 char_u **varp = (char_u **)args->os_varp;
3595
3596 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003597 return e_invalid_argument;
3598 // When changing the 'signcolumn' to or from 'number', recompute the
3599 // width of the number column if 'number' or 'relativenumber' is set.
3600 if (((*args->os_oldval.string == 'n' && args->os_oldval.string[1] == 'u')
3601 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
3602 && (curwin->w_p_nu || curwin->w_p_rnu))
3603 curwin->w_nrwidth_line_count = 0;
3604
3605 return NULL;
3606}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003607
3608 int
3609expand_set_signcolumn(optexpand_T *args, int *numMatches, char_u ***matches)
3610{
3611 return expand_set_opt_string(
3612 args,
3613 p_scl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003614 ARRAY_LENGTH(p_scl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003615 numMatches,
3616 matches);
3617}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003618#endif
3619
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003620#if defined(FEAT_SPELL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003621/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003622 * The 'spellcapcheck' option is changed.
3623 */
3624 char *
3625did_set_spellcapcheck(optset_T *args UNUSED)
3626{
3627 // compile the regexp program.
3628 return compile_cap_prog(curwin->w_s);
3629}
3630
3631/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003632 * The 'spellfile' option is changed.
3633 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003634 char *
3635did_set_spellfile(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003636{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003637 char_u **varp = (char_u **)args->os_varp;
3638
3639 if (!valid_spellfile(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003640 return e_invalid_argument;
3641
3642 // If there is a window for this buffer in which 'spell' is set load the
3643 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003644 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003645}
3646
3647/*
3648 * The 'spell' option is changed.
3649 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003650 char *
3651did_set_spelllang(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003652{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003653 char_u **varp = (char_u **)args->os_varp;
3654
3655 if (!valid_spelllang(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003656 return e_invalid_argument;
3657
3658 // If there is a window for this buffer in which 'spell' is set load the
3659 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003660 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003661}
3662
3663/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003664 * The 'spelloptions' option is changed.
3665 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003666 char *
3667did_set_spelloptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003668{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003669 char_u **varp = (char_u **)args->os_varp;
3670
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003671 if (**varp != NUL && STRCMP(*varp, "camel") != 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003672 return e_invalid_argument;
3673
3674 return NULL;
3675}
3676
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003677 int
3678expand_set_spelloptions(optexpand_T *args, int *numMatches, char_u ***matches)
3679{
3680 static char *(p_spo_values[]) = {"camel", NULL};
3681 return expand_set_opt_string(
3682 args,
3683 p_spo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003684 ARRAY_LENGTH(p_spo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003685 numMatches,
3686 matches);
3687}
3688
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003689/*
3690 * The 'spellsuggest' option is changed.
3691 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003692 char *
3693did_set_spellsuggest(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003694{
3695 if (spell_check_sps() != OK)
3696 return e_invalid_argument;
3697
3698 return NULL;
3699}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003700
3701 int
3702expand_set_spellsuggest(optexpand_T *args, int *numMatches, char_u ***matches)
3703{
3704 return expand_set_opt_string(
3705 args,
3706 p_sps_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003707 ARRAY_LENGTH(p_sps_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003708 numMatches,
3709 matches);
3710}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003711#endif
3712
3713/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003714 * The 'splitkeep' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003715 */
3716 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003717did_set_splitkeep(optset_T *args UNUSED)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003718{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003719 return did_set_opt_strings(p_spk, p_spk_values, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003720}
3721
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003722 int
3723expand_set_splitkeep(optexpand_T *args, int *numMatches, char_u ***matches)
3724{
3725 return expand_set_opt_string(
3726 args,
3727 p_spk_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003728 ARRAY_LENGTH(p_spk_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003729 numMatches,
3730 matches);
3731}
3732
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00003733#if defined(FEAT_STL_OPT) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003734/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003735 * The 'statusline' option is changed.
3736 */
3737 char *
3738did_set_statusline(optset_T *args)
3739{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003740 return parse_statustabline_rulerformat(args, FALSE);
3741}
3742#endif
3743
3744/*
3745 * The 'swapsync' option is changed.
3746 */
3747 char *
3748did_set_swapsync(optset_T *args UNUSED)
3749{
3750 return did_set_opt_strings(p_sws, p_sws_values, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003751}
3752
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003753 int
3754expand_set_swapsync(optexpand_T *args, int *numMatches, char_u ***matches)
3755{
3756 return expand_set_opt_string(
3757 args,
3758 p_sws_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003759 ARRAY_LENGTH(p_sws_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003760 numMatches,
3761 matches);
3762}
3763
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003764/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003765 * The 'switchbuf' option is changed.
3766 */
3767 char *
3768did_set_switchbuf(optset_T *args UNUSED)
3769{
3770 return did_set_opt_flags(p_swb, p_swb_values, &swb_flags, TRUE);
3771}
3772
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003773 int
3774expand_set_switchbuf(optexpand_T *args, int *numMatches, char_u ***matches)
3775{
3776 return expand_set_opt_string(
3777 args,
3778 p_swb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003779 ARRAY_LENGTH(p_swb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003780 numMatches,
3781 matches);
3782}
3783
LemonBoy5247b0b2024-07-12 19:30:58 +02003784/*
3785 * The 'tabclose' option is changed.
3786 */
3787 char *
3788did_set_tabclose(optset_T *args UNUSED)
3789{
3790 return did_set_opt_flags(p_tcl, p_tcl_values, &tcl_flags, TRUE);
3791}
3792
3793 int
3794expand_set_tabclose(optexpand_T *args, int *numMatches, char_u ***matches)
3795{
3796 return expand_set_opt_string(
3797 args,
3798 p_tcl_values,
3799 ARRAY_LENGTH(p_tcl_values) - 1,
3800 numMatches,
3801 matches);
3802}
3803
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003804#if defined(FEAT_STL_OPT) || defined(PROTO)
3805/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003806 * The 'tabline' option is changed.
3807 */
3808 char *
3809did_set_tabline(optset_T *args)
3810{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003811 return parse_statustabline_rulerformat(args, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003812}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003813#endif
3814
3815/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003816 * The 'tagcase' option is changed.
3817 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003818 char *
3819did_set_tagcase(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003820{
3821 unsigned int *flags;
3822 char_u *p;
3823
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003824 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003825 {
3826 p = curbuf->b_p_tc;
3827 flags = &curbuf->b_tc_flags;
3828 }
3829 else
3830 {
3831 p = p_tc;
3832 flags = &tc_flags;
3833 }
3834
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003835 if ((args->os_flags & OPT_LOCAL) && *p == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003836 // make the local value empty: use the global value
3837 *flags = 0;
3838 else if (*p == NUL
3839 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
3840 return e_invalid_argument;
3841
3842 return NULL;
3843}
3844
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003845 int
3846expand_set_tagcase(optexpand_T *args, int *numMatches, char_u ***matches)
3847{
3848 return expand_set_opt_string(
3849 args,
3850 p_tc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003851 ARRAY_LENGTH(p_tc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003852 numMatches,
3853 matches);
3854}
3855
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003856/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003857 * The 'term' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003858 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003859 char *
3860did_set_term(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003861{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003862 if (T_NAME[0] == NUL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003863 return e_cannot_set_term_to_empty_string;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003864#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003865 if (gui.in_use)
3866 return e_cannot_change_term_in_GUI;
3867 if (term_is_gui(T_NAME))
3868 return e_use_gui_to_start_GUI;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003869#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003870 if (set_termname(T_NAME) == FAIL)
3871 return e_not_found_in_termcap;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003872
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003873 // Screen colors may have changed.
3874 redraw_later_clear();
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003875
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003876 return NULL;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003877}
3878
3879/*
3880 * Some terminal option (t_xxx) is changed
3881 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003882 char *
3883did_set_term_option(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003884{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003885 char_u **varp = (char_u **)args->os_varp;
3886
3887 if (!full_screen)
3888 return NULL;
3889
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003890 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
3891 if (varp == &T_CCO)
3892 {
3893 int colors = atoi((char *)T_CCO);
3894
3895 // Only reinitialize colors if t_Co value has really changed to
3896 // avoid expensive reload of colorscheme if t_Co is set to the
3897 // same value multiple times.
3898 if (colors != t_colors)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003899 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003900 t_colors = colors;
3901 if (t_colors <= 1)
3902 {
3903 vim_free(T_CCO);
3904 T_CCO = empty_option;
3905 }
3906#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3907 if (is_term_win32())
3908 {
3909 swap_tcap();
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003910 args->os_did_swaptcap = TRUE;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003911 }
3912#endif
3913 // We now have a different color setup, initialize it again.
3914 init_highlight(TRUE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003915 }
3916 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003917 ttest(FALSE);
3918 if (varp == &T_ME)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003919 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003920 out_str(T_ME);
3921 redraw_later(UPD_CLEAR);
3922#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3923 // Since t_me has been set, this probably means that the user
3924 // wants to use this as default colors. Need to reset default
3925 // background/foreground colors.
3926# ifdef VIMDLL
3927 if (!gui.in_use && !gui.starting)
3928# endif
3929 mch_set_normal_colors();
3930#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003931 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003932 if (varp == &T_BE && termcap_active)
3933 {
3934 MAY_WANT_TO_LOG_THIS;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003935
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003936 if (*T_BE == NUL)
3937 // When clearing t_BE we assume the user no longer wants
3938 // bracketed paste, thus disable it by writing t_BD.
3939 out_str(T_BD);
3940 else
3941 out_str(T_BE);
3942 }
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003943
3944 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003945}
3946
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003947#if defined(FEAT_TERMINAL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003948/*
3949 * The 'termwinkey' option is changed.
3950 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003951 char *
Milly94606f72024-10-22 22:07:52 +02003952did_set_termwinkey(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003953{
Milly94606f72024-10-22 22:07:52 +02003954 char_u **varp = (char_u **)args->os_varp;
3955
3956 if ((*varp)[0] != NUL && string_to_key(*varp, TRUE) == 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003957 return e_invalid_argument;
3958
3959 return NULL;
3960}
3961
3962/*
3963 * The 'termwinsize' option is changed.
3964 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003965 char *
Milly8be10aa2024-10-22 22:01:46 +02003966did_set_termwinsize(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003967{
Milly8be10aa2024-10-22 22:01:46 +02003968 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003969 char_u *p;
3970
Milly8be10aa2024-10-22 22:01:46 +02003971 if ((*varp)[0] == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003972 return NULL;
3973
Milly8be10aa2024-10-22 22:01:46 +02003974 p = skipdigits(*varp);
3975 if (p == *varp || (*p != 'x' && *p != '*') || *skipdigits(p + 1) != NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003976 return e_invalid_argument;
3977
3978 return NULL;
3979}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003980
3981# if defined(MSWIN) || defined(PROTO)
3982/*
3983 * The 'termwintype' option is changed.
3984 */
3985 char *
3986did_set_termwintype(optset_T *args UNUSED)
3987{
3988 return did_set_opt_strings(p_twt, p_twt_values, FALSE);
3989}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003990
3991 int
3992expand_set_termwintype(optexpand_T *args, int *numMatches, char_u ***matches)
3993{
3994 return expand_set_opt_string(
3995 args,
3996 p_twt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003997 ARRAY_LENGTH(p_twt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003998 numMatches,
3999 matches);
4000}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00004001# endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004002#endif
4003
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004004/*
4005 * The 'titlestring' option is changed.
4006 */
4007 char *
4008did_set_titlestring(optset_T *args)
4009{
4010 int flagval = 0;
4011
4012#ifdef FEAT_STL_OPT
4013 flagval = STL_IN_TITLE;
4014#endif
4015 return parse_titleiconstring(args, flagval);
4016}
4017
4018#if (defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)) || defined(PROTO)
4019/*
4020 * The 'toolbar' option is changed.
4021 */
4022 char *
4023did_set_toolbar(optset_T *args UNUSED)
4024{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004025 if (opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags,
4026 TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004027 return e_invalid_argument;
4028
4029 out_flush();
4030 gui_mch_show_toolbar((toolbar_flags &
4031 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4032 return NULL;
4033}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004034
4035 int
4036expand_set_toolbar(optexpand_T *args, int *numMatches, char_u ***matches)
4037{
4038 return expand_set_opt_string(
4039 args,
4040 p_toolbar_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004041 ARRAY_LENGTH(p_toolbar_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004042 numMatches,
4043 matches);
4044}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004045#endif
4046
4047#if (defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)) || defined(PROTO)
4048/*
4049 * The 'toolbariconsize' option is changed. GTK+ 2 only.
4050 */
4051 char *
4052did_set_toolbariconsize(optset_T *args UNUSED)
4053{
4054 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
4055 return e_invalid_argument;
4056
4057 out_flush();
4058 gui_mch_show_toolbar((toolbar_flags &
4059 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4060 return NULL;
4061}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004062
4063 int
4064expand_set_toolbariconsize(optexpand_T *args, int *numMatches, char_u ***matches)
4065{
4066 return expand_set_opt_string(
4067 args,
4068 p_tbis_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004069 ARRAY_LENGTH(p_tbis_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004070 numMatches,
4071 matches);
4072}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004073#endif
4074
4075#if defined(UNIX) || defined(VMS) || defined(PROTO)
4076/*
4077 * The 'ttymouse' option is changed.
4078 */
4079 char *
4080did_set_ttymouse(optset_T *args UNUSED)
4081{
4082 char *errmsg = NULL;
4083
4084 // Switch the mouse off before changing the escape sequences used for
4085 // that.
4086 mch_setmouse(FALSE);
4087 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
4088 errmsg = e_invalid_argument;
4089 else
4090 check_mouse_termcode();
4091 if (termcap_active)
4092 setmouse(); // may switch it on again
4093
4094 return errmsg;
4095}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004096
4097 int
4098expand_set_ttymouse(optexpand_T *args, int *numMatches, char_u ***matches)
4099{
4100 return expand_set_opt_string(
4101 args,
4102 p_ttym_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004103 ARRAY_LENGTH(p_ttym_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004104 numMatches,
4105 matches);
4106}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004107#endif
4108
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004109#if defined(FEAT_VARTABS) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004110/*
4111 * The 'varsofttabstop' option is changed.
4112 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004113 char *
4114did_set_varsofttabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004115{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004116 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004117 char_u *cp;
4118
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004119 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004120 VIM_CLEAR(curbuf->b_p_vsts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004121 else
4122 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004123 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004124 {
4125 if (vim_isdigit(*cp))
4126 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004127 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004128 continue;
4129 return e_invalid_argument;
4130 }
4131
4132 int *oldarray = curbuf->b_p_vsts_array;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004133 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004134 {
4135 if (oldarray)
4136 vim_free(oldarray);
4137 }
4138 else
4139 return e_invalid_argument;
4140 }
4141
4142 return NULL;
4143}
4144
4145/*
4146 * The 'vartabstop' option is changed.
4147 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004148 char *
4149did_set_vartabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004150{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004151 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004152 char_u *cp;
4153
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004154 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004155 VIM_CLEAR(curbuf->b_p_vts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004156 else
4157 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004158 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004159 {
4160 if (vim_isdigit(*cp))
4161 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004162 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004163 continue;
4164 return e_invalid_argument;
4165 }
4166
4167 int *oldarray = curbuf->b_p_vts_array;
4168
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004169 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004170 {
4171 vim_free(oldarray);
4172# ifdef FEAT_FOLDING
4173 if (foldmethodIsIndent(curwin))
4174 foldUpdateAll(curwin);
4175# endif
4176 }
4177 else
4178 return e_invalid_argument;
4179 }
4180
4181 return NULL;
4182}
4183#endif
4184
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004185/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004186 * The 'verbosefile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004187 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004188 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004189did_set_verbosefile(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004190{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004191 verbose_stop();
4192 if (*p_vfile != NUL && verbose_open() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004193 return e_invalid_argument;
4194
4195 return NULL;
4196}
4197
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004198#if defined(FEAT_SESSION) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004199/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004200 * The 'viewoptions' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004201 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004202 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004203did_set_viewoptions(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004204{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004205 return did_set_opt_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004206}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004207#endif
4208
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004209#if defined(FEAT_VIMINFO) || defined(PROTO)
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004210/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004211 * The 'viminfo' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004212 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004213 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004214did_set_viminfo(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004215{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004216 char_u *s;
4217 char *errmsg = NULL;
4218
4219 for (s = p_viminfo; *s;)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004220 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004221 // Check it's a valid character
4222 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
4223 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01004224 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004225 break;
4226 }
4227 if (*s == 'n') // name is always last one
4228 break;
4229 else if (*s == 'r') // skip until next ','
4230 {
4231 while (*++s && *s != ',')
4232 ;
4233 }
4234 else if (*s == '%')
4235 {
4236 // optional number
4237 while (vim_isdigit(*++s))
4238 ;
4239 }
4240 else if (*s == '!' || *s == 'h' || *s == 'c')
4241 ++s; // no extra chars
4242 else // must have a number
4243 {
4244 while (vim_isdigit(*++s))
4245 ;
4246
4247 if (!VIM_ISDIGIT(*(s - 1)))
4248 {
4249 if (args->os_errbuf != NULL)
4250 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01004251 vim_snprintf(args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004252 _(e_missing_number_after_angle_str_angle),
4253 transchar_byte(*(s - 1)));
4254 errmsg = args->os_errbuf;
4255 }
4256 else
4257 errmsg = "";
4258 break;
4259 }
4260 }
4261 if (*s == ',')
4262 ++s;
4263 else if (*s)
4264 {
4265 if (args->os_errbuf != NULL)
4266 errmsg = e_missing_comma;
4267 else
4268 errmsg = "";
4269 break;
4270 }
4271 }
4272 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
4273 errmsg = e_must_specify_a_value;
4274
4275 return errmsg;
4276}
4277#endif
4278
4279/*
4280 * The 'virtualedit' option is changed.
4281 */
4282 char *
4283did_set_virtualedit(optset_T *args)
4284{
4285 char_u *ve = p_ve;
4286 unsigned int *flags = &ve_flags;
4287
4288 if (args->os_flags & OPT_LOCAL)
4289 {
4290 ve = curwin->w_p_ve;
4291 flags = &curwin->w_ve_flags;
4292 }
4293
4294 if ((args->os_flags & OPT_LOCAL) && *ve == NUL)
4295 // make the local value empty: use the global value
4296 *flags = 0;
4297 else
4298 {
4299 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
4300 return e_invalid_argument;
4301 else if (STRCMP(ve, args->os_oldval.string) != 0)
4302 {
4303 // Recompute cursor position in case the new 've' setting
4304 // changes something.
4305 validate_virtcol();
4306 coladvance(curwin->w_virtcol);
4307 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004308 }
4309
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004310 return NULL;
4311}
4312
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004313 int
4314expand_set_virtualedit(optexpand_T *args, int *numMatches, char_u ***matches)
4315{
4316 return expand_set_opt_string(
4317 args,
4318 p_ve_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004319 ARRAY_LENGTH(p_ve_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004320 numMatches,
4321 matches);
4322}
4323
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004324/*
4325 * The 'whichwrap' option is changed.
4326 */
4327 char *
4328did_set_whichwrap(optset_T *args)
4329{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004330 char_u **varp = (char_u **)args->os_varp;
4331
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004332 // Add ',' to the list flags because 'whichwrap' is a flag
4333 // list that is comma-separated.
Christian Brabandtb39b2402023-11-29 11:34:05 +01004334 return did_set_option_listflag(*varp, (char_u *)(WW_ALL ","),
4335 args->os_errbuf, args->os_errbuflen);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004336}
4337
4338 int
4339expand_set_whichwrap(optexpand_T *args, int *numMatches, char_u ***matches)
4340{
4341 return expand_set_opt_listflag(args, (char_u*)WW_ALL, numMatches, matches);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004342}
4343
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004344/*
4345 * The 'wildmode' option is changed.
4346 */
4347 char *
4348did_set_wildmode(optset_T *args UNUSED)
4349{
4350 if (check_opt_wim() == FAIL)
4351 return e_invalid_argument;
4352 return NULL;
4353}
4354
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004355 int
4356expand_set_wildmode(optexpand_T *args, int *numMatches, char_u ***matches)
4357{
4358 return expand_set_opt_string(
4359 args,
4360 p_wim_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004361 ARRAY_LENGTH(p_wim_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004362 numMatches,
4363 matches);
4364}
4365
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004366/*
4367 * The 'wildoptions' option is changed.
4368 */
4369 char *
4370did_set_wildoptions(optset_T *args UNUSED)
4371{
4372 return did_set_opt_strings(p_wop, p_wop_values, TRUE);
4373}
4374
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004375 int
4376expand_set_wildoptions(optexpand_T *args, int *numMatches, char_u ***matches)
4377{
4378 return expand_set_opt_string(
4379 args,
4380 p_wop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004381 ARRAY_LENGTH(p_wop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004382 numMatches,
4383 matches);
4384}
4385
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004386#if defined(FEAT_WAK) || defined(PROTO)
4387/*
4388 * The 'winaltkeys' option is changed.
4389 */
4390 char *
4391did_set_winaltkeys(optset_T *args UNUSED)
4392{
4393 char *errmsg = NULL;
4394
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004395 if (*p_wak == NUL || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004396 errmsg = e_invalid_argument;
4397# ifdef FEAT_MENU
4398# if defined(FEAT_GUI_MOTIF)
4399 else if (gui.in_use)
4400 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4401# elif defined(FEAT_GUI_GTK)
4402 else if (gui.in_use)
4403 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4404# endif
4405# endif
4406 return errmsg;
4407}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004408
4409 int
4410expand_set_winaltkeys(optexpand_T *args, int *numMatches, char_u ***matches)
4411{
4412 return expand_set_opt_string(
4413 args,
4414 p_wak_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004415 ARRAY_LENGTH(p_wak_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004416 numMatches,
4417 matches);
4418}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004419#endif
4420
4421/*
4422 * The 'wincolor' option is changed.
4423 */
4424 char *
4425did_set_wincolor(optset_T *args UNUSED)
4426{
4427#ifdef FEAT_TERMINAL
4428 term_update_wincolor(curwin);
4429#endif
4430 return NULL;
4431}
4432
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004433 int
4434expand_set_wincolor(optexpand_T *args, int *numMatches, char_u ***matches)
4435{
4436 return expand_set_opt_generic(
4437 args,
4438 get_highlight_name,
4439 numMatches,
4440 matches);
4441}
4442
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004443#ifdef FEAT_SYN_HL
4444/*
4445 * When the 'syntax' option is set, load the syntax of that name.
4446 */
4447 static void
4448do_syntax_autocmd(int value_changed)
4449{
4450 static int syn_recursive = 0;
4451
4452 ++syn_recursive;
4453 // Only pass TRUE for "force" when the value changed or not used
4454 // recursively, to avoid endless recurrence.
4455 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
4456 value_changed || syn_recursive == 1, curbuf);
4457 curbuf->b_flags |= BF_SYN_SET;
4458 --syn_recursive;
4459}
4460#endif
4461
4462/*
4463 * When the 'filetype' option is set, trigger the FileType autocommand.
4464 */
4465 static void
4466do_filetype_autocmd(char_u **varp, int opt_flags, int value_changed)
4467{
4468 // Skip this when called from a modeline and the filetype was already set
4469 // to this value.
4470 if ((opt_flags & OPT_MODELINE) && !value_changed)
4471 return;
4472
4473 static int ft_recursive = 0;
4474 int secure_save = secure;
4475
4476 // Reset the secure flag, since the value of 'filetype' has
4477 // been checked to be safe.
4478 secure = 0;
4479
4480 ++ft_recursive;
zeertzjq5bf6c212024-03-31 18:41:27 +02004481 curbuf->b_did_filetype = TRUE;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004482 // Only pass TRUE for "force" when the value changed or not
4483 // used recursively, to avoid endless recurrence.
4484 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
4485 value_changed || ft_recursive == 1, curbuf);
4486 --ft_recursive;
4487 // Just in case the old "curbuf" is now invalid.
4488 if (varp != &(curbuf->b_p_ft))
4489 varp = NULL;
4490
4491 secure = secure_save;
4492}
4493
4494#ifdef FEAT_SPELL
4495/*
4496 * When the 'spelllang' option is set, source the spell/LANG.vim file in
4497 * 'runtimepath'.
4498 */
4499 static void
4500do_spelllang_source(void)
4501{
4502 char_u fname[200];
4503 char_u *p;
4504 char_u *q = curwin->w_s->b_p_spl;
4505
4506 // Skip the first name if it is "cjk".
4507 if (STRNCMP(q, "cjk,", 4) == 0)
4508 q += 4;
4509
4510 // They could set 'spellcapcheck' depending on the language. Use the first
4511 // name in 'spelllang' up to '_region' or '.encoding'.
4512 for (p = q; *p != NUL; ++p)
4513 if (!ASCII_ISALNUM(*p) && *p != '-')
4514 break;
4515 if (p > q)
4516 {
4517 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
4518 (int)(p - q), q);
4519 source_runtime(fname, DIP_ALL);
4520 }
4521}
4522#endif
4523
4524/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004525 * Handle string options that need some action to perform when changed.
zeertzjqf6782732022-07-27 18:26:03 +01004526 * The new value must be allocated.
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004527 * Returns NULL for success, or an untranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +02004528 */
4529 char *
4530did_set_string_option(
4531 int opt_idx, // index in options[] table
4532 char_u **varp, // pointer to the option variable
Bram Moolenaardac13472019-09-16 21:06:21 +02004533 char_u *oldval, // previous value of the option
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004534 char_u *value, // new value of the option
Bram Moolenaardac13472019-09-16 21:06:21 +02004535 char *errbuf, // buffer for errors, or NULL
Mike Williams620f0112023-12-05 15:36:06 +01004536 size_t errbuflen, // length of error buffer
Bram Moolenaardac13472019-09-16 21:06:21 +02004537 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004538 set_op_T op, // OP_ADDING/OP_PREPENDING/OP_REMOVING
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004539 int *value_checked) // value was checked to be safe, no
Bram Moolenaardac13472019-09-16 21:06:21 +02004540 // need to set P_INSECURE
4541{
4542 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +02004543 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004544 opt_did_set_cb_T did_set_cb = get_option_did_set_cb(opt_idx);
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004545 optset_T args;
4546
4547 // 'ttytype' is an alias for 'term'. Both 'term' and 'ttytype' point to
4548 // T_NAME. If 'term' or 'ttytype' is modified, then use the index for the
4549 // 'term' option. Only set the P_ALLOCED flag on 'term'.
4550 if (varp == &T_NAME)
4551 {
4552 opt_idx = findoption((char_u *)"term");
4553 if (opt_idx >= 0)
4554 {
4555 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
4556 did_set_cb = get_option_did_set_cb(opt_idx);
4557 }
4558 }
4559
4560 CLEAR_FIELD(args);
Bram Moolenaardac13472019-09-16 21:06:21 +02004561
Bram Moolenaardac13472019-09-16 21:06:21 +02004562 // Disallow changing some options from secure mode
4563 if ((secure
4564#ifdef HAVE_SANDBOX
4565 || sandbox != 0
4566#endif
4567 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00004568 errmsg = e_not_allowed_here;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004569 // Check for a "normal" directory or file name in some options.
4570 else if (check_illegal_path_names(opt_idx, varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004571 errmsg = e_invalid_argument;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004572 else if (did_set_cb != NULL)
4573 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004574 args.os_varp = (char_u *)varp;
4575 args.os_idx = opt_idx;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004576 args.os_flags = opt_flags;
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004577 args.os_op = op;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004578 args.os_oldval.string = oldval;
4579 args.os_newval.string = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004580 args.os_errbuf = errbuf;
Christian Brabandtb39b2402023-11-29 11:34:05 +01004581 args.os_errbuflen = errbuflen;
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004582 // Invoke the option specific callback function to validate and apply
4583 // the new option value.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004584 errmsg = did_set_cb(&args);
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004585
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004586 // The 'keymap', 'filetype' and 'syntax' option callback functions
4587 // may change the os_value_checked field.
4588 *value_checked = args.os_value_checked;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004589 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004590
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004591 // If an error is detected, restore the previous value.
Bram Moolenaardac13472019-09-16 21:06:21 +02004592 if (errmsg != NULL)
4593 {
zeertzjqf6782732022-07-27 18:26:03 +01004594 free_string_option(*varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02004595 *varp = oldval;
4596 // When resetting some values, need to act on it.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004597 if (args.os_restore_chartab)
Bram Moolenaardac13472019-09-16 21:06:21 +02004598 (void)init_chartab();
4599 if (varp == &p_hl)
4600 (void)highlight_changed();
4601 }
4602 else
4603 {
4604#ifdef FEAT_EVAL
4605 // Remember where the option was set.
4606 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4607#endif
4608 // Free string options that are in allocated memory.
4609 // Use "free_oldval", because recursiveness may change the flags under
4610 // our fingers (esp. init_highlight()).
4611 if (free_oldval)
4612 free_string_option(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01004613 set_option_flag(opt_idx, P_ALLOCED);
Bram Moolenaardac13472019-09-16 21:06:21 +02004614
4615 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
4616 && is_global_local_option(opt_idx))
4617 {
4618 // global option with local value set to use global value; free
4619 // the local value and make it empty
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004620 char_u *p = get_option_varp_scope(opt_idx, OPT_LOCAL);
Bram Moolenaardac13472019-09-16 21:06:21 +02004621 free_string_option(*(char_u **)p);
4622 *(char_u **)p = empty_option;
4623 }
4624
4625 // May set global value for local option.
4626 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
4627 set_string_option_global(opt_idx, varp);
4628
4629 // Trigger the autocommand only after setting the flags.
4630#ifdef FEAT_SYN_HL
Bram Moolenaardac13472019-09-16 21:06:21 +02004631 if (varp == &(curbuf->b_p_syn))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004632 do_syntax_autocmd(args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004633#endif
4634 else if (varp == &(curbuf->b_p_ft))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004635 do_filetype_autocmd(varp, opt_flags, args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004636#ifdef FEAT_SPELL
4637 if (varp == &(curwin->w_s->b_p_spl))
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004638 do_spelllang_source();
Bram Moolenaardac13472019-09-16 21:06:21 +02004639#endif
4640 }
4641
Bram Moolenaardac13472019-09-16 21:06:21 +02004642 if (varp == &p_mouse)
4643 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004644 if (*p_mouse == NUL)
4645 mch_setmouse(FALSE); // switch mouse off
4646 else
Bram Moolenaardac13472019-09-16 21:06:21 +02004647 setmouse(); // in case 'mouse' changed
4648 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004649
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004650#if defined(FEAT_LUA) || defined(PROTO)
4651 if (varp == &p_rtp)
4652 update_package_paths_in_lua();
4653#endif
4654
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004655#if defined(FEAT_LINEBREAK)
4656 // Changing Formatlistpattern when briopt includes the list setting:
4657 // redraw
4658 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
4659 && curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004660 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004661#endif
4662
Bram Moolenaardac13472019-09-16 21:06:21 +02004663 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004664 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0
4665 && (get_option_flags(opt_idx) & P_HLONLY) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02004666 curwin->w_set_curswant = TRUE;
4667
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004668 if ((opt_flags & OPT_NO_REDRAW) == 0)
4669 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004670#ifdef FEAT_GUI
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004671 // set when changing an option that only requires a redraw in the GUI
4672 int redraw_gui_only = FALSE;
4673
4674 if (varp == &p_go // 'guioptions'
4675 || varp == &p_guifont // 'guifont'
4676# ifdef FEAT_GUI_TABLINE
4677 || varp == &p_gtl // 'guitablabel'
4678 || varp == &p_gtt // 'guitabtooltip'
4679# endif
4680# ifdef FEAT_XFONTSET
4681 || varp == &p_guifontset // 'guifontset'
4682# endif
4683 || varp == &p_guifontwide // 'guifontwide'
Erik S. V. Jansson2f026382024-02-26 22:23:05 +01004684# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004685 || varp == &p_guiligatures // 'guiligatures'
4686# endif
4687 )
4688 redraw_gui_only = TRUE;
4689
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004690 // check redraw when it's not a GUI option or the GUI is active.
4691 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02004692#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004693 check_redraw(get_option_flags(opt_idx));
4694 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004695
4696#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004697 if (args.os_did_swaptcap)
Bram Moolenaardac13472019-09-16 21:06:21 +02004698 {
4699 set_termname((char_u *)"win32");
4700 init_highlight(TRUE, FALSE);
4701 }
4702#endif
4703
4704 return errmsg;
4705}
4706
4707/*
4708 * Check an option that can be a range of string values.
4709 *
4710 * Return OK for correct value, FAIL otherwise.
4711 * Empty is always OK.
4712 */
4713 static int
4714check_opt_strings(
4715 char_u *val,
4716 char **values,
4717 int list) // when TRUE: accept a list of values
4718{
4719 return opt_strings_flags(val, values, NULL, list);
4720}
4721
4722/*
4723 * Handle an option that can be a range of string values.
4724 * Set a flag in "*flagp" for each string present.
4725 *
4726 * Return OK for correct value, FAIL otherwise.
4727 * Empty is always OK.
4728 */
4729 static int
4730opt_strings_flags(
4731 char_u *val, // new value
4732 char **values, // array of valid string values
4733 unsigned *flagp,
4734 int list) // when TRUE: accept a list of values
4735{
4736 int i;
4737 int len;
4738 unsigned new_flags = 0;
4739
4740 while (*val)
4741 {
4742 for (i = 0; ; ++i)
4743 {
4744 if (values[i] == NULL) // val not found in values[]
4745 return FAIL;
4746
4747 len = (int)STRLEN(values[i]);
4748 if (STRNCMP(values[i], val, len) == 0
4749 && ((list && val[len] == ',') || val[len] == NUL))
4750 {
4751 val += len + (val[len] == ',');
4752 new_flags |= (1 << i);
4753 break; // check next item in val list
4754 }
4755 }
4756 }
4757 if (flagp != NULL)
4758 *flagp = new_flags;
4759
4760 return OK;
4761}
4762
4763/*
4764 * return OK if "p" is a valid fileformat name, FAIL otherwise.
4765 */
4766 int
4767check_ff_value(char_u *p)
4768{
4769 return check_opt_strings(p, p_ff_values, FALSE);
4770}
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004771
4772/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004773 * Save the actual shortmess Flags and clear them temporarily to avoid that
4774 * file messages overwrites any output from the following commands.
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004775 *
4776 * Caller must make sure to first call save_clear_shm_value() and then
4777 * restore_shm_value() exactly the same number of times.
4778 */
4779 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004780save_clear_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004781{
4782 if (STRLEN(p_shm) >= SHM_LEN)
4783 {
4784 iemsg(e_internal_error_shortmess_too_long);
4785 return;
4786 }
4787
4788 if (++set_shm_recursive == 1)
4789 {
4790 STRCPY(shm_buf, p_shm);
4791 set_option_value_give_err((char_u *)"shm", 0L, (char_u *)"", 0);
4792 }
4793}
4794
4795/*
4796 * Restore the shortmess Flags set from the save_clear_shm_value() function.
4797 */
4798 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004799restore_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004800{
4801 if (--set_shm_recursive == 0)
4802 {
4803 set_option_value_give_err((char_u *)"shm", 0L, shm_buf, 0);
4804 vim_memset(shm_buf, 0, SHM_LEN);
4805 }
4806}