blob: 02806b66a6394fc95ceedd8922e292761ccd22ac [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()
97static char *(p_wim_values[]) = {"full", "longest", "list", "lastused", 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
glepniredd4ac32025-01-29 18:53:51 +0100123static char *(p_cot_values[]) = {"menu", "menuone", "longest", "preview", "popup", "popuphidden", "noinsert", "noselect", "fuzzy", "nosort", "preinsert", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200124#ifdef BACKSLASH_IN_FILENAME
125static char *(p_csl_values[]) = {"slash", "backslash", NULL};
126#endif
127#ifdef FEAT_SIGNS
128static char *(p_scl_values[]) = {"yes", "no", "auto", "number", NULL};
129#endif
130#if defined(MSWIN) && defined(FEAT_TERMINAL)
131static char *(p_twt_values[]) = {"winpty", "conpty", "", NULL};
132#endif
Luuk van Baalba936f62022-12-15 13:15:39 +0000133static char *(p_sloc_values[]) = {"last", "statusline", "tabline", NULL};
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +0000134static char *(p_sws_values[]) = {"fsync", "sync", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200135
136static int check_opt_strings(char_u *val, char **values, int list);
137static int opt_strings_flags(char_u *val, char **values, unsigned *flagp, int list);
Christian Brabandt3e2affc2025-02-28 17:34:46 +0100138static void export_myvimdir(void);
Bram Moolenaardac13472019-09-16 21:06:21 +0200139
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);
zeertzjq529b9ad2024-06-05 20:27:06 +0200150 (void)opt_strings_flags(p_cot, p_cot_values, &cot_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200151#ifdef FEAT_SESSION
152 (void)opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE);
153 (void)opt_strings_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
154#endif
155#ifdef FEAT_FOLDING
156 (void)opt_strings_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
157#endif
158 (void)opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE);
Yegappan Lakshmanan87018252023-09-20 20:20:04 +0200159 (void)opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200160 (void)opt_strings_flags(p_tc, p_tc_values, &tc_flags, FALSE);
161 (void)opt_strings_flags(p_ve, p_ve_values, &ve_flags, TRUE);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +0200162#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +0200163 (void)opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE);
164#endif
165#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
166 (void)opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags, TRUE);
167#endif
168#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
169 (void)opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE);
170#endif
Sean Dewar39c46b42022-05-12 17:44:29 +0100171 (void)opt_strings_flags(p_swb, p_swb_values, &swb_flags, TRUE);
LemonBoy5247b0b2024-07-12 19:30:58 +0200172 (void)opt_strings_flags(p_tcl, p_tcl_values, &tcl_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200173}
174
Yegappan Lakshmananf9dc2782023-05-11 15:02:56 +0100175#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200176/*
177 * Trigger the OptionSet autocommand.
178 * "opt_idx" is the index of the option being set.
179 * "opt_flags" can be OPT_LOCAL etc.
180 * "oldval" the old value
181 * "oldval_l" the old local value (only non-NULL if global and local value
182 * are set)
183 * "oldval_g" the old global value (only non-NULL if global and local value
184 * are set)
185 * "newval" the new value
186 */
187 void
zeertzjq269aa2b2022-11-28 11:36:50 +0000188trigger_optionset_string(
Bram Moolenaardac13472019-09-16 21:06:21 +0200189 int opt_idx,
190 int opt_flags,
191 char_u *oldval,
192 char_u *oldval_l,
193 char_u *oldval_g,
194 char_u *newval)
195{
196 // Don't do this recursively.
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000197 if (oldval == NULL || newval == NULL
198 || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
199 return;
Bram Moolenaardac13472019-09-16 21:06:21 +0200200
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000201 char_u buf_type[7];
202
203 sprintf((char *)buf_type, "%s",
Bram Moolenaardac13472019-09-16 21:06:21 +0200204 (opt_flags & OPT_LOCAL) ? "local" : "global");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000205 set_vim_var_string(VV_OPTION_OLD, oldval, -1);
206 set_vim_var_string(VV_OPTION_NEW, newval, -1);
207 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
208 if (opt_flags & OPT_LOCAL)
209 {
210 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
211 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
Bram Moolenaardac13472019-09-16 21:06:21 +0200212 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000213 if (opt_flags & OPT_GLOBAL)
214 {
215 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
216 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval, -1);
217 }
218 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
219 {
220 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
221 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval_l, -1);
222 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval_g, -1);
223 }
224 if (opt_flags & OPT_MODELINE)
225 {
226 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
227 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
228 }
229 apply_autocmds(EVENT_OPTIONSET,
230 get_option_fullname(opt_idx), NULL, FALSE,
231 NULL);
232 reset_v_option_vars();
Bram Moolenaardac13472019-09-16 21:06:21 +0200233}
234#endif
235
236 static char *
Mike Williams620f0112023-12-05 15:36:06 +0100237illegal_char(char *errbuf, size_t errbuflen, int c)
Bram Moolenaardac13472019-09-16 21:06:21 +0200238{
239 if (errbuf == NULL)
240 return "";
zeertzjq6a8d2e12024-01-17 20:54:49 +0100241 vim_snprintf(errbuf, errbuflen, _(e_illegal_character_str),
Christian Brabandtb39b2402023-11-29 11:34:05 +0100242 (char *)transchar(c));
Bram Moolenaardac13472019-09-16 21:06:21 +0200243 return errbuf;
244}
245
246/*
247 * Check string options in a buffer for NULL value.
248 */
249 void
250check_buf_options(buf_T *buf)
251{
252 check_string_option(&buf->b_p_bh);
253 check_string_option(&buf->b_p_bt);
254 check_string_option(&buf->b_p_fenc);
255 check_string_option(&buf->b_p_ff);
256#ifdef FEAT_FIND_ID
257 check_string_option(&buf->b_p_def);
258 check_string_option(&buf->b_p_inc);
259# ifdef FEAT_EVAL
260 check_string_option(&buf->b_p_inex);
261# endif
262#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +0100263#if defined(FEAT_EVAL)
Bram Moolenaardac13472019-09-16 21:06:21 +0200264 check_string_option(&buf->b_p_inde);
265 check_string_option(&buf->b_p_indk);
266#endif
267#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
268 check_string_option(&buf->b_p_bexpr);
269#endif
270#if defined(FEAT_CRYPT)
271 check_string_option(&buf->b_p_cm);
272#endif
273 check_string_option(&buf->b_p_fp);
274#if defined(FEAT_EVAL)
275 check_string_option(&buf->b_p_fex);
276#endif
277#ifdef FEAT_CRYPT
278 check_string_option(&buf->b_p_key);
279#endif
280 check_string_option(&buf->b_p_kp);
281 check_string_option(&buf->b_p_mps);
282 check_string_option(&buf->b_p_fo);
283 check_string_option(&buf->b_p_flp);
284 check_string_option(&buf->b_p_isk);
Bram Moolenaardac13472019-09-16 21:06:21 +0200285 check_string_option(&buf->b_p_com);
Bram Moolenaardac13472019-09-16 21:06:21 +0200286#ifdef FEAT_FOLDING
287 check_string_option(&buf->b_p_cms);
288#endif
289 check_string_option(&buf->b_p_nf);
Bram Moolenaardac13472019-09-16 21:06:21 +0200290 check_string_option(&buf->b_p_qe);
Bram Moolenaardac13472019-09-16 21:06:21 +0200291#ifdef FEAT_SYN_HL
292 check_string_option(&buf->b_p_syn);
293 check_string_option(&buf->b_s.b_syn_isk);
294#endif
295#ifdef FEAT_SPELL
296 check_string_option(&buf->b_s.b_p_spc);
297 check_string_option(&buf->b_s.b_p_spf);
298 check_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +0200299 check_string_option(&buf->b_s.b_p_spo);
Bram Moolenaardac13472019-09-16 21:06:21 +0200300#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200301 check_string_option(&buf->b_p_sua);
Bram Moolenaardac13472019-09-16 21:06:21 +0200302 check_string_option(&buf->b_p_cink);
303 check_string_option(&buf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +0100304 check_string_option(&buf->b_p_cinsd);
Bram Moolenaardac13472019-09-16 21:06:21 +0200305 parse_cino(buf);
Bram Moolenaar49846fb2022-10-15 16:05:33 +0100306 check_string_option(&buf->b_p_lop);
Bram Moolenaardac13472019-09-16 21:06:21 +0200307 check_string_option(&buf->b_p_ft);
Bram Moolenaardac13472019-09-16 21:06:21 +0200308 check_string_option(&buf->b_p_cinw);
zeertzjq529b9ad2024-06-05 20:27:06 +0200309 check_string_option(&buf->b_p_cot);
Bram Moolenaardac13472019-09-16 21:06:21 +0200310 check_string_option(&buf->b_p_cpt);
311#ifdef FEAT_COMPL_FUNC
312 check_string_option(&buf->b_p_cfu);
313 check_string_option(&buf->b_p_ofu);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +0100314 check_string_option(&buf->b_p_tsrfu);
Bram Moolenaardac13472019-09-16 21:06:21 +0200315#endif
316#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +0100317 check_string_option(&buf->b_p_ffu);
Bram Moolenaardac13472019-09-16 21:06:21 +0200318 check_string_option(&buf->b_p_tfu);
319#endif
320#ifdef FEAT_KEYMAP
321 check_string_option(&buf->b_p_keymap);
322#endif
323#ifdef FEAT_QUICKFIX
324 check_string_option(&buf->b_p_gp);
325 check_string_option(&buf->b_p_mp);
326 check_string_option(&buf->b_p_efm);
327#endif
328 check_string_option(&buf->b_p_ep);
329 check_string_option(&buf->b_p_path);
330 check_string_option(&buf->b_p_tags);
331 check_string_option(&buf->b_p_tc);
332 check_string_option(&buf->b_p_dict);
333 check_string_option(&buf->b_p_tsr);
Bram Moolenaardac13472019-09-16 21:06:21 +0200334 check_string_option(&buf->b_p_lw);
Bram Moolenaardac13472019-09-16 21:06:21 +0200335 check_string_option(&buf->b_p_bkc);
336 check_string_option(&buf->b_p_menc);
337#ifdef FEAT_VARTABS
338 check_string_option(&buf->b_p_vsts);
339 check_string_option(&buf->b_p_vts);
340#endif
341}
342
343/*
344 * Free the string allocated for an option.
345 * Checks for the string being empty_option. This may happen if we're out of
346 * memory, vim_strsave() returned NULL, which was replaced by empty_option by
347 * check_options().
348 * Does NOT check for P_ALLOCED flag!
349 */
350 void
351free_string_option(char_u *p)
352{
353 if (p != empty_option)
354 vim_free(p);
355}
356
357 void
358clear_string_option(char_u **pp)
359{
360 if (*pp != empty_option)
361 vim_free(*pp);
362 *pp = empty_option;
363}
364
365 void
366check_string_option(char_u **pp)
367{
368 if (*pp == NULL)
369 *pp = empty_option;
370}
371
372/*
373 * Set global value for string option when it's a local option.
374 */
375 static void
376set_string_option_global(
377 int opt_idx, // option index
378 char_u **varp) // pointer to option variable
379{
380 char_u **p, *s;
381
382 // the global value is always allocated
383 if (is_window_local_option(opt_idx))
384 p = (char_u **)GLOBAL_WO(varp);
385 else
386 p = (char_u **)get_option_var(opt_idx);
387 if (!is_global_option(opt_idx)
388 && p != varp
389 && (s = vim_strsave(*varp)) != NULL)
390 {
391 free_string_option(*p);
392 *p = s;
393 }
394}
395
396/*
397 * Set a string option to a new value (without checking the effect).
398 * The string is copied into allocated memory.
399 * if ("opt_idx" == -1) "name" is used, otherwise "opt_idx" is used.
400 * When "set_sid" is zero set the scriptID to current_sctx.sc_sid. When
401 * "set_sid" is SID_NONE don't set the scriptID. Otherwise set the scriptID to
402 * "set_sid".
403 */
404 void
405set_string_option_direct(
406 char_u *name,
407 int opt_idx,
408 char_u *val,
409 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
410 int set_sid UNUSED)
411{
412 char_u *s;
413 char_u **varp;
414 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
415 int idx = opt_idx;
416
417 if (idx == -1) // use name
418 {
419 idx = findoption(name);
420 if (idx < 0) // not found (should not happen)
421 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000422 semsg(_(e_internal_error_str), "set_string_option_direct()");
RestorerZ68ebcee2023-05-31 17:12:14 +0100423 siemsg("For option %s", name);
Bram Moolenaardac13472019-09-16 21:06:21 +0200424 return;
425 }
426 }
427
428 if (is_hidden_option(idx)) // can't set hidden option
429 return;
430
431 s = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000432 if (s == NULL)
433 return;
434
435 varp = (char_u **)get_option_varp_scope(idx,
436 both ? OPT_LOCAL : opt_flags);
437 if ((opt_flags & OPT_FREE) && (get_option_flags(idx) & P_ALLOCED))
438 free_string_option(*varp);
439 *varp = s;
440
441 // For buffer/window local option may also set the global value.
442 if (both)
443 set_string_option_global(idx, varp);
444
445 set_option_flag(idx, P_ALLOCED);
446
447 // When setting both values of a global option with a local value,
448 // make the local value empty, so that the global value is used.
449 if (is_global_local_option(idx) && both)
Bram Moolenaardac13472019-09-16 21:06:21 +0200450 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000451 free_string_option(*varp);
452 *varp = empty_option;
Bram Moolenaardac13472019-09-16 21:06:21 +0200453 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000454# ifdef FEAT_EVAL
455 if (set_sid != SID_NONE)
456 {
457 sctx_T script_ctx;
458
459 if (set_sid == 0)
460 script_ctx = current_sctx;
461 else
462 {
463 script_ctx.sc_sid = set_sid;
464 script_ctx.sc_seq = 0;
465 script_ctx.sc_lnum = 0;
466 script_ctx.sc_version = 1;
467 }
468 set_option_sctx_idx(idx, opt_flags, script_ctx);
469 }
470# endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200471}
472
Dominique Pellee764d1b2023-03-12 21:20:59 +0000473#if defined(FEAT_PROP_POPUP) || \
474 (defined(FEAT_DIFF) && defined(FEAT_FOLDING)) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200475/*
476 * Like set_string_option_direct(), but for a window-local option in "wp".
477 * Blocks autocommands to avoid the old curwin becoming invalid.
478 */
479 void
480set_string_option_direct_in_win(
481 win_T *wp,
482 char_u *name,
483 int opt_idx,
484 char_u *val,
485 int opt_flags,
486 int set_sid)
487{
488 win_T *save_curwin = curwin;
489
490 block_autocmds();
491 curwin = wp;
492 curbuf = curwin->w_buffer;
493 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
494 curwin = save_curwin;
495 curbuf = curwin->w_buffer;
496 unblock_autocmds();
497}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000498#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200499
Dominique Pelle748b3082022-01-08 12:41:16 +0000500#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200501/*
502 * Like set_string_option_direct(), but for a buffer-local option in "buf".
503 * Blocks autocommands to avoid the old curbuf becoming invalid.
504 */
505 void
506set_string_option_direct_in_buf(
507 buf_T *buf,
508 char_u *name,
509 int opt_idx,
510 char_u *val,
511 int opt_flags,
512 int set_sid)
513{
514 buf_T *save_curbuf = curbuf;
515
516 block_autocmds();
517 curbuf = buf;
518 curwin->w_buffer = curbuf;
519 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
520 curbuf = save_curbuf;
521 curwin->w_buffer = curbuf;
522 unblock_autocmds();
523}
Dominique Pelle748b3082022-01-08 12:41:16 +0000524#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200525
526/*
527 * Set a string option to a new value, and handle the effects.
528 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100529 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaardac13472019-09-16 21:06:21 +0200530 */
531 char *
532set_string_option(
533 int opt_idx,
534 char_u *value,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +0000535 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Christian Brabandtb39b2402023-11-29 11:34:05 +0100536 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +0100537 size_t errbuflen)
Bram Moolenaardac13472019-09-16 21:06:21 +0200538{
539 char_u *s;
540 char_u **varp;
541 char_u *oldval;
542#if defined(FEAT_EVAL)
543 char_u *oldval_l = NULL;
544 char_u *oldval_g = NULL;
545 char_u *saved_oldval = NULL;
546 char_u *saved_oldval_l = NULL;
547 char_u *saved_oldval_g = NULL;
548 char_u *saved_newval = NULL;
549#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100550 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +0200551 int value_checked = FALSE;
552
553 if (is_hidden_option(opt_idx)) // don't set hidden option
554 return NULL;
555
Bram Moolenaar7f009df2020-03-16 20:27:38 +0100556 s = vim_strsave(value == NULL ? (char_u *)"" : value);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000557 if (s == NULL)
558 return NULL;
559
560 varp = (char_u **)get_option_varp_scope(opt_idx,
561 (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
562 ? (is_global_local_option(opt_idx)
563 ? OPT_GLOBAL : OPT_LOCAL)
564 : opt_flags);
565 oldval = *varp;
566#if defined(FEAT_EVAL)
567 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +0200568 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000569 oldval_l = *(char_u **)get_option_varp_scope(opt_idx, OPT_LOCAL);
570 oldval_g = *(char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
Bram Moolenaardac13472019-09-16 21:06:21 +0200571 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000572#endif
573 *varp = s;
574
575#if defined(FEAT_EVAL)
576 if (!starting
577# ifdef FEAT_CRYPT
578 && !is_crypt_key_option(opt_idx)
579# endif
580 )
581 {
582 if (oldval_l != NULL)
583 saved_oldval_l = vim_strsave(oldval_l);
584 if (oldval_g != NULL)
585 saved_oldval_g = vim_strsave(oldval_g);
586 saved_oldval = vim_strsave(oldval);
587 saved_newval = vim_strsave(s);
588 }
589#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000590 if ((errmsg = did_set_string_option(opt_idx, varp, oldval, value, errbuf,
Christian Brabandtb39b2402023-11-29 11:34:05 +0100591 errbuflen, opt_flags, OP_NONE, &value_checked)) == NULL)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000592 did_set_option(opt_idx, opt_flags, TRUE, value_checked);
593
594#if defined(FEAT_EVAL)
595 // call autocommand after handling side effects
596 if (errmsg == NULL)
597 trigger_optionset_string(opt_idx, opt_flags,
598 saved_oldval, saved_oldval_l,
599 saved_oldval_g, saved_newval);
600 vim_free(saved_oldval);
601 vim_free(saved_oldval_l);
602 vim_free(saved_oldval_g);
603 vim_free(saved_newval);
604#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100605 return errmsg;
Bram Moolenaardac13472019-09-16 21:06:21 +0200606}
607
608/*
609 * Return TRUE if "val" is a valid 'filetype' name.
610 * Also used for 'syntax' and 'keymap'.
611 */
612 static int
613valid_filetype(char_u *val)
614{
615 return valid_name(val, ".-_");
616}
617
618#ifdef FEAT_STL_OPT
619/*
620 * Check validity of options with the 'statusline' format.
zeertzjq5dc294a2022-04-15 13:17:57 +0100621 * Return an untranslated error message or NULL.
Bram Moolenaardac13472019-09-16 21:06:21 +0200622 */
623 static char *
624check_stl_option(char_u *s)
625{
Bram Moolenaardac13472019-09-16 21:06:21 +0200626 int groupdepth = 0;
Christian Brabandtb39b2402023-11-29 11:34:05 +0100627 static char errbuf[ERR_BUFLEN];
628 int errbuflen = ERR_BUFLEN;
Bram Moolenaardac13472019-09-16 21:06:21 +0200629
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100630 while (*s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200631 {
632 // Check for valid keys after % sequences
633 while (*s && *s != '%')
634 s++;
635 if (!*s)
636 break;
637 s++;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +0000638 if (*s == '%' || *s == STL_TRUNCMARK || *s == STL_SEPARATE)
Bram Moolenaardac13472019-09-16 21:06:21 +0200639 {
640 s++;
641 continue;
642 }
643 if (*s == ')')
644 {
645 s++;
646 if (--groupdepth < 0)
647 break;
648 continue;
649 }
650 if (*s == '-')
651 s++;
652 while (VIM_ISDIGIT(*s))
653 s++;
654 if (*s == STL_USER_HL)
655 continue;
656 if (*s == '.')
657 {
658 s++;
659 while (*s && VIM_ISDIGIT(*s))
660 s++;
661 }
662 if (*s == '(')
663 {
664 groupdepth++;
665 continue;
666 }
667 if (vim_strchr(STL_ALL, *s) == NULL)
668 {
Christian Brabandtb39b2402023-11-29 11:34:05 +0100669 return illegal_char(errbuf, errbuflen, *s);
Bram Moolenaardac13472019-09-16 21:06:21 +0200670 }
671 if (*s == '{')
672 {
zeertzjq5dc294a2022-04-15 13:17:57 +0100673 int reevaluate = (*++s == '%');
shadmansaleh30e3de22021-05-15 17:23:28 +0200674
zeertzjq5dc294a2022-04-15 13:17:57 +0100675 if (reevaluate && *++s == '}')
676 // "}" is not allowed immediately after "%{%"
Christian Brabandtb39b2402023-11-29 11:34:05 +0100677 return illegal_char(errbuf, errbuflen, '}');
shadmansaleh30e3de22021-05-15 17:23:28 +0200678 while ((*s != '}' || (reevaluate && s[-1] != '%')) && *s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200679 s++;
680 if (*s != '}')
zeertzjq5dc294a2022-04-15 13:17:57 +0100681 return e_unclosed_expression_sequence;
Bram Moolenaardac13472019-09-16 21:06:21 +0200682 }
683 }
Bram Moolenaardac13472019-09-16 21:06:21 +0200684 if (groupdepth != 0)
zeertzjq5dc294a2022-04-15 13:17:57 +0100685 return e_unbalanced_groups;
Bram Moolenaardac13472019-09-16 21:06:21 +0200686 return NULL;
687}
688#endif
689
690/*
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +0000691 * Check for a "normal" directory or file name in some options. Disallow a
692 * path separator (slash and/or backslash), wildcards and characters that are
693 * often illegal in a file name. Be more permissive if "secure" is off.
694 */
695 static int
696check_illegal_path_names(int opt_idx, char_u **varp)
697{
698 return (((get_option_flags(opt_idx) & P_NFNAME)
699 && vim_strpbrk(*varp, (char_u *)(secure
700 ? "/\\*?[|;&<>\r\n" : "/\\*?[<>\r\n")) != NULL)
701 || ((get_option_flags(opt_idx) & P_NDNAME)
702 && vim_strpbrk(*varp, (char_u *)"*?[|;&<>\r\n") != NULL));
703}
704
705/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000706 * An option that accepts a list of flags is changed.
707 * e.g. 'viewoptions', 'switchbuf', 'casemap', etc.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000708 */
709 static char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000710did_set_opt_flags(char_u *val, char **values, unsigned *flagp, int list)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000711{
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000712 if (opt_strings_flags(val, values, flagp, list) == FAIL)
713 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000714
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000715 return NULL;
716}
717
718/*
719 * An option that accepts a list of string values is changed.
720 * e.g. 'nrformats', 'scrollopt', 'wildoptions', etc.
721 */
722 static char *
723did_set_opt_strings(char_u *val, char **values, int list)
724{
725 return did_set_opt_flags(val, values, NULL, list);
726}
727
728/*
729 * An option which is a list of flags is set. Valid values are in 'flags'.
730 */
731 static char *
Christian Brabandtb39b2402023-11-29 11:34:05 +0100732did_set_option_listflag(
733 char_u *val,
734 char_u *flags,
735 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +0100736 size_t errbuflen)
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000737{
738 char_u *s;
739
Yegappan Lakshmananc727b192023-03-03 12:26:15 +0000740 for (s = val; *s; ++s)
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000741 if (vim_strchr(flags, *s) == NULL)
Christian Brabandtb39b2402023-11-29 11:34:05 +0100742 return illegal_char(errbuf, errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000743
744 return NULL;
745}
746
747/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200748 * Expand an option that accepts a list of fixed string values with known
749 * number of items.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200750 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200751 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200752expand_set_opt_string(
753 optexpand_T *args,
754 char **values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +0200755 size_t numValues,
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200756 int *numMatches,
757 char_u ***matches)
758{
759 char_u *p;
760 regmatch_T *regmatch = args->oe_regmatch;
761 int include_orig_val = args->oe_include_orig_val;
762 char_u *option_val = args->oe_opt_value;
763
764 // Assume numValues is small since they are fixed enums, so just allocate
765 // upfront instead of needing two passes to calculate output size.
766 *matches = ALLOC_MULT(char_u *, numValues + 1);
767 if (*matches == NULL)
768 return FAIL;
769
770 int count = 0;
771
772 if (include_orig_val && *option_val != NUL)
773 {
774 p = vim_strsave(option_val);
775 if (p == NULL)
776 {
777 VIM_CLEAR(*matches);
778 return FAIL;
779 }
780 (*matches)[count++] = p;
781 }
782
783 for (char **val = values; *val != NULL; val++)
784 {
785 if (include_orig_val && *option_val != NUL)
786 {
787 if (STRCMP((char_u*)*val, option_val) == 0)
788 continue;
789 }
790 if (vim_regexec(regmatch, (char_u*)(*val), (colnr_T)0))
791 {
792 p = vim_strsave((char_u*)*val);
793 if (p == NULL)
794 {
795 if (count == 0)
796 {
797 VIM_CLEAR(*matches);
798 return FAIL;
799 }
800 else
801 break;
802 }
803 (*matches)[count++] = p;
804 }
805 }
806 if (count == 0)
807 {
808 VIM_CLEAR(*matches);
809 return FAIL;
810 }
811 *numMatches = count;
812 return OK;
813}
814
815static char_u *set_opt_callback_orig_option = NULL;
816static char_u *((*set_opt_callback_func)(expand_T *, int));
817
818/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200819 * Callback used by expand_set_opt_generic to also include the original value
820 * as the first item.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200821 */
822 static char_u *
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200823expand_set_opt_generic_cb(expand_T *xp, int idx)
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200824{
825 if (idx == 0)
826 {
827 if (set_opt_callback_orig_option != NULL)
828 return set_opt_callback_orig_option;
829 else
830 return (char_u *)""; // empty strings are ignored
831 }
832 return set_opt_callback_func(xp, idx - 1);
833}
834
835/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200836 * Expand an option with a callback that iterates through a list of possible
837 * names using an index.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200838 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200839 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200840expand_set_opt_generic(
841 optexpand_T *args,
842 char_u *((*func)(expand_T *, int)),
843 int *numMatches,
844 char_u ***matches)
845{
846 int ret;
847
848 set_opt_callback_orig_option = args->oe_include_orig_val ?
849 args->oe_opt_value : NULL;
850 set_opt_callback_func = func;
851
852 ret = ExpandGeneric(
853 (char_u*)"", // not using fuzzy as currently EXPAND_STRING_SETTING doesn't use it
854 args->oe_xp,
855 args->oe_regmatch,
856 matches,
857 numMatches,
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200858 expand_set_opt_generic_cb,
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200859 FALSE);
860
861 set_opt_callback_orig_option = NULL;
862 set_opt_callback_func = NULL;
863 return ret;
864}
865
Christian Brabandt9960ebc2023-10-05 22:17:09 +0200866# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200867static garray_T *expand_cb_ga;
868static optexpand_T *expand_cb_args;
869
870/*
871 * Callback provided to a function in expand_set_opt_callback. Will perform
872 * regex matching against the value and add to the list.
873 *
874 * Returns OK usually. Returns FAIL if it failed to allocate memory, and the
875 * caller should terminate the enumeration.
876 */
877 static int
878expand_set_opt_callback_cb(char_u *val)
879{
880 regmatch_T *regmatch = expand_cb_args->oe_regmatch;
881 expand_T *xp = expand_cb_args->oe_xp;
882 garray_T *ga = expand_cb_ga;
883 char_u *str;
884
885 if (val == NULL || *val == NUL)
886 return OK;
887
888 if (xp->xp_pattern[0] != NUL &&
889 !vim_regexec(regmatch, val, (colnr_T)0))
890 return OK;
891
892 str = vim_strsave_escaped(val, (char_u *)" \t\\");
893
894 if (str == NULL)
895 return FAIL;
896
897 if (ga_grow(ga, 1) == FAIL)
898 {
899 vim_free(str);
900 return FAIL;
901 }
902
903 ((char_u **)ga->ga_data)[ga->ga_len] = str;
904 ++ga->ga_len;
905 return OK;
906}
907
908/*
909 * Expand an option with a provided function that takes a callback. The
910 * function will enumerate through all options and call the callback to add it
911 * to the list.
912 *
913 * "func" is the enumerator function that will generate the list of options.
914 * "func_params" is a single parameter that will be passed to func.
915 */
916 static int
917expand_set_opt_callback(
918 optexpand_T *args,
919 void (*func)(optexpand_T *, void* params, int (*cb)(char_u *val)),
920 void *func_params,
921 int *numMatches,
922 char_u ***matches)
923{
924 garray_T ga;
925 int include_orig_val = args->oe_include_orig_val;
926 char_u *option_val = args->oe_opt_value;
927
928 ga_init2(&ga, sizeof(char *), 30);
929
930 if (include_orig_val && *option_val != NUL)
931 {
932 char_u *p = vim_strsave(option_val);
933 if (p == NULL)
934 return FAIL;
935 if (ga_grow(&ga, 1) == FAIL)
936 {
937 vim_free(p);
938 return FAIL;
939 }
940 ((char_u **)ga.ga_data)[ga.ga_len] = p;
941 ++ga.ga_len;
942 }
943
944 expand_cb_ga = &ga;
945 expand_cb_args = args;
946
947 func(args, func_params, expand_set_opt_callback_cb);
948
949 expand_cb_ga = NULL;
950 expand_cb_args = NULL;
951
952 *matches = ga.ga_data;
953 *numMatches = ga.ga_len;
954 return OK;
955}
Christian Brabandt9960ebc2023-10-05 22:17:09 +0200956#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200957
958/*
959 * Expand an option which is a list of flags.
960 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200961 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200962expand_set_opt_listflag(
963 optexpand_T *args,
964 char_u *flags,
965 int *numMatches,
966 char_u ***matches)
967{
968 char_u *p;
969 char_u *option_val = args->oe_opt_value;
970 char_u *cmdline_val = args->oe_set_arg;
971 int append = args->oe_append;
972 int include_orig_val = args->oe_include_orig_val && (*option_val != NUL);
973
Yee Cheng Chin6d113472023-10-02 21:38:39 +0200974 size_t num_flags = STRLEN(flags);
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200975
976 // Assume we only have small number of flags, so just allocate max size.
977 *matches = ALLOC_MULT(char_u *, num_flags + 1);
978 if (*matches == NULL)
979 return FAIL;
980
981 int count = 0;
982
983 if (include_orig_val)
984 {
985 p = vim_strsave(option_val);
986 if (p == NULL)
987 {
988 VIM_CLEAR(*matches);
989 return FAIL;
990 }
991 (*matches)[count++] = p;
992 }
993
994 for (char_u *flag = flags; *flag != NUL; flag++)
995 {
996 if (append && vim_strchr(option_val, *flag) != NULL)
997 continue;
998
999 if (vim_strchr(cmdline_val, *flag) == NULL)
1000 {
1001 if (include_orig_val
1002 && option_val[1] == NUL
1003 && *flag == option_val[0])
1004 {
1005 // This value is already used as the first choice as it's the
1006 // existing flag. Just skip it to avoid duplicate.
1007 continue;
1008 }
1009 p = vim_strnsave(flag, 1);
1010 if (p == NULL)
1011 {
1012 if (count == 0)
1013 {
1014 VIM_CLEAR(*matches);
1015 return FAIL;
1016 }
1017 else
1018 break;
1019 }
1020 (*matches)[count++] = p;
1021 }
1022 }
1023
1024 if (count == 0)
1025 {
1026 VIM_CLEAR(*matches);
1027 return FAIL;
1028 }
1029 *numMatches = count;
1030 return OK;
1031}
1032
1033/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001034 * The 'ambiwidth' option is changed.
1035 */
1036 char *
1037did_set_ambiwidth(optset_T *args UNUSED)
1038{
1039 if (check_opt_strings(p_ambw, p_ambw_values, FALSE) != OK)
1040 return e_invalid_argument;
1041
1042 return check_chars_options();
1043}
1044
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001045 int
1046expand_set_ambiwidth(optexpand_T *args, int *numMatches, char_u ***matches)
1047{
1048 return expand_set_opt_string(
1049 args,
1050 p_ambw_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001051 ARRAY_LENGTH(p_ambw_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001052 numMatches,
1053 matches);
1054}
1055
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001056/*
1057 * The 'background' option is changed.
1058 */
1059 char *
Gregory Anders83ad2722024-01-03 19:48:51 +01001060did_set_background(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001061{
1062 if (check_opt_strings(p_bg, p_bg_values, FALSE) == FAIL)
1063 return e_invalid_argument;
1064
Gregory Anders83ad2722024-01-03 19:48:51 +01001065 if (args->os_oldval.string != NULL && args->os_oldval.string[0] == *p_bg)
1066 // Value was not changed
1067 return NULL;
1068
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001069#ifdef FEAT_EVAL
1070 int dark = (*p_bg == 'd');
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001071#endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001072
1073 init_highlight(FALSE, FALSE);
1074
1075#ifdef FEAT_EVAL
1076 if (dark != (*p_bg == 'd')
1077 && get_var_value((char_u *)"g:colors_name") != NULL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001078 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001079 // The color scheme must have set 'background' back to another
1080 // value, that's not what we want here. Disable the color
1081 // scheme and set the colors again.
1082 do_unlet((char_u *)"g:colors_name", TRUE);
1083 free_string_option(p_bg);
1084 p_bg = vim_strsave((char_u *)(dark ? "dark" : "light"));
1085 check_string_option(&p_bg);
1086 init_highlight(FALSE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001087 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001088#endif
1089#ifdef FEAT_TERMINAL
1090 term_update_colors_all();
1091#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001092
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001093 return NULL;
1094}
1095
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001096 int
1097expand_set_background(optexpand_T *args, int *numMatches, char_u ***matches)
1098{
1099 return expand_set_opt_string(
1100 args,
1101 p_bg_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001102 ARRAY_LENGTH(p_bg_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001103 numMatches,
1104 matches);
1105}
1106
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001107/*
1108 * The 'backspace' option is changed.
1109 */
1110 char *
1111did_set_backspace(optset_T *args UNUSED)
1112{
1113 if (VIM_ISDIGIT(*p_bs))
1114 {
1115 if (*p_bs > '3' || p_bs[1] != NUL)
1116 return e_invalid_argument;
1117 }
1118 else if (check_opt_strings(p_bs, p_bs_values, TRUE) != OK)
1119 return e_invalid_argument;
1120
1121 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001122}
1123
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001124 int
1125expand_set_backspace(optexpand_T *args, int *numMatches, char_u ***matches)
1126{
1127 return expand_set_opt_string(
1128 args,
1129 p_bs_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001130 ARRAY_LENGTH(p_bs_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001131 numMatches,
1132 matches);
1133}
1134
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001135/*
1136 * The 'backupcopy' option is changed.
1137 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001138 char *
1139did_set_backupcopy(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001140{
1141 char_u *bkc = p_bkc;
1142 unsigned int *flags = &bkc_flags;
1143 char *errmsg = NULL;
1144
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001145 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001146 {
1147 bkc = curbuf->b_p_bkc;
1148 flags = &curbuf->b_bkc_flags;
1149 }
zeertzjq46dcd842024-11-03 09:10:50 +01001150 else if (!(args->os_flags & OPT_GLOBAL))
1151 // When using :set, clear the local flags.
1152 curbuf->b_bkc_flags = 0;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001153
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001154 if ((args->os_flags & OPT_LOCAL) && *bkc == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001155 // make the local value empty: use the global value
1156 *flags = 0;
1157 else
1158 {
1159 if (opt_strings_flags(bkc, p_bkc_values, flags, TRUE) != OK)
1160 errmsg = e_invalid_argument;
1161 if ((((int)*flags & BKC_AUTO) != 0)
1162 + (((int)*flags & BKC_YES) != 0)
1163 + (((int)*flags & BKC_NO) != 0) != 1)
1164 {
1165 // Must have exactly one of "auto", "yes" and "no".
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001166 (void)opt_strings_flags(args->os_oldval.string, p_bkc_values,
1167 flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001168 errmsg = e_invalid_argument;
1169 }
1170 }
1171
1172 return errmsg;
1173}
1174
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001175 int
1176expand_set_backupcopy(optexpand_T *args, int *numMatches, char_u ***matches)
1177{
1178 return expand_set_opt_string(
1179 args,
1180 p_bkc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001181 ARRAY_LENGTH(p_bkc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001182 numMatches,
1183 matches);
1184}
1185
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001186/*
1187 * The 'backupext' or the 'patchmode' option is changed.
1188 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001189 char *
1190did_set_backupext_or_patchmode(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001191{
1192 if (STRCMP(*p_bex == '.' ? p_bex + 1 : p_bex,
1193 *p_pm == '.' ? p_pm + 1 : p_pm) == 0)
1194 return e_backupext_and_patchmode_are_equal;
1195
1196 return NULL;
1197}
1198
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001199/*
1200 * The 'belloff' option is changed.
1201 */
1202 char *
1203did_set_belloff(optset_T *args UNUSED)
1204{
1205 return did_set_opt_flags(p_bo, p_bo_values, &bo_flags, TRUE);
1206}
1207
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001208 int
1209expand_set_belloff(optexpand_T *args, int *numMatches, char_u ***matches)
1210{
1211 return expand_set_opt_string(
1212 args,
1213 p_bo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001214 ARRAY_LENGTH(p_bo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001215 numMatches,
1216 matches);
1217}
1218
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001219#if defined(FEAT_LINEBREAK) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001220/*
zeertzjqeac3fdc2024-02-03 18:08:09 +01001221 * The 'breakat' option is changed.
1222 */
1223 char *
1224did_set_breakat(optset_T *args UNUSED)
1225{
1226 char_u *p;
1227 int i;
1228
1229 for (i = 0; i < 256; i++)
1230 breakat_flags[i] = FALSE;
1231
1232 if (p_breakat != NULL)
1233 for (p = p_breakat; *p; p++)
1234 breakat_flags[*p] = TRUE;
1235
1236 return NULL;
1237}
1238
1239/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001240 * The 'breakindentopt' option is changed.
1241 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001242 char *
Millyb38700a2024-10-22 22:59:39 +02001243did_set_breakindentopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001244{
Millyb38700a2024-10-22 22:59:39 +02001245 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001246
Millyb38700a2024-10-22 22:59:39 +02001247 if (briopt_check(*varp, varp == &curwin->w_p_briopt ? curwin : NULL)
1248 == FAIL)
1249 return e_invalid_argument;
1250
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001251 // list setting requires a redraw
Millyb38700a2024-10-22 22:59:39 +02001252 if (varp == &curwin->w_p_briopt && curwin->w_briopt_list)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001253 redraw_all_later(UPD_NOT_VALID);
1254
Millyb38700a2024-10-22 22:59:39 +02001255 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001256}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001257
1258 int
1259expand_set_breakindentopt(optexpand_T *args, int *numMatches, char_u ***matches)
1260{
1261 return expand_set_opt_string(
1262 args,
1263 p_briopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001264 ARRAY_LENGTH(p_briopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001265 numMatches,
1266 matches);
1267}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001268#endif
1269
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001270#if defined(FEAT_BROWSE) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001271/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001272 * The 'browsedir' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001273 */
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00001274 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001275did_set_browsedir(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001276{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001277 if (check_opt_strings(p_bsdir, p_bsdir_values, FALSE) != OK
1278 && !mch_isdir(p_bsdir))
1279 return e_invalid_argument;
1280
1281 return NULL;
1282}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001283
1284 int
1285expand_set_browsedir(optexpand_T *args, int *numMatches, char_u ***matches)
1286{
1287 return expand_set_opt_string(
1288 args,
1289 p_bsdir_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001290 ARRAY_LENGTH(p_bsdir_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001291 numMatches,
1292 matches);
1293}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001294#endif
1295
1296/*
1297 * The 'bufhidden' option is changed.
1298 */
1299 char *
1300did_set_bufhidden(optset_T *args UNUSED)
1301{
1302 return did_set_opt_strings(curbuf->b_p_bh, p_bufhidden_values, FALSE);
1303}
1304
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001305 int
1306expand_set_bufhidden(optexpand_T *args, int *numMatches, char_u ***matches)
1307{
1308 return expand_set_opt_string(
1309 args,
1310 p_bufhidden_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001311 ARRAY_LENGTH(p_bufhidden_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001312 numMatches,
1313 matches);
1314}
1315
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001316/*
1317 * The 'buftype' option is changed.
1318 */
1319 char *
1320did_set_buftype(optset_T *args UNUSED)
1321{
1322 if (check_opt_strings(curbuf->b_p_bt, p_buftype_values, FALSE) != OK)
1323 return e_invalid_argument;
1324
1325 if (curwin->w_status_height)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001326 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001327 curwin->w_redr_status = TRUE;
1328 redraw_later(UPD_VALID);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001329 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001330 curbuf->b_help = (curbuf->b_p_bt[0] == 'h');
1331 redraw_titles();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001332
1333 return NULL;
1334}
1335
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001336 int
1337expand_set_buftype(optexpand_T *args, int *numMatches, char_u ***matches)
1338{
1339 return expand_set_opt_string(
1340 args,
1341 p_buftype_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001342 ARRAY_LENGTH(p_buftype_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001343 numMatches,
1344 matches);
1345}
1346
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001347/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001348 * The 'casemap' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001349 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001350 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001351did_set_casemap(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001352{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001353 return did_set_opt_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
1354}
1355
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001356 int
1357expand_set_casemap(optexpand_T *args, int *numMatches, char_u ***matches)
1358{
1359 return expand_set_opt_string(
1360 args,
1361 p_cmp_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001362 ARRAY_LENGTH(p_cmp_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001363 numMatches,
1364 matches);
1365}
1366
1367#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1368 int
1369expand_set_clipboard(optexpand_T *args, int *numMatches, char_u ***matches)
1370{
1371 return expand_set_opt_string(
1372 args,
1373 p_cb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001374 ARRAY_LENGTH(p_cb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001375 numMatches,
1376 matches);
1377}
1378#endif
1379
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001380/*
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001381 * The global 'listchars' or 'fillchars' option is changed.
1382 */
1383 static char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01001384did_set_global_listfillchars(char_u *val, int opt_lcs, int opt_flags,
1385 char *errbuf, size_t errbuflen)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001386{
1387 char *errmsg = NULL;
1388 char_u **local_ptr = opt_lcs ? &curwin->w_p_lcs : &curwin->w_p_fcs;
1389
1390 // only apply the global value to "curwin" when it does not have a
1391 // local value
1392 if (opt_lcs)
1393 errmsg = set_listchars_option(curwin, val,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001394 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL),
1395 errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001396 else
1397 errmsg = set_fillchars_option(curwin, val,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001398 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL),
1399 errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001400 if (errmsg != NULL)
1401 return errmsg;
1402
1403 tabpage_T *tp;
1404 win_T *wp;
1405
1406 // If the current window is set to use the global
1407 // 'listchars'/'fillchars' value, clear the window-local value.
1408 if (!(opt_flags & OPT_GLOBAL))
1409 clear_string_option(local_ptr);
1410 FOR_ALL_TAB_WINDOWS(tp, wp)
1411 {
1412 // If the current window has a local value need to apply it
1413 // again, it was changed when setting the global value.
1414 // If no error was returned above, we don't expect an error
1415 // here, so ignore the return value.
1416 if (opt_lcs)
1417 {
1418 if (*wp->w_p_lcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001419 (void)set_listchars_option(wp, wp->w_p_lcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001420 }
1421 else
1422 {
1423 if (*wp->w_p_fcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001424 (void)set_fillchars_option(wp, wp->w_p_fcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001425 }
1426 }
1427
1428 redraw_all_later(UPD_NOT_VALID);
1429
1430 return NULL;
1431}
1432
1433/*
1434 * The 'fillchars' option or the 'listchars' option is changed.
1435 */
1436 char *
1437did_set_chars_option(optset_T *args)
1438{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001439 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001440 char *errmsg = NULL;
1441
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001442 if ( varp == &p_lcs // global 'listchars'
1443 || varp == &p_fcs) // global 'fillchars'
1444 errmsg = did_set_global_listfillchars(*varp, varp == &p_lcs,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001445 args->os_flags, args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001446 else if (varp == &curwin->w_p_lcs) // local 'listchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001447 errmsg = set_listchars_option(curwin, *varp, TRUE,
1448 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001449 else if (varp == &curwin->w_p_fcs) // local 'fillchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001450 errmsg = set_fillchars_option(curwin, *varp, TRUE,
1451 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001452
1453 return errmsg;
1454}
1455
1456/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001457 * Expand 'fillchars' or 'listchars' option value.
1458 */
1459 int
1460expand_set_chars_option(optexpand_T *args, int *numMatches, char_u ***matches)
1461{
1462 char_u **varp = (char_u **)args->oe_varp;
1463 int is_lcs = (varp == &p_lcs || varp == &curwin->w_p_lcs);
1464 return expand_set_opt_generic(
1465 args,
1466 is_lcs ? get_listchars_name : get_fillchars_name,
1467 numMatches,
1468 matches);
1469}
1470
1471/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001472 * The 'cinoptions' option is changed.
1473 */
1474 char *
1475did_set_cinoptions(optset_T *args UNUSED)
1476{
1477 // TODO: recognize errors
1478 parse_cino(curbuf);
1479
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001480 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001481}
1482
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00001483#if defined(FEAT_SYN_HL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001484/*
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001485 * The 'colorcolumn' option is changed.
1486 */
1487 char *
Millya441a3e2024-10-22 22:43:01 +02001488did_set_colorcolumn(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001489{
Millya441a3e2024-10-22 22:43:01 +02001490 char_u **varp = (char_u **)args->os_varp;
1491
1492 return check_colorcolumn(*varp, varp == &curwin->w_p_cc ? curwin : NULL);
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001493}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001494#endif
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001495
1496/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001497 * The 'comments' option is changed.
1498 */
1499 char *
1500did_set_comments(optset_T *args)
1501{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001502 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001503 char_u *s;
1504 char *errmsg = NULL;
1505
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001506 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001507 {
1508 while (*s && *s != ':')
1509 {
1510 if (vim_strchr((char_u *)COM_ALL, *s) == NULL
1511 && !VIM_ISDIGIT(*s) && *s != '-')
1512 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01001513 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001514 break;
1515 }
1516 ++s;
1517 }
1518 if (*s++ == NUL)
1519 errmsg = e_missing_colon;
1520 else if (*s == ',' || *s == NUL)
1521 errmsg = e_zero_length_string;
1522 if (errmsg != NULL)
1523 break;
1524 while (*s && *s != ',')
1525 {
1526 if (*s == '\\' && s[1] != NUL)
1527 ++s;
1528 ++s;
1529 }
1530 s = skip_to_option_part(s);
1531 }
1532
1533 return errmsg;
1534}
1535
1536#if defined(FEAT_FOLDING) || defined(PROTO)
1537/*
1538 * The 'commentstring' option is changed.
1539 */
1540 char *
1541did_set_commentstring(optset_T *args)
1542{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001543 char_u **varp = (char_u **)args->os_varp;
1544
1545 if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001546 return e_commentstring_must_be_empty_or_contain_str;
1547
1548 return NULL;
1549}
1550#endif
1551
1552/*
1553 * The 'complete' option is changed.
1554 */
1555 char *
1556did_set_complete(optset_T *args)
1557{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001558 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001559 char_u *s;
1560
1561 // check if it is a valid value for 'complete' -- Acevedo
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001562 for (s = *varp; *s;)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001563 {
1564 while (*s == ',' || *s == ' ')
1565 s++;
1566 if (!*s)
1567 break;
1568 if (vim_strchr((char_u *)".wbuksid]tU", *s) == NULL)
Christian Brabandtb39b2402023-11-29 11:34:05 +01001569 return illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001570 if (*++s != NUL && *s != ',' && *s != ' ')
1571 {
1572 if (s[-1] == 'k' || s[-1] == 's')
1573 {
1574 // skip optional filename after 'k' and 's'
1575 while (*s && *s != ',' && *s != ' ')
1576 {
1577 if (*s == '\\' && s[1] != NUL)
1578 ++s;
1579 ++s;
1580 }
1581 }
1582 else
1583 {
1584 if (args->os_errbuf != NULL)
1585 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01001586 vim_snprintf((char *)args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001587 _(e_illegal_character_after_chr), *--s);
1588 return args->os_errbuf;
1589 }
1590 return "";
1591 }
1592 }
1593 }
1594
1595 return NULL;
1596}
1597
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001598 int
1599expand_set_complete(optexpand_T *args, int *numMatches, char_u ***matches)
1600{
1601 static char *(p_cpt_values[]) = {
1602 ".", "w", "b", "u", "k", "kspell", "s", "i", "d", "]", "t", "U",
1603 NULL};
1604 return expand_set_opt_string(
1605 args,
1606 p_cpt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001607 ARRAY_LENGTH(p_cpt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001608 numMatches,
1609 matches);
1610}
1611
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001612/*
1613 * The 'completeopt' option is changed.
1614 */
1615 char *
1616did_set_completeopt(optset_T *args UNUSED)
1617{
zeertzjq529b9ad2024-06-05 20:27:06 +02001618 char_u *cot = p_cot;
1619 unsigned *flags = &cot_flags;
1620
1621 if (args->os_flags & OPT_LOCAL)
1622 {
1623 cot = curbuf->b_p_cot;
1624 flags = &curbuf->b_cot_flags;
1625 }
zeertzjq46dcd842024-11-03 09:10:50 +01001626 else if (!(args->os_flags & OPT_GLOBAL))
1627 // When using :set, clear the local flags.
1628 curbuf->b_cot_flags = 0;
zeertzjq529b9ad2024-06-05 20:27:06 +02001629
1630 if (check_opt_strings(cot, p_cot_values, TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001631 return e_invalid_argument;
1632
zeertzjq529b9ad2024-06-05 20:27:06 +02001633 if (opt_strings_flags(cot, p_cot_values, flags, TRUE) != OK)
1634 return e_invalid_argument;
1635
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001636 return NULL;
1637}
1638
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001639 int
1640expand_set_completeopt(optexpand_T *args, int *numMatches, char_u ***matches)
1641{
1642 return expand_set_opt_string(
1643 args,
1644 p_cot_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001645 ARRAY_LENGTH(p_cot_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001646 numMatches,
1647 matches);
1648}
1649
glepnir6a89c942024-10-01 20:32:12 +02001650/*
1651 * The 'completeitemalign' option is changed.
1652 */
1653 char *
1654did_set_completeitemalign(optset_T *args UNUSED)
1655{
1656 char_u *p = p_cia;
1657 unsigned new_cia_flags = 0;
1658 int seen[3] = { FALSE, FALSE, FALSE };
1659 int count = 0;
1660 char_u buf[10];
1661
1662 while (*p)
1663 {
1664 copy_option_part(&p, buf, sizeof(buf), ",");
1665 if (count >= 3)
1666 return e_invalid_argument;
1667
1668 if (STRCMP(buf, "abbr") == 0)
1669 {
1670 if (seen[CPT_ABBR])
1671 return e_invalid_argument;
1672 new_cia_flags = new_cia_flags * 10 + CPT_ABBR;
1673 seen[CPT_ABBR] = TRUE;
1674 count++;
1675 }
1676 else if (STRCMP(buf, "kind") == 0)
1677 {
1678 if (seen[CPT_KIND])
1679 return e_invalid_argument;
1680 new_cia_flags = new_cia_flags * 10 + CPT_KIND;
1681 seen[CPT_KIND] = TRUE;
1682 count++;
1683 }
1684 else if (STRCMP(buf, "menu") == 0)
1685 {
1686 if (seen[CPT_MENU])
1687 return e_invalid_argument;
1688 new_cia_flags = new_cia_flags * 10 + CPT_MENU;
1689 seen[CPT_MENU] = TRUE;
1690 count++;
1691 }
1692 else
1693 return e_invalid_argument;
1694 }
1695 if (new_cia_flags == 0 || count != 3)
1696 return e_invalid_argument;
1697
1698 cia_flags = new_cia_flags;
1699 return NULL;
1700}
1701
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001702#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1703/*
1704 * The 'completepopup' option is changed.
1705 */
1706 char *
1707did_set_completepopup(optset_T *args UNUSED)
1708{
1709 if (parse_completepopup(NULL) == FAIL)
1710 return e_invalid_argument;
1711
1712 popup_close_info();
1713 return NULL;
1714}
1715#endif
1716
1717#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1718/*
1719 * The 'completeslash' option is changed.
1720 */
1721 char *
1722did_set_completeslash(optset_T *args UNUSED)
1723{
1724 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1725 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
1726 return e_invalid_argument;
1727
1728 return NULL;
1729}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001730
1731 int
1732expand_set_completeslash(optexpand_T *args, int *numMatches, char_u ***matches)
1733{
1734 return expand_set_opt_string(
1735 args,
1736 p_csl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001737 ARRAY_LENGTH(p_csl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001738 numMatches,
1739 matches);
1740}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001741#endif
1742
1743#if defined(FEAT_CONCEAL) || defined(PROTO)
1744/*
1745 * The 'concealcursor' option is changed.
1746 */
1747 char *
1748did_set_concealcursor(optset_T *args)
1749{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001750 char_u **varp = (char_u **)args->os_varp;
1751
Christian Brabandtb39b2402023-11-29 11:34:05 +01001752 return did_set_option_listflag(*varp, (char_u *)COCU_ALL, args->os_errbuf,
1753 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001754}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001755
1756 int
1757expand_set_concealcursor(optexpand_T *args, int *numMatches, char_u ***matches)
1758{
1759 return expand_set_opt_listflag(args, (char_u*)COCU_ALL, numMatches, matches);
1760}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001761#endif
1762
1763/*
1764 * The 'cpoptions' option is changed.
1765 */
1766 char *
1767did_set_cpoptions(optset_T *args)
1768{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001769 char_u **varp = (char_u **)args->os_varp;
1770
Christian Brabandtb39b2402023-11-29 11:34:05 +01001771 return did_set_option_listflag(*varp, (char_u *)CPO_ALL, args->os_errbuf,
1772 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001773}
1774
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001775 int
1776expand_set_cpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
1777{
1778 return expand_set_opt_listflag(args, (char_u*)CPO_ALL, numMatches, matches);
1779}
1780
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001781#if defined(FEAT_CRYPT) || defined(PROTO)
1782/*
1783 * The 'cryptkey' option is changed.
1784 */
1785 char *
1786did_set_cryptkey(optset_T *args)
1787{
1788 // Make sure the ":set" command doesn't show the new value in the
1789 // history.
1790 remove_key_from_history();
1791
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02001792 if (args->os_op != OP_NONE)
1793 // Don't allow set+=/-=/^= as they can allow for substring guessing
1794 return e_invalid_argument;
1795
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001796 if (STRCMP(curbuf->b_p_key, args->os_oldval.string) != 0)
1797 {
1798 // Need to update the swapfile.
1799 ml_set_crypt_key(curbuf, args->os_oldval.string,
1800 *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
1801 changed_internal();
1802 }
Christian Brabandt19e6c4f2023-06-27 18:57:10 +01001803# ifdef FEAT_SODIUM
1804 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
1805 crypt_sodium_lock_key(args->os_newval.string);
1806# endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001807
1808 return NULL;
1809}
1810
1811/*
1812 * The 'cryptmethod' option is changed.
1813 */
1814 char *
1815did_set_cryptmethod(optset_T *args)
1816{
1817 char_u *p;
1818 char_u *s;
1819
1820 if (args->os_flags & OPT_LOCAL)
1821 p = curbuf->b_p_cm;
1822 else
1823 p = p_cm;
1824 if (check_opt_strings(p, p_cm_values, TRUE) != OK)
1825 return e_invalid_argument;
1826 else if (crypt_self_test() == FAIL)
1827 return e_invalid_argument;
1828
1829 // When setting the global value to empty, make it "zip".
1830 if (*p_cm == NUL)
1831 {
1832 free_string_option(p_cm);
1833 p_cm = vim_strsave((char_u *)"zip");
1834 }
1835 // When using ":set cm=name" the local value is going to be empty.
1836 // Do that here, otherwise the crypt functions will still use the
1837 // local value.
1838 if ((args->os_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1839 {
1840 free_string_option(curbuf->b_p_cm);
1841 curbuf->b_p_cm = empty_option;
1842 }
1843
1844 // Need to update the swapfile when the effective method changed.
1845 // Set "s" to the effective old value, "p" to the effective new
1846 // method and compare.
1847 if ((args->os_flags & OPT_LOCAL) && *args->os_oldval.string == NUL)
1848 s = p_cm; // was previously using the global value
1849 else
1850 s = args->os_oldval.string;
1851 if (*curbuf->b_p_cm == NUL)
1852 p = p_cm; // is now using the global value
1853 else
1854 p = curbuf->b_p_cm;
1855 if (STRCMP(s, p) != 0)
1856 ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1857
1858 // If the global value changes need to update the swapfile for all
1859 // buffers using that value.
1860 if ((args->os_flags & OPT_GLOBAL)
1861 && STRCMP(p_cm, args->os_oldval.string) != 0)
1862 {
1863 buf_T *buf;
1864
1865 FOR_ALL_BUFFERS(buf)
1866 if (buf != curbuf && *buf->b_p_cm == NUL)
1867 ml_set_crypt_key(buf, buf->b_p_key, args->os_oldval.string);
1868 }
1869 return NULL;
1870}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001871
1872 int
1873expand_set_cryptmethod(optexpand_T *args, int *numMatches, char_u ***matches)
1874{
1875 return expand_set_opt_string(
1876 args,
1877 p_cm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001878 ARRAY_LENGTH(p_cm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001879 numMatches,
1880 matches);
1881}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001882#endif
1883
1884#if (defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1885/*
1886 * The 'cscopequickfix' option is changed.
1887 */
1888 char *
1889did_set_cscopequickfix(optset_T *args UNUSED)
1890{
1891 char_u *p;
1892
1893 if (p_csqf == NULL)
1894 return NULL;
1895
1896 p = p_csqf;
1897 while (*p != NUL)
1898 {
1899 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
1900 || p[1] == NUL
1901 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
1902 || (p[2] != NUL && p[2] != ','))
1903 return e_invalid_argument;
1904 else if (p[2] == NUL)
1905 break;
1906 else
1907 p += 3;
1908 }
1909
1910 return NULL;
1911}
1912#endif
1913
1914#if defined(FEAT_SYN_HL) || defined(PROTO)
1915/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001916 * The 'cursorlineopt' option is changed.
1917 */
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001918 char *
1919did_set_cursorlineopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001920{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001921 char_u **varp = (char_u **)args->os_varp;
1922
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001923 // This could be changed to use opt_strings_flags() instead.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001924 if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001925 return e_invalid_argument;
1926
1927 return NULL;
1928}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001929
1930 int
1931expand_set_cursorlineopt(optexpand_T *args, int *numMatches, char_u ***matches)
1932{
1933 return expand_set_opt_string(
1934 args,
1935 p_culopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001936 ARRAY_LENGTH(p_culopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001937 numMatches,
1938 matches);
1939}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001940#endif
1941
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001942/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001943 * The 'debug' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001944 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001945 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001946did_set_debug(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001947{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001948 return did_set_opt_strings(p_debug, p_debug_values, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001949}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001950
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001951 int
1952expand_set_debug(optexpand_T *args, int *numMatches, char_u ***matches)
1953{
1954 return expand_set_opt_string(
1955 args,
1956 p_debug_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001957 ARRAY_LENGTH(p_debug_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001958 numMatches,
1959 matches);
1960}
1961
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001962#if defined(FEAT_DIFF) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001963/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001964 * The 'diffopt' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001965 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001966 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001967did_set_diffopt(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001968{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001969 if (diffopt_changed() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001970 return e_invalid_argument;
1971
1972 return NULL;
1973}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001974
1975 int
1976expand_set_diffopt(optexpand_T *args, int *numMatches, char_u ***matches)
1977{
1978 expand_T *xp = args->oe_xp;
1979
1980 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
1981 {
1982 // Within "algorithm:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001983 int algo_len = (int)STRLEN("algorithm:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001984 if (xp->xp_pattern - args->oe_set_arg >= algo_len &&
1985 STRNCMP(xp->xp_pattern - algo_len, "algorithm:", algo_len) == 0)
1986 {
1987 return expand_set_opt_string(
1988 args,
1989 p_dip_algorithm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001990 ARRAY_LENGTH(p_dip_algorithm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001991 numMatches,
1992 matches);
1993 }
1994 return FAIL;
1995 }
1996
1997 return expand_set_opt_string(
1998 args,
1999 p_dip_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002000 ARRAY_LENGTH(p_dip_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002001 numMatches,
2002 matches);
2003}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002004#endif
2005
2006/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002007 * The 'display' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002008 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002009 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002010did_set_display(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002011{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002012 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002013 return e_invalid_argument;
2014
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002015 (void)init_chartab();
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002016 return NULL;
2017}
2018
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002019 int
2020expand_set_display(optexpand_T *args, int *numMatches, char_u ***matches)
2021{
2022 return expand_set_opt_string(
2023 args,
2024 p_dy_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002025 ARRAY_LENGTH(p_dy_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002026 numMatches,
2027 matches);
2028}
2029
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002030/*
2031 * The 'eadirection' option is changed.
2032 */
2033 char *
2034did_set_eadirection(optset_T *args UNUSED)
2035{
2036 return did_set_opt_strings(p_ead, p_ead_values, FALSE);
2037}
2038
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002039 int
2040expand_set_eadirection(optexpand_T *args, int *numMatches, char_u ***matches)
2041{
2042 return expand_set_opt_string(
2043 args,
2044 p_ead_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002045 ARRAY_LENGTH(p_ead_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002046 numMatches,
2047 matches);
2048}
2049
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002050/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002051 * One of the 'encoding', 'fileencoding', 'termencoding' or 'makeencoding'
2052 * options is changed.
2053 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002054 char *
2055did_set_encoding(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002056{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002057 char_u **varp = (char_u **)args->os_varp;
2058 char_u **gvarp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002059 char *errmsg = NULL;
2060 char_u *p;
2061
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002062 // Get the global option to compare with, otherwise we would have to check
2063 // two values for all local options.
2064 gvarp = (char_u **)get_option_varp_scope(args->os_idx, OPT_GLOBAL);
2065
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002066 if (gvarp == &p_fenc)
2067 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002068 if (!curbuf->b_p_ma && args->os_flags != OPT_GLOBAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002069 errmsg = e_cannot_make_changes_modifiable_is_off;
2070 else if (vim_strchr(*varp, ',') != NULL)
2071 // No comma allowed in 'fileencoding'; catches confusing it
2072 // with 'fileencodings'.
2073 errmsg = e_invalid_argument;
2074 else
2075 {
2076 // May show a "+" in the title now.
2077 redraw_titles();
2078 // Add 'fileencoding' to the swap file.
2079 ml_setflags(curbuf);
2080 }
2081 }
2082 if (errmsg == NULL)
2083 {
2084 // canonize the value, so that STRCMP() can be used on it
2085 p = enc_canonize(*varp);
2086 if (p != NULL)
2087 {
2088 vim_free(*varp);
2089 *varp = p;
2090 }
2091 if (varp == &p_enc)
2092 {
2093 errmsg = mb_init();
2094 redraw_titles();
2095 }
2096 }
2097
2098#if defined(FEAT_GUI_GTK)
2099 if (errmsg == NULL && varp == &p_tenc && gui.in_use)
2100 {
2101 // GTK uses only a single encoding, and that is UTF-8.
2102 if (STRCMP(p_tenc, "utf-8") != 0)
2103 errmsg = e_cannot_be_changed_in_gtk_GUI;
2104 }
2105#endif
2106
2107 if (errmsg == NULL)
2108 {
2109#ifdef FEAT_KEYMAP
2110 // When 'keymap' is used and 'encoding' changes, reload the keymap
2111 // (with another encoding).
2112 if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
2113 (void)keymap_init();
2114#endif
2115
2116 // When 'termencoding' is not empty and 'encoding' changes or when
2117 // 'termencoding' changes, need to setup for keyboard input and
2118 // display output conversion.
2119 if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
2120 {
2121 if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
2122 || convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
2123 {
2124 semsg(_(e_cannot_convert_between_str_and_str),
2125 p_tenc, p_enc);
2126 errmsg = e_invalid_argument;
2127 }
2128 }
2129
2130#if defined(MSWIN)
K.Takatace3189d2023-02-15 19:13:43 +00002131 // $HOME, $VIM and $VIMRUNTIME may have characters in active code page.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002132 if (varp == &p_enc)
K.Takatace3189d2023-02-15 19:13:43 +00002133 {
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002134 init_homedir();
K.Takatace3189d2023-02-15 19:13:43 +00002135 init_vimdir();
2136 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002137#endif
2138 }
2139
2140 return errmsg;
2141}
2142
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002143 int
2144expand_set_encoding(optexpand_T *args, int *numMatches, char_u ***matches)
2145{
2146 return expand_set_opt_generic(
2147 args,
2148 get_encoding_name,
2149 numMatches,
2150 matches);
2151}
2152
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002153/*
Luuk van Baalb7147f82025-02-08 18:52:39 +01002154 * The 'eventignore(win)' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002155 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002156 char *
Luuk van Baalb7147f82025-02-08 18:52:39 +01002157did_set_eventignore(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002158{
Luuk van Baalb7147f82025-02-08 18:52:39 +01002159 char_u **varp = (char_u **)args->os_varp;
2160
2161 if (check_ei(*varp) == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002162 return e_invalid_argument;
2163 return NULL;
2164}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002165
Luuk van Baalb7147f82025-02-08 18:52:39 +01002166static int expand_eiw = FALSE;
2167
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002168 static char_u *
2169get_eventignore_name(expand_T *xp, int idx)
2170{
Luuk van Baalb7147f82025-02-08 18:52:39 +01002171 // 'eventignore(win)' allows special keyword "all" in addition to
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002172 // all event names.
2173 if (idx == 0)
2174 return (char_u *)"all";
Luuk van Baalb7147f82025-02-08 18:52:39 +01002175 return get_event_name_no_group(xp, idx - 1, expand_eiw);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002176}
2177
2178 int
2179expand_set_eventignore(optexpand_T *args, int *numMatches, char_u ***matches)
2180{
Luuk van Baalb7147f82025-02-08 18:52:39 +01002181 expand_eiw = args->oe_varp != (char_u *)&p_ei;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002182 return expand_set_opt_generic(
2183 args,
2184 get_eventignore_name,
2185 numMatches,
2186 matches);
2187}
2188
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002189/*
2190 * The 'fileformat' option is changed.
2191 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002192 char *
2193did_set_fileformat(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002194{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002195 char_u **varp = (char_u **)args->os_varp;
2196
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002197 if (!curbuf->b_p_ma && !(args->os_flags & OPT_GLOBAL))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002198 return e_cannot_make_changes_modifiable_is_off;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002199 else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002200 return e_invalid_argument;
2201
2202 // may also change 'textmode'
2203 if (get_fileformat(curbuf) == EOL_DOS)
2204 curbuf->b_p_tx = TRUE;
2205 else
2206 curbuf->b_p_tx = FALSE;
2207 redraw_titles();
2208 // update flag in swap file
2209 ml_setflags(curbuf);
2210 // Redraw needed when switching to/from "mac": a CR in the text
2211 // will be displayed differently.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002212 if (get_fileformat(curbuf) == EOL_MAC || *args->os_oldval.string == 'm')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002213 redraw_curbuf_later(UPD_NOT_VALID);
2214
2215 return NULL;
2216}
2217
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002218 int
2219expand_set_fileformat(optexpand_T *args, int *numMatches, char_u ***matches)
2220{
2221 return expand_set_opt_string(
2222 args,
2223 p_ff_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002224 ARRAY_LENGTH(p_ff_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002225 numMatches,
2226 matches);
2227}
2228
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002229/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002230 * Function given to ExpandGeneric() to obtain the possible arguments of the
2231 * fileformat options.
2232 */
2233 char_u *
2234get_fileformat_name(expand_T *xp UNUSED, int idx)
2235{
2236 if (idx >= (int)ARRAY_LENGTH(p_ff_values))
2237 return NULL;
2238
2239 return (char_u*)p_ff_values[idx];
2240}
2241
2242/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002243 * The 'fileformats' option is changed.
2244 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002245 char *
2246did_set_fileformats(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002247{
2248 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
2249 return e_invalid_argument;
2250
2251 // also change 'textauto'
2252 if (*p_ffs == NUL)
2253 p_ta = FALSE;
2254 else
2255 p_ta = TRUE;
2256
2257 return NULL;
2258}
2259
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002260/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002261 * The 'filetype' or the 'syntax' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002262 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002263 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002264did_set_filetype_or_syntax(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002265{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002266 char_u **varp = (char_u **)args->os_varp;
2267
2268 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002269 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002270
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002271 args->os_value_changed = STRCMP(args->os_oldval.string, *varp) != 0;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002272
2273 // Since we check the value, there is no need to set P_INSECURE,
2274 // even when the value comes from a modeline.
2275 args->os_value_checked = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002276
2277 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002278}
2279
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002280#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002281/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002282 * The 'foldclose' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002283 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002284 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002285did_set_foldclose(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002286{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002287 return did_set_opt_strings(p_fcl, p_fcl_values, TRUE);
2288}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002289
2290 int
2291expand_set_foldclose(optexpand_T *args, int *numMatches, char_u ***matches)
2292{
2293 return expand_set_opt_string(
2294 args,
2295 p_fcl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002296 ARRAY_LENGTH(p_fcl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002297 numMatches,
2298 matches);
2299}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002300#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002301
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002302#if (defined(FEAT_EVAL) && defined(FEAT_FOLDING)) || defined(PROTO)
2303/*
2304 * The 'foldexpr' option is changed.
2305 */
2306 char *
2307did_set_foldexpr(optset_T *args)
2308{
2309 (void)did_set_optexpr(args);
2310 if (foldmethodIsExpr(curwin))
2311 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002312 return NULL;
2313}
2314#endif
2315
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002316#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002317/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002318 * The 'foldignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002319 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002320 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002321did_set_foldignore(optset_T *args UNUSED)
2322{
2323 if (foldmethodIsIndent(curwin))
2324 foldUpdateAll(curwin);
2325 return NULL;
2326}
2327
2328/*
2329 * The 'foldmarker' option is changed.
2330 */
2331 char *
2332did_set_foldmarker(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002333{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002334 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002335 char_u *p;
2336
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002337 p = vim_strchr(*varp, ',');
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002338 if (p == NULL)
2339 return e_comma_required;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002340 else if (p == *varp || p[1] == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002341 return e_invalid_argument;
2342 else if (foldmethodIsMarker(curwin))
2343 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002344
2345 return NULL;
2346}
2347
2348/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002349 * The 'foldmethod' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002350 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002351 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002352did_set_foldmethod(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002353{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002354 char_u **varp = (char_u **)args->os_varp;
2355
Milly142cad12024-10-22 22:11:51 +02002356 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK || **varp == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002357 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002358
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002359 foldUpdateAll(curwin);
2360 if (foldmethodIsDiff(curwin))
2361 newFoldLevel();
2362 return NULL;
2363}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002364
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002365 int
2366expand_set_foldmethod(optexpand_T *args, int *numMatches, char_u ***matches)
2367{
2368 return expand_set_opt_string(
2369 args,
2370 p_fdm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002371 ARRAY_LENGTH(p_fdm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002372 numMatches,
2373 matches);
2374}
2375
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002376/*
2377 * The 'foldopen' option is changed.
2378 */
2379 char *
2380did_set_foldopen(optset_T *args UNUSED)
2381{
2382 return did_set_opt_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
2383}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002384
2385 int
2386expand_set_foldopen(optexpand_T *args, int *numMatches, char_u ***matches)
2387{
2388 return expand_set_opt_string(
2389 args,
2390 p_fdo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002391 ARRAY_LENGTH(p_fdo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002392 numMatches,
2393 matches);
2394}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002395#endif
2396
2397/*
2398 * The 'formatoptions' option is changed.
2399 */
2400 char *
2401did_set_formatoptions(optset_T *args)
2402{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002403 char_u **varp = (char_u **)args->os_varp;
2404
Christian Brabandtb39b2402023-11-29 11:34:05 +01002405 return did_set_option_listflag(*varp, (char_u *)FO_ALL, args->os_errbuf,
2406 args->os_errbuflen);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002407}
2408
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002409 int
2410expand_set_formatoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2411{
2412 return expand_set_opt_listflag(args, (char_u*)FO_ALL, numMatches, matches);
2413}
2414
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002415#if defined(CURSOR_SHAPE) || defined(PROTO)
2416/*
2417 * The 'guicursor' option is changed.
2418 */
2419 char *
2420did_set_guicursor(optset_T *args UNUSED)
2421{
2422 return parse_shape_opt(SHAPE_CURSOR);
2423}
2424#endif
2425
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002426#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002427/*
2428 * The 'guifont' option is changed.
2429 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002430 char *
2431did_set_guifont(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002432{
2433 char_u *p;
2434 char *errmsg = NULL;
2435
2436 if (gui.in_use)
2437 {
2438 p = p_guifont;
2439# if defined(FEAT_GUI_GTK)
2440 // Put up a font dialog and let the user select a new value.
2441 // If this is cancelled go back to the old value but don't
2442 // give an error message.
2443 if (STRCMP(p, "*") == 0)
2444 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002445 p = gui_mch_font_dialog(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002446 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002447 p_guifont = (p != NULL) ? p : vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002448 }
2449# endif
2450 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
2451 {
2452# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
2453 if (STRCMP(p_guifont, "*") == 0)
2454 {
2455 // Dialog was cancelled: Keep the old value without giving
2456 // an error message.
2457 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002458 p_guifont = vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002459 }
2460 else
2461# endif
2462 errmsg = e_invalid_fonts;
2463 }
2464 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002465
2466 return errmsg;
2467}
2468
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002469/*
2470 * Expand the 'guifont' option. Only when GUI is being used. Each platform has
2471 * specific behaviors.
2472 */
2473 int
2474expand_set_guifont(optexpand_T *args, int *numMatches, char_u ***matches)
2475{
2476 if (!gui.in_use)
2477 return FAIL;
2478
2479# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
2480 char_u **varp = (char_u **)args->oe_varp;
2481 int wide = (varp == &p_guifontwide);
2482
2483 return expand_set_opt_callback(
2484 args, gui_mch_expand_font, &wide, numMatches, matches);
2485# else
2486 return FAIL;
2487# endif
2488}
2489
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002490# if defined(FEAT_XFONTSET) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002491/*
2492 * The 'guifontset' option is changed.
2493 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002494 char *
2495did_set_guifontset(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002496{
2497 char *errmsg = NULL;
2498
2499 if (STRCMP(p_guifontset, "*") == 0)
2500 errmsg = e_cant_select_fontset;
2501 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
2502 errmsg = e_invalid_fontset;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002503
2504 return errmsg;
2505}
2506# endif
2507
2508/*
2509 * The 'guifontwide' option is changed.
2510 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002511 char *
2512did_set_guifontwide(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002513{
2514 char *errmsg = NULL;
2515
2516 if (STRCMP(p_guifontwide, "*") == 0)
2517 errmsg = e_cant_select_wide_font;
2518 else if (gui_get_wide_font() == FAIL)
2519 errmsg = e_invalid_wide_font;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002520
2521 return errmsg;
2522}
2523#endif
2524
Erik S. V. Jansson8b1e7492024-02-24 14:26:52 +01002525#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002526/*
2527 * The 'guiligatures' option is changed.
2528 */
2529 char *
2530did_set_guiligatures(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002531{
2532 gui_set_ligatures();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002533 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002534}
2535#endif
2536
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002537#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002538/*
2539 * The 'guioptions' option is changed.
2540 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002541 char *
2542did_set_guioptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002543{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002544 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002545 char *errmsg;
2546
Christian Brabandtb39b2402023-11-29 11:34:05 +01002547 errmsg = did_set_option_listflag(*varp, (char_u *)GO_ALL, args->os_errbuf,
2548 args->os_errbuflen);
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002549 if (errmsg != NULL)
2550 return errmsg;
2551
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002552 gui_init_which_components(args->os_oldval.string);
2553 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002554}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002555
2556 int
2557expand_set_guioptions(optexpand_T *args, int *numMatches, char_u ***matches)
2558{
2559 return expand_set_opt_listflag(args, (char_u*)GO_ALL, numMatches, matches);
2560}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002561#endif
2562
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002563#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002564/*
2565 * The 'guitablabel' option is changed.
2566 */
2567 char *
2568did_set_guitablabel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002569{
2570 redraw_tabline = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002571 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002572}
2573#endif
2574
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002575/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002576 * The 'helpfile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002577 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002578 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002579did_set_helpfile(optset_T *args UNUSED)
2580{
2581 // May compute new values for $VIM and $VIMRUNTIME
2582 if (didset_vim)
2583 vim_unsetenv_ext((char_u *)"VIM");
2584 if (didset_vimruntime)
2585 vim_unsetenv_ext((char_u *)"VIMRUNTIME");
2586 return NULL;
2587}
2588
2589#if defined(FEAT_MULTI_LANG) || defined(PROTO)
2590/*
2591 * The 'helplang' option is changed.
2592 */
2593 char *
2594did_set_helplang(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002595{
2596 char *errmsg = NULL;
2597
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002598 // Check for "", "ab", "ab,cd", etc.
2599 for (char_u *s = p_hlg; *s != NUL; s += 3)
2600 {
2601 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
2602 {
2603 errmsg = e_invalid_argument;
2604 break;
2605 }
2606 if (s[2] == NUL)
2607 break;
2608 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002609
2610 return errmsg;
2611}
2612#endif
2613
2614/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002615 * The 'highlight' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002616 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002617 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002618did_set_highlight(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002619{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002620 if (highlight_changed() == FAIL)
2621 return e_invalid_argument; // invalid flags
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002622
2623 return NULL;
2624}
2625
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002626/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002627 * Expand 'highlight' option.
2628 */
2629 int
2630expand_set_highlight(optexpand_T *args, int *numMatches, char_u ***matches)
2631{
2632 char_u *p;
2633 expand_T *xp = args->oe_xp;
2634 static char_u hl_flags[HLF_COUNT] = HL_FLAGS;
Christian Brabandt3f168ec2023-10-02 23:21:11 +02002635 size_t i;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002636 int count = 0;
2637
2638 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2639 {
2640 // Right after a ':', meaning we just return all highlight names.
2641 return expand_set_opt_generic(
2642 args,
2643 get_highlight_name,
2644 numMatches,
2645 matches);
2646 }
2647
2648 if (*xp->xp_pattern == NUL)
2649 {
2650 // At beginning of a comma-separated list. Return the specific list of
2651 // supported occasions.
2652 *matches = ALLOC_MULT(char_u *, HLF_COUNT + 1);
2653 if (*matches == NULL)
2654 return FAIL;
2655
2656 // We still want to return the full option if it's requested.
2657 if (args->oe_include_orig_val)
2658 {
2659 p = vim_strsave(args->oe_opt_value);
2660 if (p == NULL)
2661 {
2662 VIM_CLEAR(*matches);
2663 return FAIL;
2664 }
2665 (*matches)[count++] = p;
2666 }
2667
2668 for (i = 0; i < HLF_COUNT; i++)
2669 {
2670 p = vim_strnsave(&hl_flags[i], 1);
2671 if (p == NULL)
2672 {
2673 if (count == 0)
2674 {
2675 VIM_CLEAR(*matches);
2676 return FAIL;
2677 }
2678 else
2679 break;
2680 }
2681 (*matches)[count++] = p;
2682 }
2683
2684 if (count == 0)
2685 {
2686 VIM_CLEAR(*matches);
2687 return FAIL;
2688 }
2689 *numMatches = count;
2690 return OK;
2691 }
2692
2693 // We are after the initial character (which indicates the occasion). We
2694 // already made sure we are not matching after a ':' above, so now we want
2695 // to match against display mode modifiers.
2696 // Since the xp_pattern starts from the beginning, we need to include it in
2697 // the returned match.
2698
2699 // Note: Keep this in sync with highlight_changed()
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002700 static char_u p_hl_mode_values[] =
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002701 {':', 'b', 'i', '-', 'n', 'r', 's', 'u', 'c', '2', 'd', '=', 't'};
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002702 size_t num_hl_modes = ARRAY_LENGTH(p_hl_mode_values);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002703
2704 *matches = ALLOC_MULT(char_u *, num_hl_modes);
2705 if (*matches == NULL)
2706 return FAIL;
2707
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002708 int pattern_len = xp->xp_pattern_len;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002709
2710 for (i = 0; i < num_hl_modes; i++)
2711 {
2712 // Don't allow duplicates as these are unique flags
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002713 char_u *dup = vim_strchr(xp->xp_pattern + 1, p_hl_mode_values[i]);
2714 if (dup != NULL && (int)(dup - xp->xp_pattern) < pattern_len)
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002715 continue;
2716
2717 // ':' only works by itself, not with other flags.
2718 if (pattern_len > 1 && p_hl_mode_values[i] == ':')
2719 continue;
2720
2721 p = vim_strnsave(xp->xp_pattern, pattern_len + 1);
2722 if (p == NULL)
2723 {
2724 if (i == 0)
2725 {
2726 VIM_CLEAR(*matches);
2727 return FAIL;
2728 }
2729 else
2730 break;
2731 }
2732 p[pattern_len] = p_hl_mode_values[i];
2733 p[pattern_len + 1] = NUL;
2734 (*matches)[count++] = p;
2735 }
2736 if (count == 0)
2737 {
2738 VIM_CLEAR(*matches);
2739 return FAIL;
2740 }
2741 *numMatches = count;
2742
2743 return OK;
2744}
2745
2746/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002747 * The 'titlestring' or the 'iconstring' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002748 */
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002749 static char *
2750parse_titleiconstring(optset_T *args UNUSED, int flagval UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002751{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002752#ifdef FEAT_STL_OPT
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002753 char_u **varp = (char_u **)args->os_varp;
2754
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002755 // NULL => statusline syntax
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002756 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002757 stl_syntax |= flagval;
2758 else
2759 stl_syntax &= ~flagval;
2760#endif
2761 did_set_title();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002762
2763 return NULL;
2764}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002765
2766/*
2767 * The 'iconstring' option is changed.
2768 */
2769 char *
2770did_set_iconstring(optset_T *args)
2771{
2772 int flagval = 0;
2773
2774#ifdef FEAT_STL_OPT
2775 flagval = STL_IN_ICON;
2776#endif
2777
2778 return parse_titleiconstring(args, flagval);
2779}
2780
2781#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
2782/*
2783 * The 'imactivatekey' option is changed.
2784 */
2785 char *
2786did_set_imactivatekey(optset_T *args UNUSED)
2787{
2788 if (!im_xim_isvalid_imactivate())
2789 return e_invalid_argument;
2790 return NULL;
2791}
2792#endif
2793
2794/*
Milly5e7a6a42024-10-22 22:27:19 +02002795 * The 'iskeyword' option is changed.
2796 */
2797 char *
2798did_set_iskeyword(optset_T *args)
2799{
2800 char_u **varp = (char_u **)args->os_varp;
2801
2802 if (varp == &p_isk) // only check for global-value
2803 {
2804 if (check_isopt(*varp) == FAIL)
2805 return e_invalid_argument;
2806 }
2807 else // fallthrough for local-value
2808 return did_set_isopt(args);
2809
2810 return NULL;
2811}
2812
2813/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002814 * The 'isident' or the 'iskeyword' or the 'isprint' or the 'isfname' option is
2815 * changed.
2816 */
2817 char *
2818did_set_isopt(optset_T *args)
2819{
Milly5e7a6a42024-10-22 22:27:19 +02002820 // 'isident', 'iskeyword', 'isprint' or 'isfname' option: refill g_chartab[]
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002821 // If the new option is invalid, use old value.
2822 // 'lisp' option: refill g_chartab[] for '-' char.
2823 if (init_chartab() == FAIL)
2824 {
2825 args->os_restore_chartab = TRUE;// need to restore the chartab.
2826 return e_invalid_argument; // error in value
2827 }
2828
2829 return NULL;
2830}
2831
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002832/*
2833 * The 'jumpoptions' option is changed.
2834 */
2835 char *
Yegappan Lakshmanan1926ae42023-09-21 16:36:28 +02002836did_set_jumpoptions(optset_T *args UNUSED)
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002837{
2838 if (opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE) != OK)
2839 return e_invalid_argument;
2840
2841 return NULL;
2842}
2843
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002844 int
2845expand_set_jumpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2846{
2847 return expand_set_opt_string(
2848 args,
2849 p_jop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002850 ARRAY_LENGTH(p_jop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002851 numMatches,
2852 matches);
2853}
2854
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002855#if defined(FEAT_KEYMAP) || defined(PROTO)
2856/*
2857 * The 'keymap' option is changed.
2858 */
2859 char *
2860did_set_keymap(optset_T *args)
2861{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002862 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002863 char *errmsg = NULL;
2864
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002865 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002866 errmsg = e_invalid_argument;
2867 else
2868 {
2869 int secure_save = secure;
2870
2871 // Reset the secure flag, since the value of 'keymap' has
2872 // been checked to be safe.
2873 secure = 0;
2874
2875 // load or unload key mapping tables
2876 errmsg = keymap_init();
2877
2878 secure = secure_save;
2879
2880 // Since we check the value, there is no need to set P_INSECURE,
2881 // even when the value comes from a modeline.
2882 args->os_value_checked = TRUE;
2883 }
2884
2885 if (errmsg == NULL)
2886 {
2887 if (*curbuf->b_p_keymap != NUL)
2888 {
2889 // Installed a new keymap, switch on using it.
2890 curbuf->b_p_iminsert = B_IMODE_LMAP;
2891 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
2892 curbuf->b_p_imsearch = B_IMODE_LMAP;
2893 }
2894 else
2895 {
2896 // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
2897 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
2898 curbuf->b_p_iminsert = B_IMODE_NONE;
2899 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
2900 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
2901 }
2902 if ((args->os_flags & OPT_LOCAL) == 0)
2903 {
2904 set_iminsert_global();
2905 set_imsearch_global();
2906 }
2907 status_redraw_curbuf();
2908 }
2909
2910 return errmsg;
2911}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002912#endif
2913
2914/*
2915 * The 'keymodel' option is changed.
2916 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002917 char *
2918did_set_keymodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002919{
2920 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
2921 return e_invalid_argument;
2922
2923 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
2924 km_startsel = (vim_strchr(p_km, 'a') != NULL);
2925 return NULL;
2926}
2927
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002928 int
2929expand_set_keymodel(optexpand_T *args, int *numMatches, char_u ***matches)
2930{
2931 return expand_set_opt_string(
2932 args,
2933 p_km_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002934 ARRAY_LENGTH(p_km_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002935 numMatches,
2936 matches);
2937}
2938
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002939/*
2940 * The 'keyprotocol' option is changed.
2941 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002942 char *
2943did_set_keyprotocol(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002944{
Gregory Anders3695d0e2023-09-29 20:17:20 +02002945 char_u *term = T_NAME;
2946 keyprot_T kpc = match_keyprotocol(term);
2947 if (kpc == KEYPROTOCOL_FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002948 return e_invalid_argument;
2949
Gregory Anders3695d0e2023-09-29 20:17:20 +02002950 apply_keyprotocol(term, kpc);
2951
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002952 return NULL;
2953}
2954
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002955 int
2956expand_set_keyprotocol(optexpand_T *args, int *numMatches, char_u ***matches)
2957{
2958 expand_T *xp = args->oe_xp;
2959 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2960 {
2961 // 'keyprotocol' only has well-defined terms for completion for the
2962 // protocol part after the colon.
2963 return expand_set_opt_string(
2964 args,
2965 p_kpc_protocol_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002966 ARRAY_LENGTH(p_kpc_protocol_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002967 numMatches,
2968 matches);
2969 }
2970 // Use expand_set_opt_string instead of returning FAIL so that we can
2971 // include the original value if args->oe_include_orig_val is set.
2972 static char *(empty[]) = {NULL};
2973 return expand_set_opt_string(args, empty, 0, numMatches, matches);
2974}
2975
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002976/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002977 * The 'lispoptions' option is changed.
2978 */
2979 char *
2980did_set_lispoptions(optset_T *args)
2981{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002982 char_u **varp = (char_u **)args->os_varp;
2983
2984 if (**varp != NUL
2985 && STRCMP(*varp, "expr:0") != 0
2986 && STRCMP(*varp, "expr:1") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002987 return e_invalid_argument;
2988
2989 return NULL;
2990}
2991
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002992 int
2993expand_set_lispoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2994{
2995 static char *(p_lop_values[]) = {"expr:0", "expr:1", NULL};
2996 return expand_set_opt_string(
2997 args,
2998 p_lop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002999 ARRAY_LENGTH(p_lop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003000 numMatches,
3001 matches);
3002}
3003
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003004/*
3005 * The 'matchpairs' option is changed.
3006 */
3007 char *
3008did_set_matchpairs(optset_T *args)
3009{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003010 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003011 char_u *p;
3012
3013 if (has_mbyte)
3014 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003015 for (p = *varp; *p != NUL; ++p)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003016 {
3017 int x2 = -1;
3018 int x3 = -1;
3019
3020 p += mb_ptr2len(p);
3021 if (*p != NUL)
3022 x2 = *p++;
3023 if (*p != NUL)
3024 {
3025 x3 = mb_ptr2char(p);
3026 p += mb_ptr2len(p);
3027 }
3028 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
3029 return e_invalid_argument;
3030 if (*p == NUL)
3031 break;
3032 }
3033 }
3034 else
3035 {
3036 // Check for "x:y,x:y"
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003037 for (p = *varp; *p != NUL; p += 4)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003038 {
3039 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
3040 return e_invalid_argument;
3041 if (p[3] == NUL)
3042 break;
3043 }
3044 }
3045
3046 return NULL;
3047}
3048
Christian Brabandt51d4d842024-12-06 17:26:25 +01003049/*
3050 * Process the updated 'messagesopt' option value.
3051 */
3052 char *
3053did_set_messagesopt(optset_T *args UNUSED)
3054{
3055 if (messagesopt_changed() == FAIL)
3056 return e_invalid_argument;
3057
3058 return NULL;
3059}
3060
3061 int
3062expand_set_messagesopt(optexpand_T *args, int *numMatches, char_u ***matches)
3063{
zeertzjq8cc43da2024-12-07 16:09:08 +01003064 static char *(p_mopt_values[]) = {"hit-enter", "wait:", "history:", NULL};
Christian Brabandt51d4d842024-12-06 17:26:25 +01003065 return expand_set_opt_string(
3066 args,
zeertzjq8cc43da2024-12-07 16:09:08 +01003067 p_mopt_values,
3068 ARRAY_LENGTH(p_mopt_values) - 1,
Christian Brabandt51d4d842024-12-06 17:26:25 +01003069 numMatches,
3070 matches);
3071}
3072
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003073#if defined(FEAT_SPELL) || defined(PROTO)
3074/*
3075 * The 'mkspellmem' option is changed.
3076 */
3077 char *
3078did_set_mkspellmem(optset_T *args UNUSED)
3079{
3080 if (spell_check_msm() != OK)
3081 return e_invalid_argument;
3082
3083 return NULL;
3084}
3085#endif
3086
3087/*
3088 * The 'mouse' option is changed.
3089 */
3090 char *
3091did_set_mouse(optset_T *args)
3092{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003093 char_u **varp = (char_u **)args->os_varp;
3094
Christian Brabandtb39b2402023-11-29 11:34:05 +01003095 return did_set_option_listflag(*varp, (char_u *)MOUSE_ALL, args->os_errbuf,
3096 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003097}
3098
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003099 int
3100expand_set_mouse(optexpand_T *args, int *numMatches, char_u ***matches)
3101{
3102 return expand_set_opt_listflag(args, (char_u*)MOUSE_ALL, numMatches, matches);
3103}
3104
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003105/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003106 * The 'mousemodel' option is changed.
3107 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003108 char *
3109did_set_mousemodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003110{
3111 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
3112 return e_invalid_argument;
3113#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
Ken Takata18d0d292023-12-19 20:12:29 +01003114 else if (*p_mousem != *args->os_oldval.string)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003115 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
3116 // to create or delete the popup menus.
3117 gui_motif_update_mousemodel(root_menu);
3118#endif
3119
3120 return NULL;
3121}
3122
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003123 int
3124expand_set_mousemodel(optexpand_T *args, int *numMatches, char_u ***matches)
3125{
3126 return expand_set_opt_string(
3127 args,
3128 p_mousem_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003129 ARRAY_LENGTH(p_mousem_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003130 numMatches,
3131 matches);
3132}
3133
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003134#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3135 char *
3136did_set_mouseshape(optset_T *args UNUSED)
3137{
3138 char *errmsg = NULL;
3139
3140 errmsg = parse_shape_opt(SHAPE_MOUSE);
3141 update_mouseshape(-1);
3142
3143 return errmsg;
3144}
3145#endif
3146
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003147/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003148 * The 'nrformats' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003149 */
3150 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003151did_set_nrformats(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003152{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003153 char_u **varp = (char_u **)args->os_varp;
3154
3155 return did_set_opt_strings(*varp, p_nf_values, TRUE);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003156}
3157
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003158 int
3159expand_set_nrformats(optexpand_T *args, int *numMatches, char_u ***matches)
3160{
3161 return expand_set_opt_string(
3162 args,
3163 p_nf_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003164 ARRAY_LENGTH(p_nf_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003165 numMatches,
3166 matches);
3167}
3168
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003169#if defined(FEAT_EVAL) || defined(PROTO)
3170/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003171 * One of the '*expr' options is changed: 'balloonexpr', 'diffexpr',
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003172 * 'foldexpr', 'foldtext', 'formatexpr', 'includeexpr', 'indentexpr',
3173 * 'patchexpr', 'printexpr' and 'charconvert'.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003174 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003175 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003176did_set_optexpr(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003177{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003178 char_u **varp = (char_u **)args->os_varp;
3179
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003180 // If the option value starts with <SID> or s:, then replace that with
3181 // the script identifier.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003182 char_u *name = get_scriptlocal_funcname(*varp);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003183 if (name != NULL)
3184 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003185 free_string_option(*varp);
3186 *varp = name;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003187 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003188
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003189 return NULL;
3190}
3191#endif
3192
3193/*
3194 * The 'pastetoggle' option is changed.
3195 */
3196 char *
3197did_set_pastetoggle(optset_T *args UNUSED)
3198{
3199 char_u *p;
3200
3201 // translate key codes like in a mapping
3202 if (*p_pt)
3203 {
zeertzjq7e0bae02023-08-11 23:15:38 +02003204 (void)replace_termcodes(p_pt, &p, 0,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003205 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
3206 if (p != NULL)
3207 {
3208 free_string_option(p_pt);
3209 p_pt = p;
3210 }
3211 }
3212
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003213 return NULL;
3214}
3215
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003216#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3217/*
3218 * The 'previewpopup' option is changed.
3219 */
3220 char *
3221did_set_previewpopup(optset_T *args UNUSED)
3222{
3223 if (parse_previewpopup(NULL) == FAIL)
3224 return e_invalid_argument;
3225
3226 return NULL;
3227}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003228
3229 int
3230expand_set_popupoption(optexpand_T *args, int *numMatches, char_u ***matches)
3231{
3232 expand_T *xp = args->oe_xp;
3233
3234 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3235 {
3236 // Within "highlight:"/"border:"/"align:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003237 int border_len = (int)STRLEN("border:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003238 if (xp->xp_pattern - args->oe_set_arg >= border_len &&
3239 STRNCMP(xp->xp_pattern - border_len, "border:", border_len) == 0)
3240 {
3241 return expand_set_opt_string(
3242 args,
3243 p_popup_option_border_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003244 ARRAY_LENGTH(p_popup_option_border_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003245 numMatches,
3246 matches);
3247 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003248 int align_len = (int)STRLEN("align:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003249 if (xp->xp_pattern - args->oe_set_arg >= align_len &&
3250 STRNCMP(xp->xp_pattern - align_len, "align:", align_len) == 0)
3251 {
3252 return expand_set_opt_string(
3253 args,
3254 p_popup_option_align_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003255 ARRAY_LENGTH(p_popup_option_align_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003256 numMatches,
3257 matches);
3258 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003259 int highlight_len = (int)STRLEN("highlight:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003260 if (xp->xp_pattern - args->oe_set_arg >= highlight_len &&
3261 STRNCMP(xp->xp_pattern - highlight_len, "highlight:", highlight_len) == 0)
3262 {
3263 // Return the list of all highlight names
3264 return expand_set_opt_generic(
3265 args,
3266 get_highlight_name,
3267 numMatches,
3268 matches);
3269 }
3270 return FAIL;
3271 }
3272
3273 return expand_set_opt_string(
3274 args,
3275 p_popup_option_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003276 ARRAY_LENGTH(p_popup_option_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003277 numMatches,
3278 matches);
3279}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003280#endif
3281
3282#if defined(FEAT_POSTSCRIPT) || defined(PROTO)
3283/*
3284 * The 'printencoding' option is changed.
3285 */
3286 char *
3287did_set_printencoding(optset_T *args UNUSED)
3288{
3289 char_u *s, *p;
3290
3291 // Canonize 'printencoding' if VIM standard one
3292 p = enc_canonize(p_penc);
3293 if (p != NULL)
3294 {
3295 vim_free(p_penc);
3296 p_penc = p;
3297 }
3298 else
3299 {
3300 // Ensure lower case and '-' for '_'
3301 for (s = p_penc; *s != NUL; s++)
3302 {
3303 if (*s == '_')
3304 *s = '-';
3305 else
3306 *s = TOLOWER_ASC(*s);
3307 }
3308 }
3309
3310 return NULL;
3311}
3312#endif
3313
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003314#if defined(FEAT_PRINTER) || defined(PROTO)
3315
3316 static char_u *
3317get_printoptions_names(expand_T *xp UNUSED, int idx)
3318{
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003319 if (idx >= (int)ARRAY_LENGTH(printer_opts))
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003320 return NULL;
3321 return (char_u*)printer_opts[idx].name;
3322}
3323
3324/*
3325 * Expand 'printoptions' option
3326 */
3327 int
3328expand_set_printoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3329{
3330 return expand_set_opt_generic(
3331 args,
3332 get_printoptions_names,
3333 numMatches,
3334 matches);
3335}
3336#endif
3337
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003338#if defined(FEAT_STL_OPT) || defined(PROTO)
3339/*
3340 * The 'statusline' or the 'tabline' or the 'rulerformat' option is changed.
3341 * "rulerformat" is TRUE if the 'rulerformat' option is changed.
3342 */
3343 static char *
3344parse_statustabline_rulerformat(optset_T *args, int rulerformat)
3345{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003346 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003347 char_u *s;
3348 char *errmsg = NULL;
3349 int wid;
3350
3351 if (rulerformat) // reset ru_wid first
3352 ru_wid = 0;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003353 s = *varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003354 if (rulerformat && *s == '%')
3355 {
3356 // set ru_wid if 'ruf' starts with "%99("
3357 if (*++s == '-') // ignore a '-'
3358 s++;
3359 wid = getdigits(&s);
3360 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
3361 ru_wid = wid;
3362 else
Yegappan Lakshmananac023e82024-11-27 20:55:45 +01003363 {
3364 // Validate the flags in 'rulerformat' only if it doesn't point to
3365 // a custom function ("%!" flag).
3366 if ((*varp)[1] != '!')
3367 errmsg = check_stl_option(p_ruf);
3368 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003369 }
3370 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
3371 else if (rulerformat || s[0] != '%' || s[1] != '!')
3372 errmsg = check_stl_option(s);
3373 if (rulerformat && errmsg == NULL)
3374 comp_col();
3375
3376 return errmsg;
3377}
3378#endif
3379
3380#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
3381/*
3382 * The 'renderoptions' option is changed.
3383 */
3384 char *
3385did_set_renderoptions(optset_T *args UNUSED)
3386{
3387 if (!gui_mch_set_rendering_options(p_rop))
3388 return e_invalid_argument;
3389
3390 return NULL;
3391}
3392#endif
3393
3394#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3395/*
3396 * The 'rightleftcmd' option is changed.
3397 */
3398 char *
3399did_set_rightleftcmd(optset_T *args)
3400{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003401 char_u **varp = (char_u **)args->os_varp;
3402
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003403 // Currently only "search" is a supported value.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003404 if (**varp != NUL && STRCMP(*varp, "search") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003405 return e_invalid_argument;
3406
3407 return NULL;
3408}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003409
3410 int
3411expand_set_rightleftcmd(optexpand_T *args, int *numMatches, char_u ***matches)
3412{
3413 static char *(p_rlc_values[]) = {"search", NULL};
3414 return expand_set_opt_string(
3415 args,
3416 p_rlc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003417 ARRAY_LENGTH(p_rlc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003418 numMatches,
3419 matches);
3420}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003421#endif
3422
3423#if defined(FEAT_STL_OPT) || defined(PROTO)
3424/*
3425 * The 'rulerformat' option is changed.
3426 */
3427 char *
3428did_set_rulerformat(optset_T *args)
3429{
3430 return parse_statustabline_rulerformat(args, TRUE);
3431}
3432#endif
3433
3434/*
3435 * The 'scrollopt' option is changed.
3436 */
3437 char *
3438did_set_scrollopt(optset_T *args UNUSED)
3439{
3440 return did_set_opt_strings(p_sbo, p_scbopt_values, TRUE);
3441}
3442
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003443 int
3444expand_set_scrollopt(optexpand_T *args, int *numMatches, char_u ***matches)
3445{
3446 return expand_set_opt_string(
3447 args,
3448 p_scbopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003449 ARRAY_LENGTH(p_scbopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003450 numMatches,
3451 matches);
3452}
3453
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003454/*
3455 * The 'selection' option is changed.
3456 */
3457 char *
3458did_set_selection(optset_T *args UNUSED)
3459{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003460 if (*p_sel == NUL || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003461 return e_invalid_argument;
3462
3463 return NULL;
3464}
3465
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003466 int
3467expand_set_selection(optexpand_T *args, int *numMatches, char_u ***matches)
3468{
3469 return expand_set_opt_string(
3470 args,
3471 p_sel_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003472 ARRAY_LENGTH(p_sel_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003473 numMatches,
3474 matches);
3475}
3476
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003477/*
3478 * The 'selectmode' option is changed.
3479 */
3480 char *
3481did_set_selectmode(optset_T *args UNUSED)
3482{
3483 return did_set_opt_strings(p_slm, p_slm_values, TRUE);
3484}
3485
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003486 int
3487expand_set_selectmode(optexpand_T *args, int *numMatches, char_u ***matches)
3488{
3489 return expand_set_opt_string(
3490 args,
3491 p_slm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003492 ARRAY_LENGTH(p_slm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003493 numMatches,
3494 matches);
3495}
3496
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003497#if defined(FEAT_SESSION) || defined(PROTO)
3498/*
3499 * The 'sessionoptions' option is changed.
3500 */
3501 char *
3502did_set_sessionoptions(optset_T *args)
3503{
3504 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
3505 return e_invalid_argument;
3506 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
3507 {
3508 // Don't allow both "sesdir" and "curdir".
3509 (void)opt_strings_flags(args->os_oldval.string, p_ssop_values,
3510 &ssop_flags, TRUE);
3511 return e_invalid_argument;
3512 }
3513
3514 return NULL;
3515}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003516
3517 int
3518expand_set_sessionoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3519{
3520 return expand_set_opt_string(
3521 args,
3522 p_ssop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003523 ARRAY_LENGTH(p_ssop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003524 numMatches,
3525 matches);
3526}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003527#endif
3528
3529/*
3530 * The 'shortmess' option is changed.
3531 */
3532 char *
3533did_set_shortmess(optset_T *args)
3534{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003535 char_u **varp = (char_u **)args->os_varp;
3536
Christian Brabandtb39b2402023-11-29 11:34:05 +01003537 return did_set_option_listflag(*varp, (char_u *)SHM_ALL, args->os_errbuf,
3538 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003539}
3540
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003541 int
3542expand_set_shortmess(optexpand_T *args, int *numMatches, char_u ***matches)
3543{
3544 return expand_set_opt_listflag(args, (char_u*)SHM_ALL, numMatches, matches);
3545}
3546
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003547#if defined(FEAT_LINEBREAK) || defined(PROTO)
3548/*
3549 * The 'showbreak' option is changed.
3550 */
3551 char *
3552did_set_showbreak(optset_T *args)
3553{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003554 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003555 char_u *s;
3556
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003557 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003558 {
3559 if (ptr2cells(s) != 1)
3560 return e_showbreak_contains_unprintable_or_wide_character;
3561 MB_PTR_ADV(s);
3562 }
3563
3564 return NULL;
3565}
3566#endif
3567
3568/*
3569 * The 'showcmdloc' option is changed.
3570 */
3571 char *
3572did_set_showcmdloc(optset_T *args UNUSED)
3573{
zeertzjqc27fcf42024-03-01 23:01:43 +01003574 char *errmsg = did_set_opt_strings(p_sloc, p_sloc_values, FALSE);
3575
3576 if (errmsg == NULL)
3577 comp_col();
3578
3579 return errmsg;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003580}
3581
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003582 int
3583expand_set_showcmdloc(optexpand_T *args, int *numMatches, char_u ***matches)
3584{
3585 return expand_set_opt_string(
3586 args,
3587 p_sloc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003588 ARRAY_LENGTH(p_sloc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003589 numMatches,
3590 matches);
3591}
3592
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003593#if defined(FEAT_SIGNS) || defined(PROTO)
3594/*
3595 * The 'signcolumn' option is changed.
3596 */
3597 char *
3598did_set_signcolumn(optset_T *args)
3599{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003600 char_u **varp = (char_u **)args->os_varp;
3601
3602 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003603 return e_invalid_argument;
3604 // When changing the 'signcolumn' to or from 'number', recompute the
3605 // width of the number column if 'number' or 'relativenumber' is set.
3606 if (((*args->os_oldval.string == 'n' && args->os_oldval.string[1] == 'u')
3607 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
3608 && (curwin->w_p_nu || curwin->w_p_rnu))
3609 curwin->w_nrwidth_line_count = 0;
3610
3611 return NULL;
3612}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003613
3614 int
3615expand_set_signcolumn(optexpand_T *args, int *numMatches, char_u ***matches)
3616{
3617 return expand_set_opt_string(
3618 args,
3619 p_scl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003620 ARRAY_LENGTH(p_scl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003621 numMatches,
3622 matches);
3623}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003624#endif
3625
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003626#if defined(FEAT_SPELL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003627/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003628 * The 'spellcapcheck' option is changed.
3629 */
3630 char *
3631did_set_spellcapcheck(optset_T *args UNUSED)
3632{
3633 // compile the regexp program.
3634 return compile_cap_prog(curwin->w_s);
3635}
3636
3637/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003638 * The 'spellfile' option is changed.
3639 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003640 char *
3641did_set_spellfile(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003642{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003643 char_u **varp = (char_u **)args->os_varp;
3644
3645 if (!valid_spellfile(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003646 return e_invalid_argument;
3647
3648 // If there is a window for this buffer in which 'spell' is set load the
3649 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003650 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003651}
3652
3653/*
3654 * The 'spell' option is changed.
3655 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003656 char *
3657did_set_spelllang(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003658{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003659 char_u **varp = (char_u **)args->os_varp;
3660
3661 if (!valid_spelllang(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003662 return e_invalid_argument;
3663
3664 // If there is a window for this buffer in which 'spell' is set load the
3665 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003666 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003667}
3668
3669/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003670 * The 'spelloptions' option is changed.
3671 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003672 char *
3673did_set_spelloptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003674{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003675 char_u **varp = (char_u **)args->os_varp;
3676
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003677 if (**varp != NUL && STRCMP(*varp, "camel") != 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003678 return e_invalid_argument;
3679
3680 return NULL;
3681}
3682
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003683 int
3684expand_set_spelloptions(optexpand_T *args, int *numMatches, char_u ***matches)
3685{
3686 static char *(p_spo_values[]) = {"camel", NULL};
3687 return expand_set_opt_string(
3688 args,
3689 p_spo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003690 ARRAY_LENGTH(p_spo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003691 numMatches,
3692 matches);
3693}
3694
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003695/*
3696 * The 'spellsuggest' option is changed.
3697 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003698 char *
3699did_set_spellsuggest(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003700{
3701 if (spell_check_sps() != OK)
3702 return e_invalid_argument;
3703
3704 return NULL;
3705}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003706
3707 int
3708expand_set_spellsuggest(optexpand_T *args, int *numMatches, char_u ***matches)
3709{
3710 return expand_set_opt_string(
3711 args,
3712 p_sps_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003713 ARRAY_LENGTH(p_sps_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003714 numMatches,
3715 matches);
3716}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003717#endif
3718
3719/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003720 * The 'splitkeep' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003721 */
3722 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003723did_set_splitkeep(optset_T *args UNUSED)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003724{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003725 return did_set_opt_strings(p_spk, p_spk_values, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003726}
3727
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003728 int
3729expand_set_splitkeep(optexpand_T *args, int *numMatches, char_u ***matches)
3730{
3731 return expand_set_opt_string(
3732 args,
3733 p_spk_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003734 ARRAY_LENGTH(p_spk_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003735 numMatches,
3736 matches);
3737}
3738
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00003739#if defined(FEAT_STL_OPT) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003740/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003741 * The 'statusline' option is changed.
3742 */
3743 char *
3744did_set_statusline(optset_T *args)
3745{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003746 return parse_statustabline_rulerformat(args, FALSE);
3747}
3748#endif
3749
3750/*
3751 * The 'swapsync' option is changed.
3752 */
3753 char *
3754did_set_swapsync(optset_T *args UNUSED)
3755{
3756 return did_set_opt_strings(p_sws, p_sws_values, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003757}
3758
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003759 int
3760expand_set_swapsync(optexpand_T *args, int *numMatches, char_u ***matches)
3761{
3762 return expand_set_opt_string(
3763 args,
3764 p_sws_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003765 ARRAY_LENGTH(p_sws_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003766 numMatches,
3767 matches);
3768}
3769
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003770/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003771 * The 'switchbuf' option is changed.
3772 */
3773 char *
3774did_set_switchbuf(optset_T *args UNUSED)
3775{
3776 return did_set_opt_flags(p_swb, p_swb_values, &swb_flags, TRUE);
3777}
3778
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003779 int
3780expand_set_switchbuf(optexpand_T *args, int *numMatches, char_u ***matches)
3781{
3782 return expand_set_opt_string(
3783 args,
3784 p_swb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003785 ARRAY_LENGTH(p_swb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003786 numMatches,
3787 matches);
3788}
3789
LemonBoy5247b0b2024-07-12 19:30:58 +02003790/*
3791 * The 'tabclose' option is changed.
3792 */
3793 char *
3794did_set_tabclose(optset_T *args UNUSED)
3795{
3796 return did_set_opt_flags(p_tcl, p_tcl_values, &tcl_flags, TRUE);
3797}
3798
3799 int
3800expand_set_tabclose(optexpand_T *args, int *numMatches, char_u ***matches)
3801{
3802 return expand_set_opt_string(
3803 args,
3804 p_tcl_values,
3805 ARRAY_LENGTH(p_tcl_values) - 1,
3806 numMatches,
3807 matches);
3808}
3809
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003810#if defined(FEAT_STL_OPT) || defined(PROTO)
3811/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003812 * The 'tabline' option is changed.
3813 */
3814 char *
3815did_set_tabline(optset_T *args)
3816{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003817 return parse_statustabline_rulerformat(args, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003818}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003819#endif
3820
3821/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003822 * The 'tagcase' option is changed.
3823 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003824 char *
3825did_set_tagcase(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003826{
3827 unsigned int *flags;
3828 char_u *p;
3829
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003830 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003831 {
3832 p = curbuf->b_p_tc;
3833 flags = &curbuf->b_tc_flags;
3834 }
3835 else
3836 {
3837 p = p_tc;
3838 flags = &tc_flags;
3839 }
3840
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003841 if ((args->os_flags & OPT_LOCAL) && *p == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003842 // make the local value empty: use the global value
3843 *flags = 0;
3844 else if (*p == NUL
3845 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
3846 return e_invalid_argument;
3847
3848 return NULL;
3849}
3850
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003851 int
3852expand_set_tagcase(optexpand_T *args, int *numMatches, char_u ***matches)
3853{
3854 return expand_set_opt_string(
3855 args,
3856 p_tc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003857 ARRAY_LENGTH(p_tc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003858 numMatches,
3859 matches);
3860}
3861
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003862/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003863 * The 'term' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003864 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003865 char *
3866did_set_term(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003867{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003868 if (T_NAME[0] == NUL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003869 return e_cannot_set_term_to_empty_string;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003870#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003871 if (gui.in_use)
3872 return e_cannot_change_term_in_GUI;
3873 if (term_is_gui(T_NAME))
3874 return e_use_gui_to_start_GUI;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003875#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003876 if (set_termname(T_NAME) == FAIL)
3877 return e_not_found_in_termcap;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003878
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003879 // Screen colors may have changed.
3880 redraw_later_clear();
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003881
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003882 return NULL;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003883}
3884
3885/*
3886 * Some terminal option (t_xxx) is changed
3887 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003888 char *
3889did_set_term_option(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003890{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003891 char_u **varp = (char_u **)args->os_varp;
3892
3893 if (!full_screen)
3894 return NULL;
3895
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003896 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
3897 if (varp == &T_CCO)
3898 {
3899 int colors = atoi((char *)T_CCO);
3900
3901 // Only reinitialize colors if t_Co value has really changed to
3902 // avoid expensive reload of colorscheme if t_Co is set to the
3903 // same value multiple times.
3904 if (colors != t_colors)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003905 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003906 t_colors = colors;
3907 if (t_colors <= 1)
3908 {
3909 vim_free(T_CCO);
3910 T_CCO = empty_option;
3911 }
3912#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3913 if (is_term_win32())
3914 {
3915 swap_tcap();
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003916 args->os_did_swaptcap = TRUE;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003917 }
3918#endif
3919 // We now have a different color setup, initialize it again.
3920 init_highlight(TRUE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003921 }
3922 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003923 ttest(FALSE);
3924 if (varp == &T_ME)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003925 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003926 out_str(T_ME);
3927 redraw_later(UPD_CLEAR);
3928#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3929 // Since t_me has been set, this probably means that the user
3930 // wants to use this as default colors. Need to reset default
3931 // background/foreground colors.
3932# ifdef VIMDLL
3933 if (!gui.in_use && !gui.starting)
3934# endif
3935 mch_set_normal_colors();
3936#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003937 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003938 if (varp == &T_BE && termcap_active)
3939 {
3940 MAY_WANT_TO_LOG_THIS;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003941
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003942 if (*T_BE == NUL)
3943 // When clearing t_BE we assume the user no longer wants
3944 // bracketed paste, thus disable it by writing t_BD.
3945 out_str(T_BD);
3946 else
3947 out_str(T_BE);
3948 }
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003949
3950 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003951}
3952
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003953#if defined(FEAT_TERMINAL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003954/*
3955 * The 'termwinkey' option is changed.
3956 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003957 char *
Milly94606f72024-10-22 22:07:52 +02003958did_set_termwinkey(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003959{
Milly94606f72024-10-22 22:07:52 +02003960 char_u **varp = (char_u **)args->os_varp;
3961
3962 if ((*varp)[0] != NUL && string_to_key(*varp, TRUE) == 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003963 return e_invalid_argument;
3964
3965 return NULL;
3966}
3967
3968/*
3969 * The 'termwinsize' option is changed.
3970 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003971 char *
Milly8be10aa2024-10-22 22:01:46 +02003972did_set_termwinsize(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003973{
Milly8be10aa2024-10-22 22:01:46 +02003974 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003975 char_u *p;
3976
Milly8be10aa2024-10-22 22:01:46 +02003977 if ((*varp)[0] == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003978 return NULL;
3979
Milly8be10aa2024-10-22 22:01:46 +02003980 p = skipdigits(*varp);
3981 if (p == *varp || (*p != 'x' && *p != '*') || *skipdigits(p + 1) != NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003982 return e_invalid_argument;
3983
3984 return NULL;
3985}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003986
3987# if defined(MSWIN) || defined(PROTO)
3988/*
3989 * The 'termwintype' option is changed.
3990 */
3991 char *
3992did_set_termwintype(optset_T *args UNUSED)
3993{
3994 return did_set_opt_strings(p_twt, p_twt_values, FALSE);
3995}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003996
3997 int
3998expand_set_termwintype(optexpand_T *args, int *numMatches, char_u ***matches)
3999{
4000 return expand_set_opt_string(
4001 args,
4002 p_twt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004003 ARRAY_LENGTH(p_twt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004004 numMatches,
4005 matches);
4006}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00004007# endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004008#endif
4009
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004010/*
4011 * The 'titlestring' option is changed.
4012 */
4013 char *
4014did_set_titlestring(optset_T *args)
4015{
4016 int flagval = 0;
4017
4018#ifdef FEAT_STL_OPT
4019 flagval = STL_IN_TITLE;
4020#endif
4021 return parse_titleiconstring(args, flagval);
4022}
4023
4024#if (defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)) || defined(PROTO)
4025/*
4026 * The 'toolbar' option is changed.
4027 */
4028 char *
4029did_set_toolbar(optset_T *args UNUSED)
4030{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004031 if (opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags,
4032 TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004033 return e_invalid_argument;
4034
4035 out_flush();
4036 gui_mch_show_toolbar((toolbar_flags &
4037 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4038 return NULL;
4039}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004040
4041 int
4042expand_set_toolbar(optexpand_T *args, int *numMatches, char_u ***matches)
4043{
4044 return expand_set_opt_string(
4045 args,
4046 p_toolbar_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004047 ARRAY_LENGTH(p_toolbar_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004048 numMatches,
4049 matches);
4050}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004051#endif
4052
4053#if (defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)) || defined(PROTO)
4054/*
4055 * The 'toolbariconsize' option is changed. GTK+ 2 only.
4056 */
4057 char *
4058did_set_toolbariconsize(optset_T *args UNUSED)
4059{
4060 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
4061 return e_invalid_argument;
4062
4063 out_flush();
4064 gui_mch_show_toolbar((toolbar_flags &
4065 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4066 return NULL;
4067}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004068
4069 int
4070expand_set_toolbariconsize(optexpand_T *args, int *numMatches, char_u ***matches)
4071{
4072 return expand_set_opt_string(
4073 args,
4074 p_tbis_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004075 ARRAY_LENGTH(p_tbis_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004076 numMatches,
4077 matches);
4078}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004079#endif
4080
4081#if defined(UNIX) || defined(VMS) || defined(PROTO)
4082/*
4083 * The 'ttymouse' option is changed.
4084 */
4085 char *
4086did_set_ttymouse(optset_T *args UNUSED)
4087{
4088 char *errmsg = NULL;
4089
4090 // Switch the mouse off before changing the escape sequences used for
4091 // that.
4092 mch_setmouse(FALSE);
4093 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
4094 errmsg = e_invalid_argument;
4095 else
4096 check_mouse_termcode();
4097 if (termcap_active)
4098 setmouse(); // may switch it on again
4099
4100 return errmsg;
4101}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004102
4103 int
4104expand_set_ttymouse(optexpand_T *args, int *numMatches, char_u ***matches)
4105{
4106 return expand_set_opt_string(
4107 args,
4108 p_ttym_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004109 ARRAY_LENGTH(p_ttym_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004110 numMatches,
4111 matches);
4112}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004113#endif
4114
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004115#if defined(FEAT_VARTABS) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004116/*
4117 * The 'varsofttabstop' option is changed.
4118 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004119 char *
4120did_set_varsofttabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004121{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004122 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004123 char_u *cp;
4124
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004125 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004126 VIM_CLEAR(curbuf->b_p_vsts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004127 else
4128 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004129 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004130 {
4131 if (vim_isdigit(*cp))
4132 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004133 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004134 continue;
4135 return e_invalid_argument;
4136 }
4137
4138 int *oldarray = curbuf->b_p_vsts_array;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004139 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004140 {
4141 if (oldarray)
4142 vim_free(oldarray);
4143 }
4144 else
4145 return e_invalid_argument;
4146 }
4147
4148 return NULL;
4149}
4150
4151/*
4152 * The 'vartabstop' option is changed.
4153 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004154 char *
4155did_set_vartabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004156{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004157 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004158 char_u *cp;
4159
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004160 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004161 VIM_CLEAR(curbuf->b_p_vts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004162 else
4163 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004164 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004165 {
4166 if (vim_isdigit(*cp))
4167 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004168 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004169 continue;
4170 return e_invalid_argument;
4171 }
4172
4173 int *oldarray = curbuf->b_p_vts_array;
4174
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004175 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004176 {
4177 vim_free(oldarray);
4178# ifdef FEAT_FOLDING
4179 if (foldmethodIsIndent(curwin))
4180 foldUpdateAll(curwin);
4181# endif
4182 }
4183 else
4184 return e_invalid_argument;
4185 }
4186
4187 return NULL;
4188}
4189#endif
4190
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004191/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004192 * The 'verbosefile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004193 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004194 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004195did_set_verbosefile(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004196{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004197 verbose_stop();
4198 if (*p_vfile != NUL && verbose_open() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004199 return e_invalid_argument;
4200
4201 return NULL;
4202}
4203
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004204#if defined(FEAT_SESSION) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004205/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004206 * The 'viewoptions' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004207 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004208 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004209did_set_viewoptions(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004210{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004211 return did_set_opt_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004212}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004213#endif
4214
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004215#if defined(FEAT_VIMINFO) || defined(PROTO)
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004216/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004217 * The 'viminfo' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004218 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004219 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004220did_set_viminfo(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004221{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004222 char_u *s;
4223 char *errmsg = NULL;
4224
4225 for (s = p_viminfo; *s;)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004226 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004227 // Check it's a valid character
4228 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
4229 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01004230 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004231 break;
4232 }
4233 if (*s == 'n') // name is always last one
4234 break;
4235 else if (*s == 'r') // skip until next ','
4236 {
4237 while (*++s && *s != ',')
4238 ;
4239 }
4240 else if (*s == '%')
4241 {
4242 // optional number
4243 while (vim_isdigit(*++s))
4244 ;
4245 }
4246 else if (*s == '!' || *s == 'h' || *s == 'c')
4247 ++s; // no extra chars
4248 else // must have a number
4249 {
4250 while (vim_isdigit(*++s))
4251 ;
4252
4253 if (!VIM_ISDIGIT(*(s - 1)))
4254 {
4255 if (args->os_errbuf != NULL)
4256 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01004257 vim_snprintf(args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004258 _(e_missing_number_after_angle_str_angle),
4259 transchar_byte(*(s - 1)));
4260 errmsg = args->os_errbuf;
4261 }
4262 else
4263 errmsg = "";
4264 break;
4265 }
4266 }
4267 if (*s == ',')
4268 ++s;
4269 else if (*s)
4270 {
4271 if (args->os_errbuf != NULL)
4272 errmsg = e_missing_comma;
4273 else
4274 errmsg = "";
4275 break;
4276 }
4277 }
4278 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
4279 errmsg = e_must_specify_a_value;
4280
4281 return errmsg;
4282}
4283#endif
4284
4285/*
4286 * The 'virtualedit' option is changed.
4287 */
4288 char *
4289did_set_virtualedit(optset_T *args)
4290{
4291 char_u *ve = p_ve;
4292 unsigned int *flags = &ve_flags;
4293
4294 if (args->os_flags & OPT_LOCAL)
4295 {
4296 ve = curwin->w_p_ve;
4297 flags = &curwin->w_ve_flags;
4298 }
4299
4300 if ((args->os_flags & OPT_LOCAL) && *ve == NUL)
4301 // make the local value empty: use the global value
4302 *flags = 0;
4303 else
4304 {
4305 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
4306 return e_invalid_argument;
4307 else if (STRCMP(ve, args->os_oldval.string) != 0)
4308 {
4309 // Recompute cursor position in case the new 've' setting
4310 // changes something.
4311 validate_virtcol();
4312 coladvance(curwin->w_virtcol);
4313 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004314 }
4315
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004316 return NULL;
4317}
4318
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004319 int
4320expand_set_virtualedit(optexpand_T *args, int *numMatches, char_u ***matches)
4321{
4322 return expand_set_opt_string(
4323 args,
4324 p_ve_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004325 ARRAY_LENGTH(p_ve_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004326 numMatches,
4327 matches);
4328}
4329
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004330/*
4331 * The 'whichwrap' option is changed.
4332 */
4333 char *
4334did_set_whichwrap(optset_T *args)
4335{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004336 char_u **varp = (char_u **)args->os_varp;
4337
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004338 // Add ',' to the list flags because 'whichwrap' is a flag
4339 // list that is comma-separated.
Christian Brabandtb39b2402023-11-29 11:34:05 +01004340 return did_set_option_listflag(*varp, (char_u *)(WW_ALL ","),
4341 args->os_errbuf, args->os_errbuflen);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004342}
4343
4344 int
4345expand_set_whichwrap(optexpand_T *args, int *numMatches, char_u ***matches)
4346{
4347 return expand_set_opt_listflag(args, (char_u*)WW_ALL, numMatches, matches);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004348}
4349
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004350/*
4351 * The 'wildmode' option is changed.
4352 */
4353 char *
4354did_set_wildmode(optset_T *args UNUSED)
4355{
4356 if (check_opt_wim() == FAIL)
4357 return e_invalid_argument;
4358 return NULL;
4359}
4360
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004361 int
4362expand_set_wildmode(optexpand_T *args, int *numMatches, char_u ***matches)
4363{
4364 return expand_set_opt_string(
4365 args,
4366 p_wim_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004367 ARRAY_LENGTH(p_wim_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004368 numMatches,
4369 matches);
4370}
4371
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004372/*
4373 * The 'wildoptions' option is changed.
4374 */
4375 char *
4376did_set_wildoptions(optset_T *args UNUSED)
4377{
4378 return did_set_opt_strings(p_wop, p_wop_values, TRUE);
4379}
4380
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004381 int
4382expand_set_wildoptions(optexpand_T *args, int *numMatches, char_u ***matches)
4383{
4384 return expand_set_opt_string(
4385 args,
4386 p_wop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004387 ARRAY_LENGTH(p_wop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004388 numMatches,
4389 matches);
4390}
4391
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004392#if defined(FEAT_WAK) || defined(PROTO)
4393/*
4394 * The 'winaltkeys' option is changed.
4395 */
4396 char *
4397did_set_winaltkeys(optset_T *args UNUSED)
4398{
4399 char *errmsg = NULL;
4400
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004401 if (*p_wak == NUL || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004402 errmsg = e_invalid_argument;
4403# ifdef FEAT_MENU
4404# if defined(FEAT_GUI_MOTIF)
4405 else if (gui.in_use)
4406 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4407# elif defined(FEAT_GUI_GTK)
4408 else if (gui.in_use)
4409 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4410# endif
4411# endif
4412 return errmsg;
4413}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004414
4415 int
4416expand_set_winaltkeys(optexpand_T *args, int *numMatches, char_u ***matches)
4417{
4418 return expand_set_opt_string(
4419 args,
4420 p_wak_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004421 ARRAY_LENGTH(p_wak_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004422 numMatches,
4423 matches);
4424}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004425#endif
4426
4427/*
4428 * The 'wincolor' option is changed.
4429 */
4430 char *
4431did_set_wincolor(optset_T *args UNUSED)
4432{
4433#ifdef FEAT_TERMINAL
4434 term_update_wincolor(curwin);
4435#endif
4436 return NULL;
4437}
4438
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004439 int
4440expand_set_wincolor(optexpand_T *args, int *numMatches, char_u ***matches)
4441{
4442 return expand_set_opt_generic(
4443 args,
4444 get_highlight_name,
4445 numMatches,
4446 matches);
4447}
4448
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004449#ifdef FEAT_SYN_HL
4450/*
4451 * When the 'syntax' option is set, load the syntax of that name.
4452 */
4453 static void
4454do_syntax_autocmd(int value_changed)
4455{
4456 static int syn_recursive = 0;
4457
4458 ++syn_recursive;
4459 // Only pass TRUE for "force" when the value changed or not used
4460 // recursively, to avoid endless recurrence.
4461 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
4462 value_changed || syn_recursive == 1, curbuf);
4463 curbuf->b_flags |= BF_SYN_SET;
4464 --syn_recursive;
4465}
4466#endif
4467
4468/*
4469 * When the 'filetype' option is set, trigger the FileType autocommand.
4470 */
4471 static void
4472do_filetype_autocmd(char_u **varp, int opt_flags, int value_changed)
4473{
4474 // Skip this when called from a modeline and the filetype was already set
4475 // to this value.
4476 if ((opt_flags & OPT_MODELINE) && !value_changed)
4477 return;
4478
4479 static int ft_recursive = 0;
4480 int secure_save = secure;
4481
4482 // Reset the secure flag, since the value of 'filetype' has
4483 // been checked to be safe.
4484 secure = 0;
4485
4486 ++ft_recursive;
zeertzjq5bf6c212024-03-31 18:41:27 +02004487 curbuf->b_did_filetype = TRUE;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004488 // Only pass TRUE for "force" when the value changed or not
4489 // used recursively, to avoid endless recurrence.
4490 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
4491 value_changed || ft_recursive == 1, curbuf);
4492 --ft_recursive;
4493 // Just in case the old "curbuf" is now invalid.
4494 if (varp != &(curbuf->b_p_ft))
4495 varp = NULL;
4496
4497 secure = secure_save;
4498}
4499
4500#ifdef FEAT_SPELL
4501/*
4502 * When the 'spelllang' option is set, source the spell/LANG.vim file in
4503 * 'runtimepath'.
4504 */
4505 static void
4506do_spelllang_source(void)
4507{
4508 char_u fname[200];
4509 char_u *p;
4510 char_u *q = curwin->w_s->b_p_spl;
4511
4512 // Skip the first name if it is "cjk".
4513 if (STRNCMP(q, "cjk,", 4) == 0)
4514 q += 4;
4515
4516 // They could set 'spellcapcheck' depending on the language. Use the first
4517 // name in 'spelllang' up to '_region' or '.encoding'.
4518 for (p = q; *p != NUL; ++p)
4519 if (!ASCII_ISALNUM(*p) && *p != '-')
4520 break;
4521 if (p > q)
4522 {
4523 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
4524 (int)(p - q), q);
4525 source_runtime(fname, DIP_ALL);
4526 }
4527}
4528#endif
4529
4530/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004531 * Handle string options that need some action to perform when changed.
zeertzjqf6782732022-07-27 18:26:03 +01004532 * The new value must be allocated.
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004533 * Returns NULL for success, or an untranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +02004534 */
4535 char *
4536did_set_string_option(
4537 int opt_idx, // index in options[] table
4538 char_u **varp, // pointer to the option variable
Bram Moolenaardac13472019-09-16 21:06:21 +02004539 char_u *oldval, // previous value of the option
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004540 char_u *value, // new value of the option
Bram Moolenaardac13472019-09-16 21:06:21 +02004541 char *errbuf, // buffer for errors, or NULL
Mike Williams620f0112023-12-05 15:36:06 +01004542 size_t errbuflen, // length of error buffer
Bram Moolenaardac13472019-09-16 21:06:21 +02004543 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004544 set_op_T op, // OP_ADDING/OP_PREPENDING/OP_REMOVING
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004545 int *value_checked) // value was checked to be safe, no
Bram Moolenaardac13472019-09-16 21:06:21 +02004546 // need to set P_INSECURE
4547{
4548 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +02004549 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004550 opt_did_set_cb_T did_set_cb = get_option_did_set_cb(opt_idx);
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004551 optset_T args;
4552
4553 // 'ttytype' is an alias for 'term'. Both 'term' and 'ttytype' point to
4554 // T_NAME. If 'term' or 'ttytype' is modified, then use the index for the
4555 // 'term' option. Only set the P_ALLOCED flag on 'term'.
4556 if (varp == &T_NAME)
4557 {
4558 opt_idx = findoption((char_u *)"term");
4559 if (opt_idx >= 0)
4560 {
4561 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
4562 did_set_cb = get_option_did_set_cb(opt_idx);
4563 }
4564 }
4565
4566 CLEAR_FIELD(args);
Bram Moolenaardac13472019-09-16 21:06:21 +02004567
Bram Moolenaardac13472019-09-16 21:06:21 +02004568 // Disallow changing some options from secure mode
4569 if ((secure
4570#ifdef HAVE_SANDBOX
4571 || sandbox != 0
4572#endif
4573 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00004574 errmsg = e_not_allowed_here;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004575 // Check for a "normal" directory or file name in some options.
4576 else if (check_illegal_path_names(opt_idx, varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004577 errmsg = e_invalid_argument;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004578 else if (did_set_cb != NULL)
4579 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004580 args.os_varp = (char_u *)varp;
4581 args.os_idx = opt_idx;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004582 args.os_flags = opt_flags;
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004583 args.os_op = op;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004584 args.os_oldval.string = oldval;
4585 args.os_newval.string = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004586 args.os_errbuf = errbuf;
Christian Brabandtb39b2402023-11-29 11:34:05 +01004587 args.os_errbuflen = errbuflen;
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004588 // Invoke the option specific callback function to validate and apply
4589 // the new option value.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004590 errmsg = did_set_cb(&args);
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004591
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004592 // The 'keymap', 'filetype' and 'syntax' option callback functions
4593 // may change the os_value_checked field.
4594 *value_checked = args.os_value_checked;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004595 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004596
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004597 // If an error is detected, restore the previous value.
Bram Moolenaardac13472019-09-16 21:06:21 +02004598 if (errmsg != NULL)
4599 {
zeertzjqf6782732022-07-27 18:26:03 +01004600 free_string_option(*varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02004601 *varp = oldval;
4602 // When resetting some values, need to act on it.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004603 if (args.os_restore_chartab)
Bram Moolenaardac13472019-09-16 21:06:21 +02004604 (void)init_chartab();
4605 if (varp == &p_hl)
4606 (void)highlight_changed();
4607 }
4608 else
4609 {
4610#ifdef FEAT_EVAL
4611 // Remember where the option was set.
4612 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4613#endif
4614 // Free string options that are in allocated memory.
4615 // Use "free_oldval", because recursiveness may change the flags under
4616 // our fingers (esp. init_highlight()).
4617 if (free_oldval)
4618 free_string_option(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01004619 set_option_flag(opt_idx, P_ALLOCED);
Bram Moolenaardac13472019-09-16 21:06:21 +02004620
4621 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
4622 && is_global_local_option(opt_idx))
4623 {
4624 // global option with local value set to use global value; free
4625 // the local value and make it empty
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004626 char_u *p = get_option_varp_scope(opt_idx, OPT_LOCAL);
Bram Moolenaardac13472019-09-16 21:06:21 +02004627 free_string_option(*(char_u **)p);
4628 *(char_u **)p = empty_option;
4629 }
4630
4631 // May set global value for local option.
4632 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
4633 set_string_option_global(opt_idx, varp);
4634
4635 // Trigger the autocommand only after setting the flags.
4636#ifdef FEAT_SYN_HL
Bram Moolenaardac13472019-09-16 21:06:21 +02004637 if (varp == &(curbuf->b_p_syn))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004638 do_syntax_autocmd(args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004639#endif
4640 else if (varp == &(curbuf->b_p_ft))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004641 do_filetype_autocmd(varp, opt_flags, args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004642#ifdef FEAT_SPELL
4643 if (varp == &(curwin->w_s->b_p_spl))
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004644 do_spelllang_source();
Bram Moolenaardac13472019-09-16 21:06:21 +02004645#endif
4646 }
4647
Bram Moolenaardac13472019-09-16 21:06:21 +02004648 if (varp == &p_mouse)
4649 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004650 if (*p_mouse == NUL)
4651 mch_setmouse(FALSE); // switch mouse off
4652 else
Bram Moolenaardac13472019-09-16 21:06:21 +02004653 setmouse(); // in case 'mouse' changed
4654 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004655
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004656 if (varp == &p_rtp)
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004657 {
4658 export_myvimdir();
4659#if defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004660 update_package_paths_in_lua();
4661#endif
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004662 }
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004663
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004664#if defined(FEAT_LINEBREAK)
4665 // Changing Formatlistpattern when briopt includes the list setting:
4666 // redraw
4667 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
4668 && curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004669 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004670#endif
4671
Bram Moolenaardac13472019-09-16 21:06:21 +02004672 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004673 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0
4674 && (get_option_flags(opt_idx) & P_HLONLY) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02004675 curwin->w_set_curswant = TRUE;
4676
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004677 if ((opt_flags & OPT_NO_REDRAW) == 0)
4678 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004679#ifdef FEAT_GUI
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004680 // set when changing an option that only requires a redraw in the GUI
4681 int redraw_gui_only = FALSE;
4682
4683 if (varp == &p_go // 'guioptions'
4684 || varp == &p_guifont // 'guifont'
4685# ifdef FEAT_GUI_TABLINE
4686 || varp == &p_gtl // 'guitablabel'
4687 || varp == &p_gtt // 'guitabtooltip'
4688# endif
4689# ifdef FEAT_XFONTSET
4690 || varp == &p_guifontset // 'guifontset'
4691# endif
4692 || varp == &p_guifontwide // 'guifontwide'
Erik S. V. Jansson2f026382024-02-26 22:23:05 +01004693# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004694 || varp == &p_guiligatures // 'guiligatures'
4695# endif
4696 )
4697 redraw_gui_only = TRUE;
4698
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004699 // check redraw when it's not a GUI option or the GUI is active.
4700 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02004701#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004702 check_redraw(get_option_flags(opt_idx));
4703 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004704
4705#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004706 if (args.os_did_swaptcap)
Bram Moolenaardac13472019-09-16 21:06:21 +02004707 {
4708 set_termname((char_u *)"win32");
4709 init_highlight(TRUE, FALSE);
4710 }
4711#endif
4712
4713 return errmsg;
4714}
4715
4716/*
4717 * Check an option that can be a range of string values.
4718 *
4719 * Return OK for correct value, FAIL otherwise.
4720 * Empty is always OK.
4721 */
4722 static int
4723check_opt_strings(
4724 char_u *val,
4725 char **values,
4726 int list) // when TRUE: accept a list of values
4727{
4728 return opt_strings_flags(val, values, NULL, list);
4729}
4730
4731/*
4732 * Handle an option that can be a range of string values.
4733 * Set a flag in "*flagp" for each string present.
4734 *
4735 * Return OK for correct value, FAIL otherwise.
4736 * Empty is always OK.
4737 */
4738 static int
4739opt_strings_flags(
4740 char_u *val, // new value
4741 char **values, // array of valid string values
4742 unsigned *flagp,
4743 int list) // when TRUE: accept a list of values
4744{
4745 int i;
4746 int len;
4747 unsigned new_flags = 0;
4748
4749 while (*val)
4750 {
4751 for (i = 0; ; ++i)
4752 {
4753 if (values[i] == NULL) // val not found in values[]
4754 return FAIL;
4755
4756 len = (int)STRLEN(values[i]);
4757 if (STRNCMP(values[i], val, len) == 0
4758 && ((list && val[len] == ',') || val[len] == NUL))
4759 {
4760 val += len + (val[len] == ',');
4761 new_flags |= (1 << i);
4762 break; // check next item in val list
4763 }
4764 }
4765 }
4766 if (flagp != NULL)
4767 *flagp = new_flags;
4768
4769 return OK;
4770}
4771
4772/*
4773 * return OK if "p" is a valid fileformat name, FAIL otherwise.
4774 */
4775 int
4776check_ff_value(char_u *p)
4777{
4778 return check_opt_strings(p, p_ff_values, FALSE);
4779}
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004780
4781/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004782 * Save the actual shortmess Flags and clear them temporarily to avoid that
4783 * file messages overwrites any output from the following commands.
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004784 *
4785 * Caller must make sure to first call save_clear_shm_value() and then
4786 * restore_shm_value() exactly the same number of times.
4787 */
4788 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004789save_clear_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004790{
4791 if (STRLEN(p_shm) >= SHM_LEN)
4792 {
4793 iemsg(e_internal_error_shortmess_too_long);
4794 return;
4795 }
4796
4797 if (++set_shm_recursive == 1)
4798 {
4799 STRCPY(shm_buf, p_shm);
4800 set_option_value_give_err((char_u *)"shm", 0L, (char_u *)"", 0);
4801 }
4802}
4803
4804/*
4805 * Restore the shortmess Flags set from the save_clear_shm_value() function.
4806 */
4807 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004808restore_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004809{
4810 if (--set_shm_recursive == 0)
4811 {
4812 set_option_value_give_err((char_u *)"shm", 0L, shm_buf, 0);
4813 vim_memset(shm_buf, 0, SHM_LEN);
4814 }
4815}
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004816
4817/*
4818 * Export the environment variable $MYVIMDIR to the first item in runtimepath
4819 */
4820 static void
4821export_myvimdir()
4822{
4823 int dofree = FALSE;
4824 char_u *p;
4825 char_u *q = p_rtp;
4826 char_u *buf = alloc(MAXPATHL);
4827
4828 if (buf == NULL)
4829 return;
4830
4831 (void)copy_option_part(&q, buf, MAXPATHL, ",");
4832
4833 p = vim_getenv((char_u *)"MYVIMDIR", &dofree);
4834
4835 if (p == NULL || STRCMP(p, buf) != 0)
4836 {
4837 add_pathsep(buf);
4838#ifdef MSWIN
4839 // normalize path separators
4840 for (q = buf; *q != NUL; q++)
4841 if (*q == '/')
4842 *q = '\\';
4843#endif
4844 vim_setenv((char_u *)"MYVIMDIR", buf);
4845 }
4846 if (dofree)
4847 vim_free(p);
4848 vim_free(buf);
4849}