blob: 8e233e86c3cf8ed180a2b7f458beb3fad848e21e [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
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +020031#if defined(FEAT_TABPANEL)
32static char *(p_tpl_values[]) = {"wrap", "align:", "columns:", "vert:", NULL};
33#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +020034#if defined(FEAT_DIFF)
35// Note: Keep this in sync with diffopt_changed()
Yee Cheng Chin9943d472025-03-26 19:41:02 +010036static 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 +020037static char *(p_dip_algorithm_values[]) = {"myers", "minimal", "patience", "histogram", NULL};
Yee Cheng Chin9943d472025-03-26 19:41:02 +010038static char *(p_dip_inline_values[]) = {"none", "simple", "char", "word", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020039#endif
distobs25ac6d62024-07-06 17:50:09 +020040static char *(p_nf_values[]) = {"bin", "octal", "hex", "alpha", "unsigned", "blank", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020041static char *(p_ff_values[]) = {FF_UNIX, FF_DOS, FF_MAC, NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020042#ifdef FEAT_CLIPBOARD
43// Note: Keep this in sync with did_set_clipboard()
44static char *(p_cb_values[]) = {"unnamed", "unnamedplus", "autoselect", "autoselectplus", "autoselectml", "html", "exclude:", NULL};
45#endif
Bram Moolenaardac13472019-09-16 21:06:21 +020046#ifdef FEAT_CRYPT
Christian Brabandtf573c6e2021-06-20 14:02:16 +020047static char *(p_cm_values[]) = {"zip", "blowfish", "blowfish2",
48 # ifdef FEAT_SODIUM
Christian Brabandtaae58342023-04-23 17:50:22 +010049 "xchacha20", "xchacha20v2",
Christian Brabandtf573c6e2021-06-20 14:02:16 +020050 # endif
51 NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020052#endif
53static char *(p_cmp_values[]) = {"internal", "keepascii", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020054#ifdef FEAT_SYN_HL
55// Note: Keep this in sync with fill_culopt_flags()
56static char *(p_culopt_values[]) = {"line", "screenline", "number", "both", NULL};
57#endif
Bram Moolenaardac13472019-09-16 21:06:21 +020058static char *(p_dy_values[]) = {"lastline", "truncate", "uhex", NULL};
Yegappan Lakshmanan87018252023-09-20 20:20:04 +020059static char *(p_jop_values[]) = {"stack", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020060#ifdef FEAT_FOLDING
61static char *(p_fdo_values[]) = {"all", "block", "hor", "mark", "percent",
62 "quickfix", "search", "tag", "insert",
63 "undo", "jump", NULL};
64#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +020065// Note: Keep this in sync with match_keyprotocol()
66static char *(p_kpc_protocol_values[]) = {"none", "mok2", "kitty", NULL};
67#ifdef FEAT_PROP_POPUP
68// Note: Keep this in sync with parse_popup_option()
69static char *(p_popup_option_values[]) = {"height:", "width:", "highlight:", "border:", "align:", NULL};
70static char *(p_popup_option_border_values[]) = {"on", "off", NULL};
71static char *(p_popup_option_align_values[]) = {"item", "menu", NULL};
72#endif
73#if defined(FEAT_SPELL)
74// Note: Keep this in sync with spell_check_sps()
75static char *(p_sps_values[]) = {"best", "fast", "double", "expr:", "file:", "timeout:", NULL};
76#endif
Bram Moolenaardac13472019-09-16 21:06:21 +020077#ifdef FEAT_SESSION
Bram Moolenaar635bd602021-04-16 19:58:22 +020078// Also used for 'viewoptions'! Keep in sync with SSOP_ flags.
Bram Moolenaardac13472019-09-16 21:06:21 +020079static char *(p_ssop_values[]) = {"buffers", "winpos", "resize", "winsize",
80 "localoptions", "options", "help", "blank", "globals", "slash", "unix",
Bram Moolenaar635bd602021-04-16 19:58:22 +020081 "sesdir", "curdir", "folds", "cursor", "tabpages", "terminal", "skiprtp",
82 NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020083#endif
Bram Moolenaar539aa6b2019-11-17 18:09:38 +010084// Keep in sync with SWB_ flags in option.h
85static char *(p_swb_values[]) = {"useopen", "usetab", "split", "newtab", "vsplit", "uselast", NULL};
Luuk van Baal13ece2a2022-10-03 15:28:08 +010086static char *(p_spk_values[]) = {"cursor", "screen", "topline", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020087static char *(p_tc_values[]) = {"followic", "ignore", "match", "followscs", "smart", NULL};
LemonBoy5247b0b2024-07-12 19:30:58 +020088// Keep in sync with TCL_ flags in option.h
89static char *(p_tcl_values[]) = {"left", "uselast", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020090#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
91static char *(p_toolbar_values[]) = {"text", "icons", "tooltips", "horiz", NULL};
92#endif
93#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
94static char *(p_tbis_values[]) = {"tiny", "small", "medium", "large", "huge", "giant", NULL};
95#endif
Bram Moolenaara1cb1d12019-10-17 23:00:07 +020096#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +020097static char *(p_ttym_values[]) = {"xterm", "xterm2", "dec", "netterm", "jsbterm", "pterm", "urxvt", "sgr", NULL};
98#endif
Gary Johnson53ba05b2021-07-26 22:19:10 +020099static char *(p_ve_values[]) = {"block", "insert", "all", "onemore", "none", "NONE", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200100// Note: Keep this in sync with check_opt_wim()
Girish Palya2bacc3e2025-03-02 22:55:57 +0100101static char *(p_wim_values[]) = {"full", "longest", "list", "lastused", "noselect", NULL};
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000102static char *(p_wop_values[]) = {"fuzzy", "tagfile", "pum", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200103#ifdef FEAT_WAK
104static char *(p_wak_values[]) = {"yes", "menu", "no", NULL};
105#endif
106static char *(p_mousem_values[]) = {"extend", "popup", "popup_setpos", "mac", NULL};
107static char *(p_sel_values[]) = {"inclusive", "exclusive", "old", NULL};
108static char *(p_slm_values[]) = {"mouse", "key", "cmd", NULL};
109static char *(p_km_values[]) = {"startsel", "stopsel", NULL};
110#ifdef FEAT_BROWSE
111static char *(p_bsdir_values[]) = {"current", "last", "buffer", NULL};
112#endif
113static char *(p_scbopt_values[]) = {"ver", "hor", "jump", NULL};
114static char *(p_debug_values[]) = {"msg", "throw", "beep", NULL};
115static char *(p_ead_values[]) = {"both", "ver", "hor", NULL};
116static char *(p_buftype_values[]) = {"nofile", "nowrite", "quickfix", "help", "terminal", "acwrite", "prompt", "popup", NULL};
117static char *(p_bufhidden_values[]) = {"hide", "unload", "delete", "wipe", NULL};
Bram Moolenaaraa0489e2020-04-17 19:41:21 +0200118static char *(p_bs_values[]) = {"indent", "eol", "start", "nostop", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200119#ifdef FEAT_FOLDING
120static char *(p_fdm_values[]) = {"manual", "expr", "marker", "indent", "syntax",
121# ifdef FEAT_DIFF
122 "diff",
123# endif
124 NULL};
125static char *(p_fcl_values[]) = {"all", NULL};
126#endif
glepnirf31cfa22025-03-06 21:59:13 +0100127static char *(p_cfc_values[]) = {"keyword", "files", "whole_line", NULL};
Girish Palyab1565882025-04-15 20:16:00 +0200128static char *(p_cot_values[]) = {"menu", "menuone", "longest", "preview", "popup", "popuphidden", "noinsert", "noselect", "fuzzy", "nosort", "preinsert", "nearest", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200129#ifdef BACKSLASH_IN_FILENAME
130static char *(p_csl_values[]) = {"slash", "backslash", NULL};
131#endif
132#ifdef FEAT_SIGNS
133static char *(p_scl_values[]) = {"yes", "no", "auto", "number", NULL};
134#endif
135#if defined(MSWIN) && defined(FEAT_TERMINAL)
136static char *(p_twt_values[]) = {"winpty", "conpty", "", NULL};
137#endif
Luuk van Baalba936f62022-12-15 13:15:39 +0000138static char *(p_sloc_values[]) = {"last", "statusline", "tabline", NULL};
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +0000139static char *(p_sws_values[]) = {"fsync", "sync", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200140
141static int check_opt_strings(char_u *val, char **values, int list);
142static int opt_strings_flags(char_u *val, char **values, unsigned *flagp, int list);
143
144/*
145 * After setting various option values: recompute variables that depend on
146 * option values.
147 */
148 void
149didset_string_options(void)
150{
151 (void)opt_strings_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
152 (void)opt_strings_flags(p_bkc, p_bkc_values, &bkc_flags, TRUE);
153 (void)opt_strings_flags(p_bo, p_bo_values, &bo_flags, TRUE);
glepnirf31cfa22025-03-06 21:59:13 +0100154 (void)opt_strings_flags(p_cfc, p_cfc_values, &cfc_flags, TRUE);
zeertzjq529b9ad2024-06-05 20:27:06 +0200155 (void)opt_strings_flags(p_cot, p_cot_values, &cot_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200156#ifdef FEAT_SESSION
157 (void)opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE);
158 (void)opt_strings_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
159#endif
160#ifdef FEAT_FOLDING
161 (void)opt_strings_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
162#endif
163 (void)opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE);
Yegappan Lakshmanan87018252023-09-20 20:20:04 +0200164 (void)opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200165 (void)opt_strings_flags(p_tc, p_tc_values, &tc_flags, FALSE);
166 (void)opt_strings_flags(p_ve, p_ve_values, &ve_flags, TRUE);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +0200167#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +0200168 (void)opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE);
169#endif
170#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
171 (void)opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags, TRUE);
172#endif
173#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
174 (void)opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE);
175#endif
Sean Dewar39c46b42022-05-12 17:44:29 +0100176 (void)opt_strings_flags(p_swb, p_swb_values, &swb_flags, TRUE);
LemonBoy5247b0b2024-07-12 19:30:58 +0200177 (void)opt_strings_flags(p_tcl, p_tcl_values, &tcl_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200178}
179
Yegappan Lakshmananf9dc2782023-05-11 15:02:56 +0100180#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200181/*
182 * Trigger the OptionSet autocommand.
183 * "opt_idx" is the index of the option being set.
184 * "opt_flags" can be OPT_LOCAL etc.
185 * "oldval" the old value
186 * "oldval_l" the old local value (only non-NULL if global and local value
187 * are set)
188 * "oldval_g" the old global value (only non-NULL if global and local value
189 * are set)
190 * "newval" the new value
191 */
192 void
zeertzjq269aa2b2022-11-28 11:36:50 +0000193trigger_optionset_string(
Bram Moolenaardac13472019-09-16 21:06:21 +0200194 int opt_idx,
195 int opt_flags,
196 char_u *oldval,
197 char_u *oldval_l,
198 char_u *oldval_g,
199 char_u *newval)
200{
201 // Don't do this recursively.
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000202 if (oldval == NULL || newval == NULL
203 || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
204 return;
Bram Moolenaardac13472019-09-16 21:06:21 +0200205
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000206 char_u buf_type[7];
207
208 sprintf((char *)buf_type, "%s",
Bram Moolenaardac13472019-09-16 21:06:21 +0200209 (opt_flags & OPT_LOCAL) ? "local" : "global");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000210 set_vim_var_string(VV_OPTION_OLD, oldval, -1);
211 set_vim_var_string(VV_OPTION_NEW, newval, -1);
212 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
213 if (opt_flags & OPT_LOCAL)
214 {
215 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
216 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
Bram Moolenaardac13472019-09-16 21:06:21 +0200217 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000218 if (opt_flags & OPT_GLOBAL)
219 {
220 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
221 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval, -1);
222 }
223 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
224 {
225 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
226 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval_l, -1);
227 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval_g, -1);
228 }
229 if (opt_flags & OPT_MODELINE)
230 {
231 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
232 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
233 }
234 apply_autocmds(EVENT_OPTIONSET,
235 get_option_fullname(opt_idx), NULL, FALSE,
236 NULL);
237 reset_v_option_vars();
Bram Moolenaardac13472019-09-16 21:06:21 +0200238}
239#endif
240
241 static char *
Mike Williams620f0112023-12-05 15:36:06 +0100242illegal_char(char *errbuf, size_t errbuflen, int c)
Bram Moolenaardac13472019-09-16 21:06:21 +0200243{
244 if (errbuf == NULL)
245 return "";
zeertzjq6a8d2e12024-01-17 20:54:49 +0100246 vim_snprintf(errbuf, errbuflen, _(e_illegal_character_str),
Christian Brabandtb39b2402023-11-29 11:34:05 +0100247 (char *)transchar(c));
Bram Moolenaardac13472019-09-16 21:06:21 +0200248 return errbuf;
249}
250
251/*
252 * Check string options in a buffer for NULL value.
253 */
254 void
255check_buf_options(buf_T *buf)
256{
257 check_string_option(&buf->b_p_bh);
258 check_string_option(&buf->b_p_bt);
259 check_string_option(&buf->b_p_fenc);
260 check_string_option(&buf->b_p_ff);
261#ifdef FEAT_FIND_ID
262 check_string_option(&buf->b_p_def);
263 check_string_option(&buf->b_p_inc);
264# ifdef FEAT_EVAL
265 check_string_option(&buf->b_p_inex);
266# endif
267#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +0100268#if defined(FEAT_EVAL)
Bram Moolenaardac13472019-09-16 21:06:21 +0200269 check_string_option(&buf->b_p_inde);
270 check_string_option(&buf->b_p_indk);
271#endif
272#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
273 check_string_option(&buf->b_p_bexpr);
274#endif
275#if defined(FEAT_CRYPT)
276 check_string_option(&buf->b_p_cm);
277#endif
278 check_string_option(&buf->b_p_fp);
279#if defined(FEAT_EVAL)
280 check_string_option(&buf->b_p_fex);
281#endif
282#ifdef FEAT_CRYPT
283 check_string_option(&buf->b_p_key);
284#endif
285 check_string_option(&buf->b_p_kp);
286 check_string_option(&buf->b_p_mps);
287 check_string_option(&buf->b_p_fo);
288 check_string_option(&buf->b_p_flp);
289 check_string_option(&buf->b_p_isk);
Bram Moolenaardac13472019-09-16 21:06:21 +0200290 check_string_option(&buf->b_p_com);
Bram Moolenaardac13472019-09-16 21:06:21 +0200291#ifdef FEAT_FOLDING
292 check_string_option(&buf->b_p_cms);
293#endif
294 check_string_option(&buf->b_p_nf);
Bram Moolenaardac13472019-09-16 21:06:21 +0200295 check_string_option(&buf->b_p_qe);
Bram Moolenaardac13472019-09-16 21:06:21 +0200296#ifdef FEAT_SYN_HL
297 check_string_option(&buf->b_p_syn);
298 check_string_option(&buf->b_s.b_syn_isk);
299#endif
300#ifdef FEAT_SPELL
301 check_string_option(&buf->b_s.b_p_spc);
302 check_string_option(&buf->b_s.b_p_spf);
303 check_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +0200304 check_string_option(&buf->b_s.b_p_spo);
Bram Moolenaardac13472019-09-16 21:06:21 +0200305#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200306 check_string_option(&buf->b_p_sua);
Bram Moolenaardac13472019-09-16 21:06:21 +0200307 check_string_option(&buf->b_p_cink);
308 check_string_option(&buf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +0100309 check_string_option(&buf->b_p_cinsd);
Bram Moolenaardac13472019-09-16 21:06:21 +0200310 parse_cino(buf);
Bram Moolenaar49846fb2022-10-15 16:05:33 +0100311 check_string_option(&buf->b_p_lop);
Bram Moolenaardac13472019-09-16 21:06:21 +0200312 check_string_option(&buf->b_p_ft);
Bram Moolenaardac13472019-09-16 21:06:21 +0200313 check_string_option(&buf->b_p_cinw);
zeertzjq529b9ad2024-06-05 20:27:06 +0200314 check_string_option(&buf->b_p_cot);
Bram Moolenaardac13472019-09-16 21:06:21 +0200315 check_string_option(&buf->b_p_cpt);
glepnirbcd59952025-04-24 21:48:35 +0200316 check_string_option(&buf->b_p_ise);
Bram Moolenaardac13472019-09-16 21:06:21 +0200317#ifdef FEAT_COMPL_FUNC
318 check_string_option(&buf->b_p_cfu);
319 check_string_option(&buf->b_p_ofu);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +0100320 check_string_option(&buf->b_p_tsrfu);
Bram Moolenaardac13472019-09-16 21:06:21 +0200321#endif
322#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +0100323 check_string_option(&buf->b_p_ffu);
Bram Moolenaardac13472019-09-16 21:06:21 +0200324 check_string_option(&buf->b_p_tfu);
325#endif
326#ifdef FEAT_KEYMAP
327 check_string_option(&buf->b_p_keymap);
328#endif
329#ifdef FEAT_QUICKFIX
330 check_string_option(&buf->b_p_gp);
331 check_string_option(&buf->b_p_mp);
332 check_string_option(&buf->b_p_efm);
333#endif
334 check_string_option(&buf->b_p_ep);
335 check_string_option(&buf->b_p_path);
336 check_string_option(&buf->b_p_tags);
337 check_string_option(&buf->b_p_tc);
338 check_string_option(&buf->b_p_dict);
339 check_string_option(&buf->b_p_tsr);
Bram Moolenaardac13472019-09-16 21:06:21 +0200340 check_string_option(&buf->b_p_lw);
Bram Moolenaardac13472019-09-16 21:06:21 +0200341 check_string_option(&buf->b_p_bkc);
342 check_string_option(&buf->b_p_menc);
343#ifdef FEAT_VARTABS
344 check_string_option(&buf->b_p_vsts);
345 check_string_option(&buf->b_p_vts);
346#endif
347}
348
349/*
350 * Free the string allocated for an option.
351 * Checks for the string being empty_option. This may happen if we're out of
352 * memory, vim_strsave() returned NULL, which was replaced by empty_option by
353 * check_options().
354 * Does NOT check for P_ALLOCED flag!
355 */
356 void
357free_string_option(char_u *p)
358{
359 if (p != empty_option)
360 vim_free(p);
361}
362
363 void
364clear_string_option(char_u **pp)
365{
366 if (*pp != empty_option)
367 vim_free(*pp);
368 *pp = empty_option;
369}
370
371 void
372check_string_option(char_u **pp)
373{
374 if (*pp == NULL)
375 *pp = empty_option;
376}
377
378/*
379 * Set global value for string option when it's a local option.
380 */
381 static void
382set_string_option_global(
383 int opt_idx, // option index
384 char_u **varp) // pointer to option variable
385{
386 char_u **p, *s;
387
388 // the global value is always allocated
389 if (is_window_local_option(opt_idx))
390 p = (char_u **)GLOBAL_WO(varp);
391 else
392 p = (char_u **)get_option_var(opt_idx);
393 if (!is_global_option(opt_idx)
394 && p != varp
395 && (s = vim_strsave(*varp)) != NULL)
396 {
397 free_string_option(*p);
398 *p = s;
399 }
400}
401
402/*
403 * Set a string option to a new value (without checking the effect).
404 * The string is copied into allocated memory.
405 * if ("opt_idx" == -1) "name" is used, otherwise "opt_idx" is used.
406 * When "set_sid" is zero set the scriptID to current_sctx.sc_sid. When
407 * "set_sid" is SID_NONE don't set the scriptID. Otherwise set the scriptID to
408 * "set_sid".
409 */
410 void
411set_string_option_direct(
412 char_u *name,
413 int opt_idx,
414 char_u *val,
415 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
416 int set_sid UNUSED)
417{
418 char_u *s;
419 char_u **varp;
420 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
421 int idx = opt_idx;
422
423 if (idx == -1) // use name
424 {
425 idx = findoption(name);
426 if (idx < 0) // not found (should not happen)
427 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000428 semsg(_(e_internal_error_str), "set_string_option_direct()");
RestorerZ68ebcee2023-05-31 17:12:14 +0100429 siemsg("For option %s", name);
Bram Moolenaardac13472019-09-16 21:06:21 +0200430 return;
431 }
432 }
433
434 if (is_hidden_option(idx)) // can't set hidden option
435 return;
436
437 s = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000438 if (s == NULL)
439 return;
440
441 varp = (char_u **)get_option_varp_scope(idx,
442 both ? OPT_LOCAL : opt_flags);
443 if ((opt_flags & OPT_FREE) && (get_option_flags(idx) & P_ALLOCED))
444 free_string_option(*varp);
445 *varp = s;
446
447 // For buffer/window local option may also set the global value.
448 if (both)
449 set_string_option_global(idx, varp);
450
451 set_option_flag(idx, P_ALLOCED);
452
453 // When setting both values of a global option with a local value,
454 // make the local value empty, so that the global value is used.
455 if (is_global_local_option(idx) && both)
Bram Moolenaardac13472019-09-16 21:06:21 +0200456 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000457 free_string_option(*varp);
458 *varp = empty_option;
Bram Moolenaardac13472019-09-16 21:06:21 +0200459 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000460# ifdef FEAT_EVAL
461 if (set_sid != SID_NONE)
462 {
463 sctx_T script_ctx;
464
465 if (set_sid == 0)
466 script_ctx = current_sctx;
467 else
468 {
469 script_ctx.sc_sid = set_sid;
470 script_ctx.sc_seq = 0;
471 script_ctx.sc_lnum = 0;
472 script_ctx.sc_version = 1;
473 }
474 set_option_sctx_idx(idx, opt_flags, script_ctx);
475 }
476# endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200477}
478
Dominique Pellee764d1b2023-03-12 21:20:59 +0000479#if defined(FEAT_PROP_POPUP) || \
480 (defined(FEAT_DIFF) && defined(FEAT_FOLDING)) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200481/*
482 * Like set_string_option_direct(), but for a window-local option in "wp".
483 * Blocks autocommands to avoid the old curwin becoming invalid.
484 */
485 void
486set_string_option_direct_in_win(
487 win_T *wp,
488 char_u *name,
489 int opt_idx,
490 char_u *val,
491 int opt_flags,
492 int set_sid)
493{
494 win_T *save_curwin = curwin;
495
496 block_autocmds();
497 curwin = wp;
498 curbuf = curwin->w_buffer;
499 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
500 curwin = save_curwin;
501 curbuf = curwin->w_buffer;
502 unblock_autocmds();
503}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000504#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200505
Dominique Pelle748b3082022-01-08 12:41:16 +0000506#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200507/*
508 * Like set_string_option_direct(), but for a buffer-local option in "buf".
509 * Blocks autocommands to avoid the old curbuf becoming invalid.
510 */
511 void
512set_string_option_direct_in_buf(
513 buf_T *buf,
514 char_u *name,
515 int opt_idx,
516 char_u *val,
517 int opt_flags,
518 int set_sid)
519{
520 buf_T *save_curbuf = curbuf;
521
522 block_autocmds();
523 curbuf = buf;
524 curwin->w_buffer = curbuf;
525 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
526 curbuf = save_curbuf;
527 curwin->w_buffer = curbuf;
528 unblock_autocmds();
529}
Dominique Pelle748b3082022-01-08 12:41:16 +0000530#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200531
532/*
533 * Set a string option to a new value, and handle the effects.
534 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100535 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaardac13472019-09-16 21:06:21 +0200536 */
537 char *
538set_string_option(
539 int opt_idx,
540 char_u *value,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +0000541 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Christian Brabandtb39b2402023-11-29 11:34:05 +0100542 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +0100543 size_t errbuflen)
Bram Moolenaardac13472019-09-16 21:06:21 +0200544{
545 char_u *s;
546 char_u **varp;
547 char_u *oldval;
548#if defined(FEAT_EVAL)
549 char_u *oldval_l = NULL;
550 char_u *oldval_g = NULL;
551 char_u *saved_oldval = NULL;
552 char_u *saved_oldval_l = NULL;
553 char_u *saved_oldval_g = NULL;
554 char_u *saved_newval = NULL;
555#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100556 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +0200557 int value_checked = FALSE;
558
559 if (is_hidden_option(opt_idx)) // don't set hidden option
560 return NULL;
561
Bram Moolenaar7f009df2020-03-16 20:27:38 +0100562 s = vim_strsave(value == NULL ? (char_u *)"" : value);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000563 if (s == NULL)
564 return NULL;
565
566 varp = (char_u **)get_option_varp_scope(opt_idx,
567 (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
568 ? (is_global_local_option(opt_idx)
569 ? OPT_GLOBAL : OPT_LOCAL)
570 : opt_flags);
571 oldval = *varp;
572#if defined(FEAT_EVAL)
573 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +0200574 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000575 oldval_l = *(char_u **)get_option_varp_scope(opt_idx, OPT_LOCAL);
576 oldval_g = *(char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
Bram Moolenaardac13472019-09-16 21:06:21 +0200577 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000578#endif
579 *varp = s;
580
581#if defined(FEAT_EVAL)
582 if (!starting
583# ifdef FEAT_CRYPT
584 && !is_crypt_key_option(opt_idx)
585# endif
586 )
587 {
588 if (oldval_l != NULL)
589 saved_oldval_l = vim_strsave(oldval_l);
590 if (oldval_g != NULL)
591 saved_oldval_g = vim_strsave(oldval_g);
592 saved_oldval = vim_strsave(oldval);
593 saved_newval = vim_strsave(s);
594 }
595#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000596 if ((errmsg = did_set_string_option(opt_idx, varp, oldval, value, errbuf,
Christian Brabandtb39b2402023-11-29 11:34:05 +0100597 errbuflen, opt_flags, OP_NONE, &value_checked)) == NULL)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000598 did_set_option(opt_idx, opt_flags, TRUE, value_checked);
599
600#if defined(FEAT_EVAL)
601 // call autocommand after handling side effects
602 if (errmsg == NULL)
603 trigger_optionset_string(opt_idx, opt_flags,
604 saved_oldval, saved_oldval_l,
605 saved_oldval_g, saved_newval);
606 vim_free(saved_oldval);
607 vim_free(saved_oldval_l);
608 vim_free(saved_oldval_g);
609 vim_free(saved_newval);
610#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100611 return errmsg;
Bram Moolenaardac13472019-09-16 21:06:21 +0200612}
613
614/*
615 * Return TRUE if "val" is a valid 'filetype' name.
616 * Also used for 'syntax' and 'keymap'.
617 */
618 static int
619valid_filetype(char_u *val)
620{
621 return valid_name(val, ".-_");
622}
623
624#ifdef FEAT_STL_OPT
625/*
626 * Check validity of options with the 'statusline' format.
zeertzjq5dc294a2022-04-15 13:17:57 +0100627 * Return an untranslated error message or NULL.
Bram Moolenaardac13472019-09-16 21:06:21 +0200628 */
629 static char *
630check_stl_option(char_u *s)
631{
Bram Moolenaardac13472019-09-16 21:06:21 +0200632 int groupdepth = 0;
Christian Brabandtb39b2402023-11-29 11:34:05 +0100633 static char errbuf[ERR_BUFLEN];
634 int errbuflen = ERR_BUFLEN;
Bram Moolenaardac13472019-09-16 21:06:21 +0200635
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100636 while (*s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200637 {
638 // Check for valid keys after % sequences
639 while (*s && *s != '%')
640 s++;
641 if (!*s)
642 break;
643 s++;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +0000644 if (*s == '%' || *s == STL_TRUNCMARK || *s == STL_SEPARATE)
Bram Moolenaardac13472019-09-16 21:06:21 +0200645 {
646 s++;
647 continue;
648 }
649 if (*s == ')')
650 {
651 s++;
652 if (--groupdepth < 0)
653 break;
654 continue;
655 }
656 if (*s == '-')
657 s++;
658 while (VIM_ISDIGIT(*s))
659 s++;
660 if (*s == STL_USER_HL)
661 continue;
662 if (*s == '.')
663 {
664 s++;
665 while (*s && VIM_ISDIGIT(*s))
666 s++;
667 }
668 if (*s == '(')
669 {
670 groupdepth++;
671 continue;
672 }
673 if (vim_strchr(STL_ALL, *s) == NULL)
674 {
Christian Brabandtb39b2402023-11-29 11:34:05 +0100675 return illegal_char(errbuf, errbuflen, *s);
Bram Moolenaardac13472019-09-16 21:06:21 +0200676 }
677 if (*s == '{')
678 {
zeertzjq5dc294a2022-04-15 13:17:57 +0100679 int reevaluate = (*++s == '%');
shadmansaleh30e3de22021-05-15 17:23:28 +0200680
zeertzjq5dc294a2022-04-15 13:17:57 +0100681 if (reevaluate && *++s == '}')
682 // "}" is not allowed immediately after "%{%"
Christian Brabandtb39b2402023-11-29 11:34:05 +0100683 return illegal_char(errbuf, errbuflen, '}');
shadmansaleh30e3de22021-05-15 17:23:28 +0200684 while ((*s != '}' || (reevaluate && s[-1] != '%')) && *s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200685 s++;
686 if (*s != '}')
zeertzjq5dc294a2022-04-15 13:17:57 +0100687 return e_unclosed_expression_sequence;
Bram Moolenaardac13472019-09-16 21:06:21 +0200688 }
689 }
Bram Moolenaardac13472019-09-16 21:06:21 +0200690 if (groupdepth != 0)
zeertzjq5dc294a2022-04-15 13:17:57 +0100691 return e_unbalanced_groups;
Bram Moolenaardac13472019-09-16 21:06:21 +0200692 return NULL;
693}
694#endif
695
696/*
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +0000697 * Check for a "normal" directory or file name in some options. Disallow a
698 * path separator (slash and/or backslash), wildcards and characters that are
699 * often illegal in a file name. Be more permissive if "secure" is off.
700 */
701 static int
702check_illegal_path_names(int opt_idx, char_u **varp)
703{
704 return (((get_option_flags(opt_idx) & P_NFNAME)
705 && vim_strpbrk(*varp, (char_u *)(secure
706 ? "/\\*?[|;&<>\r\n" : "/\\*?[<>\r\n")) != NULL)
707 || ((get_option_flags(opt_idx) & P_NDNAME)
708 && vim_strpbrk(*varp, (char_u *)"*?[|;&<>\r\n") != NULL));
709}
710
711/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000712 * An option that accepts a list of flags is changed.
713 * e.g. 'viewoptions', 'switchbuf', 'casemap', etc.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000714 */
715 static char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000716did_set_opt_flags(char_u *val, char **values, unsigned *flagp, int list)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000717{
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000718 if (opt_strings_flags(val, values, flagp, list) == FAIL)
719 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000720
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000721 return NULL;
722}
723
724/*
725 * An option that accepts a list of string values is changed.
726 * e.g. 'nrformats', 'scrollopt', 'wildoptions', etc.
727 */
728 static char *
729did_set_opt_strings(char_u *val, char **values, int list)
730{
731 return did_set_opt_flags(val, values, NULL, list);
732}
733
734/*
735 * An option which is a list of flags is set. Valid values are in 'flags'.
736 */
737 static char *
Christian Brabandtb39b2402023-11-29 11:34:05 +0100738did_set_option_listflag(
739 char_u *val,
740 char_u *flags,
741 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +0100742 size_t errbuflen)
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000743{
744 char_u *s;
745
Yegappan Lakshmananc727b192023-03-03 12:26:15 +0000746 for (s = val; *s; ++s)
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000747 if (vim_strchr(flags, *s) == NULL)
Christian Brabandtb39b2402023-11-29 11:34:05 +0100748 return illegal_char(errbuf, errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000749
750 return NULL;
751}
752
753/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200754 * Expand an option that accepts a list of fixed string values with known
755 * number of items.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200756 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200757 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200758expand_set_opt_string(
759 optexpand_T *args,
760 char **values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +0200761 size_t numValues,
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200762 int *numMatches,
763 char_u ***matches)
764{
765 char_u *p;
766 regmatch_T *regmatch = args->oe_regmatch;
767 int include_orig_val = args->oe_include_orig_val;
768 char_u *option_val = args->oe_opt_value;
769
770 // Assume numValues is small since they are fixed enums, so just allocate
771 // upfront instead of needing two passes to calculate output size.
772 *matches = ALLOC_MULT(char_u *, numValues + 1);
773 if (*matches == NULL)
774 return FAIL;
775
776 int count = 0;
777
778 if (include_orig_val && *option_val != NUL)
779 {
780 p = vim_strsave(option_val);
781 if (p == NULL)
782 {
783 VIM_CLEAR(*matches);
784 return FAIL;
785 }
786 (*matches)[count++] = p;
787 }
788
789 for (char **val = values; *val != NULL; val++)
790 {
791 if (include_orig_val && *option_val != NUL)
792 {
793 if (STRCMP((char_u*)*val, option_val) == 0)
794 continue;
795 }
796 if (vim_regexec(regmatch, (char_u*)(*val), (colnr_T)0))
797 {
798 p = vim_strsave((char_u*)*val);
799 if (p == NULL)
800 {
801 if (count == 0)
802 {
803 VIM_CLEAR(*matches);
804 return FAIL;
805 }
806 else
807 break;
808 }
809 (*matches)[count++] = p;
810 }
811 }
812 if (count == 0)
813 {
814 VIM_CLEAR(*matches);
815 return FAIL;
816 }
817 *numMatches = count;
818 return OK;
819}
820
821static char_u *set_opt_callback_orig_option = NULL;
822static char_u *((*set_opt_callback_func)(expand_T *, int));
823
824/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200825 * Callback used by expand_set_opt_generic to also include the original value
826 * as the first item.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200827 */
828 static char_u *
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200829expand_set_opt_generic_cb(expand_T *xp, int idx)
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200830{
831 if (idx == 0)
832 {
833 if (set_opt_callback_orig_option != NULL)
834 return set_opt_callback_orig_option;
835 else
836 return (char_u *)""; // empty strings are ignored
837 }
838 return set_opt_callback_func(xp, idx - 1);
839}
840
841/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200842 * Expand an option with a callback that iterates through a list of possible
843 * names using an index.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200844 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200845 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200846expand_set_opt_generic(
847 optexpand_T *args,
848 char_u *((*func)(expand_T *, int)),
849 int *numMatches,
850 char_u ***matches)
851{
852 int ret;
853
854 set_opt_callback_orig_option = args->oe_include_orig_val ?
855 args->oe_opt_value : NULL;
856 set_opt_callback_func = func;
857
858 ret = ExpandGeneric(
859 (char_u*)"", // not using fuzzy as currently EXPAND_STRING_SETTING doesn't use it
860 args->oe_xp,
861 args->oe_regmatch,
862 matches,
863 numMatches,
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200864 expand_set_opt_generic_cb,
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200865 FALSE);
866
867 set_opt_callback_orig_option = NULL;
868 set_opt_callback_func = NULL;
869 return ret;
870}
871
Christian Brabandt9960ebc2023-10-05 22:17:09 +0200872# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200873static garray_T *expand_cb_ga;
874static optexpand_T *expand_cb_args;
875
876/*
877 * Callback provided to a function in expand_set_opt_callback. Will perform
878 * regex matching against the value and add to the list.
879 *
880 * Returns OK usually. Returns FAIL if it failed to allocate memory, and the
881 * caller should terminate the enumeration.
882 */
883 static int
884expand_set_opt_callback_cb(char_u *val)
885{
886 regmatch_T *regmatch = expand_cb_args->oe_regmatch;
887 expand_T *xp = expand_cb_args->oe_xp;
888 garray_T *ga = expand_cb_ga;
889 char_u *str;
890
891 if (val == NULL || *val == NUL)
892 return OK;
893
894 if (xp->xp_pattern[0] != NUL &&
895 !vim_regexec(regmatch, val, (colnr_T)0))
896 return OK;
897
898 str = vim_strsave_escaped(val, (char_u *)" \t\\");
899
900 if (str == NULL)
901 return FAIL;
902
903 if (ga_grow(ga, 1) == FAIL)
904 {
905 vim_free(str);
906 return FAIL;
907 }
908
909 ((char_u **)ga->ga_data)[ga->ga_len] = str;
910 ++ga->ga_len;
911 return OK;
912}
913
914/*
915 * Expand an option with a provided function that takes a callback. The
916 * function will enumerate through all options and call the callback to add it
917 * to the list.
918 *
919 * "func" is the enumerator function that will generate the list of options.
920 * "func_params" is a single parameter that will be passed to func.
921 */
922 static int
923expand_set_opt_callback(
924 optexpand_T *args,
925 void (*func)(optexpand_T *, void* params, int (*cb)(char_u *val)),
926 void *func_params,
927 int *numMatches,
928 char_u ***matches)
929{
930 garray_T ga;
931 int include_orig_val = args->oe_include_orig_val;
932 char_u *option_val = args->oe_opt_value;
933
934 ga_init2(&ga, sizeof(char *), 30);
935
936 if (include_orig_val && *option_val != NUL)
937 {
938 char_u *p = vim_strsave(option_val);
939 if (p == NULL)
940 return FAIL;
941 if (ga_grow(&ga, 1) == FAIL)
942 {
943 vim_free(p);
944 return FAIL;
945 }
946 ((char_u **)ga.ga_data)[ga.ga_len] = p;
947 ++ga.ga_len;
948 }
949
950 expand_cb_ga = &ga;
951 expand_cb_args = args;
952
953 func(args, func_params, expand_set_opt_callback_cb);
954
955 expand_cb_ga = NULL;
956 expand_cb_args = NULL;
957
958 *matches = ga.ga_data;
959 *numMatches = ga.ga_len;
960 return OK;
961}
Christian Brabandt9960ebc2023-10-05 22:17:09 +0200962#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200963
964/*
965 * Expand an option which is a list of flags.
966 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200967 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200968expand_set_opt_listflag(
969 optexpand_T *args,
970 char_u *flags,
971 int *numMatches,
972 char_u ***matches)
973{
974 char_u *p;
975 char_u *option_val = args->oe_opt_value;
976 char_u *cmdline_val = args->oe_set_arg;
977 int append = args->oe_append;
978 int include_orig_val = args->oe_include_orig_val && (*option_val != NUL);
979
Yee Cheng Chin6d113472023-10-02 21:38:39 +0200980 size_t num_flags = STRLEN(flags);
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200981
982 // Assume we only have small number of flags, so just allocate max size.
983 *matches = ALLOC_MULT(char_u *, num_flags + 1);
984 if (*matches == NULL)
985 return FAIL;
986
987 int count = 0;
988
989 if (include_orig_val)
990 {
991 p = vim_strsave(option_val);
992 if (p == NULL)
993 {
994 VIM_CLEAR(*matches);
995 return FAIL;
996 }
997 (*matches)[count++] = p;
998 }
999
1000 for (char_u *flag = flags; *flag != NUL; flag++)
1001 {
1002 if (append && vim_strchr(option_val, *flag) != NULL)
1003 continue;
1004
1005 if (vim_strchr(cmdline_val, *flag) == NULL)
1006 {
1007 if (include_orig_val
1008 && option_val[1] == NUL
1009 && *flag == option_val[0])
1010 {
1011 // This value is already used as the first choice as it's the
1012 // existing flag. Just skip it to avoid duplicate.
1013 continue;
1014 }
1015 p = vim_strnsave(flag, 1);
1016 if (p == NULL)
1017 {
1018 if (count == 0)
1019 {
1020 VIM_CLEAR(*matches);
1021 return FAIL;
1022 }
1023 else
1024 break;
1025 }
1026 (*matches)[count++] = p;
1027 }
1028 }
1029
1030 if (count == 0)
1031 {
1032 VIM_CLEAR(*matches);
1033 return FAIL;
1034 }
1035 *numMatches = count;
1036 return OK;
1037}
1038
1039/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001040 * The 'ambiwidth' option is changed.
1041 */
1042 char *
1043did_set_ambiwidth(optset_T *args UNUSED)
1044{
1045 if (check_opt_strings(p_ambw, p_ambw_values, FALSE) != OK)
1046 return e_invalid_argument;
1047
1048 return check_chars_options();
1049}
1050
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001051 int
1052expand_set_ambiwidth(optexpand_T *args, int *numMatches, char_u ***matches)
1053{
1054 return expand_set_opt_string(
1055 args,
1056 p_ambw_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001057 ARRAY_LENGTH(p_ambw_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001058 numMatches,
1059 matches);
1060}
1061
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001062/*
1063 * The 'background' option is changed.
1064 */
1065 char *
Gregory Anders83ad2722024-01-03 19:48:51 +01001066did_set_background(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001067{
1068 if (check_opt_strings(p_bg, p_bg_values, FALSE) == FAIL)
1069 return e_invalid_argument;
1070
Gregory Anders83ad2722024-01-03 19:48:51 +01001071 if (args->os_oldval.string != NULL && args->os_oldval.string[0] == *p_bg)
1072 // Value was not changed
1073 return NULL;
1074
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001075#ifdef FEAT_EVAL
1076 int dark = (*p_bg == 'd');
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001077#endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001078
1079 init_highlight(FALSE, FALSE);
1080
1081#ifdef FEAT_EVAL
1082 if (dark != (*p_bg == 'd')
1083 && get_var_value((char_u *)"g:colors_name") != NULL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001084 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001085 // The color scheme must have set 'background' back to another
1086 // value, that's not what we want here. Disable the color
1087 // scheme and set the colors again.
1088 do_unlet((char_u *)"g:colors_name", TRUE);
1089 free_string_option(p_bg);
1090 p_bg = vim_strsave((char_u *)(dark ? "dark" : "light"));
1091 check_string_option(&p_bg);
1092 init_highlight(FALSE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001093 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001094#endif
1095#ifdef FEAT_TERMINAL
1096 term_update_colors_all();
1097#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001098
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001099 return NULL;
1100}
1101
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001102 int
1103expand_set_background(optexpand_T *args, int *numMatches, char_u ***matches)
1104{
1105 return expand_set_opt_string(
1106 args,
1107 p_bg_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001108 ARRAY_LENGTH(p_bg_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001109 numMatches,
1110 matches);
1111}
1112
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001113/*
1114 * The 'backspace' option is changed.
1115 */
1116 char *
1117did_set_backspace(optset_T *args UNUSED)
1118{
1119 if (VIM_ISDIGIT(*p_bs))
1120 {
1121 if (*p_bs > '3' || p_bs[1] != NUL)
1122 return e_invalid_argument;
1123 }
1124 else if (check_opt_strings(p_bs, p_bs_values, TRUE) != OK)
1125 return e_invalid_argument;
1126
1127 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001128}
1129
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001130 int
1131expand_set_backspace(optexpand_T *args, int *numMatches, char_u ***matches)
1132{
1133 return expand_set_opt_string(
1134 args,
1135 p_bs_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001136 ARRAY_LENGTH(p_bs_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001137 numMatches,
1138 matches);
1139}
1140
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001141/*
1142 * The 'backupcopy' option is changed.
1143 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001144 char *
1145did_set_backupcopy(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001146{
1147 char_u *bkc = p_bkc;
1148 unsigned int *flags = &bkc_flags;
1149 char *errmsg = NULL;
1150
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001151 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001152 {
1153 bkc = curbuf->b_p_bkc;
1154 flags = &curbuf->b_bkc_flags;
1155 }
zeertzjq46dcd842024-11-03 09:10:50 +01001156 else if (!(args->os_flags & OPT_GLOBAL))
1157 // When using :set, clear the local flags.
1158 curbuf->b_bkc_flags = 0;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001159
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001160 if ((args->os_flags & OPT_LOCAL) && *bkc == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001161 // make the local value empty: use the global value
1162 *flags = 0;
1163 else
1164 {
1165 if (opt_strings_flags(bkc, p_bkc_values, flags, TRUE) != OK)
1166 errmsg = e_invalid_argument;
1167 if ((((int)*flags & BKC_AUTO) != 0)
1168 + (((int)*flags & BKC_YES) != 0)
1169 + (((int)*flags & BKC_NO) != 0) != 1)
1170 {
1171 // Must have exactly one of "auto", "yes" and "no".
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001172 (void)opt_strings_flags(args->os_oldval.string, p_bkc_values,
1173 flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001174 errmsg = e_invalid_argument;
1175 }
1176 }
1177
1178 return errmsg;
1179}
1180
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001181 int
1182expand_set_backupcopy(optexpand_T *args, int *numMatches, char_u ***matches)
1183{
1184 return expand_set_opt_string(
1185 args,
1186 p_bkc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001187 ARRAY_LENGTH(p_bkc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001188 numMatches,
1189 matches);
1190}
1191
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001192/*
1193 * The 'backupext' or the 'patchmode' option is changed.
1194 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001195 char *
1196did_set_backupext_or_patchmode(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001197{
1198 if (STRCMP(*p_bex == '.' ? p_bex + 1 : p_bex,
1199 *p_pm == '.' ? p_pm + 1 : p_pm) == 0)
1200 return e_backupext_and_patchmode_are_equal;
1201
1202 return NULL;
1203}
1204
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001205/*
1206 * The 'belloff' option is changed.
1207 */
1208 char *
1209did_set_belloff(optset_T *args UNUSED)
1210{
1211 return did_set_opt_flags(p_bo, p_bo_values, &bo_flags, TRUE);
1212}
1213
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001214 int
1215expand_set_belloff(optexpand_T *args, int *numMatches, char_u ***matches)
1216{
1217 return expand_set_opt_string(
1218 args,
1219 p_bo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001220 ARRAY_LENGTH(p_bo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001221 numMatches,
1222 matches);
1223}
1224
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001225#if defined(FEAT_LINEBREAK) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001226/*
zeertzjqeac3fdc2024-02-03 18:08:09 +01001227 * The 'breakat' option is changed.
1228 */
1229 char *
1230did_set_breakat(optset_T *args UNUSED)
1231{
1232 char_u *p;
1233 int i;
1234
1235 for (i = 0; i < 256; i++)
1236 breakat_flags[i] = FALSE;
1237
1238 if (p_breakat != NULL)
1239 for (p = p_breakat; *p; p++)
1240 breakat_flags[*p] = TRUE;
1241
1242 return NULL;
1243}
1244
1245/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001246 * The 'breakindentopt' option is changed.
1247 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001248 char *
Millyb38700a2024-10-22 22:59:39 +02001249did_set_breakindentopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001250{
Millyb38700a2024-10-22 22:59:39 +02001251 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001252
Millyb38700a2024-10-22 22:59:39 +02001253 if (briopt_check(*varp, varp == &curwin->w_p_briopt ? curwin : NULL)
1254 == FAIL)
1255 return e_invalid_argument;
1256
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001257 // list setting requires a redraw
Millyb38700a2024-10-22 22:59:39 +02001258 if (varp == &curwin->w_p_briopt && curwin->w_briopt_list)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001259 redraw_all_later(UPD_NOT_VALID);
1260
Millyb38700a2024-10-22 22:59:39 +02001261 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001262}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001263
1264 int
1265expand_set_breakindentopt(optexpand_T *args, int *numMatches, char_u ***matches)
1266{
1267 return expand_set_opt_string(
1268 args,
1269 p_briopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001270 ARRAY_LENGTH(p_briopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001271 numMatches,
1272 matches);
1273}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001274#endif
1275
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001276#if defined(FEAT_BROWSE) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001277/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001278 * The 'browsedir' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001279 */
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00001280 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001281did_set_browsedir(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001282{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001283 if (check_opt_strings(p_bsdir, p_bsdir_values, FALSE) != OK
1284 && !mch_isdir(p_bsdir))
1285 return e_invalid_argument;
1286
1287 return NULL;
1288}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001289
1290 int
1291expand_set_browsedir(optexpand_T *args, int *numMatches, char_u ***matches)
1292{
1293 return expand_set_opt_string(
1294 args,
1295 p_bsdir_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001296 ARRAY_LENGTH(p_bsdir_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001297 numMatches,
1298 matches);
1299}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001300#endif
1301
1302/*
1303 * The 'bufhidden' option is changed.
1304 */
1305 char *
1306did_set_bufhidden(optset_T *args UNUSED)
1307{
1308 return did_set_opt_strings(curbuf->b_p_bh, p_bufhidden_values, FALSE);
1309}
1310
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001311 int
1312expand_set_bufhidden(optexpand_T *args, int *numMatches, char_u ***matches)
1313{
1314 return expand_set_opt_string(
1315 args,
1316 p_bufhidden_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001317 ARRAY_LENGTH(p_bufhidden_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001318 numMatches,
1319 matches);
1320}
1321
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001322/*
1323 * The 'buftype' option is changed.
1324 */
1325 char *
1326did_set_buftype(optset_T *args UNUSED)
1327{
1328 if (check_opt_strings(curbuf->b_p_bt, p_buftype_values, FALSE) != OK)
1329 return e_invalid_argument;
1330
1331 if (curwin->w_status_height)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001332 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001333 curwin->w_redr_status = TRUE;
1334 redraw_later(UPD_VALID);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001335 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001336 curbuf->b_help = (curbuf->b_p_bt[0] == 'h');
1337 redraw_titles();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001338
1339 return NULL;
1340}
1341
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001342 int
1343expand_set_buftype(optexpand_T *args, int *numMatches, char_u ***matches)
1344{
1345 return expand_set_opt_string(
1346 args,
1347 p_buftype_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001348 ARRAY_LENGTH(p_buftype_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001349 numMatches,
1350 matches);
1351}
1352
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001353/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001354 * The 'casemap' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001355 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001356 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001357did_set_casemap(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001358{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001359 return did_set_opt_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
1360}
1361
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001362 int
1363expand_set_casemap(optexpand_T *args, int *numMatches, char_u ***matches)
1364{
1365 return expand_set_opt_string(
1366 args,
1367 p_cmp_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001368 ARRAY_LENGTH(p_cmp_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001369 numMatches,
1370 matches);
1371}
1372
1373#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1374 int
1375expand_set_clipboard(optexpand_T *args, int *numMatches, char_u ***matches)
1376{
1377 return expand_set_opt_string(
1378 args,
1379 p_cb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001380 ARRAY_LENGTH(p_cb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001381 numMatches,
1382 matches);
1383}
1384#endif
1385
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001386/*
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001387 * The global 'listchars' or 'fillchars' option is changed.
1388 */
1389 static char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01001390did_set_global_listfillchars(char_u *val, int opt_lcs, int opt_flags,
1391 char *errbuf, size_t errbuflen)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001392{
1393 char *errmsg = NULL;
1394 char_u **local_ptr = opt_lcs ? &curwin->w_p_lcs : &curwin->w_p_fcs;
1395
1396 // only apply the global value to "curwin" when it does not have a
1397 // local value
1398 if (opt_lcs)
1399 errmsg = set_listchars_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 else
1403 errmsg = set_fillchars_option(curwin, val,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001404 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL),
1405 errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001406 if (errmsg != NULL)
1407 return errmsg;
1408
1409 tabpage_T *tp;
1410 win_T *wp;
1411
1412 // If the current window is set to use the global
1413 // 'listchars'/'fillchars' value, clear the window-local value.
1414 if (!(opt_flags & OPT_GLOBAL))
1415 clear_string_option(local_ptr);
1416 FOR_ALL_TAB_WINDOWS(tp, wp)
1417 {
1418 // If the current window has a local value need to apply it
1419 // again, it was changed when setting the global value.
1420 // If no error was returned above, we don't expect an error
1421 // here, so ignore the return value.
1422 if (opt_lcs)
1423 {
1424 if (*wp->w_p_lcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001425 (void)set_listchars_option(wp, wp->w_p_lcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001426 }
1427 else
1428 {
1429 if (*wp->w_p_fcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001430 (void)set_fillchars_option(wp, wp->w_p_fcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001431 }
1432 }
1433
1434 redraw_all_later(UPD_NOT_VALID);
1435
1436 return NULL;
1437}
1438
1439/*
1440 * The 'fillchars' option or the 'listchars' option is changed.
1441 */
1442 char *
1443did_set_chars_option(optset_T *args)
1444{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001445 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001446 char *errmsg = NULL;
1447
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001448 if ( varp == &p_lcs // global 'listchars'
1449 || varp == &p_fcs) // global 'fillchars'
1450 errmsg = did_set_global_listfillchars(*varp, varp == &p_lcs,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001451 args->os_flags, args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001452 else if (varp == &curwin->w_p_lcs) // local 'listchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001453 errmsg = set_listchars_option(curwin, *varp, TRUE,
1454 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001455 else if (varp == &curwin->w_p_fcs) // local 'fillchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001456 errmsg = set_fillchars_option(curwin, *varp, TRUE,
1457 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001458
1459 return errmsg;
1460}
1461
1462/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001463 * Expand 'fillchars' or 'listchars' option value.
1464 */
1465 int
1466expand_set_chars_option(optexpand_T *args, int *numMatches, char_u ***matches)
1467{
1468 char_u **varp = (char_u **)args->oe_varp;
1469 int is_lcs = (varp == &p_lcs || varp == &curwin->w_p_lcs);
1470 return expand_set_opt_generic(
1471 args,
1472 is_lcs ? get_listchars_name : get_fillchars_name,
1473 numMatches,
1474 matches);
1475}
1476
1477/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001478 * The 'cinoptions' option is changed.
1479 */
1480 char *
1481did_set_cinoptions(optset_T *args UNUSED)
1482{
1483 // TODO: recognize errors
1484 parse_cino(curbuf);
1485
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001486 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001487}
1488
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00001489#if defined(FEAT_SYN_HL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001490/*
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001491 * The 'colorcolumn' option is changed.
1492 */
1493 char *
Millya441a3e2024-10-22 22:43:01 +02001494did_set_colorcolumn(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001495{
Millya441a3e2024-10-22 22:43:01 +02001496 char_u **varp = (char_u **)args->os_varp;
1497
1498 return check_colorcolumn(*varp, varp == &curwin->w_p_cc ? curwin : NULL);
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001499}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001500#endif
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001501
1502/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001503 * The 'comments' option is changed.
1504 */
1505 char *
1506did_set_comments(optset_T *args)
1507{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001508 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001509 char_u *s;
1510 char *errmsg = NULL;
1511
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001512 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001513 {
1514 while (*s && *s != ':')
1515 {
1516 if (vim_strchr((char_u *)COM_ALL, *s) == NULL
1517 && !VIM_ISDIGIT(*s) && *s != '-')
1518 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01001519 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001520 break;
1521 }
1522 ++s;
1523 }
1524 if (*s++ == NUL)
1525 errmsg = e_missing_colon;
1526 else if (*s == ',' || *s == NUL)
1527 errmsg = e_zero_length_string;
1528 if (errmsg != NULL)
1529 break;
1530 while (*s && *s != ',')
1531 {
1532 if (*s == '\\' && s[1] != NUL)
1533 ++s;
1534 ++s;
1535 }
1536 s = skip_to_option_part(s);
1537 }
1538
1539 return errmsg;
1540}
1541
1542#if defined(FEAT_FOLDING) || defined(PROTO)
1543/*
1544 * The 'commentstring' option is changed.
1545 */
1546 char *
1547did_set_commentstring(optset_T *args)
1548{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001549 char_u **varp = (char_u **)args->os_varp;
1550
1551 if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001552 return e_commentstring_must_be_empty_or_contain_str;
1553
1554 return NULL;
1555}
1556#endif
1557
1558/*
Girish Palyacbe53192025-04-14 22:13:15 +02001559 * Check if value for 'complete' is valid when 'complete' option is changed.
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001560 */
1561 char *
1562did_set_complete(optset_T *args)
1563{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001564 char_u **varp = (char_u **)args->os_varp;
Girish Palya0ac1eb32025-04-16 20:18:33 +02001565 char_u *p, *t;
Girish Palyacbe53192025-04-14 22:13:15 +02001566 char_u buffer[LSIZE];
1567 char_u *buf_ptr;
Girish Palya0ac1eb32025-04-16 20:18:33 +02001568 char_u char_before = NUL;
Girish Palyacbe53192025-04-14 22:13:15 +02001569 int escape;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001570
Girish Palyacbe53192025-04-14 22:13:15 +02001571 for (p = *varp; *p; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001572 {
Girish Palyacbe53192025-04-14 22:13:15 +02001573 vim_memset(buffer, 0, LSIZE);
1574 buf_ptr = buffer;
1575 escape = 0;
1576
1577 // Extract substring while handling escaped commas
1578 while (*p && (*p != ',' || escape) && buf_ptr < (buffer + LSIZE - 1))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001579 {
Girish Palyacbe53192025-04-14 22:13:15 +02001580 if (*p == '\\' && *(p + 1) == ',')
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001581 {
Girish Palyacbe53192025-04-14 22:13:15 +02001582 escape = 1; // Mark escape mode
1583 p++; // Skip '\'
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001584 }
1585 else
1586 {
Girish Palyacbe53192025-04-14 22:13:15 +02001587 escape = 0;
1588 *buf_ptr++ = *p;
1589 }
1590 p++;
1591 }
1592 *buf_ptr = NUL;
1593
1594 if (vim_strchr((char_u *)".wbuksid]tUfo", *buffer) == NULL)
1595 return illegal_char(args->os_errbuf, args->os_errbuflen, *buffer);
1596
Girish Palya0ac1eb32025-04-16 20:18:33 +02001597 if (vim_strchr((char_u *)"ksf", *buffer) == NULL && *(buffer + 1) != NUL
1598 && *(buffer + 1) != '^')
1599 char_before = *buffer;
1600 else
1601 {
1602 // Test for a number after '^'
1603 if ((t = vim_strchr(buffer, '^')) != NULL)
1604 {
1605 *t++ = NUL;
1606 if (!*t)
1607 char_before = '^';
1608 else
1609 {
1610 for (; *t; t++)
1611 {
1612 if (!vim_isdigit(*t))
1613 {
1614 char_before = '^';
1615 break;
1616 }
1617 }
1618 }
1619 }
1620 }
1621 if (char_before != NUL)
Girish Palyacbe53192025-04-14 22:13:15 +02001622 {
1623 if (args->os_errbuf)
1624 {
1625 vim_snprintf((char *)args->os_errbuf, args->os_errbuflen,
Girish Palya0ac1eb32025-04-16 20:18:33 +02001626 _(e_illegal_character_after_chr), char_before);
Girish Palyacbe53192025-04-14 22:13:15 +02001627 return args->os_errbuf;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001628 }
Girish Palya0ac1eb32025-04-16 20:18:33 +02001629 return NULL;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001630 }
Girish Palyacbe53192025-04-14 22:13:15 +02001631 // Skip comma and spaces
1632 while (*p == ',' || *p == ' ')
1633 p++;
1634 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001635 return NULL;
1636}
1637
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001638 int
1639expand_set_complete(optexpand_T *args, int *numMatches, char_u ***matches)
1640{
1641 static char *(p_cpt_values[]) = {
Girish Palyacbe53192025-04-14 22:13:15 +02001642 ".", "w", "b", "u", "k", "kspell", "s", "i", "d", "]", "t", "U", "f", "o",
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001643 NULL};
1644 return expand_set_opt_string(
1645 args,
1646 p_cpt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001647 ARRAY_LENGTH(p_cpt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001648 numMatches,
1649 matches);
1650}
1651
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001652/*
1653 * The 'completeopt' option is changed.
1654 */
1655 char *
1656did_set_completeopt(optset_T *args UNUSED)
1657{
zeertzjq529b9ad2024-06-05 20:27:06 +02001658 char_u *cot = p_cot;
1659 unsigned *flags = &cot_flags;
1660
1661 if (args->os_flags & OPT_LOCAL)
1662 {
1663 cot = curbuf->b_p_cot;
1664 flags = &curbuf->b_cot_flags;
1665 }
zeertzjq46dcd842024-11-03 09:10:50 +01001666 else if (!(args->os_flags & OPT_GLOBAL))
1667 // When using :set, clear the local flags.
1668 curbuf->b_cot_flags = 0;
zeertzjq529b9ad2024-06-05 20:27:06 +02001669
1670 if (check_opt_strings(cot, p_cot_values, TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001671 return e_invalid_argument;
1672
zeertzjq529b9ad2024-06-05 20:27:06 +02001673 if (opt_strings_flags(cot, p_cot_values, flags, TRUE) != OK)
1674 return e_invalid_argument;
1675
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001676 return NULL;
1677}
1678
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001679 int
1680expand_set_completeopt(optexpand_T *args, int *numMatches, char_u ***matches)
1681{
1682 return expand_set_opt_string(
1683 args,
1684 p_cot_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001685 ARRAY_LENGTH(p_cot_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001686 numMatches,
1687 matches);
1688}
1689
glepnir6a89c942024-10-01 20:32:12 +02001690/*
glepnirf31cfa22025-03-06 21:59:13 +01001691 * The 'completefuzzycollect' option is changed.
1692 */
1693 char *
1694did_set_completefuzzycollect(optset_T *args UNUSED)
1695{
1696 if (opt_strings_flags(p_cfc, p_cfc_values, &cfc_flags, TRUE) != OK)
1697 return e_invalid_argument;
1698 return NULL;
1699}
1700
zeertzjq53d59ec2025-03-07 19:09:09 +01001701 int
1702expand_set_completefuzzycollect(
1703 optexpand_T *args,
1704 int *numMatches,
1705 char_u ***matches)
1706{
1707 return expand_set_opt_string(
1708 args,
1709 p_cfc_values,
1710 ARRAY_LENGTH(p_cfc_values) - 1,
1711 numMatches,
1712 matches);
1713}
1714
glepnirf31cfa22025-03-06 21:59:13 +01001715/*
glepnir6a89c942024-10-01 20:32:12 +02001716 * The 'completeitemalign' option is changed.
1717 */
1718 char *
1719did_set_completeitemalign(optset_T *args UNUSED)
1720{
1721 char_u *p = p_cia;
1722 unsigned new_cia_flags = 0;
1723 int seen[3] = { FALSE, FALSE, FALSE };
1724 int count = 0;
1725 char_u buf[10];
1726
1727 while (*p)
1728 {
1729 copy_option_part(&p, buf, sizeof(buf), ",");
1730 if (count >= 3)
1731 return e_invalid_argument;
1732
1733 if (STRCMP(buf, "abbr") == 0)
1734 {
1735 if (seen[CPT_ABBR])
1736 return e_invalid_argument;
1737 new_cia_flags = new_cia_flags * 10 + CPT_ABBR;
1738 seen[CPT_ABBR] = TRUE;
1739 count++;
1740 }
1741 else if (STRCMP(buf, "kind") == 0)
1742 {
1743 if (seen[CPT_KIND])
1744 return e_invalid_argument;
1745 new_cia_flags = new_cia_flags * 10 + CPT_KIND;
1746 seen[CPT_KIND] = TRUE;
1747 count++;
1748 }
1749 else if (STRCMP(buf, "menu") == 0)
1750 {
1751 if (seen[CPT_MENU])
1752 return e_invalid_argument;
1753 new_cia_flags = new_cia_flags * 10 + CPT_MENU;
1754 seen[CPT_MENU] = TRUE;
1755 count++;
1756 }
1757 else
1758 return e_invalid_argument;
1759 }
1760 if (new_cia_flags == 0 || count != 3)
1761 return e_invalid_argument;
1762
1763 cia_flags = new_cia_flags;
1764 return NULL;
1765}
1766
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001767#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1768/*
1769 * The 'completepopup' option is changed.
1770 */
1771 char *
1772did_set_completepopup(optset_T *args UNUSED)
1773{
1774 if (parse_completepopup(NULL) == FAIL)
1775 return e_invalid_argument;
1776
1777 popup_close_info();
1778 return NULL;
1779}
1780#endif
1781
1782#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1783/*
1784 * The 'completeslash' option is changed.
1785 */
1786 char *
1787did_set_completeslash(optset_T *args UNUSED)
1788{
1789 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1790 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
1791 return e_invalid_argument;
1792
1793 return NULL;
1794}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001795
1796 int
1797expand_set_completeslash(optexpand_T *args, int *numMatches, char_u ***matches)
1798{
1799 return expand_set_opt_string(
1800 args,
1801 p_csl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001802 ARRAY_LENGTH(p_csl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001803 numMatches,
1804 matches);
1805}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001806#endif
1807
1808#if defined(FEAT_CONCEAL) || defined(PROTO)
1809/*
1810 * The 'concealcursor' option is changed.
1811 */
1812 char *
1813did_set_concealcursor(optset_T *args)
1814{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001815 char_u **varp = (char_u **)args->os_varp;
1816
Christian Brabandtb39b2402023-11-29 11:34:05 +01001817 return did_set_option_listflag(*varp, (char_u *)COCU_ALL, args->os_errbuf,
1818 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001819}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001820
1821 int
1822expand_set_concealcursor(optexpand_T *args, int *numMatches, char_u ***matches)
1823{
1824 return expand_set_opt_listflag(args, (char_u*)COCU_ALL, numMatches, matches);
1825}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001826#endif
1827
1828/*
1829 * The 'cpoptions' option is changed.
1830 */
1831 char *
1832did_set_cpoptions(optset_T *args)
1833{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001834 char_u **varp = (char_u **)args->os_varp;
1835
Christian Brabandtb39b2402023-11-29 11:34:05 +01001836 return did_set_option_listflag(*varp, (char_u *)CPO_ALL, args->os_errbuf,
1837 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001838}
1839
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001840 int
1841expand_set_cpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
1842{
1843 return expand_set_opt_listflag(args, (char_u*)CPO_ALL, numMatches, matches);
1844}
1845
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001846#if defined(FEAT_CRYPT) || defined(PROTO)
1847/*
1848 * The 'cryptkey' option is changed.
1849 */
1850 char *
1851did_set_cryptkey(optset_T *args)
1852{
1853 // Make sure the ":set" command doesn't show the new value in the
1854 // history.
1855 remove_key_from_history();
1856
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02001857 if (args->os_op != OP_NONE)
1858 // Don't allow set+=/-=/^= as they can allow for substring guessing
1859 return e_invalid_argument;
1860
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001861 if (STRCMP(curbuf->b_p_key, args->os_oldval.string) != 0)
1862 {
1863 // Need to update the swapfile.
1864 ml_set_crypt_key(curbuf, args->os_oldval.string,
1865 *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
1866 changed_internal();
1867 }
Christian Brabandt19e6c4f2023-06-27 18:57:10 +01001868# ifdef FEAT_SODIUM
1869 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +02001870 crypt_sodium_lock_key(args->os_newval.string);
Christian Brabandt19e6c4f2023-06-27 18:57:10 +01001871# endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001872
1873 return NULL;
1874}
1875
1876/*
1877 * The 'cryptmethod' option is changed.
1878 */
1879 char *
1880did_set_cryptmethod(optset_T *args)
1881{
1882 char_u *p;
1883 char_u *s;
1884
1885 if (args->os_flags & OPT_LOCAL)
1886 p = curbuf->b_p_cm;
1887 else
1888 p = p_cm;
1889 if (check_opt_strings(p, p_cm_values, TRUE) != OK)
1890 return e_invalid_argument;
1891 else if (crypt_self_test() == FAIL)
1892 return e_invalid_argument;
1893
1894 // When setting the global value to empty, make it "zip".
1895 if (*p_cm == NUL)
1896 {
1897 free_string_option(p_cm);
1898 p_cm = vim_strsave((char_u *)"zip");
1899 }
1900 // When using ":set cm=name" the local value is going to be empty.
1901 // Do that here, otherwise the crypt functions will still use the
1902 // local value.
1903 if ((args->os_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1904 {
1905 free_string_option(curbuf->b_p_cm);
1906 curbuf->b_p_cm = empty_option;
1907 }
1908
1909 // Need to update the swapfile when the effective method changed.
1910 // Set "s" to the effective old value, "p" to the effective new
1911 // method and compare.
1912 if ((args->os_flags & OPT_LOCAL) && *args->os_oldval.string == NUL)
1913 s = p_cm; // was previously using the global value
1914 else
1915 s = args->os_oldval.string;
1916 if (*curbuf->b_p_cm == NUL)
1917 p = p_cm; // is now using the global value
1918 else
1919 p = curbuf->b_p_cm;
1920 if (STRCMP(s, p) != 0)
1921 ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1922
1923 // If the global value changes need to update the swapfile for all
1924 // buffers using that value.
1925 if ((args->os_flags & OPT_GLOBAL)
1926 && STRCMP(p_cm, args->os_oldval.string) != 0)
1927 {
1928 buf_T *buf;
1929
1930 FOR_ALL_BUFFERS(buf)
1931 if (buf != curbuf && *buf->b_p_cm == NUL)
1932 ml_set_crypt_key(buf, buf->b_p_key, args->os_oldval.string);
1933 }
1934 return NULL;
1935}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001936
1937 int
1938expand_set_cryptmethod(optexpand_T *args, int *numMatches, char_u ***matches)
1939{
1940 return expand_set_opt_string(
1941 args,
1942 p_cm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001943 ARRAY_LENGTH(p_cm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001944 numMatches,
1945 matches);
1946}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001947#endif
1948
1949#if (defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1950/*
1951 * The 'cscopequickfix' option is changed.
1952 */
1953 char *
1954did_set_cscopequickfix(optset_T *args UNUSED)
1955{
1956 char_u *p;
1957
1958 if (p_csqf == NULL)
1959 return NULL;
1960
1961 p = p_csqf;
1962 while (*p != NUL)
1963 {
1964 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
1965 || p[1] == NUL
1966 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
1967 || (p[2] != NUL && p[2] != ','))
1968 return e_invalid_argument;
1969 else if (p[2] == NUL)
1970 break;
1971 else
1972 p += 3;
1973 }
1974
1975 return NULL;
1976}
1977#endif
1978
1979#if defined(FEAT_SYN_HL) || defined(PROTO)
1980/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001981 * The 'cursorlineopt' option is changed.
1982 */
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001983 char *
1984did_set_cursorlineopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001985{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001986 char_u **varp = (char_u **)args->os_varp;
1987
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001988 // This could be changed to use opt_strings_flags() instead.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001989 if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001990 return e_invalid_argument;
1991
1992 return NULL;
1993}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001994
1995 int
1996expand_set_cursorlineopt(optexpand_T *args, int *numMatches, char_u ***matches)
1997{
1998 return expand_set_opt_string(
1999 args,
2000 p_culopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002001 ARRAY_LENGTH(p_culopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002002 numMatches,
2003 matches);
2004}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002005#endif
2006
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002007/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002008 * The 'debug' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002009 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002010 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002011did_set_debug(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002012{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002013 return did_set_opt_strings(p_debug, p_debug_values, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002014}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002015
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002016 int
2017expand_set_debug(optexpand_T *args, int *numMatches, char_u ***matches)
2018{
2019 return expand_set_opt_string(
2020 args,
2021 p_debug_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002022 ARRAY_LENGTH(p_debug_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002023 numMatches,
2024 matches);
2025}
2026
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002027#if defined(FEAT_DIFF) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002028/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002029 * The 'diffopt' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002030 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002031 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002032did_set_diffopt(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002033{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002034 if (diffopt_changed() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002035 return e_invalid_argument;
2036
2037 return NULL;
2038}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002039
2040 int
2041expand_set_diffopt(optexpand_T *args, int *numMatches, char_u ***matches)
2042{
2043 expand_T *xp = args->oe_xp;
2044
2045 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2046 {
2047 // Within "algorithm:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002048 int algo_len = (int)STRLEN("algorithm:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002049 if (xp->xp_pattern - args->oe_set_arg >= algo_len &&
2050 STRNCMP(xp->xp_pattern - algo_len, "algorithm:", algo_len) == 0)
2051 {
2052 return expand_set_opt_string(
2053 args,
2054 p_dip_algorithm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002055 ARRAY_LENGTH(p_dip_algorithm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002056 numMatches,
2057 matches);
2058 }
Yee Cheng Chin9943d472025-03-26 19:41:02 +01002059 // Within "inline:", we have a subgroup of possible options.
2060 int inline_len = (int)STRLEN("inline:");
2061 if (xp->xp_pattern - args->oe_set_arg >= inline_len &&
2062 STRNCMP(xp->xp_pattern - inline_len, "inline:", inline_len) == 0)
2063 {
2064 return expand_set_opt_string(
2065 args,
2066 p_dip_inline_values,
2067 ARRAY_LENGTH(p_dip_inline_values) - 1,
2068 numMatches,
2069 matches);
2070 }
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002071 return FAIL;
2072 }
2073
2074 return expand_set_opt_string(
2075 args,
2076 p_dip_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002077 ARRAY_LENGTH(p_dip_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002078 numMatches,
2079 matches);
2080}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002081#endif
2082
2083/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002084 * The 'display' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002085 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002086 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002087did_set_display(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002088{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002089 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002090 return e_invalid_argument;
2091
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002092 (void)init_chartab();
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002093 return NULL;
2094}
2095
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002096 int
2097expand_set_display(optexpand_T *args, int *numMatches, char_u ***matches)
2098{
2099 return expand_set_opt_string(
2100 args,
2101 p_dy_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002102 ARRAY_LENGTH(p_dy_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002103 numMatches,
2104 matches);
2105}
2106
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002107/*
2108 * The 'eadirection' option is changed.
2109 */
2110 char *
2111did_set_eadirection(optset_T *args UNUSED)
2112{
2113 return did_set_opt_strings(p_ead, p_ead_values, FALSE);
2114}
2115
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002116 int
2117expand_set_eadirection(optexpand_T *args, int *numMatches, char_u ***matches)
2118{
2119 return expand_set_opt_string(
2120 args,
2121 p_ead_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002122 ARRAY_LENGTH(p_ead_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002123 numMatches,
2124 matches);
2125}
2126
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002127/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002128 * One of the 'encoding', 'fileencoding', 'termencoding' or 'makeencoding'
2129 * options is changed.
2130 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002131 char *
2132did_set_encoding(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002133{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002134 char_u **varp = (char_u **)args->os_varp;
2135 char_u **gvarp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002136 char *errmsg = NULL;
2137 char_u *p;
2138
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002139 // Get the global option to compare with, otherwise we would have to check
2140 // two values for all local options.
2141 gvarp = (char_u **)get_option_varp_scope(args->os_idx, OPT_GLOBAL);
2142
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002143 if (gvarp == &p_fenc)
2144 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002145 if (!curbuf->b_p_ma && args->os_flags != OPT_GLOBAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002146 errmsg = e_cannot_make_changes_modifiable_is_off;
2147 else if (vim_strchr(*varp, ',') != NULL)
2148 // No comma allowed in 'fileencoding'; catches confusing it
2149 // with 'fileencodings'.
2150 errmsg = e_invalid_argument;
2151 else
2152 {
2153 // May show a "+" in the title now.
2154 redraw_titles();
2155 // Add 'fileencoding' to the swap file.
2156 ml_setflags(curbuf);
2157 }
2158 }
2159 if (errmsg == NULL)
2160 {
2161 // canonize the value, so that STRCMP() can be used on it
2162 p = enc_canonize(*varp);
2163 if (p != NULL)
2164 {
2165 vim_free(*varp);
2166 *varp = p;
2167 }
2168 if (varp == &p_enc)
2169 {
2170 errmsg = mb_init();
2171 redraw_titles();
2172 }
2173 }
2174
2175#if defined(FEAT_GUI_GTK)
2176 if (errmsg == NULL && varp == &p_tenc && gui.in_use)
2177 {
2178 // GTK uses only a single encoding, and that is UTF-8.
2179 if (STRCMP(p_tenc, "utf-8") != 0)
2180 errmsg = e_cannot_be_changed_in_gtk_GUI;
2181 }
2182#endif
2183
2184 if (errmsg == NULL)
2185 {
2186#ifdef FEAT_KEYMAP
2187 // When 'keymap' is used and 'encoding' changes, reload the keymap
2188 // (with another encoding).
2189 if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
2190 (void)keymap_init();
2191#endif
2192
2193 // When 'termencoding' is not empty and 'encoding' changes or when
2194 // 'termencoding' changes, need to setup for keyboard input and
2195 // display output conversion.
2196 if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
2197 {
2198 if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
2199 || convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
2200 {
2201 semsg(_(e_cannot_convert_between_str_and_str),
2202 p_tenc, p_enc);
2203 errmsg = e_invalid_argument;
2204 }
2205 }
2206
2207#if defined(MSWIN)
K.Takatace3189d2023-02-15 19:13:43 +00002208 // $HOME, $VIM and $VIMRUNTIME may have characters in active code page.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002209 if (varp == &p_enc)
K.Takatace3189d2023-02-15 19:13:43 +00002210 {
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002211 init_homedir();
K.Takatace3189d2023-02-15 19:13:43 +00002212 init_vimdir();
2213 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002214#endif
2215 }
2216
2217 return errmsg;
2218}
2219
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002220 int
2221expand_set_encoding(optexpand_T *args, int *numMatches, char_u ***matches)
2222{
2223 return expand_set_opt_generic(
2224 args,
2225 get_encoding_name,
2226 numMatches,
2227 matches);
2228}
2229
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002230/*
Luuk van Baalb7147f82025-02-08 18:52:39 +01002231 * The 'eventignore(win)' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002232 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002233 char *
Luuk van Baalb7147f82025-02-08 18:52:39 +01002234did_set_eventignore(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002235{
Luuk van Baalb7147f82025-02-08 18:52:39 +01002236 char_u **varp = (char_u **)args->os_varp;
2237
2238 if (check_ei(*varp) == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002239 return e_invalid_argument;
2240 return NULL;
2241}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002242
Luuk van Baalb7147f82025-02-08 18:52:39 +01002243static int expand_eiw = FALSE;
2244
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002245 static char_u *
2246get_eventignore_name(expand_T *xp, int idx)
2247{
Luuk van Baalb7147f82025-02-08 18:52:39 +01002248 // 'eventignore(win)' allows special keyword "all" in addition to
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002249 // all event names.
2250 if (idx == 0)
2251 return (char_u *)"all";
Luuk van Baalb7147f82025-02-08 18:52:39 +01002252 return get_event_name_no_group(xp, idx - 1, expand_eiw);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002253}
2254
2255 int
2256expand_set_eventignore(optexpand_T *args, int *numMatches, char_u ***matches)
2257{
Luuk van Baalb7147f82025-02-08 18:52:39 +01002258 expand_eiw = args->oe_varp != (char_u *)&p_ei;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002259 return expand_set_opt_generic(
2260 args,
2261 get_eventignore_name,
2262 numMatches,
2263 matches);
2264}
2265
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002266/*
2267 * The 'fileformat' option is changed.
2268 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002269 char *
2270did_set_fileformat(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002271{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002272 char_u **varp = (char_u **)args->os_varp;
2273
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002274 if (!curbuf->b_p_ma && !(args->os_flags & OPT_GLOBAL))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002275 return e_cannot_make_changes_modifiable_is_off;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002276 else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002277 return e_invalid_argument;
2278
2279 // may also change 'textmode'
2280 if (get_fileformat(curbuf) == EOL_DOS)
2281 curbuf->b_p_tx = TRUE;
2282 else
2283 curbuf->b_p_tx = FALSE;
2284 redraw_titles();
2285 // update flag in swap file
2286 ml_setflags(curbuf);
2287 // Redraw needed when switching to/from "mac": a CR in the text
2288 // will be displayed differently.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002289 if (get_fileformat(curbuf) == EOL_MAC || *args->os_oldval.string == 'm')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002290 redraw_curbuf_later(UPD_NOT_VALID);
2291
2292 return NULL;
2293}
2294
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002295 int
2296expand_set_fileformat(optexpand_T *args, int *numMatches, char_u ***matches)
2297{
2298 return expand_set_opt_string(
2299 args,
2300 p_ff_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002301 ARRAY_LENGTH(p_ff_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002302 numMatches,
2303 matches);
2304}
2305
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002306/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002307 * Function given to ExpandGeneric() to obtain the possible arguments of the
2308 * fileformat options.
2309 */
2310 char_u *
2311get_fileformat_name(expand_T *xp UNUSED, int idx)
2312{
2313 if (idx >= (int)ARRAY_LENGTH(p_ff_values))
2314 return NULL;
2315
2316 return (char_u*)p_ff_values[idx];
2317}
2318
2319/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002320 * The 'fileformats' option is changed.
2321 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002322 char *
2323did_set_fileformats(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002324{
2325 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
2326 return e_invalid_argument;
2327
2328 // also change 'textauto'
2329 if (*p_ffs == NUL)
2330 p_ta = FALSE;
2331 else
2332 p_ta = TRUE;
2333
2334 return NULL;
2335}
2336
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002337/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002338 * The 'filetype' or the 'syntax' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002339 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002340 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002341did_set_filetype_or_syntax(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002342{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002343 char_u **varp = (char_u **)args->os_varp;
2344
2345 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002346 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002347
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002348 args->os_value_changed = STRCMP(args->os_oldval.string, *varp) != 0;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002349
2350 // Since we check the value, there is no need to set P_INSECURE,
2351 // even when the value comes from a modeline.
2352 args->os_value_checked = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002353
2354 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002355}
2356
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002357#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002358/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002359 * The 'foldclose' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002360 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002361 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002362did_set_foldclose(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002363{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002364 return did_set_opt_strings(p_fcl, p_fcl_values, TRUE);
2365}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002366
2367 int
2368expand_set_foldclose(optexpand_T *args, int *numMatches, char_u ***matches)
2369{
2370 return expand_set_opt_string(
2371 args,
2372 p_fcl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002373 ARRAY_LENGTH(p_fcl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002374 numMatches,
2375 matches);
2376}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002377#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002378
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002379#if (defined(FEAT_EVAL) && defined(FEAT_FOLDING)) || defined(PROTO)
2380/*
2381 * The 'foldexpr' option is changed.
2382 */
2383 char *
2384did_set_foldexpr(optset_T *args)
2385{
2386 (void)did_set_optexpr(args);
2387 if (foldmethodIsExpr(curwin))
2388 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002389 return NULL;
2390}
2391#endif
2392
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002393#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002394/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002395 * The 'foldignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002396 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002397 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002398did_set_foldignore(optset_T *args UNUSED)
2399{
2400 if (foldmethodIsIndent(curwin))
2401 foldUpdateAll(curwin);
2402 return NULL;
2403}
2404
2405/*
2406 * The 'foldmarker' option is changed.
2407 */
2408 char *
2409did_set_foldmarker(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002410{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002411 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002412 char_u *p;
2413
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002414 p = vim_strchr(*varp, ',');
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002415 if (p == NULL)
2416 return e_comma_required;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002417 else if (p == *varp || p[1] == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002418 return e_invalid_argument;
2419 else if (foldmethodIsMarker(curwin))
2420 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002421
2422 return NULL;
2423}
2424
2425/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002426 * The 'foldmethod' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002427 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002428 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002429did_set_foldmethod(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002430{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002431 char_u **varp = (char_u **)args->os_varp;
2432
Milly142cad12024-10-22 22:11:51 +02002433 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK || **varp == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002434 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002435
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002436 foldUpdateAll(curwin);
2437 if (foldmethodIsDiff(curwin))
2438 newFoldLevel();
2439 return NULL;
2440}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002441
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002442 int
2443expand_set_foldmethod(optexpand_T *args, int *numMatches, char_u ***matches)
2444{
2445 return expand_set_opt_string(
2446 args,
2447 p_fdm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002448 ARRAY_LENGTH(p_fdm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002449 numMatches,
2450 matches);
2451}
2452
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002453/*
2454 * The 'foldopen' option is changed.
2455 */
2456 char *
2457did_set_foldopen(optset_T *args UNUSED)
2458{
2459 return did_set_opt_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
2460}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002461
2462 int
2463expand_set_foldopen(optexpand_T *args, int *numMatches, char_u ***matches)
2464{
2465 return expand_set_opt_string(
2466 args,
2467 p_fdo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002468 ARRAY_LENGTH(p_fdo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002469 numMatches,
2470 matches);
2471}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002472#endif
2473
2474/*
2475 * The 'formatoptions' option is changed.
2476 */
2477 char *
2478did_set_formatoptions(optset_T *args)
2479{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002480 char_u **varp = (char_u **)args->os_varp;
2481
Christian Brabandtb39b2402023-11-29 11:34:05 +01002482 return did_set_option_listflag(*varp, (char_u *)FO_ALL, args->os_errbuf,
2483 args->os_errbuflen);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002484}
2485
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002486 int
2487expand_set_formatoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2488{
2489 return expand_set_opt_listflag(args, (char_u*)FO_ALL, numMatches, matches);
2490}
2491
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002492#if defined(CURSOR_SHAPE) || defined(PROTO)
2493/*
2494 * The 'guicursor' option is changed.
2495 */
2496 char *
2497did_set_guicursor(optset_T *args UNUSED)
2498{
2499 return parse_shape_opt(SHAPE_CURSOR);
2500}
2501#endif
2502
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002503#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002504/*
2505 * The 'guifont' option is changed.
2506 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002507 char *
2508did_set_guifont(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002509{
2510 char_u *p;
2511 char *errmsg = NULL;
2512
2513 if (gui.in_use)
2514 {
2515 p = p_guifont;
2516# if defined(FEAT_GUI_GTK)
2517 // Put up a font dialog and let the user select a new value.
2518 // If this is cancelled go back to the old value but don't
2519 // give an error message.
2520 if (STRCMP(p, "*") == 0)
2521 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002522 p = gui_mch_font_dialog(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002523 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002524 p_guifont = (p != NULL) ? p : vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002525 }
2526# endif
2527 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
2528 {
2529# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
2530 if (STRCMP(p_guifont, "*") == 0)
2531 {
2532 // Dialog was cancelled: Keep the old value without giving
2533 // an error message.
2534 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002535 p_guifont = vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002536 }
2537 else
2538# endif
2539 errmsg = e_invalid_fonts;
2540 }
2541 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002542
2543 return errmsg;
2544}
2545
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002546/*
2547 * Expand the 'guifont' option. Only when GUI is being used. Each platform has
2548 * specific behaviors.
2549 */
2550 int
2551expand_set_guifont(optexpand_T *args, int *numMatches, char_u ***matches)
2552{
2553 if (!gui.in_use)
2554 return FAIL;
2555
2556# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
2557 char_u **varp = (char_u **)args->oe_varp;
2558 int wide = (varp == &p_guifontwide);
2559
2560 return expand_set_opt_callback(
2561 args, gui_mch_expand_font, &wide, numMatches, matches);
2562# else
2563 return FAIL;
2564# endif
2565}
2566
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002567# if defined(FEAT_XFONTSET) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002568/*
2569 * The 'guifontset' option is changed.
2570 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002571 char *
2572did_set_guifontset(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002573{
2574 char *errmsg = NULL;
2575
2576 if (STRCMP(p_guifontset, "*") == 0)
2577 errmsg = e_cant_select_fontset;
2578 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
2579 errmsg = e_invalid_fontset;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002580
2581 return errmsg;
2582}
2583# endif
2584
2585/*
2586 * The 'guifontwide' option is changed.
2587 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002588 char *
2589did_set_guifontwide(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002590{
2591 char *errmsg = NULL;
2592
2593 if (STRCMP(p_guifontwide, "*") == 0)
2594 errmsg = e_cant_select_wide_font;
2595 else if (gui_get_wide_font() == FAIL)
2596 errmsg = e_invalid_wide_font;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002597
2598 return errmsg;
2599}
2600#endif
2601
Erik S. V. Jansson8b1e7492024-02-24 14:26:52 +01002602#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002603/*
2604 * The 'guiligatures' option is changed.
2605 */
2606 char *
2607did_set_guiligatures(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002608{
2609 gui_set_ligatures();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002610 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002611}
2612#endif
2613
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002614#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002615/*
2616 * The 'guioptions' option is changed.
2617 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002618 char *
2619did_set_guioptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002620{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002621 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002622 char *errmsg;
2623
Christian Brabandtb39b2402023-11-29 11:34:05 +01002624 errmsg = did_set_option_listflag(*varp, (char_u *)GO_ALL, args->os_errbuf,
2625 args->os_errbuflen);
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002626 if (errmsg != NULL)
2627 return errmsg;
2628
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002629 gui_init_which_components(args->os_oldval.string);
2630 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002631}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002632
2633 int
2634expand_set_guioptions(optexpand_T *args, int *numMatches, char_u ***matches)
2635{
2636 return expand_set_opt_listflag(args, (char_u*)GO_ALL, numMatches, matches);
2637}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002638#endif
2639
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002640#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002641/*
2642 * The 'guitablabel' option is changed.
2643 */
2644 char *
2645did_set_guitablabel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002646{
2647 redraw_tabline = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002648 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002649}
2650#endif
2651
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002652/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002653 * The 'helpfile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002654 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002655 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002656did_set_helpfile(optset_T *args UNUSED)
2657{
2658 // May compute new values for $VIM and $VIMRUNTIME
2659 if (didset_vim)
2660 vim_unsetenv_ext((char_u *)"VIM");
2661 if (didset_vimruntime)
2662 vim_unsetenv_ext((char_u *)"VIMRUNTIME");
2663 return NULL;
2664}
2665
2666#if defined(FEAT_MULTI_LANG) || defined(PROTO)
2667/*
2668 * The 'helplang' option is changed.
2669 */
2670 char *
2671did_set_helplang(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002672{
2673 char *errmsg = NULL;
2674
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002675 // Check for "", "ab", "ab,cd", etc.
2676 for (char_u *s = p_hlg; *s != NUL; s += 3)
2677 {
2678 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
2679 {
2680 errmsg = e_invalid_argument;
2681 break;
2682 }
2683 if (s[2] == NUL)
2684 break;
2685 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002686
2687 return errmsg;
2688}
2689#endif
2690
2691/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002692 * The 'highlight' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002693 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002694 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002695did_set_highlight(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002696{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002697 if (highlight_changed() == FAIL)
2698 return e_invalid_argument; // invalid flags
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002699
2700 return NULL;
2701}
2702
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002703/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002704 * Expand 'highlight' option.
2705 */
2706 int
2707expand_set_highlight(optexpand_T *args, int *numMatches, char_u ***matches)
2708{
2709 char_u *p;
2710 expand_T *xp = args->oe_xp;
2711 static char_u hl_flags[HLF_COUNT] = HL_FLAGS;
Christian Brabandt3f168ec2023-10-02 23:21:11 +02002712 size_t i;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002713 int count = 0;
2714
2715 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2716 {
2717 // Right after a ':', meaning we just return all highlight names.
2718 return expand_set_opt_generic(
2719 args,
2720 get_highlight_name,
2721 numMatches,
2722 matches);
2723 }
2724
2725 if (*xp->xp_pattern == NUL)
2726 {
2727 // At beginning of a comma-separated list. Return the specific list of
2728 // supported occasions.
2729 *matches = ALLOC_MULT(char_u *, HLF_COUNT + 1);
2730 if (*matches == NULL)
2731 return FAIL;
2732
2733 // We still want to return the full option if it's requested.
2734 if (args->oe_include_orig_val)
2735 {
2736 p = vim_strsave(args->oe_opt_value);
2737 if (p == NULL)
2738 {
2739 VIM_CLEAR(*matches);
2740 return FAIL;
2741 }
2742 (*matches)[count++] = p;
2743 }
2744
2745 for (i = 0; i < HLF_COUNT; i++)
2746 {
2747 p = vim_strnsave(&hl_flags[i], 1);
2748 if (p == NULL)
2749 {
2750 if (count == 0)
2751 {
2752 VIM_CLEAR(*matches);
2753 return FAIL;
2754 }
2755 else
2756 break;
2757 }
2758 (*matches)[count++] = p;
2759 }
2760
2761 if (count == 0)
2762 {
2763 VIM_CLEAR(*matches);
2764 return FAIL;
2765 }
2766 *numMatches = count;
2767 return OK;
2768 }
2769
2770 // We are after the initial character (which indicates the occasion). We
2771 // already made sure we are not matching after a ':' above, so now we want
2772 // to match against display mode modifiers.
2773 // Since the xp_pattern starts from the beginning, we need to include it in
2774 // the returned match.
2775
2776 // Note: Keep this in sync with highlight_changed()
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002777 static char_u p_hl_mode_values[] =
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002778 {':', 'b', 'i', '-', 'n', 'r', 's', 'u', 'c', '2', 'd', '=', 't'};
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002779 size_t num_hl_modes = ARRAY_LENGTH(p_hl_mode_values);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002780
2781 *matches = ALLOC_MULT(char_u *, num_hl_modes);
2782 if (*matches == NULL)
2783 return FAIL;
2784
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002785 int pattern_len = xp->xp_pattern_len;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002786
2787 for (i = 0; i < num_hl_modes; i++)
2788 {
2789 // Don't allow duplicates as these are unique flags
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002790 char_u *dup = vim_strchr(xp->xp_pattern + 1, p_hl_mode_values[i]);
2791 if (dup != NULL && (int)(dup - xp->xp_pattern) < pattern_len)
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002792 continue;
2793
2794 // ':' only works by itself, not with other flags.
2795 if (pattern_len > 1 && p_hl_mode_values[i] == ':')
2796 continue;
2797
2798 p = vim_strnsave(xp->xp_pattern, pattern_len + 1);
2799 if (p == NULL)
2800 {
2801 if (i == 0)
2802 {
2803 VIM_CLEAR(*matches);
2804 return FAIL;
2805 }
2806 else
2807 break;
2808 }
2809 p[pattern_len] = p_hl_mode_values[i];
2810 p[pattern_len + 1] = NUL;
2811 (*matches)[count++] = p;
2812 }
2813 if (count == 0)
2814 {
2815 VIM_CLEAR(*matches);
2816 return FAIL;
2817 }
2818 *numMatches = count;
2819
2820 return OK;
2821}
2822
2823/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002824 * The 'titlestring' or the 'iconstring' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002825 */
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002826 static char *
2827parse_titleiconstring(optset_T *args UNUSED, int flagval UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002828{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002829#ifdef FEAT_STL_OPT
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002830 char_u **varp = (char_u **)args->os_varp;
2831
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002832 // NULL => statusline syntax
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002833 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002834 stl_syntax |= flagval;
2835 else
2836 stl_syntax &= ~flagval;
2837#endif
2838 did_set_title();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002839
2840 return NULL;
2841}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002842
2843/*
2844 * The 'iconstring' option is changed.
2845 */
2846 char *
2847did_set_iconstring(optset_T *args)
2848{
2849 int flagval = 0;
2850
2851#ifdef FEAT_STL_OPT
2852 flagval = STL_IN_ICON;
2853#endif
2854
2855 return parse_titleiconstring(args, flagval);
2856}
2857
2858#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
2859/*
2860 * The 'imactivatekey' option is changed.
2861 */
2862 char *
2863did_set_imactivatekey(optset_T *args UNUSED)
2864{
2865 if (!im_xim_isvalid_imactivate())
2866 return e_invalid_argument;
2867 return NULL;
2868}
2869#endif
2870
2871/*
glepnirbcd59952025-04-24 21:48:35 +02002872 * The 'isexpand' option is changed.
2873 */
2874 char *
2875did_set_isexpand(optset_T *args)
2876{
2877 char_u *ise = p_ise;
2878 char_u *p;
2879 int last_was_comma = FALSE;
2880
2881 if (args->os_flags & OPT_LOCAL)
2882 ise = curbuf->b_p_ise;
2883
2884 for (p = ise; *p != NUL;)
2885 {
2886 if (*p == '\\' && p[1] == ',')
2887 {
2888 p += 2;
2889 last_was_comma = FALSE;
2890 continue;
2891 }
2892
2893 if (*p == ',')
2894 {
2895 if (last_was_comma)
2896 return e_invalid_argument;
2897 last_was_comma = TRUE;
2898 p++;
2899 continue;
2900 }
2901
2902 last_was_comma = FALSE;
2903 MB_PTR_ADV(p);
2904 }
2905
2906 if (last_was_comma)
2907 return e_invalid_argument;
2908
2909 return NULL;
2910}
2911
2912
2913/*
Milly5e7a6a42024-10-22 22:27:19 +02002914 * The 'iskeyword' option is changed.
2915 */
2916 char *
2917did_set_iskeyword(optset_T *args)
2918{
2919 char_u **varp = (char_u **)args->os_varp;
2920
2921 if (varp == &p_isk) // only check for global-value
2922 {
2923 if (check_isopt(*varp) == FAIL)
2924 return e_invalid_argument;
2925 }
2926 else // fallthrough for local-value
2927 return did_set_isopt(args);
2928
2929 return NULL;
2930}
2931
2932/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002933 * The 'isident' or the 'iskeyword' or the 'isprint' or the 'isfname' option is
2934 * changed.
2935 */
2936 char *
2937did_set_isopt(optset_T *args)
2938{
Milly5e7a6a42024-10-22 22:27:19 +02002939 // 'isident', 'iskeyword', 'isprint' or 'isfname' option: refill g_chartab[]
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002940 // If the new option is invalid, use old value.
2941 // 'lisp' option: refill g_chartab[] for '-' char.
2942 if (init_chartab() == FAIL)
2943 {
2944 args->os_restore_chartab = TRUE;// need to restore the chartab.
2945 return e_invalid_argument; // error in value
2946 }
2947
2948 return NULL;
2949}
2950
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002951/*
2952 * The 'jumpoptions' option is changed.
2953 */
2954 char *
Yegappan Lakshmanan1926ae42023-09-21 16:36:28 +02002955did_set_jumpoptions(optset_T *args UNUSED)
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002956{
2957 if (opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE) != OK)
2958 return e_invalid_argument;
2959
2960 return NULL;
2961}
2962
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002963 int
2964expand_set_jumpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2965{
2966 return expand_set_opt_string(
2967 args,
2968 p_jop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002969 ARRAY_LENGTH(p_jop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002970 numMatches,
2971 matches);
2972}
2973
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002974#if defined(FEAT_KEYMAP) || defined(PROTO)
2975/*
2976 * The 'keymap' option is changed.
2977 */
2978 char *
2979did_set_keymap(optset_T *args)
2980{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002981 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002982 char *errmsg = NULL;
2983
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002984 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002985 errmsg = e_invalid_argument;
2986 else
2987 {
2988 int secure_save = secure;
2989
2990 // Reset the secure flag, since the value of 'keymap' has
2991 // been checked to be safe.
2992 secure = 0;
2993
2994 // load or unload key mapping tables
2995 errmsg = keymap_init();
2996
2997 secure = secure_save;
2998
2999 // Since we check the value, there is no need to set P_INSECURE,
3000 // even when the value comes from a modeline.
3001 args->os_value_checked = TRUE;
3002 }
3003
3004 if (errmsg == NULL)
3005 {
3006 if (*curbuf->b_p_keymap != NUL)
3007 {
3008 // Installed a new keymap, switch on using it.
3009 curbuf->b_p_iminsert = B_IMODE_LMAP;
3010 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
3011 curbuf->b_p_imsearch = B_IMODE_LMAP;
3012 }
3013 else
3014 {
3015 // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
3016 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
3017 curbuf->b_p_iminsert = B_IMODE_NONE;
3018 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
3019 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3020 }
3021 if ((args->os_flags & OPT_LOCAL) == 0)
3022 {
3023 set_iminsert_global();
3024 set_imsearch_global();
3025 }
3026 status_redraw_curbuf();
3027 }
3028
3029 return errmsg;
3030}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003031#endif
3032
3033/*
3034 * The 'keymodel' option is changed.
3035 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003036 char *
3037did_set_keymodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003038{
3039 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
3040 return e_invalid_argument;
3041
3042 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
3043 km_startsel = (vim_strchr(p_km, 'a') != NULL);
3044 return NULL;
3045}
3046
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003047 int
3048expand_set_keymodel(optexpand_T *args, int *numMatches, char_u ***matches)
3049{
3050 return expand_set_opt_string(
3051 args,
3052 p_km_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003053 ARRAY_LENGTH(p_km_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003054 numMatches,
3055 matches);
3056}
3057
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003058/*
3059 * The 'keyprotocol' option is changed.
3060 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003061 char *
3062did_set_keyprotocol(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003063{
Gregory Anders3695d0e2023-09-29 20:17:20 +02003064 char_u *term = T_NAME;
3065 keyprot_T kpc = match_keyprotocol(term);
3066 if (kpc == KEYPROTOCOL_FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003067 return e_invalid_argument;
3068
Gregory Anders3695d0e2023-09-29 20:17:20 +02003069 apply_keyprotocol(term, kpc);
3070
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003071 return NULL;
3072}
3073
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003074 int
3075expand_set_keyprotocol(optexpand_T *args, int *numMatches, char_u ***matches)
3076{
3077 expand_T *xp = args->oe_xp;
3078 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3079 {
3080 // 'keyprotocol' only has well-defined terms for completion for the
3081 // protocol part after the colon.
3082 return expand_set_opt_string(
3083 args,
3084 p_kpc_protocol_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003085 ARRAY_LENGTH(p_kpc_protocol_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003086 numMatches,
3087 matches);
3088 }
3089 // Use expand_set_opt_string instead of returning FAIL so that we can
3090 // include the original value if args->oe_include_orig_val is set.
3091 static char *(empty[]) = {NULL};
3092 return expand_set_opt_string(args, empty, 0, numMatches, matches);
3093}
3094
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003095/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003096 * The 'lispoptions' option is changed.
3097 */
3098 char *
3099did_set_lispoptions(optset_T *args)
3100{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003101 char_u **varp = (char_u **)args->os_varp;
3102
3103 if (**varp != NUL
3104 && STRCMP(*varp, "expr:0") != 0
3105 && STRCMP(*varp, "expr:1") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003106 return e_invalid_argument;
3107
3108 return NULL;
3109}
3110
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003111 int
3112expand_set_lispoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3113{
3114 static char *(p_lop_values[]) = {"expr:0", "expr:1", NULL};
3115 return expand_set_opt_string(
3116 args,
3117 p_lop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003118 ARRAY_LENGTH(p_lop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003119 numMatches,
3120 matches);
3121}
3122
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003123/*
3124 * The 'matchpairs' option is changed.
3125 */
3126 char *
3127did_set_matchpairs(optset_T *args)
3128{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003129 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003130 char_u *p;
3131
3132 if (has_mbyte)
3133 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003134 for (p = *varp; *p != NUL; ++p)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003135 {
3136 int x2 = -1;
3137 int x3 = -1;
3138
3139 p += mb_ptr2len(p);
3140 if (*p != NUL)
3141 x2 = *p++;
3142 if (*p != NUL)
3143 {
3144 x3 = mb_ptr2char(p);
3145 p += mb_ptr2len(p);
3146 }
3147 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
3148 return e_invalid_argument;
3149 if (*p == NUL)
3150 break;
3151 }
3152 }
3153 else
3154 {
3155 // Check for "x:y,x:y"
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003156 for (p = *varp; *p != NUL; p += 4)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003157 {
3158 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
3159 return e_invalid_argument;
3160 if (p[3] == NUL)
3161 break;
3162 }
3163 }
3164
3165 return NULL;
3166}
3167
Christian Brabandt51d4d842024-12-06 17:26:25 +01003168/*
3169 * Process the updated 'messagesopt' option value.
3170 */
3171 char *
3172did_set_messagesopt(optset_T *args UNUSED)
3173{
3174 if (messagesopt_changed() == FAIL)
3175 return e_invalid_argument;
3176
3177 return NULL;
3178}
3179
3180 int
3181expand_set_messagesopt(optexpand_T *args, int *numMatches, char_u ***matches)
3182{
zeertzjq8cc43da2024-12-07 16:09:08 +01003183 static char *(p_mopt_values[]) = {"hit-enter", "wait:", "history:", NULL};
Christian Brabandt51d4d842024-12-06 17:26:25 +01003184 return expand_set_opt_string(
3185 args,
zeertzjq8cc43da2024-12-07 16:09:08 +01003186 p_mopt_values,
3187 ARRAY_LENGTH(p_mopt_values) - 1,
Christian Brabandt51d4d842024-12-06 17:26:25 +01003188 numMatches,
3189 matches);
3190}
3191
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003192#if defined(FEAT_SPELL) || defined(PROTO)
3193/*
3194 * The 'mkspellmem' option is changed.
3195 */
3196 char *
3197did_set_mkspellmem(optset_T *args UNUSED)
3198{
3199 if (spell_check_msm() != OK)
3200 return e_invalid_argument;
3201
3202 return NULL;
3203}
3204#endif
3205
3206/*
3207 * The 'mouse' option is changed.
3208 */
3209 char *
3210did_set_mouse(optset_T *args)
3211{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003212 char_u **varp = (char_u **)args->os_varp;
3213
Christian Brabandtb39b2402023-11-29 11:34:05 +01003214 return did_set_option_listflag(*varp, (char_u *)MOUSE_ALL, args->os_errbuf,
3215 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003216}
3217
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003218 int
3219expand_set_mouse(optexpand_T *args, int *numMatches, char_u ***matches)
3220{
3221 return expand_set_opt_listflag(args, (char_u*)MOUSE_ALL, numMatches, matches);
3222}
3223
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003224/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003225 * The 'mousemodel' option is changed.
3226 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003227 char *
3228did_set_mousemodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003229{
3230 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
3231 return e_invalid_argument;
3232#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
Ken Takata18d0d292023-12-19 20:12:29 +01003233 else if (*p_mousem != *args->os_oldval.string)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003234 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
3235 // to create or delete the popup menus.
3236 gui_motif_update_mousemodel(root_menu);
3237#endif
3238
3239 return NULL;
3240}
3241
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003242 int
3243expand_set_mousemodel(optexpand_T *args, int *numMatches, char_u ***matches)
3244{
3245 return expand_set_opt_string(
3246 args,
3247 p_mousem_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003248 ARRAY_LENGTH(p_mousem_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003249 numMatches,
3250 matches);
3251}
3252
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003253#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3254 char *
3255did_set_mouseshape(optset_T *args UNUSED)
3256{
3257 char *errmsg = NULL;
3258
3259 errmsg = parse_shape_opt(SHAPE_MOUSE);
3260 update_mouseshape(-1);
3261
3262 return errmsg;
3263}
3264#endif
3265
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003266/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003267 * The 'nrformats' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003268 */
3269 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003270did_set_nrformats(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003271{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003272 char_u **varp = (char_u **)args->os_varp;
3273
3274 return did_set_opt_strings(*varp, p_nf_values, TRUE);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003275}
3276
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003277 int
3278expand_set_nrformats(optexpand_T *args, int *numMatches, char_u ***matches)
3279{
3280 return expand_set_opt_string(
3281 args,
3282 p_nf_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003283 ARRAY_LENGTH(p_nf_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003284 numMatches,
3285 matches);
3286}
3287
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003288#if defined(FEAT_EVAL) || defined(PROTO)
3289/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003290 * One of the '*expr' options is changed: 'balloonexpr', 'diffexpr',
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003291 * 'foldexpr', 'foldtext', 'formatexpr', 'includeexpr', 'indentexpr',
3292 * 'patchexpr', 'printexpr' and 'charconvert'.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003293 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003294 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003295did_set_optexpr(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003296{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003297 char_u **varp = (char_u **)args->os_varp;
3298
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003299 // If the option value starts with <SID> or s:, then replace that with
3300 // the script identifier.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003301 char_u *name = get_scriptlocal_funcname(*varp);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003302 if (name != NULL)
3303 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003304 free_string_option(*varp);
3305 *varp = name;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003306 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003307
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003308 return NULL;
3309}
3310#endif
3311
3312/*
3313 * The 'pastetoggle' option is changed.
3314 */
3315 char *
3316did_set_pastetoggle(optset_T *args UNUSED)
3317{
3318 char_u *p;
3319
3320 // translate key codes like in a mapping
3321 if (*p_pt)
3322 {
zeertzjq7e0bae02023-08-11 23:15:38 +02003323 (void)replace_termcodes(p_pt, &p, 0,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003324 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
3325 if (p != NULL)
3326 {
3327 free_string_option(p_pt);
3328 p_pt = p;
3329 }
3330 }
3331
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003332 return NULL;
3333}
3334
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003335#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3336/*
3337 * The 'previewpopup' option is changed.
3338 */
3339 char *
3340did_set_previewpopup(optset_T *args UNUSED)
3341{
3342 if (parse_previewpopup(NULL) == FAIL)
3343 return e_invalid_argument;
3344
3345 return NULL;
3346}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003347
3348 int
3349expand_set_popupoption(optexpand_T *args, int *numMatches, char_u ***matches)
3350{
3351 expand_T *xp = args->oe_xp;
3352
3353 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3354 {
3355 // Within "highlight:"/"border:"/"align:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003356 int border_len = (int)STRLEN("border:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003357 if (xp->xp_pattern - args->oe_set_arg >= border_len &&
3358 STRNCMP(xp->xp_pattern - border_len, "border:", border_len) == 0)
3359 {
3360 return expand_set_opt_string(
3361 args,
3362 p_popup_option_border_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003363 ARRAY_LENGTH(p_popup_option_border_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003364 numMatches,
3365 matches);
3366 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003367 int align_len = (int)STRLEN("align:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003368 if (xp->xp_pattern - args->oe_set_arg >= align_len &&
3369 STRNCMP(xp->xp_pattern - align_len, "align:", align_len) == 0)
3370 {
3371 return expand_set_opt_string(
3372 args,
3373 p_popup_option_align_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003374 ARRAY_LENGTH(p_popup_option_align_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003375 numMatches,
3376 matches);
3377 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003378 int highlight_len = (int)STRLEN("highlight:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003379 if (xp->xp_pattern - args->oe_set_arg >= highlight_len &&
3380 STRNCMP(xp->xp_pattern - highlight_len, "highlight:", highlight_len) == 0)
3381 {
3382 // Return the list of all highlight names
3383 return expand_set_opt_generic(
3384 args,
3385 get_highlight_name,
3386 numMatches,
3387 matches);
3388 }
3389 return FAIL;
3390 }
3391
3392 return expand_set_opt_string(
3393 args,
3394 p_popup_option_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003395 ARRAY_LENGTH(p_popup_option_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003396 numMatches,
3397 matches);
3398}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003399#endif
3400
3401#if defined(FEAT_POSTSCRIPT) || defined(PROTO)
3402/*
3403 * The 'printencoding' option is changed.
3404 */
3405 char *
3406did_set_printencoding(optset_T *args UNUSED)
3407{
3408 char_u *s, *p;
3409
3410 // Canonize 'printencoding' if VIM standard one
3411 p = enc_canonize(p_penc);
3412 if (p != NULL)
3413 {
3414 vim_free(p_penc);
3415 p_penc = p;
3416 }
3417 else
3418 {
3419 // Ensure lower case and '-' for '_'
3420 for (s = p_penc; *s != NUL; s++)
3421 {
3422 if (*s == '_')
3423 *s = '-';
3424 else
3425 *s = TOLOWER_ASC(*s);
3426 }
3427 }
3428
3429 return NULL;
3430}
3431#endif
3432
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003433#if defined(FEAT_PRINTER) || defined(PROTO)
3434
3435 static char_u *
3436get_printoptions_names(expand_T *xp UNUSED, int idx)
3437{
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003438 if (idx >= (int)ARRAY_LENGTH(printer_opts))
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003439 return NULL;
3440 return (char_u*)printer_opts[idx].name;
3441}
3442
3443/*
3444 * Expand 'printoptions' option
3445 */
3446 int
3447expand_set_printoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3448{
3449 return expand_set_opt_generic(
3450 args,
3451 get_printoptions_names,
3452 numMatches,
3453 matches);
3454}
3455#endif
3456
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003457#if defined(FEAT_STL_OPT) || defined(PROTO)
3458/*
3459 * The 'statusline' or the 'tabline' or the 'rulerformat' option is changed.
3460 * "rulerformat" is TRUE if the 'rulerformat' option is changed.
3461 */
3462 static char *
3463parse_statustabline_rulerformat(optset_T *args, int rulerformat)
3464{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003465 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003466 char_u *s;
3467 char *errmsg = NULL;
3468 int wid;
3469
3470 if (rulerformat) // reset ru_wid first
3471 ru_wid = 0;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003472 s = *varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003473 if (rulerformat && *s == '%')
3474 {
3475 // set ru_wid if 'ruf' starts with "%99("
3476 if (*++s == '-') // ignore a '-'
3477 s++;
3478 wid = getdigits(&s);
3479 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
3480 ru_wid = wid;
3481 else
Yegappan Lakshmananac023e82024-11-27 20:55:45 +01003482 {
3483 // Validate the flags in 'rulerformat' only if it doesn't point to
3484 // a custom function ("%!" flag).
3485 if ((*varp)[1] != '!')
3486 errmsg = check_stl_option(p_ruf);
3487 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003488 }
3489 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
3490 else if (rulerformat || s[0] != '%' || s[1] != '!')
3491 errmsg = check_stl_option(s);
3492 if (rulerformat && errmsg == NULL)
3493 comp_col();
3494
3495 return errmsg;
3496}
3497#endif
3498
3499#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
3500/*
3501 * The 'renderoptions' option is changed.
3502 */
3503 char *
3504did_set_renderoptions(optset_T *args UNUSED)
3505{
3506 if (!gui_mch_set_rendering_options(p_rop))
3507 return e_invalid_argument;
3508
3509 return NULL;
3510}
3511#endif
3512
3513#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3514/*
3515 * The 'rightleftcmd' option is changed.
3516 */
3517 char *
3518did_set_rightleftcmd(optset_T *args)
3519{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003520 char_u **varp = (char_u **)args->os_varp;
3521
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003522 // Currently only "search" is a supported value.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003523 if (**varp != NUL && STRCMP(*varp, "search") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003524 return e_invalid_argument;
3525
3526 return NULL;
3527}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003528
3529 int
3530expand_set_rightleftcmd(optexpand_T *args, int *numMatches, char_u ***matches)
3531{
3532 static char *(p_rlc_values[]) = {"search", NULL};
3533 return expand_set_opt_string(
3534 args,
3535 p_rlc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003536 ARRAY_LENGTH(p_rlc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003537 numMatches,
3538 matches);
3539}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003540#endif
3541
3542#if defined(FEAT_STL_OPT) || defined(PROTO)
3543/*
3544 * The 'rulerformat' option is changed.
3545 */
3546 char *
3547did_set_rulerformat(optset_T *args)
3548{
3549 return parse_statustabline_rulerformat(args, TRUE);
3550}
3551#endif
3552
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02003553#if defined(FEAT_TABPANEL)
3554/*
3555 * Process the new 'tabpanelopt' option value.
3556 */
3557 char *
3558did_set_tabpanelopt(optset_T *args)
3559{
3560 if (tabpanelopt_changed() == FAIL)
3561 return e_invalid_argument;
3562
3563 shell_new_columns();
3564
3565 return NULL;
3566}
3567
3568 int
3569expand_set_tabpanelopt(optexpand_T *args, int *numMatches, char_u ***matches)
3570{
3571 return expand_set_opt_string(
3572 args,
3573 p_tpl_values,
3574 ARRAY_LENGTH(p_tpl_values) - 1,
3575 numMatches,
3576 matches);
3577}
3578#endif
3579
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003580/*
3581 * The 'scrollopt' option is changed.
3582 */
3583 char *
3584did_set_scrollopt(optset_T *args UNUSED)
3585{
3586 return did_set_opt_strings(p_sbo, p_scbopt_values, TRUE);
3587}
3588
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003589 int
3590expand_set_scrollopt(optexpand_T *args, int *numMatches, char_u ***matches)
3591{
3592 return expand_set_opt_string(
3593 args,
3594 p_scbopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003595 ARRAY_LENGTH(p_scbopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003596 numMatches,
3597 matches);
3598}
3599
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003600/*
3601 * The 'selection' option is changed.
3602 */
3603 char *
3604did_set_selection(optset_T *args UNUSED)
3605{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003606 if (*p_sel == NUL || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003607 return e_invalid_argument;
3608
3609 return NULL;
3610}
3611
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003612 int
3613expand_set_selection(optexpand_T *args, int *numMatches, char_u ***matches)
3614{
3615 return expand_set_opt_string(
3616 args,
3617 p_sel_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003618 ARRAY_LENGTH(p_sel_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003619 numMatches,
3620 matches);
3621}
3622
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003623/*
3624 * The 'selectmode' option is changed.
3625 */
3626 char *
3627did_set_selectmode(optset_T *args UNUSED)
3628{
3629 return did_set_opt_strings(p_slm, p_slm_values, TRUE);
3630}
3631
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003632 int
3633expand_set_selectmode(optexpand_T *args, int *numMatches, char_u ***matches)
3634{
3635 return expand_set_opt_string(
3636 args,
3637 p_slm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003638 ARRAY_LENGTH(p_slm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003639 numMatches,
3640 matches);
3641}
3642
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003643#if defined(FEAT_SESSION) || defined(PROTO)
3644/*
3645 * The 'sessionoptions' option is changed.
3646 */
3647 char *
3648did_set_sessionoptions(optset_T *args)
3649{
3650 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
3651 return e_invalid_argument;
3652 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
3653 {
3654 // Don't allow both "sesdir" and "curdir".
3655 (void)opt_strings_flags(args->os_oldval.string, p_ssop_values,
3656 &ssop_flags, TRUE);
3657 return e_invalid_argument;
3658 }
3659
3660 return NULL;
3661}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003662
3663 int
3664expand_set_sessionoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3665{
3666 return expand_set_opt_string(
3667 args,
3668 p_ssop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003669 ARRAY_LENGTH(p_ssop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003670 numMatches,
3671 matches);
3672}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003673#endif
3674
3675/*
3676 * The 'shortmess' option is changed.
3677 */
3678 char *
3679did_set_shortmess(optset_T *args)
3680{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003681 char_u **varp = (char_u **)args->os_varp;
3682
Christian Brabandtb39b2402023-11-29 11:34:05 +01003683 return did_set_option_listflag(*varp, (char_u *)SHM_ALL, args->os_errbuf,
3684 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003685}
3686
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003687 int
3688expand_set_shortmess(optexpand_T *args, int *numMatches, char_u ***matches)
3689{
3690 return expand_set_opt_listflag(args, (char_u*)SHM_ALL, numMatches, matches);
3691}
3692
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003693#if defined(FEAT_LINEBREAK) || defined(PROTO)
3694/*
3695 * The 'showbreak' option is changed.
3696 */
3697 char *
3698did_set_showbreak(optset_T *args)
3699{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003700 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003701 char_u *s;
3702
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003703 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003704 {
3705 if (ptr2cells(s) != 1)
3706 return e_showbreak_contains_unprintable_or_wide_character;
3707 MB_PTR_ADV(s);
3708 }
3709
3710 return NULL;
3711}
3712#endif
3713
3714/*
3715 * The 'showcmdloc' option is changed.
3716 */
3717 char *
3718did_set_showcmdloc(optset_T *args UNUSED)
3719{
zeertzjqc27fcf42024-03-01 23:01:43 +01003720 char *errmsg = did_set_opt_strings(p_sloc, p_sloc_values, FALSE);
3721
3722 if (errmsg == NULL)
3723 comp_col();
3724
3725 return errmsg;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003726}
3727
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003728 int
3729expand_set_showcmdloc(optexpand_T *args, int *numMatches, char_u ***matches)
3730{
3731 return expand_set_opt_string(
3732 args,
3733 p_sloc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003734 ARRAY_LENGTH(p_sloc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003735 numMatches,
3736 matches);
3737}
3738
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003739#if defined(FEAT_SIGNS) || defined(PROTO)
3740/*
3741 * The 'signcolumn' option is changed.
3742 */
3743 char *
3744did_set_signcolumn(optset_T *args)
3745{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003746 char_u **varp = (char_u **)args->os_varp;
3747
3748 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003749 return e_invalid_argument;
3750 // When changing the 'signcolumn' to or from 'number', recompute the
3751 // width of the number column if 'number' or 'relativenumber' is set.
3752 if (((*args->os_oldval.string == 'n' && args->os_oldval.string[1] == 'u')
3753 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
3754 && (curwin->w_p_nu || curwin->w_p_rnu))
3755 curwin->w_nrwidth_line_count = 0;
3756
3757 return NULL;
3758}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003759
3760 int
3761expand_set_signcolumn(optexpand_T *args, int *numMatches, char_u ***matches)
3762{
3763 return expand_set_opt_string(
3764 args,
3765 p_scl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003766 ARRAY_LENGTH(p_scl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003767 numMatches,
3768 matches);
3769}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003770#endif
3771
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003772#if defined(FEAT_SPELL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003773/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003774 * The 'spellcapcheck' option is changed.
3775 */
3776 char *
3777did_set_spellcapcheck(optset_T *args UNUSED)
3778{
3779 // compile the regexp program.
3780 return compile_cap_prog(curwin->w_s);
3781}
3782
3783/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003784 * The 'spellfile' option is changed.
3785 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003786 char *
3787did_set_spellfile(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003788{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003789 char_u **varp = (char_u **)args->os_varp;
3790
3791 if (!valid_spellfile(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003792 return e_invalid_argument;
3793
3794 // If there is a window for this buffer in which 'spell' is set load the
3795 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003796 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003797}
3798
3799/*
3800 * The 'spell' option is changed.
3801 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003802 char *
3803did_set_spelllang(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003804{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003805 char_u **varp = (char_u **)args->os_varp;
3806
3807 if (!valid_spelllang(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003808 return e_invalid_argument;
3809
3810 // If there is a window for this buffer in which 'spell' is set load the
3811 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003812 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003813}
3814
3815/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003816 * The 'spelloptions' option is changed.
3817 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003818 char *
3819did_set_spelloptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003820{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003821 char_u **varp = (char_u **)args->os_varp;
3822
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003823 if (**varp != NUL && STRCMP(*varp, "camel") != 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003824 return e_invalid_argument;
3825
3826 return NULL;
3827}
3828
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003829 int
3830expand_set_spelloptions(optexpand_T *args, int *numMatches, char_u ***matches)
3831{
3832 static char *(p_spo_values[]) = {"camel", NULL};
3833 return expand_set_opt_string(
3834 args,
3835 p_spo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003836 ARRAY_LENGTH(p_spo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003837 numMatches,
3838 matches);
3839}
3840
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003841/*
3842 * The 'spellsuggest' option is changed.
3843 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003844 char *
3845did_set_spellsuggest(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003846{
3847 if (spell_check_sps() != OK)
3848 return e_invalid_argument;
3849
3850 return NULL;
3851}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003852
3853 int
3854expand_set_spellsuggest(optexpand_T *args, int *numMatches, char_u ***matches)
3855{
3856 return expand_set_opt_string(
3857 args,
3858 p_sps_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003859 ARRAY_LENGTH(p_sps_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003860 numMatches,
3861 matches);
3862}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003863#endif
3864
3865/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003866 * The 'splitkeep' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003867 */
3868 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003869did_set_splitkeep(optset_T *args UNUSED)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003870{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003871 return did_set_opt_strings(p_spk, p_spk_values, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003872}
3873
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003874 int
3875expand_set_splitkeep(optexpand_T *args, int *numMatches, char_u ***matches)
3876{
3877 return expand_set_opt_string(
3878 args,
3879 p_spk_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003880 ARRAY_LENGTH(p_spk_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003881 numMatches,
3882 matches);
3883}
3884
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00003885#if defined(FEAT_STL_OPT) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003886/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003887 * The 'statusline' option is changed.
3888 */
3889 char *
3890did_set_statusline(optset_T *args)
3891{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003892 return parse_statustabline_rulerformat(args, FALSE);
3893}
3894#endif
3895
3896/*
3897 * The 'swapsync' option is changed.
3898 */
3899 char *
3900did_set_swapsync(optset_T *args UNUSED)
3901{
3902 return did_set_opt_strings(p_sws, p_sws_values, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003903}
3904
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003905 int
3906expand_set_swapsync(optexpand_T *args, int *numMatches, char_u ***matches)
3907{
3908 return expand_set_opt_string(
3909 args,
3910 p_sws_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003911 ARRAY_LENGTH(p_sws_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003912 numMatches,
3913 matches);
3914}
3915
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003916/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003917 * The 'switchbuf' option is changed.
3918 */
3919 char *
3920did_set_switchbuf(optset_T *args UNUSED)
3921{
3922 return did_set_opt_flags(p_swb, p_swb_values, &swb_flags, TRUE);
3923}
3924
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003925 int
3926expand_set_switchbuf(optexpand_T *args, int *numMatches, char_u ***matches)
3927{
3928 return expand_set_opt_string(
3929 args,
3930 p_swb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003931 ARRAY_LENGTH(p_swb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003932 numMatches,
3933 matches);
3934}
3935
LemonBoy5247b0b2024-07-12 19:30:58 +02003936/*
3937 * The 'tabclose' option is changed.
3938 */
3939 char *
3940did_set_tabclose(optset_T *args UNUSED)
3941{
3942 return did_set_opt_flags(p_tcl, p_tcl_values, &tcl_flags, TRUE);
3943}
3944
3945 int
3946expand_set_tabclose(optexpand_T *args, int *numMatches, char_u ***matches)
3947{
3948 return expand_set_opt_string(
3949 args,
3950 p_tcl_values,
3951 ARRAY_LENGTH(p_tcl_values) - 1,
3952 numMatches,
3953 matches);
3954}
3955
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003956#if defined(FEAT_STL_OPT) || defined(PROTO)
3957/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003958 * The 'tabline' option is changed.
3959 */
3960 char *
3961did_set_tabline(optset_T *args)
3962{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003963 return parse_statustabline_rulerformat(args, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003964}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003965#endif
3966
3967/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003968 * The 'tagcase' option is changed.
3969 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003970 char *
3971did_set_tagcase(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003972{
3973 unsigned int *flags;
3974 char_u *p;
3975
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003976 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003977 {
3978 p = curbuf->b_p_tc;
3979 flags = &curbuf->b_tc_flags;
3980 }
3981 else
3982 {
3983 p = p_tc;
3984 flags = &tc_flags;
3985 }
3986
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003987 if ((args->os_flags & OPT_LOCAL) && *p == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003988 // make the local value empty: use the global value
3989 *flags = 0;
3990 else if (*p == NUL
3991 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
3992 return e_invalid_argument;
3993
3994 return NULL;
3995}
3996
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003997 int
3998expand_set_tagcase(optexpand_T *args, int *numMatches, char_u ***matches)
3999{
4000 return expand_set_opt_string(
4001 args,
4002 p_tc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004003 ARRAY_LENGTH(p_tc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004004 numMatches,
4005 matches);
4006}
4007
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004008/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004009 * The 'term' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004010 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004011 char *
4012did_set_term(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004013{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004014 if (T_NAME[0] == NUL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004015 return e_cannot_set_term_to_empty_string;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004016#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004017 if (gui.in_use)
4018 return e_cannot_change_term_in_GUI;
4019 if (term_is_gui(T_NAME))
4020 return e_use_gui_to_start_GUI;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004021#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004022 if (set_termname(T_NAME) == FAIL)
4023 return e_not_found_in_termcap;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004024
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004025 // Screen colors may have changed.
4026 redraw_later_clear();
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004027
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004028 return NULL;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004029}
4030
4031/*
4032 * Some terminal option (t_xxx) is changed
4033 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004034 char *
4035did_set_term_option(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004036{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004037 char_u **varp = (char_u **)args->os_varp;
4038
4039 if (!full_screen)
4040 return NULL;
4041
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004042 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
4043 if (varp == &T_CCO)
4044 {
4045 int colors = atoi((char *)T_CCO);
4046
4047 // Only reinitialize colors if t_Co value has really changed to
4048 // avoid expensive reload of colorscheme if t_Co is set to the
4049 // same value multiple times.
4050 if (colors != t_colors)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004051 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004052 t_colors = colors;
4053 if (t_colors <= 1)
4054 {
4055 vim_free(T_CCO);
4056 T_CCO = empty_option;
4057 }
4058#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
4059 if (is_term_win32())
4060 {
4061 swap_tcap();
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004062 args->os_did_swaptcap = TRUE;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004063 }
4064#endif
4065 // We now have a different color setup, initialize it again.
4066 init_highlight(TRUE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004067 }
4068 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004069 ttest(FALSE);
4070 if (varp == &T_ME)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004071 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004072 out_str(T_ME);
4073 redraw_later(UPD_CLEAR);
4074#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4075 // Since t_me has been set, this probably means that the user
4076 // wants to use this as default colors. Need to reset default
4077 // background/foreground colors.
4078# ifdef VIMDLL
4079 if (!gui.in_use && !gui.starting)
4080# endif
4081 mch_set_normal_colors();
4082#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004083 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004084 if (varp == &T_BE && termcap_active)
4085 {
4086 MAY_WANT_TO_LOG_THIS;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004087
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004088 if (*T_BE == NUL)
4089 // When clearing t_BE we assume the user no longer wants
4090 // bracketed paste, thus disable it by writing t_BD.
4091 out_str(T_BD);
4092 else
4093 out_str(T_BE);
4094 }
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004095
4096 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004097}
4098
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004099#if defined(FEAT_TERMINAL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004100/*
4101 * The 'termwinkey' option is changed.
4102 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004103 char *
Milly94606f72024-10-22 22:07:52 +02004104did_set_termwinkey(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004105{
Milly94606f72024-10-22 22:07:52 +02004106 char_u **varp = (char_u **)args->os_varp;
4107
4108 if ((*varp)[0] != NUL && string_to_key(*varp, TRUE) == 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004109 return e_invalid_argument;
4110
4111 return NULL;
4112}
4113
4114/*
4115 * The 'termwinsize' option is changed.
4116 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004117 char *
Milly8be10aa2024-10-22 22:01:46 +02004118did_set_termwinsize(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004119{
Milly8be10aa2024-10-22 22:01:46 +02004120 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004121 char_u *p;
4122
Milly8be10aa2024-10-22 22:01:46 +02004123 if ((*varp)[0] == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004124 return NULL;
4125
Milly8be10aa2024-10-22 22:01:46 +02004126 p = skipdigits(*varp);
4127 if (p == *varp || (*p != 'x' && *p != '*') || *skipdigits(p + 1) != NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004128 return e_invalid_argument;
4129
4130 return NULL;
4131}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00004132
4133# if defined(MSWIN) || defined(PROTO)
4134/*
4135 * The 'termwintype' option is changed.
4136 */
4137 char *
4138did_set_termwintype(optset_T *args UNUSED)
4139{
4140 return did_set_opt_strings(p_twt, p_twt_values, FALSE);
4141}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004142
4143 int
4144expand_set_termwintype(optexpand_T *args, int *numMatches, char_u ***matches)
4145{
4146 return expand_set_opt_string(
4147 args,
4148 p_twt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004149 ARRAY_LENGTH(p_twt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004150 numMatches,
4151 matches);
4152}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00004153# endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004154#endif
4155
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004156/*
4157 * The 'titlestring' option is changed.
4158 */
4159 char *
4160did_set_titlestring(optset_T *args)
4161{
4162 int flagval = 0;
4163
4164#ifdef FEAT_STL_OPT
4165 flagval = STL_IN_TITLE;
4166#endif
4167 return parse_titleiconstring(args, flagval);
4168}
4169
4170#if (defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)) || defined(PROTO)
4171/*
4172 * The 'toolbar' option is changed.
4173 */
4174 char *
4175did_set_toolbar(optset_T *args UNUSED)
4176{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004177 if (opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags,
4178 TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004179 return e_invalid_argument;
4180
4181 out_flush();
4182 gui_mch_show_toolbar((toolbar_flags &
4183 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4184 return NULL;
4185}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004186
4187 int
4188expand_set_toolbar(optexpand_T *args, int *numMatches, char_u ***matches)
4189{
4190 return expand_set_opt_string(
4191 args,
4192 p_toolbar_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004193 ARRAY_LENGTH(p_toolbar_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004194 numMatches,
4195 matches);
4196}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004197#endif
4198
4199#if (defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)) || defined(PROTO)
4200/*
4201 * The 'toolbariconsize' option is changed. GTK+ 2 only.
4202 */
4203 char *
4204did_set_toolbariconsize(optset_T *args UNUSED)
4205{
4206 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
4207 return e_invalid_argument;
4208
4209 out_flush();
4210 gui_mch_show_toolbar((toolbar_flags &
4211 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4212 return NULL;
4213}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004214
4215 int
4216expand_set_toolbariconsize(optexpand_T *args, int *numMatches, char_u ***matches)
4217{
4218 return expand_set_opt_string(
4219 args,
4220 p_tbis_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004221 ARRAY_LENGTH(p_tbis_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004222 numMatches,
4223 matches);
4224}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004225#endif
4226
4227#if defined(UNIX) || defined(VMS) || defined(PROTO)
4228/*
4229 * The 'ttymouse' option is changed.
4230 */
4231 char *
4232did_set_ttymouse(optset_T *args UNUSED)
4233{
4234 char *errmsg = NULL;
4235
4236 // Switch the mouse off before changing the escape sequences used for
4237 // that.
4238 mch_setmouse(FALSE);
4239 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
4240 errmsg = e_invalid_argument;
4241 else
4242 check_mouse_termcode();
4243 if (termcap_active)
4244 setmouse(); // may switch it on again
4245
4246 return errmsg;
4247}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004248
4249 int
4250expand_set_ttymouse(optexpand_T *args, int *numMatches, char_u ***matches)
4251{
4252 return expand_set_opt_string(
4253 args,
4254 p_ttym_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004255 ARRAY_LENGTH(p_ttym_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004256 numMatches,
4257 matches);
4258}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004259#endif
4260
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004261#if defined(FEAT_VARTABS) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004262/*
4263 * The 'varsofttabstop' option is changed.
4264 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004265 char *
4266did_set_varsofttabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004267{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004268 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004269 char_u *cp;
4270
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004271 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004272 VIM_CLEAR(curbuf->b_p_vsts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004273 else
4274 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004275 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004276 {
4277 if (vim_isdigit(*cp))
4278 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004279 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004280 continue;
4281 return e_invalid_argument;
4282 }
4283
4284 int *oldarray = curbuf->b_p_vsts_array;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004285 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004286 {
4287 if (oldarray)
4288 vim_free(oldarray);
4289 }
4290 else
4291 return e_invalid_argument;
4292 }
4293
4294 return NULL;
4295}
4296
4297/*
4298 * The 'vartabstop' option is changed.
4299 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004300 char *
4301did_set_vartabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004302{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004303 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004304 char_u *cp;
4305
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004306 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004307 VIM_CLEAR(curbuf->b_p_vts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004308 else
4309 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004310 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004311 {
4312 if (vim_isdigit(*cp))
4313 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004314 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004315 continue;
4316 return e_invalid_argument;
4317 }
4318
4319 int *oldarray = curbuf->b_p_vts_array;
4320
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004321 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004322 {
4323 vim_free(oldarray);
4324# ifdef FEAT_FOLDING
4325 if (foldmethodIsIndent(curwin))
4326 foldUpdateAll(curwin);
4327# endif
4328 }
4329 else
4330 return e_invalid_argument;
4331 }
4332
4333 return NULL;
4334}
4335#endif
4336
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004337/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004338 * The 'verbosefile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004339 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004340 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004341did_set_verbosefile(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004342{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004343 verbose_stop();
4344 if (*p_vfile != NUL && verbose_open() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004345 return e_invalid_argument;
4346
4347 return NULL;
4348}
4349
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004350#if defined(FEAT_SESSION) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004351/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004352 * The 'viewoptions' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004353 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004354 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004355did_set_viewoptions(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004356{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004357 return did_set_opt_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004358}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004359#endif
4360
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004361#if defined(FEAT_VIMINFO) || defined(PROTO)
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004362/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004363 * The 'viminfo' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004364 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004365 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004366did_set_viminfo(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004367{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004368 char_u *s;
4369 char *errmsg = NULL;
4370
4371 for (s = p_viminfo; *s;)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004372 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004373 // Check it's a valid character
4374 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
4375 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01004376 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004377 break;
4378 }
4379 if (*s == 'n') // name is always last one
4380 break;
4381 else if (*s == 'r') // skip until next ','
4382 {
4383 while (*++s && *s != ',')
4384 ;
4385 }
4386 else if (*s == '%')
4387 {
4388 // optional number
4389 while (vim_isdigit(*++s))
4390 ;
4391 }
4392 else if (*s == '!' || *s == 'h' || *s == 'c')
4393 ++s; // no extra chars
4394 else // must have a number
4395 {
4396 while (vim_isdigit(*++s))
4397 ;
4398
4399 if (!VIM_ISDIGIT(*(s - 1)))
4400 {
4401 if (args->os_errbuf != NULL)
4402 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01004403 vim_snprintf(args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004404 _(e_missing_number_after_angle_str_angle),
4405 transchar_byte(*(s - 1)));
4406 errmsg = args->os_errbuf;
4407 }
4408 else
4409 errmsg = "";
4410 break;
4411 }
4412 }
4413 if (*s == ',')
4414 ++s;
4415 else if (*s)
4416 {
4417 if (args->os_errbuf != NULL)
4418 errmsg = e_missing_comma;
4419 else
4420 errmsg = "";
4421 break;
4422 }
4423 }
4424 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
4425 errmsg = e_must_specify_a_value;
4426
4427 return errmsg;
4428}
4429#endif
4430
4431/*
4432 * The 'virtualedit' option is changed.
4433 */
4434 char *
4435did_set_virtualedit(optset_T *args)
4436{
4437 char_u *ve = p_ve;
4438 unsigned int *flags = &ve_flags;
4439
4440 if (args->os_flags & OPT_LOCAL)
4441 {
4442 ve = curwin->w_p_ve;
4443 flags = &curwin->w_ve_flags;
4444 }
4445
4446 if ((args->os_flags & OPT_LOCAL) && *ve == NUL)
4447 // make the local value empty: use the global value
4448 *flags = 0;
4449 else
4450 {
4451 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
4452 return e_invalid_argument;
4453 else if (STRCMP(ve, args->os_oldval.string) != 0)
4454 {
4455 // Recompute cursor position in case the new 've' setting
4456 // changes something.
4457 validate_virtcol();
4458 coladvance(curwin->w_virtcol);
4459 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004460 }
4461
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004462 return NULL;
4463}
4464
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004465 int
4466expand_set_virtualedit(optexpand_T *args, int *numMatches, char_u ***matches)
4467{
4468 return expand_set_opt_string(
4469 args,
4470 p_ve_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004471 ARRAY_LENGTH(p_ve_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004472 numMatches,
4473 matches);
4474}
4475
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004476/*
4477 * The 'whichwrap' option is changed.
4478 */
4479 char *
4480did_set_whichwrap(optset_T *args)
4481{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004482 char_u **varp = (char_u **)args->os_varp;
4483
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004484 // Add ',' to the list flags because 'whichwrap' is a flag
4485 // list that is comma-separated.
Christian Brabandtb39b2402023-11-29 11:34:05 +01004486 return did_set_option_listflag(*varp, (char_u *)(WW_ALL ","),
4487 args->os_errbuf, args->os_errbuflen);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004488}
4489
4490 int
4491expand_set_whichwrap(optexpand_T *args, int *numMatches, char_u ***matches)
4492{
4493 return expand_set_opt_listflag(args, (char_u*)WW_ALL, numMatches, matches);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004494}
4495
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004496/*
4497 * The 'wildmode' option is changed.
4498 */
4499 char *
4500did_set_wildmode(optset_T *args UNUSED)
4501{
4502 if (check_opt_wim() == FAIL)
4503 return e_invalid_argument;
4504 return NULL;
4505}
4506
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004507 int
4508expand_set_wildmode(optexpand_T *args, int *numMatches, char_u ***matches)
4509{
4510 return expand_set_opt_string(
4511 args,
4512 p_wim_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004513 ARRAY_LENGTH(p_wim_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004514 numMatches,
4515 matches);
4516}
4517
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004518/*
4519 * The 'wildoptions' option is changed.
4520 */
4521 char *
4522did_set_wildoptions(optset_T *args UNUSED)
4523{
4524 return did_set_opt_strings(p_wop, p_wop_values, TRUE);
4525}
4526
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004527 int
4528expand_set_wildoptions(optexpand_T *args, int *numMatches, char_u ***matches)
4529{
4530 return expand_set_opt_string(
4531 args,
4532 p_wop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004533 ARRAY_LENGTH(p_wop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004534 numMatches,
4535 matches);
4536}
4537
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004538#if defined(FEAT_WAK) || defined(PROTO)
4539/*
4540 * The 'winaltkeys' option is changed.
4541 */
4542 char *
4543did_set_winaltkeys(optset_T *args UNUSED)
4544{
4545 char *errmsg = NULL;
4546
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004547 if (*p_wak == NUL || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004548 errmsg = e_invalid_argument;
4549# ifdef FEAT_MENU
4550# if defined(FEAT_GUI_MOTIF)
4551 else if (gui.in_use)
4552 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4553# elif defined(FEAT_GUI_GTK)
4554 else if (gui.in_use)
4555 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4556# endif
4557# endif
4558 return errmsg;
4559}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004560
4561 int
4562expand_set_winaltkeys(optexpand_T *args, int *numMatches, char_u ***matches)
4563{
4564 return expand_set_opt_string(
4565 args,
4566 p_wak_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004567 ARRAY_LENGTH(p_wak_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004568 numMatches,
4569 matches);
4570}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004571#endif
4572
4573/*
4574 * The 'wincolor' option is changed.
4575 */
4576 char *
4577did_set_wincolor(optset_T *args UNUSED)
4578{
4579#ifdef FEAT_TERMINAL
4580 term_update_wincolor(curwin);
4581#endif
4582 return NULL;
4583}
4584
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004585 int
4586expand_set_wincolor(optexpand_T *args, int *numMatches, char_u ***matches)
4587{
4588 return expand_set_opt_generic(
4589 args,
4590 get_highlight_name,
4591 numMatches,
4592 matches);
4593}
4594
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004595#ifdef FEAT_SYN_HL
4596/*
4597 * When the 'syntax' option is set, load the syntax of that name.
4598 */
4599 static void
4600do_syntax_autocmd(int value_changed)
4601{
4602 static int syn_recursive = 0;
4603
4604 ++syn_recursive;
4605 // Only pass TRUE for "force" when the value changed or not used
4606 // recursively, to avoid endless recurrence.
4607 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
4608 value_changed || syn_recursive == 1, curbuf);
4609 curbuf->b_flags |= BF_SYN_SET;
4610 --syn_recursive;
4611}
4612#endif
4613
4614/*
4615 * When the 'filetype' option is set, trigger the FileType autocommand.
4616 */
4617 static void
4618do_filetype_autocmd(char_u **varp, int opt_flags, int value_changed)
4619{
4620 // Skip this when called from a modeline and the filetype was already set
4621 // to this value.
4622 if ((opt_flags & OPT_MODELINE) && !value_changed)
4623 return;
4624
4625 static int ft_recursive = 0;
4626 int secure_save = secure;
4627
4628 // Reset the secure flag, since the value of 'filetype' has
4629 // been checked to be safe.
4630 secure = 0;
4631
4632 ++ft_recursive;
zeertzjq5bf6c212024-03-31 18:41:27 +02004633 curbuf->b_did_filetype = TRUE;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004634 // Only pass TRUE for "force" when the value changed or not
4635 // used recursively, to avoid endless recurrence.
4636 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
4637 value_changed || ft_recursive == 1, curbuf);
4638 --ft_recursive;
4639 // Just in case the old "curbuf" is now invalid.
4640 if (varp != &(curbuf->b_p_ft))
4641 varp = NULL;
4642
4643 secure = secure_save;
4644}
4645
4646#ifdef FEAT_SPELL
4647/*
4648 * When the 'spelllang' option is set, source the spell/LANG.vim file in
4649 * 'runtimepath'.
4650 */
4651 static void
4652do_spelllang_source(void)
4653{
4654 char_u fname[200];
4655 char_u *p;
4656 char_u *q = curwin->w_s->b_p_spl;
4657
4658 // Skip the first name if it is "cjk".
4659 if (STRNCMP(q, "cjk,", 4) == 0)
4660 q += 4;
4661
4662 // They could set 'spellcapcheck' depending on the language. Use the first
4663 // name in 'spelllang' up to '_region' or '.encoding'.
4664 for (p = q; *p != NUL; ++p)
4665 if (!ASCII_ISALNUM(*p) && *p != '-')
4666 break;
4667 if (p > q)
4668 {
4669 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
4670 (int)(p - q), q);
4671 source_runtime(fname, DIP_ALL);
4672 }
4673}
4674#endif
4675
4676/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004677 * Handle string options that need some action to perform when changed.
zeertzjqf6782732022-07-27 18:26:03 +01004678 * The new value must be allocated.
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004679 * Returns NULL for success, or an untranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +02004680 */
4681 char *
4682did_set_string_option(
4683 int opt_idx, // index in options[] table
4684 char_u **varp, // pointer to the option variable
Bram Moolenaardac13472019-09-16 21:06:21 +02004685 char_u *oldval, // previous value of the option
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004686 char_u *value, // new value of the option
Bram Moolenaardac13472019-09-16 21:06:21 +02004687 char *errbuf, // buffer for errors, or NULL
Mike Williams620f0112023-12-05 15:36:06 +01004688 size_t errbuflen, // length of error buffer
Bram Moolenaardac13472019-09-16 21:06:21 +02004689 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004690 set_op_T op, // OP_ADDING/OP_PREPENDING/OP_REMOVING
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004691 int *value_checked) // value was checked to be safe, no
Bram Moolenaardac13472019-09-16 21:06:21 +02004692 // need to set P_INSECURE
4693{
4694 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +02004695 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004696 opt_did_set_cb_T did_set_cb = get_option_did_set_cb(opt_idx);
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004697 optset_T args;
4698
4699 // 'ttytype' is an alias for 'term'. Both 'term' and 'ttytype' point to
4700 // T_NAME. If 'term' or 'ttytype' is modified, then use the index for the
4701 // 'term' option. Only set the P_ALLOCED flag on 'term'.
4702 if (varp == &T_NAME)
4703 {
4704 opt_idx = findoption((char_u *)"term");
4705 if (opt_idx >= 0)
4706 {
4707 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
4708 did_set_cb = get_option_did_set_cb(opt_idx);
4709 }
4710 }
4711
4712 CLEAR_FIELD(args);
Bram Moolenaardac13472019-09-16 21:06:21 +02004713
Bram Moolenaardac13472019-09-16 21:06:21 +02004714 // Disallow changing some options from secure mode
4715 if ((secure
4716#ifdef HAVE_SANDBOX
4717 || sandbox != 0
4718#endif
4719 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00004720 errmsg = e_not_allowed_here;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004721 // Check for a "normal" directory or file name in some options.
4722 else if (check_illegal_path_names(opt_idx, varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004723 errmsg = e_invalid_argument;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004724 else if (did_set_cb != NULL)
4725 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004726 args.os_varp = (char_u *)varp;
4727 args.os_idx = opt_idx;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004728 args.os_flags = opt_flags;
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004729 args.os_op = op;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004730 args.os_oldval.string = oldval;
4731 args.os_newval.string = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004732 args.os_errbuf = errbuf;
Christian Brabandtb39b2402023-11-29 11:34:05 +01004733 args.os_errbuflen = errbuflen;
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004734 // Invoke the option specific callback function to validate and apply
4735 // the new option value.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004736 errmsg = did_set_cb(&args);
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004737
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004738 // The 'keymap', 'filetype' and 'syntax' option callback functions
4739 // may change the os_value_checked field.
4740 *value_checked = args.os_value_checked;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004741 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004742
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004743 // If an error is detected, restore the previous value.
Bram Moolenaardac13472019-09-16 21:06:21 +02004744 if (errmsg != NULL)
4745 {
zeertzjqf6782732022-07-27 18:26:03 +01004746 free_string_option(*varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02004747 *varp = oldval;
4748 // When resetting some values, need to act on it.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004749 if (args.os_restore_chartab)
Bram Moolenaardac13472019-09-16 21:06:21 +02004750 (void)init_chartab();
4751 if (varp == &p_hl)
4752 (void)highlight_changed();
4753 }
4754 else
4755 {
4756#ifdef FEAT_EVAL
4757 // Remember where the option was set.
4758 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4759#endif
4760 // Free string options that are in allocated memory.
4761 // Use "free_oldval", because recursiveness may change the flags under
4762 // our fingers (esp. init_highlight()).
4763 if (free_oldval)
4764 free_string_option(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01004765 set_option_flag(opt_idx, P_ALLOCED);
Bram Moolenaardac13472019-09-16 21:06:21 +02004766
4767 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
4768 && is_global_local_option(opt_idx))
4769 {
4770 // global option with local value set to use global value; free
4771 // the local value and make it empty
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004772 char_u *p = get_option_varp_scope(opt_idx, OPT_LOCAL);
Bram Moolenaardac13472019-09-16 21:06:21 +02004773 free_string_option(*(char_u **)p);
4774 *(char_u **)p = empty_option;
4775 }
4776
4777 // May set global value for local option.
4778 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
4779 set_string_option_global(opt_idx, varp);
4780
4781 // Trigger the autocommand only after setting the flags.
4782#ifdef FEAT_SYN_HL
Bram Moolenaardac13472019-09-16 21:06:21 +02004783 if (varp == &(curbuf->b_p_syn))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004784 do_syntax_autocmd(args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004785#endif
4786 else if (varp == &(curbuf->b_p_ft))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004787 do_filetype_autocmd(varp, opt_flags, args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004788#ifdef FEAT_SPELL
4789 if (varp == &(curwin->w_s->b_p_spl))
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004790 do_spelllang_source();
Bram Moolenaardac13472019-09-16 21:06:21 +02004791#endif
4792 }
4793
Bram Moolenaardac13472019-09-16 21:06:21 +02004794 if (varp == &p_mouse)
4795 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004796 if (*p_mouse == NUL)
4797 mch_setmouse(FALSE); // switch mouse off
4798 else
Bram Moolenaardac13472019-09-16 21:06:21 +02004799 setmouse(); // in case 'mouse' changed
4800 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004801
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004802 if (varp == &p_rtp)
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004803 {
4804 export_myvimdir();
4805#if defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004806 update_package_paths_in_lua();
4807#endif
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004808 }
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004809
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004810#if defined(FEAT_LINEBREAK)
4811 // Changing Formatlistpattern when briopt includes the list setting:
4812 // redraw
4813 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
4814 && curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004815 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004816#endif
4817
Bram Moolenaardac13472019-09-16 21:06:21 +02004818 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004819 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0
4820 && (get_option_flags(opt_idx) & P_HLONLY) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02004821 curwin->w_set_curswant = TRUE;
4822
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004823 if ((opt_flags & OPT_NO_REDRAW) == 0)
4824 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004825#ifdef FEAT_GUI
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004826 // set when changing an option that only requires a redraw in the GUI
4827 int redraw_gui_only = FALSE;
4828
4829 if (varp == &p_go // 'guioptions'
4830 || varp == &p_guifont // 'guifont'
4831# ifdef FEAT_GUI_TABLINE
4832 || varp == &p_gtl // 'guitablabel'
4833 || varp == &p_gtt // 'guitabtooltip'
4834# endif
4835# ifdef FEAT_XFONTSET
4836 || varp == &p_guifontset // 'guifontset'
4837# endif
4838 || varp == &p_guifontwide // 'guifontwide'
Erik S. V. Jansson2f026382024-02-26 22:23:05 +01004839# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004840 || varp == &p_guiligatures // 'guiligatures'
4841# endif
4842 )
4843 redraw_gui_only = TRUE;
4844
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004845 // check redraw when it's not a GUI option or the GUI is active.
4846 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02004847#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004848 check_redraw(get_option_flags(opt_idx));
4849 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004850
4851#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004852 if (args.os_did_swaptcap)
Bram Moolenaardac13472019-09-16 21:06:21 +02004853 {
4854 set_termname((char_u *)"win32");
4855 init_highlight(TRUE, FALSE);
4856 }
4857#endif
4858
4859 return errmsg;
4860}
4861
4862/*
4863 * Check an option that can be a range of string values.
4864 *
4865 * Return OK for correct value, FAIL otherwise.
4866 * Empty is always OK.
4867 */
4868 static int
4869check_opt_strings(
4870 char_u *val,
4871 char **values,
4872 int list) // when TRUE: accept a list of values
4873{
4874 return opt_strings_flags(val, values, NULL, list);
4875}
4876
4877/*
4878 * Handle an option that can be a range of string values.
4879 * Set a flag in "*flagp" for each string present.
4880 *
4881 * Return OK for correct value, FAIL otherwise.
4882 * Empty is always OK.
4883 */
4884 static int
4885opt_strings_flags(
4886 char_u *val, // new value
4887 char **values, // array of valid string values
4888 unsigned *flagp,
4889 int list) // when TRUE: accept a list of values
4890{
4891 int i;
4892 int len;
4893 unsigned new_flags = 0;
4894
4895 while (*val)
4896 {
4897 for (i = 0; ; ++i)
4898 {
4899 if (values[i] == NULL) // val not found in values[]
4900 return FAIL;
4901
4902 len = (int)STRLEN(values[i]);
4903 if (STRNCMP(values[i], val, len) == 0
4904 && ((list && val[len] == ',') || val[len] == NUL))
4905 {
4906 val += len + (val[len] == ',');
4907 new_flags |= (1 << i);
4908 break; // check next item in val list
4909 }
4910 }
4911 }
4912 if (flagp != NULL)
4913 *flagp = new_flags;
4914
4915 return OK;
4916}
4917
4918/*
4919 * return OK if "p" is a valid fileformat name, FAIL otherwise.
4920 */
4921 int
4922check_ff_value(char_u *p)
4923{
4924 return check_opt_strings(p, p_ff_values, FALSE);
4925}
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004926
4927/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004928 * Save the actual shortmess Flags and clear them temporarily to avoid that
4929 * file messages overwrites any output from the following commands.
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004930 *
4931 * Caller must make sure to first call save_clear_shm_value() and then
4932 * restore_shm_value() exactly the same number of times.
4933 */
4934 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004935save_clear_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004936{
4937 if (STRLEN(p_shm) >= SHM_LEN)
4938 {
4939 iemsg(e_internal_error_shortmess_too_long);
4940 return;
4941 }
4942
4943 if (++set_shm_recursive == 1)
4944 {
4945 STRCPY(shm_buf, p_shm);
4946 set_option_value_give_err((char_u *)"shm", 0L, (char_u *)"", 0);
4947 }
4948}
4949
4950/*
4951 * Restore the shortmess Flags set from the save_clear_shm_value() function.
4952 */
4953 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004954restore_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004955{
4956 if (--set_shm_recursive == 0)
4957 {
4958 set_option_value_give_err((char_u *)"shm", 0L, shm_buf, 0);
4959 vim_memset(shm_buf, 0, SHM_LEN);
4960 }
4961}
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004962
4963/*
4964 * Export the environment variable $MYVIMDIR to the first item in runtimepath
4965 */
Christian Brabandt1a741d32025-03-01 16:30:33 +01004966 void
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004967export_myvimdir()
4968{
4969 int dofree = FALSE;
4970 char_u *p;
4971 char_u *q = p_rtp;
4972 char_u *buf = alloc(MAXPATHL);
4973
4974 if (buf == NULL)
4975 return;
4976
4977 (void)copy_option_part(&q, buf, MAXPATHL, ",");
4978
4979 p = vim_getenv((char_u *)"MYVIMDIR", &dofree);
4980
4981 if (p == NULL || STRCMP(p, buf) != 0)
4982 {
4983 add_pathsep(buf);
4984#ifdef MSWIN
4985 // normalize path separators
4986 for (q = buf; *q != NUL; q++)
4987 if (*q == '/')
4988 *q = '\\';
4989#endif
4990 vim_setenv((char_u *)"MYVIMDIR", buf);
4991 }
4992 if (dofree)
4993 vim_free(p);
4994 vim_free(buf);
4995}