blob: 2d809a394378e55e76b501fe4bcf37d6f8850593 [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
Bram Moolenaardca7abe2019-10-20 18:17:57 +0200121static char *(p_cot_values[]) = {"menu", "menuone", "longest", "preview", "popup", "popuphidden", "noinsert", "noselect", 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/*
1211 * The 'breakindentopt' option is changed.
1212 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001213 char *
1214did_set_breakindentopt(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001215{
1216 char *errmsg = NULL;
1217
1218 if (briopt_check(curwin) == FAIL)
1219 errmsg = e_invalid_argument;
1220 // list setting requires a redraw
1221 if (curwin->w_briopt_list)
1222 redraw_all_later(UPD_NOT_VALID);
1223
1224 return errmsg;
1225}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001226
1227 int
1228expand_set_breakindentopt(optexpand_T *args, int *numMatches, char_u ***matches)
1229{
1230 return expand_set_opt_string(
1231 args,
1232 p_briopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001233 ARRAY_LENGTH(p_briopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001234 numMatches,
1235 matches);
1236}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001237#endif
1238
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001239#if defined(FEAT_BROWSE) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001240/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001241 * The 'browsedir' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001242 */
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00001243 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001244did_set_browsedir(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001245{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001246 if (check_opt_strings(p_bsdir, p_bsdir_values, FALSE) != OK
1247 && !mch_isdir(p_bsdir))
1248 return e_invalid_argument;
1249
1250 return NULL;
1251}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001252
1253 int
1254expand_set_browsedir(optexpand_T *args, int *numMatches, char_u ***matches)
1255{
1256 return expand_set_opt_string(
1257 args,
1258 p_bsdir_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001259 ARRAY_LENGTH(p_bsdir_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001260 numMatches,
1261 matches);
1262}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001263#endif
1264
1265/*
1266 * The 'bufhidden' option is changed.
1267 */
1268 char *
1269did_set_bufhidden(optset_T *args UNUSED)
1270{
1271 return did_set_opt_strings(curbuf->b_p_bh, p_bufhidden_values, FALSE);
1272}
1273
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001274 int
1275expand_set_bufhidden(optexpand_T *args, int *numMatches, char_u ***matches)
1276{
1277 return expand_set_opt_string(
1278 args,
1279 p_bufhidden_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001280 ARRAY_LENGTH(p_bufhidden_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001281 numMatches,
1282 matches);
1283}
1284
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001285/*
1286 * The 'buftype' option is changed.
1287 */
1288 char *
1289did_set_buftype(optset_T *args UNUSED)
1290{
1291 if (check_opt_strings(curbuf->b_p_bt, p_buftype_values, FALSE) != OK)
1292 return e_invalid_argument;
1293
1294 if (curwin->w_status_height)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001295 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001296 curwin->w_redr_status = TRUE;
1297 redraw_later(UPD_VALID);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001298 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001299 curbuf->b_help = (curbuf->b_p_bt[0] == 'h');
1300 redraw_titles();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001301
1302 return NULL;
1303}
1304
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001305 int
1306expand_set_buftype(optexpand_T *args, int *numMatches, char_u ***matches)
1307{
1308 return expand_set_opt_string(
1309 args,
1310 p_buftype_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001311 ARRAY_LENGTH(p_buftype_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001312 numMatches,
1313 matches);
1314}
1315
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001316/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001317 * The 'casemap' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001318 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001319 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001320did_set_casemap(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001321{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001322 return did_set_opt_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
1323}
1324
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001325 int
1326expand_set_casemap(optexpand_T *args, int *numMatches, char_u ***matches)
1327{
1328 return expand_set_opt_string(
1329 args,
1330 p_cmp_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001331 ARRAY_LENGTH(p_cmp_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001332 numMatches,
1333 matches);
1334}
1335
1336#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1337 int
1338expand_set_clipboard(optexpand_T *args, int *numMatches, char_u ***matches)
1339{
1340 return expand_set_opt_string(
1341 args,
1342 p_cb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001343 ARRAY_LENGTH(p_cb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001344 numMatches,
1345 matches);
1346}
1347#endif
1348
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001349/*
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001350 * The global 'listchars' or 'fillchars' option is changed.
1351 */
1352 static char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01001353did_set_global_listfillchars(char_u *val, int opt_lcs, int opt_flags,
1354 char *errbuf, size_t errbuflen)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001355{
1356 char *errmsg = NULL;
1357 char_u **local_ptr = opt_lcs ? &curwin->w_p_lcs : &curwin->w_p_fcs;
1358
1359 // only apply the global value to "curwin" when it does not have a
1360 // local value
1361 if (opt_lcs)
1362 errmsg = set_listchars_option(curwin, val,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001363 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL),
1364 errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001365 else
1366 errmsg = set_fillchars_option(curwin, val,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001367 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL),
1368 errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001369 if (errmsg != NULL)
1370 return errmsg;
1371
1372 tabpage_T *tp;
1373 win_T *wp;
1374
1375 // If the current window is set to use the global
1376 // 'listchars'/'fillchars' value, clear the window-local value.
1377 if (!(opt_flags & OPT_GLOBAL))
1378 clear_string_option(local_ptr);
1379 FOR_ALL_TAB_WINDOWS(tp, wp)
1380 {
1381 // If the current window has a local value need to apply it
1382 // again, it was changed when setting the global value.
1383 // If no error was returned above, we don't expect an error
1384 // here, so ignore the return value.
1385 if (opt_lcs)
1386 {
1387 if (*wp->w_p_lcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001388 (void)set_listchars_option(wp, wp->w_p_lcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001389 }
1390 else
1391 {
1392 if (*wp->w_p_fcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001393 (void)set_fillchars_option(wp, wp->w_p_fcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001394 }
1395 }
1396
1397 redraw_all_later(UPD_NOT_VALID);
1398
1399 return NULL;
1400}
1401
1402/*
1403 * The 'fillchars' option or the 'listchars' option is changed.
1404 */
1405 char *
1406did_set_chars_option(optset_T *args)
1407{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001408 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001409 char *errmsg = NULL;
1410
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001411 if ( varp == &p_lcs // global 'listchars'
1412 || varp == &p_fcs) // global 'fillchars'
1413 errmsg = did_set_global_listfillchars(*varp, varp == &p_lcs,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001414 args->os_flags, args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001415 else if (varp == &curwin->w_p_lcs) // local 'listchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001416 errmsg = set_listchars_option(curwin, *varp, TRUE,
1417 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001418 else if (varp == &curwin->w_p_fcs) // local 'fillchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001419 errmsg = set_fillchars_option(curwin, *varp, TRUE,
1420 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001421
1422 return errmsg;
1423}
1424
1425/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001426 * Expand 'fillchars' or 'listchars' option value.
1427 */
1428 int
1429expand_set_chars_option(optexpand_T *args, int *numMatches, char_u ***matches)
1430{
1431 char_u **varp = (char_u **)args->oe_varp;
1432 int is_lcs = (varp == &p_lcs || varp == &curwin->w_p_lcs);
1433 return expand_set_opt_generic(
1434 args,
1435 is_lcs ? get_listchars_name : get_fillchars_name,
1436 numMatches,
1437 matches);
1438}
1439
1440/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001441 * The 'cinoptions' option is changed.
1442 */
1443 char *
1444did_set_cinoptions(optset_T *args UNUSED)
1445{
1446 // TODO: recognize errors
1447 parse_cino(curbuf);
1448
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001449 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001450}
1451
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00001452#if defined(FEAT_SYN_HL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001453/*
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001454 * The 'colorcolumn' option is changed.
1455 */
1456 char *
1457did_set_colorcolumn(optset_T *args UNUSED)
1458{
1459 return check_colorcolumn(curwin);
1460}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001461#endif
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001462
1463/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001464 * The 'comments' option is changed.
1465 */
1466 char *
1467did_set_comments(optset_T *args)
1468{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001469 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001470 char_u *s;
1471 char *errmsg = NULL;
1472
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001473 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001474 {
1475 while (*s && *s != ':')
1476 {
1477 if (vim_strchr((char_u *)COM_ALL, *s) == NULL
1478 && !VIM_ISDIGIT(*s) && *s != '-')
1479 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01001480 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001481 break;
1482 }
1483 ++s;
1484 }
1485 if (*s++ == NUL)
1486 errmsg = e_missing_colon;
1487 else if (*s == ',' || *s == NUL)
1488 errmsg = e_zero_length_string;
1489 if (errmsg != NULL)
1490 break;
1491 while (*s && *s != ',')
1492 {
1493 if (*s == '\\' && s[1] != NUL)
1494 ++s;
1495 ++s;
1496 }
1497 s = skip_to_option_part(s);
1498 }
1499
1500 return errmsg;
1501}
1502
1503#if defined(FEAT_FOLDING) || defined(PROTO)
1504/*
1505 * The 'commentstring' option is changed.
1506 */
1507 char *
1508did_set_commentstring(optset_T *args)
1509{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001510 char_u **varp = (char_u **)args->os_varp;
1511
1512 if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001513 return e_commentstring_must_be_empty_or_contain_str;
1514
1515 return NULL;
1516}
1517#endif
1518
1519/*
1520 * The 'complete' option is changed.
1521 */
1522 char *
1523did_set_complete(optset_T *args)
1524{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001525 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001526 char_u *s;
1527
1528 // check if it is a valid value for 'complete' -- Acevedo
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001529 for (s = *varp; *s;)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001530 {
1531 while (*s == ',' || *s == ' ')
1532 s++;
1533 if (!*s)
1534 break;
1535 if (vim_strchr((char_u *)".wbuksid]tU", *s) == NULL)
Christian Brabandtb39b2402023-11-29 11:34:05 +01001536 return illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001537 if (*++s != NUL && *s != ',' && *s != ' ')
1538 {
1539 if (s[-1] == 'k' || s[-1] == 's')
1540 {
1541 // skip optional filename after 'k' and 's'
1542 while (*s && *s != ',' && *s != ' ')
1543 {
1544 if (*s == '\\' && s[1] != NUL)
1545 ++s;
1546 ++s;
1547 }
1548 }
1549 else
1550 {
1551 if (args->os_errbuf != NULL)
1552 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01001553 vim_snprintf((char *)args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001554 _(e_illegal_character_after_chr), *--s);
1555 return args->os_errbuf;
1556 }
1557 return "";
1558 }
1559 }
1560 }
1561
1562 return NULL;
1563}
1564
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001565 int
1566expand_set_complete(optexpand_T *args, int *numMatches, char_u ***matches)
1567{
1568 static char *(p_cpt_values[]) = {
1569 ".", "w", "b", "u", "k", "kspell", "s", "i", "d", "]", "t", "U",
1570 NULL};
1571 return expand_set_opt_string(
1572 args,
1573 p_cpt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001574 ARRAY_LENGTH(p_cpt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001575 numMatches,
1576 matches);
1577}
1578
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001579/*
1580 * The 'completeopt' option is changed.
1581 */
1582 char *
1583did_set_completeopt(optset_T *args UNUSED)
1584{
1585 if (check_opt_strings(p_cot, p_cot_values, TRUE) != OK)
1586 return e_invalid_argument;
1587
1588 completeopt_was_set();
1589 return NULL;
1590}
1591
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001592 int
1593expand_set_completeopt(optexpand_T *args, int *numMatches, char_u ***matches)
1594{
1595 return expand_set_opt_string(
1596 args,
1597 p_cot_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001598 ARRAY_LENGTH(p_cot_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001599 numMatches,
1600 matches);
1601}
1602
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001603#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1604/*
1605 * The 'completepopup' option is changed.
1606 */
1607 char *
1608did_set_completepopup(optset_T *args UNUSED)
1609{
1610 if (parse_completepopup(NULL) == FAIL)
1611 return e_invalid_argument;
1612
1613 popup_close_info();
1614 return NULL;
1615}
1616#endif
1617
1618#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1619/*
1620 * The 'completeslash' option is changed.
1621 */
1622 char *
1623did_set_completeslash(optset_T *args UNUSED)
1624{
1625 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1626 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
1627 return e_invalid_argument;
1628
1629 return NULL;
1630}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001631
1632 int
1633expand_set_completeslash(optexpand_T *args, int *numMatches, char_u ***matches)
1634{
1635 return expand_set_opt_string(
1636 args,
1637 p_csl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001638 ARRAY_LENGTH(p_csl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001639 numMatches,
1640 matches);
1641}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001642#endif
1643
1644#if defined(FEAT_CONCEAL) || defined(PROTO)
1645/*
1646 * The 'concealcursor' option is changed.
1647 */
1648 char *
1649did_set_concealcursor(optset_T *args)
1650{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001651 char_u **varp = (char_u **)args->os_varp;
1652
Christian Brabandtb39b2402023-11-29 11:34:05 +01001653 return did_set_option_listflag(*varp, (char_u *)COCU_ALL, args->os_errbuf,
1654 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001655}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001656
1657 int
1658expand_set_concealcursor(optexpand_T *args, int *numMatches, char_u ***matches)
1659{
1660 return expand_set_opt_listflag(args, (char_u*)COCU_ALL, numMatches, matches);
1661}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001662#endif
1663
1664/*
1665 * The 'cpoptions' option is changed.
1666 */
1667 char *
1668did_set_cpoptions(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 *)CPO_ALL, args->os_errbuf,
1673 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001674}
1675
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001676 int
1677expand_set_cpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
1678{
1679 return expand_set_opt_listflag(args, (char_u*)CPO_ALL, numMatches, matches);
1680}
1681
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001682#if defined(FEAT_CRYPT) || defined(PROTO)
1683/*
1684 * The 'cryptkey' option is changed.
1685 */
1686 char *
1687did_set_cryptkey(optset_T *args)
1688{
1689 // Make sure the ":set" command doesn't show the new value in the
1690 // history.
1691 remove_key_from_history();
1692
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02001693 if (args->os_op != OP_NONE)
1694 // Don't allow set+=/-=/^= as they can allow for substring guessing
1695 return e_invalid_argument;
1696
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001697 if (STRCMP(curbuf->b_p_key, args->os_oldval.string) != 0)
1698 {
1699 // Need to update the swapfile.
1700 ml_set_crypt_key(curbuf, args->os_oldval.string,
1701 *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
1702 changed_internal();
1703 }
Christian Brabandt19e6c4f2023-06-27 18:57:10 +01001704# ifdef FEAT_SODIUM
1705 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
1706 crypt_sodium_lock_key(args->os_newval.string);
1707# endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001708
1709 return NULL;
1710}
1711
1712/*
1713 * The 'cryptmethod' option is changed.
1714 */
1715 char *
1716did_set_cryptmethod(optset_T *args)
1717{
1718 char_u *p;
1719 char_u *s;
1720
1721 if (args->os_flags & OPT_LOCAL)
1722 p = curbuf->b_p_cm;
1723 else
1724 p = p_cm;
1725 if (check_opt_strings(p, p_cm_values, TRUE) != OK)
1726 return e_invalid_argument;
1727 else if (crypt_self_test() == FAIL)
1728 return e_invalid_argument;
1729
1730 // When setting the global value to empty, make it "zip".
1731 if (*p_cm == NUL)
1732 {
1733 free_string_option(p_cm);
1734 p_cm = vim_strsave((char_u *)"zip");
1735 }
1736 // When using ":set cm=name" the local value is going to be empty.
1737 // Do that here, otherwise the crypt functions will still use the
1738 // local value.
1739 if ((args->os_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1740 {
1741 free_string_option(curbuf->b_p_cm);
1742 curbuf->b_p_cm = empty_option;
1743 }
1744
1745 // Need to update the swapfile when the effective method changed.
1746 // Set "s" to the effective old value, "p" to the effective new
1747 // method and compare.
1748 if ((args->os_flags & OPT_LOCAL) && *args->os_oldval.string == NUL)
1749 s = p_cm; // was previously using the global value
1750 else
1751 s = args->os_oldval.string;
1752 if (*curbuf->b_p_cm == NUL)
1753 p = p_cm; // is now using the global value
1754 else
1755 p = curbuf->b_p_cm;
1756 if (STRCMP(s, p) != 0)
1757 ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1758
1759 // If the global value changes need to update the swapfile for all
1760 // buffers using that value.
1761 if ((args->os_flags & OPT_GLOBAL)
1762 && STRCMP(p_cm, args->os_oldval.string) != 0)
1763 {
1764 buf_T *buf;
1765
1766 FOR_ALL_BUFFERS(buf)
1767 if (buf != curbuf && *buf->b_p_cm == NUL)
1768 ml_set_crypt_key(buf, buf->b_p_key, args->os_oldval.string);
1769 }
1770 return NULL;
1771}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001772
1773 int
1774expand_set_cryptmethod(optexpand_T *args, int *numMatches, char_u ***matches)
1775{
1776 return expand_set_opt_string(
1777 args,
1778 p_cm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001779 ARRAY_LENGTH(p_cm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001780 numMatches,
1781 matches);
1782}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001783#endif
1784
1785#if (defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1786/*
1787 * The 'cscopequickfix' option is changed.
1788 */
1789 char *
1790did_set_cscopequickfix(optset_T *args UNUSED)
1791{
1792 char_u *p;
1793
1794 if (p_csqf == NULL)
1795 return NULL;
1796
1797 p = p_csqf;
1798 while (*p != NUL)
1799 {
1800 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
1801 || p[1] == NUL
1802 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
1803 || (p[2] != NUL && p[2] != ','))
1804 return e_invalid_argument;
1805 else if (p[2] == NUL)
1806 break;
1807 else
1808 p += 3;
1809 }
1810
1811 return NULL;
1812}
1813#endif
1814
1815#if defined(FEAT_SYN_HL) || defined(PROTO)
1816/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001817 * The 'cursorlineopt' option is changed.
1818 */
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001819 char *
1820did_set_cursorlineopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001821{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001822 char_u **varp = (char_u **)args->os_varp;
1823
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001824 // This could be changed to use opt_strings_flags() instead.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001825 if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001826 return e_invalid_argument;
1827
1828 return NULL;
1829}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001830
1831 int
1832expand_set_cursorlineopt(optexpand_T *args, int *numMatches, char_u ***matches)
1833{
1834 return expand_set_opt_string(
1835 args,
1836 p_culopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001837 ARRAY_LENGTH(p_culopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001838 numMatches,
1839 matches);
1840}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001841#endif
1842
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001843/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001844 * The 'debug' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001845 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001846 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001847did_set_debug(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001848{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001849 return did_set_opt_strings(p_debug, p_debug_values, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001850}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001851
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001852 int
1853expand_set_debug(optexpand_T *args, int *numMatches, char_u ***matches)
1854{
1855 return expand_set_opt_string(
1856 args,
1857 p_debug_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001858 ARRAY_LENGTH(p_debug_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001859 numMatches,
1860 matches);
1861}
1862
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001863#if defined(FEAT_DIFF) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001864/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001865 * The 'diffopt' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001866 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001867 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001868did_set_diffopt(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001869{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001870 if (diffopt_changed() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001871 return e_invalid_argument;
1872
1873 return NULL;
1874}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001875
1876 int
1877expand_set_diffopt(optexpand_T *args, int *numMatches, char_u ***matches)
1878{
1879 expand_T *xp = args->oe_xp;
1880
1881 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
1882 {
1883 // Within "algorithm:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001884 int algo_len = (int)STRLEN("algorithm:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001885 if (xp->xp_pattern - args->oe_set_arg >= algo_len &&
1886 STRNCMP(xp->xp_pattern - algo_len, "algorithm:", algo_len) == 0)
1887 {
1888 return expand_set_opt_string(
1889 args,
1890 p_dip_algorithm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001891 ARRAY_LENGTH(p_dip_algorithm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001892 numMatches,
1893 matches);
1894 }
1895 return FAIL;
1896 }
1897
1898 return expand_set_opt_string(
1899 args,
1900 p_dip_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001901 ARRAY_LENGTH(p_dip_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001902 numMatches,
1903 matches);
1904}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001905#endif
1906
1907/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001908 * The 'display' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001909 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001910 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001911did_set_display(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001912{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001913 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001914 return e_invalid_argument;
1915
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001916 (void)init_chartab();
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001917 return NULL;
1918}
1919
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001920 int
1921expand_set_display(optexpand_T *args, int *numMatches, char_u ***matches)
1922{
1923 return expand_set_opt_string(
1924 args,
1925 p_dy_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001926 ARRAY_LENGTH(p_dy_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001927 numMatches,
1928 matches);
1929}
1930
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001931/*
1932 * The 'eadirection' option is changed.
1933 */
1934 char *
1935did_set_eadirection(optset_T *args UNUSED)
1936{
1937 return did_set_opt_strings(p_ead, p_ead_values, FALSE);
1938}
1939
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001940 int
1941expand_set_eadirection(optexpand_T *args, int *numMatches, char_u ***matches)
1942{
1943 return expand_set_opt_string(
1944 args,
1945 p_ead_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001946 ARRAY_LENGTH(p_ead_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001947 numMatches,
1948 matches);
1949}
1950
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001951/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001952 * One of the 'encoding', 'fileencoding', 'termencoding' or 'makeencoding'
1953 * options is changed.
1954 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001955 char *
1956did_set_encoding(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001957{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001958 char_u **varp = (char_u **)args->os_varp;
1959 char_u **gvarp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001960 char *errmsg = NULL;
1961 char_u *p;
1962
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001963 // Get the global option to compare with, otherwise we would have to check
1964 // two values for all local options.
1965 gvarp = (char_u **)get_option_varp_scope(args->os_idx, OPT_GLOBAL);
1966
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001967 if (gvarp == &p_fenc)
1968 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001969 if (!curbuf->b_p_ma && args->os_flags != OPT_GLOBAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001970 errmsg = e_cannot_make_changes_modifiable_is_off;
1971 else if (vim_strchr(*varp, ',') != NULL)
1972 // No comma allowed in 'fileencoding'; catches confusing it
1973 // with 'fileencodings'.
1974 errmsg = e_invalid_argument;
1975 else
1976 {
1977 // May show a "+" in the title now.
1978 redraw_titles();
1979 // Add 'fileencoding' to the swap file.
1980 ml_setflags(curbuf);
1981 }
1982 }
1983 if (errmsg == NULL)
1984 {
1985 // canonize the value, so that STRCMP() can be used on it
1986 p = enc_canonize(*varp);
1987 if (p != NULL)
1988 {
1989 vim_free(*varp);
1990 *varp = p;
1991 }
1992 if (varp == &p_enc)
1993 {
1994 errmsg = mb_init();
1995 redraw_titles();
1996 }
1997 }
1998
1999#if defined(FEAT_GUI_GTK)
2000 if (errmsg == NULL && varp == &p_tenc && gui.in_use)
2001 {
2002 // GTK uses only a single encoding, and that is UTF-8.
2003 if (STRCMP(p_tenc, "utf-8") != 0)
2004 errmsg = e_cannot_be_changed_in_gtk_GUI;
2005 }
2006#endif
2007
2008 if (errmsg == NULL)
2009 {
2010#ifdef FEAT_KEYMAP
2011 // When 'keymap' is used and 'encoding' changes, reload the keymap
2012 // (with another encoding).
2013 if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
2014 (void)keymap_init();
2015#endif
2016
2017 // When 'termencoding' is not empty and 'encoding' changes or when
2018 // 'termencoding' changes, need to setup for keyboard input and
2019 // display output conversion.
2020 if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
2021 {
2022 if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
2023 || convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
2024 {
2025 semsg(_(e_cannot_convert_between_str_and_str),
2026 p_tenc, p_enc);
2027 errmsg = e_invalid_argument;
2028 }
2029 }
2030
2031#if defined(MSWIN)
K.Takatace3189d2023-02-15 19:13:43 +00002032 // $HOME, $VIM and $VIMRUNTIME may have characters in active code page.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002033 if (varp == &p_enc)
K.Takatace3189d2023-02-15 19:13:43 +00002034 {
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002035 init_homedir();
K.Takatace3189d2023-02-15 19:13:43 +00002036 init_vimdir();
2037 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002038#endif
2039 }
2040
2041 return errmsg;
2042}
2043
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002044 int
2045expand_set_encoding(optexpand_T *args, int *numMatches, char_u ***matches)
2046{
2047 return expand_set_opt_generic(
2048 args,
2049 get_encoding_name,
2050 numMatches,
2051 matches);
2052}
2053
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002054/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002055 * The 'eventignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002056 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002057 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002058did_set_eventignore(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002059{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002060 if (check_ei() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002061 return e_invalid_argument;
2062 return NULL;
2063}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002064
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002065 static char_u *
2066get_eventignore_name(expand_T *xp, int idx)
2067{
2068 // 'eventignore' allows special keyword "all" in addition to
2069 // all event names.
2070 if (idx == 0)
2071 return (char_u *)"all";
2072 return get_event_name_no_group(xp, idx - 1);
2073}
2074
2075 int
2076expand_set_eventignore(optexpand_T *args, int *numMatches, char_u ***matches)
2077{
2078 return expand_set_opt_generic(
2079 args,
2080 get_eventignore_name,
2081 numMatches,
2082 matches);
2083}
2084
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002085/*
2086 * The 'fileformat' option is changed.
2087 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002088 char *
2089did_set_fileformat(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002090{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002091 char_u **varp = (char_u **)args->os_varp;
2092
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002093 if (!curbuf->b_p_ma && !(args->os_flags & OPT_GLOBAL))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002094 return e_cannot_make_changes_modifiable_is_off;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002095 else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002096 return e_invalid_argument;
2097
2098 // may also change 'textmode'
2099 if (get_fileformat(curbuf) == EOL_DOS)
2100 curbuf->b_p_tx = TRUE;
2101 else
2102 curbuf->b_p_tx = FALSE;
2103 redraw_titles();
2104 // update flag in swap file
2105 ml_setflags(curbuf);
2106 // Redraw needed when switching to/from "mac": a CR in the text
2107 // will be displayed differently.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002108 if (get_fileformat(curbuf) == EOL_MAC || *args->os_oldval.string == 'm')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002109 redraw_curbuf_later(UPD_NOT_VALID);
2110
2111 return NULL;
2112}
2113
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002114 int
2115expand_set_fileformat(optexpand_T *args, int *numMatches, char_u ***matches)
2116{
2117 return expand_set_opt_string(
2118 args,
2119 p_ff_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002120 ARRAY_LENGTH(p_ff_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002121 numMatches,
2122 matches);
2123}
2124
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002125/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002126 * Function given to ExpandGeneric() to obtain the possible arguments of the
2127 * fileformat options.
2128 */
2129 char_u *
2130get_fileformat_name(expand_T *xp UNUSED, int idx)
2131{
2132 if (idx >= (int)ARRAY_LENGTH(p_ff_values))
2133 return NULL;
2134
2135 return (char_u*)p_ff_values[idx];
2136}
2137
2138/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002139 * The 'fileformats' option is changed.
2140 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002141 char *
2142did_set_fileformats(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002143{
2144 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
2145 return e_invalid_argument;
2146
2147 // also change 'textauto'
2148 if (*p_ffs == NUL)
2149 p_ta = FALSE;
2150 else
2151 p_ta = TRUE;
2152
2153 return NULL;
2154}
2155
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002156/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002157 * The 'filetype' or the 'syntax' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002158 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002159 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002160did_set_filetype_or_syntax(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002161{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002162 char_u **varp = (char_u **)args->os_varp;
2163
2164 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002165 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002166
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002167 args->os_value_changed = STRCMP(args->os_oldval.string, *varp) != 0;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002168
2169 // Since we check the value, there is no need to set P_INSECURE,
2170 // even when the value comes from a modeline.
2171 args->os_value_checked = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002172
2173 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002174}
2175
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002176#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002177/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002178 * The 'foldclose' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002179 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002180 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002181did_set_foldclose(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002182{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002183 return did_set_opt_strings(p_fcl, p_fcl_values, TRUE);
2184}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002185
2186 int
2187expand_set_foldclose(optexpand_T *args, int *numMatches, char_u ***matches)
2188{
2189 return expand_set_opt_string(
2190 args,
2191 p_fcl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002192 ARRAY_LENGTH(p_fcl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002193 numMatches,
2194 matches);
2195}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002196#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002197
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002198#if (defined(FEAT_EVAL) && defined(FEAT_FOLDING)) || defined(PROTO)
2199/*
2200 * The 'foldexpr' option is changed.
2201 */
2202 char *
2203did_set_foldexpr(optset_T *args)
2204{
2205 (void)did_set_optexpr(args);
2206 if (foldmethodIsExpr(curwin))
2207 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002208 return NULL;
2209}
2210#endif
2211
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002212#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002213/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002214 * The 'foldignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002215 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002216 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002217did_set_foldignore(optset_T *args UNUSED)
2218{
2219 if (foldmethodIsIndent(curwin))
2220 foldUpdateAll(curwin);
2221 return NULL;
2222}
2223
2224/*
2225 * The 'foldmarker' option is changed.
2226 */
2227 char *
2228did_set_foldmarker(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002229{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002230 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002231 char_u *p;
2232
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002233 p = vim_strchr(*varp, ',');
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002234 if (p == NULL)
2235 return e_comma_required;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002236 else if (p == *varp || p[1] == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002237 return e_invalid_argument;
2238 else if (foldmethodIsMarker(curwin))
2239 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002240
2241 return NULL;
2242}
2243
2244/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002245 * The 'foldmethod' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002246 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002247 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002248did_set_foldmethod(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002249{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002250 char_u **varp = (char_u **)args->os_varp;
2251
2252 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002253 || *curwin->w_p_fdm == NUL)
2254 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002255
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002256 foldUpdateAll(curwin);
2257 if (foldmethodIsDiff(curwin))
2258 newFoldLevel();
2259 return NULL;
2260}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002261
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002262 int
2263expand_set_foldmethod(optexpand_T *args, int *numMatches, char_u ***matches)
2264{
2265 return expand_set_opt_string(
2266 args,
2267 p_fdm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002268 ARRAY_LENGTH(p_fdm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002269 numMatches,
2270 matches);
2271}
2272
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002273/*
2274 * The 'foldopen' option is changed.
2275 */
2276 char *
2277did_set_foldopen(optset_T *args UNUSED)
2278{
2279 return did_set_opt_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
2280}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002281
2282 int
2283expand_set_foldopen(optexpand_T *args, int *numMatches, char_u ***matches)
2284{
2285 return expand_set_opt_string(
2286 args,
2287 p_fdo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002288 ARRAY_LENGTH(p_fdo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002289 numMatches,
2290 matches);
2291}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002292#endif
2293
2294/*
2295 * The 'formatoptions' option is changed.
2296 */
2297 char *
2298did_set_formatoptions(optset_T *args)
2299{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002300 char_u **varp = (char_u **)args->os_varp;
2301
Christian Brabandtb39b2402023-11-29 11:34:05 +01002302 return did_set_option_listflag(*varp, (char_u *)FO_ALL, args->os_errbuf,
2303 args->os_errbuflen);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002304}
2305
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002306 int
2307expand_set_formatoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2308{
2309 return expand_set_opt_listflag(args, (char_u*)FO_ALL, numMatches, matches);
2310}
2311
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002312#if defined(CURSOR_SHAPE) || defined(PROTO)
2313/*
2314 * The 'guicursor' option is changed.
2315 */
2316 char *
2317did_set_guicursor(optset_T *args UNUSED)
2318{
2319 return parse_shape_opt(SHAPE_CURSOR);
2320}
2321#endif
2322
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002323#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002324/*
2325 * The 'guifont' option is changed.
2326 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002327 char *
2328did_set_guifont(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002329{
2330 char_u *p;
2331 char *errmsg = NULL;
2332
2333 if (gui.in_use)
2334 {
2335 p = p_guifont;
2336# if defined(FEAT_GUI_GTK)
2337 // Put up a font dialog and let the user select a new value.
2338 // If this is cancelled go back to the old value but don't
2339 // give an error message.
2340 if (STRCMP(p, "*") == 0)
2341 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002342 p = gui_mch_font_dialog(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002343 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002344 p_guifont = (p != NULL) ? p : vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002345 }
2346# endif
2347 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
2348 {
2349# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
2350 if (STRCMP(p_guifont, "*") == 0)
2351 {
2352 // Dialog was cancelled: Keep the old value without giving
2353 // an error message.
2354 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002355 p_guifont = vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002356 }
2357 else
2358# endif
2359 errmsg = e_invalid_fonts;
2360 }
2361 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002362
2363 return errmsg;
2364}
2365
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002366/*
2367 * Expand the 'guifont' option. Only when GUI is being used. Each platform has
2368 * specific behaviors.
2369 */
2370 int
2371expand_set_guifont(optexpand_T *args, int *numMatches, char_u ***matches)
2372{
2373 if (!gui.in_use)
2374 return FAIL;
2375
2376# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
2377 char_u **varp = (char_u **)args->oe_varp;
2378 int wide = (varp == &p_guifontwide);
2379
2380 return expand_set_opt_callback(
2381 args, gui_mch_expand_font, &wide, numMatches, matches);
2382# else
2383 return FAIL;
2384# endif
2385}
2386
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002387# if defined(FEAT_XFONTSET) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002388/*
2389 * The 'guifontset' option is changed.
2390 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002391 char *
2392did_set_guifontset(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002393{
2394 char *errmsg = NULL;
2395
2396 if (STRCMP(p_guifontset, "*") == 0)
2397 errmsg = e_cant_select_fontset;
2398 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
2399 errmsg = e_invalid_fontset;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002400
2401 return errmsg;
2402}
2403# endif
2404
2405/*
2406 * The 'guifontwide' option is changed.
2407 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002408 char *
2409did_set_guifontwide(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002410{
2411 char *errmsg = NULL;
2412
2413 if (STRCMP(p_guifontwide, "*") == 0)
2414 errmsg = e_cant_select_wide_font;
2415 else if (gui_get_wide_font() == FAIL)
2416 errmsg = e_invalid_wide_font;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002417
2418 return errmsg;
2419}
2420#endif
2421
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002422#if defined(FEAT_GUI_GTK) || defined(PROTO)
2423/*
2424 * The 'guiligatures' option is changed.
2425 */
2426 char *
2427did_set_guiligatures(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002428{
2429 gui_set_ligatures();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002430 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002431}
2432#endif
2433
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002434#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002435/*
2436 * The 'guioptions' option is changed.
2437 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002438 char *
2439did_set_guioptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002440{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002441 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002442 char *errmsg;
2443
Christian Brabandtb39b2402023-11-29 11:34:05 +01002444 errmsg = did_set_option_listflag(*varp, (char_u *)GO_ALL, args->os_errbuf,
2445 args->os_errbuflen);
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002446 if (errmsg != NULL)
2447 return errmsg;
2448
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002449 gui_init_which_components(args->os_oldval.string);
2450 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002451}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002452
2453 int
2454expand_set_guioptions(optexpand_T *args, int *numMatches, char_u ***matches)
2455{
2456 return expand_set_opt_listflag(args, (char_u*)GO_ALL, numMatches, matches);
2457}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002458#endif
2459
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002460#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002461/*
2462 * The 'guitablabel' option is changed.
2463 */
2464 char *
2465did_set_guitablabel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002466{
2467 redraw_tabline = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002468 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002469}
2470#endif
2471
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002472/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002473 * The 'helpfile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002474 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002475 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002476did_set_helpfile(optset_T *args UNUSED)
2477{
2478 // May compute new values for $VIM and $VIMRUNTIME
2479 if (didset_vim)
2480 vim_unsetenv_ext((char_u *)"VIM");
2481 if (didset_vimruntime)
2482 vim_unsetenv_ext((char_u *)"VIMRUNTIME");
2483 return NULL;
2484}
2485
2486#if defined(FEAT_MULTI_LANG) || defined(PROTO)
2487/*
2488 * The 'helplang' option is changed.
2489 */
2490 char *
2491did_set_helplang(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002492{
2493 char *errmsg = NULL;
2494
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002495 // Check for "", "ab", "ab,cd", etc.
2496 for (char_u *s = p_hlg; *s != NUL; s += 3)
2497 {
2498 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
2499 {
2500 errmsg = e_invalid_argument;
2501 break;
2502 }
2503 if (s[2] == NUL)
2504 break;
2505 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002506
2507 return errmsg;
2508}
2509#endif
2510
2511/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002512 * The 'highlight' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002513 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002514 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002515did_set_highlight(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002516{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002517 if (highlight_changed() == FAIL)
2518 return e_invalid_argument; // invalid flags
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002519
2520 return NULL;
2521}
2522
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002523/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002524 * Expand 'highlight' option.
2525 */
2526 int
2527expand_set_highlight(optexpand_T *args, int *numMatches, char_u ***matches)
2528{
2529 char_u *p;
2530 expand_T *xp = args->oe_xp;
2531 static char_u hl_flags[HLF_COUNT] = HL_FLAGS;
Christian Brabandt3f168ec2023-10-02 23:21:11 +02002532 size_t i;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002533 int count = 0;
2534
2535 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2536 {
2537 // Right after a ':', meaning we just return all highlight names.
2538 return expand_set_opt_generic(
2539 args,
2540 get_highlight_name,
2541 numMatches,
2542 matches);
2543 }
2544
2545 if (*xp->xp_pattern == NUL)
2546 {
2547 // At beginning of a comma-separated list. Return the specific list of
2548 // supported occasions.
2549 *matches = ALLOC_MULT(char_u *, HLF_COUNT + 1);
2550 if (*matches == NULL)
2551 return FAIL;
2552
2553 // We still want to return the full option if it's requested.
2554 if (args->oe_include_orig_val)
2555 {
2556 p = vim_strsave(args->oe_opt_value);
2557 if (p == NULL)
2558 {
2559 VIM_CLEAR(*matches);
2560 return FAIL;
2561 }
2562 (*matches)[count++] = p;
2563 }
2564
2565 for (i = 0; i < HLF_COUNT; i++)
2566 {
2567 p = vim_strnsave(&hl_flags[i], 1);
2568 if (p == NULL)
2569 {
2570 if (count == 0)
2571 {
2572 VIM_CLEAR(*matches);
2573 return FAIL;
2574 }
2575 else
2576 break;
2577 }
2578 (*matches)[count++] = p;
2579 }
2580
2581 if (count == 0)
2582 {
2583 VIM_CLEAR(*matches);
2584 return FAIL;
2585 }
2586 *numMatches = count;
2587 return OK;
2588 }
2589
2590 // We are after the initial character (which indicates the occasion). We
2591 // already made sure we are not matching after a ':' above, so now we want
2592 // to match against display mode modifiers.
2593 // Since the xp_pattern starts from the beginning, we need to include it in
2594 // the returned match.
2595
2596 // Note: Keep this in sync with highlight_changed()
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002597 static char_u p_hl_mode_values[] =
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002598 {':', 'b', 'i', '-', 'n', 'r', 's', 'u', 'c', '2', 'd', '=', 't'};
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002599 size_t num_hl_modes = ARRAY_LENGTH(p_hl_mode_values);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002600
2601 *matches = ALLOC_MULT(char_u *, num_hl_modes);
2602 if (*matches == NULL)
2603 return FAIL;
2604
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002605 int pattern_len = xp->xp_pattern_len;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002606
2607 for (i = 0; i < num_hl_modes; i++)
2608 {
2609 // Don't allow duplicates as these are unique flags
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002610 char_u *dup = vim_strchr(xp->xp_pattern + 1, p_hl_mode_values[i]);
2611 if (dup != NULL && (int)(dup - xp->xp_pattern) < pattern_len)
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002612 continue;
2613
2614 // ':' only works by itself, not with other flags.
2615 if (pattern_len > 1 && p_hl_mode_values[i] == ':')
2616 continue;
2617
2618 p = vim_strnsave(xp->xp_pattern, pattern_len + 1);
2619 if (p == NULL)
2620 {
2621 if (i == 0)
2622 {
2623 VIM_CLEAR(*matches);
2624 return FAIL;
2625 }
2626 else
2627 break;
2628 }
2629 p[pattern_len] = p_hl_mode_values[i];
2630 p[pattern_len + 1] = NUL;
2631 (*matches)[count++] = p;
2632 }
2633 if (count == 0)
2634 {
2635 VIM_CLEAR(*matches);
2636 return FAIL;
2637 }
2638 *numMatches = count;
2639
2640 return OK;
2641}
2642
2643/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002644 * The 'titlestring' or the 'iconstring' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002645 */
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002646 static char *
2647parse_titleiconstring(optset_T *args UNUSED, int flagval UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002648{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002649#ifdef FEAT_STL_OPT
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002650 char_u **varp = (char_u **)args->os_varp;
2651
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002652 // NULL => statusline syntax
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002653 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002654 stl_syntax |= flagval;
2655 else
2656 stl_syntax &= ~flagval;
2657#endif
2658 did_set_title();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002659
2660 return NULL;
2661}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002662
2663/*
2664 * The 'iconstring' option is changed.
2665 */
2666 char *
2667did_set_iconstring(optset_T *args)
2668{
2669 int flagval = 0;
2670
2671#ifdef FEAT_STL_OPT
2672 flagval = STL_IN_ICON;
2673#endif
2674
2675 return parse_titleiconstring(args, flagval);
2676}
2677
2678#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
2679/*
2680 * The 'imactivatekey' option is changed.
2681 */
2682 char *
2683did_set_imactivatekey(optset_T *args UNUSED)
2684{
2685 if (!im_xim_isvalid_imactivate())
2686 return e_invalid_argument;
2687 return NULL;
2688}
2689#endif
2690
2691/*
2692 * The 'isident' or the 'iskeyword' or the 'isprint' or the 'isfname' option is
2693 * changed.
2694 */
2695 char *
2696did_set_isopt(optset_T *args)
2697{
2698 // 'isident', 'iskeyword', 'isprint or 'isfname' option: refill g_chartab[]
2699 // If the new option is invalid, use old value.
2700 // 'lisp' option: refill g_chartab[] for '-' char.
2701 if (init_chartab() == FAIL)
2702 {
2703 args->os_restore_chartab = TRUE;// need to restore the chartab.
2704 return e_invalid_argument; // error in value
2705 }
2706
2707 return NULL;
2708}
2709
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002710/*
2711 * The 'jumpoptions' option is changed.
2712 */
2713 char *
Yegappan Lakshmanan1926ae42023-09-21 16:36:28 +02002714did_set_jumpoptions(optset_T *args UNUSED)
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002715{
2716 if (opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE) != OK)
2717 return e_invalid_argument;
2718
2719 return NULL;
2720}
2721
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002722 int
2723expand_set_jumpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2724{
2725 return expand_set_opt_string(
2726 args,
2727 p_jop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002728 ARRAY_LENGTH(p_jop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002729 numMatches,
2730 matches);
2731}
2732
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002733#if defined(FEAT_KEYMAP) || defined(PROTO)
2734/*
2735 * The 'keymap' option is changed.
2736 */
2737 char *
2738did_set_keymap(optset_T *args)
2739{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002740 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002741 char *errmsg = NULL;
2742
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002743 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002744 errmsg = e_invalid_argument;
2745 else
2746 {
2747 int secure_save = secure;
2748
2749 // Reset the secure flag, since the value of 'keymap' has
2750 // been checked to be safe.
2751 secure = 0;
2752
2753 // load or unload key mapping tables
2754 errmsg = keymap_init();
2755
2756 secure = secure_save;
2757
2758 // Since we check the value, there is no need to set P_INSECURE,
2759 // even when the value comes from a modeline.
2760 args->os_value_checked = TRUE;
2761 }
2762
2763 if (errmsg == NULL)
2764 {
2765 if (*curbuf->b_p_keymap != NUL)
2766 {
2767 // Installed a new keymap, switch on using it.
2768 curbuf->b_p_iminsert = B_IMODE_LMAP;
2769 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
2770 curbuf->b_p_imsearch = B_IMODE_LMAP;
2771 }
2772 else
2773 {
2774 // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
2775 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
2776 curbuf->b_p_iminsert = B_IMODE_NONE;
2777 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
2778 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
2779 }
2780 if ((args->os_flags & OPT_LOCAL) == 0)
2781 {
2782 set_iminsert_global();
2783 set_imsearch_global();
2784 }
2785 status_redraw_curbuf();
2786 }
2787
2788 return errmsg;
2789}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002790#endif
2791
2792/*
2793 * The 'keymodel' option is changed.
2794 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002795 char *
2796did_set_keymodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002797{
2798 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
2799 return e_invalid_argument;
2800
2801 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
2802 km_startsel = (vim_strchr(p_km, 'a') != NULL);
2803 return NULL;
2804}
2805
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002806 int
2807expand_set_keymodel(optexpand_T *args, int *numMatches, char_u ***matches)
2808{
2809 return expand_set_opt_string(
2810 args,
2811 p_km_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002812 ARRAY_LENGTH(p_km_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002813 numMatches,
2814 matches);
2815}
2816
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002817/*
2818 * The 'keyprotocol' option is changed.
2819 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002820 char *
2821did_set_keyprotocol(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002822{
Gregory Anders3695d0e2023-09-29 20:17:20 +02002823 char_u *term = T_NAME;
2824 keyprot_T kpc = match_keyprotocol(term);
2825 if (kpc == KEYPROTOCOL_FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002826 return e_invalid_argument;
2827
Gregory Anders3695d0e2023-09-29 20:17:20 +02002828 apply_keyprotocol(term, kpc);
2829
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002830 return NULL;
2831}
2832
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002833 int
2834expand_set_keyprotocol(optexpand_T *args, int *numMatches, char_u ***matches)
2835{
2836 expand_T *xp = args->oe_xp;
2837 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2838 {
2839 // 'keyprotocol' only has well-defined terms for completion for the
2840 // protocol part after the colon.
2841 return expand_set_opt_string(
2842 args,
2843 p_kpc_protocol_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002844 ARRAY_LENGTH(p_kpc_protocol_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002845 numMatches,
2846 matches);
2847 }
2848 // Use expand_set_opt_string instead of returning FAIL so that we can
2849 // include the original value if args->oe_include_orig_val is set.
2850 static char *(empty[]) = {NULL};
2851 return expand_set_opt_string(args, empty, 0, numMatches, matches);
2852}
2853
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002854/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002855 * The 'lispoptions' option is changed.
2856 */
2857 char *
2858did_set_lispoptions(optset_T *args)
2859{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002860 char_u **varp = (char_u **)args->os_varp;
2861
2862 if (**varp != NUL
2863 && STRCMP(*varp, "expr:0") != 0
2864 && STRCMP(*varp, "expr:1") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002865 return e_invalid_argument;
2866
2867 return NULL;
2868}
2869
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002870 int
2871expand_set_lispoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2872{
2873 static char *(p_lop_values[]) = {"expr:0", "expr:1", NULL};
2874 return expand_set_opt_string(
2875 args,
2876 p_lop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002877 ARRAY_LENGTH(p_lop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002878 numMatches,
2879 matches);
2880}
2881
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002882/*
2883 * The 'matchpairs' option is changed.
2884 */
2885 char *
2886did_set_matchpairs(optset_T *args)
2887{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002888 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002889 char_u *p;
2890
2891 if (has_mbyte)
2892 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002893 for (p = *varp; *p != NUL; ++p)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002894 {
2895 int x2 = -1;
2896 int x3 = -1;
2897
2898 p += mb_ptr2len(p);
2899 if (*p != NUL)
2900 x2 = *p++;
2901 if (*p != NUL)
2902 {
2903 x3 = mb_ptr2char(p);
2904 p += mb_ptr2len(p);
2905 }
2906 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
2907 return e_invalid_argument;
2908 if (*p == NUL)
2909 break;
2910 }
2911 }
2912 else
2913 {
2914 // Check for "x:y,x:y"
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002915 for (p = *varp; *p != NUL; p += 4)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002916 {
2917 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
2918 return e_invalid_argument;
2919 if (p[3] == NUL)
2920 break;
2921 }
2922 }
2923
2924 return NULL;
2925}
2926
2927#if defined(FEAT_SPELL) || defined(PROTO)
2928/*
2929 * The 'mkspellmem' option is changed.
2930 */
2931 char *
2932did_set_mkspellmem(optset_T *args UNUSED)
2933{
2934 if (spell_check_msm() != OK)
2935 return e_invalid_argument;
2936
2937 return NULL;
2938}
2939#endif
2940
2941/*
2942 * The 'mouse' option is changed.
2943 */
2944 char *
2945did_set_mouse(optset_T *args)
2946{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002947 char_u **varp = (char_u **)args->os_varp;
2948
Christian Brabandtb39b2402023-11-29 11:34:05 +01002949 return did_set_option_listflag(*varp, (char_u *)MOUSE_ALL, args->os_errbuf,
2950 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002951}
2952
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002953 int
2954expand_set_mouse(optexpand_T *args, int *numMatches, char_u ***matches)
2955{
2956 return expand_set_opt_listflag(args, (char_u*)MOUSE_ALL, numMatches, matches);
2957}
2958
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002959/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002960 * The 'mousemodel' option is changed.
2961 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002962 char *
2963did_set_mousemodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002964{
2965 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
2966 return e_invalid_argument;
2967#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
Ken Takata18d0d292023-12-19 20:12:29 +01002968 else if (*p_mousem != *args->os_oldval.string)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002969 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
2970 // to create or delete the popup menus.
2971 gui_motif_update_mousemodel(root_menu);
2972#endif
2973
2974 return NULL;
2975}
2976
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002977 int
2978expand_set_mousemodel(optexpand_T *args, int *numMatches, char_u ***matches)
2979{
2980 return expand_set_opt_string(
2981 args,
2982 p_mousem_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002983 ARRAY_LENGTH(p_mousem_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002984 numMatches,
2985 matches);
2986}
2987
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002988#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
2989 char *
2990did_set_mouseshape(optset_T *args UNUSED)
2991{
2992 char *errmsg = NULL;
2993
2994 errmsg = parse_shape_opt(SHAPE_MOUSE);
2995 update_mouseshape(-1);
2996
2997 return errmsg;
2998}
2999#endif
3000
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003001/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003002 * The 'nrformats' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003003 */
3004 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003005did_set_nrformats(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003006{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003007 char_u **varp = (char_u **)args->os_varp;
3008
3009 return did_set_opt_strings(*varp, p_nf_values, TRUE);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003010}
3011
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003012 int
3013expand_set_nrformats(optexpand_T *args, int *numMatches, char_u ***matches)
3014{
3015 return expand_set_opt_string(
3016 args,
3017 p_nf_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003018 ARRAY_LENGTH(p_nf_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003019 numMatches,
3020 matches);
3021}
3022
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003023#if defined(FEAT_EVAL) || defined(PROTO)
3024/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003025 * One of the '*expr' options is changed: 'balloonexpr', 'diffexpr',
3026 * 'foldexpr', 'foldtext', 'formatexpr', 'includeexpr', 'indentexpr',
3027 * 'patchexpr', 'printexpr' and 'charconvert'.
3028 *
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003029 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003030 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003031did_set_optexpr(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003032{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003033 char_u **varp = (char_u **)args->os_varp;
3034
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003035 // If the option value starts with <SID> or s:, then replace that with
3036 // the script identifier.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003037 char_u *name = get_scriptlocal_funcname(*varp);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003038 if (name != NULL)
3039 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003040 free_string_option(*varp);
3041 *varp = name;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003042 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003043
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003044 return NULL;
3045}
3046#endif
3047
3048/*
3049 * The 'pastetoggle' option is changed.
3050 */
3051 char *
3052did_set_pastetoggle(optset_T *args UNUSED)
3053{
3054 char_u *p;
3055
3056 // translate key codes like in a mapping
3057 if (*p_pt)
3058 {
zeertzjq7e0bae02023-08-11 23:15:38 +02003059 (void)replace_termcodes(p_pt, &p, 0,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003060 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
3061 if (p != NULL)
3062 {
3063 free_string_option(p_pt);
3064 p_pt = p;
3065 }
3066 }
3067
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003068 return NULL;
3069}
3070
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003071#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3072/*
3073 * The 'previewpopup' option is changed.
3074 */
3075 char *
3076did_set_previewpopup(optset_T *args UNUSED)
3077{
3078 if (parse_previewpopup(NULL) == FAIL)
3079 return e_invalid_argument;
3080
3081 return NULL;
3082}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003083
3084 int
3085expand_set_popupoption(optexpand_T *args, int *numMatches, char_u ***matches)
3086{
3087 expand_T *xp = args->oe_xp;
3088
3089 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3090 {
3091 // Within "highlight:"/"border:"/"align:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003092 int border_len = (int)STRLEN("border:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003093 if (xp->xp_pattern - args->oe_set_arg >= border_len &&
3094 STRNCMP(xp->xp_pattern - border_len, "border:", border_len) == 0)
3095 {
3096 return expand_set_opt_string(
3097 args,
3098 p_popup_option_border_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003099 ARRAY_LENGTH(p_popup_option_border_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003100 numMatches,
3101 matches);
3102 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003103 int align_len = (int)STRLEN("align:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003104 if (xp->xp_pattern - args->oe_set_arg >= align_len &&
3105 STRNCMP(xp->xp_pattern - align_len, "align:", align_len) == 0)
3106 {
3107 return expand_set_opt_string(
3108 args,
3109 p_popup_option_align_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003110 ARRAY_LENGTH(p_popup_option_align_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003111 numMatches,
3112 matches);
3113 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003114 int highlight_len = (int)STRLEN("highlight:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003115 if (xp->xp_pattern - args->oe_set_arg >= highlight_len &&
3116 STRNCMP(xp->xp_pattern - highlight_len, "highlight:", highlight_len) == 0)
3117 {
3118 // Return the list of all highlight names
3119 return expand_set_opt_generic(
3120 args,
3121 get_highlight_name,
3122 numMatches,
3123 matches);
3124 }
3125 return FAIL;
3126 }
3127
3128 return expand_set_opt_string(
3129 args,
3130 p_popup_option_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003131 ARRAY_LENGTH(p_popup_option_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003132 numMatches,
3133 matches);
3134}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003135#endif
3136
3137#if defined(FEAT_POSTSCRIPT) || defined(PROTO)
3138/*
3139 * The 'printencoding' option is changed.
3140 */
3141 char *
3142did_set_printencoding(optset_T *args UNUSED)
3143{
3144 char_u *s, *p;
3145
3146 // Canonize 'printencoding' if VIM standard one
3147 p = enc_canonize(p_penc);
3148 if (p != NULL)
3149 {
3150 vim_free(p_penc);
3151 p_penc = p;
3152 }
3153 else
3154 {
3155 // Ensure lower case and '-' for '_'
3156 for (s = p_penc; *s != NUL; s++)
3157 {
3158 if (*s == '_')
3159 *s = '-';
3160 else
3161 *s = TOLOWER_ASC(*s);
3162 }
3163 }
3164
3165 return NULL;
3166}
3167#endif
3168
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003169#if defined(FEAT_PRINTER) || defined(PROTO)
3170
3171 static char_u *
3172get_printoptions_names(expand_T *xp UNUSED, int idx)
3173{
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003174 if (idx >= (int)ARRAY_LENGTH(printer_opts))
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003175 return NULL;
3176 return (char_u*)printer_opts[idx].name;
3177}
3178
3179/*
3180 * Expand 'printoptions' option
3181 */
3182 int
3183expand_set_printoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3184{
3185 return expand_set_opt_generic(
3186 args,
3187 get_printoptions_names,
3188 numMatches,
3189 matches);
3190}
3191#endif
3192
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003193#if defined(FEAT_STL_OPT) || defined(PROTO)
3194/*
3195 * The 'statusline' or the 'tabline' or the 'rulerformat' option is changed.
3196 * "rulerformat" is TRUE if the 'rulerformat' option is changed.
3197 */
3198 static char *
3199parse_statustabline_rulerformat(optset_T *args, int rulerformat)
3200{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003201 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003202 char_u *s;
3203 char *errmsg = NULL;
3204 int wid;
3205
3206 if (rulerformat) // reset ru_wid first
3207 ru_wid = 0;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003208 s = *varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003209 if (rulerformat && *s == '%')
3210 {
3211 // set ru_wid if 'ruf' starts with "%99("
3212 if (*++s == '-') // ignore a '-'
3213 s++;
3214 wid = getdigits(&s);
3215 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
3216 ru_wid = wid;
3217 else
3218 errmsg = check_stl_option(p_ruf);
3219 }
3220 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
3221 else if (rulerformat || s[0] != '%' || s[1] != '!')
3222 errmsg = check_stl_option(s);
3223 if (rulerformat && errmsg == NULL)
3224 comp_col();
3225
3226 return errmsg;
3227}
3228#endif
3229
3230#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
3231/*
3232 * The 'renderoptions' option is changed.
3233 */
3234 char *
3235did_set_renderoptions(optset_T *args UNUSED)
3236{
3237 if (!gui_mch_set_rendering_options(p_rop))
3238 return e_invalid_argument;
3239
3240 return NULL;
3241}
3242#endif
3243
3244#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3245/*
3246 * The 'rightleftcmd' option is changed.
3247 */
3248 char *
3249did_set_rightleftcmd(optset_T *args)
3250{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003251 char_u **varp = (char_u **)args->os_varp;
3252
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003253 // Currently only "search" is a supported value.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003254 if (**varp != NUL && STRCMP(*varp, "search") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003255 return e_invalid_argument;
3256
3257 return NULL;
3258}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003259
3260 int
3261expand_set_rightleftcmd(optexpand_T *args, int *numMatches, char_u ***matches)
3262{
3263 static char *(p_rlc_values[]) = {"search", NULL};
3264 return expand_set_opt_string(
3265 args,
3266 p_rlc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003267 ARRAY_LENGTH(p_rlc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003268 numMatches,
3269 matches);
3270}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003271#endif
3272
3273#if defined(FEAT_STL_OPT) || defined(PROTO)
3274/*
3275 * The 'rulerformat' option is changed.
3276 */
3277 char *
3278did_set_rulerformat(optset_T *args)
3279{
3280 return parse_statustabline_rulerformat(args, TRUE);
3281}
3282#endif
3283
3284/*
3285 * The 'scrollopt' option is changed.
3286 */
3287 char *
3288did_set_scrollopt(optset_T *args UNUSED)
3289{
3290 return did_set_opt_strings(p_sbo, p_scbopt_values, TRUE);
3291}
3292
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003293 int
3294expand_set_scrollopt(optexpand_T *args, int *numMatches, char_u ***matches)
3295{
3296 return expand_set_opt_string(
3297 args,
3298 p_scbopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003299 ARRAY_LENGTH(p_scbopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003300 numMatches,
3301 matches);
3302}
3303
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003304/*
3305 * The 'selection' option is changed.
3306 */
3307 char *
3308did_set_selection(optset_T *args UNUSED)
3309{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003310 if (*p_sel == NUL || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003311 return e_invalid_argument;
3312
3313 return NULL;
3314}
3315
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003316 int
3317expand_set_selection(optexpand_T *args, int *numMatches, char_u ***matches)
3318{
3319 return expand_set_opt_string(
3320 args,
3321 p_sel_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003322 ARRAY_LENGTH(p_sel_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003323 numMatches,
3324 matches);
3325}
3326
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003327/*
3328 * The 'selectmode' option is changed.
3329 */
3330 char *
3331did_set_selectmode(optset_T *args UNUSED)
3332{
3333 return did_set_opt_strings(p_slm, p_slm_values, TRUE);
3334}
3335
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003336 int
3337expand_set_selectmode(optexpand_T *args, int *numMatches, char_u ***matches)
3338{
3339 return expand_set_opt_string(
3340 args,
3341 p_slm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003342 ARRAY_LENGTH(p_slm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003343 numMatches,
3344 matches);
3345}
3346
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003347#if defined(FEAT_SESSION) || defined(PROTO)
3348/*
3349 * The 'sessionoptions' option is changed.
3350 */
3351 char *
3352did_set_sessionoptions(optset_T *args)
3353{
3354 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
3355 return e_invalid_argument;
3356 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
3357 {
3358 // Don't allow both "sesdir" and "curdir".
3359 (void)opt_strings_flags(args->os_oldval.string, p_ssop_values,
3360 &ssop_flags, TRUE);
3361 return e_invalid_argument;
3362 }
3363
3364 return NULL;
3365}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003366
3367 int
3368expand_set_sessionoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3369{
3370 return expand_set_opt_string(
3371 args,
3372 p_ssop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003373 ARRAY_LENGTH(p_ssop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003374 numMatches,
3375 matches);
3376}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003377#endif
3378
3379/*
3380 * The 'shortmess' option is changed.
3381 */
3382 char *
3383did_set_shortmess(optset_T *args)
3384{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003385 char_u **varp = (char_u **)args->os_varp;
3386
Christian Brabandtb39b2402023-11-29 11:34:05 +01003387 return did_set_option_listflag(*varp, (char_u *)SHM_ALL, args->os_errbuf,
3388 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003389}
3390
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003391 int
3392expand_set_shortmess(optexpand_T *args, int *numMatches, char_u ***matches)
3393{
3394 return expand_set_opt_listflag(args, (char_u*)SHM_ALL, numMatches, matches);
3395}
3396
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003397#if defined(FEAT_LINEBREAK) || defined(PROTO)
3398/*
3399 * The 'showbreak' option is changed.
3400 */
3401 char *
3402did_set_showbreak(optset_T *args)
3403{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003404 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003405 char_u *s;
3406
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003407 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003408 {
3409 if (ptr2cells(s) != 1)
3410 return e_showbreak_contains_unprintable_or_wide_character;
3411 MB_PTR_ADV(s);
3412 }
3413
3414 return NULL;
3415}
3416#endif
3417
3418/*
3419 * The 'showcmdloc' option is changed.
3420 */
3421 char *
3422did_set_showcmdloc(optset_T *args UNUSED)
3423{
3424 return did_set_opt_strings(p_sloc, p_sloc_values, FALSE);
3425}
3426
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003427 int
3428expand_set_showcmdloc(optexpand_T *args, int *numMatches, char_u ***matches)
3429{
3430 return expand_set_opt_string(
3431 args,
3432 p_sloc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003433 ARRAY_LENGTH(p_sloc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003434 numMatches,
3435 matches);
3436}
3437
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003438#if defined(FEAT_SIGNS) || defined(PROTO)
3439/*
3440 * The 'signcolumn' option is changed.
3441 */
3442 char *
3443did_set_signcolumn(optset_T *args)
3444{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003445 char_u **varp = (char_u **)args->os_varp;
3446
3447 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003448 return e_invalid_argument;
3449 // When changing the 'signcolumn' to or from 'number', recompute the
3450 // width of the number column if 'number' or 'relativenumber' is set.
3451 if (((*args->os_oldval.string == 'n' && args->os_oldval.string[1] == 'u')
3452 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
3453 && (curwin->w_p_nu || curwin->w_p_rnu))
3454 curwin->w_nrwidth_line_count = 0;
3455
3456 return NULL;
3457}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003458
3459 int
3460expand_set_signcolumn(optexpand_T *args, int *numMatches, char_u ***matches)
3461{
3462 return expand_set_opt_string(
3463 args,
3464 p_scl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003465 ARRAY_LENGTH(p_scl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003466 numMatches,
3467 matches);
3468}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003469#endif
3470
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003471#if defined(FEAT_SPELL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003472/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003473 * The 'spellcapcheck' option is changed.
3474 */
3475 char *
3476did_set_spellcapcheck(optset_T *args UNUSED)
3477{
3478 // compile the regexp program.
3479 return compile_cap_prog(curwin->w_s);
3480}
3481
3482/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003483 * The 'spellfile' option is changed.
3484 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003485 char *
3486did_set_spellfile(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003487{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003488 char_u **varp = (char_u **)args->os_varp;
3489
3490 if (!valid_spellfile(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003491 return e_invalid_argument;
3492
3493 // If there is a window for this buffer in which 'spell' is set load the
3494 // wordlists.
3495 return did_set_spell_option(TRUE);
3496}
3497
3498/*
3499 * The 'spell' option is changed.
3500 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003501 char *
3502did_set_spelllang(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003503{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003504 char_u **varp = (char_u **)args->os_varp;
3505
3506 if (!valid_spelllang(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003507 return e_invalid_argument;
3508
3509 // If there is a window for this buffer in which 'spell' is set load the
3510 // wordlists.
3511 return did_set_spell_option(FALSE);
3512}
3513
3514/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003515 * The 'spelloptions' option is changed.
3516 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003517 char *
3518did_set_spelloptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003519{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003520 char_u **varp = (char_u **)args->os_varp;
3521
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003522 if (**varp != NUL && STRCMP(*varp, "camel") != 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003523 return e_invalid_argument;
3524
3525 return NULL;
3526}
3527
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003528 int
3529expand_set_spelloptions(optexpand_T *args, int *numMatches, char_u ***matches)
3530{
3531 static char *(p_spo_values[]) = {"camel", NULL};
3532 return expand_set_opt_string(
3533 args,
3534 p_spo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003535 ARRAY_LENGTH(p_spo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003536 numMatches,
3537 matches);
3538}
3539
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003540/*
3541 * The 'spellsuggest' option is changed.
3542 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003543 char *
3544did_set_spellsuggest(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003545{
3546 if (spell_check_sps() != OK)
3547 return e_invalid_argument;
3548
3549 return NULL;
3550}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003551
3552 int
3553expand_set_spellsuggest(optexpand_T *args, int *numMatches, char_u ***matches)
3554{
3555 return expand_set_opt_string(
3556 args,
3557 p_sps_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003558 ARRAY_LENGTH(p_sps_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003559 numMatches,
3560 matches);
3561}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003562#endif
3563
3564/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003565 * The 'splitkeep' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003566 */
3567 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003568did_set_splitkeep(optset_T *args UNUSED)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003569{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003570 return did_set_opt_strings(p_spk, p_spk_values, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003571}
3572
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003573 int
3574expand_set_splitkeep(optexpand_T *args, int *numMatches, char_u ***matches)
3575{
3576 return expand_set_opt_string(
3577 args,
3578 p_spk_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003579 ARRAY_LENGTH(p_spk_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003580 numMatches,
3581 matches);
3582}
3583
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00003584#if defined(FEAT_STL_OPT) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003585/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003586 * The 'statusline' option is changed.
3587 */
3588 char *
3589did_set_statusline(optset_T *args)
3590{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003591 return parse_statustabline_rulerformat(args, FALSE);
3592}
3593#endif
3594
3595/*
3596 * The 'swapsync' option is changed.
3597 */
3598 char *
3599did_set_swapsync(optset_T *args UNUSED)
3600{
3601 return did_set_opt_strings(p_sws, p_sws_values, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003602}
3603
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003604 int
3605expand_set_swapsync(optexpand_T *args, int *numMatches, char_u ***matches)
3606{
3607 return expand_set_opt_string(
3608 args,
3609 p_sws_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003610 ARRAY_LENGTH(p_sws_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003611 numMatches,
3612 matches);
3613}
3614
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003615/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003616 * The 'switchbuf' option is changed.
3617 */
3618 char *
3619did_set_switchbuf(optset_T *args UNUSED)
3620{
3621 return did_set_opt_flags(p_swb, p_swb_values, &swb_flags, TRUE);
3622}
3623
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003624 int
3625expand_set_switchbuf(optexpand_T *args, int *numMatches, char_u ***matches)
3626{
3627 return expand_set_opt_string(
3628 args,
3629 p_swb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003630 ARRAY_LENGTH(p_swb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003631 numMatches,
3632 matches);
3633}
3634
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003635#if defined(FEAT_STL_OPT) || defined(PROTO)
3636/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003637 * The 'tabline' option is changed.
3638 */
3639 char *
3640did_set_tabline(optset_T *args)
3641{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003642 return parse_statustabline_rulerformat(args, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003643}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003644#endif
3645
3646/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003647 * The 'tagcase' option is changed.
3648 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003649 char *
3650did_set_tagcase(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003651{
3652 unsigned int *flags;
3653 char_u *p;
3654
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003655 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003656 {
3657 p = curbuf->b_p_tc;
3658 flags = &curbuf->b_tc_flags;
3659 }
3660 else
3661 {
3662 p = p_tc;
3663 flags = &tc_flags;
3664 }
3665
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003666 if ((args->os_flags & OPT_LOCAL) && *p == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003667 // make the local value empty: use the global value
3668 *flags = 0;
3669 else if (*p == NUL
3670 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
3671 return e_invalid_argument;
3672
3673 return NULL;
3674}
3675
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003676 int
3677expand_set_tagcase(optexpand_T *args, int *numMatches, char_u ***matches)
3678{
3679 return expand_set_opt_string(
3680 args,
3681 p_tc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003682 ARRAY_LENGTH(p_tc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003683 numMatches,
3684 matches);
3685}
3686
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003687/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003688 * The 'term' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003689 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003690 char *
3691did_set_term(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003692{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003693 if (T_NAME[0] == NUL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003694 return e_cannot_set_term_to_empty_string;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003695#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003696 if (gui.in_use)
3697 return e_cannot_change_term_in_GUI;
3698 if (term_is_gui(T_NAME))
3699 return e_use_gui_to_start_GUI;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003700#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003701 if (set_termname(T_NAME) == FAIL)
3702 return e_not_found_in_termcap;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003703
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003704 // Screen colors may have changed.
3705 redraw_later_clear();
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003706
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003707 return NULL;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003708}
3709
3710/*
3711 * Some terminal option (t_xxx) is changed
3712 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003713 char *
3714did_set_term_option(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003715{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003716 char_u **varp = (char_u **)args->os_varp;
3717
3718 if (!full_screen)
3719 return NULL;
3720
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003721 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
3722 if (varp == &T_CCO)
3723 {
3724 int colors = atoi((char *)T_CCO);
3725
3726 // Only reinitialize colors if t_Co value has really changed to
3727 // avoid expensive reload of colorscheme if t_Co is set to the
3728 // same value multiple times.
3729 if (colors != t_colors)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003730 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003731 t_colors = colors;
3732 if (t_colors <= 1)
3733 {
3734 vim_free(T_CCO);
3735 T_CCO = empty_option;
3736 }
3737#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3738 if (is_term_win32())
3739 {
3740 swap_tcap();
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003741 args->os_did_swaptcap = TRUE;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003742 }
3743#endif
3744 // We now have a different color setup, initialize it again.
3745 init_highlight(TRUE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003746 }
3747 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003748 ttest(FALSE);
3749 if (varp == &T_ME)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003750 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003751 out_str(T_ME);
3752 redraw_later(UPD_CLEAR);
3753#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3754 // Since t_me has been set, this probably means that the user
3755 // wants to use this as default colors. Need to reset default
3756 // background/foreground colors.
3757# ifdef VIMDLL
3758 if (!gui.in_use && !gui.starting)
3759# endif
3760 mch_set_normal_colors();
3761#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003762 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003763 if (varp == &T_BE && termcap_active)
3764 {
3765 MAY_WANT_TO_LOG_THIS;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003766
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003767 if (*T_BE == NUL)
3768 // When clearing t_BE we assume the user no longer wants
3769 // bracketed paste, thus disable it by writing t_BD.
3770 out_str(T_BD);
3771 else
3772 out_str(T_BE);
3773 }
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003774
3775 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003776}
3777
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003778#if defined(FEAT_TERMINAL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003779/*
3780 * The 'termwinkey' option is changed.
3781 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003782 char *
3783did_set_termwinkey(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003784{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003785 if (*curwin->w_p_twk != NUL && string_to_key(curwin->w_p_twk, TRUE) == 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003786 return e_invalid_argument;
3787
3788 return NULL;
3789}
3790
3791/*
3792 * The 'termwinsize' option is changed.
3793 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003794 char *
3795did_set_termwinsize(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003796{
3797 char_u *p;
3798
3799 if (*curwin->w_p_tws == NUL)
3800 return NULL;
3801
3802 p = skipdigits(curwin->w_p_tws);
3803 if (p == curwin->w_p_tws
3804 || (*p != 'x' && *p != '*')
3805 || *skipdigits(p + 1) != NUL)
3806 return e_invalid_argument;
3807
3808 return NULL;
3809}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003810
3811# if defined(MSWIN) || defined(PROTO)
3812/*
3813 * The 'termwintype' option is changed.
3814 */
3815 char *
3816did_set_termwintype(optset_T *args UNUSED)
3817{
3818 return did_set_opt_strings(p_twt, p_twt_values, FALSE);
3819}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003820
3821 int
3822expand_set_termwintype(optexpand_T *args, int *numMatches, char_u ***matches)
3823{
3824 return expand_set_opt_string(
3825 args,
3826 p_twt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003827 ARRAY_LENGTH(p_twt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003828 numMatches,
3829 matches);
3830}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003831# endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003832#endif
3833
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003834/*
3835 * The 'titlestring' option is changed.
3836 */
3837 char *
3838did_set_titlestring(optset_T *args)
3839{
3840 int flagval = 0;
3841
3842#ifdef FEAT_STL_OPT
3843 flagval = STL_IN_TITLE;
3844#endif
3845 return parse_titleiconstring(args, flagval);
3846}
3847
3848#if (defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)) || defined(PROTO)
3849/*
3850 * The 'toolbar' option is changed.
3851 */
3852 char *
3853did_set_toolbar(optset_T *args UNUSED)
3854{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003855 if (opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags,
3856 TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003857 return e_invalid_argument;
3858
3859 out_flush();
3860 gui_mch_show_toolbar((toolbar_flags &
3861 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
3862 return NULL;
3863}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003864
3865 int
3866expand_set_toolbar(optexpand_T *args, int *numMatches, char_u ***matches)
3867{
3868 return expand_set_opt_string(
3869 args,
3870 p_toolbar_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003871 ARRAY_LENGTH(p_toolbar_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003872 numMatches,
3873 matches);
3874}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003875#endif
3876
3877#if (defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)) || defined(PROTO)
3878/*
3879 * The 'toolbariconsize' option is changed. GTK+ 2 only.
3880 */
3881 char *
3882did_set_toolbariconsize(optset_T *args UNUSED)
3883{
3884 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
3885 return e_invalid_argument;
3886
3887 out_flush();
3888 gui_mch_show_toolbar((toolbar_flags &
3889 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
3890 return NULL;
3891}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003892
3893 int
3894expand_set_toolbariconsize(optexpand_T *args, int *numMatches, char_u ***matches)
3895{
3896 return expand_set_opt_string(
3897 args,
3898 p_tbis_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003899 ARRAY_LENGTH(p_tbis_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003900 numMatches,
3901 matches);
3902}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003903#endif
3904
3905#if defined(UNIX) || defined(VMS) || defined(PROTO)
3906/*
3907 * The 'ttymouse' option is changed.
3908 */
3909 char *
3910did_set_ttymouse(optset_T *args UNUSED)
3911{
3912 char *errmsg = NULL;
3913
3914 // Switch the mouse off before changing the escape sequences used for
3915 // that.
3916 mch_setmouse(FALSE);
3917 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
3918 errmsg = e_invalid_argument;
3919 else
3920 check_mouse_termcode();
3921 if (termcap_active)
3922 setmouse(); // may switch it on again
3923
3924 return errmsg;
3925}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003926
3927 int
3928expand_set_ttymouse(optexpand_T *args, int *numMatches, char_u ***matches)
3929{
3930 return expand_set_opt_string(
3931 args,
3932 p_ttym_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003933 ARRAY_LENGTH(p_ttym_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003934 numMatches,
3935 matches);
3936}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003937#endif
3938
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003939#if defined(FEAT_VARTABS) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003940/*
3941 * The 'varsofttabstop' option is changed.
3942 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003943 char *
3944did_set_varsofttabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003945{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003946 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003947 char_u *cp;
3948
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003949 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00003950 VIM_CLEAR(curbuf->b_p_vsts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003951 else
3952 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003953 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003954 {
3955 if (vim_isdigit(*cp))
3956 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003957 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003958 continue;
3959 return e_invalid_argument;
3960 }
3961
3962 int *oldarray = curbuf->b_p_vsts_array;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003963 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003964 {
3965 if (oldarray)
3966 vim_free(oldarray);
3967 }
3968 else
3969 return e_invalid_argument;
3970 }
3971
3972 return NULL;
3973}
3974
3975/*
3976 * The 'vartabstop' option is changed.
3977 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003978 char *
3979did_set_vartabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003980{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003981 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003982 char_u *cp;
3983
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003984 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00003985 VIM_CLEAR(curbuf->b_p_vts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003986 else
3987 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003988 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003989 {
3990 if (vim_isdigit(*cp))
3991 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003992 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003993 continue;
3994 return e_invalid_argument;
3995 }
3996
3997 int *oldarray = curbuf->b_p_vts_array;
3998
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003999 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004000 {
4001 vim_free(oldarray);
4002# ifdef FEAT_FOLDING
4003 if (foldmethodIsIndent(curwin))
4004 foldUpdateAll(curwin);
4005# endif
4006 }
4007 else
4008 return e_invalid_argument;
4009 }
4010
4011 return NULL;
4012}
4013#endif
4014
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004015/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004016 * The 'verbosefile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004017 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004018 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004019did_set_verbosefile(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004020{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004021 verbose_stop();
4022 if (*p_vfile != NUL && verbose_open() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004023 return e_invalid_argument;
4024
4025 return NULL;
4026}
4027
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004028#if defined(FEAT_SESSION) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004029/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004030 * The 'viewoptions' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004031 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004032 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004033did_set_viewoptions(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004034{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004035 return did_set_opt_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004036}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004037#endif
4038
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004039#if defined(FEAT_VIMINFO) || defined(PROTO)
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004040/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004041 * The 'viminfo' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004042 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004043 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004044did_set_viminfo(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004045{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004046 char_u *s;
4047 char *errmsg = NULL;
4048
4049 for (s = p_viminfo; *s;)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004050 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004051 // Check it's a valid character
4052 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
4053 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01004054 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004055 break;
4056 }
4057 if (*s == 'n') // name is always last one
4058 break;
4059 else if (*s == 'r') // skip until next ','
4060 {
4061 while (*++s && *s != ',')
4062 ;
4063 }
4064 else if (*s == '%')
4065 {
4066 // optional number
4067 while (vim_isdigit(*++s))
4068 ;
4069 }
4070 else if (*s == '!' || *s == 'h' || *s == 'c')
4071 ++s; // no extra chars
4072 else // must have a number
4073 {
4074 while (vim_isdigit(*++s))
4075 ;
4076
4077 if (!VIM_ISDIGIT(*(s - 1)))
4078 {
4079 if (args->os_errbuf != NULL)
4080 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01004081 vim_snprintf(args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004082 _(e_missing_number_after_angle_str_angle),
4083 transchar_byte(*(s - 1)));
4084 errmsg = args->os_errbuf;
4085 }
4086 else
4087 errmsg = "";
4088 break;
4089 }
4090 }
4091 if (*s == ',')
4092 ++s;
4093 else if (*s)
4094 {
4095 if (args->os_errbuf != NULL)
4096 errmsg = e_missing_comma;
4097 else
4098 errmsg = "";
4099 break;
4100 }
4101 }
4102 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
4103 errmsg = e_must_specify_a_value;
4104
4105 return errmsg;
4106}
4107#endif
4108
4109/*
4110 * The 'virtualedit' option is changed.
4111 */
4112 char *
4113did_set_virtualedit(optset_T *args)
4114{
4115 char_u *ve = p_ve;
4116 unsigned int *flags = &ve_flags;
4117
4118 if (args->os_flags & OPT_LOCAL)
4119 {
4120 ve = curwin->w_p_ve;
4121 flags = &curwin->w_ve_flags;
4122 }
4123
4124 if ((args->os_flags & OPT_LOCAL) && *ve == NUL)
4125 // make the local value empty: use the global value
4126 *flags = 0;
4127 else
4128 {
4129 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
4130 return e_invalid_argument;
4131 else if (STRCMP(ve, args->os_oldval.string) != 0)
4132 {
4133 // Recompute cursor position in case the new 've' setting
4134 // changes something.
4135 validate_virtcol();
4136 coladvance(curwin->w_virtcol);
4137 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004138 }
4139
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004140 return NULL;
4141}
4142
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004143 int
4144expand_set_virtualedit(optexpand_T *args, int *numMatches, char_u ***matches)
4145{
4146 return expand_set_opt_string(
4147 args,
4148 p_ve_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004149 ARRAY_LENGTH(p_ve_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004150 numMatches,
4151 matches);
4152}
4153
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004154/*
4155 * The 'whichwrap' option is changed.
4156 */
4157 char *
4158did_set_whichwrap(optset_T *args)
4159{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004160 char_u **varp = (char_u **)args->os_varp;
4161
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004162 // Add ',' to the list flags because 'whichwrap' is a flag
4163 // list that is comma-separated.
Christian Brabandtb39b2402023-11-29 11:34:05 +01004164 return did_set_option_listflag(*varp, (char_u *)(WW_ALL ","),
4165 args->os_errbuf, args->os_errbuflen);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004166}
4167
4168 int
4169expand_set_whichwrap(optexpand_T *args, int *numMatches, char_u ***matches)
4170{
4171 return expand_set_opt_listflag(args, (char_u*)WW_ALL, numMatches, matches);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004172}
4173
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004174/*
4175 * The 'wildmode' option is changed.
4176 */
4177 char *
4178did_set_wildmode(optset_T *args UNUSED)
4179{
4180 if (check_opt_wim() == FAIL)
4181 return e_invalid_argument;
4182 return NULL;
4183}
4184
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004185 int
4186expand_set_wildmode(optexpand_T *args, int *numMatches, char_u ***matches)
4187{
4188 return expand_set_opt_string(
4189 args,
4190 p_wim_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004191 ARRAY_LENGTH(p_wim_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004192 numMatches,
4193 matches);
4194}
4195
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004196/*
4197 * The 'wildoptions' option is changed.
4198 */
4199 char *
4200did_set_wildoptions(optset_T *args UNUSED)
4201{
4202 return did_set_opt_strings(p_wop, p_wop_values, TRUE);
4203}
4204
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004205 int
4206expand_set_wildoptions(optexpand_T *args, int *numMatches, char_u ***matches)
4207{
4208 return expand_set_opt_string(
4209 args,
4210 p_wop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004211 ARRAY_LENGTH(p_wop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004212 numMatches,
4213 matches);
4214}
4215
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004216#if defined(FEAT_WAK) || defined(PROTO)
4217/*
4218 * The 'winaltkeys' option is changed.
4219 */
4220 char *
4221did_set_winaltkeys(optset_T *args UNUSED)
4222{
4223 char *errmsg = NULL;
4224
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004225 if (*p_wak == NUL || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004226 errmsg = e_invalid_argument;
4227# ifdef FEAT_MENU
4228# if defined(FEAT_GUI_MOTIF)
4229 else if (gui.in_use)
4230 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4231# elif defined(FEAT_GUI_GTK)
4232 else if (gui.in_use)
4233 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4234# endif
4235# endif
4236 return errmsg;
4237}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004238
4239 int
4240expand_set_winaltkeys(optexpand_T *args, int *numMatches, char_u ***matches)
4241{
4242 return expand_set_opt_string(
4243 args,
4244 p_wak_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004245 ARRAY_LENGTH(p_wak_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004246 numMatches,
4247 matches);
4248}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004249#endif
4250
4251/*
4252 * The 'wincolor' option is changed.
4253 */
4254 char *
4255did_set_wincolor(optset_T *args UNUSED)
4256{
4257#ifdef FEAT_TERMINAL
4258 term_update_wincolor(curwin);
4259#endif
4260 return NULL;
4261}
4262
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004263 int
4264expand_set_wincolor(optexpand_T *args, int *numMatches, char_u ***matches)
4265{
4266 return expand_set_opt_generic(
4267 args,
4268 get_highlight_name,
4269 numMatches,
4270 matches);
4271}
4272
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004273#ifdef FEAT_SYN_HL
4274/*
4275 * When the 'syntax' option is set, load the syntax of that name.
4276 */
4277 static void
4278do_syntax_autocmd(int value_changed)
4279{
4280 static int syn_recursive = 0;
4281
4282 ++syn_recursive;
4283 // Only pass TRUE for "force" when the value changed or not used
4284 // recursively, to avoid endless recurrence.
4285 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
4286 value_changed || syn_recursive == 1, curbuf);
4287 curbuf->b_flags |= BF_SYN_SET;
4288 --syn_recursive;
4289}
4290#endif
4291
4292/*
4293 * When the 'filetype' option is set, trigger the FileType autocommand.
4294 */
4295 static void
4296do_filetype_autocmd(char_u **varp, int opt_flags, int value_changed)
4297{
4298 // Skip this when called from a modeline and the filetype was already set
4299 // to this value.
4300 if ((opt_flags & OPT_MODELINE) && !value_changed)
4301 return;
4302
4303 static int ft_recursive = 0;
4304 int secure_save = secure;
4305
4306 // Reset the secure flag, since the value of 'filetype' has
4307 // been checked to be safe.
4308 secure = 0;
4309
4310 ++ft_recursive;
4311 did_filetype = TRUE;
4312 // Only pass TRUE for "force" when the value changed or not
4313 // used recursively, to avoid endless recurrence.
4314 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
4315 value_changed || ft_recursive == 1, curbuf);
4316 --ft_recursive;
4317 // Just in case the old "curbuf" is now invalid.
4318 if (varp != &(curbuf->b_p_ft))
4319 varp = NULL;
4320
4321 secure = secure_save;
4322}
4323
4324#ifdef FEAT_SPELL
4325/*
4326 * When the 'spelllang' option is set, source the spell/LANG.vim file in
4327 * 'runtimepath'.
4328 */
4329 static void
4330do_spelllang_source(void)
4331{
4332 char_u fname[200];
4333 char_u *p;
4334 char_u *q = curwin->w_s->b_p_spl;
4335
4336 // Skip the first name if it is "cjk".
4337 if (STRNCMP(q, "cjk,", 4) == 0)
4338 q += 4;
4339
4340 // They could set 'spellcapcheck' depending on the language. Use the first
4341 // name in 'spelllang' up to '_region' or '.encoding'.
4342 for (p = q; *p != NUL; ++p)
4343 if (!ASCII_ISALNUM(*p) && *p != '-')
4344 break;
4345 if (p > q)
4346 {
4347 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
4348 (int)(p - q), q);
4349 source_runtime(fname, DIP_ALL);
4350 }
4351}
4352#endif
4353
4354/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004355 * Handle string options that need some action to perform when changed.
zeertzjqf6782732022-07-27 18:26:03 +01004356 * The new value must be allocated.
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004357 * Returns NULL for success, or an untranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +02004358 */
4359 char *
4360did_set_string_option(
4361 int opt_idx, // index in options[] table
4362 char_u **varp, // pointer to the option variable
Bram Moolenaardac13472019-09-16 21:06:21 +02004363 char_u *oldval, // previous value of the option
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004364 char_u *value, // new value of the option
Bram Moolenaardac13472019-09-16 21:06:21 +02004365 char *errbuf, // buffer for errors, or NULL
Mike Williams620f0112023-12-05 15:36:06 +01004366 size_t errbuflen, // length of error buffer
Bram Moolenaardac13472019-09-16 21:06:21 +02004367 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004368 set_op_T op, // OP_ADDING/OP_PREPENDING/OP_REMOVING
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004369 int *value_checked) // value was checked to be safe, no
Bram Moolenaardac13472019-09-16 21:06:21 +02004370 // need to set P_INSECURE
4371{
4372 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +02004373 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004374 opt_did_set_cb_T did_set_cb = get_option_did_set_cb(opt_idx);
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004375 optset_T args;
4376
4377 // 'ttytype' is an alias for 'term'. Both 'term' and 'ttytype' point to
4378 // T_NAME. If 'term' or 'ttytype' is modified, then use the index for the
4379 // 'term' option. Only set the P_ALLOCED flag on 'term'.
4380 if (varp == &T_NAME)
4381 {
4382 opt_idx = findoption((char_u *)"term");
4383 if (opt_idx >= 0)
4384 {
4385 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
4386 did_set_cb = get_option_did_set_cb(opt_idx);
4387 }
4388 }
4389
4390 CLEAR_FIELD(args);
Bram Moolenaardac13472019-09-16 21:06:21 +02004391
Bram Moolenaardac13472019-09-16 21:06:21 +02004392 // Disallow changing some options from secure mode
4393 if ((secure
4394#ifdef HAVE_SANDBOX
4395 || sandbox != 0
4396#endif
4397 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00004398 errmsg = e_not_allowed_here;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004399 // Check for a "normal" directory or file name in some options.
4400 else if (check_illegal_path_names(opt_idx, varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004401 errmsg = e_invalid_argument;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004402 else if (did_set_cb != NULL)
4403 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004404 args.os_varp = (char_u *)varp;
4405 args.os_idx = opt_idx;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004406 args.os_flags = opt_flags;
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004407 args.os_op = op;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004408 args.os_oldval.string = oldval;
4409 args.os_newval.string = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004410 args.os_errbuf = errbuf;
Christian Brabandtb39b2402023-11-29 11:34:05 +01004411 args.os_errbuflen = errbuflen;
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004412 // Invoke the option specific callback function to validate and apply
4413 // the new option value.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004414 errmsg = did_set_cb(&args);
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004415
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004416 // The 'keymap', 'filetype' and 'syntax' option callback functions
4417 // may change the os_value_checked field.
4418 *value_checked = args.os_value_checked;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004419 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004420
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004421 // If an error is detected, restore the previous value.
Bram Moolenaardac13472019-09-16 21:06:21 +02004422 if (errmsg != NULL)
4423 {
zeertzjqf6782732022-07-27 18:26:03 +01004424 free_string_option(*varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02004425 *varp = oldval;
4426 // When resetting some values, need to act on it.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004427 if (args.os_restore_chartab)
Bram Moolenaardac13472019-09-16 21:06:21 +02004428 (void)init_chartab();
4429 if (varp == &p_hl)
4430 (void)highlight_changed();
4431 }
4432 else
4433 {
4434#ifdef FEAT_EVAL
4435 // Remember where the option was set.
4436 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4437#endif
4438 // Free string options that are in allocated memory.
4439 // Use "free_oldval", because recursiveness may change the flags under
4440 // our fingers (esp. init_highlight()).
4441 if (free_oldval)
4442 free_string_option(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01004443 set_option_flag(opt_idx, P_ALLOCED);
Bram Moolenaardac13472019-09-16 21:06:21 +02004444
4445 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
4446 && is_global_local_option(opt_idx))
4447 {
4448 // global option with local value set to use global value; free
4449 // the local value and make it empty
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004450 char_u *p = get_option_varp_scope(opt_idx, OPT_LOCAL);
Bram Moolenaardac13472019-09-16 21:06:21 +02004451 free_string_option(*(char_u **)p);
4452 *(char_u **)p = empty_option;
4453 }
4454
4455 // May set global value for local option.
4456 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
4457 set_string_option_global(opt_idx, varp);
4458
4459 // Trigger the autocommand only after setting the flags.
4460#ifdef FEAT_SYN_HL
Bram Moolenaardac13472019-09-16 21:06:21 +02004461 if (varp == &(curbuf->b_p_syn))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004462 do_syntax_autocmd(args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004463#endif
4464 else if (varp == &(curbuf->b_p_ft))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004465 do_filetype_autocmd(varp, opt_flags, args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004466#ifdef FEAT_SPELL
4467 if (varp == &(curwin->w_s->b_p_spl))
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004468 do_spelllang_source();
Bram Moolenaardac13472019-09-16 21:06:21 +02004469#endif
4470 }
4471
Bram Moolenaardac13472019-09-16 21:06:21 +02004472 if (varp == &p_mouse)
4473 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004474 if (*p_mouse == NUL)
4475 mch_setmouse(FALSE); // switch mouse off
4476 else
Bram Moolenaardac13472019-09-16 21:06:21 +02004477 setmouse(); // in case 'mouse' changed
4478 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004479
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004480#if defined(FEAT_LUA) || defined(PROTO)
4481 if (varp == &p_rtp)
4482 update_package_paths_in_lua();
4483#endif
4484
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004485#if defined(FEAT_LINEBREAK)
4486 // Changing Formatlistpattern when briopt includes the list setting:
4487 // redraw
4488 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
4489 && curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004490 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004491#endif
4492
Bram Moolenaardac13472019-09-16 21:06:21 +02004493 if (curwin->w_curswant != MAXCOL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004494 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02004495 curwin->w_set_curswant = TRUE;
4496
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004497 if ((opt_flags & OPT_NO_REDRAW) == 0)
4498 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004499#ifdef FEAT_GUI
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004500 // set when changing an option that only requires a redraw in the GUI
4501 int redraw_gui_only = FALSE;
4502
4503 if (varp == &p_go // 'guioptions'
4504 || varp == &p_guifont // 'guifont'
4505# ifdef FEAT_GUI_TABLINE
4506 || varp == &p_gtl // 'guitablabel'
4507 || varp == &p_gtt // 'guitabtooltip'
4508# endif
4509# ifdef FEAT_XFONTSET
4510 || varp == &p_guifontset // 'guifontset'
4511# endif
4512 || varp == &p_guifontwide // 'guifontwide'
4513# ifdef FEAT_GUI_GTK
4514 || varp == &p_guiligatures // 'guiligatures'
4515# endif
4516 )
4517 redraw_gui_only = TRUE;
4518
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004519 // check redraw when it's not a GUI option or the GUI is active.
4520 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02004521#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004522 check_redraw(get_option_flags(opt_idx));
4523 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004524
4525#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004526 if (args.os_did_swaptcap)
Bram Moolenaardac13472019-09-16 21:06:21 +02004527 {
4528 set_termname((char_u *)"win32");
4529 init_highlight(TRUE, FALSE);
4530 }
4531#endif
4532
4533 return errmsg;
4534}
4535
4536/*
4537 * Check an option that can be a range of string values.
4538 *
4539 * Return OK for correct value, FAIL otherwise.
4540 * Empty is always OK.
4541 */
4542 static int
4543check_opt_strings(
4544 char_u *val,
4545 char **values,
4546 int list) // when TRUE: accept a list of values
4547{
4548 return opt_strings_flags(val, values, NULL, list);
4549}
4550
4551/*
4552 * Handle an option that can be a range of string values.
4553 * Set a flag in "*flagp" for each string present.
4554 *
4555 * Return OK for correct value, FAIL otherwise.
4556 * Empty is always OK.
4557 */
4558 static int
4559opt_strings_flags(
4560 char_u *val, // new value
4561 char **values, // array of valid string values
4562 unsigned *flagp,
4563 int list) // when TRUE: accept a list of values
4564{
4565 int i;
4566 int len;
4567 unsigned new_flags = 0;
4568
4569 while (*val)
4570 {
4571 for (i = 0; ; ++i)
4572 {
4573 if (values[i] == NULL) // val not found in values[]
4574 return FAIL;
4575
4576 len = (int)STRLEN(values[i]);
4577 if (STRNCMP(values[i], val, len) == 0
4578 && ((list && val[len] == ',') || val[len] == NUL))
4579 {
4580 val += len + (val[len] == ',');
4581 new_flags |= (1 << i);
4582 break; // check next item in val list
4583 }
4584 }
4585 }
4586 if (flagp != NULL)
4587 *flagp = new_flags;
4588
4589 return OK;
4590}
4591
4592/*
4593 * return OK if "p" is a valid fileformat name, FAIL otherwise.
4594 */
4595 int
4596check_ff_value(char_u *p)
4597{
4598 return check_opt_strings(p, p_ff_values, FALSE);
4599}
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004600
4601/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004602 * Save the actual shortmess Flags and clear them temporarily to avoid that
4603 * file messages overwrites any output from the following commands.
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004604 *
4605 * Caller must make sure to first call save_clear_shm_value() and then
4606 * restore_shm_value() exactly the same number of times.
4607 */
4608 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004609save_clear_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004610{
4611 if (STRLEN(p_shm) >= SHM_LEN)
4612 {
4613 iemsg(e_internal_error_shortmess_too_long);
4614 return;
4615 }
4616
4617 if (++set_shm_recursive == 1)
4618 {
4619 STRCPY(shm_buf, p_shm);
4620 set_option_value_give_err((char_u *)"shm", 0L, (char_u *)"", 0);
4621 }
4622}
4623
4624/*
4625 * Restore the shortmess Flags set from the save_clear_shm_value() function.
4626 */
4627 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004628restore_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004629{
4630 if (--set_shm_recursive == 0)
4631 {
4632 set_option_value_give_err((char_u *)"shm", 0L, shm_buf, 0);
4633 vim_memset(shm_buf, 0, SHM_LEN);
4634 }
4635}