blob: 5b863bcc9f79c01c0312210b7c38e9c9f961d564 [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
1662/*
glepnir6a89c942024-10-01 20:32:12 +02001663 * The 'completeitemalign' option is changed.
1664 */
1665 char *
1666did_set_completeitemalign(optset_T *args UNUSED)
1667{
1668 char_u *p = p_cia;
1669 unsigned new_cia_flags = 0;
1670 int seen[3] = { FALSE, FALSE, FALSE };
1671 int count = 0;
1672 char_u buf[10];
1673
1674 while (*p)
1675 {
1676 copy_option_part(&p, buf, sizeof(buf), ",");
1677 if (count >= 3)
1678 return e_invalid_argument;
1679
1680 if (STRCMP(buf, "abbr") == 0)
1681 {
1682 if (seen[CPT_ABBR])
1683 return e_invalid_argument;
1684 new_cia_flags = new_cia_flags * 10 + CPT_ABBR;
1685 seen[CPT_ABBR] = TRUE;
1686 count++;
1687 }
1688 else if (STRCMP(buf, "kind") == 0)
1689 {
1690 if (seen[CPT_KIND])
1691 return e_invalid_argument;
1692 new_cia_flags = new_cia_flags * 10 + CPT_KIND;
1693 seen[CPT_KIND] = TRUE;
1694 count++;
1695 }
1696 else if (STRCMP(buf, "menu") == 0)
1697 {
1698 if (seen[CPT_MENU])
1699 return e_invalid_argument;
1700 new_cia_flags = new_cia_flags * 10 + CPT_MENU;
1701 seen[CPT_MENU] = TRUE;
1702 count++;
1703 }
1704 else
1705 return e_invalid_argument;
1706 }
1707 if (new_cia_flags == 0 || count != 3)
1708 return e_invalid_argument;
1709
1710 cia_flags = new_cia_flags;
1711 return NULL;
1712}
1713
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001714#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1715/*
1716 * The 'completepopup' option is changed.
1717 */
1718 char *
1719did_set_completepopup(optset_T *args UNUSED)
1720{
1721 if (parse_completepopup(NULL) == FAIL)
1722 return e_invalid_argument;
1723
1724 popup_close_info();
1725 return NULL;
1726}
1727#endif
1728
1729#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1730/*
1731 * The 'completeslash' option is changed.
1732 */
1733 char *
1734did_set_completeslash(optset_T *args UNUSED)
1735{
1736 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1737 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
1738 return e_invalid_argument;
1739
1740 return NULL;
1741}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001742
1743 int
1744expand_set_completeslash(optexpand_T *args, int *numMatches, char_u ***matches)
1745{
1746 return expand_set_opt_string(
1747 args,
1748 p_csl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001749 ARRAY_LENGTH(p_csl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001750 numMatches,
1751 matches);
1752}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001753#endif
1754
1755#if defined(FEAT_CONCEAL) || defined(PROTO)
1756/*
1757 * The 'concealcursor' option is changed.
1758 */
1759 char *
1760did_set_concealcursor(optset_T *args)
1761{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001762 char_u **varp = (char_u **)args->os_varp;
1763
Christian Brabandtb39b2402023-11-29 11:34:05 +01001764 return did_set_option_listflag(*varp, (char_u *)COCU_ALL, args->os_errbuf,
1765 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001766}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001767
1768 int
1769expand_set_concealcursor(optexpand_T *args, int *numMatches, char_u ***matches)
1770{
1771 return expand_set_opt_listflag(args, (char_u*)COCU_ALL, numMatches, matches);
1772}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001773#endif
1774
1775/*
1776 * The 'cpoptions' option is changed.
1777 */
1778 char *
1779did_set_cpoptions(optset_T *args)
1780{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001781 char_u **varp = (char_u **)args->os_varp;
1782
Christian Brabandtb39b2402023-11-29 11:34:05 +01001783 return did_set_option_listflag(*varp, (char_u *)CPO_ALL, args->os_errbuf,
1784 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001785}
1786
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001787 int
1788expand_set_cpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
1789{
1790 return expand_set_opt_listflag(args, (char_u*)CPO_ALL, numMatches, matches);
1791}
1792
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001793#if defined(FEAT_CRYPT) || defined(PROTO)
1794/*
1795 * The 'cryptkey' option is changed.
1796 */
1797 char *
1798did_set_cryptkey(optset_T *args)
1799{
1800 // Make sure the ":set" command doesn't show the new value in the
1801 // history.
1802 remove_key_from_history();
1803
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02001804 if (args->os_op != OP_NONE)
1805 // Don't allow set+=/-=/^= as they can allow for substring guessing
1806 return e_invalid_argument;
1807
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001808 if (STRCMP(curbuf->b_p_key, args->os_oldval.string) != 0)
1809 {
1810 // Need to update the swapfile.
1811 ml_set_crypt_key(curbuf, args->os_oldval.string,
1812 *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
1813 changed_internal();
1814 }
Christian Brabandt19e6c4f2023-06-27 18:57:10 +01001815# ifdef FEAT_SODIUM
1816 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
1817 crypt_sodium_lock_key(args->os_newval.string);
1818# endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001819
1820 return NULL;
1821}
1822
1823/*
1824 * The 'cryptmethod' option is changed.
1825 */
1826 char *
1827did_set_cryptmethod(optset_T *args)
1828{
1829 char_u *p;
1830 char_u *s;
1831
1832 if (args->os_flags & OPT_LOCAL)
1833 p = curbuf->b_p_cm;
1834 else
1835 p = p_cm;
1836 if (check_opt_strings(p, p_cm_values, TRUE) != OK)
1837 return e_invalid_argument;
1838 else if (crypt_self_test() == FAIL)
1839 return e_invalid_argument;
1840
1841 // When setting the global value to empty, make it "zip".
1842 if (*p_cm == NUL)
1843 {
1844 free_string_option(p_cm);
1845 p_cm = vim_strsave((char_u *)"zip");
1846 }
1847 // When using ":set cm=name" the local value is going to be empty.
1848 // Do that here, otherwise the crypt functions will still use the
1849 // local value.
1850 if ((args->os_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1851 {
1852 free_string_option(curbuf->b_p_cm);
1853 curbuf->b_p_cm = empty_option;
1854 }
1855
1856 // Need to update the swapfile when the effective method changed.
1857 // Set "s" to the effective old value, "p" to the effective new
1858 // method and compare.
1859 if ((args->os_flags & OPT_LOCAL) && *args->os_oldval.string == NUL)
1860 s = p_cm; // was previously using the global value
1861 else
1862 s = args->os_oldval.string;
1863 if (*curbuf->b_p_cm == NUL)
1864 p = p_cm; // is now using the global value
1865 else
1866 p = curbuf->b_p_cm;
1867 if (STRCMP(s, p) != 0)
1868 ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1869
1870 // If the global value changes need to update the swapfile for all
1871 // buffers using that value.
1872 if ((args->os_flags & OPT_GLOBAL)
1873 && STRCMP(p_cm, args->os_oldval.string) != 0)
1874 {
1875 buf_T *buf;
1876
1877 FOR_ALL_BUFFERS(buf)
1878 if (buf != curbuf && *buf->b_p_cm == NUL)
1879 ml_set_crypt_key(buf, buf->b_p_key, args->os_oldval.string);
1880 }
1881 return NULL;
1882}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001883
1884 int
1885expand_set_cryptmethod(optexpand_T *args, int *numMatches, char_u ***matches)
1886{
1887 return expand_set_opt_string(
1888 args,
1889 p_cm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001890 ARRAY_LENGTH(p_cm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001891 numMatches,
1892 matches);
1893}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001894#endif
1895
1896#if (defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1897/*
1898 * The 'cscopequickfix' option is changed.
1899 */
1900 char *
1901did_set_cscopequickfix(optset_T *args UNUSED)
1902{
1903 char_u *p;
1904
1905 if (p_csqf == NULL)
1906 return NULL;
1907
1908 p = p_csqf;
1909 while (*p != NUL)
1910 {
1911 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
1912 || p[1] == NUL
1913 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
1914 || (p[2] != NUL && p[2] != ','))
1915 return e_invalid_argument;
1916 else if (p[2] == NUL)
1917 break;
1918 else
1919 p += 3;
1920 }
1921
1922 return NULL;
1923}
1924#endif
1925
1926#if defined(FEAT_SYN_HL) || defined(PROTO)
1927/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001928 * The 'cursorlineopt' option is changed.
1929 */
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001930 char *
1931did_set_cursorlineopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001932{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001933 char_u **varp = (char_u **)args->os_varp;
1934
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001935 // This could be changed to use opt_strings_flags() instead.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001936 if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001937 return e_invalid_argument;
1938
1939 return NULL;
1940}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001941
1942 int
1943expand_set_cursorlineopt(optexpand_T *args, int *numMatches, char_u ***matches)
1944{
1945 return expand_set_opt_string(
1946 args,
1947 p_culopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001948 ARRAY_LENGTH(p_culopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001949 numMatches,
1950 matches);
1951}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001952#endif
1953
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001954/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001955 * The 'debug' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001956 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001957 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001958did_set_debug(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001959{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001960 return did_set_opt_strings(p_debug, p_debug_values, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001961}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001962
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001963 int
1964expand_set_debug(optexpand_T *args, int *numMatches, char_u ***matches)
1965{
1966 return expand_set_opt_string(
1967 args,
1968 p_debug_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001969 ARRAY_LENGTH(p_debug_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001970 numMatches,
1971 matches);
1972}
1973
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001974#if defined(FEAT_DIFF) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001975/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001976 * The 'diffopt' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001977 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001978 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001979did_set_diffopt(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001980{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001981 if (diffopt_changed() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001982 return e_invalid_argument;
1983
1984 return NULL;
1985}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001986
1987 int
1988expand_set_diffopt(optexpand_T *args, int *numMatches, char_u ***matches)
1989{
1990 expand_T *xp = args->oe_xp;
1991
1992 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
1993 {
1994 // Within "algorithm:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001995 int algo_len = (int)STRLEN("algorithm:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001996 if (xp->xp_pattern - args->oe_set_arg >= algo_len &&
1997 STRNCMP(xp->xp_pattern - algo_len, "algorithm:", algo_len) == 0)
1998 {
1999 return expand_set_opt_string(
2000 args,
2001 p_dip_algorithm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002002 ARRAY_LENGTH(p_dip_algorithm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002003 numMatches,
2004 matches);
2005 }
2006 return FAIL;
2007 }
2008
2009 return expand_set_opt_string(
2010 args,
2011 p_dip_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002012 ARRAY_LENGTH(p_dip_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002013 numMatches,
2014 matches);
2015}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002016#endif
2017
2018/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002019 * The 'display' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002020 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002021 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002022did_set_display(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002023{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002024 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002025 return e_invalid_argument;
2026
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002027 (void)init_chartab();
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002028 return NULL;
2029}
2030
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002031 int
2032expand_set_display(optexpand_T *args, int *numMatches, char_u ***matches)
2033{
2034 return expand_set_opt_string(
2035 args,
2036 p_dy_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002037 ARRAY_LENGTH(p_dy_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002038 numMatches,
2039 matches);
2040}
2041
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002042/*
2043 * The 'eadirection' option is changed.
2044 */
2045 char *
2046did_set_eadirection(optset_T *args UNUSED)
2047{
2048 return did_set_opt_strings(p_ead, p_ead_values, FALSE);
2049}
2050
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002051 int
2052expand_set_eadirection(optexpand_T *args, int *numMatches, char_u ***matches)
2053{
2054 return expand_set_opt_string(
2055 args,
2056 p_ead_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002057 ARRAY_LENGTH(p_ead_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002058 numMatches,
2059 matches);
2060}
2061
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002062/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002063 * One of the 'encoding', 'fileencoding', 'termencoding' or 'makeencoding'
2064 * options is changed.
2065 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002066 char *
2067did_set_encoding(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002068{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002069 char_u **varp = (char_u **)args->os_varp;
2070 char_u **gvarp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002071 char *errmsg = NULL;
2072 char_u *p;
2073
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002074 // Get the global option to compare with, otherwise we would have to check
2075 // two values for all local options.
2076 gvarp = (char_u **)get_option_varp_scope(args->os_idx, OPT_GLOBAL);
2077
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002078 if (gvarp == &p_fenc)
2079 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002080 if (!curbuf->b_p_ma && args->os_flags != OPT_GLOBAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002081 errmsg = e_cannot_make_changes_modifiable_is_off;
2082 else if (vim_strchr(*varp, ',') != NULL)
2083 // No comma allowed in 'fileencoding'; catches confusing it
2084 // with 'fileencodings'.
2085 errmsg = e_invalid_argument;
2086 else
2087 {
2088 // May show a "+" in the title now.
2089 redraw_titles();
2090 // Add 'fileencoding' to the swap file.
2091 ml_setflags(curbuf);
2092 }
2093 }
2094 if (errmsg == NULL)
2095 {
2096 // canonize the value, so that STRCMP() can be used on it
2097 p = enc_canonize(*varp);
2098 if (p != NULL)
2099 {
2100 vim_free(*varp);
2101 *varp = p;
2102 }
2103 if (varp == &p_enc)
2104 {
2105 errmsg = mb_init();
2106 redraw_titles();
2107 }
2108 }
2109
2110#if defined(FEAT_GUI_GTK)
2111 if (errmsg == NULL && varp == &p_tenc && gui.in_use)
2112 {
2113 // GTK uses only a single encoding, and that is UTF-8.
2114 if (STRCMP(p_tenc, "utf-8") != 0)
2115 errmsg = e_cannot_be_changed_in_gtk_GUI;
2116 }
2117#endif
2118
2119 if (errmsg == NULL)
2120 {
2121#ifdef FEAT_KEYMAP
2122 // When 'keymap' is used and 'encoding' changes, reload the keymap
2123 // (with another encoding).
2124 if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
2125 (void)keymap_init();
2126#endif
2127
2128 // When 'termencoding' is not empty and 'encoding' changes or when
2129 // 'termencoding' changes, need to setup for keyboard input and
2130 // display output conversion.
2131 if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
2132 {
2133 if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
2134 || convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
2135 {
2136 semsg(_(e_cannot_convert_between_str_and_str),
2137 p_tenc, p_enc);
2138 errmsg = e_invalid_argument;
2139 }
2140 }
2141
2142#if defined(MSWIN)
K.Takatace3189d2023-02-15 19:13:43 +00002143 // $HOME, $VIM and $VIMRUNTIME may have characters in active code page.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002144 if (varp == &p_enc)
K.Takatace3189d2023-02-15 19:13:43 +00002145 {
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002146 init_homedir();
K.Takatace3189d2023-02-15 19:13:43 +00002147 init_vimdir();
2148 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002149#endif
2150 }
2151
2152 return errmsg;
2153}
2154
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002155 int
2156expand_set_encoding(optexpand_T *args, int *numMatches, char_u ***matches)
2157{
2158 return expand_set_opt_generic(
2159 args,
2160 get_encoding_name,
2161 numMatches,
2162 matches);
2163}
2164
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002165/*
Luuk van Baalb7147f82025-02-08 18:52:39 +01002166 * The 'eventignore(win)' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002167 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002168 char *
Luuk van Baalb7147f82025-02-08 18:52:39 +01002169did_set_eventignore(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002170{
Luuk van Baalb7147f82025-02-08 18:52:39 +01002171 char_u **varp = (char_u **)args->os_varp;
2172
2173 if (check_ei(*varp) == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002174 return e_invalid_argument;
2175 return NULL;
2176}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002177
Luuk van Baalb7147f82025-02-08 18:52:39 +01002178static int expand_eiw = FALSE;
2179
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002180 static char_u *
2181get_eventignore_name(expand_T *xp, int idx)
2182{
Luuk van Baalb7147f82025-02-08 18:52:39 +01002183 // 'eventignore(win)' allows special keyword "all" in addition to
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002184 // all event names.
2185 if (idx == 0)
2186 return (char_u *)"all";
Luuk van Baalb7147f82025-02-08 18:52:39 +01002187 return get_event_name_no_group(xp, idx - 1, expand_eiw);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002188}
2189
2190 int
2191expand_set_eventignore(optexpand_T *args, int *numMatches, char_u ***matches)
2192{
Luuk van Baalb7147f82025-02-08 18:52:39 +01002193 expand_eiw = args->oe_varp != (char_u *)&p_ei;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002194 return expand_set_opt_generic(
2195 args,
2196 get_eventignore_name,
2197 numMatches,
2198 matches);
2199}
2200
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002201/*
2202 * The 'fileformat' option is changed.
2203 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002204 char *
2205did_set_fileformat(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002206{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002207 char_u **varp = (char_u **)args->os_varp;
2208
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002209 if (!curbuf->b_p_ma && !(args->os_flags & OPT_GLOBAL))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002210 return e_cannot_make_changes_modifiable_is_off;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002211 else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002212 return e_invalid_argument;
2213
2214 // may also change 'textmode'
2215 if (get_fileformat(curbuf) == EOL_DOS)
2216 curbuf->b_p_tx = TRUE;
2217 else
2218 curbuf->b_p_tx = FALSE;
2219 redraw_titles();
2220 // update flag in swap file
2221 ml_setflags(curbuf);
2222 // Redraw needed when switching to/from "mac": a CR in the text
2223 // will be displayed differently.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002224 if (get_fileformat(curbuf) == EOL_MAC || *args->os_oldval.string == 'm')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002225 redraw_curbuf_later(UPD_NOT_VALID);
2226
2227 return NULL;
2228}
2229
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002230 int
2231expand_set_fileformat(optexpand_T *args, int *numMatches, char_u ***matches)
2232{
2233 return expand_set_opt_string(
2234 args,
2235 p_ff_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002236 ARRAY_LENGTH(p_ff_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002237 numMatches,
2238 matches);
2239}
2240
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002241/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002242 * Function given to ExpandGeneric() to obtain the possible arguments of the
2243 * fileformat options.
2244 */
2245 char_u *
2246get_fileformat_name(expand_T *xp UNUSED, int idx)
2247{
2248 if (idx >= (int)ARRAY_LENGTH(p_ff_values))
2249 return NULL;
2250
2251 return (char_u*)p_ff_values[idx];
2252}
2253
2254/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002255 * The 'fileformats' option is changed.
2256 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002257 char *
2258did_set_fileformats(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002259{
2260 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
2261 return e_invalid_argument;
2262
2263 // also change 'textauto'
2264 if (*p_ffs == NUL)
2265 p_ta = FALSE;
2266 else
2267 p_ta = TRUE;
2268
2269 return NULL;
2270}
2271
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002272/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002273 * The 'filetype' or the 'syntax' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002274 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002275 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002276did_set_filetype_or_syntax(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002277{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002278 char_u **varp = (char_u **)args->os_varp;
2279
2280 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002281 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002282
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002283 args->os_value_changed = STRCMP(args->os_oldval.string, *varp) != 0;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002284
2285 // Since we check the value, there is no need to set P_INSECURE,
2286 // even when the value comes from a modeline.
2287 args->os_value_checked = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002288
2289 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002290}
2291
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002292#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002293/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002294 * The 'foldclose' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002295 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002296 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002297did_set_foldclose(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002298{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002299 return did_set_opt_strings(p_fcl, p_fcl_values, TRUE);
2300}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002301
2302 int
2303expand_set_foldclose(optexpand_T *args, int *numMatches, char_u ***matches)
2304{
2305 return expand_set_opt_string(
2306 args,
2307 p_fcl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002308 ARRAY_LENGTH(p_fcl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002309 numMatches,
2310 matches);
2311}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002312#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002313
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002314#if (defined(FEAT_EVAL) && defined(FEAT_FOLDING)) || defined(PROTO)
2315/*
2316 * The 'foldexpr' option is changed.
2317 */
2318 char *
2319did_set_foldexpr(optset_T *args)
2320{
2321 (void)did_set_optexpr(args);
2322 if (foldmethodIsExpr(curwin))
2323 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002324 return NULL;
2325}
2326#endif
2327
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002328#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002329/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002330 * The 'foldignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002331 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002332 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002333did_set_foldignore(optset_T *args UNUSED)
2334{
2335 if (foldmethodIsIndent(curwin))
2336 foldUpdateAll(curwin);
2337 return NULL;
2338}
2339
2340/*
2341 * The 'foldmarker' option is changed.
2342 */
2343 char *
2344did_set_foldmarker(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002345{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002346 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002347 char_u *p;
2348
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002349 p = vim_strchr(*varp, ',');
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002350 if (p == NULL)
2351 return e_comma_required;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002352 else if (p == *varp || p[1] == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002353 return e_invalid_argument;
2354 else if (foldmethodIsMarker(curwin))
2355 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002356
2357 return NULL;
2358}
2359
2360/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002361 * The 'foldmethod' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002362 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002363 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002364did_set_foldmethod(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002365{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002366 char_u **varp = (char_u **)args->os_varp;
2367
Milly142cad12024-10-22 22:11:51 +02002368 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK || **varp == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002369 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002370
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002371 foldUpdateAll(curwin);
2372 if (foldmethodIsDiff(curwin))
2373 newFoldLevel();
2374 return NULL;
2375}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002376
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002377 int
2378expand_set_foldmethod(optexpand_T *args, int *numMatches, char_u ***matches)
2379{
2380 return expand_set_opt_string(
2381 args,
2382 p_fdm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002383 ARRAY_LENGTH(p_fdm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002384 numMatches,
2385 matches);
2386}
2387
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002388/*
2389 * The 'foldopen' option is changed.
2390 */
2391 char *
2392did_set_foldopen(optset_T *args UNUSED)
2393{
2394 return did_set_opt_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
2395}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002396
2397 int
2398expand_set_foldopen(optexpand_T *args, int *numMatches, char_u ***matches)
2399{
2400 return expand_set_opt_string(
2401 args,
2402 p_fdo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002403 ARRAY_LENGTH(p_fdo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002404 numMatches,
2405 matches);
2406}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002407#endif
2408
2409/*
2410 * The 'formatoptions' option is changed.
2411 */
2412 char *
2413did_set_formatoptions(optset_T *args)
2414{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002415 char_u **varp = (char_u **)args->os_varp;
2416
Christian Brabandtb39b2402023-11-29 11:34:05 +01002417 return did_set_option_listflag(*varp, (char_u *)FO_ALL, args->os_errbuf,
2418 args->os_errbuflen);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002419}
2420
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002421 int
2422expand_set_formatoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2423{
2424 return expand_set_opt_listflag(args, (char_u*)FO_ALL, numMatches, matches);
2425}
2426
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002427#if defined(CURSOR_SHAPE) || defined(PROTO)
2428/*
2429 * The 'guicursor' option is changed.
2430 */
2431 char *
2432did_set_guicursor(optset_T *args UNUSED)
2433{
2434 return parse_shape_opt(SHAPE_CURSOR);
2435}
2436#endif
2437
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002438#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002439/*
2440 * The 'guifont' option is changed.
2441 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002442 char *
2443did_set_guifont(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002444{
2445 char_u *p;
2446 char *errmsg = NULL;
2447
2448 if (gui.in_use)
2449 {
2450 p = p_guifont;
2451# if defined(FEAT_GUI_GTK)
2452 // Put up a font dialog and let the user select a new value.
2453 // If this is cancelled go back to the old value but don't
2454 // give an error message.
2455 if (STRCMP(p, "*") == 0)
2456 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002457 p = gui_mch_font_dialog(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002458 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002459 p_guifont = (p != NULL) ? p : vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002460 }
2461# endif
2462 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
2463 {
2464# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
2465 if (STRCMP(p_guifont, "*") == 0)
2466 {
2467 // Dialog was cancelled: Keep the old value without giving
2468 // an error message.
2469 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002470 p_guifont = vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002471 }
2472 else
2473# endif
2474 errmsg = e_invalid_fonts;
2475 }
2476 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002477
2478 return errmsg;
2479}
2480
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002481/*
2482 * Expand the 'guifont' option. Only when GUI is being used. Each platform has
2483 * specific behaviors.
2484 */
2485 int
2486expand_set_guifont(optexpand_T *args, int *numMatches, char_u ***matches)
2487{
2488 if (!gui.in_use)
2489 return FAIL;
2490
2491# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
2492 char_u **varp = (char_u **)args->oe_varp;
2493 int wide = (varp == &p_guifontwide);
2494
2495 return expand_set_opt_callback(
2496 args, gui_mch_expand_font, &wide, numMatches, matches);
2497# else
2498 return FAIL;
2499# endif
2500}
2501
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002502# if defined(FEAT_XFONTSET) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002503/*
2504 * The 'guifontset' option is changed.
2505 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002506 char *
2507did_set_guifontset(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002508{
2509 char *errmsg = NULL;
2510
2511 if (STRCMP(p_guifontset, "*") == 0)
2512 errmsg = e_cant_select_fontset;
2513 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
2514 errmsg = e_invalid_fontset;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002515
2516 return errmsg;
2517}
2518# endif
2519
2520/*
2521 * The 'guifontwide' option is changed.
2522 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002523 char *
2524did_set_guifontwide(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002525{
2526 char *errmsg = NULL;
2527
2528 if (STRCMP(p_guifontwide, "*") == 0)
2529 errmsg = e_cant_select_wide_font;
2530 else if (gui_get_wide_font() == FAIL)
2531 errmsg = e_invalid_wide_font;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002532
2533 return errmsg;
2534}
2535#endif
2536
Erik S. V. Jansson8b1e7492024-02-24 14:26:52 +01002537#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002538/*
2539 * The 'guiligatures' option is changed.
2540 */
2541 char *
2542did_set_guiligatures(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002543{
2544 gui_set_ligatures();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002545 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002546}
2547#endif
2548
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002549#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002550/*
2551 * The 'guioptions' option is changed.
2552 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002553 char *
2554did_set_guioptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002555{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002556 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002557 char *errmsg;
2558
Christian Brabandtb39b2402023-11-29 11:34:05 +01002559 errmsg = did_set_option_listflag(*varp, (char_u *)GO_ALL, args->os_errbuf,
2560 args->os_errbuflen);
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002561 if (errmsg != NULL)
2562 return errmsg;
2563
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002564 gui_init_which_components(args->os_oldval.string);
2565 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002566}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002567
2568 int
2569expand_set_guioptions(optexpand_T *args, int *numMatches, char_u ***matches)
2570{
2571 return expand_set_opt_listflag(args, (char_u*)GO_ALL, numMatches, matches);
2572}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002573#endif
2574
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002575#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002576/*
2577 * The 'guitablabel' option is changed.
2578 */
2579 char *
2580did_set_guitablabel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002581{
2582 redraw_tabline = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002583 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002584}
2585#endif
2586
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002587/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002588 * The 'helpfile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002589 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002590 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002591did_set_helpfile(optset_T *args UNUSED)
2592{
2593 // May compute new values for $VIM and $VIMRUNTIME
2594 if (didset_vim)
2595 vim_unsetenv_ext((char_u *)"VIM");
2596 if (didset_vimruntime)
2597 vim_unsetenv_ext((char_u *)"VIMRUNTIME");
2598 return NULL;
2599}
2600
2601#if defined(FEAT_MULTI_LANG) || defined(PROTO)
2602/*
2603 * The 'helplang' option is changed.
2604 */
2605 char *
2606did_set_helplang(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002607{
2608 char *errmsg = NULL;
2609
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002610 // Check for "", "ab", "ab,cd", etc.
2611 for (char_u *s = p_hlg; *s != NUL; s += 3)
2612 {
2613 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
2614 {
2615 errmsg = e_invalid_argument;
2616 break;
2617 }
2618 if (s[2] == NUL)
2619 break;
2620 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002621
2622 return errmsg;
2623}
2624#endif
2625
2626/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002627 * The 'highlight' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002628 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002629 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002630did_set_highlight(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002631{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002632 if (highlight_changed() == FAIL)
2633 return e_invalid_argument; // invalid flags
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002634
2635 return NULL;
2636}
2637
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002638/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002639 * Expand 'highlight' option.
2640 */
2641 int
2642expand_set_highlight(optexpand_T *args, int *numMatches, char_u ***matches)
2643{
2644 char_u *p;
2645 expand_T *xp = args->oe_xp;
2646 static char_u hl_flags[HLF_COUNT] = HL_FLAGS;
Christian Brabandt3f168ec2023-10-02 23:21:11 +02002647 size_t i;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002648 int count = 0;
2649
2650 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2651 {
2652 // Right after a ':', meaning we just return all highlight names.
2653 return expand_set_opt_generic(
2654 args,
2655 get_highlight_name,
2656 numMatches,
2657 matches);
2658 }
2659
2660 if (*xp->xp_pattern == NUL)
2661 {
2662 // At beginning of a comma-separated list. Return the specific list of
2663 // supported occasions.
2664 *matches = ALLOC_MULT(char_u *, HLF_COUNT + 1);
2665 if (*matches == NULL)
2666 return FAIL;
2667
2668 // We still want to return the full option if it's requested.
2669 if (args->oe_include_orig_val)
2670 {
2671 p = vim_strsave(args->oe_opt_value);
2672 if (p == NULL)
2673 {
2674 VIM_CLEAR(*matches);
2675 return FAIL;
2676 }
2677 (*matches)[count++] = p;
2678 }
2679
2680 for (i = 0; i < HLF_COUNT; i++)
2681 {
2682 p = vim_strnsave(&hl_flags[i], 1);
2683 if (p == NULL)
2684 {
2685 if (count == 0)
2686 {
2687 VIM_CLEAR(*matches);
2688 return FAIL;
2689 }
2690 else
2691 break;
2692 }
2693 (*matches)[count++] = p;
2694 }
2695
2696 if (count == 0)
2697 {
2698 VIM_CLEAR(*matches);
2699 return FAIL;
2700 }
2701 *numMatches = count;
2702 return OK;
2703 }
2704
2705 // We are after the initial character (which indicates the occasion). We
2706 // already made sure we are not matching after a ':' above, so now we want
2707 // to match against display mode modifiers.
2708 // Since the xp_pattern starts from the beginning, we need to include it in
2709 // the returned match.
2710
2711 // Note: Keep this in sync with highlight_changed()
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002712 static char_u p_hl_mode_values[] =
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002713 {':', 'b', 'i', '-', 'n', 'r', 's', 'u', 'c', '2', 'd', '=', 't'};
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002714 size_t num_hl_modes = ARRAY_LENGTH(p_hl_mode_values);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002715
2716 *matches = ALLOC_MULT(char_u *, num_hl_modes);
2717 if (*matches == NULL)
2718 return FAIL;
2719
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002720 int pattern_len = xp->xp_pattern_len;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002721
2722 for (i = 0; i < num_hl_modes; i++)
2723 {
2724 // Don't allow duplicates as these are unique flags
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002725 char_u *dup = vim_strchr(xp->xp_pattern + 1, p_hl_mode_values[i]);
2726 if (dup != NULL && (int)(dup - xp->xp_pattern) < pattern_len)
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002727 continue;
2728
2729 // ':' only works by itself, not with other flags.
2730 if (pattern_len > 1 && p_hl_mode_values[i] == ':')
2731 continue;
2732
2733 p = vim_strnsave(xp->xp_pattern, pattern_len + 1);
2734 if (p == NULL)
2735 {
2736 if (i == 0)
2737 {
2738 VIM_CLEAR(*matches);
2739 return FAIL;
2740 }
2741 else
2742 break;
2743 }
2744 p[pattern_len] = p_hl_mode_values[i];
2745 p[pattern_len + 1] = NUL;
2746 (*matches)[count++] = p;
2747 }
2748 if (count == 0)
2749 {
2750 VIM_CLEAR(*matches);
2751 return FAIL;
2752 }
2753 *numMatches = count;
2754
2755 return OK;
2756}
2757
2758/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002759 * The 'titlestring' or the 'iconstring' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002760 */
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002761 static char *
2762parse_titleiconstring(optset_T *args UNUSED, int flagval UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002763{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002764#ifdef FEAT_STL_OPT
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002765 char_u **varp = (char_u **)args->os_varp;
2766
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002767 // NULL => statusline syntax
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002768 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002769 stl_syntax |= flagval;
2770 else
2771 stl_syntax &= ~flagval;
2772#endif
2773 did_set_title();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002774
2775 return NULL;
2776}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002777
2778/*
2779 * The 'iconstring' option is changed.
2780 */
2781 char *
2782did_set_iconstring(optset_T *args)
2783{
2784 int flagval = 0;
2785
2786#ifdef FEAT_STL_OPT
2787 flagval = STL_IN_ICON;
2788#endif
2789
2790 return parse_titleiconstring(args, flagval);
2791}
2792
2793#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
2794/*
2795 * The 'imactivatekey' option is changed.
2796 */
2797 char *
2798did_set_imactivatekey(optset_T *args UNUSED)
2799{
2800 if (!im_xim_isvalid_imactivate())
2801 return e_invalid_argument;
2802 return NULL;
2803}
2804#endif
2805
2806/*
Milly5e7a6a42024-10-22 22:27:19 +02002807 * The 'iskeyword' option is changed.
2808 */
2809 char *
2810did_set_iskeyword(optset_T *args)
2811{
2812 char_u **varp = (char_u **)args->os_varp;
2813
2814 if (varp == &p_isk) // only check for global-value
2815 {
2816 if (check_isopt(*varp) == FAIL)
2817 return e_invalid_argument;
2818 }
2819 else // fallthrough for local-value
2820 return did_set_isopt(args);
2821
2822 return NULL;
2823}
2824
2825/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002826 * The 'isident' or the 'iskeyword' or the 'isprint' or the 'isfname' option is
2827 * changed.
2828 */
2829 char *
2830did_set_isopt(optset_T *args)
2831{
Milly5e7a6a42024-10-22 22:27:19 +02002832 // 'isident', 'iskeyword', 'isprint' or 'isfname' option: refill g_chartab[]
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002833 // If the new option is invalid, use old value.
2834 // 'lisp' option: refill g_chartab[] for '-' char.
2835 if (init_chartab() == FAIL)
2836 {
2837 args->os_restore_chartab = TRUE;// need to restore the chartab.
2838 return e_invalid_argument; // error in value
2839 }
2840
2841 return NULL;
2842}
2843
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002844/*
2845 * The 'jumpoptions' option is changed.
2846 */
2847 char *
Yegappan Lakshmanan1926ae42023-09-21 16:36:28 +02002848did_set_jumpoptions(optset_T *args UNUSED)
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002849{
2850 if (opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE) != OK)
2851 return e_invalid_argument;
2852
2853 return NULL;
2854}
2855
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002856 int
2857expand_set_jumpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2858{
2859 return expand_set_opt_string(
2860 args,
2861 p_jop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002862 ARRAY_LENGTH(p_jop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002863 numMatches,
2864 matches);
2865}
2866
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002867#if defined(FEAT_KEYMAP) || defined(PROTO)
2868/*
2869 * The 'keymap' option is changed.
2870 */
2871 char *
2872did_set_keymap(optset_T *args)
2873{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002874 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002875 char *errmsg = NULL;
2876
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002877 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002878 errmsg = e_invalid_argument;
2879 else
2880 {
2881 int secure_save = secure;
2882
2883 // Reset the secure flag, since the value of 'keymap' has
2884 // been checked to be safe.
2885 secure = 0;
2886
2887 // load or unload key mapping tables
2888 errmsg = keymap_init();
2889
2890 secure = secure_save;
2891
2892 // Since we check the value, there is no need to set P_INSECURE,
2893 // even when the value comes from a modeline.
2894 args->os_value_checked = TRUE;
2895 }
2896
2897 if (errmsg == NULL)
2898 {
2899 if (*curbuf->b_p_keymap != NUL)
2900 {
2901 // Installed a new keymap, switch on using it.
2902 curbuf->b_p_iminsert = B_IMODE_LMAP;
2903 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
2904 curbuf->b_p_imsearch = B_IMODE_LMAP;
2905 }
2906 else
2907 {
2908 // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
2909 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
2910 curbuf->b_p_iminsert = B_IMODE_NONE;
2911 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
2912 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
2913 }
2914 if ((args->os_flags & OPT_LOCAL) == 0)
2915 {
2916 set_iminsert_global();
2917 set_imsearch_global();
2918 }
2919 status_redraw_curbuf();
2920 }
2921
2922 return errmsg;
2923}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002924#endif
2925
2926/*
2927 * The 'keymodel' option is changed.
2928 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002929 char *
2930did_set_keymodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002931{
2932 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
2933 return e_invalid_argument;
2934
2935 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
2936 km_startsel = (vim_strchr(p_km, 'a') != NULL);
2937 return NULL;
2938}
2939
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002940 int
2941expand_set_keymodel(optexpand_T *args, int *numMatches, char_u ***matches)
2942{
2943 return expand_set_opt_string(
2944 args,
2945 p_km_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002946 ARRAY_LENGTH(p_km_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002947 numMatches,
2948 matches);
2949}
2950
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002951/*
2952 * The 'keyprotocol' option is changed.
2953 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002954 char *
2955did_set_keyprotocol(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002956{
Gregory Anders3695d0e2023-09-29 20:17:20 +02002957 char_u *term = T_NAME;
2958 keyprot_T kpc = match_keyprotocol(term);
2959 if (kpc == KEYPROTOCOL_FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002960 return e_invalid_argument;
2961
Gregory Anders3695d0e2023-09-29 20:17:20 +02002962 apply_keyprotocol(term, kpc);
2963
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002964 return NULL;
2965}
2966
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002967 int
2968expand_set_keyprotocol(optexpand_T *args, int *numMatches, char_u ***matches)
2969{
2970 expand_T *xp = args->oe_xp;
2971 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2972 {
2973 // 'keyprotocol' only has well-defined terms for completion for the
2974 // protocol part after the colon.
2975 return expand_set_opt_string(
2976 args,
2977 p_kpc_protocol_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002978 ARRAY_LENGTH(p_kpc_protocol_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002979 numMatches,
2980 matches);
2981 }
2982 // Use expand_set_opt_string instead of returning FAIL so that we can
2983 // include the original value if args->oe_include_orig_val is set.
2984 static char *(empty[]) = {NULL};
2985 return expand_set_opt_string(args, empty, 0, numMatches, matches);
2986}
2987
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002988/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002989 * The 'lispoptions' option is changed.
2990 */
2991 char *
2992did_set_lispoptions(optset_T *args)
2993{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002994 char_u **varp = (char_u **)args->os_varp;
2995
2996 if (**varp != NUL
2997 && STRCMP(*varp, "expr:0") != 0
2998 && STRCMP(*varp, "expr:1") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002999 return e_invalid_argument;
3000
3001 return NULL;
3002}
3003
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003004 int
3005expand_set_lispoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3006{
3007 static char *(p_lop_values[]) = {"expr:0", "expr:1", NULL};
3008 return expand_set_opt_string(
3009 args,
3010 p_lop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003011 ARRAY_LENGTH(p_lop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003012 numMatches,
3013 matches);
3014}
3015
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003016/*
3017 * The 'matchpairs' option is changed.
3018 */
3019 char *
3020did_set_matchpairs(optset_T *args)
3021{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003022 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003023 char_u *p;
3024
3025 if (has_mbyte)
3026 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003027 for (p = *varp; *p != NUL; ++p)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003028 {
3029 int x2 = -1;
3030 int x3 = -1;
3031
3032 p += mb_ptr2len(p);
3033 if (*p != NUL)
3034 x2 = *p++;
3035 if (*p != NUL)
3036 {
3037 x3 = mb_ptr2char(p);
3038 p += mb_ptr2len(p);
3039 }
3040 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
3041 return e_invalid_argument;
3042 if (*p == NUL)
3043 break;
3044 }
3045 }
3046 else
3047 {
3048 // Check for "x:y,x:y"
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003049 for (p = *varp; *p != NUL; p += 4)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003050 {
3051 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
3052 return e_invalid_argument;
3053 if (p[3] == NUL)
3054 break;
3055 }
3056 }
3057
3058 return NULL;
3059}
3060
Christian Brabandt51d4d842024-12-06 17:26:25 +01003061/*
3062 * Process the updated 'messagesopt' option value.
3063 */
3064 char *
3065did_set_messagesopt(optset_T *args UNUSED)
3066{
3067 if (messagesopt_changed() == FAIL)
3068 return e_invalid_argument;
3069
3070 return NULL;
3071}
3072
3073 int
3074expand_set_messagesopt(optexpand_T *args, int *numMatches, char_u ***matches)
3075{
zeertzjq8cc43da2024-12-07 16:09:08 +01003076 static char *(p_mopt_values[]) = {"hit-enter", "wait:", "history:", NULL};
Christian Brabandt51d4d842024-12-06 17:26:25 +01003077 return expand_set_opt_string(
3078 args,
zeertzjq8cc43da2024-12-07 16:09:08 +01003079 p_mopt_values,
3080 ARRAY_LENGTH(p_mopt_values) - 1,
Christian Brabandt51d4d842024-12-06 17:26:25 +01003081 numMatches,
3082 matches);
3083}
3084
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003085#if defined(FEAT_SPELL) || defined(PROTO)
3086/*
3087 * The 'mkspellmem' option is changed.
3088 */
3089 char *
3090did_set_mkspellmem(optset_T *args UNUSED)
3091{
3092 if (spell_check_msm() != OK)
3093 return e_invalid_argument;
3094
3095 return NULL;
3096}
3097#endif
3098
3099/*
3100 * The 'mouse' option is changed.
3101 */
3102 char *
3103did_set_mouse(optset_T *args)
3104{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003105 char_u **varp = (char_u **)args->os_varp;
3106
Christian Brabandtb39b2402023-11-29 11:34:05 +01003107 return did_set_option_listflag(*varp, (char_u *)MOUSE_ALL, args->os_errbuf,
3108 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003109}
3110
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003111 int
3112expand_set_mouse(optexpand_T *args, int *numMatches, char_u ***matches)
3113{
3114 return expand_set_opt_listflag(args, (char_u*)MOUSE_ALL, numMatches, matches);
3115}
3116
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003117/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003118 * The 'mousemodel' option is changed.
3119 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003120 char *
3121did_set_mousemodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003122{
3123 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
3124 return e_invalid_argument;
3125#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
Ken Takata18d0d292023-12-19 20:12:29 +01003126 else if (*p_mousem != *args->os_oldval.string)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003127 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
3128 // to create or delete the popup menus.
3129 gui_motif_update_mousemodel(root_menu);
3130#endif
3131
3132 return NULL;
3133}
3134
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003135 int
3136expand_set_mousemodel(optexpand_T *args, int *numMatches, char_u ***matches)
3137{
3138 return expand_set_opt_string(
3139 args,
3140 p_mousem_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003141 ARRAY_LENGTH(p_mousem_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003142 numMatches,
3143 matches);
3144}
3145
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003146#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3147 char *
3148did_set_mouseshape(optset_T *args UNUSED)
3149{
3150 char *errmsg = NULL;
3151
3152 errmsg = parse_shape_opt(SHAPE_MOUSE);
3153 update_mouseshape(-1);
3154
3155 return errmsg;
3156}
3157#endif
3158
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003159/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003160 * The 'nrformats' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003161 */
3162 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003163did_set_nrformats(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003164{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003165 char_u **varp = (char_u **)args->os_varp;
3166
3167 return did_set_opt_strings(*varp, p_nf_values, TRUE);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003168}
3169
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003170 int
3171expand_set_nrformats(optexpand_T *args, int *numMatches, char_u ***matches)
3172{
3173 return expand_set_opt_string(
3174 args,
3175 p_nf_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003176 ARRAY_LENGTH(p_nf_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003177 numMatches,
3178 matches);
3179}
3180
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003181#if defined(FEAT_EVAL) || defined(PROTO)
3182/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003183 * One of the '*expr' options is changed: 'balloonexpr', 'diffexpr',
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003184 * 'foldexpr', 'foldtext', 'formatexpr', 'includeexpr', 'indentexpr',
3185 * 'patchexpr', 'printexpr' and 'charconvert'.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003186 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003187 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003188did_set_optexpr(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003189{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003190 char_u **varp = (char_u **)args->os_varp;
3191
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003192 // If the option value starts with <SID> or s:, then replace that with
3193 // the script identifier.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003194 char_u *name = get_scriptlocal_funcname(*varp);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003195 if (name != NULL)
3196 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003197 free_string_option(*varp);
3198 *varp = name;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003199 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003200
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003201 return NULL;
3202}
3203#endif
3204
3205/*
3206 * The 'pastetoggle' option is changed.
3207 */
3208 char *
3209did_set_pastetoggle(optset_T *args UNUSED)
3210{
3211 char_u *p;
3212
3213 // translate key codes like in a mapping
3214 if (*p_pt)
3215 {
zeertzjq7e0bae02023-08-11 23:15:38 +02003216 (void)replace_termcodes(p_pt, &p, 0,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003217 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
3218 if (p != NULL)
3219 {
3220 free_string_option(p_pt);
3221 p_pt = p;
3222 }
3223 }
3224
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003225 return NULL;
3226}
3227
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003228#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3229/*
3230 * The 'previewpopup' option is changed.
3231 */
3232 char *
3233did_set_previewpopup(optset_T *args UNUSED)
3234{
3235 if (parse_previewpopup(NULL) == FAIL)
3236 return e_invalid_argument;
3237
3238 return NULL;
3239}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003240
3241 int
3242expand_set_popupoption(optexpand_T *args, int *numMatches, char_u ***matches)
3243{
3244 expand_T *xp = args->oe_xp;
3245
3246 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3247 {
3248 // Within "highlight:"/"border:"/"align:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003249 int border_len = (int)STRLEN("border:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003250 if (xp->xp_pattern - args->oe_set_arg >= border_len &&
3251 STRNCMP(xp->xp_pattern - border_len, "border:", border_len) == 0)
3252 {
3253 return expand_set_opt_string(
3254 args,
3255 p_popup_option_border_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003256 ARRAY_LENGTH(p_popup_option_border_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003257 numMatches,
3258 matches);
3259 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003260 int align_len = (int)STRLEN("align:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003261 if (xp->xp_pattern - args->oe_set_arg >= align_len &&
3262 STRNCMP(xp->xp_pattern - align_len, "align:", align_len) == 0)
3263 {
3264 return expand_set_opt_string(
3265 args,
3266 p_popup_option_align_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003267 ARRAY_LENGTH(p_popup_option_align_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003268 numMatches,
3269 matches);
3270 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003271 int highlight_len = (int)STRLEN("highlight:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003272 if (xp->xp_pattern - args->oe_set_arg >= highlight_len &&
3273 STRNCMP(xp->xp_pattern - highlight_len, "highlight:", highlight_len) == 0)
3274 {
3275 // Return the list of all highlight names
3276 return expand_set_opt_generic(
3277 args,
3278 get_highlight_name,
3279 numMatches,
3280 matches);
3281 }
3282 return FAIL;
3283 }
3284
3285 return expand_set_opt_string(
3286 args,
3287 p_popup_option_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003288 ARRAY_LENGTH(p_popup_option_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003289 numMatches,
3290 matches);
3291}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003292#endif
3293
3294#if defined(FEAT_POSTSCRIPT) || defined(PROTO)
3295/*
3296 * The 'printencoding' option is changed.
3297 */
3298 char *
3299did_set_printencoding(optset_T *args UNUSED)
3300{
3301 char_u *s, *p;
3302
3303 // Canonize 'printencoding' if VIM standard one
3304 p = enc_canonize(p_penc);
3305 if (p != NULL)
3306 {
3307 vim_free(p_penc);
3308 p_penc = p;
3309 }
3310 else
3311 {
3312 // Ensure lower case and '-' for '_'
3313 for (s = p_penc; *s != NUL; s++)
3314 {
3315 if (*s == '_')
3316 *s = '-';
3317 else
3318 *s = TOLOWER_ASC(*s);
3319 }
3320 }
3321
3322 return NULL;
3323}
3324#endif
3325
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003326#if defined(FEAT_PRINTER) || defined(PROTO)
3327
3328 static char_u *
3329get_printoptions_names(expand_T *xp UNUSED, int idx)
3330{
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003331 if (idx >= (int)ARRAY_LENGTH(printer_opts))
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003332 return NULL;
3333 return (char_u*)printer_opts[idx].name;
3334}
3335
3336/*
3337 * Expand 'printoptions' option
3338 */
3339 int
3340expand_set_printoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3341{
3342 return expand_set_opt_generic(
3343 args,
3344 get_printoptions_names,
3345 numMatches,
3346 matches);
3347}
3348#endif
3349
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003350#if defined(FEAT_STL_OPT) || defined(PROTO)
3351/*
3352 * The 'statusline' or the 'tabline' or the 'rulerformat' option is changed.
3353 * "rulerformat" is TRUE if the 'rulerformat' option is changed.
3354 */
3355 static char *
3356parse_statustabline_rulerformat(optset_T *args, int rulerformat)
3357{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003358 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003359 char_u *s;
3360 char *errmsg = NULL;
3361 int wid;
3362
3363 if (rulerformat) // reset ru_wid first
3364 ru_wid = 0;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003365 s = *varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003366 if (rulerformat && *s == '%')
3367 {
3368 // set ru_wid if 'ruf' starts with "%99("
3369 if (*++s == '-') // ignore a '-'
3370 s++;
3371 wid = getdigits(&s);
3372 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
3373 ru_wid = wid;
3374 else
Yegappan Lakshmananac023e82024-11-27 20:55:45 +01003375 {
3376 // Validate the flags in 'rulerformat' only if it doesn't point to
3377 // a custom function ("%!" flag).
3378 if ((*varp)[1] != '!')
3379 errmsg = check_stl_option(p_ruf);
3380 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003381 }
3382 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
3383 else if (rulerformat || s[0] != '%' || s[1] != '!')
3384 errmsg = check_stl_option(s);
3385 if (rulerformat && errmsg == NULL)
3386 comp_col();
3387
3388 return errmsg;
3389}
3390#endif
3391
3392#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
3393/*
3394 * The 'renderoptions' option is changed.
3395 */
3396 char *
3397did_set_renderoptions(optset_T *args UNUSED)
3398{
3399 if (!gui_mch_set_rendering_options(p_rop))
3400 return e_invalid_argument;
3401
3402 return NULL;
3403}
3404#endif
3405
3406#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3407/*
3408 * The 'rightleftcmd' option is changed.
3409 */
3410 char *
3411did_set_rightleftcmd(optset_T *args)
3412{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003413 char_u **varp = (char_u **)args->os_varp;
3414
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003415 // Currently only "search" is a supported value.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003416 if (**varp != NUL && STRCMP(*varp, "search") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003417 return e_invalid_argument;
3418
3419 return NULL;
3420}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003421
3422 int
3423expand_set_rightleftcmd(optexpand_T *args, int *numMatches, char_u ***matches)
3424{
3425 static char *(p_rlc_values[]) = {"search", NULL};
3426 return expand_set_opt_string(
3427 args,
3428 p_rlc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003429 ARRAY_LENGTH(p_rlc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003430 numMatches,
3431 matches);
3432}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003433#endif
3434
3435#if defined(FEAT_STL_OPT) || defined(PROTO)
3436/*
3437 * The 'rulerformat' option is changed.
3438 */
3439 char *
3440did_set_rulerformat(optset_T *args)
3441{
3442 return parse_statustabline_rulerformat(args, TRUE);
3443}
3444#endif
3445
3446/*
3447 * The 'scrollopt' option is changed.
3448 */
3449 char *
3450did_set_scrollopt(optset_T *args UNUSED)
3451{
3452 return did_set_opt_strings(p_sbo, p_scbopt_values, TRUE);
3453}
3454
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003455 int
3456expand_set_scrollopt(optexpand_T *args, int *numMatches, char_u ***matches)
3457{
3458 return expand_set_opt_string(
3459 args,
3460 p_scbopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003461 ARRAY_LENGTH(p_scbopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003462 numMatches,
3463 matches);
3464}
3465
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003466/*
3467 * The 'selection' option is changed.
3468 */
3469 char *
3470did_set_selection(optset_T *args UNUSED)
3471{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003472 if (*p_sel == NUL || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003473 return e_invalid_argument;
3474
3475 return NULL;
3476}
3477
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003478 int
3479expand_set_selection(optexpand_T *args, int *numMatches, char_u ***matches)
3480{
3481 return expand_set_opt_string(
3482 args,
3483 p_sel_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003484 ARRAY_LENGTH(p_sel_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003485 numMatches,
3486 matches);
3487}
3488
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003489/*
3490 * The 'selectmode' option is changed.
3491 */
3492 char *
3493did_set_selectmode(optset_T *args UNUSED)
3494{
3495 return did_set_opt_strings(p_slm, p_slm_values, TRUE);
3496}
3497
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003498 int
3499expand_set_selectmode(optexpand_T *args, int *numMatches, char_u ***matches)
3500{
3501 return expand_set_opt_string(
3502 args,
3503 p_slm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003504 ARRAY_LENGTH(p_slm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003505 numMatches,
3506 matches);
3507}
3508
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003509#if defined(FEAT_SESSION) || defined(PROTO)
3510/*
3511 * The 'sessionoptions' option is changed.
3512 */
3513 char *
3514did_set_sessionoptions(optset_T *args)
3515{
3516 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
3517 return e_invalid_argument;
3518 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
3519 {
3520 // Don't allow both "sesdir" and "curdir".
3521 (void)opt_strings_flags(args->os_oldval.string, p_ssop_values,
3522 &ssop_flags, TRUE);
3523 return e_invalid_argument;
3524 }
3525
3526 return NULL;
3527}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003528
3529 int
3530expand_set_sessionoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3531{
3532 return expand_set_opt_string(
3533 args,
3534 p_ssop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003535 ARRAY_LENGTH(p_ssop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003536 numMatches,
3537 matches);
3538}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003539#endif
3540
3541/*
3542 * The 'shortmess' option is changed.
3543 */
3544 char *
3545did_set_shortmess(optset_T *args)
3546{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003547 char_u **varp = (char_u **)args->os_varp;
3548
Christian Brabandtb39b2402023-11-29 11:34:05 +01003549 return did_set_option_listflag(*varp, (char_u *)SHM_ALL, args->os_errbuf,
3550 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003551}
3552
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003553 int
3554expand_set_shortmess(optexpand_T *args, int *numMatches, char_u ***matches)
3555{
3556 return expand_set_opt_listflag(args, (char_u*)SHM_ALL, numMatches, matches);
3557}
3558
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003559#if defined(FEAT_LINEBREAK) || defined(PROTO)
3560/*
3561 * The 'showbreak' option is changed.
3562 */
3563 char *
3564did_set_showbreak(optset_T *args)
3565{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003566 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003567 char_u *s;
3568
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003569 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003570 {
3571 if (ptr2cells(s) != 1)
3572 return e_showbreak_contains_unprintable_or_wide_character;
3573 MB_PTR_ADV(s);
3574 }
3575
3576 return NULL;
3577}
3578#endif
3579
3580/*
3581 * The 'showcmdloc' option is changed.
3582 */
3583 char *
3584did_set_showcmdloc(optset_T *args UNUSED)
3585{
zeertzjqc27fcf42024-03-01 23:01:43 +01003586 char *errmsg = did_set_opt_strings(p_sloc, p_sloc_values, FALSE);
3587
3588 if (errmsg == NULL)
3589 comp_col();
3590
3591 return errmsg;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003592}
3593
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003594 int
3595expand_set_showcmdloc(optexpand_T *args, int *numMatches, char_u ***matches)
3596{
3597 return expand_set_opt_string(
3598 args,
3599 p_sloc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003600 ARRAY_LENGTH(p_sloc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003601 numMatches,
3602 matches);
3603}
3604
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003605#if defined(FEAT_SIGNS) || defined(PROTO)
3606/*
3607 * The 'signcolumn' option is changed.
3608 */
3609 char *
3610did_set_signcolumn(optset_T *args)
3611{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003612 char_u **varp = (char_u **)args->os_varp;
3613
3614 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003615 return e_invalid_argument;
3616 // When changing the 'signcolumn' to or from 'number', recompute the
3617 // width of the number column if 'number' or 'relativenumber' is set.
3618 if (((*args->os_oldval.string == 'n' && args->os_oldval.string[1] == 'u')
3619 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
3620 && (curwin->w_p_nu || curwin->w_p_rnu))
3621 curwin->w_nrwidth_line_count = 0;
3622
3623 return NULL;
3624}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003625
3626 int
3627expand_set_signcolumn(optexpand_T *args, int *numMatches, char_u ***matches)
3628{
3629 return expand_set_opt_string(
3630 args,
3631 p_scl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003632 ARRAY_LENGTH(p_scl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003633 numMatches,
3634 matches);
3635}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003636#endif
3637
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003638#if defined(FEAT_SPELL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003639/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003640 * The 'spellcapcheck' option is changed.
3641 */
3642 char *
3643did_set_spellcapcheck(optset_T *args UNUSED)
3644{
3645 // compile the regexp program.
3646 return compile_cap_prog(curwin->w_s);
3647}
3648
3649/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003650 * The 'spellfile' option is changed.
3651 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003652 char *
3653did_set_spellfile(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003654{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003655 char_u **varp = (char_u **)args->os_varp;
3656
3657 if (!valid_spellfile(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003658 return e_invalid_argument;
3659
3660 // If there is a window for this buffer in which 'spell' is set load the
3661 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003662 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003663}
3664
3665/*
3666 * The 'spell' option is changed.
3667 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003668 char *
3669did_set_spelllang(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003670{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003671 char_u **varp = (char_u **)args->os_varp;
3672
3673 if (!valid_spelllang(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003674 return e_invalid_argument;
3675
3676 // If there is a window for this buffer in which 'spell' is set load the
3677 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003678 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003679}
3680
3681/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003682 * The 'spelloptions' option is changed.
3683 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003684 char *
3685did_set_spelloptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003686{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003687 char_u **varp = (char_u **)args->os_varp;
3688
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003689 if (**varp != NUL && STRCMP(*varp, "camel") != 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003690 return e_invalid_argument;
3691
3692 return NULL;
3693}
3694
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003695 int
3696expand_set_spelloptions(optexpand_T *args, int *numMatches, char_u ***matches)
3697{
3698 static char *(p_spo_values[]) = {"camel", NULL};
3699 return expand_set_opt_string(
3700 args,
3701 p_spo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003702 ARRAY_LENGTH(p_spo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003703 numMatches,
3704 matches);
3705}
3706
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003707/*
3708 * The 'spellsuggest' option is changed.
3709 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003710 char *
3711did_set_spellsuggest(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003712{
3713 if (spell_check_sps() != OK)
3714 return e_invalid_argument;
3715
3716 return NULL;
3717}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003718
3719 int
3720expand_set_spellsuggest(optexpand_T *args, int *numMatches, char_u ***matches)
3721{
3722 return expand_set_opt_string(
3723 args,
3724 p_sps_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003725 ARRAY_LENGTH(p_sps_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003726 numMatches,
3727 matches);
3728}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003729#endif
3730
3731/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003732 * The 'splitkeep' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003733 */
3734 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003735did_set_splitkeep(optset_T *args UNUSED)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003736{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003737 return did_set_opt_strings(p_spk, p_spk_values, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003738}
3739
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003740 int
3741expand_set_splitkeep(optexpand_T *args, int *numMatches, char_u ***matches)
3742{
3743 return expand_set_opt_string(
3744 args,
3745 p_spk_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003746 ARRAY_LENGTH(p_spk_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003747 numMatches,
3748 matches);
3749}
3750
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00003751#if defined(FEAT_STL_OPT) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003752/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003753 * The 'statusline' option is changed.
3754 */
3755 char *
3756did_set_statusline(optset_T *args)
3757{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003758 return parse_statustabline_rulerformat(args, FALSE);
3759}
3760#endif
3761
3762/*
3763 * The 'swapsync' option is changed.
3764 */
3765 char *
3766did_set_swapsync(optset_T *args UNUSED)
3767{
3768 return did_set_opt_strings(p_sws, p_sws_values, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003769}
3770
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003771 int
3772expand_set_swapsync(optexpand_T *args, int *numMatches, char_u ***matches)
3773{
3774 return expand_set_opt_string(
3775 args,
3776 p_sws_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003777 ARRAY_LENGTH(p_sws_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003778 numMatches,
3779 matches);
3780}
3781
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003782/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003783 * The 'switchbuf' option is changed.
3784 */
3785 char *
3786did_set_switchbuf(optset_T *args UNUSED)
3787{
3788 return did_set_opt_flags(p_swb, p_swb_values, &swb_flags, TRUE);
3789}
3790
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003791 int
3792expand_set_switchbuf(optexpand_T *args, int *numMatches, char_u ***matches)
3793{
3794 return expand_set_opt_string(
3795 args,
3796 p_swb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003797 ARRAY_LENGTH(p_swb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003798 numMatches,
3799 matches);
3800}
3801
LemonBoy5247b0b2024-07-12 19:30:58 +02003802/*
3803 * The 'tabclose' option is changed.
3804 */
3805 char *
3806did_set_tabclose(optset_T *args UNUSED)
3807{
3808 return did_set_opt_flags(p_tcl, p_tcl_values, &tcl_flags, TRUE);
3809}
3810
3811 int
3812expand_set_tabclose(optexpand_T *args, int *numMatches, char_u ***matches)
3813{
3814 return expand_set_opt_string(
3815 args,
3816 p_tcl_values,
3817 ARRAY_LENGTH(p_tcl_values) - 1,
3818 numMatches,
3819 matches);
3820}
3821
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003822#if defined(FEAT_STL_OPT) || defined(PROTO)
3823/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003824 * The 'tabline' option is changed.
3825 */
3826 char *
3827did_set_tabline(optset_T *args)
3828{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003829 return parse_statustabline_rulerformat(args, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003830}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003831#endif
3832
3833/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003834 * The 'tagcase' option is changed.
3835 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003836 char *
3837did_set_tagcase(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003838{
3839 unsigned int *flags;
3840 char_u *p;
3841
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003842 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003843 {
3844 p = curbuf->b_p_tc;
3845 flags = &curbuf->b_tc_flags;
3846 }
3847 else
3848 {
3849 p = p_tc;
3850 flags = &tc_flags;
3851 }
3852
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003853 if ((args->os_flags & OPT_LOCAL) && *p == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003854 // make the local value empty: use the global value
3855 *flags = 0;
3856 else if (*p == NUL
3857 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
3858 return e_invalid_argument;
3859
3860 return NULL;
3861}
3862
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003863 int
3864expand_set_tagcase(optexpand_T *args, int *numMatches, char_u ***matches)
3865{
3866 return expand_set_opt_string(
3867 args,
3868 p_tc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003869 ARRAY_LENGTH(p_tc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003870 numMatches,
3871 matches);
3872}
3873
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003874/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003875 * The 'term' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003876 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003877 char *
3878did_set_term(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003879{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003880 if (T_NAME[0] == NUL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003881 return e_cannot_set_term_to_empty_string;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003882#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003883 if (gui.in_use)
3884 return e_cannot_change_term_in_GUI;
3885 if (term_is_gui(T_NAME))
3886 return e_use_gui_to_start_GUI;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003887#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003888 if (set_termname(T_NAME) == FAIL)
3889 return e_not_found_in_termcap;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003890
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003891 // Screen colors may have changed.
3892 redraw_later_clear();
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003893
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003894 return NULL;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003895}
3896
3897/*
3898 * Some terminal option (t_xxx) is changed
3899 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003900 char *
3901did_set_term_option(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003902{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003903 char_u **varp = (char_u **)args->os_varp;
3904
3905 if (!full_screen)
3906 return NULL;
3907
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003908 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
3909 if (varp == &T_CCO)
3910 {
3911 int colors = atoi((char *)T_CCO);
3912
3913 // Only reinitialize colors if t_Co value has really changed to
3914 // avoid expensive reload of colorscheme if t_Co is set to the
3915 // same value multiple times.
3916 if (colors != t_colors)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003917 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003918 t_colors = colors;
3919 if (t_colors <= 1)
3920 {
3921 vim_free(T_CCO);
3922 T_CCO = empty_option;
3923 }
3924#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3925 if (is_term_win32())
3926 {
3927 swap_tcap();
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003928 args->os_did_swaptcap = TRUE;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003929 }
3930#endif
3931 // We now have a different color setup, initialize it again.
3932 init_highlight(TRUE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003933 }
3934 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003935 ttest(FALSE);
3936 if (varp == &T_ME)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003937 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003938 out_str(T_ME);
3939 redraw_later(UPD_CLEAR);
3940#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3941 // Since t_me has been set, this probably means that the user
3942 // wants to use this as default colors. Need to reset default
3943 // background/foreground colors.
3944# ifdef VIMDLL
3945 if (!gui.in_use && !gui.starting)
3946# endif
3947 mch_set_normal_colors();
3948#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003949 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003950 if (varp == &T_BE && termcap_active)
3951 {
3952 MAY_WANT_TO_LOG_THIS;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003953
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003954 if (*T_BE == NUL)
3955 // When clearing t_BE we assume the user no longer wants
3956 // bracketed paste, thus disable it by writing t_BD.
3957 out_str(T_BD);
3958 else
3959 out_str(T_BE);
3960 }
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003961
3962 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003963}
3964
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003965#if defined(FEAT_TERMINAL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003966/*
3967 * The 'termwinkey' option is changed.
3968 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003969 char *
Milly94606f72024-10-22 22:07:52 +02003970did_set_termwinkey(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003971{
Milly94606f72024-10-22 22:07:52 +02003972 char_u **varp = (char_u **)args->os_varp;
3973
3974 if ((*varp)[0] != NUL && string_to_key(*varp, TRUE) == 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003975 return e_invalid_argument;
3976
3977 return NULL;
3978}
3979
3980/*
3981 * The 'termwinsize' option is changed.
3982 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003983 char *
Milly8be10aa2024-10-22 22:01:46 +02003984did_set_termwinsize(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003985{
Milly8be10aa2024-10-22 22:01:46 +02003986 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003987 char_u *p;
3988
Milly8be10aa2024-10-22 22:01:46 +02003989 if ((*varp)[0] == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003990 return NULL;
3991
Milly8be10aa2024-10-22 22:01:46 +02003992 p = skipdigits(*varp);
3993 if (p == *varp || (*p != 'x' && *p != '*') || *skipdigits(p + 1) != NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003994 return e_invalid_argument;
3995
3996 return NULL;
3997}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003998
3999# if defined(MSWIN) || defined(PROTO)
4000/*
4001 * The 'termwintype' option is changed.
4002 */
4003 char *
4004did_set_termwintype(optset_T *args UNUSED)
4005{
4006 return did_set_opt_strings(p_twt, p_twt_values, FALSE);
4007}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004008
4009 int
4010expand_set_termwintype(optexpand_T *args, int *numMatches, char_u ***matches)
4011{
4012 return expand_set_opt_string(
4013 args,
4014 p_twt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004015 ARRAY_LENGTH(p_twt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004016 numMatches,
4017 matches);
4018}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00004019# endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004020#endif
4021
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004022/*
4023 * The 'titlestring' option is changed.
4024 */
4025 char *
4026did_set_titlestring(optset_T *args)
4027{
4028 int flagval = 0;
4029
4030#ifdef FEAT_STL_OPT
4031 flagval = STL_IN_TITLE;
4032#endif
4033 return parse_titleiconstring(args, flagval);
4034}
4035
4036#if (defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)) || defined(PROTO)
4037/*
4038 * The 'toolbar' option is changed.
4039 */
4040 char *
4041did_set_toolbar(optset_T *args UNUSED)
4042{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004043 if (opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags,
4044 TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004045 return e_invalid_argument;
4046
4047 out_flush();
4048 gui_mch_show_toolbar((toolbar_flags &
4049 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4050 return NULL;
4051}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004052
4053 int
4054expand_set_toolbar(optexpand_T *args, int *numMatches, char_u ***matches)
4055{
4056 return expand_set_opt_string(
4057 args,
4058 p_toolbar_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004059 ARRAY_LENGTH(p_toolbar_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004060 numMatches,
4061 matches);
4062}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004063#endif
4064
4065#if (defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)) || defined(PROTO)
4066/*
4067 * The 'toolbariconsize' option is changed. GTK+ 2 only.
4068 */
4069 char *
4070did_set_toolbariconsize(optset_T *args UNUSED)
4071{
4072 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
4073 return e_invalid_argument;
4074
4075 out_flush();
4076 gui_mch_show_toolbar((toolbar_flags &
4077 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4078 return NULL;
4079}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004080
4081 int
4082expand_set_toolbariconsize(optexpand_T *args, int *numMatches, char_u ***matches)
4083{
4084 return expand_set_opt_string(
4085 args,
4086 p_tbis_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004087 ARRAY_LENGTH(p_tbis_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004088 numMatches,
4089 matches);
4090}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004091#endif
4092
4093#if defined(UNIX) || defined(VMS) || defined(PROTO)
4094/*
4095 * The 'ttymouse' option is changed.
4096 */
4097 char *
4098did_set_ttymouse(optset_T *args UNUSED)
4099{
4100 char *errmsg = NULL;
4101
4102 // Switch the mouse off before changing the escape sequences used for
4103 // that.
4104 mch_setmouse(FALSE);
4105 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
4106 errmsg = e_invalid_argument;
4107 else
4108 check_mouse_termcode();
4109 if (termcap_active)
4110 setmouse(); // may switch it on again
4111
4112 return errmsg;
4113}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004114
4115 int
4116expand_set_ttymouse(optexpand_T *args, int *numMatches, char_u ***matches)
4117{
4118 return expand_set_opt_string(
4119 args,
4120 p_ttym_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004121 ARRAY_LENGTH(p_ttym_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004122 numMatches,
4123 matches);
4124}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004125#endif
4126
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004127#if defined(FEAT_VARTABS) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004128/*
4129 * The 'varsofttabstop' option is changed.
4130 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004131 char *
4132did_set_varsofttabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004133{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004134 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004135 char_u *cp;
4136
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004137 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004138 VIM_CLEAR(curbuf->b_p_vsts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004139 else
4140 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004141 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004142 {
4143 if (vim_isdigit(*cp))
4144 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004145 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004146 continue;
4147 return e_invalid_argument;
4148 }
4149
4150 int *oldarray = curbuf->b_p_vsts_array;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004151 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004152 {
4153 if (oldarray)
4154 vim_free(oldarray);
4155 }
4156 else
4157 return e_invalid_argument;
4158 }
4159
4160 return NULL;
4161}
4162
4163/*
4164 * The 'vartabstop' option is changed.
4165 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004166 char *
4167did_set_vartabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004168{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004169 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004170 char_u *cp;
4171
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004172 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004173 VIM_CLEAR(curbuf->b_p_vts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004174 else
4175 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004176 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004177 {
4178 if (vim_isdigit(*cp))
4179 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004180 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004181 continue;
4182 return e_invalid_argument;
4183 }
4184
4185 int *oldarray = curbuf->b_p_vts_array;
4186
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004187 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004188 {
4189 vim_free(oldarray);
4190# ifdef FEAT_FOLDING
4191 if (foldmethodIsIndent(curwin))
4192 foldUpdateAll(curwin);
4193# endif
4194 }
4195 else
4196 return e_invalid_argument;
4197 }
4198
4199 return NULL;
4200}
4201#endif
4202
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004203/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004204 * The 'verbosefile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004205 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004206 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004207did_set_verbosefile(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004208{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004209 verbose_stop();
4210 if (*p_vfile != NUL && verbose_open() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004211 return e_invalid_argument;
4212
4213 return NULL;
4214}
4215
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004216#if defined(FEAT_SESSION) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004217/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004218 * The 'viewoptions' 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_viewoptions(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004222{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004223 return did_set_opt_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004224}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004225#endif
4226
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004227#if defined(FEAT_VIMINFO) || defined(PROTO)
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004228/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004229 * The 'viminfo' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004230 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004231 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004232did_set_viminfo(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004233{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004234 char_u *s;
4235 char *errmsg = NULL;
4236
4237 for (s = p_viminfo; *s;)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004238 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004239 // Check it's a valid character
4240 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
4241 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01004242 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004243 break;
4244 }
4245 if (*s == 'n') // name is always last one
4246 break;
4247 else if (*s == 'r') // skip until next ','
4248 {
4249 while (*++s && *s != ',')
4250 ;
4251 }
4252 else if (*s == '%')
4253 {
4254 // optional number
4255 while (vim_isdigit(*++s))
4256 ;
4257 }
4258 else if (*s == '!' || *s == 'h' || *s == 'c')
4259 ++s; // no extra chars
4260 else // must have a number
4261 {
4262 while (vim_isdigit(*++s))
4263 ;
4264
4265 if (!VIM_ISDIGIT(*(s - 1)))
4266 {
4267 if (args->os_errbuf != NULL)
4268 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01004269 vim_snprintf(args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004270 _(e_missing_number_after_angle_str_angle),
4271 transchar_byte(*(s - 1)));
4272 errmsg = args->os_errbuf;
4273 }
4274 else
4275 errmsg = "";
4276 break;
4277 }
4278 }
4279 if (*s == ',')
4280 ++s;
4281 else if (*s)
4282 {
4283 if (args->os_errbuf != NULL)
4284 errmsg = e_missing_comma;
4285 else
4286 errmsg = "";
4287 break;
4288 }
4289 }
4290 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
4291 errmsg = e_must_specify_a_value;
4292
4293 return errmsg;
4294}
4295#endif
4296
4297/*
4298 * The 'virtualedit' option is changed.
4299 */
4300 char *
4301did_set_virtualedit(optset_T *args)
4302{
4303 char_u *ve = p_ve;
4304 unsigned int *flags = &ve_flags;
4305
4306 if (args->os_flags & OPT_LOCAL)
4307 {
4308 ve = curwin->w_p_ve;
4309 flags = &curwin->w_ve_flags;
4310 }
4311
4312 if ((args->os_flags & OPT_LOCAL) && *ve == NUL)
4313 // make the local value empty: use the global value
4314 *flags = 0;
4315 else
4316 {
4317 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
4318 return e_invalid_argument;
4319 else if (STRCMP(ve, args->os_oldval.string) != 0)
4320 {
4321 // Recompute cursor position in case the new 've' setting
4322 // changes something.
4323 validate_virtcol();
4324 coladvance(curwin->w_virtcol);
4325 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004326 }
4327
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004328 return NULL;
4329}
4330
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004331 int
4332expand_set_virtualedit(optexpand_T *args, int *numMatches, char_u ***matches)
4333{
4334 return expand_set_opt_string(
4335 args,
4336 p_ve_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004337 ARRAY_LENGTH(p_ve_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004338 numMatches,
4339 matches);
4340}
4341
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004342/*
4343 * The 'whichwrap' option is changed.
4344 */
4345 char *
4346did_set_whichwrap(optset_T *args)
4347{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004348 char_u **varp = (char_u **)args->os_varp;
4349
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004350 // Add ',' to the list flags because 'whichwrap' is a flag
4351 // list that is comma-separated.
Christian Brabandtb39b2402023-11-29 11:34:05 +01004352 return did_set_option_listflag(*varp, (char_u *)(WW_ALL ","),
4353 args->os_errbuf, args->os_errbuflen);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004354}
4355
4356 int
4357expand_set_whichwrap(optexpand_T *args, int *numMatches, char_u ***matches)
4358{
4359 return expand_set_opt_listflag(args, (char_u*)WW_ALL, numMatches, matches);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004360}
4361
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004362/*
4363 * The 'wildmode' option is changed.
4364 */
4365 char *
4366did_set_wildmode(optset_T *args UNUSED)
4367{
4368 if (check_opt_wim() == FAIL)
4369 return e_invalid_argument;
4370 return NULL;
4371}
4372
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004373 int
4374expand_set_wildmode(optexpand_T *args, int *numMatches, char_u ***matches)
4375{
4376 return expand_set_opt_string(
4377 args,
4378 p_wim_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004379 ARRAY_LENGTH(p_wim_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004380 numMatches,
4381 matches);
4382}
4383
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004384/*
4385 * The 'wildoptions' option is changed.
4386 */
4387 char *
4388did_set_wildoptions(optset_T *args UNUSED)
4389{
4390 return did_set_opt_strings(p_wop, p_wop_values, TRUE);
4391}
4392
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004393 int
4394expand_set_wildoptions(optexpand_T *args, int *numMatches, char_u ***matches)
4395{
4396 return expand_set_opt_string(
4397 args,
4398 p_wop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004399 ARRAY_LENGTH(p_wop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004400 numMatches,
4401 matches);
4402}
4403
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004404#if defined(FEAT_WAK) || defined(PROTO)
4405/*
4406 * The 'winaltkeys' option is changed.
4407 */
4408 char *
4409did_set_winaltkeys(optset_T *args UNUSED)
4410{
4411 char *errmsg = NULL;
4412
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004413 if (*p_wak == NUL || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004414 errmsg = e_invalid_argument;
4415# ifdef FEAT_MENU
4416# if defined(FEAT_GUI_MOTIF)
4417 else if (gui.in_use)
4418 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4419# elif defined(FEAT_GUI_GTK)
4420 else if (gui.in_use)
4421 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4422# endif
4423# endif
4424 return errmsg;
4425}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004426
4427 int
4428expand_set_winaltkeys(optexpand_T *args, int *numMatches, char_u ***matches)
4429{
4430 return expand_set_opt_string(
4431 args,
4432 p_wak_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004433 ARRAY_LENGTH(p_wak_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004434 numMatches,
4435 matches);
4436}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004437#endif
4438
4439/*
4440 * The 'wincolor' option is changed.
4441 */
4442 char *
4443did_set_wincolor(optset_T *args UNUSED)
4444{
4445#ifdef FEAT_TERMINAL
4446 term_update_wincolor(curwin);
4447#endif
4448 return NULL;
4449}
4450
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004451 int
4452expand_set_wincolor(optexpand_T *args, int *numMatches, char_u ***matches)
4453{
4454 return expand_set_opt_generic(
4455 args,
4456 get_highlight_name,
4457 numMatches,
4458 matches);
4459}
4460
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004461#ifdef FEAT_SYN_HL
4462/*
4463 * When the 'syntax' option is set, load the syntax of that name.
4464 */
4465 static void
4466do_syntax_autocmd(int value_changed)
4467{
4468 static int syn_recursive = 0;
4469
4470 ++syn_recursive;
4471 // Only pass TRUE for "force" when the value changed or not used
4472 // recursively, to avoid endless recurrence.
4473 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
4474 value_changed || syn_recursive == 1, curbuf);
4475 curbuf->b_flags |= BF_SYN_SET;
4476 --syn_recursive;
4477}
4478#endif
4479
4480/*
4481 * When the 'filetype' option is set, trigger the FileType autocommand.
4482 */
4483 static void
4484do_filetype_autocmd(char_u **varp, int opt_flags, int value_changed)
4485{
4486 // Skip this when called from a modeline and the filetype was already set
4487 // to this value.
4488 if ((opt_flags & OPT_MODELINE) && !value_changed)
4489 return;
4490
4491 static int ft_recursive = 0;
4492 int secure_save = secure;
4493
4494 // Reset the secure flag, since the value of 'filetype' has
4495 // been checked to be safe.
4496 secure = 0;
4497
4498 ++ft_recursive;
zeertzjq5bf6c212024-03-31 18:41:27 +02004499 curbuf->b_did_filetype = TRUE;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004500 // Only pass TRUE for "force" when the value changed or not
4501 // used recursively, to avoid endless recurrence.
4502 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
4503 value_changed || ft_recursive == 1, curbuf);
4504 --ft_recursive;
4505 // Just in case the old "curbuf" is now invalid.
4506 if (varp != &(curbuf->b_p_ft))
4507 varp = NULL;
4508
4509 secure = secure_save;
4510}
4511
4512#ifdef FEAT_SPELL
4513/*
4514 * When the 'spelllang' option is set, source the spell/LANG.vim file in
4515 * 'runtimepath'.
4516 */
4517 static void
4518do_spelllang_source(void)
4519{
4520 char_u fname[200];
4521 char_u *p;
4522 char_u *q = curwin->w_s->b_p_spl;
4523
4524 // Skip the first name if it is "cjk".
4525 if (STRNCMP(q, "cjk,", 4) == 0)
4526 q += 4;
4527
4528 // They could set 'spellcapcheck' depending on the language. Use the first
4529 // name in 'spelllang' up to '_region' or '.encoding'.
4530 for (p = q; *p != NUL; ++p)
4531 if (!ASCII_ISALNUM(*p) && *p != '-')
4532 break;
4533 if (p > q)
4534 {
4535 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
4536 (int)(p - q), q);
4537 source_runtime(fname, DIP_ALL);
4538 }
4539}
4540#endif
4541
4542/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004543 * Handle string options that need some action to perform when changed.
zeertzjqf6782732022-07-27 18:26:03 +01004544 * The new value must be allocated.
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004545 * Returns NULL for success, or an untranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +02004546 */
4547 char *
4548did_set_string_option(
4549 int opt_idx, // index in options[] table
4550 char_u **varp, // pointer to the option variable
Bram Moolenaardac13472019-09-16 21:06:21 +02004551 char_u *oldval, // previous value of the option
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004552 char_u *value, // new value of the option
Bram Moolenaardac13472019-09-16 21:06:21 +02004553 char *errbuf, // buffer for errors, or NULL
Mike Williams620f0112023-12-05 15:36:06 +01004554 size_t errbuflen, // length of error buffer
Bram Moolenaardac13472019-09-16 21:06:21 +02004555 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004556 set_op_T op, // OP_ADDING/OP_PREPENDING/OP_REMOVING
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004557 int *value_checked) // value was checked to be safe, no
Bram Moolenaardac13472019-09-16 21:06:21 +02004558 // need to set P_INSECURE
4559{
4560 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +02004561 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004562 opt_did_set_cb_T did_set_cb = get_option_did_set_cb(opt_idx);
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004563 optset_T args;
4564
4565 // 'ttytype' is an alias for 'term'. Both 'term' and 'ttytype' point to
4566 // T_NAME. If 'term' or 'ttytype' is modified, then use the index for the
4567 // 'term' option. Only set the P_ALLOCED flag on 'term'.
4568 if (varp == &T_NAME)
4569 {
4570 opt_idx = findoption((char_u *)"term");
4571 if (opt_idx >= 0)
4572 {
4573 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
4574 did_set_cb = get_option_did_set_cb(opt_idx);
4575 }
4576 }
4577
4578 CLEAR_FIELD(args);
Bram Moolenaardac13472019-09-16 21:06:21 +02004579
Bram Moolenaardac13472019-09-16 21:06:21 +02004580 // Disallow changing some options from secure mode
4581 if ((secure
4582#ifdef HAVE_SANDBOX
4583 || sandbox != 0
4584#endif
4585 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00004586 errmsg = e_not_allowed_here;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004587 // Check for a "normal" directory or file name in some options.
4588 else if (check_illegal_path_names(opt_idx, varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004589 errmsg = e_invalid_argument;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004590 else if (did_set_cb != NULL)
4591 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004592 args.os_varp = (char_u *)varp;
4593 args.os_idx = opt_idx;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004594 args.os_flags = opt_flags;
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004595 args.os_op = op;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004596 args.os_oldval.string = oldval;
4597 args.os_newval.string = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004598 args.os_errbuf = errbuf;
Christian Brabandtb39b2402023-11-29 11:34:05 +01004599 args.os_errbuflen = errbuflen;
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004600 // Invoke the option specific callback function to validate and apply
4601 // the new option value.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004602 errmsg = did_set_cb(&args);
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004603
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004604 // The 'keymap', 'filetype' and 'syntax' option callback functions
4605 // may change the os_value_checked field.
4606 *value_checked = args.os_value_checked;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004607 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004608
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004609 // If an error is detected, restore the previous value.
Bram Moolenaardac13472019-09-16 21:06:21 +02004610 if (errmsg != NULL)
4611 {
zeertzjqf6782732022-07-27 18:26:03 +01004612 free_string_option(*varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02004613 *varp = oldval;
4614 // When resetting some values, need to act on it.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004615 if (args.os_restore_chartab)
Bram Moolenaardac13472019-09-16 21:06:21 +02004616 (void)init_chartab();
4617 if (varp == &p_hl)
4618 (void)highlight_changed();
4619 }
4620 else
4621 {
4622#ifdef FEAT_EVAL
4623 // Remember where the option was set.
4624 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4625#endif
4626 // Free string options that are in allocated memory.
4627 // Use "free_oldval", because recursiveness may change the flags under
4628 // our fingers (esp. init_highlight()).
4629 if (free_oldval)
4630 free_string_option(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01004631 set_option_flag(opt_idx, P_ALLOCED);
Bram Moolenaardac13472019-09-16 21:06:21 +02004632
4633 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
4634 && is_global_local_option(opt_idx))
4635 {
4636 // global option with local value set to use global value; free
4637 // the local value and make it empty
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004638 char_u *p = get_option_varp_scope(opt_idx, OPT_LOCAL);
Bram Moolenaardac13472019-09-16 21:06:21 +02004639 free_string_option(*(char_u **)p);
4640 *(char_u **)p = empty_option;
4641 }
4642
4643 // May set global value for local option.
4644 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
4645 set_string_option_global(opt_idx, varp);
4646
4647 // Trigger the autocommand only after setting the flags.
4648#ifdef FEAT_SYN_HL
Bram Moolenaardac13472019-09-16 21:06:21 +02004649 if (varp == &(curbuf->b_p_syn))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004650 do_syntax_autocmd(args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004651#endif
4652 else if (varp == &(curbuf->b_p_ft))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004653 do_filetype_autocmd(varp, opt_flags, args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004654#ifdef FEAT_SPELL
4655 if (varp == &(curwin->w_s->b_p_spl))
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004656 do_spelllang_source();
Bram Moolenaardac13472019-09-16 21:06:21 +02004657#endif
4658 }
4659
Bram Moolenaardac13472019-09-16 21:06:21 +02004660 if (varp == &p_mouse)
4661 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004662 if (*p_mouse == NUL)
4663 mch_setmouse(FALSE); // switch mouse off
4664 else
Bram Moolenaardac13472019-09-16 21:06:21 +02004665 setmouse(); // in case 'mouse' changed
4666 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004667
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004668 if (varp == &p_rtp)
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004669 {
4670 export_myvimdir();
4671#if defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004672 update_package_paths_in_lua();
4673#endif
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004674 }
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004675
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004676#if defined(FEAT_LINEBREAK)
4677 // Changing Formatlistpattern when briopt includes the list setting:
4678 // redraw
4679 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
4680 && curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004681 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004682#endif
4683
Bram Moolenaardac13472019-09-16 21:06:21 +02004684 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004685 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0
4686 && (get_option_flags(opt_idx) & P_HLONLY) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02004687 curwin->w_set_curswant = TRUE;
4688
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004689 if ((opt_flags & OPT_NO_REDRAW) == 0)
4690 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004691#ifdef FEAT_GUI
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004692 // set when changing an option that only requires a redraw in the GUI
4693 int redraw_gui_only = FALSE;
4694
4695 if (varp == &p_go // 'guioptions'
4696 || varp == &p_guifont // 'guifont'
4697# ifdef FEAT_GUI_TABLINE
4698 || varp == &p_gtl // 'guitablabel'
4699 || varp == &p_gtt // 'guitabtooltip'
4700# endif
4701# ifdef FEAT_XFONTSET
4702 || varp == &p_guifontset // 'guifontset'
4703# endif
4704 || varp == &p_guifontwide // 'guifontwide'
Erik S. V. Jansson2f026382024-02-26 22:23:05 +01004705# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004706 || varp == &p_guiligatures // 'guiligatures'
4707# endif
4708 )
4709 redraw_gui_only = TRUE;
4710
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004711 // check redraw when it's not a GUI option or the GUI is active.
4712 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02004713#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004714 check_redraw(get_option_flags(opt_idx));
4715 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004716
4717#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004718 if (args.os_did_swaptcap)
Bram Moolenaardac13472019-09-16 21:06:21 +02004719 {
4720 set_termname((char_u *)"win32");
4721 init_highlight(TRUE, FALSE);
4722 }
4723#endif
4724
4725 return errmsg;
4726}
4727
4728/*
4729 * Check an option that can be a range of string values.
4730 *
4731 * Return OK for correct value, FAIL otherwise.
4732 * Empty is always OK.
4733 */
4734 static int
4735check_opt_strings(
4736 char_u *val,
4737 char **values,
4738 int list) // when TRUE: accept a list of values
4739{
4740 return opt_strings_flags(val, values, NULL, list);
4741}
4742
4743/*
4744 * Handle an option that can be a range of string values.
4745 * Set a flag in "*flagp" for each string present.
4746 *
4747 * Return OK for correct value, FAIL otherwise.
4748 * Empty is always OK.
4749 */
4750 static int
4751opt_strings_flags(
4752 char_u *val, // new value
4753 char **values, // array of valid string values
4754 unsigned *flagp,
4755 int list) // when TRUE: accept a list of values
4756{
4757 int i;
4758 int len;
4759 unsigned new_flags = 0;
4760
4761 while (*val)
4762 {
4763 for (i = 0; ; ++i)
4764 {
4765 if (values[i] == NULL) // val not found in values[]
4766 return FAIL;
4767
4768 len = (int)STRLEN(values[i]);
4769 if (STRNCMP(values[i], val, len) == 0
4770 && ((list && val[len] == ',') || val[len] == NUL))
4771 {
4772 val += len + (val[len] == ',');
4773 new_flags |= (1 << i);
4774 break; // check next item in val list
4775 }
4776 }
4777 }
4778 if (flagp != NULL)
4779 *flagp = new_flags;
4780
4781 return OK;
4782}
4783
4784/*
4785 * return OK if "p" is a valid fileformat name, FAIL otherwise.
4786 */
4787 int
4788check_ff_value(char_u *p)
4789{
4790 return check_opt_strings(p, p_ff_values, FALSE);
4791}
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004792
4793/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004794 * Save the actual shortmess Flags and clear them temporarily to avoid that
4795 * file messages overwrites any output from the following commands.
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004796 *
4797 * Caller must make sure to first call save_clear_shm_value() and then
4798 * restore_shm_value() exactly the same number of times.
4799 */
4800 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004801save_clear_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004802{
4803 if (STRLEN(p_shm) >= SHM_LEN)
4804 {
4805 iemsg(e_internal_error_shortmess_too_long);
4806 return;
4807 }
4808
4809 if (++set_shm_recursive == 1)
4810 {
4811 STRCPY(shm_buf, p_shm);
4812 set_option_value_give_err((char_u *)"shm", 0L, (char_u *)"", 0);
4813 }
4814}
4815
4816/*
4817 * Restore the shortmess Flags set from the save_clear_shm_value() function.
4818 */
4819 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004820restore_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004821{
4822 if (--set_shm_recursive == 0)
4823 {
4824 set_option_value_give_err((char_u *)"shm", 0L, shm_buf, 0);
4825 vim_memset(shm_buf, 0, SHM_LEN);
4826 }
4827}
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004828
4829/*
4830 * Export the environment variable $MYVIMDIR to the first item in runtimepath
4831 */
Christian Brabandt1a741d32025-03-01 16:30:33 +01004832 void
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004833export_myvimdir()
4834{
4835 int dofree = FALSE;
4836 char_u *p;
4837 char_u *q = p_rtp;
4838 char_u *buf = alloc(MAXPATHL);
4839
4840 if (buf == NULL)
4841 return;
4842
4843 (void)copy_option_part(&q, buf, MAXPATHL, ",");
4844
4845 p = vim_getenv((char_u *)"MYVIMDIR", &dofree);
4846
4847 if (p == NULL || STRCMP(p, buf) != 0)
4848 {
4849 add_pathsep(buf);
4850#ifdef MSWIN
4851 // normalize path separators
4852 for (q = buf; *q != NUL; q++)
4853 if (*q == '/')
4854 *q = '\\';
4855#endif
4856 vim_setenv((char_u *)"MYVIMDIR", buf);
4857 }
4858 if (dofree)
4859 vim_free(p);
4860 vim_free(buf);
4861}