blob: 170d48e9a26acb6e4d34ec74c0f3fb76ea508630 [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
glepnir43eef882024-06-19 20:20:48 +0200121static char *(p_cot_values[]) = {"menu", "menuone", "longest", "preview", "popup", "popuphidden", "noinsert", "noselect", "fuzzy", "fuzzycollect", 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);
zeertzjq529b9ad2024-06-05 20:27:06 +0200147 (void)opt_strings_flags(p_cot, p_cot_values, &cot_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200148#ifdef FEAT_SESSION
149 (void)opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE);
150 (void)opt_strings_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
151#endif
152#ifdef FEAT_FOLDING
153 (void)opt_strings_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
154#endif
155 (void)opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE);
Yegappan Lakshmanan87018252023-09-20 20:20:04 +0200156 (void)opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200157 (void)opt_strings_flags(p_tc, p_tc_values, &tc_flags, FALSE);
158 (void)opt_strings_flags(p_ve, p_ve_values, &ve_flags, TRUE);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +0200159#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +0200160 (void)opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE);
161#endif
162#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
163 (void)opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags, TRUE);
164#endif
165#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
166 (void)opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE);
167#endif
Sean Dewar39c46b42022-05-12 17:44:29 +0100168 (void)opt_strings_flags(p_swb, p_swb_values, &swb_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200169}
170
Yegappan Lakshmananf9dc2782023-05-11 15:02:56 +0100171#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200172/*
173 * Trigger the OptionSet autocommand.
174 * "opt_idx" is the index of the option being set.
175 * "opt_flags" can be OPT_LOCAL etc.
176 * "oldval" the old value
177 * "oldval_l" the old local value (only non-NULL if global and local value
178 * are set)
179 * "oldval_g" the old global value (only non-NULL if global and local value
180 * are set)
181 * "newval" the new value
182 */
183 void
zeertzjq269aa2b2022-11-28 11:36:50 +0000184trigger_optionset_string(
Bram Moolenaardac13472019-09-16 21:06:21 +0200185 int opt_idx,
186 int opt_flags,
187 char_u *oldval,
188 char_u *oldval_l,
189 char_u *oldval_g,
190 char_u *newval)
191{
192 // Don't do this recursively.
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000193 if (oldval == NULL || newval == NULL
194 || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
195 return;
Bram Moolenaardac13472019-09-16 21:06:21 +0200196
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000197 char_u buf_type[7];
198
199 sprintf((char *)buf_type, "%s",
Bram Moolenaardac13472019-09-16 21:06:21 +0200200 (opt_flags & OPT_LOCAL) ? "local" : "global");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000201 set_vim_var_string(VV_OPTION_OLD, oldval, -1);
202 set_vim_var_string(VV_OPTION_NEW, newval, -1);
203 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
204 if (opt_flags & OPT_LOCAL)
205 {
206 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
207 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
Bram Moolenaardac13472019-09-16 21:06:21 +0200208 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000209 if (opt_flags & OPT_GLOBAL)
210 {
211 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
212 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval, -1);
213 }
214 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
215 {
216 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
217 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval_l, -1);
218 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval_g, -1);
219 }
220 if (opt_flags & OPT_MODELINE)
221 {
222 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
223 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
224 }
225 apply_autocmds(EVENT_OPTIONSET,
226 get_option_fullname(opt_idx), NULL, FALSE,
227 NULL);
228 reset_v_option_vars();
Bram Moolenaardac13472019-09-16 21:06:21 +0200229}
230#endif
231
232 static char *
Mike Williams620f0112023-12-05 15:36:06 +0100233illegal_char(char *errbuf, size_t errbuflen, int c)
Bram Moolenaardac13472019-09-16 21:06:21 +0200234{
235 if (errbuf == NULL)
236 return "";
zeertzjq6a8d2e12024-01-17 20:54:49 +0100237 vim_snprintf(errbuf, errbuflen, _(e_illegal_character_str),
Christian Brabandtb39b2402023-11-29 11:34:05 +0100238 (char *)transchar(c));
Bram Moolenaardac13472019-09-16 21:06:21 +0200239 return errbuf;
240}
241
242/*
243 * Check string options in a buffer for NULL value.
244 */
245 void
246check_buf_options(buf_T *buf)
247{
248 check_string_option(&buf->b_p_bh);
249 check_string_option(&buf->b_p_bt);
250 check_string_option(&buf->b_p_fenc);
251 check_string_option(&buf->b_p_ff);
252#ifdef FEAT_FIND_ID
253 check_string_option(&buf->b_p_def);
254 check_string_option(&buf->b_p_inc);
255# ifdef FEAT_EVAL
256 check_string_option(&buf->b_p_inex);
257# endif
258#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +0100259#if defined(FEAT_EVAL)
Bram Moolenaardac13472019-09-16 21:06:21 +0200260 check_string_option(&buf->b_p_inde);
261 check_string_option(&buf->b_p_indk);
262#endif
263#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
264 check_string_option(&buf->b_p_bexpr);
265#endif
266#if defined(FEAT_CRYPT)
267 check_string_option(&buf->b_p_cm);
268#endif
269 check_string_option(&buf->b_p_fp);
270#if defined(FEAT_EVAL)
271 check_string_option(&buf->b_p_fex);
272#endif
273#ifdef FEAT_CRYPT
274 check_string_option(&buf->b_p_key);
275#endif
276 check_string_option(&buf->b_p_kp);
277 check_string_option(&buf->b_p_mps);
278 check_string_option(&buf->b_p_fo);
279 check_string_option(&buf->b_p_flp);
280 check_string_option(&buf->b_p_isk);
Bram Moolenaardac13472019-09-16 21:06:21 +0200281 check_string_option(&buf->b_p_com);
Bram Moolenaardac13472019-09-16 21:06:21 +0200282#ifdef FEAT_FOLDING
283 check_string_option(&buf->b_p_cms);
284#endif
285 check_string_option(&buf->b_p_nf);
Bram Moolenaardac13472019-09-16 21:06:21 +0200286 check_string_option(&buf->b_p_qe);
Bram Moolenaardac13472019-09-16 21:06:21 +0200287#ifdef FEAT_SYN_HL
288 check_string_option(&buf->b_p_syn);
289 check_string_option(&buf->b_s.b_syn_isk);
290#endif
291#ifdef FEAT_SPELL
292 check_string_option(&buf->b_s.b_p_spc);
293 check_string_option(&buf->b_s.b_p_spf);
294 check_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +0200295 check_string_option(&buf->b_s.b_p_spo);
Bram Moolenaardac13472019-09-16 21:06:21 +0200296#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200297 check_string_option(&buf->b_p_sua);
Bram Moolenaardac13472019-09-16 21:06:21 +0200298 check_string_option(&buf->b_p_cink);
299 check_string_option(&buf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +0100300 check_string_option(&buf->b_p_cinsd);
Bram Moolenaardac13472019-09-16 21:06:21 +0200301 parse_cino(buf);
Bram Moolenaar49846fb2022-10-15 16:05:33 +0100302 check_string_option(&buf->b_p_lop);
Bram Moolenaardac13472019-09-16 21:06:21 +0200303 check_string_option(&buf->b_p_ft);
Bram Moolenaardac13472019-09-16 21:06:21 +0200304 check_string_option(&buf->b_p_cinw);
zeertzjq529b9ad2024-06-05 20:27:06 +0200305 check_string_option(&buf->b_p_cot);
Bram Moolenaardac13472019-09-16 21:06:21 +0200306 check_string_option(&buf->b_p_cpt);
307#ifdef FEAT_COMPL_FUNC
308 check_string_option(&buf->b_p_cfu);
309 check_string_option(&buf->b_p_ofu);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +0100310 check_string_option(&buf->b_p_tsrfu);
Bram Moolenaardac13472019-09-16 21:06:21 +0200311#endif
312#ifdef FEAT_EVAL
313 check_string_option(&buf->b_p_tfu);
314#endif
315#ifdef FEAT_KEYMAP
316 check_string_option(&buf->b_p_keymap);
317#endif
318#ifdef FEAT_QUICKFIX
319 check_string_option(&buf->b_p_gp);
320 check_string_option(&buf->b_p_mp);
321 check_string_option(&buf->b_p_efm);
322#endif
323 check_string_option(&buf->b_p_ep);
324 check_string_option(&buf->b_p_path);
325 check_string_option(&buf->b_p_tags);
326 check_string_option(&buf->b_p_tc);
327 check_string_option(&buf->b_p_dict);
328 check_string_option(&buf->b_p_tsr);
Bram Moolenaardac13472019-09-16 21:06:21 +0200329 check_string_option(&buf->b_p_lw);
Bram Moolenaardac13472019-09-16 21:06:21 +0200330 check_string_option(&buf->b_p_bkc);
331 check_string_option(&buf->b_p_menc);
332#ifdef FEAT_VARTABS
333 check_string_option(&buf->b_p_vsts);
334 check_string_option(&buf->b_p_vts);
335#endif
336}
337
338/*
339 * Free the string allocated for an option.
340 * Checks for the string being empty_option. This may happen if we're out of
341 * memory, vim_strsave() returned NULL, which was replaced by empty_option by
342 * check_options().
343 * Does NOT check for P_ALLOCED flag!
344 */
345 void
346free_string_option(char_u *p)
347{
348 if (p != empty_option)
349 vim_free(p);
350}
351
352 void
353clear_string_option(char_u **pp)
354{
355 if (*pp != empty_option)
356 vim_free(*pp);
357 *pp = empty_option;
358}
359
360 void
361check_string_option(char_u **pp)
362{
363 if (*pp == NULL)
364 *pp = empty_option;
365}
366
367/*
368 * Set global value for string option when it's a local option.
369 */
370 static void
371set_string_option_global(
372 int opt_idx, // option index
373 char_u **varp) // pointer to option variable
374{
375 char_u **p, *s;
376
377 // the global value is always allocated
378 if (is_window_local_option(opt_idx))
379 p = (char_u **)GLOBAL_WO(varp);
380 else
381 p = (char_u **)get_option_var(opt_idx);
382 if (!is_global_option(opt_idx)
383 && p != varp
384 && (s = vim_strsave(*varp)) != NULL)
385 {
386 free_string_option(*p);
387 *p = s;
388 }
389}
390
391/*
392 * Set a string option to a new value (without checking the effect).
393 * The string is copied into allocated memory.
394 * if ("opt_idx" == -1) "name" is used, otherwise "opt_idx" is used.
395 * When "set_sid" is zero set the scriptID to current_sctx.sc_sid. When
396 * "set_sid" is SID_NONE don't set the scriptID. Otherwise set the scriptID to
397 * "set_sid".
398 */
399 void
400set_string_option_direct(
401 char_u *name,
402 int opt_idx,
403 char_u *val,
404 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
405 int set_sid UNUSED)
406{
407 char_u *s;
408 char_u **varp;
409 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
410 int idx = opt_idx;
411
412 if (idx == -1) // use name
413 {
414 idx = findoption(name);
415 if (idx < 0) // not found (should not happen)
416 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000417 semsg(_(e_internal_error_str), "set_string_option_direct()");
RestorerZ68ebcee2023-05-31 17:12:14 +0100418 siemsg("For option %s", name);
Bram Moolenaardac13472019-09-16 21:06:21 +0200419 return;
420 }
421 }
422
423 if (is_hidden_option(idx)) // can't set hidden option
424 return;
425
426 s = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000427 if (s == NULL)
428 return;
429
430 varp = (char_u **)get_option_varp_scope(idx,
431 both ? OPT_LOCAL : opt_flags);
432 if ((opt_flags & OPT_FREE) && (get_option_flags(idx) & P_ALLOCED))
433 free_string_option(*varp);
434 *varp = s;
435
436 // For buffer/window local option may also set the global value.
437 if (both)
438 set_string_option_global(idx, varp);
439
440 set_option_flag(idx, P_ALLOCED);
441
442 // When setting both values of a global option with a local value,
443 // make the local value empty, so that the global value is used.
444 if (is_global_local_option(idx) && both)
Bram Moolenaardac13472019-09-16 21:06:21 +0200445 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000446 free_string_option(*varp);
447 *varp = empty_option;
Bram Moolenaardac13472019-09-16 21:06:21 +0200448 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000449# ifdef FEAT_EVAL
450 if (set_sid != SID_NONE)
451 {
452 sctx_T script_ctx;
453
454 if (set_sid == 0)
455 script_ctx = current_sctx;
456 else
457 {
458 script_ctx.sc_sid = set_sid;
459 script_ctx.sc_seq = 0;
460 script_ctx.sc_lnum = 0;
461 script_ctx.sc_version = 1;
462 }
463 set_option_sctx_idx(idx, opt_flags, script_ctx);
464 }
465# endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200466}
467
Dominique Pellee764d1b2023-03-12 21:20:59 +0000468#if defined(FEAT_PROP_POPUP) || \
469 (defined(FEAT_DIFF) && defined(FEAT_FOLDING)) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200470/*
471 * Like set_string_option_direct(), but for a window-local option in "wp".
472 * Blocks autocommands to avoid the old curwin becoming invalid.
473 */
474 void
475set_string_option_direct_in_win(
476 win_T *wp,
477 char_u *name,
478 int opt_idx,
479 char_u *val,
480 int opt_flags,
481 int set_sid)
482{
483 win_T *save_curwin = curwin;
484
485 block_autocmds();
486 curwin = wp;
487 curbuf = curwin->w_buffer;
488 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
489 curwin = save_curwin;
490 curbuf = curwin->w_buffer;
491 unblock_autocmds();
492}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000493#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200494
Dominique Pelle748b3082022-01-08 12:41:16 +0000495#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200496/*
497 * Like set_string_option_direct(), but for a buffer-local option in "buf".
498 * Blocks autocommands to avoid the old curbuf becoming invalid.
499 */
500 void
501set_string_option_direct_in_buf(
502 buf_T *buf,
503 char_u *name,
504 int opt_idx,
505 char_u *val,
506 int opt_flags,
507 int set_sid)
508{
509 buf_T *save_curbuf = curbuf;
510
511 block_autocmds();
512 curbuf = buf;
513 curwin->w_buffer = curbuf;
514 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
515 curbuf = save_curbuf;
516 curwin->w_buffer = curbuf;
517 unblock_autocmds();
518}
Dominique Pelle748b3082022-01-08 12:41:16 +0000519#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200520
521/*
522 * Set a string option to a new value, and handle the effects.
523 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100524 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaardac13472019-09-16 21:06:21 +0200525 */
526 char *
527set_string_option(
528 int opt_idx,
529 char_u *value,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +0000530 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Christian Brabandtb39b2402023-11-29 11:34:05 +0100531 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +0100532 size_t errbuflen)
Bram Moolenaardac13472019-09-16 21:06:21 +0200533{
534 char_u *s;
535 char_u **varp;
536 char_u *oldval;
537#if defined(FEAT_EVAL)
538 char_u *oldval_l = NULL;
539 char_u *oldval_g = NULL;
540 char_u *saved_oldval = NULL;
541 char_u *saved_oldval_l = NULL;
542 char_u *saved_oldval_g = NULL;
543 char_u *saved_newval = NULL;
544#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100545 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +0200546 int value_checked = FALSE;
547
548 if (is_hidden_option(opt_idx)) // don't set hidden option
549 return NULL;
550
Bram Moolenaar7f009df2020-03-16 20:27:38 +0100551 s = vim_strsave(value == NULL ? (char_u *)"" : value);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000552 if (s == NULL)
553 return NULL;
554
555 varp = (char_u **)get_option_varp_scope(opt_idx,
556 (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
557 ? (is_global_local_option(opt_idx)
558 ? OPT_GLOBAL : OPT_LOCAL)
559 : opt_flags);
560 oldval = *varp;
561#if defined(FEAT_EVAL)
562 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +0200563 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000564 oldval_l = *(char_u **)get_option_varp_scope(opt_idx, OPT_LOCAL);
565 oldval_g = *(char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
Bram Moolenaardac13472019-09-16 21:06:21 +0200566 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000567#endif
568 *varp = s;
569
570#if defined(FEAT_EVAL)
571 if (!starting
572# ifdef FEAT_CRYPT
573 && !is_crypt_key_option(opt_idx)
574# endif
575 )
576 {
577 if (oldval_l != NULL)
578 saved_oldval_l = vim_strsave(oldval_l);
579 if (oldval_g != NULL)
580 saved_oldval_g = vim_strsave(oldval_g);
581 saved_oldval = vim_strsave(oldval);
582 saved_newval = vim_strsave(s);
583 }
584#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000585 if ((errmsg = did_set_string_option(opt_idx, varp, oldval, value, errbuf,
Christian Brabandtb39b2402023-11-29 11:34:05 +0100586 errbuflen, opt_flags, OP_NONE, &value_checked)) == NULL)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000587 did_set_option(opt_idx, opt_flags, TRUE, value_checked);
588
589#if defined(FEAT_EVAL)
590 // call autocommand after handling side effects
591 if (errmsg == NULL)
592 trigger_optionset_string(opt_idx, opt_flags,
593 saved_oldval, saved_oldval_l,
594 saved_oldval_g, saved_newval);
595 vim_free(saved_oldval);
596 vim_free(saved_oldval_l);
597 vim_free(saved_oldval_g);
598 vim_free(saved_newval);
599#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100600 return errmsg;
Bram Moolenaardac13472019-09-16 21:06:21 +0200601}
602
603/*
604 * Return TRUE if "val" is a valid 'filetype' name.
605 * Also used for 'syntax' and 'keymap'.
606 */
607 static int
608valid_filetype(char_u *val)
609{
610 return valid_name(val, ".-_");
611}
612
613#ifdef FEAT_STL_OPT
614/*
615 * Check validity of options with the 'statusline' format.
zeertzjq5dc294a2022-04-15 13:17:57 +0100616 * Return an untranslated error message or NULL.
Bram Moolenaardac13472019-09-16 21:06:21 +0200617 */
618 static char *
619check_stl_option(char_u *s)
620{
Bram Moolenaardac13472019-09-16 21:06:21 +0200621 int groupdepth = 0;
Christian Brabandtb39b2402023-11-29 11:34:05 +0100622 static char errbuf[ERR_BUFLEN];
623 int errbuflen = ERR_BUFLEN;
Bram Moolenaardac13472019-09-16 21:06:21 +0200624
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100625 while (*s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200626 {
627 // Check for valid keys after % sequences
628 while (*s && *s != '%')
629 s++;
630 if (!*s)
631 break;
632 s++;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +0000633 if (*s == '%' || *s == STL_TRUNCMARK || *s == STL_SEPARATE)
Bram Moolenaardac13472019-09-16 21:06:21 +0200634 {
635 s++;
636 continue;
637 }
638 if (*s == ')')
639 {
640 s++;
641 if (--groupdepth < 0)
642 break;
643 continue;
644 }
645 if (*s == '-')
646 s++;
647 while (VIM_ISDIGIT(*s))
648 s++;
649 if (*s == STL_USER_HL)
650 continue;
651 if (*s == '.')
652 {
653 s++;
654 while (*s && VIM_ISDIGIT(*s))
655 s++;
656 }
657 if (*s == '(')
658 {
659 groupdepth++;
660 continue;
661 }
662 if (vim_strchr(STL_ALL, *s) == NULL)
663 {
Christian Brabandtb39b2402023-11-29 11:34:05 +0100664 return illegal_char(errbuf, errbuflen, *s);
Bram Moolenaardac13472019-09-16 21:06:21 +0200665 }
666 if (*s == '{')
667 {
zeertzjq5dc294a2022-04-15 13:17:57 +0100668 int reevaluate = (*++s == '%');
shadmansaleh30e3de22021-05-15 17:23:28 +0200669
zeertzjq5dc294a2022-04-15 13:17:57 +0100670 if (reevaluate && *++s == '}')
671 // "}" is not allowed immediately after "%{%"
Christian Brabandtb39b2402023-11-29 11:34:05 +0100672 return illegal_char(errbuf, errbuflen, '}');
shadmansaleh30e3de22021-05-15 17:23:28 +0200673 while ((*s != '}' || (reevaluate && s[-1] != '%')) && *s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200674 s++;
675 if (*s != '}')
zeertzjq5dc294a2022-04-15 13:17:57 +0100676 return e_unclosed_expression_sequence;
Bram Moolenaardac13472019-09-16 21:06:21 +0200677 }
678 }
Bram Moolenaardac13472019-09-16 21:06:21 +0200679 if (groupdepth != 0)
zeertzjq5dc294a2022-04-15 13:17:57 +0100680 return e_unbalanced_groups;
Bram Moolenaardac13472019-09-16 21:06:21 +0200681 return NULL;
682}
683#endif
684
685/*
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +0000686 * Check for a "normal" directory or file name in some options. Disallow a
687 * path separator (slash and/or backslash), wildcards and characters that are
688 * often illegal in a file name. Be more permissive if "secure" is off.
689 */
690 static int
691check_illegal_path_names(int opt_idx, char_u **varp)
692{
693 return (((get_option_flags(opt_idx) & P_NFNAME)
694 && vim_strpbrk(*varp, (char_u *)(secure
695 ? "/\\*?[|;&<>\r\n" : "/\\*?[<>\r\n")) != NULL)
696 || ((get_option_flags(opt_idx) & P_NDNAME)
697 && vim_strpbrk(*varp, (char_u *)"*?[|;&<>\r\n") != NULL));
698}
699
700/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000701 * An option that accepts a list of flags is changed.
702 * e.g. 'viewoptions', 'switchbuf', 'casemap', etc.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000703 */
704 static char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000705did_set_opt_flags(char_u *val, char **values, unsigned *flagp, int list)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000706{
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000707 if (opt_strings_flags(val, values, flagp, list) == FAIL)
708 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000709
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000710 return NULL;
711}
712
713/*
714 * An option that accepts a list of string values is changed.
715 * e.g. 'nrformats', 'scrollopt', 'wildoptions', etc.
716 */
717 static char *
718did_set_opt_strings(char_u *val, char **values, int list)
719{
720 return did_set_opt_flags(val, values, NULL, list);
721}
722
723/*
724 * An option which is a list of flags is set. Valid values are in 'flags'.
725 */
726 static char *
Christian Brabandtb39b2402023-11-29 11:34:05 +0100727did_set_option_listflag(
728 char_u *val,
729 char_u *flags,
730 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +0100731 size_t errbuflen)
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000732{
733 char_u *s;
734
Yegappan Lakshmananc727b192023-03-03 12:26:15 +0000735 for (s = val; *s; ++s)
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000736 if (vim_strchr(flags, *s) == NULL)
Christian Brabandtb39b2402023-11-29 11:34:05 +0100737 return illegal_char(errbuf, errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000738
739 return NULL;
740}
741
742/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200743 * Expand an option that accepts a list of fixed string values with known
744 * number of items.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200745 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200746 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200747expand_set_opt_string(
748 optexpand_T *args,
749 char **values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +0200750 size_t numValues,
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200751 int *numMatches,
752 char_u ***matches)
753{
754 char_u *p;
755 regmatch_T *regmatch = args->oe_regmatch;
756 int include_orig_val = args->oe_include_orig_val;
757 char_u *option_val = args->oe_opt_value;
758
759 // Assume numValues is small since they are fixed enums, so just allocate
760 // upfront instead of needing two passes to calculate output size.
761 *matches = ALLOC_MULT(char_u *, numValues + 1);
762 if (*matches == NULL)
763 return FAIL;
764
765 int count = 0;
766
767 if (include_orig_val && *option_val != NUL)
768 {
769 p = vim_strsave(option_val);
770 if (p == NULL)
771 {
772 VIM_CLEAR(*matches);
773 return FAIL;
774 }
775 (*matches)[count++] = p;
776 }
777
778 for (char **val = values; *val != NULL; val++)
779 {
780 if (include_orig_val && *option_val != NUL)
781 {
782 if (STRCMP((char_u*)*val, option_val) == 0)
783 continue;
784 }
785 if (vim_regexec(regmatch, (char_u*)(*val), (colnr_T)0))
786 {
787 p = vim_strsave((char_u*)*val);
788 if (p == NULL)
789 {
790 if (count == 0)
791 {
792 VIM_CLEAR(*matches);
793 return FAIL;
794 }
795 else
796 break;
797 }
798 (*matches)[count++] = p;
799 }
800 }
801 if (count == 0)
802 {
803 VIM_CLEAR(*matches);
804 return FAIL;
805 }
806 *numMatches = count;
807 return OK;
808}
809
810static char_u *set_opt_callback_orig_option = NULL;
811static char_u *((*set_opt_callback_func)(expand_T *, int));
812
813/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200814 * Callback used by expand_set_opt_generic to also include the original value
815 * as the first item.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200816 */
817 static char_u *
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200818expand_set_opt_generic_cb(expand_T *xp, int idx)
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200819{
820 if (idx == 0)
821 {
822 if (set_opt_callback_orig_option != NULL)
823 return set_opt_callback_orig_option;
824 else
825 return (char_u *)""; // empty strings are ignored
826 }
827 return set_opt_callback_func(xp, idx - 1);
828}
829
830/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200831 * Expand an option with a callback that iterates through a list of possible
832 * names using an index.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200833 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200834 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200835expand_set_opt_generic(
836 optexpand_T *args,
837 char_u *((*func)(expand_T *, int)),
838 int *numMatches,
839 char_u ***matches)
840{
841 int ret;
842
843 set_opt_callback_orig_option = args->oe_include_orig_val ?
844 args->oe_opt_value : NULL;
845 set_opt_callback_func = func;
846
847 ret = ExpandGeneric(
848 (char_u*)"", // not using fuzzy as currently EXPAND_STRING_SETTING doesn't use it
849 args->oe_xp,
850 args->oe_regmatch,
851 matches,
852 numMatches,
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200853 expand_set_opt_generic_cb,
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200854 FALSE);
855
856 set_opt_callback_orig_option = NULL;
857 set_opt_callback_func = NULL;
858 return ret;
859}
860
Christian Brabandt9960ebc2023-10-05 22:17:09 +0200861# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200862static garray_T *expand_cb_ga;
863static optexpand_T *expand_cb_args;
864
865/*
866 * Callback provided to a function in expand_set_opt_callback. Will perform
867 * regex matching against the value and add to the list.
868 *
869 * Returns OK usually. Returns FAIL if it failed to allocate memory, and the
870 * caller should terminate the enumeration.
871 */
872 static int
873expand_set_opt_callback_cb(char_u *val)
874{
875 regmatch_T *regmatch = expand_cb_args->oe_regmatch;
876 expand_T *xp = expand_cb_args->oe_xp;
877 garray_T *ga = expand_cb_ga;
878 char_u *str;
879
880 if (val == NULL || *val == NUL)
881 return OK;
882
883 if (xp->xp_pattern[0] != NUL &&
884 !vim_regexec(regmatch, val, (colnr_T)0))
885 return OK;
886
887 str = vim_strsave_escaped(val, (char_u *)" \t\\");
888
889 if (str == NULL)
890 return FAIL;
891
892 if (ga_grow(ga, 1) == FAIL)
893 {
894 vim_free(str);
895 return FAIL;
896 }
897
898 ((char_u **)ga->ga_data)[ga->ga_len] = str;
899 ++ga->ga_len;
900 return OK;
901}
902
903/*
904 * Expand an option with a provided function that takes a callback. The
905 * function will enumerate through all options and call the callback to add it
906 * to the list.
907 *
908 * "func" is the enumerator function that will generate the list of options.
909 * "func_params" is a single parameter that will be passed to func.
910 */
911 static int
912expand_set_opt_callback(
913 optexpand_T *args,
914 void (*func)(optexpand_T *, void* params, int (*cb)(char_u *val)),
915 void *func_params,
916 int *numMatches,
917 char_u ***matches)
918{
919 garray_T ga;
920 int include_orig_val = args->oe_include_orig_val;
921 char_u *option_val = args->oe_opt_value;
922
923 ga_init2(&ga, sizeof(char *), 30);
924
925 if (include_orig_val && *option_val != NUL)
926 {
927 char_u *p = vim_strsave(option_val);
928 if (p == NULL)
929 return FAIL;
930 if (ga_grow(&ga, 1) == FAIL)
931 {
932 vim_free(p);
933 return FAIL;
934 }
935 ((char_u **)ga.ga_data)[ga.ga_len] = p;
936 ++ga.ga_len;
937 }
938
939 expand_cb_ga = &ga;
940 expand_cb_args = args;
941
942 func(args, func_params, expand_set_opt_callback_cb);
943
944 expand_cb_ga = NULL;
945 expand_cb_args = NULL;
946
947 *matches = ga.ga_data;
948 *numMatches = ga.ga_len;
949 return OK;
950}
Christian Brabandt9960ebc2023-10-05 22:17:09 +0200951#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200952
953/*
954 * Expand an option which is a list of flags.
955 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200956 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200957expand_set_opt_listflag(
958 optexpand_T *args,
959 char_u *flags,
960 int *numMatches,
961 char_u ***matches)
962{
963 char_u *p;
964 char_u *option_val = args->oe_opt_value;
965 char_u *cmdline_val = args->oe_set_arg;
966 int append = args->oe_append;
967 int include_orig_val = args->oe_include_orig_val && (*option_val != NUL);
968
Yee Cheng Chin6d113472023-10-02 21:38:39 +0200969 size_t num_flags = STRLEN(flags);
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200970
971 // Assume we only have small number of flags, so just allocate max size.
972 *matches = ALLOC_MULT(char_u *, num_flags + 1);
973 if (*matches == NULL)
974 return FAIL;
975
976 int count = 0;
977
978 if (include_orig_val)
979 {
980 p = vim_strsave(option_val);
981 if (p == NULL)
982 {
983 VIM_CLEAR(*matches);
984 return FAIL;
985 }
986 (*matches)[count++] = p;
987 }
988
989 for (char_u *flag = flags; *flag != NUL; flag++)
990 {
991 if (append && vim_strchr(option_val, *flag) != NULL)
992 continue;
993
994 if (vim_strchr(cmdline_val, *flag) == NULL)
995 {
996 if (include_orig_val
997 && option_val[1] == NUL
998 && *flag == option_val[0])
999 {
1000 // This value is already used as the first choice as it's the
1001 // existing flag. Just skip it to avoid duplicate.
1002 continue;
1003 }
1004 p = vim_strnsave(flag, 1);
1005 if (p == NULL)
1006 {
1007 if (count == 0)
1008 {
1009 VIM_CLEAR(*matches);
1010 return FAIL;
1011 }
1012 else
1013 break;
1014 }
1015 (*matches)[count++] = p;
1016 }
1017 }
1018
1019 if (count == 0)
1020 {
1021 VIM_CLEAR(*matches);
1022 return FAIL;
1023 }
1024 *numMatches = count;
1025 return OK;
1026}
1027
1028/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001029 * The 'ambiwidth' option is changed.
1030 */
1031 char *
1032did_set_ambiwidth(optset_T *args UNUSED)
1033{
1034 if (check_opt_strings(p_ambw, p_ambw_values, FALSE) != OK)
1035 return e_invalid_argument;
1036
1037 return check_chars_options();
1038}
1039
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001040 int
1041expand_set_ambiwidth(optexpand_T *args, int *numMatches, char_u ***matches)
1042{
1043 return expand_set_opt_string(
1044 args,
1045 p_ambw_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001046 ARRAY_LENGTH(p_ambw_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001047 numMatches,
1048 matches);
1049}
1050
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001051/*
1052 * The 'background' option is changed.
1053 */
1054 char *
Gregory Anders83ad2722024-01-03 19:48:51 +01001055did_set_background(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001056{
1057 if (check_opt_strings(p_bg, p_bg_values, FALSE) == FAIL)
1058 return e_invalid_argument;
1059
Gregory Anders83ad2722024-01-03 19:48:51 +01001060 if (args->os_oldval.string != NULL && args->os_oldval.string[0] == *p_bg)
1061 // Value was not changed
1062 return NULL;
1063
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001064#ifdef FEAT_EVAL
1065 int dark = (*p_bg == 'd');
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001066#endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001067
1068 init_highlight(FALSE, FALSE);
1069
1070#ifdef FEAT_EVAL
1071 if (dark != (*p_bg == 'd')
1072 && get_var_value((char_u *)"g:colors_name") != NULL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001073 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001074 // The color scheme must have set 'background' back to another
1075 // value, that's not what we want here. Disable the color
1076 // scheme and set the colors again.
1077 do_unlet((char_u *)"g:colors_name", TRUE);
1078 free_string_option(p_bg);
1079 p_bg = vim_strsave((char_u *)(dark ? "dark" : "light"));
1080 check_string_option(&p_bg);
1081 init_highlight(FALSE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001082 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001083#endif
1084#ifdef FEAT_TERMINAL
1085 term_update_colors_all();
1086#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001087
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001088 return NULL;
1089}
1090
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001091 int
1092expand_set_background(optexpand_T *args, int *numMatches, char_u ***matches)
1093{
1094 return expand_set_opt_string(
1095 args,
1096 p_bg_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001097 ARRAY_LENGTH(p_bg_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001098 numMatches,
1099 matches);
1100}
1101
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001102/*
1103 * The 'backspace' option is changed.
1104 */
1105 char *
1106did_set_backspace(optset_T *args UNUSED)
1107{
1108 if (VIM_ISDIGIT(*p_bs))
1109 {
1110 if (*p_bs > '3' || p_bs[1] != NUL)
1111 return e_invalid_argument;
1112 }
1113 else if (check_opt_strings(p_bs, p_bs_values, TRUE) != OK)
1114 return e_invalid_argument;
1115
1116 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001117}
1118
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001119 int
1120expand_set_backspace(optexpand_T *args, int *numMatches, char_u ***matches)
1121{
1122 return expand_set_opt_string(
1123 args,
1124 p_bs_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001125 ARRAY_LENGTH(p_bs_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001126 numMatches,
1127 matches);
1128}
1129
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001130/*
1131 * The 'backupcopy' option is changed.
1132 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001133 char *
1134did_set_backupcopy(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001135{
1136 char_u *bkc = p_bkc;
1137 unsigned int *flags = &bkc_flags;
1138 char *errmsg = NULL;
1139
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001140 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001141 {
1142 bkc = curbuf->b_p_bkc;
1143 flags = &curbuf->b_bkc_flags;
1144 }
1145
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001146 if ((args->os_flags & OPT_LOCAL) && *bkc == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001147 // make the local value empty: use the global value
1148 *flags = 0;
1149 else
1150 {
1151 if (opt_strings_flags(bkc, p_bkc_values, flags, TRUE) != OK)
1152 errmsg = e_invalid_argument;
1153 if ((((int)*flags & BKC_AUTO) != 0)
1154 + (((int)*flags & BKC_YES) != 0)
1155 + (((int)*flags & BKC_NO) != 0) != 1)
1156 {
1157 // Must have exactly one of "auto", "yes" and "no".
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001158 (void)opt_strings_flags(args->os_oldval.string, p_bkc_values,
1159 flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001160 errmsg = e_invalid_argument;
1161 }
1162 }
1163
1164 return errmsg;
1165}
1166
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001167 int
1168expand_set_backupcopy(optexpand_T *args, int *numMatches, char_u ***matches)
1169{
1170 return expand_set_opt_string(
1171 args,
1172 p_bkc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001173 ARRAY_LENGTH(p_bkc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001174 numMatches,
1175 matches);
1176}
1177
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001178/*
1179 * The 'backupext' or the 'patchmode' option is changed.
1180 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001181 char *
1182did_set_backupext_or_patchmode(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001183{
1184 if (STRCMP(*p_bex == '.' ? p_bex + 1 : p_bex,
1185 *p_pm == '.' ? p_pm + 1 : p_pm) == 0)
1186 return e_backupext_and_patchmode_are_equal;
1187
1188 return NULL;
1189}
1190
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001191/*
1192 * The 'belloff' option is changed.
1193 */
1194 char *
1195did_set_belloff(optset_T *args UNUSED)
1196{
1197 return did_set_opt_flags(p_bo, p_bo_values, &bo_flags, TRUE);
1198}
1199
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001200 int
1201expand_set_belloff(optexpand_T *args, int *numMatches, char_u ***matches)
1202{
1203 return expand_set_opt_string(
1204 args,
1205 p_bo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001206 ARRAY_LENGTH(p_bo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001207 numMatches,
1208 matches);
1209}
1210
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001211#if defined(FEAT_LINEBREAK) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001212/*
zeertzjqeac3fdc2024-02-03 18:08:09 +01001213 * The 'breakat' option is changed.
1214 */
1215 char *
1216did_set_breakat(optset_T *args UNUSED)
1217{
1218 char_u *p;
1219 int i;
1220
1221 for (i = 0; i < 256; i++)
1222 breakat_flags[i] = FALSE;
1223
1224 if (p_breakat != NULL)
1225 for (p = p_breakat; *p; p++)
1226 breakat_flags[*p] = TRUE;
1227
1228 return NULL;
1229}
1230
1231/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001232 * The 'breakindentopt' option is changed.
1233 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001234 char *
1235did_set_breakindentopt(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001236{
1237 char *errmsg = NULL;
1238
1239 if (briopt_check(curwin) == FAIL)
1240 errmsg = e_invalid_argument;
1241 // list setting requires a redraw
1242 if (curwin->w_briopt_list)
1243 redraw_all_later(UPD_NOT_VALID);
1244
1245 return errmsg;
1246}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001247
1248 int
1249expand_set_breakindentopt(optexpand_T *args, int *numMatches, char_u ***matches)
1250{
1251 return expand_set_opt_string(
1252 args,
1253 p_briopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001254 ARRAY_LENGTH(p_briopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001255 numMatches,
1256 matches);
1257}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001258#endif
1259
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001260#if defined(FEAT_BROWSE) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001261/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001262 * The 'browsedir' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001263 */
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00001264 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001265did_set_browsedir(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001266{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001267 if (check_opt_strings(p_bsdir, p_bsdir_values, FALSE) != OK
1268 && !mch_isdir(p_bsdir))
1269 return e_invalid_argument;
1270
1271 return NULL;
1272}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001273
1274 int
1275expand_set_browsedir(optexpand_T *args, int *numMatches, char_u ***matches)
1276{
1277 return expand_set_opt_string(
1278 args,
1279 p_bsdir_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001280 ARRAY_LENGTH(p_bsdir_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001281 numMatches,
1282 matches);
1283}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001284#endif
1285
1286/*
1287 * The 'bufhidden' option is changed.
1288 */
1289 char *
1290did_set_bufhidden(optset_T *args UNUSED)
1291{
1292 return did_set_opt_strings(curbuf->b_p_bh, p_bufhidden_values, FALSE);
1293}
1294
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001295 int
1296expand_set_bufhidden(optexpand_T *args, int *numMatches, char_u ***matches)
1297{
1298 return expand_set_opt_string(
1299 args,
1300 p_bufhidden_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001301 ARRAY_LENGTH(p_bufhidden_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001302 numMatches,
1303 matches);
1304}
1305
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001306/*
1307 * The 'buftype' option is changed.
1308 */
1309 char *
1310did_set_buftype(optset_T *args UNUSED)
1311{
1312 if (check_opt_strings(curbuf->b_p_bt, p_buftype_values, FALSE) != OK)
1313 return e_invalid_argument;
1314
1315 if (curwin->w_status_height)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001316 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001317 curwin->w_redr_status = TRUE;
1318 redraw_later(UPD_VALID);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001319 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001320 curbuf->b_help = (curbuf->b_p_bt[0] == 'h');
1321 redraw_titles();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001322
1323 return NULL;
1324}
1325
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001326 int
1327expand_set_buftype(optexpand_T *args, int *numMatches, char_u ***matches)
1328{
1329 return expand_set_opt_string(
1330 args,
1331 p_buftype_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001332 ARRAY_LENGTH(p_buftype_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001333 numMatches,
1334 matches);
1335}
1336
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001337/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001338 * The 'casemap' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001339 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001340 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001341did_set_casemap(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001342{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001343 return did_set_opt_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
1344}
1345
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001346 int
1347expand_set_casemap(optexpand_T *args, int *numMatches, char_u ***matches)
1348{
1349 return expand_set_opt_string(
1350 args,
1351 p_cmp_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001352 ARRAY_LENGTH(p_cmp_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001353 numMatches,
1354 matches);
1355}
1356
1357#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1358 int
1359expand_set_clipboard(optexpand_T *args, int *numMatches, char_u ***matches)
1360{
1361 return expand_set_opt_string(
1362 args,
1363 p_cb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001364 ARRAY_LENGTH(p_cb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001365 numMatches,
1366 matches);
1367}
1368#endif
1369
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001370/*
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001371 * The global 'listchars' or 'fillchars' option is changed.
1372 */
1373 static char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01001374did_set_global_listfillchars(char_u *val, int opt_lcs, int opt_flags,
1375 char *errbuf, size_t errbuflen)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001376{
1377 char *errmsg = NULL;
1378 char_u **local_ptr = opt_lcs ? &curwin->w_p_lcs : &curwin->w_p_fcs;
1379
1380 // only apply the global value to "curwin" when it does not have a
1381 // local value
1382 if (opt_lcs)
1383 errmsg = set_listchars_option(curwin, val,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001384 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL),
1385 errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001386 else
1387 errmsg = set_fillchars_option(curwin, val,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001388 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL),
1389 errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001390 if (errmsg != NULL)
1391 return errmsg;
1392
1393 tabpage_T *tp;
1394 win_T *wp;
1395
1396 // If the current window is set to use the global
1397 // 'listchars'/'fillchars' value, clear the window-local value.
1398 if (!(opt_flags & OPT_GLOBAL))
1399 clear_string_option(local_ptr);
1400 FOR_ALL_TAB_WINDOWS(tp, wp)
1401 {
1402 // If the current window has a local value need to apply it
1403 // again, it was changed when setting the global value.
1404 // If no error was returned above, we don't expect an error
1405 // here, so ignore the return value.
1406 if (opt_lcs)
1407 {
1408 if (*wp->w_p_lcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001409 (void)set_listchars_option(wp, wp->w_p_lcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001410 }
1411 else
1412 {
1413 if (*wp->w_p_fcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001414 (void)set_fillchars_option(wp, wp->w_p_fcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001415 }
1416 }
1417
1418 redraw_all_later(UPD_NOT_VALID);
1419
1420 return NULL;
1421}
1422
1423/*
1424 * The 'fillchars' option or the 'listchars' option is changed.
1425 */
1426 char *
1427did_set_chars_option(optset_T *args)
1428{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001429 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001430 char *errmsg = NULL;
1431
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001432 if ( varp == &p_lcs // global 'listchars'
1433 || varp == &p_fcs) // global 'fillchars'
1434 errmsg = did_set_global_listfillchars(*varp, varp == &p_lcs,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001435 args->os_flags, args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001436 else if (varp == &curwin->w_p_lcs) // local 'listchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001437 errmsg = set_listchars_option(curwin, *varp, TRUE,
1438 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001439 else if (varp == &curwin->w_p_fcs) // local 'fillchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001440 errmsg = set_fillchars_option(curwin, *varp, TRUE,
1441 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001442
1443 return errmsg;
1444}
1445
1446/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001447 * Expand 'fillchars' or 'listchars' option value.
1448 */
1449 int
1450expand_set_chars_option(optexpand_T *args, int *numMatches, char_u ***matches)
1451{
1452 char_u **varp = (char_u **)args->oe_varp;
1453 int is_lcs = (varp == &p_lcs || varp == &curwin->w_p_lcs);
1454 return expand_set_opt_generic(
1455 args,
1456 is_lcs ? get_listchars_name : get_fillchars_name,
1457 numMatches,
1458 matches);
1459}
1460
1461/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001462 * The 'cinoptions' option is changed.
1463 */
1464 char *
1465did_set_cinoptions(optset_T *args UNUSED)
1466{
1467 // TODO: recognize errors
1468 parse_cino(curbuf);
1469
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001470 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001471}
1472
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00001473#if defined(FEAT_SYN_HL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001474/*
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001475 * The 'colorcolumn' option is changed.
1476 */
1477 char *
1478did_set_colorcolumn(optset_T *args UNUSED)
1479{
1480 return check_colorcolumn(curwin);
1481}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001482#endif
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001483
1484/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001485 * The 'comments' option is changed.
1486 */
1487 char *
1488did_set_comments(optset_T *args)
1489{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001490 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001491 char_u *s;
1492 char *errmsg = NULL;
1493
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001494 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001495 {
1496 while (*s && *s != ':')
1497 {
1498 if (vim_strchr((char_u *)COM_ALL, *s) == NULL
1499 && !VIM_ISDIGIT(*s) && *s != '-')
1500 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01001501 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001502 break;
1503 }
1504 ++s;
1505 }
1506 if (*s++ == NUL)
1507 errmsg = e_missing_colon;
1508 else if (*s == ',' || *s == NUL)
1509 errmsg = e_zero_length_string;
1510 if (errmsg != NULL)
1511 break;
1512 while (*s && *s != ',')
1513 {
1514 if (*s == '\\' && s[1] != NUL)
1515 ++s;
1516 ++s;
1517 }
1518 s = skip_to_option_part(s);
1519 }
1520
1521 return errmsg;
1522}
1523
1524#if defined(FEAT_FOLDING) || defined(PROTO)
1525/*
1526 * The 'commentstring' option is changed.
1527 */
1528 char *
1529did_set_commentstring(optset_T *args)
1530{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001531 char_u **varp = (char_u **)args->os_varp;
1532
1533 if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001534 return e_commentstring_must_be_empty_or_contain_str;
1535
1536 return NULL;
1537}
1538#endif
1539
1540/*
1541 * The 'complete' option is changed.
1542 */
1543 char *
1544did_set_complete(optset_T *args)
1545{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001546 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001547 char_u *s;
1548
1549 // check if it is a valid value for 'complete' -- Acevedo
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001550 for (s = *varp; *s;)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001551 {
1552 while (*s == ',' || *s == ' ')
1553 s++;
1554 if (!*s)
1555 break;
1556 if (vim_strchr((char_u *)".wbuksid]tU", *s) == NULL)
Christian Brabandtb39b2402023-11-29 11:34:05 +01001557 return illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001558 if (*++s != NUL && *s != ',' && *s != ' ')
1559 {
1560 if (s[-1] == 'k' || s[-1] == 's')
1561 {
1562 // skip optional filename after 'k' and 's'
1563 while (*s && *s != ',' && *s != ' ')
1564 {
1565 if (*s == '\\' && s[1] != NUL)
1566 ++s;
1567 ++s;
1568 }
1569 }
1570 else
1571 {
1572 if (args->os_errbuf != NULL)
1573 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01001574 vim_snprintf((char *)args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001575 _(e_illegal_character_after_chr), *--s);
1576 return args->os_errbuf;
1577 }
1578 return "";
1579 }
1580 }
1581 }
1582
1583 return NULL;
1584}
1585
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001586 int
1587expand_set_complete(optexpand_T *args, int *numMatches, char_u ***matches)
1588{
1589 static char *(p_cpt_values[]) = {
1590 ".", "w", "b", "u", "k", "kspell", "s", "i", "d", "]", "t", "U",
1591 NULL};
1592 return expand_set_opt_string(
1593 args,
1594 p_cpt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001595 ARRAY_LENGTH(p_cpt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001596 numMatches,
1597 matches);
1598}
1599
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001600/*
1601 * The 'completeopt' option is changed.
1602 */
1603 char *
1604did_set_completeopt(optset_T *args UNUSED)
1605{
zeertzjq529b9ad2024-06-05 20:27:06 +02001606 char_u *cot = p_cot;
1607 unsigned *flags = &cot_flags;
1608
1609 if (args->os_flags & OPT_LOCAL)
1610 {
1611 cot = curbuf->b_p_cot;
1612 flags = &curbuf->b_cot_flags;
1613 }
1614
1615 if (check_opt_strings(cot, p_cot_values, TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001616 return e_invalid_argument;
1617
zeertzjq529b9ad2024-06-05 20:27:06 +02001618 if (opt_strings_flags(cot, p_cot_values, flags, TRUE) != OK)
1619 return e_invalid_argument;
1620
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001621 return NULL;
1622}
1623
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001624 int
1625expand_set_completeopt(optexpand_T *args, int *numMatches, char_u ***matches)
1626{
1627 return expand_set_opt_string(
1628 args,
1629 p_cot_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001630 ARRAY_LENGTH(p_cot_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001631 numMatches,
1632 matches);
1633}
1634
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001635#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1636/*
1637 * The 'completepopup' option is changed.
1638 */
1639 char *
1640did_set_completepopup(optset_T *args UNUSED)
1641{
1642 if (parse_completepopup(NULL) == FAIL)
1643 return e_invalid_argument;
1644
1645 popup_close_info();
1646 return NULL;
1647}
1648#endif
1649
1650#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1651/*
1652 * The 'completeslash' option is changed.
1653 */
1654 char *
1655did_set_completeslash(optset_T *args UNUSED)
1656{
1657 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1658 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
1659 return e_invalid_argument;
1660
1661 return NULL;
1662}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001663
1664 int
1665expand_set_completeslash(optexpand_T *args, int *numMatches, char_u ***matches)
1666{
1667 return expand_set_opt_string(
1668 args,
1669 p_csl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001670 ARRAY_LENGTH(p_csl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001671 numMatches,
1672 matches);
1673}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001674#endif
1675
1676#if defined(FEAT_CONCEAL) || defined(PROTO)
1677/*
1678 * The 'concealcursor' option is changed.
1679 */
1680 char *
1681did_set_concealcursor(optset_T *args)
1682{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001683 char_u **varp = (char_u **)args->os_varp;
1684
Christian Brabandtb39b2402023-11-29 11:34:05 +01001685 return did_set_option_listflag(*varp, (char_u *)COCU_ALL, args->os_errbuf,
1686 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001687}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001688
1689 int
1690expand_set_concealcursor(optexpand_T *args, int *numMatches, char_u ***matches)
1691{
1692 return expand_set_opt_listflag(args, (char_u*)COCU_ALL, numMatches, matches);
1693}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001694#endif
1695
1696/*
1697 * The 'cpoptions' option is changed.
1698 */
1699 char *
1700did_set_cpoptions(optset_T *args)
1701{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001702 char_u **varp = (char_u **)args->os_varp;
1703
Christian Brabandtb39b2402023-11-29 11:34:05 +01001704 return did_set_option_listflag(*varp, (char_u *)CPO_ALL, args->os_errbuf,
1705 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001706}
1707
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001708 int
1709expand_set_cpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
1710{
1711 return expand_set_opt_listflag(args, (char_u*)CPO_ALL, numMatches, matches);
1712}
1713
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001714#if defined(FEAT_CRYPT) || defined(PROTO)
1715/*
1716 * The 'cryptkey' option is changed.
1717 */
1718 char *
1719did_set_cryptkey(optset_T *args)
1720{
1721 // Make sure the ":set" command doesn't show the new value in the
1722 // history.
1723 remove_key_from_history();
1724
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02001725 if (args->os_op != OP_NONE)
1726 // Don't allow set+=/-=/^= as they can allow for substring guessing
1727 return e_invalid_argument;
1728
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001729 if (STRCMP(curbuf->b_p_key, args->os_oldval.string) != 0)
1730 {
1731 // Need to update the swapfile.
1732 ml_set_crypt_key(curbuf, args->os_oldval.string,
1733 *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
1734 changed_internal();
1735 }
Christian Brabandt19e6c4f2023-06-27 18:57:10 +01001736# ifdef FEAT_SODIUM
1737 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
1738 crypt_sodium_lock_key(args->os_newval.string);
1739# endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001740
1741 return NULL;
1742}
1743
1744/*
1745 * The 'cryptmethod' option is changed.
1746 */
1747 char *
1748did_set_cryptmethod(optset_T *args)
1749{
1750 char_u *p;
1751 char_u *s;
1752
1753 if (args->os_flags & OPT_LOCAL)
1754 p = curbuf->b_p_cm;
1755 else
1756 p = p_cm;
1757 if (check_opt_strings(p, p_cm_values, TRUE) != OK)
1758 return e_invalid_argument;
1759 else if (crypt_self_test() == FAIL)
1760 return e_invalid_argument;
1761
1762 // When setting the global value to empty, make it "zip".
1763 if (*p_cm == NUL)
1764 {
1765 free_string_option(p_cm);
1766 p_cm = vim_strsave((char_u *)"zip");
1767 }
1768 // When using ":set cm=name" the local value is going to be empty.
1769 // Do that here, otherwise the crypt functions will still use the
1770 // local value.
1771 if ((args->os_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1772 {
1773 free_string_option(curbuf->b_p_cm);
1774 curbuf->b_p_cm = empty_option;
1775 }
1776
1777 // Need to update the swapfile when the effective method changed.
1778 // Set "s" to the effective old value, "p" to the effective new
1779 // method and compare.
1780 if ((args->os_flags & OPT_LOCAL) && *args->os_oldval.string == NUL)
1781 s = p_cm; // was previously using the global value
1782 else
1783 s = args->os_oldval.string;
1784 if (*curbuf->b_p_cm == NUL)
1785 p = p_cm; // is now using the global value
1786 else
1787 p = curbuf->b_p_cm;
1788 if (STRCMP(s, p) != 0)
1789 ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1790
1791 // If the global value changes need to update the swapfile for all
1792 // buffers using that value.
1793 if ((args->os_flags & OPT_GLOBAL)
1794 && STRCMP(p_cm, args->os_oldval.string) != 0)
1795 {
1796 buf_T *buf;
1797
1798 FOR_ALL_BUFFERS(buf)
1799 if (buf != curbuf && *buf->b_p_cm == NUL)
1800 ml_set_crypt_key(buf, buf->b_p_key, args->os_oldval.string);
1801 }
1802 return NULL;
1803}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001804
1805 int
1806expand_set_cryptmethod(optexpand_T *args, int *numMatches, char_u ***matches)
1807{
1808 return expand_set_opt_string(
1809 args,
1810 p_cm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001811 ARRAY_LENGTH(p_cm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001812 numMatches,
1813 matches);
1814}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001815#endif
1816
1817#if (defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1818/*
1819 * The 'cscopequickfix' option is changed.
1820 */
1821 char *
1822did_set_cscopequickfix(optset_T *args UNUSED)
1823{
1824 char_u *p;
1825
1826 if (p_csqf == NULL)
1827 return NULL;
1828
1829 p = p_csqf;
1830 while (*p != NUL)
1831 {
1832 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
1833 || p[1] == NUL
1834 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
1835 || (p[2] != NUL && p[2] != ','))
1836 return e_invalid_argument;
1837 else if (p[2] == NUL)
1838 break;
1839 else
1840 p += 3;
1841 }
1842
1843 return NULL;
1844}
1845#endif
1846
1847#if defined(FEAT_SYN_HL) || defined(PROTO)
1848/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001849 * The 'cursorlineopt' option is changed.
1850 */
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001851 char *
1852did_set_cursorlineopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001853{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001854 char_u **varp = (char_u **)args->os_varp;
1855
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001856 // This could be changed to use opt_strings_flags() instead.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001857 if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001858 return e_invalid_argument;
1859
1860 return NULL;
1861}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001862
1863 int
1864expand_set_cursorlineopt(optexpand_T *args, int *numMatches, char_u ***matches)
1865{
1866 return expand_set_opt_string(
1867 args,
1868 p_culopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001869 ARRAY_LENGTH(p_culopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001870 numMatches,
1871 matches);
1872}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001873#endif
1874
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001875/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001876 * The 'debug' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001877 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001878 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001879did_set_debug(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001880{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001881 return did_set_opt_strings(p_debug, p_debug_values, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001882}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001883
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001884 int
1885expand_set_debug(optexpand_T *args, int *numMatches, char_u ***matches)
1886{
1887 return expand_set_opt_string(
1888 args,
1889 p_debug_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001890 ARRAY_LENGTH(p_debug_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001891 numMatches,
1892 matches);
1893}
1894
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001895#if defined(FEAT_DIFF) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001896/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001897 * The 'diffopt' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001898 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001899 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001900did_set_diffopt(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001901{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001902 if (diffopt_changed() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001903 return e_invalid_argument;
1904
1905 return NULL;
1906}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001907
1908 int
1909expand_set_diffopt(optexpand_T *args, int *numMatches, char_u ***matches)
1910{
1911 expand_T *xp = args->oe_xp;
1912
1913 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
1914 {
1915 // Within "algorithm:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001916 int algo_len = (int)STRLEN("algorithm:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001917 if (xp->xp_pattern - args->oe_set_arg >= algo_len &&
1918 STRNCMP(xp->xp_pattern - algo_len, "algorithm:", algo_len) == 0)
1919 {
1920 return expand_set_opt_string(
1921 args,
1922 p_dip_algorithm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001923 ARRAY_LENGTH(p_dip_algorithm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001924 numMatches,
1925 matches);
1926 }
1927 return FAIL;
1928 }
1929
1930 return expand_set_opt_string(
1931 args,
1932 p_dip_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001933 ARRAY_LENGTH(p_dip_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001934 numMatches,
1935 matches);
1936}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001937#endif
1938
1939/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001940 * The 'display' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001941 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001942 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001943did_set_display(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001944{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001945 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001946 return e_invalid_argument;
1947
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001948 (void)init_chartab();
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001949 return NULL;
1950}
1951
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001952 int
1953expand_set_display(optexpand_T *args, int *numMatches, char_u ***matches)
1954{
1955 return expand_set_opt_string(
1956 args,
1957 p_dy_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001958 ARRAY_LENGTH(p_dy_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001959 numMatches,
1960 matches);
1961}
1962
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001963/*
1964 * The 'eadirection' option is changed.
1965 */
1966 char *
1967did_set_eadirection(optset_T *args UNUSED)
1968{
1969 return did_set_opt_strings(p_ead, p_ead_values, FALSE);
1970}
1971
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001972 int
1973expand_set_eadirection(optexpand_T *args, int *numMatches, char_u ***matches)
1974{
1975 return expand_set_opt_string(
1976 args,
1977 p_ead_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001978 ARRAY_LENGTH(p_ead_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001979 numMatches,
1980 matches);
1981}
1982
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001983/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001984 * One of the 'encoding', 'fileencoding', 'termencoding' or 'makeencoding'
1985 * options is changed.
1986 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001987 char *
1988did_set_encoding(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001989{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001990 char_u **varp = (char_u **)args->os_varp;
1991 char_u **gvarp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001992 char *errmsg = NULL;
1993 char_u *p;
1994
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001995 // Get the global option to compare with, otherwise we would have to check
1996 // two values for all local options.
1997 gvarp = (char_u **)get_option_varp_scope(args->os_idx, OPT_GLOBAL);
1998
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001999 if (gvarp == &p_fenc)
2000 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002001 if (!curbuf->b_p_ma && args->os_flags != OPT_GLOBAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002002 errmsg = e_cannot_make_changes_modifiable_is_off;
2003 else if (vim_strchr(*varp, ',') != NULL)
2004 // No comma allowed in 'fileencoding'; catches confusing it
2005 // with 'fileencodings'.
2006 errmsg = e_invalid_argument;
2007 else
2008 {
2009 // May show a "+" in the title now.
2010 redraw_titles();
2011 // Add 'fileencoding' to the swap file.
2012 ml_setflags(curbuf);
2013 }
2014 }
2015 if (errmsg == NULL)
2016 {
2017 // canonize the value, so that STRCMP() can be used on it
2018 p = enc_canonize(*varp);
2019 if (p != NULL)
2020 {
2021 vim_free(*varp);
2022 *varp = p;
2023 }
2024 if (varp == &p_enc)
2025 {
2026 errmsg = mb_init();
2027 redraw_titles();
2028 }
2029 }
2030
2031#if defined(FEAT_GUI_GTK)
2032 if (errmsg == NULL && varp == &p_tenc && gui.in_use)
2033 {
2034 // GTK uses only a single encoding, and that is UTF-8.
2035 if (STRCMP(p_tenc, "utf-8") != 0)
2036 errmsg = e_cannot_be_changed_in_gtk_GUI;
2037 }
2038#endif
2039
2040 if (errmsg == NULL)
2041 {
2042#ifdef FEAT_KEYMAP
2043 // When 'keymap' is used and 'encoding' changes, reload the keymap
2044 // (with another encoding).
2045 if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
2046 (void)keymap_init();
2047#endif
2048
2049 // When 'termencoding' is not empty and 'encoding' changes or when
2050 // 'termencoding' changes, need to setup for keyboard input and
2051 // display output conversion.
2052 if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
2053 {
2054 if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
2055 || convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
2056 {
2057 semsg(_(e_cannot_convert_between_str_and_str),
2058 p_tenc, p_enc);
2059 errmsg = e_invalid_argument;
2060 }
2061 }
2062
2063#if defined(MSWIN)
K.Takatace3189d2023-02-15 19:13:43 +00002064 // $HOME, $VIM and $VIMRUNTIME may have characters in active code page.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002065 if (varp == &p_enc)
K.Takatace3189d2023-02-15 19:13:43 +00002066 {
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002067 init_homedir();
K.Takatace3189d2023-02-15 19:13:43 +00002068 init_vimdir();
2069 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002070#endif
2071 }
2072
2073 return errmsg;
2074}
2075
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002076 int
2077expand_set_encoding(optexpand_T *args, int *numMatches, char_u ***matches)
2078{
2079 return expand_set_opt_generic(
2080 args,
2081 get_encoding_name,
2082 numMatches,
2083 matches);
2084}
2085
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002086/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002087 * The 'eventignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002088 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002089 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002090did_set_eventignore(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002091{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002092 if (check_ei() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002093 return e_invalid_argument;
2094 return NULL;
2095}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002096
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002097 static char_u *
2098get_eventignore_name(expand_T *xp, int idx)
2099{
2100 // 'eventignore' allows special keyword "all" in addition to
2101 // all event names.
2102 if (idx == 0)
2103 return (char_u *)"all";
2104 return get_event_name_no_group(xp, idx - 1);
2105}
2106
2107 int
2108expand_set_eventignore(optexpand_T *args, int *numMatches, char_u ***matches)
2109{
2110 return expand_set_opt_generic(
2111 args,
2112 get_eventignore_name,
2113 numMatches,
2114 matches);
2115}
2116
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002117/*
2118 * The 'fileformat' option is changed.
2119 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002120 char *
2121did_set_fileformat(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002122{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002123 char_u **varp = (char_u **)args->os_varp;
2124
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002125 if (!curbuf->b_p_ma && !(args->os_flags & OPT_GLOBAL))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002126 return e_cannot_make_changes_modifiable_is_off;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002127 else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002128 return e_invalid_argument;
2129
2130 // may also change 'textmode'
2131 if (get_fileformat(curbuf) == EOL_DOS)
2132 curbuf->b_p_tx = TRUE;
2133 else
2134 curbuf->b_p_tx = FALSE;
2135 redraw_titles();
2136 // update flag in swap file
2137 ml_setflags(curbuf);
2138 // Redraw needed when switching to/from "mac": a CR in the text
2139 // will be displayed differently.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002140 if (get_fileformat(curbuf) == EOL_MAC || *args->os_oldval.string == 'm')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002141 redraw_curbuf_later(UPD_NOT_VALID);
2142
2143 return NULL;
2144}
2145
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002146 int
2147expand_set_fileformat(optexpand_T *args, int *numMatches, char_u ***matches)
2148{
2149 return expand_set_opt_string(
2150 args,
2151 p_ff_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002152 ARRAY_LENGTH(p_ff_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002153 numMatches,
2154 matches);
2155}
2156
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002157/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002158 * Function given to ExpandGeneric() to obtain the possible arguments of the
2159 * fileformat options.
2160 */
2161 char_u *
2162get_fileformat_name(expand_T *xp UNUSED, int idx)
2163{
2164 if (idx >= (int)ARRAY_LENGTH(p_ff_values))
2165 return NULL;
2166
2167 return (char_u*)p_ff_values[idx];
2168}
2169
2170/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002171 * The 'fileformats' option is changed.
2172 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002173 char *
2174did_set_fileformats(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002175{
2176 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
2177 return e_invalid_argument;
2178
2179 // also change 'textauto'
2180 if (*p_ffs == NUL)
2181 p_ta = FALSE;
2182 else
2183 p_ta = TRUE;
2184
2185 return NULL;
2186}
2187
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002188/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002189 * The 'filetype' or the 'syntax' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002190 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002191 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002192did_set_filetype_or_syntax(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002193{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002194 char_u **varp = (char_u **)args->os_varp;
2195
2196 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002197 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002198
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002199 args->os_value_changed = STRCMP(args->os_oldval.string, *varp) != 0;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002200
2201 // Since we check the value, there is no need to set P_INSECURE,
2202 // even when the value comes from a modeline.
2203 args->os_value_checked = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002204
2205 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002206}
2207
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002208#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002209/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002210 * The 'foldclose' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002211 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002212 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002213did_set_foldclose(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002214{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002215 return did_set_opt_strings(p_fcl, p_fcl_values, TRUE);
2216}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002217
2218 int
2219expand_set_foldclose(optexpand_T *args, int *numMatches, char_u ***matches)
2220{
2221 return expand_set_opt_string(
2222 args,
2223 p_fcl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002224 ARRAY_LENGTH(p_fcl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002225 numMatches,
2226 matches);
2227}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002228#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002229
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002230#if (defined(FEAT_EVAL) && defined(FEAT_FOLDING)) || defined(PROTO)
2231/*
2232 * The 'foldexpr' option is changed.
2233 */
2234 char *
2235did_set_foldexpr(optset_T *args)
2236{
2237 (void)did_set_optexpr(args);
2238 if (foldmethodIsExpr(curwin))
2239 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002240 return NULL;
2241}
2242#endif
2243
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002244#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002245/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002246 * The 'foldignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002247 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002248 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002249did_set_foldignore(optset_T *args UNUSED)
2250{
2251 if (foldmethodIsIndent(curwin))
2252 foldUpdateAll(curwin);
2253 return NULL;
2254}
2255
2256/*
2257 * The 'foldmarker' option is changed.
2258 */
2259 char *
2260did_set_foldmarker(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002261{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002262 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002263 char_u *p;
2264
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002265 p = vim_strchr(*varp, ',');
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002266 if (p == NULL)
2267 return e_comma_required;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002268 else if (p == *varp || p[1] == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002269 return e_invalid_argument;
2270 else if (foldmethodIsMarker(curwin))
2271 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002272
2273 return NULL;
2274}
2275
2276/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002277 * The 'foldmethod' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002278 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002279 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002280did_set_foldmethod(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002281{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002282 char_u **varp = (char_u **)args->os_varp;
2283
2284 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002285 || *curwin->w_p_fdm == NUL)
2286 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002287
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002288 foldUpdateAll(curwin);
2289 if (foldmethodIsDiff(curwin))
2290 newFoldLevel();
2291 return NULL;
2292}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002293
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002294 int
2295expand_set_foldmethod(optexpand_T *args, int *numMatches, char_u ***matches)
2296{
2297 return expand_set_opt_string(
2298 args,
2299 p_fdm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002300 ARRAY_LENGTH(p_fdm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002301 numMatches,
2302 matches);
2303}
2304
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002305/*
2306 * The 'foldopen' option is changed.
2307 */
2308 char *
2309did_set_foldopen(optset_T *args UNUSED)
2310{
2311 return did_set_opt_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
2312}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002313
2314 int
2315expand_set_foldopen(optexpand_T *args, int *numMatches, char_u ***matches)
2316{
2317 return expand_set_opt_string(
2318 args,
2319 p_fdo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002320 ARRAY_LENGTH(p_fdo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002321 numMatches,
2322 matches);
2323}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002324#endif
2325
2326/*
2327 * The 'formatoptions' option is changed.
2328 */
2329 char *
2330did_set_formatoptions(optset_T *args)
2331{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002332 char_u **varp = (char_u **)args->os_varp;
2333
Christian Brabandtb39b2402023-11-29 11:34:05 +01002334 return did_set_option_listflag(*varp, (char_u *)FO_ALL, args->os_errbuf,
2335 args->os_errbuflen);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002336}
2337
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002338 int
2339expand_set_formatoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2340{
2341 return expand_set_opt_listflag(args, (char_u*)FO_ALL, numMatches, matches);
2342}
2343
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002344#if defined(CURSOR_SHAPE) || defined(PROTO)
2345/*
2346 * The 'guicursor' option is changed.
2347 */
2348 char *
2349did_set_guicursor(optset_T *args UNUSED)
2350{
2351 return parse_shape_opt(SHAPE_CURSOR);
2352}
2353#endif
2354
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002355#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002356/*
2357 * The 'guifont' option is changed.
2358 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002359 char *
2360did_set_guifont(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002361{
2362 char_u *p;
2363 char *errmsg = NULL;
2364
2365 if (gui.in_use)
2366 {
2367 p = p_guifont;
2368# if defined(FEAT_GUI_GTK)
2369 // Put up a font dialog and let the user select a new value.
2370 // If this is cancelled go back to the old value but don't
2371 // give an error message.
2372 if (STRCMP(p, "*") == 0)
2373 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002374 p = gui_mch_font_dialog(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002375 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002376 p_guifont = (p != NULL) ? p : vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002377 }
2378# endif
2379 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
2380 {
2381# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
2382 if (STRCMP(p_guifont, "*") == 0)
2383 {
2384 // Dialog was cancelled: Keep the old value without giving
2385 // an error message.
2386 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002387 p_guifont = vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002388 }
2389 else
2390# endif
2391 errmsg = e_invalid_fonts;
2392 }
2393 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002394
2395 return errmsg;
2396}
2397
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002398/*
2399 * Expand the 'guifont' option. Only when GUI is being used. Each platform has
2400 * specific behaviors.
2401 */
2402 int
2403expand_set_guifont(optexpand_T *args, int *numMatches, char_u ***matches)
2404{
2405 if (!gui.in_use)
2406 return FAIL;
2407
2408# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
2409 char_u **varp = (char_u **)args->oe_varp;
2410 int wide = (varp == &p_guifontwide);
2411
2412 return expand_set_opt_callback(
2413 args, gui_mch_expand_font, &wide, numMatches, matches);
2414# else
2415 return FAIL;
2416# endif
2417}
2418
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002419# if defined(FEAT_XFONTSET) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002420/*
2421 * The 'guifontset' option is changed.
2422 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002423 char *
2424did_set_guifontset(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002425{
2426 char *errmsg = NULL;
2427
2428 if (STRCMP(p_guifontset, "*") == 0)
2429 errmsg = e_cant_select_fontset;
2430 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
2431 errmsg = e_invalid_fontset;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002432
2433 return errmsg;
2434}
2435# endif
2436
2437/*
2438 * The 'guifontwide' option is changed.
2439 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002440 char *
2441did_set_guifontwide(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002442{
2443 char *errmsg = NULL;
2444
2445 if (STRCMP(p_guifontwide, "*") == 0)
2446 errmsg = e_cant_select_wide_font;
2447 else if (gui_get_wide_font() == FAIL)
2448 errmsg = e_invalid_wide_font;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002449
2450 return errmsg;
2451}
2452#endif
2453
Erik S. V. Jansson8b1e7492024-02-24 14:26:52 +01002454#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002455/*
2456 * The 'guiligatures' option is changed.
2457 */
2458 char *
2459did_set_guiligatures(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002460{
2461 gui_set_ligatures();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002462 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002463}
2464#endif
2465
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002466#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002467/*
2468 * The 'guioptions' option is changed.
2469 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002470 char *
2471did_set_guioptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002472{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002473 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002474 char *errmsg;
2475
Christian Brabandtb39b2402023-11-29 11:34:05 +01002476 errmsg = did_set_option_listflag(*varp, (char_u *)GO_ALL, args->os_errbuf,
2477 args->os_errbuflen);
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002478 if (errmsg != NULL)
2479 return errmsg;
2480
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002481 gui_init_which_components(args->os_oldval.string);
2482 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002483}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002484
2485 int
2486expand_set_guioptions(optexpand_T *args, int *numMatches, char_u ***matches)
2487{
2488 return expand_set_opt_listflag(args, (char_u*)GO_ALL, numMatches, matches);
2489}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002490#endif
2491
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002492#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002493/*
2494 * The 'guitablabel' option is changed.
2495 */
2496 char *
2497did_set_guitablabel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002498{
2499 redraw_tabline = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002500 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002501}
2502#endif
2503
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002504/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002505 * The 'helpfile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002506 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002507 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002508did_set_helpfile(optset_T *args UNUSED)
2509{
2510 // May compute new values for $VIM and $VIMRUNTIME
2511 if (didset_vim)
2512 vim_unsetenv_ext((char_u *)"VIM");
2513 if (didset_vimruntime)
2514 vim_unsetenv_ext((char_u *)"VIMRUNTIME");
2515 return NULL;
2516}
2517
2518#if defined(FEAT_MULTI_LANG) || defined(PROTO)
2519/*
2520 * The 'helplang' option is changed.
2521 */
2522 char *
2523did_set_helplang(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002524{
2525 char *errmsg = NULL;
2526
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002527 // Check for "", "ab", "ab,cd", etc.
2528 for (char_u *s = p_hlg; *s != NUL; s += 3)
2529 {
2530 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
2531 {
2532 errmsg = e_invalid_argument;
2533 break;
2534 }
2535 if (s[2] == NUL)
2536 break;
2537 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002538
2539 return errmsg;
2540}
2541#endif
2542
2543/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002544 * The 'highlight' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002545 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002546 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002547did_set_highlight(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002548{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002549 if (highlight_changed() == FAIL)
2550 return e_invalid_argument; // invalid flags
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002551
2552 return NULL;
2553}
2554
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002555/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002556 * Expand 'highlight' option.
2557 */
2558 int
2559expand_set_highlight(optexpand_T *args, int *numMatches, char_u ***matches)
2560{
2561 char_u *p;
2562 expand_T *xp = args->oe_xp;
2563 static char_u hl_flags[HLF_COUNT] = HL_FLAGS;
Christian Brabandt3f168ec2023-10-02 23:21:11 +02002564 size_t i;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002565 int count = 0;
2566
2567 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2568 {
2569 // Right after a ':', meaning we just return all highlight names.
2570 return expand_set_opt_generic(
2571 args,
2572 get_highlight_name,
2573 numMatches,
2574 matches);
2575 }
2576
2577 if (*xp->xp_pattern == NUL)
2578 {
2579 // At beginning of a comma-separated list. Return the specific list of
2580 // supported occasions.
2581 *matches = ALLOC_MULT(char_u *, HLF_COUNT + 1);
2582 if (*matches == NULL)
2583 return FAIL;
2584
2585 // We still want to return the full option if it's requested.
2586 if (args->oe_include_orig_val)
2587 {
2588 p = vim_strsave(args->oe_opt_value);
2589 if (p == NULL)
2590 {
2591 VIM_CLEAR(*matches);
2592 return FAIL;
2593 }
2594 (*matches)[count++] = p;
2595 }
2596
2597 for (i = 0; i < HLF_COUNT; i++)
2598 {
2599 p = vim_strnsave(&hl_flags[i], 1);
2600 if (p == NULL)
2601 {
2602 if (count == 0)
2603 {
2604 VIM_CLEAR(*matches);
2605 return FAIL;
2606 }
2607 else
2608 break;
2609 }
2610 (*matches)[count++] = p;
2611 }
2612
2613 if (count == 0)
2614 {
2615 VIM_CLEAR(*matches);
2616 return FAIL;
2617 }
2618 *numMatches = count;
2619 return OK;
2620 }
2621
2622 // We are after the initial character (which indicates the occasion). We
2623 // already made sure we are not matching after a ':' above, so now we want
2624 // to match against display mode modifiers.
2625 // Since the xp_pattern starts from the beginning, we need to include it in
2626 // the returned match.
2627
2628 // Note: Keep this in sync with highlight_changed()
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002629 static char_u p_hl_mode_values[] =
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002630 {':', 'b', 'i', '-', 'n', 'r', 's', 'u', 'c', '2', 'd', '=', 't'};
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002631 size_t num_hl_modes = ARRAY_LENGTH(p_hl_mode_values);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002632
2633 *matches = ALLOC_MULT(char_u *, num_hl_modes);
2634 if (*matches == NULL)
2635 return FAIL;
2636
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002637 int pattern_len = xp->xp_pattern_len;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002638
2639 for (i = 0; i < num_hl_modes; i++)
2640 {
2641 // Don't allow duplicates as these are unique flags
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002642 char_u *dup = vim_strchr(xp->xp_pattern + 1, p_hl_mode_values[i]);
2643 if (dup != NULL && (int)(dup - xp->xp_pattern) < pattern_len)
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002644 continue;
2645
2646 // ':' only works by itself, not with other flags.
2647 if (pattern_len > 1 && p_hl_mode_values[i] == ':')
2648 continue;
2649
2650 p = vim_strnsave(xp->xp_pattern, pattern_len + 1);
2651 if (p == NULL)
2652 {
2653 if (i == 0)
2654 {
2655 VIM_CLEAR(*matches);
2656 return FAIL;
2657 }
2658 else
2659 break;
2660 }
2661 p[pattern_len] = p_hl_mode_values[i];
2662 p[pattern_len + 1] = NUL;
2663 (*matches)[count++] = p;
2664 }
2665 if (count == 0)
2666 {
2667 VIM_CLEAR(*matches);
2668 return FAIL;
2669 }
2670 *numMatches = count;
2671
2672 return OK;
2673}
2674
2675/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002676 * The 'titlestring' or the 'iconstring' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002677 */
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002678 static char *
2679parse_titleiconstring(optset_T *args UNUSED, int flagval UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002680{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002681#ifdef FEAT_STL_OPT
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002682 char_u **varp = (char_u **)args->os_varp;
2683
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002684 // NULL => statusline syntax
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002685 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002686 stl_syntax |= flagval;
2687 else
2688 stl_syntax &= ~flagval;
2689#endif
2690 did_set_title();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002691
2692 return NULL;
2693}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002694
2695/*
2696 * The 'iconstring' option is changed.
2697 */
2698 char *
2699did_set_iconstring(optset_T *args)
2700{
2701 int flagval = 0;
2702
2703#ifdef FEAT_STL_OPT
2704 flagval = STL_IN_ICON;
2705#endif
2706
2707 return parse_titleiconstring(args, flagval);
2708}
2709
2710#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
2711/*
2712 * The 'imactivatekey' option is changed.
2713 */
2714 char *
2715did_set_imactivatekey(optset_T *args UNUSED)
2716{
2717 if (!im_xim_isvalid_imactivate())
2718 return e_invalid_argument;
2719 return NULL;
2720}
2721#endif
2722
2723/*
2724 * The 'isident' or the 'iskeyword' or the 'isprint' or the 'isfname' option is
2725 * changed.
2726 */
2727 char *
2728did_set_isopt(optset_T *args)
2729{
2730 // 'isident', 'iskeyword', 'isprint or 'isfname' option: refill g_chartab[]
2731 // If the new option is invalid, use old value.
2732 // 'lisp' option: refill g_chartab[] for '-' char.
2733 if (init_chartab() == FAIL)
2734 {
2735 args->os_restore_chartab = TRUE;// need to restore the chartab.
2736 return e_invalid_argument; // error in value
2737 }
2738
2739 return NULL;
2740}
2741
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002742/*
2743 * The 'jumpoptions' option is changed.
2744 */
2745 char *
Yegappan Lakshmanan1926ae42023-09-21 16:36:28 +02002746did_set_jumpoptions(optset_T *args UNUSED)
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002747{
2748 if (opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE) != OK)
2749 return e_invalid_argument;
2750
2751 return NULL;
2752}
2753
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002754 int
2755expand_set_jumpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2756{
2757 return expand_set_opt_string(
2758 args,
2759 p_jop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002760 ARRAY_LENGTH(p_jop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002761 numMatches,
2762 matches);
2763}
2764
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002765#if defined(FEAT_KEYMAP) || defined(PROTO)
2766/*
2767 * The 'keymap' option is changed.
2768 */
2769 char *
2770did_set_keymap(optset_T *args)
2771{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002772 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002773 char *errmsg = NULL;
2774
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002775 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002776 errmsg = e_invalid_argument;
2777 else
2778 {
2779 int secure_save = secure;
2780
2781 // Reset the secure flag, since the value of 'keymap' has
2782 // been checked to be safe.
2783 secure = 0;
2784
2785 // load or unload key mapping tables
2786 errmsg = keymap_init();
2787
2788 secure = secure_save;
2789
2790 // Since we check the value, there is no need to set P_INSECURE,
2791 // even when the value comes from a modeline.
2792 args->os_value_checked = TRUE;
2793 }
2794
2795 if (errmsg == NULL)
2796 {
2797 if (*curbuf->b_p_keymap != NUL)
2798 {
2799 // Installed a new keymap, switch on using it.
2800 curbuf->b_p_iminsert = B_IMODE_LMAP;
2801 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
2802 curbuf->b_p_imsearch = B_IMODE_LMAP;
2803 }
2804 else
2805 {
2806 // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
2807 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
2808 curbuf->b_p_iminsert = B_IMODE_NONE;
2809 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
2810 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
2811 }
2812 if ((args->os_flags & OPT_LOCAL) == 0)
2813 {
2814 set_iminsert_global();
2815 set_imsearch_global();
2816 }
2817 status_redraw_curbuf();
2818 }
2819
2820 return errmsg;
2821}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002822#endif
2823
2824/*
2825 * The 'keymodel' option is changed.
2826 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002827 char *
2828did_set_keymodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002829{
2830 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
2831 return e_invalid_argument;
2832
2833 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
2834 km_startsel = (vim_strchr(p_km, 'a') != NULL);
2835 return NULL;
2836}
2837
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002838 int
2839expand_set_keymodel(optexpand_T *args, int *numMatches, char_u ***matches)
2840{
2841 return expand_set_opt_string(
2842 args,
2843 p_km_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002844 ARRAY_LENGTH(p_km_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002845 numMatches,
2846 matches);
2847}
2848
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002849/*
2850 * The 'keyprotocol' option is changed.
2851 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002852 char *
2853did_set_keyprotocol(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002854{
Gregory Anders3695d0e2023-09-29 20:17:20 +02002855 char_u *term = T_NAME;
2856 keyprot_T kpc = match_keyprotocol(term);
2857 if (kpc == KEYPROTOCOL_FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002858 return e_invalid_argument;
2859
Gregory Anders3695d0e2023-09-29 20:17:20 +02002860 apply_keyprotocol(term, kpc);
2861
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002862 return NULL;
2863}
2864
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002865 int
2866expand_set_keyprotocol(optexpand_T *args, int *numMatches, char_u ***matches)
2867{
2868 expand_T *xp = args->oe_xp;
2869 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2870 {
2871 // 'keyprotocol' only has well-defined terms for completion for the
2872 // protocol part after the colon.
2873 return expand_set_opt_string(
2874 args,
2875 p_kpc_protocol_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002876 ARRAY_LENGTH(p_kpc_protocol_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002877 numMatches,
2878 matches);
2879 }
2880 // Use expand_set_opt_string instead of returning FAIL so that we can
2881 // include the original value if args->oe_include_orig_val is set.
2882 static char *(empty[]) = {NULL};
2883 return expand_set_opt_string(args, empty, 0, numMatches, matches);
2884}
2885
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002886/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002887 * The 'lispoptions' option is changed.
2888 */
2889 char *
2890did_set_lispoptions(optset_T *args)
2891{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002892 char_u **varp = (char_u **)args->os_varp;
2893
2894 if (**varp != NUL
2895 && STRCMP(*varp, "expr:0") != 0
2896 && STRCMP(*varp, "expr:1") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002897 return e_invalid_argument;
2898
2899 return NULL;
2900}
2901
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002902 int
2903expand_set_lispoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2904{
2905 static char *(p_lop_values[]) = {"expr:0", "expr:1", NULL};
2906 return expand_set_opt_string(
2907 args,
2908 p_lop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002909 ARRAY_LENGTH(p_lop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002910 numMatches,
2911 matches);
2912}
2913
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002914/*
2915 * The 'matchpairs' option is changed.
2916 */
2917 char *
2918did_set_matchpairs(optset_T *args)
2919{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002920 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002921 char_u *p;
2922
2923 if (has_mbyte)
2924 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002925 for (p = *varp; *p != NUL; ++p)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002926 {
2927 int x2 = -1;
2928 int x3 = -1;
2929
2930 p += mb_ptr2len(p);
2931 if (*p != NUL)
2932 x2 = *p++;
2933 if (*p != NUL)
2934 {
2935 x3 = mb_ptr2char(p);
2936 p += mb_ptr2len(p);
2937 }
2938 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
2939 return e_invalid_argument;
2940 if (*p == NUL)
2941 break;
2942 }
2943 }
2944 else
2945 {
2946 // Check for "x:y,x:y"
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002947 for (p = *varp; *p != NUL; p += 4)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002948 {
2949 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
2950 return e_invalid_argument;
2951 if (p[3] == NUL)
2952 break;
2953 }
2954 }
2955
2956 return NULL;
2957}
2958
2959#if defined(FEAT_SPELL) || defined(PROTO)
2960/*
2961 * The 'mkspellmem' option is changed.
2962 */
2963 char *
2964did_set_mkspellmem(optset_T *args UNUSED)
2965{
2966 if (spell_check_msm() != OK)
2967 return e_invalid_argument;
2968
2969 return NULL;
2970}
2971#endif
2972
2973/*
2974 * The 'mouse' option is changed.
2975 */
2976 char *
2977did_set_mouse(optset_T *args)
2978{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002979 char_u **varp = (char_u **)args->os_varp;
2980
Christian Brabandtb39b2402023-11-29 11:34:05 +01002981 return did_set_option_listflag(*varp, (char_u *)MOUSE_ALL, args->os_errbuf,
2982 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002983}
2984
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002985 int
2986expand_set_mouse(optexpand_T *args, int *numMatches, char_u ***matches)
2987{
2988 return expand_set_opt_listflag(args, (char_u*)MOUSE_ALL, numMatches, matches);
2989}
2990
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002991/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002992 * The 'mousemodel' option is changed.
2993 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002994 char *
2995did_set_mousemodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002996{
2997 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
2998 return e_invalid_argument;
2999#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
Ken Takata18d0d292023-12-19 20:12:29 +01003000 else if (*p_mousem != *args->os_oldval.string)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003001 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
3002 // to create or delete the popup menus.
3003 gui_motif_update_mousemodel(root_menu);
3004#endif
3005
3006 return NULL;
3007}
3008
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003009 int
3010expand_set_mousemodel(optexpand_T *args, int *numMatches, char_u ***matches)
3011{
3012 return expand_set_opt_string(
3013 args,
3014 p_mousem_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003015 ARRAY_LENGTH(p_mousem_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003016 numMatches,
3017 matches);
3018}
3019
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003020#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3021 char *
3022did_set_mouseshape(optset_T *args UNUSED)
3023{
3024 char *errmsg = NULL;
3025
3026 errmsg = parse_shape_opt(SHAPE_MOUSE);
3027 update_mouseshape(-1);
3028
3029 return errmsg;
3030}
3031#endif
3032
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003033/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003034 * The 'nrformats' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003035 */
3036 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003037did_set_nrformats(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003038{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003039 char_u **varp = (char_u **)args->os_varp;
3040
3041 return did_set_opt_strings(*varp, p_nf_values, TRUE);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003042}
3043
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003044 int
3045expand_set_nrformats(optexpand_T *args, int *numMatches, char_u ***matches)
3046{
3047 return expand_set_opt_string(
3048 args,
3049 p_nf_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003050 ARRAY_LENGTH(p_nf_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003051 numMatches,
3052 matches);
3053}
3054
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003055#if defined(FEAT_EVAL) || defined(PROTO)
3056/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003057 * One of the '*expr' options is changed: 'balloonexpr', 'diffexpr',
3058 * 'foldexpr', 'foldtext', 'formatexpr', 'includeexpr', 'indentexpr',
3059 * 'patchexpr', 'printexpr' and 'charconvert'.
3060 *
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003061 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003062 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003063did_set_optexpr(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003064{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003065 char_u **varp = (char_u **)args->os_varp;
3066
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003067 // If the option value starts with <SID> or s:, then replace that with
3068 // the script identifier.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003069 char_u *name = get_scriptlocal_funcname(*varp);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003070 if (name != NULL)
3071 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003072 free_string_option(*varp);
3073 *varp = name;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003074 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003075
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003076 return NULL;
3077}
3078#endif
3079
3080/*
3081 * The 'pastetoggle' option is changed.
3082 */
3083 char *
3084did_set_pastetoggle(optset_T *args UNUSED)
3085{
3086 char_u *p;
3087
3088 // translate key codes like in a mapping
3089 if (*p_pt)
3090 {
zeertzjq7e0bae02023-08-11 23:15:38 +02003091 (void)replace_termcodes(p_pt, &p, 0,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003092 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
3093 if (p != NULL)
3094 {
3095 free_string_option(p_pt);
3096 p_pt = p;
3097 }
3098 }
3099
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003100 return NULL;
3101}
3102
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003103#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3104/*
3105 * The 'previewpopup' option is changed.
3106 */
3107 char *
3108did_set_previewpopup(optset_T *args UNUSED)
3109{
3110 if (parse_previewpopup(NULL) == FAIL)
3111 return e_invalid_argument;
3112
3113 return NULL;
3114}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003115
3116 int
3117expand_set_popupoption(optexpand_T *args, int *numMatches, char_u ***matches)
3118{
3119 expand_T *xp = args->oe_xp;
3120
3121 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3122 {
3123 // Within "highlight:"/"border:"/"align:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003124 int border_len = (int)STRLEN("border:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003125 if (xp->xp_pattern - args->oe_set_arg >= border_len &&
3126 STRNCMP(xp->xp_pattern - border_len, "border:", border_len) == 0)
3127 {
3128 return expand_set_opt_string(
3129 args,
3130 p_popup_option_border_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003131 ARRAY_LENGTH(p_popup_option_border_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003132 numMatches,
3133 matches);
3134 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003135 int align_len = (int)STRLEN("align:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003136 if (xp->xp_pattern - args->oe_set_arg >= align_len &&
3137 STRNCMP(xp->xp_pattern - align_len, "align:", align_len) == 0)
3138 {
3139 return expand_set_opt_string(
3140 args,
3141 p_popup_option_align_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003142 ARRAY_LENGTH(p_popup_option_align_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003143 numMatches,
3144 matches);
3145 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003146 int highlight_len = (int)STRLEN("highlight:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003147 if (xp->xp_pattern - args->oe_set_arg >= highlight_len &&
3148 STRNCMP(xp->xp_pattern - highlight_len, "highlight:", highlight_len) == 0)
3149 {
3150 // Return the list of all highlight names
3151 return expand_set_opt_generic(
3152 args,
3153 get_highlight_name,
3154 numMatches,
3155 matches);
3156 }
3157 return FAIL;
3158 }
3159
3160 return expand_set_opt_string(
3161 args,
3162 p_popup_option_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003163 ARRAY_LENGTH(p_popup_option_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003164 numMatches,
3165 matches);
3166}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003167#endif
3168
3169#if defined(FEAT_POSTSCRIPT) || defined(PROTO)
3170/*
3171 * The 'printencoding' option is changed.
3172 */
3173 char *
3174did_set_printencoding(optset_T *args UNUSED)
3175{
3176 char_u *s, *p;
3177
3178 // Canonize 'printencoding' if VIM standard one
3179 p = enc_canonize(p_penc);
3180 if (p != NULL)
3181 {
3182 vim_free(p_penc);
3183 p_penc = p;
3184 }
3185 else
3186 {
3187 // Ensure lower case and '-' for '_'
3188 for (s = p_penc; *s != NUL; s++)
3189 {
3190 if (*s == '_')
3191 *s = '-';
3192 else
3193 *s = TOLOWER_ASC(*s);
3194 }
3195 }
3196
3197 return NULL;
3198}
3199#endif
3200
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003201#if defined(FEAT_PRINTER) || defined(PROTO)
3202
3203 static char_u *
3204get_printoptions_names(expand_T *xp UNUSED, int idx)
3205{
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003206 if (idx >= (int)ARRAY_LENGTH(printer_opts))
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003207 return NULL;
3208 return (char_u*)printer_opts[idx].name;
3209}
3210
3211/*
3212 * Expand 'printoptions' option
3213 */
3214 int
3215expand_set_printoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3216{
3217 return expand_set_opt_generic(
3218 args,
3219 get_printoptions_names,
3220 numMatches,
3221 matches);
3222}
3223#endif
3224
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003225#if defined(FEAT_STL_OPT) || defined(PROTO)
3226/*
3227 * The 'statusline' or the 'tabline' or the 'rulerformat' option is changed.
3228 * "rulerformat" is TRUE if the 'rulerformat' option is changed.
3229 */
3230 static char *
3231parse_statustabline_rulerformat(optset_T *args, int rulerformat)
3232{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003233 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003234 char_u *s;
3235 char *errmsg = NULL;
3236 int wid;
3237
3238 if (rulerformat) // reset ru_wid first
3239 ru_wid = 0;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003240 s = *varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003241 if (rulerformat && *s == '%')
3242 {
3243 // set ru_wid if 'ruf' starts with "%99("
3244 if (*++s == '-') // ignore a '-'
3245 s++;
3246 wid = getdigits(&s);
3247 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
3248 ru_wid = wid;
3249 else
3250 errmsg = check_stl_option(p_ruf);
3251 }
3252 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
3253 else if (rulerformat || s[0] != '%' || s[1] != '!')
3254 errmsg = check_stl_option(s);
3255 if (rulerformat && errmsg == NULL)
3256 comp_col();
3257
3258 return errmsg;
3259}
3260#endif
3261
3262#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
3263/*
3264 * The 'renderoptions' option is changed.
3265 */
3266 char *
3267did_set_renderoptions(optset_T *args UNUSED)
3268{
3269 if (!gui_mch_set_rendering_options(p_rop))
3270 return e_invalid_argument;
3271
3272 return NULL;
3273}
3274#endif
3275
3276#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3277/*
3278 * The 'rightleftcmd' option is changed.
3279 */
3280 char *
3281did_set_rightleftcmd(optset_T *args)
3282{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003283 char_u **varp = (char_u **)args->os_varp;
3284
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003285 // Currently only "search" is a supported value.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003286 if (**varp != NUL && STRCMP(*varp, "search") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003287 return e_invalid_argument;
3288
3289 return NULL;
3290}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003291
3292 int
3293expand_set_rightleftcmd(optexpand_T *args, int *numMatches, char_u ***matches)
3294{
3295 static char *(p_rlc_values[]) = {"search", NULL};
3296 return expand_set_opt_string(
3297 args,
3298 p_rlc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003299 ARRAY_LENGTH(p_rlc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003300 numMatches,
3301 matches);
3302}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003303#endif
3304
3305#if defined(FEAT_STL_OPT) || defined(PROTO)
3306/*
3307 * The 'rulerformat' option is changed.
3308 */
3309 char *
3310did_set_rulerformat(optset_T *args)
3311{
3312 return parse_statustabline_rulerformat(args, TRUE);
3313}
3314#endif
3315
3316/*
3317 * The 'scrollopt' option is changed.
3318 */
3319 char *
3320did_set_scrollopt(optset_T *args UNUSED)
3321{
3322 return did_set_opt_strings(p_sbo, p_scbopt_values, TRUE);
3323}
3324
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003325 int
3326expand_set_scrollopt(optexpand_T *args, int *numMatches, char_u ***matches)
3327{
3328 return expand_set_opt_string(
3329 args,
3330 p_scbopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003331 ARRAY_LENGTH(p_scbopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003332 numMatches,
3333 matches);
3334}
3335
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003336/*
3337 * The 'selection' option is changed.
3338 */
3339 char *
3340did_set_selection(optset_T *args UNUSED)
3341{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003342 if (*p_sel == NUL || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003343 return e_invalid_argument;
3344
3345 return NULL;
3346}
3347
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003348 int
3349expand_set_selection(optexpand_T *args, int *numMatches, char_u ***matches)
3350{
3351 return expand_set_opt_string(
3352 args,
3353 p_sel_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003354 ARRAY_LENGTH(p_sel_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003355 numMatches,
3356 matches);
3357}
3358
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003359/*
3360 * The 'selectmode' option is changed.
3361 */
3362 char *
3363did_set_selectmode(optset_T *args UNUSED)
3364{
3365 return did_set_opt_strings(p_slm, p_slm_values, TRUE);
3366}
3367
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003368 int
3369expand_set_selectmode(optexpand_T *args, int *numMatches, char_u ***matches)
3370{
3371 return expand_set_opt_string(
3372 args,
3373 p_slm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003374 ARRAY_LENGTH(p_slm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003375 numMatches,
3376 matches);
3377}
3378
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003379#if defined(FEAT_SESSION) || defined(PROTO)
3380/*
3381 * The 'sessionoptions' option is changed.
3382 */
3383 char *
3384did_set_sessionoptions(optset_T *args)
3385{
3386 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
3387 return e_invalid_argument;
3388 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
3389 {
3390 // Don't allow both "sesdir" and "curdir".
3391 (void)opt_strings_flags(args->os_oldval.string, p_ssop_values,
3392 &ssop_flags, TRUE);
3393 return e_invalid_argument;
3394 }
3395
3396 return NULL;
3397}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003398
3399 int
3400expand_set_sessionoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3401{
3402 return expand_set_opt_string(
3403 args,
3404 p_ssop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003405 ARRAY_LENGTH(p_ssop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003406 numMatches,
3407 matches);
3408}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003409#endif
3410
3411/*
3412 * The 'shortmess' option is changed.
3413 */
3414 char *
3415did_set_shortmess(optset_T *args)
3416{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003417 char_u **varp = (char_u **)args->os_varp;
3418
Christian Brabandtb39b2402023-11-29 11:34:05 +01003419 return did_set_option_listflag(*varp, (char_u *)SHM_ALL, args->os_errbuf,
3420 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003421}
3422
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003423 int
3424expand_set_shortmess(optexpand_T *args, int *numMatches, char_u ***matches)
3425{
3426 return expand_set_opt_listflag(args, (char_u*)SHM_ALL, numMatches, matches);
3427}
3428
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003429#if defined(FEAT_LINEBREAK) || defined(PROTO)
3430/*
3431 * The 'showbreak' option is changed.
3432 */
3433 char *
3434did_set_showbreak(optset_T *args)
3435{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003436 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003437 char_u *s;
3438
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003439 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003440 {
3441 if (ptr2cells(s) != 1)
3442 return e_showbreak_contains_unprintable_or_wide_character;
3443 MB_PTR_ADV(s);
3444 }
3445
3446 return NULL;
3447}
3448#endif
3449
3450/*
3451 * The 'showcmdloc' option is changed.
3452 */
3453 char *
3454did_set_showcmdloc(optset_T *args UNUSED)
3455{
zeertzjqc27fcf42024-03-01 23:01:43 +01003456 char *errmsg = did_set_opt_strings(p_sloc, p_sloc_values, FALSE);
3457
3458 if (errmsg == NULL)
3459 comp_col();
3460
3461 return errmsg;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003462}
3463
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003464 int
3465expand_set_showcmdloc(optexpand_T *args, int *numMatches, char_u ***matches)
3466{
3467 return expand_set_opt_string(
3468 args,
3469 p_sloc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003470 ARRAY_LENGTH(p_sloc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003471 numMatches,
3472 matches);
3473}
3474
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003475#if defined(FEAT_SIGNS) || defined(PROTO)
3476/*
3477 * The 'signcolumn' option is changed.
3478 */
3479 char *
3480did_set_signcolumn(optset_T *args)
3481{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003482 char_u **varp = (char_u **)args->os_varp;
3483
3484 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003485 return e_invalid_argument;
3486 // When changing the 'signcolumn' to or from 'number', recompute the
3487 // width of the number column if 'number' or 'relativenumber' is set.
3488 if (((*args->os_oldval.string == 'n' && args->os_oldval.string[1] == 'u')
3489 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
3490 && (curwin->w_p_nu || curwin->w_p_rnu))
3491 curwin->w_nrwidth_line_count = 0;
3492
3493 return NULL;
3494}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003495
3496 int
3497expand_set_signcolumn(optexpand_T *args, int *numMatches, char_u ***matches)
3498{
3499 return expand_set_opt_string(
3500 args,
3501 p_scl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003502 ARRAY_LENGTH(p_scl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003503 numMatches,
3504 matches);
3505}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003506#endif
3507
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003508#if defined(FEAT_SPELL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003509/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003510 * The 'spellcapcheck' option is changed.
3511 */
3512 char *
3513did_set_spellcapcheck(optset_T *args UNUSED)
3514{
3515 // compile the regexp program.
3516 return compile_cap_prog(curwin->w_s);
3517}
3518
3519/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003520 * The 'spellfile' option is changed.
3521 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003522 char *
3523did_set_spellfile(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003524{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003525 char_u **varp = (char_u **)args->os_varp;
3526
3527 if (!valid_spellfile(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003528 return e_invalid_argument;
3529
3530 // If there is a window for this buffer in which 'spell' is set load the
3531 // wordlists.
3532 return did_set_spell_option(TRUE);
3533}
3534
3535/*
3536 * The 'spell' option is changed.
3537 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003538 char *
3539did_set_spelllang(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003540{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003541 char_u **varp = (char_u **)args->os_varp;
3542
3543 if (!valid_spelllang(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003544 return e_invalid_argument;
3545
3546 // If there is a window for this buffer in which 'spell' is set load the
3547 // wordlists.
3548 return did_set_spell_option(FALSE);
3549}
3550
3551/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003552 * The 'spelloptions' option is changed.
3553 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003554 char *
3555did_set_spelloptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003556{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003557 char_u **varp = (char_u **)args->os_varp;
3558
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003559 if (**varp != NUL && STRCMP(*varp, "camel") != 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003560 return e_invalid_argument;
3561
3562 return NULL;
3563}
3564
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003565 int
3566expand_set_spelloptions(optexpand_T *args, int *numMatches, char_u ***matches)
3567{
3568 static char *(p_spo_values[]) = {"camel", NULL};
3569 return expand_set_opt_string(
3570 args,
3571 p_spo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003572 ARRAY_LENGTH(p_spo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003573 numMatches,
3574 matches);
3575}
3576
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003577/*
3578 * The 'spellsuggest' option is changed.
3579 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003580 char *
3581did_set_spellsuggest(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003582{
3583 if (spell_check_sps() != OK)
3584 return e_invalid_argument;
3585
3586 return NULL;
3587}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003588
3589 int
3590expand_set_spellsuggest(optexpand_T *args, int *numMatches, char_u ***matches)
3591{
3592 return expand_set_opt_string(
3593 args,
3594 p_sps_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003595 ARRAY_LENGTH(p_sps_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003596 numMatches,
3597 matches);
3598}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003599#endif
3600
3601/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003602 * The 'splitkeep' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003603 */
3604 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003605did_set_splitkeep(optset_T *args UNUSED)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003606{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003607 return did_set_opt_strings(p_spk, p_spk_values, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003608}
3609
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003610 int
3611expand_set_splitkeep(optexpand_T *args, int *numMatches, char_u ***matches)
3612{
3613 return expand_set_opt_string(
3614 args,
3615 p_spk_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003616 ARRAY_LENGTH(p_spk_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003617 numMatches,
3618 matches);
3619}
3620
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00003621#if defined(FEAT_STL_OPT) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003622/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003623 * The 'statusline' option is changed.
3624 */
3625 char *
3626did_set_statusline(optset_T *args)
3627{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003628 return parse_statustabline_rulerformat(args, FALSE);
3629}
3630#endif
3631
3632/*
3633 * The 'swapsync' option is changed.
3634 */
3635 char *
3636did_set_swapsync(optset_T *args UNUSED)
3637{
3638 return did_set_opt_strings(p_sws, p_sws_values, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003639}
3640
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003641 int
3642expand_set_swapsync(optexpand_T *args, int *numMatches, char_u ***matches)
3643{
3644 return expand_set_opt_string(
3645 args,
3646 p_sws_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003647 ARRAY_LENGTH(p_sws_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003648 numMatches,
3649 matches);
3650}
3651
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003652/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003653 * The 'switchbuf' option is changed.
3654 */
3655 char *
3656did_set_switchbuf(optset_T *args UNUSED)
3657{
3658 return did_set_opt_flags(p_swb, p_swb_values, &swb_flags, TRUE);
3659}
3660
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003661 int
3662expand_set_switchbuf(optexpand_T *args, int *numMatches, char_u ***matches)
3663{
3664 return expand_set_opt_string(
3665 args,
3666 p_swb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003667 ARRAY_LENGTH(p_swb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003668 numMatches,
3669 matches);
3670}
3671
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003672#if defined(FEAT_STL_OPT) || defined(PROTO)
3673/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003674 * The 'tabline' option is changed.
3675 */
3676 char *
3677did_set_tabline(optset_T *args)
3678{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003679 return parse_statustabline_rulerformat(args, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003680}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003681#endif
3682
3683/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003684 * The 'tagcase' option is changed.
3685 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003686 char *
3687did_set_tagcase(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003688{
3689 unsigned int *flags;
3690 char_u *p;
3691
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003692 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003693 {
3694 p = curbuf->b_p_tc;
3695 flags = &curbuf->b_tc_flags;
3696 }
3697 else
3698 {
3699 p = p_tc;
3700 flags = &tc_flags;
3701 }
3702
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003703 if ((args->os_flags & OPT_LOCAL) && *p == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003704 // make the local value empty: use the global value
3705 *flags = 0;
3706 else if (*p == NUL
3707 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
3708 return e_invalid_argument;
3709
3710 return NULL;
3711}
3712
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003713 int
3714expand_set_tagcase(optexpand_T *args, int *numMatches, char_u ***matches)
3715{
3716 return expand_set_opt_string(
3717 args,
3718 p_tc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003719 ARRAY_LENGTH(p_tc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003720 numMatches,
3721 matches);
3722}
3723
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003724/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003725 * The 'term' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003726 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003727 char *
3728did_set_term(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003729{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003730 if (T_NAME[0] == NUL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003731 return e_cannot_set_term_to_empty_string;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003732#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003733 if (gui.in_use)
3734 return e_cannot_change_term_in_GUI;
3735 if (term_is_gui(T_NAME))
3736 return e_use_gui_to_start_GUI;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003737#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003738 if (set_termname(T_NAME) == FAIL)
3739 return e_not_found_in_termcap;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003740
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003741 // Screen colors may have changed.
3742 redraw_later_clear();
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003743
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003744 return NULL;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003745}
3746
3747/*
3748 * Some terminal option (t_xxx) is changed
3749 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003750 char *
3751did_set_term_option(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003752{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003753 char_u **varp = (char_u **)args->os_varp;
3754
3755 if (!full_screen)
3756 return NULL;
3757
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003758 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
3759 if (varp == &T_CCO)
3760 {
3761 int colors = atoi((char *)T_CCO);
3762
3763 // Only reinitialize colors if t_Co value has really changed to
3764 // avoid expensive reload of colorscheme if t_Co is set to the
3765 // same value multiple times.
3766 if (colors != t_colors)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003767 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003768 t_colors = colors;
3769 if (t_colors <= 1)
3770 {
3771 vim_free(T_CCO);
3772 T_CCO = empty_option;
3773 }
3774#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3775 if (is_term_win32())
3776 {
3777 swap_tcap();
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003778 args->os_did_swaptcap = TRUE;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003779 }
3780#endif
3781 // We now have a different color setup, initialize it again.
3782 init_highlight(TRUE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003783 }
3784 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003785 ttest(FALSE);
3786 if (varp == &T_ME)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003787 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003788 out_str(T_ME);
3789 redraw_later(UPD_CLEAR);
3790#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3791 // Since t_me has been set, this probably means that the user
3792 // wants to use this as default colors. Need to reset default
3793 // background/foreground colors.
3794# ifdef VIMDLL
3795 if (!gui.in_use && !gui.starting)
3796# endif
3797 mch_set_normal_colors();
3798#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003799 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003800 if (varp == &T_BE && termcap_active)
3801 {
3802 MAY_WANT_TO_LOG_THIS;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003803
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003804 if (*T_BE == NUL)
3805 // When clearing t_BE we assume the user no longer wants
3806 // bracketed paste, thus disable it by writing t_BD.
3807 out_str(T_BD);
3808 else
3809 out_str(T_BE);
3810 }
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003811
3812 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003813}
3814
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003815#if defined(FEAT_TERMINAL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003816/*
3817 * The 'termwinkey' option is changed.
3818 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003819 char *
3820did_set_termwinkey(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003821{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003822 if (*curwin->w_p_twk != NUL && string_to_key(curwin->w_p_twk, TRUE) == 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003823 return e_invalid_argument;
3824
3825 return NULL;
3826}
3827
3828/*
3829 * The 'termwinsize' option is changed.
3830 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003831 char *
3832did_set_termwinsize(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003833{
3834 char_u *p;
3835
3836 if (*curwin->w_p_tws == NUL)
3837 return NULL;
3838
3839 p = skipdigits(curwin->w_p_tws);
3840 if (p == curwin->w_p_tws
3841 || (*p != 'x' && *p != '*')
3842 || *skipdigits(p + 1) != NUL)
3843 return e_invalid_argument;
3844
3845 return NULL;
3846}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003847
3848# if defined(MSWIN) || defined(PROTO)
3849/*
3850 * The 'termwintype' option is changed.
3851 */
3852 char *
3853did_set_termwintype(optset_T *args UNUSED)
3854{
3855 return did_set_opt_strings(p_twt, p_twt_values, FALSE);
3856}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003857
3858 int
3859expand_set_termwintype(optexpand_T *args, int *numMatches, char_u ***matches)
3860{
3861 return expand_set_opt_string(
3862 args,
3863 p_twt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003864 ARRAY_LENGTH(p_twt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003865 numMatches,
3866 matches);
3867}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003868# endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003869#endif
3870
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003871/*
3872 * The 'titlestring' option is changed.
3873 */
3874 char *
3875did_set_titlestring(optset_T *args)
3876{
3877 int flagval = 0;
3878
3879#ifdef FEAT_STL_OPT
3880 flagval = STL_IN_TITLE;
3881#endif
3882 return parse_titleiconstring(args, flagval);
3883}
3884
3885#if (defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)) || defined(PROTO)
3886/*
3887 * The 'toolbar' option is changed.
3888 */
3889 char *
3890did_set_toolbar(optset_T *args UNUSED)
3891{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003892 if (opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags,
3893 TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003894 return e_invalid_argument;
3895
3896 out_flush();
3897 gui_mch_show_toolbar((toolbar_flags &
3898 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
3899 return NULL;
3900}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003901
3902 int
3903expand_set_toolbar(optexpand_T *args, int *numMatches, char_u ***matches)
3904{
3905 return expand_set_opt_string(
3906 args,
3907 p_toolbar_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003908 ARRAY_LENGTH(p_toolbar_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003909 numMatches,
3910 matches);
3911}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003912#endif
3913
3914#if (defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)) || defined(PROTO)
3915/*
3916 * The 'toolbariconsize' option is changed. GTK+ 2 only.
3917 */
3918 char *
3919did_set_toolbariconsize(optset_T *args UNUSED)
3920{
3921 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
3922 return e_invalid_argument;
3923
3924 out_flush();
3925 gui_mch_show_toolbar((toolbar_flags &
3926 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
3927 return NULL;
3928}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003929
3930 int
3931expand_set_toolbariconsize(optexpand_T *args, int *numMatches, char_u ***matches)
3932{
3933 return expand_set_opt_string(
3934 args,
3935 p_tbis_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003936 ARRAY_LENGTH(p_tbis_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003937 numMatches,
3938 matches);
3939}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003940#endif
3941
3942#if defined(UNIX) || defined(VMS) || defined(PROTO)
3943/*
3944 * The 'ttymouse' option is changed.
3945 */
3946 char *
3947did_set_ttymouse(optset_T *args UNUSED)
3948{
3949 char *errmsg = NULL;
3950
3951 // Switch the mouse off before changing the escape sequences used for
3952 // that.
3953 mch_setmouse(FALSE);
3954 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
3955 errmsg = e_invalid_argument;
3956 else
3957 check_mouse_termcode();
3958 if (termcap_active)
3959 setmouse(); // may switch it on again
3960
3961 return errmsg;
3962}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003963
3964 int
3965expand_set_ttymouse(optexpand_T *args, int *numMatches, char_u ***matches)
3966{
3967 return expand_set_opt_string(
3968 args,
3969 p_ttym_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003970 ARRAY_LENGTH(p_ttym_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003971 numMatches,
3972 matches);
3973}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003974#endif
3975
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003976#if defined(FEAT_VARTABS) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003977/*
3978 * The 'varsofttabstop' option is changed.
3979 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003980 char *
3981did_set_varsofttabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003982{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003983 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003984 char_u *cp;
3985
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003986 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00003987 VIM_CLEAR(curbuf->b_p_vsts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003988 else
3989 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003990 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003991 {
3992 if (vim_isdigit(*cp))
3993 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003994 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003995 continue;
3996 return e_invalid_argument;
3997 }
3998
3999 int *oldarray = curbuf->b_p_vsts_array;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004000 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004001 {
4002 if (oldarray)
4003 vim_free(oldarray);
4004 }
4005 else
4006 return e_invalid_argument;
4007 }
4008
4009 return NULL;
4010}
4011
4012/*
4013 * The 'vartabstop' option is changed.
4014 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004015 char *
4016did_set_vartabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004017{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004018 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004019 char_u *cp;
4020
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004021 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004022 VIM_CLEAR(curbuf->b_p_vts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004023 else
4024 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004025 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004026 {
4027 if (vim_isdigit(*cp))
4028 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004029 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004030 continue;
4031 return e_invalid_argument;
4032 }
4033
4034 int *oldarray = curbuf->b_p_vts_array;
4035
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004036 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004037 {
4038 vim_free(oldarray);
4039# ifdef FEAT_FOLDING
4040 if (foldmethodIsIndent(curwin))
4041 foldUpdateAll(curwin);
4042# endif
4043 }
4044 else
4045 return e_invalid_argument;
4046 }
4047
4048 return NULL;
4049}
4050#endif
4051
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004052/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004053 * The 'verbosefile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004054 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004055 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004056did_set_verbosefile(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004057{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004058 verbose_stop();
4059 if (*p_vfile != NUL && verbose_open() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004060 return e_invalid_argument;
4061
4062 return NULL;
4063}
4064
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004065#if defined(FEAT_SESSION) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004066/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004067 * The 'viewoptions' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004068 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004069 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004070did_set_viewoptions(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004071{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004072 return did_set_opt_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004073}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004074#endif
4075
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004076#if defined(FEAT_VIMINFO) || defined(PROTO)
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004077/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004078 * The 'viminfo' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004079 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004080 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004081did_set_viminfo(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004082{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004083 char_u *s;
4084 char *errmsg = NULL;
4085
4086 for (s = p_viminfo; *s;)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004087 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004088 // Check it's a valid character
4089 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
4090 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01004091 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004092 break;
4093 }
4094 if (*s == 'n') // name is always last one
4095 break;
4096 else if (*s == 'r') // skip until next ','
4097 {
4098 while (*++s && *s != ',')
4099 ;
4100 }
4101 else if (*s == '%')
4102 {
4103 // optional number
4104 while (vim_isdigit(*++s))
4105 ;
4106 }
4107 else if (*s == '!' || *s == 'h' || *s == 'c')
4108 ++s; // no extra chars
4109 else // must have a number
4110 {
4111 while (vim_isdigit(*++s))
4112 ;
4113
4114 if (!VIM_ISDIGIT(*(s - 1)))
4115 {
4116 if (args->os_errbuf != NULL)
4117 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01004118 vim_snprintf(args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004119 _(e_missing_number_after_angle_str_angle),
4120 transchar_byte(*(s - 1)));
4121 errmsg = args->os_errbuf;
4122 }
4123 else
4124 errmsg = "";
4125 break;
4126 }
4127 }
4128 if (*s == ',')
4129 ++s;
4130 else if (*s)
4131 {
4132 if (args->os_errbuf != NULL)
4133 errmsg = e_missing_comma;
4134 else
4135 errmsg = "";
4136 break;
4137 }
4138 }
4139 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
4140 errmsg = e_must_specify_a_value;
4141
4142 return errmsg;
4143}
4144#endif
4145
4146/*
4147 * The 'virtualedit' option is changed.
4148 */
4149 char *
4150did_set_virtualedit(optset_T *args)
4151{
4152 char_u *ve = p_ve;
4153 unsigned int *flags = &ve_flags;
4154
4155 if (args->os_flags & OPT_LOCAL)
4156 {
4157 ve = curwin->w_p_ve;
4158 flags = &curwin->w_ve_flags;
4159 }
4160
4161 if ((args->os_flags & OPT_LOCAL) && *ve == NUL)
4162 // make the local value empty: use the global value
4163 *flags = 0;
4164 else
4165 {
4166 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
4167 return e_invalid_argument;
4168 else if (STRCMP(ve, args->os_oldval.string) != 0)
4169 {
4170 // Recompute cursor position in case the new 've' setting
4171 // changes something.
4172 validate_virtcol();
4173 coladvance(curwin->w_virtcol);
4174 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004175 }
4176
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004177 return NULL;
4178}
4179
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004180 int
4181expand_set_virtualedit(optexpand_T *args, int *numMatches, char_u ***matches)
4182{
4183 return expand_set_opt_string(
4184 args,
4185 p_ve_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004186 ARRAY_LENGTH(p_ve_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004187 numMatches,
4188 matches);
4189}
4190
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004191/*
4192 * The 'whichwrap' option is changed.
4193 */
4194 char *
4195did_set_whichwrap(optset_T *args)
4196{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004197 char_u **varp = (char_u **)args->os_varp;
4198
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004199 // Add ',' to the list flags because 'whichwrap' is a flag
4200 // list that is comma-separated.
Christian Brabandtb39b2402023-11-29 11:34:05 +01004201 return did_set_option_listflag(*varp, (char_u *)(WW_ALL ","),
4202 args->os_errbuf, args->os_errbuflen);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004203}
4204
4205 int
4206expand_set_whichwrap(optexpand_T *args, int *numMatches, char_u ***matches)
4207{
4208 return expand_set_opt_listflag(args, (char_u*)WW_ALL, numMatches, matches);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004209}
4210
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004211/*
4212 * The 'wildmode' option is changed.
4213 */
4214 char *
4215did_set_wildmode(optset_T *args UNUSED)
4216{
4217 if (check_opt_wim() == FAIL)
4218 return e_invalid_argument;
4219 return NULL;
4220}
4221
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004222 int
4223expand_set_wildmode(optexpand_T *args, int *numMatches, char_u ***matches)
4224{
4225 return expand_set_opt_string(
4226 args,
4227 p_wim_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004228 ARRAY_LENGTH(p_wim_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004229 numMatches,
4230 matches);
4231}
4232
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004233/*
4234 * The 'wildoptions' option is changed.
4235 */
4236 char *
4237did_set_wildoptions(optset_T *args UNUSED)
4238{
4239 return did_set_opt_strings(p_wop, p_wop_values, TRUE);
4240}
4241
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004242 int
4243expand_set_wildoptions(optexpand_T *args, int *numMatches, char_u ***matches)
4244{
4245 return expand_set_opt_string(
4246 args,
4247 p_wop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004248 ARRAY_LENGTH(p_wop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004249 numMatches,
4250 matches);
4251}
4252
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004253#if defined(FEAT_WAK) || defined(PROTO)
4254/*
4255 * The 'winaltkeys' option is changed.
4256 */
4257 char *
4258did_set_winaltkeys(optset_T *args UNUSED)
4259{
4260 char *errmsg = NULL;
4261
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004262 if (*p_wak == NUL || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004263 errmsg = e_invalid_argument;
4264# ifdef FEAT_MENU
4265# if defined(FEAT_GUI_MOTIF)
4266 else if (gui.in_use)
4267 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4268# elif defined(FEAT_GUI_GTK)
4269 else if (gui.in_use)
4270 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4271# endif
4272# endif
4273 return errmsg;
4274}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004275
4276 int
4277expand_set_winaltkeys(optexpand_T *args, int *numMatches, char_u ***matches)
4278{
4279 return expand_set_opt_string(
4280 args,
4281 p_wak_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004282 ARRAY_LENGTH(p_wak_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004283 numMatches,
4284 matches);
4285}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004286#endif
4287
4288/*
4289 * The 'wincolor' option is changed.
4290 */
4291 char *
4292did_set_wincolor(optset_T *args UNUSED)
4293{
4294#ifdef FEAT_TERMINAL
4295 term_update_wincolor(curwin);
4296#endif
4297 return NULL;
4298}
4299
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004300 int
4301expand_set_wincolor(optexpand_T *args, int *numMatches, char_u ***matches)
4302{
4303 return expand_set_opt_generic(
4304 args,
4305 get_highlight_name,
4306 numMatches,
4307 matches);
4308}
4309
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004310#ifdef FEAT_SYN_HL
4311/*
4312 * When the 'syntax' option is set, load the syntax of that name.
4313 */
4314 static void
4315do_syntax_autocmd(int value_changed)
4316{
4317 static int syn_recursive = 0;
4318
4319 ++syn_recursive;
4320 // Only pass TRUE for "force" when the value changed or not used
4321 // recursively, to avoid endless recurrence.
4322 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
4323 value_changed || syn_recursive == 1, curbuf);
4324 curbuf->b_flags |= BF_SYN_SET;
4325 --syn_recursive;
4326}
4327#endif
4328
4329/*
4330 * When the 'filetype' option is set, trigger the FileType autocommand.
4331 */
4332 static void
4333do_filetype_autocmd(char_u **varp, int opt_flags, int value_changed)
4334{
4335 // Skip this when called from a modeline and the filetype was already set
4336 // to this value.
4337 if ((opt_flags & OPT_MODELINE) && !value_changed)
4338 return;
4339
4340 static int ft_recursive = 0;
4341 int secure_save = secure;
4342
4343 // Reset the secure flag, since the value of 'filetype' has
4344 // been checked to be safe.
4345 secure = 0;
4346
4347 ++ft_recursive;
zeertzjq5bf6c212024-03-31 18:41:27 +02004348 curbuf->b_did_filetype = TRUE;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004349 // Only pass TRUE for "force" when the value changed or not
4350 // used recursively, to avoid endless recurrence.
4351 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
4352 value_changed || ft_recursive == 1, curbuf);
4353 --ft_recursive;
4354 // Just in case the old "curbuf" is now invalid.
4355 if (varp != &(curbuf->b_p_ft))
4356 varp = NULL;
4357
4358 secure = secure_save;
4359}
4360
4361#ifdef FEAT_SPELL
4362/*
4363 * When the 'spelllang' option is set, source the spell/LANG.vim file in
4364 * 'runtimepath'.
4365 */
4366 static void
4367do_spelllang_source(void)
4368{
4369 char_u fname[200];
4370 char_u *p;
4371 char_u *q = curwin->w_s->b_p_spl;
4372
4373 // Skip the first name if it is "cjk".
4374 if (STRNCMP(q, "cjk,", 4) == 0)
4375 q += 4;
4376
4377 // They could set 'spellcapcheck' depending on the language. Use the first
4378 // name in 'spelllang' up to '_region' or '.encoding'.
4379 for (p = q; *p != NUL; ++p)
4380 if (!ASCII_ISALNUM(*p) && *p != '-')
4381 break;
4382 if (p > q)
4383 {
4384 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
4385 (int)(p - q), q);
4386 source_runtime(fname, DIP_ALL);
4387 }
4388}
4389#endif
4390
4391/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004392 * Handle string options that need some action to perform when changed.
zeertzjqf6782732022-07-27 18:26:03 +01004393 * The new value must be allocated.
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004394 * Returns NULL for success, or an untranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +02004395 */
4396 char *
4397did_set_string_option(
4398 int opt_idx, // index in options[] table
4399 char_u **varp, // pointer to the option variable
Bram Moolenaardac13472019-09-16 21:06:21 +02004400 char_u *oldval, // previous value of the option
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004401 char_u *value, // new value of the option
Bram Moolenaardac13472019-09-16 21:06:21 +02004402 char *errbuf, // buffer for errors, or NULL
Mike Williams620f0112023-12-05 15:36:06 +01004403 size_t errbuflen, // length of error buffer
Bram Moolenaardac13472019-09-16 21:06:21 +02004404 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004405 set_op_T op, // OP_ADDING/OP_PREPENDING/OP_REMOVING
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004406 int *value_checked) // value was checked to be safe, no
Bram Moolenaardac13472019-09-16 21:06:21 +02004407 // need to set P_INSECURE
4408{
4409 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +02004410 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004411 opt_did_set_cb_T did_set_cb = get_option_did_set_cb(opt_idx);
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004412 optset_T args;
4413
4414 // 'ttytype' is an alias for 'term'. Both 'term' and 'ttytype' point to
4415 // T_NAME. If 'term' or 'ttytype' is modified, then use the index for the
4416 // 'term' option. Only set the P_ALLOCED flag on 'term'.
4417 if (varp == &T_NAME)
4418 {
4419 opt_idx = findoption((char_u *)"term");
4420 if (opt_idx >= 0)
4421 {
4422 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
4423 did_set_cb = get_option_did_set_cb(opt_idx);
4424 }
4425 }
4426
4427 CLEAR_FIELD(args);
Bram Moolenaardac13472019-09-16 21:06:21 +02004428
Bram Moolenaardac13472019-09-16 21:06:21 +02004429 // Disallow changing some options from secure mode
4430 if ((secure
4431#ifdef HAVE_SANDBOX
4432 || sandbox != 0
4433#endif
4434 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00004435 errmsg = e_not_allowed_here;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004436 // Check for a "normal" directory or file name in some options.
4437 else if (check_illegal_path_names(opt_idx, varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004438 errmsg = e_invalid_argument;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004439 else if (did_set_cb != NULL)
4440 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004441 args.os_varp = (char_u *)varp;
4442 args.os_idx = opt_idx;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004443 args.os_flags = opt_flags;
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004444 args.os_op = op;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004445 args.os_oldval.string = oldval;
4446 args.os_newval.string = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004447 args.os_errbuf = errbuf;
Christian Brabandtb39b2402023-11-29 11:34:05 +01004448 args.os_errbuflen = errbuflen;
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004449 // Invoke the option specific callback function to validate and apply
4450 // the new option value.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004451 errmsg = did_set_cb(&args);
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004452
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004453 // The 'keymap', 'filetype' and 'syntax' option callback functions
4454 // may change the os_value_checked field.
4455 *value_checked = args.os_value_checked;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004456 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004457
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004458 // If an error is detected, restore the previous value.
Bram Moolenaardac13472019-09-16 21:06:21 +02004459 if (errmsg != NULL)
4460 {
zeertzjqf6782732022-07-27 18:26:03 +01004461 free_string_option(*varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02004462 *varp = oldval;
4463 // When resetting some values, need to act on it.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004464 if (args.os_restore_chartab)
Bram Moolenaardac13472019-09-16 21:06:21 +02004465 (void)init_chartab();
4466 if (varp == &p_hl)
4467 (void)highlight_changed();
4468 }
4469 else
4470 {
4471#ifdef FEAT_EVAL
4472 // Remember where the option was set.
4473 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4474#endif
4475 // Free string options that are in allocated memory.
4476 // Use "free_oldval", because recursiveness may change the flags under
4477 // our fingers (esp. init_highlight()).
4478 if (free_oldval)
4479 free_string_option(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01004480 set_option_flag(opt_idx, P_ALLOCED);
Bram Moolenaardac13472019-09-16 21:06:21 +02004481
4482 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
4483 && is_global_local_option(opt_idx))
4484 {
4485 // global option with local value set to use global value; free
4486 // the local value and make it empty
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004487 char_u *p = get_option_varp_scope(opt_idx, OPT_LOCAL);
Bram Moolenaardac13472019-09-16 21:06:21 +02004488 free_string_option(*(char_u **)p);
4489 *(char_u **)p = empty_option;
4490 }
4491
4492 // May set global value for local option.
4493 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
4494 set_string_option_global(opt_idx, varp);
4495
4496 // Trigger the autocommand only after setting the flags.
4497#ifdef FEAT_SYN_HL
Bram Moolenaardac13472019-09-16 21:06:21 +02004498 if (varp == &(curbuf->b_p_syn))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004499 do_syntax_autocmd(args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004500#endif
4501 else if (varp == &(curbuf->b_p_ft))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004502 do_filetype_autocmd(varp, opt_flags, args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004503#ifdef FEAT_SPELL
4504 if (varp == &(curwin->w_s->b_p_spl))
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004505 do_spelllang_source();
Bram Moolenaardac13472019-09-16 21:06:21 +02004506#endif
4507 }
4508
Bram Moolenaardac13472019-09-16 21:06:21 +02004509 if (varp == &p_mouse)
4510 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004511 if (*p_mouse == NUL)
4512 mch_setmouse(FALSE); // switch mouse off
4513 else
Bram Moolenaardac13472019-09-16 21:06:21 +02004514 setmouse(); // in case 'mouse' changed
4515 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004516
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004517#if defined(FEAT_LUA) || defined(PROTO)
4518 if (varp == &p_rtp)
4519 update_package_paths_in_lua();
4520#endif
4521
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004522#if defined(FEAT_LINEBREAK)
4523 // Changing Formatlistpattern when briopt includes the list setting:
4524 // redraw
4525 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
4526 && curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004527 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004528#endif
4529
Bram Moolenaardac13472019-09-16 21:06:21 +02004530 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004531 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0
4532 && (get_option_flags(opt_idx) & P_HLONLY) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02004533 curwin->w_set_curswant = TRUE;
4534
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004535 if ((opt_flags & OPT_NO_REDRAW) == 0)
4536 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004537#ifdef FEAT_GUI
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004538 // set when changing an option that only requires a redraw in the GUI
4539 int redraw_gui_only = FALSE;
4540
4541 if (varp == &p_go // 'guioptions'
4542 || varp == &p_guifont // 'guifont'
4543# ifdef FEAT_GUI_TABLINE
4544 || varp == &p_gtl // 'guitablabel'
4545 || varp == &p_gtt // 'guitabtooltip'
4546# endif
4547# ifdef FEAT_XFONTSET
4548 || varp == &p_guifontset // 'guifontset'
4549# endif
4550 || varp == &p_guifontwide // 'guifontwide'
Erik S. V. Jansson2f026382024-02-26 22:23:05 +01004551# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004552 || varp == &p_guiligatures // 'guiligatures'
4553# endif
4554 )
4555 redraw_gui_only = TRUE;
4556
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004557 // check redraw when it's not a GUI option or the GUI is active.
4558 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02004559#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004560 check_redraw(get_option_flags(opt_idx));
4561 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004562
4563#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004564 if (args.os_did_swaptcap)
Bram Moolenaardac13472019-09-16 21:06:21 +02004565 {
4566 set_termname((char_u *)"win32");
4567 init_highlight(TRUE, FALSE);
4568 }
4569#endif
4570
4571 return errmsg;
4572}
4573
4574/*
4575 * Check an option that can be a range of string values.
4576 *
4577 * Return OK for correct value, FAIL otherwise.
4578 * Empty is always OK.
4579 */
4580 static int
4581check_opt_strings(
4582 char_u *val,
4583 char **values,
4584 int list) // when TRUE: accept a list of values
4585{
4586 return opt_strings_flags(val, values, NULL, list);
4587}
4588
4589/*
4590 * Handle an option that can be a range of string values.
4591 * Set a flag in "*flagp" for each string present.
4592 *
4593 * Return OK for correct value, FAIL otherwise.
4594 * Empty is always OK.
4595 */
4596 static int
4597opt_strings_flags(
4598 char_u *val, // new value
4599 char **values, // array of valid string values
4600 unsigned *flagp,
4601 int list) // when TRUE: accept a list of values
4602{
4603 int i;
4604 int len;
4605 unsigned new_flags = 0;
4606
4607 while (*val)
4608 {
4609 for (i = 0; ; ++i)
4610 {
4611 if (values[i] == NULL) // val not found in values[]
4612 return FAIL;
4613
4614 len = (int)STRLEN(values[i]);
4615 if (STRNCMP(values[i], val, len) == 0
4616 && ((list && val[len] == ',') || val[len] == NUL))
4617 {
4618 val += len + (val[len] == ',');
4619 new_flags |= (1 << i);
4620 break; // check next item in val list
4621 }
4622 }
4623 }
4624 if (flagp != NULL)
4625 *flagp = new_flags;
4626
4627 return OK;
4628}
4629
4630/*
4631 * return OK if "p" is a valid fileformat name, FAIL otherwise.
4632 */
4633 int
4634check_ff_value(char_u *p)
4635{
4636 return check_opt_strings(p, p_ff_values, FALSE);
4637}
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004638
4639/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004640 * Save the actual shortmess Flags and clear them temporarily to avoid that
4641 * file messages overwrites any output from the following commands.
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004642 *
4643 * Caller must make sure to first call save_clear_shm_value() and then
4644 * restore_shm_value() exactly the same number of times.
4645 */
4646 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004647save_clear_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004648{
4649 if (STRLEN(p_shm) >= SHM_LEN)
4650 {
4651 iemsg(e_internal_error_shortmess_too_long);
4652 return;
4653 }
4654
4655 if (++set_shm_recursive == 1)
4656 {
4657 STRCPY(shm_buf, p_shm);
4658 set_option_value_give_err((char_u *)"shm", 0L, (char_u *)"", 0);
4659 }
4660}
4661
4662/*
4663 * Restore the shortmess Flags set from the save_clear_shm_value() function.
4664 */
4665 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004666restore_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004667{
4668 if (--set_shm_recursive == 0)
4669 {
4670 set_option_value_give_err((char_u *)"shm", 0L, shm_buf, 0);
4671 vim_memset(shm_buf, 0, SHM_LEN);
4672 }
4673}