blob: 65a80afe1a680f6682c3ad90a13e374b6d1282f1 [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 "";
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +0100236 vim_snprintf((char *)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 *
1353did_set_global_listfillchars(char_u *val, int opt_lcs, int opt_flags)
1354{
1355 char *errmsg = NULL;
1356 char_u **local_ptr = opt_lcs ? &curwin->w_p_lcs : &curwin->w_p_fcs;
1357
1358 // only apply the global value to "curwin" when it does not have a
1359 // local value
1360 if (opt_lcs)
1361 errmsg = set_listchars_option(curwin, val,
1362 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL));
1363 else
1364 errmsg = set_fillchars_option(curwin, val,
1365 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL));
1366 if (errmsg != NULL)
1367 return errmsg;
1368
1369 tabpage_T *tp;
1370 win_T *wp;
1371
1372 // If the current window is set to use the global
1373 // 'listchars'/'fillchars' value, clear the window-local value.
1374 if (!(opt_flags & OPT_GLOBAL))
1375 clear_string_option(local_ptr);
1376 FOR_ALL_TAB_WINDOWS(tp, wp)
1377 {
1378 // If the current window has a local value need to apply it
1379 // again, it was changed when setting the global value.
1380 // If no error was returned above, we don't expect an error
1381 // here, so ignore the return value.
1382 if (opt_lcs)
1383 {
1384 if (*wp->w_p_lcs == NUL)
1385 (void)set_listchars_option(wp, wp->w_p_lcs, TRUE);
1386 }
1387 else
1388 {
1389 if (*wp->w_p_fcs == NUL)
1390 (void)set_fillchars_option(wp, wp->w_p_fcs, TRUE);
1391 }
1392 }
1393
1394 redraw_all_later(UPD_NOT_VALID);
1395
1396 return NULL;
1397}
1398
1399/*
1400 * The 'fillchars' option or the 'listchars' option is changed.
1401 */
1402 char *
1403did_set_chars_option(optset_T *args)
1404{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001405 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001406 char *errmsg = NULL;
1407
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001408 if ( varp == &p_lcs // global 'listchars'
1409 || varp == &p_fcs) // global 'fillchars'
1410 errmsg = did_set_global_listfillchars(*varp, varp == &p_lcs,
1411 args->os_flags);
1412 else if (varp == &curwin->w_p_lcs) // local 'listchars'
1413 errmsg = set_listchars_option(curwin, *varp, TRUE);
1414 else if (varp == &curwin->w_p_fcs) // local 'fillchars'
1415 errmsg = set_fillchars_option(curwin, *varp, TRUE);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001416
1417 return errmsg;
1418}
1419
1420/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001421 * Expand 'fillchars' or 'listchars' option value.
1422 */
1423 int
1424expand_set_chars_option(optexpand_T *args, int *numMatches, char_u ***matches)
1425{
1426 char_u **varp = (char_u **)args->oe_varp;
1427 int is_lcs = (varp == &p_lcs || varp == &curwin->w_p_lcs);
1428 return expand_set_opt_generic(
1429 args,
1430 is_lcs ? get_listchars_name : get_fillchars_name,
1431 numMatches,
1432 matches);
1433}
1434
1435/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001436 * The 'cinoptions' option is changed.
1437 */
1438 char *
1439did_set_cinoptions(optset_T *args UNUSED)
1440{
1441 // TODO: recognize errors
1442 parse_cino(curbuf);
1443
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001444 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001445}
1446
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00001447#if defined(FEAT_SYN_HL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001448/*
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001449 * The 'colorcolumn' option is changed.
1450 */
1451 char *
1452did_set_colorcolumn(optset_T *args UNUSED)
1453{
1454 return check_colorcolumn(curwin);
1455}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001456#endif
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001457
1458/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001459 * The 'comments' option is changed.
1460 */
1461 char *
1462did_set_comments(optset_T *args)
1463{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001464 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001465 char_u *s;
1466 char *errmsg = NULL;
1467
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001468 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001469 {
1470 while (*s && *s != ':')
1471 {
1472 if (vim_strchr((char_u *)COM_ALL, *s) == NULL
1473 && !VIM_ISDIGIT(*s) && *s != '-')
1474 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01001475 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001476 break;
1477 }
1478 ++s;
1479 }
1480 if (*s++ == NUL)
1481 errmsg = e_missing_colon;
1482 else if (*s == ',' || *s == NUL)
1483 errmsg = e_zero_length_string;
1484 if (errmsg != NULL)
1485 break;
1486 while (*s && *s != ',')
1487 {
1488 if (*s == '\\' && s[1] != NUL)
1489 ++s;
1490 ++s;
1491 }
1492 s = skip_to_option_part(s);
1493 }
1494
1495 return errmsg;
1496}
1497
1498#if defined(FEAT_FOLDING) || defined(PROTO)
1499/*
1500 * The 'commentstring' option is changed.
1501 */
1502 char *
1503did_set_commentstring(optset_T *args)
1504{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001505 char_u **varp = (char_u **)args->os_varp;
1506
1507 if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001508 return e_commentstring_must_be_empty_or_contain_str;
1509
1510 return NULL;
1511}
1512#endif
1513
1514/*
1515 * The 'complete' option is changed.
1516 */
1517 char *
1518did_set_complete(optset_T *args)
1519{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001520 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001521 char_u *s;
1522
1523 // check if it is a valid value for 'complete' -- Acevedo
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001524 for (s = *varp; *s;)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001525 {
1526 while (*s == ',' || *s == ' ')
1527 s++;
1528 if (!*s)
1529 break;
1530 if (vim_strchr((char_u *)".wbuksid]tU", *s) == NULL)
Christian Brabandtb39b2402023-11-29 11:34:05 +01001531 return illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001532 if (*++s != NUL && *s != ',' && *s != ' ')
1533 {
1534 if (s[-1] == 'k' || s[-1] == 's')
1535 {
1536 // skip optional filename after 'k' and 's'
1537 while (*s && *s != ',' && *s != ' ')
1538 {
1539 if (*s == '\\' && s[1] != NUL)
1540 ++s;
1541 ++s;
1542 }
1543 }
1544 else
1545 {
1546 if (args->os_errbuf != NULL)
1547 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01001548 vim_snprintf((char *)args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001549 _(e_illegal_character_after_chr), *--s);
1550 return args->os_errbuf;
1551 }
1552 return "";
1553 }
1554 }
1555 }
1556
1557 return NULL;
1558}
1559
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001560 int
1561expand_set_complete(optexpand_T *args, int *numMatches, char_u ***matches)
1562{
1563 static char *(p_cpt_values[]) = {
1564 ".", "w", "b", "u", "k", "kspell", "s", "i", "d", "]", "t", "U",
1565 NULL};
1566 return expand_set_opt_string(
1567 args,
1568 p_cpt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001569 ARRAY_LENGTH(p_cpt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001570 numMatches,
1571 matches);
1572}
1573
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001574/*
1575 * The 'completeopt' option is changed.
1576 */
1577 char *
1578did_set_completeopt(optset_T *args UNUSED)
1579{
1580 if (check_opt_strings(p_cot, p_cot_values, TRUE) != OK)
1581 return e_invalid_argument;
1582
1583 completeopt_was_set();
1584 return NULL;
1585}
1586
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001587 int
1588expand_set_completeopt(optexpand_T *args, int *numMatches, char_u ***matches)
1589{
1590 return expand_set_opt_string(
1591 args,
1592 p_cot_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001593 ARRAY_LENGTH(p_cot_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001594 numMatches,
1595 matches);
1596}
1597
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001598#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1599/*
1600 * The 'completepopup' option is changed.
1601 */
1602 char *
1603did_set_completepopup(optset_T *args UNUSED)
1604{
1605 if (parse_completepopup(NULL) == FAIL)
1606 return e_invalid_argument;
1607
1608 popup_close_info();
1609 return NULL;
1610}
1611#endif
1612
1613#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1614/*
1615 * The 'completeslash' option is changed.
1616 */
1617 char *
1618did_set_completeslash(optset_T *args UNUSED)
1619{
1620 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1621 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
1622 return e_invalid_argument;
1623
1624 return NULL;
1625}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001626
1627 int
1628expand_set_completeslash(optexpand_T *args, int *numMatches, char_u ***matches)
1629{
1630 return expand_set_opt_string(
1631 args,
1632 p_csl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001633 ARRAY_LENGTH(p_csl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001634 numMatches,
1635 matches);
1636}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001637#endif
1638
1639#if defined(FEAT_CONCEAL) || defined(PROTO)
1640/*
1641 * The 'concealcursor' option is changed.
1642 */
1643 char *
1644did_set_concealcursor(optset_T *args)
1645{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001646 char_u **varp = (char_u **)args->os_varp;
1647
Christian Brabandtb39b2402023-11-29 11:34:05 +01001648 return did_set_option_listflag(*varp, (char_u *)COCU_ALL, args->os_errbuf,
1649 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001650}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001651
1652 int
1653expand_set_concealcursor(optexpand_T *args, int *numMatches, char_u ***matches)
1654{
1655 return expand_set_opt_listflag(args, (char_u*)COCU_ALL, numMatches, matches);
1656}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001657#endif
1658
1659/*
1660 * The 'cpoptions' option is changed.
1661 */
1662 char *
1663did_set_cpoptions(optset_T *args)
1664{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001665 char_u **varp = (char_u **)args->os_varp;
1666
Christian Brabandtb39b2402023-11-29 11:34:05 +01001667 return did_set_option_listflag(*varp, (char_u *)CPO_ALL, args->os_errbuf,
1668 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001669}
1670
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001671 int
1672expand_set_cpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
1673{
1674 return expand_set_opt_listflag(args, (char_u*)CPO_ALL, numMatches, matches);
1675}
1676
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001677#if defined(FEAT_CRYPT) || defined(PROTO)
1678/*
1679 * The 'cryptkey' option is changed.
1680 */
1681 char *
1682did_set_cryptkey(optset_T *args)
1683{
1684 // Make sure the ":set" command doesn't show the new value in the
1685 // history.
1686 remove_key_from_history();
1687
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02001688 if (args->os_op != OP_NONE)
1689 // Don't allow set+=/-=/^= as they can allow for substring guessing
1690 return e_invalid_argument;
1691
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001692 if (STRCMP(curbuf->b_p_key, args->os_oldval.string) != 0)
1693 {
1694 // Need to update the swapfile.
1695 ml_set_crypt_key(curbuf, args->os_oldval.string,
1696 *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
1697 changed_internal();
1698 }
Christian Brabandt19e6c4f2023-06-27 18:57:10 +01001699# ifdef FEAT_SODIUM
1700 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
1701 crypt_sodium_lock_key(args->os_newval.string);
1702# endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001703
1704 return NULL;
1705}
1706
1707/*
1708 * The 'cryptmethod' option is changed.
1709 */
1710 char *
1711did_set_cryptmethod(optset_T *args)
1712{
1713 char_u *p;
1714 char_u *s;
1715
1716 if (args->os_flags & OPT_LOCAL)
1717 p = curbuf->b_p_cm;
1718 else
1719 p = p_cm;
1720 if (check_opt_strings(p, p_cm_values, TRUE) != OK)
1721 return e_invalid_argument;
1722 else if (crypt_self_test() == FAIL)
1723 return e_invalid_argument;
1724
1725 // When setting the global value to empty, make it "zip".
1726 if (*p_cm == NUL)
1727 {
1728 free_string_option(p_cm);
1729 p_cm = vim_strsave((char_u *)"zip");
1730 }
1731 // When using ":set cm=name" the local value is going to be empty.
1732 // Do that here, otherwise the crypt functions will still use the
1733 // local value.
1734 if ((args->os_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1735 {
1736 free_string_option(curbuf->b_p_cm);
1737 curbuf->b_p_cm = empty_option;
1738 }
1739
1740 // Need to update the swapfile when the effective method changed.
1741 // Set "s" to the effective old value, "p" to the effective new
1742 // method and compare.
1743 if ((args->os_flags & OPT_LOCAL) && *args->os_oldval.string == NUL)
1744 s = p_cm; // was previously using the global value
1745 else
1746 s = args->os_oldval.string;
1747 if (*curbuf->b_p_cm == NUL)
1748 p = p_cm; // is now using the global value
1749 else
1750 p = curbuf->b_p_cm;
1751 if (STRCMP(s, p) != 0)
1752 ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1753
1754 // If the global value changes need to update the swapfile for all
1755 // buffers using that value.
1756 if ((args->os_flags & OPT_GLOBAL)
1757 && STRCMP(p_cm, args->os_oldval.string) != 0)
1758 {
1759 buf_T *buf;
1760
1761 FOR_ALL_BUFFERS(buf)
1762 if (buf != curbuf && *buf->b_p_cm == NUL)
1763 ml_set_crypt_key(buf, buf->b_p_key, args->os_oldval.string);
1764 }
1765 return NULL;
1766}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001767
1768 int
1769expand_set_cryptmethod(optexpand_T *args, int *numMatches, char_u ***matches)
1770{
1771 return expand_set_opt_string(
1772 args,
1773 p_cm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001774 ARRAY_LENGTH(p_cm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001775 numMatches,
1776 matches);
1777}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001778#endif
1779
1780#if (defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1781/*
1782 * The 'cscopequickfix' option is changed.
1783 */
1784 char *
1785did_set_cscopequickfix(optset_T *args UNUSED)
1786{
1787 char_u *p;
1788
1789 if (p_csqf == NULL)
1790 return NULL;
1791
1792 p = p_csqf;
1793 while (*p != NUL)
1794 {
1795 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
1796 || p[1] == NUL
1797 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
1798 || (p[2] != NUL && p[2] != ','))
1799 return e_invalid_argument;
1800 else if (p[2] == NUL)
1801 break;
1802 else
1803 p += 3;
1804 }
1805
1806 return NULL;
1807}
1808#endif
1809
1810#if defined(FEAT_SYN_HL) || defined(PROTO)
1811/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001812 * The 'cursorlineopt' option is changed.
1813 */
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001814 char *
1815did_set_cursorlineopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001816{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001817 char_u **varp = (char_u **)args->os_varp;
1818
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001819 // This could be changed to use opt_strings_flags() instead.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001820 if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001821 return e_invalid_argument;
1822
1823 return NULL;
1824}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001825
1826 int
1827expand_set_cursorlineopt(optexpand_T *args, int *numMatches, char_u ***matches)
1828{
1829 return expand_set_opt_string(
1830 args,
1831 p_culopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001832 ARRAY_LENGTH(p_culopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001833 numMatches,
1834 matches);
1835}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001836#endif
1837
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001838/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001839 * The 'debug' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001840 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001841 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001842did_set_debug(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001843{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001844 return did_set_opt_strings(p_debug, p_debug_values, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001845}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001846
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001847 int
1848expand_set_debug(optexpand_T *args, int *numMatches, char_u ***matches)
1849{
1850 return expand_set_opt_string(
1851 args,
1852 p_debug_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001853 ARRAY_LENGTH(p_debug_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001854 numMatches,
1855 matches);
1856}
1857
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001858#if defined(FEAT_DIFF) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001859/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001860 * The 'diffopt' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001861 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001862 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001863did_set_diffopt(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001864{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001865 if (diffopt_changed() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001866 return e_invalid_argument;
1867
1868 return NULL;
1869}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001870
1871 int
1872expand_set_diffopt(optexpand_T *args, int *numMatches, char_u ***matches)
1873{
1874 expand_T *xp = args->oe_xp;
1875
1876 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
1877 {
1878 // Within "algorithm:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001879 int algo_len = (int)STRLEN("algorithm:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001880 if (xp->xp_pattern - args->oe_set_arg >= algo_len &&
1881 STRNCMP(xp->xp_pattern - algo_len, "algorithm:", algo_len) == 0)
1882 {
1883 return expand_set_opt_string(
1884 args,
1885 p_dip_algorithm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001886 ARRAY_LENGTH(p_dip_algorithm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001887 numMatches,
1888 matches);
1889 }
1890 return FAIL;
1891 }
1892
1893 return expand_set_opt_string(
1894 args,
1895 p_dip_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001896 ARRAY_LENGTH(p_dip_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001897 numMatches,
1898 matches);
1899}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001900#endif
1901
1902/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001903 * The 'display' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001904 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001905 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001906did_set_display(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001907{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001908 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001909 return e_invalid_argument;
1910
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001911 (void)init_chartab();
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001912 return NULL;
1913}
1914
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001915 int
1916expand_set_display(optexpand_T *args, int *numMatches, char_u ***matches)
1917{
1918 return expand_set_opt_string(
1919 args,
1920 p_dy_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001921 ARRAY_LENGTH(p_dy_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001922 numMatches,
1923 matches);
1924}
1925
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001926/*
1927 * The 'eadirection' option is changed.
1928 */
1929 char *
1930did_set_eadirection(optset_T *args UNUSED)
1931{
1932 return did_set_opt_strings(p_ead, p_ead_values, FALSE);
1933}
1934
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001935 int
1936expand_set_eadirection(optexpand_T *args, int *numMatches, char_u ***matches)
1937{
1938 return expand_set_opt_string(
1939 args,
1940 p_ead_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001941 ARRAY_LENGTH(p_ead_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001942 numMatches,
1943 matches);
1944}
1945
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001946/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001947 * One of the 'encoding', 'fileencoding', 'termencoding' or 'makeencoding'
1948 * options is changed.
1949 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001950 char *
1951did_set_encoding(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001952{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001953 char_u **varp = (char_u **)args->os_varp;
1954 char_u **gvarp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001955 char *errmsg = NULL;
1956 char_u *p;
1957
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001958 // Get the global option to compare with, otherwise we would have to check
1959 // two values for all local options.
1960 gvarp = (char_u **)get_option_varp_scope(args->os_idx, OPT_GLOBAL);
1961
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001962 if (gvarp == &p_fenc)
1963 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001964 if (!curbuf->b_p_ma && args->os_flags != OPT_GLOBAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001965 errmsg = e_cannot_make_changes_modifiable_is_off;
1966 else if (vim_strchr(*varp, ',') != NULL)
1967 // No comma allowed in 'fileencoding'; catches confusing it
1968 // with 'fileencodings'.
1969 errmsg = e_invalid_argument;
1970 else
1971 {
1972 // May show a "+" in the title now.
1973 redraw_titles();
1974 // Add 'fileencoding' to the swap file.
1975 ml_setflags(curbuf);
1976 }
1977 }
1978 if (errmsg == NULL)
1979 {
1980 // canonize the value, so that STRCMP() can be used on it
1981 p = enc_canonize(*varp);
1982 if (p != NULL)
1983 {
1984 vim_free(*varp);
1985 *varp = p;
1986 }
1987 if (varp == &p_enc)
1988 {
1989 errmsg = mb_init();
1990 redraw_titles();
1991 }
1992 }
1993
1994#if defined(FEAT_GUI_GTK)
1995 if (errmsg == NULL && varp == &p_tenc && gui.in_use)
1996 {
1997 // GTK uses only a single encoding, and that is UTF-8.
1998 if (STRCMP(p_tenc, "utf-8") != 0)
1999 errmsg = e_cannot_be_changed_in_gtk_GUI;
2000 }
2001#endif
2002
2003 if (errmsg == NULL)
2004 {
2005#ifdef FEAT_KEYMAP
2006 // When 'keymap' is used and 'encoding' changes, reload the keymap
2007 // (with another encoding).
2008 if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
2009 (void)keymap_init();
2010#endif
2011
2012 // When 'termencoding' is not empty and 'encoding' changes or when
2013 // 'termencoding' changes, need to setup for keyboard input and
2014 // display output conversion.
2015 if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
2016 {
2017 if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
2018 || convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
2019 {
2020 semsg(_(e_cannot_convert_between_str_and_str),
2021 p_tenc, p_enc);
2022 errmsg = e_invalid_argument;
2023 }
2024 }
2025
2026#if defined(MSWIN)
K.Takatace3189d2023-02-15 19:13:43 +00002027 // $HOME, $VIM and $VIMRUNTIME may have characters in active code page.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002028 if (varp == &p_enc)
K.Takatace3189d2023-02-15 19:13:43 +00002029 {
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002030 init_homedir();
K.Takatace3189d2023-02-15 19:13:43 +00002031 init_vimdir();
2032 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002033#endif
2034 }
2035
2036 return errmsg;
2037}
2038
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002039 int
2040expand_set_encoding(optexpand_T *args, int *numMatches, char_u ***matches)
2041{
2042 return expand_set_opt_generic(
2043 args,
2044 get_encoding_name,
2045 numMatches,
2046 matches);
2047}
2048
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002049/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002050 * The 'eventignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002051 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002052 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002053did_set_eventignore(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002054{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002055 if (check_ei() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002056 return e_invalid_argument;
2057 return NULL;
2058}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002059
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002060 static char_u *
2061get_eventignore_name(expand_T *xp, int idx)
2062{
2063 // 'eventignore' allows special keyword "all" in addition to
2064 // all event names.
2065 if (idx == 0)
2066 return (char_u *)"all";
2067 return get_event_name_no_group(xp, idx - 1);
2068}
2069
2070 int
2071expand_set_eventignore(optexpand_T *args, int *numMatches, char_u ***matches)
2072{
2073 return expand_set_opt_generic(
2074 args,
2075 get_eventignore_name,
2076 numMatches,
2077 matches);
2078}
2079
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002080/*
2081 * The 'fileformat' option is changed.
2082 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002083 char *
2084did_set_fileformat(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002085{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002086 char_u **varp = (char_u **)args->os_varp;
2087
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002088 if (!curbuf->b_p_ma && !(args->os_flags & OPT_GLOBAL))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002089 return e_cannot_make_changes_modifiable_is_off;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002090 else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002091 return e_invalid_argument;
2092
2093 // may also change 'textmode'
2094 if (get_fileformat(curbuf) == EOL_DOS)
2095 curbuf->b_p_tx = TRUE;
2096 else
2097 curbuf->b_p_tx = FALSE;
2098 redraw_titles();
2099 // update flag in swap file
2100 ml_setflags(curbuf);
2101 // Redraw needed when switching to/from "mac": a CR in the text
2102 // will be displayed differently.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002103 if (get_fileformat(curbuf) == EOL_MAC || *args->os_oldval.string == 'm')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002104 redraw_curbuf_later(UPD_NOT_VALID);
2105
2106 return NULL;
2107}
2108
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002109 int
2110expand_set_fileformat(optexpand_T *args, int *numMatches, char_u ***matches)
2111{
2112 return expand_set_opt_string(
2113 args,
2114 p_ff_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002115 ARRAY_LENGTH(p_ff_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002116 numMatches,
2117 matches);
2118}
2119
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002120/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002121 * Function given to ExpandGeneric() to obtain the possible arguments of the
2122 * fileformat options.
2123 */
2124 char_u *
2125get_fileformat_name(expand_T *xp UNUSED, int idx)
2126{
2127 if (idx >= (int)ARRAY_LENGTH(p_ff_values))
2128 return NULL;
2129
2130 return (char_u*)p_ff_values[idx];
2131}
2132
2133/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002134 * The 'fileformats' option is changed.
2135 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002136 char *
2137did_set_fileformats(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002138{
2139 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
2140 return e_invalid_argument;
2141
2142 // also change 'textauto'
2143 if (*p_ffs == NUL)
2144 p_ta = FALSE;
2145 else
2146 p_ta = TRUE;
2147
2148 return NULL;
2149}
2150
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002151/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002152 * The 'filetype' or the 'syntax' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002153 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002154 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002155did_set_filetype_or_syntax(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002156{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002157 char_u **varp = (char_u **)args->os_varp;
2158
2159 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002160 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002161
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002162 args->os_value_changed = STRCMP(args->os_oldval.string, *varp) != 0;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002163
2164 // Since we check the value, there is no need to set P_INSECURE,
2165 // even when the value comes from a modeline.
2166 args->os_value_checked = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002167
2168 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002169}
2170
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002171#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002172/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002173 * The 'foldclose' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002174 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002175 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002176did_set_foldclose(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002177{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002178 return did_set_opt_strings(p_fcl, p_fcl_values, TRUE);
2179}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002180
2181 int
2182expand_set_foldclose(optexpand_T *args, int *numMatches, char_u ***matches)
2183{
2184 return expand_set_opt_string(
2185 args,
2186 p_fcl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002187 ARRAY_LENGTH(p_fcl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002188 numMatches,
2189 matches);
2190}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002191#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002192
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002193#if (defined(FEAT_EVAL) && defined(FEAT_FOLDING)) || defined(PROTO)
2194/*
2195 * The 'foldexpr' option is changed.
2196 */
2197 char *
2198did_set_foldexpr(optset_T *args)
2199{
2200 (void)did_set_optexpr(args);
2201 if (foldmethodIsExpr(curwin))
2202 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002203 return NULL;
2204}
2205#endif
2206
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002207#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002208/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002209 * The 'foldignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002210 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002211 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002212did_set_foldignore(optset_T *args UNUSED)
2213{
2214 if (foldmethodIsIndent(curwin))
2215 foldUpdateAll(curwin);
2216 return NULL;
2217}
2218
2219/*
2220 * The 'foldmarker' option is changed.
2221 */
2222 char *
2223did_set_foldmarker(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002224{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002225 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002226 char_u *p;
2227
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002228 p = vim_strchr(*varp, ',');
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002229 if (p == NULL)
2230 return e_comma_required;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002231 else if (p == *varp || p[1] == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002232 return e_invalid_argument;
2233 else if (foldmethodIsMarker(curwin))
2234 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002235
2236 return NULL;
2237}
2238
2239/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002240 * The 'foldmethod' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002241 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002242 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002243did_set_foldmethod(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002244{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002245 char_u **varp = (char_u **)args->os_varp;
2246
2247 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002248 || *curwin->w_p_fdm == NUL)
2249 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002250
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002251 foldUpdateAll(curwin);
2252 if (foldmethodIsDiff(curwin))
2253 newFoldLevel();
2254 return NULL;
2255}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002256
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002257 int
2258expand_set_foldmethod(optexpand_T *args, int *numMatches, char_u ***matches)
2259{
2260 return expand_set_opt_string(
2261 args,
2262 p_fdm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002263 ARRAY_LENGTH(p_fdm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002264 numMatches,
2265 matches);
2266}
2267
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002268/*
2269 * The 'foldopen' option is changed.
2270 */
2271 char *
2272did_set_foldopen(optset_T *args UNUSED)
2273{
2274 return did_set_opt_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
2275}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002276
2277 int
2278expand_set_foldopen(optexpand_T *args, int *numMatches, char_u ***matches)
2279{
2280 return expand_set_opt_string(
2281 args,
2282 p_fdo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002283 ARRAY_LENGTH(p_fdo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002284 numMatches,
2285 matches);
2286}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002287#endif
2288
2289/*
2290 * The 'formatoptions' option is changed.
2291 */
2292 char *
2293did_set_formatoptions(optset_T *args)
2294{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002295 char_u **varp = (char_u **)args->os_varp;
2296
Christian Brabandtb39b2402023-11-29 11:34:05 +01002297 return did_set_option_listflag(*varp, (char_u *)FO_ALL, args->os_errbuf,
2298 args->os_errbuflen);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002299}
2300
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002301 int
2302expand_set_formatoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2303{
2304 return expand_set_opt_listflag(args, (char_u*)FO_ALL, numMatches, matches);
2305}
2306
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002307#if defined(CURSOR_SHAPE) || defined(PROTO)
2308/*
2309 * The 'guicursor' option is changed.
2310 */
2311 char *
2312did_set_guicursor(optset_T *args UNUSED)
2313{
2314 return parse_shape_opt(SHAPE_CURSOR);
2315}
2316#endif
2317
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002318#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002319/*
2320 * The 'guifont' option is changed.
2321 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002322 char *
2323did_set_guifont(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002324{
2325 char_u *p;
2326 char *errmsg = NULL;
2327
2328 if (gui.in_use)
2329 {
2330 p = p_guifont;
2331# if defined(FEAT_GUI_GTK)
2332 // Put up a font dialog and let the user select a new value.
2333 // If this is cancelled go back to the old value but don't
2334 // give an error message.
2335 if (STRCMP(p, "*") == 0)
2336 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002337 p = gui_mch_font_dialog(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002338 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002339 p_guifont = (p != NULL) ? p : vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002340 }
2341# endif
2342 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
2343 {
2344# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
2345 if (STRCMP(p_guifont, "*") == 0)
2346 {
2347 // Dialog was cancelled: Keep the old value without giving
2348 // an error message.
2349 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002350 p_guifont = vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002351 }
2352 else
2353# endif
2354 errmsg = e_invalid_fonts;
2355 }
2356 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002357
2358 return errmsg;
2359}
2360
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002361/*
2362 * Expand the 'guifont' option. Only when GUI is being used. Each platform has
2363 * specific behaviors.
2364 */
2365 int
2366expand_set_guifont(optexpand_T *args, int *numMatches, char_u ***matches)
2367{
2368 if (!gui.in_use)
2369 return FAIL;
2370
2371# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
2372 char_u **varp = (char_u **)args->oe_varp;
2373 int wide = (varp == &p_guifontwide);
2374
2375 return expand_set_opt_callback(
2376 args, gui_mch_expand_font, &wide, numMatches, matches);
2377# else
2378 return FAIL;
2379# endif
2380}
2381
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002382# if defined(FEAT_XFONTSET) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002383/*
2384 * The 'guifontset' option is changed.
2385 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002386 char *
2387did_set_guifontset(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002388{
2389 char *errmsg = NULL;
2390
2391 if (STRCMP(p_guifontset, "*") == 0)
2392 errmsg = e_cant_select_fontset;
2393 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
2394 errmsg = e_invalid_fontset;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002395
2396 return errmsg;
2397}
2398# endif
2399
2400/*
2401 * The 'guifontwide' option is changed.
2402 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002403 char *
2404did_set_guifontwide(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002405{
2406 char *errmsg = NULL;
2407
2408 if (STRCMP(p_guifontwide, "*") == 0)
2409 errmsg = e_cant_select_wide_font;
2410 else if (gui_get_wide_font() == FAIL)
2411 errmsg = e_invalid_wide_font;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002412
2413 return errmsg;
2414}
2415#endif
2416
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002417#if defined(FEAT_GUI_GTK) || defined(PROTO)
2418/*
2419 * The 'guiligatures' option is changed.
2420 */
2421 char *
2422did_set_guiligatures(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002423{
2424 gui_set_ligatures();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002425 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002426}
2427#endif
2428
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002429#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002430/*
2431 * The 'guioptions' option is changed.
2432 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002433 char *
2434did_set_guioptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002435{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002436 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002437 char *errmsg;
2438
Christian Brabandtb39b2402023-11-29 11:34:05 +01002439 errmsg = did_set_option_listflag(*varp, (char_u *)GO_ALL, args->os_errbuf,
2440 args->os_errbuflen);
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002441 if (errmsg != NULL)
2442 return errmsg;
2443
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002444 gui_init_which_components(args->os_oldval.string);
2445 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002446}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002447
2448 int
2449expand_set_guioptions(optexpand_T *args, int *numMatches, char_u ***matches)
2450{
2451 return expand_set_opt_listflag(args, (char_u*)GO_ALL, numMatches, matches);
2452}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002453#endif
2454
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002455#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002456/*
2457 * The 'guitablabel' option is changed.
2458 */
2459 char *
2460did_set_guitablabel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002461{
2462 redraw_tabline = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002463 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002464}
2465#endif
2466
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002467/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002468 * The 'helpfile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002469 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002470 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002471did_set_helpfile(optset_T *args UNUSED)
2472{
2473 // May compute new values for $VIM and $VIMRUNTIME
2474 if (didset_vim)
2475 vim_unsetenv_ext((char_u *)"VIM");
2476 if (didset_vimruntime)
2477 vim_unsetenv_ext((char_u *)"VIMRUNTIME");
2478 return NULL;
2479}
2480
2481#if defined(FEAT_MULTI_LANG) || defined(PROTO)
2482/*
2483 * The 'helplang' option is changed.
2484 */
2485 char *
2486did_set_helplang(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002487{
2488 char *errmsg = NULL;
2489
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002490 // Check for "", "ab", "ab,cd", etc.
2491 for (char_u *s = p_hlg; *s != NUL; s += 3)
2492 {
2493 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
2494 {
2495 errmsg = e_invalid_argument;
2496 break;
2497 }
2498 if (s[2] == NUL)
2499 break;
2500 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002501
2502 return errmsg;
2503}
2504#endif
2505
2506/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002507 * The 'highlight' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002508 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002509 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002510did_set_highlight(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002511{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002512 if (highlight_changed() == FAIL)
2513 return e_invalid_argument; // invalid flags
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002514
2515 return NULL;
2516}
2517
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002518/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002519 * Expand 'highlight' option.
2520 */
2521 int
2522expand_set_highlight(optexpand_T *args, int *numMatches, char_u ***matches)
2523{
2524 char_u *p;
2525 expand_T *xp = args->oe_xp;
2526 static char_u hl_flags[HLF_COUNT] = HL_FLAGS;
Christian Brabandt3f168ec2023-10-02 23:21:11 +02002527 size_t i;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002528 int count = 0;
2529
2530 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2531 {
2532 // Right after a ':', meaning we just return all highlight names.
2533 return expand_set_opt_generic(
2534 args,
2535 get_highlight_name,
2536 numMatches,
2537 matches);
2538 }
2539
2540 if (*xp->xp_pattern == NUL)
2541 {
2542 // At beginning of a comma-separated list. Return the specific list of
2543 // supported occasions.
2544 *matches = ALLOC_MULT(char_u *, HLF_COUNT + 1);
2545 if (*matches == NULL)
2546 return FAIL;
2547
2548 // We still want to return the full option if it's requested.
2549 if (args->oe_include_orig_val)
2550 {
2551 p = vim_strsave(args->oe_opt_value);
2552 if (p == NULL)
2553 {
2554 VIM_CLEAR(*matches);
2555 return FAIL;
2556 }
2557 (*matches)[count++] = p;
2558 }
2559
2560 for (i = 0; i < HLF_COUNT; i++)
2561 {
2562 p = vim_strnsave(&hl_flags[i], 1);
2563 if (p == NULL)
2564 {
2565 if (count == 0)
2566 {
2567 VIM_CLEAR(*matches);
2568 return FAIL;
2569 }
2570 else
2571 break;
2572 }
2573 (*matches)[count++] = p;
2574 }
2575
2576 if (count == 0)
2577 {
2578 VIM_CLEAR(*matches);
2579 return FAIL;
2580 }
2581 *numMatches = count;
2582 return OK;
2583 }
2584
2585 // We are after the initial character (which indicates the occasion). We
2586 // already made sure we are not matching after a ':' above, so now we want
2587 // to match against display mode modifiers.
2588 // Since the xp_pattern starts from the beginning, we need to include it in
2589 // the returned match.
2590
2591 // Note: Keep this in sync with highlight_changed()
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002592 static char_u p_hl_mode_values[] =
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002593 {':', 'b', 'i', '-', 'n', 'r', 's', 'u', 'c', '2', 'd', '=', 't'};
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002594 size_t num_hl_modes = ARRAY_LENGTH(p_hl_mode_values);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002595
2596 *matches = ALLOC_MULT(char_u *, num_hl_modes);
2597 if (*matches == NULL)
2598 return FAIL;
2599
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002600 int pattern_len = xp->xp_pattern_len;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002601
2602 for (i = 0; i < num_hl_modes; i++)
2603 {
2604 // Don't allow duplicates as these are unique flags
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002605 char_u *dup = vim_strchr(xp->xp_pattern + 1, p_hl_mode_values[i]);
2606 if (dup != NULL && (int)(dup - xp->xp_pattern) < pattern_len)
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002607 continue;
2608
2609 // ':' only works by itself, not with other flags.
2610 if (pattern_len > 1 && p_hl_mode_values[i] == ':')
2611 continue;
2612
2613 p = vim_strnsave(xp->xp_pattern, pattern_len + 1);
2614 if (p == NULL)
2615 {
2616 if (i == 0)
2617 {
2618 VIM_CLEAR(*matches);
2619 return FAIL;
2620 }
2621 else
2622 break;
2623 }
2624 p[pattern_len] = p_hl_mode_values[i];
2625 p[pattern_len + 1] = NUL;
2626 (*matches)[count++] = p;
2627 }
2628 if (count == 0)
2629 {
2630 VIM_CLEAR(*matches);
2631 return FAIL;
2632 }
2633 *numMatches = count;
2634
2635 return OK;
2636}
2637
2638/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002639 * The 'titlestring' or the 'iconstring' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002640 */
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002641 static char *
2642parse_titleiconstring(optset_T *args UNUSED, int flagval UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002643{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002644#ifdef FEAT_STL_OPT
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002645 char_u **varp = (char_u **)args->os_varp;
2646
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002647 // NULL => statusline syntax
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002648 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002649 stl_syntax |= flagval;
2650 else
2651 stl_syntax &= ~flagval;
2652#endif
2653 did_set_title();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002654
2655 return NULL;
2656}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002657
2658/*
2659 * The 'iconstring' option is changed.
2660 */
2661 char *
2662did_set_iconstring(optset_T *args)
2663{
2664 int flagval = 0;
2665
2666#ifdef FEAT_STL_OPT
2667 flagval = STL_IN_ICON;
2668#endif
2669
2670 return parse_titleiconstring(args, flagval);
2671}
2672
2673#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
2674/*
2675 * The 'imactivatekey' option is changed.
2676 */
2677 char *
2678did_set_imactivatekey(optset_T *args UNUSED)
2679{
2680 if (!im_xim_isvalid_imactivate())
2681 return e_invalid_argument;
2682 return NULL;
2683}
2684#endif
2685
2686/*
2687 * The 'isident' or the 'iskeyword' or the 'isprint' or the 'isfname' option is
2688 * changed.
2689 */
2690 char *
2691did_set_isopt(optset_T *args)
2692{
2693 // 'isident', 'iskeyword', 'isprint or 'isfname' option: refill g_chartab[]
2694 // If the new option is invalid, use old value.
2695 // 'lisp' option: refill g_chartab[] for '-' char.
2696 if (init_chartab() == FAIL)
2697 {
2698 args->os_restore_chartab = TRUE;// need to restore the chartab.
2699 return e_invalid_argument; // error in value
2700 }
2701
2702 return NULL;
2703}
2704
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002705/*
2706 * The 'jumpoptions' option is changed.
2707 */
2708 char *
Yegappan Lakshmanan1926ae42023-09-21 16:36:28 +02002709did_set_jumpoptions(optset_T *args UNUSED)
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002710{
2711 if (opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE) != OK)
2712 return e_invalid_argument;
2713
2714 return NULL;
2715}
2716
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002717 int
2718expand_set_jumpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2719{
2720 return expand_set_opt_string(
2721 args,
2722 p_jop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002723 ARRAY_LENGTH(p_jop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002724 numMatches,
2725 matches);
2726}
2727
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002728#if defined(FEAT_KEYMAP) || defined(PROTO)
2729/*
2730 * The 'keymap' option is changed.
2731 */
2732 char *
2733did_set_keymap(optset_T *args)
2734{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002735 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002736 char *errmsg = NULL;
2737
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002738 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002739 errmsg = e_invalid_argument;
2740 else
2741 {
2742 int secure_save = secure;
2743
2744 // Reset the secure flag, since the value of 'keymap' has
2745 // been checked to be safe.
2746 secure = 0;
2747
2748 // load or unload key mapping tables
2749 errmsg = keymap_init();
2750
2751 secure = secure_save;
2752
2753 // Since we check the value, there is no need to set P_INSECURE,
2754 // even when the value comes from a modeline.
2755 args->os_value_checked = TRUE;
2756 }
2757
2758 if (errmsg == NULL)
2759 {
2760 if (*curbuf->b_p_keymap != NUL)
2761 {
2762 // Installed a new keymap, switch on using it.
2763 curbuf->b_p_iminsert = B_IMODE_LMAP;
2764 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
2765 curbuf->b_p_imsearch = B_IMODE_LMAP;
2766 }
2767 else
2768 {
2769 // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
2770 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
2771 curbuf->b_p_iminsert = B_IMODE_NONE;
2772 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
2773 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
2774 }
2775 if ((args->os_flags & OPT_LOCAL) == 0)
2776 {
2777 set_iminsert_global();
2778 set_imsearch_global();
2779 }
2780 status_redraw_curbuf();
2781 }
2782
2783 return errmsg;
2784}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002785#endif
2786
2787/*
2788 * The 'keymodel' option is changed.
2789 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002790 char *
2791did_set_keymodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002792{
2793 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
2794 return e_invalid_argument;
2795
2796 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
2797 km_startsel = (vim_strchr(p_km, 'a') != NULL);
2798 return NULL;
2799}
2800
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002801 int
2802expand_set_keymodel(optexpand_T *args, int *numMatches, char_u ***matches)
2803{
2804 return expand_set_opt_string(
2805 args,
2806 p_km_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002807 ARRAY_LENGTH(p_km_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002808 numMatches,
2809 matches);
2810}
2811
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002812/*
2813 * The 'keyprotocol' option is changed.
2814 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002815 char *
2816did_set_keyprotocol(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002817{
Gregory Anders3695d0e2023-09-29 20:17:20 +02002818 char_u *term = T_NAME;
2819 keyprot_T kpc = match_keyprotocol(term);
2820 if (kpc == KEYPROTOCOL_FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002821 return e_invalid_argument;
2822
Gregory Anders3695d0e2023-09-29 20:17:20 +02002823 apply_keyprotocol(term, kpc);
2824
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002825 return NULL;
2826}
2827
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002828 int
2829expand_set_keyprotocol(optexpand_T *args, int *numMatches, char_u ***matches)
2830{
2831 expand_T *xp = args->oe_xp;
2832 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2833 {
2834 // 'keyprotocol' only has well-defined terms for completion for the
2835 // protocol part after the colon.
2836 return expand_set_opt_string(
2837 args,
2838 p_kpc_protocol_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002839 ARRAY_LENGTH(p_kpc_protocol_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002840 numMatches,
2841 matches);
2842 }
2843 // Use expand_set_opt_string instead of returning FAIL so that we can
2844 // include the original value if args->oe_include_orig_val is set.
2845 static char *(empty[]) = {NULL};
2846 return expand_set_opt_string(args, empty, 0, numMatches, matches);
2847}
2848
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002849/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002850 * The 'lispoptions' option is changed.
2851 */
2852 char *
2853did_set_lispoptions(optset_T *args)
2854{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002855 char_u **varp = (char_u **)args->os_varp;
2856
2857 if (**varp != NUL
2858 && STRCMP(*varp, "expr:0") != 0
2859 && STRCMP(*varp, "expr:1") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002860 return e_invalid_argument;
2861
2862 return NULL;
2863}
2864
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002865 int
2866expand_set_lispoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2867{
2868 static char *(p_lop_values[]) = {"expr:0", "expr:1", NULL};
2869 return expand_set_opt_string(
2870 args,
2871 p_lop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002872 ARRAY_LENGTH(p_lop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002873 numMatches,
2874 matches);
2875}
2876
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002877/*
2878 * The 'matchpairs' option is changed.
2879 */
2880 char *
2881did_set_matchpairs(optset_T *args)
2882{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002883 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002884 char_u *p;
2885
2886 if (has_mbyte)
2887 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002888 for (p = *varp; *p != NUL; ++p)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002889 {
2890 int x2 = -1;
2891 int x3 = -1;
2892
2893 p += mb_ptr2len(p);
2894 if (*p != NUL)
2895 x2 = *p++;
2896 if (*p != NUL)
2897 {
2898 x3 = mb_ptr2char(p);
2899 p += mb_ptr2len(p);
2900 }
2901 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
2902 return e_invalid_argument;
2903 if (*p == NUL)
2904 break;
2905 }
2906 }
2907 else
2908 {
2909 // Check for "x:y,x:y"
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002910 for (p = *varp; *p != NUL; p += 4)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002911 {
2912 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
2913 return e_invalid_argument;
2914 if (p[3] == NUL)
2915 break;
2916 }
2917 }
2918
2919 return NULL;
2920}
2921
2922#if defined(FEAT_SPELL) || defined(PROTO)
2923/*
2924 * The 'mkspellmem' option is changed.
2925 */
2926 char *
2927did_set_mkspellmem(optset_T *args UNUSED)
2928{
2929 if (spell_check_msm() != OK)
2930 return e_invalid_argument;
2931
2932 return NULL;
2933}
2934#endif
2935
2936/*
2937 * The 'mouse' option is changed.
2938 */
2939 char *
2940did_set_mouse(optset_T *args)
2941{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002942 char_u **varp = (char_u **)args->os_varp;
2943
Christian Brabandtb39b2402023-11-29 11:34:05 +01002944 return did_set_option_listflag(*varp, (char_u *)MOUSE_ALL, args->os_errbuf,
2945 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002946}
2947
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002948 int
2949expand_set_mouse(optexpand_T *args, int *numMatches, char_u ***matches)
2950{
2951 return expand_set_opt_listflag(args, (char_u*)MOUSE_ALL, numMatches, matches);
2952}
2953
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002954/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002955 * The 'mousemodel' option is changed.
2956 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002957 char *
2958did_set_mousemodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002959{
2960 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
2961 return e_invalid_argument;
2962#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
Ken Takata18d0d292023-12-19 20:12:29 +01002963 else if (*p_mousem != *args->os_oldval.string)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002964 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
2965 // to create or delete the popup menus.
2966 gui_motif_update_mousemodel(root_menu);
2967#endif
2968
2969 return NULL;
2970}
2971
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002972 int
2973expand_set_mousemodel(optexpand_T *args, int *numMatches, char_u ***matches)
2974{
2975 return expand_set_opt_string(
2976 args,
2977 p_mousem_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002978 ARRAY_LENGTH(p_mousem_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002979 numMatches,
2980 matches);
2981}
2982
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002983#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
2984 char *
2985did_set_mouseshape(optset_T *args UNUSED)
2986{
2987 char *errmsg = NULL;
2988
2989 errmsg = parse_shape_opt(SHAPE_MOUSE);
2990 update_mouseshape(-1);
2991
2992 return errmsg;
2993}
2994#endif
2995
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002996/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002997 * The 'nrformats' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002998 */
2999 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003000did_set_nrformats(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003001{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003002 char_u **varp = (char_u **)args->os_varp;
3003
3004 return did_set_opt_strings(*varp, p_nf_values, TRUE);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003005}
3006
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003007 int
3008expand_set_nrformats(optexpand_T *args, int *numMatches, char_u ***matches)
3009{
3010 return expand_set_opt_string(
3011 args,
3012 p_nf_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003013 ARRAY_LENGTH(p_nf_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003014 numMatches,
3015 matches);
3016}
3017
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003018#if defined(FEAT_EVAL) || defined(PROTO)
3019/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003020 * One of the '*expr' options is changed: 'balloonexpr', 'diffexpr',
3021 * 'foldexpr', 'foldtext', 'formatexpr', 'includeexpr', 'indentexpr',
3022 * 'patchexpr', 'printexpr' and 'charconvert'.
3023 *
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003024 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003025 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003026did_set_optexpr(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003027{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003028 char_u **varp = (char_u **)args->os_varp;
3029
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003030 // If the option value starts with <SID> or s:, then replace that with
3031 // the script identifier.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003032 char_u *name = get_scriptlocal_funcname(*varp);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003033 if (name != NULL)
3034 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003035 free_string_option(*varp);
3036 *varp = name;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003037 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003038
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003039 return NULL;
3040}
3041#endif
3042
3043/*
3044 * The 'pastetoggle' option is changed.
3045 */
3046 char *
3047did_set_pastetoggle(optset_T *args UNUSED)
3048{
3049 char_u *p;
3050
3051 // translate key codes like in a mapping
3052 if (*p_pt)
3053 {
zeertzjq7e0bae02023-08-11 23:15:38 +02003054 (void)replace_termcodes(p_pt, &p, 0,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003055 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
3056 if (p != NULL)
3057 {
3058 free_string_option(p_pt);
3059 p_pt = p;
3060 }
3061 }
3062
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003063 return NULL;
3064}
3065
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003066#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3067/*
3068 * The 'previewpopup' option is changed.
3069 */
3070 char *
3071did_set_previewpopup(optset_T *args UNUSED)
3072{
3073 if (parse_previewpopup(NULL) == FAIL)
3074 return e_invalid_argument;
3075
3076 return NULL;
3077}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003078
3079 int
3080expand_set_popupoption(optexpand_T *args, int *numMatches, char_u ***matches)
3081{
3082 expand_T *xp = args->oe_xp;
3083
3084 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3085 {
3086 // Within "highlight:"/"border:"/"align:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003087 int border_len = (int)STRLEN("border:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003088 if (xp->xp_pattern - args->oe_set_arg >= border_len &&
3089 STRNCMP(xp->xp_pattern - border_len, "border:", border_len) == 0)
3090 {
3091 return expand_set_opt_string(
3092 args,
3093 p_popup_option_border_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003094 ARRAY_LENGTH(p_popup_option_border_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003095 numMatches,
3096 matches);
3097 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003098 int align_len = (int)STRLEN("align:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003099 if (xp->xp_pattern - args->oe_set_arg >= align_len &&
3100 STRNCMP(xp->xp_pattern - align_len, "align:", align_len) == 0)
3101 {
3102 return expand_set_opt_string(
3103 args,
3104 p_popup_option_align_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003105 ARRAY_LENGTH(p_popup_option_align_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003106 numMatches,
3107 matches);
3108 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003109 int highlight_len = (int)STRLEN("highlight:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003110 if (xp->xp_pattern - args->oe_set_arg >= highlight_len &&
3111 STRNCMP(xp->xp_pattern - highlight_len, "highlight:", highlight_len) == 0)
3112 {
3113 // Return the list of all highlight names
3114 return expand_set_opt_generic(
3115 args,
3116 get_highlight_name,
3117 numMatches,
3118 matches);
3119 }
3120 return FAIL;
3121 }
3122
3123 return expand_set_opt_string(
3124 args,
3125 p_popup_option_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003126 ARRAY_LENGTH(p_popup_option_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003127 numMatches,
3128 matches);
3129}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003130#endif
3131
3132#if defined(FEAT_POSTSCRIPT) || defined(PROTO)
3133/*
3134 * The 'printencoding' option is changed.
3135 */
3136 char *
3137did_set_printencoding(optset_T *args UNUSED)
3138{
3139 char_u *s, *p;
3140
3141 // Canonize 'printencoding' if VIM standard one
3142 p = enc_canonize(p_penc);
3143 if (p != NULL)
3144 {
3145 vim_free(p_penc);
3146 p_penc = p;
3147 }
3148 else
3149 {
3150 // Ensure lower case and '-' for '_'
3151 for (s = p_penc; *s != NUL; s++)
3152 {
3153 if (*s == '_')
3154 *s = '-';
3155 else
3156 *s = TOLOWER_ASC(*s);
3157 }
3158 }
3159
3160 return NULL;
3161}
3162#endif
3163
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003164#if defined(FEAT_PRINTER) || defined(PROTO)
3165
3166 static char_u *
3167get_printoptions_names(expand_T *xp UNUSED, int idx)
3168{
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003169 if (idx >= (int)ARRAY_LENGTH(printer_opts))
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003170 return NULL;
3171 return (char_u*)printer_opts[idx].name;
3172}
3173
3174/*
3175 * Expand 'printoptions' option
3176 */
3177 int
3178expand_set_printoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3179{
3180 return expand_set_opt_generic(
3181 args,
3182 get_printoptions_names,
3183 numMatches,
3184 matches);
3185}
3186#endif
3187
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003188#if defined(FEAT_STL_OPT) || defined(PROTO)
3189/*
3190 * The 'statusline' or the 'tabline' or the 'rulerformat' option is changed.
3191 * "rulerformat" is TRUE if the 'rulerformat' option is changed.
3192 */
3193 static char *
3194parse_statustabline_rulerformat(optset_T *args, int rulerformat)
3195{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003196 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003197 char_u *s;
3198 char *errmsg = NULL;
3199 int wid;
3200
3201 if (rulerformat) // reset ru_wid first
3202 ru_wid = 0;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003203 s = *varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003204 if (rulerformat && *s == '%')
3205 {
3206 // set ru_wid if 'ruf' starts with "%99("
3207 if (*++s == '-') // ignore a '-'
3208 s++;
3209 wid = getdigits(&s);
3210 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
3211 ru_wid = wid;
3212 else
3213 errmsg = check_stl_option(p_ruf);
3214 }
3215 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
3216 else if (rulerformat || s[0] != '%' || s[1] != '!')
3217 errmsg = check_stl_option(s);
3218 if (rulerformat && errmsg == NULL)
3219 comp_col();
3220
3221 return errmsg;
3222}
3223#endif
3224
3225#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
3226/*
3227 * The 'renderoptions' option is changed.
3228 */
3229 char *
3230did_set_renderoptions(optset_T *args UNUSED)
3231{
3232 if (!gui_mch_set_rendering_options(p_rop))
3233 return e_invalid_argument;
3234
3235 return NULL;
3236}
3237#endif
3238
3239#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3240/*
3241 * The 'rightleftcmd' option is changed.
3242 */
3243 char *
3244did_set_rightleftcmd(optset_T *args)
3245{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003246 char_u **varp = (char_u **)args->os_varp;
3247
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003248 // Currently only "search" is a supported value.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003249 if (**varp != NUL && STRCMP(*varp, "search") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003250 return e_invalid_argument;
3251
3252 return NULL;
3253}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003254
3255 int
3256expand_set_rightleftcmd(optexpand_T *args, int *numMatches, char_u ***matches)
3257{
3258 static char *(p_rlc_values[]) = {"search", NULL};
3259 return expand_set_opt_string(
3260 args,
3261 p_rlc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003262 ARRAY_LENGTH(p_rlc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003263 numMatches,
3264 matches);
3265}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003266#endif
3267
3268#if defined(FEAT_STL_OPT) || defined(PROTO)
3269/*
3270 * The 'rulerformat' option is changed.
3271 */
3272 char *
3273did_set_rulerformat(optset_T *args)
3274{
3275 return parse_statustabline_rulerformat(args, TRUE);
3276}
3277#endif
3278
3279/*
3280 * The 'scrollopt' option is changed.
3281 */
3282 char *
3283did_set_scrollopt(optset_T *args UNUSED)
3284{
3285 return did_set_opt_strings(p_sbo, p_scbopt_values, TRUE);
3286}
3287
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003288 int
3289expand_set_scrollopt(optexpand_T *args, int *numMatches, char_u ***matches)
3290{
3291 return expand_set_opt_string(
3292 args,
3293 p_scbopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003294 ARRAY_LENGTH(p_scbopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003295 numMatches,
3296 matches);
3297}
3298
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003299/*
3300 * The 'selection' option is changed.
3301 */
3302 char *
3303did_set_selection(optset_T *args UNUSED)
3304{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003305 if (*p_sel == NUL || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003306 return e_invalid_argument;
3307
3308 return NULL;
3309}
3310
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003311 int
3312expand_set_selection(optexpand_T *args, int *numMatches, char_u ***matches)
3313{
3314 return expand_set_opt_string(
3315 args,
3316 p_sel_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003317 ARRAY_LENGTH(p_sel_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003318 numMatches,
3319 matches);
3320}
3321
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003322/*
3323 * The 'selectmode' option is changed.
3324 */
3325 char *
3326did_set_selectmode(optset_T *args UNUSED)
3327{
3328 return did_set_opt_strings(p_slm, p_slm_values, TRUE);
3329}
3330
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003331 int
3332expand_set_selectmode(optexpand_T *args, int *numMatches, char_u ***matches)
3333{
3334 return expand_set_opt_string(
3335 args,
3336 p_slm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003337 ARRAY_LENGTH(p_slm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003338 numMatches,
3339 matches);
3340}
3341
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003342#if defined(FEAT_SESSION) || defined(PROTO)
3343/*
3344 * The 'sessionoptions' option is changed.
3345 */
3346 char *
3347did_set_sessionoptions(optset_T *args)
3348{
3349 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
3350 return e_invalid_argument;
3351 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
3352 {
3353 // Don't allow both "sesdir" and "curdir".
3354 (void)opt_strings_flags(args->os_oldval.string, p_ssop_values,
3355 &ssop_flags, TRUE);
3356 return e_invalid_argument;
3357 }
3358
3359 return NULL;
3360}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003361
3362 int
3363expand_set_sessionoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3364{
3365 return expand_set_opt_string(
3366 args,
3367 p_ssop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003368 ARRAY_LENGTH(p_ssop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003369 numMatches,
3370 matches);
3371}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003372#endif
3373
3374/*
3375 * The 'shortmess' option is changed.
3376 */
3377 char *
3378did_set_shortmess(optset_T *args)
3379{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003380 char_u **varp = (char_u **)args->os_varp;
3381
Christian Brabandtb39b2402023-11-29 11:34:05 +01003382 return did_set_option_listflag(*varp, (char_u *)SHM_ALL, args->os_errbuf,
3383 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003384}
3385
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003386 int
3387expand_set_shortmess(optexpand_T *args, int *numMatches, char_u ***matches)
3388{
3389 return expand_set_opt_listflag(args, (char_u*)SHM_ALL, numMatches, matches);
3390}
3391
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003392#if defined(FEAT_LINEBREAK) || defined(PROTO)
3393/*
3394 * The 'showbreak' option is changed.
3395 */
3396 char *
3397did_set_showbreak(optset_T *args)
3398{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003399 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003400 char_u *s;
3401
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003402 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003403 {
3404 if (ptr2cells(s) != 1)
3405 return e_showbreak_contains_unprintable_or_wide_character;
3406 MB_PTR_ADV(s);
3407 }
3408
3409 return NULL;
3410}
3411#endif
3412
3413/*
3414 * The 'showcmdloc' option is changed.
3415 */
3416 char *
3417did_set_showcmdloc(optset_T *args UNUSED)
3418{
3419 return did_set_opt_strings(p_sloc, p_sloc_values, FALSE);
3420}
3421
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003422 int
3423expand_set_showcmdloc(optexpand_T *args, int *numMatches, char_u ***matches)
3424{
3425 return expand_set_opt_string(
3426 args,
3427 p_sloc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003428 ARRAY_LENGTH(p_sloc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003429 numMatches,
3430 matches);
3431}
3432
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003433#if defined(FEAT_SIGNS) || defined(PROTO)
3434/*
3435 * The 'signcolumn' option is changed.
3436 */
3437 char *
3438did_set_signcolumn(optset_T *args)
3439{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003440 char_u **varp = (char_u **)args->os_varp;
3441
3442 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003443 return e_invalid_argument;
3444 // When changing the 'signcolumn' to or from 'number', recompute the
3445 // width of the number column if 'number' or 'relativenumber' is set.
3446 if (((*args->os_oldval.string == 'n' && args->os_oldval.string[1] == 'u')
3447 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
3448 && (curwin->w_p_nu || curwin->w_p_rnu))
3449 curwin->w_nrwidth_line_count = 0;
3450
3451 return NULL;
3452}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003453
3454 int
3455expand_set_signcolumn(optexpand_T *args, int *numMatches, char_u ***matches)
3456{
3457 return expand_set_opt_string(
3458 args,
3459 p_scl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003460 ARRAY_LENGTH(p_scl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003461 numMatches,
3462 matches);
3463}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003464#endif
3465
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003466#if defined(FEAT_SPELL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003467/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003468 * The 'spellcapcheck' option is changed.
3469 */
3470 char *
3471did_set_spellcapcheck(optset_T *args UNUSED)
3472{
3473 // compile the regexp program.
3474 return compile_cap_prog(curwin->w_s);
3475}
3476
3477/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003478 * The 'spellfile' option is changed.
3479 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003480 char *
3481did_set_spellfile(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003482{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003483 char_u **varp = (char_u **)args->os_varp;
3484
3485 if (!valid_spellfile(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003486 return e_invalid_argument;
3487
3488 // If there is a window for this buffer in which 'spell' is set load the
3489 // wordlists.
3490 return did_set_spell_option(TRUE);
3491}
3492
3493/*
3494 * The 'spell' option is changed.
3495 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003496 char *
3497did_set_spelllang(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003498{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003499 char_u **varp = (char_u **)args->os_varp;
3500
3501 if (!valid_spelllang(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003502 return e_invalid_argument;
3503
3504 // If there is a window for this buffer in which 'spell' is set load the
3505 // wordlists.
3506 return did_set_spell_option(FALSE);
3507}
3508
3509/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003510 * The 'spelloptions' option is changed.
3511 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003512 char *
3513did_set_spelloptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003514{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003515 char_u **varp = (char_u **)args->os_varp;
3516
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003517 if (**varp != NUL && STRCMP(*varp, "camel") != 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003518 return e_invalid_argument;
3519
3520 return NULL;
3521}
3522
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003523 int
3524expand_set_spelloptions(optexpand_T *args, int *numMatches, char_u ***matches)
3525{
3526 static char *(p_spo_values[]) = {"camel", NULL};
3527 return expand_set_opt_string(
3528 args,
3529 p_spo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003530 ARRAY_LENGTH(p_spo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003531 numMatches,
3532 matches);
3533}
3534
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003535/*
3536 * The 'spellsuggest' option is changed.
3537 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003538 char *
3539did_set_spellsuggest(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003540{
3541 if (spell_check_sps() != OK)
3542 return e_invalid_argument;
3543
3544 return NULL;
3545}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003546
3547 int
3548expand_set_spellsuggest(optexpand_T *args, int *numMatches, char_u ***matches)
3549{
3550 return expand_set_opt_string(
3551 args,
3552 p_sps_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003553 ARRAY_LENGTH(p_sps_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003554 numMatches,
3555 matches);
3556}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003557#endif
3558
3559/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003560 * The 'splitkeep' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003561 */
3562 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003563did_set_splitkeep(optset_T *args UNUSED)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003564{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003565 return did_set_opt_strings(p_spk, p_spk_values, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003566}
3567
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003568 int
3569expand_set_splitkeep(optexpand_T *args, int *numMatches, char_u ***matches)
3570{
3571 return expand_set_opt_string(
3572 args,
3573 p_spk_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003574 ARRAY_LENGTH(p_spk_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003575 numMatches,
3576 matches);
3577}
3578
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00003579#if defined(FEAT_STL_OPT) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003580/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003581 * The 'statusline' option is changed.
3582 */
3583 char *
3584did_set_statusline(optset_T *args)
3585{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003586 return parse_statustabline_rulerformat(args, FALSE);
3587}
3588#endif
3589
3590/*
3591 * The 'swapsync' option is changed.
3592 */
3593 char *
3594did_set_swapsync(optset_T *args UNUSED)
3595{
3596 return did_set_opt_strings(p_sws, p_sws_values, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003597}
3598
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003599 int
3600expand_set_swapsync(optexpand_T *args, int *numMatches, char_u ***matches)
3601{
3602 return expand_set_opt_string(
3603 args,
3604 p_sws_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003605 ARRAY_LENGTH(p_sws_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003606 numMatches,
3607 matches);
3608}
3609
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003610/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003611 * The 'switchbuf' option is changed.
3612 */
3613 char *
3614did_set_switchbuf(optset_T *args UNUSED)
3615{
3616 return did_set_opt_flags(p_swb, p_swb_values, &swb_flags, TRUE);
3617}
3618
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003619 int
3620expand_set_switchbuf(optexpand_T *args, int *numMatches, char_u ***matches)
3621{
3622 return expand_set_opt_string(
3623 args,
3624 p_swb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003625 ARRAY_LENGTH(p_swb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003626 numMatches,
3627 matches);
3628}
3629
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003630#if defined(FEAT_STL_OPT) || defined(PROTO)
3631/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003632 * The 'tabline' option is changed.
3633 */
3634 char *
3635did_set_tabline(optset_T *args)
3636{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003637 return parse_statustabline_rulerformat(args, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003638}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003639#endif
3640
3641/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003642 * The 'tagcase' option is changed.
3643 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003644 char *
3645did_set_tagcase(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003646{
3647 unsigned int *flags;
3648 char_u *p;
3649
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003650 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003651 {
3652 p = curbuf->b_p_tc;
3653 flags = &curbuf->b_tc_flags;
3654 }
3655 else
3656 {
3657 p = p_tc;
3658 flags = &tc_flags;
3659 }
3660
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003661 if ((args->os_flags & OPT_LOCAL) && *p == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003662 // make the local value empty: use the global value
3663 *flags = 0;
3664 else if (*p == NUL
3665 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
3666 return e_invalid_argument;
3667
3668 return NULL;
3669}
3670
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003671 int
3672expand_set_tagcase(optexpand_T *args, int *numMatches, char_u ***matches)
3673{
3674 return expand_set_opt_string(
3675 args,
3676 p_tc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003677 ARRAY_LENGTH(p_tc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003678 numMatches,
3679 matches);
3680}
3681
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003682/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003683 * The 'term' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003684 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003685 char *
3686did_set_term(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003687{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003688 if (T_NAME[0] == NUL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003689 return e_cannot_set_term_to_empty_string;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003690#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003691 if (gui.in_use)
3692 return e_cannot_change_term_in_GUI;
3693 if (term_is_gui(T_NAME))
3694 return e_use_gui_to_start_GUI;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003695#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003696 if (set_termname(T_NAME) == FAIL)
3697 return e_not_found_in_termcap;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003698
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003699 // Screen colors may have changed.
3700 redraw_later_clear();
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003701
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003702 return NULL;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003703}
3704
3705/*
3706 * Some terminal option (t_xxx) is changed
3707 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003708 char *
3709did_set_term_option(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003710{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003711 char_u **varp = (char_u **)args->os_varp;
3712
3713 if (!full_screen)
3714 return NULL;
3715
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003716 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
3717 if (varp == &T_CCO)
3718 {
3719 int colors = atoi((char *)T_CCO);
3720
3721 // Only reinitialize colors if t_Co value has really changed to
3722 // avoid expensive reload of colorscheme if t_Co is set to the
3723 // same value multiple times.
3724 if (colors != t_colors)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003725 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003726 t_colors = colors;
3727 if (t_colors <= 1)
3728 {
3729 vim_free(T_CCO);
3730 T_CCO = empty_option;
3731 }
3732#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3733 if (is_term_win32())
3734 {
3735 swap_tcap();
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003736 args->os_did_swaptcap = TRUE;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003737 }
3738#endif
3739 // We now have a different color setup, initialize it again.
3740 init_highlight(TRUE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003741 }
3742 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003743 ttest(FALSE);
3744 if (varp == &T_ME)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003745 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003746 out_str(T_ME);
3747 redraw_later(UPD_CLEAR);
3748#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3749 // Since t_me has been set, this probably means that the user
3750 // wants to use this as default colors. Need to reset default
3751 // background/foreground colors.
3752# ifdef VIMDLL
3753 if (!gui.in_use && !gui.starting)
3754# endif
3755 mch_set_normal_colors();
3756#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003757 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003758 if (varp == &T_BE && termcap_active)
3759 {
3760 MAY_WANT_TO_LOG_THIS;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003761
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003762 if (*T_BE == NUL)
3763 // When clearing t_BE we assume the user no longer wants
3764 // bracketed paste, thus disable it by writing t_BD.
3765 out_str(T_BD);
3766 else
3767 out_str(T_BE);
3768 }
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003769
3770 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003771}
3772
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003773#if defined(FEAT_TERMINAL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003774/*
3775 * The 'termwinkey' option is changed.
3776 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003777 char *
3778did_set_termwinkey(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003779{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003780 if (*curwin->w_p_twk != NUL && string_to_key(curwin->w_p_twk, TRUE) == 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003781 return e_invalid_argument;
3782
3783 return NULL;
3784}
3785
3786/*
3787 * The 'termwinsize' option is changed.
3788 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003789 char *
3790did_set_termwinsize(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003791{
3792 char_u *p;
3793
3794 if (*curwin->w_p_tws == NUL)
3795 return NULL;
3796
3797 p = skipdigits(curwin->w_p_tws);
3798 if (p == curwin->w_p_tws
3799 || (*p != 'x' && *p != '*')
3800 || *skipdigits(p + 1) != NUL)
3801 return e_invalid_argument;
3802
3803 return NULL;
3804}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003805
3806# if defined(MSWIN) || defined(PROTO)
3807/*
3808 * The 'termwintype' option is changed.
3809 */
3810 char *
3811did_set_termwintype(optset_T *args UNUSED)
3812{
3813 return did_set_opt_strings(p_twt, p_twt_values, FALSE);
3814}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003815
3816 int
3817expand_set_termwintype(optexpand_T *args, int *numMatches, char_u ***matches)
3818{
3819 return expand_set_opt_string(
3820 args,
3821 p_twt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003822 ARRAY_LENGTH(p_twt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003823 numMatches,
3824 matches);
3825}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003826# endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003827#endif
3828
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003829/*
3830 * The 'titlestring' option is changed.
3831 */
3832 char *
3833did_set_titlestring(optset_T *args)
3834{
3835 int flagval = 0;
3836
3837#ifdef FEAT_STL_OPT
3838 flagval = STL_IN_TITLE;
3839#endif
3840 return parse_titleiconstring(args, flagval);
3841}
3842
3843#if (defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)) || defined(PROTO)
3844/*
3845 * The 'toolbar' option is changed.
3846 */
3847 char *
3848did_set_toolbar(optset_T *args UNUSED)
3849{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003850 if (opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags,
3851 TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003852 return e_invalid_argument;
3853
3854 out_flush();
3855 gui_mch_show_toolbar((toolbar_flags &
3856 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
3857 return NULL;
3858}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003859
3860 int
3861expand_set_toolbar(optexpand_T *args, int *numMatches, char_u ***matches)
3862{
3863 return expand_set_opt_string(
3864 args,
3865 p_toolbar_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003866 ARRAY_LENGTH(p_toolbar_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003867 numMatches,
3868 matches);
3869}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003870#endif
3871
3872#if (defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)) || defined(PROTO)
3873/*
3874 * The 'toolbariconsize' option is changed. GTK+ 2 only.
3875 */
3876 char *
3877did_set_toolbariconsize(optset_T *args UNUSED)
3878{
3879 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
3880 return e_invalid_argument;
3881
3882 out_flush();
3883 gui_mch_show_toolbar((toolbar_flags &
3884 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
3885 return NULL;
3886}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003887
3888 int
3889expand_set_toolbariconsize(optexpand_T *args, int *numMatches, char_u ***matches)
3890{
3891 return expand_set_opt_string(
3892 args,
3893 p_tbis_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003894 ARRAY_LENGTH(p_tbis_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003895 numMatches,
3896 matches);
3897}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003898#endif
3899
3900#if defined(UNIX) || defined(VMS) || defined(PROTO)
3901/*
3902 * The 'ttymouse' option is changed.
3903 */
3904 char *
3905did_set_ttymouse(optset_T *args UNUSED)
3906{
3907 char *errmsg = NULL;
3908
3909 // Switch the mouse off before changing the escape sequences used for
3910 // that.
3911 mch_setmouse(FALSE);
3912 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
3913 errmsg = e_invalid_argument;
3914 else
3915 check_mouse_termcode();
3916 if (termcap_active)
3917 setmouse(); // may switch it on again
3918
3919 return errmsg;
3920}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003921
3922 int
3923expand_set_ttymouse(optexpand_T *args, int *numMatches, char_u ***matches)
3924{
3925 return expand_set_opt_string(
3926 args,
3927 p_ttym_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003928 ARRAY_LENGTH(p_ttym_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003929 numMatches,
3930 matches);
3931}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003932#endif
3933
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003934#if defined(FEAT_VARTABS) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003935/*
3936 * The 'varsofttabstop' option is changed.
3937 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003938 char *
3939did_set_varsofttabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003940{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003941 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003942 char_u *cp;
3943
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003944 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00003945 VIM_CLEAR(curbuf->b_p_vsts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003946 else
3947 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003948 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003949 {
3950 if (vim_isdigit(*cp))
3951 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003952 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003953 continue;
3954 return e_invalid_argument;
3955 }
3956
3957 int *oldarray = curbuf->b_p_vsts_array;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003958 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003959 {
3960 if (oldarray)
3961 vim_free(oldarray);
3962 }
3963 else
3964 return e_invalid_argument;
3965 }
3966
3967 return NULL;
3968}
3969
3970/*
3971 * The 'vartabstop' option is changed.
3972 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003973 char *
3974did_set_vartabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003975{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003976 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003977 char_u *cp;
3978
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003979 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00003980 VIM_CLEAR(curbuf->b_p_vts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003981 else
3982 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003983 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003984 {
3985 if (vim_isdigit(*cp))
3986 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003987 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003988 continue;
3989 return e_invalid_argument;
3990 }
3991
3992 int *oldarray = curbuf->b_p_vts_array;
3993
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003994 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003995 {
3996 vim_free(oldarray);
3997# ifdef FEAT_FOLDING
3998 if (foldmethodIsIndent(curwin))
3999 foldUpdateAll(curwin);
4000# endif
4001 }
4002 else
4003 return e_invalid_argument;
4004 }
4005
4006 return NULL;
4007}
4008#endif
4009
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004010/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004011 * The 'verbosefile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004012 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004013 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004014did_set_verbosefile(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004015{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004016 verbose_stop();
4017 if (*p_vfile != NUL && verbose_open() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004018 return e_invalid_argument;
4019
4020 return NULL;
4021}
4022
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004023#if defined(FEAT_SESSION) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004024/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004025 * The 'viewoptions' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004026 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004027 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004028did_set_viewoptions(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004029{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004030 return did_set_opt_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004031}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004032#endif
4033
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004034#if defined(FEAT_VIMINFO) || defined(PROTO)
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004035/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004036 * The 'viminfo' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004037 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004038 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004039did_set_viminfo(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004040{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004041 char_u *s;
4042 char *errmsg = NULL;
4043
4044 for (s = p_viminfo; *s;)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004045 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004046 // Check it's a valid character
4047 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
4048 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01004049 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004050 break;
4051 }
4052 if (*s == 'n') // name is always last one
4053 break;
4054 else if (*s == 'r') // skip until next ','
4055 {
4056 while (*++s && *s != ',')
4057 ;
4058 }
4059 else if (*s == '%')
4060 {
4061 // optional number
4062 while (vim_isdigit(*++s))
4063 ;
4064 }
4065 else if (*s == '!' || *s == 'h' || *s == 'c')
4066 ++s; // no extra chars
4067 else // must have a number
4068 {
4069 while (vim_isdigit(*++s))
4070 ;
4071
4072 if (!VIM_ISDIGIT(*(s - 1)))
4073 {
4074 if (args->os_errbuf != NULL)
4075 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01004076 vim_snprintf(args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004077 _(e_missing_number_after_angle_str_angle),
4078 transchar_byte(*(s - 1)));
4079 errmsg = args->os_errbuf;
4080 }
4081 else
4082 errmsg = "";
4083 break;
4084 }
4085 }
4086 if (*s == ',')
4087 ++s;
4088 else if (*s)
4089 {
4090 if (args->os_errbuf != NULL)
4091 errmsg = e_missing_comma;
4092 else
4093 errmsg = "";
4094 break;
4095 }
4096 }
4097 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
4098 errmsg = e_must_specify_a_value;
4099
4100 return errmsg;
4101}
4102#endif
4103
4104/*
4105 * The 'virtualedit' option is changed.
4106 */
4107 char *
4108did_set_virtualedit(optset_T *args)
4109{
4110 char_u *ve = p_ve;
4111 unsigned int *flags = &ve_flags;
4112
4113 if (args->os_flags & OPT_LOCAL)
4114 {
4115 ve = curwin->w_p_ve;
4116 flags = &curwin->w_ve_flags;
4117 }
4118
4119 if ((args->os_flags & OPT_LOCAL) && *ve == NUL)
4120 // make the local value empty: use the global value
4121 *flags = 0;
4122 else
4123 {
4124 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
4125 return e_invalid_argument;
4126 else if (STRCMP(ve, args->os_oldval.string) != 0)
4127 {
4128 // Recompute cursor position in case the new 've' setting
4129 // changes something.
4130 validate_virtcol();
4131 coladvance(curwin->w_virtcol);
4132 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004133 }
4134
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004135 return NULL;
4136}
4137
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004138 int
4139expand_set_virtualedit(optexpand_T *args, int *numMatches, char_u ***matches)
4140{
4141 return expand_set_opt_string(
4142 args,
4143 p_ve_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004144 ARRAY_LENGTH(p_ve_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004145 numMatches,
4146 matches);
4147}
4148
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004149/*
4150 * The 'whichwrap' option is changed.
4151 */
4152 char *
4153did_set_whichwrap(optset_T *args)
4154{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004155 char_u **varp = (char_u **)args->os_varp;
4156
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004157 // Add ',' to the list flags because 'whichwrap' is a flag
4158 // list that is comma-separated.
Christian Brabandtb39b2402023-11-29 11:34:05 +01004159 return did_set_option_listflag(*varp, (char_u *)(WW_ALL ","),
4160 args->os_errbuf, args->os_errbuflen);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004161}
4162
4163 int
4164expand_set_whichwrap(optexpand_T *args, int *numMatches, char_u ***matches)
4165{
4166 return expand_set_opt_listflag(args, (char_u*)WW_ALL, numMatches, matches);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004167}
4168
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004169/*
4170 * The 'wildmode' option is changed.
4171 */
4172 char *
4173did_set_wildmode(optset_T *args UNUSED)
4174{
4175 if (check_opt_wim() == FAIL)
4176 return e_invalid_argument;
4177 return NULL;
4178}
4179
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004180 int
4181expand_set_wildmode(optexpand_T *args, int *numMatches, char_u ***matches)
4182{
4183 return expand_set_opt_string(
4184 args,
4185 p_wim_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004186 ARRAY_LENGTH(p_wim_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004187 numMatches,
4188 matches);
4189}
4190
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004191/*
4192 * The 'wildoptions' option is changed.
4193 */
4194 char *
4195did_set_wildoptions(optset_T *args UNUSED)
4196{
4197 return did_set_opt_strings(p_wop, p_wop_values, TRUE);
4198}
4199
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004200 int
4201expand_set_wildoptions(optexpand_T *args, int *numMatches, char_u ***matches)
4202{
4203 return expand_set_opt_string(
4204 args,
4205 p_wop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004206 ARRAY_LENGTH(p_wop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004207 numMatches,
4208 matches);
4209}
4210
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004211#if defined(FEAT_WAK) || defined(PROTO)
4212/*
4213 * The 'winaltkeys' option is changed.
4214 */
4215 char *
4216did_set_winaltkeys(optset_T *args UNUSED)
4217{
4218 char *errmsg = NULL;
4219
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004220 if (*p_wak == NUL || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004221 errmsg = e_invalid_argument;
4222# ifdef FEAT_MENU
4223# if defined(FEAT_GUI_MOTIF)
4224 else if (gui.in_use)
4225 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4226# elif defined(FEAT_GUI_GTK)
4227 else if (gui.in_use)
4228 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4229# endif
4230# endif
4231 return errmsg;
4232}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004233
4234 int
4235expand_set_winaltkeys(optexpand_T *args, int *numMatches, char_u ***matches)
4236{
4237 return expand_set_opt_string(
4238 args,
4239 p_wak_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004240 ARRAY_LENGTH(p_wak_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004241 numMatches,
4242 matches);
4243}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004244#endif
4245
4246/*
4247 * The 'wincolor' option is changed.
4248 */
4249 char *
4250did_set_wincolor(optset_T *args UNUSED)
4251{
4252#ifdef FEAT_TERMINAL
4253 term_update_wincolor(curwin);
4254#endif
4255 return NULL;
4256}
4257
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004258 int
4259expand_set_wincolor(optexpand_T *args, int *numMatches, char_u ***matches)
4260{
4261 return expand_set_opt_generic(
4262 args,
4263 get_highlight_name,
4264 numMatches,
4265 matches);
4266}
4267
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004268#ifdef FEAT_SYN_HL
4269/*
4270 * When the 'syntax' option is set, load the syntax of that name.
4271 */
4272 static void
4273do_syntax_autocmd(int value_changed)
4274{
4275 static int syn_recursive = 0;
4276
4277 ++syn_recursive;
4278 // Only pass TRUE for "force" when the value changed or not used
4279 // recursively, to avoid endless recurrence.
4280 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
4281 value_changed || syn_recursive == 1, curbuf);
4282 curbuf->b_flags |= BF_SYN_SET;
4283 --syn_recursive;
4284}
4285#endif
4286
4287/*
4288 * When the 'filetype' option is set, trigger the FileType autocommand.
4289 */
4290 static void
4291do_filetype_autocmd(char_u **varp, int opt_flags, int value_changed)
4292{
4293 // Skip this when called from a modeline and the filetype was already set
4294 // to this value.
4295 if ((opt_flags & OPT_MODELINE) && !value_changed)
4296 return;
4297
4298 static int ft_recursive = 0;
4299 int secure_save = secure;
4300
4301 // Reset the secure flag, since the value of 'filetype' has
4302 // been checked to be safe.
4303 secure = 0;
4304
4305 ++ft_recursive;
4306 did_filetype = TRUE;
4307 // Only pass TRUE for "force" when the value changed or not
4308 // used recursively, to avoid endless recurrence.
4309 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
4310 value_changed || ft_recursive == 1, curbuf);
4311 --ft_recursive;
4312 // Just in case the old "curbuf" is now invalid.
4313 if (varp != &(curbuf->b_p_ft))
4314 varp = NULL;
4315
4316 secure = secure_save;
4317}
4318
4319#ifdef FEAT_SPELL
4320/*
4321 * When the 'spelllang' option is set, source the spell/LANG.vim file in
4322 * 'runtimepath'.
4323 */
4324 static void
4325do_spelllang_source(void)
4326{
4327 char_u fname[200];
4328 char_u *p;
4329 char_u *q = curwin->w_s->b_p_spl;
4330
4331 // Skip the first name if it is "cjk".
4332 if (STRNCMP(q, "cjk,", 4) == 0)
4333 q += 4;
4334
4335 // They could set 'spellcapcheck' depending on the language. Use the first
4336 // name in 'spelllang' up to '_region' or '.encoding'.
4337 for (p = q; *p != NUL; ++p)
4338 if (!ASCII_ISALNUM(*p) && *p != '-')
4339 break;
4340 if (p > q)
4341 {
4342 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
4343 (int)(p - q), q);
4344 source_runtime(fname, DIP_ALL);
4345 }
4346}
4347#endif
4348
4349/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004350 * Handle string options that need some action to perform when changed.
zeertzjqf6782732022-07-27 18:26:03 +01004351 * The new value must be allocated.
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004352 * Returns NULL for success, or an untranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +02004353 */
4354 char *
4355did_set_string_option(
4356 int opt_idx, // index in options[] table
4357 char_u **varp, // pointer to the option variable
Bram Moolenaardac13472019-09-16 21:06:21 +02004358 char_u *oldval, // previous value of the option
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004359 char_u *value, // new value of the option
Bram Moolenaardac13472019-09-16 21:06:21 +02004360 char *errbuf, // buffer for errors, or NULL
Mike Williams620f0112023-12-05 15:36:06 +01004361 size_t errbuflen, // length of error buffer
Bram Moolenaardac13472019-09-16 21:06:21 +02004362 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004363 set_op_T op, // OP_ADDING/OP_PREPENDING/OP_REMOVING
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004364 int *value_checked) // value was checked to be safe, no
Bram Moolenaardac13472019-09-16 21:06:21 +02004365 // need to set P_INSECURE
4366{
4367 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +02004368 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004369 opt_did_set_cb_T did_set_cb = get_option_did_set_cb(opt_idx);
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004370 optset_T args;
4371
4372 // 'ttytype' is an alias for 'term'. Both 'term' and 'ttytype' point to
4373 // T_NAME. If 'term' or 'ttytype' is modified, then use the index for the
4374 // 'term' option. Only set the P_ALLOCED flag on 'term'.
4375 if (varp == &T_NAME)
4376 {
4377 opt_idx = findoption((char_u *)"term");
4378 if (opt_idx >= 0)
4379 {
4380 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
4381 did_set_cb = get_option_did_set_cb(opt_idx);
4382 }
4383 }
4384
4385 CLEAR_FIELD(args);
Bram Moolenaardac13472019-09-16 21:06:21 +02004386
Bram Moolenaardac13472019-09-16 21:06:21 +02004387 // Disallow changing some options from secure mode
4388 if ((secure
4389#ifdef HAVE_SANDBOX
4390 || sandbox != 0
4391#endif
4392 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00004393 errmsg = e_not_allowed_here;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004394 // Check for a "normal" directory or file name in some options.
4395 else if (check_illegal_path_names(opt_idx, varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004396 errmsg = e_invalid_argument;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004397 else if (did_set_cb != NULL)
4398 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004399 args.os_varp = (char_u *)varp;
4400 args.os_idx = opt_idx;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004401 args.os_flags = opt_flags;
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004402 args.os_op = op;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004403 args.os_oldval.string = oldval;
4404 args.os_newval.string = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004405 args.os_errbuf = errbuf;
Christian Brabandtb39b2402023-11-29 11:34:05 +01004406 args.os_errbuflen = errbuflen;
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004407 // Invoke the option specific callback function to validate and apply
4408 // the new option value.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004409 errmsg = did_set_cb(&args);
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004410
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004411 // The 'keymap', 'filetype' and 'syntax' option callback functions
4412 // may change the os_value_checked field.
4413 *value_checked = args.os_value_checked;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004414 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004415
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004416 // If an error is detected, restore the previous value.
Bram Moolenaardac13472019-09-16 21:06:21 +02004417 if (errmsg != NULL)
4418 {
zeertzjqf6782732022-07-27 18:26:03 +01004419 free_string_option(*varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02004420 *varp = oldval;
4421 // When resetting some values, need to act on it.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004422 if (args.os_restore_chartab)
Bram Moolenaardac13472019-09-16 21:06:21 +02004423 (void)init_chartab();
4424 if (varp == &p_hl)
4425 (void)highlight_changed();
4426 }
4427 else
4428 {
4429#ifdef FEAT_EVAL
4430 // Remember where the option was set.
4431 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4432#endif
4433 // Free string options that are in allocated memory.
4434 // Use "free_oldval", because recursiveness may change the flags under
4435 // our fingers (esp. init_highlight()).
4436 if (free_oldval)
4437 free_string_option(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01004438 set_option_flag(opt_idx, P_ALLOCED);
Bram Moolenaardac13472019-09-16 21:06:21 +02004439
4440 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
4441 && is_global_local_option(opt_idx))
4442 {
4443 // global option with local value set to use global value; free
4444 // the local value and make it empty
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004445 char_u *p = get_option_varp_scope(opt_idx, OPT_LOCAL);
Bram Moolenaardac13472019-09-16 21:06:21 +02004446 free_string_option(*(char_u **)p);
4447 *(char_u **)p = empty_option;
4448 }
4449
4450 // May set global value for local option.
4451 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
4452 set_string_option_global(opt_idx, varp);
4453
4454 // Trigger the autocommand only after setting the flags.
4455#ifdef FEAT_SYN_HL
Bram Moolenaardac13472019-09-16 21:06:21 +02004456 if (varp == &(curbuf->b_p_syn))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004457 do_syntax_autocmd(args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004458#endif
4459 else if (varp == &(curbuf->b_p_ft))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004460 do_filetype_autocmd(varp, opt_flags, args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004461#ifdef FEAT_SPELL
4462 if (varp == &(curwin->w_s->b_p_spl))
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004463 do_spelllang_source();
Bram Moolenaardac13472019-09-16 21:06:21 +02004464#endif
4465 }
4466
Bram Moolenaardac13472019-09-16 21:06:21 +02004467 if (varp == &p_mouse)
4468 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004469 if (*p_mouse == NUL)
4470 mch_setmouse(FALSE); // switch mouse off
4471 else
Bram Moolenaardac13472019-09-16 21:06:21 +02004472 setmouse(); // in case 'mouse' changed
4473 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004474
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004475#if defined(FEAT_LUA) || defined(PROTO)
4476 if (varp == &p_rtp)
4477 update_package_paths_in_lua();
4478#endif
4479
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004480#if defined(FEAT_LINEBREAK)
4481 // Changing Formatlistpattern when briopt includes the list setting:
4482 // redraw
4483 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
4484 && curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004485 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004486#endif
4487
Bram Moolenaardac13472019-09-16 21:06:21 +02004488 if (curwin->w_curswant != MAXCOL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004489 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02004490 curwin->w_set_curswant = TRUE;
4491
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004492 if ((opt_flags & OPT_NO_REDRAW) == 0)
4493 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004494#ifdef FEAT_GUI
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004495 // set when changing an option that only requires a redraw in the GUI
4496 int redraw_gui_only = FALSE;
4497
4498 if (varp == &p_go // 'guioptions'
4499 || varp == &p_guifont // 'guifont'
4500# ifdef FEAT_GUI_TABLINE
4501 || varp == &p_gtl // 'guitablabel'
4502 || varp == &p_gtt // 'guitabtooltip'
4503# endif
4504# ifdef FEAT_XFONTSET
4505 || varp == &p_guifontset // 'guifontset'
4506# endif
4507 || varp == &p_guifontwide // 'guifontwide'
4508# ifdef FEAT_GUI_GTK
4509 || varp == &p_guiligatures // 'guiligatures'
4510# endif
4511 )
4512 redraw_gui_only = TRUE;
4513
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004514 // check redraw when it's not a GUI option or the GUI is active.
4515 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02004516#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004517 check_redraw(get_option_flags(opt_idx));
4518 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004519
4520#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004521 if (args.os_did_swaptcap)
Bram Moolenaardac13472019-09-16 21:06:21 +02004522 {
4523 set_termname((char_u *)"win32");
4524 init_highlight(TRUE, FALSE);
4525 }
4526#endif
4527
4528 return errmsg;
4529}
4530
4531/*
4532 * Check an option that can be a range of string values.
4533 *
4534 * Return OK for correct value, FAIL otherwise.
4535 * Empty is always OK.
4536 */
4537 static int
4538check_opt_strings(
4539 char_u *val,
4540 char **values,
4541 int list) // when TRUE: accept a list of values
4542{
4543 return opt_strings_flags(val, values, NULL, list);
4544}
4545
4546/*
4547 * Handle an option that can be a range of string values.
4548 * Set a flag in "*flagp" for each string present.
4549 *
4550 * Return OK for correct value, FAIL otherwise.
4551 * Empty is always OK.
4552 */
4553 static int
4554opt_strings_flags(
4555 char_u *val, // new value
4556 char **values, // array of valid string values
4557 unsigned *flagp,
4558 int list) // when TRUE: accept a list of values
4559{
4560 int i;
4561 int len;
4562 unsigned new_flags = 0;
4563
4564 while (*val)
4565 {
4566 for (i = 0; ; ++i)
4567 {
4568 if (values[i] == NULL) // val not found in values[]
4569 return FAIL;
4570
4571 len = (int)STRLEN(values[i]);
4572 if (STRNCMP(values[i], val, len) == 0
4573 && ((list && val[len] == ',') || val[len] == NUL))
4574 {
4575 val += len + (val[len] == ',');
4576 new_flags |= (1 << i);
4577 break; // check next item in val list
4578 }
4579 }
4580 }
4581 if (flagp != NULL)
4582 *flagp = new_flags;
4583
4584 return OK;
4585}
4586
4587/*
4588 * return OK if "p" is a valid fileformat name, FAIL otherwise.
4589 */
4590 int
4591check_ff_value(char_u *p)
4592{
4593 return check_opt_strings(p, p_ff_values, FALSE);
4594}
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004595
4596/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004597 * Save the actual shortmess Flags and clear them temporarily to avoid that
4598 * file messages overwrites any output from the following commands.
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004599 *
4600 * Caller must make sure to first call save_clear_shm_value() and then
4601 * restore_shm_value() exactly the same number of times.
4602 */
4603 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004604save_clear_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004605{
4606 if (STRLEN(p_shm) >= SHM_LEN)
4607 {
4608 iemsg(e_internal_error_shortmess_too_long);
4609 return;
4610 }
4611
4612 if (++set_shm_recursive == 1)
4613 {
4614 STRCPY(shm_buf, p_shm);
4615 set_option_value_give_err((char_u *)"shm", 0L, (char_u *)"", 0);
4616 }
4617}
4618
4619/*
4620 * Restore the shortmess Flags set from the save_clear_shm_value() function.
4621 */
4622 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004623restore_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004624{
4625 if (--set_shm_recursive == 0)
4626 {
4627 set_option_value_give_err((char_u *)"shm", 0L, shm_buf, 0);
4628 vim_memset(shm_buf, 0, SHM_LEN);
4629 }
4630}