blob: 6b40dcd706f94fdf3fcf817da9c9bea0ef1e3634 [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 *
232illegal_char(char *errbuf, int c)
233{
234 if (errbuf == NULL)
235 return "";
Bram Moolenaar1d423ef2022-01-02 21:26:16 +0000236 sprintf((char *)errbuf, _(e_illegal_character_str), (char *)transchar(c));
Bram Moolenaardac13472019-09-16 21:06:21 +0200237 return errbuf;
238}
239
240/*
241 * Check string options in a buffer for NULL value.
242 */
243 void
244check_buf_options(buf_T *buf)
245{
246 check_string_option(&buf->b_p_bh);
247 check_string_option(&buf->b_p_bt);
248 check_string_option(&buf->b_p_fenc);
249 check_string_option(&buf->b_p_ff);
250#ifdef FEAT_FIND_ID
251 check_string_option(&buf->b_p_def);
252 check_string_option(&buf->b_p_inc);
253# ifdef FEAT_EVAL
254 check_string_option(&buf->b_p_inex);
255# endif
256#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +0100257#if defined(FEAT_EVAL)
Bram Moolenaardac13472019-09-16 21:06:21 +0200258 check_string_option(&buf->b_p_inde);
259 check_string_option(&buf->b_p_indk);
260#endif
261#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
262 check_string_option(&buf->b_p_bexpr);
263#endif
264#if defined(FEAT_CRYPT)
265 check_string_option(&buf->b_p_cm);
266#endif
267 check_string_option(&buf->b_p_fp);
268#if defined(FEAT_EVAL)
269 check_string_option(&buf->b_p_fex);
270#endif
271#ifdef FEAT_CRYPT
272 check_string_option(&buf->b_p_key);
273#endif
274 check_string_option(&buf->b_p_kp);
275 check_string_option(&buf->b_p_mps);
276 check_string_option(&buf->b_p_fo);
277 check_string_option(&buf->b_p_flp);
278 check_string_option(&buf->b_p_isk);
Bram Moolenaardac13472019-09-16 21:06:21 +0200279 check_string_option(&buf->b_p_com);
Bram Moolenaardac13472019-09-16 21:06:21 +0200280#ifdef FEAT_FOLDING
281 check_string_option(&buf->b_p_cms);
282#endif
283 check_string_option(&buf->b_p_nf);
Bram Moolenaardac13472019-09-16 21:06:21 +0200284 check_string_option(&buf->b_p_qe);
Bram Moolenaardac13472019-09-16 21:06:21 +0200285#ifdef FEAT_SYN_HL
286 check_string_option(&buf->b_p_syn);
287 check_string_option(&buf->b_s.b_syn_isk);
288#endif
289#ifdef FEAT_SPELL
290 check_string_option(&buf->b_s.b_p_spc);
291 check_string_option(&buf->b_s.b_p_spf);
292 check_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +0200293 check_string_option(&buf->b_s.b_p_spo);
Bram Moolenaardac13472019-09-16 21:06:21 +0200294#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200295 check_string_option(&buf->b_p_sua);
Bram Moolenaardac13472019-09-16 21:06:21 +0200296 check_string_option(&buf->b_p_cink);
297 check_string_option(&buf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +0100298 check_string_option(&buf->b_p_cinsd);
Bram Moolenaardac13472019-09-16 21:06:21 +0200299 parse_cino(buf);
Bram Moolenaar49846fb2022-10-15 16:05:33 +0100300 check_string_option(&buf->b_p_lop);
Bram Moolenaardac13472019-09-16 21:06:21 +0200301 check_string_option(&buf->b_p_ft);
Bram Moolenaardac13472019-09-16 21:06:21 +0200302 check_string_option(&buf->b_p_cinw);
Bram Moolenaardac13472019-09-16 21:06:21 +0200303 check_string_option(&buf->b_p_cpt);
304#ifdef FEAT_COMPL_FUNC
305 check_string_option(&buf->b_p_cfu);
306 check_string_option(&buf->b_p_ofu);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +0100307 check_string_option(&buf->b_p_tsrfu);
Bram Moolenaardac13472019-09-16 21:06:21 +0200308#endif
309#ifdef FEAT_EVAL
310 check_string_option(&buf->b_p_tfu);
311#endif
312#ifdef FEAT_KEYMAP
313 check_string_option(&buf->b_p_keymap);
314#endif
315#ifdef FEAT_QUICKFIX
316 check_string_option(&buf->b_p_gp);
317 check_string_option(&buf->b_p_mp);
318 check_string_option(&buf->b_p_efm);
319#endif
320 check_string_option(&buf->b_p_ep);
321 check_string_option(&buf->b_p_path);
322 check_string_option(&buf->b_p_tags);
323 check_string_option(&buf->b_p_tc);
324 check_string_option(&buf->b_p_dict);
325 check_string_option(&buf->b_p_tsr);
Bram Moolenaardac13472019-09-16 21:06:21 +0200326 check_string_option(&buf->b_p_lw);
Bram Moolenaardac13472019-09-16 21:06:21 +0200327 check_string_option(&buf->b_p_bkc);
328 check_string_option(&buf->b_p_menc);
329#ifdef FEAT_VARTABS
330 check_string_option(&buf->b_p_vsts);
331 check_string_option(&buf->b_p_vts);
332#endif
333}
334
335/*
336 * Free the string allocated for an option.
337 * Checks for the string being empty_option. This may happen if we're out of
338 * memory, vim_strsave() returned NULL, which was replaced by empty_option by
339 * check_options().
340 * Does NOT check for P_ALLOCED flag!
341 */
342 void
343free_string_option(char_u *p)
344{
345 if (p != empty_option)
346 vim_free(p);
347}
348
349 void
350clear_string_option(char_u **pp)
351{
352 if (*pp != empty_option)
353 vim_free(*pp);
354 *pp = empty_option;
355}
356
357 void
358check_string_option(char_u **pp)
359{
360 if (*pp == NULL)
361 *pp = empty_option;
362}
363
364/*
365 * Set global value for string option when it's a local option.
366 */
367 static void
368set_string_option_global(
369 int opt_idx, // option index
370 char_u **varp) // pointer to option variable
371{
372 char_u **p, *s;
373
374 // the global value is always allocated
375 if (is_window_local_option(opt_idx))
376 p = (char_u **)GLOBAL_WO(varp);
377 else
378 p = (char_u **)get_option_var(opt_idx);
379 if (!is_global_option(opt_idx)
380 && p != varp
381 && (s = vim_strsave(*varp)) != NULL)
382 {
383 free_string_option(*p);
384 *p = s;
385 }
386}
387
388/*
389 * Set a string option to a new value (without checking the effect).
390 * The string is copied into allocated memory.
391 * if ("opt_idx" == -1) "name" is used, otherwise "opt_idx" is used.
392 * When "set_sid" is zero set the scriptID to current_sctx.sc_sid. When
393 * "set_sid" is SID_NONE don't set the scriptID. Otherwise set the scriptID to
394 * "set_sid".
395 */
396 void
397set_string_option_direct(
398 char_u *name,
399 int opt_idx,
400 char_u *val,
401 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
402 int set_sid UNUSED)
403{
404 char_u *s;
405 char_u **varp;
406 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
407 int idx = opt_idx;
408
409 if (idx == -1) // use name
410 {
411 idx = findoption(name);
412 if (idx < 0) // not found (should not happen)
413 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000414 semsg(_(e_internal_error_str), "set_string_option_direct()");
RestorerZ68ebcee2023-05-31 17:12:14 +0100415 siemsg("For option %s", name);
Bram Moolenaardac13472019-09-16 21:06:21 +0200416 return;
417 }
418 }
419
420 if (is_hidden_option(idx)) // can't set hidden option
421 return;
422
423 s = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000424 if (s == NULL)
425 return;
426
427 varp = (char_u **)get_option_varp_scope(idx,
428 both ? OPT_LOCAL : opt_flags);
429 if ((opt_flags & OPT_FREE) && (get_option_flags(idx) & P_ALLOCED))
430 free_string_option(*varp);
431 *varp = s;
432
433 // For buffer/window local option may also set the global value.
434 if (both)
435 set_string_option_global(idx, varp);
436
437 set_option_flag(idx, P_ALLOCED);
438
439 // When setting both values of a global option with a local value,
440 // make the local value empty, so that the global value is used.
441 if (is_global_local_option(idx) && both)
Bram Moolenaardac13472019-09-16 21:06:21 +0200442 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000443 free_string_option(*varp);
444 *varp = empty_option;
Bram Moolenaardac13472019-09-16 21:06:21 +0200445 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000446# ifdef FEAT_EVAL
447 if (set_sid != SID_NONE)
448 {
449 sctx_T script_ctx;
450
451 if (set_sid == 0)
452 script_ctx = current_sctx;
453 else
454 {
455 script_ctx.sc_sid = set_sid;
456 script_ctx.sc_seq = 0;
457 script_ctx.sc_lnum = 0;
458 script_ctx.sc_version = 1;
459 }
460 set_option_sctx_idx(idx, opt_flags, script_ctx);
461 }
462# endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200463}
464
Dominique Pellee764d1b2023-03-12 21:20:59 +0000465#if defined(FEAT_PROP_POPUP) || \
466 (defined(FEAT_DIFF) && defined(FEAT_FOLDING)) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200467/*
468 * Like set_string_option_direct(), but for a window-local option in "wp".
469 * Blocks autocommands to avoid the old curwin becoming invalid.
470 */
471 void
472set_string_option_direct_in_win(
473 win_T *wp,
474 char_u *name,
475 int opt_idx,
476 char_u *val,
477 int opt_flags,
478 int set_sid)
479{
480 win_T *save_curwin = curwin;
481
482 block_autocmds();
483 curwin = wp;
484 curbuf = curwin->w_buffer;
485 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
486 curwin = save_curwin;
487 curbuf = curwin->w_buffer;
488 unblock_autocmds();
489}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000490#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200491
Dominique Pelle748b3082022-01-08 12:41:16 +0000492#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200493/*
494 * Like set_string_option_direct(), but for a buffer-local option in "buf".
495 * Blocks autocommands to avoid the old curbuf becoming invalid.
496 */
497 void
498set_string_option_direct_in_buf(
499 buf_T *buf,
500 char_u *name,
501 int opt_idx,
502 char_u *val,
503 int opt_flags,
504 int set_sid)
505{
506 buf_T *save_curbuf = curbuf;
507
508 block_autocmds();
509 curbuf = buf;
510 curwin->w_buffer = curbuf;
511 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
512 curbuf = save_curbuf;
513 curwin->w_buffer = curbuf;
514 unblock_autocmds();
515}
Dominique Pelle748b3082022-01-08 12:41:16 +0000516#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200517
518/*
519 * Set a string option to a new value, and handle the effects.
520 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100521 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaardac13472019-09-16 21:06:21 +0200522 */
523 char *
524set_string_option(
525 int opt_idx,
526 char_u *value,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +0000527 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
528 char *errbuf)
Bram Moolenaardac13472019-09-16 21:06:21 +0200529{
530 char_u *s;
531 char_u **varp;
532 char_u *oldval;
533#if defined(FEAT_EVAL)
534 char_u *oldval_l = NULL;
535 char_u *oldval_g = NULL;
536 char_u *saved_oldval = NULL;
537 char_u *saved_oldval_l = NULL;
538 char_u *saved_oldval_g = NULL;
539 char_u *saved_newval = NULL;
540#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100541 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +0200542 int value_checked = FALSE;
543
544 if (is_hidden_option(opt_idx)) // don't set hidden option
545 return NULL;
546
Bram Moolenaar7f009df2020-03-16 20:27:38 +0100547 s = vim_strsave(value == NULL ? (char_u *)"" : value);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000548 if (s == NULL)
549 return NULL;
550
551 varp = (char_u **)get_option_varp_scope(opt_idx,
552 (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
553 ? (is_global_local_option(opt_idx)
554 ? OPT_GLOBAL : OPT_LOCAL)
555 : opt_flags);
556 oldval = *varp;
557#if defined(FEAT_EVAL)
558 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +0200559 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000560 oldval_l = *(char_u **)get_option_varp_scope(opt_idx, OPT_LOCAL);
561 oldval_g = *(char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
Bram Moolenaardac13472019-09-16 21:06:21 +0200562 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000563#endif
564 *varp = s;
565
566#if defined(FEAT_EVAL)
567 if (!starting
568# ifdef FEAT_CRYPT
569 && !is_crypt_key_option(opt_idx)
570# endif
571 )
572 {
573 if (oldval_l != NULL)
574 saved_oldval_l = vim_strsave(oldval_l);
575 if (oldval_g != NULL)
576 saved_oldval_g = vim_strsave(oldval_g);
577 saved_oldval = vim_strsave(oldval);
578 saved_newval = vim_strsave(s);
579 }
580#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000581 if ((errmsg = did_set_string_option(opt_idx, varp, oldval, value, errbuf,
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +0200582 opt_flags, OP_NONE, &value_checked)) == NULL)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000583 did_set_option(opt_idx, opt_flags, TRUE, value_checked);
584
585#if defined(FEAT_EVAL)
586 // call autocommand after handling side effects
587 if (errmsg == NULL)
588 trigger_optionset_string(opt_idx, opt_flags,
589 saved_oldval, saved_oldval_l,
590 saved_oldval_g, saved_newval);
591 vim_free(saved_oldval);
592 vim_free(saved_oldval_l);
593 vim_free(saved_oldval_g);
594 vim_free(saved_newval);
595#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100596 return errmsg;
Bram Moolenaardac13472019-09-16 21:06:21 +0200597}
598
599/*
600 * Return TRUE if "val" is a valid 'filetype' name.
601 * Also used for 'syntax' and 'keymap'.
602 */
603 static int
604valid_filetype(char_u *val)
605{
606 return valid_name(val, ".-_");
607}
608
609#ifdef FEAT_STL_OPT
610/*
611 * Check validity of options with the 'statusline' format.
zeertzjq5dc294a2022-04-15 13:17:57 +0100612 * Return an untranslated error message or NULL.
Bram Moolenaardac13472019-09-16 21:06:21 +0200613 */
614 static char *
615check_stl_option(char_u *s)
616{
Bram Moolenaardac13472019-09-16 21:06:21 +0200617 int groupdepth = 0;
618 static char errbuf[80];
619
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100620 while (*s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200621 {
622 // Check for valid keys after % sequences
623 while (*s && *s != '%')
624 s++;
625 if (!*s)
626 break;
627 s++;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +0000628 if (*s == '%' || *s == STL_TRUNCMARK || *s == STL_SEPARATE)
Bram Moolenaardac13472019-09-16 21:06:21 +0200629 {
630 s++;
631 continue;
632 }
633 if (*s == ')')
634 {
635 s++;
636 if (--groupdepth < 0)
637 break;
638 continue;
639 }
640 if (*s == '-')
641 s++;
642 while (VIM_ISDIGIT(*s))
643 s++;
644 if (*s == STL_USER_HL)
645 continue;
646 if (*s == '.')
647 {
648 s++;
649 while (*s && VIM_ISDIGIT(*s))
650 s++;
651 }
652 if (*s == '(')
653 {
654 groupdepth++;
655 continue;
656 }
657 if (vim_strchr(STL_ALL, *s) == NULL)
658 {
659 return illegal_char(errbuf, *s);
660 }
661 if (*s == '{')
662 {
zeertzjq5dc294a2022-04-15 13:17:57 +0100663 int reevaluate = (*++s == '%');
shadmansaleh30e3de22021-05-15 17:23:28 +0200664
zeertzjq5dc294a2022-04-15 13:17:57 +0100665 if (reevaluate && *++s == '}')
666 // "}" is not allowed immediately after "%{%"
667 return illegal_char(errbuf, '}');
shadmansaleh30e3de22021-05-15 17:23:28 +0200668 while ((*s != '}' || (reevaluate && s[-1] != '%')) && *s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200669 s++;
670 if (*s != '}')
zeertzjq5dc294a2022-04-15 13:17:57 +0100671 return e_unclosed_expression_sequence;
Bram Moolenaardac13472019-09-16 21:06:21 +0200672 }
673 }
Bram Moolenaardac13472019-09-16 21:06:21 +0200674 if (groupdepth != 0)
zeertzjq5dc294a2022-04-15 13:17:57 +0100675 return e_unbalanced_groups;
Bram Moolenaardac13472019-09-16 21:06:21 +0200676 return NULL;
677}
678#endif
679
680/*
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +0000681 * Check for a "normal" directory or file name in some options. Disallow a
682 * path separator (slash and/or backslash), wildcards and characters that are
683 * often illegal in a file name. Be more permissive if "secure" is off.
684 */
685 static int
686check_illegal_path_names(int opt_idx, char_u **varp)
687{
688 return (((get_option_flags(opt_idx) & P_NFNAME)
689 && vim_strpbrk(*varp, (char_u *)(secure
690 ? "/\\*?[|;&<>\r\n" : "/\\*?[<>\r\n")) != NULL)
691 || ((get_option_flags(opt_idx) & P_NDNAME)
692 && vim_strpbrk(*varp, (char_u *)"*?[|;&<>\r\n") != NULL));
693}
694
695/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000696 * An option that accepts a list of flags is changed.
697 * e.g. 'viewoptions', 'switchbuf', 'casemap', etc.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000698 */
699 static char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000700did_set_opt_flags(char_u *val, char **values, unsigned *flagp, int list)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000701{
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000702 if (opt_strings_flags(val, values, flagp, list) == FAIL)
703 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000704
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000705 return NULL;
706}
707
708/*
709 * An option that accepts a list of string values is changed.
710 * e.g. 'nrformats', 'scrollopt', 'wildoptions', etc.
711 */
712 static char *
713did_set_opt_strings(char_u *val, char **values, int list)
714{
715 return did_set_opt_flags(val, values, NULL, list);
716}
717
718/*
719 * An option which is a list of flags is set. Valid values are in 'flags'.
720 */
721 static char *
Yegappan Lakshmananc727b192023-03-03 12:26:15 +0000722did_set_option_listflag(char_u *val, char_u *flags, char *errbuf)
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000723{
724 char_u *s;
725
Yegappan Lakshmananc727b192023-03-03 12:26:15 +0000726 for (s = val; *s; ++s)
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000727 if (vim_strchr(flags, *s) == NULL)
728 return illegal_char(errbuf, *s);
729
730 return NULL;
731}
732
733/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200734 * Expand an option that accepts a list of fixed string values with known
735 * number of items.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200736 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200737 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200738expand_set_opt_string(
739 optexpand_T *args,
740 char **values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +0200741 size_t numValues,
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200742 int *numMatches,
743 char_u ***matches)
744{
745 char_u *p;
746 regmatch_T *regmatch = args->oe_regmatch;
747 int include_orig_val = args->oe_include_orig_val;
748 char_u *option_val = args->oe_opt_value;
749
750 // Assume numValues is small since they are fixed enums, so just allocate
751 // upfront instead of needing two passes to calculate output size.
752 *matches = ALLOC_MULT(char_u *, numValues + 1);
753 if (*matches == NULL)
754 return FAIL;
755
756 int count = 0;
757
758 if (include_orig_val && *option_val != NUL)
759 {
760 p = vim_strsave(option_val);
761 if (p == NULL)
762 {
763 VIM_CLEAR(*matches);
764 return FAIL;
765 }
766 (*matches)[count++] = p;
767 }
768
769 for (char **val = values; *val != NULL; val++)
770 {
771 if (include_orig_val && *option_val != NUL)
772 {
773 if (STRCMP((char_u*)*val, option_val) == 0)
774 continue;
775 }
776 if (vim_regexec(regmatch, (char_u*)(*val), (colnr_T)0))
777 {
778 p = vim_strsave((char_u*)*val);
779 if (p == NULL)
780 {
781 if (count == 0)
782 {
783 VIM_CLEAR(*matches);
784 return FAIL;
785 }
786 else
787 break;
788 }
789 (*matches)[count++] = p;
790 }
791 }
792 if (count == 0)
793 {
794 VIM_CLEAR(*matches);
795 return FAIL;
796 }
797 *numMatches = count;
798 return OK;
799}
800
801static char_u *set_opt_callback_orig_option = NULL;
802static char_u *((*set_opt_callback_func)(expand_T *, int));
803
804/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200805 * Callback used by expand_set_opt_generic to also include the original value
806 * as the first item.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200807 */
808 static char_u *
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200809expand_set_opt_generic_cb(expand_T *xp, int idx)
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200810{
811 if (idx == 0)
812 {
813 if (set_opt_callback_orig_option != NULL)
814 return set_opt_callback_orig_option;
815 else
816 return (char_u *)""; // empty strings are ignored
817 }
818 return set_opt_callback_func(xp, idx - 1);
819}
820
821/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200822 * Expand an option with a callback that iterates through a list of possible
823 * names using an index.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200824 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200825 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200826expand_set_opt_generic(
827 optexpand_T *args,
828 char_u *((*func)(expand_T *, int)),
829 int *numMatches,
830 char_u ***matches)
831{
832 int ret;
833
834 set_opt_callback_orig_option = args->oe_include_orig_val ?
835 args->oe_opt_value : NULL;
836 set_opt_callback_func = func;
837
838 ret = ExpandGeneric(
839 (char_u*)"", // not using fuzzy as currently EXPAND_STRING_SETTING doesn't use it
840 args->oe_xp,
841 args->oe_regmatch,
842 matches,
843 numMatches,
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200844 expand_set_opt_generic_cb,
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200845 FALSE);
846
847 set_opt_callback_orig_option = NULL;
848 set_opt_callback_func = NULL;
849 return ret;
850}
851
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200852static garray_T *expand_cb_ga;
853static optexpand_T *expand_cb_args;
854
855/*
856 * Callback provided to a function in expand_set_opt_callback. Will perform
857 * regex matching against the value and add to the list.
858 *
859 * Returns OK usually. Returns FAIL if it failed to allocate memory, and the
860 * caller should terminate the enumeration.
861 */
862 static int
863expand_set_opt_callback_cb(char_u *val)
864{
865 regmatch_T *regmatch = expand_cb_args->oe_regmatch;
866 expand_T *xp = expand_cb_args->oe_xp;
867 garray_T *ga = expand_cb_ga;
868 char_u *str;
869
870 if (val == NULL || *val == NUL)
871 return OK;
872
873 if (xp->xp_pattern[0] != NUL &&
874 !vim_regexec(regmatch, val, (colnr_T)0))
875 return OK;
876
877 str = vim_strsave_escaped(val, (char_u *)" \t\\");
878
879 if (str == NULL)
880 return FAIL;
881
882 if (ga_grow(ga, 1) == FAIL)
883 {
884 vim_free(str);
885 return FAIL;
886 }
887
888 ((char_u **)ga->ga_data)[ga->ga_len] = str;
889 ++ga->ga_len;
890 return OK;
891}
892
893/*
894 * Expand an option with a provided function that takes a callback. The
895 * function will enumerate through all options and call the callback to add it
896 * to the list.
897 *
898 * "func" is the enumerator function that will generate the list of options.
899 * "func_params" is a single parameter that will be passed to func.
900 */
901 static int
902expand_set_opt_callback(
903 optexpand_T *args,
904 void (*func)(optexpand_T *, void* params, int (*cb)(char_u *val)),
905 void *func_params,
906 int *numMatches,
907 char_u ***matches)
908{
909 garray_T ga;
910 int include_orig_val = args->oe_include_orig_val;
911 char_u *option_val = args->oe_opt_value;
912
913 ga_init2(&ga, sizeof(char *), 30);
914
915 if (include_orig_val && *option_val != NUL)
916 {
917 char_u *p = vim_strsave(option_val);
918 if (p == NULL)
919 return FAIL;
920 if (ga_grow(&ga, 1) == FAIL)
921 {
922 vim_free(p);
923 return FAIL;
924 }
925 ((char_u **)ga.ga_data)[ga.ga_len] = p;
926 ++ga.ga_len;
927 }
928
929 expand_cb_ga = &ga;
930 expand_cb_args = args;
931
932 func(args, func_params, expand_set_opt_callback_cb);
933
934 expand_cb_ga = NULL;
935 expand_cb_args = NULL;
936
937 *matches = ga.ga_data;
938 *numMatches = ga.ga_len;
939 return OK;
940}
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200941
942/*
943 * Expand an option which is a list of flags.
944 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200945 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200946expand_set_opt_listflag(
947 optexpand_T *args,
948 char_u *flags,
949 int *numMatches,
950 char_u ***matches)
951{
952 char_u *p;
953 char_u *option_val = args->oe_opt_value;
954 char_u *cmdline_val = args->oe_set_arg;
955 int append = args->oe_append;
956 int include_orig_val = args->oe_include_orig_val && (*option_val != NUL);
957
Yee Cheng Chin6d113472023-10-02 21:38:39 +0200958 size_t num_flags = STRLEN(flags);
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200959
960 // Assume we only have small number of flags, so just allocate max size.
961 *matches = ALLOC_MULT(char_u *, num_flags + 1);
962 if (*matches == NULL)
963 return FAIL;
964
965 int count = 0;
966
967 if (include_orig_val)
968 {
969 p = vim_strsave(option_val);
970 if (p == NULL)
971 {
972 VIM_CLEAR(*matches);
973 return FAIL;
974 }
975 (*matches)[count++] = p;
976 }
977
978 for (char_u *flag = flags; *flag != NUL; flag++)
979 {
980 if (append && vim_strchr(option_val, *flag) != NULL)
981 continue;
982
983 if (vim_strchr(cmdline_val, *flag) == NULL)
984 {
985 if (include_orig_val
986 && option_val[1] == NUL
987 && *flag == option_val[0])
988 {
989 // This value is already used as the first choice as it's the
990 // existing flag. Just skip it to avoid duplicate.
991 continue;
992 }
993 p = vim_strnsave(flag, 1);
994 if (p == NULL)
995 {
996 if (count == 0)
997 {
998 VIM_CLEAR(*matches);
999 return FAIL;
1000 }
1001 else
1002 break;
1003 }
1004 (*matches)[count++] = p;
1005 }
1006 }
1007
1008 if (count == 0)
1009 {
1010 VIM_CLEAR(*matches);
1011 return FAIL;
1012 }
1013 *numMatches = count;
1014 return OK;
1015}
1016
1017/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001018 * The 'ambiwidth' option is changed.
1019 */
1020 char *
1021did_set_ambiwidth(optset_T *args UNUSED)
1022{
1023 if (check_opt_strings(p_ambw, p_ambw_values, FALSE) != OK)
1024 return e_invalid_argument;
1025
1026 return check_chars_options();
1027}
1028
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001029 int
1030expand_set_ambiwidth(optexpand_T *args, int *numMatches, char_u ***matches)
1031{
1032 return expand_set_opt_string(
1033 args,
1034 p_ambw_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001035 ARRAY_LENGTH(p_ambw_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001036 numMatches,
1037 matches);
1038}
1039
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001040/*
1041 * The 'background' option is changed.
1042 */
1043 char *
1044did_set_background(optset_T *args UNUSED)
1045{
1046 if (check_opt_strings(p_bg, p_bg_values, FALSE) == FAIL)
1047 return e_invalid_argument;
1048
1049#ifdef FEAT_EVAL
1050 int dark = (*p_bg == 'd');
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001051#endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001052
1053 init_highlight(FALSE, FALSE);
1054
1055#ifdef FEAT_EVAL
1056 if (dark != (*p_bg == 'd')
1057 && get_var_value((char_u *)"g:colors_name") != NULL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001058 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001059 // The color scheme must have set 'background' back to another
1060 // value, that's not what we want here. Disable the color
1061 // scheme and set the colors again.
1062 do_unlet((char_u *)"g:colors_name", TRUE);
1063 free_string_option(p_bg);
1064 p_bg = vim_strsave((char_u *)(dark ? "dark" : "light"));
1065 check_string_option(&p_bg);
1066 init_highlight(FALSE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001067 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001068#endif
1069#ifdef FEAT_TERMINAL
1070 term_update_colors_all();
1071#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001072
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001073 return NULL;
1074}
1075
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001076 int
1077expand_set_background(optexpand_T *args, int *numMatches, char_u ***matches)
1078{
1079 return expand_set_opt_string(
1080 args,
1081 p_bg_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001082 ARRAY_LENGTH(p_bg_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001083 numMatches,
1084 matches);
1085}
1086
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001087/*
1088 * The 'backspace' option is changed.
1089 */
1090 char *
1091did_set_backspace(optset_T *args UNUSED)
1092{
1093 if (VIM_ISDIGIT(*p_bs))
1094 {
1095 if (*p_bs > '3' || p_bs[1] != NUL)
1096 return e_invalid_argument;
1097 }
1098 else if (check_opt_strings(p_bs, p_bs_values, TRUE) != OK)
1099 return e_invalid_argument;
1100
1101 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001102}
1103
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001104 int
1105expand_set_backspace(optexpand_T *args, int *numMatches, char_u ***matches)
1106{
1107 return expand_set_opt_string(
1108 args,
1109 p_bs_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001110 ARRAY_LENGTH(p_bs_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001111 numMatches,
1112 matches);
1113}
1114
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001115/*
1116 * The 'backupcopy' option is changed.
1117 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001118 char *
1119did_set_backupcopy(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001120{
1121 char_u *bkc = p_bkc;
1122 unsigned int *flags = &bkc_flags;
1123 char *errmsg = NULL;
1124
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001125 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001126 {
1127 bkc = curbuf->b_p_bkc;
1128 flags = &curbuf->b_bkc_flags;
1129 }
1130
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001131 if ((args->os_flags & OPT_LOCAL) && *bkc == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001132 // make the local value empty: use the global value
1133 *flags = 0;
1134 else
1135 {
1136 if (opt_strings_flags(bkc, p_bkc_values, flags, TRUE) != OK)
1137 errmsg = e_invalid_argument;
1138 if ((((int)*flags & BKC_AUTO) != 0)
1139 + (((int)*flags & BKC_YES) != 0)
1140 + (((int)*flags & BKC_NO) != 0) != 1)
1141 {
1142 // Must have exactly one of "auto", "yes" and "no".
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001143 (void)opt_strings_flags(args->os_oldval.string, p_bkc_values,
1144 flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001145 errmsg = e_invalid_argument;
1146 }
1147 }
1148
1149 return errmsg;
1150}
1151
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001152 int
1153expand_set_backupcopy(optexpand_T *args, int *numMatches, char_u ***matches)
1154{
1155 return expand_set_opt_string(
1156 args,
1157 p_bkc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001158 ARRAY_LENGTH(p_bkc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001159 numMatches,
1160 matches);
1161}
1162
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001163/*
1164 * The 'backupext' or the 'patchmode' option is changed.
1165 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001166 char *
1167did_set_backupext_or_patchmode(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001168{
1169 if (STRCMP(*p_bex == '.' ? p_bex + 1 : p_bex,
1170 *p_pm == '.' ? p_pm + 1 : p_pm) == 0)
1171 return e_backupext_and_patchmode_are_equal;
1172
1173 return NULL;
1174}
1175
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001176/*
1177 * The 'belloff' option is changed.
1178 */
1179 char *
1180did_set_belloff(optset_T *args UNUSED)
1181{
1182 return did_set_opt_flags(p_bo, p_bo_values, &bo_flags, TRUE);
1183}
1184
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001185 int
1186expand_set_belloff(optexpand_T *args, int *numMatches, char_u ***matches)
1187{
1188 return expand_set_opt_string(
1189 args,
1190 p_bo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001191 ARRAY_LENGTH(p_bo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001192 numMatches,
1193 matches);
1194}
1195
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001196#if defined(FEAT_LINEBREAK) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001197/*
1198 * The 'breakindentopt' option is changed.
1199 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001200 char *
1201did_set_breakindentopt(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001202{
1203 char *errmsg = NULL;
1204
1205 if (briopt_check(curwin) == FAIL)
1206 errmsg = e_invalid_argument;
1207 // list setting requires a redraw
1208 if (curwin->w_briopt_list)
1209 redraw_all_later(UPD_NOT_VALID);
1210
1211 return errmsg;
1212}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001213
1214 int
1215expand_set_breakindentopt(optexpand_T *args, int *numMatches, char_u ***matches)
1216{
1217 return expand_set_opt_string(
1218 args,
1219 p_briopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001220 ARRAY_LENGTH(p_briopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001221 numMatches,
1222 matches);
1223}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001224#endif
1225
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001226#if defined(FEAT_BROWSE) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001227/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001228 * The 'browsedir' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001229 */
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00001230 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001231did_set_browsedir(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001232{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001233 if (check_opt_strings(p_bsdir, p_bsdir_values, FALSE) != OK
1234 && !mch_isdir(p_bsdir))
1235 return e_invalid_argument;
1236
1237 return NULL;
1238}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001239
1240 int
1241expand_set_browsedir(optexpand_T *args, int *numMatches, char_u ***matches)
1242{
1243 return expand_set_opt_string(
1244 args,
1245 p_bsdir_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001246 ARRAY_LENGTH(p_bsdir_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001247 numMatches,
1248 matches);
1249}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001250#endif
1251
1252/*
1253 * The 'bufhidden' option is changed.
1254 */
1255 char *
1256did_set_bufhidden(optset_T *args UNUSED)
1257{
1258 return did_set_opt_strings(curbuf->b_p_bh, p_bufhidden_values, FALSE);
1259}
1260
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001261 int
1262expand_set_bufhidden(optexpand_T *args, int *numMatches, char_u ***matches)
1263{
1264 return expand_set_opt_string(
1265 args,
1266 p_bufhidden_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001267 ARRAY_LENGTH(p_bufhidden_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001268 numMatches,
1269 matches);
1270}
1271
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001272/*
1273 * The 'buftype' option is changed.
1274 */
1275 char *
1276did_set_buftype(optset_T *args UNUSED)
1277{
1278 if (check_opt_strings(curbuf->b_p_bt, p_buftype_values, FALSE) != OK)
1279 return e_invalid_argument;
1280
1281 if (curwin->w_status_height)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001282 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001283 curwin->w_redr_status = TRUE;
1284 redraw_later(UPD_VALID);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001285 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001286 curbuf->b_help = (curbuf->b_p_bt[0] == 'h');
1287 redraw_titles();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001288
1289 return NULL;
1290}
1291
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001292 int
1293expand_set_buftype(optexpand_T *args, int *numMatches, char_u ***matches)
1294{
1295 return expand_set_opt_string(
1296 args,
1297 p_buftype_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001298 ARRAY_LENGTH(p_buftype_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001299 numMatches,
1300 matches);
1301}
1302
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001303/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001304 * The 'casemap' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001305 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001306 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001307did_set_casemap(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001308{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001309 return did_set_opt_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
1310}
1311
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001312 int
1313expand_set_casemap(optexpand_T *args, int *numMatches, char_u ***matches)
1314{
1315 return expand_set_opt_string(
1316 args,
1317 p_cmp_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001318 ARRAY_LENGTH(p_cmp_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001319 numMatches,
1320 matches);
1321}
1322
1323#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1324 int
1325expand_set_clipboard(optexpand_T *args, int *numMatches, char_u ***matches)
1326{
1327 return expand_set_opt_string(
1328 args,
1329 p_cb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001330 ARRAY_LENGTH(p_cb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001331 numMatches,
1332 matches);
1333}
1334#endif
1335
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001336/*
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001337 * The global 'listchars' or 'fillchars' option is changed.
1338 */
1339 static char *
1340did_set_global_listfillchars(char_u *val, int opt_lcs, int opt_flags)
1341{
1342 char *errmsg = NULL;
1343 char_u **local_ptr = opt_lcs ? &curwin->w_p_lcs : &curwin->w_p_fcs;
1344
1345 // only apply the global value to "curwin" when it does not have a
1346 // local value
1347 if (opt_lcs)
1348 errmsg = set_listchars_option(curwin, val,
1349 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL));
1350 else
1351 errmsg = set_fillchars_option(curwin, val,
1352 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL));
1353 if (errmsg != NULL)
1354 return errmsg;
1355
1356 tabpage_T *tp;
1357 win_T *wp;
1358
1359 // If the current window is set to use the global
1360 // 'listchars'/'fillchars' value, clear the window-local value.
1361 if (!(opt_flags & OPT_GLOBAL))
1362 clear_string_option(local_ptr);
1363 FOR_ALL_TAB_WINDOWS(tp, wp)
1364 {
1365 // If the current window has a local value need to apply it
1366 // again, it was changed when setting the global value.
1367 // If no error was returned above, we don't expect an error
1368 // here, so ignore the return value.
1369 if (opt_lcs)
1370 {
1371 if (*wp->w_p_lcs == NUL)
1372 (void)set_listchars_option(wp, wp->w_p_lcs, TRUE);
1373 }
1374 else
1375 {
1376 if (*wp->w_p_fcs == NUL)
1377 (void)set_fillchars_option(wp, wp->w_p_fcs, TRUE);
1378 }
1379 }
1380
1381 redraw_all_later(UPD_NOT_VALID);
1382
1383 return NULL;
1384}
1385
1386/*
1387 * The 'fillchars' option or the 'listchars' option is changed.
1388 */
1389 char *
1390did_set_chars_option(optset_T *args)
1391{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001392 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001393 char *errmsg = NULL;
1394
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001395 if ( varp == &p_lcs // global 'listchars'
1396 || varp == &p_fcs) // global 'fillchars'
1397 errmsg = did_set_global_listfillchars(*varp, varp == &p_lcs,
1398 args->os_flags);
1399 else if (varp == &curwin->w_p_lcs) // local 'listchars'
1400 errmsg = set_listchars_option(curwin, *varp, TRUE);
1401 else if (varp == &curwin->w_p_fcs) // local 'fillchars'
1402 errmsg = set_fillchars_option(curwin, *varp, TRUE);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001403
1404 return errmsg;
1405}
1406
1407/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001408 * Expand 'fillchars' or 'listchars' option value.
1409 */
1410 int
1411expand_set_chars_option(optexpand_T *args, int *numMatches, char_u ***matches)
1412{
1413 char_u **varp = (char_u **)args->oe_varp;
1414 int is_lcs = (varp == &p_lcs || varp == &curwin->w_p_lcs);
1415 return expand_set_opt_generic(
1416 args,
1417 is_lcs ? get_listchars_name : get_fillchars_name,
1418 numMatches,
1419 matches);
1420}
1421
1422/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001423 * The 'cinoptions' option is changed.
1424 */
1425 char *
1426did_set_cinoptions(optset_T *args UNUSED)
1427{
1428 // TODO: recognize errors
1429 parse_cino(curbuf);
1430
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001431 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001432}
1433
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00001434#if defined(FEAT_SYN_HL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001435/*
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001436 * The 'colorcolumn' option is changed.
1437 */
1438 char *
1439did_set_colorcolumn(optset_T *args UNUSED)
1440{
1441 return check_colorcolumn(curwin);
1442}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001443#endif
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001444
1445/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001446 * The 'comments' option is changed.
1447 */
1448 char *
1449did_set_comments(optset_T *args)
1450{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001451 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001452 char_u *s;
1453 char *errmsg = NULL;
1454
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001455 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001456 {
1457 while (*s && *s != ':')
1458 {
1459 if (vim_strchr((char_u *)COM_ALL, *s) == NULL
1460 && !VIM_ISDIGIT(*s) && *s != '-')
1461 {
1462 errmsg = illegal_char(args->os_errbuf, *s);
1463 break;
1464 }
1465 ++s;
1466 }
1467 if (*s++ == NUL)
1468 errmsg = e_missing_colon;
1469 else if (*s == ',' || *s == NUL)
1470 errmsg = e_zero_length_string;
1471 if (errmsg != NULL)
1472 break;
1473 while (*s && *s != ',')
1474 {
1475 if (*s == '\\' && s[1] != NUL)
1476 ++s;
1477 ++s;
1478 }
1479 s = skip_to_option_part(s);
1480 }
1481
1482 return errmsg;
1483}
1484
1485#if defined(FEAT_FOLDING) || defined(PROTO)
1486/*
1487 * The 'commentstring' option is changed.
1488 */
1489 char *
1490did_set_commentstring(optset_T *args)
1491{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001492 char_u **varp = (char_u **)args->os_varp;
1493
1494 if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001495 return e_commentstring_must_be_empty_or_contain_str;
1496
1497 return NULL;
1498}
1499#endif
1500
1501/*
1502 * The 'complete' option is changed.
1503 */
1504 char *
1505did_set_complete(optset_T *args)
1506{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001507 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001508 char_u *s;
1509
1510 // check if it is a valid value for 'complete' -- Acevedo
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001511 for (s = *varp; *s;)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001512 {
1513 while (*s == ',' || *s == ' ')
1514 s++;
1515 if (!*s)
1516 break;
1517 if (vim_strchr((char_u *)".wbuksid]tU", *s) == NULL)
1518 return illegal_char(args->os_errbuf, *s);
1519 if (*++s != NUL && *s != ',' && *s != ' ')
1520 {
1521 if (s[-1] == 'k' || s[-1] == 's')
1522 {
1523 // skip optional filename after 'k' and 's'
1524 while (*s && *s != ',' && *s != ' ')
1525 {
1526 if (*s == '\\' && s[1] != NUL)
1527 ++s;
1528 ++s;
1529 }
1530 }
1531 else
1532 {
1533 if (args->os_errbuf != NULL)
1534 {
1535 sprintf((char *)args->os_errbuf,
1536 _(e_illegal_character_after_chr), *--s);
1537 return args->os_errbuf;
1538 }
1539 return "";
1540 }
1541 }
1542 }
1543
1544 return NULL;
1545}
1546
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001547 int
1548expand_set_complete(optexpand_T *args, int *numMatches, char_u ***matches)
1549{
1550 static char *(p_cpt_values[]) = {
1551 ".", "w", "b", "u", "k", "kspell", "s", "i", "d", "]", "t", "U",
1552 NULL};
1553 return expand_set_opt_string(
1554 args,
1555 p_cpt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001556 ARRAY_LENGTH(p_cpt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001557 numMatches,
1558 matches);
1559}
1560
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001561/*
1562 * The 'completeopt' option is changed.
1563 */
1564 char *
1565did_set_completeopt(optset_T *args UNUSED)
1566{
1567 if (check_opt_strings(p_cot, p_cot_values, TRUE) != OK)
1568 return e_invalid_argument;
1569
1570 completeopt_was_set();
1571 return NULL;
1572}
1573
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001574 int
1575expand_set_completeopt(optexpand_T *args, int *numMatches, char_u ***matches)
1576{
1577 return expand_set_opt_string(
1578 args,
1579 p_cot_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001580 ARRAY_LENGTH(p_cot_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001581 numMatches,
1582 matches);
1583}
1584
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001585#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1586/*
1587 * The 'completepopup' option is changed.
1588 */
1589 char *
1590did_set_completepopup(optset_T *args UNUSED)
1591{
1592 if (parse_completepopup(NULL) == FAIL)
1593 return e_invalid_argument;
1594
1595 popup_close_info();
1596 return NULL;
1597}
1598#endif
1599
1600#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1601/*
1602 * The 'completeslash' option is changed.
1603 */
1604 char *
1605did_set_completeslash(optset_T *args UNUSED)
1606{
1607 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1608 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
1609 return e_invalid_argument;
1610
1611 return NULL;
1612}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001613
1614 int
1615expand_set_completeslash(optexpand_T *args, int *numMatches, char_u ***matches)
1616{
1617 return expand_set_opt_string(
1618 args,
1619 p_csl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001620 ARRAY_LENGTH(p_csl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001621 numMatches,
1622 matches);
1623}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001624#endif
1625
1626#if defined(FEAT_CONCEAL) || defined(PROTO)
1627/*
1628 * The 'concealcursor' option is changed.
1629 */
1630 char *
1631did_set_concealcursor(optset_T *args)
1632{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001633 char_u **varp = (char_u **)args->os_varp;
1634
1635 return did_set_option_listflag(*varp, (char_u *)COCU_ALL, args->os_errbuf);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001636}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001637
1638 int
1639expand_set_concealcursor(optexpand_T *args, int *numMatches, char_u ***matches)
1640{
1641 return expand_set_opt_listflag(args, (char_u*)COCU_ALL, numMatches, matches);
1642}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001643#endif
1644
1645/*
1646 * The 'cpoptions' option is changed.
1647 */
1648 char *
1649did_set_cpoptions(optset_T *args)
1650{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001651 char_u **varp = (char_u **)args->os_varp;
1652
1653 return did_set_option_listflag(*varp, (char_u *)CPO_ALL, args->os_errbuf);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001654}
1655
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001656 int
1657expand_set_cpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
1658{
1659 return expand_set_opt_listflag(args, (char_u*)CPO_ALL, numMatches, matches);
1660}
1661
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001662#if defined(FEAT_CRYPT) || defined(PROTO)
1663/*
1664 * The 'cryptkey' option is changed.
1665 */
1666 char *
1667did_set_cryptkey(optset_T *args)
1668{
1669 // Make sure the ":set" command doesn't show the new value in the
1670 // history.
1671 remove_key_from_history();
1672
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02001673 if (args->os_op != OP_NONE)
1674 // Don't allow set+=/-=/^= as they can allow for substring guessing
1675 return e_invalid_argument;
1676
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001677 if (STRCMP(curbuf->b_p_key, args->os_oldval.string) != 0)
1678 {
1679 // Need to update the swapfile.
1680 ml_set_crypt_key(curbuf, args->os_oldval.string,
1681 *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
1682 changed_internal();
1683 }
Christian Brabandt19e6c4f2023-06-27 18:57:10 +01001684# ifdef FEAT_SODIUM
1685 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
1686 crypt_sodium_lock_key(args->os_newval.string);
1687# endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001688
1689 return NULL;
1690}
1691
1692/*
1693 * The 'cryptmethod' option is changed.
1694 */
1695 char *
1696did_set_cryptmethod(optset_T *args)
1697{
1698 char_u *p;
1699 char_u *s;
1700
1701 if (args->os_flags & OPT_LOCAL)
1702 p = curbuf->b_p_cm;
1703 else
1704 p = p_cm;
1705 if (check_opt_strings(p, p_cm_values, TRUE) != OK)
1706 return e_invalid_argument;
1707 else if (crypt_self_test() == FAIL)
1708 return e_invalid_argument;
1709
1710 // When setting the global value to empty, make it "zip".
1711 if (*p_cm == NUL)
1712 {
1713 free_string_option(p_cm);
1714 p_cm = vim_strsave((char_u *)"zip");
1715 }
1716 // When using ":set cm=name" the local value is going to be empty.
1717 // Do that here, otherwise the crypt functions will still use the
1718 // local value.
1719 if ((args->os_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1720 {
1721 free_string_option(curbuf->b_p_cm);
1722 curbuf->b_p_cm = empty_option;
1723 }
1724
1725 // Need to update the swapfile when the effective method changed.
1726 // Set "s" to the effective old value, "p" to the effective new
1727 // method and compare.
1728 if ((args->os_flags & OPT_LOCAL) && *args->os_oldval.string == NUL)
1729 s = p_cm; // was previously using the global value
1730 else
1731 s = args->os_oldval.string;
1732 if (*curbuf->b_p_cm == NUL)
1733 p = p_cm; // is now using the global value
1734 else
1735 p = curbuf->b_p_cm;
1736 if (STRCMP(s, p) != 0)
1737 ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1738
1739 // If the global value changes need to update the swapfile for all
1740 // buffers using that value.
1741 if ((args->os_flags & OPT_GLOBAL)
1742 && STRCMP(p_cm, args->os_oldval.string) != 0)
1743 {
1744 buf_T *buf;
1745
1746 FOR_ALL_BUFFERS(buf)
1747 if (buf != curbuf && *buf->b_p_cm == NUL)
1748 ml_set_crypt_key(buf, buf->b_p_key, args->os_oldval.string);
1749 }
1750 return NULL;
1751}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001752
1753 int
1754expand_set_cryptmethod(optexpand_T *args, int *numMatches, char_u ***matches)
1755{
1756 return expand_set_opt_string(
1757 args,
1758 p_cm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001759 ARRAY_LENGTH(p_cm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001760 numMatches,
1761 matches);
1762}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001763#endif
1764
1765#if (defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1766/*
1767 * The 'cscopequickfix' option is changed.
1768 */
1769 char *
1770did_set_cscopequickfix(optset_T *args UNUSED)
1771{
1772 char_u *p;
1773
1774 if (p_csqf == NULL)
1775 return NULL;
1776
1777 p = p_csqf;
1778 while (*p != NUL)
1779 {
1780 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
1781 || p[1] == NUL
1782 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
1783 || (p[2] != NUL && p[2] != ','))
1784 return e_invalid_argument;
1785 else if (p[2] == NUL)
1786 break;
1787 else
1788 p += 3;
1789 }
1790
1791 return NULL;
1792}
1793#endif
1794
1795#if defined(FEAT_SYN_HL) || defined(PROTO)
1796/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001797 * The 'cursorlineopt' option is changed.
1798 */
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001799 char *
1800did_set_cursorlineopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001801{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001802 char_u **varp = (char_u **)args->os_varp;
1803
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001804 // This could be changed to use opt_strings_flags() instead.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001805 if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001806 return e_invalid_argument;
1807
1808 return NULL;
1809}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001810
1811 int
1812expand_set_cursorlineopt(optexpand_T *args, int *numMatches, char_u ***matches)
1813{
1814 return expand_set_opt_string(
1815 args,
1816 p_culopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001817 ARRAY_LENGTH(p_culopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001818 numMatches,
1819 matches);
1820}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001821#endif
1822
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001823/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001824 * The 'debug' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001825 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001826 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001827did_set_debug(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001828{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001829 return did_set_opt_strings(p_debug, p_debug_values, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001830}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001831
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001832 int
1833expand_set_debug(optexpand_T *args, int *numMatches, char_u ***matches)
1834{
1835 return expand_set_opt_string(
1836 args,
1837 p_debug_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001838 ARRAY_LENGTH(p_debug_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001839 numMatches,
1840 matches);
1841}
1842
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001843#if defined(FEAT_DIFF) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001844/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001845 * The 'diffopt' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001846 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001847 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001848did_set_diffopt(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001849{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001850 if (diffopt_changed() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001851 return e_invalid_argument;
1852
1853 return NULL;
1854}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001855
1856 int
1857expand_set_diffopt(optexpand_T *args, int *numMatches, char_u ***matches)
1858{
1859 expand_T *xp = args->oe_xp;
1860
1861 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
1862 {
1863 // Within "algorithm:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001864 int algo_len = (int)STRLEN("algorithm:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001865 if (xp->xp_pattern - args->oe_set_arg >= algo_len &&
1866 STRNCMP(xp->xp_pattern - algo_len, "algorithm:", algo_len) == 0)
1867 {
1868 return expand_set_opt_string(
1869 args,
1870 p_dip_algorithm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001871 ARRAY_LENGTH(p_dip_algorithm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001872 numMatches,
1873 matches);
1874 }
1875 return FAIL;
1876 }
1877
1878 return expand_set_opt_string(
1879 args,
1880 p_dip_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001881 ARRAY_LENGTH(p_dip_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001882 numMatches,
1883 matches);
1884}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001885#endif
1886
1887/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001888 * The 'display' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001889 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001890 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001891did_set_display(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001892{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001893 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001894 return e_invalid_argument;
1895
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001896 (void)init_chartab();
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001897 return NULL;
1898}
1899
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001900 int
1901expand_set_display(optexpand_T *args, int *numMatches, char_u ***matches)
1902{
1903 return expand_set_opt_string(
1904 args,
1905 p_dy_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001906 ARRAY_LENGTH(p_dy_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001907 numMatches,
1908 matches);
1909}
1910
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001911/*
1912 * The 'eadirection' option is changed.
1913 */
1914 char *
1915did_set_eadirection(optset_T *args UNUSED)
1916{
1917 return did_set_opt_strings(p_ead, p_ead_values, FALSE);
1918}
1919
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001920 int
1921expand_set_eadirection(optexpand_T *args, int *numMatches, char_u ***matches)
1922{
1923 return expand_set_opt_string(
1924 args,
1925 p_ead_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001926 ARRAY_LENGTH(p_ead_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001927 numMatches,
1928 matches);
1929}
1930
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001931/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001932 * One of the 'encoding', 'fileencoding', 'termencoding' or 'makeencoding'
1933 * options is changed.
1934 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001935 char *
1936did_set_encoding(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001937{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001938 char_u **varp = (char_u **)args->os_varp;
1939 char_u **gvarp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001940 char *errmsg = NULL;
1941 char_u *p;
1942
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001943 // Get the global option to compare with, otherwise we would have to check
1944 // two values for all local options.
1945 gvarp = (char_u **)get_option_varp_scope(args->os_idx, OPT_GLOBAL);
1946
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001947 if (gvarp == &p_fenc)
1948 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001949 if (!curbuf->b_p_ma && args->os_flags != OPT_GLOBAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001950 errmsg = e_cannot_make_changes_modifiable_is_off;
1951 else if (vim_strchr(*varp, ',') != NULL)
1952 // No comma allowed in 'fileencoding'; catches confusing it
1953 // with 'fileencodings'.
1954 errmsg = e_invalid_argument;
1955 else
1956 {
1957 // May show a "+" in the title now.
1958 redraw_titles();
1959 // Add 'fileencoding' to the swap file.
1960 ml_setflags(curbuf);
1961 }
1962 }
1963 if (errmsg == NULL)
1964 {
1965 // canonize the value, so that STRCMP() can be used on it
1966 p = enc_canonize(*varp);
1967 if (p != NULL)
1968 {
1969 vim_free(*varp);
1970 *varp = p;
1971 }
1972 if (varp == &p_enc)
1973 {
1974 errmsg = mb_init();
1975 redraw_titles();
1976 }
1977 }
1978
1979#if defined(FEAT_GUI_GTK)
1980 if (errmsg == NULL && varp == &p_tenc && gui.in_use)
1981 {
1982 // GTK uses only a single encoding, and that is UTF-8.
1983 if (STRCMP(p_tenc, "utf-8") != 0)
1984 errmsg = e_cannot_be_changed_in_gtk_GUI;
1985 }
1986#endif
1987
1988 if (errmsg == NULL)
1989 {
1990#ifdef FEAT_KEYMAP
1991 // When 'keymap' is used and 'encoding' changes, reload the keymap
1992 // (with another encoding).
1993 if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
1994 (void)keymap_init();
1995#endif
1996
1997 // When 'termencoding' is not empty and 'encoding' changes or when
1998 // 'termencoding' changes, need to setup for keyboard input and
1999 // display output conversion.
2000 if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
2001 {
2002 if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
2003 || convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
2004 {
2005 semsg(_(e_cannot_convert_between_str_and_str),
2006 p_tenc, p_enc);
2007 errmsg = e_invalid_argument;
2008 }
2009 }
2010
2011#if defined(MSWIN)
K.Takatace3189d2023-02-15 19:13:43 +00002012 // $HOME, $VIM and $VIMRUNTIME may have characters in active code page.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002013 if (varp == &p_enc)
K.Takatace3189d2023-02-15 19:13:43 +00002014 {
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002015 init_homedir();
K.Takatace3189d2023-02-15 19:13:43 +00002016 init_vimdir();
2017 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002018#endif
2019 }
2020
2021 return errmsg;
2022}
2023
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002024 int
2025expand_set_encoding(optexpand_T *args, int *numMatches, char_u ***matches)
2026{
2027 return expand_set_opt_generic(
2028 args,
2029 get_encoding_name,
2030 numMatches,
2031 matches);
2032}
2033
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002034/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002035 * The 'eventignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002036 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002037 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002038did_set_eventignore(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002039{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002040 if (check_ei() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002041 return e_invalid_argument;
2042 return NULL;
2043}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002044
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002045 static char_u *
2046get_eventignore_name(expand_T *xp, int idx)
2047{
2048 // 'eventignore' allows special keyword "all" in addition to
2049 // all event names.
2050 if (idx == 0)
2051 return (char_u *)"all";
2052 return get_event_name_no_group(xp, idx - 1);
2053}
2054
2055 int
2056expand_set_eventignore(optexpand_T *args, int *numMatches, char_u ***matches)
2057{
2058 return expand_set_opt_generic(
2059 args,
2060 get_eventignore_name,
2061 numMatches,
2062 matches);
2063}
2064
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002065/*
2066 * The 'fileformat' option is changed.
2067 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002068 char *
2069did_set_fileformat(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002070{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002071 char_u **varp = (char_u **)args->os_varp;
2072
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002073 if (!curbuf->b_p_ma && !(args->os_flags & OPT_GLOBAL))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002074 return e_cannot_make_changes_modifiable_is_off;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002075 else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002076 return e_invalid_argument;
2077
2078 // may also change 'textmode'
2079 if (get_fileformat(curbuf) == EOL_DOS)
2080 curbuf->b_p_tx = TRUE;
2081 else
2082 curbuf->b_p_tx = FALSE;
2083 redraw_titles();
2084 // update flag in swap file
2085 ml_setflags(curbuf);
2086 // Redraw needed when switching to/from "mac": a CR in the text
2087 // will be displayed differently.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002088 if (get_fileformat(curbuf) == EOL_MAC || *args->os_oldval.string == 'm')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002089 redraw_curbuf_later(UPD_NOT_VALID);
2090
2091 return NULL;
2092}
2093
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002094 int
2095expand_set_fileformat(optexpand_T *args, int *numMatches, char_u ***matches)
2096{
2097 return expand_set_opt_string(
2098 args,
2099 p_ff_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002100 ARRAY_LENGTH(p_ff_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002101 numMatches,
2102 matches);
2103}
2104
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002105/*
2106 * The 'fileformats' option is changed.
2107 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002108 char *
2109did_set_fileformats(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002110{
2111 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
2112 return e_invalid_argument;
2113
2114 // also change 'textauto'
2115 if (*p_ffs == NUL)
2116 p_ta = FALSE;
2117 else
2118 p_ta = TRUE;
2119
2120 return NULL;
2121}
2122
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002123/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002124 * The 'filetype' or the 'syntax' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002125 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002126 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002127did_set_filetype_or_syntax(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002128{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002129 char_u **varp = (char_u **)args->os_varp;
2130
2131 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002132 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002133
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002134 args->os_value_changed = STRCMP(args->os_oldval.string, *varp) != 0;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002135
2136 // Since we check the value, there is no need to set P_INSECURE,
2137 // even when the value comes from a modeline.
2138 args->os_value_checked = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002139
2140 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002141}
2142
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002143#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002144/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002145 * The 'foldclose' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002146 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002147 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002148did_set_foldclose(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002149{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002150 return did_set_opt_strings(p_fcl, p_fcl_values, TRUE);
2151}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002152
2153 int
2154expand_set_foldclose(optexpand_T *args, int *numMatches, char_u ***matches)
2155{
2156 return expand_set_opt_string(
2157 args,
2158 p_fcl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002159 ARRAY_LENGTH(p_fcl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002160 numMatches,
2161 matches);
2162}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002163#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002164
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002165#if (defined(FEAT_EVAL) && defined(FEAT_FOLDING)) || defined(PROTO)
2166/*
2167 * The 'foldexpr' option is changed.
2168 */
2169 char *
2170did_set_foldexpr(optset_T *args)
2171{
2172 (void)did_set_optexpr(args);
2173 if (foldmethodIsExpr(curwin))
2174 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002175 return NULL;
2176}
2177#endif
2178
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002179#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002180/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002181 * The 'foldignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002182 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002183 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002184did_set_foldignore(optset_T *args UNUSED)
2185{
2186 if (foldmethodIsIndent(curwin))
2187 foldUpdateAll(curwin);
2188 return NULL;
2189}
2190
2191/*
2192 * The 'foldmarker' option is changed.
2193 */
2194 char *
2195did_set_foldmarker(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002196{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002197 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002198 char_u *p;
2199
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002200 p = vim_strchr(*varp, ',');
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002201 if (p == NULL)
2202 return e_comma_required;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002203 else if (p == *varp || p[1] == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002204 return e_invalid_argument;
2205 else if (foldmethodIsMarker(curwin))
2206 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002207
2208 return NULL;
2209}
2210
2211/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002212 * The 'foldmethod' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002213 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002214 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002215did_set_foldmethod(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002216{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002217 char_u **varp = (char_u **)args->os_varp;
2218
2219 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002220 || *curwin->w_p_fdm == NUL)
2221 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002222
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002223 foldUpdateAll(curwin);
2224 if (foldmethodIsDiff(curwin))
2225 newFoldLevel();
2226 return NULL;
2227}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002228
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002229 int
2230expand_set_foldmethod(optexpand_T *args, int *numMatches, char_u ***matches)
2231{
2232 return expand_set_opt_string(
2233 args,
2234 p_fdm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002235 ARRAY_LENGTH(p_fdm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002236 numMatches,
2237 matches);
2238}
2239
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002240/*
2241 * The 'foldopen' option is changed.
2242 */
2243 char *
2244did_set_foldopen(optset_T *args UNUSED)
2245{
2246 return did_set_opt_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
2247}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002248
2249 int
2250expand_set_foldopen(optexpand_T *args, int *numMatches, char_u ***matches)
2251{
2252 return expand_set_opt_string(
2253 args,
2254 p_fdo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002255 ARRAY_LENGTH(p_fdo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002256 numMatches,
2257 matches);
2258}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002259#endif
2260
2261/*
2262 * The 'formatoptions' option is changed.
2263 */
2264 char *
2265did_set_formatoptions(optset_T *args)
2266{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002267 char_u **varp = (char_u **)args->os_varp;
2268
2269 return did_set_option_listflag(*varp, (char_u *)FO_ALL, args->os_errbuf);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002270}
2271
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002272 int
2273expand_set_formatoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2274{
2275 return expand_set_opt_listflag(args, (char_u*)FO_ALL, numMatches, matches);
2276}
2277
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002278#if defined(CURSOR_SHAPE) || defined(PROTO)
2279/*
2280 * The 'guicursor' option is changed.
2281 */
2282 char *
2283did_set_guicursor(optset_T *args UNUSED)
2284{
2285 return parse_shape_opt(SHAPE_CURSOR);
2286}
2287#endif
2288
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002289#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002290/*
2291 * The 'guifont' option is changed.
2292 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002293 char *
2294did_set_guifont(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002295{
2296 char_u *p;
2297 char *errmsg = NULL;
2298
2299 if (gui.in_use)
2300 {
2301 p = p_guifont;
2302# if defined(FEAT_GUI_GTK)
2303 // Put up a font dialog and let the user select a new value.
2304 // If this is cancelled go back to the old value but don't
2305 // give an error message.
2306 if (STRCMP(p, "*") == 0)
2307 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002308 p = gui_mch_font_dialog(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002309 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002310 p_guifont = (p != NULL) ? p : vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002311 }
2312# endif
2313 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
2314 {
2315# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
2316 if (STRCMP(p_guifont, "*") == 0)
2317 {
2318 // Dialog was cancelled: Keep the old value without giving
2319 // an error message.
2320 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002321 p_guifont = vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002322 }
2323 else
2324# endif
2325 errmsg = e_invalid_fonts;
2326 }
2327 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002328
2329 return errmsg;
2330}
2331
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002332/*
2333 * Expand the 'guifont' option. Only when GUI is being used. Each platform has
2334 * specific behaviors.
2335 */
2336 int
2337expand_set_guifont(optexpand_T *args, int *numMatches, char_u ***matches)
2338{
2339 if (!gui.in_use)
2340 return FAIL;
2341
2342# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
2343 char_u **varp = (char_u **)args->oe_varp;
2344 int wide = (varp == &p_guifontwide);
2345
2346 return expand_set_opt_callback(
2347 args, gui_mch_expand_font, &wide, numMatches, matches);
2348# else
2349 return FAIL;
2350# endif
2351}
2352
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002353# if defined(FEAT_XFONTSET) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002354/*
2355 * The 'guifontset' option is changed.
2356 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002357 char *
2358did_set_guifontset(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002359{
2360 char *errmsg = NULL;
2361
2362 if (STRCMP(p_guifontset, "*") == 0)
2363 errmsg = e_cant_select_fontset;
2364 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
2365 errmsg = e_invalid_fontset;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002366
2367 return errmsg;
2368}
2369# endif
2370
2371/*
2372 * The 'guifontwide' option is changed.
2373 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002374 char *
2375did_set_guifontwide(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002376{
2377 char *errmsg = NULL;
2378
2379 if (STRCMP(p_guifontwide, "*") == 0)
2380 errmsg = e_cant_select_wide_font;
2381 else if (gui_get_wide_font() == FAIL)
2382 errmsg = e_invalid_wide_font;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002383
2384 return errmsg;
2385}
2386#endif
2387
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002388#if defined(FEAT_GUI_GTK) || defined(PROTO)
2389/*
2390 * The 'guiligatures' option is changed.
2391 */
2392 char *
2393did_set_guiligatures(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002394{
2395 gui_set_ligatures();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002396 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002397}
2398#endif
2399
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002400#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002401/*
2402 * The 'guioptions' option is changed.
2403 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002404 char *
2405did_set_guioptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002406{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002407 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002408 char *errmsg;
2409
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002410 errmsg = did_set_option_listflag(*varp, (char_u *)GO_ALL, args->os_errbuf);
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002411 if (errmsg != NULL)
2412 return errmsg;
2413
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002414 gui_init_which_components(args->os_oldval.string);
2415 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002416}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002417
2418 int
2419expand_set_guioptions(optexpand_T *args, int *numMatches, char_u ***matches)
2420{
2421 return expand_set_opt_listflag(args, (char_u*)GO_ALL, numMatches, matches);
2422}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002423#endif
2424
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002425#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002426/*
2427 * The 'guitablabel' option is changed.
2428 */
2429 char *
2430did_set_guitablabel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002431{
2432 redraw_tabline = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002433 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002434}
2435#endif
2436
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002437/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002438 * The 'helpfile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002439 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002440 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002441did_set_helpfile(optset_T *args UNUSED)
2442{
2443 // May compute new values for $VIM and $VIMRUNTIME
2444 if (didset_vim)
2445 vim_unsetenv_ext((char_u *)"VIM");
2446 if (didset_vimruntime)
2447 vim_unsetenv_ext((char_u *)"VIMRUNTIME");
2448 return NULL;
2449}
2450
2451#if defined(FEAT_MULTI_LANG) || defined(PROTO)
2452/*
2453 * The 'helplang' option is changed.
2454 */
2455 char *
2456did_set_helplang(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002457{
2458 char *errmsg = NULL;
2459
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002460 // Check for "", "ab", "ab,cd", etc.
2461 for (char_u *s = p_hlg; *s != NUL; s += 3)
2462 {
2463 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
2464 {
2465 errmsg = e_invalid_argument;
2466 break;
2467 }
2468 if (s[2] == NUL)
2469 break;
2470 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002471
2472 return errmsg;
2473}
2474#endif
2475
2476/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002477 * The 'highlight' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002478 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002479 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002480did_set_highlight(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002481{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002482 if (highlight_changed() == FAIL)
2483 return e_invalid_argument; // invalid flags
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002484
2485 return NULL;
2486}
2487
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002488/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002489 * Expand 'highlight' option.
2490 */
2491 int
2492expand_set_highlight(optexpand_T *args, int *numMatches, char_u ***matches)
2493{
2494 char_u *p;
2495 expand_T *xp = args->oe_xp;
2496 static char_u hl_flags[HLF_COUNT] = HL_FLAGS;
Christian Brabandt3f168ec2023-10-02 23:21:11 +02002497 size_t i;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002498 int count = 0;
2499
2500 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2501 {
2502 // Right after a ':', meaning we just return all highlight names.
2503 return expand_set_opt_generic(
2504 args,
2505 get_highlight_name,
2506 numMatches,
2507 matches);
2508 }
2509
2510 if (*xp->xp_pattern == NUL)
2511 {
2512 // At beginning of a comma-separated list. Return the specific list of
2513 // supported occasions.
2514 *matches = ALLOC_MULT(char_u *, HLF_COUNT + 1);
2515 if (*matches == NULL)
2516 return FAIL;
2517
2518 // We still want to return the full option if it's requested.
2519 if (args->oe_include_orig_val)
2520 {
2521 p = vim_strsave(args->oe_opt_value);
2522 if (p == NULL)
2523 {
2524 VIM_CLEAR(*matches);
2525 return FAIL;
2526 }
2527 (*matches)[count++] = p;
2528 }
2529
2530 for (i = 0; i < HLF_COUNT; i++)
2531 {
2532 p = vim_strnsave(&hl_flags[i], 1);
2533 if (p == NULL)
2534 {
2535 if (count == 0)
2536 {
2537 VIM_CLEAR(*matches);
2538 return FAIL;
2539 }
2540 else
2541 break;
2542 }
2543 (*matches)[count++] = p;
2544 }
2545
2546 if (count == 0)
2547 {
2548 VIM_CLEAR(*matches);
2549 return FAIL;
2550 }
2551 *numMatches = count;
2552 return OK;
2553 }
2554
2555 // We are after the initial character (which indicates the occasion). We
2556 // already made sure we are not matching after a ':' above, so now we want
2557 // to match against display mode modifiers.
2558 // Since the xp_pattern starts from the beginning, we need to include it in
2559 // the returned match.
2560
2561 // Note: Keep this in sync with highlight_changed()
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002562 static char_u p_hl_mode_values[] =
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002563 {':', 'b', 'i', '-', 'n', 'r', 's', 'u', 'c', '2', 'd', '=', 't'};
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002564 size_t num_hl_modes = ARRAY_LENGTH(p_hl_mode_values);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002565
2566 *matches = ALLOC_MULT(char_u *, num_hl_modes);
2567 if (*matches == NULL)
2568 return FAIL;
2569
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002570 size_t pattern_len = STRLEN(xp->xp_pattern);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002571
2572 for (i = 0; i < num_hl_modes; i++)
2573 {
2574 // Don't allow duplicates as these are unique flags
2575 if (vim_strchr(xp->xp_pattern + 1, p_hl_mode_values[i]) != NULL)
2576 continue;
2577
2578 // ':' only works by itself, not with other flags.
2579 if (pattern_len > 1 && p_hl_mode_values[i] == ':')
2580 continue;
2581
2582 p = vim_strnsave(xp->xp_pattern, pattern_len + 1);
2583 if (p == NULL)
2584 {
2585 if (i == 0)
2586 {
2587 VIM_CLEAR(*matches);
2588 return FAIL;
2589 }
2590 else
2591 break;
2592 }
2593 p[pattern_len] = p_hl_mode_values[i];
2594 p[pattern_len + 1] = NUL;
2595 (*matches)[count++] = p;
2596 }
2597 if (count == 0)
2598 {
2599 VIM_CLEAR(*matches);
2600 return FAIL;
2601 }
2602 *numMatches = count;
2603
2604 return OK;
2605}
2606
2607/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002608 * The 'titlestring' or the 'iconstring' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002609 */
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002610 static char *
2611parse_titleiconstring(optset_T *args UNUSED, int flagval UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002612{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002613#ifdef FEAT_STL_OPT
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002614 char_u **varp = (char_u **)args->os_varp;
2615
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002616 // NULL => statusline syntax
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002617 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002618 stl_syntax |= flagval;
2619 else
2620 stl_syntax &= ~flagval;
2621#endif
2622 did_set_title();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002623
2624 return NULL;
2625}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002626
2627/*
2628 * The 'iconstring' option is changed.
2629 */
2630 char *
2631did_set_iconstring(optset_T *args)
2632{
2633 int flagval = 0;
2634
2635#ifdef FEAT_STL_OPT
2636 flagval = STL_IN_ICON;
2637#endif
2638
2639 return parse_titleiconstring(args, flagval);
2640}
2641
2642#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
2643/*
2644 * The 'imactivatekey' option is changed.
2645 */
2646 char *
2647did_set_imactivatekey(optset_T *args UNUSED)
2648{
2649 if (!im_xim_isvalid_imactivate())
2650 return e_invalid_argument;
2651 return NULL;
2652}
2653#endif
2654
2655/*
2656 * The 'isident' or the 'iskeyword' or the 'isprint' or the 'isfname' option is
2657 * changed.
2658 */
2659 char *
2660did_set_isopt(optset_T *args)
2661{
2662 // 'isident', 'iskeyword', 'isprint or 'isfname' option: refill g_chartab[]
2663 // If the new option is invalid, use old value.
2664 // 'lisp' option: refill g_chartab[] for '-' char.
2665 if (init_chartab() == FAIL)
2666 {
2667 args->os_restore_chartab = TRUE;// need to restore the chartab.
2668 return e_invalid_argument; // error in value
2669 }
2670
2671 return NULL;
2672}
2673
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002674/*
2675 * The 'jumpoptions' option is changed.
2676 */
2677 char *
Yegappan Lakshmanan1926ae42023-09-21 16:36:28 +02002678did_set_jumpoptions(optset_T *args UNUSED)
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002679{
2680 if (opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE) != OK)
2681 return e_invalid_argument;
2682
2683 return NULL;
2684}
2685
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002686 int
2687expand_set_jumpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2688{
2689 return expand_set_opt_string(
2690 args,
2691 p_jop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002692 ARRAY_LENGTH(p_jop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002693 numMatches,
2694 matches);
2695}
2696
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002697#if defined(FEAT_KEYMAP) || defined(PROTO)
2698/*
2699 * The 'keymap' option is changed.
2700 */
2701 char *
2702did_set_keymap(optset_T *args)
2703{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002704 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002705 char *errmsg = NULL;
2706
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002707 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002708 errmsg = e_invalid_argument;
2709 else
2710 {
2711 int secure_save = secure;
2712
2713 // Reset the secure flag, since the value of 'keymap' has
2714 // been checked to be safe.
2715 secure = 0;
2716
2717 // load or unload key mapping tables
2718 errmsg = keymap_init();
2719
2720 secure = secure_save;
2721
2722 // Since we check the value, there is no need to set P_INSECURE,
2723 // even when the value comes from a modeline.
2724 args->os_value_checked = TRUE;
2725 }
2726
2727 if (errmsg == NULL)
2728 {
2729 if (*curbuf->b_p_keymap != NUL)
2730 {
2731 // Installed a new keymap, switch on using it.
2732 curbuf->b_p_iminsert = B_IMODE_LMAP;
2733 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
2734 curbuf->b_p_imsearch = B_IMODE_LMAP;
2735 }
2736 else
2737 {
2738 // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
2739 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
2740 curbuf->b_p_iminsert = B_IMODE_NONE;
2741 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
2742 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
2743 }
2744 if ((args->os_flags & OPT_LOCAL) == 0)
2745 {
2746 set_iminsert_global();
2747 set_imsearch_global();
2748 }
2749 status_redraw_curbuf();
2750 }
2751
2752 return errmsg;
2753}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002754#endif
2755
2756/*
2757 * The 'keymodel' option is changed.
2758 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002759 char *
2760did_set_keymodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002761{
2762 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
2763 return e_invalid_argument;
2764
2765 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
2766 km_startsel = (vim_strchr(p_km, 'a') != NULL);
2767 return NULL;
2768}
2769
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002770 int
2771expand_set_keymodel(optexpand_T *args, int *numMatches, char_u ***matches)
2772{
2773 return expand_set_opt_string(
2774 args,
2775 p_km_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002776 ARRAY_LENGTH(p_km_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002777 numMatches,
2778 matches);
2779}
2780
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002781/*
2782 * The 'keyprotocol' option is changed.
2783 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002784 char *
2785did_set_keyprotocol(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002786{
Gregory Anders3695d0e2023-09-29 20:17:20 +02002787 char_u *term = T_NAME;
2788 keyprot_T kpc = match_keyprotocol(term);
2789 if (kpc == KEYPROTOCOL_FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002790 return e_invalid_argument;
2791
Gregory Anders3695d0e2023-09-29 20:17:20 +02002792 apply_keyprotocol(term, kpc);
2793
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002794 return NULL;
2795}
2796
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002797 int
2798expand_set_keyprotocol(optexpand_T *args, int *numMatches, char_u ***matches)
2799{
2800 expand_T *xp = args->oe_xp;
2801 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2802 {
2803 // 'keyprotocol' only has well-defined terms for completion for the
2804 // protocol part after the colon.
2805 return expand_set_opt_string(
2806 args,
2807 p_kpc_protocol_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002808 ARRAY_LENGTH(p_kpc_protocol_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002809 numMatches,
2810 matches);
2811 }
2812 // Use expand_set_opt_string instead of returning FAIL so that we can
2813 // include the original value if args->oe_include_orig_val is set.
2814 static char *(empty[]) = {NULL};
2815 return expand_set_opt_string(args, empty, 0, numMatches, matches);
2816}
2817
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002818/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002819 * The 'lispoptions' option is changed.
2820 */
2821 char *
2822did_set_lispoptions(optset_T *args)
2823{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002824 char_u **varp = (char_u **)args->os_varp;
2825
2826 if (**varp != NUL
2827 && STRCMP(*varp, "expr:0") != 0
2828 && STRCMP(*varp, "expr:1") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002829 return e_invalid_argument;
2830
2831 return NULL;
2832}
2833
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002834 int
2835expand_set_lispoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2836{
2837 static char *(p_lop_values[]) = {"expr:0", "expr:1", NULL};
2838 return expand_set_opt_string(
2839 args,
2840 p_lop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002841 ARRAY_LENGTH(p_lop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002842 numMatches,
2843 matches);
2844}
2845
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002846/*
2847 * The 'matchpairs' option is changed.
2848 */
2849 char *
2850did_set_matchpairs(optset_T *args)
2851{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002852 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002853 char_u *p;
2854
2855 if (has_mbyte)
2856 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002857 for (p = *varp; *p != NUL; ++p)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002858 {
2859 int x2 = -1;
2860 int x3 = -1;
2861
2862 p += mb_ptr2len(p);
2863 if (*p != NUL)
2864 x2 = *p++;
2865 if (*p != NUL)
2866 {
2867 x3 = mb_ptr2char(p);
2868 p += mb_ptr2len(p);
2869 }
2870 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
2871 return e_invalid_argument;
2872 if (*p == NUL)
2873 break;
2874 }
2875 }
2876 else
2877 {
2878 // Check for "x:y,x:y"
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002879 for (p = *varp; *p != NUL; p += 4)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002880 {
2881 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
2882 return e_invalid_argument;
2883 if (p[3] == NUL)
2884 break;
2885 }
2886 }
2887
2888 return NULL;
2889}
2890
2891#if defined(FEAT_SPELL) || defined(PROTO)
2892/*
2893 * The 'mkspellmem' option is changed.
2894 */
2895 char *
2896did_set_mkspellmem(optset_T *args UNUSED)
2897{
2898 if (spell_check_msm() != OK)
2899 return e_invalid_argument;
2900
2901 return NULL;
2902}
2903#endif
2904
2905/*
2906 * The 'mouse' option is changed.
2907 */
2908 char *
2909did_set_mouse(optset_T *args)
2910{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002911 char_u **varp = (char_u **)args->os_varp;
2912
2913 return did_set_option_listflag(*varp, (char_u *)MOUSE_ALL,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002914 args->os_errbuf);
2915}
2916
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002917 int
2918expand_set_mouse(optexpand_T *args, int *numMatches, char_u ***matches)
2919{
2920 return expand_set_opt_listflag(args, (char_u*)MOUSE_ALL, numMatches, matches);
2921}
2922
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002923/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002924 * The 'mousemodel' option is changed.
2925 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002926 char *
2927did_set_mousemodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002928{
2929 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
2930 return e_invalid_argument;
2931#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
2932 else if (*p_mousem != *oldval)
2933 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
2934 // to create or delete the popup menus.
2935 gui_motif_update_mousemodel(root_menu);
2936#endif
2937
2938 return NULL;
2939}
2940
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002941 int
2942expand_set_mousemodel(optexpand_T *args, int *numMatches, char_u ***matches)
2943{
2944 return expand_set_opt_string(
2945 args,
2946 p_mousem_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002947 ARRAY_LENGTH(p_mousem_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002948 numMatches,
2949 matches);
2950}
2951
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002952#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
2953 char *
2954did_set_mouseshape(optset_T *args UNUSED)
2955{
2956 char *errmsg = NULL;
2957
2958 errmsg = parse_shape_opt(SHAPE_MOUSE);
2959 update_mouseshape(-1);
2960
2961 return errmsg;
2962}
2963#endif
2964
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002965/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002966 * The 'nrformats' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002967 */
2968 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002969did_set_nrformats(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002970{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002971 char_u **varp = (char_u **)args->os_varp;
2972
2973 return did_set_opt_strings(*varp, p_nf_values, TRUE);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002974}
2975
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002976 int
2977expand_set_nrformats(optexpand_T *args, int *numMatches, char_u ***matches)
2978{
2979 return expand_set_opt_string(
2980 args,
2981 p_nf_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002982 ARRAY_LENGTH(p_nf_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002983 numMatches,
2984 matches);
2985}
2986
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002987#if defined(FEAT_EVAL) || defined(PROTO)
2988/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002989 * One of the '*expr' options is changed: 'balloonexpr', 'diffexpr',
2990 * 'foldexpr', 'foldtext', 'formatexpr', 'includeexpr', 'indentexpr',
2991 * 'patchexpr', 'printexpr' and 'charconvert'.
2992 *
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002993 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002994 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002995did_set_optexpr(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002996{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002997 char_u **varp = (char_u **)args->os_varp;
2998
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002999 // If the option value starts with <SID> or s:, then replace that with
3000 // the script identifier.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003001 char_u *name = get_scriptlocal_funcname(*varp);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003002 if (name != NULL)
3003 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003004 free_string_option(*varp);
3005 *varp = name;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003006 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003007
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003008 return NULL;
3009}
3010#endif
3011
3012/*
3013 * The 'pastetoggle' option is changed.
3014 */
3015 char *
3016did_set_pastetoggle(optset_T *args UNUSED)
3017{
3018 char_u *p;
3019
3020 // translate key codes like in a mapping
3021 if (*p_pt)
3022 {
zeertzjq7e0bae02023-08-11 23:15:38 +02003023 (void)replace_termcodes(p_pt, &p, 0,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003024 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
3025 if (p != NULL)
3026 {
3027 free_string_option(p_pt);
3028 p_pt = p;
3029 }
3030 }
3031
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003032 return NULL;
3033}
3034
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003035#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3036/*
3037 * The 'previewpopup' option is changed.
3038 */
3039 char *
3040did_set_previewpopup(optset_T *args UNUSED)
3041{
3042 if (parse_previewpopup(NULL) == FAIL)
3043 return e_invalid_argument;
3044
3045 return NULL;
3046}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003047
3048 int
3049expand_set_popupoption(optexpand_T *args, int *numMatches, char_u ***matches)
3050{
3051 expand_T *xp = args->oe_xp;
3052
3053 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3054 {
3055 // Within "highlight:"/"border:"/"align:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003056 int border_len = (int)STRLEN("border:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003057 if (xp->xp_pattern - args->oe_set_arg >= border_len &&
3058 STRNCMP(xp->xp_pattern - border_len, "border:", border_len) == 0)
3059 {
3060 return expand_set_opt_string(
3061 args,
3062 p_popup_option_border_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003063 ARRAY_LENGTH(p_popup_option_border_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003064 numMatches,
3065 matches);
3066 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003067 int align_len = (int)STRLEN("align:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003068 if (xp->xp_pattern - args->oe_set_arg >= align_len &&
3069 STRNCMP(xp->xp_pattern - align_len, "align:", align_len) == 0)
3070 {
3071 return expand_set_opt_string(
3072 args,
3073 p_popup_option_align_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003074 ARRAY_LENGTH(p_popup_option_align_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003075 numMatches,
3076 matches);
3077 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003078 int highlight_len = (int)STRLEN("highlight:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003079 if (xp->xp_pattern - args->oe_set_arg >= highlight_len &&
3080 STRNCMP(xp->xp_pattern - highlight_len, "highlight:", highlight_len) == 0)
3081 {
3082 // Return the list of all highlight names
3083 return expand_set_opt_generic(
3084 args,
3085 get_highlight_name,
3086 numMatches,
3087 matches);
3088 }
3089 return FAIL;
3090 }
3091
3092 return expand_set_opt_string(
3093 args,
3094 p_popup_option_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003095 ARRAY_LENGTH(p_popup_option_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003096 numMatches,
3097 matches);
3098}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003099#endif
3100
3101#if defined(FEAT_POSTSCRIPT) || defined(PROTO)
3102/*
3103 * The 'printencoding' option is changed.
3104 */
3105 char *
3106did_set_printencoding(optset_T *args UNUSED)
3107{
3108 char_u *s, *p;
3109
3110 // Canonize 'printencoding' if VIM standard one
3111 p = enc_canonize(p_penc);
3112 if (p != NULL)
3113 {
3114 vim_free(p_penc);
3115 p_penc = p;
3116 }
3117 else
3118 {
3119 // Ensure lower case and '-' for '_'
3120 for (s = p_penc; *s != NUL; s++)
3121 {
3122 if (*s == '_')
3123 *s = '-';
3124 else
3125 *s = TOLOWER_ASC(*s);
3126 }
3127 }
3128
3129 return NULL;
3130}
3131#endif
3132
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003133#if defined(FEAT_PRINTER) || defined(PROTO)
3134
3135 static char_u *
3136get_printoptions_names(expand_T *xp UNUSED, int idx)
3137{
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003138 if (idx >= (int)ARRAY_LENGTH(printer_opts))
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003139 return NULL;
3140 return (char_u*)printer_opts[idx].name;
3141}
3142
3143/*
3144 * Expand 'printoptions' option
3145 */
3146 int
3147expand_set_printoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3148{
3149 return expand_set_opt_generic(
3150 args,
3151 get_printoptions_names,
3152 numMatches,
3153 matches);
3154}
3155#endif
3156
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003157#if defined(FEAT_STL_OPT) || defined(PROTO)
3158/*
3159 * The 'statusline' or the 'tabline' or the 'rulerformat' option is changed.
3160 * "rulerformat" is TRUE if the 'rulerformat' option is changed.
3161 */
3162 static char *
3163parse_statustabline_rulerformat(optset_T *args, int rulerformat)
3164{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003165 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003166 char_u *s;
3167 char *errmsg = NULL;
3168 int wid;
3169
3170 if (rulerformat) // reset ru_wid first
3171 ru_wid = 0;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003172 s = *varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003173 if (rulerformat && *s == '%')
3174 {
3175 // set ru_wid if 'ruf' starts with "%99("
3176 if (*++s == '-') // ignore a '-'
3177 s++;
3178 wid = getdigits(&s);
3179 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
3180 ru_wid = wid;
3181 else
3182 errmsg = check_stl_option(p_ruf);
3183 }
3184 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
3185 else if (rulerformat || s[0] != '%' || s[1] != '!')
3186 errmsg = check_stl_option(s);
3187 if (rulerformat && errmsg == NULL)
3188 comp_col();
3189
3190 return errmsg;
3191}
3192#endif
3193
3194#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
3195/*
3196 * The 'renderoptions' option is changed.
3197 */
3198 char *
3199did_set_renderoptions(optset_T *args UNUSED)
3200{
3201 if (!gui_mch_set_rendering_options(p_rop))
3202 return e_invalid_argument;
3203
3204 return NULL;
3205}
3206#endif
3207
3208#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3209/*
3210 * The 'rightleftcmd' option is changed.
3211 */
3212 char *
3213did_set_rightleftcmd(optset_T *args)
3214{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003215 char_u **varp = (char_u **)args->os_varp;
3216
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003217 // Currently only "search" is a supported value.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003218 if (**varp != NUL && STRCMP(*varp, "search") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003219 return e_invalid_argument;
3220
3221 return NULL;
3222}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003223
3224 int
3225expand_set_rightleftcmd(optexpand_T *args, int *numMatches, char_u ***matches)
3226{
3227 static char *(p_rlc_values[]) = {"search", NULL};
3228 return expand_set_opt_string(
3229 args,
3230 p_rlc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003231 ARRAY_LENGTH(p_rlc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003232 numMatches,
3233 matches);
3234}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003235#endif
3236
3237#if defined(FEAT_STL_OPT) || defined(PROTO)
3238/*
3239 * The 'rulerformat' option is changed.
3240 */
3241 char *
3242did_set_rulerformat(optset_T *args)
3243{
3244 return parse_statustabline_rulerformat(args, TRUE);
3245}
3246#endif
3247
3248/*
3249 * The 'scrollopt' option is changed.
3250 */
3251 char *
3252did_set_scrollopt(optset_T *args UNUSED)
3253{
3254 return did_set_opt_strings(p_sbo, p_scbopt_values, TRUE);
3255}
3256
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003257 int
3258expand_set_scrollopt(optexpand_T *args, int *numMatches, char_u ***matches)
3259{
3260 return expand_set_opt_string(
3261 args,
3262 p_scbopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003263 ARRAY_LENGTH(p_scbopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003264 numMatches,
3265 matches);
3266}
3267
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003268/*
3269 * The 'selection' option is changed.
3270 */
3271 char *
3272did_set_selection(optset_T *args UNUSED)
3273{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003274 if (*p_sel == NUL || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003275 return e_invalid_argument;
3276
3277 return NULL;
3278}
3279
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003280 int
3281expand_set_selection(optexpand_T *args, int *numMatches, char_u ***matches)
3282{
3283 return expand_set_opt_string(
3284 args,
3285 p_sel_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003286 ARRAY_LENGTH(p_sel_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003287 numMatches,
3288 matches);
3289}
3290
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003291/*
3292 * The 'selectmode' option is changed.
3293 */
3294 char *
3295did_set_selectmode(optset_T *args UNUSED)
3296{
3297 return did_set_opt_strings(p_slm, p_slm_values, TRUE);
3298}
3299
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003300 int
3301expand_set_selectmode(optexpand_T *args, int *numMatches, char_u ***matches)
3302{
3303 return expand_set_opt_string(
3304 args,
3305 p_slm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003306 ARRAY_LENGTH(p_slm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003307 numMatches,
3308 matches);
3309}
3310
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003311#if defined(FEAT_SESSION) || defined(PROTO)
3312/*
3313 * The 'sessionoptions' option is changed.
3314 */
3315 char *
3316did_set_sessionoptions(optset_T *args)
3317{
3318 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
3319 return e_invalid_argument;
3320 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
3321 {
3322 // Don't allow both "sesdir" and "curdir".
3323 (void)opt_strings_flags(args->os_oldval.string, p_ssop_values,
3324 &ssop_flags, TRUE);
3325 return e_invalid_argument;
3326 }
3327
3328 return NULL;
3329}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003330
3331 int
3332expand_set_sessionoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3333{
3334 return expand_set_opt_string(
3335 args,
3336 p_ssop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003337 ARRAY_LENGTH(p_ssop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003338 numMatches,
3339 matches);
3340}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003341#endif
3342
3343/*
3344 * The 'shortmess' option is changed.
3345 */
3346 char *
3347did_set_shortmess(optset_T *args)
3348{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003349 char_u **varp = (char_u **)args->os_varp;
3350
3351 return did_set_option_listflag(*varp, (char_u *)SHM_ALL, args->os_errbuf);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003352}
3353
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003354 int
3355expand_set_shortmess(optexpand_T *args, int *numMatches, char_u ***matches)
3356{
3357 return expand_set_opt_listflag(args, (char_u*)SHM_ALL, numMatches, matches);
3358}
3359
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003360#if defined(FEAT_LINEBREAK) || defined(PROTO)
3361/*
3362 * The 'showbreak' option is changed.
3363 */
3364 char *
3365did_set_showbreak(optset_T *args)
3366{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003367 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003368 char_u *s;
3369
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003370 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003371 {
3372 if (ptr2cells(s) != 1)
3373 return e_showbreak_contains_unprintable_or_wide_character;
3374 MB_PTR_ADV(s);
3375 }
3376
3377 return NULL;
3378}
3379#endif
3380
3381/*
3382 * The 'showcmdloc' option is changed.
3383 */
3384 char *
3385did_set_showcmdloc(optset_T *args UNUSED)
3386{
3387 return did_set_opt_strings(p_sloc, p_sloc_values, FALSE);
3388}
3389
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003390 int
3391expand_set_showcmdloc(optexpand_T *args, int *numMatches, char_u ***matches)
3392{
3393 return expand_set_opt_string(
3394 args,
3395 p_sloc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003396 ARRAY_LENGTH(p_sloc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003397 numMatches,
3398 matches);
3399}
3400
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003401#if defined(FEAT_SIGNS) || defined(PROTO)
3402/*
3403 * The 'signcolumn' option is changed.
3404 */
3405 char *
3406did_set_signcolumn(optset_T *args)
3407{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003408 char_u **varp = (char_u **)args->os_varp;
3409
3410 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003411 return e_invalid_argument;
3412 // When changing the 'signcolumn' to or from 'number', recompute the
3413 // width of the number column if 'number' or 'relativenumber' is set.
3414 if (((*args->os_oldval.string == 'n' && args->os_oldval.string[1] == 'u')
3415 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
3416 && (curwin->w_p_nu || curwin->w_p_rnu))
3417 curwin->w_nrwidth_line_count = 0;
3418
3419 return NULL;
3420}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003421
3422 int
3423expand_set_signcolumn(optexpand_T *args, int *numMatches, char_u ***matches)
3424{
3425 return expand_set_opt_string(
3426 args,
3427 p_scl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003428 ARRAY_LENGTH(p_scl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003429 numMatches,
3430 matches);
3431}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003432#endif
3433
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003434#if defined(FEAT_SPELL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003435/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003436 * The 'spellcapcheck' option is changed.
3437 */
3438 char *
3439did_set_spellcapcheck(optset_T *args UNUSED)
3440{
3441 // compile the regexp program.
3442 return compile_cap_prog(curwin->w_s);
3443}
3444
3445/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003446 * The 'spellfile' option is changed.
3447 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003448 char *
3449did_set_spellfile(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003450{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003451 char_u **varp = (char_u **)args->os_varp;
3452
3453 if (!valid_spellfile(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003454 return e_invalid_argument;
3455
3456 // If there is a window for this buffer in which 'spell' is set load the
3457 // wordlists.
3458 return did_set_spell_option(TRUE);
3459}
3460
3461/*
3462 * The 'spell' option is changed.
3463 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003464 char *
3465did_set_spelllang(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003466{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003467 char_u **varp = (char_u **)args->os_varp;
3468
3469 if (!valid_spelllang(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003470 return e_invalid_argument;
3471
3472 // If there is a window for this buffer in which 'spell' is set load the
3473 // wordlists.
3474 return did_set_spell_option(FALSE);
3475}
3476
3477/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003478 * The 'spelloptions' option is changed.
3479 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003480 char *
3481did_set_spelloptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003482{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003483 char_u **varp = (char_u **)args->os_varp;
3484
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003485 if (**varp != NUL && STRCMP(*varp, "camel") != 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003486 return e_invalid_argument;
3487
3488 return NULL;
3489}
3490
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003491 int
3492expand_set_spelloptions(optexpand_T *args, int *numMatches, char_u ***matches)
3493{
3494 static char *(p_spo_values[]) = {"camel", NULL};
3495 return expand_set_opt_string(
3496 args,
3497 p_spo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003498 ARRAY_LENGTH(p_spo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003499 numMatches,
3500 matches);
3501}
3502
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003503/*
3504 * The 'spellsuggest' option is changed.
3505 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003506 char *
3507did_set_spellsuggest(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003508{
3509 if (spell_check_sps() != OK)
3510 return e_invalid_argument;
3511
3512 return NULL;
3513}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003514
3515 int
3516expand_set_spellsuggest(optexpand_T *args, int *numMatches, char_u ***matches)
3517{
3518 return expand_set_opt_string(
3519 args,
3520 p_sps_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003521 ARRAY_LENGTH(p_sps_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003522 numMatches,
3523 matches);
3524}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003525#endif
3526
3527/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003528 * The 'splitkeep' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003529 */
3530 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003531did_set_splitkeep(optset_T *args UNUSED)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003532{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003533 return did_set_opt_strings(p_spk, p_spk_values, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003534}
3535
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003536 int
3537expand_set_splitkeep(optexpand_T *args, int *numMatches, char_u ***matches)
3538{
3539 return expand_set_opt_string(
3540 args,
3541 p_spk_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003542 ARRAY_LENGTH(p_spk_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003543 numMatches,
3544 matches);
3545}
3546
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00003547#if defined(FEAT_STL_OPT) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003548/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003549 * The 'statusline' option is changed.
3550 */
3551 char *
3552did_set_statusline(optset_T *args)
3553{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003554 return parse_statustabline_rulerformat(args, FALSE);
3555}
3556#endif
3557
3558/*
3559 * The 'swapsync' option is changed.
3560 */
3561 char *
3562did_set_swapsync(optset_T *args UNUSED)
3563{
3564 return did_set_opt_strings(p_sws, p_sws_values, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003565}
3566
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003567 int
3568expand_set_swapsync(optexpand_T *args, int *numMatches, char_u ***matches)
3569{
3570 return expand_set_opt_string(
3571 args,
3572 p_sws_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003573 ARRAY_LENGTH(p_sws_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003574 numMatches,
3575 matches);
3576}
3577
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003578/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003579 * The 'switchbuf' option is changed.
3580 */
3581 char *
3582did_set_switchbuf(optset_T *args UNUSED)
3583{
3584 return did_set_opt_flags(p_swb, p_swb_values, &swb_flags, TRUE);
3585}
3586
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003587 int
3588expand_set_switchbuf(optexpand_T *args, int *numMatches, char_u ***matches)
3589{
3590 return expand_set_opt_string(
3591 args,
3592 p_swb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003593 ARRAY_LENGTH(p_swb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003594 numMatches,
3595 matches);
3596}
3597
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003598#if defined(FEAT_STL_OPT) || defined(PROTO)
3599/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003600 * The 'tabline' option is changed.
3601 */
3602 char *
3603did_set_tabline(optset_T *args)
3604{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003605 return parse_statustabline_rulerformat(args, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003606}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003607#endif
3608
3609/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003610 * The 'tagcase' option is changed.
3611 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003612 char *
3613did_set_tagcase(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003614{
3615 unsigned int *flags;
3616 char_u *p;
3617
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003618 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003619 {
3620 p = curbuf->b_p_tc;
3621 flags = &curbuf->b_tc_flags;
3622 }
3623 else
3624 {
3625 p = p_tc;
3626 flags = &tc_flags;
3627 }
3628
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003629 if ((args->os_flags & OPT_LOCAL) && *p == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003630 // make the local value empty: use the global value
3631 *flags = 0;
3632 else if (*p == NUL
3633 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
3634 return e_invalid_argument;
3635
3636 return NULL;
3637}
3638
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003639 int
3640expand_set_tagcase(optexpand_T *args, int *numMatches, char_u ***matches)
3641{
3642 return expand_set_opt_string(
3643 args,
3644 p_tc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003645 ARRAY_LENGTH(p_tc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003646 numMatches,
3647 matches);
3648}
3649
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003650/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003651 * The 'term' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003652 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003653 char *
3654did_set_term(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003655{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003656 if (T_NAME[0] == NUL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003657 return e_cannot_set_term_to_empty_string;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003658#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003659 if (gui.in_use)
3660 return e_cannot_change_term_in_GUI;
3661 if (term_is_gui(T_NAME))
3662 return e_use_gui_to_start_GUI;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003663#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003664 if (set_termname(T_NAME) == FAIL)
3665 return e_not_found_in_termcap;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003666
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003667 // Screen colors may have changed.
3668 redraw_later_clear();
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003669
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003670 return NULL;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003671}
3672
3673/*
3674 * Some terminal option (t_xxx) is changed
3675 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003676 char *
3677did_set_term_option(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003678{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003679 char_u **varp = (char_u **)args->os_varp;
3680
3681 if (!full_screen)
3682 return NULL;
3683
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003684 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
3685 if (varp == &T_CCO)
3686 {
3687 int colors = atoi((char *)T_CCO);
3688
3689 // Only reinitialize colors if t_Co value has really changed to
3690 // avoid expensive reload of colorscheme if t_Co is set to the
3691 // same value multiple times.
3692 if (colors != t_colors)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003693 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003694 t_colors = colors;
3695 if (t_colors <= 1)
3696 {
3697 vim_free(T_CCO);
3698 T_CCO = empty_option;
3699 }
3700#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3701 if (is_term_win32())
3702 {
3703 swap_tcap();
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003704 args->os_did_swaptcap = TRUE;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003705 }
3706#endif
3707 // We now have a different color setup, initialize it again.
3708 init_highlight(TRUE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003709 }
3710 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003711 ttest(FALSE);
3712 if (varp == &T_ME)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003713 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003714 out_str(T_ME);
3715 redraw_later(UPD_CLEAR);
3716#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3717 // Since t_me has been set, this probably means that the user
3718 // wants to use this as default colors. Need to reset default
3719 // background/foreground colors.
3720# ifdef VIMDLL
3721 if (!gui.in_use && !gui.starting)
3722# endif
3723 mch_set_normal_colors();
3724#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003725 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003726 if (varp == &T_BE && termcap_active)
3727 {
3728 MAY_WANT_TO_LOG_THIS;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003729
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003730 if (*T_BE == NUL)
3731 // When clearing t_BE we assume the user no longer wants
3732 // bracketed paste, thus disable it by writing t_BD.
3733 out_str(T_BD);
3734 else
3735 out_str(T_BE);
3736 }
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003737
3738 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003739}
3740
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003741#if defined(FEAT_TERMINAL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003742/*
3743 * The 'termwinkey' option is changed.
3744 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003745 char *
3746did_set_termwinkey(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003747{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003748 if (*curwin->w_p_twk != NUL && string_to_key(curwin->w_p_twk, TRUE) == 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003749 return e_invalid_argument;
3750
3751 return NULL;
3752}
3753
3754/*
3755 * The 'termwinsize' option is changed.
3756 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003757 char *
3758did_set_termwinsize(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003759{
3760 char_u *p;
3761
3762 if (*curwin->w_p_tws == NUL)
3763 return NULL;
3764
3765 p = skipdigits(curwin->w_p_tws);
3766 if (p == curwin->w_p_tws
3767 || (*p != 'x' && *p != '*')
3768 || *skipdigits(p + 1) != NUL)
3769 return e_invalid_argument;
3770
3771 return NULL;
3772}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003773
3774# if defined(MSWIN) || defined(PROTO)
3775/*
3776 * The 'termwintype' option is changed.
3777 */
3778 char *
3779did_set_termwintype(optset_T *args UNUSED)
3780{
3781 return did_set_opt_strings(p_twt, p_twt_values, FALSE);
3782}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003783
3784 int
3785expand_set_termwintype(optexpand_T *args, int *numMatches, char_u ***matches)
3786{
3787 return expand_set_opt_string(
3788 args,
3789 p_twt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003790 ARRAY_LENGTH(p_twt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003791 numMatches,
3792 matches);
3793}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003794# endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003795#endif
3796
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003797/*
3798 * The 'titlestring' option is changed.
3799 */
3800 char *
3801did_set_titlestring(optset_T *args)
3802{
3803 int flagval = 0;
3804
3805#ifdef FEAT_STL_OPT
3806 flagval = STL_IN_TITLE;
3807#endif
3808 return parse_titleiconstring(args, flagval);
3809}
3810
3811#if (defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)) || defined(PROTO)
3812/*
3813 * The 'toolbar' option is changed.
3814 */
3815 char *
3816did_set_toolbar(optset_T *args UNUSED)
3817{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003818 if (opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags,
3819 TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003820 return e_invalid_argument;
3821
3822 out_flush();
3823 gui_mch_show_toolbar((toolbar_flags &
3824 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
3825 return NULL;
3826}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003827
3828 int
3829expand_set_toolbar(optexpand_T *args, int *numMatches, char_u ***matches)
3830{
3831 return expand_set_opt_string(
3832 args,
3833 p_toolbar_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003834 ARRAY_LENGTH(p_toolbar_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003835 numMatches,
3836 matches);
3837}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003838#endif
3839
3840#if (defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)) || defined(PROTO)
3841/*
3842 * The 'toolbariconsize' option is changed. GTK+ 2 only.
3843 */
3844 char *
3845did_set_toolbariconsize(optset_T *args UNUSED)
3846{
3847 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
3848 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_toolbariconsize(optexpand_T *args, int *numMatches, char_u ***matches)
3858{
3859 return expand_set_opt_string(
3860 args,
3861 p_tbis_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003862 ARRAY_LENGTH(p_tbis_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(UNIX) || defined(VMS) || defined(PROTO)
3869/*
3870 * The 'ttymouse' option is changed.
3871 */
3872 char *
3873did_set_ttymouse(optset_T *args UNUSED)
3874{
3875 char *errmsg = NULL;
3876
3877 // Switch the mouse off before changing the escape sequences used for
3878 // that.
3879 mch_setmouse(FALSE);
3880 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
3881 errmsg = e_invalid_argument;
3882 else
3883 check_mouse_termcode();
3884 if (termcap_active)
3885 setmouse(); // may switch it on again
3886
3887 return errmsg;
3888}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003889
3890 int
3891expand_set_ttymouse(optexpand_T *args, int *numMatches, char_u ***matches)
3892{
3893 return expand_set_opt_string(
3894 args,
3895 p_ttym_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003896 ARRAY_LENGTH(p_ttym_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003897 numMatches,
3898 matches);
3899}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003900#endif
3901
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003902#if defined(FEAT_VARTABS) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003903/*
3904 * The 'varsofttabstop' option is changed.
3905 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003906 char *
3907did_set_varsofttabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003908{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003909 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003910 char_u *cp;
3911
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003912 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00003913 VIM_CLEAR(curbuf->b_p_vsts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003914 else
3915 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003916 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003917 {
3918 if (vim_isdigit(*cp))
3919 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003920 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003921 continue;
3922 return e_invalid_argument;
3923 }
3924
3925 int *oldarray = curbuf->b_p_vsts_array;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003926 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003927 {
3928 if (oldarray)
3929 vim_free(oldarray);
3930 }
3931 else
3932 return e_invalid_argument;
3933 }
3934
3935 return NULL;
3936}
3937
3938/*
3939 * The 'vartabstop' option is changed.
3940 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003941 char *
3942did_set_vartabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003943{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003944 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003945 char_u *cp;
3946
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003947 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00003948 VIM_CLEAR(curbuf->b_p_vts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003949 else
3950 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003951 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003952 {
3953 if (vim_isdigit(*cp))
3954 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003955 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003956 continue;
3957 return e_invalid_argument;
3958 }
3959
3960 int *oldarray = curbuf->b_p_vts_array;
3961
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003962 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003963 {
3964 vim_free(oldarray);
3965# ifdef FEAT_FOLDING
3966 if (foldmethodIsIndent(curwin))
3967 foldUpdateAll(curwin);
3968# endif
3969 }
3970 else
3971 return e_invalid_argument;
3972 }
3973
3974 return NULL;
3975}
3976#endif
3977
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003978/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003979 * The 'verbosefile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003980 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003981 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003982did_set_verbosefile(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003983{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003984 verbose_stop();
3985 if (*p_vfile != NUL && verbose_open() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003986 return e_invalid_argument;
3987
3988 return NULL;
3989}
3990
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003991#if defined(FEAT_SESSION) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003992/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003993 * The 'viewoptions' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003994 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003995 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003996did_set_viewoptions(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003997{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003998 return did_set_opt_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003999}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004000#endif
4001
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004002#if defined(FEAT_VIMINFO) || defined(PROTO)
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004003/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004004 * The 'viminfo' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004005 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004006 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004007did_set_viminfo(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004008{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004009 char_u *s;
4010 char *errmsg = NULL;
4011
4012 for (s = p_viminfo; *s;)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004013 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004014 // Check it's a valid character
4015 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
4016 {
4017 errmsg = illegal_char(args->os_errbuf, *s);
4018 break;
4019 }
4020 if (*s == 'n') // name is always last one
4021 break;
4022 else if (*s == 'r') // skip until next ','
4023 {
4024 while (*++s && *s != ',')
4025 ;
4026 }
4027 else if (*s == '%')
4028 {
4029 // optional number
4030 while (vim_isdigit(*++s))
4031 ;
4032 }
4033 else if (*s == '!' || *s == 'h' || *s == 'c')
4034 ++s; // no extra chars
4035 else // must have a number
4036 {
4037 while (vim_isdigit(*++s))
4038 ;
4039
4040 if (!VIM_ISDIGIT(*(s - 1)))
4041 {
4042 if (args->os_errbuf != NULL)
4043 {
4044 sprintf(args->os_errbuf,
4045 _(e_missing_number_after_angle_str_angle),
4046 transchar_byte(*(s - 1)));
4047 errmsg = args->os_errbuf;
4048 }
4049 else
4050 errmsg = "";
4051 break;
4052 }
4053 }
4054 if (*s == ',')
4055 ++s;
4056 else if (*s)
4057 {
4058 if (args->os_errbuf != NULL)
4059 errmsg = e_missing_comma;
4060 else
4061 errmsg = "";
4062 break;
4063 }
4064 }
4065 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
4066 errmsg = e_must_specify_a_value;
4067
4068 return errmsg;
4069}
4070#endif
4071
4072/*
4073 * The 'virtualedit' option is changed.
4074 */
4075 char *
4076did_set_virtualedit(optset_T *args)
4077{
4078 char_u *ve = p_ve;
4079 unsigned int *flags = &ve_flags;
4080
4081 if (args->os_flags & OPT_LOCAL)
4082 {
4083 ve = curwin->w_p_ve;
4084 flags = &curwin->w_ve_flags;
4085 }
4086
4087 if ((args->os_flags & OPT_LOCAL) && *ve == NUL)
4088 // make the local value empty: use the global value
4089 *flags = 0;
4090 else
4091 {
4092 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
4093 return e_invalid_argument;
4094 else if (STRCMP(ve, args->os_oldval.string) != 0)
4095 {
4096 // Recompute cursor position in case the new 've' setting
4097 // changes something.
4098 validate_virtcol();
4099 coladvance(curwin->w_virtcol);
4100 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004101 }
4102
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004103 return NULL;
4104}
4105
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004106 int
4107expand_set_virtualedit(optexpand_T *args, int *numMatches, char_u ***matches)
4108{
4109 return expand_set_opt_string(
4110 args,
4111 p_ve_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004112 ARRAY_LENGTH(p_ve_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004113 numMatches,
4114 matches);
4115}
4116
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004117/*
4118 * The 'whichwrap' option is changed.
4119 */
4120 char *
4121did_set_whichwrap(optset_T *args)
4122{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004123 char_u **varp = (char_u **)args->os_varp;
4124
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004125 // Add ',' to the list flags because 'whichwrap' is a flag
4126 // list that is comma-separated.
4127 return did_set_option_listflag(*varp, (char_u *)(WW_ALL ","), args->os_errbuf);
4128}
4129
4130 int
4131expand_set_whichwrap(optexpand_T *args, int *numMatches, char_u ***matches)
4132{
4133 return expand_set_opt_listflag(args, (char_u*)WW_ALL, numMatches, matches);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004134}
4135
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004136/*
4137 * The 'wildmode' option is changed.
4138 */
4139 char *
4140did_set_wildmode(optset_T *args UNUSED)
4141{
4142 if (check_opt_wim() == FAIL)
4143 return e_invalid_argument;
4144 return NULL;
4145}
4146
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004147 int
4148expand_set_wildmode(optexpand_T *args, int *numMatches, char_u ***matches)
4149{
4150 return expand_set_opt_string(
4151 args,
4152 p_wim_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004153 ARRAY_LENGTH(p_wim_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004154 numMatches,
4155 matches);
4156}
4157
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004158/*
4159 * The 'wildoptions' option is changed.
4160 */
4161 char *
4162did_set_wildoptions(optset_T *args UNUSED)
4163{
4164 return did_set_opt_strings(p_wop, p_wop_values, TRUE);
4165}
4166
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004167 int
4168expand_set_wildoptions(optexpand_T *args, int *numMatches, char_u ***matches)
4169{
4170 return expand_set_opt_string(
4171 args,
4172 p_wop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004173 ARRAY_LENGTH(p_wop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004174 numMatches,
4175 matches);
4176}
4177
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004178#if defined(FEAT_WAK) || defined(PROTO)
4179/*
4180 * The 'winaltkeys' option is changed.
4181 */
4182 char *
4183did_set_winaltkeys(optset_T *args UNUSED)
4184{
4185 char *errmsg = NULL;
4186
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004187 if (*p_wak == NUL || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004188 errmsg = e_invalid_argument;
4189# ifdef FEAT_MENU
4190# if defined(FEAT_GUI_MOTIF)
4191 else if (gui.in_use)
4192 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4193# elif defined(FEAT_GUI_GTK)
4194 else if (gui.in_use)
4195 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4196# endif
4197# endif
4198 return errmsg;
4199}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004200
4201 int
4202expand_set_winaltkeys(optexpand_T *args, int *numMatches, char_u ***matches)
4203{
4204 return expand_set_opt_string(
4205 args,
4206 p_wak_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004207 ARRAY_LENGTH(p_wak_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004208 numMatches,
4209 matches);
4210}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004211#endif
4212
4213/*
4214 * The 'wincolor' option is changed.
4215 */
4216 char *
4217did_set_wincolor(optset_T *args UNUSED)
4218{
4219#ifdef FEAT_TERMINAL
4220 term_update_wincolor(curwin);
4221#endif
4222 return NULL;
4223}
4224
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004225 int
4226expand_set_wincolor(optexpand_T *args, int *numMatches, char_u ***matches)
4227{
4228 return expand_set_opt_generic(
4229 args,
4230 get_highlight_name,
4231 numMatches,
4232 matches);
4233}
4234
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004235#ifdef FEAT_SYN_HL
4236/*
4237 * When the 'syntax' option is set, load the syntax of that name.
4238 */
4239 static void
4240do_syntax_autocmd(int value_changed)
4241{
4242 static int syn_recursive = 0;
4243
4244 ++syn_recursive;
4245 // Only pass TRUE for "force" when the value changed or not used
4246 // recursively, to avoid endless recurrence.
4247 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
4248 value_changed || syn_recursive == 1, curbuf);
4249 curbuf->b_flags |= BF_SYN_SET;
4250 --syn_recursive;
4251}
4252#endif
4253
4254/*
4255 * When the 'filetype' option is set, trigger the FileType autocommand.
4256 */
4257 static void
4258do_filetype_autocmd(char_u **varp, int opt_flags, int value_changed)
4259{
4260 // Skip this when called from a modeline and the filetype was already set
4261 // to this value.
4262 if ((opt_flags & OPT_MODELINE) && !value_changed)
4263 return;
4264
4265 static int ft_recursive = 0;
4266 int secure_save = secure;
4267
4268 // Reset the secure flag, since the value of 'filetype' has
4269 // been checked to be safe.
4270 secure = 0;
4271
4272 ++ft_recursive;
4273 did_filetype = TRUE;
4274 // Only pass TRUE for "force" when the value changed or not
4275 // used recursively, to avoid endless recurrence.
4276 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
4277 value_changed || ft_recursive == 1, curbuf);
4278 --ft_recursive;
4279 // Just in case the old "curbuf" is now invalid.
4280 if (varp != &(curbuf->b_p_ft))
4281 varp = NULL;
4282
4283 secure = secure_save;
4284}
4285
4286#ifdef FEAT_SPELL
4287/*
4288 * When the 'spelllang' option is set, source the spell/LANG.vim file in
4289 * 'runtimepath'.
4290 */
4291 static void
4292do_spelllang_source(void)
4293{
4294 char_u fname[200];
4295 char_u *p;
4296 char_u *q = curwin->w_s->b_p_spl;
4297
4298 // Skip the first name if it is "cjk".
4299 if (STRNCMP(q, "cjk,", 4) == 0)
4300 q += 4;
4301
4302 // They could set 'spellcapcheck' depending on the language. Use the first
4303 // name in 'spelllang' up to '_region' or '.encoding'.
4304 for (p = q; *p != NUL; ++p)
4305 if (!ASCII_ISALNUM(*p) && *p != '-')
4306 break;
4307 if (p > q)
4308 {
4309 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
4310 (int)(p - q), q);
4311 source_runtime(fname, DIP_ALL);
4312 }
4313}
4314#endif
4315
4316/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004317 * Handle string options that need some action to perform when changed.
zeertzjqf6782732022-07-27 18:26:03 +01004318 * The new value must be allocated.
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004319 * Returns NULL for success, or an untranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +02004320 */
4321 char *
4322did_set_string_option(
4323 int opt_idx, // index in options[] table
4324 char_u **varp, // pointer to the option variable
Bram Moolenaardac13472019-09-16 21:06:21 +02004325 char_u *oldval, // previous value of the option
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004326 char_u *value, // new value of the option
Bram Moolenaardac13472019-09-16 21:06:21 +02004327 char *errbuf, // buffer for errors, or NULL
4328 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004329 set_op_T op, // OP_ADDING/OP_PREPENDING/OP_REMOVING
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004330 int *value_checked) // value was checked to be safe, no
Bram Moolenaardac13472019-09-16 21:06:21 +02004331 // need to set P_INSECURE
4332{
4333 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +02004334 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004335 opt_did_set_cb_T did_set_cb = get_option_did_set_cb(opt_idx);
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004336 optset_T args;
4337
4338 // 'ttytype' is an alias for 'term'. Both 'term' and 'ttytype' point to
4339 // T_NAME. If 'term' or 'ttytype' is modified, then use the index for the
4340 // 'term' option. Only set the P_ALLOCED flag on 'term'.
4341 if (varp == &T_NAME)
4342 {
4343 opt_idx = findoption((char_u *)"term");
4344 if (opt_idx >= 0)
4345 {
4346 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
4347 did_set_cb = get_option_did_set_cb(opt_idx);
4348 }
4349 }
4350
4351 CLEAR_FIELD(args);
Bram Moolenaardac13472019-09-16 21:06:21 +02004352
Bram Moolenaardac13472019-09-16 21:06:21 +02004353 // Disallow changing some options from secure mode
4354 if ((secure
4355#ifdef HAVE_SANDBOX
4356 || sandbox != 0
4357#endif
4358 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00004359 errmsg = e_not_allowed_here;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004360 // Check for a "normal" directory or file name in some options.
4361 else if (check_illegal_path_names(opt_idx, varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004362 errmsg = e_invalid_argument;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004363 else if (did_set_cb != NULL)
4364 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004365 args.os_varp = (char_u *)varp;
4366 args.os_idx = opt_idx;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004367 args.os_flags = opt_flags;
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004368 args.os_op = op;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004369 args.os_oldval.string = oldval;
4370 args.os_newval.string = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004371 args.os_errbuf = errbuf;
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004372 // Invoke the option specific callback function to validate and apply
4373 // the new option value.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004374 errmsg = did_set_cb(&args);
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004375
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004376 // The 'keymap', 'filetype' and 'syntax' option callback functions
4377 // may change the os_value_checked field.
4378 *value_checked = args.os_value_checked;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004379 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004380
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004381 // If an error is detected, restore the previous value.
Bram Moolenaardac13472019-09-16 21:06:21 +02004382 if (errmsg != NULL)
4383 {
zeertzjqf6782732022-07-27 18:26:03 +01004384 free_string_option(*varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02004385 *varp = oldval;
4386 // When resetting some values, need to act on it.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004387 if (args.os_restore_chartab)
Bram Moolenaardac13472019-09-16 21:06:21 +02004388 (void)init_chartab();
4389 if (varp == &p_hl)
4390 (void)highlight_changed();
4391 }
4392 else
4393 {
4394#ifdef FEAT_EVAL
4395 // Remember where the option was set.
4396 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4397#endif
4398 // Free string options that are in allocated memory.
4399 // Use "free_oldval", because recursiveness may change the flags under
4400 // our fingers (esp. init_highlight()).
4401 if (free_oldval)
4402 free_string_option(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01004403 set_option_flag(opt_idx, P_ALLOCED);
Bram Moolenaardac13472019-09-16 21:06:21 +02004404
4405 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
4406 && is_global_local_option(opt_idx))
4407 {
4408 // global option with local value set to use global value; free
4409 // the local value and make it empty
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004410 char_u *p = get_option_varp_scope(opt_idx, OPT_LOCAL);
Bram Moolenaardac13472019-09-16 21:06:21 +02004411 free_string_option(*(char_u **)p);
4412 *(char_u **)p = empty_option;
4413 }
4414
4415 // May set global value for local option.
4416 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
4417 set_string_option_global(opt_idx, varp);
4418
4419 // Trigger the autocommand only after setting the flags.
4420#ifdef FEAT_SYN_HL
Bram Moolenaardac13472019-09-16 21:06:21 +02004421 if (varp == &(curbuf->b_p_syn))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004422 do_syntax_autocmd(args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004423#endif
4424 else if (varp == &(curbuf->b_p_ft))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004425 do_filetype_autocmd(varp, opt_flags, args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004426#ifdef FEAT_SPELL
4427 if (varp == &(curwin->w_s->b_p_spl))
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004428 do_spelllang_source();
Bram Moolenaardac13472019-09-16 21:06:21 +02004429#endif
4430 }
4431
Bram Moolenaardac13472019-09-16 21:06:21 +02004432 if (varp == &p_mouse)
4433 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004434 if (*p_mouse == NUL)
4435 mch_setmouse(FALSE); // switch mouse off
4436 else
Bram Moolenaardac13472019-09-16 21:06:21 +02004437 setmouse(); // in case 'mouse' changed
4438 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004439
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004440#if defined(FEAT_LUA) || defined(PROTO)
4441 if (varp == &p_rtp)
4442 update_package_paths_in_lua();
4443#endif
4444
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004445#if defined(FEAT_LINEBREAK)
4446 // Changing Formatlistpattern when briopt includes the list setting:
4447 // redraw
4448 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
4449 && curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004450 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004451#endif
4452
Bram Moolenaardac13472019-09-16 21:06:21 +02004453 if (curwin->w_curswant != MAXCOL
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02004454 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02004455 curwin->w_set_curswant = TRUE;
4456
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004457 if ((opt_flags & OPT_NO_REDRAW) == 0)
4458 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004459#ifdef FEAT_GUI
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004460 // set when changing an option that only requires a redraw in the GUI
4461 int redraw_gui_only = FALSE;
4462
4463 if (varp == &p_go // 'guioptions'
4464 || varp == &p_guifont // 'guifont'
4465# ifdef FEAT_GUI_TABLINE
4466 || varp == &p_gtl // 'guitablabel'
4467 || varp == &p_gtt // 'guitabtooltip'
4468# endif
4469# ifdef FEAT_XFONTSET
4470 || varp == &p_guifontset // 'guifontset'
4471# endif
4472 || varp == &p_guifontwide // 'guifontwide'
4473# ifdef FEAT_GUI_GTK
4474 || varp == &p_guiligatures // 'guiligatures'
4475# endif
4476 )
4477 redraw_gui_only = TRUE;
4478
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004479 // check redraw when it's not a GUI option or the GUI is active.
4480 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02004481#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004482 check_redraw(get_option_flags(opt_idx));
4483 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004484
4485#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004486 if (args.os_did_swaptcap)
Bram Moolenaardac13472019-09-16 21:06:21 +02004487 {
4488 set_termname((char_u *)"win32");
4489 init_highlight(TRUE, FALSE);
4490 }
4491#endif
4492
4493 return errmsg;
4494}
4495
4496/*
4497 * Check an option that can be a range of string values.
4498 *
4499 * Return OK for correct value, FAIL otherwise.
4500 * Empty is always OK.
4501 */
4502 static int
4503check_opt_strings(
4504 char_u *val,
4505 char **values,
4506 int list) // when TRUE: accept a list of values
4507{
4508 return opt_strings_flags(val, values, NULL, list);
4509}
4510
4511/*
4512 * Handle an option that can be a range of string values.
4513 * Set a flag in "*flagp" for each string present.
4514 *
4515 * Return OK for correct value, FAIL otherwise.
4516 * Empty is always OK.
4517 */
4518 static int
4519opt_strings_flags(
4520 char_u *val, // new value
4521 char **values, // array of valid string values
4522 unsigned *flagp,
4523 int list) // when TRUE: accept a list of values
4524{
4525 int i;
4526 int len;
4527 unsigned new_flags = 0;
4528
4529 while (*val)
4530 {
4531 for (i = 0; ; ++i)
4532 {
4533 if (values[i] == NULL) // val not found in values[]
4534 return FAIL;
4535
4536 len = (int)STRLEN(values[i]);
4537 if (STRNCMP(values[i], val, len) == 0
4538 && ((list && val[len] == ',') || val[len] == NUL))
4539 {
4540 val += len + (val[len] == ',');
4541 new_flags |= (1 << i);
4542 break; // check next item in val list
4543 }
4544 }
4545 }
4546 if (flagp != NULL)
4547 *flagp = new_flags;
4548
4549 return OK;
4550}
4551
4552/*
4553 * return OK if "p" is a valid fileformat name, FAIL otherwise.
4554 */
4555 int
4556check_ff_value(char_u *p)
4557{
4558 return check_opt_strings(p, p_ff_values, FALSE);
4559}
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004560
4561/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004562 * Save the actual shortmess Flags and clear them temporarily to avoid that
4563 * file messages overwrites any output from the following commands.
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004564 *
4565 * Caller must make sure to first call save_clear_shm_value() and then
4566 * restore_shm_value() exactly the same number of times.
4567 */
4568 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004569save_clear_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004570{
4571 if (STRLEN(p_shm) >= SHM_LEN)
4572 {
4573 iemsg(e_internal_error_shortmess_too_long);
4574 return;
4575 }
4576
4577 if (++set_shm_recursive == 1)
4578 {
4579 STRCPY(shm_buf, p_shm);
4580 set_option_value_give_err((char_u *)"shm", 0L, (char_u *)"", 0);
4581 }
4582}
4583
4584/*
4585 * Restore the shortmess Flags set from the save_clear_shm_value() function.
4586 */
4587 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004588restore_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004589{
4590 if (--set_shm_recursive == 0)
4591 {
4592 set_option_value_give_err((char_u *)"shm", 0L, shm_buf, 0);
4593 vim_memset(shm_buf, 0, SHM_LEN);
4594 }
4595}