blob: 6b59b68085671b06f86dd9a06e0ffb0bc07e26fa [file] [log] [blame]
Bram Moolenaardac13472019-09-16 21:06:21 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * optionstr.c: Functions related to string options
12 */
13
14#include "vim.h"
15
Christian Brabandt9aee8ec2022-12-16 16:41:23 +000016static char_u shm_buf[SHM_LEN];
17static int set_shm_recursive = 0;
18
Bram Moolenaardac13472019-09-16 21:06:21 +020019static char *(p_ambw_values[]) = {"single", "double", NULL};
20static char *(p_bg_values[]) = {"light", "dark", NULL};
21static char *(p_bkc_values[]) = {"yes", "auto", "no", "breaksymlink", "breakhardlink", NULL};
22static char *(p_bo_values[]) = {"all", "backspace", "cursor", "complete",
23 "copy", "ctrlg", "error", "esc", "ex",
24 "hangul", "insertmode", "lang", "mess",
25 "showmatch", "operator", "register", "shell",
LemonBoy77771d32022-04-13 11:47:25 +010026 "spell", "term", "wildmode", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020027#if defined(FEAT_LINEBREAK)
28// Note: Keep this in sync with briopt_check()
29static char *(p_briopt_values[]) = {"shift:", "min:", "sbr", "list:", "column:", NULL};
30#endif
31#if defined(FEAT_DIFF)
32// Note: Keep this in sync with diffopt_changed()
33static char *(p_dip_values[]) = {"filler", "context:", "iblank", "icase", "iwhite", "iwhiteall", "iwhiteeol", "horizontal", "vertical", "closeoff", "hiddenoff", "foldcolumn:", "followwrap", "internal", "indent-heuristic", "algorithm:", NULL};
34static char *(p_dip_algorithm_values[]) = {"myers", "minimal", "patience", "histogram", NULL};
35#endif
Bram Moolenaaraaad9952020-05-31 15:08:59 +020036static char *(p_nf_values[]) = {"bin", "octal", "hex", "alpha", "unsigned", 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};
84#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
85static char *(p_toolbar_values[]) = {"text", "icons", "tooltips", "horiz", NULL};
86#endif
87#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
88static char *(p_tbis_values[]) = {"tiny", "small", "medium", "large", "huge", "giant", NULL};
89#endif
Bram Moolenaara1cb1d12019-10-17 23:00:07 +020090#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +020091static char *(p_ttym_values[]) = {"xterm", "xterm2", "dec", "netterm", "jsbterm", "pterm", "urxvt", "sgr", NULL};
92#endif
Gary Johnson53ba05b2021-07-26 22:19:10 +020093static char *(p_ve_values[]) = {"block", "insert", "all", "onemore", "none", "NONE", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020094// Note: Keep this in sync with check_opt_wim()
95static char *(p_wim_values[]) = {"full", "longest", "list", "lastused", NULL};
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000096static char *(p_wop_values[]) = {"fuzzy", "tagfile", "pum", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020097#ifdef FEAT_WAK
98static char *(p_wak_values[]) = {"yes", "menu", "no", NULL};
99#endif
100static char *(p_mousem_values[]) = {"extend", "popup", "popup_setpos", "mac", NULL};
101static char *(p_sel_values[]) = {"inclusive", "exclusive", "old", NULL};
102static char *(p_slm_values[]) = {"mouse", "key", "cmd", NULL};
103static char *(p_km_values[]) = {"startsel", "stopsel", NULL};
104#ifdef FEAT_BROWSE
105static char *(p_bsdir_values[]) = {"current", "last", "buffer", NULL};
106#endif
107static char *(p_scbopt_values[]) = {"ver", "hor", "jump", NULL};
108static char *(p_debug_values[]) = {"msg", "throw", "beep", NULL};
109static char *(p_ead_values[]) = {"both", "ver", "hor", NULL};
110static char *(p_buftype_values[]) = {"nofile", "nowrite", "quickfix", "help", "terminal", "acwrite", "prompt", "popup", NULL};
111static char *(p_bufhidden_values[]) = {"hide", "unload", "delete", "wipe", NULL};
Bram Moolenaaraa0489e2020-04-17 19:41:21 +0200112static char *(p_bs_values[]) = {"indent", "eol", "start", "nostop", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200113#ifdef FEAT_FOLDING
114static char *(p_fdm_values[]) = {"manual", "expr", "marker", "indent", "syntax",
115# ifdef FEAT_DIFF
116 "diff",
117# endif
118 NULL};
119static char *(p_fcl_values[]) = {"all", NULL};
120#endif
glepnira218cc62024-06-03 19:32:39 +0200121static char *(p_cot_values[]) = {"menu", "menuone", "longest", "preview", "popup", "popuphidden", "noinsert", "noselect", "fuzzy", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200122#ifdef BACKSLASH_IN_FILENAME
123static char *(p_csl_values[]) = {"slash", "backslash", NULL};
124#endif
125#ifdef FEAT_SIGNS
126static char *(p_scl_values[]) = {"yes", "no", "auto", "number", NULL};
127#endif
128#if defined(MSWIN) && defined(FEAT_TERMINAL)
129static char *(p_twt_values[]) = {"winpty", "conpty", "", NULL};
130#endif
Luuk van Baalba936f62022-12-15 13:15:39 +0000131static char *(p_sloc_values[]) = {"last", "statusline", "tabline", NULL};
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +0000132static char *(p_sws_values[]) = {"fsync", "sync", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200133
134static int check_opt_strings(char_u *val, char **values, int list);
135static int opt_strings_flags(char_u *val, char **values, unsigned *flagp, int list);
136
137/*
138 * After setting various option values: recompute variables that depend on
139 * option values.
140 */
141 void
142didset_string_options(void)
143{
144 (void)opt_strings_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
145 (void)opt_strings_flags(p_bkc, p_bkc_values, &bkc_flags, TRUE);
146 (void)opt_strings_flags(p_bo, p_bo_values, &bo_flags, TRUE);
147#ifdef FEAT_SESSION
148 (void)opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE);
149 (void)opt_strings_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
150#endif
151#ifdef FEAT_FOLDING
152 (void)opt_strings_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
153#endif
154 (void)opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE);
Yegappan Lakshmanan87018252023-09-20 20:20:04 +0200155 (void)opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200156 (void)opt_strings_flags(p_tc, p_tc_values, &tc_flags, FALSE);
157 (void)opt_strings_flags(p_ve, p_ve_values, &ve_flags, TRUE);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +0200158#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +0200159 (void)opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE);
160#endif
161#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
162 (void)opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags, TRUE);
163#endif
164#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
165 (void)opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE);
166#endif
Sean Dewar39c46b42022-05-12 17:44:29 +0100167 (void)opt_strings_flags(p_swb, p_swb_values, &swb_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200168}
169
Yegappan Lakshmananf9dc2782023-05-11 15:02:56 +0100170#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200171/*
172 * Trigger the OptionSet autocommand.
173 * "opt_idx" is the index of the option being set.
174 * "opt_flags" can be OPT_LOCAL etc.
175 * "oldval" the old value
176 * "oldval_l" the old local value (only non-NULL if global and local value
177 * are set)
178 * "oldval_g" the old global value (only non-NULL if global and local value
179 * are set)
180 * "newval" the new value
181 */
182 void
zeertzjq269aa2b2022-11-28 11:36:50 +0000183trigger_optionset_string(
Bram Moolenaardac13472019-09-16 21:06:21 +0200184 int opt_idx,
185 int opt_flags,
186 char_u *oldval,
187 char_u *oldval_l,
188 char_u *oldval_g,
189 char_u *newval)
190{
191 // Don't do this recursively.
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000192 if (oldval == NULL || newval == NULL
193 || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
194 return;
Bram Moolenaardac13472019-09-16 21:06:21 +0200195
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000196 char_u buf_type[7];
197
198 sprintf((char *)buf_type, "%s",
Bram Moolenaardac13472019-09-16 21:06:21 +0200199 (opt_flags & OPT_LOCAL) ? "local" : "global");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000200 set_vim_var_string(VV_OPTION_OLD, oldval, -1);
201 set_vim_var_string(VV_OPTION_NEW, newval, -1);
202 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
203 if (opt_flags & OPT_LOCAL)
204 {
205 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
206 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
Bram Moolenaardac13472019-09-16 21:06:21 +0200207 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000208 if (opt_flags & OPT_GLOBAL)
209 {
210 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
211 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval, -1);
212 }
213 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
214 {
215 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
216 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval_l, -1);
217 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval_g, -1);
218 }
219 if (opt_flags & OPT_MODELINE)
220 {
221 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
222 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
223 }
224 apply_autocmds(EVENT_OPTIONSET,
225 get_option_fullname(opt_idx), NULL, FALSE,
226 NULL);
227 reset_v_option_vars();
Bram Moolenaardac13472019-09-16 21:06:21 +0200228}
229#endif
230
231 static char *
Mike Williams620f0112023-12-05 15:36:06 +0100232illegal_char(char *errbuf, size_t errbuflen, int c)
Bram Moolenaardac13472019-09-16 21:06:21 +0200233{
234 if (errbuf == NULL)
235 return "";
zeertzjq6a8d2e12024-01-17 20:54:49 +0100236 vim_snprintf(errbuf, errbuflen, _(e_illegal_character_str),
Christian Brabandtb39b2402023-11-29 11:34:05 +0100237 (char *)transchar(c));
Bram Moolenaardac13472019-09-16 21:06:21 +0200238 return errbuf;
239}
240
241/*
242 * Check string options in a buffer for NULL value.
243 */
244 void
245check_buf_options(buf_T *buf)
246{
247 check_string_option(&buf->b_p_bh);
248 check_string_option(&buf->b_p_bt);
249 check_string_option(&buf->b_p_fenc);
250 check_string_option(&buf->b_p_ff);
251#ifdef FEAT_FIND_ID
252 check_string_option(&buf->b_p_def);
253 check_string_option(&buf->b_p_inc);
254# ifdef FEAT_EVAL
255 check_string_option(&buf->b_p_inex);
256# endif
257#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +0100258#if defined(FEAT_EVAL)
Bram Moolenaardac13472019-09-16 21:06:21 +0200259 check_string_option(&buf->b_p_inde);
260 check_string_option(&buf->b_p_indk);
261#endif
262#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
263 check_string_option(&buf->b_p_bexpr);
264#endif
265#if defined(FEAT_CRYPT)
266 check_string_option(&buf->b_p_cm);
267#endif
268 check_string_option(&buf->b_p_fp);
269#if defined(FEAT_EVAL)
270 check_string_option(&buf->b_p_fex);
271#endif
272#ifdef FEAT_CRYPT
273 check_string_option(&buf->b_p_key);
274#endif
275 check_string_option(&buf->b_p_kp);
276 check_string_option(&buf->b_p_mps);
277 check_string_option(&buf->b_p_fo);
278 check_string_option(&buf->b_p_flp);
279 check_string_option(&buf->b_p_isk);
Bram Moolenaardac13472019-09-16 21:06:21 +0200280 check_string_option(&buf->b_p_com);
Bram Moolenaardac13472019-09-16 21:06:21 +0200281#ifdef FEAT_FOLDING
282 check_string_option(&buf->b_p_cms);
283#endif
284 check_string_option(&buf->b_p_nf);
Bram Moolenaardac13472019-09-16 21:06:21 +0200285 check_string_option(&buf->b_p_qe);
Bram Moolenaardac13472019-09-16 21:06:21 +0200286#ifdef FEAT_SYN_HL
287 check_string_option(&buf->b_p_syn);
288 check_string_option(&buf->b_s.b_syn_isk);
289#endif
290#ifdef FEAT_SPELL
291 check_string_option(&buf->b_s.b_p_spc);
292 check_string_option(&buf->b_s.b_p_spf);
293 check_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +0200294 check_string_option(&buf->b_s.b_p_spo);
Bram Moolenaardac13472019-09-16 21:06:21 +0200295#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200296 check_string_option(&buf->b_p_sua);
Bram Moolenaardac13472019-09-16 21:06:21 +0200297 check_string_option(&buf->b_p_cink);
298 check_string_option(&buf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +0100299 check_string_option(&buf->b_p_cinsd);
Bram Moolenaardac13472019-09-16 21:06:21 +0200300 parse_cino(buf);
Bram Moolenaar49846fb2022-10-15 16:05:33 +0100301 check_string_option(&buf->b_p_lop);
Bram Moolenaardac13472019-09-16 21:06:21 +0200302 check_string_option(&buf->b_p_ft);
Bram Moolenaardac13472019-09-16 21:06:21 +0200303 check_string_option(&buf->b_p_cinw);
Bram Moolenaardac13472019-09-16 21:06:21 +0200304 check_string_option(&buf->b_p_cpt);
305#ifdef FEAT_COMPL_FUNC
306 check_string_option(&buf->b_p_cfu);
307 check_string_option(&buf->b_p_ofu);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +0100308 check_string_option(&buf->b_p_tsrfu);
Bram Moolenaardac13472019-09-16 21:06:21 +0200309#endif
310#ifdef FEAT_EVAL
311 check_string_option(&buf->b_p_tfu);
312#endif
313#ifdef FEAT_KEYMAP
314 check_string_option(&buf->b_p_keymap);
315#endif
316#ifdef FEAT_QUICKFIX
317 check_string_option(&buf->b_p_gp);
318 check_string_option(&buf->b_p_mp);
319 check_string_option(&buf->b_p_efm);
320#endif
321 check_string_option(&buf->b_p_ep);
322 check_string_option(&buf->b_p_path);
323 check_string_option(&buf->b_p_tags);
324 check_string_option(&buf->b_p_tc);
325 check_string_option(&buf->b_p_dict);
326 check_string_option(&buf->b_p_tsr);
Bram Moolenaardac13472019-09-16 21:06:21 +0200327 check_string_option(&buf->b_p_lw);
Bram Moolenaardac13472019-09-16 21:06:21 +0200328 check_string_option(&buf->b_p_bkc);
329 check_string_option(&buf->b_p_menc);
330#ifdef FEAT_VARTABS
331 check_string_option(&buf->b_p_vsts);
332 check_string_option(&buf->b_p_vts);
333#endif
334}
335
336/*
337 * Free the string allocated for an option.
338 * Checks for the string being empty_option. This may happen if we're out of
339 * memory, vim_strsave() returned NULL, which was replaced by empty_option by
340 * check_options().
341 * Does NOT check for P_ALLOCED flag!
342 */
343 void
344free_string_option(char_u *p)
345{
346 if (p != empty_option)
347 vim_free(p);
348}
349
350 void
351clear_string_option(char_u **pp)
352{
353 if (*pp != empty_option)
354 vim_free(*pp);
355 *pp = empty_option;
356}
357
358 void
359check_string_option(char_u **pp)
360{
361 if (*pp == NULL)
362 *pp = empty_option;
363}
364
365/*
366 * Set global value for string option when it's a local option.
367 */
368 static void
369set_string_option_global(
370 int opt_idx, // option index
371 char_u **varp) // pointer to option variable
372{
373 char_u **p, *s;
374
375 // the global value is always allocated
376 if (is_window_local_option(opt_idx))
377 p = (char_u **)GLOBAL_WO(varp);
378 else
379 p = (char_u **)get_option_var(opt_idx);
380 if (!is_global_option(opt_idx)
381 && p != varp
382 && (s = vim_strsave(*varp)) != NULL)
383 {
384 free_string_option(*p);
385 *p = s;
386 }
387}
388
389/*
390 * Set a string option to a new value (without checking the effect).
391 * The string is copied into allocated memory.
392 * if ("opt_idx" == -1) "name" is used, otherwise "opt_idx" is used.
393 * When "set_sid" is zero set the scriptID to current_sctx.sc_sid. When
394 * "set_sid" is SID_NONE don't set the scriptID. Otherwise set the scriptID to
395 * "set_sid".
396 */
397 void
398set_string_option_direct(
399 char_u *name,
400 int opt_idx,
401 char_u *val,
402 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
403 int set_sid UNUSED)
404{
405 char_u *s;
406 char_u **varp;
407 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
408 int idx = opt_idx;
409
410 if (idx == -1) // use name
411 {
412 idx = findoption(name);
413 if (idx < 0) // not found (should not happen)
414 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000415 semsg(_(e_internal_error_str), "set_string_option_direct()");
RestorerZ68ebcee2023-05-31 17:12:14 +0100416 siemsg("For option %s", name);
Bram Moolenaardac13472019-09-16 21:06:21 +0200417 return;
418 }
419 }
420
421 if (is_hidden_option(idx)) // can't set hidden option
422 return;
423
424 s = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000425 if (s == NULL)
426 return;
427
428 varp = (char_u **)get_option_varp_scope(idx,
429 both ? OPT_LOCAL : opt_flags);
430 if ((opt_flags & OPT_FREE) && (get_option_flags(idx) & P_ALLOCED))
431 free_string_option(*varp);
432 *varp = s;
433
434 // For buffer/window local option may also set the global value.
435 if (both)
436 set_string_option_global(idx, varp);
437
438 set_option_flag(idx, P_ALLOCED);
439
440 // When setting both values of a global option with a local value,
441 // make the local value empty, so that the global value is used.
442 if (is_global_local_option(idx) && both)
Bram Moolenaardac13472019-09-16 21:06:21 +0200443 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000444 free_string_option(*varp);
445 *varp = empty_option;
Bram Moolenaardac13472019-09-16 21:06:21 +0200446 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000447# ifdef FEAT_EVAL
448 if (set_sid != SID_NONE)
449 {
450 sctx_T script_ctx;
451
452 if (set_sid == 0)
453 script_ctx = current_sctx;
454 else
455 {
456 script_ctx.sc_sid = set_sid;
457 script_ctx.sc_seq = 0;
458 script_ctx.sc_lnum = 0;
459 script_ctx.sc_version = 1;
460 }
461 set_option_sctx_idx(idx, opt_flags, script_ctx);
462 }
463# endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200464}
465
Dominique Pellee764d1b2023-03-12 21:20:59 +0000466#if defined(FEAT_PROP_POPUP) || \
467 (defined(FEAT_DIFF) && defined(FEAT_FOLDING)) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200468/*
469 * Like set_string_option_direct(), but for a window-local option in "wp".
470 * Blocks autocommands to avoid the old curwin becoming invalid.
471 */
472 void
473set_string_option_direct_in_win(
474 win_T *wp,
475 char_u *name,
476 int opt_idx,
477 char_u *val,
478 int opt_flags,
479 int set_sid)
480{
481 win_T *save_curwin = curwin;
482
483 block_autocmds();
484 curwin = wp;
485 curbuf = curwin->w_buffer;
486 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
487 curwin = save_curwin;
488 curbuf = curwin->w_buffer;
489 unblock_autocmds();
490}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000491#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200492
Dominique Pelle748b3082022-01-08 12:41:16 +0000493#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200494/*
495 * Like set_string_option_direct(), but for a buffer-local option in "buf".
496 * Blocks autocommands to avoid the old curbuf becoming invalid.
497 */
498 void
499set_string_option_direct_in_buf(
500 buf_T *buf,
501 char_u *name,
502 int opt_idx,
503 char_u *val,
504 int opt_flags,
505 int set_sid)
506{
507 buf_T *save_curbuf = curbuf;
508
509 block_autocmds();
510 curbuf = buf;
511 curwin->w_buffer = curbuf;
512 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
513 curbuf = save_curbuf;
514 curwin->w_buffer = curbuf;
515 unblock_autocmds();
516}
Dominique Pelle748b3082022-01-08 12:41:16 +0000517#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200518
519/*
520 * Set a string option to a new value, and handle the effects.
521 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100522 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaardac13472019-09-16 21:06:21 +0200523 */
524 char *
525set_string_option(
526 int opt_idx,
527 char_u *value,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +0000528 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Christian Brabandtb39b2402023-11-29 11:34:05 +0100529 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +0100530 size_t errbuflen)
Bram Moolenaardac13472019-09-16 21:06:21 +0200531{
532 char_u *s;
533 char_u **varp;
534 char_u *oldval;
535#if defined(FEAT_EVAL)
536 char_u *oldval_l = NULL;
537 char_u *oldval_g = NULL;
538 char_u *saved_oldval = NULL;
539 char_u *saved_oldval_l = NULL;
540 char_u *saved_oldval_g = NULL;
541 char_u *saved_newval = NULL;
542#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100543 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +0200544 int value_checked = FALSE;
545
546 if (is_hidden_option(opt_idx)) // don't set hidden option
547 return NULL;
548
Bram Moolenaar7f009df2020-03-16 20:27:38 +0100549 s = vim_strsave(value == NULL ? (char_u *)"" : value);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000550 if (s == NULL)
551 return NULL;
552
553 varp = (char_u **)get_option_varp_scope(opt_idx,
554 (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
555 ? (is_global_local_option(opt_idx)
556 ? OPT_GLOBAL : OPT_LOCAL)
557 : opt_flags);
558 oldval = *varp;
559#if defined(FEAT_EVAL)
560 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +0200561 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000562 oldval_l = *(char_u **)get_option_varp_scope(opt_idx, OPT_LOCAL);
563 oldval_g = *(char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
Bram Moolenaardac13472019-09-16 21:06:21 +0200564 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000565#endif
566 *varp = s;
567
568#if defined(FEAT_EVAL)
569 if (!starting
570# ifdef FEAT_CRYPT
571 && !is_crypt_key_option(opt_idx)
572# endif
573 )
574 {
575 if (oldval_l != NULL)
576 saved_oldval_l = vim_strsave(oldval_l);
577 if (oldval_g != NULL)
578 saved_oldval_g = vim_strsave(oldval_g);
579 saved_oldval = vim_strsave(oldval);
580 saved_newval = vim_strsave(s);
581 }
582#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000583 if ((errmsg = did_set_string_option(opt_idx, varp, oldval, value, errbuf,
Christian Brabandtb39b2402023-11-29 11:34:05 +0100584 errbuflen, opt_flags, OP_NONE, &value_checked)) == NULL)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000585 did_set_option(opt_idx, opt_flags, TRUE, value_checked);
586
587#if defined(FEAT_EVAL)
588 // call autocommand after handling side effects
589 if (errmsg == NULL)
590 trigger_optionset_string(opt_idx, opt_flags,
591 saved_oldval, saved_oldval_l,
592 saved_oldval_g, saved_newval);
593 vim_free(saved_oldval);
594 vim_free(saved_oldval_l);
595 vim_free(saved_oldval_g);
596 vim_free(saved_newval);
597#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100598 return errmsg;
Bram Moolenaardac13472019-09-16 21:06:21 +0200599}
600
601/*
602 * Return TRUE if "val" is a valid 'filetype' name.
603 * Also used for 'syntax' and 'keymap'.
604 */
605 static int
606valid_filetype(char_u *val)
607{
608 return valid_name(val, ".-_");
609}
610
611#ifdef FEAT_STL_OPT
612/*
613 * Check validity of options with the 'statusline' format.
zeertzjq5dc294a2022-04-15 13:17:57 +0100614 * Return an untranslated error message or NULL.
Bram Moolenaardac13472019-09-16 21:06:21 +0200615 */
616 static char *
617check_stl_option(char_u *s)
618{
Bram Moolenaardac13472019-09-16 21:06:21 +0200619 int groupdepth = 0;
Christian Brabandtb39b2402023-11-29 11:34:05 +0100620 static char errbuf[ERR_BUFLEN];
621 int errbuflen = ERR_BUFLEN;
Bram Moolenaardac13472019-09-16 21:06:21 +0200622
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100623 while (*s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200624 {
625 // Check for valid keys after % sequences
626 while (*s && *s != '%')
627 s++;
628 if (!*s)
629 break;
630 s++;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +0000631 if (*s == '%' || *s == STL_TRUNCMARK || *s == STL_SEPARATE)
Bram Moolenaardac13472019-09-16 21:06:21 +0200632 {
633 s++;
634 continue;
635 }
636 if (*s == ')')
637 {
638 s++;
639 if (--groupdepth < 0)
640 break;
641 continue;
642 }
643 if (*s == '-')
644 s++;
645 while (VIM_ISDIGIT(*s))
646 s++;
647 if (*s == STL_USER_HL)
648 continue;
649 if (*s == '.')
650 {
651 s++;
652 while (*s && VIM_ISDIGIT(*s))
653 s++;
654 }
655 if (*s == '(')
656 {
657 groupdepth++;
658 continue;
659 }
660 if (vim_strchr(STL_ALL, *s) == NULL)
661 {
Christian Brabandtb39b2402023-11-29 11:34:05 +0100662 return illegal_char(errbuf, errbuflen, *s);
Bram Moolenaardac13472019-09-16 21:06:21 +0200663 }
664 if (*s == '{')
665 {
zeertzjq5dc294a2022-04-15 13:17:57 +0100666 int reevaluate = (*++s == '%');
shadmansaleh30e3de22021-05-15 17:23:28 +0200667
zeertzjq5dc294a2022-04-15 13:17:57 +0100668 if (reevaluate && *++s == '}')
669 // "}" is not allowed immediately after "%{%"
Christian Brabandtb39b2402023-11-29 11:34:05 +0100670 return illegal_char(errbuf, errbuflen, '}');
shadmansaleh30e3de22021-05-15 17:23:28 +0200671 while ((*s != '}' || (reevaluate && s[-1] != '%')) && *s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200672 s++;
673 if (*s != '}')
zeertzjq5dc294a2022-04-15 13:17:57 +0100674 return e_unclosed_expression_sequence;
Bram Moolenaardac13472019-09-16 21:06:21 +0200675 }
676 }
Bram Moolenaardac13472019-09-16 21:06:21 +0200677 if (groupdepth != 0)
zeertzjq5dc294a2022-04-15 13:17:57 +0100678 return e_unbalanced_groups;
Bram Moolenaardac13472019-09-16 21:06:21 +0200679 return NULL;
680}
681#endif
682
683/*
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +0000684 * Check for a "normal" directory or file name in some options. Disallow a
685 * path separator (slash and/or backslash), wildcards and characters that are
686 * often illegal in a file name. Be more permissive if "secure" is off.
687 */
688 static int
689check_illegal_path_names(int opt_idx, char_u **varp)
690{
691 return (((get_option_flags(opt_idx) & P_NFNAME)
692 && vim_strpbrk(*varp, (char_u *)(secure
693 ? "/\\*?[|;&<>\r\n" : "/\\*?[<>\r\n")) != NULL)
694 || ((get_option_flags(opt_idx) & P_NDNAME)
695 && vim_strpbrk(*varp, (char_u *)"*?[|;&<>\r\n") != NULL));
696}
697
698/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000699 * An option that accepts a list of flags is changed.
700 * e.g. 'viewoptions', 'switchbuf', 'casemap', etc.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000701 */
702 static char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000703did_set_opt_flags(char_u *val, char **values, unsigned *flagp, int list)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000704{
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000705 if (opt_strings_flags(val, values, flagp, list) == FAIL)
706 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000707
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000708 return NULL;
709}
710
711/*
712 * An option that accepts a list of string values is changed.
713 * e.g. 'nrformats', 'scrollopt', 'wildoptions', etc.
714 */
715 static char *
716did_set_opt_strings(char_u *val, char **values, int list)
717{
718 return did_set_opt_flags(val, values, NULL, list);
719}
720
721/*
722 * An option which is a list of flags is set. Valid values are in 'flags'.
723 */
724 static char *
Christian Brabandtb39b2402023-11-29 11:34:05 +0100725did_set_option_listflag(
726 char_u *val,
727 char_u *flags,
728 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +0100729 size_t errbuflen)
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000730{
731 char_u *s;
732
Yegappan Lakshmananc727b192023-03-03 12:26:15 +0000733 for (s = val; *s; ++s)
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000734 if (vim_strchr(flags, *s) == NULL)
Christian Brabandtb39b2402023-11-29 11:34:05 +0100735 return illegal_char(errbuf, errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000736
737 return NULL;
738}
739
740/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200741 * Expand an option that accepts a list of fixed string values with known
742 * number of items.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200743 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200744 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200745expand_set_opt_string(
746 optexpand_T *args,
747 char **values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +0200748 size_t numValues,
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200749 int *numMatches,
750 char_u ***matches)
751{
752 char_u *p;
753 regmatch_T *regmatch = args->oe_regmatch;
754 int include_orig_val = args->oe_include_orig_val;
755 char_u *option_val = args->oe_opt_value;
756
757 // Assume numValues is small since they are fixed enums, so just allocate
758 // upfront instead of needing two passes to calculate output size.
759 *matches = ALLOC_MULT(char_u *, numValues + 1);
760 if (*matches == NULL)
761 return FAIL;
762
763 int count = 0;
764
765 if (include_orig_val && *option_val != NUL)
766 {
767 p = vim_strsave(option_val);
768 if (p == NULL)
769 {
770 VIM_CLEAR(*matches);
771 return FAIL;
772 }
773 (*matches)[count++] = p;
774 }
775
776 for (char **val = values; *val != NULL; val++)
777 {
778 if (include_orig_val && *option_val != NUL)
779 {
780 if (STRCMP((char_u*)*val, option_val) == 0)
781 continue;
782 }
783 if (vim_regexec(regmatch, (char_u*)(*val), (colnr_T)0))
784 {
785 p = vim_strsave((char_u*)*val);
786 if (p == NULL)
787 {
788 if (count == 0)
789 {
790 VIM_CLEAR(*matches);
791 return FAIL;
792 }
793 else
794 break;
795 }
796 (*matches)[count++] = p;
797 }
798 }
799 if (count == 0)
800 {
801 VIM_CLEAR(*matches);
802 return FAIL;
803 }
804 *numMatches = count;
805 return OK;
806}
807
808static char_u *set_opt_callback_orig_option = NULL;
809static char_u *((*set_opt_callback_func)(expand_T *, int));
810
811/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200812 * Callback used by expand_set_opt_generic to also include the original value
813 * as the first item.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200814 */
815 static char_u *
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200816expand_set_opt_generic_cb(expand_T *xp, int idx)
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200817{
818 if (idx == 0)
819 {
820 if (set_opt_callback_orig_option != NULL)
821 return set_opt_callback_orig_option;
822 else
823 return (char_u *)""; // empty strings are ignored
824 }
825 return set_opt_callback_func(xp, idx - 1);
826}
827
828/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200829 * Expand an option with a callback that iterates through a list of possible
830 * names using an index.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200831 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200832 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200833expand_set_opt_generic(
834 optexpand_T *args,
835 char_u *((*func)(expand_T *, int)),
836 int *numMatches,
837 char_u ***matches)
838{
839 int ret;
840
841 set_opt_callback_orig_option = args->oe_include_orig_val ?
842 args->oe_opt_value : NULL;
843 set_opt_callback_func = func;
844
845 ret = ExpandGeneric(
846 (char_u*)"", // not using fuzzy as currently EXPAND_STRING_SETTING doesn't use it
847 args->oe_xp,
848 args->oe_regmatch,
849 matches,
850 numMatches,
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200851 expand_set_opt_generic_cb,
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200852 FALSE);
853
854 set_opt_callback_orig_option = NULL;
855 set_opt_callback_func = NULL;
856 return ret;
857}
858
Christian Brabandt9960ebc2023-10-05 22:17:09 +0200859# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200860static garray_T *expand_cb_ga;
861static optexpand_T *expand_cb_args;
862
863/*
864 * Callback provided to a function in expand_set_opt_callback. Will perform
865 * regex matching against the value and add to the list.
866 *
867 * Returns OK usually. Returns FAIL if it failed to allocate memory, and the
868 * caller should terminate the enumeration.
869 */
870 static int
871expand_set_opt_callback_cb(char_u *val)
872{
873 regmatch_T *regmatch = expand_cb_args->oe_regmatch;
874 expand_T *xp = expand_cb_args->oe_xp;
875 garray_T *ga = expand_cb_ga;
876 char_u *str;
877
878 if (val == NULL || *val == NUL)
879 return OK;
880
881 if (xp->xp_pattern[0] != NUL &&
882 !vim_regexec(regmatch, val, (colnr_T)0))
883 return OK;
884
885 str = vim_strsave_escaped(val, (char_u *)" \t\\");
886
887 if (str == NULL)
888 return FAIL;
889
890 if (ga_grow(ga, 1) == FAIL)
891 {
892 vim_free(str);
893 return FAIL;
894 }
895
896 ((char_u **)ga->ga_data)[ga->ga_len] = str;
897 ++ga->ga_len;
898 return OK;
899}
900
901/*
902 * Expand an option with a provided function that takes a callback. The
903 * function will enumerate through all options and call the callback to add it
904 * to the list.
905 *
906 * "func" is the enumerator function that will generate the list of options.
907 * "func_params" is a single parameter that will be passed to func.
908 */
909 static int
910expand_set_opt_callback(
911 optexpand_T *args,
912 void (*func)(optexpand_T *, void* params, int (*cb)(char_u *val)),
913 void *func_params,
914 int *numMatches,
915 char_u ***matches)
916{
917 garray_T ga;
918 int include_orig_val = args->oe_include_orig_val;
919 char_u *option_val = args->oe_opt_value;
920
921 ga_init2(&ga, sizeof(char *), 30);
922
923 if (include_orig_val && *option_val != NUL)
924 {
925 char_u *p = vim_strsave(option_val);
926 if (p == NULL)
927 return FAIL;
928 if (ga_grow(&ga, 1) == FAIL)
929 {
930 vim_free(p);
931 return FAIL;
932 }
933 ((char_u **)ga.ga_data)[ga.ga_len] = p;
934 ++ga.ga_len;
935 }
936
937 expand_cb_ga = &ga;
938 expand_cb_args = args;
939
940 func(args, func_params, expand_set_opt_callback_cb);
941
942 expand_cb_ga = NULL;
943 expand_cb_args = NULL;
944
945 *matches = ga.ga_data;
946 *numMatches = ga.ga_len;
947 return OK;
948}
Christian Brabandt9960ebc2023-10-05 22:17:09 +0200949#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200950
951/*
952 * Expand an option which is a list of flags.
953 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200954 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200955expand_set_opt_listflag(
956 optexpand_T *args,
957 char_u *flags,
958 int *numMatches,
959 char_u ***matches)
960{
961 char_u *p;
962 char_u *option_val = args->oe_opt_value;
963 char_u *cmdline_val = args->oe_set_arg;
964 int append = args->oe_append;
965 int include_orig_val = args->oe_include_orig_val && (*option_val != NUL);
966
Yee Cheng Chin6d113472023-10-02 21:38:39 +0200967 size_t num_flags = STRLEN(flags);
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200968
969 // Assume we only have small number of flags, so just allocate max size.
970 *matches = ALLOC_MULT(char_u *, num_flags + 1);
971 if (*matches == NULL)
972 return FAIL;
973
974 int count = 0;
975
976 if (include_orig_val)
977 {
978 p = vim_strsave(option_val);
979 if (p == NULL)
980 {
981 VIM_CLEAR(*matches);
982 return FAIL;
983 }
984 (*matches)[count++] = p;
985 }
986
987 for (char_u *flag = flags; *flag != NUL; flag++)
988 {
989 if (append && vim_strchr(option_val, *flag) != NULL)
990 continue;
991
992 if (vim_strchr(cmdline_val, *flag) == NULL)
993 {
994 if (include_orig_val
995 && option_val[1] == NUL
996 && *flag == option_val[0])
997 {
998 // This value is already used as the first choice as it's the
999 // existing flag. Just skip it to avoid duplicate.
1000 continue;
1001 }
1002 p = vim_strnsave(flag, 1);
1003 if (p == NULL)
1004 {
1005 if (count == 0)
1006 {
1007 VIM_CLEAR(*matches);
1008 return FAIL;
1009 }
1010 else
1011 break;
1012 }
1013 (*matches)[count++] = p;
1014 }
1015 }
1016
1017 if (count == 0)
1018 {
1019 VIM_CLEAR(*matches);
1020 return FAIL;
1021 }
1022 *numMatches = count;
1023 return OK;
1024}
1025
1026/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001027 * The 'ambiwidth' option is changed.
1028 */
1029 char *
1030did_set_ambiwidth(optset_T *args UNUSED)
1031{
1032 if (check_opt_strings(p_ambw, p_ambw_values, FALSE) != OK)
1033 return e_invalid_argument;
1034
1035 return check_chars_options();
1036}
1037
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001038 int
1039expand_set_ambiwidth(optexpand_T *args, int *numMatches, char_u ***matches)
1040{
1041 return expand_set_opt_string(
1042 args,
1043 p_ambw_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001044 ARRAY_LENGTH(p_ambw_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001045 numMatches,
1046 matches);
1047}
1048
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001049/*
1050 * The 'background' option is changed.
1051 */
1052 char *
Gregory Anders83ad2722024-01-03 19:48:51 +01001053did_set_background(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001054{
1055 if (check_opt_strings(p_bg, p_bg_values, FALSE) == FAIL)
1056 return e_invalid_argument;
1057
Gregory Anders83ad2722024-01-03 19:48:51 +01001058 if (args->os_oldval.string != NULL && args->os_oldval.string[0] == *p_bg)
1059 // Value was not changed
1060 return NULL;
1061
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001062#ifdef FEAT_EVAL
1063 int dark = (*p_bg == 'd');
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001064#endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001065
1066 init_highlight(FALSE, FALSE);
1067
1068#ifdef FEAT_EVAL
1069 if (dark != (*p_bg == 'd')
1070 && get_var_value((char_u *)"g:colors_name") != NULL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001071 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001072 // The color scheme must have set 'background' back to another
1073 // value, that's not what we want here. Disable the color
1074 // scheme and set the colors again.
1075 do_unlet((char_u *)"g:colors_name", TRUE);
1076 free_string_option(p_bg);
1077 p_bg = vim_strsave((char_u *)(dark ? "dark" : "light"));
1078 check_string_option(&p_bg);
1079 init_highlight(FALSE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001080 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001081#endif
1082#ifdef FEAT_TERMINAL
1083 term_update_colors_all();
1084#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001085
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001086 return NULL;
1087}
1088
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001089 int
1090expand_set_background(optexpand_T *args, int *numMatches, char_u ***matches)
1091{
1092 return expand_set_opt_string(
1093 args,
1094 p_bg_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001095 ARRAY_LENGTH(p_bg_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001096 numMatches,
1097 matches);
1098}
1099
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001100/*
1101 * The 'backspace' option is changed.
1102 */
1103 char *
1104did_set_backspace(optset_T *args UNUSED)
1105{
1106 if (VIM_ISDIGIT(*p_bs))
1107 {
1108 if (*p_bs > '3' || p_bs[1] != NUL)
1109 return e_invalid_argument;
1110 }
1111 else if (check_opt_strings(p_bs, p_bs_values, TRUE) != OK)
1112 return e_invalid_argument;
1113
1114 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001115}
1116
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001117 int
1118expand_set_backspace(optexpand_T *args, int *numMatches, char_u ***matches)
1119{
1120 return expand_set_opt_string(
1121 args,
1122 p_bs_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001123 ARRAY_LENGTH(p_bs_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001124 numMatches,
1125 matches);
1126}
1127
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001128/*
1129 * The 'backupcopy' option is changed.
1130 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001131 char *
1132did_set_backupcopy(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001133{
1134 char_u *bkc = p_bkc;
1135 unsigned int *flags = &bkc_flags;
1136 char *errmsg = NULL;
1137
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001138 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001139 {
1140 bkc = curbuf->b_p_bkc;
1141 flags = &curbuf->b_bkc_flags;
1142 }
1143
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001144 if ((args->os_flags & OPT_LOCAL) && *bkc == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001145 // make the local value empty: use the global value
1146 *flags = 0;
1147 else
1148 {
1149 if (opt_strings_flags(bkc, p_bkc_values, flags, TRUE) != OK)
1150 errmsg = e_invalid_argument;
1151 if ((((int)*flags & BKC_AUTO) != 0)
1152 + (((int)*flags & BKC_YES) != 0)
1153 + (((int)*flags & BKC_NO) != 0) != 1)
1154 {
1155 // Must have exactly one of "auto", "yes" and "no".
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001156 (void)opt_strings_flags(args->os_oldval.string, p_bkc_values,
1157 flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001158 errmsg = e_invalid_argument;
1159 }
1160 }
1161
1162 return errmsg;
1163}
1164
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001165 int
1166expand_set_backupcopy(optexpand_T *args, int *numMatches, char_u ***matches)
1167{
1168 return expand_set_opt_string(
1169 args,
1170 p_bkc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001171 ARRAY_LENGTH(p_bkc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001172 numMatches,
1173 matches);
1174}
1175
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001176/*
1177 * The 'backupext' or the 'patchmode' option is changed.
1178 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001179 char *
1180did_set_backupext_or_patchmode(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001181{
1182 if (STRCMP(*p_bex == '.' ? p_bex + 1 : p_bex,
1183 *p_pm == '.' ? p_pm + 1 : p_pm) == 0)
1184 return e_backupext_and_patchmode_are_equal;
1185
1186 return NULL;
1187}
1188
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001189/*
1190 * The 'belloff' option is changed.
1191 */
1192 char *
1193did_set_belloff(optset_T *args UNUSED)
1194{
1195 return did_set_opt_flags(p_bo, p_bo_values, &bo_flags, TRUE);
1196}
1197
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001198 int
1199expand_set_belloff(optexpand_T *args, int *numMatches, char_u ***matches)
1200{
1201 return expand_set_opt_string(
1202 args,
1203 p_bo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001204 ARRAY_LENGTH(p_bo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001205 numMatches,
1206 matches);
1207}
1208
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001209#if defined(FEAT_LINEBREAK) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001210/*
zeertzjqeac3fdc2024-02-03 18:08:09 +01001211 * The 'breakat' option is changed.
1212 */
1213 char *
1214did_set_breakat(optset_T *args UNUSED)
1215{
1216 char_u *p;
1217 int i;
1218
1219 for (i = 0; i < 256; i++)
1220 breakat_flags[i] = FALSE;
1221
1222 if (p_breakat != NULL)
1223 for (p = p_breakat; *p; p++)
1224 breakat_flags[*p] = TRUE;
1225
1226 return NULL;
1227}
1228
1229/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001230 * The 'breakindentopt' option is changed.
1231 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001232 char *
1233did_set_breakindentopt(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001234{
1235 char *errmsg = NULL;
1236
1237 if (briopt_check(curwin) == FAIL)
1238 errmsg = e_invalid_argument;
1239 // list setting requires a redraw
1240 if (curwin->w_briopt_list)
1241 redraw_all_later(UPD_NOT_VALID);
1242
1243 return errmsg;
1244}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001245
1246 int
1247expand_set_breakindentopt(optexpand_T *args, int *numMatches, char_u ***matches)
1248{
1249 return expand_set_opt_string(
1250 args,
1251 p_briopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001252 ARRAY_LENGTH(p_briopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001253 numMatches,
1254 matches);
1255}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001256#endif
1257
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001258#if defined(FEAT_BROWSE) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001259/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001260 * The 'browsedir' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001261 */
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00001262 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001263did_set_browsedir(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001264{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001265 if (check_opt_strings(p_bsdir, p_bsdir_values, FALSE) != OK
1266 && !mch_isdir(p_bsdir))
1267 return e_invalid_argument;
1268
1269 return NULL;
1270}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001271
1272 int
1273expand_set_browsedir(optexpand_T *args, int *numMatches, char_u ***matches)
1274{
1275 return expand_set_opt_string(
1276 args,
1277 p_bsdir_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001278 ARRAY_LENGTH(p_bsdir_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001279 numMatches,
1280 matches);
1281}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001282#endif
1283
1284/*
1285 * The 'bufhidden' option is changed.
1286 */
1287 char *
1288did_set_bufhidden(optset_T *args UNUSED)
1289{
1290 return did_set_opt_strings(curbuf->b_p_bh, p_bufhidden_values, FALSE);
1291}
1292
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001293 int
1294expand_set_bufhidden(optexpand_T *args, int *numMatches, char_u ***matches)
1295{
1296 return expand_set_opt_string(
1297 args,
1298 p_bufhidden_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001299 ARRAY_LENGTH(p_bufhidden_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001300 numMatches,
1301 matches);
1302}
1303
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001304/*
1305 * The 'buftype' option is changed.
1306 */
1307 char *
1308did_set_buftype(optset_T *args UNUSED)
1309{
1310 if (check_opt_strings(curbuf->b_p_bt, p_buftype_values, FALSE) != OK)
1311 return e_invalid_argument;
1312
1313 if (curwin->w_status_height)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001314 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001315 curwin->w_redr_status = TRUE;
1316 redraw_later(UPD_VALID);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001317 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001318 curbuf->b_help = (curbuf->b_p_bt[0] == 'h');
1319 redraw_titles();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001320
1321 return NULL;
1322}
1323
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001324 int
1325expand_set_buftype(optexpand_T *args, int *numMatches, char_u ***matches)
1326{
1327 return expand_set_opt_string(
1328 args,
1329 p_buftype_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001330 ARRAY_LENGTH(p_buftype_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001331 numMatches,
1332 matches);
1333}
1334
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001335/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001336 * The 'casemap' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001337 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001338 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001339did_set_casemap(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001340{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001341 return did_set_opt_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
1342}
1343
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001344 int
1345expand_set_casemap(optexpand_T *args, int *numMatches, char_u ***matches)
1346{
1347 return expand_set_opt_string(
1348 args,
1349 p_cmp_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001350 ARRAY_LENGTH(p_cmp_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001351 numMatches,
1352 matches);
1353}
1354
1355#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1356 int
1357expand_set_clipboard(optexpand_T *args, int *numMatches, char_u ***matches)
1358{
1359 return expand_set_opt_string(
1360 args,
1361 p_cb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001362 ARRAY_LENGTH(p_cb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001363 numMatches,
1364 matches);
1365}
1366#endif
1367
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001368/*
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001369 * The global 'listchars' or 'fillchars' option is changed.
1370 */
1371 static char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01001372did_set_global_listfillchars(char_u *val, int opt_lcs, int opt_flags,
1373 char *errbuf, size_t errbuflen)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001374{
1375 char *errmsg = NULL;
1376 char_u **local_ptr = opt_lcs ? &curwin->w_p_lcs : &curwin->w_p_fcs;
1377
1378 // only apply the global value to "curwin" when it does not have a
1379 // local value
1380 if (opt_lcs)
1381 errmsg = set_listchars_option(curwin, val,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001382 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL),
1383 errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001384 else
1385 errmsg = set_fillchars_option(curwin, val,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001386 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL),
1387 errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001388 if (errmsg != NULL)
1389 return errmsg;
1390
1391 tabpage_T *tp;
1392 win_T *wp;
1393
1394 // If the current window is set to use the global
1395 // 'listchars'/'fillchars' value, clear the window-local value.
1396 if (!(opt_flags & OPT_GLOBAL))
1397 clear_string_option(local_ptr);
1398 FOR_ALL_TAB_WINDOWS(tp, wp)
1399 {
1400 // If the current window has a local value need to apply it
1401 // again, it was changed when setting the global value.
1402 // If no error was returned above, we don't expect an error
1403 // here, so ignore the return value.
1404 if (opt_lcs)
1405 {
1406 if (*wp->w_p_lcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001407 (void)set_listchars_option(wp, wp->w_p_lcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001408 }
1409 else
1410 {
1411 if (*wp->w_p_fcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001412 (void)set_fillchars_option(wp, wp->w_p_fcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001413 }
1414 }
1415
1416 redraw_all_later(UPD_NOT_VALID);
1417
1418 return NULL;
1419}
1420
1421/*
1422 * The 'fillchars' option or the 'listchars' option is changed.
1423 */
1424 char *
1425did_set_chars_option(optset_T *args)
1426{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001427 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001428 char *errmsg = NULL;
1429
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001430 if ( varp == &p_lcs // global 'listchars'
1431 || varp == &p_fcs) // global 'fillchars'
1432 errmsg = did_set_global_listfillchars(*varp, varp == &p_lcs,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001433 args->os_flags, args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001434 else if (varp == &curwin->w_p_lcs) // local 'listchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001435 errmsg = set_listchars_option(curwin, *varp, TRUE,
1436 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001437 else if (varp == &curwin->w_p_fcs) // local 'fillchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001438 errmsg = set_fillchars_option(curwin, *varp, TRUE,
1439 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001440
1441 return errmsg;
1442}
1443
1444/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001445 * Expand 'fillchars' or 'listchars' option value.
1446 */
1447 int
1448expand_set_chars_option(optexpand_T *args, int *numMatches, char_u ***matches)
1449{
1450 char_u **varp = (char_u **)args->oe_varp;
1451 int is_lcs = (varp == &p_lcs || varp == &curwin->w_p_lcs);
1452 return expand_set_opt_generic(
1453 args,
1454 is_lcs ? get_listchars_name : get_fillchars_name,
1455 numMatches,
1456 matches);
1457}
1458
1459/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001460 * The 'cinoptions' option is changed.
1461 */
1462 char *
1463did_set_cinoptions(optset_T *args UNUSED)
1464{
1465 // TODO: recognize errors
1466 parse_cino(curbuf);
1467
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001468 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001469}
1470
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00001471#if defined(FEAT_SYN_HL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001472/*
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001473 * The 'colorcolumn' option is changed.
1474 */
1475 char *
1476did_set_colorcolumn(optset_T *args UNUSED)
1477{
1478 return check_colorcolumn(curwin);
1479}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001480#endif
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001481
1482/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001483 * The 'comments' option is changed.
1484 */
1485 char *
1486did_set_comments(optset_T *args)
1487{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001488 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001489 char_u *s;
1490 char *errmsg = NULL;
1491
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001492 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001493 {
1494 while (*s && *s != ':')
1495 {
1496 if (vim_strchr((char_u *)COM_ALL, *s) == NULL
1497 && !VIM_ISDIGIT(*s) && *s != '-')
1498 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01001499 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001500 break;
1501 }
1502 ++s;
1503 }
1504 if (*s++ == NUL)
1505 errmsg = e_missing_colon;
1506 else if (*s == ',' || *s == NUL)
1507 errmsg = e_zero_length_string;
1508 if (errmsg != NULL)
1509 break;
1510 while (*s && *s != ',')
1511 {
1512 if (*s == '\\' && s[1] != NUL)
1513 ++s;
1514 ++s;
1515 }
1516 s = skip_to_option_part(s);
1517 }
1518
1519 return errmsg;
1520}
1521
1522#if defined(FEAT_FOLDING) || defined(PROTO)
1523/*
1524 * The 'commentstring' option is changed.
1525 */
1526 char *
1527did_set_commentstring(optset_T *args)
1528{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001529 char_u **varp = (char_u **)args->os_varp;
1530
1531 if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001532 return e_commentstring_must_be_empty_or_contain_str;
1533
1534 return NULL;
1535}
1536#endif
1537
1538/*
1539 * The 'complete' option is changed.
1540 */
1541 char *
1542did_set_complete(optset_T *args)
1543{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001544 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001545 char_u *s;
1546
1547 // check if it is a valid value for 'complete' -- Acevedo
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001548 for (s = *varp; *s;)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001549 {
1550 while (*s == ',' || *s == ' ')
1551 s++;
1552 if (!*s)
1553 break;
1554 if (vim_strchr((char_u *)".wbuksid]tU", *s) == NULL)
Christian Brabandtb39b2402023-11-29 11:34:05 +01001555 return illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001556 if (*++s != NUL && *s != ',' && *s != ' ')
1557 {
1558 if (s[-1] == 'k' || s[-1] == 's')
1559 {
1560 // skip optional filename after 'k' and 's'
1561 while (*s && *s != ',' && *s != ' ')
1562 {
1563 if (*s == '\\' && s[1] != NUL)
1564 ++s;
1565 ++s;
1566 }
1567 }
1568 else
1569 {
1570 if (args->os_errbuf != NULL)
1571 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01001572 vim_snprintf((char *)args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001573 _(e_illegal_character_after_chr), *--s);
1574 return args->os_errbuf;
1575 }
1576 return "";
1577 }
1578 }
1579 }
1580
1581 return NULL;
1582}
1583
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001584 int
1585expand_set_complete(optexpand_T *args, int *numMatches, char_u ***matches)
1586{
1587 static char *(p_cpt_values[]) = {
1588 ".", "w", "b", "u", "k", "kspell", "s", "i", "d", "]", "t", "U",
1589 NULL};
1590 return expand_set_opt_string(
1591 args,
1592 p_cpt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001593 ARRAY_LENGTH(p_cpt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001594 numMatches,
1595 matches);
1596}
1597
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001598/*
1599 * The 'completeopt' option is changed.
1600 */
1601 char *
1602did_set_completeopt(optset_T *args UNUSED)
1603{
1604 if (check_opt_strings(p_cot, p_cot_values, TRUE) != OK)
1605 return e_invalid_argument;
1606
1607 completeopt_was_set();
1608 return NULL;
1609}
1610
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001611 int
1612expand_set_completeopt(optexpand_T *args, int *numMatches, char_u ***matches)
1613{
1614 return expand_set_opt_string(
1615 args,
1616 p_cot_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001617 ARRAY_LENGTH(p_cot_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001618 numMatches,
1619 matches);
1620}
1621
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001622#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1623/*
1624 * The 'completepopup' option is changed.
1625 */
1626 char *
1627did_set_completepopup(optset_T *args UNUSED)
1628{
1629 if (parse_completepopup(NULL) == FAIL)
1630 return e_invalid_argument;
1631
1632 popup_close_info();
1633 return NULL;
1634}
1635#endif
1636
1637#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1638/*
1639 * The 'completeslash' option is changed.
1640 */
1641 char *
1642did_set_completeslash(optset_T *args UNUSED)
1643{
1644 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1645 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
1646 return e_invalid_argument;
1647
1648 return NULL;
1649}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001650
1651 int
1652expand_set_completeslash(optexpand_T *args, int *numMatches, char_u ***matches)
1653{
1654 return expand_set_opt_string(
1655 args,
1656 p_csl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001657 ARRAY_LENGTH(p_csl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001658 numMatches,
1659 matches);
1660}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001661#endif
1662
1663#if defined(FEAT_CONCEAL) || defined(PROTO)
1664/*
1665 * The 'concealcursor' option is changed.
1666 */
1667 char *
1668did_set_concealcursor(optset_T *args)
1669{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001670 char_u **varp = (char_u **)args->os_varp;
1671
Christian Brabandtb39b2402023-11-29 11:34:05 +01001672 return did_set_option_listflag(*varp, (char_u *)COCU_ALL, args->os_errbuf,
1673 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001674}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001675
1676 int
1677expand_set_concealcursor(optexpand_T *args, int *numMatches, char_u ***matches)
1678{
1679 return expand_set_opt_listflag(args, (char_u*)COCU_ALL, numMatches, matches);
1680}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001681#endif
1682
1683/*
1684 * The 'cpoptions' option is changed.
1685 */
1686 char *
1687did_set_cpoptions(optset_T *args)
1688{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001689 char_u **varp = (char_u **)args->os_varp;
1690
Christian Brabandtb39b2402023-11-29 11:34:05 +01001691 return did_set_option_listflag(*varp, (char_u *)CPO_ALL, args->os_errbuf,
1692 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001693}
1694
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001695 int
1696expand_set_cpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
1697{
1698 return expand_set_opt_listflag(args, (char_u*)CPO_ALL, numMatches, matches);
1699}
1700
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001701#if defined(FEAT_CRYPT) || defined(PROTO)
1702/*
1703 * The 'cryptkey' option is changed.
1704 */
1705 char *
1706did_set_cryptkey(optset_T *args)
1707{
1708 // Make sure the ":set" command doesn't show the new value in the
1709 // history.
1710 remove_key_from_history();
1711
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02001712 if (args->os_op != OP_NONE)
1713 // Don't allow set+=/-=/^= as they can allow for substring guessing
1714 return e_invalid_argument;
1715
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001716 if (STRCMP(curbuf->b_p_key, args->os_oldval.string) != 0)
1717 {
1718 // Need to update the swapfile.
1719 ml_set_crypt_key(curbuf, args->os_oldval.string,
1720 *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
1721 changed_internal();
1722 }
Christian Brabandt19e6c4f2023-06-27 18:57:10 +01001723# ifdef FEAT_SODIUM
1724 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
1725 crypt_sodium_lock_key(args->os_newval.string);
1726# endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001727
1728 return NULL;
1729}
1730
1731/*
1732 * The 'cryptmethod' option is changed.
1733 */
1734 char *
1735did_set_cryptmethod(optset_T *args)
1736{
1737 char_u *p;
1738 char_u *s;
1739
1740 if (args->os_flags & OPT_LOCAL)
1741 p = curbuf->b_p_cm;
1742 else
1743 p = p_cm;
1744 if (check_opt_strings(p, p_cm_values, TRUE) != OK)
1745 return e_invalid_argument;
1746 else if (crypt_self_test() == FAIL)
1747 return e_invalid_argument;
1748
1749 // When setting the global value to empty, make it "zip".
1750 if (*p_cm == NUL)
1751 {
1752 free_string_option(p_cm);
1753 p_cm = vim_strsave((char_u *)"zip");
1754 }
1755 // When using ":set cm=name" the local value is going to be empty.
1756 // Do that here, otherwise the crypt functions will still use the
1757 // local value.
1758 if ((args->os_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1759 {
1760 free_string_option(curbuf->b_p_cm);
1761 curbuf->b_p_cm = empty_option;
1762 }
1763
1764 // Need to update the swapfile when the effective method changed.
1765 // Set "s" to the effective old value, "p" to the effective new
1766 // method and compare.
1767 if ((args->os_flags & OPT_LOCAL) && *args->os_oldval.string == NUL)
1768 s = p_cm; // was previously using the global value
1769 else
1770 s = args->os_oldval.string;
1771 if (*curbuf->b_p_cm == NUL)
1772 p = p_cm; // is now using the global value
1773 else
1774 p = curbuf->b_p_cm;
1775 if (STRCMP(s, p) != 0)
1776 ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1777
1778 // If the global value changes need to update the swapfile for all
1779 // buffers using that value.
1780 if ((args->os_flags & OPT_GLOBAL)
1781 && STRCMP(p_cm, args->os_oldval.string) != 0)
1782 {
1783 buf_T *buf;
1784
1785 FOR_ALL_BUFFERS(buf)
1786 if (buf != curbuf && *buf->b_p_cm == NUL)
1787 ml_set_crypt_key(buf, buf->b_p_key, args->os_oldval.string);
1788 }
1789 return NULL;
1790}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001791
1792 int
1793expand_set_cryptmethod(optexpand_T *args, int *numMatches, char_u ***matches)
1794{
1795 return expand_set_opt_string(
1796 args,
1797 p_cm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001798 ARRAY_LENGTH(p_cm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001799 numMatches,
1800 matches);
1801}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001802#endif
1803
1804#if (defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1805/*
1806 * The 'cscopequickfix' option is changed.
1807 */
1808 char *
1809did_set_cscopequickfix(optset_T *args UNUSED)
1810{
1811 char_u *p;
1812
1813 if (p_csqf == NULL)
1814 return NULL;
1815
1816 p = p_csqf;
1817 while (*p != NUL)
1818 {
1819 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
1820 || p[1] == NUL
1821 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
1822 || (p[2] != NUL && p[2] != ','))
1823 return e_invalid_argument;
1824 else if (p[2] == NUL)
1825 break;
1826 else
1827 p += 3;
1828 }
1829
1830 return NULL;
1831}
1832#endif
1833
1834#if defined(FEAT_SYN_HL) || defined(PROTO)
1835/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001836 * The 'cursorlineopt' option is changed.
1837 */
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001838 char *
1839did_set_cursorlineopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001840{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001841 char_u **varp = (char_u **)args->os_varp;
1842
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001843 // This could be changed to use opt_strings_flags() instead.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001844 if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001845 return e_invalid_argument;
1846
1847 return NULL;
1848}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001849
1850 int
1851expand_set_cursorlineopt(optexpand_T *args, int *numMatches, char_u ***matches)
1852{
1853 return expand_set_opt_string(
1854 args,
1855 p_culopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001856 ARRAY_LENGTH(p_culopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001857 numMatches,
1858 matches);
1859}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001860#endif
1861
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001862/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001863 * The 'debug' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001864 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001865 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001866did_set_debug(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001867{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001868 return did_set_opt_strings(p_debug, p_debug_values, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001869}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001870
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001871 int
1872expand_set_debug(optexpand_T *args, int *numMatches, char_u ***matches)
1873{
1874 return expand_set_opt_string(
1875 args,
1876 p_debug_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001877 ARRAY_LENGTH(p_debug_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001878 numMatches,
1879 matches);
1880}
1881
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001882#if defined(FEAT_DIFF) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001883/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001884 * The 'diffopt' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001885 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001886 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001887did_set_diffopt(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001888{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001889 if (diffopt_changed() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001890 return e_invalid_argument;
1891
1892 return NULL;
1893}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001894
1895 int
1896expand_set_diffopt(optexpand_T *args, int *numMatches, char_u ***matches)
1897{
1898 expand_T *xp = args->oe_xp;
1899
1900 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
1901 {
1902 // Within "algorithm:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001903 int algo_len = (int)STRLEN("algorithm:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001904 if (xp->xp_pattern - args->oe_set_arg >= algo_len &&
1905 STRNCMP(xp->xp_pattern - algo_len, "algorithm:", algo_len) == 0)
1906 {
1907 return expand_set_opt_string(
1908 args,
1909 p_dip_algorithm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001910 ARRAY_LENGTH(p_dip_algorithm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001911 numMatches,
1912 matches);
1913 }
1914 return FAIL;
1915 }
1916
1917 return expand_set_opt_string(
1918 args,
1919 p_dip_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001920 ARRAY_LENGTH(p_dip_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001921 numMatches,
1922 matches);
1923}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001924#endif
1925
1926/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001927 * The 'display' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001928 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001929 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001930did_set_display(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001931{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001932 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001933 return e_invalid_argument;
1934
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001935 (void)init_chartab();
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001936 return NULL;
1937}
1938
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001939 int
1940expand_set_display(optexpand_T *args, int *numMatches, char_u ***matches)
1941{
1942 return expand_set_opt_string(
1943 args,
1944 p_dy_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001945 ARRAY_LENGTH(p_dy_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001946 numMatches,
1947 matches);
1948}
1949
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001950/*
1951 * The 'eadirection' option is changed.
1952 */
1953 char *
1954did_set_eadirection(optset_T *args UNUSED)
1955{
1956 return did_set_opt_strings(p_ead, p_ead_values, FALSE);
1957}
1958
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001959 int
1960expand_set_eadirection(optexpand_T *args, int *numMatches, char_u ***matches)
1961{
1962 return expand_set_opt_string(
1963 args,
1964 p_ead_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001965 ARRAY_LENGTH(p_ead_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001966 numMatches,
1967 matches);
1968}
1969
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001970/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001971 * One of the 'encoding', 'fileencoding', 'termencoding' or 'makeencoding'
1972 * options is changed.
1973 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001974 char *
1975did_set_encoding(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001976{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001977 char_u **varp = (char_u **)args->os_varp;
1978 char_u **gvarp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001979 char *errmsg = NULL;
1980 char_u *p;
1981
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001982 // Get the global option to compare with, otherwise we would have to check
1983 // two values for all local options.
1984 gvarp = (char_u **)get_option_varp_scope(args->os_idx, OPT_GLOBAL);
1985
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001986 if (gvarp == &p_fenc)
1987 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001988 if (!curbuf->b_p_ma && args->os_flags != OPT_GLOBAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001989 errmsg = e_cannot_make_changes_modifiable_is_off;
1990 else if (vim_strchr(*varp, ',') != NULL)
1991 // No comma allowed in 'fileencoding'; catches confusing it
1992 // with 'fileencodings'.
1993 errmsg = e_invalid_argument;
1994 else
1995 {
1996 // May show a "+" in the title now.
1997 redraw_titles();
1998 // Add 'fileencoding' to the swap file.
1999 ml_setflags(curbuf);
2000 }
2001 }
2002 if (errmsg == NULL)
2003 {
2004 // canonize the value, so that STRCMP() can be used on it
2005 p = enc_canonize(*varp);
2006 if (p != NULL)
2007 {
2008 vim_free(*varp);
2009 *varp = p;
2010 }
2011 if (varp == &p_enc)
2012 {
2013 errmsg = mb_init();
2014 redraw_titles();
2015 }
2016 }
2017
2018#if defined(FEAT_GUI_GTK)
2019 if (errmsg == NULL && varp == &p_tenc && gui.in_use)
2020 {
2021 // GTK uses only a single encoding, and that is UTF-8.
2022 if (STRCMP(p_tenc, "utf-8") != 0)
2023 errmsg = e_cannot_be_changed_in_gtk_GUI;
2024 }
2025#endif
2026
2027 if (errmsg == NULL)
2028 {
2029#ifdef FEAT_KEYMAP
2030 // When 'keymap' is used and 'encoding' changes, reload the keymap
2031 // (with another encoding).
2032 if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
2033 (void)keymap_init();
2034#endif
2035
2036 // When 'termencoding' is not empty and 'encoding' changes or when
2037 // 'termencoding' changes, need to setup for keyboard input and
2038 // display output conversion.
2039 if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
2040 {
2041 if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
2042 || convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
2043 {
2044 semsg(_(e_cannot_convert_between_str_and_str),
2045 p_tenc, p_enc);
2046 errmsg = e_invalid_argument;
2047 }
2048 }
2049
2050#if defined(MSWIN)
K.Takatace3189d2023-02-15 19:13:43 +00002051 // $HOME, $VIM and $VIMRUNTIME may have characters in active code page.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002052 if (varp == &p_enc)
K.Takatace3189d2023-02-15 19:13:43 +00002053 {
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002054 init_homedir();
K.Takatace3189d2023-02-15 19:13:43 +00002055 init_vimdir();
2056 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002057#endif
2058 }
2059
2060 return errmsg;
2061}
2062
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002063 int
2064expand_set_encoding(optexpand_T *args, int *numMatches, char_u ***matches)
2065{
2066 return expand_set_opt_generic(
2067 args,
2068 get_encoding_name,
2069 numMatches,
2070 matches);
2071}
2072
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002073/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002074 * The 'eventignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002075 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002076 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002077did_set_eventignore(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002078{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002079 if (check_ei() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002080 return e_invalid_argument;
2081 return NULL;
2082}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002083
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002084 static char_u *
2085get_eventignore_name(expand_T *xp, int idx)
2086{
2087 // 'eventignore' allows special keyword "all" in addition to
2088 // all event names.
2089 if (idx == 0)
2090 return (char_u *)"all";
2091 return get_event_name_no_group(xp, idx - 1);
2092}
2093
2094 int
2095expand_set_eventignore(optexpand_T *args, int *numMatches, char_u ***matches)
2096{
2097 return expand_set_opt_generic(
2098 args,
2099 get_eventignore_name,
2100 numMatches,
2101 matches);
2102}
2103
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002104/*
2105 * The 'fileformat' option is changed.
2106 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002107 char *
2108did_set_fileformat(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002109{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002110 char_u **varp = (char_u **)args->os_varp;
2111
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002112 if (!curbuf->b_p_ma && !(args->os_flags & OPT_GLOBAL))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002113 return e_cannot_make_changes_modifiable_is_off;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002114 else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002115 return e_invalid_argument;
2116
2117 // may also change 'textmode'
2118 if (get_fileformat(curbuf) == EOL_DOS)
2119 curbuf->b_p_tx = TRUE;
2120 else
2121 curbuf->b_p_tx = FALSE;
2122 redraw_titles();
2123 // update flag in swap file
2124 ml_setflags(curbuf);
2125 // Redraw needed when switching to/from "mac": a CR in the text
2126 // will be displayed differently.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002127 if (get_fileformat(curbuf) == EOL_MAC || *args->os_oldval.string == 'm')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002128 redraw_curbuf_later(UPD_NOT_VALID);
2129
2130 return NULL;
2131}
2132
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002133 int
2134expand_set_fileformat(optexpand_T *args, int *numMatches, char_u ***matches)
2135{
2136 return expand_set_opt_string(
2137 args,
2138 p_ff_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002139 ARRAY_LENGTH(p_ff_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002140 numMatches,
2141 matches);
2142}
2143
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002144/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002145 * Function given to ExpandGeneric() to obtain the possible arguments of the
2146 * fileformat options.
2147 */
2148 char_u *
2149get_fileformat_name(expand_T *xp UNUSED, int idx)
2150{
2151 if (idx >= (int)ARRAY_LENGTH(p_ff_values))
2152 return NULL;
2153
2154 return (char_u*)p_ff_values[idx];
2155}
2156
2157/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002158 * The 'fileformats' option is changed.
2159 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002160 char *
2161did_set_fileformats(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002162{
2163 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
2164 return e_invalid_argument;
2165
2166 // also change 'textauto'
2167 if (*p_ffs == NUL)
2168 p_ta = FALSE;
2169 else
2170 p_ta = TRUE;
2171
2172 return NULL;
2173}
2174
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002175/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002176 * The 'filetype' or the 'syntax' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002177 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002178 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002179did_set_filetype_or_syntax(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002180{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002181 char_u **varp = (char_u **)args->os_varp;
2182
2183 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002184 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002185
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002186 args->os_value_changed = STRCMP(args->os_oldval.string, *varp) != 0;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002187
2188 // Since we check the value, there is no need to set P_INSECURE,
2189 // even when the value comes from a modeline.
2190 args->os_value_checked = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002191
2192 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002193}
2194
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002195#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002196/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002197 * The 'foldclose' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002198 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002199 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002200did_set_foldclose(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002201{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002202 return did_set_opt_strings(p_fcl, p_fcl_values, TRUE);
2203}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002204
2205 int
2206expand_set_foldclose(optexpand_T *args, int *numMatches, char_u ***matches)
2207{
2208 return expand_set_opt_string(
2209 args,
2210 p_fcl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002211 ARRAY_LENGTH(p_fcl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002212 numMatches,
2213 matches);
2214}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002215#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002216
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002217#if (defined(FEAT_EVAL) && defined(FEAT_FOLDING)) || defined(PROTO)
2218/*
2219 * The 'foldexpr' option is changed.
2220 */
2221 char *
2222did_set_foldexpr(optset_T *args)
2223{
2224 (void)did_set_optexpr(args);
2225 if (foldmethodIsExpr(curwin))
2226 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002227 return NULL;
2228}
2229#endif
2230
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002231#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002232/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002233 * The 'foldignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002234 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002235 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002236did_set_foldignore(optset_T *args UNUSED)
2237{
2238 if (foldmethodIsIndent(curwin))
2239 foldUpdateAll(curwin);
2240 return NULL;
2241}
2242
2243/*
2244 * The 'foldmarker' option is changed.
2245 */
2246 char *
2247did_set_foldmarker(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002248{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002249 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002250 char_u *p;
2251
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002252 p = vim_strchr(*varp, ',');
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002253 if (p == NULL)
2254 return e_comma_required;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002255 else if (p == *varp || p[1] == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002256 return e_invalid_argument;
2257 else if (foldmethodIsMarker(curwin))
2258 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002259
2260 return NULL;
2261}
2262
2263/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002264 * The 'foldmethod' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002265 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002266 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002267did_set_foldmethod(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002268{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002269 char_u **varp = (char_u **)args->os_varp;
2270
2271 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002272 || *curwin->w_p_fdm == NUL)
2273 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002274
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002275 foldUpdateAll(curwin);
2276 if (foldmethodIsDiff(curwin))
2277 newFoldLevel();
2278 return NULL;
2279}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002280
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002281 int
2282expand_set_foldmethod(optexpand_T *args, int *numMatches, char_u ***matches)
2283{
2284 return expand_set_opt_string(
2285 args,
2286 p_fdm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002287 ARRAY_LENGTH(p_fdm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002288 numMatches,
2289 matches);
2290}
2291
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002292/*
2293 * The 'foldopen' option is changed.
2294 */
2295 char *
2296did_set_foldopen(optset_T *args UNUSED)
2297{
2298 return did_set_opt_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
2299}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002300
2301 int
2302expand_set_foldopen(optexpand_T *args, int *numMatches, char_u ***matches)
2303{
2304 return expand_set_opt_string(
2305 args,
2306 p_fdo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002307 ARRAY_LENGTH(p_fdo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002308 numMatches,
2309 matches);
2310}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002311#endif
2312
2313/*
2314 * The 'formatoptions' option is changed.
2315 */
2316 char *
2317did_set_formatoptions(optset_T *args)
2318{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002319 char_u **varp = (char_u **)args->os_varp;
2320
Christian Brabandtb39b2402023-11-29 11:34:05 +01002321 return did_set_option_listflag(*varp, (char_u *)FO_ALL, args->os_errbuf,
2322 args->os_errbuflen);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002323}
2324
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002325 int
2326expand_set_formatoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2327{
2328 return expand_set_opt_listflag(args, (char_u*)FO_ALL, numMatches, matches);
2329}
2330
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002331#if defined(CURSOR_SHAPE) || defined(PROTO)
2332/*
2333 * The 'guicursor' option is changed.
2334 */
2335 char *
2336did_set_guicursor(optset_T *args UNUSED)
2337{
2338 return parse_shape_opt(SHAPE_CURSOR);
2339}
2340#endif
2341
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002342#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002343/*
2344 * The 'guifont' option is changed.
2345 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002346 char *
2347did_set_guifont(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002348{
2349 char_u *p;
2350 char *errmsg = NULL;
2351
2352 if (gui.in_use)
2353 {
2354 p = p_guifont;
2355# if defined(FEAT_GUI_GTK)
2356 // Put up a font dialog and let the user select a new value.
2357 // If this is cancelled go back to the old value but don't
2358 // give an error message.
2359 if (STRCMP(p, "*") == 0)
2360 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002361 p = gui_mch_font_dialog(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002362 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002363 p_guifont = (p != NULL) ? p : vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002364 }
2365# endif
2366 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
2367 {
2368# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
2369 if (STRCMP(p_guifont, "*") == 0)
2370 {
2371 // Dialog was cancelled: Keep the old value without giving
2372 // an error message.
2373 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002374 p_guifont = vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002375 }
2376 else
2377# endif
2378 errmsg = e_invalid_fonts;
2379 }
2380 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002381
2382 return errmsg;
2383}
2384
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002385/*
2386 * Expand the 'guifont' option. Only when GUI is being used. Each platform has
2387 * specific behaviors.
2388 */
2389 int
2390expand_set_guifont(optexpand_T *args, int *numMatches, char_u ***matches)
2391{
2392 if (!gui.in_use)
2393 return FAIL;
2394
2395# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
2396 char_u **varp = (char_u **)args->oe_varp;
2397 int wide = (varp == &p_guifontwide);
2398
2399 return expand_set_opt_callback(
2400 args, gui_mch_expand_font, &wide, numMatches, matches);
2401# else
2402 return FAIL;
2403# endif
2404}
2405
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002406# if defined(FEAT_XFONTSET) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002407/*
2408 * The 'guifontset' option is changed.
2409 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002410 char *
2411did_set_guifontset(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002412{
2413 char *errmsg = NULL;
2414
2415 if (STRCMP(p_guifontset, "*") == 0)
2416 errmsg = e_cant_select_fontset;
2417 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
2418 errmsg = e_invalid_fontset;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002419
2420 return errmsg;
2421}
2422# endif
2423
2424/*
2425 * The 'guifontwide' option is changed.
2426 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002427 char *
2428did_set_guifontwide(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002429{
2430 char *errmsg = NULL;
2431
2432 if (STRCMP(p_guifontwide, "*") == 0)
2433 errmsg = e_cant_select_wide_font;
2434 else if (gui_get_wide_font() == FAIL)
2435 errmsg = e_invalid_wide_font;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002436
2437 return errmsg;
2438}
2439#endif
2440
Erik S. V. Jansson8b1e7492024-02-24 14:26:52 +01002441#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002442/*
2443 * The 'guiligatures' option is changed.
2444 */
2445 char *
2446did_set_guiligatures(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002447{
2448 gui_set_ligatures();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002449 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002450}
2451#endif
2452
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002453#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002454/*
2455 * The 'guioptions' option is changed.
2456 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002457 char *
2458did_set_guioptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002459{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002460 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002461 char *errmsg;
2462
Christian Brabandtb39b2402023-11-29 11:34:05 +01002463 errmsg = did_set_option_listflag(*varp, (char_u *)GO_ALL, args->os_errbuf,
2464 args->os_errbuflen);
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002465 if (errmsg != NULL)
2466 return errmsg;
2467
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002468 gui_init_which_components(args->os_oldval.string);
2469 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002470}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002471
2472 int
2473expand_set_guioptions(optexpand_T *args, int *numMatches, char_u ***matches)
2474{
2475 return expand_set_opt_listflag(args, (char_u*)GO_ALL, numMatches, matches);
2476}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002477#endif
2478
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002479#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002480/*
2481 * The 'guitablabel' option is changed.
2482 */
2483 char *
2484did_set_guitablabel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002485{
2486 redraw_tabline = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002487 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002488}
2489#endif
2490
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002491/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002492 * The 'helpfile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002493 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002494 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002495did_set_helpfile(optset_T *args UNUSED)
2496{
2497 // May compute new values for $VIM and $VIMRUNTIME
2498 if (didset_vim)
2499 vim_unsetenv_ext((char_u *)"VIM");
2500 if (didset_vimruntime)
2501 vim_unsetenv_ext((char_u *)"VIMRUNTIME");
2502 return NULL;
2503}
2504
2505#if defined(FEAT_MULTI_LANG) || defined(PROTO)
2506/*
2507 * The 'helplang' option is changed.
2508 */
2509 char *
2510did_set_helplang(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002511{
2512 char *errmsg = NULL;
2513
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002514 // Check for "", "ab", "ab,cd", etc.
2515 for (char_u *s = p_hlg; *s != NUL; s += 3)
2516 {
2517 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
2518 {
2519 errmsg = e_invalid_argument;
2520 break;
2521 }
2522 if (s[2] == NUL)
2523 break;
2524 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002525
2526 return errmsg;
2527}
2528#endif
2529
2530/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002531 * The 'highlight' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002532 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002533 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002534did_set_highlight(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002535{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002536 if (highlight_changed() == FAIL)
2537 return e_invalid_argument; // invalid flags
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002538
2539 return NULL;
2540}
2541
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002542/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002543 * Expand 'highlight' option.
2544 */
2545 int
2546expand_set_highlight(optexpand_T *args, int *numMatches, char_u ***matches)
2547{
2548 char_u *p;
2549 expand_T *xp = args->oe_xp;
2550 static char_u hl_flags[HLF_COUNT] = HL_FLAGS;
Christian Brabandt3f168ec2023-10-02 23:21:11 +02002551 size_t i;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002552 int count = 0;
2553
2554 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2555 {
2556 // Right after a ':', meaning we just return all highlight names.
2557 return expand_set_opt_generic(
2558 args,
2559 get_highlight_name,
2560 numMatches,
2561 matches);
2562 }
2563
2564 if (*xp->xp_pattern == NUL)
2565 {
2566 // At beginning of a comma-separated list. Return the specific list of
2567 // supported occasions.
2568 *matches = ALLOC_MULT(char_u *, HLF_COUNT + 1);
2569 if (*matches == NULL)
2570 return FAIL;
2571
2572 // We still want to return the full option if it's requested.
2573 if (args->oe_include_orig_val)
2574 {
2575 p = vim_strsave(args->oe_opt_value);
2576 if (p == NULL)
2577 {
2578 VIM_CLEAR(*matches);
2579 return FAIL;
2580 }
2581 (*matches)[count++] = p;
2582 }
2583
2584 for (i = 0; i < HLF_COUNT; i++)
2585 {
2586 p = vim_strnsave(&hl_flags[i], 1);
2587 if (p == NULL)
2588 {
2589 if (count == 0)
2590 {
2591 VIM_CLEAR(*matches);
2592 return FAIL;
2593 }
2594 else
2595 break;
2596 }
2597 (*matches)[count++] = p;
2598 }
2599
2600 if (count == 0)
2601 {
2602 VIM_CLEAR(*matches);
2603 return FAIL;
2604 }
2605 *numMatches = count;
2606 return OK;
2607 }
2608
2609 // We are after the initial character (which indicates the occasion). We
2610 // already made sure we are not matching after a ':' above, so now we want
2611 // to match against display mode modifiers.
2612 // Since the xp_pattern starts from the beginning, we need to include it in
2613 // the returned match.
2614
2615 // Note: Keep this in sync with highlight_changed()
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002616 static char_u p_hl_mode_values[] =
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002617 {':', 'b', 'i', '-', 'n', 'r', 's', 'u', 'c', '2', 'd', '=', 't'};
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002618 size_t num_hl_modes = ARRAY_LENGTH(p_hl_mode_values);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002619
2620 *matches = ALLOC_MULT(char_u *, num_hl_modes);
2621 if (*matches == NULL)
2622 return FAIL;
2623
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002624 int pattern_len = xp->xp_pattern_len;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002625
2626 for (i = 0; i < num_hl_modes; i++)
2627 {
2628 // Don't allow duplicates as these are unique flags
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002629 char_u *dup = vim_strchr(xp->xp_pattern + 1, p_hl_mode_values[i]);
2630 if (dup != NULL && (int)(dup - xp->xp_pattern) < pattern_len)
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002631 continue;
2632
2633 // ':' only works by itself, not with other flags.
2634 if (pattern_len > 1 && p_hl_mode_values[i] == ':')
2635 continue;
2636
2637 p = vim_strnsave(xp->xp_pattern, pattern_len + 1);
2638 if (p == NULL)
2639 {
2640 if (i == 0)
2641 {
2642 VIM_CLEAR(*matches);
2643 return FAIL;
2644 }
2645 else
2646 break;
2647 }
2648 p[pattern_len] = p_hl_mode_values[i];
2649 p[pattern_len + 1] = NUL;
2650 (*matches)[count++] = p;
2651 }
2652 if (count == 0)
2653 {
2654 VIM_CLEAR(*matches);
2655 return FAIL;
2656 }
2657 *numMatches = count;
2658
2659 return OK;
2660}
2661
2662/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002663 * The 'titlestring' or the 'iconstring' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002664 */
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002665 static char *
2666parse_titleiconstring(optset_T *args UNUSED, int flagval UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002667{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002668#ifdef FEAT_STL_OPT
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002669 char_u **varp = (char_u **)args->os_varp;
2670
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002671 // NULL => statusline syntax
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002672 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002673 stl_syntax |= flagval;
2674 else
2675 stl_syntax &= ~flagval;
2676#endif
2677 did_set_title();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002678
2679 return NULL;
2680}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002681
2682/*
2683 * The 'iconstring' option is changed.
2684 */
2685 char *
2686did_set_iconstring(optset_T *args)
2687{
2688 int flagval = 0;
2689
2690#ifdef FEAT_STL_OPT
2691 flagval = STL_IN_ICON;
2692#endif
2693
2694 return parse_titleiconstring(args, flagval);
2695}
2696
2697#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
2698/*
2699 * The 'imactivatekey' option is changed.
2700 */
2701 char *
2702did_set_imactivatekey(optset_T *args UNUSED)
2703{
2704 if (!im_xim_isvalid_imactivate())
2705 return e_invalid_argument;
2706 return NULL;
2707}
2708#endif
2709
2710/*
2711 * The 'isident' or the 'iskeyword' or the 'isprint' or the 'isfname' option is
2712 * changed.
2713 */
2714 char *
2715did_set_isopt(optset_T *args)
2716{
2717 // 'isident', 'iskeyword', 'isprint or 'isfname' option: refill g_chartab[]
2718 // If the new option is invalid, use old value.
2719 // 'lisp' option: refill g_chartab[] for '-' char.
2720 if (init_chartab() == FAIL)
2721 {
2722 args->os_restore_chartab = TRUE;// need to restore the chartab.
2723 return e_invalid_argument; // error in value
2724 }
2725
2726 return NULL;
2727}
2728
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002729/*
2730 * The 'jumpoptions' option is changed.
2731 */
2732 char *
Yegappan Lakshmanan1926ae42023-09-21 16:36:28 +02002733did_set_jumpoptions(optset_T *args UNUSED)
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002734{
2735 if (opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE) != OK)
2736 return e_invalid_argument;
2737
2738 return NULL;
2739}
2740
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002741 int
2742expand_set_jumpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2743{
2744 return expand_set_opt_string(
2745 args,
2746 p_jop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002747 ARRAY_LENGTH(p_jop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002748 numMatches,
2749 matches);
2750}
2751
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002752#if defined(FEAT_KEYMAP) || defined(PROTO)
2753/*
2754 * The 'keymap' option is changed.
2755 */
2756 char *
2757did_set_keymap(optset_T *args)
2758{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002759 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002760 char *errmsg = NULL;
2761
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002762 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002763 errmsg = e_invalid_argument;
2764 else
2765 {
2766 int secure_save = secure;
2767
2768 // Reset the secure flag, since the value of 'keymap' has
2769 // been checked to be safe.
2770 secure = 0;
2771
2772 // load or unload key mapping tables
2773 errmsg = keymap_init();
2774
2775 secure = secure_save;
2776
2777 // Since we check the value, there is no need to set P_INSECURE,
2778 // even when the value comes from a modeline.
2779 args->os_value_checked = TRUE;
2780 }
2781
2782 if (errmsg == NULL)
2783 {
2784 if (*curbuf->b_p_keymap != NUL)
2785 {
2786 // Installed a new keymap, switch on using it.
2787 curbuf->b_p_iminsert = B_IMODE_LMAP;
2788 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
2789 curbuf->b_p_imsearch = B_IMODE_LMAP;
2790 }
2791 else
2792 {
2793 // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
2794 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
2795 curbuf->b_p_iminsert = B_IMODE_NONE;
2796 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
2797 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
2798 }
2799 if ((args->os_flags & OPT_LOCAL) == 0)
2800 {
2801 set_iminsert_global();
2802 set_imsearch_global();
2803 }
2804 status_redraw_curbuf();
2805 }
2806
2807 return errmsg;
2808}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002809#endif
2810
2811/*
2812 * The 'keymodel' option is changed.
2813 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002814 char *
2815did_set_keymodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002816{
2817 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
2818 return e_invalid_argument;
2819
2820 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
2821 km_startsel = (vim_strchr(p_km, 'a') != NULL);
2822 return NULL;
2823}
2824
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002825 int
2826expand_set_keymodel(optexpand_T *args, int *numMatches, char_u ***matches)
2827{
2828 return expand_set_opt_string(
2829 args,
2830 p_km_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002831 ARRAY_LENGTH(p_km_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002832 numMatches,
2833 matches);
2834}
2835
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002836/*
2837 * The 'keyprotocol' option is changed.
2838 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002839 char *
2840did_set_keyprotocol(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002841{
Gregory Anders3695d0e2023-09-29 20:17:20 +02002842 char_u *term = T_NAME;
2843 keyprot_T kpc = match_keyprotocol(term);
2844 if (kpc == KEYPROTOCOL_FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002845 return e_invalid_argument;
2846
Gregory Anders3695d0e2023-09-29 20:17:20 +02002847 apply_keyprotocol(term, kpc);
2848
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002849 return NULL;
2850}
2851
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002852 int
2853expand_set_keyprotocol(optexpand_T *args, int *numMatches, char_u ***matches)
2854{
2855 expand_T *xp = args->oe_xp;
2856 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2857 {
2858 // 'keyprotocol' only has well-defined terms for completion for the
2859 // protocol part after the colon.
2860 return expand_set_opt_string(
2861 args,
2862 p_kpc_protocol_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002863 ARRAY_LENGTH(p_kpc_protocol_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002864 numMatches,
2865 matches);
2866 }
2867 // Use expand_set_opt_string instead of returning FAIL so that we can
2868 // include the original value if args->oe_include_orig_val is set.
2869 static char *(empty[]) = {NULL};
2870 return expand_set_opt_string(args, empty, 0, numMatches, matches);
2871}
2872
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002873/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002874 * The 'lispoptions' option is changed.
2875 */
2876 char *
2877did_set_lispoptions(optset_T *args)
2878{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002879 char_u **varp = (char_u **)args->os_varp;
2880
2881 if (**varp != NUL
2882 && STRCMP(*varp, "expr:0") != 0
2883 && STRCMP(*varp, "expr:1") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002884 return e_invalid_argument;
2885
2886 return NULL;
2887}
2888
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002889 int
2890expand_set_lispoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2891{
2892 static char *(p_lop_values[]) = {"expr:0", "expr:1", NULL};
2893 return expand_set_opt_string(
2894 args,
2895 p_lop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002896 ARRAY_LENGTH(p_lop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002897 numMatches,
2898 matches);
2899}
2900
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002901/*
2902 * The 'matchpairs' option is changed.
2903 */
2904 char *
2905did_set_matchpairs(optset_T *args)
2906{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002907 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002908 char_u *p;
2909
2910 if (has_mbyte)
2911 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002912 for (p = *varp; *p != NUL; ++p)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002913 {
2914 int x2 = -1;
2915 int x3 = -1;
2916
2917 p += mb_ptr2len(p);
2918 if (*p != NUL)
2919 x2 = *p++;
2920 if (*p != NUL)
2921 {
2922 x3 = mb_ptr2char(p);
2923 p += mb_ptr2len(p);
2924 }
2925 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
2926 return e_invalid_argument;
2927 if (*p == NUL)
2928 break;
2929 }
2930 }
2931 else
2932 {
2933 // Check for "x:y,x:y"
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002934 for (p = *varp; *p != NUL; p += 4)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002935 {
2936 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
2937 return e_invalid_argument;
2938 if (p[3] == NUL)
2939 break;
2940 }
2941 }
2942
2943 return NULL;
2944}
2945
2946#if defined(FEAT_SPELL) || defined(PROTO)
2947/*
2948 * The 'mkspellmem' option is changed.
2949 */
2950 char *
2951did_set_mkspellmem(optset_T *args UNUSED)
2952{
2953 if (spell_check_msm() != OK)
2954 return e_invalid_argument;
2955
2956 return NULL;
2957}
2958#endif
2959
2960/*
2961 * The 'mouse' option is changed.
2962 */
2963 char *
2964did_set_mouse(optset_T *args)
2965{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002966 char_u **varp = (char_u **)args->os_varp;
2967
Christian Brabandtb39b2402023-11-29 11:34:05 +01002968 return did_set_option_listflag(*varp, (char_u *)MOUSE_ALL, args->os_errbuf,
2969 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002970}
2971
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002972 int
2973expand_set_mouse(optexpand_T *args, int *numMatches, char_u ***matches)
2974{
2975 return expand_set_opt_listflag(args, (char_u*)MOUSE_ALL, numMatches, matches);
2976}
2977
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002978/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002979 * The 'mousemodel' option is changed.
2980 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002981 char *
2982did_set_mousemodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002983{
2984 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
2985 return e_invalid_argument;
2986#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
Ken Takata18d0d292023-12-19 20:12:29 +01002987 else if (*p_mousem != *args->os_oldval.string)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002988 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
2989 // to create or delete the popup menus.
2990 gui_motif_update_mousemodel(root_menu);
2991#endif
2992
2993 return NULL;
2994}
2995
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002996 int
2997expand_set_mousemodel(optexpand_T *args, int *numMatches, char_u ***matches)
2998{
2999 return expand_set_opt_string(
3000 args,
3001 p_mousem_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003002 ARRAY_LENGTH(p_mousem_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003003 numMatches,
3004 matches);
3005}
3006
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003007#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3008 char *
3009did_set_mouseshape(optset_T *args UNUSED)
3010{
3011 char *errmsg = NULL;
3012
3013 errmsg = parse_shape_opt(SHAPE_MOUSE);
3014 update_mouseshape(-1);
3015
3016 return errmsg;
3017}
3018#endif
3019
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003020/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003021 * The 'nrformats' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003022 */
3023 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003024did_set_nrformats(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003025{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003026 char_u **varp = (char_u **)args->os_varp;
3027
3028 return did_set_opt_strings(*varp, p_nf_values, TRUE);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003029}
3030
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003031 int
3032expand_set_nrformats(optexpand_T *args, int *numMatches, char_u ***matches)
3033{
3034 return expand_set_opt_string(
3035 args,
3036 p_nf_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003037 ARRAY_LENGTH(p_nf_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003038 numMatches,
3039 matches);
3040}
3041
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003042#if defined(FEAT_EVAL) || defined(PROTO)
3043/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003044 * One of the '*expr' options is changed: 'balloonexpr', 'diffexpr',
3045 * 'foldexpr', 'foldtext', 'formatexpr', 'includeexpr', 'indentexpr',
3046 * 'patchexpr', 'printexpr' and 'charconvert'.
3047 *
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003048 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003049 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003050did_set_optexpr(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003051{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003052 char_u **varp = (char_u **)args->os_varp;
3053
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003054 // If the option value starts with <SID> or s:, then replace that with
3055 // the script identifier.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003056 char_u *name = get_scriptlocal_funcname(*varp);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003057 if (name != NULL)
3058 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003059 free_string_option(*varp);
3060 *varp = name;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003061 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003062
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003063 return NULL;
3064}
3065#endif
3066
3067/*
3068 * The 'pastetoggle' option is changed.
3069 */
3070 char *
3071did_set_pastetoggle(optset_T *args UNUSED)
3072{
3073 char_u *p;
3074
3075 // translate key codes like in a mapping
3076 if (*p_pt)
3077 {
zeertzjq7e0bae02023-08-11 23:15:38 +02003078 (void)replace_termcodes(p_pt, &p, 0,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003079 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
3080 if (p != NULL)
3081 {
3082 free_string_option(p_pt);
3083 p_pt = p;
3084 }
3085 }
3086
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003087 return NULL;
3088}
3089
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003090#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3091/*
3092 * The 'previewpopup' option is changed.
3093 */
3094 char *
3095did_set_previewpopup(optset_T *args UNUSED)
3096{
3097 if (parse_previewpopup(NULL) == FAIL)
3098 return e_invalid_argument;
3099
3100 return NULL;
3101}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003102
3103 int
3104expand_set_popupoption(optexpand_T *args, int *numMatches, char_u ***matches)
3105{
3106 expand_T *xp = args->oe_xp;
3107
3108 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3109 {
3110 // Within "highlight:"/"border:"/"align:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003111 int border_len = (int)STRLEN("border:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003112 if (xp->xp_pattern - args->oe_set_arg >= border_len &&
3113 STRNCMP(xp->xp_pattern - border_len, "border:", border_len) == 0)
3114 {
3115 return expand_set_opt_string(
3116 args,
3117 p_popup_option_border_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003118 ARRAY_LENGTH(p_popup_option_border_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003119 numMatches,
3120 matches);
3121 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003122 int align_len = (int)STRLEN("align:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003123 if (xp->xp_pattern - args->oe_set_arg >= align_len &&
3124 STRNCMP(xp->xp_pattern - align_len, "align:", align_len) == 0)
3125 {
3126 return expand_set_opt_string(
3127 args,
3128 p_popup_option_align_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003129 ARRAY_LENGTH(p_popup_option_align_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003130 numMatches,
3131 matches);
3132 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003133 int highlight_len = (int)STRLEN("highlight:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003134 if (xp->xp_pattern - args->oe_set_arg >= highlight_len &&
3135 STRNCMP(xp->xp_pattern - highlight_len, "highlight:", highlight_len) == 0)
3136 {
3137 // Return the list of all highlight names
3138 return expand_set_opt_generic(
3139 args,
3140 get_highlight_name,
3141 numMatches,
3142 matches);
3143 }
3144 return FAIL;
3145 }
3146
3147 return expand_set_opt_string(
3148 args,
3149 p_popup_option_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003150 ARRAY_LENGTH(p_popup_option_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003151 numMatches,
3152 matches);
3153}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003154#endif
3155
3156#if defined(FEAT_POSTSCRIPT) || defined(PROTO)
3157/*
3158 * The 'printencoding' option is changed.
3159 */
3160 char *
3161did_set_printencoding(optset_T *args UNUSED)
3162{
3163 char_u *s, *p;
3164
3165 // Canonize 'printencoding' if VIM standard one
3166 p = enc_canonize(p_penc);
3167 if (p != NULL)
3168 {
3169 vim_free(p_penc);
3170 p_penc = p;
3171 }
3172 else
3173 {
3174 // Ensure lower case and '-' for '_'
3175 for (s = p_penc; *s != NUL; s++)
3176 {
3177 if (*s == '_')
3178 *s = '-';
3179 else
3180 *s = TOLOWER_ASC(*s);
3181 }
3182 }
3183
3184 return NULL;
3185}
3186#endif
3187
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003188#if defined(FEAT_PRINTER) || defined(PROTO)
3189
3190 static char_u *
3191get_printoptions_names(expand_T *xp UNUSED, int idx)
3192{
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003193 if (idx >= (int)ARRAY_LENGTH(printer_opts))
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003194 return NULL;
3195 return (char_u*)printer_opts[idx].name;
3196}
3197
3198/*
3199 * Expand 'printoptions' option
3200 */
3201 int
3202expand_set_printoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3203{
3204 return expand_set_opt_generic(
3205 args,
3206 get_printoptions_names,
3207 numMatches,
3208 matches);
3209}
3210#endif
3211
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003212#if defined(FEAT_STL_OPT) || defined(PROTO)
3213/*
3214 * The 'statusline' or the 'tabline' or the 'rulerformat' option is changed.
3215 * "rulerformat" is TRUE if the 'rulerformat' option is changed.
3216 */
3217 static char *
3218parse_statustabline_rulerformat(optset_T *args, int rulerformat)
3219{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003220 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003221 char_u *s;
3222 char *errmsg = NULL;
3223 int wid;
3224
3225 if (rulerformat) // reset ru_wid first
3226 ru_wid = 0;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003227 s = *varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003228 if (rulerformat && *s == '%')
3229 {
3230 // set ru_wid if 'ruf' starts with "%99("
3231 if (*++s == '-') // ignore a '-'
3232 s++;
3233 wid = getdigits(&s);
3234 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
3235 ru_wid = wid;
3236 else
3237 errmsg = check_stl_option(p_ruf);
3238 }
3239 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
3240 else if (rulerformat || s[0] != '%' || s[1] != '!')
3241 errmsg = check_stl_option(s);
3242 if (rulerformat && errmsg == NULL)
3243 comp_col();
3244
3245 return errmsg;
3246}
3247#endif
3248
3249#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
3250/*
3251 * The 'renderoptions' option is changed.
3252 */
3253 char *
3254did_set_renderoptions(optset_T *args UNUSED)
3255{
3256 if (!gui_mch_set_rendering_options(p_rop))
3257 return e_invalid_argument;
3258
3259 return NULL;
3260}
3261#endif
3262
3263#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3264/*
3265 * The 'rightleftcmd' option is changed.
3266 */
3267 char *
3268did_set_rightleftcmd(optset_T *args)
3269{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003270 char_u **varp = (char_u **)args->os_varp;
3271
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003272 // Currently only "search" is a supported value.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003273 if (**varp != NUL && STRCMP(*varp, "search") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003274 return e_invalid_argument;
3275
3276 return NULL;
3277}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003278
3279 int
3280expand_set_rightleftcmd(optexpand_T *args, int *numMatches, char_u ***matches)
3281{
3282 static char *(p_rlc_values[]) = {"search", NULL};
3283 return expand_set_opt_string(
3284 args,
3285 p_rlc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003286 ARRAY_LENGTH(p_rlc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003287 numMatches,
3288 matches);
3289}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003290#endif
3291
3292#if defined(FEAT_STL_OPT) || defined(PROTO)
3293/*
3294 * The 'rulerformat' option is changed.
3295 */
3296 char *
3297did_set_rulerformat(optset_T *args)
3298{
3299 return parse_statustabline_rulerformat(args, TRUE);
3300}
3301#endif
3302
3303/*
3304 * The 'scrollopt' option is changed.
3305 */
3306 char *
3307did_set_scrollopt(optset_T *args UNUSED)
3308{
3309 return did_set_opt_strings(p_sbo, p_scbopt_values, TRUE);
3310}
3311
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003312 int
3313expand_set_scrollopt(optexpand_T *args, int *numMatches, char_u ***matches)
3314{
3315 return expand_set_opt_string(
3316 args,
3317 p_scbopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003318 ARRAY_LENGTH(p_scbopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003319 numMatches,
3320 matches);
3321}
3322
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003323/*
3324 * The 'selection' option is changed.
3325 */
3326 char *
3327did_set_selection(optset_T *args UNUSED)
3328{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003329 if (*p_sel == NUL || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003330 return e_invalid_argument;
3331
3332 return NULL;
3333}
3334
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003335 int
3336expand_set_selection(optexpand_T *args, int *numMatches, char_u ***matches)
3337{
3338 return expand_set_opt_string(
3339 args,
3340 p_sel_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003341 ARRAY_LENGTH(p_sel_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003342 numMatches,
3343 matches);
3344}
3345
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003346/*
3347 * The 'selectmode' option is changed.
3348 */
3349 char *
3350did_set_selectmode(optset_T *args UNUSED)
3351{
3352 return did_set_opt_strings(p_slm, p_slm_values, TRUE);
3353}
3354
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003355 int
3356expand_set_selectmode(optexpand_T *args, int *numMatches, char_u ***matches)
3357{
3358 return expand_set_opt_string(
3359 args,
3360 p_slm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003361 ARRAY_LENGTH(p_slm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003362 numMatches,
3363 matches);
3364}
3365
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003366#if defined(FEAT_SESSION) || defined(PROTO)
3367/*
3368 * The 'sessionoptions' option is changed.
3369 */
3370 char *
3371did_set_sessionoptions(optset_T *args)
3372{
3373 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
3374 return e_invalid_argument;
3375 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
3376 {
3377 // Don't allow both "sesdir" and "curdir".
3378 (void)opt_strings_flags(args->os_oldval.string, p_ssop_values,
3379 &ssop_flags, TRUE);
3380 return e_invalid_argument;
3381 }
3382
3383 return NULL;
3384}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003385
3386 int
3387expand_set_sessionoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3388{
3389 return expand_set_opt_string(
3390 args,
3391 p_ssop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003392 ARRAY_LENGTH(p_ssop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003393 numMatches,
3394 matches);
3395}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003396#endif
3397
3398/*
3399 * The 'shortmess' option is changed.
3400 */
3401 char *
3402did_set_shortmess(optset_T *args)
3403{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003404 char_u **varp = (char_u **)args->os_varp;
3405
Christian Brabandtb39b2402023-11-29 11:34:05 +01003406 return did_set_option_listflag(*varp, (char_u *)SHM_ALL, args->os_errbuf,
3407 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003408}
3409
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003410 int
3411expand_set_shortmess(optexpand_T *args, int *numMatches, char_u ***matches)
3412{
3413 return expand_set_opt_listflag(args, (char_u*)SHM_ALL, numMatches, matches);
3414}
3415
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003416#if defined(FEAT_LINEBREAK) || defined(PROTO)
3417/*
3418 * The 'showbreak' option is changed.
3419 */
3420 char *
3421did_set_showbreak(optset_T *args)
3422{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003423 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003424 char_u *s;
3425
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003426 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003427 {
3428 if (ptr2cells(s) != 1)
3429 return e_showbreak_contains_unprintable_or_wide_character;
3430 MB_PTR_ADV(s);
3431 }
3432
3433 return NULL;
3434}
3435#endif
3436
3437/*
3438 * The 'showcmdloc' option is changed.
3439 */
3440 char *
3441did_set_showcmdloc(optset_T *args UNUSED)
3442{
zeertzjqc27fcf42024-03-01 23:01:43 +01003443 char *errmsg = did_set_opt_strings(p_sloc, p_sloc_values, FALSE);
3444
3445 if (errmsg == NULL)
3446 comp_col();
3447
3448 return errmsg;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003449}
3450
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003451 int
3452expand_set_showcmdloc(optexpand_T *args, int *numMatches, char_u ***matches)
3453{
3454 return expand_set_opt_string(
3455 args,
3456 p_sloc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003457 ARRAY_LENGTH(p_sloc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003458 numMatches,
3459 matches);
3460}
3461
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003462#if defined(FEAT_SIGNS) || defined(PROTO)
3463/*
3464 * The 'signcolumn' option is changed.
3465 */
3466 char *
3467did_set_signcolumn(optset_T *args)
3468{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003469 char_u **varp = (char_u **)args->os_varp;
3470
3471 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003472 return e_invalid_argument;
3473 // When changing the 'signcolumn' to or from 'number', recompute the
3474 // width of the number column if 'number' or 'relativenumber' is set.
3475 if (((*args->os_oldval.string == 'n' && args->os_oldval.string[1] == 'u')
3476 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
3477 && (curwin->w_p_nu || curwin->w_p_rnu))
3478 curwin->w_nrwidth_line_count = 0;
3479
3480 return NULL;
3481}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003482
3483 int
3484expand_set_signcolumn(optexpand_T *args, int *numMatches, char_u ***matches)
3485{
3486 return expand_set_opt_string(
3487 args,
3488 p_scl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003489 ARRAY_LENGTH(p_scl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003490 numMatches,
3491 matches);
3492}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003493#endif
3494
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003495#if defined(FEAT_SPELL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003496/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003497 * The 'spellcapcheck' option is changed.
3498 */
3499 char *
3500did_set_spellcapcheck(optset_T *args UNUSED)
3501{
3502 // compile the regexp program.
3503 return compile_cap_prog(curwin->w_s);
3504}
3505
3506/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003507 * The 'spellfile' option is changed.
3508 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003509 char *
3510did_set_spellfile(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003511{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003512 char_u **varp = (char_u **)args->os_varp;
3513
3514 if (!valid_spellfile(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003515 return e_invalid_argument;
3516
3517 // If there is a window for this buffer in which 'spell' is set load the
3518 // wordlists.
3519 return did_set_spell_option(TRUE);
3520}
3521
3522/*
3523 * The 'spell' option is changed.
3524 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003525 char *
3526did_set_spelllang(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003527{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003528 char_u **varp = (char_u **)args->os_varp;
3529
3530 if (!valid_spelllang(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003531 return e_invalid_argument;
3532
3533 // If there is a window for this buffer in which 'spell' is set load the
3534 // wordlists.
3535 return did_set_spell_option(FALSE);
3536}
3537
3538/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003539 * The 'spelloptions' option is changed.
3540 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003541 char *
3542did_set_spelloptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003543{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003544 char_u **varp = (char_u **)args->os_varp;
3545
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003546 if (**varp != NUL && STRCMP(*varp, "camel") != 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003547 return e_invalid_argument;
3548
3549 return NULL;
3550}
3551
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003552 int
3553expand_set_spelloptions(optexpand_T *args, int *numMatches, char_u ***matches)
3554{
3555 static char *(p_spo_values[]) = {"camel", NULL};
3556 return expand_set_opt_string(
3557 args,
3558 p_spo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003559 ARRAY_LENGTH(p_spo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003560 numMatches,
3561 matches);
3562}
3563
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003564/*
3565 * The 'spellsuggest' option is changed.
3566 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003567 char *
3568did_set_spellsuggest(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003569{
3570 if (spell_check_sps() != OK)
3571 return e_invalid_argument;
3572
3573 return NULL;
3574}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003575
3576 int
3577expand_set_spellsuggest(optexpand_T *args, int *numMatches, char_u ***matches)
3578{
3579 return expand_set_opt_string(
3580 args,
3581 p_sps_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003582 ARRAY_LENGTH(p_sps_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003583 numMatches,
3584 matches);
3585}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003586#endif
3587
3588/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003589 * The 'splitkeep' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003590 */
3591 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003592did_set_splitkeep(optset_T *args UNUSED)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003593{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003594 return did_set_opt_strings(p_spk, p_spk_values, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003595}
3596
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003597 int
3598expand_set_splitkeep(optexpand_T *args, int *numMatches, char_u ***matches)
3599{
3600 return expand_set_opt_string(
3601 args,
3602 p_spk_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003603 ARRAY_LENGTH(p_spk_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003604 numMatches,
3605 matches);
3606}
3607
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00003608#if defined(FEAT_STL_OPT) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003609/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003610 * The 'statusline' option is changed.
3611 */
3612 char *
3613did_set_statusline(optset_T *args)
3614{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003615 return parse_statustabline_rulerformat(args, FALSE);
3616}
3617#endif
3618
3619/*
3620 * The 'swapsync' option is changed.
3621 */
3622 char *
3623did_set_swapsync(optset_T *args UNUSED)
3624{
3625 return did_set_opt_strings(p_sws, p_sws_values, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003626}
3627
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003628 int
3629expand_set_swapsync(optexpand_T *args, int *numMatches, char_u ***matches)
3630{
3631 return expand_set_opt_string(
3632 args,
3633 p_sws_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003634 ARRAY_LENGTH(p_sws_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003635 numMatches,
3636 matches);
3637}
3638
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003639/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003640 * The 'switchbuf' option is changed.
3641 */
3642 char *
3643did_set_switchbuf(optset_T *args UNUSED)
3644{
3645 return did_set_opt_flags(p_swb, p_swb_values, &swb_flags, TRUE);
3646}
3647
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003648 int
3649expand_set_switchbuf(optexpand_T *args, int *numMatches, char_u ***matches)
3650{
3651 return expand_set_opt_string(
3652 args,
3653 p_swb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003654 ARRAY_LENGTH(p_swb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003655 numMatches,
3656 matches);
3657}
3658
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003659#if defined(FEAT_STL_OPT) || defined(PROTO)
3660/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003661 * The 'tabline' option is changed.
3662 */
3663 char *
3664did_set_tabline(optset_T *args)
3665{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003666 return parse_statustabline_rulerformat(args, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003667}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003668#endif
3669
3670/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003671 * The 'tagcase' option is changed.
3672 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003673 char *
3674did_set_tagcase(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003675{
3676 unsigned int *flags;
3677 char_u *p;
3678
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003679 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003680 {
3681 p = curbuf->b_p_tc;
3682 flags = &curbuf->b_tc_flags;
3683 }
3684 else
3685 {
3686 p = p_tc;
3687 flags = &tc_flags;
3688 }
3689
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003690 if ((args->os_flags & OPT_LOCAL) && *p == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003691 // make the local value empty: use the global value
3692 *flags = 0;
3693 else if (*p == NUL
3694 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
3695 return e_invalid_argument;
3696
3697 return NULL;
3698}
3699
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003700 int
3701expand_set_tagcase(optexpand_T *args, int *numMatches, char_u ***matches)
3702{
3703 return expand_set_opt_string(
3704 args,
3705 p_tc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003706 ARRAY_LENGTH(p_tc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003707 numMatches,
3708 matches);
3709}
3710
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003711/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003712 * The 'term' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003713 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003714 char *
3715did_set_term(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003716{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003717 if (T_NAME[0] == NUL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003718 return e_cannot_set_term_to_empty_string;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003719#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003720 if (gui.in_use)
3721 return e_cannot_change_term_in_GUI;
3722 if (term_is_gui(T_NAME))
3723 return e_use_gui_to_start_GUI;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003724#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003725 if (set_termname(T_NAME) == FAIL)
3726 return e_not_found_in_termcap;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003727
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003728 // Screen colors may have changed.
3729 redraw_later_clear();
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003730
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003731 return NULL;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003732}
3733
3734/*
3735 * Some terminal option (t_xxx) is changed
3736 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003737 char *
3738did_set_term_option(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003739{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003740 char_u **varp = (char_u **)args->os_varp;
3741
3742 if (!full_screen)
3743 return NULL;
3744
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003745 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
3746 if (varp == &T_CCO)
3747 {
3748 int colors = atoi((char *)T_CCO);
3749
3750 // Only reinitialize colors if t_Co value has really changed to
3751 // avoid expensive reload of colorscheme if t_Co is set to the
3752 // same value multiple times.
3753 if (colors != t_colors)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003754 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003755 t_colors = colors;
3756 if (t_colors <= 1)
3757 {
3758 vim_free(T_CCO);
3759 T_CCO = empty_option;
3760 }
3761#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3762 if (is_term_win32())
3763 {
3764 swap_tcap();
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003765 args->os_did_swaptcap = TRUE;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003766 }
3767#endif
3768 // We now have a different color setup, initialize it again.
3769 init_highlight(TRUE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003770 }
3771 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003772 ttest(FALSE);
3773 if (varp == &T_ME)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003774 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003775 out_str(T_ME);
3776 redraw_later(UPD_CLEAR);
3777#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3778 // Since t_me has been set, this probably means that the user
3779 // wants to use this as default colors. Need to reset default
3780 // background/foreground colors.
3781# ifdef VIMDLL
3782 if (!gui.in_use && !gui.starting)
3783# endif
3784 mch_set_normal_colors();
3785#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003786 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003787 if (varp == &T_BE && termcap_active)
3788 {
3789 MAY_WANT_TO_LOG_THIS;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003790
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003791 if (*T_BE == NUL)
3792 // When clearing t_BE we assume the user no longer wants
3793 // bracketed paste, thus disable it by writing t_BD.
3794 out_str(T_BD);
3795 else
3796 out_str(T_BE);
3797 }
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003798
3799 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003800}
3801
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003802#if defined(FEAT_TERMINAL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003803/*
3804 * The 'termwinkey' option is changed.
3805 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003806 char *
3807did_set_termwinkey(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003808{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003809 if (*curwin->w_p_twk != NUL && string_to_key(curwin->w_p_twk, TRUE) == 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003810 return e_invalid_argument;
3811
3812 return NULL;
3813}
3814
3815/*
3816 * The 'termwinsize' option is changed.
3817 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003818 char *
3819did_set_termwinsize(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003820{
3821 char_u *p;
3822
3823 if (*curwin->w_p_tws == NUL)
3824 return NULL;
3825
3826 p = skipdigits(curwin->w_p_tws);
3827 if (p == curwin->w_p_tws
3828 || (*p != 'x' && *p != '*')
3829 || *skipdigits(p + 1) != NUL)
3830 return e_invalid_argument;
3831
3832 return NULL;
3833}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003834
3835# if defined(MSWIN) || defined(PROTO)
3836/*
3837 * The 'termwintype' option is changed.
3838 */
3839 char *
3840did_set_termwintype(optset_T *args UNUSED)
3841{
3842 return did_set_opt_strings(p_twt, p_twt_values, FALSE);
3843}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003844
3845 int
3846expand_set_termwintype(optexpand_T *args, int *numMatches, char_u ***matches)
3847{
3848 return expand_set_opt_string(
3849 args,
3850 p_twt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003851 ARRAY_LENGTH(p_twt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003852 numMatches,
3853 matches);
3854}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003855# endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003856#endif
3857
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003858/*
3859 * The 'titlestring' option is changed.
3860 */
3861 char *
3862did_set_titlestring(optset_T *args)
3863{
3864 int flagval = 0;
3865
3866#ifdef FEAT_STL_OPT
3867 flagval = STL_IN_TITLE;
3868#endif
3869 return parse_titleiconstring(args, flagval);
3870}
3871
3872#if (defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)) || defined(PROTO)
3873/*
3874 * The 'toolbar' option is changed.
3875 */
3876 char *
3877did_set_toolbar(optset_T *args UNUSED)
3878{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003879 if (opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags,
3880 TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003881 return e_invalid_argument;
3882
3883 out_flush();
3884 gui_mch_show_toolbar((toolbar_flags &
3885 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
3886 return NULL;
3887}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003888
3889 int
3890expand_set_toolbar(optexpand_T *args, int *numMatches, char_u ***matches)
3891{
3892 return expand_set_opt_string(
3893 args,
3894 p_toolbar_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003895 ARRAY_LENGTH(p_toolbar_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003896 numMatches,
3897 matches);
3898}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003899#endif
3900
3901#if (defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)) || defined(PROTO)
3902/*
3903 * The 'toolbariconsize' option is changed. GTK+ 2 only.
3904 */
3905 char *
3906did_set_toolbariconsize(optset_T *args UNUSED)
3907{
3908 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
3909 return e_invalid_argument;
3910
3911 out_flush();
3912 gui_mch_show_toolbar((toolbar_flags &
3913 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
3914 return NULL;
3915}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003916
3917 int
3918expand_set_toolbariconsize(optexpand_T *args, int *numMatches, char_u ***matches)
3919{
3920 return expand_set_opt_string(
3921 args,
3922 p_tbis_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003923 ARRAY_LENGTH(p_tbis_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003924 numMatches,
3925 matches);
3926}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003927#endif
3928
3929#if defined(UNIX) || defined(VMS) || defined(PROTO)
3930/*
3931 * The 'ttymouse' option is changed.
3932 */
3933 char *
3934did_set_ttymouse(optset_T *args UNUSED)
3935{
3936 char *errmsg = NULL;
3937
3938 // Switch the mouse off before changing the escape sequences used for
3939 // that.
3940 mch_setmouse(FALSE);
3941 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
3942 errmsg = e_invalid_argument;
3943 else
3944 check_mouse_termcode();
3945 if (termcap_active)
3946 setmouse(); // may switch it on again
3947
3948 return errmsg;
3949}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003950
3951 int
3952expand_set_ttymouse(optexpand_T *args, int *numMatches, char_u ***matches)
3953{
3954 return expand_set_opt_string(
3955 args,
3956 p_ttym_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003957 ARRAY_LENGTH(p_ttym_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003958 numMatches,
3959 matches);
3960}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003961#endif
3962
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003963#if defined(FEAT_VARTABS) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003964/*
3965 * The 'varsofttabstop' option is changed.
3966 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003967 char *
3968did_set_varsofttabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003969{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003970 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003971 char_u *cp;
3972
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003973 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00003974 VIM_CLEAR(curbuf->b_p_vsts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003975 else
3976 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003977 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003978 {
3979 if (vim_isdigit(*cp))
3980 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003981 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003982 continue;
3983 return e_invalid_argument;
3984 }
3985
3986 int *oldarray = curbuf->b_p_vsts_array;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003987 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003988 {
3989 if (oldarray)
3990 vim_free(oldarray);
3991 }
3992 else
3993 return e_invalid_argument;
3994 }
3995
3996 return NULL;
3997}
3998
3999/*
4000 * The 'vartabstop' option is changed.
4001 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004002 char *
4003did_set_vartabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004004{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004005 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004006 char_u *cp;
4007
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004008 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004009 VIM_CLEAR(curbuf->b_p_vts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004010 else
4011 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004012 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004013 {
4014 if (vim_isdigit(*cp))
4015 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004016 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004017 continue;
4018 return e_invalid_argument;
4019 }
4020
4021 int *oldarray = curbuf->b_p_vts_array;
4022
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004023 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004024 {
4025 vim_free(oldarray);
4026# ifdef FEAT_FOLDING
4027 if (foldmethodIsIndent(curwin))
4028 foldUpdateAll(curwin);
4029# endif
4030 }
4031 else
4032 return e_invalid_argument;
4033 }
4034
4035 return NULL;
4036}
4037#endif
4038
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004039/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004040 * The 'verbosefile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004041 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004042 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004043did_set_verbosefile(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004044{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004045 verbose_stop();
4046 if (*p_vfile != NUL && verbose_open() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004047 return e_invalid_argument;
4048
4049 return NULL;
4050}
4051
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004052#if defined(FEAT_SESSION) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004053/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004054 * The 'viewoptions' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004055 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004056 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004057did_set_viewoptions(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004058{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004059 return did_set_opt_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004060}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004061#endif
4062
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004063#if defined(FEAT_VIMINFO) || defined(PROTO)
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004064/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004065 * The 'viminfo' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004066 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004067 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004068did_set_viminfo(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004069{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004070 char_u *s;
4071 char *errmsg = NULL;
4072
4073 for (s = p_viminfo; *s;)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004074 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004075 // Check it's a valid character
4076 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
4077 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01004078 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004079 break;
4080 }
4081 if (*s == 'n') // name is always last one
4082 break;
4083 else if (*s == 'r') // skip until next ','
4084 {
4085 while (*++s && *s != ',')
4086 ;
4087 }
4088 else if (*s == '%')
4089 {
4090 // optional number
4091 while (vim_isdigit(*++s))
4092 ;
4093 }
4094 else if (*s == '!' || *s == 'h' || *s == 'c')
4095 ++s; // no extra chars
4096 else // must have a number
4097 {
4098 while (vim_isdigit(*++s))
4099 ;
4100
4101 if (!VIM_ISDIGIT(*(s - 1)))
4102 {
4103 if (args->os_errbuf != NULL)
4104 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01004105 vim_snprintf(args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004106 _(e_missing_number_after_angle_str_angle),
4107 transchar_byte(*(s - 1)));
4108 errmsg = args->os_errbuf;
4109 }
4110 else
4111 errmsg = "";
4112 break;
4113 }
4114 }
4115 if (*s == ',')
4116 ++s;
4117 else if (*s)
4118 {
4119 if (args->os_errbuf != NULL)
4120 errmsg = e_missing_comma;
4121 else
4122 errmsg = "";
4123 break;
4124 }
4125 }
4126 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
4127 errmsg = e_must_specify_a_value;
4128
4129 return errmsg;
4130}
4131#endif
4132
4133/*
4134 * The 'virtualedit' option is changed.
4135 */
4136 char *
4137did_set_virtualedit(optset_T *args)
4138{
4139 char_u *ve = p_ve;
4140 unsigned int *flags = &ve_flags;
4141
4142 if (args->os_flags & OPT_LOCAL)
4143 {
4144 ve = curwin->w_p_ve;
4145 flags = &curwin->w_ve_flags;
4146 }
4147
4148 if ((args->os_flags & OPT_LOCAL) && *ve == NUL)
4149 // make the local value empty: use the global value
4150 *flags = 0;
4151 else
4152 {
4153 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
4154 return e_invalid_argument;
4155 else if (STRCMP(ve, args->os_oldval.string) != 0)
4156 {
4157 // Recompute cursor position in case the new 've' setting
4158 // changes something.
4159 validate_virtcol();
4160 coladvance(curwin->w_virtcol);
4161 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004162 }
4163
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004164 return NULL;
4165}
4166
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004167 int
4168expand_set_virtualedit(optexpand_T *args, int *numMatches, char_u ***matches)
4169{
4170 return expand_set_opt_string(
4171 args,
4172 p_ve_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004173 ARRAY_LENGTH(p_ve_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004174 numMatches,
4175 matches);
4176}
4177
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004178/*
4179 * The 'whichwrap' option is changed.
4180 */
4181 char *
4182did_set_whichwrap(optset_T *args)
4183{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004184 char_u **varp = (char_u **)args->os_varp;
4185
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004186 // Add ',' to the list flags because 'whichwrap' is a flag
4187 // list that is comma-separated.
Christian Brabandtb39b2402023-11-29 11:34:05 +01004188 return did_set_option_listflag(*varp, (char_u *)(WW_ALL ","),
4189 args->os_errbuf, args->os_errbuflen);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004190}
4191
4192 int
4193expand_set_whichwrap(optexpand_T *args, int *numMatches, char_u ***matches)
4194{
4195 return expand_set_opt_listflag(args, (char_u*)WW_ALL, numMatches, matches);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004196}
4197
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004198/*
4199 * The 'wildmode' option is changed.
4200 */
4201 char *
4202did_set_wildmode(optset_T *args UNUSED)
4203{
4204 if (check_opt_wim() == FAIL)
4205 return e_invalid_argument;
4206 return NULL;
4207}
4208
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004209 int
4210expand_set_wildmode(optexpand_T *args, int *numMatches, char_u ***matches)
4211{
4212 return expand_set_opt_string(
4213 args,
4214 p_wim_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004215 ARRAY_LENGTH(p_wim_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004216 numMatches,
4217 matches);
4218}
4219
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004220/*
4221 * The 'wildoptions' option is changed.
4222 */
4223 char *
4224did_set_wildoptions(optset_T *args UNUSED)
4225{
4226 return did_set_opt_strings(p_wop, p_wop_values, TRUE);
4227}
4228
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004229 int
4230expand_set_wildoptions(optexpand_T *args, int *numMatches, char_u ***matches)
4231{
4232 return expand_set_opt_string(
4233 args,
4234 p_wop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004235 ARRAY_LENGTH(p_wop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004236 numMatches,
4237 matches);
4238}
4239
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004240#if defined(FEAT_WAK) || defined(PROTO)
4241/*
4242 * The 'winaltkeys' option is changed.
4243 */
4244 char *
4245did_set_winaltkeys(optset_T *args UNUSED)
4246{
4247 char *errmsg = NULL;
4248
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004249 if (*p_wak == NUL || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004250 errmsg = e_invalid_argument;
4251# ifdef FEAT_MENU
4252# if defined(FEAT_GUI_MOTIF)
4253 else if (gui.in_use)
4254 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4255# elif defined(FEAT_GUI_GTK)
4256 else if (gui.in_use)
4257 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4258# endif
4259# endif
4260 return errmsg;
4261}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004262
4263 int
4264expand_set_winaltkeys(optexpand_T *args, int *numMatches, char_u ***matches)
4265{
4266 return expand_set_opt_string(
4267 args,
4268 p_wak_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004269 ARRAY_LENGTH(p_wak_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004270 numMatches,
4271 matches);
4272}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004273#endif
4274
4275/*
4276 * The 'wincolor' option is changed.
4277 */
4278 char *
4279did_set_wincolor(optset_T *args UNUSED)
4280{
4281#ifdef FEAT_TERMINAL
4282 term_update_wincolor(curwin);
4283#endif
4284 return NULL;
4285}
4286
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004287 int
4288expand_set_wincolor(optexpand_T *args, int *numMatches, char_u ***matches)
4289{
4290 return expand_set_opt_generic(
4291 args,
4292 get_highlight_name,
4293 numMatches,
4294 matches);
4295}
4296
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004297#ifdef FEAT_SYN_HL
4298/*
4299 * When the 'syntax' option is set, load the syntax of that name.
4300 */
4301 static void
4302do_syntax_autocmd(int value_changed)
4303{
4304 static int syn_recursive = 0;
4305
4306 ++syn_recursive;
4307 // Only pass TRUE for "force" when the value changed or not used
4308 // recursively, to avoid endless recurrence.
4309 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
4310 value_changed || syn_recursive == 1, curbuf);
4311 curbuf->b_flags |= BF_SYN_SET;
4312 --syn_recursive;
4313}
4314#endif
4315
4316/*
4317 * When the 'filetype' option is set, trigger the FileType autocommand.
4318 */
4319 static void
4320do_filetype_autocmd(char_u **varp, int opt_flags, int value_changed)
4321{
4322 // Skip this when called from a modeline and the filetype was already set
4323 // to this value.
4324 if ((opt_flags & OPT_MODELINE) && !value_changed)
4325 return;
4326
4327 static int ft_recursive = 0;
4328 int secure_save = secure;
4329
4330 // Reset the secure flag, since the value of 'filetype' has
4331 // been checked to be safe.
4332 secure = 0;
4333
4334 ++ft_recursive;
zeertzjq5bf6c212024-03-31 18:41:27 +02004335 curbuf->b_did_filetype = TRUE;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004336 // Only pass TRUE for "force" when the value changed or not
4337 // used recursively, to avoid endless recurrence.
4338 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
4339 value_changed || ft_recursive == 1, curbuf);
4340 --ft_recursive;
4341 // Just in case the old "curbuf" is now invalid.
4342 if (varp != &(curbuf->b_p_ft))
4343 varp = NULL;
4344
4345 secure = secure_save;
4346}
4347
4348#ifdef FEAT_SPELL
4349/*
4350 * When the 'spelllang' option is set, source the spell/LANG.vim file in
4351 * 'runtimepath'.
4352 */
4353 static void
4354do_spelllang_source(void)
4355{
4356 char_u fname[200];
4357 char_u *p;
4358 char_u *q = curwin->w_s->b_p_spl;
4359
4360 // Skip the first name if it is "cjk".
4361 if (STRNCMP(q, "cjk,", 4) == 0)
4362 q += 4;
4363
4364 // They could set 'spellcapcheck' depending on the language. Use the first
4365 // name in 'spelllang' up to '_region' or '.encoding'.
4366 for (p = q; *p != NUL; ++p)
4367 if (!ASCII_ISALNUM(*p) && *p != '-')
4368 break;
4369 if (p > q)
4370 {
4371 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
4372 (int)(p - q), q);
4373 source_runtime(fname, DIP_ALL);
4374 }
4375}
4376#endif
4377
4378/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004379 * Handle string options that need some action to perform when changed.
zeertzjqf6782732022-07-27 18:26:03 +01004380 * The new value must be allocated.
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004381 * Returns NULL for success, or an untranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +02004382 */
4383 char *
4384did_set_string_option(
4385 int opt_idx, // index in options[] table
4386 char_u **varp, // pointer to the option variable
Bram Moolenaardac13472019-09-16 21:06:21 +02004387 char_u *oldval, // previous value of the option
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004388 char_u *value, // new value of the option
Bram Moolenaardac13472019-09-16 21:06:21 +02004389 char *errbuf, // buffer for errors, or NULL
Mike Williams620f0112023-12-05 15:36:06 +01004390 size_t errbuflen, // length of error buffer
Bram Moolenaardac13472019-09-16 21:06:21 +02004391 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004392 set_op_T op, // OP_ADDING/OP_PREPENDING/OP_REMOVING
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004393 int *value_checked) // value was checked to be safe, no
Bram Moolenaardac13472019-09-16 21:06:21 +02004394 // need to set P_INSECURE
4395{
4396 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +02004397 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004398 opt_did_set_cb_T did_set_cb = get_option_did_set_cb(opt_idx);
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004399 optset_T args;
4400
4401 // 'ttytype' is an alias for 'term'. Both 'term' and 'ttytype' point to
4402 // T_NAME. If 'term' or 'ttytype' is modified, then use the index for the
4403 // 'term' option. Only set the P_ALLOCED flag on 'term'.
4404 if (varp == &T_NAME)
4405 {
4406 opt_idx = findoption((char_u *)"term");
4407 if (opt_idx >= 0)
4408 {
4409 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
4410 did_set_cb = get_option_did_set_cb(opt_idx);
4411 }
4412 }
4413
4414 CLEAR_FIELD(args);
Bram Moolenaardac13472019-09-16 21:06:21 +02004415
Bram Moolenaardac13472019-09-16 21:06:21 +02004416 // Disallow changing some options from secure mode
4417 if ((secure
4418#ifdef HAVE_SANDBOX
4419 || sandbox != 0
4420#endif
4421 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00004422 errmsg = e_not_allowed_here;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004423 // Check for a "normal" directory or file name in some options.
4424 else if (check_illegal_path_names(opt_idx, varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004425 errmsg = e_invalid_argument;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004426 else if (did_set_cb != NULL)
4427 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004428 args.os_varp = (char_u *)varp;
4429 args.os_idx = opt_idx;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004430 args.os_flags = opt_flags;
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004431 args.os_op = op;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004432 args.os_oldval.string = oldval;
4433 args.os_newval.string = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004434 args.os_errbuf = errbuf;
Christian Brabandtb39b2402023-11-29 11:34:05 +01004435 args.os_errbuflen = errbuflen;
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004436 // Invoke the option specific callback function to validate and apply
4437 // the new option value.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004438 errmsg = did_set_cb(&args);
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004439
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004440 // The 'keymap', 'filetype' and 'syntax' option callback functions
4441 // may change the os_value_checked field.
4442 *value_checked = args.os_value_checked;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004443 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004444
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004445 // If an error is detected, restore the previous value.
Bram Moolenaardac13472019-09-16 21:06:21 +02004446 if (errmsg != NULL)
4447 {
zeertzjqf6782732022-07-27 18:26:03 +01004448 free_string_option(*varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02004449 *varp = oldval;
4450 // When resetting some values, need to act on it.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004451 if (args.os_restore_chartab)
Bram Moolenaardac13472019-09-16 21:06:21 +02004452 (void)init_chartab();
4453 if (varp == &p_hl)
4454 (void)highlight_changed();
4455 }
4456 else
4457 {
4458#ifdef FEAT_EVAL
4459 // Remember where the option was set.
4460 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4461#endif
4462 // Free string options that are in allocated memory.
4463 // Use "free_oldval", because recursiveness may change the flags under
4464 // our fingers (esp. init_highlight()).
4465 if (free_oldval)
4466 free_string_option(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01004467 set_option_flag(opt_idx, P_ALLOCED);
Bram Moolenaardac13472019-09-16 21:06:21 +02004468
4469 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
4470 && is_global_local_option(opt_idx))
4471 {
4472 // global option with local value set to use global value; free
4473 // the local value and make it empty
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004474 char_u *p = get_option_varp_scope(opt_idx, OPT_LOCAL);
Bram Moolenaardac13472019-09-16 21:06:21 +02004475 free_string_option(*(char_u **)p);
4476 *(char_u **)p = empty_option;
4477 }
4478
4479 // May set global value for local option.
4480 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
4481 set_string_option_global(opt_idx, varp);
4482
4483 // Trigger the autocommand only after setting the flags.
4484#ifdef FEAT_SYN_HL
Bram Moolenaardac13472019-09-16 21:06:21 +02004485 if (varp == &(curbuf->b_p_syn))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004486 do_syntax_autocmd(args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004487#endif
4488 else if (varp == &(curbuf->b_p_ft))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004489 do_filetype_autocmd(varp, opt_flags, args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004490#ifdef FEAT_SPELL
4491 if (varp == &(curwin->w_s->b_p_spl))
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004492 do_spelllang_source();
Bram Moolenaardac13472019-09-16 21:06:21 +02004493#endif
4494 }
4495
Bram Moolenaardac13472019-09-16 21:06:21 +02004496 if (varp == &p_mouse)
4497 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004498 if (*p_mouse == NUL)
4499 mch_setmouse(FALSE); // switch mouse off
4500 else
Bram Moolenaardac13472019-09-16 21:06:21 +02004501 setmouse(); // in case 'mouse' changed
4502 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004503
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004504#if defined(FEAT_LUA) || defined(PROTO)
4505 if (varp == &p_rtp)
4506 update_package_paths_in_lua();
4507#endif
4508
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004509#if defined(FEAT_LINEBREAK)
4510 // Changing Formatlistpattern when briopt includes the list setting:
4511 // redraw
4512 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
4513 && curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004514 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004515#endif
4516
Bram Moolenaardac13472019-09-16 21:06:21 +02004517 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004518 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0
4519 && (get_option_flags(opt_idx) & P_HLONLY) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02004520 curwin->w_set_curswant = TRUE;
4521
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004522 if ((opt_flags & OPT_NO_REDRAW) == 0)
4523 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004524#ifdef FEAT_GUI
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004525 // set when changing an option that only requires a redraw in the GUI
4526 int redraw_gui_only = FALSE;
4527
4528 if (varp == &p_go // 'guioptions'
4529 || varp == &p_guifont // 'guifont'
4530# ifdef FEAT_GUI_TABLINE
4531 || varp == &p_gtl // 'guitablabel'
4532 || varp == &p_gtt // 'guitabtooltip'
4533# endif
4534# ifdef FEAT_XFONTSET
4535 || varp == &p_guifontset // 'guifontset'
4536# endif
4537 || varp == &p_guifontwide // 'guifontwide'
Erik S. V. Jansson2f026382024-02-26 22:23:05 +01004538# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004539 || varp == &p_guiligatures // 'guiligatures'
4540# endif
4541 )
4542 redraw_gui_only = TRUE;
4543
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004544 // check redraw when it's not a GUI option or the GUI is active.
4545 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02004546#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004547 check_redraw(get_option_flags(opt_idx));
4548 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004549
4550#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004551 if (args.os_did_swaptcap)
Bram Moolenaardac13472019-09-16 21:06:21 +02004552 {
4553 set_termname((char_u *)"win32");
4554 init_highlight(TRUE, FALSE);
4555 }
4556#endif
4557
4558 return errmsg;
4559}
4560
4561/*
4562 * Check an option that can be a range of string values.
4563 *
4564 * Return OK for correct value, FAIL otherwise.
4565 * Empty is always OK.
4566 */
4567 static int
4568check_opt_strings(
4569 char_u *val,
4570 char **values,
4571 int list) // when TRUE: accept a list of values
4572{
4573 return opt_strings_flags(val, values, NULL, list);
4574}
4575
4576/*
4577 * Handle an option that can be a range of string values.
4578 * Set a flag in "*flagp" for each string present.
4579 *
4580 * Return OK for correct value, FAIL otherwise.
4581 * Empty is always OK.
4582 */
4583 static int
4584opt_strings_flags(
4585 char_u *val, // new value
4586 char **values, // array of valid string values
4587 unsigned *flagp,
4588 int list) // when TRUE: accept a list of values
4589{
4590 int i;
4591 int len;
4592 unsigned new_flags = 0;
4593
4594 while (*val)
4595 {
4596 for (i = 0; ; ++i)
4597 {
4598 if (values[i] == NULL) // val not found in values[]
4599 return FAIL;
4600
4601 len = (int)STRLEN(values[i]);
4602 if (STRNCMP(values[i], val, len) == 0
4603 && ((list && val[len] == ',') || val[len] == NUL))
4604 {
4605 val += len + (val[len] == ',');
4606 new_flags |= (1 << i);
4607 break; // check next item in val list
4608 }
4609 }
4610 }
4611 if (flagp != NULL)
4612 *flagp = new_flags;
4613
4614 return OK;
4615}
4616
4617/*
4618 * return OK if "p" is a valid fileformat name, FAIL otherwise.
4619 */
4620 int
4621check_ff_value(char_u *p)
4622{
4623 return check_opt_strings(p, p_ff_values, FALSE);
4624}
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004625
4626/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004627 * Save the actual shortmess Flags and clear them temporarily to avoid that
4628 * file messages overwrites any output from the following commands.
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004629 *
4630 * Caller must make sure to first call save_clear_shm_value() and then
4631 * restore_shm_value() exactly the same number of times.
4632 */
4633 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004634save_clear_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004635{
4636 if (STRLEN(p_shm) >= SHM_LEN)
4637 {
4638 iemsg(e_internal_error_shortmess_too_long);
4639 return;
4640 }
4641
4642 if (++set_shm_recursive == 1)
4643 {
4644 STRCPY(shm_buf, p_shm);
4645 set_option_value_give_err((char_u *)"shm", 0L, (char_u *)"", 0);
4646 }
4647}
4648
4649/*
4650 * Restore the shortmess Flags set from the save_clear_shm_value() function.
4651 */
4652 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004653restore_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004654{
4655 if (--set_shm_recursive == 0)
4656 {
4657 set_option_value_give_err((char_u *)"shm", 0L, shm_buf, 0);
4658 vim_memset(shm_buf, 0, SHM_LEN);
4659 }
4660}