blob: 60e28d68b77eb9666578cd9e8f77d995d4710b16 [file] [log] [blame]
Bram Moolenaardac13472019-09-16 21:06:21 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * optionstr.c: Functions related to string options
12 */
13
14#include "vim.h"
15
Christian Brabandt9aee8ec2022-12-16 16:41:23 +000016static char_u shm_buf[SHM_LEN];
17static int set_shm_recursive = 0;
18
Bram Moolenaardac13472019-09-16 21:06:21 +020019static char *(p_ambw_values[]) = {"single", "double", NULL};
20static char *(p_bg_values[]) = {"light", "dark", NULL};
21static char *(p_bkc_values[]) = {"yes", "auto", "no", "breaksymlink", "breakhardlink", NULL};
22static char *(p_bo_values[]) = {"all", "backspace", "cursor", "complete",
23 "copy", "ctrlg", "error", "esc", "ex",
24 "hangul", "insertmode", "lang", "mess",
25 "showmatch", "operator", "register", "shell",
LemonBoy77771d32022-04-13 11:47:25 +010026 "spell", "term", "wildmode", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020027#if defined(FEAT_LINEBREAK)
28// Note: Keep this in sync with briopt_check()
29static char *(p_briopt_values[]) = {"shift:", "min:", "sbr", "list:", "column:", NULL};
30#endif
31#if defined(FEAT_DIFF)
32// Note: Keep this in sync with diffopt_changed()
Yee Cheng Chin9943d472025-03-26 19:41:02 +010033static char *(p_dip_values[]) = {"filler", "context:", "iblank", "icase", "iwhite", "iwhiteall", "iwhiteeol", "horizontal", "vertical", "closeoff", "hiddenoff", "foldcolumn:", "followwrap", "internal", "indent-heuristic", "algorithm:", "inline:", "linematch:", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020034static char *(p_dip_algorithm_values[]) = {"myers", "minimal", "patience", "histogram", NULL};
Yee Cheng Chin9943d472025-03-26 19:41:02 +010035static char *(p_dip_inline_values[]) = {"none", "simple", "char", "word", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020036#endif
distobs25ac6d62024-07-06 17:50:09 +020037static char *(p_nf_values[]) = {"bin", "octal", "hex", "alpha", "unsigned", "blank", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020038static char *(p_ff_values[]) = {FF_UNIX, FF_DOS, FF_MAC, NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020039#ifdef FEAT_CLIPBOARD
40// Note: Keep this in sync with did_set_clipboard()
41static char *(p_cb_values[]) = {"unnamed", "unnamedplus", "autoselect", "autoselectplus", "autoselectml", "html", "exclude:", NULL};
42#endif
Bram Moolenaardac13472019-09-16 21:06:21 +020043#ifdef FEAT_CRYPT
Christian Brabandtf573c6e2021-06-20 14:02:16 +020044static char *(p_cm_values[]) = {"zip", "blowfish", "blowfish2",
45 # ifdef FEAT_SODIUM
Christian Brabandtaae58342023-04-23 17:50:22 +010046 "xchacha20", "xchacha20v2",
Christian Brabandtf573c6e2021-06-20 14:02:16 +020047 # endif
48 NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020049#endif
50static char *(p_cmp_values[]) = {"internal", "keepascii", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020051#ifdef FEAT_SYN_HL
52// Note: Keep this in sync with fill_culopt_flags()
53static char *(p_culopt_values[]) = {"line", "screenline", "number", "both", NULL};
54#endif
Bram Moolenaardac13472019-09-16 21:06:21 +020055static char *(p_dy_values[]) = {"lastline", "truncate", "uhex", NULL};
Yegappan Lakshmanan87018252023-09-20 20:20:04 +020056static char *(p_jop_values[]) = {"stack", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020057#ifdef FEAT_FOLDING
58static char *(p_fdo_values[]) = {"all", "block", "hor", "mark", "percent",
59 "quickfix", "search", "tag", "insert",
60 "undo", "jump", NULL};
61#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +020062// Note: Keep this in sync with match_keyprotocol()
63static char *(p_kpc_protocol_values[]) = {"none", "mok2", "kitty", NULL};
64#ifdef FEAT_PROP_POPUP
65// Note: Keep this in sync with parse_popup_option()
66static char *(p_popup_option_values[]) = {"height:", "width:", "highlight:", "border:", "align:", NULL};
67static char *(p_popup_option_border_values[]) = {"on", "off", NULL};
68static char *(p_popup_option_align_values[]) = {"item", "menu", NULL};
69#endif
70#if defined(FEAT_SPELL)
71// Note: Keep this in sync with spell_check_sps()
72static char *(p_sps_values[]) = {"best", "fast", "double", "expr:", "file:", "timeout:", NULL};
73#endif
Bram Moolenaardac13472019-09-16 21:06:21 +020074#ifdef FEAT_SESSION
Bram Moolenaar635bd602021-04-16 19:58:22 +020075// Also used for 'viewoptions'! Keep in sync with SSOP_ flags.
Bram Moolenaardac13472019-09-16 21:06:21 +020076static char *(p_ssop_values[]) = {"buffers", "winpos", "resize", "winsize",
77 "localoptions", "options", "help", "blank", "globals", "slash", "unix",
Bram Moolenaar635bd602021-04-16 19:58:22 +020078 "sesdir", "curdir", "folds", "cursor", "tabpages", "terminal", "skiprtp",
79 NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020080#endif
Bram Moolenaar539aa6b2019-11-17 18:09:38 +010081// Keep in sync with SWB_ flags in option.h
82static char *(p_swb_values[]) = {"useopen", "usetab", "split", "newtab", "vsplit", "uselast", NULL};
Luuk van Baal13ece2a2022-10-03 15:28:08 +010083static char *(p_spk_values[]) = {"cursor", "screen", "topline", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020084static char *(p_tc_values[]) = {"followic", "ignore", "match", "followscs", "smart", NULL};
LemonBoy5247b0b2024-07-12 19:30:58 +020085// Keep in sync with TCL_ flags in option.h
86static char *(p_tcl_values[]) = {"left", "uselast", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020087#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
88static char *(p_toolbar_values[]) = {"text", "icons", "tooltips", "horiz", NULL};
89#endif
90#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
91static char *(p_tbis_values[]) = {"tiny", "small", "medium", "large", "huge", "giant", NULL};
92#endif
Bram Moolenaara1cb1d12019-10-17 23:00:07 +020093#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +020094static char *(p_ttym_values[]) = {"xterm", "xterm2", "dec", "netterm", "jsbterm", "pterm", "urxvt", "sgr", NULL};
95#endif
Gary Johnson53ba05b2021-07-26 22:19:10 +020096static char *(p_ve_values[]) = {"block", "insert", "all", "onemore", "none", "NONE", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020097// Note: Keep this in sync with check_opt_wim()
Girish Palya2bacc3e2025-03-02 22:55:57 +010098static char *(p_wim_values[]) = {"full", "longest", "list", "lastused", "noselect", NULL};
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000099static char *(p_wop_values[]) = {"fuzzy", "tagfile", "pum", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200100#ifdef FEAT_WAK
101static char *(p_wak_values[]) = {"yes", "menu", "no", NULL};
102#endif
103static char *(p_mousem_values[]) = {"extend", "popup", "popup_setpos", "mac", NULL};
104static char *(p_sel_values[]) = {"inclusive", "exclusive", "old", NULL};
105static char *(p_slm_values[]) = {"mouse", "key", "cmd", NULL};
106static char *(p_km_values[]) = {"startsel", "stopsel", NULL};
107#ifdef FEAT_BROWSE
108static char *(p_bsdir_values[]) = {"current", "last", "buffer", NULL};
109#endif
110static char *(p_scbopt_values[]) = {"ver", "hor", "jump", NULL};
111static char *(p_debug_values[]) = {"msg", "throw", "beep", NULL};
112static char *(p_ead_values[]) = {"both", "ver", "hor", NULL};
113static char *(p_buftype_values[]) = {"nofile", "nowrite", "quickfix", "help", "terminal", "acwrite", "prompt", "popup", NULL};
114static char *(p_bufhidden_values[]) = {"hide", "unload", "delete", "wipe", NULL};
Bram Moolenaaraa0489e2020-04-17 19:41:21 +0200115static char *(p_bs_values[]) = {"indent", "eol", "start", "nostop", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200116#ifdef FEAT_FOLDING
117static char *(p_fdm_values[]) = {"manual", "expr", "marker", "indent", "syntax",
118# ifdef FEAT_DIFF
119 "diff",
120# endif
121 NULL};
122static char *(p_fcl_values[]) = {"all", NULL};
123#endif
glepnirf31cfa22025-03-06 21:59:13 +0100124static char *(p_cfc_values[]) = {"keyword", "files", "whole_line", NULL};
glepniredd4ac32025-01-29 18:53:51 +0100125static char *(p_cot_values[]) = {"menu", "menuone", "longest", "preview", "popup", "popuphidden", "noinsert", "noselect", "fuzzy", "nosort", "preinsert", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200126#ifdef BACKSLASH_IN_FILENAME
127static char *(p_csl_values[]) = {"slash", "backslash", NULL};
128#endif
129#ifdef FEAT_SIGNS
130static char *(p_scl_values[]) = {"yes", "no", "auto", "number", NULL};
131#endif
132#if defined(MSWIN) && defined(FEAT_TERMINAL)
133static char *(p_twt_values[]) = {"winpty", "conpty", "", NULL};
134#endif
Luuk van Baalba936f62022-12-15 13:15:39 +0000135static char *(p_sloc_values[]) = {"last", "statusline", "tabline", NULL};
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +0000136static char *(p_sws_values[]) = {"fsync", "sync", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200137
138static int check_opt_strings(char_u *val, char **values, int list);
139static int opt_strings_flags(char_u *val, char **values, unsigned *flagp, int list);
140
141/*
142 * After setting various option values: recompute variables that depend on
143 * option values.
144 */
145 void
146didset_string_options(void)
147{
148 (void)opt_strings_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
149 (void)opt_strings_flags(p_bkc, p_bkc_values, &bkc_flags, TRUE);
150 (void)opt_strings_flags(p_bo, p_bo_values, &bo_flags, TRUE);
glepnirf31cfa22025-03-06 21:59:13 +0100151 (void)opt_strings_flags(p_cfc, p_cfc_values, &cfc_flags, TRUE);
zeertzjq529b9ad2024-06-05 20:27:06 +0200152 (void)opt_strings_flags(p_cot, p_cot_values, &cot_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200153#ifdef FEAT_SESSION
154 (void)opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE);
155 (void)opt_strings_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
156#endif
157#ifdef FEAT_FOLDING
158 (void)opt_strings_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
159#endif
160 (void)opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE);
Yegappan Lakshmanan87018252023-09-20 20:20:04 +0200161 (void)opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200162 (void)opt_strings_flags(p_tc, p_tc_values, &tc_flags, FALSE);
163 (void)opt_strings_flags(p_ve, p_ve_values, &ve_flags, TRUE);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +0200164#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +0200165 (void)opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE);
166#endif
167#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
168 (void)opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags, TRUE);
169#endif
170#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
171 (void)opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE);
172#endif
Sean Dewar39c46b42022-05-12 17:44:29 +0100173 (void)opt_strings_flags(p_swb, p_swb_values, &swb_flags, TRUE);
LemonBoy5247b0b2024-07-12 19:30:58 +0200174 (void)opt_strings_flags(p_tcl, p_tcl_values, &tcl_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200175}
176
Yegappan Lakshmananf9dc2782023-05-11 15:02:56 +0100177#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200178/*
179 * Trigger the OptionSet autocommand.
180 * "opt_idx" is the index of the option being set.
181 * "opt_flags" can be OPT_LOCAL etc.
182 * "oldval" the old value
183 * "oldval_l" the old local value (only non-NULL if global and local value
184 * are set)
185 * "oldval_g" the old global value (only non-NULL if global and local value
186 * are set)
187 * "newval" the new value
188 */
189 void
zeertzjq269aa2b2022-11-28 11:36:50 +0000190trigger_optionset_string(
Bram Moolenaardac13472019-09-16 21:06:21 +0200191 int opt_idx,
192 int opt_flags,
193 char_u *oldval,
194 char_u *oldval_l,
195 char_u *oldval_g,
196 char_u *newval)
197{
198 // Don't do this recursively.
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000199 if (oldval == NULL || newval == NULL
200 || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
201 return;
Bram Moolenaardac13472019-09-16 21:06:21 +0200202
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000203 char_u buf_type[7];
204
205 sprintf((char *)buf_type, "%s",
Bram Moolenaardac13472019-09-16 21:06:21 +0200206 (opt_flags & OPT_LOCAL) ? "local" : "global");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000207 set_vim_var_string(VV_OPTION_OLD, oldval, -1);
208 set_vim_var_string(VV_OPTION_NEW, newval, -1);
209 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
210 if (opt_flags & OPT_LOCAL)
211 {
212 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
213 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
Bram Moolenaardac13472019-09-16 21:06:21 +0200214 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000215 if (opt_flags & OPT_GLOBAL)
216 {
217 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
218 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval, -1);
219 }
220 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
221 {
222 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
223 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval_l, -1);
224 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval_g, -1);
225 }
226 if (opt_flags & OPT_MODELINE)
227 {
228 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
229 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
230 }
231 apply_autocmds(EVENT_OPTIONSET,
232 get_option_fullname(opt_idx), NULL, FALSE,
233 NULL);
234 reset_v_option_vars();
Bram Moolenaardac13472019-09-16 21:06:21 +0200235}
236#endif
237
238 static char *
Mike Williams620f0112023-12-05 15:36:06 +0100239illegal_char(char *errbuf, size_t errbuflen, int c)
Bram Moolenaardac13472019-09-16 21:06:21 +0200240{
241 if (errbuf == NULL)
242 return "";
zeertzjq6a8d2e12024-01-17 20:54:49 +0100243 vim_snprintf(errbuf, errbuflen, _(e_illegal_character_str),
Christian Brabandtb39b2402023-11-29 11:34:05 +0100244 (char *)transchar(c));
Bram Moolenaardac13472019-09-16 21:06:21 +0200245 return errbuf;
246}
247
248/*
249 * Check string options in a buffer for NULL value.
250 */
251 void
252check_buf_options(buf_T *buf)
253{
254 check_string_option(&buf->b_p_bh);
255 check_string_option(&buf->b_p_bt);
256 check_string_option(&buf->b_p_fenc);
257 check_string_option(&buf->b_p_ff);
258#ifdef FEAT_FIND_ID
259 check_string_option(&buf->b_p_def);
260 check_string_option(&buf->b_p_inc);
261# ifdef FEAT_EVAL
262 check_string_option(&buf->b_p_inex);
263# endif
264#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +0100265#if defined(FEAT_EVAL)
Bram Moolenaardac13472019-09-16 21:06:21 +0200266 check_string_option(&buf->b_p_inde);
267 check_string_option(&buf->b_p_indk);
268#endif
269#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
270 check_string_option(&buf->b_p_bexpr);
271#endif
272#if defined(FEAT_CRYPT)
273 check_string_option(&buf->b_p_cm);
274#endif
275 check_string_option(&buf->b_p_fp);
276#if defined(FEAT_EVAL)
277 check_string_option(&buf->b_p_fex);
278#endif
279#ifdef FEAT_CRYPT
280 check_string_option(&buf->b_p_key);
281#endif
282 check_string_option(&buf->b_p_kp);
283 check_string_option(&buf->b_p_mps);
284 check_string_option(&buf->b_p_fo);
285 check_string_option(&buf->b_p_flp);
286 check_string_option(&buf->b_p_isk);
Bram Moolenaardac13472019-09-16 21:06:21 +0200287 check_string_option(&buf->b_p_com);
Bram Moolenaardac13472019-09-16 21:06:21 +0200288#ifdef FEAT_FOLDING
289 check_string_option(&buf->b_p_cms);
290#endif
291 check_string_option(&buf->b_p_nf);
Bram Moolenaardac13472019-09-16 21:06:21 +0200292 check_string_option(&buf->b_p_qe);
Bram Moolenaardac13472019-09-16 21:06:21 +0200293#ifdef FEAT_SYN_HL
294 check_string_option(&buf->b_p_syn);
295 check_string_option(&buf->b_s.b_syn_isk);
296#endif
297#ifdef FEAT_SPELL
298 check_string_option(&buf->b_s.b_p_spc);
299 check_string_option(&buf->b_s.b_p_spf);
300 check_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +0200301 check_string_option(&buf->b_s.b_p_spo);
Bram Moolenaardac13472019-09-16 21:06:21 +0200302#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200303 check_string_option(&buf->b_p_sua);
Bram Moolenaardac13472019-09-16 21:06:21 +0200304 check_string_option(&buf->b_p_cink);
305 check_string_option(&buf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +0100306 check_string_option(&buf->b_p_cinsd);
Bram Moolenaardac13472019-09-16 21:06:21 +0200307 parse_cino(buf);
Bram Moolenaar49846fb2022-10-15 16:05:33 +0100308 check_string_option(&buf->b_p_lop);
Bram Moolenaardac13472019-09-16 21:06:21 +0200309 check_string_option(&buf->b_p_ft);
Bram Moolenaardac13472019-09-16 21:06:21 +0200310 check_string_option(&buf->b_p_cinw);
zeertzjq529b9ad2024-06-05 20:27:06 +0200311 check_string_option(&buf->b_p_cot);
Bram Moolenaardac13472019-09-16 21:06:21 +0200312 check_string_option(&buf->b_p_cpt);
313#ifdef FEAT_COMPL_FUNC
314 check_string_option(&buf->b_p_cfu);
315 check_string_option(&buf->b_p_ofu);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +0100316 check_string_option(&buf->b_p_tsrfu);
Bram Moolenaardac13472019-09-16 21:06:21 +0200317#endif
318#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +0100319 check_string_option(&buf->b_p_ffu);
Bram Moolenaardac13472019-09-16 21:06:21 +0200320 check_string_option(&buf->b_p_tfu);
321#endif
322#ifdef FEAT_KEYMAP
323 check_string_option(&buf->b_p_keymap);
324#endif
325#ifdef FEAT_QUICKFIX
326 check_string_option(&buf->b_p_gp);
327 check_string_option(&buf->b_p_mp);
328 check_string_option(&buf->b_p_efm);
329#endif
330 check_string_option(&buf->b_p_ep);
331 check_string_option(&buf->b_p_path);
332 check_string_option(&buf->b_p_tags);
333 check_string_option(&buf->b_p_tc);
334 check_string_option(&buf->b_p_dict);
335 check_string_option(&buf->b_p_tsr);
Bram Moolenaardac13472019-09-16 21:06:21 +0200336 check_string_option(&buf->b_p_lw);
Bram Moolenaardac13472019-09-16 21:06:21 +0200337 check_string_option(&buf->b_p_bkc);
338 check_string_option(&buf->b_p_menc);
339#ifdef FEAT_VARTABS
340 check_string_option(&buf->b_p_vsts);
341 check_string_option(&buf->b_p_vts);
342#endif
343}
344
345/*
346 * Free the string allocated for an option.
347 * Checks for the string being empty_option. This may happen if we're out of
348 * memory, vim_strsave() returned NULL, which was replaced by empty_option by
349 * check_options().
350 * Does NOT check for P_ALLOCED flag!
351 */
352 void
353free_string_option(char_u *p)
354{
355 if (p != empty_option)
356 vim_free(p);
357}
358
359 void
360clear_string_option(char_u **pp)
361{
362 if (*pp != empty_option)
363 vim_free(*pp);
364 *pp = empty_option;
365}
366
367 void
368check_string_option(char_u **pp)
369{
370 if (*pp == NULL)
371 *pp = empty_option;
372}
373
374/*
375 * Set global value for string option when it's a local option.
376 */
377 static void
378set_string_option_global(
379 int opt_idx, // option index
380 char_u **varp) // pointer to option variable
381{
382 char_u **p, *s;
383
384 // the global value is always allocated
385 if (is_window_local_option(opt_idx))
386 p = (char_u **)GLOBAL_WO(varp);
387 else
388 p = (char_u **)get_option_var(opt_idx);
389 if (!is_global_option(opt_idx)
390 && p != varp
391 && (s = vim_strsave(*varp)) != NULL)
392 {
393 free_string_option(*p);
394 *p = s;
395 }
396}
397
398/*
399 * Set a string option to a new value (without checking the effect).
400 * The string is copied into allocated memory.
401 * if ("opt_idx" == -1) "name" is used, otherwise "opt_idx" is used.
402 * When "set_sid" is zero set the scriptID to current_sctx.sc_sid. When
403 * "set_sid" is SID_NONE don't set the scriptID. Otherwise set the scriptID to
404 * "set_sid".
405 */
406 void
407set_string_option_direct(
408 char_u *name,
409 int opt_idx,
410 char_u *val,
411 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
412 int set_sid UNUSED)
413{
414 char_u *s;
415 char_u **varp;
416 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
417 int idx = opt_idx;
418
419 if (idx == -1) // use name
420 {
421 idx = findoption(name);
422 if (idx < 0) // not found (should not happen)
423 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000424 semsg(_(e_internal_error_str), "set_string_option_direct()");
RestorerZ68ebcee2023-05-31 17:12:14 +0100425 siemsg("For option %s", name);
Bram Moolenaardac13472019-09-16 21:06:21 +0200426 return;
427 }
428 }
429
430 if (is_hidden_option(idx)) // can't set hidden option
431 return;
432
433 s = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000434 if (s == NULL)
435 return;
436
437 varp = (char_u **)get_option_varp_scope(idx,
438 both ? OPT_LOCAL : opt_flags);
439 if ((opt_flags & OPT_FREE) && (get_option_flags(idx) & P_ALLOCED))
440 free_string_option(*varp);
441 *varp = s;
442
443 // For buffer/window local option may also set the global value.
444 if (both)
445 set_string_option_global(idx, varp);
446
447 set_option_flag(idx, P_ALLOCED);
448
449 // When setting both values of a global option with a local value,
450 // make the local value empty, so that the global value is used.
451 if (is_global_local_option(idx) && both)
Bram Moolenaardac13472019-09-16 21:06:21 +0200452 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000453 free_string_option(*varp);
454 *varp = empty_option;
Bram Moolenaardac13472019-09-16 21:06:21 +0200455 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000456# ifdef FEAT_EVAL
457 if (set_sid != SID_NONE)
458 {
459 sctx_T script_ctx;
460
461 if (set_sid == 0)
462 script_ctx = current_sctx;
463 else
464 {
465 script_ctx.sc_sid = set_sid;
466 script_ctx.sc_seq = 0;
467 script_ctx.sc_lnum = 0;
468 script_ctx.sc_version = 1;
469 }
470 set_option_sctx_idx(idx, opt_flags, script_ctx);
471 }
472# endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200473}
474
Dominique Pellee764d1b2023-03-12 21:20:59 +0000475#if defined(FEAT_PROP_POPUP) || \
476 (defined(FEAT_DIFF) && defined(FEAT_FOLDING)) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200477/*
478 * Like set_string_option_direct(), but for a window-local option in "wp".
479 * Blocks autocommands to avoid the old curwin becoming invalid.
480 */
481 void
482set_string_option_direct_in_win(
483 win_T *wp,
484 char_u *name,
485 int opt_idx,
486 char_u *val,
487 int opt_flags,
488 int set_sid)
489{
490 win_T *save_curwin = curwin;
491
492 block_autocmds();
493 curwin = wp;
494 curbuf = curwin->w_buffer;
495 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
496 curwin = save_curwin;
497 curbuf = curwin->w_buffer;
498 unblock_autocmds();
499}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000500#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200501
Dominique Pelle748b3082022-01-08 12:41:16 +0000502#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200503/*
504 * Like set_string_option_direct(), but for a buffer-local option in "buf".
505 * Blocks autocommands to avoid the old curbuf becoming invalid.
506 */
507 void
508set_string_option_direct_in_buf(
509 buf_T *buf,
510 char_u *name,
511 int opt_idx,
512 char_u *val,
513 int opt_flags,
514 int set_sid)
515{
516 buf_T *save_curbuf = curbuf;
517
518 block_autocmds();
519 curbuf = buf;
520 curwin->w_buffer = curbuf;
521 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
522 curbuf = save_curbuf;
523 curwin->w_buffer = curbuf;
524 unblock_autocmds();
525}
Dominique Pelle748b3082022-01-08 12:41:16 +0000526#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200527
528/*
529 * Set a string option to a new value, and handle the effects.
530 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100531 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaardac13472019-09-16 21:06:21 +0200532 */
533 char *
534set_string_option(
535 int opt_idx,
536 char_u *value,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +0000537 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Christian Brabandtb39b2402023-11-29 11:34:05 +0100538 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +0100539 size_t errbuflen)
Bram Moolenaardac13472019-09-16 21:06:21 +0200540{
541 char_u *s;
542 char_u **varp;
543 char_u *oldval;
544#if defined(FEAT_EVAL)
545 char_u *oldval_l = NULL;
546 char_u *oldval_g = NULL;
547 char_u *saved_oldval = NULL;
548 char_u *saved_oldval_l = NULL;
549 char_u *saved_oldval_g = NULL;
550 char_u *saved_newval = NULL;
551#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100552 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +0200553 int value_checked = FALSE;
554
555 if (is_hidden_option(opt_idx)) // don't set hidden option
556 return NULL;
557
Bram Moolenaar7f009df2020-03-16 20:27:38 +0100558 s = vim_strsave(value == NULL ? (char_u *)"" : value);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000559 if (s == NULL)
560 return NULL;
561
562 varp = (char_u **)get_option_varp_scope(opt_idx,
563 (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
564 ? (is_global_local_option(opt_idx)
565 ? OPT_GLOBAL : OPT_LOCAL)
566 : opt_flags);
567 oldval = *varp;
568#if defined(FEAT_EVAL)
569 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +0200570 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000571 oldval_l = *(char_u **)get_option_varp_scope(opt_idx, OPT_LOCAL);
572 oldval_g = *(char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
Bram Moolenaardac13472019-09-16 21:06:21 +0200573 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000574#endif
575 *varp = s;
576
577#if defined(FEAT_EVAL)
578 if (!starting
579# ifdef FEAT_CRYPT
580 && !is_crypt_key_option(opt_idx)
581# endif
582 )
583 {
584 if (oldval_l != NULL)
585 saved_oldval_l = vim_strsave(oldval_l);
586 if (oldval_g != NULL)
587 saved_oldval_g = vim_strsave(oldval_g);
588 saved_oldval = vim_strsave(oldval);
589 saved_newval = vim_strsave(s);
590 }
591#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000592 if ((errmsg = did_set_string_option(opt_idx, varp, oldval, value, errbuf,
Christian Brabandtb39b2402023-11-29 11:34:05 +0100593 errbuflen, opt_flags, OP_NONE, &value_checked)) == NULL)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000594 did_set_option(opt_idx, opt_flags, TRUE, value_checked);
595
596#if defined(FEAT_EVAL)
597 // call autocommand after handling side effects
598 if (errmsg == NULL)
599 trigger_optionset_string(opt_idx, opt_flags,
600 saved_oldval, saved_oldval_l,
601 saved_oldval_g, saved_newval);
602 vim_free(saved_oldval);
603 vim_free(saved_oldval_l);
604 vim_free(saved_oldval_g);
605 vim_free(saved_newval);
606#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100607 return errmsg;
Bram Moolenaardac13472019-09-16 21:06:21 +0200608}
609
610/*
611 * Return TRUE if "val" is a valid 'filetype' name.
612 * Also used for 'syntax' and 'keymap'.
613 */
614 static int
615valid_filetype(char_u *val)
616{
617 return valid_name(val, ".-_");
618}
619
620#ifdef FEAT_STL_OPT
621/*
622 * Check validity of options with the 'statusline' format.
zeertzjq5dc294a2022-04-15 13:17:57 +0100623 * Return an untranslated error message or NULL.
Bram Moolenaardac13472019-09-16 21:06:21 +0200624 */
625 static char *
626check_stl_option(char_u *s)
627{
Bram Moolenaardac13472019-09-16 21:06:21 +0200628 int groupdepth = 0;
Christian Brabandtb39b2402023-11-29 11:34:05 +0100629 static char errbuf[ERR_BUFLEN];
630 int errbuflen = ERR_BUFLEN;
Bram Moolenaardac13472019-09-16 21:06:21 +0200631
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100632 while (*s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200633 {
634 // Check for valid keys after % sequences
635 while (*s && *s != '%')
636 s++;
637 if (!*s)
638 break;
639 s++;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +0000640 if (*s == '%' || *s == STL_TRUNCMARK || *s == STL_SEPARATE)
Bram Moolenaardac13472019-09-16 21:06:21 +0200641 {
642 s++;
643 continue;
644 }
645 if (*s == ')')
646 {
647 s++;
648 if (--groupdepth < 0)
649 break;
650 continue;
651 }
652 if (*s == '-')
653 s++;
654 while (VIM_ISDIGIT(*s))
655 s++;
656 if (*s == STL_USER_HL)
657 continue;
658 if (*s == '.')
659 {
660 s++;
661 while (*s && VIM_ISDIGIT(*s))
662 s++;
663 }
664 if (*s == '(')
665 {
666 groupdepth++;
667 continue;
668 }
669 if (vim_strchr(STL_ALL, *s) == NULL)
670 {
Christian Brabandtb39b2402023-11-29 11:34:05 +0100671 return illegal_char(errbuf, errbuflen, *s);
Bram Moolenaardac13472019-09-16 21:06:21 +0200672 }
673 if (*s == '{')
674 {
zeertzjq5dc294a2022-04-15 13:17:57 +0100675 int reevaluate = (*++s == '%');
shadmansaleh30e3de22021-05-15 17:23:28 +0200676
zeertzjq5dc294a2022-04-15 13:17:57 +0100677 if (reevaluate && *++s == '}')
678 // "}" is not allowed immediately after "%{%"
Christian Brabandtb39b2402023-11-29 11:34:05 +0100679 return illegal_char(errbuf, errbuflen, '}');
shadmansaleh30e3de22021-05-15 17:23:28 +0200680 while ((*s != '}' || (reevaluate && s[-1] != '%')) && *s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200681 s++;
682 if (*s != '}')
zeertzjq5dc294a2022-04-15 13:17:57 +0100683 return e_unclosed_expression_sequence;
Bram Moolenaardac13472019-09-16 21:06:21 +0200684 }
685 }
Bram Moolenaardac13472019-09-16 21:06:21 +0200686 if (groupdepth != 0)
zeertzjq5dc294a2022-04-15 13:17:57 +0100687 return e_unbalanced_groups;
Bram Moolenaardac13472019-09-16 21:06:21 +0200688 return NULL;
689}
690#endif
691
692/*
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +0000693 * Check for a "normal" directory or file name in some options. Disallow a
694 * path separator (slash and/or backslash), wildcards and characters that are
695 * often illegal in a file name. Be more permissive if "secure" is off.
696 */
697 static int
698check_illegal_path_names(int opt_idx, char_u **varp)
699{
700 return (((get_option_flags(opt_idx) & P_NFNAME)
701 && vim_strpbrk(*varp, (char_u *)(secure
702 ? "/\\*?[|;&<>\r\n" : "/\\*?[<>\r\n")) != NULL)
703 || ((get_option_flags(opt_idx) & P_NDNAME)
704 && vim_strpbrk(*varp, (char_u *)"*?[|;&<>\r\n") != NULL));
705}
706
707/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000708 * An option that accepts a list of flags is changed.
709 * e.g. 'viewoptions', 'switchbuf', 'casemap', etc.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000710 */
711 static char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000712did_set_opt_flags(char_u *val, char **values, unsigned *flagp, int list)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000713{
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000714 if (opt_strings_flags(val, values, flagp, list) == FAIL)
715 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000716
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000717 return NULL;
718}
719
720/*
721 * An option that accepts a list of string values is changed.
722 * e.g. 'nrformats', 'scrollopt', 'wildoptions', etc.
723 */
724 static char *
725did_set_opt_strings(char_u *val, char **values, int list)
726{
727 return did_set_opt_flags(val, values, NULL, list);
728}
729
730/*
731 * An option which is a list of flags is set. Valid values are in 'flags'.
732 */
733 static char *
Christian Brabandtb39b2402023-11-29 11:34:05 +0100734did_set_option_listflag(
735 char_u *val,
736 char_u *flags,
737 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +0100738 size_t errbuflen)
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000739{
740 char_u *s;
741
Yegappan Lakshmananc727b192023-03-03 12:26:15 +0000742 for (s = val; *s; ++s)
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000743 if (vim_strchr(flags, *s) == NULL)
Christian Brabandtb39b2402023-11-29 11:34:05 +0100744 return illegal_char(errbuf, errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000745
746 return NULL;
747}
748
749/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200750 * Expand an option that accepts a list of fixed string values with known
751 * number of items.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200752 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200753 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200754expand_set_opt_string(
755 optexpand_T *args,
756 char **values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +0200757 size_t numValues,
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200758 int *numMatches,
759 char_u ***matches)
760{
761 char_u *p;
762 regmatch_T *regmatch = args->oe_regmatch;
763 int include_orig_val = args->oe_include_orig_val;
764 char_u *option_val = args->oe_opt_value;
765
766 // Assume numValues is small since they are fixed enums, so just allocate
767 // upfront instead of needing two passes to calculate output size.
768 *matches = ALLOC_MULT(char_u *, numValues + 1);
769 if (*matches == NULL)
770 return FAIL;
771
772 int count = 0;
773
774 if (include_orig_val && *option_val != NUL)
775 {
776 p = vim_strsave(option_val);
777 if (p == NULL)
778 {
779 VIM_CLEAR(*matches);
780 return FAIL;
781 }
782 (*matches)[count++] = p;
783 }
784
785 for (char **val = values; *val != NULL; val++)
786 {
787 if (include_orig_val && *option_val != NUL)
788 {
789 if (STRCMP((char_u*)*val, option_val) == 0)
790 continue;
791 }
792 if (vim_regexec(regmatch, (char_u*)(*val), (colnr_T)0))
793 {
794 p = vim_strsave((char_u*)*val);
795 if (p == NULL)
796 {
797 if (count == 0)
798 {
799 VIM_CLEAR(*matches);
800 return FAIL;
801 }
802 else
803 break;
804 }
805 (*matches)[count++] = p;
806 }
807 }
808 if (count == 0)
809 {
810 VIM_CLEAR(*matches);
811 return FAIL;
812 }
813 *numMatches = count;
814 return OK;
815}
816
817static char_u *set_opt_callback_orig_option = NULL;
818static char_u *((*set_opt_callback_func)(expand_T *, int));
819
820/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200821 * Callback used by expand_set_opt_generic to also include the original value
822 * as the first item.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200823 */
824 static char_u *
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200825expand_set_opt_generic_cb(expand_T *xp, int idx)
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200826{
827 if (idx == 0)
828 {
829 if (set_opt_callback_orig_option != NULL)
830 return set_opt_callback_orig_option;
831 else
832 return (char_u *)""; // empty strings are ignored
833 }
834 return set_opt_callback_func(xp, idx - 1);
835}
836
837/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200838 * Expand an option with a callback that iterates through a list of possible
839 * names using an index.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200840 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200841 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200842expand_set_opt_generic(
843 optexpand_T *args,
844 char_u *((*func)(expand_T *, int)),
845 int *numMatches,
846 char_u ***matches)
847{
848 int ret;
849
850 set_opt_callback_orig_option = args->oe_include_orig_val ?
851 args->oe_opt_value : NULL;
852 set_opt_callback_func = func;
853
854 ret = ExpandGeneric(
855 (char_u*)"", // not using fuzzy as currently EXPAND_STRING_SETTING doesn't use it
856 args->oe_xp,
857 args->oe_regmatch,
858 matches,
859 numMatches,
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200860 expand_set_opt_generic_cb,
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200861 FALSE);
862
863 set_opt_callback_orig_option = NULL;
864 set_opt_callback_func = NULL;
865 return ret;
866}
867
Christian Brabandt9960ebc2023-10-05 22:17:09 +0200868# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200869static garray_T *expand_cb_ga;
870static optexpand_T *expand_cb_args;
871
872/*
873 * Callback provided to a function in expand_set_opt_callback. Will perform
874 * regex matching against the value and add to the list.
875 *
876 * Returns OK usually. Returns FAIL if it failed to allocate memory, and the
877 * caller should terminate the enumeration.
878 */
879 static int
880expand_set_opt_callback_cb(char_u *val)
881{
882 regmatch_T *regmatch = expand_cb_args->oe_regmatch;
883 expand_T *xp = expand_cb_args->oe_xp;
884 garray_T *ga = expand_cb_ga;
885 char_u *str;
886
887 if (val == NULL || *val == NUL)
888 return OK;
889
890 if (xp->xp_pattern[0] != NUL &&
891 !vim_regexec(regmatch, val, (colnr_T)0))
892 return OK;
893
894 str = vim_strsave_escaped(val, (char_u *)" \t\\");
895
896 if (str == NULL)
897 return FAIL;
898
899 if (ga_grow(ga, 1) == FAIL)
900 {
901 vim_free(str);
902 return FAIL;
903 }
904
905 ((char_u **)ga->ga_data)[ga->ga_len] = str;
906 ++ga->ga_len;
907 return OK;
908}
909
910/*
911 * Expand an option with a provided function that takes a callback. The
912 * function will enumerate through all options and call the callback to add it
913 * to the list.
914 *
915 * "func" is the enumerator function that will generate the list of options.
916 * "func_params" is a single parameter that will be passed to func.
917 */
918 static int
919expand_set_opt_callback(
920 optexpand_T *args,
921 void (*func)(optexpand_T *, void* params, int (*cb)(char_u *val)),
922 void *func_params,
923 int *numMatches,
924 char_u ***matches)
925{
926 garray_T ga;
927 int include_orig_val = args->oe_include_orig_val;
928 char_u *option_val = args->oe_opt_value;
929
930 ga_init2(&ga, sizeof(char *), 30);
931
932 if (include_orig_val && *option_val != NUL)
933 {
934 char_u *p = vim_strsave(option_val);
935 if (p == NULL)
936 return FAIL;
937 if (ga_grow(&ga, 1) == FAIL)
938 {
939 vim_free(p);
940 return FAIL;
941 }
942 ((char_u **)ga.ga_data)[ga.ga_len] = p;
943 ++ga.ga_len;
944 }
945
946 expand_cb_ga = &ga;
947 expand_cb_args = args;
948
949 func(args, func_params, expand_set_opt_callback_cb);
950
951 expand_cb_ga = NULL;
952 expand_cb_args = NULL;
953
954 *matches = ga.ga_data;
955 *numMatches = ga.ga_len;
956 return OK;
957}
Christian Brabandt9960ebc2023-10-05 22:17:09 +0200958#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200959
960/*
961 * Expand an option which is a list of flags.
962 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200963 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200964expand_set_opt_listflag(
965 optexpand_T *args,
966 char_u *flags,
967 int *numMatches,
968 char_u ***matches)
969{
970 char_u *p;
971 char_u *option_val = args->oe_opt_value;
972 char_u *cmdline_val = args->oe_set_arg;
973 int append = args->oe_append;
974 int include_orig_val = args->oe_include_orig_val && (*option_val != NUL);
975
Yee Cheng Chin6d113472023-10-02 21:38:39 +0200976 size_t num_flags = STRLEN(flags);
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200977
978 // Assume we only have small number of flags, so just allocate max size.
979 *matches = ALLOC_MULT(char_u *, num_flags + 1);
980 if (*matches == NULL)
981 return FAIL;
982
983 int count = 0;
984
985 if (include_orig_val)
986 {
987 p = vim_strsave(option_val);
988 if (p == NULL)
989 {
990 VIM_CLEAR(*matches);
991 return FAIL;
992 }
993 (*matches)[count++] = p;
994 }
995
996 for (char_u *flag = flags; *flag != NUL; flag++)
997 {
998 if (append && vim_strchr(option_val, *flag) != NULL)
999 continue;
1000
1001 if (vim_strchr(cmdline_val, *flag) == NULL)
1002 {
1003 if (include_orig_val
1004 && option_val[1] == NUL
1005 && *flag == option_val[0])
1006 {
1007 // This value is already used as the first choice as it's the
1008 // existing flag. Just skip it to avoid duplicate.
1009 continue;
1010 }
1011 p = vim_strnsave(flag, 1);
1012 if (p == NULL)
1013 {
1014 if (count == 0)
1015 {
1016 VIM_CLEAR(*matches);
1017 return FAIL;
1018 }
1019 else
1020 break;
1021 }
1022 (*matches)[count++] = p;
1023 }
1024 }
1025
1026 if (count == 0)
1027 {
1028 VIM_CLEAR(*matches);
1029 return FAIL;
1030 }
1031 *numMatches = count;
1032 return OK;
1033}
1034
1035/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001036 * The 'ambiwidth' option is changed.
1037 */
1038 char *
1039did_set_ambiwidth(optset_T *args UNUSED)
1040{
1041 if (check_opt_strings(p_ambw, p_ambw_values, FALSE) != OK)
1042 return e_invalid_argument;
1043
1044 return check_chars_options();
1045}
1046
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001047 int
1048expand_set_ambiwidth(optexpand_T *args, int *numMatches, char_u ***matches)
1049{
1050 return expand_set_opt_string(
1051 args,
1052 p_ambw_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001053 ARRAY_LENGTH(p_ambw_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001054 numMatches,
1055 matches);
1056}
1057
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001058/*
1059 * The 'background' option is changed.
1060 */
1061 char *
Gregory Anders83ad2722024-01-03 19:48:51 +01001062did_set_background(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001063{
1064 if (check_opt_strings(p_bg, p_bg_values, FALSE) == FAIL)
1065 return e_invalid_argument;
1066
Gregory Anders83ad2722024-01-03 19:48:51 +01001067 if (args->os_oldval.string != NULL && args->os_oldval.string[0] == *p_bg)
1068 // Value was not changed
1069 return NULL;
1070
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001071#ifdef FEAT_EVAL
1072 int dark = (*p_bg == 'd');
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001073#endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001074
1075 init_highlight(FALSE, FALSE);
1076
1077#ifdef FEAT_EVAL
1078 if (dark != (*p_bg == 'd')
1079 && get_var_value((char_u *)"g:colors_name") != NULL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001080 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001081 // The color scheme must have set 'background' back to another
1082 // value, that's not what we want here. Disable the color
1083 // scheme and set the colors again.
1084 do_unlet((char_u *)"g:colors_name", TRUE);
1085 free_string_option(p_bg);
1086 p_bg = vim_strsave((char_u *)(dark ? "dark" : "light"));
1087 check_string_option(&p_bg);
1088 init_highlight(FALSE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001089 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001090#endif
1091#ifdef FEAT_TERMINAL
1092 term_update_colors_all();
1093#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001094
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001095 return NULL;
1096}
1097
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001098 int
1099expand_set_background(optexpand_T *args, int *numMatches, char_u ***matches)
1100{
1101 return expand_set_opt_string(
1102 args,
1103 p_bg_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001104 ARRAY_LENGTH(p_bg_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001105 numMatches,
1106 matches);
1107}
1108
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001109/*
1110 * The 'backspace' option is changed.
1111 */
1112 char *
1113did_set_backspace(optset_T *args UNUSED)
1114{
1115 if (VIM_ISDIGIT(*p_bs))
1116 {
1117 if (*p_bs > '3' || p_bs[1] != NUL)
1118 return e_invalid_argument;
1119 }
1120 else if (check_opt_strings(p_bs, p_bs_values, TRUE) != OK)
1121 return e_invalid_argument;
1122
1123 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001124}
1125
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001126 int
1127expand_set_backspace(optexpand_T *args, int *numMatches, char_u ***matches)
1128{
1129 return expand_set_opt_string(
1130 args,
1131 p_bs_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001132 ARRAY_LENGTH(p_bs_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001133 numMatches,
1134 matches);
1135}
1136
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001137/*
1138 * The 'backupcopy' option is changed.
1139 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001140 char *
1141did_set_backupcopy(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001142{
1143 char_u *bkc = p_bkc;
1144 unsigned int *flags = &bkc_flags;
1145 char *errmsg = NULL;
1146
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001147 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001148 {
1149 bkc = curbuf->b_p_bkc;
1150 flags = &curbuf->b_bkc_flags;
1151 }
zeertzjq46dcd842024-11-03 09:10:50 +01001152 else if (!(args->os_flags & OPT_GLOBAL))
1153 // When using :set, clear the local flags.
1154 curbuf->b_bkc_flags = 0;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001155
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001156 if ((args->os_flags & OPT_LOCAL) && *bkc == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001157 // make the local value empty: use the global value
1158 *flags = 0;
1159 else
1160 {
1161 if (opt_strings_flags(bkc, p_bkc_values, flags, TRUE) != OK)
1162 errmsg = e_invalid_argument;
1163 if ((((int)*flags & BKC_AUTO) != 0)
1164 + (((int)*flags & BKC_YES) != 0)
1165 + (((int)*flags & BKC_NO) != 0) != 1)
1166 {
1167 // Must have exactly one of "auto", "yes" and "no".
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001168 (void)opt_strings_flags(args->os_oldval.string, p_bkc_values,
1169 flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001170 errmsg = e_invalid_argument;
1171 }
1172 }
1173
1174 return errmsg;
1175}
1176
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001177 int
1178expand_set_backupcopy(optexpand_T *args, int *numMatches, char_u ***matches)
1179{
1180 return expand_set_opt_string(
1181 args,
1182 p_bkc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001183 ARRAY_LENGTH(p_bkc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001184 numMatches,
1185 matches);
1186}
1187
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001188/*
1189 * The 'backupext' or the 'patchmode' option is changed.
1190 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001191 char *
1192did_set_backupext_or_patchmode(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001193{
1194 if (STRCMP(*p_bex == '.' ? p_bex + 1 : p_bex,
1195 *p_pm == '.' ? p_pm + 1 : p_pm) == 0)
1196 return e_backupext_and_patchmode_are_equal;
1197
1198 return NULL;
1199}
1200
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001201/*
1202 * The 'belloff' option is changed.
1203 */
1204 char *
1205did_set_belloff(optset_T *args UNUSED)
1206{
1207 return did_set_opt_flags(p_bo, p_bo_values, &bo_flags, TRUE);
1208}
1209
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001210 int
1211expand_set_belloff(optexpand_T *args, int *numMatches, char_u ***matches)
1212{
1213 return expand_set_opt_string(
1214 args,
1215 p_bo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001216 ARRAY_LENGTH(p_bo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001217 numMatches,
1218 matches);
1219}
1220
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001221#if defined(FEAT_LINEBREAK) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001222/*
zeertzjqeac3fdc2024-02-03 18:08:09 +01001223 * The 'breakat' option is changed.
1224 */
1225 char *
1226did_set_breakat(optset_T *args UNUSED)
1227{
1228 char_u *p;
1229 int i;
1230
1231 for (i = 0; i < 256; i++)
1232 breakat_flags[i] = FALSE;
1233
1234 if (p_breakat != NULL)
1235 for (p = p_breakat; *p; p++)
1236 breakat_flags[*p] = TRUE;
1237
1238 return NULL;
1239}
1240
1241/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001242 * The 'breakindentopt' option is changed.
1243 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001244 char *
Millyb38700a2024-10-22 22:59:39 +02001245did_set_breakindentopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001246{
Millyb38700a2024-10-22 22:59:39 +02001247 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001248
Millyb38700a2024-10-22 22:59:39 +02001249 if (briopt_check(*varp, varp == &curwin->w_p_briopt ? curwin : NULL)
1250 == FAIL)
1251 return e_invalid_argument;
1252
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001253 // list setting requires a redraw
Millyb38700a2024-10-22 22:59:39 +02001254 if (varp == &curwin->w_p_briopt && curwin->w_briopt_list)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001255 redraw_all_later(UPD_NOT_VALID);
1256
Millyb38700a2024-10-22 22:59:39 +02001257 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001258}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001259
1260 int
1261expand_set_breakindentopt(optexpand_T *args, int *numMatches, char_u ***matches)
1262{
1263 return expand_set_opt_string(
1264 args,
1265 p_briopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001266 ARRAY_LENGTH(p_briopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001267 numMatches,
1268 matches);
1269}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001270#endif
1271
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001272#if defined(FEAT_BROWSE) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001273/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001274 * The 'browsedir' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001275 */
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00001276 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001277did_set_browsedir(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001278{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001279 if (check_opt_strings(p_bsdir, p_bsdir_values, FALSE) != OK
1280 && !mch_isdir(p_bsdir))
1281 return e_invalid_argument;
1282
1283 return NULL;
1284}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001285
1286 int
1287expand_set_browsedir(optexpand_T *args, int *numMatches, char_u ***matches)
1288{
1289 return expand_set_opt_string(
1290 args,
1291 p_bsdir_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001292 ARRAY_LENGTH(p_bsdir_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001293 numMatches,
1294 matches);
1295}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001296#endif
1297
1298/*
1299 * The 'bufhidden' option is changed.
1300 */
1301 char *
1302did_set_bufhidden(optset_T *args UNUSED)
1303{
1304 return did_set_opt_strings(curbuf->b_p_bh, p_bufhidden_values, FALSE);
1305}
1306
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001307 int
1308expand_set_bufhidden(optexpand_T *args, int *numMatches, char_u ***matches)
1309{
1310 return expand_set_opt_string(
1311 args,
1312 p_bufhidden_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001313 ARRAY_LENGTH(p_bufhidden_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001314 numMatches,
1315 matches);
1316}
1317
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001318/*
1319 * The 'buftype' option is changed.
1320 */
1321 char *
1322did_set_buftype(optset_T *args UNUSED)
1323{
1324 if (check_opt_strings(curbuf->b_p_bt, p_buftype_values, FALSE) != OK)
1325 return e_invalid_argument;
1326
1327 if (curwin->w_status_height)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001328 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001329 curwin->w_redr_status = TRUE;
1330 redraw_later(UPD_VALID);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001331 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001332 curbuf->b_help = (curbuf->b_p_bt[0] == 'h');
1333 redraw_titles();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001334
1335 return NULL;
1336}
1337
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001338 int
1339expand_set_buftype(optexpand_T *args, int *numMatches, char_u ***matches)
1340{
1341 return expand_set_opt_string(
1342 args,
1343 p_buftype_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001344 ARRAY_LENGTH(p_buftype_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001345 numMatches,
1346 matches);
1347}
1348
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001349/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001350 * The 'casemap' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001351 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001352 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001353did_set_casemap(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001354{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001355 return did_set_opt_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
1356}
1357
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001358 int
1359expand_set_casemap(optexpand_T *args, int *numMatches, char_u ***matches)
1360{
1361 return expand_set_opt_string(
1362 args,
1363 p_cmp_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001364 ARRAY_LENGTH(p_cmp_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001365 numMatches,
1366 matches);
1367}
1368
1369#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1370 int
1371expand_set_clipboard(optexpand_T *args, int *numMatches, char_u ***matches)
1372{
1373 return expand_set_opt_string(
1374 args,
1375 p_cb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001376 ARRAY_LENGTH(p_cb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001377 numMatches,
1378 matches);
1379}
1380#endif
1381
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001382/*
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001383 * The global 'listchars' or 'fillchars' option is changed.
1384 */
1385 static char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01001386did_set_global_listfillchars(char_u *val, int opt_lcs, int opt_flags,
1387 char *errbuf, size_t errbuflen)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001388{
1389 char *errmsg = NULL;
1390 char_u **local_ptr = opt_lcs ? &curwin->w_p_lcs : &curwin->w_p_fcs;
1391
1392 // only apply the global value to "curwin" when it does not have a
1393 // local value
1394 if (opt_lcs)
1395 errmsg = set_listchars_option(curwin, val,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001396 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL),
1397 errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001398 else
1399 errmsg = set_fillchars_option(curwin, val,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001400 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL),
1401 errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001402 if (errmsg != NULL)
1403 return errmsg;
1404
1405 tabpage_T *tp;
1406 win_T *wp;
1407
1408 // If the current window is set to use the global
1409 // 'listchars'/'fillchars' value, clear the window-local value.
1410 if (!(opt_flags & OPT_GLOBAL))
1411 clear_string_option(local_ptr);
1412 FOR_ALL_TAB_WINDOWS(tp, wp)
1413 {
1414 // If the current window has a local value need to apply it
1415 // again, it was changed when setting the global value.
1416 // If no error was returned above, we don't expect an error
1417 // here, so ignore the return value.
1418 if (opt_lcs)
1419 {
1420 if (*wp->w_p_lcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001421 (void)set_listchars_option(wp, wp->w_p_lcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001422 }
1423 else
1424 {
1425 if (*wp->w_p_fcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001426 (void)set_fillchars_option(wp, wp->w_p_fcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001427 }
1428 }
1429
1430 redraw_all_later(UPD_NOT_VALID);
1431
1432 return NULL;
1433}
1434
1435/*
1436 * The 'fillchars' option or the 'listchars' option is changed.
1437 */
1438 char *
1439did_set_chars_option(optset_T *args)
1440{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001441 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001442 char *errmsg = NULL;
1443
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001444 if ( varp == &p_lcs // global 'listchars'
1445 || varp == &p_fcs) // global 'fillchars'
1446 errmsg = did_set_global_listfillchars(*varp, varp == &p_lcs,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001447 args->os_flags, args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001448 else if (varp == &curwin->w_p_lcs) // local 'listchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001449 errmsg = set_listchars_option(curwin, *varp, TRUE,
1450 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001451 else if (varp == &curwin->w_p_fcs) // local 'fillchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001452 errmsg = set_fillchars_option(curwin, *varp, TRUE,
1453 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001454
1455 return errmsg;
1456}
1457
1458/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001459 * Expand 'fillchars' or 'listchars' option value.
1460 */
1461 int
1462expand_set_chars_option(optexpand_T *args, int *numMatches, char_u ***matches)
1463{
1464 char_u **varp = (char_u **)args->oe_varp;
1465 int is_lcs = (varp == &p_lcs || varp == &curwin->w_p_lcs);
1466 return expand_set_opt_generic(
1467 args,
1468 is_lcs ? get_listchars_name : get_fillchars_name,
1469 numMatches,
1470 matches);
1471}
1472
1473/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001474 * The 'cinoptions' option is changed.
1475 */
1476 char *
1477did_set_cinoptions(optset_T *args UNUSED)
1478{
1479 // TODO: recognize errors
1480 parse_cino(curbuf);
1481
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001482 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001483}
1484
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00001485#if defined(FEAT_SYN_HL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001486/*
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001487 * The 'colorcolumn' option is changed.
1488 */
1489 char *
Millya441a3e2024-10-22 22:43:01 +02001490did_set_colorcolumn(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001491{
Millya441a3e2024-10-22 22:43:01 +02001492 char_u **varp = (char_u **)args->os_varp;
1493
1494 return check_colorcolumn(*varp, varp == &curwin->w_p_cc ? curwin : NULL);
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001495}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001496#endif
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001497
1498/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001499 * The 'comments' option is changed.
1500 */
1501 char *
1502did_set_comments(optset_T *args)
1503{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001504 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001505 char_u *s;
1506 char *errmsg = NULL;
1507
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001508 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001509 {
1510 while (*s && *s != ':')
1511 {
1512 if (vim_strchr((char_u *)COM_ALL, *s) == NULL
1513 && !VIM_ISDIGIT(*s) && *s != '-')
1514 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01001515 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001516 break;
1517 }
1518 ++s;
1519 }
1520 if (*s++ == NUL)
1521 errmsg = e_missing_colon;
1522 else if (*s == ',' || *s == NUL)
1523 errmsg = e_zero_length_string;
1524 if (errmsg != NULL)
1525 break;
1526 while (*s && *s != ',')
1527 {
1528 if (*s == '\\' && s[1] != NUL)
1529 ++s;
1530 ++s;
1531 }
1532 s = skip_to_option_part(s);
1533 }
1534
1535 return errmsg;
1536}
1537
1538#if defined(FEAT_FOLDING) || defined(PROTO)
1539/*
1540 * The 'commentstring' option is changed.
1541 */
1542 char *
1543did_set_commentstring(optset_T *args)
1544{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001545 char_u **varp = (char_u **)args->os_varp;
1546
1547 if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001548 return e_commentstring_must_be_empty_or_contain_str;
1549
1550 return NULL;
1551}
1552#endif
1553
1554/*
1555 * The 'complete' option is changed.
1556 */
1557 char *
1558did_set_complete(optset_T *args)
1559{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001560 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001561 char_u *s;
1562
1563 // check if it is a valid value for 'complete' -- Acevedo
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001564 for (s = *varp; *s;)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001565 {
1566 while (*s == ',' || *s == ' ')
1567 s++;
1568 if (!*s)
1569 break;
1570 if (vim_strchr((char_u *)".wbuksid]tU", *s) == NULL)
Christian Brabandtb39b2402023-11-29 11:34:05 +01001571 return illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001572 if (*++s != NUL && *s != ',' && *s != ' ')
1573 {
1574 if (s[-1] == 'k' || s[-1] == 's')
1575 {
1576 // skip optional filename after 'k' and 's'
1577 while (*s && *s != ',' && *s != ' ')
1578 {
1579 if (*s == '\\' && s[1] != NUL)
1580 ++s;
1581 ++s;
1582 }
1583 }
1584 else
1585 {
1586 if (args->os_errbuf != NULL)
1587 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01001588 vim_snprintf((char *)args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001589 _(e_illegal_character_after_chr), *--s);
1590 return args->os_errbuf;
1591 }
1592 return "";
1593 }
1594 }
1595 }
1596
1597 return NULL;
1598}
1599
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001600 int
1601expand_set_complete(optexpand_T *args, int *numMatches, char_u ***matches)
1602{
1603 static char *(p_cpt_values[]) = {
1604 ".", "w", "b", "u", "k", "kspell", "s", "i", "d", "]", "t", "U",
1605 NULL};
1606 return expand_set_opt_string(
1607 args,
1608 p_cpt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001609 ARRAY_LENGTH(p_cpt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001610 numMatches,
1611 matches);
1612}
1613
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001614/*
1615 * The 'completeopt' option is changed.
1616 */
1617 char *
1618did_set_completeopt(optset_T *args UNUSED)
1619{
zeertzjq529b9ad2024-06-05 20:27:06 +02001620 char_u *cot = p_cot;
1621 unsigned *flags = &cot_flags;
1622
1623 if (args->os_flags & OPT_LOCAL)
1624 {
1625 cot = curbuf->b_p_cot;
1626 flags = &curbuf->b_cot_flags;
1627 }
zeertzjq46dcd842024-11-03 09:10:50 +01001628 else if (!(args->os_flags & OPT_GLOBAL))
1629 // When using :set, clear the local flags.
1630 curbuf->b_cot_flags = 0;
zeertzjq529b9ad2024-06-05 20:27:06 +02001631
1632 if (check_opt_strings(cot, p_cot_values, TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001633 return e_invalid_argument;
1634
zeertzjq529b9ad2024-06-05 20:27:06 +02001635 if (opt_strings_flags(cot, p_cot_values, flags, TRUE) != OK)
1636 return e_invalid_argument;
1637
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001638 return NULL;
1639}
1640
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001641 int
1642expand_set_completeopt(optexpand_T *args, int *numMatches, char_u ***matches)
1643{
1644 return expand_set_opt_string(
1645 args,
1646 p_cot_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001647 ARRAY_LENGTH(p_cot_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001648 numMatches,
1649 matches);
1650}
1651
glepnir6a89c942024-10-01 20:32:12 +02001652/*
glepnirf31cfa22025-03-06 21:59:13 +01001653 * The 'completefuzzycollect' option is changed.
1654 */
1655 char *
1656did_set_completefuzzycollect(optset_T *args UNUSED)
1657{
1658 if (opt_strings_flags(p_cfc, p_cfc_values, &cfc_flags, TRUE) != OK)
1659 return e_invalid_argument;
1660 return NULL;
1661}
1662
zeertzjq53d59ec2025-03-07 19:09:09 +01001663 int
1664expand_set_completefuzzycollect(
1665 optexpand_T *args,
1666 int *numMatches,
1667 char_u ***matches)
1668{
1669 return expand_set_opt_string(
1670 args,
1671 p_cfc_values,
1672 ARRAY_LENGTH(p_cfc_values) - 1,
1673 numMatches,
1674 matches);
1675}
1676
glepnirf31cfa22025-03-06 21:59:13 +01001677/*
glepnir6a89c942024-10-01 20:32:12 +02001678 * The 'completeitemalign' option is changed.
1679 */
1680 char *
1681did_set_completeitemalign(optset_T *args UNUSED)
1682{
1683 char_u *p = p_cia;
1684 unsigned new_cia_flags = 0;
1685 int seen[3] = { FALSE, FALSE, FALSE };
1686 int count = 0;
1687 char_u buf[10];
1688
1689 while (*p)
1690 {
1691 copy_option_part(&p, buf, sizeof(buf), ",");
1692 if (count >= 3)
1693 return e_invalid_argument;
1694
1695 if (STRCMP(buf, "abbr") == 0)
1696 {
1697 if (seen[CPT_ABBR])
1698 return e_invalid_argument;
1699 new_cia_flags = new_cia_flags * 10 + CPT_ABBR;
1700 seen[CPT_ABBR] = TRUE;
1701 count++;
1702 }
1703 else if (STRCMP(buf, "kind") == 0)
1704 {
1705 if (seen[CPT_KIND])
1706 return e_invalid_argument;
1707 new_cia_flags = new_cia_flags * 10 + CPT_KIND;
1708 seen[CPT_KIND] = TRUE;
1709 count++;
1710 }
1711 else if (STRCMP(buf, "menu") == 0)
1712 {
1713 if (seen[CPT_MENU])
1714 return e_invalid_argument;
1715 new_cia_flags = new_cia_flags * 10 + CPT_MENU;
1716 seen[CPT_MENU] = TRUE;
1717 count++;
1718 }
1719 else
1720 return e_invalid_argument;
1721 }
1722 if (new_cia_flags == 0 || count != 3)
1723 return e_invalid_argument;
1724
1725 cia_flags = new_cia_flags;
1726 return NULL;
1727}
1728
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001729#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1730/*
1731 * The 'completepopup' option is changed.
1732 */
1733 char *
1734did_set_completepopup(optset_T *args UNUSED)
1735{
1736 if (parse_completepopup(NULL) == FAIL)
1737 return e_invalid_argument;
1738
1739 popup_close_info();
1740 return NULL;
1741}
1742#endif
1743
1744#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1745/*
1746 * The 'completeslash' option is changed.
1747 */
1748 char *
1749did_set_completeslash(optset_T *args UNUSED)
1750{
1751 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1752 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
1753 return e_invalid_argument;
1754
1755 return NULL;
1756}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001757
1758 int
1759expand_set_completeslash(optexpand_T *args, int *numMatches, char_u ***matches)
1760{
1761 return expand_set_opt_string(
1762 args,
1763 p_csl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001764 ARRAY_LENGTH(p_csl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001765 numMatches,
1766 matches);
1767}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001768#endif
1769
1770#if defined(FEAT_CONCEAL) || defined(PROTO)
1771/*
1772 * The 'concealcursor' option is changed.
1773 */
1774 char *
1775did_set_concealcursor(optset_T *args)
1776{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001777 char_u **varp = (char_u **)args->os_varp;
1778
Christian Brabandtb39b2402023-11-29 11:34:05 +01001779 return did_set_option_listflag(*varp, (char_u *)COCU_ALL, args->os_errbuf,
1780 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001781}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001782
1783 int
1784expand_set_concealcursor(optexpand_T *args, int *numMatches, char_u ***matches)
1785{
1786 return expand_set_opt_listflag(args, (char_u*)COCU_ALL, numMatches, matches);
1787}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001788#endif
1789
1790/*
1791 * The 'cpoptions' option is changed.
1792 */
1793 char *
1794did_set_cpoptions(optset_T *args)
1795{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001796 char_u **varp = (char_u **)args->os_varp;
1797
Christian Brabandtb39b2402023-11-29 11:34:05 +01001798 return did_set_option_listflag(*varp, (char_u *)CPO_ALL, args->os_errbuf,
1799 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001800}
1801
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001802 int
1803expand_set_cpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
1804{
1805 return expand_set_opt_listflag(args, (char_u*)CPO_ALL, numMatches, matches);
1806}
1807
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001808#if defined(FEAT_CRYPT) || defined(PROTO)
1809/*
1810 * The 'cryptkey' option is changed.
1811 */
1812 char *
1813did_set_cryptkey(optset_T *args)
1814{
1815 // Make sure the ":set" command doesn't show the new value in the
1816 // history.
1817 remove_key_from_history();
1818
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02001819 if (args->os_op != OP_NONE)
1820 // Don't allow set+=/-=/^= as they can allow for substring guessing
1821 return e_invalid_argument;
1822
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001823 if (STRCMP(curbuf->b_p_key, args->os_oldval.string) != 0)
1824 {
1825 // Need to update the swapfile.
1826 ml_set_crypt_key(curbuf, args->os_oldval.string,
1827 *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
1828 changed_internal();
1829 }
Christian Brabandt19e6c4f2023-06-27 18:57:10 +01001830# ifdef FEAT_SODIUM
1831 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
1832 crypt_sodium_lock_key(args->os_newval.string);
1833# endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001834
1835 return NULL;
1836}
1837
1838/*
1839 * The 'cryptmethod' option is changed.
1840 */
1841 char *
1842did_set_cryptmethod(optset_T *args)
1843{
1844 char_u *p;
1845 char_u *s;
1846
1847 if (args->os_flags & OPT_LOCAL)
1848 p = curbuf->b_p_cm;
1849 else
1850 p = p_cm;
1851 if (check_opt_strings(p, p_cm_values, TRUE) != OK)
1852 return e_invalid_argument;
1853 else if (crypt_self_test() == FAIL)
1854 return e_invalid_argument;
1855
1856 // When setting the global value to empty, make it "zip".
1857 if (*p_cm == NUL)
1858 {
1859 free_string_option(p_cm);
1860 p_cm = vim_strsave((char_u *)"zip");
1861 }
1862 // When using ":set cm=name" the local value is going to be empty.
1863 // Do that here, otherwise the crypt functions will still use the
1864 // local value.
1865 if ((args->os_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1866 {
1867 free_string_option(curbuf->b_p_cm);
1868 curbuf->b_p_cm = empty_option;
1869 }
1870
1871 // Need to update the swapfile when the effective method changed.
1872 // Set "s" to the effective old value, "p" to the effective new
1873 // method and compare.
1874 if ((args->os_flags & OPT_LOCAL) && *args->os_oldval.string == NUL)
1875 s = p_cm; // was previously using the global value
1876 else
1877 s = args->os_oldval.string;
1878 if (*curbuf->b_p_cm == NUL)
1879 p = p_cm; // is now using the global value
1880 else
1881 p = curbuf->b_p_cm;
1882 if (STRCMP(s, p) != 0)
1883 ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1884
1885 // If the global value changes need to update the swapfile for all
1886 // buffers using that value.
1887 if ((args->os_flags & OPT_GLOBAL)
1888 && STRCMP(p_cm, args->os_oldval.string) != 0)
1889 {
1890 buf_T *buf;
1891
1892 FOR_ALL_BUFFERS(buf)
1893 if (buf != curbuf && *buf->b_p_cm == NUL)
1894 ml_set_crypt_key(buf, buf->b_p_key, args->os_oldval.string);
1895 }
1896 return NULL;
1897}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001898
1899 int
1900expand_set_cryptmethod(optexpand_T *args, int *numMatches, char_u ***matches)
1901{
1902 return expand_set_opt_string(
1903 args,
1904 p_cm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001905 ARRAY_LENGTH(p_cm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001906 numMatches,
1907 matches);
1908}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001909#endif
1910
1911#if (defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1912/*
1913 * The 'cscopequickfix' option is changed.
1914 */
1915 char *
1916did_set_cscopequickfix(optset_T *args UNUSED)
1917{
1918 char_u *p;
1919
1920 if (p_csqf == NULL)
1921 return NULL;
1922
1923 p = p_csqf;
1924 while (*p != NUL)
1925 {
1926 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
1927 || p[1] == NUL
1928 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
1929 || (p[2] != NUL && p[2] != ','))
1930 return e_invalid_argument;
1931 else if (p[2] == NUL)
1932 break;
1933 else
1934 p += 3;
1935 }
1936
1937 return NULL;
1938}
1939#endif
1940
1941#if defined(FEAT_SYN_HL) || defined(PROTO)
1942/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001943 * The 'cursorlineopt' option is changed.
1944 */
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001945 char *
1946did_set_cursorlineopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001947{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001948 char_u **varp = (char_u **)args->os_varp;
1949
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001950 // This could be changed to use opt_strings_flags() instead.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001951 if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001952 return e_invalid_argument;
1953
1954 return NULL;
1955}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001956
1957 int
1958expand_set_cursorlineopt(optexpand_T *args, int *numMatches, char_u ***matches)
1959{
1960 return expand_set_opt_string(
1961 args,
1962 p_culopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001963 ARRAY_LENGTH(p_culopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001964 numMatches,
1965 matches);
1966}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001967#endif
1968
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001969/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001970 * The 'debug' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001971 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001972 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001973did_set_debug(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001974{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001975 return did_set_opt_strings(p_debug, p_debug_values, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001976}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001977
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001978 int
1979expand_set_debug(optexpand_T *args, int *numMatches, char_u ***matches)
1980{
1981 return expand_set_opt_string(
1982 args,
1983 p_debug_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001984 ARRAY_LENGTH(p_debug_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001985 numMatches,
1986 matches);
1987}
1988
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001989#if defined(FEAT_DIFF) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001990/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001991 * The 'diffopt' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001992 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001993 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001994did_set_diffopt(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001995{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001996 if (diffopt_changed() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001997 return e_invalid_argument;
1998
1999 return NULL;
2000}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002001
2002 int
2003expand_set_diffopt(optexpand_T *args, int *numMatches, char_u ***matches)
2004{
2005 expand_T *xp = args->oe_xp;
2006
2007 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2008 {
2009 // Within "algorithm:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002010 int algo_len = (int)STRLEN("algorithm:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002011 if (xp->xp_pattern - args->oe_set_arg >= algo_len &&
2012 STRNCMP(xp->xp_pattern - algo_len, "algorithm:", algo_len) == 0)
2013 {
2014 return expand_set_opt_string(
2015 args,
2016 p_dip_algorithm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002017 ARRAY_LENGTH(p_dip_algorithm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002018 numMatches,
2019 matches);
2020 }
Yee Cheng Chin9943d472025-03-26 19:41:02 +01002021 // Within "inline:", we have a subgroup of possible options.
2022 int inline_len = (int)STRLEN("inline:");
2023 if (xp->xp_pattern - args->oe_set_arg >= inline_len &&
2024 STRNCMP(xp->xp_pattern - inline_len, "inline:", inline_len) == 0)
2025 {
2026 return expand_set_opt_string(
2027 args,
2028 p_dip_inline_values,
2029 ARRAY_LENGTH(p_dip_inline_values) - 1,
2030 numMatches,
2031 matches);
2032 }
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002033 return FAIL;
2034 }
2035
2036 return expand_set_opt_string(
2037 args,
2038 p_dip_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002039 ARRAY_LENGTH(p_dip_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002040 numMatches,
2041 matches);
2042}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002043#endif
2044
2045/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002046 * The 'display' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002047 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002048 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002049did_set_display(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002050{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002051 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002052 return e_invalid_argument;
2053
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002054 (void)init_chartab();
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002055 return NULL;
2056}
2057
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002058 int
2059expand_set_display(optexpand_T *args, int *numMatches, char_u ***matches)
2060{
2061 return expand_set_opt_string(
2062 args,
2063 p_dy_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002064 ARRAY_LENGTH(p_dy_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002065 numMatches,
2066 matches);
2067}
2068
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002069/*
2070 * The 'eadirection' option is changed.
2071 */
2072 char *
2073did_set_eadirection(optset_T *args UNUSED)
2074{
2075 return did_set_opt_strings(p_ead, p_ead_values, FALSE);
2076}
2077
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002078 int
2079expand_set_eadirection(optexpand_T *args, int *numMatches, char_u ***matches)
2080{
2081 return expand_set_opt_string(
2082 args,
2083 p_ead_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002084 ARRAY_LENGTH(p_ead_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002085 numMatches,
2086 matches);
2087}
2088
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002089/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002090 * One of the 'encoding', 'fileencoding', 'termencoding' or 'makeencoding'
2091 * options is changed.
2092 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002093 char *
2094did_set_encoding(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002095{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002096 char_u **varp = (char_u **)args->os_varp;
2097 char_u **gvarp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002098 char *errmsg = NULL;
2099 char_u *p;
2100
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002101 // Get the global option to compare with, otherwise we would have to check
2102 // two values for all local options.
2103 gvarp = (char_u **)get_option_varp_scope(args->os_idx, OPT_GLOBAL);
2104
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002105 if (gvarp == &p_fenc)
2106 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002107 if (!curbuf->b_p_ma && args->os_flags != OPT_GLOBAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002108 errmsg = e_cannot_make_changes_modifiable_is_off;
2109 else if (vim_strchr(*varp, ',') != NULL)
2110 // No comma allowed in 'fileencoding'; catches confusing it
2111 // with 'fileencodings'.
2112 errmsg = e_invalid_argument;
2113 else
2114 {
2115 // May show a "+" in the title now.
2116 redraw_titles();
2117 // Add 'fileencoding' to the swap file.
2118 ml_setflags(curbuf);
2119 }
2120 }
2121 if (errmsg == NULL)
2122 {
2123 // canonize the value, so that STRCMP() can be used on it
2124 p = enc_canonize(*varp);
2125 if (p != NULL)
2126 {
2127 vim_free(*varp);
2128 *varp = p;
2129 }
2130 if (varp == &p_enc)
2131 {
2132 errmsg = mb_init();
2133 redraw_titles();
2134 }
2135 }
2136
2137#if defined(FEAT_GUI_GTK)
2138 if (errmsg == NULL && varp == &p_tenc && gui.in_use)
2139 {
2140 // GTK uses only a single encoding, and that is UTF-8.
2141 if (STRCMP(p_tenc, "utf-8") != 0)
2142 errmsg = e_cannot_be_changed_in_gtk_GUI;
2143 }
2144#endif
2145
2146 if (errmsg == NULL)
2147 {
2148#ifdef FEAT_KEYMAP
2149 // When 'keymap' is used and 'encoding' changes, reload the keymap
2150 // (with another encoding).
2151 if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
2152 (void)keymap_init();
2153#endif
2154
2155 // When 'termencoding' is not empty and 'encoding' changes or when
2156 // 'termencoding' changes, need to setup for keyboard input and
2157 // display output conversion.
2158 if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
2159 {
2160 if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
2161 || convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
2162 {
2163 semsg(_(e_cannot_convert_between_str_and_str),
2164 p_tenc, p_enc);
2165 errmsg = e_invalid_argument;
2166 }
2167 }
2168
2169#if defined(MSWIN)
K.Takatace3189d2023-02-15 19:13:43 +00002170 // $HOME, $VIM and $VIMRUNTIME may have characters in active code page.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002171 if (varp == &p_enc)
K.Takatace3189d2023-02-15 19:13:43 +00002172 {
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002173 init_homedir();
K.Takatace3189d2023-02-15 19:13:43 +00002174 init_vimdir();
2175 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002176#endif
2177 }
2178
2179 return errmsg;
2180}
2181
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002182 int
2183expand_set_encoding(optexpand_T *args, int *numMatches, char_u ***matches)
2184{
2185 return expand_set_opt_generic(
2186 args,
2187 get_encoding_name,
2188 numMatches,
2189 matches);
2190}
2191
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002192/*
Luuk van Baalb7147f82025-02-08 18:52:39 +01002193 * The 'eventignore(win)' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002194 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002195 char *
Luuk van Baalb7147f82025-02-08 18:52:39 +01002196did_set_eventignore(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002197{
Luuk van Baalb7147f82025-02-08 18:52:39 +01002198 char_u **varp = (char_u **)args->os_varp;
2199
2200 if (check_ei(*varp) == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002201 return e_invalid_argument;
2202 return NULL;
2203}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002204
Luuk van Baalb7147f82025-02-08 18:52:39 +01002205static int expand_eiw = FALSE;
2206
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002207 static char_u *
2208get_eventignore_name(expand_T *xp, int idx)
2209{
Luuk van Baalb7147f82025-02-08 18:52:39 +01002210 // 'eventignore(win)' allows special keyword "all" in addition to
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002211 // all event names.
2212 if (idx == 0)
2213 return (char_u *)"all";
Luuk van Baalb7147f82025-02-08 18:52:39 +01002214 return get_event_name_no_group(xp, idx - 1, expand_eiw);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002215}
2216
2217 int
2218expand_set_eventignore(optexpand_T *args, int *numMatches, char_u ***matches)
2219{
Luuk van Baalb7147f82025-02-08 18:52:39 +01002220 expand_eiw = args->oe_varp != (char_u *)&p_ei;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002221 return expand_set_opt_generic(
2222 args,
2223 get_eventignore_name,
2224 numMatches,
2225 matches);
2226}
2227
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002228/*
2229 * The 'fileformat' option is changed.
2230 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002231 char *
2232did_set_fileformat(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002233{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002234 char_u **varp = (char_u **)args->os_varp;
2235
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002236 if (!curbuf->b_p_ma && !(args->os_flags & OPT_GLOBAL))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002237 return e_cannot_make_changes_modifiable_is_off;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002238 else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002239 return e_invalid_argument;
2240
2241 // may also change 'textmode'
2242 if (get_fileformat(curbuf) == EOL_DOS)
2243 curbuf->b_p_tx = TRUE;
2244 else
2245 curbuf->b_p_tx = FALSE;
2246 redraw_titles();
2247 // update flag in swap file
2248 ml_setflags(curbuf);
2249 // Redraw needed when switching to/from "mac": a CR in the text
2250 // will be displayed differently.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002251 if (get_fileformat(curbuf) == EOL_MAC || *args->os_oldval.string == 'm')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002252 redraw_curbuf_later(UPD_NOT_VALID);
2253
2254 return NULL;
2255}
2256
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002257 int
2258expand_set_fileformat(optexpand_T *args, int *numMatches, char_u ***matches)
2259{
2260 return expand_set_opt_string(
2261 args,
2262 p_ff_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002263 ARRAY_LENGTH(p_ff_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002264 numMatches,
2265 matches);
2266}
2267
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002268/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002269 * Function given to ExpandGeneric() to obtain the possible arguments of the
2270 * fileformat options.
2271 */
2272 char_u *
2273get_fileformat_name(expand_T *xp UNUSED, int idx)
2274{
2275 if (idx >= (int)ARRAY_LENGTH(p_ff_values))
2276 return NULL;
2277
2278 return (char_u*)p_ff_values[idx];
2279}
2280
2281/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002282 * The 'fileformats' option is changed.
2283 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002284 char *
2285did_set_fileformats(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002286{
2287 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
2288 return e_invalid_argument;
2289
2290 // also change 'textauto'
2291 if (*p_ffs == NUL)
2292 p_ta = FALSE;
2293 else
2294 p_ta = TRUE;
2295
2296 return NULL;
2297}
2298
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002299/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002300 * The 'filetype' or the 'syntax' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002301 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002302 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002303did_set_filetype_or_syntax(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002304{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002305 char_u **varp = (char_u **)args->os_varp;
2306
2307 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002308 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002309
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002310 args->os_value_changed = STRCMP(args->os_oldval.string, *varp) != 0;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002311
2312 // Since we check the value, there is no need to set P_INSECURE,
2313 // even when the value comes from a modeline.
2314 args->os_value_checked = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002315
2316 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002317}
2318
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002319#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002320/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002321 * The 'foldclose' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002322 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002323 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002324did_set_foldclose(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002325{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002326 return did_set_opt_strings(p_fcl, p_fcl_values, TRUE);
2327}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002328
2329 int
2330expand_set_foldclose(optexpand_T *args, int *numMatches, char_u ***matches)
2331{
2332 return expand_set_opt_string(
2333 args,
2334 p_fcl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002335 ARRAY_LENGTH(p_fcl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002336 numMatches,
2337 matches);
2338}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002339#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002340
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002341#if (defined(FEAT_EVAL) && defined(FEAT_FOLDING)) || defined(PROTO)
2342/*
2343 * The 'foldexpr' option is changed.
2344 */
2345 char *
2346did_set_foldexpr(optset_T *args)
2347{
2348 (void)did_set_optexpr(args);
2349 if (foldmethodIsExpr(curwin))
2350 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002351 return NULL;
2352}
2353#endif
2354
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002355#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002356/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002357 * The 'foldignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002358 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002359 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002360did_set_foldignore(optset_T *args UNUSED)
2361{
2362 if (foldmethodIsIndent(curwin))
2363 foldUpdateAll(curwin);
2364 return NULL;
2365}
2366
2367/*
2368 * The 'foldmarker' option is changed.
2369 */
2370 char *
2371did_set_foldmarker(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002372{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002373 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002374 char_u *p;
2375
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002376 p = vim_strchr(*varp, ',');
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002377 if (p == NULL)
2378 return e_comma_required;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002379 else if (p == *varp || p[1] == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002380 return e_invalid_argument;
2381 else if (foldmethodIsMarker(curwin))
2382 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002383
2384 return NULL;
2385}
2386
2387/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002388 * The 'foldmethod' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002389 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002390 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002391did_set_foldmethod(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002392{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002393 char_u **varp = (char_u **)args->os_varp;
2394
Milly142cad12024-10-22 22:11:51 +02002395 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK || **varp == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002396 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002397
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002398 foldUpdateAll(curwin);
2399 if (foldmethodIsDiff(curwin))
2400 newFoldLevel();
2401 return NULL;
2402}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002403
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002404 int
2405expand_set_foldmethod(optexpand_T *args, int *numMatches, char_u ***matches)
2406{
2407 return expand_set_opt_string(
2408 args,
2409 p_fdm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002410 ARRAY_LENGTH(p_fdm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002411 numMatches,
2412 matches);
2413}
2414
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002415/*
2416 * The 'foldopen' option is changed.
2417 */
2418 char *
2419did_set_foldopen(optset_T *args UNUSED)
2420{
2421 return did_set_opt_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
2422}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002423
2424 int
2425expand_set_foldopen(optexpand_T *args, int *numMatches, char_u ***matches)
2426{
2427 return expand_set_opt_string(
2428 args,
2429 p_fdo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002430 ARRAY_LENGTH(p_fdo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002431 numMatches,
2432 matches);
2433}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002434#endif
2435
2436/*
2437 * The 'formatoptions' option is changed.
2438 */
2439 char *
2440did_set_formatoptions(optset_T *args)
2441{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002442 char_u **varp = (char_u **)args->os_varp;
2443
Christian Brabandtb39b2402023-11-29 11:34:05 +01002444 return did_set_option_listflag(*varp, (char_u *)FO_ALL, args->os_errbuf,
2445 args->os_errbuflen);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002446}
2447
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002448 int
2449expand_set_formatoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2450{
2451 return expand_set_opt_listflag(args, (char_u*)FO_ALL, numMatches, matches);
2452}
2453
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002454#if defined(CURSOR_SHAPE) || defined(PROTO)
2455/*
2456 * The 'guicursor' option is changed.
2457 */
2458 char *
2459did_set_guicursor(optset_T *args UNUSED)
2460{
2461 return parse_shape_opt(SHAPE_CURSOR);
2462}
2463#endif
2464
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002465#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002466/*
2467 * The 'guifont' option is changed.
2468 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002469 char *
2470did_set_guifont(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002471{
2472 char_u *p;
2473 char *errmsg = NULL;
2474
2475 if (gui.in_use)
2476 {
2477 p = p_guifont;
2478# if defined(FEAT_GUI_GTK)
2479 // Put up a font dialog and let the user select a new value.
2480 // If this is cancelled go back to the old value but don't
2481 // give an error message.
2482 if (STRCMP(p, "*") == 0)
2483 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002484 p = gui_mch_font_dialog(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002485 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002486 p_guifont = (p != NULL) ? p : vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002487 }
2488# endif
2489 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
2490 {
2491# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
2492 if (STRCMP(p_guifont, "*") == 0)
2493 {
2494 // Dialog was cancelled: Keep the old value without giving
2495 // an error message.
2496 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002497 p_guifont = vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002498 }
2499 else
2500# endif
2501 errmsg = e_invalid_fonts;
2502 }
2503 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002504
2505 return errmsg;
2506}
2507
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002508/*
2509 * Expand the 'guifont' option. Only when GUI is being used. Each platform has
2510 * specific behaviors.
2511 */
2512 int
2513expand_set_guifont(optexpand_T *args, int *numMatches, char_u ***matches)
2514{
2515 if (!gui.in_use)
2516 return FAIL;
2517
2518# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
2519 char_u **varp = (char_u **)args->oe_varp;
2520 int wide = (varp == &p_guifontwide);
2521
2522 return expand_set_opt_callback(
2523 args, gui_mch_expand_font, &wide, numMatches, matches);
2524# else
2525 return FAIL;
2526# endif
2527}
2528
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002529# if defined(FEAT_XFONTSET) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002530/*
2531 * The 'guifontset' option is changed.
2532 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002533 char *
2534did_set_guifontset(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002535{
2536 char *errmsg = NULL;
2537
2538 if (STRCMP(p_guifontset, "*") == 0)
2539 errmsg = e_cant_select_fontset;
2540 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
2541 errmsg = e_invalid_fontset;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002542
2543 return errmsg;
2544}
2545# endif
2546
2547/*
2548 * The 'guifontwide' option is changed.
2549 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002550 char *
2551did_set_guifontwide(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002552{
2553 char *errmsg = NULL;
2554
2555 if (STRCMP(p_guifontwide, "*") == 0)
2556 errmsg = e_cant_select_wide_font;
2557 else if (gui_get_wide_font() == FAIL)
2558 errmsg = e_invalid_wide_font;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002559
2560 return errmsg;
2561}
2562#endif
2563
Erik S. V. Jansson8b1e7492024-02-24 14:26:52 +01002564#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002565/*
2566 * The 'guiligatures' option is changed.
2567 */
2568 char *
2569did_set_guiligatures(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002570{
2571 gui_set_ligatures();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002572 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002573}
2574#endif
2575
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002576#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002577/*
2578 * The 'guioptions' option is changed.
2579 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002580 char *
2581did_set_guioptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002582{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002583 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002584 char *errmsg;
2585
Christian Brabandtb39b2402023-11-29 11:34:05 +01002586 errmsg = did_set_option_listflag(*varp, (char_u *)GO_ALL, args->os_errbuf,
2587 args->os_errbuflen);
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002588 if (errmsg != NULL)
2589 return errmsg;
2590
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002591 gui_init_which_components(args->os_oldval.string);
2592 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002593}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002594
2595 int
2596expand_set_guioptions(optexpand_T *args, int *numMatches, char_u ***matches)
2597{
2598 return expand_set_opt_listflag(args, (char_u*)GO_ALL, numMatches, matches);
2599}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002600#endif
2601
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002602#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002603/*
2604 * The 'guitablabel' option is changed.
2605 */
2606 char *
2607did_set_guitablabel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002608{
2609 redraw_tabline = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002610 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002611}
2612#endif
2613
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002614/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002615 * The 'helpfile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002616 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002617 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002618did_set_helpfile(optset_T *args UNUSED)
2619{
2620 // May compute new values for $VIM and $VIMRUNTIME
2621 if (didset_vim)
2622 vim_unsetenv_ext((char_u *)"VIM");
2623 if (didset_vimruntime)
2624 vim_unsetenv_ext((char_u *)"VIMRUNTIME");
2625 return NULL;
2626}
2627
2628#if defined(FEAT_MULTI_LANG) || defined(PROTO)
2629/*
2630 * The 'helplang' option is changed.
2631 */
2632 char *
2633did_set_helplang(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002634{
2635 char *errmsg = NULL;
2636
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002637 // Check for "", "ab", "ab,cd", etc.
2638 for (char_u *s = p_hlg; *s != NUL; s += 3)
2639 {
2640 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
2641 {
2642 errmsg = e_invalid_argument;
2643 break;
2644 }
2645 if (s[2] == NUL)
2646 break;
2647 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002648
2649 return errmsg;
2650}
2651#endif
2652
2653/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002654 * The 'highlight' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002655 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002656 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002657did_set_highlight(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002658{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002659 if (highlight_changed() == FAIL)
2660 return e_invalid_argument; // invalid flags
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002661
2662 return NULL;
2663}
2664
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002665/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002666 * Expand 'highlight' option.
2667 */
2668 int
2669expand_set_highlight(optexpand_T *args, int *numMatches, char_u ***matches)
2670{
2671 char_u *p;
2672 expand_T *xp = args->oe_xp;
2673 static char_u hl_flags[HLF_COUNT] = HL_FLAGS;
Christian Brabandt3f168ec2023-10-02 23:21:11 +02002674 size_t i;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002675 int count = 0;
2676
2677 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2678 {
2679 // Right after a ':', meaning we just return all highlight names.
2680 return expand_set_opt_generic(
2681 args,
2682 get_highlight_name,
2683 numMatches,
2684 matches);
2685 }
2686
2687 if (*xp->xp_pattern == NUL)
2688 {
2689 // At beginning of a comma-separated list. Return the specific list of
2690 // supported occasions.
2691 *matches = ALLOC_MULT(char_u *, HLF_COUNT + 1);
2692 if (*matches == NULL)
2693 return FAIL;
2694
2695 // We still want to return the full option if it's requested.
2696 if (args->oe_include_orig_val)
2697 {
2698 p = vim_strsave(args->oe_opt_value);
2699 if (p == NULL)
2700 {
2701 VIM_CLEAR(*matches);
2702 return FAIL;
2703 }
2704 (*matches)[count++] = p;
2705 }
2706
2707 for (i = 0; i < HLF_COUNT; i++)
2708 {
2709 p = vim_strnsave(&hl_flags[i], 1);
2710 if (p == NULL)
2711 {
2712 if (count == 0)
2713 {
2714 VIM_CLEAR(*matches);
2715 return FAIL;
2716 }
2717 else
2718 break;
2719 }
2720 (*matches)[count++] = p;
2721 }
2722
2723 if (count == 0)
2724 {
2725 VIM_CLEAR(*matches);
2726 return FAIL;
2727 }
2728 *numMatches = count;
2729 return OK;
2730 }
2731
2732 // We are after the initial character (which indicates the occasion). We
2733 // already made sure we are not matching after a ':' above, so now we want
2734 // to match against display mode modifiers.
2735 // Since the xp_pattern starts from the beginning, we need to include it in
2736 // the returned match.
2737
2738 // Note: Keep this in sync with highlight_changed()
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002739 static char_u p_hl_mode_values[] =
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002740 {':', 'b', 'i', '-', 'n', 'r', 's', 'u', 'c', '2', 'd', '=', 't'};
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002741 size_t num_hl_modes = ARRAY_LENGTH(p_hl_mode_values);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002742
2743 *matches = ALLOC_MULT(char_u *, num_hl_modes);
2744 if (*matches == NULL)
2745 return FAIL;
2746
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002747 int pattern_len = xp->xp_pattern_len;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002748
2749 for (i = 0; i < num_hl_modes; i++)
2750 {
2751 // Don't allow duplicates as these are unique flags
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002752 char_u *dup = vim_strchr(xp->xp_pattern + 1, p_hl_mode_values[i]);
2753 if (dup != NULL && (int)(dup - xp->xp_pattern) < pattern_len)
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002754 continue;
2755
2756 // ':' only works by itself, not with other flags.
2757 if (pattern_len > 1 && p_hl_mode_values[i] == ':')
2758 continue;
2759
2760 p = vim_strnsave(xp->xp_pattern, pattern_len + 1);
2761 if (p == NULL)
2762 {
2763 if (i == 0)
2764 {
2765 VIM_CLEAR(*matches);
2766 return FAIL;
2767 }
2768 else
2769 break;
2770 }
2771 p[pattern_len] = p_hl_mode_values[i];
2772 p[pattern_len + 1] = NUL;
2773 (*matches)[count++] = p;
2774 }
2775 if (count == 0)
2776 {
2777 VIM_CLEAR(*matches);
2778 return FAIL;
2779 }
2780 *numMatches = count;
2781
2782 return OK;
2783}
2784
2785/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002786 * The 'titlestring' or the 'iconstring' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002787 */
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002788 static char *
2789parse_titleiconstring(optset_T *args UNUSED, int flagval UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002790{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002791#ifdef FEAT_STL_OPT
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002792 char_u **varp = (char_u **)args->os_varp;
2793
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002794 // NULL => statusline syntax
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002795 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002796 stl_syntax |= flagval;
2797 else
2798 stl_syntax &= ~flagval;
2799#endif
2800 did_set_title();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002801
2802 return NULL;
2803}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002804
2805/*
2806 * The 'iconstring' option is changed.
2807 */
2808 char *
2809did_set_iconstring(optset_T *args)
2810{
2811 int flagval = 0;
2812
2813#ifdef FEAT_STL_OPT
2814 flagval = STL_IN_ICON;
2815#endif
2816
2817 return parse_titleiconstring(args, flagval);
2818}
2819
2820#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
2821/*
2822 * The 'imactivatekey' option is changed.
2823 */
2824 char *
2825did_set_imactivatekey(optset_T *args UNUSED)
2826{
2827 if (!im_xim_isvalid_imactivate())
2828 return e_invalid_argument;
2829 return NULL;
2830}
2831#endif
2832
2833/*
Milly5e7a6a42024-10-22 22:27:19 +02002834 * The 'iskeyword' option is changed.
2835 */
2836 char *
2837did_set_iskeyword(optset_T *args)
2838{
2839 char_u **varp = (char_u **)args->os_varp;
2840
2841 if (varp == &p_isk) // only check for global-value
2842 {
2843 if (check_isopt(*varp) == FAIL)
2844 return e_invalid_argument;
2845 }
2846 else // fallthrough for local-value
2847 return did_set_isopt(args);
2848
2849 return NULL;
2850}
2851
2852/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002853 * The 'isident' or the 'iskeyword' or the 'isprint' or the 'isfname' option is
2854 * changed.
2855 */
2856 char *
2857did_set_isopt(optset_T *args)
2858{
Milly5e7a6a42024-10-22 22:27:19 +02002859 // 'isident', 'iskeyword', 'isprint' or 'isfname' option: refill g_chartab[]
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002860 // If the new option is invalid, use old value.
2861 // 'lisp' option: refill g_chartab[] for '-' char.
2862 if (init_chartab() == FAIL)
2863 {
2864 args->os_restore_chartab = TRUE;// need to restore the chartab.
2865 return e_invalid_argument; // error in value
2866 }
2867
2868 return NULL;
2869}
2870
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002871/*
2872 * The 'jumpoptions' option is changed.
2873 */
2874 char *
Yegappan Lakshmanan1926ae42023-09-21 16:36:28 +02002875did_set_jumpoptions(optset_T *args UNUSED)
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002876{
2877 if (opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE) != OK)
2878 return e_invalid_argument;
2879
2880 return NULL;
2881}
2882
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002883 int
2884expand_set_jumpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2885{
2886 return expand_set_opt_string(
2887 args,
2888 p_jop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002889 ARRAY_LENGTH(p_jop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002890 numMatches,
2891 matches);
2892}
2893
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002894#if defined(FEAT_KEYMAP) || defined(PROTO)
2895/*
2896 * The 'keymap' option is changed.
2897 */
2898 char *
2899did_set_keymap(optset_T *args)
2900{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002901 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002902 char *errmsg = NULL;
2903
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002904 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002905 errmsg = e_invalid_argument;
2906 else
2907 {
2908 int secure_save = secure;
2909
2910 // Reset the secure flag, since the value of 'keymap' has
2911 // been checked to be safe.
2912 secure = 0;
2913
2914 // load or unload key mapping tables
2915 errmsg = keymap_init();
2916
2917 secure = secure_save;
2918
2919 // Since we check the value, there is no need to set P_INSECURE,
2920 // even when the value comes from a modeline.
2921 args->os_value_checked = TRUE;
2922 }
2923
2924 if (errmsg == NULL)
2925 {
2926 if (*curbuf->b_p_keymap != NUL)
2927 {
2928 // Installed a new keymap, switch on using it.
2929 curbuf->b_p_iminsert = B_IMODE_LMAP;
2930 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
2931 curbuf->b_p_imsearch = B_IMODE_LMAP;
2932 }
2933 else
2934 {
2935 // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
2936 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
2937 curbuf->b_p_iminsert = B_IMODE_NONE;
2938 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
2939 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
2940 }
2941 if ((args->os_flags & OPT_LOCAL) == 0)
2942 {
2943 set_iminsert_global();
2944 set_imsearch_global();
2945 }
2946 status_redraw_curbuf();
2947 }
2948
2949 return errmsg;
2950}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002951#endif
2952
2953/*
2954 * The 'keymodel' option is changed.
2955 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002956 char *
2957did_set_keymodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002958{
2959 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
2960 return e_invalid_argument;
2961
2962 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
2963 km_startsel = (vim_strchr(p_km, 'a') != NULL);
2964 return NULL;
2965}
2966
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002967 int
2968expand_set_keymodel(optexpand_T *args, int *numMatches, char_u ***matches)
2969{
2970 return expand_set_opt_string(
2971 args,
2972 p_km_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002973 ARRAY_LENGTH(p_km_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002974 numMatches,
2975 matches);
2976}
2977
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002978/*
2979 * The 'keyprotocol' option is changed.
2980 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002981 char *
2982did_set_keyprotocol(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002983{
Gregory Anders3695d0e2023-09-29 20:17:20 +02002984 char_u *term = T_NAME;
2985 keyprot_T kpc = match_keyprotocol(term);
2986 if (kpc == KEYPROTOCOL_FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002987 return e_invalid_argument;
2988
Gregory Anders3695d0e2023-09-29 20:17:20 +02002989 apply_keyprotocol(term, kpc);
2990
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002991 return NULL;
2992}
2993
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002994 int
2995expand_set_keyprotocol(optexpand_T *args, int *numMatches, char_u ***matches)
2996{
2997 expand_T *xp = args->oe_xp;
2998 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2999 {
3000 // 'keyprotocol' only has well-defined terms for completion for the
3001 // protocol part after the colon.
3002 return expand_set_opt_string(
3003 args,
3004 p_kpc_protocol_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003005 ARRAY_LENGTH(p_kpc_protocol_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003006 numMatches,
3007 matches);
3008 }
3009 // Use expand_set_opt_string instead of returning FAIL so that we can
3010 // include the original value if args->oe_include_orig_val is set.
3011 static char *(empty[]) = {NULL};
3012 return expand_set_opt_string(args, empty, 0, numMatches, matches);
3013}
3014
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003015/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003016 * The 'lispoptions' option is changed.
3017 */
3018 char *
3019did_set_lispoptions(optset_T *args)
3020{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003021 char_u **varp = (char_u **)args->os_varp;
3022
3023 if (**varp != NUL
3024 && STRCMP(*varp, "expr:0") != 0
3025 && STRCMP(*varp, "expr:1") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003026 return e_invalid_argument;
3027
3028 return NULL;
3029}
3030
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003031 int
3032expand_set_lispoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3033{
3034 static char *(p_lop_values[]) = {"expr:0", "expr:1", NULL};
3035 return expand_set_opt_string(
3036 args,
3037 p_lop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003038 ARRAY_LENGTH(p_lop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003039 numMatches,
3040 matches);
3041}
3042
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003043/*
3044 * The 'matchpairs' option is changed.
3045 */
3046 char *
3047did_set_matchpairs(optset_T *args)
3048{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003049 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003050 char_u *p;
3051
3052 if (has_mbyte)
3053 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003054 for (p = *varp; *p != NUL; ++p)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003055 {
3056 int x2 = -1;
3057 int x3 = -1;
3058
3059 p += mb_ptr2len(p);
3060 if (*p != NUL)
3061 x2 = *p++;
3062 if (*p != NUL)
3063 {
3064 x3 = mb_ptr2char(p);
3065 p += mb_ptr2len(p);
3066 }
3067 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
3068 return e_invalid_argument;
3069 if (*p == NUL)
3070 break;
3071 }
3072 }
3073 else
3074 {
3075 // Check for "x:y,x:y"
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003076 for (p = *varp; *p != NUL; p += 4)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003077 {
3078 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
3079 return e_invalid_argument;
3080 if (p[3] == NUL)
3081 break;
3082 }
3083 }
3084
3085 return NULL;
3086}
3087
Christian Brabandt51d4d842024-12-06 17:26:25 +01003088/*
3089 * Process the updated 'messagesopt' option value.
3090 */
3091 char *
3092did_set_messagesopt(optset_T *args UNUSED)
3093{
3094 if (messagesopt_changed() == FAIL)
3095 return e_invalid_argument;
3096
3097 return NULL;
3098}
3099
3100 int
3101expand_set_messagesopt(optexpand_T *args, int *numMatches, char_u ***matches)
3102{
zeertzjq8cc43da2024-12-07 16:09:08 +01003103 static char *(p_mopt_values[]) = {"hit-enter", "wait:", "history:", NULL};
Christian Brabandt51d4d842024-12-06 17:26:25 +01003104 return expand_set_opt_string(
3105 args,
zeertzjq8cc43da2024-12-07 16:09:08 +01003106 p_mopt_values,
3107 ARRAY_LENGTH(p_mopt_values) - 1,
Christian Brabandt51d4d842024-12-06 17:26:25 +01003108 numMatches,
3109 matches);
3110}
3111
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003112#if defined(FEAT_SPELL) || defined(PROTO)
3113/*
3114 * The 'mkspellmem' option is changed.
3115 */
3116 char *
3117did_set_mkspellmem(optset_T *args UNUSED)
3118{
3119 if (spell_check_msm() != OK)
3120 return e_invalid_argument;
3121
3122 return NULL;
3123}
3124#endif
3125
3126/*
3127 * The 'mouse' option is changed.
3128 */
3129 char *
3130did_set_mouse(optset_T *args)
3131{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003132 char_u **varp = (char_u **)args->os_varp;
3133
Christian Brabandtb39b2402023-11-29 11:34:05 +01003134 return did_set_option_listflag(*varp, (char_u *)MOUSE_ALL, args->os_errbuf,
3135 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003136}
3137
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003138 int
3139expand_set_mouse(optexpand_T *args, int *numMatches, char_u ***matches)
3140{
3141 return expand_set_opt_listflag(args, (char_u*)MOUSE_ALL, numMatches, matches);
3142}
3143
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003144/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003145 * The 'mousemodel' option is changed.
3146 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003147 char *
3148did_set_mousemodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003149{
3150 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
3151 return e_invalid_argument;
3152#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
Ken Takata18d0d292023-12-19 20:12:29 +01003153 else if (*p_mousem != *args->os_oldval.string)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003154 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
3155 // to create or delete the popup menus.
3156 gui_motif_update_mousemodel(root_menu);
3157#endif
3158
3159 return NULL;
3160}
3161
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003162 int
3163expand_set_mousemodel(optexpand_T *args, int *numMatches, char_u ***matches)
3164{
3165 return expand_set_opt_string(
3166 args,
3167 p_mousem_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003168 ARRAY_LENGTH(p_mousem_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003169 numMatches,
3170 matches);
3171}
3172
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003173#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3174 char *
3175did_set_mouseshape(optset_T *args UNUSED)
3176{
3177 char *errmsg = NULL;
3178
3179 errmsg = parse_shape_opt(SHAPE_MOUSE);
3180 update_mouseshape(-1);
3181
3182 return errmsg;
3183}
3184#endif
3185
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003186/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003187 * The 'nrformats' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003188 */
3189 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003190did_set_nrformats(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003191{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003192 char_u **varp = (char_u **)args->os_varp;
3193
3194 return did_set_opt_strings(*varp, p_nf_values, TRUE);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003195}
3196
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003197 int
3198expand_set_nrformats(optexpand_T *args, int *numMatches, char_u ***matches)
3199{
3200 return expand_set_opt_string(
3201 args,
3202 p_nf_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003203 ARRAY_LENGTH(p_nf_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003204 numMatches,
3205 matches);
3206}
3207
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003208#if defined(FEAT_EVAL) || defined(PROTO)
3209/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003210 * One of the '*expr' options is changed: 'balloonexpr', 'diffexpr',
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003211 * 'foldexpr', 'foldtext', 'formatexpr', 'includeexpr', 'indentexpr',
3212 * 'patchexpr', 'printexpr' and 'charconvert'.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003213 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003214 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003215did_set_optexpr(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003216{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003217 char_u **varp = (char_u **)args->os_varp;
3218
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003219 // If the option value starts with <SID> or s:, then replace that with
3220 // the script identifier.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003221 char_u *name = get_scriptlocal_funcname(*varp);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003222 if (name != NULL)
3223 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003224 free_string_option(*varp);
3225 *varp = name;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003226 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003227
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003228 return NULL;
3229}
3230#endif
3231
3232/*
3233 * The 'pastetoggle' option is changed.
3234 */
3235 char *
3236did_set_pastetoggle(optset_T *args UNUSED)
3237{
3238 char_u *p;
3239
3240 // translate key codes like in a mapping
3241 if (*p_pt)
3242 {
zeertzjq7e0bae02023-08-11 23:15:38 +02003243 (void)replace_termcodes(p_pt, &p, 0,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003244 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
3245 if (p != NULL)
3246 {
3247 free_string_option(p_pt);
3248 p_pt = p;
3249 }
3250 }
3251
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003252 return NULL;
3253}
3254
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003255#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3256/*
3257 * The 'previewpopup' option is changed.
3258 */
3259 char *
3260did_set_previewpopup(optset_T *args UNUSED)
3261{
3262 if (parse_previewpopup(NULL) == FAIL)
3263 return e_invalid_argument;
3264
3265 return NULL;
3266}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003267
3268 int
3269expand_set_popupoption(optexpand_T *args, int *numMatches, char_u ***matches)
3270{
3271 expand_T *xp = args->oe_xp;
3272
3273 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3274 {
3275 // Within "highlight:"/"border:"/"align:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003276 int border_len = (int)STRLEN("border:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003277 if (xp->xp_pattern - args->oe_set_arg >= border_len &&
3278 STRNCMP(xp->xp_pattern - border_len, "border:", border_len) == 0)
3279 {
3280 return expand_set_opt_string(
3281 args,
3282 p_popup_option_border_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003283 ARRAY_LENGTH(p_popup_option_border_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003284 numMatches,
3285 matches);
3286 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003287 int align_len = (int)STRLEN("align:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003288 if (xp->xp_pattern - args->oe_set_arg >= align_len &&
3289 STRNCMP(xp->xp_pattern - align_len, "align:", align_len) == 0)
3290 {
3291 return expand_set_opt_string(
3292 args,
3293 p_popup_option_align_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003294 ARRAY_LENGTH(p_popup_option_align_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003295 numMatches,
3296 matches);
3297 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003298 int highlight_len = (int)STRLEN("highlight:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003299 if (xp->xp_pattern - args->oe_set_arg >= highlight_len &&
3300 STRNCMP(xp->xp_pattern - highlight_len, "highlight:", highlight_len) == 0)
3301 {
3302 // Return the list of all highlight names
3303 return expand_set_opt_generic(
3304 args,
3305 get_highlight_name,
3306 numMatches,
3307 matches);
3308 }
3309 return FAIL;
3310 }
3311
3312 return expand_set_opt_string(
3313 args,
3314 p_popup_option_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003315 ARRAY_LENGTH(p_popup_option_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003316 numMatches,
3317 matches);
3318}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003319#endif
3320
3321#if defined(FEAT_POSTSCRIPT) || defined(PROTO)
3322/*
3323 * The 'printencoding' option is changed.
3324 */
3325 char *
3326did_set_printencoding(optset_T *args UNUSED)
3327{
3328 char_u *s, *p;
3329
3330 // Canonize 'printencoding' if VIM standard one
3331 p = enc_canonize(p_penc);
3332 if (p != NULL)
3333 {
3334 vim_free(p_penc);
3335 p_penc = p;
3336 }
3337 else
3338 {
3339 // Ensure lower case and '-' for '_'
3340 for (s = p_penc; *s != NUL; s++)
3341 {
3342 if (*s == '_')
3343 *s = '-';
3344 else
3345 *s = TOLOWER_ASC(*s);
3346 }
3347 }
3348
3349 return NULL;
3350}
3351#endif
3352
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003353#if defined(FEAT_PRINTER) || defined(PROTO)
3354
3355 static char_u *
3356get_printoptions_names(expand_T *xp UNUSED, int idx)
3357{
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003358 if (idx >= (int)ARRAY_LENGTH(printer_opts))
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003359 return NULL;
3360 return (char_u*)printer_opts[idx].name;
3361}
3362
3363/*
3364 * Expand 'printoptions' option
3365 */
3366 int
3367expand_set_printoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3368{
3369 return expand_set_opt_generic(
3370 args,
3371 get_printoptions_names,
3372 numMatches,
3373 matches);
3374}
3375#endif
3376
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003377#if defined(FEAT_STL_OPT) || defined(PROTO)
3378/*
3379 * The 'statusline' or the 'tabline' or the 'rulerformat' option is changed.
3380 * "rulerformat" is TRUE if the 'rulerformat' option is changed.
3381 */
3382 static char *
3383parse_statustabline_rulerformat(optset_T *args, int rulerformat)
3384{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003385 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003386 char_u *s;
3387 char *errmsg = NULL;
3388 int wid;
3389
3390 if (rulerformat) // reset ru_wid first
3391 ru_wid = 0;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003392 s = *varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003393 if (rulerformat && *s == '%')
3394 {
3395 // set ru_wid if 'ruf' starts with "%99("
3396 if (*++s == '-') // ignore a '-'
3397 s++;
3398 wid = getdigits(&s);
3399 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
3400 ru_wid = wid;
3401 else
Yegappan Lakshmananac023e82024-11-27 20:55:45 +01003402 {
3403 // Validate the flags in 'rulerformat' only if it doesn't point to
3404 // a custom function ("%!" flag).
3405 if ((*varp)[1] != '!')
3406 errmsg = check_stl_option(p_ruf);
3407 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003408 }
3409 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
3410 else if (rulerformat || s[0] != '%' || s[1] != '!')
3411 errmsg = check_stl_option(s);
3412 if (rulerformat && errmsg == NULL)
3413 comp_col();
3414
3415 return errmsg;
3416}
3417#endif
3418
3419#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
3420/*
3421 * The 'renderoptions' option is changed.
3422 */
3423 char *
3424did_set_renderoptions(optset_T *args UNUSED)
3425{
3426 if (!gui_mch_set_rendering_options(p_rop))
3427 return e_invalid_argument;
3428
3429 return NULL;
3430}
3431#endif
3432
3433#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3434/*
3435 * The 'rightleftcmd' option is changed.
3436 */
3437 char *
3438did_set_rightleftcmd(optset_T *args)
3439{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003440 char_u **varp = (char_u **)args->os_varp;
3441
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003442 // Currently only "search" is a supported value.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003443 if (**varp != NUL && STRCMP(*varp, "search") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003444 return e_invalid_argument;
3445
3446 return NULL;
3447}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003448
3449 int
3450expand_set_rightleftcmd(optexpand_T *args, int *numMatches, char_u ***matches)
3451{
3452 static char *(p_rlc_values[]) = {"search", NULL};
3453 return expand_set_opt_string(
3454 args,
3455 p_rlc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003456 ARRAY_LENGTH(p_rlc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003457 numMatches,
3458 matches);
3459}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003460#endif
3461
3462#if defined(FEAT_STL_OPT) || defined(PROTO)
3463/*
3464 * The 'rulerformat' option is changed.
3465 */
3466 char *
3467did_set_rulerformat(optset_T *args)
3468{
3469 return parse_statustabline_rulerformat(args, TRUE);
3470}
3471#endif
3472
3473/*
3474 * The 'scrollopt' option is changed.
3475 */
3476 char *
3477did_set_scrollopt(optset_T *args UNUSED)
3478{
3479 return did_set_opt_strings(p_sbo, p_scbopt_values, TRUE);
3480}
3481
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003482 int
3483expand_set_scrollopt(optexpand_T *args, int *numMatches, char_u ***matches)
3484{
3485 return expand_set_opt_string(
3486 args,
3487 p_scbopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003488 ARRAY_LENGTH(p_scbopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003489 numMatches,
3490 matches);
3491}
3492
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003493/*
3494 * The 'selection' option is changed.
3495 */
3496 char *
3497did_set_selection(optset_T *args UNUSED)
3498{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003499 if (*p_sel == NUL || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003500 return e_invalid_argument;
3501
3502 return NULL;
3503}
3504
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003505 int
3506expand_set_selection(optexpand_T *args, int *numMatches, char_u ***matches)
3507{
3508 return expand_set_opt_string(
3509 args,
3510 p_sel_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003511 ARRAY_LENGTH(p_sel_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003512 numMatches,
3513 matches);
3514}
3515
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003516/*
3517 * The 'selectmode' option is changed.
3518 */
3519 char *
3520did_set_selectmode(optset_T *args UNUSED)
3521{
3522 return did_set_opt_strings(p_slm, p_slm_values, TRUE);
3523}
3524
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003525 int
3526expand_set_selectmode(optexpand_T *args, int *numMatches, char_u ***matches)
3527{
3528 return expand_set_opt_string(
3529 args,
3530 p_slm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003531 ARRAY_LENGTH(p_slm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003532 numMatches,
3533 matches);
3534}
3535
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003536#if defined(FEAT_SESSION) || defined(PROTO)
3537/*
3538 * The 'sessionoptions' option is changed.
3539 */
3540 char *
3541did_set_sessionoptions(optset_T *args)
3542{
3543 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
3544 return e_invalid_argument;
3545 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
3546 {
3547 // Don't allow both "sesdir" and "curdir".
3548 (void)opt_strings_flags(args->os_oldval.string, p_ssop_values,
3549 &ssop_flags, TRUE);
3550 return e_invalid_argument;
3551 }
3552
3553 return NULL;
3554}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003555
3556 int
3557expand_set_sessionoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3558{
3559 return expand_set_opt_string(
3560 args,
3561 p_ssop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003562 ARRAY_LENGTH(p_ssop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003563 numMatches,
3564 matches);
3565}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003566#endif
3567
3568/*
3569 * The 'shortmess' option is changed.
3570 */
3571 char *
3572did_set_shortmess(optset_T *args)
3573{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003574 char_u **varp = (char_u **)args->os_varp;
3575
Christian Brabandtb39b2402023-11-29 11:34:05 +01003576 return did_set_option_listflag(*varp, (char_u *)SHM_ALL, args->os_errbuf,
3577 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003578}
3579
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003580 int
3581expand_set_shortmess(optexpand_T *args, int *numMatches, char_u ***matches)
3582{
3583 return expand_set_opt_listflag(args, (char_u*)SHM_ALL, numMatches, matches);
3584}
3585
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003586#if defined(FEAT_LINEBREAK) || defined(PROTO)
3587/*
3588 * The 'showbreak' option is changed.
3589 */
3590 char *
3591did_set_showbreak(optset_T *args)
3592{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003593 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003594 char_u *s;
3595
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003596 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003597 {
3598 if (ptr2cells(s) != 1)
3599 return e_showbreak_contains_unprintable_or_wide_character;
3600 MB_PTR_ADV(s);
3601 }
3602
3603 return NULL;
3604}
3605#endif
3606
3607/*
3608 * The 'showcmdloc' option is changed.
3609 */
3610 char *
3611did_set_showcmdloc(optset_T *args UNUSED)
3612{
zeertzjqc27fcf42024-03-01 23:01:43 +01003613 char *errmsg = did_set_opt_strings(p_sloc, p_sloc_values, FALSE);
3614
3615 if (errmsg == NULL)
3616 comp_col();
3617
3618 return errmsg;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003619}
3620
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003621 int
3622expand_set_showcmdloc(optexpand_T *args, int *numMatches, char_u ***matches)
3623{
3624 return expand_set_opt_string(
3625 args,
3626 p_sloc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003627 ARRAY_LENGTH(p_sloc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003628 numMatches,
3629 matches);
3630}
3631
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003632#if defined(FEAT_SIGNS) || defined(PROTO)
3633/*
3634 * The 'signcolumn' option is changed.
3635 */
3636 char *
3637did_set_signcolumn(optset_T *args)
3638{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003639 char_u **varp = (char_u **)args->os_varp;
3640
3641 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003642 return e_invalid_argument;
3643 // When changing the 'signcolumn' to or from 'number', recompute the
3644 // width of the number column if 'number' or 'relativenumber' is set.
3645 if (((*args->os_oldval.string == 'n' && args->os_oldval.string[1] == 'u')
3646 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
3647 && (curwin->w_p_nu || curwin->w_p_rnu))
3648 curwin->w_nrwidth_line_count = 0;
3649
3650 return NULL;
3651}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003652
3653 int
3654expand_set_signcolumn(optexpand_T *args, int *numMatches, char_u ***matches)
3655{
3656 return expand_set_opt_string(
3657 args,
3658 p_scl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003659 ARRAY_LENGTH(p_scl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003660 numMatches,
3661 matches);
3662}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003663#endif
3664
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003665#if defined(FEAT_SPELL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003666/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003667 * The 'spellcapcheck' option is changed.
3668 */
3669 char *
3670did_set_spellcapcheck(optset_T *args UNUSED)
3671{
3672 // compile the regexp program.
3673 return compile_cap_prog(curwin->w_s);
3674}
3675
3676/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003677 * The 'spellfile' option is changed.
3678 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003679 char *
3680did_set_spellfile(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003681{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003682 char_u **varp = (char_u **)args->os_varp;
3683
3684 if (!valid_spellfile(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003685 return e_invalid_argument;
3686
3687 // If there is a window for this buffer in which 'spell' is set load the
3688 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003689 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003690}
3691
3692/*
3693 * The 'spell' option is changed.
3694 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003695 char *
3696did_set_spelllang(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003697{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003698 char_u **varp = (char_u **)args->os_varp;
3699
3700 if (!valid_spelllang(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003701 return e_invalid_argument;
3702
3703 // If there is a window for this buffer in which 'spell' is set load the
3704 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003705 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003706}
3707
3708/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003709 * The 'spelloptions' option is changed.
3710 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003711 char *
3712did_set_spelloptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003713{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003714 char_u **varp = (char_u **)args->os_varp;
3715
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003716 if (**varp != NUL && STRCMP(*varp, "camel") != 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003717 return e_invalid_argument;
3718
3719 return NULL;
3720}
3721
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003722 int
3723expand_set_spelloptions(optexpand_T *args, int *numMatches, char_u ***matches)
3724{
3725 static char *(p_spo_values[]) = {"camel", NULL};
3726 return expand_set_opt_string(
3727 args,
3728 p_spo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003729 ARRAY_LENGTH(p_spo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003730 numMatches,
3731 matches);
3732}
3733
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003734/*
3735 * The 'spellsuggest' option is changed.
3736 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003737 char *
3738did_set_spellsuggest(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003739{
3740 if (spell_check_sps() != OK)
3741 return e_invalid_argument;
3742
3743 return NULL;
3744}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003745
3746 int
3747expand_set_spellsuggest(optexpand_T *args, int *numMatches, char_u ***matches)
3748{
3749 return expand_set_opt_string(
3750 args,
3751 p_sps_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003752 ARRAY_LENGTH(p_sps_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003753 numMatches,
3754 matches);
3755}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003756#endif
3757
3758/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003759 * The 'splitkeep' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003760 */
3761 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003762did_set_splitkeep(optset_T *args UNUSED)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003763{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003764 return did_set_opt_strings(p_spk, p_spk_values, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003765}
3766
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003767 int
3768expand_set_splitkeep(optexpand_T *args, int *numMatches, char_u ***matches)
3769{
3770 return expand_set_opt_string(
3771 args,
3772 p_spk_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003773 ARRAY_LENGTH(p_spk_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003774 numMatches,
3775 matches);
3776}
3777
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00003778#if defined(FEAT_STL_OPT) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003779/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003780 * The 'statusline' option is changed.
3781 */
3782 char *
3783did_set_statusline(optset_T *args)
3784{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003785 return parse_statustabline_rulerformat(args, FALSE);
3786}
3787#endif
3788
3789/*
3790 * The 'swapsync' option is changed.
3791 */
3792 char *
3793did_set_swapsync(optset_T *args UNUSED)
3794{
3795 return did_set_opt_strings(p_sws, p_sws_values, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003796}
3797
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003798 int
3799expand_set_swapsync(optexpand_T *args, int *numMatches, char_u ***matches)
3800{
3801 return expand_set_opt_string(
3802 args,
3803 p_sws_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003804 ARRAY_LENGTH(p_sws_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003805 numMatches,
3806 matches);
3807}
3808
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003809/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003810 * The 'switchbuf' option is changed.
3811 */
3812 char *
3813did_set_switchbuf(optset_T *args UNUSED)
3814{
3815 return did_set_opt_flags(p_swb, p_swb_values, &swb_flags, TRUE);
3816}
3817
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003818 int
3819expand_set_switchbuf(optexpand_T *args, int *numMatches, char_u ***matches)
3820{
3821 return expand_set_opt_string(
3822 args,
3823 p_swb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003824 ARRAY_LENGTH(p_swb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003825 numMatches,
3826 matches);
3827}
3828
LemonBoy5247b0b2024-07-12 19:30:58 +02003829/*
3830 * The 'tabclose' option is changed.
3831 */
3832 char *
3833did_set_tabclose(optset_T *args UNUSED)
3834{
3835 return did_set_opt_flags(p_tcl, p_tcl_values, &tcl_flags, TRUE);
3836}
3837
3838 int
3839expand_set_tabclose(optexpand_T *args, int *numMatches, char_u ***matches)
3840{
3841 return expand_set_opt_string(
3842 args,
3843 p_tcl_values,
3844 ARRAY_LENGTH(p_tcl_values) - 1,
3845 numMatches,
3846 matches);
3847}
3848
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003849#if defined(FEAT_STL_OPT) || defined(PROTO)
3850/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003851 * The 'tabline' option is changed.
3852 */
3853 char *
3854did_set_tabline(optset_T *args)
3855{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003856 return parse_statustabline_rulerformat(args, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003857}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003858#endif
3859
3860/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003861 * The 'tagcase' option is changed.
3862 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003863 char *
3864did_set_tagcase(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003865{
3866 unsigned int *flags;
3867 char_u *p;
3868
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003869 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003870 {
3871 p = curbuf->b_p_tc;
3872 flags = &curbuf->b_tc_flags;
3873 }
3874 else
3875 {
3876 p = p_tc;
3877 flags = &tc_flags;
3878 }
3879
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003880 if ((args->os_flags & OPT_LOCAL) && *p == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003881 // make the local value empty: use the global value
3882 *flags = 0;
3883 else if (*p == NUL
3884 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
3885 return e_invalid_argument;
3886
3887 return NULL;
3888}
3889
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003890 int
3891expand_set_tagcase(optexpand_T *args, int *numMatches, char_u ***matches)
3892{
3893 return expand_set_opt_string(
3894 args,
3895 p_tc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003896 ARRAY_LENGTH(p_tc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003897 numMatches,
3898 matches);
3899}
3900
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003901/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003902 * The 'term' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003903 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003904 char *
3905did_set_term(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003906{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003907 if (T_NAME[0] == NUL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003908 return e_cannot_set_term_to_empty_string;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003909#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003910 if (gui.in_use)
3911 return e_cannot_change_term_in_GUI;
3912 if (term_is_gui(T_NAME))
3913 return e_use_gui_to_start_GUI;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003914#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003915 if (set_termname(T_NAME) == FAIL)
3916 return e_not_found_in_termcap;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003917
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003918 // Screen colors may have changed.
3919 redraw_later_clear();
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003920
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003921 return NULL;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003922}
3923
3924/*
3925 * Some terminal option (t_xxx) is changed
3926 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003927 char *
3928did_set_term_option(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003929{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003930 char_u **varp = (char_u **)args->os_varp;
3931
3932 if (!full_screen)
3933 return NULL;
3934
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003935 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
3936 if (varp == &T_CCO)
3937 {
3938 int colors = atoi((char *)T_CCO);
3939
3940 // Only reinitialize colors if t_Co value has really changed to
3941 // avoid expensive reload of colorscheme if t_Co is set to the
3942 // same value multiple times.
3943 if (colors != t_colors)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003944 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003945 t_colors = colors;
3946 if (t_colors <= 1)
3947 {
3948 vim_free(T_CCO);
3949 T_CCO = empty_option;
3950 }
3951#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
3952 if (is_term_win32())
3953 {
3954 swap_tcap();
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003955 args->os_did_swaptcap = TRUE;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003956 }
3957#endif
3958 // We now have a different color setup, initialize it again.
3959 init_highlight(TRUE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003960 }
3961 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003962 ttest(FALSE);
3963 if (varp == &T_ME)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003964 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003965 out_str(T_ME);
3966 redraw_later(UPD_CLEAR);
3967#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3968 // Since t_me has been set, this probably means that the user
3969 // wants to use this as default colors. Need to reset default
3970 // background/foreground colors.
3971# ifdef VIMDLL
3972 if (!gui.in_use && !gui.starting)
3973# endif
3974 mch_set_normal_colors();
3975#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003976 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003977 if (varp == &T_BE && termcap_active)
3978 {
3979 MAY_WANT_TO_LOG_THIS;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003980
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003981 if (*T_BE == NUL)
3982 // When clearing t_BE we assume the user no longer wants
3983 // bracketed paste, thus disable it by writing t_BD.
3984 out_str(T_BD);
3985 else
3986 out_str(T_BE);
3987 }
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003988
3989 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003990}
3991
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003992#if defined(FEAT_TERMINAL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003993/*
3994 * The 'termwinkey' option is changed.
3995 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003996 char *
Milly94606f72024-10-22 22:07:52 +02003997did_set_termwinkey(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003998{
Milly94606f72024-10-22 22:07:52 +02003999 char_u **varp = (char_u **)args->os_varp;
4000
4001 if ((*varp)[0] != NUL && string_to_key(*varp, TRUE) == 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004002 return e_invalid_argument;
4003
4004 return NULL;
4005}
4006
4007/*
4008 * The 'termwinsize' option is changed.
4009 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004010 char *
Milly8be10aa2024-10-22 22:01:46 +02004011did_set_termwinsize(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004012{
Milly8be10aa2024-10-22 22:01:46 +02004013 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004014 char_u *p;
4015
Milly8be10aa2024-10-22 22:01:46 +02004016 if ((*varp)[0] == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004017 return NULL;
4018
Milly8be10aa2024-10-22 22:01:46 +02004019 p = skipdigits(*varp);
4020 if (p == *varp || (*p != 'x' && *p != '*') || *skipdigits(p + 1) != NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004021 return e_invalid_argument;
4022
4023 return NULL;
4024}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00004025
4026# if defined(MSWIN) || defined(PROTO)
4027/*
4028 * The 'termwintype' option is changed.
4029 */
4030 char *
4031did_set_termwintype(optset_T *args UNUSED)
4032{
4033 return did_set_opt_strings(p_twt, p_twt_values, FALSE);
4034}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004035
4036 int
4037expand_set_termwintype(optexpand_T *args, int *numMatches, char_u ***matches)
4038{
4039 return expand_set_opt_string(
4040 args,
4041 p_twt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004042 ARRAY_LENGTH(p_twt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004043 numMatches,
4044 matches);
4045}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00004046# endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004047#endif
4048
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004049/*
4050 * The 'titlestring' option is changed.
4051 */
4052 char *
4053did_set_titlestring(optset_T *args)
4054{
4055 int flagval = 0;
4056
4057#ifdef FEAT_STL_OPT
4058 flagval = STL_IN_TITLE;
4059#endif
4060 return parse_titleiconstring(args, flagval);
4061}
4062
4063#if (defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)) || defined(PROTO)
4064/*
4065 * The 'toolbar' option is changed.
4066 */
4067 char *
4068did_set_toolbar(optset_T *args UNUSED)
4069{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004070 if (opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags,
4071 TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004072 return e_invalid_argument;
4073
4074 out_flush();
4075 gui_mch_show_toolbar((toolbar_flags &
4076 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4077 return NULL;
4078}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004079
4080 int
4081expand_set_toolbar(optexpand_T *args, int *numMatches, char_u ***matches)
4082{
4083 return expand_set_opt_string(
4084 args,
4085 p_toolbar_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004086 ARRAY_LENGTH(p_toolbar_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004087 numMatches,
4088 matches);
4089}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004090#endif
4091
4092#if (defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)) || defined(PROTO)
4093/*
4094 * The 'toolbariconsize' option is changed. GTK+ 2 only.
4095 */
4096 char *
4097did_set_toolbariconsize(optset_T *args UNUSED)
4098{
4099 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
4100 return e_invalid_argument;
4101
4102 out_flush();
4103 gui_mch_show_toolbar((toolbar_flags &
4104 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4105 return NULL;
4106}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004107
4108 int
4109expand_set_toolbariconsize(optexpand_T *args, int *numMatches, char_u ***matches)
4110{
4111 return expand_set_opt_string(
4112 args,
4113 p_tbis_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004114 ARRAY_LENGTH(p_tbis_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004115 numMatches,
4116 matches);
4117}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004118#endif
4119
4120#if defined(UNIX) || defined(VMS) || defined(PROTO)
4121/*
4122 * The 'ttymouse' option is changed.
4123 */
4124 char *
4125did_set_ttymouse(optset_T *args UNUSED)
4126{
4127 char *errmsg = NULL;
4128
4129 // Switch the mouse off before changing the escape sequences used for
4130 // that.
4131 mch_setmouse(FALSE);
4132 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
4133 errmsg = e_invalid_argument;
4134 else
4135 check_mouse_termcode();
4136 if (termcap_active)
4137 setmouse(); // may switch it on again
4138
4139 return errmsg;
4140}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004141
4142 int
4143expand_set_ttymouse(optexpand_T *args, int *numMatches, char_u ***matches)
4144{
4145 return expand_set_opt_string(
4146 args,
4147 p_ttym_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004148 ARRAY_LENGTH(p_ttym_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004149 numMatches,
4150 matches);
4151}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004152#endif
4153
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004154#if defined(FEAT_VARTABS) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004155/*
4156 * The 'varsofttabstop' option is changed.
4157 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004158 char *
4159did_set_varsofttabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004160{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004161 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004162 char_u *cp;
4163
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004164 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004165 VIM_CLEAR(curbuf->b_p_vsts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004166 else
4167 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004168 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004169 {
4170 if (vim_isdigit(*cp))
4171 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004172 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004173 continue;
4174 return e_invalid_argument;
4175 }
4176
4177 int *oldarray = curbuf->b_p_vsts_array;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004178 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004179 {
4180 if (oldarray)
4181 vim_free(oldarray);
4182 }
4183 else
4184 return e_invalid_argument;
4185 }
4186
4187 return NULL;
4188}
4189
4190/*
4191 * The 'vartabstop' option is changed.
4192 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004193 char *
4194did_set_vartabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004195{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004196 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004197 char_u *cp;
4198
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004199 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004200 VIM_CLEAR(curbuf->b_p_vts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004201 else
4202 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004203 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004204 {
4205 if (vim_isdigit(*cp))
4206 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004207 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004208 continue;
4209 return e_invalid_argument;
4210 }
4211
4212 int *oldarray = curbuf->b_p_vts_array;
4213
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004214 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004215 {
4216 vim_free(oldarray);
4217# ifdef FEAT_FOLDING
4218 if (foldmethodIsIndent(curwin))
4219 foldUpdateAll(curwin);
4220# endif
4221 }
4222 else
4223 return e_invalid_argument;
4224 }
4225
4226 return NULL;
4227}
4228#endif
4229
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004230/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004231 * The 'verbosefile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004232 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004233 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004234did_set_verbosefile(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004235{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004236 verbose_stop();
4237 if (*p_vfile != NUL && verbose_open() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004238 return e_invalid_argument;
4239
4240 return NULL;
4241}
4242
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004243#if defined(FEAT_SESSION) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004244/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004245 * The 'viewoptions' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004246 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004247 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004248did_set_viewoptions(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004249{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004250 return did_set_opt_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004251}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004252#endif
4253
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004254#if defined(FEAT_VIMINFO) || defined(PROTO)
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004255/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004256 * The 'viminfo' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004257 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004258 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004259did_set_viminfo(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004260{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004261 char_u *s;
4262 char *errmsg = NULL;
4263
4264 for (s = p_viminfo; *s;)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004265 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004266 // Check it's a valid character
4267 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
4268 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01004269 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004270 break;
4271 }
4272 if (*s == 'n') // name is always last one
4273 break;
4274 else if (*s == 'r') // skip until next ','
4275 {
4276 while (*++s && *s != ',')
4277 ;
4278 }
4279 else if (*s == '%')
4280 {
4281 // optional number
4282 while (vim_isdigit(*++s))
4283 ;
4284 }
4285 else if (*s == '!' || *s == 'h' || *s == 'c')
4286 ++s; // no extra chars
4287 else // must have a number
4288 {
4289 while (vim_isdigit(*++s))
4290 ;
4291
4292 if (!VIM_ISDIGIT(*(s - 1)))
4293 {
4294 if (args->os_errbuf != NULL)
4295 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01004296 vim_snprintf(args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004297 _(e_missing_number_after_angle_str_angle),
4298 transchar_byte(*(s - 1)));
4299 errmsg = args->os_errbuf;
4300 }
4301 else
4302 errmsg = "";
4303 break;
4304 }
4305 }
4306 if (*s == ',')
4307 ++s;
4308 else if (*s)
4309 {
4310 if (args->os_errbuf != NULL)
4311 errmsg = e_missing_comma;
4312 else
4313 errmsg = "";
4314 break;
4315 }
4316 }
4317 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
4318 errmsg = e_must_specify_a_value;
4319
4320 return errmsg;
4321}
4322#endif
4323
4324/*
4325 * The 'virtualedit' option is changed.
4326 */
4327 char *
4328did_set_virtualedit(optset_T *args)
4329{
4330 char_u *ve = p_ve;
4331 unsigned int *flags = &ve_flags;
4332
4333 if (args->os_flags & OPT_LOCAL)
4334 {
4335 ve = curwin->w_p_ve;
4336 flags = &curwin->w_ve_flags;
4337 }
4338
4339 if ((args->os_flags & OPT_LOCAL) && *ve == NUL)
4340 // make the local value empty: use the global value
4341 *flags = 0;
4342 else
4343 {
4344 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
4345 return e_invalid_argument;
4346 else if (STRCMP(ve, args->os_oldval.string) != 0)
4347 {
4348 // Recompute cursor position in case the new 've' setting
4349 // changes something.
4350 validate_virtcol();
4351 coladvance(curwin->w_virtcol);
4352 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004353 }
4354
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004355 return NULL;
4356}
4357
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004358 int
4359expand_set_virtualedit(optexpand_T *args, int *numMatches, char_u ***matches)
4360{
4361 return expand_set_opt_string(
4362 args,
4363 p_ve_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004364 ARRAY_LENGTH(p_ve_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004365 numMatches,
4366 matches);
4367}
4368
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004369/*
4370 * The 'whichwrap' option is changed.
4371 */
4372 char *
4373did_set_whichwrap(optset_T *args)
4374{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004375 char_u **varp = (char_u **)args->os_varp;
4376
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004377 // Add ',' to the list flags because 'whichwrap' is a flag
4378 // list that is comma-separated.
Christian Brabandtb39b2402023-11-29 11:34:05 +01004379 return did_set_option_listflag(*varp, (char_u *)(WW_ALL ","),
4380 args->os_errbuf, args->os_errbuflen);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004381}
4382
4383 int
4384expand_set_whichwrap(optexpand_T *args, int *numMatches, char_u ***matches)
4385{
4386 return expand_set_opt_listflag(args, (char_u*)WW_ALL, numMatches, matches);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004387}
4388
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004389/*
4390 * The 'wildmode' option is changed.
4391 */
4392 char *
4393did_set_wildmode(optset_T *args UNUSED)
4394{
4395 if (check_opt_wim() == FAIL)
4396 return e_invalid_argument;
4397 return NULL;
4398}
4399
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004400 int
4401expand_set_wildmode(optexpand_T *args, int *numMatches, char_u ***matches)
4402{
4403 return expand_set_opt_string(
4404 args,
4405 p_wim_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004406 ARRAY_LENGTH(p_wim_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004407 numMatches,
4408 matches);
4409}
4410
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004411/*
4412 * The 'wildoptions' option is changed.
4413 */
4414 char *
4415did_set_wildoptions(optset_T *args UNUSED)
4416{
4417 return did_set_opt_strings(p_wop, p_wop_values, TRUE);
4418}
4419
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004420 int
4421expand_set_wildoptions(optexpand_T *args, int *numMatches, char_u ***matches)
4422{
4423 return expand_set_opt_string(
4424 args,
4425 p_wop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004426 ARRAY_LENGTH(p_wop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004427 numMatches,
4428 matches);
4429}
4430
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004431#if defined(FEAT_WAK) || defined(PROTO)
4432/*
4433 * The 'winaltkeys' option is changed.
4434 */
4435 char *
4436did_set_winaltkeys(optset_T *args UNUSED)
4437{
4438 char *errmsg = NULL;
4439
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004440 if (*p_wak == NUL || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004441 errmsg = e_invalid_argument;
4442# ifdef FEAT_MENU
4443# if defined(FEAT_GUI_MOTIF)
4444 else if (gui.in_use)
4445 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4446# elif defined(FEAT_GUI_GTK)
4447 else if (gui.in_use)
4448 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4449# endif
4450# endif
4451 return errmsg;
4452}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004453
4454 int
4455expand_set_winaltkeys(optexpand_T *args, int *numMatches, char_u ***matches)
4456{
4457 return expand_set_opt_string(
4458 args,
4459 p_wak_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004460 ARRAY_LENGTH(p_wak_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004461 numMatches,
4462 matches);
4463}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004464#endif
4465
4466/*
4467 * The 'wincolor' option is changed.
4468 */
4469 char *
4470did_set_wincolor(optset_T *args UNUSED)
4471{
4472#ifdef FEAT_TERMINAL
4473 term_update_wincolor(curwin);
4474#endif
4475 return NULL;
4476}
4477
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004478 int
4479expand_set_wincolor(optexpand_T *args, int *numMatches, char_u ***matches)
4480{
4481 return expand_set_opt_generic(
4482 args,
4483 get_highlight_name,
4484 numMatches,
4485 matches);
4486}
4487
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004488#ifdef FEAT_SYN_HL
4489/*
4490 * When the 'syntax' option is set, load the syntax of that name.
4491 */
4492 static void
4493do_syntax_autocmd(int value_changed)
4494{
4495 static int syn_recursive = 0;
4496
4497 ++syn_recursive;
4498 // Only pass TRUE for "force" when the value changed or not used
4499 // recursively, to avoid endless recurrence.
4500 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
4501 value_changed || syn_recursive == 1, curbuf);
4502 curbuf->b_flags |= BF_SYN_SET;
4503 --syn_recursive;
4504}
4505#endif
4506
4507/*
4508 * When the 'filetype' option is set, trigger the FileType autocommand.
4509 */
4510 static void
4511do_filetype_autocmd(char_u **varp, int opt_flags, int value_changed)
4512{
4513 // Skip this when called from a modeline and the filetype was already set
4514 // to this value.
4515 if ((opt_flags & OPT_MODELINE) && !value_changed)
4516 return;
4517
4518 static int ft_recursive = 0;
4519 int secure_save = secure;
4520
4521 // Reset the secure flag, since the value of 'filetype' has
4522 // been checked to be safe.
4523 secure = 0;
4524
4525 ++ft_recursive;
zeertzjq5bf6c212024-03-31 18:41:27 +02004526 curbuf->b_did_filetype = TRUE;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004527 // Only pass TRUE for "force" when the value changed or not
4528 // used recursively, to avoid endless recurrence.
4529 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
4530 value_changed || ft_recursive == 1, curbuf);
4531 --ft_recursive;
4532 // Just in case the old "curbuf" is now invalid.
4533 if (varp != &(curbuf->b_p_ft))
4534 varp = NULL;
4535
4536 secure = secure_save;
4537}
4538
4539#ifdef FEAT_SPELL
4540/*
4541 * When the 'spelllang' option is set, source the spell/LANG.vim file in
4542 * 'runtimepath'.
4543 */
4544 static void
4545do_spelllang_source(void)
4546{
4547 char_u fname[200];
4548 char_u *p;
4549 char_u *q = curwin->w_s->b_p_spl;
4550
4551 // Skip the first name if it is "cjk".
4552 if (STRNCMP(q, "cjk,", 4) == 0)
4553 q += 4;
4554
4555 // They could set 'spellcapcheck' depending on the language. Use the first
4556 // name in 'spelllang' up to '_region' or '.encoding'.
4557 for (p = q; *p != NUL; ++p)
4558 if (!ASCII_ISALNUM(*p) && *p != '-')
4559 break;
4560 if (p > q)
4561 {
4562 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
4563 (int)(p - q), q);
4564 source_runtime(fname, DIP_ALL);
4565 }
4566}
4567#endif
4568
4569/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004570 * Handle string options that need some action to perform when changed.
zeertzjqf6782732022-07-27 18:26:03 +01004571 * The new value must be allocated.
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004572 * Returns NULL for success, or an untranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +02004573 */
4574 char *
4575did_set_string_option(
4576 int opt_idx, // index in options[] table
4577 char_u **varp, // pointer to the option variable
Bram Moolenaardac13472019-09-16 21:06:21 +02004578 char_u *oldval, // previous value of the option
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004579 char_u *value, // new value of the option
Bram Moolenaardac13472019-09-16 21:06:21 +02004580 char *errbuf, // buffer for errors, or NULL
Mike Williams620f0112023-12-05 15:36:06 +01004581 size_t errbuflen, // length of error buffer
Bram Moolenaardac13472019-09-16 21:06:21 +02004582 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004583 set_op_T op, // OP_ADDING/OP_PREPENDING/OP_REMOVING
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004584 int *value_checked) // value was checked to be safe, no
Bram Moolenaardac13472019-09-16 21:06:21 +02004585 // need to set P_INSECURE
4586{
4587 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +02004588 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004589 opt_did_set_cb_T did_set_cb = get_option_did_set_cb(opt_idx);
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004590 optset_T args;
4591
4592 // 'ttytype' is an alias for 'term'. Both 'term' and 'ttytype' point to
4593 // T_NAME. If 'term' or 'ttytype' is modified, then use the index for the
4594 // 'term' option. Only set the P_ALLOCED flag on 'term'.
4595 if (varp == &T_NAME)
4596 {
4597 opt_idx = findoption((char_u *)"term");
4598 if (opt_idx >= 0)
4599 {
4600 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
4601 did_set_cb = get_option_did_set_cb(opt_idx);
4602 }
4603 }
4604
4605 CLEAR_FIELD(args);
Bram Moolenaardac13472019-09-16 21:06:21 +02004606
Bram Moolenaardac13472019-09-16 21:06:21 +02004607 // Disallow changing some options from secure mode
4608 if ((secure
4609#ifdef HAVE_SANDBOX
4610 || sandbox != 0
4611#endif
4612 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00004613 errmsg = e_not_allowed_here;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004614 // Check for a "normal" directory or file name in some options.
4615 else if (check_illegal_path_names(opt_idx, varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004616 errmsg = e_invalid_argument;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004617 else if (did_set_cb != NULL)
4618 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004619 args.os_varp = (char_u *)varp;
4620 args.os_idx = opt_idx;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004621 args.os_flags = opt_flags;
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004622 args.os_op = op;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004623 args.os_oldval.string = oldval;
4624 args.os_newval.string = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004625 args.os_errbuf = errbuf;
Christian Brabandtb39b2402023-11-29 11:34:05 +01004626 args.os_errbuflen = errbuflen;
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004627 // Invoke the option specific callback function to validate and apply
4628 // the new option value.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004629 errmsg = did_set_cb(&args);
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004630
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004631 // The 'keymap', 'filetype' and 'syntax' option callback functions
4632 // may change the os_value_checked field.
4633 *value_checked = args.os_value_checked;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004634 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004635
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004636 // If an error is detected, restore the previous value.
Bram Moolenaardac13472019-09-16 21:06:21 +02004637 if (errmsg != NULL)
4638 {
zeertzjqf6782732022-07-27 18:26:03 +01004639 free_string_option(*varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02004640 *varp = oldval;
4641 // When resetting some values, need to act on it.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004642 if (args.os_restore_chartab)
Bram Moolenaardac13472019-09-16 21:06:21 +02004643 (void)init_chartab();
4644 if (varp == &p_hl)
4645 (void)highlight_changed();
4646 }
4647 else
4648 {
4649#ifdef FEAT_EVAL
4650 // Remember where the option was set.
4651 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4652#endif
4653 // Free string options that are in allocated memory.
4654 // Use "free_oldval", because recursiveness may change the flags under
4655 // our fingers (esp. init_highlight()).
4656 if (free_oldval)
4657 free_string_option(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01004658 set_option_flag(opt_idx, P_ALLOCED);
Bram Moolenaardac13472019-09-16 21:06:21 +02004659
4660 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
4661 && is_global_local_option(opt_idx))
4662 {
4663 // global option with local value set to use global value; free
4664 // the local value and make it empty
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004665 char_u *p = get_option_varp_scope(opt_idx, OPT_LOCAL);
Bram Moolenaardac13472019-09-16 21:06:21 +02004666 free_string_option(*(char_u **)p);
4667 *(char_u **)p = empty_option;
4668 }
4669
4670 // May set global value for local option.
4671 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
4672 set_string_option_global(opt_idx, varp);
4673
4674 // Trigger the autocommand only after setting the flags.
4675#ifdef FEAT_SYN_HL
Bram Moolenaardac13472019-09-16 21:06:21 +02004676 if (varp == &(curbuf->b_p_syn))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004677 do_syntax_autocmd(args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004678#endif
4679 else if (varp == &(curbuf->b_p_ft))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004680 do_filetype_autocmd(varp, opt_flags, args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004681#ifdef FEAT_SPELL
4682 if (varp == &(curwin->w_s->b_p_spl))
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004683 do_spelllang_source();
Bram Moolenaardac13472019-09-16 21:06:21 +02004684#endif
4685 }
4686
Bram Moolenaardac13472019-09-16 21:06:21 +02004687 if (varp == &p_mouse)
4688 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004689 if (*p_mouse == NUL)
4690 mch_setmouse(FALSE); // switch mouse off
4691 else
Bram Moolenaardac13472019-09-16 21:06:21 +02004692 setmouse(); // in case 'mouse' changed
4693 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004694
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004695 if (varp == &p_rtp)
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004696 {
4697 export_myvimdir();
4698#if defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004699 update_package_paths_in_lua();
4700#endif
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004701 }
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004702
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004703#if defined(FEAT_LINEBREAK)
4704 // Changing Formatlistpattern when briopt includes the list setting:
4705 // redraw
4706 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
4707 && curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004708 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004709#endif
4710
Bram Moolenaardac13472019-09-16 21:06:21 +02004711 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004712 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0
4713 && (get_option_flags(opt_idx) & P_HLONLY) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02004714 curwin->w_set_curswant = TRUE;
4715
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004716 if ((opt_flags & OPT_NO_REDRAW) == 0)
4717 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004718#ifdef FEAT_GUI
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004719 // set when changing an option that only requires a redraw in the GUI
4720 int redraw_gui_only = FALSE;
4721
4722 if (varp == &p_go // 'guioptions'
4723 || varp == &p_guifont // 'guifont'
4724# ifdef FEAT_GUI_TABLINE
4725 || varp == &p_gtl // 'guitablabel'
4726 || varp == &p_gtt // 'guitabtooltip'
4727# endif
4728# ifdef FEAT_XFONTSET
4729 || varp == &p_guifontset // 'guifontset'
4730# endif
4731 || varp == &p_guifontwide // 'guifontwide'
Erik S. V. Jansson2f026382024-02-26 22:23:05 +01004732# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004733 || varp == &p_guiligatures // 'guiligatures'
4734# endif
4735 )
4736 redraw_gui_only = TRUE;
4737
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004738 // check redraw when it's not a GUI option or the GUI is active.
4739 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02004740#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004741 check_redraw(get_option_flags(opt_idx));
4742 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004743
4744#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004745 if (args.os_did_swaptcap)
Bram Moolenaardac13472019-09-16 21:06:21 +02004746 {
4747 set_termname((char_u *)"win32");
4748 init_highlight(TRUE, FALSE);
4749 }
4750#endif
4751
4752 return errmsg;
4753}
4754
4755/*
4756 * Check an option that can be a range of string values.
4757 *
4758 * Return OK for correct value, FAIL otherwise.
4759 * Empty is always OK.
4760 */
4761 static int
4762check_opt_strings(
4763 char_u *val,
4764 char **values,
4765 int list) // when TRUE: accept a list of values
4766{
4767 return opt_strings_flags(val, values, NULL, list);
4768}
4769
4770/*
4771 * Handle an option that can be a range of string values.
4772 * Set a flag in "*flagp" for each string present.
4773 *
4774 * Return OK for correct value, FAIL otherwise.
4775 * Empty is always OK.
4776 */
4777 static int
4778opt_strings_flags(
4779 char_u *val, // new value
4780 char **values, // array of valid string values
4781 unsigned *flagp,
4782 int list) // when TRUE: accept a list of values
4783{
4784 int i;
4785 int len;
4786 unsigned new_flags = 0;
4787
4788 while (*val)
4789 {
4790 for (i = 0; ; ++i)
4791 {
4792 if (values[i] == NULL) // val not found in values[]
4793 return FAIL;
4794
4795 len = (int)STRLEN(values[i]);
4796 if (STRNCMP(values[i], val, len) == 0
4797 && ((list && val[len] == ',') || val[len] == NUL))
4798 {
4799 val += len + (val[len] == ',');
4800 new_flags |= (1 << i);
4801 break; // check next item in val list
4802 }
4803 }
4804 }
4805 if (flagp != NULL)
4806 *flagp = new_flags;
4807
4808 return OK;
4809}
4810
4811/*
4812 * return OK if "p" is a valid fileformat name, FAIL otherwise.
4813 */
4814 int
4815check_ff_value(char_u *p)
4816{
4817 return check_opt_strings(p, p_ff_values, FALSE);
4818}
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004819
4820/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004821 * Save the actual shortmess Flags and clear them temporarily to avoid that
4822 * file messages overwrites any output from the following commands.
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004823 *
4824 * Caller must make sure to first call save_clear_shm_value() and then
4825 * restore_shm_value() exactly the same number of times.
4826 */
4827 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004828save_clear_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004829{
4830 if (STRLEN(p_shm) >= SHM_LEN)
4831 {
4832 iemsg(e_internal_error_shortmess_too_long);
4833 return;
4834 }
4835
4836 if (++set_shm_recursive == 1)
4837 {
4838 STRCPY(shm_buf, p_shm);
4839 set_option_value_give_err((char_u *)"shm", 0L, (char_u *)"", 0);
4840 }
4841}
4842
4843/*
4844 * Restore the shortmess Flags set from the save_clear_shm_value() function.
4845 */
4846 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004847restore_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004848{
4849 if (--set_shm_recursive == 0)
4850 {
4851 set_option_value_give_err((char_u *)"shm", 0L, shm_buf, 0);
4852 vim_memset(shm_buf, 0, SHM_LEN);
4853 }
4854}
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004855
4856/*
4857 * Export the environment variable $MYVIMDIR to the first item in runtimepath
4858 */
Christian Brabandt1a741d32025-03-01 16:30:33 +01004859 void
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004860export_myvimdir()
4861{
4862 int dofree = FALSE;
4863 char_u *p;
4864 char_u *q = p_rtp;
4865 char_u *buf = alloc(MAXPATHL);
4866
4867 if (buf == NULL)
4868 return;
4869
4870 (void)copy_option_part(&q, buf, MAXPATHL, ",");
4871
4872 p = vim_getenv((char_u *)"MYVIMDIR", &dofree);
4873
4874 if (p == NULL || STRCMP(p, buf) != 0)
4875 {
4876 add_pathsep(buf);
4877#ifdef MSWIN
4878 // normalize path separators
4879 for (q = buf; *q != NUL; q++)
4880 if (*q == '/')
4881 *q = '\\';
4882#endif
4883 vim_setenv((char_u *)"MYVIMDIR", buf);
4884 }
4885 if (dofree)
4886 vim_free(p);
4887 vim_free(buf);
4888}