blob: 62a708683e9c1c9927cf089bf8faf03c906d799c [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()
Yee Cheng Chin9943d472025-03-26 19:41:02 +010033static char *(p_dip_values[]) = {"filler", "context:", "iblank", "icase", "iwhite", "iwhiteall", "iwhiteeol", "horizontal", "vertical", "closeoff", "hiddenoff", "foldcolumn:", "followwrap", "internal", "indent-heuristic", "algorithm:", "inline:", "linematch:", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020034static char *(p_dip_algorithm_values[]) = {"myers", "minimal", "patience", "histogram", NULL};
Yee Cheng Chin9943d472025-03-26 19:41:02 +010035static char *(p_dip_inline_values[]) = {"none", "simple", "char", "word", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020036#endif
distobs25ac6d62024-07-06 17:50:09 +020037static char *(p_nf_values[]) = {"bin", "octal", "hex", "alpha", "unsigned", "blank", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020038static char *(p_ff_values[]) = {FF_UNIX, FF_DOS, FF_MAC, NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020039#ifdef FEAT_CLIPBOARD
40// Note: Keep this in sync with did_set_clipboard()
41static char *(p_cb_values[]) = {"unnamed", "unnamedplus", "autoselect", "autoselectplus", "autoselectml", "html", "exclude:", NULL};
42#endif
Bram Moolenaardac13472019-09-16 21:06:21 +020043#ifdef FEAT_CRYPT
Christian Brabandtf573c6e2021-06-20 14:02:16 +020044static char *(p_cm_values[]) = {"zip", "blowfish", "blowfish2",
45 # ifdef FEAT_SODIUM
Christian Brabandtaae58342023-04-23 17:50:22 +010046 "xchacha20", "xchacha20v2",
Christian Brabandtf573c6e2021-06-20 14:02:16 +020047 # endif
48 NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020049#endif
50static char *(p_cmp_values[]) = {"internal", "keepascii", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020051#ifdef FEAT_SYN_HL
52// Note: Keep this in sync with fill_culopt_flags()
53static char *(p_culopt_values[]) = {"line", "screenline", "number", "both", NULL};
54#endif
Bram Moolenaardac13472019-09-16 21:06:21 +020055static char *(p_dy_values[]) = {"lastline", "truncate", "uhex", NULL};
Yegappan Lakshmanan87018252023-09-20 20:20:04 +020056static char *(p_jop_values[]) = {"stack", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020057#ifdef FEAT_FOLDING
58static char *(p_fdo_values[]) = {"all", "block", "hor", "mark", "percent",
59 "quickfix", "search", "tag", "insert",
60 "undo", "jump", NULL};
61#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +020062// Note: Keep this in sync with match_keyprotocol()
63static char *(p_kpc_protocol_values[]) = {"none", "mok2", "kitty", NULL};
64#ifdef FEAT_PROP_POPUP
65// Note: Keep this in sync with parse_popup_option()
66static char *(p_popup_option_values[]) = {"height:", "width:", "highlight:", "border:", "align:", NULL};
67static char *(p_popup_option_border_values[]) = {"on", "off", NULL};
68static char *(p_popup_option_align_values[]) = {"item", "menu", NULL};
69#endif
70#if defined(FEAT_SPELL)
71// Note: Keep this in sync with spell_check_sps()
72static char *(p_sps_values[]) = {"best", "fast", "double", "expr:", "file:", "timeout:", NULL};
73#endif
Bram Moolenaardac13472019-09-16 21:06:21 +020074#ifdef FEAT_SESSION
Bram Moolenaar635bd602021-04-16 19:58:22 +020075// Also used for 'viewoptions'! Keep in sync with SSOP_ flags.
Bram Moolenaardac13472019-09-16 21:06:21 +020076static char *(p_ssop_values[]) = {"buffers", "winpos", "resize", "winsize",
77 "localoptions", "options", "help", "blank", "globals", "slash", "unix",
Bram Moolenaar635bd602021-04-16 19:58:22 +020078 "sesdir", "curdir", "folds", "cursor", "tabpages", "terminal", "skiprtp",
79 NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020080#endif
Bram Moolenaar539aa6b2019-11-17 18:09:38 +010081// Keep in sync with SWB_ flags in option.h
82static char *(p_swb_values[]) = {"useopen", "usetab", "split", "newtab", "vsplit", "uselast", NULL};
Luuk van Baal13ece2a2022-10-03 15:28:08 +010083static char *(p_spk_values[]) = {"cursor", "screen", "topline", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020084static char *(p_tc_values[]) = {"followic", "ignore", "match", "followscs", "smart", NULL};
LemonBoy5247b0b2024-07-12 19:30:58 +020085// Keep in sync with TCL_ flags in option.h
86static char *(p_tcl_values[]) = {"left", "uselast", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020087#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
88static char *(p_toolbar_values[]) = {"text", "icons", "tooltips", "horiz", NULL};
89#endif
90#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
91static char *(p_tbis_values[]) = {"tiny", "small", "medium", "large", "huge", "giant", NULL};
92#endif
Bram Moolenaara1cb1d12019-10-17 23:00:07 +020093#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +020094static char *(p_ttym_values[]) = {"xterm", "xterm2", "dec", "netterm", "jsbterm", "pterm", "urxvt", "sgr", NULL};
95#endif
Gary Johnson53ba05b2021-07-26 22:19:10 +020096static char *(p_ve_values[]) = {"block", "insert", "all", "onemore", "none", "NONE", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020097// Note: Keep this in sync with check_opt_wim()
Girish Palya2bacc3e2025-03-02 22:55:57 +010098static char *(p_wim_values[]) = {"full", "longest", "list", "lastused", "noselect", NULL};
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000099static char *(p_wop_values[]) = {"fuzzy", "tagfile", "pum", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200100#ifdef FEAT_WAK
101static char *(p_wak_values[]) = {"yes", "menu", "no", NULL};
102#endif
103static char *(p_mousem_values[]) = {"extend", "popup", "popup_setpos", "mac", NULL};
104static char *(p_sel_values[]) = {"inclusive", "exclusive", "old", NULL};
105static char *(p_slm_values[]) = {"mouse", "key", "cmd", NULL};
106static char *(p_km_values[]) = {"startsel", "stopsel", NULL};
107#ifdef FEAT_BROWSE
108static char *(p_bsdir_values[]) = {"current", "last", "buffer", NULL};
109#endif
110static char *(p_scbopt_values[]) = {"ver", "hor", "jump", NULL};
111static char *(p_debug_values[]) = {"msg", "throw", "beep", NULL};
112static char *(p_ead_values[]) = {"both", "ver", "hor", NULL};
113static char *(p_buftype_values[]) = {"nofile", "nowrite", "quickfix", "help", "terminal", "acwrite", "prompt", "popup", NULL};
114static char *(p_bufhidden_values[]) = {"hide", "unload", "delete", "wipe", NULL};
Bram Moolenaaraa0489e2020-04-17 19:41:21 +0200115static char *(p_bs_values[]) = {"indent", "eol", "start", "nostop", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200116#ifdef FEAT_FOLDING
117static char *(p_fdm_values[]) = {"manual", "expr", "marker", "indent", "syntax",
118# ifdef FEAT_DIFF
119 "diff",
120# endif
121 NULL};
122static char *(p_fcl_values[]) = {"all", NULL};
123#endif
glepnirf31cfa22025-03-06 21:59:13 +0100124static char *(p_cfc_values[]) = {"keyword", "files", "whole_line", NULL};
Girish Palyab1565882025-04-15 20:16:00 +0200125static char *(p_cot_values[]) = {"menu", "menuone", "longest", "preview", "popup", "popuphidden", "noinsert", "noselect", "fuzzy", "nosort", "preinsert", "nearest", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200126#ifdef BACKSLASH_IN_FILENAME
127static char *(p_csl_values[]) = {"slash", "backslash", NULL};
128#endif
129#ifdef FEAT_SIGNS
130static char *(p_scl_values[]) = {"yes", "no", "auto", "number", NULL};
131#endif
132#if defined(MSWIN) && defined(FEAT_TERMINAL)
133static char *(p_twt_values[]) = {"winpty", "conpty", "", NULL};
134#endif
Luuk van Baalba936f62022-12-15 13:15:39 +0000135static char *(p_sloc_values[]) = {"last", "statusline", "tabline", NULL};
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +0000136static char *(p_sws_values[]) = {"fsync", "sync", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200137
138static int check_opt_strings(char_u *val, char **values, int list);
139static int opt_strings_flags(char_u *val, char **values, unsigned *flagp, int list);
140
141/*
142 * After setting various option values: recompute variables that depend on
143 * option values.
144 */
145 void
146didset_string_options(void)
147{
148 (void)opt_strings_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
149 (void)opt_strings_flags(p_bkc, p_bkc_values, &bkc_flags, TRUE);
150 (void)opt_strings_flags(p_bo, p_bo_values, &bo_flags, TRUE);
glepnirf31cfa22025-03-06 21:59:13 +0100151 (void)opt_strings_flags(p_cfc, p_cfc_values, &cfc_flags, TRUE);
zeertzjq529b9ad2024-06-05 20:27:06 +0200152 (void)opt_strings_flags(p_cot, p_cot_values, &cot_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200153#ifdef FEAT_SESSION
154 (void)opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE);
155 (void)opt_strings_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
156#endif
157#ifdef FEAT_FOLDING
158 (void)opt_strings_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
159#endif
160 (void)opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE);
Yegappan Lakshmanan87018252023-09-20 20:20:04 +0200161 (void)opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200162 (void)opt_strings_flags(p_tc, p_tc_values, &tc_flags, FALSE);
163 (void)opt_strings_flags(p_ve, p_ve_values, &ve_flags, TRUE);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +0200164#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +0200165 (void)opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE);
166#endif
167#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
168 (void)opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags, TRUE);
169#endif
170#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
171 (void)opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE);
172#endif
Sean Dewar39c46b42022-05-12 17:44:29 +0100173 (void)opt_strings_flags(p_swb, p_swb_values, &swb_flags, TRUE);
LemonBoy5247b0b2024-07-12 19:30:58 +0200174 (void)opt_strings_flags(p_tcl, p_tcl_values, &tcl_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200175}
176
Yegappan Lakshmananf9dc2782023-05-11 15:02:56 +0100177#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200178/*
179 * Trigger the OptionSet autocommand.
180 * "opt_idx" is the index of the option being set.
181 * "opt_flags" can be OPT_LOCAL etc.
182 * "oldval" the old value
183 * "oldval_l" the old local value (only non-NULL if global and local value
184 * are set)
185 * "oldval_g" the old global value (only non-NULL if global and local value
186 * are set)
187 * "newval" the new value
188 */
189 void
zeertzjq269aa2b2022-11-28 11:36:50 +0000190trigger_optionset_string(
Bram Moolenaardac13472019-09-16 21:06:21 +0200191 int opt_idx,
192 int opt_flags,
193 char_u *oldval,
194 char_u *oldval_l,
195 char_u *oldval_g,
196 char_u *newval)
197{
198 // Don't do this recursively.
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000199 if (oldval == NULL || newval == NULL
200 || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
201 return;
Bram Moolenaardac13472019-09-16 21:06:21 +0200202
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000203 char_u buf_type[7];
204
205 sprintf((char *)buf_type, "%s",
Bram Moolenaardac13472019-09-16 21:06:21 +0200206 (opt_flags & OPT_LOCAL) ? "local" : "global");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000207 set_vim_var_string(VV_OPTION_OLD, oldval, -1);
208 set_vim_var_string(VV_OPTION_NEW, newval, -1);
209 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
210 if (opt_flags & OPT_LOCAL)
211 {
212 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
213 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
Bram Moolenaardac13472019-09-16 21:06:21 +0200214 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000215 if (opt_flags & OPT_GLOBAL)
216 {
217 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
218 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval, -1);
219 }
220 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
221 {
222 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
223 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval_l, -1);
224 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval_g, -1);
225 }
226 if (opt_flags & OPT_MODELINE)
227 {
228 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
229 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
230 }
231 apply_autocmds(EVENT_OPTIONSET,
232 get_option_fullname(opt_idx), NULL, FALSE,
233 NULL);
234 reset_v_option_vars();
Bram Moolenaardac13472019-09-16 21:06:21 +0200235}
236#endif
237
238 static char *
Mike Williams620f0112023-12-05 15:36:06 +0100239illegal_char(char *errbuf, size_t errbuflen, int c)
Bram Moolenaardac13472019-09-16 21:06:21 +0200240{
241 if (errbuf == NULL)
242 return "";
zeertzjq6a8d2e12024-01-17 20:54:49 +0100243 vim_snprintf(errbuf, errbuflen, _(e_illegal_character_str),
Christian Brabandtb39b2402023-11-29 11:34:05 +0100244 (char *)transchar(c));
Bram Moolenaardac13472019-09-16 21:06:21 +0200245 return errbuf;
246}
247
248/*
249 * Check string options in a buffer for NULL value.
250 */
251 void
252check_buf_options(buf_T *buf)
253{
254 check_string_option(&buf->b_p_bh);
255 check_string_option(&buf->b_p_bt);
256 check_string_option(&buf->b_p_fenc);
257 check_string_option(&buf->b_p_ff);
258#ifdef FEAT_FIND_ID
259 check_string_option(&buf->b_p_def);
260 check_string_option(&buf->b_p_inc);
261# ifdef FEAT_EVAL
262 check_string_option(&buf->b_p_inex);
263# endif
264#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +0100265#if defined(FEAT_EVAL)
Bram Moolenaardac13472019-09-16 21:06:21 +0200266 check_string_option(&buf->b_p_inde);
267 check_string_option(&buf->b_p_indk);
268#endif
269#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
270 check_string_option(&buf->b_p_bexpr);
271#endif
272#if defined(FEAT_CRYPT)
273 check_string_option(&buf->b_p_cm);
274#endif
275 check_string_option(&buf->b_p_fp);
276#if defined(FEAT_EVAL)
277 check_string_option(&buf->b_p_fex);
278#endif
279#ifdef FEAT_CRYPT
280 check_string_option(&buf->b_p_key);
281#endif
282 check_string_option(&buf->b_p_kp);
283 check_string_option(&buf->b_p_mps);
284 check_string_option(&buf->b_p_fo);
285 check_string_option(&buf->b_p_flp);
286 check_string_option(&buf->b_p_isk);
Bram Moolenaardac13472019-09-16 21:06:21 +0200287 check_string_option(&buf->b_p_com);
Bram Moolenaardac13472019-09-16 21:06:21 +0200288#ifdef FEAT_FOLDING
289 check_string_option(&buf->b_p_cms);
290#endif
291 check_string_option(&buf->b_p_nf);
Bram Moolenaardac13472019-09-16 21:06:21 +0200292 check_string_option(&buf->b_p_qe);
Bram Moolenaardac13472019-09-16 21:06:21 +0200293#ifdef FEAT_SYN_HL
294 check_string_option(&buf->b_p_syn);
295 check_string_option(&buf->b_s.b_syn_isk);
296#endif
297#ifdef FEAT_SPELL
298 check_string_option(&buf->b_s.b_p_spc);
299 check_string_option(&buf->b_s.b_p_spf);
300 check_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +0200301 check_string_option(&buf->b_s.b_p_spo);
Bram Moolenaardac13472019-09-16 21:06:21 +0200302#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200303 check_string_option(&buf->b_p_sua);
Bram Moolenaardac13472019-09-16 21:06:21 +0200304 check_string_option(&buf->b_p_cink);
305 check_string_option(&buf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +0100306 check_string_option(&buf->b_p_cinsd);
Bram Moolenaardac13472019-09-16 21:06:21 +0200307 parse_cino(buf);
Bram Moolenaar49846fb2022-10-15 16:05:33 +0100308 check_string_option(&buf->b_p_lop);
Bram Moolenaardac13472019-09-16 21:06:21 +0200309 check_string_option(&buf->b_p_ft);
Bram Moolenaardac13472019-09-16 21:06:21 +0200310 check_string_option(&buf->b_p_cinw);
zeertzjq529b9ad2024-06-05 20:27:06 +0200311 check_string_option(&buf->b_p_cot);
Bram Moolenaardac13472019-09-16 21:06:21 +0200312 check_string_option(&buf->b_p_cpt);
313#ifdef FEAT_COMPL_FUNC
314 check_string_option(&buf->b_p_cfu);
315 check_string_option(&buf->b_p_ofu);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +0100316 check_string_option(&buf->b_p_tsrfu);
Bram Moolenaardac13472019-09-16 21:06:21 +0200317#endif
318#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +0100319 check_string_option(&buf->b_p_ffu);
Bram Moolenaardac13472019-09-16 21:06:21 +0200320 check_string_option(&buf->b_p_tfu);
321#endif
322#ifdef FEAT_KEYMAP
323 check_string_option(&buf->b_p_keymap);
324#endif
325#ifdef FEAT_QUICKFIX
326 check_string_option(&buf->b_p_gp);
327 check_string_option(&buf->b_p_mp);
328 check_string_option(&buf->b_p_efm);
329#endif
330 check_string_option(&buf->b_p_ep);
331 check_string_option(&buf->b_p_path);
332 check_string_option(&buf->b_p_tags);
333 check_string_option(&buf->b_p_tc);
334 check_string_option(&buf->b_p_dict);
335 check_string_option(&buf->b_p_tsr);
Bram Moolenaardac13472019-09-16 21:06:21 +0200336 check_string_option(&buf->b_p_lw);
Bram Moolenaardac13472019-09-16 21:06:21 +0200337 check_string_option(&buf->b_p_bkc);
338 check_string_option(&buf->b_p_menc);
339#ifdef FEAT_VARTABS
340 check_string_option(&buf->b_p_vsts);
341 check_string_option(&buf->b_p_vts);
342#endif
343}
344
345/*
346 * Free the string allocated for an option.
347 * Checks for the string being empty_option. This may happen if we're out of
348 * memory, vim_strsave() returned NULL, which was replaced by empty_option by
349 * check_options().
350 * Does NOT check for P_ALLOCED flag!
351 */
352 void
353free_string_option(char_u *p)
354{
355 if (p != empty_option)
356 vim_free(p);
357}
358
359 void
360clear_string_option(char_u **pp)
361{
362 if (*pp != empty_option)
363 vim_free(*pp);
364 *pp = empty_option;
365}
366
367 void
368check_string_option(char_u **pp)
369{
370 if (*pp == NULL)
371 *pp = empty_option;
372}
373
374/*
375 * Set global value for string option when it's a local option.
376 */
377 static void
378set_string_option_global(
379 int opt_idx, // option index
380 char_u **varp) // pointer to option variable
381{
382 char_u **p, *s;
383
384 // the global value is always allocated
385 if (is_window_local_option(opt_idx))
386 p = (char_u **)GLOBAL_WO(varp);
387 else
388 p = (char_u **)get_option_var(opt_idx);
389 if (!is_global_option(opt_idx)
390 && p != varp
391 && (s = vim_strsave(*varp)) != NULL)
392 {
393 free_string_option(*p);
394 *p = s;
395 }
396}
397
398/*
399 * Set a string option to a new value (without checking the effect).
400 * The string is copied into allocated memory.
401 * if ("opt_idx" == -1) "name" is used, otherwise "opt_idx" is used.
402 * When "set_sid" is zero set the scriptID to current_sctx.sc_sid. When
403 * "set_sid" is SID_NONE don't set the scriptID. Otherwise set the scriptID to
404 * "set_sid".
405 */
406 void
407set_string_option_direct(
408 char_u *name,
409 int opt_idx,
410 char_u *val,
411 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
412 int set_sid UNUSED)
413{
414 char_u *s;
415 char_u **varp;
416 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
417 int idx = opt_idx;
418
419 if (idx == -1) // use name
420 {
421 idx = findoption(name);
422 if (idx < 0) // not found (should not happen)
423 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000424 semsg(_(e_internal_error_str), "set_string_option_direct()");
RestorerZ68ebcee2023-05-31 17:12:14 +0100425 siemsg("For option %s", name);
Bram Moolenaardac13472019-09-16 21:06:21 +0200426 return;
427 }
428 }
429
430 if (is_hidden_option(idx)) // can't set hidden option
431 return;
432
433 s = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000434 if (s == NULL)
435 return;
436
437 varp = (char_u **)get_option_varp_scope(idx,
438 both ? OPT_LOCAL : opt_flags);
439 if ((opt_flags & OPT_FREE) && (get_option_flags(idx) & P_ALLOCED))
440 free_string_option(*varp);
441 *varp = s;
442
443 // For buffer/window local option may also set the global value.
444 if (both)
445 set_string_option_global(idx, varp);
446
447 set_option_flag(idx, P_ALLOCED);
448
449 // When setting both values of a global option with a local value,
450 // make the local value empty, so that the global value is used.
451 if (is_global_local_option(idx) && both)
Bram Moolenaardac13472019-09-16 21:06:21 +0200452 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000453 free_string_option(*varp);
454 *varp = empty_option;
Bram Moolenaardac13472019-09-16 21:06:21 +0200455 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000456# ifdef FEAT_EVAL
457 if (set_sid != SID_NONE)
458 {
459 sctx_T script_ctx;
460
461 if (set_sid == 0)
462 script_ctx = current_sctx;
463 else
464 {
465 script_ctx.sc_sid = set_sid;
466 script_ctx.sc_seq = 0;
467 script_ctx.sc_lnum = 0;
468 script_ctx.sc_version = 1;
469 }
470 set_option_sctx_idx(idx, opt_flags, script_ctx);
471 }
472# endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200473}
474
Dominique Pellee764d1b2023-03-12 21:20:59 +0000475#if defined(FEAT_PROP_POPUP) || \
476 (defined(FEAT_DIFF) && defined(FEAT_FOLDING)) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200477/*
478 * Like set_string_option_direct(), but for a window-local option in "wp".
479 * Blocks autocommands to avoid the old curwin becoming invalid.
480 */
481 void
482set_string_option_direct_in_win(
483 win_T *wp,
484 char_u *name,
485 int opt_idx,
486 char_u *val,
487 int opt_flags,
488 int set_sid)
489{
490 win_T *save_curwin = curwin;
491
492 block_autocmds();
493 curwin = wp;
494 curbuf = curwin->w_buffer;
495 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
496 curwin = save_curwin;
497 curbuf = curwin->w_buffer;
498 unblock_autocmds();
499}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000500#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200501
Dominique Pelle748b3082022-01-08 12:41:16 +0000502#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200503/*
504 * Like set_string_option_direct(), but for a buffer-local option in "buf".
505 * Blocks autocommands to avoid the old curbuf becoming invalid.
506 */
507 void
508set_string_option_direct_in_buf(
509 buf_T *buf,
510 char_u *name,
511 int opt_idx,
512 char_u *val,
513 int opt_flags,
514 int set_sid)
515{
516 buf_T *save_curbuf = curbuf;
517
518 block_autocmds();
519 curbuf = buf;
520 curwin->w_buffer = curbuf;
521 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
522 curbuf = save_curbuf;
523 curwin->w_buffer = curbuf;
524 unblock_autocmds();
525}
Dominique Pelle748b3082022-01-08 12:41:16 +0000526#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200527
528/*
529 * Set a string option to a new value, and handle the effects.
530 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100531 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaardac13472019-09-16 21:06:21 +0200532 */
533 char *
534set_string_option(
535 int opt_idx,
536 char_u *value,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +0000537 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Christian Brabandtb39b2402023-11-29 11:34:05 +0100538 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +0100539 size_t errbuflen)
Bram Moolenaardac13472019-09-16 21:06:21 +0200540{
541 char_u *s;
542 char_u **varp;
543 char_u *oldval;
544#if defined(FEAT_EVAL)
545 char_u *oldval_l = NULL;
546 char_u *oldval_g = NULL;
547 char_u *saved_oldval = NULL;
548 char_u *saved_oldval_l = NULL;
549 char_u *saved_oldval_g = NULL;
550 char_u *saved_newval = NULL;
551#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100552 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +0200553 int value_checked = FALSE;
554
555 if (is_hidden_option(opt_idx)) // don't set hidden option
556 return NULL;
557
Bram Moolenaar7f009df2020-03-16 20:27:38 +0100558 s = vim_strsave(value == NULL ? (char_u *)"" : value);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000559 if (s == NULL)
560 return NULL;
561
562 varp = (char_u **)get_option_varp_scope(opt_idx,
563 (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
564 ? (is_global_local_option(opt_idx)
565 ? OPT_GLOBAL : OPT_LOCAL)
566 : opt_flags);
567 oldval = *varp;
568#if defined(FEAT_EVAL)
569 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +0200570 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000571 oldval_l = *(char_u **)get_option_varp_scope(opt_idx, OPT_LOCAL);
572 oldval_g = *(char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
Bram Moolenaardac13472019-09-16 21:06:21 +0200573 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000574#endif
575 *varp = s;
576
577#if defined(FEAT_EVAL)
578 if (!starting
579# ifdef FEAT_CRYPT
580 && !is_crypt_key_option(opt_idx)
581# endif
582 )
583 {
584 if (oldval_l != NULL)
585 saved_oldval_l = vim_strsave(oldval_l);
586 if (oldval_g != NULL)
587 saved_oldval_g = vim_strsave(oldval_g);
588 saved_oldval = vim_strsave(oldval);
589 saved_newval = vim_strsave(s);
590 }
591#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000592 if ((errmsg = did_set_string_option(opt_idx, varp, oldval, value, errbuf,
Christian Brabandtb39b2402023-11-29 11:34:05 +0100593 errbuflen, opt_flags, OP_NONE, &value_checked)) == NULL)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000594 did_set_option(opt_idx, opt_flags, TRUE, value_checked);
595
596#if defined(FEAT_EVAL)
597 // call autocommand after handling side effects
598 if (errmsg == NULL)
599 trigger_optionset_string(opt_idx, opt_flags,
600 saved_oldval, saved_oldval_l,
601 saved_oldval_g, saved_newval);
602 vim_free(saved_oldval);
603 vim_free(saved_oldval_l);
604 vim_free(saved_oldval_g);
605 vim_free(saved_newval);
606#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100607 return errmsg;
Bram Moolenaardac13472019-09-16 21:06:21 +0200608}
609
610/*
611 * Return TRUE if "val" is a valid 'filetype' name.
612 * Also used for 'syntax' and 'keymap'.
613 */
614 static int
615valid_filetype(char_u *val)
616{
617 return valid_name(val, ".-_");
618}
619
620#ifdef FEAT_STL_OPT
621/*
622 * Check validity of options with the 'statusline' format.
zeertzjq5dc294a2022-04-15 13:17:57 +0100623 * Return an untranslated error message or NULL.
Bram Moolenaardac13472019-09-16 21:06:21 +0200624 */
625 static char *
626check_stl_option(char_u *s)
627{
Bram Moolenaardac13472019-09-16 21:06:21 +0200628 int groupdepth = 0;
Christian Brabandtb39b2402023-11-29 11:34:05 +0100629 static char errbuf[ERR_BUFLEN];
630 int errbuflen = ERR_BUFLEN;
Bram Moolenaardac13472019-09-16 21:06:21 +0200631
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100632 while (*s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200633 {
634 // Check for valid keys after % sequences
635 while (*s && *s != '%')
636 s++;
637 if (!*s)
638 break;
639 s++;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +0000640 if (*s == '%' || *s == STL_TRUNCMARK || *s == STL_SEPARATE)
Bram Moolenaardac13472019-09-16 21:06:21 +0200641 {
642 s++;
643 continue;
644 }
645 if (*s == ')')
646 {
647 s++;
648 if (--groupdepth < 0)
649 break;
650 continue;
651 }
652 if (*s == '-')
653 s++;
654 while (VIM_ISDIGIT(*s))
655 s++;
656 if (*s == STL_USER_HL)
657 continue;
658 if (*s == '.')
659 {
660 s++;
661 while (*s && VIM_ISDIGIT(*s))
662 s++;
663 }
664 if (*s == '(')
665 {
666 groupdepth++;
667 continue;
668 }
669 if (vim_strchr(STL_ALL, *s) == NULL)
670 {
Christian Brabandtb39b2402023-11-29 11:34:05 +0100671 return illegal_char(errbuf, errbuflen, *s);
Bram Moolenaardac13472019-09-16 21:06:21 +0200672 }
673 if (*s == '{')
674 {
zeertzjq5dc294a2022-04-15 13:17:57 +0100675 int reevaluate = (*++s == '%');
shadmansaleh30e3de22021-05-15 17:23:28 +0200676
zeertzjq5dc294a2022-04-15 13:17:57 +0100677 if (reevaluate && *++s == '}')
678 // "}" is not allowed immediately after "%{%"
Christian Brabandtb39b2402023-11-29 11:34:05 +0100679 return illegal_char(errbuf, errbuflen, '}');
shadmansaleh30e3de22021-05-15 17:23:28 +0200680 while ((*s != '}' || (reevaluate && s[-1] != '%')) && *s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200681 s++;
682 if (*s != '}')
zeertzjq5dc294a2022-04-15 13:17:57 +0100683 return e_unclosed_expression_sequence;
Bram Moolenaardac13472019-09-16 21:06:21 +0200684 }
685 }
Bram Moolenaardac13472019-09-16 21:06:21 +0200686 if (groupdepth != 0)
zeertzjq5dc294a2022-04-15 13:17:57 +0100687 return e_unbalanced_groups;
Bram Moolenaardac13472019-09-16 21:06:21 +0200688 return NULL;
689}
690#endif
691
692/*
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +0000693 * Check for a "normal" directory or file name in some options. Disallow a
694 * path separator (slash and/or backslash), wildcards and characters that are
695 * often illegal in a file name. Be more permissive if "secure" is off.
696 */
697 static int
698check_illegal_path_names(int opt_idx, char_u **varp)
699{
700 return (((get_option_flags(opt_idx) & P_NFNAME)
701 && vim_strpbrk(*varp, (char_u *)(secure
702 ? "/\\*?[|;&<>\r\n" : "/\\*?[<>\r\n")) != NULL)
703 || ((get_option_flags(opt_idx) & P_NDNAME)
704 && vim_strpbrk(*varp, (char_u *)"*?[|;&<>\r\n") != NULL));
705}
706
707/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000708 * An option that accepts a list of flags is changed.
709 * e.g. 'viewoptions', 'switchbuf', 'casemap', etc.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000710 */
711 static char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000712did_set_opt_flags(char_u *val, char **values, unsigned *flagp, int list)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000713{
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000714 if (opt_strings_flags(val, values, flagp, list) == FAIL)
715 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000716
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000717 return NULL;
718}
719
720/*
721 * An option that accepts a list of string values is changed.
722 * e.g. 'nrformats', 'scrollopt', 'wildoptions', etc.
723 */
724 static char *
725did_set_opt_strings(char_u *val, char **values, int list)
726{
727 return did_set_opt_flags(val, values, NULL, list);
728}
729
730/*
731 * An option which is a list of flags is set. Valid values are in 'flags'.
732 */
733 static char *
Christian Brabandtb39b2402023-11-29 11:34:05 +0100734did_set_option_listflag(
735 char_u *val,
736 char_u *flags,
737 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +0100738 size_t errbuflen)
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000739{
740 char_u *s;
741
Yegappan Lakshmananc727b192023-03-03 12:26:15 +0000742 for (s = val; *s; ++s)
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000743 if (vim_strchr(flags, *s) == NULL)
Christian Brabandtb39b2402023-11-29 11:34:05 +0100744 return illegal_char(errbuf, errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000745
746 return NULL;
747}
748
749/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200750 * Expand an option that accepts a list of fixed string values with known
751 * number of items.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200752 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200753 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200754expand_set_opt_string(
755 optexpand_T *args,
756 char **values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +0200757 size_t numValues,
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200758 int *numMatches,
759 char_u ***matches)
760{
761 char_u *p;
762 regmatch_T *regmatch = args->oe_regmatch;
763 int include_orig_val = args->oe_include_orig_val;
764 char_u *option_val = args->oe_opt_value;
765
766 // Assume numValues is small since they are fixed enums, so just allocate
767 // upfront instead of needing two passes to calculate output size.
768 *matches = ALLOC_MULT(char_u *, numValues + 1);
769 if (*matches == NULL)
770 return FAIL;
771
772 int count = 0;
773
774 if (include_orig_val && *option_val != NUL)
775 {
776 p = vim_strsave(option_val);
777 if (p == NULL)
778 {
779 VIM_CLEAR(*matches);
780 return FAIL;
781 }
782 (*matches)[count++] = p;
783 }
784
785 for (char **val = values; *val != NULL; val++)
786 {
787 if (include_orig_val && *option_val != NUL)
788 {
789 if (STRCMP((char_u*)*val, option_val) == 0)
790 continue;
791 }
792 if (vim_regexec(regmatch, (char_u*)(*val), (colnr_T)0))
793 {
794 p = vim_strsave((char_u*)*val);
795 if (p == NULL)
796 {
797 if (count == 0)
798 {
799 VIM_CLEAR(*matches);
800 return FAIL;
801 }
802 else
803 break;
804 }
805 (*matches)[count++] = p;
806 }
807 }
808 if (count == 0)
809 {
810 VIM_CLEAR(*matches);
811 return FAIL;
812 }
813 *numMatches = count;
814 return OK;
815}
816
817static char_u *set_opt_callback_orig_option = NULL;
818static char_u *((*set_opt_callback_func)(expand_T *, int));
819
820/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200821 * Callback used by expand_set_opt_generic to also include the original value
822 * as the first item.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200823 */
824 static char_u *
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200825expand_set_opt_generic_cb(expand_T *xp, int idx)
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200826{
827 if (idx == 0)
828 {
829 if (set_opt_callback_orig_option != NULL)
830 return set_opt_callback_orig_option;
831 else
832 return (char_u *)""; // empty strings are ignored
833 }
834 return set_opt_callback_func(xp, idx - 1);
835}
836
837/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200838 * Expand an option with a callback that iterates through a list of possible
839 * names using an index.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200840 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200841 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200842expand_set_opt_generic(
843 optexpand_T *args,
844 char_u *((*func)(expand_T *, int)),
845 int *numMatches,
846 char_u ***matches)
847{
848 int ret;
849
850 set_opt_callback_orig_option = args->oe_include_orig_val ?
851 args->oe_opt_value : NULL;
852 set_opt_callback_func = func;
853
854 ret = ExpandGeneric(
855 (char_u*)"", // not using fuzzy as currently EXPAND_STRING_SETTING doesn't use it
856 args->oe_xp,
857 args->oe_regmatch,
858 matches,
859 numMatches,
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200860 expand_set_opt_generic_cb,
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200861 FALSE);
862
863 set_opt_callback_orig_option = NULL;
864 set_opt_callback_func = NULL;
865 return ret;
866}
867
Christian Brabandt9960ebc2023-10-05 22:17:09 +0200868# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200869static garray_T *expand_cb_ga;
870static optexpand_T *expand_cb_args;
871
872/*
873 * Callback provided to a function in expand_set_opt_callback. Will perform
874 * regex matching against the value and add to the list.
875 *
876 * Returns OK usually. Returns FAIL if it failed to allocate memory, and the
877 * caller should terminate the enumeration.
878 */
879 static int
880expand_set_opt_callback_cb(char_u *val)
881{
882 regmatch_T *regmatch = expand_cb_args->oe_regmatch;
883 expand_T *xp = expand_cb_args->oe_xp;
884 garray_T *ga = expand_cb_ga;
885 char_u *str;
886
887 if (val == NULL || *val == NUL)
888 return OK;
889
890 if (xp->xp_pattern[0] != NUL &&
891 !vim_regexec(regmatch, val, (colnr_T)0))
892 return OK;
893
894 str = vim_strsave_escaped(val, (char_u *)" \t\\");
895
896 if (str == NULL)
897 return FAIL;
898
899 if (ga_grow(ga, 1) == FAIL)
900 {
901 vim_free(str);
902 return FAIL;
903 }
904
905 ((char_u **)ga->ga_data)[ga->ga_len] = str;
906 ++ga->ga_len;
907 return OK;
908}
909
910/*
911 * Expand an option with a provided function that takes a callback. The
912 * function will enumerate through all options and call the callback to add it
913 * to the list.
914 *
915 * "func" is the enumerator function that will generate the list of options.
916 * "func_params" is a single parameter that will be passed to func.
917 */
918 static int
919expand_set_opt_callback(
920 optexpand_T *args,
921 void (*func)(optexpand_T *, void* params, int (*cb)(char_u *val)),
922 void *func_params,
923 int *numMatches,
924 char_u ***matches)
925{
926 garray_T ga;
927 int include_orig_val = args->oe_include_orig_val;
928 char_u *option_val = args->oe_opt_value;
929
930 ga_init2(&ga, sizeof(char *), 30);
931
932 if (include_orig_val && *option_val != NUL)
933 {
934 char_u *p = vim_strsave(option_val);
935 if (p == NULL)
936 return FAIL;
937 if (ga_grow(&ga, 1) == FAIL)
938 {
939 vim_free(p);
940 return FAIL;
941 }
942 ((char_u **)ga.ga_data)[ga.ga_len] = p;
943 ++ga.ga_len;
944 }
945
946 expand_cb_ga = &ga;
947 expand_cb_args = args;
948
949 func(args, func_params, expand_set_opt_callback_cb);
950
951 expand_cb_ga = NULL;
952 expand_cb_args = NULL;
953
954 *matches = ga.ga_data;
955 *numMatches = ga.ga_len;
956 return OK;
957}
Christian Brabandt9960ebc2023-10-05 22:17:09 +0200958#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200959
960/*
961 * Expand an option which is a list of flags.
962 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200963 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200964expand_set_opt_listflag(
965 optexpand_T *args,
966 char_u *flags,
967 int *numMatches,
968 char_u ***matches)
969{
970 char_u *p;
971 char_u *option_val = args->oe_opt_value;
972 char_u *cmdline_val = args->oe_set_arg;
973 int append = args->oe_append;
974 int include_orig_val = args->oe_include_orig_val && (*option_val != NUL);
975
Yee Cheng Chin6d113472023-10-02 21:38:39 +0200976 size_t num_flags = STRLEN(flags);
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200977
978 // Assume we only have small number of flags, so just allocate max size.
979 *matches = ALLOC_MULT(char_u *, num_flags + 1);
980 if (*matches == NULL)
981 return FAIL;
982
983 int count = 0;
984
985 if (include_orig_val)
986 {
987 p = vim_strsave(option_val);
988 if (p == NULL)
989 {
990 VIM_CLEAR(*matches);
991 return FAIL;
992 }
993 (*matches)[count++] = p;
994 }
995
996 for (char_u *flag = flags; *flag != NUL; flag++)
997 {
998 if (append && vim_strchr(option_val, *flag) != NULL)
999 continue;
1000
1001 if (vim_strchr(cmdline_val, *flag) == NULL)
1002 {
1003 if (include_orig_val
1004 && option_val[1] == NUL
1005 && *flag == option_val[0])
1006 {
1007 // This value is already used as the first choice as it's the
1008 // existing flag. Just skip it to avoid duplicate.
1009 continue;
1010 }
1011 p = vim_strnsave(flag, 1);
1012 if (p == NULL)
1013 {
1014 if (count == 0)
1015 {
1016 VIM_CLEAR(*matches);
1017 return FAIL;
1018 }
1019 else
1020 break;
1021 }
1022 (*matches)[count++] = p;
1023 }
1024 }
1025
1026 if (count == 0)
1027 {
1028 VIM_CLEAR(*matches);
1029 return FAIL;
1030 }
1031 *numMatches = count;
1032 return OK;
1033}
1034
1035/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001036 * The 'ambiwidth' option is changed.
1037 */
1038 char *
1039did_set_ambiwidth(optset_T *args UNUSED)
1040{
1041 if (check_opt_strings(p_ambw, p_ambw_values, FALSE) != OK)
1042 return e_invalid_argument;
1043
1044 return check_chars_options();
1045}
1046
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001047 int
1048expand_set_ambiwidth(optexpand_T *args, int *numMatches, char_u ***matches)
1049{
1050 return expand_set_opt_string(
1051 args,
1052 p_ambw_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001053 ARRAY_LENGTH(p_ambw_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001054 numMatches,
1055 matches);
1056}
1057
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001058/*
1059 * The 'background' option is changed.
1060 */
1061 char *
Gregory Anders83ad2722024-01-03 19:48:51 +01001062did_set_background(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001063{
1064 if (check_opt_strings(p_bg, p_bg_values, FALSE) == FAIL)
1065 return e_invalid_argument;
1066
Gregory Anders83ad2722024-01-03 19:48:51 +01001067 if (args->os_oldval.string != NULL && args->os_oldval.string[0] == *p_bg)
1068 // Value was not changed
1069 return NULL;
1070
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001071#ifdef FEAT_EVAL
1072 int dark = (*p_bg == 'd');
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001073#endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001074
1075 init_highlight(FALSE, FALSE);
1076
1077#ifdef FEAT_EVAL
1078 if (dark != (*p_bg == 'd')
1079 && get_var_value((char_u *)"g:colors_name") != NULL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001080 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001081 // The color scheme must have set 'background' back to another
1082 // value, that's not what we want here. Disable the color
1083 // scheme and set the colors again.
1084 do_unlet((char_u *)"g:colors_name", TRUE);
1085 free_string_option(p_bg);
1086 p_bg = vim_strsave((char_u *)(dark ? "dark" : "light"));
1087 check_string_option(&p_bg);
1088 init_highlight(FALSE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001089 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001090#endif
1091#ifdef FEAT_TERMINAL
1092 term_update_colors_all();
1093#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001094
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001095 return NULL;
1096}
1097
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001098 int
1099expand_set_background(optexpand_T *args, int *numMatches, char_u ***matches)
1100{
1101 return expand_set_opt_string(
1102 args,
1103 p_bg_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001104 ARRAY_LENGTH(p_bg_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001105 numMatches,
1106 matches);
1107}
1108
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001109/*
1110 * The 'backspace' option is changed.
1111 */
1112 char *
1113did_set_backspace(optset_T *args UNUSED)
1114{
1115 if (VIM_ISDIGIT(*p_bs))
1116 {
1117 if (*p_bs > '3' || p_bs[1] != NUL)
1118 return e_invalid_argument;
1119 }
1120 else if (check_opt_strings(p_bs, p_bs_values, TRUE) != OK)
1121 return e_invalid_argument;
1122
1123 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001124}
1125
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001126 int
1127expand_set_backspace(optexpand_T *args, int *numMatches, char_u ***matches)
1128{
1129 return expand_set_opt_string(
1130 args,
1131 p_bs_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001132 ARRAY_LENGTH(p_bs_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001133 numMatches,
1134 matches);
1135}
1136
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001137/*
1138 * The 'backupcopy' option is changed.
1139 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001140 char *
1141did_set_backupcopy(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001142{
1143 char_u *bkc = p_bkc;
1144 unsigned int *flags = &bkc_flags;
1145 char *errmsg = NULL;
1146
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001147 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001148 {
1149 bkc = curbuf->b_p_bkc;
1150 flags = &curbuf->b_bkc_flags;
1151 }
zeertzjq46dcd842024-11-03 09:10:50 +01001152 else if (!(args->os_flags & OPT_GLOBAL))
1153 // When using :set, clear the local flags.
1154 curbuf->b_bkc_flags = 0;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001155
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001156 if ((args->os_flags & OPT_LOCAL) && *bkc == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001157 // make the local value empty: use the global value
1158 *flags = 0;
1159 else
1160 {
1161 if (opt_strings_flags(bkc, p_bkc_values, flags, TRUE) != OK)
1162 errmsg = e_invalid_argument;
1163 if ((((int)*flags & BKC_AUTO) != 0)
1164 + (((int)*flags & BKC_YES) != 0)
1165 + (((int)*flags & BKC_NO) != 0) != 1)
1166 {
1167 // Must have exactly one of "auto", "yes" and "no".
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001168 (void)opt_strings_flags(args->os_oldval.string, p_bkc_values,
1169 flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001170 errmsg = e_invalid_argument;
1171 }
1172 }
1173
1174 return errmsg;
1175}
1176
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001177 int
1178expand_set_backupcopy(optexpand_T *args, int *numMatches, char_u ***matches)
1179{
1180 return expand_set_opt_string(
1181 args,
1182 p_bkc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001183 ARRAY_LENGTH(p_bkc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001184 numMatches,
1185 matches);
1186}
1187
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001188/*
1189 * The 'backupext' or the 'patchmode' option is changed.
1190 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001191 char *
1192did_set_backupext_or_patchmode(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001193{
1194 if (STRCMP(*p_bex == '.' ? p_bex + 1 : p_bex,
1195 *p_pm == '.' ? p_pm + 1 : p_pm) == 0)
1196 return e_backupext_and_patchmode_are_equal;
1197
1198 return NULL;
1199}
1200
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001201/*
1202 * The 'belloff' option is changed.
1203 */
1204 char *
1205did_set_belloff(optset_T *args UNUSED)
1206{
1207 return did_set_opt_flags(p_bo, p_bo_values, &bo_flags, TRUE);
1208}
1209
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001210 int
1211expand_set_belloff(optexpand_T *args, int *numMatches, char_u ***matches)
1212{
1213 return expand_set_opt_string(
1214 args,
1215 p_bo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001216 ARRAY_LENGTH(p_bo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001217 numMatches,
1218 matches);
1219}
1220
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001221#if defined(FEAT_LINEBREAK) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001222/*
zeertzjqeac3fdc2024-02-03 18:08:09 +01001223 * The 'breakat' option is changed.
1224 */
1225 char *
1226did_set_breakat(optset_T *args UNUSED)
1227{
1228 char_u *p;
1229 int i;
1230
1231 for (i = 0; i < 256; i++)
1232 breakat_flags[i] = FALSE;
1233
1234 if (p_breakat != NULL)
1235 for (p = p_breakat; *p; p++)
1236 breakat_flags[*p] = TRUE;
1237
1238 return NULL;
1239}
1240
1241/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001242 * The 'breakindentopt' option is changed.
1243 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001244 char *
Millyb38700a2024-10-22 22:59:39 +02001245did_set_breakindentopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001246{
Millyb38700a2024-10-22 22:59:39 +02001247 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001248
Millyb38700a2024-10-22 22:59:39 +02001249 if (briopt_check(*varp, varp == &curwin->w_p_briopt ? curwin : NULL)
1250 == FAIL)
1251 return e_invalid_argument;
1252
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001253 // list setting requires a redraw
Millyb38700a2024-10-22 22:59:39 +02001254 if (varp == &curwin->w_p_briopt && curwin->w_briopt_list)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001255 redraw_all_later(UPD_NOT_VALID);
1256
Millyb38700a2024-10-22 22:59:39 +02001257 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001258}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001259
1260 int
1261expand_set_breakindentopt(optexpand_T *args, int *numMatches, char_u ***matches)
1262{
1263 return expand_set_opt_string(
1264 args,
1265 p_briopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001266 ARRAY_LENGTH(p_briopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001267 numMatches,
1268 matches);
1269}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001270#endif
1271
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001272#if defined(FEAT_BROWSE) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001273/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001274 * The 'browsedir' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001275 */
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00001276 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001277did_set_browsedir(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001278{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001279 if (check_opt_strings(p_bsdir, p_bsdir_values, FALSE) != OK
1280 && !mch_isdir(p_bsdir))
1281 return e_invalid_argument;
1282
1283 return NULL;
1284}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001285
1286 int
1287expand_set_browsedir(optexpand_T *args, int *numMatches, char_u ***matches)
1288{
1289 return expand_set_opt_string(
1290 args,
1291 p_bsdir_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001292 ARRAY_LENGTH(p_bsdir_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001293 numMatches,
1294 matches);
1295}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001296#endif
1297
1298/*
1299 * The 'bufhidden' option is changed.
1300 */
1301 char *
1302did_set_bufhidden(optset_T *args UNUSED)
1303{
1304 return did_set_opt_strings(curbuf->b_p_bh, p_bufhidden_values, FALSE);
1305}
1306
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001307 int
1308expand_set_bufhidden(optexpand_T *args, int *numMatches, char_u ***matches)
1309{
1310 return expand_set_opt_string(
1311 args,
1312 p_bufhidden_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001313 ARRAY_LENGTH(p_bufhidden_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001314 numMatches,
1315 matches);
1316}
1317
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001318/*
1319 * The 'buftype' option is changed.
1320 */
1321 char *
1322did_set_buftype(optset_T *args UNUSED)
1323{
1324 if (check_opt_strings(curbuf->b_p_bt, p_buftype_values, FALSE) != OK)
1325 return e_invalid_argument;
1326
1327 if (curwin->w_status_height)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001328 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001329 curwin->w_redr_status = TRUE;
1330 redraw_later(UPD_VALID);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001331 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001332 curbuf->b_help = (curbuf->b_p_bt[0] == 'h');
1333 redraw_titles();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001334
1335 return NULL;
1336}
1337
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001338 int
1339expand_set_buftype(optexpand_T *args, int *numMatches, char_u ***matches)
1340{
1341 return expand_set_opt_string(
1342 args,
1343 p_buftype_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001344 ARRAY_LENGTH(p_buftype_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001345 numMatches,
1346 matches);
1347}
1348
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001349/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001350 * The 'casemap' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001351 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001352 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001353did_set_casemap(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001354{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001355 return did_set_opt_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
1356}
1357
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001358 int
1359expand_set_casemap(optexpand_T *args, int *numMatches, char_u ***matches)
1360{
1361 return expand_set_opt_string(
1362 args,
1363 p_cmp_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001364 ARRAY_LENGTH(p_cmp_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001365 numMatches,
1366 matches);
1367}
1368
1369#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1370 int
1371expand_set_clipboard(optexpand_T *args, int *numMatches, char_u ***matches)
1372{
1373 return expand_set_opt_string(
1374 args,
1375 p_cb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001376 ARRAY_LENGTH(p_cb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001377 numMatches,
1378 matches);
1379}
1380#endif
1381
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001382/*
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001383 * The global 'listchars' or 'fillchars' option is changed.
1384 */
1385 static char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01001386did_set_global_listfillchars(char_u *val, int opt_lcs, int opt_flags,
1387 char *errbuf, size_t errbuflen)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001388{
1389 char *errmsg = NULL;
1390 char_u **local_ptr = opt_lcs ? &curwin->w_p_lcs : &curwin->w_p_fcs;
1391
1392 // only apply the global value to "curwin" when it does not have a
1393 // local value
1394 if (opt_lcs)
1395 errmsg = set_listchars_option(curwin, val,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001396 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL),
1397 errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001398 else
1399 errmsg = set_fillchars_option(curwin, val,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001400 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL),
1401 errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001402 if (errmsg != NULL)
1403 return errmsg;
1404
1405 tabpage_T *tp;
1406 win_T *wp;
1407
1408 // If the current window is set to use the global
1409 // 'listchars'/'fillchars' value, clear the window-local value.
1410 if (!(opt_flags & OPT_GLOBAL))
1411 clear_string_option(local_ptr);
1412 FOR_ALL_TAB_WINDOWS(tp, wp)
1413 {
1414 // If the current window has a local value need to apply it
1415 // again, it was changed when setting the global value.
1416 // If no error was returned above, we don't expect an error
1417 // here, so ignore the return value.
1418 if (opt_lcs)
1419 {
1420 if (*wp->w_p_lcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001421 (void)set_listchars_option(wp, wp->w_p_lcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001422 }
1423 else
1424 {
1425 if (*wp->w_p_fcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001426 (void)set_fillchars_option(wp, wp->w_p_fcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001427 }
1428 }
1429
1430 redraw_all_later(UPD_NOT_VALID);
1431
1432 return NULL;
1433}
1434
1435/*
1436 * The 'fillchars' option or the 'listchars' option is changed.
1437 */
1438 char *
1439did_set_chars_option(optset_T *args)
1440{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001441 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001442 char *errmsg = NULL;
1443
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001444 if ( varp == &p_lcs // global 'listchars'
1445 || varp == &p_fcs) // global 'fillchars'
1446 errmsg = did_set_global_listfillchars(*varp, varp == &p_lcs,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001447 args->os_flags, args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001448 else if (varp == &curwin->w_p_lcs) // local 'listchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001449 errmsg = set_listchars_option(curwin, *varp, TRUE,
1450 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001451 else if (varp == &curwin->w_p_fcs) // local 'fillchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001452 errmsg = set_fillchars_option(curwin, *varp, TRUE,
1453 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001454
1455 return errmsg;
1456}
1457
1458/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001459 * Expand 'fillchars' or 'listchars' option value.
1460 */
1461 int
1462expand_set_chars_option(optexpand_T *args, int *numMatches, char_u ***matches)
1463{
1464 char_u **varp = (char_u **)args->oe_varp;
1465 int is_lcs = (varp == &p_lcs || varp == &curwin->w_p_lcs);
1466 return expand_set_opt_generic(
1467 args,
1468 is_lcs ? get_listchars_name : get_fillchars_name,
1469 numMatches,
1470 matches);
1471}
1472
1473/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001474 * The 'cinoptions' option is changed.
1475 */
1476 char *
1477did_set_cinoptions(optset_T *args UNUSED)
1478{
1479 // TODO: recognize errors
1480 parse_cino(curbuf);
1481
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001482 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001483}
1484
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00001485#if defined(FEAT_SYN_HL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001486/*
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001487 * The 'colorcolumn' option is changed.
1488 */
1489 char *
Millya441a3e2024-10-22 22:43:01 +02001490did_set_colorcolumn(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001491{
Millya441a3e2024-10-22 22:43:01 +02001492 char_u **varp = (char_u **)args->os_varp;
1493
1494 return check_colorcolumn(*varp, varp == &curwin->w_p_cc ? curwin : NULL);
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001495}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001496#endif
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001497
1498/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001499 * The 'comments' option is changed.
1500 */
1501 char *
1502did_set_comments(optset_T *args)
1503{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001504 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001505 char_u *s;
1506 char *errmsg = NULL;
1507
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001508 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001509 {
1510 while (*s && *s != ':')
1511 {
1512 if (vim_strchr((char_u *)COM_ALL, *s) == NULL
1513 && !VIM_ISDIGIT(*s) && *s != '-')
1514 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01001515 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001516 break;
1517 }
1518 ++s;
1519 }
1520 if (*s++ == NUL)
1521 errmsg = e_missing_colon;
1522 else if (*s == ',' || *s == NUL)
1523 errmsg = e_zero_length_string;
1524 if (errmsg != NULL)
1525 break;
1526 while (*s && *s != ',')
1527 {
1528 if (*s == '\\' && s[1] != NUL)
1529 ++s;
1530 ++s;
1531 }
1532 s = skip_to_option_part(s);
1533 }
1534
1535 return errmsg;
1536}
1537
1538#if defined(FEAT_FOLDING) || defined(PROTO)
1539/*
1540 * The 'commentstring' option is changed.
1541 */
1542 char *
1543did_set_commentstring(optset_T *args)
1544{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001545 char_u **varp = (char_u **)args->os_varp;
1546
1547 if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001548 return e_commentstring_must_be_empty_or_contain_str;
1549
1550 return NULL;
1551}
1552#endif
1553
1554/*
Girish Palyacbe53192025-04-14 22:13:15 +02001555 * Check if value for 'complete' is valid when 'complete' option is changed.
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001556 */
1557 char *
1558did_set_complete(optset_T *args)
1559{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001560 char_u **varp = (char_u **)args->os_varp;
Girish Palyacbe53192025-04-14 22:13:15 +02001561 char_u *p = NULL;
1562 char_u buffer[LSIZE];
1563 char_u *buf_ptr;
1564 int escape;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001565
Girish Palyacbe53192025-04-14 22:13:15 +02001566 for (p = *varp; *p; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001567 {
Girish Palyacbe53192025-04-14 22:13:15 +02001568 vim_memset(buffer, 0, LSIZE);
1569 buf_ptr = buffer;
1570 escape = 0;
1571
1572 // Extract substring while handling escaped commas
1573 while (*p && (*p != ',' || escape) && buf_ptr < (buffer + LSIZE - 1))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001574 {
Girish Palyacbe53192025-04-14 22:13:15 +02001575 if (*p == '\\' && *(p + 1) == ',')
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001576 {
Girish Palyacbe53192025-04-14 22:13:15 +02001577 escape = 1; // Mark escape mode
1578 p++; // Skip '\'
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001579 }
1580 else
1581 {
Girish Palyacbe53192025-04-14 22:13:15 +02001582 escape = 0;
1583 *buf_ptr++ = *p;
1584 }
1585 p++;
1586 }
1587 *buf_ptr = NUL;
1588
1589 if (vim_strchr((char_u *)".wbuksid]tUfo", *buffer) == NULL)
1590 return illegal_char(args->os_errbuf, args->os_errbuflen, *buffer);
1591
1592 if (!vim_strchr((char_u *)"ksf", *buffer) && *(buffer + 1) != NUL)
1593 {
1594 if (args->os_errbuf)
1595 {
1596 vim_snprintf((char *)args->os_errbuf, args->os_errbuflen,
1597 _(e_illegal_character_after_chr), *buffer);
1598 return args->os_errbuf;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001599 }
1600 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001601
Girish Palyacbe53192025-04-14 22:13:15 +02001602 // Skip comma and spaces
1603 while (*p == ',' || *p == ' ')
1604 p++;
1605 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001606 return NULL;
1607}
1608
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001609 int
1610expand_set_complete(optexpand_T *args, int *numMatches, char_u ***matches)
1611{
1612 static char *(p_cpt_values[]) = {
Girish Palyacbe53192025-04-14 22:13:15 +02001613 ".", "w", "b", "u", "k", "kspell", "s", "i", "d", "]", "t", "U", "f", "o",
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001614 NULL};
1615 return expand_set_opt_string(
1616 args,
1617 p_cpt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001618 ARRAY_LENGTH(p_cpt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001619 numMatches,
1620 matches);
1621}
1622
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001623/*
1624 * The 'completeopt' option is changed.
1625 */
1626 char *
1627did_set_completeopt(optset_T *args UNUSED)
1628{
zeertzjq529b9ad2024-06-05 20:27:06 +02001629 char_u *cot = p_cot;
1630 unsigned *flags = &cot_flags;
1631
1632 if (args->os_flags & OPT_LOCAL)
1633 {
1634 cot = curbuf->b_p_cot;
1635 flags = &curbuf->b_cot_flags;
1636 }
zeertzjq46dcd842024-11-03 09:10:50 +01001637 else if (!(args->os_flags & OPT_GLOBAL))
1638 // When using :set, clear the local flags.
1639 curbuf->b_cot_flags = 0;
zeertzjq529b9ad2024-06-05 20:27:06 +02001640
1641 if (check_opt_strings(cot, p_cot_values, TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001642 return e_invalid_argument;
1643
zeertzjq529b9ad2024-06-05 20:27:06 +02001644 if (opt_strings_flags(cot, p_cot_values, flags, TRUE) != OK)
1645 return e_invalid_argument;
1646
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001647 return NULL;
1648}
1649
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001650 int
1651expand_set_completeopt(optexpand_T *args, int *numMatches, char_u ***matches)
1652{
1653 return expand_set_opt_string(
1654 args,
1655 p_cot_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001656 ARRAY_LENGTH(p_cot_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001657 numMatches,
1658 matches);
1659}
1660
glepnir6a89c942024-10-01 20:32:12 +02001661/*
glepnirf31cfa22025-03-06 21:59:13 +01001662 * The 'completefuzzycollect' option is changed.
1663 */
1664 char *
1665did_set_completefuzzycollect(optset_T *args UNUSED)
1666{
1667 if (opt_strings_flags(p_cfc, p_cfc_values, &cfc_flags, TRUE) != OK)
1668 return e_invalid_argument;
1669 return NULL;
1670}
1671
zeertzjq53d59ec2025-03-07 19:09:09 +01001672 int
1673expand_set_completefuzzycollect(
1674 optexpand_T *args,
1675 int *numMatches,
1676 char_u ***matches)
1677{
1678 return expand_set_opt_string(
1679 args,
1680 p_cfc_values,
1681 ARRAY_LENGTH(p_cfc_values) - 1,
1682 numMatches,
1683 matches);
1684}
1685
glepnirf31cfa22025-03-06 21:59:13 +01001686/*
glepnir6a89c942024-10-01 20:32:12 +02001687 * The 'completeitemalign' option is changed.
1688 */
1689 char *
1690did_set_completeitemalign(optset_T *args UNUSED)
1691{
1692 char_u *p = p_cia;
1693 unsigned new_cia_flags = 0;
1694 int seen[3] = { FALSE, FALSE, FALSE };
1695 int count = 0;
1696 char_u buf[10];
1697
1698 while (*p)
1699 {
1700 copy_option_part(&p, buf, sizeof(buf), ",");
1701 if (count >= 3)
1702 return e_invalid_argument;
1703
1704 if (STRCMP(buf, "abbr") == 0)
1705 {
1706 if (seen[CPT_ABBR])
1707 return e_invalid_argument;
1708 new_cia_flags = new_cia_flags * 10 + CPT_ABBR;
1709 seen[CPT_ABBR] = TRUE;
1710 count++;
1711 }
1712 else if (STRCMP(buf, "kind") == 0)
1713 {
1714 if (seen[CPT_KIND])
1715 return e_invalid_argument;
1716 new_cia_flags = new_cia_flags * 10 + CPT_KIND;
1717 seen[CPT_KIND] = TRUE;
1718 count++;
1719 }
1720 else if (STRCMP(buf, "menu") == 0)
1721 {
1722 if (seen[CPT_MENU])
1723 return e_invalid_argument;
1724 new_cia_flags = new_cia_flags * 10 + CPT_MENU;
1725 seen[CPT_MENU] = TRUE;
1726 count++;
1727 }
1728 else
1729 return e_invalid_argument;
1730 }
1731 if (new_cia_flags == 0 || count != 3)
1732 return e_invalid_argument;
1733
1734 cia_flags = new_cia_flags;
1735 return NULL;
1736}
1737
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001738#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1739/*
1740 * The 'completepopup' option is changed.
1741 */
1742 char *
1743did_set_completepopup(optset_T *args UNUSED)
1744{
1745 if (parse_completepopup(NULL) == FAIL)
1746 return e_invalid_argument;
1747
1748 popup_close_info();
1749 return NULL;
1750}
1751#endif
1752
1753#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1754/*
1755 * The 'completeslash' option is changed.
1756 */
1757 char *
1758did_set_completeslash(optset_T *args UNUSED)
1759{
1760 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1761 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
1762 return e_invalid_argument;
1763
1764 return NULL;
1765}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001766
1767 int
1768expand_set_completeslash(optexpand_T *args, int *numMatches, char_u ***matches)
1769{
1770 return expand_set_opt_string(
1771 args,
1772 p_csl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001773 ARRAY_LENGTH(p_csl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001774 numMatches,
1775 matches);
1776}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001777#endif
1778
1779#if defined(FEAT_CONCEAL) || defined(PROTO)
1780/*
1781 * The 'concealcursor' option is changed.
1782 */
1783 char *
1784did_set_concealcursor(optset_T *args)
1785{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001786 char_u **varp = (char_u **)args->os_varp;
1787
Christian Brabandtb39b2402023-11-29 11:34:05 +01001788 return did_set_option_listflag(*varp, (char_u *)COCU_ALL, args->os_errbuf,
1789 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001790}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001791
1792 int
1793expand_set_concealcursor(optexpand_T *args, int *numMatches, char_u ***matches)
1794{
1795 return expand_set_opt_listflag(args, (char_u*)COCU_ALL, numMatches, matches);
1796}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001797#endif
1798
1799/*
1800 * The 'cpoptions' option is changed.
1801 */
1802 char *
1803did_set_cpoptions(optset_T *args)
1804{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001805 char_u **varp = (char_u **)args->os_varp;
1806
Christian Brabandtb39b2402023-11-29 11:34:05 +01001807 return did_set_option_listflag(*varp, (char_u *)CPO_ALL, args->os_errbuf,
1808 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001809}
1810
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001811 int
1812expand_set_cpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
1813{
1814 return expand_set_opt_listflag(args, (char_u*)CPO_ALL, numMatches, matches);
1815}
1816
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001817#if defined(FEAT_CRYPT) || defined(PROTO)
1818/*
1819 * The 'cryptkey' option is changed.
1820 */
1821 char *
1822did_set_cryptkey(optset_T *args)
1823{
1824 // Make sure the ":set" command doesn't show the new value in the
1825 // history.
1826 remove_key_from_history();
1827
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02001828 if (args->os_op != OP_NONE)
1829 // Don't allow set+=/-=/^= as they can allow for substring guessing
1830 return e_invalid_argument;
1831
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001832 if (STRCMP(curbuf->b_p_key, args->os_oldval.string) != 0)
1833 {
1834 // Need to update the swapfile.
1835 ml_set_crypt_key(curbuf, args->os_oldval.string,
1836 *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
1837 changed_internal();
1838 }
Christian Brabandt19e6c4f2023-06-27 18:57:10 +01001839# ifdef FEAT_SODIUM
1840 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
1841 crypt_sodium_lock_key(args->os_newval.string);
1842# endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001843
1844 return NULL;
1845}
1846
1847/*
1848 * The 'cryptmethod' option is changed.
1849 */
1850 char *
1851did_set_cryptmethod(optset_T *args)
1852{
1853 char_u *p;
1854 char_u *s;
1855
1856 if (args->os_flags & OPT_LOCAL)
1857 p = curbuf->b_p_cm;
1858 else
1859 p = p_cm;
1860 if (check_opt_strings(p, p_cm_values, TRUE) != OK)
1861 return e_invalid_argument;
1862 else if (crypt_self_test() == FAIL)
1863 return e_invalid_argument;
1864
1865 // When setting the global value to empty, make it "zip".
1866 if (*p_cm == NUL)
1867 {
1868 free_string_option(p_cm);
1869 p_cm = vim_strsave((char_u *)"zip");
1870 }
1871 // When using ":set cm=name" the local value is going to be empty.
1872 // Do that here, otherwise the crypt functions will still use the
1873 // local value.
1874 if ((args->os_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1875 {
1876 free_string_option(curbuf->b_p_cm);
1877 curbuf->b_p_cm = empty_option;
1878 }
1879
1880 // Need to update the swapfile when the effective method changed.
1881 // Set "s" to the effective old value, "p" to the effective new
1882 // method and compare.
1883 if ((args->os_flags & OPT_LOCAL) && *args->os_oldval.string == NUL)
1884 s = p_cm; // was previously using the global value
1885 else
1886 s = args->os_oldval.string;
1887 if (*curbuf->b_p_cm == NUL)
1888 p = p_cm; // is now using the global value
1889 else
1890 p = curbuf->b_p_cm;
1891 if (STRCMP(s, p) != 0)
1892 ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1893
1894 // If the global value changes need to update the swapfile for all
1895 // buffers using that value.
1896 if ((args->os_flags & OPT_GLOBAL)
1897 && STRCMP(p_cm, args->os_oldval.string) != 0)
1898 {
1899 buf_T *buf;
1900
1901 FOR_ALL_BUFFERS(buf)
1902 if (buf != curbuf && *buf->b_p_cm == NUL)
1903 ml_set_crypt_key(buf, buf->b_p_key, args->os_oldval.string);
1904 }
1905 return NULL;
1906}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001907
1908 int
1909expand_set_cryptmethod(optexpand_T *args, int *numMatches, char_u ***matches)
1910{
1911 return expand_set_opt_string(
1912 args,
1913 p_cm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001914 ARRAY_LENGTH(p_cm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001915 numMatches,
1916 matches);
1917}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001918#endif
1919
1920#if (defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1921/*
1922 * The 'cscopequickfix' option is changed.
1923 */
1924 char *
1925did_set_cscopequickfix(optset_T *args UNUSED)
1926{
1927 char_u *p;
1928
1929 if (p_csqf == NULL)
1930 return NULL;
1931
1932 p = p_csqf;
1933 while (*p != NUL)
1934 {
1935 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
1936 || p[1] == NUL
1937 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
1938 || (p[2] != NUL && p[2] != ','))
1939 return e_invalid_argument;
1940 else if (p[2] == NUL)
1941 break;
1942 else
1943 p += 3;
1944 }
1945
1946 return NULL;
1947}
1948#endif
1949
1950#if defined(FEAT_SYN_HL) || defined(PROTO)
1951/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001952 * The 'cursorlineopt' option is changed.
1953 */
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001954 char *
1955did_set_cursorlineopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001956{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001957 char_u **varp = (char_u **)args->os_varp;
1958
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001959 // This could be changed to use opt_strings_flags() instead.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001960 if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001961 return e_invalid_argument;
1962
1963 return NULL;
1964}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001965
1966 int
1967expand_set_cursorlineopt(optexpand_T *args, int *numMatches, char_u ***matches)
1968{
1969 return expand_set_opt_string(
1970 args,
1971 p_culopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001972 ARRAY_LENGTH(p_culopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001973 numMatches,
1974 matches);
1975}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001976#endif
1977
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001978/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001979 * The 'debug' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001980 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001981 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001982did_set_debug(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001983{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001984 return did_set_opt_strings(p_debug, p_debug_values, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001985}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001986
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001987 int
1988expand_set_debug(optexpand_T *args, int *numMatches, char_u ***matches)
1989{
1990 return expand_set_opt_string(
1991 args,
1992 p_debug_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001993 ARRAY_LENGTH(p_debug_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001994 numMatches,
1995 matches);
1996}
1997
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001998#if defined(FEAT_DIFF) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001999/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002000 * The 'diffopt' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002001 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002002 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002003did_set_diffopt(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002004{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002005 if (diffopt_changed() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002006 return e_invalid_argument;
2007
2008 return NULL;
2009}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002010
2011 int
2012expand_set_diffopt(optexpand_T *args, int *numMatches, char_u ***matches)
2013{
2014 expand_T *xp = args->oe_xp;
2015
2016 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2017 {
2018 // Within "algorithm:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002019 int algo_len = (int)STRLEN("algorithm:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002020 if (xp->xp_pattern - args->oe_set_arg >= algo_len &&
2021 STRNCMP(xp->xp_pattern - algo_len, "algorithm:", algo_len) == 0)
2022 {
2023 return expand_set_opt_string(
2024 args,
2025 p_dip_algorithm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002026 ARRAY_LENGTH(p_dip_algorithm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002027 numMatches,
2028 matches);
2029 }
Yee Cheng Chin9943d472025-03-26 19:41:02 +01002030 // Within "inline:", we have a subgroup of possible options.
2031 int inline_len = (int)STRLEN("inline:");
2032 if (xp->xp_pattern - args->oe_set_arg >= inline_len &&
2033 STRNCMP(xp->xp_pattern - inline_len, "inline:", inline_len) == 0)
2034 {
2035 return expand_set_opt_string(
2036 args,
2037 p_dip_inline_values,
2038 ARRAY_LENGTH(p_dip_inline_values) - 1,
2039 numMatches,
2040 matches);
2041 }
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002042 return FAIL;
2043 }
2044
2045 return expand_set_opt_string(
2046 args,
2047 p_dip_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002048 ARRAY_LENGTH(p_dip_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002049 numMatches,
2050 matches);
2051}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002052#endif
2053
2054/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002055 * The 'display' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002056 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002057 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002058did_set_display(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002059{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002060 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002061 return e_invalid_argument;
2062
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002063 (void)init_chartab();
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002064 return NULL;
2065}
2066
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002067 int
2068expand_set_display(optexpand_T *args, int *numMatches, char_u ***matches)
2069{
2070 return expand_set_opt_string(
2071 args,
2072 p_dy_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002073 ARRAY_LENGTH(p_dy_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002074 numMatches,
2075 matches);
2076}
2077
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002078/*
2079 * The 'eadirection' option is changed.
2080 */
2081 char *
2082did_set_eadirection(optset_T *args UNUSED)
2083{
2084 return did_set_opt_strings(p_ead, p_ead_values, FALSE);
2085}
2086
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002087 int
2088expand_set_eadirection(optexpand_T *args, int *numMatches, char_u ***matches)
2089{
2090 return expand_set_opt_string(
2091 args,
2092 p_ead_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002093 ARRAY_LENGTH(p_ead_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002094 numMatches,
2095 matches);
2096}
2097
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002098/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002099 * One of the 'encoding', 'fileencoding', 'termencoding' or 'makeencoding'
2100 * options is changed.
2101 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002102 char *
2103did_set_encoding(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002104{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002105 char_u **varp = (char_u **)args->os_varp;
2106 char_u **gvarp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002107 char *errmsg = NULL;
2108 char_u *p;
2109
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002110 // Get the global option to compare with, otherwise we would have to check
2111 // two values for all local options.
2112 gvarp = (char_u **)get_option_varp_scope(args->os_idx, OPT_GLOBAL);
2113
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002114 if (gvarp == &p_fenc)
2115 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002116 if (!curbuf->b_p_ma && args->os_flags != OPT_GLOBAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002117 errmsg = e_cannot_make_changes_modifiable_is_off;
2118 else if (vim_strchr(*varp, ',') != NULL)
2119 // No comma allowed in 'fileencoding'; catches confusing it
2120 // with 'fileencodings'.
2121 errmsg = e_invalid_argument;
2122 else
2123 {
2124 // May show a "+" in the title now.
2125 redraw_titles();
2126 // Add 'fileencoding' to the swap file.
2127 ml_setflags(curbuf);
2128 }
2129 }
2130 if (errmsg == NULL)
2131 {
2132 // canonize the value, so that STRCMP() can be used on it
2133 p = enc_canonize(*varp);
2134 if (p != NULL)
2135 {
2136 vim_free(*varp);
2137 *varp = p;
2138 }
2139 if (varp == &p_enc)
2140 {
2141 errmsg = mb_init();
2142 redraw_titles();
2143 }
2144 }
2145
2146#if defined(FEAT_GUI_GTK)
2147 if (errmsg == NULL && varp == &p_tenc && gui.in_use)
2148 {
2149 // GTK uses only a single encoding, and that is UTF-8.
2150 if (STRCMP(p_tenc, "utf-8") != 0)
2151 errmsg = e_cannot_be_changed_in_gtk_GUI;
2152 }
2153#endif
2154
2155 if (errmsg == NULL)
2156 {
2157#ifdef FEAT_KEYMAP
2158 // When 'keymap' is used and 'encoding' changes, reload the keymap
2159 // (with another encoding).
2160 if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
2161 (void)keymap_init();
2162#endif
2163
2164 // When 'termencoding' is not empty and 'encoding' changes or when
2165 // 'termencoding' changes, need to setup for keyboard input and
2166 // display output conversion.
2167 if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
2168 {
2169 if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
2170 || convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
2171 {
2172 semsg(_(e_cannot_convert_between_str_and_str),
2173 p_tenc, p_enc);
2174 errmsg = e_invalid_argument;
2175 }
2176 }
2177
2178#if defined(MSWIN)
K.Takatace3189d2023-02-15 19:13:43 +00002179 // $HOME, $VIM and $VIMRUNTIME may have characters in active code page.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002180 if (varp == &p_enc)
K.Takatace3189d2023-02-15 19:13:43 +00002181 {
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002182 init_homedir();
K.Takatace3189d2023-02-15 19:13:43 +00002183 init_vimdir();
2184 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002185#endif
2186 }
2187
2188 return errmsg;
2189}
2190
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002191 int
2192expand_set_encoding(optexpand_T *args, int *numMatches, char_u ***matches)
2193{
2194 return expand_set_opt_generic(
2195 args,
2196 get_encoding_name,
2197 numMatches,
2198 matches);
2199}
2200
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002201/*
Luuk van Baalb7147f82025-02-08 18:52:39 +01002202 * The 'eventignore(win)' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002203 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002204 char *
Luuk van Baalb7147f82025-02-08 18:52:39 +01002205did_set_eventignore(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002206{
Luuk van Baalb7147f82025-02-08 18:52:39 +01002207 char_u **varp = (char_u **)args->os_varp;
2208
2209 if (check_ei(*varp) == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002210 return e_invalid_argument;
2211 return NULL;
2212}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002213
Luuk van Baalb7147f82025-02-08 18:52:39 +01002214static int expand_eiw = FALSE;
2215
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002216 static char_u *
2217get_eventignore_name(expand_T *xp, int idx)
2218{
Luuk van Baalb7147f82025-02-08 18:52:39 +01002219 // 'eventignore(win)' allows special keyword "all" in addition to
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002220 // all event names.
2221 if (idx == 0)
2222 return (char_u *)"all";
Luuk van Baalb7147f82025-02-08 18:52:39 +01002223 return get_event_name_no_group(xp, idx - 1, expand_eiw);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002224}
2225
2226 int
2227expand_set_eventignore(optexpand_T *args, int *numMatches, char_u ***matches)
2228{
Luuk van Baalb7147f82025-02-08 18:52:39 +01002229 expand_eiw = args->oe_varp != (char_u *)&p_ei;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002230 return expand_set_opt_generic(
2231 args,
2232 get_eventignore_name,
2233 numMatches,
2234 matches);
2235}
2236
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002237/*
2238 * The 'fileformat' option is changed.
2239 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002240 char *
2241did_set_fileformat(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002242{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002243 char_u **varp = (char_u **)args->os_varp;
2244
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002245 if (!curbuf->b_p_ma && !(args->os_flags & OPT_GLOBAL))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002246 return e_cannot_make_changes_modifiable_is_off;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002247 else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002248 return e_invalid_argument;
2249
2250 // may also change 'textmode'
2251 if (get_fileformat(curbuf) == EOL_DOS)
2252 curbuf->b_p_tx = TRUE;
2253 else
2254 curbuf->b_p_tx = FALSE;
2255 redraw_titles();
2256 // update flag in swap file
2257 ml_setflags(curbuf);
2258 // Redraw needed when switching to/from "mac": a CR in the text
2259 // will be displayed differently.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002260 if (get_fileformat(curbuf) == EOL_MAC || *args->os_oldval.string == 'm')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002261 redraw_curbuf_later(UPD_NOT_VALID);
2262
2263 return NULL;
2264}
2265
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002266 int
2267expand_set_fileformat(optexpand_T *args, int *numMatches, char_u ***matches)
2268{
2269 return expand_set_opt_string(
2270 args,
2271 p_ff_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002272 ARRAY_LENGTH(p_ff_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002273 numMatches,
2274 matches);
2275}
2276
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002277/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002278 * Function given to ExpandGeneric() to obtain the possible arguments of the
2279 * fileformat options.
2280 */
2281 char_u *
2282get_fileformat_name(expand_T *xp UNUSED, int idx)
2283{
2284 if (idx >= (int)ARRAY_LENGTH(p_ff_values))
2285 return NULL;
2286
2287 return (char_u*)p_ff_values[idx];
2288}
2289
2290/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002291 * The 'fileformats' option is changed.
2292 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002293 char *
2294did_set_fileformats(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002295{
2296 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
2297 return e_invalid_argument;
2298
2299 // also change 'textauto'
2300 if (*p_ffs == NUL)
2301 p_ta = FALSE;
2302 else
2303 p_ta = TRUE;
2304
2305 return NULL;
2306}
2307
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002308/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002309 * The 'filetype' or the 'syntax' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002310 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002311 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002312did_set_filetype_or_syntax(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002313{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002314 char_u **varp = (char_u **)args->os_varp;
2315
2316 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002317 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002318
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002319 args->os_value_changed = STRCMP(args->os_oldval.string, *varp) != 0;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002320
2321 // Since we check the value, there is no need to set P_INSECURE,
2322 // even when the value comes from a modeline.
2323 args->os_value_checked = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002324
2325 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002326}
2327
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002328#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002329/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002330 * The 'foldclose' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002331 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002332 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002333did_set_foldclose(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002334{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002335 return did_set_opt_strings(p_fcl, p_fcl_values, TRUE);
2336}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002337
2338 int
2339expand_set_foldclose(optexpand_T *args, int *numMatches, char_u ***matches)
2340{
2341 return expand_set_opt_string(
2342 args,
2343 p_fcl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002344 ARRAY_LENGTH(p_fcl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002345 numMatches,
2346 matches);
2347}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002348#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002349
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002350#if (defined(FEAT_EVAL) && defined(FEAT_FOLDING)) || defined(PROTO)
2351/*
2352 * The 'foldexpr' option is changed.
2353 */
2354 char *
2355did_set_foldexpr(optset_T *args)
2356{
2357 (void)did_set_optexpr(args);
2358 if (foldmethodIsExpr(curwin))
2359 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002360 return NULL;
2361}
2362#endif
2363
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002364#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002365/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002366 * The 'foldignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002367 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002368 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002369did_set_foldignore(optset_T *args UNUSED)
2370{
2371 if (foldmethodIsIndent(curwin))
2372 foldUpdateAll(curwin);
2373 return NULL;
2374}
2375
2376/*
2377 * The 'foldmarker' option is changed.
2378 */
2379 char *
2380did_set_foldmarker(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002381{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002382 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002383 char_u *p;
2384
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002385 p = vim_strchr(*varp, ',');
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002386 if (p == NULL)
2387 return e_comma_required;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002388 else if (p == *varp || p[1] == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002389 return e_invalid_argument;
2390 else if (foldmethodIsMarker(curwin))
2391 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002392
2393 return NULL;
2394}
2395
2396/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002397 * The 'foldmethod' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002398 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002399 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002400did_set_foldmethod(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002401{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002402 char_u **varp = (char_u **)args->os_varp;
2403
Milly142cad12024-10-22 22:11:51 +02002404 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK || **varp == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002405 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002406
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002407 foldUpdateAll(curwin);
2408 if (foldmethodIsDiff(curwin))
2409 newFoldLevel();
2410 return NULL;
2411}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002412
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002413 int
2414expand_set_foldmethod(optexpand_T *args, int *numMatches, char_u ***matches)
2415{
2416 return expand_set_opt_string(
2417 args,
2418 p_fdm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002419 ARRAY_LENGTH(p_fdm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002420 numMatches,
2421 matches);
2422}
2423
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002424/*
2425 * The 'foldopen' option is changed.
2426 */
2427 char *
2428did_set_foldopen(optset_T *args UNUSED)
2429{
2430 return did_set_opt_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
2431}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002432
2433 int
2434expand_set_foldopen(optexpand_T *args, int *numMatches, char_u ***matches)
2435{
2436 return expand_set_opt_string(
2437 args,
2438 p_fdo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002439 ARRAY_LENGTH(p_fdo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002440 numMatches,
2441 matches);
2442}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002443#endif
2444
2445/*
2446 * The 'formatoptions' option is changed.
2447 */
2448 char *
2449did_set_formatoptions(optset_T *args)
2450{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002451 char_u **varp = (char_u **)args->os_varp;
2452
Christian Brabandtb39b2402023-11-29 11:34:05 +01002453 return did_set_option_listflag(*varp, (char_u *)FO_ALL, args->os_errbuf,
2454 args->os_errbuflen);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002455}
2456
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002457 int
2458expand_set_formatoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2459{
2460 return expand_set_opt_listflag(args, (char_u*)FO_ALL, numMatches, matches);
2461}
2462
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002463#if defined(CURSOR_SHAPE) || defined(PROTO)
2464/*
2465 * The 'guicursor' option is changed.
2466 */
2467 char *
2468did_set_guicursor(optset_T *args UNUSED)
2469{
2470 return parse_shape_opt(SHAPE_CURSOR);
2471}
2472#endif
2473
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002474#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002475/*
2476 * The 'guifont' option is changed.
2477 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002478 char *
2479did_set_guifont(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002480{
2481 char_u *p;
2482 char *errmsg = NULL;
2483
2484 if (gui.in_use)
2485 {
2486 p = p_guifont;
2487# if defined(FEAT_GUI_GTK)
2488 // Put up a font dialog and let the user select a new value.
2489 // If this is cancelled go back to the old value but don't
2490 // give an error message.
2491 if (STRCMP(p, "*") == 0)
2492 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002493 p = gui_mch_font_dialog(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002494 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002495 p_guifont = (p != NULL) ? p : vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002496 }
2497# endif
2498 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
2499 {
2500# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
2501 if (STRCMP(p_guifont, "*") == 0)
2502 {
2503 // Dialog was cancelled: Keep the old value without giving
2504 // an error message.
2505 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002506 p_guifont = vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002507 }
2508 else
2509# endif
2510 errmsg = e_invalid_fonts;
2511 }
2512 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002513
2514 return errmsg;
2515}
2516
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002517/*
2518 * Expand the 'guifont' option. Only when GUI is being used. Each platform has
2519 * specific behaviors.
2520 */
2521 int
2522expand_set_guifont(optexpand_T *args, int *numMatches, char_u ***matches)
2523{
2524 if (!gui.in_use)
2525 return FAIL;
2526
2527# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
2528 char_u **varp = (char_u **)args->oe_varp;
2529 int wide = (varp == &p_guifontwide);
2530
2531 return expand_set_opt_callback(
2532 args, gui_mch_expand_font, &wide, numMatches, matches);
2533# else
2534 return FAIL;
2535# endif
2536}
2537
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002538# if defined(FEAT_XFONTSET) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002539/*
2540 * The 'guifontset' option is changed.
2541 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002542 char *
2543did_set_guifontset(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002544{
2545 char *errmsg = NULL;
2546
2547 if (STRCMP(p_guifontset, "*") == 0)
2548 errmsg = e_cant_select_fontset;
2549 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
2550 errmsg = e_invalid_fontset;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002551
2552 return errmsg;
2553}
2554# endif
2555
2556/*
2557 * The 'guifontwide' option is changed.
2558 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002559 char *
2560did_set_guifontwide(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002561{
2562 char *errmsg = NULL;
2563
2564 if (STRCMP(p_guifontwide, "*") == 0)
2565 errmsg = e_cant_select_wide_font;
2566 else if (gui_get_wide_font() == FAIL)
2567 errmsg = e_invalid_wide_font;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002568
2569 return errmsg;
2570}
2571#endif
2572
Erik S. V. Jansson8b1e7492024-02-24 14:26:52 +01002573#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002574/*
2575 * The 'guiligatures' option is changed.
2576 */
2577 char *
2578did_set_guiligatures(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002579{
2580 gui_set_ligatures();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002581 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002582}
2583#endif
2584
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002585#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002586/*
2587 * The 'guioptions' option is changed.
2588 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002589 char *
2590did_set_guioptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002591{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002592 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002593 char *errmsg;
2594
Christian Brabandtb39b2402023-11-29 11:34:05 +01002595 errmsg = did_set_option_listflag(*varp, (char_u *)GO_ALL, args->os_errbuf,
2596 args->os_errbuflen);
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002597 if (errmsg != NULL)
2598 return errmsg;
2599
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002600 gui_init_which_components(args->os_oldval.string);
2601 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002602}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002603
2604 int
2605expand_set_guioptions(optexpand_T *args, int *numMatches, char_u ***matches)
2606{
2607 return expand_set_opt_listflag(args, (char_u*)GO_ALL, numMatches, matches);
2608}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002609#endif
2610
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002611#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002612/*
2613 * The 'guitablabel' option is changed.
2614 */
2615 char *
2616did_set_guitablabel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002617{
2618 redraw_tabline = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002619 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002620}
2621#endif
2622
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002623/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002624 * The 'helpfile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002625 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002626 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002627did_set_helpfile(optset_T *args UNUSED)
2628{
2629 // May compute new values for $VIM and $VIMRUNTIME
2630 if (didset_vim)
2631 vim_unsetenv_ext((char_u *)"VIM");
2632 if (didset_vimruntime)
2633 vim_unsetenv_ext((char_u *)"VIMRUNTIME");
2634 return NULL;
2635}
2636
2637#if defined(FEAT_MULTI_LANG) || defined(PROTO)
2638/*
2639 * The 'helplang' option is changed.
2640 */
2641 char *
2642did_set_helplang(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002643{
2644 char *errmsg = NULL;
2645
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002646 // Check for "", "ab", "ab,cd", etc.
2647 for (char_u *s = p_hlg; *s != NUL; s += 3)
2648 {
2649 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
2650 {
2651 errmsg = e_invalid_argument;
2652 break;
2653 }
2654 if (s[2] == NUL)
2655 break;
2656 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002657
2658 return errmsg;
2659}
2660#endif
2661
2662/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002663 * The 'highlight' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002664 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002665 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002666did_set_highlight(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002667{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002668 if (highlight_changed() == FAIL)
2669 return e_invalid_argument; // invalid flags
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002670
2671 return NULL;
2672}
2673
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002674/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002675 * Expand 'highlight' option.
2676 */
2677 int
2678expand_set_highlight(optexpand_T *args, int *numMatches, char_u ***matches)
2679{
2680 char_u *p;
2681 expand_T *xp = args->oe_xp;
2682 static char_u hl_flags[HLF_COUNT] = HL_FLAGS;
Christian Brabandt3f168ec2023-10-02 23:21:11 +02002683 size_t i;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002684 int count = 0;
2685
2686 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2687 {
2688 // Right after a ':', meaning we just return all highlight names.
2689 return expand_set_opt_generic(
2690 args,
2691 get_highlight_name,
2692 numMatches,
2693 matches);
2694 }
2695
2696 if (*xp->xp_pattern == NUL)
2697 {
2698 // At beginning of a comma-separated list. Return the specific list of
2699 // supported occasions.
2700 *matches = ALLOC_MULT(char_u *, HLF_COUNT + 1);
2701 if (*matches == NULL)
2702 return FAIL;
2703
2704 // We still want to return the full option if it's requested.
2705 if (args->oe_include_orig_val)
2706 {
2707 p = vim_strsave(args->oe_opt_value);
2708 if (p == NULL)
2709 {
2710 VIM_CLEAR(*matches);
2711 return FAIL;
2712 }
2713 (*matches)[count++] = p;
2714 }
2715
2716 for (i = 0; i < HLF_COUNT; i++)
2717 {
2718 p = vim_strnsave(&hl_flags[i], 1);
2719 if (p == NULL)
2720 {
2721 if (count == 0)
2722 {
2723 VIM_CLEAR(*matches);
2724 return FAIL;
2725 }
2726 else
2727 break;
2728 }
2729 (*matches)[count++] = p;
2730 }
2731
2732 if (count == 0)
2733 {
2734 VIM_CLEAR(*matches);
2735 return FAIL;
2736 }
2737 *numMatches = count;
2738 return OK;
2739 }
2740
2741 // We are after the initial character (which indicates the occasion). We
2742 // already made sure we are not matching after a ':' above, so now we want
2743 // to match against display mode modifiers.
2744 // Since the xp_pattern starts from the beginning, we need to include it in
2745 // the returned match.
2746
2747 // Note: Keep this in sync with highlight_changed()
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002748 static char_u p_hl_mode_values[] =
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002749 {':', 'b', 'i', '-', 'n', 'r', 's', 'u', 'c', '2', 'd', '=', 't'};
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002750 size_t num_hl_modes = ARRAY_LENGTH(p_hl_mode_values);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002751
2752 *matches = ALLOC_MULT(char_u *, num_hl_modes);
2753 if (*matches == NULL)
2754 return FAIL;
2755
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002756 int pattern_len = xp->xp_pattern_len;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002757
2758 for (i = 0; i < num_hl_modes; i++)
2759 {
2760 // Don't allow duplicates as these are unique flags
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002761 char_u *dup = vim_strchr(xp->xp_pattern + 1, p_hl_mode_values[i]);
2762 if (dup != NULL && (int)(dup - xp->xp_pattern) < pattern_len)
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002763 continue;
2764
2765 // ':' only works by itself, not with other flags.
2766 if (pattern_len > 1 && p_hl_mode_values[i] == ':')
2767 continue;
2768
2769 p = vim_strnsave(xp->xp_pattern, pattern_len + 1);
2770 if (p == NULL)
2771 {
2772 if (i == 0)
2773 {
2774 VIM_CLEAR(*matches);
2775 return FAIL;
2776 }
2777 else
2778 break;
2779 }
2780 p[pattern_len] = p_hl_mode_values[i];
2781 p[pattern_len + 1] = NUL;
2782 (*matches)[count++] = p;
2783 }
2784 if (count == 0)
2785 {
2786 VIM_CLEAR(*matches);
2787 return FAIL;
2788 }
2789 *numMatches = count;
2790
2791 return OK;
2792}
2793
2794/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002795 * The 'titlestring' or the 'iconstring' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002796 */
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002797 static char *
2798parse_titleiconstring(optset_T *args UNUSED, int flagval UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002799{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002800#ifdef FEAT_STL_OPT
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002801 char_u **varp = (char_u **)args->os_varp;
2802
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002803 // NULL => statusline syntax
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002804 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002805 stl_syntax |= flagval;
2806 else
2807 stl_syntax &= ~flagval;
2808#endif
2809 did_set_title();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002810
2811 return NULL;
2812}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002813
2814/*
2815 * The 'iconstring' option is changed.
2816 */
2817 char *
2818did_set_iconstring(optset_T *args)
2819{
2820 int flagval = 0;
2821
2822#ifdef FEAT_STL_OPT
2823 flagval = STL_IN_ICON;
2824#endif
2825
2826 return parse_titleiconstring(args, flagval);
2827}
2828
2829#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
2830/*
2831 * The 'imactivatekey' option is changed.
2832 */
2833 char *
2834did_set_imactivatekey(optset_T *args UNUSED)
2835{
2836 if (!im_xim_isvalid_imactivate())
2837 return e_invalid_argument;
2838 return NULL;
2839}
2840#endif
2841
2842/*
Milly5e7a6a42024-10-22 22:27:19 +02002843 * The 'iskeyword' option is changed.
2844 */
2845 char *
2846did_set_iskeyword(optset_T *args)
2847{
2848 char_u **varp = (char_u **)args->os_varp;
2849
2850 if (varp == &p_isk) // only check for global-value
2851 {
2852 if (check_isopt(*varp) == FAIL)
2853 return e_invalid_argument;
2854 }
2855 else // fallthrough for local-value
2856 return did_set_isopt(args);
2857
2858 return NULL;
2859}
2860
2861/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002862 * The 'isident' or the 'iskeyword' or the 'isprint' or the 'isfname' option is
2863 * changed.
2864 */
2865 char *
2866did_set_isopt(optset_T *args)
2867{
Milly5e7a6a42024-10-22 22:27:19 +02002868 // 'isident', 'iskeyword', 'isprint' or 'isfname' option: refill g_chartab[]
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002869 // If the new option is invalid, use old value.
2870 // 'lisp' option: refill g_chartab[] for '-' char.
2871 if (init_chartab() == FAIL)
2872 {
2873 args->os_restore_chartab = TRUE;// need to restore the chartab.
2874 return e_invalid_argument; // error in value
2875 }
2876
2877 return NULL;
2878}
2879
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002880/*
2881 * The 'jumpoptions' option is changed.
2882 */
2883 char *
Yegappan Lakshmanan1926ae42023-09-21 16:36:28 +02002884did_set_jumpoptions(optset_T *args UNUSED)
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002885{
2886 if (opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE) != OK)
2887 return e_invalid_argument;
2888
2889 return NULL;
2890}
2891
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002892 int
2893expand_set_jumpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2894{
2895 return expand_set_opt_string(
2896 args,
2897 p_jop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002898 ARRAY_LENGTH(p_jop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002899 numMatches,
2900 matches);
2901}
2902
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002903#if defined(FEAT_KEYMAP) || defined(PROTO)
2904/*
2905 * The 'keymap' option is changed.
2906 */
2907 char *
2908did_set_keymap(optset_T *args)
2909{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002910 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002911 char *errmsg = NULL;
2912
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002913 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002914 errmsg = e_invalid_argument;
2915 else
2916 {
2917 int secure_save = secure;
2918
2919 // Reset the secure flag, since the value of 'keymap' has
2920 // been checked to be safe.
2921 secure = 0;
2922
2923 // load or unload key mapping tables
2924 errmsg = keymap_init();
2925
2926 secure = secure_save;
2927
2928 // Since we check the value, there is no need to set P_INSECURE,
2929 // even when the value comes from a modeline.
2930 args->os_value_checked = TRUE;
2931 }
2932
2933 if (errmsg == NULL)
2934 {
2935 if (*curbuf->b_p_keymap != NUL)
2936 {
2937 // Installed a new keymap, switch on using it.
2938 curbuf->b_p_iminsert = B_IMODE_LMAP;
2939 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
2940 curbuf->b_p_imsearch = B_IMODE_LMAP;
2941 }
2942 else
2943 {
2944 // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
2945 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
2946 curbuf->b_p_iminsert = B_IMODE_NONE;
2947 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
2948 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
2949 }
2950 if ((args->os_flags & OPT_LOCAL) == 0)
2951 {
2952 set_iminsert_global();
2953 set_imsearch_global();
2954 }
2955 status_redraw_curbuf();
2956 }
2957
2958 return errmsg;
2959}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002960#endif
2961
2962/*
2963 * The 'keymodel' option is changed.
2964 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002965 char *
2966did_set_keymodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002967{
2968 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
2969 return e_invalid_argument;
2970
2971 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
2972 km_startsel = (vim_strchr(p_km, 'a') != NULL);
2973 return NULL;
2974}
2975
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002976 int
2977expand_set_keymodel(optexpand_T *args, int *numMatches, char_u ***matches)
2978{
2979 return expand_set_opt_string(
2980 args,
2981 p_km_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002982 ARRAY_LENGTH(p_km_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002983 numMatches,
2984 matches);
2985}
2986
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002987/*
2988 * The 'keyprotocol' option is changed.
2989 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002990 char *
2991did_set_keyprotocol(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002992{
Gregory Anders3695d0e2023-09-29 20:17:20 +02002993 char_u *term = T_NAME;
2994 keyprot_T kpc = match_keyprotocol(term);
2995 if (kpc == KEYPROTOCOL_FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002996 return e_invalid_argument;
2997
Gregory Anders3695d0e2023-09-29 20:17:20 +02002998 apply_keyprotocol(term, kpc);
2999
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003000 return NULL;
3001}
3002
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003003 int
3004expand_set_keyprotocol(optexpand_T *args, int *numMatches, char_u ***matches)
3005{
3006 expand_T *xp = args->oe_xp;
3007 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3008 {
3009 // 'keyprotocol' only has well-defined terms for completion for the
3010 // protocol part after the colon.
3011 return expand_set_opt_string(
3012 args,
3013 p_kpc_protocol_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003014 ARRAY_LENGTH(p_kpc_protocol_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003015 numMatches,
3016 matches);
3017 }
3018 // Use expand_set_opt_string instead of returning FAIL so that we can
3019 // include the original value if args->oe_include_orig_val is set.
3020 static char *(empty[]) = {NULL};
3021 return expand_set_opt_string(args, empty, 0, numMatches, matches);
3022}
3023
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003024/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003025 * The 'lispoptions' option is changed.
3026 */
3027 char *
3028did_set_lispoptions(optset_T *args)
3029{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003030 char_u **varp = (char_u **)args->os_varp;
3031
3032 if (**varp != NUL
3033 && STRCMP(*varp, "expr:0") != 0
3034 && STRCMP(*varp, "expr:1") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003035 return e_invalid_argument;
3036
3037 return NULL;
3038}
3039
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003040 int
3041expand_set_lispoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3042{
3043 static char *(p_lop_values[]) = {"expr:0", "expr:1", NULL};
3044 return expand_set_opt_string(
3045 args,
3046 p_lop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003047 ARRAY_LENGTH(p_lop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003048 numMatches,
3049 matches);
3050}
3051
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003052/*
3053 * The 'matchpairs' option is changed.
3054 */
3055 char *
3056did_set_matchpairs(optset_T *args)
3057{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003058 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003059 char_u *p;
3060
3061 if (has_mbyte)
3062 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003063 for (p = *varp; *p != NUL; ++p)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003064 {
3065 int x2 = -1;
3066 int x3 = -1;
3067
3068 p += mb_ptr2len(p);
3069 if (*p != NUL)
3070 x2 = *p++;
3071 if (*p != NUL)
3072 {
3073 x3 = mb_ptr2char(p);
3074 p += mb_ptr2len(p);
3075 }
3076 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
3077 return e_invalid_argument;
3078 if (*p == NUL)
3079 break;
3080 }
3081 }
3082 else
3083 {
3084 // Check for "x:y,x:y"
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003085 for (p = *varp; *p != NUL; p += 4)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003086 {
3087 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
3088 return e_invalid_argument;
3089 if (p[3] == NUL)
3090 break;
3091 }
3092 }
3093
3094 return NULL;
3095}
3096
Christian Brabandt51d4d842024-12-06 17:26:25 +01003097/*
3098 * Process the updated 'messagesopt' option value.
3099 */
3100 char *
3101did_set_messagesopt(optset_T *args UNUSED)
3102{
3103 if (messagesopt_changed() == FAIL)
3104 return e_invalid_argument;
3105
3106 return NULL;
3107}
3108
3109 int
3110expand_set_messagesopt(optexpand_T *args, int *numMatches, char_u ***matches)
3111{
zeertzjq8cc43da2024-12-07 16:09:08 +01003112 static char *(p_mopt_values[]) = {"hit-enter", "wait:", "history:", NULL};
Christian Brabandt51d4d842024-12-06 17:26:25 +01003113 return expand_set_opt_string(
3114 args,
zeertzjq8cc43da2024-12-07 16:09:08 +01003115 p_mopt_values,
3116 ARRAY_LENGTH(p_mopt_values) - 1,
Christian Brabandt51d4d842024-12-06 17:26:25 +01003117 numMatches,
3118 matches);
3119}
3120
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003121#if defined(FEAT_SPELL) || defined(PROTO)
3122/*
3123 * The 'mkspellmem' option is changed.
3124 */
3125 char *
3126did_set_mkspellmem(optset_T *args UNUSED)
3127{
3128 if (spell_check_msm() != OK)
3129 return e_invalid_argument;
3130
3131 return NULL;
3132}
3133#endif
3134
3135/*
3136 * The 'mouse' option is changed.
3137 */
3138 char *
3139did_set_mouse(optset_T *args)
3140{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003141 char_u **varp = (char_u **)args->os_varp;
3142
Christian Brabandtb39b2402023-11-29 11:34:05 +01003143 return did_set_option_listflag(*varp, (char_u *)MOUSE_ALL, args->os_errbuf,
3144 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003145}
3146
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003147 int
3148expand_set_mouse(optexpand_T *args, int *numMatches, char_u ***matches)
3149{
3150 return expand_set_opt_listflag(args, (char_u*)MOUSE_ALL, numMatches, matches);
3151}
3152
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003153/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003154 * The 'mousemodel' option is changed.
3155 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003156 char *
3157did_set_mousemodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003158{
3159 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
3160 return e_invalid_argument;
3161#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
Ken Takata18d0d292023-12-19 20:12:29 +01003162 else if (*p_mousem != *args->os_oldval.string)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003163 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
3164 // to create or delete the popup menus.
3165 gui_motif_update_mousemodel(root_menu);
3166#endif
3167
3168 return NULL;
3169}
3170
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003171 int
3172expand_set_mousemodel(optexpand_T *args, int *numMatches, char_u ***matches)
3173{
3174 return expand_set_opt_string(
3175 args,
3176 p_mousem_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003177 ARRAY_LENGTH(p_mousem_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003178 numMatches,
3179 matches);
3180}
3181
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003182#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3183 char *
3184did_set_mouseshape(optset_T *args UNUSED)
3185{
3186 char *errmsg = NULL;
3187
3188 errmsg = parse_shape_opt(SHAPE_MOUSE);
3189 update_mouseshape(-1);
3190
3191 return errmsg;
3192}
3193#endif
3194
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003195/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003196 * The 'nrformats' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003197 */
3198 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003199did_set_nrformats(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003200{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003201 char_u **varp = (char_u **)args->os_varp;
3202
3203 return did_set_opt_strings(*varp, p_nf_values, TRUE);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003204}
3205
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003206 int
3207expand_set_nrformats(optexpand_T *args, int *numMatches, char_u ***matches)
3208{
3209 return expand_set_opt_string(
3210 args,
3211 p_nf_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003212 ARRAY_LENGTH(p_nf_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003213 numMatches,
3214 matches);
3215}
3216
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003217#if defined(FEAT_EVAL) || defined(PROTO)
3218/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003219 * One of the '*expr' options is changed: 'balloonexpr', 'diffexpr',
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003220 * 'foldexpr', 'foldtext', 'formatexpr', 'includeexpr', 'indentexpr',
3221 * 'patchexpr', 'printexpr' and 'charconvert'.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003222 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003223 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003224did_set_optexpr(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003225{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003226 char_u **varp = (char_u **)args->os_varp;
3227
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003228 // If the option value starts with <SID> or s:, then replace that with
3229 // the script identifier.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003230 char_u *name = get_scriptlocal_funcname(*varp);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003231 if (name != NULL)
3232 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003233 free_string_option(*varp);
3234 *varp = name;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003235 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003236
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003237 return NULL;
3238}
3239#endif
3240
3241/*
3242 * The 'pastetoggle' option is changed.
3243 */
3244 char *
3245did_set_pastetoggle(optset_T *args UNUSED)
3246{
3247 char_u *p;
3248
3249 // translate key codes like in a mapping
3250 if (*p_pt)
3251 {
zeertzjq7e0bae02023-08-11 23:15:38 +02003252 (void)replace_termcodes(p_pt, &p, 0,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003253 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
3254 if (p != NULL)
3255 {
3256 free_string_option(p_pt);
3257 p_pt = p;
3258 }
3259 }
3260
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003261 return NULL;
3262}
3263
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003264#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3265/*
3266 * The 'previewpopup' option is changed.
3267 */
3268 char *
3269did_set_previewpopup(optset_T *args UNUSED)
3270{
3271 if (parse_previewpopup(NULL) == FAIL)
3272 return e_invalid_argument;
3273
3274 return NULL;
3275}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003276
3277 int
3278expand_set_popupoption(optexpand_T *args, int *numMatches, char_u ***matches)
3279{
3280 expand_T *xp = args->oe_xp;
3281
3282 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3283 {
3284 // Within "highlight:"/"border:"/"align:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003285 int border_len = (int)STRLEN("border:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003286 if (xp->xp_pattern - args->oe_set_arg >= border_len &&
3287 STRNCMP(xp->xp_pattern - border_len, "border:", border_len) == 0)
3288 {
3289 return expand_set_opt_string(
3290 args,
3291 p_popup_option_border_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003292 ARRAY_LENGTH(p_popup_option_border_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003293 numMatches,
3294 matches);
3295 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003296 int align_len = (int)STRLEN("align:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003297 if (xp->xp_pattern - args->oe_set_arg >= align_len &&
3298 STRNCMP(xp->xp_pattern - align_len, "align:", align_len) == 0)
3299 {
3300 return expand_set_opt_string(
3301 args,
3302 p_popup_option_align_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003303 ARRAY_LENGTH(p_popup_option_align_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003304 numMatches,
3305 matches);
3306 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003307 int highlight_len = (int)STRLEN("highlight:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003308 if (xp->xp_pattern - args->oe_set_arg >= highlight_len &&
3309 STRNCMP(xp->xp_pattern - highlight_len, "highlight:", highlight_len) == 0)
3310 {
3311 // Return the list of all highlight names
3312 return expand_set_opt_generic(
3313 args,
3314 get_highlight_name,
3315 numMatches,
3316 matches);
3317 }
3318 return FAIL;
3319 }
3320
3321 return expand_set_opt_string(
3322 args,
3323 p_popup_option_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003324 ARRAY_LENGTH(p_popup_option_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003325 numMatches,
3326 matches);
3327}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003328#endif
3329
3330#if defined(FEAT_POSTSCRIPT) || defined(PROTO)
3331/*
3332 * The 'printencoding' option is changed.
3333 */
3334 char *
3335did_set_printencoding(optset_T *args UNUSED)
3336{
3337 char_u *s, *p;
3338
3339 // Canonize 'printencoding' if VIM standard one
3340 p = enc_canonize(p_penc);
3341 if (p != NULL)
3342 {
3343 vim_free(p_penc);
3344 p_penc = p;
3345 }
3346 else
3347 {
3348 // Ensure lower case and '-' for '_'
3349 for (s = p_penc; *s != NUL; s++)
3350 {
3351 if (*s == '_')
3352 *s = '-';
3353 else
3354 *s = TOLOWER_ASC(*s);
3355 }
3356 }
3357
3358 return NULL;
3359}
3360#endif
3361
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003362#if defined(FEAT_PRINTER) || defined(PROTO)
3363
3364 static char_u *
3365get_printoptions_names(expand_T *xp UNUSED, int idx)
3366{
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003367 if (idx >= (int)ARRAY_LENGTH(printer_opts))
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003368 return NULL;
3369 return (char_u*)printer_opts[idx].name;
3370}
3371
3372/*
3373 * Expand 'printoptions' option
3374 */
3375 int
3376expand_set_printoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3377{
3378 return expand_set_opt_generic(
3379 args,
3380 get_printoptions_names,
3381 numMatches,
3382 matches);
3383}
3384#endif
3385
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003386#if defined(FEAT_STL_OPT) || defined(PROTO)
3387/*
3388 * The 'statusline' or the 'tabline' or the 'rulerformat' option is changed.
3389 * "rulerformat" is TRUE if the 'rulerformat' option is changed.
3390 */
3391 static char *
3392parse_statustabline_rulerformat(optset_T *args, int rulerformat)
3393{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003394 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003395 char_u *s;
3396 char *errmsg = NULL;
3397 int wid;
3398
3399 if (rulerformat) // reset ru_wid first
3400 ru_wid = 0;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003401 s = *varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003402 if (rulerformat && *s == '%')
3403 {
3404 // set ru_wid if 'ruf' starts with "%99("
3405 if (*++s == '-') // ignore a '-'
3406 s++;
3407 wid = getdigits(&s);
3408 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
3409 ru_wid = wid;
3410 else
Yegappan Lakshmananac023e82024-11-27 20:55:45 +01003411 {
3412 // Validate the flags in 'rulerformat' only if it doesn't point to
3413 // a custom function ("%!" flag).
3414 if ((*varp)[1] != '!')
3415 errmsg = check_stl_option(p_ruf);
3416 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003417 }
3418 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
3419 else if (rulerformat || s[0] != '%' || s[1] != '!')
3420 errmsg = check_stl_option(s);
3421 if (rulerformat && errmsg == NULL)
3422 comp_col();
3423
3424 return errmsg;
3425}
3426#endif
3427
3428#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
3429/*
3430 * The 'renderoptions' option is changed.
3431 */
3432 char *
3433did_set_renderoptions(optset_T *args UNUSED)
3434{
3435 if (!gui_mch_set_rendering_options(p_rop))
3436 return e_invalid_argument;
3437
3438 return NULL;
3439}
3440#endif
3441
3442#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3443/*
3444 * The 'rightleftcmd' option is changed.
3445 */
3446 char *
3447did_set_rightleftcmd(optset_T *args)
3448{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003449 char_u **varp = (char_u **)args->os_varp;
3450
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003451 // Currently only "search" is a supported value.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003452 if (**varp != NUL && STRCMP(*varp, "search") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003453 return e_invalid_argument;
3454
3455 return NULL;
3456}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003457
3458 int
3459expand_set_rightleftcmd(optexpand_T *args, int *numMatches, char_u ***matches)
3460{
3461 static char *(p_rlc_values[]) = {"search", NULL};
3462 return expand_set_opt_string(
3463 args,
3464 p_rlc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003465 ARRAY_LENGTH(p_rlc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003466 numMatches,
3467 matches);
3468}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003469#endif
3470
3471#if defined(FEAT_STL_OPT) || defined(PROTO)
3472/*
3473 * The 'rulerformat' option is changed.
3474 */
3475 char *
3476did_set_rulerformat(optset_T *args)
3477{
3478 return parse_statustabline_rulerformat(args, TRUE);
3479}
3480#endif
3481
3482/*
3483 * The 'scrollopt' option is changed.
3484 */
3485 char *
3486did_set_scrollopt(optset_T *args UNUSED)
3487{
3488 return did_set_opt_strings(p_sbo, p_scbopt_values, TRUE);
3489}
3490
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003491 int
3492expand_set_scrollopt(optexpand_T *args, int *numMatches, char_u ***matches)
3493{
3494 return expand_set_opt_string(
3495 args,
3496 p_scbopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003497 ARRAY_LENGTH(p_scbopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003498 numMatches,
3499 matches);
3500}
3501
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003502/*
3503 * The 'selection' option is changed.
3504 */
3505 char *
3506did_set_selection(optset_T *args UNUSED)
3507{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003508 if (*p_sel == NUL || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003509 return e_invalid_argument;
3510
3511 return NULL;
3512}
3513
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003514 int
3515expand_set_selection(optexpand_T *args, int *numMatches, char_u ***matches)
3516{
3517 return expand_set_opt_string(
3518 args,
3519 p_sel_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003520 ARRAY_LENGTH(p_sel_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003521 numMatches,
3522 matches);
3523}
3524
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003525/*
3526 * The 'selectmode' option is changed.
3527 */
3528 char *
3529did_set_selectmode(optset_T *args UNUSED)
3530{
3531 return did_set_opt_strings(p_slm, p_slm_values, TRUE);
3532}
3533
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003534 int
3535expand_set_selectmode(optexpand_T *args, int *numMatches, char_u ***matches)
3536{
3537 return expand_set_opt_string(
3538 args,
3539 p_slm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003540 ARRAY_LENGTH(p_slm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003541 numMatches,
3542 matches);
3543}
3544
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003545#if defined(FEAT_SESSION) || defined(PROTO)
3546/*
3547 * The 'sessionoptions' option is changed.
3548 */
3549 char *
3550did_set_sessionoptions(optset_T *args)
3551{
3552 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
3553 return e_invalid_argument;
3554 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
3555 {
3556 // Don't allow both "sesdir" and "curdir".
3557 (void)opt_strings_flags(args->os_oldval.string, p_ssop_values,
3558 &ssop_flags, TRUE);
3559 return e_invalid_argument;
3560 }
3561
3562 return NULL;
3563}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003564
3565 int
3566expand_set_sessionoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3567{
3568 return expand_set_opt_string(
3569 args,
3570 p_ssop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003571 ARRAY_LENGTH(p_ssop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003572 numMatches,
3573 matches);
3574}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003575#endif
3576
3577/*
3578 * The 'shortmess' option is changed.
3579 */
3580 char *
3581did_set_shortmess(optset_T *args)
3582{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003583 char_u **varp = (char_u **)args->os_varp;
3584
Christian Brabandtb39b2402023-11-29 11:34:05 +01003585 return did_set_option_listflag(*varp, (char_u *)SHM_ALL, args->os_errbuf,
3586 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003587}
3588
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003589 int
3590expand_set_shortmess(optexpand_T *args, int *numMatches, char_u ***matches)
3591{
3592 return expand_set_opt_listflag(args, (char_u*)SHM_ALL, numMatches, matches);
3593}
3594
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003595#if defined(FEAT_LINEBREAK) || defined(PROTO)
3596/*
3597 * The 'showbreak' option is changed.
3598 */
3599 char *
3600did_set_showbreak(optset_T *args)
3601{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003602 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003603 char_u *s;
3604
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003605 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003606 {
3607 if (ptr2cells(s) != 1)
3608 return e_showbreak_contains_unprintable_or_wide_character;
3609 MB_PTR_ADV(s);
3610 }
3611
3612 return NULL;
3613}
3614#endif
3615
3616/*
3617 * The 'showcmdloc' option is changed.
3618 */
3619 char *
3620did_set_showcmdloc(optset_T *args UNUSED)
3621{
zeertzjqc27fcf42024-03-01 23:01:43 +01003622 char *errmsg = did_set_opt_strings(p_sloc, p_sloc_values, FALSE);
3623
3624 if (errmsg == NULL)
3625 comp_col();
3626
3627 return errmsg;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003628}
3629
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003630 int
3631expand_set_showcmdloc(optexpand_T *args, int *numMatches, char_u ***matches)
3632{
3633 return expand_set_opt_string(
3634 args,
3635 p_sloc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003636 ARRAY_LENGTH(p_sloc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003637 numMatches,
3638 matches);
3639}
3640
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003641#if defined(FEAT_SIGNS) || defined(PROTO)
3642/*
3643 * The 'signcolumn' option is changed.
3644 */
3645 char *
3646did_set_signcolumn(optset_T *args)
3647{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003648 char_u **varp = (char_u **)args->os_varp;
3649
3650 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003651 return e_invalid_argument;
3652 // When changing the 'signcolumn' to or from 'number', recompute the
3653 // width of the number column if 'number' or 'relativenumber' is set.
3654 if (((*args->os_oldval.string == 'n' && args->os_oldval.string[1] == 'u')
3655 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
3656 && (curwin->w_p_nu || curwin->w_p_rnu))
3657 curwin->w_nrwidth_line_count = 0;
3658
3659 return NULL;
3660}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003661
3662 int
3663expand_set_signcolumn(optexpand_T *args, int *numMatches, char_u ***matches)
3664{
3665 return expand_set_opt_string(
3666 args,
3667 p_scl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003668 ARRAY_LENGTH(p_scl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003669 numMatches,
3670 matches);
3671}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003672#endif
3673
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003674#if defined(FEAT_SPELL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003675/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003676 * The 'spellcapcheck' option is changed.
3677 */
3678 char *
3679did_set_spellcapcheck(optset_T *args UNUSED)
3680{
3681 // compile the regexp program.
3682 return compile_cap_prog(curwin->w_s);
3683}
3684
3685/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003686 * The 'spellfile' option is changed.
3687 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003688 char *
3689did_set_spellfile(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003690{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003691 char_u **varp = (char_u **)args->os_varp;
3692
3693 if (!valid_spellfile(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003694 return e_invalid_argument;
3695
3696 // If there is a window for this buffer in which 'spell' is set load the
3697 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003698 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003699}
3700
3701/*
3702 * The 'spell' option is changed.
3703 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003704 char *
3705did_set_spelllang(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003706{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003707 char_u **varp = (char_u **)args->os_varp;
3708
3709 if (!valid_spelllang(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003710 return e_invalid_argument;
3711
3712 // If there is a window for this buffer in which 'spell' is set load the
3713 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003714 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003715}
3716
3717/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003718 * The 'spelloptions' option is changed.
3719 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003720 char *
3721did_set_spelloptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003722{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003723 char_u **varp = (char_u **)args->os_varp;
3724
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003725 if (**varp != NUL && STRCMP(*varp, "camel") != 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003726 return e_invalid_argument;
3727
3728 return NULL;
3729}
3730
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003731 int
3732expand_set_spelloptions(optexpand_T *args, int *numMatches, char_u ***matches)
3733{
3734 static char *(p_spo_values[]) = {"camel", NULL};
3735 return expand_set_opt_string(
3736 args,
3737 p_spo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003738 ARRAY_LENGTH(p_spo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003739 numMatches,
3740 matches);
3741}
3742
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003743/*
3744 * The 'spellsuggest' option is changed.
3745 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003746 char *
3747did_set_spellsuggest(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003748{
3749 if (spell_check_sps() != OK)
3750 return e_invalid_argument;
3751
3752 return NULL;
3753}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003754
3755 int
3756expand_set_spellsuggest(optexpand_T *args, int *numMatches, char_u ***matches)
3757{
3758 return expand_set_opt_string(
3759 args,
3760 p_sps_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003761 ARRAY_LENGTH(p_sps_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003762 numMatches,
3763 matches);
3764}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003765#endif
3766
3767/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003768 * The 'splitkeep' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003769 */
3770 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003771did_set_splitkeep(optset_T *args UNUSED)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003772{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003773 return did_set_opt_strings(p_spk, p_spk_values, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003774}
3775
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003776 int
3777expand_set_splitkeep(optexpand_T *args, int *numMatches, char_u ***matches)
3778{
3779 return expand_set_opt_string(
3780 args,
3781 p_spk_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003782 ARRAY_LENGTH(p_spk_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003783 numMatches,
3784 matches);
3785}
3786
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00003787#if defined(FEAT_STL_OPT) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003788/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003789 * The 'statusline' option is changed.
3790 */
3791 char *
3792did_set_statusline(optset_T *args)
3793{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003794 return parse_statustabline_rulerformat(args, FALSE);
3795}
3796#endif
3797
3798/*
3799 * The 'swapsync' option is changed.
3800 */
3801 char *
3802did_set_swapsync(optset_T *args UNUSED)
3803{
3804 return did_set_opt_strings(p_sws, p_sws_values, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003805}
3806
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003807 int
3808expand_set_swapsync(optexpand_T *args, int *numMatches, char_u ***matches)
3809{
3810 return expand_set_opt_string(
3811 args,
3812 p_sws_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003813 ARRAY_LENGTH(p_sws_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003814 numMatches,
3815 matches);
3816}
3817
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003818/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003819 * The 'switchbuf' option is changed.
3820 */
3821 char *
3822did_set_switchbuf(optset_T *args UNUSED)
3823{
3824 return did_set_opt_flags(p_swb, p_swb_values, &swb_flags, TRUE);
3825}
3826
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003827 int
3828expand_set_switchbuf(optexpand_T *args, int *numMatches, char_u ***matches)
3829{
3830 return expand_set_opt_string(
3831 args,
3832 p_swb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003833 ARRAY_LENGTH(p_swb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003834 numMatches,
3835 matches);
3836}
3837
LemonBoy5247b0b2024-07-12 19:30:58 +02003838/*
3839 * The 'tabclose' option is changed.
3840 */
3841 char *
3842did_set_tabclose(optset_T *args UNUSED)
3843{
3844 return did_set_opt_flags(p_tcl, p_tcl_values, &tcl_flags, TRUE);
3845}
3846
3847 int
3848expand_set_tabclose(optexpand_T *args, int *numMatches, char_u ***matches)
3849{
3850 return expand_set_opt_string(
3851 args,
3852 p_tcl_values,
3853 ARRAY_LENGTH(p_tcl_values) - 1,
3854 numMatches,
3855 matches);
3856}
3857
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003858#if defined(FEAT_STL_OPT) || defined(PROTO)
3859/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003860 * The 'tabline' option is changed.
3861 */
3862 char *
3863did_set_tabline(optset_T *args)
3864{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003865 return parse_statustabline_rulerformat(args, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003866}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003867#endif
3868
3869/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003870 * The 'tagcase' option is changed.
3871 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003872 char *
3873did_set_tagcase(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003874{
3875 unsigned int *flags;
3876 char_u *p;
3877
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003878 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003879 {
3880 p = curbuf->b_p_tc;
3881 flags = &curbuf->b_tc_flags;
3882 }
3883 else
3884 {
3885 p = p_tc;
3886 flags = &tc_flags;
3887 }
3888
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003889 if ((args->os_flags & OPT_LOCAL) && *p == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003890 // make the local value empty: use the global value
3891 *flags = 0;
3892 else if (*p == NUL
3893 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
3894 return e_invalid_argument;
3895
3896 return NULL;
3897}
3898
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003899 int
3900expand_set_tagcase(optexpand_T *args, int *numMatches, char_u ***matches)
3901{
3902 return expand_set_opt_string(
3903 args,
3904 p_tc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003905 ARRAY_LENGTH(p_tc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003906 numMatches,
3907 matches);
3908}
3909
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003910/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003911 * The 'term' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003912 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003913 char *
3914did_set_term(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003915{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003916 if (T_NAME[0] == NUL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003917 return e_cannot_set_term_to_empty_string;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003918#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003919 if (gui.in_use)
3920 return e_cannot_change_term_in_GUI;
3921 if (term_is_gui(T_NAME))
3922 return e_use_gui_to_start_GUI;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003923#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003924 if (set_termname(T_NAME) == FAIL)
3925 return e_not_found_in_termcap;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003926
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003927 // Screen colors may have changed.
3928 redraw_later_clear();
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003929
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003930 return NULL;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003931}
3932
3933/*
3934 * Some terminal option (t_xxx) is changed
3935 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003936 char *
3937did_set_term_option(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003938{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003939 char_u **varp = (char_u **)args->os_varp;
3940
3941 if (!full_screen)
3942 return NULL;
3943
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003944 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
3945 if (varp == &T_CCO)
3946 {
3947 int colors = atoi((char *)T_CCO);
3948
3949 // Only reinitialize colors if t_Co value has really changed to
3950 // avoid expensive reload of colorscheme if t_Co is set to the
3951 // same value multiple times.
3952 if (colors != t_colors)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003953 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003954 t_colors = colors;
3955 if (t_colors <= 1)
3956 {
3957 vim_free(T_CCO);
3958 T_CCO = empty_option;
3959 }
3960#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3961 if (is_term_win32())
3962 {
3963 swap_tcap();
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003964 args->os_did_swaptcap = TRUE;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003965 }
3966#endif
3967 // We now have a different color setup, initialize it again.
3968 init_highlight(TRUE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003969 }
3970 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003971 ttest(FALSE);
3972 if (varp == &T_ME)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003973 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003974 out_str(T_ME);
3975 redraw_later(UPD_CLEAR);
3976#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3977 // Since t_me has been set, this probably means that the user
3978 // wants to use this as default colors. Need to reset default
3979 // background/foreground colors.
3980# ifdef VIMDLL
3981 if (!gui.in_use && !gui.starting)
3982# endif
3983 mch_set_normal_colors();
3984#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003985 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003986 if (varp == &T_BE && termcap_active)
3987 {
3988 MAY_WANT_TO_LOG_THIS;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003989
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003990 if (*T_BE == NUL)
3991 // When clearing t_BE we assume the user no longer wants
3992 // bracketed paste, thus disable it by writing t_BD.
3993 out_str(T_BD);
3994 else
3995 out_str(T_BE);
3996 }
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003997
3998 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003999}
4000
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004001#if defined(FEAT_TERMINAL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004002/*
4003 * The 'termwinkey' option is changed.
4004 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004005 char *
Milly94606f72024-10-22 22:07:52 +02004006did_set_termwinkey(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004007{
Milly94606f72024-10-22 22:07:52 +02004008 char_u **varp = (char_u **)args->os_varp;
4009
4010 if ((*varp)[0] != NUL && string_to_key(*varp, TRUE) == 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004011 return e_invalid_argument;
4012
4013 return NULL;
4014}
4015
4016/*
4017 * The 'termwinsize' option is changed.
4018 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004019 char *
Milly8be10aa2024-10-22 22:01:46 +02004020did_set_termwinsize(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004021{
Milly8be10aa2024-10-22 22:01:46 +02004022 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004023 char_u *p;
4024
Milly8be10aa2024-10-22 22:01:46 +02004025 if ((*varp)[0] == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004026 return NULL;
4027
Milly8be10aa2024-10-22 22:01:46 +02004028 p = skipdigits(*varp);
4029 if (p == *varp || (*p != 'x' && *p != '*') || *skipdigits(p + 1) != NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004030 return e_invalid_argument;
4031
4032 return NULL;
4033}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00004034
4035# if defined(MSWIN) || defined(PROTO)
4036/*
4037 * The 'termwintype' option is changed.
4038 */
4039 char *
4040did_set_termwintype(optset_T *args UNUSED)
4041{
4042 return did_set_opt_strings(p_twt, p_twt_values, FALSE);
4043}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004044
4045 int
4046expand_set_termwintype(optexpand_T *args, int *numMatches, char_u ***matches)
4047{
4048 return expand_set_opt_string(
4049 args,
4050 p_twt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004051 ARRAY_LENGTH(p_twt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004052 numMatches,
4053 matches);
4054}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00004055# endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004056#endif
4057
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004058/*
4059 * The 'titlestring' option is changed.
4060 */
4061 char *
4062did_set_titlestring(optset_T *args)
4063{
4064 int flagval = 0;
4065
4066#ifdef FEAT_STL_OPT
4067 flagval = STL_IN_TITLE;
4068#endif
4069 return parse_titleiconstring(args, flagval);
4070}
4071
4072#if (defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)) || defined(PROTO)
4073/*
4074 * The 'toolbar' option is changed.
4075 */
4076 char *
4077did_set_toolbar(optset_T *args UNUSED)
4078{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004079 if (opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags,
4080 TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004081 return e_invalid_argument;
4082
4083 out_flush();
4084 gui_mch_show_toolbar((toolbar_flags &
4085 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4086 return NULL;
4087}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004088
4089 int
4090expand_set_toolbar(optexpand_T *args, int *numMatches, char_u ***matches)
4091{
4092 return expand_set_opt_string(
4093 args,
4094 p_toolbar_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004095 ARRAY_LENGTH(p_toolbar_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004096 numMatches,
4097 matches);
4098}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004099#endif
4100
4101#if (defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)) || defined(PROTO)
4102/*
4103 * The 'toolbariconsize' option is changed. GTK+ 2 only.
4104 */
4105 char *
4106did_set_toolbariconsize(optset_T *args UNUSED)
4107{
4108 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
4109 return e_invalid_argument;
4110
4111 out_flush();
4112 gui_mch_show_toolbar((toolbar_flags &
4113 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4114 return NULL;
4115}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004116
4117 int
4118expand_set_toolbariconsize(optexpand_T *args, int *numMatches, char_u ***matches)
4119{
4120 return expand_set_opt_string(
4121 args,
4122 p_tbis_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004123 ARRAY_LENGTH(p_tbis_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004124 numMatches,
4125 matches);
4126}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004127#endif
4128
4129#if defined(UNIX) || defined(VMS) || defined(PROTO)
4130/*
4131 * The 'ttymouse' option is changed.
4132 */
4133 char *
4134did_set_ttymouse(optset_T *args UNUSED)
4135{
4136 char *errmsg = NULL;
4137
4138 // Switch the mouse off before changing the escape sequences used for
4139 // that.
4140 mch_setmouse(FALSE);
4141 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
4142 errmsg = e_invalid_argument;
4143 else
4144 check_mouse_termcode();
4145 if (termcap_active)
4146 setmouse(); // may switch it on again
4147
4148 return errmsg;
4149}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004150
4151 int
4152expand_set_ttymouse(optexpand_T *args, int *numMatches, char_u ***matches)
4153{
4154 return expand_set_opt_string(
4155 args,
4156 p_ttym_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004157 ARRAY_LENGTH(p_ttym_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004158 numMatches,
4159 matches);
4160}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004161#endif
4162
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004163#if defined(FEAT_VARTABS) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004164/*
4165 * The 'varsofttabstop' option is changed.
4166 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004167 char *
4168did_set_varsofttabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004169{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004170 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004171 char_u *cp;
4172
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004173 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004174 VIM_CLEAR(curbuf->b_p_vsts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004175 else
4176 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004177 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004178 {
4179 if (vim_isdigit(*cp))
4180 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004181 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004182 continue;
4183 return e_invalid_argument;
4184 }
4185
4186 int *oldarray = curbuf->b_p_vsts_array;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004187 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004188 {
4189 if (oldarray)
4190 vim_free(oldarray);
4191 }
4192 else
4193 return e_invalid_argument;
4194 }
4195
4196 return NULL;
4197}
4198
4199/*
4200 * The 'vartabstop' option is changed.
4201 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004202 char *
4203did_set_vartabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004204{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004205 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004206 char_u *cp;
4207
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004208 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004209 VIM_CLEAR(curbuf->b_p_vts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004210 else
4211 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004212 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004213 {
4214 if (vim_isdigit(*cp))
4215 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004216 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004217 continue;
4218 return e_invalid_argument;
4219 }
4220
4221 int *oldarray = curbuf->b_p_vts_array;
4222
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004223 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004224 {
4225 vim_free(oldarray);
4226# ifdef FEAT_FOLDING
4227 if (foldmethodIsIndent(curwin))
4228 foldUpdateAll(curwin);
4229# endif
4230 }
4231 else
4232 return e_invalid_argument;
4233 }
4234
4235 return NULL;
4236}
4237#endif
4238
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004239/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004240 * The 'verbosefile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004241 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004242 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004243did_set_verbosefile(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004244{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004245 verbose_stop();
4246 if (*p_vfile != NUL && verbose_open() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004247 return e_invalid_argument;
4248
4249 return NULL;
4250}
4251
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004252#if defined(FEAT_SESSION) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004253/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004254 * The 'viewoptions' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004255 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004256 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004257did_set_viewoptions(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004258{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004259 return did_set_opt_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004260}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004261#endif
4262
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004263#if defined(FEAT_VIMINFO) || defined(PROTO)
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004264/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004265 * The 'viminfo' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004266 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004267 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004268did_set_viminfo(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004269{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004270 char_u *s;
4271 char *errmsg = NULL;
4272
4273 for (s = p_viminfo; *s;)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004274 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004275 // Check it's a valid character
4276 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
4277 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01004278 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004279 break;
4280 }
4281 if (*s == 'n') // name is always last one
4282 break;
4283 else if (*s == 'r') // skip until next ','
4284 {
4285 while (*++s && *s != ',')
4286 ;
4287 }
4288 else if (*s == '%')
4289 {
4290 // optional number
4291 while (vim_isdigit(*++s))
4292 ;
4293 }
4294 else if (*s == '!' || *s == 'h' || *s == 'c')
4295 ++s; // no extra chars
4296 else // must have a number
4297 {
4298 while (vim_isdigit(*++s))
4299 ;
4300
4301 if (!VIM_ISDIGIT(*(s - 1)))
4302 {
4303 if (args->os_errbuf != NULL)
4304 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01004305 vim_snprintf(args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004306 _(e_missing_number_after_angle_str_angle),
4307 transchar_byte(*(s - 1)));
4308 errmsg = args->os_errbuf;
4309 }
4310 else
4311 errmsg = "";
4312 break;
4313 }
4314 }
4315 if (*s == ',')
4316 ++s;
4317 else if (*s)
4318 {
4319 if (args->os_errbuf != NULL)
4320 errmsg = e_missing_comma;
4321 else
4322 errmsg = "";
4323 break;
4324 }
4325 }
4326 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
4327 errmsg = e_must_specify_a_value;
4328
4329 return errmsg;
4330}
4331#endif
4332
4333/*
4334 * The 'virtualedit' option is changed.
4335 */
4336 char *
4337did_set_virtualedit(optset_T *args)
4338{
4339 char_u *ve = p_ve;
4340 unsigned int *flags = &ve_flags;
4341
4342 if (args->os_flags & OPT_LOCAL)
4343 {
4344 ve = curwin->w_p_ve;
4345 flags = &curwin->w_ve_flags;
4346 }
4347
4348 if ((args->os_flags & OPT_LOCAL) && *ve == NUL)
4349 // make the local value empty: use the global value
4350 *flags = 0;
4351 else
4352 {
4353 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
4354 return e_invalid_argument;
4355 else if (STRCMP(ve, args->os_oldval.string) != 0)
4356 {
4357 // Recompute cursor position in case the new 've' setting
4358 // changes something.
4359 validate_virtcol();
4360 coladvance(curwin->w_virtcol);
4361 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004362 }
4363
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004364 return NULL;
4365}
4366
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004367 int
4368expand_set_virtualedit(optexpand_T *args, int *numMatches, char_u ***matches)
4369{
4370 return expand_set_opt_string(
4371 args,
4372 p_ve_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004373 ARRAY_LENGTH(p_ve_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004374 numMatches,
4375 matches);
4376}
4377
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004378/*
4379 * The 'whichwrap' option is changed.
4380 */
4381 char *
4382did_set_whichwrap(optset_T *args)
4383{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004384 char_u **varp = (char_u **)args->os_varp;
4385
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004386 // Add ',' to the list flags because 'whichwrap' is a flag
4387 // list that is comma-separated.
Christian Brabandtb39b2402023-11-29 11:34:05 +01004388 return did_set_option_listflag(*varp, (char_u *)(WW_ALL ","),
4389 args->os_errbuf, args->os_errbuflen);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004390}
4391
4392 int
4393expand_set_whichwrap(optexpand_T *args, int *numMatches, char_u ***matches)
4394{
4395 return expand_set_opt_listflag(args, (char_u*)WW_ALL, numMatches, matches);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004396}
4397
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004398/*
4399 * The 'wildmode' option is changed.
4400 */
4401 char *
4402did_set_wildmode(optset_T *args UNUSED)
4403{
4404 if (check_opt_wim() == FAIL)
4405 return e_invalid_argument;
4406 return NULL;
4407}
4408
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004409 int
4410expand_set_wildmode(optexpand_T *args, int *numMatches, char_u ***matches)
4411{
4412 return expand_set_opt_string(
4413 args,
4414 p_wim_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004415 ARRAY_LENGTH(p_wim_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004416 numMatches,
4417 matches);
4418}
4419
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004420/*
4421 * The 'wildoptions' option is changed.
4422 */
4423 char *
4424did_set_wildoptions(optset_T *args UNUSED)
4425{
4426 return did_set_opt_strings(p_wop, p_wop_values, TRUE);
4427}
4428
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004429 int
4430expand_set_wildoptions(optexpand_T *args, int *numMatches, char_u ***matches)
4431{
4432 return expand_set_opt_string(
4433 args,
4434 p_wop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004435 ARRAY_LENGTH(p_wop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004436 numMatches,
4437 matches);
4438}
4439
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004440#if defined(FEAT_WAK) || defined(PROTO)
4441/*
4442 * The 'winaltkeys' option is changed.
4443 */
4444 char *
4445did_set_winaltkeys(optset_T *args UNUSED)
4446{
4447 char *errmsg = NULL;
4448
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004449 if (*p_wak == NUL || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004450 errmsg = e_invalid_argument;
4451# ifdef FEAT_MENU
4452# if defined(FEAT_GUI_MOTIF)
4453 else if (gui.in_use)
4454 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4455# elif defined(FEAT_GUI_GTK)
4456 else if (gui.in_use)
4457 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4458# endif
4459# endif
4460 return errmsg;
4461}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004462
4463 int
4464expand_set_winaltkeys(optexpand_T *args, int *numMatches, char_u ***matches)
4465{
4466 return expand_set_opt_string(
4467 args,
4468 p_wak_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004469 ARRAY_LENGTH(p_wak_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004470 numMatches,
4471 matches);
4472}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004473#endif
4474
4475/*
4476 * The 'wincolor' option is changed.
4477 */
4478 char *
4479did_set_wincolor(optset_T *args UNUSED)
4480{
4481#ifdef FEAT_TERMINAL
4482 term_update_wincolor(curwin);
4483#endif
4484 return NULL;
4485}
4486
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004487 int
4488expand_set_wincolor(optexpand_T *args, int *numMatches, char_u ***matches)
4489{
4490 return expand_set_opt_generic(
4491 args,
4492 get_highlight_name,
4493 numMatches,
4494 matches);
4495}
4496
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004497#ifdef FEAT_SYN_HL
4498/*
4499 * When the 'syntax' option is set, load the syntax of that name.
4500 */
4501 static void
4502do_syntax_autocmd(int value_changed)
4503{
4504 static int syn_recursive = 0;
4505
4506 ++syn_recursive;
4507 // Only pass TRUE for "force" when the value changed or not used
4508 // recursively, to avoid endless recurrence.
4509 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
4510 value_changed || syn_recursive == 1, curbuf);
4511 curbuf->b_flags |= BF_SYN_SET;
4512 --syn_recursive;
4513}
4514#endif
4515
4516/*
4517 * When the 'filetype' option is set, trigger the FileType autocommand.
4518 */
4519 static void
4520do_filetype_autocmd(char_u **varp, int opt_flags, int value_changed)
4521{
4522 // Skip this when called from a modeline and the filetype was already set
4523 // to this value.
4524 if ((opt_flags & OPT_MODELINE) && !value_changed)
4525 return;
4526
4527 static int ft_recursive = 0;
4528 int secure_save = secure;
4529
4530 // Reset the secure flag, since the value of 'filetype' has
4531 // been checked to be safe.
4532 secure = 0;
4533
4534 ++ft_recursive;
zeertzjq5bf6c212024-03-31 18:41:27 +02004535 curbuf->b_did_filetype = TRUE;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004536 // Only pass TRUE for "force" when the value changed or not
4537 // used recursively, to avoid endless recurrence.
4538 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
4539 value_changed || ft_recursive == 1, curbuf);
4540 --ft_recursive;
4541 // Just in case the old "curbuf" is now invalid.
4542 if (varp != &(curbuf->b_p_ft))
4543 varp = NULL;
4544
4545 secure = secure_save;
4546}
4547
4548#ifdef FEAT_SPELL
4549/*
4550 * When the 'spelllang' option is set, source the spell/LANG.vim file in
4551 * 'runtimepath'.
4552 */
4553 static void
4554do_spelllang_source(void)
4555{
4556 char_u fname[200];
4557 char_u *p;
4558 char_u *q = curwin->w_s->b_p_spl;
4559
4560 // Skip the first name if it is "cjk".
4561 if (STRNCMP(q, "cjk,", 4) == 0)
4562 q += 4;
4563
4564 // They could set 'spellcapcheck' depending on the language. Use the first
4565 // name in 'spelllang' up to '_region' or '.encoding'.
4566 for (p = q; *p != NUL; ++p)
4567 if (!ASCII_ISALNUM(*p) && *p != '-')
4568 break;
4569 if (p > q)
4570 {
4571 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
4572 (int)(p - q), q);
4573 source_runtime(fname, DIP_ALL);
4574 }
4575}
4576#endif
4577
4578/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004579 * Handle string options that need some action to perform when changed.
zeertzjqf6782732022-07-27 18:26:03 +01004580 * The new value must be allocated.
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004581 * Returns NULL for success, or an untranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +02004582 */
4583 char *
4584did_set_string_option(
4585 int opt_idx, // index in options[] table
4586 char_u **varp, // pointer to the option variable
Bram Moolenaardac13472019-09-16 21:06:21 +02004587 char_u *oldval, // previous value of the option
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004588 char_u *value, // new value of the option
Bram Moolenaardac13472019-09-16 21:06:21 +02004589 char *errbuf, // buffer for errors, or NULL
Mike Williams620f0112023-12-05 15:36:06 +01004590 size_t errbuflen, // length of error buffer
Bram Moolenaardac13472019-09-16 21:06:21 +02004591 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004592 set_op_T op, // OP_ADDING/OP_PREPENDING/OP_REMOVING
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004593 int *value_checked) // value was checked to be safe, no
Bram Moolenaardac13472019-09-16 21:06:21 +02004594 // need to set P_INSECURE
4595{
4596 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +02004597 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004598 opt_did_set_cb_T did_set_cb = get_option_did_set_cb(opt_idx);
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004599 optset_T args;
4600
4601 // 'ttytype' is an alias for 'term'. Both 'term' and 'ttytype' point to
4602 // T_NAME. If 'term' or 'ttytype' is modified, then use the index for the
4603 // 'term' option. Only set the P_ALLOCED flag on 'term'.
4604 if (varp == &T_NAME)
4605 {
4606 opt_idx = findoption((char_u *)"term");
4607 if (opt_idx >= 0)
4608 {
4609 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
4610 did_set_cb = get_option_did_set_cb(opt_idx);
4611 }
4612 }
4613
4614 CLEAR_FIELD(args);
Bram Moolenaardac13472019-09-16 21:06:21 +02004615
Bram Moolenaardac13472019-09-16 21:06:21 +02004616 // Disallow changing some options from secure mode
4617 if ((secure
4618#ifdef HAVE_SANDBOX
4619 || sandbox != 0
4620#endif
4621 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00004622 errmsg = e_not_allowed_here;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004623 // Check for a "normal" directory or file name in some options.
4624 else if (check_illegal_path_names(opt_idx, varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004625 errmsg = e_invalid_argument;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004626 else if (did_set_cb != NULL)
4627 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004628 args.os_varp = (char_u *)varp;
4629 args.os_idx = opt_idx;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004630 args.os_flags = opt_flags;
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004631 args.os_op = op;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004632 args.os_oldval.string = oldval;
4633 args.os_newval.string = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004634 args.os_errbuf = errbuf;
Christian Brabandtb39b2402023-11-29 11:34:05 +01004635 args.os_errbuflen = errbuflen;
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004636 // Invoke the option specific callback function to validate and apply
4637 // the new option value.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004638 errmsg = did_set_cb(&args);
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004639
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004640 // The 'keymap', 'filetype' and 'syntax' option callback functions
4641 // may change the os_value_checked field.
4642 *value_checked = args.os_value_checked;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004643 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004644
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004645 // If an error is detected, restore the previous value.
Bram Moolenaardac13472019-09-16 21:06:21 +02004646 if (errmsg != NULL)
4647 {
zeertzjqf6782732022-07-27 18:26:03 +01004648 free_string_option(*varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02004649 *varp = oldval;
4650 // When resetting some values, need to act on it.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004651 if (args.os_restore_chartab)
Bram Moolenaardac13472019-09-16 21:06:21 +02004652 (void)init_chartab();
4653 if (varp == &p_hl)
4654 (void)highlight_changed();
4655 }
4656 else
4657 {
4658#ifdef FEAT_EVAL
4659 // Remember where the option was set.
4660 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4661#endif
4662 // Free string options that are in allocated memory.
4663 // Use "free_oldval", because recursiveness may change the flags under
4664 // our fingers (esp. init_highlight()).
4665 if (free_oldval)
4666 free_string_option(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01004667 set_option_flag(opt_idx, P_ALLOCED);
Bram Moolenaardac13472019-09-16 21:06:21 +02004668
4669 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
4670 && is_global_local_option(opt_idx))
4671 {
4672 // global option with local value set to use global value; free
4673 // the local value and make it empty
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004674 char_u *p = get_option_varp_scope(opt_idx, OPT_LOCAL);
Bram Moolenaardac13472019-09-16 21:06:21 +02004675 free_string_option(*(char_u **)p);
4676 *(char_u **)p = empty_option;
4677 }
4678
4679 // May set global value for local option.
4680 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
4681 set_string_option_global(opt_idx, varp);
4682
4683 // Trigger the autocommand only after setting the flags.
4684#ifdef FEAT_SYN_HL
Bram Moolenaardac13472019-09-16 21:06:21 +02004685 if (varp == &(curbuf->b_p_syn))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004686 do_syntax_autocmd(args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004687#endif
4688 else if (varp == &(curbuf->b_p_ft))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004689 do_filetype_autocmd(varp, opt_flags, args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004690#ifdef FEAT_SPELL
4691 if (varp == &(curwin->w_s->b_p_spl))
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004692 do_spelllang_source();
Bram Moolenaardac13472019-09-16 21:06:21 +02004693#endif
4694 }
4695
Bram Moolenaardac13472019-09-16 21:06:21 +02004696 if (varp == &p_mouse)
4697 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004698 if (*p_mouse == NUL)
4699 mch_setmouse(FALSE); // switch mouse off
4700 else
Bram Moolenaardac13472019-09-16 21:06:21 +02004701 setmouse(); // in case 'mouse' changed
4702 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004703
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004704 if (varp == &p_rtp)
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004705 {
4706 export_myvimdir();
4707#if defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004708 update_package_paths_in_lua();
4709#endif
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004710 }
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004711
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004712#if defined(FEAT_LINEBREAK)
4713 // Changing Formatlistpattern when briopt includes the list setting:
4714 // redraw
4715 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
4716 && curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004717 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004718#endif
4719
Bram Moolenaardac13472019-09-16 21:06:21 +02004720 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004721 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0
4722 && (get_option_flags(opt_idx) & P_HLONLY) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02004723 curwin->w_set_curswant = TRUE;
4724
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004725 if ((opt_flags & OPT_NO_REDRAW) == 0)
4726 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004727#ifdef FEAT_GUI
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004728 // set when changing an option that only requires a redraw in the GUI
4729 int redraw_gui_only = FALSE;
4730
4731 if (varp == &p_go // 'guioptions'
4732 || varp == &p_guifont // 'guifont'
4733# ifdef FEAT_GUI_TABLINE
4734 || varp == &p_gtl // 'guitablabel'
4735 || varp == &p_gtt // 'guitabtooltip'
4736# endif
4737# ifdef FEAT_XFONTSET
4738 || varp == &p_guifontset // 'guifontset'
4739# endif
4740 || varp == &p_guifontwide // 'guifontwide'
Erik S. V. Jansson2f026382024-02-26 22:23:05 +01004741# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004742 || varp == &p_guiligatures // 'guiligatures'
4743# endif
4744 )
4745 redraw_gui_only = TRUE;
4746
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004747 // check redraw when it's not a GUI option or the GUI is active.
4748 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02004749#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004750 check_redraw(get_option_flags(opt_idx));
4751 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004752
4753#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004754 if (args.os_did_swaptcap)
Bram Moolenaardac13472019-09-16 21:06:21 +02004755 {
4756 set_termname((char_u *)"win32");
4757 init_highlight(TRUE, FALSE);
4758 }
4759#endif
4760
4761 return errmsg;
4762}
4763
4764/*
4765 * Check an option that can be a range of string values.
4766 *
4767 * Return OK for correct value, FAIL otherwise.
4768 * Empty is always OK.
4769 */
4770 static int
4771check_opt_strings(
4772 char_u *val,
4773 char **values,
4774 int list) // when TRUE: accept a list of values
4775{
4776 return opt_strings_flags(val, values, NULL, list);
4777}
4778
4779/*
4780 * Handle an option that can be a range of string values.
4781 * Set a flag in "*flagp" for each string present.
4782 *
4783 * Return OK for correct value, FAIL otherwise.
4784 * Empty is always OK.
4785 */
4786 static int
4787opt_strings_flags(
4788 char_u *val, // new value
4789 char **values, // array of valid string values
4790 unsigned *flagp,
4791 int list) // when TRUE: accept a list of values
4792{
4793 int i;
4794 int len;
4795 unsigned new_flags = 0;
4796
4797 while (*val)
4798 {
4799 for (i = 0; ; ++i)
4800 {
4801 if (values[i] == NULL) // val not found in values[]
4802 return FAIL;
4803
4804 len = (int)STRLEN(values[i]);
4805 if (STRNCMP(values[i], val, len) == 0
4806 && ((list && val[len] == ',') || val[len] == NUL))
4807 {
4808 val += len + (val[len] == ',');
4809 new_flags |= (1 << i);
4810 break; // check next item in val list
4811 }
4812 }
4813 }
4814 if (flagp != NULL)
4815 *flagp = new_flags;
4816
4817 return OK;
4818}
4819
4820/*
4821 * return OK if "p" is a valid fileformat name, FAIL otherwise.
4822 */
4823 int
4824check_ff_value(char_u *p)
4825{
4826 return check_opt_strings(p, p_ff_values, FALSE);
4827}
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004828
4829/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004830 * Save the actual shortmess Flags and clear them temporarily to avoid that
4831 * file messages overwrites any output from the following commands.
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004832 *
4833 * Caller must make sure to first call save_clear_shm_value() and then
4834 * restore_shm_value() exactly the same number of times.
4835 */
4836 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004837save_clear_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004838{
4839 if (STRLEN(p_shm) >= SHM_LEN)
4840 {
4841 iemsg(e_internal_error_shortmess_too_long);
4842 return;
4843 }
4844
4845 if (++set_shm_recursive == 1)
4846 {
4847 STRCPY(shm_buf, p_shm);
4848 set_option_value_give_err((char_u *)"shm", 0L, (char_u *)"", 0);
4849 }
4850}
4851
4852/*
4853 * Restore the shortmess Flags set from the save_clear_shm_value() function.
4854 */
4855 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004856restore_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004857{
4858 if (--set_shm_recursive == 0)
4859 {
4860 set_option_value_give_err((char_u *)"shm", 0L, shm_buf, 0);
4861 vim_memset(shm_buf, 0, SHM_LEN);
4862 }
4863}
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004864
4865/*
4866 * Export the environment variable $MYVIMDIR to the first item in runtimepath
4867 */
Christian Brabandt1a741d32025-03-01 16:30:33 +01004868 void
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004869export_myvimdir()
4870{
4871 int dofree = FALSE;
4872 char_u *p;
4873 char_u *q = p_rtp;
4874 char_u *buf = alloc(MAXPATHL);
4875
4876 if (buf == NULL)
4877 return;
4878
4879 (void)copy_option_part(&q, buf, MAXPATHL, ",");
4880
4881 p = vim_getenv((char_u *)"MYVIMDIR", &dofree);
4882
4883 if (p == NULL || STRCMP(p, buf) != 0)
4884 {
4885 add_pathsep(buf);
4886#ifdef MSWIN
4887 // normalize path separators
4888 for (q = buf; *q != NUL; q++)
4889 if (*q == '/')
4890 *q = '\\';
4891#endif
4892 vim_setenv((char_u *)"MYVIMDIR", buf);
4893 }
4894 if (dofree)
4895 vim_free(p);
4896 vim_free(buf);
4897}