blob: ad1dc81a844e20ab53d629d83424c032a880dae8 [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 *
1053did_set_background(optset_T *args UNUSED)
1054{
1055 if (check_opt_strings(p_bg, p_bg_values, FALSE) == FAIL)
1056 return e_invalid_argument;
1057
1058#ifdef FEAT_EVAL
1059 int dark = (*p_bg == 'd');
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001060#endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001061
1062 init_highlight(FALSE, FALSE);
1063
1064#ifdef FEAT_EVAL
1065 if (dark != (*p_bg == 'd')
1066 && get_var_value((char_u *)"g:colors_name") != NULL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001067 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001068 // The color scheme must have set 'background' back to another
1069 // value, that's not what we want here. Disable the color
1070 // scheme and set the colors again.
1071 do_unlet((char_u *)"g:colors_name", TRUE);
1072 free_string_option(p_bg);
1073 p_bg = vim_strsave((char_u *)(dark ? "dark" : "light"));
1074 check_string_option(&p_bg);
1075 init_highlight(FALSE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001076 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001077#endif
1078#ifdef FEAT_TERMINAL
1079 term_update_colors_all();
1080#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001081
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001082 return NULL;
1083}
1084
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001085 int
1086expand_set_background(optexpand_T *args, int *numMatches, char_u ***matches)
1087{
1088 return expand_set_opt_string(
1089 args,
1090 p_bg_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001091 ARRAY_LENGTH(p_bg_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001092 numMatches,
1093 matches);
1094}
1095
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001096/*
1097 * The 'backspace' option is changed.
1098 */
1099 char *
1100did_set_backspace(optset_T *args UNUSED)
1101{
1102 if (VIM_ISDIGIT(*p_bs))
1103 {
1104 if (*p_bs > '3' || p_bs[1] != NUL)
1105 return e_invalid_argument;
1106 }
1107 else if (check_opt_strings(p_bs, p_bs_values, TRUE) != OK)
1108 return e_invalid_argument;
1109
1110 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001111}
1112
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001113 int
1114expand_set_backspace(optexpand_T *args, int *numMatches, char_u ***matches)
1115{
1116 return expand_set_opt_string(
1117 args,
1118 p_bs_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001119 ARRAY_LENGTH(p_bs_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001120 numMatches,
1121 matches);
1122}
1123
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001124/*
1125 * The 'backupcopy' option is changed.
1126 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001127 char *
1128did_set_backupcopy(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001129{
1130 char_u *bkc = p_bkc;
1131 unsigned int *flags = &bkc_flags;
1132 char *errmsg = NULL;
1133
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001134 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001135 {
1136 bkc = curbuf->b_p_bkc;
1137 flags = &curbuf->b_bkc_flags;
1138 }
1139
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001140 if ((args->os_flags & OPT_LOCAL) && *bkc == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001141 // make the local value empty: use the global value
1142 *flags = 0;
1143 else
1144 {
1145 if (opt_strings_flags(bkc, p_bkc_values, flags, TRUE) != OK)
1146 errmsg = e_invalid_argument;
1147 if ((((int)*flags & BKC_AUTO) != 0)
1148 + (((int)*flags & BKC_YES) != 0)
1149 + (((int)*flags & BKC_NO) != 0) != 1)
1150 {
1151 // Must have exactly one of "auto", "yes" and "no".
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001152 (void)opt_strings_flags(args->os_oldval.string, p_bkc_values,
1153 flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001154 errmsg = e_invalid_argument;
1155 }
1156 }
1157
1158 return errmsg;
1159}
1160
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001161 int
1162expand_set_backupcopy(optexpand_T *args, int *numMatches, char_u ***matches)
1163{
1164 return expand_set_opt_string(
1165 args,
1166 p_bkc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001167 ARRAY_LENGTH(p_bkc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001168 numMatches,
1169 matches);
1170}
1171
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001172/*
1173 * The 'backupext' or the 'patchmode' option is changed.
1174 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001175 char *
1176did_set_backupext_or_patchmode(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001177{
1178 if (STRCMP(*p_bex == '.' ? p_bex + 1 : p_bex,
1179 *p_pm == '.' ? p_pm + 1 : p_pm) == 0)
1180 return e_backupext_and_patchmode_are_equal;
1181
1182 return NULL;
1183}
1184
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001185/*
1186 * The 'belloff' option is changed.
1187 */
1188 char *
1189did_set_belloff(optset_T *args UNUSED)
1190{
1191 return did_set_opt_flags(p_bo, p_bo_values, &bo_flags, TRUE);
1192}
1193
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001194 int
1195expand_set_belloff(optexpand_T *args, int *numMatches, char_u ***matches)
1196{
1197 return expand_set_opt_string(
1198 args,
1199 p_bo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001200 ARRAY_LENGTH(p_bo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001201 numMatches,
1202 matches);
1203}
1204
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001205#if defined(FEAT_LINEBREAK) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001206/*
1207 * The 'breakindentopt' option is changed.
1208 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001209 char *
1210did_set_breakindentopt(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001211{
1212 char *errmsg = NULL;
1213
1214 if (briopt_check(curwin) == FAIL)
1215 errmsg = e_invalid_argument;
1216 // list setting requires a redraw
1217 if (curwin->w_briopt_list)
1218 redraw_all_later(UPD_NOT_VALID);
1219
1220 return errmsg;
1221}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001222
1223 int
1224expand_set_breakindentopt(optexpand_T *args, int *numMatches, char_u ***matches)
1225{
1226 return expand_set_opt_string(
1227 args,
1228 p_briopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001229 ARRAY_LENGTH(p_briopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001230 numMatches,
1231 matches);
1232}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001233#endif
1234
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001235#if defined(FEAT_BROWSE) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001236/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001237 * The 'browsedir' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001238 */
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00001239 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001240did_set_browsedir(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001241{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001242 if (check_opt_strings(p_bsdir, p_bsdir_values, FALSE) != OK
1243 && !mch_isdir(p_bsdir))
1244 return e_invalid_argument;
1245
1246 return NULL;
1247}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001248
1249 int
1250expand_set_browsedir(optexpand_T *args, int *numMatches, char_u ***matches)
1251{
1252 return expand_set_opt_string(
1253 args,
1254 p_bsdir_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001255 ARRAY_LENGTH(p_bsdir_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001256 numMatches,
1257 matches);
1258}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001259#endif
1260
1261/*
1262 * The 'bufhidden' option is changed.
1263 */
1264 char *
1265did_set_bufhidden(optset_T *args UNUSED)
1266{
1267 return did_set_opt_strings(curbuf->b_p_bh, p_bufhidden_values, FALSE);
1268}
1269
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001270 int
1271expand_set_bufhidden(optexpand_T *args, int *numMatches, char_u ***matches)
1272{
1273 return expand_set_opt_string(
1274 args,
1275 p_bufhidden_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001276 ARRAY_LENGTH(p_bufhidden_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001277 numMatches,
1278 matches);
1279}
1280
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001281/*
1282 * The 'buftype' option is changed.
1283 */
1284 char *
1285did_set_buftype(optset_T *args UNUSED)
1286{
1287 if (check_opt_strings(curbuf->b_p_bt, p_buftype_values, FALSE) != OK)
1288 return e_invalid_argument;
1289
1290 if (curwin->w_status_height)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001291 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001292 curwin->w_redr_status = TRUE;
1293 redraw_later(UPD_VALID);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001294 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001295 curbuf->b_help = (curbuf->b_p_bt[0] == 'h');
1296 redraw_titles();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001297
1298 return NULL;
1299}
1300
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001301 int
1302expand_set_buftype(optexpand_T *args, int *numMatches, char_u ***matches)
1303{
1304 return expand_set_opt_string(
1305 args,
1306 p_buftype_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001307 ARRAY_LENGTH(p_buftype_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001308 numMatches,
1309 matches);
1310}
1311
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001312/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001313 * The 'casemap' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001314 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001315 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001316did_set_casemap(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001317{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001318 return did_set_opt_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
1319}
1320
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001321 int
1322expand_set_casemap(optexpand_T *args, int *numMatches, char_u ***matches)
1323{
1324 return expand_set_opt_string(
1325 args,
1326 p_cmp_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001327 ARRAY_LENGTH(p_cmp_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001328 numMatches,
1329 matches);
1330}
1331
1332#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1333 int
1334expand_set_clipboard(optexpand_T *args, int *numMatches, char_u ***matches)
1335{
1336 return expand_set_opt_string(
1337 args,
1338 p_cb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001339 ARRAY_LENGTH(p_cb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001340 numMatches,
1341 matches);
1342}
1343#endif
1344
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001345/*
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001346 * The global 'listchars' or 'fillchars' option is changed.
1347 */
1348 static char *
1349did_set_global_listfillchars(char_u *val, int opt_lcs, int opt_flags)
1350{
1351 char *errmsg = NULL;
1352 char_u **local_ptr = opt_lcs ? &curwin->w_p_lcs : &curwin->w_p_fcs;
1353
1354 // only apply the global value to "curwin" when it does not have a
1355 // local value
1356 if (opt_lcs)
1357 errmsg = set_listchars_option(curwin, val,
1358 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL));
1359 else
1360 errmsg = set_fillchars_option(curwin, val,
1361 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL));
1362 if (errmsg != NULL)
1363 return errmsg;
1364
1365 tabpage_T *tp;
1366 win_T *wp;
1367
1368 // If the current window is set to use the global
1369 // 'listchars'/'fillchars' value, clear the window-local value.
1370 if (!(opt_flags & OPT_GLOBAL))
1371 clear_string_option(local_ptr);
1372 FOR_ALL_TAB_WINDOWS(tp, wp)
1373 {
1374 // If the current window has a local value need to apply it
1375 // again, it was changed when setting the global value.
1376 // If no error was returned above, we don't expect an error
1377 // here, so ignore the return value.
1378 if (opt_lcs)
1379 {
1380 if (*wp->w_p_lcs == NUL)
1381 (void)set_listchars_option(wp, wp->w_p_lcs, TRUE);
1382 }
1383 else
1384 {
1385 if (*wp->w_p_fcs == NUL)
1386 (void)set_fillchars_option(wp, wp->w_p_fcs, TRUE);
1387 }
1388 }
1389
1390 redraw_all_later(UPD_NOT_VALID);
1391
1392 return NULL;
1393}
1394
1395/*
1396 * The 'fillchars' option or the 'listchars' option is changed.
1397 */
1398 char *
1399did_set_chars_option(optset_T *args)
1400{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001401 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001402 char *errmsg = NULL;
1403
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001404 if ( varp == &p_lcs // global 'listchars'
1405 || varp == &p_fcs) // global 'fillchars'
1406 errmsg = did_set_global_listfillchars(*varp, varp == &p_lcs,
1407 args->os_flags);
1408 else if (varp == &curwin->w_p_lcs) // local 'listchars'
1409 errmsg = set_listchars_option(curwin, *varp, TRUE);
1410 else if (varp == &curwin->w_p_fcs) // local 'fillchars'
1411 errmsg = set_fillchars_option(curwin, *varp, TRUE);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001412
1413 return errmsg;
1414}
1415
1416/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001417 * Expand 'fillchars' or 'listchars' option value.
1418 */
1419 int
1420expand_set_chars_option(optexpand_T *args, int *numMatches, char_u ***matches)
1421{
1422 char_u **varp = (char_u **)args->oe_varp;
1423 int is_lcs = (varp == &p_lcs || varp == &curwin->w_p_lcs);
1424 return expand_set_opt_generic(
1425 args,
1426 is_lcs ? get_listchars_name : get_fillchars_name,
1427 numMatches,
1428 matches);
1429}
1430
1431/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001432 * The 'cinoptions' option is changed.
1433 */
1434 char *
1435did_set_cinoptions(optset_T *args UNUSED)
1436{
1437 // TODO: recognize errors
1438 parse_cino(curbuf);
1439
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001440 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001441}
1442
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00001443#if defined(FEAT_SYN_HL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001444/*
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001445 * The 'colorcolumn' option is changed.
1446 */
1447 char *
1448did_set_colorcolumn(optset_T *args UNUSED)
1449{
1450 return check_colorcolumn(curwin);
1451}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001452#endif
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001453
1454/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001455 * The 'comments' option is changed.
1456 */
1457 char *
1458did_set_comments(optset_T *args)
1459{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001460 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001461 char_u *s;
1462 char *errmsg = NULL;
1463
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001464 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001465 {
1466 while (*s && *s != ':')
1467 {
1468 if (vim_strchr((char_u *)COM_ALL, *s) == NULL
1469 && !VIM_ISDIGIT(*s) && *s != '-')
1470 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01001471 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001472 break;
1473 }
1474 ++s;
1475 }
1476 if (*s++ == NUL)
1477 errmsg = e_missing_colon;
1478 else if (*s == ',' || *s == NUL)
1479 errmsg = e_zero_length_string;
1480 if (errmsg != NULL)
1481 break;
1482 while (*s && *s != ',')
1483 {
1484 if (*s == '\\' && s[1] != NUL)
1485 ++s;
1486 ++s;
1487 }
1488 s = skip_to_option_part(s);
1489 }
1490
1491 return errmsg;
1492}
1493
1494#if defined(FEAT_FOLDING) || defined(PROTO)
1495/*
1496 * The 'commentstring' option is changed.
1497 */
1498 char *
1499did_set_commentstring(optset_T *args)
1500{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001501 char_u **varp = (char_u **)args->os_varp;
1502
1503 if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001504 return e_commentstring_must_be_empty_or_contain_str;
1505
1506 return NULL;
1507}
1508#endif
1509
1510/*
1511 * The 'complete' option is changed.
1512 */
1513 char *
1514did_set_complete(optset_T *args)
1515{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001516 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001517 char_u *s;
1518
1519 // check if it is a valid value for 'complete' -- Acevedo
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001520 for (s = *varp; *s;)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001521 {
1522 while (*s == ',' || *s == ' ')
1523 s++;
1524 if (!*s)
1525 break;
1526 if (vim_strchr((char_u *)".wbuksid]tU", *s) == NULL)
Christian Brabandtb39b2402023-11-29 11:34:05 +01001527 return illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001528 if (*++s != NUL && *s != ',' && *s != ' ')
1529 {
1530 if (s[-1] == 'k' || s[-1] == 's')
1531 {
1532 // skip optional filename after 'k' and 's'
1533 while (*s && *s != ',' && *s != ' ')
1534 {
1535 if (*s == '\\' && s[1] != NUL)
1536 ++s;
1537 ++s;
1538 }
1539 }
1540 else
1541 {
1542 if (args->os_errbuf != NULL)
1543 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01001544 vim_snprintf((char *)args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001545 _(e_illegal_character_after_chr), *--s);
1546 return args->os_errbuf;
1547 }
1548 return "";
1549 }
1550 }
1551 }
1552
1553 return NULL;
1554}
1555
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001556 int
1557expand_set_complete(optexpand_T *args, int *numMatches, char_u ***matches)
1558{
1559 static char *(p_cpt_values[]) = {
1560 ".", "w", "b", "u", "k", "kspell", "s", "i", "d", "]", "t", "U",
1561 NULL};
1562 return expand_set_opt_string(
1563 args,
1564 p_cpt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001565 ARRAY_LENGTH(p_cpt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001566 numMatches,
1567 matches);
1568}
1569
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001570/*
1571 * The 'completeopt' option is changed.
1572 */
1573 char *
1574did_set_completeopt(optset_T *args UNUSED)
1575{
1576 if (check_opt_strings(p_cot, p_cot_values, TRUE) != OK)
1577 return e_invalid_argument;
1578
1579 completeopt_was_set();
1580 return NULL;
1581}
1582
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001583 int
1584expand_set_completeopt(optexpand_T *args, int *numMatches, char_u ***matches)
1585{
1586 return expand_set_opt_string(
1587 args,
1588 p_cot_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001589 ARRAY_LENGTH(p_cot_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001590 numMatches,
1591 matches);
1592}
1593
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001594#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1595/*
1596 * The 'completepopup' option is changed.
1597 */
1598 char *
1599did_set_completepopup(optset_T *args UNUSED)
1600{
1601 if (parse_completepopup(NULL) == FAIL)
1602 return e_invalid_argument;
1603
1604 popup_close_info();
1605 return NULL;
1606}
1607#endif
1608
1609#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1610/*
1611 * The 'completeslash' option is changed.
1612 */
1613 char *
1614did_set_completeslash(optset_T *args UNUSED)
1615{
1616 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1617 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
1618 return e_invalid_argument;
1619
1620 return NULL;
1621}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001622
1623 int
1624expand_set_completeslash(optexpand_T *args, int *numMatches, char_u ***matches)
1625{
1626 return expand_set_opt_string(
1627 args,
1628 p_csl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001629 ARRAY_LENGTH(p_csl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001630 numMatches,
1631 matches);
1632}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001633#endif
1634
1635#if defined(FEAT_CONCEAL) || defined(PROTO)
1636/*
1637 * The 'concealcursor' option is changed.
1638 */
1639 char *
1640did_set_concealcursor(optset_T *args)
1641{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001642 char_u **varp = (char_u **)args->os_varp;
1643
Christian Brabandtb39b2402023-11-29 11:34:05 +01001644 return did_set_option_listflag(*varp, (char_u *)COCU_ALL, args->os_errbuf,
1645 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001646}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001647
1648 int
1649expand_set_concealcursor(optexpand_T *args, int *numMatches, char_u ***matches)
1650{
1651 return expand_set_opt_listflag(args, (char_u*)COCU_ALL, numMatches, matches);
1652}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001653#endif
1654
1655/*
1656 * The 'cpoptions' option is changed.
1657 */
1658 char *
1659did_set_cpoptions(optset_T *args)
1660{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001661 char_u **varp = (char_u **)args->os_varp;
1662
Christian Brabandtb39b2402023-11-29 11:34:05 +01001663 return did_set_option_listflag(*varp, (char_u *)CPO_ALL, args->os_errbuf,
1664 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001665}
1666
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001667 int
1668expand_set_cpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
1669{
1670 return expand_set_opt_listflag(args, (char_u*)CPO_ALL, numMatches, matches);
1671}
1672
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001673#if defined(FEAT_CRYPT) || defined(PROTO)
1674/*
1675 * The 'cryptkey' option is changed.
1676 */
1677 char *
1678did_set_cryptkey(optset_T *args)
1679{
1680 // Make sure the ":set" command doesn't show the new value in the
1681 // history.
1682 remove_key_from_history();
1683
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02001684 if (args->os_op != OP_NONE)
1685 // Don't allow set+=/-=/^= as they can allow for substring guessing
1686 return e_invalid_argument;
1687
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001688 if (STRCMP(curbuf->b_p_key, args->os_oldval.string) != 0)
1689 {
1690 // Need to update the swapfile.
1691 ml_set_crypt_key(curbuf, args->os_oldval.string,
1692 *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
1693 changed_internal();
1694 }
Christian Brabandt19e6c4f2023-06-27 18:57:10 +01001695# ifdef FEAT_SODIUM
1696 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
1697 crypt_sodium_lock_key(args->os_newval.string);
1698# endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001699
1700 return NULL;
1701}
1702
1703/*
1704 * The 'cryptmethod' option is changed.
1705 */
1706 char *
1707did_set_cryptmethod(optset_T *args)
1708{
1709 char_u *p;
1710 char_u *s;
1711
1712 if (args->os_flags & OPT_LOCAL)
1713 p = curbuf->b_p_cm;
1714 else
1715 p = p_cm;
1716 if (check_opt_strings(p, p_cm_values, TRUE) != OK)
1717 return e_invalid_argument;
1718 else if (crypt_self_test() == FAIL)
1719 return e_invalid_argument;
1720
1721 // When setting the global value to empty, make it "zip".
1722 if (*p_cm == NUL)
1723 {
1724 free_string_option(p_cm);
1725 p_cm = vim_strsave((char_u *)"zip");
1726 }
1727 // When using ":set cm=name" the local value is going to be empty.
1728 // Do that here, otherwise the crypt functions will still use the
1729 // local value.
1730 if ((args->os_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1731 {
1732 free_string_option(curbuf->b_p_cm);
1733 curbuf->b_p_cm = empty_option;
1734 }
1735
1736 // Need to update the swapfile when the effective method changed.
1737 // Set "s" to the effective old value, "p" to the effective new
1738 // method and compare.
1739 if ((args->os_flags & OPT_LOCAL) && *args->os_oldval.string == NUL)
1740 s = p_cm; // was previously using the global value
1741 else
1742 s = args->os_oldval.string;
1743 if (*curbuf->b_p_cm == NUL)
1744 p = p_cm; // is now using the global value
1745 else
1746 p = curbuf->b_p_cm;
1747 if (STRCMP(s, p) != 0)
1748 ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1749
1750 // If the global value changes need to update the swapfile for all
1751 // buffers using that value.
1752 if ((args->os_flags & OPT_GLOBAL)
1753 && STRCMP(p_cm, args->os_oldval.string) != 0)
1754 {
1755 buf_T *buf;
1756
1757 FOR_ALL_BUFFERS(buf)
1758 if (buf != curbuf && *buf->b_p_cm == NUL)
1759 ml_set_crypt_key(buf, buf->b_p_key, args->os_oldval.string);
1760 }
1761 return NULL;
1762}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001763
1764 int
1765expand_set_cryptmethod(optexpand_T *args, int *numMatches, char_u ***matches)
1766{
1767 return expand_set_opt_string(
1768 args,
1769 p_cm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001770 ARRAY_LENGTH(p_cm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001771 numMatches,
1772 matches);
1773}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001774#endif
1775
1776#if (defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1777/*
1778 * The 'cscopequickfix' option is changed.
1779 */
1780 char *
1781did_set_cscopequickfix(optset_T *args UNUSED)
1782{
1783 char_u *p;
1784
1785 if (p_csqf == NULL)
1786 return NULL;
1787
1788 p = p_csqf;
1789 while (*p != NUL)
1790 {
1791 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
1792 || p[1] == NUL
1793 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
1794 || (p[2] != NUL && p[2] != ','))
1795 return e_invalid_argument;
1796 else if (p[2] == NUL)
1797 break;
1798 else
1799 p += 3;
1800 }
1801
1802 return NULL;
1803}
1804#endif
1805
1806#if defined(FEAT_SYN_HL) || defined(PROTO)
1807/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001808 * The 'cursorlineopt' option is changed.
1809 */
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001810 char *
1811did_set_cursorlineopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001812{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001813 char_u **varp = (char_u **)args->os_varp;
1814
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001815 // This could be changed to use opt_strings_flags() instead.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001816 if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001817 return e_invalid_argument;
1818
1819 return NULL;
1820}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001821
1822 int
1823expand_set_cursorlineopt(optexpand_T *args, int *numMatches, char_u ***matches)
1824{
1825 return expand_set_opt_string(
1826 args,
1827 p_culopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001828 ARRAY_LENGTH(p_culopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001829 numMatches,
1830 matches);
1831}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001832#endif
1833
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001834/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001835 * The 'debug' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001836 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001837 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001838did_set_debug(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001839{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001840 return did_set_opt_strings(p_debug, p_debug_values, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001841}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001842
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001843 int
1844expand_set_debug(optexpand_T *args, int *numMatches, char_u ***matches)
1845{
1846 return expand_set_opt_string(
1847 args,
1848 p_debug_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001849 ARRAY_LENGTH(p_debug_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001850 numMatches,
1851 matches);
1852}
1853
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001854#if defined(FEAT_DIFF) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001855/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001856 * The 'diffopt' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001857 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001858 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001859did_set_diffopt(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001860{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001861 if (diffopt_changed() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001862 return e_invalid_argument;
1863
1864 return NULL;
1865}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001866
1867 int
1868expand_set_diffopt(optexpand_T *args, int *numMatches, char_u ***matches)
1869{
1870 expand_T *xp = args->oe_xp;
1871
1872 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
1873 {
1874 // Within "algorithm:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001875 int algo_len = (int)STRLEN("algorithm:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001876 if (xp->xp_pattern - args->oe_set_arg >= algo_len &&
1877 STRNCMP(xp->xp_pattern - algo_len, "algorithm:", algo_len) == 0)
1878 {
1879 return expand_set_opt_string(
1880 args,
1881 p_dip_algorithm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001882 ARRAY_LENGTH(p_dip_algorithm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001883 numMatches,
1884 matches);
1885 }
1886 return FAIL;
1887 }
1888
1889 return expand_set_opt_string(
1890 args,
1891 p_dip_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001892 ARRAY_LENGTH(p_dip_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001893 numMatches,
1894 matches);
1895}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001896#endif
1897
1898/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001899 * The 'display' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001900 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001901 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001902did_set_display(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001903{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001904 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001905 return e_invalid_argument;
1906
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001907 (void)init_chartab();
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001908 return NULL;
1909}
1910
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001911 int
1912expand_set_display(optexpand_T *args, int *numMatches, char_u ***matches)
1913{
1914 return expand_set_opt_string(
1915 args,
1916 p_dy_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001917 ARRAY_LENGTH(p_dy_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001918 numMatches,
1919 matches);
1920}
1921
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001922/*
1923 * The 'eadirection' option is changed.
1924 */
1925 char *
1926did_set_eadirection(optset_T *args UNUSED)
1927{
1928 return did_set_opt_strings(p_ead, p_ead_values, FALSE);
1929}
1930
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001931 int
1932expand_set_eadirection(optexpand_T *args, int *numMatches, char_u ***matches)
1933{
1934 return expand_set_opt_string(
1935 args,
1936 p_ead_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001937 ARRAY_LENGTH(p_ead_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001938 numMatches,
1939 matches);
1940}
1941
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001942/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001943 * One of the 'encoding', 'fileencoding', 'termencoding' or 'makeencoding'
1944 * options is changed.
1945 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001946 char *
1947did_set_encoding(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001948{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001949 char_u **varp = (char_u **)args->os_varp;
1950 char_u **gvarp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001951 char *errmsg = NULL;
1952 char_u *p;
1953
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001954 // Get the global option to compare with, otherwise we would have to check
1955 // two values for all local options.
1956 gvarp = (char_u **)get_option_varp_scope(args->os_idx, OPT_GLOBAL);
1957
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001958 if (gvarp == &p_fenc)
1959 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001960 if (!curbuf->b_p_ma && args->os_flags != OPT_GLOBAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001961 errmsg = e_cannot_make_changes_modifiable_is_off;
1962 else if (vim_strchr(*varp, ',') != NULL)
1963 // No comma allowed in 'fileencoding'; catches confusing it
1964 // with 'fileencodings'.
1965 errmsg = e_invalid_argument;
1966 else
1967 {
1968 // May show a "+" in the title now.
1969 redraw_titles();
1970 // Add 'fileencoding' to the swap file.
1971 ml_setflags(curbuf);
1972 }
1973 }
1974 if (errmsg == NULL)
1975 {
1976 // canonize the value, so that STRCMP() can be used on it
1977 p = enc_canonize(*varp);
1978 if (p != NULL)
1979 {
1980 vim_free(*varp);
1981 *varp = p;
1982 }
1983 if (varp == &p_enc)
1984 {
1985 errmsg = mb_init();
1986 redraw_titles();
1987 }
1988 }
1989
1990#if defined(FEAT_GUI_GTK)
1991 if (errmsg == NULL && varp == &p_tenc && gui.in_use)
1992 {
1993 // GTK uses only a single encoding, and that is UTF-8.
1994 if (STRCMP(p_tenc, "utf-8") != 0)
1995 errmsg = e_cannot_be_changed_in_gtk_GUI;
1996 }
1997#endif
1998
1999 if (errmsg == NULL)
2000 {
2001#ifdef FEAT_KEYMAP
2002 // When 'keymap' is used and 'encoding' changes, reload the keymap
2003 // (with another encoding).
2004 if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
2005 (void)keymap_init();
2006#endif
2007
2008 // When 'termencoding' is not empty and 'encoding' changes or when
2009 // 'termencoding' changes, need to setup for keyboard input and
2010 // display output conversion.
2011 if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
2012 {
2013 if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
2014 || convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
2015 {
2016 semsg(_(e_cannot_convert_between_str_and_str),
2017 p_tenc, p_enc);
2018 errmsg = e_invalid_argument;
2019 }
2020 }
2021
2022#if defined(MSWIN)
K.Takatace3189d2023-02-15 19:13:43 +00002023 // $HOME, $VIM and $VIMRUNTIME may have characters in active code page.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002024 if (varp == &p_enc)
K.Takatace3189d2023-02-15 19:13:43 +00002025 {
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002026 init_homedir();
K.Takatace3189d2023-02-15 19:13:43 +00002027 init_vimdir();
2028 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002029#endif
2030 }
2031
2032 return errmsg;
2033}
2034
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002035 int
2036expand_set_encoding(optexpand_T *args, int *numMatches, char_u ***matches)
2037{
2038 return expand_set_opt_generic(
2039 args,
2040 get_encoding_name,
2041 numMatches,
2042 matches);
2043}
2044
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002045/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002046 * The 'eventignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002047 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002048 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002049did_set_eventignore(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002050{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002051 if (check_ei() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002052 return e_invalid_argument;
2053 return NULL;
2054}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002055
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002056 static char_u *
2057get_eventignore_name(expand_T *xp, int idx)
2058{
2059 // 'eventignore' allows special keyword "all" in addition to
2060 // all event names.
2061 if (idx == 0)
2062 return (char_u *)"all";
2063 return get_event_name_no_group(xp, idx - 1);
2064}
2065
2066 int
2067expand_set_eventignore(optexpand_T *args, int *numMatches, char_u ***matches)
2068{
2069 return expand_set_opt_generic(
2070 args,
2071 get_eventignore_name,
2072 numMatches,
2073 matches);
2074}
2075
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002076/*
2077 * The 'fileformat' option is changed.
2078 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002079 char *
2080did_set_fileformat(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002081{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002082 char_u **varp = (char_u **)args->os_varp;
2083
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002084 if (!curbuf->b_p_ma && !(args->os_flags & OPT_GLOBAL))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002085 return e_cannot_make_changes_modifiable_is_off;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002086 else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002087 return e_invalid_argument;
2088
2089 // may also change 'textmode'
2090 if (get_fileformat(curbuf) == EOL_DOS)
2091 curbuf->b_p_tx = TRUE;
2092 else
2093 curbuf->b_p_tx = FALSE;
2094 redraw_titles();
2095 // update flag in swap file
2096 ml_setflags(curbuf);
2097 // Redraw needed when switching to/from "mac": a CR in the text
2098 // will be displayed differently.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002099 if (get_fileformat(curbuf) == EOL_MAC || *args->os_oldval.string == 'm')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002100 redraw_curbuf_later(UPD_NOT_VALID);
2101
2102 return NULL;
2103}
2104
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002105 int
2106expand_set_fileformat(optexpand_T *args, int *numMatches, char_u ***matches)
2107{
2108 return expand_set_opt_string(
2109 args,
2110 p_ff_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002111 ARRAY_LENGTH(p_ff_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002112 numMatches,
2113 matches);
2114}
2115
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002116/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002117 * Function given to ExpandGeneric() to obtain the possible arguments of the
2118 * fileformat options.
2119 */
2120 char_u *
2121get_fileformat_name(expand_T *xp UNUSED, int idx)
2122{
2123 if (idx >= (int)ARRAY_LENGTH(p_ff_values))
2124 return NULL;
2125
2126 return (char_u*)p_ff_values[idx];
2127}
2128
2129/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002130 * The 'fileformats' option is changed.
2131 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002132 char *
2133did_set_fileformats(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002134{
2135 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
2136 return e_invalid_argument;
2137
2138 // also change 'textauto'
2139 if (*p_ffs == NUL)
2140 p_ta = FALSE;
2141 else
2142 p_ta = TRUE;
2143
2144 return NULL;
2145}
2146
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002147/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002148 * The 'filetype' or the 'syntax' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002149 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002150 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002151did_set_filetype_or_syntax(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002152{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002153 char_u **varp = (char_u **)args->os_varp;
2154
2155 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002156 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002157
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002158 args->os_value_changed = STRCMP(args->os_oldval.string, *varp) != 0;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002159
2160 // Since we check the value, there is no need to set P_INSECURE,
2161 // even when the value comes from a modeline.
2162 args->os_value_checked = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002163
2164 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002165}
2166
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002167#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002168/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002169 * The 'foldclose' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002170 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002171 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002172did_set_foldclose(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002173{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002174 return did_set_opt_strings(p_fcl, p_fcl_values, TRUE);
2175}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002176
2177 int
2178expand_set_foldclose(optexpand_T *args, int *numMatches, char_u ***matches)
2179{
2180 return expand_set_opt_string(
2181 args,
2182 p_fcl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002183 ARRAY_LENGTH(p_fcl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002184 numMatches,
2185 matches);
2186}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002187#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002188
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002189#if (defined(FEAT_EVAL) && defined(FEAT_FOLDING)) || defined(PROTO)
2190/*
2191 * The 'foldexpr' option is changed.
2192 */
2193 char *
2194did_set_foldexpr(optset_T *args)
2195{
2196 (void)did_set_optexpr(args);
2197 if (foldmethodIsExpr(curwin))
2198 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002199 return NULL;
2200}
2201#endif
2202
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002203#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002204/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002205 * The 'foldignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002206 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002207 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002208did_set_foldignore(optset_T *args UNUSED)
2209{
2210 if (foldmethodIsIndent(curwin))
2211 foldUpdateAll(curwin);
2212 return NULL;
2213}
2214
2215/*
2216 * The 'foldmarker' option is changed.
2217 */
2218 char *
2219did_set_foldmarker(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002220{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002221 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002222 char_u *p;
2223
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002224 p = vim_strchr(*varp, ',');
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002225 if (p == NULL)
2226 return e_comma_required;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002227 else if (p == *varp || p[1] == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002228 return e_invalid_argument;
2229 else if (foldmethodIsMarker(curwin))
2230 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002231
2232 return NULL;
2233}
2234
2235/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002236 * The 'foldmethod' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002237 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002238 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002239did_set_foldmethod(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002240{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002241 char_u **varp = (char_u **)args->os_varp;
2242
2243 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002244 || *curwin->w_p_fdm == NUL)
2245 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002246
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002247 foldUpdateAll(curwin);
2248 if (foldmethodIsDiff(curwin))
2249 newFoldLevel();
2250 return NULL;
2251}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002252
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002253 int
2254expand_set_foldmethod(optexpand_T *args, int *numMatches, char_u ***matches)
2255{
2256 return expand_set_opt_string(
2257 args,
2258 p_fdm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002259 ARRAY_LENGTH(p_fdm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002260 numMatches,
2261 matches);
2262}
2263
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002264/*
2265 * The 'foldopen' option is changed.
2266 */
2267 char *
2268did_set_foldopen(optset_T *args UNUSED)
2269{
2270 return did_set_opt_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
2271}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002272
2273 int
2274expand_set_foldopen(optexpand_T *args, int *numMatches, char_u ***matches)
2275{
2276 return expand_set_opt_string(
2277 args,
2278 p_fdo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002279 ARRAY_LENGTH(p_fdo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002280 numMatches,
2281 matches);
2282}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002283#endif
2284
2285/*
2286 * The 'formatoptions' option is changed.
2287 */
2288 char *
2289did_set_formatoptions(optset_T *args)
2290{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002291 char_u **varp = (char_u **)args->os_varp;
2292
Christian Brabandtb39b2402023-11-29 11:34:05 +01002293 return did_set_option_listflag(*varp, (char_u *)FO_ALL, args->os_errbuf,
2294 args->os_errbuflen);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002295}
2296
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002297 int
2298expand_set_formatoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2299{
2300 return expand_set_opt_listflag(args, (char_u*)FO_ALL, numMatches, matches);
2301}
2302
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002303#if defined(CURSOR_SHAPE) || defined(PROTO)
2304/*
2305 * The 'guicursor' option is changed.
2306 */
2307 char *
2308did_set_guicursor(optset_T *args UNUSED)
2309{
2310 return parse_shape_opt(SHAPE_CURSOR);
2311}
2312#endif
2313
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002314#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002315/*
2316 * The 'guifont' option is changed.
2317 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002318 char *
2319did_set_guifont(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002320{
2321 char_u *p;
2322 char *errmsg = NULL;
2323
2324 if (gui.in_use)
2325 {
2326 p = p_guifont;
2327# if defined(FEAT_GUI_GTK)
2328 // Put up a font dialog and let the user select a new value.
2329 // If this is cancelled go back to the old value but don't
2330 // give an error message.
2331 if (STRCMP(p, "*") == 0)
2332 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002333 p = gui_mch_font_dialog(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002334 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002335 p_guifont = (p != NULL) ? p : vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002336 }
2337# endif
2338 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
2339 {
2340# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
2341 if (STRCMP(p_guifont, "*") == 0)
2342 {
2343 // Dialog was cancelled: Keep the old value without giving
2344 // an error message.
2345 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002346 p_guifont = vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002347 }
2348 else
2349# endif
2350 errmsg = e_invalid_fonts;
2351 }
2352 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002353
2354 return errmsg;
2355}
2356
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002357/*
2358 * Expand the 'guifont' option. Only when GUI is being used. Each platform has
2359 * specific behaviors.
2360 */
2361 int
2362expand_set_guifont(optexpand_T *args, int *numMatches, char_u ***matches)
2363{
2364 if (!gui.in_use)
2365 return FAIL;
2366
2367# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
2368 char_u **varp = (char_u **)args->oe_varp;
2369 int wide = (varp == &p_guifontwide);
2370
2371 return expand_set_opt_callback(
2372 args, gui_mch_expand_font, &wide, numMatches, matches);
2373# else
2374 return FAIL;
2375# endif
2376}
2377
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002378# if defined(FEAT_XFONTSET) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002379/*
2380 * The 'guifontset' option is changed.
2381 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002382 char *
2383did_set_guifontset(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002384{
2385 char *errmsg = NULL;
2386
2387 if (STRCMP(p_guifontset, "*") == 0)
2388 errmsg = e_cant_select_fontset;
2389 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
2390 errmsg = e_invalid_fontset;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002391
2392 return errmsg;
2393}
2394# endif
2395
2396/*
2397 * The 'guifontwide' option is changed.
2398 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002399 char *
2400did_set_guifontwide(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002401{
2402 char *errmsg = NULL;
2403
2404 if (STRCMP(p_guifontwide, "*") == 0)
2405 errmsg = e_cant_select_wide_font;
2406 else if (gui_get_wide_font() == FAIL)
2407 errmsg = e_invalid_wide_font;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002408
2409 return errmsg;
2410}
2411#endif
2412
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002413#if defined(FEAT_GUI_GTK) || defined(PROTO)
2414/*
2415 * The 'guiligatures' option is changed.
2416 */
2417 char *
2418did_set_guiligatures(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002419{
2420 gui_set_ligatures();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002421 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002422}
2423#endif
2424
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002425#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002426/*
2427 * The 'guioptions' option is changed.
2428 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002429 char *
2430did_set_guioptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002431{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002432 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002433 char *errmsg;
2434
Christian Brabandtb39b2402023-11-29 11:34:05 +01002435 errmsg = did_set_option_listflag(*varp, (char_u *)GO_ALL, args->os_errbuf,
2436 args->os_errbuflen);
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002437 if (errmsg != NULL)
2438 return errmsg;
2439
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002440 gui_init_which_components(args->os_oldval.string);
2441 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002442}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002443
2444 int
2445expand_set_guioptions(optexpand_T *args, int *numMatches, char_u ***matches)
2446{
2447 return expand_set_opt_listflag(args, (char_u*)GO_ALL, numMatches, matches);
2448}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002449#endif
2450
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002451#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002452/*
2453 * The 'guitablabel' option is changed.
2454 */
2455 char *
2456did_set_guitablabel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002457{
2458 redraw_tabline = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002459 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002460}
2461#endif
2462
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002463/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002464 * The 'helpfile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002465 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002466 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002467did_set_helpfile(optset_T *args UNUSED)
2468{
2469 // May compute new values for $VIM and $VIMRUNTIME
2470 if (didset_vim)
2471 vim_unsetenv_ext((char_u *)"VIM");
2472 if (didset_vimruntime)
2473 vim_unsetenv_ext((char_u *)"VIMRUNTIME");
2474 return NULL;
2475}
2476
2477#if defined(FEAT_MULTI_LANG) || defined(PROTO)
2478/*
2479 * The 'helplang' option is changed.
2480 */
2481 char *
2482did_set_helplang(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002483{
2484 char *errmsg = NULL;
2485
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002486 // Check for "", "ab", "ab,cd", etc.
2487 for (char_u *s = p_hlg; *s != NUL; s += 3)
2488 {
2489 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
2490 {
2491 errmsg = e_invalid_argument;
2492 break;
2493 }
2494 if (s[2] == NUL)
2495 break;
2496 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002497
2498 return errmsg;
2499}
2500#endif
2501
2502/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002503 * The 'highlight' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002504 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002505 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002506did_set_highlight(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002507{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002508 if (highlight_changed() == FAIL)
2509 return e_invalid_argument; // invalid flags
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002510
2511 return NULL;
2512}
2513
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002514/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002515 * Expand 'highlight' option.
2516 */
2517 int
2518expand_set_highlight(optexpand_T *args, int *numMatches, char_u ***matches)
2519{
2520 char_u *p;
2521 expand_T *xp = args->oe_xp;
2522 static char_u hl_flags[HLF_COUNT] = HL_FLAGS;
Christian Brabandt3f168ec2023-10-02 23:21:11 +02002523 size_t i;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002524 int count = 0;
2525
2526 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2527 {
2528 // Right after a ':', meaning we just return all highlight names.
2529 return expand_set_opt_generic(
2530 args,
2531 get_highlight_name,
2532 numMatches,
2533 matches);
2534 }
2535
2536 if (*xp->xp_pattern == NUL)
2537 {
2538 // At beginning of a comma-separated list. Return the specific list of
2539 // supported occasions.
2540 *matches = ALLOC_MULT(char_u *, HLF_COUNT + 1);
2541 if (*matches == NULL)
2542 return FAIL;
2543
2544 // We still want to return the full option if it's requested.
2545 if (args->oe_include_orig_val)
2546 {
2547 p = vim_strsave(args->oe_opt_value);
2548 if (p == NULL)
2549 {
2550 VIM_CLEAR(*matches);
2551 return FAIL;
2552 }
2553 (*matches)[count++] = p;
2554 }
2555
2556 for (i = 0; i < HLF_COUNT; i++)
2557 {
2558 p = vim_strnsave(&hl_flags[i], 1);
2559 if (p == NULL)
2560 {
2561 if (count == 0)
2562 {
2563 VIM_CLEAR(*matches);
2564 return FAIL;
2565 }
2566 else
2567 break;
2568 }
2569 (*matches)[count++] = p;
2570 }
2571
2572 if (count == 0)
2573 {
2574 VIM_CLEAR(*matches);
2575 return FAIL;
2576 }
2577 *numMatches = count;
2578 return OK;
2579 }
2580
2581 // We are after the initial character (which indicates the occasion). We
2582 // already made sure we are not matching after a ':' above, so now we want
2583 // to match against display mode modifiers.
2584 // Since the xp_pattern starts from the beginning, we need to include it in
2585 // the returned match.
2586
2587 // Note: Keep this in sync with highlight_changed()
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002588 static char_u p_hl_mode_values[] =
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002589 {':', 'b', 'i', '-', 'n', 'r', 's', 'u', 'c', '2', 'd', '=', 't'};
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002590 size_t num_hl_modes = ARRAY_LENGTH(p_hl_mode_values);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002591
2592 *matches = ALLOC_MULT(char_u *, num_hl_modes);
2593 if (*matches == NULL)
2594 return FAIL;
2595
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002596 int pattern_len = xp->xp_pattern_len;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002597
2598 for (i = 0; i < num_hl_modes; i++)
2599 {
2600 // Don't allow duplicates as these are unique flags
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002601 char_u *dup = vim_strchr(xp->xp_pattern + 1, p_hl_mode_values[i]);
2602 if (dup != NULL && (int)(dup - xp->xp_pattern) < pattern_len)
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002603 continue;
2604
2605 // ':' only works by itself, not with other flags.
2606 if (pattern_len > 1 && p_hl_mode_values[i] == ':')
2607 continue;
2608
2609 p = vim_strnsave(xp->xp_pattern, pattern_len + 1);
2610 if (p == NULL)
2611 {
2612 if (i == 0)
2613 {
2614 VIM_CLEAR(*matches);
2615 return FAIL;
2616 }
2617 else
2618 break;
2619 }
2620 p[pattern_len] = p_hl_mode_values[i];
2621 p[pattern_len + 1] = NUL;
2622 (*matches)[count++] = p;
2623 }
2624 if (count == 0)
2625 {
2626 VIM_CLEAR(*matches);
2627 return FAIL;
2628 }
2629 *numMatches = count;
2630
2631 return OK;
2632}
2633
2634/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002635 * The 'titlestring' or the 'iconstring' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002636 */
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002637 static char *
2638parse_titleiconstring(optset_T *args UNUSED, int flagval UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002639{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002640#ifdef FEAT_STL_OPT
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002641 char_u **varp = (char_u **)args->os_varp;
2642
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002643 // NULL => statusline syntax
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002644 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002645 stl_syntax |= flagval;
2646 else
2647 stl_syntax &= ~flagval;
2648#endif
2649 did_set_title();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002650
2651 return NULL;
2652}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002653
2654/*
2655 * The 'iconstring' option is changed.
2656 */
2657 char *
2658did_set_iconstring(optset_T *args)
2659{
2660 int flagval = 0;
2661
2662#ifdef FEAT_STL_OPT
2663 flagval = STL_IN_ICON;
2664#endif
2665
2666 return parse_titleiconstring(args, flagval);
2667}
2668
2669#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
2670/*
2671 * The 'imactivatekey' option is changed.
2672 */
2673 char *
2674did_set_imactivatekey(optset_T *args UNUSED)
2675{
2676 if (!im_xim_isvalid_imactivate())
2677 return e_invalid_argument;
2678 return NULL;
2679}
2680#endif
2681
2682/*
2683 * The 'isident' or the 'iskeyword' or the 'isprint' or the 'isfname' option is
2684 * changed.
2685 */
2686 char *
2687did_set_isopt(optset_T *args)
2688{
2689 // 'isident', 'iskeyword', 'isprint or 'isfname' option: refill g_chartab[]
2690 // If the new option is invalid, use old value.
2691 // 'lisp' option: refill g_chartab[] for '-' char.
2692 if (init_chartab() == FAIL)
2693 {
2694 args->os_restore_chartab = TRUE;// need to restore the chartab.
2695 return e_invalid_argument; // error in value
2696 }
2697
2698 return NULL;
2699}
2700
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002701/*
2702 * The 'jumpoptions' option is changed.
2703 */
2704 char *
Yegappan Lakshmanan1926ae42023-09-21 16:36:28 +02002705did_set_jumpoptions(optset_T *args UNUSED)
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002706{
2707 if (opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE) != OK)
2708 return e_invalid_argument;
2709
2710 return NULL;
2711}
2712
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002713 int
2714expand_set_jumpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2715{
2716 return expand_set_opt_string(
2717 args,
2718 p_jop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002719 ARRAY_LENGTH(p_jop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002720 numMatches,
2721 matches);
2722}
2723
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002724#if defined(FEAT_KEYMAP) || defined(PROTO)
2725/*
2726 * The 'keymap' option is changed.
2727 */
2728 char *
2729did_set_keymap(optset_T *args)
2730{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002731 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002732 char *errmsg = NULL;
2733
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002734 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002735 errmsg = e_invalid_argument;
2736 else
2737 {
2738 int secure_save = secure;
2739
2740 // Reset the secure flag, since the value of 'keymap' has
2741 // been checked to be safe.
2742 secure = 0;
2743
2744 // load or unload key mapping tables
2745 errmsg = keymap_init();
2746
2747 secure = secure_save;
2748
2749 // Since we check the value, there is no need to set P_INSECURE,
2750 // even when the value comes from a modeline.
2751 args->os_value_checked = TRUE;
2752 }
2753
2754 if (errmsg == NULL)
2755 {
2756 if (*curbuf->b_p_keymap != NUL)
2757 {
2758 // Installed a new keymap, switch on using it.
2759 curbuf->b_p_iminsert = B_IMODE_LMAP;
2760 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
2761 curbuf->b_p_imsearch = B_IMODE_LMAP;
2762 }
2763 else
2764 {
2765 // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
2766 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
2767 curbuf->b_p_iminsert = B_IMODE_NONE;
2768 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
2769 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
2770 }
2771 if ((args->os_flags & OPT_LOCAL) == 0)
2772 {
2773 set_iminsert_global();
2774 set_imsearch_global();
2775 }
2776 status_redraw_curbuf();
2777 }
2778
2779 return errmsg;
2780}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002781#endif
2782
2783/*
2784 * The 'keymodel' option is changed.
2785 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002786 char *
2787did_set_keymodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002788{
2789 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
2790 return e_invalid_argument;
2791
2792 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
2793 km_startsel = (vim_strchr(p_km, 'a') != NULL);
2794 return NULL;
2795}
2796
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002797 int
2798expand_set_keymodel(optexpand_T *args, int *numMatches, char_u ***matches)
2799{
2800 return expand_set_opt_string(
2801 args,
2802 p_km_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002803 ARRAY_LENGTH(p_km_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002804 numMatches,
2805 matches);
2806}
2807
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002808/*
2809 * The 'keyprotocol' option is changed.
2810 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002811 char *
2812did_set_keyprotocol(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002813{
Gregory Anders3695d0e2023-09-29 20:17:20 +02002814 char_u *term = T_NAME;
2815 keyprot_T kpc = match_keyprotocol(term);
2816 if (kpc == KEYPROTOCOL_FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002817 return e_invalid_argument;
2818
Gregory Anders3695d0e2023-09-29 20:17:20 +02002819 apply_keyprotocol(term, kpc);
2820
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002821 return NULL;
2822}
2823
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002824 int
2825expand_set_keyprotocol(optexpand_T *args, int *numMatches, char_u ***matches)
2826{
2827 expand_T *xp = args->oe_xp;
2828 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2829 {
2830 // 'keyprotocol' only has well-defined terms for completion for the
2831 // protocol part after the colon.
2832 return expand_set_opt_string(
2833 args,
2834 p_kpc_protocol_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002835 ARRAY_LENGTH(p_kpc_protocol_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002836 numMatches,
2837 matches);
2838 }
2839 // Use expand_set_opt_string instead of returning FAIL so that we can
2840 // include the original value if args->oe_include_orig_val is set.
2841 static char *(empty[]) = {NULL};
2842 return expand_set_opt_string(args, empty, 0, numMatches, matches);
2843}
2844
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002845/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002846 * The 'lispoptions' option is changed.
2847 */
2848 char *
2849did_set_lispoptions(optset_T *args)
2850{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002851 char_u **varp = (char_u **)args->os_varp;
2852
2853 if (**varp != NUL
2854 && STRCMP(*varp, "expr:0") != 0
2855 && STRCMP(*varp, "expr:1") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002856 return e_invalid_argument;
2857
2858 return NULL;
2859}
2860
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002861 int
2862expand_set_lispoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2863{
2864 static char *(p_lop_values[]) = {"expr:0", "expr:1", NULL};
2865 return expand_set_opt_string(
2866 args,
2867 p_lop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002868 ARRAY_LENGTH(p_lop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002869 numMatches,
2870 matches);
2871}
2872
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002873/*
2874 * The 'matchpairs' option is changed.
2875 */
2876 char *
2877did_set_matchpairs(optset_T *args)
2878{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002879 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002880 char_u *p;
2881
2882 if (has_mbyte)
2883 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002884 for (p = *varp; *p != NUL; ++p)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002885 {
2886 int x2 = -1;
2887 int x3 = -1;
2888
2889 p += mb_ptr2len(p);
2890 if (*p != NUL)
2891 x2 = *p++;
2892 if (*p != NUL)
2893 {
2894 x3 = mb_ptr2char(p);
2895 p += mb_ptr2len(p);
2896 }
2897 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
2898 return e_invalid_argument;
2899 if (*p == NUL)
2900 break;
2901 }
2902 }
2903 else
2904 {
2905 // Check for "x:y,x:y"
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002906 for (p = *varp; *p != NUL; p += 4)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002907 {
2908 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
2909 return e_invalid_argument;
2910 if (p[3] == NUL)
2911 break;
2912 }
2913 }
2914
2915 return NULL;
2916}
2917
2918#if defined(FEAT_SPELL) || defined(PROTO)
2919/*
2920 * The 'mkspellmem' option is changed.
2921 */
2922 char *
2923did_set_mkspellmem(optset_T *args UNUSED)
2924{
2925 if (spell_check_msm() != OK)
2926 return e_invalid_argument;
2927
2928 return NULL;
2929}
2930#endif
2931
2932/*
2933 * The 'mouse' option is changed.
2934 */
2935 char *
2936did_set_mouse(optset_T *args)
2937{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002938 char_u **varp = (char_u **)args->os_varp;
2939
Christian Brabandtb39b2402023-11-29 11:34:05 +01002940 return did_set_option_listflag(*varp, (char_u *)MOUSE_ALL, args->os_errbuf,
2941 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002942}
2943
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002944 int
2945expand_set_mouse(optexpand_T *args, int *numMatches, char_u ***matches)
2946{
2947 return expand_set_opt_listflag(args, (char_u*)MOUSE_ALL, numMatches, matches);
2948}
2949
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002950/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002951 * The 'mousemodel' option is changed.
2952 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002953 char *
2954did_set_mousemodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002955{
2956 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
2957 return e_invalid_argument;
2958#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
2959 else if (*p_mousem != *oldval)
2960 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
2961 // to create or delete the popup menus.
2962 gui_motif_update_mousemodel(root_menu);
2963#endif
2964
2965 return NULL;
2966}
2967
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002968 int
2969expand_set_mousemodel(optexpand_T *args, int *numMatches, char_u ***matches)
2970{
2971 return expand_set_opt_string(
2972 args,
2973 p_mousem_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002974 ARRAY_LENGTH(p_mousem_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002975 numMatches,
2976 matches);
2977}
2978
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002979#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
2980 char *
2981did_set_mouseshape(optset_T *args UNUSED)
2982{
2983 char *errmsg = NULL;
2984
2985 errmsg = parse_shape_opt(SHAPE_MOUSE);
2986 update_mouseshape(-1);
2987
2988 return errmsg;
2989}
2990#endif
2991
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002992/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002993 * The 'nrformats' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002994 */
2995 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002996did_set_nrformats(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002997{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002998 char_u **varp = (char_u **)args->os_varp;
2999
3000 return did_set_opt_strings(*varp, p_nf_values, TRUE);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003001}
3002
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003003 int
3004expand_set_nrformats(optexpand_T *args, int *numMatches, char_u ***matches)
3005{
3006 return expand_set_opt_string(
3007 args,
3008 p_nf_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003009 ARRAY_LENGTH(p_nf_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003010 numMatches,
3011 matches);
3012}
3013
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003014#if defined(FEAT_EVAL) || defined(PROTO)
3015/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003016 * One of the '*expr' options is changed: 'balloonexpr', 'diffexpr',
3017 * 'foldexpr', 'foldtext', 'formatexpr', 'includeexpr', 'indentexpr',
3018 * 'patchexpr', 'printexpr' and 'charconvert'.
3019 *
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003020 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003021 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003022did_set_optexpr(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003023{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003024 char_u **varp = (char_u **)args->os_varp;
3025
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003026 // If the option value starts with <SID> or s:, then replace that with
3027 // the script identifier.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003028 char_u *name = get_scriptlocal_funcname(*varp);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003029 if (name != NULL)
3030 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003031 free_string_option(*varp);
3032 *varp = name;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003033 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003034
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003035 return NULL;
3036}
3037#endif
3038
3039/*
3040 * The 'pastetoggle' option is changed.
3041 */
3042 char *
3043did_set_pastetoggle(optset_T *args UNUSED)
3044{
3045 char_u *p;
3046
3047 // translate key codes like in a mapping
3048 if (*p_pt)
3049 {
zeertzjq7e0bae02023-08-11 23:15:38 +02003050 (void)replace_termcodes(p_pt, &p, 0,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003051 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
3052 if (p != NULL)
3053 {
3054 free_string_option(p_pt);
3055 p_pt = p;
3056 }
3057 }
3058
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003059 return NULL;
3060}
3061
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003062#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3063/*
3064 * The 'previewpopup' option is changed.
3065 */
3066 char *
3067did_set_previewpopup(optset_T *args UNUSED)
3068{
3069 if (parse_previewpopup(NULL) == FAIL)
3070 return e_invalid_argument;
3071
3072 return NULL;
3073}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003074
3075 int
3076expand_set_popupoption(optexpand_T *args, int *numMatches, char_u ***matches)
3077{
3078 expand_T *xp = args->oe_xp;
3079
3080 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3081 {
3082 // Within "highlight:"/"border:"/"align:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003083 int border_len = (int)STRLEN("border:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003084 if (xp->xp_pattern - args->oe_set_arg >= border_len &&
3085 STRNCMP(xp->xp_pattern - border_len, "border:", border_len) == 0)
3086 {
3087 return expand_set_opt_string(
3088 args,
3089 p_popup_option_border_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003090 ARRAY_LENGTH(p_popup_option_border_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003091 numMatches,
3092 matches);
3093 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003094 int align_len = (int)STRLEN("align:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003095 if (xp->xp_pattern - args->oe_set_arg >= align_len &&
3096 STRNCMP(xp->xp_pattern - align_len, "align:", align_len) == 0)
3097 {
3098 return expand_set_opt_string(
3099 args,
3100 p_popup_option_align_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003101 ARRAY_LENGTH(p_popup_option_align_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003102 numMatches,
3103 matches);
3104 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003105 int highlight_len = (int)STRLEN("highlight:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003106 if (xp->xp_pattern - args->oe_set_arg >= highlight_len &&
3107 STRNCMP(xp->xp_pattern - highlight_len, "highlight:", highlight_len) == 0)
3108 {
3109 // Return the list of all highlight names
3110 return expand_set_opt_generic(
3111 args,
3112 get_highlight_name,
3113 numMatches,
3114 matches);
3115 }
3116 return FAIL;
3117 }
3118
3119 return expand_set_opt_string(
3120 args,
3121 p_popup_option_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003122 ARRAY_LENGTH(p_popup_option_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003123 numMatches,
3124 matches);
3125}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003126#endif
3127
3128#if defined(FEAT_POSTSCRIPT) || defined(PROTO)
3129/*
3130 * The 'printencoding' option is changed.
3131 */
3132 char *
3133did_set_printencoding(optset_T *args UNUSED)
3134{
3135 char_u *s, *p;
3136
3137 // Canonize 'printencoding' if VIM standard one
3138 p = enc_canonize(p_penc);
3139 if (p != NULL)
3140 {
3141 vim_free(p_penc);
3142 p_penc = p;
3143 }
3144 else
3145 {
3146 // Ensure lower case and '-' for '_'
3147 for (s = p_penc; *s != NUL; s++)
3148 {
3149 if (*s == '_')
3150 *s = '-';
3151 else
3152 *s = TOLOWER_ASC(*s);
3153 }
3154 }
3155
3156 return NULL;
3157}
3158#endif
3159
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003160#if defined(FEAT_PRINTER) || defined(PROTO)
3161
3162 static char_u *
3163get_printoptions_names(expand_T *xp UNUSED, int idx)
3164{
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003165 if (idx >= (int)ARRAY_LENGTH(printer_opts))
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003166 return NULL;
3167 return (char_u*)printer_opts[idx].name;
3168}
3169
3170/*
3171 * Expand 'printoptions' option
3172 */
3173 int
3174expand_set_printoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3175{
3176 return expand_set_opt_generic(
3177 args,
3178 get_printoptions_names,
3179 numMatches,
3180 matches);
3181}
3182#endif
3183
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003184#if defined(FEAT_STL_OPT) || defined(PROTO)
3185/*
3186 * The 'statusline' or the 'tabline' or the 'rulerformat' option is changed.
3187 * "rulerformat" is TRUE if the 'rulerformat' option is changed.
3188 */
3189 static char *
3190parse_statustabline_rulerformat(optset_T *args, int rulerformat)
3191{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003192 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003193 char_u *s;
3194 char *errmsg = NULL;
3195 int wid;
3196
3197 if (rulerformat) // reset ru_wid first
3198 ru_wid = 0;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003199 s = *varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003200 if (rulerformat && *s == '%')
3201 {
3202 // set ru_wid if 'ruf' starts with "%99("
3203 if (*++s == '-') // ignore a '-'
3204 s++;
3205 wid = getdigits(&s);
3206 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
3207 ru_wid = wid;
3208 else
3209 errmsg = check_stl_option(p_ruf);
3210 }
3211 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
3212 else if (rulerformat || s[0] != '%' || s[1] != '!')
3213 errmsg = check_stl_option(s);
3214 if (rulerformat && errmsg == NULL)
3215 comp_col();
3216
3217 return errmsg;
3218}
3219#endif
3220
3221#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
3222/*
3223 * The 'renderoptions' option is changed.
3224 */
3225 char *
3226did_set_renderoptions(optset_T *args UNUSED)
3227{
3228 if (!gui_mch_set_rendering_options(p_rop))
3229 return e_invalid_argument;
3230
3231 return NULL;
3232}
3233#endif
3234
3235#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3236/*
3237 * The 'rightleftcmd' option is changed.
3238 */
3239 char *
3240did_set_rightleftcmd(optset_T *args)
3241{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003242 char_u **varp = (char_u **)args->os_varp;
3243
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003244 // Currently only "search" is a supported value.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003245 if (**varp != NUL && STRCMP(*varp, "search") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003246 return e_invalid_argument;
3247
3248 return NULL;
3249}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003250
3251 int
3252expand_set_rightleftcmd(optexpand_T *args, int *numMatches, char_u ***matches)
3253{
3254 static char *(p_rlc_values[]) = {"search", NULL};
3255 return expand_set_opt_string(
3256 args,
3257 p_rlc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003258 ARRAY_LENGTH(p_rlc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003259 numMatches,
3260 matches);
3261}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003262#endif
3263
3264#if defined(FEAT_STL_OPT) || defined(PROTO)
3265/*
3266 * The 'rulerformat' option is changed.
3267 */
3268 char *
3269did_set_rulerformat(optset_T *args)
3270{
3271 return parse_statustabline_rulerformat(args, TRUE);
3272}
3273#endif
3274
3275/*
3276 * The 'scrollopt' option is changed.
3277 */
3278 char *
3279did_set_scrollopt(optset_T *args UNUSED)
3280{
3281 return did_set_opt_strings(p_sbo, p_scbopt_values, TRUE);
3282}
3283
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003284 int
3285expand_set_scrollopt(optexpand_T *args, int *numMatches, char_u ***matches)
3286{
3287 return expand_set_opt_string(
3288 args,
3289 p_scbopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003290 ARRAY_LENGTH(p_scbopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003291 numMatches,
3292 matches);
3293}
3294
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003295/*
3296 * The 'selection' option is changed.
3297 */
3298 char *
3299did_set_selection(optset_T *args UNUSED)
3300{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003301 if (*p_sel == NUL || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003302 return e_invalid_argument;
3303
3304 return NULL;
3305}
3306
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003307 int
3308expand_set_selection(optexpand_T *args, int *numMatches, char_u ***matches)
3309{
3310 return expand_set_opt_string(
3311 args,
3312 p_sel_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003313 ARRAY_LENGTH(p_sel_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003314 numMatches,
3315 matches);
3316}
3317
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003318/*
3319 * The 'selectmode' option is changed.
3320 */
3321 char *
3322did_set_selectmode(optset_T *args UNUSED)
3323{
3324 return did_set_opt_strings(p_slm, p_slm_values, TRUE);
3325}
3326
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003327 int
3328expand_set_selectmode(optexpand_T *args, int *numMatches, char_u ***matches)
3329{
3330 return expand_set_opt_string(
3331 args,
3332 p_slm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003333 ARRAY_LENGTH(p_slm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003334 numMatches,
3335 matches);
3336}
3337
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003338#if defined(FEAT_SESSION) || defined(PROTO)
3339/*
3340 * The 'sessionoptions' option is changed.
3341 */
3342 char *
3343did_set_sessionoptions(optset_T *args)
3344{
3345 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
3346 return e_invalid_argument;
3347 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
3348 {
3349 // Don't allow both "sesdir" and "curdir".
3350 (void)opt_strings_flags(args->os_oldval.string, p_ssop_values,
3351 &ssop_flags, TRUE);
3352 return e_invalid_argument;
3353 }
3354
3355 return NULL;
3356}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003357
3358 int
3359expand_set_sessionoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3360{
3361 return expand_set_opt_string(
3362 args,
3363 p_ssop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003364 ARRAY_LENGTH(p_ssop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003365 numMatches,
3366 matches);
3367}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003368#endif
3369
3370/*
3371 * The 'shortmess' option is changed.
3372 */
3373 char *
3374did_set_shortmess(optset_T *args)
3375{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003376 char_u **varp = (char_u **)args->os_varp;
3377
Christian Brabandtb39b2402023-11-29 11:34:05 +01003378 return did_set_option_listflag(*varp, (char_u *)SHM_ALL, args->os_errbuf,
3379 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003380}
3381
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003382 int
3383expand_set_shortmess(optexpand_T *args, int *numMatches, char_u ***matches)
3384{
3385 return expand_set_opt_listflag(args, (char_u*)SHM_ALL, numMatches, matches);
3386}
3387
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003388#if defined(FEAT_LINEBREAK) || defined(PROTO)
3389/*
3390 * The 'showbreak' option is changed.
3391 */
3392 char *
3393did_set_showbreak(optset_T *args)
3394{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003395 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003396 char_u *s;
3397
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003398 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003399 {
3400 if (ptr2cells(s) != 1)
3401 return e_showbreak_contains_unprintable_or_wide_character;
3402 MB_PTR_ADV(s);
3403 }
3404
3405 return NULL;
3406}
3407#endif
3408
3409/*
3410 * The 'showcmdloc' option is changed.
3411 */
3412 char *
3413did_set_showcmdloc(optset_T *args UNUSED)
3414{
3415 return did_set_opt_strings(p_sloc, p_sloc_values, FALSE);
3416}
3417
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003418 int
3419expand_set_showcmdloc(optexpand_T *args, int *numMatches, char_u ***matches)
3420{
3421 return expand_set_opt_string(
3422 args,
3423 p_sloc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003424 ARRAY_LENGTH(p_sloc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003425 numMatches,
3426 matches);
3427}
3428
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003429#if defined(FEAT_SIGNS) || defined(PROTO)
3430/*
3431 * The 'signcolumn' option is changed.
3432 */
3433 char *
3434did_set_signcolumn(optset_T *args)
3435{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003436 char_u **varp = (char_u **)args->os_varp;
3437
3438 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003439 return e_invalid_argument;
3440 // When changing the 'signcolumn' to or from 'number', recompute the
3441 // width of the number column if 'number' or 'relativenumber' is set.
3442 if (((*args->os_oldval.string == 'n' && args->os_oldval.string[1] == 'u')
3443 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
3444 && (curwin->w_p_nu || curwin->w_p_rnu))
3445 curwin->w_nrwidth_line_count = 0;
3446
3447 return NULL;
3448}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003449
3450 int
3451expand_set_signcolumn(optexpand_T *args, int *numMatches, char_u ***matches)
3452{
3453 return expand_set_opt_string(
3454 args,
3455 p_scl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003456 ARRAY_LENGTH(p_scl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003457 numMatches,
3458 matches);
3459}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003460#endif
3461
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003462#if defined(FEAT_SPELL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003463/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003464 * The 'spellcapcheck' option is changed.
3465 */
3466 char *
3467did_set_spellcapcheck(optset_T *args UNUSED)
3468{
3469 // compile the regexp program.
3470 return compile_cap_prog(curwin->w_s);
3471}
3472
3473/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003474 * The 'spellfile' option is changed.
3475 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003476 char *
3477did_set_spellfile(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003478{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003479 char_u **varp = (char_u **)args->os_varp;
3480
3481 if (!valid_spellfile(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003482 return e_invalid_argument;
3483
3484 // If there is a window for this buffer in which 'spell' is set load the
3485 // wordlists.
3486 return did_set_spell_option(TRUE);
3487}
3488
3489/*
3490 * The 'spell' option is changed.
3491 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003492 char *
3493did_set_spelllang(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003494{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003495 char_u **varp = (char_u **)args->os_varp;
3496
3497 if (!valid_spelllang(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003498 return e_invalid_argument;
3499
3500 // If there is a window for this buffer in which 'spell' is set load the
3501 // wordlists.
3502 return did_set_spell_option(FALSE);
3503}
3504
3505/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003506 * The 'spelloptions' option is changed.
3507 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003508 char *
3509did_set_spelloptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003510{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003511 char_u **varp = (char_u **)args->os_varp;
3512
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003513 if (**varp != NUL && STRCMP(*varp, "camel") != 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003514 return e_invalid_argument;
3515
3516 return NULL;
3517}
3518
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003519 int
3520expand_set_spelloptions(optexpand_T *args, int *numMatches, char_u ***matches)
3521{
3522 static char *(p_spo_values[]) = {"camel", NULL};
3523 return expand_set_opt_string(
3524 args,
3525 p_spo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003526 ARRAY_LENGTH(p_spo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003527 numMatches,
3528 matches);
3529}
3530
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003531/*
3532 * The 'spellsuggest' option is changed.
3533 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003534 char *
3535did_set_spellsuggest(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003536{
3537 if (spell_check_sps() != OK)
3538 return e_invalid_argument;
3539
3540 return NULL;
3541}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003542
3543 int
3544expand_set_spellsuggest(optexpand_T *args, int *numMatches, char_u ***matches)
3545{
3546 return expand_set_opt_string(
3547 args,
3548 p_sps_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003549 ARRAY_LENGTH(p_sps_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003550 numMatches,
3551 matches);
3552}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003553#endif
3554
3555/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003556 * The 'splitkeep' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003557 */
3558 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003559did_set_splitkeep(optset_T *args UNUSED)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003560{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003561 return did_set_opt_strings(p_spk, p_spk_values, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003562}
3563
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003564 int
3565expand_set_splitkeep(optexpand_T *args, int *numMatches, char_u ***matches)
3566{
3567 return expand_set_opt_string(
3568 args,
3569 p_spk_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003570 ARRAY_LENGTH(p_spk_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003571 numMatches,
3572 matches);
3573}
3574
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00003575#if defined(FEAT_STL_OPT) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003576/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003577 * The 'statusline' option is changed.
3578 */
3579 char *
3580did_set_statusline(optset_T *args)
3581{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003582 return parse_statustabline_rulerformat(args, FALSE);
3583}
3584#endif
3585
3586/*
3587 * The 'swapsync' option is changed.
3588 */
3589 char *
3590did_set_swapsync(optset_T *args UNUSED)
3591{
3592 return did_set_opt_strings(p_sws, p_sws_values, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003593}
3594
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003595 int
3596expand_set_swapsync(optexpand_T *args, int *numMatches, char_u ***matches)
3597{
3598 return expand_set_opt_string(
3599 args,
3600 p_sws_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003601 ARRAY_LENGTH(p_sws_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003602 numMatches,
3603 matches);
3604}
3605
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003606/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003607 * The 'switchbuf' option is changed.
3608 */
3609 char *
3610did_set_switchbuf(optset_T *args UNUSED)
3611{
3612 return did_set_opt_flags(p_swb, p_swb_values, &swb_flags, TRUE);
3613}
3614
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003615 int
3616expand_set_switchbuf(optexpand_T *args, int *numMatches, char_u ***matches)
3617{
3618 return expand_set_opt_string(
3619 args,
3620 p_swb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003621 ARRAY_LENGTH(p_swb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003622 numMatches,
3623 matches);
3624}
3625
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003626#if defined(FEAT_STL_OPT) || defined(PROTO)
3627/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003628 * The 'tabline' option is changed.
3629 */
3630 char *
3631did_set_tabline(optset_T *args)
3632{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003633 return parse_statustabline_rulerformat(args, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003634}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003635#endif
3636
3637/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003638 * The 'tagcase' option is changed.
3639 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003640 char *
3641did_set_tagcase(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003642{
3643 unsigned int *flags;
3644 char_u *p;
3645
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003646 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003647 {
3648 p = curbuf->b_p_tc;
3649 flags = &curbuf->b_tc_flags;
3650 }
3651 else
3652 {
3653 p = p_tc;
3654 flags = &tc_flags;
3655 }
3656
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003657 if ((args->os_flags & OPT_LOCAL) && *p == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003658 // make the local value empty: use the global value
3659 *flags = 0;
3660 else if (*p == NUL
3661 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
3662 return e_invalid_argument;
3663
3664 return NULL;
3665}
3666
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003667 int
3668expand_set_tagcase(optexpand_T *args, int *numMatches, char_u ***matches)
3669{
3670 return expand_set_opt_string(
3671 args,
3672 p_tc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003673 ARRAY_LENGTH(p_tc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003674 numMatches,
3675 matches);
3676}
3677
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003678/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003679 * The 'term' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003680 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003681 char *
3682did_set_term(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003683{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003684 if (T_NAME[0] == NUL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003685 return e_cannot_set_term_to_empty_string;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003686#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003687 if (gui.in_use)
3688 return e_cannot_change_term_in_GUI;
3689 if (term_is_gui(T_NAME))
3690 return e_use_gui_to_start_GUI;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003691#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003692 if (set_termname(T_NAME) == FAIL)
3693 return e_not_found_in_termcap;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003694
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003695 // Screen colors may have changed.
3696 redraw_later_clear();
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003697
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003698 return NULL;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003699}
3700
3701/*
3702 * Some terminal option (t_xxx) is changed
3703 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003704 char *
3705did_set_term_option(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003706{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003707 char_u **varp = (char_u **)args->os_varp;
3708
3709 if (!full_screen)
3710 return NULL;
3711
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003712 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
3713 if (varp == &T_CCO)
3714 {
3715 int colors = atoi((char *)T_CCO);
3716
3717 // Only reinitialize colors if t_Co value has really changed to
3718 // avoid expensive reload of colorscheme if t_Co is set to the
3719 // same value multiple times.
3720 if (colors != t_colors)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003721 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003722 t_colors = colors;
3723 if (t_colors <= 1)
3724 {
3725 vim_free(T_CCO);
3726 T_CCO = empty_option;
3727 }
3728#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3729 if (is_term_win32())
3730 {
3731 swap_tcap();
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003732 args->os_did_swaptcap = TRUE;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003733 }
3734#endif
3735 // We now have a different color setup, initialize it again.
3736 init_highlight(TRUE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003737 }
3738 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003739 ttest(FALSE);
3740 if (varp == &T_ME)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003741 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003742 out_str(T_ME);
3743 redraw_later(UPD_CLEAR);
3744#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3745 // Since t_me has been set, this probably means that the user
3746 // wants to use this as default colors. Need to reset default
3747 // background/foreground colors.
3748# ifdef VIMDLL
3749 if (!gui.in_use && !gui.starting)
3750# endif
3751 mch_set_normal_colors();
3752#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003753 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003754 if (varp == &T_BE && termcap_active)
3755 {
3756 MAY_WANT_TO_LOG_THIS;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003757
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003758 if (*T_BE == NUL)
3759 // When clearing t_BE we assume the user no longer wants
3760 // bracketed paste, thus disable it by writing t_BD.
3761 out_str(T_BD);
3762 else
3763 out_str(T_BE);
3764 }
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003765
3766 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003767}
3768
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003769#if defined(FEAT_TERMINAL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003770/*
3771 * The 'termwinkey' option is changed.
3772 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003773 char *
3774did_set_termwinkey(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003775{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003776 if (*curwin->w_p_twk != NUL && string_to_key(curwin->w_p_twk, TRUE) == 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003777 return e_invalid_argument;
3778
3779 return NULL;
3780}
3781
3782/*
3783 * The 'termwinsize' option is changed.
3784 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003785 char *
3786did_set_termwinsize(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003787{
3788 char_u *p;
3789
3790 if (*curwin->w_p_tws == NUL)
3791 return NULL;
3792
3793 p = skipdigits(curwin->w_p_tws);
3794 if (p == curwin->w_p_tws
3795 || (*p != 'x' && *p != '*')
3796 || *skipdigits(p + 1) != NUL)
3797 return e_invalid_argument;
3798
3799 return NULL;
3800}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003801
3802# if defined(MSWIN) || defined(PROTO)
3803/*
3804 * The 'termwintype' option is changed.
3805 */
3806 char *
3807did_set_termwintype(optset_T *args UNUSED)
3808{
3809 return did_set_opt_strings(p_twt, p_twt_values, FALSE);
3810}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003811
3812 int
3813expand_set_termwintype(optexpand_T *args, int *numMatches, char_u ***matches)
3814{
3815 return expand_set_opt_string(
3816 args,
3817 p_twt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003818 ARRAY_LENGTH(p_twt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003819 numMatches,
3820 matches);
3821}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003822# endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003823#endif
3824
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003825/*
3826 * The 'titlestring' option is changed.
3827 */
3828 char *
3829did_set_titlestring(optset_T *args)
3830{
3831 int flagval = 0;
3832
3833#ifdef FEAT_STL_OPT
3834 flagval = STL_IN_TITLE;
3835#endif
3836 return parse_titleiconstring(args, flagval);
3837}
3838
3839#if (defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)) || defined(PROTO)
3840/*
3841 * The 'toolbar' option is changed.
3842 */
3843 char *
3844did_set_toolbar(optset_T *args UNUSED)
3845{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003846 if (opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags,
3847 TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003848 return e_invalid_argument;
3849
3850 out_flush();
3851 gui_mch_show_toolbar((toolbar_flags &
3852 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
3853 return NULL;
3854}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003855
3856 int
3857expand_set_toolbar(optexpand_T *args, int *numMatches, char_u ***matches)
3858{
3859 return expand_set_opt_string(
3860 args,
3861 p_toolbar_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003862 ARRAY_LENGTH(p_toolbar_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003863 numMatches,
3864 matches);
3865}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003866#endif
3867
3868#if (defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)) || defined(PROTO)
3869/*
3870 * The 'toolbariconsize' option is changed. GTK+ 2 only.
3871 */
3872 char *
3873did_set_toolbariconsize(optset_T *args UNUSED)
3874{
3875 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
3876 return e_invalid_argument;
3877
3878 out_flush();
3879 gui_mch_show_toolbar((toolbar_flags &
3880 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
3881 return NULL;
3882}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003883
3884 int
3885expand_set_toolbariconsize(optexpand_T *args, int *numMatches, char_u ***matches)
3886{
3887 return expand_set_opt_string(
3888 args,
3889 p_tbis_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003890 ARRAY_LENGTH(p_tbis_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003891 numMatches,
3892 matches);
3893}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003894#endif
3895
3896#if defined(UNIX) || defined(VMS) || defined(PROTO)
3897/*
3898 * The 'ttymouse' option is changed.
3899 */
3900 char *
3901did_set_ttymouse(optset_T *args UNUSED)
3902{
3903 char *errmsg = NULL;
3904
3905 // Switch the mouse off before changing the escape sequences used for
3906 // that.
3907 mch_setmouse(FALSE);
3908 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
3909 errmsg = e_invalid_argument;
3910 else
3911 check_mouse_termcode();
3912 if (termcap_active)
3913 setmouse(); // may switch it on again
3914
3915 return errmsg;
3916}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003917
3918 int
3919expand_set_ttymouse(optexpand_T *args, int *numMatches, char_u ***matches)
3920{
3921 return expand_set_opt_string(
3922 args,
3923 p_ttym_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003924 ARRAY_LENGTH(p_ttym_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003925 numMatches,
3926 matches);
3927}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003928#endif
3929
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003930#if defined(FEAT_VARTABS) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003931/*
3932 * The 'varsofttabstop' option is changed.
3933 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003934 char *
3935did_set_varsofttabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003936{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003937 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003938 char_u *cp;
3939
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003940 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00003941 VIM_CLEAR(curbuf->b_p_vsts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003942 else
3943 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003944 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003945 {
3946 if (vim_isdigit(*cp))
3947 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003948 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003949 continue;
3950 return e_invalid_argument;
3951 }
3952
3953 int *oldarray = curbuf->b_p_vsts_array;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003954 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003955 {
3956 if (oldarray)
3957 vim_free(oldarray);
3958 }
3959 else
3960 return e_invalid_argument;
3961 }
3962
3963 return NULL;
3964}
3965
3966/*
3967 * The 'vartabstop' option is changed.
3968 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003969 char *
3970did_set_vartabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003971{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003972 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003973 char_u *cp;
3974
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003975 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00003976 VIM_CLEAR(curbuf->b_p_vts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003977 else
3978 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003979 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003980 {
3981 if (vim_isdigit(*cp))
3982 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003983 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003984 continue;
3985 return e_invalid_argument;
3986 }
3987
3988 int *oldarray = curbuf->b_p_vts_array;
3989
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003990 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003991 {
3992 vim_free(oldarray);
3993# ifdef FEAT_FOLDING
3994 if (foldmethodIsIndent(curwin))
3995 foldUpdateAll(curwin);
3996# endif
3997 }
3998 else
3999 return e_invalid_argument;
4000 }
4001
4002 return NULL;
4003}
4004#endif
4005
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004006/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004007 * The 'verbosefile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004008 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004009 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004010did_set_verbosefile(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004011{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004012 verbose_stop();
4013 if (*p_vfile != NUL && verbose_open() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004014 return e_invalid_argument;
4015
4016 return NULL;
4017}
4018
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004019#if defined(FEAT_SESSION) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004020/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004021 * The 'viewoptions' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004022 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004023 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004024did_set_viewoptions(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004025{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004026 return did_set_opt_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004027}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004028#endif
4029
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004030#if defined(FEAT_VIMINFO) || defined(PROTO)
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004031/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004032 * The 'viminfo' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004033 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004034 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004035did_set_viminfo(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004036{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004037 char_u *s;
4038 char *errmsg = NULL;
4039
4040 for (s = p_viminfo; *s;)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004041 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004042 // Check it's a valid character
4043 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
4044 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01004045 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004046 break;
4047 }
4048 if (*s == 'n') // name is always last one
4049 break;
4050 else if (*s == 'r') // skip until next ','
4051 {
4052 while (*++s && *s != ',')
4053 ;
4054 }
4055 else if (*s == '%')
4056 {
4057 // optional number
4058 while (vim_isdigit(*++s))
4059 ;
4060 }
4061 else if (*s == '!' || *s == 'h' || *s == 'c')
4062 ++s; // no extra chars
4063 else // must have a number
4064 {
4065 while (vim_isdigit(*++s))
4066 ;
4067
4068 if (!VIM_ISDIGIT(*(s - 1)))
4069 {
4070 if (args->os_errbuf != NULL)
4071 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01004072 vim_snprintf(args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004073 _(e_missing_number_after_angle_str_angle),
4074 transchar_byte(*(s - 1)));
4075 errmsg = args->os_errbuf;
4076 }
4077 else
4078 errmsg = "";
4079 break;
4080 }
4081 }
4082 if (*s == ',')
4083 ++s;
4084 else if (*s)
4085 {
4086 if (args->os_errbuf != NULL)
4087 errmsg = e_missing_comma;
4088 else
4089 errmsg = "";
4090 break;
4091 }
4092 }
4093 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
4094 errmsg = e_must_specify_a_value;
4095
4096 return errmsg;
4097}
4098#endif
4099
4100/*
4101 * The 'virtualedit' option is changed.
4102 */
4103 char *
4104did_set_virtualedit(optset_T *args)
4105{
4106 char_u *ve = p_ve;
4107 unsigned int *flags = &ve_flags;
4108
4109 if (args->os_flags & OPT_LOCAL)
4110 {
4111 ve = curwin->w_p_ve;
4112 flags = &curwin->w_ve_flags;
4113 }
4114
4115 if ((args->os_flags & OPT_LOCAL) && *ve == NUL)
4116 // make the local value empty: use the global value
4117 *flags = 0;
4118 else
4119 {
4120 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
4121 return e_invalid_argument;
4122 else if (STRCMP(ve, args->os_oldval.string) != 0)
4123 {
4124 // Recompute cursor position in case the new 've' setting
4125 // changes something.
4126 validate_virtcol();
4127 coladvance(curwin->w_virtcol);
4128 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004129 }
4130
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004131 return NULL;
4132}
4133
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004134 int
4135expand_set_virtualedit(optexpand_T *args, int *numMatches, char_u ***matches)
4136{
4137 return expand_set_opt_string(
4138 args,
4139 p_ve_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004140 ARRAY_LENGTH(p_ve_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004141 numMatches,
4142 matches);
4143}
4144
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004145/*
4146 * The 'whichwrap' option is changed.
4147 */
4148 char *
4149did_set_whichwrap(optset_T *args)
4150{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004151 char_u **varp = (char_u **)args->os_varp;
4152
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004153 // Add ',' to the list flags because 'whichwrap' is a flag
4154 // list that is comma-separated.
Christian Brabandtb39b2402023-11-29 11:34:05 +01004155 return did_set_option_listflag(*varp, (char_u *)(WW_ALL ","),
4156 args->os_errbuf, args->os_errbuflen);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004157}
4158
4159 int
4160expand_set_whichwrap(optexpand_T *args, int *numMatches, char_u ***matches)
4161{
4162 return expand_set_opt_listflag(args, (char_u*)WW_ALL, numMatches, matches);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004163}
4164
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004165/*
4166 * The 'wildmode' option is changed.
4167 */
4168 char *
4169did_set_wildmode(optset_T *args UNUSED)
4170{
4171 if (check_opt_wim() == FAIL)
4172 return e_invalid_argument;
4173 return NULL;
4174}
4175
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004176 int
4177expand_set_wildmode(optexpand_T *args, int *numMatches, char_u ***matches)
4178{
4179 return expand_set_opt_string(
4180 args,
4181 p_wim_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004182 ARRAY_LENGTH(p_wim_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004183 numMatches,
4184 matches);
4185}
4186
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004187/*
4188 * The 'wildoptions' option is changed.
4189 */
4190 char *
4191did_set_wildoptions(optset_T *args UNUSED)
4192{
4193 return did_set_opt_strings(p_wop, p_wop_values, TRUE);
4194}
4195
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004196 int
4197expand_set_wildoptions(optexpand_T *args, int *numMatches, char_u ***matches)
4198{
4199 return expand_set_opt_string(
4200 args,
4201 p_wop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004202 ARRAY_LENGTH(p_wop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004203 numMatches,
4204 matches);
4205}
4206
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004207#if defined(FEAT_WAK) || defined(PROTO)
4208/*
4209 * The 'winaltkeys' option is changed.
4210 */
4211 char *
4212did_set_winaltkeys(optset_T *args UNUSED)
4213{
4214 char *errmsg = NULL;
4215
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004216 if (*p_wak == NUL || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004217 errmsg = e_invalid_argument;
4218# ifdef FEAT_MENU
4219# if defined(FEAT_GUI_MOTIF)
4220 else if (gui.in_use)
4221 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4222# elif defined(FEAT_GUI_GTK)
4223 else if (gui.in_use)
4224 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4225# endif
4226# endif
4227 return errmsg;
4228}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004229
4230 int
4231expand_set_winaltkeys(optexpand_T *args, int *numMatches, char_u ***matches)
4232{
4233 return expand_set_opt_string(
4234 args,
4235 p_wak_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004236 ARRAY_LENGTH(p_wak_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004237 numMatches,
4238 matches);
4239}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004240#endif
4241
4242/*
4243 * The 'wincolor' option is changed.
4244 */
4245 char *
4246did_set_wincolor(optset_T *args UNUSED)
4247{
4248#ifdef FEAT_TERMINAL
4249 term_update_wincolor(curwin);
4250#endif
4251 return NULL;
4252}
4253
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004254 int
4255expand_set_wincolor(optexpand_T *args, int *numMatches, char_u ***matches)
4256{
4257 return expand_set_opt_generic(
4258 args,
4259 get_highlight_name,
4260 numMatches,
4261 matches);
4262}
4263
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004264#ifdef FEAT_SYN_HL
4265/*
4266 * When the 'syntax' option is set, load the syntax of that name.
4267 */
4268 static void
4269do_syntax_autocmd(int value_changed)
4270{
4271 static int syn_recursive = 0;
4272
4273 ++syn_recursive;
4274 // Only pass TRUE for "force" when the value changed or not used
4275 // recursively, to avoid endless recurrence.
4276 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
4277 value_changed || syn_recursive == 1, curbuf);
4278 curbuf->b_flags |= BF_SYN_SET;
4279 --syn_recursive;
4280}
4281#endif
4282
4283/*
4284 * When the 'filetype' option is set, trigger the FileType autocommand.
4285 */
4286 static void
4287do_filetype_autocmd(char_u **varp, int opt_flags, int value_changed)
4288{
4289 // Skip this when called from a modeline and the filetype was already set
4290 // to this value.
4291 if ((opt_flags & OPT_MODELINE) && !value_changed)
4292 return;
4293
4294 static int ft_recursive = 0;
4295 int secure_save = secure;
4296
4297 // Reset the secure flag, since the value of 'filetype' has
4298 // been checked to be safe.
4299 secure = 0;
4300
4301 ++ft_recursive;
4302 did_filetype = TRUE;
4303 // Only pass TRUE for "force" when the value changed or not
4304 // used recursively, to avoid endless recurrence.
4305 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
4306 value_changed || ft_recursive == 1, curbuf);
4307 --ft_recursive;
4308 // Just in case the old "curbuf" is now invalid.
4309 if (varp != &(curbuf->b_p_ft))
4310 varp = NULL;
4311
4312 secure = secure_save;
4313}
4314
4315#ifdef FEAT_SPELL
4316/*
4317 * When the 'spelllang' option is set, source the spell/LANG.vim file in
4318 * 'runtimepath'.
4319 */
4320 static void
4321do_spelllang_source(void)
4322{
4323 char_u fname[200];
4324 char_u *p;
4325 char_u *q = curwin->w_s->b_p_spl;
4326
4327 // Skip the first name if it is "cjk".
4328 if (STRNCMP(q, "cjk,", 4) == 0)
4329 q += 4;
4330
4331 // They could set 'spellcapcheck' depending on the language. Use the first
4332 // name in 'spelllang' up to '_region' or '.encoding'.
4333 for (p = q; *p != NUL; ++p)
4334 if (!ASCII_ISALNUM(*p) && *p != '-')
4335 break;
4336 if (p > q)
4337 {
4338 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
4339 (int)(p - q), q);
4340 source_runtime(fname, DIP_ALL);
4341 }
4342}
4343#endif
4344
4345/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004346 * Handle string options that need some action to perform when changed.
zeertzjqf6782732022-07-27 18:26:03 +01004347 * The new value must be allocated.
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004348 * Returns NULL for success, or an untranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +02004349 */
4350 char *
4351did_set_string_option(
4352 int opt_idx, // index in options[] table
4353 char_u **varp, // pointer to the option variable
Bram Moolenaardac13472019-09-16 21:06:21 +02004354 char_u *oldval, // previous value of the option
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004355 char_u *value, // new value of the option
Bram Moolenaardac13472019-09-16 21:06:21 +02004356 char *errbuf, // buffer for errors, or NULL
Mike Williams620f0112023-12-05 15:36:06 +01004357 size_t errbuflen, // length of error buffer
Bram Moolenaardac13472019-09-16 21:06:21 +02004358 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004359 set_op_T op, // OP_ADDING/OP_PREPENDING/OP_REMOVING
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004360 int *value_checked) // value was checked to be safe, no
Bram Moolenaardac13472019-09-16 21:06:21 +02004361 // need to set P_INSECURE
4362{
4363 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +02004364 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004365 opt_did_set_cb_T did_set_cb = get_option_did_set_cb(opt_idx);
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004366 optset_T args;
4367
4368 // 'ttytype' is an alias for 'term'. Both 'term' and 'ttytype' point to
4369 // T_NAME. If 'term' or 'ttytype' is modified, then use the index for the
4370 // 'term' option. Only set the P_ALLOCED flag on 'term'.
4371 if (varp == &T_NAME)
4372 {
4373 opt_idx = findoption((char_u *)"term");
4374 if (opt_idx >= 0)
4375 {
4376 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
4377 did_set_cb = get_option_did_set_cb(opt_idx);
4378 }
4379 }
4380
4381 CLEAR_FIELD(args);
Bram Moolenaardac13472019-09-16 21:06:21 +02004382
Bram Moolenaardac13472019-09-16 21:06:21 +02004383 // Disallow changing some options from secure mode
4384 if ((secure
4385#ifdef HAVE_SANDBOX
4386 || sandbox != 0
4387#endif
4388 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00004389 errmsg = e_not_allowed_here;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004390 // Check for a "normal" directory or file name in some options.
4391 else if (check_illegal_path_names(opt_idx, varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004392 errmsg = e_invalid_argument;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004393 else if (did_set_cb != NULL)
4394 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004395 args.os_varp = (char_u *)varp;
4396 args.os_idx = opt_idx;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004397 args.os_flags = opt_flags;
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004398 args.os_op = op;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004399 args.os_oldval.string = oldval;
4400 args.os_newval.string = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004401 args.os_errbuf = errbuf;
Christian Brabandtb39b2402023-11-29 11:34:05 +01004402 args.os_errbuflen = errbuflen;
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004403 // Invoke the option specific callback function to validate and apply
4404 // the new option value.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004405 errmsg = did_set_cb(&args);
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004406
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004407 // The 'keymap', 'filetype' and 'syntax' option callback functions
4408 // may change the os_value_checked field.
4409 *value_checked = args.os_value_checked;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004410 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004411
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004412 // If an error is detected, restore the previous value.
Bram Moolenaardac13472019-09-16 21:06:21 +02004413 if (errmsg != NULL)
4414 {
zeertzjqf6782732022-07-27 18:26:03 +01004415 free_string_option(*varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02004416 *varp = oldval;
4417 // When resetting some values, need to act on it.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004418 if (args.os_restore_chartab)
Bram Moolenaardac13472019-09-16 21:06:21 +02004419 (void)init_chartab();
4420 if (varp == &p_hl)
4421 (void)highlight_changed();
4422 }
4423 else
4424 {
4425#ifdef FEAT_EVAL
4426 // Remember where the option was set.
4427 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4428#endif
4429 // Free string options that are in allocated memory.
4430 // Use "free_oldval", because recursiveness may change the flags under
4431 // our fingers (esp. init_highlight()).
4432 if (free_oldval)
4433 free_string_option(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01004434 set_option_flag(opt_idx, P_ALLOCED);
Bram Moolenaardac13472019-09-16 21:06:21 +02004435
4436 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
4437 && is_global_local_option(opt_idx))
4438 {
4439 // global option with local value set to use global value; free
4440 // the local value and make it empty
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004441 char_u *p = get_option_varp_scope(opt_idx, OPT_LOCAL);
Bram Moolenaardac13472019-09-16 21:06:21 +02004442 free_string_option(*(char_u **)p);
4443 *(char_u **)p = empty_option;
4444 }
4445
4446 // May set global value for local option.
4447 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
4448 set_string_option_global(opt_idx, varp);
4449
4450 // Trigger the autocommand only after setting the flags.
4451#ifdef FEAT_SYN_HL
Bram Moolenaardac13472019-09-16 21:06:21 +02004452 if (varp == &(curbuf->b_p_syn))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004453 do_syntax_autocmd(args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004454#endif
4455 else if (varp == &(curbuf->b_p_ft))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004456 do_filetype_autocmd(varp, opt_flags, args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004457#ifdef FEAT_SPELL
4458 if (varp == &(curwin->w_s->b_p_spl))
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004459 do_spelllang_source();
Bram Moolenaardac13472019-09-16 21:06:21 +02004460#endif
4461 }
4462
Bram Moolenaardac13472019-09-16 21:06:21 +02004463 if (varp == &p_mouse)
4464 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004465 if (*p_mouse == NUL)
4466 mch_setmouse(FALSE); // switch mouse off
4467 else
Bram Moolenaardac13472019-09-16 21:06:21 +02004468 setmouse(); // in case 'mouse' changed
4469 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004470
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004471#if defined(FEAT_LUA) || defined(PROTO)
4472 if (varp == &p_rtp)
4473 update_package_paths_in_lua();
4474#endif
4475
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004476#if defined(FEAT_LINEBREAK)
4477 // Changing Formatlistpattern when briopt includes the list setting:
4478 // redraw
4479 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
4480 && curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004481 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004482#endif
4483
Bram Moolenaardac13472019-09-16 21:06:21 +02004484 if (curwin->w_curswant != MAXCOL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004485 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02004486 curwin->w_set_curswant = TRUE;
4487
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004488 if ((opt_flags & OPT_NO_REDRAW) == 0)
4489 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004490#ifdef FEAT_GUI
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004491 // set when changing an option that only requires a redraw in the GUI
4492 int redraw_gui_only = FALSE;
4493
4494 if (varp == &p_go // 'guioptions'
4495 || varp == &p_guifont // 'guifont'
4496# ifdef FEAT_GUI_TABLINE
4497 || varp == &p_gtl // 'guitablabel'
4498 || varp == &p_gtt // 'guitabtooltip'
4499# endif
4500# ifdef FEAT_XFONTSET
4501 || varp == &p_guifontset // 'guifontset'
4502# endif
4503 || varp == &p_guifontwide // 'guifontwide'
4504# ifdef FEAT_GUI_GTK
4505 || varp == &p_guiligatures // 'guiligatures'
4506# endif
4507 )
4508 redraw_gui_only = TRUE;
4509
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004510 // check redraw when it's not a GUI option or the GUI is active.
4511 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02004512#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004513 check_redraw(get_option_flags(opt_idx));
4514 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004515
4516#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004517 if (args.os_did_swaptcap)
Bram Moolenaardac13472019-09-16 21:06:21 +02004518 {
4519 set_termname((char_u *)"win32");
4520 init_highlight(TRUE, FALSE);
4521 }
4522#endif
4523
4524 return errmsg;
4525}
4526
4527/*
4528 * Check an option that can be a range of string values.
4529 *
4530 * Return OK for correct value, FAIL otherwise.
4531 * Empty is always OK.
4532 */
4533 static int
4534check_opt_strings(
4535 char_u *val,
4536 char **values,
4537 int list) // when TRUE: accept a list of values
4538{
4539 return opt_strings_flags(val, values, NULL, list);
4540}
4541
4542/*
4543 * Handle an option that can be a range of string values.
4544 * Set a flag in "*flagp" for each string present.
4545 *
4546 * Return OK for correct value, FAIL otherwise.
4547 * Empty is always OK.
4548 */
4549 static int
4550opt_strings_flags(
4551 char_u *val, // new value
4552 char **values, // array of valid string values
4553 unsigned *flagp,
4554 int list) // when TRUE: accept a list of values
4555{
4556 int i;
4557 int len;
4558 unsigned new_flags = 0;
4559
4560 while (*val)
4561 {
4562 for (i = 0; ; ++i)
4563 {
4564 if (values[i] == NULL) // val not found in values[]
4565 return FAIL;
4566
4567 len = (int)STRLEN(values[i]);
4568 if (STRNCMP(values[i], val, len) == 0
4569 && ((list && val[len] == ',') || val[len] == NUL))
4570 {
4571 val += len + (val[len] == ',');
4572 new_flags |= (1 << i);
4573 break; // check next item in val list
4574 }
4575 }
4576 }
4577 if (flagp != NULL)
4578 *flagp = new_flags;
4579
4580 return OK;
4581}
4582
4583/*
4584 * return OK if "p" is a valid fileformat name, FAIL otherwise.
4585 */
4586 int
4587check_ff_value(char_u *p)
4588{
4589 return check_opt_strings(p, p_ff_values, FALSE);
4590}
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004591
4592/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004593 * Save the actual shortmess Flags and clear them temporarily to avoid that
4594 * file messages overwrites any output from the following commands.
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004595 *
4596 * Caller must make sure to first call save_clear_shm_value() and then
4597 * restore_shm_value() exactly the same number of times.
4598 */
4599 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004600save_clear_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004601{
4602 if (STRLEN(p_shm) >= SHM_LEN)
4603 {
4604 iemsg(e_internal_error_shortmess_too_long);
4605 return;
4606 }
4607
4608 if (++set_shm_recursive == 1)
4609 {
4610 STRCPY(shm_buf, p_shm);
4611 set_option_value_give_err((char_u *)"shm", 0L, (char_u *)"", 0);
4612 }
4613}
4614
4615/*
4616 * Restore the shortmess Flags set from the save_clear_shm_value() function.
4617 */
4618 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004619restore_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004620{
4621 if (--set_shm_recursive == 0)
4622 {
4623 set_option_value_give_err((char_u *)"shm", 0L, shm_buf, 0);
4624 vim_memset(shm_buf, 0, SHM_LEN);
4625 }
4626}