blob: 517a8358c0d942db0e461afec1e8127a02fc6a8b [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()
Christian Brabandt9162e632025-01-16 19:03:40 +010033static char *(p_dip_values[]) = {"filler", "context:", "iblank", "icase", "iwhite", "iwhiteall", "iwhiteeol", "horizontal", "vertical", "closeoff", "hiddenoff", "foldcolumn:", "followwrap", "internal", "indent-heuristic", "algorithm:", "linematch:", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020034static char *(p_dip_algorithm_values[]) = {"myers", "minimal", "patience", "histogram", NULL};
35#endif
distobs25ac6d62024-07-06 17:50:09 +020036static char *(p_nf_values[]) = {"bin", "octal", "hex", "alpha", "unsigned", "blank", 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};
LemonBoy5247b0b2024-07-12 19:30:58 +020084// Keep in sync with TCL_ flags in option.h
85static char *(p_tcl_values[]) = {"left", "uselast", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020086#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
87static char *(p_toolbar_values[]) = {"text", "icons", "tooltips", "horiz", NULL};
88#endif
89#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
90static char *(p_tbis_values[]) = {"tiny", "small", "medium", "large", "huge", "giant", NULL};
91#endif
Bram Moolenaara1cb1d12019-10-17 23:00:07 +020092#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +020093static char *(p_ttym_values[]) = {"xterm", "xterm2", "dec", "netterm", "jsbterm", "pterm", "urxvt", "sgr", NULL};
94#endif
Gary Johnson53ba05b2021-07-26 22:19:10 +020095static char *(p_ve_values[]) = {"block", "insert", "all", "onemore", "none", "NONE", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020096// Note: Keep this in sync with check_opt_wim()
Girish Palya2bacc3e2025-03-02 22:55:57 +010097static char *(p_wim_values[]) = {"full", "longest", "list", "lastused", "noselect", NULL};
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000098static char *(p_wop_values[]) = {"fuzzy", "tagfile", "pum", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020099#ifdef FEAT_WAK
100static char *(p_wak_values[]) = {"yes", "menu", "no", NULL};
101#endif
102static char *(p_mousem_values[]) = {"extend", "popup", "popup_setpos", "mac", NULL};
103static char *(p_sel_values[]) = {"inclusive", "exclusive", "old", NULL};
104static char *(p_slm_values[]) = {"mouse", "key", "cmd", NULL};
105static char *(p_km_values[]) = {"startsel", "stopsel", NULL};
106#ifdef FEAT_BROWSE
107static char *(p_bsdir_values[]) = {"current", "last", "buffer", NULL};
108#endif
109static char *(p_scbopt_values[]) = {"ver", "hor", "jump", NULL};
110static char *(p_debug_values[]) = {"msg", "throw", "beep", NULL};
111static char *(p_ead_values[]) = {"both", "ver", "hor", NULL};
112static char *(p_buftype_values[]) = {"nofile", "nowrite", "quickfix", "help", "terminal", "acwrite", "prompt", "popup", NULL};
113static char *(p_bufhidden_values[]) = {"hide", "unload", "delete", "wipe", NULL};
Bram Moolenaaraa0489e2020-04-17 19:41:21 +0200114static char *(p_bs_values[]) = {"indent", "eol", "start", "nostop", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200115#ifdef FEAT_FOLDING
116static char *(p_fdm_values[]) = {"manual", "expr", "marker", "indent", "syntax",
117# ifdef FEAT_DIFF
118 "diff",
119# endif
120 NULL};
121static char *(p_fcl_values[]) = {"all", NULL};
122#endif
glepnirf31cfa22025-03-06 21:59:13 +0100123static char *(p_cfc_values[]) = {"keyword", "files", "whole_line", NULL};
glepniredd4ac32025-01-29 18:53:51 +0100124static char *(p_cot_values[]) = {"menu", "menuone", "longest", "preview", "popup", "popuphidden", "noinsert", "noselect", "fuzzy", "nosort", "preinsert", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200125#ifdef BACKSLASH_IN_FILENAME
126static char *(p_csl_values[]) = {"slash", "backslash", NULL};
127#endif
128#ifdef FEAT_SIGNS
129static char *(p_scl_values[]) = {"yes", "no", "auto", "number", NULL};
130#endif
131#if defined(MSWIN) && defined(FEAT_TERMINAL)
132static char *(p_twt_values[]) = {"winpty", "conpty", "", NULL};
133#endif
Luuk van Baalba936f62022-12-15 13:15:39 +0000134static char *(p_sloc_values[]) = {"last", "statusline", "tabline", NULL};
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +0000135static char *(p_sws_values[]) = {"fsync", "sync", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200136
137static int check_opt_strings(char_u *val, char **values, int list);
138static int opt_strings_flags(char_u *val, char **values, unsigned *flagp, int list);
139
140/*
141 * After setting various option values: recompute variables that depend on
142 * option values.
143 */
144 void
145didset_string_options(void)
146{
147 (void)opt_strings_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
148 (void)opt_strings_flags(p_bkc, p_bkc_values, &bkc_flags, TRUE);
149 (void)opt_strings_flags(p_bo, p_bo_values, &bo_flags, TRUE);
glepnirf31cfa22025-03-06 21:59:13 +0100150 (void)opt_strings_flags(p_cfc, p_cfc_values, &cfc_flags, TRUE);
zeertzjq529b9ad2024-06-05 20:27:06 +0200151 (void)opt_strings_flags(p_cot, p_cot_values, &cot_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200152#ifdef FEAT_SESSION
153 (void)opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE);
154 (void)opt_strings_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
155#endif
156#ifdef FEAT_FOLDING
157 (void)opt_strings_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
158#endif
159 (void)opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE);
Yegappan Lakshmanan87018252023-09-20 20:20:04 +0200160 (void)opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200161 (void)opt_strings_flags(p_tc, p_tc_values, &tc_flags, FALSE);
162 (void)opt_strings_flags(p_ve, p_ve_values, &ve_flags, TRUE);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +0200163#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +0200164 (void)opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE);
165#endif
166#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
167 (void)opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags, TRUE);
168#endif
169#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
170 (void)opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE);
171#endif
Sean Dewar39c46b42022-05-12 17:44:29 +0100172 (void)opt_strings_flags(p_swb, p_swb_values, &swb_flags, TRUE);
LemonBoy5247b0b2024-07-12 19:30:58 +0200173 (void)opt_strings_flags(p_tcl, p_tcl_values, &tcl_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200174}
175
Yegappan Lakshmananf9dc2782023-05-11 15:02:56 +0100176#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200177/*
178 * Trigger the OptionSet autocommand.
179 * "opt_idx" is the index of the option being set.
180 * "opt_flags" can be OPT_LOCAL etc.
181 * "oldval" the old value
182 * "oldval_l" the old local value (only non-NULL if global and local value
183 * are set)
184 * "oldval_g" the old global value (only non-NULL if global and local value
185 * are set)
186 * "newval" the new value
187 */
188 void
zeertzjq269aa2b2022-11-28 11:36:50 +0000189trigger_optionset_string(
Bram Moolenaardac13472019-09-16 21:06:21 +0200190 int opt_idx,
191 int opt_flags,
192 char_u *oldval,
193 char_u *oldval_l,
194 char_u *oldval_g,
195 char_u *newval)
196{
197 // Don't do this recursively.
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000198 if (oldval == NULL || newval == NULL
199 || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
200 return;
Bram Moolenaardac13472019-09-16 21:06:21 +0200201
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000202 char_u buf_type[7];
203
204 sprintf((char *)buf_type, "%s",
Bram Moolenaardac13472019-09-16 21:06:21 +0200205 (opt_flags & OPT_LOCAL) ? "local" : "global");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000206 set_vim_var_string(VV_OPTION_OLD, oldval, -1);
207 set_vim_var_string(VV_OPTION_NEW, newval, -1);
208 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
209 if (opt_flags & OPT_LOCAL)
210 {
211 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
212 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
Bram Moolenaardac13472019-09-16 21:06:21 +0200213 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000214 if (opt_flags & OPT_GLOBAL)
215 {
216 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
217 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval, -1);
218 }
219 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
220 {
221 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
222 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval_l, -1);
223 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval_g, -1);
224 }
225 if (opt_flags & OPT_MODELINE)
226 {
227 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
228 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
229 }
230 apply_autocmds(EVENT_OPTIONSET,
231 get_option_fullname(opt_idx), NULL, FALSE,
232 NULL);
233 reset_v_option_vars();
Bram Moolenaardac13472019-09-16 21:06:21 +0200234}
235#endif
236
237 static char *
Mike Williams620f0112023-12-05 15:36:06 +0100238illegal_char(char *errbuf, size_t errbuflen, int c)
Bram Moolenaardac13472019-09-16 21:06:21 +0200239{
240 if (errbuf == NULL)
241 return "";
zeertzjq6a8d2e12024-01-17 20:54:49 +0100242 vim_snprintf(errbuf, errbuflen, _(e_illegal_character_str),
Christian Brabandtb39b2402023-11-29 11:34:05 +0100243 (char *)transchar(c));
Bram Moolenaardac13472019-09-16 21:06:21 +0200244 return errbuf;
245}
246
247/*
248 * Check string options in a buffer for NULL value.
249 */
250 void
251check_buf_options(buf_T *buf)
252{
253 check_string_option(&buf->b_p_bh);
254 check_string_option(&buf->b_p_bt);
255 check_string_option(&buf->b_p_fenc);
256 check_string_option(&buf->b_p_ff);
257#ifdef FEAT_FIND_ID
258 check_string_option(&buf->b_p_def);
259 check_string_option(&buf->b_p_inc);
260# ifdef FEAT_EVAL
261 check_string_option(&buf->b_p_inex);
262# endif
263#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +0100264#if defined(FEAT_EVAL)
Bram Moolenaardac13472019-09-16 21:06:21 +0200265 check_string_option(&buf->b_p_inde);
266 check_string_option(&buf->b_p_indk);
267#endif
268#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
269 check_string_option(&buf->b_p_bexpr);
270#endif
271#if defined(FEAT_CRYPT)
272 check_string_option(&buf->b_p_cm);
273#endif
274 check_string_option(&buf->b_p_fp);
275#if defined(FEAT_EVAL)
276 check_string_option(&buf->b_p_fex);
277#endif
278#ifdef FEAT_CRYPT
279 check_string_option(&buf->b_p_key);
280#endif
281 check_string_option(&buf->b_p_kp);
282 check_string_option(&buf->b_p_mps);
283 check_string_option(&buf->b_p_fo);
284 check_string_option(&buf->b_p_flp);
285 check_string_option(&buf->b_p_isk);
Bram Moolenaardac13472019-09-16 21:06:21 +0200286 check_string_option(&buf->b_p_com);
Bram Moolenaardac13472019-09-16 21:06:21 +0200287#ifdef FEAT_FOLDING
288 check_string_option(&buf->b_p_cms);
289#endif
290 check_string_option(&buf->b_p_nf);
Bram Moolenaardac13472019-09-16 21:06:21 +0200291 check_string_option(&buf->b_p_qe);
Bram Moolenaardac13472019-09-16 21:06:21 +0200292#ifdef FEAT_SYN_HL
293 check_string_option(&buf->b_p_syn);
294 check_string_option(&buf->b_s.b_syn_isk);
295#endif
296#ifdef FEAT_SPELL
297 check_string_option(&buf->b_s.b_p_spc);
298 check_string_option(&buf->b_s.b_p_spf);
299 check_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +0200300 check_string_option(&buf->b_s.b_p_spo);
Bram Moolenaardac13472019-09-16 21:06:21 +0200301#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200302 check_string_option(&buf->b_p_sua);
Bram Moolenaardac13472019-09-16 21:06:21 +0200303 check_string_option(&buf->b_p_cink);
304 check_string_option(&buf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +0100305 check_string_option(&buf->b_p_cinsd);
Bram Moolenaardac13472019-09-16 21:06:21 +0200306 parse_cino(buf);
Bram Moolenaar49846fb2022-10-15 16:05:33 +0100307 check_string_option(&buf->b_p_lop);
Bram Moolenaardac13472019-09-16 21:06:21 +0200308 check_string_option(&buf->b_p_ft);
Bram Moolenaardac13472019-09-16 21:06:21 +0200309 check_string_option(&buf->b_p_cinw);
zeertzjq529b9ad2024-06-05 20:27:06 +0200310 check_string_option(&buf->b_p_cot);
Bram Moolenaardac13472019-09-16 21:06:21 +0200311 check_string_option(&buf->b_p_cpt);
312#ifdef FEAT_COMPL_FUNC
313 check_string_option(&buf->b_p_cfu);
314 check_string_option(&buf->b_p_ofu);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +0100315 check_string_option(&buf->b_p_tsrfu);
Bram Moolenaardac13472019-09-16 21:06:21 +0200316#endif
317#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +0100318 check_string_option(&buf->b_p_ffu);
Bram Moolenaardac13472019-09-16 21:06:21 +0200319 check_string_option(&buf->b_p_tfu);
320#endif
321#ifdef FEAT_KEYMAP
322 check_string_option(&buf->b_p_keymap);
323#endif
324#ifdef FEAT_QUICKFIX
325 check_string_option(&buf->b_p_gp);
326 check_string_option(&buf->b_p_mp);
327 check_string_option(&buf->b_p_efm);
328#endif
329 check_string_option(&buf->b_p_ep);
330 check_string_option(&buf->b_p_path);
331 check_string_option(&buf->b_p_tags);
332 check_string_option(&buf->b_p_tc);
333 check_string_option(&buf->b_p_dict);
334 check_string_option(&buf->b_p_tsr);
Bram Moolenaardac13472019-09-16 21:06:21 +0200335 check_string_option(&buf->b_p_lw);
Bram Moolenaardac13472019-09-16 21:06:21 +0200336 check_string_option(&buf->b_p_bkc);
337 check_string_option(&buf->b_p_menc);
338#ifdef FEAT_VARTABS
339 check_string_option(&buf->b_p_vsts);
340 check_string_option(&buf->b_p_vts);
341#endif
342}
343
344/*
345 * Free the string allocated for an option.
346 * Checks for the string being empty_option. This may happen if we're out of
347 * memory, vim_strsave() returned NULL, which was replaced by empty_option by
348 * check_options().
349 * Does NOT check for P_ALLOCED flag!
350 */
351 void
352free_string_option(char_u *p)
353{
354 if (p != empty_option)
355 vim_free(p);
356}
357
358 void
359clear_string_option(char_u **pp)
360{
361 if (*pp != empty_option)
362 vim_free(*pp);
363 *pp = empty_option;
364}
365
366 void
367check_string_option(char_u **pp)
368{
369 if (*pp == NULL)
370 *pp = empty_option;
371}
372
373/*
374 * Set global value for string option when it's a local option.
375 */
376 static void
377set_string_option_global(
378 int opt_idx, // option index
379 char_u **varp) // pointer to option variable
380{
381 char_u **p, *s;
382
383 // the global value is always allocated
384 if (is_window_local_option(opt_idx))
385 p = (char_u **)GLOBAL_WO(varp);
386 else
387 p = (char_u **)get_option_var(opt_idx);
388 if (!is_global_option(opt_idx)
389 && p != varp
390 && (s = vim_strsave(*varp)) != NULL)
391 {
392 free_string_option(*p);
393 *p = s;
394 }
395}
396
397/*
398 * Set a string option to a new value (without checking the effect).
399 * The string is copied into allocated memory.
400 * if ("opt_idx" == -1) "name" is used, otherwise "opt_idx" is used.
401 * When "set_sid" is zero set the scriptID to current_sctx.sc_sid. When
402 * "set_sid" is SID_NONE don't set the scriptID. Otherwise set the scriptID to
403 * "set_sid".
404 */
405 void
406set_string_option_direct(
407 char_u *name,
408 int opt_idx,
409 char_u *val,
410 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
411 int set_sid UNUSED)
412{
413 char_u *s;
414 char_u **varp;
415 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
416 int idx = opt_idx;
417
418 if (idx == -1) // use name
419 {
420 idx = findoption(name);
421 if (idx < 0) // not found (should not happen)
422 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000423 semsg(_(e_internal_error_str), "set_string_option_direct()");
RestorerZ68ebcee2023-05-31 17:12:14 +0100424 siemsg("For option %s", name);
Bram Moolenaardac13472019-09-16 21:06:21 +0200425 return;
426 }
427 }
428
429 if (is_hidden_option(idx)) // can't set hidden option
430 return;
431
432 s = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000433 if (s == NULL)
434 return;
435
436 varp = (char_u **)get_option_varp_scope(idx,
437 both ? OPT_LOCAL : opt_flags);
438 if ((opt_flags & OPT_FREE) && (get_option_flags(idx) & P_ALLOCED))
439 free_string_option(*varp);
440 *varp = s;
441
442 // For buffer/window local option may also set the global value.
443 if (both)
444 set_string_option_global(idx, varp);
445
446 set_option_flag(idx, P_ALLOCED);
447
448 // When setting both values of a global option with a local value,
449 // make the local value empty, so that the global value is used.
450 if (is_global_local_option(idx) && both)
Bram Moolenaardac13472019-09-16 21:06:21 +0200451 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000452 free_string_option(*varp);
453 *varp = empty_option;
Bram Moolenaardac13472019-09-16 21:06:21 +0200454 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000455# ifdef FEAT_EVAL
456 if (set_sid != SID_NONE)
457 {
458 sctx_T script_ctx;
459
460 if (set_sid == 0)
461 script_ctx = current_sctx;
462 else
463 {
464 script_ctx.sc_sid = set_sid;
465 script_ctx.sc_seq = 0;
466 script_ctx.sc_lnum = 0;
467 script_ctx.sc_version = 1;
468 }
469 set_option_sctx_idx(idx, opt_flags, script_ctx);
470 }
471# endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200472}
473
Dominique Pellee764d1b2023-03-12 21:20:59 +0000474#if defined(FEAT_PROP_POPUP) || \
475 (defined(FEAT_DIFF) && defined(FEAT_FOLDING)) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200476/*
477 * Like set_string_option_direct(), but for a window-local option in "wp".
478 * Blocks autocommands to avoid the old curwin becoming invalid.
479 */
480 void
481set_string_option_direct_in_win(
482 win_T *wp,
483 char_u *name,
484 int opt_idx,
485 char_u *val,
486 int opt_flags,
487 int set_sid)
488{
489 win_T *save_curwin = curwin;
490
491 block_autocmds();
492 curwin = wp;
493 curbuf = curwin->w_buffer;
494 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
495 curwin = save_curwin;
496 curbuf = curwin->w_buffer;
497 unblock_autocmds();
498}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000499#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200500
Dominique Pelle748b3082022-01-08 12:41:16 +0000501#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200502/*
503 * Like set_string_option_direct(), but for a buffer-local option in "buf".
504 * Blocks autocommands to avoid the old curbuf becoming invalid.
505 */
506 void
507set_string_option_direct_in_buf(
508 buf_T *buf,
509 char_u *name,
510 int opt_idx,
511 char_u *val,
512 int opt_flags,
513 int set_sid)
514{
515 buf_T *save_curbuf = curbuf;
516
517 block_autocmds();
518 curbuf = buf;
519 curwin->w_buffer = curbuf;
520 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
521 curbuf = save_curbuf;
522 curwin->w_buffer = curbuf;
523 unblock_autocmds();
524}
Dominique Pelle748b3082022-01-08 12:41:16 +0000525#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200526
527/*
528 * Set a string option to a new value, and handle the effects.
529 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100530 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaardac13472019-09-16 21:06:21 +0200531 */
532 char *
533set_string_option(
534 int opt_idx,
535 char_u *value,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +0000536 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Christian Brabandtb39b2402023-11-29 11:34:05 +0100537 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +0100538 size_t errbuflen)
Bram Moolenaardac13472019-09-16 21:06:21 +0200539{
540 char_u *s;
541 char_u **varp;
542 char_u *oldval;
543#if defined(FEAT_EVAL)
544 char_u *oldval_l = NULL;
545 char_u *oldval_g = NULL;
546 char_u *saved_oldval = NULL;
547 char_u *saved_oldval_l = NULL;
548 char_u *saved_oldval_g = NULL;
549 char_u *saved_newval = NULL;
550#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100551 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +0200552 int value_checked = FALSE;
553
554 if (is_hidden_option(opt_idx)) // don't set hidden option
555 return NULL;
556
Bram Moolenaar7f009df2020-03-16 20:27:38 +0100557 s = vim_strsave(value == NULL ? (char_u *)"" : value);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000558 if (s == NULL)
559 return NULL;
560
561 varp = (char_u **)get_option_varp_scope(opt_idx,
562 (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
563 ? (is_global_local_option(opt_idx)
564 ? OPT_GLOBAL : OPT_LOCAL)
565 : opt_flags);
566 oldval = *varp;
567#if defined(FEAT_EVAL)
568 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +0200569 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000570 oldval_l = *(char_u **)get_option_varp_scope(opt_idx, OPT_LOCAL);
571 oldval_g = *(char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
Bram Moolenaardac13472019-09-16 21:06:21 +0200572 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000573#endif
574 *varp = s;
575
576#if defined(FEAT_EVAL)
577 if (!starting
578# ifdef FEAT_CRYPT
579 && !is_crypt_key_option(opt_idx)
580# endif
581 )
582 {
583 if (oldval_l != NULL)
584 saved_oldval_l = vim_strsave(oldval_l);
585 if (oldval_g != NULL)
586 saved_oldval_g = vim_strsave(oldval_g);
587 saved_oldval = vim_strsave(oldval);
588 saved_newval = vim_strsave(s);
589 }
590#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000591 if ((errmsg = did_set_string_option(opt_idx, varp, oldval, value, errbuf,
Christian Brabandtb39b2402023-11-29 11:34:05 +0100592 errbuflen, opt_flags, OP_NONE, &value_checked)) == NULL)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000593 did_set_option(opt_idx, opt_flags, TRUE, value_checked);
594
595#if defined(FEAT_EVAL)
596 // call autocommand after handling side effects
597 if (errmsg == NULL)
598 trigger_optionset_string(opt_idx, opt_flags,
599 saved_oldval, saved_oldval_l,
600 saved_oldval_g, saved_newval);
601 vim_free(saved_oldval);
602 vim_free(saved_oldval_l);
603 vim_free(saved_oldval_g);
604 vim_free(saved_newval);
605#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100606 return errmsg;
Bram Moolenaardac13472019-09-16 21:06:21 +0200607}
608
609/*
610 * Return TRUE if "val" is a valid 'filetype' name.
611 * Also used for 'syntax' and 'keymap'.
612 */
613 static int
614valid_filetype(char_u *val)
615{
616 return valid_name(val, ".-_");
617}
618
619#ifdef FEAT_STL_OPT
620/*
621 * Check validity of options with the 'statusline' format.
zeertzjq5dc294a2022-04-15 13:17:57 +0100622 * Return an untranslated error message or NULL.
Bram Moolenaardac13472019-09-16 21:06:21 +0200623 */
624 static char *
625check_stl_option(char_u *s)
626{
Bram Moolenaardac13472019-09-16 21:06:21 +0200627 int groupdepth = 0;
Christian Brabandtb39b2402023-11-29 11:34:05 +0100628 static char errbuf[ERR_BUFLEN];
629 int errbuflen = ERR_BUFLEN;
Bram Moolenaardac13472019-09-16 21:06:21 +0200630
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100631 while (*s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200632 {
633 // Check for valid keys after % sequences
634 while (*s && *s != '%')
635 s++;
636 if (!*s)
637 break;
638 s++;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +0000639 if (*s == '%' || *s == STL_TRUNCMARK || *s == STL_SEPARATE)
Bram Moolenaardac13472019-09-16 21:06:21 +0200640 {
641 s++;
642 continue;
643 }
644 if (*s == ')')
645 {
646 s++;
647 if (--groupdepth < 0)
648 break;
649 continue;
650 }
651 if (*s == '-')
652 s++;
653 while (VIM_ISDIGIT(*s))
654 s++;
655 if (*s == STL_USER_HL)
656 continue;
657 if (*s == '.')
658 {
659 s++;
660 while (*s && VIM_ISDIGIT(*s))
661 s++;
662 }
663 if (*s == '(')
664 {
665 groupdepth++;
666 continue;
667 }
668 if (vim_strchr(STL_ALL, *s) == NULL)
669 {
Christian Brabandtb39b2402023-11-29 11:34:05 +0100670 return illegal_char(errbuf, errbuflen, *s);
Bram Moolenaardac13472019-09-16 21:06:21 +0200671 }
672 if (*s == '{')
673 {
zeertzjq5dc294a2022-04-15 13:17:57 +0100674 int reevaluate = (*++s == '%');
shadmansaleh30e3de22021-05-15 17:23:28 +0200675
zeertzjq5dc294a2022-04-15 13:17:57 +0100676 if (reevaluate && *++s == '}')
677 // "}" is not allowed immediately after "%{%"
Christian Brabandtb39b2402023-11-29 11:34:05 +0100678 return illegal_char(errbuf, errbuflen, '}');
shadmansaleh30e3de22021-05-15 17:23:28 +0200679 while ((*s != '}' || (reevaluate && s[-1] != '%')) && *s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200680 s++;
681 if (*s != '}')
zeertzjq5dc294a2022-04-15 13:17:57 +0100682 return e_unclosed_expression_sequence;
Bram Moolenaardac13472019-09-16 21:06:21 +0200683 }
684 }
Bram Moolenaardac13472019-09-16 21:06:21 +0200685 if (groupdepth != 0)
zeertzjq5dc294a2022-04-15 13:17:57 +0100686 return e_unbalanced_groups;
Bram Moolenaardac13472019-09-16 21:06:21 +0200687 return NULL;
688}
689#endif
690
691/*
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +0000692 * Check for a "normal" directory or file name in some options. Disallow a
693 * path separator (slash and/or backslash), wildcards and characters that are
694 * often illegal in a file name. Be more permissive if "secure" is off.
695 */
696 static int
697check_illegal_path_names(int opt_idx, char_u **varp)
698{
699 return (((get_option_flags(opt_idx) & P_NFNAME)
700 && vim_strpbrk(*varp, (char_u *)(secure
701 ? "/\\*?[|;&<>\r\n" : "/\\*?[<>\r\n")) != NULL)
702 || ((get_option_flags(opt_idx) & P_NDNAME)
703 && vim_strpbrk(*varp, (char_u *)"*?[|;&<>\r\n") != NULL));
704}
705
706/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000707 * An option that accepts a list of flags is changed.
708 * e.g. 'viewoptions', 'switchbuf', 'casemap', etc.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000709 */
710 static char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000711did_set_opt_flags(char_u *val, char **values, unsigned *flagp, int list)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000712{
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000713 if (opt_strings_flags(val, values, flagp, list) == FAIL)
714 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000715
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000716 return NULL;
717}
718
719/*
720 * An option that accepts a list of string values is changed.
721 * e.g. 'nrformats', 'scrollopt', 'wildoptions', etc.
722 */
723 static char *
724did_set_opt_strings(char_u *val, char **values, int list)
725{
726 return did_set_opt_flags(val, values, NULL, list);
727}
728
729/*
730 * An option which is a list of flags is set. Valid values are in 'flags'.
731 */
732 static char *
Christian Brabandtb39b2402023-11-29 11:34:05 +0100733did_set_option_listflag(
734 char_u *val,
735 char_u *flags,
736 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +0100737 size_t errbuflen)
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000738{
739 char_u *s;
740
Yegappan Lakshmananc727b192023-03-03 12:26:15 +0000741 for (s = val; *s; ++s)
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000742 if (vim_strchr(flags, *s) == NULL)
Christian Brabandtb39b2402023-11-29 11:34:05 +0100743 return illegal_char(errbuf, errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000744
745 return NULL;
746}
747
748/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200749 * Expand an option that accepts a list of fixed string values with known
750 * number of items.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200751 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200752 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200753expand_set_opt_string(
754 optexpand_T *args,
755 char **values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +0200756 size_t numValues,
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200757 int *numMatches,
758 char_u ***matches)
759{
760 char_u *p;
761 regmatch_T *regmatch = args->oe_regmatch;
762 int include_orig_val = args->oe_include_orig_val;
763 char_u *option_val = args->oe_opt_value;
764
765 // Assume numValues is small since they are fixed enums, so just allocate
766 // upfront instead of needing two passes to calculate output size.
767 *matches = ALLOC_MULT(char_u *, numValues + 1);
768 if (*matches == NULL)
769 return FAIL;
770
771 int count = 0;
772
773 if (include_orig_val && *option_val != NUL)
774 {
775 p = vim_strsave(option_val);
776 if (p == NULL)
777 {
778 VIM_CLEAR(*matches);
779 return FAIL;
780 }
781 (*matches)[count++] = p;
782 }
783
784 for (char **val = values; *val != NULL; val++)
785 {
786 if (include_orig_val && *option_val != NUL)
787 {
788 if (STRCMP((char_u*)*val, option_val) == 0)
789 continue;
790 }
791 if (vim_regexec(regmatch, (char_u*)(*val), (colnr_T)0))
792 {
793 p = vim_strsave((char_u*)*val);
794 if (p == NULL)
795 {
796 if (count == 0)
797 {
798 VIM_CLEAR(*matches);
799 return FAIL;
800 }
801 else
802 break;
803 }
804 (*matches)[count++] = p;
805 }
806 }
807 if (count == 0)
808 {
809 VIM_CLEAR(*matches);
810 return FAIL;
811 }
812 *numMatches = count;
813 return OK;
814}
815
816static char_u *set_opt_callback_orig_option = NULL;
817static char_u *((*set_opt_callback_func)(expand_T *, int));
818
819/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200820 * Callback used by expand_set_opt_generic to also include the original value
821 * as the first item.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200822 */
823 static char_u *
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200824expand_set_opt_generic_cb(expand_T *xp, int idx)
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200825{
826 if (idx == 0)
827 {
828 if (set_opt_callback_orig_option != NULL)
829 return set_opt_callback_orig_option;
830 else
831 return (char_u *)""; // empty strings are ignored
832 }
833 return set_opt_callback_func(xp, idx - 1);
834}
835
836/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200837 * Expand an option with a callback that iterates through a list of possible
838 * names using an index.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200839 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200840 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200841expand_set_opt_generic(
842 optexpand_T *args,
843 char_u *((*func)(expand_T *, int)),
844 int *numMatches,
845 char_u ***matches)
846{
847 int ret;
848
849 set_opt_callback_orig_option = args->oe_include_orig_val ?
850 args->oe_opt_value : NULL;
851 set_opt_callback_func = func;
852
853 ret = ExpandGeneric(
854 (char_u*)"", // not using fuzzy as currently EXPAND_STRING_SETTING doesn't use it
855 args->oe_xp,
856 args->oe_regmatch,
857 matches,
858 numMatches,
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200859 expand_set_opt_generic_cb,
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200860 FALSE);
861
862 set_opt_callback_orig_option = NULL;
863 set_opt_callback_func = NULL;
864 return ret;
865}
866
Christian Brabandt9960ebc2023-10-05 22:17:09 +0200867# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200868static garray_T *expand_cb_ga;
869static optexpand_T *expand_cb_args;
870
871/*
872 * Callback provided to a function in expand_set_opt_callback. Will perform
873 * regex matching against the value and add to the list.
874 *
875 * Returns OK usually. Returns FAIL if it failed to allocate memory, and the
876 * caller should terminate the enumeration.
877 */
878 static int
879expand_set_opt_callback_cb(char_u *val)
880{
881 regmatch_T *regmatch = expand_cb_args->oe_regmatch;
882 expand_T *xp = expand_cb_args->oe_xp;
883 garray_T *ga = expand_cb_ga;
884 char_u *str;
885
886 if (val == NULL || *val == NUL)
887 return OK;
888
889 if (xp->xp_pattern[0] != NUL &&
890 !vim_regexec(regmatch, val, (colnr_T)0))
891 return OK;
892
893 str = vim_strsave_escaped(val, (char_u *)" \t\\");
894
895 if (str == NULL)
896 return FAIL;
897
898 if (ga_grow(ga, 1) == FAIL)
899 {
900 vim_free(str);
901 return FAIL;
902 }
903
904 ((char_u **)ga->ga_data)[ga->ga_len] = str;
905 ++ga->ga_len;
906 return OK;
907}
908
909/*
910 * Expand an option with a provided function that takes a callback. The
911 * function will enumerate through all options and call the callback to add it
912 * to the list.
913 *
914 * "func" is the enumerator function that will generate the list of options.
915 * "func_params" is a single parameter that will be passed to func.
916 */
917 static int
918expand_set_opt_callback(
919 optexpand_T *args,
920 void (*func)(optexpand_T *, void* params, int (*cb)(char_u *val)),
921 void *func_params,
922 int *numMatches,
923 char_u ***matches)
924{
925 garray_T ga;
926 int include_orig_val = args->oe_include_orig_val;
927 char_u *option_val = args->oe_opt_value;
928
929 ga_init2(&ga, sizeof(char *), 30);
930
931 if (include_orig_val && *option_val != NUL)
932 {
933 char_u *p = vim_strsave(option_val);
934 if (p == NULL)
935 return FAIL;
936 if (ga_grow(&ga, 1) == FAIL)
937 {
938 vim_free(p);
939 return FAIL;
940 }
941 ((char_u **)ga.ga_data)[ga.ga_len] = p;
942 ++ga.ga_len;
943 }
944
945 expand_cb_ga = &ga;
946 expand_cb_args = args;
947
948 func(args, func_params, expand_set_opt_callback_cb);
949
950 expand_cb_ga = NULL;
951 expand_cb_args = NULL;
952
953 *matches = ga.ga_data;
954 *numMatches = ga.ga_len;
955 return OK;
956}
Christian Brabandt9960ebc2023-10-05 22:17:09 +0200957#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200958
959/*
960 * Expand an option which is a list of flags.
961 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200962 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200963expand_set_opt_listflag(
964 optexpand_T *args,
965 char_u *flags,
966 int *numMatches,
967 char_u ***matches)
968{
969 char_u *p;
970 char_u *option_val = args->oe_opt_value;
971 char_u *cmdline_val = args->oe_set_arg;
972 int append = args->oe_append;
973 int include_orig_val = args->oe_include_orig_val && (*option_val != NUL);
974
Yee Cheng Chin6d113472023-10-02 21:38:39 +0200975 size_t num_flags = STRLEN(flags);
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200976
977 // Assume we only have small number of flags, so just allocate max size.
978 *matches = ALLOC_MULT(char_u *, num_flags + 1);
979 if (*matches == NULL)
980 return FAIL;
981
982 int count = 0;
983
984 if (include_orig_val)
985 {
986 p = vim_strsave(option_val);
987 if (p == NULL)
988 {
989 VIM_CLEAR(*matches);
990 return FAIL;
991 }
992 (*matches)[count++] = p;
993 }
994
995 for (char_u *flag = flags; *flag != NUL; flag++)
996 {
997 if (append && vim_strchr(option_val, *flag) != NULL)
998 continue;
999
1000 if (vim_strchr(cmdline_val, *flag) == NULL)
1001 {
1002 if (include_orig_val
1003 && option_val[1] == NUL
1004 && *flag == option_val[0])
1005 {
1006 // This value is already used as the first choice as it's the
1007 // existing flag. Just skip it to avoid duplicate.
1008 continue;
1009 }
1010 p = vim_strnsave(flag, 1);
1011 if (p == NULL)
1012 {
1013 if (count == 0)
1014 {
1015 VIM_CLEAR(*matches);
1016 return FAIL;
1017 }
1018 else
1019 break;
1020 }
1021 (*matches)[count++] = p;
1022 }
1023 }
1024
1025 if (count == 0)
1026 {
1027 VIM_CLEAR(*matches);
1028 return FAIL;
1029 }
1030 *numMatches = count;
1031 return OK;
1032}
1033
1034/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001035 * The 'ambiwidth' option is changed.
1036 */
1037 char *
1038did_set_ambiwidth(optset_T *args UNUSED)
1039{
1040 if (check_opt_strings(p_ambw, p_ambw_values, FALSE) != OK)
1041 return e_invalid_argument;
1042
1043 return check_chars_options();
1044}
1045
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001046 int
1047expand_set_ambiwidth(optexpand_T *args, int *numMatches, char_u ***matches)
1048{
1049 return expand_set_opt_string(
1050 args,
1051 p_ambw_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001052 ARRAY_LENGTH(p_ambw_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001053 numMatches,
1054 matches);
1055}
1056
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001057/*
1058 * The 'background' option is changed.
1059 */
1060 char *
Gregory Anders83ad2722024-01-03 19:48:51 +01001061did_set_background(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001062{
1063 if (check_opt_strings(p_bg, p_bg_values, FALSE) == FAIL)
1064 return e_invalid_argument;
1065
Gregory Anders83ad2722024-01-03 19:48:51 +01001066 if (args->os_oldval.string != NULL && args->os_oldval.string[0] == *p_bg)
1067 // Value was not changed
1068 return NULL;
1069
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001070#ifdef FEAT_EVAL
1071 int dark = (*p_bg == 'd');
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001072#endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001073
1074 init_highlight(FALSE, FALSE);
1075
1076#ifdef FEAT_EVAL
1077 if (dark != (*p_bg == 'd')
1078 && get_var_value((char_u *)"g:colors_name") != NULL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001079 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001080 // The color scheme must have set 'background' back to another
1081 // value, that's not what we want here. Disable the color
1082 // scheme and set the colors again.
1083 do_unlet((char_u *)"g:colors_name", TRUE);
1084 free_string_option(p_bg);
1085 p_bg = vim_strsave((char_u *)(dark ? "dark" : "light"));
1086 check_string_option(&p_bg);
1087 init_highlight(FALSE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001088 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001089#endif
1090#ifdef FEAT_TERMINAL
1091 term_update_colors_all();
1092#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001093
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001094 return NULL;
1095}
1096
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001097 int
1098expand_set_background(optexpand_T *args, int *numMatches, char_u ***matches)
1099{
1100 return expand_set_opt_string(
1101 args,
1102 p_bg_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001103 ARRAY_LENGTH(p_bg_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001104 numMatches,
1105 matches);
1106}
1107
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001108/*
1109 * The 'backspace' option is changed.
1110 */
1111 char *
1112did_set_backspace(optset_T *args UNUSED)
1113{
1114 if (VIM_ISDIGIT(*p_bs))
1115 {
1116 if (*p_bs > '3' || p_bs[1] != NUL)
1117 return e_invalid_argument;
1118 }
1119 else if (check_opt_strings(p_bs, p_bs_values, TRUE) != OK)
1120 return e_invalid_argument;
1121
1122 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001123}
1124
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001125 int
1126expand_set_backspace(optexpand_T *args, int *numMatches, char_u ***matches)
1127{
1128 return expand_set_opt_string(
1129 args,
1130 p_bs_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001131 ARRAY_LENGTH(p_bs_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001132 numMatches,
1133 matches);
1134}
1135
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001136/*
1137 * The 'backupcopy' option is changed.
1138 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001139 char *
1140did_set_backupcopy(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001141{
1142 char_u *bkc = p_bkc;
1143 unsigned int *flags = &bkc_flags;
1144 char *errmsg = NULL;
1145
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001146 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001147 {
1148 bkc = curbuf->b_p_bkc;
1149 flags = &curbuf->b_bkc_flags;
1150 }
zeertzjq46dcd842024-11-03 09:10:50 +01001151 else if (!(args->os_flags & OPT_GLOBAL))
1152 // When using :set, clear the local flags.
1153 curbuf->b_bkc_flags = 0;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001154
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001155 if ((args->os_flags & OPT_LOCAL) && *bkc == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001156 // make the local value empty: use the global value
1157 *flags = 0;
1158 else
1159 {
1160 if (opt_strings_flags(bkc, p_bkc_values, flags, TRUE) != OK)
1161 errmsg = e_invalid_argument;
1162 if ((((int)*flags & BKC_AUTO) != 0)
1163 + (((int)*flags & BKC_YES) != 0)
1164 + (((int)*flags & BKC_NO) != 0) != 1)
1165 {
1166 // Must have exactly one of "auto", "yes" and "no".
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001167 (void)opt_strings_flags(args->os_oldval.string, p_bkc_values,
1168 flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001169 errmsg = e_invalid_argument;
1170 }
1171 }
1172
1173 return errmsg;
1174}
1175
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001176 int
1177expand_set_backupcopy(optexpand_T *args, int *numMatches, char_u ***matches)
1178{
1179 return expand_set_opt_string(
1180 args,
1181 p_bkc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001182 ARRAY_LENGTH(p_bkc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001183 numMatches,
1184 matches);
1185}
1186
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001187/*
1188 * The 'backupext' or the 'patchmode' option is changed.
1189 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001190 char *
1191did_set_backupext_or_patchmode(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001192{
1193 if (STRCMP(*p_bex == '.' ? p_bex + 1 : p_bex,
1194 *p_pm == '.' ? p_pm + 1 : p_pm) == 0)
1195 return e_backupext_and_patchmode_are_equal;
1196
1197 return NULL;
1198}
1199
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001200/*
1201 * The 'belloff' option is changed.
1202 */
1203 char *
1204did_set_belloff(optset_T *args UNUSED)
1205{
1206 return did_set_opt_flags(p_bo, p_bo_values, &bo_flags, TRUE);
1207}
1208
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001209 int
1210expand_set_belloff(optexpand_T *args, int *numMatches, char_u ***matches)
1211{
1212 return expand_set_opt_string(
1213 args,
1214 p_bo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001215 ARRAY_LENGTH(p_bo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001216 numMatches,
1217 matches);
1218}
1219
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001220#if defined(FEAT_LINEBREAK) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001221/*
zeertzjqeac3fdc2024-02-03 18:08:09 +01001222 * The 'breakat' option is changed.
1223 */
1224 char *
1225did_set_breakat(optset_T *args UNUSED)
1226{
1227 char_u *p;
1228 int i;
1229
1230 for (i = 0; i < 256; i++)
1231 breakat_flags[i] = FALSE;
1232
1233 if (p_breakat != NULL)
1234 for (p = p_breakat; *p; p++)
1235 breakat_flags[*p] = TRUE;
1236
1237 return NULL;
1238}
1239
1240/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001241 * The 'breakindentopt' option is changed.
1242 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001243 char *
Millyb38700a2024-10-22 22:59:39 +02001244did_set_breakindentopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001245{
Millyb38700a2024-10-22 22:59:39 +02001246 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001247
Millyb38700a2024-10-22 22:59:39 +02001248 if (briopt_check(*varp, varp == &curwin->w_p_briopt ? curwin : NULL)
1249 == FAIL)
1250 return e_invalid_argument;
1251
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001252 // list setting requires a redraw
Millyb38700a2024-10-22 22:59:39 +02001253 if (varp == &curwin->w_p_briopt && curwin->w_briopt_list)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001254 redraw_all_later(UPD_NOT_VALID);
1255
Millyb38700a2024-10-22 22:59:39 +02001256 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001257}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001258
1259 int
1260expand_set_breakindentopt(optexpand_T *args, int *numMatches, char_u ***matches)
1261{
1262 return expand_set_opt_string(
1263 args,
1264 p_briopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001265 ARRAY_LENGTH(p_briopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001266 numMatches,
1267 matches);
1268}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001269#endif
1270
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001271#if defined(FEAT_BROWSE) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001272/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001273 * The 'browsedir' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001274 */
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00001275 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001276did_set_browsedir(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001277{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001278 if (check_opt_strings(p_bsdir, p_bsdir_values, FALSE) != OK
1279 && !mch_isdir(p_bsdir))
1280 return e_invalid_argument;
1281
1282 return NULL;
1283}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001284
1285 int
1286expand_set_browsedir(optexpand_T *args, int *numMatches, char_u ***matches)
1287{
1288 return expand_set_opt_string(
1289 args,
1290 p_bsdir_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001291 ARRAY_LENGTH(p_bsdir_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001292 numMatches,
1293 matches);
1294}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001295#endif
1296
1297/*
1298 * The 'bufhidden' option is changed.
1299 */
1300 char *
1301did_set_bufhidden(optset_T *args UNUSED)
1302{
1303 return did_set_opt_strings(curbuf->b_p_bh, p_bufhidden_values, FALSE);
1304}
1305
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001306 int
1307expand_set_bufhidden(optexpand_T *args, int *numMatches, char_u ***matches)
1308{
1309 return expand_set_opt_string(
1310 args,
1311 p_bufhidden_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001312 ARRAY_LENGTH(p_bufhidden_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001313 numMatches,
1314 matches);
1315}
1316
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001317/*
1318 * The 'buftype' option is changed.
1319 */
1320 char *
1321did_set_buftype(optset_T *args UNUSED)
1322{
1323 if (check_opt_strings(curbuf->b_p_bt, p_buftype_values, FALSE) != OK)
1324 return e_invalid_argument;
1325
1326 if (curwin->w_status_height)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001327 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001328 curwin->w_redr_status = TRUE;
1329 redraw_later(UPD_VALID);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001330 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001331 curbuf->b_help = (curbuf->b_p_bt[0] == 'h');
1332 redraw_titles();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001333
1334 return NULL;
1335}
1336
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001337 int
1338expand_set_buftype(optexpand_T *args, int *numMatches, char_u ***matches)
1339{
1340 return expand_set_opt_string(
1341 args,
1342 p_buftype_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001343 ARRAY_LENGTH(p_buftype_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001344 numMatches,
1345 matches);
1346}
1347
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001348/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001349 * The 'casemap' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001350 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001351 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001352did_set_casemap(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001353{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001354 return did_set_opt_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
1355}
1356
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001357 int
1358expand_set_casemap(optexpand_T *args, int *numMatches, char_u ***matches)
1359{
1360 return expand_set_opt_string(
1361 args,
1362 p_cmp_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001363 ARRAY_LENGTH(p_cmp_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001364 numMatches,
1365 matches);
1366}
1367
1368#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1369 int
1370expand_set_clipboard(optexpand_T *args, int *numMatches, char_u ***matches)
1371{
1372 return expand_set_opt_string(
1373 args,
1374 p_cb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001375 ARRAY_LENGTH(p_cb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001376 numMatches,
1377 matches);
1378}
1379#endif
1380
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001381/*
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001382 * The global 'listchars' or 'fillchars' option is changed.
1383 */
1384 static char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01001385did_set_global_listfillchars(char_u *val, int opt_lcs, int opt_flags,
1386 char *errbuf, size_t errbuflen)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001387{
1388 char *errmsg = NULL;
1389 char_u **local_ptr = opt_lcs ? &curwin->w_p_lcs : &curwin->w_p_fcs;
1390
1391 // only apply the global value to "curwin" when it does not have a
1392 // local value
1393 if (opt_lcs)
1394 errmsg = set_listchars_option(curwin, val,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001395 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL),
1396 errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001397 else
1398 errmsg = set_fillchars_option(curwin, val,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001399 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL),
1400 errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001401 if (errmsg != NULL)
1402 return errmsg;
1403
1404 tabpage_T *tp;
1405 win_T *wp;
1406
1407 // If the current window is set to use the global
1408 // 'listchars'/'fillchars' value, clear the window-local value.
1409 if (!(opt_flags & OPT_GLOBAL))
1410 clear_string_option(local_ptr);
1411 FOR_ALL_TAB_WINDOWS(tp, wp)
1412 {
1413 // If the current window has a local value need to apply it
1414 // again, it was changed when setting the global value.
1415 // If no error was returned above, we don't expect an error
1416 // here, so ignore the return value.
1417 if (opt_lcs)
1418 {
1419 if (*wp->w_p_lcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001420 (void)set_listchars_option(wp, wp->w_p_lcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001421 }
1422 else
1423 {
1424 if (*wp->w_p_fcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001425 (void)set_fillchars_option(wp, wp->w_p_fcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001426 }
1427 }
1428
1429 redraw_all_later(UPD_NOT_VALID);
1430
1431 return NULL;
1432}
1433
1434/*
1435 * The 'fillchars' option or the 'listchars' option is changed.
1436 */
1437 char *
1438did_set_chars_option(optset_T *args)
1439{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001440 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001441 char *errmsg = NULL;
1442
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001443 if ( varp == &p_lcs // global 'listchars'
1444 || varp == &p_fcs) // global 'fillchars'
1445 errmsg = did_set_global_listfillchars(*varp, varp == &p_lcs,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001446 args->os_flags, args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001447 else if (varp == &curwin->w_p_lcs) // local 'listchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001448 errmsg = set_listchars_option(curwin, *varp, TRUE,
1449 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001450 else if (varp == &curwin->w_p_fcs) // local 'fillchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001451 errmsg = set_fillchars_option(curwin, *varp, TRUE,
1452 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001453
1454 return errmsg;
1455}
1456
1457/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001458 * Expand 'fillchars' or 'listchars' option value.
1459 */
1460 int
1461expand_set_chars_option(optexpand_T *args, int *numMatches, char_u ***matches)
1462{
1463 char_u **varp = (char_u **)args->oe_varp;
1464 int is_lcs = (varp == &p_lcs || varp == &curwin->w_p_lcs);
1465 return expand_set_opt_generic(
1466 args,
1467 is_lcs ? get_listchars_name : get_fillchars_name,
1468 numMatches,
1469 matches);
1470}
1471
1472/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001473 * The 'cinoptions' option is changed.
1474 */
1475 char *
1476did_set_cinoptions(optset_T *args UNUSED)
1477{
1478 // TODO: recognize errors
1479 parse_cino(curbuf);
1480
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001481 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001482}
1483
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00001484#if defined(FEAT_SYN_HL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001485/*
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001486 * The 'colorcolumn' option is changed.
1487 */
1488 char *
Millya441a3e2024-10-22 22:43:01 +02001489did_set_colorcolumn(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001490{
Millya441a3e2024-10-22 22:43:01 +02001491 char_u **varp = (char_u **)args->os_varp;
1492
1493 return check_colorcolumn(*varp, varp == &curwin->w_p_cc ? curwin : NULL);
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001494}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001495#endif
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001496
1497/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001498 * The 'comments' option is changed.
1499 */
1500 char *
1501did_set_comments(optset_T *args)
1502{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001503 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001504 char_u *s;
1505 char *errmsg = NULL;
1506
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001507 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001508 {
1509 while (*s && *s != ':')
1510 {
1511 if (vim_strchr((char_u *)COM_ALL, *s) == NULL
1512 && !VIM_ISDIGIT(*s) && *s != '-')
1513 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01001514 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001515 break;
1516 }
1517 ++s;
1518 }
1519 if (*s++ == NUL)
1520 errmsg = e_missing_colon;
1521 else if (*s == ',' || *s == NUL)
1522 errmsg = e_zero_length_string;
1523 if (errmsg != NULL)
1524 break;
1525 while (*s && *s != ',')
1526 {
1527 if (*s == '\\' && s[1] != NUL)
1528 ++s;
1529 ++s;
1530 }
1531 s = skip_to_option_part(s);
1532 }
1533
1534 return errmsg;
1535}
1536
1537#if defined(FEAT_FOLDING) || defined(PROTO)
1538/*
1539 * The 'commentstring' option is changed.
1540 */
1541 char *
1542did_set_commentstring(optset_T *args)
1543{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001544 char_u **varp = (char_u **)args->os_varp;
1545
1546 if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001547 return e_commentstring_must_be_empty_or_contain_str;
1548
1549 return NULL;
1550}
1551#endif
1552
1553/*
1554 * The 'complete' option is changed.
1555 */
1556 char *
1557did_set_complete(optset_T *args)
1558{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001559 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001560 char_u *s;
1561
1562 // check if it is a valid value for 'complete' -- Acevedo
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001563 for (s = *varp; *s;)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001564 {
1565 while (*s == ',' || *s == ' ')
1566 s++;
1567 if (!*s)
1568 break;
1569 if (vim_strchr((char_u *)".wbuksid]tU", *s) == NULL)
Christian Brabandtb39b2402023-11-29 11:34:05 +01001570 return illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001571 if (*++s != NUL && *s != ',' && *s != ' ')
1572 {
1573 if (s[-1] == 'k' || s[-1] == 's')
1574 {
1575 // skip optional filename after 'k' and 's'
1576 while (*s && *s != ',' && *s != ' ')
1577 {
1578 if (*s == '\\' && s[1] != NUL)
1579 ++s;
1580 ++s;
1581 }
1582 }
1583 else
1584 {
1585 if (args->os_errbuf != NULL)
1586 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01001587 vim_snprintf((char *)args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001588 _(e_illegal_character_after_chr), *--s);
1589 return args->os_errbuf;
1590 }
1591 return "";
1592 }
1593 }
1594 }
1595
1596 return NULL;
1597}
1598
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001599 int
1600expand_set_complete(optexpand_T *args, int *numMatches, char_u ***matches)
1601{
1602 static char *(p_cpt_values[]) = {
1603 ".", "w", "b", "u", "k", "kspell", "s", "i", "d", "]", "t", "U",
1604 NULL};
1605 return expand_set_opt_string(
1606 args,
1607 p_cpt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001608 ARRAY_LENGTH(p_cpt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001609 numMatches,
1610 matches);
1611}
1612
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001613/*
1614 * The 'completeopt' option is changed.
1615 */
1616 char *
1617did_set_completeopt(optset_T *args UNUSED)
1618{
zeertzjq529b9ad2024-06-05 20:27:06 +02001619 char_u *cot = p_cot;
1620 unsigned *flags = &cot_flags;
1621
1622 if (args->os_flags & OPT_LOCAL)
1623 {
1624 cot = curbuf->b_p_cot;
1625 flags = &curbuf->b_cot_flags;
1626 }
zeertzjq46dcd842024-11-03 09:10:50 +01001627 else if (!(args->os_flags & OPT_GLOBAL))
1628 // When using :set, clear the local flags.
1629 curbuf->b_cot_flags = 0;
zeertzjq529b9ad2024-06-05 20:27:06 +02001630
1631 if (check_opt_strings(cot, p_cot_values, TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001632 return e_invalid_argument;
1633
zeertzjq529b9ad2024-06-05 20:27:06 +02001634 if (opt_strings_flags(cot, p_cot_values, flags, TRUE) != OK)
1635 return e_invalid_argument;
1636
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001637 return NULL;
1638}
1639
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001640 int
1641expand_set_completeopt(optexpand_T *args, int *numMatches, char_u ***matches)
1642{
1643 return expand_set_opt_string(
1644 args,
1645 p_cot_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001646 ARRAY_LENGTH(p_cot_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001647 numMatches,
1648 matches);
1649}
1650
glepnir6a89c942024-10-01 20:32:12 +02001651/*
glepnirf31cfa22025-03-06 21:59:13 +01001652 * The 'completefuzzycollect' option is changed.
1653 */
1654 char *
1655did_set_completefuzzycollect(optset_T *args UNUSED)
1656{
1657 if (opt_strings_flags(p_cfc, p_cfc_values, &cfc_flags, TRUE) != OK)
1658 return e_invalid_argument;
1659 return NULL;
1660}
1661
zeertzjq53d59ec2025-03-07 19:09:09 +01001662 int
1663expand_set_completefuzzycollect(
1664 optexpand_T *args,
1665 int *numMatches,
1666 char_u ***matches)
1667{
1668 return expand_set_opt_string(
1669 args,
1670 p_cfc_values,
1671 ARRAY_LENGTH(p_cfc_values) - 1,
1672 numMatches,
1673 matches);
1674}
1675
glepnirf31cfa22025-03-06 21:59:13 +01001676/*
glepnir6a89c942024-10-01 20:32:12 +02001677 * The 'completeitemalign' option is changed.
1678 */
1679 char *
1680did_set_completeitemalign(optset_T *args UNUSED)
1681{
1682 char_u *p = p_cia;
1683 unsigned new_cia_flags = 0;
1684 int seen[3] = { FALSE, FALSE, FALSE };
1685 int count = 0;
1686 char_u buf[10];
1687
1688 while (*p)
1689 {
1690 copy_option_part(&p, buf, sizeof(buf), ",");
1691 if (count >= 3)
1692 return e_invalid_argument;
1693
1694 if (STRCMP(buf, "abbr") == 0)
1695 {
1696 if (seen[CPT_ABBR])
1697 return e_invalid_argument;
1698 new_cia_flags = new_cia_flags * 10 + CPT_ABBR;
1699 seen[CPT_ABBR] = TRUE;
1700 count++;
1701 }
1702 else if (STRCMP(buf, "kind") == 0)
1703 {
1704 if (seen[CPT_KIND])
1705 return e_invalid_argument;
1706 new_cia_flags = new_cia_flags * 10 + CPT_KIND;
1707 seen[CPT_KIND] = TRUE;
1708 count++;
1709 }
1710 else if (STRCMP(buf, "menu") == 0)
1711 {
1712 if (seen[CPT_MENU])
1713 return e_invalid_argument;
1714 new_cia_flags = new_cia_flags * 10 + CPT_MENU;
1715 seen[CPT_MENU] = TRUE;
1716 count++;
1717 }
1718 else
1719 return e_invalid_argument;
1720 }
1721 if (new_cia_flags == 0 || count != 3)
1722 return e_invalid_argument;
1723
1724 cia_flags = new_cia_flags;
1725 return NULL;
1726}
1727
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001728#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1729/*
1730 * The 'completepopup' option is changed.
1731 */
1732 char *
1733did_set_completepopup(optset_T *args UNUSED)
1734{
1735 if (parse_completepopup(NULL) == FAIL)
1736 return e_invalid_argument;
1737
1738 popup_close_info();
1739 return NULL;
1740}
1741#endif
1742
1743#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1744/*
1745 * The 'completeslash' option is changed.
1746 */
1747 char *
1748did_set_completeslash(optset_T *args UNUSED)
1749{
1750 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1751 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
1752 return e_invalid_argument;
1753
1754 return NULL;
1755}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001756
1757 int
1758expand_set_completeslash(optexpand_T *args, int *numMatches, char_u ***matches)
1759{
1760 return expand_set_opt_string(
1761 args,
1762 p_csl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001763 ARRAY_LENGTH(p_csl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001764 numMatches,
1765 matches);
1766}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001767#endif
1768
1769#if defined(FEAT_CONCEAL) || defined(PROTO)
1770/*
1771 * The 'concealcursor' option is changed.
1772 */
1773 char *
1774did_set_concealcursor(optset_T *args)
1775{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001776 char_u **varp = (char_u **)args->os_varp;
1777
Christian Brabandtb39b2402023-11-29 11:34:05 +01001778 return did_set_option_listflag(*varp, (char_u *)COCU_ALL, args->os_errbuf,
1779 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001780}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001781
1782 int
1783expand_set_concealcursor(optexpand_T *args, int *numMatches, char_u ***matches)
1784{
1785 return expand_set_opt_listflag(args, (char_u*)COCU_ALL, numMatches, matches);
1786}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001787#endif
1788
1789/*
1790 * The 'cpoptions' option is changed.
1791 */
1792 char *
1793did_set_cpoptions(optset_T *args)
1794{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001795 char_u **varp = (char_u **)args->os_varp;
1796
Christian Brabandtb39b2402023-11-29 11:34:05 +01001797 return did_set_option_listflag(*varp, (char_u *)CPO_ALL, args->os_errbuf,
1798 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001799}
1800
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001801 int
1802expand_set_cpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
1803{
1804 return expand_set_opt_listflag(args, (char_u*)CPO_ALL, numMatches, matches);
1805}
1806
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001807#if defined(FEAT_CRYPT) || defined(PROTO)
1808/*
1809 * The 'cryptkey' option is changed.
1810 */
1811 char *
1812did_set_cryptkey(optset_T *args)
1813{
1814 // Make sure the ":set" command doesn't show the new value in the
1815 // history.
1816 remove_key_from_history();
1817
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02001818 if (args->os_op != OP_NONE)
1819 // Don't allow set+=/-=/^= as they can allow for substring guessing
1820 return e_invalid_argument;
1821
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001822 if (STRCMP(curbuf->b_p_key, args->os_oldval.string) != 0)
1823 {
1824 // Need to update the swapfile.
1825 ml_set_crypt_key(curbuf, args->os_oldval.string,
1826 *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
1827 changed_internal();
1828 }
Christian Brabandt19e6c4f2023-06-27 18:57:10 +01001829# ifdef FEAT_SODIUM
1830 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
1831 crypt_sodium_lock_key(args->os_newval.string);
1832# endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001833
1834 return NULL;
1835}
1836
1837/*
1838 * The 'cryptmethod' option is changed.
1839 */
1840 char *
1841did_set_cryptmethod(optset_T *args)
1842{
1843 char_u *p;
1844 char_u *s;
1845
1846 if (args->os_flags & OPT_LOCAL)
1847 p = curbuf->b_p_cm;
1848 else
1849 p = p_cm;
1850 if (check_opt_strings(p, p_cm_values, TRUE) != OK)
1851 return e_invalid_argument;
1852 else if (crypt_self_test() == FAIL)
1853 return e_invalid_argument;
1854
1855 // When setting the global value to empty, make it "zip".
1856 if (*p_cm == NUL)
1857 {
1858 free_string_option(p_cm);
1859 p_cm = vim_strsave((char_u *)"zip");
1860 }
1861 // When using ":set cm=name" the local value is going to be empty.
1862 // Do that here, otherwise the crypt functions will still use the
1863 // local value.
1864 if ((args->os_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1865 {
1866 free_string_option(curbuf->b_p_cm);
1867 curbuf->b_p_cm = empty_option;
1868 }
1869
1870 // Need to update the swapfile when the effective method changed.
1871 // Set "s" to the effective old value, "p" to the effective new
1872 // method and compare.
1873 if ((args->os_flags & OPT_LOCAL) && *args->os_oldval.string == NUL)
1874 s = p_cm; // was previously using the global value
1875 else
1876 s = args->os_oldval.string;
1877 if (*curbuf->b_p_cm == NUL)
1878 p = p_cm; // is now using the global value
1879 else
1880 p = curbuf->b_p_cm;
1881 if (STRCMP(s, p) != 0)
1882 ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1883
1884 // If the global value changes need to update the swapfile for all
1885 // buffers using that value.
1886 if ((args->os_flags & OPT_GLOBAL)
1887 && STRCMP(p_cm, args->os_oldval.string) != 0)
1888 {
1889 buf_T *buf;
1890
1891 FOR_ALL_BUFFERS(buf)
1892 if (buf != curbuf && *buf->b_p_cm == NUL)
1893 ml_set_crypt_key(buf, buf->b_p_key, args->os_oldval.string);
1894 }
1895 return NULL;
1896}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001897
1898 int
1899expand_set_cryptmethod(optexpand_T *args, int *numMatches, char_u ***matches)
1900{
1901 return expand_set_opt_string(
1902 args,
1903 p_cm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001904 ARRAY_LENGTH(p_cm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001905 numMatches,
1906 matches);
1907}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001908#endif
1909
1910#if (defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1911/*
1912 * The 'cscopequickfix' option is changed.
1913 */
1914 char *
1915did_set_cscopequickfix(optset_T *args UNUSED)
1916{
1917 char_u *p;
1918
1919 if (p_csqf == NULL)
1920 return NULL;
1921
1922 p = p_csqf;
1923 while (*p != NUL)
1924 {
1925 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
1926 || p[1] == NUL
1927 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
1928 || (p[2] != NUL && p[2] != ','))
1929 return e_invalid_argument;
1930 else if (p[2] == NUL)
1931 break;
1932 else
1933 p += 3;
1934 }
1935
1936 return NULL;
1937}
1938#endif
1939
1940#if defined(FEAT_SYN_HL) || defined(PROTO)
1941/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001942 * The 'cursorlineopt' option is changed.
1943 */
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001944 char *
1945did_set_cursorlineopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001946{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001947 char_u **varp = (char_u **)args->os_varp;
1948
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001949 // This could be changed to use opt_strings_flags() instead.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001950 if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001951 return e_invalid_argument;
1952
1953 return NULL;
1954}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001955
1956 int
1957expand_set_cursorlineopt(optexpand_T *args, int *numMatches, char_u ***matches)
1958{
1959 return expand_set_opt_string(
1960 args,
1961 p_culopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001962 ARRAY_LENGTH(p_culopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001963 numMatches,
1964 matches);
1965}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001966#endif
1967
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001968/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001969 * The 'debug' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001970 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001971 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001972did_set_debug(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001973{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001974 return did_set_opt_strings(p_debug, p_debug_values, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001975}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001976
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001977 int
1978expand_set_debug(optexpand_T *args, int *numMatches, char_u ***matches)
1979{
1980 return expand_set_opt_string(
1981 args,
1982 p_debug_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001983 ARRAY_LENGTH(p_debug_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001984 numMatches,
1985 matches);
1986}
1987
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001988#if defined(FEAT_DIFF) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001989/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001990 * The 'diffopt' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001991 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001992 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001993did_set_diffopt(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001994{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001995 if (diffopt_changed() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001996 return e_invalid_argument;
1997
1998 return NULL;
1999}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002000
2001 int
2002expand_set_diffopt(optexpand_T *args, int *numMatches, char_u ***matches)
2003{
2004 expand_T *xp = args->oe_xp;
2005
2006 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2007 {
2008 // Within "algorithm:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002009 int algo_len = (int)STRLEN("algorithm:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002010 if (xp->xp_pattern - args->oe_set_arg >= algo_len &&
2011 STRNCMP(xp->xp_pattern - algo_len, "algorithm:", algo_len) == 0)
2012 {
2013 return expand_set_opt_string(
2014 args,
2015 p_dip_algorithm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002016 ARRAY_LENGTH(p_dip_algorithm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002017 numMatches,
2018 matches);
2019 }
2020 return FAIL;
2021 }
2022
2023 return expand_set_opt_string(
2024 args,
2025 p_dip_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002026 ARRAY_LENGTH(p_dip_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002027 numMatches,
2028 matches);
2029}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002030#endif
2031
2032/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002033 * The 'display' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002034 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002035 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002036did_set_display(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002037{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002038 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002039 return e_invalid_argument;
2040
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002041 (void)init_chartab();
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002042 return NULL;
2043}
2044
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002045 int
2046expand_set_display(optexpand_T *args, int *numMatches, char_u ***matches)
2047{
2048 return expand_set_opt_string(
2049 args,
2050 p_dy_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002051 ARRAY_LENGTH(p_dy_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002052 numMatches,
2053 matches);
2054}
2055
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002056/*
2057 * The 'eadirection' option is changed.
2058 */
2059 char *
2060did_set_eadirection(optset_T *args UNUSED)
2061{
2062 return did_set_opt_strings(p_ead, p_ead_values, FALSE);
2063}
2064
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002065 int
2066expand_set_eadirection(optexpand_T *args, int *numMatches, char_u ***matches)
2067{
2068 return expand_set_opt_string(
2069 args,
2070 p_ead_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002071 ARRAY_LENGTH(p_ead_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002072 numMatches,
2073 matches);
2074}
2075
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002076/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002077 * One of the 'encoding', 'fileencoding', 'termencoding' or 'makeencoding'
2078 * options is changed.
2079 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002080 char *
2081did_set_encoding(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002082{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002083 char_u **varp = (char_u **)args->os_varp;
2084 char_u **gvarp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002085 char *errmsg = NULL;
2086 char_u *p;
2087
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002088 // Get the global option to compare with, otherwise we would have to check
2089 // two values for all local options.
2090 gvarp = (char_u **)get_option_varp_scope(args->os_idx, OPT_GLOBAL);
2091
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002092 if (gvarp == &p_fenc)
2093 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002094 if (!curbuf->b_p_ma && args->os_flags != OPT_GLOBAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002095 errmsg = e_cannot_make_changes_modifiable_is_off;
2096 else if (vim_strchr(*varp, ',') != NULL)
2097 // No comma allowed in 'fileencoding'; catches confusing it
2098 // with 'fileencodings'.
2099 errmsg = e_invalid_argument;
2100 else
2101 {
2102 // May show a "+" in the title now.
2103 redraw_titles();
2104 // Add 'fileencoding' to the swap file.
2105 ml_setflags(curbuf);
2106 }
2107 }
2108 if (errmsg == NULL)
2109 {
2110 // canonize the value, so that STRCMP() can be used on it
2111 p = enc_canonize(*varp);
2112 if (p != NULL)
2113 {
2114 vim_free(*varp);
2115 *varp = p;
2116 }
2117 if (varp == &p_enc)
2118 {
2119 errmsg = mb_init();
2120 redraw_titles();
2121 }
2122 }
2123
2124#if defined(FEAT_GUI_GTK)
2125 if (errmsg == NULL && varp == &p_tenc && gui.in_use)
2126 {
2127 // GTK uses only a single encoding, and that is UTF-8.
2128 if (STRCMP(p_tenc, "utf-8") != 0)
2129 errmsg = e_cannot_be_changed_in_gtk_GUI;
2130 }
2131#endif
2132
2133 if (errmsg == NULL)
2134 {
2135#ifdef FEAT_KEYMAP
2136 // When 'keymap' is used and 'encoding' changes, reload the keymap
2137 // (with another encoding).
2138 if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
2139 (void)keymap_init();
2140#endif
2141
2142 // When 'termencoding' is not empty and 'encoding' changes or when
2143 // 'termencoding' changes, need to setup for keyboard input and
2144 // display output conversion.
2145 if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
2146 {
2147 if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
2148 || convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
2149 {
2150 semsg(_(e_cannot_convert_between_str_and_str),
2151 p_tenc, p_enc);
2152 errmsg = e_invalid_argument;
2153 }
2154 }
2155
2156#if defined(MSWIN)
K.Takatace3189d2023-02-15 19:13:43 +00002157 // $HOME, $VIM and $VIMRUNTIME may have characters in active code page.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002158 if (varp == &p_enc)
K.Takatace3189d2023-02-15 19:13:43 +00002159 {
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002160 init_homedir();
K.Takatace3189d2023-02-15 19:13:43 +00002161 init_vimdir();
2162 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002163#endif
2164 }
2165
2166 return errmsg;
2167}
2168
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002169 int
2170expand_set_encoding(optexpand_T *args, int *numMatches, char_u ***matches)
2171{
2172 return expand_set_opt_generic(
2173 args,
2174 get_encoding_name,
2175 numMatches,
2176 matches);
2177}
2178
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002179/*
Luuk van Baalb7147f82025-02-08 18:52:39 +01002180 * The 'eventignore(win)' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002181 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002182 char *
Luuk van Baalb7147f82025-02-08 18:52:39 +01002183did_set_eventignore(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002184{
Luuk van Baalb7147f82025-02-08 18:52:39 +01002185 char_u **varp = (char_u **)args->os_varp;
2186
2187 if (check_ei(*varp) == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002188 return e_invalid_argument;
2189 return NULL;
2190}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002191
Luuk van Baalb7147f82025-02-08 18:52:39 +01002192static int expand_eiw = FALSE;
2193
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002194 static char_u *
2195get_eventignore_name(expand_T *xp, int idx)
2196{
Luuk van Baalb7147f82025-02-08 18:52:39 +01002197 // 'eventignore(win)' allows special keyword "all" in addition to
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002198 // all event names.
2199 if (idx == 0)
2200 return (char_u *)"all";
Luuk van Baalb7147f82025-02-08 18:52:39 +01002201 return get_event_name_no_group(xp, idx - 1, expand_eiw);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002202}
2203
2204 int
2205expand_set_eventignore(optexpand_T *args, int *numMatches, char_u ***matches)
2206{
Luuk van Baalb7147f82025-02-08 18:52:39 +01002207 expand_eiw = args->oe_varp != (char_u *)&p_ei;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002208 return expand_set_opt_generic(
2209 args,
2210 get_eventignore_name,
2211 numMatches,
2212 matches);
2213}
2214
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002215/*
2216 * The 'fileformat' option is changed.
2217 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002218 char *
2219did_set_fileformat(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002220{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002221 char_u **varp = (char_u **)args->os_varp;
2222
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002223 if (!curbuf->b_p_ma && !(args->os_flags & OPT_GLOBAL))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002224 return e_cannot_make_changes_modifiable_is_off;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002225 else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002226 return e_invalid_argument;
2227
2228 // may also change 'textmode'
2229 if (get_fileformat(curbuf) == EOL_DOS)
2230 curbuf->b_p_tx = TRUE;
2231 else
2232 curbuf->b_p_tx = FALSE;
2233 redraw_titles();
2234 // update flag in swap file
2235 ml_setflags(curbuf);
2236 // Redraw needed when switching to/from "mac": a CR in the text
2237 // will be displayed differently.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002238 if (get_fileformat(curbuf) == EOL_MAC || *args->os_oldval.string == 'm')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002239 redraw_curbuf_later(UPD_NOT_VALID);
2240
2241 return NULL;
2242}
2243
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002244 int
2245expand_set_fileformat(optexpand_T *args, int *numMatches, char_u ***matches)
2246{
2247 return expand_set_opt_string(
2248 args,
2249 p_ff_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002250 ARRAY_LENGTH(p_ff_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002251 numMatches,
2252 matches);
2253}
2254
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002255/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002256 * Function given to ExpandGeneric() to obtain the possible arguments of the
2257 * fileformat options.
2258 */
2259 char_u *
2260get_fileformat_name(expand_T *xp UNUSED, int idx)
2261{
2262 if (idx >= (int)ARRAY_LENGTH(p_ff_values))
2263 return NULL;
2264
2265 return (char_u*)p_ff_values[idx];
2266}
2267
2268/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002269 * The 'fileformats' option is changed.
2270 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002271 char *
2272did_set_fileformats(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002273{
2274 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
2275 return e_invalid_argument;
2276
2277 // also change 'textauto'
2278 if (*p_ffs == NUL)
2279 p_ta = FALSE;
2280 else
2281 p_ta = TRUE;
2282
2283 return NULL;
2284}
2285
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002286/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002287 * The 'filetype' or the 'syntax' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002288 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002289 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002290did_set_filetype_or_syntax(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002291{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002292 char_u **varp = (char_u **)args->os_varp;
2293
2294 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002295 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002296
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002297 args->os_value_changed = STRCMP(args->os_oldval.string, *varp) != 0;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002298
2299 // Since we check the value, there is no need to set P_INSECURE,
2300 // even when the value comes from a modeline.
2301 args->os_value_checked = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002302
2303 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002304}
2305
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002306#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002307/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002308 * The 'foldclose' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002309 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002310 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002311did_set_foldclose(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002312{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002313 return did_set_opt_strings(p_fcl, p_fcl_values, TRUE);
2314}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002315
2316 int
2317expand_set_foldclose(optexpand_T *args, int *numMatches, char_u ***matches)
2318{
2319 return expand_set_opt_string(
2320 args,
2321 p_fcl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002322 ARRAY_LENGTH(p_fcl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002323 numMatches,
2324 matches);
2325}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002326#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002327
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002328#if (defined(FEAT_EVAL) && defined(FEAT_FOLDING)) || defined(PROTO)
2329/*
2330 * The 'foldexpr' option is changed.
2331 */
2332 char *
2333did_set_foldexpr(optset_T *args)
2334{
2335 (void)did_set_optexpr(args);
2336 if (foldmethodIsExpr(curwin))
2337 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002338 return NULL;
2339}
2340#endif
2341
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002342#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002343/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002344 * The 'foldignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002345 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002346 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002347did_set_foldignore(optset_T *args UNUSED)
2348{
2349 if (foldmethodIsIndent(curwin))
2350 foldUpdateAll(curwin);
2351 return NULL;
2352}
2353
2354/*
2355 * The 'foldmarker' option is changed.
2356 */
2357 char *
2358did_set_foldmarker(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002359{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002360 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002361 char_u *p;
2362
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002363 p = vim_strchr(*varp, ',');
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002364 if (p == NULL)
2365 return e_comma_required;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002366 else if (p == *varp || p[1] == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002367 return e_invalid_argument;
2368 else if (foldmethodIsMarker(curwin))
2369 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002370
2371 return NULL;
2372}
2373
2374/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002375 * The 'foldmethod' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002376 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002377 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002378did_set_foldmethod(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002379{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002380 char_u **varp = (char_u **)args->os_varp;
2381
Milly142cad12024-10-22 22:11:51 +02002382 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK || **varp == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002383 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002384
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002385 foldUpdateAll(curwin);
2386 if (foldmethodIsDiff(curwin))
2387 newFoldLevel();
2388 return NULL;
2389}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002390
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002391 int
2392expand_set_foldmethod(optexpand_T *args, int *numMatches, char_u ***matches)
2393{
2394 return expand_set_opt_string(
2395 args,
2396 p_fdm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002397 ARRAY_LENGTH(p_fdm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002398 numMatches,
2399 matches);
2400}
2401
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002402/*
2403 * The 'foldopen' option is changed.
2404 */
2405 char *
2406did_set_foldopen(optset_T *args UNUSED)
2407{
2408 return did_set_opt_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
2409}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002410
2411 int
2412expand_set_foldopen(optexpand_T *args, int *numMatches, char_u ***matches)
2413{
2414 return expand_set_opt_string(
2415 args,
2416 p_fdo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002417 ARRAY_LENGTH(p_fdo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002418 numMatches,
2419 matches);
2420}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002421#endif
2422
2423/*
2424 * The 'formatoptions' option is changed.
2425 */
2426 char *
2427did_set_formatoptions(optset_T *args)
2428{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002429 char_u **varp = (char_u **)args->os_varp;
2430
Christian Brabandtb39b2402023-11-29 11:34:05 +01002431 return did_set_option_listflag(*varp, (char_u *)FO_ALL, args->os_errbuf,
2432 args->os_errbuflen);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002433}
2434
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002435 int
2436expand_set_formatoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2437{
2438 return expand_set_opt_listflag(args, (char_u*)FO_ALL, numMatches, matches);
2439}
2440
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002441#if defined(CURSOR_SHAPE) || defined(PROTO)
2442/*
2443 * The 'guicursor' option is changed.
2444 */
2445 char *
2446did_set_guicursor(optset_T *args UNUSED)
2447{
2448 return parse_shape_opt(SHAPE_CURSOR);
2449}
2450#endif
2451
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002452#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002453/*
2454 * The 'guifont' option is changed.
2455 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002456 char *
2457did_set_guifont(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002458{
2459 char_u *p;
2460 char *errmsg = NULL;
2461
2462 if (gui.in_use)
2463 {
2464 p = p_guifont;
2465# if defined(FEAT_GUI_GTK)
2466 // Put up a font dialog and let the user select a new value.
2467 // If this is cancelled go back to the old value but don't
2468 // give an error message.
2469 if (STRCMP(p, "*") == 0)
2470 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002471 p = gui_mch_font_dialog(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002472 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002473 p_guifont = (p != NULL) ? p : vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002474 }
2475# endif
2476 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
2477 {
2478# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
2479 if (STRCMP(p_guifont, "*") == 0)
2480 {
2481 // Dialog was cancelled: Keep the old value without giving
2482 // an error message.
2483 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002484 p_guifont = vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002485 }
2486 else
2487# endif
2488 errmsg = e_invalid_fonts;
2489 }
2490 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002491
2492 return errmsg;
2493}
2494
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002495/*
2496 * Expand the 'guifont' option. Only when GUI is being used. Each platform has
2497 * specific behaviors.
2498 */
2499 int
2500expand_set_guifont(optexpand_T *args, int *numMatches, char_u ***matches)
2501{
2502 if (!gui.in_use)
2503 return FAIL;
2504
2505# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
2506 char_u **varp = (char_u **)args->oe_varp;
2507 int wide = (varp == &p_guifontwide);
2508
2509 return expand_set_opt_callback(
2510 args, gui_mch_expand_font, &wide, numMatches, matches);
2511# else
2512 return FAIL;
2513# endif
2514}
2515
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002516# if defined(FEAT_XFONTSET) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002517/*
2518 * The 'guifontset' option is changed.
2519 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002520 char *
2521did_set_guifontset(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002522{
2523 char *errmsg = NULL;
2524
2525 if (STRCMP(p_guifontset, "*") == 0)
2526 errmsg = e_cant_select_fontset;
2527 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
2528 errmsg = e_invalid_fontset;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002529
2530 return errmsg;
2531}
2532# endif
2533
2534/*
2535 * The 'guifontwide' option is changed.
2536 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002537 char *
2538did_set_guifontwide(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002539{
2540 char *errmsg = NULL;
2541
2542 if (STRCMP(p_guifontwide, "*") == 0)
2543 errmsg = e_cant_select_wide_font;
2544 else if (gui_get_wide_font() == FAIL)
2545 errmsg = e_invalid_wide_font;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002546
2547 return errmsg;
2548}
2549#endif
2550
Erik S. V. Jansson8b1e7492024-02-24 14:26:52 +01002551#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002552/*
2553 * The 'guiligatures' option is changed.
2554 */
2555 char *
2556did_set_guiligatures(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002557{
2558 gui_set_ligatures();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002559 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002560}
2561#endif
2562
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002563#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002564/*
2565 * The 'guioptions' option is changed.
2566 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002567 char *
2568did_set_guioptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002569{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002570 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002571 char *errmsg;
2572
Christian Brabandtb39b2402023-11-29 11:34:05 +01002573 errmsg = did_set_option_listflag(*varp, (char_u *)GO_ALL, args->os_errbuf,
2574 args->os_errbuflen);
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002575 if (errmsg != NULL)
2576 return errmsg;
2577
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002578 gui_init_which_components(args->os_oldval.string);
2579 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002580}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002581
2582 int
2583expand_set_guioptions(optexpand_T *args, int *numMatches, char_u ***matches)
2584{
2585 return expand_set_opt_listflag(args, (char_u*)GO_ALL, numMatches, matches);
2586}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002587#endif
2588
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002589#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002590/*
2591 * The 'guitablabel' option is changed.
2592 */
2593 char *
2594did_set_guitablabel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002595{
2596 redraw_tabline = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002597 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002598}
2599#endif
2600
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002601/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002602 * The 'helpfile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002603 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002604 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002605did_set_helpfile(optset_T *args UNUSED)
2606{
2607 // May compute new values for $VIM and $VIMRUNTIME
2608 if (didset_vim)
2609 vim_unsetenv_ext((char_u *)"VIM");
2610 if (didset_vimruntime)
2611 vim_unsetenv_ext((char_u *)"VIMRUNTIME");
2612 return NULL;
2613}
2614
2615#if defined(FEAT_MULTI_LANG) || defined(PROTO)
2616/*
2617 * The 'helplang' option is changed.
2618 */
2619 char *
2620did_set_helplang(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002621{
2622 char *errmsg = NULL;
2623
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002624 // Check for "", "ab", "ab,cd", etc.
2625 for (char_u *s = p_hlg; *s != NUL; s += 3)
2626 {
2627 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
2628 {
2629 errmsg = e_invalid_argument;
2630 break;
2631 }
2632 if (s[2] == NUL)
2633 break;
2634 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002635
2636 return errmsg;
2637}
2638#endif
2639
2640/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002641 * The 'highlight' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002642 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002643 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002644did_set_highlight(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002645{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002646 if (highlight_changed() == FAIL)
2647 return e_invalid_argument; // invalid flags
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002648
2649 return NULL;
2650}
2651
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002652/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002653 * Expand 'highlight' option.
2654 */
2655 int
2656expand_set_highlight(optexpand_T *args, int *numMatches, char_u ***matches)
2657{
2658 char_u *p;
2659 expand_T *xp = args->oe_xp;
2660 static char_u hl_flags[HLF_COUNT] = HL_FLAGS;
Christian Brabandt3f168ec2023-10-02 23:21:11 +02002661 size_t i;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002662 int count = 0;
2663
2664 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2665 {
2666 // Right after a ':', meaning we just return all highlight names.
2667 return expand_set_opt_generic(
2668 args,
2669 get_highlight_name,
2670 numMatches,
2671 matches);
2672 }
2673
2674 if (*xp->xp_pattern == NUL)
2675 {
2676 // At beginning of a comma-separated list. Return the specific list of
2677 // supported occasions.
2678 *matches = ALLOC_MULT(char_u *, HLF_COUNT + 1);
2679 if (*matches == NULL)
2680 return FAIL;
2681
2682 // We still want to return the full option if it's requested.
2683 if (args->oe_include_orig_val)
2684 {
2685 p = vim_strsave(args->oe_opt_value);
2686 if (p == NULL)
2687 {
2688 VIM_CLEAR(*matches);
2689 return FAIL;
2690 }
2691 (*matches)[count++] = p;
2692 }
2693
2694 for (i = 0; i < HLF_COUNT; i++)
2695 {
2696 p = vim_strnsave(&hl_flags[i], 1);
2697 if (p == NULL)
2698 {
2699 if (count == 0)
2700 {
2701 VIM_CLEAR(*matches);
2702 return FAIL;
2703 }
2704 else
2705 break;
2706 }
2707 (*matches)[count++] = p;
2708 }
2709
2710 if (count == 0)
2711 {
2712 VIM_CLEAR(*matches);
2713 return FAIL;
2714 }
2715 *numMatches = count;
2716 return OK;
2717 }
2718
2719 // We are after the initial character (which indicates the occasion). We
2720 // already made sure we are not matching after a ':' above, so now we want
2721 // to match against display mode modifiers.
2722 // Since the xp_pattern starts from the beginning, we need to include it in
2723 // the returned match.
2724
2725 // Note: Keep this in sync with highlight_changed()
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002726 static char_u p_hl_mode_values[] =
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002727 {':', 'b', 'i', '-', 'n', 'r', 's', 'u', 'c', '2', 'd', '=', 't'};
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002728 size_t num_hl_modes = ARRAY_LENGTH(p_hl_mode_values);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002729
2730 *matches = ALLOC_MULT(char_u *, num_hl_modes);
2731 if (*matches == NULL)
2732 return FAIL;
2733
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002734 int pattern_len = xp->xp_pattern_len;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002735
2736 for (i = 0; i < num_hl_modes; i++)
2737 {
2738 // Don't allow duplicates as these are unique flags
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002739 char_u *dup = vim_strchr(xp->xp_pattern + 1, p_hl_mode_values[i]);
2740 if (dup != NULL && (int)(dup - xp->xp_pattern) < pattern_len)
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002741 continue;
2742
2743 // ':' only works by itself, not with other flags.
2744 if (pattern_len > 1 && p_hl_mode_values[i] == ':')
2745 continue;
2746
2747 p = vim_strnsave(xp->xp_pattern, pattern_len + 1);
2748 if (p == NULL)
2749 {
2750 if (i == 0)
2751 {
2752 VIM_CLEAR(*matches);
2753 return FAIL;
2754 }
2755 else
2756 break;
2757 }
2758 p[pattern_len] = p_hl_mode_values[i];
2759 p[pattern_len + 1] = NUL;
2760 (*matches)[count++] = p;
2761 }
2762 if (count == 0)
2763 {
2764 VIM_CLEAR(*matches);
2765 return FAIL;
2766 }
2767 *numMatches = count;
2768
2769 return OK;
2770}
2771
2772/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002773 * The 'titlestring' or the 'iconstring' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002774 */
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002775 static char *
2776parse_titleiconstring(optset_T *args UNUSED, int flagval UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002777{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002778#ifdef FEAT_STL_OPT
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002779 char_u **varp = (char_u **)args->os_varp;
2780
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002781 // NULL => statusline syntax
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002782 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002783 stl_syntax |= flagval;
2784 else
2785 stl_syntax &= ~flagval;
2786#endif
2787 did_set_title();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002788
2789 return NULL;
2790}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002791
2792/*
2793 * The 'iconstring' option is changed.
2794 */
2795 char *
2796did_set_iconstring(optset_T *args)
2797{
2798 int flagval = 0;
2799
2800#ifdef FEAT_STL_OPT
2801 flagval = STL_IN_ICON;
2802#endif
2803
2804 return parse_titleiconstring(args, flagval);
2805}
2806
2807#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
2808/*
2809 * The 'imactivatekey' option is changed.
2810 */
2811 char *
2812did_set_imactivatekey(optset_T *args UNUSED)
2813{
2814 if (!im_xim_isvalid_imactivate())
2815 return e_invalid_argument;
2816 return NULL;
2817}
2818#endif
2819
2820/*
Milly5e7a6a42024-10-22 22:27:19 +02002821 * The 'iskeyword' option is changed.
2822 */
2823 char *
2824did_set_iskeyword(optset_T *args)
2825{
2826 char_u **varp = (char_u **)args->os_varp;
2827
2828 if (varp == &p_isk) // only check for global-value
2829 {
2830 if (check_isopt(*varp) == FAIL)
2831 return e_invalid_argument;
2832 }
2833 else // fallthrough for local-value
2834 return did_set_isopt(args);
2835
2836 return NULL;
2837}
2838
2839/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002840 * The 'isident' or the 'iskeyword' or the 'isprint' or the 'isfname' option is
2841 * changed.
2842 */
2843 char *
2844did_set_isopt(optset_T *args)
2845{
Milly5e7a6a42024-10-22 22:27:19 +02002846 // 'isident', 'iskeyword', 'isprint' or 'isfname' option: refill g_chartab[]
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002847 // If the new option is invalid, use old value.
2848 // 'lisp' option: refill g_chartab[] for '-' char.
2849 if (init_chartab() == FAIL)
2850 {
2851 args->os_restore_chartab = TRUE;// need to restore the chartab.
2852 return e_invalid_argument; // error in value
2853 }
2854
2855 return NULL;
2856}
2857
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002858/*
2859 * The 'jumpoptions' option is changed.
2860 */
2861 char *
Yegappan Lakshmanan1926ae42023-09-21 16:36:28 +02002862did_set_jumpoptions(optset_T *args UNUSED)
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002863{
2864 if (opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE) != OK)
2865 return e_invalid_argument;
2866
2867 return NULL;
2868}
2869
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002870 int
2871expand_set_jumpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2872{
2873 return expand_set_opt_string(
2874 args,
2875 p_jop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002876 ARRAY_LENGTH(p_jop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002877 numMatches,
2878 matches);
2879}
2880
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002881#if defined(FEAT_KEYMAP) || defined(PROTO)
2882/*
2883 * The 'keymap' option is changed.
2884 */
2885 char *
2886did_set_keymap(optset_T *args)
2887{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002888 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002889 char *errmsg = NULL;
2890
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002891 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002892 errmsg = e_invalid_argument;
2893 else
2894 {
2895 int secure_save = secure;
2896
2897 // Reset the secure flag, since the value of 'keymap' has
2898 // been checked to be safe.
2899 secure = 0;
2900
2901 // load or unload key mapping tables
2902 errmsg = keymap_init();
2903
2904 secure = secure_save;
2905
2906 // Since we check the value, there is no need to set P_INSECURE,
2907 // even when the value comes from a modeline.
2908 args->os_value_checked = TRUE;
2909 }
2910
2911 if (errmsg == NULL)
2912 {
2913 if (*curbuf->b_p_keymap != NUL)
2914 {
2915 // Installed a new keymap, switch on using it.
2916 curbuf->b_p_iminsert = B_IMODE_LMAP;
2917 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
2918 curbuf->b_p_imsearch = B_IMODE_LMAP;
2919 }
2920 else
2921 {
2922 // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
2923 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
2924 curbuf->b_p_iminsert = B_IMODE_NONE;
2925 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
2926 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
2927 }
2928 if ((args->os_flags & OPT_LOCAL) == 0)
2929 {
2930 set_iminsert_global();
2931 set_imsearch_global();
2932 }
2933 status_redraw_curbuf();
2934 }
2935
2936 return errmsg;
2937}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002938#endif
2939
2940/*
2941 * The 'keymodel' option is changed.
2942 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002943 char *
2944did_set_keymodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002945{
2946 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
2947 return e_invalid_argument;
2948
2949 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
2950 km_startsel = (vim_strchr(p_km, 'a') != NULL);
2951 return NULL;
2952}
2953
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002954 int
2955expand_set_keymodel(optexpand_T *args, int *numMatches, char_u ***matches)
2956{
2957 return expand_set_opt_string(
2958 args,
2959 p_km_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002960 ARRAY_LENGTH(p_km_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002961 numMatches,
2962 matches);
2963}
2964
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002965/*
2966 * The 'keyprotocol' option is changed.
2967 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002968 char *
2969did_set_keyprotocol(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002970{
Gregory Anders3695d0e2023-09-29 20:17:20 +02002971 char_u *term = T_NAME;
2972 keyprot_T kpc = match_keyprotocol(term);
2973 if (kpc == KEYPROTOCOL_FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002974 return e_invalid_argument;
2975
Gregory Anders3695d0e2023-09-29 20:17:20 +02002976 apply_keyprotocol(term, kpc);
2977
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002978 return NULL;
2979}
2980
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002981 int
2982expand_set_keyprotocol(optexpand_T *args, int *numMatches, char_u ***matches)
2983{
2984 expand_T *xp = args->oe_xp;
2985 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2986 {
2987 // 'keyprotocol' only has well-defined terms for completion for the
2988 // protocol part after the colon.
2989 return expand_set_opt_string(
2990 args,
2991 p_kpc_protocol_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002992 ARRAY_LENGTH(p_kpc_protocol_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002993 numMatches,
2994 matches);
2995 }
2996 // Use expand_set_opt_string instead of returning FAIL so that we can
2997 // include the original value if args->oe_include_orig_val is set.
2998 static char *(empty[]) = {NULL};
2999 return expand_set_opt_string(args, empty, 0, numMatches, matches);
3000}
3001
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003002/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003003 * The 'lispoptions' option is changed.
3004 */
3005 char *
3006did_set_lispoptions(optset_T *args)
3007{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003008 char_u **varp = (char_u **)args->os_varp;
3009
3010 if (**varp != NUL
3011 && STRCMP(*varp, "expr:0") != 0
3012 && STRCMP(*varp, "expr:1") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003013 return e_invalid_argument;
3014
3015 return NULL;
3016}
3017
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003018 int
3019expand_set_lispoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3020{
3021 static char *(p_lop_values[]) = {"expr:0", "expr:1", NULL};
3022 return expand_set_opt_string(
3023 args,
3024 p_lop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003025 ARRAY_LENGTH(p_lop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003026 numMatches,
3027 matches);
3028}
3029
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003030/*
3031 * The 'matchpairs' option is changed.
3032 */
3033 char *
3034did_set_matchpairs(optset_T *args)
3035{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003036 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003037 char_u *p;
3038
3039 if (has_mbyte)
3040 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003041 for (p = *varp; *p != NUL; ++p)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003042 {
3043 int x2 = -1;
3044 int x3 = -1;
3045
3046 p += mb_ptr2len(p);
3047 if (*p != NUL)
3048 x2 = *p++;
3049 if (*p != NUL)
3050 {
3051 x3 = mb_ptr2char(p);
3052 p += mb_ptr2len(p);
3053 }
3054 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
3055 return e_invalid_argument;
3056 if (*p == NUL)
3057 break;
3058 }
3059 }
3060 else
3061 {
3062 // Check for "x:y,x:y"
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003063 for (p = *varp; *p != NUL; p += 4)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003064 {
3065 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
3066 return e_invalid_argument;
3067 if (p[3] == NUL)
3068 break;
3069 }
3070 }
3071
3072 return NULL;
3073}
3074
Christian Brabandt51d4d842024-12-06 17:26:25 +01003075/*
3076 * Process the updated 'messagesopt' option value.
3077 */
3078 char *
3079did_set_messagesopt(optset_T *args UNUSED)
3080{
3081 if (messagesopt_changed() == FAIL)
3082 return e_invalid_argument;
3083
3084 return NULL;
3085}
3086
3087 int
3088expand_set_messagesopt(optexpand_T *args, int *numMatches, char_u ***matches)
3089{
zeertzjq8cc43da2024-12-07 16:09:08 +01003090 static char *(p_mopt_values[]) = {"hit-enter", "wait:", "history:", NULL};
Christian Brabandt51d4d842024-12-06 17:26:25 +01003091 return expand_set_opt_string(
3092 args,
zeertzjq8cc43da2024-12-07 16:09:08 +01003093 p_mopt_values,
3094 ARRAY_LENGTH(p_mopt_values) - 1,
Christian Brabandt51d4d842024-12-06 17:26:25 +01003095 numMatches,
3096 matches);
3097}
3098
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003099#if defined(FEAT_SPELL) || defined(PROTO)
3100/*
3101 * The 'mkspellmem' option is changed.
3102 */
3103 char *
3104did_set_mkspellmem(optset_T *args UNUSED)
3105{
3106 if (spell_check_msm() != OK)
3107 return e_invalid_argument;
3108
3109 return NULL;
3110}
3111#endif
3112
3113/*
3114 * The 'mouse' option is changed.
3115 */
3116 char *
3117did_set_mouse(optset_T *args)
3118{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003119 char_u **varp = (char_u **)args->os_varp;
3120
Christian Brabandtb39b2402023-11-29 11:34:05 +01003121 return did_set_option_listflag(*varp, (char_u *)MOUSE_ALL, args->os_errbuf,
3122 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003123}
3124
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003125 int
3126expand_set_mouse(optexpand_T *args, int *numMatches, char_u ***matches)
3127{
3128 return expand_set_opt_listflag(args, (char_u*)MOUSE_ALL, numMatches, matches);
3129}
3130
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003131/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003132 * The 'mousemodel' option is changed.
3133 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003134 char *
3135did_set_mousemodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003136{
3137 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
3138 return e_invalid_argument;
3139#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
Ken Takata18d0d292023-12-19 20:12:29 +01003140 else if (*p_mousem != *args->os_oldval.string)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003141 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
3142 // to create or delete the popup menus.
3143 gui_motif_update_mousemodel(root_menu);
3144#endif
3145
3146 return NULL;
3147}
3148
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003149 int
3150expand_set_mousemodel(optexpand_T *args, int *numMatches, char_u ***matches)
3151{
3152 return expand_set_opt_string(
3153 args,
3154 p_mousem_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003155 ARRAY_LENGTH(p_mousem_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003156 numMatches,
3157 matches);
3158}
3159
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003160#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3161 char *
3162did_set_mouseshape(optset_T *args UNUSED)
3163{
3164 char *errmsg = NULL;
3165
3166 errmsg = parse_shape_opt(SHAPE_MOUSE);
3167 update_mouseshape(-1);
3168
3169 return errmsg;
3170}
3171#endif
3172
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003173/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003174 * The 'nrformats' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003175 */
3176 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003177did_set_nrformats(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003178{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003179 char_u **varp = (char_u **)args->os_varp;
3180
3181 return did_set_opt_strings(*varp, p_nf_values, TRUE);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003182}
3183
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003184 int
3185expand_set_nrformats(optexpand_T *args, int *numMatches, char_u ***matches)
3186{
3187 return expand_set_opt_string(
3188 args,
3189 p_nf_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003190 ARRAY_LENGTH(p_nf_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003191 numMatches,
3192 matches);
3193}
3194
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003195#if defined(FEAT_EVAL) || defined(PROTO)
3196/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003197 * One of the '*expr' options is changed: 'balloonexpr', 'diffexpr',
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003198 * 'foldexpr', 'foldtext', 'formatexpr', 'includeexpr', 'indentexpr',
3199 * 'patchexpr', 'printexpr' and 'charconvert'.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003200 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003201 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003202did_set_optexpr(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003203{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003204 char_u **varp = (char_u **)args->os_varp;
3205
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003206 // If the option value starts with <SID> or s:, then replace that with
3207 // the script identifier.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003208 char_u *name = get_scriptlocal_funcname(*varp);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003209 if (name != NULL)
3210 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003211 free_string_option(*varp);
3212 *varp = name;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003213 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003214
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003215 return NULL;
3216}
3217#endif
3218
3219/*
3220 * The 'pastetoggle' option is changed.
3221 */
3222 char *
3223did_set_pastetoggle(optset_T *args UNUSED)
3224{
3225 char_u *p;
3226
3227 // translate key codes like in a mapping
3228 if (*p_pt)
3229 {
zeertzjq7e0bae02023-08-11 23:15:38 +02003230 (void)replace_termcodes(p_pt, &p, 0,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003231 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
3232 if (p != NULL)
3233 {
3234 free_string_option(p_pt);
3235 p_pt = p;
3236 }
3237 }
3238
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003239 return NULL;
3240}
3241
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003242#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3243/*
3244 * The 'previewpopup' option is changed.
3245 */
3246 char *
3247did_set_previewpopup(optset_T *args UNUSED)
3248{
3249 if (parse_previewpopup(NULL) == FAIL)
3250 return e_invalid_argument;
3251
3252 return NULL;
3253}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003254
3255 int
3256expand_set_popupoption(optexpand_T *args, int *numMatches, char_u ***matches)
3257{
3258 expand_T *xp = args->oe_xp;
3259
3260 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3261 {
3262 // Within "highlight:"/"border:"/"align:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003263 int border_len = (int)STRLEN("border:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003264 if (xp->xp_pattern - args->oe_set_arg >= border_len &&
3265 STRNCMP(xp->xp_pattern - border_len, "border:", border_len) == 0)
3266 {
3267 return expand_set_opt_string(
3268 args,
3269 p_popup_option_border_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003270 ARRAY_LENGTH(p_popup_option_border_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003271 numMatches,
3272 matches);
3273 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003274 int align_len = (int)STRLEN("align:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003275 if (xp->xp_pattern - args->oe_set_arg >= align_len &&
3276 STRNCMP(xp->xp_pattern - align_len, "align:", align_len) == 0)
3277 {
3278 return expand_set_opt_string(
3279 args,
3280 p_popup_option_align_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003281 ARRAY_LENGTH(p_popup_option_align_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003282 numMatches,
3283 matches);
3284 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003285 int highlight_len = (int)STRLEN("highlight:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003286 if (xp->xp_pattern - args->oe_set_arg >= highlight_len &&
3287 STRNCMP(xp->xp_pattern - highlight_len, "highlight:", highlight_len) == 0)
3288 {
3289 // Return the list of all highlight names
3290 return expand_set_opt_generic(
3291 args,
3292 get_highlight_name,
3293 numMatches,
3294 matches);
3295 }
3296 return FAIL;
3297 }
3298
3299 return expand_set_opt_string(
3300 args,
3301 p_popup_option_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003302 ARRAY_LENGTH(p_popup_option_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003303 numMatches,
3304 matches);
3305}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003306#endif
3307
3308#if defined(FEAT_POSTSCRIPT) || defined(PROTO)
3309/*
3310 * The 'printencoding' option is changed.
3311 */
3312 char *
3313did_set_printencoding(optset_T *args UNUSED)
3314{
3315 char_u *s, *p;
3316
3317 // Canonize 'printencoding' if VIM standard one
3318 p = enc_canonize(p_penc);
3319 if (p != NULL)
3320 {
3321 vim_free(p_penc);
3322 p_penc = p;
3323 }
3324 else
3325 {
3326 // Ensure lower case and '-' for '_'
3327 for (s = p_penc; *s != NUL; s++)
3328 {
3329 if (*s == '_')
3330 *s = '-';
3331 else
3332 *s = TOLOWER_ASC(*s);
3333 }
3334 }
3335
3336 return NULL;
3337}
3338#endif
3339
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003340#if defined(FEAT_PRINTER) || defined(PROTO)
3341
3342 static char_u *
3343get_printoptions_names(expand_T *xp UNUSED, int idx)
3344{
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003345 if (idx >= (int)ARRAY_LENGTH(printer_opts))
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003346 return NULL;
3347 return (char_u*)printer_opts[idx].name;
3348}
3349
3350/*
3351 * Expand 'printoptions' option
3352 */
3353 int
3354expand_set_printoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3355{
3356 return expand_set_opt_generic(
3357 args,
3358 get_printoptions_names,
3359 numMatches,
3360 matches);
3361}
3362#endif
3363
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003364#if defined(FEAT_STL_OPT) || defined(PROTO)
3365/*
3366 * The 'statusline' or the 'tabline' or the 'rulerformat' option is changed.
3367 * "rulerformat" is TRUE if the 'rulerformat' option is changed.
3368 */
3369 static char *
3370parse_statustabline_rulerformat(optset_T *args, int rulerformat)
3371{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003372 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003373 char_u *s;
3374 char *errmsg = NULL;
3375 int wid;
3376
3377 if (rulerformat) // reset ru_wid first
3378 ru_wid = 0;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003379 s = *varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003380 if (rulerformat && *s == '%')
3381 {
3382 // set ru_wid if 'ruf' starts with "%99("
3383 if (*++s == '-') // ignore a '-'
3384 s++;
3385 wid = getdigits(&s);
3386 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
3387 ru_wid = wid;
3388 else
Yegappan Lakshmananac023e82024-11-27 20:55:45 +01003389 {
3390 // Validate the flags in 'rulerformat' only if it doesn't point to
3391 // a custom function ("%!" flag).
3392 if ((*varp)[1] != '!')
3393 errmsg = check_stl_option(p_ruf);
3394 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003395 }
3396 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
3397 else if (rulerformat || s[0] != '%' || s[1] != '!')
3398 errmsg = check_stl_option(s);
3399 if (rulerformat && errmsg == NULL)
3400 comp_col();
3401
3402 return errmsg;
3403}
3404#endif
3405
3406#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
3407/*
3408 * The 'renderoptions' option is changed.
3409 */
3410 char *
3411did_set_renderoptions(optset_T *args UNUSED)
3412{
3413 if (!gui_mch_set_rendering_options(p_rop))
3414 return e_invalid_argument;
3415
3416 return NULL;
3417}
3418#endif
3419
3420#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3421/*
3422 * The 'rightleftcmd' option is changed.
3423 */
3424 char *
3425did_set_rightleftcmd(optset_T *args)
3426{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003427 char_u **varp = (char_u **)args->os_varp;
3428
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003429 // Currently only "search" is a supported value.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003430 if (**varp != NUL && STRCMP(*varp, "search") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003431 return e_invalid_argument;
3432
3433 return NULL;
3434}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003435
3436 int
3437expand_set_rightleftcmd(optexpand_T *args, int *numMatches, char_u ***matches)
3438{
3439 static char *(p_rlc_values[]) = {"search", NULL};
3440 return expand_set_opt_string(
3441 args,
3442 p_rlc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003443 ARRAY_LENGTH(p_rlc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003444 numMatches,
3445 matches);
3446}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003447#endif
3448
3449#if defined(FEAT_STL_OPT) || defined(PROTO)
3450/*
3451 * The 'rulerformat' option is changed.
3452 */
3453 char *
3454did_set_rulerformat(optset_T *args)
3455{
3456 return parse_statustabline_rulerformat(args, TRUE);
3457}
3458#endif
3459
3460/*
3461 * The 'scrollopt' option is changed.
3462 */
3463 char *
3464did_set_scrollopt(optset_T *args UNUSED)
3465{
3466 return did_set_opt_strings(p_sbo, p_scbopt_values, TRUE);
3467}
3468
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003469 int
3470expand_set_scrollopt(optexpand_T *args, int *numMatches, char_u ***matches)
3471{
3472 return expand_set_opt_string(
3473 args,
3474 p_scbopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003475 ARRAY_LENGTH(p_scbopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003476 numMatches,
3477 matches);
3478}
3479
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003480/*
3481 * The 'selection' option is changed.
3482 */
3483 char *
3484did_set_selection(optset_T *args UNUSED)
3485{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003486 if (*p_sel == NUL || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003487 return e_invalid_argument;
3488
3489 return NULL;
3490}
3491
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003492 int
3493expand_set_selection(optexpand_T *args, int *numMatches, char_u ***matches)
3494{
3495 return expand_set_opt_string(
3496 args,
3497 p_sel_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003498 ARRAY_LENGTH(p_sel_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003499 numMatches,
3500 matches);
3501}
3502
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003503/*
3504 * The 'selectmode' option is changed.
3505 */
3506 char *
3507did_set_selectmode(optset_T *args UNUSED)
3508{
3509 return did_set_opt_strings(p_slm, p_slm_values, TRUE);
3510}
3511
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003512 int
3513expand_set_selectmode(optexpand_T *args, int *numMatches, char_u ***matches)
3514{
3515 return expand_set_opt_string(
3516 args,
3517 p_slm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003518 ARRAY_LENGTH(p_slm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003519 numMatches,
3520 matches);
3521}
3522
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003523#if defined(FEAT_SESSION) || defined(PROTO)
3524/*
3525 * The 'sessionoptions' option is changed.
3526 */
3527 char *
3528did_set_sessionoptions(optset_T *args)
3529{
3530 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
3531 return e_invalid_argument;
3532 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
3533 {
3534 // Don't allow both "sesdir" and "curdir".
3535 (void)opt_strings_flags(args->os_oldval.string, p_ssop_values,
3536 &ssop_flags, TRUE);
3537 return e_invalid_argument;
3538 }
3539
3540 return NULL;
3541}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003542
3543 int
3544expand_set_sessionoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3545{
3546 return expand_set_opt_string(
3547 args,
3548 p_ssop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003549 ARRAY_LENGTH(p_ssop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003550 numMatches,
3551 matches);
3552}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003553#endif
3554
3555/*
3556 * The 'shortmess' option is changed.
3557 */
3558 char *
3559did_set_shortmess(optset_T *args)
3560{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003561 char_u **varp = (char_u **)args->os_varp;
3562
Christian Brabandtb39b2402023-11-29 11:34:05 +01003563 return did_set_option_listflag(*varp, (char_u *)SHM_ALL, args->os_errbuf,
3564 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003565}
3566
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003567 int
3568expand_set_shortmess(optexpand_T *args, int *numMatches, char_u ***matches)
3569{
3570 return expand_set_opt_listflag(args, (char_u*)SHM_ALL, numMatches, matches);
3571}
3572
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003573#if defined(FEAT_LINEBREAK) || defined(PROTO)
3574/*
3575 * The 'showbreak' option is changed.
3576 */
3577 char *
3578did_set_showbreak(optset_T *args)
3579{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003580 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003581 char_u *s;
3582
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003583 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003584 {
3585 if (ptr2cells(s) != 1)
3586 return e_showbreak_contains_unprintable_or_wide_character;
3587 MB_PTR_ADV(s);
3588 }
3589
3590 return NULL;
3591}
3592#endif
3593
3594/*
3595 * The 'showcmdloc' option is changed.
3596 */
3597 char *
3598did_set_showcmdloc(optset_T *args UNUSED)
3599{
zeertzjqc27fcf42024-03-01 23:01:43 +01003600 char *errmsg = did_set_opt_strings(p_sloc, p_sloc_values, FALSE);
3601
3602 if (errmsg == NULL)
3603 comp_col();
3604
3605 return errmsg;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003606}
3607
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003608 int
3609expand_set_showcmdloc(optexpand_T *args, int *numMatches, char_u ***matches)
3610{
3611 return expand_set_opt_string(
3612 args,
3613 p_sloc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003614 ARRAY_LENGTH(p_sloc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003615 numMatches,
3616 matches);
3617}
3618
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003619#if defined(FEAT_SIGNS) || defined(PROTO)
3620/*
3621 * The 'signcolumn' option is changed.
3622 */
3623 char *
3624did_set_signcolumn(optset_T *args)
3625{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003626 char_u **varp = (char_u **)args->os_varp;
3627
3628 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003629 return e_invalid_argument;
3630 // When changing the 'signcolumn' to or from 'number', recompute the
3631 // width of the number column if 'number' or 'relativenumber' is set.
3632 if (((*args->os_oldval.string == 'n' && args->os_oldval.string[1] == 'u')
3633 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
3634 && (curwin->w_p_nu || curwin->w_p_rnu))
3635 curwin->w_nrwidth_line_count = 0;
3636
3637 return NULL;
3638}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003639
3640 int
3641expand_set_signcolumn(optexpand_T *args, int *numMatches, char_u ***matches)
3642{
3643 return expand_set_opt_string(
3644 args,
3645 p_scl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003646 ARRAY_LENGTH(p_scl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003647 numMatches,
3648 matches);
3649}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003650#endif
3651
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003652#if defined(FEAT_SPELL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003653/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003654 * The 'spellcapcheck' option is changed.
3655 */
3656 char *
3657did_set_spellcapcheck(optset_T *args UNUSED)
3658{
3659 // compile the regexp program.
3660 return compile_cap_prog(curwin->w_s);
3661}
3662
3663/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003664 * The 'spellfile' option is changed.
3665 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003666 char *
3667did_set_spellfile(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003668{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003669 char_u **varp = (char_u **)args->os_varp;
3670
3671 if (!valid_spellfile(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003672 return e_invalid_argument;
3673
3674 // If there is a window for this buffer in which 'spell' is set load the
3675 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003676 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003677}
3678
3679/*
3680 * The 'spell' option is changed.
3681 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003682 char *
3683did_set_spelllang(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003684{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003685 char_u **varp = (char_u **)args->os_varp;
3686
3687 if (!valid_spelllang(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003688 return e_invalid_argument;
3689
3690 // If there is a window for this buffer in which 'spell' is set load the
3691 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003692 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003693}
3694
3695/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003696 * The 'spelloptions' option is changed.
3697 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003698 char *
3699did_set_spelloptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003700{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003701 char_u **varp = (char_u **)args->os_varp;
3702
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003703 if (**varp != NUL && STRCMP(*varp, "camel") != 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003704 return e_invalid_argument;
3705
3706 return NULL;
3707}
3708
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003709 int
3710expand_set_spelloptions(optexpand_T *args, int *numMatches, char_u ***matches)
3711{
3712 static char *(p_spo_values[]) = {"camel", NULL};
3713 return expand_set_opt_string(
3714 args,
3715 p_spo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003716 ARRAY_LENGTH(p_spo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003717 numMatches,
3718 matches);
3719}
3720
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003721/*
3722 * The 'spellsuggest' option is changed.
3723 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003724 char *
3725did_set_spellsuggest(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003726{
3727 if (spell_check_sps() != OK)
3728 return e_invalid_argument;
3729
3730 return NULL;
3731}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003732
3733 int
3734expand_set_spellsuggest(optexpand_T *args, int *numMatches, char_u ***matches)
3735{
3736 return expand_set_opt_string(
3737 args,
3738 p_sps_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003739 ARRAY_LENGTH(p_sps_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003740 numMatches,
3741 matches);
3742}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003743#endif
3744
3745/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003746 * The 'splitkeep' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003747 */
3748 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003749did_set_splitkeep(optset_T *args UNUSED)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003750{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003751 return did_set_opt_strings(p_spk, p_spk_values, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003752}
3753
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003754 int
3755expand_set_splitkeep(optexpand_T *args, int *numMatches, char_u ***matches)
3756{
3757 return expand_set_opt_string(
3758 args,
3759 p_spk_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003760 ARRAY_LENGTH(p_spk_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003761 numMatches,
3762 matches);
3763}
3764
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00003765#if defined(FEAT_STL_OPT) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003766/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003767 * The 'statusline' option is changed.
3768 */
3769 char *
3770did_set_statusline(optset_T *args)
3771{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003772 return parse_statustabline_rulerformat(args, FALSE);
3773}
3774#endif
3775
3776/*
3777 * The 'swapsync' option is changed.
3778 */
3779 char *
3780did_set_swapsync(optset_T *args UNUSED)
3781{
3782 return did_set_opt_strings(p_sws, p_sws_values, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003783}
3784
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003785 int
3786expand_set_swapsync(optexpand_T *args, int *numMatches, char_u ***matches)
3787{
3788 return expand_set_opt_string(
3789 args,
3790 p_sws_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003791 ARRAY_LENGTH(p_sws_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003792 numMatches,
3793 matches);
3794}
3795
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003796/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003797 * The 'switchbuf' option is changed.
3798 */
3799 char *
3800did_set_switchbuf(optset_T *args UNUSED)
3801{
3802 return did_set_opt_flags(p_swb, p_swb_values, &swb_flags, TRUE);
3803}
3804
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003805 int
3806expand_set_switchbuf(optexpand_T *args, int *numMatches, char_u ***matches)
3807{
3808 return expand_set_opt_string(
3809 args,
3810 p_swb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003811 ARRAY_LENGTH(p_swb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003812 numMatches,
3813 matches);
3814}
3815
LemonBoy5247b0b2024-07-12 19:30:58 +02003816/*
3817 * The 'tabclose' option is changed.
3818 */
3819 char *
3820did_set_tabclose(optset_T *args UNUSED)
3821{
3822 return did_set_opt_flags(p_tcl, p_tcl_values, &tcl_flags, TRUE);
3823}
3824
3825 int
3826expand_set_tabclose(optexpand_T *args, int *numMatches, char_u ***matches)
3827{
3828 return expand_set_opt_string(
3829 args,
3830 p_tcl_values,
3831 ARRAY_LENGTH(p_tcl_values) - 1,
3832 numMatches,
3833 matches);
3834}
3835
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003836#if defined(FEAT_STL_OPT) || defined(PROTO)
3837/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003838 * The 'tabline' option is changed.
3839 */
3840 char *
3841did_set_tabline(optset_T *args)
3842{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003843 return parse_statustabline_rulerformat(args, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003844}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003845#endif
3846
3847/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003848 * The 'tagcase' option is changed.
3849 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003850 char *
3851did_set_tagcase(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003852{
3853 unsigned int *flags;
3854 char_u *p;
3855
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003856 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003857 {
3858 p = curbuf->b_p_tc;
3859 flags = &curbuf->b_tc_flags;
3860 }
3861 else
3862 {
3863 p = p_tc;
3864 flags = &tc_flags;
3865 }
3866
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003867 if ((args->os_flags & OPT_LOCAL) && *p == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003868 // make the local value empty: use the global value
3869 *flags = 0;
3870 else if (*p == NUL
3871 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
3872 return e_invalid_argument;
3873
3874 return NULL;
3875}
3876
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003877 int
3878expand_set_tagcase(optexpand_T *args, int *numMatches, char_u ***matches)
3879{
3880 return expand_set_opt_string(
3881 args,
3882 p_tc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003883 ARRAY_LENGTH(p_tc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003884 numMatches,
3885 matches);
3886}
3887
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003888/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003889 * The 'term' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003890 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003891 char *
3892did_set_term(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003893{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003894 if (T_NAME[0] == NUL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003895 return e_cannot_set_term_to_empty_string;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003896#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003897 if (gui.in_use)
3898 return e_cannot_change_term_in_GUI;
3899 if (term_is_gui(T_NAME))
3900 return e_use_gui_to_start_GUI;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003901#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003902 if (set_termname(T_NAME) == FAIL)
3903 return e_not_found_in_termcap;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003904
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003905 // Screen colors may have changed.
3906 redraw_later_clear();
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003907
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003908 return NULL;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003909}
3910
3911/*
3912 * Some terminal option (t_xxx) is changed
3913 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003914 char *
3915did_set_term_option(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003916{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003917 char_u **varp = (char_u **)args->os_varp;
3918
3919 if (!full_screen)
3920 return NULL;
3921
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003922 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
3923 if (varp == &T_CCO)
3924 {
3925 int colors = atoi((char *)T_CCO);
3926
3927 // Only reinitialize colors if t_Co value has really changed to
3928 // avoid expensive reload of colorscheme if t_Co is set to the
3929 // same value multiple times.
3930 if (colors != t_colors)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003931 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003932 t_colors = colors;
3933 if (t_colors <= 1)
3934 {
3935 vim_free(T_CCO);
3936 T_CCO = empty_option;
3937 }
3938#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3939 if (is_term_win32())
3940 {
3941 swap_tcap();
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003942 args->os_did_swaptcap = TRUE;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003943 }
3944#endif
3945 // We now have a different color setup, initialize it again.
3946 init_highlight(TRUE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003947 }
3948 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003949 ttest(FALSE);
3950 if (varp == &T_ME)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003951 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003952 out_str(T_ME);
3953 redraw_later(UPD_CLEAR);
3954#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3955 // Since t_me has been set, this probably means that the user
3956 // wants to use this as default colors. Need to reset default
3957 // background/foreground colors.
3958# ifdef VIMDLL
3959 if (!gui.in_use && !gui.starting)
3960# endif
3961 mch_set_normal_colors();
3962#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003963 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003964 if (varp == &T_BE && termcap_active)
3965 {
3966 MAY_WANT_TO_LOG_THIS;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003967
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003968 if (*T_BE == NUL)
3969 // When clearing t_BE we assume the user no longer wants
3970 // bracketed paste, thus disable it by writing t_BD.
3971 out_str(T_BD);
3972 else
3973 out_str(T_BE);
3974 }
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003975
3976 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003977}
3978
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003979#if defined(FEAT_TERMINAL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003980/*
3981 * The 'termwinkey' option is changed.
3982 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003983 char *
Milly94606f72024-10-22 22:07:52 +02003984did_set_termwinkey(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003985{
Milly94606f72024-10-22 22:07:52 +02003986 char_u **varp = (char_u **)args->os_varp;
3987
3988 if ((*varp)[0] != NUL && string_to_key(*varp, TRUE) == 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003989 return e_invalid_argument;
3990
3991 return NULL;
3992}
3993
3994/*
3995 * The 'termwinsize' option is changed.
3996 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003997 char *
Milly8be10aa2024-10-22 22:01:46 +02003998did_set_termwinsize(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003999{
Milly8be10aa2024-10-22 22:01:46 +02004000 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004001 char_u *p;
4002
Milly8be10aa2024-10-22 22:01:46 +02004003 if ((*varp)[0] == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004004 return NULL;
4005
Milly8be10aa2024-10-22 22:01:46 +02004006 p = skipdigits(*varp);
4007 if (p == *varp || (*p != 'x' && *p != '*') || *skipdigits(p + 1) != NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004008 return e_invalid_argument;
4009
4010 return NULL;
4011}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00004012
4013# if defined(MSWIN) || defined(PROTO)
4014/*
4015 * The 'termwintype' option is changed.
4016 */
4017 char *
4018did_set_termwintype(optset_T *args UNUSED)
4019{
4020 return did_set_opt_strings(p_twt, p_twt_values, FALSE);
4021}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004022
4023 int
4024expand_set_termwintype(optexpand_T *args, int *numMatches, char_u ***matches)
4025{
4026 return expand_set_opt_string(
4027 args,
4028 p_twt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004029 ARRAY_LENGTH(p_twt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004030 numMatches,
4031 matches);
4032}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00004033# endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004034#endif
4035
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004036/*
4037 * The 'titlestring' option is changed.
4038 */
4039 char *
4040did_set_titlestring(optset_T *args)
4041{
4042 int flagval = 0;
4043
4044#ifdef FEAT_STL_OPT
4045 flagval = STL_IN_TITLE;
4046#endif
4047 return parse_titleiconstring(args, flagval);
4048}
4049
4050#if (defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)) || defined(PROTO)
4051/*
4052 * The 'toolbar' option is changed.
4053 */
4054 char *
4055did_set_toolbar(optset_T *args UNUSED)
4056{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004057 if (opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags,
4058 TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004059 return e_invalid_argument;
4060
4061 out_flush();
4062 gui_mch_show_toolbar((toolbar_flags &
4063 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4064 return NULL;
4065}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004066
4067 int
4068expand_set_toolbar(optexpand_T *args, int *numMatches, char_u ***matches)
4069{
4070 return expand_set_opt_string(
4071 args,
4072 p_toolbar_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004073 ARRAY_LENGTH(p_toolbar_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004074 numMatches,
4075 matches);
4076}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004077#endif
4078
4079#if (defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)) || defined(PROTO)
4080/*
4081 * The 'toolbariconsize' option is changed. GTK+ 2 only.
4082 */
4083 char *
4084did_set_toolbariconsize(optset_T *args UNUSED)
4085{
4086 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
4087 return e_invalid_argument;
4088
4089 out_flush();
4090 gui_mch_show_toolbar((toolbar_flags &
4091 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4092 return NULL;
4093}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004094
4095 int
4096expand_set_toolbariconsize(optexpand_T *args, int *numMatches, char_u ***matches)
4097{
4098 return expand_set_opt_string(
4099 args,
4100 p_tbis_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004101 ARRAY_LENGTH(p_tbis_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004102 numMatches,
4103 matches);
4104}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004105#endif
4106
4107#if defined(UNIX) || defined(VMS) || defined(PROTO)
4108/*
4109 * The 'ttymouse' option is changed.
4110 */
4111 char *
4112did_set_ttymouse(optset_T *args UNUSED)
4113{
4114 char *errmsg = NULL;
4115
4116 // Switch the mouse off before changing the escape sequences used for
4117 // that.
4118 mch_setmouse(FALSE);
4119 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
4120 errmsg = e_invalid_argument;
4121 else
4122 check_mouse_termcode();
4123 if (termcap_active)
4124 setmouse(); // may switch it on again
4125
4126 return errmsg;
4127}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004128
4129 int
4130expand_set_ttymouse(optexpand_T *args, int *numMatches, char_u ***matches)
4131{
4132 return expand_set_opt_string(
4133 args,
4134 p_ttym_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004135 ARRAY_LENGTH(p_ttym_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004136 numMatches,
4137 matches);
4138}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004139#endif
4140
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004141#if defined(FEAT_VARTABS) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004142/*
4143 * The 'varsofttabstop' option is changed.
4144 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004145 char *
4146did_set_varsofttabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004147{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004148 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004149 char_u *cp;
4150
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004151 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004152 VIM_CLEAR(curbuf->b_p_vsts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004153 else
4154 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004155 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004156 {
4157 if (vim_isdigit(*cp))
4158 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004159 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004160 continue;
4161 return e_invalid_argument;
4162 }
4163
4164 int *oldarray = curbuf->b_p_vsts_array;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004165 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004166 {
4167 if (oldarray)
4168 vim_free(oldarray);
4169 }
4170 else
4171 return e_invalid_argument;
4172 }
4173
4174 return NULL;
4175}
4176
4177/*
4178 * The 'vartabstop' option is changed.
4179 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004180 char *
4181did_set_vartabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004182{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004183 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004184 char_u *cp;
4185
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004186 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004187 VIM_CLEAR(curbuf->b_p_vts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004188 else
4189 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004190 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004191 {
4192 if (vim_isdigit(*cp))
4193 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004194 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004195 continue;
4196 return e_invalid_argument;
4197 }
4198
4199 int *oldarray = curbuf->b_p_vts_array;
4200
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004201 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004202 {
4203 vim_free(oldarray);
4204# ifdef FEAT_FOLDING
4205 if (foldmethodIsIndent(curwin))
4206 foldUpdateAll(curwin);
4207# endif
4208 }
4209 else
4210 return e_invalid_argument;
4211 }
4212
4213 return NULL;
4214}
4215#endif
4216
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004217/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004218 * The 'verbosefile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004219 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004220 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004221did_set_verbosefile(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004222{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004223 verbose_stop();
4224 if (*p_vfile != NUL && verbose_open() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004225 return e_invalid_argument;
4226
4227 return NULL;
4228}
4229
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004230#if defined(FEAT_SESSION) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004231/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004232 * The 'viewoptions' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004233 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004234 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004235did_set_viewoptions(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004236{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004237 return did_set_opt_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004238}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004239#endif
4240
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004241#if defined(FEAT_VIMINFO) || defined(PROTO)
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004242/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004243 * The 'viminfo' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004244 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004245 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004246did_set_viminfo(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004247{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004248 char_u *s;
4249 char *errmsg = NULL;
4250
4251 for (s = p_viminfo; *s;)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004252 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004253 // Check it's a valid character
4254 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
4255 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01004256 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004257 break;
4258 }
4259 if (*s == 'n') // name is always last one
4260 break;
4261 else if (*s == 'r') // skip until next ','
4262 {
4263 while (*++s && *s != ',')
4264 ;
4265 }
4266 else if (*s == '%')
4267 {
4268 // optional number
4269 while (vim_isdigit(*++s))
4270 ;
4271 }
4272 else if (*s == '!' || *s == 'h' || *s == 'c')
4273 ++s; // no extra chars
4274 else // must have a number
4275 {
4276 while (vim_isdigit(*++s))
4277 ;
4278
4279 if (!VIM_ISDIGIT(*(s - 1)))
4280 {
4281 if (args->os_errbuf != NULL)
4282 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01004283 vim_snprintf(args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004284 _(e_missing_number_after_angle_str_angle),
4285 transchar_byte(*(s - 1)));
4286 errmsg = args->os_errbuf;
4287 }
4288 else
4289 errmsg = "";
4290 break;
4291 }
4292 }
4293 if (*s == ',')
4294 ++s;
4295 else if (*s)
4296 {
4297 if (args->os_errbuf != NULL)
4298 errmsg = e_missing_comma;
4299 else
4300 errmsg = "";
4301 break;
4302 }
4303 }
4304 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
4305 errmsg = e_must_specify_a_value;
4306
4307 return errmsg;
4308}
4309#endif
4310
4311/*
4312 * The 'virtualedit' option is changed.
4313 */
4314 char *
4315did_set_virtualedit(optset_T *args)
4316{
4317 char_u *ve = p_ve;
4318 unsigned int *flags = &ve_flags;
4319
4320 if (args->os_flags & OPT_LOCAL)
4321 {
4322 ve = curwin->w_p_ve;
4323 flags = &curwin->w_ve_flags;
4324 }
4325
4326 if ((args->os_flags & OPT_LOCAL) && *ve == NUL)
4327 // make the local value empty: use the global value
4328 *flags = 0;
4329 else
4330 {
4331 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
4332 return e_invalid_argument;
4333 else if (STRCMP(ve, args->os_oldval.string) != 0)
4334 {
4335 // Recompute cursor position in case the new 've' setting
4336 // changes something.
4337 validate_virtcol();
4338 coladvance(curwin->w_virtcol);
4339 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004340 }
4341
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004342 return NULL;
4343}
4344
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004345 int
4346expand_set_virtualedit(optexpand_T *args, int *numMatches, char_u ***matches)
4347{
4348 return expand_set_opt_string(
4349 args,
4350 p_ve_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004351 ARRAY_LENGTH(p_ve_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004352 numMatches,
4353 matches);
4354}
4355
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004356/*
4357 * The 'whichwrap' option is changed.
4358 */
4359 char *
4360did_set_whichwrap(optset_T *args)
4361{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004362 char_u **varp = (char_u **)args->os_varp;
4363
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004364 // Add ',' to the list flags because 'whichwrap' is a flag
4365 // list that is comma-separated.
Christian Brabandtb39b2402023-11-29 11:34:05 +01004366 return did_set_option_listflag(*varp, (char_u *)(WW_ALL ","),
4367 args->os_errbuf, args->os_errbuflen);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004368}
4369
4370 int
4371expand_set_whichwrap(optexpand_T *args, int *numMatches, char_u ***matches)
4372{
4373 return expand_set_opt_listflag(args, (char_u*)WW_ALL, numMatches, matches);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004374}
4375
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004376/*
4377 * The 'wildmode' option is changed.
4378 */
4379 char *
4380did_set_wildmode(optset_T *args UNUSED)
4381{
4382 if (check_opt_wim() == FAIL)
4383 return e_invalid_argument;
4384 return NULL;
4385}
4386
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004387 int
4388expand_set_wildmode(optexpand_T *args, int *numMatches, char_u ***matches)
4389{
4390 return expand_set_opt_string(
4391 args,
4392 p_wim_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004393 ARRAY_LENGTH(p_wim_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004394 numMatches,
4395 matches);
4396}
4397
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004398/*
4399 * The 'wildoptions' option is changed.
4400 */
4401 char *
4402did_set_wildoptions(optset_T *args UNUSED)
4403{
4404 return did_set_opt_strings(p_wop, p_wop_values, TRUE);
4405}
4406
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004407 int
4408expand_set_wildoptions(optexpand_T *args, int *numMatches, char_u ***matches)
4409{
4410 return expand_set_opt_string(
4411 args,
4412 p_wop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004413 ARRAY_LENGTH(p_wop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004414 numMatches,
4415 matches);
4416}
4417
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004418#if defined(FEAT_WAK) || defined(PROTO)
4419/*
4420 * The 'winaltkeys' option is changed.
4421 */
4422 char *
4423did_set_winaltkeys(optset_T *args UNUSED)
4424{
4425 char *errmsg = NULL;
4426
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004427 if (*p_wak == NUL || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004428 errmsg = e_invalid_argument;
4429# ifdef FEAT_MENU
4430# if defined(FEAT_GUI_MOTIF)
4431 else if (gui.in_use)
4432 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4433# elif defined(FEAT_GUI_GTK)
4434 else if (gui.in_use)
4435 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4436# endif
4437# endif
4438 return errmsg;
4439}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004440
4441 int
4442expand_set_winaltkeys(optexpand_T *args, int *numMatches, char_u ***matches)
4443{
4444 return expand_set_opt_string(
4445 args,
4446 p_wak_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004447 ARRAY_LENGTH(p_wak_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004448 numMatches,
4449 matches);
4450}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004451#endif
4452
4453/*
4454 * The 'wincolor' option is changed.
4455 */
4456 char *
4457did_set_wincolor(optset_T *args UNUSED)
4458{
4459#ifdef FEAT_TERMINAL
4460 term_update_wincolor(curwin);
4461#endif
4462 return NULL;
4463}
4464
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004465 int
4466expand_set_wincolor(optexpand_T *args, int *numMatches, char_u ***matches)
4467{
4468 return expand_set_opt_generic(
4469 args,
4470 get_highlight_name,
4471 numMatches,
4472 matches);
4473}
4474
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004475#ifdef FEAT_SYN_HL
4476/*
4477 * When the 'syntax' option is set, load the syntax of that name.
4478 */
4479 static void
4480do_syntax_autocmd(int value_changed)
4481{
4482 static int syn_recursive = 0;
4483
4484 ++syn_recursive;
4485 // Only pass TRUE for "force" when the value changed or not used
4486 // recursively, to avoid endless recurrence.
4487 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
4488 value_changed || syn_recursive == 1, curbuf);
4489 curbuf->b_flags |= BF_SYN_SET;
4490 --syn_recursive;
4491}
4492#endif
4493
4494/*
4495 * When the 'filetype' option is set, trigger the FileType autocommand.
4496 */
4497 static void
4498do_filetype_autocmd(char_u **varp, int opt_flags, int value_changed)
4499{
4500 // Skip this when called from a modeline and the filetype was already set
4501 // to this value.
4502 if ((opt_flags & OPT_MODELINE) && !value_changed)
4503 return;
4504
4505 static int ft_recursive = 0;
4506 int secure_save = secure;
4507
4508 // Reset the secure flag, since the value of 'filetype' has
4509 // been checked to be safe.
4510 secure = 0;
4511
4512 ++ft_recursive;
zeertzjq5bf6c212024-03-31 18:41:27 +02004513 curbuf->b_did_filetype = TRUE;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004514 // Only pass TRUE for "force" when the value changed or not
4515 // used recursively, to avoid endless recurrence.
4516 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
4517 value_changed || ft_recursive == 1, curbuf);
4518 --ft_recursive;
4519 // Just in case the old "curbuf" is now invalid.
4520 if (varp != &(curbuf->b_p_ft))
4521 varp = NULL;
4522
4523 secure = secure_save;
4524}
4525
4526#ifdef FEAT_SPELL
4527/*
4528 * When the 'spelllang' option is set, source the spell/LANG.vim file in
4529 * 'runtimepath'.
4530 */
4531 static void
4532do_spelllang_source(void)
4533{
4534 char_u fname[200];
4535 char_u *p;
4536 char_u *q = curwin->w_s->b_p_spl;
4537
4538 // Skip the first name if it is "cjk".
4539 if (STRNCMP(q, "cjk,", 4) == 0)
4540 q += 4;
4541
4542 // They could set 'spellcapcheck' depending on the language. Use the first
4543 // name in 'spelllang' up to '_region' or '.encoding'.
4544 for (p = q; *p != NUL; ++p)
4545 if (!ASCII_ISALNUM(*p) && *p != '-')
4546 break;
4547 if (p > q)
4548 {
4549 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
4550 (int)(p - q), q);
4551 source_runtime(fname, DIP_ALL);
4552 }
4553}
4554#endif
4555
4556/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004557 * Handle string options that need some action to perform when changed.
zeertzjqf6782732022-07-27 18:26:03 +01004558 * The new value must be allocated.
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004559 * Returns NULL for success, or an untranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +02004560 */
4561 char *
4562did_set_string_option(
4563 int opt_idx, // index in options[] table
4564 char_u **varp, // pointer to the option variable
Bram Moolenaardac13472019-09-16 21:06:21 +02004565 char_u *oldval, // previous value of the option
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004566 char_u *value, // new value of the option
Bram Moolenaardac13472019-09-16 21:06:21 +02004567 char *errbuf, // buffer for errors, or NULL
Mike Williams620f0112023-12-05 15:36:06 +01004568 size_t errbuflen, // length of error buffer
Bram Moolenaardac13472019-09-16 21:06:21 +02004569 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004570 set_op_T op, // OP_ADDING/OP_PREPENDING/OP_REMOVING
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004571 int *value_checked) // value was checked to be safe, no
Bram Moolenaardac13472019-09-16 21:06:21 +02004572 // need to set P_INSECURE
4573{
4574 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +02004575 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004576 opt_did_set_cb_T did_set_cb = get_option_did_set_cb(opt_idx);
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004577 optset_T args;
4578
4579 // 'ttytype' is an alias for 'term'. Both 'term' and 'ttytype' point to
4580 // T_NAME. If 'term' or 'ttytype' is modified, then use the index for the
4581 // 'term' option. Only set the P_ALLOCED flag on 'term'.
4582 if (varp == &T_NAME)
4583 {
4584 opt_idx = findoption((char_u *)"term");
4585 if (opt_idx >= 0)
4586 {
4587 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
4588 did_set_cb = get_option_did_set_cb(opt_idx);
4589 }
4590 }
4591
4592 CLEAR_FIELD(args);
Bram Moolenaardac13472019-09-16 21:06:21 +02004593
Bram Moolenaardac13472019-09-16 21:06:21 +02004594 // Disallow changing some options from secure mode
4595 if ((secure
4596#ifdef HAVE_SANDBOX
4597 || sandbox != 0
4598#endif
4599 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00004600 errmsg = e_not_allowed_here;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004601 // Check for a "normal" directory or file name in some options.
4602 else if (check_illegal_path_names(opt_idx, varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004603 errmsg = e_invalid_argument;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004604 else if (did_set_cb != NULL)
4605 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004606 args.os_varp = (char_u *)varp;
4607 args.os_idx = opt_idx;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004608 args.os_flags = opt_flags;
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004609 args.os_op = op;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004610 args.os_oldval.string = oldval;
4611 args.os_newval.string = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004612 args.os_errbuf = errbuf;
Christian Brabandtb39b2402023-11-29 11:34:05 +01004613 args.os_errbuflen = errbuflen;
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004614 // Invoke the option specific callback function to validate and apply
4615 // the new option value.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004616 errmsg = did_set_cb(&args);
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004617
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004618 // The 'keymap', 'filetype' and 'syntax' option callback functions
4619 // may change the os_value_checked field.
4620 *value_checked = args.os_value_checked;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004621 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004622
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004623 // If an error is detected, restore the previous value.
Bram Moolenaardac13472019-09-16 21:06:21 +02004624 if (errmsg != NULL)
4625 {
zeertzjqf6782732022-07-27 18:26:03 +01004626 free_string_option(*varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02004627 *varp = oldval;
4628 // When resetting some values, need to act on it.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004629 if (args.os_restore_chartab)
Bram Moolenaardac13472019-09-16 21:06:21 +02004630 (void)init_chartab();
4631 if (varp == &p_hl)
4632 (void)highlight_changed();
4633 }
4634 else
4635 {
4636#ifdef FEAT_EVAL
4637 // Remember where the option was set.
4638 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4639#endif
4640 // Free string options that are in allocated memory.
4641 // Use "free_oldval", because recursiveness may change the flags under
4642 // our fingers (esp. init_highlight()).
4643 if (free_oldval)
4644 free_string_option(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01004645 set_option_flag(opt_idx, P_ALLOCED);
Bram Moolenaardac13472019-09-16 21:06:21 +02004646
4647 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
4648 && is_global_local_option(opt_idx))
4649 {
4650 // global option with local value set to use global value; free
4651 // the local value and make it empty
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004652 char_u *p = get_option_varp_scope(opt_idx, OPT_LOCAL);
Bram Moolenaardac13472019-09-16 21:06:21 +02004653 free_string_option(*(char_u **)p);
4654 *(char_u **)p = empty_option;
4655 }
4656
4657 // May set global value for local option.
4658 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
4659 set_string_option_global(opt_idx, varp);
4660
4661 // Trigger the autocommand only after setting the flags.
4662#ifdef FEAT_SYN_HL
Bram Moolenaardac13472019-09-16 21:06:21 +02004663 if (varp == &(curbuf->b_p_syn))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004664 do_syntax_autocmd(args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004665#endif
4666 else if (varp == &(curbuf->b_p_ft))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004667 do_filetype_autocmd(varp, opt_flags, args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004668#ifdef FEAT_SPELL
4669 if (varp == &(curwin->w_s->b_p_spl))
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004670 do_spelllang_source();
Bram Moolenaardac13472019-09-16 21:06:21 +02004671#endif
4672 }
4673
Bram Moolenaardac13472019-09-16 21:06:21 +02004674 if (varp == &p_mouse)
4675 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004676 if (*p_mouse == NUL)
4677 mch_setmouse(FALSE); // switch mouse off
4678 else
Bram Moolenaardac13472019-09-16 21:06:21 +02004679 setmouse(); // in case 'mouse' changed
4680 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004681
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004682 if (varp == &p_rtp)
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004683 {
4684 export_myvimdir();
4685#if defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004686 update_package_paths_in_lua();
4687#endif
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004688 }
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004689
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004690#if defined(FEAT_LINEBREAK)
4691 // Changing Formatlistpattern when briopt includes the list setting:
4692 // redraw
4693 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
4694 && curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004695 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004696#endif
4697
Bram Moolenaardac13472019-09-16 21:06:21 +02004698 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004699 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0
4700 && (get_option_flags(opt_idx) & P_HLONLY) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02004701 curwin->w_set_curswant = TRUE;
4702
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004703 if ((opt_flags & OPT_NO_REDRAW) == 0)
4704 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004705#ifdef FEAT_GUI
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004706 // set when changing an option that only requires a redraw in the GUI
4707 int redraw_gui_only = FALSE;
4708
4709 if (varp == &p_go // 'guioptions'
4710 || varp == &p_guifont // 'guifont'
4711# ifdef FEAT_GUI_TABLINE
4712 || varp == &p_gtl // 'guitablabel'
4713 || varp == &p_gtt // 'guitabtooltip'
4714# endif
4715# ifdef FEAT_XFONTSET
4716 || varp == &p_guifontset // 'guifontset'
4717# endif
4718 || varp == &p_guifontwide // 'guifontwide'
Erik S. V. Jansson2f026382024-02-26 22:23:05 +01004719# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004720 || varp == &p_guiligatures // 'guiligatures'
4721# endif
4722 )
4723 redraw_gui_only = TRUE;
4724
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004725 // check redraw when it's not a GUI option or the GUI is active.
4726 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02004727#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004728 check_redraw(get_option_flags(opt_idx));
4729 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004730
4731#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004732 if (args.os_did_swaptcap)
Bram Moolenaardac13472019-09-16 21:06:21 +02004733 {
4734 set_termname((char_u *)"win32");
4735 init_highlight(TRUE, FALSE);
4736 }
4737#endif
4738
4739 return errmsg;
4740}
4741
4742/*
4743 * Check an option that can be a range of string values.
4744 *
4745 * Return OK for correct value, FAIL otherwise.
4746 * Empty is always OK.
4747 */
4748 static int
4749check_opt_strings(
4750 char_u *val,
4751 char **values,
4752 int list) // when TRUE: accept a list of values
4753{
4754 return opt_strings_flags(val, values, NULL, list);
4755}
4756
4757/*
4758 * Handle an option that can be a range of string values.
4759 * Set a flag in "*flagp" for each string present.
4760 *
4761 * Return OK for correct value, FAIL otherwise.
4762 * Empty is always OK.
4763 */
4764 static int
4765opt_strings_flags(
4766 char_u *val, // new value
4767 char **values, // array of valid string values
4768 unsigned *flagp,
4769 int list) // when TRUE: accept a list of values
4770{
4771 int i;
4772 int len;
4773 unsigned new_flags = 0;
4774
4775 while (*val)
4776 {
4777 for (i = 0; ; ++i)
4778 {
4779 if (values[i] == NULL) // val not found in values[]
4780 return FAIL;
4781
4782 len = (int)STRLEN(values[i]);
4783 if (STRNCMP(values[i], val, len) == 0
4784 && ((list && val[len] == ',') || val[len] == NUL))
4785 {
4786 val += len + (val[len] == ',');
4787 new_flags |= (1 << i);
4788 break; // check next item in val list
4789 }
4790 }
4791 }
4792 if (flagp != NULL)
4793 *flagp = new_flags;
4794
4795 return OK;
4796}
4797
4798/*
4799 * return OK if "p" is a valid fileformat name, FAIL otherwise.
4800 */
4801 int
4802check_ff_value(char_u *p)
4803{
4804 return check_opt_strings(p, p_ff_values, FALSE);
4805}
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004806
4807/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004808 * Save the actual shortmess Flags and clear them temporarily to avoid that
4809 * file messages overwrites any output from the following commands.
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004810 *
4811 * Caller must make sure to first call save_clear_shm_value() and then
4812 * restore_shm_value() exactly the same number of times.
4813 */
4814 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004815save_clear_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004816{
4817 if (STRLEN(p_shm) >= SHM_LEN)
4818 {
4819 iemsg(e_internal_error_shortmess_too_long);
4820 return;
4821 }
4822
4823 if (++set_shm_recursive == 1)
4824 {
4825 STRCPY(shm_buf, p_shm);
4826 set_option_value_give_err((char_u *)"shm", 0L, (char_u *)"", 0);
4827 }
4828}
4829
4830/*
4831 * Restore the shortmess Flags set from the save_clear_shm_value() function.
4832 */
4833 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004834restore_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004835{
4836 if (--set_shm_recursive == 0)
4837 {
4838 set_option_value_give_err((char_u *)"shm", 0L, shm_buf, 0);
4839 vim_memset(shm_buf, 0, SHM_LEN);
4840 }
4841}
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004842
4843/*
4844 * Export the environment variable $MYVIMDIR to the first item in runtimepath
4845 */
Christian Brabandt1a741d32025-03-01 16:30:33 +01004846 void
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004847export_myvimdir()
4848{
4849 int dofree = FALSE;
4850 char_u *p;
4851 char_u *q = p_rtp;
4852 char_u *buf = alloc(MAXPATHL);
4853
4854 if (buf == NULL)
4855 return;
4856
4857 (void)copy_option_part(&q, buf, MAXPATHL, ",");
4858
4859 p = vim_getenv((char_u *)"MYVIMDIR", &dofree);
4860
4861 if (p == NULL || STRCMP(p, buf) != 0)
4862 {
4863 add_pathsep(buf);
4864#ifdef MSWIN
4865 // normalize path separators
4866 for (q = buf; *q != NUL; q++)
4867 if (*q == '/')
4868 *q = '\\';
4869#endif
4870 vim_setenv((char_u *)"MYVIMDIR", buf);
4871 }
4872 if (dofree)
4873 vim_free(p);
4874 vim_free(buf);
4875}