blob: 90b8e52ffbdc23ecf29b26e1764874ef4f298f36 [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 Palya0ac1eb32025-04-16 20:18:33 +02001561 char_u *p, *t;
Girish Palyacbe53192025-04-14 22:13:15 +02001562 char_u buffer[LSIZE];
1563 char_u *buf_ptr;
Girish Palya0ac1eb32025-04-16 20:18:33 +02001564 char_u char_before = NUL;
Girish Palyacbe53192025-04-14 22:13:15 +02001565 int escape;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001566
Girish Palyacbe53192025-04-14 22:13:15 +02001567 for (p = *varp; *p; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001568 {
Girish Palyacbe53192025-04-14 22:13:15 +02001569 vim_memset(buffer, 0, LSIZE);
1570 buf_ptr = buffer;
1571 escape = 0;
1572
1573 // Extract substring while handling escaped commas
1574 while (*p && (*p != ',' || escape) && buf_ptr < (buffer + LSIZE - 1))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001575 {
Girish Palyacbe53192025-04-14 22:13:15 +02001576 if (*p == '\\' && *(p + 1) == ',')
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001577 {
Girish Palyacbe53192025-04-14 22:13:15 +02001578 escape = 1; // Mark escape mode
1579 p++; // Skip '\'
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001580 }
1581 else
1582 {
Girish Palyacbe53192025-04-14 22:13:15 +02001583 escape = 0;
1584 *buf_ptr++ = *p;
1585 }
1586 p++;
1587 }
1588 *buf_ptr = NUL;
1589
1590 if (vim_strchr((char_u *)".wbuksid]tUfo", *buffer) == NULL)
1591 return illegal_char(args->os_errbuf, args->os_errbuflen, *buffer);
1592
Girish Palya0ac1eb32025-04-16 20:18:33 +02001593 if (vim_strchr((char_u *)"ksf", *buffer) == NULL && *(buffer + 1) != NUL
1594 && *(buffer + 1) != '^')
1595 char_before = *buffer;
1596 else
1597 {
1598 // Test for a number after '^'
1599 if ((t = vim_strchr(buffer, '^')) != NULL)
1600 {
1601 *t++ = NUL;
1602 if (!*t)
1603 char_before = '^';
1604 else
1605 {
1606 for (; *t; t++)
1607 {
1608 if (!vim_isdigit(*t))
1609 {
1610 char_before = '^';
1611 break;
1612 }
1613 }
1614 }
1615 }
1616 }
1617 if (char_before != NUL)
Girish Palyacbe53192025-04-14 22:13:15 +02001618 {
1619 if (args->os_errbuf)
1620 {
1621 vim_snprintf((char *)args->os_errbuf, args->os_errbuflen,
Girish Palya0ac1eb32025-04-16 20:18:33 +02001622 _(e_illegal_character_after_chr), char_before);
Girish Palyacbe53192025-04-14 22:13:15 +02001623 return args->os_errbuf;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001624 }
Girish Palya0ac1eb32025-04-16 20:18:33 +02001625 return NULL;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001626 }
Girish Palyacbe53192025-04-14 22:13:15 +02001627 // Skip comma and spaces
1628 while (*p == ',' || *p == ' ')
1629 p++;
1630 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001631 return NULL;
1632}
1633
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001634 int
1635expand_set_complete(optexpand_T *args, int *numMatches, char_u ***matches)
1636{
1637 static char *(p_cpt_values[]) = {
Girish Palyacbe53192025-04-14 22:13:15 +02001638 ".", "w", "b", "u", "k", "kspell", "s", "i", "d", "]", "t", "U", "f", "o",
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001639 NULL};
1640 return expand_set_opt_string(
1641 args,
1642 p_cpt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001643 ARRAY_LENGTH(p_cpt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001644 numMatches,
1645 matches);
1646}
1647
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001648/*
1649 * The 'completeopt' option is changed.
1650 */
1651 char *
1652did_set_completeopt(optset_T *args UNUSED)
1653{
zeertzjq529b9ad2024-06-05 20:27:06 +02001654 char_u *cot = p_cot;
1655 unsigned *flags = &cot_flags;
1656
1657 if (args->os_flags & OPT_LOCAL)
1658 {
1659 cot = curbuf->b_p_cot;
1660 flags = &curbuf->b_cot_flags;
1661 }
zeertzjq46dcd842024-11-03 09:10:50 +01001662 else if (!(args->os_flags & OPT_GLOBAL))
1663 // When using :set, clear the local flags.
1664 curbuf->b_cot_flags = 0;
zeertzjq529b9ad2024-06-05 20:27:06 +02001665
1666 if (check_opt_strings(cot, p_cot_values, TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001667 return e_invalid_argument;
1668
zeertzjq529b9ad2024-06-05 20:27:06 +02001669 if (opt_strings_flags(cot, p_cot_values, flags, TRUE) != OK)
1670 return e_invalid_argument;
1671
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001672 return NULL;
1673}
1674
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001675 int
1676expand_set_completeopt(optexpand_T *args, int *numMatches, char_u ***matches)
1677{
1678 return expand_set_opt_string(
1679 args,
1680 p_cot_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001681 ARRAY_LENGTH(p_cot_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001682 numMatches,
1683 matches);
1684}
1685
glepnir6a89c942024-10-01 20:32:12 +02001686/*
glepnirf31cfa22025-03-06 21:59:13 +01001687 * The 'completefuzzycollect' option is changed.
1688 */
1689 char *
1690did_set_completefuzzycollect(optset_T *args UNUSED)
1691{
1692 if (opt_strings_flags(p_cfc, p_cfc_values, &cfc_flags, TRUE) != OK)
1693 return e_invalid_argument;
1694 return NULL;
1695}
1696
zeertzjq53d59ec2025-03-07 19:09:09 +01001697 int
1698expand_set_completefuzzycollect(
1699 optexpand_T *args,
1700 int *numMatches,
1701 char_u ***matches)
1702{
1703 return expand_set_opt_string(
1704 args,
1705 p_cfc_values,
1706 ARRAY_LENGTH(p_cfc_values) - 1,
1707 numMatches,
1708 matches);
1709}
1710
glepnirf31cfa22025-03-06 21:59:13 +01001711/*
glepnir6a89c942024-10-01 20:32:12 +02001712 * The 'completeitemalign' option is changed.
1713 */
1714 char *
1715did_set_completeitemalign(optset_T *args UNUSED)
1716{
1717 char_u *p = p_cia;
1718 unsigned new_cia_flags = 0;
1719 int seen[3] = { FALSE, FALSE, FALSE };
1720 int count = 0;
1721 char_u buf[10];
1722
1723 while (*p)
1724 {
1725 copy_option_part(&p, buf, sizeof(buf), ",");
1726 if (count >= 3)
1727 return e_invalid_argument;
1728
1729 if (STRCMP(buf, "abbr") == 0)
1730 {
1731 if (seen[CPT_ABBR])
1732 return e_invalid_argument;
1733 new_cia_flags = new_cia_flags * 10 + CPT_ABBR;
1734 seen[CPT_ABBR] = TRUE;
1735 count++;
1736 }
1737 else if (STRCMP(buf, "kind") == 0)
1738 {
1739 if (seen[CPT_KIND])
1740 return e_invalid_argument;
1741 new_cia_flags = new_cia_flags * 10 + CPT_KIND;
1742 seen[CPT_KIND] = TRUE;
1743 count++;
1744 }
1745 else if (STRCMP(buf, "menu") == 0)
1746 {
1747 if (seen[CPT_MENU])
1748 return e_invalid_argument;
1749 new_cia_flags = new_cia_flags * 10 + CPT_MENU;
1750 seen[CPT_MENU] = TRUE;
1751 count++;
1752 }
1753 else
1754 return e_invalid_argument;
1755 }
1756 if (new_cia_flags == 0 || count != 3)
1757 return e_invalid_argument;
1758
1759 cia_flags = new_cia_flags;
1760 return NULL;
1761}
1762
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001763#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1764/*
1765 * The 'completepopup' option is changed.
1766 */
1767 char *
1768did_set_completepopup(optset_T *args UNUSED)
1769{
1770 if (parse_completepopup(NULL) == FAIL)
1771 return e_invalid_argument;
1772
1773 popup_close_info();
1774 return NULL;
1775}
1776#endif
1777
1778#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1779/*
1780 * The 'completeslash' option is changed.
1781 */
1782 char *
1783did_set_completeslash(optset_T *args UNUSED)
1784{
1785 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1786 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
1787 return e_invalid_argument;
1788
1789 return NULL;
1790}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001791
1792 int
1793expand_set_completeslash(optexpand_T *args, int *numMatches, char_u ***matches)
1794{
1795 return expand_set_opt_string(
1796 args,
1797 p_csl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001798 ARRAY_LENGTH(p_csl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001799 numMatches,
1800 matches);
1801}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001802#endif
1803
1804#if defined(FEAT_CONCEAL) || defined(PROTO)
1805/*
1806 * The 'concealcursor' option is changed.
1807 */
1808 char *
1809did_set_concealcursor(optset_T *args)
1810{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001811 char_u **varp = (char_u **)args->os_varp;
1812
Christian Brabandtb39b2402023-11-29 11:34:05 +01001813 return did_set_option_listflag(*varp, (char_u *)COCU_ALL, args->os_errbuf,
1814 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001815}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001816
1817 int
1818expand_set_concealcursor(optexpand_T *args, int *numMatches, char_u ***matches)
1819{
1820 return expand_set_opt_listflag(args, (char_u*)COCU_ALL, numMatches, matches);
1821}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001822#endif
1823
1824/*
1825 * The 'cpoptions' option is changed.
1826 */
1827 char *
1828did_set_cpoptions(optset_T *args)
1829{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001830 char_u **varp = (char_u **)args->os_varp;
1831
Christian Brabandtb39b2402023-11-29 11:34:05 +01001832 return did_set_option_listflag(*varp, (char_u *)CPO_ALL, args->os_errbuf,
1833 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001834}
1835
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001836 int
1837expand_set_cpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
1838{
1839 return expand_set_opt_listflag(args, (char_u*)CPO_ALL, numMatches, matches);
1840}
1841
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001842#if defined(FEAT_CRYPT) || defined(PROTO)
1843/*
1844 * The 'cryptkey' option is changed.
1845 */
1846 char *
1847did_set_cryptkey(optset_T *args)
1848{
1849 // Make sure the ":set" command doesn't show the new value in the
1850 // history.
1851 remove_key_from_history();
1852
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02001853 if (args->os_op != OP_NONE)
1854 // Don't allow set+=/-=/^= as they can allow for substring guessing
1855 return e_invalid_argument;
1856
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001857 if (STRCMP(curbuf->b_p_key, args->os_oldval.string) != 0)
1858 {
1859 // Need to update the swapfile.
1860 ml_set_crypt_key(curbuf, args->os_oldval.string,
1861 *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
1862 changed_internal();
1863 }
Christian Brabandt19e6c4f2023-06-27 18:57:10 +01001864# ifdef FEAT_SODIUM
1865 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
1866 crypt_sodium_lock_key(args->os_newval.string);
1867# endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001868
1869 return NULL;
1870}
1871
1872/*
1873 * The 'cryptmethod' option is changed.
1874 */
1875 char *
1876did_set_cryptmethod(optset_T *args)
1877{
1878 char_u *p;
1879 char_u *s;
1880
1881 if (args->os_flags & OPT_LOCAL)
1882 p = curbuf->b_p_cm;
1883 else
1884 p = p_cm;
1885 if (check_opt_strings(p, p_cm_values, TRUE) != OK)
1886 return e_invalid_argument;
1887 else if (crypt_self_test() == FAIL)
1888 return e_invalid_argument;
1889
1890 // When setting the global value to empty, make it "zip".
1891 if (*p_cm == NUL)
1892 {
1893 free_string_option(p_cm);
1894 p_cm = vim_strsave((char_u *)"zip");
1895 }
1896 // When using ":set cm=name" the local value is going to be empty.
1897 // Do that here, otherwise the crypt functions will still use the
1898 // local value.
1899 if ((args->os_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1900 {
1901 free_string_option(curbuf->b_p_cm);
1902 curbuf->b_p_cm = empty_option;
1903 }
1904
1905 // Need to update the swapfile when the effective method changed.
1906 // Set "s" to the effective old value, "p" to the effective new
1907 // method and compare.
1908 if ((args->os_flags & OPT_LOCAL) && *args->os_oldval.string == NUL)
1909 s = p_cm; // was previously using the global value
1910 else
1911 s = args->os_oldval.string;
1912 if (*curbuf->b_p_cm == NUL)
1913 p = p_cm; // is now using the global value
1914 else
1915 p = curbuf->b_p_cm;
1916 if (STRCMP(s, p) != 0)
1917 ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1918
1919 // If the global value changes need to update the swapfile for all
1920 // buffers using that value.
1921 if ((args->os_flags & OPT_GLOBAL)
1922 && STRCMP(p_cm, args->os_oldval.string) != 0)
1923 {
1924 buf_T *buf;
1925
1926 FOR_ALL_BUFFERS(buf)
1927 if (buf != curbuf && *buf->b_p_cm == NUL)
1928 ml_set_crypt_key(buf, buf->b_p_key, args->os_oldval.string);
1929 }
1930 return NULL;
1931}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001932
1933 int
1934expand_set_cryptmethod(optexpand_T *args, int *numMatches, char_u ***matches)
1935{
1936 return expand_set_opt_string(
1937 args,
1938 p_cm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001939 ARRAY_LENGTH(p_cm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001940 numMatches,
1941 matches);
1942}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001943#endif
1944
1945#if (defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1946/*
1947 * The 'cscopequickfix' option is changed.
1948 */
1949 char *
1950did_set_cscopequickfix(optset_T *args UNUSED)
1951{
1952 char_u *p;
1953
1954 if (p_csqf == NULL)
1955 return NULL;
1956
1957 p = p_csqf;
1958 while (*p != NUL)
1959 {
1960 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
1961 || p[1] == NUL
1962 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
1963 || (p[2] != NUL && p[2] != ','))
1964 return e_invalid_argument;
1965 else if (p[2] == NUL)
1966 break;
1967 else
1968 p += 3;
1969 }
1970
1971 return NULL;
1972}
1973#endif
1974
1975#if defined(FEAT_SYN_HL) || defined(PROTO)
1976/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001977 * The 'cursorlineopt' option is changed.
1978 */
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001979 char *
1980did_set_cursorlineopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001981{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001982 char_u **varp = (char_u **)args->os_varp;
1983
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001984 // This could be changed to use opt_strings_flags() instead.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001985 if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001986 return e_invalid_argument;
1987
1988 return NULL;
1989}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001990
1991 int
1992expand_set_cursorlineopt(optexpand_T *args, int *numMatches, char_u ***matches)
1993{
1994 return expand_set_opt_string(
1995 args,
1996 p_culopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001997 ARRAY_LENGTH(p_culopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001998 numMatches,
1999 matches);
2000}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002001#endif
2002
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002003/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002004 * The 'debug' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002005 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002006 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002007did_set_debug(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002008{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002009 return did_set_opt_strings(p_debug, p_debug_values, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002010}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002011
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002012 int
2013expand_set_debug(optexpand_T *args, int *numMatches, char_u ***matches)
2014{
2015 return expand_set_opt_string(
2016 args,
2017 p_debug_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002018 ARRAY_LENGTH(p_debug_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002019 numMatches,
2020 matches);
2021}
2022
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002023#if defined(FEAT_DIFF) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002024/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002025 * The 'diffopt' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002026 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002027 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002028did_set_diffopt(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002029{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002030 if (diffopt_changed() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002031 return e_invalid_argument;
2032
2033 return NULL;
2034}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002035
2036 int
2037expand_set_diffopt(optexpand_T *args, int *numMatches, char_u ***matches)
2038{
2039 expand_T *xp = args->oe_xp;
2040
2041 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2042 {
2043 // Within "algorithm:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002044 int algo_len = (int)STRLEN("algorithm:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002045 if (xp->xp_pattern - args->oe_set_arg >= algo_len &&
2046 STRNCMP(xp->xp_pattern - algo_len, "algorithm:", algo_len) == 0)
2047 {
2048 return expand_set_opt_string(
2049 args,
2050 p_dip_algorithm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002051 ARRAY_LENGTH(p_dip_algorithm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002052 numMatches,
2053 matches);
2054 }
Yee Cheng Chin9943d472025-03-26 19:41:02 +01002055 // Within "inline:", we have a subgroup of possible options.
2056 int inline_len = (int)STRLEN("inline:");
2057 if (xp->xp_pattern - args->oe_set_arg >= inline_len &&
2058 STRNCMP(xp->xp_pattern - inline_len, "inline:", inline_len) == 0)
2059 {
2060 return expand_set_opt_string(
2061 args,
2062 p_dip_inline_values,
2063 ARRAY_LENGTH(p_dip_inline_values) - 1,
2064 numMatches,
2065 matches);
2066 }
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002067 return FAIL;
2068 }
2069
2070 return expand_set_opt_string(
2071 args,
2072 p_dip_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002073 ARRAY_LENGTH(p_dip_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002074 numMatches,
2075 matches);
2076}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002077#endif
2078
2079/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002080 * The 'display' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002081 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002082 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002083did_set_display(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002084{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002085 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002086 return e_invalid_argument;
2087
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002088 (void)init_chartab();
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002089 return NULL;
2090}
2091
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002092 int
2093expand_set_display(optexpand_T *args, int *numMatches, char_u ***matches)
2094{
2095 return expand_set_opt_string(
2096 args,
2097 p_dy_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002098 ARRAY_LENGTH(p_dy_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002099 numMatches,
2100 matches);
2101}
2102
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002103/*
2104 * The 'eadirection' option is changed.
2105 */
2106 char *
2107did_set_eadirection(optset_T *args UNUSED)
2108{
2109 return did_set_opt_strings(p_ead, p_ead_values, FALSE);
2110}
2111
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002112 int
2113expand_set_eadirection(optexpand_T *args, int *numMatches, char_u ***matches)
2114{
2115 return expand_set_opt_string(
2116 args,
2117 p_ead_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002118 ARRAY_LENGTH(p_ead_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002119 numMatches,
2120 matches);
2121}
2122
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002123/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002124 * One of the 'encoding', 'fileencoding', 'termencoding' or 'makeencoding'
2125 * options is changed.
2126 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002127 char *
2128did_set_encoding(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002129{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002130 char_u **varp = (char_u **)args->os_varp;
2131 char_u **gvarp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002132 char *errmsg = NULL;
2133 char_u *p;
2134
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002135 // Get the global option to compare with, otherwise we would have to check
2136 // two values for all local options.
2137 gvarp = (char_u **)get_option_varp_scope(args->os_idx, OPT_GLOBAL);
2138
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002139 if (gvarp == &p_fenc)
2140 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002141 if (!curbuf->b_p_ma && args->os_flags != OPT_GLOBAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002142 errmsg = e_cannot_make_changes_modifiable_is_off;
2143 else if (vim_strchr(*varp, ',') != NULL)
2144 // No comma allowed in 'fileencoding'; catches confusing it
2145 // with 'fileencodings'.
2146 errmsg = e_invalid_argument;
2147 else
2148 {
2149 // May show a "+" in the title now.
2150 redraw_titles();
2151 // Add 'fileencoding' to the swap file.
2152 ml_setflags(curbuf);
2153 }
2154 }
2155 if (errmsg == NULL)
2156 {
2157 // canonize the value, so that STRCMP() can be used on it
2158 p = enc_canonize(*varp);
2159 if (p != NULL)
2160 {
2161 vim_free(*varp);
2162 *varp = p;
2163 }
2164 if (varp == &p_enc)
2165 {
2166 errmsg = mb_init();
2167 redraw_titles();
2168 }
2169 }
2170
2171#if defined(FEAT_GUI_GTK)
2172 if (errmsg == NULL && varp == &p_tenc && gui.in_use)
2173 {
2174 // GTK uses only a single encoding, and that is UTF-8.
2175 if (STRCMP(p_tenc, "utf-8") != 0)
2176 errmsg = e_cannot_be_changed_in_gtk_GUI;
2177 }
2178#endif
2179
2180 if (errmsg == NULL)
2181 {
2182#ifdef FEAT_KEYMAP
2183 // When 'keymap' is used and 'encoding' changes, reload the keymap
2184 // (with another encoding).
2185 if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
2186 (void)keymap_init();
2187#endif
2188
2189 // When 'termencoding' is not empty and 'encoding' changes or when
2190 // 'termencoding' changes, need to setup for keyboard input and
2191 // display output conversion.
2192 if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
2193 {
2194 if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
2195 || convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
2196 {
2197 semsg(_(e_cannot_convert_between_str_and_str),
2198 p_tenc, p_enc);
2199 errmsg = e_invalid_argument;
2200 }
2201 }
2202
2203#if defined(MSWIN)
K.Takatace3189d2023-02-15 19:13:43 +00002204 // $HOME, $VIM and $VIMRUNTIME may have characters in active code page.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002205 if (varp == &p_enc)
K.Takatace3189d2023-02-15 19:13:43 +00002206 {
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002207 init_homedir();
K.Takatace3189d2023-02-15 19:13:43 +00002208 init_vimdir();
2209 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002210#endif
2211 }
2212
2213 return errmsg;
2214}
2215
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002216 int
2217expand_set_encoding(optexpand_T *args, int *numMatches, char_u ***matches)
2218{
2219 return expand_set_opt_generic(
2220 args,
2221 get_encoding_name,
2222 numMatches,
2223 matches);
2224}
2225
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002226/*
Luuk van Baalb7147f82025-02-08 18:52:39 +01002227 * The 'eventignore(win)' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002228 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002229 char *
Luuk van Baalb7147f82025-02-08 18:52:39 +01002230did_set_eventignore(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002231{
Luuk van Baalb7147f82025-02-08 18:52:39 +01002232 char_u **varp = (char_u **)args->os_varp;
2233
2234 if (check_ei(*varp) == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002235 return e_invalid_argument;
2236 return NULL;
2237}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002238
Luuk van Baalb7147f82025-02-08 18:52:39 +01002239static int expand_eiw = FALSE;
2240
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002241 static char_u *
2242get_eventignore_name(expand_T *xp, int idx)
2243{
Luuk van Baalb7147f82025-02-08 18:52:39 +01002244 // 'eventignore(win)' allows special keyword "all" in addition to
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002245 // all event names.
2246 if (idx == 0)
2247 return (char_u *)"all";
Luuk van Baalb7147f82025-02-08 18:52:39 +01002248 return get_event_name_no_group(xp, idx - 1, expand_eiw);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002249}
2250
2251 int
2252expand_set_eventignore(optexpand_T *args, int *numMatches, char_u ***matches)
2253{
Luuk van Baalb7147f82025-02-08 18:52:39 +01002254 expand_eiw = args->oe_varp != (char_u *)&p_ei;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002255 return expand_set_opt_generic(
2256 args,
2257 get_eventignore_name,
2258 numMatches,
2259 matches);
2260}
2261
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002262/*
2263 * The 'fileformat' option is changed.
2264 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002265 char *
2266did_set_fileformat(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002267{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002268 char_u **varp = (char_u **)args->os_varp;
2269
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002270 if (!curbuf->b_p_ma && !(args->os_flags & OPT_GLOBAL))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002271 return e_cannot_make_changes_modifiable_is_off;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002272 else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002273 return e_invalid_argument;
2274
2275 // may also change 'textmode'
2276 if (get_fileformat(curbuf) == EOL_DOS)
2277 curbuf->b_p_tx = TRUE;
2278 else
2279 curbuf->b_p_tx = FALSE;
2280 redraw_titles();
2281 // update flag in swap file
2282 ml_setflags(curbuf);
2283 // Redraw needed when switching to/from "mac": a CR in the text
2284 // will be displayed differently.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002285 if (get_fileformat(curbuf) == EOL_MAC || *args->os_oldval.string == 'm')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002286 redraw_curbuf_later(UPD_NOT_VALID);
2287
2288 return NULL;
2289}
2290
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002291 int
2292expand_set_fileformat(optexpand_T *args, int *numMatches, char_u ***matches)
2293{
2294 return expand_set_opt_string(
2295 args,
2296 p_ff_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002297 ARRAY_LENGTH(p_ff_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002298 numMatches,
2299 matches);
2300}
2301
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002302/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002303 * Function given to ExpandGeneric() to obtain the possible arguments of the
2304 * fileformat options.
2305 */
2306 char_u *
2307get_fileformat_name(expand_T *xp UNUSED, int idx)
2308{
2309 if (idx >= (int)ARRAY_LENGTH(p_ff_values))
2310 return NULL;
2311
2312 return (char_u*)p_ff_values[idx];
2313}
2314
2315/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002316 * The 'fileformats' option is changed.
2317 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002318 char *
2319did_set_fileformats(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002320{
2321 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
2322 return e_invalid_argument;
2323
2324 // also change 'textauto'
2325 if (*p_ffs == NUL)
2326 p_ta = FALSE;
2327 else
2328 p_ta = TRUE;
2329
2330 return NULL;
2331}
2332
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002333/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002334 * The 'filetype' or the 'syntax' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002335 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002336 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002337did_set_filetype_or_syntax(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002338{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002339 char_u **varp = (char_u **)args->os_varp;
2340
2341 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002342 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002343
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002344 args->os_value_changed = STRCMP(args->os_oldval.string, *varp) != 0;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002345
2346 // Since we check the value, there is no need to set P_INSECURE,
2347 // even when the value comes from a modeline.
2348 args->os_value_checked = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002349
2350 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002351}
2352
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002353#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002354/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002355 * The 'foldclose' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002356 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002357 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002358did_set_foldclose(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002359{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002360 return did_set_opt_strings(p_fcl, p_fcl_values, TRUE);
2361}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002362
2363 int
2364expand_set_foldclose(optexpand_T *args, int *numMatches, char_u ***matches)
2365{
2366 return expand_set_opt_string(
2367 args,
2368 p_fcl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002369 ARRAY_LENGTH(p_fcl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002370 numMatches,
2371 matches);
2372}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002373#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002374
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002375#if (defined(FEAT_EVAL) && defined(FEAT_FOLDING)) || defined(PROTO)
2376/*
2377 * The 'foldexpr' option is changed.
2378 */
2379 char *
2380did_set_foldexpr(optset_T *args)
2381{
2382 (void)did_set_optexpr(args);
2383 if (foldmethodIsExpr(curwin))
2384 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002385 return NULL;
2386}
2387#endif
2388
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002389#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002390/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002391 * The 'foldignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002392 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002393 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002394did_set_foldignore(optset_T *args UNUSED)
2395{
2396 if (foldmethodIsIndent(curwin))
2397 foldUpdateAll(curwin);
2398 return NULL;
2399}
2400
2401/*
2402 * The 'foldmarker' option is changed.
2403 */
2404 char *
2405did_set_foldmarker(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002406{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002407 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002408 char_u *p;
2409
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002410 p = vim_strchr(*varp, ',');
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002411 if (p == NULL)
2412 return e_comma_required;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002413 else if (p == *varp || p[1] == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002414 return e_invalid_argument;
2415 else if (foldmethodIsMarker(curwin))
2416 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002417
2418 return NULL;
2419}
2420
2421/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002422 * The 'foldmethod' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002423 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002424 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002425did_set_foldmethod(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002426{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002427 char_u **varp = (char_u **)args->os_varp;
2428
Milly142cad12024-10-22 22:11:51 +02002429 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK || **varp == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002430 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002431
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002432 foldUpdateAll(curwin);
2433 if (foldmethodIsDiff(curwin))
2434 newFoldLevel();
2435 return NULL;
2436}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002437
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002438 int
2439expand_set_foldmethod(optexpand_T *args, int *numMatches, char_u ***matches)
2440{
2441 return expand_set_opt_string(
2442 args,
2443 p_fdm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002444 ARRAY_LENGTH(p_fdm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002445 numMatches,
2446 matches);
2447}
2448
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002449/*
2450 * The 'foldopen' option is changed.
2451 */
2452 char *
2453did_set_foldopen(optset_T *args UNUSED)
2454{
2455 return did_set_opt_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
2456}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002457
2458 int
2459expand_set_foldopen(optexpand_T *args, int *numMatches, char_u ***matches)
2460{
2461 return expand_set_opt_string(
2462 args,
2463 p_fdo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002464 ARRAY_LENGTH(p_fdo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002465 numMatches,
2466 matches);
2467}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002468#endif
2469
2470/*
2471 * The 'formatoptions' option is changed.
2472 */
2473 char *
2474did_set_formatoptions(optset_T *args)
2475{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002476 char_u **varp = (char_u **)args->os_varp;
2477
Christian Brabandtb39b2402023-11-29 11:34:05 +01002478 return did_set_option_listflag(*varp, (char_u *)FO_ALL, args->os_errbuf,
2479 args->os_errbuflen);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002480}
2481
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002482 int
2483expand_set_formatoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2484{
2485 return expand_set_opt_listflag(args, (char_u*)FO_ALL, numMatches, matches);
2486}
2487
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002488#if defined(CURSOR_SHAPE) || defined(PROTO)
2489/*
2490 * The 'guicursor' option is changed.
2491 */
2492 char *
2493did_set_guicursor(optset_T *args UNUSED)
2494{
2495 return parse_shape_opt(SHAPE_CURSOR);
2496}
2497#endif
2498
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002499#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002500/*
2501 * The 'guifont' option is changed.
2502 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002503 char *
2504did_set_guifont(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002505{
2506 char_u *p;
2507 char *errmsg = NULL;
2508
2509 if (gui.in_use)
2510 {
2511 p = p_guifont;
2512# if defined(FEAT_GUI_GTK)
2513 // Put up a font dialog and let the user select a new value.
2514 // If this is cancelled go back to the old value but don't
2515 // give an error message.
2516 if (STRCMP(p, "*") == 0)
2517 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002518 p = gui_mch_font_dialog(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002519 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002520 p_guifont = (p != NULL) ? p : vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002521 }
2522# endif
2523 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
2524 {
2525# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
2526 if (STRCMP(p_guifont, "*") == 0)
2527 {
2528 // Dialog was cancelled: Keep the old value without giving
2529 // an error message.
2530 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002531 p_guifont = vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002532 }
2533 else
2534# endif
2535 errmsg = e_invalid_fonts;
2536 }
2537 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002538
2539 return errmsg;
2540}
2541
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002542/*
2543 * Expand the 'guifont' option. Only when GUI is being used. Each platform has
2544 * specific behaviors.
2545 */
2546 int
2547expand_set_guifont(optexpand_T *args, int *numMatches, char_u ***matches)
2548{
2549 if (!gui.in_use)
2550 return FAIL;
2551
2552# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
2553 char_u **varp = (char_u **)args->oe_varp;
2554 int wide = (varp == &p_guifontwide);
2555
2556 return expand_set_opt_callback(
2557 args, gui_mch_expand_font, &wide, numMatches, matches);
2558# else
2559 return FAIL;
2560# endif
2561}
2562
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002563# if defined(FEAT_XFONTSET) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002564/*
2565 * The 'guifontset' option is changed.
2566 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002567 char *
2568did_set_guifontset(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002569{
2570 char *errmsg = NULL;
2571
2572 if (STRCMP(p_guifontset, "*") == 0)
2573 errmsg = e_cant_select_fontset;
2574 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
2575 errmsg = e_invalid_fontset;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002576
2577 return errmsg;
2578}
2579# endif
2580
2581/*
2582 * The 'guifontwide' option is changed.
2583 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002584 char *
2585did_set_guifontwide(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002586{
2587 char *errmsg = NULL;
2588
2589 if (STRCMP(p_guifontwide, "*") == 0)
2590 errmsg = e_cant_select_wide_font;
2591 else if (gui_get_wide_font() == FAIL)
2592 errmsg = e_invalid_wide_font;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002593
2594 return errmsg;
2595}
2596#endif
2597
Erik S. V. Jansson8b1e7492024-02-24 14:26:52 +01002598#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002599/*
2600 * The 'guiligatures' option is changed.
2601 */
2602 char *
2603did_set_guiligatures(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002604{
2605 gui_set_ligatures();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002606 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002607}
2608#endif
2609
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002610#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002611/*
2612 * The 'guioptions' option is changed.
2613 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002614 char *
2615did_set_guioptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002616{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002617 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002618 char *errmsg;
2619
Christian Brabandtb39b2402023-11-29 11:34:05 +01002620 errmsg = did_set_option_listflag(*varp, (char_u *)GO_ALL, args->os_errbuf,
2621 args->os_errbuflen);
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002622 if (errmsg != NULL)
2623 return errmsg;
2624
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002625 gui_init_which_components(args->os_oldval.string);
2626 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002627}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002628
2629 int
2630expand_set_guioptions(optexpand_T *args, int *numMatches, char_u ***matches)
2631{
2632 return expand_set_opt_listflag(args, (char_u*)GO_ALL, numMatches, matches);
2633}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002634#endif
2635
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002636#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002637/*
2638 * The 'guitablabel' option is changed.
2639 */
2640 char *
2641did_set_guitablabel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002642{
2643 redraw_tabline = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002644 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002645}
2646#endif
2647
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002648/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002649 * The 'helpfile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002650 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002651 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002652did_set_helpfile(optset_T *args UNUSED)
2653{
2654 // May compute new values for $VIM and $VIMRUNTIME
2655 if (didset_vim)
2656 vim_unsetenv_ext((char_u *)"VIM");
2657 if (didset_vimruntime)
2658 vim_unsetenv_ext((char_u *)"VIMRUNTIME");
2659 return NULL;
2660}
2661
2662#if defined(FEAT_MULTI_LANG) || defined(PROTO)
2663/*
2664 * The 'helplang' option is changed.
2665 */
2666 char *
2667did_set_helplang(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002668{
2669 char *errmsg = NULL;
2670
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002671 // Check for "", "ab", "ab,cd", etc.
2672 for (char_u *s = p_hlg; *s != NUL; s += 3)
2673 {
2674 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
2675 {
2676 errmsg = e_invalid_argument;
2677 break;
2678 }
2679 if (s[2] == NUL)
2680 break;
2681 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002682
2683 return errmsg;
2684}
2685#endif
2686
2687/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002688 * The 'highlight' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002689 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002690 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002691did_set_highlight(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002692{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002693 if (highlight_changed() == FAIL)
2694 return e_invalid_argument; // invalid flags
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002695
2696 return NULL;
2697}
2698
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002699/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002700 * Expand 'highlight' option.
2701 */
2702 int
2703expand_set_highlight(optexpand_T *args, int *numMatches, char_u ***matches)
2704{
2705 char_u *p;
2706 expand_T *xp = args->oe_xp;
2707 static char_u hl_flags[HLF_COUNT] = HL_FLAGS;
Christian Brabandt3f168ec2023-10-02 23:21:11 +02002708 size_t i;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002709 int count = 0;
2710
2711 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2712 {
2713 // Right after a ':', meaning we just return all highlight names.
2714 return expand_set_opt_generic(
2715 args,
2716 get_highlight_name,
2717 numMatches,
2718 matches);
2719 }
2720
2721 if (*xp->xp_pattern == NUL)
2722 {
2723 // At beginning of a comma-separated list. Return the specific list of
2724 // supported occasions.
2725 *matches = ALLOC_MULT(char_u *, HLF_COUNT + 1);
2726 if (*matches == NULL)
2727 return FAIL;
2728
2729 // We still want to return the full option if it's requested.
2730 if (args->oe_include_orig_val)
2731 {
2732 p = vim_strsave(args->oe_opt_value);
2733 if (p == NULL)
2734 {
2735 VIM_CLEAR(*matches);
2736 return FAIL;
2737 }
2738 (*matches)[count++] = p;
2739 }
2740
2741 for (i = 0; i < HLF_COUNT; i++)
2742 {
2743 p = vim_strnsave(&hl_flags[i], 1);
2744 if (p == NULL)
2745 {
2746 if (count == 0)
2747 {
2748 VIM_CLEAR(*matches);
2749 return FAIL;
2750 }
2751 else
2752 break;
2753 }
2754 (*matches)[count++] = p;
2755 }
2756
2757 if (count == 0)
2758 {
2759 VIM_CLEAR(*matches);
2760 return FAIL;
2761 }
2762 *numMatches = count;
2763 return OK;
2764 }
2765
2766 // We are after the initial character (which indicates the occasion). We
2767 // already made sure we are not matching after a ':' above, so now we want
2768 // to match against display mode modifiers.
2769 // Since the xp_pattern starts from the beginning, we need to include it in
2770 // the returned match.
2771
2772 // Note: Keep this in sync with highlight_changed()
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002773 static char_u p_hl_mode_values[] =
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002774 {':', 'b', 'i', '-', 'n', 'r', 's', 'u', 'c', '2', 'd', '=', 't'};
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002775 size_t num_hl_modes = ARRAY_LENGTH(p_hl_mode_values);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002776
2777 *matches = ALLOC_MULT(char_u *, num_hl_modes);
2778 if (*matches == NULL)
2779 return FAIL;
2780
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002781 int pattern_len = xp->xp_pattern_len;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002782
2783 for (i = 0; i < num_hl_modes; i++)
2784 {
2785 // Don't allow duplicates as these are unique flags
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002786 char_u *dup = vim_strchr(xp->xp_pattern + 1, p_hl_mode_values[i]);
2787 if (dup != NULL && (int)(dup - xp->xp_pattern) < pattern_len)
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002788 continue;
2789
2790 // ':' only works by itself, not with other flags.
2791 if (pattern_len > 1 && p_hl_mode_values[i] == ':')
2792 continue;
2793
2794 p = vim_strnsave(xp->xp_pattern, pattern_len + 1);
2795 if (p == NULL)
2796 {
2797 if (i == 0)
2798 {
2799 VIM_CLEAR(*matches);
2800 return FAIL;
2801 }
2802 else
2803 break;
2804 }
2805 p[pattern_len] = p_hl_mode_values[i];
2806 p[pattern_len + 1] = NUL;
2807 (*matches)[count++] = p;
2808 }
2809 if (count == 0)
2810 {
2811 VIM_CLEAR(*matches);
2812 return FAIL;
2813 }
2814 *numMatches = count;
2815
2816 return OK;
2817}
2818
2819/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002820 * The 'titlestring' or the 'iconstring' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002821 */
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002822 static char *
2823parse_titleiconstring(optset_T *args UNUSED, int flagval UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002824{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002825#ifdef FEAT_STL_OPT
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002826 char_u **varp = (char_u **)args->os_varp;
2827
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002828 // NULL => statusline syntax
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002829 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002830 stl_syntax |= flagval;
2831 else
2832 stl_syntax &= ~flagval;
2833#endif
2834 did_set_title();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002835
2836 return NULL;
2837}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002838
2839/*
2840 * The 'iconstring' option is changed.
2841 */
2842 char *
2843did_set_iconstring(optset_T *args)
2844{
2845 int flagval = 0;
2846
2847#ifdef FEAT_STL_OPT
2848 flagval = STL_IN_ICON;
2849#endif
2850
2851 return parse_titleiconstring(args, flagval);
2852}
2853
2854#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
2855/*
2856 * The 'imactivatekey' option is changed.
2857 */
2858 char *
2859did_set_imactivatekey(optset_T *args UNUSED)
2860{
2861 if (!im_xim_isvalid_imactivate())
2862 return e_invalid_argument;
2863 return NULL;
2864}
2865#endif
2866
2867/*
Milly5e7a6a42024-10-22 22:27:19 +02002868 * The 'iskeyword' option is changed.
2869 */
2870 char *
2871did_set_iskeyword(optset_T *args)
2872{
2873 char_u **varp = (char_u **)args->os_varp;
2874
2875 if (varp == &p_isk) // only check for global-value
2876 {
2877 if (check_isopt(*varp) == FAIL)
2878 return e_invalid_argument;
2879 }
2880 else // fallthrough for local-value
2881 return did_set_isopt(args);
2882
2883 return NULL;
2884}
2885
2886/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002887 * The 'isident' or the 'iskeyword' or the 'isprint' or the 'isfname' option is
2888 * changed.
2889 */
2890 char *
2891did_set_isopt(optset_T *args)
2892{
Milly5e7a6a42024-10-22 22:27:19 +02002893 // 'isident', 'iskeyword', 'isprint' or 'isfname' option: refill g_chartab[]
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002894 // If the new option is invalid, use old value.
2895 // 'lisp' option: refill g_chartab[] for '-' char.
2896 if (init_chartab() == FAIL)
2897 {
2898 args->os_restore_chartab = TRUE;// need to restore the chartab.
2899 return e_invalid_argument; // error in value
2900 }
2901
2902 return NULL;
2903}
2904
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002905/*
2906 * The 'jumpoptions' option is changed.
2907 */
2908 char *
Yegappan Lakshmanan1926ae42023-09-21 16:36:28 +02002909did_set_jumpoptions(optset_T *args UNUSED)
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002910{
2911 if (opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE) != OK)
2912 return e_invalid_argument;
2913
2914 return NULL;
2915}
2916
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002917 int
2918expand_set_jumpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2919{
2920 return expand_set_opt_string(
2921 args,
2922 p_jop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002923 ARRAY_LENGTH(p_jop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002924 numMatches,
2925 matches);
2926}
2927
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002928#if defined(FEAT_KEYMAP) || defined(PROTO)
2929/*
2930 * The 'keymap' option is changed.
2931 */
2932 char *
2933did_set_keymap(optset_T *args)
2934{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002935 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002936 char *errmsg = NULL;
2937
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002938 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002939 errmsg = e_invalid_argument;
2940 else
2941 {
2942 int secure_save = secure;
2943
2944 // Reset the secure flag, since the value of 'keymap' has
2945 // been checked to be safe.
2946 secure = 0;
2947
2948 // load or unload key mapping tables
2949 errmsg = keymap_init();
2950
2951 secure = secure_save;
2952
2953 // Since we check the value, there is no need to set P_INSECURE,
2954 // even when the value comes from a modeline.
2955 args->os_value_checked = TRUE;
2956 }
2957
2958 if (errmsg == NULL)
2959 {
2960 if (*curbuf->b_p_keymap != NUL)
2961 {
2962 // Installed a new keymap, switch on using it.
2963 curbuf->b_p_iminsert = B_IMODE_LMAP;
2964 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
2965 curbuf->b_p_imsearch = B_IMODE_LMAP;
2966 }
2967 else
2968 {
2969 // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
2970 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
2971 curbuf->b_p_iminsert = B_IMODE_NONE;
2972 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
2973 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
2974 }
2975 if ((args->os_flags & OPT_LOCAL) == 0)
2976 {
2977 set_iminsert_global();
2978 set_imsearch_global();
2979 }
2980 status_redraw_curbuf();
2981 }
2982
2983 return errmsg;
2984}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002985#endif
2986
2987/*
2988 * The 'keymodel' option is changed.
2989 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002990 char *
2991did_set_keymodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002992{
2993 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
2994 return e_invalid_argument;
2995
2996 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
2997 km_startsel = (vim_strchr(p_km, 'a') != NULL);
2998 return NULL;
2999}
3000
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003001 int
3002expand_set_keymodel(optexpand_T *args, int *numMatches, char_u ***matches)
3003{
3004 return expand_set_opt_string(
3005 args,
3006 p_km_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003007 ARRAY_LENGTH(p_km_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003008 numMatches,
3009 matches);
3010}
3011
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003012/*
3013 * The 'keyprotocol' option is changed.
3014 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003015 char *
3016did_set_keyprotocol(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003017{
Gregory Anders3695d0e2023-09-29 20:17:20 +02003018 char_u *term = T_NAME;
3019 keyprot_T kpc = match_keyprotocol(term);
3020 if (kpc == KEYPROTOCOL_FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003021 return e_invalid_argument;
3022
Gregory Anders3695d0e2023-09-29 20:17:20 +02003023 apply_keyprotocol(term, kpc);
3024
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003025 return NULL;
3026}
3027
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003028 int
3029expand_set_keyprotocol(optexpand_T *args, int *numMatches, char_u ***matches)
3030{
3031 expand_T *xp = args->oe_xp;
3032 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3033 {
3034 // 'keyprotocol' only has well-defined terms for completion for the
3035 // protocol part after the colon.
3036 return expand_set_opt_string(
3037 args,
3038 p_kpc_protocol_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003039 ARRAY_LENGTH(p_kpc_protocol_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003040 numMatches,
3041 matches);
3042 }
3043 // Use expand_set_opt_string instead of returning FAIL so that we can
3044 // include the original value if args->oe_include_orig_val is set.
3045 static char *(empty[]) = {NULL};
3046 return expand_set_opt_string(args, empty, 0, numMatches, matches);
3047}
3048
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003049/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003050 * The 'lispoptions' option is changed.
3051 */
3052 char *
3053did_set_lispoptions(optset_T *args)
3054{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003055 char_u **varp = (char_u **)args->os_varp;
3056
3057 if (**varp != NUL
3058 && STRCMP(*varp, "expr:0") != 0
3059 && STRCMP(*varp, "expr:1") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003060 return e_invalid_argument;
3061
3062 return NULL;
3063}
3064
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003065 int
3066expand_set_lispoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3067{
3068 static char *(p_lop_values[]) = {"expr:0", "expr:1", NULL};
3069 return expand_set_opt_string(
3070 args,
3071 p_lop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003072 ARRAY_LENGTH(p_lop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003073 numMatches,
3074 matches);
3075}
3076
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003077/*
3078 * The 'matchpairs' option is changed.
3079 */
3080 char *
3081did_set_matchpairs(optset_T *args)
3082{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003083 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003084 char_u *p;
3085
3086 if (has_mbyte)
3087 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003088 for (p = *varp; *p != NUL; ++p)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003089 {
3090 int x2 = -1;
3091 int x3 = -1;
3092
3093 p += mb_ptr2len(p);
3094 if (*p != NUL)
3095 x2 = *p++;
3096 if (*p != NUL)
3097 {
3098 x3 = mb_ptr2char(p);
3099 p += mb_ptr2len(p);
3100 }
3101 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
3102 return e_invalid_argument;
3103 if (*p == NUL)
3104 break;
3105 }
3106 }
3107 else
3108 {
3109 // Check for "x:y,x:y"
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003110 for (p = *varp; *p != NUL; p += 4)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003111 {
3112 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
3113 return e_invalid_argument;
3114 if (p[3] == NUL)
3115 break;
3116 }
3117 }
3118
3119 return NULL;
3120}
3121
Christian Brabandt51d4d842024-12-06 17:26:25 +01003122/*
3123 * Process the updated 'messagesopt' option value.
3124 */
3125 char *
3126did_set_messagesopt(optset_T *args UNUSED)
3127{
3128 if (messagesopt_changed() == FAIL)
3129 return e_invalid_argument;
3130
3131 return NULL;
3132}
3133
3134 int
3135expand_set_messagesopt(optexpand_T *args, int *numMatches, char_u ***matches)
3136{
zeertzjq8cc43da2024-12-07 16:09:08 +01003137 static char *(p_mopt_values[]) = {"hit-enter", "wait:", "history:", NULL};
Christian Brabandt51d4d842024-12-06 17:26:25 +01003138 return expand_set_opt_string(
3139 args,
zeertzjq8cc43da2024-12-07 16:09:08 +01003140 p_mopt_values,
3141 ARRAY_LENGTH(p_mopt_values) - 1,
Christian Brabandt51d4d842024-12-06 17:26:25 +01003142 numMatches,
3143 matches);
3144}
3145
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003146#if defined(FEAT_SPELL) || defined(PROTO)
3147/*
3148 * The 'mkspellmem' option is changed.
3149 */
3150 char *
3151did_set_mkspellmem(optset_T *args UNUSED)
3152{
3153 if (spell_check_msm() != OK)
3154 return e_invalid_argument;
3155
3156 return NULL;
3157}
3158#endif
3159
3160/*
3161 * The 'mouse' option is changed.
3162 */
3163 char *
3164did_set_mouse(optset_T *args)
3165{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003166 char_u **varp = (char_u **)args->os_varp;
3167
Christian Brabandtb39b2402023-11-29 11:34:05 +01003168 return did_set_option_listflag(*varp, (char_u *)MOUSE_ALL, args->os_errbuf,
3169 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003170}
3171
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003172 int
3173expand_set_mouse(optexpand_T *args, int *numMatches, char_u ***matches)
3174{
3175 return expand_set_opt_listflag(args, (char_u*)MOUSE_ALL, numMatches, matches);
3176}
3177
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003178/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003179 * The 'mousemodel' option is changed.
3180 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003181 char *
3182did_set_mousemodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003183{
3184 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
3185 return e_invalid_argument;
3186#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
Ken Takata18d0d292023-12-19 20:12:29 +01003187 else if (*p_mousem != *args->os_oldval.string)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003188 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
3189 // to create or delete the popup menus.
3190 gui_motif_update_mousemodel(root_menu);
3191#endif
3192
3193 return NULL;
3194}
3195
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003196 int
3197expand_set_mousemodel(optexpand_T *args, int *numMatches, char_u ***matches)
3198{
3199 return expand_set_opt_string(
3200 args,
3201 p_mousem_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003202 ARRAY_LENGTH(p_mousem_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003203 numMatches,
3204 matches);
3205}
3206
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003207#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3208 char *
3209did_set_mouseshape(optset_T *args UNUSED)
3210{
3211 char *errmsg = NULL;
3212
3213 errmsg = parse_shape_opt(SHAPE_MOUSE);
3214 update_mouseshape(-1);
3215
3216 return errmsg;
3217}
3218#endif
3219
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003220/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003221 * The 'nrformats' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003222 */
3223 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003224did_set_nrformats(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003225{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003226 char_u **varp = (char_u **)args->os_varp;
3227
3228 return did_set_opt_strings(*varp, p_nf_values, TRUE);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003229}
3230
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003231 int
3232expand_set_nrformats(optexpand_T *args, int *numMatches, char_u ***matches)
3233{
3234 return expand_set_opt_string(
3235 args,
3236 p_nf_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003237 ARRAY_LENGTH(p_nf_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003238 numMatches,
3239 matches);
3240}
3241
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003242#if defined(FEAT_EVAL) || defined(PROTO)
3243/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003244 * One of the '*expr' options is changed: 'balloonexpr', 'diffexpr',
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003245 * 'foldexpr', 'foldtext', 'formatexpr', 'includeexpr', 'indentexpr',
3246 * 'patchexpr', 'printexpr' and 'charconvert'.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003247 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003248 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003249did_set_optexpr(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003250{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003251 char_u **varp = (char_u **)args->os_varp;
3252
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003253 // If the option value starts with <SID> or s:, then replace that with
3254 // the script identifier.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003255 char_u *name = get_scriptlocal_funcname(*varp);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003256 if (name != NULL)
3257 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003258 free_string_option(*varp);
3259 *varp = name;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003260 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003261
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003262 return NULL;
3263}
3264#endif
3265
3266/*
3267 * The 'pastetoggle' option is changed.
3268 */
3269 char *
3270did_set_pastetoggle(optset_T *args UNUSED)
3271{
3272 char_u *p;
3273
3274 // translate key codes like in a mapping
3275 if (*p_pt)
3276 {
zeertzjq7e0bae02023-08-11 23:15:38 +02003277 (void)replace_termcodes(p_pt, &p, 0,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003278 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
3279 if (p != NULL)
3280 {
3281 free_string_option(p_pt);
3282 p_pt = p;
3283 }
3284 }
3285
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003286 return NULL;
3287}
3288
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003289#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3290/*
3291 * The 'previewpopup' option is changed.
3292 */
3293 char *
3294did_set_previewpopup(optset_T *args UNUSED)
3295{
3296 if (parse_previewpopup(NULL) == FAIL)
3297 return e_invalid_argument;
3298
3299 return NULL;
3300}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003301
3302 int
3303expand_set_popupoption(optexpand_T *args, int *numMatches, char_u ***matches)
3304{
3305 expand_T *xp = args->oe_xp;
3306
3307 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3308 {
3309 // Within "highlight:"/"border:"/"align:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003310 int border_len = (int)STRLEN("border:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003311 if (xp->xp_pattern - args->oe_set_arg >= border_len &&
3312 STRNCMP(xp->xp_pattern - border_len, "border:", border_len) == 0)
3313 {
3314 return expand_set_opt_string(
3315 args,
3316 p_popup_option_border_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003317 ARRAY_LENGTH(p_popup_option_border_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003318 numMatches,
3319 matches);
3320 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003321 int align_len = (int)STRLEN("align:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003322 if (xp->xp_pattern - args->oe_set_arg >= align_len &&
3323 STRNCMP(xp->xp_pattern - align_len, "align:", align_len) == 0)
3324 {
3325 return expand_set_opt_string(
3326 args,
3327 p_popup_option_align_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003328 ARRAY_LENGTH(p_popup_option_align_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003329 numMatches,
3330 matches);
3331 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003332 int highlight_len = (int)STRLEN("highlight:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003333 if (xp->xp_pattern - args->oe_set_arg >= highlight_len &&
3334 STRNCMP(xp->xp_pattern - highlight_len, "highlight:", highlight_len) == 0)
3335 {
3336 // Return the list of all highlight names
3337 return expand_set_opt_generic(
3338 args,
3339 get_highlight_name,
3340 numMatches,
3341 matches);
3342 }
3343 return FAIL;
3344 }
3345
3346 return expand_set_opt_string(
3347 args,
3348 p_popup_option_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003349 ARRAY_LENGTH(p_popup_option_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003350 numMatches,
3351 matches);
3352}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003353#endif
3354
3355#if defined(FEAT_POSTSCRIPT) || defined(PROTO)
3356/*
3357 * The 'printencoding' option is changed.
3358 */
3359 char *
3360did_set_printencoding(optset_T *args UNUSED)
3361{
3362 char_u *s, *p;
3363
3364 // Canonize 'printencoding' if VIM standard one
3365 p = enc_canonize(p_penc);
3366 if (p != NULL)
3367 {
3368 vim_free(p_penc);
3369 p_penc = p;
3370 }
3371 else
3372 {
3373 // Ensure lower case and '-' for '_'
3374 for (s = p_penc; *s != NUL; s++)
3375 {
3376 if (*s == '_')
3377 *s = '-';
3378 else
3379 *s = TOLOWER_ASC(*s);
3380 }
3381 }
3382
3383 return NULL;
3384}
3385#endif
3386
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003387#if defined(FEAT_PRINTER) || defined(PROTO)
3388
3389 static char_u *
3390get_printoptions_names(expand_T *xp UNUSED, int idx)
3391{
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003392 if (idx >= (int)ARRAY_LENGTH(printer_opts))
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003393 return NULL;
3394 return (char_u*)printer_opts[idx].name;
3395}
3396
3397/*
3398 * Expand 'printoptions' option
3399 */
3400 int
3401expand_set_printoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3402{
3403 return expand_set_opt_generic(
3404 args,
3405 get_printoptions_names,
3406 numMatches,
3407 matches);
3408}
3409#endif
3410
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003411#if defined(FEAT_STL_OPT) || defined(PROTO)
3412/*
3413 * The 'statusline' or the 'tabline' or the 'rulerformat' option is changed.
3414 * "rulerformat" is TRUE if the 'rulerformat' option is changed.
3415 */
3416 static char *
3417parse_statustabline_rulerformat(optset_T *args, int rulerformat)
3418{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003419 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003420 char_u *s;
3421 char *errmsg = NULL;
3422 int wid;
3423
3424 if (rulerformat) // reset ru_wid first
3425 ru_wid = 0;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003426 s = *varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003427 if (rulerformat && *s == '%')
3428 {
3429 // set ru_wid if 'ruf' starts with "%99("
3430 if (*++s == '-') // ignore a '-'
3431 s++;
3432 wid = getdigits(&s);
3433 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
3434 ru_wid = wid;
3435 else
Yegappan Lakshmananac023e82024-11-27 20:55:45 +01003436 {
3437 // Validate the flags in 'rulerformat' only if it doesn't point to
3438 // a custom function ("%!" flag).
3439 if ((*varp)[1] != '!')
3440 errmsg = check_stl_option(p_ruf);
3441 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003442 }
3443 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
3444 else if (rulerformat || s[0] != '%' || s[1] != '!')
3445 errmsg = check_stl_option(s);
3446 if (rulerformat && errmsg == NULL)
3447 comp_col();
3448
3449 return errmsg;
3450}
3451#endif
3452
3453#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
3454/*
3455 * The 'renderoptions' option is changed.
3456 */
3457 char *
3458did_set_renderoptions(optset_T *args UNUSED)
3459{
3460 if (!gui_mch_set_rendering_options(p_rop))
3461 return e_invalid_argument;
3462
3463 return NULL;
3464}
3465#endif
3466
3467#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3468/*
3469 * The 'rightleftcmd' option is changed.
3470 */
3471 char *
3472did_set_rightleftcmd(optset_T *args)
3473{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003474 char_u **varp = (char_u **)args->os_varp;
3475
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003476 // Currently only "search" is a supported value.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003477 if (**varp != NUL && STRCMP(*varp, "search") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003478 return e_invalid_argument;
3479
3480 return NULL;
3481}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003482
3483 int
3484expand_set_rightleftcmd(optexpand_T *args, int *numMatches, char_u ***matches)
3485{
3486 static char *(p_rlc_values[]) = {"search", NULL};
3487 return expand_set_opt_string(
3488 args,
3489 p_rlc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003490 ARRAY_LENGTH(p_rlc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003491 numMatches,
3492 matches);
3493}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003494#endif
3495
3496#if defined(FEAT_STL_OPT) || defined(PROTO)
3497/*
3498 * The 'rulerformat' option is changed.
3499 */
3500 char *
3501did_set_rulerformat(optset_T *args)
3502{
3503 return parse_statustabline_rulerformat(args, TRUE);
3504}
3505#endif
3506
3507/*
3508 * The 'scrollopt' option is changed.
3509 */
3510 char *
3511did_set_scrollopt(optset_T *args UNUSED)
3512{
3513 return did_set_opt_strings(p_sbo, p_scbopt_values, TRUE);
3514}
3515
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003516 int
3517expand_set_scrollopt(optexpand_T *args, int *numMatches, char_u ***matches)
3518{
3519 return expand_set_opt_string(
3520 args,
3521 p_scbopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003522 ARRAY_LENGTH(p_scbopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003523 numMatches,
3524 matches);
3525}
3526
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003527/*
3528 * The 'selection' option is changed.
3529 */
3530 char *
3531did_set_selection(optset_T *args UNUSED)
3532{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003533 if (*p_sel == NUL || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003534 return e_invalid_argument;
3535
3536 return NULL;
3537}
3538
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003539 int
3540expand_set_selection(optexpand_T *args, int *numMatches, char_u ***matches)
3541{
3542 return expand_set_opt_string(
3543 args,
3544 p_sel_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003545 ARRAY_LENGTH(p_sel_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003546 numMatches,
3547 matches);
3548}
3549
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003550/*
3551 * The 'selectmode' option is changed.
3552 */
3553 char *
3554did_set_selectmode(optset_T *args UNUSED)
3555{
3556 return did_set_opt_strings(p_slm, p_slm_values, TRUE);
3557}
3558
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003559 int
3560expand_set_selectmode(optexpand_T *args, int *numMatches, char_u ***matches)
3561{
3562 return expand_set_opt_string(
3563 args,
3564 p_slm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003565 ARRAY_LENGTH(p_slm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003566 numMatches,
3567 matches);
3568}
3569
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003570#if defined(FEAT_SESSION) || defined(PROTO)
3571/*
3572 * The 'sessionoptions' option is changed.
3573 */
3574 char *
3575did_set_sessionoptions(optset_T *args)
3576{
3577 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
3578 return e_invalid_argument;
3579 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
3580 {
3581 // Don't allow both "sesdir" and "curdir".
3582 (void)opt_strings_flags(args->os_oldval.string, p_ssop_values,
3583 &ssop_flags, TRUE);
3584 return e_invalid_argument;
3585 }
3586
3587 return NULL;
3588}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003589
3590 int
3591expand_set_sessionoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3592{
3593 return expand_set_opt_string(
3594 args,
3595 p_ssop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003596 ARRAY_LENGTH(p_ssop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003597 numMatches,
3598 matches);
3599}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003600#endif
3601
3602/*
3603 * The 'shortmess' option is changed.
3604 */
3605 char *
3606did_set_shortmess(optset_T *args)
3607{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003608 char_u **varp = (char_u **)args->os_varp;
3609
Christian Brabandtb39b2402023-11-29 11:34:05 +01003610 return did_set_option_listflag(*varp, (char_u *)SHM_ALL, args->os_errbuf,
3611 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003612}
3613
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003614 int
3615expand_set_shortmess(optexpand_T *args, int *numMatches, char_u ***matches)
3616{
3617 return expand_set_opt_listflag(args, (char_u*)SHM_ALL, numMatches, matches);
3618}
3619
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003620#if defined(FEAT_LINEBREAK) || defined(PROTO)
3621/*
3622 * The 'showbreak' option is changed.
3623 */
3624 char *
3625did_set_showbreak(optset_T *args)
3626{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003627 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003628 char_u *s;
3629
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003630 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003631 {
3632 if (ptr2cells(s) != 1)
3633 return e_showbreak_contains_unprintable_or_wide_character;
3634 MB_PTR_ADV(s);
3635 }
3636
3637 return NULL;
3638}
3639#endif
3640
3641/*
3642 * The 'showcmdloc' option is changed.
3643 */
3644 char *
3645did_set_showcmdloc(optset_T *args UNUSED)
3646{
zeertzjqc27fcf42024-03-01 23:01:43 +01003647 char *errmsg = did_set_opt_strings(p_sloc, p_sloc_values, FALSE);
3648
3649 if (errmsg == NULL)
3650 comp_col();
3651
3652 return errmsg;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003653}
3654
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003655 int
3656expand_set_showcmdloc(optexpand_T *args, int *numMatches, char_u ***matches)
3657{
3658 return expand_set_opt_string(
3659 args,
3660 p_sloc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003661 ARRAY_LENGTH(p_sloc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003662 numMatches,
3663 matches);
3664}
3665
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003666#if defined(FEAT_SIGNS) || defined(PROTO)
3667/*
3668 * The 'signcolumn' option is changed.
3669 */
3670 char *
3671did_set_signcolumn(optset_T *args)
3672{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003673 char_u **varp = (char_u **)args->os_varp;
3674
3675 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003676 return e_invalid_argument;
3677 // When changing the 'signcolumn' to or from 'number', recompute the
3678 // width of the number column if 'number' or 'relativenumber' is set.
3679 if (((*args->os_oldval.string == 'n' && args->os_oldval.string[1] == 'u')
3680 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
3681 && (curwin->w_p_nu || curwin->w_p_rnu))
3682 curwin->w_nrwidth_line_count = 0;
3683
3684 return NULL;
3685}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003686
3687 int
3688expand_set_signcolumn(optexpand_T *args, int *numMatches, char_u ***matches)
3689{
3690 return expand_set_opt_string(
3691 args,
3692 p_scl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003693 ARRAY_LENGTH(p_scl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003694 numMatches,
3695 matches);
3696}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003697#endif
3698
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003699#if defined(FEAT_SPELL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003700/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003701 * The 'spellcapcheck' option is changed.
3702 */
3703 char *
3704did_set_spellcapcheck(optset_T *args UNUSED)
3705{
3706 // compile the regexp program.
3707 return compile_cap_prog(curwin->w_s);
3708}
3709
3710/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003711 * The 'spellfile' option is changed.
3712 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003713 char *
3714did_set_spellfile(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003715{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003716 char_u **varp = (char_u **)args->os_varp;
3717
3718 if (!valid_spellfile(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003719 return e_invalid_argument;
3720
3721 // If there is a window for this buffer in which 'spell' is set load the
3722 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003723 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003724}
3725
3726/*
3727 * The 'spell' option is changed.
3728 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003729 char *
3730did_set_spelllang(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003731{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003732 char_u **varp = (char_u **)args->os_varp;
3733
3734 if (!valid_spelllang(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003735 return e_invalid_argument;
3736
3737 // If there is a window for this buffer in which 'spell' is set load the
3738 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003739 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003740}
3741
3742/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003743 * The 'spelloptions' option is changed.
3744 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003745 char *
3746did_set_spelloptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003747{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003748 char_u **varp = (char_u **)args->os_varp;
3749
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003750 if (**varp != NUL && STRCMP(*varp, "camel") != 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003751 return e_invalid_argument;
3752
3753 return NULL;
3754}
3755
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003756 int
3757expand_set_spelloptions(optexpand_T *args, int *numMatches, char_u ***matches)
3758{
3759 static char *(p_spo_values[]) = {"camel", NULL};
3760 return expand_set_opt_string(
3761 args,
3762 p_spo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003763 ARRAY_LENGTH(p_spo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003764 numMatches,
3765 matches);
3766}
3767
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003768/*
3769 * The 'spellsuggest' option is changed.
3770 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003771 char *
3772did_set_spellsuggest(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003773{
3774 if (spell_check_sps() != OK)
3775 return e_invalid_argument;
3776
3777 return NULL;
3778}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003779
3780 int
3781expand_set_spellsuggest(optexpand_T *args, int *numMatches, char_u ***matches)
3782{
3783 return expand_set_opt_string(
3784 args,
3785 p_sps_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003786 ARRAY_LENGTH(p_sps_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003787 numMatches,
3788 matches);
3789}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003790#endif
3791
3792/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003793 * The 'splitkeep' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003794 */
3795 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003796did_set_splitkeep(optset_T *args UNUSED)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003797{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003798 return did_set_opt_strings(p_spk, p_spk_values, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003799}
3800
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003801 int
3802expand_set_splitkeep(optexpand_T *args, int *numMatches, char_u ***matches)
3803{
3804 return expand_set_opt_string(
3805 args,
3806 p_spk_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003807 ARRAY_LENGTH(p_spk_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003808 numMatches,
3809 matches);
3810}
3811
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00003812#if defined(FEAT_STL_OPT) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003813/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003814 * The 'statusline' option is changed.
3815 */
3816 char *
3817did_set_statusline(optset_T *args)
3818{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003819 return parse_statustabline_rulerformat(args, FALSE);
3820}
3821#endif
3822
3823/*
3824 * The 'swapsync' option is changed.
3825 */
3826 char *
3827did_set_swapsync(optset_T *args UNUSED)
3828{
3829 return did_set_opt_strings(p_sws, p_sws_values, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003830}
3831
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003832 int
3833expand_set_swapsync(optexpand_T *args, int *numMatches, char_u ***matches)
3834{
3835 return expand_set_opt_string(
3836 args,
3837 p_sws_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003838 ARRAY_LENGTH(p_sws_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003839 numMatches,
3840 matches);
3841}
3842
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003843/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003844 * The 'switchbuf' option is changed.
3845 */
3846 char *
3847did_set_switchbuf(optset_T *args UNUSED)
3848{
3849 return did_set_opt_flags(p_swb, p_swb_values, &swb_flags, TRUE);
3850}
3851
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003852 int
3853expand_set_switchbuf(optexpand_T *args, int *numMatches, char_u ***matches)
3854{
3855 return expand_set_opt_string(
3856 args,
3857 p_swb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003858 ARRAY_LENGTH(p_swb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003859 numMatches,
3860 matches);
3861}
3862
LemonBoy5247b0b2024-07-12 19:30:58 +02003863/*
3864 * The 'tabclose' option is changed.
3865 */
3866 char *
3867did_set_tabclose(optset_T *args UNUSED)
3868{
3869 return did_set_opt_flags(p_tcl, p_tcl_values, &tcl_flags, TRUE);
3870}
3871
3872 int
3873expand_set_tabclose(optexpand_T *args, int *numMatches, char_u ***matches)
3874{
3875 return expand_set_opt_string(
3876 args,
3877 p_tcl_values,
3878 ARRAY_LENGTH(p_tcl_values) - 1,
3879 numMatches,
3880 matches);
3881}
3882
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003883#if defined(FEAT_STL_OPT) || defined(PROTO)
3884/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003885 * The 'tabline' option is changed.
3886 */
3887 char *
3888did_set_tabline(optset_T *args)
3889{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003890 return parse_statustabline_rulerformat(args, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003891}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003892#endif
3893
3894/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003895 * The 'tagcase' option is changed.
3896 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003897 char *
3898did_set_tagcase(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003899{
3900 unsigned int *flags;
3901 char_u *p;
3902
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003903 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003904 {
3905 p = curbuf->b_p_tc;
3906 flags = &curbuf->b_tc_flags;
3907 }
3908 else
3909 {
3910 p = p_tc;
3911 flags = &tc_flags;
3912 }
3913
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003914 if ((args->os_flags & OPT_LOCAL) && *p == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003915 // make the local value empty: use the global value
3916 *flags = 0;
3917 else if (*p == NUL
3918 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
3919 return e_invalid_argument;
3920
3921 return NULL;
3922}
3923
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003924 int
3925expand_set_tagcase(optexpand_T *args, int *numMatches, char_u ***matches)
3926{
3927 return expand_set_opt_string(
3928 args,
3929 p_tc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003930 ARRAY_LENGTH(p_tc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003931 numMatches,
3932 matches);
3933}
3934
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003935/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003936 * The 'term' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003937 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003938 char *
3939did_set_term(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003940{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003941 if (T_NAME[0] == NUL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003942 return e_cannot_set_term_to_empty_string;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003943#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003944 if (gui.in_use)
3945 return e_cannot_change_term_in_GUI;
3946 if (term_is_gui(T_NAME))
3947 return e_use_gui_to_start_GUI;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003948#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003949 if (set_termname(T_NAME) == FAIL)
3950 return e_not_found_in_termcap;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003951
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003952 // Screen colors may have changed.
3953 redraw_later_clear();
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003954
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003955 return NULL;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003956}
3957
3958/*
3959 * Some terminal option (t_xxx) is changed
3960 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003961 char *
3962did_set_term_option(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003963{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003964 char_u **varp = (char_u **)args->os_varp;
3965
3966 if (!full_screen)
3967 return NULL;
3968
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003969 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
3970 if (varp == &T_CCO)
3971 {
3972 int colors = atoi((char *)T_CCO);
3973
3974 // Only reinitialize colors if t_Co value has really changed to
3975 // avoid expensive reload of colorscheme if t_Co is set to the
3976 // same value multiple times.
3977 if (colors != t_colors)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003978 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003979 t_colors = colors;
3980 if (t_colors <= 1)
3981 {
3982 vim_free(T_CCO);
3983 T_CCO = empty_option;
3984 }
3985#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3986 if (is_term_win32())
3987 {
3988 swap_tcap();
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003989 args->os_did_swaptcap = TRUE;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003990 }
3991#endif
3992 // We now have a different color setup, initialize it again.
3993 init_highlight(TRUE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003994 }
3995 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003996 ttest(FALSE);
3997 if (varp == &T_ME)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003998 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003999 out_str(T_ME);
4000 redraw_later(UPD_CLEAR);
4001#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4002 // Since t_me has been set, this probably means that the user
4003 // wants to use this as default colors. Need to reset default
4004 // background/foreground colors.
4005# ifdef VIMDLL
4006 if (!gui.in_use && !gui.starting)
4007# endif
4008 mch_set_normal_colors();
4009#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004010 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004011 if (varp == &T_BE && termcap_active)
4012 {
4013 MAY_WANT_TO_LOG_THIS;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004014
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004015 if (*T_BE == NUL)
4016 // When clearing t_BE we assume the user no longer wants
4017 // bracketed paste, thus disable it by writing t_BD.
4018 out_str(T_BD);
4019 else
4020 out_str(T_BE);
4021 }
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004022
4023 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004024}
4025
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004026#if defined(FEAT_TERMINAL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004027/*
4028 * The 'termwinkey' option is changed.
4029 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004030 char *
Milly94606f72024-10-22 22:07:52 +02004031did_set_termwinkey(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004032{
Milly94606f72024-10-22 22:07:52 +02004033 char_u **varp = (char_u **)args->os_varp;
4034
4035 if ((*varp)[0] != NUL && string_to_key(*varp, TRUE) == 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004036 return e_invalid_argument;
4037
4038 return NULL;
4039}
4040
4041/*
4042 * The 'termwinsize' option is changed.
4043 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004044 char *
Milly8be10aa2024-10-22 22:01:46 +02004045did_set_termwinsize(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004046{
Milly8be10aa2024-10-22 22:01:46 +02004047 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004048 char_u *p;
4049
Milly8be10aa2024-10-22 22:01:46 +02004050 if ((*varp)[0] == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004051 return NULL;
4052
Milly8be10aa2024-10-22 22:01:46 +02004053 p = skipdigits(*varp);
4054 if (p == *varp || (*p != 'x' && *p != '*') || *skipdigits(p + 1) != NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004055 return e_invalid_argument;
4056
4057 return NULL;
4058}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00004059
4060# if defined(MSWIN) || defined(PROTO)
4061/*
4062 * The 'termwintype' option is changed.
4063 */
4064 char *
4065did_set_termwintype(optset_T *args UNUSED)
4066{
4067 return did_set_opt_strings(p_twt, p_twt_values, FALSE);
4068}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004069
4070 int
4071expand_set_termwintype(optexpand_T *args, int *numMatches, char_u ***matches)
4072{
4073 return expand_set_opt_string(
4074 args,
4075 p_twt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004076 ARRAY_LENGTH(p_twt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004077 numMatches,
4078 matches);
4079}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00004080# endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004081#endif
4082
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004083/*
4084 * The 'titlestring' option is changed.
4085 */
4086 char *
4087did_set_titlestring(optset_T *args)
4088{
4089 int flagval = 0;
4090
4091#ifdef FEAT_STL_OPT
4092 flagval = STL_IN_TITLE;
4093#endif
4094 return parse_titleiconstring(args, flagval);
4095}
4096
4097#if (defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)) || defined(PROTO)
4098/*
4099 * The 'toolbar' option is changed.
4100 */
4101 char *
4102did_set_toolbar(optset_T *args UNUSED)
4103{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004104 if (opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags,
4105 TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004106 return e_invalid_argument;
4107
4108 out_flush();
4109 gui_mch_show_toolbar((toolbar_flags &
4110 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4111 return NULL;
4112}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004113
4114 int
4115expand_set_toolbar(optexpand_T *args, int *numMatches, char_u ***matches)
4116{
4117 return expand_set_opt_string(
4118 args,
4119 p_toolbar_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004120 ARRAY_LENGTH(p_toolbar_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004121 numMatches,
4122 matches);
4123}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004124#endif
4125
4126#if (defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)) || defined(PROTO)
4127/*
4128 * The 'toolbariconsize' option is changed. GTK+ 2 only.
4129 */
4130 char *
4131did_set_toolbariconsize(optset_T *args UNUSED)
4132{
4133 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
4134 return e_invalid_argument;
4135
4136 out_flush();
4137 gui_mch_show_toolbar((toolbar_flags &
4138 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4139 return NULL;
4140}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004141
4142 int
4143expand_set_toolbariconsize(optexpand_T *args, int *numMatches, char_u ***matches)
4144{
4145 return expand_set_opt_string(
4146 args,
4147 p_tbis_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004148 ARRAY_LENGTH(p_tbis_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004149 numMatches,
4150 matches);
4151}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004152#endif
4153
4154#if defined(UNIX) || defined(VMS) || defined(PROTO)
4155/*
4156 * The 'ttymouse' option is changed.
4157 */
4158 char *
4159did_set_ttymouse(optset_T *args UNUSED)
4160{
4161 char *errmsg = NULL;
4162
4163 // Switch the mouse off before changing the escape sequences used for
4164 // that.
4165 mch_setmouse(FALSE);
4166 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
4167 errmsg = e_invalid_argument;
4168 else
4169 check_mouse_termcode();
4170 if (termcap_active)
4171 setmouse(); // may switch it on again
4172
4173 return errmsg;
4174}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004175
4176 int
4177expand_set_ttymouse(optexpand_T *args, int *numMatches, char_u ***matches)
4178{
4179 return expand_set_opt_string(
4180 args,
4181 p_ttym_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004182 ARRAY_LENGTH(p_ttym_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004183 numMatches,
4184 matches);
4185}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004186#endif
4187
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004188#if defined(FEAT_VARTABS) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004189/*
4190 * The 'varsofttabstop' option is changed.
4191 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004192 char *
4193did_set_varsofttabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004194{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004195 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004196 char_u *cp;
4197
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004198 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004199 VIM_CLEAR(curbuf->b_p_vsts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004200 else
4201 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004202 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004203 {
4204 if (vim_isdigit(*cp))
4205 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004206 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004207 continue;
4208 return e_invalid_argument;
4209 }
4210
4211 int *oldarray = curbuf->b_p_vsts_array;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004212 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004213 {
4214 if (oldarray)
4215 vim_free(oldarray);
4216 }
4217 else
4218 return e_invalid_argument;
4219 }
4220
4221 return NULL;
4222}
4223
4224/*
4225 * The 'vartabstop' option is changed.
4226 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004227 char *
4228did_set_vartabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004229{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004230 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004231 char_u *cp;
4232
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004233 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004234 VIM_CLEAR(curbuf->b_p_vts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004235 else
4236 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004237 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004238 {
4239 if (vim_isdigit(*cp))
4240 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004241 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004242 continue;
4243 return e_invalid_argument;
4244 }
4245
4246 int *oldarray = curbuf->b_p_vts_array;
4247
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004248 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004249 {
4250 vim_free(oldarray);
4251# ifdef FEAT_FOLDING
4252 if (foldmethodIsIndent(curwin))
4253 foldUpdateAll(curwin);
4254# endif
4255 }
4256 else
4257 return e_invalid_argument;
4258 }
4259
4260 return NULL;
4261}
4262#endif
4263
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004264/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004265 * The 'verbosefile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004266 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004267 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004268did_set_verbosefile(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004269{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004270 verbose_stop();
4271 if (*p_vfile != NUL && verbose_open() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004272 return e_invalid_argument;
4273
4274 return NULL;
4275}
4276
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004277#if defined(FEAT_SESSION) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004278/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004279 * The 'viewoptions' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004280 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004281 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004282did_set_viewoptions(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004283{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004284 return did_set_opt_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004285}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004286#endif
4287
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004288#if defined(FEAT_VIMINFO) || defined(PROTO)
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004289/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004290 * The 'viminfo' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004291 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004292 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004293did_set_viminfo(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004294{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004295 char_u *s;
4296 char *errmsg = NULL;
4297
4298 for (s = p_viminfo; *s;)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004299 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004300 // Check it's a valid character
4301 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
4302 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01004303 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004304 break;
4305 }
4306 if (*s == 'n') // name is always last one
4307 break;
4308 else if (*s == 'r') // skip until next ','
4309 {
4310 while (*++s && *s != ',')
4311 ;
4312 }
4313 else if (*s == '%')
4314 {
4315 // optional number
4316 while (vim_isdigit(*++s))
4317 ;
4318 }
4319 else if (*s == '!' || *s == 'h' || *s == 'c')
4320 ++s; // no extra chars
4321 else // must have a number
4322 {
4323 while (vim_isdigit(*++s))
4324 ;
4325
4326 if (!VIM_ISDIGIT(*(s - 1)))
4327 {
4328 if (args->os_errbuf != NULL)
4329 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01004330 vim_snprintf(args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004331 _(e_missing_number_after_angle_str_angle),
4332 transchar_byte(*(s - 1)));
4333 errmsg = args->os_errbuf;
4334 }
4335 else
4336 errmsg = "";
4337 break;
4338 }
4339 }
4340 if (*s == ',')
4341 ++s;
4342 else if (*s)
4343 {
4344 if (args->os_errbuf != NULL)
4345 errmsg = e_missing_comma;
4346 else
4347 errmsg = "";
4348 break;
4349 }
4350 }
4351 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
4352 errmsg = e_must_specify_a_value;
4353
4354 return errmsg;
4355}
4356#endif
4357
4358/*
4359 * The 'virtualedit' option is changed.
4360 */
4361 char *
4362did_set_virtualedit(optset_T *args)
4363{
4364 char_u *ve = p_ve;
4365 unsigned int *flags = &ve_flags;
4366
4367 if (args->os_flags & OPT_LOCAL)
4368 {
4369 ve = curwin->w_p_ve;
4370 flags = &curwin->w_ve_flags;
4371 }
4372
4373 if ((args->os_flags & OPT_LOCAL) && *ve == NUL)
4374 // make the local value empty: use the global value
4375 *flags = 0;
4376 else
4377 {
4378 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
4379 return e_invalid_argument;
4380 else if (STRCMP(ve, args->os_oldval.string) != 0)
4381 {
4382 // Recompute cursor position in case the new 've' setting
4383 // changes something.
4384 validate_virtcol();
4385 coladvance(curwin->w_virtcol);
4386 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004387 }
4388
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004389 return NULL;
4390}
4391
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004392 int
4393expand_set_virtualedit(optexpand_T *args, int *numMatches, char_u ***matches)
4394{
4395 return expand_set_opt_string(
4396 args,
4397 p_ve_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004398 ARRAY_LENGTH(p_ve_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004399 numMatches,
4400 matches);
4401}
4402
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004403/*
4404 * The 'whichwrap' option is changed.
4405 */
4406 char *
4407did_set_whichwrap(optset_T *args)
4408{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004409 char_u **varp = (char_u **)args->os_varp;
4410
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004411 // Add ',' to the list flags because 'whichwrap' is a flag
4412 // list that is comma-separated.
Christian Brabandtb39b2402023-11-29 11:34:05 +01004413 return did_set_option_listflag(*varp, (char_u *)(WW_ALL ","),
4414 args->os_errbuf, args->os_errbuflen);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004415}
4416
4417 int
4418expand_set_whichwrap(optexpand_T *args, int *numMatches, char_u ***matches)
4419{
4420 return expand_set_opt_listflag(args, (char_u*)WW_ALL, numMatches, matches);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004421}
4422
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004423/*
4424 * The 'wildmode' option is changed.
4425 */
4426 char *
4427did_set_wildmode(optset_T *args UNUSED)
4428{
4429 if (check_opt_wim() == FAIL)
4430 return e_invalid_argument;
4431 return NULL;
4432}
4433
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004434 int
4435expand_set_wildmode(optexpand_T *args, int *numMatches, char_u ***matches)
4436{
4437 return expand_set_opt_string(
4438 args,
4439 p_wim_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004440 ARRAY_LENGTH(p_wim_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004441 numMatches,
4442 matches);
4443}
4444
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004445/*
4446 * The 'wildoptions' option is changed.
4447 */
4448 char *
4449did_set_wildoptions(optset_T *args UNUSED)
4450{
4451 return did_set_opt_strings(p_wop, p_wop_values, TRUE);
4452}
4453
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004454 int
4455expand_set_wildoptions(optexpand_T *args, int *numMatches, char_u ***matches)
4456{
4457 return expand_set_opt_string(
4458 args,
4459 p_wop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004460 ARRAY_LENGTH(p_wop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004461 numMatches,
4462 matches);
4463}
4464
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004465#if defined(FEAT_WAK) || defined(PROTO)
4466/*
4467 * The 'winaltkeys' option is changed.
4468 */
4469 char *
4470did_set_winaltkeys(optset_T *args UNUSED)
4471{
4472 char *errmsg = NULL;
4473
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004474 if (*p_wak == NUL || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004475 errmsg = e_invalid_argument;
4476# ifdef FEAT_MENU
4477# if defined(FEAT_GUI_MOTIF)
4478 else if (gui.in_use)
4479 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4480# elif defined(FEAT_GUI_GTK)
4481 else if (gui.in_use)
4482 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4483# endif
4484# endif
4485 return errmsg;
4486}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004487
4488 int
4489expand_set_winaltkeys(optexpand_T *args, int *numMatches, char_u ***matches)
4490{
4491 return expand_set_opt_string(
4492 args,
4493 p_wak_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004494 ARRAY_LENGTH(p_wak_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004495 numMatches,
4496 matches);
4497}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004498#endif
4499
4500/*
4501 * The 'wincolor' option is changed.
4502 */
4503 char *
4504did_set_wincolor(optset_T *args UNUSED)
4505{
4506#ifdef FEAT_TERMINAL
4507 term_update_wincolor(curwin);
4508#endif
4509 return NULL;
4510}
4511
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004512 int
4513expand_set_wincolor(optexpand_T *args, int *numMatches, char_u ***matches)
4514{
4515 return expand_set_opt_generic(
4516 args,
4517 get_highlight_name,
4518 numMatches,
4519 matches);
4520}
4521
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004522#ifdef FEAT_SYN_HL
4523/*
4524 * When the 'syntax' option is set, load the syntax of that name.
4525 */
4526 static void
4527do_syntax_autocmd(int value_changed)
4528{
4529 static int syn_recursive = 0;
4530
4531 ++syn_recursive;
4532 // Only pass TRUE for "force" when the value changed or not used
4533 // recursively, to avoid endless recurrence.
4534 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
4535 value_changed || syn_recursive == 1, curbuf);
4536 curbuf->b_flags |= BF_SYN_SET;
4537 --syn_recursive;
4538}
4539#endif
4540
4541/*
4542 * When the 'filetype' option is set, trigger the FileType autocommand.
4543 */
4544 static void
4545do_filetype_autocmd(char_u **varp, int opt_flags, int value_changed)
4546{
4547 // Skip this when called from a modeline and the filetype was already set
4548 // to this value.
4549 if ((opt_flags & OPT_MODELINE) && !value_changed)
4550 return;
4551
4552 static int ft_recursive = 0;
4553 int secure_save = secure;
4554
4555 // Reset the secure flag, since the value of 'filetype' has
4556 // been checked to be safe.
4557 secure = 0;
4558
4559 ++ft_recursive;
zeertzjq5bf6c212024-03-31 18:41:27 +02004560 curbuf->b_did_filetype = TRUE;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004561 // Only pass TRUE for "force" when the value changed or not
4562 // used recursively, to avoid endless recurrence.
4563 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
4564 value_changed || ft_recursive == 1, curbuf);
4565 --ft_recursive;
4566 // Just in case the old "curbuf" is now invalid.
4567 if (varp != &(curbuf->b_p_ft))
4568 varp = NULL;
4569
4570 secure = secure_save;
4571}
4572
4573#ifdef FEAT_SPELL
4574/*
4575 * When the 'spelllang' option is set, source the spell/LANG.vim file in
4576 * 'runtimepath'.
4577 */
4578 static void
4579do_spelllang_source(void)
4580{
4581 char_u fname[200];
4582 char_u *p;
4583 char_u *q = curwin->w_s->b_p_spl;
4584
4585 // Skip the first name if it is "cjk".
4586 if (STRNCMP(q, "cjk,", 4) == 0)
4587 q += 4;
4588
4589 // They could set 'spellcapcheck' depending on the language. Use the first
4590 // name in 'spelllang' up to '_region' or '.encoding'.
4591 for (p = q; *p != NUL; ++p)
4592 if (!ASCII_ISALNUM(*p) && *p != '-')
4593 break;
4594 if (p > q)
4595 {
4596 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
4597 (int)(p - q), q);
4598 source_runtime(fname, DIP_ALL);
4599 }
4600}
4601#endif
4602
4603/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004604 * Handle string options that need some action to perform when changed.
zeertzjqf6782732022-07-27 18:26:03 +01004605 * The new value must be allocated.
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004606 * Returns NULL for success, or an untranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +02004607 */
4608 char *
4609did_set_string_option(
4610 int opt_idx, // index in options[] table
4611 char_u **varp, // pointer to the option variable
Bram Moolenaardac13472019-09-16 21:06:21 +02004612 char_u *oldval, // previous value of the option
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004613 char_u *value, // new value of the option
Bram Moolenaardac13472019-09-16 21:06:21 +02004614 char *errbuf, // buffer for errors, or NULL
Mike Williams620f0112023-12-05 15:36:06 +01004615 size_t errbuflen, // length of error buffer
Bram Moolenaardac13472019-09-16 21:06:21 +02004616 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004617 set_op_T op, // OP_ADDING/OP_PREPENDING/OP_REMOVING
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004618 int *value_checked) // value was checked to be safe, no
Bram Moolenaardac13472019-09-16 21:06:21 +02004619 // need to set P_INSECURE
4620{
4621 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +02004622 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004623 opt_did_set_cb_T did_set_cb = get_option_did_set_cb(opt_idx);
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004624 optset_T args;
4625
4626 // 'ttytype' is an alias for 'term'. Both 'term' and 'ttytype' point to
4627 // T_NAME. If 'term' or 'ttytype' is modified, then use the index for the
4628 // 'term' option. Only set the P_ALLOCED flag on 'term'.
4629 if (varp == &T_NAME)
4630 {
4631 opt_idx = findoption((char_u *)"term");
4632 if (opt_idx >= 0)
4633 {
4634 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
4635 did_set_cb = get_option_did_set_cb(opt_idx);
4636 }
4637 }
4638
4639 CLEAR_FIELD(args);
Bram Moolenaardac13472019-09-16 21:06:21 +02004640
Bram Moolenaardac13472019-09-16 21:06:21 +02004641 // Disallow changing some options from secure mode
4642 if ((secure
4643#ifdef HAVE_SANDBOX
4644 || sandbox != 0
4645#endif
4646 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00004647 errmsg = e_not_allowed_here;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004648 // Check for a "normal" directory or file name in some options.
4649 else if (check_illegal_path_names(opt_idx, varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004650 errmsg = e_invalid_argument;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004651 else if (did_set_cb != NULL)
4652 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004653 args.os_varp = (char_u *)varp;
4654 args.os_idx = opt_idx;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004655 args.os_flags = opt_flags;
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004656 args.os_op = op;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004657 args.os_oldval.string = oldval;
4658 args.os_newval.string = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004659 args.os_errbuf = errbuf;
Christian Brabandtb39b2402023-11-29 11:34:05 +01004660 args.os_errbuflen = errbuflen;
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004661 // Invoke the option specific callback function to validate and apply
4662 // the new option value.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004663 errmsg = did_set_cb(&args);
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004664
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004665 // The 'keymap', 'filetype' and 'syntax' option callback functions
4666 // may change the os_value_checked field.
4667 *value_checked = args.os_value_checked;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004668 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004669
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004670 // If an error is detected, restore the previous value.
Bram Moolenaardac13472019-09-16 21:06:21 +02004671 if (errmsg != NULL)
4672 {
zeertzjqf6782732022-07-27 18:26:03 +01004673 free_string_option(*varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02004674 *varp = oldval;
4675 // When resetting some values, need to act on it.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004676 if (args.os_restore_chartab)
Bram Moolenaardac13472019-09-16 21:06:21 +02004677 (void)init_chartab();
4678 if (varp == &p_hl)
4679 (void)highlight_changed();
4680 }
4681 else
4682 {
4683#ifdef FEAT_EVAL
4684 // Remember where the option was set.
4685 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4686#endif
4687 // Free string options that are in allocated memory.
4688 // Use "free_oldval", because recursiveness may change the flags under
4689 // our fingers (esp. init_highlight()).
4690 if (free_oldval)
4691 free_string_option(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01004692 set_option_flag(opt_idx, P_ALLOCED);
Bram Moolenaardac13472019-09-16 21:06:21 +02004693
4694 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
4695 && is_global_local_option(opt_idx))
4696 {
4697 // global option with local value set to use global value; free
4698 // the local value and make it empty
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004699 char_u *p = get_option_varp_scope(opt_idx, OPT_LOCAL);
Bram Moolenaardac13472019-09-16 21:06:21 +02004700 free_string_option(*(char_u **)p);
4701 *(char_u **)p = empty_option;
4702 }
4703
4704 // May set global value for local option.
4705 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
4706 set_string_option_global(opt_idx, varp);
4707
4708 // Trigger the autocommand only after setting the flags.
4709#ifdef FEAT_SYN_HL
Bram Moolenaardac13472019-09-16 21:06:21 +02004710 if (varp == &(curbuf->b_p_syn))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004711 do_syntax_autocmd(args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004712#endif
4713 else if (varp == &(curbuf->b_p_ft))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004714 do_filetype_autocmd(varp, opt_flags, args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004715#ifdef FEAT_SPELL
4716 if (varp == &(curwin->w_s->b_p_spl))
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004717 do_spelllang_source();
Bram Moolenaardac13472019-09-16 21:06:21 +02004718#endif
4719 }
4720
Bram Moolenaardac13472019-09-16 21:06:21 +02004721 if (varp == &p_mouse)
4722 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004723 if (*p_mouse == NUL)
4724 mch_setmouse(FALSE); // switch mouse off
4725 else
Bram Moolenaardac13472019-09-16 21:06:21 +02004726 setmouse(); // in case 'mouse' changed
4727 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004728
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004729 if (varp == &p_rtp)
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004730 {
4731 export_myvimdir();
4732#if defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004733 update_package_paths_in_lua();
4734#endif
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004735 }
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004736
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004737#if defined(FEAT_LINEBREAK)
4738 // Changing Formatlistpattern when briopt includes the list setting:
4739 // redraw
4740 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
4741 && curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004742 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004743#endif
4744
Bram Moolenaardac13472019-09-16 21:06:21 +02004745 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004746 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0
4747 && (get_option_flags(opt_idx) & P_HLONLY) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02004748 curwin->w_set_curswant = TRUE;
4749
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004750 if ((opt_flags & OPT_NO_REDRAW) == 0)
4751 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004752#ifdef FEAT_GUI
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004753 // set when changing an option that only requires a redraw in the GUI
4754 int redraw_gui_only = FALSE;
4755
4756 if (varp == &p_go // 'guioptions'
4757 || varp == &p_guifont // 'guifont'
4758# ifdef FEAT_GUI_TABLINE
4759 || varp == &p_gtl // 'guitablabel'
4760 || varp == &p_gtt // 'guitabtooltip'
4761# endif
4762# ifdef FEAT_XFONTSET
4763 || varp == &p_guifontset // 'guifontset'
4764# endif
4765 || varp == &p_guifontwide // 'guifontwide'
Erik S. V. Jansson2f026382024-02-26 22:23:05 +01004766# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004767 || varp == &p_guiligatures // 'guiligatures'
4768# endif
4769 )
4770 redraw_gui_only = TRUE;
4771
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004772 // check redraw when it's not a GUI option or the GUI is active.
4773 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02004774#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004775 check_redraw(get_option_flags(opt_idx));
4776 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004777
4778#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004779 if (args.os_did_swaptcap)
Bram Moolenaardac13472019-09-16 21:06:21 +02004780 {
4781 set_termname((char_u *)"win32");
4782 init_highlight(TRUE, FALSE);
4783 }
4784#endif
4785
4786 return errmsg;
4787}
4788
4789/*
4790 * Check an option that can be a range of string values.
4791 *
4792 * Return OK for correct value, FAIL otherwise.
4793 * Empty is always OK.
4794 */
4795 static int
4796check_opt_strings(
4797 char_u *val,
4798 char **values,
4799 int list) // when TRUE: accept a list of values
4800{
4801 return opt_strings_flags(val, values, NULL, list);
4802}
4803
4804/*
4805 * Handle an option that can be a range of string values.
4806 * Set a flag in "*flagp" for each string present.
4807 *
4808 * Return OK for correct value, FAIL otherwise.
4809 * Empty is always OK.
4810 */
4811 static int
4812opt_strings_flags(
4813 char_u *val, // new value
4814 char **values, // array of valid string values
4815 unsigned *flagp,
4816 int list) // when TRUE: accept a list of values
4817{
4818 int i;
4819 int len;
4820 unsigned new_flags = 0;
4821
4822 while (*val)
4823 {
4824 for (i = 0; ; ++i)
4825 {
4826 if (values[i] == NULL) // val not found in values[]
4827 return FAIL;
4828
4829 len = (int)STRLEN(values[i]);
4830 if (STRNCMP(values[i], val, len) == 0
4831 && ((list && val[len] == ',') || val[len] == NUL))
4832 {
4833 val += len + (val[len] == ',');
4834 new_flags |= (1 << i);
4835 break; // check next item in val list
4836 }
4837 }
4838 }
4839 if (flagp != NULL)
4840 *flagp = new_flags;
4841
4842 return OK;
4843}
4844
4845/*
4846 * return OK if "p" is a valid fileformat name, FAIL otherwise.
4847 */
4848 int
4849check_ff_value(char_u *p)
4850{
4851 return check_opt_strings(p, p_ff_values, FALSE);
4852}
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004853
4854/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004855 * Save the actual shortmess Flags and clear them temporarily to avoid that
4856 * file messages overwrites any output from the following commands.
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004857 *
4858 * Caller must make sure to first call save_clear_shm_value() and then
4859 * restore_shm_value() exactly the same number of times.
4860 */
4861 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004862save_clear_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004863{
4864 if (STRLEN(p_shm) >= SHM_LEN)
4865 {
4866 iemsg(e_internal_error_shortmess_too_long);
4867 return;
4868 }
4869
4870 if (++set_shm_recursive == 1)
4871 {
4872 STRCPY(shm_buf, p_shm);
4873 set_option_value_give_err((char_u *)"shm", 0L, (char_u *)"", 0);
4874 }
4875}
4876
4877/*
4878 * Restore the shortmess Flags set from the save_clear_shm_value() function.
4879 */
4880 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004881restore_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004882{
4883 if (--set_shm_recursive == 0)
4884 {
4885 set_option_value_give_err((char_u *)"shm", 0L, shm_buf, 0);
4886 vim_memset(shm_buf, 0, SHM_LEN);
4887 }
4888}
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004889
4890/*
4891 * Export the environment variable $MYVIMDIR to the first item in runtimepath
4892 */
Christian Brabandt1a741d32025-03-01 16:30:33 +01004893 void
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004894export_myvimdir()
4895{
4896 int dofree = FALSE;
4897 char_u *p;
4898 char_u *q = p_rtp;
4899 char_u *buf = alloc(MAXPATHL);
4900
4901 if (buf == NULL)
4902 return;
4903
4904 (void)copy_option_part(&q, buf, MAXPATHL, ",");
4905
4906 p = vim_getenv((char_u *)"MYVIMDIR", &dofree);
4907
4908 if (p == NULL || STRCMP(p, buf) != 0)
4909 {
4910 add_pathsep(buf);
4911#ifdef MSWIN
4912 // normalize path separators
4913 for (q = buf; *q != NUL; q++)
4914 if (*q == '/')
4915 *q = '\\';
4916#endif
4917 vim_setenv((char_u *)"MYVIMDIR", buf);
4918 }
4919 if (dofree)
4920 vim_free(p);
4921 vim_free(buf);
4922}