blob: 603d0f4251f2a26970797e788b4843b5bb35d14a [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 Baalb7147f82025-02-08 18:52:39 +01002251 // 'eventignore(win)' allows special keyword "all" in addition to
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002252 // all event names.
2253 if (idx == 0)
2254 return (char_u *)"all";
Luuk van Baalb7147f82025-02-08 18:52:39 +01002255 return get_event_name_no_group(xp, idx - 1, expand_eiw);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002256}
2257
2258 int
2259expand_set_eventignore(optexpand_T *args, int *numMatches, char_u ***matches)
2260{
Luuk van Baalb7147f82025-02-08 18:52:39 +01002261 expand_eiw = args->oe_varp != (char_u *)&p_ei;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002262 return expand_set_opt_generic(
2263 args,
2264 get_eventignore_name,
2265 numMatches,
2266 matches);
2267}
2268
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002269/*
2270 * The 'fileformat' option is changed.
2271 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002272 char *
2273did_set_fileformat(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002274{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002275 char_u **varp = (char_u **)args->os_varp;
2276
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002277 if (!curbuf->b_p_ma && !(args->os_flags & OPT_GLOBAL))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002278 return e_cannot_make_changes_modifiable_is_off;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002279 else if (check_opt_strings(*varp, p_ff_values, FALSE) != OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002280 return e_invalid_argument;
2281
2282 // may also change 'textmode'
2283 if (get_fileformat(curbuf) == EOL_DOS)
2284 curbuf->b_p_tx = TRUE;
2285 else
2286 curbuf->b_p_tx = FALSE;
2287 redraw_titles();
2288 // update flag in swap file
2289 ml_setflags(curbuf);
2290 // Redraw needed when switching to/from "mac": a CR in the text
2291 // will be displayed differently.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002292 if (get_fileformat(curbuf) == EOL_MAC || *args->os_oldval.string == 'm')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002293 redraw_curbuf_later(UPD_NOT_VALID);
2294
2295 return NULL;
2296}
2297
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002298 int
2299expand_set_fileformat(optexpand_T *args, int *numMatches, char_u ***matches)
2300{
2301 return expand_set_opt_string(
2302 args,
2303 p_ff_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002304 ARRAY_LENGTH(p_ff_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002305 numMatches,
2306 matches);
2307}
2308
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002309/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002310 * Function given to ExpandGeneric() to obtain the possible arguments of the
2311 * fileformat options.
2312 */
2313 char_u *
2314get_fileformat_name(expand_T *xp UNUSED, int idx)
2315{
2316 if (idx >= (int)ARRAY_LENGTH(p_ff_values))
2317 return NULL;
2318
2319 return (char_u*)p_ff_values[idx];
2320}
2321
2322/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002323 * The 'fileformats' option is changed.
2324 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002325 char *
2326did_set_fileformats(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002327{
2328 if (check_opt_strings(p_ffs, p_ff_values, TRUE) != OK)
2329 return e_invalid_argument;
2330
2331 // also change 'textauto'
2332 if (*p_ffs == NUL)
2333 p_ta = FALSE;
2334 else
2335 p_ta = TRUE;
2336
2337 return NULL;
2338}
2339
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002340/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002341 * The 'filetype' or the 'syntax' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002342 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002343 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002344did_set_filetype_or_syntax(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002345{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002346 char_u **varp = (char_u **)args->os_varp;
2347
2348 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002349 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002350
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002351 args->os_value_changed = STRCMP(args->os_oldval.string, *varp) != 0;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002352
2353 // Since we check the value, there is no need to set P_INSECURE,
2354 // even when the value comes from a modeline.
2355 args->os_value_checked = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002356
2357 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002358}
2359
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002360#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002361/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002362 * The 'foldclose' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002363 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002364 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002365did_set_foldclose(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002366{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002367 return did_set_opt_strings(p_fcl, p_fcl_values, TRUE);
2368}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002369
2370 int
2371expand_set_foldclose(optexpand_T *args, int *numMatches, char_u ***matches)
2372{
2373 return expand_set_opt_string(
2374 args,
2375 p_fcl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002376 ARRAY_LENGTH(p_fcl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002377 numMatches,
2378 matches);
2379}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002380#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002381
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002382#if (defined(FEAT_EVAL) && defined(FEAT_FOLDING)) || defined(PROTO)
2383/*
2384 * The 'foldexpr' option is changed.
2385 */
2386 char *
2387did_set_foldexpr(optset_T *args)
2388{
2389 (void)did_set_optexpr(args);
2390 if (foldmethodIsExpr(curwin))
2391 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002392 return NULL;
2393}
2394#endif
2395
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002396#if defined(FEAT_FOLDING) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002397/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002398 * The 'foldignore' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002399 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002400 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002401did_set_foldignore(optset_T *args UNUSED)
2402{
2403 if (foldmethodIsIndent(curwin))
2404 foldUpdateAll(curwin);
2405 return NULL;
2406}
2407
2408/*
2409 * The 'foldmarker' option is changed.
2410 */
2411 char *
2412did_set_foldmarker(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002413{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002414 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002415 char_u *p;
2416
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002417 p = vim_strchr(*varp, ',');
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002418 if (p == NULL)
2419 return e_comma_required;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002420 else if (p == *varp || p[1] == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002421 return e_invalid_argument;
2422 else if (foldmethodIsMarker(curwin))
2423 foldUpdateAll(curwin);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002424
2425 return NULL;
2426}
2427
2428/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002429 * The 'foldmethod' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002430 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002431 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002432did_set_foldmethod(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002433{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002434 char_u **varp = (char_u **)args->os_varp;
2435
Milly142cad12024-10-22 22:11:51 +02002436 if (check_opt_strings(*varp, p_fdm_values, FALSE) != OK || **varp == NUL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002437 return e_invalid_argument;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002438
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002439 foldUpdateAll(curwin);
2440 if (foldmethodIsDiff(curwin))
2441 newFoldLevel();
2442 return NULL;
2443}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002444
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002445 int
2446expand_set_foldmethod(optexpand_T *args, int *numMatches, char_u ***matches)
2447{
2448 return expand_set_opt_string(
2449 args,
2450 p_fdm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002451 ARRAY_LENGTH(p_fdm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002452 numMatches,
2453 matches);
2454}
2455
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002456/*
2457 * The 'foldopen' option is changed.
2458 */
2459 char *
2460did_set_foldopen(optset_T *args UNUSED)
2461{
2462 return did_set_opt_flags(p_fdo, p_fdo_values, &fdo_flags, TRUE);
2463}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002464
2465 int
2466expand_set_foldopen(optexpand_T *args, int *numMatches, char_u ***matches)
2467{
2468 return expand_set_opt_string(
2469 args,
2470 p_fdo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002471 ARRAY_LENGTH(p_fdo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002472 numMatches,
2473 matches);
2474}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002475#endif
2476
2477/*
2478 * The 'formatoptions' option is changed.
2479 */
2480 char *
2481did_set_formatoptions(optset_T *args)
2482{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002483 char_u **varp = (char_u **)args->os_varp;
2484
Christian Brabandtb39b2402023-11-29 11:34:05 +01002485 return did_set_option_listflag(*varp, (char_u *)FO_ALL, args->os_errbuf,
2486 args->os_errbuflen);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002487}
2488
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002489 int
2490expand_set_formatoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2491{
2492 return expand_set_opt_listflag(args, (char_u*)FO_ALL, numMatches, matches);
2493}
2494
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002495#if defined(CURSOR_SHAPE) || defined(PROTO)
2496/*
2497 * The 'guicursor' option is changed.
2498 */
2499 char *
2500did_set_guicursor(optset_T *args UNUSED)
2501{
2502 return parse_shape_opt(SHAPE_CURSOR);
2503}
2504#endif
2505
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002506#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002507/*
2508 * The 'guifont' option is changed.
2509 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002510 char *
2511did_set_guifont(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002512{
2513 char_u *p;
2514 char *errmsg = NULL;
2515
2516 if (gui.in_use)
2517 {
2518 p = p_guifont;
2519# if defined(FEAT_GUI_GTK)
2520 // Put up a font dialog and let the user select a new value.
2521 // If this is cancelled go back to the old value but don't
2522 // give an error message.
2523 if (STRCMP(p, "*") == 0)
2524 {
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002525 p = gui_mch_font_dialog(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002526 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002527 p_guifont = (p != NULL) ? p : vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002528 }
2529# endif
2530 if (p != NULL && gui_init_font(p_guifont, FALSE) != OK)
2531 {
2532# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)
2533 if (STRCMP(p_guifont, "*") == 0)
2534 {
2535 // Dialog was cancelled: Keep the old value without giving
2536 // an error message.
2537 free_string_option(p_guifont);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002538 p_guifont = vim_strsave(args->os_oldval.string);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002539 }
2540 else
2541# endif
2542 errmsg = e_invalid_fonts;
2543 }
2544 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002545
2546 return errmsg;
2547}
2548
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002549/*
2550 * Expand the 'guifont' option. Only when GUI is being used. Each platform has
2551 * specific behaviors.
2552 */
2553 int
2554expand_set_guifont(optexpand_T *args, int *numMatches, char_u ***matches)
2555{
2556 if (!gui.in_use)
2557 return FAIL;
2558
2559# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
2560 char_u **varp = (char_u **)args->oe_varp;
2561 int wide = (varp == &p_guifontwide);
2562
2563 return expand_set_opt_callback(
2564 args, gui_mch_expand_font, &wide, numMatches, matches);
2565# else
2566 return FAIL;
2567# endif
2568}
2569
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002570# if defined(FEAT_XFONTSET) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002571/*
2572 * The 'guifontset' option is changed.
2573 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002574 char *
2575did_set_guifontset(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002576{
2577 char *errmsg = NULL;
2578
2579 if (STRCMP(p_guifontset, "*") == 0)
2580 errmsg = e_cant_select_fontset;
2581 else if (gui.in_use && gui_init_font(p_guifontset, TRUE) != OK)
2582 errmsg = e_invalid_fontset;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002583
2584 return errmsg;
2585}
2586# endif
2587
2588/*
2589 * The 'guifontwide' option is changed.
2590 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002591 char *
2592did_set_guifontwide(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002593{
2594 char *errmsg = NULL;
2595
2596 if (STRCMP(p_guifontwide, "*") == 0)
2597 errmsg = e_cant_select_wide_font;
2598 else if (gui_get_wide_font() == FAIL)
2599 errmsg = e_invalid_wide_font;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002600
2601 return errmsg;
2602}
2603#endif
2604
Erik S. V. Jansson8b1e7492024-02-24 14:26:52 +01002605#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002606/*
2607 * The 'guiligatures' option is changed.
2608 */
2609 char *
2610did_set_guiligatures(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002611{
2612 gui_set_ligatures();
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002613 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002614}
2615#endif
2616
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002617#if defined(FEAT_GUI) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002618/*
2619 * The 'guioptions' option is changed.
2620 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002621 char *
2622did_set_guioptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002623{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002624 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002625 char *errmsg;
2626
Christian Brabandtb39b2402023-11-29 11:34:05 +01002627 errmsg = did_set_option_listflag(*varp, (char_u *)GO_ALL, args->os_errbuf,
2628 args->os_errbuflen);
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00002629 if (errmsg != NULL)
2630 return errmsg;
2631
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002632 gui_init_which_components(args->os_oldval.string);
2633 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002634}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002635
2636 int
2637expand_set_guioptions(optexpand_T *args, int *numMatches, char_u ***matches)
2638{
2639 return expand_set_opt_listflag(args, (char_u*)GO_ALL, numMatches, matches);
2640}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002641#endif
2642
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002643#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002644/*
2645 * The 'guitablabel' option is changed.
2646 */
2647 char *
2648did_set_guitablabel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002649{
2650 redraw_tabline = TRUE;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002651 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002652}
2653#endif
2654
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002655/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002656 * The 'helpfile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002657 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002658 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002659did_set_helpfile(optset_T *args UNUSED)
2660{
2661 // May compute new values for $VIM and $VIMRUNTIME
2662 if (didset_vim)
2663 vim_unsetenv_ext((char_u *)"VIM");
2664 if (didset_vimruntime)
2665 vim_unsetenv_ext((char_u *)"VIMRUNTIME");
2666 return NULL;
2667}
2668
2669#if defined(FEAT_MULTI_LANG) || defined(PROTO)
2670/*
2671 * The 'helplang' option is changed.
2672 */
2673 char *
2674did_set_helplang(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002675{
2676 char *errmsg = NULL;
2677
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002678 // Check for "", "ab", "ab,cd", etc.
2679 for (char_u *s = p_hlg; *s != NUL; s += 3)
2680 {
2681 if (s[1] == NUL || ((s[2] != ',' || s[3] == NUL) && s[2] != NUL))
2682 {
2683 errmsg = e_invalid_argument;
2684 break;
2685 }
2686 if (s[2] == NUL)
2687 break;
2688 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002689
2690 return errmsg;
2691}
2692#endif
2693
2694/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002695 * The 'highlight' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002696 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002697 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002698did_set_highlight(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002699{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002700 if (highlight_changed() == FAIL)
2701 return e_invalid_argument; // invalid flags
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002702
2703 return NULL;
2704}
2705
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002706/*
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002707 * Expand 'highlight' option.
2708 */
2709 int
2710expand_set_highlight(optexpand_T *args, int *numMatches, char_u ***matches)
2711{
2712 char_u *p;
2713 expand_T *xp = args->oe_xp;
2714 static char_u hl_flags[HLF_COUNT] = HL_FLAGS;
Christian Brabandt3f168ec2023-10-02 23:21:11 +02002715 size_t i;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002716 int count = 0;
2717
2718 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2719 {
2720 // Right after a ':', meaning we just return all highlight names.
2721 return expand_set_opt_generic(
2722 args,
2723 get_highlight_name,
2724 numMatches,
2725 matches);
2726 }
2727
2728 if (*xp->xp_pattern == NUL)
2729 {
2730 // At beginning of a comma-separated list. Return the specific list of
2731 // supported occasions.
2732 *matches = ALLOC_MULT(char_u *, HLF_COUNT + 1);
2733 if (*matches == NULL)
2734 return FAIL;
2735
2736 // We still want to return the full option if it's requested.
2737 if (args->oe_include_orig_val)
2738 {
2739 p = vim_strsave(args->oe_opt_value);
2740 if (p == NULL)
2741 {
2742 VIM_CLEAR(*matches);
2743 return FAIL;
2744 }
2745 (*matches)[count++] = p;
2746 }
2747
2748 for (i = 0; i < HLF_COUNT; i++)
2749 {
2750 p = vim_strnsave(&hl_flags[i], 1);
2751 if (p == NULL)
2752 {
2753 if (count == 0)
2754 {
2755 VIM_CLEAR(*matches);
2756 return FAIL;
2757 }
2758 else
2759 break;
2760 }
2761 (*matches)[count++] = p;
2762 }
2763
2764 if (count == 0)
2765 {
2766 VIM_CLEAR(*matches);
2767 return FAIL;
2768 }
2769 *numMatches = count;
2770 return OK;
2771 }
2772
2773 // We are after the initial character (which indicates the occasion). We
2774 // already made sure we are not matching after a ':' above, so now we want
2775 // to match against display mode modifiers.
2776 // Since the xp_pattern starts from the beginning, we need to include it in
2777 // the returned match.
2778
2779 // Note: Keep this in sync with highlight_changed()
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002780 static char_u p_hl_mode_values[] =
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002781 {':', 'b', 'i', '-', 'n', 'r', 's', 'u', 'c', '2', 'd', '=', 't'};
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002782 size_t num_hl_modes = ARRAY_LENGTH(p_hl_mode_values);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002783
2784 *matches = ALLOC_MULT(char_u *, num_hl_modes);
2785 if (*matches == NULL)
2786 return FAIL;
2787
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002788 int pattern_len = xp->xp_pattern_len;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002789
2790 for (i = 0; i < num_hl_modes; i++)
2791 {
2792 // Don't allow duplicates as these are unique flags
Yee Cheng Chin209ec902023-10-17 10:56:25 +02002793 char_u *dup = vim_strchr(xp->xp_pattern + 1, p_hl_mode_values[i]);
2794 if (dup != NULL && (int)(dup - xp->xp_pattern) < pattern_len)
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002795 continue;
2796
2797 // ':' only works by itself, not with other flags.
2798 if (pattern_len > 1 && p_hl_mode_values[i] == ':')
2799 continue;
2800
2801 p = vim_strnsave(xp->xp_pattern, pattern_len + 1);
2802 if (p == NULL)
2803 {
2804 if (i == 0)
2805 {
2806 VIM_CLEAR(*matches);
2807 return FAIL;
2808 }
2809 else
2810 break;
2811 }
2812 p[pattern_len] = p_hl_mode_values[i];
2813 p[pattern_len + 1] = NUL;
2814 (*matches)[count++] = p;
2815 }
2816 if (count == 0)
2817 {
2818 VIM_CLEAR(*matches);
2819 return FAIL;
2820 }
2821 *numMatches = count;
2822
2823 return OK;
2824}
2825
2826/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002827 * The 'titlestring' or the 'iconstring' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002828 */
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002829 static char *
2830parse_titleiconstring(optset_T *args UNUSED, int flagval UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002831{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002832#ifdef FEAT_STL_OPT
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002833 char_u **varp = (char_u **)args->os_varp;
2834
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002835 // NULL => statusline syntax
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002836 if (vim_strchr(*varp, '%') && check_stl_option(*varp) == NULL)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002837 stl_syntax |= flagval;
2838 else
2839 stl_syntax &= ~flagval;
2840#endif
2841 did_set_title();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00002842
2843 return NULL;
2844}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002845
2846/*
2847 * The 'iconstring' option is changed.
2848 */
2849 char *
2850did_set_iconstring(optset_T *args)
2851{
2852 int flagval = 0;
2853
2854#ifdef FEAT_STL_OPT
2855 flagval = STL_IN_ICON;
2856#endif
2857
2858 return parse_titleiconstring(args, flagval);
2859}
2860
2861#if (defined(FEAT_XIM) && defined(FEAT_GUI_GTK)) || defined(PROTO)
2862/*
2863 * The 'imactivatekey' option is changed.
2864 */
2865 char *
2866did_set_imactivatekey(optset_T *args UNUSED)
2867{
2868 if (!im_xim_isvalid_imactivate())
2869 return e_invalid_argument;
2870 return NULL;
2871}
2872#endif
2873
2874/*
glepnirbcd59952025-04-24 21:48:35 +02002875 * The 'isexpand' option is changed.
2876 */
2877 char *
2878did_set_isexpand(optset_T *args)
2879{
2880 char_u *ise = p_ise;
2881 char_u *p;
2882 int last_was_comma = FALSE;
2883
2884 if (args->os_flags & OPT_LOCAL)
2885 ise = curbuf->b_p_ise;
2886
2887 for (p = ise; *p != NUL;)
2888 {
2889 if (*p == '\\' && p[1] == ',')
2890 {
2891 p += 2;
2892 last_was_comma = FALSE;
2893 continue;
2894 }
2895
2896 if (*p == ',')
2897 {
2898 if (last_was_comma)
2899 return e_invalid_argument;
2900 last_was_comma = TRUE;
2901 p++;
2902 continue;
2903 }
2904
2905 last_was_comma = FALSE;
2906 MB_PTR_ADV(p);
2907 }
2908
2909 if (last_was_comma)
2910 return e_invalid_argument;
2911
2912 return NULL;
2913}
2914
2915
2916/*
Milly5e7a6a42024-10-22 22:27:19 +02002917 * The 'iskeyword' option is changed.
2918 */
2919 char *
2920did_set_iskeyword(optset_T *args)
2921{
2922 char_u **varp = (char_u **)args->os_varp;
2923
2924 if (varp == &p_isk) // only check for global-value
2925 {
2926 if (check_isopt(*varp) == FAIL)
2927 return e_invalid_argument;
2928 }
2929 else // fallthrough for local-value
2930 return did_set_isopt(args);
2931
2932 return NULL;
2933}
2934
2935/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002936 * The 'isident' or the 'iskeyword' or the 'isprint' or the 'isfname' option is
2937 * changed.
2938 */
2939 char *
2940did_set_isopt(optset_T *args)
2941{
Milly5e7a6a42024-10-22 22:27:19 +02002942 // 'isident', 'iskeyword', 'isprint' or 'isfname' option: refill g_chartab[]
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002943 // If the new option is invalid, use old value.
2944 // 'lisp' option: refill g_chartab[] for '-' char.
2945 if (init_chartab() == FAIL)
2946 {
2947 args->os_restore_chartab = TRUE;// need to restore the chartab.
2948 return e_invalid_argument; // error in value
2949 }
2950
2951 return NULL;
2952}
2953
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002954/*
2955 * The 'jumpoptions' option is changed.
2956 */
2957 char *
Yegappan Lakshmanan1926ae42023-09-21 16:36:28 +02002958did_set_jumpoptions(optset_T *args UNUSED)
Yegappan Lakshmanan87018252023-09-20 20:20:04 +02002959{
2960 if (opt_strings_flags(p_jop, p_jop_values, &jop_flags, TRUE) != OK)
2961 return e_invalid_argument;
2962
2963 return NULL;
2964}
2965
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002966 int
2967expand_set_jumpoptions(optexpand_T *args, int *numMatches, char_u ***matches)
2968{
2969 return expand_set_opt_string(
2970 args,
2971 p_jop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02002972 ARRAY_LENGTH(p_jop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002973 numMatches,
2974 matches);
2975}
2976
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002977#if defined(FEAT_KEYMAP) || defined(PROTO)
2978/*
2979 * The 'keymap' option is changed.
2980 */
2981 char *
2982did_set_keymap(optset_T *args)
2983{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002984 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002985 char *errmsg = NULL;
2986
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00002987 if (!valid_filetype(*varp))
Yegappan Lakshmananad608982023-03-01 12:44:06 +00002988 errmsg = e_invalid_argument;
2989 else
2990 {
2991 int secure_save = secure;
2992
2993 // Reset the secure flag, since the value of 'keymap' has
2994 // been checked to be safe.
2995 secure = 0;
2996
2997 // load or unload key mapping tables
2998 errmsg = keymap_init();
2999
3000 secure = secure_save;
3001
3002 // Since we check the value, there is no need to set P_INSECURE,
3003 // even when the value comes from a modeline.
3004 args->os_value_checked = TRUE;
3005 }
3006
3007 if (errmsg == NULL)
3008 {
3009 if (*curbuf->b_p_keymap != NUL)
3010 {
3011 // Installed a new keymap, switch on using it.
3012 curbuf->b_p_iminsert = B_IMODE_LMAP;
3013 if (curbuf->b_p_imsearch != B_IMODE_USE_INSERT)
3014 curbuf->b_p_imsearch = B_IMODE_LMAP;
3015 }
3016 else
3017 {
3018 // Cleared the keymap, may reset 'iminsert' and 'imsearch'.
3019 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
3020 curbuf->b_p_iminsert = B_IMODE_NONE;
3021 if (curbuf->b_p_imsearch == B_IMODE_LMAP)
3022 curbuf->b_p_imsearch = B_IMODE_USE_INSERT;
3023 }
3024 if ((args->os_flags & OPT_LOCAL) == 0)
3025 {
3026 set_iminsert_global();
3027 set_imsearch_global();
3028 }
3029 status_redraw_curbuf();
3030 }
3031
3032 return errmsg;
3033}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003034#endif
3035
3036/*
3037 * The 'keymodel' option is changed.
3038 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003039 char *
3040did_set_keymodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003041{
3042 if (check_opt_strings(p_km, p_km_values, TRUE) != OK)
3043 return e_invalid_argument;
3044
3045 km_stopsel = (vim_strchr(p_km, 'o') != NULL);
3046 km_startsel = (vim_strchr(p_km, 'a') != NULL);
3047 return NULL;
3048}
3049
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003050 int
3051expand_set_keymodel(optexpand_T *args, int *numMatches, char_u ***matches)
3052{
3053 return expand_set_opt_string(
3054 args,
3055 p_km_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003056 ARRAY_LENGTH(p_km_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003057 numMatches,
3058 matches);
3059}
3060
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003061/*
3062 * The 'keyprotocol' option is changed.
3063 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003064 char *
3065did_set_keyprotocol(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003066{
Gregory Anders3695d0e2023-09-29 20:17:20 +02003067 char_u *term = T_NAME;
3068 keyprot_T kpc = match_keyprotocol(term);
3069 if (kpc == KEYPROTOCOL_FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003070 return e_invalid_argument;
3071
Gregory Anders3695d0e2023-09-29 20:17:20 +02003072 apply_keyprotocol(term, kpc);
3073
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003074 return NULL;
3075}
3076
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003077 int
3078expand_set_keyprotocol(optexpand_T *args, int *numMatches, char_u ***matches)
3079{
3080 expand_T *xp = args->oe_xp;
3081 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3082 {
3083 // 'keyprotocol' only has well-defined terms for completion for the
3084 // protocol part after the colon.
3085 return expand_set_opt_string(
3086 args,
3087 p_kpc_protocol_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003088 ARRAY_LENGTH(p_kpc_protocol_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003089 numMatches,
3090 matches);
3091 }
3092 // Use expand_set_opt_string instead of returning FAIL so that we can
3093 // include the original value if args->oe_include_orig_val is set.
3094 static char *(empty[]) = {NULL};
3095 return expand_set_opt_string(args, empty, 0, numMatches, matches);
3096}
3097
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003098/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003099 * The 'lispoptions' option is changed.
3100 */
3101 char *
3102did_set_lispoptions(optset_T *args)
3103{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003104 char_u **varp = (char_u **)args->os_varp;
3105
3106 if (**varp != NUL
3107 && STRCMP(*varp, "expr:0") != 0
3108 && STRCMP(*varp, "expr:1") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003109 return e_invalid_argument;
3110
3111 return NULL;
3112}
3113
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003114 int
3115expand_set_lispoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3116{
3117 static char *(p_lop_values[]) = {"expr:0", "expr:1", NULL};
3118 return expand_set_opt_string(
3119 args,
3120 p_lop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003121 ARRAY_LENGTH(p_lop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003122 numMatches,
3123 matches);
3124}
3125
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003126/*
3127 * The 'matchpairs' option is changed.
3128 */
3129 char *
3130did_set_matchpairs(optset_T *args)
3131{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003132 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003133 char_u *p;
3134
3135 if (has_mbyte)
3136 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003137 for (p = *varp; *p != NUL; ++p)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003138 {
3139 int x2 = -1;
3140 int x3 = -1;
3141
3142 p += mb_ptr2len(p);
3143 if (*p != NUL)
3144 x2 = *p++;
3145 if (*p != NUL)
3146 {
3147 x3 = mb_ptr2char(p);
3148 p += mb_ptr2len(p);
3149 }
3150 if (x2 != ':' || x3 == -1 || (*p != NUL && *p != ','))
3151 return e_invalid_argument;
3152 if (*p == NUL)
3153 break;
3154 }
3155 }
3156 else
3157 {
3158 // Check for "x:y,x:y"
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003159 for (p = *varp; *p != NUL; p += 4)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003160 {
3161 if (p[1] != ':' || p[2] == NUL || (p[3] != NUL && p[3] != ','))
3162 return e_invalid_argument;
3163 if (p[3] == NUL)
3164 break;
3165 }
3166 }
3167
3168 return NULL;
3169}
3170
Christian Brabandt51d4d842024-12-06 17:26:25 +01003171/*
3172 * Process the updated 'messagesopt' option value.
3173 */
3174 char *
3175did_set_messagesopt(optset_T *args UNUSED)
3176{
3177 if (messagesopt_changed() == FAIL)
3178 return e_invalid_argument;
3179
3180 return NULL;
3181}
3182
3183 int
3184expand_set_messagesopt(optexpand_T *args, int *numMatches, char_u ***matches)
3185{
zeertzjq8cc43da2024-12-07 16:09:08 +01003186 static char *(p_mopt_values[]) = {"hit-enter", "wait:", "history:", NULL};
Christian Brabandt51d4d842024-12-06 17:26:25 +01003187 return expand_set_opt_string(
3188 args,
zeertzjq8cc43da2024-12-07 16:09:08 +01003189 p_mopt_values,
3190 ARRAY_LENGTH(p_mopt_values) - 1,
Christian Brabandt51d4d842024-12-06 17:26:25 +01003191 numMatches,
3192 matches);
3193}
3194
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003195#if defined(FEAT_SPELL) || defined(PROTO)
3196/*
3197 * The 'mkspellmem' option is changed.
3198 */
3199 char *
3200did_set_mkspellmem(optset_T *args UNUSED)
3201{
3202 if (spell_check_msm() != OK)
3203 return e_invalid_argument;
3204
3205 return NULL;
3206}
3207#endif
3208
3209/*
3210 * The 'mouse' option is changed.
3211 */
3212 char *
3213did_set_mouse(optset_T *args)
3214{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003215 char_u **varp = (char_u **)args->os_varp;
3216
Christian Brabandtb39b2402023-11-29 11:34:05 +01003217 return did_set_option_listflag(*varp, (char_u *)MOUSE_ALL, args->os_errbuf,
3218 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003219}
3220
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003221 int
3222expand_set_mouse(optexpand_T *args, int *numMatches, char_u ***matches)
3223{
3224 return expand_set_opt_listflag(args, (char_u*)MOUSE_ALL, numMatches, matches);
3225}
3226
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003227/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003228 * The 'mousemodel' option is changed.
3229 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003230 char *
3231did_set_mousemodel(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003232{
3233 if (check_opt_strings(p_mousem, p_mousem_values, FALSE) != OK)
3234 return e_invalid_argument;
3235#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU) && (XmVersion <= 1002)
Ken Takata18d0d292023-12-19 20:12:29 +01003236 else if (*p_mousem != *args->os_oldval.string)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003237 // Changed from "extend" to "popup" or "popup_setpos" or vv: need
3238 // to create or delete the popup menus.
3239 gui_motif_update_mousemodel(root_menu);
3240#endif
3241
3242 return NULL;
3243}
3244
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003245 int
3246expand_set_mousemodel(optexpand_T *args, int *numMatches, char_u ***matches)
3247{
3248 return expand_set_opt_string(
3249 args,
3250 p_mousem_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003251 ARRAY_LENGTH(p_mousem_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003252 numMatches,
3253 matches);
3254}
3255
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003256#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
3257 char *
3258did_set_mouseshape(optset_T *args UNUSED)
3259{
3260 char *errmsg = NULL;
3261
3262 errmsg = parse_shape_opt(SHAPE_MOUSE);
3263 update_mouseshape(-1);
3264
3265 return errmsg;
3266}
3267#endif
3268
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003269/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003270 * The 'nrformats' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003271 */
3272 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003273did_set_nrformats(optset_T *args)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003274{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003275 char_u **varp = (char_u **)args->os_varp;
3276
3277 return did_set_opt_strings(*varp, p_nf_values, TRUE);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003278}
3279
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003280 int
3281expand_set_nrformats(optexpand_T *args, int *numMatches, char_u ***matches)
3282{
3283 return expand_set_opt_string(
3284 args,
3285 p_nf_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003286 ARRAY_LENGTH(p_nf_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003287 numMatches,
3288 matches);
3289}
3290
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003291#if defined(FEAT_EVAL) || defined(PROTO)
3292/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003293 * One of the '*expr' options is changed: 'balloonexpr', 'diffexpr',
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003294 * 'foldexpr', 'foldtext', 'formatexpr', 'includeexpr', 'indentexpr',
3295 * 'patchexpr', 'printexpr' and 'charconvert'.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003296 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003297 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003298did_set_optexpr(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003299{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003300 char_u **varp = (char_u **)args->os_varp;
3301
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003302 // If the option value starts with <SID> or s:, then replace that with
3303 // the script identifier.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003304 char_u *name = get_scriptlocal_funcname(*varp);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003305 if (name != NULL)
3306 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003307 free_string_option(*varp);
3308 *varp = name;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003309 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003310
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003311 return NULL;
3312}
3313#endif
3314
3315/*
3316 * The 'pastetoggle' option is changed.
3317 */
3318 char *
3319did_set_pastetoggle(optset_T *args UNUSED)
3320{
3321 char_u *p;
3322
3323 // translate key codes like in a mapping
3324 if (*p_pt)
3325 {
zeertzjq7e0bae02023-08-11 23:15:38 +02003326 (void)replace_termcodes(p_pt, &p, 0,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003327 REPTERM_FROM_PART | REPTERM_DO_LT, NULL);
3328 if (p != NULL)
3329 {
3330 free_string_option(p_pt);
3331 p_pt = p;
3332 }
3333 }
3334
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003335 return NULL;
3336}
3337
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003338#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3339/*
3340 * The 'previewpopup' option is changed.
3341 */
3342 char *
3343did_set_previewpopup(optset_T *args UNUSED)
3344{
3345 if (parse_previewpopup(NULL) == FAIL)
3346 return e_invalid_argument;
3347
3348 return NULL;
3349}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003350
3351 int
3352expand_set_popupoption(optexpand_T *args, int *numMatches, char_u ***matches)
3353{
3354 expand_T *xp = args->oe_xp;
3355
3356 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3357 {
3358 // Within "highlight:"/"border:"/"align:", we have a subgroup of possible options.
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003359 int border_len = (int)STRLEN("border:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003360 if (xp->xp_pattern - args->oe_set_arg >= border_len &&
3361 STRNCMP(xp->xp_pattern - border_len, "border:", border_len) == 0)
3362 {
3363 return expand_set_opt_string(
3364 args,
3365 p_popup_option_border_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003366 ARRAY_LENGTH(p_popup_option_border_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003367 numMatches,
3368 matches);
3369 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003370 int align_len = (int)STRLEN("align:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003371 if (xp->xp_pattern - args->oe_set_arg >= align_len &&
3372 STRNCMP(xp->xp_pattern - align_len, "align:", align_len) == 0)
3373 {
3374 return expand_set_opt_string(
3375 args,
3376 p_popup_option_align_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003377 ARRAY_LENGTH(p_popup_option_align_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003378 numMatches,
3379 matches);
3380 }
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003381 int highlight_len = (int)STRLEN("highlight:");
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003382 if (xp->xp_pattern - args->oe_set_arg >= highlight_len &&
3383 STRNCMP(xp->xp_pattern - highlight_len, "highlight:", highlight_len) == 0)
3384 {
3385 // Return the list of all highlight names
3386 return expand_set_opt_generic(
3387 args,
3388 get_highlight_name,
3389 numMatches,
3390 matches);
3391 }
3392 return FAIL;
3393 }
3394
3395 return expand_set_opt_string(
3396 args,
3397 p_popup_option_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003398 ARRAY_LENGTH(p_popup_option_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003399 numMatches,
3400 matches);
3401}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003402#endif
3403
3404#if defined(FEAT_POSTSCRIPT) || defined(PROTO)
3405/*
3406 * The 'printencoding' option is changed.
3407 */
3408 char *
3409did_set_printencoding(optset_T *args UNUSED)
3410{
3411 char_u *s, *p;
3412
3413 // Canonize 'printencoding' if VIM standard one
3414 p = enc_canonize(p_penc);
3415 if (p != NULL)
3416 {
3417 vim_free(p_penc);
3418 p_penc = p;
3419 }
3420 else
3421 {
3422 // Ensure lower case and '-' for '_'
3423 for (s = p_penc; *s != NUL; s++)
3424 {
3425 if (*s == '_')
3426 *s = '-';
3427 else
3428 *s = TOLOWER_ASC(*s);
3429 }
3430 }
3431
3432 return NULL;
3433}
3434#endif
3435
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003436#if defined(FEAT_PRINTER) || defined(PROTO)
3437
3438 static char_u *
3439get_printoptions_names(expand_T *xp UNUSED, int idx)
3440{
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003441 if (idx >= (int)ARRAY_LENGTH(printer_opts))
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003442 return NULL;
3443 return (char_u*)printer_opts[idx].name;
3444}
3445
3446/*
3447 * Expand 'printoptions' option
3448 */
3449 int
3450expand_set_printoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3451{
3452 return expand_set_opt_generic(
3453 args,
3454 get_printoptions_names,
3455 numMatches,
3456 matches);
3457}
3458#endif
3459
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003460#if defined(FEAT_STL_OPT) || defined(PROTO)
3461/*
3462 * The 'statusline' or the 'tabline' or the 'rulerformat' option is changed.
3463 * "rulerformat" is TRUE if the 'rulerformat' option is changed.
3464 */
3465 static char *
3466parse_statustabline_rulerformat(optset_T *args, int rulerformat)
3467{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003468 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003469 char_u *s;
3470 char *errmsg = NULL;
3471 int wid;
3472
3473 if (rulerformat) // reset ru_wid first
3474 ru_wid = 0;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003475 s = *varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003476 if (rulerformat && *s == '%')
3477 {
3478 // set ru_wid if 'ruf' starts with "%99("
3479 if (*++s == '-') // ignore a '-'
3480 s++;
3481 wid = getdigits(&s);
3482 if (wid && *s == '(' && (errmsg = check_stl_option(p_ruf)) == NULL)
3483 ru_wid = wid;
3484 else
Yegappan Lakshmananac023e82024-11-27 20:55:45 +01003485 {
3486 // Validate the flags in 'rulerformat' only if it doesn't point to
3487 // a custom function ("%!" flag).
3488 if ((*varp)[1] != '!')
3489 errmsg = check_stl_option(p_ruf);
3490 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003491 }
3492 // check 'statusline' or 'tabline' only if it doesn't start with "%!"
3493 else if (rulerformat || s[0] != '%' || s[1] != '!')
3494 errmsg = check_stl_option(s);
3495 if (rulerformat && errmsg == NULL)
3496 comp_col();
3497
3498 return errmsg;
3499}
3500#endif
3501
3502#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
3503/*
3504 * The 'renderoptions' option is changed.
3505 */
3506 char *
3507did_set_renderoptions(optset_T *args UNUSED)
3508{
3509 if (!gui_mch_set_rendering_options(p_rop))
3510 return e_invalid_argument;
3511
3512 return NULL;
3513}
3514#endif
3515
3516#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
3517/*
3518 * The 'rightleftcmd' option is changed.
3519 */
3520 char *
3521did_set_rightleftcmd(optset_T *args)
3522{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003523 char_u **varp = (char_u **)args->os_varp;
3524
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003525 // Currently only "search" is a supported value.
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003526 if (**varp != NUL && STRCMP(*varp, "search") != 0)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003527 return e_invalid_argument;
3528
3529 return NULL;
3530}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003531
3532 int
3533expand_set_rightleftcmd(optexpand_T *args, int *numMatches, char_u ***matches)
3534{
3535 static char *(p_rlc_values[]) = {"search", NULL};
3536 return expand_set_opt_string(
3537 args,
3538 p_rlc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003539 ARRAY_LENGTH(p_rlc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003540 numMatches,
3541 matches);
3542}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003543#endif
3544
3545#if defined(FEAT_STL_OPT) || defined(PROTO)
3546/*
3547 * The 'rulerformat' option is changed.
3548 */
3549 char *
3550did_set_rulerformat(optset_T *args)
3551{
3552 return parse_statustabline_rulerformat(args, TRUE);
3553}
3554#endif
3555
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02003556#if defined(FEAT_TABPANEL)
3557/*
3558 * Process the new 'tabpanelopt' option value.
3559 */
3560 char *
3561did_set_tabpanelopt(optset_T *args)
3562{
3563 if (tabpanelopt_changed() == FAIL)
3564 return e_invalid_argument;
3565
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02003566 return NULL;
3567}
3568
3569 int
3570expand_set_tabpanelopt(optexpand_T *args, int *numMatches, char_u ***matches)
3571{
Hirohito Higashi598bbb12025-05-22 22:41:05 +02003572 expand_T *xp = args->oe_xp;
3573
3574 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
3575 {
3576 // Within "align:", we have a subgroup of possible options.
3577 int align_len = (int)STRLEN("align:");
3578 if (xp->xp_pattern - args->oe_set_arg >= align_len &&
3579 STRNCMP(xp->xp_pattern - align_len, "align:", align_len) == 0)
3580 {
3581 return expand_set_opt_string(
3582 args,
3583 p_tplo_align_values,
3584 ARRAY_LENGTH(p_tplo_align_values) - 1,
3585 numMatches,
3586 matches);
3587 }
3588 return FAIL;
3589 }
3590
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02003591 return expand_set_opt_string(
3592 args,
Hirohito Higashi598bbb12025-05-22 22:41:05 +02003593 p_tplo_values,
3594 ARRAY_LENGTH(p_tplo_values) - 1,
Naruhiko Nishinobe5bd4d2025-05-14 21:20:28 +02003595 numMatches,
3596 matches);
3597}
3598#endif
3599
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003600/*
3601 * The 'scrollopt' option is changed.
3602 */
3603 char *
3604did_set_scrollopt(optset_T *args UNUSED)
3605{
3606 return did_set_opt_strings(p_sbo, p_scbopt_values, TRUE);
3607}
3608
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003609 int
3610expand_set_scrollopt(optexpand_T *args, int *numMatches, char_u ***matches)
3611{
3612 return expand_set_opt_string(
3613 args,
3614 p_scbopt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003615 ARRAY_LENGTH(p_scbopt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003616 numMatches,
3617 matches);
3618}
3619
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003620/*
3621 * The 'selection' option is changed.
3622 */
3623 char *
3624did_set_selection(optset_T *args UNUSED)
3625{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003626 if (*p_sel == NUL || check_opt_strings(p_sel, p_sel_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003627 return e_invalid_argument;
3628
3629 return NULL;
3630}
3631
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003632 int
3633expand_set_selection(optexpand_T *args, int *numMatches, char_u ***matches)
3634{
3635 return expand_set_opt_string(
3636 args,
3637 p_sel_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003638 ARRAY_LENGTH(p_sel_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003639 numMatches,
3640 matches);
3641}
3642
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003643/*
3644 * The 'selectmode' option is changed.
3645 */
3646 char *
3647did_set_selectmode(optset_T *args UNUSED)
3648{
3649 return did_set_opt_strings(p_slm, p_slm_values, TRUE);
3650}
3651
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003652 int
3653expand_set_selectmode(optexpand_T *args, int *numMatches, char_u ***matches)
3654{
3655 return expand_set_opt_string(
3656 args,
3657 p_slm_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003658 ARRAY_LENGTH(p_slm_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003659 numMatches,
3660 matches);
3661}
3662
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003663#if defined(FEAT_SESSION) || defined(PROTO)
3664/*
3665 * The 'sessionoptions' option is changed.
3666 */
3667 char *
3668did_set_sessionoptions(optset_T *args)
3669{
3670 if (opt_strings_flags(p_ssop, p_ssop_values, &ssop_flags, TRUE) != OK)
3671 return e_invalid_argument;
3672 if ((ssop_flags & SSOP_CURDIR) && (ssop_flags & SSOP_SESDIR))
3673 {
3674 // Don't allow both "sesdir" and "curdir".
3675 (void)opt_strings_flags(args->os_oldval.string, p_ssop_values,
3676 &ssop_flags, TRUE);
3677 return e_invalid_argument;
3678 }
3679
3680 return NULL;
3681}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003682
3683 int
3684expand_set_sessionoptions(optexpand_T *args, int *numMatches, char_u ***matches)
3685{
3686 return expand_set_opt_string(
3687 args,
3688 p_ssop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003689 ARRAY_LENGTH(p_ssop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003690 numMatches,
3691 matches);
3692}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003693#endif
3694
3695/*
3696 * The 'shortmess' option is changed.
3697 */
3698 char *
3699did_set_shortmess(optset_T *args)
3700{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003701 char_u **varp = (char_u **)args->os_varp;
3702
Christian Brabandtb39b2402023-11-29 11:34:05 +01003703 return did_set_option_listflag(*varp, (char_u *)SHM_ALL, args->os_errbuf,
3704 args->os_errbuflen);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003705}
3706
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003707 int
3708expand_set_shortmess(optexpand_T *args, int *numMatches, char_u ***matches)
3709{
3710 return expand_set_opt_listflag(args, (char_u*)SHM_ALL, numMatches, matches);
3711}
3712
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003713#if defined(FEAT_LINEBREAK) || defined(PROTO)
3714/*
3715 * The 'showbreak' option is changed.
3716 */
3717 char *
3718did_set_showbreak(optset_T *args)
3719{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003720 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003721 char_u *s;
3722
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003723 for (s = *varp; *s; )
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003724 {
3725 if (ptr2cells(s) != 1)
3726 return e_showbreak_contains_unprintable_or_wide_character;
3727 MB_PTR_ADV(s);
3728 }
3729
3730 return NULL;
3731}
3732#endif
3733
3734/*
3735 * The 'showcmdloc' option is changed.
3736 */
3737 char *
3738did_set_showcmdloc(optset_T *args UNUSED)
3739{
zeertzjqc27fcf42024-03-01 23:01:43 +01003740 char *errmsg = did_set_opt_strings(p_sloc, p_sloc_values, FALSE);
3741
3742 if (errmsg == NULL)
3743 comp_col();
3744
3745 return errmsg;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003746}
3747
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003748 int
3749expand_set_showcmdloc(optexpand_T *args, int *numMatches, char_u ***matches)
3750{
3751 return expand_set_opt_string(
3752 args,
3753 p_sloc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003754 ARRAY_LENGTH(p_sloc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003755 numMatches,
3756 matches);
3757}
3758
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003759#if defined(FEAT_SIGNS) || defined(PROTO)
3760/*
3761 * The 'signcolumn' option is changed.
3762 */
3763 char *
3764did_set_signcolumn(optset_T *args)
3765{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003766 char_u **varp = (char_u **)args->os_varp;
3767
3768 if (check_opt_strings(*varp, p_scl_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003769 return e_invalid_argument;
3770 // When changing the 'signcolumn' to or from 'number', recompute the
3771 // width of the number column if 'number' or 'relativenumber' is set.
3772 if (((*args->os_oldval.string == 'n' && args->os_oldval.string[1] == 'u')
3773 || (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) =='u'))
3774 && (curwin->w_p_nu || curwin->w_p_rnu))
3775 curwin->w_nrwidth_line_count = 0;
3776
3777 return NULL;
3778}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003779
3780 int
3781expand_set_signcolumn(optexpand_T *args, int *numMatches, char_u ***matches)
3782{
3783 return expand_set_opt_string(
3784 args,
3785 p_scl_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003786 ARRAY_LENGTH(p_scl_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003787 numMatches,
3788 matches);
3789}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003790#endif
3791
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003792#if defined(FEAT_SPELL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003793/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003794 * The 'spellcapcheck' option is changed.
3795 */
3796 char *
3797did_set_spellcapcheck(optset_T *args UNUSED)
3798{
3799 // compile the regexp program.
3800 return compile_cap_prog(curwin->w_s);
3801}
3802
3803/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003804 * The 'spellfile' option is changed.
3805 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003806 char *
3807did_set_spellfile(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003808{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003809 char_u **varp = (char_u **)args->os_varp;
3810
3811 if (!valid_spellfile(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003812 return e_invalid_argument;
3813
3814 // If there is a window for this buffer in which 'spell' is set load the
3815 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003816 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003817}
3818
3819/*
3820 * The 'spell' option is changed.
3821 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003822 char *
3823did_set_spelllang(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003824{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003825 char_u **varp = (char_u **)args->os_varp;
3826
3827 if (!valid_spelllang(*varp))
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003828 return e_invalid_argument;
3829
3830 // If there is a window for this buffer in which 'spell' is set load the
3831 // wordlists.
Milly322ad0c2024-10-14 20:21:48 +02003832 return did_set_spell_option();
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003833}
3834
3835/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003836 * The 'spelloptions' option is changed.
3837 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003838 char *
3839did_set_spelloptions(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003840{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00003841 char_u **varp = (char_u **)args->os_varp;
3842
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00003843 if (**varp != NUL && STRCMP(*varp, "camel") != 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003844 return e_invalid_argument;
3845
3846 return NULL;
3847}
3848
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003849 int
3850expand_set_spelloptions(optexpand_T *args, int *numMatches, char_u ***matches)
3851{
3852 static char *(p_spo_values[]) = {"camel", NULL};
3853 return expand_set_opt_string(
3854 args,
3855 p_spo_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003856 ARRAY_LENGTH(p_spo_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003857 numMatches,
3858 matches);
3859}
3860
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003861/*
3862 * The 'spellsuggest' option is changed.
3863 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003864 char *
3865did_set_spellsuggest(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003866{
3867 if (spell_check_sps() != OK)
3868 return e_invalid_argument;
3869
3870 return NULL;
3871}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003872
3873 int
3874expand_set_spellsuggest(optexpand_T *args, int *numMatches, char_u ***matches)
3875{
3876 return expand_set_opt_string(
3877 args,
3878 p_sps_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003879 ARRAY_LENGTH(p_sps_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003880 numMatches,
3881 matches);
3882}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003883#endif
3884
3885/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003886 * The 'splitkeep' option is changed.
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003887 */
3888 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003889did_set_splitkeep(optset_T *args UNUSED)
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00003890{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003891 return did_set_opt_strings(p_spk, p_spk_values, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003892}
3893
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003894 int
3895expand_set_splitkeep(optexpand_T *args, int *numMatches, char_u ***matches)
3896{
3897 return expand_set_opt_string(
3898 args,
3899 p_spk_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003900 ARRAY_LENGTH(p_spk_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003901 numMatches,
3902 matches);
3903}
3904
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00003905#if defined(FEAT_STL_OPT) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003906/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003907 * The 'statusline' option is changed.
3908 */
3909 char *
3910did_set_statusline(optset_T *args)
3911{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003912 return parse_statustabline_rulerformat(args, FALSE);
3913}
3914#endif
3915
3916/*
3917 * The 'swapsync' option is changed.
3918 */
3919 char *
3920did_set_swapsync(optset_T *args UNUSED)
3921{
3922 return did_set_opt_strings(p_sws, p_sws_values, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003923}
3924
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003925 int
3926expand_set_swapsync(optexpand_T *args, int *numMatches, char_u ***matches)
3927{
3928 return expand_set_opt_string(
3929 args,
3930 p_sws_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003931 ARRAY_LENGTH(p_sws_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003932 numMatches,
3933 matches);
3934}
3935
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003936/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003937 * The 'switchbuf' option is changed.
3938 */
3939 char *
3940did_set_switchbuf(optset_T *args UNUSED)
3941{
3942 return did_set_opt_flags(p_swb, p_swb_values, &swb_flags, TRUE);
3943}
3944
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003945 int
3946expand_set_switchbuf(optexpand_T *args, int *numMatches, char_u ***matches)
3947{
3948 return expand_set_opt_string(
3949 args,
3950 p_swb_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02003951 ARRAY_LENGTH(p_swb_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003952 numMatches,
3953 matches);
3954}
3955
LemonBoy5247b0b2024-07-12 19:30:58 +02003956/*
3957 * The 'tabclose' option is changed.
3958 */
3959 char *
3960did_set_tabclose(optset_T *args UNUSED)
3961{
3962 return did_set_opt_flags(p_tcl, p_tcl_values, &tcl_flags, TRUE);
3963}
3964
3965 int
3966expand_set_tabclose(optexpand_T *args, int *numMatches, char_u ***matches)
3967{
3968 return expand_set_opt_string(
3969 args,
3970 p_tcl_values,
3971 ARRAY_LENGTH(p_tcl_values) - 1,
3972 numMatches,
3973 matches);
3974}
3975
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003976#if defined(FEAT_STL_OPT) || defined(PROTO)
3977/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003978 * The 'tabline' option is changed.
3979 */
3980 char *
3981did_set_tabline(optset_T *args)
3982{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00003983 return parse_statustabline_rulerformat(args, FALSE);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003984}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003985#endif
3986
3987/*
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003988 * The 'tagcase' option is changed.
3989 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003990 char *
3991did_set_tagcase(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003992{
3993 unsigned int *flags;
3994 char_u *p;
3995
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00003996 if (args->os_flags & OPT_LOCAL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00003997 {
3998 p = curbuf->b_p_tc;
3999 flags = &curbuf->b_tc_flags;
4000 }
4001 else
4002 {
4003 p = p_tc;
4004 flags = &tc_flags;
4005 }
4006
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004007 if ((args->os_flags & OPT_LOCAL) && *p == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004008 // make the local value empty: use the global value
4009 *flags = 0;
4010 else if (*p == NUL
4011 || opt_strings_flags(p, p_tc_values, flags, FALSE) != OK)
4012 return e_invalid_argument;
4013
4014 return NULL;
4015}
4016
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004017 int
4018expand_set_tagcase(optexpand_T *args, int *numMatches, char_u ***matches)
4019{
4020 return expand_set_opt_string(
4021 args,
4022 p_tc_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004023 ARRAY_LENGTH(p_tc_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004024 numMatches,
4025 matches);
4026}
4027
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004028/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004029 * The 'term' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004030 */
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004031 char *
4032did_set_term(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004033{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004034 if (T_NAME[0] == NUL)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004035 return e_cannot_set_term_to_empty_string;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004036#ifdef FEAT_GUI
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004037 if (gui.in_use)
4038 return e_cannot_change_term_in_GUI;
4039 if (term_is_gui(T_NAME))
4040 return e_use_gui_to_start_GUI;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004041#endif
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004042 if (set_termname(T_NAME) == FAIL)
4043 return e_not_found_in_termcap;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004044
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004045 // Screen colors may have changed.
4046 redraw_later_clear();
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004047
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004048 return NULL;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004049}
4050
4051/*
4052 * Some terminal option (t_xxx) is changed
4053 */
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004054 char *
4055did_set_term_option(optset_T *args)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004056{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004057 char_u **varp = (char_u **)args->os_varp;
4058
4059 if (!full_screen)
4060 return NULL;
4061
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004062 // ":set t_Co=0" and ":set t_Co=1" do ":set t_Co="
4063 if (varp == &T_CCO)
4064 {
4065 int colors = atoi((char *)T_CCO);
4066
4067 // Only reinitialize colors if t_Co value has really changed to
4068 // avoid expensive reload of colorscheme if t_Co is set to the
4069 // same value multiple times.
4070 if (colors != t_colors)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004071 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004072 t_colors = colors;
4073 if (t_colors <= 1)
4074 {
4075 vim_free(T_CCO);
4076 T_CCO = empty_option;
4077 }
4078#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
4079 if (is_term_win32())
4080 {
4081 swap_tcap();
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004082 args->os_did_swaptcap = TRUE;
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004083 }
4084#endif
4085 // We now have a different color setup, initialize it again.
4086 init_highlight(TRUE, FALSE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004087 }
4088 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004089 ttest(FALSE);
4090 if (varp == &T_ME)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004091 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004092 out_str(T_ME);
4093 redraw_later(UPD_CLEAR);
4094#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4095 // Since t_me has been set, this probably means that the user
4096 // wants to use this as default colors. Need to reset default
4097 // background/foreground colors.
4098# ifdef VIMDLL
4099 if (!gui.in_use && !gui.starting)
4100# endif
4101 mch_set_normal_colors();
4102#endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004103 }
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004104 if (varp == &T_BE && termcap_active)
4105 {
4106 MAY_WANT_TO_LOG_THIS;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004107
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004108 if (*T_BE == NUL)
4109 // When clearing t_BE we assume the user no longer wants
4110 // bracketed paste, thus disable it by writing t_BD.
4111 out_str(T_BD);
4112 else
4113 out_str(T_BE);
4114 }
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004115
4116 return NULL;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004117}
4118
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004119#if defined(FEAT_TERMINAL) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004120/*
4121 * The 'termwinkey' option is changed.
4122 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004123 char *
Milly94606f72024-10-22 22:07:52 +02004124did_set_termwinkey(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004125{
Milly94606f72024-10-22 22:07:52 +02004126 char_u **varp = (char_u **)args->os_varp;
4127
4128 if ((*varp)[0] != NUL && string_to_key(*varp, TRUE) == 0)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004129 return e_invalid_argument;
4130
4131 return NULL;
4132}
4133
4134/*
4135 * The 'termwinsize' option is changed.
4136 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004137 char *
Milly8be10aa2024-10-22 22:01:46 +02004138did_set_termwinsize(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004139{
Milly8be10aa2024-10-22 22:01:46 +02004140 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004141 char_u *p;
4142
Milly8be10aa2024-10-22 22:01:46 +02004143 if ((*varp)[0] == NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004144 return NULL;
4145
Milly8be10aa2024-10-22 22:01:46 +02004146 p = skipdigits(*varp);
4147 if (p == *varp || (*p != 'x' && *p != '*') || *skipdigits(p + 1) != NUL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004148 return e_invalid_argument;
4149
4150 return NULL;
4151}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00004152
4153# if defined(MSWIN) || defined(PROTO)
4154/*
4155 * The 'termwintype' option is changed.
4156 */
4157 char *
4158did_set_termwintype(optset_T *args UNUSED)
4159{
4160 return did_set_opt_strings(p_twt, p_twt_values, FALSE);
4161}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004162
4163 int
4164expand_set_termwintype(optexpand_T *args, int *numMatches, char_u ***matches)
4165{
4166 return expand_set_opt_string(
4167 args,
4168 p_twt_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004169 ARRAY_LENGTH(p_twt_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004170 numMatches,
4171 matches);
4172}
Yegappan Lakshmanan8ad862a2023-02-23 15:05:22 +00004173# endif
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004174#endif
4175
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004176/*
4177 * The 'titlestring' option is changed.
4178 */
4179 char *
4180did_set_titlestring(optset_T *args)
4181{
4182 int flagval = 0;
4183
4184#ifdef FEAT_STL_OPT
4185 flagval = STL_IN_TITLE;
4186#endif
4187 return parse_titleiconstring(args, flagval);
4188}
4189
4190#if (defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_MSWIN)) || defined(PROTO)
4191/*
4192 * The 'toolbar' option is changed.
4193 */
4194 char *
4195did_set_toolbar(optset_T *args UNUSED)
4196{
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004197 if (opt_strings_flags(p_toolbar, p_toolbar_values, &toolbar_flags,
4198 TRUE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004199 return e_invalid_argument;
4200
4201 out_flush();
4202 gui_mch_show_toolbar((toolbar_flags &
4203 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4204 return NULL;
4205}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004206
4207 int
4208expand_set_toolbar(optexpand_T *args, int *numMatches, char_u ***matches)
4209{
4210 return expand_set_opt_string(
4211 args,
4212 p_toolbar_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004213 ARRAY_LENGTH(p_toolbar_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004214 numMatches,
4215 matches);
4216}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004217#endif
4218
4219#if (defined(FEAT_TOOLBAR) && defined(FEAT_GUI_GTK)) || defined(PROTO)
4220/*
4221 * The 'toolbariconsize' option is changed. GTK+ 2 only.
4222 */
4223 char *
4224did_set_toolbariconsize(optset_T *args UNUSED)
4225{
4226 if (opt_strings_flags(p_tbis, p_tbis_values, &tbis_flags, FALSE) != OK)
4227 return e_invalid_argument;
4228
4229 out_flush();
4230 gui_mch_show_toolbar((toolbar_flags &
4231 (TOOLBAR_TEXT | TOOLBAR_ICONS)) != 0);
4232 return NULL;
4233}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004234
4235 int
4236expand_set_toolbariconsize(optexpand_T *args, int *numMatches, char_u ***matches)
4237{
4238 return expand_set_opt_string(
4239 args,
4240 p_tbis_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004241 ARRAY_LENGTH(p_tbis_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004242 numMatches,
4243 matches);
4244}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004245#endif
4246
4247#if defined(UNIX) || defined(VMS) || defined(PROTO)
4248/*
4249 * The 'ttymouse' option is changed.
4250 */
4251 char *
4252did_set_ttymouse(optset_T *args UNUSED)
4253{
4254 char *errmsg = NULL;
4255
4256 // Switch the mouse off before changing the escape sequences used for
4257 // that.
4258 mch_setmouse(FALSE);
4259 if (opt_strings_flags(p_ttym, p_ttym_values, &ttym_flags, FALSE) != OK)
4260 errmsg = e_invalid_argument;
4261 else
4262 check_mouse_termcode();
4263 if (termcap_active)
4264 setmouse(); // may switch it on again
4265
4266 return errmsg;
4267}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004268
4269 int
4270expand_set_ttymouse(optexpand_T *args, int *numMatches, char_u ***matches)
4271{
4272 return expand_set_opt_string(
4273 args,
4274 p_ttym_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004275 ARRAY_LENGTH(p_ttym_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004276 numMatches,
4277 matches);
4278}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004279#endif
4280
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004281#if defined(FEAT_VARTABS) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004282/*
4283 * The 'varsofttabstop' option is changed.
4284 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004285 char *
4286did_set_varsofttabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004287{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004288 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004289 char_u *cp;
4290
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004291 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004292 VIM_CLEAR(curbuf->b_p_vsts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004293 else
4294 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004295 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004296 {
4297 if (vim_isdigit(*cp))
4298 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004299 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004300 continue;
4301 return e_invalid_argument;
4302 }
4303
4304 int *oldarray = curbuf->b_p_vsts_array;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004305 if (tabstop_set(*varp, &(curbuf->b_p_vsts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004306 {
4307 if (oldarray)
4308 vim_free(oldarray);
4309 }
4310 else
4311 return e_invalid_argument;
4312 }
4313
4314 return NULL;
4315}
4316
4317/*
4318 * The 'vartabstop' option is changed.
4319 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004320 char *
4321did_set_vartabstop(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004322{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004323 char_u **varp = (char_u **)args->os_varp;
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004324 char_u *cp;
4325
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004326 if (!((*varp)[0]) || ((*varp)[0] == '0' && !((*varp)[1])))
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00004327 VIM_CLEAR(curbuf->b_p_vts_array);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004328 else
4329 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004330 for (cp = *varp; *cp; ++cp)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004331 {
4332 if (vim_isdigit(*cp))
4333 continue;
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004334 if (*cp == ',' && cp > *varp && *(cp-1) != ',')
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004335 continue;
4336 return e_invalid_argument;
4337 }
4338
4339 int *oldarray = curbuf->b_p_vts_array;
4340
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004341 if (tabstop_set(*varp, &(curbuf->b_p_vts_array)) == OK)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004342 {
4343 vim_free(oldarray);
4344# ifdef FEAT_FOLDING
4345 if (foldmethodIsIndent(curwin))
4346 foldUpdateAll(curwin);
4347# endif
4348 }
4349 else
4350 return e_invalid_argument;
4351 }
4352
4353 return NULL;
4354}
4355#endif
4356
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004357/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004358 * The 'verbosefile' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004359 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004360 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004361did_set_verbosefile(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004362{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004363 verbose_stop();
4364 if (*p_vfile != NUL && verbose_open() == FAIL)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004365 return e_invalid_argument;
4366
4367 return NULL;
4368}
4369
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004370#if defined(FEAT_SESSION) || defined(PROTO)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004371/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004372 * The 'viewoptions' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004373 */
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004374 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004375did_set_viewoptions(optset_T *args UNUSED)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004376{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004377 return did_set_opt_flags(p_vop, p_ssop_values, &vop_flags, TRUE);
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004378}
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004379#endif
4380
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004381#if defined(FEAT_VIMINFO) || defined(PROTO)
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004382/*
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004383 * The 'viminfo' option is changed.
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004384 */
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004385 char *
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004386did_set_viminfo(optset_T *args)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004387{
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004388 char_u *s;
4389 char *errmsg = NULL;
4390
4391 for (s = p_viminfo; *s;)
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004392 {
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004393 // Check it's a valid character
4394 if (vim_strchr((char_u *)"!\"%'/:<@cfhnrs", *s) == NULL)
4395 {
Christian Brabandtb39b2402023-11-29 11:34:05 +01004396 errmsg = illegal_char(args->os_errbuf, args->os_errbuflen, *s);
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004397 break;
4398 }
4399 if (*s == 'n') // name is always last one
4400 break;
4401 else if (*s == 'r') // skip until next ','
4402 {
4403 while (*++s && *s != ',')
4404 ;
4405 }
4406 else if (*s == '%')
4407 {
4408 // optional number
4409 while (vim_isdigit(*++s))
4410 ;
4411 }
4412 else if (*s == '!' || *s == 'h' || *s == 'c')
4413 ++s; // no extra chars
4414 else // must have a number
4415 {
4416 while (vim_isdigit(*++s))
4417 ;
4418
4419 if (!VIM_ISDIGIT(*(s - 1)))
4420 {
4421 if (args->os_errbuf != NULL)
4422 {
Zoltan Arpadffy1c8e2332023-12-05 16:04:23 +01004423 vim_snprintf(args->os_errbuf, args->os_errbuflen,
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004424 _(e_missing_number_after_angle_str_angle),
4425 transchar_byte(*(s - 1)));
4426 errmsg = args->os_errbuf;
4427 }
4428 else
4429 errmsg = "";
4430 break;
4431 }
4432 }
4433 if (*s == ',')
4434 ++s;
4435 else if (*s)
4436 {
4437 if (args->os_errbuf != NULL)
4438 errmsg = e_missing_comma;
4439 else
4440 errmsg = "";
4441 break;
4442 }
4443 }
4444 if (*p_viminfo && errmsg == NULL && get_viminfo_parameter('\'') < 0)
4445 errmsg = e_must_specify_a_value;
4446
4447 return errmsg;
4448}
4449#endif
4450
4451/*
4452 * The 'virtualedit' option is changed.
4453 */
4454 char *
4455did_set_virtualedit(optset_T *args)
4456{
4457 char_u *ve = p_ve;
4458 unsigned int *flags = &ve_flags;
4459
4460 if (args->os_flags & OPT_LOCAL)
4461 {
4462 ve = curwin->w_p_ve;
4463 flags = &curwin->w_ve_flags;
4464 }
4465
4466 if ((args->os_flags & OPT_LOCAL) && *ve == NUL)
4467 // make the local value empty: use the global value
4468 *flags = 0;
4469 else
4470 {
4471 if (opt_strings_flags(ve, p_ve_values, flags, TRUE) != OK)
4472 return e_invalid_argument;
4473 else if (STRCMP(ve, args->os_oldval.string) != 0)
4474 {
4475 // Recompute cursor position in case the new 've' setting
4476 // changes something.
4477 validate_virtcol();
4478 coladvance(curwin->w_virtcol);
4479 }
Yegappan Lakshmananf2e30d02023-01-30 13:04:42 +00004480 }
4481
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004482 return NULL;
4483}
4484
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004485 int
4486expand_set_virtualedit(optexpand_T *args, int *numMatches, char_u ***matches)
4487{
4488 return expand_set_opt_string(
4489 args,
4490 p_ve_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004491 ARRAY_LENGTH(p_ve_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004492 numMatches,
4493 matches);
4494}
4495
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004496/*
4497 * The 'whichwrap' option is changed.
4498 */
4499 char *
4500did_set_whichwrap(optset_T *args)
4501{
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004502 char_u **varp = (char_u **)args->os_varp;
4503
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004504 // Add ',' to the list flags because 'whichwrap' is a flag
4505 // list that is comma-separated.
Christian Brabandtb39b2402023-11-29 11:34:05 +01004506 return did_set_option_listflag(*varp, (char_u *)(WW_ALL ","),
4507 args->os_errbuf, args->os_errbuflen);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004508}
4509
4510 int
4511expand_set_whichwrap(optexpand_T *args, int *numMatches, char_u ***matches)
4512{
4513 return expand_set_opt_listflag(args, (char_u*)WW_ALL, numMatches, matches);
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004514}
4515
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004516/*
4517 * The 'wildmode' option is changed.
4518 */
4519 char *
4520did_set_wildmode(optset_T *args UNUSED)
4521{
4522 if (check_opt_wim() == FAIL)
4523 return e_invalid_argument;
4524 return NULL;
4525}
4526
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004527 int
4528expand_set_wildmode(optexpand_T *args, int *numMatches, char_u ***matches)
4529{
4530 return expand_set_opt_string(
4531 args,
4532 p_wim_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004533 ARRAY_LENGTH(p_wim_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004534 numMatches,
4535 matches);
4536}
4537
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004538/*
4539 * The 'wildoptions' option is changed.
4540 */
4541 char *
4542did_set_wildoptions(optset_T *args UNUSED)
4543{
4544 return did_set_opt_strings(p_wop, p_wop_values, TRUE);
4545}
4546
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004547 int
4548expand_set_wildoptions(optexpand_T *args, int *numMatches, char_u ***matches)
4549{
4550 return expand_set_opt_string(
4551 args,
4552 p_wop_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004553 ARRAY_LENGTH(p_wop_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004554 numMatches,
4555 matches);
4556}
4557
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004558#if defined(FEAT_WAK) || defined(PROTO)
4559/*
4560 * The 'winaltkeys' option is changed.
4561 */
4562 char *
4563did_set_winaltkeys(optset_T *args UNUSED)
4564{
4565 char *errmsg = NULL;
4566
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004567 if (*p_wak == NUL || check_opt_strings(p_wak, p_wak_values, FALSE) != OK)
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004568 errmsg = e_invalid_argument;
4569# ifdef FEAT_MENU
4570# if defined(FEAT_GUI_MOTIF)
4571 else if (gui.in_use)
4572 gui_motif_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4573# elif defined(FEAT_GUI_GTK)
4574 else if (gui.in_use)
4575 gui_gtk_set_mnemonics(p_wak[0] == 'y' || p_wak[0] == 'm');
4576# endif
4577# endif
4578 return errmsg;
4579}
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004580
4581 int
4582expand_set_winaltkeys(optexpand_T *args, int *numMatches, char_u ***matches)
4583{
4584 return expand_set_opt_string(
4585 args,
4586 p_wak_values,
Yee Cheng Chin6d113472023-10-02 21:38:39 +02004587 ARRAY_LENGTH(p_wak_values) - 1,
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004588 numMatches,
4589 matches);
4590}
Yegappan Lakshmananad608982023-03-01 12:44:06 +00004591#endif
4592
4593/*
4594 * The 'wincolor' option is changed.
4595 */
4596 char *
4597did_set_wincolor(optset_T *args UNUSED)
4598{
4599#ifdef FEAT_TERMINAL
4600 term_update_wincolor(curwin);
4601#endif
4602 return NULL;
4603}
4604
Yee Cheng Chin900894b2023-09-29 20:42:32 +02004605 int
4606expand_set_wincolor(optexpand_T *args, int *numMatches, char_u ***matches)
4607{
4608 return expand_set_opt_generic(
4609 args,
4610 get_highlight_name,
4611 numMatches,
4612 matches);
4613}
4614
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004615#ifdef FEAT_SYN_HL
4616/*
4617 * When the 'syntax' option is set, load the syntax of that name.
4618 */
4619 static void
4620do_syntax_autocmd(int value_changed)
4621{
4622 static int syn_recursive = 0;
4623
4624 ++syn_recursive;
4625 // Only pass TRUE for "force" when the value changed or not used
4626 // recursively, to avoid endless recurrence.
4627 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, curbuf->b_fname,
4628 value_changed || syn_recursive == 1, curbuf);
4629 curbuf->b_flags |= BF_SYN_SET;
4630 --syn_recursive;
4631}
4632#endif
4633
4634/*
4635 * When the 'filetype' option is set, trigger the FileType autocommand.
4636 */
4637 static void
4638do_filetype_autocmd(char_u **varp, int opt_flags, int value_changed)
4639{
4640 // Skip this when called from a modeline and the filetype was already set
4641 // to this value.
4642 if ((opt_flags & OPT_MODELINE) && !value_changed)
4643 return;
4644
4645 static int ft_recursive = 0;
4646 int secure_save = secure;
4647
4648 // Reset the secure flag, since the value of 'filetype' has
4649 // been checked to be safe.
4650 secure = 0;
4651
4652 ++ft_recursive;
zeertzjq5bf6c212024-03-31 18:41:27 +02004653 curbuf->b_did_filetype = TRUE;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004654 // Only pass TRUE for "force" when the value changed or not
4655 // used recursively, to avoid endless recurrence.
4656 apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname,
4657 value_changed || ft_recursive == 1, curbuf);
4658 --ft_recursive;
4659 // Just in case the old "curbuf" is now invalid.
4660 if (varp != &(curbuf->b_p_ft))
4661 varp = NULL;
4662
4663 secure = secure_save;
4664}
4665
4666#ifdef FEAT_SPELL
4667/*
4668 * When the 'spelllang' option is set, source the spell/LANG.vim file in
4669 * 'runtimepath'.
4670 */
4671 static void
4672do_spelllang_source(void)
4673{
4674 char_u fname[200];
4675 char_u *p;
4676 char_u *q = curwin->w_s->b_p_spl;
4677
4678 // Skip the first name if it is "cjk".
4679 if (STRNCMP(q, "cjk,", 4) == 0)
4680 q += 4;
4681
4682 // They could set 'spellcapcheck' depending on the language. Use the first
4683 // name in 'spelllang' up to '_region' or '.encoding'.
4684 for (p = q; *p != NUL; ++p)
4685 if (!ASCII_ISALNUM(*p) && *p != '-')
4686 break;
4687 if (p > q)
4688 {
4689 vim_snprintf((char *)fname, 200, "spell/%.*s.vim",
4690 (int)(p - q), q);
4691 source_runtime(fname, DIP_ALL);
4692 }
4693}
4694#endif
4695
4696/*
Bram Moolenaardac13472019-09-16 21:06:21 +02004697 * Handle string options that need some action to perform when changed.
zeertzjqf6782732022-07-27 18:26:03 +01004698 * The new value must be allocated.
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004699 * Returns NULL for success, or an untranslated error message for an error.
Bram Moolenaardac13472019-09-16 21:06:21 +02004700 */
4701 char *
4702did_set_string_option(
4703 int opt_idx, // index in options[] table
4704 char_u **varp, // pointer to the option variable
Bram Moolenaardac13472019-09-16 21:06:21 +02004705 char_u *oldval, // previous value of the option
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004706 char_u *value, // new value of the option
Bram Moolenaardac13472019-09-16 21:06:21 +02004707 char *errbuf, // buffer for errors, or NULL
Mike Williams620f0112023-12-05 15:36:06 +01004708 size_t errbuflen, // length of error buffer
Bram Moolenaardac13472019-09-16 21:06:21 +02004709 int opt_flags, // OPT_LOCAL and/or OPT_GLOBAL
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004710 set_op_T op, // OP_ADDING/OP_PREPENDING/OP_REMOVING
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004711 int *value_checked) // value was checked to be safe, no
Bram Moolenaardac13472019-09-16 21:06:21 +02004712 // need to set P_INSECURE
4713{
4714 char *errmsg = NULL;
Bram Moolenaardac13472019-09-16 21:06:21 +02004715 long_u free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004716 opt_did_set_cb_T did_set_cb = get_option_did_set_cb(opt_idx);
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004717 optset_T args;
4718
4719 // 'ttytype' is an alias for 'term'. Both 'term' and 'ttytype' point to
4720 // T_NAME. If 'term' or 'ttytype' is modified, then use the index for the
4721 // 'term' option. Only set the P_ALLOCED flag on 'term'.
4722 if (varp == &T_NAME)
4723 {
4724 opt_idx = findoption((char_u *)"term");
4725 if (opt_idx >= 0)
4726 {
4727 free_oldval = (get_option_flags(opt_idx) & P_ALLOCED);
4728 did_set_cb = get_option_did_set_cb(opt_idx);
4729 }
4730 }
4731
4732 CLEAR_FIELD(args);
Bram Moolenaardac13472019-09-16 21:06:21 +02004733
Bram Moolenaardac13472019-09-16 21:06:21 +02004734 // Disallow changing some options from secure mode
4735 if ((secure
4736#ifdef HAVE_SANDBOX
4737 || sandbox != 0
4738#endif
4739 ) && (get_option_flags(opt_idx) & P_SECURE))
Bram Moolenaar74409f62022-01-01 15:58:22 +00004740 errmsg = e_not_allowed_here;
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004741 // Check for a "normal" directory or file name in some options.
4742 else if (check_illegal_path_names(opt_idx, varp))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004743 errmsg = e_invalid_argument;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004744 else if (did_set_cb != NULL)
4745 {
Yegappan Lakshmananc727b192023-03-03 12:26:15 +00004746 args.os_varp = (char_u *)varp;
4747 args.os_idx = opt_idx;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004748 args.os_flags = opt_flags;
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +02004749 args.os_op = op;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004750 args.os_oldval.string = oldval;
4751 args.os_newval.string = value;
Yegappan Lakshmanan6d611de2023-02-25 11:59:33 +00004752 args.os_errbuf = errbuf;
Christian Brabandtb39b2402023-11-29 11:34:05 +01004753 args.os_errbuflen = errbuflen;
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004754 // Invoke the option specific callback function to validate and apply
4755 // the new option value.
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004756 errmsg = did_set_cb(&args);
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004757
Yegappan Lakshmanan5da901b2023-02-27 12:47:47 +00004758 // The 'keymap', 'filetype' and 'syntax' option callback functions
4759 // may change the os_value_checked field.
4760 *value_checked = args.os_value_checked;
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004761 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004762
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004763 // If an error is detected, restore the previous value.
Bram Moolenaardac13472019-09-16 21:06:21 +02004764 if (errmsg != NULL)
4765 {
zeertzjqf6782732022-07-27 18:26:03 +01004766 free_string_option(*varp);
Bram Moolenaardac13472019-09-16 21:06:21 +02004767 *varp = oldval;
4768 // When resetting some values, need to act on it.
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004769 if (args.os_restore_chartab)
Bram Moolenaardac13472019-09-16 21:06:21 +02004770 (void)init_chartab();
4771 if (varp == &p_hl)
4772 (void)highlight_changed();
4773 }
4774 else
4775 {
4776#ifdef FEAT_EVAL
4777 // Remember where the option was set.
4778 set_option_sctx_idx(opt_idx, opt_flags, current_sctx);
4779#endif
4780 // Free string options that are in allocated memory.
4781 // Use "free_oldval", because recursiveness may change the flags under
4782 // our fingers (esp. init_highlight()).
4783 if (free_oldval)
4784 free_string_option(oldval);
zeertzjqf6782732022-07-27 18:26:03 +01004785 set_option_flag(opt_idx, P_ALLOCED);
Bram Moolenaardac13472019-09-16 21:06:21 +02004786
4787 if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0
4788 && is_global_local_option(opt_idx))
4789 {
4790 // global option with local value set to use global value; free
4791 // the local value and make it empty
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004792 char_u *p = get_option_varp_scope(opt_idx, OPT_LOCAL);
Bram Moolenaardac13472019-09-16 21:06:21 +02004793 free_string_option(*(char_u **)p);
4794 *(char_u **)p = empty_option;
4795 }
4796
4797 // May set global value for local option.
4798 else if (!(opt_flags & OPT_LOCAL) && opt_flags != OPT_GLOBAL)
4799 set_string_option_global(opt_idx, varp);
4800
4801 // Trigger the autocommand only after setting the flags.
4802#ifdef FEAT_SYN_HL
Bram Moolenaardac13472019-09-16 21:06:21 +02004803 if (varp == &(curbuf->b_p_syn))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004804 do_syntax_autocmd(args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004805#endif
4806 else if (varp == &(curbuf->b_p_ft))
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004807 do_filetype_autocmd(varp, opt_flags, args.os_value_changed);
Bram Moolenaardac13472019-09-16 21:06:21 +02004808#ifdef FEAT_SPELL
4809 if (varp == &(curwin->w_s->b_p_spl))
Yegappan Lakshmanand6e4c752023-01-31 13:25:58 +00004810 do_spelllang_source();
Bram Moolenaardac13472019-09-16 21:06:21 +02004811#endif
4812 }
4813
Bram Moolenaardac13472019-09-16 21:06:21 +02004814 if (varp == &p_mouse)
4815 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004816 if (*p_mouse == NUL)
4817 mch_setmouse(FALSE); // switch mouse off
4818 else
Bram Moolenaardac13472019-09-16 21:06:21 +02004819 setmouse(); // in case 'mouse' changed
4820 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004821
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004822 if (varp == &p_rtp)
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004823 {
4824 export_myvimdir();
4825#if defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004826 update_package_paths_in_lua();
4827#endif
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004828 }
Bram Moolenaar788fbb42020-05-31 14:08:12 +02004829
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004830#if defined(FEAT_LINEBREAK)
4831 // Changing Formatlistpattern when briopt includes the list setting:
4832 // redraw
4833 if ((varp == &p_flp || varp == &(curbuf->b_p_flp))
4834 && curwin->w_briopt_list)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004835 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb2d85e32022-01-07 16:55:32 +00004836#endif
4837
Bram Moolenaardac13472019-09-16 21:06:21 +02004838 if (curwin->w_curswant != MAXCOL
zeertzjqfcaed6a2024-02-18 09:33:54 +01004839 && (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0
4840 && (get_option_flags(opt_idx) & P_HLONLY) == 0)
Bram Moolenaardac13472019-09-16 21:06:21 +02004841 curwin->w_set_curswant = TRUE;
4842
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004843 if ((opt_flags & OPT_NO_REDRAW) == 0)
4844 {
Bram Moolenaardac13472019-09-16 21:06:21 +02004845#ifdef FEAT_GUI
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004846 // set when changing an option that only requires a redraw in the GUI
4847 int redraw_gui_only = FALSE;
4848
4849 if (varp == &p_go // 'guioptions'
4850 || varp == &p_guifont // 'guifont'
4851# ifdef FEAT_GUI_TABLINE
4852 || varp == &p_gtl // 'guitablabel'
4853 || varp == &p_gtt // 'guitabtooltip'
4854# endif
4855# ifdef FEAT_XFONTSET
4856 || varp == &p_guifontset // 'guifontset'
4857# endif
4858 || varp == &p_guifontwide // 'guifontwide'
Erik S. V. Jansson2f026382024-02-26 22:23:05 +01004859# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004860 || varp == &p_guiligatures // 'guiligatures'
4861# endif
4862 )
4863 redraw_gui_only = TRUE;
4864
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004865 // check redraw when it's not a GUI option or the GUI is active.
4866 if (!redraw_gui_only || gui.in_use)
Bram Moolenaardac13472019-09-16 21:06:21 +02004867#endif
Bram Moolenaar37294bd2021-03-10 13:40:08 +01004868 check_redraw(get_option_flags(opt_idx));
4869 }
Bram Moolenaardac13472019-09-16 21:06:21 +02004870
4871#if defined(FEAT_VTP) && defined(FEAT_TERMGUICOLORS)
Yegappan Lakshmanan5284b232023-03-04 19:57:32 +00004872 if (args.os_did_swaptcap)
Bram Moolenaardac13472019-09-16 21:06:21 +02004873 {
4874 set_termname((char_u *)"win32");
4875 init_highlight(TRUE, FALSE);
4876 }
4877#endif
4878
4879 return errmsg;
4880}
4881
4882/*
4883 * Check an option that can be a range of string values.
4884 *
4885 * Return OK for correct value, FAIL otherwise.
4886 * Empty is always OK.
4887 */
4888 static int
4889check_opt_strings(
4890 char_u *val,
4891 char **values,
4892 int list) // when TRUE: accept a list of values
4893{
4894 return opt_strings_flags(val, values, NULL, list);
4895}
4896
4897/*
4898 * Handle an option that can be a range of string values.
4899 * Set a flag in "*flagp" for each string present.
4900 *
4901 * Return OK for correct value, FAIL otherwise.
4902 * Empty is always OK.
4903 */
4904 static int
4905opt_strings_flags(
4906 char_u *val, // new value
4907 char **values, // array of valid string values
4908 unsigned *flagp,
4909 int list) // when TRUE: accept a list of values
4910{
4911 int i;
4912 int len;
4913 unsigned new_flags = 0;
4914
4915 while (*val)
4916 {
4917 for (i = 0; ; ++i)
4918 {
4919 if (values[i] == NULL) // val not found in values[]
4920 return FAIL;
4921
4922 len = (int)STRLEN(values[i]);
4923 if (STRNCMP(values[i], val, len) == 0
4924 && ((list && val[len] == ',') || val[len] == NUL))
4925 {
4926 val += len + (val[len] == ',');
4927 new_flags |= (1 << i);
4928 break; // check next item in val list
4929 }
4930 }
4931 }
4932 if (flagp != NULL)
4933 *flagp = new_flags;
4934
4935 return OK;
4936}
4937
4938/*
4939 * return OK if "p" is a valid fileformat name, FAIL otherwise.
4940 */
4941 int
4942check_ff_value(char_u *p)
4943{
4944 return check_opt_strings(p, p_ff_values, FALSE);
4945}
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004946
4947/*
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00004948 * Save the actual shortmess Flags and clear them temporarily to avoid that
4949 * file messages overwrites any output from the following commands.
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004950 *
4951 * Caller must make sure to first call save_clear_shm_value() and then
4952 * restore_shm_value() exactly the same number of times.
4953 */
4954 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004955save_clear_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004956{
4957 if (STRLEN(p_shm) >= SHM_LEN)
4958 {
4959 iemsg(e_internal_error_shortmess_too_long);
4960 return;
4961 }
4962
4963 if (++set_shm_recursive == 1)
4964 {
4965 STRCPY(shm_buf, p_shm);
4966 set_option_value_give_err((char_u *)"shm", 0L, (char_u *)"", 0);
4967 }
4968}
4969
4970/*
4971 * Restore the shortmess Flags set from the save_clear_shm_value() function.
4972 */
4973 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004974restore_shm_value(void)
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004975{
4976 if (--set_shm_recursive == 0)
4977 {
4978 set_option_value_give_err((char_u *)"shm", 0L, shm_buf, 0);
4979 vim_memset(shm_buf, 0, SHM_LEN);
4980 }
4981}
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004982
4983/*
4984 * Export the environment variable $MYVIMDIR to the first item in runtimepath
4985 */
Christian Brabandt1a741d32025-03-01 16:30:33 +01004986 void
Christian Brabandt3e2affc2025-02-28 17:34:46 +01004987export_myvimdir()
4988{
4989 int dofree = FALSE;
4990 char_u *p;
4991 char_u *q = p_rtp;
4992 char_u *buf = alloc(MAXPATHL);
4993
4994 if (buf == NULL)
4995 return;
4996
4997 (void)copy_option_part(&q, buf, MAXPATHL, ",");
4998
4999 p = vim_getenv((char_u *)"MYVIMDIR", &dofree);
5000
5001 if (p == NULL || STRCMP(p, buf) != 0)
5002 {
5003 add_pathsep(buf);
5004#ifdef MSWIN
5005 // normalize path separators
5006 for (q = buf; *q != NUL; q++)
5007 if (*q == '/')
5008 *q = '\\';
5009#endif
5010 vim_setenv((char_u *)"MYVIMDIR", buf);
5011 }
5012 if (dofree)
5013 vim_free(p);
5014 vim_free(buf);
5015}