blob: d667e535809d28f6eda09079a0488390ee18a401 [file] [log] [blame]
Bram Moolenaardac13472019-09-16 21:06:21 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * optionstr.c: Functions related to string options
12 */
13
14#include "vim.h"
15
Christian Brabandt9aee8ec2022-12-16 16:41:23 +000016static char_u shm_buf[SHM_LEN];
17static int set_shm_recursive = 0;
18
Bram Moolenaardac13472019-09-16 21:06:21 +020019static char *(p_ambw_values[]) = {"single", "double", NULL};
20static char *(p_bg_values[]) = {"light", "dark", NULL};
21static char *(p_bkc_values[]) = {"yes", "auto", "no", "breaksymlink", "breakhardlink", NULL};
22static char *(p_bo_values[]) = {"all", "backspace", "cursor", "complete",
23 "copy", "ctrlg", "error", "esc", "ex",
24 "hangul", "insertmode", "lang", "mess",
25 "showmatch", "operator", "register", "shell",
LemonBoy77771d32022-04-13 11:47:25 +010026 "spell", "term", "wildmode", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020027#if defined(FEAT_LINEBREAK)
28// Note: Keep this in sync with briopt_check()
29static char *(p_briopt_values[]) = {"shift:", "min:", "sbr", "list:", "column:", NULL};
30#endif
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +020031#if defined(FEAT_TABPANEL)
Hirohito Higashi598bbb12025-05-22 22:41:05 +020032// Note: Keep this in sync with tabpanelopt_changed()
33static char *(p_tplo_values[]) = {"align:", "columns:", "vert", NULL};
34static char *(p_tplo_align_values[]) = {"left", "right", NULL};
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +020035#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +020036#if defined(FEAT_DIFF)
37// Note: Keep this in sync with diffopt_changed()
Yee Cheng Chin9943d472025-03-26 19:41:02 +010038static 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 +020039static char *(p_dip_algorithm_values[]) = {"myers", "minimal", "patience", "histogram", NULL};
Yee Cheng Chin9943d472025-03-26 19:41:02 +010040static char *(p_dip_inline_values[]) = {"none", "simple", "char", "word", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020041#endif
distobs25ac6d62024-07-06 17:50:09 +020042static char *(p_nf_values[]) = {"bin", "octal", "hex", "alpha", "unsigned", "blank", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020043static char *(p_ff_values[]) = {FF_UNIX, FF_DOS, FF_MAC, NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020044#ifdef FEAT_CLIPBOARD
45// Note: Keep this in sync with did_set_clipboard()
46static char *(p_cb_values[]) = {"unnamed", "unnamedplus", "autoselect", "autoselectplus", "autoselectml", "html", "exclude:", NULL};
47#endif
Bram Moolenaardac13472019-09-16 21:06:21 +020048#ifdef FEAT_CRYPT
Christian Brabandtf573c6e2021-06-20 14:02:16 +020049static char *(p_cm_values[]) = {"zip", "blowfish", "blowfish2",
50 # ifdef FEAT_SODIUM
Christian Brabandtaae58342023-04-23 17:50:22 +010051 "xchacha20", "xchacha20v2",
Christian Brabandtf573c6e2021-06-20 14:02:16 +020052 # endif
53 NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020054#endif
55static char *(p_cmp_values[]) = {"internal", "keepascii", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +020056#ifdef FEAT_SYN_HL
57// Note: Keep this in sync with fill_culopt_flags()
58static char *(p_culopt_values[]) = {"line", "screenline", "number", "both", NULL};
59#endif
Bram Moolenaardac13472019-09-16 21:06:21 +020060static char *(p_dy_values[]) = {"lastline", "truncate", "uhex", NULL};
Yegappan Lakshmanan87018252023-09-20 20:20:04 +020061static char *(p_jop_values[]) = {"stack", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020062#ifdef FEAT_FOLDING
63static char *(p_fdo_values[]) = {"all", "block", "hor", "mark", "percent",
64 "quickfix", "search", "tag", "insert",
65 "undo", "jump", NULL};
66#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +020067// Note: Keep this in sync with match_keyprotocol()
68static char *(p_kpc_protocol_values[]) = {"none", "mok2", "kitty", NULL};
69#ifdef FEAT_PROP_POPUP
70// Note: Keep this in sync with parse_popup_option()
71static char *(p_popup_option_values[]) = {"height:", "width:", "highlight:", "border:", "align:", NULL};
72static char *(p_popup_option_border_values[]) = {"on", "off", NULL};
73static char *(p_popup_option_align_values[]) = {"item", "menu", NULL};
74#endif
75#if defined(FEAT_SPELL)
76// Note: Keep this in sync with spell_check_sps()
77static char *(p_sps_values[]) = {"best", "fast", "double", "expr:", "file:", "timeout:", NULL};
78#endif
Bram Moolenaardac13472019-09-16 21:06:21 +020079#ifdef FEAT_SESSION
Bram Moolenaar635bd602021-04-16 19:58:22 +020080// Also used for 'viewoptions'! Keep in sync with SSOP_ flags.
Bram Moolenaardac13472019-09-16 21:06:21 +020081static char *(p_ssop_values[]) = {"buffers", "winpos", "resize", "winsize",
82 "localoptions", "options", "help", "blank", "globals", "slash", "unix",
Bram Moolenaar635bd602021-04-16 19:58:22 +020083 "sesdir", "curdir", "folds", "cursor", "tabpages", "terminal", "skiprtp",
84 NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020085#endif
Bram Moolenaar539aa6b2019-11-17 18:09:38 +010086// Keep in sync with SWB_ flags in option.h
87static char *(p_swb_values[]) = {"useopen", "usetab", "split", "newtab", "vsplit", "uselast", NULL};
Luuk van Baal13ece2a2022-10-03 15:28:08 +010088static char *(p_spk_values[]) = {"cursor", "screen", "topline", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020089static char *(p_tc_values[]) = {"followic", "ignore", "match", "followscs", "smart", NULL};
LemonBoy5247b0b2024-07-12 19:30:58 +020090// Keep in sync with TCL_ flags in option.h
91static char *(p_tcl_values[]) = {"left", "uselast", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +020092#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
93static char *(p_toolbar_values[]) = {"text", "icons", "tooltips", "horiz", NULL};
94#endif
95#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
96static char *(p_tbis_values[]) = {"tiny", "small", "medium", "large", "huge", "giant", NULL};
97#endif
Bram Moolenaara1cb1d12019-10-17 23:00:07 +020098#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +020099static char *(p_ttym_values[]) = {"xterm", "xterm2", "dec", "netterm", "jsbterm", "pterm", "urxvt", "sgr", NULL};
100#endif
Gary Johnson53ba05b2021-07-26 22:19:10 +0200101static char *(p_ve_values[]) = {"block", "insert", "all", "onemore", "none", "NONE", NULL};
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200102// Note: Keep this in sync with check_opt_wim()
Girish Palya2bacc3e2025-03-02 22:55:57 +0100103static char *(p_wim_values[]) = {"full", "longest", "list", "lastused", "noselect", NULL};
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000104static char *(p_wop_values[]) = {"fuzzy", "tagfile", "pum", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200105#ifdef FEAT_WAK
106static char *(p_wak_values[]) = {"yes", "menu", "no", NULL};
107#endif
108static char *(p_mousem_values[]) = {"extend", "popup", "popup_setpos", "mac", NULL};
109static char *(p_sel_values[]) = {"inclusive", "exclusive", "old", NULL};
110static char *(p_slm_values[]) = {"mouse", "key", "cmd", NULL};
111static char *(p_km_values[]) = {"startsel", "stopsel", NULL};
112#ifdef FEAT_BROWSE
113static char *(p_bsdir_values[]) = {"current", "last", "buffer", NULL};
114#endif
115static char *(p_scbopt_values[]) = {"ver", "hor", "jump", NULL};
116static char *(p_debug_values[]) = {"msg", "throw", "beep", NULL};
117static char *(p_ead_values[]) = {"both", "ver", "hor", NULL};
118static char *(p_buftype_values[]) = {"nofile", "nowrite", "quickfix", "help", "terminal", "acwrite", "prompt", "popup", NULL};
119static char *(p_bufhidden_values[]) = {"hide", "unload", "delete", "wipe", NULL};
Bram Moolenaaraa0489e2020-04-17 19:41:21 +0200120static char *(p_bs_values[]) = {"indent", "eol", "start", "nostop", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200121#ifdef FEAT_FOLDING
122static char *(p_fdm_values[]) = {"manual", "expr", "marker", "indent", "syntax",
123# ifdef FEAT_DIFF
124 "diff",
125# endif
126 NULL};
127static char *(p_fcl_values[]) = {"all", NULL};
128#endif
glepnirf31cfa22025-03-06 21:59:13 +0100129static char *(p_cfc_values[]) = {"keyword", "files", "whole_line", NULL};
Girish Palyab1565882025-04-15 20:16:00 +0200130static char *(p_cot_values[]) = {"menu", "menuone", "longest", "preview", "popup", "popuphidden", "noinsert", "noselect", "fuzzy", "nosort", "preinsert", "nearest", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200131#ifdef BACKSLASH_IN_FILENAME
132static char *(p_csl_values[]) = {"slash", "backslash", NULL};
133#endif
134#ifdef FEAT_SIGNS
135static char *(p_scl_values[]) = {"yes", "no", "auto", "number", NULL};
136#endif
137#if defined(MSWIN) && defined(FEAT_TERMINAL)
138static char *(p_twt_values[]) = {"winpty", "conpty", "", NULL};
139#endif
Luuk van Baalba936f62022-12-15 13:15:39 +0000140static char *(p_sloc_values[]) = {"last", "statusline", "tabline", NULL};
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +0000141static char *(p_sws_values[]) = {"fsync", "sync", NULL};
Bram Moolenaardac13472019-09-16 21:06:21 +0200142
143static int check_opt_strings(char_u *val, char **values, int list);
144static int opt_strings_flags(char_u *val, char **values, unsigned *flagp, int list);
145
146/*
147 * After setting various option values: recompute variables that depend on
148 * option values.
149 */
150 void
151didset_string_options(void)
152{
153 (void)opt_strings_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
154 (void)opt_strings_flags(p_bkc, p_bkc_values, &bkc_flags, TRUE);
155 (void)opt_strings_flags(p_bo, p_bo_values, &bo_flags, TRUE);
glepnirf31cfa22025-03-06 21:59:13 +0100156 (void)opt_strings_flags(p_cfc, p_cfc_values, &cfc_flags, TRUE);
zeertzjq529b9ad2024-06-05 20:27:06 +0200157 (void)opt_strings_flags(p_cot, p_cot_values, &cot_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200158#ifdef FEAT_SESSION
159 (void)opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE);
160 (void)opt_strings_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
161#endif
162#ifdef FEAT_FOLDING
163 (void)opt_strings_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
164#endif
165 (void)opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE);
Yegappan Lakshmanan87018252023-09-20 20:20:04 +0200166 (void)opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200167 (void)opt_strings_flags(p_tc, p_tc_values, &tc_flags, FALSE);
168 (void)opt_strings_flags(p_ve, p_ve_values, &ve_flags, TRUE);
Bram Moolenaara1cb1d12019-10-17 23:00:07 +0200169#if defined(UNIX) || defined(VMS)
Bram Moolenaardac13472019-09-16 21:06:21 +0200170 (void)opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE);
171#endif
172#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)
173 (void)opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags, TRUE);
174#endif
175#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)
176 (void)opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE);
177#endif
Sean Dewar39c46b42022-05-12 17:44:29 +0100178 (void)opt_strings_flags(p_swb, p_swb_values, &swb_flags, TRUE);
LemonBoy5247b0b2024-07-12 19:30:58 +0200179 (void)opt_strings_flags(p_tcl, p_tcl_values, &tcl_flags, TRUE);
Bram Moolenaardac13472019-09-16 21:06:21 +0200180}
181
Yegappan Lakshmananf9dc2782023-05-11 15:02:56 +0100182#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200183/*
184 * Trigger the OptionSet autocommand.
185 * "opt_idx" is the index of the option being set.
186 * "opt_flags" can be OPT_LOCAL etc.
187 * "oldval" the old value
188 * "oldval_l" the old local value (only non-NULL if global and local value
189 * are set)
190 * "oldval_g" the old global value (only non-NULL if global and local value
191 * are set)
192 * "newval" the new value
193 */
194 void
zeertzjq269aa2b2022-11-28 11:36:50 +0000195trigger_optionset_string(
Bram Moolenaardac13472019-09-16 21:06:21 +0200196 int opt_idx,
197 int opt_flags,
198 char_u *oldval,
199 char_u *oldval_l,
200 char_u *oldval_g,
201 char_u *newval)
202{
203 // Don't do this recursively.
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000204 if (oldval == NULL || newval == NULL
205 || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
206 return;
Bram Moolenaardac13472019-09-16 21:06:21 +0200207
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000208 char_u buf_type[7];
209
210 sprintf((char *)buf_type, "%s",
Bram Moolenaardac13472019-09-16 21:06:21 +0200211 (opt_flags & OPT_LOCAL) ? "local" : "global");
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000212 set_vim_var_string(VV_OPTION_OLD, oldval, -1);
213 set_vim_var_string(VV_OPTION_NEW, newval, -1);
214 set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
215 if (opt_flags & OPT_LOCAL)
216 {
217 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
218 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
Bram Moolenaardac13472019-09-16 21:06:21 +0200219 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000220 if (opt_flags & OPT_GLOBAL)
221 {
222 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
223 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval, -1);
224 }
225 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
226 {
227 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
228 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval_l, -1);
229 set_vim_var_string(VV_OPTION_OLDGLOBAL, oldval_g, -1);
230 }
231 if (opt_flags & OPT_MODELINE)
232 {
233 set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
234 set_vim_var_string(VV_OPTION_OLDLOCAL, oldval, -1);
235 }
236 apply_autocmds(EVENT_OPTIONSET,
237 get_option_fullname(opt_idx), NULL, FALSE,
238 NULL);
239 reset_v_option_vars();
Bram Moolenaardac13472019-09-16 21:06:21 +0200240}
241#endif
242
243 static char *
Mike Williams620f0112023-12-05 15:36:06 +0100244illegal_char(char *errbuf, size_t errbuflen, int c)
Bram Moolenaardac13472019-09-16 21:06:21 +0200245{
246 if (errbuf == NULL)
247 return "";
zeertzjq6a8d2e12024-01-17 20:54:49 +0100248 vim_snprintf(errbuf, errbuflen, _(e_illegal_character_str),
Christian Brabandtb39b2402023-11-29 11:34:05 +0100249 (char *)transchar(c));
Bram Moolenaardac13472019-09-16 21:06:21 +0200250 return errbuf;
251}
252
253/*
254 * Check string options in a buffer for NULL value.
255 */
256 void
257check_buf_options(buf_T *buf)
258{
259 check_string_option(&buf->b_p_bh);
260 check_string_option(&buf->b_p_bt);
261 check_string_option(&buf->b_p_fenc);
262 check_string_option(&buf->b_p_ff);
263#ifdef FEAT_FIND_ID
264 check_string_option(&buf->b_p_def);
265 check_string_option(&buf->b_p_inc);
266# ifdef FEAT_EVAL
267 check_string_option(&buf->b_p_inex);
268# endif
269#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +0100270#if defined(FEAT_EVAL)
Bram Moolenaardac13472019-09-16 21:06:21 +0200271 check_string_option(&buf->b_p_inde);
272 check_string_option(&buf->b_p_indk);
273#endif
274#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
275 check_string_option(&buf->b_p_bexpr);
276#endif
277#if defined(FEAT_CRYPT)
278 check_string_option(&buf->b_p_cm);
279#endif
280 check_string_option(&buf->b_p_fp);
281#if defined(FEAT_EVAL)
282 check_string_option(&buf->b_p_fex);
283#endif
284#ifdef FEAT_CRYPT
285 check_string_option(&buf->b_p_key);
286#endif
287 check_string_option(&buf->b_p_kp);
288 check_string_option(&buf->b_p_mps);
289 check_string_option(&buf->b_p_fo);
290 check_string_option(&buf->b_p_flp);
291 check_string_option(&buf->b_p_isk);
Bram Moolenaardac13472019-09-16 21:06:21 +0200292 check_string_option(&buf->b_p_com);
Bram Moolenaardac13472019-09-16 21:06:21 +0200293#ifdef FEAT_FOLDING
294 check_string_option(&buf->b_p_cms);
295#endif
296 check_string_option(&buf->b_p_nf);
Bram Moolenaardac13472019-09-16 21:06:21 +0200297 check_string_option(&buf->b_p_qe);
Bram Moolenaardac13472019-09-16 21:06:21 +0200298#ifdef FEAT_SYN_HL
299 check_string_option(&buf->b_p_syn);
300 check_string_option(&buf->b_s.b_syn_isk);
301#endif
302#ifdef FEAT_SPELL
303 check_string_option(&buf->b_s.b_p_spc);
304 check_string_option(&buf->b_s.b_p_spf);
305 check_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +0200306 check_string_option(&buf->b_s.b_p_spo);
Bram Moolenaardac13472019-09-16 21:06:21 +0200307#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200308 check_string_option(&buf->b_p_sua);
Bram Moolenaardac13472019-09-16 21:06:21 +0200309 check_string_option(&buf->b_p_cink);
310 check_string_option(&buf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +0100311 check_string_option(&buf->b_p_cinsd);
Bram Moolenaardac13472019-09-16 21:06:21 +0200312 parse_cino(buf);
Bram Moolenaar49846fb2022-10-15 16:05:33 +0100313 check_string_option(&buf->b_p_lop);
Bram Moolenaardac13472019-09-16 21:06:21 +0200314 check_string_option(&buf->b_p_ft);
Bram Moolenaardac13472019-09-16 21:06:21 +0200315 check_string_option(&buf->b_p_cinw);
zeertzjq529b9ad2024-06-05 20:27:06 +0200316 check_string_option(&buf->b_p_cot);
Bram Moolenaardac13472019-09-16 21:06:21 +0200317 check_string_option(&buf->b_p_cpt);
glepnirbcd59952025-04-24 21:48:35 +0200318 check_string_option(&buf->b_p_ise);
Bram Moolenaardac13472019-09-16 21:06:21 +0200319#ifdef FEAT_COMPL_FUNC
320 check_string_option(&buf->b_p_cfu);
321 check_string_option(&buf->b_p_ofu);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +0100322 check_string_option(&buf->b_p_tsrfu);
Bram Moolenaardac13472019-09-16 21:06:21 +0200323#endif
324#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +0100325 check_string_option(&buf->b_p_ffu);
Bram Moolenaardac13472019-09-16 21:06:21 +0200326 check_string_option(&buf->b_p_tfu);
327#endif
328#ifdef FEAT_KEYMAP
329 check_string_option(&buf->b_p_keymap);
330#endif
331#ifdef FEAT_QUICKFIX
glepnir7b9eb632025-05-16 19:49:23 +0200332 check_string_option(&buf->b_p_gefm);
Bram Moolenaardac13472019-09-16 21:06:21 +0200333 check_string_option(&buf->b_p_gp);
334 check_string_option(&buf->b_p_mp);
335 check_string_option(&buf->b_p_efm);
336#endif
337 check_string_option(&buf->b_p_ep);
338 check_string_option(&buf->b_p_path);
339 check_string_option(&buf->b_p_tags);
340 check_string_option(&buf->b_p_tc);
341 check_string_option(&buf->b_p_dict);
342 check_string_option(&buf->b_p_tsr);
Bram Moolenaardac13472019-09-16 21:06:21 +0200343 check_string_option(&buf->b_p_lw);
Bram Moolenaardac13472019-09-16 21:06:21 +0200344 check_string_option(&buf->b_p_bkc);
345 check_string_option(&buf->b_p_menc);
346#ifdef FEAT_VARTABS
347 check_string_option(&buf->b_p_vsts);
348 check_string_option(&buf->b_p_vts);
349#endif
350}
351
352/*
353 * Free the string allocated for an option.
354 * Checks for the string being empty_option. This may happen if we're out of
355 * memory, vim_strsave() returned NULL, which was replaced by empty_option by
356 * check_options().
357 * Does NOT check for P_ALLOCED flag!
358 */
359 void
360free_string_option(char_u *p)
361{
362 if (p != empty_option)
363 vim_free(p);
364}
365
366 void
367clear_string_option(char_u **pp)
368{
369 if (*pp != empty_option)
370 vim_free(*pp);
371 *pp = empty_option;
372}
373
374 void
375check_string_option(char_u **pp)
376{
377 if (*pp == NULL)
378 *pp = empty_option;
379}
380
381/*
382 * Set global value for string option when it's a local option.
383 */
384 static void
385set_string_option_global(
386 int opt_idx, // option index
387 char_u **varp) // pointer to option variable
388{
389 char_u **p, *s;
390
391 // the global value is always allocated
392 if (is_window_local_option(opt_idx))
393 p = (char_u **)GLOBAL_WO(varp);
394 else
395 p = (char_u **)get_option_var(opt_idx);
396 if (!is_global_option(opt_idx)
397 && p != varp
398 && (s = vim_strsave(*varp)) != NULL)
399 {
400 free_string_option(*p);
401 *p = s;
402 }
403}
404
405/*
406 * Set a string option to a new value (without checking the effect).
407 * The string is copied into allocated memory.
408 * if ("opt_idx" == -1) "name" is used, otherwise "opt_idx" is used.
409 * When "set_sid" is zero set the scriptID to current_sctx.sc_sid. When
410 * "set_sid" is SID_NONE don't set the scriptID. Otherwise set the scriptID to
411 * "set_sid".
412 */
413 void
414set_string_option_direct(
415 char_u *name,
416 int opt_idx,
417 char_u *val,
418 int opt_flags, // OPT_FREE, OPT_LOCAL and/or OPT_GLOBAL
419 int set_sid UNUSED)
420{
421 char_u *s;
422 char_u **varp;
423 int both = (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0;
424 int idx = opt_idx;
425
426 if (idx == -1) // use name
427 {
428 idx = findoption(name);
429 if (idx < 0) // not found (should not happen)
430 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000431 semsg(_(e_internal_error_str), "set_string_option_direct()");
RestorerZ68ebcee2023-05-31 17:12:14 +0100432 siemsg("For option %s", name);
Bram Moolenaardac13472019-09-16 21:06:21 +0200433 return;
434 }
435 }
436
437 if (is_hidden_option(idx)) // can't set hidden option
438 return;
439
440 s = vim_strsave(val);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000441 if (s == NULL)
442 return;
443
444 varp = (char_u **)get_option_varp_scope(idx,
445 both ? OPT_LOCAL : opt_flags);
446 if ((opt_flags & OPT_FREE) && (get_option_flags(idx) & P_ALLOCED))
447 free_string_option(*varp);
448 *varp = s;
449
450 // For buffer/window local option may also set the global value.
451 if (both)
452 set_string_option_global(idx, varp);
453
454 set_option_flag(idx, P_ALLOCED);
455
456 // When setting both values of a global option with a local value,
457 // make the local value empty, so that the global value is used.
458 if (is_global_local_option(idx) && both)
Bram Moolenaardac13472019-09-16 21:06:21 +0200459 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000460 free_string_option(*varp);
461 *varp = empty_option;
Bram Moolenaardac13472019-09-16 21:06:21 +0200462 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000463# ifdef FEAT_EVAL
464 if (set_sid != SID_NONE)
465 {
466 sctx_T script_ctx;
467
468 if (set_sid == 0)
469 script_ctx = current_sctx;
470 else
471 {
472 script_ctx.sc_sid = set_sid;
473 script_ctx.sc_seq = 0;
474 script_ctx.sc_lnum = 0;
475 script_ctx.sc_version = 1;
476 }
477 set_option_sctx_idx(idx, opt_flags, script_ctx);
478 }
479# endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200480}
481
Dominique Pellee764d1b2023-03-12 21:20:59 +0000482#if defined(FEAT_PROP_POPUP) || \
483 (defined(FEAT_DIFF) && defined(FEAT_FOLDING)) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200484/*
485 * Like set_string_option_direct(), but for a window-local option in "wp".
486 * Blocks autocommands to avoid the old curwin becoming invalid.
487 */
488 void
489set_string_option_direct_in_win(
490 win_T *wp,
491 char_u *name,
492 int opt_idx,
493 char_u *val,
494 int opt_flags,
495 int set_sid)
496{
497 win_T *save_curwin = curwin;
498
499 block_autocmds();
500 curwin = wp;
501 curbuf = curwin->w_buffer;
502 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
503 curwin = save_curwin;
504 curbuf = curwin->w_buffer;
505 unblock_autocmds();
506}
Dominique Pellee764d1b2023-03-12 21:20:59 +0000507#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200508
Dominique Pelle748b3082022-01-08 12:41:16 +0000509#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaardac13472019-09-16 21:06:21 +0200510/*
511 * Like set_string_option_direct(), but for a buffer-local option in "buf".
512 * Blocks autocommands to avoid the old curbuf becoming invalid.
513 */
514 void
515set_string_option_direct_in_buf(
516 buf_T *buf,
517 char_u *name,
518 int opt_idx,
519 char_u *val,
520 int opt_flags,
521 int set_sid)
522{
523 buf_T *save_curbuf = curbuf;
524
525 block_autocmds();
526 curbuf = buf;
527 curwin->w_buffer = curbuf;
528 set_string_option_direct(name, opt_idx, val, opt_flags, set_sid);
529 curbuf = save_curbuf;
530 curwin->w_buffer = curbuf;
531 unblock_autocmds();
532}
Dominique Pelle748b3082022-01-08 12:41:16 +0000533#endif
Bram Moolenaardac13472019-09-16 21:06:21 +0200534
535/*
536 * Set a string option to a new value, and handle the effects.
537 *
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100538 * Returns NULL on success or an untranslated error message on error.
Bram Moolenaardac13472019-09-16 21:06:21 +0200539 */
540 char *
541set_string_option(
542 int opt_idx,
543 char_u *value,
Yegappan Lakshmanan32ff96e2023-02-13 16:10:04 +0000544 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Christian Brabandtb39b2402023-11-29 11:34:05 +0100545 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +0100546 size_t errbuflen)
Bram Moolenaardac13472019-09-16 21:06:21 +0200547{
548 char_u *s;
549 char_u **varp;
550 char_u *oldval;
551#if defined(FEAT_EVAL)
552 char_u *oldval_l = NULL;
553 char_u *oldval_g = NULL;
554 char_u *saved_oldval = NULL;
555 char_u *saved_oldval_l = NULL;
556 char_u *saved_oldval_g = NULL;
557 char_u *saved_newval = NULL;
558#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100559 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +0200560 int value_checked = FALSE;
561
562 if (is_hidden_option(opt_idx)) // don't set hidden option
563 return NULL;
564
Bram Moolenaar7f009df2020-03-16 20:27:38 +0100565 s = vim_strsave(value == NULL ? (char_u *)"" : value);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000566 if (s == NULL)
567 return NULL;
568
569 varp = (char_u **)get_option_varp_scope(opt_idx,
570 (opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
571 ? (is_global_local_option(opt_idx)
572 ? OPT_GLOBAL : OPT_LOCAL)
573 : opt_flags);
574 oldval = *varp;
575#if defined(FEAT_EVAL)
576 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +0200577 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000578 oldval_l = *(char_u **)get_option_varp_scope(opt_idx, OPT_LOCAL);
579 oldval_g = *(char_u **)get_option_varp_scope(opt_idx, OPT_GLOBAL);
Bram Moolenaardac13472019-09-16 21:06:21 +0200580 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000581#endif
582 *varp = s;
583
584#if defined(FEAT_EVAL)
585 if (!starting
586# ifdef FEAT_CRYPT
587 && !is_crypt_key_option(opt_idx)
588# endif
589 )
590 {
591 if (oldval_l != NULL)
592 saved_oldval_l = vim_strsave(oldval_l);
593 if (oldval_g != NULL)
594 saved_oldval_g = vim_strsave(oldval_g);
595 saved_oldval = vim_strsave(oldval);
596 saved_newval = vim_strsave(s);
597 }
598#endif
Yegappan Lakshmananaf936912023-02-20 12:16:39 +0000599 if ((errmsg = did_set_string_option(opt_idx, varp, oldval, value, errbuf,
Christian Brabandtb39b2402023-11-29 11:34:05 +0100600 errbuflen, opt_flags, OP_NONE, &value_checked)) == NULL)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000601 did_set_option(opt_idx, opt_flags, TRUE, value_checked);
602
603#if defined(FEAT_EVAL)
604 // call autocommand after handling side effects
605 if (errmsg == NULL)
606 trigger_optionset_string(opt_idx, opt_flags,
607 saved_oldval, saved_oldval_l,
608 saved_oldval_g, saved_newval);
609 vim_free(saved_oldval);
610 vim_free(saved_oldval_l);
611 vim_free(saved_oldval_g);
612 vim_free(saved_newval);
613#endif
Bram Moolenaar31e5c602022-04-15 13:53:33 +0100614 return errmsg;
Bram Moolenaardac13472019-09-16 21:06:21 +0200615}
616
617/*
618 * Return TRUE if "val" is a valid 'filetype' name.
619 * Also used for 'syntax' and 'keymap'.
620 */
621 static int
622valid_filetype(char_u *val)
623{
624 return valid_name(val, ".-_");
625}
626
627#ifdef FEAT_STL_OPT
628/*
629 * Check validity of options with the 'statusline' format.
zeertzjq5dc294a2022-04-15 13:17:57 +0100630 * Return an untranslated error message or NULL.
Bram Moolenaardac13472019-09-16 21:06:21 +0200631 */
632 static char *
633check_stl_option(char_u *s)
634{
Bram Moolenaardac13472019-09-16 21:06:21 +0200635 int groupdepth = 0;
Christian Brabandtb39b2402023-11-29 11:34:05 +0100636 static char errbuf[ERR_BUFLEN];
637 int errbuflen = ERR_BUFLEN;
Bram Moolenaardac13472019-09-16 21:06:21 +0200638
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100639 while (*s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200640 {
641 // Check for valid keys after % sequences
642 while (*s && *s != '%')
643 s++;
644 if (!*s)
645 break;
646 s++;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +0000647 if (*s == '%' || *s == STL_TRUNCMARK || *s == STL_SEPARATE)
Bram Moolenaardac13472019-09-16 21:06:21 +0200648 {
649 s++;
650 continue;
651 }
652 if (*s == ')')
653 {
654 s++;
655 if (--groupdepth < 0)
656 break;
657 continue;
658 }
659 if (*s == '-')
660 s++;
661 while (VIM_ISDIGIT(*s))
662 s++;
663 if (*s == STL_USER_HL)
664 continue;
665 if (*s == '.')
666 {
667 s++;
668 while (*s && VIM_ISDIGIT(*s))
669 s++;
670 }
671 if (*s == '(')
672 {
673 groupdepth++;
674 continue;
675 }
676 if (vim_strchr(STL_ALL, *s) == NULL)
677 {
Christian Brabandtb39b2402023-11-29 11:34:05 +0100678 return illegal_char(errbuf, errbuflen, *s);
Bram Moolenaardac13472019-09-16 21:06:21 +0200679 }
680 if (*s == '{')
681 {
zeertzjq5dc294a2022-04-15 13:17:57 +0100682 int reevaluate = (*++s == '%');
shadmansaleh30e3de22021-05-15 17:23:28 +0200683
zeertzjq5dc294a2022-04-15 13:17:57 +0100684 if (reevaluate && *++s == '}')
685 // "}" is not allowed immediately after "%{%"
Christian Brabandtb39b2402023-11-29 11:34:05 +0100686 return illegal_char(errbuf, errbuflen, '}');
shadmansaleh30e3de22021-05-15 17:23:28 +0200687 while ((*s != '}' || (reevaluate && s[-1] != '%')) && *s)
Bram Moolenaardac13472019-09-16 21:06:21 +0200688 s++;
689 if (*s != '}')
zeertzjq5dc294a2022-04-15 13:17:57 +0100690 return e_unclosed_expression_sequence;
Bram Moolenaardac13472019-09-16 21:06:21 +0200691 }
692 }
Bram Moolenaardac13472019-09-16 21:06:21 +0200693 if (groupdepth != 0)
zeertzjq5dc294a2022-04-15 13:17:57 +0100694 return e_unbalanced_groups;
Bram Moolenaardac13472019-09-16 21:06:21 +0200695 return NULL;
696}
697#endif
698
699/*
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +0000700 * Check for a "normal" directory or file name in some options. Disallow a
701 * path separator (slash and/or backslash), wildcards and characters that are
702 * often illegal in a file name. Be more permissive if "secure" is off.
703 */
704 static int
705check_illegal_path_names(int opt_idx, char_u **varp)
706{
707 return (((get_option_flags(opt_idx) & P_NFNAME)
708 && vim_strpbrk(*varp, (char_u *)(secure
709 ? "/\\*?[|;&<>\r\n" : "/\\*?[<>\r\n")) != NULL)
710 || ((get_option_flags(opt_idx) & P_NDNAME)
711 && vim_strpbrk(*varp, (char_u *)"*?[|;&<>\r\n") != NULL));
712}
713
714/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000715 * An option that accepts a list of flags is changed.
716 * e.g. 'viewoptions', 'switchbuf', 'casemap', etc.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000717 */
718 static char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000719did_set_opt_flags(char_u *val, char **values, unsigned *flagp, int list)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000720{
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000721 if (opt_strings_flags(val, values, flagp, list) == FAIL)
722 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +0000723
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000724 return NULL;
725}
726
727/*
728 * An option that accepts a list of string values is changed.
729 * e.g. 'nrformats', 'scrollopt', 'wildoptions', etc.
730 */
731 static char *
732did_set_opt_strings(char_u *val, char **values, int list)
733{
734 return did_set_opt_flags(val, values, NULL, list);
735}
736
737/*
738 * An option which is a list of flags is set. Valid values are in 'flags'.
739 */
740 static char *
Christian Brabandtb39b2402023-11-29 11:34:05 +0100741did_set_option_listflag(
742 char_u *val,
743 char_u *flags,
744 char *errbuf,
Mike Williams620f0112023-12-05 15:36:06 +0100745 size_t errbuflen)
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000746{
747 char_u *s;
748
Yegappan Lakshmananc727b192023-03-03 12:26:15 +0000749 for (s = val; *s; ++s)
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000750 if (vim_strchr(flags, *s) == NULL)
Christian Brabandtb39b2402023-11-29 11:34:05 +0100751 return illegal_char(errbuf, errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +0000752
753 return NULL;
754}
755
756/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200757 * Expand an option that accepts a list of fixed string values with known
758 * number of items.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200759 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200760 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200761expand_set_opt_string(
762 optexpand_T *args,
763 char **values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +0200764 size_t numValues,
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200765 int *numMatches,
766 char_u ***matches)
767{
768 char_u *p;
769 regmatch_T *regmatch = args->oe_regmatch;
770 int include_orig_val = args->oe_include_orig_val;
771 char_u *option_val = args->oe_opt_value;
772
773 // Assume numValues is small since they are fixed enums, so just allocate
774 // upfront instead of needing two passes to calculate output size.
775 *matches = ALLOC_MULT(char_u *, numValues + 1);
776 if (*matches == NULL)
777 return FAIL;
778
779 int count = 0;
780
781 if (include_orig_val && *option_val != NUL)
782 {
783 p = vim_strsave(option_val);
784 if (p == NULL)
785 {
786 VIM_CLEAR(*matches);
787 return FAIL;
788 }
789 (*matches)[count++] = p;
790 }
791
792 for (char **val = values; *val != NULL; val++)
793 {
794 if (include_orig_val && *option_val != NUL)
795 {
796 if (STRCMP((char_u*)*val, option_val) == 0)
797 continue;
798 }
799 if (vim_regexec(regmatch, (char_u*)(*val), (colnr_T)0))
800 {
801 p = vim_strsave((char_u*)*val);
802 if (p == NULL)
803 {
804 if (count == 0)
805 {
806 VIM_CLEAR(*matches);
807 return FAIL;
808 }
809 else
810 break;
811 }
812 (*matches)[count++] = p;
813 }
814 }
815 if (count == 0)
816 {
817 VIM_CLEAR(*matches);
818 return FAIL;
819 }
820 *numMatches = count;
821 return OK;
822}
823
824static char_u *set_opt_callback_orig_option = NULL;
825static char_u *((*set_opt_callback_func)(expand_T *, int));
826
827/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200828 * Callback used by expand_set_opt_generic to also include the original value
829 * as the first item.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200830 */
831 static char_u *
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200832expand_set_opt_generic_cb(expand_T *xp, int idx)
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200833{
834 if (idx == 0)
835 {
836 if (set_opt_callback_orig_option != NULL)
837 return set_opt_callback_orig_option;
838 else
839 return (char_u *)""; // empty strings are ignored
840 }
841 return set_opt_callback_func(xp, idx - 1);
842}
843
844/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200845 * Expand an option with a callback that iterates through a list of possible
846 * names using an index.
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200847 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200848 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200849expand_set_opt_generic(
850 optexpand_T *args,
851 char_u *((*func)(expand_T *, int)),
852 int *numMatches,
853 char_u ***matches)
854{
855 int ret;
856
857 set_opt_callback_orig_option = args->oe_include_orig_val ?
858 args->oe_opt_value : NULL;
859 set_opt_callback_func = func;
860
861 ret = ExpandGeneric(
862 (char_u*)"", // not using fuzzy as currently EXPAND_STRING_SETTING doesn't use it
863 args->oe_xp,
864 args->oe_regmatch,
865 matches,
866 numMatches,
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200867 expand_set_opt_generic_cb,
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200868 FALSE);
869
870 set_opt_callback_orig_option = NULL;
871 set_opt_callback_func = NULL;
872 return ret;
873}
874
Christian Brabandt9960ebc2023-10-05 22:17:09 +0200875# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200876static garray_T *expand_cb_ga;
877static optexpand_T *expand_cb_args;
878
879/*
880 * Callback provided to a function in expand_set_opt_callback. Will perform
881 * regex matching against the value and add to the list.
882 *
883 * Returns OK usually. Returns FAIL if it failed to allocate memory, and the
884 * caller should terminate the enumeration.
885 */
886 static int
887expand_set_opt_callback_cb(char_u *val)
888{
889 regmatch_T *regmatch = expand_cb_args->oe_regmatch;
890 expand_T *xp = expand_cb_args->oe_xp;
891 garray_T *ga = expand_cb_ga;
892 char_u *str;
893
894 if (val == NULL || *val == NUL)
895 return OK;
896
897 if (xp->xp_pattern[0] != NUL &&
898 !vim_regexec(regmatch, val, (colnr_T)0))
899 return OK;
900
901 str = vim_strsave_escaped(val, (char_u *)" \t\\");
902
903 if (str == NULL)
904 return FAIL;
905
906 if (ga_grow(ga, 1) == FAIL)
907 {
908 vim_free(str);
909 return FAIL;
910 }
911
912 ((char_u **)ga->ga_data)[ga->ga_len] = str;
913 ++ga->ga_len;
914 return OK;
915}
916
917/*
918 * Expand an option with a provided function that takes a callback. The
919 * function will enumerate through all options and call the callback to add it
920 * to the list.
921 *
922 * "func" is the enumerator function that will generate the list of options.
923 * "func_params" is a single parameter that will be passed to func.
924 */
925 static int
926expand_set_opt_callback(
927 optexpand_T *args,
928 void (*func)(optexpand_T *, void* params, int (*cb)(char_u *val)),
929 void *func_params,
930 int *numMatches,
931 char_u ***matches)
932{
933 garray_T ga;
934 int include_orig_val = args->oe_include_orig_val;
935 char_u *option_val = args->oe_opt_value;
936
937 ga_init2(&ga, sizeof(char *), 30);
938
939 if (include_orig_val && *option_val != NUL)
940 {
941 char_u *p = vim_strsave(option_val);
942 if (p == NULL)
943 return FAIL;
944 if (ga_grow(&ga, 1) == FAIL)
945 {
946 vim_free(p);
947 return FAIL;
948 }
949 ((char_u **)ga.ga_data)[ga.ga_len] = p;
950 ++ga.ga_len;
951 }
952
953 expand_cb_ga = &ga;
954 expand_cb_args = args;
955
956 func(args, func_params, expand_set_opt_callback_cb);
957
958 expand_cb_ga = NULL;
959 expand_cb_args = NULL;
960
961 *matches = ga.ga_data;
962 *numMatches = ga.ga_len;
963 return OK;
964}
Christian Brabandt9960ebc2023-10-05 22:17:09 +0200965#endif
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200966
967/*
968 * Expand an option which is a list of flags.
969 */
Yee Cheng Chinf7f746b2023-09-30 12:28:50 +0200970 static int
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200971expand_set_opt_listflag(
972 optexpand_T *args,
973 char_u *flags,
974 int *numMatches,
975 char_u ***matches)
976{
977 char_u *p;
978 char_u *option_val = args->oe_opt_value;
979 char_u *cmdline_val = args->oe_set_arg;
980 int append = args->oe_append;
981 int include_orig_val = args->oe_include_orig_val && (*option_val != NUL);
982
Yee Cheng Chin6d113472023-10-02 21:38:39 +0200983 size_t num_flags = STRLEN(flags);
Yee Cheng Chin900894b2023-09-29 20:42:32 +0200984
985 // Assume we only have small number of flags, so just allocate max size.
986 *matches = ALLOC_MULT(char_u *, num_flags + 1);
987 if (*matches == NULL)
988 return FAIL;
989
990 int count = 0;
991
992 if (include_orig_val)
993 {
994 p = vim_strsave(option_val);
995 if (p == NULL)
996 {
997 VIM_CLEAR(*matches);
998 return FAIL;
999 }
1000 (*matches)[count++] = p;
1001 }
1002
1003 for (char_u *flag = flags; *flag != NUL; flag++)
1004 {
1005 if (append && vim_strchr(option_val, *flag) != NULL)
1006 continue;
1007
1008 if (vim_strchr(cmdline_val, *flag) == NULL)
1009 {
1010 if (include_orig_val
1011 && option_val[1] == NUL
1012 && *flag == option_val[0])
1013 {
1014 // This value is already used as the first choice as it's the
1015 // existing flag. Just skip it to avoid duplicate.
1016 continue;
1017 }
1018 p = vim_strnsave(flag, 1);
1019 if (p == NULL)
1020 {
1021 if (count == 0)
1022 {
1023 VIM_CLEAR(*matches);
1024 return FAIL;
1025 }
1026 else
1027 break;
1028 }
1029 (*matches)[count++] = p;
1030 }
1031 }
1032
1033 if (count == 0)
1034 {
1035 VIM_CLEAR(*matches);
1036 return FAIL;
1037 }
1038 *numMatches = count;
1039 return OK;
1040}
1041
1042/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001043 * The 'ambiwidth' option is changed.
1044 */
1045 char *
1046did_set_ambiwidth(optset_T *args UNUSED)
1047{
1048 if (check_opt_strings(p_ambw, p_ambw_values, FALSE) != OK)
1049 return e_invalid_argument;
1050
1051 return check_chars_options();
1052}
1053
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001054 int
1055expand_set_ambiwidth(optexpand_T *args, int *numMatches, char_u ***matches)
1056{
1057 return expand_set_opt_string(
1058 args,
1059 p_ambw_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001060 ARRAY_LENGTH(p_ambw_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001061 numMatches,
1062 matches);
1063}
1064
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001065/*
1066 * The 'background' option is changed.
1067 */
1068 char *
Gregory Anders83ad2722024-01-03 19:48:51 +01001069did_set_background(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001070{
1071 if (check_opt_strings(p_bg, p_bg_values, FALSE) == FAIL)
1072 return e_invalid_argument;
1073
Gregory Anders83ad2722024-01-03 19:48:51 +01001074 if (args->os_oldval.string != NULL && args->os_oldval.string[0] == *p_bg)
1075 // Value was not changed
1076 return NULL;
1077
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001078#ifdef FEAT_EVAL
1079 int dark = (*p_bg == 'd');
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001080#endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001081
1082 init_highlight(FALSE, FALSE);
1083
1084#ifdef FEAT_EVAL
1085 if (dark != (*p_bg == 'd')
1086 && get_var_value((char_u *)"g:colors_name") != NULL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001087 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001088 // The color scheme must have set 'background' back to another
1089 // value, that's not what we want here. Disable the color
1090 // scheme and set the colors again.
1091 do_unlet((char_u *)"g:colors_name", TRUE);
1092 free_string_option(p_bg);
1093 p_bg = vim_strsave((char_u *)(dark ? "dark" : "light"));
1094 check_string_option(&p_bg);
1095 init_highlight(FALSE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001096 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001097#endif
1098#ifdef FEAT_TERMINAL
1099 term_update_colors_all();
1100#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001101
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001102 return NULL;
1103}
1104
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001105 int
1106expand_set_background(optexpand_T *args, int *numMatches, char_u ***matches)
1107{
1108 return expand_set_opt_string(
1109 args,
1110 p_bg_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001111 ARRAY_LENGTH(p_bg_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001112 numMatches,
1113 matches);
1114}
1115
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001116/*
1117 * The 'backspace' option is changed.
1118 */
1119 char *
1120did_set_backspace(optset_T *args UNUSED)
1121{
1122 if (VIM_ISDIGIT(*p_bs))
1123 {
1124 if (*p_bs > '3' || p_bs[1] != NUL)
1125 return e_invalid_argument;
1126 }
1127 else if (check_opt_strings(p_bs, p_bs_values, TRUE) != OK)
1128 return e_invalid_argument;
1129
1130 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001131}
1132
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001133 int
1134expand_set_backspace(optexpand_T *args, int *numMatches, char_u ***matches)
1135{
1136 return expand_set_opt_string(
1137 args,
1138 p_bs_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001139 ARRAY_LENGTH(p_bs_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001140 numMatches,
1141 matches);
1142}
1143
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001144/*
1145 * The 'backupcopy' option is changed.
1146 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001147 char *
1148did_set_backupcopy(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001149{
1150 char_u *bkc = p_bkc;
1151 unsigned int *flags = &bkc_flags;
1152 char *errmsg = NULL;
1153
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001154 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001155 {
1156 bkc = curbuf->b_p_bkc;
1157 flags = &curbuf->b_bkc_flags;
1158 }
zeertzjq46dcd842024-11-03 09:10:50 +01001159 else if (!(args->os_flags & OPT_GLOBAL))
1160 // When using :set, clear the local flags.
1161 curbuf->b_bkc_flags = 0;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001162
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001163 if ((args->os_flags & OPT_LOCAL) && *bkc == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001164 // make the local value empty: use the global value
1165 *flags = 0;
1166 else
1167 {
1168 if (opt_strings_flags(bkc, p_bkc_values, flags, TRUE) != OK)
1169 errmsg = e_invalid_argument;
1170 if ((((int)*flags & BKC_AUTO) != 0)
1171 + (((int)*flags & BKC_YES) != 0)
1172 + (((int)*flags & BKC_NO) != 0) != 1)
1173 {
1174 // Must have exactly one of "auto", "yes" and "no".
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001175 (void)opt_strings_flags(args->os_oldval.string, p_bkc_values,
1176 flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001177 errmsg = e_invalid_argument;
1178 }
1179 }
1180
1181 return errmsg;
1182}
1183
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001184 int
1185expand_set_backupcopy(optexpand_T *args, int *numMatches, char_u ***matches)
1186{
1187 return expand_set_opt_string(
1188 args,
1189 p_bkc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001190 ARRAY_LENGTH(p_bkc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001191 numMatches,
1192 matches);
1193}
1194
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001195/*
1196 * The 'backupext' or the 'patchmode' option is changed.
1197 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001198 char *
1199did_set_backupext_or_patchmode(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001200{
1201 if (STRCMP(*p_bex == '.' ? p_bex + 1 : p_bex,
1202 *p_pm == '.' ? p_pm + 1 : p_pm) == 0)
1203 return e_backupext_and_patchmode_are_equal;
1204
1205 return NULL;
1206}
1207
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001208/*
1209 * The 'belloff' option is changed.
1210 */
1211 char *
1212did_set_belloff(optset_T *args UNUSED)
1213{
1214 return did_set_opt_flags(p_bo, p_bo_values, &bo_flags, TRUE);
1215}
1216
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001217 int
1218expand_set_belloff(optexpand_T *args, int *numMatches, char_u ***matches)
1219{
1220 return expand_set_opt_string(
1221 args,
1222 p_bo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001223 ARRAY_LENGTH(p_bo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001224 numMatches,
1225 matches);
1226}
1227
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001228#if defined(FEAT_LINEBREAK) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001229/*
zeertzjqeac3fdc2024-02-03 18:08:09 +01001230 * The 'breakat' option is changed.
1231 */
1232 char *
1233did_set_breakat(optset_T *args UNUSED)
1234{
1235 char_u *p;
1236 int i;
1237
1238 for (i = 0; i < 256; i++)
1239 breakat_flags[i] = FALSE;
1240
1241 if (p_breakat != NULL)
1242 for (p = p_breakat; *p; p++)
1243 breakat_flags[*p] = TRUE;
1244
1245 return NULL;
1246}
1247
1248/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001249 * The 'breakindentopt' option is changed.
1250 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001251 char *
Millyb38700a2024-10-22 22:59:39 +02001252did_set_breakindentopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001253{
Millyb38700a2024-10-22 22:59:39 +02001254 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001255
Millyb38700a2024-10-22 22:59:39 +02001256 if (briopt_check(*varp, varp == &curwin->w_p_briopt ? curwin : NULL)
1257 == FAIL)
1258 return e_invalid_argument;
1259
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001260 // list setting requires a redraw
Millyb38700a2024-10-22 22:59:39 +02001261 if (varp == &curwin->w_p_briopt && curwin->w_briopt_list)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001262 redraw_all_later(UPD_NOT_VALID);
1263
Millyb38700a2024-10-22 22:59:39 +02001264 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001265}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001266
1267 int
1268expand_set_breakindentopt(optexpand_T *args, int *numMatches, char_u ***matches)
1269{
1270 return expand_set_opt_string(
1271 args,
1272 p_briopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001273 ARRAY_LENGTH(p_briopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001274 numMatches,
1275 matches);
1276}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001277#endif
1278
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001279#if defined(FEAT_BROWSE) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001280/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001281 * The 'browsedir' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001282 */
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00001283 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001284did_set_browsedir(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001285{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001286 if (check_opt_strings(p_bsdir, p_bsdir_values, FALSE) != OK
1287 && !mch_isdir(p_bsdir))
1288 return e_invalid_argument;
1289
1290 return NULL;
1291}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001292
1293 int
1294expand_set_browsedir(optexpand_T *args, int *numMatches, char_u ***matches)
1295{
1296 return expand_set_opt_string(
1297 args,
1298 p_bsdir_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001299 ARRAY_LENGTH(p_bsdir_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001300 numMatches,
1301 matches);
1302}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001303#endif
1304
1305/*
1306 * The 'bufhidden' option is changed.
1307 */
1308 char *
1309did_set_bufhidden(optset_T *args UNUSED)
1310{
1311 return did_set_opt_strings(curbuf->b_p_bh, p_bufhidden_values, FALSE);
1312}
1313
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001314 int
1315expand_set_bufhidden(optexpand_T *args, int *numMatches, char_u ***matches)
1316{
1317 return expand_set_opt_string(
1318 args,
1319 p_bufhidden_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001320 ARRAY_LENGTH(p_bufhidden_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001321 numMatches,
1322 matches);
1323}
1324
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001325/*
1326 * The 'buftype' option is changed.
1327 */
1328 char *
1329did_set_buftype(optset_T *args UNUSED)
1330{
1331 if (check_opt_strings(curbuf->b_p_bt, p_buftype_values, FALSE) != OK)
1332 return e_invalid_argument;
1333
1334 if (curwin->w_status_height)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001335 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001336 curwin->w_redr_status = TRUE;
1337 redraw_later(UPD_VALID);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001338 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001339 curbuf->b_help = (curbuf->b_p_bt[0] == 'h');
1340 redraw_titles();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001341
1342 return NULL;
1343}
1344
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001345 int
1346expand_set_buftype(optexpand_T *args, int *numMatches, char_u ***matches)
1347{
1348 return expand_set_opt_string(
1349 args,
1350 p_buftype_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001351 ARRAY_LENGTH(p_buftype_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001352 numMatches,
1353 matches);
1354}
1355
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001356/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001357 * The 'casemap' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001358 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001359 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001360did_set_casemap(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001361{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001362 return did_set_opt_flags(p_cmp, p_cmp_values, &cmp_flags, TRUE);
1363}
1364
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001365 int
1366expand_set_casemap(optexpand_T *args, int *numMatches, char_u ***matches)
1367{
1368 return expand_set_opt_string(
1369 args,
1370 p_cmp_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001371 ARRAY_LENGTH(p_cmp_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001372 numMatches,
1373 matches);
1374}
1375
1376#if defined(FEAT_CLIPBOARD) || defined(PROTO)
1377 int
1378expand_set_clipboard(optexpand_T *args, int *numMatches, char_u ***matches)
1379{
1380 return expand_set_opt_string(
1381 args,
1382 p_cb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001383 ARRAY_LENGTH(p_cb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001384 numMatches,
1385 matches);
1386}
1387#endif
1388
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001389/*
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001390 * The global 'listchars' or 'fillchars' option is changed.
1391 */
1392 static char *
zeertzjq6a8d2e12024-01-17 20:54:49 +01001393did_set_global_listfillchars(char_u *val, int opt_lcs, int opt_flags,
1394 char *errbuf, size_t errbuflen)
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001395{
1396 char *errmsg = NULL;
1397 char_u **local_ptr = opt_lcs ? &curwin->w_p_lcs : &curwin->w_p_fcs;
1398
1399 // only apply the global value to "curwin" when it does not have a
1400 // local value
1401 if (opt_lcs)
1402 errmsg = set_listchars_option(curwin, val,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001403 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL),
1404 errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001405 else
1406 errmsg = set_fillchars_option(curwin, val,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001407 **local_ptr == NUL || !(opt_flags & OPT_GLOBAL),
1408 errbuf, errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001409 if (errmsg != NULL)
1410 return errmsg;
1411
1412 tabpage_T *tp;
1413 win_T *wp;
1414
1415 // If the current window is set to use the global
1416 // 'listchars'/'fillchars' value, clear the window-local value.
1417 if (!(opt_flags & OPT_GLOBAL))
1418 clear_string_option(local_ptr);
1419 FOR_ALL_TAB_WINDOWS(tp, wp)
1420 {
1421 // If the current window has a local value need to apply it
1422 // again, it was changed when setting the global value.
1423 // If no error was returned above, we don't expect an error
1424 // here, so ignore the return value.
1425 if (opt_lcs)
1426 {
1427 if (*wp->w_p_lcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001428 (void)set_listchars_option(wp, wp->w_p_lcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001429 }
1430 else
1431 {
1432 if (*wp->w_p_fcs == NUL)
zeertzjq6a8d2e12024-01-17 20:54:49 +01001433 (void)set_fillchars_option(wp, wp->w_p_fcs, TRUE, NULL, 0);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001434 }
1435 }
1436
1437 redraw_all_later(UPD_NOT_VALID);
1438
1439 return NULL;
1440}
1441
1442/*
1443 * The 'fillchars' option or the 'listchars' option is changed.
1444 */
1445 char *
1446did_set_chars_option(optset_T *args)
1447{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001448 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001449 char *errmsg = NULL;
1450
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001451 if ( varp == &p_lcs // global 'listchars'
1452 || varp == &p_fcs) // global 'fillchars'
1453 errmsg = did_set_global_listfillchars(*varp, varp == &p_lcs,
zeertzjq6a8d2e12024-01-17 20:54:49 +01001454 args->os_flags, args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001455 else if (varp == &curwin->w_p_lcs) // local 'listchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001456 errmsg = set_listchars_option(curwin, *varp, TRUE,
1457 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001458 else if (varp == &curwin->w_p_fcs) // local 'fillchars'
zeertzjq6a8d2e12024-01-17 20:54:49 +01001459 errmsg = set_fillchars_option(curwin, *varp, TRUE,
1460 args->os_errbuf, args->os_errbuflen);
Yegappan Lakshmananc6ff21e2023-03-02 14:46:48 +00001461
1462 return errmsg;
1463}
1464
1465/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001466 * Expand 'fillchars' or 'listchars' option value.
1467 */
1468 int
1469expand_set_chars_option(optexpand_T *args, int *numMatches, char_u ***matches)
1470{
1471 char_u **varp = (char_u **)args->oe_varp;
1472 int is_lcs = (varp == &p_lcs || varp == &curwin->w_p_lcs);
1473 return expand_set_opt_generic(
1474 args,
1475 is_lcs ? get_listchars_name : get_fillchars_name,
1476 numMatches,
1477 matches);
1478}
1479
1480/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001481 * The 'cinoptions' option is changed.
1482 */
1483 char *
1484did_set_cinoptions(optset_T *args UNUSED)
1485{
1486 // TODO: recognize errors
1487 parse_cino(curbuf);
1488
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001489 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001490}
1491
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00001492#if defined(FEAT_SYN_HL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001493/*
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001494 * The 'colorcolumn' option is changed.
1495 */
1496 char *
Millya441a3e2024-10-22 22:43:01 +02001497did_set_colorcolumn(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001498{
Millya441a3e2024-10-22 22:43:01 +02001499 char_u **varp = (char_u **)args->os_varp;
1500
1501 return check_colorcolumn(*varp, varp == &curwin->w_p_cc ? curwin : NULL);
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001502}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001503#endif
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001504
1505/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001506 * The 'comments' option is changed.
1507 */
1508 char *
1509did_set_comments(optset_T *args)
1510{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001511 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001512 char_u *s;
1513 char *errmsg = NULL;
1514
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001515 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001516 {
1517 while (*s && *s != ':')
1518 {
1519 if (vim_strchr((char_u *)COM_ALL, *s) == NULL
1520 && !VIM_ISDIGIT(*s) && *s != '-')
1521 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01001522 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001523 break;
1524 }
1525 ++s;
1526 }
1527 if (*s++ == NUL)
1528 errmsg = e_missing_colon;
1529 else if (*s == ',' || *s == NUL)
1530 errmsg = e_zero_length_string;
1531 if (errmsg != NULL)
1532 break;
1533 while (*s && *s != ',')
1534 {
1535 if (*s == '\\' && s[1] != NUL)
1536 ++s;
1537 ++s;
1538 }
1539 s = skip_to_option_part(s);
1540 }
1541
1542 return errmsg;
1543}
1544
1545#if defined(FEAT_FOLDING) || defined(PROTO)
1546/*
1547 * The 'commentstring' option is changed.
1548 */
1549 char *
1550did_set_commentstring(optset_T *args)
1551{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001552 char_u **varp = (char_u **)args->os_varp;
1553
1554 if (**varp != NUL && strstr((char *)*varp, "%s") == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001555 return e_commentstring_must_be_empty_or_contain_str;
1556
1557 return NULL;
1558}
1559#endif
1560
1561/*
Girish Palyacbe53192025-04-14 22:13:15 +02001562 * Check if value for 'complete' is valid when 'complete' option is changed.
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001563 */
1564 char *
1565did_set_complete(optset_T *args)
1566{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001567 char_u **varp = (char_u **)args->os_varp;
Girish Palya0ac1eb32025-04-16 20:18:33 +02001568 char_u *p, *t;
Girish Palyacbe53192025-04-14 22:13:15 +02001569 char_u buffer[LSIZE];
1570 char_u *buf_ptr;
Girish Palya0ac1eb32025-04-16 20:18:33 +02001571 char_u char_before = NUL;
Girish Palyacbe53192025-04-14 22:13:15 +02001572 int escape;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001573
Girish Palyacbe53192025-04-14 22:13:15 +02001574 for (p = *varp; *p; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001575 {
Girish Palyacbe53192025-04-14 22:13:15 +02001576 vim_memset(buffer, 0, LSIZE);
1577 buf_ptr = buffer;
1578 escape = 0;
1579
1580 // Extract substring while handling escaped commas
1581 while (*p && (*p != ',' || escape) && buf_ptr < (buffer + LSIZE - 1))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001582 {
Girish Palyacbe53192025-04-14 22:13:15 +02001583 if (*p == '\\' && *(p + 1) == ',')
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001584 {
Girish Palyacbe53192025-04-14 22:13:15 +02001585 escape = 1; // Mark escape mode
1586 p++; // Skip '\'
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001587 }
1588 else
1589 {
Girish Palyacbe53192025-04-14 22:13:15 +02001590 escape = 0;
1591 *buf_ptr++ = *p;
1592 }
1593 p++;
1594 }
1595 *buf_ptr = NUL;
1596
Girish Palya14f6da52025-05-26 19:04:25 +02001597 if (vim_strchr((char_u *)".wbuksid]tUFo", *buffer) == NULL)
Girish Palyacbe53192025-04-14 22:13:15 +02001598 return illegal_char(args->os_errbuf, args->os_errbuflen, *buffer);
1599
Girish Palya14f6da52025-05-26 19:04:25 +02001600 if (vim_strchr((char_u *)"ksF", *buffer) == NULL && *(buffer + 1) != NUL
Girish Palya0ac1eb32025-04-16 20:18:33 +02001601 && *(buffer + 1) != '^')
1602 char_before = *buffer;
1603 else
1604 {
1605 // Test for a number after '^'
1606 if ((t = vim_strchr(buffer, '^')) != NULL)
1607 {
1608 *t++ = NUL;
1609 if (!*t)
1610 char_before = '^';
1611 else
1612 {
1613 for (; *t; t++)
1614 {
1615 if (!vim_isdigit(*t))
1616 {
1617 char_before = '^';
1618 break;
1619 }
1620 }
1621 }
1622 }
1623 }
1624 if (char_before != NUL)
Girish Palyacbe53192025-04-14 22:13:15 +02001625 {
1626 if (args->os_errbuf)
1627 {
1628 vim_snprintf((char *)args->os_errbuf, args->os_errbuflen,
Girish Palya0ac1eb32025-04-16 20:18:33 +02001629 _(e_illegal_character_after_chr), char_before);
Girish Palyacbe53192025-04-14 22:13:15 +02001630 return args->os_errbuf;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001631 }
Girish Palya0ac1eb32025-04-16 20:18:33 +02001632 return NULL;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001633 }
Girish Palyacbe53192025-04-14 22:13:15 +02001634 // Skip comma and spaces
1635 while (*p == ',' || *p == ' ')
1636 p++;
1637 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001638 return NULL;
1639}
1640
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001641 int
1642expand_set_complete(optexpand_T *args, int *numMatches, char_u ***matches)
1643{
1644 static char *(p_cpt_values[]) = {
Girish Palya14f6da52025-05-26 19:04:25 +02001645 ".", "w", "b", "u", "k", "kspell", "s", "i", "d", "]", "t", "U", "F", "o",
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001646 NULL};
1647 return expand_set_opt_string(
1648 args,
1649 p_cpt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001650 ARRAY_LENGTH(p_cpt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001651 numMatches,
1652 matches);
1653}
1654
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001655/*
1656 * The 'completeopt' option is changed.
1657 */
1658 char *
1659did_set_completeopt(optset_T *args UNUSED)
1660{
zeertzjq529b9ad2024-06-05 20:27:06 +02001661 char_u *cot = p_cot;
1662 unsigned *flags = &cot_flags;
1663
1664 if (args->os_flags & OPT_LOCAL)
1665 {
1666 cot = curbuf->b_p_cot;
1667 flags = &curbuf->b_cot_flags;
1668 }
zeertzjq46dcd842024-11-03 09:10:50 +01001669 else if (!(args->os_flags & OPT_GLOBAL))
1670 // When using :set, clear the local flags.
1671 curbuf->b_cot_flags = 0;
zeertzjq529b9ad2024-06-05 20:27:06 +02001672
1673 if (check_opt_strings(cot, p_cot_values, TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001674 return e_invalid_argument;
1675
zeertzjq529b9ad2024-06-05 20:27:06 +02001676 if (opt_strings_flags(cot, p_cot_values, flags, TRUE) != OK)
1677 return e_invalid_argument;
1678
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001679 return NULL;
1680}
1681
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001682 int
1683expand_set_completeopt(optexpand_T *args, int *numMatches, char_u ***matches)
1684{
1685 return expand_set_opt_string(
1686 args,
1687 p_cot_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001688 ARRAY_LENGTH(p_cot_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001689 numMatches,
1690 matches);
1691}
1692
glepnir6a89c942024-10-01 20:32:12 +02001693/*
glepnirf31cfa22025-03-06 21:59:13 +01001694 * The 'completefuzzycollect' option is changed.
1695 */
1696 char *
1697did_set_completefuzzycollect(optset_T *args UNUSED)
1698{
1699 if (opt_strings_flags(p_cfc, p_cfc_values, &cfc_flags, TRUE) != OK)
1700 return e_invalid_argument;
1701 return NULL;
1702}
1703
zeertzjq53d59ec2025-03-07 19:09:09 +01001704 int
1705expand_set_completefuzzycollect(
1706 optexpand_T *args,
1707 int *numMatches,
1708 char_u ***matches)
1709{
1710 return expand_set_opt_string(
1711 args,
1712 p_cfc_values,
1713 ARRAY_LENGTH(p_cfc_values) - 1,
1714 numMatches,
1715 matches);
1716}
1717
glepnirf31cfa22025-03-06 21:59:13 +01001718/*
glepnir6a89c942024-10-01 20:32:12 +02001719 * The 'completeitemalign' option is changed.
1720 */
1721 char *
1722did_set_completeitemalign(optset_T *args UNUSED)
1723{
1724 char_u *p = p_cia;
1725 unsigned new_cia_flags = 0;
1726 int seen[3] = { FALSE, FALSE, FALSE };
1727 int count = 0;
1728 char_u buf[10];
1729
1730 while (*p)
1731 {
1732 copy_option_part(&p, buf, sizeof(buf), ",");
1733 if (count >= 3)
1734 return e_invalid_argument;
1735
1736 if (STRCMP(buf, "abbr") == 0)
1737 {
1738 if (seen[CPT_ABBR])
1739 return e_invalid_argument;
1740 new_cia_flags = new_cia_flags * 10 + CPT_ABBR;
1741 seen[CPT_ABBR] = TRUE;
1742 count++;
1743 }
1744 else if (STRCMP(buf, "kind") == 0)
1745 {
1746 if (seen[CPT_KIND])
1747 return e_invalid_argument;
1748 new_cia_flags = new_cia_flags * 10 + CPT_KIND;
1749 seen[CPT_KIND] = TRUE;
1750 count++;
1751 }
1752 else if (STRCMP(buf, "menu") == 0)
1753 {
1754 if (seen[CPT_MENU])
1755 return e_invalid_argument;
1756 new_cia_flags = new_cia_flags * 10 + CPT_MENU;
1757 seen[CPT_MENU] = TRUE;
1758 count++;
1759 }
1760 else
1761 return e_invalid_argument;
1762 }
1763 if (new_cia_flags == 0 || count != 3)
1764 return e_invalid_argument;
1765
1766 cia_flags = new_cia_flags;
1767 return NULL;
1768}
1769
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001770#if (defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1771/*
1772 * The 'completepopup' option is changed.
1773 */
1774 char *
1775did_set_completepopup(optset_T *args UNUSED)
1776{
1777 if (parse_completepopup(NULL) == FAIL)
1778 return e_invalid_argument;
1779
1780 popup_close_info();
1781 return NULL;
1782}
1783#endif
1784
1785#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
1786/*
1787 * The 'completeslash' option is changed.
1788 */
1789 char *
1790did_set_completeslash(optset_T *args UNUSED)
1791{
1792 if (check_opt_strings(p_csl, p_csl_values, FALSE) != OK
1793 || check_opt_strings(curbuf->b_p_csl, p_csl_values, FALSE) != OK)
1794 return e_invalid_argument;
1795
1796 return NULL;
1797}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001798
1799 int
1800expand_set_completeslash(optexpand_T *args, int *numMatches, char_u ***matches)
1801{
1802 return expand_set_opt_string(
1803 args,
1804 p_csl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001805 ARRAY_LENGTH(p_csl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001806 numMatches,
1807 matches);
1808}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001809#endif
1810
1811#if defined(FEAT_CONCEAL) || defined(PROTO)
1812/*
1813 * The 'concealcursor' option is changed.
1814 */
1815 char *
1816did_set_concealcursor(optset_T *args)
1817{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001818 char_u **varp = (char_u **)args->os_varp;
1819
Christian Brabandtb39b2402023-11-29 11:34:05 +01001820 return did_set_option_listflag(*varp, (char_u *)COCU_ALL, args->os_errbuf,
1821 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001822}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001823
1824 int
1825expand_set_concealcursor(optexpand_T *args, int *numMatches, char_u ***matches)
1826{
1827 return expand_set_opt_listflag(args, (char_u*)COCU_ALL, numMatches, matches);
1828}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001829#endif
1830
1831/*
1832 * The 'cpoptions' option is changed.
1833 */
1834 char *
1835did_set_cpoptions(optset_T *args)
1836{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001837 char_u **varp = (char_u **)args->os_varp;
1838
Christian Brabandtb39b2402023-11-29 11:34:05 +01001839 return did_set_option_listflag(*varp, (char_u *)CPO_ALL, args->os_errbuf,
1840 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001841}
1842
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001843 int
1844expand_set_cpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
1845{
1846 return expand_set_opt_listflag(args, (char_u*)CPO_ALL, numMatches, matches);
1847}
1848
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001849#if defined(FEAT_CRYPT) || defined(PROTO)
1850/*
1851 * The 'cryptkey' option is changed.
1852 */
1853 char *
1854did_set_cryptkey(optset_T *args)
1855{
1856 // Make sure the ":set" command doesn't show the new value in the
1857 // history.
1858 remove_key_from_history();
1859
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02001860 if (args->os_op != OP_NONE)
1861 // Don't allow set+=/-=/^= as they can allow for substring guessing
1862 return e_invalid_argument;
1863
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001864 if (STRCMP(curbuf->b_p_key, args->os_oldval.string) != 0)
1865 {
1866 // Need to update the swapfile.
1867 ml_set_crypt_key(curbuf, args->os_oldval.string,
1868 *curbuf->b_p_cm == NUL ? p_cm : curbuf->b_p_cm);
1869 changed_internal();
1870 }
Christian Brabandt19e6c4f2023-06-27 18:57:10 +01001871# ifdef FEAT_SODIUM
1872 if (crypt_method_is_sodium(crypt_get_method_nr(curbuf)))
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +02001873 crypt_sodium_lock_key(args->os_newval.string);
Christian Brabandt19e6c4f2023-06-27 18:57:10 +01001874# endif
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001875
1876 return NULL;
1877}
1878
1879/*
1880 * The 'cryptmethod' option is changed.
1881 */
1882 char *
1883did_set_cryptmethod(optset_T *args)
1884{
1885 char_u *p;
1886 char_u *s;
1887
1888 if (args->os_flags & OPT_LOCAL)
1889 p = curbuf->b_p_cm;
1890 else
1891 p = p_cm;
1892 if (check_opt_strings(p, p_cm_values, TRUE) != OK)
1893 return e_invalid_argument;
1894 else if (crypt_self_test() == FAIL)
1895 return e_invalid_argument;
1896
1897 // When setting the global value to empty, make it "zip".
1898 if (*p_cm == NUL)
1899 {
1900 free_string_option(p_cm);
1901 p_cm = vim_strsave((char_u *)"zip");
1902 }
1903 // When using ":set cm=name" the local value is going to be empty.
1904 // Do that here, otherwise the crypt functions will still use the
1905 // local value.
1906 if ((args->os_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
1907 {
1908 free_string_option(curbuf->b_p_cm);
1909 curbuf->b_p_cm = empty_option;
1910 }
1911
1912 // Need to update the swapfile when the effective method changed.
1913 // Set "s" to the effective old value, "p" to the effective new
1914 // method and compare.
1915 if ((args->os_flags & OPT_LOCAL) && *args->os_oldval.string == NUL)
1916 s = p_cm; // was previously using the global value
1917 else
1918 s = args->os_oldval.string;
1919 if (*curbuf->b_p_cm == NUL)
1920 p = p_cm; // is now using the global value
1921 else
1922 p = curbuf->b_p_cm;
1923 if (STRCMP(s, p) != 0)
1924 ml_set_crypt_key(curbuf, curbuf->b_p_key, s);
1925
1926 // If the global value changes need to update the swapfile for all
1927 // buffers using that value.
1928 if ((args->os_flags & OPT_GLOBAL)
1929 && STRCMP(p_cm, args->os_oldval.string) != 0)
1930 {
1931 buf_T *buf;
1932
1933 FOR_ALL_BUFFERS(buf)
1934 if (buf != curbuf && *buf->b_p_cm == NUL)
1935 ml_set_crypt_key(buf, buf->b_p_key, args->os_oldval.string);
1936 }
1937 return NULL;
1938}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001939
1940 int
1941expand_set_cryptmethod(optexpand_T *args, int *numMatches, char_u ***matches)
1942{
1943 return expand_set_opt_string(
1944 args,
1945 p_cm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02001946 ARRAY_LENGTH(p_cm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001947 numMatches,
1948 matches);
1949}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00001950#endif
1951
1952#if (defined(FEAT_CSCOPE) && defined(FEAT_QUICKFIX)) || defined(PROTO)
1953/*
1954 * The 'cscopequickfix' option is changed.
1955 */
1956 char *
1957did_set_cscopequickfix(optset_T *args UNUSED)
1958{
1959 char_u *p;
1960
1961 if (p_csqf == NULL)
1962 return NULL;
1963
1964 p = p_csqf;
1965 while (*p != NUL)
1966 {
1967 if (vim_strchr((char_u *)CSQF_CMDS, *p) == NULL
1968 || p[1] == NUL
1969 || vim_strchr((char_u *)CSQF_FLAGS, p[1]) == NULL
1970 || (p[2] != NUL && p[2] != ','))
1971 return e_invalid_argument;
1972 else if (p[2] == NUL)
1973 break;
1974 else
1975 p += 3;
1976 }
1977
1978 return NULL;
1979}
1980#endif
1981
1982#if defined(FEAT_SYN_HL) || defined(PROTO)
1983/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001984 * The 'cursorlineopt' option is changed.
1985 */
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00001986 char *
1987did_set_cursorlineopt(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001988{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001989 char_u **varp = (char_u **)args->os_varp;
1990
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001991 // This could be changed to use opt_strings_flags() instead.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00001992 if (**varp == NUL || fill_culopt_flags(*varp, curwin) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00001993 return e_invalid_argument;
1994
1995 return NULL;
1996}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02001997
1998 int
1999expand_set_cursorlineopt(optexpand_T *args, int *numMatches, char_u ***matches)
2000{
2001 return expand_set_opt_string(
2002 args,
2003 p_culopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002004 ARRAY_LENGTH(p_culopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002005 numMatches,
2006 matches);
2007}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002008#endif
2009
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002010/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002011 * The 'debug' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002012 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002013 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002014did_set_debug(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002015{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002016 return did_set_opt_strings(p_debug, p_debug_values, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002017}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002018
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002019 int
2020expand_set_debug(optexpand_T *args, int *numMatches, char_u ***matches)
2021{
2022 return expand_set_opt_string(
2023 args,
2024 p_debug_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002025 ARRAY_LENGTH(p_debug_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002026 numMatches,
2027 matches);
2028}
2029
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002030#if defined(FEAT_DIFF) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002031/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002032 * The 'diffopt' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002033 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002034 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002035did_set_diffopt(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002036{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002037 if (diffopt_changed() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002038 return e_invalid_argument;
2039
2040 return NULL;
2041}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002042
2043 int
2044expand_set_diffopt(optexpand_T *args, int *numMatches, char_u ***matches)
2045{
2046 expand_T *xp = args->oe_xp;
2047
2048 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2049 {
2050 // Within "algorithm:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002051 int algo_len = (int)STRLEN("algorithm:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002052 if (xp->xp_pattern - args->oe_set_arg >= algo_len &&
2053 STRNCMP(xp->xp_pattern - algo_len, "algorithm:", algo_len) == 0)
2054 {
2055 return expand_set_opt_string(
2056 args,
2057 p_dip_algorithm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002058 ARRAY_LENGTH(p_dip_algorithm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002059 numMatches,
2060 matches);
2061 }
Yee Cheng Chin9943d472025-03-26 19:41:02 +01002062 // Within "inline:", we have a subgroup of possible options.
2063 int inline_len = (int)STRLEN("inline:");
2064 if (xp->xp_pattern - args->oe_set_arg >= inline_len &&
2065 STRNCMP(xp->xp_pattern - inline_len, "inline:", inline_len) == 0)
2066 {
2067 return expand_set_opt_string(
2068 args,
2069 p_dip_inline_values,
2070 ARRAY_LENGTH(p_dip_inline_values) - 1,
2071 numMatches,
2072 matches);
2073 }
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002074 return FAIL;
2075 }
2076
2077 return expand_set_opt_string(
2078 args,
2079 p_dip_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002080 ARRAY_LENGTH(p_dip_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002081 numMatches,
2082 matches);
2083}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002084#endif
2085
2086/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002087 * The 'display' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002088 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002089 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002090did_set_display(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002091{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002092 if (opt_strings_flags(p_dy, p_dy_values, &dy_flags, TRUE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002093 return e_invalid_argument;
2094
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002095 (void)init_chartab();
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002096 return NULL;
2097}
2098
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002099 int
2100expand_set_display(optexpand_T *args, int *numMatches, char_u ***matches)
2101{
2102 return expand_set_opt_string(
2103 args,
2104 p_dy_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002105 ARRAY_LENGTH(p_dy_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002106 numMatches,
2107 matches);
2108}
2109
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002110/*
2111 * The 'eadirection' option is changed.
2112 */
2113 char *
2114did_set_eadirection(optset_T *args UNUSED)
2115{
2116 return did_set_opt_strings(p_ead, p_ead_values, FALSE);
2117}
2118
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002119 int
2120expand_set_eadirection(optexpand_T *args, int *numMatches, char_u ***matches)
2121{
2122 return expand_set_opt_string(
2123 args,
2124 p_ead_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002125 ARRAY_LENGTH(p_ead_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002126 numMatches,
2127 matches);
2128}
2129
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00002130/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002131 * One of the 'encoding', 'fileencoding', 'termencoding' or 'makeencoding'
2132 * options is changed.
2133 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002134 char *
2135did_set_encoding(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002136{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002137 char_u **varp = (char_u **)args->os_varp;
2138 char_u **gvarp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002139 char *errmsg = NULL;
2140 char_u *p;
2141
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002142 // Get the global option to compare with, otherwise we would have to check
2143 // two values for all local options.
2144 gvarp = (char_u **)get_option_varp_scope(args->os_idx, OPT_GLOBAL);
2145
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002146 if (gvarp == &p_fenc)
2147 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002148 if (!curbuf->b_p_ma && args->os_flags != OPT_GLOBAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002149 errmsg = e_cannot_make_changes_modifiable_is_off;
2150 else if (vim_strchr(*varp, ',') != NULL)
2151 // No comma allowed in 'fileencoding'; catches confusing it
2152 // with 'fileencodings'.
2153 errmsg = e_invalid_argument;
2154 else
2155 {
2156 // May show a "+" in the title now.
2157 redraw_titles();
2158 // Add 'fileencoding' to the swap file.
2159 ml_setflags(curbuf);
2160 }
2161 }
2162 if (errmsg == NULL)
2163 {
2164 // canonize the value, so that STRCMP() can be used on it
2165 p = enc_canonize(*varp);
2166 if (p != NULL)
2167 {
2168 vim_free(*varp);
2169 *varp = p;
2170 }
2171 if (varp == &p_enc)
2172 {
2173 errmsg = mb_init();
2174 redraw_titles();
2175 }
2176 }
2177
2178#if defined(FEAT_GUI_GTK)
2179 if (errmsg == NULL && varp == &p_tenc && gui.in_use)
2180 {
2181 // GTK uses only a single encoding, and that is UTF-8.
2182 if (STRCMP(p_tenc, "utf-8") != 0)
2183 errmsg = e_cannot_be_changed_in_gtk_GUI;
2184 }
2185#endif
2186
2187 if (errmsg == NULL)
2188 {
2189#ifdef FEAT_KEYMAP
2190 // When 'keymap' is used and 'encoding' changes, reload the keymap
2191 // (with another encoding).
2192 if (varp == &p_enc && *curbuf->b_p_keymap != NUL)
2193 (void)keymap_init();
2194#endif
2195
2196 // When 'termencoding' is not empty and 'encoding' changes or when
2197 // 'termencoding' changes, need to setup for keyboard input and
2198 // display output conversion.
2199 if (((varp == &p_enc && *p_tenc != NUL) || varp == &p_tenc))
2200 {
2201 if (convert_setup(&input_conv, p_tenc, p_enc) == FAIL
2202 || convert_setup(&output_conv, p_enc, p_tenc) == FAIL)
2203 {
2204 semsg(_(e_cannot_convert_between_str_and_str),
2205 p_tenc, p_enc);
2206 errmsg = e_invalid_argument;
2207 }
2208 }
2209
2210#if defined(MSWIN)
K.Takatace3189d2023-02-15 19:13:43 +00002211 // $HOME, $VIM and $VIMRUNTIME may have characters in active code page.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002212 if (varp == &p_enc)
K.Takatace3189d2023-02-15 19:13:43 +00002213 {
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002214 init_homedir();
K.Takatace3189d2023-02-15 19:13:43 +00002215 init_vimdir();
2216 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002217#endif
2218 }
2219
2220 return errmsg;
2221}
2222
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002223 int
2224expand_set_encoding(optexpand_T *args, int *numMatches, char_u ***matches)
2225{
2226 return expand_set_opt_generic(
2227 args,
2228 get_encoding_name,
2229 numMatches,
2230 matches);
2231}
2232
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002233/*
Luuk van Baalb7147f82025-02-08 18:52:39 +01002234 * The 'eventignore(win)' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002235 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002236 char *
Luuk van Baalb7147f82025-02-08 18:52:39 +01002237did_set_eventignore(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002238{
Luuk van Baalb7147f82025-02-08 18:52:39 +01002239 char_u **varp = (char_u **)args->os_varp;
2240
2241 if (check_ei(*varp) == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002242 return e_invalid_argument;
2243 return NULL;
2244}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002245
Luuk van Baalb7147f82025-02-08 18:52:39 +01002246static int expand_eiw = FALSE;
2247
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002248 static char_u *
2249get_eventignore_name(expand_T *xp, int idx)
2250{
Luuk van Baal8cc6d8b2025-05-31 12:10:31 +02002251 int subtract = *xp->xp_pattern == '-';
Luuk van Baalb7147f82025-02-08 18:52:39 +01002252 // 'eventignore(win)' allows special keyword "all" in addition to
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002253 // all event names.
Luuk van Baal8cc6d8b2025-05-31 12:10:31 +02002254 if (!subtract && idx == 0)
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002255 return (char_u *)"all";
Luuk van Baal8cc6d8b2025-05-31 12:10:31 +02002256
2257 char_u *name = get_event_name_no_group(xp, idx - 1 + subtract, expand_eiw);
2258 if (name == NULL)
2259 return NULL;
2260
2261 sprintf((char *)IObuff, "%s%s", subtract ? "-" : "", name);
2262 return IObuff;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002263}
2264
2265 int
2266expand_set_eventignore(optexpand_T *args, int *numMatches, char_u ***matches)
2267{
Luuk van Baalb7147f82025-02-08 18:52:39 +01002268 expand_eiw = args->oe_varp != (char_u *)&p_ei;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002269 return expand_set_opt_generic(
2270 args,
2271 get_eventignore_name,
2272 numMatches,
2273 matches);
2274}
2275
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002276/*
2277 * The 'fileformat' option is changed.
2278 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002279 char *
2280did_set_fileformat(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002281{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002282 char_u **varp = (char_u **)args->os_varp;
2283
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002284 if (!curbuf->b_p_ma && !(args->os_flags & OPT_GLOBAL))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002285 return e_cannot_make_changes_modifiable_is_off;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002286 else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002287 return e_invalid_argument;
2288
2289 // may also change 'textmode'
2290 if (get_fileformat(curbuf) == EOL_DOS)
2291 curbuf->b_p_tx = TRUE;
2292 else
2293 curbuf->b_p_tx = FALSE;
2294 redraw_titles();
2295 // update flag in swap file
2296 ml_setflags(curbuf);
2297 // Redraw needed when switching to/from "mac": a CR in the text
2298 // will be displayed differently.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002299 if (get_fileformat(curbuf) == EOL_MAC || *args->os_oldval.string == 'm')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002300 redraw_curbuf_later(UPD_NOT_VALID);
2301
2302 return NULL;
2303}
2304
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002305 int
2306expand_set_fileformat(optexpand_T *args, int *numMatches, char_u ***matches)
2307{
2308 return expand_set_opt_string(
2309 args,
2310 p_ff_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002311 ARRAY_LENGTH(p_ff_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002312 numMatches,
2313 matches);
2314}
2315
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002316/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002317 * Function given to ExpandGeneric() to obtain the possible arguments of the
2318 * fileformat options.
2319 */
2320 char_u *
2321get_fileformat_name(expand_T *xp UNUSED, int idx)
2322{
2323 if (idx >= (int)ARRAY_LENGTH(p_ff_values))
2324 return NULL;
2325
2326 return (char_u*)p_ff_values[idx];
2327}
2328
2329/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002330 * The 'fileformats' option is changed.
2331 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002332 char *
2333did_set_fileformats(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002334{
2335 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
2336 return e_invalid_argument;
2337
2338 // also change 'textauto'
2339 if (*p_ffs == NUL)
2340 p_ta = FALSE;
2341 else
2342 p_ta = TRUE;
2343
2344 return NULL;
2345}
2346
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002347/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002348 * The 'filetype' or the 'syntax' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002349 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002350 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002351did_set_filetype_or_syntax(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002352{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002353 char_u **varp = (char_u **)args->os_varp;
2354
2355 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002356 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002357
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002358 args->os_value_changed = STRCMP(args->os_oldval.string, *varp) != 0;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002359
2360 // Since we check the value, there is no need to set P_INSECURE,
2361 // even when the value comes from a modeline.
2362 args->os_value_checked = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002363
2364 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002365}
2366
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002367#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002368/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002369 * The 'foldclose' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002370 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002371 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002372did_set_foldclose(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002373{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002374 return did_set_opt_strings(p_fcl, p_fcl_values, TRUE);
2375}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002376
2377 int
2378expand_set_foldclose(optexpand_T *args, int *numMatches, char_u ***matches)
2379{
2380 return expand_set_opt_string(
2381 args,
2382 p_fcl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002383 ARRAY_LENGTH(p_fcl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002384 numMatches,
2385 matches);
2386}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002387#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002388
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002389#if (defined(FEAT_EVAL) && defined(FEAT_FOLDING)) || defined(PROTO)
2390/*
2391 * The 'foldexpr' option is changed.
2392 */
2393 char *
2394did_set_foldexpr(optset_T *args)
2395{
2396 (void)did_set_optexpr(args);
2397 if (foldmethodIsExpr(curwin))
2398 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002399 return NULL;
2400}
2401#endif
2402
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002403#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002404/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002405 * The 'foldignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002406 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002407 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002408did_set_foldignore(optset_T *args UNUSED)
2409{
2410 if (foldmethodIsIndent(curwin))
2411 foldUpdateAll(curwin);
2412 return NULL;
2413}
2414
2415/*
2416 * The 'foldmarker' option is changed.
2417 */
2418 char *
2419did_set_foldmarker(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002420{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002421 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002422 char_u *p;
2423
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002424 p = vim_strchr(*varp, ',');
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002425 if (p == NULL)
2426 return e_comma_required;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002427 else if (p == *varp || p[1] == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002428 return e_invalid_argument;
2429 else if (foldmethodIsMarker(curwin))
2430 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002431
2432 return NULL;
2433}
2434
2435/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002436 * The 'foldmethod' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002437 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002438 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002439did_set_foldmethod(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002440{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002441 char_u **varp = (char_u **)args->os_varp;
2442
Milly142cad12024-10-22 22:11:51 +02002443 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK || **varp == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002444 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002445
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002446 foldUpdateAll(curwin);
2447 if (foldmethodIsDiff(curwin))
2448 newFoldLevel();
2449 return NULL;
2450}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002451
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002452 int
2453expand_set_foldmethod(optexpand_T *args, int *numMatches, char_u ***matches)
2454{
2455 return expand_set_opt_string(
2456 args,
2457 p_fdm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002458 ARRAY_LENGTH(p_fdm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002459 numMatches,
2460 matches);
2461}
2462
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002463/*
2464 * The 'foldopen' option is changed.
2465 */
2466 char *
2467did_set_foldopen(optset_T *args UNUSED)
2468{
2469 return did_set_opt_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
2470}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002471
2472 int
2473expand_set_foldopen(optexpand_T *args, int *numMatches, char_u ***matches)
2474{
2475 return expand_set_opt_string(
2476 args,
2477 p_fdo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002478 ARRAY_LENGTH(p_fdo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002479 numMatches,
2480 matches);
2481}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002482#endif
2483
2484/*
2485 * The 'formatoptions' option is changed.
2486 */
2487 char *
2488did_set_formatoptions(optset_T *args)
2489{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002490 char_u **varp = (char_u **)args->os_varp;
2491
Christian Brabandtb39b2402023-11-29 11:34:05 +01002492 return did_set_option_listflag(*varp, (char_u *)FO_ALL, args->os_errbuf,
2493 args->os_errbuflen);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002494}
2495
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002496 int
2497expand_set_formatoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2498{
2499 return expand_set_opt_listflag(args, (char_u*)FO_ALL, numMatches, matches);
2500}
2501
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002502#if defined(CURSOR_SHAPE) || defined(PROTO)
2503/*
2504 * The 'guicursor' option is changed.
2505 */
2506 char *
2507did_set_guicursor(optset_T *args UNUSED)
2508{
2509 return parse_shape_opt(SHAPE_CURSOR);
2510}
2511#endif
2512
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002513#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002514/*
2515 * The 'guifont' option is changed.
2516 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002517 char *
2518did_set_guifont(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002519{
2520 char_u *p;
2521 char *errmsg = NULL;
2522
2523 if (gui.in_use)
2524 {
2525 p = p_guifont;
2526# if defined(FEAT_GUI_GTK)
2527 // Put up a font dialog and let the user select a new value.
2528 // If this is cancelled go back to the old value but don't
2529 // give an error message.
2530 if (STRCMP(p, "*") == 0)
2531 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002532 p = gui_mch_font_dialog(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002533 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002534 p_guifont = (p != NULL) ? p : vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002535 }
2536# endif
2537 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
2538 {
2539# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
2540 if (STRCMP(p_guifont, "*") == 0)
2541 {
2542 // Dialog was cancelled: Keep the old value without giving
2543 // an error message.
2544 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002545 p_guifont = vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002546 }
2547 else
2548# endif
2549 errmsg = e_invalid_fonts;
2550 }
2551 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002552
2553 return errmsg;
2554}
2555
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002556/*
2557 * Expand the 'guifont' option. Only when GUI is being used. Each platform has
2558 * specific behaviors.
2559 */
2560 int
2561expand_set_guifont(optexpand_T *args, int *numMatches, char_u ***matches)
2562{
2563 if (!gui.in_use)
2564 return FAIL;
2565
2566# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
2567 char_u **varp = (char_u **)args->oe_varp;
2568 int wide = (varp == &p_guifontwide);
2569
2570 return expand_set_opt_callback(
2571 args, gui_mch_expand_font, &wide, numMatches, matches);
2572# else
2573 return FAIL;
2574# endif
2575}
2576
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002577# if defined(FEAT_XFONTSET) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002578/*
2579 * The 'guifontset' option is changed.
2580 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002581 char *
2582did_set_guifontset(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002583{
2584 char *errmsg = NULL;
2585
2586 if (STRCMP(p_guifontset, "*") == 0)
2587 errmsg = e_cant_select_fontset;
2588 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
2589 errmsg = e_invalid_fontset;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002590
2591 return errmsg;
2592}
2593# endif
2594
2595/*
2596 * The 'guifontwide' option is changed.
2597 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002598 char *
2599did_set_guifontwide(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002600{
2601 char *errmsg = NULL;
2602
2603 if (STRCMP(p_guifontwide, "*") == 0)
2604 errmsg = e_cant_select_wide_font;
2605 else if (gui_get_wide_font() == FAIL)
2606 errmsg = e_invalid_wide_font;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002607
2608 return errmsg;
2609}
2610#endif
2611
Erik S. V. Jansson8b1e7492024-02-24 14:26:52 +01002612#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002613/*
2614 * The 'guiligatures' option is changed.
2615 */
2616 char *
2617did_set_guiligatures(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002618{
2619 gui_set_ligatures();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002620 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002621}
2622#endif
2623
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002624#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002625/*
2626 * The 'guioptions' option is changed.
2627 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002628 char *
2629did_set_guioptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002630{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002631 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002632 char *errmsg;
2633
Christian Brabandtb39b2402023-11-29 11:34:05 +01002634 errmsg = did_set_option_listflag(*varp, (char_u *)GO_ALL, args->os_errbuf,
2635 args->os_errbuflen);
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002636 if (errmsg != NULL)
2637 return errmsg;
2638
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002639 gui_init_which_components(args->os_oldval.string);
2640 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002641}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002642
2643 int
2644expand_set_guioptions(optexpand_T *args, int *numMatches, char_u ***matches)
2645{
2646 return expand_set_opt_listflag(args, (char_u*)GO_ALL, numMatches, matches);
2647}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002648#endif
2649
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002650#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002651/*
2652 * The 'guitablabel' option is changed.
2653 */
2654 char *
2655did_set_guitablabel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002656{
2657 redraw_tabline = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002658 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002659}
2660#endif
2661
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002662/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002663 * The 'helpfile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002664 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002665 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002666did_set_helpfile(optset_T *args UNUSED)
2667{
2668 // May compute new values for $VIM and $VIMRUNTIME
2669 if (didset_vim)
2670 vim_unsetenv_ext((char_u *)"VIM");
2671 if (didset_vimruntime)
2672 vim_unsetenv_ext((char_u *)"VIMRUNTIME");
2673 return NULL;
2674}
2675
2676#if defined(FEAT_MULTI_LANG) || defined(PROTO)
2677/*
2678 * The 'helplang' option is changed.
2679 */
2680 char *
2681did_set_helplang(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002682{
2683 char *errmsg = NULL;
2684
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002685 // Check for "", "ab", "ab,cd", etc.
2686 for (char_u *s = p_hlg; *s != NUL; s += 3)
2687 {
2688 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
2689 {
2690 errmsg = e_invalid_argument;
2691 break;
2692 }
2693 if (s[2] == NUL)
2694 break;
2695 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002696
2697 return errmsg;
2698}
2699#endif
2700
2701/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002702 * The 'highlight' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002703 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002704 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002705did_set_highlight(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002706{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002707 if (highlight_changed() == FAIL)
2708 return e_invalid_argument; // invalid flags
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002709
2710 return NULL;
2711}
2712
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002713/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002714 * Expand 'highlight' option.
2715 */
2716 int
2717expand_set_highlight(optexpand_T *args, int *numMatches, char_u ***matches)
2718{
2719 char_u *p;
2720 expand_T *xp = args->oe_xp;
2721 static char_u hl_flags[HLF_COUNT] = HL_FLAGS;
Christian Brabandt3f168ec2023-10-02 23:21:11 +02002722 size_t i;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002723 int count = 0;
2724
2725 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2726 {
2727 // Right after a ':', meaning we just return all highlight names.
2728 return expand_set_opt_generic(
2729 args,
2730 get_highlight_name,
2731 numMatches,
2732 matches);
2733 }
2734
2735 if (*xp->xp_pattern == NUL)
2736 {
2737 // At beginning of a comma-separated list. Return the specific list of
2738 // supported occasions.
2739 *matches = ALLOC_MULT(char_u *, HLF_COUNT + 1);
2740 if (*matches == NULL)
2741 return FAIL;
2742
2743 // We still want to return the full option if it's requested.
2744 if (args->oe_include_orig_val)
2745 {
2746 p = vim_strsave(args->oe_opt_value);
2747 if (p == NULL)
2748 {
2749 VIM_CLEAR(*matches);
2750 return FAIL;
2751 }
2752 (*matches)[count++] = p;
2753 }
2754
2755 for (i = 0; i < HLF_COUNT; i++)
2756 {
2757 p = vim_strnsave(&hl_flags[i], 1);
2758 if (p == NULL)
2759 {
2760 if (count == 0)
2761 {
2762 VIM_CLEAR(*matches);
2763 return FAIL;
2764 }
2765 else
2766 break;
2767 }
2768 (*matches)[count++] = p;
2769 }
2770
2771 if (count == 0)
2772 {
2773 VIM_CLEAR(*matches);
2774 return FAIL;
2775 }
2776 *numMatches = count;
2777 return OK;
2778 }
2779
2780 // We are after the initial character (which indicates the occasion). We
2781 // already made sure we are not matching after a ':' above, so now we want
2782 // to match against display mode modifiers.
2783 // Since the xp_pattern starts from the beginning, we need to include it in
2784 // the returned match.
2785
2786 // Note: Keep this in sync with highlight_changed()
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002787 static char_u p_hl_mode_values[] =
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002788 {':', 'b', 'i', '-', 'n', 'r', 's', 'u', 'c', '2', 'd', '=', 't'};
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002789 size_t num_hl_modes = ARRAY_LENGTH(p_hl_mode_values);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002790
2791 *matches = ALLOC_MULT(char_u *, num_hl_modes);
2792 if (*matches == NULL)
2793 return FAIL;
2794
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002795 int pattern_len = xp->xp_pattern_len;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002796
2797 for (i = 0; i < num_hl_modes; i++)
2798 {
2799 // Don't allow duplicates as these are unique flags
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002800 char_u *dup = vim_strchr(xp->xp_pattern + 1, p_hl_mode_values[i]);
2801 if (dup != NULL && (int)(dup - xp->xp_pattern) < pattern_len)
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002802 continue;
2803
2804 // ':' only works by itself, not with other flags.
2805 if (pattern_len > 1 && p_hl_mode_values[i] == ':')
2806 continue;
2807
2808 p = vim_strnsave(xp->xp_pattern, pattern_len + 1);
2809 if (p == NULL)
2810 {
2811 if (i == 0)
2812 {
2813 VIM_CLEAR(*matches);
2814 return FAIL;
2815 }
2816 else
2817 break;
2818 }
2819 p[pattern_len] = p_hl_mode_values[i];
2820 p[pattern_len + 1] = NUL;
2821 (*matches)[count++] = p;
2822 }
2823 if (count == 0)
2824 {
2825 VIM_CLEAR(*matches);
2826 return FAIL;
2827 }
2828 *numMatches = count;
2829
2830 return OK;
2831}
2832
2833/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002834 * The 'titlestring' or the 'iconstring' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002835 */
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002836 static char *
2837parse_titleiconstring(optset_T *args UNUSED, int flagval UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002838{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002839#ifdef FEAT_STL_OPT
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002840 char_u **varp = (char_u **)args->os_varp;
2841
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002842 // NULL => statusline syntax
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002843 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002844 stl_syntax |= flagval;
2845 else
2846 stl_syntax &= ~flagval;
2847#endif
2848 did_set_title();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002849
2850 return NULL;
2851}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002852
2853/*
2854 * The 'iconstring' option is changed.
2855 */
2856 char *
2857did_set_iconstring(optset_T *args)
2858{
2859 int flagval = 0;
2860
2861#ifdef FEAT_STL_OPT
2862 flagval = STL_IN_ICON;
2863#endif
2864
2865 return parse_titleiconstring(args, flagval);
2866}
2867
2868#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
2869/*
2870 * The 'imactivatekey' option is changed.
2871 */
2872 char *
2873did_set_imactivatekey(optset_T *args UNUSED)
2874{
2875 if (!im_xim_isvalid_imactivate())
2876 return e_invalid_argument;
2877 return NULL;
2878}
2879#endif
2880
2881/*
glepnirbcd59952025-04-24 21:48:35 +02002882 * The 'isexpand' option is changed.
2883 */
2884 char *
2885did_set_isexpand(optset_T *args)
2886{
2887 char_u *ise = p_ise;
2888 char_u *p;
2889 int last_was_comma = FALSE;
2890
2891 if (args->os_flags & OPT_LOCAL)
2892 ise = curbuf->b_p_ise;
2893
2894 for (p = ise; *p != NUL;)
2895 {
2896 if (*p == '\\' && p[1] == ',')
2897 {
2898 p += 2;
2899 last_was_comma = FALSE;
2900 continue;
2901 }
2902
2903 if (*p == ',')
2904 {
2905 if (last_was_comma)
2906 return e_invalid_argument;
2907 last_was_comma = TRUE;
2908 p++;
2909 continue;
2910 }
2911
2912 last_was_comma = FALSE;
2913 MB_PTR_ADV(p);
2914 }
2915
2916 if (last_was_comma)
2917 return e_invalid_argument;
2918
2919 return NULL;
2920}
2921
2922
2923/*
Milly5e7a6a42024-10-22 22:27:19 +02002924 * The 'iskeyword' option is changed.
2925 */
2926 char *
2927did_set_iskeyword(optset_T *args)
2928{
2929 char_u **varp = (char_u **)args->os_varp;
2930
2931 if (varp == &p_isk) // only check for global-value
2932 {
2933 if (check_isopt(*varp) == FAIL)
2934 return e_invalid_argument;
2935 }
2936 else // fallthrough for local-value
2937 return did_set_isopt(args);
2938
2939 return NULL;
2940}
2941
2942/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002943 * The 'isident' or the 'iskeyword' or the 'isprint' or the 'isfname' option is
2944 * changed.
2945 */
2946 char *
2947did_set_isopt(optset_T *args)
2948{
Milly5e7a6a42024-10-22 22:27:19 +02002949 // 'isident', 'iskeyword', 'isprint' or 'isfname' option: refill g_chartab[]
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002950 // If the new option is invalid, use old value.
2951 // 'lisp' option: refill g_chartab[] for '-' char.
2952 if (init_chartab() == FAIL)
2953 {
2954 args->os_restore_chartab = TRUE;// need to restore the chartab.
2955 return e_invalid_argument; // error in value
2956 }
2957
2958 return NULL;
2959}
2960
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002961/*
2962 * The 'jumpoptions' option is changed.
2963 */
2964 char *
Yegappan Lakshmanan1926ae42023-09-21 16:36:28 +02002965did_set_jumpoptions(optset_T *args UNUSED)
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002966{
2967 if (opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE) != OK)
2968 return e_invalid_argument;
2969
2970 return NULL;
2971}
2972
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002973 int
2974expand_set_jumpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2975{
2976 return expand_set_opt_string(
2977 args,
2978 p_jop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002979 ARRAY_LENGTH(p_jop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002980 numMatches,
2981 matches);
2982}
2983
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002984#if defined(FEAT_KEYMAP) || defined(PROTO)
2985/*
2986 * The 'keymap' option is changed.
2987 */
2988 char *
2989did_set_keymap(optset_T *args)
2990{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002991 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002992 char *errmsg = NULL;
2993
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002994 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002995 errmsg = e_invalid_argument;
2996 else
2997 {
2998 int secure_save = secure;
2999
3000 // Reset the secure flag, since the value of 'keymap' has
3001 // been checked to be safe.
3002 secure = 0;
3003
3004 // load or unload key mapping tables
3005 errmsg = keymap_init();
3006
3007 secure = secure_save;
3008
3009 // Since we check the value, there is no need to set P_INSECURE,
3010 // even when the value comes from a modeline.
3011 args->os_value_checked = TRUE;
3012 }
3013
3014 if (errmsg == NULL)
3015 {
3016 if (*curbuf->b_p_keymap != NUL)
3017 {
3018 // Installed a new keymap, switch on using it.
3019 curbuf->b_p_iminsert = B_IMODE_LMAP;
3020 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
3021 curbuf->b_p_imsearch = B_IMODE_LMAP;
3022 }
3023 else
3024 {
3025 // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
3026 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
3027 curbuf->b_p_iminsert = B_IMODE_NONE;
3028 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
3029 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3030 }
3031 if ((args->os_flags & OPT_LOCAL) == 0)
3032 {
3033 set_iminsert_global();
3034 set_imsearch_global();
3035 }
3036 status_redraw_curbuf();
3037 }
3038
3039 return errmsg;
3040}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003041#endif
3042
3043/*
3044 * The 'keymodel' option is changed.
3045 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003046 char *
3047did_set_keymodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003048{
3049 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
3050 return e_invalid_argument;
3051
3052 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
3053 km_startsel = (vim_strchr(p_km, 'a') != NULL);
3054 return NULL;
3055}
3056
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003057 int
3058expand_set_keymodel(optexpand_T *args, int *numMatches, char_u ***matches)
3059{
3060 return expand_set_opt_string(
3061 args,
3062 p_km_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003063 ARRAY_LENGTH(p_km_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003064 numMatches,
3065 matches);
3066}
3067
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003068/*
3069 * The 'keyprotocol' option is changed.
3070 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003071 char *
3072did_set_keyprotocol(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003073{
Gregory Anders3695d0e2023-09-29 20:17:20 +02003074 char_u *term = T_NAME;
3075 keyprot_T kpc = match_keyprotocol(term);
3076 if (kpc == KEYPROTOCOL_FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003077 return e_invalid_argument;
3078
Gregory Anders3695d0e2023-09-29 20:17:20 +02003079 apply_keyprotocol(term, kpc);
3080
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003081 return NULL;
3082}
3083
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003084 int
3085expand_set_keyprotocol(optexpand_T *args, int *numMatches, char_u ***matches)
3086{
3087 expand_T *xp = args->oe_xp;
3088 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3089 {
3090 // 'keyprotocol' only has well-defined terms for completion for the
3091 // protocol part after the colon.
3092 return expand_set_opt_string(
3093 args,
3094 p_kpc_protocol_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003095 ARRAY_LENGTH(p_kpc_protocol_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003096 numMatches,
3097 matches);
3098 }
3099 // Use expand_set_opt_string instead of returning FAIL so that we can
3100 // include the original value if args->oe_include_orig_val is set.
3101 static char *(empty[]) = {NULL};
3102 return expand_set_opt_string(args, empty, 0, numMatches, matches);
3103}
3104
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003105/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003106 * The 'lispoptions' option is changed.
3107 */
3108 char *
3109did_set_lispoptions(optset_T *args)
3110{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003111 char_u **varp = (char_u **)args->os_varp;
3112
3113 if (**varp != NUL
3114 && STRCMP(*varp, "expr:0") != 0
3115 && STRCMP(*varp, "expr:1") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003116 return e_invalid_argument;
3117
3118 return NULL;
3119}
3120
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003121 int
3122expand_set_lispoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3123{
3124 static char *(p_lop_values[]) = {"expr:0", "expr:1", NULL};
3125 return expand_set_opt_string(
3126 args,
3127 p_lop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003128 ARRAY_LENGTH(p_lop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003129 numMatches,
3130 matches);
3131}
3132
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003133/*
3134 * The 'matchpairs' option is changed.
3135 */
3136 char *
3137did_set_matchpairs(optset_T *args)
3138{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003139 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003140 char_u *p;
3141
3142 if (has_mbyte)
3143 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003144 for (p = *varp; *p != NUL; ++p)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003145 {
3146 int x2 = -1;
3147 int x3 = -1;
3148
3149 p += mb_ptr2len(p);
3150 if (*p != NUL)
3151 x2 = *p++;
3152 if (*p != NUL)
3153 {
3154 x3 = mb_ptr2char(p);
3155 p += mb_ptr2len(p);
3156 }
3157 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
3158 return e_invalid_argument;
3159 if (*p == NUL)
3160 break;
3161 }
3162 }
3163 else
3164 {
3165 // Check for "x:y,x:y"
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003166 for (p = *varp; *p != NUL; p += 4)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003167 {
3168 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
3169 return e_invalid_argument;
3170 if (p[3] == NUL)
3171 break;
3172 }
3173 }
3174
3175 return NULL;
3176}
3177
Christian Brabandt51d4d842024-12-06 17:26:25 +01003178/*
3179 * Process the updated 'messagesopt' option value.
3180 */
3181 char *
3182did_set_messagesopt(optset_T *args UNUSED)
3183{
3184 if (messagesopt_changed() == FAIL)
3185 return e_invalid_argument;
3186
3187 return NULL;
3188}
3189
3190 int
3191expand_set_messagesopt(optexpand_T *args, int *numMatches, char_u ***matches)
3192{
zeertzjq8cc43da2024-12-07 16:09:08 +01003193 static char *(p_mopt_values[]) = {"hit-enter", "wait:", "history:", NULL};
Christian Brabandt51d4d842024-12-06 17:26:25 +01003194 return expand_set_opt_string(
3195 args,
zeertzjq8cc43da2024-12-07 16:09:08 +01003196 p_mopt_values,
3197 ARRAY_LENGTH(p_mopt_values) - 1,
Christian Brabandt51d4d842024-12-06 17:26:25 +01003198 numMatches,
3199 matches);
3200}
3201
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003202#if defined(FEAT_SPELL) || defined(PROTO)
3203/*
3204 * The 'mkspellmem' option is changed.
3205 */
3206 char *
3207did_set_mkspellmem(optset_T *args UNUSED)
3208{
3209 if (spell_check_msm() != OK)
3210 return e_invalid_argument;
3211
3212 return NULL;
3213}
3214#endif
3215
3216/*
3217 * The 'mouse' option is changed.
3218 */
3219 char *
3220did_set_mouse(optset_T *args)
3221{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003222 char_u **varp = (char_u **)args->os_varp;
3223
Christian Brabandtb39b2402023-11-29 11:34:05 +01003224 return did_set_option_listflag(*varp, (char_u *)MOUSE_ALL, args->os_errbuf,
3225 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003226}
3227
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003228 int
3229expand_set_mouse(optexpand_T *args, int *numMatches, char_u ***matches)
3230{
3231 return expand_set_opt_listflag(args, (char_u*)MOUSE_ALL, numMatches, matches);
3232}
3233
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003234/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003235 * The 'mousemodel' option is changed.
3236 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003237 char *
3238did_set_mousemodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003239{
3240 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
3241 return e_invalid_argument;
3242#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
Ken Takata18d0d292023-12-19 20:12:29 +01003243 else if (*p_mousem != *args->os_oldval.string)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003244 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
3245 // to create or delete the popup menus.
3246 gui_motif_update_mousemodel(root_menu);
3247#endif
3248
3249 return NULL;
3250}
3251
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003252 int
3253expand_set_mousemodel(optexpand_T *args, int *numMatches, char_u ***matches)
3254{
3255 return expand_set_opt_string(
3256 args,
3257 p_mousem_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003258 ARRAY_LENGTH(p_mousem_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003259 numMatches,
3260 matches);
3261}
3262
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003263#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3264 char *
3265did_set_mouseshape(optset_T *args UNUSED)
3266{
3267 char *errmsg = NULL;
3268
3269 errmsg = parse_shape_opt(SHAPE_MOUSE);
3270 update_mouseshape(-1);
3271
3272 return errmsg;
3273}
3274#endif
3275
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003276/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003277 * The 'nrformats' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003278 */
3279 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003280did_set_nrformats(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003281{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003282 char_u **varp = (char_u **)args->os_varp;
3283
3284 return did_set_opt_strings(*varp, p_nf_values, TRUE);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003285}
3286
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003287 int
3288expand_set_nrformats(optexpand_T *args, int *numMatches, char_u ***matches)
3289{
3290 return expand_set_opt_string(
3291 args,
3292 p_nf_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003293 ARRAY_LENGTH(p_nf_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003294 numMatches,
3295 matches);
3296}
3297
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003298#if defined(FEAT_EVAL) || defined(PROTO)
3299/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003300 * One of the '*expr' options is changed: 'balloonexpr', 'diffexpr',
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003301 * 'foldexpr', 'foldtext', 'formatexpr', 'includeexpr', 'indentexpr',
3302 * 'patchexpr', 'printexpr' and 'charconvert'.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003303 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003304 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003305did_set_optexpr(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003306{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003307 char_u **varp = (char_u **)args->os_varp;
3308
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003309 // If the option value starts with <SID> or s:, then replace that with
3310 // the script identifier.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003311 char_u *name = get_scriptlocal_funcname(*varp);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003312 if (name != NULL)
3313 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003314 free_string_option(*varp);
3315 *varp = name;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003316 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003317
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003318 return NULL;
3319}
3320#endif
3321
3322/*
3323 * The 'pastetoggle' option is changed.
3324 */
3325 char *
3326did_set_pastetoggle(optset_T *args UNUSED)
3327{
3328 char_u *p;
3329
3330 // translate key codes like in a mapping
3331 if (*p_pt)
3332 {
zeertzjq7e0bae02023-08-11 23:15:38 +02003333 (void)replace_termcodes(p_pt, &p, 0,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003334 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
3335 if (p != NULL)
3336 {
3337 free_string_option(p_pt);
3338 p_pt = p;
3339 }
3340 }
3341
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003342 return NULL;
3343}
3344
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003345#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3346/*
3347 * The 'previewpopup' option is changed.
3348 */
3349 char *
3350did_set_previewpopup(optset_T *args UNUSED)
3351{
3352 if (parse_previewpopup(NULL) == FAIL)
3353 return e_invalid_argument;
3354
3355 return NULL;
3356}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003357
3358 int
3359expand_set_popupoption(optexpand_T *args, int *numMatches, char_u ***matches)
3360{
3361 expand_T *xp = args->oe_xp;
3362
3363 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3364 {
3365 // Within "highlight:"/"border:"/"align:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003366 int border_len = (int)STRLEN("border:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003367 if (xp->xp_pattern - args->oe_set_arg >= border_len &&
3368 STRNCMP(xp->xp_pattern - border_len, "border:", border_len) == 0)
3369 {
3370 return expand_set_opt_string(
3371 args,
3372 p_popup_option_border_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003373 ARRAY_LENGTH(p_popup_option_border_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003374 numMatches,
3375 matches);
3376 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003377 int align_len = (int)STRLEN("align:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003378 if (xp->xp_pattern - args->oe_set_arg >= align_len &&
3379 STRNCMP(xp->xp_pattern - align_len, "align:", align_len) == 0)
3380 {
3381 return expand_set_opt_string(
3382 args,
3383 p_popup_option_align_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003384 ARRAY_LENGTH(p_popup_option_align_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003385 numMatches,
3386 matches);
3387 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003388 int highlight_len = (int)STRLEN("highlight:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003389 if (xp->xp_pattern - args->oe_set_arg >= highlight_len &&
3390 STRNCMP(xp->xp_pattern - highlight_len, "highlight:", highlight_len) == 0)
3391 {
3392 // Return the list of all highlight names
3393 return expand_set_opt_generic(
3394 args,
3395 get_highlight_name,
3396 numMatches,
3397 matches);
3398 }
3399 return FAIL;
3400 }
3401
3402 return expand_set_opt_string(
3403 args,
3404 p_popup_option_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003405 ARRAY_LENGTH(p_popup_option_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003406 numMatches,
3407 matches);
3408}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003409#endif
3410
3411#if defined(FEAT_POSTSCRIPT) || defined(PROTO)
3412/*
3413 * The 'printencoding' option is changed.
3414 */
3415 char *
3416did_set_printencoding(optset_T *args UNUSED)
3417{
3418 char_u *s, *p;
3419
3420 // Canonize 'printencoding' if VIM standard one
3421 p = enc_canonize(p_penc);
3422 if (p != NULL)
3423 {
3424 vim_free(p_penc);
3425 p_penc = p;
3426 }
3427 else
3428 {
3429 // Ensure lower case and '-' for '_'
3430 for (s = p_penc; *s != NUL; s++)
3431 {
3432 if (*s == '_')
3433 *s = '-';
3434 else
3435 *s = TOLOWER_ASC(*s);
3436 }
3437 }
3438
3439 return NULL;
3440}
3441#endif
3442
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003443#if defined(FEAT_PRINTER) || defined(PROTO)
3444
3445 static char_u *
3446get_printoptions_names(expand_T *xp UNUSED, int idx)
3447{
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003448 if (idx >= (int)ARRAY_LENGTH(printer_opts))
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003449 return NULL;
3450 return (char_u*)printer_opts[idx].name;
3451}
3452
3453/*
3454 * Expand 'printoptions' option
3455 */
3456 int
3457expand_set_printoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3458{
3459 return expand_set_opt_generic(
3460 args,
3461 get_printoptions_names,
3462 numMatches,
3463 matches);
3464}
3465#endif
3466
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003467#if defined(FEAT_STL_OPT) || defined(PROTO)
3468/*
3469 * The 'statusline' or the 'tabline' or the 'rulerformat' option is changed.
3470 * "rulerformat" is TRUE if the 'rulerformat' option is changed.
3471 */
3472 static char *
3473parse_statustabline_rulerformat(optset_T *args, int rulerformat)
3474{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003475 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003476 char_u *s;
3477 char *errmsg = NULL;
3478 int wid;
3479
3480 if (rulerformat) // reset ru_wid first
3481 ru_wid = 0;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003482 s = *varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003483 if (rulerformat && *s == '%')
3484 {
3485 // set ru_wid if 'ruf' starts with "%99("
3486 if (*++s == '-') // ignore a '-'
3487 s++;
3488 wid = getdigits(&s);
3489 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
3490 ru_wid = wid;
3491 else
Yegappan Lakshmananac023e82024-11-27 20:55:45 +01003492 {
3493 // Validate the flags in 'rulerformat' only if it doesn't point to
3494 // a custom function ("%!" flag).
3495 if ((*varp)[1] != '!')
3496 errmsg = check_stl_option(p_ruf);
3497 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003498 }
3499 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
3500 else if (rulerformat || s[0] != '%' || s[1] != '!')
3501 errmsg = check_stl_option(s);
3502 if (rulerformat && errmsg == NULL)
3503 comp_col();
3504
3505 return errmsg;
3506}
3507#endif
3508
3509#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
3510/*
3511 * The 'renderoptions' option is changed.
3512 */
3513 char *
3514did_set_renderoptions(optset_T *args UNUSED)
3515{
3516 if (!gui_mch_set_rendering_options(p_rop))
3517 return e_invalid_argument;
3518
3519 return NULL;
3520}
3521#endif
3522
3523#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3524/*
3525 * The 'rightleftcmd' option is changed.
3526 */
3527 char *
3528did_set_rightleftcmd(optset_T *args)
3529{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003530 char_u **varp = (char_u **)args->os_varp;
3531
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003532 // Currently only "search" is a supported value.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003533 if (**varp != NUL && STRCMP(*varp, "search") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003534 return e_invalid_argument;
3535
3536 return NULL;
3537}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003538
3539 int
3540expand_set_rightleftcmd(optexpand_T *args, int *numMatches, char_u ***matches)
3541{
3542 static char *(p_rlc_values[]) = {"search", NULL};
3543 return expand_set_opt_string(
3544 args,
3545 p_rlc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003546 ARRAY_LENGTH(p_rlc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003547 numMatches,
3548 matches);
3549}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003550#endif
3551
3552#if defined(FEAT_STL_OPT) || defined(PROTO)
3553/*
3554 * The 'rulerformat' option is changed.
3555 */
3556 char *
3557did_set_rulerformat(optset_T *args)
3558{
3559 return parse_statustabline_rulerformat(args, TRUE);
3560}
3561#endif
3562
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02003563#if defined(FEAT_TABPANEL)
3564/*
3565 * Process the new 'tabpanelopt' option value.
3566 */
3567 char *
3568did_set_tabpanelopt(optset_T *args)
3569{
3570 if (tabpanelopt_changed() == FAIL)
3571 return e_invalid_argument;
3572
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02003573 return NULL;
3574}
3575
3576 int
3577expand_set_tabpanelopt(optexpand_T *args, int *numMatches, char_u ***matches)
3578{
Hirohito Higashi598bbb12025-05-22 22:41:05 +02003579 expand_T *xp = args->oe_xp;
3580
3581 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3582 {
3583 // Within "align:", we have a subgroup of possible options.
3584 int align_len = (int)STRLEN("align:");
3585 if (xp->xp_pattern - args->oe_set_arg >= align_len &&
3586 STRNCMP(xp->xp_pattern - align_len, "align:", align_len) == 0)
3587 {
3588 return expand_set_opt_string(
3589 args,
3590 p_tplo_align_values,
3591 ARRAY_LENGTH(p_tplo_align_values) - 1,
3592 numMatches,
3593 matches);
3594 }
3595 return FAIL;
3596 }
3597
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02003598 return expand_set_opt_string(
3599 args,
Hirohito Higashi598bbb12025-05-22 22:41:05 +02003600 p_tplo_values,
3601 ARRAY_LENGTH(p_tplo_values) - 1,
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02003602 numMatches,
3603 matches);
3604}
3605#endif
3606
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003607/*
3608 * The 'scrollopt' option is changed.
3609 */
3610 char *
3611did_set_scrollopt(optset_T *args UNUSED)
3612{
3613 return did_set_opt_strings(p_sbo, p_scbopt_values, TRUE);
3614}
3615
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003616 int
3617expand_set_scrollopt(optexpand_T *args, int *numMatches, char_u ***matches)
3618{
3619 return expand_set_opt_string(
3620 args,
3621 p_scbopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003622 ARRAY_LENGTH(p_scbopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003623 numMatches,
3624 matches);
3625}
3626
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003627/*
3628 * The 'selection' option is changed.
3629 */
3630 char *
3631did_set_selection(optset_T *args UNUSED)
3632{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003633 if (*p_sel == NUL || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003634 return e_invalid_argument;
3635
3636 return NULL;
3637}
3638
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003639 int
3640expand_set_selection(optexpand_T *args, int *numMatches, char_u ***matches)
3641{
3642 return expand_set_opt_string(
3643 args,
3644 p_sel_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003645 ARRAY_LENGTH(p_sel_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003646 numMatches,
3647 matches);
3648}
3649
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003650/*
3651 * The 'selectmode' option is changed.
3652 */
3653 char *
3654did_set_selectmode(optset_T *args UNUSED)
3655{
3656 return did_set_opt_strings(p_slm, p_slm_values, TRUE);
3657}
3658
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003659 int
3660expand_set_selectmode(optexpand_T *args, int *numMatches, char_u ***matches)
3661{
3662 return expand_set_opt_string(
3663 args,
3664 p_slm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003665 ARRAY_LENGTH(p_slm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003666 numMatches,
3667 matches);
3668}
3669
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003670#if defined(FEAT_SESSION) || defined(PROTO)
3671/*
3672 * The 'sessionoptions' option is changed.
3673 */
3674 char *
3675did_set_sessionoptions(optset_T *args)
3676{
3677 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
3678 return e_invalid_argument;
3679 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
3680 {
3681 // Don't allow both "sesdir" and "curdir".
3682 (void)opt_strings_flags(args->os_oldval.string, p_ssop_values,
3683 &ssop_flags, TRUE);
3684 return e_invalid_argument;
3685 }
3686
3687 return NULL;
3688}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003689
3690 int
3691expand_set_sessionoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3692{
3693 return expand_set_opt_string(
3694 args,
3695 p_ssop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003696 ARRAY_LENGTH(p_ssop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003697 numMatches,
3698 matches);
3699}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003700#endif
3701
3702/*
3703 * The 'shortmess' option is changed.
3704 */
3705 char *
3706did_set_shortmess(optset_T *args)
3707{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003708 char_u **varp = (char_u **)args->os_varp;
3709
Christian Brabandtb39b2402023-11-29 11:34:05 +01003710 return did_set_option_listflag(*varp, (char_u *)SHM_ALL, args->os_errbuf,
3711 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003712}
3713
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003714 int
3715expand_set_shortmess(optexpand_T *args, int *numMatches, char_u ***matches)
3716{
3717 return expand_set_opt_listflag(args, (char_u*)SHM_ALL, numMatches, matches);
3718}
3719
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003720#if defined(FEAT_LINEBREAK) || defined(PROTO)
3721/*
3722 * The 'showbreak' option is changed.
3723 */
3724 char *
3725did_set_showbreak(optset_T *args)
3726{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003727 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003728 char_u *s;
3729
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003730 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003731 {
3732 if (ptr2cells(s) != 1)
3733 return e_showbreak_contains_unprintable_or_wide_character;
3734 MB_PTR_ADV(s);
3735 }
3736
3737 return NULL;
3738}
3739#endif
3740
3741/*
3742 * The 'showcmdloc' option is changed.
3743 */
3744 char *
3745did_set_showcmdloc(optset_T *args UNUSED)
3746{
zeertzjqc27fcf42024-03-01 23:01:43 +01003747 char *errmsg = did_set_opt_strings(p_sloc, p_sloc_values, FALSE);
3748
3749 if (errmsg == NULL)
3750 comp_col();
3751
3752 return errmsg;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003753}
3754
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003755 int
3756expand_set_showcmdloc(optexpand_T *args, int *numMatches, char_u ***matches)
3757{
3758 return expand_set_opt_string(
3759 args,
3760 p_sloc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003761 ARRAY_LENGTH(p_sloc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003762 numMatches,
3763 matches);
3764}
3765
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003766#if defined(FEAT_SIGNS) || defined(PROTO)
3767/*
3768 * The 'signcolumn' option is changed.
3769 */
3770 char *
3771did_set_signcolumn(optset_T *args)
3772{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003773 char_u **varp = (char_u **)args->os_varp;
3774
3775 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003776 return e_invalid_argument;
3777 // When changing the 'signcolumn' to or from 'number', recompute the
3778 // width of the number column if 'number' or 'relativenumber' is set.
3779 if (((*args->os_oldval.string == 'n' && args->os_oldval.string[1] == 'u')
3780 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
3781 && (curwin->w_p_nu || curwin->w_p_rnu))
3782 curwin->w_nrwidth_line_count = 0;
3783
3784 return NULL;
3785}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003786
3787 int
3788expand_set_signcolumn(optexpand_T *args, int *numMatches, char_u ***matches)
3789{
3790 return expand_set_opt_string(
3791 args,
3792 p_scl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003793 ARRAY_LENGTH(p_scl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003794 numMatches,
3795 matches);
3796}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003797#endif
3798
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003799#if defined(FEAT_SPELL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003800/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003801 * The 'spellcapcheck' option is changed.
3802 */
3803 char *
3804did_set_spellcapcheck(optset_T *args UNUSED)
3805{
3806 // compile the regexp program.
3807 return compile_cap_prog(curwin->w_s);
3808}
3809
3810/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003811 * The 'spellfile' option is changed.
3812 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003813 char *
3814did_set_spellfile(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003815{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003816 char_u **varp = (char_u **)args->os_varp;
3817
3818 if (!valid_spellfile(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003819 return e_invalid_argument;
3820
3821 // If there is a window for this buffer in which 'spell' is set load the
3822 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003823 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003824}
3825
3826/*
3827 * The 'spell' option is changed.
3828 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003829 char *
3830did_set_spelllang(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003831{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003832 char_u **varp = (char_u **)args->os_varp;
3833
3834 if (!valid_spelllang(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003835 return e_invalid_argument;
3836
3837 // If there is a window for this buffer in which 'spell' is set load the
3838 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003839 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003840}
3841
3842/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003843 * The 'spelloptions' option is changed.
3844 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003845 char *
3846did_set_spelloptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003847{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003848 char_u **varp = (char_u **)args->os_varp;
3849
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003850 if (**varp != NUL && STRCMP(*varp, "camel") != 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003851 return e_invalid_argument;
3852
3853 return NULL;
3854}
3855
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003856 int
3857expand_set_spelloptions(optexpand_T *args, int *numMatches, char_u ***matches)
3858{
3859 static char *(p_spo_values[]) = {"camel", NULL};
3860 return expand_set_opt_string(
3861 args,
3862 p_spo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003863 ARRAY_LENGTH(p_spo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003864 numMatches,
3865 matches);
3866}
3867
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003868/*
3869 * The 'spellsuggest' option is changed.
3870 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003871 char *
3872did_set_spellsuggest(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003873{
3874 if (spell_check_sps() != OK)
3875 return e_invalid_argument;
3876
3877 return NULL;
3878}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003879
3880 int
3881expand_set_spellsuggest(optexpand_T *args, int *numMatches, char_u ***matches)
3882{
3883 return expand_set_opt_string(
3884 args,
3885 p_sps_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003886 ARRAY_LENGTH(p_sps_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003887 numMatches,
3888 matches);
3889}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003890#endif
3891
3892/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003893 * The 'splitkeep' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003894 */
3895 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003896did_set_splitkeep(optset_T *args UNUSED)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003897{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003898 return did_set_opt_strings(p_spk, p_spk_values, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003899}
3900
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003901 int
3902expand_set_splitkeep(optexpand_T *args, int *numMatches, char_u ***matches)
3903{
3904 return expand_set_opt_string(
3905 args,
3906 p_spk_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003907 ARRAY_LENGTH(p_spk_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003908 numMatches,
3909 matches);
3910}
3911
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00003912#if defined(FEAT_STL_OPT) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003913/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003914 * The 'statusline' option is changed.
3915 */
3916 char *
3917did_set_statusline(optset_T *args)
3918{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003919 return parse_statustabline_rulerformat(args, FALSE);
3920}
3921#endif
3922
3923/*
3924 * The 'swapsync' option is changed.
3925 */
3926 char *
3927did_set_swapsync(optset_T *args UNUSED)
3928{
3929 return did_set_opt_strings(p_sws, p_sws_values, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003930}
3931
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003932 int
3933expand_set_swapsync(optexpand_T *args, int *numMatches, char_u ***matches)
3934{
3935 return expand_set_opt_string(
3936 args,
3937 p_sws_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003938 ARRAY_LENGTH(p_sws_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003939 numMatches,
3940 matches);
3941}
3942
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003943/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003944 * The 'switchbuf' option is changed.
3945 */
3946 char *
3947did_set_switchbuf(optset_T *args UNUSED)
3948{
3949 return did_set_opt_flags(p_swb, p_swb_values, &swb_flags, TRUE);
3950}
3951
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003952 int
3953expand_set_switchbuf(optexpand_T *args, int *numMatches, char_u ***matches)
3954{
3955 return expand_set_opt_string(
3956 args,
3957 p_swb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003958 ARRAY_LENGTH(p_swb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003959 numMatches,
3960 matches);
3961}
3962
LemonBoy5247b0b2024-07-12 19:30:58 +02003963/*
3964 * The 'tabclose' option is changed.
3965 */
3966 char *
3967did_set_tabclose(optset_T *args UNUSED)
3968{
3969 return did_set_opt_flags(p_tcl, p_tcl_values, &tcl_flags, TRUE);
3970}
3971
3972 int
3973expand_set_tabclose(optexpand_T *args, int *numMatches, char_u ***matches)
3974{
3975 return expand_set_opt_string(
3976 args,
3977 p_tcl_values,
3978 ARRAY_LENGTH(p_tcl_values) - 1,
3979 numMatches,
3980 matches);
3981}
3982
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003983#if defined(FEAT_STL_OPT) || defined(PROTO)
3984/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003985 * The 'tabline' option is changed.
3986 */
3987 char *
3988did_set_tabline(optset_T *args)
3989{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003990 return parse_statustabline_rulerformat(args, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003991}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003992#endif
3993
3994/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003995 * The 'tagcase' option is changed.
3996 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003997 char *
3998did_set_tagcase(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003999{
4000 unsigned int *flags;
4001 char_u *p;
4002
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004003 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004004 {
4005 p = curbuf->b_p_tc;
4006 flags = &curbuf->b_tc_flags;
4007 }
4008 else
4009 {
4010 p = p_tc;
4011 flags = &tc_flags;
4012 }
4013
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004014 if ((args->os_flags & OPT_LOCAL) && *p == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004015 // make the local value empty: use the global value
4016 *flags = 0;
4017 else if (*p == NUL
4018 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
4019 return e_invalid_argument;
4020
4021 return NULL;
4022}
4023
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004024 int
4025expand_set_tagcase(optexpand_T *args, int *numMatches, char_u ***matches)
4026{
4027 return expand_set_opt_string(
4028 args,
4029 p_tc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004030 ARRAY_LENGTH(p_tc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004031 numMatches,
4032 matches);
4033}
4034
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004035/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004036 * The 'term' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004037 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004038 char *
4039did_set_term(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004040{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004041 if (T_NAME[0] == NUL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004042 return e_cannot_set_term_to_empty_string;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004043#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004044 if (gui.in_use)
4045 return e_cannot_change_term_in_GUI;
4046 if (term_is_gui(T_NAME))
4047 return e_use_gui_to_start_GUI;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004048#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004049 if (set_termname(T_NAME) == FAIL)
4050 return e_not_found_in_termcap;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004051
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004052 // Screen colors may have changed.
4053 redraw_later_clear();
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004054
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004055 return NULL;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004056}
4057
4058/*
4059 * Some terminal option (t_xxx) is changed
4060 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004061 char *
4062did_set_term_option(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004063{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004064 char_u **varp = (char_u **)args->os_varp;
4065
4066 if (!full_screen)
4067 return NULL;
4068
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004069 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
4070 if (varp == &T_CCO)
4071 {
4072 int colors = atoi((char *)T_CCO);
4073
4074 // Only reinitialize colors if t_Co value has really changed to
4075 // avoid expensive reload of colorscheme if t_Co is set to the
4076 // same value multiple times.
4077 if (colors != t_colors)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004078 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004079 t_colors = colors;
4080 if (t_colors <= 1)
4081 {
4082 vim_free(T_CCO);
4083 T_CCO = empty_option;
4084 }
4085#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
4086 if (is_term_win32())
4087 {
4088 swap_tcap();
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004089 args->os_did_swaptcap = TRUE;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004090 }
4091#endif
4092 // We now have a different color setup, initialize it again.
4093 init_highlight(TRUE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004094 }
4095 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004096 ttest(FALSE);
4097 if (varp == &T_ME)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004098 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004099 out_str(T_ME);
4100 redraw_later(UPD_CLEAR);
4101#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4102 // Since t_me has been set, this probably means that the user
4103 // wants to use this as default colors. Need to reset default
4104 // background/foreground colors.
4105# ifdef VIMDLL
4106 if (!gui.in_use && !gui.starting)
4107# endif
4108 mch_set_normal_colors();
4109#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004110 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004111 if (varp == &T_BE && termcap_active)
4112 {
4113 MAY_WANT_TO_LOG_THIS;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004114
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004115 if (*T_BE == NUL)
4116 // When clearing t_BE we assume the user no longer wants
4117 // bracketed paste, thus disable it by writing t_BD.
4118 out_str(T_BD);
4119 else
4120 out_str(T_BE);
4121 }
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004122
4123 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004124}
4125
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004126#if defined(FEAT_TERMINAL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004127/*
4128 * The 'termwinkey' option is changed.
4129 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004130 char *
Milly94606f72024-10-22 22:07:52 +02004131did_set_termwinkey(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004132{
Milly94606f72024-10-22 22:07:52 +02004133 char_u **varp = (char_u **)args->os_varp;
4134
4135 if ((*varp)[0] != NUL && string_to_key(*varp, TRUE) == 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004136 return e_invalid_argument;
4137
4138 return NULL;
4139}
4140
4141/*
4142 * The 'termwinsize' option is changed.
4143 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004144 char *
Milly8be10aa2024-10-22 22:01:46 +02004145did_set_termwinsize(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004146{
Milly8be10aa2024-10-22 22:01:46 +02004147 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004148 char_u *p;
4149
Milly8be10aa2024-10-22 22:01:46 +02004150 if ((*varp)[0] == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004151 return NULL;
4152
Milly8be10aa2024-10-22 22:01:46 +02004153 p = skipdigits(*varp);
4154 if (p == *varp || (*p != 'x' && *p != '*') || *skipdigits(p + 1) != NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004155 return e_invalid_argument;
4156
4157 return NULL;
4158}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00004159
4160# if defined(MSWIN) || defined(PROTO)
4161/*
4162 * The 'termwintype' option is changed.
4163 */
4164 char *
4165did_set_termwintype(optset_T *args UNUSED)
4166{
4167 return did_set_opt_strings(p_twt, p_twt_values, FALSE);
4168}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004169
4170 int
4171expand_set_termwintype(optexpand_T *args, int *numMatches, char_u ***matches)
4172{
4173 return expand_set_opt_string(
4174 args,
4175 p_twt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004176 ARRAY_LENGTH(p_twt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004177 numMatches,
4178 matches);
4179}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00004180# endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004181#endif
4182
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004183/*
4184 * The 'titlestring' option is changed.
4185 */
4186 char *
4187did_set_titlestring(optset_T *args)
4188{
4189 int flagval = 0;
4190
4191#ifdef FEAT_STL_OPT
4192 flagval = STL_IN_TITLE;
4193#endif
4194 return parse_titleiconstring(args, flagval);
4195}
4196
4197#if (defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)) || defined(PROTO)
4198/*
4199 * The 'toolbar' option is changed.
4200 */
4201 char *
4202did_set_toolbar(optset_T *args UNUSED)
4203{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004204 if (opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags,
4205 TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004206 return e_invalid_argument;
4207
4208 out_flush();
4209 gui_mch_show_toolbar((toolbar_flags &
4210 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4211 return NULL;
4212}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004213
4214 int
4215expand_set_toolbar(optexpand_T *args, int *numMatches, char_u ***matches)
4216{
4217 return expand_set_opt_string(
4218 args,
4219 p_toolbar_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004220 ARRAY_LENGTH(p_toolbar_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004221 numMatches,
4222 matches);
4223}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004224#endif
4225
4226#if (defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)) || defined(PROTO)
4227/*
4228 * The 'toolbariconsize' option is changed. GTK+ 2 only.
4229 */
4230 char *
4231did_set_toolbariconsize(optset_T *args UNUSED)
4232{
4233 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
4234 return e_invalid_argument;
4235
4236 out_flush();
4237 gui_mch_show_toolbar((toolbar_flags &
4238 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4239 return NULL;
4240}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004241
4242 int
4243expand_set_toolbariconsize(optexpand_T *args, int *numMatches, char_u ***matches)
4244{
4245 return expand_set_opt_string(
4246 args,
4247 p_tbis_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004248 ARRAY_LENGTH(p_tbis_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004249 numMatches,
4250 matches);
4251}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004252#endif
4253
4254#if defined(UNIX) || defined(VMS) || defined(PROTO)
4255/*
4256 * The 'ttymouse' option is changed.
4257 */
4258 char *
4259did_set_ttymouse(optset_T *args UNUSED)
4260{
4261 char *errmsg = NULL;
4262
4263 // Switch the mouse off before changing the escape sequences used for
4264 // that.
4265 mch_setmouse(FALSE);
4266 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
4267 errmsg = e_invalid_argument;
4268 else
4269 check_mouse_termcode();
4270 if (termcap_active)
4271 setmouse(); // may switch it on again
4272
4273 return errmsg;
4274}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004275
4276 int
4277expand_set_ttymouse(optexpand_T *args, int *numMatches, char_u ***matches)
4278{
4279 return expand_set_opt_string(
4280 args,
4281 p_ttym_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004282 ARRAY_LENGTH(p_ttym_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004283 numMatches,
4284 matches);
4285}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004286#endif
4287
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004288#if defined(FEAT_VARTABS) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004289/*
4290 * The 'varsofttabstop' option is changed.
4291 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004292 char *
4293did_set_varsofttabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004294{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004295 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004296 char_u *cp;
4297
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004298 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004299 VIM_CLEAR(curbuf->b_p_vsts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004300 else
4301 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004302 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004303 {
4304 if (vim_isdigit(*cp))
4305 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004306 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004307 continue;
4308 return e_invalid_argument;
4309 }
4310
4311 int *oldarray = curbuf->b_p_vsts_array;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004312 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004313 {
4314 if (oldarray)
4315 vim_free(oldarray);
4316 }
4317 else
4318 return e_invalid_argument;
4319 }
4320
4321 return NULL;
4322}
4323
4324/*
4325 * The 'vartabstop' option is changed.
4326 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004327 char *
4328did_set_vartabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004329{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004330 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004331 char_u *cp;
4332
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004333 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004334 VIM_CLEAR(curbuf->b_p_vts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004335 else
4336 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004337 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004338 {
4339 if (vim_isdigit(*cp))
4340 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004341 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004342 continue;
4343 return e_invalid_argument;
4344 }
4345
4346 int *oldarray = curbuf->b_p_vts_array;
4347
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004348 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004349 {
4350 vim_free(oldarray);
4351# ifdef FEAT_FOLDING
4352 if (foldmethodIsIndent(curwin))
4353 foldUpdateAll(curwin);
4354# endif
4355 }
4356 else
4357 return e_invalid_argument;
4358 }
4359
4360 return NULL;
4361}
4362#endif
4363
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004364/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004365 * The 'verbosefile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004366 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004367 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004368did_set_verbosefile(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004369{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004370 verbose_stop();
4371 if (*p_vfile != NUL && verbose_open() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004372 return e_invalid_argument;
4373
4374 return NULL;
4375}
4376
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004377#if defined(FEAT_SESSION) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004378/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004379 * The 'viewoptions' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004380 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004381 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004382did_set_viewoptions(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004383{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004384 return did_set_opt_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004385}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004386#endif
4387
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004388#if defined(FEAT_VIMINFO) || defined(PROTO)
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004389/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004390 * The 'viminfo' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004391 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004392 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004393did_set_viminfo(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004394{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004395 char_u *s;
4396 char *errmsg = NULL;
4397
4398 for (s = p_viminfo; *s;)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004399 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004400 // Check it's a valid character
4401 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
4402 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01004403 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004404 break;
4405 }
4406 if (*s == 'n') // name is always last one
4407 break;
4408 else if (*s == 'r') // skip until next ','
4409 {
4410 while (*++s && *s != ',')
4411 ;
4412 }
4413 else if (*s == '%')
4414 {
4415 // optional number
4416 while (vim_isdigit(*++s))
4417 ;
4418 }
4419 else if (*s == '!' || *s == 'h' || *s == 'c')
4420 ++s; // no extra chars
4421 else // must have a number
4422 {
4423 while (vim_isdigit(*++s))
4424 ;
4425
4426 if (!VIM_ISDIGIT(*(s - 1)))
4427 {
4428 if (args->os_errbuf != NULL)
4429 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01004430 vim_snprintf(args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004431 _(e_missing_number_after_angle_str_angle),
4432 transchar_byte(*(s - 1)));
4433 errmsg = args->os_errbuf;
4434 }
4435 else
4436 errmsg = "";
4437 break;
4438 }
4439 }
4440 if (*s == ',')
4441 ++s;
4442 else if (*s)
4443 {
4444 if (args->os_errbuf != NULL)
4445 errmsg = e_missing_comma;
4446 else
4447 errmsg = "";
4448 break;
4449 }
4450 }
4451 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
4452 errmsg = e_must_specify_a_value;
4453
4454 return errmsg;
4455}
4456#endif
4457
4458/*
4459 * The 'virtualedit' option is changed.
4460 */
4461 char *
4462did_set_virtualedit(optset_T *args)
4463{
4464 char_u *ve = p_ve;
4465 unsigned int *flags = &ve_flags;
4466
4467 if (args->os_flags & OPT_LOCAL)
4468 {
4469 ve = curwin->w_p_ve;
4470 flags = &curwin->w_ve_flags;
4471 }
4472
4473 if ((args->os_flags & OPT_LOCAL) && *ve == NUL)
4474 // make the local value empty: use the global value
4475 *flags = 0;
4476 else
4477 {
4478 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
4479 return e_invalid_argument;
4480 else if (STRCMP(ve, args->os_oldval.string) != 0)
4481 {
4482 // Recompute cursor position in case the new 've' setting
4483 // changes something.
4484 validate_virtcol();
4485 coladvance(curwin->w_virtcol);
4486 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004487 }
4488
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004489 return NULL;
4490}
4491
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004492 int
4493expand_set_virtualedit(optexpand_T *args, int *numMatches, char_u ***matches)
4494{
4495 return expand_set_opt_string(
4496 args,
4497 p_ve_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004498 ARRAY_LENGTH(p_ve_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004499 numMatches,
4500 matches);
4501}
4502
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004503/*
4504 * The 'whichwrap' option is changed.
4505 */
4506 char *
4507did_set_whichwrap(optset_T *args)
4508{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004509 char_u **varp = (char_u **)args->os_varp;
4510
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004511 // Add ',' to the list flags because 'whichwrap' is a flag
4512 // list that is comma-separated.
Christian Brabandtb39b2402023-11-29 11:34:05 +01004513 return did_set_option_listflag(*varp, (char_u *)(WW_ALL ","),
4514 args->os_errbuf, args->os_errbuflen);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004515}
4516
4517 int
4518expand_set_whichwrap(optexpand_T *args, int *numMatches, char_u ***matches)
4519{
4520 return expand_set_opt_listflag(args, (char_u*)WW_ALL, numMatches, matches);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004521}
4522
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004523/*
4524 * The 'wildmode' option is changed.
4525 */
4526 char *
4527did_set_wildmode(optset_T *args UNUSED)
4528{
4529 if (check_opt_wim() == FAIL)
4530 return e_invalid_argument;
4531 return NULL;
4532}
4533
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004534 int
4535expand_set_wildmode(optexpand_T *args, int *numMatches, char_u ***matches)
4536{
4537 return expand_set_opt_string(
4538 args,
4539 p_wim_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004540 ARRAY_LENGTH(p_wim_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004541 numMatches,
4542 matches);
4543}
4544
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004545/*
4546 * The 'wildoptions' option is changed.
4547 */
4548 char *
4549did_set_wildoptions(optset_T *args UNUSED)
4550{
4551 return did_set_opt_strings(p_wop, p_wop_values, TRUE);
4552}
4553
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004554 int
4555expand_set_wildoptions(optexpand_T *args, int *numMatches, char_u ***matches)
4556{
4557 return expand_set_opt_string(
4558 args,
4559 p_wop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004560 ARRAY_LENGTH(p_wop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004561 numMatches,
4562 matches);
4563}
4564
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004565#if defined(FEAT_WAK) || defined(PROTO)
4566/*
4567 * The 'winaltkeys' option is changed.
4568 */
4569 char *
4570did_set_winaltkeys(optset_T *args UNUSED)
4571{
4572 char *errmsg = NULL;
4573
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004574 if (*p_wak == NUL || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004575 errmsg = e_invalid_argument;
4576# ifdef FEAT_MENU
4577# if defined(FEAT_GUI_MOTIF)
4578 else if (gui.in_use)
4579 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4580# elif defined(FEAT_GUI_GTK)
4581 else if (gui.in_use)
4582 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4583# endif
4584# endif
4585 return errmsg;
4586}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004587
4588 int
4589expand_set_winaltkeys(optexpand_T *args, int *numMatches, char_u ***matches)
4590{
4591 return expand_set_opt_string(
4592 args,
4593 p_wak_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004594 ARRAY_LENGTH(p_wak_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004595 numMatches,
4596 matches);
4597}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004598#endif
4599
4600/*
4601 * The 'wincolor' option is changed.
4602 */
4603 char *
4604did_set_wincolor(optset_T *args UNUSED)
4605{
4606#ifdef FEAT_TERMINAL
4607 term_update_wincolor(curwin);
4608#endif
4609 return NULL;
4610}
4611
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004612 int
4613expand_set_wincolor(optexpand_T *args, int *numMatches, char_u ***matches)
4614{
4615 return expand_set_opt_generic(
4616 args,
4617 get_highlight_name,
4618 numMatches,
4619 matches);
4620}
4621
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004622#ifdef FEAT_SYN_HL
4623/*
4624 * When the 'syntax' option is set, load the syntax of that name.
4625 */
4626 static void
4627do_syntax_autocmd(int value_changed)
4628{
4629 static int syn_recursive = 0;
4630
4631 ++syn_recursive;
4632 // Only pass TRUE for "force" when the value changed or not used
4633 // recursively, to avoid endless recurrence.
4634 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
4635 value_changed || syn_recursive == 1, curbuf);
4636 curbuf->b_flags |= BF_SYN_SET;
4637 --syn_recursive;
4638}
4639#endif
4640
4641/*
4642 * When the 'filetype' option is set, trigger the FileType autocommand.
4643 */
4644 static void
4645do_filetype_autocmd(char_u **varp, int opt_flags, int value_changed)
4646{
4647 // Skip this when called from a modeline and the filetype was already set
4648 // to this value.
4649 if ((opt_flags & OPT_MODELINE) && !value_changed)
4650 return;
4651
4652 static int ft_recursive = 0;
4653 int secure_save = secure;
4654
4655 // Reset the secure flag, since the value of 'filetype' has
4656 // been checked to be safe.
4657 secure = 0;
4658
4659 ++ft_recursive;
zeertzjq5bf6c212024-03-31 18:41:27 +02004660 curbuf->b_did_filetype = TRUE;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004661 // Only pass TRUE for "force" when the value changed or not
4662 // used recursively, to avoid endless recurrence.
4663 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
4664 value_changed || ft_recursive == 1, curbuf);
4665 --ft_recursive;
4666 // Just in case the old "curbuf" is now invalid.
4667 if (varp != &(curbuf->b_p_ft))
4668 varp = NULL;
4669
4670 secure = secure_save;
4671}
4672
4673#ifdef FEAT_SPELL
4674/*
4675 * When the 'spelllang' option is set, source the spell/LANG.vim file in
4676 * 'runtimepath'.
4677 */
4678 static void
4679do_spelllang_source(void)
4680{
4681 char_u fname[200];
4682 char_u *p;
4683 char_u *q = curwin->w_s->b_p_spl;
4684
4685 // Skip the first name if it is "cjk".
4686 if (STRNCMP(q, "cjk,", 4) == 0)
4687 q += 4;
4688
4689 // They could set 'spellcapcheck' depending on the language. Use the first
4690 // name in 'spelllang' up to '_region' or '.encoding'.
4691 for (p = q; *p != NUL; ++p)
4692 if (!ASCII_ISALNUM(*p) && *p != '-')
4693 break;
4694 if (p > q)
4695 {
4696 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
4697 (int)(p - q), q);
4698 source_runtime(fname, DIP_ALL);
4699 }
4700}
4701#endif
4702
4703/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004704 * Handle string options that need some action to perform when changed.
zeertzjqf6782732022-07-27 18:26:03 +01004705 * The new value must be allocated.
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004706 * Returns NULL for success, or an untranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +02004707 */
4708 char *
4709did_set_string_option(
4710 int opt_idx, // index in options[] table
4711 char_u **varp, // pointer to the option variable
Bram Moolenaardac13472019-09-16 21:06:21 +02004712 char_u *oldval, // previous value of the option
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004713 char_u *value, // new value of the option
Bram Moolenaardac13472019-09-16 21:06:21 +02004714 char *errbuf, // buffer for errors, or NULL
Mike Williams620f0112023-12-05 15:36:06 +01004715 size_t errbuflen, // length of error buffer
Bram Moolenaardac13472019-09-16 21:06:21 +02004716 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004717 set_op_T op, // OP_ADDING/OP_PREPENDING/OP_REMOVING
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004718 int *value_checked) // value was checked to be safe, no
Bram Moolenaardac13472019-09-16 21:06:21 +02004719 // need to set P_INSECURE
4720{
4721 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +02004722 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004723 opt_did_set_cb_T did_set_cb = get_option_did_set_cb(opt_idx);
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004724 optset_T args;
4725
4726 // 'ttytype' is an alias for 'term'. Both 'term' and 'ttytype' point to
4727 // T_NAME. If 'term' or 'ttytype' is modified, then use the index for the
4728 // 'term' option. Only set the P_ALLOCED flag on 'term'.
4729 if (varp == &T_NAME)
4730 {
4731 opt_idx = findoption((char_u *)"term");
4732 if (opt_idx >= 0)
4733 {
4734 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
4735 did_set_cb = get_option_did_set_cb(opt_idx);
4736 }
4737 }
4738
4739 CLEAR_FIELD(args);
Bram Moolenaardac13472019-09-16 21:06:21 +02004740
Bram Moolenaardac13472019-09-16 21:06:21 +02004741 // Disallow changing some options from secure mode
4742 if ((secure
4743#ifdef HAVE_SANDBOX
4744 || sandbox != 0
4745#endif
4746 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00004747 errmsg = e_not_allowed_here;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004748 // Check for a "normal" directory or file name in some options.
4749 else if (check_illegal_path_names(opt_idx, varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004750 errmsg = e_invalid_argument;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004751 else if (did_set_cb != NULL)
4752 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004753 args.os_varp = (char_u *)varp;
4754 args.os_idx = opt_idx;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004755 args.os_flags = opt_flags;
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004756 args.os_op = op;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004757 args.os_oldval.string = oldval;
4758 args.os_newval.string = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004759 args.os_errbuf = errbuf;
Christian Brabandtb39b2402023-11-29 11:34:05 +01004760 args.os_errbuflen = errbuflen;
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004761 // Invoke the option specific callback function to validate and apply
4762 // the new option value.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004763 errmsg = did_set_cb(&args);
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004764
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004765 // The 'keymap', 'filetype' and 'syntax' option callback functions
4766 // may change the os_value_checked field.
4767 *value_checked = args.os_value_checked;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004768 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004769
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004770 // If an error is detected, restore the previous value.
Bram Moolenaardac13472019-09-16 21:06:21 +02004771 if (errmsg != NULL)
4772 {
zeertzjqf6782732022-07-27 18:26:03 +01004773 free_string_option(*varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02004774 *varp = oldval;
4775 // When resetting some values, need to act on it.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004776 if (args.os_restore_chartab)
Bram Moolenaardac13472019-09-16 21:06:21 +02004777 (void)init_chartab();
4778 if (varp == &p_hl)
4779 (void)highlight_changed();
4780 }
4781 else
4782 {
4783#ifdef FEAT_EVAL
4784 // Remember where the option was set.
4785 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4786#endif
4787 // Free string options that are in allocated memory.
4788 // Use "free_oldval", because recursiveness may change the flags under
4789 // our fingers (esp. init_highlight()).
4790 if (free_oldval)
4791 free_string_option(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01004792 set_option_flag(opt_idx, P_ALLOCED);
Bram Moolenaardac13472019-09-16 21:06:21 +02004793
4794 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
4795 && is_global_local_option(opt_idx))
4796 {
4797 // global option with local value set to use global value; free
4798 // the local value and make it empty
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004799 char_u *p = get_option_varp_scope(opt_idx, OPT_LOCAL);
Bram Moolenaardac13472019-09-16 21:06:21 +02004800 free_string_option(*(char_u **)p);
4801 *(char_u **)p = empty_option;
4802 }
4803
4804 // May set global value for local option.
4805 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
4806 set_string_option_global(opt_idx, varp);
4807
4808 // Trigger the autocommand only after setting the flags.
4809#ifdef FEAT_SYN_HL
Bram Moolenaardac13472019-09-16 21:06:21 +02004810 if (varp == &(curbuf->b_p_syn))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004811 do_syntax_autocmd(args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004812#endif
4813 else if (varp == &(curbuf->b_p_ft))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004814 do_filetype_autocmd(varp, opt_flags, args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004815#ifdef FEAT_SPELL
4816 if (varp == &(curwin->w_s->b_p_spl))
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004817 do_spelllang_source();
Bram Moolenaardac13472019-09-16 21:06:21 +02004818#endif
4819 }
4820
Bram Moolenaardac13472019-09-16 21:06:21 +02004821 if (varp == &p_mouse)
4822 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004823 if (*p_mouse == NUL)
4824 mch_setmouse(FALSE); // switch mouse off
4825 else
Bram Moolenaardac13472019-09-16 21:06:21 +02004826 setmouse(); // in case 'mouse' changed
4827 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004828
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004829 if (varp == &p_rtp)
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004830 {
4831 export_myvimdir();
4832#if defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004833 update_package_paths_in_lua();
4834#endif
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004835 }
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004836
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004837#if defined(FEAT_LINEBREAK)
4838 // Changing Formatlistpattern when briopt includes the list setting:
4839 // redraw
4840 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
4841 && curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004842 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004843#endif
4844
Bram Moolenaardac13472019-09-16 21:06:21 +02004845 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004846 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0
4847 && (get_option_flags(opt_idx) & P_HLONLY) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02004848 curwin->w_set_curswant = TRUE;
4849
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004850 if ((opt_flags & OPT_NO_REDRAW) == 0)
4851 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004852#ifdef FEAT_GUI
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004853 // set when changing an option that only requires a redraw in the GUI
4854 int redraw_gui_only = FALSE;
4855
4856 if (varp == &p_go // 'guioptions'
4857 || varp == &p_guifont // 'guifont'
4858# ifdef FEAT_GUI_TABLINE
4859 || varp == &p_gtl // 'guitablabel'
4860 || varp == &p_gtt // 'guitabtooltip'
4861# endif
4862# ifdef FEAT_XFONTSET
4863 || varp == &p_guifontset // 'guifontset'
4864# endif
4865 || varp == &p_guifontwide // 'guifontwide'
Erik S. V. Jansson2f026382024-02-26 22:23:05 +01004866# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004867 || varp == &p_guiligatures // 'guiligatures'
4868# endif
4869 )
4870 redraw_gui_only = TRUE;
4871
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004872 // check redraw when it's not a GUI option or the GUI is active.
4873 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02004874#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004875 check_redraw(get_option_flags(opt_idx));
4876 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004877
4878#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004879 if (args.os_did_swaptcap)
Bram Moolenaardac13472019-09-16 21:06:21 +02004880 {
4881 set_termname((char_u *)"win32");
4882 init_highlight(TRUE, FALSE);
4883 }
4884#endif
4885
4886 return errmsg;
4887}
4888
4889/*
4890 * Check an option that can be a range of string values.
4891 *
4892 * Return OK for correct value, FAIL otherwise.
4893 * Empty is always OK.
4894 */
4895 static int
4896check_opt_strings(
4897 char_u *val,
4898 char **values,
4899 int list) // when TRUE: accept a list of values
4900{
4901 return opt_strings_flags(val, values, NULL, list);
4902}
4903
4904/*
4905 * Handle an option that can be a range of string values.
4906 * Set a flag in "*flagp" for each string present.
4907 *
4908 * Return OK for correct value, FAIL otherwise.
4909 * Empty is always OK.
4910 */
4911 static int
4912opt_strings_flags(
4913 char_u *val, // new value
4914 char **values, // array of valid string values
4915 unsigned *flagp,
4916 int list) // when TRUE: accept a list of values
4917{
4918 int i;
4919 int len;
4920 unsigned new_flags = 0;
4921
4922 while (*val)
4923 {
4924 for (i = 0; ; ++i)
4925 {
4926 if (values[i] == NULL) // val not found in values[]
4927 return FAIL;
4928
4929 len = (int)STRLEN(values[i]);
4930 if (STRNCMP(values[i], val, len) == 0
4931 && ((list && val[len] == ',') || val[len] == NUL))
4932 {
4933 val += len + (val[len] == ',');
4934 new_flags |= (1 << i);
4935 break; // check next item in val list
4936 }
4937 }
4938 }
4939 if (flagp != NULL)
4940 *flagp = new_flags;
4941
4942 return OK;
4943}
4944
4945/*
4946 * return OK if "p" is a valid fileformat name, FAIL otherwise.
4947 */
4948 int
4949check_ff_value(char_u *p)
4950{
4951 return check_opt_strings(p, p_ff_values, FALSE);
4952}
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004953
4954/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004955 * Save the actual shortmess Flags and clear them temporarily to avoid that
4956 * file messages overwrites any output from the following commands.
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004957 *
4958 * Caller must make sure to first call save_clear_shm_value() and then
4959 * restore_shm_value() exactly the same number of times.
4960 */
4961 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004962save_clear_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004963{
4964 if (STRLEN(p_shm) >= SHM_LEN)
4965 {
4966 iemsg(e_internal_error_shortmess_too_long);
4967 return;
4968 }
4969
4970 if (++set_shm_recursive == 1)
4971 {
4972 STRCPY(shm_buf, p_shm);
4973 set_option_value_give_err((char_u *)"shm", 0L, (char_u *)"", 0);
4974 }
4975}
4976
4977/*
4978 * Restore the shortmess Flags set from the save_clear_shm_value() function.
4979 */
4980 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004981restore_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004982{
4983 if (--set_shm_recursive == 0)
4984 {
4985 set_option_value_give_err((char_u *)"shm", 0L, shm_buf, 0);
4986 vim_memset(shm_buf, 0, SHM_LEN);
4987 }
4988}
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004989
4990/*
4991 * Export the environment variable $MYVIMDIR to the first item in runtimepath
4992 */
Christian Brabandt1a741d32025-03-01 16:30:33 +01004993 void
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004994export_myvimdir()
4995{
4996 int dofree = FALSE;
4997 char_u *p;
4998 char_u *q = p_rtp;
4999 char_u *buf = alloc(MAXPATHL);
5000
5001 if (buf == NULL)
5002 return;
5003
5004 (void)copy_option_part(&q, buf, MAXPATHL, ",");
5005
5006 p = vim_getenv((char_u *)"MYVIMDIR", &dofree);
5007
5008 if (p == NULL || STRCMP(p, buf) != 0)
5009 {
5010 add_pathsep(buf);
5011#ifdef MSWIN
5012 // normalize path separators
5013 for (q = buf; *q != NUL; q++)
5014 if (*q == '/')
5015 *q = '\\';
5016#endif
5017 vim_setenv((char_u *)"MYVIMDIR", buf);
5018 }
5019 if (dofree)
5020 vim_free(p);
5021 vim_free(buf);
5022}