blob: f60957eb2315a96f2e168d802a73bb95869063f4 [file] [log] [blame]
Bram Moolenaardac13472019-09-16 21:06:21 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * optionstr.c: Functions related to string options
12 */
13
14#include "vim.h"
15
Christian Brabandt9aee8ec2022-12-16 16:41:23 +000016static char_u shm_buf[SHM_LEN];
17static int set_shm_recursive = 0;
18
Bram Moolenaardac13472019-09-16 21:06:21 +020019static char *(p_ambw_values[]) = {"single", "double", NULL};
20static char *(p_bg_values[]) = {"light", "dark", NULL};
21static char *(p_bkc_values[]) = {"yes", "auto", "no", "breaksymlink", "breakhardlink", NULL};
22static char *(p_bo_values[]) = {"all", "backspace", "cursor", "complete",
23 "copy", "ctrlg", "error", "esc", "ex",
24 "hangul", "insertmode", "lang", "mess",
25 "showmatch", "operator", "register", "shell",
LemonBoy77771d32022-04-13 11:47:25 +010026 "spell", "term", "wildmode", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020027#if defined(FEAT_LINEBREAK)
28// Note: Keep this in sync with briopt_check()
29static char *(p_briopt_values[]) = {"shift:", "min:", "sbr", "list:", "column:", NULL};
30#endif
31#if defined(FEAT_DIFF)
32// Note: Keep this in sync with diffopt_changed()
Yee Cheng Chin9943d472025-03-26 19:41:02 +010033static char *(p_dip_values[]) = {"filler", "context:", "iblank", "icase", "iwhite", "iwhiteall", "iwhiteeol", "horizontal", "vertical", "closeoff", "hiddenoff", "foldcolumn:", "followwrap", "internal", "indent-heuristic", "algorithm:", "inline:", "linematch:", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020034static char *(p_dip_algorithm_values[]) = {"myers", "minimal", "patience", "histogram", NULL};
Yee Cheng Chin9943d472025-03-26 19:41:02 +010035static char *(p_dip_inline_values[]) = {"none", "simple", "char", "word", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020036#endif
distobs25ac6d62024-07-06 17:50:09 +020037static char *(p_nf_values[]) = {"bin", "octal", "hex", "alpha", "unsigned", "blank", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020038static char *(p_ff_values[]) = {FF_UNIX, FF_DOS, FF_MAC, NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020039#ifdef FEAT_CLIPBOARD
40// Note: Keep this in sync with did_set_clipboard()
41static char *(p_cb_values[]) = {"unnamed", "unnamedplus", "autoselect", "autoselectplus", "autoselectml", "html", "exclude:", NULL};
42#endif
Bram Moolenaardac13472019-09-16 21:06:21 +020043#ifdef FEAT_CRYPT
Christian Brabandtf573c6e2021-06-20 14:02:16 +020044static char *(p_cm_values[]) = {"zip", "blowfish", "blowfish2",
45 # ifdef FEAT_SODIUM
Christian Brabandtaae58342023-04-23 17:50:22 +010046 "xchacha20", "xchacha20v2",
Christian Brabandtf573c6e2021-06-20 14:02:16 +020047 # endif
48 NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020049#endif
50static char *(p_cmp_values[]) = {"internal", "keepascii", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020051#ifdef FEAT_SYN_HL
52// Note: Keep this in sync with fill_culopt_flags()
53static char *(p_culopt_values[]) = {"line", "screenline", "number", "both", NULL};
54#endif
Bram Moolenaardac13472019-09-16 21:06:21 +020055static char *(p_dy_values[]) = {"lastline", "truncate", "uhex", NULL};
Yegappan Lakshmanan87018252023-09-20 20:20:04 +020056static char *(p_jop_values[]) = {"stack", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020057#ifdef FEAT_FOLDING
58static char *(p_fdo_values[]) = {"all", "block", "hor", "mark", "percent",
59 "quickfix", "search", "tag", "insert",
60 "undo", "jump", NULL};
61#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +020062// Note: Keep this in sync with match_keyprotocol()
63static char *(p_kpc_protocol_values[]) = {"none", "mok2", "kitty", NULL};
64#ifdef FEAT_PROP_POPUP
65// Note: Keep this in sync with parse_popup_option()
66static char *(p_popup_option_values[]) = {"height:", "width:", "highlight:", "border:", "align:", NULL};
67static char *(p_popup_option_border_values[]) = {"on", "off", NULL};
68static char *(p_popup_option_align_values[]) = {"item", "menu", NULL};
69#endif
70#if defined(FEAT_SPELL)
71// Note: Keep this in sync with spell_check_sps()
72static char *(p_sps_values[]) = {"best", "fast", "double", "expr:", "file:", "timeout:", NULL};
73#endif
Bram Moolenaardac13472019-09-16 21:06:21 +020074#ifdef FEAT_SESSION
Bram Moolenaar635bd602021-04-16 19:58:22 +020075// Also used for 'viewoptions'! Keep in sync with SSOP_ flags.
Bram Moolenaardac13472019-09-16 21:06:21 +020076static char *(p_ssop_values[]) = {"buffers", "winpos", "resize", "winsize",
77 "localoptions", "options", "help", "blank", "globals", "slash", "unix",
Bram Moolenaar635bd602021-04-16 19:58:22 +020078 "sesdir", "curdir", "folds", "cursor", "tabpages", "terminal", "skiprtp",
79 NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020080#endif
Bram Moolenaar539aa6b2019-11-17 18:09:38 +010081// Keep in sync with SWB_ flags in option.h
82static char *(p_swb_values[]) = {"useopen", "usetab", "split", "newtab", "vsplit", "uselast", NULL};
Luuk van Baal13ece2a2022-10-03 15:28:08 +010083static char *(p_spk_values[]) = {"cursor", "screen", "topline", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020084static char *(p_tc_values[]) = {"followic", "ignore", "match", "followscs", "smart", NULL};
LemonBoy5247b0b2024-07-12 19:30:58 +020085// Keep in sync with TCL_ flags in option.h
86static char *(p_tcl_values[]) = {"left", "uselast", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020087#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
88static char *(p_toolbar_values[]) = {"text", "icons", "tooltips", "horiz", NULL};
89#endif
90#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
91static char *(p_tbis_values[]) = {"tiny", "small", "medium", "large", "huge", "giant", NULL};
92#endif
Bram Moolenaara1cb1d12019-10-17 23:00:07 +020093#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +020094static char *(p_ttym_values[]) = {"xterm", "xterm2", "dec", "netterm", "jsbterm", "pterm", "urxvt", "sgr", NULL};
95#endif
Gary Johnson53ba05b2021-07-26 22:19:10 +020096static char *(p_ve_values[]) = {"block", "insert", "all", "onemore", "none", "NONE", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020097// Note: Keep this in sync with check_opt_wim()
Girish Palya2bacc3e2025-03-02 22:55:57 +010098static char *(p_wim_values[]) = {"full", "longest", "list", "lastused", "noselect", NULL};
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000099static char *(p_wop_values[]) = {"fuzzy", "tagfile", "pum", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200100#ifdef FEAT_WAK
101static char *(p_wak_values[]) = {"yes", "menu", "no", NULL};
102#endif
103static char *(p_mousem_values[]) = {"extend", "popup", "popup_setpos", "mac", NULL};
104static char *(p_sel_values[]) = {"inclusive", "exclusive", "old", NULL};
105static char *(p_slm_values[]) = {"mouse", "key", "cmd", NULL};
106static char *(p_km_values[]) = {"startsel", "stopsel", NULL};
107#ifdef FEAT_BROWSE
108static char *(p_bsdir_values[]) = {"current", "last", "buffer", NULL};
109#endif
110static char *(p_scbopt_values[]) = {"ver", "hor", "jump", NULL};
111static char *(p_debug_values[]) = {"msg", "throw", "beep", NULL};
112static char *(p_ead_values[]) = {"both", "ver", "hor", NULL};
113static char *(p_buftype_values[]) = {"nofile", "nowrite", "quickfix", "help", "terminal", "acwrite", "prompt", "popup", NULL};
114static char *(p_bufhidden_values[]) = {"hide", "unload", "delete", "wipe", NULL};
Bram Moolenaaraa0489e2020-04-17 19:41:21 +0200115static char *(p_bs_values[]) = {"indent", "eol", "start", "nostop", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200116#ifdef FEAT_FOLDING
117static char *(p_fdm_values[]) = {"manual", "expr", "marker", "indent", "syntax",
118# ifdef FEAT_DIFF
119 "diff",
120# endif
121 NULL};
122static char *(p_fcl_values[]) = {"all", NULL};
123#endif
glepnirf31cfa22025-03-06 21:59:13 +0100124static char *(p_cfc_values[]) = {"keyword", "files", "whole_line", NULL};
Girish Palyab1565882025-04-15 20:16:00 +0200125static char *(p_cot_values[]) = {"menu", "menuone", "longest", "preview", "popup", "popuphidden", "noinsert", "noselect", "fuzzy", "nosort", "preinsert", "nearest", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200126#ifdef BACKSLASH_IN_FILENAME
127static char *(p_csl_values[]) = {"slash", "backslash", NULL};
128#endif
129#ifdef FEAT_SIGNS
130static char *(p_scl_values[]) = {"yes", "no", "auto", "number", NULL};
131#endif
132#if defined(MSWIN) && defined(FEAT_TERMINAL)
133static char *(p_twt_values[]) = {"winpty", "conpty", "", NULL};
134#endif
Luuk van Baalba936f62022-12-15 13:15:39 +0000135static char *(p_sloc_values[]) = {"last", "statusline", "tabline", NULL};
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +0000136static char *(p_sws_values[]) = {"fsync", "sync", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200137
138static int check_opt_strings(char_u *val, char **values, int list);
139static int opt_strings_flags(char_u *val, char **values, unsigned *flagp, int list);
140
141/*
142 * After setting various option values: recompute variables that depend on
143 * option values.
144 */
145 void
146didset_string_options(void)
147{
148 (void)opt_strings_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
149 (void)opt_strings_flags(p_bkc, p_bkc_values, &bkc_flags, TRUE);
150 (void)opt_strings_flags(p_bo, p_bo_values, &bo_flags, TRUE);
glepnirf31cfa22025-03-06 21:59:13 +0100151 (void)opt_strings_flags(p_cfc, p_cfc_values, &cfc_flags, TRUE);
zeertzjq529b9ad2024-06-05 20:27:06 +0200152 (void)opt_strings_flags(p_cot, p_cot_values, &cot_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200153#ifdef FEAT_SESSION
154 (void)opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE);
155 (void)opt_strings_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
156#endif
157#ifdef FEAT_FOLDING
158 (void)opt_strings_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
159#endif
160 (void)opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE);
Yegappan Lakshmanan87018252023-09-20 20:20:04 +0200161 (void)opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200162 (void)opt_strings_flags(p_tc, p_tc_values, &tc_flags, FALSE);
163 (void)opt_strings_flags(p_ve, p_ve_values, &ve_flags, TRUE);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +0200164#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +0200165 (void)opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE);
166#endif
167#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
168 (void)opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags, TRUE);
169#endif
170#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
171 (void)opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE);
172#endif
Sean Dewar39c46b42022-05-12 17:44:29 +0100173 (void)opt_strings_flags(p_swb, p_swb_values, &swb_flags, TRUE);
LemonBoy5247b0b2024-07-12 19:30:58 +0200174 (void)opt_strings_flags(p_tcl, p_tcl_values, &tcl_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200175}
176
Yegappan Lakshmananf9dc2782023-05-11 15:02:56 +0100177#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200178/*
179 * Trigger the OptionSet autocommand.
180 * "opt_idx" is the index of the option being set.
181 * "opt_flags" can be OPT_LOCAL etc.
182 * "oldval" the old value
183 * "oldval_l" the old local value (only non-NULL if global and local value
184 * are set)
185 * "oldval_g" the old global value (only non-NULL if global and local value
186 * are set)
187 * "newval" the new value
188 */
189 void
zeertzjq269aa2b2022-11-28 11:36:50 +0000190trigger_optionset_string(
Bram Moolenaardac13472019-09-16 21:06:21 +0200191 int opt_idx,
192 int opt_flags,
193 char_u *oldval,
194 char_u *oldval_l,
195 char_u *oldval_g,
196 char_u *newval)
197{
198 // Don't do this recursively.
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000199 if (oldval == NULL || newval == NULL
200 || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
201 return;
Bram Moolenaardac13472019-09-16 21:06:21 +0200202
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000203 char_u buf_type[7];
204
205 sprintf((char *)buf_type, "%s",
Bram Moolenaardac13472019-09-16 21:06:21 +0200206 (opt_flags & OPT_LOCAL) ? "local" : "global");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000207 set_vim_var_string(VV_OPTION_OLD, oldval, -1);
208 set_vim_var_string(VV_OPTION_NEW, newval, -1);
209 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
210 if (opt_flags & OPT_LOCAL)
211 {
212 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
213 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
Bram Moolenaardac13472019-09-16 21:06:21 +0200214 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000215 if (opt_flags & OPT_GLOBAL)
216 {
217 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
218 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval, -1);
219 }
220 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
221 {
222 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
223 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval_l, -1);
224 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval_g, -1);
225 }
226 if (opt_flags & OPT_MODELINE)
227 {
228 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
229 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
230 }
231 apply_autocmds(EVENT_OPTIONSET,
232 get_option_fullname(opt_idx), NULL, FALSE,
233 NULL);
234 reset_v_option_vars();
Bram Moolenaardac13472019-09-16 21:06:21 +0200235}
236#endif
237
238 static char *
Mike Williams620f0112023-12-05 15:36:06 +0100239illegal_char(char *errbuf, size_t errbuflen, int c)
Bram Moolenaardac13472019-09-16 21:06:21 +0200240{
241 if (errbuf == NULL)
242 return "";
zeertzjq6a8d2e12024-01-17 20:54:49 +0100243 vim_snprintf(errbuf, errbuflen, _(e_illegal_character_str),
Christian Brabandtb39b2402023-11-29 11:34:05 +0100244 (char *)transchar(c));
Bram Moolenaardac13472019-09-16 21:06:21 +0200245 return errbuf;
246}
247
248/*
249 * Check string options in a buffer for NULL value.
250 */
251 void
252check_buf_options(buf_T *buf)
253{
254 check_string_option(&buf->b_p_bh);
255 check_string_option(&buf->b_p_bt);
256 check_string_option(&buf->b_p_fenc);
257 check_string_option(&buf->b_p_ff);
258#ifdef FEAT_FIND_ID
259 check_string_option(&buf->b_p_def);
260 check_string_option(&buf->b_p_inc);
261# ifdef FEAT_EVAL
262 check_string_option(&buf->b_p_inex);
263# endif
264#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +0100265#if defined(FEAT_EVAL)
Bram Moolenaardac13472019-09-16 21:06:21 +0200266 check_string_option(&buf->b_p_inde);
267 check_string_option(&buf->b_p_indk);
268#endif
269#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
270 check_string_option(&buf->b_p_bexpr);
271#endif
272#if defined(FEAT_CRYPT)
273 check_string_option(&buf->b_p_cm);
274#endif
275 check_string_option(&buf->b_p_fp);
276#if defined(FEAT_EVAL)
277 check_string_option(&buf->b_p_fex);
278#endif
279#ifdef FEAT_CRYPT
280 check_string_option(&buf->b_p_key);
281#endif
282 check_string_option(&buf->b_p_kp);
283 check_string_option(&buf->b_p_mps);
284 check_string_option(&buf->b_p_fo);
285 check_string_option(&buf->b_p_flp);
286 check_string_option(&buf->b_p_isk);
Bram Moolenaardac13472019-09-16 21:06:21 +0200287 check_string_option(&buf->b_p_com);
Bram Moolenaardac13472019-09-16 21:06:21 +0200288#ifdef FEAT_FOLDING
289 check_string_option(&buf->b_p_cms);
290#endif
291 check_string_option(&buf->b_p_nf);
Bram Moolenaardac13472019-09-16 21:06:21 +0200292 check_string_option(&buf->b_p_qe);
Bram Moolenaardac13472019-09-16 21:06:21 +0200293#ifdef FEAT_SYN_HL
294 check_string_option(&buf->b_p_syn);
295 check_string_option(&buf->b_s.b_syn_isk);
296#endif
297#ifdef FEAT_SPELL
298 check_string_option(&buf->b_s.b_p_spc);
299 check_string_option(&buf->b_s.b_p_spf);
300 check_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +0200301 check_string_option(&buf->b_s.b_p_spo);
Bram Moolenaardac13472019-09-16 21:06:21 +0200302#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200303 check_string_option(&buf->b_p_sua);
Bram Moolenaardac13472019-09-16 21:06:21 +0200304 check_string_option(&buf->b_p_cink);
305 check_string_option(&buf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +0100306 check_string_option(&buf->b_p_cinsd);
Bram Moolenaardac13472019-09-16 21:06:21 +0200307 parse_cino(buf);
Bram Moolenaar49846fb2022-10-15 16:05:33 +0100308 check_string_option(&buf->b_p_lop);
Bram Moolenaardac13472019-09-16 21:06:21 +0200309 check_string_option(&buf->b_p_ft);
Bram Moolenaardac13472019-09-16 21:06:21 +0200310 check_string_option(&buf->b_p_cinw);
zeertzjq529b9ad2024-06-05 20:27:06 +0200311 check_string_option(&buf->b_p_cot);
Bram Moolenaardac13472019-09-16 21:06:21 +0200312 check_string_option(&buf->b_p_cpt);
glepnirbcd59952025-04-24 21:48:35 +0200313 check_string_option(&buf->b_p_ise);
Bram Moolenaardac13472019-09-16 21:06:21 +0200314#ifdef FEAT_COMPL_FUNC
315 check_string_option(&buf->b_p_cfu);
316 check_string_option(&buf->b_p_ofu);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +0100317 check_string_option(&buf->b_p_tsrfu);
Bram Moolenaardac13472019-09-16 21:06:21 +0200318#endif
319#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +0100320 check_string_option(&buf->b_p_ffu);
Bram Moolenaardac13472019-09-16 21:06:21 +0200321 check_string_option(&buf->b_p_tfu);
322#endif
323#ifdef FEAT_KEYMAP
324 check_string_option(&buf->b_p_keymap);
325#endif
326#ifdef FEAT_QUICKFIX
327 check_string_option(&buf->b_p_gp);
328 check_string_option(&buf->b_p_mp);
329 check_string_option(&buf->b_p_efm);
330#endif
331 check_string_option(&buf->b_p_ep);
332 check_string_option(&buf->b_p_path);
333 check_string_option(&buf->b_p_tags);
334 check_string_option(&buf->b_p_tc);
335 check_string_option(&buf->b_p_dict);
336 check_string_option(&buf->b_p_tsr);
Bram Moolenaardac13472019-09-16 21:06:21 +0200337 check_string_option(&buf->b_p_lw);
Bram Moolenaardac13472019-09-16 21:06:21 +0200338 check_string_option(&buf->b_p_bkc);
339 check_string_option(&buf->b_p_menc);
340#ifdef FEAT_VARTABS
341 check_string_option(&buf->b_p_vsts);
342 check_string_option(&buf->b_p_vts);
343#endif
344}
345
346/*
347 * Free the string allocated for an option.
348 * Checks for the string being empty_option. This may happen if we're out of
349 * memory, vim_strsave() returned NULL, which was replaced by empty_option by
350 * check_options().
351 * Does NOT check for P_ALLOCED flag!
352 */
353 void
354free_string_option(char_u *p)
355{
356 if (p != empty_option)
357 vim_free(p);
358}
359
360 void
361clear_string_option(char_u **pp)
362{
363 if (*pp != empty_option)
364 vim_free(*pp);
365 *pp = empty_option;
366}
367
368 void
369check_string_option(char_u **pp)
370{
371 if (*pp == NULL)
372 *pp = empty_option;
373}
374
375/*
376 * Set global value for string option when it's a local option.
377 */
378 static void
379set_string_option_global(
380 int opt_idx, // option index
381 char_u **varp) // pointer to option variable
382{
383 char_u **p, *s;
384
385 // the global value is always allocated
386 if (is_window_local_option(opt_idx))
387 p = (char_u **)GLOBAL_WO(varp);
388 else
389 p = (char_u **)get_option_var(opt_idx);
390 if (!is_global_option(opt_idx)
391 && p != varp
392 && (s = vim_strsave(*varp)) != NULL)
393 {
394 free_string_option(*p);
395 *p = s;
396 }
397}
398
399/*
400 * Set a string option to a new value (without checking the effect).
401 * The string is copied into allocated memory.
402 * if ("opt_idx" == -1) "name" is used, otherwise "opt_idx" is used.
403 * When "set_sid" is zero set the scriptID to current_sctx.sc_sid. When
404 * "set_sid" is SID_NONE don't set the scriptID. Otherwise set the scriptID to
405 * "set_sid".
406 */
407 void
408set_string_option_direct(
409 char_u *name,
410 int opt_idx,
411 char_u *val,
412 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
413 int set_sid UNUSED)
414{
415 char_u *s;
416 char_u **varp;
417 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
418 int idx = opt_idx;
419
420 if (idx == -1) // use name
421 {
422 idx = findoption(name);
423 if (idx < 0) // not found (should not happen)
424 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000425 semsg(_(e_internal_error_str), "set_string_option_direct()");
RestorerZ68ebcee2023-05-31 17:12:14 +0100426 siemsg("For option %s", name);
Bram Moolenaardac13472019-09-16 21:06:21 +0200427 return;
428 }
429 }
430
431 if (is_hidden_option(idx)) // can't set hidden option
432 return;
433
434 s = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000435 if (s == NULL)
436 return;
437
438 varp = (char_u **)get_option_varp_scope(idx,
439 both ? OPT_LOCAL : opt_flags);
440 if ((opt_flags & OPT_FREE) && (get_option_flags(idx) & P_ALLOCED))
441 free_string_option(*varp);
442 *varp = s;
443
444 // For buffer/window local option may also set the global value.
445 if (both)
446 set_string_option_global(idx, varp);
447
448 set_option_flag(idx, P_ALLOCED);
449
450 // When setting both values of a global option with a local value,
451 // make the local value empty, so that the global value is used.
452 if (is_global_local_option(idx) && both)
Bram Moolenaardac13472019-09-16 21:06:21 +0200453 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000454 free_string_option(*varp);
455 *varp = empty_option;
Bram Moolenaardac13472019-09-16 21:06:21 +0200456 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000457# ifdef FEAT_EVAL
458 if (set_sid != SID_NONE)
459 {
460 sctx_T script_ctx;
461
462 if (set_sid == 0)
463 script_ctx = current_sctx;
464 else
465 {
466 script_ctx.sc_sid = set_sid;
467 script_ctx.sc_seq = 0;
468 script_ctx.sc_lnum = 0;
469 script_ctx.sc_version = 1;
470 }
471 set_option_sctx_idx(idx, opt_flags, script_ctx);
472 }
473# endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200474}
475
Dominique Pellee764d1b2023-03-12 21:20:59 +0000476#if defined(FEAT_PROP_POPUP) || \
477 (defined(FEAT_DIFF) && defined(FEAT_FOLDING)) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200478/*
479 * Like set_string_option_direct(), but for a window-local option in "wp".
480 * Blocks autocommands to avoid the old curwin becoming invalid.
481 */
482 void
483set_string_option_direct_in_win(
484 win_T *wp,
485 char_u *name,
486 int opt_idx,
487 char_u *val,
488 int opt_flags,
489 int set_sid)
490{
491 win_T *save_curwin = curwin;
492
493 block_autocmds();
494 curwin = wp;
495 curbuf = curwin->w_buffer;
496 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
497 curwin = save_curwin;
498 curbuf = curwin->w_buffer;
499 unblock_autocmds();
500}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000501#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200502
Dominique Pelle748b3082022-01-08 12:41:16 +0000503#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200504/*
505 * Like set_string_option_direct(), but for a buffer-local option in "buf".
506 * Blocks autocommands to avoid the old curbuf becoming invalid.
507 */
508 void
509set_string_option_direct_in_buf(
510 buf_T *buf,
511 char_u *name,
512 int opt_idx,
513 char_u *val,
514 int opt_flags,
515 int set_sid)
516{
517 buf_T *save_curbuf = curbuf;
518
519 block_autocmds();
520 curbuf = buf;
521 curwin->w_buffer = curbuf;
522 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
523 curbuf = save_curbuf;
524 curwin->w_buffer = curbuf;
525 unblock_autocmds();
526}
Dominique Pelle748b3082022-01-08 12:41:16 +0000527#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200528
529/*
530 * Set a string option to a new value, and handle the effects.
531 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100532 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaardac13472019-09-16 21:06:21 +0200533 */
534 char *
535set_string_option(
536 int opt_idx,
537 char_u *value,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +0000538 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Christian Brabandtb39b2402023-11-29 11:34:05 +0100539 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +0100540 size_t errbuflen)
Bram Moolenaardac13472019-09-16 21:06:21 +0200541{
542 char_u *s;
543 char_u **varp;
544 char_u *oldval;
545#if defined(FEAT_EVAL)
546 char_u *oldval_l = NULL;
547 char_u *oldval_g = NULL;
548 char_u *saved_oldval = NULL;
549 char_u *saved_oldval_l = NULL;
550 char_u *saved_oldval_g = NULL;
551 char_u *saved_newval = NULL;
552#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100553 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +0200554 int value_checked = FALSE;
555
556 if (is_hidden_option(opt_idx)) // don't set hidden option
557 return NULL;
558
Bram Moolenaar7f009df2020-03-16 20:27:38 +0100559 s = vim_strsave(value == NULL ? (char_u *)"" : value);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000560 if (s == NULL)
561 return NULL;
562
563 varp = (char_u **)get_option_varp_scope(opt_idx,
564 (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
565 ? (is_global_local_option(opt_idx)
566 ? OPT_GLOBAL : OPT_LOCAL)
567 : opt_flags);
568 oldval = *varp;
569#if defined(FEAT_EVAL)
570 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +0200571 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000572 oldval_l = *(char_u **)get_option_varp_scope(opt_idx, OPT_LOCAL);
573 oldval_g = *(char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
Bram Moolenaardac13472019-09-16 21:06:21 +0200574 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000575#endif
576 *varp = s;
577
578#if defined(FEAT_EVAL)
579 if (!starting
580# ifdef FEAT_CRYPT
581 && !is_crypt_key_option(opt_idx)
582# endif
583 )
584 {
585 if (oldval_l != NULL)
586 saved_oldval_l = vim_strsave(oldval_l);
587 if (oldval_g != NULL)
588 saved_oldval_g = vim_strsave(oldval_g);
589 saved_oldval = vim_strsave(oldval);
590 saved_newval = vim_strsave(s);
591 }
592#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000593 if ((errmsg = did_set_string_option(opt_idx, varp, oldval, value, errbuf,
Christian Brabandtb39b2402023-11-29 11:34:05 +0100594 errbuflen, opt_flags, OP_NONE, &value_checked)) == NULL)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000595 did_set_option(opt_idx, opt_flags, TRUE, value_checked);
596
597#if defined(FEAT_EVAL)
598 // call autocommand after handling side effects
599 if (errmsg == NULL)
600 trigger_optionset_string(opt_idx, opt_flags,
601 saved_oldval, saved_oldval_l,
602 saved_oldval_g, saved_newval);
603 vim_free(saved_oldval);
604 vim_free(saved_oldval_l);
605 vim_free(saved_oldval_g);
606 vim_free(saved_newval);
607#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100608 return errmsg;
Bram Moolenaardac13472019-09-16 21:06:21 +0200609}
610
611/*
612 * Return TRUE if "val" is a valid 'filetype' name.
613 * Also used for 'syntax' and 'keymap'.
614 */
615 static int
616valid_filetype(char_u *val)
617{
618 return valid_name(val, ".-_");
619}
620
621#ifdef FEAT_STL_OPT
622/*
623 * Check validity of options with the 'statusline' format.
zeertzjq5dc294a2022-04-15 13:17:57 +0100624 * Return an untranslated error message or NULL.
Bram Moolenaardac13472019-09-16 21:06:21 +0200625 */
626 static char *
627check_stl_option(char_u *s)
628{
Bram Moolenaardac13472019-09-16 21:06:21 +0200629 int groupdepth = 0;
Christian Brabandtb39b2402023-11-29 11:34:05 +0100630 static char errbuf[ERR_BUFLEN];
631 int errbuflen = ERR_BUFLEN;
Bram Moolenaardac13472019-09-16 21:06:21 +0200632
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100633 while (*s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200634 {
635 // Check for valid keys after % sequences
636 while (*s && *s != '%')
637 s++;
638 if (!*s)
639 break;
640 s++;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +0000641 if (*s == '%' || *s == STL_TRUNCMARK || *s == STL_SEPARATE)
Bram Moolenaardac13472019-09-16 21:06:21 +0200642 {
643 s++;
644 continue;
645 }
646 if (*s == ')')
647 {
648 s++;
649 if (--groupdepth < 0)
650 break;
651 continue;
652 }
653 if (*s == '-')
654 s++;
655 while (VIM_ISDIGIT(*s))
656 s++;
657 if (*s == STL_USER_HL)
658 continue;
659 if (*s == '.')
660 {
661 s++;
662 while (*s && VIM_ISDIGIT(*s))
663 s++;
664 }
665 if (*s == '(')
666 {
667 groupdepth++;
668 continue;
669 }
670 if (vim_strchr(STL_ALL, *s) == NULL)
671 {
Christian Brabandtb39b2402023-11-29 11:34:05 +0100672 return illegal_char(errbuf, errbuflen, *s);
Bram Moolenaardac13472019-09-16 21:06:21 +0200673 }
674 if (*s == '{')
675 {
zeertzjq5dc294a2022-04-15 13:17:57 +0100676 int reevaluate = (*++s == '%');
shadmansaleh30e3de22021-05-15 17:23:28 +0200677
zeertzjq5dc294a2022-04-15 13:17:57 +0100678 if (reevaluate && *++s == '}')
679 // "}" is not allowed immediately after "%{%"
Christian Brabandtb39b2402023-11-29 11:34:05 +0100680 return illegal_char(errbuf, errbuflen, '}');
shadmansaleh30e3de22021-05-15 17:23:28 +0200681 while ((*s != '}' || (reevaluate && s[-1] != '%')) && *s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200682 s++;
683 if (*s != '}')
zeertzjq5dc294a2022-04-15 13:17:57 +0100684 return e_unclosed_expression_sequence;
Bram Moolenaardac13472019-09-16 21:06:21 +0200685 }
686 }
Bram Moolenaardac13472019-09-16 21:06:21 +0200687 if (groupdepth != 0)
zeertzjq5dc294a2022-04-15 13:17:57 +0100688 return e_unbalanced_groups;
Bram Moolenaardac13472019-09-16 21:06:21 +0200689 return NULL;
690}
691#endif
692
693/*
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +0000694 * Check for a "normal" directory or file name in some options. Disallow a
695 * path separator (slash and/or backslash), wildcards and characters that are
696 * often illegal in a file name. Be more permissive if "secure" is off.
697 */
698 static int
699check_illegal_path_names(int opt_idx, char_u **varp)
700{
701 return (((get_option_flags(opt_idx) & P_NFNAME)
702 && vim_strpbrk(*varp, (char_u *)(secure
703 ? "/\\*?[|;&<>\r\n" : "/\\*?[<>\r\n")) != NULL)
704 || ((get_option_flags(opt_idx) & P_NDNAME)
705 && vim_strpbrk(*varp, (char_u *)"*?[|;&<>\r\n") != NULL));
706}
707
708/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000709 * An option that accepts a list of flags is changed.
710 * e.g. 'viewoptions', 'switchbuf', 'casemap', etc.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000711 */
712 static char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000713did_set_opt_flags(char_u *val, char **values, unsigned *flagp, int list)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000714{
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000715 if (opt_strings_flags(val, values, flagp, list) == FAIL)
716 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000717
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000718 return NULL;
719}
720
721/*
722 * An option that accepts a list of string values is changed.
723 * e.g. 'nrformats', 'scrollopt', 'wildoptions', etc.
724 */
725 static char *
726did_set_opt_strings(char_u *val, char **values, int list)
727{
728 return did_set_opt_flags(val, values, NULL, list);
729}
730
731/*
732 * An option which is a list of flags is set. Valid values are in 'flags'.
733 */
734 static char *
Christian Brabandtb39b2402023-11-29 11:34:05 +0100735did_set_option_listflag(
736 char_u *val,
737 char_u *flags,
738 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +0100739 size_t errbuflen)
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000740{
741 char_u *s;
742
Yegappan Lakshmananc727b192023-03-03 12:26:15 +0000743 for (s = val; *s; ++s)
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000744 if (vim_strchr(flags, *s) == NULL)
Christian Brabandtb39b2402023-11-29 11:34:05 +0100745 return illegal_char(errbuf, errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000746
747 return NULL;
748}
749
750/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200751 * Expand an option that accepts a list of fixed string values with known
752 * number of items.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200753 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200754 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200755expand_set_opt_string(
756 optexpand_T *args,
757 char **values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +0200758 size_t numValues,
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200759 int *numMatches,
760 char_u ***matches)
761{
762 char_u *p;
763 regmatch_T *regmatch = args->oe_regmatch;
764 int include_orig_val = args->oe_include_orig_val;
765 char_u *option_val = args->oe_opt_value;
766
767 // Assume numValues is small since they are fixed enums, so just allocate
768 // upfront instead of needing two passes to calculate output size.
769 *matches = ALLOC_MULT(char_u *, numValues + 1);
770 if (*matches == NULL)
771 return FAIL;
772
773 int count = 0;
774
775 if (include_orig_val && *option_val != NUL)
776 {
777 p = vim_strsave(option_val);
778 if (p == NULL)
779 {
780 VIM_CLEAR(*matches);
781 return FAIL;
782 }
783 (*matches)[count++] = p;
784 }
785
786 for (char **val = values; *val != NULL; val++)
787 {
788 if (include_orig_val && *option_val != NUL)
789 {
790 if (STRCMP((char_u*)*val, option_val) == 0)
791 continue;
792 }
793 if (vim_regexec(regmatch, (char_u*)(*val), (colnr_T)0))
794 {
795 p = vim_strsave((char_u*)*val);
796 if (p == NULL)
797 {
798 if (count == 0)
799 {
800 VIM_CLEAR(*matches);
801 return FAIL;
802 }
803 else
804 break;
805 }
806 (*matches)[count++] = p;
807 }
808 }
809 if (count == 0)
810 {
811 VIM_CLEAR(*matches);
812 return FAIL;
813 }
814 *numMatches = count;
815 return OK;
816}
817
818static char_u *set_opt_callback_orig_option = NULL;
819static char_u *((*set_opt_callback_func)(expand_T *, int));
820
821/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200822 * Callback used by expand_set_opt_generic to also include the original value
823 * as the first item.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200824 */
825 static char_u *
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200826expand_set_opt_generic_cb(expand_T *xp, int idx)
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200827{
828 if (idx == 0)
829 {
830 if (set_opt_callback_orig_option != NULL)
831 return set_opt_callback_orig_option;
832 else
833 return (char_u *)""; // empty strings are ignored
834 }
835 return set_opt_callback_func(xp, idx - 1);
836}
837
838/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200839 * Expand an option with a callback that iterates through a list of possible
840 * names using an index.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200841 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200842 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200843expand_set_opt_generic(
844 optexpand_T *args,
845 char_u *((*func)(expand_T *, int)),
846 int *numMatches,
847 char_u ***matches)
848{
849 int ret;
850
851 set_opt_callback_orig_option = args->oe_include_orig_val ?
852 args->oe_opt_value : NULL;
853 set_opt_callback_func = func;
854
855 ret = ExpandGeneric(
856 (char_u*)"", // not using fuzzy as currently EXPAND_STRING_SETTING doesn't use it
857 args->oe_xp,
858 args->oe_regmatch,
859 matches,
860 numMatches,
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200861 expand_set_opt_generic_cb,
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200862 FALSE);
863
864 set_opt_callback_orig_option = NULL;
865 set_opt_callback_func = NULL;
866 return ret;
867}
868
Christian Brabandt9960ebc2023-10-05 22:17:09 +0200869# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200870static garray_T *expand_cb_ga;
871static optexpand_T *expand_cb_args;
872
873/*
874 * Callback provided to a function in expand_set_opt_callback. Will perform
875 * regex matching against the value and add to the list.
876 *
877 * Returns OK usually. Returns FAIL if it failed to allocate memory, and the
878 * caller should terminate the enumeration.
879 */
880 static int
881expand_set_opt_callback_cb(char_u *val)
882{
883 regmatch_T *regmatch = expand_cb_args->oe_regmatch;
884 expand_T *xp = expand_cb_args->oe_xp;
885 garray_T *ga = expand_cb_ga;
886 char_u *str;
887
888 if (val == NULL || *val == NUL)
889 return OK;
890
891 if (xp->xp_pattern[0] != NUL &&
892 !vim_regexec(regmatch, val, (colnr_T)0))
893 return OK;
894
895 str = vim_strsave_escaped(val, (char_u *)" \t\\");
896
897 if (str == NULL)
898 return FAIL;
899
900 if (ga_grow(ga, 1) == FAIL)
901 {
902 vim_free(str);
903 return FAIL;
904 }
905
906 ((char_u **)ga->ga_data)[ga->ga_len] = str;
907 ++ga->ga_len;
908 return OK;
909}
910
911/*
912 * Expand an option with a provided function that takes a callback. The
913 * function will enumerate through all options and call the callback to add it
914 * to the list.
915 *
916 * "func" is the enumerator function that will generate the list of options.
917 * "func_params" is a single parameter that will be passed to func.
918 */
919 static int
920expand_set_opt_callback(
921 optexpand_T *args,
922 void (*func)(optexpand_T *, void* params, int (*cb)(char_u *val)),
923 void *func_params,
924 int *numMatches,
925 char_u ***matches)
926{
927 garray_T ga;
928 int include_orig_val = args->oe_include_orig_val;
929 char_u *option_val = args->oe_opt_value;
930
931 ga_init2(&ga, sizeof(char *), 30);
932
933 if (include_orig_val && *option_val != NUL)
934 {
935 char_u *p = vim_strsave(option_val);
936 if (p == NULL)
937 return FAIL;
938 if (ga_grow(&ga, 1) == FAIL)
939 {
940 vim_free(p);
941 return FAIL;
942 }
943 ((char_u **)ga.ga_data)[ga.ga_len] = p;
944 ++ga.ga_len;
945 }
946
947 expand_cb_ga = &ga;
948 expand_cb_args = args;
949
950 func(args, func_params, expand_set_opt_callback_cb);
951
952 expand_cb_ga = NULL;
953 expand_cb_args = NULL;
954
955 *matches = ga.ga_data;
956 *numMatches = ga.ga_len;
957 return OK;
958}
Christian Brabandt9960ebc2023-10-05 22:17:09 +0200959#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200960
961/*
962 * Expand an option which is a list of flags.
963 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200964 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200965expand_set_opt_listflag(
966 optexpand_T *args,
967 char_u *flags,
968 int *numMatches,
969 char_u ***matches)
970{
971 char_u *p;
972 char_u *option_val = args->oe_opt_value;
973 char_u *cmdline_val = args->oe_set_arg;
974 int append = args->oe_append;
975 int include_orig_val = args->oe_include_orig_val && (*option_val != NUL);
976
Yee Cheng Chin6d113472023-10-02 21:38:39 +0200977 size_t num_flags = STRLEN(flags);
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200978
979 // Assume we only have small number of flags, so just allocate max size.
980 *matches = ALLOC_MULT(char_u *, num_flags + 1);
981 if (*matches == NULL)
982 return FAIL;
983
984 int count = 0;
985
986 if (include_orig_val)
987 {
988 p = vim_strsave(option_val);
989 if (p == NULL)
990 {
991 VIM_CLEAR(*matches);
992 return FAIL;
993 }
994 (*matches)[count++] = p;
995 }
996
997 for (char_u *flag = flags; *flag != NUL; flag++)
998 {
999 if (append && vim_strchr(option_val, *flag) != NULL)
1000 continue;
1001
1002 if (vim_strchr(cmdline_val, *flag) == NULL)
1003 {
1004 if (include_orig_val
1005 && option_val[1] == NUL
1006 && *flag == option_val[0])
1007 {
1008 // This value is already used as the first choice as it's the
1009 // existing flag. Just skip it to avoid duplicate.
1010 continue;
1011 }
1012 p = vim_strnsave(flag, 1);
1013 if (p == NULL)
1014 {
1015 if (count == 0)
1016 {
1017 VIM_CLEAR(*matches);
1018 return FAIL;
1019 }
1020 else
1021 break;
1022 }
1023 (*matches)[count++] = p;
1024 }
1025 }
1026
1027 if (count == 0)
1028 {
1029 VIM_CLEAR(*matches);
1030 return FAIL;
1031 }
1032 *numMatches = count;
1033 return OK;
1034}
1035
1036/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001037 * The 'ambiwidth' option is changed.
1038 */
1039 char *
1040did_set_ambiwidth(optset_T *args UNUSED)
1041{
1042 if (check_opt_strings(p_ambw, p_ambw_values, FALSE) != OK)
1043 return e_invalid_argument;
1044
1045 return check_chars_options();
1046}
1047
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001048 int
1049expand_set_ambiwidth(optexpand_T *args, int *numMatches, char_u ***matches)
1050{
1051 return expand_set_opt_string(
1052 args,
1053 p_ambw_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001054 ARRAY_LENGTH(p_ambw_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001055 numMatches,
1056 matches);
1057}
1058
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001059/*
1060 * The 'background' option is changed.
1061 */
1062 char *
Gregory Anders83ad2722024-01-03 19:48:51 +01001063did_set_background(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001064{
1065 if (check_opt_strings(p_bg, p_bg_values, FALSE) == FAIL)
1066 return e_invalid_argument;
1067
Gregory Anders83ad2722024-01-03 19:48:51 +01001068 if (args->os_oldval.string != NULL && args->os_oldval.string[0] == *p_bg)
1069 // Value was not changed
1070 return NULL;
1071
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001072#ifdef FEAT_EVAL
1073 int dark = (*p_bg == 'd');
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001074#endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001075
1076 init_highlight(FALSE, FALSE);
1077
1078#ifdef FEAT_EVAL
1079 if (dark != (*p_bg == 'd')
1080 && get_var_value((char_u *)"g:colors_name") != NULL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001081 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001082 // The color scheme must have set 'background' back to another
1083 // value, that's not what we want here. Disable the color
1084 // scheme and set the colors again.
1085 do_unlet((char_u *)"g:colors_name", TRUE);
1086 free_string_option(p_bg);
1087 p_bg = vim_strsave((char_u *)(dark ? "dark" : "light"));
1088 check_string_option(&p_bg);
1089 init_highlight(FALSE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001090 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001091#endif
1092#ifdef FEAT_TERMINAL
1093 term_update_colors_all();
1094#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001095
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001096 return NULL;
1097}
1098
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001099 int
1100expand_set_background(optexpand_T *args, int *numMatches, char_u ***matches)
1101{
1102 return expand_set_opt_string(
1103 args,
1104 p_bg_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001105 ARRAY_LENGTH(p_bg_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001106 numMatches,
1107 matches);
1108}
1109
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001110/*
1111 * The 'backspace' option is changed.
1112 */
1113 char *
1114did_set_backspace(optset_T *args UNUSED)
1115{
1116 if (VIM_ISDIGIT(*p_bs))
1117 {
1118 if (*p_bs > '3' || p_bs[1] != NUL)
1119 return e_invalid_argument;
1120 }
1121 else if (check_opt_strings(p_bs, p_bs_values, TRUE) != OK)
1122 return e_invalid_argument;
1123
1124 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001125}
1126
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001127 int
1128expand_set_backspace(optexpand_T *args, int *numMatches, char_u ***matches)
1129{
1130 return expand_set_opt_string(
1131 args,
1132 p_bs_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001133 ARRAY_LENGTH(p_bs_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001134 numMatches,
1135 matches);
1136}
1137
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001138/*
1139 * The 'backupcopy' option is changed.
1140 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001141 char *
1142did_set_backupcopy(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001143{
1144 char_u *bkc = p_bkc;
1145 unsigned int *flags = &bkc_flags;
1146 char *errmsg = NULL;
1147
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001148 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001149 {
1150 bkc = curbuf->b_p_bkc;
1151 flags = &curbuf->b_bkc_flags;
1152 }
zeertzjq46dcd842024-11-03 09:10:50 +01001153 else if (!(args->os_flags & OPT_GLOBAL))
1154 // When using :set, clear the local flags.
1155 curbuf->b_bkc_flags = 0;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001156
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001157 if ((args->os_flags & OPT_LOCAL) && *bkc == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001158 // make the local value empty: use the global value
1159 *flags = 0;
1160 else
1161 {
1162 if (opt_strings_flags(bkc, p_bkc_values, flags, TRUE) != OK)
1163 errmsg = e_invalid_argument;
1164 if ((((int)*flags & BKC_AUTO) != 0)
1165 + (((int)*flags & BKC_YES) != 0)
1166 + (((int)*flags & BKC_NO) != 0) != 1)
1167 {
1168 // Must have exactly one of "auto", "yes" and "no".
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001169 (void)opt_strings_flags(args->os_oldval.string, p_bkc_values,
1170 flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001171 errmsg = e_invalid_argument;
1172 }
1173 }
1174
1175 return errmsg;
1176}
1177
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001178 int
1179expand_set_backupcopy(optexpand_T *args, int *numMatches, char_u ***matches)
1180{
1181 return expand_set_opt_string(
1182 args,
1183 p_bkc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001184 ARRAY_LENGTH(p_bkc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001185 numMatches,
1186 matches);
1187}
1188
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001189/*
1190 * The 'backupext' or the 'patchmode' option is changed.
1191 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001192 char *
1193did_set_backupext_or_patchmode(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001194{
1195 if (STRCMP(*p_bex == '.' ? p_bex + 1 : p_bex,
1196 *p_pm == '.' ? p_pm + 1 : p_pm) == 0)
1197 return e_backupext_and_patchmode_are_equal;
1198
1199 return NULL;
1200}
1201
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001202/*
1203 * The 'belloff' option is changed.
1204 */
1205 char *
1206did_set_belloff(optset_T *args UNUSED)
1207{
1208 return did_set_opt_flags(p_bo, p_bo_values, &bo_flags, TRUE);
1209}
1210
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001211 int
1212expand_set_belloff(optexpand_T *args, int *numMatches, char_u ***matches)
1213{
1214 return expand_set_opt_string(
1215 args,
1216 p_bo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001217 ARRAY_LENGTH(p_bo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001218 numMatches,
1219 matches);
1220}
1221
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001222#if defined(FEAT_LINEBREAK) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001223/*
zeertzjqeac3fdc2024-02-03 18:08:09 +01001224 * The 'breakat' option is changed.
1225 */
1226 char *
1227did_set_breakat(optset_T *args UNUSED)
1228{
1229 char_u *p;
1230 int i;
1231
1232 for (i = 0; i < 256; i++)
1233 breakat_flags[i] = FALSE;
1234
1235 if (p_breakat != NULL)
1236 for (p = p_breakat; *p; p++)
1237 breakat_flags[*p] = TRUE;
1238
1239 return NULL;
1240}
1241
1242/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001243 * The 'breakindentopt' option is changed.
1244 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001245 char *
Millyb38700a2024-10-22 22:59:39 +02001246did_set_breakindentopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001247{
Millyb38700a2024-10-22 22:59:39 +02001248 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001249
Millyb38700a2024-10-22 22:59:39 +02001250 if (briopt_check(*varp, varp == &curwin->w_p_briopt ? curwin : NULL)
1251 == FAIL)
1252 return e_invalid_argument;
1253
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001254 // list setting requires a redraw
Millyb38700a2024-10-22 22:59:39 +02001255 if (varp == &curwin->w_p_briopt && curwin->w_briopt_list)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001256 redraw_all_later(UPD_NOT_VALID);
1257
Millyb38700a2024-10-22 22:59:39 +02001258 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001259}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001260
1261 int
1262expand_set_breakindentopt(optexpand_T *args, int *numMatches, char_u ***matches)
1263{
1264 return expand_set_opt_string(
1265 args,
1266 p_briopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001267 ARRAY_LENGTH(p_briopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001268 numMatches,
1269 matches);
1270}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001271#endif
1272
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001273#if defined(FEAT_BROWSE) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001274/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001275 * The 'browsedir' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001276 */
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00001277 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001278did_set_browsedir(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001279{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001280 if (check_opt_strings(p_bsdir, p_bsdir_values, FALSE) != OK
1281 && !mch_isdir(p_bsdir))
1282 return e_invalid_argument;
1283
1284 return NULL;
1285}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001286
1287 int
1288expand_set_browsedir(optexpand_T *args, int *numMatches, char_u ***matches)
1289{
1290 return expand_set_opt_string(
1291 args,
1292 p_bsdir_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001293 ARRAY_LENGTH(p_bsdir_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001294 numMatches,
1295 matches);
1296}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001297#endif
1298
1299/*
1300 * The 'bufhidden' option is changed.
1301 */
1302 char *
1303did_set_bufhidden(optset_T *args UNUSED)
1304{
1305 return did_set_opt_strings(curbuf->b_p_bh, p_bufhidden_values, FALSE);
1306}
1307
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001308 int
1309expand_set_bufhidden(optexpand_T *args, int *numMatches, char_u ***matches)
1310{
1311 return expand_set_opt_string(
1312 args,
1313 p_bufhidden_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001314 ARRAY_LENGTH(p_bufhidden_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001315 numMatches,
1316 matches);
1317}
1318
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001319/*
1320 * The 'buftype' option is changed.
1321 */
1322 char *
1323did_set_buftype(optset_T *args UNUSED)
1324{
1325 if (check_opt_strings(curbuf->b_p_bt, p_buftype_values, FALSE) != OK)
1326 return e_invalid_argument;
1327
1328 if (curwin->w_status_height)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001329 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001330 curwin->w_redr_status = TRUE;
1331 redraw_later(UPD_VALID);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001332 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001333 curbuf->b_help = (curbuf->b_p_bt[0] == 'h');
1334 redraw_titles();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001335
1336 return NULL;
1337}
1338
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001339 int
1340expand_set_buftype(optexpand_T *args, int *numMatches, char_u ***matches)
1341{
1342 return expand_set_opt_string(
1343 args,
1344 p_buftype_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001345 ARRAY_LENGTH(p_buftype_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001346 numMatches,
1347 matches);
1348}
1349
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001350/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001351 * The 'casemap' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001352 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001353 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001354did_set_casemap(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001355{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001356 return did_set_opt_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
1357}
1358
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001359 int
1360expand_set_casemap(optexpand_T *args, int *numMatches, char_u ***matches)
1361{
1362 return expand_set_opt_string(
1363 args,
1364 p_cmp_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001365 ARRAY_LENGTH(p_cmp_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001366 numMatches,
1367 matches);
1368}
1369
1370#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1371 int
1372expand_set_clipboard(optexpand_T *args, int *numMatches, char_u ***matches)
1373{
1374 return expand_set_opt_string(
1375 args,
1376 p_cb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001377 ARRAY_LENGTH(p_cb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001378 numMatches,
1379 matches);
1380}
1381#endif
1382
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001383/*
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001384 * The global 'listchars' or 'fillchars' option is changed.
1385 */
1386 static char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01001387did_set_global_listfillchars(char_u *val, int opt_lcs, int opt_flags,
1388 char *errbuf, size_t errbuflen)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001389{
1390 char *errmsg = NULL;
1391 char_u **local_ptr = opt_lcs ? &curwin->w_p_lcs : &curwin->w_p_fcs;
1392
1393 // only apply the global value to "curwin" when it does not have a
1394 // local value
1395 if (opt_lcs)
1396 errmsg = set_listchars_option(curwin, val,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001397 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL),
1398 errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001399 else
1400 errmsg = set_fillchars_option(curwin, val,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001401 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL),
1402 errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001403 if (errmsg != NULL)
1404 return errmsg;
1405
1406 tabpage_T *tp;
1407 win_T *wp;
1408
1409 // If the current window is set to use the global
1410 // 'listchars'/'fillchars' value, clear the window-local value.
1411 if (!(opt_flags & OPT_GLOBAL))
1412 clear_string_option(local_ptr);
1413 FOR_ALL_TAB_WINDOWS(tp, wp)
1414 {
1415 // If the current window has a local value need to apply it
1416 // again, it was changed when setting the global value.
1417 // If no error was returned above, we don't expect an error
1418 // here, so ignore the return value.
1419 if (opt_lcs)
1420 {
1421 if (*wp->w_p_lcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001422 (void)set_listchars_option(wp, wp->w_p_lcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001423 }
1424 else
1425 {
1426 if (*wp->w_p_fcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001427 (void)set_fillchars_option(wp, wp->w_p_fcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001428 }
1429 }
1430
1431 redraw_all_later(UPD_NOT_VALID);
1432
1433 return NULL;
1434}
1435
1436/*
1437 * The 'fillchars' option or the 'listchars' option is changed.
1438 */
1439 char *
1440did_set_chars_option(optset_T *args)
1441{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001442 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001443 char *errmsg = NULL;
1444
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001445 if ( varp == &p_lcs // global 'listchars'
1446 || varp == &p_fcs) // global 'fillchars'
1447 errmsg = did_set_global_listfillchars(*varp, varp == &p_lcs,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001448 args->os_flags, args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001449 else if (varp == &curwin->w_p_lcs) // local 'listchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001450 errmsg = set_listchars_option(curwin, *varp, TRUE,
1451 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001452 else if (varp == &curwin->w_p_fcs) // local 'fillchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001453 errmsg = set_fillchars_option(curwin, *varp, TRUE,
1454 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001455
1456 return errmsg;
1457}
1458
1459/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001460 * Expand 'fillchars' or 'listchars' option value.
1461 */
1462 int
1463expand_set_chars_option(optexpand_T *args, int *numMatches, char_u ***matches)
1464{
1465 char_u **varp = (char_u **)args->oe_varp;
1466 int is_lcs = (varp == &p_lcs || varp == &curwin->w_p_lcs);
1467 return expand_set_opt_generic(
1468 args,
1469 is_lcs ? get_listchars_name : get_fillchars_name,
1470 numMatches,
1471 matches);
1472}
1473
1474/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001475 * The 'cinoptions' option is changed.
1476 */
1477 char *
1478did_set_cinoptions(optset_T *args UNUSED)
1479{
1480 // TODO: recognize errors
1481 parse_cino(curbuf);
1482
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001483 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001484}
1485
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00001486#if defined(FEAT_SYN_HL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001487/*
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001488 * The 'colorcolumn' option is changed.
1489 */
1490 char *
Millya441a3e2024-10-22 22:43:01 +02001491did_set_colorcolumn(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001492{
Millya441a3e2024-10-22 22:43:01 +02001493 char_u **varp = (char_u **)args->os_varp;
1494
1495 return check_colorcolumn(*varp, varp == &curwin->w_p_cc ? curwin : NULL);
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001496}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001497#endif
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001498
1499/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001500 * The 'comments' option is changed.
1501 */
1502 char *
1503did_set_comments(optset_T *args)
1504{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001505 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001506 char_u *s;
1507 char *errmsg = NULL;
1508
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001509 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001510 {
1511 while (*s && *s != ':')
1512 {
1513 if (vim_strchr((char_u *)COM_ALL, *s) == NULL
1514 && !VIM_ISDIGIT(*s) && *s != '-')
1515 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01001516 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001517 break;
1518 }
1519 ++s;
1520 }
1521 if (*s++ == NUL)
1522 errmsg = e_missing_colon;
1523 else if (*s == ',' || *s == NUL)
1524 errmsg = e_zero_length_string;
1525 if (errmsg != NULL)
1526 break;
1527 while (*s && *s != ',')
1528 {
1529 if (*s == '\\' && s[1] != NUL)
1530 ++s;
1531 ++s;
1532 }
1533 s = skip_to_option_part(s);
1534 }
1535
1536 return errmsg;
1537}
1538
1539#if defined(FEAT_FOLDING) || defined(PROTO)
1540/*
1541 * The 'commentstring' option is changed.
1542 */
1543 char *
1544did_set_commentstring(optset_T *args)
1545{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001546 char_u **varp = (char_u **)args->os_varp;
1547
1548 if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001549 return e_commentstring_must_be_empty_or_contain_str;
1550
1551 return NULL;
1552}
1553#endif
1554
1555/*
Girish Palyacbe53192025-04-14 22:13:15 +02001556 * Check if value for 'complete' is valid when 'complete' option is changed.
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001557 */
1558 char *
1559did_set_complete(optset_T *args)
1560{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001561 char_u **varp = (char_u **)args->os_varp;
Girish Palya0ac1eb32025-04-16 20:18:33 +02001562 char_u *p, *t;
Girish Palyacbe53192025-04-14 22:13:15 +02001563 char_u buffer[LSIZE];
1564 char_u *buf_ptr;
Girish Palya0ac1eb32025-04-16 20:18:33 +02001565 char_u char_before = NUL;
Girish Palyacbe53192025-04-14 22:13:15 +02001566 int escape;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001567
Girish Palyacbe53192025-04-14 22:13:15 +02001568 for (p = *varp; *p; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001569 {
Girish Palyacbe53192025-04-14 22:13:15 +02001570 vim_memset(buffer, 0, LSIZE);
1571 buf_ptr = buffer;
1572 escape = 0;
1573
1574 // Extract substring while handling escaped commas
1575 while (*p && (*p != ',' || escape) && buf_ptr < (buffer + LSIZE - 1))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001576 {
Girish Palyacbe53192025-04-14 22:13:15 +02001577 if (*p == '\\' && *(p + 1) == ',')
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001578 {
Girish Palyacbe53192025-04-14 22:13:15 +02001579 escape = 1; // Mark escape mode
1580 p++; // Skip '\'
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001581 }
1582 else
1583 {
Girish Palyacbe53192025-04-14 22:13:15 +02001584 escape = 0;
1585 *buf_ptr++ = *p;
1586 }
1587 p++;
1588 }
1589 *buf_ptr = NUL;
1590
1591 if (vim_strchr((char_u *)".wbuksid]tUfo", *buffer) == NULL)
1592 return illegal_char(args->os_errbuf, args->os_errbuflen, *buffer);
1593
Girish Palya0ac1eb32025-04-16 20:18:33 +02001594 if (vim_strchr((char_u *)"ksf", *buffer) == NULL && *(buffer + 1) != NUL
1595 && *(buffer + 1) != '^')
1596 char_before = *buffer;
1597 else
1598 {
1599 // Test for a number after '^'
1600 if ((t = vim_strchr(buffer, '^')) != NULL)
1601 {
1602 *t++ = NUL;
1603 if (!*t)
1604 char_before = '^';
1605 else
1606 {
1607 for (; *t; t++)
1608 {
1609 if (!vim_isdigit(*t))
1610 {
1611 char_before = '^';
1612 break;
1613 }
1614 }
1615 }
1616 }
1617 }
1618 if (char_before != NUL)
Girish Palyacbe53192025-04-14 22:13:15 +02001619 {
1620 if (args->os_errbuf)
1621 {
1622 vim_snprintf((char *)args->os_errbuf, args->os_errbuflen,
Girish Palya0ac1eb32025-04-16 20:18:33 +02001623 _(e_illegal_character_after_chr), char_before);
Girish Palyacbe53192025-04-14 22:13:15 +02001624 return args->os_errbuf;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001625 }
Girish Palya0ac1eb32025-04-16 20:18:33 +02001626 return NULL;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001627 }
Girish Palyacbe53192025-04-14 22:13:15 +02001628 // Skip comma and spaces
1629 while (*p == ',' || *p == ' ')
1630 p++;
1631 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001632 return NULL;
1633}
1634
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001635 int
1636expand_set_complete(optexpand_T *args, int *numMatches, char_u ***matches)
1637{
1638 static char *(p_cpt_values[]) = {
Girish Palyacbe53192025-04-14 22:13:15 +02001639 ".", "w", "b", "u", "k", "kspell", "s", "i", "d", "]", "t", "U", "f", "o",
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001640 NULL};
1641 return expand_set_opt_string(
1642 args,
1643 p_cpt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001644 ARRAY_LENGTH(p_cpt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001645 numMatches,
1646 matches);
1647}
1648
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001649/*
1650 * The 'completeopt' option is changed.
1651 */
1652 char *
1653did_set_completeopt(optset_T *args UNUSED)
1654{
zeertzjq529b9ad2024-06-05 20:27:06 +02001655 char_u *cot = p_cot;
1656 unsigned *flags = &cot_flags;
1657
1658 if (args->os_flags & OPT_LOCAL)
1659 {
1660 cot = curbuf->b_p_cot;
1661 flags = &curbuf->b_cot_flags;
1662 }
zeertzjq46dcd842024-11-03 09:10:50 +01001663 else if (!(args->os_flags & OPT_GLOBAL))
1664 // When using :set, clear the local flags.
1665 curbuf->b_cot_flags = 0;
zeertzjq529b9ad2024-06-05 20:27:06 +02001666
1667 if (check_opt_strings(cot, p_cot_values, TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001668 return e_invalid_argument;
1669
zeertzjq529b9ad2024-06-05 20:27:06 +02001670 if (opt_strings_flags(cot, p_cot_values, flags, TRUE) != OK)
1671 return e_invalid_argument;
1672
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001673 return NULL;
1674}
1675
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001676 int
1677expand_set_completeopt(optexpand_T *args, int *numMatches, char_u ***matches)
1678{
1679 return expand_set_opt_string(
1680 args,
1681 p_cot_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001682 ARRAY_LENGTH(p_cot_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001683 numMatches,
1684 matches);
1685}
1686
glepnir6a89c942024-10-01 20:32:12 +02001687/*
glepnirf31cfa22025-03-06 21:59:13 +01001688 * The 'completefuzzycollect' option is changed.
1689 */
1690 char *
1691did_set_completefuzzycollect(optset_T *args UNUSED)
1692{
1693 if (opt_strings_flags(p_cfc, p_cfc_values, &cfc_flags, TRUE) != OK)
1694 return e_invalid_argument;
1695 return NULL;
1696}
1697
zeertzjq53d59ec2025-03-07 19:09:09 +01001698 int
1699expand_set_completefuzzycollect(
1700 optexpand_T *args,
1701 int *numMatches,
1702 char_u ***matches)
1703{
1704 return expand_set_opt_string(
1705 args,
1706 p_cfc_values,
1707 ARRAY_LENGTH(p_cfc_values) - 1,
1708 numMatches,
1709 matches);
1710}
1711
glepnirf31cfa22025-03-06 21:59:13 +01001712/*
glepnir6a89c942024-10-01 20:32:12 +02001713 * The 'completeitemalign' option is changed.
1714 */
1715 char *
1716did_set_completeitemalign(optset_T *args UNUSED)
1717{
1718 char_u *p = p_cia;
1719 unsigned new_cia_flags = 0;
1720 int seen[3] = { FALSE, FALSE, FALSE };
1721 int count = 0;
1722 char_u buf[10];
1723
1724 while (*p)
1725 {
1726 copy_option_part(&p, buf, sizeof(buf), ",");
1727 if (count >= 3)
1728 return e_invalid_argument;
1729
1730 if (STRCMP(buf, "abbr") == 0)
1731 {
1732 if (seen[CPT_ABBR])
1733 return e_invalid_argument;
1734 new_cia_flags = new_cia_flags * 10 + CPT_ABBR;
1735 seen[CPT_ABBR] = TRUE;
1736 count++;
1737 }
1738 else if (STRCMP(buf, "kind") == 0)
1739 {
1740 if (seen[CPT_KIND])
1741 return e_invalid_argument;
1742 new_cia_flags = new_cia_flags * 10 + CPT_KIND;
1743 seen[CPT_KIND] = TRUE;
1744 count++;
1745 }
1746 else if (STRCMP(buf, "menu") == 0)
1747 {
1748 if (seen[CPT_MENU])
1749 return e_invalid_argument;
1750 new_cia_flags = new_cia_flags * 10 + CPT_MENU;
1751 seen[CPT_MENU] = TRUE;
1752 count++;
1753 }
1754 else
1755 return e_invalid_argument;
1756 }
1757 if (new_cia_flags == 0 || count != 3)
1758 return e_invalid_argument;
1759
1760 cia_flags = new_cia_flags;
1761 return NULL;
1762}
1763
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001764#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1765/*
1766 * The 'completepopup' option is changed.
1767 */
1768 char *
1769did_set_completepopup(optset_T *args UNUSED)
1770{
1771 if (parse_completepopup(NULL) == FAIL)
1772 return e_invalid_argument;
1773
1774 popup_close_info();
1775 return NULL;
1776}
1777#endif
1778
1779#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1780/*
1781 * The 'completeslash' option is changed.
1782 */
1783 char *
1784did_set_completeslash(optset_T *args UNUSED)
1785{
1786 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1787 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
1788 return e_invalid_argument;
1789
1790 return NULL;
1791}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001792
1793 int
1794expand_set_completeslash(optexpand_T *args, int *numMatches, char_u ***matches)
1795{
1796 return expand_set_opt_string(
1797 args,
1798 p_csl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001799 ARRAY_LENGTH(p_csl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001800 numMatches,
1801 matches);
1802}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001803#endif
1804
1805#if defined(FEAT_CONCEAL) || defined(PROTO)
1806/*
1807 * The 'concealcursor' option is changed.
1808 */
1809 char *
1810did_set_concealcursor(optset_T *args)
1811{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001812 char_u **varp = (char_u **)args->os_varp;
1813
Christian Brabandtb39b2402023-11-29 11:34:05 +01001814 return did_set_option_listflag(*varp, (char_u *)COCU_ALL, args->os_errbuf,
1815 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001816}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001817
1818 int
1819expand_set_concealcursor(optexpand_T *args, int *numMatches, char_u ***matches)
1820{
1821 return expand_set_opt_listflag(args, (char_u*)COCU_ALL, numMatches, matches);
1822}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001823#endif
1824
1825/*
1826 * The 'cpoptions' option is changed.
1827 */
1828 char *
1829did_set_cpoptions(optset_T *args)
1830{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001831 char_u **varp = (char_u **)args->os_varp;
1832
Christian Brabandtb39b2402023-11-29 11:34:05 +01001833 return did_set_option_listflag(*varp, (char_u *)CPO_ALL, args->os_errbuf,
1834 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001835}
1836
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001837 int
1838expand_set_cpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
1839{
1840 return expand_set_opt_listflag(args, (char_u*)CPO_ALL, numMatches, matches);
1841}
1842
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001843#if defined(FEAT_CRYPT) || defined(PROTO)
1844/*
1845 * The 'cryptkey' option is changed.
1846 */
1847 char *
1848did_set_cryptkey(optset_T *args)
1849{
1850 // Make sure the ":set" command doesn't show the new value in the
1851 // history.
1852 remove_key_from_history();
1853
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02001854 if (args->os_op != OP_NONE)
1855 // Don't allow set+=/-=/^= as they can allow for substring guessing
1856 return e_invalid_argument;
1857
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001858 if (STRCMP(curbuf->b_p_key, args->os_oldval.string) != 0)
1859 {
1860 // Need to update the swapfile.
1861 ml_set_crypt_key(curbuf, args->os_oldval.string,
1862 *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
1863 changed_internal();
1864 }
Christian Brabandt19e6c4f2023-06-27 18:57:10 +01001865# ifdef FEAT_SODIUM
1866 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
1867 crypt_sodium_lock_key(args->os_newval.string);
1868# endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001869
1870 return NULL;
1871}
1872
1873/*
1874 * The 'cryptmethod' option is changed.
1875 */
1876 char *
1877did_set_cryptmethod(optset_T *args)
1878{
1879 char_u *p;
1880 char_u *s;
1881
1882 if (args->os_flags & OPT_LOCAL)
1883 p = curbuf->b_p_cm;
1884 else
1885 p = p_cm;
1886 if (check_opt_strings(p, p_cm_values, TRUE) != OK)
1887 return e_invalid_argument;
1888 else if (crypt_self_test() == FAIL)
1889 return e_invalid_argument;
1890
1891 // When setting the global value to empty, make it "zip".
1892 if (*p_cm == NUL)
1893 {
1894 free_string_option(p_cm);
1895 p_cm = vim_strsave((char_u *)"zip");
1896 }
1897 // When using ":set cm=name" the local value is going to be empty.
1898 // Do that here, otherwise the crypt functions will still use the
1899 // local value.
1900 if ((args->os_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1901 {
1902 free_string_option(curbuf->b_p_cm);
1903 curbuf->b_p_cm = empty_option;
1904 }
1905
1906 // Need to update the swapfile when the effective method changed.
1907 // Set "s" to the effective old value, "p" to the effective new
1908 // method and compare.
1909 if ((args->os_flags & OPT_LOCAL) && *args->os_oldval.string == NUL)
1910 s = p_cm; // was previously using the global value
1911 else
1912 s = args->os_oldval.string;
1913 if (*curbuf->b_p_cm == NUL)
1914 p = p_cm; // is now using the global value
1915 else
1916 p = curbuf->b_p_cm;
1917 if (STRCMP(s, p) != 0)
1918 ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1919
1920 // If the global value changes need to update the swapfile for all
1921 // buffers using that value.
1922 if ((args->os_flags & OPT_GLOBAL)
1923 && STRCMP(p_cm, args->os_oldval.string) != 0)
1924 {
1925 buf_T *buf;
1926
1927 FOR_ALL_BUFFERS(buf)
1928 if (buf != curbuf && *buf->b_p_cm == NUL)
1929 ml_set_crypt_key(buf, buf->b_p_key, args->os_oldval.string);
1930 }
1931 return NULL;
1932}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001933
1934 int
1935expand_set_cryptmethod(optexpand_T *args, int *numMatches, char_u ***matches)
1936{
1937 return expand_set_opt_string(
1938 args,
1939 p_cm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001940 ARRAY_LENGTH(p_cm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001941 numMatches,
1942 matches);
1943}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001944#endif
1945
1946#if (defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1947/*
1948 * The 'cscopequickfix' option is changed.
1949 */
1950 char *
1951did_set_cscopequickfix(optset_T *args UNUSED)
1952{
1953 char_u *p;
1954
1955 if (p_csqf == NULL)
1956 return NULL;
1957
1958 p = p_csqf;
1959 while (*p != NUL)
1960 {
1961 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
1962 || p[1] == NUL
1963 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
1964 || (p[2] != NUL && p[2] != ','))
1965 return e_invalid_argument;
1966 else if (p[2] == NUL)
1967 break;
1968 else
1969 p += 3;
1970 }
1971
1972 return NULL;
1973}
1974#endif
1975
1976#if defined(FEAT_SYN_HL) || defined(PROTO)
1977/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001978 * The 'cursorlineopt' option is changed.
1979 */
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001980 char *
1981did_set_cursorlineopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001982{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001983 char_u **varp = (char_u **)args->os_varp;
1984
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001985 // This could be changed to use opt_strings_flags() instead.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001986 if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001987 return e_invalid_argument;
1988
1989 return NULL;
1990}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001991
1992 int
1993expand_set_cursorlineopt(optexpand_T *args, int *numMatches, char_u ***matches)
1994{
1995 return expand_set_opt_string(
1996 args,
1997 p_culopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001998 ARRAY_LENGTH(p_culopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001999 numMatches,
2000 matches);
2001}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002002#endif
2003
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002004/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002005 * The 'debug' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002006 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002007 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002008did_set_debug(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002009{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002010 return did_set_opt_strings(p_debug, p_debug_values, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002011}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002012
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002013 int
2014expand_set_debug(optexpand_T *args, int *numMatches, char_u ***matches)
2015{
2016 return expand_set_opt_string(
2017 args,
2018 p_debug_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002019 ARRAY_LENGTH(p_debug_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002020 numMatches,
2021 matches);
2022}
2023
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002024#if defined(FEAT_DIFF) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002025/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002026 * The 'diffopt' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002027 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002028 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002029did_set_diffopt(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002030{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002031 if (diffopt_changed() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002032 return e_invalid_argument;
2033
2034 return NULL;
2035}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002036
2037 int
2038expand_set_diffopt(optexpand_T *args, int *numMatches, char_u ***matches)
2039{
2040 expand_T *xp = args->oe_xp;
2041
2042 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2043 {
2044 // Within "algorithm:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002045 int algo_len = (int)STRLEN("algorithm:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002046 if (xp->xp_pattern - args->oe_set_arg >= algo_len &&
2047 STRNCMP(xp->xp_pattern - algo_len, "algorithm:", algo_len) == 0)
2048 {
2049 return expand_set_opt_string(
2050 args,
2051 p_dip_algorithm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002052 ARRAY_LENGTH(p_dip_algorithm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002053 numMatches,
2054 matches);
2055 }
Yee Cheng Chin9943d472025-03-26 19:41:02 +01002056 // Within "inline:", we have a subgroup of possible options.
2057 int inline_len = (int)STRLEN("inline:");
2058 if (xp->xp_pattern - args->oe_set_arg >= inline_len &&
2059 STRNCMP(xp->xp_pattern - inline_len, "inline:", inline_len) == 0)
2060 {
2061 return expand_set_opt_string(
2062 args,
2063 p_dip_inline_values,
2064 ARRAY_LENGTH(p_dip_inline_values) - 1,
2065 numMatches,
2066 matches);
2067 }
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002068 return FAIL;
2069 }
2070
2071 return expand_set_opt_string(
2072 args,
2073 p_dip_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002074 ARRAY_LENGTH(p_dip_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002075 numMatches,
2076 matches);
2077}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002078#endif
2079
2080/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002081 * The 'display' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002082 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002083 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002084did_set_display(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002085{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002086 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002087 return e_invalid_argument;
2088
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002089 (void)init_chartab();
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002090 return NULL;
2091}
2092
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002093 int
2094expand_set_display(optexpand_T *args, int *numMatches, char_u ***matches)
2095{
2096 return expand_set_opt_string(
2097 args,
2098 p_dy_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002099 ARRAY_LENGTH(p_dy_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002100 numMatches,
2101 matches);
2102}
2103
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002104/*
2105 * The 'eadirection' option is changed.
2106 */
2107 char *
2108did_set_eadirection(optset_T *args UNUSED)
2109{
2110 return did_set_opt_strings(p_ead, p_ead_values, FALSE);
2111}
2112
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002113 int
2114expand_set_eadirection(optexpand_T *args, int *numMatches, char_u ***matches)
2115{
2116 return expand_set_opt_string(
2117 args,
2118 p_ead_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002119 ARRAY_LENGTH(p_ead_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002120 numMatches,
2121 matches);
2122}
2123
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002124/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002125 * One of the 'encoding', 'fileencoding', 'termencoding' or 'makeencoding'
2126 * options is changed.
2127 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002128 char *
2129did_set_encoding(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002130{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002131 char_u **varp = (char_u **)args->os_varp;
2132 char_u **gvarp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002133 char *errmsg = NULL;
2134 char_u *p;
2135
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002136 // Get the global option to compare with, otherwise we would have to check
2137 // two values for all local options.
2138 gvarp = (char_u **)get_option_varp_scope(args->os_idx, OPT_GLOBAL);
2139
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002140 if (gvarp == &p_fenc)
2141 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002142 if (!curbuf->b_p_ma && args->os_flags != OPT_GLOBAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002143 errmsg = e_cannot_make_changes_modifiable_is_off;
2144 else if (vim_strchr(*varp, ',') != NULL)
2145 // No comma allowed in 'fileencoding'; catches confusing it
2146 // with 'fileencodings'.
2147 errmsg = e_invalid_argument;
2148 else
2149 {
2150 // May show a "+" in the title now.
2151 redraw_titles();
2152 // Add 'fileencoding' to the swap file.
2153 ml_setflags(curbuf);
2154 }
2155 }
2156 if (errmsg == NULL)
2157 {
2158 // canonize the value, so that STRCMP() can be used on it
2159 p = enc_canonize(*varp);
2160 if (p != NULL)
2161 {
2162 vim_free(*varp);
2163 *varp = p;
2164 }
2165 if (varp == &p_enc)
2166 {
2167 errmsg = mb_init();
2168 redraw_titles();
2169 }
2170 }
2171
2172#if defined(FEAT_GUI_GTK)
2173 if (errmsg == NULL && varp == &p_tenc && gui.in_use)
2174 {
2175 // GTK uses only a single encoding, and that is UTF-8.
2176 if (STRCMP(p_tenc, "utf-8") != 0)
2177 errmsg = e_cannot_be_changed_in_gtk_GUI;
2178 }
2179#endif
2180
2181 if (errmsg == NULL)
2182 {
2183#ifdef FEAT_KEYMAP
2184 // When 'keymap' is used and 'encoding' changes, reload the keymap
2185 // (with another encoding).
2186 if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
2187 (void)keymap_init();
2188#endif
2189
2190 // When 'termencoding' is not empty and 'encoding' changes or when
2191 // 'termencoding' changes, need to setup for keyboard input and
2192 // display output conversion.
2193 if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
2194 {
2195 if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
2196 || convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
2197 {
2198 semsg(_(e_cannot_convert_between_str_and_str),
2199 p_tenc, p_enc);
2200 errmsg = e_invalid_argument;
2201 }
2202 }
2203
2204#if defined(MSWIN)
K.Takatace3189d2023-02-15 19:13:43 +00002205 // $HOME, $VIM and $VIMRUNTIME may have characters in active code page.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002206 if (varp == &p_enc)
K.Takatace3189d2023-02-15 19:13:43 +00002207 {
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002208 init_homedir();
K.Takatace3189d2023-02-15 19:13:43 +00002209 init_vimdir();
2210 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002211#endif
2212 }
2213
2214 return errmsg;
2215}
2216
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002217 int
2218expand_set_encoding(optexpand_T *args, int *numMatches, char_u ***matches)
2219{
2220 return expand_set_opt_generic(
2221 args,
2222 get_encoding_name,
2223 numMatches,
2224 matches);
2225}
2226
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002227/*
Luuk van Baalb7147f82025-02-08 18:52:39 +01002228 * The 'eventignore(win)' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002229 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002230 char *
Luuk van Baalb7147f82025-02-08 18:52:39 +01002231did_set_eventignore(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002232{
Luuk van Baalb7147f82025-02-08 18:52:39 +01002233 char_u **varp = (char_u **)args->os_varp;
2234
2235 if (check_ei(*varp) == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002236 return e_invalid_argument;
2237 return NULL;
2238}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002239
Luuk van Baalb7147f82025-02-08 18:52:39 +01002240static int expand_eiw = FALSE;
2241
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002242 static char_u *
2243get_eventignore_name(expand_T *xp, int idx)
2244{
Luuk van Baalb7147f82025-02-08 18:52:39 +01002245 // 'eventignore(win)' allows special keyword "all" in addition to
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002246 // all event names.
2247 if (idx == 0)
2248 return (char_u *)"all";
Luuk van Baalb7147f82025-02-08 18:52:39 +01002249 return get_event_name_no_group(xp, idx - 1, expand_eiw);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002250}
2251
2252 int
2253expand_set_eventignore(optexpand_T *args, int *numMatches, char_u ***matches)
2254{
Luuk van Baalb7147f82025-02-08 18:52:39 +01002255 expand_eiw = args->oe_varp != (char_u *)&p_ei;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002256 return expand_set_opt_generic(
2257 args,
2258 get_eventignore_name,
2259 numMatches,
2260 matches);
2261}
2262
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002263/*
2264 * The 'fileformat' option is changed.
2265 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002266 char *
2267did_set_fileformat(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002268{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002269 char_u **varp = (char_u **)args->os_varp;
2270
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002271 if (!curbuf->b_p_ma && !(args->os_flags & OPT_GLOBAL))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002272 return e_cannot_make_changes_modifiable_is_off;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002273 else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002274 return e_invalid_argument;
2275
2276 // may also change 'textmode'
2277 if (get_fileformat(curbuf) == EOL_DOS)
2278 curbuf->b_p_tx = TRUE;
2279 else
2280 curbuf->b_p_tx = FALSE;
2281 redraw_titles();
2282 // update flag in swap file
2283 ml_setflags(curbuf);
2284 // Redraw needed when switching to/from "mac": a CR in the text
2285 // will be displayed differently.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002286 if (get_fileformat(curbuf) == EOL_MAC || *args->os_oldval.string == 'm')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002287 redraw_curbuf_later(UPD_NOT_VALID);
2288
2289 return NULL;
2290}
2291
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002292 int
2293expand_set_fileformat(optexpand_T *args, int *numMatches, char_u ***matches)
2294{
2295 return expand_set_opt_string(
2296 args,
2297 p_ff_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002298 ARRAY_LENGTH(p_ff_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002299 numMatches,
2300 matches);
2301}
2302
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002303/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002304 * Function given to ExpandGeneric() to obtain the possible arguments of the
2305 * fileformat options.
2306 */
2307 char_u *
2308get_fileformat_name(expand_T *xp UNUSED, int idx)
2309{
2310 if (idx >= (int)ARRAY_LENGTH(p_ff_values))
2311 return NULL;
2312
2313 return (char_u*)p_ff_values[idx];
2314}
2315
2316/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002317 * The 'fileformats' option is changed.
2318 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002319 char *
2320did_set_fileformats(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002321{
2322 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
2323 return e_invalid_argument;
2324
2325 // also change 'textauto'
2326 if (*p_ffs == NUL)
2327 p_ta = FALSE;
2328 else
2329 p_ta = TRUE;
2330
2331 return NULL;
2332}
2333
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002334/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002335 * The 'filetype' or the 'syntax' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002336 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002337 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002338did_set_filetype_or_syntax(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002339{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002340 char_u **varp = (char_u **)args->os_varp;
2341
2342 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002343 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002344
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002345 args->os_value_changed = STRCMP(args->os_oldval.string, *varp) != 0;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002346
2347 // Since we check the value, there is no need to set P_INSECURE,
2348 // even when the value comes from a modeline.
2349 args->os_value_checked = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002350
2351 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002352}
2353
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002354#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002355/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002356 * The 'foldclose' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002357 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002358 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002359did_set_foldclose(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002360{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002361 return did_set_opt_strings(p_fcl, p_fcl_values, TRUE);
2362}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002363
2364 int
2365expand_set_foldclose(optexpand_T *args, int *numMatches, char_u ***matches)
2366{
2367 return expand_set_opt_string(
2368 args,
2369 p_fcl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002370 ARRAY_LENGTH(p_fcl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002371 numMatches,
2372 matches);
2373}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002374#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002375
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002376#if (defined(FEAT_EVAL) && defined(FEAT_FOLDING)) || defined(PROTO)
2377/*
2378 * The 'foldexpr' option is changed.
2379 */
2380 char *
2381did_set_foldexpr(optset_T *args)
2382{
2383 (void)did_set_optexpr(args);
2384 if (foldmethodIsExpr(curwin))
2385 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002386 return NULL;
2387}
2388#endif
2389
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002390#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002391/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002392 * The 'foldignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002393 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002394 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002395did_set_foldignore(optset_T *args UNUSED)
2396{
2397 if (foldmethodIsIndent(curwin))
2398 foldUpdateAll(curwin);
2399 return NULL;
2400}
2401
2402/*
2403 * The 'foldmarker' option is changed.
2404 */
2405 char *
2406did_set_foldmarker(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002407{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002408 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002409 char_u *p;
2410
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002411 p = vim_strchr(*varp, ',');
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002412 if (p == NULL)
2413 return e_comma_required;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002414 else if (p == *varp || p[1] == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002415 return e_invalid_argument;
2416 else if (foldmethodIsMarker(curwin))
2417 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002418
2419 return NULL;
2420}
2421
2422/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002423 * The 'foldmethod' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002424 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002425 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002426did_set_foldmethod(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002427{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002428 char_u **varp = (char_u **)args->os_varp;
2429
Milly142cad12024-10-22 22:11:51 +02002430 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK || **varp == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002431 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002432
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002433 foldUpdateAll(curwin);
2434 if (foldmethodIsDiff(curwin))
2435 newFoldLevel();
2436 return NULL;
2437}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002438
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002439 int
2440expand_set_foldmethod(optexpand_T *args, int *numMatches, char_u ***matches)
2441{
2442 return expand_set_opt_string(
2443 args,
2444 p_fdm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002445 ARRAY_LENGTH(p_fdm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002446 numMatches,
2447 matches);
2448}
2449
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002450/*
2451 * The 'foldopen' option is changed.
2452 */
2453 char *
2454did_set_foldopen(optset_T *args UNUSED)
2455{
2456 return did_set_opt_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
2457}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002458
2459 int
2460expand_set_foldopen(optexpand_T *args, int *numMatches, char_u ***matches)
2461{
2462 return expand_set_opt_string(
2463 args,
2464 p_fdo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002465 ARRAY_LENGTH(p_fdo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002466 numMatches,
2467 matches);
2468}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002469#endif
2470
2471/*
2472 * The 'formatoptions' option is changed.
2473 */
2474 char *
2475did_set_formatoptions(optset_T *args)
2476{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002477 char_u **varp = (char_u **)args->os_varp;
2478
Christian Brabandtb39b2402023-11-29 11:34:05 +01002479 return did_set_option_listflag(*varp, (char_u *)FO_ALL, args->os_errbuf,
2480 args->os_errbuflen);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002481}
2482
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002483 int
2484expand_set_formatoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2485{
2486 return expand_set_opt_listflag(args, (char_u*)FO_ALL, numMatches, matches);
2487}
2488
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002489#if defined(CURSOR_SHAPE) || defined(PROTO)
2490/*
2491 * The 'guicursor' option is changed.
2492 */
2493 char *
2494did_set_guicursor(optset_T *args UNUSED)
2495{
2496 return parse_shape_opt(SHAPE_CURSOR);
2497}
2498#endif
2499
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002500#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002501/*
2502 * The 'guifont' option is changed.
2503 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002504 char *
2505did_set_guifont(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002506{
2507 char_u *p;
2508 char *errmsg = NULL;
2509
2510 if (gui.in_use)
2511 {
2512 p = p_guifont;
2513# if defined(FEAT_GUI_GTK)
2514 // Put up a font dialog and let the user select a new value.
2515 // If this is cancelled go back to the old value but don't
2516 // give an error message.
2517 if (STRCMP(p, "*") == 0)
2518 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002519 p = gui_mch_font_dialog(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002520 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002521 p_guifont = (p != NULL) ? p : vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002522 }
2523# endif
2524 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
2525 {
2526# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
2527 if (STRCMP(p_guifont, "*") == 0)
2528 {
2529 // Dialog was cancelled: Keep the old value without giving
2530 // an error message.
2531 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002532 p_guifont = vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002533 }
2534 else
2535# endif
2536 errmsg = e_invalid_fonts;
2537 }
2538 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002539
2540 return errmsg;
2541}
2542
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002543/*
2544 * Expand the 'guifont' option. Only when GUI is being used. Each platform has
2545 * specific behaviors.
2546 */
2547 int
2548expand_set_guifont(optexpand_T *args, int *numMatches, char_u ***matches)
2549{
2550 if (!gui.in_use)
2551 return FAIL;
2552
2553# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
2554 char_u **varp = (char_u **)args->oe_varp;
2555 int wide = (varp == &p_guifontwide);
2556
2557 return expand_set_opt_callback(
2558 args, gui_mch_expand_font, &wide, numMatches, matches);
2559# else
2560 return FAIL;
2561# endif
2562}
2563
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002564# if defined(FEAT_XFONTSET) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002565/*
2566 * The 'guifontset' option is changed.
2567 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002568 char *
2569did_set_guifontset(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002570{
2571 char *errmsg = NULL;
2572
2573 if (STRCMP(p_guifontset, "*") == 0)
2574 errmsg = e_cant_select_fontset;
2575 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
2576 errmsg = e_invalid_fontset;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002577
2578 return errmsg;
2579}
2580# endif
2581
2582/*
2583 * The 'guifontwide' option is changed.
2584 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002585 char *
2586did_set_guifontwide(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002587{
2588 char *errmsg = NULL;
2589
2590 if (STRCMP(p_guifontwide, "*") == 0)
2591 errmsg = e_cant_select_wide_font;
2592 else if (gui_get_wide_font() == FAIL)
2593 errmsg = e_invalid_wide_font;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002594
2595 return errmsg;
2596}
2597#endif
2598
Erik S. V. Jansson8b1e7492024-02-24 14:26:52 +01002599#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002600/*
2601 * The 'guiligatures' option is changed.
2602 */
2603 char *
2604did_set_guiligatures(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002605{
2606 gui_set_ligatures();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002607 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002608}
2609#endif
2610
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002611#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002612/*
2613 * The 'guioptions' option is changed.
2614 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002615 char *
2616did_set_guioptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002617{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002618 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002619 char *errmsg;
2620
Christian Brabandtb39b2402023-11-29 11:34:05 +01002621 errmsg = did_set_option_listflag(*varp, (char_u *)GO_ALL, args->os_errbuf,
2622 args->os_errbuflen);
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002623 if (errmsg != NULL)
2624 return errmsg;
2625
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002626 gui_init_which_components(args->os_oldval.string);
2627 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002628}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002629
2630 int
2631expand_set_guioptions(optexpand_T *args, int *numMatches, char_u ***matches)
2632{
2633 return expand_set_opt_listflag(args, (char_u*)GO_ALL, numMatches, matches);
2634}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002635#endif
2636
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002637#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002638/*
2639 * The 'guitablabel' option is changed.
2640 */
2641 char *
2642did_set_guitablabel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002643{
2644 redraw_tabline = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002645 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002646}
2647#endif
2648
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002649/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002650 * The 'helpfile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002651 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002652 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002653did_set_helpfile(optset_T *args UNUSED)
2654{
2655 // May compute new values for $VIM and $VIMRUNTIME
2656 if (didset_vim)
2657 vim_unsetenv_ext((char_u *)"VIM");
2658 if (didset_vimruntime)
2659 vim_unsetenv_ext((char_u *)"VIMRUNTIME");
2660 return NULL;
2661}
2662
2663#if defined(FEAT_MULTI_LANG) || defined(PROTO)
2664/*
2665 * The 'helplang' option is changed.
2666 */
2667 char *
2668did_set_helplang(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002669{
2670 char *errmsg = NULL;
2671
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002672 // Check for "", "ab", "ab,cd", etc.
2673 for (char_u *s = p_hlg; *s != NUL; s += 3)
2674 {
2675 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
2676 {
2677 errmsg = e_invalid_argument;
2678 break;
2679 }
2680 if (s[2] == NUL)
2681 break;
2682 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002683
2684 return errmsg;
2685}
2686#endif
2687
2688/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002689 * The 'highlight' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002690 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002691 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002692did_set_highlight(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002693{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002694 if (highlight_changed() == FAIL)
2695 return e_invalid_argument; // invalid flags
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002696
2697 return NULL;
2698}
2699
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002700/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002701 * Expand 'highlight' option.
2702 */
2703 int
2704expand_set_highlight(optexpand_T *args, int *numMatches, char_u ***matches)
2705{
2706 char_u *p;
2707 expand_T *xp = args->oe_xp;
2708 static char_u hl_flags[HLF_COUNT] = HL_FLAGS;
Christian Brabandt3f168ec2023-10-02 23:21:11 +02002709 size_t i;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002710 int count = 0;
2711
2712 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2713 {
2714 // Right after a ':', meaning we just return all highlight names.
2715 return expand_set_opt_generic(
2716 args,
2717 get_highlight_name,
2718 numMatches,
2719 matches);
2720 }
2721
2722 if (*xp->xp_pattern == NUL)
2723 {
2724 // At beginning of a comma-separated list. Return the specific list of
2725 // supported occasions.
2726 *matches = ALLOC_MULT(char_u *, HLF_COUNT + 1);
2727 if (*matches == NULL)
2728 return FAIL;
2729
2730 // We still want to return the full option if it's requested.
2731 if (args->oe_include_orig_val)
2732 {
2733 p = vim_strsave(args->oe_opt_value);
2734 if (p == NULL)
2735 {
2736 VIM_CLEAR(*matches);
2737 return FAIL;
2738 }
2739 (*matches)[count++] = p;
2740 }
2741
2742 for (i = 0; i < HLF_COUNT; i++)
2743 {
2744 p = vim_strnsave(&hl_flags[i], 1);
2745 if (p == NULL)
2746 {
2747 if (count == 0)
2748 {
2749 VIM_CLEAR(*matches);
2750 return FAIL;
2751 }
2752 else
2753 break;
2754 }
2755 (*matches)[count++] = p;
2756 }
2757
2758 if (count == 0)
2759 {
2760 VIM_CLEAR(*matches);
2761 return FAIL;
2762 }
2763 *numMatches = count;
2764 return OK;
2765 }
2766
2767 // We are after the initial character (which indicates the occasion). We
2768 // already made sure we are not matching after a ':' above, so now we want
2769 // to match against display mode modifiers.
2770 // Since the xp_pattern starts from the beginning, we need to include it in
2771 // the returned match.
2772
2773 // Note: Keep this in sync with highlight_changed()
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002774 static char_u p_hl_mode_values[] =
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002775 {':', 'b', 'i', '-', 'n', 'r', 's', 'u', 'c', '2', 'd', '=', 't'};
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002776 size_t num_hl_modes = ARRAY_LENGTH(p_hl_mode_values);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002777
2778 *matches = ALLOC_MULT(char_u *, num_hl_modes);
2779 if (*matches == NULL)
2780 return FAIL;
2781
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002782 int pattern_len = xp->xp_pattern_len;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002783
2784 for (i = 0; i < num_hl_modes; i++)
2785 {
2786 // Don't allow duplicates as these are unique flags
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002787 char_u *dup = vim_strchr(xp->xp_pattern + 1, p_hl_mode_values[i]);
2788 if (dup != NULL && (int)(dup - xp->xp_pattern) < pattern_len)
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002789 continue;
2790
2791 // ':' only works by itself, not with other flags.
2792 if (pattern_len > 1 && p_hl_mode_values[i] == ':')
2793 continue;
2794
2795 p = vim_strnsave(xp->xp_pattern, pattern_len + 1);
2796 if (p == NULL)
2797 {
2798 if (i == 0)
2799 {
2800 VIM_CLEAR(*matches);
2801 return FAIL;
2802 }
2803 else
2804 break;
2805 }
2806 p[pattern_len] = p_hl_mode_values[i];
2807 p[pattern_len + 1] = NUL;
2808 (*matches)[count++] = p;
2809 }
2810 if (count == 0)
2811 {
2812 VIM_CLEAR(*matches);
2813 return FAIL;
2814 }
2815 *numMatches = count;
2816
2817 return OK;
2818}
2819
2820/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002821 * The 'titlestring' or the 'iconstring' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002822 */
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002823 static char *
2824parse_titleiconstring(optset_T *args UNUSED, int flagval UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002825{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002826#ifdef FEAT_STL_OPT
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002827 char_u **varp = (char_u **)args->os_varp;
2828
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002829 // NULL => statusline syntax
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002830 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002831 stl_syntax |= flagval;
2832 else
2833 stl_syntax &= ~flagval;
2834#endif
2835 did_set_title();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002836
2837 return NULL;
2838}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002839
2840/*
2841 * The 'iconstring' option is changed.
2842 */
2843 char *
2844did_set_iconstring(optset_T *args)
2845{
2846 int flagval = 0;
2847
2848#ifdef FEAT_STL_OPT
2849 flagval = STL_IN_ICON;
2850#endif
2851
2852 return parse_titleiconstring(args, flagval);
2853}
2854
2855#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
2856/*
2857 * The 'imactivatekey' option is changed.
2858 */
2859 char *
2860did_set_imactivatekey(optset_T *args UNUSED)
2861{
2862 if (!im_xim_isvalid_imactivate())
2863 return e_invalid_argument;
2864 return NULL;
2865}
2866#endif
2867
2868/*
glepnirbcd59952025-04-24 21:48:35 +02002869 * The 'isexpand' option is changed.
2870 */
2871 char *
2872did_set_isexpand(optset_T *args)
2873{
2874 char_u *ise = p_ise;
2875 char_u *p;
2876 int last_was_comma = FALSE;
2877
2878 if (args->os_flags & OPT_LOCAL)
2879 ise = curbuf->b_p_ise;
2880
2881 for (p = ise; *p != NUL;)
2882 {
2883 if (*p == '\\' && p[1] == ',')
2884 {
2885 p += 2;
2886 last_was_comma = FALSE;
2887 continue;
2888 }
2889
2890 if (*p == ',')
2891 {
2892 if (last_was_comma)
2893 return e_invalid_argument;
2894 last_was_comma = TRUE;
2895 p++;
2896 continue;
2897 }
2898
2899 last_was_comma = FALSE;
2900 MB_PTR_ADV(p);
2901 }
2902
2903 if (last_was_comma)
2904 return e_invalid_argument;
2905
2906 return NULL;
2907}
2908
2909
2910/*
Milly5e7a6a42024-10-22 22:27:19 +02002911 * The 'iskeyword' option is changed.
2912 */
2913 char *
2914did_set_iskeyword(optset_T *args)
2915{
2916 char_u **varp = (char_u **)args->os_varp;
2917
2918 if (varp == &p_isk) // only check for global-value
2919 {
2920 if (check_isopt(*varp) == FAIL)
2921 return e_invalid_argument;
2922 }
2923 else // fallthrough for local-value
2924 return did_set_isopt(args);
2925
2926 return NULL;
2927}
2928
2929/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002930 * The 'isident' or the 'iskeyword' or the 'isprint' or the 'isfname' option is
2931 * changed.
2932 */
2933 char *
2934did_set_isopt(optset_T *args)
2935{
Milly5e7a6a42024-10-22 22:27:19 +02002936 // 'isident', 'iskeyword', 'isprint' or 'isfname' option: refill g_chartab[]
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002937 // If the new option is invalid, use old value.
2938 // 'lisp' option: refill g_chartab[] for '-' char.
2939 if (init_chartab() == FAIL)
2940 {
2941 args->os_restore_chartab = TRUE;// need to restore the chartab.
2942 return e_invalid_argument; // error in value
2943 }
2944
2945 return NULL;
2946}
2947
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002948/*
2949 * The 'jumpoptions' option is changed.
2950 */
2951 char *
Yegappan Lakshmanan1926ae42023-09-21 16:36:28 +02002952did_set_jumpoptions(optset_T *args UNUSED)
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002953{
2954 if (opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE) != OK)
2955 return e_invalid_argument;
2956
2957 return NULL;
2958}
2959
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002960 int
2961expand_set_jumpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2962{
2963 return expand_set_opt_string(
2964 args,
2965 p_jop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002966 ARRAY_LENGTH(p_jop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002967 numMatches,
2968 matches);
2969}
2970
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002971#if defined(FEAT_KEYMAP) || defined(PROTO)
2972/*
2973 * The 'keymap' option is changed.
2974 */
2975 char *
2976did_set_keymap(optset_T *args)
2977{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002978 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002979 char *errmsg = NULL;
2980
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002981 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002982 errmsg = e_invalid_argument;
2983 else
2984 {
2985 int secure_save = secure;
2986
2987 // Reset the secure flag, since the value of 'keymap' has
2988 // been checked to be safe.
2989 secure = 0;
2990
2991 // load or unload key mapping tables
2992 errmsg = keymap_init();
2993
2994 secure = secure_save;
2995
2996 // Since we check the value, there is no need to set P_INSECURE,
2997 // even when the value comes from a modeline.
2998 args->os_value_checked = TRUE;
2999 }
3000
3001 if (errmsg == NULL)
3002 {
3003 if (*curbuf->b_p_keymap != NUL)
3004 {
3005 // Installed a new keymap, switch on using it.
3006 curbuf->b_p_iminsert = B_IMODE_LMAP;
3007 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
3008 curbuf->b_p_imsearch = B_IMODE_LMAP;
3009 }
3010 else
3011 {
3012 // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
3013 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
3014 curbuf->b_p_iminsert = B_IMODE_NONE;
3015 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
3016 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3017 }
3018 if ((args->os_flags & OPT_LOCAL) == 0)
3019 {
3020 set_iminsert_global();
3021 set_imsearch_global();
3022 }
3023 status_redraw_curbuf();
3024 }
3025
3026 return errmsg;
3027}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003028#endif
3029
3030/*
3031 * The 'keymodel' option is changed.
3032 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003033 char *
3034did_set_keymodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003035{
3036 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
3037 return e_invalid_argument;
3038
3039 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
3040 km_startsel = (vim_strchr(p_km, 'a') != NULL);
3041 return NULL;
3042}
3043
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003044 int
3045expand_set_keymodel(optexpand_T *args, int *numMatches, char_u ***matches)
3046{
3047 return expand_set_opt_string(
3048 args,
3049 p_km_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003050 ARRAY_LENGTH(p_km_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003051 numMatches,
3052 matches);
3053}
3054
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003055/*
3056 * The 'keyprotocol' option is changed.
3057 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003058 char *
3059did_set_keyprotocol(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003060{
Gregory Anders3695d0e2023-09-29 20:17:20 +02003061 char_u *term = T_NAME;
3062 keyprot_T kpc = match_keyprotocol(term);
3063 if (kpc == KEYPROTOCOL_FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003064 return e_invalid_argument;
3065
Gregory Anders3695d0e2023-09-29 20:17:20 +02003066 apply_keyprotocol(term, kpc);
3067
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003068 return NULL;
3069}
3070
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003071 int
3072expand_set_keyprotocol(optexpand_T *args, int *numMatches, char_u ***matches)
3073{
3074 expand_T *xp = args->oe_xp;
3075 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3076 {
3077 // 'keyprotocol' only has well-defined terms for completion for the
3078 // protocol part after the colon.
3079 return expand_set_opt_string(
3080 args,
3081 p_kpc_protocol_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003082 ARRAY_LENGTH(p_kpc_protocol_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003083 numMatches,
3084 matches);
3085 }
3086 // Use expand_set_opt_string instead of returning FAIL so that we can
3087 // include the original value if args->oe_include_orig_val is set.
3088 static char *(empty[]) = {NULL};
3089 return expand_set_opt_string(args, empty, 0, numMatches, matches);
3090}
3091
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003092/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003093 * The 'lispoptions' option is changed.
3094 */
3095 char *
3096did_set_lispoptions(optset_T *args)
3097{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003098 char_u **varp = (char_u **)args->os_varp;
3099
3100 if (**varp != NUL
3101 && STRCMP(*varp, "expr:0") != 0
3102 && STRCMP(*varp, "expr:1") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003103 return e_invalid_argument;
3104
3105 return NULL;
3106}
3107
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003108 int
3109expand_set_lispoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3110{
3111 static char *(p_lop_values[]) = {"expr:0", "expr:1", NULL};
3112 return expand_set_opt_string(
3113 args,
3114 p_lop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003115 ARRAY_LENGTH(p_lop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003116 numMatches,
3117 matches);
3118}
3119
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003120/*
3121 * The 'matchpairs' option is changed.
3122 */
3123 char *
3124did_set_matchpairs(optset_T *args)
3125{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003126 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003127 char_u *p;
3128
3129 if (has_mbyte)
3130 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003131 for (p = *varp; *p != NUL; ++p)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003132 {
3133 int x2 = -1;
3134 int x3 = -1;
3135
3136 p += mb_ptr2len(p);
3137 if (*p != NUL)
3138 x2 = *p++;
3139 if (*p != NUL)
3140 {
3141 x3 = mb_ptr2char(p);
3142 p += mb_ptr2len(p);
3143 }
3144 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
3145 return e_invalid_argument;
3146 if (*p == NUL)
3147 break;
3148 }
3149 }
3150 else
3151 {
3152 // Check for "x:y,x:y"
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003153 for (p = *varp; *p != NUL; p += 4)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003154 {
3155 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
3156 return e_invalid_argument;
3157 if (p[3] == NUL)
3158 break;
3159 }
3160 }
3161
3162 return NULL;
3163}
3164
Christian Brabandt51d4d842024-12-06 17:26:25 +01003165/*
3166 * Process the updated 'messagesopt' option value.
3167 */
3168 char *
3169did_set_messagesopt(optset_T *args UNUSED)
3170{
3171 if (messagesopt_changed() == FAIL)
3172 return e_invalid_argument;
3173
3174 return NULL;
3175}
3176
3177 int
3178expand_set_messagesopt(optexpand_T *args, int *numMatches, char_u ***matches)
3179{
zeertzjq8cc43da2024-12-07 16:09:08 +01003180 static char *(p_mopt_values[]) = {"hit-enter", "wait:", "history:", NULL};
Christian Brabandt51d4d842024-12-06 17:26:25 +01003181 return expand_set_opt_string(
3182 args,
zeertzjq8cc43da2024-12-07 16:09:08 +01003183 p_mopt_values,
3184 ARRAY_LENGTH(p_mopt_values) - 1,
Christian Brabandt51d4d842024-12-06 17:26:25 +01003185 numMatches,
3186 matches);
3187}
3188
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003189#if defined(FEAT_SPELL) || defined(PROTO)
3190/*
3191 * The 'mkspellmem' option is changed.
3192 */
3193 char *
3194did_set_mkspellmem(optset_T *args UNUSED)
3195{
3196 if (spell_check_msm() != OK)
3197 return e_invalid_argument;
3198
3199 return NULL;
3200}
3201#endif
3202
3203/*
3204 * The 'mouse' option is changed.
3205 */
3206 char *
3207did_set_mouse(optset_T *args)
3208{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003209 char_u **varp = (char_u **)args->os_varp;
3210
Christian Brabandtb39b2402023-11-29 11:34:05 +01003211 return did_set_option_listflag(*varp, (char_u *)MOUSE_ALL, args->os_errbuf,
3212 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003213}
3214
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003215 int
3216expand_set_mouse(optexpand_T *args, int *numMatches, char_u ***matches)
3217{
3218 return expand_set_opt_listflag(args, (char_u*)MOUSE_ALL, numMatches, matches);
3219}
3220
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003221/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003222 * The 'mousemodel' option is changed.
3223 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003224 char *
3225did_set_mousemodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003226{
3227 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
3228 return e_invalid_argument;
3229#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
Ken Takata18d0d292023-12-19 20:12:29 +01003230 else if (*p_mousem != *args->os_oldval.string)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003231 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
3232 // to create or delete the popup menus.
3233 gui_motif_update_mousemodel(root_menu);
3234#endif
3235
3236 return NULL;
3237}
3238
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003239 int
3240expand_set_mousemodel(optexpand_T *args, int *numMatches, char_u ***matches)
3241{
3242 return expand_set_opt_string(
3243 args,
3244 p_mousem_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003245 ARRAY_LENGTH(p_mousem_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003246 numMatches,
3247 matches);
3248}
3249
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003250#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3251 char *
3252did_set_mouseshape(optset_T *args UNUSED)
3253{
3254 char *errmsg = NULL;
3255
3256 errmsg = parse_shape_opt(SHAPE_MOUSE);
3257 update_mouseshape(-1);
3258
3259 return errmsg;
3260}
3261#endif
3262
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003263/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003264 * The 'nrformats' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003265 */
3266 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003267did_set_nrformats(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003268{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003269 char_u **varp = (char_u **)args->os_varp;
3270
3271 return did_set_opt_strings(*varp, p_nf_values, TRUE);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003272}
3273
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003274 int
3275expand_set_nrformats(optexpand_T *args, int *numMatches, char_u ***matches)
3276{
3277 return expand_set_opt_string(
3278 args,
3279 p_nf_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003280 ARRAY_LENGTH(p_nf_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003281 numMatches,
3282 matches);
3283}
3284
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003285#if defined(FEAT_EVAL) || defined(PROTO)
3286/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003287 * One of the '*expr' options is changed: 'balloonexpr', 'diffexpr',
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003288 * 'foldexpr', 'foldtext', 'formatexpr', 'includeexpr', 'indentexpr',
3289 * 'patchexpr', 'printexpr' and 'charconvert'.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003290 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003291 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003292did_set_optexpr(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003293{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003294 char_u **varp = (char_u **)args->os_varp;
3295
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003296 // If the option value starts with <SID> or s:, then replace that with
3297 // the script identifier.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003298 char_u *name = get_scriptlocal_funcname(*varp);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003299 if (name != NULL)
3300 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003301 free_string_option(*varp);
3302 *varp = name;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003303 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003304
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003305 return NULL;
3306}
3307#endif
3308
3309/*
3310 * The 'pastetoggle' option is changed.
3311 */
3312 char *
3313did_set_pastetoggle(optset_T *args UNUSED)
3314{
3315 char_u *p;
3316
3317 // translate key codes like in a mapping
3318 if (*p_pt)
3319 {
zeertzjq7e0bae02023-08-11 23:15:38 +02003320 (void)replace_termcodes(p_pt, &p, 0,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003321 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
3322 if (p != NULL)
3323 {
3324 free_string_option(p_pt);
3325 p_pt = p;
3326 }
3327 }
3328
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003329 return NULL;
3330}
3331
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003332#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3333/*
3334 * The 'previewpopup' option is changed.
3335 */
3336 char *
3337did_set_previewpopup(optset_T *args UNUSED)
3338{
3339 if (parse_previewpopup(NULL) == FAIL)
3340 return e_invalid_argument;
3341
3342 return NULL;
3343}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003344
3345 int
3346expand_set_popupoption(optexpand_T *args, int *numMatches, char_u ***matches)
3347{
3348 expand_T *xp = args->oe_xp;
3349
3350 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3351 {
3352 // Within "highlight:"/"border:"/"align:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003353 int border_len = (int)STRLEN("border:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003354 if (xp->xp_pattern - args->oe_set_arg >= border_len &&
3355 STRNCMP(xp->xp_pattern - border_len, "border:", border_len) == 0)
3356 {
3357 return expand_set_opt_string(
3358 args,
3359 p_popup_option_border_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003360 ARRAY_LENGTH(p_popup_option_border_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003361 numMatches,
3362 matches);
3363 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003364 int align_len = (int)STRLEN("align:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003365 if (xp->xp_pattern - args->oe_set_arg >= align_len &&
3366 STRNCMP(xp->xp_pattern - align_len, "align:", align_len) == 0)
3367 {
3368 return expand_set_opt_string(
3369 args,
3370 p_popup_option_align_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003371 ARRAY_LENGTH(p_popup_option_align_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003372 numMatches,
3373 matches);
3374 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003375 int highlight_len = (int)STRLEN("highlight:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003376 if (xp->xp_pattern - args->oe_set_arg >= highlight_len &&
3377 STRNCMP(xp->xp_pattern - highlight_len, "highlight:", highlight_len) == 0)
3378 {
3379 // Return the list of all highlight names
3380 return expand_set_opt_generic(
3381 args,
3382 get_highlight_name,
3383 numMatches,
3384 matches);
3385 }
3386 return FAIL;
3387 }
3388
3389 return expand_set_opt_string(
3390 args,
3391 p_popup_option_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003392 ARRAY_LENGTH(p_popup_option_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003393 numMatches,
3394 matches);
3395}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003396#endif
3397
3398#if defined(FEAT_POSTSCRIPT) || defined(PROTO)
3399/*
3400 * The 'printencoding' option is changed.
3401 */
3402 char *
3403did_set_printencoding(optset_T *args UNUSED)
3404{
3405 char_u *s, *p;
3406
3407 // Canonize 'printencoding' if VIM standard one
3408 p = enc_canonize(p_penc);
3409 if (p != NULL)
3410 {
3411 vim_free(p_penc);
3412 p_penc = p;
3413 }
3414 else
3415 {
3416 // Ensure lower case and '-' for '_'
3417 for (s = p_penc; *s != NUL; s++)
3418 {
3419 if (*s == '_')
3420 *s = '-';
3421 else
3422 *s = TOLOWER_ASC(*s);
3423 }
3424 }
3425
3426 return NULL;
3427}
3428#endif
3429
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003430#if defined(FEAT_PRINTER) || defined(PROTO)
3431
3432 static char_u *
3433get_printoptions_names(expand_T *xp UNUSED, int idx)
3434{
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003435 if (idx >= (int)ARRAY_LENGTH(printer_opts))
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003436 return NULL;
3437 return (char_u*)printer_opts[idx].name;
3438}
3439
3440/*
3441 * Expand 'printoptions' option
3442 */
3443 int
3444expand_set_printoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3445{
3446 return expand_set_opt_generic(
3447 args,
3448 get_printoptions_names,
3449 numMatches,
3450 matches);
3451}
3452#endif
3453
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003454#if defined(FEAT_STL_OPT) || defined(PROTO)
3455/*
3456 * The 'statusline' or the 'tabline' or the 'rulerformat' option is changed.
3457 * "rulerformat" is TRUE if the 'rulerformat' option is changed.
3458 */
3459 static char *
3460parse_statustabline_rulerformat(optset_T *args, int rulerformat)
3461{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003462 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003463 char_u *s;
3464 char *errmsg = NULL;
3465 int wid;
3466
3467 if (rulerformat) // reset ru_wid first
3468 ru_wid = 0;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003469 s = *varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003470 if (rulerformat && *s == '%')
3471 {
3472 // set ru_wid if 'ruf' starts with "%99("
3473 if (*++s == '-') // ignore a '-'
3474 s++;
3475 wid = getdigits(&s);
3476 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
3477 ru_wid = wid;
3478 else
Yegappan Lakshmananac023e82024-11-27 20:55:45 +01003479 {
3480 // Validate the flags in 'rulerformat' only if it doesn't point to
3481 // a custom function ("%!" flag).
3482 if ((*varp)[1] != '!')
3483 errmsg = check_stl_option(p_ruf);
3484 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003485 }
3486 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
3487 else if (rulerformat || s[0] != '%' || s[1] != '!')
3488 errmsg = check_stl_option(s);
3489 if (rulerformat && errmsg == NULL)
3490 comp_col();
3491
3492 return errmsg;
3493}
3494#endif
3495
3496#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
3497/*
3498 * The 'renderoptions' option is changed.
3499 */
3500 char *
3501did_set_renderoptions(optset_T *args UNUSED)
3502{
3503 if (!gui_mch_set_rendering_options(p_rop))
3504 return e_invalid_argument;
3505
3506 return NULL;
3507}
3508#endif
3509
3510#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3511/*
3512 * The 'rightleftcmd' option is changed.
3513 */
3514 char *
3515did_set_rightleftcmd(optset_T *args)
3516{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003517 char_u **varp = (char_u **)args->os_varp;
3518
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003519 // Currently only "search" is a supported value.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003520 if (**varp != NUL && STRCMP(*varp, "search") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003521 return e_invalid_argument;
3522
3523 return NULL;
3524}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003525
3526 int
3527expand_set_rightleftcmd(optexpand_T *args, int *numMatches, char_u ***matches)
3528{
3529 static char *(p_rlc_values[]) = {"search", NULL};
3530 return expand_set_opt_string(
3531 args,
3532 p_rlc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003533 ARRAY_LENGTH(p_rlc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003534 numMatches,
3535 matches);
3536}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003537#endif
3538
3539#if defined(FEAT_STL_OPT) || defined(PROTO)
3540/*
3541 * The 'rulerformat' option is changed.
3542 */
3543 char *
3544did_set_rulerformat(optset_T *args)
3545{
3546 return parse_statustabline_rulerformat(args, TRUE);
3547}
3548#endif
3549
3550/*
3551 * The 'scrollopt' option is changed.
3552 */
3553 char *
3554did_set_scrollopt(optset_T *args UNUSED)
3555{
3556 return did_set_opt_strings(p_sbo, p_scbopt_values, TRUE);
3557}
3558
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003559 int
3560expand_set_scrollopt(optexpand_T *args, int *numMatches, char_u ***matches)
3561{
3562 return expand_set_opt_string(
3563 args,
3564 p_scbopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003565 ARRAY_LENGTH(p_scbopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003566 numMatches,
3567 matches);
3568}
3569
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003570/*
3571 * The 'selection' option is changed.
3572 */
3573 char *
3574did_set_selection(optset_T *args UNUSED)
3575{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003576 if (*p_sel == NUL || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003577 return e_invalid_argument;
3578
3579 return NULL;
3580}
3581
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003582 int
3583expand_set_selection(optexpand_T *args, int *numMatches, char_u ***matches)
3584{
3585 return expand_set_opt_string(
3586 args,
3587 p_sel_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003588 ARRAY_LENGTH(p_sel_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003589 numMatches,
3590 matches);
3591}
3592
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003593/*
3594 * The 'selectmode' option is changed.
3595 */
3596 char *
3597did_set_selectmode(optset_T *args UNUSED)
3598{
3599 return did_set_opt_strings(p_slm, p_slm_values, TRUE);
3600}
3601
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003602 int
3603expand_set_selectmode(optexpand_T *args, int *numMatches, char_u ***matches)
3604{
3605 return expand_set_opt_string(
3606 args,
3607 p_slm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003608 ARRAY_LENGTH(p_slm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003609 numMatches,
3610 matches);
3611}
3612
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003613#if defined(FEAT_SESSION) || defined(PROTO)
3614/*
3615 * The 'sessionoptions' option is changed.
3616 */
3617 char *
3618did_set_sessionoptions(optset_T *args)
3619{
3620 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
3621 return e_invalid_argument;
3622 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
3623 {
3624 // Don't allow both "sesdir" and "curdir".
3625 (void)opt_strings_flags(args->os_oldval.string, p_ssop_values,
3626 &ssop_flags, TRUE);
3627 return e_invalid_argument;
3628 }
3629
3630 return NULL;
3631}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003632
3633 int
3634expand_set_sessionoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3635{
3636 return expand_set_opt_string(
3637 args,
3638 p_ssop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003639 ARRAY_LENGTH(p_ssop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003640 numMatches,
3641 matches);
3642}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003643#endif
3644
3645/*
3646 * The 'shortmess' option is changed.
3647 */
3648 char *
3649did_set_shortmess(optset_T *args)
3650{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003651 char_u **varp = (char_u **)args->os_varp;
3652
Christian Brabandtb39b2402023-11-29 11:34:05 +01003653 return did_set_option_listflag(*varp, (char_u *)SHM_ALL, args->os_errbuf,
3654 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003655}
3656
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003657 int
3658expand_set_shortmess(optexpand_T *args, int *numMatches, char_u ***matches)
3659{
3660 return expand_set_opt_listflag(args, (char_u*)SHM_ALL, numMatches, matches);
3661}
3662
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003663#if defined(FEAT_LINEBREAK) || defined(PROTO)
3664/*
3665 * The 'showbreak' option is changed.
3666 */
3667 char *
3668did_set_showbreak(optset_T *args)
3669{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003670 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003671 char_u *s;
3672
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003673 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003674 {
3675 if (ptr2cells(s) != 1)
3676 return e_showbreak_contains_unprintable_or_wide_character;
3677 MB_PTR_ADV(s);
3678 }
3679
3680 return NULL;
3681}
3682#endif
3683
3684/*
3685 * The 'showcmdloc' option is changed.
3686 */
3687 char *
3688did_set_showcmdloc(optset_T *args UNUSED)
3689{
zeertzjqc27fcf42024-03-01 23:01:43 +01003690 char *errmsg = did_set_opt_strings(p_sloc, p_sloc_values, FALSE);
3691
3692 if (errmsg == NULL)
3693 comp_col();
3694
3695 return errmsg;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003696}
3697
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003698 int
3699expand_set_showcmdloc(optexpand_T *args, int *numMatches, char_u ***matches)
3700{
3701 return expand_set_opt_string(
3702 args,
3703 p_sloc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003704 ARRAY_LENGTH(p_sloc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003705 numMatches,
3706 matches);
3707}
3708
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003709#if defined(FEAT_SIGNS) || defined(PROTO)
3710/*
3711 * The 'signcolumn' option is changed.
3712 */
3713 char *
3714did_set_signcolumn(optset_T *args)
3715{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003716 char_u **varp = (char_u **)args->os_varp;
3717
3718 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003719 return e_invalid_argument;
3720 // When changing the 'signcolumn' to or from 'number', recompute the
3721 // width of the number column if 'number' or 'relativenumber' is set.
3722 if (((*args->os_oldval.string == 'n' && args->os_oldval.string[1] == 'u')
3723 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
3724 && (curwin->w_p_nu || curwin->w_p_rnu))
3725 curwin->w_nrwidth_line_count = 0;
3726
3727 return NULL;
3728}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003729
3730 int
3731expand_set_signcolumn(optexpand_T *args, int *numMatches, char_u ***matches)
3732{
3733 return expand_set_opt_string(
3734 args,
3735 p_scl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003736 ARRAY_LENGTH(p_scl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003737 numMatches,
3738 matches);
3739}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003740#endif
3741
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003742#if defined(FEAT_SPELL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003743/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003744 * The 'spellcapcheck' option is changed.
3745 */
3746 char *
3747did_set_spellcapcheck(optset_T *args UNUSED)
3748{
3749 // compile the regexp program.
3750 return compile_cap_prog(curwin->w_s);
3751}
3752
3753/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003754 * The 'spellfile' option is changed.
3755 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003756 char *
3757did_set_spellfile(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003758{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003759 char_u **varp = (char_u **)args->os_varp;
3760
3761 if (!valid_spellfile(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003762 return e_invalid_argument;
3763
3764 // If there is a window for this buffer in which 'spell' is set load the
3765 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003766 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003767}
3768
3769/*
3770 * The 'spell' option is changed.
3771 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003772 char *
3773did_set_spelllang(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003774{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003775 char_u **varp = (char_u **)args->os_varp;
3776
3777 if (!valid_spelllang(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003778 return e_invalid_argument;
3779
3780 // If there is a window for this buffer in which 'spell' is set load the
3781 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003782 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003783}
3784
3785/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003786 * The 'spelloptions' option is changed.
3787 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003788 char *
3789did_set_spelloptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003790{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003791 char_u **varp = (char_u **)args->os_varp;
3792
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003793 if (**varp != NUL && STRCMP(*varp, "camel") != 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003794 return e_invalid_argument;
3795
3796 return NULL;
3797}
3798
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003799 int
3800expand_set_spelloptions(optexpand_T *args, int *numMatches, char_u ***matches)
3801{
3802 static char *(p_spo_values[]) = {"camel", NULL};
3803 return expand_set_opt_string(
3804 args,
3805 p_spo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003806 ARRAY_LENGTH(p_spo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003807 numMatches,
3808 matches);
3809}
3810
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003811/*
3812 * The 'spellsuggest' option is changed.
3813 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003814 char *
3815did_set_spellsuggest(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003816{
3817 if (spell_check_sps() != OK)
3818 return e_invalid_argument;
3819
3820 return NULL;
3821}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003822
3823 int
3824expand_set_spellsuggest(optexpand_T *args, int *numMatches, char_u ***matches)
3825{
3826 return expand_set_opt_string(
3827 args,
3828 p_sps_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003829 ARRAY_LENGTH(p_sps_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003830 numMatches,
3831 matches);
3832}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003833#endif
3834
3835/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003836 * The 'splitkeep' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003837 */
3838 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003839did_set_splitkeep(optset_T *args UNUSED)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003840{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003841 return did_set_opt_strings(p_spk, p_spk_values, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003842}
3843
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003844 int
3845expand_set_splitkeep(optexpand_T *args, int *numMatches, char_u ***matches)
3846{
3847 return expand_set_opt_string(
3848 args,
3849 p_spk_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003850 ARRAY_LENGTH(p_spk_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003851 numMatches,
3852 matches);
3853}
3854
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00003855#if defined(FEAT_STL_OPT) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003856/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003857 * The 'statusline' option is changed.
3858 */
3859 char *
3860did_set_statusline(optset_T *args)
3861{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003862 return parse_statustabline_rulerformat(args, FALSE);
3863}
3864#endif
3865
3866/*
3867 * The 'swapsync' option is changed.
3868 */
3869 char *
3870did_set_swapsync(optset_T *args UNUSED)
3871{
3872 return did_set_opt_strings(p_sws, p_sws_values, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003873}
3874
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003875 int
3876expand_set_swapsync(optexpand_T *args, int *numMatches, char_u ***matches)
3877{
3878 return expand_set_opt_string(
3879 args,
3880 p_sws_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003881 ARRAY_LENGTH(p_sws_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003882 numMatches,
3883 matches);
3884}
3885
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003886/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003887 * The 'switchbuf' option is changed.
3888 */
3889 char *
3890did_set_switchbuf(optset_T *args UNUSED)
3891{
3892 return did_set_opt_flags(p_swb, p_swb_values, &swb_flags, TRUE);
3893}
3894
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003895 int
3896expand_set_switchbuf(optexpand_T *args, int *numMatches, char_u ***matches)
3897{
3898 return expand_set_opt_string(
3899 args,
3900 p_swb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003901 ARRAY_LENGTH(p_swb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003902 numMatches,
3903 matches);
3904}
3905
LemonBoy5247b0b2024-07-12 19:30:58 +02003906/*
3907 * The 'tabclose' option is changed.
3908 */
3909 char *
3910did_set_tabclose(optset_T *args UNUSED)
3911{
3912 return did_set_opt_flags(p_tcl, p_tcl_values, &tcl_flags, TRUE);
3913}
3914
3915 int
3916expand_set_tabclose(optexpand_T *args, int *numMatches, char_u ***matches)
3917{
3918 return expand_set_opt_string(
3919 args,
3920 p_tcl_values,
3921 ARRAY_LENGTH(p_tcl_values) - 1,
3922 numMatches,
3923 matches);
3924}
3925
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003926#if defined(FEAT_STL_OPT) || defined(PROTO)
3927/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003928 * The 'tabline' option is changed.
3929 */
3930 char *
3931did_set_tabline(optset_T *args)
3932{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003933 return parse_statustabline_rulerformat(args, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003934}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003935#endif
3936
3937/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003938 * The 'tagcase' option is changed.
3939 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003940 char *
3941did_set_tagcase(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003942{
3943 unsigned int *flags;
3944 char_u *p;
3945
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003946 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003947 {
3948 p = curbuf->b_p_tc;
3949 flags = &curbuf->b_tc_flags;
3950 }
3951 else
3952 {
3953 p = p_tc;
3954 flags = &tc_flags;
3955 }
3956
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003957 if ((args->os_flags & OPT_LOCAL) && *p == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003958 // make the local value empty: use the global value
3959 *flags = 0;
3960 else if (*p == NUL
3961 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
3962 return e_invalid_argument;
3963
3964 return NULL;
3965}
3966
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003967 int
3968expand_set_tagcase(optexpand_T *args, int *numMatches, char_u ***matches)
3969{
3970 return expand_set_opt_string(
3971 args,
3972 p_tc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003973 ARRAY_LENGTH(p_tc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003974 numMatches,
3975 matches);
3976}
3977
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003978/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003979 * The 'term' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003980 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003981 char *
3982did_set_term(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003983{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003984 if (T_NAME[0] == NUL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003985 return e_cannot_set_term_to_empty_string;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003986#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003987 if (gui.in_use)
3988 return e_cannot_change_term_in_GUI;
3989 if (term_is_gui(T_NAME))
3990 return e_use_gui_to_start_GUI;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003991#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003992 if (set_termname(T_NAME) == FAIL)
3993 return e_not_found_in_termcap;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003994
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003995 // Screen colors may have changed.
3996 redraw_later_clear();
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003997
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003998 return NULL;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003999}
4000
4001/*
4002 * Some terminal option (t_xxx) is changed
4003 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004004 char *
4005did_set_term_option(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004006{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004007 char_u **varp = (char_u **)args->os_varp;
4008
4009 if (!full_screen)
4010 return NULL;
4011
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004012 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
4013 if (varp == &T_CCO)
4014 {
4015 int colors = atoi((char *)T_CCO);
4016
4017 // Only reinitialize colors if t_Co value has really changed to
4018 // avoid expensive reload of colorscheme if t_Co is set to the
4019 // same value multiple times.
4020 if (colors != t_colors)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004021 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004022 t_colors = colors;
4023 if (t_colors <= 1)
4024 {
4025 vim_free(T_CCO);
4026 T_CCO = empty_option;
4027 }
4028#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
4029 if (is_term_win32())
4030 {
4031 swap_tcap();
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004032 args->os_did_swaptcap = TRUE;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004033 }
4034#endif
4035 // We now have a different color setup, initialize it again.
4036 init_highlight(TRUE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004037 }
4038 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004039 ttest(FALSE);
4040 if (varp == &T_ME)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004041 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004042 out_str(T_ME);
4043 redraw_later(UPD_CLEAR);
4044#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4045 // Since t_me has been set, this probably means that the user
4046 // wants to use this as default colors. Need to reset default
4047 // background/foreground colors.
4048# ifdef VIMDLL
4049 if (!gui.in_use && !gui.starting)
4050# endif
4051 mch_set_normal_colors();
4052#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004053 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004054 if (varp == &T_BE && termcap_active)
4055 {
4056 MAY_WANT_TO_LOG_THIS;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004057
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004058 if (*T_BE == NUL)
4059 // When clearing t_BE we assume the user no longer wants
4060 // bracketed paste, thus disable it by writing t_BD.
4061 out_str(T_BD);
4062 else
4063 out_str(T_BE);
4064 }
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004065
4066 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004067}
4068
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004069#if defined(FEAT_TERMINAL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004070/*
4071 * The 'termwinkey' option is changed.
4072 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004073 char *
Milly94606f72024-10-22 22:07:52 +02004074did_set_termwinkey(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004075{
Milly94606f72024-10-22 22:07:52 +02004076 char_u **varp = (char_u **)args->os_varp;
4077
4078 if ((*varp)[0] != NUL && string_to_key(*varp, TRUE) == 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004079 return e_invalid_argument;
4080
4081 return NULL;
4082}
4083
4084/*
4085 * The 'termwinsize' option is changed.
4086 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004087 char *
Milly8be10aa2024-10-22 22:01:46 +02004088did_set_termwinsize(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004089{
Milly8be10aa2024-10-22 22:01:46 +02004090 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004091 char_u *p;
4092
Milly8be10aa2024-10-22 22:01:46 +02004093 if ((*varp)[0] == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004094 return NULL;
4095
Milly8be10aa2024-10-22 22:01:46 +02004096 p = skipdigits(*varp);
4097 if (p == *varp || (*p != 'x' && *p != '*') || *skipdigits(p + 1) != NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004098 return e_invalid_argument;
4099
4100 return NULL;
4101}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00004102
4103# if defined(MSWIN) || defined(PROTO)
4104/*
4105 * The 'termwintype' option is changed.
4106 */
4107 char *
4108did_set_termwintype(optset_T *args UNUSED)
4109{
4110 return did_set_opt_strings(p_twt, p_twt_values, FALSE);
4111}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004112
4113 int
4114expand_set_termwintype(optexpand_T *args, int *numMatches, char_u ***matches)
4115{
4116 return expand_set_opt_string(
4117 args,
4118 p_twt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004119 ARRAY_LENGTH(p_twt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004120 numMatches,
4121 matches);
4122}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00004123# endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004124#endif
4125
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004126/*
4127 * The 'titlestring' option is changed.
4128 */
4129 char *
4130did_set_titlestring(optset_T *args)
4131{
4132 int flagval = 0;
4133
4134#ifdef FEAT_STL_OPT
4135 flagval = STL_IN_TITLE;
4136#endif
4137 return parse_titleiconstring(args, flagval);
4138}
4139
4140#if (defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)) || defined(PROTO)
4141/*
4142 * The 'toolbar' option is changed.
4143 */
4144 char *
4145did_set_toolbar(optset_T *args UNUSED)
4146{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004147 if (opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags,
4148 TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004149 return e_invalid_argument;
4150
4151 out_flush();
4152 gui_mch_show_toolbar((toolbar_flags &
4153 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4154 return NULL;
4155}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004156
4157 int
4158expand_set_toolbar(optexpand_T *args, int *numMatches, char_u ***matches)
4159{
4160 return expand_set_opt_string(
4161 args,
4162 p_toolbar_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004163 ARRAY_LENGTH(p_toolbar_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004164 numMatches,
4165 matches);
4166}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004167#endif
4168
4169#if (defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)) || defined(PROTO)
4170/*
4171 * The 'toolbariconsize' option is changed. GTK+ 2 only.
4172 */
4173 char *
4174did_set_toolbariconsize(optset_T *args UNUSED)
4175{
4176 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
4177 return e_invalid_argument;
4178
4179 out_flush();
4180 gui_mch_show_toolbar((toolbar_flags &
4181 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4182 return NULL;
4183}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004184
4185 int
4186expand_set_toolbariconsize(optexpand_T *args, int *numMatches, char_u ***matches)
4187{
4188 return expand_set_opt_string(
4189 args,
4190 p_tbis_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004191 ARRAY_LENGTH(p_tbis_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004192 numMatches,
4193 matches);
4194}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004195#endif
4196
4197#if defined(UNIX) || defined(VMS) || defined(PROTO)
4198/*
4199 * The 'ttymouse' option is changed.
4200 */
4201 char *
4202did_set_ttymouse(optset_T *args UNUSED)
4203{
4204 char *errmsg = NULL;
4205
4206 // Switch the mouse off before changing the escape sequences used for
4207 // that.
4208 mch_setmouse(FALSE);
4209 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
4210 errmsg = e_invalid_argument;
4211 else
4212 check_mouse_termcode();
4213 if (termcap_active)
4214 setmouse(); // may switch it on again
4215
4216 return errmsg;
4217}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004218
4219 int
4220expand_set_ttymouse(optexpand_T *args, int *numMatches, char_u ***matches)
4221{
4222 return expand_set_opt_string(
4223 args,
4224 p_ttym_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004225 ARRAY_LENGTH(p_ttym_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004226 numMatches,
4227 matches);
4228}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004229#endif
4230
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004231#if defined(FEAT_VARTABS) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004232/*
4233 * The 'varsofttabstop' option is changed.
4234 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004235 char *
4236did_set_varsofttabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004237{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004238 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004239 char_u *cp;
4240
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004241 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004242 VIM_CLEAR(curbuf->b_p_vsts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004243 else
4244 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004245 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004246 {
4247 if (vim_isdigit(*cp))
4248 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004249 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004250 continue;
4251 return e_invalid_argument;
4252 }
4253
4254 int *oldarray = curbuf->b_p_vsts_array;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004255 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004256 {
4257 if (oldarray)
4258 vim_free(oldarray);
4259 }
4260 else
4261 return e_invalid_argument;
4262 }
4263
4264 return NULL;
4265}
4266
4267/*
4268 * The 'vartabstop' option is changed.
4269 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004270 char *
4271did_set_vartabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004272{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004273 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004274 char_u *cp;
4275
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004276 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004277 VIM_CLEAR(curbuf->b_p_vts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004278 else
4279 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004280 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004281 {
4282 if (vim_isdigit(*cp))
4283 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004284 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004285 continue;
4286 return e_invalid_argument;
4287 }
4288
4289 int *oldarray = curbuf->b_p_vts_array;
4290
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004291 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004292 {
4293 vim_free(oldarray);
4294# ifdef FEAT_FOLDING
4295 if (foldmethodIsIndent(curwin))
4296 foldUpdateAll(curwin);
4297# endif
4298 }
4299 else
4300 return e_invalid_argument;
4301 }
4302
4303 return NULL;
4304}
4305#endif
4306
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004307/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004308 * The 'verbosefile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004309 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004310 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004311did_set_verbosefile(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004312{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004313 verbose_stop();
4314 if (*p_vfile != NUL && verbose_open() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004315 return e_invalid_argument;
4316
4317 return NULL;
4318}
4319
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004320#if defined(FEAT_SESSION) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004321/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004322 * The 'viewoptions' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004323 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004324 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004325did_set_viewoptions(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004326{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004327 return did_set_opt_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004328}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004329#endif
4330
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004331#if defined(FEAT_VIMINFO) || defined(PROTO)
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004332/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004333 * The 'viminfo' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004334 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004335 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004336did_set_viminfo(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004337{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004338 char_u *s;
4339 char *errmsg = NULL;
4340
4341 for (s = p_viminfo; *s;)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004342 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004343 // Check it's a valid character
4344 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
4345 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01004346 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004347 break;
4348 }
4349 if (*s == 'n') // name is always last one
4350 break;
4351 else if (*s == 'r') // skip until next ','
4352 {
4353 while (*++s && *s != ',')
4354 ;
4355 }
4356 else if (*s == '%')
4357 {
4358 // optional number
4359 while (vim_isdigit(*++s))
4360 ;
4361 }
4362 else if (*s == '!' || *s == 'h' || *s == 'c')
4363 ++s; // no extra chars
4364 else // must have a number
4365 {
4366 while (vim_isdigit(*++s))
4367 ;
4368
4369 if (!VIM_ISDIGIT(*(s - 1)))
4370 {
4371 if (args->os_errbuf != NULL)
4372 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01004373 vim_snprintf(args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004374 _(e_missing_number_after_angle_str_angle),
4375 transchar_byte(*(s - 1)));
4376 errmsg = args->os_errbuf;
4377 }
4378 else
4379 errmsg = "";
4380 break;
4381 }
4382 }
4383 if (*s == ',')
4384 ++s;
4385 else if (*s)
4386 {
4387 if (args->os_errbuf != NULL)
4388 errmsg = e_missing_comma;
4389 else
4390 errmsg = "";
4391 break;
4392 }
4393 }
4394 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
4395 errmsg = e_must_specify_a_value;
4396
4397 return errmsg;
4398}
4399#endif
4400
4401/*
4402 * The 'virtualedit' option is changed.
4403 */
4404 char *
4405did_set_virtualedit(optset_T *args)
4406{
4407 char_u *ve = p_ve;
4408 unsigned int *flags = &ve_flags;
4409
4410 if (args->os_flags & OPT_LOCAL)
4411 {
4412 ve = curwin->w_p_ve;
4413 flags = &curwin->w_ve_flags;
4414 }
4415
4416 if ((args->os_flags & OPT_LOCAL) && *ve == NUL)
4417 // make the local value empty: use the global value
4418 *flags = 0;
4419 else
4420 {
4421 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
4422 return e_invalid_argument;
4423 else if (STRCMP(ve, args->os_oldval.string) != 0)
4424 {
4425 // Recompute cursor position in case the new 've' setting
4426 // changes something.
4427 validate_virtcol();
4428 coladvance(curwin->w_virtcol);
4429 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004430 }
4431
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004432 return NULL;
4433}
4434
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004435 int
4436expand_set_virtualedit(optexpand_T *args, int *numMatches, char_u ***matches)
4437{
4438 return expand_set_opt_string(
4439 args,
4440 p_ve_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004441 ARRAY_LENGTH(p_ve_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004442 numMatches,
4443 matches);
4444}
4445
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004446/*
4447 * The 'whichwrap' option is changed.
4448 */
4449 char *
4450did_set_whichwrap(optset_T *args)
4451{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004452 char_u **varp = (char_u **)args->os_varp;
4453
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004454 // Add ',' to the list flags because 'whichwrap' is a flag
4455 // list that is comma-separated.
Christian Brabandtb39b2402023-11-29 11:34:05 +01004456 return did_set_option_listflag(*varp, (char_u *)(WW_ALL ","),
4457 args->os_errbuf, args->os_errbuflen);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004458}
4459
4460 int
4461expand_set_whichwrap(optexpand_T *args, int *numMatches, char_u ***matches)
4462{
4463 return expand_set_opt_listflag(args, (char_u*)WW_ALL, numMatches, matches);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004464}
4465
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004466/*
4467 * The 'wildmode' option is changed.
4468 */
4469 char *
4470did_set_wildmode(optset_T *args UNUSED)
4471{
4472 if (check_opt_wim() == FAIL)
4473 return e_invalid_argument;
4474 return NULL;
4475}
4476
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004477 int
4478expand_set_wildmode(optexpand_T *args, int *numMatches, char_u ***matches)
4479{
4480 return expand_set_opt_string(
4481 args,
4482 p_wim_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004483 ARRAY_LENGTH(p_wim_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004484 numMatches,
4485 matches);
4486}
4487
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004488/*
4489 * The 'wildoptions' option is changed.
4490 */
4491 char *
4492did_set_wildoptions(optset_T *args UNUSED)
4493{
4494 return did_set_opt_strings(p_wop, p_wop_values, TRUE);
4495}
4496
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004497 int
4498expand_set_wildoptions(optexpand_T *args, int *numMatches, char_u ***matches)
4499{
4500 return expand_set_opt_string(
4501 args,
4502 p_wop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004503 ARRAY_LENGTH(p_wop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004504 numMatches,
4505 matches);
4506}
4507
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004508#if defined(FEAT_WAK) || defined(PROTO)
4509/*
4510 * The 'winaltkeys' option is changed.
4511 */
4512 char *
4513did_set_winaltkeys(optset_T *args UNUSED)
4514{
4515 char *errmsg = NULL;
4516
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004517 if (*p_wak == NUL || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004518 errmsg = e_invalid_argument;
4519# ifdef FEAT_MENU
4520# if defined(FEAT_GUI_MOTIF)
4521 else if (gui.in_use)
4522 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4523# elif defined(FEAT_GUI_GTK)
4524 else if (gui.in_use)
4525 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4526# endif
4527# endif
4528 return errmsg;
4529}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004530
4531 int
4532expand_set_winaltkeys(optexpand_T *args, int *numMatches, char_u ***matches)
4533{
4534 return expand_set_opt_string(
4535 args,
4536 p_wak_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004537 ARRAY_LENGTH(p_wak_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004538 numMatches,
4539 matches);
4540}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004541#endif
4542
4543/*
4544 * The 'wincolor' option is changed.
4545 */
4546 char *
4547did_set_wincolor(optset_T *args UNUSED)
4548{
4549#ifdef FEAT_TERMINAL
4550 term_update_wincolor(curwin);
4551#endif
4552 return NULL;
4553}
4554
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004555 int
4556expand_set_wincolor(optexpand_T *args, int *numMatches, char_u ***matches)
4557{
4558 return expand_set_opt_generic(
4559 args,
4560 get_highlight_name,
4561 numMatches,
4562 matches);
4563}
4564
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004565#ifdef FEAT_SYN_HL
4566/*
4567 * When the 'syntax' option is set, load the syntax of that name.
4568 */
4569 static void
4570do_syntax_autocmd(int value_changed)
4571{
4572 static int syn_recursive = 0;
4573
4574 ++syn_recursive;
4575 // Only pass TRUE for "force" when the value changed or not used
4576 // recursively, to avoid endless recurrence.
4577 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
4578 value_changed || syn_recursive == 1, curbuf);
4579 curbuf->b_flags |= BF_SYN_SET;
4580 --syn_recursive;
4581}
4582#endif
4583
4584/*
4585 * When the 'filetype' option is set, trigger the FileType autocommand.
4586 */
4587 static void
4588do_filetype_autocmd(char_u **varp, int opt_flags, int value_changed)
4589{
4590 // Skip this when called from a modeline and the filetype was already set
4591 // to this value.
4592 if ((opt_flags & OPT_MODELINE) && !value_changed)
4593 return;
4594
4595 static int ft_recursive = 0;
4596 int secure_save = secure;
4597
4598 // Reset the secure flag, since the value of 'filetype' has
4599 // been checked to be safe.
4600 secure = 0;
4601
4602 ++ft_recursive;
zeertzjq5bf6c212024-03-31 18:41:27 +02004603 curbuf->b_did_filetype = TRUE;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004604 // Only pass TRUE for "force" when the value changed or not
4605 // used recursively, to avoid endless recurrence.
4606 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
4607 value_changed || ft_recursive == 1, curbuf);
4608 --ft_recursive;
4609 // Just in case the old "curbuf" is now invalid.
4610 if (varp != &(curbuf->b_p_ft))
4611 varp = NULL;
4612
4613 secure = secure_save;
4614}
4615
4616#ifdef FEAT_SPELL
4617/*
4618 * When the 'spelllang' option is set, source the spell/LANG.vim file in
4619 * 'runtimepath'.
4620 */
4621 static void
4622do_spelllang_source(void)
4623{
4624 char_u fname[200];
4625 char_u *p;
4626 char_u *q = curwin->w_s->b_p_spl;
4627
4628 // Skip the first name if it is "cjk".
4629 if (STRNCMP(q, "cjk,", 4) == 0)
4630 q += 4;
4631
4632 // They could set 'spellcapcheck' depending on the language. Use the first
4633 // name in 'spelllang' up to '_region' or '.encoding'.
4634 for (p = q; *p != NUL; ++p)
4635 if (!ASCII_ISALNUM(*p) && *p != '-')
4636 break;
4637 if (p > q)
4638 {
4639 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
4640 (int)(p - q), q);
4641 source_runtime(fname, DIP_ALL);
4642 }
4643}
4644#endif
4645
4646/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004647 * Handle string options that need some action to perform when changed.
zeertzjqf6782732022-07-27 18:26:03 +01004648 * The new value must be allocated.
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004649 * Returns NULL for success, or an untranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +02004650 */
4651 char *
4652did_set_string_option(
4653 int opt_idx, // index in options[] table
4654 char_u **varp, // pointer to the option variable
Bram Moolenaardac13472019-09-16 21:06:21 +02004655 char_u *oldval, // previous value of the option
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004656 char_u *value, // new value of the option
Bram Moolenaardac13472019-09-16 21:06:21 +02004657 char *errbuf, // buffer for errors, or NULL
Mike Williams620f0112023-12-05 15:36:06 +01004658 size_t errbuflen, // length of error buffer
Bram Moolenaardac13472019-09-16 21:06:21 +02004659 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004660 set_op_T op, // OP_ADDING/OP_PREPENDING/OP_REMOVING
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004661 int *value_checked) // value was checked to be safe, no
Bram Moolenaardac13472019-09-16 21:06:21 +02004662 // need to set P_INSECURE
4663{
4664 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +02004665 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004666 opt_did_set_cb_T did_set_cb = get_option_did_set_cb(opt_idx);
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004667 optset_T args;
4668
4669 // 'ttytype' is an alias for 'term'. Both 'term' and 'ttytype' point to
4670 // T_NAME. If 'term' or 'ttytype' is modified, then use the index for the
4671 // 'term' option. Only set the P_ALLOCED flag on 'term'.
4672 if (varp == &T_NAME)
4673 {
4674 opt_idx = findoption((char_u *)"term");
4675 if (opt_idx >= 0)
4676 {
4677 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
4678 did_set_cb = get_option_did_set_cb(opt_idx);
4679 }
4680 }
4681
4682 CLEAR_FIELD(args);
Bram Moolenaardac13472019-09-16 21:06:21 +02004683
Bram Moolenaardac13472019-09-16 21:06:21 +02004684 // Disallow changing some options from secure mode
4685 if ((secure
4686#ifdef HAVE_SANDBOX
4687 || sandbox != 0
4688#endif
4689 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00004690 errmsg = e_not_allowed_here;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004691 // Check for a "normal" directory or file name in some options.
4692 else if (check_illegal_path_names(opt_idx, varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004693 errmsg = e_invalid_argument;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004694 else if (did_set_cb != NULL)
4695 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004696 args.os_varp = (char_u *)varp;
4697 args.os_idx = opt_idx;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004698 args.os_flags = opt_flags;
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004699 args.os_op = op;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004700 args.os_oldval.string = oldval;
4701 args.os_newval.string = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004702 args.os_errbuf = errbuf;
Christian Brabandtb39b2402023-11-29 11:34:05 +01004703 args.os_errbuflen = errbuflen;
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004704 // Invoke the option specific callback function to validate and apply
4705 // the new option value.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004706 errmsg = did_set_cb(&args);
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004707
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004708 // The 'keymap', 'filetype' and 'syntax' option callback functions
4709 // may change the os_value_checked field.
4710 *value_checked = args.os_value_checked;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004711 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004712
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004713 // If an error is detected, restore the previous value.
Bram Moolenaardac13472019-09-16 21:06:21 +02004714 if (errmsg != NULL)
4715 {
zeertzjqf6782732022-07-27 18:26:03 +01004716 free_string_option(*varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02004717 *varp = oldval;
4718 // When resetting some values, need to act on it.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004719 if (args.os_restore_chartab)
Bram Moolenaardac13472019-09-16 21:06:21 +02004720 (void)init_chartab();
4721 if (varp == &p_hl)
4722 (void)highlight_changed();
4723 }
4724 else
4725 {
4726#ifdef FEAT_EVAL
4727 // Remember where the option was set.
4728 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4729#endif
4730 // Free string options that are in allocated memory.
4731 // Use "free_oldval", because recursiveness may change the flags under
4732 // our fingers (esp. init_highlight()).
4733 if (free_oldval)
4734 free_string_option(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01004735 set_option_flag(opt_idx, P_ALLOCED);
Bram Moolenaardac13472019-09-16 21:06:21 +02004736
4737 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
4738 && is_global_local_option(opt_idx))
4739 {
4740 // global option with local value set to use global value; free
4741 // the local value and make it empty
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004742 char_u *p = get_option_varp_scope(opt_idx, OPT_LOCAL);
Bram Moolenaardac13472019-09-16 21:06:21 +02004743 free_string_option(*(char_u **)p);
4744 *(char_u **)p = empty_option;
4745 }
4746
4747 // May set global value for local option.
4748 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
4749 set_string_option_global(opt_idx, varp);
4750
4751 // Trigger the autocommand only after setting the flags.
4752#ifdef FEAT_SYN_HL
Bram Moolenaardac13472019-09-16 21:06:21 +02004753 if (varp == &(curbuf->b_p_syn))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004754 do_syntax_autocmd(args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004755#endif
4756 else if (varp == &(curbuf->b_p_ft))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004757 do_filetype_autocmd(varp, opt_flags, args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004758#ifdef FEAT_SPELL
4759 if (varp == &(curwin->w_s->b_p_spl))
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004760 do_spelllang_source();
Bram Moolenaardac13472019-09-16 21:06:21 +02004761#endif
4762 }
4763
Bram Moolenaardac13472019-09-16 21:06:21 +02004764 if (varp == &p_mouse)
4765 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004766 if (*p_mouse == NUL)
4767 mch_setmouse(FALSE); // switch mouse off
4768 else
Bram Moolenaardac13472019-09-16 21:06:21 +02004769 setmouse(); // in case 'mouse' changed
4770 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004771
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004772 if (varp == &p_rtp)
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004773 {
4774 export_myvimdir();
4775#if defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004776 update_package_paths_in_lua();
4777#endif
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004778 }
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004779
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004780#if defined(FEAT_LINEBREAK)
4781 // Changing Formatlistpattern when briopt includes the list setting:
4782 // redraw
4783 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
4784 && curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004785 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004786#endif
4787
Bram Moolenaardac13472019-09-16 21:06:21 +02004788 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004789 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0
4790 && (get_option_flags(opt_idx) & P_HLONLY) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02004791 curwin->w_set_curswant = TRUE;
4792
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004793 if ((opt_flags & OPT_NO_REDRAW) == 0)
4794 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004795#ifdef FEAT_GUI
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004796 // set when changing an option that only requires a redraw in the GUI
4797 int redraw_gui_only = FALSE;
4798
4799 if (varp == &p_go // 'guioptions'
4800 || varp == &p_guifont // 'guifont'
4801# ifdef FEAT_GUI_TABLINE
4802 || varp == &p_gtl // 'guitablabel'
4803 || varp == &p_gtt // 'guitabtooltip'
4804# endif
4805# ifdef FEAT_XFONTSET
4806 || varp == &p_guifontset // 'guifontset'
4807# endif
4808 || varp == &p_guifontwide // 'guifontwide'
Erik S. V. Jansson2f026382024-02-26 22:23:05 +01004809# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004810 || varp == &p_guiligatures // 'guiligatures'
4811# endif
4812 )
4813 redraw_gui_only = TRUE;
4814
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004815 // check redraw when it's not a GUI option or the GUI is active.
4816 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02004817#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004818 check_redraw(get_option_flags(opt_idx));
4819 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004820
4821#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004822 if (args.os_did_swaptcap)
Bram Moolenaardac13472019-09-16 21:06:21 +02004823 {
4824 set_termname((char_u *)"win32");
4825 init_highlight(TRUE, FALSE);
4826 }
4827#endif
4828
4829 return errmsg;
4830}
4831
4832/*
4833 * Check an option that can be a range of string values.
4834 *
4835 * Return OK for correct value, FAIL otherwise.
4836 * Empty is always OK.
4837 */
4838 static int
4839check_opt_strings(
4840 char_u *val,
4841 char **values,
4842 int list) // when TRUE: accept a list of values
4843{
4844 return opt_strings_flags(val, values, NULL, list);
4845}
4846
4847/*
4848 * Handle an option that can be a range of string values.
4849 * Set a flag in "*flagp" for each string present.
4850 *
4851 * Return OK for correct value, FAIL otherwise.
4852 * Empty is always OK.
4853 */
4854 static int
4855opt_strings_flags(
4856 char_u *val, // new value
4857 char **values, // array of valid string values
4858 unsigned *flagp,
4859 int list) // when TRUE: accept a list of values
4860{
4861 int i;
4862 int len;
4863 unsigned new_flags = 0;
4864
4865 while (*val)
4866 {
4867 for (i = 0; ; ++i)
4868 {
4869 if (values[i] == NULL) // val not found in values[]
4870 return FAIL;
4871
4872 len = (int)STRLEN(values[i]);
4873 if (STRNCMP(values[i], val, len) == 0
4874 && ((list && val[len] == ',') || val[len] == NUL))
4875 {
4876 val += len + (val[len] == ',');
4877 new_flags |= (1 << i);
4878 break; // check next item in val list
4879 }
4880 }
4881 }
4882 if (flagp != NULL)
4883 *flagp = new_flags;
4884
4885 return OK;
4886}
4887
4888/*
4889 * return OK if "p" is a valid fileformat name, FAIL otherwise.
4890 */
4891 int
4892check_ff_value(char_u *p)
4893{
4894 return check_opt_strings(p, p_ff_values, FALSE);
4895}
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004896
4897/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004898 * Save the actual shortmess Flags and clear them temporarily to avoid that
4899 * file messages overwrites any output from the following commands.
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004900 *
4901 * Caller must make sure to first call save_clear_shm_value() and then
4902 * restore_shm_value() exactly the same number of times.
4903 */
4904 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004905save_clear_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004906{
4907 if (STRLEN(p_shm) >= SHM_LEN)
4908 {
4909 iemsg(e_internal_error_shortmess_too_long);
4910 return;
4911 }
4912
4913 if (++set_shm_recursive == 1)
4914 {
4915 STRCPY(shm_buf, p_shm);
4916 set_option_value_give_err((char_u *)"shm", 0L, (char_u *)"", 0);
4917 }
4918}
4919
4920/*
4921 * Restore the shortmess Flags set from the save_clear_shm_value() function.
4922 */
4923 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004924restore_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004925{
4926 if (--set_shm_recursive == 0)
4927 {
4928 set_option_value_give_err((char_u *)"shm", 0L, shm_buf, 0);
4929 vim_memset(shm_buf, 0, SHM_LEN);
4930 }
4931}
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004932
4933/*
4934 * Export the environment variable $MYVIMDIR to the first item in runtimepath
4935 */
Christian Brabandt1a741d32025-03-01 16:30:33 +01004936 void
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004937export_myvimdir()
4938{
4939 int dofree = FALSE;
4940 char_u *p;
4941 char_u *q = p_rtp;
4942 char_u *buf = alloc(MAXPATHL);
4943
4944 if (buf == NULL)
4945 return;
4946
4947 (void)copy_option_part(&q, buf, MAXPATHL, ",");
4948
4949 p = vim_getenv((char_u *)"MYVIMDIR", &dofree);
4950
4951 if (p == NULL || STRCMP(p, buf) != 0)
4952 {
4953 add_pathsep(buf);
4954#ifdef MSWIN
4955 // normalize path separators
4956 for (q = buf; *q != NUL; q++)
4957 if (*q == '/')
4958 *q = '\\';
4959#endif
4960 vim_setenv((char_u *)"MYVIMDIR", buf);
4961 }
4962 if (dofree)
4963 vim_free(p);
4964 vim_free(buf);
4965}